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 75 #define USB_DEBUG_VAR uaudio_debug 76 #include <dev/usb/usb_debug.h> 77 78 #include <dev/usb/quirk/usb_quirk.h> 79 80 #include <sys/reboot.h> /* for bootverbose */ 81 82 #ifdef HAVE_KERNEL_OPTION_HEADERS 83 #include "opt_snd.h" 84 #endif 85 86 #include <dev/sound/pcm/sound.h> 87 #include <dev/sound/usb/uaudioreg.h> 88 #include <dev/sound/usb/uaudio.h> 89 #include <dev/sound/chip.h> 90 #include "feeder_if.h" 91 92 static int uaudio_default_rate = 0; /* use rate list */ 93 static int uaudio_default_bits = 32; 94 static int uaudio_default_channels = 0; /* use default */ 95 96 #ifdef USB_DEBUG 97 static int uaudio_debug = 0; 98 99 static SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW, 0, "USB uaudio"); 100 101 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, debug, CTLFLAG_RW, 102 &uaudio_debug, 0, "uaudio debug level"); 103 104 TUNABLE_INT("hw.usb.uaudio.default_rate", &uaudio_default_rate); 105 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_rate, CTLFLAG_RW, 106 &uaudio_default_rate, 0, "uaudio default sample rate"); 107 108 TUNABLE_INT("hw.usb.uaudio.default_bits", &uaudio_default_bits); 109 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_bits, CTLFLAG_RW, 110 &uaudio_default_bits, 0, "uaudio default sample bits"); 111 112 TUNABLE_INT("hw.usb.uaudio.default_channels", &uaudio_default_channels); 113 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_channels, CTLFLAG_RW, 114 &uaudio_default_channels, 0, "uaudio default sample channels"); 115 #endif 116 117 #define UAUDIO_NFRAMES 64 /* must be factor of 8 due HS-USB */ 118 #define UAUDIO_NCHANBUFS 2 /* number of outstanding request */ 119 #define UAUDIO_RECURSE_LIMIT 255 /* rounds */ 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 125 union uaudio_asid { 126 const struct usb_audio_streaming_interface_descriptor *v1; 127 const struct usb_audio20_streaming_interface_descriptor *v2; 128 }; 129 130 union uaudio_asf1d { 131 const struct usb_audio_streaming_type1_descriptor *v1; 132 const struct usb_audio20_streaming_type1_descriptor *v2; 133 }; 134 135 union uaudio_sed { 136 const struct usb_audio_streaming_endpoint_descriptor *v1; 137 const struct usb_audio20_streaming_endpoint_descriptor *v2; 138 }; 139 140 struct uaudio_mixer_node { 141 int32_t minval; 142 int32_t maxval; 143 #define MIX_MAX_CHAN 8 144 int32_t wValue[MIX_MAX_CHAN]; /* using nchan */ 145 uint32_t mul; 146 uint32_t ctl; 147 148 uint16_t wData[MIX_MAX_CHAN]; /* using nchan */ 149 uint16_t wIndex; 150 151 uint8_t update[(MIX_MAX_CHAN + 7) / 8]; 152 uint8_t nchan; 153 uint8_t type; 154 #define MIX_ON_OFF 1 155 #define MIX_SIGNED_16 2 156 #define MIX_UNSIGNED_16 3 157 #define MIX_SIGNED_8 4 158 #define MIX_SELECTOR 5 159 #define MIX_UNKNOWN 6 160 #define MIX_SIZE(n) ((((n) == MIX_SIGNED_16) || \ 161 ((n) == MIX_UNSIGNED_16)) ? 2 : 1) 162 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16) 163 164 #define MAX_SELECTOR_INPUT_PIN 256 165 uint8_t slctrtype[MAX_SELECTOR_INPUT_PIN]; 166 uint8_t class; 167 168 struct uaudio_mixer_node *next; 169 }; 170 171 struct uaudio_chan { 172 struct pcmchan_caps pcm_cap; /* capabilities */ 173 174 struct snd_dbuf *pcm_buf; 175 const struct usb_config *usb_cfg; 176 struct mtx *pcm_mtx; /* lock protecting this structure */ 177 struct uaudio_softc *priv_sc; 178 struct pcm_channel *pcm_ch; 179 struct usb_xfer *xfer[UAUDIO_NCHANBUFS]; 180 union uaudio_asf1d p_asf1d; 181 union uaudio_sed p_sed; 182 const usb_endpoint_descriptor_audio_t *p_ed1; 183 const struct uaudio_format *p_fmt; 184 185 uint8_t *buf; /* pointer to buffer */ 186 uint8_t *start; /* upper layer buffer start */ 187 uint8_t *end; /* upper layer buffer end */ 188 uint8_t *cur; /* current position in upper layer 189 * buffer */ 190 191 uint32_t intr_size; /* in bytes */ 192 uint32_t intr_frames; /* in units */ 193 uint32_t sample_rate; 194 uint32_t frames_per_second; 195 uint32_t sample_rem; 196 uint32_t sample_curr; 197 198 uint32_t format; 199 uint32_t pcm_format[2]; 200 201 uint16_t bytes_per_frame[2]; 202 203 uint16_t sample_size; 204 205 uint8_t valid; 206 uint8_t iface_index; 207 uint8_t iface_alt_index; 208 uint8_t channels; 209 }; 210 211 #define UMIDI_CABLES_MAX 16 /* units */ 212 #define UMIDI_TX_FRAMES 256 /* units */ 213 #define UMIDI_TX_BUFFER (UMIDI_TX_FRAMES * 4) /* bytes */ 214 215 enum { 216 UMIDI_TX_TRANSFER, 217 UMIDI_RX_TRANSFER, 218 UMIDI_N_TRANSFER, 219 }; 220 221 struct umidi_sub_chan { 222 struct usb_fifo_sc fifo; 223 uint8_t *temp_cmd; 224 uint8_t temp_0[4]; 225 uint8_t temp_1[4]; 226 uint8_t state; 227 #define UMIDI_ST_UNKNOWN 0 /* scan for command */ 228 #define UMIDI_ST_1PARAM 1 229 #define UMIDI_ST_2PARAM_1 2 230 #define UMIDI_ST_2PARAM_2 3 231 #define UMIDI_ST_SYSEX_0 4 232 #define UMIDI_ST_SYSEX_1 5 233 #define UMIDI_ST_SYSEX_2 6 234 235 uint8_t read_open:1; 236 uint8_t write_open:1; 237 uint8_t unused:6; 238 }; 239 240 struct umidi_chan { 241 242 struct umidi_sub_chan sub[UMIDI_CABLES_MAX]; 243 struct mtx mtx; 244 245 struct usb_xfer *xfer[UMIDI_N_TRANSFER]; 246 247 uint8_t iface_index; 248 uint8_t iface_alt_index; 249 250 uint8_t read_open_refcount; 251 uint8_t write_open_refcount; 252 253 uint8_t curr_cable; 254 uint8_t max_cable; 255 uint8_t valid; 256 uint8_t single_command; 257 }; 258 259 struct uaudio_search_result { 260 uint8_t bit_input[(256 + 7) / 8]; 261 uint8_t bit_output[(256 + 7) / 8]; 262 uint8_t recurse_level; 263 uint8_t id_max; 264 uint8_t is_input; 265 }; 266 267 struct uaudio_softc { 268 struct sbuf sc_sndstat; 269 struct sndcard_func sc_sndcard_func; 270 struct uaudio_chan sc_rec_chan; 271 struct uaudio_chan sc_play_chan; 272 struct umidi_chan sc_midi_chan; 273 struct uaudio_search_result sc_mixer_clocks; 274 275 struct usb_device *sc_udev; 276 struct usb_xfer *sc_mixer_xfer[1]; 277 struct uaudio_mixer_node *sc_mixer_root; 278 struct uaudio_mixer_node *sc_mixer_curr; 279 280 uint32_t sc_mix_info; 281 uint32_t sc_recsrc_info; 282 283 uint16_t sc_audio_rev; 284 uint16_t sc_mixer_count; 285 286 uint8_t sc_sndstat_valid; 287 uint8_t sc_mixer_iface_index; 288 uint8_t sc_mixer_iface_no; 289 uint8_t sc_mixer_chan; 290 uint8_t sc_pcm_registered:1; 291 uint8_t sc_mixer_init:1; 292 uint8_t sc_uq_audio_swap_lr:1; 293 uint8_t sc_uq_au_inp_async:1; 294 uint8_t sc_uq_au_no_xu:1; 295 uint8_t sc_uq_bad_adc:1; 296 uint8_t sc_uq_au_vendor_class:1; 297 }; 298 299 struct uaudio_terminal_node { 300 union { 301 const struct usb_descriptor *desc; 302 const struct usb_audio_input_terminal *it_v1; 303 const struct usb_audio_output_terminal *ot_v1; 304 const struct usb_audio_mixer_unit_0 *mu_v1; 305 const struct usb_audio_selector_unit *su_v1; 306 const struct usb_audio_feature_unit *fu_v1; 307 const struct usb_audio_processing_unit_0 *pu_v1; 308 const struct usb_audio_extension_unit_0 *eu_v1; 309 const struct usb_audio20_clock_source_unit *csrc_v2; 310 const struct usb_audio20_clock_selector_unit_0 *csel_v2; 311 const struct usb_audio20_clock_multiplier_unit *cmul_v2; 312 const struct usb_audio20_input_terminal *it_v2; 313 const struct usb_audio20_output_terminal *ot_v2; 314 const struct usb_audio20_mixer_unit_0 *mu_v2; 315 const struct usb_audio20_selector_unit *su_v2; 316 const struct usb_audio20_feature_unit *fu_v2; 317 const struct usb_audio20_sample_rate_unit *ru_v2; 318 const struct usb_audio20_processing_unit_0 *pu_v2; 319 const struct usb_audio20_extension_unit_0 *eu_v2; 320 const struct usb_audio20_effect_unit *ef_v2; 321 } u; 322 struct uaudio_search_result usr; 323 struct uaudio_terminal_node *root; 324 }; 325 326 struct uaudio_format { 327 uint16_t wFormat; 328 uint8_t bPrecision; 329 uint32_t freebsd_fmt; 330 const char *description; 331 }; 332 333 static const struct uaudio_format uaudio10_formats[] = { 334 335 {UA_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"}, 336 {UA_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"}, 337 {UA_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"}, 338 {UA_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"}, 339 340 {UA_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"}, 341 {UA_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"}, 342 {UA_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"}, 343 {UA_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"}, 344 345 {UA_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"}, 346 {UA_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"}, 347 348 {0, 0, 0, NULL} 349 }; 350 351 static const struct uaudio_format uaudio20_formats[] = { 352 353 {UA20_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"}, 354 {UA20_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"}, 355 {UA20_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"}, 356 {UA20_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"}, 357 358 {UA20_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"}, 359 {UA20_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"}, 360 {UA20_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"}, 361 {UA20_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"}, 362 363 {UA20_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"}, 364 {UA20_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"}, 365 366 {0, 0, 0, NULL} 367 }; 368 369 #define UAC_OUTPUT 0 370 #define UAC_INPUT 1 371 #define UAC_EQUAL 2 372 #define UAC_RECORD 3 373 #define UAC_NCLASSES 4 374 375 #ifdef USB_DEBUG 376 static const char *uac_names[] = { 377 "outputs", "inputs", "equalization", "record" 378 }; 379 380 #endif 381 382 /* prototypes */ 383 384 static device_probe_t uaudio_probe; 385 static device_attach_t uaudio_attach; 386 static device_detach_t uaudio_detach; 387 388 static usb_callback_t uaudio_chan_play_callback; 389 static usb_callback_t uaudio_chan_record_callback; 390 static usb_callback_t uaudio_mixer_write_cfg_callback; 391 static usb_callback_t umidi_bulk_read_callback; 392 static usb_callback_t umidi_bulk_write_callback; 393 394 /* ==== USB audio v1.0 ==== */ 395 396 static void uaudio_mixer_add_mixer(struct uaudio_softc *, 397 const struct uaudio_terminal_node *, int); 398 static void uaudio_mixer_add_selector(struct uaudio_softc *, 399 const struct uaudio_terminal_node *, int); 400 static uint32_t uaudio_mixer_feature_get_bmaControls( 401 const struct usb_audio_feature_unit *, uint8_t); 402 static void uaudio_mixer_add_feature(struct uaudio_softc *, 403 const struct uaudio_terminal_node *, int); 404 static void uaudio_mixer_add_processing_updown(struct uaudio_softc *, 405 const struct uaudio_terminal_node *, int); 406 static void uaudio_mixer_add_processing(struct uaudio_softc *, 407 const struct uaudio_terminal_node *, int); 408 static void uaudio_mixer_add_extension(struct uaudio_softc *, 409 const struct uaudio_terminal_node *, int); 410 static struct usb_audio_cluster uaudio_mixer_get_cluster(uint8_t, 411 const struct uaudio_terminal_node *); 412 static uint16_t uaudio_mixer_determine_class(const struct uaudio_terminal_node *, 413 struct uaudio_mixer_node *); 414 static uint16_t uaudio_mixer_feature_name(const struct uaudio_terminal_node *, 415 struct uaudio_mixer_node *); 416 static void uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *, 417 const uint8_t *, uint8_t, struct uaudio_search_result *); 418 static const void *uaudio_mixer_verify_desc(const void *, uint32_t); 419 static usb_error_t uaudio_set_speed(struct usb_device *, uint8_t, uint32_t); 420 static int uaudio_mixer_get(struct usb_device *, uint16_t, uint8_t, 421 struct uaudio_mixer_node *); 422 423 /* ==== USB audio v2.0 ==== */ 424 425 static void uaudio20_mixer_add_mixer(struct uaudio_softc *, 426 const struct uaudio_terminal_node *, int); 427 static void uaudio20_mixer_add_selector(struct uaudio_softc *, 428 const struct uaudio_terminal_node *, int); 429 static void uaudio20_mixer_add_feature(struct uaudio_softc *, 430 const struct uaudio_terminal_node *, int); 431 static struct usb_audio20_cluster uaudio20_mixer_get_cluster(uint8_t, 432 const struct uaudio_terminal_node *); 433 static uint16_t uaudio20_mixer_determine_class(const struct uaudio_terminal_node *, 434 struct uaudio_mixer_node *); 435 static uint16_t uaudio20_mixer_feature_name(const struct uaudio_terminal_node *, 436 struct uaudio_mixer_node *); 437 static void uaudio20_mixer_find_inputs_sub(struct uaudio_terminal_node *, 438 const uint8_t *, uint8_t, struct uaudio_search_result *); 439 static const void *uaudio20_mixer_verify_desc(const void *, uint32_t); 440 static usb_error_t uaudio20_set_speed(struct usb_device *, uint8_t, 441 uint8_t, uint32_t); 442 443 /* USB audio v1.0 and v2.0 */ 444 445 static void uaudio_chan_fill_info_sub(struct uaudio_softc *, 446 struct usb_device *, uint32_t, uint8_t, uint8_t); 447 static void uaudio_chan_fill_info(struct uaudio_softc *, 448 struct usb_device *); 449 static void uaudio_mixer_add_ctl_sub(struct uaudio_softc *, 450 struct uaudio_mixer_node *); 451 static void uaudio_mixer_add_ctl(struct uaudio_softc *, 452 struct uaudio_mixer_node *); 453 static void uaudio_mixer_fill_info(struct uaudio_softc *, 454 struct usb_device *, void *); 455 static void uaudio_mixer_ctl_set(struct uaudio_softc *, 456 struct uaudio_mixer_node *, uint8_t, int32_t val); 457 static int uaudio_mixer_signext(uint8_t, int); 458 static int uaudio_mixer_bsd2value(struct uaudio_mixer_node *, int32_t val); 459 static void uaudio_mixer_init(struct uaudio_softc *); 460 static const struct uaudio_terminal_node *uaudio_mixer_get_input( 461 const struct uaudio_terminal_node *, uint8_t); 462 static const struct uaudio_terminal_node *uaudio_mixer_get_output( 463 const struct uaudio_terminal_node *, uint8_t); 464 static void uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *, 465 uint8_t, uint8_t, struct uaudio_search_result *); 466 static uint8_t umidi_convert_to_usb(struct umidi_sub_chan *, uint8_t, uint8_t); 467 static struct umidi_sub_chan *umidi_sub_by_fifo(struct usb_fifo *); 468 static void umidi_start_read(struct usb_fifo *); 469 static void umidi_stop_read(struct usb_fifo *); 470 static void umidi_start_write(struct usb_fifo *); 471 static void umidi_stop_write(struct usb_fifo *); 472 static int umidi_open(struct usb_fifo *, int); 473 static int umidi_ioctl(struct usb_fifo *, u_long cmd, void *, int); 474 static void umidi_close(struct usb_fifo *, int); 475 static void umidi_init(device_t dev); 476 static int umidi_probe(device_t dev); 477 static int umidi_detach(device_t dev); 478 479 #ifdef USB_DEBUG 480 static void uaudio_chan_dump_ep_desc( 481 const usb_endpoint_descriptor_audio_t *); 482 #endif 483 484 static const struct usb_config 485 uaudio_cfg_record[UAUDIO_NCHANBUFS] = { 486 [0] = { 487 .type = UE_ISOCHRONOUS, 488 .endpoint = UE_ADDR_ANY, 489 .direction = UE_DIR_IN, 490 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 491 .frames = UAUDIO_NFRAMES, 492 .flags = {.short_xfer_ok = 1,}, 493 .callback = &uaudio_chan_record_callback, 494 }, 495 496 [1] = { 497 .type = UE_ISOCHRONOUS, 498 .endpoint = UE_ADDR_ANY, 499 .direction = UE_DIR_IN, 500 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 501 .frames = UAUDIO_NFRAMES, 502 .flags = {.short_xfer_ok = 1,}, 503 .callback = &uaudio_chan_record_callback, 504 }, 505 }; 506 507 static const struct usb_config 508 uaudio_cfg_play[UAUDIO_NCHANBUFS] = { 509 [0] = { 510 .type = UE_ISOCHRONOUS, 511 .endpoint = UE_ADDR_ANY, 512 .direction = UE_DIR_OUT, 513 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 514 .frames = UAUDIO_NFRAMES, 515 .flags = {.short_xfer_ok = 1,}, 516 .callback = &uaudio_chan_play_callback, 517 }, 518 519 [1] = { 520 .type = UE_ISOCHRONOUS, 521 .endpoint = UE_ADDR_ANY, 522 .direction = UE_DIR_OUT, 523 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 524 .frames = UAUDIO_NFRAMES, 525 .flags = {.short_xfer_ok = 1,}, 526 .callback = &uaudio_chan_play_callback, 527 }, 528 }; 529 530 static const struct usb_config 531 uaudio_mixer_config[1] = { 532 [0] = { 533 .type = UE_CONTROL, 534 .endpoint = 0x00, /* Control pipe */ 535 .direction = UE_DIR_ANY, 536 .bufsize = (sizeof(struct usb_device_request) + 4), 537 .callback = &uaudio_mixer_write_cfg_callback, 538 .timeout = 1000, /* 1 second */ 539 }, 540 }; 541 542 static const 543 uint8_t umidi_cmd_to_len[16] = { 544 [0x0] = 0, /* reserved */ 545 [0x1] = 0, /* reserved */ 546 [0x2] = 2, /* bytes */ 547 [0x3] = 3, /* bytes */ 548 [0x4] = 3, /* bytes */ 549 [0x5] = 1, /* bytes */ 550 [0x6] = 2, /* bytes */ 551 [0x7] = 3, /* bytes */ 552 [0x8] = 3, /* bytes */ 553 [0x9] = 3, /* bytes */ 554 [0xA] = 3, /* bytes */ 555 [0xB] = 3, /* bytes */ 556 [0xC] = 2, /* bytes */ 557 [0xD] = 2, /* bytes */ 558 [0xE] = 3, /* bytes */ 559 [0xF] = 1, /* bytes */ 560 }; 561 562 static const struct usb_config 563 umidi_config[UMIDI_N_TRANSFER] = { 564 [UMIDI_TX_TRANSFER] = { 565 .type = UE_BULK, 566 .endpoint = UE_ADDR_ANY, 567 .direction = UE_DIR_OUT, 568 .bufsize = UMIDI_TX_BUFFER, 569 .callback = &umidi_bulk_write_callback, 570 }, 571 572 [UMIDI_RX_TRANSFER] = { 573 .type = UE_BULK, 574 .endpoint = UE_ADDR_ANY, 575 .direction = UE_DIR_IN, 576 .bufsize = 4, /* bytes */ 577 .flags = {.short_xfer_ok = 1,.proxy_buffer = 1,}, 578 .callback = &umidi_bulk_read_callback, 579 }, 580 }; 581 582 static devclass_t uaudio_devclass; 583 584 static device_method_t uaudio_methods[] = { 585 DEVMETHOD(device_probe, uaudio_probe), 586 DEVMETHOD(device_attach, uaudio_attach), 587 DEVMETHOD(device_detach, uaudio_detach), 588 DEVMETHOD(device_suspend, bus_generic_suspend), 589 DEVMETHOD(device_resume, bus_generic_resume), 590 DEVMETHOD(device_shutdown, bus_generic_shutdown), 591 592 DEVMETHOD_END 593 }; 594 595 static driver_t uaudio_driver = { 596 .name = "uaudio", 597 .methods = uaudio_methods, 598 .size = sizeof(struct uaudio_softc), 599 }; 600 601 static const STRUCT_USB_HOST_ID __used uaudio_devs[] = { 602 /* Generic USB audio class match */ 603 {USB_IFACE_CLASS(UICLASS_AUDIO), 604 USB_IFACE_SUBCLASS(UISUBCLASS_AUDIOCONTROL),}, 605 /* Generic USB MIDI class match */ 606 {USB_IFACE_CLASS(UICLASS_AUDIO), 607 USB_IFACE_SUBCLASS(UISUBCLASS_MIDISTREAM),}, 608 }; 609 610 static int 611 uaudio_probe(device_t dev) 612 { 613 struct usb_attach_arg *uaa = device_get_ivars(dev); 614 615 if (uaa->usb_mode != USB_MODE_HOST) 616 return (ENXIO); 617 618 /* lookup non-standard device */ 619 620 if (uaa->info.bInterfaceClass != UICLASS_AUDIO) { 621 if (usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS) == 0) 622 return (ENXIO); 623 } 624 625 /* check for AUDIO control interface */ 626 627 if (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL) { 628 if (usb_test_quirk(uaa, UQ_BAD_AUDIO)) 629 return (ENXIO); 630 else 631 return (BUS_PROBE_GENERIC); 632 } 633 634 /* check for MIDI stream */ 635 636 if (uaa->info.bInterfaceSubClass == UISUBCLASS_MIDISTREAM) { 637 if (usb_test_quirk(uaa, UQ_BAD_MIDI)) 638 return (ENXIO); 639 else 640 return (BUS_PROBE_GENERIC); 641 } 642 return (ENXIO); 643 } 644 645 static int 646 uaudio_attach(device_t dev) 647 { 648 struct usb_attach_arg *uaa = device_get_ivars(dev); 649 struct uaudio_softc *sc = device_get_softc(dev); 650 struct usb_interface_descriptor *id; 651 device_t child; 652 653 sc->sc_play_chan.priv_sc = sc; 654 sc->sc_rec_chan.priv_sc = sc; 655 sc->sc_udev = uaa->device; 656 sc->sc_mixer_iface_index = uaa->info.bIfaceIndex; 657 sc->sc_mixer_iface_no = uaa->info.bIfaceNum; 658 659 if (usb_test_quirk(uaa, UQ_AUDIO_SWAP_LR)) 660 sc->sc_uq_audio_swap_lr = 1; 661 662 if (usb_test_quirk(uaa, UQ_AU_INP_ASYNC)) 663 sc->sc_uq_au_inp_async = 1; 664 665 if (usb_test_quirk(uaa, UQ_AU_NO_XU)) 666 sc->sc_uq_au_no_xu = 1; 667 668 if (usb_test_quirk(uaa, UQ_BAD_ADC)) 669 sc->sc_uq_bad_adc = 1; 670 671 if (usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS)) 672 sc->sc_uq_au_vendor_class = 1; 673 674 umidi_init(dev); 675 676 device_set_usb_desc(dev); 677 678 id = usbd_get_interface_descriptor(uaa->iface); 679 680 /* must fill mixer info before channel info */ 681 uaudio_mixer_fill_info(sc, uaa->device, id); 682 683 /* fill channel info */ 684 uaudio_chan_fill_info(sc, uaa->device); 685 686 DPRINTF("audio rev %d.%02x\n", 687 sc->sc_audio_rev >> 8, 688 sc->sc_audio_rev & 0xff); 689 690 DPRINTF("%d mixer controls\n", 691 sc->sc_mixer_count); 692 693 if (sc->sc_play_chan.valid) { 694 device_printf(dev, "Play: %d Hz, %d ch, %s format.\n", 695 sc->sc_play_chan.sample_rate, 696 sc->sc_play_chan.channels, 697 sc->sc_play_chan.p_fmt->description); 698 } else { 699 device_printf(dev, "No playback.\n"); 700 } 701 702 if (sc->sc_rec_chan.valid) { 703 device_printf(dev, "Record: %d Hz, %d ch, %s format.\n", 704 sc->sc_rec_chan.sample_rate, 705 sc->sc_play_chan.channels, 706 sc->sc_rec_chan.p_fmt->description); 707 } else { 708 device_printf(dev, "No recording.\n"); 709 } 710 711 if (sc->sc_midi_chan.valid) { 712 713 if (umidi_probe(dev)) { 714 goto detach; 715 } 716 device_printf(dev, "MIDI sequencer.\n"); 717 } else { 718 device_printf(dev, "No midi sequencer.\n"); 719 } 720 721 DPRINTF("doing child attach\n"); 722 723 /* attach the children */ 724 725 sc->sc_sndcard_func.func = SCF_PCM; 726 727 /* 728 * Only attach a PCM device if we have a playback, recording 729 * or mixer device present: 730 */ 731 if (sc->sc_play_chan.valid || 732 sc->sc_rec_chan.valid || 733 sc->sc_mix_info) { 734 child = device_add_child(dev, "pcm", -1); 735 736 if (child == NULL) { 737 DPRINTF("out of memory\n"); 738 goto detach; 739 } 740 device_set_ivars(child, &sc->sc_sndcard_func); 741 } 742 743 if (bus_generic_attach(dev)) { 744 DPRINTF("child attach failed\n"); 745 goto detach; 746 } 747 return (0); /* success */ 748 749 detach: 750 uaudio_detach(dev); 751 return (ENXIO); 752 } 753 754 static void 755 uaudio_pcm_setflags(device_t dev, uint32_t flags) 756 { 757 pcm_setflags(dev, pcm_getflags(dev) | flags); 758 } 759 760 int 761 uaudio_attach_sub(device_t dev, kobj_class_t mixer_class, kobj_class_t chan_class) 762 { 763 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev)); 764 char status[SND_STATUSLEN]; 765 766 uaudio_mixer_init(sc); 767 768 if (sc->sc_uq_audio_swap_lr) { 769 DPRINTF("hardware has swapped left and right\n"); 770 /* uaudio_pcm_setflags(dev, SD_F_PSWAPLR); */ 771 } 772 if (!(sc->sc_mix_info & SOUND_MASK_PCM)) { 773 774 DPRINTF("emulating master volume\n"); 775 776 /* 777 * Emulate missing pcm mixer controller 778 * through FEEDER_VOLUME 779 */ 780 uaudio_pcm_setflags(dev, SD_F_SOFTPCMVOL); 781 } 782 if (mixer_init(dev, mixer_class, sc)) { 783 goto detach; 784 } 785 sc->sc_mixer_init = 1; 786 787 snprintf(status, sizeof(status), "at ? %s", PCM_KLDSTRING(snd_uaudio)); 788 789 if (pcm_register(dev, sc, 790 sc->sc_play_chan.valid ? 1 : 0, 791 sc->sc_rec_chan.valid ? 1 : 0)) { 792 goto detach; 793 } 794 795 uaudio_pcm_setflags(dev, SD_F_MPSAFE); 796 sc->sc_pcm_registered = 1; 797 798 if (sc->sc_play_chan.valid) { 799 pcm_addchan(dev, PCMDIR_PLAY, chan_class, sc); 800 } 801 if (sc->sc_rec_chan.valid) { 802 pcm_addchan(dev, PCMDIR_REC, chan_class, sc); 803 } 804 pcm_setstatus(dev, status); 805 806 return (0); /* success */ 807 808 detach: 809 uaudio_detach_sub(dev); 810 return (ENXIO); 811 } 812 813 int 814 uaudio_detach_sub(device_t dev) 815 { 816 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev)); 817 int error = 0; 818 819 repeat: 820 if (sc->sc_pcm_registered) { 821 error = pcm_unregister(dev); 822 } else { 823 if (sc->sc_mixer_init) { 824 error = mixer_uninit(dev); 825 } 826 } 827 828 if (error) { 829 device_printf(dev, "Waiting for sound application to exit!\n"); 830 usb_pause_mtx(NULL, 2 * hz); 831 goto repeat; /* try again */ 832 } 833 return (0); /* success */ 834 } 835 836 static int 837 uaudio_detach(device_t dev) 838 { 839 struct uaudio_softc *sc = device_get_softc(dev); 840 841 /* 842 * Stop USB transfers early so that any audio applications 843 * will time out and close opened /dev/dspX.Y device(s), if 844 * any. 845 */ 846 if (sc->sc_play_chan.valid) 847 usbd_transfer_unsetup(sc->sc_play_chan.xfer, UAUDIO_NCHANBUFS); 848 if (sc->sc_rec_chan.valid) 849 usbd_transfer_unsetup(sc->sc_rec_chan.xfer, UAUDIO_NCHANBUFS); 850 851 if (bus_generic_detach(dev) != 0) { 852 DPRINTF("detach failed!\n"); 853 } 854 sbuf_delete(&sc->sc_sndstat); 855 sc->sc_sndstat_valid = 0; 856 857 umidi_detach(dev); 858 859 return (0); 860 } 861 862 /*========================================================================* 863 * AS - Audio Stream - routines 864 *========================================================================*/ 865 866 #ifdef USB_DEBUG 867 static void 868 uaudio_chan_dump_ep_desc(const usb_endpoint_descriptor_audio_t *ed) 869 { 870 if (ed) { 871 DPRINTF("endpoint=%p bLength=%d bDescriptorType=%d \n" 872 "bEndpointAddress=%d bmAttributes=0x%x \n" 873 "wMaxPacketSize=%d bInterval=%d \n" 874 "bRefresh=%d bSynchAddress=%d\n", 875 ed, ed->bLength, ed->bDescriptorType, 876 ed->bEndpointAddress, ed->bmAttributes, 877 UGETW(ed->wMaxPacketSize), ed->bInterval, 878 UEP_HAS_REFRESH(ed) ? ed->bRefresh : 0, 879 UEP_HAS_SYNCADDR(ed) ? ed->bSynchAddress : 0); 880 } 881 } 882 883 #endif 884 885 /* 886 * The following is a workaround for broken no-name USB audio devices 887 * sold by dealextreme called "3D sound". The problem is that the 888 * manufacturer computed wMaxPacketSize is too small to hold the 889 * actual data sent. In other words the device sometimes sends more 890 * data than it actually reports it can send in a single isochronous 891 * packet. 892 */ 893 static void 894 uaudio_record_fix_fs(usb_endpoint_descriptor_audio_t *ep, 895 uint32_t xps, uint32_t add) 896 { 897 uint32_t mps; 898 899 mps = UGETW(ep->wMaxPacketSize); 900 901 /* 902 * If the device indicates it can send more data than what the 903 * sample rate indicates, we apply the workaround. 904 */ 905 if (mps > xps) { 906 907 /* allow additional data */ 908 xps += add; 909 910 /* check against the maximum USB 1.x length */ 911 if (xps > 1023) 912 xps = 1023; 913 914 /* check if we should do an update */ 915 if (mps < xps) { 916 /* simply update the wMaxPacketSize field */ 917 USETW(ep->wMaxPacketSize, xps); 918 DPRINTF("Workaround: Updated wMaxPacketSize " 919 "from %d to %d bytes.\n", 920 (int)mps, (int)xps); 921 } 922 } 923 } 924 925 static usb_error_t 926 uaudio20_check_rate(struct usb_device *udev, uint8_t iface_no, 927 uint8_t clockid, uint32_t rate) 928 { 929 struct usb_device_request req; 930 usb_error_t error; 931 uint8_t data[255]; 932 uint16_t actlen; 933 uint16_t rates; 934 uint16_t x; 935 936 DPRINTFN(6, "ifaceno=%d clockid=%d rate=%u\n", 937 iface_no, clockid, rate); 938 939 req.bmRequestType = UT_READ_CLASS_INTERFACE; 940 req.bRequest = UA20_CS_RANGE; 941 USETW2(req.wValue, UA20_CS_SAM_FREQ_CONTROL, 0); 942 USETW2(req.wIndex, clockid, iface_no); 943 USETW(req.wLength, 255); 944 945 error = usbd_do_request_flags(udev, NULL, &req, data, 946 USB_SHORT_XFER_OK, &actlen, USB_DEFAULT_TIMEOUT); 947 948 if (error != 0 || actlen < 2) 949 return (USB_ERR_INVAL); 950 951 rates = data[0] | (data[1] << 8); 952 actlen = (actlen - 2) / 12; 953 954 if (rates > actlen) { 955 DPRINTF("Too many rates\n"); 956 rates = actlen; 957 } 958 959 for (x = 0; x != rates; x++) { 960 uint32_t min = UGETDW(data + 2 + (12 * x)); 961 uint32_t max = UGETDW(data + 6 + (12 * x)); 962 uint32_t res = UGETDW(data + 10 + (12 * x)); 963 964 if (res == 0) { 965 DPRINTF("Zero residue\n"); 966 res = 1; 967 } 968 969 if (min > max) { 970 DPRINTF("Swapped max and min\n"); 971 uint32_t temp; 972 temp = min; 973 min = max; 974 max = temp; 975 } 976 977 if (rate >= min && rate <= max && 978 (((rate - min) % res) == 0)) { 979 return (0); 980 } 981 } 982 return (USB_ERR_INVAL); 983 } 984 985 static void 986 uaudio_chan_fill_info_sub(struct uaudio_softc *sc, struct usb_device *udev, 987 uint32_t rate, uint8_t channels, uint8_t bit_resolution) 988 { 989 struct usb_descriptor *desc = NULL; 990 union uaudio_asid asid = { NULL }; 991 union uaudio_asf1d asf1d = { NULL }; 992 union uaudio_sed sed = { NULL }; 993 usb_endpoint_descriptor_audio_t *ed1 = NULL; 994 const struct usb_audio_control_descriptor *acdp = NULL; 995 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 996 struct usb_interface_descriptor *id; 997 const struct uaudio_format *p_fmt = NULL; 998 struct uaudio_chan *chan; 999 uint16_t curidx = 0xFFFF; 1000 uint16_t lastidx = 0xFFFF; 1001 uint16_t alt_index = 0; 1002 uint16_t audio_rev = 0; 1003 uint16_t x; 1004 uint8_t ep_dir; 1005 uint8_t bChannels; 1006 uint8_t bBitResolution; 1007 uint8_t audio_if = 0; 1008 uint8_t uma_if_class; 1009 1010 while ((desc = usb_desc_foreach(cd, desc))) { 1011 1012 if ((desc->bDescriptorType == UDESC_INTERFACE) && 1013 (desc->bLength >= sizeof(*id))) { 1014 1015 id = (void *)desc; 1016 1017 if (id->bInterfaceNumber != lastidx) { 1018 lastidx = id->bInterfaceNumber; 1019 curidx++; 1020 alt_index = 0; 1021 1022 } else { 1023 alt_index++; 1024 } 1025 1026 uma_if_class = 1027 ((id->bInterfaceClass == UICLASS_AUDIO) || 1028 ((id->bInterfaceClass == UICLASS_VENDOR) && 1029 (sc->sc_uq_au_vendor_class != 0))); 1030 1031 if ((uma_if_class != 0) && (id->bInterfaceSubClass == UISUBCLASS_AUDIOSTREAM)) { 1032 audio_if = 1; 1033 } else { 1034 audio_if = 0; 1035 } 1036 1037 if ((uma_if_class != 0) && 1038 (id->bInterfaceSubClass == UISUBCLASS_MIDISTREAM)) { 1039 1040 /* 1041 * XXX could allow multiple MIDI interfaces 1042 */ 1043 1044 if ((sc->sc_midi_chan.valid == 0) && 1045 usbd_get_iface(udev, curidx)) { 1046 sc->sc_midi_chan.iface_index = curidx; 1047 sc->sc_midi_chan.iface_alt_index = alt_index; 1048 sc->sc_midi_chan.valid = 1; 1049 } 1050 } 1051 asid.v1 = NULL; 1052 asf1d.v1 = NULL; 1053 ed1 = NULL; 1054 sed.v1 = NULL; 1055 } 1056 1057 if (audio_if == 0) { 1058 if ((acdp == NULL) && 1059 (desc->bDescriptorType == UDESC_CS_INTERFACE) && 1060 (desc->bDescriptorSubtype == UDESCSUB_AC_HEADER) && 1061 (desc->bLength >= sizeof(*acdp))) { 1062 acdp = (void *)desc; 1063 audio_rev = UGETW(acdp->bcdADC); 1064 } 1065 1066 /* 1067 * Don't collect any USB audio descriptors if 1068 * this is not an USB audio stream interface. 1069 */ 1070 continue; 1071 } 1072 1073 if ((acdp != NULL) && 1074 (desc->bDescriptorType == UDESC_CS_INTERFACE) && 1075 (desc->bDescriptorSubtype == AS_GENERAL) && 1076 (asid.v1 == NULL)) { 1077 if (audio_rev >= UAUDIO_VERSION_30) { 1078 /* FALLTHROUGH */ 1079 } else if (audio_rev >= UAUDIO_VERSION_20) { 1080 if (desc->bLength >= sizeof(*asid.v2)) { 1081 asid.v2 = (void *)desc; 1082 } 1083 } else { 1084 if (desc->bLength >= sizeof(*asid.v1)) { 1085 asid.v1 = (void *)desc; 1086 } 1087 } 1088 } 1089 if ((acdp != NULL) && 1090 (desc->bDescriptorType == UDESC_CS_INTERFACE) && 1091 (desc->bDescriptorSubtype == FORMAT_TYPE) && 1092 (asf1d.v1 == NULL)) { 1093 if (audio_rev >= UAUDIO_VERSION_30) { 1094 /* FALLTHROUGH */ 1095 } else if (audio_rev >= UAUDIO_VERSION_20) { 1096 if (desc->bLength >= sizeof(*asf1d.v2)) 1097 asf1d.v2 = (void *)desc; 1098 } else { 1099 if (desc->bLength >= sizeof(*asf1d.v1)) { 1100 asf1d.v1 = (void *)desc; 1101 1102 if (asf1d.v1->bFormatType != FORMAT_TYPE_I) { 1103 DPRINTFN(11, "ignored bFormatType = %d\n", 1104 asf1d.v1->bFormatType); 1105 asf1d.v1 = NULL; 1106 continue; 1107 } 1108 if (desc->bLength < (sizeof(*asf1d.v1) + 1109 ((asf1d.v1->bSamFreqType == 0) ? 6 : 1110 (asf1d.v1->bSamFreqType * 3)))) { 1111 DPRINTFN(11, "invalid descriptor, " 1112 "too short\n"); 1113 asf1d.v1 = NULL; 1114 continue; 1115 } 1116 } 1117 } 1118 } 1119 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 1120 (desc->bLength >= UEP_MINSIZE) && 1121 (ed1 == NULL)) { 1122 ed1 = (void *)desc; 1123 if (UE_GET_XFERTYPE(ed1->bmAttributes) != UE_ISOCHRONOUS) { 1124 ed1 = NULL; 1125 continue; 1126 } 1127 } 1128 if ((acdp != NULL) && 1129 (desc->bDescriptorType == UDESC_CS_ENDPOINT) && 1130 (desc->bDescriptorSubtype == AS_GENERAL) && 1131 (sed.v1 == NULL)) { 1132 if (audio_rev >= UAUDIO_VERSION_30) { 1133 /* FALLTHROUGH */ 1134 } else if (audio_rev >= UAUDIO_VERSION_20) { 1135 if (desc->bLength >= sizeof(*sed.v2)) 1136 sed.v2 = (void *)desc; 1137 } else { 1138 if (desc->bLength >= sizeof(*sed.v1)) 1139 sed.v1 = (void *)desc; 1140 } 1141 } 1142 if (asid.v1 == NULL || asf1d.v1 == NULL || 1143 ed1 == NULL || sed.v1 == NULL) { 1144 /* need more descriptors */ 1145 continue; 1146 } 1147 1148 ep_dir = UE_GET_DIR(ed1->bEndpointAddress); 1149 1150 /* We ignore sync endpoint information until further. */ 1151 1152 if (audio_rev >= UAUDIO_VERSION_30) { 1153 goto next_ep; 1154 } else if (audio_rev >= UAUDIO_VERSION_20) { 1155 1156 uint32_t dwFormat; 1157 uint8_t bSubslotSize; 1158 1159 dwFormat = UGETDW(asid.v2->bmFormats); 1160 bChannels = asid.v2->bNrChannels; 1161 bBitResolution = asf1d.v2->bBitResolution; 1162 bSubslotSize = asf1d.v2->bSubslotSize; 1163 1164 if (bBitResolution != (bSubslotSize * 8)) { 1165 DPRINTF("Invalid bSubslotSize\n"); 1166 goto next_ep; 1167 } 1168 1169 if ((bChannels != channels) || 1170 (bBitResolution != bit_resolution)) { 1171 DPRINTF("Wrong number of channels\n"); 1172 goto next_ep; 1173 } 1174 1175 for (p_fmt = uaudio20_formats; 1176 p_fmt->wFormat != 0; p_fmt++) { 1177 if ((p_fmt->wFormat & dwFormat) && 1178 (p_fmt->bPrecision == bBitResolution)) 1179 break; 1180 } 1181 1182 if (p_fmt->wFormat == 0) { 1183 DPRINTF("Unsupported audio format\n"); 1184 goto next_ep; 1185 } 1186 1187 for (x = 0; x != 256; x++) { 1188 if (ep_dir == UE_DIR_OUT) { 1189 if (!(sc->sc_mixer_clocks.bit_output[x / 8] & 1190 (1 << (x % 8)))) { 1191 continue; 1192 } 1193 } else { 1194 if (!(sc->sc_mixer_clocks.bit_input[x / 8] & 1195 (1 << (x % 8)))) { 1196 continue; 1197 } 1198 } 1199 1200 DPRINTF("Checking clock ID=%d\n", x); 1201 1202 if (uaudio20_check_rate(udev, 1203 sc->sc_mixer_iface_no, x, rate)) { 1204 DPRINTF("Unsupported sampling " 1205 "rate, id=%d\n", x); 1206 goto next_ep; 1207 } 1208 } 1209 } else { 1210 uint16_t wFormat; 1211 1212 wFormat = UGETW(asid.v1->wFormatTag); 1213 bChannels = UAUDIO_MAX_CHAN(asf1d.v1->bNrChannels); 1214 bBitResolution = asf1d.v1->bBitResolution; 1215 1216 if (asf1d.v1->bSamFreqType == 0) { 1217 DPRINTFN(16, "Sample rate: %d-%dHz\n", 1218 UA_SAMP_LO(asf1d.v1), 1219 UA_SAMP_HI(asf1d.v1)); 1220 1221 if ((rate >= UA_SAMP_LO(asf1d.v1)) && 1222 (rate <= UA_SAMP_HI(asf1d.v1))) 1223 goto found_rate; 1224 } else { 1225 1226 for (x = 0; x < asf1d.v1->bSamFreqType; x++) { 1227 DPRINTFN(16, "Sample rate = %dHz\n", 1228 UA_GETSAMP(asf1d.v1, x)); 1229 1230 if (rate == UA_GETSAMP(asf1d.v1, x)) 1231 goto found_rate; 1232 } 1233 } 1234 goto next_ep; 1235 1236 found_rate: 1237 for (p_fmt = uaudio10_formats; 1238 p_fmt->wFormat != 0; p_fmt++) { 1239 if ((p_fmt->wFormat == wFormat) && 1240 (p_fmt->bPrecision == bBitResolution)) 1241 break; 1242 } 1243 if (p_fmt->wFormat == 0) { 1244 DPRINTF("Unsupported audio format\n"); 1245 goto next_ep; 1246 } 1247 1248 if ((bChannels != channels) || 1249 (bBitResolution != bit_resolution)) { 1250 DPRINTF("Wrong number of channels\n"); 1251 goto next_ep; 1252 } 1253 } 1254 1255 chan = (ep_dir == UE_DIR_IN) ? 1256 &sc->sc_rec_chan : &sc->sc_play_chan; 1257 1258 if (chan->valid != 0 || 1259 usbd_get_iface(udev, curidx) == NULL) { 1260 DPRINTF("Channel already exists or " 1261 "interface is not valid\n"); 1262 goto next_ep; 1263 } 1264 1265 chan->valid = 1; 1266 #ifdef USB_DEBUG 1267 uaudio_chan_dump_ep_desc(ed1); 1268 #endif 1269 DPRINTF("Sample rate = %dHz, channels = %d, " 1270 "bits = %d, format = %s\n", rate, channels, 1271 bit_resolution, p_fmt->description); 1272 1273 chan->sample_rate = rate; 1274 chan->p_asf1d = asf1d; 1275 chan->p_ed1 = ed1; 1276 chan->p_fmt = p_fmt; 1277 chan->p_sed = sed; 1278 chan->iface_index = curidx; 1279 chan->iface_alt_index = alt_index; 1280 1281 if (ep_dir == UE_DIR_IN) 1282 chan->usb_cfg = uaudio_cfg_record; 1283 else 1284 chan->usb_cfg = uaudio_cfg_play; 1285 1286 chan->sample_size = (UAUDIO_MAX_CHAN(channels) * 1287 p_fmt->bPrecision) / 8; 1288 chan->channels = channels; 1289 1290 if (ep_dir == UE_DIR_IN && 1291 usbd_get_speed(udev) == USB_SPEED_FULL) { 1292 uaudio_record_fix_fs(ed1, 1293 chan->sample_size * (rate / 1000), 1294 chan->sample_size * (rate / 4000)); 1295 } 1296 1297 if (sc->sc_sndstat_valid != 0) { 1298 sbuf_printf(&sc->sc_sndstat, "\n\t" 1299 "mode %d.%d:(%s) %dch, %dbit, %s, %dHz", 1300 curidx, alt_index, 1301 (ep_dir == UE_DIR_IN) ? "input" : "output", 1302 channels, p_fmt->bPrecision, 1303 p_fmt->description, rate); 1304 } 1305 1306 next_ep: 1307 sed.v1 = NULL; 1308 ed1 = NULL; 1309 } 1310 } 1311 1312 /* This structure defines all the supported rates. */ 1313 1314 static const uint32_t uaudio_rate_list[] = { 1315 96000, 1316 88000, 1317 80000, 1318 72000, 1319 64000, 1320 56000, 1321 48000, 1322 44100, 1323 40000, 1324 32000, 1325 24000, 1326 22050, 1327 16000, 1328 11025, 1329 8000, 1330 0 1331 }; 1332 1333 static void 1334 uaudio_chan_fill_info(struct uaudio_softc *sc, struct usb_device *udev) 1335 { 1336 uint32_t rate = uaudio_default_rate; 1337 uint8_t z; 1338 uint8_t bits = uaudio_default_bits; 1339 uint8_t y; 1340 uint8_t channels = uaudio_default_channels; 1341 uint8_t x; 1342 1343 bits -= (bits % 8); 1344 if ((bits == 0) || (bits > 32)) { 1345 /* set a valid value */ 1346 bits = 32; 1347 } 1348 if (channels == 0) { 1349 switch (usbd_get_speed(udev)) { 1350 case USB_SPEED_LOW: 1351 case USB_SPEED_FULL: 1352 /* 1353 * Due to high bandwidth usage and problems 1354 * with HIGH-speed split transactions we 1355 * disable surround setups on FULL-speed USB 1356 * by default 1357 */ 1358 channels = 4; 1359 break; 1360 default: 1361 channels = 16; 1362 break; 1363 } 1364 } else if (channels > 16) { 1365 channels = 16; 1366 } 1367 if (sbuf_new(&sc->sc_sndstat, NULL, 4096, SBUF_AUTOEXTEND)) { 1368 sc->sc_sndstat_valid = 1; 1369 } 1370 /* try to search for a valid config */ 1371 1372 for (x = channels; x; x--) { 1373 for (y = bits; y; y -= 8) { 1374 1375 /* try user defined rate, if any */ 1376 if (rate != 0) 1377 uaudio_chan_fill_info_sub(sc, udev, rate, x, y); 1378 1379 /* try find a matching rate, if any */ 1380 for (z = 0; uaudio_rate_list[z]; z++) { 1381 uaudio_chan_fill_info_sub(sc, udev, uaudio_rate_list[z], x, y); 1382 1383 if (sc->sc_rec_chan.valid && 1384 sc->sc_play_chan.valid) { 1385 goto done; 1386 } 1387 } 1388 } 1389 } 1390 1391 done: 1392 if (sc->sc_sndstat_valid) { 1393 sbuf_finish(&sc->sc_sndstat); 1394 } 1395 } 1396 1397 static void 1398 uaudio_chan_play_callback(struct usb_xfer *xfer, usb_error_t error) 1399 { 1400 struct uaudio_chan *ch = usbd_xfer_softc(xfer); 1401 struct usb_page_cache *pc; 1402 uint32_t total; 1403 uint32_t blockcount; 1404 uint32_t n; 1405 uint32_t offset; 1406 int actlen; 1407 int sumlen; 1408 1409 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1410 1411 if (ch->end == ch->start) { 1412 DPRINTF("no buffer!\n"); 1413 return; 1414 } 1415 1416 switch (USB_GET_STATE(xfer)) { 1417 case USB_ST_TRANSFERRED: 1418 tr_transferred: 1419 if (actlen < sumlen) { 1420 DPRINTF("short transfer, " 1421 "%d of %d bytes\n", actlen, sumlen); 1422 } 1423 chn_intr(ch->pcm_ch); 1424 1425 case USB_ST_SETUP: 1426 if (ch->bytes_per_frame[1] > usbd_xfer_max_framelen(xfer)) { 1427 DPRINTF("bytes per transfer, %d, " 1428 "exceeds maximum, %d!\n", 1429 ch->bytes_per_frame[1], 1430 usbd_xfer_max_framelen(xfer)); 1431 break; 1432 } 1433 1434 blockcount = ch->intr_frames; 1435 1436 /* setup number of frames */ 1437 usbd_xfer_set_frames(xfer, blockcount); 1438 1439 /* reset total length */ 1440 total = 0; 1441 1442 /* setup frame lengths */ 1443 for (n = 0; n != blockcount; n++) { 1444 ch->sample_curr += ch->sample_rem; 1445 if (ch->sample_curr >= ch->frames_per_second) { 1446 ch->sample_curr -= ch->frames_per_second; 1447 usbd_xfer_set_frame_len(xfer, n, ch->bytes_per_frame[1]); 1448 total += ch->bytes_per_frame[1]; 1449 } else { 1450 usbd_xfer_set_frame_len(xfer, n, ch->bytes_per_frame[0]); 1451 total += ch->bytes_per_frame[0]; 1452 } 1453 } 1454 1455 DPRINTFN(6, "transfer %d bytes\n", total); 1456 1457 offset = 0; 1458 1459 pc = usbd_xfer_get_frame(xfer, 0); 1460 while (total > 0) { 1461 1462 n = (ch->end - ch->cur); 1463 if (n > total) { 1464 n = total; 1465 } 1466 usbd_copy_in(pc, offset, ch->cur, n); 1467 1468 total -= n; 1469 ch->cur += n; 1470 offset += n; 1471 1472 if (ch->cur >= ch->end) { 1473 ch->cur = ch->start; 1474 } 1475 } 1476 1477 usbd_transfer_submit(xfer); 1478 break; 1479 1480 default: /* Error */ 1481 if (error == USB_ERR_CANCELLED) { 1482 break; 1483 } 1484 goto tr_transferred; 1485 } 1486 } 1487 1488 static void 1489 uaudio_chan_record_callback(struct usb_xfer *xfer, usb_error_t error) 1490 { 1491 struct uaudio_chan *ch = usbd_xfer_softc(xfer); 1492 struct usb_page_cache *pc; 1493 uint32_t offset0; 1494 uint32_t offset1; 1495 uint32_t mfl; 1496 int m; 1497 int n; 1498 int len; 1499 int actlen; 1500 int nframes; 1501 int blockcount; 1502 1503 usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes); 1504 mfl = usbd_xfer_max_framelen(xfer); 1505 1506 if (ch->end == ch->start) { 1507 DPRINTF("no buffer!\n"); 1508 return; 1509 } 1510 1511 switch (USB_GET_STATE(xfer)) { 1512 case USB_ST_TRANSFERRED: 1513 1514 DPRINTFN(6, "transferred %d bytes\n", actlen); 1515 1516 offset0 = 0; 1517 pc = usbd_xfer_get_frame(xfer, 0); 1518 1519 for (n = 0; n != nframes; n++) { 1520 1521 offset1 = offset0; 1522 len = usbd_xfer_frame_len(xfer, n); 1523 1524 while (len > 0) { 1525 1526 m = (ch->end - ch->cur); 1527 1528 if (m > len) 1529 m = len; 1530 1531 usbd_copy_out(pc, offset1, ch->cur, m); 1532 1533 len -= m; 1534 offset1 += m; 1535 ch->cur += m; 1536 1537 if (ch->cur >= ch->end) { 1538 ch->cur = ch->start; 1539 } 1540 } 1541 1542 offset0 += mfl; 1543 } 1544 1545 chn_intr(ch->pcm_ch); 1546 1547 case USB_ST_SETUP: 1548 tr_setup: 1549 blockcount = ch->intr_frames; 1550 1551 usbd_xfer_set_frames(xfer, blockcount); 1552 for (n = 0; n < blockcount; n++) { 1553 usbd_xfer_set_frame_len(xfer, n, mfl); 1554 } 1555 1556 usbd_transfer_submit(xfer); 1557 break; 1558 1559 default: /* Error */ 1560 if (error == USB_ERR_CANCELLED) { 1561 break; 1562 } 1563 goto tr_setup; 1564 } 1565 } 1566 1567 void * 1568 uaudio_chan_init(struct uaudio_softc *sc, struct snd_dbuf *b, 1569 struct pcm_channel *c, int dir) 1570 { 1571 struct uaudio_chan *ch = ((dir == PCMDIR_PLAY) ? 1572 &sc->sc_play_chan : &sc->sc_rec_chan); 1573 uint32_t buf_size; 1574 uint32_t frames; 1575 uint32_t format; 1576 uint16_t fps; 1577 uint8_t endpoint; 1578 uint8_t blocks; 1579 uint8_t iface_index; 1580 uint8_t alt_index; 1581 uint8_t fps_shift; 1582 usb_error_t err; 1583 1584 fps = usbd_get_isoc_fps(sc->sc_udev); 1585 1586 if (fps < 8000) { 1587 /* FULL speed USB */ 1588 frames = 8; 1589 } else { 1590 /* HIGH speed USB */ 1591 frames = UAUDIO_NFRAMES; 1592 } 1593 1594 /* setup play/record format */ 1595 1596 ch->pcm_cap.fmtlist = ch->pcm_format; 1597 1598 ch->pcm_format[0] = 0; 1599 ch->pcm_format[1] = 0; 1600 1601 ch->pcm_cap.minspeed = ch->sample_rate; 1602 ch->pcm_cap.maxspeed = ch->sample_rate; 1603 1604 /* setup mutex and PCM channel */ 1605 1606 ch->pcm_ch = c; 1607 ch->pcm_mtx = c->lock; 1608 1609 format = ch->p_fmt->freebsd_fmt; 1610 1611 switch (ch->channels) { 1612 case 2: 1613 /* stereo */ 1614 format = SND_FORMAT(format, 2, 0); 1615 break; 1616 case 1: 1617 /* mono */ 1618 format = SND_FORMAT(format, 1, 0); 1619 break; 1620 default: 1621 /* surround and more */ 1622 format = feeder_matrix_default_format( 1623 SND_FORMAT(format, ch->channels, 0)); 1624 break; 1625 } 1626 1627 ch->pcm_cap.fmtlist[0] = format; 1628 ch->pcm_cap.fmtlist[1] = 0; 1629 1630 /* check if format is not supported */ 1631 1632 if (format == 0) { 1633 DPRINTF("The selected audio format is not supported\n"); 1634 goto error; 1635 } 1636 1637 /* set alternate interface corresponding to the mode */ 1638 1639 endpoint = ch->p_ed1->bEndpointAddress; 1640 iface_index = ch->iface_index; 1641 alt_index = ch->iface_alt_index; 1642 1643 DPRINTF("endpoint=0x%02x, speed=%d, iface=%d alt=%d\n", 1644 endpoint, ch->sample_rate, iface_index, alt_index); 1645 1646 err = usbd_set_alt_interface_index(sc->sc_udev, iface_index, alt_index); 1647 if (err) { 1648 DPRINTF("setting of alternate index failed: %s!\n", 1649 usbd_errstr(err)); 1650 goto error; 1651 } 1652 usbd_set_parent_iface(sc->sc_udev, iface_index, 1653 sc->sc_mixer_iface_index); 1654 1655 /* 1656 * Only set the sample rate if the channel reports that it 1657 * supports the frequency control. 1658 */ 1659 1660 if (sc->sc_audio_rev >= UAUDIO_VERSION_30) { 1661 /* FALLTHROUGH */ 1662 } else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) { 1663 unsigned int x; 1664 1665 for (x = 0; x != 256; x++) { 1666 if (dir == PCMDIR_PLAY) { 1667 if (!(sc->sc_mixer_clocks.bit_output[x / 8] & 1668 (1 << (x % 8)))) { 1669 continue; 1670 } 1671 } else { 1672 if (!(sc->sc_mixer_clocks.bit_input[x / 8] & 1673 (1 << (x % 8)))) { 1674 continue; 1675 } 1676 } 1677 1678 if (uaudio20_set_speed(sc->sc_udev, 1679 sc->sc_mixer_iface_no, x, ch->sample_rate)) { 1680 /* 1681 * If the endpoint is adaptive setting 1682 * the speed may fail. 1683 */ 1684 DPRINTF("setting of sample rate failed! " 1685 "(continuing anyway)\n"); 1686 } 1687 } 1688 } else if (ch->p_sed.v1->bmAttributes & UA_SED_FREQ_CONTROL) { 1689 if (uaudio_set_speed(sc->sc_udev, endpoint, ch->sample_rate)) { 1690 /* 1691 * If the endpoint is adaptive setting the 1692 * speed may fail. 1693 */ 1694 DPRINTF("setting of sample rate failed! " 1695 "(continuing anyway)\n"); 1696 } 1697 } 1698 if (usbd_transfer_setup(sc->sc_udev, &iface_index, ch->xfer, 1699 ch->usb_cfg, UAUDIO_NCHANBUFS, ch, ch->pcm_mtx)) { 1700 DPRINTF("could not allocate USB transfers!\n"); 1701 goto error; 1702 } 1703 1704 fps_shift = usbd_xfer_get_fps_shift(ch->xfer[0]); 1705 1706 /* down shift number of frames per second, if any */ 1707 fps >>= fps_shift; 1708 frames >>= fps_shift; 1709 1710 /* bytes per frame should not be zero */ 1711 ch->bytes_per_frame[0] = ((ch->sample_rate / fps) * ch->sample_size); 1712 ch->bytes_per_frame[1] = (((ch->sample_rate + fps - 1) / fps) * ch->sample_size); 1713 1714 /* setup data rate dithering, if any */ 1715 ch->frames_per_second = fps; 1716 ch->sample_rem = ch->sample_rate % fps; 1717 ch->sample_curr = 0; 1718 ch->frames_per_second = fps; 1719 1720 /* compute required buffer size */ 1721 buf_size = (ch->bytes_per_frame[1] * frames); 1722 1723 ch->intr_size = buf_size; 1724 ch->intr_frames = frames; 1725 1726 DPRINTF("fps=%d sample_rem=%d\n", fps, ch->sample_rem); 1727 1728 if (ch->intr_frames == 0) { 1729 DPRINTF("frame shift is too high!\n"); 1730 goto error; 1731 } 1732 1733 /* setup double buffering */ 1734 buf_size *= 2; 1735 blocks = 2; 1736 1737 ch->buf = malloc(buf_size, M_DEVBUF, M_WAITOK | M_ZERO); 1738 if (ch->buf == NULL) 1739 goto error; 1740 if (sndbuf_setup(b, ch->buf, buf_size) != 0) 1741 goto error; 1742 if (sndbuf_resize(b, blocks, ch->intr_size)) 1743 goto error; 1744 1745 ch->start = ch->buf; 1746 ch->end = ch->buf + buf_size; 1747 ch->cur = ch->buf; 1748 ch->pcm_buf = b; 1749 1750 if (ch->pcm_mtx == NULL) { 1751 DPRINTF("ERROR: PCM channels does not have a mutex!\n"); 1752 goto error; 1753 } 1754 1755 return (ch); 1756 1757 error: 1758 uaudio_chan_free(ch); 1759 return (NULL); 1760 } 1761 1762 int 1763 uaudio_chan_free(struct uaudio_chan *ch) 1764 { 1765 if (ch->buf != NULL) { 1766 free(ch->buf, M_DEVBUF); 1767 ch->buf = NULL; 1768 } 1769 usbd_transfer_unsetup(ch->xfer, UAUDIO_NCHANBUFS); 1770 1771 ch->valid = 0; 1772 1773 return (0); 1774 } 1775 1776 int 1777 uaudio_chan_set_param_blocksize(struct uaudio_chan *ch, uint32_t blocksize) 1778 { 1779 return (ch->intr_size); 1780 } 1781 1782 int 1783 uaudio_chan_set_param_fragments(struct uaudio_chan *ch, uint32_t blocksize, 1784 uint32_t blockcount) 1785 { 1786 return (1); 1787 } 1788 1789 int 1790 uaudio_chan_set_param_speed(struct uaudio_chan *ch, uint32_t speed) 1791 { 1792 if (speed != ch->sample_rate) { 1793 DPRINTF("rate conversion required\n"); 1794 } 1795 return (ch->sample_rate); 1796 } 1797 1798 int 1799 uaudio_chan_getptr(struct uaudio_chan *ch) 1800 { 1801 return (ch->cur - ch->start); 1802 } 1803 1804 struct pcmchan_caps * 1805 uaudio_chan_getcaps(struct uaudio_chan *ch) 1806 { 1807 return (&ch->pcm_cap); 1808 } 1809 1810 static struct pcmchan_matrix uaudio_chan_matrix_swap_2_0 = { 1811 .id = SND_CHN_MATRIX_DRV, 1812 .channels = 2, 1813 .ext = 0, 1814 .map = { 1815 /* Right */ 1816 [0] = { 1817 .type = SND_CHN_T_FR, 1818 .members = 1819 SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FC | 1820 SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BR | 1821 SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SR 1822 }, 1823 /* Left */ 1824 [1] = { 1825 .type = SND_CHN_T_FL, 1826 .members = 1827 SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FC | 1828 SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BL | 1829 SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SL 1830 }, 1831 [2] = { 1832 .type = SND_CHN_T_MAX, 1833 .members = 0 1834 } 1835 }, 1836 .mask = SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FL, 1837 .offset = { 1, 0, -1, -1, -1, -1, -1, -1, -1, 1838 -1, -1, -1, -1, -1, -1, -1, -1, -1 } 1839 }; 1840 1841 struct pcmchan_matrix * 1842 uaudio_chan_getmatrix(struct uaudio_chan *ch, uint32_t format) 1843 { 1844 struct uaudio_softc *sc; 1845 1846 sc = ch->priv_sc; 1847 1848 if (sc != NULL && sc->sc_uq_audio_swap_lr != 0 && 1849 AFMT_CHANNEL(format) == 2) 1850 return (&uaudio_chan_matrix_swap_2_0); 1851 1852 return (feeder_matrix_format_map(format)); 1853 } 1854 1855 int 1856 uaudio_chan_set_param_format(struct uaudio_chan *ch, uint32_t format) 1857 { 1858 ch->format = format; 1859 return (0); 1860 } 1861 1862 int 1863 uaudio_chan_start(struct uaudio_chan *ch) 1864 { 1865 ch->cur = ch->start; 1866 1867 #if (UAUDIO_NCHANBUFS != 2) 1868 #error "please update code" 1869 #endif 1870 if (ch->xfer[0]) { 1871 usbd_transfer_start(ch->xfer[0]); 1872 } 1873 if (ch->xfer[1]) { 1874 usbd_transfer_start(ch->xfer[1]); 1875 } 1876 return (0); 1877 } 1878 1879 int 1880 uaudio_chan_stop(struct uaudio_chan *ch) 1881 { 1882 #if (UAUDIO_NCHANBUFS != 2) 1883 #error "please update code" 1884 #endif 1885 usbd_transfer_stop(ch->xfer[0]); 1886 usbd_transfer_stop(ch->xfer[1]); 1887 return (0); 1888 } 1889 1890 /*========================================================================* 1891 * AC - Audio Controller - routines 1892 *========================================================================*/ 1893 1894 static void 1895 uaudio_mixer_add_ctl_sub(struct uaudio_softc *sc, struct uaudio_mixer_node *mc) 1896 { 1897 struct uaudio_mixer_node *p_mc_new = 1898 malloc(sizeof(*p_mc_new), M_USBDEV, M_WAITOK); 1899 1900 if (p_mc_new != NULL) { 1901 memcpy(p_mc_new, mc, sizeof(*p_mc_new)); 1902 p_mc_new->next = sc->sc_mixer_root; 1903 sc->sc_mixer_root = p_mc_new; 1904 sc->sc_mixer_count++; 1905 } else { 1906 DPRINTF("out of memory\n"); 1907 } 1908 } 1909 1910 static void 1911 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct uaudio_mixer_node *mc) 1912 { 1913 int32_t res; 1914 1915 if (mc->class < UAC_NCLASSES) { 1916 DPRINTF("adding %s.%d\n", 1917 uac_names[mc->class], mc->ctl); 1918 } else { 1919 DPRINTF("adding %d\n", mc->ctl); 1920 } 1921 1922 if (mc->type == MIX_ON_OFF) { 1923 mc->minval = 0; 1924 mc->maxval = 1; 1925 } else if (mc->type == MIX_SELECTOR) { 1926 } else { 1927 1928 /* determine min and max values */ 1929 1930 mc->minval = uaudio_mixer_get(sc->sc_udev, 1931 sc->sc_audio_rev, GET_MIN, mc); 1932 mc->maxval = uaudio_mixer_get(sc->sc_udev, 1933 sc->sc_audio_rev, GET_MAX, mc); 1934 1935 /* check if max and min was swapped */ 1936 1937 if (mc->maxval < mc->minval) { 1938 res = mc->maxval; 1939 mc->maxval = mc->minval; 1940 mc->minval = res; 1941 } 1942 1943 /* compute value range */ 1944 mc->mul = mc->maxval - mc->minval; 1945 if (mc->mul == 0) 1946 mc->mul = 1; 1947 1948 /* compute value alignment */ 1949 res = uaudio_mixer_get(sc->sc_udev, 1950 sc->sc_audio_rev, GET_RES, mc); 1951 1952 DPRINTF("Resolution = %d\n", (int)res); 1953 } 1954 1955 uaudio_mixer_add_ctl_sub(sc, mc); 1956 1957 #ifdef USB_DEBUG 1958 if (uaudio_debug > 2) { 1959 uint8_t i; 1960 1961 for (i = 0; i < mc->nchan; i++) { 1962 DPRINTF("[mix] wValue=%04x\n", mc->wValue[0]); 1963 } 1964 DPRINTF("[mix] wIndex=%04x type=%d ctl='%d' " 1965 "min=%d max=%d\n", 1966 mc->wIndex, mc->type, mc->ctl, 1967 mc->minval, mc->maxval); 1968 } 1969 #endif 1970 } 1971 1972 static void 1973 uaudio_mixer_add_mixer(struct uaudio_softc *sc, 1974 const struct uaudio_terminal_node *iot, int id) 1975 { 1976 struct uaudio_mixer_node mix; 1977 1978 const struct usb_audio_mixer_unit_0 *d0 = iot[id].u.mu_v1; 1979 const struct usb_audio_mixer_unit_1 *d1; 1980 1981 uint32_t bno; /* bit number */ 1982 uint32_t p; /* bit number accumulator */ 1983 uint32_t mo; /* matching outputs */ 1984 uint32_t mc; /* matching channels */ 1985 uint32_t ichs; /* input channels */ 1986 uint32_t ochs; /* output channels */ 1987 uint32_t c; 1988 uint32_t chs; /* channels */ 1989 uint32_t i; 1990 uint32_t o; 1991 1992 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 1993 d0->bUnitId, d0->bNrInPins); 1994 1995 /* compute the number of input channels */ 1996 1997 ichs = 0; 1998 for (i = 0; i < d0->bNrInPins; i++) { 1999 ichs += uaudio_mixer_get_cluster( 2000 d0->baSourceId[i], iot).bNrChannels; 2001 } 2002 2003 d1 = (const void *)(d0->baSourceId + d0->bNrInPins); 2004 2005 /* and the number of output channels */ 2006 2007 ochs = d1->bNrChannels; 2008 2009 DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs); 2010 2011 memset(&mix, 0, sizeof(mix)); 2012 2013 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 2014 uaudio_mixer_determine_class(&iot[id], &mix); 2015 mix.type = MIX_SIGNED_16; 2016 2017 if (uaudio_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL) 2018 return; 2019 2020 for (p = i = 0; i < d0->bNrInPins; i++) { 2021 chs = uaudio_mixer_get_cluster( 2022 d0->baSourceId[i], iot).bNrChannels; 2023 mc = 0; 2024 for (c = 0; c < chs; c++) { 2025 mo = 0; 2026 for (o = 0; o < ochs; o++) { 2027 bno = ((p + c) * ochs) + o; 2028 if (BIT_TEST(d1->bmControls, bno)) 2029 mo++; 2030 } 2031 if (mo == 1) 2032 mc++; 2033 } 2034 if ((mc == chs) && (chs <= MIX_MAX_CHAN)) { 2035 2036 /* repeat bit-scan */ 2037 2038 mc = 0; 2039 for (c = 0; c < chs; c++) { 2040 for (o = 0; o < ochs; o++) { 2041 bno = ((p + c) * ochs) + o; 2042 if (BIT_TEST(d1->bmControls, bno)) 2043 mix.wValue[mc++] = MAKE_WORD(p + c + 1, o + 1); 2044 } 2045 } 2046 mix.nchan = chs; 2047 uaudio_mixer_add_ctl(sc, &mix); 2048 } 2049 p += chs; 2050 } 2051 } 2052 2053 static void 2054 uaudio20_mixer_add_mixer(struct uaudio_softc *sc, 2055 const struct uaudio_terminal_node *iot, int id) 2056 { 2057 struct uaudio_mixer_node mix; 2058 2059 const struct usb_audio20_mixer_unit_0 *d0 = iot[id].u.mu_v2; 2060 const struct usb_audio20_mixer_unit_1 *d1; 2061 2062 uint32_t bno; /* bit number */ 2063 uint32_t p; /* bit number accumulator */ 2064 uint32_t mo; /* matching outputs */ 2065 uint32_t mc; /* matching channels */ 2066 uint32_t ichs; /* input channels */ 2067 uint32_t ochs; /* output channels */ 2068 uint32_t c; 2069 uint32_t chs; /* channels */ 2070 uint32_t i; 2071 uint32_t o; 2072 2073 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 2074 d0->bUnitId, d0->bNrInPins); 2075 2076 /* compute the number of input channels */ 2077 2078 ichs = 0; 2079 for (i = 0; i < d0->bNrInPins; i++) { 2080 ichs += uaudio20_mixer_get_cluster( 2081 d0->baSourceId[i], iot).bNrChannels; 2082 } 2083 2084 d1 = (const void *)(d0->baSourceId + d0->bNrInPins); 2085 2086 /* and the number of output channels */ 2087 2088 ochs = d1->bNrChannels; 2089 2090 DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs); 2091 2092 memset(&mix, 0, sizeof(mix)); 2093 2094 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 2095 uaudio20_mixer_determine_class(&iot[id], &mix); 2096 mix.type = MIX_SIGNED_16; 2097 2098 if (uaudio20_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL) 2099 return; 2100 2101 for (p = i = 0; i < d0->bNrInPins; i++) { 2102 chs = uaudio20_mixer_get_cluster( 2103 d0->baSourceId[i], iot).bNrChannels; 2104 mc = 0; 2105 for (c = 0; c < chs; c++) { 2106 mo = 0; 2107 for (o = 0; o < ochs; o++) { 2108 bno = ((p + c) * ochs) + o; 2109 if (BIT_TEST(d1->bmControls, bno)) 2110 mo++; 2111 } 2112 if (mo == 1) 2113 mc++; 2114 } 2115 if ((mc == chs) && (chs <= MIX_MAX_CHAN)) { 2116 2117 /* repeat bit-scan */ 2118 2119 mc = 0; 2120 for (c = 0; c < chs; c++) { 2121 for (o = 0; o < ochs; o++) { 2122 bno = ((p + c) * ochs) + o; 2123 if (BIT_TEST(d1->bmControls, bno)) 2124 mix.wValue[mc++] = MAKE_WORD(p + c + 1, o + 1); 2125 } 2126 } 2127 mix.nchan = chs; 2128 uaudio_mixer_add_ctl(sc, &mix); 2129 } 2130 p += chs; 2131 } 2132 } 2133 2134 static void 2135 uaudio_mixer_add_selector(struct uaudio_softc *sc, 2136 const struct uaudio_terminal_node *iot, int id) 2137 { 2138 const struct usb_audio_selector_unit *d = iot[id].u.su_v1; 2139 struct uaudio_mixer_node mix; 2140 uint16_t i; 2141 2142 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 2143 d->bUnitId, d->bNrInPins); 2144 2145 if (d->bNrInPins == 0) { 2146 return; 2147 } 2148 memset(&mix, 0, sizeof(mix)); 2149 2150 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 2151 mix.wValue[0] = MAKE_WORD(0, 0); 2152 uaudio_mixer_determine_class(&iot[id], &mix); 2153 mix.nchan = 1; 2154 mix.type = MIX_SELECTOR; 2155 2156 mix.ctl = SOUND_MIXER_NRDEVICES; 2157 mix.minval = 1; 2158 mix.maxval = d->bNrInPins; 2159 2160 if (mix.maxval > MAX_SELECTOR_INPUT_PIN) { 2161 mix.maxval = MAX_SELECTOR_INPUT_PIN; 2162 } 2163 mix.mul = (mix.maxval - mix.minval); 2164 for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) { 2165 mix.slctrtype[i] = SOUND_MIXER_NRDEVICES; 2166 } 2167 2168 for (i = 0; i < mix.maxval; i++) { 2169 mix.slctrtype[i] = uaudio_mixer_feature_name( 2170 &iot[d->baSourceId[i]], &mix); 2171 } 2172 2173 mix.class = 0; /* not used */ 2174 2175 uaudio_mixer_add_ctl(sc, &mix); 2176 } 2177 2178 static void 2179 uaudio20_mixer_add_selector(struct uaudio_softc *sc, 2180 const struct uaudio_terminal_node *iot, int id) 2181 { 2182 const struct usb_audio20_selector_unit *d = iot[id].u.su_v2; 2183 struct uaudio_mixer_node mix; 2184 uint16_t i; 2185 2186 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 2187 d->bUnitId, d->bNrInPins); 2188 2189 if (d->bNrInPins == 0) 2190 return; 2191 2192 memset(&mix, 0, sizeof(mix)); 2193 2194 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 2195 mix.wValue[0] = MAKE_WORD(0, 0); 2196 uaudio20_mixer_determine_class(&iot[id], &mix); 2197 mix.nchan = 1; 2198 mix.type = MIX_SELECTOR; 2199 2200 mix.ctl = SOUND_MIXER_NRDEVICES; 2201 mix.minval = 1; 2202 mix.maxval = d->bNrInPins; 2203 2204 if (mix.maxval > MAX_SELECTOR_INPUT_PIN) 2205 mix.maxval = MAX_SELECTOR_INPUT_PIN; 2206 2207 mix.mul = (mix.maxval - mix.minval); 2208 for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) 2209 mix.slctrtype[i] = SOUND_MIXER_NRDEVICES; 2210 2211 for (i = 0; i < mix.maxval; i++) { 2212 mix.slctrtype[i] = uaudio20_mixer_feature_name( 2213 &iot[d->baSourceId[i]], &mix); 2214 } 2215 2216 mix.class = 0; /* not used */ 2217 2218 uaudio_mixer_add_ctl(sc, &mix); 2219 } 2220 2221 static uint32_t 2222 uaudio_mixer_feature_get_bmaControls(const struct usb_audio_feature_unit *d, 2223 uint8_t i) 2224 { 2225 uint32_t temp = 0; 2226 uint32_t offset = (i * d->bControlSize); 2227 2228 if (d->bControlSize > 0) { 2229 temp |= d->bmaControls[offset]; 2230 if (d->bControlSize > 1) { 2231 temp |= d->bmaControls[offset + 1] << 8; 2232 if (d->bControlSize > 2) { 2233 temp |= d->bmaControls[offset + 2] << 16; 2234 if (d->bControlSize > 3) { 2235 temp |= d->bmaControls[offset + 3] << 24; 2236 } 2237 } 2238 } 2239 } 2240 return (temp); 2241 } 2242 2243 static void 2244 uaudio_mixer_add_feature(struct uaudio_softc *sc, 2245 const struct uaudio_terminal_node *iot, int id) 2246 { 2247 const struct usb_audio_feature_unit *d = iot[id].u.fu_v1; 2248 struct uaudio_mixer_node mix; 2249 uint32_t fumask; 2250 uint32_t mmask; 2251 uint32_t cmask; 2252 uint16_t mixernumber; 2253 uint8_t nchan; 2254 uint8_t chan; 2255 uint8_t ctl; 2256 uint8_t i; 2257 2258 if (d->bControlSize == 0) { 2259 return; 2260 } 2261 memset(&mix, 0, sizeof(mix)); 2262 2263 nchan = (d->bLength - 7) / d->bControlSize; 2264 mmask = uaudio_mixer_feature_get_bmaControls(d, 0); 2265 cmask = 0; 2266 2267 if (nchan == 0) { 2268 return; 2269 } 2270 /* figure out what we can control */ 2271 2272 for (chan = 1; chan < nchan; chan++) { 2273 DPRINTFN(10, "chan=%d mask=%x\n", 2274 chan, uaudio_mixer_feature_get_bmaControls(d, chan)); 2275 2276 cmask |= uaudio_mixer_feature_get_bmaControls(d, chan); 2277 } 2278 2279 if (nchan > MIX_MAX_CHAN) { 2280 nchan = MIX_MAX_CHAN; 2281 } 2282 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 2283 2284 for (ctl = 1; ctl <= LOUDNESS_CONTROL; ctl++) { 2285 2286 fumask = FU_MASK(ctl); 2287 2288 DPRINTFN(5, "ctl=%d fumask=0x%04x\n", 2289 ctl, fumask); 2290 2291 if (mmask & fumask) { 2292 mix.nchan = 1; 2293 mix.wValue[0] = MAKE_WORD(ctl, 0); 2294 } else if (cmask & fumask) { 2295 mix.nchan = nchan - 1; 2296 for (i = 1; i < nchan; i++) { 2297 if (uaudio_mixer_feature_get_bmaControls(d, i) & fumask) 2298 mix.wValue[i - 1] = MAKE_WORD(ctl, i); 2299 else 2300 mix.wValue[i - 1] = -1; 2301 } 2302 } else { 2303 continue; 2304 } 2305 2306 mixernumber = uaudio_mixer_feature_name(&iot[id], &mix); 2307 2308 switch (ctl) { 2309 case MUTE_CONTROL: 2310 mix.type = MIX_ON_OFF; 2311 mix.ctl = SOUND_MIXER_NRDEVICES; 2312 break; 2313 2314 case VOLUME_CONTROL: 2315 mix.type = MIX_SIGNED_16; 2316 mix.ctl = mixernumber; 2317 break; 2318 2319 case BASS_CONTROL: 2320 mix.type = MIX_SIGNED_8; 2321 mix.ctl = SOUND_MIXER_BASS; 2322 break; 2323 2324 case MID_CONTROL: 2325 mix.type = MIX_SIGNED_8; 2326 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2327 break; 2328 2329 case TREBLE_CONTROL: 2330 mix.type = MIX_SIGNED_8; 2331 mix.ctl = SOUND_MIXER_TREBLE; 2332 break; 2333 2334 case GRAPHIC_EQUALIZER_CONTROL: 2335 continue; /* XXX don't add anything */ 2336 break; 2337 2338 case AGC_CONTROL: 2339 mix.type = MIX_ON_OFF; 2340 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2341 break; 2342 2343 case DELAY_CONTROL: 2344 mix.type = MIX_UNSIGNED_16; 2345 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2346 break; 2347 2348 case BASS_BOOST_CONTROL: 2349 mix.type = MIX_ON_OFF; 2350 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2351 break; 2352 2353 case LOUDNESS_CONTROL: 2354 mix.type = MIX_ON_OFF; 2355 mix.ctl = SOUND_MIXER_LOUD; /* Is this correct ? */ 2356 break; 2357 2358 default: 2359 mix.type = MIX_UNKNOWN; 2360 break; 2361 } 2362 2363 if (mix.type != MIX_UNKNOWN) 2364 uaudio_mixer_add_ctl(sc, &mix); 2365 } 2366 } 2367 2368 static void 2369 uaudio20_mixer_add_feature(struct uaudio_softc *sc, 2370 const struct uaudio_terminal_node *iot, int id) 2371 { 2372 const struct usb_audio20_feature_unit *d = iot[id].u.fu_v2; 2373 struct uaudio_mixer_node mix; 2374 uint32_t ctl; 2375 uint32_t mmask; 2376 uint32_t cmask; 2377 uint16_t mixernumber; 2378 uint8_t nchan; 2379 uint8_t chan; 2380 uint8_t i; 2381 uint8_t what; 2382 2383 if (UGETDW(d->bmaControls[0]) == 0) 2384 return; 2385 2386 memset(&mix, 0, sizeof(mix)); 2387 2388 nchan = (d->bLength - 6) / 4; 2389 mmask = UGETDW(d->bmaControls[0]); 2390 cmask = 0; 2391 2392 if (nchan == 0) 2393 return; 2394 2395 /* figure out what we can control */ 2396 2397 for (chan = 1; chan < nchan; chan++) 2398 cmask |= UGETDW(d->bmaControls[chan]); 2399 2400 if (nchan > MIX_MAX_CHAN) 2401 nchan = MIX_MAX_CHAN; 2402 2403 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 2404 2405 for (ctl = 3; ctl != 0; ctl <<= 2) { 2406 2407 mixernumber = uaudio20_mixer_feature_name(&iot[id], &mix); 2408 2409 switch (ctl) { 2410 case (3 << 0): 2411 mix.type = MIX_ON_OFF; 2412 mix.ctl = SOUND_MIXER_NRDEVICES; 2413 what = MUTE_CONTROL; 2414 break; 2415 case (3 << 2): 2416 mix.type = MIX_SIGNED_16; 2417 mix.ctl = mixernumber; 2418 what = VOLUME_CONTROL; 2419 break; 2420 case (3 << 4): 2421 mix.type = MIX_SIGNED_8; 2422 mix.ctl = SOUND_MIXER_BASS; 2423 what = BASS_CONTROL; 2424 break; 2425 case (3 << 6): 2426 mix.type = MIX_SIGNED_8; 2427 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2428 what = MID_CONTROL; 2429 break; 2430 case (3 << 8): 2431 mix.type = MIX_SIGNED_8; 2432 mix.ctl = SOUND_MIXER_TREBLE; 2433 what = TREBLE_CONTROL; 2434 break; 2435 case (3 << 12): 2436 mix.type = MIX_ON_OFF; 2437 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2438 what = AGC_CONTROL; 2439 break; 2440 case (3 << 14): 2441 mix.type = MIX_UNSIGNED_16; 2442 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2443 what = DELAY_CONTROL; 2444 break; 2445 case (3 << 16): 2446 mix.type = MIX_ON_OFF; 2447 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 2448 what = BASS_BOOST_CONTROL; 2449 break; 2450 case (3 << 18): 2451 mix.type = MIX_ON_OFF; 2452 mix.ctl = SOUND_MIXER_LOUD; /* Is this correct ? */ 2453 what = LOUDNESS_CONTROL; 2454 break; 2455 case (3 << 20): 2456 mix.type = MIX_SIGNED_16; 2457 mix.ctl = mixernumber; 2458 what = INPUT_GAIN_CONTROL; 2459 break; 2460 case (3 << 22): 2461 mix.type = MIX_SIGNED_16; 2462 mix.ctl = mixernumber; 2463 what = INPUT_GAIN_PAD_CONTROL; 2464 break; 2465 default: 2466 continue; 2467 } 2468 2469 if ((mmask & ctl) == ctl) { 2470 mix.nchan = 1; 2471 mix.wValue[0] = MAKE_WORD(what, 0); 2472 } else if ((cmask & ctl) == ctl) { 2473 mix.nchan = nchan - 1; 2474 for (i = 1; i < nchan; i++) { 2475 if ((UGETDW(d->bmaControls[i]) & ctl) == ctl) 2476 mix.wValue[i - 1] = MAKE_WORD(what, i); 2477 else 2478 mix.wValue[i - 1] = -1; 2479 } 2480 } else { 2481 continue; 2482 } 2483 2484 if (mix.type != MIX_UNKNOWN) 2485 uaudio_mixer_add_ctl(sc, &mix); 2486 } 2487 } 2488 2489 static void 2490 uaudio_mixer_add_processing_updown(struct uaudio_softc *sc, 2491 const struct uaudio_terminal_node *iot, int id) 2492 { 2493 const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1; 2494 const struct usb_audio_processing_unit_1 *d1 = 2495 (const void *)(d0->baSourceId + d0->bNrInPins); 2496 const struct usb_audio_processing_unit_updown *ud = 2497 (const void *)(d1->bmControls + d1->bControlSize); 2498 struct uaudio_mixer_node mix; 2499 uint8_t i; 2500 2501 if (uaudio_mixer_verify_desc(d0, sizeof(*ud)) == NULL) { 2502 return; 2503 } 2504 if (uaudio_mixer_verify_desc(d0, sizeof(*ud) + (2 * ud->bNrModes)) 2505 == NULL) { 2506 return; 2507 } 2508 DPRINTFN(3, "bUnitId=%d bNrModes=%d\n", 2509 d0->bUnitId, ud->bNrModes); 2510 2511 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) { 2512 DPRINTF("no mode select\n"); 2513 return; 2514 } 2515 memset(&mix, 0, sizeof(mix)); 2516 2517 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 2518 mix.nchan = 1; 2519 mix.wValue[0] = MAKE_WORD(UD_MODE_SELECT_CONTROL, 0); 2520 uaudio_mixer_determine_class(&iot[id], &mix); 2521 mix.type = MIX_ON_OFF; /* XXX */ 2522 2523 for (i = 0; i < ud->bNrModes; i++) { 2524 DPRINTFN(3, "i=%d bm=0x%x\n", i, UGETW(ud->waModes[i])); 2525 /* XXX */ 2526 } 2527 2528 uaudio_mixer_add_ctl(sc, &mix); 2529 } 2530 2531 static void 2532 uaudio_mixer_add_processing(struct uaudio_softc *sc, 2533 const struct uaudio_terminal_node *iot, int id) 2534 { 2535 const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu_v1; 2536 const struct usb_audio_processing_unit_1 *d1 = 2537 (const void *)(d0->baSourceId + d0->bNrInPins); 2538 struct uaudio_mixer_node mix; 2539 uint16_t ptype; 2540 2541 memset(&mix, 0, sizeof(mix)); 2542 2543 ptype = UGETW(d0->wProcessType); 2544 2545 DPRINTFN(3, "wProcessType=%d bUnitId=%d " 2546 "bNrInPins=%d\n", ptype, d0->bUnitId, d0->bNrInPins); 2547 2548 if (d1->bControlSize == 0) { 2549 return; 2550 } 2551 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) { 2552 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 2553 mix.nchan = 1; 2554 mix.wValue[0] = MAKE_WORD(XX_ENABLE_CONTROL, 0); 2555 uaudio_mixer_determine_class(&iot[id], &mix); 2556 mix.type = MIX_ON_OFF; 2557 uaudio_mixer_add_ctl(sc, &mix); 2558 } 2559 switch (ptype) { 2560 case UPDOWNMIX_PROCESS: 2561 uaudio_mixer_add_processing_updown(sc, iot, id); 2562 break; 2563 2564 case DOLBY_PROLOGIC_PROCESS: 2565 case P3D_STEREO_EXTENDER_PROCESS: 2566 case REVERBATION_PROCESS: 2567 case CHORUS_PROCESS: 2568 case DYN_RANGE_COMP_PROCESS: 2569 default: 2570 DPRINTF("unit %d, type=%d is not implemented\n", 2571 d0->bUnitId, ptype); 2572 break; 2573 } 2574 } 2575 2576 static void 2577 uaudio_mixer_add_extension(struct uaudio_softc *sc, 2578 const struct uaudio_terminal_node *iot, int id) 2579 { 2580 const struct usb_audio_extension_unit_0 *d0 = iot[id].u.eu_v1; 2581 const struct usb_audio_extension_unit_1 *d1 = 2582 (const void *)(d0->baSourceId + d0->bNrInPins); 2583 struct uaudio_mixer_node mix; 2584 2585 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 2586 d0->bUnitId, d0->bNrInPins); 2587 2588 if (sc->sc_uq_au_no_xu) { 2589 return; 2590 } 2591 if (d1->bControlSize == 0) { 2592 return; 2593 } 2594 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) { 2595 2596 memset(&mix, 0, sizeof(mix)); 2597 2598 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 2599 mix.nchan = 1; 2600 mix.wValue[0] = MAKE_WORD(UA_EXT_ENABLE, 0); 2601 uaudio_mixer_determine_class(&iot[id], &mix); 2602 mix.type = MIX_ON_OFF; 2603 2604 uaudio_mixer_add_ctl(sc, &mix); 2605 } 2606 } 2607 2608 static const void * 2609 uaudio_mixer_verify_desc(const void *arg, uint32_t len) 2610 { 2611 const struct usb_audio_mixer_unit_1 *d1; 2612 const struct usb_audio_extension_unit_1 *e1; 2613 const struct usb_audio_processing_unit_1 *u1; 2614 2615 union { 2616 const struct usb_descriptor *desc; 2617 const struct usb_audio_input_terminal *it; 2618 const struct usb_audio_output_terminal *ot; 2619 const struct usb_audio_mixer_unit_0 *mu; 2620 const struct usb_audio_selector_unit *su; 2621 const struct usb_audio_feature_unit *fu; 2622 const struct usb_audio_processing_unit_0 *pu; 2623 const struct usb_audio_extension_unit_0 *eu; 2624 } u; 2625 2626 u.desc = arg; 2627 2628 if (u.desc == NULL) { 2629 goto error; 2630 } 2631 if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) { 2632 goto error; 2633 } 2634 switch (u.desc->bDescriptorSubtype) { 2635 case UDESCSUB_AC_INPUT: 2636 len += sizeof(*u.it); 2637 break; 2638 2639 case UDESCSUB_AC_OUTPUT: 2640 len += sizeof(*u.ot); 2641 break; 2642 2643 case UDESCSUB_AC_MIXER: 2644 len += sizeof(*u.mu); 2645 2646 if (u.desc->bLength < len) { 2647 goto error; 2648 } 2649 len += u.mu->bNrInPins; 2650 2651 if (u.desc->bLength < len) { 2652 goto error; 2653 } 2654 d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins); 2655 2656 len += sizeof(*d1); 2657 break; 2658 2659 case UDESCSUB_AC_SELECTOR: 2660 len += sizeof(*u.su); 2661 2662 if (u.desc->bLength < len) { 2663 goto error; 2664 } 2665 len += u.su->bNrInPins; 2666 break; 2667 2668 case UDESCSUB_AC_FEATURE: 2669 len += (sizeof(*u.fu) + 1); 2670 break; 2671 2672 case UDESCSUB_AC_PROCESSING: 2673 len += sizeof(*u.pu); 2674 2675 if (u.desc->bLength < len) { 2676 goto error; 2677 } 2678 len += u.pu->bNrInPins; 2679 2680 if (u.desc->bLength < len) { 2681 goto error; 2682 } 2683 u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins); 2684 2685 len += sizeof(*u1); 2686 2687 if (u.desc->bLength < len) { 2688 goto error; 2689 } 2690 len += u1->bControlSize; 2691 2692 break; 2693 2694 case UDESCSUB_AC_EXTENSION: 2695 len += sizeof(*u.eu); 2696 2697 if (u.desc->bLength < len) { 2698 goto error; 2699 } 2700 len += u.eu->bNrInPins; 2701 2702 if (u.desc->bLength < len) { 2703 goto error; 2704 } 2705 e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins); 2706 2707 len += sizeof(*e1); 2708 2709 if (u.desc->bLength < len) { 2710 goto error; 2711 } 2712 len += e1->bControlSize; 2713 break; 2714 2715 default: 2716 goto error; 2717 } 2718 2719 if (u.desc->bLength < len) { 2720 goto error; 2721 } 2722 return (u.desc); 2723 2724 error: 2725 if (u.desc) { 2726 DPRINTF("invalid descriptor, type=%d, " 2727 "sub_type=%d, len=%d of %d bytes\n", 2728 u.desc->bDescriptorType, 2729 u.desc->bDescriptorSubtype, 2730 u.desc->bLength, len); 2731 } 2732 return (NULL); 2733 } 2734 2735 static const void * 2736 uaudio20_mixer_verify_desc(const void *arg, uint32_t len) 2737 { 2738 const struct usb_audio20_mixer_unit_1 *d1; 2739 const struct usb_audio20_extension_unit_1 *e1; 2740 const struct usb_audio20_processing_unit_1 *u1; 2741 const struct usb_audio20_clock_selector_unit_1 *c1; 2742 2743 union { 2744 const struct usb_descriptor *desc; 2745 const struct usb_audio20_clock_source_unit *csrc; 2746 const struct usb_audio20_clock_selector_unit_0 *csel; 2747 const struct usb_audio20_clock_multiplier_unit *cmul; 2748 const struct usb_audio20_input_terminal *it; 2749 const struct usb_audio20_output_terminal *ot; 2750 const struct usb_audio20_mixer_unit_0 *mu; 2751 const struct usb_audio20_selector_unit *su; 2752 const struct usb_audio20_feature_unit *fu; 2753 const struct usb_audio20_sample_rate_unit *ru; 2754 const struct usb_audio20_processing_unit_0 *pu; 2755 const struct usb_audio20_extension_unit_0 *eu; 2756 const struct usb_audio20_effect_unit *ef; 2757 } u; 2758 2759 u.desc = arg; 2760 2761 if (u.desc == NULL) 2762 goto error; 2763 2764 if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) 2765 goto error; 2766 2767 switch (u.desc->bDescriptorSubtype) { 2768 case UDESCSUB_AC_INPUT: 2769 len += sizeof(*u.it); 2770 break; 2771 2772 case UDESCSUB_AC_OUTPUT: 2773 len += sizeof(*u.ot); 2774 break; 2775 2776 case UDESCSUB_AC_MIXER: 2777 len += sizeof(*u.mu); 2778 2779 if (u.desc->bLength < len) 2780 goto error; 2781 len += u.mu->bNrInPins; 2782 2783 if (u.desc->bLength < len) 2784 goto error; 2785 2786 d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins); 2787 2788 len += sizeof(*d1) + d1->bNrChannels; 2789 break; 2790 2791 case UDESCSUB_AC_SELECTOR: 2792 len += sizeof(*u.su); 2793 2794 if (u.desc->bLength < len) 2795 goto error; 2796 2797 len += u.su->bNrInPins; 2798 break; 2799 2800 case UDESCSUB_AC_FEATURE: 2801 len += sizeof(*u.fu) + 1; 2802 2803 if (u.desc->bLength < len) 2804 goto error; 2805 break; 2806 2807 case UDESCSUB_AC_EFFECT: 2808 len += sizeof(*u.ef) + 4; 2809 break; 2810 2811 case UDESCSUB_AC_PROCESSING_V2: 2812 len += sizeof(*u.pu); 2813 2814 if (u.desc->bLength < len) 2815 goto error; 2816 2817 len += u.pu->bNrInPins; 2818 2819 if (u.desc->bLength < len) 2820 goto error; 2821 2822 u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins); 2823 2824 len += sizeof(*u1); 2825 break; 2826 2827 case UDESCSUB_AC_EXTENSION_V2: 2828 len += sizeof(*u.eu); 2829 2830 if (u.desc->bLength < len) 2831 goto error; 2832 2833 len += u.eu->bNrInPins; 2834 2835 if (u.desc->bLength < len) 2836 goto error; 2837 2838 e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins); 2839 2840 len += sizeof(*e1); 2841 break; 2842 2843 case UDESCSUB_AC_CLOCK_SRC: 2844 len += sizeof(*u.csrc); 2845 break; 2846 2847 case UDESCSUB_AC_CLOCK_SEL: 2848 len += sizeof(*u.csel); 2849 2850 if (u.desc->bLength < len) 2851 goto error; 2852 2853 len += u.csel->bNrInPins; 2854 2855 if (u.desc->bLength < len) 2856 goto error; 2857 2858 c1 = (const void *)(u.csel->baCSourceId + u.csel->bNrInPins); 2859 2860 len += sizeof(*c1); 2861 break; 2862 2863 case UDESCSUB_AC_CLOCK_MUL: 2864 len += sizeof(*u.cmul); 2865 break; 2866 2867 case UDESCSUB_AC_SAMPLE_RT: 2868 len += sizeof(*u.ru); 2869 break; 2870 2871 default: 2872 goto error; 2873 } 2874 2875 if (u.desc->bLength < len) 2876 goto error; 2877 2878 return (u.desc); 2879 2880 error: 2881 if (u.desc) { 2882 DPRINTF("invalid descriptor, type=%d, " 2883 "sub_type=%d, len=%d of %d bytes\n", 2884 u.desc->bDescriptorType, 2885 u.desc->bDescriptorSubtype, 2886 u.desc->bLength, len); 2887 } 2888 return (NULL); 2889 } 2890 2891 static struct usb_audio_cluster 2892 uaudio_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot) 2893 { 2894 struct usb_audio_cluster r; 2895 const struct usb_descriptor *dp; 2896 uint8_t i; 2897 2898 for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) { /* avoid infinite loops */ 2899 dp = iot[id].u.desc; 2900 if (dp == NULL) { 2901 goto error; 2902 } 2903 switch (dp->bDescriptorSubtype) { 2904 case UDESCSUB_AC_INPUT: 2905 r.bNrChannels = iot[id].u.it_v1->bNrChannels; 2906 r.wChannelConfig[0] = iot[id].u.it_v1->wChannelConfig[0]; 2907 r.wChannelConfig[1] = iot[id].u.it_v1->wChannelConfig[1]; 2908 r.iChannelNames = iot[id].u.it_v1->iChannelNames; 2909 goto done; 2910 2911 case UDESCSUB_AC_OUTPUT: 2912 id = iot[id].u.ot_v1->bSourceId; 2913 break; 2914 2915 case UDESCSUB_AC_MIXER: 2916 r = *(const struct usb_audio_cluster *) 2917 &iot[id].u.mu_v1->baSourceId[ 2918 iot[id].u.mu_v1->bNrInPins]; 2919 goto done; 2920 2921 case UDESCSUB_AC_SELECTOR: 2922 if (iot[id].u.su_v1->bNrInPins > 0) { 2923 /* XXX This is not really right */ 2924 id = iot[id].u.su_v1->baSourceId[0]; 2925 } 2926 break; 2927 2928 case UDESCSUB_AC_FEATURE: 2929 id = iot[id].u.fu_v1->bSourceId; 2930 break; 2931 2932 case UDESCSUB_AC_PROCESSING: 2933 r = *((const struct usb_audio_cluster *) 2934 &iot[id].u.pu_v1->baSourceId[ 2935 iot[id].u.pu_v1->bNrInPins]); 2936 goto done; 2937 2938 case UDESCSUB_AC_EXTENSION: 2939 r = *((const struct usb_audio_cluster *) 2940 &iot[id].u.eu_v1->baSourceId[ 2941 iot[id].u.eu_v1->bNrInPins]); 2942 goto done; 2943 2944 default: 2945 goto error; 2946 } 2947 } 2948 error: 2949 DPRINTF("bad data\n"); 2950 memset(&r, 0, sizeof(r)); 2951 done: 2952 return (r); 2953 } 2954 2955 static struct usb_audio20_cluster 2956 uaudio20_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot) 2957 { 2958 struct usb_audio20_cluster r; 2959 const struct usb_descriptor *dp; 2960 uint8_t i; 2961 2962 for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) { /* avoid infinite loops */ 2963 dp = iot[id].u.desc; 2964 if (dp == NULL) 2965 goto error; 2966 2967 switch (dp->bDescriptorSubtype) { 2968 case UDESCSUB_AC_INPUT: 2969 r.bNrChannels = iot[id].u.it_v2->bNrChannels; 2970 r.bmChannelConfig[0] = iot[id].u.it_v2->bmChannelConfig[0]; 2971 r.bmChannelConfig[1] = iot[id].u.it_v2->bmChannelConfig[1]; 2972 r.bmChannelConfig[2] = iot[id].u.it_v2->bmChannelConfig[2]; 2973 r.bmChannelConfig[3] = iot[id].u.it_v2->bmChannelConfig[3]; 2974 r.iChannelNames = iot[id].u.it_v2->iTerminal; 2975 goto done; 2976 2977 case UDESCSUB_AC_OUTPUT: 2978 id = iot[id].u.ot_v2->bSourceId; 2979 break; 2980 2981 case UDESCSUB_AC_MIXER: 2982 r = *(const struct usb_audio20_cluster *) 2983 &iot[id].u.mu_v2->baSourceId[ 2984 iot[id].u.mu_v2->bNrInPins]; 2985 goto done; 2986 2987 case UDESCSUB_AC_SELECTOR: 2988 if (iot[id].u.su_v2->bNrInPins > 0) { 2989 /* XXX This is not really right */ 2990 id = iot[id].u.su_v2->baSourceId[0]; 2991 } 2992 break; 2993 2994 case UDESCSUB_AC_SAMPLE_RT: 2995 id = iot[id].u.ru_v2->bSourceId; 2996 break; 2997 2998 case UDESCSUB_AC_EFFECT: 2999 id = iot[id].u.ef_v2->bSourceId; 3000 break; 3001 3002 case UDESCSUB_AC_FEATURE: 3003 id = iot[id].u.fu_v2->bSourceId; 3004 break; 3005 3006 case UDESCSUB_AC_PROCESSING_V2: 3007 r = *((const struct usb_audio20_cluster *) 3008 &iot[id].u.pu_v2->baSourceId[ 3009 iot[id].u.pu_v2->bNrInPins]); 3010 goto done; 3011 3012 case UDESCSUB_AC_EXTENSION_V2: 3013 r = *((const struct usb_audio20_cluster *) 3014 &iot[id].u.eu_v2->baSourceId[ 3015 iot[id].u.eu_v2->bNrInPins]); 3016 goto done; 3017 3018 default: 3019 goto error; 3020 } 3021 } 3022 error: 3023 DPRINTF("Bad data!\n"); 3024 memset(&r, 0, sizeof(r)); 3025 done: 3026 return (r); 3027 } 3028 3029 static uint16_t 3030 uaudio_mixer_determine_class(const struct uaudio_terminal_node *iot, 3031 struct uaudio_mixer_node *mix) 3032 { 3033 uint16_t terminal_type = 0x0000; 3034 const struct uaudio_terminal_node *input[2]; 3035 const struct uaudio_terminal_node *output[2]; 3036 3037 input[0] = uaudio_mixer_get_input(iot, 0); 3038 input[1] = uaudio_mixer_get_input(iot, 1); 3039 3040 output[0] = uaudio_mixer_get_output(iot, 0); 3041 output[1] = uaudio_mixer_get_output(iot, 1); 3042 3043 /* 3044 * check if there is only 3045 * one output terminal: 3046 */ 3047 if (output[0] && (!output[1])) { 3048 terminal_type = 3049 UGETW(output[0]->u.ot_v1->wTerminalType); 3050 } 3051 /* 3052 * If the only output terminal is USB, 3053 * the class is UAC_RECORD. 3054 */ 3055 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) { 3056 3057 mix->class = UAC_RECORD; 3058 if (input[0] && (!input[1])) { 3059 terminal_type = 3060 UGETW(input[0]->u.it_v1->wTerminalType); 3061 } else { 3062 terminal_type = 0; 3063 } 3064 goto done; 3065 } 3066 /* 3067 * if the unit is connected to just 3068 * one input terminal, the 3069 * class is UAC_INPUT: 3070 */ 3071 if (input[0] && (!input[1])) { 3072 mix->class = UAC_INPUT; 3073 terminal_type = 3074 UGETW(input[0]->u.it_v1->wTerminalType); 3075 goto done; 3076 } 3077 /* 3078 * Otherwise, the class is UAC_OUTPUT. 3079 */ 3080 mix->class = UAC_OUTPUT; 3081 done: 3082 return (terminal_type); 3083 } 3084 3085 static uint16_t 3086 uaudio20_mixer_determine_class(const struct uaudio_terminal_node *iot, 3087 struct uaudio_mixer_node *mix) 3088 { 3089 uint16_t terminal_type = 0x0000; 3090 const struct uaudio_terminal_node *input[2]; 3091 const struct uaudio_terminal_node *output[2]; 3092 3093 input[0] = uaudio_mixer_get_input(iot, 0); 3094 input[1] = uaudio_mixer_get_input(iot, 1); 3095 3096 output[0] = uaudio_mixer_get_output(iot, 0); 3097 output[1] = uaudio_mixer_get_output(iot, 1); 3098 3099 /* 3100 * check if there is only 3101 * one output terminal: 3102 */ 3103 if (output[0] && (!output[1])) 3104 terminal_type = UGETW(output[0]->u.ot_v2->wTerminalType); 3105 /* 3106 * If the only output terminal is USB, 3107 * the class is UAC_RECORD. 3108 */ 3109 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) { 3110 3111 mix->class = UAC_RECORD; 3112 if (input[0] && (!input[1])) { 3113 terminal_type = 3114 UGETW(input[0]->u.it_v2->wTerminalType); 3115 } else { 3116 terminal_type = 0; 3117 } 3118 goto done; 3119 } 3120 /* 3121 * if the unit is connected to just 3122 * one input terminal, the 3123 * class is UAC_INPUT: 3124 */ 3125 if (input[0] && (!input[1])) { 3126 mix->class = UAC_INPUT; 3127 terminal_type = 3128 UGETW(input[0]->u.it_v2->wTerminalType); 3129 goto done; 3130 } 3131 /* 3132 * Otherwise, the class is UAC_OUTPUT. 3133 */ 3134 mix->class = UAC_OUTPUT; 3135 done: 3136 return (terminal_type); 3137 } 3138 3139 struct uaudio_tt_to_feature { 3140 uint16_t terminal_type; 3141 uint16_t feature; 3142 }; 3143 3144 static const struct uaudio_tt_to_feature uaudio_tt_to_feature[] = { 3145 3146 {UAT_STREAM, SOUND_MIXER_PCM}, 3147 3148 {UATI_MICROPHONE, SOUND_MIXER_MIC}, 3149 {UATI_DESKMICROPHONE, SOUND_MIXER_MIC}, 3150 {UATI_PERSONALMICROPHONE, SOUND_MIXER_MIC}, 3151 {UATI_OMNIMICROPHONE, SOUND_MIXER_MIC}, 3152 {UATI_MICROPHONEARRAY, SOUND_MIXER_MIC}, 3153 {UATI_PROCMICROPHONEARR, SOUND_MIXER_MIC}, 3154 3155 {UATO_SPEAKER, SOUND_MIXER_SPEAKER}, 3156 {UATO_DESKTOPSPEAKER, SOUND_MIXER_SPEAKER}, 3157 {UATO_ROOMSPEAKER, SOUND_MIXER_SPEAKER}, 3158 {UATO_COMMSPEAKER, SOUND_MIXER_SPEAKER}, 3159 3160 {UATE_ANALOGCONN, SOUND_MIXER_LINE}, 3161 {UATE_LINECONN, SOUND_MIXER_LINE}, 3162 {UATE_LEGACYCONN, SOUND_MIXER_LINE}, 3163 3164 {UATE_DIGITALAUIFC, SOUND_MIXER_ALTPCM}, 3165 {UATE_SPDIF, SOUND_MIXER_ALTPCM}, 3166 {UATE_1394DA, SOUND_MIXER_ALTPCM}, 3167 {UATE_1394DV, SOUND_MIXER_ALTPCM}, 3168 3169 {UATF_CDPLAYER, SOUND_MIXER_CD}, 3170 3171 {UATF_SYNTHESIZER, SOUND_MIXER_SYNTH}, 3172 3173 {UATF_VIDEODISCAUDIO, SOUND_MIXER_VIDEO}, 3174 {UATF_DVDAUDIO, SOUND_MIXER_VIDEO}, 3175 {UATF_TVTUNERAUDIO, SOUND_MIXER_VIDEO}, 3176 3177 /* telephony terminal types */ 3178 {UATT_UNDEFINED, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 3179 {UATT_PHONELINE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 3180 {UATT_TELEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 3181 {UATT_DOWNLINEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 3182 3183 {UATF_RADIORECV, SOUND_MIXER_RADIO}, 3184 {UATF_RADIOXMIT, SOUND_MIXER_RADIO}, 3185 3186 {UAT_UNDEFINED, SOUND_MIXER_VOLUME}, 3187 {UAT_VENDOR, SOUND_MIXER_VOLUME}, 3188 {UATI_UNDEFINED, SOUND_MIXER_VOLUME}, 3189 3190 /* output terminal types */ 3191 {UATO_UNDEFINED, SOUND_MIXER_VOLUME}, 3192 {UATO_DISPLAYAUDIO, SOUND_MIXER_VOLUME}, 3193 {UATO_SUBWOOFER, SOUND_MIXER_VOLUME}, 3194 {UATO_HEADPHONES, SOUND_MIXER_VOLUME}, 3195 3196 /* bidir terminal types */ 3197 {UATB_UNDEFINED, SOUND_MIXER_VOLUME}, 3198 {UATB_HANDSET, SOUND_MIXER_VOLUME}, 3199 {UATB_HEADSET, SOUND_MIXER_VOLUME}, 3200 {UATB_SPEAKERPHONE, SOUND_MIXER_VOLUME}, 3201 {UATB_SPEAKERPHONEESUP, SOUND_MIXER_VOLUME}, 3202 {UATB_SPEAKERPHONEECANC, SOUND_MIXER_VOLUME}, 3203 3204 /* external terminal types */ 3205 {UATE_UNDEFINED, SOUND_MIXER_VOLUME}, 3206 3207 /* embedded function terminal types */ 3208 {UATF_UNDEFINED, SOUND_MIXER_VOLUME}, 3209 {UATF_CALIBNOISE, SOUND_MIXER_VOLUME}, 3210 {UATF_EQUNOISE, SOUND_MIXER_VOLUME}, 3211 {UATF_DAT, SOUND_MIXER_VOLUME}, 3212 {UATF_DCC, SOUND_MIXER_VOLUME}, 3213 {UATF_MINIDISK, SOUND_MIXER_VOLUME}, 3214 {UATF_ANALOGTAPE, SOUND_MIXER_VOLUME}, 3215 {UATF_PHONOGRAPH, SOUND_MIXER_VOLUME}, 3216 {UATF_VCRAUDIO, SOUND_MIXER_VOLUME}, 3217 {UATF_SATELLITE, SOUND_MIXER_VOLUME}, 3218 {UATF_CABLETUNER, SOUND_MIXER_VOLUME}, 3219 {UATF_DSS, SOUND_MIXER_VOLUME}, 3220 {UATF_MULTITRACK, SOUND_MIXER_VOLUME}, 3221 {0xffff, SOUND_MIXER_VOLUME}, 3222 3223 /* default */ 3224 {0x0000, SOUND_MIXER_VOLUME}, 3225 }; 3226 3227 static uint16_t 3228 uaudio_mixer_feature_name(const struct uaudio_terminal_node *iot, 3229 struct uaudio_mixer_node *mix) 3230 { 3231 const struct uaudio_tt_to_feature *uat = uaudio_tt_to_feature; 3232 uint16_t terminal_type = uaudio_mixer_determine_class(iot, mix); 3233 3234 if ((mix->class == UAC_RECORD) && (terminal_type == 0)) { 3235 return (SOUND_MIXER_IMIX); 3236 } 3237 while (uat->terminal_type) { 3238 if (uat->terminal_type == terminal_type) { 3239 break; 3240 } 3241 uat++; 3242 } 3243 3244 DPRINTF("terminal_type=0x%04x -> %d\n", 3245 terminal_type, uat->feature); 3246 3247 return (uat->feature); 3248 } 3249 3250 static uint16_t 3251 uaudio20_mixer_feature_name(const struct uaudio_terminal_node *iot, 3252 struct uaudio_mixer_node *mix) 3253 { 3254 const struct uaudio_tt_to_feature *uat; 3255 uint16_t terminal_type = uaudio20_mixer_determine_class(iot, mix); 3256 3257 if ((mix->class == UAC_RECORD) && (terminal_type == 0)) 3258 return (SOUND_MIXER_IMIX); 3259 3260 for (uat = uaudio_tt_to_feature; uat->terminal_type != 0; uat++) { 3261 if (uat->terminal_type == terminal_type) 3262 break; 3263 } 3264 3265 DPRINTF("terminal_type=0x%04x -> %d\n", 3266 terminal_type, uat->feature); 3267 3268 return (uat->feature); 3269 } 3270 3271 static const struct uaudio_terminal_node * 3272 uaudio_mixer_get_input(const struct uaudio_terminal_node *iot, uint8_t i) 3273 { 3274 struct uaudio_terminal_node *root = iot->root; 3275 uint8_t n; 3276 3277 n = iot->usr.id_max; 3278 do { 3279 if (iot->usr.bit_input[n / 8] & (1 << (n % 8))) { 3280 if (!i--) 3281 return (root + n); 3282 } 3283 } while (n--); 3284 3285 return (NULL); 3286 } 3287 3288 static const struct uaudio_terminal_node * 3289 uaudio_mixer_get_output(const struct uaudio_terminal_node *iot, uint8_t i) 3290 { 3291 struct uaudio_terminal_node *root = iot->root; 3292 uint8_t n; 3293 3294 n = iot->usr.id_max; 3295 do { 3296 if (iot->usr.bit_output[n / 8] & (1 << (n % 8))) { 3297 if (!i--) 3298 return (root + n); 3299 } 3300 } while (n--); 3301 3302 return (NULL); 3303 } 3304 3305 static void 3306 uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *root, 3307 const uint8_t *p_id, uint8_t n_id, 3308 struct uaudio_search_result *info) 3309 { 3310 struct uaudio_terminal_node *iot; 3311 uint8_t n; 3312 uint8_t i; 3313 uint8_t is_last; 3314 3315 top: 3316 for (n = 0; n < n_id; n++) { 3317 3318 i = p_id[n]; 3319 3320 if (info->recurse_level == UAUDIO_RECURSE_LIMIT) { 3321 DPRINTF("avoided going into a circle at id=%d!\n", i); 3322 return; 3323 } 3324 3325 info->recurse_level++; 3326 3327 iot = (root + i); 3328 3329 if (iot->u.desc == NULL) 3330 continue; 3331 3332 is_last = ((n + 1) == n_id); 3333 3334 switch (iot->u.desc->bDescriptorSubtype) { 3335 case UDESCSUB_AC_INPUT: 3336 info->bit_input[i / 8] |= (1 << (i % 8)); 3337 break; 3338 3339 case UDESCSUB_AC_FEATURE: 3340 if (is_last) { 3341 p_id = &iot->u.fu_v1->bSourceId; 3342 n_id = 1; 3343 goto top; 3344 } 3345 uaudio_mixer_find_inputs_sub( 3346 root, &iot->u.fu_v1->bSourceId, 1, info); 3347 break; 3348 3349 case UDESCSUB_AC_OUTPUT: 3350 if (is_last) { 3351 p_id = &iot->u.ot_v1->bSourceId; 3352 n_id = 1; 3353 goto top; 3354 } 3355 uaudio_mixer_find_inputs_sub( 3356 root, &iot->u.ot_v1->bSourceId, 1, info); 3357 break; 3358 3359 case UDESCSUB_AC_MIXER: 3360 if (is_last) { 3361 p_id = iot->u.mu_v1->baSourceId; 3362 n_id = iot->u.mu_v1->bNrInPins; 3363 goto top; 3364 } 3365 uaudio_mixer_find_inputs_sub( 3366 root, iot->u.mu_v1->baSourceId, 3367 iot->u.mu_v1->bNrInPins, info); 3368 break; 3369 3370 case UDESCSUB_AC_SELECTOR: 3371 if (is_last) { 3372 p_id = iot->u.su_v1->baSourceId; 3373 n_id = iot->u.su_v1->bNrInPins; 3374 goto top; 3375 } 3376 uaudio_mixer_find_inputs_sub( 3377 root, iot->u.su_v1->baSourceId, 3378 iot->u.su_v1->bNrInPins, info); 3379 break; 3380 3381 case UDESCSUB_AC_PROCESSING: 3382 if (is_last) { 3383 p_id = iot->u.pu_v1->baSourceId; 3384 n_id = iot->u.pu_v1->bNrInPins; 3385 goto top; 3386 } 3387 uaudio_mixer_find_inputs_sub( 3388 root, iot->u.pu_v1->baSourceId, 3389 iot->u.pu_v1->bNrInPins, info); 3390 break; 3391 3392 case UDESCSUB_AC_EXTENSION: 3393 if (is_last) { 3394 p_id = iot->u.eu_v1->baSourceId; 3395 n_id = iot->u.eu_v1->bNrInPins; 3396 goto top; 3397 } 3398 uaudio_mixer_find_inputs_sub( 3399 root, iot->u.eu_v1->baSourceId, 3400 iot->u.eu_v1->bNrInPins, info); 3401 break; 3402 3403 default: 3404 break; 3405 } 3406 } 3407 } 3408 3409 static void 3410 uaudio20_mixer_find_inputs_sub(struct uaudio_terminal_node *root, 3411 const uint8_t *p_id, uint8_t n_id, 3412 struct uaudio_search_result *info) 3413 { 3414 struct uaudio_terminal_node *iot; 3415 uint8_t n; 3416 uint8_t i; 3417 uint8_t is_last; 3418 3419 top: 3420 for (n = 0; n < n_id; n++) { 3421 3422 i = p_id[n]; 3423 3424 if (info->recurse_level == UAUDIO_RECURSE_LIMIT) { 3425 DPRINTF("avoided going into a circle at id=%d!\n", i); 3426 return; 3427 } 3428 3429 info->recurse_level++; 3430 3431 iot = (root + i); 3432 3433 if (iot->u.desc == NULL) 3434 continue; 3435 3436 is_last = ((n + 1) == n_id); 3437 3438 switch (iot->u.desc->bDescriptorSubtype) { 3439 case UDESCSUB_AC_INPUT: 3440 info->bit_input[i / 8] |= (1 << (i % 8)); 3441 break; 3442 3443 case UDESCSUB_AC_OUTPUT: 3444 if (is_last) { 3445 p_id = &iot->u.ot_v2->bSourceId; 3446 n_id = 1; 3447 goto top; 3448 } 3449 uaudio20_mixer_find_inputs_sub( 3450 root, &iot->u.ot_v2->bSourceId, 1, info); 3451 break; 3452 3453 case UDESCSUB_AC_MIXER: 3454 if (is_last) { 3455 p_id = iot->u.mu_v2->baSourceId; 3456 n_id = iot->u.mu_v2->bNrInPins; 3457 goto top; 3458 } 3459 uaudio20_mixer_find_inputs_sub( 3460 root, iot->u.mu_v2->baSourceId, 3461 iot->u.mu_v2->bNrInPins, info); 3462 break; 3463 3464 case UDESCSUB_AC_SELECTOR: 3465 if (is_last) { 3466 p_id = iot->u.su_v2->baSourceId; 3467 n_id = iot->u.su_v2->bNrInPins; 3468 goto top; 3469 } 3470 uaudio20_mixer_find_inputs_sub( 3471 root, iot->u.su_v2->baSourceId, 3472 iot->u.su_v2->bNrInPins, info); 3473 break; 3474 3475 case UDESCSUB_AC_SAMPLE_RT: 3476 if (is_last) { 3477 p_id = &iot->u.ru_v2->bSourceId; 3478 n_id = 1; 3479 goto top; 3480 } 3481 uaudio20_mixer_find_inputs_sub( 3482 root, &iot->u.ru_v2->bSourceId, 3483 1, info); 3484 break; 3485 3486 case UDESCSUB_AC_EFFECT: 3487 if (is_last) { 3488 p_id = &iot->u.ef_v2->bSourceId; 3489 n_id = 1; 3490 goto top; 3491 } 3492 uaudio20_mixer_find_inputs_sub( 3493 root, &iot->u.ef_v2->bSourceId, 3494 1, info); 3495 break; 3496 3497 case UDESCSUB_AC_FEATURE: 3498 if (is_last) { 3499 p_id = &iot->u.fu_v2->bSourceId; 3500 n_id = 1; 3501 goto top; 3502 } 3503 uaudio20_mixer_find_inputs_sub( 3504 root, &iot->u.fu_v2->bSourceId, 1, info); 3505 break; 3506 3507 case UDESCSUB_AC_PROCESSING_V2: 3508 if (is_last) { 3509 p_id = iot->u.pu_v2->baSourceId; 3510 n_id = iot->u.pu_v2->bNrInPins; 3511 goto top; 3512 } 3513 uaudio20_mixer_find_inputs_sub( 3514 root, iot->u.pu_v2->baSourceId, 3515 iot->u.pu_v2->bNrInPins, info); 3516 break; 3517 3518 case UDESCSUB_AC_EXTENSION_V2: 3519 if (is_last) { 3520 p_id = iot->u.eu_v2->baSourceId; 3521 n_id = iot->u.eu_v2->bNrInPins; 3522 goto top; 3523 } 3524 uaudio20_mixer_find_inputs_sub( 3525 root, iot->u.eu_v2->baSourceId, 3526 iot->u.eu_v2->bNrInPins, info); 3527 break; 3528 default: 3529 break; 3530 } 3531 } 3532 } 3533 3534 static void 3535 uaudio20_mixer_find_clocks_sub(struct uaudio_terminal_node *root, 3536 const uint8_t *p_id, uint8_t n_id, 3537 struct uaudio_search_result *info) 3538 { 3539 struct uaudio_terminal_node *iot; 3540 uint8_t n; 3541 uint8_t i; 3542 uint8_t is_last; 3543 uint8_t id; 3544 3545 top: 3546 for (n = 0; n < n_id; n++) { 3547 3548 i = p_id[n]; 3549 3550 if (info->recurse_level == UAUDIO_RECURSE_LIMIT) { 3551 DPRINTF("avoided going into a circle at id=%d!\n", i); 3552 return; 3553 } 3554 3555 info->recurse_level++; 3556 3557 iot = (root + i); 3558 3559 if (iot->u.desc == NULL) 3560 continue; 3561 3562 is_last = ((n + 1) == n_id); 3563 3564 switch (iot->u.desc->bDescriptorSubtype) { 3565 case UDESCSUB_AC_INPUT: 3566 info->is_input = 1; 3567 if (is_last) { 3568 p_id = &iot->u.it_v2->bCSourceId; 3569 n_id = 1; 3570 goto top; 3571 } 3572 uaudio20_mixer_find_clocks_sub(root, 3573 &iot->u.it_v2->bCSourceId, 1, info); 3574 break; 3575 3576 case UDESCSUB_AC_OUTPUT: 3577 info->is_input = 0; 3578 if (is_last) { 3579 p_id = &iot->u.ot_v2->bCSourceId; 3580 n_id = 1; 3581 goto top; 3582 } 3583 uaudio20_mixer_find_clocks_sub(root, 3584 &iot->u.ot_v2->bCSourceId, 1, info); 3585 break; 3586 3587 case UDESCSUB_AC_CLOCK_SEL: 3588 if (is_last) { 3589 p_id = iot->u.csel_v2->baCSourceId; 3590 n_id = iot->u.csel_v2->bNrInPins; 3591 goto top; 3592 } 3593 uaudio20_mixer_find_clocks_sub(root, 3594 iot->u.csel_v2->baCSourceId, 3595 iot->u.csel_v2->bNrInPins, info); 3596 break; 3597 3598 case UDESCSUB_AC_CLOCK_MUL: 3599 if (is_last) { 3600 p_id = &iot->u.cmul_v2->bCSourceId; 3601 n_id = 1; 3602 goto top; 3603 } 3604 uaudio20_mixer_find_clocks_sub(root, 3605 &iot->u.cmul_v2->bCSourceId, 3606 1, info); 3607 break; 3608 3609 case UDESCSUB_AC_CLOCK_SRC: 3610 3611 id = iot->u.csrc_v2->bClockId; 3612 3613 switch (info->is_input) { 3614 case 0: 3615 info->bit_output[id / 8] |= (1 << (id % 8)); 3616 break; 3617 case 1: 3618 info->bit_input[id / 8] |= (1 << (id % 8)); 3619 break; 3620 default: 3621 break; 3622 } 3623 break; 3624 3625 default: 3626 break; 3627 } 3628 } 3629 } 3630 3631 static void 3632 uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *root, uint8_t id, 3633 uint8_t n_id, struct uaudio_search_result *info) 3634 { 3635 struct uaudio_terminal_node *iot = (root + id); 3636 uint8_t j; 3637 3638 j = n_id; 3639 do { 3640 if ((j != id) && ((root + j)->u.desc) && 3641 ((root + j)->u.desc->bDescriptorSubtype == UDESCSUB_AC_OUTPUT)) { 3642 3643 /* 3644 * "j" (output) <--- virtual wire <--- "id" (input) 3645 * 3646 * if "j" has "id" on the input, then "id" have "j" on 3647 * the output, because they are connected: 3648 */ 3649 if ((root + j)->usr.bit_input[id / 8] & (1 << (id % 8))) { 3650 iot->usr.bit_output[j / 8] |= (1 << (j % 8)); 3651 } 3652 } 3653 } while (j--); 3654 } 3655 3656 static void 3657 uaudio_mixer_fill_info(struct uaudio_softc *sc, 3658 struct usb_device *udev, void *desc) 3659 { 3660 const struct usb_audio_control_descriptor *acdp; 3661 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 3662 const struct usb_descriptor *dp; 3663 const struct usb_audio_unit *au; 3664 struct uaudio_terminal_node *iot = NULL; 3665 uint16_t wTotalLen; 3666 uint8_t ID_max = 0; /* inclusive */ 3667 uint8_t i; 3668 3669 desc = usb_desc_foreach(cd, desc); 3670 3671 if (desc == NULL) { 3672 DPRINTF("no Audio Control header\n"); 3673 goto done; 3674 } 3675 acdp = desc; 3676 3677 if ((acdp->bLength < sizeof(*acdp)) || 3678 (acdp->bDescriptorType != UDESC_CS_INTERFACE) || 3679 (acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)) { 3680 DPRINTF("invalid Audio Control header\n"); 3681 goto done; 3682 } 3683 /* "wTotalLen" is allowed to be corrupt */ 3684 wTotalLen = UGETW(acdp->wTotalLength) - acdp->bLength; 3685 3686 /* get USB audio revision */ 3687 sc->sc_audio_rev = UGETW(acdp->bcdADC); 3688 3689 DPRINTFN(3, "found AC header, vers=%03x, len=%d\n", 3690 sc->sc_audio_rev, wTotalLen); 3691 3692 iot = malloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP, 3693 M_WAITOK | M_ZERO); 3694 3695 if (iot == NULL) { 3696 DPRINTF("no memory!\n"); 3697 goto done; 3698 } 3699 while ((desc = usb_desc_foreach(cd, desc))) { 3700 3701 dp = desc; 3702 3703 if (dp->bLength > wTotalLen) { 3704 break; 3705 } else { 3706 wTotalLen -= dp->bLength; 3707 } 3708 3709 if (sc->sc_audio_rev >= UAUDIO_VERSION_30) 3710 au = NULL; 3711 else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) 3712 au = uaudio20_mixer_verify_desc(dp, 0); 3713 else 3714 au = uaudio_mixer_verify_desc(dp, 0); 3715 3716 if (au) { 3717 iot[au->bUnitId].u.desc = (const void *)au; 3718 if (au->bUnitId > ID_max) 3719 ID_max = au->bUnitId; 3720 } 3721 } 3722 3723 DPRINTF("Maximum ID=%d\n", ID_max); 3724 3725 /* 3726 * determine sourcing inputs for 3727 * all nodes in the tree: 3728 */ 3729 i = ID_max; 3730 do { 3731 if (sc->sc_audio_rev >= UAUDIO_VERSION_30) { 3732 /* FALLTHROUGH */ 3733 } else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) { 3734 uaudio20_mixer_find_inputs_sub(iot, 3735 &i, 1, &((iot + i)->usr)); 3736 3737 sc->sc_mixer_clocks.is_input = 255; 3738 sc->sc_mixer_clocks.recurse_level = 0; 3739 3740 uaudio20_mixer_find_clocks_sub(iot, 3741 &i, 1, &sc->sc_mixer_clocks); 3742 } else { 3743 uaudio_mixer_find_inputs_sub(iot, 3744 &i, 1, &((iot + i)->usr)); 3745 } 3746 } while (i--); 3747 3748 /* 3749 * determine outputs for 3750 * all nodes in the tree: 3751 */ 3752 i = ID_max; 3753 do { 3754 uaudio_mixer_find_outputs_sub(iot, 3755 i, ID_max, &((iot + i)->usr)); 3756 } while (i--); 3757 3758 /* set "id_max" and "root" */ 3759 3760 i = ID_max; 3761 do { 3762 (iot + i)->usr.id_max = ID_max; 3763 (iot + i)->root = iot; 3764 } while (i--); 3765 3766 /* 3767 * Scan the config to create a linked list of "mixer" nodes: 3768 */ 3769 3770 i = ID_max; 3771 do { 3772 dp = iot[i].u.desc; 3773 3774 if (dp == NULL) 3775 continue; 3776 3777 DPRINTFN(11, "id=%d subtype=%d\n", 3778 i, dp->bDescriptorSubtype); 3779 3780 if (sc->sc_audio_rev >= UAUDIO_VERSION_30) { 3781 continue; 3782 } else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) { 3783 3784 switch (dp->bDescriptorSubtype) { 3785 case UDESCSUB_AC_HEADER: 3786 DPRINTF("unexpected AC header\n"); 3787 break; 3788 3789 case UDESCSUB_AC_INPUT: 3790 case UDESCSUB_AC_OUTPUT: 3791 case UDESCSUB_AC_PROCESSING_V2: 3792 case UDESCSUB_AC_EXTENSION_V2: 3793 case UDESCSUB_AC_EFFECT: 3794 case UDESCSUB_AC_CLOCK_SRC: 3795 case UDESCSUB_AC_CLOCK_SEL: 3796 case UDESCSUB_AC_CLOCK_MUL: 3797 case UDESCSUB_AC_SAMPLE_RT: 3798 break; 3799 3800 case UDESCSUB_AC_MIXER: 3801 uaudio20_mixer_add_mixer(sc, iot, i); 3802 break; 3803 3804 case UDESCSUB_AC_SELECTOR: 3805 uaudio20_mixer_add_selector(sc, iot, i); 3806 break; 3807 3808 case UDESCSUB_AC_FEATURE: 3809 uaudio20_mixer_add_feature(sc, iot, i); 3810 break; 3811 3812 default: 3813 DPRINTF("bad AC desc subtype=0x%02x\n", 3814 dp->bDescriptorSubtype); 3815 break; 3816 } 3817 continue; 3818 } 3819 3820 switch (dp->bDescriptorSubtype) { 3821 case UDESCSUB_AC_HEADER: 3822 DPRINTF("unexpected AC header\n"); 3823 break; 3824 3825 case UDESCSUB_AC_INPUT: 3826 case UDESCSUB_AC_OUTPUT: 3827 break; 3828 3829 case UDESCSUB_AC_MIXER: 3830 uaudio_mixer_add_mixer(sc, iot, i); 3831 break; 3832 3833 case UDESCSUB_AC_SELECTOR: 3834 uaudio_mixer_add_selector(sc, iot, i); 3835 break; 3836 3837 case UDESCSUB_AC_FEATURE: 3838 uaudio_mixer_add_feature(sc, iot, i); 3839 break; 3840 3841 case UDESCSUB_AC_PROCESSING: 3842 uaudio_mixer_add_processing(sc, iot, i); 3843 break; 3844 3845 case UDESCSUB_AC_EXTENSION: 3846 uaudio_mixer_add_extension(sc, iot, i); 3847 break; 3848 3849 default: 3850 DPRINTF("bad AC desc subtype=0x%02x\n", 3851 dp->bDescriptorSubtype); 3852 break; 3853 } 3854 3855 } while (i--); 3856 3857 done: 3858 free(iot, M_TEMP); 3859 } 3860 3861 static int 3862 uaudio_mixer_get(struct usb_device *udev, uint16_t audio_rev, 3863 uint8_t what, struct uaudio_mixer_node *mc) 3864 { 3865 struct usb_device_request req; 3866 int val; 3867 uint8_t data[2 + (2 * 3)]; 3868 usb_error_t err; 3869 3870 if (mc->wValue[0] == -1) 3871 return (0); 3872 3873 if (audio_rev >= UAUDIO_VERSION_30) 3874 return (0); 3875 else if (audio_rev >= UAUDIO_VERSION_20) { 3876 if (what == GET_CUR) { 3877 req.bRequest = UA20_CS_CUR; 3878 USETW(req.wLength, 2); 3879 } else { 3880 req.bRequest = UA20_CS_RANGE; 3881 USETW(req.wLength, 8); 3882 } 3883 } else { 3884 uint16_t len = MIX_SIZE(mc->type); 3885 3886 req.bRequest = what; 3887 USETW(req.wLength, len); 3888 } 3889 3890 req.bmRequestType = UT_READ_CLASS_INTERFACE; 3891 USETW(req.wValue, mc->wValue[0]); 3892 USETW(req.wIndex, mc->wIndex); 3893 3894 memset(data, 0, sizeof(data)); 3895 3896 err = usbd_do_request(udev, NULL, &req, data); 3897 if (err) { 3898 DPRINTF("err=%s\n", usbd_errstr(err)); 3899 return (0); 3900 } 3901 3902 if (audio_rev >= UAUDIO_VERSION_30) { 3903 val = 0; 3904 } else if (audio_rev >= UAUDIO_VERSION_20) { 3905 switch (what) { 3906 case GET_CUR: 3907 val = (data[0] | (data[1] << 8)); 3908 break; 3909 case GET_MIN: 3910 val = (data[2] | (data[3] << 8)); 3911 break; 3912 case GET_MAX: 3913 val = (data[4] | (data[5] << 8)); 3914 break; 3915 case GET_RES: 3916 val = (data[6] | (data[7] << 8)); 3917 break; 3918 default: 3919 val = 0; 3920 break; 3921 } 3922 } else { 3923 val = (data[0] | (data[1] << 8)); 3924 } 3925 3926 if (what == GET_CUR || what == GET_MIN || what == GET_MAX) 3927 val = uaudio_mixer_signext(mc->type, val); 3928 3929 DPRINTFN(3, "val=%d\n", val); 3930 3931 return (val); 3932 } 3933 3934 static void 3935 uaudio_mixer_write_cfg_callback(struct usb_xfer *xfer, usb_error_t error) 3936 { 3937 struct usb_device_request req; 3938 struct uaudio_softc *sc = usbd_xfer_softc(xfer); 3939 struct uaudio_mixer_node *mc = sc->sc_mixer_curr; 3940 struct usb_page_cache *pc; 3941 uint16_t len; 3942 uint8_t repeat = 1; 3943 uint8_t update; 3944 uint8_t chan; 3945 uint8_t buf[2]; 3946 3947 DPRINTF("\n"); 3948 3949 switch (USB_GET_STATE(xfer)) { 3950 case USB_ST_TRANSFERRED: 3951 tr_transferred: 3952 case USB_ST_SETUP: 3953 tr_setup: 3954 3955 if (mc == NULL) { 3956 mc = sc->sc_mixer_root; 3957 sc->sc_mixer_curr = mc; 3958 sc->sc_mixer_chan = 0; 3959 repeat = 0; 3960 } 3961 while (mc) { 3962 while (sc->sc_mixer_chan < mc->nchan) { 3963 3964 chan = sc->sc_mixer_chan; 3965 3966 sc->sc_mixer_chan++; 3967 3968 update = ((mc->update[chan / 8] & (1 << (chan % 8))) && 3969 (mc->wValue[chan] != -1)); 3970 3971 mc->update[chan / 8] &= ~(1 << (chan % 8)); 3972 3973 if (update) { 3974 3975 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 3976 USETW(req.wValue, mc->wValue[chan]); 3977 USETW(req.wIndex, mc->wIndex); 3978 3979 if (sc->sc_audio_rev >= UAUDIO_VERSION_30) { 3980 return; 3981 } else if (sc->sc_audio_rev >= UAUDIO_VERSION_20) { 3982 len = 2; 3983 req.bRequest = UA20_CS_CUR; 3984 USETW(req.wLength, len); 3985 } else { 3986 len = MIX_SIZE(mc->type); 3987 req.bRequest = SET_CUR; 3988 USETW(req.wLength, len); 3989 } 3990 3991 buf[0] = (mc->wData[chan] & 0xFF); 3992 buf[1] = (mc->wData[chan] >> 8) & 0xFF; 3993 3994 pc = usbd_xfer_get_frame(xfer, 0); 3995 usbd_copy_in(pc, 0, &req, sizeof(req)); 3996 pc = usbd_xfer_get_frame(xfer, 1); 3997 usbd_copy_in(pc, 0, buf, len); 3998 3999 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 4000 usbd_xfer_set_frame_len(xfer, 1, len); 4001 usbd_xfer_set_frames(xfer, len ? 2 : 1); 4002 usbd_transfer_submit(xfer); 4003 return; 4004 } 4005 } 4006 4007 mc = mc->next; 4008 sc->sc_mixer_curr = mc; 4009 sc->sc_mixer_chan = 0; 4010 } 4011 4012 if (repeat) { 4013 goto tr_setup; 4014 } 4015 break; 4016 4017 default: /* Error */ 4018 DPRINTF("error=%s\n", usbd_errstr(error)); 4019 if (error == USB_ERR_CANCELLED) { 4020 /* do nothing - we are detaching */ 4021 break; 4022 } 4023 goto tr_transferred; 4024 } 4025 } 4026 4027 static usb_error_t 4028 uaudio_set_speed(struct usb_device *udev, uint8_t endpt, uint32_t speed) 4029 { 4030 struct usb_device_request req; 4031 uint8_t data[3]; 4032 4033 DPRINTFN(6, "endpt=%d speed=%u\n", endpt, speed); 4034 4035 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT; 4036 req.bRequest = SET_CUR; 4037 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0); 4038 USETW(req.wIndex, endpt); 4039 USETW(req.wLength, 3); 4040 data[0] = speed; 4041 data[1] = speed >> 8; 4042 data[2] = speed >> 16; 4043 4044 return (usbd_do_request(udev, NULL, &req, data)); 4045 } 4046 4047 static usb_error_t 4048 uaudio20_set_speed(struct usb_device *udev, uint8_t iface_no, 4049 uint8_t clockid, uint32_t speed) 4050 { 4051 struct usb_device_request req; 4052 uint8_t data[4]; 4053 4054 DPRINTFN(6, "ifaceno=%d clockid=%d speed=%u\n", 4055 iface_no, clockid, speed); 4056 4057 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 4058 req.bRequest = UA20_CS_CUR; 4059 USETW2(req.wValue, UA20_CS_SAM_FREQ_CONTROL, 0); 4060 USETW2(req.wIndex, clockid, iface_no); 4061 USETW(req.wLength, 4); 4062 data[0] = speed; 4063 data[1] = speed >> 8; 4064 data[2] = speed >> 16; 4065 data[3] = speed >> 24; 4066 4067 return (usbd_do_request(udev, NULL, &req, data)); 4068 } 4069 4070 static int 4071 uaudio_mixer_signext(uint8_t type, int val) 4072 { 4073 if (!MIX_UNSIGNED(type)) { 4074 if (MIX_SIZE(type) == 2) { 4075 val = (int16_t)val; 4076 } else { 4077 val = (int8_t)val; 4078 } 4079 } 4080 return (val); 4081 } 4082 4083 static int 4084 uaudio_mixer_bsd2value(struct uaudio_mixer_node *mc, int32_t val) 4085 { 4086 if (mc->type == MIX_ON_OFF) { 4087 val = (val != 0); 4088 } else if (mc->type == MIX_SELECTOR) { 4089 if ((val < mc->minval) || 4090 (val > mc->maxval)) { 4091 val = mc->minval; 4092 } 4093 } else { 4094 4095 /* compute actual volume */ 4096 val = (val * mc->mul) / 255; 4097 4098 /* add lower offset */ 4099 val = val + mc->minval; 4100 4101 /* make sure we don't write a value out of range */ 4102 if (val > mc->maxval) 4103 val = mc->maxval; 4104 else if (val < mc->minval) 4105 val = mc->minval; 4106 } 4107 4108 DPRINTFN(6, "type=0x%03x val=%d min=%d max=%d val=%d\n", 4109 mc->type, val, mc->minval, mc->maxval, val); 4110 return (val); 4111 } 4112 4113 static void 4114 uaudio_mixer_ctl_set(struct uaudio_softc *sc, struct uaudio_mixer_node *mc, 4115 uint8_t chan, int32_t val) 4116 { 4117 val = uaudio_mixer_bsd2value(mc, val); 4118 4119 mc->update[chan / 8] |= (1 << (chan % 8)); 4120 mc->wData[chan] = val; 4121 4122 /* start the transfer, if not already started */ 4123 4124 usbd_transfer_start(sc->sc_mixer_xfer[0]); 4125 } 4126 4127 static void 4128 uaudio_mixer_init(struct uaudio_softc *sc) 4129 { 4130 struct uaudio_mixer_node *mc; 4131 int32_t i; 4132 4133 for (mc = sc->sc_mixer_root; mc; 4134 mc = mc->next) { 4135 4136 if (mc->ctl != SOUND_MIXER_NRDEVICES) { 4137 /* 4138 * Set device mask bits. See 4139 * /usr/include/machine/soundcard.h 4140 */ 4141 sc->sc_mix_info |= (1 << mc->ctl); 4142 } 4143 if ((mc->ctl == SOUND_MIXER_NRDEVICES) && 4144 (mc->type == MIX_SELECTOR)) { 4145 4146 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 4147 if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES) { 4148 continue; 4149 } 4150 sc->sc_recsrc_info |= 1 << mc->slctrtype[i - 1]; 4151 } 4152 } 4153 } 4154 } 4155 4156 int 4157 uaudio_mixer_init_sub(struct uaudio_softc *sc, struct snd_mixer *m) 4158 { 4159 DPRINTF("\n"); 4160 4161 if (usbd_transfer_setup(sc->sc_udev, &sc->sc_mixer_iface_index, 4162 sc->sc_mixer_xfer, uaudio_mixer_config, 1, sc, 4163 mixer_get_lock(m))) { 4164 DPRINTFN(0, "could not allocate USB " 4165 "transfer for audio mixer!\n"); 4166 return (ENOMEM); 4167 } 4168 if (!(sc->sc_mix_info & SOUND_MASK_VOLUME)) { 4169 mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM); 4170 mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE); 4171 } 4172 mix_setdevs(m, sc->sc_mix_info); 4173 mix_setrecdevs(m, sc->sc_recsrc_info); 4174 return (0); 4175 } 4176 4177 int 4178 uaudio_mixer_uninit_sub(struct uaudio_softc *sc) 4179 { 4180 DPRINTF("\n"); 4181 4182 usbd_transfer_unsetup(sc->sc_mixer_xfer, 1); 4183 4184 return (0); 4185 } 4186 4187 void 4188 uaudio_mixer_set(struct uaudio_softc *sc, unsigned type, 4189 unsigned left, unsigned right) 4190 { 4191 struct uaudio_mixer_node *mc; 4192 4193 for (mc = sc->sc_mixer_root; mc; 4194 mc = mc->next) { 4195 4196 if (mc->ctl == type) { 4197 if (mc->nchan == 2) { 4198 /* set Right */ 4199 uaudio_mixer_ctl_set(sc, mc, 1, (int)(right * 255) / 100); 4200 } 4201 /* set Left or Mono */ 4202 uaudio_mixer_ctl_set(sc, mc, 0, (int)(left * 255) / 100); 4203 } 4204 } 4205 } 4206 4207 uint32_t 4208 uaudio_mixer_setrecsrc(struct uaudio_softc *sc, uint32_t src) 4209 { 4210 struct uaudio_mixer_node *mc; 4211 uint32_t mask; 4212 uint32_t temp; 4213 int32_t i; 4214 4215 for (mc = sc->sc_mixer_root; mc; 4216 mc = mc->next) { 4217 4218 if ((mc->ctl == SOUND_MIXER_NRDEVICES) && 4219 (mc->type == MIX_SELECTOR)) { 4220 4221 /* compute selector mask */ 4222 4223 mask = 0; 4224 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 4225 mask |= (1 << mc->slctrtype[i - 1]); 4226 } 4227 4228 temp = mask & src; 4229 if (temp == 0) { 4230 continue; 4231 } 4232 /* find the first set bit */ 4233 temp = (-temp) & temp; 4234 4235 /* update "src" */ 4236 src &= ~mask; 4237 src |= temp; 4238 4239 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 4240 if (temp != (1 << mc->slctrtype[i - 1])) { 4241 continue; 4242 } 4243 uaudio_mixer_ctl_set(sc, mc, 0, i); 4244 break; 4245 } 4246 } 4247 } 4248 return (src); 4249 } 4250 4251 /*========================================================================* 4252 * MIDI support routines 4253 *========================================================================*/ 4254 4255 static void 4256 umidi_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 4257 { 4258 struct umidi_chan *chan = usbd_xfer_softc(xfer); 4259 struct umidi_sub_chan *sub; 4260 struct usb_page_cache *pc; 4261 uint8_t buf[4]; 4262 uint8_t cmd_len; 4263 uint8_t cn; 4264 uint16_t pos; 4265 int actlen; 4266 4267 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 4268 4269 switch (USB_GET_STATE(xfer)) { 4270 case USB_ST_TRANSFERRED: 4271 4272 DPRINTF("actlen=%d bytes\n", actlen); 4273 4274 pos = 0; 4275 pc = usbd_xfer_get_frame(xfer, 0); 4276 4277 while (actlen >= 4) { 4278 4279 /* copy out the MIDI data */ 4280 usbd_copy_out(pc, pos, buf, 4); 4281 /* command length */ 4282 cmd_len = umidi_cmd_to_len[buf[0] & 0xF]; 4283 /* cable number */ 4284 cn = buf[0] >> 4; 4285 /* 4286 * Lookup sub-channel. The index is range 4287 * checked below. 4288 */ 4289 sub = &chan->sub[cn]; 4290 4291 if ((cmd_len != 0) && 4292 (cn < chan->max_cable) && 4293 (sub->read_open != 0)) { 4294 4295 /* Send data to the application */ 4296 usb_fifo_put_data_linear( 4297 sub->fifo.fp[USB_FIFO_RX], 4298 buf + 1, cmd_len, 1); 4299 } 4300 actlen -= 4; 4301 pos += 4; 4302 } 4303 4304 case USB_ST_SETUP: 4305 DPRINTF("start\n"); 4306 tr_setup: 4307 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 4308 usbd_transfer_submit(xfer); 4309 break; 4310 4311 default: 4312 DPRINTF("error=%s\n", usbd_errstr(error)); 4313 4314 if (error != USB_ERR_CANCELLED) { 4315 /* try to clear stall first */ 4316 usbd_xfer_set_stall(xfer); 4317 goto tr_setup; 4318 } 4319 break; 4320 } 4321 } 4322 4323 /* 4324 * The following statemachine, that converts MIDI commands to 4325 * USB MIDI packets, derives from Linux's usbmidi.c, which 4326 * was written by "Clemens Ladisch": 4327 * 4328 * Returns: 4329 * 0: No command 4330 * Else: Command is complete 4331 */ 4332 static uint8_t 4333 umidi_convert_to_usb(struct umidi_sub_chan *sub, uint8_t cn, uint8_t b) 4334 { 4335 uint8_t p0 = (cn << 4); 4336 4337 if (b >= 0xf8) { 4338 sub->temp_0[0] = p0 | 0x0f; 4339 sub->temp_0[1] = b; 4340 sub->temp_0[2] = 0; 4341 sub->temp_0[3] = 0; 4342 sub->temp_cmd = sub->temp_0; 4343 return (1); 4344 4345 } else if (b >= 0xf0) { 4346 switch (b) { 4347 case 0xf0: /* system exclusive begin */ 4348 sub->temp_1[1] = b; 4349 sub->state = UMIDI_ST_SYSEX_1; 4350 break; 4351 case 0xf1: /* MIDI time code */ 4352 case 0xf3: /* song select */ 4353 sub->temp_1[1] = b; 4354 sub->state = UMIDI_ST_1PARAM; 4355 break; 4356 case 0xf2: /* song position pointer */ 4357 sub->temp_1[1] = b; 4358 sub->state = UMIDI_ST_2PARAM_1; 4359 break; 4360 case 0xf4: /* unknown */ 4361 case 0xf5: /* unknown */ 4362 sub->state = UMIDI_ST_UNKNOWN; 4363 break; 4364 case 0xf6: /* tune request */ 4365 sub->temp_1[0] = p0 | 0x05; 4366 sub->temp_1[1] = 0xf6; 4367 sub->temp_1[2] = 0; 4368 sub->temp_1[3] = 0; 4369 sub->temp_cmd = sub->temp_1; 4370 sub->state = UMIDI_ST_UNKNOWN; 4371 return (1); 4372 4373 case 0xf7: /* system exclusive end */ 4374 switch (sub->state) { 4375 case UMIDI_ST_SYSEX_0: 4376 sub->temp_1[0] = p0 | 0x05; 4377 sub->temp_1[1] = 0xf7; 4378 sub->temp_1[2] = 0; 4379 sub->temp_1[3] = 0; 4380 sub->temp_cmd = sub->temp_1; 4381 sub->state = UMIDI_ST_UNKNOWN; 4382 return (1); 4383 case UMIDI_ST_SYSEX_1: 4384 sub->temp_1[0] = p0 | 0x06; 4385 sub->temp_1[2] = 0xf7; 4386 sub->temp_1[3] = 0; 4387 sub->temp_cmd = sub->temp_1; 4388 sub->state = UMIDI_ST_UNKNOWN; 4389 return (1); 4390 case UMIDI_ST_SYSEX_2: 4391 sub->temp_1[0] = p0 | 0x07; 4392 sub->temp_1[3] = 0xf7; 4393 sub->temp_cmd = sub->temp_1; 4394 sub->state = UMIDI_ST_UNKNOWN; 4395 return (1); 4396 } 4397 sub->state = UMIDI_ST_UNKNOWN; 4398 break; 4399 } 4400 } else if (b >= 0x80) { 4401 sub->temp_1[1] = b; 4402 if ((b >= 0xc0) && (b <= 0xdf)) { 4403 sub->state = UMIDI_ST_1PARAM; 4404 } else { 4405 sub->state = UMIDI_ST_2PARAM_1; 4406 } 4407 } else { /* b < 0x80 */ 4408 switch (sub->state) { 4409 case UMIDI_ST_1PARAM: 4410 if (sub->temp_1[1] < 0xf0) { 4411 p0 |= sub->temp_1[1] >> 4; 4412 } else { 4413 p0 |= 0x02; 4414 sub->state = UMIDI_ST_UNKNOWN; 4415 } 4416 sub->temp_1[0] = p0; 4417 sub->temp_1[2] = b; 4418 sub->temp_1[3] = 0; 4419 sub->temp_cmd = sub->temp_1; 4420 return (1); 4421 case UMIDI_ST_2PARAM_1: 4422 sub->temp_1[2] = b; 4423 sub->state = UMIDI_ST_2PARAM_2; 4424 break; 4425 case UMIDI_ST_2PARAM_2: 4426 if (sub->temp_1[1] < 0xf0) { 4427 p0 |= sub->temp_1[1] >> 4; 4428 sub->state = UMIDI_ST_2PARAM_1; 4429 } else { 4430 p0 |= 0x03; 4431 sub->state = UMIDI_ST_UNKNOWN; 4432 } 4433 sub->temp_1[0] = p0; 4434 sub->temp_1[3] = b; 4435 sub->temp_cmd = sub->temp_1; 4436 return (1); 4437 case UMIDI_ST_SYSEX_0: 4438 sub->temp_1[1] = b; 4439 sub->state = UMIDI_ST_SYSEX_1; 4440 break; 4441 case UMIDI_ST_SYSEX_1: 4442 sub->temp_1[2] = b; 4443 sub->state = UMIDI_ST_SYSEX_2; 4444 break; 4445 case UMIDI_ST_SYSEX_2: 4446 sub->temp_1[0] = p0 | 0x04; 4447 sub->temp_1[3] = b; 4448 sub->temp_cmd = sub->temp_1; 4449 sub->state = UMIDI_ST_SYSEX_0; 4450 return (1); 4451 default: 4452 break; 4453 } 4454 } 4455 return (0); 4456 } 4457 4458 static void 4459 umidi_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 4460 { 4461 struct umidi_chan *chan = usbd_xfer_softc(xfer); 4462 struct umidi_sub_chan *sub; 4463 struct usb_page_cache *pc; 4464 uint32_t actlen; 4465 uint16_t nframes; 4466 uint8_t buf; 4467 uint8_t start_cable; 4468 uint8_t tr_any; 4469 int len; 4470 4471 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 4472 4473 /* 4474 * NOTE: Some MIDI devices only accept 4 bytes of data per 4475 * short terminated USB transfer. 4476 */ 4477 switch (USB_GET_STATE(xfer)) { 4478 case USB_ST_TRANSFERRED: 4479 DPRINTF("actlen=%d bytes\n", len); 4480 4481 case USB_ST_SETUP: 4482 tr_setup: 4483 DPRINTF("start\n"); 4484 4485 nframes = 0; /* reset */ 4486 start_cable = chan->curr_cable; 4487 tr_any = 0; 4488 pc = usbd_xfer_get_frame(xfer, 0); 4489 4490 while (1) { 4491 4492 /* round robin de-queueing */ 4493 4494 sub = &chan->sub[chan->curr_cable]; 4495 4496 if (sub->write_open) { 4497 usb_fifo_get_data_linear(sub->fifo.fp[USB_FIFO_TX], 4498 &buf, 1, &actlen, 0); 4499 } else { 4500 actlen = 0; 4501 } 4502 4503 if (actlen) { 4504 4505 tr_any = 1; 4506 4507 DPRINTF("byte=0x%02x from FIFO %u\n", buf, 4508 (unsigned int)chan->curr_cable); 4509 4510 if (umidi_convert_to_usb(sub, chan->curr_cable, buf)) { 4511 4512 DPRINTF("sub=0x%02x 0x%02x 0x%02x 0x%02x\n", 4513 sub->temp_cmd[0], sub->temp_cmd[1], 4514 sub->temp_cmd[2], sub->temp_cmd[3]); 4515 4516 usbd_copy_in(pc, nframes * 4, sub->temp_cmd, 4); 4517 4518 nframes++; 4519 4520 if ((nframes >= UMIDI_TX_FRAMES) || (chan->single_command != 0)) 4521 break; 4522 } else { 4523 continue; 4524 } 4525 } 4526 4527 chan->curr_cable++; 4528 if (chan->curr_cable >= chan->max_cable) 4529 chan->curr_cable = 0; 4530 4531 if (chan->curr_cable == start_cable) { 4532 if (tr_any == 0) 4533 break; 4534 tr_any = 0; 4535 } 4536 } 4537 4538 if (nframes != 0) { 4539 DPRINTF("Transferring %d frames\n", (int)nframes); 4540 usbd_xfer_set_frame_len(xfer, 0, 4 * nframes); 4541 usbd_transfer_submit(xfer); 4542 } 4543 break; 4544 4545 default: /* Error */ 4546 4547 DPRINTF("error=%s\n", usbd_errstr(error)); 4548 4549 if (error != USB_ERR_CANCELLED) { 4550 /* try to clear stall first */ 4551 usbd_xfer_set_stall(xfer); 4552 goto tr_setup; 4553 } 4554 break; 4555 } 4556 } 4557 4558 static struct umidi_sub_chan * 4559 umidi_sub_by_fifo(struct usb_fifo *fifo) 4560 { 4561 struct umidi_chan *chan = usb_fifo_softc(fifo); 4562 struct umidi_sub_chan *sub; 4563 uint32_t n; 4564 4565 for (n = 0; n < UMIDI_CABLES_MAX; n++) { 4566 sub = &chan->sub[n]; 4567 if ((sub->fifo.fp[USB_FIFO_RX] == fifo) || 4568 (sub->fifo.fp[USB_FIFO_TX] == fifo)) { 4569 return (sub); 4570 } 4571 } 4572 4573 panic("%s:%d cannot find usb_fifo!\n", 4574 __FILE__, __LINE__); 4575 4576 return (NULL); 4577 } 4578 4579 static void 4580 umidi_start_read(struct usb_fifo *fifo) 4581 { 4582 struct umidi_chan *chan = usb_fifo_softc(fifo); 4583 4584 usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]); 4585 } 4586 4587 static void 4588 umidi_stop_read(struct usb_fifo *fifo) 4589 { 4590 struct umidi_chan *chan = usb_fifo_softc(fifo); 4591 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 4592 4593 DPRINTF("\n"); 4594 4595 sub->read_open = 0; 4596 4597 if (--(chan->read_open_refcount) == 0) { 4598 /* 4599 * XXX don't stop the read transfer here, hence that causes 4600 * problems with some MIDI adapters 4601 */ 4602 DPRINTF("(stopping read transfer)\n"); 4603 } 4604 } 4605 4606 static void 4607 umidi_start_write(struct usb_fifo *fifo) 4608 { 4609 struct umidi_chan *chan = usb_fifo_softc(fifo); 4610 4611 usbd_transfer_start(chan->xfer[UMIDI_TX_TRANSFER]); 4612 } 4613 4614 static void 4615 umidi_stop_write(struct usb_fifo *fifo) 4616 { 4617 struct umidi_chan *chan = usb_fifo_softc(fifo); 4618 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 4619 4620 DPRINTF("\n"); 4621 4622 sub->write_open = 0; 4623 4624 if (--(chan->write_open_refcount) == 0) { 4625 DPRINTF("(stopping write transfer)\n"); 4626 usbd_transfer_stop(chan->xfer[UMIDI_TX_TRANSFER]); 4627 } 4628 } 4629 4630 static int 4631 umidi_open(struct usb_fifo *fifo, int fflags) 4632 { 4633 struct umidi_chan *chan = usb_fifo_softc(fifo); 4634 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 4635 4636 if (fflags & FREAD) { 4637 if (usb_fifo_alloc_buffer(fifo, 4, (1024 / 4))) { 4638 return (ENOMEM); 4639 } 4640 mtx_lock(&chan->mtx); 4641 chan->read_open_refcount++; 4642 sub->read_open = 1; 4643 mtx_unlock(&chan->mtx); 4644 } 4645 if (fflags & FWRITE) { 4646 if (usb_fifo_alloc_buffer(fifo, 32, (1024 / 32))) { 4647 return (ENOMEM); 4648 } 4649 /* clear stall first */ 4650 mtx_lock(&chan->mtx); 4651 usbd_xfer_set_stall(chan->xfer[UMIDI_TX_TRANSFER]); 4652 chan->write_open_refcount++; 4653 sub->write_open = 1; 4654 4655 /* reset */ 4656 sub->state = UMIDI_ST_UNKNOWN; 4657 mtx_unlock(&chan->mtx); 4658 } 4659 return (0); /* success */ 4660 } 4661 4662 static void 4663 umidi_close(struct usb_fifo *fifo, int fflags) 4664 { 4665 if (fflags & FREAD) { 4666 usb_fifo_free_buffer(fifo); 4667 } 4668 if (fflags & FWRITE) { 4669 usb_fifo_free_buffer(fifo); 4670 } 4671 } 4672 4673 4674 static int 4675 umidi_ioctl(struct usb_fifo *fifo, u_long cmd, void *data, 4676 int fflags) 4677 { 4678 return (ENODEV); 4679 } 4680 4681 static void 4682 umidi_init(device_t dev) 4683 { 4684 struct uaudio_softc *sc = device_get_softc(dev); 4685 struct umidi_chan *chan = &sc->sc_midi_chan; 4686 4687 mtx_init(&chan->mtx, "umidi lock", NULL, MTX_DEF | MTX_RECURSE); 4688 } 4689 4690 static struct usb_fifo_methods umidi_fifo_methods = { 4691 .f_start_read = &umidi_start_read, 4692 .f_start_write = &umidi_start_write, 4693 .f_stop_read = &umidi_stop_read, 4694 .f_stop_write = &umidi_stop_write, 4695 .f_open = &umidi_open, 4696 .f_close = &umidi_close, 4697 .f_ioctl = &umidi_ioctl, 4698 .basename[0] = "umidi", 4699 }; 4700 4701 static int 4702 umidi_probe(device_t dev) 4703 { 4704 struct uaudio_softc *sc = device_get_softc(dev); 4705 struct usb_attach_arg *uaa = device_get_ivars(dev); 4706 struct umidi_chan *chan = &sc->sc_midi_chan; 4707 struct umidi_sub_chan *sub; 4708 int unit = device_get_unit(dev); 4709 int error; 4710 uint32_t n; 4711 4712 if (usb_test_quirk(uaa, UQ_SINGLE_CMD_MIDI)) 4713 chan->single_command = 1; 4714 4715 if (usbd_set_alt_interface_index(sc->sc_udev, chan->iface_index, 4716 chan->iface_alt_index)) { 4717 DPRINTF("setting of alternate index failed!\n"); 4718 goto detach; 4719 } 4720 usbd_set_parent_iface(sc->sc_udev, chan->iface_index, 4721 sc->sc_mixer_iface_index); 4722 4723 error = usbd_transfer_setup(uaa->device, &chan->iface_index, 4724 chan->xfer, umidi_config, UMIDI_N_TRANSFER, 4725 chan, &chan->mtx); 4726 if (error) { 4727 DPRINTF("error=%s\n", usbd_errstr(error)); 4728 goto detach; 4729 } 4730 if ((chan->max_cable > UMIDI_CABLES_MAX) || 4731 (chan->max_cable == 0)) { 4732 chan->max_cable = UMIDI_CABLES_MAX; 4733 } 4734 4735 for (n = 0; n < chan->max_cable; n++) { 4736 4737 sub = &chan->sub[n]; 4738 4739 error = usb_fifo_attach(sc->sc_udev, chan, &chan->mtx, 4740 &umidi_fifo_methods, &sub->fifo, unit, n, 4741 chan->iface_index, 4742 UID_ROOT, GID_OPERATOR, 0644); 4743 if (error) { 4744 goto detach; 4745 } 4746 } 4747 4748 mtx_lock(&chan->mtx); 4749 4750 /* clear stall first */ 4751 usbd_xfer_set_stall(chan->xfer[UMIDI_RX_TRANSFER]); 4752 4753 /* 4754 * NOTE: At least one device will not work properly unless the 4755 * BULK IN pipe is open all the time. This might have to do 4756 * about that the internal queues of the device overflow if we 4757 * don't read them regularly. 4758 */ 4759 usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]); 4760 4761 mtx_unlock(&chan->mtx); 4762 4763 return (0); /* success */ 4764 4765 detach: 4766 return (ENXIO); /* failure */ 4767 } 4768 4769 static int 4770 umidi_detach(device_t dev) 4771 { 4772 struct uaudio_softc *sc = device_get_softc(dev); 4773 struct umidi_chan *chan = &sc->sc_midi_chan; 4774 uint32_t n; 4775 4776 for (n = 0; n < UMIDI_CABLES_MAX; n++) { 4777 usb_fifo_detach(&chan->sub[n].fifo); 4778 } 4779 4780 mtx_lock(&chan->mtx); 4781 4782 usbd_transfer_stop(chan->xfer[UMIDI_RX_TRANSFER]); 4783 4784 mtx_unlock(&chan->mtx); 4785 4786 usbd_transfer_unsetup(chan->xfer, UMIDI_N_TRANSFER); 4787 4788 mtx_destroy(&chan->mtx); 4789 4790 return (0); 4791 } 4792 4793 DRIVER_MODULE(uaudio, uhub, uaudio_driver, uaudio_devclass, NULL, 0); 4794 MODULE_DEPEND(uaudio, usb, 1, 1, 1); 4795 MODULE_DEPEND(uaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 4796 MODULE_VERSION(uaudio, 1); 4797