pad.c revision 1.6 1 /* $NetBSD: pad.c,v 1.6 2008/03/04 18:23:44 cube Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) 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.6 2008/03/04 18:23:44 cube 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 #include <sys/vnode.h>
50
51 #include <dev/audio_if.h>
52 #include <dev/audiovar.h>
53 #include <dev/auconv.h>
54
55 #include <dev/pad/padvar.h>
56 #include <dev/pad/padvol.h>
57
58 #define PADUNIT(x) minor(x)
59
60 extern struct cfdriver pad_cd;
61
62 static struct audio_device pad_device = {
63 "Pseudo Audio",
64 "1.0",
65 "pad",
66 };
67
68 typedef struct pad_block {
69 uint8_t *pb_ptr;
70 int pb_len;
71 } pad_block_t;
72
73 enum {
74 PAD_OUTPUT_CLASS,
75 PAD_INPUT_CLASS,
76 PAD_OUTPUT_MASTER_VOLUME,
77 PAD_INPUT_DAC_VOLUME,
78 PAD_ENUM_LAST,
79 };
80
81 static int pad_match(device_t, struct cfdata *, void *);
82 static void pad_attach(device_t, device_t, void *);
83 static int pad_detach(device_t, int);
84 static void pad_childdet(device_t, device_t);
85
86 static int pad_query_encoding(void *, struct audio_encoding *);
87 static int pad_set_params(void *, int, int,
88 audio_params_t *, audio_params_t *,
89 stream_filter_list_t *, stream_filter_list_t *);
90 static int pad_start_output(void *, void *, int,
91 void (*)(void *), void *);
92 static int pad_start_input(void *, void *, int,
93 void (*)(void *), void *);
94 static int pad_halt_output(void *);
95 static int pad_halt_input(void *);
96 static int pad_getdev(void *, struct audio_device *);
97 static int pad_set_port(void *, mixer_ctrl_t *);
98 static int pad_get_port(void *, mixer_ctrl_t *);
99 static int pad_query_devinfo(void *, mixer_devinfo_t *);
100 static int pad_get_props(void *);
101 static int pad_round_blocksize(void *, int, int, const audio_params_t *);
102
103 static const struct audio_hw_if pad_hw_if = {
104 .query_encoding = pad_query_encoding,
105 .set_params = pad_set_params,
106 .start_output = pad_start_output,
107 .start_input = pad_start_input,
108 .halt_output = pad_halt_output,
109 .halt_input = pad_halt_input,
110 .getdev = pad_getdev,
111 .set_port = pad_set_port,
112 .get_port = pad_get_port,
113 .query_devinfo = pad_query_devinfo,
114 .get_props = pad_get_props,
115 .round_blocksize = pad_round_blocksize,
116 };
117
118 #define PAD_NFORMATS 1
119 static const struct audio_format pad_formats[PAD_NFORMATS] = {
120 { NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
121 2, AUFMT_STEREO, 1, { 44100 } },
122 };
123
124 extern void padattach(int);
125
126 static pad_softc_t * pad_find_softc(dev_t);
127 static int pad_add_block(pad_softc_t *, uint8_t *, int);
128 static int pad_get_block(pad_softc_t *, pad_block_t *, int);
129
130 dev_type_open(pad_open);
131 dev_type_close(pad_close);
132 dev_type_read(pad_read);
133
134 const struct cdevsw pad_cdevsw = {
135 .d_open = pad_open,
136 .d_close = pad_close,
137 .d_read = pad_read,
138 .d_write = nowrite,
139 .d_ioctl = noioctl,
140 .d_stop = nostop,
141 .d_tty = notty,
142 .d_poll = nopoll,
143 .d_mmap = nommap,
144 .d_kqfilter = nokqfilter,
145 .d_flag = D_OTHER,
146 };
147
148 CFATTACH_DECL2(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
149 NULL, NULL, pad_childdet);
150
151 void
152 padattach(int n)
153 {
154 int i, err;
155 struct cfdata *cf;
156
157 #ifdef DEBUG
158 printf("pad: requested %d units\n", n);
159 #endif
160
161 err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
162 if (err) {
163 aprint_error("%s: couldn't register cfattach: %d\n",
164 pad_cd.cd_name, err);
165 config_cfdriver_detach(&pad_cd);
166 return;
167 }
168
169 for (i = 0; i < n; i++) {
170 cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
171 if (cf == NULL) {
172 aprint_error("%s: couldn't allocate cfdata\n",
173 pad_cd.cd_name);
174 continue;
175 }
176 cf->cf_name = pad_cd.cd_name;
177 cf->cf_atname = pad_cd.cd_name;
178 cf->cf_unit = i;
179 cf->cf_fstate = FSTATE_STAR;
180
181 (void)config_attach_pseudo(cf);
182 }
183
184 return;
185 }
186
187 static pad_softc_t *
188 pad_find_softc(dev_t dev)
189 {
190 int unit;
191
192 unit = PADUNIT(dev);
193 if (unit >= pad_cd.cd_ndevs)
194 return NULL;
195
196 return pad_cd.cd_devs[unit];
197 }
198
199 static int
200 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
201 {
202 int l;
203
204 if (sc->sc_buflen + blksize > PAD_BUFSIZE)
205 return ENOBUFS;
206
207 if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
208 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
209 else {
210 l = PAD_BUFSIZE - sc->sc_wpos;
211 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
212 memcpy(sc->sc_audiobuf, blk + l, blksize - l);
213 }
214
215 sc->sc_wpos += blksize;
216 if (sc->sc_wpos > PAD_BUFSIZE)
217 sc->sc_wpos -= PAD_BUFSIZE;
218
219 sc->sc_buflen += blksize;
220
221 return 0;
222 }
223
224 static int
225 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
226 {
227 int l;
228
229 KASSERT(pb != NULL);
230
231 if (sc->sc_buflen < blksize)
232 return ERESTART;
233
234 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
235 if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
236 pb->pb_len = blksize;
237 sc->sc_rpos += blksize;
238 } else {
239 l = PAD_BUFSIZE - sc->sc_rpos;
240 pb->pb_len = l;
241 sc->sc_rpos = 0;
242 }
243 sc->sc_buflen -= pb->pb_len;
244
245 return 0;
246 }
247
248 static int
249 pad_match(device_t parent, struct cfdata *data, void *opaque)
250 {
251 return 1;
252 }
253
254 static void
255 pad_childdet(device_t self, device_t child)
256 {
257 pad_softc_t *sc = device_private(self);
258
259 KASSERT(sc->sc_audiodev->dev == child);
260 sc->sc_audiodev = NULL;
261 }
262
263 static void
264 pad_attach(device_t parent, device_t self, void *opaque)
265 {
266 pad_softc_t *sc = device_private(self);
267
268 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
269
270 sc->sc_open = 0;
271 if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
272 &sc->sc_encodings) != 0) {
273 aprint_error_dev(self, "couldn't create encodings\n");
274 return;
275 }
276
277 cv_init(&sc->sc_condvar, device_xname(self));
278 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_SCHED);
279
280 sc->sc_swvol = 255;
281 sc->sc_buflen = 0;
282 sc->sc_rpos = sc->sc_wpos = 0;
283 sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
284
285 if (!pmf_device_register(self, NULL, NULL))
286 aprint_error_dev(self, "couldn't establish power handler\n");
287
288 return;
289 }
290
291 static int
292 pad_detach(device_t self, int flags)
293 {
294 pad_softc_t *sc = device_private(self);
295 int cmaj, mn, rc;
296
297 cmaj = cdevsw_lookup_major(&pad_cdevsw);
298 mn = device_unit(self);
299 vdevgone(cmaj, mn, mn, VCHR);
300
301 if ((rc = config_detach_children(self, flags)) != 0)
302 return rc;
303
304 pmf_device_deregister(self);
305
306 mutex_destroy(&sc->sc_mutex);
307 cv_destroy(&sc->sc_condvar);
308
309 auconv_delete_encodings(sc->sc_encodings);
310
311 return 0;
312 }
313
314 int
315 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
316 {
317 pad_softc_t *sc;
318
319 sc = pad_find_softc(dev);
320 if (sc == NULL)
321 return ENODEV;
322
323 if (sc->sc_open++) {
324 sc->sc_open--;
325 return EBUSY;
326 }
327
328 return 0;
329 }
330
331 int
332 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
333 {
334 pad_softc_t *sc;
335
336 sc = pad_find_softc(dev);
337 if (sc == NULL)
338 return ENODEV;
339
340 KASSERT(sc->sc_open > 0);
341 sc->sc_open--;
342
343 return 0;
344 }
345
346 int
347 pad_read(dev_t dev, struct uio *uio, int flags)
348 {
349 pad_softc_t *sc;
350 pad_block_t pb;
351 void (*intr)(void *);
352 void *intrarg;
353 int err;
354
355 sc = pad_find_softc(dev);
356 if (sc == NULL)
357 return ENODEV;
358
359 err = 0;
360
361 intr = sc->sc_intr;
362 intrarg = sc->sc_intrarg;
363
364 while (uio->uio_resid > 0 && !err) {
365 err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
366 if (!err)
367 err = uiomove(pb.pb_ptr, pb.pb_len, uio);
368 else {
369 if (intr) {
370 (*intr)(intrarg);
371 intr = sc->sc_intr;
372 intrarg = sc->sc_intrarg;
373 err = 0;
374 continue;
375 }
376
377 mutex_enter(&sc->sc_mutex);
378 err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
379 hz/100);
380 if (err != 0 && err != EWOULDBLOCK) {
381 mutex_exit(&sc->sc_mutex);
382 aprint_error_dev(&sc->sc_dev,
383 "cv_timedwait_sig returned %d\n", err);
384 return EINTR;
385 }
386 intr = sc->sc_intr;
387 intrarg = sc->sc_intrarg;
388 mutex_exit(&sc->sc_mutex);
389 err = 0;
390 }
391 }
392
393 if (intr)
394 (*intr)(intrarg);
395
396 return err;
397 }
398
399 static int
400 pad_query_encoding(void *opaque, struct audio_encoding *ae)
401 {
402 pad_softc_t *sc;
403
404 sc = (pad_softc_t *)opaque;
405
406 return auconv_query_encoding(sc->sc_encodings, ae);
407 }
408
409 static int
410 pad_set_params(void *opaque, int setmode, int usemode,
411 audio_params_t *play, audio_params_t *rec,
412 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
413 {
414 pad_softc_t *sc;
415
416 sc = (pad_softc_t *)opaque;
417
418 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
419 play, true, pfil) < 0)
420 return EINVAL;
421 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
422 rec, true, rfil) < 0)
423 return EINVAL;
424
425 if (pfil->req_size > 0)
426 play = &pfil->filters[0].param;
427 switch (play->encoding) {
428 case AUDIO_ENCODING_SLINEAR_LE:
429 if (play->precision == 16 && play->validbits == 16)
430 pfil->prepend(pfil, pad_vol_slinear16_le, play);
431 break;
432 case AUDIO_ENCODING_SLINEAR_BE:
433 if (play->precision == 16 && play->validbits == 16)
434 pfil->prepend(pfil, pad_vol_slinear16_be, play);
435 break;
436 default:
437 break;
438 }
439
440 return 0;
441 }
442
443 static int
444 pad_start_output(void *opaque, void *block, int blksize,
445 void (*intr)(void *), void *intrarg)
446 {
447 pad_softc_t *sc;
448 int err;
449
450 sc = (pad_softc_t *)opaque;
451
452 mutex_enter(&sc->sc_mutex);
453
454 sc->sc_intr = intr;
455 sc->sc_intrarg = intrarg;
456 sc->sc_blksize = blksize;
457
458 err = pad_add_block(sc, block, blksize);
459
460 cv_signal(&sc->sc_condvar);
461
462 mutex_exit(&sc->sc_mutex);
463
464 return err;
465 }
466
467 static int
468 pad_start_input(void *opaque, void *block, int blksize,
469 void (*intr)(void *), void *intrarg)
470 {
471 return EINVAL;
472 }
473
474 static int
475 pad_halt_output(void *opaque)
476 {
477 pad_softc_t *sc;
478
479 sc = (pad_softc_t *)opaque;
480 sc->sc_intr = NULL;
481 sc->sc_intrarg = NULL;
482 sc->sc_buflen = 0;
483 sc->sc_rpos = sc->sc_wpos = 0;
484
485 return 0;
486 }
487
488 static int
489 pad_halt_input(void *opaque)
490 {
491 return 0;
492 }
493
494 static int
495 pad_getdev(void *opaque, struct audio_device *ret)
496 {
497
498 *ret = pad_device;
499
500 return 0;
501 }
502
503 static int
504 pad_set_port(void *opaque, mixer_ctrl_t *mc)
505 {
506 pad_softc_t *sc;
507
508 sc = (pad_softc_t *)opaque;
509
510 switch (mc->dev) {
511 case PAD_OUTPUT_MASTER_VOLUME:
512 case PAD_INPUT_DAC_VOLUME:
513 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
514 return 0;
515 }
516
517 return ENXIO;
518 }
519
520 static int
521 pad_get_port(void *opaque, mixer_ctrl_t *mc)
522 {
523 pad_softc_t *sc;
524
525 sc = (pad_softc_t *)opaque;
526
527 switch (mc->dev) {
528 case PAD_OUTPUT_MASTER_VOLUME:
529 case PAD_INPUT_DAC_VOLUME:
530 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
531 return 0;
532 }
533
534 return ENXIO;
535 }
536
537 static int
538 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
539 {
540 pad_softc_t *sc;
541
542 sc = (pad_softc_t *)opaque;
543
544 switch (di->index) {
545 case PAD_OUTPUT_CLASS:
546 di->mixer_class = PAD_OUTPUT_CLASS;
547 strcpy(di->label.name, AudioCoutputs);
548 di->type = AUDIO_MIXER_CLASS;
549 di->next = di->prev = AUDIO_MIXER_LAST;
550 return 0;
551 case PAD_INPUT_CLASS:
552 di->mixer_class = PAD_INPUT_CLASS;
553 strcpy(di->label.name, AudioCinputs);
554 di->type = AUDIO_MIXER_CLASS;
555 di->next = di->prev = AUDIO_MIXER_LAST;
556 return 0;
557 case PAD_OUTPUT_MASTER_VOLUME:
558 di->mixer_class = PAD_OUTPUT_CLASS;
559 strcpy(di->label.name, AudioNmaster);
560 di->type = AUDIO_MIXER_VALUE;
561 di->next = di->prev = AUDIO_MIXER_LAST;
562 di->un.v.num_channels = 1;
563 strcpy(di->un.v.units.name, AudioNvolume);
564 return 0;
565 case PAD_INPUT_DAC_VOLUME:
566 di->mixer_class = PAD_INPUT_CLASS;
567 strcpy(di->label.name, AudioNdac);
568 di->type = AUDIO_MIXER_VALUE;
569 di->next = di->prev = AUDIO_MIXER_LAST;
570 di->un.v.num_channels = 1;
571 strcpy(di->un.v.units.name, AudioNvolume);
572 return 0;
573 }
574
575 return ENXIO;
576 }
577
578 static int
579 pad_get_props(void *opaque)
580 {
581 return 0;
582 }
583
584 static int
585 pad_round_blocksize(void *opaque, int blksize, int mode,
586 const audio_params_t *p)
587 {
588 return PAD_BLKSIZE;
589 }
590