1 /* $NetBSD: pad.c,v 1.85 2023/05/27 14:51:47 nat 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 *
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.85 2023/05/27 14:51:47 nat Exp $");
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34
35 #include <sys/audioio.h>
36 #include <sys/buf.h>
37 #include <sys/condvar.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/kauth.h>
43 #include <sys/kernel.h>
44 #include <sys/kmem.h>
45 #include <sys/module.h>
46 #include <sys/poll.h>
47 #include <sys/proc.h>
48 #include <sys/select.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51
52 #include <dev/audio/audio_if.h>
53 #include <dev/audio/audiovar.h>
54
55 #include <dev/pad/padvar.h>
56
57 #include "ioconf.h"
58
59 /* #define PAD_DEBUG */
60 #ifdef PAD_DEBUG
61 #define DPRINTF(fmt...) printf(fmt)
62 #else
63 #define DPRINTF(fmt...) /**/
64 #endif
65
66 #define PADFREQ 44100
67 #define PADCHAN 2
68 #define PADPREC 16
69
70 typedef struct pad_block {
71 uint8_t *pb_ptr;
72 int pb_len;
73 } pad_block_t;
74
75 enum {
76 PAD_OUTPUT_CLASS,
77 PAD_INPUT_CLASS,
78 PAD_OUTPUT_MASTER_VOLUME,
79 PAD_INPUT_DAC_VOLUME,
80 PAD_ENUM_LAST,
81 };
82
83 static int pad_match(device_t, cfdata_t, void *);
84 static void pad_attach(device_t, device_t, void *);
85 static int pad_detach(device_t, int);
86 static void pad_childdet(device_t, device_t);
87
88 static int pad_query_format(void *, audio_format_query_t *);
89 static int pad_set_format(void *, int,
90 const audio_params_t *, const audio_params_t *,
91 audio_filter_reg_t *, audio_filter_reg_t *);
92 static int pad_start_output(void *, void *, int,
93 void (*)(void *), void *);
94 static int pad_halt_output(void *);
95 static int pad_getdev(void *, struct audio_device *);
96 static int pad_set_port(void *, mixer_ctrl_t *);
97 static int pad_get_port(void *, mixer_ctrl_t *);
98 static int pad_query_devinfo(void *, mixer_devinfo_t *);
99 static int pad_get_props(void *);
100 static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
101
102 static void pad_done_output(void *);
103 static void pad_swvol_codec(audio_filter_arg_t *);
104
105 static void pad_close(struct pad_softc *);
106 static int pad_read(struct pad_softc *, off_t *, struct uio *,
107 kauth_cred_t, int);
108
109 static int fops_pad_close(struct file *);
110 static int fops_pad_read(struct file *, off_t *, struct uio *,
111 kauth_cred_t, int);
112 static int fops_pad_write(struct file *, off_t *, struct uio *,
113 kauth_cred_t, int);
114 static int fops_pad_ioctl(struct file *, u_long, void *);
115 static int fops_pad_kqfilter(struct file *, struct knote *);
116 static int fops_pad_poll(struct file *, int);
117 static int fops_pad_stat(struct file *, struct stat *);
118 static int fops_pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
119 struct uvm_object **, int *);
120
121 static const struct audio_hw_if pad_hw_if = {
122 .query_format = pad_query_format,
123 .set_format = pad_set_format,
124 .start_output = pad_start_output,
125 .halt_output = pad_halt_output,
126 .getdev = pad_getdev,
127 .set_port = pad_set_port,
128 .get_port = pad_get_port,
129 .query_devinfo = pad_query_devinfo,
130 .get_props = pad_get_props,
131 .get_locks = pad_get_locks,
132 };
133
134 #define PAD_NFORMATS 1
135 static const struct audio_format pad_formats[PAD_NFORMATS] = {
136 {
137 .mode = AUMODE_PLAY,
138 .encoding = AUDIO_ENCODING_SLINEAR_LE,
139 .validbits = PADPREC,
140 .precision = PADPREC,
141 .channels = PADCHAN,
142 .channel_mask = AUFMT_STEREO,
143 .frequency_type = 1,
144 .frequency = { PADFREQ },
145 },
146 };
147
148 extern void padattach(int);
149
150 static int pad_add_block(struct pad_softc *, uint8_t *, int);
151 static int pad_get_block(struct pad_softc *, pad_block_t *, int, int);
152
153 static dev_type_open(pad_open);
154
155 const struct cdevsw pad_cdevsw = {
156 .d_open = pad_open,
157 .d_close = noclose,
158 .d_read = noread,
159 .d_write = nowrite,
160 .d_ioctl = noioctl,
161 .d_stop = nostop,
162 .d_tty = notty,
163 .d_poll = nopoll,
164 .d_mmap = nommap,
165 .d_kqfilter = nokqfilter,
166 .d_discard = nodiscard,
167 .d_flag = D_OTHER | D_MPSAFE,
168 };
169
170 const struct fileops pad_fileops = {
171 .fo_name = "pad",
172 .fo_read = fops_pad_read,
173 .fo_write = fops_pad_write,
174 .fo_ioctl = fops_pad_ioctl,
175 .fo_fcntl = fnullop_fcntl,
176 .fo_stat = fops_pad_stat,
177 .fo_poll = fops_pad_poll,
178 .fo_close = fops_pad_close,
179 .fo_mmap = fops_pad_mmap,
180 .fo_kqfilter = fops_pad_kqfilter,
181 .fo_restart = fnullop_restart
182 };
183
184 CFATTACH_DECL2_NEW(pad, sizeof(struct pad_softc),
185 pad_match, pad_attach, pad_detach,
186 NULL, NULL, pad_childdet);
187
188 void
padattach(int n)189 padattach(int n)
190 {
191 int error;
192
193 error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
194 if (error) {
195 aprint_error("%s: couldn't register cfattach: %d\n",
196 pad_cd.cd_name, error);
197 config_cfdriver_detach(&pad_cd);
198 return;
199 }
200 }
201
202 static int
pad_match(device_t parent,cfdata_t data,void * opaque)203 pad_match(device_t parent, cfdata_t data, void *opaque)
204 {
205
206 return 1;
207 }
208
209 static void
pad_attach(device_t parent,device_t self,void * opaque)210 pad_attach(device_t parent, device_t self, void *opaque)
211 {
212 struct pad_softc *sc = device_private(self);
213
214 KASSERT(KERNEL_LOCKED_P());
215
216 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
217
218 sc->sc_dev = self;
219
220 cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
221 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
222 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
223 callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
224 callout_setfunc(&sc->sc_pcallout, pad_done_output, sc);
225
226 sc->sc_swvol = 255;
227 sc->sc_buflen = 0;
228 sc->sc_rpos = sc->sc_wpos = 0;
229 sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
230
231 if (!pmf_device_register(sc->sc_dev, NULL, NULL))
232 aprint_error_dev(sc->sc_dev,
233 "couldn't establish power handler\n");
234
235 sc->sc_open = 1;
236 }
237
238 static int
pad_detach(device_t self,int flags)239 pad_detach(device_t self, int flags)
240 {
241 struct pad_softc *sc = device_private(self);
242 int cmaj, mn;
243 int error;
244
245 KASSERT(KERNEL_LOCKED_P());
246
247 /* Prevent detach without going through close -- e.g., drvctl. */
248 if (sc->sc_open)
249 return EBUSY;
250
251 error = config_detach_children(self, flags);
252 if (error)
253 return error;
254
255 cmaj = cdevsw_lookup_major(&pad_cdevsw);
256 mn = device_unit(sc->sc_dev);
257 vdevgone(cmaj, mn, mn, VCHR);
258
259 pmf_device_deregister(sc->sc_dev);
260
261 callout_destroy(&sc->sc_pcallout);
262 mutex_destroy(&sc->sc_lock);
263 mutex_destroy(&sc->sc_intr_lock);
264 cv_destroy(&sc->sc_condvar);
265
266 return 0;
267 }
268
269 static void
pad_childdet(device_t self,device_t child)270 pad_childdet(device_t self, device_t child)
271 {
272 struct pad_softc *sc = device_private(self);
273
274 KASSERT(KERNEL_LOCKED_P());
275
276 if (child == sc->sc_audiodev)
277 sc->sc_audiodev = NULL;
278 }
279
280 static int
pad_add_block(struct pad_softc * sc,uint8_t * blk,int blksize)281 pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
282 {
283 int foff, flen, tlen;
284
285 KASSERT(blksize >= 0);
286 KASSERT(mutex_owned(&sc->sc_intr_lock));
287
288 if (blksize > PAD_BUFSIZE ||
289 sc->sc_buflen > PAD_BUFSIZE - (unsigned)blksize)
290 return ENOBUFS;
291
292 foff = sc->sc_wpos;
293 if (sc->sc_wpos + blksize <= PAD_BUFSIZE) {
294 flen = blksize;
295 tlen = 0;
296 } else {
297 flen = PAD_BUFSIZE - sc->sc_wpos;
298 tlen = blksize - flen;
299 }
300
301 sc->sc_wpos = foff + blksize;
302 if (sc->sc_wpos >= PAD_BUFSIZE)
303 sc->sc_wpos -= PAD_BUFSIZE;
304
305 /*
306 * release interrupt lock for bulk copy to audio buffer
307 */
308 mutex_exit(&sc->sc_intr_lock);
309 memcpy(sc->sc_audiobuf + foff, blk, flen);
310 memcpy(sc->sc_audiobuf, blk + flen, tlen);
311 mutex_enter(&sc->sc_intr_lock);
312
313 sc->sc_buflen += blksize;
314 cv_broadcast(&sc->sc_condvar);
315
316 return 0;
317 }
318
319 static int
pad_get_block(struct pad_softc * sc,pad_block_t * pb,int maxblksize,int dowait)320 pad_get_block(struct pad_softc *sc, pad_block_t *pb, int maxblksize, int dowait)
321 {
322 int l, blksize, error;
323
324 KASSERT(maxblksize > 0);
325 KASSERT(mutex_owned(&sc->sc_intr_lock));
326
327 if (sc->sc_buflen == 0 && !dowait)
328 return EAGAIN;
329
330 while (sc->sc_buflen == 0) {
331 DPRINTF("%s: wait\n", __func__);
332 error = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
333 DPRINTF("%s: wake up %d\n", __func__, err);
334 if (error)
335 return error;
336 }
337 blksize = uimin(maxblksize, sc->sc_buflen);
338
339 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
340 if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
341 pb->pb_len = blksize;
342 sc->sc_rpos += blksize;
343 } else {
344 l = PAD_BUFSIZE - sc->sc_rpos;
345 pb->pb_len = l;
346 sc->sc_rpos = 0;
347 }
348 sc->sc_buflen -= pb->pb_len;
349
350 return 0;
351 }
352
353 static int
pad_open(dev_t dev,int flags,int fmt,struct lwp * l)354 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
355 {
356 struct file *fp = NULL;
357 device_t self;
358 struct pad_softc *sc = NULL;
359 cfdata_t cf = NULL;
360 int error, fd;
361
362 error = fd_allocfile(&fp, &fd);
363 if (error)
364 goto out;
365
366 cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
367 cf->cf_name = pad_cd.cd_name;
368 cf->cf_atname = pad_cd.cd_name;
369 cf->cf_unit = 0;
370 cf->cf_fstate = FSTATE_STAR;
371
372 self = config_attach_pseudo(cf);
373 if (self == NULL) {
374 error = ENXIO;
375 goto out;
376 }
377 sc = device_private(self);
378 KASSERT(sc->sc_dev == self);
379 cf = NULL;
380
381 error = fd_clone(fp, fd, flags, &pad_fileops, sc);
382 KASSERT(error == EMOVEFD);
383 fp = NULL;
384 sc = NULL;
385
386 out: if (sc)
387 pad_close(sc);
388 if (cf)
389 kmem_free(cf, sizeof(*cf));
390 if (fp)
391 fd_abort(curproc, fp, fd);
392 return error;
393 }
394
395 static void
pad_close(struct pad_softc * sc)396 pad_close(struct pad_softc *sc)
397 {
398 device_t self = sc->sc_dev;
399 cfdata_t cf = device_cfdata(self);
400
401 /*
402 * XXX This is not quite enough to prevent racing with drvctl
403 * detach. What can happen:
404 *
405 * cpu0 cpu1
406 *
407 * pad_close
408 * take kernel lock
409 * sc->sc_open = 0
410 * drop kernel lock
411 * wait for config_misc_lock
412 * drvctl detach
413 * take kernel lock
414 * drop kernel lock
415 * wait for config_misc_lock
416 * retake kernel lock
417 * drop config_misc_lock
418 * take config_misc_lock
419 * wait for kernel lock
420 * pad_detach (sc_open=0 already)
421 * free device
422 * drop kernel lock
423 * use device after free
424 *
425 * We need a way to grab a reference to the device so it won't
426 * be freed until we're done -- it's OK if we config_detach
427 * twice as long as it's idempotent, but not OK if the first
428 * config_detach frees the struct device before the second one
429 * has finished handling it.
430 */
431 KERNEL_LOCK(1, NULL);
432 KASSERT(sc->sc_open);
433 sc->sc_open = 0;
434 (void)config_detach(self, DETACH_FORCE);
435 KERNEL_UNLOCK_ONE(NULL);
436
437 kmem_free(cf, sizeof(*cf));
438 }
439
440 static int
fops_pad_close(struct file * fp)441 fops_pad_close(struct file *fp)
442 {
443 struct pad_softc *sc = fp->f_pad;
444
445 pad_close(sc);
446
447 return 0;
448 }
449
450 static int
fops_pad_poll(struct file * fp,int events)451 fops_pad_poll(struct file *fp, int events)
452 {
453
454 return POLLERR;
455 }
456
457 static int
fops_pad_kqfilter(struct file * fp,struct knote * kn)458 fops_pad_kqfilter(struct file *fp, struct knote *kn)
459 {
460 struct pad_softc *sc = fp->f_pad;
461 dev_t dev;
462
463 dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
464 device_unit(sc->sc_dev));
465
466 return seltrue_kqfilter(dev, kn);
467 }
468
469 static int
fops_pad_ioctl(struct file * fp,u_long cmd,void * data)470 fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
471 {
472
473 return ENODEV;
474 }
475
476 static int
fops_pad_stat(struct file * fp,struct stat * st)477 fops_pad_stat(struct file *fp, struct stat *st)
478 {
479 struct pad_softc *sc = fp->f_pad;
480
481 memset(st, 0, sizeof(*st));
482
483 st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
484 device_unit(sc->sc_dev));
485
486 st->st_uid = kauth_cred_geteuid(fp->f_cred);
487 st->st_gid = kauth_cred_getegid(fp->f_cred);
488 st->st_mode = S_IFCHR;
489
490 return 0;
491 }
492
493 static int
fops_pad_mmap(struct file * fp,off_t * offp,size_t len,int prot,int * flagsp,int * advicep,struct uvm_object ** uobjp,int * maxprotp)494 fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
495 int *advicep, struct uvm_object **uobjp, int *maxprotp)
496 {
497
498 return 1;
499 }
500
501 static int
fops_pad_read(struct file * fp,off_t * offp,struct uio * uio,kauth_cred_t cred,int ioflag)502 fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
503 int ioflag)
504 {
505 struct pad_softc *sc = fp->f_pad;
506
507 return pad_read(sc, offp, uio, cred, ioflag);
508 }
509
510 static int
pad_read(struct pad_softc * sc,off_t * offp,struct uio * uio,kauth_cred_t cred,int ioflag)511 pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
512 int ioflag)
513 {
514 pad_block_t pb;
515 int err, first;
516
517 err = 0;
518 first = 1;
519 DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
520 while (uio->uio_resid > 0) {
521 mutex_enter(&sc->sc_intr_lock);
522 err = pad_get_block(sc, &pb, MIN(uio->uio_resid, INT_MAX), first);
523 mutex_exit(&sc->sc_intr_lock);
524 first = 0;
525 if (err == EAGAIN) {
526 err = 0;
527 break;
528 }
529 if (err)
530 break;
531
532 DPRINTF("%s: move %d\n", __func__, pb.pb_len);
533 err = uiomove(pb.pb_ptr, pb.pb_len, uio);
534 if (err)
535 break;
536 }
537
538 return err;
539 }
540
541 static int
fops_pad_write(struct file * fp,off_t * offp,struct uio * uio,kauth_cred_t cred,int ioflag)542 fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
543 int ioflag)
544 {
545
546 return EOPNOTSUPP;
547 }
548
549 static int
pad_query_format(void * opaque,audio_format_query_t * afp)550 pad_query_format(void *opaque, audio_format_query_t *afp)
551 {
552
553 return audio_query_format(pad_formats, PAD_NFORMATS, afp);
554 }
555
556 static int
pad_set_format(void * opaque,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)557 pad_set_format(void *opaque, int setmode,
558 const audio_params_t *play, const audio_params_t *rec,
559 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
560 {
561 struct pad_softc *sc = opaque;
562
563 KASSERT(mutex_owned(&sc->sc_lock));
564
565 /* XXX playback only */
566 pfil->codec = pad_swvol_codec;
567 pfil->context = sc;
568
569 return 0;
570 }
571
572 static int
pad_start_output(void * opaque,void * block,int blksize,void (* intr)(void *),void * intrarg)573 pad_start_output(void *opaque, void *block, int blksize,
574 void (*intr)(void *), void *intrarg)
575 {
576 struct pad_softc *sc = opaque;
577 int err;
578 u_int framesize;
579 int ticks;
580
581 KASSERT(mutex_owned(&sc->sc_intr_lock));
582
583 sc->sc_intr = intr;
584 sc->sc_intrarg = intrarg;
585
586 DPRINTF("%s: blksize=%d\n", __func__, blksize);
587 err = pad_add_block(sc, block, blksize);
588 if (err) {
589 DPRINTF("%s: failed: %d\n", __func__, err);
590 /* "Silently" drop overflows, but keep pace */
591 err = 0;
592 }
593
594 framesize = PADCHAN * (PADPREC / NBBY) * PADFREQ;
595
596 sc->sc_resid += blksize;
597 ticks = mstohz(sc->sc_resid * 1000 / framesize);
598 sc->sc_resid -= hztoms(ticks) * framesize / 1000;
599
600 DPRINTF("%s: callout ms=%d\n", __func__, ms);
601 callout_schedule(&sc->sc_pcallout, ticks);
602
603 return err;
604 }
605
606 static int
pad_halt_output(void * opaque)607 pad_halt_output(void *opaque)
608 {
609 struct pad_softc *sc = opaque;
610
611 DPRINTF("%s\n", __func__);
612 KASSERT(mutex_owned(&sc->sc_intr_lock));
613
614 callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
615
616 sc->sc_intr = NULL;
617 sc->sc_intrarg = NULL;
618 sc->sc_buflen = 0;
619 sc->sc_resid = 0;
620 sc->sc_rpos = sc->sc_wpos = 0;
621
622 return 0;
623 }
624
625 static void
pad_done_output(void * arg)626 pad_done_output(void *arg)
627 {
628 struct pad_softc *sc = arg;
629
630 DPRINTF("%s\n", __func__);
631
632 mutex_enter(&sc->sc_intr_lock);
633 (*sc->sc_intr)(sc->sc_intrarg);
634 mutex_exit(&sc->sc_intr_lock);
635 }
636
637 static int
pad_getdev(void * opaque,struct audio_device * ret)638 pad_getdev(void *opaque, struct audio_device *ret)
639 {
640
641 strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
642 strlcpy(ret->version, osrelease, sizeof(ret->version));
643 strlcpy(ret->config, "pad", sizeof(ret->config));
644
645 return 0;
646 }
647
648 static int
pad_set_port(void * opaque,mixer_ctrl_t * mc)649 pad_set_port(void *opaque, mixer_ctrl_t *mc)
650 {
651 struct pad_softc *sc = opaque;
652
653 KASSERT(mutex_owned(&sc->sc_lock));
654
655 switch (mc->dev) {
656 case PAD_OUTPUT_MASTER_VOLUME:
657 case PAD_INPUT_DAC_VOLUME:
658 if (mc->un.value.num_channels != 1)
659 return EINVAL;
660 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
661 return 0;
662 }
663
664 return ENXIO;
665 }
666
667 static int
pad_get_port(void * opaque,mixer_ctrl_t * mc)668 pad_get_port(void *opaque, mixer_ctrl_t *mc)
669 {
670 struct pad_softc *sc = opaque;
671
672 KASSERT(mutex_owned(&sc->sc_lock));
673
674 switch (mc->dev) {
675 case PAD_OUTPUT_MASTER_VOLUME:
676 case PAD_INPUT_DAC_VOLUME:
677 if (mc->un.value.num_channels != 1)
678 return EINVAL;
679 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
680 return 0;
681 }
682
683 return ENXIO;
684 }
685
686 static int
pad_query_devinfo(void * opaque,mixer_devinfo_t * di)687 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
688 {
689 struct pad_softc *sc __diagused = opaque;
690
691 KASSERT(mutex_owned(&sc->sc_lock));
692
693 switch (di->index) {
694 case PAD_OUTPUT_CLASS:
695 di->mixer_class = PAD_OUTPUT_CLASS;
696 strcpy(di->label.name, AudioCoutputs);
697 di->type = AUDIO_MIXER_CLASS;
698 di->next = di->prev = AUDIO_MIXER_LAST;
699 return 0;
700 case PAD_INPUT_CLASS:
701 di->mixer_class = PAD_INPUT_CLASS;
702 strcpy(di->label.name, AudioCinputs);
703 di->type = AUDIO_MIXER_CLASS;
704 di->next = di->prev = AUDIO_MIXER_LAST;
705 return 0;
706 case PAD_OUTPUT_MASTER_VOLUME:
707 di->mixer_class = PAD_OUTPUT_CLASS;
708 strcpy(di->label.name, AudioNmaster);
709 di->type = AUDIO_MIXER_VALUE;
710 di->next = di->prev = AUDIO_MIXER_LAST;
711 di->un.v.num_channels = 1;
712 strcpy(di->un.v.units.name, AudioNvolume);
713 return 0;
714 case PAD_INPUT_DAC_VOLUME:
715 di->mixer_class = PAD_INPUT_CLASS;
716 strcpy(di->label.name, AudioNdac);
717 di->type = AUDIO_MIXER_VALUE;
718 di->next = di->prev = AUDIO_MIXER_LAST;
719 di->un.v.num_channels = 1;
720 strcpy(di->un.v.units.name, AudioNvolume);
721 return 0;
722 }
723
724 return ENXIO;
725 }
726
727 static int
pad_get_props(void * opaque)728 pad_get_props(void *opaque)
729 {
730
731 return AUDIO_PROP_PLAYBACK;
732 }
733
734 static void
pad_get_locks(void * opaque,kmutex_t ** intr,kmutex_t ** thread)735 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
736 {
737 struct pad_softc *sc = opaque;
738
739 *intr = &sc->sc_intr_lock;
740 *thread = &sc->sc_lock;
741 }
742
743 static void
pad_swvol_codec(audio_filter_arg_t * arg)744 pad_swvol_codec(audio_filter_arg_t *arg)
745 {
746 struct pad_softc *sc = arg->context;
747 const uint8_t *src;
748 uint8_t *dst;
749 u_int sample_count;
750 u_int i;
751 u_int bits;
752
753 src = arg->src;
754 dst = arg->dst;
755 sample_count = arg->count * arg->srcfmt->channels;
756 bits = arg->srcfmt->precision;
757
758 for (i = 0; i < sample_count; i++) {
759 int64_t v;
760
761 switch (howmany(bits, NBBY)) {
762 case 2: /* AUDIO_INTERNAL_BITS == 16 */
763 v = *(const int16_t *)src;
764 src += sizeof(int16_t);
765 break;
766 case 4: /* AUDIO_INTERNAL_BITS == 32 */
767 v = *(const int32_t *)src;
768 src += sizeof(int32_t);
769 break;
770 default:
771 v = 0;
772 break;
773 }
774
775 v = v * sc->sc_swvol / 255;
776
777 if (PADPREC > bits)
778 v = v << (PADPREC - bits);
779 else if (PADPREC < bits)
780 v = v >> (bits - PADPREC);
781
782 /* AUDIO_ENCODING_SLINEAR_LE */
783 #if PADPREC > 0
784 *dst++ = v;
785 #endif
786 #if PADPREC > 8
787 v >>= 8;
788 *dst++ = v;
789 #endif
790 #if PADPREC > 16
791 v >>= 8;
792 *dst++ = v;
793 #endif
794 #if PADPREC > 24
795 v >>= 8;
796 *dst++ = v;
797 #endif
798 }
799 }
800
801 MODULE(MODULE_CLASS_DRIVER, pad, "audio");
802
803 #ifdef _MODULE
804
805 #include "ioconf.c"
806
807 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
808
809 /*
810 * We need our own version of cfattach since config(1)'s ioconf does not
811 * generate what we need
812 */
813
814 static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
815
816 static struct cfattachinit pad_cfattach[] = {
817 { "pad", pad_cfattachinit },
818 { NULL, NULL }
819 };
820 #endif
821
822 static int
pad_modcmd(modcmd_t cmd,void * arg)823 pad_modcmd(modcmd_t cmd, void *arg)
824 {
825 int error = 0;
826
827 switch (cmd) {
828 case MODULE_CMD_INIT:
829 #ifdef _MODULE
830 error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
831 &pad_cdevsw, &cmajor);
832 if (error)
833 break;
834
835 pad_cfattach[1] = cfattach_ioconf_pad[0];
836 error = config_init_component(cfdriver_ioconf_pad,
837 pad_cfattach, cfdata_ioconf_pad);
838 if (error) {
839 devsw_detach(NULL, &pad_cdevsw);
840 break;
841 }
842 #endif
843 break;
844
845 case MODULE_CMD_FINI:
846 #ifdef _MODULE
847 error = config_fini_component(cfdriver_ioconf_pad,
848 pad_cfattach, cfdata_ioconf_pad);
849 if (error == 0)
850 devsw_detach(NULL, &pad_cdevsw);
851 #endif
852 break;
853
854 default:
855 error = ENOTTY;
856 }
857
858 return error;
859 }
860