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