Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.25.2.2
      1 /* $NetBSD: pad.c,v 1.25.2.2 2016/07/18 11:25:07 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.25.2.2 2016/07/18 11:25:07 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/kmem.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/proc.h>
     40 #include <sys/condvar.h>
     41 #include <sys/select.h>
     42 #include <sys/audioio.h>
     43 #include <sys/vnode.h>
     44 #include <sys/module.h>
     45 #include <sys/atomic.h>
     46 #include <sys/time.h>
     47 #include <sys/localcount.h>
     48 
     49 #include <dev/audio_if.h>
     50 #include <dev/audiovar.h>
     51 #include <dev/auconv.h>
     52 #include <dev/auvolconv.h>
     53 
     54 #include <dev/pad/padvar.h>
     55 
     56 #define PADUNIT(x)	minor(x)
     57 
     58 extern struct cfdriver pad_cd;
     59 
     60 typedef struct pad_block {
     61 	uint8_t		*pb_ptr;
     62 	int		pb_len;
     63 } pad_block_t;
     64 
     65 enum {
     66 	PAD_OUTPUT_CLASS,
     67 	PAD_INPUT_CLASS,
     68 	PAD_OUTPUT_MASTER_VOLUME,
     69 	PAD_INPUT_DAC_VOLUME,
     70 	PAD_ENUM_LAST,
     71 };
     72 
     73 static int	pad_match(device_t, cfdata_t, void *);
     74 static void	pad_attach(device_t, device_t, void *);
     75 static int	pad_detach(device_t, int);
     76 static void	pad_childdet(device_t, device_t);
     77 
     78 static int	pad_query_encoding(void *, struct audio_encoding *);
     79 static int	pad_set_params(void *, int, int,
     80 				audio_params_t *, audio_params_t *,
     81 				stream_filter_list_t *, stream_filter_list_t *);
     82 static int	pad_start_output(void *, void *, int,
     83 				    void (*)(void *), void *);
     84 static int	pad_start_input(void *, void *, int,
     85 				   void (*)(void *), void *);
     86 static int	pad_halt_output(void *);
     87 static int	pad_halt_input(void *);
     88 static int	pad_getdev(void *, struct audio_device *);
     89 static int	pad_set_port(void *, mixer_ctrl_t *);
     90 static int	pad_get_port(void *, mixer_ctrl_t *);
     91 static int	pad_query_devinfo(void *, mixer_devinfo_t *);
     92 static int	pad_get_props(void *);
     93 static int	pad_round_blocksize(void *, int, int, const audio_params_t *);
     94 static void	pad_get_locks(void *, kmutex_t **, kmutex_t **);
     95 
     96 static stream_filter_t *pad_swvol_filter_le(struct audio_softc *,
     97     const audio_params_t *, const audio_params_t *);
     98 static stream_filter_t *pad_swvol_filter_be(struct audio_softc *,
     99     const audio_params_t *, const audio_params_t *);
    100 static void	pad_swvol_dtor(stream_filter_t *);
    101 
    102 static const struct audio_hw_if pad_hw_if = {
    103 	.query_encoding = pad_query_encoding,
    104 	.set_params = pad_set_params,
    105 	.start_output = pad_start_output,
    106 	.start_input = pad_start_input,
    107 	.halt_output = pad_halt_output,
    108 	.halt_input = pad_halt_input,
    109 	.getdev = pad_getdev,
    110 	.set_port = pad_set_port,
    111 	.get_port = pad_get_port,
    112 	.query_devinfo = pad_query_devinfo,
    113 	.get_props = pad_get_props,
    114 	.round_blocksize = pad_round_blocksize,
    115 	.get_locks = pad_get_locks,
    116 };
    117 
    118 #define PAD_NFORMATS	1
    119 static const struct audio_format pad_formats[PAD_NFORMATS] = {
    120 	{ NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    121 	  2, AUFMT_STEREO, 1, { 44100 } },
    122 };
    123 
    124 extern void	padattach(int);
    125 
    126 static int	pad_add_block(pad_softc_t *, uint8_t *, int);
    127 static int	pad_get_block(pad_softc_t *, pad_block_t *, int);
    128 
    129 dev_type_open(pad_open);
    130 dev_type_close(pad_close);
    131 dev_type_read(pad_read);
    132 
    133 #ifdef _MODULE
    134 struct localcount pad_localcount;
    135 #endif
    136 
    137 const struct cdevsw pad_cdevsw = {
    138 	.d_open = pad_open,
    139 	.d_close = pad_close,
    140 	.d_read = pad_read,
    141 	.d_write = nowrite,
    142 	.d_ioctl = noioctl,
    143 	.d_stop = nostop,
    144 	.d_tty = notty,
    145 	.d_poll = nopoll,
    146 	.d_mmap = nommap,
    147 	.d_kqfilter = nokqfilter,
    148 	.d_discard = nodiscard,
    149 #ifdef _MODULE
    150 	.d_localcount = &pad_localcount,
    151 #endif
    152 	.d_flag = D_OTHER | D_MPSAFE,
    153 };
    154 
    155 CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
    156     NULL, NULL, pad_childdet);
    157 
    158 void
    159 padattach(int n)
    160 {
    161 	int i, err;
    162 	cfdata_t cf;
    163 
    164 	aprint_debug("pad: requested %d units\n", n);
    165 
    166 	err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    167 	if (err) {
    168 		aprint_error("%s: couldn't register cfattach: %d\n",
    169 		    pad_cd.cd_name, err);
    170 		config_cfdriver_detach(&pad_cd);
    171 		return;
    172 	}
    173 
    174 	for (i = 0; i < n; i++) {
    175 		cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
    176 		if (cf == NULL) {
    177 			aprint_error("%s: couldn't allocate cfdata\n",
    178 			    pad_cd.cd_name);
    179 			continue;
    180 		}
    181 		cf->cf_name = pad_cd.cd_name;
    182 		cf->cf_atname = pad_cd.cd_name;
    183 		cf->cf_unit = i;
    184 		cf->cf_fstate = FSTATE_STAR;
    185 
    186 		(void)config_attach_pseudo(cf);
    187 	}
    188 
    189 	return;
    190 }
    191 
    192 static int
    193 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
    194 {
    195 	int l;
    196 
    197 	if (sc->sc_open == 0)
    198 		return EIO;
    199 
    200 	KASSERT(mutex_owned(&sc->sc_lock));
    201 
    202 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
    203 		return ENOBUFS;
    204 
    205 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    206 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    207 	else {
    208 		l = PAD_BUFSIZE - sc->sc_wpos;
    209 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    210 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    211 	}
    212 
    213 	sc->sc_wpos += blksize;
    214 	if (sc->sc_wpos > PAD_BUFSIZE)
    215 		sc->sc_wpos -= PAD_BUFSIZE;
    216 
    217 	sc->sc_buflen += blksize;
    218 
    219 	return 0;
    220 }
    221 
    222 static int
    223 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
    224 {
    225 	int l;
    226 
    227 	KASSERT(mutex_owned(&sc->sc_lock));
    228 	KASSERT(pb != NULL);
    229 
    230 	if (sc->sc_buflen < blksize)
    231 		return ERESTART;
    232 
    233 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    234 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    235 		pb->pb_len = blksize;
    236 		sc->sc_rpos += blksize;
    237 	} else {
    238 		l = PAD_BUFSIZE - sc->sc_rpos;
    239 		pb->pb_len = l;
    240 		sc->sc_rpos = 0;
    241 	}
    242 	sc->sc_buflen -= pb->pb_len;
    243 
    244 	return 0;
    245 }
    246 
    247 static int
    248 pad_match(device_t parent, cfdata_t data, void *opaque)
    249 {
    250 
    251 	return 1;
    252 }
    253 
    254 static void
    255 pad_childdet(device_t self, device_t child)
    256 {
    257 	pad_softc_t *sc = device_private(self);
    258 
    259 	sc->sc_audiodev = NULL;
    260 }
    261 
    262 static void
    263 pad_attach(device_t parent, device_t self, void *opaque)
    264 {
    265 	pad_softc_t *sc = device_private(self);
    266 
    267 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    268 
    269 	sc->sc_dev = self;
    270 	sc->sc_open = 0;
    271 	if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
    272 	    &sc->sc_encodings) != 0) {
    273 		aprint_error_dev(self, "couldn't create encodings\n");
    274 		return;
    275 	}
    276 
    277 	cv_init(&sc->sc_condvar, device_xname(self));
    278 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    279 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
    280 
    281 	sc->sc_swvol = 255;
    282 	sc->sc_buflen = 0;
    283 	sc->sc_rpos = sc->sc_wpos = 0;
    284 	sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
    285 
    286 	if (!pmf_device_register(self, NULL, NULL))
    287 		aprint_error_dev(self, "couldn't establish power handler\n");
    288 
    289 	return;
    290 }
    291 
    292 static int
    293 pad_detach(device_t self, int flags)
    294 {
    295 	pad_softc_t *sc = device_private(self);
    296 	int cmaj, mn, rc;
    297 
    298 	cmaj = cdevsw_lookup_major(&pad_cdevsw);
    299 	mn = device_unit(self);
    300 	vdevgone(cmaj, mn, mn, VCHR);
    301 
    302 	if ((rc = config_detach_children(self, flags)) != 0)
    303 		return rc;
    304 
    305 	pmf_device_deregister(self);
    306 
    307 	mutex_destroy(&sc->sc_lock);
    308 	mutex_destroy(&sc->sc_intr_lock);
    309 	cv_destroy(&sc->sc_condvar);
    310 
    311 	auconv_delete_encodings(sc->sc_encodings);
    312 
    313 	return 0;
    314 }
    315 
    316 int
    317 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    318 {
    319 	pad_softc_t *sc;
    320 
    321 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    322 	if (sc == NULL)
    323 		return ENXIO;
    324 
    325 	if (atomic_swap_uint(&sc->sc_open, 1) != 0) {
    326 		return EBUSY;
    327 	}
    328 
    329 	getmicrotime(&sc->sc_last);
    330 	sc->sc_bytes_count = 0;
    331 
    332 	return 0;
    333 }
    334 
    335 int
    336 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
    337 {
    338 	pad_softc_t *sc;
    339 
    340 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    341 	if (sc == NULL)
    342 		return ENXIO;
    343 
    344 	KASSERT(sc->sc_open > 0);
    345 	sc->sc_open = 0;
    346 
    347 	return 0;
    348 }
    349 
    350 #define PAD_BYTES_PER_SEC (44100 * sizeof(int16_t) * 2)
    351 #define TIMENEXTREAD	(20 * 1000)
    352 #define BYTESTOSLEEP (PAD_BYTES_PER_SEC / (1000000 / TIMENEXTREAD))
    353 
    354 int
    355 pad_read(dev_t dev, struct uio *uio, int flags)
    356 {
    357 	struct timeval now;
    358 	uint64_t nowusec, lastusec;
    359 	pad_softc_t *sc;
    360 	pad_block_t pb;
    361 	void (*intr)(void *);
    362 	void *intrarg;
    363 	int err, wait_ticks;
    364 
    365 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    366 	if (sc == NULL)
    367 		return ENXIO;
    368 
    369 	err = 0;
    370 
    371 	mutex_enter(&sc->sc_lock);
    372 	intr = sc->sc_intr;
    373 	intrarg = sc->sc_intrarg;
    374 
    375 	while (uio->uio_resid > 0 && !err) {
    376 		getmicrotime(&now);
    377 		nowusec = (now.tv_sec * 1000000) + now.tv_usec;
    378 		lastusec = (sc->sc_last.tv_sec * 1000000) +
    379 		     sc->sc_last.tv_usec;
    380 		if (lastusec + TIMENEXTREAD > nowusec &&
    381 		     sc->sc_bytes_count >= BYTESTOSLEEP) {
    382 			wait_ticks = (hz * ((lastusec + TIMENEXTREAD) -
    383 			     nowusec)) / 1000000;
    384 			if (wait_ticks > 0) {
    385 				kpause("padwait", TRUE, wait_ticks,
    386 				     &sc->sc_lock);
    387 			}
    388 
    389 			sc->sc_bytes_count -= BYTESTOSLEEP;
    390 			getmicrotime(&sc->sc_last);
    391 		} else if (sc->sc_bytes_count >= BYTESTOSLEEP) {
    392 			sc->sc_bytes_count -= BYTESTOSLEEP;
    393 			getmicrotime(&sc->sc_last);
    394 		} else if (lastusec + TIMENEXTREAD <= nowusec)
    395 			getmicrotime(&sc->sc_last);
    396 
    397 		err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
    398 		if (!err) {
    399 			sc->sc_bytes_count += pb.pb_len;
    400 
    401 			mutex_exit(&sc->sc_lock);
    402 			err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    403 			mutex_enter(&sc->sc_lock);
    404 			continue;
    405 		}
    406 
    407 		if (intr) {
    408 			mutex_enter(&sc->sc_intr_lock);
    409 			kpreempt_disable();
    410 			(*intr)(intrarg);
    411 			kpreempt_enable();
    412 			mutex_exit(&sc->sc_intr_lock);
    413 			intr = sc->sc_intr;
    414 			intrarg = sc->sc_intrarg;
    415 			err = 0;
    416 			continue;
    417 		}
    418 		err = cv_wait_sig(&sc->sc_condvar, &sc->sc_lock);
    419 		if (err != 0)
    420 			break;
    421 
    422 		intr = sc->sc_intr;
    423 		intrarg = sc->sc_intrarg;
    424 	}
    425 	mutex_exit(&sc->sc_lock);
    426 
    427 	return err;
    428 }
    429 
    430 static int
    431 pad_query_encoding(void *opaque, struct audio_encoding *ae)
    432 {
    433 	pad_softc_t *sc;
    434 
    435 	sc = (pad_softc_t *)opaque;
    436 
    437 	KASSERT(mutex_owned(&sc->sc_lock));
    438 
    439 	return auconv_query_encoding(sc->sc_encodings, ae);
    440 }
    441 
    442 static int
    443 pad_set_params(void *opaque, int setmode, int usemode,
    444     audio_params_t *play, audio_params_t *rec,
    445     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    446 {
    447 	pad_softc_t *sc __diagused;
    448 
    449 	sc = (pad_softc_t *)opaque;
    450 
    451 	KASSERT(mutex_owned(&sc->sc_lock));
    452 
    453 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
    454 	    play, true, pfil) < 0)
    455 		return EINVAL;
    456 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
    457 	    rec, true, rfil) < 0)
    458 		return EINVAL;
    459 
    460 	if (pfil->req_size > 0)
    461 		play = &pfil->filters[0].param;
    462 	switch (play->encoding) {
    463 	case AUDIO_ENCODING_SLINEAR_LE:
    464 		if (play->precision == 16 && play->validbits == 16)
    465 			pfil->prepend(pfil, pad_swvol_filter_le, play);
    466 		break;
    467 	case AUDIO_ENCODING_SLINEAR_BE:
    468 		if (play->precision == 16 && play->validbits == 16)
    469 			pfil->prepend(pfil, pad_swvol_filter_be, play);
    470 		break;
    471 	default:
    472 		break;
    473 	}
    474 
    475 	return 0;
    476 }
    477 
    478 static int
    479 pad_start_output(void *opaque, void *block, int blksize,
    480     void (*intr)(void *), void *intrarg)
    481 {
    482 	pad_softc_t *sc;
    483 	int err;
    484 
    485 	sc = (pad_softc_t *)opaque;
    486 
    487 	KASSERT(mutex_owned(&sc->sc_lock));
    488 
    489 	sc->sc_intr = intr;
    490 	sc->sc_intrarg = intrarg;
    491 	sc->sc_blksize = blksize;
    492 
    493 	err = pad_add_block(sc, block, blksize);
    494 
    495 	cv_broadcast(&sc->sc_condvar);
    496 
    497 	return err;
    498 }
    499 
    500 static int
    501 pad_start_input(void *opaque, void *block, int blksize,
    502     void (*intr)(void *), void *intrarg)
    503 {
    504 	pad_softc_t *sc __diagused;
    505 
    506 	sc = (pad_softc_t *)opaque;
    507 
    508 	KASSERT(mutex_owned(&sc->sc_lock));
    509 
    510 	return EOPNOTSUPP;
    511 }
    512 
    513 static int
    514 pad_halt_output(void *opaque)
    515 {
    516 	pad_softc_t *sc;
    517 
    518 	sc = (pad_softc_t *)opaque;
    519 
    520 	KASSERT(mutex_owned(&sc->sc_lock));
    521 
    522 	sc->sc_intr = NULL;
    523 	sc->sc_intrarg = NULL;
    524 	sc->sc_buflen = 0;
    525 	sc->sc_rpos = sc->sc_wpos = 0;
    526 
    527 	return 0;
    528 }
    529 
    530 static int
    531 pad_halt_input(void *opaque)
    532 {
    533 	pad_softc_t *sc __diagused;
    534 
    535 	sc = (pad_softc_t *)opaque;
    536 
    537 	KASSERT(mutex_owned(&sc->sc_lock));
    538 
    539 	return 0;
    540 }
    541 
    542 static int
    543 pad_getdev(void *opaque, struct audio_device *ret)
    544 {
    545 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    546 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    547 	strlcpy(ret->config, "pad", sizeof(ret->config));
    548 
    549 	return 0;
    550 }
    551 
    552 static int
    553 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    554 {
    555 	pad_softc_t *sc;
    556 
    557 	sc = (pad_softc_t *)opaque;
    558 
    559 	KASSERT(mutex_owned(&sc->sc_lock));
    560 
    561 	switch (mc->dev) {
    562 	case PAD_OUTPUT_MASTER_VOLUME:
    563 	case PAD_INPUT_DAC_VOLUME:
    564 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    565 		return 0;
    566 	}
    567 
    568 	return ENXIO;
    569 }
    570 
    571 static int
    572 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    573 {
    574 	pad_softc_t *sc;
    575 
    576 	sc = (pad_softc_t *)opaque;
    577 
    578 	KASSERT(mutex_owned(&sc->sc_lock));
    579 
    580 	switch (mc->dev) {
    581 	case PAD_OUTPUT_MASTER_VOLUME:
    582 	case PAD_INPUT_DAC_VOLUME:
    583 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    584 		return 0;
    585 	}
    586 
    587 	return ENXIO;
    588 }
    589 
    590 static int
    591 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    592 {
    593 	pad_softc_t *sc __diagused;
    594 
    595 	sc = (pad_softc_t *)opaque;
    596 
    597 	KASSERT(mutex_owned(&sc->sc_lock));
    598 
    599 	switch (di->index) {
    600 	case PAD_OUTPUT_CLASS:
    601 		di->mixer_class = PAD_OUTPUT_CLASS;
    602 		strcpy(di->label.name, AudioCoutputs);
    603 		di->type = AUDIO_MIXER_CLASS;
    604 		di->next = di->prev = AUDIO_MIXER_LAST;
    605 		return 0;
    606 	case PAD_INPUT_CLASS:
    607 		di->mixer_class = PAD_INPUT_CLASS;
    608 		strcpy(di->label.name, AudioCinputs);
    609 		di->type = AUDIO_MIXER_CLASS;
    610 		di->next = di->prev = AUDIO_MIXER_LAST;
    611 		return 0;
    612 	case PAD_OUTPUT_MASTER_VOLUME:
    613 		di->mixer_class = PAD_OUTPUT_CLASS;
    614 		strcpy(di->label.name, AudioNmaster);
    615 		di->type = AUDIO_MIXER_VALUE;
    616 		di->next = di->prev = AUDIO_MIXER_LAST;
    617 		di->un.v.num_channels = 1;
    618 		strcpy(di->un.v.units.name, AudioNvolume);
    619 		return 0;
    620 	case PAD_INPUT_DAC_VOLUME:
    621 		di->mixer_class = PAD_INPUT_CLASS;
    622 		strcpy(di->label.name, AudioNdac);
    623 		di->type = AUDIO_MIXER_VALUE;
    624 		di->next = di->prev = AUDIO_MIXER_LAST;
    625 		di->un.v.num_channels = 1;
    626 		strcpy(di->un.v.units.name, AudioNvolume);
    627 		return 0;
    628 	}
    629 
    630 	return ENXIO;
    631 }
    632 
    633 static int
    634 pad_get_props(void *opaque)
    635 {
    636 	pad_softc_t *sc __diagused;
    637 
    638 	sc = (pad_softc_t *)opaque;
    639 
    640 	KASSERT(mutex_owned(&sc->sc_lock));
    641 
    642 	return 0;
    643 }
    644 
    645 static int
    646 pad_round_blocksize(void *opaque, int blksize, int mode,
    647     const audio_params_t *p)
    648 {
    649 	pad_softc_t *sc __diagused;
    650 
    651 	sc = (pad_softc_t *)opaque;
    652 	KASSERT(mutex_owned(&sc->sc_lock));
    653 
    654 	return PAD_BLKSIZE;
    655 }
    656 
    657 static void
    658 pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    659 {
    660 	pad_softc_t *sc;
    661 
    662 	sc = (pad_softc_t *)opaque;
    663 
    664 	*intr = &sc->sc_intr_lock;
    665 	*thread = &sc->sc_lock;
    666 }
    667 
    668 static stream_filter_t *
    669 pad_swvol_filter_le(struct audio_softc *asc,
    670     const audio_params_t *from, const audio_params_t *to)
    671 {
    672 	auvolconv_filter_t *this;
    673 	device_t dev = audio_get_device(asc);
    674 	struct pad_softc *sc = device_private(dev);
    675 
    676 	this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
    677 	this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to;
    678 	this->base.dtor = pad_swvol_dtor;
    679 	this->base.set_fetcher = stream_filter_set_fetcher;
    680 	this->base.set_inputbuffer = stream_filter_set_inputbuffer;
    681 	this->vol = &sc->sc_swvol;
    682 
    683 	return (stream_filter_t *)this;
    684 }
    685 
    686 static stream_filter_t *
    687 pad_swvol_filter_be(struct audio_softc *asc,
    688     const audio_params_t *from, const audio_params_t *to)
    689 {
    690 	auvolconv_filter_t *this;
    691 	device_t dev = audio_get_device(asc);
    692 	struct pad_softc *sc = device_private(dev);
    693 
    694 	this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
    695 	this->base.base.fetch_to = auvolconv_slinear16_be_fetch_to;
    696 	this->base.dtor = pad_swvol_dtor;
    697 	this->base.set_fetcher = stream_filter_set_fetcher;
    698 	this->base.set_inputbuffer = stream_filter_set_inputbuffer;
    699 	this->vol = &sc->sc_swvol;
    700 
    701 	return (stream_filter_t *)this;
    702 }
    703 
    704 static void
    705 pad_swvol_dtor(stream_filter_t *this)
    706 {
    707 	if (this)
    708 		kmem_free(this, sizeof(auvolconv_filter_t));
    709 }
    710 
    711 #ifdef _MODULE
    712 
    713 MODULE(MODULE_CLASS_DRIVER, pad, NULL);
    714 
    715 static const struct cfiattrdata audiobuscf_iattrdata = {
    716 	"audiobus", 0, { { NULL, NULL, 0 }, }
    717 };
    718 static const struct cfiattrdata * const pad_attrs[] = {
    719 	&audiobuscf_iattrdata, NULL
    720 };
    721 
    722 CFDRIVER_DECL(pad, DV_DULL, pad_attrs);
    723 extern struct cfattach pad_ca;
    724 static int padloc[] = { -1, -1 };
    725 
    726 static struct cfdata pad_cfdata[] = {
    727 	{
    728 		.cf_name = "pad",
    729 		.cf_atname = "pad",
    730 		.cf_unit = 0,
    731 		.cf_fstate = FSTATE_STAR,
    732 		.cf_loc = padloc,
    733 		.cf_flags = 0,
    734 		.cf_pspec = NULL,
    735 	},
    736 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    737 };
    738 
    739 static int
    740 pad_modcmd(modcmd_t cmd, void *arg)
    741 {
    742 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    743 	int error;
    744 
    745 	switch (cmd) {
    746 	case MODULE_CMD_INIT:
    747 		error = config_cfdriver_attach(&pad_cd);
    748 		if (error) {
    749 			return error;
    750 		}
    751 
    752 		error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    753 		if (error) {
    754 			config_cfdriver_detach(&pad_cd);
    755 			aprint_error("%s: unable to register cfattach\n",
    756 				pad_cd.cd_name);
    757 
    758 			return error;
    759 		}
    760 
    761 		error = config_cfdata_attach(pad_cfdata, 1);
    762 		if (error) {
    763 			config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    764 			config_cfdriver_detach(&pad_cd);
    765 			aprint_error("%s: unable to register cfdata\n",
    766 				pad_cd.cd_name);
    767 
    768 			return error;
    769 		}
    770 
    771 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    772 		    &pad_cdevsw, &cmajor);
    773 		if (error) {
    774 			error = config_cfdata_detach(pad_cfdata);
    775 			if (error) {
    776 				return error;
    777 			}
    778 			config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    779 			config_cfdriver_detach(&pad_cd);
    780 			aprint_error("%s: unable to register devsw\n",
    781 				pad_cd.cd_name);
    782 
    783 			return error;
    784 		}
    785 
    786 		(void)config_attach_pseudo(pad_cfdata);
    787 
    788 		return 0;
    789 	case MODULE_CMD_FINI:
    790 		error = config_cfdata_detach(pad_cfdata);
    791 		if (error) {
    792 			return error;
    793 		}
    794 
    795 		config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    796 		config_cfdriver_detach(&pad_cd);
    797 		devsw_detach(NULL, &pad_cdevsw);
    798 
    799 		return 0;
    800 	default:
    801 		return ENOTTY;
    802 	}
    803 }
    804 
    805 #endif
    806