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