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