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