xref: /freebsd/sys/dev/sound/usb/uaudio.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
1 /*	$NetBSD: uaudio.c,v 1.91 2004/11/05 17:46:14 kent Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf
39  *                  http://www.usb.org/developers/devclass_docs/frmts10.pdf
40  *                  http://www.usb.org/developers/devclass_docs/termt10.pdf
41  */
42 
43 /*
44  * Also merged:
45  *  $NetBSD: uaudio.c,v 1.94 2005/01/15 15:19:53 kent Exp $
46  *  $NetBSD: uaudio.c,v 1.95 2005/01/16 06:02:19 dsainty Exp $
47  *  $NetBSD: uaudio.c,v 1.96 2005/01/16 12:46:00 kent Exp $
48  *  $NetBSD: uaudio.c,v 1.97 2005/02/24 08:19:38 martin Exp $
49  */
50 
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/bus.h>
59 #include <sys/module.h>
60 #include <sys/lock.h>
61 #include <sys/mutex.h>
62 #include <sys/condvar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sx.h>
65 #include <sys/unistd.h>
66 #include <sys/callout.h>
67 #include <sys/malloc.h>
68 #include <sys/priv.h>
69 
70 #include "usbdevs.h"
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbhid.h>
75 #include <dev/usb/usb_request.h>
76 #include <dev/usb/usb_process.h>
77 
78 #define	USB_DEBUG_VAR uaudio_debug
79 #include <dev/usb/usb_debug.h>
80 
81 #include <dev/usb/quirk/usb_quirk.h>
82 
83 #include <sys/reboot.h>			/* for bootverbose */
84 
85 #ifdef HAVE_KERNEL_OPTION_HEADERS
86 #include "opt_snd.h"
87 #endif
88 
89 #include <dev/sound/pcm/sound.h>
90 #include <dev/sound/usb/uaudioreg.h>
91 #include <dev/sound/usb/uaudio.h>
92 #include <dev/sound/chip.h>
93 #include "feeder_if.h"
94 
95 static int uaudio_default_rate = 0;		/* use rate list */
96 static int uaudio_default_bits = 32;
97 static int uaudio_default_channels = 0;		/* use default */
98 
99 #ifdef USB_DEBUG
100 static int uaudio_debug = 0;
101 
102 static SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW, 0, "USB uaudio");
103 
104 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, debug, CTLFLAG_RW,
105     &uaudio_debug, 0, "uaudio debug level");
106 
107 TUNABLE_INT("hw.usb.uaudio.default_rate", &uaudio_default_rate);
108 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_rate, CTLFLAG_RW,
109     &uaudio_default_rate, 0, "uaudio default sample rate");
110 
111 TUNABLE_INT("hw.usb.uaudio.default_bits", &uaudio_default_bits);
112 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_bits, CTLFLAG_RW,
113     &uaudio_default_bits, 0, "uaudio default sample bits");
114 
115 TUNABLE_INT("hw.usb.uaudio.default_channels", &uaudio_default_channels);
116 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_channels, CTLFLAG_RW,
117     &uaudio_default_channels, 0, "uaudio default sample channels");
118 #endif
119 
120 #define	UAUDIO_NFRAMES		64	/* must be factor of 8 due HS-USB */
121 #define	UAUDIO_NCHANBUFS	2	/* number of outstanding request */
122 #define	UAUDIO_RECURSE_LIMIT	255	/* rounds */
123 
124 #define	MAKE_WORD(h,l) (((h) << 8) | (l))
125 #define	BIT_TEST(bm,bno) (((bm)[(bno) / 8] >> (7 - ((bno) % 8))) & 1)
126 #define	UAUDIO_MAX_CHAN(x) (x)
127 #define	MIX(sc) ((sc)->sc_mixer_node)
128 
129 union uaudio_asid {
130 	const struct usb_audio_streaming_interface_descriptor *v1;
131 	const struct usb_audio20_streaming_interface_descriptor *v2;
132 };
133 
134 union uaudio_asf1d {
135 	const struct usb_audio_streaming_type1_descriptor *v1;
136 	const struct usb_audio20_streaming_type1_descriptor *v2;
137 };
138 
139 union uaudio_sed {
140 	const struct usb_audio_streaming_endpoint_descriptor *v1;
141 	const struct usb_audio20_streaming_endpoint_descriptor *v2;
142 };
143 
144 struct uaudio_mixer_node {
145 	const char *name;
146 
147 	int32_t	minval;
148 	int32_t	maxval;
149 #define	MIX_MAX_CHAN 16
150 	int32_t	wValue[MIX_MAX_CHAN];	/* using nchan */
151 	uint32_t mul;
152 	uint32_t ctl;
153 
154 	int wData[MIX_MAX_CHAN];	/* using nchan */
155 	uint16_t wIndex;
156 
157 	uint8_t	update[(MIX_MAX_CHAN + 7) / 8];
158 	uint8_t	nchan;
159 	uint8_t	type;
160 #define	MIX_ON_OFF	1
161 #define	MIX_SIGNED_16	2
162 #define	MIX_UNSIGNED_16	3
163 #define	MIX_SIGNED_8	4
164 #define	MIX_SELECTOR	5
165 #define	MIX_UNKNOWN     6
166 #define	MIX_SIZE(n) ((((n) == MIX_SIGNED_16) || \
167 		      ((n) == MIX_UNSIGNED_16)) ? 2 : 1)
168 #define	MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
169 
170 #define	MAX_SELECTOR_INPUT_PIN 256
171 	uint8_t	slctrtype[MAX_SELECTOR_INPUT_PIN];
172 	uint8_t	class;
173 	uint8_t val_default;
174 
175 	uint8_t desc[64];
176 
177 	struct uaudio_mixer_node *next;
178 };
179 
180 struct uaudio_configure_msg {
181 	struct usb_proc_msg hdr;
182 	struct uaudio_softc *sc;
183 };
184 
185 #define	CHAN_MAX_ALT 20
186 
187 struct uaudio_chan_alt {
188 	union uaudio_asf1d p_asf1d;
189 	union uaudio_sed p_sed;
190 	const usb_endpoint_descriptor_audio_t *p_ed1;
191 	const struct uaudio_format *p_fmt;
192 	const struct usb_config *usb_cfg;
193 	uint32_t sample_rate;	/* in Hz */
194 	uint16_t sample_size;
195 	uint8_t	iface_index;
196 	uint8_t	iface_alt_index;
197 	uint8_t channels;
198 };
199 
200 struct uaudio_chan {
201 	struct pcmchan_caps pcm_cap;	/* capabilities */
202 	struct uaudio_chan_alt usb_alt[CHAN_MAX_ALT];
203 	struct snd_dbuf *pcm_buf;
204 	struct mtx *pcm_mtx;		/* lock protecting this structure */
205 	struct uaudio_softc *priv_sc;
206 	struct pcm_channel *pcm_ch;
207 	struct usb_xfer *xfer[UAUDIO_NCHANBUFS + 1];
208 
209 	uint8_t *buf;			/* pointer to buffer */
210 	uint8_t *start;			/* upper layer buffer start */
211 	uint8_t *end;			/* upper layer buffer end */
212 	uint8_t *cur;			/* current position in upper layer
213 					 * buffer */
214 
215 	uint32_t intr_frames;		/* in units */
216 	uint32_t frames_per_second;
217 	uint32_t sample_rem;
218 	uint32_t sample_curr;
219 	uint32_t max_buf;
220 
221 	uint32_t pcm_format[2];
222 
223 	uint16_t bytes_per_frame[2];
224 
225 	uint8_t	num_alt;
226 	uint8_t cur_alt;
227 	uint8_t set_alt;
228 	uint8_t operation;
229 #define	CHAN_OP_NONE 0
230 #define	CHAN_OP_START 1
231 #define	CHAN_OP_STOP 2
232 #define	CHAN_OP_DRAIN 3
233 
234 	uint8_t last_sync_time;
235 	uint8_t last_sync_state;
236 #define	UAUDIO_SYNC_NONE 0
237 #define	UAUDIO_SYNC_MORE 1
238 #define	UAUDIO_SYNC_LESS 2
239 };
240 
241 #define	UMIDI_CABLES_MAX   16		/* units */
242 #define	UMIDI_TX_FRAMES	   256		/* units */
243 #define	UMIDI_TX_BUFFER    (UMIDI_TX_FRAMES * 4)	/* bytes */
244 
245 enum {
246 	UMIDI_TX_TRANSFER,
247 	UMIDI_RX_TRANSFER,
248 	UMIDI_N_TRANSFER,
249 };
250 
251 struct umidi_sub_chan {
252 	struct usb_fifo_sc fifo;
253 	uint8_t *temp_cmd;
254 	uint8_t	temp_0[4];
255 	uint8_t	temp_1[4];
256 	uint8_t	state;
257 #define	UMIDI_ST_UNKNOWN   0		/* scan for command */
258 #define	UMIDI_ST_1PARAM    1
259 #define	UMIDI_ST_2PARAM_1  2
260 #define	UMIDI_ST_2PARAM_2  3
261 #define	UMIDI_ST_SYSEX_0   4
262 #define	UMIDI_ST_SYSEX_1   5
263 #define	UMIDI_ST_SYSEX_2   6
264 
265 	uint8_t	read_open:1;
266 	uint8_t	write_open:1;
267 	uint8_t	unused:6;
268 };
269 
270 struct umidi_chan {
271 
272 	struct umidi_sub_chan sub[UMIDI_CABLES_MAX];
273 	struct mtx mtx;
274 
275 	struct usb_xfer *xfer[UMIDI_N_TRANSFER];
276 
277 	uint8_t	iface_index;
278 	uint8_t	iface_alt_index;
279 
280 	uint8_t	read_open_refcount;
281 	uint8_t	write_open_refcount;
282 
283 	uint8_t	curr_cable;
284 	uint8_t	max_cable;
285 	uint8_t	valid;
286 	uint8_t single_command;
287 };
288 
289 struct uaudio_search_result {
290 	uint8_t	bit_input[(256 + 7) / 8];
291 	uint8_t	bit_output[(256 + 7) / 8];
292 	uint8_t	recurse_level;
293 	uint8_t	id_max;
294 	uint8_t is_input;
295 };
296 
297 enum {
298 	UAUDIO_HID_RX_TRANSFER,
299 	UAUDIO_HID_N_TRANSFER,
300 };
301 
302 struct uaudio_hid {
303 	struct usb_xfer *xfer[UAUDIO_HID_N_TRANSFER];
304 	struct hid_location volume_up_loc;
305 	struct hid_location volume_down_loc;
306 	struct hid_location mute_loc;
307 	uint32_t flags;
308 #define	UAUDIO_HID_VALID		0x0001
309 #define	UAUDIO_HID_HAS_ID		0x0002
310 #define	UAUDIO_HID_HAS_VOLUME_UP	0x0004
311 #define	UAUDIO_HID_HAS_VOLUME_DOWN	0x0008
312 #define	UAUDIO_HID_HAS_MUTE		0x0010
313 	uint8_t iface_index;
314 	uint8_t volume_up_id;
315 	uint8_t volume_down_id;
316 	uint8_t mute_id;
317 };
318 
319 struct uaudio_softc {
320 	struct sbuf sc_sndstat;
321 	struct sndcard_func sc_sndcard_func;
322 	struct uaudio_chan sc_rec_chan;
323 	struct uaudio_chan sc_play_chan;
324 	struct umidi_chan sc_midi_chan;
325 	struct uaudio_hid sc_hid;
326 	struct uaudio_search_result sc_mixer_clocks;
327 	struct uaudio_mixer_node sc_mixer_node;
328 	struct uaudio_configure_msg sc_config_msg[2];
329 
330 	struct mtx *sc_mixer_lock;
331 	struct snd_mixer *sc_mixer_dev;
332 	struct usb_device *sc_udev;
333 	struct usb_xfer *sc_mixer_xfer[1];
334 	struct uaudio_mixer_node *sc_mixer_root;
335 	struct uaudio_mixer_node *sc_mixer_curr;
336 
337 	uint32_t sc_mix_info;
338 	uint32_t sc_recsrc_info;
339 
340 	uint16_t sc_audio_rev;
341 	uint16_t sc_mixer_count;
342 
343 	uint8_t	sc_sndstat_valid;
344 	uint8_t	sc_mixer_iface_index;
345 	uint8_t	sc_mixer_iface_no;
346 	uint8_t	sc_mixer_chan;
347 	uint8_t	sc_pcm_registered:1;
348 	uint8_t	sc_mixer_init:1;
349 	uint8_t	sc_uq_audio_swap_lr:1;
350 	uint8_t	sc_uq_au_inp_async:1;
351 	uint8_t	sc_uq_au_no_xu:1;
352 	uint8_t	sc_uq_bad_adc:1;
353 	uint8_t	sc_uq_au_vendor_class:1;
354 };
355 
356 struct uaudio_terminal_node {
357 	union {
358 		const struct usb_descriptor *desc;
359 		const struct usb_audio_input_terminal *it_v1;
360 		const struct usb_audio_output_terminal *ot_v1;
361 		const struct usb_audio_mixer_unit_0 *mu_v1;
362 		const struct usb_audio_selector_unit *su_v1;
363 		const struct usb_audio_feature_unit *fu_v1;
364 		const struct usb_audio_processing_unit_0 *pu_v1;
365 		const struct usb_audio_extension_unit_0 *eu_v1;
366 		const struct usb_audio20_clock_source_unit *csrc_v2;
367 		const struct usb_audio20_clock_selector_unit_0 *csel_v2;
368 		const struct usb_audio20_clock_multiplier_unit *cmul_v2;
369 		const struct usb_audio20_input_terminal *it_v2;
370 		const struct usb_audio20_output_terminal *ot_v2;
371 		const struct usb_audio20_mixer_unit_0 *mu_v2;
372 		const struct usb_audio20_selector_unit *su_v2;
373 		const struct usb_audio20_feature_unit *fu_v2;
374 		const struct usb_audio20_sample_rate_unit *ru_v2;
375 		const struct usb_audio20_processing_unit_0 *pu_v2;
376 		const struct usb_audio20_extension_unit_0 *eu_v2;
377 		const struct usb_audio20_effect_unit *ef_v2;
378 	}	u;
379 	struct uaudio_search_result usr;
380 	struct uaudio_terminal_node *root;
381 };
382 
383 struct uaudio_format {
384 	uint16_t wFormat;
385 	uint8_t	bPrecision;
386 	uint32_t freebsd_fmt;
387 	const char *description;
388 };
389 
390 static const struct uaudio_format uaudio10_formats[] = {
391 
392 	{UA_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"},
393 	{UA_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"},
394 	{UA_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"},
395 	{UA_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"},
396 
397 	{UA_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"},
398 	{UA_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"},
399 	{UA_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"},
400 	{UA_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"},
401 
402 	{UA_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"},
403 	{UA_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"},
404 
405 	{0, 0, 0, NULL}
406 };
407 
408 static const struct uaudio_format uaudio20_formats[] = {
409 
410 	{UA20_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"},
411 	{UA20_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"},
412 	{UA20_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"},
413 	{UA20_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"},
414 
415 	{UA20_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"},
416 	{UA20_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"},
417 	{UA20_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"},
418 	{UA20_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"},
419 
420 	{UA20_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"},
421 	{UA20_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"},
422 
423 	{0, 0, 0, NULL}
424 };
425 
426 #define	UAC_OUTPUT	0
427 #define	UAC_INPUT	1
428 #define	UAC_EQUAL	2
429 #define	UAC_RECORD	3
430 #define	UAC_NCLASSES	4
431 
432 #ifdef USB_DEBUG
433 static const char *uac_names[] = {
434 	"outputs", "inputs", "equalization", "record"
435 };
436 
437 #endif
438 
439 /* prototypes */
440 
441 static device_probe_t uaudio_probe;
442 static device_attach_t uaudio_attach;
443 static device_detach_t uaudio_detach;
444 
445 static usb_callback_t uaudio_chan_play_callback;
446 static usb_callback_t uaudio_chan_play_sync_callback;
447 static usb_callback_t uaudio_chan_record_callback;
448 static usb_callback_t uaudio_chan_record_sync_callback;
449 static usb_callback_t uaudio_mixer_write_cfg_callback;
450 static usb_callback_t umidi_bulk_read_callback;
451 static usb_callback_t umidi_bulk_write_callback;
452 static usb_callback_t uaudio_hid_rx_callback;
453 
454 static usb_proc_callback_t uaudio_configure_msg;
455 
456 /* ==== USB mixer ==== */
457 
458 static int uaudio_mixer_sysctl_handler(SYSCTL_HANDLER_ARGS);
459 static void uaudio_mixer_ctl_free(struct uaudio_softc *);
460 static void uaudio_mixer_register_sysctl(struct uaudio_softc *, device_t);
461 static void uaudio_mixer_reload_all(struct uaudio_softc *);
462 static void uaudio_mixer_controls_create_ftu(struct uaudio_softc *);
463 
464 /* ==== USB audio v1.0 ==== */
465 
466 static void	uaudio_mixer_add_mixer(struct uaudio_softc *,
467 		    const struct uaudio_terminal_node *, int);
468 static void	uaudio_mixer_add_selector(struct uaudio_softc *,
469 		    const struct uaudio_terminal_node *, int);
470 static uint32_t	uaudio_mixer_feature_get_bmaControls(
471 		    const struct usb_audio_feature_unit *, uint8_t);
472 static void	uaudio_mixer_add_feature(struct uaudio_softc *,
473 		    const struct uaudio_terminal_node *, int);
474 static void	uaudio_mixer_add_processing_updown(struct uaudio_softc *,
475 		    const struct uaudio_terminal_node *, int);
476 static void	uaudio_mixer_add_processing(struct uaudio_softc *,
477 		    const struct uaudio_terminal_node *, int);
478 static void	uaudio_mixer_add_extension(struct uaudio_softc *,
479 		    const struct uaudio_terminal_node *, int);
480 static struct	usb_audio_cluster uaudio_mixer_get_cluster(uint8_t,
481 		    const struct uaudio_terminal_node *);
482 static uint16_t	uaudio_mixer_determine_class(const struct uaudio_terminal_node *,
483 		    struct uaudio_mixer_node *);
484 static uint16_t	uaudio_mixer_feature_name(const struct uaudio_terminal_node *,
485 		    struct uaudio_mixer_node *);
486 static void	uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *,
487 		    const uint8_t *, uint8_t, struct uaudio_search_result *);
488 static const void *uaudio_mixer_verify_desc(const void *, uint32_t);
489 static usb_error_t uaudio_set_speed(struct usb_device *, uint8_t, uint32_t);
490 static int	uaudio_mixer_get(struct usb_device *, uint16_t, uint8_t,
491 		    struct uaudio_mixer_node *);
492 
493 /* ==== USB audio v2.0 ==== */
494 
495 static void	uaudio20_mixer_add_mixer(struct uaudio_softc *,
496 		    const struct uaudio_terminal_node *, int);
497 static void	uaudio20_mixer_add_selector(struct uaudio_softc *,
498 		    const struct uaudio_terminal_node *, int);
499 static void	uaudio20_mixer_add_feature(struct uaudio_softc *,
500 		    const struct uaudio_terminal_node *, int);
501 static struct	usb_audio20_cluster uaudio20_mixer_get_cluster(uint8_t,
502 		    const struct uaudio_terminal_node *);
503 static uint16_t	uaudio20_mixer_determine_class(const struct uaudio_terminal_node *,
504 		    struct uaudio_mixer_node *);
505 static uint16_t	uaudio20_mixer_feature_name(const struct uaudio_terminal_node *,
506 		    struct uaudio_mixer_node *);
507 static void	uaudio20_mixer_find_inputs_sub(struct uaudio_terminal_node *,
508 		    const uint8_t *, uint8_t, struct uaudio_search_result *);
509 static const void *uaudio20_mixer_verify_desc(const void *, uint32_t);
510 static usb_error_t uaudio20_set_speed(struct usb_device *, uint8_t,
511 		    uint8_t, uint32_t);
512 
513 /* USB audio v1.0 and v2.0 */
514 
515 static void	uaudio_chan_fill_info_sub(struct uaudio_softc *,
516 		    struct usb_device *, uint32_t, uint8_t, uint8_t);
517 static void	uaudio_chan_fill_info(struct uaudio_softc *,
518 		    struct usb_device *);
519 static void	uaudio_mixer_add_ctl_sub(struct uaudio_softc *,
520 		    struct uaudio_mixer_node *);
521 static void	uaudio_mixer_add_ctl(struct uaudio_softc *,
522 		    struct uaudio_mixer_node *);
523 static void	uaudio_mixer_fill_info(struct uaudio_softc *,
524 		    struct usb_device *, void *);
525 static void	uaudio_mixer_ctl_set(struct uaudio_softc *,
526 		    struct uaudio_mixer_node *, uint8_t, int32_t val);
527 static int	uaudio_mixer_signext(uint8_t, int);
528 static int	uaudio_mixer_bsd2value(struct uaudio_mixer_node *, int32_t val);
529 static void	uaudio_mixer_init(struct uaudio_softc *);
530 static const struct uaudio_terminal_node *uaudio_mixer_get_input(
531 		    const struct uaudio_terminal_node *, uint8_t);
532 static const struct uaudio_terminal_node *uaudio_mixer_get_output(
533 		    const struct uaudio_terminal_node *, uint8_t);
534 static void	uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *,
535 		    uint8_t, uint8_t, struct uaudio_search_result *);
536 static uint8_t	umidi_convert_to_usb(struct umidi_sub_chan *, uint8_t, uint8_t);
537 static struct	umidi_sub_chan *umidi_sub_by_fifo(struct usb_fifo *);
538 static void	umidi_start_read(struct usb_fifo *);
539 static void	umidi_stop_read(struct usb_fifo *);
540 static void	umidi_start_write(struct usb_fifo *);
541 static void	umidi_stop_write(struct usb_fifo *);
542 static int	umidi_open(struct usb_fifo *, int);
543 static int	umidi_ioctl(struct usb_fifo *, u_long cmd, void *, int);
544 static void	umidi_close(struct usb_fifo *, int);
545 static void	umidi_init(device_t dev);
546 static int	umidi_probe(device_t dev);
547 static int	umidi_detach(device_t dev);
548 static int	uaudio_hid_probe(struct uaudio_softc *sc,
549 		    struct usb_attach_arg *uaa);
550 static void	uaudio_hid_detach(struct uaudio_softc *sc);
551 
552 #ifdef USB_DEBUG
553 static void	uaudio_chan_dump_ep_desc(
554 		    const usb_endpoint_descriptor_audio_t *);
555 #endif
556 
557 static const struct usb_config
558 	uaudio_cfg_record[UAUDIO_NCHANBUFS + 1] = {
559 	[0] = {
560 		.type = UE_ISOCHRONOUS,
561 		.endpoint = UE_ADDR_ANY,
562 		.direction = UE_DIR_IN,
563 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
564 		.frames = UAUDIO_NFRAMES,
565 		.flags = {.short_xfer_ok = 1,},
566 		.callback = &uaudio_chan_record_callback,
567 	},
568 
569 	[1] = {
570 		.type = UE_ISOCHRONOUS,
571 		.endpoint = UE_ADDR_ANY,
572 		.direction = UE_DIR_IN,
573 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
574 		.frames = UAUDIO_NFRAMES,
575 		.flags = {.short_xfer_ok = 1,},
576 		.callback = &uaudio_chan_record_callback,
577 	},
578 
579 	[2] = {
580 		.type = UE_ISOCHRONOUS,
581 		.endpoint = UE_ADDR_ANY,
582 		.direction = UE_DIR_OUT,
583 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
584 		.frames = 1,
585 		.flags = {.no_pipe_ok = 1,.short_xfer_ok = 1,},
586 		.callback = &uaudio_chan_record_sync_callback,
587 	},
588 };
589 
590 static const struct usb_config
591 	uaudio_cfg_play[UAUDIO_NCHANBUFS + 1] = {
592 	[0] = {
593 		.type = UE_ISOCHRONOUS,
594 		.endpoint = UE_ADDR_ANY,
595 		.direction = UE_DIR_OUT,
596 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
597 		.frames = UAUDIO_NFRAMES,
598 		.flags = {.short_xfer_ok = 1,},
599 		.callback = &uaudio_chan_play_callback,
600 	},
601 
602 	[1] = {
603 		.type = UE_ISOCHRONOUS,
604 		.endpoint = UE_ADDR_ANY,
605 		.direction = UE_DIR_OUT,
606 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
607 		.frames = UAUDIO_NFRAMES,
608 		.flags = {.short_xfer_ok = 1,},
609 		.callback = &uaudio_chan_play_callback,
610 	},
611 
612 	[2] = {
613 		.type = UE_ISOCHRONOUS,
614 		.endpoint = UE_ADDR_ANY,
615 		.direction = UE_DIR_IN,
616 		.bufsize = 0,	/* use "wMaxPacketSize * frames" */
617 		.frames = 1,
618 		.flags = {.no_pipe_ok = 1,.short_xfer_ok = 1,},
619 		.callback = &uaudio_chan_play_sync_callback,
620 	},
621 };
622 
623 static const struct usb_config
624 	uaudio_mixer_config[1] = {
625 	[0] = {
626 		.type = UE_CONTROL,
627 		.endpoint = 0x00,	/* Control pipe */
628 		.direction = UE_DIR_ANY,
629 		.bufsize = (sizeof(struct usb_device_request) + 4),
630 		.callback = &uaudio_mixer_write_cfg_callback,
631 		.timeout = 1000,	/* 1 second */
632 	},
633 };
634 
635 static const
636 uint8_t	umidi_cmd_to_len[16] = {
637 	[0x0] = 0,			/* reserved */
638 	[0x1] = 0,			/* reserved */
639 	[0x2] = 2,			/* bytes */
640 	[0x3] = 3,			/* bytes */
641 	[0x4] = 3,			/* bytes */
642 	[0x5] = 1,			/* bytes */
643 	[0x6] = 2,			/* bytes */
644 	[0x7] = 3,			/* bytes */
645 	[0x8] = 3,			/* bytes */
646 	[0x9] = 3,			/* bytes */
647 	[0xA] = 3,			/* bytes */
648 	[0xB] = 3,			/* bytes */
649 	[0xC] = 2,			/* bytes */
650 	[0xD] = 2,			/* bytes */
651 	[0xE] = 3,			/* bytes */
652 	[0xF] = 1,			/* bytes */
653 };
654 
655 static const struct usb_config
656 	umidi_config[UMIDI_N_TRANSFER] = {
657 	[UMIDI_TX_TRANSFER] = {
658 		.type = UE_BULK,
659 		.endpoint = UE_ADDR_ANY,
660 		.direction = UE_DIR_OUT,
661 		.bufsize = UMIDI_TX_BUFFER,
662 		.callback = &umidi_bulk_write_callback,
663 	},
664 
665 	[UMIDI_RX_TRANSFER] = {
666 		.type = UE_BULK,
667 		.endpoint = UE_ADDR_ANY,
668 		.direction = UE_DIR_IN,
669 		.bufsize = 4,	/* bytes */
670 		.flags = {.short_xfer_ok = 1,.proxy_buffer = 1,},
671 		.callback = &umidi_bulk_read_callback,
672 	},
673 };
674 
675 static const struct usb_config
676 	uaudio_hid_config[UAUDIO_HID_N_TRANSFER] = {
677 	[UAUDIO_HID_RX_TRANSFER] = {
678 		.type = UE_INTERRUPT,
679 		.endpoint = UE_ADDR_ANY,
680 		.direction = UE_DIR_IN,
681 		.bufsize = 0,	/* use wMaxPacketSize */
682 		.flags = {.short_xfer_ok = 1,},
683 		.callback = &uaudio_hid_rx_callback,
684 	},
685 };
686 
687 static devclass_t uaudio_devclass;
688 
689 static device_method_t uaudio_methods[] = {
690 	DEVMETHOD(device_probe, uaudio_probe),
691 	DEVMETHOD(device_attach, uaudio_attach),
692 	DEVMETHOD(device_detach, uaudio_detach),
693 	DEVMETHOD(device_suspend, bus_generic_suspend),
694 	DEVMETHOD(device_resume, bus_generic_resume),
695 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
696 
697 	DEVMETHOD_END
698 };
699 
700 static driver_t uaudio_driver = {
701 	.name = "uaudio",
702 	.methods = uaudio_methods,
703 	.size = sizeof(struct uaudio_softc),
704 };
705 
706 /* The following table is derived from Linux's quirks-table.h */
707 static const STRUCT_USB_HOST_ID uaudio_vendor_midi[] = {
708 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1000, 0) }, /* UX256 */
709 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1001, 0) }, /* MU1000 */
710 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1002, 0) }, /* MU2000 */
711 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1003, 0) }, /* MU500 */
712 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1004, 3) }, /* UW500 */
713 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1005, 0) }, /* MOTIF6 */
714 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1006, 0) }, /* MOTIF7 */
715 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1007, 0) }, /* MOTIF8 */
716 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1008, 0) }, /* UX96 */
717 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1009, 0) }, /* UX16 */
718 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x100a, 3) }, /* EOS BX */
719 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x100c, 0) }, /* UC-MX */
720 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x100d, 0) }, /* UC-KX */
721 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x100e, 0) }, /* S08 */
722 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x100f, 0) }, /* CLP-150 */
723 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1010, 0) }, /* CLP-170 */
724 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1011, 0) }, /* P-250 */
725 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1012, 0) }, /* TYROS */
726 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1013, 0) }, /* PF-500 */
727 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1014, 0) }, /* S90 */
728 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1015, 0) }, /* MOTIF-R */
729 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1016, 0) }, /* MDP-5 */
730 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1017, 0) }, /* CVP-204 */
731 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1018, 0) }, /* CVP-206 */
732 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1019, 0) }, /* CVP-208 */
733 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101a, 0) }, /* CVP-210 */
734 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101b, 0) }, /* PSR-1100 */
735 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101c, 0) }, /* PSR-2100 */
736 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101d, 0) }, /* CLP-175 */
737 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101e, 0) }, /* PSR-K1 */
738 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x101f, 0) }, /* EZ-J24 */
739 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1020, 0) }, /* EZ-250i */
740 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1021, 0) }, /* MOTIF ES 6 */
741 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1022, 0) }, /* MOTIF ES 7 */
742 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1023, 0) }, /* MOTIF ES 8 */
743 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1024, 0) }, /* CVP-301 */
744 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1025, 0) }, /* CVP-303 */
745 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1026, 0) }, /* CVP-305 */
746 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1027, 0) }, /* CVP-307 */
747 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1028, 0) }, /* CVP-309 */
748 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1029, 0) }, /* CVP-309GP */
749 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x102a, 0) }, /* PSR-1500 */
750 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x102b, 0) }, /* PSR-3000 */
751 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x102e, 0) }, /* ELS-01/01C */
752 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1030, 0) }, /* PSR-295/293 */
753 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1031, 0) }, /* DGX-205/203 */
754 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1032, 0) }, /* DGX-305 */
755 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1033, 0) }, /* DGX-505 */
756 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1034, 0) }, /* NULL */
757 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1035, 0) }, /* NULL */
758 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1036, 0) }, /* NULL */
759 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1037, 0) }, /* NULL */
760 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1038, 0) }, /* NULL */
761 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1039, 0) }, /* NULL */
762 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103a, 0) }, /* NULL */
763 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103b, 0) }, /* NULL */
764 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103c, 0) }, /* NULL */
765 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103d, 0) }, /* NULL */
766 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103e, 0) }, /* NULL */
767 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x103f, 0) }, /* NULL */
768 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1040, 0) }, /* NULL */
769 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1041, 0) }, /* NULL */
770 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1042, 0) }, /* NULL */
771 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1043, 0) }, /* NULL */
772 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1044, 0) }, /* NULL */
773 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1045, 0) }, /* NULL */
774 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x104e, 0) }, /* NULL */
775 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x104f, 0) }, /* NULL */
776 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1050, 0) }, /* NULL */
777 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1051, 0) }, /* NULL */
778 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1052, 0) }, /* NULL */
779 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1053, 0) }, /* NULL */
780 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1054, 0) }, /* NULL */
781 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1055, 0) }, /* NULL */
782 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1056, 0) }, /* NULL */
783 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1057, 0) }, /* NULL */
784 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1058, 0) }, /* NULL */
785 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1059, 0) }, /* NULL */
786 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x105a, 0) }, /* NULL */
787 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x105b, 0) }, /* NULL */
788 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x105c, 0) }, /* NULL */
789 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x105d, 0) }, /* NULL */
790 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x1503, 3) }, /* MOX6/MOX8 */
791 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x2000, 0) }, /* DGP-7 */
792 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x2001, 0) }, /* DGP-5 */
793 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x2002, 0) }, /* NULL */
794 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x2003, 0) }, /* NULL */
795 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5000, 0) }, /* CS1D */
796 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5001, 0) }, /* DSP1D */
797 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5002, 0) }, /* DME32 */
798 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5003, 0) }, /* DM2000 */
799 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5004, 0) }, /* 02R96 */
800 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5005, 0) }, /* ACU16-C */
801 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5006, 0) }, /* NHB32-C */
802 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5007, 0) }, /* DM1000 */
803 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5008, 0) }, /* 01V96 */
804 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x5009, 0) }, /* SPX2000 */
805 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500a, 0) }, /* PM5D */
806 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500b, 0) }, /* DME64N */
807 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500c, 0) }, /* DME24N */
808 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500d, 0) }, /* NULL */
809 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500e, 0) }, /* NULL */
810 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x500f, 0) }, /* NULL */
811 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x7000, 0) }, /* DTX */
812 	{ USB_VPI(USB_VENDOR_YAMAHA, 0x7010, 0) }, /* UB99 */
813 };
814 
815 static const STRUCT_USB_HOST_ID __used uaudio_devs[] = {
816 	/* Generic USB audio class match */
817 	{USB_IFACE_CLASS(UICLASS_AUDIO),
818 	 USB_IFACE_SUBCLASS(UISUBCLASS_AUDIOCONTROL),},
819 	/* Generic USB MIDI class match */
820 	{USB_IFACE_CLASS(UICLASS_AUDIO),
821 	 USB_IFACE_SUBCLASS(UISUBCLASS_MIDISTREAM),},
822 };
823 
824 static int
825 uaudio_probe(device_t dev)
826 {
827 	struct usb_attach_arg *uaa = device_get_ivars(dev);
828 
829 	if (uaa->usb_mode != USB_MODE_HOST)
830 		return (ENXIO);
831 
832 	/* lookup non-standard device(s) */
833 
834 	if (usbd_lookup_id_by_uaa(uaudio_vendor_midi,
835 	    sizeof(uaudio_vendor_midi), uaa) == 0) {
836 		return (BUS_PROBE_SPECIFIC);
837 	}
838 
839 	if (uaa->info.bInterfaceClass != UICLASS_AUDIO) {
840 		if (uaa->info.bInterfaceClass != UICLASS_VENDOR ||
841 		    usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS) == 0)
842 			return (ENXIO);
843 	}
844 
845 	/* check for AUDIO control interface */
846 
847 	if (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL) {
848 		if (usb_test_quirk(uaa, UQ_BAD_AUDIO))
849 			return (ENXIO);
850 		else
851 			return (BUS_PROBE_GENERIC);
852 	}
853 
854 	/* check for MIDI stream */
855 
856 	if (uaa->info.bInterfaceSubClass == UISUBCLASS_MIDISTREAM) {
857 		if (usb_test_quirk(uaa, UQ_BAD_MIDI))
858 			return (ENXIO);
859 		else
860 			return (BUS_PROBE_GENERIC);
861 	}
862 	return (ENXIO);
863 }
864 
865 static int
866 uaudio_attach(device_t dev)
867 {
868 	struct usb_attach_arg *uaa = device_get_ivars(dev);
869 	struct uaudio_softc *sc = device_get_softc(dev);
870 	struct usb_interface_descriptor *id;
871 	usb_error_t err;
872 	device_t child;
873 
874 	sc->sc_play_chan.priv_sc = sc;
875 	sc->sc_rec_chan.priv_sc = sc;
876 	sc->sc_udev = uaa->device;
877 	sc->sc_mixer_iface_index = uaa->info.bIfaceIndex;
878 	sc->sc_mixer_iface_no = uaa->info.bIfaceNum;
879 	sc->sc_config_msg[0].hdr.pm_callback = &uaudio_configure_msg;
880 	sc->sc_config_msg[0].sc = sc;
881 	sc->sc_config_msg[1].hdr.pm_callback = &uaudio_configure_msg;
882 	sc->sc_config_msg[1].sc = sc;
883 
884 	if (usb_test_quirk(uaa, UQ_AUDIO_SWAP_LR))
885 		sc->sc_uq_audio_swap_lr = 1;
886 
887 	if (usb_test_quirk(uaa, UQ_AU_INP_ASYNC))
888 		sc->sc_uq_au_inp_async = 1;
889 
890 	if (usb_test_quirk(uaa, UQ_AU_NO_XU))
891 		sc->sc_uq_au_no_xu = 1;
892 
893 	if (usb_test_quirk(uaa, UQ_BAD_ADC))
894 		sc->sc_uq_bad_adc = 1;
895 
896 	if (usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS))
897 		sc->sc_uq_au_vendor_class = 1;
898 
899 	umidi_init(dev);
900 
901 	device_set_usb_desc(dev);
902 
903 	id = usbd_get_interface_descriptor(uaa->iface);
904 
905 	/* must fill mixer info before channel info */
906 	uaudio_mixer_fill_info(sc, uaa->device, id);
907 
908 	/* fill channel info */
909 	uaudio_chan_fill_info(sc, uaa->device);
910 
911 	DPRINTF("audio rev %d.%02x\n",
912 	    sc->sc_audio_rev >> 8,
913 	    sc->sc_audio_rev & 0xff);
914 
915 	if (sc->sc_mixer_count == 0) {
916 		if (uaa->info.idVendor == USB_VENDOR_MAUDIO &&
917 		    (uaa->info.idProduct == USB_PRODUCT_MAUDIO_FASTTRACKULTRA ||
918 		    uaa->info.idProduct == USB_PRODUCT_MAUDIO_FASTTRACKULTRA8R)) {
919 			DPRINTF("Generating mixer descriptors\n");
920 			uaudio_mixer_controls_create_ftu(sc);
921 		}
922 	}
923 
924 	DPRINTF("%d mixer controls\n",
925 	    sc->sc_mixer_count);
926 
927 	if (sc->sc_play_chan.num_alt > 0) {
928 		uint8_t x;
929 
930 		/*
931 		 * Need to set a default alternate interface, else
932 		 * some USB audio devices might go into an infinte
933 		 * re-enumeration loop:
934 		 */
935 		err = usbd_set_alt_interface_index(sc->sc_udev,
936 		    sc->sc_play_chan.usb_alt[0].iface_index,
937 		    sc->sc_play_chan.usb_alt[0].iface_alt_index);
938 		if (err) {
939 			DPRINTF("setting of alternate index failed: %s!\n",
940 			    usbd_errstr(err));
941 		}
942 		for (x = 0; x != sc->sc_play_chan.num_alt; x++) {
943 			device_printf(dev, "Play: %d Hz, %d ch, %s format, "
944 			    "2x8ms buffer.\n",
945 			    sc->sc_play_chan.usb_alt[x].sample_rate,
946 			    sc->sc_play_chan.usb_alt[x].channels,
947 			    sc->sc_play_chan.usb_alt[x].p_fmt->description);
948 		}
949 	} else {
950 		device_printf(dev, "No playback.\n");
951 	}
952 
953 	if (sc->sc_rec_chan.num_alt > 0) {
954 		uint8_t x;
955 
956 		/*
957 		 * Need to set a default alternate interface, else
958 		 * some USB audio devices might go into an infinte
959 		 * re-enumeration loop:
960 		 */
961 		err = usbd_set_alt_interface_index(sc->sc_udev,
962 		    sc->sc_rec_chan.usb_alt[0].iface_index,
963 		    sc->sc_rec_chan.usb_alt[0].iface_alt_index);
964 		if (err) {
965 			DPRINTF("setting of alternate index failed: %s!\n",
966 			    usbd_errstr(err));
967 		}
968 		for (x = 0; x != sc->sc_rec_chan.num_alt; x++) {
969 			device_printf(dev, "Record: %d Hz, %d ch, %s format, "
970 			    "2x8ms buffer.\n",
971 			    sc->sc_rec_chan.usb_alt[x].sample_rate,
972 			    sc->sc_rec_chan.usb_alt[x].channels,
973 			    sc->sc_rec_chan.usb_alt[x].p_fmt->description);
974 		}
975 	} else {
976 		device_printf(dev, "No recording.\n");
977 	}
978 
979 	if (sc->sc_midi_chan.valid == 0) {
980 		if (usbd_lookup_id_by_uaa(uaudio_vendor_midi,
981 		    sizeof(uaudio_vendor_midi), uaa) == 0) {
982 			sc->sc_midi_chan.iface_index =
983 			    (uint8_t)uaa->driver_info;
984 			sc->sc_midi_chan.iface_alt_index = 0;
985 			sc->sc_midi_chan.valid = 1;
986 		}
987 	}
988 
989 	if (sc->sc_midi_chan.valid) {
990 
991 		if (umidi_probe(dev)) {
992 			goto detach;
993 		}
994 		device_printf(dev, "MIDI sequencer.\n");
995 	} else {
996 		device_printf(dev, "No MIDI sequencer.\n");
997 	}
998 
999 	DPRINTF("doing child attach\n");
1000 
1001 	/* attach the children */
1002 
1003 	sc->sc_sndcard_func.func = SCF_PCM;
1004 
1005 	/*
1006 	 * Only attach a PCM device if we have a playback, recording
1007 	 * or mixer device present:
1008 	 */
1009 	if (sc->sc_play_chan.num_alt > 0 ||
1010 	    sc->sc_rec_chan.num_alt > 0 ||
1011 	    sc->sc_mix_info) {
1012 		child = device_add_child(dev, "pcm", -1);
1013 
1014 		if (child == NULL) {
1015 			DPRINTF("out of memory\n");
1016 			goto detach;
1017 		}
1018 		device_set_ivars(child, &sc->sc_sndcard_func);
1019 	}
1020 
1021 	if (bus_generic_attach(dev)) {
1022 		DPRINTF("child attach failed\n");
1023 		goto detach;
1024 	}
1025 
1026 	if (uaudio_hid_probe(sc, uaa) == 0) {
1027 		device_printf(dev, "HID volume keys found.\n");
1028 	} else {
1029 		device_printf(dev, "No HID volume keys found.\n");
1030 	}
1031 
1032 	/* reload all mixer settings */
1033 	uaudio_mixer_reload_all(sc);
1034 
1035 	return (0);			/* success */
1036 
1037 detach:
1038 	uaudio_detach(dev);
1039 	return (ENXIO);
1040 }
1041 
1042 static void
1043 uaudio_pcm_setflags(device_t dev, uint32_t flags)
1044 {
1045 	pcm_setflags(dev, pcm_getflags(dev) | flags);
1046 }
1047 
1048 int
1049 uaudio_attach_sub(device_t dev, kobj_class_t mixer_class, kobj_class_t chan_class)
1050 {
1051 	struct uaudio_softc *sc = device_get_softc(device_get_parent(dev));
1052 	char status[SND_STATUSLEN];
1053 
1054 	uaudio_mixer_init(sc);
1055 
1056 	if (sc->sc_uq_audio_swap_lr) {
1057 		DPRINTF("hardware has swapped left and right\n");
1058 		/* uaudio_pcm_setflags(dev, SD_F_PSWAPLR); */
1059 	}
1060 	if (!(sc->sc_mix_info & SOUND_MASK_PCM)) {
1061 
1062 		DPRINTF("emulating master volume\n");
1063 
1064 		/*
1065 		 * Emulate missing pcm mixer controller
1066 		 * through FEEDER_VOLUME
1067 		 */
1068 		uaudio_pcm_setflags(dev, SD_F_SOFTPCMVOL);
1069 	}
1070 	if (mixer_init(dev, mixer_class, sc))
1071 		goto detach;
1072 	sc->sc_mixer_init = 1;
1073 
1074 	mixer_hwvol_init(dev);
1075 
1076 	snprintf(status, sizeof(status), "at ? %s", PCM_KLDSTRING(snd_uaudio));
1077 
1078 	if (pcm_register(dev, sc,
1079 	    (sc->sc_play_chan.num_alt > 0) ? 1 : 0,
1080 	    (sc->sc_rec_chan.num_alt > 0) ? 1 : 0)) {
1081 		goto detach;
1082 	}
1083 
1084 	uaudio_pcm_setflags(dev, SD_F_MPSAFE);
1085 	sc->sc_pcm_registered = 1;
1086 
1087 	if (sc->sc_play_chan.num_alt > 0) {
1088 		pcm_addchan(dev, PCMDIR_PLAY, chan_class, sc);
1089 	}
1090 	if (sc->sc_rec_chan.num_alt > 0) {
1091 		pcm_addchan(dev, PCMDIR_REC, chan_class, sc);
1092 	}
1093 	pcm_setstatus(dev, status);
1094 
1095 	uaudio_mixer_register_sysctl(sc, dev);
1096 
1097 	return (0);			/* success */
1098 
1099 detach:
1100 	uaudio_detach_sub(dev);
1101 	return (ENXIO);
1102 }
1103 
1104 int
1105 uaudio_detach_sub(device_t dev)
1106 {
1107 	struct uaudio_softc *sc = device_get_softc(device_get_parent(dev));
1108 	int error = 0;
1109 
1110 repeat:
1111 	if (sc->sc_pcm_registered) {
1112 		error = pcm_unregister(dev);
1113 	} else {
1114 		if (sc->sc_mixer_init) {
1115 			error = mixer_uninit(dev);
1116 		}
1117 	}
1118 
1119 	if (error) {
1120 		device_printf(dev, "Waiting for sound application to exit!\n");
1121 		usb_pause_mtx(NULL, 2 * hz);
1122 		goto repeat;		/* try again */
1123 	}
1124 	return (0);			/* success */
1125 }
1126 
1127 static int
1128 uaudio_detach(device_t dev)
1129 {
1130 	struct uaudio_softc *sc = device_get_softc(dev);
1131 
1132 	/*
1133 	 * Stop USB transfers early so that any audio applications
1134 	 * will time out and close opened /dev/dspX.Y device(s), if
1135 	 * any.
1136 	 */
1137 	usb_proc_explore_lock(sc->sc_udev);
1138 	sc->sc_play_chan.operation = CHAN_OP_DRAIN;
1139 	sc->sc_rec_chan.operation = CHAN_OP_DRAIN;
1140 	usb_proc_explore_mwait(sc->sc_udev,
1141 	    &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
1142 	usb_proc_explore_unlock(sc->sc_udev);
1143 
1144 	usbd_transfer_unsetup(sc->sc_play_chan.xfer, UAUDIO_NCHANBUFS + 1);
1145 	usbd_transfer_unsetup(sc->sc_rec_chan.xfer, UAUDIO_NCHANBUFS + 1);
1146 
1147 	uaudio_hid_detach(sc);
1148 
1149 	if (bus_generic_detach(dev) != 0) {
1150 		DPRINTF("detach failed!\n");
1151 	}
1152 	sbuf_delete(&sc->sc_sndstat);
1153 	sc->sc_sndstat_valid = 0;
1154 
1155 	umidi_detach(dev);
1156 
1157 	/* free mixer data */
1158 
1159 	uaudio_mixer_ctl_free(sc);
1160 
1161 	return (0);
1162 }
1163 
1164 static uint32_t
1165 uaudio_get_buffer_size(struct uaudio_chan *ch, uint8_t alt)
1166 {
1167 	struct uaudio_chan_alt *chan_alt = &ch->usb_alt[alt];
1168 	/* We use 2 times 8ms of buffer */
1169 	uint32_t buf_size = (((chan_alt->sample_rate * (UAUDIO_NFRAMES / 8)) +
1170 	    1000 - 1) / 1000) * chan_alt->sample_size;
1171 	return (buf_size);
1172 }
1173 
1174 static void
1175 uaudio_configure_msg_sub(struct uaudio_softc *sc,
1176     struct uaudio_chan *chan, int dir)
1177 {
1178 	struct uaudio_chan_alt *chan_alt;
1179 	uint32_t frames;
1180 	uint32_t buf_size;
1181 	uint16_t fps;
1182 	uint8_t set_alt;
1183 	uint8_t fps_shift;
1184 	uint8_t operation;
1185 	usb_error_t err;
1186 
1187 	if (chan->num_alt <= 0)
1188 		return;
1189 
1190 	DPRINTF("\n");
1191 
1192 	usb_proc_explore_lock(sc->sc_udev);
1193 	operation = chan->operation;
1194 	chan->operation = CHAN_OP_NONE;
1195 	usb_proc_explore_unlock(sc->sc_udev);
1196 
1197 	mtx_lock(chan->pcm_mtx);
1198 	if (chan->cur_alt != chan->set_alt)
1199 		set_alt = chan->set_alt;
1200 	else
1201 		set_alt = CHAN_MAX_ALT;
1202 	mtx_unlock(chan->pcm_mtx);
1203 
1204 	if (set_alt >= chan->num_alt)
1205 		goto done;
1206 
1207 	chan_alt = chan->usb_alt + set_alt;
1208 
1209 	usbd_transfer_unsetup(chan->xfer, UAUDIO_NCHANBUFS + 1);
1210 
1211 	err = usbd_set_alt_interface_index(sc->sc_udev,
1212 	    chan_alt->iface_index, chan_alt->iface_alt_index);
1213 	if (err) {
1214 		DPRINTF("setting of alternate index failed: %s!\n",
1215 		    usbd_errstr(err));
1216 		goto error;
1217 	}
1218 
1219 	/*
1220 	 * Only set the sample rate if the channel reports that it
1221 	 * supports the frequency control.
1222 	 */
1223 
1224 	if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
1225 		/* FALLTHROUGH */
1226 	} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
1227 		unsigned int x;
1228 
1229 		for (x = 0; x != 256; x++) {
1230 			if (dir == PCMDIR_PLAY) {
1231 				if (!(sc->sc_mixer_clocks.bit_output[x / 8] &
1232 				    (1 << (x % 8)))) {
1233 					continue;
1234 				}
1235 			} else {
1236 				if (!(sc->sc_mixer_clocks.bit_input[x / 8] &
1237 				    (1 << (x % 8)))) {
1238 					continue;
1239 				}
1240 			}
1241 
1242 			if (uaudio20_set_speed(sc->sc_udev,
1243 			    sc->sc_mixer_iface_no, x, chan_alt->sample_rate)) {
1244 				/*
1245 				 * If the endpoint is adaptive setting
1246 				 * the speed may fail.
1247 				 */
1248 				DPRINTF("setting of sample rate failed! "
1249 				    "(continuing anyway)\n");
1250 			}
1251 		}
1252 	} else if (chan_alt->p_sed.v1->bmAttributes & UA_SED_FREQ_CONTROL) {
1253 		if (uaudio_set_speed(sc->sc_udev,
1254 		    chan_alt->p_ed1->bEndpointAddress, chan_alt->sample_rate)) {
1255 			/*
1256 			 * If the endpoint is adaptive setting the
1257 			 * speed may fail.
1258 			 */
1259 			DPRINTF("setting of sample rate failed! "
1260 			    "(continuing anyway)\n");
1261 		}
1262 	}
1263 	if (usbd_transfer_setup(sc->sc_udev, &chan_alt->iface_index, chan->xfer,
1264 	    chan_alt->usb_cfg, UAUDIO_NCHANBUFS + 1, chan, chan->pcm_mtx)) {
1265 		DPRINTF("could not allocate USB transfers!\n");
1266 		goto error;
1267 	}
1268 
1269 	fps = usbd_get_isoc_fps(sc->sc_udev);
1270 
1271 	if (fps < 8000) {
1272 		/* FULL speed USB */
1273 		frames = 8;
1274 	} else {
1275 		/* HIGH speed USB */
1276 		frames = UAUDIO_NFRAMES;
1277 	}
1278 
1279 	fps_shift = usbd_xfer_get_fps_shift(chan->xfer[0]);
1280 
1281 	/* down shift number of frames per second, if any */
1282 	fps >>= fps_shift;
1283 	frames >>= fps_shift;
1284 
1285 	/* bytes per frame should not be zero */
1286 	chan->bytes_per_frame[0] =
1287 	    ((chan_alt->sample_rate / fps) * chan_alt->sample_size);
1288 	chan->bytes_per_frame[1] =
1289 	    (((chan_alt->sample_rate + fps - 1) / fps) * chan_alt->sample_size);
1290 
1291 	/* setup data rate dithering, if any */
1292 	chan->frames_per_second = fps;
1293 	chan->sample_rem = chan_alt->sample_rate % fps;
1294 	chan->sample_curr = 0;
1295 	chan->frames_per_second = fps;
1296 
1297 	/* compute required buffer size */
1298 	buf_size = (chan->bytes_per_frame[1] * frames);
1299 
1300 	if (buf_size > (chan->end - chan->start)) {
1301 		DPRINTF("buffer size is too big\n");
1302 		goto error;
1303 	}
1304 
1305 	chan->intr_frames = frames;
1306 
1307 	DPRINTF("fps=%d sample_rem=%d\n", (int)fps, (int)chan->sample_rem);
1308 
1309 	if (chan->intr_frames == 0) {
1310 		DPRINTF("frame shift is too high!\n");
1311 		goto error;
1312 	}
1313 
1314 	mtx_lock(chan->pcm_mtx);
1315 	chan->cur_alt = set_alt;
1316 	mtx_unlock(chan->pcm_mtx);
1317 
1318 done:
1319 #if (UAUDIO_NCHANBUFS != 2)
1320 #error "please update code"
1321 #endif
1322 	switch (operation) {
1323 	case CHAN_OP_START:
1324 		mtx_lock(chan->pcm_mtx);
1325 		usbd_transfer_start(chan->xfer[0]);
1326 		usbd_transfer_start(chan->xfer[1]);
1327 		mtx_unlock(chan->pcm_mtx);
1328 		break;
1329 	case CHAN_OP_STOP:
1330 		mtx_lock(chan->pcm_mtx);
1331 		usbd_transfer_stop(chan->xfer[0]);
1332 		usbd_transfer_stop(chan->xfer[1]);
1333 		mtx_unlock(chan->pcm_mtx);
1334 		break;
1335 	default:
1336 		break;
1337 	}
1338 	return;
1339 
1340 error:
1341 	usbd_transfer_unsetup(chan->xfer, UAUDIO_NCHANBUFS + 1);
1342 
1343 	mtx_lock(chan->pcm_mtx);
1344 	chan->cur_alt = CHAN_MAX_ALT;
1345 	mtx_unlock(chan->pcm_mtx);
1346 }
1347 
1348 static void
1349 uaudio_configure_msg(struct usb_proc_msg *pm)
1350 {
1351 	struct uaudio_softc *sc = ((struct uaudio_configure_msg *)pm)->sc;
1352 
1353 	usb_proc_explore_unlock(sc->sc_udev);
1354 	uaudio_configure_msg_sub(sc, &sc->sc_play_chan, PCMDIR_PLAY);
1355 	uaudio_configure_msg_sub(sc, &sc->sc_rec_chan, PCMDIR_REC);
1356 	usb_proc_explore_lock(sc->sc_udev);
1357 }
1358 
1359 /*========================================================================*
1360  * AS - Audio Stream - routines
1361  *========================================================================*/
1362 
1363 #ifdef USB_DEBUG
1364 static void
1365 uaudio_chan_dump_ep_desc(const usb_endpoint_descriptor_audio_t *ed)
1366 {
1367 	if (ed) {
1368 		DPRINTF("endpoint=%p bLength=%d bDescriptorType=%d \n"
1369 		    "bEndpointAddress=%d bmAttributes=0x%x \n"
1370 		    "wMaxPacketSize=%d bInterval=%d \n"
1371 		    "bRefresh=%d bSynchAddress=%d\n",
1372 		    ed, ed->bLength, ed->bDescriptorType,
1373 		    ed->bEndpointAddress, ed->bmAttributes,
1374 		    UGETW(ed->wMaxPacketSize), ed->bInterval,
1375 		    UEP_HAS_REFRESH(ed) ? ed->bRefresh : 0,
1376 		    UEP_HAS_SYNCADDR(ed) ? ed->bSynchAddress : 0);
1377 	}
1378 }
1379 
1380 #endif
1381 
1382 /*
1383  * The following is a workaround for broken no-name USB audio devices
1384  * sold by dealextreme called "3D sound". The problem is that the
1385  * manufacturer computed wMaxPacketSize is too small to hold the
1386  * actual data sent. In other words the device sometimes sends more
1387  * data than it actually reports it can send in a single isochronous
1388  * packet.
1389  */
1390 static void
1391 uaudio_record_fix_fs(usb_endpoint_descriptor_audio_t *ep,
1392     uint32_t xps, uint32_t add)
1393 {
1394 	uint32_t mps;
1395 
1396 	mps = UGETW(ep->wMaxPacketSize);
1397 
1398 	/*
1399 	 * If the device indicates it can send more data than what the
1400 	 * sample rate indicates, we apply the workaround.
1401 	 */
1402 	if (mps > xps) {
1403 
1404 		/* allow additional data */
1405 		xps += add;
1406 
1407 		/* check against the maximum USB 1.x length */
1408 		if (xps > 1023)
1409 			xps = 1023;
1410 
1411 		/* check if we should do an update */
1412 		if (mps < xps) {
1413 			/* simply update the wMaxPacketSize field */
1414 			USETW(ep->wMaxPacketSize, xps);
1415 			DPRINTF("Workaround: Updated wMaxPacketSize "
1416 			    "from %d to %d bytes.\n",
1417 			    (int)mps, (int)xps);
1418 		}
1419 	}
1420 }
1421 
1422 static usb_error_t
1423 uaudio20_check_rate(struct usb_device *udev, uint8_t iface_no,
1424     uint8_t clockid, uint32_t rate)
1425 {
1426 	struct usb_device_request req;
1427 	usb_error_t error;
1428 	uint8_t data[255];
1429 	uint16_t actlen;
1430 	uint16_t rates;
1431 	uint16_t x;
1432 
1433 	DPRINTFN(6, "ifaceno=%d clockid=%d rate=%u\n",
1434 	    iface_no, clockid, rate);
1435 
1436 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1437 	req.bRequest = UA20_CS_RANGE;
1438 	USETW2(req.wValue, UA20_CS_SAM_FREQ_CONTROL, 0);
1439 	USETW2(req.wIndex, clockid, iface_no);
1440 	USETW(req.wLength, 255);
1441 
1442         error = usbd_do_request_flags(udev, NULL, &req, data,
1443 	    USB_SHORT_XFER_OK, &actlen, USB_DEFAULT_TIMEOUT);
1444 
1445 	if (error != 0 || actlen < 2)
1446 		return (USB_ERR_INVAL);
1447 
1448 	rates = data[0] | (data[1] << 8);
1449 	actlen = (actlen - 2) / 12;
1450 
1451 	if (rates > actlen) {
1452 		DPRINTF("Too many rates\n");
1453 		rates = actlen;
1454 	}
1455 
1456 	for (x = 0; x != rates; x++) {
1457 		uint32_t min = UGETDW(data + 2 + (12 * x));
1458 		uint32_t max = UGETDW(data + 6 + (12 * x));
1459 		uint32_t res = UGETDW(data + 10 + (12 * x));
1460 
1461 		if (res == 0) {
1462 			DPRINTF("Zero residue\n");
1463 			res = 1;
1464 		}
1465 
1466 		if (min > max) {
1467 			DPRINTF("Swapped max and min\n");
1468 			uint32_t temp;
1469 			temp = min;
1470 			min = max;
1471 			max = temp;
1472 		}
1473 
1474 		if (rate >= min && rate <= max &&
1475 		    (((rate - min) % res) == 0)) {
1476 			return (0);
1477 		}
1478 	}
1479 	return (USB_ERR_INVAL);
1480 }
1481 
1482 static void
1483 uaudio_chan_fill_info_sub(struct uaudio_softc *sc, struct usb_device *udev,
1484     uint32_t rate, uint8_t channels, uint8_t bit_resolution)
1485 {
1486 	struct usb_descriptor *desc = NULL;
1487 	union uaudio_asid asid = { NULL };
1488 	union uaudio_asf1d asf1d = { NULL };
1489 	union uaudio_sed sed = { NULL };
1490 	usb_endpoint_descriptor_audio_t *ed1 = NULL;
1491 	const struct usb_audio_control_descriptor *acdp = NULL;
1492 	struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
1493 	struct usb_interface_descriptor *id;
1494 	const struct uaudio_format *p_fmt = NULL;
1495 	struct uaudio_chan *chan;
1496 	struct uaudio_chan_alt *chan_alt;
1497 	uint32_t format;
1498 	uint16_t curidx = 0xFFFF;
1499 	uint16_t lastidx = 0xFFFF;
1500 	uint16_t alt_index = 0;
1501 	uint16_t audio_rev = 0;
1502 	uint16_t x;
1503 	uint8_t ep_dir;
1504 	uint8_t bChannels;
1505 	uint8_t bBitResolution;
1506 	uint8_t audio_if = 0;
1507 	uint8_t uma_if_class;
1508 
1509 	while ((desc = usb_desc_foreach(cd, desc))) {
1510 
1511 		if ((desc->bDescriptorType == UDESC_INTERFACE) &&
1512 		    (desc->bLength >= sizeof(*id))) {
1513 
1514 			id = (void *)desc;
1515 
1516 			if (id->bInterfaceNumber != lastidx) {
1517 				lastidx = id->bInterfaceNumber;
1518 				curidx++;
1519 				alt_index = 0;
1520 
1521 			} else {
1522 				alt_index++;
1523 			}
1524 
1525 			if ((!(sc->sc_hid.flags & UAUDIO_HID_VALID)) &&
1526 			    (id->bInterfaceClass == UICLASS_HID) &&
1527 			    (id->bInterfaceSubClass == 0) &&
1528 			    (id->bInterfaceProtocol == 0) &&
1529 			    (alt_index == 0) &&
1530 			    usbd_get_iface(udev, curidx) != NULL) {
1531 				DPRINTF("Found HID interface at %d\n",
1532 				    curidx);
1533 				sc->sc_hid.flags |= UAUDIO_HID_VALID;
1534 				sc->sc_hid.iface_index = curidx;
1535 			}
1536 
1537 			uma_if_class =
1538 			    ((id->bInterfaceClass == UICLASS_AUDIO) ||
1539 			    ((id->bInterfaceClass == UICLASS_VENDOR) &&
1540 			    (sc->sc_uq_au_vendor_class != 0)));
1541 
1542 			if ((uma_if_class != 0) && (id->bInterfaceSubClass == UISUBCLASS_AUDIOSTREAM)) {
1543 				audio_if = 1;
1544 			} else {
1545 				audio_if = 0;
1546 			}
1547 
1548 			if ((uma_if_class != 0) &&
1549 			    (id->bInterfaceSubClass == UISUBCLASS_MIDISTREAM)) {
1550 
1551 				/*
1552 				 * XXX could allow multiple MIDI interfaces
1553 				 */
1554 
1555 				if ((sc->sc_midi_chan.valid == 0) &&
1556 				    usbd_get_iface(udev, curidx)) {
1557 					sc->sc_midi_chan.iface_index = curidx;
1558 					sc->sc_midi_chan.iface_alt_index = alt_index;
1559 					sc->sc_midi_chan.valid = 1;
1560 				}
1561 			}
1562 			asid.v1 = NULL;
1563 			asf1d.v1 = NULL;
1564 			ed1 = NULL;
1565 			sed.v1 = NULL;
1566 		}
1567 
1568 		if (audio_if == 0) {
1569 			if ((acdp == NULL) &&
1570 			    (desc->bDescriptorType == UDESC_CS_INTERFACE) &&
1571 			    (desc->bDescriptorSubtype == UDESCSUB_AC_HEADER) &&
1572 			    (desc->bLength >= sizeof(*acdp))) {
1573 				acdp = (void *)desc;
1574 				audio_rev = UGETW(acdp->bcdADC);
1575 			}
1576 
1577 			/*
1578 			 * Don't collect any USB audio descriptors if
1579 			 * this is not an USB audio stream interface.
1580 			 */
1581 			continue;
1582 		}
1583 
1584 		if ((acdp != NULL) &&
1585 		    (desc->bDescriptorType == UDESC_CS_INTERFACE) &&
1586 		    (desc->bDescriptorSubtype == AS_GENERAL) &&
1587 		    (asid.v1 == NULL)) {
1588 			if (audio_rev >= UAUDIO_VERSION_30) {
1589 				/* FALLTHROUGH */
1590 			} else if (audio_rev >= UAUDIO_VERSION_20) {
1591 				if (desc->bLength >= sizeof(*asid.v2)) {
1592 					asid.v2 = (void *)desc;
1593 				}
1594 			} else {
1595 				if (desc->bLength >= sizeof(*asid.v1)) {
1596 					asid.v1 = (void *)desc;
1597 				}
1598 			}
1599 		}
1600 		if ((acdp != NULL) &&
1601 		    (desc->bDescriptorType == UDESC_CS_INTERFACE) &&
1602 		    (desc->bDescriptorSubtype == FORMAT_TYPE) &&
1603 		    (asf1d.v1 == NULL)) {
1604 			if (audio_rev >= UAUDIO_VERSION_30) {
1605 				/* FALLTHROUGH */
1606 			} else if (audio_rev >= UAUDIO_VERSION_20) {
1607 				if (desc->bLength >= sizeof(*asf1d.v2))
1608 					asf1d.v2 = (void *)desc;
1609 			} else {
1610 				if (desc->bLength >= sizeof(*asf1d.v1)) {
1611 					asf1d.v1 = (void *)desc;
1612 
1613 					if (asf1d.v1->bFormatType != FORMAT_TYPE_I) {
1614 						DPRINTFN(11, "ignored bFormatType = %d\n",
1615 						    asf1d.v1->bFormatType);
1616 						asf1d.v1 = NULL;
1617 						continue;
1618 					}
1619 					if (desc->bLength < (sizeof(*asf1d.v1) +
1620 					    ((asf1d.v1->bSamFreqType == 0) ? 6 :
1621 					    (asf1d.v1->bSamFreqType * 3)))) {
1622 						DPRINTFN(11, "invalid descriptor, "
1623 						    "too short\n");
1624 						asf1d.v1 = NULL;
1625 						continue;
1626 					}
1627 				}
1628 			}
1629 		}
1630 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1631 		    (desc->bLength >= UEP_MINSIZE) &&
1632 		    (ed1 == NULL)) {
1633 			ed1 = (void *)desc;
1634 			if (UE_GET_XFERTYPE(ed1->bmAttributes) != UE_ISOCHRONOUS) {
1635 				ed1 = NULL;
1636 				continue;
1637 			}
1638 		}
1639 		if ((acdp != NULL) &&
1640 		    (desc->bDescriptorType == UDESC_CS_ENDPOINT) &&
1641 		    (desc->bDescriptorSubtype == AS_GENERAL) &&
1642 		    (sed.v1 == NULL)) {
1643 			if (audio_rev >= UAUDIO_VERSION_30) {
1644 				/* FALLTHROUGH */
1645 			} else if (audio_rev >= UAUDIO_VERSION_20) {
1646 				if (desc->bLength >= sizeof(*sed.v2))
1647 					sed.v2 = (void *)desc;
1648 			} else {
1649 				if (desc->bLength >= sizeof(*sed.v1))
1650 					sed.v1 = (void *)desc;
1651 			}
1652 		}
1653 		if (asid.v1 == NULL || asf1d.v1 == NULL ||
1654 		    ed1 == NULL || sed.v1 == NULL) {
1655 			/* need more descriptors */
1656 			continue;
1657 		}
1658 
1659 		ep_dir = UE_GET_DIR(ed1->bEndpointAddress);
1660 
1661 		/* We ignore sync endpoint information until further. */
1662 
1663 		if (audio_rev >= UAUDIO_VERSION_30) {
1664 			goto next_ep;
1665 		} else if (audio_rev >= UAUDIO_VERSION_20) {
1666 
1667 			uint32_t dwFormat;
1668 			uint8_t bSubslotSize;
1669 
1670 			dwFormat = UGETDW(asid.v2->bmFormats);
1671 			bChannels = asid.v2->bNrChannels;
1672 			bBitResolution = asf1d.v2->bBitResolution;
1673 			bSubslotSize = asf1d.v2->bSubslotSize;
1674 
1675 			/* Map 4-byte aligned 24-bit samples into 32-bit */
1676 			if (bBitResolution == 24 && bSubslotSize == 4)
1677 				bBitResolution = 32;
1678 
1679 			if (bBitResolution != (bSubslotSize * 8)) {
1680 				DPRINTF("Invalid bSubslotSize\n");
1681 				goto next_ep;
1682 			}
1683 
1684 			if ((bChannels != channels) ||
1685 			    (bBitResolution != bit_resolution)) {
1686 				DPRINTF("Wrong number of channels\n");
1687 				goto next_ep;
1688 			}
1689 
1690 			for (p_fmt = uaudio20_formats;
1691 			    p_fmt->wFormat != 0; p_fmt++) {
1692 				if ((p_fmt->wFormat & dwFormat) &&
1693 				    (p_fmt->bPrecision == bBitResolution))
1694 					break;
1695 			}
1696 
1697 			if (p_fmt->wFormat == 0) {
1698 				DPRINTF("Unsupported audio format\n");
1699 				goto next_ep;
1700 			}
1701 
1702 			for (x = 0; x != 256; x++) {
1703 				if (ep_dir == UE_DIR_OUT) {
1704 					if (!(sc->sc_mixer_clocks.bit_output[x / 8] &
1705 					    (1 << (x % 8)))) {
1706 						continue;
1707 					}
1708 				} else {
1709 					if (!(sc->sc_mixer_clocks.bit_input[x / 8] &
1710 					    (1 << (x % 8)))) {
1711 						continue;
1712 					}
1713 				}
1714 
1715 				DPRINTF("Checking clock ID=%d\n", x);
1716 
1717 				if (uaudio20_check_rate(udev,
1718 				    sc->sc_mixer_iface_no, x, rate)) {
1719 					DPRINTF("Unsupported sampling "
1720 					    "rate, id=%d\n", x);
1721 					goto next_ep;
1722 				}
1723 			}
1724 		} else {
1725 			uint16_t wFormat;
1726 
1727 			wFormat = UGETW(asid.v1->wFormatTag);
1728 			bChannels = UAUDIO_MAX_CHAN(asf1d.v1->bNrChannels);
1729 			bBitResolution = asf1d.v1->bBitResolution;
1730 
1731 			if (asf1d.v1->bSamFreqType == 0) {
1732 				DPRINTFN(16, "Sample rate: %d-%dHz\n",
1733 				    UA_SAMP_LO(asf1d.v1),
1734 				    UA_SAMP_HI(asf1d.v1));
1735 
1736 				if ((rate >= UA_SAMP_LO(asf1d.v1)) &&
1737 				    (rate <= UA_SAMP_HI(asf1d.v1)))
1738 					goto found_rate;
1739 			} else {
1740 
1741 				for (x = 0; x < asf1d.v1->bSamFreqType; x++) {
1742 					DPRINTFN(16, "Sample rate = %dHz\n",
1743 					    UA_GETSAMP(asf1d.v1, x));
1744 
1745 					if (rate == UA_GETSAMP(asf1d.v1, x))
1746 						goto found_rate;
1747 				}
1748 			}
1749 			goto next_ep;
1750 
1751 	found_rate:
1752 			for (p_fmt = uaudio10_formats;
1753 			    p_fmt->wFormat != 0; p_fmt++) {
1754 				if ((p_fmt->wFormat == wFormat) &&
1755 				    (p_fmt->bPrecision == bBitResolution))
1756 					break;
1757 			}
1758 			if (p_fmt->wFormat == 0) {
1759 				DPRINTF("Unsupported audio format\n");
1760 				goto next_ep;
1761 			}
1762 
1763 			if ((bChannels != channels) ||
1764 			    (bBitResolution != bit_resolution)) {
1765 				DPRINTF("Wrong number of channels\n");
1766 				goto next_ep;
1767 			}
1768 		}
1769 
1770 		chan = (ep_dir == UE_DIR_IN) ?
1771 		    &sc->sc_rec_chan : &sc->sc_play_chan;
1772 
1773 		if (usbd_get_iface(udev, curidx) == NULL) {
1774 			DPRINTF("Interface is not valid\n");
1775 			goto next_ep;
1776 		}
1777 		if (chan->num_alt == CHAN_MAX_ALT) {
1778 			DPRINTF("Too many alternate settings\n");
1779 			goto next_ep;
1780 		}
1781 		chan->set_alt = 0;
1782 		chan->cur_alt = CHAN_MAX_ALT;
1783 
1784 		chan_alt = &chan->usb_alt[chan->num_alt++];
1785 
1786 #ifdef USB_DEBUG
1787 		uaudio_chan_dump_ep_desc(ed1);
1788 #endif
1789 		DPRINTF("Sample rate = %dHz, channels = %d, "
1790 		    "bits = %d, format = %s\n", rate, channels,
1791 		    bit_resolution, p_fmt->description);
1792 
1793 		chan_alt->sample_rate = rate;
1794 		chan_alt->p_asf1d = asf1d;
1795 		chan_alt->p_ed1 = ed1;
1796 		chan_alt->p_fmt = p_fmt;
1797 		chan_alt->p_sed = sed;
1798 		chan_alt->iface_index = curidx;
1799 		chan_alt->iface_alt_index = alt_index;
1800 
1801 		usbd_set_parent_iface(sc->sc_udev, curidx,
1802 		    sc->sc_mixer_iface_index);
1803 
1804 		if (ep_dir == UE_DIR_IN)
1805 			chan_alt->usb_cfg = uaudio_cfg_record;
1806 		else
1807 			chan_alt->usb_cfg = uaudio_cfg_play;
1808 
1809 		chan_alt->sample_size = (UAUDIO_MAX_CHAN(channels) *
1810 		    p_fmt->bPrecision) / 8;
1811 		chan_alt->channels = channels;
1812 
1813 		if (ep_dir == UE_DIR_IN &&
1814 		    usbd_get_speed(udev) == USB_SPEED_FULL) {
1815 			uaudio_record_fix_fs(ed1,
1816 			    chan_alt->sample_size * (rate / 1000),
1817 			    chan_alt->sample_size * (rate / 4000));
1818 		}
1819 
1820 		/* setup play/record format */
1821 
1822 		format = chan_alt->p_fmt->freebsd_fmt;
1823 
1824 		switch (chan_alt->channels) {
1825 		case 2:
1826 			/* stereo */
1827 			format = SND_FORMAT(format, 2, 0);
1828 			break;
1829 		case 1:
1830 			/* mono */
1831 			format = SND_FORMAT(format, 1, 0);
1832 			break;
1833 		default:
1834 			/* surround and more */
1835 			format = feeder_matrix_default_format(
1836 			    SND_FORMAT(format, chan_alt->channels, 0));
1837 			break;
1838 		}
1839 
1840 		/* check if format is not supported */
1841 		if (format == 0) {
1842 			DPRINTF("The selected audio format is not supported\n");
1843 			chan->num_alt--;
1844 			goto next_ep;
1845 		}
1846 		if (chan->num_alt > 1) {
1847 			/* we only accumulate one format at different sample rates */
1848 			if (chan->pcm_format[0] != format) {
1849 				DPRINTF("Multiple formats is not supported\n");
1850 				chan->num_alt--;
1851 				goto next_ep;
1852 			}
1853 			/* ignore if duplicate sample rate entry */
1854 			if (rate == chan->usb_alt[chan->num_alt - 2].sample_rate) {
1855 				DPRINTF("Duplicate sample rate detected\n");
1856 				chan->num_alt--;
1857 				goto next_ep;
1858 			}
1859 		}
1860 		chan->pcm_cap.fmtlist = chan->pcm_format;
1861 		chan->pcm_cap.fmtlist[0] = format;
1862 
1863 		if (rate < chan->pcm_cap.minspeed || chan->pcm_cap.minspeed == 0)
1864 			chan->pcm_cap.minspeed = rate;
1865 		if (rate > chan->pcm_cap.maxspeed || chan->pcm_cap.maxspeed == 0)
1866 			chan->pcm_cap.maxspeed = rate;
1867 
1868 		if (sc->sc_sndstat_valid != 0) {
1869 			sbuf_printf(&sc->sc_sndstat, "\n\t"
1870 			    "mode %d.%d:(%s) %dch, %dbit, %s, %dHz",
1871 			    curidx, alt_index,
1872 			    (ep_dir == UE_DIR_IN) ? "input" : "output",
1873 				    channels, p_fmt->bPrecision,
1874 				    p_fmt->description, rate);
1875 		}
1876 
1877 	next_ep:
1878 		sed.v1 = NULL;
1879 		ed1 = NULL;
1880 	}
1881 }
1882 
1883 /* This structure defines all the supported rates. */
1884 
1885 static const uint32_t uaudio_rate_list[CHAN_MAX_ALT] = {
1886 	96000,
1887 	88200,
1888 	88000,
1889 	80000,
1890 	72000,
1891 	64000,
1892 	56000,
1893 	48000,
1894 	44100,
1895 	40000,
1896 	32000,
1897 	24000,
1898 	22050,
1899 	16000,
1900 	11025,
1901 	8000,
1902 	0
1903 };
1904 
1905 static void
1906 uaudio_chan_fill_info(struct uaudio_softc *sc, struct usb_device *udev)
1907 {
1908 	uint32_t rate = uaudio_default_rate;
1909 	uint8_t z;
1910 	uint8_t bits = uaudio_default_bits;
1911 	uint8_t y;
1912 	uint8_t channels = uaudio_default_channels;
1913 	uint8_t x;
1914 
1915 	bits -= (bits % 8);
1916 	if ((bits == 0) || (bits > 32)) {
1917 		/* set a valid value */
1918 		bits = 32;
1919 	}
1920 	if (channels == 0) {
1921 		switch (usbd_get_speed(udev)) {
1922 		case USB_SPEED_LOW:
1923 		case USB_SPEED_FULL:
1924 			/*
1925 			 * Due to high bandwidth usage and problems
1926 			 * with HIGH-speed split transactions we
1927 			 * disable surround setups on FULL-speed USB
1928 			 * by default
1929 			 */
1930 			channels = 4;
1931 			break;
1932 		default:
1933 			channels = 16;
1934 			break;
1935 		}
1936 	} else if (channels > 16) {
1937 		channels = 16;
1938 	}
1939 	if (sbuf_new(&sc->sc_sndstat, NULL, 4096, SBUF_AUTOEXTEND)) {
1940 		sc->sc_sndstat_valid = 1;
1941 	}
1942 	/* try to search for a valid config */
1943 
1944 	for (x = channels; x; x--) {
1945 		for (y = bits; y; y -= 8) {
1946 
1947 			/* try user defined rate, if any */
1948 			if (rate != 0)
1949 				uaudio_chan_fill_info_sub(sc, udev, rate, x, y);
1950 
1951 			/* try find a matching rate, if any */
1952 			for (z = 0; uaudio_rate_list[z]; z++)
1953 				uaudio_chan_fill_info_sub(sc, udev, uaudio_rate_list[z], x, y);
1954 		}
1955 	}
1956 	if (sc->sc_sndstat_valid)
1957 		sbuf_finish(&sc->sc_sndstat);
1958 }
1959 
1960 static void
1961 uaudio_chan_play_sync_callback(struct usb_xfer *xfer, usb_error_t error)
1962 {
1963 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
1964 	struct usb_page_cache *pc;
1965 	uint64_t sample_rate = ch->usb_alt[ch->cur_alt].sample_rate;
1966 	uint8_t buf[4];
1967 	uint64_t temp;
1968 	int len;
1969 	int actlen;
1970 	int nframes;
1971 
1972 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
1973 
1974 	switch (USB_GET_STATE(xfer)) {
1975 	case USB_ST_TRANSFERRED:
1976 
1977 		DPRINTFN(6, "transferred %d bytes\n", actlen);
1978 
1979 		if (nframes == 0)
1980 			break;
1981 		len = usbd_xfer_frame_len(xfer, 0);
1982 		if (len == 0)
1983 			break;
1984 		if (len > sizeof(buf))
1985 			len = sizeof(buf);
1986 
1987 		memset(buf, 0, sizeof(buf));
1988 
1989 		pc = usbd_xfer_get_frame(xfer, 0);
1990 		usbd_copy_out(pc, 0, buf, len);
1991 
1992 		temp = UGETDW(buf);
1993 
1994 		DPRINTF("Value = 0x%08x\n", (int)temp);
1995 
1996 		/* auto-detect SYNC format */
1997 
1998 		if (len == 4)
1999 			temp &= 0x0fffffff;
2000 
2001 		/* check for no data */
2002 
2003 		if (temp == 0)
2004 			break;
2005 
2006 		/* correctly scale value */
2007 
2008 		temp = (temp * 125ULL) - 64;
2009 
2010 		/* auto adjust */
2011 
2012 		while (temp < (sample_rate - (sample_rate / 4)))
2013 			temp *= 2;
2014 
2015 		while (temp > (sample_rate + (sample_rate / 2)))
2016 			temp /= 2;
2017 
2018 		/* compare */
2019 
2020 		DPRINTF("Comparing %d < %d\n",
2021 		    (int)temp, (int)sample_rate);
2022 
2023 		if (temp == sample_rate)
2024 			ch->last_sync_state = UAUDIO_SYNC_NONE;
2025 		else if (temp > sample_rate)
2026 			ch->last_sync_state = UAUDIO_SYNC_MORE;
2027 		else
2028 			ch->last_sync_state = UAUDIO_SYNC_LESS;
2029 		break;
2030 
2031 	case USB_ST_SETUP:
2032 		usbd_xfer_set_frames(xfer, 1);
2033 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_framelen(xfer));
2034 		usbd_transfer_submit(xfer);
2035 		break;
2036 
2037 	default:			/* Error */
2038 		break;
2039 	}
2040 }
2041 
2042 static void
2043 uaudio_chan_play_callback(struct usb_xfer *xfer, usb_error_t error)
2044 {
2045 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
2046 	struct usb_page_cache *pc;
2047 	uint32_t sample_size = ch->usb_alt[ch->cur_alt].sample_size;
2048 	uint32_t mfl;
2049 	uint32_t total;
2050 	uint32_t blockcount;
2051 	uint32_t n;
2052 	uint32_t offset;
2053 	int actlen;
2054 	int sumlen;
2055 
2056 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2057 
2058 	if (ch->end == ch->start) {
2059 		DPRINTF("no buffer!\n");
2060 		return;
2061 	}
2062 
2063 	switch (USB_GET_STATE(xfer)) {
2064 	case USB_ST_TRANSFERRED:
2065 tr_transferred:
2066 		if (actlen < sumlen) {
2067 			DPRINTF("short transfer, "
2068 			    "%d of %d bytes\n", actlen, sumlen);
2069 		}
2070 		chn_intr(ch->pcm_ch);
2071 
2072 		/* start SYNC transfer, if any */
2073 		if ((ch->last_sync_time++ & 7) == 0)
2074 			usbd_transfer_start(ch->xfer[UAUDIO_NCHANBUFS]);
2075 
2076 	case USB_ST_SETUP:
2077 		mfl = usbd_xfer_max_framelen(xfer);
2078 
2079 		if (ch->bytes_per_frame[1] > mfl) {
2080 			DPRINTF("bytes per transfer, %d, "
2081 			    "exceeds maximum, %d!\n",
2082 			    ch->bytes_per_frame[1],
2083 			    mfl);
2084 			break;
2085 		}
2086 
2087 		blockcount = ch->intr_frames;
2088 
2089 		/* setup number of frames */
2090 		usbd_xfer_set_frames(xfer, blockcount);
2091 
2092 		/* reset total length */
2093 		total = 0;
2094 
2095 		/* setup frame lengths */
2096 		for (n = 0; n != blockcount; n++) {
2097 			uint32_t frame_len;
2098 
2099 			ch->sample_curr += ch->sample_rem;
2100 			if (ch->sample_curr >= ch->frames_per_second) {
2101 				ch->sample_curr -= ch->frames_per_second;
2102 				frame_len = ch->bytes_per_frame[1];
2103 			} else {
2104 				frame_len = ch->bytes_per_frame[0];
2105 			}
2106 
2107 			if (n == (blockcount - 1)) {
2108 				switch (ch->last_sync_state) {
2109 				case UAUDIO_SYNC_MORE:
2110 					DPRINTFN(6, "sending one sample more\n");
2111 					if ((frame_len + sample_size) <= mfl)
2112 						frame_len += sample_size;
2113 					ch->last_sync_state = UAUDIO_SYNC_NONE;
2114 					break;
2115 				case UAUDIO_SYNC_LESS:
2116 					DPRINTFN(6, "sending one sample less\n");
2117 					if (frame_len >= sample_size)
2118 						frame_len -= sample_size;
2119 					ch->last_sync_state = UAUDIO_SYNC_NONE;
2120 					break;
2121 				default:
2122 					break;
2123 				}
2124 			}
2125 
2126 			usbd_xfer_set_frame_len(xfer, n, frame_len);
2127 			total += frame_len;
2128 		}
2129 
2130 		DPRINTFN(6, "transfer %d bytes\n", total);
2131 
2132 		offset = 0;
2133 
2134 		pc = usbd_xfer_get_frame(xfer, 0);
2135 		while (total > 0) {
2136 
2137 			n = (ch->end - ch->cur);
2138 			if (n > total) {
2139 				n = total;
2140 			}
2141 			usbd_copy_in(pc, offset, ch->cur, n);
2142 
2143 			total -= n;
2144 			ch->cur += n;
2145 			offset += n;
2146 
2147 			if (ch->cur >= ch->end) {
2148 				ch->cur = ch->start;
2149 			}
2150 		}
2151 
2152 		usbd_transfer_submit(xfer);
2153 		break;
2154 
2155 	default:			/* Error */
2156 		if (error == USB_ERR_CANCELLED) {
2157 			break;
2158 		}
2159 		goto tr_transferred;
2160 	}
2161 }
2162 
2163 static void
2164 uaudio_chan_record_sync_callback(struct usb_xfer *xfer, usb_error_t error)
2165 {
2166 	/* TODO */
2167 }
2168 
2169 static void
2170 uaudio_chan_record_callback(struct usb_xfer *xfer, usb_error_t error)
2171 {
2172 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
2173 	struct usb_page_cache *pc;
2174 	uint32_t offset0;
2175 	uint32_t offset1;
2176 	uint32_t mfl;
2177 	int m;
2178 	int n;
2179 	int len;
2180 	int actlen;
2181 	int nframes;
2182 	int blockcount;
2183 
2184 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
2185 	mfl = usbd_xfer_max_framelen(xfer);
2186 
2187 	if (ch->end == ch->start) {
2188 		DPRINTF("no buffer!\n");
2189 		return;
2190 	}
2191 
2192 	switch (USB_GET_STATE(xfer)) {
2193 	case USB_ST_TRANSFERRED:
2194 
2195 		DPRINTFN(6, "transferred %d bytes\n", actlen);
2196 
2197 		offset0 = 0;
2198 		pc = usbd_xfer_get_frame(xfer, 0);
2199 
2200 		for (n = 0; n != nframes; n++) {
2201 
2202 			offset1 = offset0;
2203 			len = usbd_xfer_frame_len(xfer, n);
2204 
2205 			while (len > 0) {
2206 
2207 				m = (ch->end - ch->cur);
2208 
2209 				if (m > len)
2210 					m = len;
2211 
2212 				usbd_copy_out(pc, offset1, ch->cur, m);
2213 
2214 				len -= m;
2215 				offset1 += m;
2216 				ch->cur += m;
2217 
2218 				if (ch->cur >= ch->end) {
2219 					ch->cur = ch->start;
2220 				}
2221 			}
2222 
2223 			offset0 += mfl;
2224 		}
2225 
2226 		chn_intr(ch->pcm_ch);
2227 
2228 	case USB_ST_SETUP:
2229 tr_setup:
2230 		blockcount = ch->intr_frames;
2231 
2232 		usbd_xfer_set_frames(xfer, blockcount);
2233 		for (n = 0; n < blockcount; n++) {
2234 			usbd_xfer_set_frame_len(xfer, n, mfl);
2235 		}
2236 
2237 		usbd_transfer_submit(xfer);
2238 		break;
2239 
2240 	default:			/* Error */
2241 		if (error == USB_ERR_CANCELLED) {
2242 			break;
2243 		}
2244 		goto tr_setup;
2245 	}
2246 }
2247 
2248 void   *
2249 uaudio_chan_init(struct uaudio_softc *sc, struct snd_dbuf *b,
2250     struct pcm_channel *c, int dir)
2251 {
2252 	struct uaudio_chan *ch = ((dir == PCMDIR_PLAY) ?
2253 	    &sc->sc_play_chan : &sc->sc_rec_chan);
2254 	uint32_t buf_size;
2255 	uint8_t x;
2256 
2257 	/* store mutex and PCM channel */
2258 
2259 	ch->pcm_ch = c;
2260 	ch->pcm_mtx = c->lock;
2261 
2262 	/* compute worst case buffer */
2263 
2264 	buf_size = 0;
2265 	for (x = 0; x != ch->num_alt; x++) {
2266 		uint32_t temp = uaudio_get_buffer_size(ch, x);
2267 		if (temp > buf_size)
2268 			buf_size = temp;
2269 	}
2270 
2271 	/* allow double buffering */
2272 	buf_size *= 2;
2273 
2274 	DPRINTF("Worst case buffer is %d bytes\n", (int)buf_size);
2275 
2276 	ch->buf = malloc(buf_size, M_DEVBUF, M_WAITOK | M_ZERO);
2277 	if (ch->buf == NULL)
2278 		goto error;
2279 	if (sndbuf_setup(b, ch->buf, buf_size) != 0)
2280 		goto error;
2281 
2282 	ch->start = ch->buf;
2283 	ch->end = ch->buf + buf_size;
2284 	ch->cur = ch->buf;
2285 	ch->pcm_buf = b;
2286 	ch->max_buf = buf_size;
2287 
2288 	if (ch->pcm_mtx == NULL) {
2289 		DPRINTF("ERROR: PCM channels does not have a mutex!\n");
2290 		goto error;
2291 	}
2292 	return (ch);
2293 
2294 error:
2295 	uaudio_chan_free(ch);
2296 	return (NULL);
2297 }
2298 
2299 int
2300 uaudio_chan_free(struct uaudio_chan *ch)
2301 {
2302 	if (ch->buf != NULL) {
2303 		free(ch->buf, M_DEVBUF);
2304 		ch->buf = NULL;
2305 	}
2306 	usbd_transfer_unsetup(ch->xfer, UAUDIO_NCHANBUFS + 1);
2307 
2308 	ch->num_alt = 0;
2309 
2310 	return (0);
2311 }
2312 
2313 int
2314 uaudio_chan_set_param_blocksize(struct uaudio_chan *ch, uint32_t blocksize)
2315 {
2316 	uint32_t temp = 2 * uaudio_get_buffer_size(ch, ch->set_alt);
2317 
2318 	sndbuf_setup(ch->pcm_buf, ch->buf, temp);
2319 
2320 	ch->start = ch->buf;
2321 	ch->end = ch->buf + temp;
2322 	ch->cur = ch->buf;
2323 
2324 	return (temp / 2);
2325 }
2326 
2327 int
2328 uaudio_chan_set_param_fragments(struct uaudio_chan *ch, uint32_t blocksize,
2329     uint32_t blockcount)
2330 {
2331 	return (1);
2332 }
2333 
2334 int
2335 uaudio_chan_set_param_speed(struct uaudio_chan *ch, uint32_t speed)
2336 {
2337 	uint8_t x;
2338 
2339 	for (x = 0; x < ch->num_alt; x++) {
2340 		if (ch->usb_alt[x].sample_rate < speed) {
2341 			/* sample rate is too low */
2342 			break;
2343 		}
2344 	}
2345 
2346 	if (x != 0)
2347 		x--;
2348 
2349 	ch->set_alt = x;
2350 
2351 	DPRINTF("Selecting alt %d\n", (int)x);
2352 
2353 	return (ch->usb_alt[x].sample_rate);
2354 }
2355 
2356 int
2357 uaudio_chan_getptr(struct uaudio_chan *ch)
2358 {
2359 	return (ch->cur - ch->start);
2360 }
2361 
2362 struct pcmchan_caps *
2363 uaudio_chan_getcaps(struct uaudio_chan *ch)
2364 {
2365 	return (&ch->pcm_cap);
2366 }
2367 
2368 static struct pcmchan_matrix uaudio_chan_matrix_swap_2_0 = {
2369 	.id = SND_CHN_MATRIX_DRV,
2370 	.channels = 2,
2371 	.ext = 0,
2372 	.map = {
2373 		/* Right */
2374 		[0] = {
2375 			.type = SND_CHN_T_FR,
2376 			.members =
2377 			    SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FC |
2378 			    SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BR |
2379 			    SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SR
2380 		},
2381 		/* Left */
2382 		[1] = {
2383 			.type = SND_CHN_T_FL,
2384 			.members =
2385 			    SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FC |
2386 			    SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BL |
2387 			    SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SL
2388 		},
2389 		[2] = {
2390 			.type = SND_CHN_T_MAX,
2391 			.members = 0
2392 		}
2393 	},
2394 	.mask = SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FL,
2395 	.offset = {  1,  0, -1, -1, -1, -1, -1, -1, -1,
2396 		    -1, -1, -1, -1, -1, -1, -1, -1, -1  }
2397 };
2398 
2399 struct pcmchan_matrix *
2400 uaudio_chan_getmatrix(struct uaudio_chan *ch, uint32_t format)
2401 {
2402 	struct uaudio_softc *sc;
2403 
2404 	sc = ch->priv_sc;
2405 
2406 	if (sc != NULL && sc->sc_uq_audio_swap_lr != 0 &&
2407 	    AFMT_CHANNEL(format) == 2)
2408 		return (&uaudio_chan_matrix_swap_2_0);
2409 
2410 	return (feeder_matrix_format_map(format));
2411 }
2412 
2413 int
2414 uaudio_chan_set_param_format(struct uaudio_chan *ch, uint32_t format)
2415 {
2416 	DPRINTF("Selecting format 0x%08x\n", (unsigned int)format);
2417 	return (0);
2418 }
2419 
2420 int
2421 uaudio_chan_start(struct uaudio_chan *ch)
2422 {
2423 	struct uaudio_softc *sc = ch->priv_sc;
2424 	int do_start = 0;
2425 
2426 	usb_proc_explore_lock(sc->sc_udev);
2427 	if (ch->operation != CHAN_OP_DRAIN) {
2428 		if (ch->cur_alt == ch->set_alt &&
2429 		    ch->operation == CHAN_OP_NONE) {
2430 			/* save doing the explore task */
2431 			do_start = 1;
2432 		} else {
2433 			ch->operation = CHAN_OP_START;
2434 			(void)usb_proc_explore_msignal(sc->sc_udev,
2435 			    &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2436 		}
2437 	}
2438 	usb_proc_explore_unlock(sc->sc_udev);
2439 
2440 	if (do_start) {
2441 		usbd_transfer_start(ch->xfer[0]);
2442 		usbd_transfer_start(ch->xfer[1]);
2443 	}
2444 	return (0);
2445 }
2446 
2447 int
2448 uaudio_chan_stop(struct uaudio_chan *ch)
2449 {
2450 	struct uaudio_softc *sc = ch->priv_sc;
2451 	int do_stop = 0;
2452 
2453 	usb_proc_explore_lock(sc->sc_udev);
2454 	if (ch->operation != CHAN_OP_DRAIN) {
2455 		if (ch->cur_alt == ch->set_alt &&
2456 		    ch->operation == CHAN_OP_NONE) {
2457 			/* save doing the explore task */
2458 			do_stop = 1;
2459 		} else {
2460 			ch->operation = CHAN_OP_STOP;
2461 			(void)usb_proc_explore_msignal(sc->sc_udev,
2462 			    &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2463 		}
2464 	}
2465 	usb_proc_explore_unlock(sc->sc_udev);
2466 
2467 	if (do_stop) {
2468 		usbd_transfer_stop(ch->xfer[0]);
2469 		usbd_transfer_stop(ch->xfer[1]);
2470 	}
2471 	return (0);
2472 }
2473 
2474 /*========================================================================*
2475  * AC - Audio Controller - routines
2476  *========================================================================*/
2477 
2478 static int
2479 uaudio_mixer_sysctl_handler(SYSCTL_HANDLER_ARGS)
2480 {
2481 	struct uaudio_softc *sc;
2482 	struct uaudio_mixer_node *pmc;
2483 	int hint;
2484 	int error;
2485 	int temp = 0;
2486 	int chan = 0;
2487 
2488 	sc = (struct uaudio_softc *)oidp->oid_arg1;
2489 	hint = oidp->oid_arg2;
2490 
2491 	if (sc->sc_mixer_lock == NULL)
2492 		return (ENXIO);
2493 
2494 	/* lookup mixer node */
2495 
2496 	mtx_lock(sc->sc_mixer_lock);
2497 	for (pmc = sc->sc_mixer_root; pmc != NULL; pmc = pmc->next) {
2498 		for (chan = 0; chan != (int)pmc->nchan; chan++) {
2499 			if (pmc->wValue[chan] != -1 &&
2500 			    pmc->wValue[chan] == hint) {
2501 				temp = pmc->wData[chan];
2502 				goto found;
2503 			}
2504 		}
2505 	}
2506 found:
2507 	mtx_unlock(sc->sc_mixer_lock);
2508 
2509 	error = sysctl_handle_int(oidp, &temp, 0, req);
2510 	if (error != 0 || req->newptr == NULL)
2511 		return (error);
2512 
2513 	/* update mixer value */
2514 
2515 	mtx_lock(sc->sc_mixer_lock);
2516 	if (pmc != NULL &&
2517 	    temp >= pmc->minval &&
2518 	    temp <= pmc->maxval) {
2519 
2520 		pmc->wData[chan] = temp;
2521 		pmc->update[(chan / 8)] |= (1 << (chan % 8));
2522 
2523 		/* start the transfer, if not already started */
2524 		usbd_transfer_start(sc->sc_mixer_xfer[0]);
2525 	}
2526 	mtx_unlock(sc->sc_mixer_lock);
2527 
2528 	return (0);
2529 }
2530 
2531 static void
2532 uaudio_mixer_ctl_free(struct uaudio_softc *sc)
2533 {
2534 	struct uaudio_mixer_node *p_mc;
2535 
2536 	while ((p_mc = sc->sc_mixer_root) != NULL) {
2537 		sc->sc_mixer_root = p_mc->next;
2538 		free(p_mc, M_USBDEV);
2539 	}
2540 }
2541 
2542 static void
2543 uaudio_mixer_register_sysctl(struct uaudio_softc *sc, device_t dev)
2544 {
2545 	struct uaudio_mixer_node *pmc;
2546 	struct sysctl_oid *mixer_tree;
2547 	struct sysctl_oid *control_tree;
2548 	char buf[32];
2549 	int chan;
2550 	int n;
2551 
2552 	mixer_tree = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
2553 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "mixer",
2554 	    CTLFLAG_RD, NULL, "");
2555 
2556 	if (mixer_tree == NULL)
2557 		return;
2558 
2559 	for (n = 0, pmc = sc->sc_mixer_root; pmc != NULL;
2560 	    pmc = pmc->next, n++) {
2561 
2562 		for (chan = 0; chan < pmc->nchan; chan++) {
2563 
2564 			if (pmc->nchan > 1) {
2565 				snprintf(buf, sizeof(buf), "%s_%d_%d",
2566 				    pmc->name, n, chan);
2567 			} else {
2568 				snprintf(buf, sizeof(buf), "%s_%d",
2569 				    pmc->name, n);
2570 			}
2571 
2572 			control_tree = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
2573 			    SYSCTL_CHILDREN(mixer_tree), OID_AUTO, buf,
2574 			    CTLFLAG_RD, NULL, "Mixer control nodes");
2575 
2576 			if (control_tree == NULL)
2577 				continue;
2578 
2579 			SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
2580 			    SYSCTL_CHILDREN(control_tree),
2581 			    OID_AUTO, "val", CTLTYPE_INT | CTLFLAG_RW, sc,
2582 			    pmc->wValue[chan],
2583 			    uaudio_mixer_sysctl_handler, "I", "Current value");
2584 
2585 			SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
2586 			    SYSCTL_CHILDREN(control_tree),
2587 			    OID_AUTO, "min", CTLFLAG_RD, 0, pmc->minval,
2588 			    "Minimum value");
2589 
2590 			SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
2591 			    SYSCTL_CHILDREN(control_tree),
2592 			    OID_AUTO, "max", CTLFLAG_RD, 0, pmc->maxval,
2593 			    "Maximum value");
2594 
2595 			SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
2596 			    SYSCTL_CHILDREN(control_tree),
2597 			    OID_AUTO, "desc", CTLFLAG_RD, pmc->desc, 0,
2598 			    "Description");
2599 		}
2600 	}
2601 }
2602 
2603 /* M-Audio FastTrack Ultra Mixer Description */
2604 /* Origin: Linux USB Audio driver */
2605 static void
2606 uaudio_mixer_controls_create_ftu(struct uaudio_softc *sc)
2607 {
2608 	int chx;
2609 	int chy;
2610 
2611 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2612 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2613 	MIX(sc).wValue[0] = MAKE_WORD(8, 0);
2614 	MIX(sc).class = UAC_OUTPUT;
2615 	MIX(sc).type = MIX_UNSIGNED_16;
2616 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2617 	MIX(sc).name = "effect";
2618 	MIX(sc).minval = 0;
2619 	MIX(sc).maxval = 7;
2620 	MIX(sc).mul = 7;
2621 	MIX(sc).nchan = 1;
2622 	MIX(sc).update[0] = 1;
2623 	strlcpy(MIX(sc).desc, "Room1,2,3,Hall1,2,Plate,Delay,Echo", sizeof(MIX(sc).desc));
2624 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2625 
2626 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2627 	MIX(sc).wIndex = MAKE_WORD(5, sc->sc_mixer_iface_no);
2628 
2629 	for (chx = 0; chx != 8; chx++) {
2630 		for (chy = 0; chy != 8; chy++) {
2631 
2632 			MIX(sc).wValue[0] = MAKE_WORD(chx + 1, chy + 1);
2633 			MIX(sc).type = MIX_SIGNED_16;
2634 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2635 			MIX(sc).name = "mix_rec";
2636 			MIX(sc).nchan = 1;
2637 			MIX(sc).update[0] = 1;
2638 			MIX(sc).val_default = 0;
2639 			snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2640 			    "AIn%d - Out%d Record Volume", chy + 1, chx + 1);
2641 
2642 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2643 
2644 			MIX(sc).wValue[0] = MAKE_WORD(chx + 1, chy + 1 + 8);
2645 			MIX(sc).type = MIX_SIGNED_16;
2646 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2647 			MIX(sc).name = "mix_play";
2648 			MIX(sc).nchan = 1;
2649 			MIX(sc).update[0] = 1;
2650 			MIX(sc).val_default = (chx == chy) ? 2 : 0;
2651 			snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2652 			    "DIn%d - Out%d Playback Volume", chy + 1, chx + 1);
2653 
2654 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2655 		}
2656 	}
2657 
2658 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2659 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2660 	MIX(sc).wValue[0] = MAKE_WORD(2, 0);
2661 	MIX(sc).class = UAC_OUTPUT;
2662 	MIX(sc).type = MIX_SIGNED_8;
2663 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2664 	MIX(sc).name = "effect_vol";
2665 	MIX(sc).nchan = 1;
2666 	MIX(sc).update[0] = 1;
2667 	MIX(sc).minval = 0;
2668 	MIX(sc).maxval = 0x7f;
2669 	MIX(sc).mul = 0x7f;
2670 	MIX(sc).nchan = 1;
2671 	MIX(sc).update[0] = 1;
2672 	strlcpy(MIX(sc).desc, "Effect Volume", sizeof(MIX(sc).desc));
2673 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2674 
2675 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2676 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2677 	MIX(sc).wValue[0] = MAKE_WORD(3, 0);
2678 	MIX(sc).class = UAC_OUTPUT;
2679 	MIX(sc).type = MIX_SIGNED_16;
2680 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2681 	MIX(sc).name = "effect_dur";
2682 	MIX(sc).nchan = 1;
2683 	MIX(sc).update[0] = 1;
2684 	MIX(sc).minval = 0;
2685 	MIX(sc).maxval = 0x7f00;
2686 	MIX(sc).mul = 0x7f00;
2687 	MIX(sc).nchan = 1;
2688 	MIX(sc).update[0] = 1;
2689 	strlcpy(MIX(sc).desc, "Effect Duration", sizeof(MIX(sc).desc));
2690 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2691 
2692 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2693 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2694 	MIX(sc).wValue[0] = MAKE_WORD(4, 0);
2695 	MIX(sc).class = UAC_OUTPUT;
2696 	MIX(sc).type = MIX_SIGNED_8;
2697 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2698 	MIX(sc).name = "effect_fb";
2699 	MIX(sc).nchan = 1;
2700 	MIX(sc).update[0] = 1;
2701 	MIX(sc).minval = 0;
2702 	MIX(sc).maxval = 0x7f;
2703 	MIX(sc).mul = 0x7f;
2704 	MIX(sc).nchan = 1;
2705 	MIX(sc).update[0] = 1;
2706 	strlcpy(MIX(sc).desc, "Effect Feedback Volume", sizeof(MIX(sc).desc));
2707 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2708 
2709 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2710 	MIX(sc).wIndex = MAKE_WORD(7, sc->sc_mixer_iface_no);
2711 	for (chy = 0; chy != 4; chy++) {
2712 
2713 		MIX(sc).wValue[0] = MAKE_WORD(7, chy + 1);
2714 		MIX(sc).type = MIX_SIGNED_16;
2715 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2716 		MIX(sc).name = "effect_ret";
2717 		MIX(sc).nchan = 1;
2718 		MIX(sc).update[0] = 1;
2719 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2720 		    "Effect Return %d Volume", chy + 1);
2721 
2722 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2723 	}
2724 
2725 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2726 	MIX(sc).wIndex = MAKE_WORD(5, sc->sc_mixer_iface_no);
2727 
2728 	for (chy = 0; chy != 8; chy++) {
2729 		MIX(sc).wValue[0] = MAKE_WORD(9, chy + 1);
2730 		MIX(sc).type = MIX_SIGNED_16;
2731 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2732 		MIX(sc).name = "effect_send";
2733 		MIX(sc).nchan = 1;
2734 		MIX(sc).update[0] = 1;
2735 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2736 		    "Effect Send AIn%d Volume", chy + 1);
2737 
2738 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2739 
2740 		MIX(sc).wValue[0] = MAKE_WORD(9, chy + 1);
2741 		MIX(sc).type = MIX_SIGNED_16;
2742 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2743 		MIX(sc).name = "effect_send";
2744 		MIX(sc).nchan = 1;
2745 		MIX(sc).update[0] = 1;
2746 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2747 		    "Effect Send DIn%d Volume", chy + 1 + 8);
2748 
2749 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2750 	}
2751 }
2752 
2753 static void
2754 uaudio_mixer_reload_all(struct uaudio_softc *sc)
2755 {
2756 	struct uaudio_mixer_node *pmc;
2757 	int chan;
2758 
2759 	if (sc->sc_mixer_lock == NULL)
2760 		return;
2761 
2762 	mtx_lock(sc->sc_mixer_lock);
2763 	for (pmc = sc->sc_mixer_root; pmc != NULL; pmc = pmc->next) {
2764 		/* use reset defaults for non-oss controlled settings */
2765 		if (pmc->ctl == SOUND_MIXER_NRDEVICES)
2766 			continue;
2767 		for (chan = 0; chan < pmc->nchan; chan++)
2768 			pmc->update[chan / 8] |= (1 << (chan % 8));
2769 	}
2770 	usbd_transfer_start(sc->sc_mixer_xfer[0]);
2771 
2772 	/* start HID volume keys, if any */
2773 	usbd_transfer_start(sc->sc_hid.xfer[0]);
2774 	mtx_unlock(sc->sc_mixer_lock);
2775 }
2776 
2777 static void
2778 uaudio_mixer_add_ctl_sub(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
2779 {
2780 	struct uaudio_mixer_node *p_mc_new =
2781 	    malloc(sizeof(*p_mc_new), M_USBDEV, M_WAITOK);
2782 	int ch;
2783 
2784 	if (p_mc_new != NULL) {
2785 		memcpy(p_mc_new, mc, sizeof(*p_mc_new));
2786 		p_mc_new->next = sc->sc_mixer_root;
2787 		sc->sc_mixer_root = p_mc_new;
2788 		sc->sc_mixer_count++;
2789 
2790 		/* set default value for all channels */
2791 		for (ch = 0; ch < p_mc_new->nchan; ch++) {
2792 			switch (p_mc_new->val_default) {
2793 			case 1:
2794 				/* 50% */
2795 				p_mc_new->wData[ch] = (p_mc_new->maxval + p_mc_new->minval) / 2;
2796 				break;
2797 			case 2:
2798 				/* 100% */
2799 				p_mc_new->wData[ch] = p_mc_new->maxval;
2800 				break;
2801 			default:
2802 				/* 0% */
2803 				p_mc_new->wData[ch] = p_mc_new->minval;
2804 				break;
2805 			}
2806 		}
2807 	} else {
2808 		DPRINTF("out of memory\n");
2809 	}
2810 }
2811 
2812 static void
2813 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
2814 {
2815 	int32_t res;
2816 
2817 	if (mc->class < UAC_NCLASSES) {
2818 		DPRINTF("adding %s.%d\n",
2819 		    uac_names[mc->class], mc->ctl);
2820 	} else {
2821 		DPRINTF("adding %d\n", mc->ctl);
2822 	}
2823 
2824 	if (mc->type == MIX_ON_OFF) {
2825 		mc->minval = 0;
2826 		mc->maxval = 1;
2827 	} else if (mc->type == MIX_SELECTOR) {
2828 	} else {
2829 
2830 		/* determine min and max values */
2831 
2832 		mc->minval = uaudio_mixer_get(sc->sc_udev,
2833 		    sc->sc_audio_rev, GET_MIN, mc);
2834 		mc->maxval = uaudio_mixer_get(sc->sc_udev,
2835 		    sc->sc_audio_rev, GET_MAX, mc);
2836 
2837 		/* check if max and min was swapped */
2838 
2839 		if (mc->maxval < mc->minval) {
2840 			res = mc->maxval;
2841 			mc->maxval = mc->minval;
2842 			mc->minval = res;
2843 		}
2844 
2845 		/* compute value range */
2846 		mc->mul = mc->maxval - mc->minval;
2847 		if (mc->mul == 0)
2848 			mc->mul = 1;
2849 
2850 		/* compute value alignment */
2851 		res = uaudio_mixer_get(sc->sc_udev,
2852 		    sc->sc_audio_rev, GET_RES, mc);
2853 
2854 		DPRINTF("Resolution = %d\n", (int)res);
2855 	}
2856 
2857 	uaudio_mixer_add_ctl_sub(sc, mc);
2858 
2859 #ifdef USB_DEBUG
2860 	if (uaudio_debug > 2) {
2861 		uint8_t i;
2862 
2863 		for (i = 0; i < mc->nchan; i++) {
2864 			DPRINTF("[mix] wValue=%04x\n", mc->wValue[0]);
2865 		}
2866 		DPRINTF("[mix] wIndex=%04x type=%d ctl='%d' "
2867 		    "min=%d max=%d\n",
2868 		    mc->wIndex, mc->type, mc->ctl,
2869 		    mc->minval, mc->maxval);
2870 	}
2871 #endif
2872 }
2873 
2874 static void
2875 uaudio_mixer_add_mixer(struct uaudio_softc *sc,
2876     const struct uaudio_terminal_node *iot, int id)
2877 {
2878 	const struct usb_audio_mixer_unit_0 *d0 = iot[id].u.mu_v1;
2879 	const struct usb_audio_mixer_unit_1 *d1;
2880 
2881 	uint32_t bno;			/* bit number */
2882 	uint32_t p;			/* bit number accumulator */
2883 	uint32_t mo;			/* matching outputs */
2884 	uint32_t mc;			/* matching channels */
2885 	uint32_t ichs;			/* input channels */
2886 	uint32_t ochs;			/* output channels */
2887 	uint32_t c;
2888 	uint32_t chs;			/* channels */
2889 	uint32_t i;
2890 	uint32_t o;
2891 
2892 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
2893 	    d0->bUnitId, d0->bNrInPins);
2894 
2895 	/* compute the number of input channels */
2896 
2897 	ichs = 0;
2898 	for (i = 0; i < d0->bNrInPins; i++) {
2899 		ichs += uaudio_mixer_get_cluster(
2900 		    d0->baSourceId[i], iot).bNrChannels;
2901 	}
2902 
2903 	d1 = (const void *)(d0->baSourceId + d0->bNrInPins);
2904 
2905 	/* and the number of output channels */
2906 
2907 	ochs = d1->bNrChannels;
2908 
2909 	DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs);
2910 
2911 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2912 
2913 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2914 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
2915 	MIX(sc).type = MIX_SIGNED_16;
2916 
2917 	if (uaudio_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL)
2918 		return;
2919 
2920 	for (p = i = 0; i < d0->bNrInPins; i++) {
2921 		chs = uaudio_mixer_get_cluster(
2922 		    d0->baSourceId[i], iot).bNrChannels;
2923 		mc = 0;
2924 		for (c = 0; c < chs; c++) {
2925 			mo = 0;
2926 			for (o = 0; o < ochs; o++) {
2927 				bno = ((p + c) * ochs) + o;
2928 				if (BIT_TEST(d1->bmControls, bno))
2929 					mo++;
2930 			}
2931 			if (mo == 1)
2932 				mc++;
2933 		}
2934 		if ((mc == chs) && (chs <= MIX_MAX_CHAN)) {
2935 
2936 			/* repeat bit-scan */
2937 
2938 			mc = 0;
2939 			for (c = 0; c < chs; c++) {
2940 				for (o = 0; o < ochs; o++) {
2941 					bno = ((p + c) * ochs) + o;
2942 					if (BIT_TEST(d1->bmControls, bno))
2943 						MIX(sc).wValue[mc++] = MAKE_WORD(p + c + 1, o + 1);
2944 				}
2945 			}
2946 			MIX(sc).nchan = chs;
2947 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2948 		}
2949 		p += chs;
2950 	}
2951 }
2952 
2953 static void
2954 uaudio20_mixer_add_mixer(struct uaudio_softc *sc,
2955     const struct uaudio_terminal_node *iot, int id)
2956 {
2957 	const struct usb_audio20_mixer_unit_0 *d0 = iot[id].u.mu_v2;
2958 	const struct usb_audio20_mixer_unit_1 *d1;
2959 
2960 	uint32_t bno;			/* bit number */
2961 	uint32_t p;			/* bit number accumulator */
2962 	uint32_t mo;			/* matching outputs */
2963 	uint32_t mc;			/* matching channels */
2964 	uint32_t ichs;			/* input channels */
2965 	uint32_t ochs;			/* output channels */
2966 	uint32_t c;
2967 	uint32_t chs;			/* channels */
2968 	uint32_t i;
2969 	uint32_t o;
2970 
2971 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
2972 	    d0->bUnitId, d0->bNrInPins);
2973 
2974 	/* compute the number of input channels */
2975 
2976 	ichs = 0;
2977 	for (i = 0; i < d0->bNrInPins; i++) {
2978 		ichs += uaudio20_mixer_get_cluster(
2979 		    d0->baSourceId[i], iot).bNrChannels;
2980 	}
2981 
2982 	d1 = (const void *)(d0->baSourceId + d0->bNrInPins);
2983 
2984 	/* and the number of output channels */
2985 
2986 	ochs = d1->bNrChannels;
2987 
2988 	DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs);
2989 
2990 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2991 
2992 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2993 	uaudio20_mixer_determine_class(&iot[id], &MIX(sc));
2994 	MIX(sc).type = MIX_SIGNED_16;
2995 
2996 	if (uaudio20_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL)
2997 		return;
2998 
2999 	for (p = i = 0; i < d0->bNrInPins; i++) {
3000 		chs = uaudio20_mixer_get_cluster(
3001 		    d0->baSourceId[i], iot).bNrChannels;
3002 		mc = 0;
3003 		for (c = 0; c < chs; c++) {
3004 			mo = 0;
3005 			for (o = 0; o < ochs; o++) {
3006 				bno = ((p + c) * ochs) + o;
3007 				if (BIT_TEST(d1->bmControls, bno))
3008 					mo++;
3009 			}
3010 			if (mo == 1)
3011 				mc++;
3012 		}
3013 		if ((mc == chs) && (chs <= MIX_MAX_CHAN)) {
3014 
3015 			/* repeat bit-scan */
3016 
3017 			mc = 0;
3018 			for (c = 0; c < chs; c++) {
3019 				for (o = 0; o < ochs; o++) {
3020 					bno = ((p + c) * ochs) + o;
3021 					if (BIT_TEST(d1->bmControls, bno))
3022 						MIX(sc).wValue[mc++] = MAKE_WORD(p + c + 1, o + 1);
3023 				}
3024 			}
3025 			MIX(sc).nchan = chs;
3026 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3027 		}
3028 		p += chs;
3029 	}
3030 }
3031 
3032 static void
3033 uaudio_mixer_add_selector(struct uaudio_softc *sc,
3034     const struct uaudio_terminal_node *iot, int id)
3035 {
3036 	const struct usb_audio_selector_unit *d = iot[id].u.su_v1;
3037 	uint16_t i;
3038 
3039 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3040 	    d->bUnitId, d->bNrInPins);
3041 
3042 	if (d->bNrInPins == 0)
3043 		return;
3044 
3045 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3046 
3047 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3048 	MIX(sc).wValue[0] = MAKE_WORD(0, 0);
3049 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3050 	MIX(sc).nchan = 1;
3051 	MIX(sc).type = MIX_SELECTOR;
3052 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3053 	MIX(sc).minval = 1;
3054 	MIX(sc).maxval = d->bNrInPins;
3055 	MIX(sc).name = "selector";
3056 
3057 	i = d->baSourceId[d->bNrInPins];
3058 	if (i == 0 ||
3059 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3060 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3061 		MIX(sc).desc[0] = 0;
3062 	}
3063 
3064 	if (MIX(sc).maxval > MAX_SELECTOR_INPUT_PIN) {
3065 		MIX(sc).maxval = MAX_SELECTOR_INPUT_PIN;
3066 	}
3067 	MIX(sc).mul = (MIX(sc).maxval - MIX(sc).minval);
3068 	for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) {
3069 		MIX(sc).slctrtype[i] = SOUND_MIXER_NRDEVICES;
3070 	}
3071 
3072 	for (i = 0; i < MIX(sc).maxval; i++) {
3073 		MIX(sc).slctrtype[i] = uaudio_mixer_feature_name(
3074 		    &iot[d->baSourceId[i]], &MIX(sc));
3075 	}
3076 
3077 	MIX(sc).class = 0;			/* not used */
3078 
3079 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3080 }
3081 
3082 static void
3083 uaudio20_mixer_add_selector(struct uaudio_softc *sc,
3084     const struct uaudio_terminal_node *iot, int id)
3085 {
3086 	const struct usb_audio20_selector_unit *d = iot[id].u.su_v2;
3087 	uint16_t i;
3088 
3089 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3090 	    d->bUnitId, d->bNrInPins);
3091 
3092 	if (d->bNrInPins == 0)
3093 		return;
3094 
3095 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3096 
3097 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3098 	MIX(sc).wValue[0] = MAKE_WORD(0, 0);
3099 	uaudio20_mixer_determine_class(&iot[id], &MIX(sc));
3100 	MIX(sc).nchan = 1;
3101 	MIX(sc).type = MIX_SELECTOR;
3102 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3103 	MIX(sc).minval = 1;
3104 	MIX(sc).maxval = d->bNrInPins;
3105 	MIX(sc).name = "selector";
3106 
3107 	i = d->baSourceId[d->bNrInPins];
3108 	if (i == 0 ||
3109 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3110 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3111 		MIX(sc).desc[0] = 0;
3112 	}
3113 
3114 	if (MIX(sc).maxval > MAX_SELECTOR_INPUT_PIN)
3115 		MIX(sc).maxval = MAX_SELECTOR_INPUT_PIN;
3116 
3117 	MIX(sc).mul = (MIX(sc).maxval - MIX(sc).minval);
3118 	for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++)
3119 		MIX(sc).slctrtype[i] = SOUND_MIXER_NRDEVICES;
3120 
3121 	for (i = 0; i < MIX(sc).maxval; i++) {
3122 		MIX(sc).slctrtype[i] = uaudio20_mixer_feature_name(
3123 		    &iot[d->baSourceId[i]], &MIX(sc));
3124 	}
3125 
3126 	MIX(sc).class = 0;			/* not used */
3127 
3128 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3129 }
3130 
3131 static uint32_t
3132 uaudio_mixer_feature_get_bmaControls(const struct usb_audio_feature_unit *d,
3133     uint8_t i)
3134 {
3135 	uint32_t temp = 0;
3136 	uint32_t offset = (i * d->bControlSize);
3137 
3138 	if (d->bControlSize > 0) {
3139 		temp |= d->bmaControls[offset];
3140 		if (d->bControlSize > 1) {
3141 			temp |= d->bmaControls[offset + 1] << 8;
3142 			if (d->bControlSize > 2) {
3143 				temp |= d->bmaControls[offset + 2] << 16;
3144 				if (d->bControlSize > 3) {
3145 					temp |= d->bmaControls[offset + 3] << 24;
3146 				}
3147 			}
3148 		}
3149 	}
3150 	return (temp);
3151 }
3152 
3153 static void
3154 uaudio_mixer_add_feature(struct uaudio_softc *sc,
3155     const struct uaudio_terminal_node *iot, int id)
3156 {
3157 	const struct usb_audio_feature_unit *d = iot[id].u.fu_v1;
3158 	uint32_t fumask;
3159 	uint32_t mmask;
3160 	uint32_t cmask;
3161 	uint16_t mixernumber;
3162 	uint8_t nchan;
3163 	uint8_t chan;
3164 	uint8_t ctl;
3165 	uint8_t i;
3166 
3167 	if (d->bControlSize == 0)
3168 		return;
3169 
3170 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3171 
3172 	nchan = (d->bLength - 7) / d->bControlSize;
3173 	mmask = uaudio_mixer_feature_get_bmaControls(d, 0);
3174 	cmask = 0;
3175 
3176 	if (nchan == 0)
3177 		return;
3178 
3179 	/* figure out what we can control */
3180 
3181 	for (chan = 1; chan < nchan; chan++) {
3182 		DPRINTFN(10, "chan=%d mask=%x\n",
3183 		    chan, uaudio_mixer_feature_get_bmaControls(d, chan));
3184 
3185 		cmask |= uaudio_mixer_feature_get_bmaControls(d, chan);
3186 	}
3187 
3188 	if (nchan > MIX_MAX_CHAN) {
3189 		nchan = MIX_MAX_CHAN;
3190 	}
3191 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3192 
3193 	i = d->bmaControls[d->bControlSize];
3194 	if (i == 0 ||
3195 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3196 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3197 		MIX(sc).desc[0] = 0;
3198 	}
3199 
3200 	for (ctl = 1; ctl <= LOUDNESS_CONTROL; ctl++) {
3201 
3202 		fumask = FU_MASK(ctl);
3203 
3204 		DPRINTFN(5, "ctl=%d fumask=0x%04x\n",
3205 		    ctl, fumask);
3206 
3207 		if (mmask & fumask) {
3208 			MIX(sc).nchan = 1;
3209 			MIX(sc).wValue[0] = MAKE_WORD(ctl, 0);
3210 		} else if (cmask & fumask) {
3211 			MIX(sc).nchan = nchan - 1;
3212 			for (i = 1; i < nchan; i++) {
3213 				if (uaudio_mixer_feature_get_bmaControls(d, i) & fumask)
3214 					MIX(sc).wValue[i - 1] = MAKE_WORD(ctl, i);
3215 				else
3216 					MIX(sc).wValue[i - 1] = -1;
3217 			}
3218 		} else {
3219 			continue;
3220 		}
3221 
3222 		mixernumber = uaudio_mixer_feature_name(&iot[id], &MIX(sc));
3223 
3224 		switch (ctl) {
3225 		case MUTE_CONTROL:
3226 			MIX(sc).type = MIX_ON_OFF;
3227 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3228 			MIX(sc).name = "mute";
3229 			break;
3230 
3231 		case VOLUME_CONTROL:
3232 			MIX(sc).type = MIX_SIGNED_16;
3233 			MIX(sc).ctl = mixernumber;
3234 			MIX(sc).name = "vol";
3235 			break;
3236 
3237 		case BASS_CONTROL:
3238 			MIX(sc).type = MIX_SIGNED_8;
3239 			MIX(sc).ctl = SOUND_MIXER_BASS;
3240 			MIX(sc).name = "bass";
3241 			break;
3242 
3243 		case MID_CONTROL:
3244 			MIX(sc).type = MIX_SIGNED_8;
3245 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3246 			MIX(sc).name = "mid";
3247 			break;
3248 
3249 		case TREBLE_CONTROL:
3250 			MIX(sc).type = MIX_SIGNED_8;
3251 			MIX(sc).ctl = SOUND_MIXER_TREBLE;
3252 			MIX(sc).name = "treble";
3253 			break;
3254 
3255 		case GRAPHIC_EQUALIZER_CONTROL:
3256 			continue;	/* XXX don't add anything */
3257 
3258 		case AGC_CONTROL:
3259 			MIX(sc).type = MIX_ON_OFF;
3260 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3261 			MIX(sc).name = "agc";
3262 			break;
3263 
3264 		case DELAY_CONTROL:
3265 			MIX(sc).type = MIX_UNSIGNED_16;
3266 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3267 			MIX(sc).name = "delay";
3268 			break;
3269 
3270 		case BASS_BOOST_CONTROL:
3271 			MIX(sc).type = MIX_ON_OFF;
3272 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3273 			MIX(sc).name = "boost";
3274 			break;
3275 
3276 		case LOUDNESS_CONTROL:
3277 			MIX(sc).type = MIX_ON_OFF;
3278 			MIX(sc).ctl = SOUND_MIXER_LOUD;	/* Is this correct ? */
3279 			MIX(sc).name = "loudness";
3280 			break;
3281 
3282 		default:
3283 			MIX(sc).type = MIX_UNKNOWN;
3284 			break;
3285 		}
3286 
3287 		if (MIX(sc).type != MIX_UNKNOWN)
3288 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3289 	}
3290 }
3291 
3292 static void
3293 uaudio20_mixer_add_feature(struct uaudio_softc *sc,
3294     const struct uaudio_terminal_node *iot, int id)
3295 {
3296 	const struct usb_audio20_feature_unit *d = iot[id].u.fu_v2;
3297 	uint32_t ctl;
3298 	uint32_t mmask;
3299 	uint32_t cmask;
3300 	uint16_t mixernumber;
3301 	uint8_t nchan;
3302 	uint8_t chan;
3303 	uint8_t i;
3304 	uint8_t what;
3305 
3306 	if (UGETDW(d->bmaControls[0]) == 0)
3307 		return;
3308 
3309 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3310 
3311 	nchan = (d->bLength - 6) / 4;
3312 	mmask = UGETDW(d->bmaControls[0]);
3313 	cmask = 0;
3314 
3315 	if (nchan == 0)
3316 		return;
3317 
3318 	/* figure out what we can control */
3319 
3320 	for (chan = 1; chan < nchan; chan++)
3321 		cmask |= UGETDW(d->bmaControls[chan]);
3322 
3323 	if (nchan > MIX_MAX_CHAN)
3324 		nchan = MIX_MAX_CHAN;
3325 
3326 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3327 
3328 	i = d->bmaControls[nchan][0];
3329 	if (i == 0 ||
3330 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3331 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3332 		MIX(sc).desc[0] = 0;
3333 	}
3334 
3335 	for (ctl = 3; ctl != 0; ctl <<= 2) {
3336 
3337 		mixernumber = uaudio20_mixer_feature_name(&iot[id], &MIX(sc));
3338 
3339 		switch (ctl) {
3340 		case (3 << 0):
3341 			MIX(sc).type = MIX_ON_OFF;
3342 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3343 			MIX(sc).name = "mute";
3344 			what = MUTE_CONTROL;
3345 			break;
3346 		case (3 << 2):
3347 			MIX(sc).type = MIX_SIGNED_16;
3348 			MIX(sc).ctl = mixernumber;
3349 			MIX(sc).name = "vol";
3350 			what = VOLUME_CONTROL;
3351 			break;
3352 		case (3 << 4):
3353 			MIX(sc).type = MIX_SIGNED_8;
3354 			MIX(sc).ctl = SOUND_MIXER_BASS;
3355 			MIX(sc).name = "bass";
3356 			what = BASS_CONTROL;
3357 			break;
3358 		case (3 << 6):
3359 			MIX(sc).type = MIX_SIGNED_8;
3360 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3361 			MIX(sc).name = "mid";
3362 			what = MID_CONTROL;
3363 			break;
3364 		case (3 << 8):
3365 			MIX(sc).type = MIX_SIGNED_8;
3366 			MIX(sc).ctl = SOUND_MIXER_TREBLE;
3367 			MIX(sc).name = "treble";
3368 			what = TREBLE_CONTROL;
3369 			break;
3370 		case (3 << 12):
3371 			MIX(sc).type = MIX_ON_OFF;
3372 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3373 			MIX(sc).name = "agc";
3374 			what = AGC_CONTROL;
3375 			break;
3376 		case (3 << 14):
3377 			MIX(sc).type = MIX_UNSIGNED_16;
3378 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3379 			MIX(sc).name = "delay";
3380 			what = DELAY_CONTROL;
3381 			break;
3382 		case (3 << 16):
3383 			MIX(sc).type = MIX_ON_OFF;
3384 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3385 			MIX(sc).name = "boost";
3386 			what = BASS_BOOST_CONTROL;
3387 			break;
3388 		case (3 << 18):
3389 			MIX(sc).type = MIX_ON_OFF;
3390 			MIX(sc).ctl = SOUND_MIXER_LOUD;	/* Is this correct ? */
3391 			MIX(sc).name = "loudness";
3392 			what = LOUDNESS_CONTROL;
3393 			break;
3394 		case (3 << 20):
3395 			MIX(sc).type = MIX_SIGNED_16;
3396 			MIX(sc).ctl = mixernumber;
3397 			MIX(sc).name = "igain";
3398 			what = INPUT_GAIN_CONTROL;
3399 			break;
3400 		case (3 << 22):
3401 			MIX(sc).type = MIX_SIGNED_16;
3402 			MIX(sc).ctl = mixernumber;
3403 			MIX(sc).name = "igainpad";
3404 			what = INPUT_GAIN_PAD_CONTROL;
3405 			break;
3406 		default:
3407 			continue;
3408 		}
3409 
3410 		if ((mmask & ctl) == ctl) {
3411 			MIX(sc).nchan = 1;
3412 			MIX(sc).wValue[0] = MAKE_WORD(what, 0);
3413 		} else if ((cmask & ctl) == ctl) {
3414 			MIX(sc).nchan = nchan - 1;
3415 			for (i = 1; i < nchan; i++) {
3416 				if ((UGETDW(d->bmaControls[i]) & ctl) == ctl)
3417 					MIX(sc).wValue[i - 1] = MAKE_WORD(what, i);
3418 				else
3419 					MIX(sc).wValue[i - 1] = -1;
3420 			}
3421 		} else {
3422 			continue;
3423 		}
3424 
3425 		if (MIX(sc).type != MIX_UNKNOWN)
3426 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3427 	}
3428 }
3429 
3430 static void
3431 uaudio_mixer_add_processing_updown(struct uaudio_softc *sc,
3432     const struct uaudio_terminal_node *iot, int id)
3433 {
3434 	const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1;
3435 	const struct usb_audio_processing_unit_1 *d1 =
3436 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3437 	const struct usb_audio_processing_unit_updown *ud =
3438 	    (const void *)(d1->bmControls + d1->bControlSize);
3439 	uint8_t i;
3440 
3441 	if (uaudio_mixer_verify_desc(d0, sizeof(*ud)) == NULL) {
3442 		return;
3443 	}
3444 	if (uaudio_mixer_verify_desc(d0, sizeof(*ud) + (2 * ud->bNrModes))
3445 	    == NULL) {
3446 		return;
3447 	}
3448 	DPRINTFN(3, "bUnitId=%d bNrModes=%d\n",
3449 	    d0->bUnitId, ud->bNrModes);
3450 
3451 	if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
3452 		DPRINTF("no mode select\n");
3453 		return;
3454 	}
3455 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3456 
3457 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3458 	MIX(sc).nchan = 1;
3459 	MIX(sc).wValue[0] = MAKE_WORD(UD_MODE_SELECT_CONTROL, 0);
3460 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3461 	MIX(sc).type = MIX_ON_OFF;		/* XXX */
3462 
3463 	for (i = 0; i < ud->bNrModes; i++) {
3464 		DPRINTFN(3, "i=%d bm=0x%x\n", i, UGETW(ud->waModes[i]));
3465 		/* XXX */
3466 	}
3467 
3468 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3469 }
3470 
3471 static void
3472 uaudio_mixer_add_processing(struct uaudio_softc *sc,
3473     const struct uaudio_terminal_node *iot, int id)
3474 {
3475 	const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1;
3476 	const struct usb_audio_processing_unit_1 *d1 =
3477 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3478 	uint16_t ptype;
3479 
3480 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3481 
3482 	ptype = UGETW(d0->wProcessType);
3483 
3484 	DPRINTFN(3, "wProcessType=%d bUnitId=%d "
3485 	    "bNrInPins=%d\n", ptype, d0->bUnitId, d0->bNrInPins);
3486 
3487 	if (d1->bControlSize == 0) {
3488 		return;
3489 	}
3490 	if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
3491 		MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3492 		MIX(sc).nchan = 1;
3493 		MIX(sc).wValue[0] = MAKE_WORD(XX_ENABLE_CONTROL, 0);
3494 		uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3495 		MIX(sc).type = MIX_ON_OFF;
3496 		uaudio_mixer_add_ctl(sc, &MIX(sc));
3497 	}
3498 	switch (ptype) {
3499 	case UPDOWNMIX_PROCESS:
3500 		uaudio_mixer_add_processing_updown(sc, iot, id);
3501 		break;
3502 
3503 	case DOLBY_PROLOGIC_PROCESS:
3504 	case P3D_STEREO_EXTENDER_PROCESS:
3505 	case REVERBATION_PROCESS:
3506 	case CHORUS_PROCESS:
3507 	case DYN_RANGE_COMP_PROCESS:
3508 	default:
3509 		DPRINTF("unit %d, type=%d is not implemented\n",
3510 		    d0->bUnitId, ptype);
3511 		break;
3512 	}
3513 }
3514 
3515 static void
3516 uaudio_mixer_add_extension(struct uaudio_softc *sc,
3517     const struct uaudio_terminal_node *iot, int id)
3518 {
3519 	const struct usb_audio_extension_unit_0 *d0 = iot[id].u.eu_v1;
3520 	const struct usb_audio_extension_unit_1 *d1 =
3521 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3522 
3523 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3524 	    d0->bUnitId, d0->bNrInPins);
3525 
3526 	if (sc->sc_uq_au_no_xu) {
3527 		return;
3528 	}
3529 	if (d1->bControlSize == 0) {
3530 		return;
3531 	}
3532 	if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
3533 
3534 		memset(&MIX(sc), 0, sizeof(MIX(sc)));
3535 
3536 		MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3537 		MIX(sc).nchan = 1;
3538 		MIX(sc).wValue[0] = MAKE_WORD(UA_EXT_ENABLE, 0);
3539 		uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3540 		MIX(sc).type = MIX_ON_OFF;
3541 
3542 		uaudio_mixer_add_ctl(sc, &MIX(sc));
3543 	}
3544 }
3545 
3546 static const void *
3547 uaudio_mixer_verify_desc(const void *arg, uint32_t len)
3548 {
3549 	const struct usb_audio_mixer_unit_1 *d1;
3550 	const struct usb_audio_extension_unit_1 *e1;
3551 	const struct usb_audio_processing_unit_1 *u1;
3552 
3553 	union {
3554 		const struct usb_descriptor *desc;
3555 		const struct usb_audio_input_terminal *it;
3556 		const struct usb_audio_output_terminal *ot;
3557 		const struct usb_audio_mixer_unit_0 *mu;
3558 		const struct usb_audio_selector_unit *su;
3559 		const struct usb_audio_feature_unit *fu;
3560 		const struct usb_audio_processing_unit_0 *pu;
3561 		const struct usb_audio_extension_unit_0 *eu;
3562 	}     u;
3563 
3564 	u.desc = arg;
3565 
3566 	if (u.desc == NULL) {
3567 		goto error;
3568 	}
3569 	if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) {
3570 		goto error;
3571 	}
3572 	switch (u.desc->bDescriptorSubtype) {
3573 	case UDESCSUB_AC_INPUT:
3574 		len += sizeof(*u.it);
3575 		break;
3576 
3577 	case UDESCSUB_AC_OUTPUT:
3578 		len += sizeof(*u.ot);
3579 		break;
3580 
3581 	case UDESCSUB_AC_MIXER:
3582 		len += sizeof(*u.mu);
3583 
3584 		if (u.desc->bLength < len) {
3585 			goto error;
3586 		}
3587 		len += u.mu->bNrInPins;
3588 
3589 		if (u.desc->bLength < len) {
3590 			goto error;
3591 		}
3592 		d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins);
3593 
3594 		len += sizeof(*d1);
3595 		break;
3596 
3597 	case UDESCSUB_AC_SELECTOR:
3598 		len += sizeof(*u.su);
3599 
3600 		if (u.desc->bLength < len) {
3601 			goto error;
3602 		}
3603 		len += u.su->bNrInPins + 1;
3604 		break;
3605 
3606 	case UDESCSUB_AC_FEATURE:
3607 		len += sizeof(*u.fu) + 1;
3608 
3609 		if (u.desc->bLength < len)
3610 			goto error;
3611 
3612 		len += u.fu->bControlSize;
3613 		break;
3614 
3615 	case UDESCSUB_AC_PROCESSING:
3616 		len += sizeof(*u.pu);
3617 
3618 		if (u.desc->bLength < len) {
3619 			goto error;
3620 		}
3621 		len += u.pu->bNrInPins;
3622 
3623 		if (u.desc->bLength < len) {
3624 			goto error;
3625 		}
3626 		u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins);
3627 
3628 		len += sizeof(*u1);
3629 
3630 		if (u.desc->bLength < len) {
3631 			goto error;
3632 		}
3633 		len += u1->bControlSize;
3634 
3635 		break;
3636 
3637 	case UDESCSUB_AC_EXTENSION:
3638 		len += sizeof(*u.eu);
3639 
3640 		if (u.desc->bLength < len) {
3641 			goto error;
3642 		}
3643 		len += u.eu->bNrInPins;
3644 
3645 		if (u.desc->bLength < len) {
3646 			goto error;
3647 		}
3648 		e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins);
3649 
3650 		len += sizeof(*e1);
3651 
3652 		if (u.desc->bLength < len) {
3653 			goto error;
3654 		}
3655 		len += e1->bControlSize;
3656 		break;
3657 
3658 	default:
3659 		goto error;
3660 	}
3661 
3662 	if (u.desc->bLength < len) {
3663 		goto error;
3664 	}
3665 	return (u.desc);
3666 
3667 error:
3668 	if (u.desc) {
3669 		DPRINTF("invalid descriptor, type=%d, "
3670 		    "sub_type=%d, len=%d of %d bytes\n",
3671 		    u.desc->bDescriptorType,
3672 		    u.desc->bDescriptorSubtype,
3673 		    u.desc->bLength, len);
3674 	}
3675 	return (NULL);
3676 }
3677 
3678 static const void *
3679 uaudio20_mixer_verify_desc(const void *arg, uint32_t len)
3680 {
3681 	const struct usb_audio20_mixer_unit_1 *d1;
3682 	const struct usb_audio20_extension_unit_1 *e1;
3683 	const struct usb_audio20_processing_unit_1 *u1;
3684 	const struct usb_audio20_clock_selector_unit_1 *c1;
3685 
3686 	union {
3687 		const struct usb_descriptor *desc;
3688 		const struct usb_audio20_clock_source_unit *csrc;
3689 		const struct usb_audio20_clock_selector_unit_0 *csel;
3690 		const struct usb_audio20_clock_multiplier_unit *cmul;
3691 		const struct usb_audio20_input_terminal *it;
3692 		const struct usb_audio20_output_terminal *ot;
3693 		const struct usb_audio20_mixer_unit_0 *mu;
3694 		const struct usb_audio20_selector_unit *su;
3695 		const struct usb_audio20_feature_unit *fu;
3696 		const struct usb_audio20_sample_rate_unit *ru;
3697 		const struct usb_audio20_processing_unit_0 *pu;
3698 		const struct usb_audio20_extension_unit_0 *eu;
3699 		const struct usb_audio20_effect_unit *ef;
3700 	}     u;
3701 
3702 	u.desc = arg;
3703 
3704 	if (u.desc == NULL)
3705 		goto error;
3706 
3707 	if (u.desc->bDescriptorType != UDESC_CS_INTERFACE)
3708 		goto error;
3709 
3710 	switch (u.desc->bDescriptorSubtype) {
3711 	case UDESCSUB_AC_INPUT:
3712 		len += sizeof(*u.it);
3713 		break;
3714 
3715 	case UDESCSUB_AC_OUTPUT:
3716 		len += sizeof(*u.ot);
3717 		break;
3718 
3719 	case UDESCSUB_AC_MIXER:
3720 		len += sizeof(*u.mu);
3721 
3722 		if (u.desc->bLength < len)
3723 			goto error;
3724 		len += u.mu->bNrInPins;
3725 
3726 		if (u.desc->bLength < len)
3727 			goto error;
3728 
3729 		d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins);
3730 
3731 		len += sizeof(*d1) + d1->bNrChannels;
3732 		break;
3733 
3734 	case UDESCSUB_AC_SELECTOR:
3735 		len += sizeof(*u.su);
3736 
3737 		if (u.desc->bLength < len)
3738 			goto error;
3739 
3740 		len += u.su->bNrInPins + 1;
3741 		break;
3742 
3743 	case UDESCSUB_AC_FEATURE:
3744 		len += sizeof(*u.fu) + 1;
3745 		break;
3746 
3747 	case UDESCSUB_AC_EFFECT:
3748 		len += sizeof(*u.ef) + 4;
3749 		break;
3750 
3751 	case UDESCSUB_AC_PROCESSING_V2:
3752 		len += sizeof(*u.pu);
3753 
3754 		if (u.desc->bLength < len)
3755 			goto error;
3756 
3757 		len += u.pu->bNrInPins;
3758 
3759 		if (u.desc->bLength < len)
3760 			goto error;
3761 
3762 		u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins);
3763 
3764 		len += sizeof(*u1);
3765 		break;
3766 
3767 	case UDESCSUB_AC_EXTENSION_V2:
3768 		len += sizeof(*u.eu);
3769 
3770 		if (u.desc->bLength < len)
3771 			goto error;
3772 
3773 		len += u.eu->bNrInPins;
3774 
3775 		if (u.desc->bLength < len)
3776 			goto error;
3777 
3778 		e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins);
3779 
3780 		len += sizeof(*e1);
3781 		break;
3782 
3783 	case UDESCSUB_AC_CLOCK_SRC:
3784 		len += sizeof(*u.csrc);
3785 		break;
3786 
3787 	case UDESCSUB_AC_CLOCK_SEL:
3788 		len += sizeof(*u.csel);
3789 
3790 		if (u.desc->bLength < len)
3791 			goto error;
3792 
3793 		len += u.csel->bNrInPins;
3794 
3795 		if (u.desc->bLength < len)
3796 			goto error;
3797 
3798 		c1 = (const void *)(u.csel->baCSourceId + u.csel->bNrInPins);
3799 
3800 		len += sizeof(*c1);
3801 		break;
3802 
3803 	case UDESCSUB_AC_CLOCK_MUL:
3804 		len += sizeof(*u.cmul);
3805 		break;
3806 
3807 	case UDESCSUB_AC_SAMPLE_RT:
3808 		len += sizeof(*u.ru);
3809 		break;
3810 
3811 	default:
3812 		goto error;
3813 	}
3814 
3815 	if (u.desc->bLength < len)
3816 		goto error;
3817 
3818 	return (u.desc);
3819 
3820 error:
3821 	if (u.desc) {
3822 		DPRINTF("invalid descriptor, type=%d, "
3823 		    "sub_type=%d, len=%d of %d bytes\n",
3824 		    u.desc->bDescriptorType,
3825 		    u.desc->bDescriptorSubtype,
3826 		    u.desc->bLength, len);
3827 	}
3828 	return (NULL);
3829 }
3830 
3831 static struct usb_audio_cluster
3832 uaudio_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
3833 {
3834 	struct usb_audio_cluster r;
3835 	const struct usb_descriptor *dp;
3836 	uint8_t i;
3837 
3838 	for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) {	/* avoid infinite loops */
3839 		dp = iot[id].u.desc;
3840 		if (dp == NULL) {
3841 			goto error;
3842 		}
3843 		switch (dp->bDescriptorSubtype) {
3844 		case UDESCSUB_AC_INPUT:
3845 			r.bNrChannels = iot[id].u.it_v1->bNrChannels;
3846 			r.wChannelConfig[0] = iot[id].u.it_v1->wChannelConfig[0];
3847 			r.wChannelConfig[1] = iot[id].u.it_v1->wChannelConfig[1];
3848 			r.iChannelNames = iot[id].u.it_v1->iChannelNames;
3849 			goto done;
3850 
3851 		case UDESCSUB_AC_OUTPUT:
3852 			id = iot[id].u.ot_v1->bSourceId;
3853 			break;
3854 
3855 		case UDESCSUB_AC_MIXER:
3856 			r = *(const struct usb_audio_cluster *)
3857 			    &iot[id].u.mu_v1->baSourceId[
3858 			    iot[id].u.mu_v1->bNrInPins];
3859 			goto done;
3860 
3861 		case UDESCSUB_AC_SELECTOR:
3862 			if (iot[id].u.su_v1->bNrInPins > 0) {
3863 				/* XXX This is not really right */
3864 				id = iot[id].u.su_v1->baSourceId[0];
3865 			}
3866 			break;
3867 
3868 		case UDESCSUB_AC_FEATURE:
3869 			id = iot[id].u.fu_v1->bSourceId;
3870 			break;
3871 
3872 		case UDESCSUB_AC_PROCESSING:
3873 			r = *((const struct usb_audio_cluster *)
3874 			    &iot[id].u.pu_v1->baSourceId[
3875 			    iot[id].u.pu_v1->bNrInPins]);
3876 			goto done;
3877 
3878 		case UDESCSUB_AC_EXTENSION:
3879 			r = *((const struct usb_audio_cluster *)
3880 			    &iot[id].u.eu_v1->baSourceId[
3881 			    iot[id].u.eu_v1->bNrInPins]);
3882 			goto done;
3883 
3884 		default:
3885 			goto error;
3886 		}
3887 	}
3888 error:
3889 	DPRINTF("bad data\n");
3890 	memset(&r, 0, sizeof(r));
3891 done:
3892 	return (r);
3893 }
3894 
3895 static struct usb_audio20_cluster
3896 uaudio20_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
3897 {
3898 	struct usb_audio20_cluster r;
3899 	const struct usb_descriptor *dp;
3900 	uint8_t i;
3901 
3902 	for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) {	/* avoid infinite loops */
3903 		dp = iot[id].u.desc;
3904 		if (dp == NULL)
3905 			goto error;
3906 
3907 		switch (dp->bDescriptorSubtype) {
3908 		case UDESCSUB_AC_INPUT:
3909 			r.bNrChannels = iot[id].u.it_v2->bNrChannels;
3910 			r.bmChannelConfig[0] = iot[id].u.it_v2->bmChannelConfig[0];
3911 			r.bmChannelConfig[1] = iot[id].u.it_v2->bmChannelConfig[1];
3912 			r.bmChannelConfig[2] = iot[id].u.it_v2->bmChannelConfig[2];
3913 			r.bmChannelConfig[3] = iot[id].u.it_v2->bmChannelConfig[3];
3914 			r.iChannelNames = iot[id].u.it_v2->iTerminal;
3915 			goto done;
3916 
3917 		case UDESCSUB_AC_OUTPUT:
3918 			id = iot[id].u.ot_v2->bSourceId;
3919 			break;
3920 
3921 		case UDESCSUB_AC_MIXER:
3922 			r = *(const struct usb_audio20_cluster *)
3923 			    &iot[id].u.mu_v2->baSourceId[
3924 			    iot[id].u.mu_v2->bNrInPins];
3925 			goto done;
3926 
3927 		case UDESCSUB_AC_SELECTOR:
3928 			if (iot[id].u.su_v2->bNrInPins > 0) {
3929 				/* XXX This is not really right */
3930 				id = iot[id].u.su_v2->baSourceId[0];
3931 			}
3932 			break;
3933 
3934 		case UDESCSUB_AC_SAMPLE_RT:
3935 			id = iot[id].u.ru_v2->bSourceId;
3936 			break;
3937 
3938 		case UDESCSUB_AC_EFFECT:
3939 			id = iot[id].u.ef_v2->bSourceId;
3940 			break;
3941 
3942 		case UDESCSUB_AC_FEATURE:
3943 			id = iot[id].u.fu_v2->bSourceId;
3944 			break;
3945 
3946 		case UDESCSUB_AC_PROCESSING_V2:
3947 			r = *((const struct usb_audio20_cluster *)
3948 			    &iot[id].u.pu_v2->baSourceId[
3949 			    iot[id].u.pu_v2->bNrInPins]);
3950 			goto done;
3951 
3952 		case UDESCSUB_AC_EXTENSION_V2:
3953 			r = *((const struct usb_audio20_cluster *)
3954 			    &iot[id].u.eu_v2->baSourceId[
3955 			    iot[id].u.eu_v2->bNrInPins]);
3956 			goto done;
3957 
3958 		default:
3959 			goto error;
3960 		}
3961 	}
3962 error:
3963 	DPRINTF("Bad data!\n");
3964 	memset(&r, 0, sizeof(r));
3965 done:
3966 	return (r);
3967 }
3968 
3969 static uint16_t
3970 uaudio_mixer_determine_class(const struct uaudio_terminal_node *iot,
3971     struct uaudio_mixer_node *mix)
3972 {
3973 	uint16_t terminal_type = 0x0000;
3974 	const struct uaudio_terminal_node *input[2];
3975 	const struct uaudio_terminal_node *output[2];
3976 
3977 	input[0] = uaudio_mixer_get_input(iot, 0);
3978 	input[1] = uaudio_mixer_get_input(iot, 1);
3979 
3980 	output[0] = uaudio_mixer_get_output(iot, 0);
3981 	output[1] = uaudio_mixer_get_output(iot, 1);
3982 
3983 	/*
3984 	 * check if there is only
3985 	 * one output terminal:
3986 	 */
3987 	if (output[0] && (!output[1])) {
3988 		terminal_type =
3989 		    UGETW(output[0]->u.ot_v1->wTerminalType);
3990 	}
3991 	/*
3992 	 * If the only output terminal is USB,
3993 	 * the class is UAC_RECORD.
3994 	 */
3995 	if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
3996 
3997 		mix->class = UAC_RECORD;
3998 		if (input[0] && (!input[1])) {
3999 			terminal_type =
4000 			    UGETW(input[0]->u.it_v1->wTerminalType);
4001 		} else {
4002 			terminal_type = 0;
4003 		}
4004 		goto done;
4005 	}
4006 	/*
4007 	 * if the unit is connected to just
4008 	 * one input terminal, the
4009 	 * class is UAC_INPUT:
4010 	 */
4011 	if (input[0] && (!input[1])) {
4012 		mix->class = UAC_INPUT;
4013 		terminal_type =
4014 		    UGETW(input[0]->u.it_v1->wTerminalType);
4015 		goto done;
4016 	}
4017 	/*
4018 	 * Otherwise, the class is UAC_OUTPUT.
4019 	 */
4020 	mix->class = UAC_OUTPUT;
4021 done:
4022 	return (terminal_type);
4023 }
4024 
4025 static uint16_t
4026 uaudio20_mixer_determine_class(const struct uaudio_terminal_node *iot,
4027     struct uaudio_mixer_node *mix)
4028 {
4029 	uint16_t terminal_type = 0x0000;
4030 	const struct uaudio_terminal_node *input[2];
4031 	const struct uaudio_terminal_node *output[2];
4032 
4033 	input[0] = uaudio_mixer_get_input(iot, 0);
4034 	input[1] = uaudio_mixer_get_input(iot, 1);
4035 
4036 	output[0] = uaudio_mixer_get_output(iot, 0);
4037 	output[1] = uaudio_mixer_get_output(iot, 1);
4038 
4039 	/*
4040 	 * check if there is only
4041 	 * one output terminal:
4042 	 */
4043 	if (output[0] && (!output[1]))
4044 		terminal_type = UGETW(output[0]->u.ot_v2->wTerminalType);
4045 	/*
4046 	 * If the only output terminal is USB,
4047 	 * the class is UAC_RECORD.
4048 	 */
4049 	if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
4050 
4051 		mix->class = UAC_RECORD;
4052 		if (input[0] && (!input[1])) {
4053 			terminal_type =
4054 			    UGETW(input[0]->u.it_v2->wTerminalType);
4055 		} else {
4056 			terminal_type = 0;
4057 		}
4058 		goto done;
4059 	}
4060 	/*
4061 	 * if the unit is connected to just
4062 	 * one input terminal, the
4063 	 * class is UAC_INPUT:
4064 	 */
4065 	if (input[0] && (!input[1])) {
4066 		mix->class = UAC_INPUT;
4067 		terminal_type =
4068 		    UGETW(input[0]->u.it_v2->wTerminalType);
4069 		goto done;
4070 	}
4071 	/*
4072 	 * Otherwise, the class is UAC_OUTPUT.
4073 	 */
4074 	mix->class = UAC_OUTPUT;
4075 done:
4076 	return (terminal_type);
4077 }
4078 
4079 struct uaudio_tt_to_feature {
4080 	uint16_t terminal_type;
4081 	uint16_t feature;
4082 };
4083 
4084 static const struct uaudio_tt_to_feature uaudio_tt_to_feature[] = {
4085 
4086 	{UAT_STREAM, SOUND_MIXER_PCM},
4087 
4088 	{UATI_MICROPHONE, SOUND_MIXER_MIC},
4089 	{UATI_DESKMICROPHONE, SOUND_MIXER_MIC},
4090 	{UATI_PERSONALMICROPHONE, SOUND_MIXER_MIC},
4091 	{UATI_OMNIMICROPHONE, SOUND_MIXER_MIC},
4092 	{UATI_MICROPHONEARRAY, SOUND_MIXER_MIC},
4093 	{UATI_PROCMICROPHONEARR, SOUND_MIXER_MIC},
4094 
4095 	{UATO_SPEAKER, SOUND_MIXER_SPEAKER},
4096 	{UATO_DESKTOPSPEAKER, SOUND_MIXER_SPEAKER},
4097 	{UATO_ROOMSPEAKER, SOUND_MIXER_SPEAKER},
4098 	{UATO_COMMSPEAKER, SOUND_MIXER_SPEAKER},
4099 
4100 	{UATE_ANALOGCONN, SOUND_MIXER_LINE},
4101 	{UATE_LINECONN, SOUND_MIXER_LINE},
4102 	{UATE_LEGACYCONN, SOUND_MIXER_LINE},
4103 
4104 	{UATE_DIGITALAUIFC, SOUND_MIXER_ALTPCM},
4105 	{UATE_SPDIF, SOUND_MIXER_ALTPCM},
4106 	{UATE_1394DA, SOUND_MIXER_ALTPCM},
4107 	{UATE_1394DV, SOUND_MIXER_ALTPCM},
4108 
4109 	{UATF_CDPLAYER, SOUND_MIXER_CD},
4110 
4111 	{UATF_SYNTHESIZER, SOUND_MIXER_SYNTH},
4112 
4113 	{UATF_VIDEODISCAUDIO, SOUND_MIXER_VIDEO},
4114 	{UATF_DVDAUDIO, SOUND_MIXER_VIDEO},
4115 	{UATF_TVTUNERAUDIO, SOUND_MIXER_VIDEO},
4116 
4117 	/* telephony terminal types */
4118 	{UATT_UNDEFINED, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4119 	{UATT_PHONELINE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4120 	{UATT_TELEPHONE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4121 	{UATT_DOWNLINEPHONE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4122 
4123 	{UATF_RADIORECV, SOUND_MIXER_RADIO},
4124 	{UATF_RADIOXMIT, SOUND_MIXER_RADIO},
4125 
4126 	{UAT_UNDEFINED, SOUND_MIXER_VOLUME},
4127 	{UAT_VENDOR, SOUND_MIXER_VOLUME},
4128 	{UATI_UNDEFINED, SOUND_MIXER_VOLUME},
4129 
4130 	/* output terminal types */
4131 	{UATO_UNDEFINED, SOUND_MIXER_VOLUME},
4132 	{UATO_DISPLAYAUDIO, SOUND_MIXER_VOLUME},
4133 	{UATO_SUBWOOFER, SOUND_MIXER_VOLUME},
4134 	{UATO_HEADPHONES, SOUND_MIXER_VOLUME},
4135 
4136 	/* bidir terminal types */
4137 	{UATB_UNDEFINED, SOUND_MIXER_VOLUME},
4138 	{UATB_HANDSET, SOUND_MIXER_VOLUME},
4139 	{UATB_HEADSET, SOUND_MIXER_VOLUME},
4140 	{UATB_SPEAKERPHONE, SOUND_MIXER_VOLUME},
4141 	{UATB_SPEAKERPHONEESUP, SOUND_MIXER_VOLUME},
4142 	{UATB_SPEAKERPHONEECANC, SOUND_MIXER_VOLUME},
4143 
4144 	/* external terminal types */
4145 	{UATE_UNDEFINED, SOUND_MIXER_VOLUME},
4146 
4147 	/* embedded function terminal types */
4148 	{UATF_UNDEFINED, SOUND_MIXER_VOLUME},
4149 	{UATF_CALIBNOISE, SOUND_MIXER_VOLUME},
4150 	{UATF_EQUNOISE, SOUND_MIXER_VOLUME},
4151 	{UATF_DAT, SOUND_MIXER_VOLUME},
4152 	{UATF_DCC, SOUND_MIXER_VOLUME},
4153 	{UATF_MINIDISK, SOUND_MIXER_VOLUME},
4154 	{UATF_ANALOGTAPE, SOUND_MIXER_VOLUME},
4155 	{UATF_PHONOGRAPH, SOUND_MIXER_VOLUME},
4156 	{UATF_VCRAUDIO, SOUND_MIXER_VOLUME},
4157 	{UATF_SATELLITE, SOUND_MIXER_VOLUME},
4158 	{UATF_CABLETUNER, SOUND_MIXER_VOLUME},
4159 	{UATF_DSS, SOUND_MIXER_VOLUME},
4160 	{UATF_MULTITRACK, SOUND_MIXER_VOLUME},
4161 	{0xffff, SOUND_MIXER_VOLUME},
4162 
4163 	/* default */
4164 	{0x0000, SOUND_MIXER_VOLUME},
4165 };
4166 
4167 static uint16_t
4168 uaudio_mixer_feature_name(const struct uaudio_terminal_node *iot,
4169     struct uaudio_mixer_node *mix)
4170 {
4171 	const struct uaudio_tt_to_feature *uat = uaudio_tt_to_feature;
4172 	uint16_t terminal_type = uaudio_mixer_determine_class(iot, mix);
4173 
4174 	if ((mix->class == UAC_RECORD) && (terminal_type == 0)) {
4175 		return (SOUND_MIXER_IMIX);
4176 	}
4177 	while (uat->terminal_type) {
4178 		if (uat->terminal_type == terminal_type) {
4179 			break;
4180 		}
4181 		uat++;
4182 	}
4183 
4184 	DPRINTF("terminal_type=0x%04x -> %d\n",
4185 	    terminal_type, uat->feature);
4186 
4187 	return (uat->feature);
4188 }
4189 
4190 static uint16_t
4191 uaudio20_mixer_feature_name(const struct uaudio_terminal_node *iot,
4192     struct uaudio_mixer_node *mix)
4193 {
4194 	const struct uaudio_tt_to_feature *uat;
4195 	uint16_t terminal_type = uaudio20_mixer_determine_class(iot, mix);
4196 
4197 	if ((mix->class == UAC_RECORD) && (terminal_type == 0))
4198 		return (SOUND_MIXER_IMIX);
4199 
4200 	for (uat = uaudio_tt_to_feature; uat->terminal_type != 0; uat++) {
4201 		if (uat->terminal_type == terminal_type)
4202 			break;
4203 	}
4204 
4205 	DPRINTF("terminal_type=0x%04x -> %d\n",
4206 	    terminal_type, uat->feature);
4207 
4208 	return (uat->feature);
4209 }
4210 
4211 static const struct uaudio_terminal_node *
4212 uaudio_mixer_get_input(const struct uaudio_terminal_node *iot, uint8_t i)
4213 {
4214 	struct uaudio_terminal_node *root = iot->root;
4215 	uint8_t n;
4216 
4217 	n = iot->usr.id_max;
4218 	do {
4219 		if (iot->usr.bit_input[n / 8] & (1 << (n % 8))) {
4220 			if (!i--)
4221 				return (root + n);
4222 		}
4223 	} while (n--);
4224 
4225 	return (NULL);
4226 }
4227 
4228 static const struct uaudio_terminal_node *
4229 uaudio_mixer_get_output(const struct uaudio_terminal_node *iot, uint8_t i)
4230 {
4231 	struct uaudio_terminal_node *root = iot->root;
4232 	uint8_t n;
4233 
4234 	n = iot->usr.id_max;
4235 	do {
4236 		if (iot->usr.bit_output[n / 8] & (1 << (n % 8))) {
4237 			if (!i--)
4238 				return (root + n);
4239 		}
4240 	} while (n--);
4241 
4242 	return (NULL);
4243 }
4244 
4245 static void
4246 uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *root,
4247     const uint8_t *p_id, uint8_t n_id,
4248     struct uaudio_search_result *info)
4249 {
4250 	struct uaudio_terminal_node *iot;
4251 	uint8_t n;
4252 	uint8_t i;
4253 	uint8_t is_last;
4254 
4255 top:
4256 	for (n = 0; n < n_id; n++) {
4257 
4258 		i = p_id[n];
4259 
4260 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4261 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4262 			return;
4263 		}
4264 
4265 		info->recurse_level++;
4266 
4267 		iot = (root + i);
4268 
4269 		if (iot->u.desc == NULL)
4270 			continue;
4271 
4272 		is_last = ((n + 1) == n_id);
4273 
4274 		switch (iot->u.desc->bDescriptorSubtype) {
4275 		case UDESCSUB_AC_INPUT:
4276 			info->bit_input[i / 8] |= (1 << (i % 8));
4277 			break;
4278 
4279 		case UDESCSUB_AC_FEATURE:
4280 			if (is_last) {
4281 				p_id = &iot->u.fu_v1->bSourceId;
4282 				n_id = 1;
4283 				goto top;
4284 			}
4285 			uaudio_mixer_find_inputs_sub(
4286 			    root, &iot->u.fu_v1->bSourceId, 1, info);
4287 			break;
4288 
4289 		case UDESCSUB_AC_OUTPUT:
4290 			if (is_last) {
4291 				p_id = &iot->u.ot_v1->bSourceId;
4292 				n_id = 1;
4293 				goto top;
4294 			}
4295 			uaudio_mixer_find_inputs_sub(
4296 			    root, &iot->u.ot_v1->bSourceId, 1, info);
4297 			break;
4298 
4299 		case UDESCSUB_AC_MIXER:
4300 			if (is_last) {
4301 				p_id = iot->u.mu_v1->baSourceId;
4302 				n_id = iot->u.mu_v1->bNrInPins;
4303 				goto top;
4304 			}
4305 			uaudio_mixer_find_inputs_sub(
4306 			    root, iot->u.mu_v1->baSourceId,
4307 			    iot->u.mu_v1->bNrInPins, info);
4308 			break;
4309 
4310 		case UDESCSUB_AC_SELECTOR:
4311 			if (is_last) {
4312 				p_id = iot->u.su_v1->baSourceId;
4313 				n_id = iot->u.su_v1->bNrInPins;
4314 				goto top;
4315 			}
4316 			uaudio_mixer_find_inputs_sub(
4317 			    root, iot->u.su_v1->baSourceId,
4318 			    iot->u.su_v1->bNrInPins, info);
4319 			break;
4320 
4321 		case UDESCSUB_AC_PROCESSING:
4322 			if (is_last) {
4323 				p_id = iot->u.pu_v1->baSourceId;
4324 				n_id = iot->u.pu_v1->bNrInPins;
4325 				goto top;
4326 			}
4327 			uaudio_mixer_find_inputs_sub(
4328 			    root, iot->u.pu_v1->baSourceId,
4329 			    iot->u.pu_v1->bNrInPins, info);
4330 			break;
4331 
4332 		case UDESCSUB_AC_EXTENSION:
4333 			if (is_last) {
4334 				p_id = iot->u.eu_v1->baSourceId;
4335 				n_id = iot->u.eu_v1->bNrInPins;
4336 				goto top;
4337 			}
4338 			uaudio_mixer_find_inputs_sub(
4339 			    root, iot->u.eu_v1->baSourceId,
4340 			    iot->u.eu_v1->bNrInPins, info);
4341 			break;
4342 
4343 		default:
4344 			break;
4345 		}
4346 	}
4347 }
4348 
4349 static void
4350 uaudio20_mixer_find_inputs_sub(struct uaudio_terminal_node *root,
4351     const uint8_t *p_id, uint8_t n_id,
4352     struct uaudio_search_result *info)
4353 {
4354 	struct uaudio_terminal_node *iot;
4355 	uint8_t n;
4356 	uint8_t i;
4357 	uint8_t is_last;
4358 
4359 top:
4360 	for (n = 0; n < n_id; n++) {
4361 
4362 		i = p_id[n];
4363 
4364 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4365 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4366 			return;
4367 		}
4368 
4369 		info->recurse_level++;
4370 
4371 		iot = (root + i);
4372 
4373 		if (iot->u.desc == NULL)
4374 			continue;
4375 
4376 		is_last = ((n + 1) == n_id);
4377 
4378 		switch (iot->u.desc->bDescriptorSubtype) {
4379 		case UDESCSUB_AC_INPUT:
4380 			info->bit_input[i / 8] |= (1 << (i % 8));
4381 			break;
4382 
4383 		case UDESCSUB_AC_OUTPUT:
4384 			if (is_last) {
4385 				p_id = &iot->u.ot_v2->bSourceId;
4386 				n_id = 1;
4387 				goto top;
4388 			}
4389 			uaudio20_mixer_find_inputs_sub(
4390 			    root, &iot->u.ot_v2->bSourceId, 1, info);
4391 			break;
4392 
4393 		case UDESCSUB_AC_MIXER:
4394 			if (is_last) {
4395 				p_id = iot->u.mu_v2->baSourceId;
4396 				n_id = iot->u.mu_v2->bNrInPins;
4397 				goto top;
4398 			}
4399 			uaudio20_mixer_find_inputs_sub(
4400 			    root, iot->u.mu_v2->baSourceId,
4401 			    iot->u.mu_v2->bNrInPins, info);
4402 			break;
4403 
4404 		case UDESCSUB_AC_SELECTOR:
4405 			if (is_last) {
4406 				p_id = iot->u.su_v2->baSourceId;
4407 				n_id = iot->u.su_v2->bNrInPins;
4408 				goto top;
4409 			}
4410 			uaudio20_mixer_find_inputs_sub(
4411 			    root, iot->u.su_v2->baSourceId,
4412 			    iot->u.su_v2->bNrInPins, info);
4413 			break;
4414 
4415 		case UDESCSUB_AC_SAMPLE_RT:
4416 			if (is_last) {
4417 				p_id = &iot->u.ru_v2->bSourceId;
4418 				n_id = 1;
4419 				goto top;
4420 			}
4421 			uaudio20_mixer_find_inputs_sub(
4422 			    root, &iot->u.ru_v2->bSourceId,
4423 			    1, info);
4424 			break;
4425 
4426 		case UDESCSUB_AC_EFFECT:
4427 			if (is_last) {
4428 				p_id = &iot->u.ef_v2->bSourceId;
4429 				n_id = 1;
4430 				goto top;
4431 			}
4432 			uaudio20_mixer_find_inputs_sub(
4433 			    root, &iot->u.ef_v2->bSourceId,
4434 			    1, info);
4435 			break;
4436 
4437 		case UDESCSUB_AC_FEATURE:
4438 			if (is_last) {
4439 				p_id = &iot->u.fu_v2->bSourceId;
4440 				n_id = 1;
4441 				goto top;
4442 			}
4443 			uaudio20_mixer_find_inputs_sub(
4444 			    root, &iot->u.fu_v2->bSourceId, 1, info);
4445 			break;
4446 
4447 		case UDESCSUB_AC_PROCESSING_V2:
4448 			if (is_last) {
4449 				p_id = iot->u.pu_v2->baSourceId;
4450 				n_id = iot->u.pu_v2->bNrInPins;
4451 				goto top;
4452 			}
4453 			uaudio20_mixer_find_inputs_sub(
4454 			    root, iot->u.pu_v2->baSourceId,
4455 			    iot->u.pu_v2->bNrInPins, info);
4456 			break;
4457 
4458 		case UDESCSUB_AC_EXTENSION_V2:
4459 			if (is_last) {
4460 				p_id = iot->u.eu_v2->baSourceId;
4461 				n_id = iot->u.eu_v2->bNrInPins;
4462 				goto top;
4463 			}
4464 			uaudio20_mixer_find_inputs_sub(
4465 			    root, iot->u.eu_v2->baSourceId,
4466 			    iot->u.eu_v2->bNrInPins, info);
4467 			break;
4468 		default:
4469 			break;
4470 		}
4471 	}
4472 }
4473 
4474 static void
4475 uaudio20_mixer_find_clocks_sub(struct uaudio_terminal_node *root,
4476     const uint8_t *p_id, uint8_t n_id,
4477     struct uaudio_search_result *info)
4478 {
4479 	struct uaudio_terminal_node *iot;
4480 	uint8_t n;
4481 	uint8_t i;
4482 	uint8_t is_last;
4483 	uint8_t id;
4484 
4485 top:
4486 	for (n = 0; n < n_id; n++) {
4487 
4488 		i = p_id[n];
4489 
4490 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4491 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4492 			return;
4493 		}
4494 
4495 		info->recurse_level++;
4496 
4497 		iot = (root + i);
4498 
4499 		if (iot->u.desc == NULL)
4500 			continue;
4501 
4502 		is_last = ((n + 1) == n_id);
4503 
4504 		switch (iot->u.desc->bDescriptorSubtype) {
4505 		case UDESCSUB_AC_INPUT:
4506 			info->is_input = 1;
4507 			if (is_last) {
4508 				p_id = &iot->u.it_v2->bCSourceId;
4509 				n_id = 1;
4510 				goto top;
4511 			}
4512 			uaudio20_mixer_find_clocks_sub(root,
4513 			    &iot->u.it_v2->bCSourceId, 1, info);
4514 			break;
4515 
4516 		case UDESCSUB_AC_OUTPUT:
4517 			info->is_input = 0;
4518 			if (is_last) {
4519 				p_id = &iot->u.ot_v2->bCSourceId;
4520 				n_id = 1;
4521 				goto top;
4522 			}
4523 			uaudio20_mixer_find_clocks_sub(root,
4524 			    &iot->u.ot_v2->bCSourceId, 1, info);
4525 			break;
4526 
4527 		case UDESCSUB_AC_CLOCK_SEL:
4528 			if (is_last) {
4529 				p_id = iot->u.csel_v2->baCSourceId;
4530 				n_id = iot->u.csel_v2->bNrInPins;
4531 				goto top;
4532 			}
4533 			uaudio20_mixer_find_clocks_sub(root,
4534 			    iot->u.csel_v2->baCSourceId,
4535 			    iot->u.csel_v2->bNrInPins, info);
4536 			break;
4537 
4538 		case UDESCSUB_AC_CLOCK_MUL:
4539 			if (is_last) {
4540 				p_id = &iot->u.cmul_v2->bCSourceId;
4541 				n_id = 1;
4542 				goto top;
4543 			}
4544 			uaudio20_mixer_find_clocks_sub(root,
4545 			    &iot->u.cmul_v2->bCSourceId,
4546 			    1, info);
4547 			break;
4548 
4549 		case UDESCSUB_AC_CLOCK_SRC:
4550 
4551 			id = iot->u.csrc_v2->bClockId;
4552 
4553 			switch (info->is_input) {
4554 			case 0:
4555 				info->bit_output[id / 8] |= (1 << (id % 8));
4556 				break;
4557 			case 1:
4558 				info->bit_input[id / 8] |= (1 << (id % 8));
4559 				break;
4560 			default:
4561 				break;
4562 			}
4563 			break;
4564 
4565 		default:
4566 			break;
4567 		}
4568 	}
4569 }
4570 
4571 static void
4572 uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *root, uint8_t id,
4573     uint8_t n_id, struct uaudio_search_result *info)
4574 {
4575 	struct uaudio_terminal_node *iot = (root + id);
4576 	uint8_t j;
4577 
4578 	j = n_id;
4579 	do {
4580 		if ((j != id) && ((root + j)->u.desc) &&
4581 		    ((root + j)->u.desc->bDescriptorSubtype == UDESCSUB_AC_OUTPUT)) {
4582 
4583 			/*
4584 			 * "j" (output) <--- virtual wire <--- "id" (input)
4585 			 *
4586 			 * if "j" has "id" on the input, then "id" have "j" on
4587 			 * the output, because they are connected:
4588 			 */
4589 			if ((root + j)->usr.bit_input[id / 8] & (1 << (id % 8))) {
4590 				iot->usr.bit_output[j / 8] |= (1 << (j % 8));
4591 			}
4592 		}
4593 	} while (j--);
4594 }
4595 
4596 static void
4597 uaudio_mixer_fill_info(struct uaudio_softc *sc,
4598     struct usb_device *udev, void *desc)
4599 {
4600 	const struct usb_audio_control_descriptor *acdp;
4601 	struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
4602 	const struct usb_descriptor *dp;
4603 	const struct usb_audio_unit *au;
4604 	struct uaudio_terminal_node *iot = NULL;
4605 	uint16_t wTotalLen;
4606 	uint8_t ID_max = 0;		/* inclusive */
4607 	uint8_t i;
4608 
4609 	desc = usb_desc_foreach(cd, desc);
4610 
4611 	if (desc == NULL) {
4612 		DPRINTF("no Audio Control header\n");
4613 		goto done;
4614 	}
4615 	acdp = desc;
4616 
4617 	if ((acdp->bLength < sizeof(*acdp)) ||
4618 	    (acdp->bDescriptorType != UDESC_CS_INTERFACE) ||
4619 	    (acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)) {
4620 		DPRINTF("invalid Audio Control header\n");
4621 		goto done;
4622 	}
4623 	/* "wTotalLen" is allowed to be corrupt */
4624 	wTotalLen = UGETW(acdp->wTotalLength) - acdp->bLength;
4625 
4626 	/* get USB audio revision */
4627 	sc->sc_audio_rev = UGETW(acdp->bcdADC);
4628 
4629 	DPRINTFN(3, "found AC header, vers=%03x, len=%d\n",
4630 	    sc->sc_audio_rev, wTotalLen);
4631 
4632 	iot = malloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP,
4633 	    M_WAITOK | M_ZERO);
4634 
4635 	if (iot == NULL) {
4636 		DPRINTF("no memory!\n");
4637 		goto done;
4638 	}
4639 	while ((desc = usb_desc_foreach(cd, desc))) {
4640 
4641 		dp = desc;
4642 
4643 		if (dp->bLength > wTotalLen) {
4644 			break;
4645 		} else {
4646 			wTotalLen -= dp->bLength;
4647 		}
4648 
4649 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30)
4650 			au = NULL;
4651 		else if (sc->sc_audio_rev >= UAUDIO_VERSION_20)
4652 			au = uaudio20_mixer_verify_desc(dp, 0);
4653 		else
4654 			au = uaudio_mixer_verify_desc(dp, 0);
4655 
4656 		if (au) {
4657 			iot[au->bUnitId].u.desc = (const void *)au;
4658 			if (au->bUnitId > ID_max)
4659 				ID_max = au->bUnitId;
4660 		}
4661 	}
4662 
4663 	DPRINTF("Maximum ID=%d\n", ID_max);
4664 
4665 	/*
4666 	 * determine sourcing inputs for
4667 	 * all nodes in the tree:
4668 	 */
4669 	i = ID_max;
4670 	do {
4671 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4672 			/* FALLTHROUGH */
4673 		} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4674 			uaudio20_mixer_find_inputs_sub(iot,
4675 			    &i, 1, &((iot + i)->usr));
4676 
4677 			sc->sc_mixer_clocks.is_input = 255;
4678 			sc->sc_mixer_clocks.recurse_level = 0;
4679 
4680 			uaudio20_mixer_find_clocks_sub(iot,
4681 			    &i, 1, &sc->sc_mixer_clocks);
4682 		} else {
4683 			uaudio_mixer_find_inputs_sub(iot,
4684 			    &i, 1, &((iot + i)->usr));
4685 		}
4686 	} while (i--);
4687 
4688 	/*
4689 	 * determine outputs for
4690 	 * all nodes in the tree:
4691 	 */
4692 	i = ID_max;
4693 	do {
4694 		uaudio_mixer_find_outputs_sub(iot,
4695 		    i, ID_max, &((iot + i)->usr));
4696 	} while (i--);
4697 
4698 	/* set "id_max" and "root" */
4699 
4700 	i = ID_max;
4701 	do {
4702 		(iot + i)->usr.id_max = ID_max;
4703 		(iot + i)->root = iot;
4704 	} while (i--);
4705 
4706 	/*
4707 	 * Scan the config to create a linked list of "mixer" nodes:
4708 	 */
4709 
4710 	i = ID_max;
4711 	do {
4712 		dp = iot[i].u.desc;
4713 
4714 		if (dp == NULL)
4715 			continue;
4716 
4717 		DPRINTFN(11, "id=%d subtype=%d\n",
4718 		    i, dp->bDescriptorSubtype);
4719 
4720 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4721 			continue;
4722 		} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4723 
4724 			switch (dp->bDescriptorSubtype) {
4725 			case UDESCSUB_AC_HEADER:
4726 				DPRINTF("unexpected AC header\n");
4727 				break;
4728 
4729 			case UDESCSUB_AC_INPUT:
4730 			case UDESCSUB_AC_OUTPUT:
4731 			case UDESCSUB_AC_PROCESSING_V2:
4732 			case UDESCSUB_AC_EXTENSION_V2:
4733 			case UDESCSUB_AC_EFFECT:
4734 			case UDESCSUB_AC_CLOCK_SRC:
4735 			case UDESCSUB_AC_CLOCK_SEL:
4736 			case UDESCSUB_AC_CLOCK_MUL:
4737 			case UDESCSUB_AC_SAMPLE_RT:
4738 				break;
4739 
4740 			case UDESCSUB_AC_MIXER:
4741 				uaudio20_mixer_add_mixer(sc, iot, i);
4742 				break;
4743 
4744 			case UDESCSUB_AC_SELECTOR:
4745 				uaudio20_mixer_add_selector(sc, iot, i);
4746 				break;
4747 
4748 			case UDESCSUB_AC_FEATURE:
4749 				uaudio20_mixer_add_feature(sc, iot, i);
4750 				break;
4751 
4752 			default:
4753 				DPRINTF("bad AC desc subtype=0x%02x\n",
4754 				    dp->bDescriptorSubtype);
4755 				break;
4756 			}
4757 			continue;
4758 		}
4759 
4760 		switch (dp->bDescriptorSubtype) {
4761 		case UDESCSUB_AC_HEADER:
4762 			DPRINTF("unexpected AC header\n");
4763 			break;
4764 
4765 		case UDESCSUB_AC_INPUT:
4766 		case UDESCSUB_AC_OUTPUT:
4767 			break;
4768 
4769 		case UDESCSUB_AC_MIXER:
4770 			uaudio_mixer_add_mixer(sc, iot, i);
4771 			break;
4772 
4773 		case UDESCSUB_AC_SELECTOR:
4774 			uaudio_mixer_add_selector(sc, iot, i);
4775 			break;
4776 
4777 		case UDESCSUB_AC_FEATURE:
4778 			uaudio_mixer_add_feature(sc, iot, i);
4779 			break;
4780 
4781 		case UDESCSUB_AC_PROCESSING:
4782 			uaudio_mixer_add_processing(sc, iot, i);
4783 			break;
4784 
4785 		case UDESCSUB_AC_EXTENSION:
4786 			uaudio_mixer_add_extension(sc, iot, i);
4787 			break;
4788 
4789 		default:
4790 			DPRINTF("bad AC desc subtype=0x%02x\n",
4791 			    dp->bDescriptorSubtype);
4792 			break;
4793 		}
4794 
4795 	} while (i--);
4796 
4797 done:
4798 	free(iot, M_TEMP);
4799 }
4800 
4801 static int
4802 uaudio_mixer_get(struct usb_device *udev, uint16_t audio_rev,
4803     uint8_t what, struct uaudio_mixer_node *mc)
4804 {
4805 	struct usb_device_request req;
4806 	int val;
4807 	uint8_t data[2 + (2 * 3)];
4808 	usb_error_t err;
4809 
4810 	if (mc->wValue[0] == -1)
4811 		return (0);
4812 
4813 	if (audio_rev >= UAUDIO_VERSION_30)
4814 		return (0);
4815 	else if (audio_rev >= UAUDIO_VERSION_20) {
4816 		if (what == GET_CUR) {
4817 			req.bRequest = UA20_CS_CUR;
4818 			USETW(req.wLength, 2);
4819 		} else {
4820 			req.bRequest = UA20_CS_RANGE;
4821 			USETW(req.wLength, 8);
4822 		}
4823 	} else {
4824 		uint16_t len = MIX_SIZE(mc->type);
4825 
4826 		req.bRequest = what;
4827 		USETW(req.wLength, len);
4828 	}
4829 
4830 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
4831 	USETW(req.wValue, mc->wValue[0]);
4832 	USETW(req.wIndex, mc->wIndex);
4833 
4834 	memset(data, 0, sizeof(data));
4835 
4836 	err = usbd_do_request(udev, NULL, &req, data);
4837 	if (err) {
4838 		DPRINTF("err=%s\n", usbd_errstr(err));
4839 		return (0);
4840 	}
4841 
4842 	if (audio_rev >= UAUDIO_VERSION_30) {
4843 		val = 0;
4844 	} else if (audio_rev >= UAUDIO_VERSION_20) {
4845 		switch (what) {
4846 		case GET_CUR:
4847 			val = (data[0] | (data[1] << 8));
4848 			break;
4849 		case GET_MIN:
4850 			val = (data[2] | (data[3] << 8));
4851 			break;
4852 		case GET_MAX:
4853 			val = (data[4] | (data[5] << 8));
4854 			break;
4855 		case GET_RES:
4856 			val = (data[6] | (data[7] << 8));
4857 			break;
4858 		default:
4859 			val = 0;
4860 			break;
4861 		}
4862 	} else {
4863 		val = (data[0] | (data[1] << 8));
4864 	}
4865 
4866 	if (what == GET_CUR || what == GET_MIN || what == GET_MAX)
4867 		val = uaudio_mixer_signext(mc->type, val);
4868 
4869 	DPRINTFN(3, "val=%d\n", val);
4870 
4871 	return (val);
4872 }
4873 
4874 static void
4875 uaudio_mixer_write_cfg_callback(struct usb_xfer *xfer, usb_error_t error)
4876 {
4877 	struct usb_device_request req;
4878 	struct uaudio_softc *sc = usbd_xfer_softc(xfer);
4879 	struct uaudio_mixer_node *mc = sc->sc_mixer_curr;
4880 	struct usb_page_cache *pc;
4881 	uint16_t len;
4882 	uint8_t repeat = 1;
4883 	uint8_t update;
4884 	uint8_t chan;
4885 	uint8_t buf[2];
4886 
4887 	DPRINTF("\n");
4888 
4889 	switch (USB_GET_STATE(xfer)) {
4890 	case USB_ST_TRANSFERRED:
4891 tr_transferred:
4892 	case USB_ST_SETUP:
4893 tr_setup:
4894 
4895 		if (mc == NULL) {
4896 			mc = sc->sc_mixer_root;
4897 			sc->sc_mixer_curr = mc;
4898 			sc->sc_mixer_chan = 0;
4899 			repeat = 0;
4900 		}
4901 		while (mc) {
4902 			while (sc->sc_mixer_chan < mc->nchan) {
4903 
4904 				chan = sc->sc_mixer_chan;
4905 
4906 				sc->sc_mixer_chan++;
4907 
4908 				update = ((mc->update[chan / 8] & (1 << (chan % 8))) &&
4909 				    (mc->wValue[chan] != -1));
4910 
4911 				mc->update[chan / 8] &= ~(1 << (chan % 8));
4912 
4913 				if (update) {
4914 
4915 					req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4916 					USETW(req.wValue, mc->wValue[chan]);
4917 					USETW(req.wIndex, mc->wIndex);
4918 
4919 					if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4920 						return;
4921 					} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4922 						len = 2;
4923 						req.bRequest = UA20_CS_CUR;
4924 						USETW(req.wLength, len);
4925 					} else {
4926 						len = MIX_SIZE(mc->type);
4927 						req.bRequest = SET_CUR;
4928 						USETW(req.wLength, len);
4929 					}
4930 
4931 					buf[0] = (mc->wData[chan] & 0xFF);
4932 					buf[1] = (mc->wData[chan] >> 8) & 0xFF;
4933 
4934 					pc = usbd_xfer_get_frame(xfer, 0);
4935 					usbd_copy_in(pc, 0, &req, sizeof(req));
4936 					pc = usbd_xfer_get_frame(xfer, 1);
4937 					usbd_copy_in(pc, 0, buf, len);
4938 
4939 					usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
4940 					usbd_xfer_set_frame_len(xfer, 1, len);
4941 					usbd_xfer_set_frames(xfer, len ? 2 : 1);
4942 					usbd_transfer_submit(xfer);
4943 					return;
4944 				}
4945 			}
4946 
4947 			mc = mc->next;
4948 			sc->sc_mixer_curr = mc;
4949 			sc->sc_mixer_chan = 0;
4950 		}
4951 
4952 		if (repeat) {
4953 			goto tr_setup;
4954 		}
4955 		break;
4956 
4957 	default:			/* Error */
4958 		DPRINTF("error=%s\n", usbd_errstr(error));
4959 		if (error == USB_ERR_CANCELLED) {
4960 			/* do nothing - we are detaching */
4961 			break;
4962 		}
4963 		goto tr_transferred;
4964 	}
4965 }
4966 
4967 static usb_error_t
4968 uaudio_set_speed(struct usb_device *udev, uint8_t endpt, uint32_t speed)
4969 {
4970 	struct usb_device_request req;
4971 	uint8_t data[3];
4972 
4973 	DPRINTFN(6, "endpt=%d speed=%u\n", endpt, speed);
4974 
4975 	req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
4976 	req.bRequest = SET_CUR;
4977 	USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
4978 	USETW(req.wIndex, endpt);
4979 	USETW(req.wLength, 3);
4980 	data[0] = speed;
4981 	data[1] = speed >> 8;
4982 	data[2] = speed >> 16;
4983 
4984 	return (usbd_do_request(udev, NULL, &req, data));
4985 }
4986 
4987 static usb_error_t
4988 uaudio20_set_speed(struct usb_device *udev, uint8_t iface_no,
4989     uint8_t clockid, uint32_t speed)
4990 {
4991 	struct usb_device_request req;
4992 	uint8_t data[4];
4993 
4994 	DPRINTFN(6, "ifaceno=%d clockid=%d speed=%u\n",
4995 	    iface_no, clockid, speed);
4996 
4997 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4998 	req.bRequest = UA20_CS_CUR;
4999 	USETW2(req.wValue, UA20_CS_SAM_FREQ_CONTROL, 0);
5000 	USETW2(req.wIndex, clockid, iface_no);
5001 	USETW(req.wLength, 4);
5002 	data[0] = speed;
5003 	data[1] = speed >> 8;
5004 	data[2] = speed >> 16;
5005 	data[3] = speed >> 24;
5006 
5007 	return (usbd_do_request(udev, NULL, &req, data));
5008 }
5009 
5010 static int
5011 uaudio_mixer_signext(uint8_t type, int val)
5012 {
5013 	if (!MIX_UNSIGNED(type)) {
5014 		if (MIX_SIZE(type) == 2) {
5015 			val = (int16_t)val;
5016 		} else {
5017 			val = (int8_t)val;
5018 		}
5019 	}
5020 	return (val);
5021 }
5022 
5023 static int
5024 uaudio_mixer_bsd2value(struct uaudio_mixer_node *mc, int32_t val)
5025 {
5026 	if (mc->type == MIX_ON_OFF) {
5027 		val = (val != 0);
5028 	} else if (mc->type == MIX_SELECTOR) {
5029 		if ((val < mc->minval) ||
5030 		    (val > mc->maxval)) {
5031 			val = mc->minval;
5032 		}
5033 	} else {
5034 
5035 		/* compute actual volume */
5036 		val = (val * mc->mul) / 255;
5037 
5038 		/* add lower offset */
5039 		val = val + mc->minval;
5040 
5041 		/* make sure we don't write a value out of range */
5042 		if (val > mc->maxval)
5043 			val = mc->maxval;
5044 		else if (val < mc->minval)
5045 			val = mc->minval;
5046 	}
5047 
5048 	DPRINTFN(6, "type=0x%03x val=%d min=%d max=%d val=%d\n",
5049 	    mc->type, val, mc->minval, mc->maxval, val);
5050 	return (val);
5051 }
5052 
5053 static void
5054 uaudio_mixer_ctl_set(struct uaudio_softc *sc, struct uaudio_mixer_node *mc,
5055     uint8_t chan, int32_t val)
5056 {
5057 	val = uaudio_mixer_bsd2value(mc, val);
5058 
5059 	mc->update[chan / 8] |= (1 << (chan % 8));
5060 	mc->wData[chan] = val;
5061 
5062 	/* start the transfer, if not already started */
5063 
5064 	usbd_transfer_start(sc->sc_mixer_xfer[0]);
5065 }
5066 
5067 static void
5068 uaudio_mixer_init(struct uaudio_softc *sc)
5069 {
5070 	struct uaudio_mixer_node *mc;
5071 	int32_t i;
5072 
5073 	for (mc = sc->sc_mixer_root; mc;
5074 	    mc = mc->next) {
5075 
5076 		if (mc->ctl != SOUND_MIXER_NRDEVICES) {
5077 			/*
5078 			 * Set device mask bits. See
5079 			 * /usr/include/machine/soundcard.h
5080 			 */
5081 			sc->sc_mix_info |= (1 << mc->ctl);
5082 		}
5083 		if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
5084 		    (mc->type == MIX_SELECTOR)) {
5085 
5086 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5087 				if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES) {
5088 					continue;
5089 				}
5090 				sc->sc_recsrc_info |= 1 << mc->slctrtype[i - 1];
5091 			}
5092 		}
5093 	}
5094 }
5095 
5096 int
5097 uaudio_mixer_init_sub(struct uaudio_softc *sc, struct snd_mixer *m)
5098 {
5099 	DPRINTF("\n");
5100 
5101 	sc->sc_mixer_lock = mixer_get_lock(m);
5102 	sc->sc_mixer_dev = m;
5103 
5104 	if (usbd_transfer_setup(sc->sc_udev, &sc->sc_mixer_iface_index,
5105 	    sc->sc_mixer_xfer, uaudio_mixer_config, 1, sc,
5106 	    sc->sc_mixer_lock)) {
5107 		DPRINTFN(0, "could not allocate USB "
5108 		    "transfer for audio mixer!\n");
5109 		return (ENOMEM);
5110 	}
5111 	if (!(sc->sc_mix_info & SOUND_MASK_VOLUME)) {
5112 		mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM);
5113 		mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE);
5114 	}
5115 	mix_setdevs(m, sc->sc_mix_info);
5116 	mix_setrecdevs(m, sc->sc_recsrc_info);
5117 	return (0);
5118 }
5119 
5120 int
5121 uaudio_mixer_uninit_sub(struct uaudio_softc *sc)
5122 {
5123 	DPRINTF("\n");
5124 
5125 	usbd_transfer_unsetup(sc->sc_mixer_xfer, 1);
5126 
5127 	sc->sc_mixer_lock = NULL;
5128 
5129 	return (0);
5130 }
5131 
5132 void
5133 uaudio_mixer_set(struct uaudio_softc *sc, unsigned type,
5134     unsigned left, unsigned right)
5135 {
5136 	struct uaudio_mixer_node *mc;
5137 	int chan;
5138 
5139 	for (mc = sc->sc_mixer_root; mc != NULL; mc = mc->next) {
5140 
5141 		if (mc->ctl == type) {
5142 			for (chan = 0; chan < mc->nchan; chan++) {
5143 				uaudio_mixer_ctl_set(sc, mc, chan,
5144 				    (int)((chan == 0 ? left : right) *
5145 				    255) / 100);
5146 			}
5147 		}
5148 	}
5149 }
5150 
5151 uint32_t
5152 uaudio_mixer_setrecsrc(struct uaudio_softc *sc, uint32_t src)
5153 {
5154 	struct uaudio_mixer_node *mc;
5155 	uint32_t mask;
5156 	uint32_t temp;
5157 	int32_t i;
5158 
5159 	for (mc = sc->sc_mixer_root; mc;
5160 	    mc = mc->next) {
5161 
5162 		if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
5163 		    (mc->type == MIX_SELECTOR)) {
5164 
5165 			/* compute selector mask */
5166 
5167 			mask = 0;
5168 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5169 				mask |= (1 << mc->slctrtype[i - 1]);
5170 			}
5171 
5172 			temp = mask & src;
5173 			if (temp == 0) {
5174 				continue;
5175 			}
5176 			/* find the first set bit */
5177 			temp = (-temp) & temp;
5178 
5179 			/* update "src" */
5180 			src &= ~mask;
5181 			src |= temp;
5182 
5183 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5184 				if (temp != (1 << mc->slctrtype[i - 1])) {
5185 					continue;
5186 				}
5187 				uaudio_mixer_ctl_set(sc, mc, 0, i);
5188 				break;
5189 			}
5190 		}
5191 	}
5192 	return (src);
5193 }
5194 
5195 /*========================================================================*
5196  * MIDI support routines
5197  *========================================================================*/
5198 
5199 static void
5200 umidi_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
5201 {
5202 	struct umidi_chan *chan = usbd_xfer_softc(xfer);
5203 	struct umidi_sub_chan *sub;
5204 	struct usb_page_cache *pc;
5205 	uint8_t buf[4];
5206 	uint8_t cmd_len;
5207 	uint8_t cn;
5208 	uint16_t pos;
5209 	int actlen;
5210 
5211 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
5212 
5213 	switch (USB_GET_STATE(xfer)) {
5214 	case USB_ST_TRANSFERRED:
5215 
5216 		DPRINTF("actlen=%d bytes\n", actlen);
5217 
5218 		pos = 0;
5219 		pc = usbd_xfer_get_frame(xfer, 0);
5220 
5221 		while (actlen >= 4) {
5222 
5223 			/* copy out the MIDI data */
5224 			usbd_copy_out(pc, pos, buf, 4);
5225 			/* command length */
5226 			cmd_len = umidi_cmd_to_len[buf[0] & 0xF];
5227 			/* cable number */
5228 			cn = buf[0] >> 4;
5229 			/*
5230 			 * Lookup sub-channel. The index is range
5231 			 * checked below.
5232 			 */
5233 			sub = &chan->sub[cn];
5234 
5235 			if ((cmd_len != 0) &&
5236 			    (cn < chan->max_cable) &&
5237 			    (sub->read_open != 0)) {
5238 
5239 				/* Send data to the application */
5240 				usb_fifo_put_data_linear(
5241 				    sub->fifo.fp[USB_FIFO_RX],
5242 				    buf + 1, cmd_len, 1);
5243 			}
5244 			actlen -= 4;
5245 			pos += 4;
5246 		}
5247 
5248 	case USB_ST_SETUP:
5249 		DPRINTF("start\n");
5250 tr_setup:
5251 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
5252 		usbd_transfer_submit(xfer);
5253 		break;
5254 
5255 	default:
5256 		DPRINTF("error=%s\n", usbd_errstr(error));
5257 
5258 		if (error != USB_ERR_CANCELLED) {
5259 			/* try to clear stall first */
5260 			usbd_xfer_set_stall(xfer);
5261 			goto tr_setup;
5262 		}
5263 		break;
5264 	}
5265 }
5266 
5267 /*
5268  * The following statemachine, that converts MIDI commands to
5269  * USB MIDI packets, derives from Linux's usbmidi.c, which
5270  * was written by "Clemens Ladisch":
5271  *
5272  * Returns:
5273  *    0: No command
5274  * Else: Command is complete
5275  */
5276 static uint8_t
5277 umidi_convert_to_usb(struct umidi_sub_chan *sub, uint8_t cn, uint8_t b)
5278 {
5279 	uint8_t p0 = (cn << 4);
5280 
5281 	if (b >= 0xf8) {
5282 		sub->temp_0[0] = p0 | 0x0f;
5283 		sub->temp_0[1] = b;
5284 		sub->temp_0[2] = 0;
5285 		sub->temp_0[3] = 0;
5286 		sub->temp_cmd = sub->temp_0;
5287 		return (1);
5288 
5289 	} else if (b >= 0xf0) {
5290 		switch (b) {
5291 		case 0xf0:		/* system exclusive begin */
5292 			sub->temp_1[1] = b;
5293 			sub->state = UMIDI_ST_SYSEX_1;
5294 			break;
5295 		case 0xf1:		/* MIDI time code */
5296 		case 0xf3:		/* song select */
5297 			sub->temp_1[1] = b;
5298 			sub->state = UMIDI_ST_1PARAM;
5299 			break;
5300 		case 0xf2:		/* song position pointer */
5301 			sub->temp_1[1] = b;
5302 			sub->state = UMIDI_ST_2PARAM_1;
5303 			break;
5304 		case 0xf4:		/* unknown */
5305 		case 0xf5:		/* unknown */
5306 			sub->state = UMIDI_ST_UNKNOWN;
5307 			break;
5308 		case 0xf6:		/* tune request */
5309 			sub->temp_1[0] = p0 | 0x05;
5310 			sub->temp_1[1] = 0xf6;
5311 			sub->temp_1[2] = 0;
5312 			sub->temp_1[3] = 0;
5313 			sub->temp_cmd = sub->temp_1;
5314 			sub->state = UMIDI_ST_UNKNOWN;
5315 			return (1);
5316 
5317 		case 0xf7:		/* system exclusive end */
5318 			switch (sub->state) {
5319 			case UMIDI_ST_SYSEX_0:
5320 				sub->temp_1[0] = p0 | 0x05;
5321 				sub->temp_1[1] = 0xf7;
5322 				sub->temp_1[2] = 0;
5323 				sub->temp_1[3] = 0;
5324 				sub->temp_cmd = sub->temp_1;
5325 				sub->state = UMIDI_ST_UNKNOWN;
5326 				return (1);
5327 			case UMIDI_ST_SYSEX_1:
5328 				sub->temp_1[0] = p0 | 0x06;
5329 				sub->temp_1[2] = 0xf7;
5330 				sub->temp_1[3] = 0;
5331 				sub->temp_cmd = sub->temp_1;
5332 				sub->state = UMIDI_ST_UNKNOWN;
5333 				return (1);
5334 			case UMIDI_ST_SYSEX_2:
5335 				sub->temp_1[0] = p0 | 0x07;
5336 				sub->temp_1[3] = 0xf7;
5337 				sub->temp_cmd = sub->temp_1;
5338 				sub->state = UMIDI_ST_UNKNOWN;
5339 				return (1);
5340 			}
5341 			sub->state = UMIDI_ST_UNKNOWN;
5342 			break;
5343 		}
5344 	} else if (b >= 0x80) {
5345 		sub->temp_1[1] = b;
5346 		if ((b >= 0xc0) && (b <= 0xdf)) {
5347 			sub->state = UMIDI_ST_1PARAM;
5348 		} else {
5349 			sub->state = UMIDI_ST_2PARAM_1;
5350 		}
5351 	} else {			/* b < 0x80 */
5352 		switch (sub->state) {
5353 		case UMIDI_ST_1PARAM:
5354 			if (sub->temp_1[1] < 0xf0) {
5355 				p0 |= sub->temp_1[1] >> 4;
5356 			} else {
5357 				p0 |= 0x02;
5358 				sub->state = UMIDI_ST_UNKNOWN;
5359 			}
5360 			sub->temp_1[0] = p0;
5361 			sub->temp_1[2] = b;
5362 			sub->temp_1[3] = 0;
5363 			sub->temp_cmd = sub->temp_1;
5364 			return (1);
5365 		case UMIDI_ST_2PARAM_1:
5366 			sub->temp_1[2] = b;
5367 			sub->state = UMIDI_ST_2PARAM_2;
5368 			break;
5369 		case UMIDI_ST_2PARAM_2:
5370 			if (sub->temp_1[1] < 0xf0) {
5371 				p0 |= sub->temp_1[1] >> 4;
5372 				sub->state = UMIDI_ST_2PARAM_1;
5373 			} else {
5374 				p0 |= 0x03;
5375 				sub->state = UMIDI_ST_UNKNOWN;
5376 			}
5377 			sub->temp_1[0] = p0;
5378 			sub->temp_1[3] = b;
5379 			sub->temp_cmd = sub->temp_1;
5380 			return (1);
5381 		case UMIDI_ST_SYSEX_0:
5382 			sub->temp_1[1] = b;
5383 			sub->state = UMIDI_ST_SYSEX_1;
5384 			break;
5385 		case UMIDI_ST_SYSEX_1:
5386 			sub->temp_1[2] = b;
5387 			sub->state = UMIDI_ST_SYSEX_2;
5388 			break;
5389 		case UMIDI_ST_SYSEX_2:
5390 			sub->temp_1[0] = p0 | 0x04;
5391 			sub->temp_1[3] = b;
5392 			sub->temp_cmd = sub->temp_1;
5393 			sub->state = UMIDI_ST_SYSEX_0;
5394 			return (1);
5395 		default:
5396 			break;
5397 		}
5398 	}
5399 	return (0);
5400 }
5401 
5402 static void
5403 umidi_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
5404 {
5405 	struct umidi_chan *chan = usbd_xfer_softc(xfer);
5406 	struct umidi_sub_chan *sub;
5407 	struct usb_page_cache *pc;
5408 	uint32_t actlen;
5409 	uint16_t nframes;
5410 	uint8_t buf;
5411 	uint8_t start_cable;
5412 	uint8_t tr_any;
5413 	int len;
5414 
5415 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
5416 
5417 	/*
5418 	 * NOTE: Some MIDI devices only accept 4 bytes of data per
5419 	 * short terminated USB transfer.
5420 	 */
5421 	switch (USB_GET_STATE(xfer)) {
5422 	case USB_ST_TRANSFERRED:
5423 		DPRINTF("actlen=%d bytes\n", len);
5424 
5425 	case USB_ST_SETUP:
5426 tr_setup:
5427 		DPRINTF("start\n");
5428 
5429 		nframes = 0;	/* reset */
5430 		start_cable = chan->curr_cable;
5431 		tr_any = 0;
5432 		pc = usbd_xfer_get_frame(xfer, 0);
5433 
5434 		while (1) {
5435 
5436 			/* round robin de-queueing */
5437 
5438 			sub = &chan->sub[chan->curr_cable];
5439 
5440 			if (sub->write_open) {
5441 				usb_fifo_get_data_linear(sub->fifo.fp[USB_FIFO_TX],
5442 				    &buf, 1, &actlen, 0);
5443 			} else {
5444 				actlen = 0;
5445 			}
5446 
5447 			if (actlen) {
5448 
5449 				tr_any = 1;
5450 
5451 				DPRINTF("byte=0x%02x from FIFO %u\n", buf,
5452 				    (unsigned int)chan->curr_cable);
5453 
5454 				if (umidi_convert_to_usb(sub, chan->curr_cable, buf)) {
5455 
5456 					DPRINTF("sub=0x%02x 0x%02x 0x%02x 0x%02x\n",
5457 					    sub->temp_cmd[0], sub->temp_cmd[1],
5458 					    sub->temp_cmd[2], sub->temp_cmd[3]);
5459 
5460 					usbd_copy_in(pc, nframes * 4, sub->temp_cmd, 4);
5461 
5462 					nframes++;
5463 
5464 					if ((nframes >= UMIDI_TX_FRAMES) || (chan->single_command != 0))
5465 						break;
5466 				} else {
5467 					continue;
5468 				}
5469 			}
5470 
5471 			chan->curr_cable++;
5472 			if (chan->curr_cable >= chan->max_cable)
5473 				chan->curr_cable = 0;
5474 
5475 			if (chan->curr_cable == start_cable) {
5476 				if (tr_any == 0)
5477 					break;
5478 				tr_any = 0;
5479 			}
5480 		}
5481 
5482 		if (nframes != 0) {
5483 			DPRINTF("Transferring %d frames\n", (int)nframes);
5484 			usbd_xfer_set_frame_len(xfer, 0, 4 * nframes);
5485 			usbd_transfer_submit(xfer);
5486 		}
5487 		break;
5488 
5489 	default:			/* Error */
5490 
5491 		DPRINTF("error=%s\n", usbd_errstr(error));
5492 
5493 		if (error != USB_ERR_CANCELLED) {
5494 			/* try to clear stall first */
5495 			usbd_xfer_set_stall(xfer);
5496 			goto tr_setup;
5497 		}
5498 		break;
5499 	}
5500 }
5501 
5502 static struct umidi_sub_chan *
5503 umidi_sub_by_fifo(struct usb_fifo *fifo)
5504 {
5505 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5506 	struct umidi_sub_chan *sub;
5507 	uint32_t n;
5508 
5509 	for (n = 0; n < UMIDI_CABLES_MAX; n++) {
5510 		sub = &chan->sub[n];
5511 		if ((sub->fifo.fp[USB_FIFO_RX] == fifo) ||
5512 		    (sub->fifo.fp[USB_FIFO_TX] == fifo)) {
5513 			return (sub);
5514 		}
5515 	}
5516 
5517 	panic("%s:%d cannot find usb_fifo!\n",
5518 	    __FILE__, __LINE__);
5519 
5520 	return (NULL);
5521 }
5522 
5523 static void
5524 umidi_start_read(struct usb_fifo *fifo)
5525 {
5526 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5527 
5528 	usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
5529 }
5530 
5531 static void
5532 umidi_stop_read(struct usb_fifo *fifo)
5533 {
5534 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5535 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5536 
5537 	DPRINTF("\n");
5538 
5539 	sub->read_open = 0;
5540 
5541 	if (--(chan->read_open_refcount) == 0) {
5542 		/*
5543 		 * XXX don't stop the read transfer here, hence that causes
5544 		 * problems with some MIDI adapters
5545 		 */
5546 		DPRINTF("(stopping read transfer)\n");
5547 	}
5548 }
5549 
5550 static void
5551 umidi_start_write(struct usb_fifo *fifo)
5552 {
5553 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5554 
5555 	usbd_transfer_start(chan->xfer[UMIDI_TX_TRANSFER]);
5556 }
5557 
5558 static void
5559 umidi_stop_write(struct usb_fifo *fifo)
5560 {
5561 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5562 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5563 
5564 	DPRINTF("\n");
5565 
5566 	sub->write_open = 0;
5567 
5568 	if (--(chan->write_open_refcount) == 0) {
5569 		DPRINTF("(stopping write transfer)\n");
5570 		usbd_transfer_stop(chan->xfer[UMIDI_TX_TRANSFER]);
5571 	}
5572 }
5573 
5574 static int
5575 umidi_open(struct usb_fifo *fifo, int fflags)
5576 {
5577 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5578 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5579 
5580 	if (fflags & FREAD) {
5581 		if (usb_fifo_alloc_buffer(fifo, 4, (1024 / 4))) {
5582 			return (ENOMEM);
5583 		}
5584 		mtx_lock(&chan->mtx);
5585 		chan->read_open_refcount++;
5586 		sub->read_open = 1;
5587 		mtx_unlock(&chan->mtx);
5588 	}
5589 	if (fflags & FWRITE) {
5590 		if (usb_fifo_alloc_buffer(fifo, 32, (1024 / 32))) {
5591 			return (ENOMEM);
5592 		}
5593 		/* clear stall first */
5594 		mtx_lock(&chan->mtx);
5595 		chan->write_open_refcount++;
5596 		sub->write_open = 1;
5597 
5598 		/* reset */
5599 		sub->state = UMIDI_ST_UNKNOWN;
5600 		mtx_unlock(&chan->mtx);
5601 	}
5602 	return (0);			/* success */
5603 }
5604 
5605 static void
5606 umidi_close(struct usb_fifo *fifo, int fflags)
5607 {
5608 	if (fflags & FREAD) {
5609 		usb_fifo_free_buffer(fifo);
5610 	}
5611 	if (fflags & FWRITE) {
5612 		usb_fifo_free_buffer(fifo);
5613 	}
5614 }
5615 
5616 
5617 static int
5618 umidi_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
5619     int fflags)
5620 {
5621 	return (ENODEV);
5622 }
5623 
5624 static void
5625 umidi_init(device_t dev)
5626 {
5627 	struct uaudio_softc *sc = device_get_softc(dev);
5628 	struct umidi_chan *chan = &sc->sc_midi_chan;
5629 
5630 	mtx_init(&chan->mtx, "umidi lock", NULL, MTX_DEF | MTX_RECURSE);
5631 }
5632 
5633 static struct usb_fifo_methods umidi_fifo_methods = {
5634 	.f_start_read = &umidi_start_read,
5635 	.f_start_write = &umidi_start_write,
5636 	.f_stop_read = &umidi_stop_read,
5637 	.f_stop_write = &umidi_stop_write,
5638 	.f_open = &umidi_open,
5639 	.f_close = &umidi_close,
5640 	.f_ioctl = &umidi_ioctl,
5641 	.basename[0] = "umidi",
5642 };
5643 
5644 static int
5645 umidi_probe(device_t dev)
5646 {
5647 	struct uaudio_softc *sc = device_get_softc(dev);
5648 	struct usb_attach_arg *uaa = device_get_ivars(dev);
5649 	struct umidi_chan *chan = &sc->sc_midi_chan;
5650 	struct umidi_sub_chan *sub;
5651 	int unit = device_get_unit(dev);
5652 	int error;
5653 	uint32_t n;
5654 
5655 	if (usb_test_quirk(uaa, UQ_SINGLE_CMD_MIDI))
5656 		chan->single_command = 1;
5657 
5658 	if (usbd_set_alt_interface_index(sc->sc_udev, chan->iface_index,
5659 	    chan->iface_alt_index)) {
5660 		DPRINTF("setting of alternate index failed!\n");
5661 		goto detach;
5662 	}
5663 	usbd_set_parent_iface(sc->sc_udev, chan->iface_index,
5664 	    sc->sc_mixer_iface_index);
5665 
5666 	error = usbd_transfer_setup(uaa->device, &chan->iface_index,
5667 	    chan->xfer, umidi_config, UMIDI_N_TRANSFER,
5668 	    chan, &chan->mtx);
5669 	if (error) {
5670 		DPRINTF("error=%s\n", usbd_errstr(error));
5671 		goto detach;
5672 	}
5673 	if ((chan->max_cable > UMIDI_CABLES_MAX) ||
5674 	    (chan->max_cable == 0)) {
5675 		chan->max_cable = UMIDI_CABLES_MAX;
5676 	}
5677 
5678 	for (n = 0; n < chan->max_cable; n++) {
5679 
5680 		sub = &chan->sub[n];
5681 
5682 		error = usb_fifo_attach(sc->sc_udev, chan, &chan->mtx,
5683 		    &umidi_fifo_methods, &sub->fifo, unit, n,
5684 		    chan->iface_index,
5685 		    UID_ROOT, GID_OPERATOR, 0644);
5686 		if (error) {
5687 			goto detach;
5688 		}
5689 	}
5690 
5691 	mtx_lock(&chan->mtx);
5692 
5693 	/*
5694 	 * NOTE: At least one device will not work properly unless the
5695 	 * BULK IN pipe is open all the time. This might have to do
5696 	 * about that the internal queues of the device overflow if we
5697 	 * don't read them regularly.
5698 	 */
5699 	usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
5700 
5701 	mtx_unlock(&chan->mtx);
5702 
5703 	return (0);			/* success */
5704 
5705 detach:
5706 	return (ENXIO);			/* failure */
5707 }
5708 
5709 static int
5710 umidi_detach(device_t dev)
5711 {
5712 	struct uaudio_softc *sc = device_get_softc(dev);
5713 	struct umidi_chan *chan = &sc->sc_midi_chan;
5714 	uint32_t n;
5715 
5716 	for (n = 0; n < UMIDI_CABLES_MAX; n++) {
5717 		usb_fifo_detach(&chan->sub[n].fifo);
5718 	}
5719 
5720 	mtx_lock(&chan->mtx);
5721 
5722 	usbd_transfer_stop(chan->xfer[UMIDI_RX_TRANSFER]);
5723 
5724 	mtx_unlock(&chan->mtx);
5725 
5726 	usbd_transfer_unsetup(chan->xfer, UMIDI_N_TRANSFER);
5727 
5728 	mtx_destroy(&chan->mtx);
5729 
5730 	return (0);
5731 }
5732 
5733 static void
5734 uaudio_hid_rx_callback(struct usb_xfer *xfer, usb_error_t error)
5735 {
5736 	struct uaudio_softc *sc = usbd_xfer_softc(xfer);
5737 	const uint8_t *buffer = usbd_xfer_get_frame_buffer(xfer, 0);
5738 	struct snd_mixer *m;
5739 	uint8_t id;
5740 	int actlen;
5741 
5742 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
5743 
5744 	switch (USB_GET_STATE(xfer)) {
5745 	case USB_ST_TRANSFERRED:
5746 		DPRINTF("actlen=%d\n", actlen);
5747 
5748 		if (actlen != 0 &&
5749 		    (sc->sc_hid.flags & UAUDIO_HID_HAS_ID)) {
5750 			id = *buffer;
5751 			buffer++;
5752 			actlen--;
5753 		} else {
5754 			id = 0;
5755 		}
5756 
5757 		m = sc->sc_mixer_dev;
5758 
5759 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_MUTE) &&
5760 		    (sc->sc_hid.mute_id == id) &&
5761 		    hid_get_data(buffer, actlen,
5762 		    &sc->sc_hid.mute_loc)) {
5763 
5764 			DPRINTF("Mute toggle\n");
5765 
5766 			mixer_hwvol_mute_locked(m);
5767 		}
5768 
5769 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_VOLUME_UP) &&
5770 		    (sc->sc_hid.volume_up_id == id) &&
5771 		    hid_get_data(buffer, actlen,
5772 		    &sc->sc_hid.volume_up_loc)) {
5773 
5774 			DPRINTF("Volume Up\n");
5775 
5776 			mixer_hwvol_step_locked(m, 1, 1);
5777 		}
5778 
5779 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_VOLUME_DOWN) &&
5780 		    (sc->sc_hid.volume_down_id == id) &&
5781 		    hid_get_data(buffer, actlen,
5782 		    &sc->sc_hid.volume_down_loc)) {
5783 
5784 			DPRINTF("Volume Down\n");
5785 
5786 			mixer_hwvol_step_locked(m, -1, -1);
5787 		}
5788 
5789 	case USB_ST_SETUP:
5790 tr_setup:
5791 		/* check if we can put more data into the FIFO */
5792 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
5793 		usbd_transfer_submit(xfer);
5794 		break;
5795 
5796 	default:			/* Error */
5797 
5798 		DPRINTF("error=%s\n", usbd_errstr(error));
5799 
5800 		if (error != USB_ERR_CANCELLED) {
5801 			/* try to clear stall first */
5802 			usbd_xfer_set_stall(xfer);
5803 			goto tr_setup;
5804 		}
5805 		break;
5806 	}
5807 }
5808 
5809 static int
5810 uaudio_hid_probe(struct uaudio_softc *sc,
5811     struct usb_attach_arg *uaa)
5812 {
5813 	void *d_ptr;
5814 	uint32_t flags;
5815 	uint16_t d_len;
5816 	uint8_t id;
5817 	int error;
5818 
5819 	if (!(sc->sc_hid.flags & UAUDIO_HID_VALID))
5820 		return (-1);
5821 
5822 	if (sc->sc_mixer_lock == NULL)
5823 		return (-1);
5824 
5825 	/* Get HID descriptor */
5826 	error = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
5827 	    &d_len, M_TEMP, sc->sc_hid.iface_index);
5828 
5829 	if (error) {
5830 		DPRINTF("error reading report description\n");
5831 		return (-1);
5832 	}
5833 
5834 	/* check if there is an ID byte */
5835 	hid_report_size(d_ptr, d_len, hid_input, &id);
5836 
5837 	if (id != 0)
5838 		sc->sc_hid.flags |= UAUDIO_HID_HAS_ID;
5839 
5840 	if (hid_locate(d_ptr, d_len,
5841 	    HID_USAGE2(HUP_CONSUMER, 0xE9 /* Volume Increment */),
5842 	    hid_input, 0, &sc->sc_hid.volume_up_loc, &flags,
5843 	    &sc->sc_hid.volume_up_id)) {
5844 		if (flags & HIO_VARIABLE)
5845 			sc->sc_hid.flags |= UAUDIO_HID_HAS_VOLUME_UP;
5846 		DPRINTFN(1, "Found Volume Up key\n");
5847 	}
5848 
5849 	if (hid_locate(d_ptr, d_len,
5850 	    HID_USAGE2(HUP_CONSUMER, 0xEA /* Volume Decrement */),
5851 	    hid_input, 0, &sc->sc_hid.volume_down_loc, &flags,
5852 	    &sc->sc_hid.volume_down_id)) {
5853 		if (flags & HIO_VARIABLE)
5854 			sc->sc_hid.flags |= UAUDIO_HID_HAS_VOLUME_DOWN;
5855 		DPRINTFN(1, "Found Volume Down key\n");
5856 	}
5857 
5858 	if (hid_locate(d_ptr, d_len,
5859 	    HID_USAGE2(HUP_CONSUMER, 0xE2 /* Mute */),
5860 	    hid_input, 0, &sc->sc_hid.mute_loc, &flags,
5861 	    &sc->sc_hid.mute_id)) {
5862 		if (flags & HIO_VARIABLE)
5863 			sc->sc_hid.flags |= UAUDIO_HID_HAS_MUTE;
5864 		DPRINTFN(1, "Found Mute key\n");
5865 	}
5866 
5867 	free(d_ptr, M_TEMP);
5868 
5869 	if (!(sc->sc_hid.flags & (UAUDIO_HID_HAS_VOLUME_UP |
5870 	    UAUDIO_HID_HAS_VOLUME_DOWN |
5871 	    UAUDIO_HID_HAS_MUTE))) {
5872 		DPRINTFN(1, "Did not find any volume related keys\n");
5873 		return (-1);
5874 	}
5875 
5876 	/* prevent the uhid driver from attaching */
5877 	usbd_set_parent_iface(uaa->device, sc->sc_hid.iface_index,
5878 	    sc->sc_mixer_iface_index);
5879 
5880 	/* allocate USB transfers */
5881 	error = usbd_transfer_setup(uaa->device, &sc->sc_hid.iface_index,
5882 	    sc->sc_hid.xfer, uaudio_hid_config, UAUDIO_HID_N_TRANSFER,
5883 	    sc, sc->sc_mixer_lock);
5884 	if (error) {
5885 		DPRINTF("error=%s\n", usbd_errstr(error));
5886 		return (-1);
5887 	}
5888 	return (0);
5889 }
5890 
5891 static void
5892 uaudio_hid_detach(struct uaudio_softc *sc)
5893 {
5894 	usbd_transfer_unsetup(sc->sc_hid.xfer, UAUDIO_HID_N_TRANSFER);
5895 }
5896 
5897 DRIVER_MODULE(uaudio, uhub, uaudio_driver, uaudio_devclass, NULL, 0);
5898 MODULE_DEPEND(uaudio, usb, 1, 1, 1);
5899 MODULE_DEPEND(uaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
5900 MODULE_VERSION(uaudio, 1);
5901