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