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 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf 43 * http://www.usb.org/developers/devclass_docs/frmts10.pdf 44 * http://www.usb.org/developers/devclass_docs/termt10.pdf 45 */ 46 47 /* 48 * Also merged: 49 * $NetBSD: uaudio.c,v 1.94 2005/01/15 15:19:53 kent Exp $ 50 * $NetBSD: uaudio.c,v 1.95 2005/01/16 06:02:19 dsainty Exp $ 51 * $NetBSD: uaudio.c,v 1.96 2005/01/16 12:46:00 kent Exp $ 52 * $NetBSD: uaudio.c,v 1.97 2005/02/24 08:19:38 martin Exp $ 53 */ 54 55 #include "usbdevs.h" 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usb_mfunc.h> 58 #include <dev/usb/usb_error.h> 59 60 #define USB_DEBUG_VAR uaudio_debug 61 62 #include <dev/usb/usb_core.h> 63 #include <dev/usb/usb_lookup.h> 64 #include <dev/usb/usb_debug.h> 65 #include <dev/usb/usb_util.h> 66 #include <dev/usb/usb_busdma.h> 67 #include <dev/usb/usb_parse.h> 68 #include <dev/usb/usb_request.h> 69 #include <dev/usb/usb_mbuf.h> 70 #include <dev/usb/usb_dev.h> 71 #include <dev/usb/usb_dynamic.h> 72 73 #include <dev/usb/quirk/usb_quirk.h> 74 75 #include <sys/reboot.h> /* for bootverbose */ 76 77 #include <dev/sound/pcm/sound.h> 78 #include <dev/sound/usb/uaudioreg.h> 79 #include <dev/sound/usb/uaudio.h> 80 #include <dev/sound/chip.h> 81 #include "feeder_if.h" 82 83 static int uaudio_default_rate = 96000; 84 static int uaudio_default_bits = 32; 85 static int uaudio_default_channels = 2; 86 87 #if USB_DEBUG 88 static int uaudio_debug = 0; 89 90 SYSCTL_NODE(_hw_usb2, OID_AUTO, uaudio, CTLFLAG_RW, 0, "USB uaudio"); 91 SYSCTL_INT(_hw_usb2_uaudio, OID_AUTO, debug, CTLFLAG_RW, 92 &uaudio_debug, 0, "uaudio debug level"); 93 SYSCTL_INT(_hw_usb2_uaudio, OID_AUTO, default_rate, CTLFLAG_RW, 94 &uaudio_default_rate, 0, "uaudio default sample rate"); 95 SYSCTL_INT(_hw_usb2_uaudio, OID_AUTO, default_bits, CTLFLAG_RW, 96 &uaudio_default_bits, 0, "uaudio default sample bits"); 97 SYSCTL_INT(_hw_usb2_uaudio, OID_AUTO, default_channels, CTLFLAG_RW, 98 &uaudio_default_channels, 0, "uaudio default sample channels"); 99 #endif 100 101 #define UAUDIO_MINFRAMES 16 /* must be factor of 8 due HS-USB */ 102 #define UAUDIO_NCHANBUFS 2 /* number of outstanding request */ 103 #define UAUDIO_RECURSE_LIMIT 24 /* rounds */ 104 105 #define MAKE_WORD(h,l) (((h) << 8) | (l)) 106 #define BIT_TEST(bm,bno) (((bm)[(bno) / 8] >> (7 - ((bno) % 8))) & 1) 107 108 struct uaudio_mixer_node { 109 int32_t minval; 110 int32_t maxval; 111 #define MIX_MAX_CHAN 8 112 int32_t wValue[MIX_MAX_CHAN]; /* using nchan */ 113 uint32_t delta; 114 uint32_t mul; 115 uint32_t ctl; 116 117 uint16_t wData[MIX_MAX_CHAN]; /* using nchan */ 118 uint16_t wIndex; 119 120 uint8_t update[(MIX_MAX_CHAN + 7) / 8]; 121 uint8_t nchan; 122 uint8_t type; 123 #define MIX_ON_OFF 1 124 #define MIX_SIGNED_16 2 125 #define MIX_UNSIGNED_16 3 126 #define MIX_SIGNED_8 4 127 #define MIX_SELECTOR 5 128 #define MIX_UNKNOWN 6 129 #define MIX_SIZE(n) ((((n) == MIX_SIGNED_16) || \ 130 ((n) == MIX_UNSIGNED_16)) ? 2 : 1) 131 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16) 132 133 #define MAX_SELECTOR_INPUT_PIN 256 134 uint8_t slctrtype[MAX_SELECTOR_INPUT_PIN]; 135 uint8_t class; 136 137 struct uaudio_mixer_node *next; 138 }; 139 140 struct uaudio_chan { 141 struct pcmchan_caps pcm_cap; /* capabilities */ 142 143 struct snd_dbuf *pcm_buf; 144 const struct usb2_config *usb2_cfg; 145 struct mtx *pcm_mtx; /* lock protecting this structure */ 146 struct uaudio_softc *priv_sc; 147 struct pcm_channel *pcm_ch; 148 struct usb2_xfer *xfer[UAUDIO_NCHANBUFS]; 149 const struct usb2_audio_streaming_interface_descriptor *p_asid; 150 const struct usb2_audio_streaming_type1_descriptor *p_asf1d; 151 const struct usb2_audio_streaming_endpoint_descriptor *p_sed; 152 const usb2_endpoint_descriptor_audio_t *p_ed1; 153 const usb2_endpoint_descriptor_audio_t *p_ed2; 154 const struct uaudio_format *p_fmt; 155 156 uint8_t *buf; /* pointer to buffer */ 157 uint8_t *start; /* upper layer buffer start */ 158 uint8_t *end; /* upper layer buffer end */ 159 uint8_t *cur; /* current position in upper layer 160 * buffer */ 161 162 uint32_t intr_size; /* in bytes */ 163 uint32_t block_size; 164 uint32_t sample_rate; 165 uint32_t format; 166 uint32_t pcm_format[2]; 167 168 uint16_t bytes_per_frame; 169 170 uint8_t valid; 171 uint8_t iface_index; 172 uint8_t iface_alt_index; 173 }; 174 175 #define UMIDI_N_TRANSFER 4 /* units */ 176 #define UMIDI_CABLES_MAX 16 /* units */ 177 #define UMIDI_BULK_SIZE 1024 /* bytes */ 178 179 struct umidi_sub_chan { 180 struct usb2_fifo_sc fifo; 181 uint8_t *temp_cmd; 182 uint8_t temp_0[4]; 183 uint8_t temp_1[4]; 184 uint8_t state; 185 #define UMIDI_ST_UNKNOWN 0 /* scan for command */ 186 #define UMIDI_ST_1PARAM 1 187 #define UMIDI_ST_2PARAM_1 2 188 #define UMIDI_ST_2PARAM_2 3 189 #define UMIDI_ST_SYSEX_0 4 190 #define UMIDI_ST_SYSEX_1 5 191 #define UMIDI_ST_SYSEX_2 6 192 193 uint8_t read_open:1; 194 uint8_t write_open:1; 195 uint8_t unused:6; 196 }; 197 198 struct umidi_chan { 199 200 struct umidi_sub_chan sub[UMIDI_CABLES_MAX]; 201 struct mtx mtx; 202 203 struct usb2_xfer *xfer[UMIDI_N_TRANSFER]; 204 205 uint8_t iface_index; 206 uint8_t iface_alt_index; 207 208 uint8_t flags; 209 #define UMIDI_FLAG_READ_STALL 0x01 210 #define UMIDI_FLAG_WRITE_STALL 0x02 211 212 uint8_t read_open_refcount; 213 uint8_t write_open_refcount; 214 215 uint8_t curr_cable; 216 uint8_t max_cable; 217 uint8_t valid; 218 }; 219 220 struct uaudio_softc { 221 struct sbuf sc_sndstat; 222 struct sndcard_func sc_sndcard_func; 223 struct uaudio_chan sc_rec_chan; 224 struct uaudio_chan sc_play_chan; 225 struct umidi_chan sc_midi_chan; 226 227 struct usb2_device *sc_udev; 228 struct usb2_xfer *sc_mixer_xfer[1]; 229 struct uaudio_mixer_node *sc_mixer_root; 230 struct uaudio_mixer_node *sc_mixer_curr; 231 232 uint32_t sc_mix_info; 233 uint32_t sc_recsrc_info; 234 235 uint16_t sc_audio_rev; 236 uint16_t sc_mixer_count; 237 238 uint8_t sc_sndstat_valid; 239 uint8_t sc_mixer_iface_index; 240 uint8_t sc_mixer_iface_no; 241 uint8_t sc_mixer_chan; 242 uint8_t sc_pcm_registered:1; 243 uint8_t sc_mixer_init:1; 244 uint8_t sc_uq_audio_swap_lr:1; 245 uint8_t sc_uq_au_inp_async:1; 246 uint8_t sc_uq_au_no_xu:1; 247 uint8_t sc_uq_bad_adc:1; 248 }; 249 250 struct uaudio_search_result { 251 uint8_t bit_input[(256 + 7) / 8]; 252 uint8_t bit_output[(256 + 7) / 8]; 253 uint8_t bit_visited[(256 + 7) / 8]; 254 uint8_t recurse_level; 255 uint8_t id_max; 256 }; 257 258 struct uaudio_terminal_node { 259 union { 260 const struct usb2_descriptor *desc; 261 const struct usb2_audio_input_terminal *it; 262 const struct usb2_audio_output_terminal *ot; 263 const struct usb2_audio_mixer_unit_0 *mu; 264 const struct usb2_audio_selector_unit *su; 265 const struct usb2_audio_feature_unit *fu; 266 const struct usb2_audio_processing_unit_0 *pu; 267 const struct usb2_audio_extension_unit_0 *eu; 268 } u; 269 struct uaudio_search_result usr; 270 struct uaudio_terminal_node *root; 271 }; 272 273 struct uaudio_format { 274 uint16_t wFormat; 275 uint8_t bPrecision; 276 uint32_t freebsd_fmt; 277 const char *description; 278 }; 279 280 static const struct uaudio_format uaudio_formats[] = { 281 282 {UA_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"}, 283 {UA_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"}, 284 {UA_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"}, 285 {UA_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"}, 286 287 {UA_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"}, 288 {UA_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"}, 289 {UA_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"}, 290 {UA_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"}, 291 292 {UA_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"}, 293 {UA_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"}, 294 295 {0, 0, 0, NULL} 296 }; 297 298 #define UAC_OUTPUT 0 299 #define UAC_INPUT 1 300 #define UAC_EQUAL 2 301 #define UAC_RECORD 3 302 #define UAC_NCLASSES 4 303 304 #if USB_DEBUG 305 static const char *uac_names[] = { 306 "outputs", "inputs", "equalization", "record" 307 }; 308 309 #endif 310 311 /* prototypes */ 312 313 static device_probe_t uaudio_probe; 314 static device_attach_t uaudio_attach; 315 static device_detach_t uaudio_detach; 316 317 static usb2_callback_t uaudio_chan_play_callback; 318 static usb2_callback_t uaudio_chan_record_callback; 319 static usb2_callback_t uaudio_mixer_write_cfg_callback; 320 static usb2_callback_t umidi_read_clear_stall_callback; 321 static usb2_callback_t umidi_bulk_read_callback; 322 static usb2_callback_t umidi_write_clear_stall_callback; 323 static usb2_callback_t umidi_bulk_write_callback; 324 325 static void uaudio_chan_fill_info_sub(struct uaudio_softc *, 326 struct usb2_device *, uint32_t, uint16_t, uint8_t, uint8_t); 327 static void uaudio_chan_fill_info(struct uaudio_softc *, 328 struct usb2_device *); 329 static void uaudio_mixer_add_ctl_sub(struct uaudio_softc *, 330 struct uaudio_mixer_node *); 331 static void uaudio_mixer_add_ctl(struct uaudio_softc *, 332 struct uaudio_mixer_node *); 333 static void uaudio_mixer_add_input(struct uaudio_softc *, 334 const struct uaudio_terminal_node *, int); 335 static void uaudio_mixer_add_output(struct uaudio_softc *, 336 const struct uaudio_terminal_node *, int); 337 static void uaudio_mixer_add_mixer(struct uaudio_softc *, 338 const struct uaudio_terminal_node *, int); 339 static void uaudio_mixer_add_selector(struct uaudio_softc *, 340 const struct uaudio_terminal_node *, int); 341 static uint32_t uaudio_mixer_feature_get_bmaControls( 342 const struct usb2_audio_feature_unit *, uint8_t); 343 static void uaudio_mixer_add_feature(struct uaudio_softc *, 344 const struct uaudio_terminal_node *, int); 345 static void uaudio_mixer_add_processing_updown(struct uaudio_softc *, 346 const struct uaudio_terminal_node *, int); 347 static void uaudio_mixer_add_processing(struct uaudio_softc *, 348 const struct uaudio_terminal_node *, int); 349 static void uaudio_mixer_add_extension(struct uaudio_softc *, 350 const struct uaudio_terminal_node *, int); 351 static struct usb2_audio_cluster uaudio_mixer_get_cluster(uint8_t, 352 const struct uaudio_terminal_node *); 353 static uint16_t uaudio_mixer_determine_class(const struct uaudio_terminal_node *, 354 struct uaudio_mixer_node *); 355 static uint16_t uaudio_mixer_feature_name(const struct uaudio_terminal_node *, 356 struct uaudio_mixer_node *); 357 static const struct uaudio_terminal_node *uaudio_mixer_get_input( 358 const struct uaudio_terminal_node *, uint8_t); 359 static const struct uaudio_terminal_node *uaudio_mixer_get_output( 360 const struct uaudio_terminal_node *, uint8_t); 361 static void uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *, 362 const uint8_t *, uint8_t, struct uaudio_search_result *); 363 static void uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *, 364 uint8_t, uint8_t, struct uaudio_search_result *); 365 static void uaudio_mixer_fill_info(struct uaudio_softc *, 366 struct usb2_device *, void *); 367 static uint16_t uaudio_mixer_get(struct usb2_device *, uint8_t, 368 struct uaudio_mixer_node *); 369 static void uaudio_mixer_ctl_set(struct uaudio_softc *, 370 struct uaudio_mixer_node *, uint8_t, int32_t val); 371 static usb2_error_t uaudio_set_speed(struct usb2_device *, uint8_t, uint32_t); 372 static int uaudio_mixer_signext(uint8_t, int); 373 static int uaudio_mixer_bsd2value(struct uaudio_mixer_node *, int32_t val); 374 static const void *uaudio_mixer_verify_desc(const void *, uint32_t); 375 static void uaudio_mixer_init(struct uaudio_softc *); 376 static uint8_t umidi_convert_to_usb(struct umidi_sub_chan *, uint8_t, uint8_t); 377 static struct umidi_sub_chan *umidi_sub_by_fifo(struct usb2_fifo *); 378 static void umidi_start_read(struct usb2_fifo *); 379 static void umidi_stop_read(struct usb2_fifo *); 380 static void umidi_start_write(struct usb2_fifo *); 381 static void umidi_stop_write(struct usb2_fifo *); 382 static int umidi_open(struct usb2_fifo *, int); 383 static int umidi_ioctl(struct usb2_fifo *, u_long cmd, void *, int); 384 static void umidi_close(struct usb2_fifo *, int); 385 static void umidi_init(device_t dev); 386 static int32_t umidi_probe(device_t dev); 387 static int32_t umidi_detach(device_t dev); 388 389 #if USB_DEBUG 390 static void uaudio_chan_dump_ep_desc( 391 const usb2_endpoint_descriptor_audio_t *); 392 static void uaudio_mixer_dump_cluster(uint8_t, 393 const struct uaudio_terminal_node *); 394 static const char *uaudio_mixer_get_terminal_name(uint16_t); 395 #endif 396 397 static const struct usb2_config 398 uaudio_cfg_record[UAUDIO_NCHANBUFS] = { 399 [0] = { 400 .type = UE_ISOCHRONOUS, 401 .endpoint = UE_ADDR_ANY, 402 .direction = UE_DIR_IN, 403 .mh.bufsize = 0, /* use "wMaxPacketSize * frames" */ 404 .mh.frames = UAUDIO_MINFRAMES, 405 .mh.flags = {.short_xfer_ok = 1,}, 406 .mh.callback = &uaudio_chan_record_callback, 407 }, 408 409 [1] = { 410 .type = UE_ISOCHRONOUS, 411 .endpoint = UE_ADDR_ANY, 412 .direction = UE_DIR_IN, 413 .mh.bufsize = 0, /* use "wMaxPacketSize * frames" */ 414 .mh.frames = UAUDIO_MINFRAMES, 415 .mh.flags = {.short_xfer_ok = 1,}, 416 .mh.callback = &uaudio_chan_record_callback, 417 }, 418 }; 419 420 static const struct usb2_config 421 uaudio_cfg_play[UAUDIO_NCHANBUFS] = { 422 [0] = { 423 .type = UE_ISOCHRONOUS, 424 .endpoint = UE_ADDR_ANY, 425 .direction = UE_DIR_OUT, 426 .mh.bufsize = 0, /* use "wMaxPacketSize * frames" */ 427 .mh.frames = UAUDIO_MINFRAMES, 428 .mh.flags = {.short_xfer_ok = 1,}, 429 .mh.callback = &uaudio_chan_play_callback, 430 }, 431 432 [1] = { 433 .type = UE_ISOCHRONOUS, 434 .endpoint = UE_ADDR_ANY, 435 .direction = UE_DIR_OUT, 436 .mh.bufsize = 0, /* use "wMaxPacketSize * frames" */ 437 .mh.frames = UAUDIO_MINFRAMES, 438 .mh.flags = {.short_xfer_ok = 1,}, 439 .mh.callback = &uaudio_chan_play_callback, 440 }, 441 }; 442 443 static const struct usb2_config 444 uaudio_mixer_config[1] = { 445 [0] = { 446 .type = UE_CONTROL, 447 .endpoint = 0x00, /* Control pipe */ 448 .direction = UE_DIR_ANY, 449 .mh.bufsize = (sizeof(struct usb2_device_request) + 4), 450 .mh.callback = &uaudio_mixer_write_cfg_callback, 451 .mh.timeout = 1000, /* 1 second */ 452 }, 453 }; 454 455 static const 456 uint8_t umidi_cmd_to_len[16] = { 457 [0x0] = 0, /* reserved */ 458 [0x1] = 0, /* reserved */ 459 [0x2] = 2, /* bytes */ 460 [0x3] = 3, /* bytes */ 461 [0x4] = 3, /* bytes */ 462 [0x5] = 1, /* bytes */ 463 [0x6] = 2, /* bytes */ 464 [0x7] = 3, /* bytes */ 465 [0x8] = 3, /* bytes */ 466 [0x9] = 3, /* bytes */ 467 [0xA] = 3, /* bytes */ 468 [0xB] = 3, /* bytes */ 469 [0xC] = 2, /* bytes */ 470 [0xD] = 2, /* bytes */ 471 [0xE] = 3, /* bytes */ 472 [0xF] = 1, /* bytes */ 473 }; 474 475 static const struct usb2_config 476 umidi_config[UMIDI_N_TRANSFER] = { 477 [0] = { 478 .type = UE_BULK, 479 .endpoint = UE_ADDR_ANY, 480 .direction = UE_DIR_OUT, 481 .mh.bufsize = UMIDI_BULK_SIZE, 482 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 483 .mh.callback = &umidi_bulk_write_callback, 484 }, 485 486 [1] = { 487 .type = UE_BULK, 488 .endpoint = UE_ADDR_ANY, 489 .direction = UE_DIR_IN, 490 .mh.bufsize = UMIDI_BULK_SIZE, 491 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 492 .mh.callback = &umidi_bulk_read_callback, 493 }, 494 495 [2] = { 496 .type = UE_CONTROL, 497 .endpoint = 0x00, /* Control pipe */ 498 .direction = UE_DIR_ANY, 499 .mh.bufsize = sizeof(struct usb2_device_request), 500 .mh.flags = {}, 501 .mh.callback = &umidi_write_clear_stall_callback, 502 .mh.timeout = 1000, /* 1 second */ 503 .mh.interval = 50, /* 50ms */ 504 }, 505 506 [3] = { 507 .type = UE_CONTROL, 508 .endpoint = 0x00, /* Control pipe */ 509 .direction = UE_DIR_ANY, 510 .mh.bufsize = sizeof(struct usb2_device_request), 511 .mh.flags = {}, 512 .mh.callback = &umidi_read_clear_stall_callback, 513 .mh.timeout = 1000, /* 1 second */ 514 .mh.interval = 50, /* 50ms */ 515 }, 516 }; 517 518 static devclass_t uaudio_devclass; 519 520 static device_method_t uaudio_methods[] = { 521 DEVMETHOD(device_probe, uaudio_probe), 522 DEVMETHOD(device_attach, uaudio_attach), 523 DEVMETHOD(device_detach, uaudio_detach), 524 DEVMETHOD(device_suspend, bus_generic_suspend), 525 DEVMETHOD(device_resume, bus_generic_resume), 526 DEVMETHOD(device_shutdown, bus_generic_shutdown), 527 DEVMETHOD(bus_print_child, bus_generic_print_child), 528 {0, 0} 529 }; 530 531 static driver_t uaudio_driver = { 532 .name = "uaudio", 533 .methods = uaudio_methods, 534 .size = sizeof(struct uaudio_softc), 535 }; 536 537 static int 538 uaudio_probe(device_t dev) 539 { 540 struct usb2_attach_arg *uaa = device_get_ivars(dev); 541 542 if (uaa->usb2_mode != USB_MODE_HOST) 543 return (ENXIO); 544 545 if (uaa->use_generic == 0) 546 return (ENXIO); 547 548 /* trigger on the control interface */ 549 550 if ((uaa->info.bInterfaceClass == UICLASS_AUDIO) && 551 (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL)) { 552 if (usb2_test_quirk(uaa, UQ_BAD_AUDIO)) 553 return (ENXIO); 554 else 555 return (0); 556 } 557 return (ENXIO); 558 } 559 560 static int 561 uaudio_attach(device_t dev) 562 { 563 struct usb2_attach_arg *uaa = device_get_ivars(dev); 564 struct uaudio_softc *sc = device_get_softc(dev); 565 struct usb2_interface_descriptor *id; 566 device_t child; 567 568 sc->sc_play_chan.priv_sc = sc; 569 sc->sc_rec_chan.priv_sc = sc; 570 sc->sc_udev = uaa->device; 571 572 if (usb2_test_quirk(uaa, UQ_AUDIO_SWAP_LR)) 573 sc->sc_uq_audio_swap_lr = 1; 574 575 if (usb2_test_quirk(uaa, UQ_AU_INP_ASYNC)) 576 sc->sc_uq_au_inp_async = 1; 577 578 if (usb2_test_quirk(uaa, UQ_AU_NO_XU)) 579 sc->sc_uq_au_no_xu = 1; 580 581 if (usb2_test_quirk(uaa, UQ_BAD_ADC)) 582 sc->sc_uq_bad_adc = 1; 583 584 umidi_init(dev); 585 586 device_set_usb2_desc(dev); 587 588 id = usb2_get_interface_descriptor(uaa->iface); 589 590 uaudio_chan_fill_info(sc, uaa->device); 591 592 uaudio_mixer_fill_info(sc, uaa->device, id); 593 594 sc->sc_mixer_iface_index = uaa->info.bIfaceIndex; 595 sc->sc_mixer_iface_no = uaa->info.bIfaceNum; 596 597 DPRINTF("audio rev %d.%02x\n", 598 sc->sc_audio_rev >> 8, 599 sc->sc_audio_rev & 0xff); 600 601 DPRINTF("%d mixer controls\n", 602 sc->sc_mixer_count); 603 604 if (sc->sc_play_chan.valid) { 605 device_printf(dev, "Play: %d Hz, %d ch, %s format\n", 606 sc->sc_play_chan.sample_rate, 607 sc->sc_play_chan.p_asf1d->bNrChannels, 608 sc->sc_play_chan.p_fmt->description); 609 } else { 610 device_printf(dev, "No playback!\n"); 611 } 612 613 if (sc->sc_rec_chan.valid) { 614 device_printf(dev, "Record: %d Hz, %d ch, %s format\n", 615 sc->sc_rec_chan.sample_rate, 616 sc->sc_rec_chan.p_asf1d->bNrChannels, 617 sc->sc_rec_chan.p_fmt->description); 618 } else { 619 device_printf(dev, "No recording!\n"); 620 } 621 622 if (sc->sc_midi_chan.valid) { 623 624 if (umidi_probe(dev)) { 625 goto detach; 626 } 627 device_printf(dev, "MIDI sequencer\n"); 628 } else { 629 device_printf(dev, "No midi sequencer\n"); 630 } 631 632 DPRINTF("doing child attach\n"); 633 634 /* attach the children */ 635 636 sc->sc_sndcard_func.func = SCF_PCM; 637 638 child = device_add_child(dev, "pcm", -1); 639 640 if (child == NULL) { 641 DPRINTF("out of memory\n"); 642 goto detach; 643 } 644 device_set_ivars(child, &sc->sc_sndcard_func); 645 646 if (bus_generic_attach(dev)) { 647 DPRINTF("child attach failed\n"); 648 goto detach; 649 } 650 return (0); /* success */ 651 652 detach: 653 uaudio_detach(dev); 654 return (ENXIO); 655 } 656 657 static void 658 uaudio_pcm_setflags(device_t dev, uint32_t flags) 659 { 660 pcm_setflags(dev, pcm_getflags(dev) | flags); 661 } 662 663 int 664 uaudio_attach_sub(device_t dev, kobj_class_t mixer_class, kobj_class_t chan_class) 665 { 666 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev)); 667 char status[SND_STATUSLEN]; 668 669 uaudio_mixer_init(sc); 670 671 if (sc->sc_uq_audio_swap_lr) { 672 DPRINTF("hardware has swapped left and right\n"); 673 uaudio_pcm_setflags(dev, SD_F_PSWAPLR); 674 } 675 if (!(sc->sc_mix_info & SOUND_MASK_PCM)) { 676 677 DPRINTF("emulating master volume\n"); 678 679 /* 680 * Emulate missing pcm mixer controller 681 * through FEEDER_VOLUME 682 */ 683 uaudio_pcm_setflags(dev, SD_F_SOFTPCMVOL); 684 } 685 if (mixer_init(dev, mixer_class, sc)) { 686 goto detach; 687 } 688 sc->sc_mixer_init = 1; 689 690 snprintf(status, sizeof(status), "at ? %s", PCM_KLDSTRING(snd_uaudio)); 691 692 if (pcm_register(dev, sc, 693 sc->sc_play_chan.valid ? 1 : 0, 694 sc->sc_rec_chan.valid ? 1 : 0)) { 695 goto detach; 696 } 697 sc->sc_pcm_registered = 1; 698 699 if (sc->sc_play_chan.valid) { 700 pcm_addchan(dev, PCMDIR_PLAY, chan_class, sc); 701 } 702 if (sc->sc_rec_chan.valid) { 703 pcm_addchan(dev, PCMDIR_REC, chan_class, sc); 704 } 705 pcm_setstatus(dev, status); 706 707 return (0); /* success */ 708 709 detach: 710 uaudio_detach_sub(dev); 711 return (ENXIO); 712 } 713 714 int 715 uaudio_detach_sub(device_t dev) 716 { 717 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev)); 718 int error = 0; 719 720 repeat: 721 if (sc->sc_pcm_registered) { 722 error = pcm_unregister(dev); 723 } else { 724 if (sc->sc_mixer_init) { 725 error = mixer_uninit(dev); 726 } 727 } 728 729 if (error) { 730 device_printf(dev, "Waiting for sound application to exit!\n"); 731 usb2_pause_mtx(NULL, 2 * hz); 732 goto repeat; /* try again */ 733 } 734 return (0); /* success */ 735 } 736 737 static int 738 uaudio_detach(device_t dev) 739 { 740 struct uaudio_softc *sc = device_get_softc(dev); 741 742 if (bus_generic_detach(dev)) { 743 DPRINTF("detach failed!\n"); 744 } 745 sbuf_delete(&sc->sc_sndstat); 746 sc->sc_sndstat_valid = 0; 747 748 umidi_detach(dev); 749 750 return (0); 751 } 752 753 /*========================================================================* 754 * AS - Audio Stream - routines 755 *========================================================================*/ 756 757 #if USB_DEBUG 758 static void 759 uaudio_chan_dump_ep_desc(const usb2_endpoint_descriptor_audio_t *ed) 760 { 761 if (ed) { 762 DPRINTF("endpoint=%p bLength=%d bDescriptorType=%d \n" 763 "bEndpointAddress=%d bmAttributes=0x%x \n" 764 "wMaxPacketSize=%d bInterval=%d \n" 765 "bRefresh=%d bSynchAddress=%d\n", 766 ed, ed->bLength, ed->bDescriptorType, 767 ed->bEndpointAddress, ed->bmAttributes, 768 UGETW(ed->wMaxPacketSize), ed->bInterval, 769 ed->bRefresh, ed->bSynchAddress); 770 } 771 } 772 773 #endif 774 775 static void 776 uaudio_chan_fill_info_sub(struct uaudio_softc *sc, struct usb2_device *udev, 777 uint32_t rate, uint16_t fps, uint8_t channels, 778 uint8_t bit_resolution) 779 { 780 struct usb2_descriptor *desc = NULL; 781 const struct usb2_audio_streaming_interface_descriptor *asid = NULL; 782 const struct usb2_audio_streaming_type1_descriptor *asf1d = NULL; 783 const struct usb2_audio_streaming_endpoint_descriptor *sed = NULL; 784 const usb2_endpoint_descriptor_audio_t *ed1 = NULL; 785 const usb2_endpoint_descriptor_audio_t *ed2 = NULL; 786 struct usb2_config_descriptor *cd = usb2_get_config_descriptor(udev); 787 struct usb2_interface_descriptor *id; 788 const struct uaudio_format *p_fmt; 789 struct uaudio_chan *chan; 790 uint16_t curidx = 0xFFFF; 791 uint16_t lastidx = 0xFFFF; 792 uint16_t alt_index = 0; 793 uint16_t wFormat; 794 uint8_t ep_dir; 795 uint8_t ep_type; 796 uint8_t ep_sync; 797 uint8_t bChannels; 798 uint8_t bBitResolution; 799 uint8_t x; 800 uint8_t audio_if = 0; 801 uint8_t sample_size; 802 803 while ((desc = usb2_desc_foreach(cd, desc))) { 804 805 if ((desc->bDescriptorType == UDESC_INTERFACE) && 806 (desc->bLength >= sizeof(*id))) { 807 808 id = (void *)desc; 809 810 if (id->bInterfaceNumber != lastidx) { 811 lastidx = id->bInterfaceNumber; 812 curidx++; 813 alt_index = 0; 814 815 } else { 816 alt_index++; 817 } 818 819 if ((id->bInterfaceClass == UICLASS_AUDIO) && 820 (id->bInterfaceSubClass == UISUBCLASS_AUDIOSTREAM)) { 821 audio_if = 1; 822 } else { 823 audio_if = 0; 824 } 825 826 if ((id->bInterfaceClass == UICLASS_AUDIO) && 827 (id->bInterfaceSubClass == UISUBCLASS_MIDISTREAM)) { 828 829 /* 830 * XXX could allow multiple MIDI interfaces 831 * XXX 832 */ 833 834 if ((sc->sc_midi_chan.valid == 0) && 835 usb2_get_iface(udev, curidx)) { 836 sc->sc_midi_chan.iface_index = curidx; 837 sc->sc_midi_chan.iface_alt_index = alt_index; 838 sc->sc_midi_chan.valid = 1; 839 } 840 } 841 asid = NULL; 842 asf1d = NULL; 843 ed1 = NULL; 844 ed2 = NULL; 845 sed = NULL; 846 } 847 if ((desc->bDescriptorType == UDESC_CS_INTERFACE) && 848 (desc->bDescriptorSubtype == AS_GENERAL) && 849 (desc->bLength >= sizeof(*asid))) { 850 if (asid == NULL) { 851 asid = (void *)desc; 852 } 853 } 854 if ((desc->bDescriptorType == UDESC_CS_INTERFACE) && 855 (desc->bDescriptorSubtype == FORMAT_TYPE) && 856 (desc->bLength >= sizeof(*asf1d))) { 857 if (asf1d == NULL) { 858 asf1d = (void *)desc; 859 if (asf1d->bFormatType != FORMAT_TYPE_I) { 860 DPRINTFN(11, "ignored bFormatType = %d\n", 861 asf1d->bFormatType); 862 asf1d = NULL; 863 continue; 864 } 865 if (asf1d->bLength < (sizeof(*asf1d) + 866 (asf1d->bSamFreqType == 0) ? 6 : 867 (asf1d->bSamFreqType * 3))) { 868 DPRINTFN(11, "'asf1d' descriptor is too short\n"); 869 asf1d = NULL; 870 continue; 871 } 872 } 873 } 874 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 875 (desc->bLength >= sizeof(*ed1))) { 876 if (ed1 == NULL) { 877 ed1 = (void *)desc; 878 if (UE_GET_XFERTYPE(ed1->bmAttributes) != UE_ISOCHRONOUS) { 879 ed1 = NULL; 880 } 881 } else { 882 if (ed2 == NULL) { 883 ed2 = (void *)desc; 884 if (UE_GET_XFERTYPE(ed2->bmAttributes) != UE_ISOCHRONOUS) { 885 ed2 = NULL; 886 continue; 887 } 888 if (ed2->bSynchAddress != 0) { 889 DPRINTFN(11, "invalid endpoint: bSynchAddress != 0\n"); 890 ed2 = NULL; 891 continue; 892 } 893 if (ed2->bEndpointAddress != ed1->bSynchAddress) { 894 DPRINTFN(11, "invalid endpoint addresses: " 895 "ep[0]->bSynchAddress=0x%x " 896 "ep[1]->bEndpointAddress=0x%x\n", 897 ed1->bSynchAddress, 898 ed2->bEndpointAddress); 899 ed2 = NULL; 900 continue; 901 } 902 } 903 } 904 } 905 if ((desc->bDescriptorType == UDESC_CS_ENDPOINT) && 906 (desc->bDescriptorSubtype == AS_GENERAL) && 907 (desc->bLength >= sizeof(*sed))) { 908 if (sed == NULL) { 909 sed = (void *)desc; 910 } 911 } 912 if (audio_if && asid && asf1d && ed1 && sed) { 913 914 ep_dir = UE_GET_DIR(ed1->bEndpointAddress); 915 ep_type = UE_GET_ISO_TYPE(ed1->bmAttributes); 916 ep_sync = 0; 917 918 if ((sc->sc_uq_au_inp_async) && 919 (ep_dir == UE_DIR_IN) && (ep_type == UE_ISO_ADAPT)) { 920 ep_type = UE_ISO_ASYNC; 921 } 922 if ((ep_dir == UE_DIR_IN) && (ep_type == UE_ISO_ADAPT)) { 923 ep_sync = 1; 924 } 925 if ((ep_dir != UE_DIR_IN) && (ep_type == UE_ISO_ASYNC)) { 926 ep_sync = 1; 927 } 928 /* Ignore sync endpoint information until further. */ 929 #if 0 930 if (ep_sync && (!ed2)) { 931 continue; 932 } 933 /* 934 * we can't handle endpoints that need a sync pipe 935 * yet 936 */ 937 938 if (ep_sync) { 939 DPRINTF("skipped sync interface\n"); 940 audio_if = 0; 941 continue; 942 } 943 #endif 944 945 wFormat = UGETW(asid->wFormatTag); 946 bChannels = asf1d->bNrChannels; 947 bBitResolution = asf1d->bBitResolution; 948 949 if (asf1d->bSamFreqType == 0) { 950 DPRINTFN(16, "Sample rate: %d-%dHz\n", 951 UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d)); 952 953 if ((rate >= UA_SAMP_LO(asf1d)) && 954 (rate <= UA_SAMP_HI(asf1d))) { 955 goto found_rate; 956 } 957 } else { 958 959 for (x = 0; x < asf1d->bSamFreqType; x++) { 960 DPRINTFN(16, "Sample rate = %dHz\n", 961 UA_GETSAMP(asf1d, x)); 962 963 if (rate == UA_GETSAMP(asf1d, x)) { 964 goto found_rate; 965 } 966 } 967 } 968 969 audio_if = 0; 970 continue; 971 972 found_rate: 973 974 for (p_fmt = uaudio_formats; 975 p_fmt->wFormat; 976 p_fmt++) { 977 if ((p_fmt->wFormat == wFormat) && 978 (p_fmt->bPrecision == bBitResolution)) { 979 goto found_format; 980 } 981 } 982 983 audio_if = 0; 984 continue; 985 986 found_format: 987 988 if ((bChannels == channels) && 989 (bBitResolution == bit_resolution)) { 990 991 chan = (ep_dir == UE_DIR_IN) ? 992 &sc->sc_rec_chan : 993 &sc->sc_play_chan; 994 995 if ((chan->valid == 0) && usb2_get_iface(udev, curidx)) { 996 997 chan->valid = 1; 998 #if USB_DEBUG 999 uaudio_chan_dump_ep_desc(ed1); 1000 uaudio_chan_dump_ep_desc(ed2); 1001 1002 if (sed->bmAttributes & UA_SED_FREQ_CONTROL) { 1003 DPRINTFN(2, "FREQ_CONTROL\n"); 1004 } 1005 if (sed->bmAttributes & UA_SED_PITCH_CONTROL) { 1006 DPRINTFN(2, "PITCH_CONTROL\n"); 1007 } 1008 #endif 1009 DPRINTF("Sample rate = %dHz, channels = %d, " 1010 "bits = %d, format = %s\n", rate, channels, 1011 bit_resolution, p_fmt->description); 1012 1013 chan->sample_rate = rate; 1014 chan->p_asid = asid; 1015 chan->p_asf1d = asf1d; 1016 chan->p_ed1 = ed1; 1017 chan->p_ed2 = ed2; 1018 chan->p_fmt = p_fmt; 1019 chan->p_sed = sed; 1020 chan->iface_index = curidx; 1021 chan->iface_alt_index = alt_index; 1022 1023 if (ep_dir == UE_DIR_IN) 1024 chan->usb2_cfg = 1025 uaudio_cfg_record; 1026 else 1027 chan->usb2_cfg = 1028 uaudio_cfg_play; 1029 1030 sample_size = ((chan->p_asf1d->bNrChannels * 1031 chan->p_asf1d->bBitResolution) / 8); 1032 1033 /* 1034 * NOTE: "chan->bytes_per_frame" 1035 * should not be zero! 1036 */ 1037 chan->bytes_per_frame = ((rate / fps) * sample_size); 1038 1039 if (sc->sc_sndstat_valid) { 1040 sbuf_printf(&sc->sc_sndstat, "\n\t" 1041 "mode %d.%d:(%s) %dch, %d/%dbit, %s, %dHz", 1042 curidx, alt_index, 1043 (ep_dir == UE_DIR_IN) ? "input" : "output", 1044 asf1d->bNrChannels, asf1d->bBitResolution, 1045 asf1d->bSubFrameSize * 8, 1046 p_fmt->description, rate); 1047 } 1048 } 1049 } 1050 audio_if = 0; 1051 continue; 1052 } 1053 } 1054 } 1055 1056 static void 1057 uaudio_chan_fill_info(struct uaudio_softc *sc, struct usb2_device *udev) 1058 { 1059 uint32_t rate = uaudio_default_rate; 1060 uint32_t z; 1061 uint16_t fps = usb2_get_isoc_fps(udev); 1062 uint8_t bits = uaudio_default_bits; 1063 uint8_t y; 1064 uint8_t channels = uaudio_default_channels; 1065 uint8_t x; 1066 1067 bits -= (bits % 8); 1068 if ((bits == 0) || (bits > 32)) { 1069 /* set a valid value */ 1070 bits = 32; 1071 } 1072 rate -= (rate % fps); 1073 if ((rate == 0) || (rate > 192000)) { 1074 /* set a valid value */ 1075 rate = 192000 - (192000 % fps); 1076 } 1077 if ((channels == 0) || (channels > 2)) { 1078 /* set a valid value */ 1079 channels = 2; 1080 } 1081 if (sbuf_new(&sc->sc_sndstat, NULL, 4096, SBUF_AUTOEXTEND)) { 1082 sc->sc_sndstat_valid = 1; 1083 } 1084 /* try to search for a valid config */ 1085 1086 for (x = channels; x; x--) { 1087 for (y = bits; y; y -= 8) { 1088 for (z = rate; z; z -= fps) { 1089 uaudio_chan_fill_info_sub(sc, udev, z, fps, x, y); 1090 1091 if (sc->sc_rec_chan.valid && 1092 sc->sc_play_chan.valid) { 1093 goto done; 1094 } 1095 } 1096 } 1097 } 1098 1099 done: 1100 if (sc->sc_sndstat_valid) { 1101 sbuf_finish(&sc->sc_sndstat); 1102 } 1103 } 1104 1105 static void 1106 uaudio_chan_play_callback(struct usb2_xfer *xfer) 1107 { 1108 struct uaudio_chan *ch = xfer->priv_sc; 1109 uint32_t *p_len = xfer->frlengths; 1110 uint32_t total; 1111 uint32_t blockcount; 1112 uint32_t n; 1113 uint32_t offset; 1114 1115 /* allow dynamic sizing of play buffer */ 1116 total = ch->intr_size; 1117 1118 /* allow dynamic sizing of play buffer */ 1119 blockcount = total / ch->bytes_per_frame; 1120 1121 /* align units */ 1122 blockcount -= (blockcount % UAUDIO_MINFRAMES); 1123 1124 /* range check - min */ 1125 if (blockcount == 0) { 1126 blockcount = UAUDIO_MINFRAMES; 1127 } 1128 /* range check - max */ 1129 if (blockcount > xfer->max_frame_count) { 1130 blockcount = xfer->max_frame_count; 1131 } 1132 /* compute the total length */ 1133 total = blockcount * ch->bytes_per_frame; 1134 1135 switch (USB_GET_STATE(xfer)) { 1136 case USB_ST_TRANSFERRED: 1137 tr_transferred: 1138 if (xfer->actlen < xfer->sumlen) { 1139 DPRINTF("short transfer, " 1140 "%d of %d bytes\n", xfer->actlen, total); 1141 } 1142 chn_intr(ch->pcm_ch); 1143 1144 case USB_ST_SETUP: 1145 if (ch->bytes_per_frame > xfer->max_frame_size) { 1146 DPRINTF("bytes per transfer, %d, " 1147 "exceeds maximum, %d!\n", 1148 ch->bytes_per_frame, 1149 xfer->max_frame_size); 1150 break; 1151 } 1152 /* setup frame length */ 1153 xfer->nframes = blockcount; 1154 for (n = 0; n != blockcount; n++) { 1155 p_len[n] = ch->bytes_per_frame; 1156 } 1157 1158 if (ch->end == ch->start) { 1159 DPRINTF("no buffer!\n"); 1160 break; 1161 } 1162 DPRINTFN(6, "transfer %d bytes\n", total); 1163 1164 offset = 0; 1165 1166 while (total > 0) { 1167 1168 n = (ch->end - ch->cur); 1169 if (n > total) { 1170 n = total; 1171 } 1172 usb2_copy_in(xfer->frbuffers, offset, ch->cur, n); 1173 1174 total -= n; 1175 ch->cur += n; 1176 offset += n; 1177 1178 if (ch->cur >= ch->end) { 1179 ch->cur = ch->start; 1180 } 1181 } 1182 1183 usb2_start_hardware(xfer); 1184 break; 1185 1186 default: /* Error */ 1187 if (xfer->error == USB_ERR_CANCELLED) { 1188 break; 1189 } 1190 goto tr_transferred; 1191 } 1192 } 1193 1194 static void 1195 uaudio_chan_record_callback(struct usb2_xfer *xfer) 1196 { 1197 struct uaudio_chan *ch = xfer->priv_sc; 1198 uint32_t *p_len = xfer->frlengths; 1199 uint32_t n; 1200 uint32_t m; 1201 uint32_t total; 1202 uint32_t blockcount; 1203 uint32_t offset0; 1204 uint32_t offset1; 1205 1206 /* allow dynamic sizing of play buffer */ 1207 total = ch->intr_size; 1208 1209 /* allow dynamic sizing of play buffer */ 1210 blockcount = total / ch->bytes_per_frame; 1211 1212 /* align units */ 1213 blockcount -= (blockcount % UAUDIO_MINFRAMES); 1214 1215 /* range check - min */ 1216 if (blockcount == 0) { 1217 blockcount = UAUDIO_MINFRAMES; 1218 } 1219 /* range check - max */ 1220 if (blockcount > xfer->max_frame_count) { 1221 blockcount = xfer->max_frame_count; 1222 } 1223 /* compute the total length */ 1224 total = blockcount * ch->bytes_per_frame; 1225 1226 switch (USB_GET_STATE(xfer)) { 1227 case USB_ST_TRANSFERRED: 1228 tr_transferred: 1229 if (xfer->actlen < total) { 1230 DPRINTF("short transfer, " 1231 "%d of %d bytes\n", xfer->actlen, total); 1232 } else { 1233 DPRINTFN(6, "transferred %d bytes\n", xfer->actlen); 1234 } 1235 1236 offset0 = 0; 1237 1238 for (n = 0; n != xfer->nframes; n++) { 1239 1240 offset1 = offset0; 1241 1242 while (p_len[n] > 0) { 1243 1244 m = (ch->end - ch->cur); 1245 1246 if (m > p_len[n]) { 1247 m = p_len[n]; 1248 } 1249 usb2_copy_out(xfer->frbuffers, offset1, ch->cur, m); 1250 1251 p_len[n] -= m; 1252 offset1 += m; 1253 ch->cur += m; 1254 1255 if (ch->cur >= ch->end) { 1256 ch->cur = ch->start; 1257 } 1258 } 1259 1260 offset0 += ch->bytes_per_frame; 1261 } 1262 1263 chn_intr(ch->pcm_ch); 1264 1265 case USB_ST_SETUP: 1266 if (ch->bytes_per_frame > xfer->max_frame_size) { 1267 DPRINTF("bytes per transfer, %d, " 1268 "exceeds maximum, %d!\n", 1269 ch->bytes_per_frame, 1270 xfer->max_frame_size); 1271 return; 1272 } 1273 xfer->nframes = blockcount; 1274 for (n = 0; n != xfer->nframes; n++) { 1275 p_len[n] = ch->bytes_per_frame; 1276 } 1277 1278 if (ch->end == ch->start) { 1279 DPRINTF("no buffer!\n"); 1280 return; 1281 } 1282 usb2_start_hardware(xfer); 1283 return; 1284 1285 default: /* Error */ 1286 if (xfer->error == USB_ERR_CANCELLED) { 1287 return; 1288 } 1289 goto tr_transferred; 1290 } 1291 } 1292 1293 void * 1294 uaudio_chan_init(struct uaudio_softc *sc, struct snd_dbuf *b, 1295 struct pcm_channel *c, int dir) 1296 { 1297 struct uaudio_chan *ch = ((dir == PCMDIR_PLAY) ? 1298 &sc->sc_play_chan : &sc->sc_rec_chan); 1299 uint32_t buf_size; 1300 uint8_t endpoint; 1301 uint8_t iface_index; 1302 uint8_t alt_index; 1303 usb2_error_t err; 1304 1305 /* compute required buffer size */ 1306 buf_size = (ch->bytes_per_frame * UAUDIO_MINFRAMES); 1307 1308 /* setup interrupt interval */ 1309 ch->intr_size = buf_size; 1310 1311 /* double buffering */ 1312 buf_size *= 2; 1313 1314 ch->buf = malloc(buf_size, M_DEVBUF, M_WAITOK | M_ZERO); 1315 if (ch->buf == NULL) { 1316 goto error; 1317 } 1318 if (sndbuf_setup(b, ch->buf, buf_size) != 0) { 1319 goto error; 1320 } 1321 ch->start = ch->buf; 1322 ch->end = ch->buf + buf_size; 1323 ch->cur = ch->buf; 1324 ch->pcm_ch = c; 1325 ch->pcm_mtx = c->lock; 1326 ch->pcm_buf = b; 1327 1328 if (ch->pcm_mtx == NULL) { 1329 DPRINTF("ERROR: PCM channels does not have a mutex!\n"); 1330 goto error; 1331 } 1332 /* setup play/record format */ 1333 1334 ch->pcm_cap.fmtlist = ch->pcm_format; 1335 1336 ch->pcm_format[0] = 0; 1337 ch->pcm_format[1] = 0; 1338 1339 ch->pcm_cap.minspeed = ch->sample_rate; 1340 ch->pcm_cap.maxspeed = ch->sample_rate; 1341 1342 ch->pcm_cap.fmtlist[0] = ch->p_fmt->freebsd_fmt; 1343 1344 if (ch->p_asf1d->bNrChannels == 2) { 1345 ch->pcm_cap.fmtlist[0] |= AFMT_STEREO; 1346 } 1347 ch->pcm_cap.fmtlist[1] = 0; 1348 1349 1350 /* set alternate interface corresponding to the mode */ 1351 1352 endpoint = ch->p_ed1->bEndpointAddress; 1353 iface_index = ch->iface_index; 1354 alt_index = ch->iface_alt_index; 1355 1356 DPRINTF("endpoint=0x%02x, speed=%d, iface=%d alt=%d\n", 1357 endpoint, ch->sample_rate, iface_index, alt_index); 1358 1359 err = usb2_set_alt_interface_index(sc->sc_udev, iface_index, alt_index); 1360 if (err) { 1361 DPRINTF("setting of alternate index failed: %s!\n", 1362 usb2_errstr(err)); 1363 goto error; 1364 } 1365 usb2_set_parent_iface(sc->sc_udev, iface_index, sc->sc_mixer_iface_index); 1366 1367 /* 1368 * If just one sampling rate is supported, 1369 * no need to call "uaudio_set_speed()". 1370 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request. 1371 */ 1372 if (ch->p_asf1d->bSamFreqType != 1) { 1373 if (uaudio_set_speed(sc->sc_udev, endpoint, ch->sample_rate)) { 1374 /* 1375 * If the endpoint is adaptive setting the speed may 1376 * fail. 1377 */ 1378 DPRINTF("setting of sample rate failed! (continuing anyway)\n"); 1379 } 1380 } 1381 if (usb2_transfer_setup(sc->sc_udev, &iface_index, ch->xfer, 1382 ch->usb2_cfg, UAUDIO_NCHANBUFS, ch, ch->pcm_mtx)) { 1383 DPRINTF("could not allocate USB transfers!\n"); 1384 goto error; 1385 } 1386 return (ch); 1387 1388 error: 1389 uaudio_chan_free(ch); 1390 return (NULL); 1391 } 1392 1393 int 1394 uaudio_chan_free(struct uaudio_chan *ch) 1395 { 1396 if (ch->buf != NULL) { 1397 free(ch->buf, M_DEVBUF); 1398 ch->buf = NULL; 1399 } 1400 usb2_transfer_unsetup(ch->xfer, UAUDIO_NCHANBUFS); 1401 1402 ch->valid = 0; 1403 1404 return (0); 1405 } 1406 1407 int 1408 uaudio_chan_set_param_blocksize(struct uaudio_chan *ch, uint32_t blocksize) 1409 { 1410 uaudio_chan_set_param_fragments(ch, blocksize, 0 - 1); 1411 1412 return (ch->block_size); 1413 } 1414 1415 int 1416 uaudio_chan_set_param_fragments(struct uaudio_chan *ch, uint32_t blocksize, 1417 uint32_t blockcount) 1418 { 1419 /* we only support one size */ 1420 blocksize = ch->intr_size; 1421 blockcount = 2; 1422 1423 if ((sndbuf_getblksz(ch->pcm_buf) != blocksize) || 1424 (sndbuf_getblkcnt(ch->pcm_buf) != blockcount)) { 1425 DPRINTFN(1, "resizing to %u x " 1426 "%u bytes\n", blockcount, blocksize); 1427 if (sndbuf_resize(ch->pcm_buf, blockcount, blocksize)) { 1428 DPRINTFN(0, "failed to resize sound buffer, count=%u, " 1429 "size=%u\n", blockcount, blocksize); 1430 } 1431 } 1432 ch->block_size = sndbuf_getblksz(ch->pcm_buf); 1433 1434 return (1); 1435 } 1436 1437 int 1438 uaudio_chan_set_param_speed(struct uaudio_chan *ch, uint32_t speed) 1439 { 1440 if (speed != ch->sample_rate) { 1441 DPRINTF("rate conversion required\n"); 1442 } 1443 return (ch->sample_rate); 1444 } 1445 1446 int 1447 uaudio_chan_getptr(struct uaudio_chan *ch) 1448 { 1449 return (ch->cur - ch->start); 1450 } 1451 1452 struct pcmchan_caps * 1453 uaudio_chan_getcaps(struct uaudio_chan *ch) 1454 { 1455 return (&ch->pcm_cap); 1456 } 1457 1458 int 1459 uaudio_chan_set_param_format(struct uaudio_chan *ch, uint32_t format) 1460 { 1461 ch->format = format; 1462 return (0); 1463 } 1464 1465 int 1466 uaudio_chan_start(struct uaudio_chan *ch) 1467 { 1468 ch->cur = ch->start; 1469 1470 #if (UAUDIO_NCHANBUFS != 2) 1471 #error "please update code" 1472 #endif 1473 if (ch->xfer[0]) { 1474 usb2_transfer_start(ch->xfer[0]); 1475 } 1476 if (ch->xfer[1]) { 1477 usb2_transfer_start(ch->xfer[1]); 1478 } 1479 return (0); 1480 } 1481 1482 int 1483 uaudio_chan_stop(struct uaudio_chan *ch) 1484 { 1485 #if (UAUDIO_NCHANBUFS != 2) 1486 #error "please update code" 1487 #endif 1488 usb2_transfer_stop(ch->xfer[0]); 1489 usb2_transfer_stop(ch->xfer[1]); 1490 return (0); 1491 } 1492 1493 /*========================================================================* 1494 * AC - Audio Controller - routines 1495 *========================================================================*/ 1496 1497 static void 1498 uaudio_mixer_add_ctl_sub(struct uaudio_softc *sc, struct uaudio_mixer_node *mc) 1499 { 1500 struct uaudio_mixer_node *p_mc_new = 1501 malloc(sizeof(*p_mc_new), M_USBDEV, M_WAITOK); 1502 1503 if (p_mc_new) { 1504 bcopy(mc, p_mc_new, sizeof(*p_mc_new)); 1505 p_mc_new->next = sc->sc_mixer_root; 1506 sc->sc_mixer_root = p_mc_new; 1507 sc->sc_mixer_count++; 1508 } else { 1509 DPRINTF("out of memory\n"); 1510 } 1511 } 1512 1513 static void 1514 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct uaudio_mixer_node *mc) 1515 { 1516 int32_t res; 1517 1518 if (mc->class < UAC_NCLASSES) { 1519 DPRINTF("adding %s.%d\n", 1520 uac_names[mc->class], mc->ctl); 1521 } else { 1522 DPRINTF("adding %d\n", mc->ctl); 1523 } 1524 1525 mc->delta = 0; 1526 if (mc->type == MIX_ON_OFF) { 1527 mc->minval = 0; 1528 mc->maxval = 1; 1529 } else if (mc->type == MIX_SELECTOR) { 1530 1531 } else { 1532 1533 /* determine min and max values */ 1534 1535 mc->minval = uaudio_mixer_get(sc->sc_udev, GET_MIN, mc); 1536 1537 mc->minval = uaudio_mixer_signext(mc->type, mc->minval); 1538 1539 mc->maxval = uaudio_mixer_get(sc->sc_udev, GET_MAX, mc); 1540 1541 mc->maxval = 1 + uaudio_mixer_signext(mc->type, mc->maxval); 1542 1543 mc->mul = mc->maxval - mc->minval; 1544 if (mc->mul == 0) { 1545 mc->mul = 1; 1546 } 1547 res = uaudio_mixer_get(sc->sc_udev, GET_RES, mc); 1548 if (res > 0) { 1549 mc->delta = ((res * 255) + (mc->mul / 2)) / mc->mul; 1550 } 1551 } 1552 1553 if (mc->maxval < mc->minval) { 1554 mc->maxval = mc->minval; 1555 } 1556 uaudio_mixer_add_ctl_sub(sc, mc); 1557 1558 #if USB_DEBUG 1559 if (uaudio_debug > 2) { 1560 uint8_t i; 1561 1562 for (i = 0; i < mc->nchan; i++) { 1563 DPRINTF("[mix] wValue=%04x\n", mc->wValue[0]); 1564 } 1565 DPRINTF("[mix] wIndex=%04x type=%d ctl='%d' " 1566 "min=%d max=%d\n", 1567 mc->wIndex, mc->type, mc->ctl, 1568 mc->minval, mc->maxval); 1569 } 1570 #endif 1571 } 1572 1573 static void 1574 uaudio_mixer_add_input(struct uaudio_softc *sc, 1575 const struct uaudio_terminal_node *iot, int id) 1576 { 1577 #if USB_DEBUG 1578 const struct usb2_audio_input_terminal *d = iot[id].u.it; 1579 1580 DPRINTFN(3, "bTerminalId=%d wTerminalType=0x%04x " 1581 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d " 1582 "iChannelNames=%d\n", 1583 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 1584 d->bNrChannels, UGETW(d->wChannelConfig), 1585 d->iChannelNames); 1586 #endif 1587 } 1588 1589 static void 1590 uaudio_mixer_add_output(struct uaudio_softc *sc, 1591 const struct uaudio_terminal_node *iot, int id) 1592 { 1593 #if USB_DEBUG 1594 const struct usb2_audio_output_terminal *d = iot[id].u.ot; 1595 1596 DPRINTFN(3, "bTerminalId=%d wTerminalType=0x%04x " 1597 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n", 1598 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 1599 d->bSourceId, d->iTerminal); 1600 #endif 1601 } 1602 1603 static void 1604 uaudio_mixer_add_mixer(struct uaudio_softc *sc, 1605 const struct uaudio_terminal_node *iot, int id) 1606 { 1607 struct uaudio_mixer_node mix; 1608 1609 const struct usb2_audio_mixer_unit_0 *d0 = iot[id].u.mu; 1610 const struct usb2_audio_mixer_unit_1 *d1; 1611 1612 uint32_t bno; /* bit number */ 1613 uint32_t p; /* bit number accumulator */ 1614 uint32_t mo; /* matching outputs */ 1615 uint32_t mc; /* matching channels */ 1616 uint32_t ichs; /* input channels */ 1617 uint32_t ochs; /* output channels */ 1618 uint32_t c; 1619 uint32_t chs; /* channels */ 1620 uint32_t i; 1621 uint32_t o; 1622 1623 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 1624 d0->bUnitId, d0->bNrInPins); 1625 1626 /* compute the number of input channels */ 1627 1628 ichs = 0; 1629 for (i = 0; i < d0->bNrInPins; i++) { 1630 ichs += (uaudio_mixer_get_cluster(d0->baSourceId[i], iot) 1631 .bNrChannels); 1632 } 1633 1634 d1 = (const void *)(d0->baSourceId + d0->bNrInPins); 1635 1636 /* and the number of output channels */ 1637 1638 ochs = d1->bNrChannels; 1639 1640 DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs); 1641 1642 bzero(&mix, sizeof(mix)); 1643 1644 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 1645 uaudio_mixer_determine_class(&iot[id], &mix); 1646 mix.type = MIX_SIGNED_16; 1647 1648 if (uaudio_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL) { 1649 return; 1650 } 1651 for (p = i = 0; i < d0->bNrInPins; i++) { 1652 chs = uaudio_mixer_get_cluster(d0->baSourceId[i], iot).bNrChannels; 1653 mc = 0; 1654 for (c = 0; c < chs; c++) { 1655 mo = 0; 1656 for (o = 0; o < ochs; o++) { 1657 bno = ((p + c) * ochs) + o; 1658 if (BIT_TEST(d1->bmControls, bno)) { 1659 mo++; 1660 } 1661 } 1662 if (mo == 1) { 1663 mc++; 1664 } 1665 } 1666 if ((mc == chs) && (chs <= MIX_MAX_CHAN)) { 1667 1668 /* repeat bit-scan */ 1669 1670 mc = 0; 1671 for (c = 0; c < chs; c++) { 1672 for (o = 0; o < ochs; o++) { 1673 bno = ((p + c) * ochs) + o; 1674 if (BIT_TEST(d1->bmControls, bno)) { 1675 mix.wValue[mc++] = MAKE_WORD(p + c + 1, o + 1); 1676 } 1677 } 1678 } 1679 mix.nchan = chs; 1680 uaudio_mixer_add_ctl(sc, &mix); 1681 } else { 1682 /* XXX */ 1683 } 1684 p += chs; 1685 } 1686 } 1687 1688 static void 1689 uaudio_mixer_add_selector(struct uaudio_softc *sc, 1690 const struct uaudio_terminal_node *iot, int id) 1691 { 1692 const struct usb2_audio_selector_unit *d = iot[id].u.su; 1693 struct uaudio_mixer_node mix; 1694 uint16_t i; 1695 1696 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 1697 d->bUnitId, d->bNrInPins); 1698 1699 if (d->bNrInPins == 0) { 1700 return; 1701 } 1702 bzero(&mix, sizeof(mix)); 1703 1704 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 1705 mix.wValue[0] = MAKE_WORD(0, 0); 1706 uaudio_mixer_determine_class(&iot[id], &mix); 1707 mix.nchan = 1; 1708 mix.type = MIX_SELECTOR; 1709 1710 mix.ctl = SOUND_MIXER_NRDEVICES; 1711 mix.minval = 1; 1712 mix.maxval = d->bNrInPins; 1713 1714 if (mix.maxval > MAX_SELECTOR_INPUT_PIN) { 1715 mix.maxval = MAX_SELECTOR_INPUT_PIN; 1716 } 1717 mix.mul = (mix.maxval - mix.minval); 1718 for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) { 1719 mix.slctrtype[i] = SOUND_MIXER_NRDEVICES; 1720 } 1721 1722 for (i = 0; i < mix.maxval; i++) { 1723 mix.slctrtype[i] = uaudio_mixer_feature_name 1724 (&iot[d->baSourceId[i]], &mix); 1725 } 1726 1727 mix.class = 0; /* not used */ 1728 1729 uaudio_mixer_add_ctl(sc, &mix); 1730 } 1731 1732 static uint32_t 1733 uaudio_mixer_feature_get_bmaControls(const struct usb2_audio_feature_unit *d, 1734 uint8_t index) 1735 { 1736 uint32_t temp = 0; 1737 uint32_t offset = (index * d->bControlSize); 1738 1739 if (d->bControlSize > 0) { 1740 temp |= d->bmaControls[offset]; 1741 if (d->bControlSize > 1) { 1742 temp |= d->bmaControls[offset + 1] << 8; 1743 if (d->bControlSize > 2) { 1744 temp |= d->bmaControls[offset + 2] << 16; 1745 if (d->bControlSize > 3) { 1746 temp |= d->bmaControls[offset + 3] << 24; 1747 } 1748 } 1749 } 1750 } 1751 return (temp); 1752 } 1753 1754 static void 1755 uaudio_mixer_add_feature(struct uaudio_softc *sc, 1756 const struct uaudio_terminal_node *iot, int id) 1757 { 1758 const struct usb2_audio_feature_unit *d = iot[id].u.fu; 1759 struct uaudio_mixer_node mix; 1760 uint32_t fumask; 1761 uint32_t mmask; 1762 uint32_t cmask; 1763 uint16_t mixernumber; 1764 uint8_t nchan; 1765 uint8_t chan; 1766 uint8_t ctl; 1767 uint8_t i; 1768 1769 if (d->bControlSize == 0) { 1770 return; 1771 } 1772 bzero(&mix, sizeof(mix)); 1773 1774 nchan = (d->bLength - 7) / d->bControlSize; 1775 mmask = uaudio_mixer_feature_get_bmaControls(d, 0); 1776 cmask = 0; 1777 1778 if (nchan == 0) { 1779 return; 1780 } 1781 /* figure out what we can control */ 1782 1783 for (chan = 1; chan < nchan; chan++) { 1784 DPRINTFN(10, "chan=%d mask=%x\n", 1785 chan, uaudio_mixer_feature_get_bmaControls(d, chan)); 1786 1787 cmask |= uaudio_mixer_feature_get_bmaControls(d, chan); 1788 } 1789 1790 if (nchan > MIX_MAX_CHAN) { 1791 nchan = MIX_MAX_CHAN; 1792 } 1793 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no); 1794 1795 for (ctl = 1; ctl <= LOUDNESS_CONTROL; ctl++) { 1796 1797 fumask = FU_MASK(ctl); 1798 1799 DPRINTFN(5, "ctl=%d fumask=0x%04x\n", 1800 ctl, fumask); 1801 1802 if (mmask & fumask) { 1803 mix.nchan = 1; 1804 mix.wValue[0] = MAKE_WORD(ctl, 0); 1805 } else if (cmask & fumask) { 1806 mix.nchan = nchan - 1; 1807 for (i = 1; i < nchan; i++) { 1808 if (uaudio_mixer_feature_get_bmaControls(d, i) & fumask) 1809 mix.wValue[i - 1] = MAKE_WORD(ctl, i); 1810 else 1811 mix.wValue[i - 1] = -1; 1812 } 1813 } else { 1814 continue; 1815 } 1816 1817 mixernumber = uaudio_mixer_feature_name(&iot[id], &mix); 1818 1819 switch (ctl) { 1820 case MUTE_CONTROL: 1821 mix.type = MIX_ON_OFF; 1822 mix.ctl = SOUND_MIXER_NRDEVICES; 1823 break; 1824 1825 case VOLUME_CONTROL: 1826 mix.type = MIX_SIGNED_16; 1827 mix.ctl = mixernumber; 1828 break; 1829 1830 case BASS_CONTROL: 1831 mix.type = MIX_SIGNED_8; 1832 mix.ctl = SOUND_MIXER_BASS; 1833 break; 1834 1835 case MID_CONTROL: 1836 mix.type = MIX_SIGNED_8; 1837 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 1838 break; 1839 1840 case TREBLE_CONTROL: 1841 mix.type = MIX_SIGNED_8; 1842 mix.ctl = SOUND_MIXER_TREBLE; 1843 break; 1844 1845 case GRAPHIC_EQUALIZER_CONTROL: 1846 continue; /* XXX don't add anything */ 1847 break; 1848 1849 case AGC_CONTROL: 1850 mix.type = MIX_ON_OFF; 1851 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 1852 break; 1853 1854 case DELAY_CONTROL: 1855 mix.type = MIX_UNSIGNED_16; 1856 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 1857 break; 1858 1859 case BASS_BOOST_CONTROL: 1860 mix.type = MIX_ON_OFF; 1861 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */ 1862 break; 1863 1864 case LOUDNESS_CONTROL: 1865 mix.type = MIX_ON_OFF; 1866 mix.ctl = SOUND_MIXER_LOUD; /* Is this correct ? */ 1867 break; 1868 1869 default: 1870 mix.type = MIX_UNKNOWN; 1871 break; 1872 } 1873 1874 if (mix.type != MIX_UNKNOWN) { 1875 uaudio_mixer_add_ctl(sc, &mix); 1876 } 1877 } 1878 } 1879 1880 static void 1881 uaudio_mixer_add_processing_updown(struct uaudio_softc *sc, 1882 const struct uaudio_terminal_node *iot, int id) 1883 { 1884 const struct usb2_audio_processing_unit_0 *d0 = iot[id].u.pu; 1885 const struct usb2_audio_processing_unit_1 *d1 = 1886 (const void *)(d0->baSourceId + d0->bNrInPins); 1887 const struct usb2_audio_processing_unit_updown *ud = 1888 (const void *)(d1->bmControls + d1->bControlSize); 1889 struct uaudio_mixer_node mix; 1890 uint8_t i; 1891 1892 if (uaudio_mixer_verify_desc(d0, sizeof(*ud)) == NULL) { 1893 return; 1894 } 1895 if (uaudio_mixer_verify_desc(d0, sizeof(*ud) + (2 * ud->bNrModes)) 1896 == NULL) { 1897 return; 1898 } 1899 DPRINTFN(3, "bUnitId=%d bNrModes=%d\n", 1900 d0->bUnitId, ud->bNrModes); 1901 1902 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) { 1903 DPRINTF("no mode select\n"); 1904 return; 1905 } 1906 bzero(&mix, sizeof(mix)); 1907 1908 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 1909 mix.nchan = 1; 1910 mix.wValue[0] = MAKE_WORD(UD_MODE_SELECT_CONTROL, 0); 1911 uaudio_mixer_determine_class(&iot[id], &mix); 1912 mix.type = MIX_ON_OFF; /* XXX */ 1913 1914 for (i = 0; i < ud->bNrModes; i++) { 1915 DPRINTFN(3, "i=%d bm=0x%x\n", i, UGETW(ud->waModes[i])); 1916 /* XXX */ 1917 } 1918 1919 uaudio_mixer_add_ctl(sc, &mix); 1920 } 1921 1922 static void 1923 uaudio_mixer_add_processing(struct uaudio_softc *sc, 1924 const struct uaudio_terminal_node *iot, int id) 1925 { 1926 const struct usb2_audio_processing_unit_0 *d0 = iot[id].u.pu; 1927 const struct usb2_audio_processing_unit_1 *d1 = 1928 (const void *)(d0->baSourceId + d0->bNrInPins); 1929 struct uaudio_mixer_node mix; 1930 uint16_t ptype; 1931 1932 bzero(&mix, sizeof(mix)); 1933 1934 ptype = UGETW(d0->wProcessType); 1935 1936 DPRINTFN(3, "wProcessType=%d bUnitId=%d " 1937 "bNrInPins=%d\n", ptype, d0->bUnitId, d0->bNrInPins); 1938 1939 if (d1->bControlSize == 0) { 1940 return; 1941 } 1942 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) { 1943 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 1944 mix.nchan = 1; 1945 mix.wValue[0] = MAKE_WORD(XX_ENABLE_CONTROL, 0); 1946 uaudio_mixer_determine_class(&iot[id], &mix); 1947 mix.type = MIX_ON_OFF; 1948 uaudio_mixer_add_ctl(sc, &mix); 1949 } 1950 switch (ptype) { 1951 case UPDOWNMIX_PROCESS: 1952 uaudio_mixer_add_processing_updown(sc, iot, id); 1953 break; 1954 1955 case DOLBY_PROLOGIC_PROCESS: 1956 case P3D_STEREO_EXTENDER_PROCESS: 1957 case REVERBATION_PROCESS: 1958 case CHORUS_PROCESS: 1959 case DYN_RANGE_COMP_PROCESS: 1960 default: 1961 DPRINTF("unit %d, type=%d is not implemented\n", 1962 d0->bUnitId, ptype); 1963 break; 1964 } 1965 } 1966 1967 static void 1968 uaudio_mixer_add_extension(struct uaudio_softc *sc, 1969 const struct uaudio_terminal_node *iot, int id) 1970 { 1971 const struct usb2_audio_extension_unit_0 *d0 = iot[id].u.eu; 1972 const struct usb2_audio_extension_unit_1 *d1 = 1973 (const void *)(d0->baSourceId + d0->bNrInPins); 1974 struct uaudio_mixer_node mix; 1975 1976 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n", 1977 d0->bUnitId, d0->bNrInPins); 1978 1979 if (sc->sc_uq_au_no_xu) { 1980 return; 1981 } 1982 if (d1->bControlSize == 0) { 1983 return; 1984 } 1985 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) { 1986 1987 bzero(&mix, sizeof(mix)); 1988 1989 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no); 1990 mix.nchan = 1; 1991 mix.wValue[0] = MAKE_WORD(UA_EXT_ENABLE, 0); 1992 uaudio_mixer_determine_class(&iot[id], &mix); 1993 mix.type = MIX_ON_OFF; 1994 1995 uaudio_mixer_add_ctl(sc, &mix); 1996 } 1997 } 1998 1999 static const void * 2000 uaudio_mixer_verify_desc(const void *arg, uint32_t len) 2001 { 2002 const struct usb2_audio_mixer_unit_1 *d1; 2003 const struct usb2_audio_extension_unit_1 *e1; 2004 const struct usb2_audio_processing_unit_1 *u1; 2005 2006 union { 2007 const struct usb2_descriptor *desc; 2008 const struct usb2_audio_input_terminal *it; 2009 const struct usb2_audio_output_terminal *ot; 2010 const struct usb2_audio_mixer_unit_0 *mu; 2011 const struct usb2_audio_selector_unit *su; 2012 const struct usb2_audio_feature_unit *fu; 2013 const struct usb2_audio_processing_unit_0 *pu; 2014 const struct usb2_audio_extension_unit_0 *eu; 2015 } u; 2016 2017 u.desc = arg; 2018 2019 if (u.desc == NULL) { 2020 goto error; 2021 } 2022 if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) { 2023 goto error; 2024 } 2025 switch (u.desc->bDescriptorSubtype) { 2026 case UDESCSUB_AC_INPUT: 2027 len += sizeof(*u.it); 2028 break; 2029 2030 case UDESCSUB_AC_OUTPUT: 2031 len += sizeof(*u.ot); 2032 break; 2033 2034 case UDESCSUB_AC_MIXER: 2035 len += sizeof(*u.mu); 2036 2037 if (u.desc->bLength < len) { 2038 goto error; 2039 } 2040 len += u.mu->bNrInPins; 2041 2042 if (u.desc->bLength < len) { 2043 goto error; 2044 } 2045 d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins); 2046 2047 len += sizeof(*d1); 2048 break; 2049 2050 case UDESCSUB_AC_SELECTOR: 2051 len += sizeof(*u.su); 2052 2053 if (u.desc->bLength < len) { 2054 goto error; 2055 } 2056 len += u.su->bNrInPins; 2057 break; 2058 2059 case UDESCSUB_AC_FEATURE: 2060 len += (sizeof(*u.fu) + 1); 2061 break; 2062 2063 case UDESCSUB_AC_PROCESSING: 2064 len += sizeof(*u.pu); 2065 2066 if (u.desc->bLength < len) { 2067 goto error; 2068 } 2069 len += u.pu->bNrInPins; 2070 2071 if (u.desc->bLength < len) { 2072 goto error; 2073 } 2074 u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins); 2075 2076 len += sizeof(*u1); 2077 2078 if (u.desc->bLength < len) { 2079 goto error; 2080 } 2081 len += u1->bControlSize; 2082 2083 break; 2084 2085 case UDESCSUB_AC_EXTENSION: 2086 len += sizeof(*u.eu); 2087 2088 if (u.desc->bLength < len) { 2089 goto error; 2090 } 2091 len += u.eu->bNrInPins; 2092 2093 if (u.desc->bLength < len) { 2094 goto error; 2095 } 2096 e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins); 2097 2098 len += sizeof(*e1); 2099 2100 if (u.desc->bLength < len) { 2101 goto error; 2102 } 2103 len += e1->bControlSize; 2104 break; 2105 2106 default: 2107 goto error; 2108 } 2109 2110 if (u.desc->bLength < len) { 2111 goto error; 2112 } 2113 return (u.desc); 2114 2115 error: 2116 if (u.desc) { 2117 DPRINTF("invalid descriptor, type=%d, " 2118 "sub_type=%d, len=%d of %d bytes\n", 2119 u.desc->bDescriptorType, 2120 u.desc->bDescriptorSubtype, 2121 u.desc->bLength, len); 2122 } 2123 return (NULL); 2124 } 2125 2126 #if USB_DEBUG 2127 static void 2128 uaudio_mixer_dump_cluster(uint8_t id, const struct uaudio_terminal_node *iot) 2129 { 2130 static const char *channel_names[16] = { 2131 "LEFT", "RIGHT", "CENTER", "LFE", 2132 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER", 2133 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP", 2134 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15", 2135 }; 2136 uint16_t cc; 2137 uint8_t i; 2138 const struct usb2_audio_cluster cl = uaudio_mixer_get_cluster(id, iot); 2139 2140 cc = UGETW(cl.wChannelConfig); 2141 2142 DPRINTF("cluster: bNrChannels=%u iChannelNames=%u wChannelConfig=" 2143 "0x%04x:\n", cl.iChannelNames, cl.bNrChannels, cc); 2144 2145 for (i = 0; cc; i++) { 2146 if (cc & 1) { 2147 DPRINTF(" - %s\n", channel_names[i]); 2148 } 2149 cc >>= 1; 2150 } 2151 } 2152 2153 #endif 2154 2155 static struct usb2_audio_cluster 2156 uaudio_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot) 2157 { 2158 struct usb2_audio_cluster r; 2159 const struct usb2_descriptor *dp; 2160 uint8_t i; 2161 2162 for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) { /* avoid infinite loops */ 2163 dp = iot[id].u.desc; 2164 if (dp == NULL) { 2165 goto error; 2166 } 2167 switch (dp->bDescriptorSubtype) { 2168 case UDESCSUB_AC_INPUT: 2169 r.bNrChannels = iot[id].u.it->bNrChannels; 2170 r.wChannelConfig[0] = iot[id].u.it->wChannelConfig[0]; 2171 r.wChannelConfig[1] = iot[id].u.it->wChannelConfig[1]; 2172 r.iChannelNames = iot[id].u.it->iChannelNames; 2173 goto done; 2174 2175 case UDESCSUB_AC_OUTPUT: 2176 id = iot[id].u.ot->bSourceId; 2177 break; 2178 2179 case UDESCSUB_AC_MIXER: 2180 r = *(const struct usb2_audio_cluster *) 2181 &iot[id].u.mu->baSourceId[iot[id].u.mu-> 2182 bNrInPins]; 2183 goto done; 2184 2185 case UDESCSUB_AC_SELECTOR: 2186 if (iot[id].u.su->bNrInPins > 0) { 2187 /* XXX This is not really right */ 2188 id = iot[id].u.su->baSourceId[0]; 2189 } 2190 break; 2191 2192 case UDESCSUB_AC_FEATURE: 2193 id = iot[id].u.fu->bSourceId; 2194 break; 2195 2196 case UDESCSUB_AC_PROCESSING: 2197 r = *((const struct usb2_audio_cluster *) 2198 &iot[id].u.pu->baSourceId[iot[id].u.pu-> 2199 bNrInPins]); 2200 goto done; 2201 2202 case UDESCSUB_AC_EXTENSION: 2203 r = *((const struct usb2_audio_cluster *) 2204 &iot[id].u.eu->baSourceId[iot[id].u.eu-> 2205 bNrInPins]); 2206 goto done; 2207 2208 default: 2209 goto error; 2210 } 2211 } 2212 error: 2213 DPRINTF("bad data\n"); 2214 bzero(&r, sizeof(r)); 2215 done: 2216 return (r); 2217 } 2218 2219 #if USB_DEBUG 2220 2221 struct uaudio_tt_to_string { 2222 uint16_t terminal_type; 2223 const char *desc; 2224 }; 2225 2226 static const struct uaudio_tt_to_string uaudio_tt_to_string[] = { 2227 2228 /* USB terminal types */ 2229 {UAT_UNDEFINED, "UAT_UNDEFINED"}, 2230 {UAT_STREAM, "UAT_STREAM"}, 2231 {UAT_VENDOR, "UAT_VENDOR"}, 2232 2233 /* input terminal types */ 2234 {UATI_UNDEFINED, "UATI_UNDEFINED"}, 2235 {UATI_MICROPHONE, "UATI_MICROPHONE"}, 2236 {UATI_DESKMICROPHONE, "UATI_DESKMICROPHONE"}, 2237 {UATI_PERSONALMICROPHONE, "UATI_PERSONALMICROPHONE"}, 2238 {UATI_OMNIMICROPHONE, "UATI_OMNIMICROPHONE"}, 2239 {UATI_MICROPHONEARRAY, "UATI_MICROPHONEARRAY"}, 2240 {UATI_PROCMICROPHONEARR, "UATI_PROCMICROPHONEARR"}, 2241 2242 /* output terminal types */ 2243 {UATO_UNDEFINED, "UATO_UNDEFINED"}, 2244 {UATO_SPEAKER, "UATO_SPEAKER"}, 2245 {UATO_HEADPHONES, "UATO_HEADPHONES"}, 2246 {UATO_DISPLAYAUDIO, "UATO_DISPLAYAUDIO"}, 2247 {UATO_DESKTOPSPEAKER, "UATO_DESKTOPSPEAKER"}, 2248 {UATO_ROOMSPEAKER, "UATO_ROOMSPEAKER"}, 2249 {UATO_COMMSPEAKER, "UATO_COMMSPEAKER"}, 2250 {UATO_SUBWOOFER, "UATO_SUBWOOFER"}, 2251 2252 /* bidir terminal types */ 2253 {UATB_UNDEFINED, "UATB_UNDEFINED"}, 2254 {UATB_HANDSET, "UATB_HANDSET"}, 2255 {UATB_HEADSET, "UATB_HEADSET"}, 2256 {UATB_SPEAKERPHONE, "UATB_SPEAKERPHONE"}, 2257 {UATB_SPEAKERPHONEESUP, "UATB_SPEAKERPHONEESUP"}, 2258 {UATB_SPEAKERPHONEECANC, "UATB_SPEAKERPHONEECANC"}, 2259 2260 /* telephony terminal types */ 2261 {UATT_UNDEFINED, "UATT_UNDEFINED"}, 2262 {UATT_PHONELINE, "UATT_PHONELINE"}, 2263 {UATT_TELEPHONE, "UATT_TELEPHONE"}, 2264 {UATT_DOWNLINEPHONE, "UATT_DOWNLINEPHONE"}, 2265 2266 /* external terminal types */ 2267 {UATE_UNDEFINED, "UATE_UNDEFINED"}, 2268 {UATE_ANALOGCONN, "UATE_ANALOGCONN"}, 2269 {UATE_LINECONN, "UATE_LINECONN"}, 2270 {UATE_LEGACYCONN, "UATE_LEGACYCONN"}, 2271 {UATE_DIGITALAUIFC, "UATE_DIGITALAUIFC"}, 2272 {UATE_SPDIF, "UATE_SPDIF"}, 2273 {UATE_1394DA, "UATE_1394DA"}, 2274 {UATE_1394DV, "UATE_1394DV"}, 2275 2276 /* embedded function terminal types */ 2277 {UATF_UNDEFINED, "UATF_UNDEFINED"}, 2278 {UATF_CALIBNOISE, "UATF_CALIBNOISE"}, 2279 {UATF_EQUNOISE, "UATF_EQUNOISE"}, 2280 {UATF_CDPLAYER, "UATF_CDPLAYER"}, 2281 {UATF_DAT, "UATF_DAT"}, 2282 {UATF_DCC, "UATF_DCC"}, 2283 {UATF_MINIDISK, "UATF_MINIDISK"}, 2284 {UATF_ANALOGTAPE, "UATF_ANALOGTAPE"}, 2285 {UATF_PHONOGRAPH, "UATF_PHONOGRAPH"}, 2286 {UATF_VCRAUDIO, "UATF_VCRAUDIO"}, 2287 {UATF_VIDEODISCAUDIO, "UATF_VIDEODISCAUDIO"}, 2288 {UATF_DVDAUDIO, "UATF_DVDAUDIO"}, 2289 {UATF_TVTUNERAUDIO, "UATF_TVTUNERAUDIO"}, 2290 {UATF_SATELLITE, "UATF_SATELLITE"}, 2291 {UATF_CABLETUNER, "UATF_CABLETUNER"}, 2292 {UATF_DSS, "UATF_DSS"}, 2293 {UATF_RADIORECV, "UATF_RADIORECV"}, 2294 {UATF_RADIOXMIT, "UATF_RADIOXMIT"}, 2295 {UATF_MULTITRACK, "UATF_MULTITRACK"}, 2296 {UATF_SYNTHESIZER, "UATF_SYNTHESIZER"}, 2297 2298 /* unknown */ 2299 {0x0000, "UNKNOWN"}, 2300 }; 2301 2302 static const char * 2303 uaudio_mixer_get_terminal_name(uint16_t terminal_type) 2304 { 2305 const struct uaudio_tt_to_string *uat = uaudio_tt_to_string; 2306 2307 while (uat->terminal_type) { 2308 if (uat->terminal_type == terminal_type) { 2309 break; 2310 } 2311 uat++; 2312 } 2313 if (uat->terminal_type == 0) { 2314 DPRINTF("unknown terminal type (0x%04x)", terminal_type); 2315 } 2316 return (uat->desc); 2317 } 2318 2319 #endif 2320 2321 static uint16_t 2322 uaudio_mixer_determine_class(const struct uaudio_terminal_node *iot, 2323 struct uaudio_mixer_node *mix) 2324 { 2325 uint16_t terminal_type = 0x0000; 2326 const struct uaudio_terminal_node *input[2]; 2327 const struct uaudio_terminal_node *output[2]; 2328 2329 input[0] = uaudio_mixer_get_input(iot, 0); 2330 input[1] = uaudio_mixer_get_input(iot, 1); 2331 2332 output[0] = uaudio_mixer_get_output(iot, 0); 2333 output[1] = uaudio_mixer_get_output(iot, 1); 2334 2335 /* 2336 * check if there is only 2337 * one output terminal: 2338 */ 2339 if (output[0] && (!output[1])) { 2340 terminal_type = UGETW(output[0]->u.ot->wTerminalType); 2341 } 2342 /* 2343 * If the only output terminal is USB, 2344 * the class is UAC_RECORD. 2345 */ 2346 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) { 2347 2348 mix->class = UAC_RECORD; 2349 if (input[0] && (!input[1])) { 2350 terminal_type = UGETW(input[0]->u.it->wTerminalType); 2351 } else { 2352 terminal_type = 0; 2353 } 2354 goto done; 2355 } 2356 /* 2357 * if the unit is connected to just 2358 * one input terminal, the 2359 * class is UAC_INPUT: 2360 */ 2361 if (input[0] && (!input[1])) { 2362 mix->class = UAC_INPUT; 2363 terminal_type = UGETW(input[0]->u.it->wTerminalType); 2364 goto done; 2365 } 2366 /* 2367 * Otherwise, the class is UAC_OUTPUT. 2368 */ 2369 mix->class = UAC_OUTPUT; 2370 done: 2371 return (terminal_type); 2372 } 2373 2374 struct uaudio_tt_to_feature { 2375 uint16_t terminal_type; 2376 uint16_t feature; 2377 }; 2378 2379 static const struct uaudio_tt_to_feature uaudio_tt_to_feature[] = { 2380 2381 {UAT_STREAM, SOUND_MIXER_PCM}, 2382 2383 {UATI_MICROPHONE, SOUND_MIXER_MIC}, 2384 {UATI_DESKMICROPHONE, SOUND_MIXER_MIC}, 2385 {UATI_PERSONALMICROPHONE, SOUND_MIXER_MIC}, 2386 {UATI_OMNIMICROPHONE, SOUND_MIXER_MIC}, 2387 {UATI_MICROPHONEARRAY, SOUND_MIXER_MIC}, 2388 {UATI_PROCMICROPHONEARR, SOUND_MIXER_MIC}, 2389 2390 {UATO_SPEAKER, SOUND_MIXER_SPEAKER}, 2391 {UATO_DESKTOPSPEAKER, SOUND_MIXER_SPEAKER}, 2392 {UATO_ROOMSPEAKER, SOUND_MIXER_SPEAKER}, 2393 {UATO_COMMSPEAKER, SOUND_MIXER_SPEAKER}, 2394 2395 {UATE_ANALOGCONN, SOUND_MIXER_LINE}, 2396 {UATE_LINECONN, SOUND_MIXER_LINE}, 2397 {UATE_LEGACYCONN, SOUND_MIXER_LINE}, 2398 2399 {UATE_DIGITALAUIFC, SOUND_MIXER_ALTPCM}, 2400 {UATE_SPDIF, SOUND_MIXER_ALTPCM}, 2401 {UATE_1394DA, SOUND_MIXER_ALTPCM}, 2402 {UATE_1394DV, SOUND_MIXER_ALTPCM}, 2403 2404 {UATF_CDPLAYER, SOUND_MIXER_CD}, 2405 2406 {UATF_SYNTHESIZER, SOUND_MIXER_SYNTH}, 2407 2408 {UATF_VIDEODISCAUDIO, SOUND_MIXER_VIDEO}, 2409 {UATF_DVDAUDIO, SOUND_MIXER_VIDEO}, 2410 {UATF_TVTUNERAUDIO, SOUND_MIXER_VIDEO}, 2411 2412 /* telephony terminal types */ 2413 {UATT_UNDEFINED, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 2414 {UATT_PHONELINE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 2415 {UATT_TELEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 2416 {UATT_DOWNLINEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */ 2417 2418 {UATF_RADIORECV, SOUND_MIXER_RADIO}, 2419 {UATF_RADIOXMIT, SOUND_MIXER_RADIO}, 2420 2421 {UAT_UNDEFINED, SOUND_MIXER_VOLUME}, 2422 {UAT_VENDOR, SOUND_MIXER_VOLUME}, 2423 {UATI_UNDEFINED, SOUND_MIXER_VOLUME}, 2424 2425 /* output terminal types */ 2426 {UATO_UNDEFINED, SOUND_MIXER_VOLUME}, 2427 {UATO_DISPLAYAUDIO, SOUND_MIXER_VOLUME}, 2428 {UATO_SUBWOOFER, SOUND_MIXER_VOLUME}, 2429 {UATO_HEADPHONES, SOUND_MIXER_VOLUME}, 2430 2431 /* bidir terminal types */ 2432 {UATB_UNDEFINED, SOUND_MIXER_VOLUME}, 2433 {UATB_HANDSET, SOUND_MIXER_VOLUME}, 2434 {UATB_HEADSET, SOUND_MIXER_VOLUME}, 2435 {UATB_SPEAKERPHONE, SOUND_MIXER_VOLUME}, 2436 {UATB_SPEAKERPHONEESUP, SOUND_MIXER_VOLUME}, 2437 {UATB_SPEAKERPHONEECANC, SOUND_MIXER_VOLUME}, 2438 2439 /* external terminal types */ 2440 {UATE_UNDEFINED, SOUND_MIXER_VOLUME}, 2441 2442 /* embedded function terminal types */ 2443 {UATF_UNDEFINED, SOUND_MIXER_VOLUME}, 2444 {UATF_CALIBNOISE, SOUND_MIXER_VOLUME}, 2445 {UATF_EQUNOISE, SOUND_MIXER_VOLUME}, 2446 {UATF_DAT, SOUND_MIXER_VOLUME}, 2447 {UATF_DCC, SOUND_MIXER_VOLUME}, 2448 {UATF_MINIDISK, SOUND_MIXER_VOLUME}, 2449 {UATF_ANALOGTAPE, SOUND_MIXER_VOLUME}, 2450 {UATF_PHONOGRAPH, SOUND_MIXER_VOLUME}, 2451 {UATF_VCRAUDIO, SOUND_MIXER_VOLUME}, 2452 {UATF_SATELLITE, SOUND_MIXER_VOLUME}, 2453 {UATF_CABLETUNER, SOUND_MIXER_VOLUME}, 2454 {UATF_DSS, SOUND_MIXER_VOLUME}, 2455 {UATF_MULTITRACK, SOUND_MIXER_VOLUME}, 2456 {0xffff, SOUND_MIXER_VOLUME}, 2457 2458 /* default */ 2459 {0x0000, SOUND_MIXER_VOLUME}, 2460 }; 2461 2462 static uint16_t 2463 uaudio_mixer_feature_name(const struct uaudio_terminal_node *iot, 2464 struct uaudio_mixer_node *mix) 2465 { 2466 const struct uaudio_tt_to_feature *uat = uaudio_tt_to_feature; 2467 uint16_t terminal_type = uaudio_mixer_determine_class(iot, mix); 2468 2469 if ((mix->class == UAC_RECORD) && (terminal_type == 0)) { 2470 return (SOUND_MIXER_IMIX); 2471 } 2472 while (uat->terminal_type) { 2473 if (uat->terminal_type == terminal_type) { 2474 break; 2475 } 2476 uat++; 2477 } 2478 2479 DPRINTF("terminal_type=%s (0x%04x) -> %d\n", 2480 uaudio_mixer_get_terminal_name(terminal_type), 2481 terminal_type, uat->feature); 2482 2483 return (uat->feature); 2484 } 2485 2486 const static struct uaudio_terminal_node * 2487 uaudio_mixer_get_input(const struct uaudio_terminal_node *iot, uint8_t index) 2488 { 2489 struct uaudio_terminal_node *root = iot->root; 2490 uint8_t n; 2491 2492 n = iot->usr.id_max; 2493 do { 2494 if (iot->usr.bit_input[n / 8] & (1 << (n % 8))) { 2495 if (!index--) { 2496 return (root + n); 2497 } 2498 } 2499 } while (n--); 2500 2501 return (NULL); 2502 } 2503 2504 const static struct uaudio_terminal_node * 2505 uaudio_mixer_get_output(const struct uaudio_terminal_node *iot, uint8_t index) 2506 { 2507 struct uaudio_terminal_node *root = iot->root; 2508 uint8_t n; 2509 2510 n = iot->usr.id_max; 2511 do { 2512 if (iot->usr.bit_output[n / 8] & (1 << (n % 8))) { 2513 if (!index--) { 2514 return (root + n); 2515 } 2516 } 2517 } while (n--); 2518 2519 return (NULL); 2520 } 2521 2522 static void 2523 uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *root, 2524 const uint8_t *p_id, uint8_t n_id, 2525 struct uaudio_search_result *info) 2526 { 2527 struct uaudio_terminal_node *iot; 2528 uint8_t n; 2529 uint8_t i; 2530 2531 if (info->recurse_level >= UAUDIO_RECURSE_LIMIT) { 2532 return; 2533 } 2534 info->recurse_level++; 2535 2536 for (n = 0; n < n_id; n++) { 2537 2538 i = p_id[n]; 2539 2540 if (info->bit_visited[i / 8] & (1 << (i % 8))) { 2541 /* don't go into a circle */ 2542 DPRINTF("avoided going into a circle at id=%d!\n", i); 2543 continue; 2544 } else { 2545 info->bit_visited[i / 8] |= (1 << (i % 8)); 2546 } 2547 2548 iot = (root + i); 2549 2550 if (iot->u.desc == NULL) { 2551 continue; 2552 } 2553 switch (iot->u.desc->bDescriptorSubtype) { 2554 case UDESCSUB_AC_INPUT: 2555 info->bit_input[i / 8] |= (1 << (i % 8)); 2556 break; 2557 2558 case UDESCSUB_AC_FEATURE: 2559 uaudio_mixer_find_inputs_sub 2560 (root, &iot->u.fu->bSourceId, 1, info); 2561 break; 2562 2563 case UDESCSUB_AC_OUTPUT: 2564 uaudio_mixer_find_inputs_sub 2565 (root, &iot->u.ot->bSourceId, 1, info); 2566 break; 2567 2568 case UDESCSUB_AC_MIXER: 2569 uaudio_mixer_find_inputs_sub 2570 (root, iot->u.mu->baSourceId, 2571 iot->u.mu->bNrInPins, info); 2572 break; 2573 2574 case UDESCSUB_AC_SELECTOR: 2575 uaudio_mixer_find_inputs_sub 2576 (root, iot->u.su->baSourceId, 2577 iot->u.su->bNrInPins, info); 2578 break; 2579 2580 case UDESCSUB_AC_PROCESSING: 2581 uaudio_mixer_find_inputs_sub 2582 (root, iot->u.pu->baSourceId, 2583 iot->u.pu->bNrInPins, info); 2584 break; 2585 2586 case UDESCSUB_AC_EXTENSION: 2587 uaudio_mixer_find_inputs_sub 2588 (root, iot->u.eu->baSourceId, 2589 iot->u.eu->bNrInPins, info); 2590 break; 2591 2592 case UDESCSUB_AC_HEADER: 2593 default: 2594 break; 2595 } 2596 } 2597 info->recurse_level--; 2598 } 2599 2600 static void 2601 uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *root, uint8_t id, 2602 uint8_t n_id, struct uaudio_search_result *info) 2603 { 2604 struct uaudio_terminal_node *iot = (root + id); 2605 uint8_t j; 2606 2607 j = n_id; 2608 do { 2609 if ((j != id) && ((root + j)->u.desc) && 2610 ((root + j)->u.desc->bDescriptorSubtype == UDESCSUB_AC_OUTPUT)) { 2611 2612 /* 2613 * "j" (output) <--- virtual wire <--- "id" (input) 2614 * 2615 * if "j" has "id" on the input, then "id" have "j" on 2616 * the output, because they are connected: 2617 */ 2618 if ((root + j)->usr.bit_input[id / 8] & (1 << (id % 8))) { 2619 iot->usr.bit_output[j / 8] |= (1 << (j % 8)); 2620 } 2621 } 2622 } while (j--); 2623 } 2624 2625 static void 2626 uaudio_mixer_fill_info(struct uaudio_softc *sc, struct usb2_device *udev, 2627 void *desc) 2628 { 2629 const struct usb2_audio_control_descriptor *acdp; 2630 struct usb2_config_descriptor *cd = usb2_get_config_descriptor(udev); 2631 const struct usb2_descriptor *dp; 2632 const struct usb2_audio_unit *au; 2633 struct uaudio_terminal_node *iot = NULL; 2634 uint16_t wTotalLen; 2635 uint8_t ID_max = 0; /* inclusive */ 2636 uint8_t i; 2637 2638 desc = usb2_desc_foreach(cd, desc); 2639 2640 if (desc == NULL) { 2641 DPRINTF("no Audio Control header\n"); 2642 goto done; 2643 } 2644 acdp = desc; 2645 2646 if ((acdp->bLength < sizeof(*acdp)) || 2647 (acdp->bDescriptorType != UDESC_CS_INTERFACE) || 2648 (acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)) { 2649 DPRINTF("invalid Audio Control header\n"); 2650 goto done; 2651 } 2652 /* "wTotalLen" is allowed to be corrupt */ 2653 wTotalLen = UGETW(acdp->wTotalLength) - acdp->bLength; 2654 2655 /* get USB audio revision */ 2656 sc->sc_audio_rev = UGETW(acdp->bcdADC); 2657 2658 DPRINTFN(3, "found AC header, vers=%03x, len=%d\n", 2659 sc->sc_audio_rev, wTotalLen); 2660 2661 if (sc->sc_audio_rev != UAUDIO_VERSION) { 2662 2663 if (sc->sc_uq_bad_adc) { 2664 2665 } else { 2666 DPRINTF("invalid audio version\n"); 2667 goto done; 2668 } 2669 } 2670 iot = malloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP, 2671 M_WAITOK | M_ZERO); 2672 2673 if (iot == NULL) { 2674 DPRINTF("no memory!\n"); 2675 goto done; 2676 } 2677 while ((desc = usb2_desc_foreach(cd, desc))) { 2678 2679 dp = desc; 2680 2681 if (dp->bLength > wTotalLen) { 2682 break; 2683 } else { 2684 wTotalLen -= dp->bLength; 2685 } 2686 2687 au = uaudio_mixer_verify_desc(dp, 0); 2688 2689 if (au) { 2690 iot[au->bUnitId].u.desc = (const void *)au; 2691 if (au->bUnitId > ID_max) { 2692 ID_max = au->bUnitId; 2693 } 2694 } 2695 } 2696 2697 DPRINTF("Maximum ID=%d\n", ID_max); 2698 2699 /* 2700 * determine sourcing inputs for 2701 * all nodes in the tree: 2702 */ 2703 i = ID_max; 2704 do { 2705 uaudio_mixer_find_inputs_sub(iot, &i, 1, &((iot + i)->usr)); 2706 } while (i--); 2707 2708 /* 2709 * determine outputs for 2710 * all nodes in the tree: 2711 */ 2712 i = ID_max; 2713 do { 2714 uaudio_mixer_find_outputs_sub(iot, i, ID_max, &((iot + i)->usr)); 2715 } while (i--); 2716 2717 /* set "id_max" and "root" */ 2718 2719 i = ID_max; 2720 do { 2721 (iot + i)->usr.id_max = ID_max; 2722 (iot + i)->root = iot; 2723 } while (i--); 2724 2725 #if USB_DEBUG 2726 i = ID_max; 2727 do { 2728 uint8_t j; 2729 2730 if (iot[i].u.desc == NULL) { 2731 continue; 2732 } 2733 DPRINTF("id %d:\n", i); 2734 2735 switch (iot[i].u.desc->bDescriptorSubtype) { 2736 case UDESCSUB_AC_INPUT: 2737 DPRINTF(" - AC_INPUT type=%s\n", 2738 uaudio_mixer_get_terminal_name 2739 (UGETW(iot[i].u.it->wTerminalType))); 2740 uaudio_mixer_dump_cluster(i, iot); 2741 break; 2742 2743 case UDESCSUB_AC_OUTPUT: 2744 DPRINTF(" - AC_OUTPUT type=%s " 2745 "src=%d\n", uaudio_mixer_get_terminal_name 2746 (UGETW(iot[i].u.ot->wTerminalType)), 2747 iot[i].u.ot->bSourceId); 2748 break; 2749 2750 case UDESCSUB_AC_MIXER: 2751 DPRINTF(" - AC_MIXER src:\n"); 2752 for (j = 0; j < iot[i].u.mu->bNrInPins; j++) { 2753 DPRINTF(" - %d\n", iot[i].u.mu->baSourceId[j]); 2754 } 2755 uaudio_mixer_dump_cluster(i, iot); 2756 break; 2757 2758 case UDESCSUB_AC_SELECTOR: 2759 DPRINTF(" - AC_SELECTOR src:\n"); 2760 for (j = 0; j < iot[i].u.su->bNrInPins; j++) { 2761 DPRINTF(" - %d\n", iot[i].u.su->baSourceId[j]); 2762 } 2763 break; 2764 2765 case UDESCSUB_AC_FEATURE: 2766 DPRINTF(" - AC_FEATURE src=%d\n", iot[i].u.fu->bSourceId); 2767 break; 2768 2769 case UDESCSUB_AC_PROCESSING: 2770 DPRINTF(" - AC_PROCESSING src:\n"); 2771 for (j = 0; j < iot[i].u.pu->bNrInPins; j++) { 2772 DPRINTF(" - %d\n", iot[i].u.pu->baSourceId[j]); 2773 } 2774 uaudio_mixer_dump_cluster(i, iot); 2775 break; 2776 2777 case UDESCSUB_AC_EXTENSION: 2778 DPRINTF(" - AC_EXTENSION src:\n"); 2779 for (j = 0; j < iot[i].u.eu->bNrInPins; j++) { 2780 DPRINTF("%d ", iot[i].u.eu->baSourceId[j]); 2781 } 2782 uaudio_mixer_dump_cluster(i, iot); 2783 break; 2784 2785 default: 2786 DPRINTF("unknown audio control (subtype=%d)\n", 2787 iot[i].u.desc->bDescriptorSubtype); 2788 } 2789 2790 DPRINTF("Inputs to this ID are:\n"); 2791 2792 j = ID_max; 2793 do { 2794 if (iot[i].usr.bit_input[j / 8] & (1 << (j % 8))) { 2795 DPRINTF(" -- ID=%d\n", j); 2796 } 2797 } while (j--); 2798 2799 DPRINTF("Outputs from this ID are:\n"); 2800 2801 j = ID_max; 2802 do { 2803 if (iot[i].usr.bit_output[j / 8] & (1 << (j % 8))) { 2804 DPRINTF(" -- ID=%d\n", j); 2805 } 2806 } while (j--); 2807 2808 } while (i--); 2809 #endif 2810 2811 /* 2812 * scan the config to create a linked 2813 * list of "mixer" nodes: 2814 */ 2815 2816 i = ID_max; 2817 do { 2818 dp = iot[i].u.desc; 2819 2820 if (dp == NULL) { 2821 continue; 2822 } 2823 DPRINTFN(11, "id=%d subtype=%d\n", 2824 i, dp->bDescriptorSubtype); 2825 2826 switch (dp->bDescriptorSubtype) { 2827 case UDESCSUB_AC_HEADER: 2828 DPRINTF("unexpected AC header\n"); 2829 break; 2830 2831 case UDESCSUB_AC_INPUT: 2832 uaudio_mixer_add_input(sc, iot, i); 2833 break; 2834 2835 case UDESCSUB_AC_OUTPUT: 2836 uaudio_mixer_add_output(sc, iot, i); 2837 break; 2838 2839 case UDESCSUB_AC_MIXER: 2840 uaudio_mixer_add_mixer(sc, iot, i); 2841 break; 2842 2843 case UDESCSUB_AC_SELECTOR: 2844 uaudio_mixer_add_selector(sc, iot, i); 2845 break; 2846 2847 case UDESCSUB_AC_FEATURE: 2848 uaudio_mixer_add_feature(sc, iot, i); 2849 break; 2850 2851 case UDESCSUB_AC_PROCESSING: 2852 uaudio_mixer_add_processing(sc, iot, i); 2853 break; 2854 2855 case UDESCSUB_AC_EXTENSION: 2856 uaudio_mixer_add_extension(sc, iot, i); 2857 break; 2858 2859 default: 2860 DPRINTF("bad AC desc subtype=0x%02x\n", 2861 dp->bDescriptorSubtype); 2862 break; 2863 } 2864 2865 } while (i--); 2866 2867 done: 2868 if (iot) { 2869 free(iot, M_TEMP); 2870 } 2871 } 2872 2873 static uint16_t 2874 uaudio_mixer_get(struct usb2_device *udev, uint8_t what, 2875 struct uaudio_mixer_node *mc) 2876 { 2877 struct usb2_device_request req; 2878 uint16_t val; 2879 uint16_t len = MIX_SIZE(mc->type); 2880 uint8_t data[4]; 2881 usb2_error_t err; 2882 2883 if (mc->wValue[0] == -1) { 2884 return (0); 2885 } 2886 req.bmRequestType = UT_READ_CLASS_INTERFACE; 2887 req.bRequest = what; 2888 USETW(req.wValue, mc->wValue[0]); 2889 USETW(req.wIndex, mc->wIndex); 2890 USETW(req.wLength, len); 2891 2892 err = usb2_do_request(udev, &Giant, &req, data); 2893 if (err) { 2894 DPRINTF("err=%s\n", usb2_errstr(err)); 2895 return (0); 2896 } 2897 if (len < 1) { 2898 data[0] = 0; 2899 } 2900 if (len < 2) { 2901 data[1] = 0; 2902 } 2903 val = (data[0] | (data[1] << 8)); 2904 2905 DPRINTFN(3, "val=%d\n", val); 2906 2907 return (val); 2908 } 2909 2910 static void 2911 uaudio_mixer_write_cfg_callback(struct usb2_xfer *xfer) 2912 { 2913 struct usb2_device_request req; 2914 struct uaudio_softc *sc = xfer->priv_sc; 2915 struct uaudio_mixer_node *mc = sc->sc_mixer_curr; 2916 uint16_t len; 2917 uint8_t repeat = 1; 2918 uint8_t update; 2919 uint8_t chan; 2920 uint8_t buf[2]; 2921 2922 DPRINTF("\n"); 2923 2924 switch (USB_GET_STATE(xfer)) { 2925 case USB_ST_TRANSFERRED: 2926 tr_transferred: 2927 case USB_ST_SETUP: 2928 tr_setup: 2929 2930 if (mc == NULL) { 2931 mc = sc->sc_mixer_root; 2932 sc->sc_mixer_curr = mc; 2933 sc->sc_mixer_chan = 0; 2934 repeat = 0; 2935 } 2936 while (mc) { 2937 while (sc->sc_mixer_chan < mc->nchan) { 2938 2939 len = MIX_SIZE(mc->type); 2940 2941 chan = sc->sc_mixer_chan; 2942 2943 sc->sc_mixer_chan++; 2944 2945 update = ((mc->update[chan / 8] & (1 << (chan % 8))) && 2946 (mc->wValue[chan] != -1)); 2947 2948 mc->update[chan / 8] &= ~(1 << (chan % 8)); 2949 2950 if (update) { 2951 2952 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 2953 req.bRequest = SET_CUR; 2954 USETW(req.wValue, mc->wValue[chan]); 2955 USETW(req.wIndex, mc->wIndex); 2956 USETW(req.wLength, len); 2957 2958 if (len > 0) { 2959 buf[0] = (mc->wData[chan] & 0xFF); 2960 } 2961 if (len > 1) { 2962 buf[1] = (mc->wData[chan] >> 8) & 0xFF; 2963 } 2964 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req)); 2965 usb2_copy_in(xfer->frbuffers + 1, 0, buf, len); 2966 2967 xfer->frlengths[0] = sizeof(req); 2968 xfer->frlengths[1] = len; 2969 xfer->nframes = xfer->frlengths[1] ? 2 : 1; 2970 usb2_start_hardware(xfer); 2971 return; 2972 } 2973 } 2974 2975 mc = mc->next; 2976 sc->sc_mixer_curr = mc; 2977 sc->sc_mixer_chan = 0; 2978 } 2979 2980 if (repeat) { 2981 goto tr_setup; 2982 } 2983 break; 2984 2985 default: /* Error */ 2986 DPRINTF("error=%s\n", usb2_errstr(xfer->error)); 2987 if (xfer->error == USB_ERR_CANCELLED) { 2988 /* do nothing - we are detaching */ 2989 break; 2990 } 2991 goto tr_transferred; 2992 } 2993 } 2994 2995 static usb2_error_t 2996 uaudio_set_speed(struct usb2_device *udev, uint8_t endpt, uint32_t speed) 2997 { 2998 struct usb2_device_request req; 2999 uint8_t data[3]; 3000 3001 DPRINTFN(6, "endpt=%d speed=%u\n", endpt, speed); 3002 3003 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT; 3004 req.bRequest = SET_CUR; 3005 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0); 3006 USETW(req.wIndex, endpt); 3007 USETW(req.wLength, 3); 3008 data[0] = speed; 3009 data[1] = speed >> 8; 3010 data[2] = speed >> 16; 3011 3012 return (usb2_do_request(udev, &Giant, &req, data)); 3013 } 3014 3015 static int 3016 uaudio_mixer_signext(uint8_t type, int val) 3017 { 3018 if (!MIX_UNSIGNED(type)) { 3019 if (MIX_SIZE(type) == 2) { 3020 val = (int16_t)val; 3021 } else { 3022 val = (int8_t)val; 3023 } 3024 } 3025 return (val); 3026 } 3027 3028 static int 3029 uaudio_mixer_bsd2value(struct uaudio_mixer_node *mc, int32_t val) 3030 { 3031 if (mc->type == MIX_ON_OFF) { 3032 val = (val != 0); 3033 } else if (mc->type == MIX_SELECTOR) { 3034 if ((val < mc->minval) || 3035 (val > mc->maxval)) { 3036 val = mc->minval; 3037 } 3038 } else { 3039 val = (((val + (mc->delta / 2)) * mc->mul) / 255) + mc->minval; 3040 } 3041 3042 DPRINTFN(6, "type=0x%03x val=%d min=%d max=%d val=%d\n", 3043 mc->type, val, mc->minval, mc->maxval, val); 3044 return (val); 3045 } 3046 3047 static void 3048 uaudio_mixer_ctl_set(struct uaudio_softc *sc, struct uaudio_mixer_node *mc, 3049 uint8_t chan, int32_t val) 3050 { 3051 val = uaudio_mixer_bsd2value(mc, val); 3052 3053 mc->update[chan / 8] |= (1 << (chan % 8)); 3054 mc->wData[chan] = val; 3055 3056 /* start the transfer, if not already started */ 3057 3058 usb2_transfer_start(sc->sc_mixer_xfer[0]); 3059 } 3060 3061 static void 3062 uaudio_mixer_init(struct uaudio_softc *sc) 3063 { 3064 struct uaudio_mixer_node *mc; 3065 int32_t i; 3066 3067 for (mc = sc->sc_mixer_root; mc; 3068 mc = mc->next) { 3069 3070 if (mc->ctl != SOUND_MIXER_NRDEVICES) { 3071 /* 3072 * Set device mask bits. See 3073 * /usr/include/machine/soundcard.h 3074 */ 3075 sc->sc_mix_info |= (1 << mc->ctl); 3076 } 3077 if ((mc->ctl == SOUND_MIXER_NRDEVICES) && 3078 (mc->type == MIX_SELECTOR)) { 3079 3080 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 3081 if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES) { 3082 continue; 3083 } 3084 sc->sc_recsrc_info |= 1 << mc->slctrtype[i - 1]; 3085 } 3086 } 3087 } 3088 } 3089 3090 int 3091 uaudio_mixer_init_sub(struct uaudio_softc *sc, struct snd_mixer *m) 3092 { 3093 DPRINTF("\n"); 3094 3095 if (usb2_transfer_setup(sc->sc_udev, &sc->sc_mixer_iface_index, 3096 sc->sc_mixer_xfer, uaudio_mixer_config, 1, sc, 3097 mixer_get_lock(m))) { 3098 DPRINTFN(0, "could not allocate USB " 3099 "transfer for audio mixer!\n"); 3100 return (ENOMEM); 3101 } 3102 if (!(sc->sc_mix_info & SOUND_MASK_VOLUME)) { 3103 mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM); 3104 mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE); 3105 } 3106 mix_setdevs(m, sc->sc_mix_info); 3107 mix_setrecdevs(m, sc->sc_recsrc_info); 3108 return (0); 3109 } 3110 3111 int 3112 uaudio_mixer_uninit_sub(struct uaudio_softc *sc) 3113 { 3114 DPRINTF("\n"); 3115 3116 usb2_transfer_unsetup(sc->sc_mixer_xfer, 1); 3117 3118 return (0); 3119 } 3120 3121 void 3122 uaudio_mixer_set(struct uaudio_softc *sc, unsigned type, 3123 unsigned left, unsigned right) 3124 { 3125 struct uaudio_mixer_node *mc; 3126 3127 for (mc = sc->sc_mixer_root; mc; 3128 mc = mc->next) { 3129 3130 if (mc->ctl == type) { 3131 if (mc->nchan == 2) { 3132 /* set Right */ 3133 uaudio_mixer_ctl_set(sc, mc, 1, (int)(right * 255) / 100); 3134 } 3135 /* set Left or Mono */ 3136 uaudio_mixer_ctl_set(sc, mc, 0, (int)(left * 255) / 100); 3137 } 3138 } 3139 } 3140 3141 uint32_t 3142 uaudio_mixer_setrecsrc(struct uaudio_softc *sc, uint32_t src) 3143 { 3144 struct uaudio_mixer_node *mc; 3145 uint32_t mask; 3146 uint32_t temp; 3147 int32_t i; 3148 3149 for (mc = sc->sc_mixer_root; mc; 3150 mc = mc->next) { 3151 3152 if ((mc->ctl == SOUND_MIXER_NRDEVICES) && 3153 (mc->type == MIX_SELECTOR)) { 3154 3155 /* compute selector mask */ 3156 3157 mask = 0; 3158 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 3159 mask |= (1 << mc->slctrtype[i - 1]); 3160 } 3161 3162 temp = mask & src; 3163 if (temp == 0) { 3164 continue; 3165 } 3166 /* find the first set bit */ 3167 temp = (-temp) & temp; 3168 3169 /* update "src" */ 3170 src &= ~mask; 3171 src |= temp; 3172 3173 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) { 3174 if (temp != (1 << mc->slctrtype[i - 1])) { 3175 continue; 3176 } 3177 uaudio_mixer_ctl_set(sc, mc, 0, i); 3178 break; 3179 } 3180 } 3181 } 3182 return (src); 3183 } 3184 3185 /*========================================================================* 3186 * MIDI support routines 3187 *========================================================================*/ 3188 3189 static void 3190 umidi_read_clear_stall_callback(struct usb2_xfer *xfer) 3191 { 3192 struct umidi_chan *chan = xfer->priv_sc; 3193 struct usb2_xfer *xfer_other = chan->xfer[1]; 3194 3195 if (usb2_clear_stall_callback(xfer, xfer_other)) { 3196 DPRINTF("stall cleared\n"); 3197 chan->flags &= ~UMIDI_FLAG_READ_STALL; 3198 usb2_transfer_start(xfer_other); 3199 } 3200 } 3201 3202 static void 3203 umidi_bulk_read_callback(struct usb2_xfer *xfer) 3204 { 3205 struct umidi_chan *chan = xfer->priv_sc; 3206 struct umidi_sub_chan *sub; 3207 uint8_t buf[1]; 3208 uint8_t cmd_len; 3209 uint8_t cn; 3210 uint16_t pos; 3211 3212 switch (USB_GET_STATE(xfer)) { 3213 case USB_ST_TRANSFERRED: 3214 3215 DPRINTF("actlen=%d bytes\n", xfer->actlen); 3216 3217 if (xfer->actlen == 0) { 3218 /* should not happen */ 3219 goto tr_error; 3220 } 3221 pos = 0; 3222 3223 while (xfer->actlen >= 4) { 3224 3225 usb2_copy_out(xfer->frbuffers, pos, buf, 1); 3226 3227 cmd_len = umidi_cmd_to_len[buf[0] & 0xF]; /* command length */ 3228 cn = buf[0] >> 4; /* cable number */ 3229 sub = &chan->sub[cn]; 3230 3231 if (cmd_len && (cn < chan->max_cable) && sub->read_open) { 3232 usb2_fifo_put_data(sub->fifo.fp[USB_FIFO_RX], xfer->frbuffers, 3233 pos + 1, cmd_len, 1); 3234 } else { 3235 /* ignore the command */ 3236 } 3237 3238 xfer->actlen -= 4; 3239 pos += 4; 3240 } 3241 3242 case USB_ST_SETUP: 3243 DPRINTF("start\n"); 3244 3245 if (chan->flags & UMIDI_FLAG_READ_STALL) { 3246 usb2_transfer_start(chan->xfer[3]); 3247 return; 3248 } 3249 xfer->frlengths[0] = xfer->max_data_length; 3250 usb2_start_hardware(xfer); 3251 return; 3252 3253 default: 3254 tr_error: 3255 3256 DPRINTF("error=%s\n", usb2_errstr(xfer->error)); 3257 3258 if (xfer->error != USB_ERR_CANCELLED) { 3259 /* try to clear stall first */ 3260 chan->flags |= UMIDI_FLAG_READ_STALL; 3261 usb2_transfer_start(chan->xfer[3]); 3262 } 3263 return; 3264 3265 } 3266 } 3267 3268 static void 3269 umidi_write_clear_stall_callback(struct usb2_xfer *xfer) 3270 { 3271 struct umidi_chan *chan = xfer->priv_sc; 3272 struct usb2_xfer *xfer_other = chan->xfer[0]; 3273 3274 if (usb2_clear_stall_callback(xfer, xfer_other)) { 3275 DPRINTF("stall cleared\n"); 3276 chan->flags &= ~UMIDI_FLAG_WRITE_STALL; 3277 usb2_transfer_start(xfer_other); 3278 } 3279 } 3280 3281 /* 3282 * The following statemachine, that converts MIDI commands to 3283 * USB MIDI packets, derives from Linux's usbmidi.c, which 3284 * was written by "Clemens Ladisch": 3285 * 3286 * Returns: 3287 * 0: No command 3288 * Else: Command is complete 3289 */ 3290 static uint8_t 3291 umidi_convert_to_usb(struct umidi_sub_chan *sub, uint8_t cn, uint8_t b) 3292 { 3293 uint8_t p0 = (cn << 4); 3294 3295 if (b >= 0xf8) { 3296 sub->temp_0[0] = p0 | 0x0f; 3297 sub->temp_0[1] = b; 3298 sub->temp_0[2] = 0; 3299 sub->temp_0[3] = 0; 3300 sub->temp_cmd = sub->temp_0; 3301 return (1); 3302 3303 } else if (b >= 0xf0) { 3304 switch (b) { 3305 case 0xf0: /* system exclusive begin */ 3306 sub->temp_1[1] = b; 3307 sub->state = UMIDI_ST_SYSEX_1; 3308 break; 3309 case 0xf1: /* MIDI time code */ 3310 case 0xf3: /* song select */ 3311 sub->temp_1[1] = b; 3312 sub->state = UMIDI_ST_1PARAM; 3313 break; 3314 case 0xf2: /* song position pointer */ 3315 sub->temp_1[1] = b; 3316 sub->state = UMIDI_ST_2PARAM_1; 3317 break; 3318 case 0xf4: /* unknown */ 3319 case 0xf5: /* unknown */ 3320 sub->state = UMIDI_ST_UNKNOWN; 3321 break; 3322 case 0xf6: /* tune request */ 3323 sub->temp_1[0] = p0 | 0x05; 3324 sub->temp_1[1] = 0xf6; 3325 sub->temp_1[2] = 0; 3326 sub->temp_1[3] = 0; 3327 sub->temp_cmd = sub->temp_1; 3328 sub->state = UMIDI_ST_UNKNOWN; 3329 return (1); 3330 3331 case 0xf7: /* system exclusive end */ 3332 switch (sub->state) { 3333 case UMIDI_ST_SYSEX_0: 3334 sub->temp_1[0] = p0 | 0x05; 3335 sub->temp_1[1] = 0xf7; 3336 sub->temp_1[2] = 0; 3337 sub->temp_1[3] = 0; 3338 sub->temp_cmd = sub->temp_1; 3339 sub->state = UMIDI_ST_UNKNOWN; 3340 return (1); 3341 case UMIDI_ST_SYSEX_1: 3342 sub->temp_1[0] = p0 | 0x06; 3343 sub->temp_1[2] = 0xf7; 3344 sub->temp_1[3] = 0; 3345 sub->temp_cmd = sub->temp_1; 3346 sub->state = UMIDI_ST_UNKNOWN; 3347 return (1); 3348 case UMIDI_ST_SYSEX_2: 3349 sub->temp_1[0] = p0 | 0x07; 3350 sub->temp_1[3] = 0xf7; 3351 sub->temp_cmd = sub->temp_1; 3352 sub->state = UMIDI_ST_UNKNOWN; 3353 return (1); 3354 } 3355 sub->state = UMIDI_ST_UNKNOWN; 3356 break; 3357 } 3358 } else if (b >= 0x80) { 3359 sub->temp_1[1] = b; 3360 if ((b >= 0xc0) && (b <= 0xdf)) { 3361 sub->state = UMIDI_ST_1PARAM; 3362 } else { 3363 sub->state = UMIDI_ST_2PARAM_1; 3364 } 3365 } else { /* b < 0x80 */ 3366 switch (sub->state) { 3367 case UMIDI_ST_1PARAM: 3368 if (sub->temp_1[1] < 0xf0) { 3369 p0 |= sub->temp_1[1] >> 4; 3370 } else { 3371 p0 |= 0x02; 3372 sub->state = UMIDI_ST_UNKNOWN; 3373 } 3374 sub->temp_1[0] = p0; 3375 sub->temp_1[2] = b; 3376 sub->temp_1[3] = 0; 3377 sub->temp_cmd = sub->temp_1; 3378 return (1); 3379 case UMIDI_ST_2PARAM_1: 3380 sub->temp_1[2] = b; 3381 sub->state = UMIDI_ST_2PARAM_2; 3382 break; 3383 case UMIDI_ST_2PARAM_2: 3384 if (sub->temp_1[1] < 0xf0) { 3385 p0 |= sub->temp_1[1] >> 4; 3386 sub->state = UMIDI_ST_2PARAM_1; 3387 } else { 3388 p0 |= 0x03; 3389 sub->state = UMIDI_ST_UNKNOWN; 3390 } 3391 sub->temp_1[0] = p0; 3392 sub->temp_1[3] = b; 3393 sub->temp_cmd = sub->temp_1; 3394 return (1); 3395 case UMIDI_ST_SYSEX_0: 3396 sub->temp_1[1] = b; 3397 sub->state = UMIDI_ST_SYSEX_1; 3398 break; 3399 case UMIDI_ST_SYSEX_1: 3400 sub->temp_1[2] = b; 3401 sub->state = UMIDI_ST_SYSEX_2; 3402 break; 3403 case UMIDI_ST_SYSEX_2: 3404 sub->temp_1[0] = p0 | 0x04; 3405 sub->temp_1[3] = b; 3406 sub->temp_cmd = sub->temp_1; 3407 sub->state = UMIDI_ST_SYSEX_0; 3408 return (1); 3409 } 3410 } 3411 return (0); 3412 } 3413 3414 static void 3415 umidi_bulk_write_callback(struct usb2_xfer *xfer) 3416 { 3417 struct umidi_chan *chan = xfer->priv_sc; 3418 struct umidi_sub_chan *sub; 3419 uint32_t actlen; 3420 uint16_t total_length; 3421 uint8_t buf; 3422 uint8_t start_cable; 3423 uint8_t tr_any; 3424 3425 switch (USB_GET_STATE(xfer)) { 3426 case USB_ST_TRANSFERRED: 3427 DPRINTF("actlen=%d bytes\n", xfer->actlen); 3428 3429 case USB_ST_SETUP: 3430 3431 DPRINTF("start\n"); 3432 3433 if (chan->flags & UMIDI_FLAG_WRITE_STALL) { 3434 usb2_transfer_start(chan->xfer[2]); 3435 return; 3436 } 3437 total_length = 0; /* reset */ 3438 3439 start_cable = chan->curr_cable; 3440 3441 tr_any = 0; 3442 3443 while (1) { 3444 3445 /* round robin de-queueing */ 3446 3447 sub = &chan->sub[chan->curr_cable]; 3448 3449 if (sub->write_open) { 3450 usb2_fifo_get_data(sub->fifo.fp[USB_FIFO_TX], 3451 xfer->frbuffers, total_length, 3452 1, &actlen, 0); 3453 } else { 3454 actlen = 0; 3455 } 3456 3457 if (actlen) { 3458 usb2_copy_out(xfer->frbuffers, total_length, &buf, 1); 3459 3460 tr_any = 1; 3461 3462 DPRINTF("byte=0x%02x\n", buf); 3463 3464 if (umidi_convert_to_usb(sub, chan->curr_cable, buf)) { 3465 3466 DPRINTF("sub= %02x %02x %02x %02x\n", 3467 sub->temp_cmd[0], sub->temp_cmd[1], 3468 sub->temp_cmd[2], sub->temp_cmd[3]); 3469 3470 usb2_copy_in(xfer->frbuffers, total_length, 3471 sub->temp_cmd, 4); 3472 3473 total_length += 4; 3474 3475 if (total_length >= UMIDI_BULK_SIZE) { 3476 break; 3477 } 3478 } else { 3479 continue; 3480 } 3481 } 3482 chan->curr_cable++; 3483 if (chan->curr_cable >= chan->max_cable) { 3484 chan->curr_cable = 0; 3485 } 3486 if (chan->curr_cable == start_cable) { 3487 if (tr_any == 0) { 3488 break; 3489 } 3490 tr_any = 0; 3491 } 3492 } 3493 3494 if (total_length) { 3495 xfer->frlengths[0] = total_length; 3496 usb2_start_hardware(xfer); 3497 } 3498 return; 3499 3500 default: /* Error */ 3501 3502 DPRINTF("error=%s\n", usb2_errstr(xfer->error)); 3503 3504 if (xfer->error != USB_ERR_CANCELLED) { 3505 /* try to clear stall first */ 3506 chan->flags |= UMIDI_FLAG_WRITE_STALL; 3507 usb2_transfer_start(chan->xfer[2]); 3508 } 3509 return; 3510 3511 } 3512 } 3513 3514 static struct umidi_sub_chan * 3515 umidi_sub_by_fifo(struct usb2_fifo *fifo) 3516 { 3517 struct umidi_chan *chan = fifo->priv_sc0; 3518 struct umidi_sub_chan *sub; 3519 uint32_t n; 3520 3521 for (n = 0; n < UMIDI_CABLES_MAX; n++) { 3522 sub = &chan->sub[n]; 3523 if ((sub->fifo.fp[USB_FIFO_RX] == fifo) || 3524 (sub->fifo.fp[USB_FIFO_TX] == fifo)) { 3525 return (sub); 3526 } 3527 } 3528 3529 panic("%s:%d cannot find usb2_fifo!\n", 3530 __FILE__, __LINE__); 3531 3532 return (NULL); 3533 } 3534 3535 static void 3536 umidi_start_read(struct usb2_fifo *fifo) 3537 { 3538 struct umidi_chan *chan = fifo->priv_sc0; 3539 3540 usb2_transfer_start(chan->xfer[1]); 3541 } 3542 3543 static void 3544 umidi_stop_read(struct usb2_fifo *fifo) 3545 { 3546 struct umidi_chan *chan = fifo->priv_sc0; 3547 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 3548 3549 DPRINTF("\n"); 3550 3551 sub->read_open = 0; 3552 3553 if (--(chan->read_open_refcount) == 0) { 3554 /* 3555 * XXX don't stop the read transfer here, hence that causes 3556 * problems with some MIDI adapters 3557 */ 3558 DPRINTF("(stopping read transfer)\n"); 3559 } 3560 } 3561 3562 static void 3563 umidi_start_write(struct usb2_fifo *fifo) 3564 { 3565 struct umidi_chan *chan = fifo->priv_sc0; 3566 3567 usb2_transfer_start(chan->xfer[0]); 3568 } 3569 3570 static void 3571 umidi_stop_write(struct usb2_fifo *fifo) 3572 { 3573 struct umidi_chan *chan = fifo->priv_sc0; 3574 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 3575 3576 DPRINTF("\n"); 3577 3578 sub->write_open = 0; 3579 3580 if (--(chan->write_open_refcount) == 0) { 3581 DPRINTF("(stopping write transfer)\n"); 3582 usb2_transfer_stop(chan->xfer[2]); 3583 usb2_transfer_stop(chan->xfer[0]); 3584 } 3585 } 3586 3587 static int 3588 umidi_open(struct usb2_fifo *fifo, int fflags) 3589 { 3590 struct umidi_chan *chan = fifo->priv_sc0; 3591 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo); 3592 3593 if (fflags & FREAD) { 3594 if (usb2_fifo_alloc_buffer(fifo, 4, (1024 / 4))) { 3595 return (ENOMEM); 3596 } 3597 mtx_lock(fifo->priv_mtx); 3598 chan->read_open_refcount++; 3599 sub->read_open = 1; 3600 mtx_unlock(fifo->priv_mtx); 3601 } 3602 if (fflags & FWRITE) { 3603 if (usb2_fifo_alloc_buffer(fifo, 32, (1024 / 32))) { 3604 return (ENOMEM); 3605 } 3606 /* clear stall first */ 3607 mtx_lock(fifo->priv_mtx); 3608 chan->flags |= UMIDI_FLAG_WRITE_STALL; 3609 chan->write_open_refcount++; 3610 sub->write_open = 1; 3611 3612 /* reset */ 3613 sub->state = UMIDI_ST_UNKNOWN; 3614 mtx_unlock(fifo->priv_mtx); 3615 } 3616 return (0); /* success */ 3617 } 3618 3619 static void 3620 umidi_close(struct usb2_fifo *fifo, int fflags) 3621 { 3622 if (fflags & FREAD) { 3623 usb2_fifo_free_buffer(fifo); 3624 } 3625 if (fflags & FWRITE) { 3626 usb2_fifo_free_buffer(fifo); 3627 } 3628 } 3629 3630 3631 static int 3632 umidi_ioctl(struct usb2_fifo *fifo, u_long cmd, void *data, 3633 int fflags) 3634 { 3635 return (ENODEV); 3636 } 3637 3638 static void 3639 umidi_init(device_t dev) 3640 { 3641 struct uaudio_softc *sc = device_get_softc(dev); 3642 struct umidi_chan *chan = &sc->sc_midi_chan; 3643 3644 mtx_init(&chan->mtx, "umidi lock", NULL, MTX_DEF | MTX_RECURSE); 3645 } 3646 3647 static struct usb2_fifo_methods umidi_fifo_methods = { 3648 .f_start_read = &umidi_start_read, 3649 .f_start_write = &umidi_start_write, 3650 .f_stop_read = &umidi_stop_read, 3651 .f_stop_write = &umidi_stop_write, 3652 .f_open = &umidi_open, 3653 .f_close = &umidi_close, 3654 .f_ioctl = &umidi_ioctl, 3655 .basename[0] = "umidi", 3656 }; 3657 3658 static int32_t 3659 umidi_probe(device_t dev) 3660 { 3661 struct uaudio_softc *sc = device_get_softc(dev); 3662 struct usb2_attach_arg *uaa = device_get_ivars(dev); 3663 struct umidi_chan *chan = &sc->sc_midi_chan; 3664 struct umidi_sub_chan *sub; 3665 int unit = device_get_unit(dev); 3666 int error; 3667 uint32_t n; 3668 3669 if (usb2_set_alt_interface_index(sc->sc_udev, chan->iface_index, 3670 chan->iface_alt_index)) { 3671 DPRINTF("setting of alternate index failed!\n"); 3672 goto detach; 3673 } 3674 usb2_set_parent_iface(sc->sc_udev, chan->iface_index, sc->sc_mixer_iface_index); 3675 3676 error = usb2_transfer_setup(uaa->device, &chan->iface_index, 3677 chan->xfer, umidi_config, UMIDI_N_TRANSFER, 3678 chan, &chan->mtx); 3679 if (error) { 3680 DPRINTF("error=%s\n", usb2_errstr(error)); 3681 goto detach; 3682 } 3683 if ((chan->max_cable > UMIDI_CABLES_MAX) || 3684 (chan->max_cable == 0)) { 3685 chan->max_cable = UMIDI_CABLES_MAX; 3686 } 3687 3688 for (n = 0; n < chan->max_cable; n++) { 3689 3690 sub = &chan->sub[n]; 3691 3692 error = usb2_fifo_attach(sc->sc_udev, chan, &chan->mtx, 3693 &umidi_fifo_methods, &sub->fifo, unit, n, 3694 chan->iface_index, 3695 UID_ROOT, GID_OPERATOR, 0644); 3696 if (error) { 3697 goto detach; 3698 } 3699 } 3700 3701 mtx_lock(&chan->mtx); 3702 3703 /* clear stall first */ 3704 chan->flags |= UMIDI_FLAG_READ_STALL; 3705 3706 /* 3707 * NOTE: at least one device will not work properly unless 3708 * the BULK pipe is open all the time. 3709 */ 3710 usb2_transfer_start(chan->xfer[1]); 3711 3712 mtx_unlock(&chan->mtx); 3713 3714 return (0); /* success */ 3715 3716 detach: 3717 return (ENXIO); /* failure */ 3718 } 3719 3720 static int32_t 3721 umidi_detach(device_t dev) 3722 { 3723 struct uaudio_softc *sc = device_get_softc(dev); 3724 struct umidi_chan *chan = &sc->sc_midi_chan; 3725 uint32_t n; 3726 3727 for (n = 0; n < UMIDI_CABLES_MAX; n++) { 3728 usb2_fifo_detach(&chan->sub[n].fifo); 3729 } 3730 3731 mtx_lock(&chan->mtx); 3732 3733 usb2_transfer_stop(chan->xfer[3]); 3734 usb2_transfer_stop(chan->xfer[1]); 3735 3736 mtx_unlock(&chan->mtx); 3737 3738 usb2_transfer_unsetup(chan->xfer, UMIDI_N_TRANSFER); 3739 3740 mtx_destroy(&chan->mtx); 3741 3742 return (0); 3743 } 3744 3745 DRIVER_MODULE(uaudio, ushub, uaudio_driver, uaudio_devclass, NULL, 0); 3746 MODULE_DEPEND(uaudio, usb, 1, 1, 1); 3747 MODULE_DEPEND(uaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 3748 MODULE_VERSION(uaudio, 1); 3749