Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.73
      1 /* $NetBSD: pad.c,v 1.73 2021/06/14 18:44:37 riastradh 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.73 2021/06/14 18:44:37 riastradh 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);
    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
    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
    203 pad_match(device_t parent, cfdata_t data, void *opaque)
    204 {
    205 
    206 	return 1;
    207 }
    208 
    209 static void
    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
    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
    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
    281 pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
    282 {
    283 	int l;
    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 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    293 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    294 	else {
    295 		l = PAD_BUFSIZE - sc->sc_wpos;
    296 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    297 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    298 	}
    299 
    300 	sc->sc_wpos += blksize;
    301 	if (sc->sc_wpos >= PAD_BUFSIZE)
    302 		sc->sc_wpos -= PAD_BUFSIZE;
    303 
    304 	sc->sc_buflen += blksize;
    305 	cv_broadcast(&sc->sc_condvar);
    306 
    307 	return 0;
    308 }
    309 
    310 static int
    311 pad_get_block(struct pad_softc *sc, pad_block_t *pb, int maxblksize)
    312 {
    313 	int l, blksize, error;
    314 
    315 	KASSERT(maxblksize > 0);
    316 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    317 
    318 	while (sc->sc_buflen == 0) {
    319 		DPRINTF("%s: wait\n", __func__);
    320 		error = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
    321 		DPRINTF("%s: wake up %d\n", __func__, err);
    322 		if (error)
    323 			return error;
    324 	}
    325 	blksize = uimin(maxblksize, sc->sc_buflen);
    326 
    327 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    328 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    329 		pb->pb_len = blksize;
    330 		sc->sc_rpos += blksize;
    331 	} else {
    332 		l = PAD_BUFSIZE - sc->sc_rpos;
    333 		pb->pb_len = l;
    334 		sc->sc_rpos = 0;
    335 	}
    336 	sc->sc_buflen -= pb->pb_len;
    337 
    338 	return 0;
    339 }
    340 
    341 static int
    342 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    343 {
    344 	struct file *fp = NULL;
    345 	device_t self;
    346 	struct pad_softc *sc = NULL;
    347 	cfdata_t cf = NULL;
    348 	int error, fd;
    349 
    350 	error = fd_allocfile(&fp, &fd);
    351 	if (error)
    352 		goto out;
    353 
    354 	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
    355 	cf->cf_name = pad_cd.cd_name;
    356 	cf->cf_atname = pad_cd.cd_name;
    357 	cf->cf_unit = 0;
    358 	cf->cf_fstate = FSTATE_STAR;
    359 
    360 	self = config_attach_pseudo(cf);
    361 	if (self == NULL) {
    362 		error = ENXIO;
    363 		goto out;
    364 	}
    365 	sc = device_private(self);
    366 	KASSERT(sc->sc_dev == self);
    367 	cf = NULL;
    368 
    369 	error = fd_clone(fp, fd, flags, &pad_fileops, sc);
    370 	KASSERT(error == EMOVEFD);
    371 	fp = NULL;
    372 	sc = NULL;
    373 
    374 out:	if (sc)
    375 		pad_close(sc);
    376 	if (cf)
    377 		kmem_free(cf, sizeof(*cf));
    378 	if (fp)
    379 		fd_abort(curproc, fp, fd);
    380 	return error;
    381 }
    382 
    383 static void
    384 pad_close(struct pad_softc *sc)
    385 {
    386 	device_t self = sc->sc_dev;
    387 	cfdata_t cf = device_cfdata(self);
    388 
    389 	KERNEL_LOCK(1, NULL);
    390 	KASSERT(sc->sc_open);
    391 	sc->sc_open = 0;
    392 	(void)config_detach(self, DETACH_FORCE);
    393 	KERNEL_UNLOCK_ONE(NULL);
    394 
    395 	kmem_free(cf, sizeof(*cf));
    396 }
    397 
    398 static int
    399 fops_pad_close(struct file *fp)
    400 {
    401 	struct pad_softc *sc = fp->f_pad;
    402 
    403 	pad_close(sc);
    404 
    405 	return 0;
    406 }
    407 
    408 static int
    409 fops_pad_poll(struct file *fp, int events)
    410 {
    411 
    412 	return POLLERR;
    413 }
    414 
    415 static int
    416 fops_pad_kqfilter(struct file *fp, struct knote *kn)
    417 {
    418 	struct pad_softc *sc = fp->f_pad;
    419 	dev_t dev;
    420 
    421 	dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    422 	    device_unit(sc->sc_dev));
    423 
    424 	return seltrue_kqfilter(dev, kn);
    425 }
    426 
    427 static int
    428 fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
    429 {
    430 
    431 	return ENODEV;
    432 }
    433 
    434 static int
    435 fops_pad_stat(struct file *fp, struct stat *st)
    436 {
    437 	struct pad_softc *sc = fp->f_pad;
    438 
    439 	memset(st, 0, sizeof(*st));
    440 
    441 	st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    442 	    device_unit(sc->sc_dev));
    443 
    444 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    445 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    446 	st->st_mode = S_IFCHR;
    447 
    448 	return 0;
    449 }
    450 
    451 static int
    452 fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
    453     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    454 {
    455 
    456 	return 1;
    457 }
    458 
    459 static int
    460 fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    461     int ioflag)
    462 {
    463 	struct pad_softc *sc = fp->f_pad;
    464 
    465 	return pad_read(sc, offp, uio, cred, ioflag);
    466 }
    467 
    468 static int
    469 pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
    470     int ioflag)
    471 {
    472 	pad_block_t pb;
    473 	int err;
    474 
    475 	err = 0;
    476 	DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
    477 	while (uio->uio_resid > 0) {
    478 		mutex_enter(&sc->sc_intr_lock);
    479 		err = pad_get_block(sc, &pb, uio->uio_resid);
    480 		mutex_exit(&sc->sc_intr_lock);
    481 		if (err)
    482 			break;
    483 
    484 		DPRINTF("%s: move %d\n", __func__, pb.pb_len);
    485 		err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    486 		if (err)
    487 			break;
    488 	}
    489 
    490 	return err;
    491 }
    492 
    493 static int
    494 fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    495     int ioflag)
    496 {
    497 
    498 	return EOPNOTSUPP;
    499 }
    500 
    501 static int
    502 pad_query_format(void *opaque, audio_format_query_t *afp)
    503 {
    504 
    505 	return audio_query_format(pad_formats, PAD_NFORMATS, afp);
    506 }
    507 
    508 static int
    509 pad_set_format(void *opaque, int setmode,
    510     const audio_params_t *play, const audio_params_t *rec,
    511     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    512 {
    513 	struct pad_softc *sc = opaque;
    514 
    515 	KASSERT(mutex_owned(&sc->sc_lock));
    516 
    517 	/* XXX playback only */
    518 	pfil->codec = pad_swvol_codec;
    519 	pfil->context = sc;
    520 
    521 	return 0;
    522 }
    523 
    524 static int
    525 pad_start_output(void *opaque, void *block, int blksize,
    526     void (*intr)(void *), void *intrarg)
    527 {
    528 	struct pad_softc *sc = opaque;
    529 	int err;
    530 	int ms;
    531 
    532 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    533 
    534 	sc->sc_intr = intr;
    535 	sc->sc_intrarg = intrarg;
    536 	sc->sc_blksize = blksize;
    537 
    538 	DPRINTF("%s: blksize=%d\n", __func__, blksize);
    539 	err = pad_add_block(sc, block, blksize);
    540 
    541 	ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
    542 	DPRINTF("%s: callout ms=%d\n", __func__, ms);
    543 	callout_schedule(&sc->sc_pcallout, mstohz(ms));
    544 
    545 	return err;
    546 }
    547 
    548 static int
    549 pad_halt_output(void *opaque)
    550 {
    551 	struct pad_softc *sc = opaque;
    552 
    553 	DPRINTF("%s\n", __func__);
    554 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    555 
    556 	callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
    557 
    558 	sc->sc_intr = NULL;
    559 	sc->sc_intrarg = NULL;
    560 	sc->sc_buflen = 0;
    561 	sc->sc_rpos = sc->sc_wpos = 0;
    562 
    563 	return 0;
    564 }
    565 
    566 static void
    567 pad_done_output(void *arg)
    568 {
    569 	struct pad_softc *sc = arg;
    570 
    571 	DPRINTF("%s\n", __func__);
    572 
    573 	mutex_enter(&sc->sc_intr_lock);
    574 	(*sc->sc_intr)(sc->sc_intrarg);
    575 	mutex_exit(&sc->sc_intr_lock);
    576 }
    577 
    578 static int
    579 pad_getdev(void *opaque, struct audio_device *ret)
    580 {
    581 
    582 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    583 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    584 	strlcpy(ret->config, "pad", sizeof(ret->config));
    585 
    586 	return 0;
    587 }
    588 
    589 static int
    590 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    591 {
    592 	struct pad_softc *sc = opaque;
    593 
    594 	KASSERT(mutex_owned(&sc->sc_lock));
    595 
    596 	switch (mc->dev) {
    597 	case PAD_OUTPUT_MASTER_VOLUME:
    598 	case PAD_INPUT_DAC_VOLUME:
    599 		if (mc->un.value.num_channels != 1)
    600 			return EINVAL;
    601 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    602 		return 0;
    603 	}
    604 
    605 	return ENXIO;
    606 }
    607 
    608 static int
    609 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    610 {
    611 	struct pad_softc *sc = opaque;
    612 
    613 	KASSERT(mutex_owned(&sc->sc_lock));
    614 
    615 	switch (mc->dev) {
    616 	case PAD_OUTPUT_MASTER_VOLUME:
    617 	case PAD_INPUT_DAC_VOLUME:
    618 		if (mc->un.value.num_channels != 1)
    619 			return EINVAL;
    620 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    621 		return 0;
    622 	}
    623 
    624 	return ENXIO;
    625 }
    626 
    627 static int
    628 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    629 {
    630 	struct pad_softc *sc __diagused = opaque;
    631 
    632 	KASSERT(mutex_owned(&sc->sc_lock));
    633 
    634 	switch (di->index) {
    635 	case PAD_OUTPUT_CLASS:
    636 		di->mixer_class = PAD_OUTPUT_CLASS;
    637 		strcpy(di->label.name, AudioCoutputs);
    638 		di->type = AUDIO_MIXER_CLASS;
    639 		di->next = di->prev = AUDIO_MIXER_LAST;
    640 		return 0;
    641 	case PAD_INPUT_CLASS:
    642 		di->mixer_class = PAD_INPUT_CLASS;
    643 		strcpy(di->label.name, AudioCinputs);
    644 		di->type = AUDIO_MIXER_CLASS;
    645 		di->next = di->prev = AUDIO_MIXER_LAST;
    646 		return 0;
    647 	case PAD_OUTPUT_MASTER_VOLUME:
    648 		di->mixer_class = PAD_OUTPUT_CLASS;
    649 		strcpy(di->label.name, AudioNmaster);
    650 		di->type = AUDIO_MIXER_VALUE;
    651 		di->next = di->prev = AUDIO_MIXER_LAST;
    652 		di->un.v.num_channels = 1;
    653 		strcpy(di->un.v.units.name, AudioNvolume);
    654 		return 0;
    655 	case PAD_INPUT_DAC_VOLUME:
    656 		di->mixer_class = PAD_INPUT_CLASS;
    657 		strcpy(di->label.name, AudioNdac);
    658 		di->type = AUDIO_MIXER_VALUE;
    659 		di->next = di->prev = AUDIO_MIXER_LAST;
    660 		di->un.v.num_channels = 1;
    661 		strcpy(di->un.v.units.name, AudioNvolume);
    662 		return 0;
    663 	}
    664 
    665 	return ENXIO;
    666 }
    667 
    668 static int
    669 pad_get_props(void *opaque)
    670 {
    671 
    672 	return AUDIO_PROP_PLAYBACK;
    673 }
    674 
    675 static void
    676 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    677 {
    678 	struct pad_softc *sc = opaque;
    679 
    680 	*intr = &sc->sc_intr_lock;
    681 	*thread = &sc->sc_lock;
    682 }
    683 
    684 static void
    685 pad_swvol_codec(audio_filter_arg_t *arg)
    686 {
    687 	struct pad_softc *sc = arg->context;
    688 	const aint_t *src;
    689 	aint_t *dst;
    690 	u_int sample_count;
    691 	u_int i;
    692 
    693 	src = arg->src;
    694 	dst = arg->dst;
    695 	sample_count = arg->count * arg->srcfmt->channels;
    696 	for (i = 0; i < sample_count; i++) {
    697 		aint2_t v = (aint2_t)(*src++);
    698 		v = v * sc->sc_swvol / 255;
    699 		*dst++ = (aint_t)v;
    700 	}
    701 }
    702 
    703 MODULE(MODULE_CLASS_DRIVER, pad, "audio");
    704 
    705 #ifdef _MODULE
    706 
    707 #include "ioconf.c"
    708 
    709 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    710 
    711 /*
    712  * We need our own version of cfattach since config(1)'s ioconf does not
    713  * generate what we need
    714  */
    715 
    716 static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
    717 
    718 static struct cfattachinit pad_cfattach[] = {
    719 	{ "pad", pad_cfattachinit },
    720 	{ NULL, NULL }
    721 };
    722 #endif
    723 
    724 static int
    725 pad_modcmd(modcmd_t cmd, void *arg)
    726 {
    727 	int error = 0;
    728 
    729 	switch (cmd) {
    730 	case MODULE_CMD_INIT:
    731 #ifdef _MODULE
    732 		pad_cfattach[1] = cfattach_ioconf_pad[0];
    733 		error = config_init_component(cfdriver_ioconf_pad,
    734 		    pad_cfattach, cfdata_ioconf_pad);
    735 		if (error)
    736 			break;
    737 
    738 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    739 			    &pad_cdevsw, &cmajor);
    740 		if (error) {
    741 			config_fini_component(cfdriver_ioconf_pad,
    742 			    pad_cfattach, cfdata_ioconf_pad);
    743 			break;
    744 		}
    745 
    746 #endif
    747 		break;
    748 
    749 	case MODULE_CMD_FINI:
    750 #ifdef _MODULE
    751 		error = devsw_detach(NULL, &pad_cdevsw);
    752 		if (error)
    753 			break;
    754 
    755 		error = config_fini_component(cfdriver_ioconf_pad,
    756 		    pad_cfattach, cfdata_ioconf_pad);
    757 		if (error) {
    758 			devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    759 			    &pad_cdevsw, &cmajor);
    760 			break;
    761 		}
    762 #endif
    763 		break;
    764 
    765 	default:
    766 		error = ENOTTY;
    767 	}
    768 
    769 	return error;
    770 }
    771