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