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