xref: /netbsd-src/sys/dev/pad/pad.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /* $NetBSD: pad.c,v 1.2 2007/11/11 19:53:38 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by Jared D. McNeill.
18  * 4. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.2 2007/11/11 19:53:38 jmcneill Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/conf.h>
41 #include <sys/buf.h>
42 #include <sys/kmem.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46 #include <sys/condvar.h>
47 #include <sys/select.h>
48 #include <sys/audioio.h>
49 
50 #include <dev/audio_if.h>
51 #include <dev/audiovar.h>
52 #include <dev/auconv.h>
53 
54 #include <dev/pad/padvar.h>
55 #include <dev/pad/padvol.h>
56 
57 #define PADUNIT(x)	minor(x)
58 
59 extern struct cfdriver pad_cd;
60 
61 static struct audio_device pad_device = {
62 	"Pseudo Audio",
63 	"1.0",
64 	"pad",
65 };
66 
67 typedef struct pad_block {
68 	uint8_t		*pb_ptr;
69 	int		pb_len;
70 } pad_block_t;
71 
72 enum {
73 	PAD_OUTPUT_CLASS,
74 	PAD_INPUT_CLASS,
75 	PAD_OUTPUT_MASTER_VOLUME,
76 	PAD_INPUT_DAC_VOLUME,
77 	PAD_ENUM_LAST,
78 };
79 
80 static int	pad_match(struct device *, struct cfdata *, void *);
81 static void	pad_attach(struct device *, struct device *, void *);
82 
83 static int	pad_query_encoding(void *, struct audio_encoding *);
84 static int	pad_set_params(void *, int, int,
85 				audio_params_t *, audio_params_t *,
86 				stream_filter_list_t *, stream_filter_list_t *);
87 static int	pad_start_output(void *, void *, int,
88 				    void (*)(void *), void *);
89 static int	pad_start_input(void *, void *, int,
90 				   void (*)(void *), void *);
91 static int	pad_halt_output(void *);
92 static int	pad_halt_input(void *);
93 static int	pad_getdev(void *, struct audio_device *);
94 static int	pad_set_port(void *, mixer_ctrl_t *);
95 static int	pad_get_port(void *, mixer_ctrl_t *);
96 static int	pad_query_devinfo(void *, mixer_devinfo_t *);
97 static int	pad_get_props(void *);
98 static int	pad_round_blocksize(void *, int, int, const audio_params_t *);
99 
100 static const struct audio_hw_if pad_hw_if = {
101 	.query_encoding = pad_query_encoding,
102 	.set_params = pad_set_params,
103 	.start_output = pad_start_output,
104 	.start_input = pad_start_input,
105 	.halt_output = pad_halt_output,
106 	.halt_input = pad_halt_input,
107 	.getdev = pad_getdev,
108 	.set_port = pad_set_port,
109 	.get_port = pad_get_port,
110 	.query_devinfo = pad_query_devinfo,
111 	.get_props = pad_get_props,
112 	.round_blocksize = pad_round_blocksize,
113 };
114 
115 #define PAD_NFORMATS	1
116 static const struct audio_format pad_formats[PAD_NFORMATS] = {
117 	{ NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
118 	  2, AUFMT_STEREO, 1, { 44100 } },
119 };
120 
121 extern void	padattach(int);
122 
123 static pad_softc_t *	pad_find_softc(dev_t);
124 static int		pad_add_block(pad_softc_t *, uint8_t *, int);
125 static int		pad_get_block(pad_softc_t *, pad_block_t *, int);
126 
127 dev_type_open(pad_open);
128 dev_type_close(pad_close);
129 dev_type_read(pad_read);
130 
131 const struct cdevsw pad_cdevsw = {
132 	.d_open = pad_open,
133 	.d_close = pad_close,
134 	.d_read = pad_read,
135 	.d_write = nowrite,
136 	.d_ioctl = noioctl,
137 	.d_stop = nostop,
138 	.d_tty = notty,
139 	.d_poll = nopoll,
140 	.d_mmap = nommap,
141 	.d_kqfilter = nokqfilter,
142 	.d_flag = D_OTHER,
143 };
144 
145 CFATTACH_DECL(pad, sizeof(pad_softc_t), pad_match, pad_attach, NULL, NULL);
146 
147 void
148 padattach(int n)
149 {
150 	int i, err;
151 	struct cfdata *cf;
152 
153 #ifdef DEBUG
154 	printf("pad: requested %d units\n", n);
155 #endif
156 
157 	err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
158 	if (err) {
159 		aprint_error("%s: couldn't register cfattach: %d\n",
160 		    pad_cd.cd_name, err);
161 		config_cfdriver_detach(&pad_cd);
162 		return;
163 	}
164 
165 	for (i = 0; i < n; i++) {
166 		cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
167 		if (cf == NULL) {
168 			aprint_error("%s: couldn't allocate cfdata\n",
169 			    pad_cd.cd_name);
170 			continue;
171 		}
172 		cf->cf_name = pad_cd.cd_name;
173 		cf->cf_atname = pad_cd.cd_name;
174 		cf->cf_unit = i;
175 		cf->cf_fstate = FSTATE_STAR;
176 
177 		(void)config_attach_pseudo(cf);
178 	}
179 
180 	return;
181 }
182 
183 static pad_softc_t *
184 pad_find_softc(dev_t dev)
185 {
186 	int unit;
187 
188 	unit = PADUNIT(dev);
189 	if (unit >= pad_cd.cd_ndevs)
190 		return NULL;
191 
192 	return pad_cd.cd_devs[unit];
193 }
194 
195 static int
196 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
197 {
198 	int l;
199 
200 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
201 		return ENOBUFS;
202 
203 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
204 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
205 	else {
206 		l = PAD_BUFSIZE - sc->sc_wpos;
207 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
208 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
209 	}
210 
211 	sc->sc_wpos += blksize;
212 	if (sc->sc_wpos > PAD_BUFSIZE)
213 		sc->sc_wpos -= PAD_BUFSIZE;
214 
215 	sc->sc_buflen += blksize;
216 
217 	return 0;
218 }
219 
220 static int
221 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
222 {
223 	int l;
224 
225 	KASSERT(pb != NULL);
226 
227 	if (sc->sc_buflen < blksize)
228 		return ERESTART;
229 
230 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
231 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
232 		pb->pb_len = blksize;
233 		sc->sc_rpos += blksize;
234 	} else {
235 		l = PAD_BUFSIZE - sc->sc_rpos;
236 		pb->pb_len = l;
237 		sc->sc_rpos = 0;
238 	}
239 	sc->sc_buflen -= pb->pb_len;
240 
241 	return 0;
242 }
243 
244 static int
245 pad_match(struct device *parent, struct cfdata *data, void *opaque)
246 {
247 	return 1;
248 }
249 
250 static void
251 pad_attach(struct device *parent, struct device *self, void *opaque)
252 {
253 	pad_softc_t *sc;
254 
255 	sc = (pad_softc_t *)self;
256 
257 	aprint_normal_dev(self, "Pseudo Audio Device\n");
258 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
259 
260 	sc->sc_open = 0;
261 	if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
262 	    &sc->sc_encodings) != 0) {
263 		aprint_error_dev(self, "couldn't create encodings\n");
264 		return;
265 	}
266 
267 	cv_init(&sc->sc_condvar, device_xname(self));
268 	mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_AUDIO);
269 
270 	sc->sc_swvol = 255;
271 	sc->sc_buflen = 0;
272 	sc->sc_rpos = sc->sc_wpos = 0;
273 	sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
274 
275 	return;
276 }
277 
278 int
279 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
280 {
281 	pad_softc_t *sc;
282 
283 	sc = pad_find_softc(dev);
284 	if (sc == NULL)
285 		return ENODEV;
286 
287 	if (sc->sc_open++) {
288 		sc->sc_open--;
289 		return EBUSY;
290 	}
291 
292 	return 0;
293 }
294 
295 int
296 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
297 {
298 	pad_softc_t *sc;
299 
300 	sc = pad_find_softc(dev);
301 	if (sc == NULL)
302 		return ENODEV;
303 
304 	KASSERT(sc->sc_open > 0);
305 	sc->sc_open--;
306 
307 	return 0;
308 }
309 
310 int
311 pad_read(dev_t dev, struct uio *uio, int flags)
312 {
313 	pad_softc_t *sc;
314 	pad_block_t pb;
315 	void (*intr)(void *);
316 	void *intrarg;
317 	int err;
318 
319 	sc = pad_find_softc(dev);
320 	if (sc == NULL)
321 		return ENODEV;
322 
323 	err = 0;
324 
325 	intr = sc->sc_intr;
326 	intrarg = sc->sc_intrarg;
327 
328 	while (uio->uio_resid > 0 && !err) {
329 		err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
330 		if (!err)
331 			err = uiomove(pb.pb_ptr, pb.pb_len, uio);
332 		else {
333 			if (intr) {
334 				(*intr)(intrarg);
335 				intr = sc->sc_intr;
336 				intrarg = sc->sc_intrarg;
337 				err = 0;
338 				continue;
339 			}
340 
341 			mutex_enter(&sc->sc_mutex);
342 			err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
343 			    hz/100);
344 			if (err != 0 && err != EWOULDBLOCK) {
345 				mutex_exit(&sc->sc_mutex);
346 				aprint_error_dev(&sc->sc_dev,
347 				    "cv_timedwait_sig returned %d\n", err);
348 				return EINTR;
349 			}
350 			intr = sc->sc_intr;
351 			intrarg = sc->sc_intrarg;
352 			mutex_exit(&sc->sc_mutex);
353 			err = 0;
354 		}
355 	}
356 
357 	if (intr)
358 		(*intr)(intrarg);
359 
360 	return err;
361 }
362 
363 static int
364 pad_query_encoding(void *opaque, struct audio_encoding *ae)
365 {
366 	pad_softc_t *sc;
367 
368 	sc = (pad_softc_t *)opaque;
369 
370 	return auconv_query_encoding(sc->sc_encodings, ae);
371 }
372 
373 static int
374 pad_set_params(void *opaque, int setmode, int usemode,
375     audio_params_t *play, audio_params_t *rec,
376     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
377 {
378 	pad_softc_t *sc;
379 
380 	sc = (pad_softc_t *)opaque;
381 
382 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
383 	    play, true, pfil) < 0)
384 		return EINVAL;
385 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
386 	    rec, true, rfil) < 0)
387 		return EINVAL;
388 
389 	if (pfil->req_size > 0)
390 		play = &pfil->filters[0].param;
391 	switch (play->encoding) {
392 	case AUDIO_ENCODING_SLINEAR_LE:
393 		if (play->precision == 16 && play->validbits == 16)
394 			pfil->prepend(pfil, pad_vol_slinear16_le, play);
395 		break;
396 	case AUDIO_ENCODING_SLINEAR_BE:
397 		if (play->precision == 16 && play->validbits == 16)
398 			pfil->prepend(pfil, pad_vol_slinear16_be, play);
399 		break;
400 	default:
401 		break;
402 	}
403 
404 	return 0;
405 }
406 
407 static int
408 pad_start_output(void *opaque, void *block, int blksize,
409     void (*intr)(void *), void *intrarg)
410 {
411 	pad_softc_t *sc;
412 	int err;
413 
414 	sc = (pad_softc_t *)opaque;
415 
416 	mutex_enter(&sc->sc_mutex);
417 
418 	sc->sc_intr = intr;
419 	sc->sc_intrarg = intrarg;
420 	sc->sc_blksize = blksize;
421 
422 	err = pad_add_block(sc, block, blksize);
423 
424 	cv_signal(&sc->sc_condvar);
425 
426 	mutex_exit(&sc->sc_mutex);
427 
428 	return err;
429 }
430 
431 static int
432 pad_start_input(void *opaque, void *block, int blksize,
433     void (*intr)(void *), void *intrarg)
434 {
435 	return EINVAL;
436 }
437 
438 static int
439 pad_halt_output(void *opaque)
440 {
441 	pad_softc_t *sc;
442 
443 	sc = (pad_softc_t *)opaque;
444 	sc->sc_intr = NULL;
445 	sc->sc_intrarg = NULL;
446 	sc->sc_buflen = 0;
447 	sc->sc_rpos = sc->sc_wpos = 0;
448 
449 	return 0;
450 }
451 
452 static int
453 pad_halt_input(void *opaque)
454 {
455 	return 0;
456 }
457 
458 static int
459 pad_getdev(void *opaque, struct audio_device *ret)
460 {
461 
462 	*ret = pad_device;
463 
464 	return 0;
465 }
466 
467 static int
468 pad_set_port(void *opaque, mixer_ctrl_t *mc)
469 {
470 	pad_softc_t *sc;
471 
472 	sc = (pad_softc_t *)opaque;
473 
474 	switch (mc->dev) {
475 	case PAD_OUTPUT_MASTER_VOLUME:
476 	case PAD_INPUT_DAC_VOLUME:
477 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
478 		return 0;
479 	}
480 
481 	return ENXIO;
482 }
483 
484 static int
485 pad_get_port(void *opaque, mixer_ctrl_t *mc)
486 {
487 	pad_softc_t *sc;
488 
489 	sc = (pad_softc_t *)opaque;
490 
491 	switch (mc->dev) {
492 	case PAD_OUTPUT_MASTER_VOLUME:
493 	case PAD_INPUT_DAC_VOLUME:
494 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
495 		return 0;
496 	}
497 
498 	return ENXIO;
499 }
500 
501 static int
502 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
503 {
504 	pad_softc_t *sc;
505 
506 	sc = (pad_softc_t *)opaque;
507 
508 	switch (di->index) {
509 	case PAD_OUTPUT_CLASS:
510 		di->mixer_class = PAD_OUTPUT_CLASS;
511 		strcpy(di->label.name, AudioCoutputs);
512 		di->type = AUDIO_MIXER_CLASS;
513 		di->next = di->prev = AUDIO_MIXER_LAST;
514 		return 0;
515 	case PAD_INPUT_CLASS:
516 		di->mixer_class = PAD_INPUT_CLASS;
517 		strcpy(di->label.name, AudioCinputs);
518 		di->type = AUDIO_MIXER_CLASS;
519 		di->next = di->prev = AUDIO_MIXER_LAST;
520 		return 0;
521 	case PAD_OUTPUT_MASTER_VOLUME:
522 		di->mixer_class = PAD_OUTPUT_CLASS;
523 		strcpy(di->label.name, AudioNmaster);
524 		di->type = AUDIO_MIXER_VALUE;
525 		di->next = di->prev = AUDIO_MIXER_LAST;
526 		di->un.v.num_channels = 1;
527 		strcpy(di->un.v.units.name, AudioNvolume);
528 		return 0;
529 	case PAD_INPUT_DAC_VOLUME:
530 		di->mixer_class = PAD_INPUT_CLASS;
531 		strcpy(di->label.name, AudioNdac);
532 		di->type = AUDIO_MIXER_VALUE;
533 		di->next = di->prev = AUDIO_MIXER_LAST;
534 		di->un.v.num_channels = 1;
535 		strcpy(di->un.v.units.name, AudioNvolume);
536 		return 0;
537 	}
538 
539 	return ENXIO;
540 }
541 
542 static int
543 pad_get_props(void *opaque)
544 {
545 	return 0;
546 }
547 
548 static int
549 pad_round_blocksize(void *opaque, int blksize, int mode,
550     const audio_params_t *p)
551 {
552 	return PAD_BLKSIZE;
553 }
554