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