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