pad.c revision 1.42 1 /* $NetBSD: pad.c,v 1.42 2017/07/30 00:50:52 nat 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.42 2017/07/30 00:50:52 nat 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/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/vnode.h>
39 #include <sys/kauth.h>
40 #include <sys/kmem.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44 #include <sys/condvar.h>
45 #include <sys/select.h>
46 #include <sys/stat.h>
47 #include <sys/audioio.h>
48 #include <sys/vnode.h>
49 #include <sys/module.h>
50 #include <sys/atomic.h>
51 #include <sys/time.h>
52
53 #include <dev/audio_if.h>
54 #include <dev/audiovar.h>
55 #include <dev/auconv.h>
56 #include <dev/auvolconv.h>
57
58 #include <dev/pad/padvar.h>
59
60 #define MAXDEVS 128
61 #define PADCLONER 254
62 #define PADUNIT(x) minor(x)
63
64 #define PADFREQ 44100
65 #define PADCHAN 2
66 #define PADPREC 16
67 #define PADENC AUDIO_ENCODING_SLINEAR_LE
68
69 extern struct cfdriver pad_cd;
70
71 typedef struct pad_block {
72 uint8_t *pb_ptr;
73 int pb_len;
74 } pad_block_t;
75
76 enum {
77 PAD_OUTPUT_CLASS,
78 PAD_INPUT_CLASS,
79 PAD_OUTPUT_MASTER_VOLUME,
80 PAD_INPUT_DAC_VOLUME,
81 PAD_ENUM_LAST,
82 };
83
84 static int pad_match(device_t, cfdata_t, void *);
85 static void pad_attach(device_t, device_t, void *);
86 static int pad_detach(device_t, int);
87 static void pad_childdet(device_t, device_t);
88
89 static int pad_audio_open(void *, int);
90 static int pad_query_encoding(void *, struct audio_encoding *);
91 static int pad_set_params(void *, int, int,
92 audio_params_t *, audio_params_t *,
93 stream_filter_list_t *, stream_filter_list_t *);
94 static int pad_start_output(void *, void *, int,
95 void (*)(void *), void *);
96 static int pad_start_input(void *, void *, int,
97 void (*)(void *), void *);
98 static int pad_halt_output(void *);
99 static int pad_halt_input(void *);
100 static int pad_getdev(void *, struct audio_device *);
101 static int pad_set_port(void *, mixer_ctrl_t *);
102 static int pad_get_port(void *, mixer_ctrl_t *);
103 static int pad_query_devinfo(void *, mixer_devinfo_t *);
104 static int pad_get_props(void *);
105 static int pad_round_blocksize(void *, int, int, const audio_params_t *);
106 static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
107
108 static stream_filter_t *pad_swvol_filter_le(struct audio_softc *,
109 const audio_params_t *, const audio_params_t *);
110 static stream_filter_t *pad_swvol_filter_be(struct audio_softc *,
111 const audio_params_t *, const audio_params_t *);
112 static void pad_swvol_dtor(stream_filter_t *);
113
114 static int pad_close(struct pad_softc *);
115 static int pad_read(struct pad_softc *, off_t *, struct uio *, kauth_cred_t, int);
116
117 static int fops_pad_close(struct file *);
118 static int fops_pad_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
119 static int pad_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
120 static int pad_ioctl(struct file *, u_long, void *);
121 static int pad_kqfilter(struct file *, struct knote *);
122 static int pad_poll(struct file *, int);
123 static int pad_stat(struct file *, struct stat *);
124 static int pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
125 struct uvm_object **, int *);
126
127 static const struct audio_hw_if pad_hw_if = {
128 .open = pad_audio_open,
129 .query_encoding = pad_query_encoding,
130 .set_params = pad_set_params,
131 .start_output = pad_start_output,
132 .start_input = pad_start_input,
133 .halt_output = pad_halt_output,
134 .halt_input = pad_halt_input,
135 .getdev = pad_getdev,
136 .set_port = pad_set_port,
137 .get_port = pad_get_port,
138 .query_devinfo = pad_query_devinfo,
139 .get_props = pad_get_props,
140 .round_blocksize = pad_round_blocksize,
141 .get_locks = pad_get_locks,
142 };
143
144 #define PAD_NFORMATS 1
145 static const struct audio_format pad_formats[PAD_NFORMATS] = {
146 { NULL, AUMODE_PLAY|AUMODE_RECORD, PADENC, PADPREC, PADPREC,
147 PADCHAN, AUFMT_STEREO, 1, { PADFREQ } },
148 };
149
150 extern void padattach(int);
151
152 static int pad_add_block(pad_softc_t *, uint8_t *, int);
153 static int pad_get_block(pad_softc_t *, pad_block_t *, int);
154
155 dev_type_open(pad_open);
156 dev_type_close(cdev_pad_close);
157 dev_type_read(cdev_pad_read);
158
159 const struct cdevsw pad_cdevsw = {
160 .d_open = pad_open,
161 .d_close = cdev_pad_close,
162 .d_read = cdev_pad_read,
163 .d_write = nowrite,
164 .d_ioctl = noioctl,
165 .d_stop = nostop,
166 .d_tty = notty,
167 .d_poll = nopoll,
168 .d_mmap = nommap,
169 .d_kqfilter = nokqfilter,
170 .d_discard = nodiscard,
171 .d_flag = D_OTHER | D_MPSAFE,
172 };
173
174 const struct fileops pad_fileops = {
175 .fo_read = fops_pad_read,
176 .fo_write = pad_write,
177 .fo_ioctl = pad_ioctl,
178 .fo_fcntl = fnullop_fcntl,
179 .fo_stat = pad_stat,
180 .fo_poll = pad_poll,
181 .fo_close = fops_pad_close,
182 .fo_mmap = pad_mmap,
183 .fo_kqfilter = pad_kqfilter,
184 .fo_restart = fnullop_restart
185 };
186
187 CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
188 NULL, NULL, pad_childdet);
189
190 void
191 padattach(int n)
192 {
193 int error;
194
195 error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
196 if (error) {
197 aprint_error("%s: couldn't register cfattach: %d\n",
198 pad_cd.cd_name, error);
199 config_cfdriver_detach(&pad_cd);
200 return;
201 }
202
203 return;
204 }
205
206 static int
207 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
208 {
209 int l;
210
211 if (sc->sc_open == 0)
212 return EIO;
213
214 KASSERT(mutex_owned(&sc->sc_lock));
215
216 if (sc->sc_buflen + blksize > PAD_BUFSIZE)
217 return ENOBUFS;
218
219 if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
220 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
221 else {
222 l = PAD_BUFSIZE - sc->sc_wpos;
223 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
224 memcpy(sc->sc_audiobuf, blk + l, blksize - l);
225 }
226
227 sc->sc_wpos += blksize;
228 if (sc->sc_wpos > PAD_BUFSIZE)
229 sc->sc_wpos -= PAD_BUFSIZE;
230
231 sc->sc_buflen += blksize;
232
233 return 0;
234 }
235
236 static int
237 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
238 {
239 int l;
240
241 KASSERT(mutex_owned(&sc->sc_lock));
242 KASSERT(pb != NULL);
243
244 if (sc->sc_buflen < (uint)blksize)
245 return ERESTART;
246
247 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
248 if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
249 pb->pb_len = blksize;
250 sc->sc_rpos += blksize;
251 } else {
252 l = PAD_BUFSIZE - sc->sc_rpos;
253 pb->pb_len = l;
254 sc->sc_rpos = 0;
255 }
256 sc->sc_buflen -= pb->pb_len;
257
258 return 0;
259 }
260
261 static int
262 pad_match(device_t parent, cfdata_t data, void *opaque)
263 {
264
265 return 1;
266 }
267
268 static void
269 pad_childdet(device_t self, device_t child)
270 {
271 pad_softc_t *sc = device_private(self);
272
273 sc->sc_audiodev = NULL;
274 }
275
276 static void
277 pad_attach(device_t parent, device_t self, void *opaque)
278 {
279 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
280
281 return;
282 }
283
284 static int
285 pad_detach(device_t self, int flags)
286 {
287 pad_softc_t *sc;
288 int cmaj, mn, rc;
289
290 sc = device_private(self);
291 config_deactivate(sc->sc_audiodev);
292
293 /* Start draining existing accessors of the device. */
294 if ((rc = config_detach_children(self, flags)) != 0)
295 return rc;
296
297 mutex_enter(&sc->sc_lock);
298 sc->sc_dying = true;
299 cv_broadcast(&sc->sc_condvar);
300 mutex_exit(&sc->sc_lock);
301
302 KASSERT(sc->sc_open > 0);
303 sc->sc_open = 0;
304
305 cmaj = cdevsw_lookup_major(&pad_cdevsw);
306 mn = device_unit(sc->sc_dev);
307 vdevgone(cmaj, mn, mn, VCHR);
308
309 pmf_device_deregister(sc->sc_dev);
310
311 mutex_destroy(&sc->sc_lock);
312 mutex_destroy(&sc->sc_intr_lock);
313 cv_destroy(&sc->sc_condvar);
314
315 auconv_delete_encodings(sc->sc_encodings);
316
317 return 0;
318 }
319
320 int
321 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
322 {
323 pad_softc_t *sc;
324 struct file *fp;
325 device_t paddev;
326 cfdata_t cf;
327 int error, fd, i;
328
329 if (PADUNIT(dev) == PADCLONER) {
330 for (i = 0; i < MAXDEVS; i++) {
331 if (device_lookup(&pad_cd, i) == NULL)
332 break;
333 }
334 if (i == MAXDEVS)
335 return ENXIO;
336 } else {
337 if (PADUNIT(dev) >= MAXDEVS)
338 return ENXIO;
339 i = PADUNIT(dev);
340 }
341
342 cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
343 cf->cf_name = pad_cd.cd_name;
344 cf->cf_atname = pad_cd.cd_name;
345 cf->cf_unit = i;
346 cf->cf_fstate = FSTATE_STAR;
347
348 if (device_lookup(&pad_cd, minor(dev)) == NULL)
349 paddev = config_attach_pseudo(cf);
350 else
351 paddev = device_lookup(&pad_cd, minor(dev));
352
353 sc = device_private(paddev);
354 if (sc == NULL)
355 return ENXIO;
356
357 if (sc->sc_open == 1)
358 return EBUSY;
359
360 sc->sc_dev = paddev;
361 sc->sc_dying = false;
362
363 if (PADUNIT(dev) == PADCLONER) {
364 error = fd_allocfile(&fp, &fd);
365 if (error) {
366 config_detach(sc->sc_dev, 0);
367 return error;
368 }
369 }
370
371 if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
372 &sc->sc_encodings) != 0) {
373 aprint_error_dev(sc->sc_dev, "couldn't create encodings\n");
374 config_detach(sc->sc_dev, 0);
375 return EINVAL;
376 }
377
378 cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
379 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
380 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
381
382 sc->sc_swvol = 255;
383 sc->sc_buflen = 0;
384 sc->sc_rpos = sc->sc_wpos = 0;
385 sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
386
387 if (!pmf_device_register(sc->sc_dev, NULL, NULL))
388 aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
389
390 if (PADUNIT(dev) == PADCLONER) {
391 error = fd_clone(fp, fd, flags, &pad_fileops, sc);
392 KASSERT(error == EMOVEFD);
393 }
394 sc->sc_open = 1;
395
396 return error;
397 }
398
399 static int
400 pad_close(struct pad_softc *sc)
401 {
402 if (sc == NULL)
403 return ENXIO;
404
405 return config_detach(sc->sc_dev, DETACH_FORCE);
406 }
407
408 static int
409 fops_pad_close(struct file *fp)
410 {
411 pad_softc_t *sc;
412 int error;
413
414 sc = fp->f_pad;
415
416 error = pad_close(sc);
417
418 if (error == 0)
419 fp->f_pad = NULL;
420
421 return error;
422 }
423
424 int
425 cdev_pad_close(dev_t dev, int flags, int ifmt, struct lwp *l)
426 {
427 pad_softc_t *sc;
428 sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
429
430 return pad_close(sc);
431 }
432
433 static int
434 pad_poll(struct file *fp, int events)
435 {
436 return ENODEV;
437 }
438
439 static int
440 pad_kqfilter(struct file *fp, struct knote *kn)
441 {
442 struct pad_softc *sc;
443 dev_t dev;
444
445 sc = fp->f_pad;
446 if (sc == NULL)
447 return EIO;
448
449 dev = makedev(cdevsw_lookup_major(&pad_cdevsw), device_unit(sc->sc_dev));
450
451 return seltrue_kqfilter(dev, kn);
452 }
453
454 static int
455 pad_ioctl(struct file *fp, u_long cmd, void *data)
456 {
457 return ENODEV;
458 }
459
460 static int
461 pad_stat(struct file *fp, struct stat *st)
462 {
463 struct pad_softc *sc;
464
465 sc = fp->f_pad;
466 if (sc == NULL)
467 return EIO;
468
469 memset(st, 0, sizeof(*st));
470
471 st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw), device_unit(sc->sc_dev));
472
473 st->st_uid = kauth_cred_geteuid(fp->f_cred);
474 st->st_gid = kauth_cred_getegid(fp->f_cred);
475 st->st_mode = S_IFCHR;
476
477 return 0;
478 }
479
480 static int
481 pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
482 int *advicep, struct uvm_object **uobjp, int *maxprotp)
483 {
484 return 1;
485 }
486
487 #define PAD_BYTES_PER_SEC (PADFREQ * PADPREC / NBBY * PADCHAN)
488 #define BYTESTOSLEEP (int64_t)(PAD_BLKSIZE)
489 #define TIMENEXTREAD (int64_t)(BYTESTOSLEEP * 1000000 / PAD_BYTES_PER_SEC)
490
491 int
492 cdev_pad_read(dev_t dev, struct uio *uio, int ioflag)
493 {
494 pad_softc_t *sc;
495 sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
496 if (sc == NULL)
497 return ENXIO;
498
499 return pad_read(sc, NULL, uio, NULL, ioflag);
500 }
501
502 static int
503 fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
504 int ioflag)
505 {
506 pad_softc_t *sc;
507
508 sc = fp->f_pad;
509 if (sc == NULL)
510 return ENXIO;
511
512 return pad_read(sc, offp, uio, cred, ioflag);
513 }
514
515 static int
516 pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
517 int ioflag)
518 {
519 struct timeval now;
520 uint64_t nowusec, lastusec;
521 pad_block_t pb;
522 void (*intr)(void *);
523 void *intrarg;
524 int err, wait_ticks;
525
526 err = 0;
527
528 while (uio->uio_resid > 0 && !err) {
529 mutex_enter(&sc->sc_lock);
530 if (sc->sc_dying == true) {
531 mutex_exit(&sc->sc_lock);
532 return EIO;
533 }
534 intr = sc->sc_intr;
535 intrarg = sc->sc_intrarg;
536
537 getmicrotime(&now);
538 nowusec = (now.tv_sec * 1000000) + now.tv_usec;
539 lastusec = (sc->sc_last.tv_sec * 1000000) +
540 sc->sc_last.tv_usec;
541 if (lastusec + TIMENEXTREAD > nowusec) {
542 if (sc->sc_bytes_count >= BYTESTOSLEEP) {
543 sc->sc_remainder +=
544 ((lastusec + TIMENEXTREAD) - nowusec);
545 }
546
547 wait_ticks = (hz * sc->sc_remainder) / 1000000;
548 if (wait_ticks > 0) {
549 sc->sc_remainder -= wait_ticks * 1000000 / hz;
550 err = kpause("padwait", TRUE, wait_ticks,
551 &sc->sc_lock);
552 if (err != EWOULDBLOCK) {
553 mutex_exit(&sc->sc_lock);
554 continue;
555 }
556 }
557 }
558
559 if (sc->sc_bytes_count >= BYTESTOSLEEP)
560 sc->sc_bytes_count -= BYTESTOSLEEP;
561
562 err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
563 if (!err) {
564 getmicrotime(&sc->sc_last);
565 sc->sc_bytes_count += pb.pb_len;
566 mutex_exit(&sc->sc_lock);
567 err = uiomove(pb.pb_ptr, pb.pb_len, uio);
568 continue;
569 }
570
571 if (intr) {
572 mutex_enter(&sc->sc_intr_lock);
573 kpreempt_disable();
574 (*intr)(intrarg);
575 kpreempt_enable();
576 mutex_exit(&sc->sc_intr_lock);
577 intr = sc->sc_intr;
578 intrarg = sc->sc_intrarg;
579 err = 0;
580 mutex_exit(&sc->sc_lock);
581 continue;
582 }
583 err = cv_wait_sig(&sc->sc_condvar, &sc->sc_lock);
584 if (err != 0) {
585 mutex_exit(&sc->sc_lock);
586 break;
587 }
588
589 mutex_exit(&sc->sc_lock);
590 }
591
592 return err;
593 }
594
595 static int
596 pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
597 int ioflag)
598 {
599 return EOPNOTSUPP;
600 }
601
602 static int
603 pad_audio_open(void *opaque, int flags)
604 {
605 pad_softc_t *sc;
606 sc = opaque;
607
608 if (sc->sc_open == 0)
609 return EIO;
610
611 getmicrotime(&sc->sc_last);
612 sc->sc_bytes_count = 0;
613 sc->sc_remainder = 0;
614
615 return 0;
616 }
617
618 static int
619 pad_query_encoding(void *opaque, struct audio_encoding *ae)
620 {
621 pad_softc_t *sc;
622
623 sc = (pad_softc_t *)opaque;
624
625 KASSERT(mutex_owned(&sc->sc_lock));
626
627 return auconv_query_encoding(sc->sc_encodings, ae);
628 }
629
630 static int
631 pad_set_params(void *opaque, int setmode, int usemode,
632 audio_params_t *play, audio_params_t *rec,
633 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
634 {
635 pad_softc_t *sc __diagused;
636
637 sc = (pad_softc_t *)opaque;
638
639 KASSERT(mutex_owned(&sc->sc_lock));
640
641 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
642 play, true, pfil) < 0)
643 return EINVAL;
644 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
645 rec, true, rfil) < 0)
646 return EINVAL;
647
648 if (pfil->req_size > 0)
649 play = &pfil->filters[0].param;
650 switch (play->encoding) {
651 case AUDIO_ENCODING_SLINEAR_LE:
652 if (play->precision == 16 && play->validbits == 16)
653 pfil->prepend(pfil, pad_swvol_filter_le, play);
654 break;
655 case AUDIO_ENCODING_SLINEAR_BE:
656 if (play->precision == 16 && play->validbits == 16)
657 pfil->prepend(pfil, pad_swvol_filter_be, play);
658 break;
659 default:
660 break;
661 }
662
663 return 0;
664 }
665
666 static int
667 pad_start_output(void *opaque, void *block, int blksize,
668 void (*intr)(void *), void *intrarg)
669 {
670 pad_softc_t *sc;
671 int err;
672
673 sc = (pad_softc_t *)opaque;
674
675 KASSERT(mutex_owned(&sc->sc_lock));
676 if (!sc->sc_open)
677 return EIO;
678
679 sc->sc_intr = intr;
680 sc->sc_intrarg = intrarg;
681 sc->sc_blksize = blksize;
682
683 err = pad_add_block(sc, block, blksize);
684
685 cv_broadcast(&sc->sc_condvar);
686
687 return err;
688 }
689
690 static int
691 pad_start_input(void *opaque, void *block, int blksize,
692 void (*intr)(void *), void *intrarg)
693 {
694 pad_softc_t *sc __diagused;
695
696 sc = (pad_softc_t *)opaque;
697
698 KASSERT(mutex_owned(&sc->sc_lock));
699
700 return EOPNOTSUPP;
701 }
702
703 static int
704 pad_halt_output(void *opaque)
705 {
706 pad_softc_t *sc;
707
708 sc = (pad_softc_t *)opaque;
709
710 KASSERT(mutex_owned(&sc->sc_lock));
711
712 sc->sc_intr = NULL;
713 sc->sc_intrarg = NULL;
714 sc->sc_buflen = 0;
715 sc->sc_rpos = sc->sc_wpos = 0;
716
717 return 0;
718 }
719
720 static int
721 pad_halt_input(void *opaque)
722 {
723 pad_softc_t *sc __diagused;
724
725 sc = (pad_softc_t *)opaque;
726
727 KASSERT(mutex_owned(&sc->sc_lock));
728
729 return 0;
730 }
731
732 static int
733 pad_getdev(void *opaque, struct audio_device *ret)
734 {
735 strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
736 strlcpy(ret->version, osrelease, sizeof(ret->version));
737 strlcpy(ret->config, "pad", sizeof(ret->config));
738
739 return 0;
740 }
741
742 static int
743 pad_set_port(void *opaque, mixer_ctrl_t *mc)
744 {
745 pad_softc_t *sc;
746
747 sc = (pad_softc_t *)opaque;
748
749 KASSERT(mutex_owned(&sc->sc_lock));
750
751 switch (mc->dev) {
752 case PAD_OUTPUT_MASTER_VOLUME:
753 case PAD_INPUT_DAC_VOLUME:
754 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
755 return 0;
756 }
757
758 return ENXIO;
759 }
760
761 static int
762 pad_get_port(void *opaque, mixer_ctrl_t *mc)
763 {
764 pad_softc_t *sc;
765
766 sc = (pad_softc_t *)opaque;
767
768 KASSERT(mutex_owned(&sc->sc_lock));
769
770 switch (mc->dev) {
771 case PAD_OUTPUT_MASTER_VOLUME:
772 case PAD_INPUT_DAC_VOLUME:
773 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
774 return 0;
775 }
776
777 return ENXIO;
778 }
779
780 static int
781 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
782 {
783 pad_softc_t *sc __diagused;
784
785 sc = (pad_softc_t *)opaque;
786
787 KASSERT(mutex_owned(&sc->sc_lock));
788
789 switch (di->index) {
790 case PAD_OUTPUT_CLASS:
791 di->mixer_class = PAD_OUTPUT_CLASS;
792 strcpy(di->label.name, AudioCoutputs);
793 di->type = AUDIO_MIXER_CLASS;
794 di->next = di->prev = AUDIO_MIXER_LAST;
795 return 0;
796 case PAD_INPUT_CLASS:
797 di->mixer_class = PAD_INPUT_CLASS;
798 strcpy(di->label.name, AudioCinputs);
799 di->type = AUDIO_MIXER_CLASS;
800 di->next = di->prev = AUDIO_MIXER_LAST;
801 return 0;
802 case PAD_OUTPUT_MASTER_VOLUME:
803 di->mixer_class = PAD_OUTPUT_CLASS;
804 strcpy(di->label.name, AudioNmaster);
805 di->type = AUDIO_MIXER_VALUE;
806 di->next = di->prev = AUDIO_MIXER_LAST;
807 di->un.v.num_channels = 1;
808 strcpy(di->un.v.units.name, AudioNvolume);
809 return 0;
810 case PAD_INPUT_DAC_VOLUME:
811 di->mixer_class = PAD_INPUT_CLASS;
812 strcpy(di->label.name, AudioNdac);
813 di->type = AUDIO_MIXER_VALUE;
814 di->next = di->prev = AUDIO_MIXER_LAST;
815 di->un.v.num_channels = 1;
816 strcpy(di->un.v.units.name, AudioNvolume);
817 return 0;
818 }
819
820 return ENXIO;
821 }
822
823 static int
824 pad_get_props(void *opaque)
825 {
826 pad_softc_t *sc __diagused;
827
828 sc = (pad_softc_t *)opaque;
829
830 KASSERT(mutex_owned(&sc->sc_lock));
831
832 return 0;
833 }
834
835 static int
836 pad_round_blocksize(void *opaque, int blksize, int mode,
837 const audio_params_t *p)
838 {
839 pad_softc_t *sc __diagused;
840
841 sc = (pad_softc_t *)opaque;
842 KASSERT(mutex_owned(&sc->sc_lock));
843
844 return PAD_BLKSIZE;
845 }
846
847 static void
848 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
849 {
850 pad_softc_t *sc;
851
852 sc = (pad_softc_t *)opaque;
853
854 *intr = &sc->sc_intr_lock;
855 *thread = &sc->sc_lock;
856 }
857
858 static stream_filter_t *
859 pad_swvol_filter_le(struct audio_softc *asc,
860 const audio_params_t *from, const audio_params_t *to)
861 {
862 auvolconv_filter_t *this;
863 device_t dev = audio_get_device(asc);
864 struct pad_softc *sc = device_private(dev);
865
866 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
867 this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to;
868 this->base.dtor = pad_swvol_dtor;
869 this->base.set_fetcher = stream_filter_set_fetcher;
870 this->base.set_inputbuffer = stream_filter_set_inputbuffer;
871 this->vol = &sc->sc_swvol;
872
873 return (stream_filter_t *)this;
874 }
875
876 static stream_filter_t *
877 pad_swvol_filter_be(struct audio_softc *asc,
878 const audio_params_t *from, const audio_params_t *to)
879 {
880 auvolconv_filter_t *this;
881 device_t dev = audio_get_device(asc);
882 struct pad_softc *sc = device_private(dev);
883
884 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
885 this->base.base.fetch_to = auvolconv_slinear16_be_fetch_to;
886 this->base.dtor = pad_swvol_dtor;
887 this->base.set_fetcher = stream_filter_set_fetcher;
888 this->base.set_inputbuffer = stream_filter_set_inputbuffer;
889 this->vol = &sc->sc_swvol;
890
891 return (stream_filter_t *)this;
892 }
893
894 static void
895 pad_swvol_dtor(stream_filter_t *this)
896 {
897 if (this)
898 kmem_free(this, sizeof(auvolconv_filter_t));
899 }
900
901 #ifdef _MODULE
902
903 MODULE(MODULE_CLASS_DRIVER, pad, "audio");
904
905 static const struct cfiattrdata audiobuscf_iattrdata = {
906 "audiobus", 0, { { NULL, NULL, 0 }, }
907 };
908 static const struct cfiattrdata * const pad_attrs[] = {
909 &audiobuscf_iattrdata, NULL
910 };
911
912 CFDRIVER_DECL(pad, DV_DULL, pad_attrs);
913 extern struct cfattach pad_ca;
914 static int padloc[] = { -1, -1 };
915
916 static struct cfdata pad_cfdata[] = {
917 {
918 .cf_name = "pad",
919 .cf_atname = "pad",
920 .cf_unit = 0,
921 .cf_fstate = FSTATE_STAR,
922 .cf_loc = padloc,
923 .cf_flags = 0,
924 .cf_pspec = NULL,
925 },
926 { NULL, NULL, 0, 0, NULL, 0, NULL }
927 };
928
929 static int
930 pad_modcmd(modcmd_t cmd, void *arg)
931 {
932 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
933 int error;
934
935 switch (cmd) {
936 case MODULE_CMD_INIT:
937 error = config_cfdriver_attach(&pad_cd);
938 if (error) {
939 return error;
940 }
941
942 error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
943 if (error) {
944 config_cfdriver_detach(&pad_cd);
945 aprint_error("%s: unable to register cfattach\n",
946 pad_cd.cd_name);
947
948 return error;
949 }
950
951 error = config_cfdata_attach(pad_cfdata, 1);
952 if (error) {
953 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
954 config_cfdriver_detach(&pad_cd);
955 aprint_error("%s: unable to register cfdata\n",
956 pad_cd.cd_name);
957
958 return error;
959 }
960
961 error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
962 &pad_cdevsw, &cmajor);
963 if (error) {
964 error = config_cfdata_detach(pad_cfdata);
965 if (error) {
966 return error;
967 }
968 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
969 config_cfdriver_detach(&pad_cd);
970 aprint_error("%s: unable to register devsw\n",
971 pad_cd.cd_name);
972
973 return error;
974 }
975
976 (void)config_attach_pseudo(pad_cfdata);
977
978 return 0;
979 case MODULE_CMD_FINI:
980 error = config_cfdata_detach(pad_cfdata);
981 if (error) {
982 return error;
983 }
984
985 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
986 config_cfdriver_detach(&pad_cd);
987 devsw_detach(NULL, &pad_cdevsw);
988
989 return 0;
990 default:
991 return ENOTTY;
992 }
993 }
994
995 #endif
996