Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.8
      1 /* $NetBSD: pad.c,v 1.8 2008/06/08 18:14:31 tsutsui 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.8 2008/06/08 18:14:31 tsutsui 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 
     45 #include <dev/audio_if.h>
     46 #include <dev/audiovar.h>
     47 #include <dev/auconv.h>
     48 
     49 #include <dev/pad/padvar.h>
     50 #include <dev/pad/padvol.h>
     51 
     52 #define PADUNIT(x)	minor(x)
     53 
     54 extern struct cfdriver pad_cd;
     55 
     56 static struct audio_device pad_device = {
     57 	"Pseudo Audio",
     58 	"1.0",
     59 	"pad",
     60 };
     61 
     62 typedef struct pad_block {
     63 	uint8_t		*pb_ptr;
     64 	int		pb_len;
     65 } pad_block_t;
     66 
     67 enum {
     68 	PAD_OUTPUT_CLASS,
     69 	PAD_INPUT_CLASS,
     70 	PAD_OUTPUT_MASTER_VOLUME,
     71 	PAD_INPUT_DAC_VOLUME,
     72 	PAD_ENUM_LAST,
     73 };
     74 
     75 static int	pad_match(device_t, struct cfdata *, void *);
     76 static void	pad_attach(device_t, device_t, void *);
     77 static int	pad_detach(device_t, int);
     78 static void	pad_childdet(device_t, device_t);
     79 
     80 static int	pad_query_encoding(void *, struct audio_encoding *);
     81 static int	pad_set_params(void *, int, int,
     82 				audio_params_t *, audio_params_t *,
     83 				stream_filter_list_t *, stream_filter_list_t *);
     84 static int	pad_start_output(void *, void *, int,
     85 				    void (*)(void *), void *);
     86 static int	pad_start_input(void *, void *, int,
     87 				   void (*)(void *), void *);
     88 static int	pad_halt_output(void *);
     89 static int	pad_halt_input(void *);
     90 static int	pad_getdev(void *, struct audio_device *);
     91 static int	pad_set_port(void *, mixer_ctrl_t *);
     92 static int	pad_get_port(void *, mixer_ctrl_t *);
     93 static int	pad_query_devinfo(void *, mixer_devinfo_t *);
     94 static int	pad_get_props(void *);
     95 static int	pad_round_blocksize(void *, int, int, const audio_params_t *);
     96 
     97 static const struct audio_hw_if pad_hw_if = {
     98 	.query_encoding = pad_query_encoding,
     99 	.set_params = pad_set_params,
    100 	.start_output = pad_start_output,
    101 	.start_input = pad_start_input,
    102 	.halt_output = pad_halt_output,
    103 	.halt_input = pad_halt_input,
    104 	.getdev = pad_getdev,
    105 	.set_port = pad_set_port,
    106 	.get_port = pad_get_port,
    107 	.query_devinfo = pad_query_devinfo,
    108 	.get_props = pad_get_props,
    109 	.round_blocksize = pad_round_blocksize,
    110 };
    111 
    112 #define PAD_NFORMATS	1
    113 static const struct audio_format pad_formats[PAD_NFORMATS] = {
    114 	{ NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    115 	  2, AUFMT_STEREO, 1, { 44100 } },
    116 };
    117 
    118 extern void	padattach(int);
    119 
    120 static int		pad_add_block(pad_softc_t *, uint8_t *, int);
    121 static int		pad_get_block(pad_softc_t *, pad_block_t *, int);
    122 
    123 dev_type_open(pad_open);
    124 dev_type_close(pad_close);
    125 dev_type_read(pad_read);
    126 
    127 const struct cdevsw pad_cdevsw = {
    128 	.d_open = pad_open,
    129 	.d_close = pad_close,
    130 	.d_read = pad_read,
    131 	.d_write = nowrite,
    132 	.d_ioctl = noioctl,
    133 	.d_stop = nostop,
    134 	.d_tty = notty,
    135 	.d_poll = nopoll,
    136 	.d_mmap = nommap,
    137 	.d_kqfilter = nokqfilter,
    138 	.d_flag = D_OTHER,
    139 };
    140 
    141 CFATTACH_DECL2(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
    142     NULL, NULL, pad_childdet);
    143 
    144 void
    145 padattach(int n)
    146 {
    147 	int i, err;
    148 	struct cfdata *cf;
    149 
    150 #ifdef DEBUG
    151 	printf("pad: requested %d units\n", n);
    152 #endif
    153 
    154 	err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    155 	if (err) {
    156 		aprint_error("%s: couldn't register cfattach: %d\n",
    157 		    pad_cd.cd_name, err);
    158 		config_cfdriver_detach(&pad_cd);
    159 		return;
    160 	}
    161 
    162 	for (i = 0; i < n; i++) {
    163 		cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    164 		if (cf == NULL) {
    165 			aprint_error("%s: couldn't allocate cfdata\n",
    166 			    pad_cd.cd_name);
    167 			continue;
    168 		}
    169 		cf->cf_name = pad_cd.cd_name;
    170 		cf->cf_atname = pad_cd.cd_name;
    171 		cf->cf_unit = i;
    172 		cf->cf_fstate = FSTATE_STAR;
    173 
    174 		(void)config_attach_pseudo(cf);
    175 	}
    176 
    177 	return;
    178 }
    179 
    180 static int
    181 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
    182 {
    183 	int l;
    184 
    185 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
    186 		return ENOBUFS;
    187 
    188 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    189 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    190 	else {
    191 		l = PAD_BUFSIZE - sc->sc_wpos;
    192 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    193 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    194 	}
    195 
    196 	sc->sc_wpos += blksize;
    197 	if (sc->sc_wpos > PAD_BUFSIZE)
    198 		sc->sc_wpos -= PAD_BUFSIZE;
    199 
    200 	sc->sc_buflen += blksize;
    201 
    202 	return 0;
    203 }
    204 
    205 static int
    206 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
    207 {
    208 	int l;
    209 
    210 	KASSERT(pb != NULL);
    211 
    212 	if (sc->sc_buflen < blksize)
    213 		return ERESTART;
    214 
    215 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    216 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    217 		pb->pb_len = blksize;
    218 		sc->sc_rpos += blksize;
    219 	} else {
    220 		l = PAD_BUFSIZE - sc->sc_rpos;
    221 		pb->pb_len = l;
    222 		sc->sc_rpos = 0;
    223 	}
    224 	sc->sc_buflen -= pb->pb_len;
    225 
    226 	return 0;
    227 }
    228 
    229 static int
    230 pad_match(device_t parent, struct cfdata *data, void *opaque)
    231 {
    232 	return 1;
    233 }
    234 
    235 static void
    236 pad_childdet(device_t self, device_t child)
    237 {
    238 	pad_softc_t *sc = device_private(self);
    239 
    240 	KASSERT(sc->sc_audiodev->dev == child);
    241 	sc->sc_audiodev = NULL;
    242 }
    243 
    244 static void
    245 pad_attach(device_t parent, device_t self, void *opaque)
    246 {
    247 	pad_softc_t *sc = device_private(self);
    248 
    249 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    250 
    251 	sc->sc_open = 0;
    252 	if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
    253 	    &sc->sc_encodings) != 0) {
    254 		aprint_error_dev(self, "couldn't create encodings\n");
    255 		return;
    256 	}
    257 
    258 	cv_init(&sc->sc_condvar, device_xname(self));
    259 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_SCHED);
    260 
    261 	sc->sc_swvol = 255;
    262 	sc->sc_buflen = 0;
    263 	sc->sc_rpos = sc->sc_wpos = 0;
    264 	sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
    265 
    266 	if (!pmf_device_register(self, NULL, NULL))
    267 		aprint_error_dev(self, "couldn't establish power handler\n");
    268 
    269 	return;
    270 }
    271 
    272 static int
    273 pad_detach(device_t self, int flags)
    274 {
    275 	pad_softc_t *sc = device_private(self);
    276 	int cmaj, mn, rc;
    277 
    278 	cmaj = cdevsw_lookup_major(&pad_cdevsw);
    279 	mn = device_unit(self);
    280 	vdevgone(cmaj, mn, mn, VCHR);
    281 
    282 	if ((rc = config_detach_children(self, flags)) != 0)
    283 		return rc;
    284 
    285 	pmf_device_deregister(self);
    286 
    287 	mutex_destroy(&sc->sc_mutex);
    288 	cv_destroy(&sc->sc_condvar);
    289 
    290 	auconv_delete_encodings(sc->sc_encodings);
    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 
    300 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    301 	if (sc == NULL)
    302 		return ENODEV;
    303 
    304 	if (sc->sc_open++) {
    305 		sc->sc_open--;
    306 		return EBUSY;
    307 	}
    308 
    309 	return 0;
    310 }
    311 
    312 int
    313 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
    314 {
    315 	pad_softc_t *sc;
    316 
    317 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    318 	if (sc == NULL)
    319 		return ENODEV;
    320 
    321 	KASSERT(sc->sc_open > 0);
    322 	sc->sc_open--;
    323 
    324 	return 0;
    325 }
    326 
    327 int
    328 pad_read(dev_t dev, struct uio *uio, int flags)
    329 {
    330 	pad_softc_t *sc;
    331 	pad_block_t pb;
    332 	void (*intr)(void *);
    333 	void *intrarg;
    334 	int err;
    335 
    336 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    337 	if (sc == NULL)
    338 		return ENODEV;
    339 
    340 	err = 0;
    341 
    342 	intr = sc->sc_intr;
    343 	intrarg = sc->sc_intrarg;
    344 
    345 	while (uio->uio_resid > 0 && !err) {
    346 		err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
    347 		if (!err)
    348 			err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    349 		else {
    350 			if (intr) {
    351 				(*intr)(intrarg);
    352 				intr = sc->sc_intr;
    353 				intrarg = sc->sc_intrarg;
    354 				err = 0;
    355 				continue;
    356 			}
    357 
    358 			mutex_enter(&sc->sc_mutex);
    359 			err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
    360 			    hz/100);
    361 			if (err != 0 && err != EWOULDBLOCK) {
    362 				mutex_exit(&sc->sc_mutex);
    363 				aprint_error_dev(&sc->sc_dev,
    364 				    "cv_timedwait_sig returned %d\n", err);
    365 				return EINTR;
    366 			}
    367 			intr = sc->sc_intr;
    368 			intrarg = sc->sc_intrarg;
    369 			mutex_exit(&sc->sc_mutex);
    370 			err = 0;
    371 		}
    372 	}
    373 
    374 	if (intr)
    375 		(*intr)(intrarg);
    376 
    377 	return err;
    378 }
    379 
    380 static int
    381 pad_query_encoding(void *opaque, struct audio_encoding *ae)
    382 {
    383 	pad_softc_t *sc;
    384 
    385 	sc = (pad_softc_t *)opaque;
    386 
    387 	return auconv_query_encoding(sc->sc_encodings, ae);
    388 }
    389 
    390 static int
    391 pad_set_params(void *opaque, int setmode, int usemode,
    392     audio_params_t *play, audio_params_t *rec,
    393     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    394 {
    395 	pad_softc_t *sc;
    396 
    397 	sc = (pad_softc_t *)opaque;
    398 
    399 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
    400 	    play, true, pfil) < 0)
    401 		return EINVAL;
    402 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
    403 	    rec, true, rfil) < 0)
    404 		return EINVAL;
    405 
    406 	if (pfil->req_size > 0)
    407 		play = &pfil->filters[0].param;
    408 	switch (play->encoding) {
    409 	case AUDIO_ENCODING_SLINEAR_LE:
    410 		if (play->precision == 16 && play->validbits == 16)
    411 			pfil->prepend(pfil, pad_vol_slinear16_le, play);
    412 		break;
    413 	case AUDIO_ENCODING_SLINEAR_BE:
    414 		if (play->precision == 16 && play->validbits == 16)
    415 			pfil->prepend(pfil, pad_vol_slinear16_be, play);
    416 		break;
    417 	default:
    418 		break;
    419 	}
    420 
    421 	return 0;
    422 }
    423 
    424 static int
    425 pad_start_output(void *opaque, void *block, int blksize,
    426     void (*intr)(void *), void *intrarg)
    427 {
    428 	pad_softc_t *sc;
    429 	int err;
    430 
    431 	sc = (pad_softc_t *)opaque;
    432 
    433 	mutex_enter(&sc->sc_mutex);
    434 
    435 	sc->sc_intr = intr;
    436 	sc->sc_intrarg = intrarg;
    437 	sc->sc_blksize = blksize;
    438 
    439 	err = pad_add_block(sc, block, blksize);
    440 
    441 	cv_signal(&sc->sc_condvar);
    442 
    443 	mutex_exit(&sc->sc_mutex);
    444 
    445 	return err;
    446 }
    447 
    448 static int
    449 pad_start_input(void *opaque, void *block, int blksize,
    450     void (*intr)(void *), void *intrarg)
    451 {
    452 	return EINVAL;
    453 }
    454 
    455 static int
    456 pad_halt_output(void *opaque)
    457 {
    458 	pad_softc_t *sc;
    459 
    460 	sc = (pad_softc_t *)opaque;
    461 	sc->sc_intr = NULL;
    462 	sc->sc_intrarg = NULL;
    463 	sc->sc_buflen = 0;
    464 	sc->sc_rpos = sc->sc_wpos = 0;
    465 
    466 	return 0;
    467 }
    468 
    469 static int
    470 pad_halt_input(void *opaque)
    471 {
    472 	return 0;
    473 }
    474 
    475 static int
    476 pad_getdev(void *opaque, struct audio_device *ret)
    477 {
    478 
    479 	*ret = pad_device;
    480 
    481 	return 0;
    482 }
    483 
    484 static int
    485 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    486 {
    487 	pad_softc_t *sc;
    488 
    489 	sc = (pad_softc_t *)opaque;
    490 
    491 	switch (mc->dev) {
    492 	case PAD_OUTPUT_MASTER_VOLUME:
    493 	case PAD_INPUT_DAC_VOLUME:
    494 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    495 		return 0;
    496 	}
    497 
    498 	return ENXIO;
    499 }
    500 
    501 static int
    502 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    503 {
    504 	pad_softc_t *sc;
    505 
    506 	sc = (pad_softc_t *)opaque;
    507 
    508 	switch (mc->dev) {
    509 	case PAD_OUTPUT_MASTER_VOLUME:
    510 	case PAD_INPUT_DAC_VOLUME:
    511 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    512 		return 0;
    513 	}
    514 
    515 	return ENXIO;
    516 }
    517 
    518 static int
    519 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    520 {
    521 	pad_softc_t *sc;
    522 
    523 	sc = (pad_softc_t *)opaque;
    524 
    525 	switch (di->index) {
    526 	case PAD_OUTPUT_CLASS:
    527 		di->mixer_class = PAD_OUTPUT_CLASS;
    528 		strcpy(di->label.name, AudioCoutputs);
    529 		di->type = AUDIO_MIXER_CLASS;
    530 		di->next = di->prev = AUDIO_MIXER_LAST;
    531 		return 0;
    532 	case PAD_INPUT_CLASS:
    533 		di->mixer_class = PAD_INPUT_CLASS;
    534 		strcpy(di->label.name, AudioCinputs);
    535 		di->type = AUDIO_MIXER_CLASS;
    536 		di->next = di->prev = AUDIO_MIXER_LAST;
    537 		return 0;
    538 	case PAD_OUTPUT_MASTER_VOLUME:
    539 		di->mixer_class = PAD_OUTPUT_CLASS;
    540 		strcpy(di->label.name, AudioNmaster);
    541 		di->type = AUDIO_MIXER_VALUE;
    542 		di->next = di->prev = AUDIO_MIXER_LAST;
    543 		di->un.v.num_channels = 1;
    544 		strcpy(di->un.v.units.name, AudioNvolume);
    545 		return 0;
    546 	case PAD_INPUT_DAC_VOLUME:
    547 		di->mixer_class = PAD_INPUT_CLASS;
    548 		strcpy(di->label.name, AudioNdac);
    549 		di->type = AUDIO_MIXER_VALUE;
    550 		di->next = di->prev = AUDIO_MIXER_LAST;
    551 		di->un.v.num_channels = 1;
    552 		strcpy(di->un.v.units.name, AudioNvolume);
    553 		return 0;
    554 	}
    555 
    556 	return ENXIO;
    557 }
    558 
    559 static int
    560 pad_get_props(void *opaque)
    561 {
    562 	return 0;
    563 }
    564 
    565 static int
    566 pad_round_blocksize(void *opaque, int blksize, int mode,
    567     const audio_params_t *p)
    568 {
    569 	return PAD_BLKSIZE;
    570 }
    571