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