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