xref: /freebsd/sys/dev/sound/usb/uaudio.c (revision 864c53ead899f7838cd2e1cca3b485a4a82f5cdc)
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 24
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 	384000,
1887 	352800,
1888 	192000,
1889 	176400,
1890 	96000,
1891 	88200,
1892 	88000,
1893 	80000,
1894 	72000,
1895 	64000,
1896 	56000,
1897 	48000,
1898 	44100,
1899 	40000,
1900 	32000,
1901 	24000,
1902 	22050,
1903 	16000,
1904 	11025,
1905 	8000,
1906 	0
1907 };
1908 
1909 static void
1910 uaudio_chan_fill_info(struct uaudio_softc *sc, struct usb_device *udev)
1911 {
1912 	uint32_t rate = uaudio_default_rate;
1913 	uint8_t z;
1914 	uint8_t bits = uaudio_default_bits;
1915 	uint8_t y;
1916 	uint8_t channels = uaudio_default_channels;
1917 	uint8_t x;
1918 
1919 	bits -= (bits % 8);
1920 	if ((bits == 0) || (bits > 32)) {
1921 		/* set a valid value */
1922 		bits = 32;
1923 	}
1924 	if (channels == 0) {
1925 		switch (usbd_get_speed(udev)) {
1926 		case USB_SPEED_LOW:
1927 		case USB_SPEED_FULL:
1928 			/*
1929 			 * Due to high bandwidth usage and problems
1930 			 * with HIGH-speed split transactions we
1931 			 * disable surround setups on FULL-speed USB
1932 			 * by default
1933 			 */
1934 			channels = 4;
1935 			break;
1936 		default:
1937 			channels = 16;
1938 			break;
1939 		}
1940 	} else if (channels > 16) {
1941 		channels = 16;
1942 	}
1943 	if (sbuf_new(&sc->sc_sndstat, NULL, 4096, SBUF_AUTOEXTEND)) {
1944 		sc->sc_sndstat_valid = 1;
1945 	}
1946 	/* try to search for a valid config */
1947 
1948 	for (x = channels; x; x--) {
1949 		for (y = bits; y; y -= 8) {
1950 
1951 			/* try user defined rate, if any */
1952 			if (rate != 0)
1953 				uaudio_chan_fill_info_sub(sc, udev, rate, x, y);
1954 
1955 			/* try find a matching rate, if any */
1956 			for (z = 0; uaudio_rate_list[z]; z++)
1957 				uaudio_chan_fill_info_sub(sc, udev, uaudio_rate_list[z], x, y);
1958 		}
1959 	}
1960 	if (sc->sc_sndstat_valid)
1961 		sbuf_finish(&sc->sc_sndstat);
1962 }
1963 
1964 static void
1965 uaudio_chan_play_sync_callback(struct usb_xfer *xfer, usb_error_t error)
1966 {
1967 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
1968 	struct usb_page_cache *pc;
1969 	uint64_t sample_rate = ch->usb_alt[ch->cur_alt].sample_rate;
1970 	uint8_t buf[4];
1971 	uint64_t temp;
1972 	int len;
1973 	int actlen;
1974 	int nframes;
1975 
1976 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
1977 
1978 	switch (USB_GET_STATE(xfer)) {
1979 	case USB_ST_TRANSFERRED:
1980 
1981 		DPRINTFN(6, "transferred %d bytes\n", actlen);
1982 
1983 		if (nframes == 0)
1984 			break;
1985 		len = usbd_xfer_frame_len(xfer, 0);
1986 		if (len == 0)
1987 			break;
1988 		if (len > sizeof(buf))
1989 			len = sizeof(buf);
1990 
1991 		memset(buf, 0, sizeof(buf));
1992 
1993 		pc = usbd_xfer_get_frame(xfer, 0);
1994 		usbd_copy_out(pc, 0, buf, len);
1995 
1996 		temp = UGETDW(buf);
1997 
1998 		DPRINTF("Value = 0x%08x\n", (int)temp);
1999 
2000 		/* auto-detect SYNC format */
2001 
2002 		if (len == 4)
2003 			temp &= 0x0fffffff;
2004 
2005 		/* check for no data */
2006 
2007 		if (temp == 0)
2008 			break;
2009 
2010 		/* correctly scale value */
2011 
2012 		temp = (temp * 125ULL) - 64;
2013 
2014 		/* auto adjust */
2015 
2016 		while (temp < (sample_rate - (sample_rate / 4)))
2017 			temp *= 2;
2018 
2019 		while (temp > (sample_rate + (sample_rate / 2)))
2020 			temp /= 2;
2021 
2022 		/* compare */
2023 
2024 		DPRINTF("Comparing %d < %d\n",
2025 		    (int)temp, (int)sample_rate);
2026 
2027 		if (temp == sample_rate)
2028 			ch->last_sync_state = UAUDIO_SYNC_NONE;
2029 		else if (temp > sample_rate)
2030 			ch->last_sync_state = UAUDIO_SYNC_MORE;
2031 		else
2032 			ch->last_sync_state = UAUDIO_SYNC_LESS;
2033 		break;
2034 
2035 	case USB_ST_SETUP:
2036 		usbd_xfer_set_frames(xfer, 1);
2037 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_framelen(xfer));
2038 		usbd_transfer_submit(xfer);
2039 		break;
2040 
2041 	default:			/* Error */
2042 		break;
2043 	}
2044 }
2045 
2046 static void
2047 uaudio_chan_play_callback(struct usb_xfer *xfer, usb_error_t error)
2048 {
2049 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
2050 	struct usb_page_cache *pc;
2051 	uint32_t sample_size = ch->usb_alt[ch->cur_alt].sample_size;
2052 	uint32_t mfl;
2053 	uint32_t total;
2054 	uint32_t blockcount;
2055 	uint32_t n;
2056 	uint32_t offset;
2057 	int actlen;
2058 	int sumlen;
2059 
2060 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2061 
2062 	if (ch->end == ch->start) {
2063 		DPRINTF("no buffer!\n");
2064 		return;
2065 	}
2066 
2067 	switch (USB_GET_STATE(xfer)) {
2068 	case USB_ST_TRANSFERRED:
2069 tr_transferred:
2070 		if (actlen < sumlen) {
2071 			DPRINTF("short transfer, "
2072 			    "%d of %d bytes\n", actlen, sumlen);
2073 		}
2074 		chn_intr(ch->pcm_ch);
2075 
2076 		/* start SYNC transfer, if any */
2077 		if ((ch->last_sync_time++ & 7) == 0)
2078 			usbd_transfer_start(ch->xfer[UAUDIO_NCHANBUFS]);
2079 
2080 	case USB_ST_SETUP:
2081 		mfl = usbd_xfer_max_framelen(xfer);
2082 
2083 		if (ch->bytes_per_frame[1] > mfl) {
2084 			DPRINTF("bytes per transfer, %d, "
2085 			    "exceeds maximum, %d!\n",
2086 			    ch->bytes_per_frame[1],
2087 			    mfl);
2088 			break;
2089 		}
2090 
2091 		blockcount = ch->intr_frames;
2092 
2093 		/* setup number of frames */
2094 		usbd_xfer_set_frames(xfer, blockcount);
2095 
2096 		/* reset total length */
2097 		total = 0;
2098 
2099 		/* setup frame lengths */
2100 		for (n = 0; n != blockcount; n++) {
2101 			uint32_t frame_len;
2102 
2103 			ch->sample_curr += ch->sample_rem;
2104 			if (ch->sample_curr >= ch->frames_per_second) {
2105 				ch->sample_curr -= ch->frames_per_second;
2106 				frame_len = ch->bytes_per_frame[1];
2107 			} else {
2108 				frame_len = ch->bytes_per_frame[0];
2109 			}
2110 
2111 			if (n == (blockcount - 1)) {
2112 				switch (ch->last_sync_state) {
2113 				case UAUDIO_SYNC_MORE:
2114 					DPRINTFN(6, "sending one sample more\n");
2115 					if ((frame_len + sample_size) <= mfl)
2116 						frame_len += sample_size;
2117 					ch->last_sync_state = UAUDIO_SYNC_NONE;
2118 					break;
2119 				case UAUDIO_SYNC_LESS:
2120 					DPRINTFN(6, "sending one sample less\n");
2121 					if (frame_len >= sample_size)
2122 						frame_len -= sample_size;
2123 					ch->last_sync_state = UAUDIO_SYNC_NONE;
2124 					break;
2125 				default:
2126 					break;
2127 				}
2128 			}
2129 
2130 			usbd_xfer_set_frame_len(xfer, n, frame_len);
2131 			total += frame_len;
2132 		}
2133 
2134 		DPRINTFN(6, "transfer %d bytes\n", total);
2135 
2136 		offset = 0;
2137 
2138 		pc = usbd_xfer_get_frame(xfer, 0);
2139 		while (total > 0) {
2140 
2141 			n = (ch->end - ch->cur);
2142 			if (n > total) {
2143 				n = total;
2144 			}
2145 			usbd_copy_in(pc, offset, ch->cur, n);
2146 
2147 			total -= n;
2148 			ch->cur += n;
2149 			offset += n;
2150 
2151 			if (ch->cur >= ch->end) {
2152 				ch->cur = ch->start;
2153 			}
2154 		}
2155 
2156 		usbd_transfer_submit(xfer);
2157 		break;
2158 
2159 	default:			/* Error */
2160 		if (error == USB_ERR_CANCELLED) {
2161 			break;
2162 		}
2163 		goto tr_transferred;
2164 	}
2165 }
2166 
2167 static void
2168 uaudio_chan_record_sync_callback(struct usb_xfer *xfer, usb_error_t error)
2169 {
2170 	/* TODO */
2171 }
2172 
2173 static void
2174 uaudio_chan_record_callback(struct usb_xfer *xfer, usb_error_t error)
2175 {
2176 	struct uaudio_chan *ch = usbd_xfer_softc(xfer);
2177 	struct usb_page_cache *pc;
2178 	uint32_t offset0;
2179 	uint32_t offset1;
2180 	uint32_t mfl;
2181 	int m;
2182 	int n;
2183 	int len;
2184 	int actlen;
2185 	int nframes;
2186 	int blockcount;
2187 
2188 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
2189 	mfl = usbd_xfer_max_framelen(xfer);
2190 
2191 	if (ch->end == ch->start) {
2192 		DPRINTF("no buffer!\n");
2193 		return;
2194 	}
2195 
2196 	switch (USB_GET_STATE(xfer)) {
2197 	case USB_ST_TRANSFERRED:
2198 
2199 		DPRINTFN(6, "transferred %d bytes\n", actlen);
2200 
2201 		offset0 = 0;
2202 		pc = usbd_xfer_get_frame(xfer, 0);
2203 
2204 		for (n = 0; n != nframes; n++) {
2205 
2206 			offset1 = offset0;
2207 			len = usbd_xfer_frame_len(xfer, n);
2208 
2209 			while (len > 0) {
2210 
2211 				m = (ch->end - ch->cur);
2212 
2213 				if (m > len)
2214 					m = len;
2215 
2216 				usbd_copy_out(pc, offset1, ch->cur, m);
2217 
2218 				len -= m;
2219 				offset1 += m;
2220 				ch->cur += m;
2221 
2222 				if (ch->cur >= ch->end) {
2223 					ch->cur = ch->start;
2224 				}
2225 			}
2226 
2227 			offset0 += mfl;
2228 		}
2229 
2230 		chn_intr(ch->pcm_ch);
2231 
2232 	case USB_ST_SETUP:
2233 tr_setup:
2234 		blockcount = ch->intr_frames;
2235 
2236 		usbd_xfer_set_frames(xfer, blockcount);
2237 		for (n = 0; n < blockcount; n++) {
2238 			usbd_xfer_set_frame_len(xfer, n, mfl);
2239 		}
2240 
2241 		usbd_transfer_submit(xfer);
2242 		break;
2243 
2244 	default:			/* Error */
2245 		if (error == USB_ERR_CANCELLED) {
2246 			break;
2247 		}
2248 		goto tr_setup;
2249 	}
2250 }
2251 
2252 void   *
2253 uaudio_chan_init(struct uaudio_softc *sc, struct snd_dbuf *b,
2254     struct pcm_channel *c, int dir)
2255 {
2256 	struct uaudio_chan *ch = ((dir == PCMDIR_PLAY) ?
2257 	    &sc->sc_play_chan : &sc->sc_rec_chan);
2258 	uint32_t buf_size;
2259 	uint8_t x;
2260 
2261 	/* store mutex and PCM channel */
2262 
2263 	ch->pcm_ch = c;
2264 	ch->pcm_mtx = c->lock;
2265 
2266 	/* compute worst case buffer */
2267 
2268 	buf_size = 0;
2269 	for (x = 0; x != ch->num_alt; x++) {
2270 		uint32_t temp = uaudio_get_buffer_size(ch, x);
2271 		if (temp > buf_size)
2272 			buf_size = temp;
2273 	}
2274 
2275 	/* allow double buffering */
2276 	buf_size *= 2;
2277 
2278 	DPRINTF("Worst case buffer is %d bytes\n", (int)buf_size);
2279 
2280 	ch->buf = malloc(buf_size, M_DEVBUF, M_WAITOK | M_ZERO);
2281 	if (ch->buf == NULL)
2282 		goto error;
2283 	if (sndbuf_setup(b, ch->buf, buf_size) != 0)
2284 		goto error;
2285 
2286 	ch->start = ch->buf;
2287 	ch->end = ch->buf + buf_size;
2288 	ch->cur = ch->buf;
2289 	ch->pcm_buf = b;
2290 	ch->max_buf = buf_size;
2291 
2292 	if (ch->pcm_mtx == NULL) {
2293 		DPRINTF("ERROR: PCM channels does not have a mutex!\n");
2294 		goto error;
2295 	}
2296 	return (ch);
2297 
2298 error:
2299 	uaudio_chan_free(ch);
2300 	return (NULL);
2301 }
2302 
2303 int
2304 uaudio_chan_free(struct uaudio_chan *ch)
2305 {
2306 	if (ch->buf != NULL) {
2307 		free(ch->buf, M_DEVBUF);
2308 		ch->buf = NULL;
2309 	}
2310 	usbd_transfer_unsetup(ch->xfer, UAUDIO_NCHANBUFS + 1);
2311 
2312 	ch->num_alt = 0;
2313 
2314 	return (0);
2315 }
2316 
2317 int
2318 uaudio_chan_set_param_blocksize(struct uaudio_chan *ch, uint32_t blocksize)
2319 {
2320 	uint32_t temp = 2 * uaudio_get_buffer_size(ch, ch->set_alt);
2321 
2322 	sndbuf_setup(ch->pcm_buf, ch->buf, temp);
2323 
2324 	ch->start = ch->buf;
2325 	ch->end = ch->buf + temp;
2326 	ch->cur = ch->buf;
2327 
2328 	return (temp / 2);
2329 }
2330 
2331 int
2332 uaudio_chan_set_param_fragments(struct uaudio_chan *ch, uint32_t blocksize,
2333     uint32_t blockcount)
2334 {
2335 	return (1);
2336 }
2337 
2338 int
2339 uaudio_chan_set_param_speed(struct uaudio_chan *ch, uint32_t speed)
2340 {
2341 	uint8_t x;
2342 
2343 	for (x = 0; x < ch->num_alt; x++) {
2344 		if (ch->usb_alt[x].sample_rate < speed) {
2345 			/* sample rate is too low */
2346 			break;
2347 		}
2348 	}
2349 
2350 	if (x != 0)
2351 		x--;
2352 
2353 	ch->set_alt = x;
2354 
2355 	DPRINTF("Selecting alt %d\n", (int)x);
2356 
2357 	return (ch->usb_alt[x].sample_rate);
2358 }
2359 
2360 int
2361 uaudio_chan_getptr(struct uaudio_chan *ch)
2362 {
2363 	return (ch->cur - ch->start);
2364 }
2365 
2366 struct pcmchan_caps *
2367 uaudio_chan_getcaps(struct uaudio_chan *ch)
2368 {
2369 	return (&ch->pcm_cap);
2370 }
2371 
2372 static struct pcmchan_matrix uaudio_chan_matrix_swap_2_0 = {
2373 	.id = SND_CHN_MATRIX_DRV,
2374 	.channels = 2,
2375 	.ext = 0,
2376 	.map = {
2377 		/* Right */
2378 		[0] = {
2379 			.type = SND_CHN_T_FR,
2380 			.members =
2381 			    SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FC |
2382 			    SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BR |
2383 			    SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SR
2384 		},
2385 		/* Left */
2386 		[1] = {
2387 			.type = SND_CHN_T_FL,
2388 			.members =
2389 			    SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FC |
2390 			    SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BL |
2391 			    SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SL
2392 		},
2393 		[2] = {
2394 			.type = SND_CHN_T_MAX,
2395 			.members = 0
2396 		}
2397 	},
2398 	.mask = SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FL,
2399 	.offset = {  1,  0, -1, -1, -1, -1, -1, -1, -1,
2400 		    -1, -1, -1, -1, -1, -1, -1, -1, -1  }
2401 };
2402 
2403 struct pcmchan_matrix *
2404 uaudio_chan_getmatrix(struct uaudio_chan *ch, uint32_t format)
2405 {
2406 	struct uaudio_softc *sc;
2407 
2408 	sc = ch->priv_sc;
2409 
2410 	if (sc != NULL && sc->sc_uq_audio_swap_lr != 0 &&
2411 	    AFMT_CHANNEL(format) == 2)
2412 		return (&uaudio_chan_matrix_swap_2_0);
2413 
2414 	return (feeder_matrix_format_map(format));
2415 }
2416 
2417 int
2418 uaudio_chan_set_param_format(struct uaudio_chan *ch, uint32_t format)
2419 {
2420 	DPRINTF("Selecting format 0x%08x\n", (unsigned int)format);
2421 	return (0);
2422 }
2423 
2424 int
2425 uaudio_chan_start(struct uaudio_chan *ch)
2426 {
2427 	struct uaudio_softc *sc = ch->priv_sc;
2428 	int do_start = 0;
2429 
2430 	usb_proc_explore_lock(sc->sc_udev);
2431 	if (ch->operation != CHAN_OP_DRAIN) {
2432 		if (ch->cur_alt == ch->set_alt &&
2433 		    ch->operation == CHAN_OP_NONE) {
2434 			/* save doing the explore task */
2435 			do_start = 1;
2436 		} else {
2437 			ch->operation = CHAN_OP_START;
2438 			(void)usb_proc_explore_msignal(sc->sc_udev,
2439 			    &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2440 		}
2441 	}
2442 	usb_proc_explore_unlock(sc->sc_udev);
2443 
2444 	if (do_start) {
2445 		usbd_transfer_start(ch->xfer[0]);
2446 		usbd_transfer_start(ch->xfer[1]);
2447 	}
2448 	return (0);
2449 }
2450 
2451 int
2452 uaudio_chan_stop(struct uaudio_chan *ch)
2453 {
2454 	struct uaudio_softc *sc = ch->priv_sc;
2455 	int do_stop = 0;
2456 
2457 	usb_proc_explore_lock(sc->sc_udev);
2458 	if (ch->operation != CHAN_OP_DRAIN) {
2459 		if (ch->cur_alt == ch->set_alt &&
2460 		    ch->operation == CHAN_OP_NONE) {
2461 			/* save doing the explore task */
2462 			do_stop = 1;
2463 		} else {
2464 			ch->operation = CHAN_OP_STOP;
2465 			(void)usb_proc_explore_msignal(sc->sc_udev,
2466 			    &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2467 		}
2468 	}
2469 	usb_proc_explore_unlock(sc->sc_udev);
2470 
2471 	if (do_stop) {
2472 		usbd_transfer_stop(ch->xfer[0]);
2473 		usbd_transfer_stop(ch->xfer[1]);
2474 	}
2475 	return (0);
2476 }
2477 
2478 /*========================================================================*
2479  * AC - Audio Controller - routines
2480  *========================================================================*/
2481 
2482 static int
2483 uaudio_mixer_sysctl_handler(SYSCTL_HANDLER_ARGS)
2484 {
2485 	struct uaudio_softc *sc;
2486 	struct uaudio_mixer_node *pmc;
2487 	int hint;
2488 	int error;
2489 	int temp = 0;
2490 	int chan = 0;
2491 
2492 	sc = (struct uaudio_softc *)oidp->oid_arg1;
2493 	hint = oidp->oid_arg2;
2494 
2495 	if (sc->sc_mixer_lock == NULL)
2496 		return (ENXIO);
2497 
2498 	/* lookup mixer node */
2499 
2500 	mtx_lock(sc->sc_mixer_lock);
2501 	for (pmc = sc->sc_mixer_root; pmc != NULL; pmc = pmc->next) {
2502 		for (chan = 0; chan != (int)pmc->nchan; chan++) {
2503 			if (pmc->wValue[chan] != -1 &&
2504 			    pmc->wValue[chan] == hint) {
2505 				temp = pmc->wData[chan];
2506 				goto found;
2507 			}
2508 		}
2509 	}
2510 found:
2511 	mtx_unlock(sc->sc_mixer_lock);
2512 
2513 	error = sysctl_handle_int(oidp, &temp, 0, req);
2514 	if (error != 0 || req->newptr == NULL)
2515 		return (error);
2516 
2517 	/* update mixer value */
2518 
2519 	mtx_lock(sc->sc_mixer_lock);
2520 	if (pmc != NULL &&
2521 	    temp >= pmc->minval &&
2522 	    temp <= pmc->maxval) {
2523 
2524 		pmc->wData[chan] = temp;
2525 		pmc->update[(chan / 8)] |= (1 << (chan % 8));
2526 
2527 		/* start the transfer, if not already started */
2528 		usbd_transfer_start(sc->sc_mixer_xfer[0]);
2529 	}
2530 	mtx_unlock(sc->sc_mixer_lock);
2531 
2532 	return (0);
2533 }
2534 
2535 static void
2536 uaudio_mixer_ctl_free(struct uaudio_softc *sc)
2537 {
2538 	struct uaudio_mixer_node *p_mc;
2539 
2540 	while ((p_mc = sc->sc_mixer_root) != NULL) {
2541 		sc->sc_mixer_root = p_mc->next;
2542 		free(p_mc, M_USBDEV);
2543 	}
2544 }
2545 
2546 static void
2547 uaudio_mixer_register_sysctl(struct uaudio_softc *sc, device_t dev)
2548 {
2549 	struct uaudio_mixer_node *pmc;
2550 	struct sysctl_oid *mixer_tree;
2551 	struct sysctl_oid *control_tree;
2552 	char buf[32];
2553 	int chan;
2554 	int n;
2555 
2556 	mixer_tree = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
2557 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "mixer",
2558 	    CTLFLAG_RD, NULL, "");
2559 
2560 	if (mixer_tree == NULL)
2561 		return;
2562 
2563 	for (n = 0, pmc = sc->sc_mixer_root; pmc != NULL;
2564 	    pmc = pmc->next, n++) {
2565 
2566 		for (chan = 0; chan < pmc->nchan; chan++) {
2567 
2568 			if (pmc->nchan > 1) {
2569 				snprintf(buf, sizeof(buf), "%s_%d_%d",
2570 				    pmc->name, n, chan);
2571 			} else {
2572 				snprintf(buf, sizeof(buf), "%s_%d",
2573 				    pmc->name, n);
2574 			}
2575 
2576 			control_tree = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
2577 			    SYSCTL_CHILDREN(mixer_tree), OID_AUTO, buf,
2578 			    CTLFLAG_RD, NULL, "Mixer control nodes");
2579 
2580 			if (control_tree == NULL)
2581 				continue;
2582 
2583 			SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
2584 			    SYSCTL_CHILDREN(control_tree),
2585 			    OID_AUTO, "val", CTLTYPE_INT | CTLFLAG_RW, sc,
2586 			    pmc->wValue[chan],
2587 			    uaudio_mixer_sysctl_handler, "I", "Current value");
2588 
2589 			SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
2590 			    SYSCTL_CHILDREN(control_tree),
2591 			    OID_AUTO, "min", CTLFLAG_RD, 0, pmc->minval,
2592 			    "Minimum value");
2593 
2594 			SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
2595 			    SYSCTL_CHILDREN(control_tree),
2596 			    OID_AUTO, "max", CTLFLAG_RD, 0, pmc->maxval,
2597 			    "Maximum value");
2598 
2599 			SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
2600 			    SYSCTL_CHILDREN(control_tree),
2601 			    OID_AUTO, "desc", CTLFLAG_RD, pmc->desc, 0,
2602 			    "Description");
2603 		}
2604 	}
2605 }
2606 
2607 /* M-Audio FastTrack Ultra Mixer Description */
2608 /* Origin: Linux USB Audio driver */
2609 static void
2610 uaudio_mixer_controls_create_ftu(struct uaudio_softc *sc)
2611 {
2612 	int chx;
2613 	int chy;
2614 
2615 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2616 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2617 	MIX(sc).wValue[0] = MAKE_WORD(8, 0);
2618 	MIX(sc).class = UAC_OUTPUT;
2619 	MIX(sc).type = MIX_UNSIGNED_16;
2620 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2621 	MIX(sc).name = "effect";
2622 	MIX(sc).minval = 0;
2623 	MIX(sc).maxval = 7;
2624 	MIX(sc).mul = 7;
2625 	MIX(sc).nchan = 1;
2626 	MIX(sc).update[0] = 1;
2627 	strlcpy(MIX(sc).desc, "Room1,2,3,Hall1,2,Plate,Delay,Echo", sizeof(MIX(sc).desc));
2628 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2629 
2630 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2631 	MIX(sc).wIndex = MAKE_WORD(5, sc->sc_mixer_iface_no);
2632 
2633 	for (chx = 0; chx != 8; chx++) {
2634 		for (chy = 0; chy != 8; chy++) {
2635 
2636 			MIX(sc).wValue[0] = MAKE_WORD(chx + 1, chy + 1);
2637 			MIX(sc).type = MIX_SIGNED_16;
2638 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2639 			MIX(sc).name = "mix_rec";
2640 			MIX(sc).nchan = 1;
2641 			MIX(sc).update[0] = 1;
2642 			MIX(sc).val_default = 0;
2643 			snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2644 			    "AIn%d - Out%d Record Volume", chy + 1, chx + 1);
2645 
2646 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2647 
2648 			MIX(sc).wValue[0] = MAKE_WORD(chx + 1, chy + 1 + 8);
2649 			MIX(sc).type = MIX_SIGNED_16;
2650 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2651 			MIX(sc).name = "mix_play";
2652 			MIX(sc).nchan = 1;
2653 			MIX(sc).update[0] = 1;
2654 			MIX(sc).val_default = (chx == chy) ? 2 : 0;
2655 			snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2656 			    "DIn%d - Out%d Playback Volume", chy + 1, chx + 1);
2657 
2658 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2659 		}
2660 	}
2661 
2662 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2663 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2664 	MIX(sc).wValue[0] = MAKE_WORD(2, 0);
2665 	MIX(sc).class = UAC_OUTPUT;
2666 	MIX(sc).type = MIX_SIGNED_8;
2667 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2668 	MIX(sc).name = "effect_vol";
2669 	MIX(sc).nchan = 1;
2670 	MIX(sc).update[0] = 1;
2671 	MIX(sc).minval = 0;
2672 	MIX(sc).maxval = 0x7f;
2673 	MIX(sc).mul = 0x7f;
2674 	MIX(sc).nchan = 1;
2675 	MIX(sc).update[0] = 1;
2676 	strlcpy(MIX(sc).desc, "Effect Volume", sizeof(MIX(sc).desc));
2677 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2678 
2679 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2680 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2681 	MIX(sc).wValue[0] = MAKE_WORD(3, 0);
2682 	MIX(sc).class = UAC_OUTPUT;
2683 	MIX(sc).type = MIX_SIGNED_16;
2684 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2685 	MIX(sc).name = "effect_dur";
2686 	MIX(sc).nchan = 1;
2687 	MIX(sc).update[0] = 1;
2688 	MIX(sc).minval = 0;
2689 	MIX(sc).maxval = 0x7f00;
2690 	MIX(sc).mul = 0x7f00;
2691 	MIX(sc).nchan = 1;
2692 	MIX(sc).update[0] = 1;
2693 	strlcpy(MIX(sc).desc, "Effect Duration", sizeof(MIX(sc).desc));
2694 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2695 
2696 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2697 	MIX(sc).wIndex = MAKE_WORD(6, sc->sc_mixer_iface_no);
2698 	MIX(sc).wValue[0] = MAKE_WORD(4, 0);
2699 	MIX(sc).class = UAC_OUTPUT;
2700 	MIX(sc).type = MIX_SIGNED_8;
2701 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2702 	MIX(sc).name = "effect_fb";
2703 	MIX(sc).nchan = 1;
2704 	MIX(sc).update[0] = 1;
2705 	MIX(sc).minval = 0;
2706 	MIX(sc).maxval = 0x7f;
2707 	MIX(sc).mul = 0x7f;
2708 	MIX(sc).nchan = 1;
2709 	MIX(sc).update[0] = 1;
2710 	strlcpy(MIX(sc).desc, "Effect Feedback Volume", sizeof(MIX(sc).desc));
2711 	uaudio_mixer_add_ctl_sub(sc, &MIX(sc));
2712 
2713 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2714 	MIX(sc).wIndex = MAKE_WORD(7, sc->sc_mixer_iface_no);
2715 	for (chy = 0; chy != 4; chy++) {
2716 
2717 		MIX(sc).wValue[0] = MAKE_WORD(7, chy + 1);
2718 		MIX(sc).type = MIX_SIGNED_16;
2719 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2720 		MIX(sc).name = "effect_ret";
2721 		MIX(sc).nchan = 1;
2722 		MIX(sc).update[0] = 1;
2723 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2724 		    "Effect Return %d Volume", chy + 1);
2725 
2726 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2727 	}
2728 
2729 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2730 	MIX(sc).wIndex = MAKE_WORD(5, sc->sc_mixer_iface_no);
2731 
2732 	for (chy = 0; chy != 8; chy++) {
2733 		MIX(sc).wValue[0] = MAKE_WORD(9, chy + 1);
2734 		MIX(sc).type = MIX_SIGNED_16;
2735 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2736 		MIX(sc).name = "effect_send";
2737 		MIX(sc).nchan = 1;
2738 		MIX(sc).update[0] = 1;
2739 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2740 		    "Effect Send AIn%d Volume", chy + 1);
2741 
2742 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2743 
2744 		MIX(sc).wValue[0] = MAKE_WORD(9, chy + 1);
2745 		MIX(sc).type = MIX_SIGNED_16;
2746 		MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
2747 		MIX(sc).name = "effect_send";
2748 		MIX(sc).nchan = 1;
2749 		MIX(sc).update[0] = 1;
2750 		snprintf(MIX(sc).desc, sizeof(MIX(sc).desc),
2751 		    "Effect Send DIn%d Volume", chy + 1 + 8);
2752 
2753 		uaudio_mixer_add_ctl(sc, &MIX(sc));
2754 	}
2755 }
2756 
2757 static void
2758 uaudio_mixer_reload_all(struct uaudio_softc *sc)
2759 {
2760 	struct uaudio_mixer_node *pmc;
2761 	int chan;
2762 
2763 	if (sc->sc_mixer_lock == NULL)
2764 		return;
2765 
2766 	mtx_lock(sc->sc_mixer_lock);
2767 	for (pmc = sc->sc_mixer_root; pmc != NULL; pmc = pmc->next) {
2768 		/* use reset defaults for non-oss controlled settings */
2769 		if (pmc->ctl == SOUND_MIXER_NRDEVICES)
2770 			continue;
2771 		for (chan = 0; chan < pmc->nchan; chan++)
2772 			pmc->update[chan / 8] |= (1 << (chan % 8));
2773 	}
2774 	usbd_transfer_start(sc->sc_mixer_xfer[0]);
2775 
2776 	/* start HID volume keys, if any */
2777 	usbd_transfer_start(sc->sc_hid.xfer[0]);
2778 	mtx_unlock(sc->sc_mixer_lock);
2779 }
2780 
2781 static void
2782 uaudio_mixer_add_ctl_sub(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
2783 {
2784 	struct uaudio_mixer_node *p_mc_new =
2785 	    malloc(sizeof(*p_mc_new), M_USBDEV, M_WAITOK);
2786 	int ch;
2787 
2788 	if (p_mc_new != NULL) {
2789 		memcpy(p_mc_new, mc, sizeof(*p_mc_new));
2790 		p_mc_new->next = sc->sc_mixer_root;
2791 		sc->sc_mixer_root = p_mc_new;
2792 		sc->sc_mixer_count++;
2793 
2794 		/* set default value for all channels */
2795 		for (ch = 0; ch < p_mc_new->nchan; ch++) {
2796 			switch (p_mc_new->val_default) {
2797 			case 1:
2798 				/* 50% */
2799 				p_mc_new->wData[ch] = (p_mc_new->maxval + p_mc_new->minval) / 2;
2800 				break;
2801 			case 2:
2802 				/* 100% */
2803 				p_mc_new->wData[ch] = p_mc_new->maxval;
2804 				break;
2805 			default:
2806 				/* 0% */
2807 				p_mc_new->wData[ch] = p_mc_new->minval;
2808 				break;
2809 			}
2810 		}
2811 	} else {
2812 		DPRINTF("out of memory\n");
2813 	}
2814 }
2815 
2816 static void
2817 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
2818 {
2819 	int32_t res;
2820 
2821 	if (mc->class < UAC_NCLASSES) {
2822 		DPRINTF("adding %s.%d\n",
2823 		    uac_names[mc->class], mc->ctl);
2824 	} else {
2825 		DPRINTF("adding %d\n", mc->ctl);
2826 	}
2827 
2828 	if (mc->type == MIX_ON_OFF) {
2829 		mc->minval = 0;
2830 		mc->maxval = 1;
2831 	} else if (mc->type == MIX_SELECTOR) {
2832 	} else {
2833 
2834 		/* determine min and max values */
2835 
2836 		mc->minval = uaudio_mixer_get(sc->sc_udev,
2837 		    sc->sc_audio_rev, GET_MIN, mc);
2838 		mc->maxval = uaudio_mixer_get(sc->sc_udev,
2839 		    sc->sc_audio_rev, GET_MAX, mc);
2840 
2841 		/* check if max and min was swapped */
2842 
2843 		if (mc->maxval < mc->minval) {
2844 			res = mc->maxval;
2845 			mc->maxval = mc->minval;
2846 			mc->minval = res;
2847 		}
2848 
2849 		/* compute value range */
2850 		mc->mul = mc->maxval - mc->minval;
2851 		if (mc->mul == 0)
2852 			mc->mul = 1;
2853 
2854 		/* compute value alignment */
2855 		res = uaudio_mixer_get(sc->sc_udev,
2856 		    sc->sc_audio_rev, GET_RES, mc);
2857 
2858 		DPRINTF("Resolution = %d\n", (int)res);
2859 	}
2860 
2861 	uaudio_mixer_add_ctl_sub(sc, mc);
2862 
2863 #ifdef USB_DEBUG
2864 	if (uaudio_debug > 2) {
2865 		uint8_t i;
2866 
2867 		for (i = 0; i < mc->nchan; i++) {
2868 			DPRINTF("[mix] wValue=%04x\n", mc->wValue[0]);
2869 		}
2870 		DPRINTF("[mix] wIndex=%04x type=%d ctl='%d' "
2871 		    "min=%d max=%d\n",
2872 		    mc->wIndex, mc->type, mc->ctl,
2873 		    mc->minval, mc->maxval);
2874 	}
2875 #endif
2876 }
2877 
2878 static void
2879 uaudio_mixer_add_mixer(struct uaudio_softc *sc,
2880     const struct uaudio_terminal_node *iot, int id)
2881 {
2882 	const struct usb_audio_mixer_unit_0 *d0 = iot[id].u.mu_v1;
2883 	const struct usb_audio_mixer_unit_1 *d1;
2884 
2885 	uint32_t bno;			/* bit number */
2886 	uint32_t p;			/* bit number accumulator */
2887 	uint32_t mo;			/* matching outputs */
2888 	uint32_t mc;			/* matching channels */
2889 	uint32_t ichs;			/* input channels */
2890 	uint32_t ochs;			/* output channels */
2891 	uint32_t c;
2892 	uint32_t chs;			/* channels */
2893 	uint32_t i;
2894 	uint32_t o;
2895 
2896 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
2897 	    d0->bUnitId, d0->bNrInPins);
2898 
2899 	/* compute the number of input channels */
2900 
2901 	ichs = 0;
2902 	for (i = 0; i < d0->bNrInPins; i++) {
2903 		ichs += uaudio_mixer_get_cluster(
2904 		    d0->baSourceId[i], iot).bNrChannels;
2905 	}
2906 
2907 	d1 = (const void *)(d0->baSourceId + d0->bNrInPins);
2908 
2909 	/* and the number of output channels */
2910 
2911 	ochs = d1->bNrChannels;
2912 
2913 	DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs);
2914 
2915 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2916 
2917 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2918 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
2919 	MIX(sc).type = MIX_SIGNED_16;
2920 
2921 	if (uaudio_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL)
2922 		return;
2923 
2924 	for (p = i = 0; i < d0->bNrInPins; i++) {
2925 		chs = uaudio_mixer_get_cluster(
2926 		    d0->baSourceId[i], iot).bNrChannels;
2927 		mc = 0;
2928 		for (c = 0; c < chs; c++) {
2929 			mo = 0;
2930 			for (o = 0; o < ochs; o++) {
2931 				bno = ((p + c) * ochs) + o;
2932 				if (BIT_TEST(d1->bmControls, bno))
2933 					mo++;
2934 			}
2935 			if (mo == 1)
2936 				mc++;
2937 		}
2938 		if ((mc == chs) && (chs <= MIX_MAX_CHAN)) {
2939 
2940 			/* repeat bit-scan */
2941 
2942 			mc = 0;
2943 			for (c = 0; c < chs; c++) {
2944 				for (o = 0; o < ochs; o++) {
2945 					bno = ((p + c) * ochs) + o;
2946 					if (BIT_TEST(d1->bmControls, bno))
2947 						MIX(sc).wValue[mc++] = MAKE_WORD(p + c + 1, o + 1);
2948 				}
2949 			}
2950 			MIX(sc).nchan = chs;
2951 			uaudio_mixer_add_ctl(sc, &MIX(sc));
2952 		}
2953 		p += chs;
2954 	}
2955 }
2956 
2957 static void
2958 uaudio20_mixer_add_mixer(struct uaudio_softc *sc,
2959     const struct uaudio_terminal_node *iot, int id)
2960 {
2961 	const struct usb_audio20_mixer_unit_0 *d0 = iot[id].u.mu_v2;
2962 	const struct usb_audio20_mixer_unit_1 *d1;
2963 
2964 	uint32_t bno;			/* bit number */
2965 	uint32_t p;			/* bit number accumulator */
2966 	uint32_t mo;			/* matching outputs */
2967 	uint32_t mc;			/* matching channels */
2968 	uint32_t ichs;			/* input channels */
2969 	uint32_t ochs;			/* output channels */
2970 	uint32_t c;
2971 	uint32_t chs;			/* channels */
2972 	uint32_t i;
2973 	uint32_t o;
2974 
2975 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
2976 	    d0->bUnitId, d0->bNrInPins);
2977 
2978 	/* compute the number of input channels */
2979 
2980 	ichs = 0;
2981 	for (i = 0; i < d0->bNrInPins; i++) {
2982 		ichs += uaudio20_mixer_get_cluster(
2983 		    d0->baSourceId[i], iot).bNrChannels;
2984 	}
2985 
2986 	d1 = (const void *)(d0->baSourceId + d0->bNrInPins);
2987 
2988 	/* and the number of output channels */
2989 
2990 	ochs = d1->bNrChannels;
2991 
2992 	DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs);
2993 
2994 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
2995 
2996 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2997 	uaudio20_mixer_determine_class(&iot[id], &MIX(sc));
2998 	MIX(sc).type = MIX_SIGNED_16;
2999 
3000 	if (uaudio20_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL)
3001 		return;
3002 
3003 	for (p = i = 0; i < d0->bNrInPins; i++) {
3004 		chs = uaudio20_mixer_get_cluster(
3005 		    d0->baSourceId[i], iot).bNrChannels;
3006 		mc = 0;
3007 		for (c = 0; c < chs; c++) {
3008 			mo = 0;
3009 			for (o = 0; o < ochs; o++) {
3010 				bno = ((p + c) * ochs) + o;
3011 				if (BIT_TEST(d1->bmControls, bno))
3012 					mo++;
3013 			}
3014 			if (mo == 1)
3015 				mc++;
3016 		}
3017 		if ((mc == chs) && (chs <= MIX_MAX_CHAN)) {
3018 
3019 			/* repeat bit-scan */
3020 
3021 			mc = 0;
3022 			for (c = 0; c < chs; c++) {
3023 				for (o = 0; o < ochs; o++) {
3024 					bno = ((p + c) * ochs) + o;
3025 					if (BIT_TEST(d1->bmControls, bno))
3026 						MIX(sc).wValue[mc++] = MAKE_WORD(p + c + 1, o + 1);
3027 				}
3028 			}
3029 			MIX(sc).nchan = chs;
3030 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3031 		}
3032 		p += chs;
3033 	}
3034 }
3035 
3036 static void
3037 uaudio_mixer_add_selector(struct uaudio_softc *sc,
3038     const struct uaudio_terminal_node *iot, int id)
3039 {
3040 	const struct usb_audio_selector_unit *d = iot[id].u.su_v1;
3041 	uint16_t i;
3042 
3043 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3044 	    d->bUnitId, d->bNrInPins);
3045 
3046 	if (d->bNrInPins == 0)
3047 		return;
3048 
3049 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3050 
3051 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3052 	MIX(sc).wValue[0] = MAKE_WORD(0, 0);
3053 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3054 	MIX(sc).nchan = 1;
3055 	MIX(sc).type = MIX_SELECTOR;
3056 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3057 	MIX(sc).minval = 1;
3058 	MIX(sc).maxval = d->bNrInPins;
3059 	MIX(sc).name = "selector";
3060 
3061 	i = d->baSourceId[d->bNrInPins];
3062 	if (i == 0 ||
3063 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3064 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3065 		MIX(sc).desc[0] = 0;
3066 	}
3067 
3068 	if (MIX(sc).maxval > MAX_SELECTOR_INPUT_PIN) {
3069 		MIX(sc).maxval = MAX_SELECTOR_INPUT_PIN;
3070 	}
3071 	MIX(sc).mul = (MIX(sc).maxval - MIX(sc).minval);
3072 	for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) {
3073 		MIX(sc).slctrtype[i] = SOUND_MIXER_NRDEVICES;
3074 	}
3075 
3076 	for (i = 0; i < MIX(sc).maxval; i++) {
3077 		MIX(sc).slctrtype[i] = uaudio_mixer_feature_name(
3078 		    &iot[d->baSourceId[i]], &MIX(sc));
3079 	}
3080 
3081 	MIX(sc).class = 0;			/* not used */
3082 
3083 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3084 }
3085 
3086 static void
3087 uaudio20_mixer_add_selector(struct uaudio_softc *sc,
3088     const struct uaudio_terminal_node *iot, int id)
3089 {
3090 	const struct usb_audio20_selector_unit *d = iot[id].u.su_v2;
3091 	uint16_t i;
3092 
3093 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3094 	    d->bUnitId, d->bNrInPins);
3095 
3096 	if (d->bNrInPins == 0)
3097 		return;
3098 
3099 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3100 
3101 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3102 	MIX(sc).wValue[0] = MAKE_WORD(0, 0);
3103 	uaudio20_mixer_determine_class(&iot[id], &MIX(sc));
3104 	MIX(sc).nchan = 1;
3105 	MIX(sc).type = MIX_SELECTOR;
3106 	MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3107 	MIX(sc).minval = 1;
3108 	MIX(sc).maxval = d->bNrInPins;
3109 	MIX(sc).name = "selector";
3110 
3111 	i = d->baSourceId[d->bNrInPins];
3112 	if (i == 0 ||
3113 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3114 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3115 		MIX(sc).desc[0] = 0;
3116 	}
3117 
3118 	if (MIX(sc).maxval > MAX_SELECTOR_INPUT_PIN)
3119 		MIX(sc).maxval = MAX_SELECTOR_INPUT_PIN;
3120 
3121 	MIX(sc).mul = (MIX(sc).maxval - MIX(sc).minval);
3122 	for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++)
3123 		MIX(sc).slctrtype[i] = SOUND_MIXER_NRDEVICES;
3124 
3125 	for (i = 0; i < MIX(sc).maxval; i++) {
3126 		MIX(sc).slctrtype[i] = uaudio20_mixer_feature_name(
3127 		    &iot[d->baSourceId[i]], &MIX(sc));
3128 	}
3129 
3130 	MIX(sc).class = 0;			/* not used */
3131 
3132 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3133 }
3134 
3135 static uint32_t
3136 uaudio_mixer_feature_get_bmaControls(const struct usb_audio_feature_unit *d,
3137     uint8_t i)
3138 {
3139 	uint32_t temp = 0;
3140 	uint32_t offset = (i * d->bControlSize);
3141 
3142 	if (d->bControlSize > 0) {
3143 		temp |= d->bmaControls[offset];
3144 		if (d->bControlSize > 1) {
3145 			temp |= d->bmaControls[offset + 1] << 8;
3146 			if (d->bControlSize > 2) {
3147 				temp |= d->bmaControls[offset + 2] << 16;
3148 				if (d->bControlSize > 3) {
3149 					temp |= d->bmaControls[offset + 3] << 24;
3150 				}
3151 			}
3152 		}
3153 	}
3154 	return (temp);
3155 }
3156 
3157 static void
3158 uaudio_mixer_add_feature(struct uaudio_softc *sc,
3159     const struct uaudio_terminal_node *iot, int id)
3160 {
3161 	const struct usb_audio_feature_unit *d = iot[id].u.fu_v1;
3162 	uint32_t fumask;
3163 	uint32_t mmask;
3164 	uint32_t cmask;
3165 	uint16_t mixernumber;
3166 	uint8_t nchan;
3167 	uint8_t chan;
3168 	uint8_t ctl;
3169 	uint8_t i;
3170 
3171 	if (d->bControlSize == 0)
3172 		return;
3173 
3174 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3175 
3176 	nchan = (d->bLength - 7) / d->bControlSize;
3177 	mmask = uaudio_mixer_feature_get_bmaControls(d, 0);
3178 	cmask = 0;
3179 
3180 	if (nchan == 0)
3181 		return;
3182 
3183 	/* figure out what we can control */
3184 
3185 	for (chan = 1; chan < nchan; chan++) {
3186 		DPRINTFN(10, "chan=%d mask=%x\n",
3187 		    chan, uaudio_mixer_feature_get_bmaControls(d, chan));
3188 
3189 		cmask |= uaudio_mixer_feature_get_bmaControls(d, chan);
3190 	}
3191 
3192 	if (nchan > MIX_MAX_CHAN) {
3193 		nchan = MIX_MAX_CHAN;
3194 	}
3195 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3196 
3197 	i = d->bmaControls[d->bControlSize];
3198 	if (i == 0 ||
3199 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3200 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3201 		MIX(sc).desc[0] = 0;
3202 	}
3203 
3204 	for (ctl = 1; ctl <= LOUDNESS_CONTROL; ctl++) {
3205 
3206 		fumask = FU_MASK(ctl);
3207 
3208 		DPRINTFN(5, "ctl=%d fumask=0x%04x\n",
3209 		    ctl, fumask);
3210 
3211 		if (mmask & fumask) {
3212 			MIX(sc).nchan = 1;
3213 			MIX(sc).wValue[0] = MAKE_WORD(ctl, 0);
3214 		} else if (cmask & fumask) {
3215 			MIX(sc).nchan = nchan - 1;
3216 			for (i = 1; i < nchan; i++) {
3217 				if (uaudio_mixer_feature_get_bmaControls(d, i) & fumask)
3218 					MIX(sc).wValue[i - 1] = MAKE_WORD(ctl, i);
3219 				else
3220 					MIX(sc).wValue[i - 1] = -1;
3221 			}
3222 		} else {
3223 			continue;
3224 		}
3225 
3226 		mixernumber = uaudio_mixer_feature_name(&iot[id], &MIX(sc));
3227 
3228 		switch (ctl) {
3229 		case MUTE_CONTROL:
3230 			MIX(sc).type = MIX_ON_OFF;
3231 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3232 			MIX(sc).name = "mute";
3233 			break;
3234 
3235 		case VOLUME_CONTROL:
3236 			MIX(sc).type = MIX_SIGNED_16;
3237 			MIX(sc).ctl = mixernumber;
3238 			MIX(sc).name = "vol";
3239 			break;
3240 
3241 		case BASS_CONTROL:
3242 			MIX(sc).type = MIX_SIGNED_8;
3243 			MIX(sc).ctl = SOUND_MIXER_BASS;
3244 			MIX(sc).name = "bass";
3245 			break;
3246 
3247 		case MID_CONTROL:
3248 			MIX(sc).type = MIX_SIGNED_8;
3249 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3250 			MIX(sc).name = "mid";
3251 			break;
3252 
3253 		case TREBLE_CONTROL:
3254 			MIX(sc).type = MIX_SIGNED_8;
3255 			MIX(sc).ctl = SOUND_MIXER_TREBLE;
3256 			MIX(sc).name = "treble";
3257 			break;
3258 
3259 		case GRAPHIC_EQUALIZER_CONTROL:
3260 			continue;	/* XXX don't add anything */
3261 
3262 		case AGC_CONTROL:
3263 			MIX(sc).type = MIX_ON_OFF;
3264 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3265 			MIX(sc).name = "agc";
3266 			break;
3267 
3268 		case DELAY_CONTROL:
3269 			MIX(sc).type = MIX_UNSIGNED_16;
3270 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3271 			MIX(sc).name = "delay";
3272 			break;
3273 
3274 		case BASS_BOOST_CONTROL:
3275 			MIX(sc).type = MIX_ON_OFF;
3276 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3277 			MIX(sc).name = "boost";
3278 			break;
3279 
3280 		case LOUDNESS_CONTROL:
3281 			MIX(sc).type = MIX_ON_OFF;
3282 			MIX(sc).ctl = SOUND_MIXER_LOUD;	/* Is this correct ? */
3283 			MIX(sc).name = "loudness";
3284 			break;
3285 
3286 		default:
3287 			MIX(sc).type = MIX_UNKNOWN;
3288 			break;
3289 		}
3290 
3291 		if (MIX(sc).type != MIX_UNKNOWN)
3292 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3293 	}
3294 }
3295 
3296 static void
3297 uaudio20_mixer_add_feature(struct uaudio_softc *sc,
3298     const struct uaudio_terminal_node *iot, int id)
3299 {
3300 	const struct usb_audio20_feature_unit *d = iot[id].u.fu_v2;
3301 	uint32_t ctl;
3302 	uint32_t mmask;
3303 	uint32_t cmask;
3304 	uint16_t mixernumber;
3305 	uint8_t nchan;
3306 	uint8_t chan;
3307 	uint8_t i;
3308 	uint8_t what;
3309 
3310 	if (UGETDW(d->bmaControls[0]) == 0)
3311 		return;
3312 
3313 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3314 
3315 	nchan = (d->bLength - 6) / 4;
3316 	mmask = UGETDW(d->bmaControls[0]);
3317 	cmask = 0;
3318 
3319 	if (nchan == 0)
3320 		return;
3321 
3322 	/* figure out what we can control */
3323 
3324 	for (chan = 1; chan < nchan; chan++)
3325 		cmask |= UGETDW(d->bmaControls[chan]);
3326 
3327 	if (nchan > MIX_MAX_CHAN)
3328 		nchan = MIX_MAX_CHAN;
3329 
3330 	MIX(sc).wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
3331 
3332 	i = d->bmaControls[nchan][0];
3333 	if (i == 0 ||
3334 	    usbd_req_get_string_any(sc->sc_udev, NULL,
3335 	    MIX(sc).desc, sizeof(MIX(sc).desc), i) != 0) {
3336 		MIX(sc).desc[0] = 0;
3337 	}
3338 
3339 	for (ctl = 3; ctl != 0; ctl <<= 2) {
3340 
3341 		mixernumber = uaudio20_mixer_feature_name(&iot[id], &MIX(sc));
3342 
3343 		switch (ctl) {
3344 		case (3 << 0):
3345 			MIX(sc).type = MIX_ON_OFF;
3346 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;
3347 			MIX(sc).name = "mute";
3348 			what = MUTE_CONTROL;
3349 			break;
3350 		case (3 << 2):
3351 			MIX(sc).type = MIX_SIGNED_16;
3352 			MIX(sc).ctl = mixernumber;
3353 			MIX(sc).name = "vol";
3354 			what = VOLUME_CONTROL;
3355 			break;
3356 		case (3 << 4):
3357 			MIX(sc).type = MIX_SIGNED_8;
3358 			MIX(sc).ctl = SOUND_MIXER_BASS;
3359 			MIX(sc).name = "bass";
3360 			what = BASS_CONTROL;
3361 			break;
3362 		case (3 << 6):
3363 			MIX(sc).type = MIX_SIGNED_8;
3364 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3365 			MIX(sc).name = "mid";
3366 			what = MID_CONTROL;
3367 			break;
3368 		case (3 << 8):
3369 			MIX(sc).type = MIX_SIGNED_8;
3370 			MIX(sc).ctl = SOUND_MIXER_TREBLE;
3371 			MIX(sc).name = "treble";
3372 			what = TREBLE_CONTROL;
3373 			break;
3374 		case (3 << 12):
3375 			MIX(sc).type = MIX_ON_OFF;
3376 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3377 			MIX(sc).name = "agc";
3378 			what = AGC_CONTROL;
3379 			break;
3380 		case (3 << 14):
3381 			MIX(sc).type = MIX_UNSIGNED_16;
3382 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3383 			MIX(sc).name = "delay";
3384 			what = DELAY_CONTROL;
3385 			break;
3386 		case (3 << 16):
3387 			MIX(sc).type = MIX_ON_OFF;
3388 			MIX(sc).ctl = SOUND_MIXER_NRDEVICES;	/* XXXXX */
3389 			MIX(sc).name = "boost";
3390 			what = BASS_BOOST_CONTROL;
3391 			break;
3392 		case (3 << 18):
3393 			MIX(sc).type = MIX_ON_OFF;
3394 			MIX(sc).ctl = SOUND_MIXER_LOUD;	/* Is this correct ? */
3395 			MIX(sc).name = "loudness";
3396 			what = LOUDNESS_CONTROL;
3397 			break;
3398 		case (3 << 20):
3399 			MIX(sc).type = MIX_SIGNED_16;
3400 			MIX(sc).ctl = mixernumber;
3401 			MIX(sc).name = "igain";
3402 			what = INPUT_GAIN_CONTROL;
3403 			break;
3404 		case (3 << 22):
3405 			MIX(sc).type = MIX_SIGNED_16;
3406 			MIX(sc).ctl = mixernumber;
3407 			MIX(sc).name = "igainpad";
3408 			what = INPUT_GAIN_PAD_CONTROL;
3409 			break;
3410 		default:
3411 			continue;
3412 		}
3413 
3414 		if ((mmask & ctl) == ctl) {
3415 			MIX(sc).nchan = 1;
3416 			MIX(sc).wValue[0] = MAKE_WORD(what, 0);
3417 		} else if ((cmask & ctl) == ctl) {
3418 			MIX(sc).nchan = nchan - 1;
3419 			for (i = 1; i < nchan; i++) {
3420 				if ((UGETDW(d->bmaControls[i]) & ctl) == ctl)
3421 					MIX(sc).wValue[i - 1] = MAKE_WORD(what, i);
3422 				else
3423 					MIX(sc).wValue[i - 1] = -1;
3424 			}
3425 		} else {
3426 			continue;
3427 		}
3428 
3429 		if (MIX(sc).type != MIX_UNKNOWN)
3430 			uaudio_mixer_add_ctl(sc, &MIX(sc));
3431 	}
3432 }
3433 
3434 static void
3435 uaudio_mixer_add_processing_updown(struct uaudio_softc *sc,
3436     const struct uaudio_terminal_node *iot, int id)
3437 {
3438 	const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1;
3439 	const struct usb_audio_processing_unit_1 *d1 =
3440 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3441 	const struct usb_audio_processing_unit_updown *ud =
3442 	    (const void *)(d1->bmControls + d1->bControlSize);
3443 	uint8_t i;
3444 
3445 	if (uaudio_mixer_verify_desc(d0, sizeof(*ud)) == NULL) {
3446 		return;
3447 	}
3448 	if (uaudio_mixer_verify_desc(d0, sizeof(*ud) + (2 * ud->bNrModes))
3449 	    == NULL) {
3450 		return;
3451 	}
3452 	DPRINTFN(3, "bUnitId=%d bNrModes=%d\n",
3453 	    d0->bUnitId, ud->bNrModes);
3454 
3455 	if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
3456 		DPRINTF("no mode select\n");
3457 		return;
3458 	}
3459 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3460 
3461 	MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3462 	MIX(sc).nchan = 1;
3463 	MIX(sc).wValue[0] = MAKE_WORD(UD_MODE_SELECT_CONTROL, 0);
3464 	uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3465 	MIX(sc).type = MIX_ON_OFF;		/* XXX */
3466 
3467 	for (i = 0; i < ud->bNrModes; i++) {
3468 		DPRINTFN(3, "i=%d bm=0x%x\n", i, UGETW(ud->waModes[i]));
3469 		/* XXX */
3470 	}
3471 
3472 	uaudio_mixer_add_ctl(sc, &MIX(sc));
3473 }
3474 
3475 static void
3476 uaudio_mixer_add_processing(struct uaudio_softc *sc,
3477     const struct uaudio_terminal_node *iot, int id)
3478 {
3479 	const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1;
3480 	const struct usb_audio_processing_unit_1 *d1 =
3481 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3482 	uint16_t ptype;
3483 
3484 	memset(&MIX(sc), 0, sizeof(MIX(sc)));
3485 
3486 	ptype = UGETW(d0->wProcessType);
3487 
3488 	DPRINTFN(3, "wProcessType=%d bUnitId=%d "
3489 	    "bNrInPins=%d\n", ptype, d0->bUnitId, d0->bNrInPins);
3490 
3491 	if (d1->bControlSize == 0) {
3492 		return;
3493 	}
3494 	if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
3495 		MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3496 		MIX(sc).nchan = 1;
3497 		MIX(sc).wValue[0] = MAKE_WORD(XX_ENABLE_CONTROL, 0);
3498 		uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3499 		MIX(sc).type = MIX_ON_OFF;
3500 		uaudio_mixer_add_ctl(sc, &MIX(sc));
3501 	}
3502 	switch (ptype) {
3503 	case UPDOWNMIX_PROCESS:
3504 		uaudio_mixer_add_processing_updown(sc, iot, id);
3505 		break;
3506 
3507 	case DOLBY_PROLOGIC_PROCESS:
3508 	case P3D_STEREO_EXTENDER_PROCESS:
3509 	case REVERBATION_PROCESS:
3510 	case CHORUS_PROCESS:
3511 	case DYN_RANGE_COMP_PROCESS:
3512 	default:
3513 		DPRINTF("unit %d, type=%d is not implemented\n",
3514 		    d0->bUnitId, ptype);
3515 		break;
3516 	}
3517 }
3518 
3519 static void
3520 uaudio_mixer_add_extension(struct uaudio_softc *sc,
3521     const struct uaudio_terminal_node *iot, int id)
3522 {
3523 	const struct usb_audio_extension_unit_0 *d0 = iot[id].u.eu_v1;
3524 	const struct usb_audio_extension_unit_1 *d1 =
3525 	    (const void *)(d0->baSourceId + d0->bNrInPins);
3526 
3527 	DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
3528 	    d0->bUnitId, d0->bNrInPins);
3529 
3530 	if (sc->sc_uq_au_no_xu) {
3531 		return;
3532 	}
3533 	if (d1->bControlSize == 0) {
3534 		return;
3535 	}
3536 	if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
3537 
3538 		memset(&MIX(sc), 0, sizeof(MIX(sc)));
3539 
3540 		MIX(sc).wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
3541 		MIX(sc).nchan = 1;
3542 		MIX(sc).wValue[0] = MAKE_WORD(UA_EXT_ENABLE, 0);
3543 		uaudio_mixer_determine_class(&iot[id], &MIX(sc));
3544 		MIX(sc).type = MIX_ON_OFF;
3545 
3546 		uaudio_mixer_add_ctl(sc, &MIX(sc));
3547 	}
3548 }
3549 
3550 static const void *
3551 uaudio_mixer_verify_desc(const void *arg, uint32_t len)
3552 {
3553 	const struct usb_audio_mixer_unit_1 *d1;
3554 	const struct usb_audio_extension_unit_1 *e1;
3555 	const struct usb_audio_processing_unit_1 *u1;
3556 
3557 	union {
3558 		const struct usb_descriptor *desc;
3559 		const struct usb_audio_input_terminal *it;
3560 		const struct usb_audio_output_terminal *ot;
3561 		const struct usb_audio_mixer_unit_0 *mu;
3562 		const struct usb_audio_selector_unit *su;
3563 		const struct usb_audio_feature_unit *fu;
3564 		const struct usb_audio_processing_unit_0 *pu;
3565 		const struct usb_audio_extension_unit_0 *eu;
3566 	}     u;
3567 
3568 	u.desc = arg;
3569 
3570 	if (u.desc == NULL) {
3571 		goto error;
3572 	}
3573 	if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) {
3574 		goto error;
3575 	}
3576 	switch (u.desc->bDescriptorSubtype) {
3577 	case UDESCSUB_AC_INPUT:
3578 		len += sizeof(*u.it);
3579 		break;
3580 
3581 	case UDESCSUB_AC_OUTPUT:
3582 		len += sizeof(*u.ot);
3583 		break;
3584 
3585 	case UDESCSUB_AC_MIXER:
3586 		len += sizeof(*u.mu);
3587 
3588 		if (u.desc->bLength < len) {
3589 			goto error;
3590 		}
3591 		len += u.mu->bNrInPins;
3592 
3593 		if (u.desc->bLength < len) {
3594 			goto error;
3595 		}
3596 		d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins);
3597 
3598 		len += sizeof(*d1);
3599 		break;
3600 
3601 	case UDESCSUB_AC_SELECTOR:
3602 		len += sizeof(*u.su);
3603 
3604 		if (u.desc->bLength < len) {
3605 			goto error;
3606 		}
3607 		len += u.su->bNrInPins + 1;
3608 		break;
3609 
3610 	case UDESCSUB_AC_FEATURE:
3611 		len += sizeof(*u.fu) + 1;
3612 
3613 		if (u.desc->bLength < len)
3614 			goto error;
3615 
3616 		len += u.fu->bControlSize;
3617 		break;
3618 
3619 	case UDESCSUB_AC_PROCESSING:
3620 		len += sizeof(*u.pu);
3621 
3622 		if (u.desc->bLength < len) {
3623 			goto error;
3624 		}
3625 		len += u.pu->bNrInPins;
3626 
3627 		if (u.desc->bLength < len) {
3628 			goto error;
3629 		}
3630 		u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins);
3631 
3632 		len += sizeof(*u1);
3633 
3634 		if (u.desc->bLength < len) {
3635 			goto error;
3636 		}
3637 		len += u1->bControlSize;
3638 
3639 		break;
3640 
3641 	case UDESCSUB_AC_EXTENSION:
3642 		len += sizeof(*u.eu);
3643 
3644 		if (u.desc->bLength < len) {
3645 			goto error;
3646 		}
3647 		len += u.eu->bNrInPins;
3648 
3649 		if (u.desc->bLength < len) {
3650 			goto error;
3651 		}
3652 		e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins);
3653 
3654 		len += sizeof(*e1);
3655 
3656 		if (u.desc->bLength < len) {
3657 			goto error;
3658 		}
3659 		len += e1->bControlSize;
3660 		break;
3661 
3662 	default:
3663 		goto error;
3664 	}
3665 
3666 	if (u.desc->bLength < len) {
3667 		goto error;
3668 	}
3669 	return (u.desc);
3670 
3671 error:
3672 	if (u.desc) {
3673 		DPRINTF("invalid descriptor, type=%d, "
3674 		    "sub_type=%d, len=%d of %d bytes\n",
3675 		    u.desc->bDescriptorType,
3676 		    u.desc->bDescriptorSubtype,
3677 		    u.desc->bLength, len);
3678 	}
3679 	return (NULL);
3680 }
3681 
3682 static const void *
3683 uaudio20_mixer_verify_desc(const void *arg, uint32_t len)
3684 {
3685 	const struct usb_audio20_mixer_unit_1 *d1;
3686 	const struct usb_audio20_extension_unit_1 *e1;
3687 	const struct usb_audio20_processing_unit_1 *u1;
3688 	const struct usb_audio20_clock_selector_unit_1 *c1;
3689 
3690 	union {
3691 		const struct usb_descriptor *desc;
3692 		const struct usb_audio20_clock_source_unit *csrc;
3693 		const struct usb_audio20_clock_selector_unit_0 *csel;
3694 		const struct usb_audio20_clock_multiplier_unit *cmul;
3695 		const struct usb_audio20_input_terminal *it;
3696 		const struct usb_audio20_output_terminal *ot;
3697 		const struct usb_audio20_mixer_unit_0 *mu;
3698 		const struct usb_audio20_selector_unit *su;
3699 		const struct usb_audio20_feature_unit *fu;
3700 		const struct usb_audio20_sample_rate_unit *ru;
3701 		const struct usb_audio20_processing_unit_0 *pu;
3702 		const struct usb_audio20_extension_unit_0 *eu;
3703 		const struct usb_audio20_effect_unit *ef;
3704 	}     u;
3705 
3706 	u.desc = arg;
3707 
3708 	if (u.desc == NULL)
3709 		goto error;
3710 
3711 	if (u.desc->bDescriptorType != UDESC_CS_INTERFACE)
3712 		goto error;
3713 
3714 	switch (u.desc->bDescriptorSubtype) {
3715 	case UDESCSUB_AC_INPUT:
3716 		len += sizeof(*u.it);
3717 		break;
3718 
3719 	case UDESCSUB_AC_OUTPUT:
3720 		len += sizeof(*u.ot);
3721 		break;
3722 
3723 	case UDESCSUB_AC_MIXER:
3724 		len += sizeof(*u.mu);
3725 
3726 		if (u.desc->bLength < len)
3727 			goto error;
3728 		len += u.mu->bNrInPins;
3729 
3730 		if (u.desc->bLength < len)
3731 			goto error;
3732 
3733 		d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins);
3734 
3735 		len += sizeof(*d1) + d1->bNrChannels;
3736 		break;
3737 
3738 	case UDESCSUB_AC_SELECTOR:
3739 		len += sizeof(*u.su);
3740 
3741 		if (u.desc->bLength < len)
3742 			goto error;
3743 
3744 		len += u.su->bNrInPins + 1;
3745 		break;
3746 
3747 	case UDESCSUB_AC_FEATURE:
3748 		len += sizeof(*u.fu) + 1;
3749 		break;
3750 
3751 	case UDESCSUB_AC_EFFECT:
3752 		len += sizeof(*u.ef) + 4;
3753 		break;
3754 
3755 	case UDESCSUB_AC_PROCESSING_V2:
3756 		len += sizeof(*u.pu);
3757 
3758 		if (u.desc->bLength < len)
3759 			goto error;
3760 
3761 		len += u.pu->bNrInPins;
3762 
3763 		if (u.desc->bLength < len)
3764 			goto error;
3765 
3766 		u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins);
3767 
3768 		len += sizeof(*u1);
3769 		break;
3770 
3771 	case UDESCSUB_AC_EXTENSION_V2:
3772 		len += sizeof(*u.eu);
3773 
3774 		if (u.desc->bLength < len)
3775 			goto error;
3776 
3777 		len += u.eu->bNrInPins;
3778 
3779 		if (u.desc->bLength < len)
3780 			goto error;
3781 
3782 		e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins);
3783 
3784 		len += sizeof(*e1);
3785 		break;
3786 
3787 	case UDESCSUB_AC_CLOCK_SRC:
3788 		len += sizeof(*u.csrc);
3789 		break;
3790 
3791 	case UDESCSUB_AC_CLOCK_SEL:
3792 		len += sizeof(*u.csel);
3793 
3794 		if (u.desc->bLength < len)
3795 			goto error;
3796 
3797 		len += u.csel->bNrInPins;
3798 
3799 		if (u.desc->bLength < len)
3800 			goto error;
3801 
3802 		c1 = (const void *)(u.csel->baCSourceId + u.csel->bNrInPins);
3803 
3804 		len += sizeof(*c1);
3805 		break;
3806 
3807 	case UDESCSUB_AC_CLOCK_MUL:
3808 		len += sizeof(*u.cmul);
3809 		break;
3810 
3811 	case UDESCSUB_AC_SAMPLE_RT:
3812 		len += sizeof(*u.ru);
3813 		break;
3814 
3815 	default:
3816 		goto error;
3817 	}
3818 
3819 	if (u.desc->bLength < len)
3820 		goto error;
3821 
3822 	return (u.desc);
3823 
3824 error:
3825 	if (u.desc) {
3826 		DPRINTF("invalid descriptor, type=%d, "
3827 		    "sub_type=%d, len=%d of %d bytes\n",
3828 		    u.desc->bDescriptorType,
3829 		    u.desc->bDescriptorSubtype,
3830 		    u.desc->bLength, len);
3831 	}
3832 	return (NULL);
3833 }
3834 
3835 static struct usb_audio_cluster
3836 uaudio_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
3837 {
3838 	struct usb_audio_cluster r;
3839 	const struct usb_descriptor *dp;
3840 	uint8_t i;
3841 
3842 	for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) {	/* avoid infinite loops */
3843 		dp = iot[id].u.desc;
3844 		if (dp == NULL) {
3845 			goto error;
3846 		}
3847 		switch (dp->bDescriptorSubtype) {
3848 		case UDESCSUB_AC_INPUT:
3849 			r.bNrChannels = iot[id].u.it_v1->bNrChannels;
3850 			r.wChannelConfig[0] = iot[id].u.it_v1->wChannelConfig[0];
3851 			r.wChannelConfig[1] = iot[id].u.it_v1->wChannelConfig[1];
3852 			r.iChannelNames = iot[id].u.it_v1->iChannelNames;
3853 			goto done;
3854 
3855 		case UDESCSUB_AC_OUTPUT:
3856 			id = iot[id].u.ot_v1->bSourceId;
3857 			break;
3858 
3859 		case UDESCSUB_AC_MIXER:
3860 			r = *(const struct usb_audio_cluster *)
3861 			    &iot[id].u.mu_v1->baSourceId[
3862 			    iot[id].u.mu_v1->bNrInPins];
3863 			goto done;
3864 
3865 		case UDESCSUB_AC_SELECTOR:
3866 			if (iot[id].u.su_v1->bNrInPins > 0) {
3867 				/* XXX This is not really right */
3868 				id = iot[id].u.su_v1->baSourceId[0];
3869 			}
3870 			break;
3871 
3872 		case UDESCSUB_AC_FEATURE:
3873 			id = iot[id].u.fu_v1->bSourceId;
3874 			break;
3875 
3876 		case UDESCSUB_AC_PROCESSING:
3877 			r = *((const struct usb_audio_cluster *)
3878 			    &iot[id].u.pu_v1->baSourceId[
3879 			    iot[id].u.pu_v1->bNrInPins]);
3880 			goto done;
3881 
3882 		case UDESCSUB_AC_EXTENSION:
3883 			r = *((const struct usb_audio_cluster *)
3884 			    &iot[id].u.eu_v1->baSourceId[
3885 			    iot[id].u.eu_v1->bNrInPins]);
3886 			goto done;
3887 
3888 		default:
3889 			goto error;
3890 		}
3891 	}
3892 error:
3893 	DPRINTF("bad data\n");
3894 	memset(&r, 0, sizeof(r));
3895 done:
3896 	return (r);
3897 }
3898 
3899 static struct usb_audio20_cluster
3900 uaudio20_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
3901 {
3902 	struct usb_audio20_cluster r;
3903 	const struct usb_descriptor *dp;
3904 	uint8_t i;
3905 
3906 	for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) {	/* avoid infinite loops */
3907 		dp = iot[id].u.desc;
3908 		if (dp == NULL)
3909 			goto error;
3910 
3911 		switch (dp->bDescriptorSubtype) {
3912 		case UDESCSUB_AC_INPUT:
3913 			r.bNrChannels = iot[id].u.it_v2->bNrChannels;
3914 			r.bmChannelConfig[0] = iot[id].u.it_v2->bmChannelConfig[0];
3915 			r.bmChannelConfig[1] = iot[id].u.it_v2->bmChannelConfig[1];
3916 			r.bmChannelConfig[2] = iot[id].u.it_v2->bmChannelConfig[2];
3917 			r.bmChannelConfig[3] = iot[id].u.it_v2->bmChannelConfig[3];
3918 			r.iChannelNames = iot[id].u.it_v2->iTerminal;
3919 			goto done;
3920 
3921 		case UDESCSUB_AC_OUTPUT:
3922 			id = iot[id].u.ot_v2->bSourceId;
3923 			break;
3924 
3925 		case UDESCSUB_AC_MIXER:
3926 			r = *(const struct usb_audio20_cluster *)
3927 			    &iot[id].u.mu_v2->baSourceId[
3928 			    iot[id].u.mu_v2->bNrInPins];
3929 			goto done;
3930 
3931 		case UDESCSUB_AC_SELECTOR:
3932 			if (iot[id].u.su_v2->bNrInPins > 0) {
3933 				/* XXX This is not really right */
3934 				id = iot[id].u.su_v2->baSourceId[0];
3935 			}
3936 			break;
3937 
3938 		case UDESCSUB_AC_SAMPLE_RT:
3939 			id = iot[id].u.ru_v2->bSourceId;
3940 			break;
3941 
3942 		case UDESCSUB_AC_EFFECT:
3943 			id = iot[id].u.ef_v2->bSourceId;
3944 			break;
3945 
3946 		case UDESCSUB_AC_FEATURE:
3947 			id = iot[id].u.fu_v2->bSourceId;
3948 			break;
3949 
3950 		case UDESCSUB_AC_PROCESSING_V2:
3951 			r = *((const struct usb_audio20_cluster *)
3952 			    &iot[id].u.pu_v2->baSourceId[
3953 			    iot[id].u.pu_v2->bNrInPins]);
3954 			goto done;
3955 
3956 		case UDESCSUB_AC_EXTENSION_V2:
3957 			r = *((const struct usb_audio20_cluster *)
3958 			    &iot[id].u.eu_v2->baSourceId[
3959 			    iot[id].u.eu_v2->bNrInPins]);
3960 			goto done;
3961 
3962 		default:
3963 			goto error;
3964 		}
3965 	}
3966 error:
3967 	DPRINTF("Bad data!\n");
3968 	memset(&r, 0, sizeof(r));
3969 done:
3970 	return (r);
3971 }
3972 
3973 static uint16_t
3974 uaudio_mixer_determine_class(const struct uaudio_terminal_node *iot,
3975     struct uaudio_mixer_node *mix)
3976 {
3977 	uint16_t terminal_type = 0x0000;
3978 	const struct uaudio_terminal_node *input[2];
3979 	const struct uaudio_terminal_node *output[2];
3980 
3981 	input[0] = uaudio_mixer_get_input(iot, 0);
3982 	input[1] = uaudio_mixer_get_input(iot, 1);
3983 
3984 	output[0] = uaudio_mixer_get_output(iot, 0);
3985 	output[1] = uaudio_mixer_get_output(iot, 1);
3986 
3987 	/*
3988 	 * check if there is only
3989 	 * one output terminal:
3990 	 */
3991 	if (output[0] && (!output[1])) {
3992 		terminal_type =
3993 		    UGETW(output[0]->u.ot_v1->wTerminalType);
3994 	}
3995 	/*
3996 	 * If the only output terminal is USB,
3997 	 * the class is UAC_RECORD.
3998 	 */
3999 	if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
4000 
4001 		mix->class = UAC_RECORD;
4002 		if (input[0] && (!input[1])) {
4003 			terminal_type =
4004 			    UGETW(input[0]->u.it_v1->wTerminalType);
4005 		} else {
4006 			terminal_type = 0;
4007 		}
4008 		goto done;
4009 	}
4010 	/*
4011 	 * if the unit is connected to just
4012 	 * one input terminal, the
4013 	 * class is UAC_INPUT:
4014 	 */
4015 	if (input[0] && (!input[1])) {
4016 		mix->class = UAC_INPUT;
4017 		terminal_type =
4018 		    UGETW(input[0]->u.it_v1->wTerminalType);
4019 		goto done;
4020 	}
4021 	/*
4022 	 * Otherwise, the class is UAC_OUTPUT.
4023 	 */
4024 	mix->class = UAC_OUTPUT;
4025 done:
4026 	return (terminal_type);
4027 }
4028 
4029 static uint16_t
4030 uaudio20_mixer_determine_class(const struct uaudio_terminal_node *iot,
4031     struct uaudio_mixer_node *mix)
4032 {
4033 	uint16_t terminal_type = 0x0000;
4034 	const struct uaudio_terminal_node *input[2];
4035 	const struct uaudio_terminal_node *output[2];
4036 
4037 	input[0] = uaudio_mixer_get_input(iot, 0);
4038 	input[1] = uaudio_mixer_get_input(iot, 1);
4039 
4040 	output[0] = uaudio_mixer_get_output(iot, 0);
4041 	output[1] = uaudio_mixer_get_output(iot, 1);
4042 
4043 	/*
4044 	 * check if there is only
4045 	 * one output terminal:
4046 	 */
4047 	if (output[0] && (!output[1]))
4048 		terminal_type = UGETW(output[0]->u.ot_v2->wTerminalType);
4049 	/*
4050 	 * If the only output terminal is USB,
4051 	 * the class is UAC_RECORD.
4052 	 */
4053 	if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
4054 
4055 		mix->class = UAC_RECORD;
4056 		if (input[0] && (!input[1])) {
4057 			terminal_type =
4058 			    UGETW(input[0]->u.it_v2->wTerminalType);
4059 		} else {
4060 			terminal_type = 0;
4061 		}
4062 		goto done;
4063 	}
4064 	/*
4065 	 * if the unit is connected to just
4066 	 * one input terminal, the
4067 	 * class is UAC_INPUT:
4068 	 */
4069 	if (input[0] && (!input[1])) {
4070 		mix->class = UAC_INPUT;
4071 		terminal_type =
4072 		    UGETW(input[0]->u.it_v2->wTerminalType);
4073 		goto done;
4074 	}
4075 	/*
4076 	 * Otherwise, the class is UAC_OUTPUT.
4077 	 */
4078 	mix->class = UAC_OUTPUT;
4079 done:
4080 	return (terminal_type);
4081 }
4082 
4083 struct uaudio_tt_to_feature {
4084 	uint16_t terminal_type;
4085 	uint16_t feature;
4086 };
4087 
4088 static const struct uaudio_tt_to_feature uaudio_tt_to_feature[] = {
4089 
4090 	{UAT_STREAM, SOUND_MIXER_PCM},
4091 
4092 	{UATI_MICROPHONE, SOUND_MIXER_MIC},
4093 	{UATI_DESKMICROPHONE, SOUND_MIXER_MIC},
4094 	{UATI_PERSONALMICROPHONE, SOUND_MIXER_MIC},
4095 	{UATI_OMNIMICROPHONE, SOUND_MIXER_MIC},
4096 	{UATI_MICROPHONEARRAY, SOUND_MIXER_MIC},
4097 	{UATI_PROCMICROPHONEARR, SOUND_MIXER_MIC},
4098 
4099 	{UATO_SPEAKER, SOUND_MIXER_SPEAKER},
4100 	{UATO_DESKTOPSPEAKER, SOUND_MIXER_SPEAKER},
4101 	{UATO_ROOMSPEAKER, SOUND_MIXER_SPEAKER},
4102 	{UATO_COMMSPEAKER, SOUND_MIXER_SPEAKER},
4103 
4104 	{UATE_ANALOGCONN, SOUND_MIXER_LINE},
4105 	{UATE_LINECONN, SOUND_MIXER_LINE},
4106 	{UATE_LEGACYCONN, SOUND_MIXER_LINE},
4107 
4108 	{UATE_DIGITALAUIFC, SOUND_MIXER_ALTPCM},
4109 	{UATE_SPDIF, SOUND_MIXER_ALTPCM},
4110 	{UATE_1394DA, SOUND_MIXER_ALTPCM},
4111 	{UATE_1394DV, SOUND_MIXER_ALTPCM},
4112 
4113 	{UATF_CDPLAYER, SOUND_MIXER_CD},
4114 
4115 	{UATF_SYNTHESIZER, SOUND_MIXER_SYNTH},
4116 
4117 	{UATF_VIDEODISCAUDIO, SOUND_MIXER_VIDEO},
4118 	{UATF_DVDAUDIO, SOUND_MIXER_VIDEO},
4119 	{UATF_TVTUNERAUDIO, SOUND_MIXER_VIDEO},
4120 
4121 	/* telephony terminal types */
4122 	{UATT_UNDEFINED, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4123 	{UATT_PHONELINE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4124 	{UATT_TELEPHONE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4125 	{UATT_DOWNLINEPHONE, SOUND_MIXER_PHONEIN},	/* SOUND_MIXER_PHONEOUT */
4126 
4127 	{UATF_RADIORECV, SOUND_MIXER_RADIO},
4128 	{UATF_RADIOXMIT, SOUND_MIXER_RADIO},
4129 
4130 	{UAT_UNDEFINED, SOUND_MIXER_VOLUME},
4131 	{UAT_VENDOR, SOUND_MIXER_VOLUME},
4132 	{UATI_UNDEFINED, SOUND_MIXER_VOLUME},
4133 
4134 	/* output terminal types */
4135 	{UATO_UNDEFINED, SOUND_MIXER_VOLUME},
4136 	{UATO_DISPLAYAUDIO, SOUND_MIXER_VOLUME},
4137 	{UATO_SUBWOOFER, SOUND_MIXER_VOLUME},
4138 	{UATO_HEADPHONES, SOUND_MIXER_VOLUME},
4139 
4140 	/* bidir terminal types */
4141 	{UATB_UNDEFINED, SOUND_MIXER_VOLUME},
4142 	{UATB_HANDSET, SOUND_MIXER_VOLUME},
4143 	{UATB_HEADSET, SOUND_MIXER_VOLUME},
4144 	{UATB_SPEAKERPHONE, SOUND_MIXER_VOLUME},
4145 	{UATB_SPEAKERPHONEESUP, SOUND_MIXER_VOLUME},
4146 	{UATB_SPEAKERPHONEECANC, SOUND_MIXER_VOLUME},
4147 
4148 	/* external terminal types */
4149 	{UATE_UNDEFINED, SOUND_MIXER_VOLUME},
4150 
4151 	/* embedded function terminal types */
4152 	{UATF_UNDEFINED, SOUND_MIXER_VOLUME},
4153 	{UATF_CALIBNOISE, SOUND_MIXER_VOLUME},
4154 	{UATF_EQUNOISE, SOUND_MIXER_VOLUME},
4155 	{UATF_DAT, SOUND_MIXER_VOLUME},
4156 	{UATF_DCC, SOUND_MIXER_VOLUME},
4157 	{UATF_MINIDISK, SOUND_MIXER_VOLUME},
4158 	{UATF_ANALOGTAPE, SOUND_MIXER_VOLUME},
4159 	{UATF_PHONOGRAPH, SOUND_MIXER_VOLUME},
4160 	{UATF_VCRAUDIO, SOUND_MIXER_VOLUME},
4161 	{UATF_SATELLITE, SOUND_MIXER_VOLUME},
4162 	{UATF_CABLETUNER, SOUND_MIXER_VOLUME},
4163 	{UATF_DSS, SOUND_MIXER_VOLUME},
4164 	{UATF_MULTITRACK, SOUND_MIXER_VOLUME},
4165 	{0xffff, SOUND_MIXER_VOLUME},
4166 
4167 	/* default */
4168 	{0x0000, SOUND_MIXER_VOLUME},
4169 };
4170 
4171 static uint16_t
4172 uaudio_mixer_feature_name(const struct uaudio_terminal_node *iot,
4173     struct uaudio_mixer_node *mix)
4174 {
4175 	const struct uaudio_tt_to_feature *uat = uaudio_tt_to_feature;
4176 	uint16_t terminal_type = uaudio_mixer_determine_class(iot, mix);
4177 
4178 	if ((mix->class == UAC_RECORD) && (terminal_type == 0)) {
4179 		return (SOUND_MIXER_IMIX);
4180 	}
4181 	while (uat->terminal_type) {
4182 		if (uat->terminal_type == terminal_type) {
4183 			break;
4184 		}
4185 		uat++;
4186 	}
4187 
4188 	DPRINTF("terminal_type=0x%04x -> %d\n",
4189 	    terminal_type, uat->feature);
4190 
4191 	return (uat->feature);
4192 }
4193 
4194 static uint16_t
4195 uaudio20_mixer_feature_name(const struct uaudio_terminal_node *iot,
4196     struct uaudio_mixer_node *mix)
4197 {
4198 	const struct uaudio_tt_to_feature *uat;
4199 	uint16_t terminal_type = uaudio20_mixer_determine_class(iot, mix);
4200 
4201 	if ((mix->class == UAC_RECORD) && (terminal_type == 0))
4202 		return (SOUND_MIXER_IMIX);
4203 
4204 	for (uat = uaudio_tt_to_feature; uat->terminal_type != 0; uat++) {
4205 		if (uat->terminal_type == terminal_type)
4206 			break;
4207 	}
4208 
4209 	DPRINTF("terminal_type=0x%04x -> %d\n",
4210 	    terminal_type, uat->feature);
4211 
4212 	return (uat->feature);
4213 }
4214 
4215 static const struct uaudio_terminal_node *
4216 uaudio_mixer_get_input(const struct uaudio_terminal_node *iot, uint8_t i)
4217 {
4218 	struct uaudio_terminal_node *root = iot->root;
4219 	uint8_t n;
4220 
4221 	n = iot->usr.id_max;
4222 	do {
4223 		if (iot->usr.bit_input[n / 8] & (1 << (n % 8))) {
4224 			if (!i--)
4225 				return (root + n);
4226 		}
4227 	} while (n--);
4228 
4229 	return (NULL);
4230 }
4231 
4232 static const struct uaudio_terminal_node *
4233 uaudio_mixer_get_output(const struct uaudio_terminal_node *iot, uint8_t i)
4234 {
4235 	struct uaudio_terminal_node *root = iot->root;
4236 	uint8_t n;
4237 
4238 	n = iot->usr.id_max;
4239 	do {
4240 		if (iot->usr.bit_output[n / 8] & (1 << (n % 8))) {
4241 			if (!i--)
4242 				return (root + n);
4243 		}
4244 	} while (n--);
4245 
4246 	return (NULL);
4247 }
4248 
4249 static void
4250 uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *root,
4251     const uint8_t *p_id, uint8_t n_id,
4252     struct uaudio_search_result *info)
4253 {
4254 	struct uaudio_terminal_node *iot;
4255 	uint8_t n;
4256 	uint8_t i;
4257 	uint8_t is_last;
4258 
4259 top:
4260 	for (n = 0; n < n_id; n++) {
4261 
4262 		i = p_id[n];
4263 
4264 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4265 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4266 			return;
4267 		}
4268 
4269 		info->recurse_level++;
4270 
4271 		iot = (root + i);
4272 
4273 		if (iot->u.desc == NULL)
4274 			continue;
4275 
4276 		is_last = ((n + 1) == n_id);
4277 
4278 		switch (iot->u.desc->bDescriptorSubtype) {
4279 		case UDESCSUB_AC_INPUT:
4280 			info->bit_input[i / 8] |= (1 << (i % 8));
4281 			break;
4282 
4283 		case UDESCSUB_AC_FEATURE:
4284 			if (is_last) {
4285 				p_id = &iot->u.fu_v1->bSourceId;
4286 				n_id = 1;
4287 				goto top;
4288 			}
4289 			uaudio_mixer_find_inputs_sub(
4290 			    root, &iot->u.fu_v1->bSourceId, 1, info);
4291 			break;
4292 
4293 		case UDESCSUB_AC_OUTPUT:
4294 			if (is_last) {
4295 				p_id = &iot->u.ot_v1->bSourceId;
4296 				n_id = 1;
4297 				goto top;
4298 			}
4299 			uaudio_mixer_find_inputs_sub(
4300 			    root, &iot->u.ot_v1->bSourceId, 1, info);
4301 			break;
4302 
4303 		case UDESCSUB_AC_MIXER:
4304 			if (is_last) {
4305 				p_id = iot->u.mu_v1->baSourceId;
4306 				n_id = iot->u.mu_v1->bNrInPins;
4307 				goto top;
4308 			}
4309 			uaudio_mixer_find_inputs_sub(
4310 			    root, iot->u.mu_v1->baSourceId,
4311 			    iot->u.mu_v1->bNrInPins, info);
4312 			break;
4313 
4314 		case UDESCSUB_AC_SELECTOR:
4315 			if (is_last) {
4316 				p_id = iot->u.su_v1->baSourceId;
4317 				n_id = iot->u.su_v1->bNrInPins;
4318 				goto top;
4319 			}
4320 			uaudio_mixer_find_inputs_sub(
4321 			    root, iot->u.su_v1->baSourceId,
4322 			    iot->u.su_v1->bNrInPins, info);
4323 			break;
4324 
4325 		case UDESCSUB_AC_PROCESSING:
4326 			if (is_last) {
4327 				p_id = iot->u.pu_v1->baSourceId;
4328 				n_id = iot->u.pu_v1->bNrInPins;
4329 				goto top;
4330 			}
4331 			uaudio_mixer_find_inputs_sub(
4332 			    root, iot->u.pu_v1->baSourceId,
4333 			    iot->u.pu_v1->bNrInPins, info);
4334 			break;
4335 
4336 		case UDESCSUB_AC_EXTENSION:
4337 			if (is_last) {
4338 				p_id = iot->u.eu_v1->baSourceId;
4339 				n_id = iot->u.eu_v1->bNrInPins;
4340 				goto top;
4341 			}
4342 			uaudio_mixer_find_inputs_sub(
4343 			    root, iot->u.eu_v1->baSourceId,
4344 			    iot->u.eu_v1->bNrInPins, info);
4345 			break;
4346 
4347 		default:
4348 			break;
4349 		}
4350 	}
4351 }
4352 
4353 static void
4354 uaudio20_mixer_find_inputs_sub(struct uaudio_terminal_node *root,
4355     const uint8_t *p_id, uint8_t n_id,
4356     struct uaudio_search_result *info)
4357 {
4358 	struct uaudio_terminal_node *iot;
4359 	uint8_t n;
4360 	uint8_t i;
4361 	uint8_t is_last;
4362 
4363 top:
4364 	for (n = 0; n < n_id; n++) {
4365 
4366 		i = p_id[n];
4367 
4368 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4369 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4370 			return;
4371 		}
4372 
4373 		info->recurse_level++;
4374 
4375 		iot = (root + i);
4376 
4377 		if (iot->u.desc == NULL)
4378 			continue;
4379 
4380 		is_last = ((n + 1) == n_id);
4381 
4382 		switch (iot->u.desc->bDescriptorSubtype) {
4383 		case UDESCSUB_AC_INPUT:
4384 			info->bit_input[i / 8] |= (1 << (i % 8));
4385 			break;
4386 
4387 		case UDESCSUB_AC_OUTPUT:
4388 			if (is_last) {
4389 				p_id = &iot->u.ot_v2->bSourceId;
4390 				n_id = 1;
4391 				goto top;
4392 			}
4393 			uaudio20_mixer_find_inputs_sub(
4394 			    root, &iot->u.ot_v2->bSourceId, 1, info);
4395 			break;
4396 
4397 		case UDESCSUB_AC_MIXER:
4398 			if (is_last) {
4399 				p_id = iot->u.mu_v2->baSourceId;
4400 				n_id = iot->u.mu_v2->bNrInPins;
4401 				goto top;
4402 			}
4403 			uaudio20_mixer_find_inputs_sub(
4404 			    root, iot->u.mu_v2->baSourceId,
4405 			    iot->u.mu_v2->bNrInPins, info);
4406 			break;
4407 
4408 		case UDESCSUB_AC_SELECTOR:
4409 			if (is_last) {
4410 				p_id = iot->u.su_v2->baSourceId;
4411 				n_id = iot->u.su_v2->bNrInPins;
4412 				goto top;
4413 			}
4414 			uaudio20_mixer_find_inputs_sub(
4415 			    root, iot->u.su_v2->baSourceId,
4416 			    iot->u.su_v2->bNrInPins, info);
4417 			break;
4418 
4419 		case UDESCSUB_AC_SAMPLE_RT:
4420 			if (is_last) {
4421 				p_id = &iot->u.ru_v2->bSourceId;
4422 				n_id = 1;
4423 				goto top;
4424 			}
4425 			uaudio20_mixer_find_inputs_sub(
4426 			    root, &iot->u.ru_v2->bSourceId,
4427 			    1, info);
4428 			break;
4429 
4430 		case UDESCSUB_AC_EFFECT:
4431 			if (is_last) {
4432 				p_id = &iot->u.ef_v2->bSourceId;
4433 				n_id = 1;
4434 				goto top;
4435 			}
4436 			uaudio20_mixer_find_inputs_sub(
4437 			    root, &iot->u.ef_v2->bSourceId,
4438 			    1, info);
4439 			break;
4440 
4441 		case UDESCSUB_AC_FEATURE:
4442 			if (is_last) {
4443 				p_id = &iot->u.fu_v2->bSourceId;
4444 				n_id = 1;
4445 				goto top;
4446 			}
4447 			uaudio20_mixer_find_inputs_sub(
4448 			    root, &iot->u.fu_v2->bSourceId, 1, info);
4449 			break;
4450 
4451 		case UDESCSUB_AC_PROCESSING_V2:
4452 			if (is_last) {
4453 				p_id = iot->u.pu_v2->baSourceId;
4454 				n_id = iot->u.pu_v2->bNrInPins;
4455 				goto top;
4456 			}
4457 			uaudio20_mixer_find_inputs_sub(
4458 			    root, iot->u.pu_v2->baSourceId,
4459 			    iot->u.pu_v2->bNrInPins, info);
4460 			break;
4461 
4462 		case UDESCSUB_AC_EXTENSION_V2:
4463 			if (is_last) {
4464 				p_id = iot->u.eu_v2->baSourceId;
4465 				n_id = iot->u.eu_v2->bNrInPins;
4466 				goto top;
4467 			}
4468 			uaudio20_mixer_find_inputs_sub(
4469 			    root, iot->u.eu_v2->baSourceId,
4470 			    iot->u.eu_v2->bNrInPins, info);
4471 			break;
4472 		default:
4473 			break;
4474 		}
4475 	}
4476 }
4477 
4478 static void
4479 uaudio20_mixer_find_clocks_sub(struct uaudio_terminal_node *root,
4480     const uint8_t *p_id, uint8_t n_id,
4481     struct uaudio_search_result *info)
4482 {
4483 	struct uaudio_terminal_node *iot;
4484 	uint8_t n;
4485 	uint8_t i;
4486 	uint8_t is_last;
4487 	uint8_t id;
4488 
4489 top:
4490 	for (n = 0; n < n_id; n++) {
4491 
4492 		i = p_id[n];
4493 
4494 		if (info->recurse_level == UAUDIO_RECURSE_LIMIT) {
4495 			DPRINTF("avoided going into a circle at id=%d!\n", i);
4496 			return;
4497 		}
4498 
4499 		info->recurse_level++;
4500 
4501 		iot = (root + i);
4502 
4503 		if (iot->u.desc == NULL)
4504 			continue;
4505 
4506 		is_last = ((n + 1) == n_id);
4507 
4508 		switch (iot->u.desc->bDescriptorSubtype) {
4509 		case UDESCSUB_AC_INPUT:
4510 			info->is_input = 1;
4511 			if (is_last) {
4512 				p_id = &iot->u.it_v2->bCSourceId;
4513 				n_id = 1;
4514 				goto top;
4515 			}
4516 			uaudio20_mixer_find_clocks_sub(root,
4517 			    &iot->u.it_v2->bCSourceId, 1, info);
4518 			break;
4519 
4520 		case UDESCSUB_AC_OUTPUT:
4521 			info->is_input = 0;
4522 			if (is_last) {
4523 				p_id = &iot->u.ot_v2->bCSourceId;
4524 				n_id = 1;
4525 				goto top;
4526 			}
4527 			uaudio20_mixer_find_clocks_sub(root,
4528 			    &iot->u.ot_v2->bCSourceId, 1, info);
4529 			break;
4530 
4531 		case UDESCSUB_AC_CLOCK_SEL:
4532 			if (is_last) {
4533 				p_id = iot->u.csel_v2->baCSourceId;
4534 				n_id = iot->u.csel_v2->bNrInPins;
4535 				goto top;
4536 			}
4537 			uaudio20_mixer_find_clocks_sub(root,
4538 			    iot->u.csel_v2->baCSourceId,
4539 			    iot->u.csel_v2->bNrInPins, info);
4540 			break;
4541 
4542 		case UDESCSUB_AC_CLOCK_MUL:
4543 			if (is_last) {
4544 				p_id = &iot->u.cmul_v2->bCSourceId;
4545 				n_id = 1;
4546 				goto top;
4547 			}
4548 			uaudio20_mixer_find_clocks_sub(root,
4549 			    &iot->u.cmul_v2->bCSourceId,
4550 			    1, info);
4551 			break;
4552 
4553 		case UDESCSUB_AC_CLOCK_SRC:
4554 
4555 			id = iot->u.csrc_v2->bClockId;
4556 
4557 			switch (info->is_input) {
4558 			case 0:
4559 				info->bit_output[id / 8] |= (1 << (id % 8));
4560 				break;
4561 			case 1:
4562 				info->bit_input[id / 8] |= (1 << (id % 8));
4563 				break;
4564 			default:
4565 				break;
4566 			}
4567 			break;
4568 
4569 		default:
4570 			break;
4571 		}
4572 	}
4573 }
4574 
4575 static void
4576 uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *root, uint8_t id,
4577     uint8_t n_id, struct uaudio_search_result *info)
4578 {
4579 	struct uaudio_terminal_node *iot = (root + id);
4580 	uint8_t j;
4581 
4582 	j = n_id;
4583 	do {
4584 		if ((j != id) && ((root + j)->u.desc) &&
4585 		    ((root + j)->u.desc->bDescriptorSubtype == UDESCSUB_AC_OUTPUT)) {
4586 
4587 			/*
4588 			 * "j" (output) <--- virtual wire <--- "id" (input)
4589 			 *
4590 			 * if "j" has "id" on the input, then "id" have "j" on
4591 			 * the output, because they are connected:
4592 			 */
4593 			if ((root + j)->usr.bit_input[id / 8] & (1 << (id % 8))) {
4594 				iot->usr.bit_output[j / 8] |= (1 << (j % 8));
4595 			}
4596 		}
4597 	} while (j--);
4598 }
4599 
4600 static void
4601 uaudio_mixer_fill_info(struct uaudio_softc *sc,
4602     struct usb_device *udev, void *desc)
4603 {
4604 	const struct usb_audio_control_descriptor *acdp;
4605 	struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
4606 	const struct usb_descriptor *dp;
4607 	const struct usb_audio_unit *au;
4608 	struct uaudio_terminal_node *iot = NULL;
4609 	uint16_t wTotalLen;
4610 	uint8_t ID_max = 0;		/* inclusive */
4611 	uint8_t i;
4612 
4613 	desc = usb_desc_foreach(cd, desc);
4614 
4615 	if (desc == NULL) {
4616 		DPRINTF("no Audio Control header\n");
4617 		goto done;
4618 	}
4619 	acdp = desc;
4620 
4621 	if ((acdp->bLength < sizeof(*acdp)) ||
4622 	    (acdp->bDescriptorType != UDESC_CS_INTERFACE) ||
4623 	    (acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)) {
4624 		DPRINTF("invalid Audio Control header\n");
4625 		goto done;
4626 	}
4627 	/* "wTotalLen" is allowed to be corrupt */
4628 	wTotalLen = UGETW(acdp->wTotalLength) - acdp->bLength;
4629 
4630 	/* get USB audio revision */
4631 	sc->sc_audio_rev = UGETW(acdp->bcdADC);
4632 
4633 	DPRINTFN(3, "found AC header, vers=%03x, len=%d\n",
4634 	    sc->sc_audio_rev, wTotalLen);
4635 
4636 	iot = malloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP,
4637 	    M_WAITOK | M_ZERO);
4638 
4639 	if (iot == NULL) {
4640 		DPRINTF("no memory!\n");
4641 		goto done;
4642 	}
4643 	while ((desc = usb_desc_foreach(cd, desc))) {
4644 
4645 		dp = desc;
4646 
4647 		if (dp->bLength > wTotalLen) {
4648 			break;
4649 		} else {
4650 			wTotalLen -= dp->bLength;
4651 		}
4652 
4653 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30)
4654 			au = NULL;
4655 		else if (sc->sc_audio_rev >= UAUDIO_VERSION_20)
4656 			au = uaudio20_mixer_verify_desc(dp, 0);
4657 		else
4658 			au = uaudio_mixer_verify_desc(dp, 0);
4659 
4660 		if (au) {
4661 			iot[au->bUnitId].u.desc = (const void *)au;
4662 			if (au->bUnitId > ID_max)
4663 				ID_max = au->bUnitId;
4664 		}
4665 	}
4666 
4667 	DPRINTF("Maximum ID=%d\n", ID_max);
4668 
4669 	/*
4670 	 * determine sourcing inputs for
4671 	 * all nodes in the tree:
4672 	 */
4673 	i = ID_max;
4674 	do {
4675 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4676 			/* FALLTHROUGH */
4677 		} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4678 			uaudio20_mixer_find_inputs_sub(iot,
4679 			    &i, 1, &((iot + i)->usr));
4680 
4681 			sc->sc_mixer_clocks.is_input = 255;
4682 			sc->sc_mixer_clocks.recurse_level = 0;
4683 
4684 			uaudio20_mixer_find_clocks_sub(iot,
4685 			    &i, 1, &sc->sc_mixer_clocks);
4686 		} else {
4687 			uaudio_mixer_find_inputs_sub(iot,
4688 			    &i, 1, &((iot + i)->usr));
4689 		}
4690 	} while (i--);
4691 
4692 	/*
4693 	 * determine outputs for
4694 	 * all nodes in the tree:
4695 	 */
4696 	i = ID_max;
4697 	do {
4698 		uaudio_mixer_find_outputs_sub(iot,
4699 		    i, ID_max, &((iot + i)->usr));
4700 	} while (i--);
4701 
4702 	/* set "id_max" and "root" */
4703 
4704 	i = ID_max;
4705 	do {
4706 		(iot + i)->usr.id_max = ID_max;
4707 		(iot + i)->root = iot;
4708 	} while (i--);
4709 
4710 	/*
4711 	 * Scan the config to create a linked list of "mixer" nodes:
4712 	 */
4713 
4714 	i = ID_max;
4715 	do {
4716 		dp = iot[i].u.desc;
4717 
4718 		if (dp == NULL)
4719 			continue;
4720 
4721 		DPRINTFN(11, "id=%d subtype=%d\n",
4722 		    i, dp->bDescriptorSubtype);
4723 
4724 		if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4725 			continue;
4726 		} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4727 
4728 			switch (dp->bDescriptorSubtype) {
4729 			case UDESCSUB_AC_HEADER:
4730 				DPRINTF("unexpected AC header\n");
4731 				break;
4732 
4733 			case UDESCSUB_AC_INPUT:
4734 			case UDESCSUB_AC_OUTPUT:
4735 			case UDESCSUB_AC_PROCESSING_V2:
4736 			case UDESCSUB_AC_EXTENSION_V2:
4737 			case UDESCSUB_AC_EFFECT:
4738 			case UDESCSUB_AC_CLOCK_SRC:
4739 			case UDESCSUB_AC_CLOCK_SEL:
4740 			case UDESCSUB_AC_CLOCK_MUL:
4741 			case UDESCSUB_AC_SAMPLE_RT:
4742 				break;
4743 
4744 			case UDESCSUB_AC_MIXER:
4745 				uaudio20_mixer_add_mixer(sc, iot, i);
4746 				break;
4747 
4748 			case UDESCSUB_AC_SELECTOR:
4749 				uaudio20_mixer_add_selector(sc, iot, i);
4750 				break;
4751 
4752 			case UDESCSUB_AC_FEATURE:
4753 				uaudio20_mixer_add_feature(sc, iot, i);
4754 				break;
4755 
4756 			default:
4757 				DPRINTF("bad AC desc subtype=0x%02x\n",
4758 				    dp->bDescriptorSubtype);
4759 				break;
4760 			}
4761 			continue;
4762 		}
4763 
4764 		switch (dp->bDescriptorSubtype) {
4765 		case UDESCSUB_AC_HEADER:
4766 			DPRINTF("unexpected AC header\n");
4767 			break;
4768 
4769 		case UDESCSUB_AC_INPUT:
4770 		case UDESCSUB_AC_OUTPUT:
4771 			break;
4772 
4773 		case UDESCSUB_AC_MIXER:
4774 			uaudio_mixer_add_mixer(sc, iot, i);
4775 			break;
4776 
4777 		case UDESCSUB_AC_SELECTOR:
4778 			uaudio_mixer_add_selector(sc, iot, i);
4779 			break;
4780 
4781 		case UDESCSUB_AC_FEATURE:
4782 			uaudio_mixer_add_feature(sc, iot, i);
4783 			break;
4784 
4785 		case UDESCSUB_AC_PROCESSING:
4786 			uaudio_mixer_add_processing(sc, iot, i);
4787 			break;
4788 
4789 		case UDESCSUB_AC_EXTENSION:
4790 			uaudio_mixer_add_extension(sc, iot, i);
4791 			break;
4792 
4793 		default:
4794 			DPRINTF("bad AC desc subtype=0x%02x\n",
4795 			    dp->bDescriptorSubtype);
4796 			break;
4797 		}
4798 
4799 	} while (i--);
4800 
4801 done:
4802 	free(iot, M_TEMP);
4803 }
4804 
4805 static int
4806 uaudio_mixer_get(struct usb_device *udev, uint16_t audio_rev,
4807     uint8_t what, struct uaudio_mixer_node *mc)
4808 {
4809 	struct usb_device_request req;
4810 	int val;
4811 	uint8_t data[2 + (2 * 3)];
4812 	usb_error_t err;
4813 
4814 	if (mc->wValue[0] == -1)
4815 		return (0);
4816 
4817 	if (audio_rev >= UAUDIO_VERSION_30)
4818 		return (0);
4819 	else if (audio_rev >= UAUDIO_VERSION_20) {
4820 		if (what == GET_CUR) {
4821 			req.bRequest = UA20_CS_CUR;
4822 			USETW(req.wLength, 2);
4823 		} else {
4824 			req.bRequest = UA20_CS_RANGE;
4825 			USETW(req.wLength, 8);
4826 		}
4827 	} else {
4828 		uint16_t len = MIX_SIZE(mc->type);
4829 
4830 		req.bRequest = what;
4831 		USETW(req.wLength, len);
4832 	}
4833 
4834 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
4835 	USETW(req.wValue, mc->wValue[0]);
4836 	USETW(req.wIndex, mc->wIndex);
4837 
4838 	memset(data, 0, sizeof(data));
4839 
4840 	err = usbd_do_request(udev, NULL, &req, data);
4841 	if (err) {
4842 		DPRINTF("err=%s\n", usbd_errstr(err));
4843 		return (0);
4844 	}
4845 
4846 	if (audio_rev >= UAUDIO_VERSION_30) {
4847 		val = 0;
4848 	} else if (audio_rev >= UAUDIO_VERSION_20) {
4849 		switch (what) {
4850 		case GET_CUR:
4851 			val = (data[0] | (data[1] << 8));
4852 			break;
4853 		case GET_MIN:
4854 			val = (data[2] | (data[3] << 8));
4855 			break;
4856 		case GET_MAX:
4857 			val = (data[4] | (data[5] << 8));
4858 			break;
4859 		case GET_RES:
4860 			val = (data[6] | (data[7] << 8));
4861 			break;
4862 		default:
4863 			val = 0;
4864 			break;
4865 		}
4866 	} else {
4867 		val = (data[0] | (data[1] << 8));
4868 	}
4869 
4870 	if (what == GET_CUR || what == GET_MIN || what == GET_MAX)
4871 		val = uaudio_mixer_signext(mc->type, val);
4872 
4873 	DPRINTFN(3, "val=%d\n", val);
4874 
4875 	return (val);
4876 }
4877 
4878 static void
4879 uaudio_mixer_write_cfg_callback(struct usb_xfer *xfer, usb_error_t error)
4880 {
4881 	struct usb_device_request req;
4882 	struct uaudio_softc *sc = usbd_xfer_softc(xfer);
4883 	struct uaudio_mixer_node *mc = sc->sc_mixer_curr;
4884 	struct usb_page_cache *pc;
4885 	uint16_t len;
4886 	uint8_t repeat = 1;
4887 	uint8_t update;
4888 	uint8_t chan;
4889 	uint8_t buf[2];
4890 
4891 	DPRINTF("\n");
4892 
4893 	switch (USB_GET_STATE(xfer)) {
4894 	case USB_ST_TRANSFERRED:
4895 tr_transferred:
4896 	case USB_ST_SETUP:
4897 tr_setup:
4898 
4899 		if (mc == NULL) {
4900 			mc = sc->sc_mixer_root;
4901 			sc->sc_mixer_curr = mc;
4902 			sc->sc_mixer_chan = 0;
4903 			repeat = 0;
4904 		}
4905 		while (mc) {
4906 			while (sc->sc_mixer_chan < mc->nchan) {
4907 
4908 				chan = sc->sc_mixer_chan;
4909 
4910 				sc->sc_mixer_chan++;
4911 
4912 				update = ((mc->update[chan / 8] & (1 << (chan % 8))) &&
4913 				    (mc->wValue[chan] != -1));
4914 
4915 				mc->update[chan / 8] &= ~(1 << (chan % 8));
4916 
4917 				if (update) {
4918 
4919 					req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4920 					USETW(req.wValue, mc->wValue[chan]);
4921 					USETW(req.wIndex, mc->wIndex);
4922 
4923 					if (sc->sc_audio_rev >= UAUDIO_VERSION_30) {
4924 						return;
4925 					} else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) {
4926 						len = 2;
4927 						req.bRequest = UA20_CS_CUR;
4928 						USETW(req.wLength, len);
4929 					} else {
4930 						len = MIX_SIZE(mc->type);
4931 						req.bRequest = SET_CUR;
4932 						USETW(req.wLength, len);
4933 					}
4934 
4935 					buf[0] = (mc->wData[chan] & 0xFF);
4936 					buf[1] = (mc->wData[chan] >> 8) & 0xFF;
4937 
4938 					pc = usbd_xfer_get_frame(xfer, 0);
4939 					usbd_copy_in(pc, 0, &req, sizeof(req));
4940 					pc = usbd_xfer_get_frame(xfer, 1);
4941 					usbd_copy_in(pc, 0, buf, len);
4942 
4943 					usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
4944 					usbd_xfer_set_frame_len(xfer, 1, len);
4945 					usbd_xfer_set_frames(xfer, len ? 2 : 1);
4946 					usbd_transfer_submit(xfer);
4947 					return;
4948 				}
4949 			}
4950 
4951 			mc = mc->next;
4952 			sc->sc_mixer_curr = mc;
4953 			sc->sc_mixer_chan = 0;
4954 		}
4955 
4956 		if (repeat) {
4957 			goto tr_setup;
4958 		}
4959 		break;
4960 
4961 	default:			/* Error */
4962 		DPRINTF("error=%s\n", usbd_errstr(error));
4963 		if (error == USB_ERR_CANCELLED) {
4964 			/* do nothing - we are detaching */
4965 			break;
4966 		}
4967 		goto tr_transferred;
4968 	}
4969 }
4970 
4971 static usb_error_t
4972 uaudio_set_speed(struct usb_device *udev, uint8_t endpt, uint32_t speed)
4973 {
4974 	struct usb_device_request req;
4975 	uint8_t data[3];
4976 
4977 	DPRINTFN(6, "endpt=%d speed=%u\n", endpt, speed);
4978 
4979 	req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
4980 	req.bRequest = SET_CUR;
4981 	USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
4982 	USETW(req.wIndex, endpt);
4983 	USETW(req.wLength, 3);
4984 	data[0] = speed;
4985 	data[1] = speed >> 8;
4986 	data[2] = speed >> 16;
4987 
4988 	return (usbd_do_request(udev, NULL, &req, data));
4989 }
4990 
4991 static usb_error_t
4992 uaudio20_set_speed(struct usb_device *udev, uint8_t iface_no,
4993     uint8_t clockid, uint32_t speed)
4994 {
4995 	struct usb_device_request req;
4996 	uint8_t data[4];
4997 
4998 	DPRINTFN(6, "ifaceno=%d clockid=%d speed=%u\n",
4999 	    iface_no, clockid, speed);
5000 
5001 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
5002 	req.bRequest = UA20_CS_CUR;
5003 	USETW2(req.wValue, UA20_CS_SAM_FREQ_CONTROL, 0);
5004 	USETW2(req.wIndex, clockid, iface_no);
5005 	USETW(req.wLength, 4);
5006 	data[0] = speed;
5007 	data[1] = speed >> 8;
5008 	data[2] = speed >> 16;
5009 	data[3] = speed >> 24;
5010 
5011 	return (usbd_do_request(udev, NULL, &req, data));
5012 }
5013 
5014 static int
5015 uaudio_mixer_signext(uint8_t type, int val)
5016 {
5017 	if (!MIX_UNSIGNED(type)) {
5018 		if (MIX_SIZE(type) == 2) {
5019 			val = (int16_t)val;
5020 		} else {
5021 			val = (int8_t)val;
5022 		}
5023 	}
5024 	return (val);
5025 }
5026 
5027 static int
5028 uaudio_mixer_bsd2value(struct uaudio_mixer_node *mc, int32_t val)
5029 {
5030 	if (mc->type == MIX_ON_OFF) {
5031 		val = (val != 0);
5032 	} else if (mc->type == MIX_SELECTOR) {
5033 		if ((val < mc->minval) ||
5034 		    (val > mc->maxval)) {
5035 			val = mc->minval;
5036 		}
5037 	} else {
5038 
5039 		/* compute actual volume */
5040 		val = (val * mc->mul) / 255;
5041 
5042 		/* add lower offset */
5043 		val = val + mc->minval;
5044 
5045 		/* make sure we don't write a value out of range */
5046 		if (val > mc->maxval)
5047 			val = mc->maxval;
5048 		else if (val < mc->minval)
5049 			val = mc->minval;
5050 	}
5051 
5052 	DPRINTFN(6, "type=0x%03x val=%d min=%d max=%d val=%d\n",
5053 	    mc->type, val, mc->minval, mc->maxval, val);
5054 	return (val);
5055 }
5056 
5057 static void
5058 uaudio_mixer_ctl_set(struct uaudio_softc *sc, struct uaudio_mixer_node *mc,
5059     uint8_t chan, int32_t val)
5060 {
5061 	val = uaudio_mixer_bsd2value(mc, val);
5062 
5063 	mc->update[chan / 8] |= (1 << (chan % 8));
5064 	mc->wData[chan] = val;
5065 
5066 	/* start the transfer, if not already started */
5067 
5068 	usbd_transfer_start(sc->sc_mixer_xfer[0]);
5069 }
5070 
5071 static void
5072 uaudio_mixer_init(struct uaudio_softc *sc)
5073 {
5074 	struct uaudio_mixer_node *mc;
5075 	int32_t i;
5076 
5077 	for (mc = sc->sc_mixer_root; mc;
5078 	    mc = mc->next) {
5079 
5080 		if (mc->ctl != SOUND_MIXER_NRDEVICES) {
5081 			/*
5082 			 * Set device mask bits. See
5083 			 * /usr/include/machine/soundcard.h
5084 			 */
5085 			sc->sc_mix_info |= (1 << mc->ctl);
5086 		}
5087 		if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
5088 		    (mc->type == MIX_SELECTOR)) {
5089 
5090 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5091 				if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES) {
5092 					continue;
5093 				}
5094 				sc->sc_recsrc_info |= 1 << mc->slctrtype[i - 1];
5095 			}
5096 		}
5097 	}
5098 }
5099 
5100 int
5101 uaudio_mixer_init_sub(struct uaudio_softc *sc, struct snd_mixer *m)
5102 {
5103 	DPRINTF("\n");
5104 
5105 	sc->sc_mixer_lock = mixer_get_lock(m);
5106 	sc->sc_mixer_dev = m;
5107 
5108 	if (usbd_transfer_setup(sc->sc_udev, &sc->sc_mixer_iface_index,
5109 	    sc->sc_mixer_xfer, uaudio_mixer_config, 1, sc,
5110 	    sc->sc_mixer_lock)) {
5111 		DPRINTFN(0, "could not allocate USB "
5112 		    "transfer for audio mixer!\n");
5113 		return (ENOMEM);
5114 	}
5115 	if (!(sc->sc_mix_info & SOUND_MASK_VOLUME)) {
5116 		mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM);
5117 		mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE);
5118 	}
5119 	mix_setdevs(m, sc->sc_mix_info);
5120 	mix_setrecdevs(m, sc->sc_recsrc_info);
5121 	return (0);
5122 }
5123 
5124 int
5125 uaudio_mixer_uninit_sub(struct uaudio_softc *sc)
5126 {
5127 	DPRINTF("\n");
5128 
5129 	usbd_transfer_unsetup(sc->sc_mixer_xfer, 1);
5130 
5131 	sc->sc_mixer_lock = NULL;
5132 
5133 	return (0);
5134 }
5135 
5136 void
5137 uaudio_mixer_set(struct uaudio_softc *sc, unsigned type,
5138     unsigned left, unsigned right)
5139 {
5140 	struct uaudio_mixer_node *mc;
5141 	int chan;
5142 
5143 	for (mc = sc->sc_mixer_root; mc != NULL; mc = mc->next) {
5144 
5145 		if (mc->ctl == type) {
5146 			for (chan = 0; chan < mc->nchan; chan++) {
5147 				uaudio_mixer_ctl_set(sc, mc, chan,
5148 				    (int)((chan == 0 ? left : right) *
5149 				    255) / 100);
5150 			}
5151 		}
5152 	}
5153 }
5154 
5155 uint32_t
5156 uaudio_mixer_setrecsrc(struct uaudio_softc *sc, uint32_t src)
5157 {
5158 	struct uaudio_mixer_node *mc;
5159 	uint32_t mask;
5160 	uint32_t temp;
5161 	int32_t i;
5162 
5163 	for (mc = sc->sc_mixer_root; mc;
5164 	    mc = mc->next) {
5165 
5166 		if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
5167 		    (mc->type == MIX_SELECTOR)) {
5168 
5169 			/* compute selector mask */
5170 
5171 			mask = 0;
5172 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5173 				mask |= (1 << mc->slctrtype[i - 1]);
5174 			}
5175 
5176 			temp = mask & src;
5177 			if (temp == 0) {
5178 				continue;
5179 			}
5180 			/* find the first set bit */
5181 			temp = (-temp) & temp;
5182 
5183 			/* update "src" */
5184 			src &= ~mask;
5185 			src |= temp;
5186 
5187 			for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
5188 				if (temp != (1 << mc->slctrtype[i - 1])) {
5189 					continue;
5190 				}
5191 				uaudio_mixer_ctl_set(sc, mc, 0, i);
5192 				break;
5193 			}
5194 		}
5195 	}
5196 	return (src);
5197 }
5198 
5199 /*========================================================================*
5200  * MIDI support routines
5201  *========================================================================*/
5202 
5203 static void
5204 umidi_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
5205 {
5206 	struct umidi_chan *chan = usbd_xfer_softc(xfer);
5207 	struct umidi_sub_chan *sub;
5208 	struct usb_page_cache *pc;
5209 	uint8_t buf[4];
5210 	uint8_t cmd_len;
5211 	uint8_t cn;
5212 	uint16_t pos;
5213 	int actlen;
5214 
5215 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
5216 
5217 	switch (USB_GET_STATE(xfer)) {
5218 	case USB_ST_TRANSFERRED:
5219 
5220 		DPRINTF("actlen=%d bytes\n", actlen);
5221 
5222 		pos = 0;
5223 		pc = usbd_xfer_get_frame(xfer, 0);
5224 
5225 		while (actlen >= 4) {
5226 
5227 			/* copy out the MIDI data */
5228 			usbd_copy_out(pc, pos, buf, 4);
5229 			/* command length */
5230 			cmd_len = umidi_cmd_to_len[buf[0] & 0xF];
5231 			/* cable number */
5232 			cn = buf[0] >> 4;
5233 			/*
5234 			 * Lookup sub-channel. The index is range
5235 			 * checked below.
5236 			 */
5237 			sub = &chan->sub[cn];
5238 
5239 			if ((cmd_len != 0) &&
5240 			    (cn < chan->max_cable) &&
5241 			    (sub->read_open != 0)) {
5242 
5243 				/* Send data to the application */
5244 				usb_fifo_put_data_linear(
5245 				    sub->fifo.fp[USB_FIFO_RX],
5246 				    buf + 1, cmd_len, 1);
5247 			}
5248 			actlen -= 4;
5249 			pos += 4;
5250 		}
5251 
5252 	case USB_ST_SETUP:
5253 		DPRINTF("start\n");
5254 tr_setup:
5255 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
5256 		usbd_transfer_submit(xfer);
5257 		break;
5258 
5259 	default:
5260 		DPRINTF("error=%s\n", usbd_errstr(error));
5261 
5262 		if (error != USB_ERR_CANCELLED) {
5263 			/* try to clear stall first */
5264 			usbd_xfer_set_stall(xfer);
5265 			goto tr_setup;
5266 		}
5267 		break;
5268 	}
5269 }
5270 
5271 /*
5272  * The following statemachine, that converts MIDI commands to
5273  * USB MIDI packets, derives from Linux's usbmidi.c, which
5274  * was written by "Clemens Ladisch":
5275  *
5276  * Returns:
5277  *    0: No command
5278  * Else: Command is complete
5279  */
5280 static uint8_t
5281 umidi_convert_to_usb(struct umidi_sub_chan *sub, uint8_t cn, uint8_t b)
5282 {
5283 	uint8_t p0 = (cn << 4);
5284 
5285 	if (b >= 0xf8) {
5286 		sub->temp_0[0] = p0 | 0x0f;
5287 		sub->temp_0[1] = b;
5288 		sub->temp_0[2] = 0;
5289 		sub->temp_0[3] = 0;
5290 		sub->temp_cmd = sub->temp_0;
5291 		return (1);
5292 
5293 	} else if (b >= 0xf0) {
5294 		switch (b) {
5295 		case 0xf0:		/* system exclusive begin */
5296 			sub->temp_1[1] = b;
5297 			sub->state = UMIDI_ST_SYSEX_1;
5298 			break;
5299 		case 0xf1:		/* MIDI time code */
5300 		case 0xf3:		/* song select */
5301 			sub->temp_1[1] = b;
5302 			sub->state = UMIDI_ST_1PARAM;
5303 			break;
5304 		case 0xf2:		/* song position pointer */
5305 			sub->temp_1[1] = b;
5306 			sub->state = UMIDI_ST_2PARAM_1;
5307 			break;
5308 		case 0xf4:		/* unknown */
5309 		case 0xf5:		/* unknown */
5310 			sub->state = UMIDI_ST_UNKNOWN;
5311 			break;
5312 		case 0xf6:		/* tune request */
5313 			sub->temp_1[0] = p0 | 0x05;
5314 			sub->temp_1[1] = 0xf6;
5315 			sub->temp_1[2] = 0;
5316 			sub->temp_1[3] = 0;
5317 			sub->temp_cmd = sub->temp_1;
5318 			sub->state = UMIDI_ST_UNKNOWN;
5319 			return (1);
5320 
5321 		case 0xf7:		/* system exclusive end */
5322 			switch (sub->state) {
5323 			case UMIDI_ST_SYSEX_0:
5324 				sub->temp_1[0] = p0 | 0x05;
5325 				sub->temp_1[1] = 0xf7;
5326 				sub->temp_1[2] = 0;
5327 				sub->temp_1[3] = 0;
5328 				sub->temp_cmd = sub->temp_1;
5329 				sub->state = UMIDI_ST_UNKNOWN;
5330 				return (1);
5331 			case UMIDI_ST_SYSEX_1:
5332 				sub->temp_1[0] = p0 | 0x06;
5333 				sub->temp_1[2] = 0xf7;
5334 				sub->temp_1[3] = 0;
5335 				sub->temp_cmd = sub->temp_1;
5336 				sub->state = UMIDI_ST_UNKNOWN;
5337 				return (1);
5338 			case UMIDI_ST_SYSEX_2:
5339 				sub->temp_1[0] = p0 | 0x07;
5340 				sub->temp_1[3] = 0xf7;
5341 				sub->temp_cmd = sub->temp_1;
5342 				sub->state = UMIDI_ST_UNKNOWN;
5343 				return (1);
5344 			}
5345 			sub->state = UMIDI_ST_UNKNOWN;
5346 			break;
5347 		}
5348 	} else if (b >= 0x80) {
5349 		sub->temp_1[1] = b;
5350 		if ((b >= 0xc0) && (b <= 0xdf)) {
5351 			sub->state = UMIDI_ST_1PARAM;
5352 		} else {
5353 			sub->state = UMIDI_ST_2PARAM_1;
5354 		}
5355 	} else {			/* b < 0x80 */
5356 		switch (sub->state) {
5357 		case UMIDI_ST_1PARAM:
5358 			if (sub->temp_1[1] < 0xf0) {
5359 				p0 |= sub->temp_1[1] >> 4;
5360 			} else {
5361 				p0 |= 0x02;
5362 				sub->state = UMIDI_ST_UNKNOWN;
5363 			}
5364 			sub->temp_1[0] = p0;
5365 			sub->temp_1[2] = b;
5366 			sub->temp_1[3] = 0;
5367 			sub->temp_cmd = sub->temp_1;
5368 			return (1);
5369 		case UMIDI_ST_2PARAM_1:
5370 			sub->temp_1[2] = b;
5371 			sub->state = UMIDI_ST_2PARAM_2;
5372 			break;
5373 		case UMIDI_ST_2PARAM_2:
5374 			if (sub->temp_1[1] < 0xf0) {
5375 				p0 |= sub->temp_1[1] >> 4;
5376 				sub->state = UMIDI_ST_2PARAM_1;
5377 			} else {
5378 				p0 |= 0x03;
5379 				sub->state = UMIDI_ST_UNKNOWN;
5380 			}
5381 			sub->temp_1[0] = p0;
5382 			sub->temp_1[3] = b;
5383 			sub->temp_cmd = sub->temp_1;
5384 			return (1);
5385 		case UMIDI_ST_SYSEX_0:
5386 			sub->temp_1[1] = b;
5387 			sub->state = UMIDI_ST_SYSEX_1;
5388 			break;
5389 		case UMIDI_ST_SYSEX_1:
5390 			sub->temp_1[2] = b;
5391 			sub->state = UMIDI_ST_SYSEX_2;
5392 			break;
5393 		case UMIDI_ST_SYSEX_2:
5394 			sub->temp_1[0] = p0 | 0x04;
5395 			sub->temp_1[3] = b;
5396 			sub->temp_cmd = sub->temp_1;
5397 			sub->state = UMIDI_ST_SYSEX_0;
5398 			return (1);
5399 		default:
5400 			break;
5401 		}
5402 	}
5403 	return (0);
5404 }
5405 
5406 static void
5407 umidi_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
5408 {
5409 	struct umidi_chan *chan = usbd_xfer_softc(xfer);
5410 	struct umidi_sub_chan *sub;
5411 	struct usb_page_cache *pc;
5412 	uint32_t actlen;
5413 	uint16_t nframes;
5414 	uint8_t buf;
5415 	uint8_t start_cable;
5416 	uint8_t tr_any;
5417 	int len;
5418 
5419 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
5420 
5421 	/*
5422 	 * NOTE: Some MIDI devices only accept 4 bytes of data per
5423 	 * short terminated USB transfer.
5424 	 */
5425 	switch (USB_GET_STATE(xfer)) {
5426 	case USB_ST_TRANSFERRED:
5427 		DPRINTF("actlen=%d bytes\n", len);
5428 
5429 	case USB_ST_SETUP:
5430 tr_setup:
5431 		DPRINTF("start\n");
5432 
5433 		nframes = 0;	/* reset */
5434 		start_cable = chan->curr_cable;
5435 		tr_any = 0;
5436 		pc = usbd_xfer_get_frame(xfer, 0);
5437 
5438 		while (1) {
5439 
5440 			/* round robin de-queueing */
5441 
5442 			sub = &chan->sub[chan->curr_cable];
5443 
5444 			if (sub->write_open) {
5445 				usb_fifo_get_data_linear(sub->fifo.fp[USB_FIFO_TX],
5446 				    &buf, 1, &actlen, 0);
5447 			} else {
5448 				actlen = 0;
5449 			}
5450 
5451 			if (actlen) {
5452 
5453 				tr_any = 1;
5454 
5455 				DPRINTF("byte=0x%02x from FIFO %u\n", buf,
5456 				    (unsigned int)chan->curr_cable);
5457 
5458 				if (umidi_convert_to_usb(sub, chan->curr_cable, buf)) {
5459 
5460 					DPRINTF("sub=0x%02x 0x%02x 0x%02x 0x%02x\n",
5461 					    sub->temp_cmd[0], sub->temp_cmd[1],
5462 					    sub->temp_cmd[2], sub->temp_cmd[3]);
5463 
5464 					usbd_copy_in(pc, nframes * 4, sub->temp_cmd, 4);
5465 
5466 					nframes++;
5467 
5468 					if ((nframes >= UMIDI_TX_FRAMES) || (chan->single_command != 0))
5469 						break;
5470 				} else {
5471 					continue;
5472 				}
5473 			}
5474 
5475 			chan->curr_cable++;
5476 			if (chan->curr_cable >= chan->max_cable)
5477 				chan->curr_cable = 0;
5478 
5479 			if (chan->curr_cable == start_cable) {
5480 				if (tr_any == 0)
5481 					break;
5482 				tr_any = 0;
5483 			}
5484 		}
5485 
5486 		if (nframes != 0) {
5487 			DPRINTF("Transferring %d frames\n", (int)nframes);
5488 			usbd_xfer_set_frame_len(xfer, 0, 4 * nframes);
5489 			usbd_transfer_submit(xfer);
5490 		}
5491 		break;
5492 
5493 	default:			/* Error */
5494 
5495 		DPRINTF("error=%s\n", usbd_errstr(error));
5496 
5497 		if (error != USB_ERR_CANCELLED) {
5498 			/* try to clear stall first */
5499 			usbd_xfer_set_stall(xfer);
5500 			goto tr_setup;
5501 		}
5502 		break;
5503 	}
5504 }
5505 
5506 static struct umidi_sub_chan *
5507 umidi_sub_by_fifo(struct usb_fifo *fifo)
5508 {
5509 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5510 	struct umidi_sub_chan *sub;
5511 	uint32_t n;
5512 
5513 	for (n = 0; n < UMIDI_CABLES_MAX; n++) {
5514 		sub = &chan->sub[n];
5515 		if ((sub->fifo.fp[USB_FIFO_RX] == fifo) ||
5516 		    (sub->fifo.fp[USB_FIFO_TX] == fifo)) {
5517 			return (sub);
5518 		}
5519 	}
5520 
5521 	panic("%s:%d cannot find usb_fifo!\n",
5522 	    __FILE__, __LINE__);
5523 
5524 	return (NULL);
5525 }
5526 
5527 static void
5528 umidi_start_read(struct usb_fifo *fifo)
5529 {
5530 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5531 
5532 	usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
5533 }
5534 
5535 static void
5536 umidi_stop_read(struct usb_fifo *fifo)
5537 {
5538 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5539 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5540 
5541 	DPRINTF("\n");
5542 
5543 	sub->read_open = 0;
5544 
5545 	if (--(chan->read_open_refcount) == 0) {
5546 		/*
5547 		 * XXX don't stop the read transfer here, hence that causes
5548 		 * problems with some MIDI adapters
5549 		 */
5550 		DPRINTF("(stopping read transfer)\n");
5551 	}
5552 }
5553 
5554 static void
5555 umidi_start_write(struct usb_fifo *fifo)
5556 {
5557 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5558 
5559 	usbd_transfer_start(chan->xfer[UMIDI_TX_TRANSFER]);
5560 }
5561 
5562 static void
5563 umidi_stop_write(struct usb_fifo *fifo)
5564 {
5565 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5566 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5567 
5568 	DPRINTF("\n");
5569 
5570 	sub->write_open = 0;
5571 
5572 	if (--(chan->write_open_refcount) == 0) {
5573 		DPRINTF("(stopping write transfer)\n");
5574 		usbd_transfer_stop(chan->xfer[UMIDI_TX_TRANSFER]);
5575 	}
5576 }
5577 
5578 static int
5579 umidi_open(struct usb_fifo *fifo, int fflags)
5580 {
5581 	struct umidi_chan *chan = usb_fifo_softc(fifo);
5582 	struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
5583 
5584 	if (fflags & FREAD) {
5585 		if (usb_fifo_alloc_buffer(fifo, 4, (1024 / 4))) {
5586 			return (ENOMEM);
5587 		}
5588 		mtx_lock(&chan->mtx);
5589 		chan->read_open_refcount++;
5590 		sub->read_open = 1;
5591 		mtx_unlock(&chan->mtx);
5592 	}
5593 	if (fflags & FWRITE) {
5594 		if (usb_fifo_alloc_buffer(fifo, 32, (1024 / 32))) {
5595 			return (ENOMEM);
5596 		}
5597 		/* clear stall first */
5598 		mtx_lock(&chan->mtx);
5599 		chan->write_open_refcount++;
5600 		sub->write_open = 1;
5601 
5602 		/* reset */
5603 		sub->state = UMIDI_ST_UNKNOWN;
5604 		mtx_unlock(&chan->mtx);
5605 	}
5606 	return (0);			/* success */
5607 }
5608 
5609 static void
5610 umidi_close(struct usb_fifo *fifo, int fflags)
5611 {
5612 	if (fflags & FREAD) {
5613 		usb_fifo_free_buffer(fifo);
5614 	}
5615 	if (fflags & FWRITE) {
5616 		usb_fifo_free_buffer(fifo);
5617 	}
5618 }
5619 
5620 
5621 static int
5622 umidi_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
5623     int fflags)
5624 {
5625 	return (ENODEV);
5626 }
5627 
5628 static void
5629 umidi_init(device_t dev)
5630 {
5631 	struct uaudio_softc *sc = device_get_softc(dev);
5632 	struct umidi_chan *chan = &sc->sc_midi_chan;
5633 
5634 	mtx_init(&chan->mtx, "umidi lock", NULL, MTX_DEF | MTX_RECURSE);
5635 }
5636 
5637 static struct usb_fifo_methods umidi_fifo_methods = {
5638 	.f_start_read = &umidi_start_read,
5639 	.f_start_write = &umidi_start_write,
5640 	.f_stop_read = &umidi_stop_read,
5641 	.f_stop_write = &umidi_stop_write,
5642 	.f_open = &umidi_open,
5643 	.f_close = &umidi_close,
5644 	.f_ioctl = &umidi_ioctl,
5645 	.basename[0] = "umidi",
5646 };
5647 
5648 static int
5649 umidi_probe(device_t dev)
5650 {
5651 	struct uaudio_softc *sc = device_get_softc(dev);
5652 	struct usb_attach_arg *uaa = device_get_ivars(dev);
5653 	struct umidi_chan *chan = &sc->sc_midi_chan;
5654 	struct umidi_sub_chan *sub;
5655 	int unit = device_get_unit(dev);
5656 	int error;
5657 	uint32_t n;
5658 
5659 	if (usb_test_quirk(uaa, UQ_SINGLE_CMD_MIDI))
5660 		chan->single_command = 1;
5661 
5662 	if (usbd_set_alt_interface_index(sc->sc_udev, chan->iface_index,
5663 	    chan->iface_alt_index)) {
5664 		DPRINTF("setting of alternate index failed!\n");
5665 		goto detach;
5666 	}
5667 	usbd_set_parent_iface(sc->sc_udev, chan->iface_index,
5668 	    sc->sc_mixer_iface_index);
5669 
5670 	error = usbd_transfer_setup(uaa->device, &chan->iface_index,
5671 	    chan->xfer, umidi_config, UMIDI_N_TRANSFER,
5672 	    chan, &chan->mtx);
5673 	if (error) {
5674 		DPRINTF("error=%s\n", usbd_errstr(error));
5675 		goto detach;
5676 	}
5677 
5678 	/*
5679 	 * Some USB MIDI device makers couldn't resist using
5680 	 * wMaxPacketSize = 4 for RX and TX BULK endpoints, although
5681 	 * that size is an unsupported value for FULL speed BULK
5682 	 * endpoints. The same applies to some HIGH speed MIDI devices
5683 	 * which are using a wMaxPacketSize different from 512 bytes.
5684 	 *
5685 	 * Refer to section 5.8.3 in USB 2.0 PDF: Cite: "All Host
5686 	 * Controllers are required to have support for 8-, 16-, 32-,
5687 	 * and 64-byte maximum packet sizes for full-speed bulk
5688 	 * endpoints and 512 bytes for high-speed bulk endpoints."
5689 	 */
5690 	if (usbd_xfer_maxp_was_clamped(chan->xfer[UMIDI_TX_TRANSFER]))
5691 		chan->single_command = 1;
5692 
5693 	if (chan->single_command != 0)
5694 		device_printf(dev, "Single command MIDI quirk enabled\n");
5695 
5696 	if ((chan->max_cable > UMIDI_CABLES_MAX) ||
5697 	    (chan->max_cable == 0)) {
5698 		chan->max_cable = UMIDI_CABLES_MAX;
5699 	}
5700 
5701 	for (n = 0; n < chan->max_cable; n++) {
5702 
5703 		sub = &chan->sub[n];
5704 
5705 		error = usb_fifo_attach(sc->sc_udev, chan, &chan->mtx,
5706 		    &umidi_fifo_methods, &sub->fifo, unit, n,
5707 		    chan->iface_index,
5708 		    UID_ROOT, GID_OPERATOR, 0644);
5709 		if (error) {
5710 			goto detach;
5711 		}
5712 	}
5713 
5714 	mtx_lock(&chan->mtx);
5715 
5716 	/*
5717 	 * NOTE: At least one device will not work properly unless the
5718 	 * BULK IN pipe is open all the time. This might have to do
5719 	 * about that the internal queues of the device overflow if we
5720 	 * don't read them regularly.
5721 	 */
5722 	usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
5723 
5724 	mtx_unlock(&chan->mtx);
5725 
5726 	return (0);			/* success */
5727 
5728 detach:
5729 	return (ENXIO);			/* failure */
5730 }
5731 
5732 static int
5733 umidi_detach(device_t dev)
5734 {
5735 	struct uaudio_softc *sc = device_get_softc(dev);
5736 	struct umidi_chan *chan = &sc->sc_midi_chan;
5737 	uint32_t n;
5738 
5739 	for (n = 0; n < UMIDI_CABLES_MAX; n++) {
5740 		usb_fifo_detach(&chan->sub[n].fifo);
5741 	}
5742 
5743 	mtx_lock(&chan->mtx);
5744 
5745 	usbd_transfer_stop(chan->xfer[UMIDI_RX_TRANSFER]);
5746 
5747 	mtx_unlock(&chan->mtx);
5748 
5749 	usbd_transfer_unsetup(chan->xfer, UMIDI_N_TRANSFER);
5750 
5751 	mtx_destroy(&chan->mtx);
5752 
5753 	return (0);
5754 }
5755 
5756 static void
5757 uaudio_hid_rx_callback(struct usb_xfer *xfer, usb_error_t error)
5758 {
5759 	struct uaudio_softc *sc = usbd_xfer_softc(xfer);
5760 	const uint8_t *buffer = usbd_xfer_get_frame_buffer(xfer, 0);
5761 	struct snd_mixer *m;
5762 	uint8_t id;
5763 	int actlen;
5764 
5765 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
5766 
5767 	switch (USB_GET_STATE(xfer)) {
5768 	case USB_ST_TRANSFERRED:
5769 		DPRINTF("actlen=%d\n", actlen);
5770 
5771 		if (actlen != 0 &&
5772 		    (sc->sc_hid.flags & UAUDIO_HID_HAS_ID)) {
5773 			id = *buffer;
5774 			buffer++;
5775 			actlen--;
5776 		} else {
5777 			id = 0;
5778 		}
5779 
5780 		m = sc->sc_mixer_dev;
5781 
5782 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_MUTE) &&
5783 		    (sc->sc_hid.mute_id == id) &&
5784 		    hid_get_data(buffer, actlen,
5785 		    &sc->sc_hid.mute_loc)) {
5786 
5787 			DPRINTF("Mute toggle\n");
5788 
5789 			mixer_hwvol_mute_locked(m);
5790 		}
5791 
5792 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_VOLUME_UP) &&
5793 		    (sc->sc_hid.volume_up_id == id) &&
5794 		    hid_get_data(buffer, actlen,
5795 		    &sc->sc_hid.volume_up_loc)) {
5796 
5797 			DPRINTF("Volume Up\n");
5798 
5799 			mixer_hwvol_step_locked(m, 1, 1);
5800 		}
5801 
5802 		if ((sc->sc_hid.flags & UAUDIO_HID_HAS_VOLUME_DOWN) &&
5803 		    (sc->sc_hid.volume_down_id == id) &&
5804 		    hid_get_data(buffer, actlen,
5805 		    &sc->sc_hid.volume_down_loc)) {
5806 
5807 			DPRINTF("Volume Down\n");
5808 
5809 			mixer_hwvol_step_locked(m, -1, -1);
5810 		}
5811 
5812 	case USB_ST_SETUP:
5813 tr_setup:
5814 		/* check if we can put more data into the FIFO */
5815 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
5816 		usbd_transfer_submit(xfer);
5817 		break;
5818 
5819 	default:			/* Error */
5820 
5821 		DPRINTF("error=%s\n", usbd_errstr(error));
5822 
5823 		if (error != USB_ERR_CANCELLED) {
5824 			/* try to clear stall first */
5825 			usbd_xfer_set_stall(xfer);
5826 			goto tr_setup;
5827 		}
5828 		break;
5829 	}
5830 }
5831 
5832 static int
5833 uaudio_hid_probe(struct uaudio_softc *sc,
5834     struct usb_attach_arg *uaa)
5835 {
5836 	void *d_ptr;
5837 	uint32_t flags;
5838 	uint16_t d_len;
5839 	uint8_t id;
5840 	int error;
5841 
5842 	if (!(sc->sc_hid.flags & UAUDIO_HID_VALID))
5843 		return (-1);
5844 
5845 	if (sc->sc_mixer_lock == NULL)
5846 		return (-1);
5847 
5848 	/* Get HID descriptor */
5849 	error = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
5850 	    &d_len, M_TEMP, sc->sc_hid.iface_index);
5851 
5852 	if (error) {
5853 		DPRINTF("error reading report description\n");
5854 		return (-1);
5855 	}
5856 
5857 	/* check if there is an ID byte */
5858 	hid_report_size(d_ptr, d_len, hid_input, &id);
5859 
5860 	if (id != 0)
5861 		sc->sc_hid.flags |= UAUDIO_HID_HAS_ID;
5862 
5863 	if (hid_locate(d_ptr, d_len,
5864 	    HID_USAGE2(HUP_CONSUMER, 0xE9 /* Volume Increment */),
5865 	    hid_input, 0, &sc->sc_hid.volume_up_loc, &flags,
5866 	    &sc->sc_hid.volume_up_id)) {
5867 		if (flags & HIO_VARIABLE)
5868 			sc->sc_hid.flags |= UAUDIO_HID_HAS_VOLUME_UP;
5869 		DPRINTFN(1, "Found Volume Up key\n");
5870 	}
5871 
5872 	if (hid_locate(d_ptr, d_len,
5873 	    HID_USAGE2(HUP_CONSUMER, 0xEA /* Volume Decrement */),
5874 	    hid_input, 0, &sc->sc_hid.volume_down_loc, &flags,
5875 	    &sc->sc_hid.volume_down_id)) {
5876 		if (flags & HIO_VARIABLE)
5877 			sc->sc_hid.flags |= UAUDIO_HID_HAS_VOLUME_DOWN;
5878 		DPRINTFN(1, "Found Volume Down key\n");
5879 	}
5880 
5881 	if (hid_locate(d_ptr, d_len,
5882 	    HID_USAGE2(HUP_CONSUMER, 0xE2 /* Mute */),
5883 	    hid_input, 0, &sc->sc_hid.mute_loc, &flags,
5884 	    &sc->sc_hid.mute_id)) {
5885 		if (flags & HIO_VARIABLE)
5886 			sc->sc_hid.flags |= UAUDIO_HID_HAS_MUTE;
5887 		DPRINTFN(1, "Found Mute key\n");
5888 	}
5889 
5890 	free(d_ptr, M_TEMP);
5891 
5892 	if (!(sc->sc_hid.flags & (UAUDIO_HID_HAS_VOLUME_UP |
5893 	    UAUDIO_HID_HAS_VOLUME_DOWN |
5894 	    UAUDIO_HID_HAS_MUTE))) {
5895 		DPRINTFN(1, "Did not find any volume related keys\n");
5896 		return (-1);
5897 	}
5898 
5899 	/* prevent the uhid driver from attaching */
5900 	usbd_set_parent_iface(uaa->device, sc->sc_hid.iface_index,
5901 	    sc->sc_mixer_iface_index);
5902 
5903 	/* allocate USB transfers */
5904 	error = usbd_transfer_setup(uaa->device, &sc->sc_hid.iface_index,
5905 	    sc->sc_hid.xfer, uaudio_hid_config, UAUDIO_HID_N_TRANSFER,
5906 	    sc, sc->sc_mixer_lock);
5907 	if (error) {
5908 		DPRINTF("error=%s\n", usbd_errstr(error));
5909 		return (-1);
5910 	}
5911 	return (0);
5912 }
5913 
5914 static void
5915 uaudio_hid_detach(struct uaudio_softc *sc)
5916 {
5917 	usbd_transfer_unsetup(sc->sc_hid.xfer, UAUDIO_HID_N_TRANSFER);
5918 }
5919 
5920 DRIVER_MODULE_ORDERED(uaudio, uhub, uaudio_driver, uaudio_devclass, NULL, 0, SI_ORDER_ANY);
5921 MODULE_DEPEND(uaudio, usb, 1, 1, 1);
5922 MODULE_DEPEND(uaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
5923 MODULE_VERSION(uaudio, 1);
5924