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