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