Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.66
      1 /* $NetBSD: pad.c,v 1.66 2021/06/08 09:09:28 nia 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.66 2021/06/08 09:09:28 nia 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 	sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
    366 
    367 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    368 		aprint_error_dev(sc->sc_dev,
    369 		    "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 	struct pad_softc *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 	struct pad_softc *sc;
    443 	sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
    444 
    445 	return pad_close(sc);
    446 }
    447 
    448 static int
    449 fops_pad_poll(struct file *fp, int events)
    450 {
    451 
    452 	return POLLERR;
    453 }
    454 
    455 static int
    456 fops_pad_kqfilter(struct file *fp, struct knote *kn)
    457 {
    458 	struct pad_softc *sc;
    459 	dev_t dev;
    460 
    461 	sc = fp->f_pad;
    462 	if (sc == NULL)
    463 		return EIO;
    464 
    465 	dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    466 	    device_unit(sc->sc_dev));
    467 
    468 	return seltrue_kqfilter(dev, kn);
    469 }
    470 
    471 static int
    472 fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
    473 {
    474 
    475 	return ENODEV;
    476 }
    477 
    478 static int
    479 fops_pad_stat(struct file *fp, struct stat *st)
    480 {
    481 	struct pad_softc *sc;
    482 
    483 	sc = fp->f_pad;
    484 	if (sc == NULL)
    485 		return EIO;
    486 
    487 	memset(st, 0, sizeof(*st));
    488 
    489 	st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    490 	    device_unit(sc->sc_dev));
    491 
    492 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    493 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    494 	st->st_mode = S_IFCHR;
    495 
    496 	return 0;
    497 }
    498 
    499 static int
    500 fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
    501     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    502 {
    503 
    504 	return 1;
    505 }
    506 
    507 int
    508 cdev_pad_read(dev_t dev, struct uio *uio, int ioflag)
    509 {
    510 	struct pad_softc *sc;
    511 
    512 	sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
    513 	if (sc == NULL)
    514 		return ENXIO;
    515 
    516 	return pad_read(sc, NULL, uio, NULL, ioflag);
    517 }
    518 
    519 static int
    520 fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    521     int ioflag)
    522 {
    523 	struct pad_softc *sc;
    524 
    525 	sc = fp->f_pad;
    526 	if (sc == NULL)
    527 		return ENXIO;
    528 
    529 	return pad_read(sc, offp, uio, cred, ioflag);
    530 }
    531 
    532 static int
    533 pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
    534     int ioflag)
    535 {
    536 	pad_block_t pb;
    537 	int len;
    538 	int err;
    539 
    540 	err = 0;
    541 	DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
    542 	while (uio->uio_resid > 0 && !err) {
    543 		mutex_enter(&sc->sc_cond_lock);
    544 		if (sc->sc_buflen == 0) {
    545 			DPRINTF("%s: wait\n", __func__);
    546 			err = cv_wait_sig(&sc->sc_condvar, &sc->sc_cond_lock);
    547 			DPRINTF("%s: wake up %d\n", __func__, err);
    548 			mutex_exit(&sc->sc_cond_lock);
    549 			if (err) {
    550 				if (err == ERESTART)
    551 					err = EINTR;
    552 				break;
    553 			}
    554 			if (sc->sc_buflen == 0)
    555 				break;
    556 			continue;
    557 		}
    558 
    559 		len = uimin(uio->uio_resid, sc->sc_buflen);
    560 		err = pad_get_block(sc, &pb, len);
    561 		mutex_exit(&sc->sc_cond_lock);
    562 		if (err)
    563 			break;
    564 		DPRINTF("%s: move %d\n", __func__, pb.pb_len);
    565 		uiomove(pb.pb_ptr, pb.pb_len, uio);
    566 	}
    567 
    568 	return err;
    569 }
    570 
    571 static int
    572 fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    573     int ioflag)
    574 {
    575 
    576 	return EOPNOTSUPP;
    577 }
    578 
    579 static int
    580 pad_query_format(void *opaque, audio_format_query_t *afp)
    581 {
    582 
    583 	return audio_query_format(pad_formats, PAD_NFORMATS, afp);
    584 }
    585 
    586 static int
    587 pad_set_format(void *opaque, int setmode,
    588     const audio_params_t *play, const audio_params_t *rec,
    589     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    590 {
    591 	struct pad_softc *sc;
    592 
    593 	sc = (struct pad_softc *)opaque;
    594 
    595 	KASSERT(mutex_owned(&sc->sc_lock));
    596 
    597 	/* XXX playback only */
    598 	pfil->codec = pad_swvol_codec;
    599 	pfil->context = sc;
    600 
    601 	return 0;
    602 }
    603 
    604 static int
    605 pad_start_output(void *opaque, void *block, int blksize,
    606     void (*intr)(void *), void *intrarg)
    607 {
    608 	struct pad_softc *sc;
    609 	int err;
    610 	int ms;
    611 
    612 	sc = (struct pad_softc *)opaque;
    613 
    614 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    615 
    616 	sc->sc_intr = intr;
    617 	sc->sc_intrarg = intrarg;
    618 	sc->sc_blksize = blksize;
    619 
    620 	DPRINTF("%s: blksize=%d\n", __func__, blksize);
    621 	mutex_enter(&sc->sc_cond_lock);
    622 	err = pad_add_block(sc, block, blksize);
    623 	mutex_exit(&sc->sc_cond_lock);
    624 	cv_broadcast(&sc->sc_condvar);
    625 
    626 	ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
    627 	DPRINTF("%s: callout ms=%d\n", __func__, ms);
    628 	callout_reset(&sc->sc_pcallout, mstohz(ms), pad_done_output, sc);
    629 
    630 	return err;
    631 }
    632 
    633 static int
    634 pad_halt_output(void *opaque)
    635 {
    636 	struct pad_softc *sc;
    637 
    638 	sc = (struct pad_softc *)opaque;
    639 
    640 	DPRINTF("%s\n", __func__);
    641 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    642 
    643 	cv_broadcast(&sc->sc_condvar);
    644 	callout_stop(&sc->sc_pcallout);
    645 	sc->sc_intr = NULL;
    646 	sc->sc_intrarg = NULL;
    647 	sc->sc_buflen = 0;
    648 	sc->sc_rpos = sc->sc_wpos = 0;
    649 
    650 	return 0;
    651 }
    652 
    653 static void
    654 pad_done_output(void *arg)
    655 {
    656 	struct pad_softc *sc;
    657 
    658 	DPRINTF("%s\n", __func__);
    659 	sc = (struct pad_softc *)arg;
    660 	callout_stop(&sc->sc_pcallout);
    661 
    662 	mutex_enter(&sc->sc_intr_lock);
    663 	(*sc->sc_intr)(sc->sc_intrarg);
    664 	mutex_exit(&sc->sc_intr_lock);
    665 }
    666 
    667 static int
    668 pad_getdev(void *opaque, struct audio_device *ret)
    669 {
    670 
    671 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    672 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    673 	strlcpy(ret->config, "pad", sizeof(ret->config));
    674 
    675 	return 0;
    676 }
    677 
    678 static int
    679 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    680 {
    681 	struct pad_softc *sc;
    682 
    683 	sc = (struct pad_softc *)opaque;
    684 
    685 	KASSERT(mutex_owned(&sc->sc_lock));
    686 
    687 	switch (mc->dev) {
    688 	case PAD_OUTPUT_MASTER_VOLUME:
    689 	case PAD_INPUT_DAC_VOLUME:
    690 		if (mc->un.value.num_channels != 1)
    691 			return EINVAL;
    692 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    693 		return 0;
    694 	}
    695 
    696 	return ENXIO;
    697 }
    698 
    699 static int
    700 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    701 {
    702 	struct pad_softc *sc;
    703 
    704 	sc = (struct pad_softc *)opaque;
    705 
    706 	KASSERT(mutex_owned(&sc->sc_lock));
    707 
    708 	switch (mc->dev) {
    709 	case PAD_OUTPUT_MASTER_VOLUME:
    710 	case PAD_INPUT_DAC_VOLUME:
    711 		if (mc->un.value.num_channels != 1)
    712 			return EINVAL;
    713 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    714 		return 0;
    715 	}
    716 
    717 	return ENXIO;
    718 }
    719 
    720 static int
    721 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    722 {
    723 	struct pad_softc *sc __diagused;
    724 
    725 	sc = (struct pad_softc *)opaque;
    726 
    727 	KASSERT(mutex_owned(&sc->sc_lock));
    728 
    729 	switch (di->index) {
    730 	case PAD_OUTPUT_CLASS:
    731 		di->mixer_class = PAD_OUTPUT_CLASS;
    732 		strcpy(di->label.name, AudioCoutputs);
    733 		di->type = AUDIO_MIXER_CLASS;
    734 		di->next = di->prev = AUDIO_MIXER_LAST;
    735 		return 0;
    736 	case PAD_INPUT_CLASS:
    737 		di->mixer_class = PAD_INPUT_CLASS;
    738 		strcpy(di->label.name, AudioCinputs);
    739 		di->type = AUDIO_MIXER_CLASS;
    740 		di->next = di->prev = AUDIO_MIXER_LAST;
    741 		return 0;
    742 	case PAD_OUTPUT_MASTER_VOLUME:
    743 		di->mixer_class = PAD_OUTPUT_CLASS;
    744 		strcpy(di->label.name, AudioNmaster);
    745 		di->type = AUDIO_MIXER_VALUE;
    746 		di->next = di->prev = AUDIO_MIXER_LAST;
    747 		di->un.v.num_channels = 1;
    748 		strcpy(di->un.v.units.name, AudioNvolume);
    749 		return 0;
    750 	case PAD_INPUT_DAC_VOLUME:
    751 		di->mixer_class = PAD_INPUT_CLASS;
    752 		strcpy(di->label.name, AudioNdac);
    753 		di->type = AUDIO_MIXER_VALUE;
    754 		di->next = di->prev = AUDIO_MIXER_LAST;
    755 		di->un.v.num_channels = 1;
    756 		strcpy(di->un.v.units.name, AudioNvolume);
    757 		return 0;
    758 	}
    759 
    760 	return ENXIO;
    761 }
    762 
    763 static int
    764 pad_get_props(void *opaque)
    765 {
    766 
    767 	return AUDIO_PROP_PLAYBACK;
    768 }
    769 
    770 static void
    771 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    772 {
    773 	struct pad_softc *sc;
    774 
    775 	sc = (struct pad_softc *)opaque;
    776 
    777 	*intr = &sc->sc_intr_lock;
    778 	*thread = &sc->sc_lock;
    779 }
    780 
    781 static void
    782 pad_swvol_codec(audio_filter_arg_t *arg)
    783 {
    784 	struct pad_softc *sc = arg->context;
    785 	const aint_t *src;
    786 	aint_t *dst;
    787 	u_int sample_count;
    788 	u_int i;
    789 
    790 	src = arg->src;
    791 	dst = arg->dst;
    792 	sample_count = arg->count * arg->srcfmt->channels;
    793 	for (i = 0; i < sample_count; i++) {
    794 		aint2_t v = (aint2_t)(*src++);
    795 		v = v * sc->sc_swvol / 255;
    796 		*dst++ = (aint_t)v;
    797 	}
    798 }
    799 
    800 MODULE(MODULE_CLASS_DRIVER, pad, "audio");
    801 
    802 #ifdef _MODULE
    803 
    804 #include "ioconf.c"
    805 
    806 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    807 
    808 /*
    809  * We need our own version of cfattach since config(1)'s ioconf does not
    810  * generate what we need
    811  */
    812 
    813 static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
    814 
    815 static struct cfattachinit pad_cfattach[] = {
    816 	{ "pad", pad_cfattachinit },
    817 	{ NULL, NULL }
    818 };
    819 #endif
    820 
    821 static int
    822 pad_modcmd(modcmd_t cmd, void *arg)
    823 {
    824 	int error = 0;
    825 
    826 	switch (cmd) {
    827 	case MODULE_CMD_INIT:
    828 #ifdef _MODULE
    829 		pad_cfattach[1] = cfattach_ioconf_pad[0];
    830 		error = config_init_component(cfdriver_ioconf_pad,
    831 		    pad_cfattach, cfdata_ioconf_pad);
    832 		if (error)
    833 			break;
    834 
    835 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    836 			    &pad_cdevsw, &cmajor);
    837 		if (error) {
    838 			config_fini_component(cfdriver_ioconf_pad,
    839 			    pad_cfattach, cfdata_ioconf_pad);
    840 			break;
    841 		}
    842 		mutex_init(&padconfig, MUTEX_DEFAULT, IPL_NONE);
    843 
    844 #endif
    845 		break;
    846 
    847 	case MODULE_CMD_FINI:
    848 #ifdef _MODULE
    849 		error = devsw_detach(NULL, &pad_cdevsw);
    850 		if (error)
    851 			break;
    852 
    853 		error = config_fini_component(cfdriver_ioconf_pad,
    854 		    pad_cfattach, cfdata_ioconf_pad);
    855 		if (error) {
    856 			devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    857 			    &pad_cdevsw, &cmajor);
    858 			break;
    859 		}
    860 		mutex_destroy(&padconfig);
    861 #endif
    862 		break;
    863 
    864 	default:
    865 		error = ENOTTY;
    866 	}
    867 
    868 	return error;
    869 }
    870