Home | History | Annotate | Line # | Download | only in usb
uaudio.c revision 1.121
      1  1.121  jmcneill /*	$NetBSD: uaudio.c,v 1.121 2011/11/23 23:07:36 jmcneill Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4    1.1  augustss  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5    1.1  augustss  * All rights reserved.
      6    1.1  augustss  *
      7    1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.24  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9    1.5  augustss  * Carlstedt Research & Technology.
     10    1.1  augustss  *
     11    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12    1.1  augustss  * modification, are permitted provided that the following conditions
     13    1.1  augustss  * are met:
     14    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19    1.1  augustss  *
     20    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31    1.1  augustss  */
     32    1.1  augustss 
     33    1.1  augustss /*
     34   1.68  augustss  * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf
     35   1.68  augustss  *                  http://www.usb.org/developers/devclass_docs/frmts10.pdf
     36   1.68  augustss  *                  http://www.usb.org/developers/devclass_docs/termt10.pdf
     37    1.1  augustss  */
     38   1.44     lukem 
     39   1.44     lukem #include <sys/cdefs.h>
     40  1.121  jmcneill __KERNEL_RCSID(0, "$NetBSD: uaudio.c,v 1.121 2011/11/23 23:07:36 jmcneill Exp $");
     41    1.1  augustss 
     42    1.1  augustss #include <sys/param.h>
     43    1.1  augustss #include <sys/systm.h>
     44    1.1  augustss #include <sys/kernel.h>
     45    1.1  augustss #include <sys/malloc.h>
     46    1.1  augustss #include <sys/device.h>
     47    1.1  augustss #include <sys/ioctl.h>
     48    1.1  augustss #include <sys/file.h>
     49   1.40  augustss #include <sys/reboot.h>		/* for bootverbose */
     50    1.1  augustss #include <sys/select.h>
     51    1.1  augustss #include <sys/proc.h>
     52    1.1  augustss #include <sys/vnode.h>
     53    1.1  augustss #include <sys/poll.h>
     54  1.114      kent #include <sys/module.h>
     55  1.119  jmcneill #include <sys/bus.h>
     56    1.1  augustss 
     57    1.1  augustss #include <sys/audioio.h>
     58    1.1  augustss #include <dev/audio_if.h>
     59   1.77   mycroft #include <dev/audiovar.h>
     60    1.1  augustss #include <dev/mulaw.h>
     61    1.1  augustss #include <dev/auconv.h>
     62    1.1  augustss 
     63    1.1  augustss #include <dev/usb/usb.h>
     64    1.1  augustss #include <dev/usb/usbdi.h>
     65  1.119  jmcneill #include <dev/usb/usbdivar.h>
     66    1.1  augustss #include <dev/usb/usbdi_util.h>
     67    1.1  augustss #include <dev/usb/usb_quirks.h>
     68    1.1  augustss 
     69  1.120  jakllsch #include <dev/usb/usbdevs.h>
     70  1.120  jakllsch 
     71    1.1  augustss #include <dev/usb/uaudioreg.h>
     72    1.1  augustss 
     73   1.80      kent /* #define UAUDIO_DEBUG */
     74   1.80      kent /* #define UAUDIO_MULTIPLE_ENDPOINTS */
     75    1.4  augustss #ifdef UAUDIO_DEBUG
     76  1.118    dyoung #define DPRINTF(x)	do { if (uaudiodebug) printf x; } while (0)
     77  1.118    dyoung #define DPRINTFN(n,x)	do { if (uaudiodebug>(n)) printf x; } while (0)
     78    1.1  augustss int	uaudiodebug = 0;
     79    1.1  augustss #else
     80    1.1  augustss #define DPRINTF(x)
     81    1.1  augustss #define DPRINTFN(n,x)
     82    1.1  augustss #endif
     83    1.1  augustss 
     84   1.22  augustss #define UAUDIO_NCHANBUFS 6	/* number of outstanding request */
     85   1.55      kent #define UAUDIO_NFRAMES   10	/* ms of sound in each request */
     86    1.1  augustss 
     87    1.1  augustss 
     88    1.1  augustss #define MIX_MAX_CHAN 8
     89    1.1  augustss struct mixerctl {
     90   1.94      kent 	uint16_t	wValue[MIX_MAX_CHAN]; /* using nchan */
     91   1.94      kent 	uint16_t	wIndex;
     92   1.94      kent 	uint8_t		nchan;
     93   1.94      kent 	uint8_t		type;
     94    1.1  augustss #define MIX_ON_OFF	1
     95    1.1  augustss #define MIX_SIGNED_16	2
     96    1.1  augustss #define MIX_UNSIGNED_16	3
     97    1.1  augustss #define MIX_SIGNED_8	4
     98   1.79      kent #define MIX_SELECTOR	5
     99    1.1  augustss #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1)
    100    1.1  augustss #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
    101   1.34  augustss 	int		minval, maxval;
    102   1.34  augustss 	u_int		delta;
    103   1.34  augustss 	u_int		mul;
    104   1.94      kent 	uint8_t		class;
    105    1.1  augustss 	char		ctlname[MAX_AUDIO_DEV_LEN];
    106   1.99  christos 	const char	*ctlunit;
    107    1.1  augustss };
    108    1.1  augustss #define MAKE(h,l) (((h) << 8) | (l))
    109    1.1  augustss 
    110    1.1  augustss struct as_info {
    111   1.94      kent 	uint8_t		alt;
    112   1.94      kent 	uint8_t		encoding;
    113   1.94      kent 	uint8_t		attributes; /* Copy of bmAttributes of
    114   1.50      kent 				     * usb_audio_streaming_endpoint_descriptor
    115   1.50      kent 				     */
    116   1.37   mycroft 	usbd_interface_handle	ifaceh;
    117   1.78      kent 	const usb_interface_descriptor_t *idesc;
    118   1.78      kent 	const usb_endpoint_descriptor_audio_t *edesc;
    119   1.80      kent 	const usb_endpoint_descriptor_audio_t *edesc1;
    120   1.78      kent 	const struct usb_audio_streaming_type1_descriptor *asf1desc;
    121   1.92      kent 	struct audio_format *aformat;
    122   1.47  jdolecek 	int		sc_busy;	/* currently used */
    123    1.1  augustss };
    124    1.1  augustss 
    125    1.1  augustss struct chan {
    126   1.67       wiz 	void	(*intr)(void *);	/* DMA completion intr handler */
    127    1.1  augustss 	void	*arg;		/* arg for intr() */
    128    1.1  augustss 	usbd_pipe_handle pipe;
    129   1.83      kent 	usbd_pipe_handle sync_pipe;
    130    1.1  augustss 
    131    1.1  augustss 	u_int	sample_size;
    132    1.1  augustss 	u_int	sample_rate;
    133    1.1  augustss 	u_int	bytes_per_frame;
    134    1.1  augustss 	u_int	fraction;	/* fraction/1000 is the extra samples/frame */
    135    1.1  augustss 	u_int	residue;	/* accumulates the fractional samples */
    136    1.1  augustss 
    137    1.1  augustss 	u_char	*start;		/* upper layer buffer start */
    138    1.1  augustss 	u_char	*end;		/* upper layer buffer end */
    139    1.1  augustss 	u_char	*cur;		/* current position in upper layer buffer */
    140    1.1  augustss 	int	blksize;	/* chunk size to report up */
    141    1.1  augustss 	int	transferred;	/* transferred bytes not reported up */
    142    1.1  augustss 
    143   1.47  jdolecek 	int	altidx;		/* currently used altidx */
    144   1.31  augustss 
    145    1.1  augustss 	int	curchanbuf;
    146    1.1  augustss 	struct chanbuf {
    147   1.56      kent 		struct chan	*chan;
    148    1.9  augustss 		usbd_xfer_handle xfer;
    149   1.56      kent 		u_char		*buffer;
    150   1.94      kent 		uint16_t	sizes[UAUDIO_NFRAMES];
    151   1.94      kent 		uint16_t	offsets[UAUDIO_NFRAMES];
    152   1.94      kent 		uint16_t	size;
    153    1.1  augustss 	} chanbufs[UAUDIO_NCHANBUFS];
    154    1.8  augustss 
    155    1.8  augustss 	struct uaudio_softc *sc; /* our softc */
    156    1.1  augustss };
    157    1.1  augustss 
    158  1.121  jmcneill /*
    159  1.121  jmcneill  * XXX Locking notes:
    160  1.121  jmcneill  *
    161  1.121  jmcneill  *    The MI USB audio subsystem is not MP-SAFE.  Our strategy here
    162  1.121  jmcneill  *    is to ensure we have the kernel lock held when calling into
    163  1.121  jmcneill  *    usbd, and, generally, to have dropped the sc_intr_lock during
    164  1.121  jmcneill  *    these sections as well since the usb code will sleep.
    165  1.121  jmcneill  */
    166    1.1  augustss struct uaudio_softc {
    167  1.118    dyoung 	device_t	sc_dev;		/* base device */
    168  1.121  jmcneill 	kmutex_t	sc_lock;
    169  1.121  jmcneill 	kmutex_t	sc_intr_lock;
    170    1.1  augustss 	usbd_device_handle sc_udev;	/* USB device */
    171   1.86      kent 	int		sc_ac_iface;	/* Audio Control interface */
    172    1.1  augustss 	usbd_interface_handle	sc_ac_ifaceh;
    173   1.86      kent 	struct chan	sc_playchan;	/* play channel */
    174   1.86      kent 	struct chan	sc_recchan;	/* record channel */
    175   1.86      kent 	int		sc_nullalt;
    176   1.86      kent 	int		sc_audio_rev;
    177   1.86      kent 	struct as_info	*sc_alts;	/* alternate settings */
    178   1.86      kent 	int		sc_nalts;	/* # of alternate settings */
    179   1.86      kent 	int		sc_altflags;
    180   1.56      kent #define HAS_8		0x01
    181   1.56      kent #define HAS_16		0x02
    182   1.56      kent #define HAS_8U		0x04
    183   1.56      kent #define HAS_ALAW	0x08
    184   1.56      kent #define HAS_MULAW	0x10
    185   1.47  jdolecek #define UA_NOFRAC	0x20		/* don't do sample rate adjustment */
    186   1.51      kent #define HAS_24		0x40
    187   1.86      kent 	int		sc_mode;	/* play/record capability */
    188   1.86      kent 	struct mixerctl *sc_ctls;	/* mixer controls */
    189   1.86      kent 	int		sc_nctls;	/* # of mixer controls */
    190  1.117    dyoung 	device_t	sc_audiodev;
    191   1.92      kent 	struct audio_format *sc_formats;
    192   1.92      kent 	int		sc_nformats;
    193   1.92      kent 	struct audio_encoding_set *sc_encodings;
    194   1.92      kent 	u_int		sc_channel_config;
    195   1.86      kent 	char		sc_dying;
    196  1.119  jmcneill 	struct audio_device sc_adev;
    197    1.1  augustss };
    198    1.1  augustss 
    199   1.82      kent struct terminal_list {
    200   1.82      kent 	int size;
    201   1.82      kent 	uint16_t terminals[1];
    202   1.82      kent };
    203   1.82      kent #define TERMINAL_LIST_SIZE(N)	(offsetof(struct terminal_list, terminals) \
    204   1.82      kent 				+ sizeof(uint16_t) * (N))
    205   1.82      kent 
    206   1.82      kent struct io_terminal {
    207   1.82      kent 	union {
    208  1.109  drochner 		const uaudio_cs_descriptor_t *desc;
    209   1.82      kent 		const struct usb_audio_input_terminal *it;
    210   1.82      kent 		const struct usb_audio_output_terminal *ot;
    211   1.82      kent 		const struct usb_audio_mixer_unit *mu;
    212   1.82      kent 		const struct usb_audio_selector_unit *su;
    213   1.82      kent 		const struct usb_audio_feature_unit *fu;
    214   1.82      kent 		const struct usb_audio_processing_unit *pu;
    215   1.82      kent 		const struct usb_audio_extension_unit *eu;
    216   1.82      kent 	} d;
    217   1.82      kent 	int inputs_size;
    218   1.82      kent 	struct terminal_list **inputs; /* list of source input terminals */
    219   1.82      kent 	struct terminal_list *output; /* list of destination output terminals */
    220   1.83      kent 	int direct;		/* directly connected to an output terminal */
    221   1.82      kent };
    222   1.82      kent 
    223   1.82      kent #define UAC_OUTPUT	0
    224   1.82      kent #define UAC_INPUT	1
    225   1.82      kent #define UAC_EQUAL	2
    226   1.82      kent #define UAC_RECORD	3
    227   1.82      kent #define UAC_NCLASSES	4
    228   1.82      kent #ifdef UAUDIO_DEBUG
    229   1.82      kent Static const char *uac_names[] = {
    230   1.82      kent 	AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord,
    231   1.82      kent };
    232   1.82      kent #endif
    233    1.1  augustss 
    234   1.86      kent Static usbd_status uaudio_identify_ac
    235   1.86      kent 	(struct uaudio_softc *, const usb_config_descriptor_t *);
    236   1.86      kent Static usbd_status uaudio_identify_as
    237   1.86      kent 	(struct uaudio_softc *, const usb_config_descriptor_t *);
    238   1.86      kent Static usbd_status uaudio_process_as
    239   1.86      kent 	(struct uaudio_softc *, const char *, int *, int,
    240   1.86      kent 	 const usb_interface_descriptor_t *);
    241   1.78      kent 
    242   1.78      kent Static void	uaudio_add_alt(struct uaudio_softc *, const struct as_info *);
    243   1.78      kent 
    244   1.78      kent Static const usb_interface_descriptor_t *uaudio_find_iface
    245   1.78      kent 	(const char *, int, int *, int);
    246   1.78      kent 
    247   1.78      kent Static void	uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *);
    248   1.86      kent Static char	*uaudio_id_name
    249   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    250   1.91      kent #ifdef UAUDIO_DEBUG
    251   1.91      kent Static void	uaudio_dump_cluster(const struct usb_audio_cluster *);
    252   1.91      kent #endif
    253   1.86      kent Static struct usb_audio_cluster uaudio_get_cluster
    254   1.86      kent 	(int, const struct io_terminal *);
    255   1.86      kent Static void	uaudio_add_input
    256   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    257   1.86      kent Static void	uaudio_add_output
    258   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    259   1.86      kent Static void	uaudio_add_mixer
    260   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    261   1.86      kent Static void	uaudio_add_selector
    262   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    263   1.82      kent #ifdef UAUDIO_DEBUG
    264   1.82      kent Static const char *uaudio_get_terminal_name(int);
    265   1.82      kent #endif
    266   1.86      kent Static int	uaudio_determine_class
    267   1.86      kent 	(const struct io_terminal *, struct mixerctl *);
    268   1.86      kent Static const char *uaudio_feature_name
    269   1.86      kent 	(const struct io_terminal *, struct mixerctl *);
    270   1.86      kent Static void	uaudio_add_feature
    271   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    272   1.86      kent Static void	uaudio_add_processing_updown
    273   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    274   1.86      kent Static void	uaudio_add_processing
    275   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    276   1.86      kent Static void	uaudio_add_extension
    277   1.86      kent 	(struct uaudio_softc *, const struct io_terminal *, int);
    278   1.86      kent Static struct terminal_list *uaudio_merge_terminal_list
    279   1.86      kent 	(const struct io_terminal *);
    280   1.86      kent Static struct terminal_list *uaudio_io_terminaltype
    281   1.86      kent 	(int, struct io_terminal *, int);
    282   1.86      kent Static usbd_status uaudio_identify
    283   1.86      kent 	(struct uaudio_softc *, const usb_config_descriptor_t *);
    284   1.78      kent 
    285   1.78      kent Static int	uaudio_signext(int, int);
    286   1.78      kent Static int	uaudio_value2bsd(struct mixerctl *, int);
    287   1.78      kent Static int	uaudio_bsd2value(struct mixerctl *, int);
    288   1.78      kent Static int	uaudio_get(struct uaudio_softc *, int, int, int, int, int);
    289   1.86      kent Static int	uaudio_ctl_get
    290   1.86      kent 	(struct uaudio_softc *, int, struct mixerctl *, int);
    291   1.86      kent Static void	uaudio_set
    292   1.86      kent 	(struct uaudio_softc *, int, int, int, int, int, int);
    293   1.86      kent Static void	uaudio_ctl_set
    294   1.86      kent 	(struct uaudio_softc *, int, struct mixerctl *, int, int);
    295   1.78      kent 
    296   1.78      kent Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int);
    297   1.78      kent 
    298   1.78      kent Static usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *);
    299   1.78      kent Static void	uaudio_chan_close(struct uaudio_softc *, struct chan *);
    300   1.86      kent Static usbd_status uaudio_chan_alloc_buffers
    301   1.86      kent 	(struct uaudio_softc *, struct chan *);
    302   1.78      kent Static void	uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *);
    303   1.86      kent Static void	uaudio_chan_init
    304   1.86      kent 	(struct chan *, int, const struct audio_params *, int);
    305   1.78      kent Static void	uaudio_chan_set_param(struct chan *, u_char *, u_char *, int);
    306   1.78      kent Static void	uaudio_chan_ptransfer(struct chan *);
    307   1.86      kent Static void	uaudio_chan_pintr
    308   1.86      kent 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
    309   1.78      kent 
    310   1.78      kent Static void	uaudio_chan_rtransfer(struct chan *);
    311   1.86      kent Static void	uaudio_chan_rintr
    312   1.86      kent 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
    313   1.78      kent 
    314   1.78      kent Static int	uaudio_open(void *, int);
    315   1.78      kent Static void	uaudio_close(void *);
    316   1.78      kent Static int	uaudio_drain(void *);
    317   1.78      kent Static int	uaudio_query_encoding(void *, struct audio_encoding *);
    318   1.86      kent Static int	uaudio_set_params
    319   1.93      kent 	(void *, int, int, struct audio_params *, struct audio_params *,
    320   1.93      kent 	 stream_filter_list_t *, stream_filter_list_t *);
    321   1.93      kent Static int	uaudio_round_blocksize(void *, int, int, const audio_params_t *);
    322   1.86      kent Static int	uaudio_trigger_output
    323   1.86      kent 	(void *, void *, void *, int, void (*)(void *), void *,
    324   1.93      kent 	 const audio_params_t *);
    325   1.86      kent Static int	uaudio_trigger_input
    326   1.86      kent 	(void *, void *, void *, int, void (*)(void *), void *,
    327   1.93      kent 	 const audio_params_t *);
    328   1.78      kent Static int	uaudio_halt_in_dma(void *);
    329   1.78      kent Static int	uaudio_halt_out_dma(void *);
    330   1.78      kent Static int	uaudio_getdev(void *, struct audio_device *);
    331   1.78      kent Static int	uaudio_mixer_set_port(void *, mixer_ctrl_t *);
    332   1.78      kent Static int	uaudio_mixer_get_port(void *, mixer_ctrl_t *);
    333   1.78      kent Static int	uaudio_query_devinfo(void *, mixer_devinfo_t *);
    334   1.78      kent Static int	uaudio_get_props(void *);
    335  1.121  jmcneill Static void	uaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    336    1.1  augustss 
    337   1.88      yamt Static const struct audio_hw_if uaudio_hw_if = {
    338    1.1  augustss 	uaudio_open,
    339    1.1  augustss 	uaudio_close,
    340    1.1  augustss 	uaudio_drain,
    341    1.1  augustss 	uaudio_query_encoding,
    342    1.1  augustss 	uaudio_set_params,
    343    1.1  augustss 	uaudio_round_blocksize,
    344    1.1  augustss 	NULL,
    345    1.1  augustss 	NULL,
    346    1.1  augustss 	NULL,
    347    1.1  augustss 	NULL,
    348    1.1  augustss 	NULL,
    349    1.1  augustss 	uaudio_halt_out_dma,
    350    1.1  augustss 	uaudio_halt_in_dma,
    351    1.1  augustss 	NULL,
    352    1.1  augustss 	uaudio_getdev,
    353    1.1  augustss 	NULL,
    354    1.1  augustss 	uaudio_mixer_set_port,
    355    1.1  augustss 	uaudio_mixer_get_port,
    356    1.1  augustss 	uaudio_query_devinfo,
    357    1.1  augustss 	NULL,
    358    1.1  augustss 	NULL,
    359    1.1  augustss 	NULL,
    360    1.1  augustss 	NULL,
    361    1.1  augustss 	uaudio_get_props,
    362    1.1  augustss 	uaudio_trigger_output,
    363    1.1  augustss 	uaudio_trigger_input,
    364   1.43  augustss 	NULL,
    365  1.121  jmcneill 	uaudio_get_locks,
    366    1.1  augustss };
    367    1.1  augustss 
    368  1.113      cube int uaudio_match(device_t, cfdata_t, void *);
    369  1.111    dyoung void uaudio_attach(device_t, device_t, void *);
    370  1.111    dyoung int uaudio_detach(device_t, int);
    371  1.111    dyoung void uaudio_childdet(device_t, device_t);
    372  1.111    dyoung int uaudio_activate(device_t, enum devact);
    373  1.111    dyoung 
    374  1.111    dyoung extern struct cfdriver uaudio_cd;
    375  1.111    dyoung 
    376  1.113      cube CFATTACH_DECL2_NEW(uaudio, sizeof(struct uaudio_softc),
    377  1.111    dyoung     uaudio_match, uaudio_attach, uaudio_detach, uaudio_activate, NULL,
    378  1.111    dyoung     uaudio_childdet);
    379    1.1  augustss 
    380  1.118    dyoung int
    381  1.118    dyoung uaudio_match(device_t parent, cfdata_t match, void *aux)
    382    1.1  augustss {
    383  1.118    dyoung 	struct usbif_attach_arg *uaa = aux;
    384    1.1  augustss 
    385    1.1  augustss 	/* Trigger on the control interface. */
    386  1.110  drochner 	if (uaa->class != UICLASS_AUDIO ||
    387  1.110  drochner 	    uaa->subclass != UISUBCLASS_AUDIOCONTROL ||
    388   1.10  augustss 	    (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_AUDIO))
    389   1.94      kent 		return UMATCH_NONE;
    390    1.1  augustss 
    391   1.94      kent 	return UMATCH_IFACECLASS_IFACESUBCLASS;
    392    1.1  augustss }
    393    1.1  augustss 
    394  1.118    dyoung void
    395  1.118    dyoung uaudio_attach(device_t parent, device_t self, void *aux)
    396    1.1  augustss {
    397  1.118    dyoung 	struct uaudio_softc *sc = device_private(self);
    398  1.118    dyoung 	struct usbif_attach_arg *uaa = aux;
    399    1.1  augustss 	usb_interface_descriptor_t *id;
    400    1.1  augustss 	usb_config_descriptor_t *cdesc;
    401   1.98  augustss 	char *devinfop;
    402    1.9  augustss 	usbd_status err;
    403   1.37   mycroft 	int i, j, found;
    404    1.1  augustss 
    405  1.113      cube 	sc->sc_dev = self;
    406  1.116    plunky 	sc->sc_udev = uaa->device;
    407  1.121  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    408  1.121  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
    409  1.116    plunky 
    410  1.119  jmcneill 	strlcpy(sc->sc_adev.name, "USB audio", sizeof(sc->sc_adev.name));
    411  1.119  jmcneill 	strlcpy(sc->sc_adev.version, "", sizeof(sc->sc_adev.version));
    412  1.119  jmcneill 	snprintf(sc->sc_adev.config, sizeof(sc->sc_adev.config), "usb:%08x",
    413  1.119  jmcneill 	    sc->sc_udev->cookie.cookie);
    414  1.119  jmcneill 
    415  1.116    plunky 	aprint_naive("\n");
    416  1.116    plunky 	aprint_normal("\n");
    417  1.113      cube 
    418   1.98  augustss 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    419  1.116    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    420   1.98  augustss 	usbd_devinfo_free(devinfop);
    421    1.1  augustss 
    422    1.1  augustss 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    423   1.13  augustss 	if (cdesc == NULL) {
    424  1.113      cube 		aprint_error_dev(self,
    425  1.113      cube 		    "failed to get configuration descriptor\n");
    426  1.118    dyoung 		return;
    427   1.13  augustss 	}
    428    1.1  augustss 
    429    1.9  augustss 	err = uaudio_identify(sc, cdesc);
    430    1.9  augustss 	if (err) {
    431  1.113      cube 		aprint_error_dev(self,
    432  1.113      cube 		    "audio descriptors make no sense, error=%d\n", err);
    433  1.118    dyoung 		return;
    434    1.1  augustss 	}
    435    1.1  augustss 
    436    1.1  augustss 	sc->sc_ac_ifaceh = uaa->iface;
    437    1.1  augustss 	/* Pick up the AS interface. */
    438    1.1  augustss 	for (i = 0; i < uaa->nifaces; i++) {
    439   1.37   mycroft 		if (uaa->ifaces[i] == NULL)
    440   1.37   mycroft 			continue;
    441   1.37   mycroft 		id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    442   1.37   mycroft 		if (id == NULL)
    443   1.37   mycroft 			continue;
    444   1.37   mycroft 		found = 0;
    445   1.37   mycroft 		for (j = 0; j < sc->sc_nalts; j++) {
    446   1.37   mycroft 			if (id->bInterfaceNumber ==
    447   1.37   mycroft 			    sc->sc_alts[j].idesc->bInterfaceNumber) {
    448   1.37   mycroft 				sc->sc_alts[j].ifaceh = uaa->ifaces[i];
    449   1.37   mycroft 				found = 1;
    450    1.1  augustss 			}
    451    1.1  augustss 		}
    452   1.37   mycroft 		if (found)
    453   1.37   mycroft 			uaa->ifaces[i] = NULL;
    454    1.1  augustss 	}
    455    1.1  augustss 
    456   1.37   mycroft 	for (j = 0; j < sc->sc_nalts; j++) {
    457   1.37   mycroft 		if (sc->sc_alts[j].ifaceh == NULL) {
    458  1.113      cube 			aprint_error_dev(self,
    459  1.113      cube 			    "alt %d missing AS interface(s)\n", j);
    460  1.118    dyoung 			return;
    461   1.37   mycroft 		}
    462    1.1  augustss 	}
    463    1.1  augustss 
    464  1.113      cube 	aprint_normal_dev(self, "audio rev %d.%02x\n",
    465    1.7  augustss 	       sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff);
    466    1.7  augustss 
    467   1.47  jdolecek 	sc->sc_playchan.sc = sc->sc_recchan.sc = sc;
    468   1.60      kent 	sc->sc_playchan.altidx = -1;
    469   1.60      kent 	sc->sc_recchan.altidx = -1;
    470    1.8  augustss 
    471   1.31  augustss 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC)
    472   1.47  jdolecek 		sc->sc_altflags |= UA_NOFRAC;
    473   1.31  augustss 
    474   1.40  augustss #ifndef UAUDIO_DEBUG
    475   1.40  augustss 	if (bootverbose)
    476   1.40  augustss #endif
    477  1.113      cube 		aprint_normal_dev(self, "%d mixer controls\n",
    478   1.40  augustss 		    sc->sc_nctls);
    479   1.40  augustss 
    480   1.41  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    481  1.118    dyoung 			   sc->sc_dev);
    482   1.41  augustss 
    483    1.1  augustss 	DPRINTF(("uaudio_attach: doing audio_attach_mi\n"));
    484   1.23  augustss #if defined(__OpenBSD__)
    485   1.23  augustss 	audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev);
    486   1.23  augustss #else
    487  1.113      cube 	sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, sc->sc_dev);
    488   1.23  augustss #endif
    489   1.17  augustss 
    490  1.118    dyoung 	return;
    491    1.1  augustss }
    492    1.1  augustss 
    493    1.1  augustss int
    494  1.117    dyoung uaudio_activate(device_t self, enum devact act)
    495    1.1  augustss {
    496  1.117    dyoung 	struct uaudio_softc *sc = device_private(self);
    497    1.1  augustss 
    498    1.1  augustss 	switch (act) {
    499    1.1  augustss 	case DVACT_DEACTIVATE:
    500    1.1  augustss 		sc->sc_dying = 1;
    501  1.117    dyoung 		return 0;
    502  1.117    dyoung 	default:
    503  1.117    dyoung 		return EOPNOTSUPP;
    504    1.1  augustss 	}
    505    1.1  augustss }
    506    1.1  augustss 
    507  1.111    dyoung void
    508  1.111    dyoung uaudio_childdet(device_t self, device_t child)
    509  1.111    dyoung {
    510  1.111    dyoung 	struct uaudio_softc *sc = device_private(self);
    511  1.111    dyoung 
    512  1.111    dyoung 	KASSERT(sc->sc_audiodev == child);
    513  1.111    dyoung 	sc->sc_audiodev = NULL;
    514  1.111    dyoung }
    515  1.111    dyoung 
    516    1.1  augustss int
    517  1.111    dyoung uaudio_detach(device_t self, int flags)
    518    1.1  augustss {
    519  1.111    dyoung 	struct uaudio_softc *sc = device_private(self);
    520   1.94      kent 	int rv;
    521    1.1  augustss 
    522   1.94      kent 	rv = 0;
    523    1.8  augustss 	/* Wait for outstanding requests to complete. */
    524    1.8  augustss 	usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
    525    1.8  augustss 
    526    1.9  augustss 	if (sc->sc_audiodev != NULL)
    527    1.1  augustss 		rv = config_detach(sc->sc_audiodev, flags);
    528   1.17  augustss 
    529   1.17  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    530  1.118    dyoung 			   sc->sc_dev);
    531    1.1  augustss 
    532   1.92      kent 	if (sc->sc_formats != NULL)
    533   1.92      kent 		free(sc->sc_formats, M_USBDEV);
    534   1.92      kent 	auconv_delete_encodings(sc->sc_encodings);
    535  1.121  jmcneill 
    536  1.121  jmcneill 	mutex_destroy(&sc->sc_lock);
    537  1.121  jmcneill 	mutex_destroy(&sc->sc_intr_lock);
    538  1.121  jmcneill 
    539   1.94      kent 	return rv;
    540    1.1  augustss }
    541    1.1  augustss 
    542   1.86      kent Static int
    543   1.26  augustss uaudio_query_encoding(void *addr, struct audio_encoding *fp)
    544    1.1  augustss {
    545   1.94      kent 	struct uaudio_softc *sc;
    546   1.94      kent 	int flags;
    547    1.1  augustss 
    548   1.94      kent 	sc = addr;
    549   1.94      kent 	flags = sc->sc_altflags;
    550    1.1  augustss 	if (sc->sc_dying)
    551   1.94      kent 		return EIO;
    552   1.56      kent 
    553    1.1  augustss 	if (sc->sc_nalts == 0 || flags == 0)
    554   1.94      kent 		return ENXIO;
    555    1.1  augustss 
    556   1.92      kent 	return auconv_query_encoding(sc->sc_encodings, fp);
    557    1.1  augustss }
    558    1.1  augustss 
    559   1.86      kent Static const usb_interface_descriptor_t *
    560   1.99  christos uaudio_find_iface(const char *tbuf, int size, int *offsp, int subtype)
    561    1.1  augustss {
    562   1.78      kent 	const usb_interface_descriptor_t *d;
    563    1.1  augustss 
    564    1.1  augustss 	while (*offsp < size) {
    565   1.99  christos 		d = (const void *)(tbuf + *offsp);
    566    1.1  augustss 		*offsp += d->bLength;
    567    1.1  augustss 		if (d->bDescriptorType == UDESC_INTERFACE &&
    568   1.19  augustss 		    d->bInterfaceClass == UICLASS_AUDIO &&
    569    1.1  augustss 		    d->bInterfaceSubClass == subtype)
    570   1.94      kent 			return d;
    571    1.1  augustss 	}
    572   1.94      kent 	return NULL;
    573    1.1  augustss }
    574    1.1  augustss 
    575   1.86      kent Static void
    576   1.26  augustss uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc)
    577    1.1  augustss {
    578   1.33  augustss 	int res;
    579   1.84      kent 	size_t len;
    580   1.84      kent 	struct mixerctl *nmc;
    581   1.33  augustss 
    582   1.82      kent 	if (mc->class < UAC_NCLASSES) {
    583   1.82      kent 		DPRINTF(("%s: adding %s.%s\n",
    584   1.82      kent 			 __func__, uac_names[mc->class], mc->ctlname));
    585   1.82      kent 	} else {
    586   1.82      kent 		DPRINTF(("%s: adding %s\n", __func__, mc->ctlname));
    587   1.82      kent 	}
    588   1.84      kent 	len = sizeof(*mc) * (sc->sc_nctls + 1);
    589   1.84      kent 	nmc = malloc(len, M_USBDEV, M_NOWAIT);
    590   1.42  christos 	if (nmc == NULL) {
    591  1.113      cube 		aprint_error("uaudio_mixer_add_ctl: no memory\n");
    592    1.1  augustss 		return;
    593    1.1  augustss 	}
    594   1.84      kent 	/* Copy old data, if there was any */
    595   1.84      kent 	if (sc->sc_nctls != 0) {
    596   1.84      kent 		memcpy(nmc, sc->sc_ctls, sizeof(*mc) * (sc->sc_nctls));
    597   1.84      kent 		free(sc->sc_ctls, M_USBDEV);
    598   1.84      kent 	}
    599   1.42  christos 	sc->sc_ctls = nmc;
    600    1.1  augustss 
    601   1.33  augustss 	mc->delta = 0;
    602   1.79      kent 	if (mc->type == MIX_ON_OFF) {
    603   1.79      kent 		mc->minval = 0;
    604   1.79      kent 		mc->maxval = 1;
    605   1.79      kent 	} else if (mc->type == MIX_SELECTOR) {
    606   1.79      kent 		;
    607   1.79      kent 	} else {
    608    1.1  augustss 		/* Determine min and max values. */
    609   1.56      kent 		mc->minval = uaudio_signext(mc->type,
    610   1.56      kent 			uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE,
    611   1.56      kent 				   mc->wValue[0], mc->wIndex,
    612    1.1  augustss 				   MIX_SIZE(mc->type)));
    613   1.56      kent 		mc->maxval = 1 + uaudio_signext(mc->type,
    614    1.1  augustss 			uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE,
    615    1.1  augustss 				   mc->wValue[0], mc->wIndex,
    616    1.1  augustss 				   MIX_SIZE(mc->type)));
    617   1.34  augustss 		mc->mul = mc->maxval - mc->minval;
    618   1.34  augustss 		if (mc->mul == 0)
    619   1.34  augustss 			mc->mul = 1;
    620   1.33  augustss 		res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE,
    621   1.33  augustss 				 mc->wValue[0], mc->wIndex,
    622   1.33  augustss 				 MIX_SIZE(mc->type));
    623   1.34  augustss 		if (res > 0)
    624   1.69       wiz 			mc->delta = (res * 255 + mc->mul/2) / mc->mul;
    625    1.1  augustss 	}
    626    1.1  augustss 
    627    1.1  augustss 	sc->sc_ctls[sc->sc_nctls++] = *mc;
    628    1.1  augustss 
    629    1.4  augustss #ifdef UAUDIO_DEBUG
    630    1.1  augustss 	if (uaudiodebug > 2) {
    631    1.1  augustss 		int i;
    632    1.1  augustss 		DPRINTF(("uaudio_mixer_add_ctl: wValue=%04x",mc->wValue[0]));
    633    1.1  augustss 		for (i = 1; i < mc->nchan; i++)
    634    1.1  augustss 			DPRINTF((",%04x", mc->wValue[i]));
    635    1.1  augustss 		DPRINTF((" wIndex=%04x type=%d name='%s' unit='%s' "
    636    1.1  augustss 			 "min=%d max=%d\n",
    637    1.1  augustss 			 mc->wIndex, mc->type, mc->ctlname, mc->ctlunit,
    638    1.1  augustss 			 mc->minval, mc->maxval));
    639    1.1  augustss 	}
    640    1.1  augustss #endif
    641    1.1  augustss }
    642    1.1  augustss 
    643   1.86      kent Static char *
    644  1.107  christos uaudio_id_name(struct uaudio_softc *sc,
    645  1.107  christos     const struct io_terminal *iot, int id)
    646    1.1  augustss {
    647   1.99  christos 	static char tbuf[32];
    648   1.94      kent 
    649   1.99  christos 	snprintf(tbuf, sizeof(tbuf), "i%d", id);
    650   1.99  christos 	return tbuf;
    651    1.1  augustss }
    652    1.1  augustss 
    653   1.91      kent #ifdef UAUDIO_DEBUG
    654   1.91      kent Static void
    655   1.91      kent uaudio_dump_cluster(const struct usb_audio_cluster *cl)
    656   1.91      kent {
    657   1.91      kent 	static const char *channel_names[16] = {
    658   1.91      kent 		"LEFT", "RIGHT", "CENTER", "LFE",
    659   1.91      kent 		"LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
    660   1.91      kent 		"SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
    661   1.91      kent 		"RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
    662   1.91      kent 	};
    663   1.91      kent 	int cc, i, first;
    664   1.91      kent 
    665   1.91      kent 	cc = UGETW(cl->wChannelConfig);
    666  1.118    dyoung 	printf("cluster: bNrChannels=%u wChannelConfig=0x%.4x",
    667   1.91      kent 		  cl->bNrChannels, cc);
    668   1.91      kent 	first = TRUE;
    669   1.91      kent 	for (i = 0; cc != 0; i++) {
    670   1.91      kent 		if (cc & 1) {
    671  1.118    dyoung 			printf("%c%s", first ? '<' : ',', channel_names[i]);
    672   1.91      kent 			first = FALSE;
    673   1.91      kent 		}
    674   1.91      kent 		cc = cc >> 1;
    675   1.91      kent 	}
    676  1.118    dyoung 	printf("> iChannelNames=%u", cl->iChannelNames);
    677   1.91      kent }
    678   1.91      kent #endif
    679   1.91      kent 
    680   1.86      kent Static struct usb_audio_cluster
    681   1.82      kent uaudio_get_cluster(int id, const struct io_terminal *iot)
    682    1.1  augustss {
    683    1.1  augustss 	struct usb_audio_cluster r;
    684  1.109  drochner 	const uaudio_cs_descriptor_t *dp;
    685    1.1  augustss 	int i;
    686    1.1  augustss 
    687    1.1  augustss 	for (i = 0; i < 25; i++) { /* avoid infinite loops */
    688   1.82      kent 		dp = iot[id].d.desc;
    689    1.1  augustss 		if (dp == 0)
    690    1.1  augustss 			goto bad;
    691    1.1  augustss 		switch (dp->bDescriptorSubtype) {
    692    1.1  augustss 		case UDESCSUB_AC_INPUT:
    693   1.82      kent 			r.bNrChannels = iot[id].d.it->bNrChannels;
    694   1.82      kent 			USETW(r.wChannelConfig, UGETW(iot[id].d.it->wChannelConfig));
    695   1.82      kent 			r.iChannelNames = iot[id].d.it->iChannelNames;
    696   1.94      kent 			return r;
    697    1.1  augustss 		case UDESCSUB_AC_OUTPUT:
    698   1.82      kent 			id = iot[id].d.ot->bSourceId;
    699    1.1  augustss 			break;
    700    1.1  augustss 		case UDESCSUB_AC_MIXER:
    701   1.99  christos 			r = *(const struct usb_audio_cluster *)
    702   1.82      kent 				&iot[id].d.mu->baSourceId[iot[id].d.mu->bNrInPins];
    703   1.94      kent 			return r;
    704    1.1  augustss 		case UDESCSUB_AC_SELECTOR:
    705    1.1  augustss 			/* XXX This is not really right */
    706   1.82      kent 			id = iot[id].d.su->baSourceId[0];
    707    1.1  augustss 			break;
    708    1.1  augustss 		case UDESCSUB_AC_FEATURE:
    709   1.82      kent 			id = iot[id].d.fu->bSourceId;
    710    1.1  augustss 			break;
    711    1.1  augustss 		case UDESCSUB_AC_PROCESSING:
    712   1.99  christos 			r = *(const struct usb_audio_cluster *)
    713   1.82      kent 				&iot[id].d.pu->baSourceId[iot[id].d.pu->bNrInPins];
    714   1.94      kent 			return r;
    715    1.1  augustss 		case UDESCSUB_AC_EXTENSION:
    716   1.99  christos 			r = *(const struct usb_audio_cluster *)
    717   1.82      kent 				&iot[id].d.eu->baSourceId[iot[id].d.eu->bNrInPins];
    718   1.94      kent 			return r;
    719    1.1  augustss 		default:
    720    1.1  augustss 			goto bad;
    721    1.1  augustss 		}
    722    1.1  augustss 	}
    723    1.1  augustss  bad:
    724  1.113      cube 	aprint_error("uaudio_get_cluster: bad data\n");
    725    1.1  augustss 	memset(&r, 0, sizeof r);
    726   1.94      kent 	return r;
    727    1.1  augustss 
    728    1.1  augustss }
    729    1.1  augustss 
    730   1.86      kent Static void
    731   1.82      kent uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
    732    1.1  augustss {
    733   1.94      kent 	const struct usb_audio_input_terminal *d;
    734    1.1  augustss 
    735   1.94      kent 	d = iot[id].d.it;
    736   1.92      kent #ifdef UAUDIO_DEBUG
    737    1.1  augustss 	DPRINTFN(2,("uaudio_add_input: bTerminalId=%d wTerminalType=0x%04x "
    738    1.1  augustss 		    "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
    739    1.1  augustss 		    "iChannelNames=%d iTerminal=%d\n",
    740    1.1  augustss 		    d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
    741    1.1  augustss 		    d->bNrChannels, UGETW(d->wChannelConfig),
    742    1.1  augustss 		    d->iChannelNames, d->iTerminal));
    743    1.1  augustss #endif
    744   1.92      kent 	/* If USB input terminal, record wChannelConfig */
    745   1.92      kent 	if ((UGETW(d->wTerminalType) & 0xff00) != 0x0100)
    746   1.92      kent 		return;
    747   1.92      kent 	sc->sc_channel_config = UGETW(d->wChannelConfig);
    748    1.1  augustss }
    749    1.1  augustss 
    750   1.86      kent Static void
    751  1.107  christos uaudio_add_output(struct uaudio_softc *sc,
    752  1.107  christos     const struct io_terminal *iot, int id)
    753    1.1  augustss {
    754    1.4  augustss #ifdef UAUDIO_DEBUG
    755   1.94      kent 	const struct usb_audio_output_terminal *d;
    756    1.1  augustss 
    757   1.94      kent 	d = iot[id].d.ot;
    758    1.1  augustss 	DPRINTFN(2,("uaudio_add_output: bTerminalId=%d wTerminalType=0x%04x "
    759    1.1  augustss 		    "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
    760    1.1  augustss 		    d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
    761    1.1  augustss 		    d->bSourceId, d->iTerminal));
    762    1.1  augustss #endif
    763    1.1  augustss }
    764    1.1  augustss 
    765   1.86      kent Static void
    766   1.82      kent uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
    767    1.1  augustss {
    768   1.94      kent 	const struct usb_audio_mixer_unit *d;
    769   1.99  christos 	const struct usb_audio_mixer_unit_1 *d1;
    770    1.1  augustss 	int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k;
    771   1.99  christos 	const uByte *bm;
    772    1.1  augustss 	struct mixerctl mix;
    773    1.1  augustss 
    774   1.94      kent 	d = iot[id].d.mu;
    775    1.1  augustss 	DPRINTFN(2,("uaudio_add_mixer: bUnitId=%d bNrInPins=%d\n",
    776    1.1  augustss 		    d->bUnitId, d->bNrInPins));
    777   1.56      kent 
    778    1.1  augustss 	/* Compute the number of input channels */
    779    1.1  augustss 	ichs = 0;
    780    1.1  augustss 	for (i = 0; i < d->bNrInPins; i++)
    781   1.82      kent 		ichs += uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
    782    1.1  augustss 
    783    1.1  augustss 	/* and the number of output channels */
    784   1.99  christos 	d1 = (const struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins];
    785    1.1  augustss 	ochs = d1->bNrChannels;
    786    1.1  augustss 	DPRINTFN(2,("uaudio_add_mixer: ichs=%d ochs=%d\n", ichs, ochs));
    787    1.1  augustss 
    788    1.1  augustss 	bm = d1->bmControls;
    789    1.1  augustss 	mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
    790   1.82      kent 	uaudio_determine_class(&iot[id], &mix);
    791    1.1  augustss 	mix.type = MIX_SIGNED_16;
    792    1.1  augustss 	mix.ctlunit = AudioNvolume;
    793  1.101    dyoung #define _BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1)
    794    1.1  augustss 	for (p = i = 0; i < d->bNrInPins; i++) {
    795   1.82      kent 		chs = uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
    796    1.1  augustss 		mc = 0;
    797    1.1  augustss 		for (c = 0; c < chs; c++) {
    798    1.1  augustss 			mo = 0;
    799    1.1  augustss 			for (o = 0; o < ochs; o++) {
    800    1.1  augustss 				bno = (p + c) * ochs + o;
    801  1.101    dyoung 				if (_BIT(bno))
    802    1.1  augustss 					mo++;
    803    1.1  augustss 			}
    804    1.1  augustss 			if (mo == 1)
    805    1.1  augustss 				mc++;
    806    1.1  augustss 		}
    807    1.1  augustss 		if (mc == chs && chs <= MIX_MAX_CHAN) {
    808    1.1  augustss 			k = 0;
    809    1.1  augustss 			for (c = 0; c < chs; c++)
    810    1.1  augustss 				for (o = 0; o < ochs; o++) {
    811    1.1  augustss 					bno = (p + c) * ochs + o;
    812  1.101    dyoung 					if (_BIT(bno))
    813   1.56      kent 						mix.wValue[k++] =
    814    1.1  augustss 							MAKE(p+c+1, o+1);
    815    1.1  augustss 				}
    816   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname), "mix%d-%s",
    817   1.82      kent 			    d->bUnitId, uaudio_id_name(sc, iot,
    818   1.70    itojun 			    d->baSourceId[i]));
    819    1.1  augustss 			mix.nchan = chs;
    820    1.1  augustss 			uaudio_mixer_add_ctl(sc, &mix);
    821    1.1  augustss 		} else {
    822    1.1  augustss 			/* XXX */
    823    1.1  augustss 		}
    824  1.101    dyoung #undef _BIT
    825    1.1  augustss 		p += chs;
    826    1.1  augustss 	}
    827    1.1  augustss 
    828    1.1  augustss }
    829    1.1  augustss 
    830   1.86      kent Static void
    831   1.82      kent uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
    832    1.1  augustss {
    833   1.94      kent 	const struct usb_audio_selector_unit *d;
    834   1.79      kent 	struct mixerctl mix;
    835   1.79      kent 	int i, wp;
    836    1.1  augustss 
    837   1.94      kent 	d = iot[id].d.su;
    838    1.1  augustss 	DPRINTFN(2,("uaudio_add_selector: bUnitId=%d bNrInPins=%d\n",
    839    1.1  augustss 		    d->bUnitId, d->bNrInPins));
    840   1.79      kent 	mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
    841   1.79      kent 	mix.wValue[0] = MAKE(0, 0);
    842   1.82      kent 	uaudio_determine_class(&iot[id], &mix);
    843   1.79      kent 	mix.nchan = 1;
    844   1.79      kent 	mix.type = MIX_SELECTOR;
    845   1.79      kent 	mix.ctlunit = "";
    846   1.79      kent 	mix.minval = 1;
    847   1.79      kent 	mix.maxval = d->bNrInPins;
    848   1.79      kent 	mix.mul = mix.maxval - mix.minval;
    849   1.81      kent 	wp = snprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId);
    850   1.79      kent 	for (i = 1; i <= d->bNrInPins; i++) {
    851   1.79      kent 		wp += snprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp,
    852   1.79      kent 			       "i%d", d->baSourceId[i - 1]);
    853   1.79      kent 		if (wp > MAX_AUDIO_DEV_LEN - 1)
    854   1.79      kent 			break;
    855   1.79      kent 	}
    856   1.79      kent 	uaudio_mixer_add_ctl(sc, &mix);
    857    1.1  augustss }
    858    1.1  augustss 
    859   1.82      kent #ifdef UAUDIO_DEBUG
    860   1.86      kent Static const char *
    861   1.82      kent uaudio_get_terminal_name(int terminal_type)
    862   1.82      kent {
    863   1.99  christos 	static char tbuf[100];
    864   1.82      kent 
    865   1.82      kent 	switch (terminal_type) {
    866   1.87      kent 	/* USB terminal types */
    867   1.87      kent 	case UAT_UNDEFINED:	return "UAT_UNDEFINED";
    868   1.82      kent 	case UAT_STREAM:	return "UAT_STREAM";
    869   1.87      kent 	case UAT_VENDOR:	return "UAT_VENDOR";
    870   1.87      kent 	/* input terminal types */
    871   1.87      kent 	case UATI_UNDEFINED:	return "UATI_UNDEFINED";
    872   1.82      kent 	case UATI_MICROPHONE:	return "UATI_MICROPHONE";
    873   1.82      kent 	case UATI_DESKMICROPHONE:	return "UATI_DESKMICROPHONE";
    874   1.82      kent 	case UATI_PERSONALMICROPHONE:	return "UATI_PERSONALMICROPHONE";
    875   1.82      kent 	case UATI_OMNIMICROPHONE:	return "UATI_OMNIMICROPHONE";
    876   1.82      kent 	case UATI_MICROPHONEARRAY:	return "UATI_MICROPHONEARRAY";
    877   1.82      kent 	case UATI_PROCMICROPHONEARR:	return "UATI_PROCMICROPHONEARR";
    878   1.87      kent 	/* output terminal types */
    879   1.87      kent 	case UATO_UNDEFINED:	return "UATO_UNDEFINED";
    880   1.82      kent 	case UATO_SPEAKER:	return "UATO_SPEAKER";
    881   1.87      kent 	case UATO_HEADPHONES:	return "UATO_HEADPHONES";
    882   1.87      kent 	case UATO_DISPLAYAUDIO:	return "UATO_DISPLAYAUDIO";
    883   1.82      kent 	case UATO_DESKTOPSPEAKER:	return "UATO_DESKTOPSPEAKER";
    884   1.82      kent 	case UATO_ROOMSPEAKER:	return "UATO_ROOMSPEAKER";
    885   1.82      kent 	case UATO_COMMSPEAKER:	return "UATO_COMMSPEAKER";
    886   1.82      kent 	case UATO_SUBWOOFER:	return "UATO_SUBWOOFER";
    887   1.87      kent 	/* bidir terminal types */
    888   1.82      kent 	case UATB_UNDEFINED:	return "UATB_UNDEFINED";
    889   1.82      kent 	case UATB_HANDSET:	return "UATB_HANDSET";
    890   1.82      kent 	case UATB_HEADSET:	return "UATB_HEADSET";
    891   1.82      kent 	case UATB_SPEAKERPHONE:	return "UATB_SPEAKERPHONE";
    892   1.82      kent 	case UATB_SPEAKERPHONEESUP:	return "UATB_SPEAKERPHONEESUP";
    893   1.82      kent 	case UATB_SPEAKERPHONEECANC:	return "UATB_SPEAKERPHONEECANC";
    894   1.87      kent 	/* telephony terminal types */
    895   1.82      kent 	case UATT_UNDEFINED:	return "UATT_UNDEFINED";
    896   1.82      kent 	case UATT_PHONELINE:	return "UATT_PHONELINE";
    897   1.82      kent 	case UATT_TELEPHONE:	return "UATT_TELEPHONE";
    898   1.82      kent 	case UATT_DOWNLINEPHONE:	return "UATT_DOWNLINEPHONE";
    899   1.87      kent 	/* external terminal types */
    900   1.82      kent 	case UATE_UNDEFINED:	return "UATE_UNDEFINED";
    901   1.87      kent 	case UATE_ANALOGCONN:	return "UATE_ANALOGCONN";
    902   1.87      kent 	case UATE_LINECONN:	return "UATE_LINECONN";
    903   1.87      kent 	case UATE_LEGACYCONN:	return "UATE_LEGACYCONN";
    904   1.87      kent 	case UATE_DIGITALAUIFC:	return "UATE_DIGITALAUIFC";
    905   1.87      kent 	case UATE_SPDIF:	return "UATE_SPDIF";
    906   1.87      kent 	case UATE_1394DA:	return "UATE_1394DA";
    907   1.87      kent 	case UATE_1394DV:	return "UATE_1394DV";
    908   1.87      kent 	/* embedded function terminal types */
    909   1.82      kent 	case UATF_UNDEFINED:	return "UATF_UNDEFINED";
    910   1.82      kent 	case UATF_CALIBNOISE:	return "UATF_CALIBNOISE";
    911   1.82      kent 	case UATF_EQUNOISE:	return "UATF_EQUNOISE";
    912   1.87      kent 	case UATF_CDPLAYER:	return "UATF_CDPLAYER";
    913   1.82      kent 	case UATF_DAT:	return "UATF_DAT";
    914   1.82      kent 	case UATF_DCC:	return "UATF_DCC";
    915   1.82      kent 	case UATF_MINIDISK:	return "UATF_MINIDISK";
    916   1.82      kent 	case UATF_ANALOGTAPE:	return "UATF_ANALOGTAPE";
    917   1.82      kent 	case UATF_PHONOGRAPH:	return "UATF_PHONOGRAPH";
    918   1.82      kent 	case UATF_VCRAUDIO:	return "UATF_VCRAUDIO";
    919   1.87      kent 	case UATF_VIDEODISCAUDIO:	return "UATF_VIDEODISCAUDIO";
    920   1.87      kent 	case UATF_DVDAUDIO:	return "UATF_DVDAUDIO";
    921   1.87      kent 	case UATF_TVTUNERAUDIO:	return "UATF_TVTUNERAUDIO";
    922   1.82      kent 	case UATF_SATELLITE:	return "UATF_SATELLITE";
    923   1.82      kent 	case UATF_CABLETUNER:	return "UATF_CABLETUNER";
    924   1.82      kent 	case UATF_DSS:	return "UATF_DSS";
    925   1.82      kent 	case UATF_RADIORECV:	return "UATF_RADIORECV";
    926   1.82      kent 	case UATF_RADIOXMIT:	return "UATF_RADIOXMIT";
    927   1.82      kent 	case UATF_MULTITRACK:	return "UATF_MULTITRACK";
    928   1.87      kent 	case UATF_SYNTHESIZER:	return "UATF_SYNTHESIZER";
    929   1.82      kent 	default:
    930   1.99  christos 		snprintf(tbuf, sizeof(tbuf), "unknown type (0x%.4x)", terminal_type);
    931   1.99  christos 		return tbuf;
    932   1.82      kent 	}
    933   1.82      kent }
    934   1.82      kent #endif
    935   1.82      kent 
    936   1.86      kent Static int
    937   1.82      kent uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix)
    938   1.82      kent {
    939   1.82      kent 	int terminal_type;
    940   1.82      kent 
    941   1.82      kent 	if (iot == NULL || iot->output == NULL) {
    942   1.82      kent 		mix->class = UAC_OUTPUT;
    943   1.82      kent 		return 0;
    944   1.82      kent 	}
    945   1.82      kent 	terminal_type = 0;
    946   1.82      kent 	if (iot->output->size == 1)
    947   1.82      kent 		terminal_type = iot->output->terminals[0];
    948   1.82      kent 	/*
    949   1.82      kent 	 * If the only output terminal is USB,
    950   1.82      kent 	 * the class is UAC_RECORD.
    951   1.82      kent 	 */
    952   1.82      kent 	if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
    953   1.82      kent 		mix->class = UAC_RECORD;
    954   1.82      kent 		if (iot->inputs_size == 1
    955   1.82      kent 		    && iot->inputs[0] != NULL
    956   1.82      kent 		    && iot->inputs[0]->size == 1)
    957   1.82      kent 			return iot->inputs[0]->terminals[0];
    958   1.82      kent 		else
    959   1.82      kent 			return 0;
    960   1.82      kent 	}
    961   1.82      kent 	/*
    962   1.83      kent 	 * If the ultimate destination of the unit is just one output
    963   1.82      kent 	 * terminal and the unit is connected to the output terminal
    964   1.82      kent 	 * directly, the class is UAC_OUTPUT.
    965   1.82      kent 	 */
    966   1.82      kent 	if (terminal_type != 0 && iot->direct) {
    967   1.82      kent 		mix->class = UAC_OUTPUT;
    968   1.82      kent 		return terminal_type;
    969   1.82      kent 	}
    970   1.82      kent 	/*
    971   1.82      kent 	 * If the unit is connected to just one input terminal,
    972   1.82      kent 	 * the class is UAC_INPUT.
    973   1.82      kent 	 */
    974   1.82      kent 	if (iot->inputs_size == 1 && iot->inputs[0] != NULL
    975   1.82      kent 	    && iot->inputs[0]->size == 1) {
    976   1.82      kent 		mix->class = UAC_INPUT;
    977   1.82      kent 		return iot->inputs[0]->terminals[0];
    978   1.82      kent 	}
    979   1.82      kent 	/*
    980   1.82      kent 	 * Otherwise, the class is UAC_OUTPUT.
    981   1.82      kent 	 */
    982   1.82      kent 	mix->class = UAC_OUTPUT;
    983   1.82      kent 	return terminal_type;
    984   1.82      kent }
    985   1.82      kent 
    986   1.86      kent Static const char *
    987   1.82      kent uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix)
    988   1.82      kent {
    989   1.82      kent 	int terminal_type;
    990   1.82      kent 
    991   1.82      kent 	terminal_type = uaudio_determine_class(iot, mix);
    992   1.82      kent 	if (mix->class == UAC_RECORD && terminal_type == 0)
    993   1.82      kent 		return AudioNmixerout;
    994   1.82      kent 	DPRINTF(("%s: terminal_type=%s\n", __func__,
    995   1.82      kent 		 uaudio_get_terminal_name(terminal_type)));
    996   1.82      kent 	switch (terminal_type) {
    997   1.82      kent 	case UAT_STREAM:
    998   1.82      kent 		return AudioNdac;
    999   1.82      kent 
   1000   1.82      kent 	case UATI_MICROPHONE:
   1001   1.82      kent 	case UATI_DESKMICROPHONE:
   1002   1.82      kent 	case UATI_PERSONALMICROPHONE:
   1003   1.82      kent 	case UATI_OMNIMICROPHONE:
   1004   1.82      kent 	case UATI_MICROPHONEARRAY:
   1005   1.82      kent 	case UATI_PROCMICROPHONEARR:
   1006   1.82      kent 		return AudioNmicrophone;
   1007   1.82      kent 
   1008   1.82      kent 	case UATO_SPEAKER:
   1009   1.82      kent 	case UATO_DESKTOPSPEAKER:
   1010   1.82      kent 	case UATO_ROOMSPEAKER:
   1011   1.82      kent 	case UATO_COMMSPEAKER:
   1012   1.82      kent 		return AudioNspeaker;
   1013   1.82      kent 
   1014   1.82      kent 	case UATO_HEADPHONES:
   1015   1.82      kent 		return AudioNheadphone;
   1016   1.82      kent 
   1017   1.87      kent 	case UATO_SUBWOOFER:
   1018   1.87      kent 		return AudioNlfe;
   1019   1.87      kent 
   1020   1.87      kent 	/* telephony terminal types */
   1021   1.87      kent 	case UATT_UNDEFINED:
   1022   1.87      kent 	case UATT_PHONELINE:
   1023   1.87      kent 	case UATT_TELEPHONE:
   1024   1.87      kent 	case UATT_DOWNLINEPHONE:
   1025   1.87      kent 		return "phone";
   1026   1.87      kent 
   1027   1.82      kent 	case UATE_ANALOGCONN:
   1028   1.82      kent 	case UATE_LINECONN:
   1029   1.82      kent 	case UATE_LEGACYCONN:
   1030   1.82      kent 		return AudioNline;
   1031   1.82      kent 
   1032   1.82      kent 	case UATE_DIGITALAUIFC:
   1033   1.82      kent 	case UATE_SPDIF:
   1034   1.82      kent 	case UATE_1394DA:
   1035   1.82      kent 	case UATE_1394DV:
   1036   1.82      kent 		return AudioNaux;
   1037   1.82      kent 
   1038   1.82      kent 	case UATF_CDPLAYER:
   1039   1.82      kent 		return AudioNcd;
   1040   1.82      kent 
   1041   1.82      kent 	case UATF_SYNTHESIZER:
   1042   1.82      kent 		return AudioNfmsynth;
   1043   1.82      kent 
   1044   1.82      kent 	case UATF_VIDEODISCAUDIO:
   1045   1.82      kent 	case UATF_DVDAUDIO:
   1046   1.82      kent 	case UATF_TVTUNERAUDIO:
   1047   1.82      kent 		return AudioNvideo;
   1048   1.82      kent 
   1049   1.82      kent 	case UAT_UNDEFINED:
   1050   1.82      kent 	case UAT_VENDOR:
   1051   1.82      kent 	case UATI_UNDEFINED:
   1052   1.82      kent /* output terminal types */
   1053   1.82      kent 	case UATO_UNDEFINED:
   1054   1.82      kent 	case UATO_DISPLAYAUDIO:
   1055   1.82      kent /* bidir terminal types */
   1056   1.82      kent 	case UATB_UNDEFINED:
   1057   1.82      kent 	case UATB_HANDSET:
   1058   1.82      kent 	case UATB_HEADSET:
   1059   1.82      kent 	case UATB_SPEAKERPHONE:
   1060   1.82      kent 	case UATB_SPEAKERPHONEESUP:
   1061   1.82      kent 	case UATB_SPEAKERPHONEECANC:
   1062   1.82      kent /* external terminal types */
   1063   1.82      kent 	case UATE_UNDEFINED:
   1064   1.82      kent /* embedded function terminal types */
   1065   1.82      kent 	case UATF_UNDEFINED:
   1066   1.82      kent 	case UATF_CALIBNOISE:
   1067   1.82      kent 	case UATF_EQUNOISE:
   1068   1.82      kent 	case UATF_DAT:
   1069   1.82      kent 	case UATF_DCC:
   1070   1.82      kent 	case UATF_MINIDISK:
   1071   1.82      kent 	case UATF_ANALOGTAPE:
   1072   1.82      kent 	case UATF_PHONOGRAPH:
   1073   1.82      kent 	case UATF_VCRAUDIO:
   1074   1.82      kent 	case UATF_SATELLITE:
   1075   1.82      kent 	case UATF_CABLETUNER:
   1076   1.82      kent 	case UATF_DSS:
   1077   1.82      kent 	case UATF_RADIORECV:
   1078   1.82      kent 	case UATF_RADIOXMIT:
   1079   1.82      kent 	case UATF_MULTITRACK:
   1080   1.82      kent 	case 0xffff:
   1081   1.82      kent 	default:
   1082   1.82      kent 		DPRINTF(("%s: 'master' for 0x%.4x\n", __func__, terminal_type));
   1083   1.82      kent 		return AudioNmaster;
   1084   1.82      kent 	}
   1085   1.82      kent 	return AudioNmaster;
   1086   1.82      kent }
   1087   1.82      kent 
   1088   1.86      kent Static void
   1089   1.82      kent uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
   1090    1.1  augustss {
   1091   1.94      kent 	const struct usb_audio_feature_unit *d;
   1092   1.94      kent 	const uByte *ctls;
   1093   1.94      kent 	int ctlsize;
   1094   1.94      kent 	int nchan;
   1095    1.1  augustss 	u_int fumask, mmask, cmask;
   1096    1.1  augustss 	struct mixerctl mix;
   1097    1.1  augustss 	int chan, ctl, i, unit;
   1098   1.82      kent 	const char *mixername;
   1099    1.1  augustss 
   1100    1.1  augustss #define GET(i) (ctls[(i)*ctlsize] | \
   1101    1.1  augustss 		(ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0))
   1102   1.94      kent 	d = iot[id].d.fu;
   1103   1.94      kent 	ctls = d->bmaControls;
   1104   1.94      kent 	ctlsize = d->bControlSize;
   1105   1.94      kent 	nchan = (d->bLength - 7) / ctlsize;
   1106    1.1  augustss 	mmask = GET(0);
   1107    1.1  augustss 	/* Figure out what we can control */
   1108    1.1  augustss 	for (cmask = 0, chan = 1; chan < nchan; chan++) {
   1109    1.1  augustss 		DPRINTFN(9,("uaudio_add_feature: chan=%d mask=%x\n",
   1110    1.1  augustss 			    chan, GET(chan)));
   1111    1.1  augustss 		cmask |= GET(chan);
   1112    1.1  augustss 	}
   1113    1.1  augustss 
   1114   1.82      kent 	DPRINTFN(1,("uaudio_add_feature: bUnitId=%d, "
   1115   1.56      kent 		    "%d channels, mmask=0x%04x, cmask=0x%04x\n",
   1116   1.82      kent 		    d->bUnitId, nchan, mmask, cmask));
   1117    1.1  augustss 
   1118    1.1  augustss 	if (nchan > MIX_MAX_CHAN)
   1119    1.1  augustss 		nchan = MIX_MAX_CHAN;
   1120    1.1  augustss 	unit = d->bUnitId;
   1121    1.1  augustss 	mix.wIndex = MAKE(unit, sc->sc_ac_iface);
   1122    1.1  augustss 	for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) {
   1123    1.1  augustss 		fumask = FU_MASK(ctl);
   1124    1.1  augustss 		DPRINTFN(4,("uaudio_add_feature: ctl=%d fumask=0x%04x\n",
   1125    1.1  augustss 			    ctl, fumask));
   1126    1.1  augustss 		if (mmask & fumask) {
   1127    1.1  augustss 			mix.nchan = 1;
   1128    1.1  augustss 			mix.wValue[0] = MAKE(ctl, 0);
   1129    1.1  augustss 		} else if (cmask & fumask) {
   1130    1.1  augustss 			mix.nchan = nchan - 1;
   1131    1.1  augustss 			for (i = 1; i < nchan; i++) {
   1132    1.1  augustss 				if (GET(i) & fumask)
   1133    1.1  augustss 					mix.wValue[i-1] = MAKE(ctl, i);
   1134    1.1  augustss 				else
   1135    1.1  augustss 					mix.wValue[i-1] = -1;
   1136    1.1  augustss 			}
   1137    1.1  augustss 		} else {
   1138    1.1  augustss 			continue;
   1139    1.1  augustss 		}
   1140    1.1  augustss #undef GET
   1141   1.82      kent 		mixername = uaudio_feature_name(&iot[id], &mix);
   1142    1.1  augustss 		switch (ctl) {
   1143    1.1  augustss 		case MUTE_CONTROL:
   1144    1.1  augustss 			mix.type = MIX_ON_OFF;
   1145   1.63  augustss 			mix.ctlunit = "";
   1146   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1147   1.82      kent 				 "%s.%s", mixername, AudioNmute);
   1148    1.1  augustss 			break;
   1149    1.1  augustss 		case VOLUME_CONTROL:
   1150    1.1  augustss 			mix.type = MIX_SIGNED_16;
   1151   1.63  augustss 			mix.ctlunit = AudioNvolume;
   1152   1.82      kent 			strlcpy(mix.ctlname, mixername, sizeof(mix.ctlname));
   1153    1.1  augustss 			break;
   1154    1.1  augustss 		case BASS_CONTROL:
   1155    1.1  augustss 			mix.type = MIX_SIGNED_8;
   1156   1.63  augustss 			mix.ctlunit = AudioNbass;
   1157   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1158   1.82      kent 				 "%s.%s", mixername, AudioNbass);
   1159    1.1  augustss 			break;
   1160    1.1  augustss 		case MID_CONTROL:
   1161    1.1  augustss 			mix.type = MIX_SIGNED_8;
   1162   1.63  augustss 			mix.ctlunit = AudioNmid;
   1163   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1164   1.82      kent 				 "%s.%s", mixername, AudioNmid);
   1165    1.1  augustss 			break;
   1166    1.1  augustss 		case TREBLE_CONTROL:
   1167    1.1  augustss 			mix.type = MIX_SIGNED_8;
   1168   1.63  augustss 			mix.ctlunit = AudioNtreble;
   1169   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1170   1.82      kent 				 "%s.%s", mixername, AudioNtreble);
   1171    1.1  augustss 			break;
   1172    1.1  augustss 		case GRAPHIC_EQUALIZER_CONTROL:
   1173    1.7  augustss 			continue; /* XXX don't add anything */
   1174    1.1  augustss 			break;
   1175    1.1  augustss 		case AGC_CONTROL:
   1176    1.1  augustss 			mix.type = MIX_ON_OFF;
   1177    1.1  augustss 			mix.ctlunit = "";
   1178   1.82      kent 			snprintf(mix.ctlname, sizeof(mix.ctlname), "%s.%s",
   1179   1.82      kent 				 mixername, AudioNagc);
   1180    1.1  augustss 			break;
   1181    1.1  augustss 		case DELAY_CONTROL:
   1182    1.1  augustss 			mix.type = MIX_UNSIGNED_16;
   1183   1.82      kent 			mix.ctlunit = "4 ms";
   1184   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1185   1.82      kent 				 "%s.%s", mixername, AudioNdelay);
   1186    1.1  augustss 			break;
   1187    1.1  augustss 		case BASS_BOOST_CONTROL:
   1188    1.1  augustss 			mix.type = MIX_ON_OFF;
   1189   1.82      kent 			mix.ctlunit = "";
   1190   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1191   1.82      kent 				 "%s.%s", mixername, AudioNbassboost);
   1192    1.1  augustss 			break;
   1193    1.1  augustss 		case LOUDNESS_CONTROL:
   1194    1.1  augustss 			mix.type = MIX_ON_OFF;
   1195   1.82      kent 			mix.ctlunit = "";
   1196   1.70    itojun 			snprintf(mix.ctlname, sizeof(mix.ctlname),
   1197   1.82      kent 				 "%s.%s", mixername, AudioNloudness);
   1198    1.1  augustss 			break;
   1199    1.1  augustss 		}
   1200    1.1  augustss 		uaudio_mixer_add_ctl(sc, &mix);
   1201    1.1  augustss 	}
   1202    1.1  augustss }
   1203    1.1  augustss 
   1204   1.86      kent Static void
   1205   1.78      kent uaudio_add_processing_updown(struct uaudio_softc *sc,
   1206   1.82      kent 			     const struct io_terminal *iot, int id)
   1207   1.78      kent {
   1208   1.94      kent 	const struct usb_audio_processing_unit *d;
   1209   1.94      kent 	const struct usb_audio_processing_unit_1 *d1;
   1210   1.94      kent 	const struct usb_audio_processing_unit_updown *ud;
   1211   1.16  augustss 	struct mixerctl mix;
   1212   1.16  augustss 	int i;
   1213   1.16  augustss 
   1214   1.94      kent 	d = iot[id].d.pu;
   1215   1.94      kent 	d1 = (const struct usb_audio_processing_unit_1 *)
   1216   1.94      kent 	    &d->baSourceId[d->bNrInPins];
   1217   1.94      kent 	ud = (const struct usb_audio_processing_unit_updown *)
   1218   1.94      kent 	    &d1->bmControls[d1->bControlSize];
   1219   1.16  augustss 	DPRINTFN(2,("uaudio_add_processing_updown: bUnitId=%d bNrModes=%d\n",
   1220   1.16  augustss 		    d->bUnitId, ud->bNrModes));
   1221   1.16  augustss 
   1222   1.16  augustss 	if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
   1223   1.16  augustss 		DPRINTF(("uaudio_add_processing_updown: no mode select\n"));
   1224   1.16  augustss 		return;
   1225   1.16  augustss 	}
   1226   1.16  augustss 
   1227   1.16  augustss 	mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
   1228   1.16  augustss 	mix.nchan = 1;
   1229   1.16  augustss 	mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0);
   1230   1.82      kent 	uaudio_determine_class(&iot[id], &mix);
   1231   1.16  augustss 	mix.type = MIX_ON_OFF;	/* XXX */
   1232   1.16  augustss 	mix.ctlunit = "";
   1233   1.70    itojun 	snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId);
   1234   1.16  augustss 
   1235   1.16  augustss 	for (i = 0; i < ud->bNrModes; i++) {
   1236   1.16  augustss 		DPRINTFN(2,("uaudio_add_processing_updown: i=%d bm=0x%x\n",
   1237   1.16  augustss 			    i, UGETW(ud->waModes[i])));
   1238   1.16  augustss 		/* XXX */
   1239   1.16  augustss 	}
   1240   1.16  augustss 	uaudio_mixer_add_ctl(sc, &mix);
   1241   1.16  augustss }
   1242   1.16  augustss 
   1243   1.86      kent Static void
   1244   1.82      kent uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
   1245    1.1  augustss {
   1246   1.94      kent 	const struct usb_audio_processing_unit *d;
   1247   1.94      kent 	const struct usb_audio_processing_unit_1 *d1;
   1248   1.94      kent 	int ptype;
   1249   1.15  augustss 	struct mixerctl mix;
   1250   1.15  augustss 
   1251   1.94      kent 	d = iot[id].d.pu;
   1252   1.94      kent 	d1 = (const struct usb_audio_processing_unit_1 *)
   1253   1.94      kent 	    &d->baSourceId[d->bNrInPins];
   1254   1.94      kent 	ptype = UGETW(d->wProcessType);
   1255   1.15  augustss 	DPRINTFN(2,("uaudio_add_processing: wProcessType=%d bUnitId=%d "
   1256   1.15  augustss 		    "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins));
   1257   1.15  augustss 
   1258   1.15  augustss 	if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
   1259   1.15  augustss 		mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
   1260   1.15  augustss 		mix.nchan = 1;
   1261   1.15  augustss 		mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0);
   1262   1.82      kent 		uaudio_determine_class(&iot[id], &mix);
   1263   1.15  augustss 		mix.type = MIX_ON_OFF;
   1264   1.15  augustss 		mix.ctlunit = "";
   1265   1.70    itojun 		snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable",
   1266   1.70    itojun 		    d->bUnitId, ptype);
   1267   1.15  augustss 		uaudio_mixer_add_ctl(sc, &mix);
   1268   1.15  augustss 	}
   1269    1.1  augustss 
   1270   1.15  augustss 	switch(ptype) {
   1271   1.15  augustss 	case UPDOWNMIX_PROCESS:
   1272   1.82      kent 		uaudio_add_processing_updown(sc, iot, id);
   1273   1.16  augustss 		break;
   1274   1.15  augustss 	case DOLBY_PROLOGIC_PROCESS:
   1275   1.15  augustss 	case P3D_STEREO_EXTENDER_PROCESS:
   1276   1.15  augustss 	case REVERBATION_PROCESS:
   1277   1.15  augustss 	case CHORUS_PROCESS:
   1278   1.15  augustss 	case DYN_RANGE_COMP_PROCESS:
   1279   1.15  augustss 	default:
   1280   1.15  augustss #ifdef UAUDIO_DEBUG
   1281  1.113      cube 		aprint_debug(
   1282  1.113      cube 		    "uaudio_add_processing: unit %d, type=%d not impl.\n",
   1283  1.113      cube 		    d->bUnitId, ptype);
   1284    1.1  augustss #endif
   1285   1.15  augustss 		break;
   1286   1.15  augustss 	}
   1287    1.1  augustss }
   1288    1.1  augustss 
   1289   1.86      kent Static void
   1290   1.82      kent uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
   1291    1.1  augustss {
   1292   1.94      kent 	const struct usb_audio_extension_unit *d;
   1293   1.94      kent 	const struct usb_audio_extension_unit_1 *d1;
   1294    1.1  augustss 	struct mixerctl mix;
   1295    1.1  augustss 
   1296   1.94      kent 	d = iot[id].d.eu;
   1297   1.94      kent 	d1 = (const struct usb_audio_extension_unit_1 *)
   1298   1.94      kent 	    &d->baSourceId[d->bNrInPins];
   1299    1.1  augustss 	DPRINTFN(2,("uaudio_add_extension: bUnitId=%d bNrInPins=%d\n",
   1300    1.1  augustss 		    d->bUnitId, d->bNrInPins));
   1301   1.28  augustss 
   1302   1.31  augustss 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU)
   1303   1.28  augustss 		return;
   1304    1.1  augustss 
   1305   1.15  augustss 	if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
   1306    1.1  augustss 		mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
   1307    1.1  augustss 		mix.nchan = 1;
   1308    1.1  augustss 		mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0);
   1309   1.82      kent 		uaudio_determine_class(&iot[id], &mix);
   1310    1.1  augustss 		mix.type = MIX_ON_OFF;
   1311    1.1  augustss 		mix.ctlunit = "";
   1312   1.70    itojun 		snprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable",
   1313   1.70    itojun 		    d->bUnitId);
   1314    1.1  augustss 		uaudio_mixer_add_ctl(sc, &mix);
   1315    1.1  augustss 	}
   1316    1.1  augustss }
   1317    1.1  augustss 
   1318   1.86      kent Static struct terminal_list*
   1319   1.82      kent uaudio_merge_terminal_list(const struct io_terminal *iot)
   1320   1.82      kent {
   1321   1.82      kent 	struct terminal_list *tml;
   1322   1.82      kent 	uint16_t *ptm;
   1323   1.82      kent 	int i, len;
   1324   1.82      kent 
   1325   1.82      kent 	len = 0;
   1326   1.82      kent 	if (iot->inputs == NULL)
   1327   1.82      kent 		return NULL;
   1328   1.82      kent 	for (i = 0; i < iot->inputs_size; i++) {
   1329   1.82      kent 		if (iot->inputs[i] != NULL)
   1330   1.82      kent 			len += iot->inputs[i]->size;
   1331   1.82      kent 	}
   1332   1.82      kent 	tml = malloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT);
   1333   1.82      kent 	if (tml == NULL) {
   1334  1.113      cube 		aprint_error("uaudio_merge_terminal_list: no memory\n");
   1335   1.82      kent 		return NULL;
   1336   1.82      kent 	}
   1337   1.82      kent 	tml->size = 0;
   1338   1.82      kent 	ptm = tml->terminals;
   1339   1.82      kent 	for (i = 0; i < iot->inputs_size; i++) {
   1340   1.82      kent 		if (iot->inputs[i] == NULL)
   1341   1.82      kent 			continue;
   1342   1.82      kent 		if (iot->inputs[i]->size > len)
   1343   1.82      kent 			break;
   1344   1.82      kent 		memcpy(ptm, iot->inputs[i]->terminals,
   1345   1.82      kent 		       iot->inputs[i]->size * sizeof(uint16_t));
   1346   1.82      kent 		tml->size += iot->inputs[i]->size;
   1347   1.82      kent 		ptm += iot->inputs[i]->size;
   1348   1.82      kent 		len -= iot->inputs[i]->size;
   1349   1.82      kent 	}
   1350   1.82      kent 	return tml;
   1351   1.82      kent }
   1352   1.82      kent 
   1353   1.86      kent Static struct terminal_list *
   1354   1.82      kent uaudio_io_terminaltype(int outtype, struct io_terminal *iot, int id)
   1355   1.82      kent {
   1356   1.82      kent 	struct terminal_list *tml;
   1357   1.82      kent 	struct io_terminal *it;
   1358   1.82      kent 	int src_id, i;
   1359   1.82      kent 
   1360   1.82      kent 	it = &iot[id];
   1361   1.82      kent 	if (it->output != NULL) {
   1362   1.82      kent 		/* already has outtype? */
   1363   1.82      kent 		for (i = 0; i < it->output->size; i++)
   1364   1.82      kent 			if (it->output->terminals[i] == outtype)
   1365   1.82      kent 				return uaudio_merge_terminal_list(it);
   1366   1.82      kent 		tml = malloc(TERMINAL_LIST_SIZE(it->output->size + 1),
   1367   1.82      kent 			     M_TEMP, M_NOWAIT);
   1368   1.82      kent 		if (tml == NULL) {
   1369  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1370   1.82      kent 			return uaudio_merge_terminal_list(it);
   1371   1.82      kent 		}
   1372   1.82      kent 		memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size));
   1373   1.82      kent 		tml->terminals[it->output->size] = outtype;
   1374   1.82      kent 		tml->size++;
   1375   1.82      kent 		free(it->output, M_TEMP);
   1376   1.82      kent 		it->output = tml;
   1377   1.82      kent 		if (it->inputs != NULL) {
   1378   1.82      kent 			for (i = 0; i < it->inputs_size; i++)
   1379   1.82      kent 				if (it->inputs[i] != NULL)
   1380   1.82      kent 					free(it->inputs[i], M_TEMP);
   1381   1.82      kent 			free(it->inputs, M_TEMP);
   1382   1.82      kent 		}
   1383   1.82      kent 		it->inputs_size = 0;
   1384   1.82      kent 		it->inputs = NULL;
   1385   1.82      kent 	} else {		/* end `iot[id] != NULL' */
   1386   1.82      kent 		it->inputs_size = 0;
   1387   1.82      kent 		it->inputs = NULL;
   1388   1.82      kent 		it->output = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
   1389   1.82      kent 		if (it->output == NULL) {
   1390  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1391   1.82      kent 			return NULL;
   1392   1.82      kent 		}
   1393   1.82      kent 		it->output->terminals[0] = outtype;
   1394   1.82      kent 		it->output->size = 1;
   1395   1.82      kent 		it->direct = FALSE;
   1396   1.82      kent 	}
   1397   1.82      kent 
   1398   1.82      kent 	switch (it->d.desc->bDescriptorSubtype) {
   1399   1.82      kent 	case UDESCSUB_AC_INPUT:
   1400   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
   1401   1.82      kent 		if (it->inputs == NULL) {
   1402  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1403   1.82      kent 			return NULL;
   1404   1.82      kent 		}
   1405   1.82      kent 		tml = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
   1406   1.82      kent 		if (tml == NULL) {
   1407  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1408   1.82      kent 			free(it->inputs, M_TEMP);
   1409   1.82      kent 			it->inputs = NULL;
   1410   1.82      kent 			return NULL;
   1411   1.82      kent 		}
   1412   1.82      kent 		it->inputs[0] = tml;
   1413   1.82      kent 		tml->terminals[0] = UGETW(it->d.it->wTerminalType);
   1414   1.82      kent 		tml->size = 1;
   1415   1.82      kent 		it->inputs_size = 1;
   1416   1.82      kent 		return uaudio_merge_terminal_list(it);
   1417   1.82      kent 	case UDESCSUB_AC_FEATURE:
   1418   1.82      kent 		src_id = it->d.fu->bSourceId;
   1419   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
   1420   1.82      kent 		if (it->inputs == NULL) {
   1421  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1422   1.82      kent 			return uaudio_io_terminaltype(outtype, iot, src_id);
   1423   1.82      kent 		}
   1424   1.82      kent 		it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
   1425   1.82      kent 		it->inputs_size = 1;
   1426   1.82      kent 		return uaudio_merge_terminal_list(it);
   1427   1.82      kent 	case UDESCSUB_AC_OUTPUT:
   1428   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
   1429   1.82      kent 		if (it->inputs == NULL) {
   1430  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1431   1.82      kent 			return NULL;
   1432   1.82      kent 		}
   1433   1.82      kent 		src_id = it->d.ot->bSourceId;
   1434   1.82      kent 		it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
   1435   1.82      kent 		it->inputs_size = 1;
   1436   1.82      kent 		iot[src_id].direct = TRUE;
   1437   1.82      kent 		return NULL;
   1438   1.82      kent 	case UDESCSUB_AC_MIXER:
   1439   1.82      kent 		it->inputs_size = 0;
   1440   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *)
   1441   1.82      kent 				    * it->d.mu->bNrInPins, M_TEMP, M_NOWAIT);
   1442   1.82      kent 		if (it->inputs == NULL) {
   1443  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1444   1.82      kent 			return NULL;
   1445   1.82      kent 		}
   1446   1.82      kent 		for (i = 0; i < it->d.mu->bNrInPins; i++) {
   1447   1.82      kent 			src_id = it->d.mu->baSourceId[i];
   1448   1.82      kent 			it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
   1449   1.82      kent 							       src_id);
   1450   1.82      kent 			it->inputs_size++;
   1451   1.82      kent 		}
   1452   1.82      kent 		return uaudio_merge_terminal_list(it);
   1453   1.82      kent 	case UDESCSUB_AC_SELECTOR:
   1454   1.82      kent 		it->inputs_size = 0;
   1455   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *)
   1456   1.82      kent 				    * it->d.su->bNrInPins, M_TEMP, M_NOWAIT);
   1457   1.82      kent 		if (it->inputs == NULL) {
   1458  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1459   1.82      kent 			return NULL;
   1460   1.82      kent 		}
   1461   1.82      kent 		for (i = 0; i < it->d.su->bNrInPins; i++) {
   1462   1.82      kent 			src_id = it->d.su->baSourceId[i];
   1463   1.82      kent 			it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
   1464   1.82      kent 							       src_id);
   1465   1.82      kent 			it->inputs_size++;
   1466   1.82      kent 		}
   1467   1.82      kent 		return uaudio_merge_terminal_list(it);
   1468   1.82      kent 	case UDESCSUB_AC_PROCESSING:
   1469   1.82      kent 		it->inputs_size = 0;
   1470   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *)
   1471   1.82      kent 				    * it->d.pu->bNrInPins, M_TEMP, M_NOWAIT);
   1472   1.82      kent 		if (it->inputs == NULL) {
   1473  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1474   1.82      kent 			return NULL;
   1475   1.82      kent 		}
   1476   1.82      kent 		for (i = 0; i < it->d.pu->bNrInPins; i++) {
   1477   1.82      kent 			src_id = it->d.pu->baSourceId[i];
   1478   1.82      kent 			it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
   1479   1.82      kent 							       src_id);
   1480   1.82      kent 			it->inputs_size++;
   1481   1.82      kent 		}
   1482   1.82      kent 		return uaudio_merge_terminal_list(it);
   1483   1.82      kent 	case UDESCSUB_AC_EXTENSION:
   1484   1.82      kent 		it->inputs_size = 0;
   1485   1.82      kent 		it->inputs = malloc(sizeof(struct terminal_list *)
   1486   1.82      kent 				    * it->d.eu->bNrInPins, M_TEMP, M_NOWAIT);
   1487   1.82      kent 		if (it->inputs == NULL) {
   1488  1.113      cube 			aprint_error("uaudio_io_terminaltype: no memory\n");
   1489   1.82      kent 			return NULL;
   1490   1.82      kent 		}
   1491   1.82      kent 		for (i = 0; i < it->d.eu->bNrInPins; i++) {
   1492   1.82      kent 			src_id = it->d.eu->baSourceId[i];
   1493   1.82      kent 			it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
   1494   1.82      kent 							       src_id);
   1495   1.82      kent 			it->inputs_size++;
   1496   1.82      kent 		}
   1497   1.82      kent 		return uaudio_merge_terminal_list(it);
   1498   1.82      kent 	case UDESCSUB_AC_HEADER:
   1499   1.82      kent 	default:
   1500   1.82      kent 		return NULL;
   1501   1.82      kent 	}
   1502   1.82      kent }
   1503   1.82      kent 
   1504   1.86      kent Static usbd_status
   1505   1.78      kent uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
   1506    1.1  augustss {
   1507    1.9  augustss 	usbd_status err;
   1508    1.1  augustss 
   1509    1.9  augustss 	err = uaudio_identify_ac(sc, cdesc);
   1510    1.9  augustss 	if (err)
   1511   1.94      kent 		return err;
   1512   1.94      kent 	return uaudio_identify_as(sc, cdesc);
   1513    1.1  augustss }
   1514    1.1  augustss 
   1515   1.86      kent Static void
   1516   1.78      kent uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai)
   1517    1.1  augustss {
   1518   1.84      kent 	size_t len;
   1519   1.84      kent 	struct as_info *nai;
   1520   1.42  christos 
   1521   1.84      kent 	len = sizeof(*ai) * (sc->sc_nalts + 1);
   1522   1.84      kent 	nai = malloc(len, M_USBDEV, M_NOWAIT);
   1523   1.42  christos 	if (nai == NULL) {
   1524  1.113      cube 		aprint_error("uaudio_add_alt: no memory\n");
   1525    1.1  augustss 		return;
   1526    1.1  augustss 	}
   1527   1.84      kent 	/* Copy old data, if there was any */
   1528   1.84      kent 	if (sc->sc_nalts != 0) {
   1529   1.84      kent 		memcpy(nai, sc->sc_alts, sizeof(*ai) * (sc->sc_nalts));
   1530   1.84      kent 		free(sc->sc_alts, M_USBDEV);
   1531   1.84      kent 	}
   1532   1.42  christos 	sc->sc_alts = nai;
   1533    1.1  augustss 	DPRINTFN(2,("uaudio_add_alt: adding alt=%d, enc=%d\n",
   1534    1.1  augustss 		    ai->alt, ai->encoding));
   1535    1.1  augustss 	sc->sc_alts[sc->sc_nalts++] = *ai;
   1536    1.1  augustss }
   1537    1.1  augustss 
   1538   1.86      kent Static usbd_status
   1539   1.99  christos uaudio_process_as(struct uaudio_softc *sc, const char *tbuf, int *offsp,
   1540   1.78      kent 		  int size, const usb_interface_descriptor_t *id)
   1541    1.1  augustss #define offs (*offsp)
   1542    1.1  augustss {
   1543   1.78      kent 	const struct usb_audio_streaming_interface_descriptor *asid;
   1544   1.78      kent 	const struct usb_audio_streaming_type1_descriptor *asf1d;
   1545   1.78      kent 	const usb_endpoint_descriptor_audio_t *ed;
   1546   1.80      kent 	const usb_endpoint_descriptor_audio_t *epdesc1;
   1547   1.78      kent 	const struct usb_audio_streaming_endpoint_descriptor *sed;
   1548    1.1  augustss 	int format, chan, prec, enc;
   1549   1.80      kent 	int dir, type, sync;
   1550    1.1  augustss 	struct as_info ai;
   1551   1.78      kent 	const char *format_str;
   1552    1.1  augustss 
   1553   1.99  christos 	asid = (const void *)(tbuf + offs);
   1554    1.1  augustss 	if (asid->bDescriptorType != UDESC_CS_INTERFACE ||
   1555    1.1  augustss 	    asid->bDescriptorSubtype != AS_GENERAL)
   1556   1.94      kent 		return USBD_INVAL;
   1557   1.78      kent 	DPRINTF(("uaudio_process_as: asid: bTerminakLink=%d wFormatTag=%d\n",
   1558   1.78      kent 		 asid->bTerminalLink, UGETW(asid->wFormatTag)));
   1559    1.1  augustss 	offs += asid->bLength;
   1560    1.1  augustss 	if (offs > size)
   1561   1.94      kent 		return USBD_INVAL;
   1562   1.78      kent 
   1563   1.99  christos 	asf1d = (const void *)(tbuf + offs);
   1564    1.1  augustss 	if (asf1d->bDescriptorType != UDESC_CS_INTERFACE ||
   1565    1.1  augustss 	    asf1d->bDescriptorSubtype != FORMAT_TYPE)
   1566   1.94      kent 		return USBD_INVAL;
   1567    1.1  augustss 	offs += asf1d->bLength;
   1568    1.1  augustss 	if (offs > size)
   1569   1.94      kent 		return USBD_INVAL;
   1570    1.1  augustss 
   1571    1.3  augustss 	if (asf1d->bFormatType != FORMAT_TYPE_I) {
   1572  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1573  1.113      cube 		    "ignored setting with type %d format\n", UGETW(asid->wFormatTag));
   1574   1.94      kent 		return USBD_NORMAL_COMPLETION;
   1575    1.3  augustss 	}
   1576    1.3  augustss 
   1577   1.99  christos 	ed = (const void *)(tbuf + offs);
   1578    1.1  augustss 	if (ed->bDescriptorType != UDESC_ENDPOINT)
   1579   1.94      kent 		return USBD_INVAL;
   1580   1.80      kent 	DPRINTF(("uaudio_process_as: endpoint[0] bLength=%d bDescriptorType=%d "
   1581    1.5  augustss 		 "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d "
   1582    1.5  augustss 		 "bInterval=%d bRefresh=%d bSynchAddress=%d\n",
   1583    1.5  augustss 		 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress,
   1584    1.5  augustss 		 ed->bmAttributes, UGETW(ed->wMaxPacketSize),
   1585    1.5  augustss 		 ed->bInterval, ed->bRefresh, ed->bSynchAddress));
   1586    1.1  augustss 	offs += ed->bLength;
   1587    1.1  augustss 	if (offs > size)
   1588   1.94      kent 		return USBD_INVAL;
   1589    1.5  augustss 	if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
   1590   1.94      kent 		return USBD_INVAL;
   1591    1.5  augustss 
   1592    1.5  augustss 	dir = UE_GET_DIR(ed->bEndpointAddress);
   1593    1.5  augustss 	type = UE_GET_ISO_TYPE(ed->bmAttributes);
   1594   1.31  augustss 	if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) &&
   1595   1.31  augustss 	    dir == UE_DIR_IN && type == UE_ISO_ADAPT)
   1596   1.31  augustss 		type = UE_ISO_ASYNC;
   1597   1.31  augustss 
   1598   1.40  augustss 	/* We can't handle endpoints that need a sync pipe yet. */
   1599   1.80      kent 	sync = FALSE;
   1600   1.78      kent 	if (dir == UE_DIR_IN && type == UE_ISO_ADAPT) {
   1601   1.80      kent 		sync = TRUE;
   1602   1.80      kent #ifndef UAUDIO_MULTIPLE_ENDPOINTS
   1603  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1604  1.113      cube 		    "ignored input endpoint of type adaptive\n");
   1605   1.94      kent 		return USBD_NORMAL_COMPLETION;
   1606   1.80      kent #endif
   1607   1.78      kent 	}
   1608   1.78      kent 	if (dir != UE_DIR_IN && type == UE_ISO_ASYNC) {
   1609   1.80      kent 		sync = TRUE;
   1610   1.80      kent #ifndef UAUDIO_MULTIPLE_ENDPOINTS
   1611  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1612  1.113      cube 		    "ignored output endpoint of type async\n");
   1613   1.94      kent 		return USBD_NORMAL_COMPLETION;
   1614   1.80      kent #endif
   1615    1.1  augustss 	}
   1616   1.56      kent 
   1617   1.99  christos 	sed = (const void *)(tbuf + offs);
   1618    1.1  augustss 	if (sed->bDescriptorType != UDESC_CS_ENDPOINT ||
   1619    1.1  augustss 	    sed->bDescriptorSubtype != AS_GENERAL)
   1620   1.94      kent 		return USBD_INVAL;
   1621   1.80      kent 	DPRINTF((" streadming_endpoint: offset=%d bLength=%d\n", offs, sed->bLength));
   1622    1.1  augustss 	offs += sed->bLength;
   1623    1.1  augustss 	if (offs > size)
   1624   1.94      kent 		return USBD_INVAL;
   1625   1.56      kent 
   1626  1.102  christos #ifdef UAUDIO_MULTIPLE_ENDPOINTS
   1627   1.80      kent 	if (sync && id->bNumEndpoints <= 1) {
   1628  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1629  1.113      cube 		    "a sync-pipe endpoint but no other endpoint\n");
   1630   1.80      kent 		return USBD_INVAL;
   1631   1.80      kent 	}
   1632  1.102  christos #endif
   1633   1.80      kent 	if (!sync && id->bNumEndpoints > 1) {
   1634  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1635  1.113      cube 		    "non sync-pipe endpoint but multiple endpoints\n");
   1636   1.80      kent 		return USBD_INVAL;
   1637   1.80      kent 	}
   1638   1.80      kent 	epdesc1 = NULL;
   1639   1.80      kent 	if (id->bNumEndpoints > 1) {
   1640   1.99  christos 		epdesc1 = (const void*)(tbuf + offs);
   1641   1.80      kent 		if (epdesc1->bDescriptorType != UDESC_ENDPOINT)
   1642   1.80      kent 			return USBD_INVAL;
   1643   1.80      kent 		DPRINTF(("uaudio_process_as: endpoint[1] bLength=%d "
   1644   1.80      kent 			 "bDescriptorType=%d bEndpointAddress=%d "
   1645   1.80      kent 			 "bmAttributes=0x%x wMaxPacketSize=%d bInterval=%d "
   1646   1.80      kent 			 "bRefresh=%d bSynchAddress=%d\n",
   1647   1.80      kent 			 epdesc1->bLength, epdesc1->bDescriptorType,
   1648   1.80      kent 			 epdesc1->bEndpointAddress, epdesc1->bmAttributes,
   1649   1.80      kent 			 UGETW(epdesc1->wMaxPacketSize), epdesc1->bInterval,
   1650   1.80      kent 			 epdesc1->bRefresh, epdesc1->bSynchAddress));
   1651   1.80      kent 		offs += epdesc1->bLength;
   1652   1.80      kent 		if (offs > size)
   1653   1.80      kent 			return USBD_INVAL;
   1654   1.80      kent 		if (epdesc1->bSynchAddress != 0) {
   1655  1.113      cube 			aprint_error_dev(sc->sc_dev,
   1656  1.113      cube 			    "invalid endpoint: bSynchAddress=0\n");
   1657   1.80      kent 			return USBD_INVAL;
   1658   1.80      kent 		}
   1659   1.80      kent 		if (UE_GET_XFERTYPE(epdesc1->bmAttributes) != UE_ISOCHRONOUS) {
   1660  1.113      cube 			aprint_error_dev(sc->sc_dev,
   1661  1.113      cube 			    "invalid endpoint: bmAttributes=0x%x\n",
   1662  1.113      cube 			     epdesc1->bmAttributes);
   1663   1.80      kent 			return USBD_INVAL;
   1664   1.80      kent 		}
   1665   1.80      kent 		if (epdesc1->bEndpointAddress != ed->bSynchAddress) {
   1666  1.113      cube 			aprint_error_dev(sc->sc_dev,
   1667  1.113      cube 			    "invalid endpoint addresses: "
   1668  1.113      cube 			    "ep[0]->bSynchAddress=0x%x "
   1669  1.113      cube 			    "ep[1]->bEndpointAddress=0x%x\n",
   1670  1.113      cube 			    ed->bSynchAddress, epdesc1->bEndpointAddress);
   1671   1.80      kent 			return USBD_INVAL;
   1672   1.80      kent 		}
   1673   1.80      kent 		/* UE_GET_ADDR(epdesc1->bEndpointAddress), and epdesc1->bRefresh */
   1674   1.80      kent 	}
   1675   1.80      kent 
   1676    1.3  augustss 	format = UGETW(asid->wFormatTag);
   1677    1.1  augustss 	chan = asf1d->bNrChannels;
   1678    1.1  augustss 	prec = asf1d->bBitResolution;
   1679   1.51      kent 	if (prec != 8 && prec != 16 && prec != 24) {
   1680  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1681  1.113      cube 		    "ignored setting with precision %d\n", prec);
   1682   1.94      kent 		return USBD_NORMAL_COMPLETION;
   1683    1.1  augustss 	}
   1684    1.1  augustss 	switch (format) {
   1685    1.3  augustss 	case UA_FMT_PCM:
   1686   1.51      kent 		if (prec == 8) {
   1687   1.51      kent 			sc->sc_altflags |= HAS_8;
   1688   1.51      kent 		} else if (prec == 16) {
   1689   1.51      kent 			sc->sc_altflags |= HAS_16;
   1690   1.51      kent 		} else if (prec == 24) {
   1691   1.51      kent 			sc->sc_altflags |= HAS_24;
   1692   1.51      kent 		}
   1693    1.5  augustss 		enc = AUDIO_ENCODING_SLINEAR_LE;
   1694   1.78      kent 		format_str = "pcm";
   1695    1.1  augustss 		break;
   1696    1.3  augustss 	case UA_FMT_PCM8:
   1697    1.5  augustss 		enc = AUDIO_ENCODING_ULINEAR_LE;
   1698    1.1  augustss 		sc->sc_altflags |= HAS_8U;
   1699   1.78      kent 		format_str = "pcm8";
   1700    1.1  augustss 		break;
   1701    1.3  augustss 	case UA_FMT_ALAW:
   1702    1.5  augustss 		enc = AUDIO_ENCODING_ALAW;
   1703    1.1  augustss 		sc->sc_altflags |= HAS_ALAW;
   1704   1.78      kent 		format_str = "alaw";
   1705    1.1  augustss 		break;
   1706    1.3  augustss 	case UA_FMT_MULAW:
   1707    1.5  augustss 		enc = AUDIO_ENCODING_ULAW;
   1708    1.1  augustss 		sc->sc_altflags |= HAS_MULAW;
   1709   1.78      kent 		format_str = "mulaw";
   1710    1.1  augustss 		break;
   1711   1.80      kent 	case UA_FMT_IEEE_FLOAT:
   1712    1.1  augustss 	default:
   1713  1.113      cube 		aprint_error_dev(sc->sc_dev,
   1714  1.113      cube 		    "ignored setting with format %d\n", format);
   1715   1.94      kent 		return USBD_NORMAL_COMPLETION;
   1716    1.1  augustss 	}
   1717   1.78      kent #ifdef UAUDIO_DEBUG
   1718  1.113      cube 	aprint_debug_dev(sc->sc_dev, "%s: %dch, %d/%dbit, %s,",
   1719   1.78      kent 	       dir == UE_DIR_IN ? "recording" : "playback",
   1720   1.78      kent 	       chan, prec, asf1d->bSubFrameSize * 8, format_str);
   1721   1.78      kent 	if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
   1722  1.113      cube 		aprint_debug(" %d-%dHz\n", UA_SAMP_LO(asf1d),
   1723  1.113      cube 		    UA_SAMP_HI(asf1d));
   1724   1.78      kent 	} else {
   1725   1.78      kent 		int r;
   1726  1.113      cube 		aprint_debug(" %d", UA_GETSAMP(asf1d, 0));
   1727   1.78      kent 		for (r = 1; r < asf1d->bSamFreqType; r++)
   1728  1.113      cube 			aprint_debug(",%d", UA_GETSAMP(asf1d, r));
   1729  1.113      cube 		aprint_debug("Hz\n");
   1730   1.78      kent 	}
   1731   1.78      kent #endif
   1732    1.1  augustss 	ai.alt = id->bAlternateSetting;
   1733    1.1  augustss 	ai.encoding = enc;
   1734   1.50      kent 	ai.attributes = sed->bmAttributes;
   1735    1.1  augustss 	ai.idesc = id;
   1736    1.1  augustss 	ai.edesc = ed;
   1737   1.80      kent 	ai.edesc1 = epdesc1;
   1738    1.1  augustss 	ai.asf1desc = asf1d;
   1739   1.50      kent 	ai.sc_busy = 0;
   1740  1.103       mrg 	ai.aformat = NULL;
   1741  1.103       mrg 	ai.ifaceh = NULL;
   1742    1.1  augustss 	uaudio_add_alt(sc, &ai);
   1743   1.50      kent #ifdef UAUDIO_DEBUG
   1744   1.78      kent 	if (ai.attributes & UA_SED_FREQ_CONTROL)
   1745   1.78      kent 		DPRINTFN(1, ("uaudio_process_as:  FREQ_CONTROL\n"));
   1746   1.78      kent 	if (ai.attributes & UA_SED_PITCH_CONTROL)
   1747   1.78      kent 		DPRINTFN(1, ("uaudio_process_as:  PITCH_CONTROL\n"));
   1748   1.50      kent #endif
   1749   1.47  jdolecek 	sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD;
   1750   1.47  jdolecek 
   1751   1.94      kent 	return USBD_NORMAL_COMPLETION;
   1752    1.1  augustss }
   1753    1.1  augustss #undef offs
   1754   1.56      kent 
   1755   1.86      kent Static usbd_status
   1756   1.78      kent uaudio_identify_as(struct uaudio_softc *sc,
   1757   1.78      kent 		   const usb_config_descriptor_t *cdesc)
   1758    1.1  augustss {
   1759   1.78      kent 	const usb_interface_descriptor_t *id;
   1760   1.99  christos 	const char *tbuf;
   1761   1.92      kent 	struct audio_format *auf;
   1762   1.92      kent 	const struct usb_audio_streaming_type1_descriptor *t1desc;
   1763    1.1  augustss 	int size, offs;
   1764   1.92      kent 	int i, j;
   1765    1.1  augustss 
   1766    1.1  augustss 	size = UGETW(cdesc->wTotalLength);
   1767   1.99  christos 	tbuf = (const char *)cdesc;
   1768    1.1  augustss 
   1769    1.1  augustss 	/* Locate the AudioStreaming interface descriptor. */
   1770    1.1  augustss 	offs = 0;
   1771   1.99  christos 	id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOSTREAM);
   1772    1.9  augustss 	if (id == NULL)
   1773   1.94      kent 		return USBD_INVAL;
   1774    1.1  augustss 
   1775    1.1  augustss 	/* Loop through all the alternate settings. */
   1776    1.1  augustss 	while (offs <= size) {
   1777   1.80      kent 		DPRINTFN(2, ("uaudio_identify: interface=%d offset=%d\n",
   1778   1.80      kent 		    id->bInterfaceNumber, offs));
   1779    1.1  augustss 		switch (id->bNumEndpoints) {
   1780    1.1  augustss 		case 0:
   1781    1.1  augustss 			DPRINTFN(2, ("uaudio_identify: AS null alt=%d\n",
   1782    1.1  augustss 				     id->bAlternateSetting));
   1783    1.1  augustss 			sc->sc_nullalt = id->bAlternateSetting;
   1784    1.1  augustss 			break;
   1785    1.1  augustss 		case 1:
   1786   1.80      kent #ifdef UAUDIO_MULTIPLE_ENDPOINTS
   1787   1.80      kent 		case 2:
   1788   1.80      kent #endif
   1789   1.99  christos 			uaudio_process_as(sc, tbuf, &offs, size, id);
   1790    1.1  augustss 			break;
   1791    1.1  augustss 		default:
   1792  1.113      cube 			aprint_error_dev(sc->sc_dev,
   1793  1.113      cube 			    "ignored audio interface with %d endpoints\n",
   1794  1.113      cube 			     id->bNumEndpoints);
   1795    1.1  augustss 			break;
   1796    1.1  augustss 		}
   1797   1.99  christos 		id = uaudio_find_iface(tbuf, size, &offs,UISUBCLASS_AUDIOSTREAM);
   1798    1.9  augustss 		if (id == NULL)
   1799    1.1  augustss 			break;
   1800    1.1  augustss 	}
   1801    1.1  augustss 	if (offs > size)
   1802   1.94      kent 		return USBD_INVAL;
   1803    1.1  augustss 	DPRINTF(("uaudio_identify_as: %d alts available\n", sc->sc_nalts));
   1804   1.47  jdolecek 
   1805   1.74   mycroft 	if (sc->sc_mode == 0) {
   1806  1.113      cube 		aprint_error_dev(sc->sc_dev, "no usable endpoint found\n");
   1807   1.94      kent 		return USBD_INVAL;
   1808    1.5  augustss 	}
   1809   1.47  jdolecek 
   1810   1.92      kent 	/* build audio_format array */
   1811   1.92      kent 	sc->sc_formats = malloc(sizeof(struct audio_format) * sc->sc_nalts,
   1812   1.92      kent 				M_USBDEV, M_NOWAIT);
   1813   1.92      kent 	if (sc->sc_formats == NULL)
   1814   1.92      kent 		return USBD_NOMEM;
   1815   1.92      kent 	sc->sc_nformats = sc->sc_nalts;
   1816   1.92      kent 	for (i = 0; i < sc->sc_nalts; i++) {
   1817   1.92      kent 		auf = &sc->sc_formats[i];
   1818   1.92      kent 		t1desc = sc->sc_alts[i].asf1desc;
   1819   1.92      kent 		auf->driver_data = NULL;
   1820   1.92      kent 		if (UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress) == UE_DIR_OUT)
   1821   1.92      kent 			auf->mode = AUMODE_PLAY;
   1822   1.92      kent 		else
   1823   1.92      kent 			auf->mode = AUMODE_RECORD;
   1824   1.92      kent 		auf->encoding = sc->sc_alts[i].encoding;
   1825   1.93      kent 		auf->validbits = t1desc->bBitResolution;
   1826   1.93      kent 		auf->precision = t1desc->bSubFrameSize * 8;
   1827   1.92      kent 		auf->channels = t1desc->bNrChannels;
   1828   1.92      kent 		auf->channel_mask = sc->sc_channel_config;
   1829   1.92      kent 		auf->frequency_type = t1desc->bSamFreqType;
   1830   1.92      kent 		if (t1desc->bSamFreqType == UA_SAMP_CONTNUOUS) {
   1831   1.92      kent 			auf->frequency[0] = UA_SAMP_LO(t1desc);
   1832   1.92      kent 			auf->frequency[1] = UA_SAMP_HI(t1desc);
   1833   1.92      kent 		} else {
   1834   1.92      kent 			for (j = 0; j  < t1desc->bSamFreqType; j++) {
   1835   1.92      kent 				if (j >= AUFMT_MAX_FREQUENCIES) {
   1836  1.113      cube 					aprint_error("%s: please increase "
   1837   1.92      kent 					       "AUFMT_MAX_FREQUENCIES to %d\n",
   1838   1.92      kent 					       __func__, t1desc->bSamFreqType);
   1839   1.92      kent 					break;
   1840   1.92      kent 				}
   1841   1.92      kent 				auf->frequency[j] = UA_GETSAMP(t1desc, j);
   1842   1.92      kent 			}
   1843   1.92      kent 		}
   1844   1.92      kent 		sc->sc_alts[i].aformat = auf;
   1845   1.92      kent 	}
   1846   1.92      kent 
   1847   1.92      kent 	if (0 != auconv_create_encodings(sc->sc_formats, sc->sc_nformats,
   1848   1.92      kent 					 &sc->sc_encodings)) {
   1849   1.92      kent 		free(sc->sc_formats, M_DEVBUF);
   1850   1.92      kent 		sc->sc_formats = NULL;
   1851   1.92      kent 		return ENOMEM;
   1852   1.92      kent 	}
   1853   1.92      kent 
   1854   1.94      kent 	return USBD_NORMAL_COMPLETION;
   1855    1.1  augustss }
   1856    1.1  augustss 
   1857   1.86      kent Static usbd_status
   1858   1.78      kent uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
   1859    1.1  augustss {
   1860   1.82      kent 	struct io_terminal* iot;
   1861   1.78      kent 	const usb_interface_descriptor_t *id;
   1862   1.78      kent 	const struct usb_audio_control_descriptor *acdp;
   1863  1.109  drochner 	const uaudio_cs_descriptor_t *dp;
   1864   1.82      kent 	const struct usb_audio_output_terminal *pot;
   1865   1.82      kent 	struct terminal_list *tml;
   1866   1.99  christos 	const char *tbuf, *ibuf, *ibufend;
   1867  1.120  jakllsch 	int size, offs, ndps, i, j;
   1868    1.1  augustss 
   1869    1.1  augustss 	size = UGETW(cdesc->wTotalLength);
   1870   1.99  christos 	tbuf = (const char *)cdesc;
   1871    1.1  augustss 
   1872    1.1  augustss 	/* Locate the AudioControl interface descriptor. */
   1873    1.1  augustss 	offs = 0;
   1874   1.99  christos 	id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOCONTROL);
   1875   1.14  augustss 	if (id == NULL)
   1876   1.94      kent 		return USBD_INVAL;
   1877    1.1  augustss 	if (offs + sizeof *acdp > size)
   1878   1.94      kent 		return USBD_INVAL;
   1879    1.1  augustss 	sc->sc_ac_iface = id->bInterfaceNumber;
   1880   1.82      kent 	DPRINTFN(2,("uaudio_identify_ac: AC interface is %d\n", sc->sc_ac_iface));
   1881    1.1  augustss 
   1882    1.1  augustss 	/* A class-specific AC interface header should follow. */
   1883   1.99  christos 	ibuf = tbuf + offs;
   1884  1.120  jakllsch 	ibufend = tbuf + size;
   1885   1.78      kent 	acdp = (const struct usb_audio_control_descriptor *)ibuf;
   1886    1.1  augustss 	if (acdp->bDescriptorType != UDESC_CS_INTERFACE ||
   1887    1.1  augustss 	    acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)
   1888   1.94      kent 		return USBD_INVAL;
   1889    1.5  augustss 
   1890    1.1  augustss 	if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC) &&
   1891    1.1  augustss 	     UGETW(acdp->bcdADC) != UAUDIO_VERSION)
   1892   1.94      kent 		return USBD_INVAL;
   1893    1.1  augustss 
   1894    1.7  augustss 	sc->sc_audio_rev = UGETW(acdp->bcdADC);
   1895  1.120  jakllsch 	DPRINTFN(2,("uaudio_identify_ac: found AC header, vers=%03x\n",
   1896  1.120  jakllsch 		 sc->sc_audio_rev));
   1897    1.1  augustss 
   1898    1.1  augustss 	sc->sc_nullalt = -1;
   1899    1.1  augustss 
   1900    1.1  augustss 	/* Scan through all the AC specific descriptors */
   1901  1.109  drochner 	dp = (const uaudio_cs_descriptor_t *)ibuf;
   1902    1.1  augustss 	ndps = 0;
   1903   1.82      kent 	iot = malloc(sizeof(struct io_terminal) * 256, M_TEMP, M_NOWAIT | M_ZERO);
   1904   1.82      kent 	if (iot == NULL) {
   1905  1.113      cube 		aprint_error("%s: no memory\n", __func__);
   1906   1.82      kent 		return USBD_NOMEM;
   1907   1.82      kent 	}
   1908    1.1  augustss 	for (;;) {
   1909    1.1  augustss 		ibuf += dp->bLength;
   1910    1.1  augustss 		if (ibuf >= ibufend)
   1911    1.1  augustss 			break;
   1912  1.109  drochner 		dp = (const uaudio_cs_descriptor_t *)ibuf;
   1913   1.97    martin 		if (ibuf + dp->bLength > ibufend) {
   1914   1.97    martin 			free(iot, M_TEMP);
   1915   1.94      kent 			return USBD_INVAL;
   1916   1.97    martin 		}
   1917  1.120  jakllsch 		if (dp->bDescriptorType != UDESC_CS_INTERFACE)
   1918  1.120  jakllsch 			break;
   1919   1.82      kent 		i = ((const struct usb_audio_input_terminal *)dp)->bTerminalId;
   1920   1.82      kent 		iot[i].d.desc = dp;
   1921    1.1  augustss 		if (i > ndps)
   1922    1.1  augustss 			ndps = i;
   1923    1.1  augustss 	}
   1924    1.1  augustss 	ndps++;
   1925    1.1  augustss 
   1926   1.82      kent 	/* construct io_terminal */
   1927   1.82      kent 	for (i = 0; i < ndps; i++) {
   1928   1.82      kent 		dp = iot[i].d.desc;
   1929   1.82      kent 		if (dp == NULL)
   1930   1.82      kent 			continue;
   1931   1.82      kent 		if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT)
   1932   1.82      kent 			continue;
   1933   1.82      kent 		pot = iot[i].d.ot;
   1934   1.82      kent 		tml = uaudio_io_terminaltype(UGETW(pot->wTerminalType), iot, i);
   1935   1.82      kent 		if (tml != NULL)
   1936   1.82      kent 			free(tml, M_TEMP);
   1937   1.82      kent 	}
   1938   1.82      kent 
   1939   1.82      kent #ifdef UAUDIO_DEBUG
   1940   1.82      kent 	for (i = 0; i < 256; i++) {
   1941   1.91      kent 		struct usb_audio_cluster cluster;
   1942   1.91      kent 
   1943   1.82      kent 		if (iot[i].d.desc == NULL)
   1944   1.82      kent 			continue;
   1945  1.118    dyoung 		printf("id %d:\t", i);
   1946   1.82      kent 		switch (iot[i].d.desc->bDescriptorSubtype) {
   1947   1.82      kent 		case UDESCSUB_AC_INPUT:
   1948  1.118    dyoung 			printf("AC_INPUT type=%s\n", uaudio_get_terminal_name
   1949   1.89      kent 				  (UGETW(iot[i].d.it->wTerminalType)));
   1950  1.118    dyoung 			printf("\t");
   1951   1.91      kent 			cluster = uaudio_get_cluster(i, iot);
   1952   1.91      kent 			uaudio_dump_cluster(&cluster);
   1953  1.118    dyoung 			printf("\n");
   1954   1.82      kent 			break;
   1955   1.82      kent 		case UDESCSUB_AC_OUTPUT:
   1956  1.118    dyoung 			printf("AC_OUTPUT type=%s ", uaudio_get_terminal_name
   1957   1.89      kent 				  (UGETW(iot[i].d.ot->wTerminalType)));
   1958  1.118    dyoung 			printf("src=%d\n", iot[i].d.ot->bSourceId);
   1959   1.82      kent 			break;
   1960   1.82      kent 		case UDESCSUB_AC_MIXER:
   1961  1.118    dyoung 			printf("AC_MIXER src=");
   1962   1.82      kent 			for (j = 0; j < iot[i].d.mu->bNrInPins; j++)
   1963  1.118    dyoung 				printf("%d ", iot[i].d.mu->baSourceId[j]);
   1964  1.118    dyoung 			printf("\n\t");
   1965   1.91      kent 			cluster = uaudio_get_cluster(i, iot);
   1966   1.91      kent 			uaudio_dump_cluster(&cluster);
   1967  1.118    dyoung 			printf("\n");
   1968   1.82      kent 			break;
   1969   1.82      kent 		case UDESCSUB_AC_SELECTOR:
   1970  1.118    dyoung 			printf("AC_SELECTOR src=");
   1971   1.82      kent 			for (j = 0; j < iot[i].d.su->bNrInPins; j++)
   1972  1.118    dyoung 				printf("%d ", iot[i].d.su->baSourceId[j]);
   1973  1.118    dyoung 			printf("\n");
   1974   1.82      kent 			break;
   1975   1.82      kent 		case UDESCSUB_AC_FEATURE:
   1976  1.118    dyoung 			printf("AC_FEATURE src=%d\n", iot[i].d.fu->bSourceId);
   1977   1.82      kent 			break;
   1978   1.82      kent 		case UDESCSUB_AC_PROCESSING:
   1979  1.118    dyoung 			printf("AC_PROCESSING src=");
   1980   1.82      kent 			for (j = 0; j < iot[i].d.pu->bNrInPins; j++)
   1981  1.118    dyoung 				printf("%d ", iot[i].d.pu->baSourceId[j]);
   1982  1.118    dyoung 			printf("\n\t");
   1983   1.91      kent 			cluster = uaudio_get_cluster(i, iot);
   1984   1.91      kent 			uaudio_dump_cluster(&cluster);
   1985  1.118    dyoung 			printf("\n");
   1986   1.82      kent 			break;
   1987   1.82      kent 		case UDESCSUB_AC_EXTENSION:
   1988  1.118    dyoung 			printf("AC_EXTENSION src=");
   1989   1.82      kent 			for (j = 0; j < iot[i].d.eu->bNrInPins; j++)
   1990  1.118    dyoung 				printf("%d ", iot[i].d.eu->baSourceId[j]);
   1991  1.118    dyoung 			printf("\n\t");
   1992   1.91      kent 			cluster = uaudio_get_cluster(i, iot);
   1993   1.91      kent 			uaudio_dump_cluster(&cluster);
   1994  1.118    dyoung 			printf("\n");
   1995   1.82      kent 			break;
   1996   1.82      kent 		default:
   1997  1.118    dyoung 			printf("unknown audio control (subtype=%d)\n",
   1998   1.89      kent 				  iot[i].d.desc->bDescriptorSubtype);
   1999   1.82      kent 		}
   2000   1.82      kent 		for (j = 0; j < iot[i].inputs_size; j++) {
   2001   1.82      kent 			int k;
   2002  1.118    dyoung 			printf("\tinput%d: ", j);
   2003   1.82      kent 			tml = iot[i].inputs[j];
   2004   1.82      kent 			if (tml == NULL) {
   2005  1.118    dyoung 				printf("NULL\n");
   2006   1.82      kent 				continue;
   2007   1.82      kent 			}
   2008   1.82      kent 			for (k = 0; k < tml->size; k++)
   2009  1.118    dyoung 				printf("%s ", uaudio_get_terminal_name
   2010   1.89      kent 					  (tml->terminals[k]));
   2011  1.118    dyoung 			printf("\n");
   2012   1.82      kent 		}
   2013  1.118    dyoung 		printf("\toutput: ");
   2014   1.82      kent 		tml = iot[i].output;
   2015   1.82      kent 		for (j = 0; j < tml->size; j++)
   2016  1.118    dyoung 			printf("%s ", uaudio_get_terminal_name(tml->terminals[j]));
   2017  1.118    dyoung 		printf("\n");
   2018   1.82      kent 	}
   2019   1.82      kent #endif
   2020   1.82      kent 
   2021    1.1  augustss 	for (i = 0; i < ndps; i++) {
   2022   1.82      kent 		dp = iot[i].d.desc;
   2023   1.14  augustss 		if (dp == NULL)
   2024    1.1  augustss 			continue;
   2025   1.82      kent 		DPRINTF(("uaudio_identify_ac: id=%d subtype=%d\n",
   2026   1.82      kent 			 i, dp->bDescriptorSubtype));
   2027    1.1  augustss 		switch (dp->bDescriptorSubtype) {
   2028    1.1  augustss 		case UDESCSUB_AC_HEADER:
   2029  1.113      cube 			aprint_error("uaudio_identify_ac: unexpected AC header\n");
   2030    1.1  augustss 			break;
   2031    1.1  augustss 		case UDESCSUB_AC_INPUT:
   2032   1.82      kent 			uaudio_add_input(sc, iot, i);
   2033    1.1  augustss 			break;
   2034    1.1  augustss 		case UDESCSUB_AC_OUTPUT:
   2035   1.82      kent 			uaudio_add_output(sc, iot, i);
   2036    1.1  augustss 			break;
   2037    1.1  augustss 		case UDESCSUB_AC_MIXER:
   2038   1.82      kent 			uaudio_add_mixer(sc, iot, i);
   2039    1.1  augustss 			break;
   2040    1.1  augustss 		case UDESCSUB_AC_SELECTOR:
   2041   1.82      kent 			uaudio_add_selector(sc, iot, i);
   2042    1.1  augustss 			break;
   2043    1.1  augustss 		case UDESCSUB_AC_FEATURE:
   2044   1.82      kent 			uaudio_add_feature(sc, iot, i);
   2045    1.1  augustss 			break;
   2046    1.1  augustss 		case UDESCSUB_AC_PROCESSING:
   2047   1.82      kent 			uaudio_add_processing(sc, iot, i);
   2048    1.1  augustss 			break;
   2049    1.1  augustss 		case UDESCSUB_AC_EXTENSION:
   2050   1.82      kent 			uaudio_add_extension(sc, iot, i);
   2051    1.1  augustss 			break;
   2052    1.1  augustss 		default:
   2053  1.113      cube 			aprint_error(
   2054  1.113      cube 			    "uaudio_identify_ac: bad AC desc subtype=0x%02x\n",
   2055  1.113      cube 			    dp->bDescriptorSubtype);
   2056    1.1  augustss 			break;
   2057    1.1  augustss 		}
   2058    1.1  augustss 	}
   2059   1.82      kent 
   2060   1.82      kent 	/* delete io_terminal */
   2061   1.82      kent 	for (i = 0; i < 256; i++) {
   2062   1.82      kent 		if (iot[i].d.desc == NULL)
   2063   1.82      kent 			continue;
   2064   1.82      kent 		if (iot[i].inputs != NULL) {
   2065   1.82      kent 			for (j = 0; j < iot[i].inputs_size; j++) {
   2066   1.82      kent 				if (iot[i].inputs[j] != NULL)
   2067   1.82      kent 					free(iot[i].inputs[j], M_TEMP);
   2068   1.82      kent 			}
   2069   1.82      kent 			free(iot[i].inputs, M_TEMP);
   2070   1.82      kent 		}
   2071   1.82      kent 		if (iot[i].output != NULL)
   2072   1.82      kent 			free(iot[i].output, M_TEMP);
   2073   1.82      kent 		iot[i].d.desc = NULL;
   2074   1.82      kent 	}
   2075   1.82      kent 	free(iot, M_TEMP);
   2076   1.82      kent 
   2077   1.94      kent 	return USBD_NORMAL_COMPLETION;
   2078    1.1  augustss }
   2079    1.1  augustss 
   2080   1.86      kent Static int
   2081   1.26  augustss uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi)
   2082    1.1  augustss {
   2083   1.94      kent 	struct uaudio_softc *sc;
   2084    1.1  augustss 	struct mixerctl *mc;
   2085   1.79      kent 	int n, nctls, i;
   2086    1.1  augustss 
   2087    1.1  augustss 	DPRINTFN(2,("uaudio_query_devinfo: index=%d\n", mi->index));
   2088   1.94      kent 	sc = addr;
   2089    1.1  augustss 	if (sc->sc_dying)
   2090   1.94      kent 		return EIO;
   2091   1.56      kent 
   2092    1.1  augustss 	n = mi->index;
   2093    1.1  augustss 	nctls = sc->sc_nctls;
   2094    1.1  augustss 
   2095   1.63  augustss 	switch (n) {
   2096   1.63  augustss 	case UAC_OUTPUT:
   2097   1.63  augustss 		mi->type = AUDIO_MIXER_CLASS;
   2098   1.63  augustss 		mi->mixer_class = UAC_OUTPUT;
   2099   1.63  augustss 		mi->next = mi->prev = AUDIO_MIXER_LAST;
   2100   1.71    itojun 		strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name));
   2101   1.94      kent 		return 0;
   2102   1.63  augustss 	case UAC_INPUT:
   2103   1.63  augustss 		mi->type = AUDIO_MIXER_CLASS;
   2104   1.63  augustss 		mi->mixer_class = UAC_INPUT;
   2105   1.63  augustss 		mi->next = mi->prev = AUDIO_MIXER_LAST;
   2106   1.71    itojun 		strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name));
   2107   1.94      kent 		return 0;
   2108   1.63  augustss 	case UAC_EQUAL:
   2109   1.63  augustss 		mi->type = AUDIO_MIXER_CLASS;
   2110   1.63  augustss 		mi->mixer_class = UAC_EQUAL;
   2111   1.63  augustss 		mi->next = mi->prev = AUDIO_MIXER_LAST;
   2112   1.71    itojun 		strlcpy(mi->label.name, AudioCequalization,
   2113   1.71    itojun 		    sizeof(mi->label.name));
   2114   1.94      kent 		return 0;
   2115   1.82      kent 	case UAC_RECORD:
   2116   1.82      kent 		mi->type = AUDIO_MIXER_CLASS;
   2117   1.82      kent 		mi->mixer_class = UAC_RECORD;
   2118   1.82      kent 		mi->next = mi->prev = AUDIO_MIXER_LAST;
   2119   1.82      kent 		strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name));
   2120   1.82      kent 		return 0;
   2121   1.63  augustss 	default:
   2122   1.63  augustss 		break;
   2123    1.1  augustss 	}
   2124   1.63  augustss 
   2125   1.63  augustss 	n -= UAC_NCLASSES;
   2126   1.63  augustss 	if (n < 0 || n >= nctls)
   2127   1.94      kent 		return ENXIO;
   2128   1.63  augustss 
   2129    1.1  augustss 	mc = &sc->sc_ctls[n];
   2130   1.71    itojun 	strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name));
   2131    1.1  augustss 	mi->mixer_class = mc->class;
   2132    1.1  augustss 	mi->next = mi->prev = AUDIO_MIXER_LAST;	/* XXX */
   2133    1.1  augustss 	switch (mc->type) {
   2134    1.1  augustss 	case MIX_ON_OFF:
   2135    1.1  augustss 		mi->type = AUDIO_MIXER_ENUM;
   2136    1.1  augustss 		mi->un.e.num_mem = 2;
   2137   1.71    itojun 		strlcpy(mi->un.e.member[0].label.name, AudioNoff,
   2138   1.71    itojun 		    sizeof(mi->un.e.member[0].label.name));
   2139    1.1  augustss 		mi->un.e.member[0].ord = 0;
   2140   1.71    itojun 		strlcpy(mi->un.e.member[1].label.name, AudioNon,
   2141   1.71    itojun 		    sizeof(mi->un.e.member[1].label.name));
   2142    1.1  augustss 		mi->un.e.member[1].ord = 1;
   2143    1.1  augustss 		break;
   2144   1.79      kent 	case MIX_SELECTOR:
   2145   1.79      kent 		mi->type = AUDIO_MIXER_ENUM;
   2146   1.79      kent 		mi->un.e.num_mem = mc->maxval - mc->minval + 1;
   2147   1.79      kent 		for (i = 0; i <= mc->maxval - mc->minval; i++) {
   2148   1.79      kent 			snprintf(mi->un.e.member[i].label.name,
   2149   1.79      kent 				 sizeof(mi->un.e.member[i].label.name),
   2150   1.79      kent 				 "%d", i + mc->minval);
   2151   1.79      kent 			mi->un.e.member[i].ord = i + mc->minval;
   2152   1.79      kent 		}
   2153   1.79      kent 		break;
   2154    1.1  augustss 	default:
   2155    1.1  augustss 		mi->type = AUDIO_MIXER_VALUE;
   2156    1.1  augustss 		strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN);
   2157    1.1  augustss 		mi->un.v.num_channels = mc->nchan;
   2158   1.33  augustss 		mi->un.v.delta = mc->delta;
   2159    1.1  augustss 		break;
   2160    1.1  augustss 	}
   2161   1.94      kent 	return 0;
   2162    1.1  augustss }
   2163    1.1  augustss 
   2164   1.86      kent Static int
   2165   1.26  augustss uaudio_open(void *addr, int flags)
   2166    1.1  augustss {
   2167   1.94      kent 	struct uaudio_softc *sc;
   2168    1.1  augustss 
   2169   1.95   dsainty 	sc = addr;
   2170   1.56      kent 	DPRINTF(("uaudio_open: sc=%p\n", sc));
   2171    1.1  augustss 	if (sc->sc_dying)
   2172   1.94      kent 		return EIO;
   2173    1.1  augustss 
   2174   1.77   mycroft 	if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY))
   2175   1.94      kent 		return EACCES;
   2176   1.77   mycroft 	if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD))
   2177   1.94      kent 		return EACCES;
   2178    1.1  augustss 
   2179   1.94      kent 	return 0;
   2180    1.1  augustss }
   2181    1.1  augustss 
   2182    1.1  augustss /*
   2183    1.1  augustss  * Close function is called at splaudio().
   2184    1.1  augustss  */
   2185   1.86      kent Static void
   2186  1.107  christos uaudio_close(void *addr)
   2187    1.1  augustss {
   2188    1.1  augustss }
   2189    1.1  augustss 
   2190   1.86      kent Static int
   2191   1.26  augustss uaudio_drain(void *addr)
   2192    1.1  augustss {
   2193  1.121  jmcneill 	struct uaudio_softc *sc = addr;
   2194  1.121  jmcneill 
   2195  1.121  jmcneill 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2196    1.1  augustss 
   2197  1.121  jmcneill 	kpause("uaudiodr", false,
   2198  1.121  jmcneill 	    mstohz(UAUDIO_NCHANBUFS * UAUDIO_NFRAMES), &sc->sc_intr_lock);
   2199    1.1  augustss 
   2200   1.94      kent 	return 0;
   2201    1.1  augustss }
   2202    1.1  augustss 
   2203   1.86      kent Static int
   2204   1.26  augustss uaudio_halt_out_dma(void *addr)
   2205    1.1  augustss {
   2206  1.121  jmcneill 	struct uaudio_softc *sc = addr;
   2207    1.1  augustss 
   2208    1.1  augustss 	DPRINTF(("uaudio_halt_out_dma: enter\n"));
   2209  1.121  jmcneill 
   2210  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2211  1.121  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   2212   1.47  jdolecek 	if (sc->sc_playchan.pipe != NULL) {
   2213   1.47  jdolecek 		uaudio_chan_close(sc, &sc->sc_playchan);
   2214   1.47  jdolecek 		sc->sc_playchan.pipe = NULL;
   2215   1.47  jdolecek 		uaudio_chan_free_buffers(sc, &sc->sc_playchan);
   2216   1.74   mycroft 		sc->sc_playchan.intr = NULL;
   2217    1.1  augustss 	}
   2218  1.121  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   2219  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2220  1.121  jmcneill 
   2221   1.94      kent 	return 0;
   2222    1.1  augustss }
   2223    1.1  augustss 
   2224   1.86      kent Static int
   2225   1.26  augustss uaudio_halt_in_dma(void *addr)
   2226    1.1  augustss {
   2227  1.121  jmcneill 	struct uaudio_softc *sc = addr;
   2228    1.1  augustss 
   2229    1.1  augustss 	DPRINTF(("uaudio_halt_in_dma: enter\n"));
   2230  1.121  jmcneill 
   2231  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2232  1.121  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   2233   1.47  jdolecek 	if (sc->sc_recchan.pipe != NULL) {
   2234   1.47  jdolecek 		uaudio_chan_close(sc, &sc->sc_recchan);
   2235   1.47  jdolecek 		sc->sc_recchan.pipe = NULL;
   2236   1.47  jdolecek 		uaudio_chan_free_buffers(sc, &sc->sc_recchan);
   2237   1.74   mycroft 		sc->sc_recchan.intr = NULL;
   2238    1.5  augustss 	}
   2239  1.121  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   2240  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2241  1.121  jmcneill 
   2242   1.94      kent 	return 0;
   2243    1.1  augustss }
   2244    1.1  augustss 
   2245   1.86      kent Static int
   2246   1.26  augustss uaudio_getdev(void *addr, struct audio_device *retp)
   2247    1.1  augustss {
   2248   1.94      kent 	struct uaudio_softc *sc;
   2249    1.1  augustss 
   2250    1.1  augustss 	DPRINTF(("uaudio_mixer_getdev:\n"));
   2251   1.94      kent 	sc = addr;
   2252    1.1  augustss 	if (sc->sc_dying)
   2253   1.94      kent 		return EIO;
   2254   1.56      kent 
   2255  1.119  jmcneill 	*retp = sc->sc_adev;
   2256   1.94      kent 	return 0;
   2257    1.1  augustss }
   2258    1.1  augustss 
   2259    1.1  augustss /*
   2260    1.1  augustss  * Make sure the block size is large enough to hold all outstanding transfers.
   2261    1.1  augustss  */
   2262   1.86      kent Static int
   2263   1.93      kent uaudio_round_blocksize(void *addr, int blk,
   2264   1.93      kent 		       int mode, const audio_params_t *param)
   2265    1.1  augustss {
   2266   1.94      kent 	struct uaudio_softc *sc;
   2267   1.96      kent 	int b;
   2268    1.1  augustss 
   2269   1.94      kent 	sc = addr;
   2270   1.96      kent 	DPRINTF(("uaudio_round_blocksize: blk=%d mode=%s\n", blk,
   2271   1.96      kent 	    mode == AUMODE_PLAY ? "AUMODE_PLAY" : "AUMODE_RECORD"));
   2272   1.96      kent 
   2273   1.96      kent 	/* chan.bytes_per_frame can be 0. */
   2274   1.96      kent 	if (mode == AUMODE_PLAY || sc->sc_recchan.bytes_per_frame <= 0) {
   2275   1.96      kent 		b = param->sample_rate * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
   2276   1.96      kent 
   2277   1.96      kent 		/*
   2278   1.96      kent 		 * This does not make accurate value in the case
   2279   1.96      kent 		 * of b % USB_FRAMES_PER_SECOND != 0
   2280   1.96      kent 		 */
   2281   1.96      kent 		b /= USB_FRAMES_PER_SECOND;
   2282   1.96      kent 
   2283   1.96      kent 		b *= param->precision / 8 * param->channels;
   2284   1.47  jdolecek 	} else {
   2285   1.96      kent 		/*
   2286   1.96      kent 		 * use wMaxPacketSize in bytes_per_frame.
   2287   1.96      kent 		 * See uaudio_set_params() and uaudio_chan_init()
   2288   1.96      kent 		 */
   2289   1.96      kent 		b = sc->sc_recchan.bytes_per_frame
   2290   1.96      kent 		    * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
   2291   1.47  jdolecek 	}
   2292    1.1  augustss 
   2293   1.96      kent 	if (b <= 0)
   2294   1.96      kent 		b = 1;
   2295   1.96      kent 	blk = blk <= b ? b : blk / b * b;
   2296    1.1  augustss 
   2297    1.5  augustss #ifdef DIAGNOSTIC
   2298    1.5  augustss 	if (blk <= 0) {
   2299  1.113      cube 		aprint_debug("uaudio_round_blocksize: blk=%d\n", blk);
   2300    1.5  augustss 		blk = 512;
   2301    1.5  augustss 	}
   2302    1.5  augustss #endif
   2303    1.5  augustss 
   2304   1.96      kent 	DPRINTF(("uaudio_round_blocksize: resultant blk=%d\n", blk));
   2305   1.94      kent 	return blk;
   2306    1.1  augustss }
   2307    1.1  augustss 
   2308   1.86      kent Static int
   2309  1.107  christos uaudio_get_props(void *addr)
   2310    1.1  augustss {
   2311   1.94      kent 	return AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
   2312    1.1  augustss 
   2313    1.1  augustss }
   2314    1.1  augustss 
   2315  1.121  jmcneill Static void
   2316  1.121  jmcneill uaudio_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
   2317  1.121  jmcneill {
   2318  1.121  jmcneill 	struct uaudio_softc *sc;
   2319  1.121  jmcneill 
   2320  1.121  jmcneill 	sc = addr;
   2321  1.121  jmcneill 	*intr = &sc->sc_intr_lock;
   2322  1.121  jmcneill 	*thread = &sc->sc_lock;
   2323  1.121  jmcneill }
   2324  1.121  jmcneill 
   2325   1.86      kent Static int
   2326   1.27  augustss uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue,
   2327   1.26  augustss 	   int wIndex, int len)
   2328    1.1  augustss {
   2329    1.1  augustss 	usb_device_request_t req;
   2330    1.1  augustss 	u_int8_t data[4];
   2331    1.9  augustss 	usbd_status err;
   2332    1.1  augustss 	int val;
   2333    1.1  augustss 
   2334    1.1  augustss 	if (wValue == -1)
   2335   1.94      kent 		return 0;
   2336    1.1  augustss 
   2337    1.1  augustss 	req.bmRequestType = type;
   2338    1.1  augustss 	req.bRequest = which;
   2339    1.1  augustss 	USETW(req.wValue, wValue);
   2340    1.1  augustss 	USETW(req.wIndex, wIndex);
   2341    1.1  augustss 	USETW(req.wLength, len);
   2342    1.1  augustss 	DPRINTFN(2,("uaudio_get: type=0x%02x req=0x%02x wValue=0x%04x "
   2343   1.56      kent 		    "wIndex=0x%04x len=%d\n",
   2344    1.1  augustss 		    type, which, wValue, wIndex, len));
   2345   1.47  jdolecek 	err = usbd_do_request(sc->sc_udev, &req, data);
   2346    1.9  augustss 	if (err) {
   2347   1.20  augustss 		DPRINTF(("uaudio_get: err=%s\n", usbd_errstr(err)));
   2348   1.94      kent 		return -1;
   2349    1.1  augustss 	}
   2350    1.1  augustss 	switch (len) {
   2351    1.1  augustss 	case 1:
   2352    1.1  augustss 		val = data[0];
   2353    1.1  augustss 		break;
   2354    1.1  augustss 	case 2:
   2355    1.1  augustss 		val = data[0] | (data[1] << 8);
   2356    1.1  augustss 		break;
   2357    1.1  augustss 	default:
   2358    1.1  augustss 		DPRINTF(("uaudio_get: bad length=%d\n", len));
   2359   1.94      kent 		return -1;
   2360    1.1  augustss 	}
   2361    1.1  augustss 	DPRINTFN(2,("uaudio_get: val=%d\n", val));
   2362   1.94      kent 	return val;
   2363    1.1  augustss }
   2364    1.1  augustss 
   2365   1.86      kent Static void
   2366   1.27  augustss uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue,
   2367   1.26  augustss 	   int wIndex, int len, int val)
   2368    1.1  augustss {
   2369    1.1  augustss 	usb_device_request_t req;
   2370    1.1  augustss 	u_int8_t data[4];
   2371    1.9  augustss 	usbd_status err;
   2372    1.1  augustss 
   2373    1.1  augustss 	if (wValue == -1)
   2374    1.1  augustss 		return;
   2375    1.1  augustss 
   2376    1.1  augustss 	req.bmRequestType = type;
   2377    1.1  augustss 	req.bRequest = which;
   2378    1.1  augustss 	USETW(req.wValue, wValue);
   2379    1.1  augustss 	USETW(req.wIndex, wIndex);
   2380    1.1  augustss 	USETW(req.wLength, len);
   2381    1.1  augustss 	switch (len) {
   2382    1.1  augustss 	case 1:
   2383    1.1  augustss 		data[0] = val;
   2384    1.1  augustss 		break;
   2385    1.1  augustss 	case 2:
   2386    1.1  augustss 		data[0] = val;
   2387    1.1  augustss 		data[1] = val >> 8;
   2388    1.1  augustss 		break;
   2389    1.1  augustss 	default:
   2390    1.1  augustss 		return;
   2391    1.1  augustss 	}
   2392    1.1  augustss 	DPRINTFN(2,("uaudio_set: type=0x%02x req=0x%02x wValue=0x%04x "
   2393   1.56      kent 		    "wIndex=0x%04x len=%d, val=%d\n",
   2394    1.1  augustss 		    type, which, wValue, wIndex, len, val & 0xffff));
   2395   1.47  jdolecek 	err = usbd_do_request(sc->sc_udev, &req, data);
   2396    1.4  augustss #ifdef UAUDIO_DEBUG
   2397   1.12  augustss 	if (err)
   2398    1.9  augustss 		DPRINTF(("uaudio_set: err=%d\n", err));
   2399    1.1  augustss #endif
   2400    1.1  augustss }
   2401    1.1  augustss 
   2402   1.86      kent Static int
   2403   1.26  augustss uaudio_signext(int type, int val)
   2404    1.1  augustss {
   2405    1.1  augustss 	if (!MIX_UNSIGNED(type)) {
   2406    1.1  augustss 		if (MIX_SIZE(type) == 2)
   2407    1.1  augustss 			val = (int16_t)val;
   2408    1.1  augustss 		else
   2409    1.1  augustss 			val = (int8_t)val;
   2410    1.1  augustss 	}
   2411   1.94      kent 	return val;
   2412    1.1  augustss }
   2413    1.1  augustss 
   2414   1.86      kent Static int
   2415   1.26  augustss uaudio_value2bsd(struct mixerctl *mc, int val)
   2416    1.1  augustss {
   2417    1.1  augustss 	DPRINTFN(5, ("uaudio_value2bsd: type=%03x val=%d min=%d max=%d ",
   2418    1.1  augustss 		     mc->type, val, mc->minval, mc->maxval));
   2419   1.79      kent 	if (mc->type == MIX_ON_OFF) {
   2420   1.47  jdolecek 		val = (val != 0);
   2421   1.79      kent 	} else if (mc->type == MIX_SELECTOR) {
   2422   1.79      kent 		if (val < mc->minval || val > mc->maxval)
   2423   1.79      kent 			val = mc->minval;
   2424   1.79      kent 	} else
   2425   1.69       wiz 		val = ((uaudio_signext(mc->type, val) - mc->minval) * 255
   2426   1.34  augustss 			+ mc->mul/2) / mc->mul;
   2427    1.1  augustss 	DPRINTFN(5, ("val'=%d\n", val));
   2428   1.94      kent 	return val;
   2429    1.1  augustss }
   2430    1.1  augustss 
   2431    1.1  augustss int
   2432   1.26  augustss uaudio_bsd2value(struct mixerctl *mc, int val)
   2433    1.1  augustss {
   2434    1.1  augustss 	DPRINTFN(5,("uaudio_bsd2value: type=%03x val=%d min=%d max=%d ",
   2435    1.1  augustss 		    mc->type, val, mc->minval, mc->maxval));
   2436   1.79      kent 	if (mc->type == MIX_ON_OFF) {
   2437   1.47  jdolecek 		val = (val != 0);
   2438   1.79      kent 	} else if (mc->type == MIX_SELECTOR) {
   2439   1.79      kent 		if (val < mc->minval || val > mc->maxval)
   2440   1.79      kent 			val = mc->minval;
   2441   1.79      kent 	} else
   2442   1.69       wiz 		val = (val + mc->delta/2) * mc->mul / 255 + mc->minval;
   2443    1.1  augustss 	DPRINTFN(5, ("val'=%d\n", val));
   2444   1.94      kent 	return val;
   2445    1.1  augustss }
   2446    1.1  augustss 
   2447   1.86      kent Static int
   2448   1.56      kent uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc,
   2449   1.26  augustss 	       int chan)
   2450    1.1  augustss {
   2451    1.1  augustss 	int val;
   2452    1.1  augustss 
   2453    1.1  augustss 	DPRINTFN(5,("uaudio_ctl_get: which=%d chan=%d\n", which, chan));
   2454  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2455    1.1  augustss 	val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan],
   2456    1.1  augustss 			 mc->wIndex, MIX_SIZE(mc->type));
   2457  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2458   1.94      kent 	return uaudio_value2bsd(mc, val);
   2459    1.1  augustss }
   2460    1.1  augustss 
   2461   1.86      kent Static void
   2462   1.26  augustss uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc,
   2463   1.26  augustss 	       int chan, int val)
   2464    1.1  augustss {
   2465    1.1  augustss 	val = uaudio_bsd2value(mc, val);
   2466  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2467    1.1  augustss 	uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan],
   2468    1.1  augustss 		   mc->wIndex, MIX_SIZE(mc->type), val);
   2469  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2470    1.1  augustss }
   2471    1.1  augustss 
   2472   1.86      kent Static int
   2473   1.26  augustss uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   2474    1.1  augustss {
   2475   1.94      kent 	struct uaudio_softc *sc;
   2476    1.1  augustss 	struct mixerctl *mc;
   2477    1.1  augustss 	int i, n, vals[MIX_MAX_CHAN], val;
   2478    1.1  augustss 
   2479    1.1  augustss 	DPRINTFN(2,("uaudio_mixer_get_port: index=%d\n", cp->dev));
   2480   1.94      kent 	sc = addr;
   2481    1.1  augustss 	if (sc->sc_dying)
   2482   1.94      kent 		return EIO;
   2483   1.56      kent 
   2484   1.63  augustss 	n = cp->dev - UAC_NCLASSES;
   2485    1.1  augustss 	if (n < 0 || n >= sc->sc_nctls)
   2486   1.94      kent 		return ENXIO;
   2487    1.1  augustss 	mc = &sc->sc_ctls[n];
   2488    1.1  augustss 
   2489    1.1  augustss 	if (mc->type == MIX_ON_OFF) {
   2490    1.1  augustss 		if (cp->type != AUDIO_MIXER_ENUM)
   2491   1.94      kent 			return EINVAL;
   2492    1.1  augustss 		cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
   2493   1.79      kent 	} else if (mc->type == MIX_SELECTOR) {
   2494   1.79      kent 		if (cp->type != AUDIO_MIXER_ENUM)
   2495   1.94      kent 			return EINVAL;
   2496   1.79      kent 		cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
   2497    1.1  augustss 	} else {
   2498    1.1  augustss 		if (cp->type != AUDIO_MIXER_VALUE)
   2499   1.94      kent 			return EINVAL;
   2500    1.1  augustss 		if (cp->un.value.num_channels != 1 &&
   2501    1.1  augustss 		    cp->un.value.num_channels != mc->nchan)
   2502   1.94      kent 			return EINVAL;
   2503    1.1  augustss 		for (i = 0; i < mc->nchan; i++)
   2504    1.1  augustss 			vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i);
   2505    1.1  augustss 		if (cp->un.value.num_channels == 1 && mc->nchan != 1) {
   2506    1.1  augustss 			for (val = 0, i = 0; i < mc->nchan; i++)
   2507    1.1  augustss 				val += vals[i];
   2508    1.1  augustss 			vals[0] = val / mc->nchan;
   2509    1.1  augustss 		}
   2510    1.1  augustss 		for (i = 0; i < cp->un.value.num_channels; i++)
   2511    1.1  augustss 			cp->un.value.level[i] = vals[i];
   2512    1.1  augustss 	}
   2513    1.1  augustss 
   2514   1.94      kent 	return 0;
   2515    1.1  augustss }
   2516   1.56      kent 
   2517   1.86      kent Static int
   2518   1.26  augustss uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   2519    1.1  augustss {
   2520   1.94      kent 	struct uaudio_softc *sc;
   2521    1.1  augustss 	struct mixerctl *mc;
   2522    1.1  augustss 	int i, n, vals[MIX_MAX_CHAN];
   2523    1.1  augustss 
   2524    1.1  augustss 	DPRINTFN(2,("uaudio_mixer_set_port: index = %d\n", cp->dev));
   2525   1.94      kent 	sc = addr;
   2526    1.1  augustss 	if (sc->sc_dying)
   2527   1.94      kent 		return EIO;
   2528   1.56      kent 
   2529   1.63  augustss 	n = cp->dev - UAC_NCLASSES;
   2530    1.1  augustss 	if (n < 0 || n >= sc->sc_nctls)
   2531   1.94      kent 		return ENXIO;
   2532    1.1  augustss 	mc = &sc->sc_ctls[n];
   2533    1.1  augustss 
   2534    1.1  augustss 	if (mc->type == MIX_ON_OFF) {
   2535    1.1  augustss 		if (cp->type != AUDIO_MIXER_ENUM)
   2536   1.94      kent 			return EINVAL;
   2537    1.1  augustss 		uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
   2538   1.79      kent 	} else if (mc->type == MIX_SELECTOR) {
   2539   1.79      kent 		if (cp->type != AUDIO_MIXER_ENUM)
   2540   1.94      kent 			return EINVAL;
   2541   1.79      kent 		uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
   2542    1.1  augustss 	} else {
   2543    1.1  augustss 		if (cp->type != AUDIO_MIXER_VALUE)
   2544   1.94      kent 			return EINVAL;
   2545    1.1  augustss 		if (cp->un.value.num_channels == 1)
   2546    1.1  augustss 			for (i = 0; i < mc->nchan; i++)
   2547    1.1  augustss 				vals[i] = cp->un.value.level[0];
   2548    1.1  augustss 		else if (cp->un.value.num_channels == mc->nchan)
   2549    1.1  augustss 			for (i = 0; i < mc->nchan; i++)
   2550    1.1  augustss 				vals[i] = cp->un.value.level[i];
   2551    1.1  augustss 		else
   2552   1.94      kent 			return EINVAL;
   2553    1.1  augustss 		for (i = 0; i < mc->nchan; i++)
   2554    1.1  augustss 			uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]);
   2555    1.1  augustss 	}
   2556   1.94      kent 	return 0;
   2557    1.1  augustss }
   2558    1.1  augustss 
   2559   1.86      kent Static int
   2560   1.26  augustss uaudio_trigger_input(void *addr, void *start, void *end, int blksize,
   2561   1.26  augustss 		     void (*intr)(void *), void *arg,
   2562  1.107  christos 		     const audio_params_t *param)
   2563    1.1  augustss {
   2564   1.94      kent 	struct uaudio_softc *sc;
   2565   1.94      kent 	struct chan *ch;
   2566    1.9  augustss 	usbd_status err;
   2567    1.5  augustss 	int i, s;
   2568    1.1  augustss 
   2569   1.94      kent 	sc = addr;
   2570    1.1  augustss 	if (sc->sc_dying)
   2571   1.94      kent 		return EIO;
   2572    1.1  augustss 
   2573    1.5  augustss 	DPRINTFN(3,("uaudio_trigger_input: sc=%p start=%p end=%p "
   2574    1.1  augustss 		    "blksize=%d\n", sc, start, end, blksize));
   2575   1.94      kent 	ch = &sc->sc_recchan;
   2576   1.54      kent 	uaudio_chan_set_param(ch, start, end, blksize);
   2577    1.5  augustss 	DPRINTFN(3,("uaudio_trigger_input: sample_size=%d bytes/frame=%d "
   2578    1.5  augustss 		    "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
   2579    1.5  augustss 		    ch->fraction));
   2580    1.5  augustss 
   2581  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2582  1.121  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   2583    1.9  augustss 	err = uaudio_chan_alloc_buffers(sc, ch);
   2584  1.121  jmcneill 	if (err) {
   2585  1.121  jmcneill 		mutex_spin_enter(&sc->sc_intr_lock);
   2586  1.121  jmcneill 		KERNEL_UNLOCK_ONE(curlwp);
   2587   1.94      kent 		return EIO;
   2588  1.121  jmcneill 	}
   2589    1.5  augustss 
   2590    1.9  augustss 	err = uaudio_chan_open(sc, ch);
   2591  1.121  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   2592  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2593    1.9  augustss 	if (err) {
   2594    1.5  augustss 		uaudio_chan_free_buffers(sc, ch);
   2595   1.94      kent 		return EIO;
   2596    1.5  augustss 	}
   2597    1.5  augustss 
   2598   1.47  jdolecek 	ch->intr = intr;
   2599   1.47  jdolecek 	ch->arg = arg;
   2600    1.5  augustss 
   2601  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2602    1.5  augustss 	s = splusb();
   2603   1.22  augustss 	for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX -1 shouldn't be needed */
   2604    1.5  augustss 		uaudio_chan_rtransfer(ch);
   2605    1.5  augustss 	splx(s);
   2606  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2607    1.5  augustss 
   2608   1.94      kent 	return 0;
   2609    1.1  augustss }
   2610   1.56      kent 
   2611   1.86      kent Static int
   2612   1.26  augustss uaudio_trigger_output(void *addr, void *start, void *end, int blksize,
   2613   1.26  augustss 		      void (*intr)(void *), void *arg,
   2614  1.107  christos 		      const audio_params_t *param)
   2615    1.1  augustss {
   2616   1.94      kent 	struct uaudio_softc *sc;
   2617   1.94      kent 	struct chan *ch;
   2618    1.9  augustss 	usbd_status err;
   2619    1.1  augustss 	int i, s;
   2620    1.1  augustss 
   2621   1.94      kent 	sc = addr;
   2622    1.1  augustss 	if (sc->sc_dying)
   2623   1.94      kent 		return EIO;
   2624    1.1  augustss 
   2625    1.1  augustss 	DPRINTFN(3,("uaudio_trigger_output: sc=%p start=%p end=%p "
   2626    1.1  augustss 		    "blksize=%d\n", sc, start, end, blksize));
   2627   1.94      kent 	ch = &sc->sc_playchan;
   2628   1.54      kent 	uaudio_chan_set_param(ch, start, end, blksize);
   2629    1.1  augustss 	DPRINTFN(3,("uaudio_trigger_output: sample_size=%d bytes/frame=%d "
   2630    1.1  augustss 		    "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
   2631    1.1  augustss 		    ch->fraction));
   2632    1.1  augustss 
   2633  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2634  1.121  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   2635    1.9  augustss 	err = uaudio_chan_alloc_buffers(sc, ch);
   2636  1.121  jmcneill 	if (err) {
   2637  1.121  jmcneill 		mutex_spin_enter(&sc->sc_intr_lock);
   2638  1.121  jmcneill 		KERNEL_UNLOCK_ONE(curlwp);
   2639   1.94      kent 		return EIO;
   2640  1.121  jmcneill 	}
   2641    1.1  augustss 
   2642    1.9  augustss 	err = uaudio_chan_open(sc, ch);
   2643  1.121  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   2644  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2645    1.9  augustss 	if (err) {
   2646    1.1  augustss 		uaudio_chan_free_buffers(sc, ch);
   2647   1.94      kent 		return EIO;
   2648    1.1  augustss 	}
   2649    1.1  augustss 
   2650   1.47  jdolecek 	ch->intr = intr;
   2651   1.47  jdolecek 	ch->arg = arg;
   2652    1.1  augustss 
   2653  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   2654    1.1  augustss 	s = splusb();
   2655   1.22  augustss 	for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX */
   2656    1.5  augustss 		uaudio_chan_ptransfer(ch);
   2657    1.1  augustss 	splx(s);
   2658  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   2659    1.1  augustss 
   2660   1.94      kent 	return 0;
   2661    1.1  augustss }
   2662    1.1  augustss 
   2663    1.1  augustss /* Set up a pipe for a channel. */
   2664   1.86      kent Static usbd_status
   2665   1.26  augustss uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch)
   2666    1.1  augustss {
   2667   1.94      kent 	struct as_info *as;
   2668   1.94      kent 	int endpt;
   2669    1.9  augustss 	usbd_status err;
   2670    1.1  augustss 
   2671   1.94      kent 	as = &sc->sc_alts[ch->altidx];
   2672   1.94      kent 	endpt = as->edesc->bEndpointAddress;
   2673   1.56      kent 	DPRINTF(("uaudio_chan_open: endpt=0x%02x, speed=%d, alt=%d\n",
   2674    1.1  augustss 		 endpt, ch->sample_rate, as->alt));
   2675    1.1  augustss 
   2676    1.1  augustss 	/* Set alternate interface corresponding to the mode. */
   2677   1.37   mycroft 	err = usbd_set_interface(as->ifaceh, as->alt);
   2678    1.9  augustss 	if (err)
   2679   1.94      kent 		return err;
   2680    1.1  augustss 
   2681   1.90      kent 	/*
   2682   1.90      kent 	 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request.
   2683   1.90      kent 	 */
   2684  1.120  jakllsch 	if ((UGETW(sc->sc_udev->ddesc.idVendor) != USB_VENDOR_ROLAND) &&
   2685  1.120  jakllsch 	    (UGETW(sc->sc_udev->ddesc.idProduct) != USB_PRODUCT_ROLAND_SD90)) {
   2686   1.90      kent 		err = uaudio_set_speed(sc, endpt, ch->sample_rate);
   2687  1.105  christos 		if (err) {
   2688   1.90      kent 			DPRINTF(("uaudio_chan_open: set_speed failed err=%s\n",
   2689   1.90      kent 				 usbd_errstr(err)));
   2690  1.105  christos 		}
   2691   1.90      kent 	}
   2692    1.1  augustss 
   2693   1.83      kent 	ch->pipe = 0;
   2694   1.83      kent 	ch->sync_pipe = 0;
   2695   1.47  jdolecek 	DPRINTF(("uaudio_chan_open: create pipe to 0x%02x\n", endpt));
   2696   1.37   mycroft 	err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->pipe);
   2697   1.83      kent 	if (err)
   2698   1.83      kent 		return err;
   2699   1.83      kent 	if (as->edesc1 != NULL) {
   2700   1.83      kent 		endpt = as->edesc1->bEndpointAddress;
   2701   1.83      kent 		DPRINTF(("uaudio_chan_open: create sync-pipe to 0x%02x\n", endpt));
   2702   1.83      kent 		err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->sync_pipe);
   2703   1.83      kent 	}
   2704   1.83      kent 	return err;
   2705    1.1  augustss }
   2706    1.1  augustss 
   2707   1.86      kent Static void
   2708   1.26  augustss uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch)
   2709    1.1  augustss {
   2710   1.94      kent 	struct as_info *as;
   2711   1.37   mycroft 
   2712   1.94      kent 	as = &sc->sc_alts[ch->altidx];
   2713   1.58      kent 	as->sc_busy = 0;
   2714   1.92      kent 	AUFMT_VALIDATE(as->aformat);
   2715    1.1  augustss 	if (sc->sc_nullalt >= 0) {
   2716   1.47  jdolecek 		DPRINTF(("uaudio_chan_close: set null alt=%d\n",
   2717    1.1  augustss 			 sc->sc_nullalt));
   2718   1.37   mycroft 		usbd_set_interface(as->ifaceh, sc->sc_nullalt);
   2719    1.1  augustss 	}
   2720   1.83      kent 	if (ch->pipe) {
   2721   1.83      kent 		usbd_abort_pipe(ch->pipe);
   2722   1.83      kent 		usbd_close_pipe(ch->pipe);
   2723   1.83      kent 	}
   2724   1.83      kent 	if (ch->sync_pipe) {
   2725   1.83      kent 		usbd_abort_pipe(ch->sync_pipe);
   2726   1.83      kent 		usbd_close_pipe(ch->sync_pipe);
   2727   1.83      kent 	}
   2728    1.1  augustss }
   2729    1.1  augustss 
   2730   1.86      kent Static usbd_status
   2731   1.26  augustss uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch)
   2732    1.1  augustss {
   2733    1.9  augustss 	usbd_xfer_handle xfer;
   2734   1.99  christos 	void *tbuf;
   2735    1.1  augustss 	int i, size;
   2736    1.1  augustss 
   2737    1.1  augustss 	size = (ch->bytes_per_frame + ch->sample_size) * UAUDIO_NFRAMES;
   2738    1.1  augustss 	for (i = 0; i < UAUDIO_NCHANBUFS; i++) {
   2739   1.11  augustss 		xfer = usbd_alloc_xfer(sc->sc_udev);
   2740    1.9  augustss 		if (xfer == 0)
   2741    1.1  augustss 			goto bad;
   2742    1.9  augustss 		ch->chanbufs[i].xfer = xfer;
   2743   1.99  christos 		tbuf = usbd_alloc_buffer(xfer, size);
   2744   1.99  christos 		if (tbuf == 0) {
   2745    1.1  augustss 			i++;
   2746    1.1  augustss 			goto bad;
   2747    1.1  augustss 		}
   2748   1.99  christos 		ch->chanbufs[i].buffer = tbuf;
   2749    1.1  augustss 		ch->chanbufs[i].chan = ch;
   2750    1.1  augustss 	}
   2751    1.1  augustss 
   2752   1.94      kent 	return USBD_NORMAL_COMPLETION;
   2753    1.1  augustss 
   2754    1.1  augustss bad:
   2755    1.1  augustss 	while (--i >= 0)
   2756    1.1  augustss 		/* implicit buffer free */
   2757   1.11  augustss 		usbd_free_xfer(ch->chanbufs[i].xfer);
   2758   1.94      kent 	return USBD_NOMEM;
   2759    1.1  augustss }
   2760    1.1  augustss 
   2761   1.86      kent Static void
   2762  1.107  christos uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch)
   2763    1.1  augustss {
   2764    1.1  augustss 	int i;
   2765    1.1  augustss 
   2766    1.1  augustss 	for (i = 0; i < UAUDIO_NCHANBUFS; i++)
   2767   1.11  augustss 		usbd_free_xfer(ch->chanbufs[i].xfer);
   2768    1.1  augustss }
   2769    1.1  augustss 
   2770    1.1  augustss /* Called at splusb() */
   2771   1.86      kent Static void
   2772   1.26  augustss uaudio_chan_ptransfer(struct chan *ch)
   2773    1.1  augustss {
   2774    1.1  augustss 	struct chanbuf *cb;
   2775    1.1  augustss 	int i, n, size, residue, total;
   2776    1.1  augustss 
   2777    1.8  augustss 	if (ch->sc->sc_dying)
   2778    1.8  augustss 		return;
   2779    1.8  augustss 
   2780    1.1  augustss 	/* Pick the next channel buffer. */
   2781    1.1  augustss 	cb = &ch->chanbufs[ch->curchanbuf];
   2782    1.1  augustss 	if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
   2783    1.1  augustss 		ch->curchanbuf = 0;
   2784    1.1  augustss 
   2785    1.1  augustss 	/* Compute the size of each frame in the next transfer. */
   2786    1.1  augustss 	residue = ch->residue;
   2787    1.1  augustss 	total = 0;
   2788    1.1  augustss 	for (i = 0; i < UAUDIO_NFRAMES; i++) {
   2789    1.1  augustss 		size = ch->bytes_per_frame;
   2790    1.1  augustss 		residue += ch->fraction;
   2791    1.1  augustss 		if (residue >= USB_FRAMES_PER_SECOND) {
   2792   1.47  jdolecek 			if ((ch->sc->sc_altflags & UA_NOFRAC) == 0)
   2793   1.31  augustss 				size += ch->sample_size;
   2794    1.1  augustss 			residue -= USB_FRAMES_PER_SECOND;
   2795    1.1  augustss 		}
   2796    1.1  augustss 		cb->sizes[i] = size;
   2797    1.1  augustss 		total += size;
   2798    1.1  augustss 	}
   2799    1.1  augustss 	ch->residue = residue;
   2800    1.1  augustss 	cb->size = total;
   2801    1.1  augustss 
   2802   1.56      kent 	/*
   2803    1.1  augustss 	 * Transfer data from upper layer buffer to channel buffer, taking
   2804    1.1  augustss 	 * care of wrapping the upper layer buffer.
   2805    1.1  augustss 	 */
   2806    1.1  augustss 	n = min(total, ch->end - ch->cur);
   2807    1.1  augustss 	memcpy(cb->buffer, ch->cur, n);
   2808    1.1  augustss 	ch->cur += n;
   2809    1.1  augustss 	if (ch->cur >= ch->end)
   2810    1.1  augustss 		ch->cur = ch->start;
   2811    1.1  augustss 	if (total > n) {
   2812    1.1  augustss 		total -= n;
   2813    1.1  augustss 		memcpy(cb->buffer + n, ch->cur, total);
   2814    1.1  augustss 		ch->cur += total;
   2815    1.1  augustss 	}
   2816    1.1  augustss 
   2817    1.4  augustss #ifdef UAUDIO_DEBUG
   2818    1.1  augustss 	if (uaudiodebug > 8) {
   2819    1.5  augustss 		DPRINTF(("uaudio_chan_ptransfer: buffer=%p, residue=0.%03d\n",
   2820    1.1  augustss 			 cb->buffer, ch->residue));
   2821    1.1  augustss 		for (i = 0; i < UAUDIO_NFRAMES; i++) {
   2822    1.1  augustss 			DPRINTF(("   [%d] length %d\n", i, cb->sizes[i]));
   2823    1.1  augustss 		}
   2824    1.1  augustss 	}
   2825    1.1  augustss #endif
   2826    1.1  augustss 
   2827    1.9  augustss 	DPRINTFN(5,("uaudio_chan_transfer: ptransfer xfer=%p\n", cb->xfer));
   2828    1.1  augustss 	/* Fill the request */
   2829   1.56      kent 	usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
   2830   1.56      kent 			     UAUDIO_NFRAMES, USBD_NO_COPY,
   2831   1.11  augustss 			     uaudio_chan_pintr);
   2832    1.1  augustss 
   2833    1.9  augustss 	(void)usbd_transfer(cb->xfer);
   2834    1.1  augustss }
   2835    1.1  augustss 
   2836   1.86      kent Static void
   2837   1.26  augustss uaudio_chan_pintr(usbd_xfer_handle xfer, usbd_private_handle priv,
   2838   1.26  augustss 		  usbd_status status)
   2839    1.1  augustss {
   2840   1.94      kent 	struct chanbuf *cb;
   2841   1.94      kent 	struct chan *ch;
   2842   1.94      kent 	uint32_t count;
   2843    1.1  augustss 
   2844   1.94      kent 	cb = priv;
   2845   1.94      kent 	ch = cb->chan;
   2846    1.1  augustss 	/* Return if we are aborting. */
   2847    1.1  augustss 	if (status == USBD_CANCELLED)
   2848    1.1  augustss 		return;
   2849    1.1  augustss 
   2850   1.18  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   2851    1.5  augustss 	DPRINTFN(5,("uaudio_chan_pintr: count=%d, transferred=%d\n",
   2852    1.5  augustss 		    count, ch->transferred));
   2853    1.5  augustss #ifdef DIAGNOSTIC
   2854    1.5  augustss 	if (count != cb->size) {
   2855  1.113      cube 		aprint_error("uaudio_chan_pintr: count(%d) != size(%d)\n",
   2856    1.5  augustss 		       count, cb->size);
   2857    1.5  augustss 	}
   2858    1.5  augustss #endif
   2859    1.5  augustss 
   2860    1.5  augustss 	ch->transferred += cb->size;
   2861  1.121  jmcneill 	mutex_spin_enter(&ch->sc->sc_intr_lock);
   2862    1.1  augustss 	/* Call back to upper layer */
   2863    1.5  augustss 	while (ch->transferred >= ch->blksize) {
   2864    1.5  augustss 		ch->transferred -= ch->blksize;
   2865   1.56      kent 		DPRINTFN(5,("uaudio_chan_pintr: call %p(%p)\n",
   2866    1.5  augustss 			    ch->intr, ch->arg));
   2867    1.5  augustss 		ch->intr(ch->arg);
   2868    1.5  augustss 	}
   2869  1.121  jmcneill 	mutex_spin_exit(&ch->sc->sc_intr_lock);
   2870    1.5  augustss 
   2871    1.5  augustss 	/* start next transfer */
   2872    1.5  augustss 	uaudio_chan_ptransfer(ch);
   2873    1.5  augustss }
   2874    1.5  augustss 
   2875    1.5  augustss /* Called at splusb() */
   2876   1.86      kent Static void
   2877   1.26  augustss uaudio_chan_rtransfer(struct chan *ch)
   2878    1.5  augustss {
   2879    1.5  augustss 	struct chanbuf *cb;
   2880    1.5  augustss 	int i, size, residue, total;
   2881    1.8  augustss 
   2882    1.8  augustss 	if (ch->sc->sc_dying)
   2883    1.8  augustss 		return;
   2884    1.5  augustss 
   2885    1.5  augustss 	/* Pick the next channel buffer. */
   2886    1.5  augustss 	cb = &ch->chanbufs[ch->curchanbuf];
   2887    1.5  augustss 	if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
   2888    1.5  augustss 		ch->curchanbuf = 0;
   2889    1.5  augustss 
   2890    1.5  augustss 	/* Compute the size of each frame in the next transfer. */
   2891    1.5  augustss 	residue = ch->residue;
   2892    1.5  augustss 	total = 0;
   2893    1.5  augustss 	for (i = 0; i < UAUDIO_NFRAMES; i++) {
   2894    1.5  augustss 		size = ch->bytes_per_frame;
   2895    1.5  augustss 		cb->sizes[i] = size;
   2896   1.62    toshii 		cb->offsets[i] = total;
   2897    1.5  augustss 		total += size;
   2898    1.5  augustss 	}
   2899    1.5  augustss 	ch->residue = residue;
   2900    1.5  augustss 	cb->size = total;
   2901    1.5  augustss 
   2902    1.5  augustss #ifdef UAUDIO_DEBUG
   2903    1.5  augustss 	if (uaudiodebug > 8) {
   2904    1.5  augustss 		DPRINTF(("uaudio_chan_rtransfer: buffer=%p, residue=0.%03d\n",
   2905    1.5  augustss 			 cb->buffer, ch->residue));
   2906    1.5  augustss 		for (i = 0; i < UAUDIO_NFRAMES; i++) {
   2907    1.5  augustss 			DPRINTF(("   [%d] length %d\n", i, cb->sizes[i]));
   2908    1.5  augustss 		}
   2909    1.5  augustss 	}
   2910    1.5  augustss #endif
   2911    1.5  augustss 
   2912    1.9  augustss 	DPRINTFN(5,("uaudio_chan_rtransfer: transfer xfer=%p\n", cb->xfer));
   2913    1.5  augustss 	/* Fill the request */
   2914   1.56      kent 	usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
   2915   1.56      kent 			     UAUDIO_NFRAMES, USBD_NO_COPY,
   2916   1.11  augustss 			     uaudio_chan_rintr);
   2917    1.5  augustss 
   2918    1.9  augustss 	(void)usbd_transfer(cb->xfer);
   2919    1.5  augustss }
   2920    1.5  augustss 
   2921   1.86      kent Static void
   2922   1.26  augustss uaudio_chan_rintr(usbd_xfer_handle xfer, usbd_private_handle priv,
   2923   1.26  augustss 		  usbd_status status)
   2924    1.5  augustss {
   2925   1.94      kent 	struct chanbuf *cb;
   2926   1.94      kent 	struct chan *ch;
   2927   1.94      kent 	uint32_t count;
   2928  1.121  jmcneill 	int i, n, frsize;
   2929    1.5  augustss 
   2930   1.94      kent 	cb = priv;
   2931   1.94      kent 	ch = cb->chan;
   2932    1.5  augustss 	/* Return if we are aborting. */
   2933    1.5  augustss 	if (status == USBD_CANCELLED)
   2934    1.5  augustss 		return;
   2935    1.5  augustss 
   2936   1.18  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   2937    1.5  augustss 	DPRINTFN(5,("uaudio_chan_rintr: count=%d, transferred=%d\n",
   2938    1.1  augustss 		    count, ch->transferred));
   2939   1.31  augustss 
   2940   1.62    toshii 	/* count < cb->size is normal for asynchronous source */
   2941    1.1  augustss #ifdef DIAGNOSTIC
   2942   1.62    toshii 	if (count > cb->size) {
   2943  1.113      cube 		aprint_error("uaudio_chan_rintr: count(%d) > size(%d)\n",
   2944    1.1  augustss 		       count, cb->size);
   2945    1.1  augustss 	}
   2946    1.1  augustss #endif
   2947    1.1  augustss 
   2948   1.56      kent 	/*
   2949    1.5  augustss 	 * Transfer data from channel buffer to upper layer buffer, taking
   2950    1.5  augustss 	 * care of wrapping the upper layer buffer.
   2951    1.5  augustss 	 */
   2952   1.62    toshii 	for(i = 0; i < UAUDIO_NFRAMES; i++) {
   2953   1.62    toshii 		frsize = cb->sizes[i];
   2954   1.62    toshii 		n = min(frsize, ch->end - ch->cur);
   2955   1.62    toshii 		memcpy(ch->cur, cb->buffer + cb->offsets[i], n);
   2956   1.62    toshii 		ch->cur += n;
   2957   1.62    toshii 		if (ch->cur >= ch->end)
   2958   1.62    toshii 			ch->cur = ch->start;
   2959   1.62    toshii 		if (frsize > n) {
   2960   1.62    toshii 			memcpy(ch->cur, cb->buffer + cb->offsets[i] + n,
   2961   1.62    toshii 			    frsize - n);
   2962   1.62    toshii 			ch->cur += frsize - n;
   2963   1.62    toshii 		}
   2964    1.5  augustss 	}
   2965    1.5  augustss 
   2966    1.5  augustss 	/* Call back to upper layer */
   2967   1.62    toshii 	ch->transferred += count;
   2968  1.121  jmcneill 	mutex_spin_enter(&ch->sc->sc_intr_lock);
   2969    1.1  augustss 	while (ch->transferred >= ch->blksize) {
   2970    1.1  augustss 		ch->transferred -= ch->blksize;
   2971   1.56      kent 		DPRINTFN(5,("uaudio_chan_rintr: call %p(%p)\n",
   2972    1.1  augustss 			    ch->intr, ch->arg));
   2973    1.1  augustss 		ch->intr(ch->arg);
   2974    1.1  augustss 	}
   2975  1.121  jmcneill 	mutex_spin_exit(&ch->sc->sc_intr_lock);
   2976    1.1  augustss 
   2977    1.1  augustss 	/* start next transfer */
   2978    1.5  augustss 	uaudio_chan_rtransfer(ch);
   2979    1.1  augustss }
   2980    1.1  augustss 
   2981   1.86      kent Static void
   2982   1.65    toshii uaudio_chan_init(struct chan *ch, int altidx, const struct audio_params *param,
   2983   1.65    toshii     int maxpktsize)
   2984    1.1  augustss {
   2985    1.1  augustss 	int samples_per_frame, sample_size;
   2986    1.1  augustss 
   2987   1.54      kent 	ch->altidx = altidx;
   2988   1.93      kent 	sample_size = param->precision * param->channels / 8;
   2989   1.93      kent 	samples_per_frame = param->sample_rate / USB_FRAMES_PER_SECOND;
   2990    1.1  augustss 	ch->sample_size = sample_size;
   2991   1.93      kent 	ch->sample_rate = param->sample_rate;
   2992   1.65    toshii 	if (maxpktsize == 0) {
   2993   1.93      kent 		ch->fraction = param->sample_rate % USB_FRAMES_PER_SECOND;
   2994   1.65    toshii 		ch->bytes_per_frame = samples_per_frame * sample_size;
   2995   1.65    toshii 	} else {
   2996   1.65    toshii 		ch->fraction = 0;
   2997   1.65    toshii 		ch->bytes_per_frame = maxpktsize;
   2998   1.65    toshii 	}
   2999    1.1  augustss 	ch->residue = 0;
   3000   1.54      kent }
   3001    1.1  augustss 
   3002   1.86      kent Static void
   3003   1.54      kent uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize)
   3004   1.54      kent {
   3005   1.94      kent 
   3006    1.1  augustss 	ch->start = start;
   3007    1.1  augustss 	ch->end = end;
   3008    1.1  augustss 	ch->cur = start;
   3009    1.1  augustss 	ch->blksize = blksize;
   3010    1.1  augustss 	ch->transferred = 0;
   3011    1.1  augustss 	ch->curchanbuf = 0;
   3012    1.1  augustss }
   3013    1.1  augustss 
   3014   1.86      kent Static int
   3015   1.26  augustss uaudio_set_params(void *addr, int setmode, int usemode,
   3016   1.93      kent 		  struct audio_params *play, struct audio_params *rec,
   3017   1.93      kent 		  stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   3018    1.1  augustss {
   3019   1.94      kent 	struct uaudio_softc *sc;
   3020   1.94      kent 	int paltidx, raltidx;
   3021   1.38   mycroft 	struct audio_params *p;
   3022   1.93      kent 	stream_filter_list_t *fil;
   3023   1.92      kent 	int mode, i;
   3024    1.1  augustss 
   3025   1.94      kent 	sc = addr;
   3026   1.94      kent 	paltidx = -1;
   3027   1.94      kent 	raltidx = -1;
   3028    1.1  augustss 	if (sc->sc_dying)
   3029   1.94      kent 		return EIO;
   3030    1.1  augustss 
   3031   1.73   mycroft 	if (((usemode & AUMODE_PLAY) && sc->sc_playchan.pipe != NULL) ||
   3032   1.73   mycroft 	    ((usemode & AUMODE_RECORD) && sc->sc_recchan.pipe != NULL))
   3033   1.94      kent 		return EBUSY;
   3034    1.1  augustss 
   3035   1.92      kent 	if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
   3036   1.47  jdolecek 		sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0;
   3037   1.92      kent 		AUFMT_VALIDATE(sc->sc_alts[sc->sc_playchan.altidx].aformat);
   3038   1.92      kent 	}
   3039   1.92      kent 	if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
   3040   1.47  jdolecek 		sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0;
   3041   1.92      kent 		AUFMT_VALIDATE(sc->sc_alts[sc->sc_recchan.altidx].aformat);
   3042   1.92      kent 	}
   3043   1.47  jdolecek 
   3044   1.76   mycroft 	/* Some uaudio devices are unidirectional.  Don't try to find a
   3045   1.76   mycroft 	   matching mode for the unsupported direction. */
   3046   1.76   mycroft 	setmode &= sc->sc_mode;
   3047   1.76   mycroft 
   3048   1.38   mycroft 	for (mode = AUMODE_RECORD; mode != -1;
   3049   1.38   mycroft 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   3050   1.76   mycroft 		if ((setmode & mode) == 0)
   3051   1.38   mycroft 			continue;
   3052   1.56      kent 
   3053   1.93      kent 		if (mode == AUMODE_PLAY) {
   3054   1.93      kent 			p = play;
   3055   1.93      kent 			fil = pfil;
   3056   1.93      kent 		} else {
   3057   1.93      kent 			p = rec;
   3058   1.93      kent 			fil = rfil;
   3059   1.93      kent 		}
   3060   1.92      kent 		i = auconv_set_converter(sc->sc_formats, sc->sc_nformats,
   3061   1.93      kent 					 mode, p, TRUE, fil);
   3062   1.50      kent 		if (i < 0)
   3063   1.92      kent 			return EINVAL;
   3064   1.73   mycroft 
   3065   1.73   mycroft 		if (mode == AUMODE_PLAY)
   3066   1.73   mycroft 			paltidx = i;
   3067   1.73   mycroft 		else
   3068   1.73   mycroft 			raltidx = i;
   3069    1.1  augustss 	}
   3070    1.1  augustss 
   3071   1.73   mycroft 	if ((setmode & AUMODE_PLAY)) {
   3072   1.93      kent 		p = pfil->req_size > 0 ? &pfil->filters[0].param : play;
   3073   1.47  jdolecek 		/* XXX abort transfer if currently happening? */
   3074   1.93      kent 		uaudio_chan_init(&sc->sc_playchan, paltidx, p, 0);
   3075   1.47  jdolecek 	}
   3076   1.73   mycroft 	if ((setmode & AUMODE_RECORD)) {
   3077   1.93      kent 		p = rfil->req_size > 0 ? &pfil->filters[0].param : rec;
   3078   1.47  jdolecek 		/* XXX abort transfer if currently happening? */
   3079   1.93      kent 		uaudio_chan_init(&sc->sc_recchan, raltidx, p,
   3080   1.65    toshii 		    UGETW(sc->sc_alts[raltidx].edesc->wMaxPacketSize));
   3081   1.47  jdolecek 	}
   3082   1.47  jdolecek 
   3083   1.92      kent 	if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
   3084   1.73   mycroft 		sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1;
   3085   1.92      kent 		AUFMT_INVALIDATE(sc->sc_alts[sc->sc_playchan.altidx].aformat);
   3086   1.92      kent 	}
   3087   1.92      kent 	if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
   3088   1.73   mycroft 		sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1;
   3089   1.92      kent 		AUFMT_INVALIDATE(sc->sc_alts[sc->sc_recchan.altidx].aformat);
   3090   1.92      kent 	}
   3091   1.73   mycroft 
   3092   1.56      kent 	DPRINTF(("uaudio_set_params: use altidx=p%d/r%d, altno=p%d/r%d\n",
   3093   1.56      kent 		 sc->sc_playchan.altidx, sc->sc_recchan.altidx,
   3094   1.47  jdolecek 		 (sc->sc_playchan.altidx >= 0)
   3095   1.47  jdolecek 		   ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting
   3096   1.47  jdolecek 		   : -1,
   3097   1.47  jdolecek 		 (sc->sc_recchan.altidx >= 0)
   3098   1.47  jdolecek 		   ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting
   3099   1.47  jdolecek 		   : -1));
   3100   1.56      kent 
   3101   1.94      kent 	return 0;
   3102    1.1  augustss }
   3103    1.1  augustss 
   3104   1.86      kent Static usbd_status
   3105   1.26  augustss uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed)
   3106    1.1  augustss {
   3107    1.1  augustss 	usb_device_request_t req;
   3108  1.121  jmcneill 	usbd_status err;
   3109   1.94      kent 	uint8_t data[3];
   3110    1.1  augustss 
   3111    1.1  augustss 	DPRINTFN(5,("uaudio_set_speed: endpt=%d speed=%u\n", endpt, speed));
   3112    1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
   3113    1.1  augustss 	req.bRequest = SET_CUR;
   3114    1.1  augustss 	USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
   3115    1.1  augustss 	USETW(req.wIndex, endpt);
   3116    1.1  augustss 	USETW(req.wLength, 3);
   3117    1.1  augustss 	data[0] = speed;
   3118    1.1  augustss 	data[1] = speed >> 8;
   3119    1.1  augustss 	data[2] = speed >> 16;
   3120    1.1  augustss 
   3121  1.121  jmcneill 	KERNEL_LOCK(1, curlwp);
   3122  1.121  jmcneill 	err = usbd_do_request(sc->sc_udev, &req, data);
   3123  1.121  jmcneill 	KERNEL_UNLOCK_ONE(curlwp);
   3124  1.121  jmcneill 
   3125  1.121  jmcneill 	return err;
   3126    1.1  augustss }
   3127  1.114      kent 
   3128  1.114      kent #ifdef _MODULE
   3129  1.114      kent 
   3130  1.114      kent MODULE(MODULE_CLASS_DRIVER, uaudio, NULL);
   3131  1.114      kent 
   3132  1.114      kent static const struct cfiattrdata audiobuscf_iattrdata = {
   3133  1.114      kent 	"audiobus", 0, { { NULL, NULL, 0 }, }
   3134  1.114      kent };
   3135  1.114      kent static const struct cfiattrdata * const uaudio_attrs[] = {
   3136  1.114      kent 	&audiobuscf_iattrdata, NULL
   3137  1.114      kent };
   3138  1.114      kent CFDRIVER_DECL(uaudio, DV_DULL, uaudio_attrs);
   3139  1.114      kent extern struct cfattach uaudio_ca;
   3140  1.114      kent static int uaudioloc[6/*USBIFIFCF_NLOCS*/] = {
   3141  1.114      kent 	-1/*USBIFIFCF_PORT_DEFAULT*/,
   3142  1.114      kent 	-1/*USBIFIFCF_CONFIGURATION_DEFAULT*/,
   3143  1.114      kent 	-1/*USBIFIFCF_INTERFACE_DEFAULT*/,
   3144  1.114      kent 	-1/*USBIFIFCF_VENDOR_DEFAULT*/,
   3145  1.114      kent 	-1/*USBIFIFCF_PRODUCT_DEFAULT*/,
   3146  1.114      kent 	-1/*USBIFIFCF_RELEASE_DEFAULT*/};
   3147  1.114      kent static struct cfparent uhubparent = {
   3148  1.114      kent 	"usbifif", NULL, DVUNIT_ANY
   3149  1.114      kent };
   3150  1.114      kent static struct cfdata uaudio_cfdata[] = {
   3151  1.114      kent 	{
   3152  1.114      kent 		.cf_name = "uaudio",
   3153  1.114      kent 		.cf_atname = "uaudio",
   3154  1.114      kent 		.cf_unit = 0,
   3155  1.114      kent 		.cf_fstate = FSTATE_STAR,
   3156  1.114      kent 		.cf_loc = uaudioloc,
   3157  1.114      kent 		.cf_flags = 0,
   3158  1.114      kent 		.cf_pspec = &uhubparent,
   3159  1.114      kent 	},
   3160  1.114      kent 	{ NULL }
   3161  1.114      kent };
   3162  1.114      kent 
   3163  1.114      kent static int
   3164  1.114      kent uaudio_modcmd(modcmd_t cmd, void *arg)
   3165  1.114      kent {
   3166  1.114      kent 	int err;
   3167  1.114      kent 
   3168  1.114      kent 	switch (cmd) {
   3169  1.114      kent 	case MODULE_CMD_INIT:
   3170  1.114      kent 		err = config_cfdriver_attach(&uaudio_cd);
   3171  1.114      kent 		if (err) {
   3172  1.114      kent 			return err;
   3173  1.114      kent 		}
   3174  1.114      kent 		err = config_cfattach_attach("uaudio", &uaudio_ca);
   3175  1.114      kent 		if (err) {
   3176  1.114      kent 			config_cfdriver_detach(&uaudio_cd);
   3177  1.114      kent 			return err;
   3178  1.114      kent 		}
   3179  1.114      kent 		err = config_cfdata_attach(uaudio_cfdata, 1);
   3180  1.114      kent 		if (err) {
   3181  1.114      kent 			config_cfattach_detach("uaudio", &uaudio_ca);
   3182  1.114      kent 			config_cfdriver_detach(&uaudio_cd);
   3183  1.114      kent 			return err;
   3184  1.114      kent 		}
   3185  1.114      kent 		return 0;
   3186  1.114      kent 	case MODULE_CMD_FINI:
   3187  1.114      kent 		err = config_cfdata_detach(uaudio_cfdata);
   3188  1.114      kent 		if (err)
   3189  1.114      kent 			return err;
   3190  1.114      kent 		config_cfattach_detach("uaudio", &uaudio_ca);
   3191  1.114      kent 		config_cfdriver_detach(&uaudio_cd);
   3192  1.114      kent 		return 0;
   3193  1.114      kent 	default:
   3194  1.114      kent 		return ENOTTY;
   3195  1.114      kent 	}
   3196  1.114      kent }
   3197  1.114      kent 
   3198  1.114      kent #endif
   3199