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