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