Home | History | Annotate | Line # | Download | only in pad
pad.c revision 1.79
      1  1.79   mlelstv /* $NetBSD: pad.c,v 1.79 2023/01/24 08:17:11 mlelstv 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.79   mlelstv __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.79 2023/01/24 08:17:11 mlelstv Exp $");
     31   1.1  jmcneill 
     32  1.70  riastrad #include <sys/param.h>
     33   1.1  jmcneill #include <sys/types.h>
     34  1.70  riastrad 
     35  1.70  riastrad #include <sys/audioio.h>
     36  1.70  riastrad #include <sys/buf.h>
     37  1.70  riastrad #include <sys/condvar.h>
     38   1.1  jmcneill #include <sys/conf.h>
     39  1.70  riastrad #include <sys/device.h>
     40  1.57  nakayama #include <sys/file.h>
     41  1.57  nakayama #include <sys/filedesc.h>
     42  1.57  nakayama #include <sys/kauth.h>
     43  1.70  riastrad #include <sys/kernel.h>
     44   1.1  jmcneill #include <sys/kmem.h>
     45  1.70  riastrad #include <sys/module.h>
     46  1.63     isaki #include <sys/poll.h>
     47   1.1  jmcneill #include <sys/proc.h>
     48   1.1  jmcneill #include <sys/select.h>
     49  1.57  nakayama #include <sys/stat.h>
     50  1.70  riastrad #include <sys/vnode.h>
     51   1.1  jmcneill 
     52  1.59     isaki #include <dev/audio/audio_if.h>
     53  1.59     isaki #include <dev/audio/audiovar.h>
     54   1.1  jmcneill 
     55   1.2  jmcneill #include <dev/pad/padvar.h>
     56   1.2  jmcneill 
     57  1.71  riastrad #include "ioconf.h"
     58  1.71  riastrad 
     59  1.59     isaki /* #define PAD_DEBUG */
     60  1.59     isaki #ifdef PAD_DEBUG
     61  1.59     isaki #define DPRINTF(fmt...)	printf(fmt)
     62  1.59     isaki #else
     63  1.59     isaki #define DPRINTF(fmt...) /**/
     64  1.59     isaki #endif
     65  1.59     isaki 
     66  1.57  nakayama #define PADFREQ		44100
     67  1.57  nakayama #define PADCHAN		2
     68  1.57  nakayama #define PADPREC		16
     69  1.57  nakayama 
     70   1.1  jmcneill typedef struct pad_block {
     71   1.1  jmcneill 	uint8_t		*pb_ptr;
     72   1.1  jmcneill 	int		pb_len;
     73   1.1  jmcneill } pad_block_t;
     74   1.1  jmcneill 
     75   1.2  jmcneill enum {
     76   1.2  jmcneill 	PAD_OUTPUT_CLASS,
     77   1.2  jmcneill 	PAD_INPUT_CLASS,
     78   1.2  jmcneill 	PAD_OUTPUT_MASTER_VOLUME,
     79   1.2  jmcneill 	PAD_INPUT_DAC_VOLUME,
     80   1.2  jmcneill 	PAD_ENUM_LAST,
     81   1.2  jmcneill };
     82   1.1  jmcneill 
     83  1.10  christos static int	pad_match(device_t, cfdata_t, void *);
     84   1.5    dyoung static void	pad_attach(device_t, device_t, void *);
     85   1.5    dyoung static int	pad_detach(device_t, int);
     86   1.5    dyoung static void	pad_childdet(device_t, device_t);
     87   1.1  jmcneill 
     88  1.59     isaki static int	pad_query_format(void *, audio_format_query_t *);
     89  1.59     isaki static int	pad_set_format(void *, int,
     90  1.59     isaki 		    const audio_params_t *, const audio_params_t *,
     91  1.59     isaki 		    audio_filter_reg_t *, audio_filter_reg_t *);
     92   1.1  jmcneill static int	pad_start_output(void *, void *, int,
     93  1.62     isaki 		    void (*)(void *), void *);
     94   1.1  jmcneill static int	pad_halt_output(void *);
     95   1.1  jmcneill static int	pad_getdev(void *, struct audio_device *);
     96   1.1  jmcneill static int	pad_set_port(void *, mixer_ctrl_t *);
     97   1.1  jmcneill static int	pad_get_port(void *, mixer_ctrl_t *);
     98   1.1  jmcneill static int	pad_query_devinfo(void *, mixer_devinfo_t *);
     99   1.1  jmcneill static int	pad_get_props(void *);
    100  1.17  jmcneill static void	pad_get_locks(void *, kmutex_t **, kmutex_t **);
    101   1.1  jmcneill 
    102  1.59     isaki static void	pad_done_output(void *);
    103  1.59     isaki static void	pad_swvol_codec(audio_filter_arg_t *);
    104  1.22  jmcneill 
    105  1.71  riastrad static void	pad_close(struct pad_softc *);
    106  1.62     isaki static int	pad_read(struct pad_softc *, off_t *, struct uio *,
    107  1.62     isaki 		    kauth_cred_t, int);
    108  1.62     isaki 
    109  1.62     isaki static int	fops_pad_close(struct file *);
    110  1.62     isaki static int	fops_pad_read(struct file *, off_t *, struct uio *,
    111  1.62     isaki 		    kauth_cred_t, int);
    112  1.62     isaki static int	fops_pad_write(struct file *, off_t *, struct uio *,
    113  1.62     isaki 		    kauth_cred_t, int);
    114  1.62     isaki static int	fops_pad_ioctl(struct file *, u_long, void *);
    115  1.62     isaki static int	fops_pad_kqfilter(struct file *, struct knote *);
    116  1.62     isaki static int	fops_pad_poll(struct file *, int);
    117  1.62     isaki static int	fops_pad_stat(struct file *, struct stat *);
    118  1.62     isaki static int	fops_pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
    119  1.62     isaki 		    struct uvm_object **, int *);
    120  1.32  pgoyette 
    121   1.1  jmcneill static const struct audio_hw_if pad_hw_if = {
    122  1.62     isaki 	.query_format	= pad_query_format,
    123  1.62     isaki 	.set_format	= pad_set_format,
    124  1.62     isaki 	.start_output	= pad_start_output,
    125  1.62     isaki 	.halt_output	= pad_halt_output,
    126  1.62     isaki 	.getdev		= pad_getdev,
    127  1.62     isaki 	.set_port	= pad_set_port,
    128  1.62     isaki 	.get_port	= pad_get_port,
    129  1.62     isaki 	.query_devinfo	= pad_query_devinfo,
    130  1.62     isaki 	.get_props	= pad_get_props,
    131  1.62     isaki 	.get_locks	= pad_get_locks,
    132   1.1  jmcneill };
    133   1.1  jmcneill 
    134   1.1  jmcneill #define PAD_NFORMATS	1
    135   1.1  jmcneill static const struct audio_format pad_formats[PAD_NFORMATS] = {
    136  1.59     isaki 	{
    137  1.59     isaki 		.mode		= AUMODE_PLAY,
    138  1.60     isaki 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    139  1.59     isaki 		.validbits	= PADPREC,
    140  1.59     isaki 		.precision	= PADPREC,
    141  1.59     isaki 		.channels	= PADCHAN,
    142  1.59     isaki 		.channel_mask	= AUFMT_STEREO,
    143  1.59     isaki 		.frequency_type	= 1,
    144  1.59     isaki 		.frequency	= { PADFREQ },
    145  1.59     isaki 	},
    146   1.1  jmcneill };
    147   1.1  jmcneill 
    148   1.1  jmcneill extern void	padattach(int);
    149   1.1  jmcneill 
    150  1.62     isaki static int	pad_add_block(struct pad_softc *, uint8_t *, int);
    151  1.79   mlelstv static int	pad_get_block(struct pad_softc *, pad_block_t *, int, int);
    152   1.1  jmcneill 
    153  1.71  riastrad static dev_type_open(pad_open);
    154   1.1  jmcneill 
    155   1.1  jmcneill const struct cdevsw pad_cdevsw = {
    156  1.71  riastrad 	.d_open		= pad_open,
    157  1.71  riastrad 	.d_close	= noclose,
    158  1.71  riastrad 	.d_read		= noread,
    159  1.62     isaki 	.d_write	= nowrite,
    160  1.62     isaki 	.d_ioctl	= noioctl,
    161  1.62     isaki 	.d_stop		= nostop,
    162  1.62     isaki 	.d_tty		= notty,
    163  1.62     isaki 	.d_poll		= nopoll,
    164  1.62     isaki 	.d_mmap		= nommap,
    165  1.62     isaki 	.d_kqfilter	= nokqfilter,
    166  1.62     isaki 	.d_discard	= nodiscard,
    167  1.62     isaki 	.d_flag		= D_OTHER | D_MPSAFE,
    168   1.1  jmcneill };
    169   1.1  jmcneill 
    170  1.57  nakayama const struct fileops pad_fileops = {
    171  1.62     isaki 	.fo_name	= "pad",
    172  1.62     isaki 	.fo_read	= fops_pad_read,
    173  1.62     isaki 	.fo_write	= fops_pad_write,
    174  1.62     isaki 	.fo_ioctl	= fops_pad_ioctl,
    175  1.62     isaki 	.fo_fcntl	= fnullop_fcntl,
    176  1.62     isaki 	.fo_stat	= fops_pad_stat,
    177  1.62     isaki 	.fo_poll	= fops_pad_poll,
    178  1.62     isaki 	.fo_close	= fops_pad_close,
    179  1.62     isaki 	.fo_mmap	= fops_pad_mmap,
    180  1.62     isaki 	.fo_kqfilter	= fops_pad_kqfilter,
    181  1.62     isaki 	.fo_restart	= fnullop_restart
    182  1.57  nakayama };
    183  1.57  nakayama 
    184  1.62     isaki CFATTACH_DECL2_NEW(pad, sizeof(struct pad_softc),
    185  1.62     isaki     pad_match, pad_attach, pad_detach,
    186   1.5    dyoung     NULL, NULL, pad_childdet);
    187   1.1  jmcneill 
    188   1.1  jmcneill void
    189   1.1  jmcneill padattach(int n)
    190   1.1  jmcneill {
    191  1.57  nakayama 	int error;
    192   1.1  jmcneill 
    193  1.57  nakayama 	error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
    194  1.57  nakayama 	if (error) {
    195   1.1  jmcneill 		aprint_error("%s: couldn't register cfattach: %d\n",
    196  1.57  nakayama 		    pad_cd.cd_name, error);
    197   1.1  jmcneill 		config_cfdriver_detach(&pad_cd);
    198   1.1  jmcneill 		return;
    199   1.1  jmcneill 	}
    200   1.1  jmcneill }
    201   1.1  jmcneill 
    202   1.1  jmcneill static int
    203  1.62     isaki pad_match(device_t parent, cfdata_t data, void *opaque)
    204  1.62     isaki {
    205  1.62     isaki 
    206  1.62     isaki 	return 1;
    207  1.62     isaki }
    208  1.62     isaki 
    209  1.62     isaki static void
    210  1.62     isaki pad_attach(device_t parent, device_t self, void *opaque)
    211  1.62     isaki {
    212  1.68  riastrad 	struct pad_softc *sc = device_private(self);
    213  1.62     isaki 
    214  1.71  riastrad 	KASSERT(KERNEL_LOCKED_P());
    215  1.71  riastrad 
    216  1.62     isaki 	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
    217  1.68  riastrad 
    218  1.68  riastrad 	sc->sc_dev = self;
    219  1.68  riastrad 
    220  1.68  riastrad 	cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
    221  1.68  riastrad 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    222  1.69  riastrad 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    223  1.69  riastrad 	callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
    224  1.69  riastrad 	callout_setfunc(&sc->sc_pcallout, pad_done_output, sc);
    225  1.68  riastrad 
    226  1.68  riastrad 	sc->sc_swvol = 255;
    227  1.68  riastrad 	sc->sc_buflen = 0;
    228  1.68  riastrad 	sc->sc_rpos = sc->sc_wpos = 0;
    229  1.68  riastrad 	sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
    230  1.68  riastrad 
    231  1.68  riastrad 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    232  1.68  riastrad 		aprint_error_dev(sc->sc_dev,
    233  1.68  riastrad 		    "couldn't establish power handler\n");
    234  1.71  riastrad 
    235  1.71  riastrad 	sc->sc_open = 1;
    236  1.62     isaki }
    237  1.62     isaki 
    238  1.62     isaki static int
    239  1.62     isaki pad_detach(device_t self, int flags)
    240  1.62     isaki {
    241  1.68  riastrad 	struct pad_softc *sc = device_private(self);
    242  1.62     isaki 	int cmaj, mn;
    243  1.68  riastrad 	int error;
    244  1.68  riastrad 
    245  1.71  riastrad 	KASSERT(KERNEL_LOCKED_P());
    246  1.71  riastrad 
    247  1.71  riastrad 	/* Prevent detach without going through close -- e.g., drvctl.  */
    248  1.71  riastrad 	if (sc->sc_open)
    249  1.71  riastrad 		return EBUSY;
    250  1.71  riastrad 
    251  1.68  riastrad 	error = config_detach_children(self, flags);
    252  1.68  riastrad 	if (error)
    253  1.68  riastrad 		return error;
    254  1.62     isaki 
    255  1.62     isaki 	cmaj = cdevsw_lookup_major(&pad_cdevsw);
    256  1.62     isaki 	mn = device_unit(sc->sc_dev);
    257  1.68  riastrad 	vdevgone(cmaj, mn, mn, VCHR);
    258  1.68  riastrad 
    259  1.68  riastrad 	pmf_device_deregister(sc->sc_dev);
    260  1.68  riastrad 
    261  1.72  riastrad 	callout_destroy(&sc->sc_pcallout);
    262  1.68  riastrad 	mutex_destroy(&sc->sc_lock);
    263  1.68  riastrad 	mutex_destroy(&sc->sc_intr_lock);
    264  1.68  riastrad 	cv_destroy(&sc->sc_condvar);
    265  1.62     isaki 
    266  1.62     isaki 	return 0;
    267  1.62     isaki }
    268  1.62     isaki 
    269  1.62     isaki static void
    270  1.62     isaki pad_childdet(device_t self, device_t child)
    271  1.62     isaki {
    272  1.62     isaki 	struct pad_softc *sc = device_private(self);
    273  1.62     isaki 
    274  1.73  riastrad 	KASSERT(KERNEL_LOCKED_P());
    275  1.73  riastrad 
    276  1.68  riastrad 	if (child == sc->sc_audiodev)
    277  1.68  riastrad 		sc->sc_audiodev = NULL;
    278  1.62     isaki }
    279  1.62     isaki 
    280  1.62     isaki static int
    281  1.62     isaki pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
    282   1.1  jmcneill {
    283  1.79   mlelstv 	int foff, flen, tlen;
    284   1.1  jmcneill 
    285  1.73  riastrad 	KASSERT(blksize >= 0);
    286  1.73  riastrad 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    287  1.73  riastrad 
    288  1.73  riastrad 	if (blksize > PAD_BUFSIZE ||
    289  1.73  riastrad 	    sc->sc_buflen > PAD_BUFSIZE - (unsigned)blksize)
    290   1.1  jmcneill 		return ENOBUFS;
    291   1.1  jmcneill 
    292  1.79   mlelstv 	foff = sc->sc_wpos;
    293  1.79   mlelstv 	if (sc->sc_wpos + blksize <= PAD_BUFSIZE) {
    294  1.79   mlelstv 		flen = blksize;
    295  1.79   mlelstv 		tlen = 0;
    296  1.79   mlelstv 	} else {
    297  1.79   mlelstv 		flen = PAD_BUFSIZE - sc->sc_wpos;
    298  1.79   mlelstv 		tlen = blksize - flen;
    299   1.1  jmcneill 	}
    300   1.1  jmcneill 
    301  1.79   mlelstv 	sc->sc_wpos = foff + blksize;
    302  1.59     isaki 	if (sc->sc_wpos >= PAD_BUFSIZE)
    303   1.1  jmcneill 		sc->sc_wpos -= PAD_BUFSIZE;
    304   1.1  jmcneill 
    305  1.79   mlelstv 	/*
    306  1.79   mlelstv 	 * release interrupt lock for bulk copy to audio buffer
    307  1.79   mlelstv 	 */
    308  1.79   mlelstv 	mutex_exit(&sc->sc_intr_lock);
    309  1.79   mlelstv 	memcpy(sc->sc_audiobuf + foff, blk, flen);
    310  1.79   mlelstv 	memcpy(sc->sc_audiobuf, blk + flen, tlen);
    311  1.79   mlelstv 	mutex_enter(&sc->sc_intr_lock);
    312  1.79   mlelstv 
    313   1.1  jmcneill 	sc->sc_buflen += blksize;
    314  1.73  riastrad 	cv_broadcast(&sc->sc_condvar);
    315   1.1  jmcneill 
    316   1.1  jmcneill 	return 0;
    317   1.1  jmcneill }
    318   1.1  jmcneill 
    319   1.1  jmcneill static int
    320  1.79   mlelstv pad_get_block(struct pad_softc *sc, pad_block_t *pb, int maxblksize, int dowait)
    321   1.1  jmcneill {
    322  1.73  riastrad 	int l, blksize, error;
    323   1.1  jmcneill 
    324  1.73  riastrad 	KASSERT(maxblksize > 0);
    325  1.73  riastrad 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    326  1.73  riastrad 
    327  1.79   mlelstv 	if (sc->sc_buflen == 0 && !dowait)
    328  1.79   mlelstv 		return EAGAIN;
    329  1.79   mlelstv 
    330  1.73  riastrad 	while (sc->sc_buflen == 0) {
    331  1.73  riastrad 		DPRINTF("%s: wait\n", __func__);
    332  1.73  riastrad 		error = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
    333  1.73  riastrad 		DPRINTF("%s: wake up %d\n", __func__, err);
    334  1.73  riastrad 		if (error)
    335  1.73  riastrad 			return error;
    336  1.73  riastrad 	}
    337  1.73  riastrad 	blksize = uimin(maxblksize, sc->sc_buflen);
    338   1.1  jmcneill 
    339   1.1  jmcneill 	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
    340   1.1  jmcneill 	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
    341   1.1  jmcneill 		pb->pb_len = blksize;
    342   1.1  jmcneill 		sc->sc_rpos += blksize;
    343   1.1  jmcneill 	} else {
    344   1.1  jmcneill 		l = PAD_BUFSIZE - sc->sc_rpos;
    345   1.1  jmcneill 		pb->pb_len = l;
    346   1.1  jmcneill 		sc->sc_rpos = 0;
    347   1.1  jmcneill 	}
    348   1.1  jmcneill 	sc->sc_buflen -= pb->pb_len;
    349   1.1  jmcneill 
    350   1.1  jmcneill 	return 0;
    351   1.1  jmcneill }
    352   1.1  jmcneill 
    353  1.71  riastrad static int
    354  1.71  riastrad pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
    355  1.57  nakayama {
    356  1.71  riastrad 	struct file *fp = NULL;
    357  1.71  riastrad 	device_t self;
    358  1.71  riastrad 	struct pad_softc *sc = NULL;
    359  1.71  riastrad 	cfdata_t cf = NULL;
    360  1.71  riastrad 	int error, fd;
    361  1.71  riastrad 
    362  1.71  riastrad 	error = fd_allocfile(&fp, &fd);
    363  1.71  riastrad 	if (error)
    364  1.71  riastrad 		goto out;
    365  1.57  nakayama 
    366  1.71  riastrad 	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
    367  1.57  nakayama 	cf->cf_name = pad_cd.cd_name;
    368  1.57  nakayama 	cf->cf_atname = pad_cd.cd_name;
    369  1.71  riastrad 	cf->cf_unit = 0;
    370  1.57  nakayama 	cf->cf_fstate = FSTATE_STAR;
    371  1.57  nakayama 
    372  1.71  riastrad 	self = config_attach_pseudo(cf);
    373  1.71  riastrad 	if (self == NULL) {
    374  1.71  riastrad 		error = ENXIO;
    375  1.71  riastrad 		goto out;
    376  1.71  riastrad 	}
    377  1.71  riastrad 	sc = device_private(self);
    378  1.71  riastrad 	KASSERT(sc->sc_dev == self);
    379  1.71  riastrad 	cf = NULL;
    380  1.71  riastrad 
    381  1.71  riastrad 	error = fd_clone(fp, fd, flags, &pad_fileops, sc);
    382  1.71  riastrad 	KASSERT(error == EMOVEFD);
    383  1.71  riastrad 	fp = NULL;
    384  1.71  riastrad 	sc = NULL;
    385  1.71  riastrad 
    386  1.71  riastrad out:	if (sc)
    387  1.71  riastrad 		pad_close(sc);
    388  1.71  riastrad 	if (cf)
    389  1.71  riastrad 		kmem_free(cf, sizeof(*cf));
    390  1.71  riastrad 	if (fp)
    391  1.71  riastrad 		fd_abort(curproc, fp, fd);
    392  1.57  nakayama 	return error;
    393   1.1  jmcneill }
    394   1.1  jmcneill 
    395  1.71  riastrad static void
    396  1.57  nakayama pad_close(struct pad_softc *sc)
    397  1.41       nat {
    398  1.71  riastrad 	device_t self = sc->sc_dev;
    399  1.71  riastrad 	cfdata_t cf = device_cfdata(self);
    400  1.50       nat 
    401  1.75  riastrad 	/*
    402  1.75  riastrad 	 * XXX This is not quite enough to prevent racing with drvctl
    403  1.75  riastrad 	 * detach.  What can happen:
    404  1.75  riastrad 	 *
    405  1.75  riastrad 	 *	cpu0				cpu1
    406  1.75  riastrad 	 *
    407  1.75  riastrad 	 *	pad_close
    408  1.75  riastrad 	 *	take kernel lock
    409  1.75  riastrad 	 *	sc->sc_open = 0
    410  1.75  riastrad 	 *	drop kernel lock
    411  1.75  riastrad 	 *	wait for config_misc_lock
    412  1.75  riastrad 	 *					drvctl detach
    413  1.75  riastrad 	 *					take kernel lock
    414  1.75  riastrad 	 *					drop kernel lock
    415  1.75  riastrad 	 *					wait for config_misc_lock
    416  1.75  riastrad 	 *					retake kernel lock
    417  1.75  riastrad 	 *					drop config_misc_lock
    418  1.75  riastrad 	 *	take config_misc_lock
    419  1.75  riastrad 	 *	wait for kernel lock
    420  1.75  riastrad 	 *					pad_detach (sc_open=0 already)
    421  1.75  riastrad 	 *					free device
    422  1.75  riastrad 	 *					drop kernel lock
    423  1.75  riastrad 	 *	use device after free
    424  1.75  riastrad 	 *
    425  1.75  riastrad 	 * We need a way to grab a reference to the device so it won't
    426  1.75  riastrad 	 * be freed until we're done -- it's OK if we config_detach
    427  1.75  riastrad 	 * twice as long as it's idempotent, but not OK if the first
    428  1.75  riastrad 	 * config_detach frees the struct device before the second one
    429  1.75  riastrad 	 * has finished handling it.
    430  1.75  riastrad 	 */
    431  1.71  riastrad 	KERNEL_LOCK(1, NULL);
    432  1.71  riastrad 	KASSERT(sc->sc_open);
    433  1.71  riastrad 	sc->sc_open = 0;
    434  1.71  riastrad 	(void)config_detach(self, DETACH_FORCE);
    435  1.71  riastrad 	KERNEL_UNLOCK_ONE(NULL);
    436  1.41       nat 
    437  1.71  riastrad 	kmem_free(cf, sizeof(*cf));
    438  1.57  nakayama }
    439  1.57  nakayama 
    440  1.57  nakayama static int
    441  1.57  nakayama fops_pad_close(struct file *fp)
    442  1.57  nakayama {
    443  1.68  riastrad 	struct pad_softc *sc = fp->f_pad;
    444  1.57  nakayama 
    445  1.68  riastrad 	pad_close(sc);
    446  1.68  riastrad 
    447  1.68  riastrad 	return 0;
    448  1.57  nakayama }
    449   1.1  jmcneill 
    450  1.57  nakayama static int
    451  1.62     isaki fops_pad_poll(struct file *fp, int events)
    452  1.57  nakayama {
    453  1.62     isaki 
    454  1.63     isaki 	return POLLERR;
    455  1.57  nakayama }
    456  1.57  nakayama 
    457  1.57  nakayama static int
    458  1.62     isaki fops_pad_kqfilter(struct file *fp, struct knote *kn)
    459  1.57  nakayama {
    460  1.68  riastrad 	struct pad_softc *sc = fp->f_pad;
    461  1.57  nakayama 	dev_t dev;
    462  1.57  nakayama 
    463  1.62     isaki 	dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    464  1.62     isaki 	    device_unit(sc->sc_dev));
    465  1.57  nakayama 
    466  1.57  nakayama 	return seltrue_kqfilter(dev, kn);
    467  1.57  nakayama }
    468  1.57  nakayama 
    469  1.57  nakayama static int
    470  1.62     isaki fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
    471  1.57  nakayama {
    472  1.62     isaki 
    473  1.57  nakayama 	return ENODEV;
    474  1.57  nakayama }
    475   1.1  jmcneill 
    476  1.57  nakayama static int
    477  1.62     isaki fops_pad_stat(struct file *fp, struct stat *st)
    478  1.57  nakayama {
    479  1.68  riastrad 	struct pad_softc *sc = fp->f_pad;
    480  1.57  nakayama 
    481  1.57  nakayama 	memset(st, 0, sizeof(*st));
    482  1.57  nakayama 
    483  1.62     isaki 	st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
    484  1.62     isaki 	    device_unit(sc->sc_dev));
    485  1.57  nakayama 
    486  1.57  nakayama 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    487  1.57  nakayama 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    488  1.57  nakayama 	st->st_mode = S_IFCHR;
    489  1.57  nakayama 
    490  1.54  nakayama 	return 0;
    491  1.41       nat }
    492  1.39       nat 
    493  1.57  nakayama static int
    494  1.62     isaki fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
    495  1.62     isaki     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    496  1.57  nakayama {
    497  1.62     isaki 
    498  1.57  nakayama 	return 1;
    499  1.57  nakayama }
    500  1.57  nakayama 
    501  1.57  nakayama static int
    502  1.57  nakayama fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    503  1.62     isaki     int ioflag)
    504  1.57  nakayama {
    505  1.68  riastrad 	struct pad_softc *sc = fp->f_pad;
    506  1.39       nat 
    507  1.57  nakayama 	return pad_read(sc, offp, uio, cred, ioflag);
    508   1.1  jmcneill }
    509   1.1  jmcneill 
    510  1.57  nakayama static int
    511  1.57  nakayama pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
    512  1.62     isaki     int ioflag)
    513   1.1  jmcneill {
    514   1.1  jmcneill 	pad_block_t pb;
    515  1.79   mlelstv 	int err, first;
    516   1.1  jmcneill 
    517   1.1  jmcneill 	err = 0;
    518  1.79   mlelstv 	first = 1;
    519  1.59     isaki 	DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
    520  1.73  riastrad 	while (uio->uio_resid > 0) {
    521  1.69  riastrad 		mutex_enter(&sc->sc_intr_lock);
    522  1.79   mlelstv 		err = pad_get_block(sc, &pb, MIN(uio->uio_resid, INT_MAX), first);
    523  1.69  riastrad 		mutex_exit(&sc->sc_intr_lock);
    524  1.79   mlelstv 		first = 0;
    525  1.79   mlelstv 		if (err == EAGAIN) {
    526  1.79   mlelstv 			err = 0;
    527  1.79   mlelstv 			break;
    528  1.79   mlelstv 		}
    529  1.59     isaki 		if (err)
    530  1.24       nat 			break;
    531  1.73  riastrad 
    532  1.59     isaki 		DPRINTF("%s: move %d\n", __func__, pb.pb_len);
    533  1.73  riastrad 		err = uiomove(pb.pb_ptr, pb.pb_len, uio);
    534  1.73  riastrad 		if (err)
    535  1.73  riastrad 			break;
    536   1.1  jmcneill 	}
    537   1.1  jmcneill 
    538   1.1  jmcneill 	return err;
    539   1.1  jmcneill }
    540   1.1  jmcneill 
    541   1.1  jmcneill static int
    542  1.62     isaki fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    543  1.62     isaki     int ioflag)
    544  1.57  nakayama {
    545  1.62     isaki 
    546  1.57  nakayama 	return EOPNOTSUPP;
    547  1.57  nakayama }
    548  1.57  nakayama 
    549  1.57  nakayama static int
    550  1.59     isaki pad_query_format(void *opaque, audio_format_query_t *afp)
    551  1.26       nat {
    552  1.26       nat 
    553  1.59     isaki 	return audio_query_format(pad_formats, PAD_NFORMATS, afp);
    554  1.26       nat }
    555  1.26       nat 
    556  1.26       nat static int
    557  1.59     isaki pad_set_format(void *opaque, int setmode,
    558  1.59     isaki     const audio_params_t *play, const audio_params_t *rec,
    559  1.59     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    560   1.1  jmcneill {
    561  1.69  riastrad 	struct pad_softc *sc = opaque;
    562   1.1  jmcneill 
    563  1.17  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    564  1.17  jmcneill 
    565  1.59     isaki 	/* XXX playback only */
    566  1.59     isaki 	pfil->codec = pad_swvol_codec;
    567  1.59     isaki 	pfil->context = sc;
    568   1.2  jmcneill 
    569   1.1  jmcneill 	return 0;
    570   1.1  jmcneill }
    571   1.1  jmcneill 
    572   1.1  jmcneill static int
    573   1.1  jmcneill pad_start_output(void *opaque, void *block, int blksize,
    574   1.1  jmcneill     void (*intr)(void *), void *intrarg)
    575   1.1  jmcneill {
    576  1.69  riastrad 	struct pad_softc *sc = opaque;
    577   1.1  jmcneill 	int err;
    578  1.79   mlelstv 	u_int framesize;
    579  1.79   mlelstv 	int ticks;
    580   1.1  jmcneill 
    581  1.59     isaki 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    582   1.1  jmcneill 
    583   1.1  jmcneill 	sc->sc_intr = intr;
    584   1.1  jmcneill 	sc->sc_intrarg = intrarg;
    585   1.1  jmcneill 
    586  1.59     isaki 	DPRINTF("%s: blksize=%d\n", __func__, blksize);
    587   1.1  jmcneill 	err = pad_add_block(sc, block, blksize);
    588  1.79   mlelstv 	if (err) {
    589  1.79   mlelstv 		DPRINTF("%s: failed: %d\n", __func__, err);
    590  1.79   mlelstv 		/* "Silently" drop overflows, but keep pace */
    591  1.79   mlelstv 		err = 0;
    592  1.79   mlelstv 	}
    593  1.79   mlelstv 
    594  1.79   mlelstv 	framesize = PADCHAN * (PADPREC / NBBY) * PADFREQ;
    595  1.79   mlelstv 
    596  1.79   mlelstv 	sc->sc_resid += blksize;
    597  1.79   mlelstv 	ticks = mstohz(sc->sc_resid * 1000 / framesize);
    598  1.79   mlelstv 	sc->sc_resid -= hztoms(ticks) * framesize / 1000;
    599   1.1  jmcneill 
    600  1.59     isaki 	DPRINTF("%s: callout ms=%d\n", __func__, ms);
    601  1.79   mlelstv 	callout_schedule(&sc->sc_pcallout, ticks);
    602   1.1  jmcneill 
    603  1.66       nia 	return err;
    604   1.1  jmcneill }
    605   1.1  jmcneill 
    606   1.1  jmcneill static int
    607   1.1  jmcneill pad_halt_output(void *opaque)
    608   1.1  jmcneill {
    609  1.69  riastrad 	struct pad_softc *sc = opaque;
    610  1.17  jmcneill 
    611  1.59     isaki 	DPRINTF("%s\n", __func__);
    612  1.59     isaki 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    613  1.17  jmcneill 
    614  1.69  riastrad 	callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
    615  1.69  riastrad 
    616   1.1  jmcneill 	sc->sc_intr = NULL;
    617   1.1  jmcneill 	sc->sc_intrarg = NULL;
    618   1.1  jmcneill 	sc->sc_buflen = 0;
    619  1.79   mlelstv 	sc->sc_resid = 0;
    620   1.1  jmcneill 	sc->sc_rpos = sc->sc_wpos = 0;
    621   1.1  jmcneill 
    622   1.1  jmcneill 	return 0;
    623   1.1  jmcneill }
    624   1.1  jmcneill 
    625  1.59     isaki static void
    626  1.59     isaki pad_done_output(void *arg)
    627  1.59     isaki {
    628  1.69  riastrad 	struct pad_softc *sc = arg;
    629  1.59     isaki 
    630  1.59     isaki 	DPRINTF("%s\n", __func__);
    631  1.59     isaki 
    632  1.59     isaki 	mutex_enter(&sc->sc_intr_lock);
    633  1.59     isaki 	(*sc->sc_intr)(sc->sc_intrarg);
    634  1.59     isaki 	mutex_exit(&sc->sc_intr_lock);
    635  1.59     isaki }
    636  1.59     isaki 
    637   1.1  jmcneill static int
    638   1.1  jmcneill pad_getdev(void *opaque, struct audio_device *ret)
    639   1.1  jmcneill {
    640  1.62     isaki 
    641  1.16  jmcneill 	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
    642  1.16  jmcneill 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    643  1.16  jmcneill 	strlcpy(ret->config, "pad", sizeof(ret->config));
    644   1.1  jmcneill 
    645   1.1  jmcneill 	return 0;
    646   1.1  jmcneill }
    647   1.1  jmcneill 
    648   1.1  jmcneill static int
    649   1.1  jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
    650   1.1  jmcneill {
    651  1.69  riastrad 	struct pad_softc *sc = opaque;
    652   1.2  jmcneill 
    653  1.17  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    654  1.17  jmcneill 
    655   1.2  jmcneill 	switch (mc->dev) {
    656   1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    657   1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    658  1.58  nakayama 		if (mc->un.value.num_channels != 1)
    659  1.58  nakayama 			return EINVAL;
    660   1.2  jmcneill 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    661   1.2  jmcneill 		return 0;
    662   1.2  jmcneill 	}
    663   1.2  jmcneill 
    664   1.1  jmcneill 	return ENXIO;
    665   1.1  jmcneill }
    666   1.1  jmcneill 
    667   1.1  jmcneill static int
    668   1.1  jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
    669   1.1  jmcneill {
    670  1.69  riastrad 	struct pad_softc *sc = opaque;
    671   1.2  jmcneill 
    672  1.17  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    673  1.17  jmcneill 
    674   1.2  jmcneill 	switch (mc->dev) {
    675   1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    676   1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    677  1.58  nakayama 		if (mc->un.value.num_channels != 1)
    678  1.58  nakayama 			return EINVAL;
    679   1.2  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
    680   1.2  jmcneill 		return 0;
    681   1.2  jmcneill 	}
    682   1.2  jmcneill 
    683   1.1  jmcneill 	return ENXIO;
    684   1.1  jmcneill }
    685   1.1  jmcneill 
    686   1.1  jmcneill static int
    687   1.1  jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
    688   1.1  jmcneill {
    689  1.69  riastrad 	struct pad_softc *sc __diagused = opaque;
    690   1.2  jmcneill 
    691  1.17  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    692  1.17  jmcneill 
    693   1.2  jmcneill 	switch (di->index) {
    694   1.2  jmcneill 	case PAD_OUTPUT_CLASS:
    695   1.2  jmcneill 		di->mixer_class = PAD_OUTPUT_CLASS;
    696   1.2  jmcneill 		strcpy(di->label.name, AudioCoutputs);
    697   1.2  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    698   1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    699   1.2  jmcneill 		return 0;
    700   1.2  jmcneill 	case PAD_INPUT_CLASS:
    701   1.2  jmcneill 		di->mixer_class = PAD_INPUT_CLASS;
    702   1.2  jmcneill 		strcpy(di->label.name, AudioCinputs);
    703   1.2  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    704   1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    705   1.2  jmcneill 		return 0;
    706   1.2  jmcneill 	case PAD_OUTPUT_MASTER_VOLUME:
    707   1.2  jmcneill 		di->mixer_class = PAD_OUTPUT_CLASS;
    708   1.2  jmcneill 		strcpy(di->label.name, AudioNmaster);
    709   1.2  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    710   1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    711   1.2  jmcneill 		di->un.v.num_channels = 1;
    712   1.2  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    713   1.2  jmcneill 		return 0;
    714   1.2  jmcneill 	case PAD_INPUT_DAC_VOLUME:
    715   1.2  jmcneill 		di->mixer_class = PAD_INPUT_CLASS;
    716   1.2  jmcneill 		strcpy(di->label.name, AudioNdac);
    717   1.2  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    718   1.2  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    719   1.2  jmcneill 		di->un.v.num_channels = 1;
    720   1.2  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    721   1.2  jmcneill 		return 0;
    722   1.2  jmcneill 	}
    723   1.2  jmcneill 
    724   1.1  jmcneill 	return ENXIO;
    725   1.1  jmcneill }
    726   1.1  jmcneill 
    727   1.1  jmcneill static int
    728   1.1  jmcneill pad_get_props(void *opaque)
    729   1.1  jmcneill {
    730  1.17  jmcneill 
    731  1.59     isaki 	return AUDIO_PROP_PLAYBACK;
    732   1.1  jmcneill }
    733  1.13     ahoka 
    734  1.17  jmcneill static void
    735  1.17  jmcneill pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    736  1.17  jmcneill {
    737  1.69  riastrad 	struct pad_softc *sc = opaque;
    738  1.17  jmcneill 
    739  1.17  jmcneill 	*intr = &sc->sc_intr_lock;
    740  1.17  jmcneill 	*thread = &sc->sc_lock;
    741  1.17  jmcneill }
    742  1.17  jmcneill 
    743  1.22  jmcneill static void
    744  1.59     isaki pad_swvol_codec(audio_filter_arg_t *arg)
    745  1.22  jmcneill {
    746  1.59     isaki 	struct pad_softc *sc = arg->context;
    747  1.59     isaki 	const aint_t *src;
    748  1.59     isaki 	aint_t *dst;
    749  1.59     isaki 	u_int sample_count;
    750  1.59     isaki 	u_int i;
    751  1.59     isaki 
    752  1.59     isaki 	src = arg->src;
    753  1.59     isaki 	dst = arg->dst;
    754  1.59     isaki 	sample_count = arg->count * arg->srcfmt->channels;
    755  1.59     isaki 	for (i = 0; i < sample_count; i++) {
    756  1.59     isaki 		aint2_t v = (aint2_t)(*src++);
    757  1.59     isaki 		v = v * sc->sc_swvol / 255;
    758  1.59     isaki 		*dst++ = (aint_t)v;
    759  1.59     isaki 	}
    760  1.22  jmcneill }
    761  1.22  jmcneill 
    762  1.44  pgoyette MODULE(MODULE_CLASS_DRIVER, pad, "audio");
    763  1.44  pgoyette 
    764  1.13     ahoka #ifdef _MODULE
    765  1.13     ahoka 
    766  1.57  nakayama #include "ioconf.c"
    767  1.57  nakayama 
    768  1.57  nakayama devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    769  1.57  nakayama 
    770  1.57  nakayama /*
    771  1.57  nakayama  * We need our own version of cfattach since config(1)'s ioconf does not
    772  1.57  nakayama  * generate what we need
    773  1.57  nakayama  */
    774  1.57  nakayama 
    775  1.57  nakayama static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
    776  1.13     ahoka 
    777  1.57  nakayama static struct cfattachinit pad_cfattach[] = {
    778  1.57  nakayama 	{ "pad", pad_cfattachinit },
    779  1.57  nakayama 	{ NULL, NULL }
    780  1.45  pgoyette };
    781  1.47  pgoyette #endif
    782  1.45  pgoyette 
    783  1.13     ahoka static int
    784  1.13     ahoka pad_modcmd(modcmd_t cmd, void *arg)
    785  1.13     ahoka {
    786  1.44  pgoyette 	int error = 0;
    787  1.13     ahoka 
    788  1.13     ahoka 	switch (cmd) {
    789  1.13     ahoka 	case MODULE_CMD_INIT:
    790  1.44  pgoyette #ifdef _MODULE
    791  1.78  pgoyette 		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
    792  1.78  pgoyette 			    &pad_cdevsw, &cmajor);
    793  1.78  pgoyette 		if (error)
    794  1.78  pgoyette 			break;
    795  1.78  pgoyette 
    796  1.57  nakayama 		pad_cfattach[1] = cfattach_ioconf_pad[0];
    797  1.57  nakayama 		error = config_init_component(cfdriver_ioconf_pad,
    798  1.57  nakayama 		    pad_cfattach, cfdata_ioconf_pad);
    799  1.13     ahoka 		if (error) {
    800  1.78  pgoyette 			devsw_detach(NULL, &pad_cdevsw);
    801  1.44  pgoyette 			break;
    802  1.13     ahoka 		}
    803  1.44  pgoyette #endif
    804  1.57  nakayama 		break;
    805  1.54  nakayama 
    806  1.13     ahoka 	case MODULE_CMD_FINI:
    807  1.44  pgoyette #ifdef _MODULE
    808  1.57  nakayama 		error = config_fini_component(cfdriver_ioconf_pad,
    809  1.57  nakayama 		    pad_cfattach, cfdata_ioconf_pad);
    810  1.78  pgoyette 		if (error == 0)
    811  1.78  pgoyette 			devsw_detach(NULL, &pad_cdevsw);
    812  1.44  pgoyette #endif
    813  1.57  nakayama 		break;
    814  1.54  nakayama 
    815  1.13     ahoka 	default:
    816  1.44  pgoyette 		error = ENOTTY;
    817  1.13     ahoka 	}
    818  1.44  pgoyette 
    819  1.44  pgoyette 	return error;
    820  1.13     ahoka }
    821