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