Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.67
      1 /* $NetBSD: pad.c,v 1.67 2021/06/13 23:09:22 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.67 2021/06/13 23:09:22 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 
    222 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    223 }
    224 
    225 static int
    226 pad_detach(device_t self, int flags)
    227 {
    228 	struct pad_softc *sc;
    229 	int cmaj, mn;
    230 
    231 	sc = device_private(self);
    232 	cmaj = cdevsw_lookup_major(&pad_cdevsw);
    233 	mn = device_unit(sc->sc_dev);
    234 	if (!sc->sc_dying)
    235 		vdevgone(cmaj, mn, mn, VCHR);
    236 
    237 	return 0;
    238 }
    239 
    240 static void
    241 pad_childdet(device_t self, device_t child)
    242 {
    243 	struct pad_softc *sc = device_private(self);
    244 
    245 	sc->sc_audiodev = NULL;
    246 }
    247 
    248 static int
    249 pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
    250 {
    251 	int l;
    252 
    253 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
    254 		return ENOBUFS;
    255 
    256 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    257 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    258 	else {
    259 		l = PAD_BUFSIZE - sc->sc_wpos;
    260 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    261 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    262 	}
    263 
    264 	sc->sc_wpos += blksize;
    265 	if (sc->sc_wpos >= PAD_BUFSIZE)
    266 		sc->sc_wpos -= PAD_BUFSIZE;
    267 
    268 	sc->sc_buflen += blksize;
    269 
    270 	return 0;
    271 }
    272 
    273 static int
    274 pad_get_block(struct pad_softc *sc, pad_block_t *pb, int blksize)
    275 {
    276 	int l;
    277 
    278 	KASSERT(pb != NULL);
    279 
    280 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    281 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    282 		pb->pb_len = blksize;
    283 		sc->sc_rpos += blksize;
    284 	} else {
    285 		l = PAD_BUFSIZE - sc->sc_rpos;
    286 		pb->pb_len = l;
    287 		sc->sc_rpos = 0;
    288 	}
    289 	sc->sc_buflen -= pb->pb_len;
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 cdev_pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    296 {
    297 	struct pad_softc *sc;
    298 	struct file *fp;
    299 	device_t paddev;
    300 	cfdata_t cf;
    301 	int error, fd, i;
    302 
    303 	error = 0;
    304 
    305 	mutex_enter(&padconfig);
    306 	if (PADUNIT(dev) == PADCLONER) {
    307 		for (i = 0; i < MAXDEVS; i++) {
    308 			if (device_lookup(&pad_cd, i) == NULL)
    309 				break;
    310 		}
    311 		if (i == MAXDEVS)
    312 			goto bad;
    313 	} else {
    314 		if (PADUNIT(dev) >= MAXDEVS)
    315 			goto bad;
    316 		i = PADUNIT(dev);
    317 	}
    318 
    319 	cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
    320 	cf->cf_name = pad_cd.cd_name;
    321 	cf->cf_atname = pad_cd.cd_name;
    322 	cf->cf_unit = i;
    323 	cf->cf_fstate = FSTATE_STAR;
    324 
    325 	bool existing = false;
    326 	paddev = device_lookup(&pad_cd, minor(dev));
    327 	if (paddev == NULL)
    328 		paddev = config_attach_pseudo(cf);
    329 	else
    330 		existing = true;
    331 	if (paddev == NULL)
    332 		goto bad;
    333 
    334 	sc = device_private(paddev);
    335 	if (sc == NULL)
    336 		goto bad;
    337 
    338 	if (sc->sc_open == 1) {
    339 		mutex_exit(&padconfig);
    340 		return EBUSY;
    341 	}
    342 
    343 	sc->sc_dev = paddev;
    344 	sc->sc_dying = false;
    345 
    346 	if (PADUNIT(dev) == PADCLONER) {
    347 		error = fd_allocfile(&fp, &fd);
    348 		if (error) {
    349 			if (existing == false)
    350 				config_detach(sc->sc_dev, 0);
    351 			mutex_exit(&padconfig);
    352 			return error;
    353 		}
    354 	}
    355 
    356 	cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
    357 	mutex_init(&sc->sc_cond_lock, MUTEX_DEFAULT, IPL_NONE);
    358 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    359 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
    360 	callout_init(&sc->sc_pcallout, 0/*XXX?*/);
    361 
    362 	sc->sc_swvol = 255;
    363 	sc->sc_buflen = 0;
    364 	sc->sc_rpos = sc->sc_wpos = 0;
    365 	KERNEL_LOCK(1, NULL);
    366 	sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
    367 	KERNEL_UNLOCK_ONE(NULL);
    368 
    369 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    370 		aprint_error_dev(sc->sc_dev,
    371 		    "couldn't establish power handler\n");
    372 
    373 	if (PADUNIT(dev) == PADCLONER) {
    374 		error = fd_clone(fp, fd, flags, &pad_fileops, sc);
    375 		KASSERT(error == EMOVEFD);
    376 	}
    377 	sc->sc_open = 1;
    378 	mutex_exit(&padconfig);
    379 
    380 	return error;
    381 bad:
    382 	mutex_exit(&padconfig);
    383 	return ENXIO;
    384 }
    385 
    386 static int
    387 pad_close(struct pad_softc *sc)
    388 {
    389 	int rc;
    390 
    391 	if (sc == NULL)
    392 		return ENXIO;
    393 
    394 	mutex_enter(&padconfig);
    395 	config_deactivate(sc->sc_audiodev);
    396 
    397 	/* Start draining existing accessors of the device. */
    398 	if ((rc = config_detach_children(sc->sc_dev,
    399 	    DETACH_SHUTDOWN|DETACH_FORCE)) != 0) {
    400 		mutex_exit(&padconfig);
    401 		return rc;
    402 	}
    403 
    404 	mutex_enter(&sc->sc_lock);
    405 	sc->sc_dying = true;
    406 	cv_broadcast(&sc->sc_condvar);
    407 	mutex_exit(&sc->sc_lock);
    408 
    409 	KASSERT(sc->sc_open > 0);
    410 	sc->sc_open = 0;
    411 
    412 	pmf_device_deregister(sc->sc_dev);
    413 
    414 	mutex_destroy(&sc->sc_cond_lock);
    415 	mutex_destroy(&sc->sc_lock);
    416 	mutex_destroy(&sc->sc_intr_lock);
    417 	cv_destroy(&sc->sc_condvar);
    418 
    419 	rc = config_detach(sc->sc_dev, 0);
    420 	mutex_exit(&padconfig);
    421 
    422 	return rc;
    423 }
    424 
    425 static int
    426 fops_pad_close(struct file *fp)
    427 {
    428 	struct pad_softc *sc;
    429 	int error;
    430 
    431 	sc = fp->f_pad;
    432 
    433 	error = pad_close(sc);
    434 
    435 	if (error == 0)
    436 		fp->f_pad = NULL;
    437 
    438 	return error;
    439 }
    440 
    441 int
    442 cdev_pad_close(dev_t dev, int flags, int ifmt, struct lwp *l)
    443 {
    444 	struct pad_softc *sc;
    445 	sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
    446 
    447 	return pad_close(sc);
    448 }
    449 
    450 static int
    451 fops_pad_poll(struct file *fp, int events)
    452 {
    453 
    454 	return POLLERR;
    455 }
    456 
    457 static int
    458 fops_pad_kqfilter(struct file *fp, struct knote *kn)
    459 {
    460 	struct pad_softc *sc;
    461 	dev_t dev;
    462 
    463 	sc = fp->f_pad;
    464 	if (sc == NULL)
    465 		return EIO;
    466 
    467 	dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    468 	    device_unit(sc->sc_dev));
    469 
    470 	return seltrue_kqfilter(dev, kn);
    471 }
    472 
    473 static int
    474 fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
    475 {
    476 
    477 	return ENODEV;
    478 }
    479 
    480 static int
    481 fops_pad_stat(struct file *fp, struct stat *st)
    482 {
    483 	struct pad_softc *sc;
    484 
    485 	sc = fp->f_pad;
    486 	if (sc == NULL)
    487 		return EIO;
    488 
    489 	memset(st, 0, sizeof(*st));
    490 
    491 	st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    492 	    device_unit(sc->sc_dev));
    493 
    494 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    495 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    496 	st->st_mode = S_IFCHR;
    497 
    498 	return 0;
    499 }
    500 
    501 static int
    502 fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
    503     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    504 {
    505 
    506 	return 1;
    507 }
    508 
    509 int
    510 cdev_pad_read(dev_t dev, struct uio *uio, int ioflag)
    511 {
    512 	struct pad_softc *sc;
    513 
    514 	sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
    515 	if (sc == NULL)
    516 		return ENXIO;
    517 
    518 	return pad_read(sc, NULL, uio, NULL, ioflag);
    519 }
    520 
    521 static int
    522 fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    523     int ioflag)
    524 {
    525 	struct pad_softc *sc;
    526 
    527 	sc = fp->f_pad;
    528 	if (sc == NULL)
    529 		return ENXIO;
    530 
    531 	return pad_read(sc, offp, uio, cred, ioflag);
    532 }
    533 
    534 static int
    535 pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
    536     int ioflag)
    537 {
    538 	pad_block_t pb;
    539 	int len;
    540 	int err;
    541 
    542 	err = 0;
    543 	DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
    544 	while (uio->uio_resid > 0 && !err) {
    545 		mutex_enter(&sc->sc_cond_lock);
    546 		if (sc->sc_buflen == 0) {
    547 			DPRINTF("%s: wait\n", __func__);
    548 			err = cv_wait_sig(&sc->sc_condvar, &sc->sc_cond_lock);
    549 			DPRINTF("%s: wake up %d\n", __func__, err);
    550 			mutex_exit(&sc->sc_cond_lock);
    551 			if (err) {
    552 				if (err == ERESTART)
    553 					err = EINTR;
    554 				break;
    555 			}
    556 			if (sc->sc_buflen == 0)
    557 				break;
    558 			continue;
    559 		}
    560 
    561 		len = uimin(uio->uio_resid, sc->sc_buflen);
    562 		err = pad_get_block(sc, &pb, len);
    563 		mutex_exit(&sc->sc_cond_lock);
    564 		if (err)
    565 			break;
    566 		DPRINTF("%s: move %d\n", __func__, pb.pb_len);
    567 		uiomove(pb.pb_ptr, pb.pb_len, uio);
    568 	}
    569 
    570 	return err;
    571 }
    572 
    573 static int
    574 fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    575     int ioflag)
    576 {
    577 
    578 	return EOPNOTSUPP;
    579 }
    580 
    581 static int
    582 pad_query_format(void *opaque, audio_format_query_t *afp)
    583 {
    584 
    585 	return audio_query_format(pad_formats, PAD_NFORMATS, afp);
    586 }
    587 
    588 static int
    589 pad_set_format(void *opaque, int setmode,
    590     const audio_params_t *play, const audio_params_t *rec,
    591     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    592 {
    593 	struct pad_softc *sc;
    594 
    595 	sc = (struct pad_softc *)opaque;
    596 
    597 	KASSERT(mutex_owned(&sc->sc_lock));
    598 
    599 	/* XXX playback only */
    600 	pfil->codec = pad_swvol_codec;
    601 	pfil->context = sc;
    602 
    603 	return 0;
    604 }
    605 
    606 static int
    607 pad_start_output(void *opaque, void *block, int blksize,
    608     void (*intr)(void *), void *intrarg)
    609 {
    610 	struct pad_softc *sc;
    611 	int err;
    612 	int ms;
    613 
    614 	sc = (struct pad_softc *)opaque;
    615 
    616 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    617 
    618 	sc->sc_intr = intr;
    619 	sc->sc_intrarg = intrarg;
    620 	sc->sc_blksize = blksize;
    621 
    622 	DPRINTF("%s: blksize=%d\n", __func__, blksize);
    623 	mutex_enter(&sc->sc_cond_lock);
    624 	err = pad_add_block(sc, block, blksize);
    625 	mutex_exit(&sc->sc_cond_lock);
    626 	cv_broadcast(&sc->sc_condvar);
    627 
    628 	ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
    629 	DPRINTF("%s: callout ms=%d\n", __func__, ms);
    630 	callout_reset(&sc->sc_pcallout, mstohz(ms), pad_done_output, sc);
    631 
    632 	return err;
    633 }
    634 
    635 static int
    636 pad_halt_output(void *opaque)
    637 {
    638 	struct pad_softc *sc;
    639 
    640 	sc = (struct pad_softc *)opaque;
    641 
    642 	DPRINTF("%s\n", __func__);
    643 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    644 
    645 	cv_broadcast(&sc->sc_condvar);
    646 	callout_stop(&sc->sc_pcallout);
    647 	sc->sc_intr = NULL;
    648 	sc->sc_intrarg = NULL;
    649 	sc->sc_buflen = 0;
    650 	sc->sc_rpos = sc->sc_wpos = 0;
    651 
    652 	return 0;
    653 }
    654 
    655 static void
    656 pad_done_output(void *arg)
    657 {
    658 	struct pad_softc *sc;
    659 
    660 	DPRINTF("%s\n", __func__);
    661 	sc = (struct pad_softc *)arg;
    662 	callout_stop(&sc->sc_pcallout);
    663 
    664 	mutex_enter(&sc->sc_intr_lock);
    665 	(*sc->sc_intr)(sc->sc_intrarg);
    666 	mutex_exit(&sc->sc_intr_lock);
    667 }
    668 
    669 static int
    670 pad_getdev(void *opaque, struct audio_device *ret)
    671 {
    672 
    673 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    674 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    675 	strlcpy(ret->config, "pad", sizeof(ret->config));
    676 
    677 	return 0;
    678 }
    679 
    680 static int
    681 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    682 {
    683 	struct pad_softc *sc;
    684 
    685 	sc = (struct pad_softc *)opaque;
    686 
    687 	KASSERT(mutex_owned(&sc->sc_lock));
    688 
    689 	switch (mc->dev) {
    690 	case PAD_OUTPUT_MASTER_VOLUME:
    691 	case PAD_INPUT_DAC_VOLUME:
    692 		if (mc->un.value.num_channels != 1)
    693 			return EINVAL;
    694 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    695 		return 0;
    696 	}
    697 
    698 	return ENXIO;
    699 }
    700 
    701 static int
    702 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    703 {
    704 	struct pad_softc *sc;
    705 
    706 	sc = (struct pad_softc *)opaque;
    707 
    708 	KASSERT(mutex_owned(&sc->sc_lock));
    709 
    710 	switch (mc->dev) {
    711 	case PAD_OUTPUT_MASTER_VOLUME:
    712 	case PAD_INPUT_DAC_VOLUME:
    713 		if (mc->un.value.num_channels != 1)
    714 			return EINVAL;
    715 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    716 		return 0;
    717 	}
    718 
    719 	return ENXIO;
    720 }
    721 
    722 static int
    723 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    724 {
    725 	struct pad_softc *sc __diagused;
    726 
    727 	sc = (struct pad_softc *)opaque;
    728 
    729 	KASSERT(mutex_owned(&sc->sc_lock));
    730 
    731 	switch (di->index) {
    732 	case PAD_OUTPUT_CLASS:
    733 		di->mixer_class = PAD_OUTPUT_CLASS;
    734 		strcpy(di->label.name, AudioCoutputs);
    735 		di->type = AUDIO_MIXER_CLASS;
    736 		di->next = di->prev = AUDIO_MIXER_LAST;
    737 		return 0;
    738 	case PAD_INPUT_CLASS:
    739 		di->mixer_class = PAD_INPUT_CLASS;
    740 		strcpy(di->label.name, AudioCinputs);
    741 		di->type = AUDIO_MIXER_CLASS;
    742 		di->next = di->prev = AUDIO_MIXER_LAST;
    743 		return 0;
    744 	case PAD_OUTPUT_MASTER_VOLUME:
    745 		di->mixer_class = PAD_OUTPUT_CLASS;
    746 		strcpy(di->label.name, AudioNmaster);
    747 		di->type = AUDIO_MIXER_VALUE;
    748 		di->next = di->prev = AUDIO_MIXER_LAST;
    749 		di->un.v.num_channels = 1;
    750 		strcpy(di->un.v.units.name, AudioNvolume);
    751 		return 0;
    752 	case PAD_INPUT_DAC_VOLUME:
    753 		di->mixer_class = PAD_INPUT_CLASS;
    754 		strcpy(di->label.name, AudioNdac);
    755 		di->type = AUDIO_MIXER_VALUE;
    756 		di->next = di->prev = AUDIO_MIXER_LAST;
    757 		di->un.v.num_channels = 1;
    758 		strcpy(di->un.v.units.name, AudioNvolume);
    759 		return 0;
    760 	}
    761 
    762 	return ENXIO;
    763 }
    764 
    765 static int
    766 pad_get_props(void *opaque)
    767 {
    768 
    769 	return AUDIO_PROP_PLAYBACK;
    770 }
    771 
    772 static void
    773 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    774 {
    775 	struct pad_softc *sc;
    776 
    777 	sc = (struct pad_softc *)opaque;
    778 
    779 	*intr = &sc->sc_intr_lock;
    780 	*thread = &sc->sc_lock;
    781 }
    782 
    783 static void
    784 pad_swvol_codec(audio_filter_arg_t *arg)
    785 {
    786 	struct pad_softc *sc = arg->context;
    787 	const aint_t *src;
    788 	aint_t *dst;
    789 	u_int sample_count;
    790 	u_int i;
    791 
    792 	src = arg->src;
    793 	dst = arg->dst;
    794 	sample_count = arg->count * arg->srcfmt->channels;
    795 	for (i = 0; i < sample_count; i++) {
    796 		aint2_t v = (aint2_t)(*src++);
    797 		v = v * sc->sc_swvol / 255;
    798 		*dst++ = (aint_t)v;
    799 	}
    800 }
    801 
    802 MODULE(MODULE_CLASS_DRIVER, pad, "audio");
    803 
    804 #ifdef _MODULE
    805 
    806 #include "ioconf.c"
    807 
    808 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    809 
    810 /*
    811  * We need our own version of cfattach since config(1)'s ioconf does not
    812  * generate what we need
    813  */
    814 
    815 static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
    816 
    817 static struct cfattachinit pad_cfattach[] = {
    818 	{ "pad", pad_cfattachinit },
    819 	{ NULL, NULL }
    820 };
    821 #endif
    822 
    823 static int
    824 pad_modcmd(modcmd_t cmd, void *arg)
    825 {
    826 	int error = 0;
    827 
    828 	switch (cmd) {
    829 	case MODULE_CMD_INIT:
    830 #ifdef _MODULE
    831 		pad_cfattach[1] = cfattach_ioconf_pad[0];
    832 		error = config_init_component(cfdriver_ioconf_pad,
    833 		    pad_cfattach, cfdata_ioconf_pad);
    834 		if (error)
    835 			break;
    836 
    837 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    838 			    &pad_cdevsw, &cmajor);
    839 		if (error) {
    840 			config_fini_component(cfdriver_ioconf_pad,
    841 			    pad_cfattach, cfdata_ioconf_pad);
    842 			break;
    843 		}
    844 		mutex_init(&padconfig, MUTEX_DEFAULT, IPL_NONE);
    845 
    846 #endif
    847 		break;
    848 
    849 	case MODULE_CMD_FINI:
    850 #ifdef _MODULE
    851 		error = devsw_detach(NULL, &pad_cdevsw);
    852 		if (error)
    853 			break;
    854 
    855 		error = config_fini_component(cfdriver_ioconf_pad,
    856 		    pad_cfattach, cfdata_ioconf_pad);
    857 		if (error) {
    858 			devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    859 			    &pad_cdevsw, &cmajor);
    860 			break;
    861 		}
    862 		mutex_destroy(&padconfig);
    863 #endif
    864 		break;
    865 
    866 	default:
    867 		error = ENOTTY;
    868 	}
    869 
    870 	return error;
    871 }
    872