Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.4
      1 /* $NetBSD: pad.c,v 1.4 2007/12/09 20:28:04 jmcneill 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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by Jared D. McNeill.
     18  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.4 2007/12/09 20:28:04 jmcneill Exp $");
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/conf.h>
     41 #include <sys/buf.h>
     42 #include <sys/kmem.h>
     43 #include <sys/kernel.h>
     44 #include <sys/device.h>
     45 #include <sys/proc.h>
     46 #include <sys/condvar.h>
     47 #include <sys/select.h>
     48 #include <sys/audioio.h>
     49 
     50 #include <dev/audio_if.h>
     51 #include <dev/audiovar.h>
     52 #include <dev/auconv.h>
     53 
     54 #include <dev/pad/padvar.h>
     55 #include <dev/pad/padvol.h>
     56 
     57 #define PADUNIT(x)	minor(x)
     58 
     59 extern struct cfdriver pad_cd;
     60 
     61 static struct audio_device pad_device = {
     62 	"Pseudo Audio",
     63 	"1.0",
     64 	"pad",
     65 };
     66 
     67 typedef struct pad_block {
     68 	uint8_t		*pb_ptr;
     69 	int		pb_len;
     70 } pad_block_t;
     71 
     72 enum {
     73 	PAD_OUTPUT_CLASS,
     74 	PAD_INPUT_CLASS,
     75 	PAD_OUTPUT_MASTER_VOLUME,
     76 	PAD_INPUT_DAC_VOLUME,
     77 	PAD_ENUM_LAST,
     78 };
     79 
     80 static int	pad_match(struct device *, struct cfdata *, void *);
     81 static void	pad_attach(struct device *, struct device *, void *);
     82 
     83 static int	pad_query_encoding(void *, struct audio_encoding *);
     84 static int	pad_set_params(void *, int, int,
     85 				audio_params_t *, audio_params_t *,
     86 				stream_filter_list_t *, stream_filter_list_t *);
     87 static int	pad_start_output(void *, void *, int,
     88 				    void (*)(void *), void *);
     89 static int	pad_start_input(void *, void *, int,
     90 				   void (*)(void *), void *);
     91 static int	pad_halt_output(void *);
     92 static int	pad_halt_input(void *);
     93 static int	pad_getdev(void *, struct audio_device *);
     94 static int	pad_set_port(void *, mixer_ctrl_t *);
     95 static int	pad_get_port(void *, mixer_ctrl_t *);
     96 static int	pad_query_devinfo(void *, mixer_devinfo_t *);
     97 static int	pad_get_props(void *);
     98 static int	pad_round_blocksize(void *, int, int, const audio_params_t *);
     99 
    100 static const struct audio_hw_if pad_hw_if = {
    101 	.query_encoding = pad_query_encoding,
    102 	.set_params = pad_set_params,
    103 	.start_output = pad_start_output,
    104 	.start_input = pad_start_input,
    105 	.halt_output = pad_halt_output,
    106 	.halt_input = pad_halt_input,
    107 	.getdev = pad_getdev,
    108 	.set_port = pad_set_port,
    109 	.get_port = pad_get_port,
    110 	.query_devinfo = pad_query_devinfo,
    111 	.get_props = pad_get_props,
    112 	.round_blocksize = pad_round_blocksize,
    113 };
    114 
    115 #define PAD_NFORMATS	1
    116 static const struct audio_format pad_formats[PAD_NFORMATS] = {
    117 	{ NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    118 	  2, AUFMT_STEREO, 1, { 44100 } },
    119 };
    120 
    121 extern void	padattach(int);
    122 
    123 static pad_softc_t *	pad_find_softc(dev_t);
    124 static int		pad_add_block(pad_softc_t *, uint8_t *, int);
    125 static int		pad_get_block(pad_softc_t *, pad_block_t *, int);
    126 
    127 dev_type_open(pad_open);
    128 dev_type_close(pad_close);
    129 dev_type_read(pad_read);
    130 
    131 const struct cdevsw pad_cdevsw = {
    132 	.d_open = pad_open,
    133 	.d_close = pad_close,
    134 	.d_read = pad_read,
    135 	.d_write = nowrite,
    136 	.d_ioctl = noioctl,
    137 	.d_stop = nostop,
    138 	.d_tty = notty,
    139 	.d_poll = nopoll,
    140 	.d_mmap = nommap,
    141 	.d_kqfilter = nokqfilter,
    142 	.d_flag = D_OTHER,
    143 };
    144 
    145 CFATTACH_DECL(pad, sizeof(pad_softc_t), pad_match, pad_attach, NULL, NULL);
    146 
    147 void
    148 padattach(int n)
    149 {
    150 	int i, err;
    151 	struct cfdata *cf;
    152 
    153 #ifdef DEBUG
    154 	printf("pad: requested %d units\n", n);
    155 #endif
    156 
    157 	err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    158 	if (err) {
    159 		aprint_error("%s: couldn't register cfattach: %d\n",
    160 		    pad_cd.cd_name, err);
    161 		config_cfdriver_detach(&pad_cd);
    162 		return;
    163 	}
    164 
    165 	for (i = 0; i < n; i++) {
    166 		cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    167 		if (cf == NULL) {
    168 			aprint_error("%s: couldn't allocate cfdata\n",
    169 			    pad_cd.cd_name);
    170 			continue;
    171 		}
    172 		cf->cf_name = pad_cd.cd_name;
    173 		cf->cf_atname = pad_cd.cd_name;
    174 		cf->cf_unit = i;
    175 		cf->cf_fstate = FSTATE_STAR;
    176 
    177 		(void)config_attach_pseudo(cf);
    178 	}
    179 
    180 	return;
    181 }
    182 
    183 static pad_softc_t *
    184 pad_find_softc(dev_t dev)
    185 {
    186 	int unit;
    187 
    188 	unit = PADUNIT(dev);
    189 	if (unit >= pad_cd.cd_ndevs)
    190 		return NULL;
    191 
    192 	return pad_cd.cd_devs[unit];
    193 }
    194 
    195 static int
    196 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
    197 {
    198 	int l;
    199 
    200 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
    201 		return ENOBUFS;
    202 
    203 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    204 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    205 	else {
    206 		l = PAD_BUFSIZE - sc->sc_wpos;
    207 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    208 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    209 	}
    210 
    211 	sc->sc_wpos += blksize;
    212 	if (sc->sc_wpos > PAD_BUFSIZE)
    213 		sc->sc_wpos -= PAD_BUFSIZE;
    214 
    215 	sc->sc_buflen += blksize;
    216 
    217 	return 0;
    218 }
    219 
    220 static int
    221 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
    222 {
    223 	int l;
    224 
    225 	KASSERT(pb != NULL);
    226 
    227 	if (sc->sc_buflen < blksize)
    228 		return ERESTART;
    229 
    230 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    231 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    232 		pb->pb_len = blksize;
    233 		sc->sc_rpos += blksize;
    234 	} else {
    235 		l = PAD_BUFSIZE - sc->sc_rpos;
    236 		pb->pb_len = l;
    237 		sc->sc_rpos = 0;
    238 	}
    239 	sc->sc_buflen -= pb->pb_len;
    240 
    241 	return 0;
    242 }
    243 
    244 static int
    245 pad_match(struct device *parent, struct cfdata *data, void *opaque)
    246 {
    247 	return 1;
    248 }
    249 
    250 static void
    251 pad_attach(struct device *parent, struct device *self, void *opaque)
    252 {
    253 	pad_softc_t *sc;
    254 
    255 	sc = (pad_softc_t *)self;
    256 
    257 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    258 
    259 	sc->sc_open = 0;
    260 	if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
    261 	    &sc->sc_encodings) != 0) {
    262 		aprint_error_dev(self, "couldn't create encodings\n");
    263 		return;
    264 	}
    265 
    266 	cv_init(&sc->sc_condvar, device_xname(self));
    267 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_SCHED);
    268 
    269 	sc->sc_swvol = 255;
    270 	sc->sc_buflen = 0;
    271 	sc->sc_rpos = sc->sc_wpos = 0;
    272 	sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
    273 
    274 	if (!pmf_device_register(self, NULL, NULL))
    275 		aprint_error_dev(self, "couldn't establish power handler\n");
    276 
    277 	return;
    278 }
    279 
    280 int
    281 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    282 {
    283 	pad_softc_t *sc;
    284 
    285 	sc = pad_find_softc(dev);
    286 	if (sc == NULL)
    287 		return ENODEV;
    288 
    289 	if (sc->sc_open++) {
    290 		sc->sc_open--;
    291 		return EBUSY;
    292 	}
    293 
    294 	return 0;
    295 }
    296 
    297 int
    298 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
    299 {
    300 	pad_softc_t *sc;
    301 
    302 	sc = pad_find_softc(dev);
    303 	if (sc == NULL)
    304 		return ENODEV;
    305 
    306 	KASSERT(sc->sc_open > 0);
    307 	sc->sc_open--;
    308 
    309 	return 0;
    310 }
    311 
    312 int
    313 pad_read(dev_t dev, struct uio *uio, int flags)
    314 {
    315 	pad_softc_t *sc;
    316 	pad_block_t pb;
    317 	void (*intr)(void *);
    318 	void *intrarg;
    319 	int err;
    320 
    321 	sc = pad_find_softc(dev);
    322 	if (sc == NULL)
    323 		return ENODEV;
    324 
    325 	err = 0;
    326 
    327 	intr = sc->sc_intr;
    328 	intrarg = sc->sc_intrarg;
    329 
    330 	while (uio->uio_resid > 0 && !err) {
    331 		err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
    332 		if (!err)
    333 			err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    334 		else {
    335 			if (intr) {
    336 				(*intr)(intrarg);
    337 				intr = sc->sc_intr;
    338 				intrarg = sc->sc_intrarg;
    339 				err = 0;
    340 				continue;
    341 			}
    342 
    343 			mutex_enter(&sc->sc_mutex);
    344 			err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
    345 			    hz/100);
    346 			if (err != 0 && err != EWOULDBLOCK) {
    347 				mutex_exit(&sc->sc_mutex);
    348 				aprint_error_dev(&sc->sc_dev,
    349 				    "cv_timedwait_sig returned %d\n", err);
    350 				return EINTR;
    351 			}
    352 			intr = sc->sc_intr;
    353 			intrarg = sc->sc_intrarg;
    354 			mutex_exit(&sc->sc_mutex);
    355 			err = 0;
    356 		}
    357 	}
    358 
    359 	if (intr)
    360 		(*intr)(intrarg);
    361 
    362 	return err;
    363 }
    364 
    365 static int
    366 pad_query_encoding(void *opaque, struct audio_encoding *ae)
    367 {
    368 	pad_softc_t *sc;
    369 
    370 	sc = (pad_softc_t *)opaque;
    371 
    372 	return auconv_query_encoding(sc->sc_encodings, ae);
    373 }
    374 
    375 static int
    376 pad_set_params(void *opaque, int setmode, int usemode,
    377     audio_params_t *play, audio_params_t *rec,
    378     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    379 {
    380 	pad_softc_t *sc;
    381 
    382 	sc = (pad_softc_t *)opaque;
    383 
    384 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
    385 	    play, true, pfil) < 0)
    386 		return EINVAL;
    387 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
    388 	    rec, true, rfil) < 0)
    389 		return EINVAL;
    390 
    391 	if (pfil->req_size > 0)
    392 		play = &pfil->filters[0].param;
    393 	switch (play->encoding) {
    394 	case AUDIO_ENCODING_SLINEAR_LE:
    395 		if (play->precision == 16 && play->validbits == 16)
    396 			pfil->prepend(pfil, pad_vol_slinear16_le, play);
    397 		break;
    398 	case AUDIO_ENCODING_SLINEAR_BE:
    399 		if (play->precision == 16 && play->validbits == 16)
    400 			pfil->prepend(pfil, pad_vol_slinear16_be, play);
    401 		break;
    402 	default:
    403 		break;
    404 	}
    405 
    406 	return 0;
    407 }
    408 
    409 static int
    410 pad_start_output(void *opaque, void *block, int blksize,
    411     void (*intr)(void *), void *intrarg)
    412 {
    413 	pad_softc_t *sc;
    414 	int err;
    415 
    416 	sc = (pad_softc_t *)opaque;
    417 
    418 	mutex_enter(&sc->sc_mutex);
    419 
    420 	sc->sc_intr = intr;
    421 	sc->sc_intrarg = intrarg;
    422 	sc->sc_blksize = blksize;
    423 
    424 	err = pad_add_block(sc, block, blksize);
    425 
    426 	cv_signal(&sc->sc_condvar);
    427 
    428 	mutex_exit(&sc->sc_mutex);
    429 
    430 	return err;
    431 }
    432 
    433 static int
    434 pad_start_input(void *opaque, void *block, int blksize,
    435     void (*intr)(void *), void *intrarg)
    436 {
    437 	return EINVAL;
    438 }
    439 
    440 static int
    441 pad_halt_output(void *opaque)
    442 {
    443 	pad_softc_t *sc;
    444 
    445 	sc = (pad_softc_t *)opaque;
    446 	sc->sc_intr = NULL;
    447 	sc->sc_intrarg = NULL;
    448 	sc->sc_buflen = 0;
    449 	sc->sc_rpos = sc->sc_wpos = 0;
    450 
    451 	return 0;
    452 }
    453 
    454 static int
    455 pad_halt_input(void *opaque)
    456 {
    457 	return 0;
    458 }
    459 
    460 static int
    461 pad_getdev(void *opaque, struct audio_device *ret)
    462 {
    463 
    464 	*ret = pad_device;
    465 
    466 	return 0;
    467 }
    468 
    469 static int
    470 pad_set_port(void *opaque, mixer_ctrl_t *mc)
    471 {
    472 	pad_softc_t *sc;
    473 
    474 	sc = (pad_softc_t *)opaque;
    475 
    476 	switch (mc->dev) {
    477 	case PAD_OUTPUT_MASTER_VOLUME:
    478 	case PAD_INPUT_DAC_VOLUME:
    479 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    480 		return 0;
    481 	}
    482 
    483 	return ENXIO;
    484 }
    485 
    486 static int
    487 pad_get_port(void *opaque, mixer_ctrl_t *mc)
    488 {
    489 	pad_softc_t *sc;
    490 
    491 	sc = (pad_softc_t *)opaque;
    492 
    493 	switch (mc->dev) {
    494 	case PAD_OUTPUT_MASTER_VOLUME:
    495 	case PAD_INPUT_DAC_VOLUME:
    496 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    497 		return 0;
    498 	}
    499 
    500 	return ENXIO;
    501 }
    502 
    503 static int
    504 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    505 {
    506 	pad_softc_t *sc;
    507 
    508 	sc = (pad_softc_t *)opaque;
    509 
    510 	switch (di->index) {
    511 	case PAD_OUTPUT_CLASS:
    512 		di->mixer_class = PAD_OUTPUT_CLASS;
    513 		strcpy(di->label.name, AudioCoutputs);
    514 		di->type = AUDIO_MIXER_CLASS;
    515 		di->next = di->prev = AUDIO_MIXER_LAST;
    516 		return 0;
    517 	case PAD_INPUT_CLASS:
    518 		di->mixer_class = PAD_INPUT_CLASS;
    519 		strcpy(di->label.name, AudioCinputs);
    520 		di->type = AUDIO_MIXER_CLASS;
    521 		di->next = di->prev = AUDIO_MIXER_LAST;
    522 		return 0;
    523 	case PAD_OUTPUT_MASTER_VOLUME:
    524 		di->mixer_class = PAD_OUTPUT_CLASS;
    525 		strcpy(di->label.name, AudioNmaster);
    526 		di->type = AUDIO_MIXER_VALUE;
    527 		di->next = di->prev = AUDIO_MIXER_LAST;
    528 		di->un.v.num_channels = 1;
    529 		strcpy(di->un.v.units.name, AudioNvolume);
    530 		return 0;
    531 	case PAD_INPUT_DAC_VOLUME:
    532 		di->mixer_class = PAD_INPUT_CLASS;
    533 		strcpy(di->label.name, AudioNdac);
    534 		di->type = AUDIO_MIXER_VALUE;
    535 		di->next = di->prev = AUDIO_MIXER_LAST;
    536 		di->un.v.num_channels = 1;
    537 		strcpy(di->un.v.units.name, AudioNvolume);
    538 		return 0;
    539 	}
    540 
    541 	return ENXIO;
    542 }
    543 
    544 static int
    545 pad_get_props(void *opaque)
    546 {
    547 	return 0;
    548 }
    549 
    550 static int
    551 pad_round_blocksize(void *opaque, int blksize, int mode,
    552     const audio_params_t *p)
    553 {
    554 	return PAD_BLKSIZE;
    555 }
    556