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