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