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