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