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