Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.12.4.3
      1  1.12.4.3     rmind /* $NetBSD: pad.c,v 1.12.4.3 2011/03/05 20:53:33 rmind Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5       1.1  jmcneill  * All rights reserved.
      6       1.1  jmcneill  *
      7       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8       1.1  jmcneill  * modification, are permitted provided that the following conditions
      9       1.1  jmcneill  * are met:
     10       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15       1.1  jmcneill  *
     16       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  jmcneill  */
     28       1.1  jmcneill 
     29       1.1  jmcneill #include <sys/cdefs.h>
     30  1.12.4.3     rmind __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.12.4.3 2011/03/05 20:53:33 rmind Exp $");
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/types.h>
     33       1.1  jmcneill #include <sys/param.h>
     34       1.1  jmcneill #include <sys/conf.h>
     35       1.1  jmcneill #include <sys/buf.h>
     36       1.1  jmcneill #include <sys/kmem.h>
     37       1.1  jmcneill #include <sys/kernel.h>
     38       1.1  jmcneill #include <sys/device.h>
     39       1.1  jmcneill #include <sys/proc.h>
     40       1.1  jmcneill #include <sys/condvar.h>
     41       1.1  jmcneill #include <sys/select.h>
     42       1.1  jmcneill #include <sys/audioio.h>
     43       1.5    dyoung #include <sys/vnode.h>
     44  1.12.4.1     rmind #include <sys/module.h>
     45       1.1  jmcneill 
     46       1.1  jmcneill #include <dev/audio_if.h>
     47       1.1  jmcneill #include <dev/audiovar.h>
     48       1.1  jmcneill #include <dev/auconv.h>
     49       1.1  jmcneill 
     50       1.2  jmcneill #include <dev/pad/padvar.h>
     51       1.2  jmcneill #include <dev/pad/padvol.h>
     52       1.2  jmcneill 
     53       1.1  jmcneill #define PADUNIT(x)	minor(x)
     54       1.1  jmcneill 
     55       1.1  jmcneill extern struct cfdriver pad_cd;
     56       1.1  jmcneill 
     57       1.1  jmcneill typedef struct pad_block {
     58       1.1  jmcneill 	uint8_t		*pb_ptr;
     59       1.1  jmcneill 	int		pb_len;
     60       1.1  jmcneill } pad_block_t;
     61       1.1  jmcneill 
     62       1.2  jmcneill enum {
     63       1.2  jmcneill 	PAD_OUTPUT_CLASS,
     64       1.2  jmcneill 	PAD_INPUT_CLASS,
     65       1.2  jmcneill 	PAD_OUTPUT_MASTER_VOLUME,
     66       1.2  jmcneill 	PAD_INPUT_DAC_VOLUME,
     67       1.2  jmcneill 	PAD_ENUM_LAST,
     68       1.2  jmcneill };
     69       1.1  jmcneill 
     70      1.10  christos static int	pad_match(device_t, cfdata_t, void *);
     71       1.5    dyoung static void	pad_attach(device_t, device_t, void *);
     72       1.5    dyoung static int	pad_detach(device_t, int);
     73       1.5    dyoung static void	pad_childdet(device_t, device_t);
     74       1.1  jmcneill 
     75       1.1  jmcneill static int	pad_query_encoding(void *, struct audio_encoding *);
     76       1.1  jmcneill static int	pad_set_params(void *, int, int,
     77       1.1  jmcneill 				audio_params_t *, audio_params_t *,
     78       1.1  jmcneill 				stream_filter_list_t *, stream_filter_list_t *);
     79       1.1  jmcneill static int	pad_start_output(void *, void *, int,
     80       1.1  jmcneill 				    void (*)(void *), void *);
     81       1.1  jmcneill static int	pad_start_input(void *, void *, int,
     82       1.1  jmcneill 				   void (*)(void *), void *);
     83       1.1  jmcneill static int	pad_halt_output(void *);
     84       1.1  jmcneill static int	pad_halt_input(void *);
     85       1.1  jmcneill static int	pad_getdev(void *, struct audio_device *);
     86       1.1  jmcneill static int	pad_set_port(void *, mixer_ctrl_t *);
     87       1.1  jmcneill static int	pad_get_port(void *, mixer_ctrl_t *);
     88       1.1  jmcneill static int	pad_query_devinfo(void *, mixer_devinfo_t *);
     89       1.1  jmcneill static int	pad_get_props(void *);
     90       1.1  jmcneill static int	pad_round_blocksize(void *, int, int, const audio_params_t *);
     91       1.1  jmcneill 
     92       1.1  jmcneill static const struct audio_hw_if pad_hw_if = {
     93       1.1  jmcneill 	.query_encoding = pad_query_encoding,
     94       1.1  jmcneill 	.set_params = pad_set_params,
     95       1.1  jmcneill 	.start_output = pad_start_output,
     96       1.1  jmcneill 	.start_input = pad_start_input,
     97       1.1  jmcneill 	.halt_output = pad_halt_output,
     98       1.1  jmcneill 	.halt_input = pad_halt_input,
     99       1.1  jmcneill 	.getdev = pad_getdev,
    100       1.1  jmcneill 	.set_port = pad_set_port,
    101       1.1  jmcneill 	.get_port = pad_get_port,
    102       1.1  jmcneill 	.query_devinfo = pad_query_devinfo,
    103       1.1  jmcneill 	.get_props = pad_get_props,
    104       1.1  jmcneill 	.round_blocksize = pad_round_blocksize,
    105       1.1  jmcneill };
    106       1.1  jmcneill 
    107       1.1  jmcneill #define PAD_NFORMATS	1
    108       1.1  jmcneill static const struct audio_format pad_formats[PAD_NFORMATS] = {
    109       1.1  jmcneill 	{ NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    110       1.1  jmcneill 	  2, AUFMT_STEREO, 1, { 44100 } },
    111       1.1  jmcneill };
    112       1.1  jmcneill 
    113       1.1  jmcneill extern void	padattach(int);
    114       1.1  jmcneill 
    115       1.1  jmcneill static int		pad_add_block(pad_softc_t *, uint8_t *, int);
    116       1.1  jmcneill static int		pad_get_block(pad_softc_t *, pad_block_t *, int);
    117       1.1  jmcneill 
    118       1.1  jmcneill dev_type_open(pad_open);
    119       1.1  jmcneill dev_type_close(pad_close);
    120       1.1  jmcneill dev_type_read(pad_read);
    121       1.1  jmcneill 
    122       1.1  jmcneill const struct cdevsw pad_cdevsw = {
    123       1.1  jmcneill 	.d_open = pad_open,
    124       1.1  jmcneill 	.d_close = pad_close,
    125       1.1  jmcneill 	.d_read = pad_read,
    126       1.1  jmcneill 	.d_write = nowrite,
    127       1.1  jmcneill 	.d_ioctl = noioctl,
    128       1.1  jmcneill 	.d_stop = nostop,
    129       1.1  jmcneill 	.d_tty = notty,
    130       1.1  jmcneill 	.d_poll = nopoll,
    131       1.1  jmcneill 	.d_mmap = nommap,
    132       1.1  jmcneill 	.d_kqfilter = nokqfilter,
    133       1.1  jmcneill 	.d_flag = D_OTHER,
    134       1.1  jmcneill };
    135       1.1  jmcneill 
    136      1.10  christos CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
    137       1.5    dyoung     NULL, NULL, pad_childdet);
    138       1.1  jmcneill 
    139       1.1  jmcneill void
    140       1.1  jmcneill padattach(int n)
    141       1.1  jmcneill {
    142       1.1  jmcneill 	int i, err;
    143      1.10  christos 	cfdata_t cf;
    144       1.1  jmcneill 
    145      1.11        ad 	aprint_debug("pad: requested %d units\n", n);
    146       1.1  jmcneill 
    147       1.1  jmcneill 	err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    148       1.1  jmcneill 	if (err) {
    149       1.1  jmcneill 		aprint_error("%s: couldn't register cfattach: %d\n",
    150       1.1  jmcneill 		    pad_cd.cd_name, err);
    151       1.1  jmcneill 		config_cfdriver_detach(&pad_cd);
    152       1.1  jmcneill 		return;
    153       1.1  jmcneill 	}
    154       1.1  jmcneill 
    155       1.1  jmcneill 	for (i = 0; i < n; i++) {
    156       1.1  jmcneill 		cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    157       1.1  jmcneill 		if (cf == NULL) {
    158       1.1  jmcneill 			aprint_error("%s: couldn't allocate cfdata\n",
    159       1.1  jmcneill 			    pad_cd.cd_name);
    160       1.1  jmcneill 			continue;
    161       1.1  jmcneill 		}
    162       1.1  jmcneill 		cf->cf_name = pad_cd.cd_name;
    163       1.1  jmcneill 		cf->cf_atname = pad_cd.cd_name;
    164       1.1  jmcneill 		cf->cf_unit = i;
    165       1.1  jmcneill 		cf->cf_fstate = FSTATE_STAR;
    166       1.1  jmcneill 
    167       1.1  jmcneill 		(void)config_attach_pseudo(cf);
    168       1.1  jmcneill 	}
    169       1.1  jmcneill 
    170       1.1  jmcneill 	return;
    171       1.1  jmcneill }
    172       1.1  jmcneill 
    173       1.1  jmcneill static int
    174       1.1  jmcneill pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
    175       1.1  jmcneill {
    176       1.1  jmcneill 	int l;
    177       1.1  jmcneill 
    178  1.12.4.3     rmind 	if (sc->sc_open == 0)
    179  1.12.4.3     rmind 		return EIO;
    180  1.12.4.3     rmind 
    181       1.1  jmcneill 	if (sc->sc_buflen + blksize > PAD_BUFSIZE)
    182       1.1  jmcneill 		return ENOBUFS;
    183       1.1  jmcneill 
    184       1.1  jmcneill 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
    185       1.1  jmcneill 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
    186       1.1  jmcneill 	else {
    187       1.1  jmcneill 		l = PAD_BUFSIZE - sc->sc_wpos;
    188       1.1  jmcneill 		memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
    189       1.1  jmcneill 		memcpy(sc->sc_audiobuf, blk + l, blksize - l);
    190       1.1  jmcneill 	}
    191       1.1  jmcneill 
    192       1.1  jmcneill 	sc->sc_wpos += blksize;
    193       1.1  jmcneill 	if (sc->sc_wpos > PAD_BUFSIZE)
    194       1.1  jmcneill 		sc->sc_wpos -= PAD_BUFSIZE;
    195       1.1  jmcneill 
    196       1.1  jmcneill 	sc->sc_buflen += blksize;
    197       1.1  jmcneill 
    198       1.1  jmcneill 	return 0;
    199       1.1  jmcneill }
    200       1.1  jmcneill 
    201       1.1  jmcneill static int
    202       1.1  jmcneill pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
    203       1.1  jmcneill {
    204       1.1  jmcneill 	int l;
    205       1.1  jmcneill 
    206       1.1  jmcneill 	KASSERT(pb != NULL);
    207       1.1  jmcneill 
    208       1.1  jmcneill 	if (sc->sc_buflen < blksize)
    209       1.1  jmcneill 		return ERESTART;
    210       1.1  jmcneill 
    211       1.1  jmcneill 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    212       1.1  jmcneill 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    213       1.1  jmcneill 		pb->pb_len = blksize;
    214       1.1  jmcneill 		sc->sc_rpos += blksize;
    215       1.1  jmcneill 	} else {
    216       1.1  jmcneill 		l = PAD_BUFSIZE - sc->sc_rpos;
    217       1.1  jmcneill 		pb->pb_len = l;
    218       1.1  jmcneill 		sc->sc_rpos = 0;
    219       1.1  jmcneill 	}
    220       1.1  jmcneill 	sc->sc_buflen -= pb->pb_len;
    221       1.1  jmcneill 
    222       1.1  jmcneill 	return 0;
    223       1.1  jmcneill }
    224       1.1  jmcneill 
    225       1.1  jmcneill static int
    226      1.10  christos pad_match(device_t parent, cfdata_t data, void *opaque)
    227       1.1  jmcneill {
    228       1.1  jmcneill 	return 1;
    229       1.1  jmcneill }
    230       1.1  jmcneill 
    231       1.1  jmcneill static void
    232       1.5    dyoung pad_childdet(device_t self, device_t child)
    233       1.1  jmcneill {
    234       1.5    dyoung 	pad_softc_t *sc = device_private(self);
    235       1.5    dyoung 
    236       1.5    dyoung 	sc->sc_audiodev = NULL;
    237       1.5    dyoung }
    238       1.1  jmcneill 
    239       1.5    dyoung static void
    240       1.5    dyoung pad_attach(device_t parent, device_t self, void *opaque)
    241       1.5    dyoung {
    242       1.5    dyoung 	pad_softc_t *sc = device_private(self);
    243       1.1  jmcneill 
    244       1.1  jmcneill 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    245       1.1  jmcneill 
    246      1.10  christos 	sc->sc_dev = self;
    247       1.1  jmcneill 	sc->sc_open = 0;
    248       1.1  jmcneill 	if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
    249       1.1  jmcneill 	    &sc->sc_encodings) != 0) {
    250       1.1  jmcneill 		aprint_error_dev(self, "couldn't create encodings\n");
    251       1.1  jmcneill 		return;
    252       1.1  jmcneill 	}
    253       1.1  jmcneill 
    254       1.1  jmcneill 	cv_init(&sc->sc_condvar, device_xname(self));
    255       1.3        ad 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_SCHED);
    256       1.1  jmcneill 
    257       1.2  jmcneill 	sc->sc_swvol = 255;
    258       1.1  jmcneill 	sc->sc_buflen = 0;
    259       1.1  jmcneill 	sc->sc_rpos = sc->sc_wpos = 0;
    260      1.10  christos 	sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
    261       1.1  jmcneill 
    262       1.4  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    263       1.4  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    264       1.4  jmcneill 
    265       1.1  jmcneill 	return;
    266       1.1  jmcneill }
    267       1.1  jmcneill 
    268       1.5    dyoung static int
    269       1.5    dyoung pad_detach(device_t self, int flags)
    270       1.5    dyoung {
    271       1.5    dyoung 	pad_softc_t *sc = device_private(self);
    272       1.5    dyoung 	int cmaj, mn, rc;
    273       1.5    dyoung 
    274       1.5    dyoung 	cmaj = cdevsw_lookup_major(&pad_cdevsw);
    275       1.5    dyoung 	mn = device_unit(self);
    276       1.5    dyoung 	vdevgone(cmaj, mn, mn, VCHR);
    277       1.5    dyoung 
    278       1.5    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    279       1.5    dyoung 		return rc;
    280       1.5    dyoung 
    281       1.5    dyoung 	pmf_device_deregister(self);
    282       1.5    dyoung 
    283       1.5    dyoung 	mutex_destroy(&sc->sc_mutex);
    284       1.5    dyoung 	cv_destroy(&sc->sc_condvar);
    285       1.5    dyoung 
    286       1.5    dyoung 	auconv_delete_encodings(sc->sc_encodings);
    287       1.5    dyoung 
    288       1.5    dyoung 	return 0;
    289       1.5    dyoung }
    290       1.5    dyoung 
    291       1.1  jmcneill int
    292       1.1  jmcneill pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    293       1.1  jmcneill {
    294       1.1  jmcneill 	pad_softc_t *sc;
    295       1.1  jmcneill 
    296       1.8   tsutsui 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    297       1.1  jmcneill 	if (sc == NULL)
    298      1.12    dyoung 		return ENXIO;
    299       1.1  jmcneill 
    300       1.1  jmcneill 	if (sc->sc_open++) {
    301       1.1  jmcneill 		sc->sc_open--;
    302       1.1  jmcneill 		return EBUSY;
    303       1.1  jmcneill 	}
    304       1.1  jmcneill 
    305       1.1  jmcneill 	return 0;
    306       1.1  jmcneill }
    307       1.1  jmcneill 
    308       1.1  jmcneill int
    309       1.1  jmcneill pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
    310       1.1  jmcneill {
    311       1.1  jmcneill 	pad_softc_t *sc;
    312       1.1  jmcneill 
    313       1.8   tsutsui 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    314       1.1  jmcneill 	if (sc == NULL)
    315      1.12    dyoung 		return ENXIO;
    316       1.1  jmcneill 
    317       1.1  jmcneill 	KASSERT(sc->sc_open > 0);
    318       1.1  jmcneill 	sc->sc_open--;
    319       1.1  jmcneill 
    320       1.1  jmcneill 	return 0;
    321       1.1  jmcneill }
    322       1.1  jmcneill 
    323       1.1  jmcneill int
    324       1.1  jmcneill pad_read(dev_t dev, struct uio *uio, int flags)
    325       1.1  jmcneill {
    326       1.1  jmcneill 	pad_softc_t *sc;
    327       1.1  jmcneill 	pad_block_t pb;
    328       1.1  jmcneill 	void (*intr)(void *);
    329       1.1  jmcneill 	void *intrarg;
    330       1.1  jmcneill 	int err;
    331       1.1  jmcneill 
    332       1.8   tsutsui 	sc = device_lookup_private(&pad_cd, PADUNIT(dev));
    333       1.1  jmcneill 	if (sc == NULL)
    334      1.12    dyoung 		return ENXIO;
    335       1.1  jmcneill 
    336       1.1  jmcneill 	err = 0;
    337       1.1  jmcneill 
    338       1.1  jmcneill 	intr = sc->sc_intr;
    339       1.1  jmcneill 	intrarg = sc->sc_intrarg;
    340       1.1  jmcneill 
    341       1.1  jmcneill 	while (uio->uio_resid > 0 && !err) {
    342       1.1  jmcneill 		err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
    343       1.1  jmcneill 		if (!err)
    344       1.1  jmcneill 			err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    345       1.1  jmcneill 		else {
    346       1.1  jmcneill 			if (intr) {
    347       1.1  jmcneill 				(*intr)(intrarg);
    348       1.1  jmcneill 				intr = sc->sc_intr;
    349       1.1  jmcneill 				intrarg = sc->sc_intrarg;
    350       1.1  jmcneill 				err = 0;
    351       1.1  jmcneill 				continue;
    352       1.1  jmcneill 			}
    353       1.1  jmcneill 
    354       1.1  jmcneill 			mutex_enter(&sc->sc_mutex);
    355       1.1  jmcneill 			err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
    356       1.1  jmcneill 			    hz/100);
    357       1.1  jmcneill 			if (err != 0 && err != EWOULDBLOCK) {
    358       1.1  jmcneill 				mutex_exit(&sc->sc_mutex);
    359  1.12.4.2     rmind 				if (err != ERESTART)
    360  1.12.4.2     rmind 					aprint_error_dev(sc->sc_dev,
    361  1.12.4.2     rmind 					    "cv_timedwait_sig returned %d\n",
    362  1.12.4.2     rmind 					    err);
    363       1.1  jmcneill 				return EINTR;
    364       1.1  jmcneill 			}
    365       1.1  jmcneill 			intr = sc->sc_intr;
    366       1.1  jmcneill 			intrarg = sc->sc_intrarg;
    367       1.1  jmcneill 			mutex_exit(&sc->sc_mutex);
    368       1.1  jmcneill 			err = 0;
    369       1.1  jmcneill 		}
    370       1.1  jmcneill 	}
    371       1.1  jmcneill 
    372       1.1  jmcneill 	if (intr)
    373       1.1  jmcneill 		(*intr)(intrarg);
    374       1.1  jmcneill 
    375       1.1  jmcneill 	return err;
    376       1.1  jmcneill }
    377       1.1  jmcneill 
    378       1.1  jmcneill static int
    379       1.1  jmcneill pad_query_encoding(void *opaque, struct audio_encoding *ae)
    380       1.1  jmcneill {
    381       1.1  jmcneill 	pad_softc_t *sc;
    382       1.1  jmcneill 
    383       1.1  jmcneill 	sc = (pad_softc_t *)opaque;
    384       1.1  jmcneill 
    385       1.1  jmcneill 	return auconv_query_encoding(sc->sc_encodings, ae);
    386       1.1  jmcneill }
    387       1.1  jmcneill 
    388       1.1  jmcneill static int
    389       1.1  jmcneill pad_set_params(void *opaque, int setmode, int usemode,
    390       1.1  jmcneill     audio_params_t *play, audio_params_t *rec,
    391       1.1  jmcneill     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    392       1.1  jmcneill {
    393       1.1  jmcneill 	pad_softc_t *sc;
    394       1.1  jmcneill 
    395       1.1  jmcneill 	sc = (pad_softc_t *)opaque;
    396       1.1  jmcneill 
    397       1.1  jmcneill 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
    398       1.1  jmcneill 	    play, true, pfil) < 0)
    399       1.1  jmcneill 		return EINVAL;
    400       1.1  jmcneill 	if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
    401       1.1  jmcneill 	    rec, true, rfil) < 0)
    402       1.1  jmcneill 		return EINVAL;
    403       1.1  jmcneill 
    404       1.2  jmcneill 	if (pfil->req_size > 0)
    405       1.2  jmcneill 		play = &pfil->filters[0].param;
    406       1.2  jmcneill 	switch (play->encoding) {
    407       1.2  jmcneill 	case AUDIO_ENCODING_SLINEAR_LE:
    408       1.2  jmcneill 		if (play->precision == 16 && play->validbits == 16)
    409       1.2  jmcneill 			pfil->prepend(pfil, pad_vol_slinear16_le, play);
    410       1.2  jmcneill 		break;
    411       1.2  jmcneill 	case AUDIO_ENCODING_SLINEAR_BE:
    412       1.2  jmcneill 		if (play->precision == 16 && play->validbits == 16)
    413       1.2  jmcneill 			pfil->prepend(pfil, pad_vol_slinear16_be, play);
    414       1.2  jmcneill 		break;
    415       1.2  jmcneill 	default:
    416       1.2  jmcneill 		break;
    417       1.2  jmcneill 	}
    418       1.2  jmcneill 
    419       1.1  jmcneill 	return 0;
    420       1.1  jmcneill }
    421       1.1  jmcneill 
    422       1.1  jmcneill static int
    423       1.1  jmcneill pad_start_output(void *opaque, void *block, int blksize,
    424       1.1  jmcneill     void (*intr)(void *), void *intrarg)
    425       1.1  jmcneill {
    426       1.1  jmcneill 	pad_softc_t *sc;
    427       1.1  jmcneill 	int err;
    428       1.1  jmcneill 
    429       1.1  jmcneill 	sc = (pad_softc_t *)opaque;
    430       1.1  jmcneill 
    431       1.1  jmcneill 	mutex_enter(&sc->sc_mutex);
    432       1.1  jmcneill 
    433       1.1  jmcneill 	sc->sc_intr = intr;
    434       1.1  jmcneill 	sc->sc_intrarg = intrarg;
    435       1.1  jmcneill 	sc->sc_blksize = blksize;
    436       1.1  jmcneill 
    437       1.1  jmcneill 	err = pad_add_block(sc, block, blksize);
    438       1.1  jmcneill 
    439       1.1  jmcneill 	cv_signal(&sc->sc_condvar);
    440       1.1  jmcneill 
    441       1.1  jmcneill 	mutex_exit(&sc->sc_mutex);
    442       1.1  jmcneill 
    443       1.1  jmcneill 	return err;
    444       1.1  jmcneill }
    445       1.1  jmcneill 
    446       1.1  jmcneill static int
    447       1.1  jmcneill pad_start_input(void *opaque, void *block, int blksize,
    448       1.1  jmcneill     void (*intr)(void *), void *intrarg)
    449       1.1  jmcneill {
    450       1.1  jmcneill 	return EINVAL;
    451       1.1  jmcneill }
    452       1.1  jmcneill 
    453       1.1  jmcneill static int
    454       1.1  jmcneill pad_halt_output(void *opaque)
    455       1.1  jmcneill {
    456       1.1  jmcneill 	pad_softc_t *sc;
    457       1.1  jmcneill 
    458       1.1  jmcneill 	sc = (pad_softc_t *)opaque;
    459       1.1  jmcneill 	sc->sc_intr = NULL;
    460       1.1  jmcneill 	sc->sc_intrarg = NULL;
    461       1.1  jmcneill 	sc->sc_buflen = 0;
    462       1.1  jmcneill 	sc->sc_rpos = sc->sc_wpos = 0;
    463       1.1  jmcneill 
    464       1.1  jmcneill 	return 0;
    465       1.1  jmcneill }
    466       1.1  jmcneill 
    467       1.1  jmcneill static int
    468       1.1  jmcneill pad_halt_input(void *opaque)
    469       1.1  jmcneill {
    470       1.1  jmcneill 	return 0;
    471       1.1  jmcneill }
    472       1.1  jmcneill 
    473       1.1  jmcneill static int
    474       1.1  jmcneill pad_getdev(void *opaque, struct audio_device *ret)
    475       1.1  jmcneill {
    476  1.12.4.3     rmind 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    477  1.12.4.3     rmind 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    478  1.12.4.3     rmind 	strlcpy(ret->config, "pad", sizeof(ret->config));
    479       1.1  jmcneill 
    480       1.1  jmcneill 	return 0;
    481       1.1  jmcneill }
    482       1.1  jmcneill 
    483       1.1  jmcneill static int
    484       1.1  jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
    485       1.1  jmcneill {
    486       1.2  jmcneill 	pad_softc_t *sc;
    487       1.2  jmcneill 
    488       1.2  jmcneill 	sc = (pad_softc_t *)opaque;
    489       1.2  jmcneill 
    490       1.2  jmcneill 	switch (mc->dev) {
    491       1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    492       1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    493       1.2  jmcneill 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    494       1.2  jmcneill 		return 0;
    495       1.2  jmcneill 	}
    496       1.2  jmcneill 
    497       1.1  jmcneill 	return ENXIO;
    498       1.1  jmcneill }
    499       1.1  jmcneill 
    500       1.1  jmcneill static int
    501       1.1  jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
    502       1.1  jmcneill {
    503       1.2  jmcneill 	pad_softc_t *sc;
    504       1.2  jmcneill 
    505       1.2  jmcneill 	sc = (pad_softc_t *)opaque;
    506       1.2  jmcneill 
    507       1.2  jmcneill 	switch (mc->dev) {
    508       1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    509       1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    510       1.2  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    511       1.2  jmcneill 		return 0;
    512       1.2  jmcneill 	}
    513       1.2  jmcneill 
    514       1.1  jmcneill 	return ENXIO;
    515       1.1  jmcneill }
    516       1.1  jmcneill 
    517       1.1  jmcneill static int
    518       1.1  jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    519       1.1  jmcneill {
    520       1.2  jmcneill 	pad_softc_t *sc;
    521       1.2  jmcneill 
    522       1.2  jmcneill 	sc = (pad_softc_t *)opaque;
    523       1.2  jmcneill 
    524       1.2  jmcneill 	switch (di->index) {
    525       1.2  jmcneill 	case PAD_OUTPUT_CLASS:
    526       1.2  jmcneill 		di->mixer_class = PAD_OUTPUT_CLASS;
    527       1.2  jmcneill 		strcpy(di->label.name, AudioCoutputs);
    528       1.2  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    529       1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    530       1.2  jmcneill 		return 0;
    531       1.2  jmcneill 	case PAD_INPUT_CLASS:
    532       1.2  jmcneill 		di->mixer_class = PAD_INPUT_CLASS;
    533       1.2  jmcneill 		strcpy(di->label.name, AudioCinputs);
    534       1.2  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    535       1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    536       1.2  jmcneill 		return 0;
    537       1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    538       1.2  jmcneill 		di->mixer_class = PAD_OUTPUT_CLASS;
    539       1.2  jmcneill 		strcpy(di->label.name, AudioNmaster);
    540       1.2  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    541       1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    542       1.2  jmcneill 		di->un.v.num_channels = 1;
    543       1.2  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    544       1.2  jmcneill 		return 0;
    545       1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    546       1.2  jmcneill 		di->mixer_class = PAD_INPUT_CLASS;
    547       1.2  jmcneill 		strcpy(di->label.name, AudioNdac);
    548       1.2  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    549       1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    550       1.2  jmcneill 		di->un.v.num_channels = 1;
    551       1.2  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    552       1.2  jmcneill 		return 0;
    553       1.2  jmcneill 	}
    554       1.2  jmcneill 
    555       1.1  jmcneill 	return ENXIO;
    556       1.1  jmcneill }
    557       1.1  jmcneill 
    558       1.1  jmcneill static int
    559       1.1  jmcneill pad_get_props(void *opaque)
    560       1.1  jmcneill {
    561       1.1  jmcneill 	return 0;
    562       1.1  jmcneill }
    563       1.1  jmcneill 
    564       1.1  jmcneill static int
    565       1.1  jmcneill pad_round_blocksize(void *opaque, int blksize, int mode,
    566       1.1  jmcneill     const audio_params_t *p)
    567       1.1  jmcneill {
    568       1.1  jmcneill 	return PAD_BLKSIZE;
    569       1.1  jmcneill }
    570  1.12.4.1     rmind 
    571  1.12.4.1     rmind #ifdef _MODULE
    572  1.12.4.1     rmind 
    573  1.12.4.1     rmind MODULE(MODULE_CLASS_DRIVER, pad, NULL);
    574  1.12.4.1     rmind 
    575  1.12.4.1     rmind static const struct cfiattrdata audiobuscf_iattrdata = {
    576  1.12.4.1     rmind 	"audiobus", 0, { { NULL, NULL, 0 }, }
    577  1.12.4.1     rmind };
    578  1.12.4.1     rmind static const struct cfiattrdata * const pad_attrs[] = {
    579  1.12.4.1     rmind 	&audiobuscf_iattrdata, NULL
    580  1.12.4.1     rmind };
    581  1.12.4.1     rmind 
    582  1.12.4.1     rmind CFDRIVER_DECL(pad, DV_DULL, pad_attrs);
    583  1.12.4.1     rmind extern struct cfattach pad_ca;
    584  1.12.4.1     rmind static int padloc[] = { -1, -1 };
    585  1.12.4.1     rmind 
    586  1.12.4.1     rmind static struct cfdata pad_cfdata[] = {
    587  1.12.4.1     rmind 	{
    588  1.12.4.1     rmind 		.cf_name = "pad",
    589  1.12.4.1     rmind 		.cf_atname = "pad",
    590  1.12.4.1     rmind 		.cf_unit = 0,
    591  1.12.4.1     rmind 		.cf_fstate = FSTATE_STAR,
    592  1.12.4.1     rmind 		.cf_loc = padloc,
    593  1.12.4.1     rmind 		.cf_flags = 0,
    594  1.12.4.1     rmind 		.cf_pspec = NULL,
    595  1.12.4.1     rmind 	},
    596  1.12.4.1     rmind 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    597  1.12.4.1     rmind };
    598  1.12.4.1     rmind 
    599  1.12.4.1     rmind static int
    600  1.12.4.1     rmind pad_modcmd(modcmd_t cmd, void *arg)
    601  1.12.4.1     rmind {
    602  1.12.4.1     rmind 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    603  1.12.4.1     rmind 	int error, s;
    604  1.12.4.1     rmind 
    605  1.12.4.1     rmind 	switch (cmd) {
    606  1.12.4.1     rmind 	case MODULE_CMD_INIT:
    607  1.12.4.1     rmind 		error = config_cfdriver_attach(&pad_cd);
    608  1.12.4.1     rmind 		if (error) {
    609  1.12.4.1     rmind 			return error;
    610  1.12.4.1     rmind 		}
    611  1.12.4.1     rmind 
    612  1.12.4.1     rmind 		error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    613  1.12.4.1     rmind 		if (error) {
    614  1.12.4.1     rmind 			config_cfdriver_detach(&pad_cd);
    615  1.12.4.1     rmind 			aprint_error("%s: unable to register cfattach\n",
    616  1.12.4.1     rmind 				pad_cd.cd_name);
    617  1.12.4.1     rmind 
    618  1.12.4.1     rmind 			return error;
    619  1.12.4.1     rmind 		}
    620  1.12.4.1     rmind 
    621  1.12.4.1     rmind 		s = splaudio();
    622  1.12.4.1     rmind 		error = config_cfdata_attach(pad_cfdata, 1);
    623  1.12.4.1     rmind 		splx(s);
    624  1.12.4.1     rmind 		if (error) {
    625  1.12.4.1     rmind 			config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    626  1.12.4.1     rmind 			config_cfdriver_detach(&pad_cd);
    627  1.12.4.1     rmind 			aprint_error("%s: unable to register cfdata\n",
    628  1.12.4.1     rmind 				pad_cd.cd_name);
    629  1.12.4.1     rmind 
    630  1.12.4.1     rmind 			return error;
    631  1.12.4.1     rmind 		}
    632  1.12.4.1     rmind 
    633  1.12.4.1     rmind 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor, &pad_cdevsw, &cmajor);
    634  1.12.4.1     rmind 		if (error) {
    635  1.12.4.1     rmind 			error = config_cfdata_detach(pad_cfdata);
    636  1.12.4.1     rmind 			if (error) {
    637  1.12.4.1     rmind 				return error;
    638  1.12.4.1     rmind 			}
    639  1.12.4.1     rmind 			config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    640  1.12.4.1     rmind 			config_cfdriver_detach(&pad_cd);
    641  1.12.4.1     rmind 			aprint_error("%s: unable to register devsw\n",
    642  1.12.4.1     rmind 				pad_cd.cd_name);
    643  1.12.4.1     rmind 
    644  1.12.4.1     rmind 			return error;
    645  1.12.4.1     rmind 		}
    646  1.12.4.1     rmind 
    647  1.12.4.1     rmind 		(void)config_attach_pseudo(pad_cfdata);
    648  1.12.4.1     rmind 
    649  1.12.4.1     rmind 		return 0;
    650  1.12.4.1     rmind 	case MODULE_CMD_FINI:
    651  1.12.4.1     rmind 		error = config_cfdata_detach(pad_cfdata);
    652  1.12.4.1     rmind 		if (error) {
    653  1.12.4.1     rmind 			return error;
    654  1.12.4.1     rmind 		}
    655  1.12.4.1     rmind 
    656  1.12.4.1     rmind 		config_cfattach_detach(pad_cd.cd_name, &pad_ca);
    657  1.12.4.1     rmind 		config_cfdriver_detach(&pad_cd);
    658  1.12.4.1     rmind 		devsw_detach(NULL, &pad_cdevsw);
    659  1.12.4.1     rmind 
    660  1.12.4.1     rmind 		return 0;
    661  1.12.4.1     rmind 	default:
    662  1.12.4.1     rmind 		return ENOTTY;
    663  1.12.4.1     rmind 	}
    664  1.12.4.1     rmind }
    665  1.12.4.1     rmind 
    666  1.12.4.1     rmind #endif
    667