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