1 /* $OpenBSD: uaudio.c,v 1.115 2016/09/19 06:46:44 ratchov Exp $ */ 2 /* $NetBSD: uaudio.c,v 1.90 2004/10/29 17:12:53 kent Exp $ */ 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 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/device.h> 45 #include <sys/ioctl.h> 46 #include <sys/tty.h> 47 #include <sys/file.h> 48 #include <sys/selinfo.h> 49 #include <sys/poll.h> 50 51 #include <machine/bus.h> 52 53 #include <sys/audioio.h> 54 #include <dev/audio_if.h> 55 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdivar.h> 61 62 #include <dev/usb/uaudioreg.h> 63 64 /* #define UAUDIO_DEBUG */ 65 #ifdef UAUDIO_DEBUG 66 #define DPRINTF(x) do { if (uaudiodebug) printf x; } while (0) 67 #define DPRINTFN(n,x) do { if (uaudiodebug>(n)) printf x; } while (0) 68 int uaudiodebug = 0; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 #define UAUDIO_NCHANBUFS 3 /* number of outstanding request */ 75 #define UAUDIO_MIN_FRAMES 2 /* ms of sound in each request */ 76 #define UAUDIO_MAX_FRAMES 16 77 #define UAUDIO_NSYNCBUFS 3 /* number of outstanding sync requests */ 78 79 #define UAUDIO_MAX_ALTS 32 /* max alt settings allowed by driver */ 80 81 #define MIX_MAX_CHAN 8 82 struct mixerctl { 83 u_int16_t wValue[MIX_MAX_CHAN]; /* using nchan */ 84 u_int16_t wIndex; 85 u_int8_t nchan; 86 u_int8_t type; 87 #define MIX_ON_OFF 1 88 #define MIX_SIGNED_16 2 89 #define MIX_UNSIGNED_16 3 90 #define MIX_SIGNED_8 4 91 #define MIX_SELECTOR 5 92 #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1) 93 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16) 94 int minval, maxval; 95 u_int delta; 96 u_int8_t class; 97 char ctlname[MAX_AUDIO_DEV_LEN]; 98 char *ctlunit; 99 }; 100 #define MAKE(h,l) (((h) << 8) | (l)) 101 102 struct as_info { 103 u_int8_t alt; 104 u_int8_t encoding; 105 u_int8_t attributes; /* Copy of bmAttributes of 106 * usb_audio_streaming_endpoint_descriptor 107 */ 108 struct usbd_interface *ifaceh; 109 const usb_interface_descriptor_t *idesc; 110 const struct usb_endpoint_descriptor_audio *edesc; 111 const struct usb_endpoint_descriptor_audio *edesc1; 112 const struct usb_audio_streaming_type1_descriptor *asf1desc; 113 int sc_busy; /* currently used */ 114 }; 115 116 struct chan { 117 void (*intr)(void *); /* DMA completion intr handler */ 118 void *arg; /* arg for intr() */ 119 struct usbd_pipe *pipe; 120 struct usbd_pipe *sync_pipe; 121 122 u_int sample_size; 123 u_int sample_rate; 124 u_int bytes_per_frame; 125 u_int max_bytes_per_frame; 126 u_int fraction; /* fraction/frac_denom is the extra samples/frame */ 127 u_int frac_denom; /* denominator for fractional samples */ 128 u_int residue; /* accumulates the fractional samples */ 129 u_int nframes; /* # of frames per transfer */ 130 u_int nsync_frames; /* # of frames per sync transfer */ 131 u_int usb_fps; 132 u_int maxpktsize; 133 u_int reqms; /* usb request data duration, in ms */ 134 u_int hi_speed; 135 136 u_char *start; /* upper layer buffer start */ 137 u_char *end; /* upper layer buffer end */ 138 u_char *cur; /* current position in upper layer buffer */ 139 int blksize; /* chunk size to report up */ 140 int transferred; /* transferred bytes not reported up */ 141 142 int altidx; /* currently used altidx */ 143 144 int curchanbuf; 145 int cursyncbuf; 146 147 struct chanbuf { 148 struct chan *chan; 149 struct usbd_xfer *xfer; 150 u_char *buffer; 151 u_int16_t sizes[UAUDIO_MAX_FRAMES]; 152 u_int16_t offsets[UAUDIO_MAX_FRAMES]; 153 u_int16_t size; 154 } chanbufs[UAUDIO_NCHANBUFS]; 155 156 struct syncbuf { 157 struct chan *chan; 158 struct usbd_xfer *xfer; 159 u_char *buffer; 160 u_int16_t sizes[UAUDIO_MAX_FRAMES]; 161 u_int16_t offsets[UAUDIO_MAX_FRAMES]; 162 u_int16_t size; 163 } syncbufs[UAUDIO_NSYNCBUFS]; 164 165 struct uaudio_softc *sc; /* our softc */ 166 }; 167 168 #define UAUDIO_FLAG_BAD_AUDIO 0x0001 /* claims audio class, but isn't */ 169 #define UAUDIO_FLAG_NO_FRAC 0x0002 /* don't use fractional samples */ 170 #define UAUDIO_FLAG_NO_XU 0x0004 /* has broken extension unit */ 171 #define UAUDIO_FLAG_BAD_ADC 0x0008 /* bad audio spec version number */ 172 #define UAUDIO_FLAG_VENDOR_CLASS 0x0010 /* claims vendor class but works */ 173 #define UAUDIO_FLAG_DEPENDENT 0x0020 /* play and record params must equal */ 174 #define UAUDIO_FLAG_EMU0202 0x0040 175 176 struct uaudio_devs { 177 struct usb_devno uv_dev; 178 int flags; 179 } uaudio_devs[] = { 180 { { USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ADA70 }, 181 UAUDIO_FLAG_BAD_ADC } , 182 { { USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ASC495 }, 183 UAUDIO_FLAG_BAD_AUDIO }, 184 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE }, 185 UAUDIO_FLAG_BAD_AUDIO }, 186 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G }, 187 UAUDIO_FLAG_BAD_AUDIO }, 188 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS }, 189 UAUDIO_FLAG_BAD_AUDIO }, 190 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4_GSM }, 191 UAUDIO_FLAG_BAD_AUDIO }, 192 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4_CDMA }, 193 UAUDIO_FLAG_BAD_AUDIO }, 194 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S }, 195 UAUDIO_FLAG_BAD_AUDIO }, 196 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_6 }, 197 UAUDIO_FLAG_BAD_AUDIO }, 198 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPOD_TOUCH }, 199 UAUDIO_FLAG_BAD_AUDIO }, 200 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPOD_TOUCH_2G }, 201 UAUDIO_FLAG_BAD_AUDIO }, 202 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPOD_TOUCH_3G }, 203 UAUDIO_FLAG_BAD_AUDIO }, 204 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPOD_TOUCH_4G }, 205 UAUDIO_FLAG_BAD_AUDIO }, 206 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD }, 207 UAUDIO_FLAG_BAD_AUDIO }, 208 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD2 }, 209 UAUDIO_FLAG_BAD_AUDIO }, 210 { { USB_VENDOR_CREATIVE, USB_PRODUCT_CREATIVE_EMU0202 }, 211 UAUDIO_FLAG_VENDOR_CLASS | UAUDIO_FLAG_EMU0202 | 212 UAUDIO_FLAG_DEPENDENT }, 213 { { USB_VENDOR_DALLAS, USB_PRODUCT_DALLAS_J6502 }, 214 UAUDIO_FLAG_NO_XU | UAUDIO_FLAG_BAD_ADC }, 215 { { USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_QUICKCAMNBDLX }, 216 UAUDIO_FLAG_BAD_AUDIO }, 217 { { USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_QUICKCAMPRONB }, 218 UAUDIO_FLAG_BAD_AUDIO }, 219 { { USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_QUICKCAMPRO4K }, 220 UAUDIO_FLAG_BAD_AUDIO }, 221 { { USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_QUICKCAMZOOM }, 222 UAUDIO_FLAG_BAD_AUDIO }, 223 { { USB_VENDOR_TELEX, USB_PRODUCT_TELEX_MIC1 }, 224 UAUDIO_FLAG_NO_FRAC } 225 }; 226 #define uaudio_lookup(v, p) \ 227 ((struct uaudio_devs *)usb_lookup(uaudio_devs, v, p)) 228 229 struct uaudio_softc { 230 struct device sc_dev; /* base device */ 231 struct usbd_device *sc_udev; /* USB device */ 232 int sc_ac_iface; /* Audio Control interface */ 233 struct chan sc_playchan; /* play channel */ 234 struct chan sc_recchan; /* record channel */ 235 int sc_nullalt; 236 int sc_audio_rev; 237 struct as_info *sc_alts; /* alternate settings */ 238 int sc_nalts; /* # of alternate settings */ 239 int sc_altflags; 240 #define HAS_8 0x01 241 #define HAS_16 0x02 242 #define HAS_8U 0x04 243 #define HAS_ALAW 0x08 244 #define HAS_MULAW 0x10 245 #define UA_NOFRAC 0x20 /* don't do sample rate adjustment */ 246 #define HAS_24 0x40 247 int sc_mode; /* play/record capability */ 248 struct mixerctl *sc_ctls; /* mixer controls */ 249 int sc_nctls; /* # of mixer controls */ 250 int sc_quirks; 251 }; 252 253 struct terminal_list { 254 int size; 255 uint16_t terminals[1]; 256 }; 257 #define TERMINAL_LIST_SIZE(N) (offsetof(struct terminal_list, terminals) \ 258 + sizeof(uint16_t) * (N)) 259 260 struct io_terminal { 261 union { 262 const usb_descriptor_t *desc; 263 const struct usb_audio_input_terminal *it; 264 const struct usb_audio_output_terminal *ot; 265 const struct usb_audio_mixer_unit *mu; 266 const struct usb_audio_selector_unit *su; 267 const struct usb_audio_feature_unit *fu; 268 const struct usb_audio_processing_unit *pu; 269 const struct usb_audio_extension_unit *eu; 270 } d; 271 int inputs_size; 272 struct terminal_list **inputs; /* list of source input terminals */ 273 struct terminal_list *output; /* list of destination output terminals */ 274 int direct; /* directly connected to an output terminal */ 275 }; 276 277 #define UAC_OUTPUT 0 278 #define UAC_INPUT 1 279 #define UAC_EQUAL 2 280 #define UAC_RECORD 3 281 #define UAC_NCLASSES 4 282 #ifdef UAUDIO_DEBUG 283 const char *uac_names[] = { 284 AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord, 285 }; 286 #endif 287 288 usbd_status uaudio_identify_ac 289 (struct uaudio_softc *, const usb_config_descriptor_t *); 290 usbd_status uaudio_identify_as 291 (struct uaudio_softc *, const usb_config_descriptor_t *); 292 usbd_status uaudio_process_as 293 (struct uaudio_softc *, const char *, int *, int, 294 const usb_interface_descriptor_t *); 295 296 void uaudio_add_alt(struct uaudio_softc *, const struct as_info *); 297 298 const usb_interface_descriptor_t *uaudio_find_iface 299 (const char *, int, int *, int, int); 300 301 void uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *); 302 char *uaudio_id_name 303 (struct uaudio_softc *, const struct io_terminal *, int); 304 uByte uaudio_get_cluster_nchan 305 (int, const struct io_terminal *); 306 void uaudio_add_input 307 (struct uaudio_softc *, const struct io_terminal *, int); 308 void uaudio_add_output 309 (struct uaudio_softc *, const struct io_terminal *, int); 310 void uaudio_add_mixer 311 (struct uaudio_softc *, const struct io_terminal *, int); 312 void uaudio_add_selector 313 (struct uaudio_softc *, const struct io_terminal *, int); 314 #ifdef UAUDIO_DEBUG 315 const char *uaudio_get_terminal_name(int); 316 #endif 317 int uaudio_determine_class 318 (const struct io_terminal *, struct mixerctl *); 319 const char *uaudio_feature_name 320 (const struct io_terminal *, struct mixerctl *); 321 void uaudio_add_feature 322 (struct uaudio_softc *, const struct io_terminal *, int); 323 void uaudio_add_processing_updown 324 (struct uaudio_softc *, const struct io_terminal *, int); 325 void uaudio_add_processing 326 (struct uaudio_softc *, const struct io_terminal *, int); 327 void uaudio_add_extension 328 (struct uaudio_softc *, const struct io_terminal *, int); 329 struct terminal_list *uaudio_merge_terminal_list 330 (const struct io_terminal *); 331 struct terminal_list *uaudio_io_terminaltype 332 (int, struct io_terminal *, int); 333 usbd_status uaudio_identify 334 (struct uaudio_softc *, const usb_config_descriptor_t *); 335 336 int uaudio_signext(int, int); 337 int uaudio_unsignext(int, int); 338 int uaudio_value2bsd(struct mixerctl *, int); 339 int uaudio_bsd2value(struct mixerctl *, int); 340 int uaudio_get(struct uaudio_softc *, int, int, int, int, int); 341 int uaudio_ctl_get 342 (struct uaudio_softc *, int, struct mixerctl *, int); 343 void uaudio_set 344 (struct uaudio_softc *, int, int, int, int, int, int); 345 void uaudio_ctl_set 346 (struct uaudio_softc *, int, struct mixerctl *, int, int); 347 348 usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int); 349 void uaudio_set_speed_emu0202(struct chan *ch); 350 351 usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *); 352 void uaudio_chan_close(struct uaudio_softc *, struct chan *); 353 usbd_status uaudio_chan_alloc_buffers 354 (struct uaudio_softc *, struct chan *); 355 void uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *); 356 void uaudio_chan_init 357 (struct chan *, int, int, const struct audio_params *); 358 void uaudio_chan_set_param(struct chan *, u_char *, u_char *, int); 359 void uaudio_chan_ptransfer(struct chan *); 360 void uaudio_chan_pintr 361 (struct usbd_xfer *, void *, usbd_status); 362 void uaudio_chan_psync_transfer(struct chan *); 363 void uaudio_chan_psync_intr 364 (struct usbd_xfer *, void *, usbd_status); 365 366 void uaudio_chan_rtransfer(struct chan *); 367 void uaudio_chan_rintr 368 (struct usbd_xfer *, void *, usbd_status); 369 370 int uaudio_open(void *, int); 371 void uaudio_close(void *); 372 int uaudio_drain(void *); 373 void uaudio_get_minmax_rates 374 (int, const struct as_info *, const struct audio_params *, 375 int, int, int, u_long *, u_long *); 376 int uaudio_match_alt_rate(void *, int, int); 377 int uaudio_match_alt(void *, struct audio_params *, int); 378 int uaudio_set_params 379 (void *, int, int, struct audio_params *, struct audio_params *); 380 int uaudio_round_blocksize(void *, int); 381 int uaudio_trigger_output 382 (void *, void *, void *, int, void (*)(void *), void *, 383 struct audio_params *); 384 int uaudio_trigger_input 385 (void *, void *, void *, int, void (*)(void *), void *, 386 struct audio_params *); 387 int uaudio_halt_in_dma(void *); 388 int uaudio_halt_out_dma(void *); 389 int uaudio_mixer_set_port(void *, mixer_ctrl_t *); 390 int uaudio_mixer_get_port(void *, mixer_ctrl_t *); 391 int uaudio_query_devinfo(void *, mixer_devinfo_t *); 392 int uaudio_get_props(void *); 393 394 struct audio_hw_if uaudio_hw_if = { 395 uaudio_open, 396 uaudio_close, 397 uaudio_set_params, 398 uaudio_round_blocksize, 399 NULL, 400 NULL, 401 NULL, 402 NULL, 403 NULL, 404 uaudio_halt_out_dma, 405 uaudio_halt_in_dma, 406 NULL, 407 NULL, 408 uaudio_mixer_set_port, 409 uaudio_mixer_get_port, 410 uaudio_query_devinfo, 411 NULL, 412 NULL, 413 NULL, 414 uaudio_get_props, 415 uaudio_trigger_output, 416 uaudio_trigger_input 417 }; 418 419 int uaudio_match(struct device *, void *, void *); 420 void uaudio_attach(struct device *, struct device *, void *); 421 int uaudio_detach(struct device *, int); 422 423 struct cfdriver uaudio_cd = { 424 NULL, "uaudio", DV_DULL 425 }; 426 427 const struct cfattach uaudio_ca = { 428 sizeof(struct uaudio_softc), uaudio_match, uaudio_attach, uaudio_detach 429 }; 430 431 int 432 uaudio_match(struct device *parent, void *match, void *aux) 433 { 434 struct usb_attach_arg *uaa = aux; 435 usb_interface_descriptor_t *id; 436 const usb_interface_descriptor_t *cd_id; 437 usb_config_descriptor_t *cdesc; 438 struct uaudio_devs *quirk; 439 const char *buf; 440 int flags = 0, size, offs; 441 442 if (uaa->iface == NULL || uaa->device == NULL) 443 return (UMATCH_NONE); 444 445 quirk = uaudio_lookup(uaa->vendor, uaa->product); 446 if (quirk) 447 flags = quirk->flags; 448 449 if (flags & UAUDIO_FLAG_BAD_AUDIO) 450 return (UMATCH_NONE); 451 452 id = usbd_get_interface_descriptor(uaa->iface); 453 if (id == NULL) 454 return (UMATCH_NONE); 455 456 if (!(id->bInterfaceClass == UICLASS_AUDIO || 457 ((flags & UAUDIO_FLAG_VENDOR_CLASS) && 458 id->bInterfaceClass == UICLASS_VENDOR))) 459 return (UMATCH_NONE); 460 461 if (id->bInterfaceSubClass != UISUBCLASS_AUDIOCONTROL) 462 return (UMATCH_NONE); 463 464 cdesc = usbd_get_config_descriptor(uaa->device); 465 if (cdesc == NULL) 466 return (UMATCH_NONE); 467 468 size = UGETW(cdesc->wTotalLength); 469 buf = (const char *)cdesc; 470 471 offs = 0; 472 cd_id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOSTREAM, 473 flags); 474 if (cd_id == NULL) 475 return (UMATCH_NONE); 476 477 offs = 0; 478 cd_id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOCONTROL, 479 flags); 480 if (cd_id == NULL) 481 return (UMATCH_NONE); 482 483 return (UMATCH_VENDOR_PRODUCT_CONF_IFACE); 484 } 485 486 void 487 uaudio_attach(struct device *parent, struct device *self, void *aux) 488 { 489 struct uaudio_softc *sc = (struct uaudio_softc *)self; 490 struct usb_attach_arg *uaa = aux; 491 struct uaudio_devs *quirk; 492 usb_interface_descriptor_t *id; 493 usb_config_descriptor_t *cdesc; 494 usbd_status err; 495 int i, j, found; 496 497 sc->sc_udev = uaa->device; 498 499 quirk = uaudio_lookup(uaa->vendor, uaa->product); 500 if (quirk) 501 sc->sc_quirks = quirk->flags; 502 503 cdesc = usbd_get_config_descriptor(sc->sc_udev); 504 if (cdesc == NULL) { 505 printf("%s: failed to get configuration descriptor\n", 506 sc->sc_dev.dv_xname); 507 return; 508 } 509 510 err = uaudio_identify(sc, cdesc); 511 if (err) { 512 printf("%s: audio descriptors make no sense, error=%d\n", 513 sc->sc_dev.dv_xname, err); 514 return; 515 } 516 517 /* Pick up the AS interface. */ 518 for (i = 0; i < uaa->nifaces; i++) { 519 if (usbd_iface_claimed(sc->sc_udev, i)) 520 continue; 521 id = usbd_get_interface_descriptor(uaa->ifaces[i]); 522 if (id == NULL) 523 continue; 524 found = 0; 525 for (j = 0; j < sc->sc_nalts; j++) { 526 if (id->bInterfaceNumber == 527 sc->sc_alts[j].idesc->bInterfaceNumber) { 528 sc->sc_alts[j].ifaceh = uaa->ifaces[i]; 529 found = 1; 530 } 531 } 532 if (found) 533 usbd_claim_iface(sc->sc_udev, i); 534 } 535 536 for (j = 0; j < sc->sc_nalts; j++) { 537 if (sc->sc_alts[j].ifaceh == NULL) { 538 printf("%s: alt %d missing AS interface(s)\n", 539 sc->sc_dev.dv_xname, j); 540 return; 541 } 542 } 543 544 printf("%s: audio rev %d.%02x", sc->sc_dev.dv_xname, 545 sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff); 546 547 sc->sc_playchan.sc = sc->sc_recchan.sc = sc; 548 sc->sc_playchan.altidx = -1; 549 sc->sc_recchan.altidx = -1; 550 551 if (sc->sc_quirks & UAUDIO_FLAG_NO_FRAC) 552 sc->sc_altflags |= UA_NOFRAC; 553 554 printf(", %d mixer controls\n", sc->sc_nctls); 555 556 DPRINTF(("uaudio_attach: doing audio_attach_mi\n")); 557 audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev); 558 } 559 560 int 561 uaudio_detach(struct device *self, int flags) 562 { 563 struct uaudio_softc *sc = (struct uaudio_softc *)self; 564 int rv = 0; 565 566 /* 567 * sc_alts may be NULL if uaudio_identify_as() failed, in 568 * which case uaudio_attach() didn't finish and there's 569 * nothing to detach. 570 */ 571 if (sc->sc_alts == NULL) 572 return (rv); 573 574 /* Wait for outstanding requests to complete. */ 575 uaudio_drain(sc); 576 577 rv = config_detach_children(self, flags); 578 579 return (rv); 580 } 581 582 const usb_interface_descriptor_t * 583 uaudio_find_iface(const char *buf, int size, int *offsp, int subtype, int flags) 584 { 585 const usb_interface_descriptor_t *d; 586 587 while (*offsp < size) { 588 d = (const void *)(buf + *offsp); 589 *offsp += d->bLength; 590 if (d->bDescriptorType == UDESC_INTERFACE && 591 d->bInterfaceSubClass == subtype && 592 (d->bInterfaceClass == UICLASS_AUDIO || 593 (d->bInterfaceClass == UICLASS_VENDOR && 594 (flags & UAUDIO_FLAG_VENDOR_CLASS)))) 595 return (d); 596 } 597 return (NULL); 598 } 599 600 void 601 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc) 602 { 603 int res, range; 604 size_t len; 605 struct mixerctl *nmc; 606 607 if (mc->class < UAC_NCLASSES) { 608 DPRINTF(("%s: adding %s.%s\n", 609 __func__, uac_names[mc->class], mc->ctlname)); 610 } else { 611 DPRINTF(("%s: adding %s\n", __func__, mc->ctlname)); 612 } 613 614 nmc = mallocarray(sc->sc_nctls + 1, sizeof(*mc), M_USBDEV, M_NOWAIT); 615 if (nmc == NULL) { 616 printf("uaudio_mixer_add_ctl: no memory\n"); 617 return; 618 } 619 len = sizeof(*mc) * (sc->sc_nctls + 1); 620 621 /* Copy old data, if there was any */ 622 if (sc->sc_nctls != 0) { 623 bcopy(sc->sc_ctls, nmc, sizeof(*mc) * (sc->sc_nctls)); 624 free(sc->sc_ctls, M_USBDEV, 0); 625 } 626 sc->sc_ctls = nmc; 627 628 mc->delta = 0; 629 if (mc->type == MIX_ON_OFF) { 630 mc->minval = 0; 631 mc->maxval = 1; 632 } else if (mc->type == MIX_SELECTOR) { 633 ; 634 } else { 635 /* Determine min and max values. */ 636 mc->minval = uaudio_signext(mc->type, 637 uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE, 638 mc->wValue[0], mc->wIndex, 639 MIX_SIZE(mc->type))); 640 mc->maxval = uaudio_signext(mc->type, 641 uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE, 642 mc->wValue[0], mc->wIndex, 643 MIX_SIZE(mc->type))); 644 range = mc->maxval - mc->minval; 645 res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE, 646 mc->wValue[0], mc->wIndex, 647 MIX_SIZE(mc->type)); 648 if (res > 0 && range > 0) 649 mc->delta = (res * 255 + res - 1) / range; 650 } 651 652 sc->sc_ctls[sc->sc_nctls++] = *mc; 653 654 #ifdef UAUDIO_DEBUG 655 if (uaudiodebug > 2) { 656 int i; 657 DPRINTF(("uaudio_mixer_add_ctl: wValue=%04x",mc->wValue[0])); 658 for (i = 1; i < mc->nchan; i++) 659 DPRINTF((",%04x", mc->wValue[i])); 660 DPRINTF((" wIndex=%04x type=%d name='%s' unit='%s' " 661 "min=%d max=%d\n", 662 mc->wIndex, mc->type, mc->ctlname, mc->ctlunit, 663 mc->minval, mc->maxval)); 664 } 665 #endif 666 } 667 668 char * 669 uaudio_id_name(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 670 { 671 static char buf[32]; 672 snprintf(buf, sizeof(buf), "i%d", id); 673 return (buf); 674 } 675 676 uByte 677 uaudio_get_cluster_nchan(int id, const struct io_terminal *iot) 678 { 679 struct usb_audio_cluster r; 680 const usb_descriptor_t *dp; 681 int i; 682 683 for (i = 0; i < 25; i++) { /* avoid infinite loops */ 684 dp = iot[id].d.desc; 685 if (dp == 0) 686 goto bad; 687 switch (dp->bDescriptorSubtype) { 688 case UDESCSUB_AC_INPUT: 689 return (iot[id].d.it->bNrChannels); 690 case UDESCSUB_AC_OUTPUT: 691 id = iot[id].d.ot->bSourceId; 692 break; 693 case UDESCSUB_AC_MIXER: 694 r = *(struct usb_audio_cluster *) 695 &iot[id].d.mu->baSourceId[iot[id].d.mu->bNrInPins]; 696 return (r.bNrChannels); 697 case UDESCSUB_AC_SELECTOR: 698 /* XXX This is not really right */ 699 id = iot[id].d.su->baSourceId[0]; 700 break; 701 case UDESCSUB_AC_FEATURE: 702 id = iot[id].d.fu->bSourceId; 703 break; 704 case UDESCSUB_AC_PROCESSING: 705 r = *(struct usb_audio_cluster *) 706 &iot[id].d.pu->baSourceId[iot[id].d.pu->bNrInPins]; 707 return (r.bNrChannels); 708 case UDESCSUB_AC_EXTENSION: 709 r = *(struct usb_audio_cluster *) 710 &iot[id].d.eu->baSourceId[iot[id].d.eu->bNrInPins]; 711 return (r.bNrChannels); 712 default: 713 goto bad; 714 } 715 } 716 bad: 717 printf("uaudio_get_cluster_nchan: bad data\n"); 718 return (0); 719 } 720 721 void 722 uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 723 { 724 #ifdef UAUDIO_DEBUG 725 const struct usb_audio_input_terminal *d = iot[id].d.it; 726 727 DPRINTFN(2,("uaudio_add_input: bTerminalId=%d wTerminalType=0x%04x " 728 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d " 729 "iChannelNames=%d iTerminal=%d\n", 730 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 731 d->bNrChannels, UGETW(d->wChannelConfig), 732 d->iChannelNames, d->iTerminal)); 733 #endif 734 } 735 736 void 737 uaudio_add_output(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 738 { 739 #ifdef UAUDIO_DEBUG 740 const struct usb_audio_output_terminal *d = iot[id].d.ot; 741 742 DPRINTFN(2,("uaudio_add_output: bTerminalId=%d wTerminalType=0x%04x " 743 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n", 744 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 745 d->bSourceId, d->iTerminal)); 746 #endif 747 } 748 749 void 750 uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 751 { 752 const struct usb_audio_mixer_unit *d = iot[id].d.mu; 753 struct usb_audio_mixer_unit_1 *d1; 754 int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k; 755 uByte *bm; 756 struct mixerctl mix; 757 758 DPRINTFN(2,("uaudio_add_mixer: bUnitId=%d bNrInPins=%d\n", 759 d->bUnitId, d->bNrInPins)); 760 761 /* Compute the number of input channels */ 762 ichs = 0; 763 for (i = 0; i < d->bNrInPins; i++) 764 ichs += uaudio_get_cluster_nchan(d->baSourceId[i], iot); 765 766 /* and the number of output channels */ 767 d1 = (struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins]; 768 ochs = d1->bNrChannels; 769 DPRINTFN(2,("uaudio_add_mixer: ichs=%d ochs=%d\n", ichs, ochs)); 770 771 bm = d1->bmControls; 772 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 773 uaudio_determine_class(&iot[id], &mix); 774 mix.type = MIX_SIGNED_16; 775 mix.ctlunit = AudioNvolume; 776 #define BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1) 777 for (p = i = 0; i < d->bNrInPins; i++) { 778 chs = uaudio_get_cluster_nchan(d->baSourceId[i], iot); 779 mc = 0; 780 for (c = 0; c < chs; c++) { 781 mo = 0; 782 for (o = 0; o < ochs; o++) { 783 bno = (p + c) * ochs + o; 784 if (BIT(bno)) 785 mo++; 786 } 787 if (mo == 1) 788 mc++; 789 } 790 if (mc == chs && chs <= MIX_MAX_CHAN) { 791 k = 0; 792 for (c = 0; c < chs; c++) 793 for (o = 0; o < ochs; o++) { 794 bno = (p + c) * ochs + o; 795 if (BIT(bno)) 796 mix.wValue[k++] = 797 MAKE(p+c+1, o+1); 798 } 799 snprintf(mix.ctlname, sizeof(mix.ctlname), "mix%d-%s", 800 d->bUnitId, uaudio_id_name(sc, iot, 801 d->baSourceId[i])); 802 mix.nchan = chs; 803 uaudio_mixer_add_ctl(sc, &mix); 804 } else { 805 /* XXX */ 806 } 807 #undef BIT 808 p += chs; 809 } 810 811 } 812 813 void 814 uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 815 { 816 const struct usb_audio_selector_unit *d = iot[id].d.su; 817 struct mixerctl mix; 818 int i, wp; 819 820 DPRINTFN(2,("uaudio_add_selector: bUnitId=%d bNrInPins=%d\n", 821 d->bUnitId, d->bNrInPins)); 822 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 823 mix.wValue[0] = MAKE(0, 0); 824 uaudio_determine_class(&iot[id], &mix); 825 mix.nchan = 1; 826 mix.type = MIX_SELECTOR; 827 mix.ctlunit = ""; 828 mix.minval = 1; 829 mix.maxval = d->bNrInPins; 830 wp = snprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId); 831 for (i = 1; i <= d->bNrInPins; i++) { 832 wp += snprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp, 833 "i%d", d->baSourceId[i - 1]); 834 if (wp > MAX_AUDIO_DEV_LEN - 1) 835 break; 836 } 837 uaudio_mixer_add_ctl(sc, &mix); 838 } 839 840 #ifdef UAUDIO_DEBUG 841 const char * 842 uaudio_get_terminal_name(int terminal_type) 843 { 844 static char buf[100]; 845 846 switch (terminal_type) { 847 /* USB terminal types */ 848 case UAT_UNDEFINED: return "UAT_UNDEFINED"; 849 case UAT_STREAM: return "UAT_STREAM"; 850 case UAT_VENDOR: return "UAT_VENDOR"; 851 /* input terminal types */ 852 case UATI_UNDEFINED: return "UATI_UNDEFINED"; 853 case UATI_MICROPHONE: return "UATI_MICROPHONE"; 854 case UATI_DESKMICROPHONE: return "UATI_DESKMICROPHONE"; 855 case UATI_PERSONALMICROPHONE: return "UATI_PERSONALMICROPHONE"; 856 case UATI_OMNIMICROPHONE: return "UATI_OMNIMICROPHONE"; 857 case UATI_MICROPHONEARRAY: return "UATI_MICROPHONEARRAY"; 858 case UATI_PROCMICROPHONEARR: return "UATI_PROCMICROPHONEARR"; 859 /* output terminal types */ 860 case UATO_UNDEFINED: return "UATO_UNDEFINED"; 861 case UATO_SPEAKER: return "UATO_SPEAKER"; 862 case UATO_HEADPHONES: return "UATO_HEADPHONES"; 863 case UATO_DISPLAYAUDIO: return "UATO_DISPLAYAUDIO"; 864 case UATO_DESKTOPSPEAKER: return "UATO_DESKTOPSPEAKER"; 865 case UATO_ROOMSPEAKER: return "UATO_ROOMSPEAKER"; 866 case UATO_COMMSPEAKER: return "UATO_COMMSPEAKER"; 867 case UATO_SUBWOOFER: return "UATO_SUBWOOFER"; 868 /* bidir terminal types */ 869 case UATB_UNDEFINED: return "UATB_UNDEFINED"; 870 case UATB_HANDSET: return "UATB_HANDSET"; 871 case UATB_HEADSET: return "UATB_HEADSET"; 872 case UATB_SPEAKERPHONE: return "UATB_SPEAKERPHONE"; 873 case UATB_SPEAKERPHONEESUP: return "UATB_SPEAKERPHONEESUP"; 874 case UATB_SPEAKERPHONEECANC: return "UATB_SPEAKERPHONEECANC"; 875 /* telephony terminal types */ 876 case UATT_UNDEFINED: return "UATT_UNDEFINED"; 877 case UATT_PHONELINE: return "UATT_PHONELINE"; 878 case UATT_TELEPHONE: return "UATT_TELEPHONE"; 879 case UATT_DOWNLINEPHONE: return "UATT_DOWNLINEPHONE"; 880 /* external terminal types */ 881 case UATE_UNDEFINED: return "UATE_UNDEFINED"; 882 case UATE_ANALOGCONN: return "UATE_ANALOGCONN"; 883 case UATE_LINECONN: return "UATE_LINECONN"; 884 case UATE_LEGACYCONN: return "UATE_LEGACYCONN"; 885 case UATE_DIGITALAUIFC: return "UATE_DIGITALAUIFC"; 886 case UATE_SPDIF: return "UATE_SPDIF"; 887 case UATE_1394DA: return "UATE_1394DA"; 888 case UATE_1394DV: return "UATE_1394DV"; 889 /* embedded function terminal types */ 890 case UATF_UNDEFINED: return "UATF_UNDEFINED"; 891 case UATF_CALIBNOISE: return "UATF_CALIBNOISE"; 892 case UATF_EQUNOISE: return "UATF_EQUNOISE"; 893 case UATF_CDPLAYER: return "UATF_CDPLAYER"; 894 case UATF_DAT: return "UATF_DAT"; 895 case UATF_DCC: return "UATF_DCC"; 896 case UATF_MINIDISK: return "UATF_MINIDISK"; 897 case UATF_ANALOGTAPE: return "UATF_ANALOGTAPE"; 898 case UATF_PHONOGRAPH: return "UATF_PHONOGRAPH"; 899 case UATF_VCRAUDIO: return "UATF_VCRAUDIO"; 900 case UATF_VIDEODISCAUDIO: return "UATF_VIDEODISCAUDIO"; 901 case UATF_DVDAUDIO: return "UATF_DVDAUDIO"; 902 case UATF_TVTUNERAUDIO: return "UATF_TVTUNERAUDIO"; 903 case UATF_SATELLITE: return "UATF_SATELLITE"; 904 case UATF_CABLETUNER: return "UATF_CABLETUNER"; 905 case UATF_DSS: return "UATF_DSS"; 906 case UATF_RADIORECV: return "UATF_RADIORECV"; 907 case UATF_RADIOXMIT: return "UATF_RADIOXMIT"; 908 case UATF_MULTITRACK: return "UATF_MULTITRACK"; 909 case UATF_SYNTHESIZER: return "UATF_SYNTHESIZER"; 910 default: 911 snprintf(buf, sizeof(buf), "unknown type (0x%.4x)", terminal_type); 912 return buf; 913 } 914 } 915 #endif 916 917 int 918 uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix) 919 { 920 int terminal_type; 921 922 if (iot == NULL || iot->output == NULL) { 923 mix->class = UAC_OUTPUT; 924 return 0; 925 } 926 terminal_type = 0; 927 if (iot->output->size == 1) 928 terminal_type = iot->output->terminals[0]; 929 /* 930 * If the only output terminal is USB, 931 * the class is UAC_RECORD. 932 */ 933 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) { 934 mix->class = UAC_RECORD; 935 if (iot->inputs_size == 1 936 && iot->inputs[0] != NULL 937 && iot->inputs[0]->size == 1) 938 return iot->inputs[0]->terminals[0]; 939 else 940 return 0; 941 } 942 /* 943 * If the ultimate destination of the unit is just one output 944 * terminal and the unit is connected to the output terminal 945 * directly, the class is UAC_OUTPUT. 946 */ 947 if (terminal_type != 0 && iot->direct) { 948 mix->class = UAC_OUTPUT; 949 return terminal_type; 950 } 951 /* 952 * If the unit is connected to just one input terminal, 953 * the class is UAC_INPUT. 954 */ 955 if (iot->inputs_size == 1 && iot->inputs[0] != NULL 956 && iot->inputs[0]->size == 1) { 957 mix->class = UAC_INPUT; 958 return iot->inputs[0]->terminals[0]; 959 } 960 /* 961 * Otherwise, the class is UAC_OUTPUT. 962 */ 963 mix->class = UAC_OUTPUT; 964 return terminal_type; 965 } 966 967 const char * 968 uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix) 969 { 970 int terminal_type; 971 972 terminal_type = uaudio_determine_class(iot, mix); 973 if (mix->class == UAC_RECORD && terminal_type == 0) 974 return AudioNmixerout; 975 DPRINTF(("%s: terminal_type=%s\n", __func__, 976 uaudio_get_terminal_name(terminal_type))); 977 switch (terminal_type) { 978 case UAT_STREAM: 979 return AudioNdac; 980 981 case UATI_MICROPHONE: 982 case UATI_DESKMICROPHONE: 983 case UATI_PERSONALMICROPHONE: 984 case UATI_OMNIMICROPHONE: 985 case UATI_MICROPHONEARRAY: 986 case UATI_PROCMICROPHONEARR: 987 return AudioNmicrophone; 988 989 case UATO_SPEAKER: 990 case UATO_DESKTOPSPEAKER: 991 case UATO_ROOMSPEAKER: 992 case UATO_COMMSPEAKER: 993 return AudioNspeaker; 994 995 case UATO_HEADPHONES: 996 return AudioNheadphone; 997 998 case UATO_SUBWOOFER: 999 return AudioNlfe; 1000 1001 /* telephony terminal types */ 1002 case UATT_UNDEFINED: 1003 case UATT_PHONELINE: 1004 case UATT_TELEPHONE: 1005 case UATT_DOWNLINEPHONE: 1006 return "phone"; 1007 1008 case UATE_ANALOGCONN: 1009 case UATE_LINECONN: 1010 case UATE_LEGACYCONN: 1011 return AudioNline; 1012 1013 case UATE_DIGITALAUIFC: 1014 case UATE_SPDIF: 1015 case UATE_1394DA: 1016 case UATE_1394DV: 1017 return AudioNaux; 1018 1019 case UATF_CDPLAYER: 1020 return AudioNcd; 1021 1022 case UATF_SYNTHESIZER: 1023 return AudioNfmsynth; 1024 1025 case UATF_VIDEODISCAUDIO: 1026 case UATF_DVDAUDIO: 1027 case UATF_TVTUNERAUDIO: 1028 return AudioNvideo; 1029 1030 case UAT_UNDEFINED: 1031 case UAT_VENDOR: 1032 case UATI_UNDEFINED: 1033 /* output terminal types */ 1034 case UATO_UNDEFINED: 1035 case UATO_DISPLAYAUDIO: 1036 /* bidir terminal types */ 1037 case UATB_UNDEFINED: 1038 case UATB_HANDSET: 1039 case UATB_HEADSET: 1040 case UATB_SPEAKERPHONE: 1041 case UATB_SPEAKERPHONEESUP: 1042 case UATB_SPEAKERPHONEECANC: 1043 /* external terminal types */ 1044 case UATE_UNDEFINED: 1045 /* embedded function terminal types */ 1046 case UATF_UNDEFINED: 1047 case UATF_CALIBNOISE: 1048 case UATF_EQUNOISE: 1049 case UATF_DAT: 1050 case UATF_DCC: 1051 case UATF_MINIDISK: 1052 case UATF_ANALOGTAPE: 1053 case UATF_PHONOGRAPH: 1054 case UATF_VCRAUDIO: 1055 case UATF_SATELLITE: 1056 case UATF_CABLETUNER: 1057 case UATF_DSS: 1058 case UATF_RADIORECV: 1059 case UATF_RADIOXMIT: 1060 case UATF_MULTITRACK: 1061 case 0xffff: 1062 default: 1063 DPRINTF(("%s: 'master' for 0x%.4x\n", __func__, terminal_type)); 1064 return AudioNmaster; 1065 } 1066 } 1067 1068 void 1069 uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 1070 { 1071 const struct usb_audio_feature_unit *d = iot[id].d.fu; 1072 uByte *ctls = (uByte *)d->bmaControls; 1073 int ctlsize = d->bControlSize; 1074 int nchan = (d->bLength - 7) / ctlsize; 1075 u_int fumask, mmask, cmask; 1076 struct mixerctl mix; 1077 int chan, ctl, i, unit; 1078 const char *mixername; 1079 1080 #define GET(i) (ctls[(i)*ctlsize] | \ 1081 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0)) 1082 1083 mmask = GET(0); 1084 /* Figure out what we can control */ 1085 for (cmask = 0, chan = 1; chan < nchan; chan++) { 1086 DPRINTFN(9,("uaudio_add_feature: chan=%d mask=%x\n", 1087 chan, GET(chan))); 1088 cmask |= GET(chan); 1089 } 1090 1091 DPRINTFN(1,("uaudio_add_feature: bUnitId=%d, " 1092 "%d channels, mmask=0x%04x, cmask=0x%04x\n", 1093 d->bUnitId, nchan, mmask, cmask)); 1094 1095 if (nchan > MIX_MAX_CHAN) 1096 nchan = MIX_MAX_CHAN; 1097 unit = d->bUnitId; 1098 mix.wIndex = MAKE(unit, sc->sc_ac_iface); 1099 for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) { 1100 fumask = FU_MASK(ctl); 1101 DPRINTFN(4,("uaudio_add_feature: ctl=%d fumask=0x%04x\n", 1102 ctl, fumask)); 1103 if (mmask & fumask) { 1104 mix.nchan = 1; 1105 mix.wValue[0] = MAKE(ctl, 0); 1106 } else if (cmask & fumask) { 1107 mix.nchan = nchan - 1; 1108 for (i = 1; i < nchan; i++) { 1109 if (GET(i) & fumask) 1110 mix.wValue[i-1] = MAKE(ctl, i); 1111 else 1112 mix.wValue[i-1] = -1; 1113 } 1114 } else { 1115 continue; 1116 } 1117 #undef GET 1118 mixername = uaudio_feature_name(&iot[id], &mix); 1119 switch (ctl) { 1120 case MUTE_CONTROL: 1121 mix.type = MIX_ON_OFF; 1122 mix.ctlunit = ""; 1123 snprintf(mix.ctlname, sizeof(mix.ctlname), 1124 "%s.%s", mixername, AudioNmute); 1125 break; 1126 case VOLUME_CONTROL: 1127 mix.type = MIX_SIGNED_16; 1128 mix.ctlunit = AudioNvolume; 1129 strlcpy(mix.ctlname, mixername, sizeof(mix.ctlname)); 1130 break; 1131 case BASS_CONTROL: 1132 mix.type = MIX_SIGNED_8; 1133 mix.ctlunit = AudioNbass; 1134 snprintf(mix.ctlname, sizeof(mix.ctlname), 1135 "%s.%s", mixername, AudioNbass); 1136 break; 1137 case MID_CONTROL: 1138 mix.type = MIX_SIGNED_8; 1139 mix.ctlunit = AudioNmid; 1140 snprintf(mix.ctlname, sizeof(mix.ctlname), 1141 "%s.%s", mixername, AudioNmid); 1142 break; 1143 case TREBLE_CONTROL: 1144 mix.type = MIX_SIGNED_8; 1145 mix.ctlunit = AudioNtreble; 1146 snprintf(mix.ctlname, sizeof(mix.ctlname), 1147 "%s.%s", mixername, AudioNtreble); 1148 break; 1149 case GRAPHIC_EQUALIZER_CONTROL: 1150 continue; /* XXX don't add anything */ 1151 break; 1152 case AGC_CONTROL: 1153 mix.type = MIX_ON_OFF; 1154 mix.ctlunit = ""; 1155 snprintf(mix.ctlname, sizeof(mix.ctlname), "%s.%s", 1156 mixername, AudioNagc); 1157 break; 1158 case DELAY_CONTROL: 1159 mix.type = MIX_UNSIGNED_16; 1160 mix.ctlunit = "4 ms"; 1161 snprintf(mix.ctlname, sizeof(mix.ctlname), 1162 "%s.%s", mixername, AudioNdelay); 1163 break; 1164 case BASS_BOOST_CONTROL: 1165 mix.type = MIX_ON_OFF; 1166 mix.ctlunit = ""; 1167 snprintf(mix.ctlname, sizeof(mix.ctlname), 1168 "%s.%s", mixername, AudioNbassboost); 1169 break; 1170 case LOUDNESS_CONTROL: 1171 mix.type = MIX_ON_OFF; 1172 mix.ctlunit = ""; 1173 snprintf(mix.ctlname, sizeof(mix.ctlname), 1174 "%s.%s", mixername, AudioNloudness); 1175 break; 1176 } 1177 uaudio_mixer_add_ctl(sc, &mix); 1178 } 1179 } 1180 1181 void 1182 uaudio_add_processing_updown(struct uaudio_softc *sc, 1183 const struct io_terminal *iot, int id) 1184 { 1185 const struct usb_audio_processing_unit *d = iot[id].d.pu; 1186 const struct usb_audio_processing_unit_1 *d1 = 1187 (const struct usb_audio_processing_unit_1 *)&d->baSourceId[d->bNrInPins]; 1188 const struct usb_audio_processing_unit_updown *ud = 1189 (const struct usb_audio_processing_unit_updown *) 1190 &d1->bmControls[d1->bControlSize]; 1191 struct mixerctl mix; 1192 int i; 1193 1194 DPRINTFN(2,("uaudio_add_processing_updown: bUnitId=%d bNrModes=%d\n", 1195 d->bUnitId, ud->bNrModes)); 1196 1197 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) { 1198 DPRINTF(("uaudio_add_processing_updown: no mode select\n")); 1199 return; 1200 } 1201 1202 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 1203 mix.nchan = 1; 1204 mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0); 1205 uaudio_determine_class(&iot[id], &mix); 1206 mix.type = MIX_ON_OFF; /* XXX */ 1207 mix.ctlunit = ""; 1208 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId); 1209 1210 for (i = 0; i < ud->bNrModes; i++) { 1211 DPRINTFN(2,("uaudio_add_processing_updown: i=%d bm=0x%x\n", 1212 i, UGETW(ud->waModes[i]))); 1213 /* XXX */ 1214 } 1215 uaudio_mixer_add_ctl(sc, &mix); 1216 } 1217 1218 void 1219 uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 1220 { 1221 const struct usb_audio_processing_unit *d = iot[id].d.pu; 1222 const struct usb_audio_processing_unit_1 *d1 = 1223 (const struct usb_audio_processing_unit_1 *)&d->baSourceId[d->bNrInPins]; 1224 int ptype = UGETW(d->wProcessType); 1225 struct mixerctl mix; 1226 1227 DPRINTFN(2,("uaudio_add_processing: wProcessType=%d bUnitId=%d " 1228 "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins)); 1229 1230 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) { 1231 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 1232 mix.nchan = 1; 1233 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0); 1234 uaudio_determine_class(&iot[id], &mix); 1235 mix.type = MIX_ON_OFF; 1236 mix.ctlunit = ""; 1237 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable", 1238 d->bUnitId, ptype); 1239 uaudio_mixer_add_ctl(sc, &mix); 1240 } 1241 1242 switch(ptype) { 1243 case UPDOWNMIX_PROCESS: 1244 uaudio_add_processing_updown(sc, iot, id); 1245 break; 1246 case DOLBY_PROLOGIC_PROCESS: 1247 case P3D_STEREO_EXTENDER_PROCESS: 1248 case REVERBATION_PROCESS: 1249 case CHORUS_PROCESS: 1250 case DYN_RANGE_COMP_PROCESS: 1251 default: 1252 #ifdef UAUDIO_DEBUG 1253 printf("uaudio_add_processing: unit %d, type=%d not impl.\n", 1254 d->bUnitId, ptype); 1255 #endif 1256 break; 1257 } 1258 } 1259 1260 void 1261 uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id) 1262 { 1263 const struct usb_audio_extension_unit *d = iot[id].d.eu; 1264 const struct usb_audio_extension_unit_1 *d1 = 1265 (const struct usb_audio_extension_unit_1 *)&d->baSourceId[d->bNrInPins]; 1266 struct mixerctl mix; 1267 1268 DPRINTFN(2,("uaudio_add_extension: bUnitId=%d bNrInPins=%d\n", 1269 d->bUnitId, d->bNrInPins)); 1270 1271 if (sc->sc_quirks & UAUDIO_FLAG_NO_XU) 1272 return; 1273 1274 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) { 1275 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 1276 mix.nchan = 1; 1277 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0); 1278 uaudio_determine_class(&iot[id], &mix); 1279 mix.type = MIX_ON_OFF; 1280 mix.ctlunit = ""; 1281 snprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable", 1282 d->bUnitId); 1283 uaudio_mixer_add_ctl(sc, &mix); 1284 } 1285 } 1286 1287 struct terminal_list* 1288 uaudio_merge_terminal_list(const struct io_terminal *iot) 1289 { 1290 struct terminal_list *tml; 1291 uint16_t *ptm; 1292 int i, len; 1293 1294 len = 0; 1295 if (iot->inputs == NULL) 1296 return NULL; 1297 for (i = 0; i < iot->inputs_size; i++) { 1298 if (iot->inputs[i] != NULL) 1299 len += iot->inputs[i]->size; 1300 } 1301 tml = malloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT); 1302 if (tml == NULL) { 1303 printf("uaudio_merge_terminal_list: no memory\n"); 1304 return NULL; 1305 } 1306 tml->size = 0; 1307 ptm = tml->terminals; 1308 for (i = 0; i < iot->inputs_size; i++) { 1309 if (iot->inputs[i] == NULL) 1310 continue; 1311 if (iot->inputs[i]->size > len) 1312 break; 1313 memcpy(ptm, iot->inputs[i]->terminals, 1314 iot->inputs[i]->size * sizeof(uint16_t)); 1315 tml->size += iot->inputs[i]->size; 1316 ptm += iot->inputs[i]->size; 1317 len -= iot->inputs[i]->size; 1318 } 1319 return tml; 1320 } 1321 1322 struct terminal_list * 1323 uaudio_io_terminaltype(int outtype, struct io_terminal *iot, int id) 1324 { 1325 struct terminal_list *tml; 1326 struct io_terminal *it; 1327 int src_id, i; 1328 1329 it = &iot[id]; 1330 if (it->output != NULL) { 1331 /* already has outtype? */ 1332 for (i = 0; i < it->output->size; i++) 1333 if (it->output->terminals[i] == outtype) 1334 return uaudio_merge_terminal_list(it); 1335 tml = malloc(TERMINAL_LIST_SIZE(it->output->size + 1), 1336 M_TEMP, M_NOWAIT); 1337 if (tml == NULL) { 1338 printf("uaudio_io_terminaltype: no memory\n"); 1339 return uaudio_merge_terminal_list(it); 1340 } 1341 memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size)); 1342 tml->terminals[it->output->size] = outtype; 1343 tml->size++; 1344 free(it->output, M_TEMP, 0); 1345 it->output = tml; 1346 if (it->inputs != NULL) { 1347 for (i = 0; i < it->inputs_size; i++) 1348 if (it->inputs[i] != NULL) 1349 free(it->inputs[i], M_TEMP, 0); 1350 free(it->inputs, M_TEMP, 0); 1351 } 1352 it->inputs_size = 0; 1353 it->inputs = NULL; 1354 } else { /* end `iot[id] != NULL' */ 1355 it->inputs_size = 0; 1356 it->inputs = NULL; 1357 it->output = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT); 1358 if (it->output == NULL) { 1359 printf("uaudio_io_terminaltype: no memory\n"); 1360 return NULL; 1361 } 1362 it->output->terminals[0] = outtype; 1363 it->output->size = 1; 1364 it->direct = 0; 1365 } 1366 1367 switch (it->d.desc->bDescriptorSubtype) { 1368 case UDESCSUB_AC_INPUT: 1369 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1370 if (it->inputs == NULL) { 1371 printf("uaudio_io_terminaltype: no memory\n"); 1372 return NULL; 1373 } 1374 tml = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT); 1375 if (tml == NULL) { 1376 printf("uaudio_io_terminaltype: no memory\n"); 1377 free(it->inputs, M_TEMP, 0); 1378 it->inputs = NULL; 1379 return NULL; 1380 } 1381 it->inputs[0] = tml; 1382 tml->terminals[0] = UGETW(it->d.it->wTerminalType); 1383 tml->size = 1; 1384 it->inputs_size = 1; 1385 return uaudio_merge_terminal_list(it); 1386 case UDESCSUB_AC_FEATURE: 1387 src_id = it->d.fu->bSourceId; 1388 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1389 if (it->inputs == NULL) { 1390 printf("uaudio_io_terminaltype: no memory\n"); 1391 return uaudio_io_terminaltype(outtype, iot, src_id); 1392 } 1393 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id); 1394 it->inputs_size = 1; 1395 return uaudio_merge_terminal_list(it); 1396 case UDESCSUB_AC_OUTPUT: 1397 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1398 if (it->inputs == NULL) { 1399 printf("uaudio_io_terminaltype: no memory\n"); 1400 return NULL; 1401 } 1402 src_id = it->d.ot->bSourceId; 1403 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id); 1404 it->inputs_size = 1; 1405 iot[src_id].direct = 1; 1406 return NULL; 1407 case UDESCSUB_AC_MIXER: 1408 it->inputs_size = 0; 1409 it->inputs = mallocarray(it->d.mu->bNrInPins, 1410 sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1411 if (it->inputs == NULL) { 1412 printf("uaudio_io_terminaltype: no memory\n"); 1413 return NULL; 1414 } 1415 for (i = 0; i < it->d.mu->bNrInPins; i++) { 1416 src_id = it->d.mu->baSourceId[i]; 1417 it->inputs[i] = uaudio_io_terminaltype(outtype, iot, 1418 src_id); 1419 it->inputs_size++; 1420 } 1421 return uaudio_merge_terminal_list(it); 1422 case UDESCSUB_AC_SELECTOR: 1423 it->inputs_size = 0; 1424 it->inputs = mallocarray(it->d.su->bNrInPins, 1425 sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1426 if (it->inputs == NULL) { 1427 printf("uaudio_io_terminaltype: no memory\n"); 1428 return NULL; 1429 } 1430 for (i = 0; i < it->d.su->bNrInPins; i++) { 1431 src_id = it->d.su->baSourceId[i]; 1432 it->inputs[i] = uaudio_io_terminaltype(outtype, iot, 1433 src_id); 1434 it->inputs_size++; 1435 } 1436 return uaudio_merge_terminal_list(it); 1437 case UDESCSUB_AC_PROCESSING: 1438 it->inputs_size = 0; 1439 it->inputs = mallocarray(it->d.pu->bNrInPins, 1440 sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1441 if (it->inputs == NULL) { 1442 printf("uaudio_io_terminaltype: no memory\n"); 1443 return NULL; 1444 } 1445 for (i = 0; i < it->d.pu->bNrInPins; i++) { 1446 src_id = it->d.pu->baSourceId[i]; 1447 it->inputs[i] = uaudio_io_terminaltype(outtype, iot, 1448 src_id); 1449 it->inputs_size++; 1450 } 1451 return uaudio_merge_terminal_list(it); 1452 case UDESCSUB_AC_EXTENSION: 1453 it->inputs_size = 0; 1454 it->inputs = mallocarray(it->d.eu->bNrInPins, 1455 sizeof(struct terminal_list *), M_TEMP, M_NOWAIT); 1456 if (it->inputs == NULL) { 1457 printf("uaudio_io_terminaltype: no memory\n"); 1458 return NULL; 1459 } 1460 for (i = 0; i < it->d.eu->bNrInPins; i++) { 1461 src_id = it->d.eu->baSourceId[i]; 1462 it->inputs[i] = uaudio_io_terminaltype(outtype, iot, 1463 src_id); 1464 it->inputs_size++; 1465 } 1466 return uaudio_merge_terminal_list(it); 1467 case UDESCSUB_AC_HEADER: 1468 default: 1469 return NULL; 1470 } 1471 } 1472 1473 usbd_status 1474 uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc) 1475 { 1476 usbd_status err; 1477 1478 err = uaudio_identify_ac(sc, cdesc); 1479 if (err) 1480 return (err); 1481 return (uaudio_identify_as(sc, cdesc)); 1482 } 1483 1484 void 1485 uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai) 1486 { 1487 size_t len; 1488 struct as_info *nai; 1489 1490 nai = mallocarray(sc->sc_nalts + 1, sizeof(*ai), M_USBDEV, M_NOWAIT); 1491 if (nai == NULL) { 1492 printf("uaudio_add_alt: no memory\n"); 1493 return; 1494 } 1495 len = sizeof(*ai) * (sc->sc_nalts + 1); 1496 1497 /* Copy old data, if there was any */ 1498 if (sc->sc_nalts != 0) { 1499 bcopy(sc->sc_alts, nai, sizeof(*ai) * (sc->sc_nalts)); 1500 free(sc->sc_alts, M_USBDEV, 0); 1501 } 1502 sc->sc_alts = nai; 1503 DPRINTFN(2,("uaudio_add_alt: adding alt=%d, enc=%d\n", 1504 ai->alt, ai->encoding)); 1505 sc->sc_alts[sc->sc_nalts++] = *ai; 1506 } 1507 1508 usbd_status 1509 uaudio_process_as(struct uaudio_softc *sc, const char *buf, int *offsp, 1510 int size, const usb_interface_descriptor_t *id) 1511 #define offs (*offsp) 1512 { 1513 const struct usb_audio_streaming_interface_descriptor *asid; 1514 const struct usb_audio_streaming_type1_descriptor *asf1d; 1515 const struct usb_endpoint_descriptor_audio *ed; 1516 const struct usb_endpoint_descriptor_audio *sync_ed; 1517 const struct usb_audio_streaming_endpoint_descriptor *sed; 1518 int format, chan, prec, enc, bps; 1519 int dir, type, sync, sync_addr; 1520 struct as_info ai; 1521 const char *format_str; 1522 1523 asid = (const void *)(buf + offs); 1524 if (asid->bDescriptorType != UDESC_CS_INTERFACE || 1525 asid->bDescriptorSubtype != AS_GENERAL) 1526 return (USBD_INVAL); 1527 DPRINTF(("uaudio_process_as: asid: bTerminalLink=%d wFormatTag=%d\n", 1528 asid->bTerminalLink, UGETW(asid->wFormatTag))); 1529 offs += asid->bLength; 1530 if (offs > size) 1531 return (USBD_INVAL); 1532 1533 asf1d = (const void *)(buf + offs); 1534 if (asf1d->bDescriptorType != UDESC_CS_INTERFACE || 1535 asf1d->bDescriptorSubtype != FORMAT_TYPE) 1536 return (USBD_INVAL); 1537 offs += asf1d->bLength; 1538 if (offs > size) 1539 return (USBD_INVAL); 1540 1541 if (asf1d->bFormatType != FORMAT_TYPE_I) { 1542 printf("%s: ignored setting with type %d format\n", 1543 sc->sc_dev.dv_xname, UGETW(asid->wFormatTag)); 1544 return (USBD_NORMAL_COMPLETION); 1545 } 1546 1547 ed = (const void *)(buf + offs); 1548 if (ed->bDescriptorType != UDESC_ENDPOINT) 1549 return (USBD_INVAL); 1550 DPRINTF(("uaudio_process_as: endpoint[0] bLength=%d bDescriptorType=%d " 1551 "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d " 1552 "bInterval=%d bRefresh=%d bSynchAddress=%d\n", 1553 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress, 1554 ed->bmAttributes, UGETW(ed->wMaxPacketSize), 1555 ed->bInterval, ed->bRefresh, ed->bSynchAddress)); 1556 offs += ed->bLength; 1557 if (offs > size) 1558 return (USBD_INVAL); 1559 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS) 1560 return (USBD_INVAL); 1561 1562 dir = UE_GET_DIR(ed->bEndpointAddress); 1563 type = UE_GET_ISO_TYPE(ed->bmAttributes); 1564 1565 /* Check for sync endpoint. */ 1566 sync = 0; 1567 sync_addr = 0; 1568 if (id->bNumEndpoints > 1 && 1569 ((dir == UE_DIR_IN && type == UE_ISO_ADAPT) || 1570 (dir != UE_DIR_IN && type == UE_ISO_ASYNC))) 1571 sync = 1; 1572 1573 /* Check whether sync endpoint address is given. */ 1574 if (ed->bLength >= USB_ENDPOINT_DESCRIPTOR_AUDIO_SIZE) { 1575 /* bSynchAdress set to 0 indicates sync is not used. */ 1576 if (ed->bSynchAddress == 0) 1577 sync = 0; 1578 else 1579 sync_addr = ed->bSynchAddress; 1580 } 1581 1582 sed = (const void *)(buf + offs); 1583 if (sed->bDescriptorType != UDESC_CS_ENDPOINT || 1584 sed->bDescriptorSubtype != AS_GENERAL) 1585 return (USBD_INVAL); 1586 DPRINTF((" streaming_endpoint: offset=%d bLength=%d\n", offs, sed->bLength)); 1587 offs += sed->bLength; 1588 if (offs > size) 1589 return (USBD_INVAL); 1590 1591 sync_ed = NULL; 1592 if (sync == 1) { 1593 sync_ed = (const void*)(buf + offs); 1594 if (sync_ed->bDescriptorType != UDESC_ENDPOINT) { 1595 printf("%s: sync ep descriptor wrong type\n", 1596 sc->sc_dev.dv_xname); 1597 return (USBD_NORMAL_COMPLETION); 1598 } 1599 DPRINTF(("uaudio_process_as: endpoint[1] bLength=%d " 1600 "bDescriptorType=%d bEndpointAddress=%d " 1601 "bmAttributes=0x%x wMaxPacketSize=%d bInterval=%d " 1602 "bRefresh=%d bSynchAddress=%d\n", 1603 sync_ed->bLength, sync_ed->bDescriptorType, 1604 sync_ed->bEndpointAddress, sync_ed->bmAttributes, 1605 UGETW(sync_ed->wMaxPacketSize), sync_ed->bInterval, 1606 sync_ed->bRefresh, sync_ed->bSynchAddress)); 1607 offs += sync_ed->bLength; 1608 if (offs > size) { 1609 printf("%s: sync ep descriptor too large\n", 1610 sc->sc_dev.dv_xname); 1611 return (USBD_NORMAL_COMPLETION); 1612 } 1613 if (dir == UE_GET_DIR(sync_ed->bEndpointAddress)) { 1614 printf("%s: sync ep wrong direction\n", 1615 sc->sc_dev.dv_xname); 1616 return (USBD_NORMAL_COMPLETION); 1617 } 1618 if (UE_GET_XFERTYPE(sync_ed->bmAttributes) != UE_ISOCHRONOUS) { 1619 printf("%s: sync ep wrong xfer type\n", 1620 sc->sc_dev.dv_xname); 1621 return (USBD_NORMAL_COMPLETION); 1622 } 1623 if (sync_ed->bLength >= 1624 USB_ENDPOINT_DESCRIPTOR_AUDIO_SIZE && 1625 sync_ed->bSynchAddress != 0) { 1626 printf("%s: sync ep bSynchAddress != 0\n", 1627 sc->sc_dev.dv_xname); 1628 return (USBD_NORMAL_COMPLETION); 1629 } 1630 if (sync_addr && 1631 UE_GET_ADDR(sync_ed->bEndpointAddress) != 1632 UE_GET_ADDR(sync_addr)) { 1633 printf("%s: sync ep address mismatch\n", 1634 sc->sc_dev.dv_xname); 1635 return (USBD_NORMAL_COMPLETION); 1636 } 1637 } 1638 if (sync_ed != NULL && dir == UE_DIR_IN) { 1639 printf("%s: sync pipe for recording not yet implemented\n", 1640 sc->sc_dev.dv_xname); 1641 return (USBD_NORMAL_COMPLETION); 1642 } 1643 1644 format = UGETW(asid->wFormatTag); 1645 chan = asf1d->bNrChannels; 1646 prec = asf1d->bBitResolution; 1647 bps = asf1d->bSubFrameSize; 1648 if ((prec != 8 && prec != 16 && prec != 24) || (bps < 1 || bps > 4)) { 1649 printf("%s: ignored setting with precision %d bps %d\n", 1650 sc->sc_dev.dv_xname, prec, bps); 1651 return (USBD_NORMAL_COMPLETION); 1652 } 1653 switch (format) { 1654 case UA_FMT_PCM: 1655 if (prec == 8) { 1656 sc->sc_altflags |= HAS_8; 1657 } else if (prec == 16) { 1658 sc->sc_altflags |= HAS_16; 1659 } else if (prec == 24) { 1660 sc->sc_altflags |= HAS_24; 1661 } 1662 enc = AUDIO_ENCODING_SLINEAR_LE; 1663 format_str = "pcm"; 1664 break; 1665 case UA_FMT_PCM8: 1666 enc = AUDIO_ENCODING_ULINEAR_LE; 1667 sc->sc_altflags |= HAS_8U; 1668 format_str = "pcm8"; 1669 break; 1670 case UA_FMT_ALAW: 1671 enc = AUDIO_ENCODING_ALAW; 1672 sc->sc_altflags |= HAS_ALAW; 1673 format_str = "alaw"; 1674 break; 1675 case UA_FMT_MULAW: 1676 enc = AUDIO_ENCODING_ULAW; 1677 sc->sc_altflags |= HAS_MULAW; 1678 format_str = "mulaw"; 1679 break; 1680 case UA_FMT_IEEE_FLOAT: 1681 default: 1682 printf("%s: ignored setting with format %d\n", 1683 sc->sc_dev.dv_xname, format); 1684 return (USBD_NORMAL_COMPLETION); 1685 } 1686 #ifdef UAUDIO_DEBUG 1687 printf("%s: %s: %d-ch %d-bit %d-byte %s,", sc->sc_dev.dv_xname, 1688 dir == UE_DIR_IN ? "recording" : "playback", 1689 chan, prec, bps, format_str); 1690 if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) { 1691 printf(" %d-%dHz\n", UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d)); 1692 } else { 1693 int r; 1694 printf(" %d", UA_GETSAMP(asf1d, 0)); 1695 for (r = 1; r < asf1d->bSamFreqType; r++) 1696 printf(",%d", UA_GETSAMP(asf1d, r)); 1697 printf("Hz\n"); 1698 } 1699 #endif 1700 ai.alt = id->bAlternateSetting; 1701 ai.encoding = enc; 1702 ai.attributes = sed->bmAttributes; 1703 ai.idesc = id; 1704 ai.edesc = ed; 1705 ai.edesc1 = sync_ed; 1706 ai.asf1desc = asf1d; 1707 ai.sc_busy = 0; 1708 if (sc->sc_nalts < UAUDIO_MAX_ALTS) 1709 uaudio_add_alt(sc, &ai); 1710 #ifdef UAUDIO_DEBUG 1711 if (ai.attributes & UA_SED_FREQ_CONTROL) 1712 DPRINTFN(1, ("uaudio_process_as: FREQ_CONTROL\n")); 1713 if (ai.attributes & UA_SED_PITCH_CONTROL) 1714 DPRINTFN(1, ("uaudio_process_as: PITCH_CONTROL\n")); 1715 #endif 1716 sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD; 1717 1718 return (USBD_NORMAL_COMPLETION); 1719 } 1720 #undef offs 1721 1722 usbd_status 1723 uaudio_identify_as(struct uaudio_softc *sc, 1724 const usb_config_descriptor_t *cdesc) 1725 { 1726 const usb_interface_descriptor_t *id; 1727 const char *buf; 1728 int size, offs; 1729 1730 size = UGETW(cdesc->wTotalLength); 1731 buf = (const char *)cdesc; 1732 1733 /* Locate the AudioStreaming interface descriptor. */ 1734 offs = 0; 1735 id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOSTREAM, 1736 sc->sc_quirks); 1737 if (id == NULL) 1738 return (USBD_INVAL); 1739 1740 /* Loop through all the alternate settings. */ 1741 while (offs <= size) { 1742 DPRINTFN(2, ("uaudio_identify: interface=%d offset=%d\n", 1743 id->bInterfaceNumber, offs)); 1744 switch (id->bNumEndpoints) { 1745 case 0: 1746 DPRINTFN(2, ("uaudio_identify: AS null alt=%d\n", 1747 id->bAlternateSetting)); 1748 sc->sc_nullalt = id->bAlternateSetting; 1749 break; 1750 case 1: 1751 case 2: 1752 uaudio_process_as(sc, buf, &offs, size, id); 1753 break; 1754 default: 1755 printf("%s: ignored audio interface with %d " 1756 "endpoints\n", 1757 sc->sc_dev.dv_xname, id->bNumEndpoints); 1758 break; 1759 } 1760 id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOSTREAM, 1761 sc->sc_quirks); 1762 if (id == NULL) 1763 break; 1764 } 1765 if (offs > size) 1766 return (USBD_INVAL); 1767 DPRINTF(("uaudio_identify_as: %d alts available\n", sc->sc_nalts)); 1768 1769 if (sc->sc_mode == 0) { 1770 printf("%s: no usable endpoint found\n", 1771 sc->sc_dev.dv_xname); 1772 return (USBD_INVAL); 1773 } 1774 1775 return (USBD_NORMAL_COMPLETION); 1776 } 1777 1778 usbd_status 1779 uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc) 1780 { 1781 struct io_terminal* iot; 1782 const usb_interface_descriptor_t *id; 1783 const struct usb_audio_control_descriptor *acdp; 1784 const usb_descriptor_t *dp; 1785 const struct usb_audio_output_terminal *pot; 1786 struct terminal_list *tml; 1787 const char *buf, *ibuf, *ibufend; 1788 int size, offs, aclen, ndps, i, j; 1789 1790 size = UGETW(cdesc->wTotalLength); 1791 buf = (char *)cdesc; 1792 1793 /* Locate the AudioControl interface descriptor. */ 1794 offs = 0; 1795 id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOCONTROL, 1796 sc->sc_quirks); 1797 if (id == NULL) 1798 return (USBD_INVAL); 1799 if (offs + sizeof *acdp > size) 1800 return (USBD_INVAL); 1801 sc->sc_ac_iface = id->bInterfaceNumber; 1802 DPRINTFN(2,("uaudio_identify_ac: AC interface is %d\n", sc->sc_ac_iface)); 1803 1804 /* A class-specific AC interface header should follow. */ 1805 ibuf = buf + offs; 1806 acdp = (const struct usb_audio_control_descriptor *)ibuf; 1807 if (acdp->bDescriptorType != UDESC_CS_INTERFACE || 1808 acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER) 1809 return (USBD_INVAL); 1810 aclen = UGETW(acdp->wTotalLength); 1811 if (offs + aclen > size) 1812 return (USBD_INVAL); 1813 1814 if (!(sc->sc_quirks & UAUDIO_FLAG_BAD_ADC) && 1815 UGETW(acdp->bcdADC) != UAUDIO_VERSION) 1816 return (USBD_INVAL); 1817 1818 sc->sc_audio_rev = UGETW(acdp->bcdADC); 1819 DPRINTFN(2,("uaudio_identify_ac: found AC header, vers=%03x, len=%d\n", 1820 sc->sc_audio_rev, aclen)); 1821 1822 sc->sc_nullalt = -1; 1823 1824 /* Scan through all the AC specific descriptors */ 1825 ibufend = ibuf + aclen; 1826 dp = (const usb_descriptor_t *)ibuf; 1827 ndps = 0; 1828 iot = malloc(256 * sizeof(struct io_terminal), 1829 M_TEMP, M_NOWAIT | M_ZERO); 1830 if (iot == NULL) { 1831 printf("%s: no memory\n", __func__); 1832 return USBD_NOMEM; 1833 } 1834 for (;;) { 1835 ibuf += dp->bLength; 1836 if (ibuf >= ibufend) 1837 break; 1838 dp = (const usb_descriptor_t *)ibuf; 1839 if (ibuf + dp->bLength > ibufend) { 1840 free(iot, M_TEMP, 0); 1841 return (USBD_INVAL); 1842 } 1843 if (dp->bDescriptorType != UDESC_CS_INTERFACE) { 1844 printf("uaudio_identify_ac: skip desc type=0x%02x\n", 1845 dp->bDescriptorType); 1846 continue; 1847 } 1848 i = ((const struct usb_audio_input_terminal *)dp)->bTerminalId; 1849 iot[i].d.desc = dp; 1850 if (i > ndps) 1851 ndps = i; 1852 } 1853 ndps++; 1854 1855 /* construct io_terminal */ 1856 for (i = 0; i < ndps; i++) { 1857 dp = iot[i].d.desc; 1858 if (dp == NULL) 1859 continue; 1860 if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT) 1861 continue; 1862 pot = iot[i].d.ot; 1863 tml = uaudio_io_terminaltype(UGETW(pot->wTerminalType), iot, i); 1864 if (tml != NULL) 1865 free(tml, M_TEMP, 0); 1866 } 1867 1868 #ifdef UAUDIO_DEBUG 1869 for (i = 0; i < 256; i++) { 1870 if (iot[i].d.desc == NULL) 1871 continue; 1872 printf("id %d:\t", i); 1873 switch (iot[i].d.desc->bDescriptorSubtype) { 1874 case UDESCSUB_AC_INPUT: 1875 printf("AC_INPUT type=%s\n", uaudio_get_terminal_name 1876 (UGETW(iot[i].d.it->wTerminalType))); 1877 break; 1878 case UDESCSUB_AC_OUTPUT: 1879 printf("AC_OUTPUT type=%s ", uaudio_get_terminal_name 1880 (UGETW(iot[i].d.ot->wTerminalType))); 1881 printf("src=%d\n", iot[i].d.ot->bSourceId); 1882 break; 1883 case UDESCSUB_AC_MIXER: 1884 printf("AC_MIXER src="); 1885 for (j = 0; j < iot[i].d.mu->bNrInPins; j++) 1886 printf("%d ", iot[i].d.mu->baSourceId[j]); 1887 printf("\n"); 1888 break; 1889 case UDESCSUB_AC_SELECTOR: 1890 printf("AC_SELECTOR src="); 1891 for (j = 0; j < iot[i].d.su->bNrInPins; j++) 1892 printf("%d ", iot[i].d.su->baSourceId[j]); 1893 printf("\n"); 1894 break; 1895 case UDESCSUB_AC_FEATURE: 1896 printf("AC_FEATURE src=%d\n", iot[i].d.fu->bSourceId); 1897 break; 1898 case UDESCSUB_AC_PROCESSING: 1899 printf("AC_PROCESSING src="); 1900 for (j = 0; j < iot[i].d.pu->bNrInPins; j++) 1901 printf("%d ", iot[i].d.pu->baSourceId[j]); 1902 printf("\n"); 1903 break; 1904 case UDESCSUB_AC_EXTENSION: 1905 printf("AC_EXTENSION src="); 1906 for (j = 0; j < iot[i].d.eu->bNrInPins; j++) 1907 printf("%d ", iot[i].d.eu->baSourceId[j]); 1908 printf("\n"); 1909 break; 1910 default: 1911 printf("unknown audio control (subtype=%d)\n", 1912 iot[i].d.desc->bDescriptorSubtype); 1913 } 1914 for (j = 0; j < iot[i].inputs_size; j++) { 1915 int k; 1916 printf("\tinput%d: ", j); 1917 tml = iot[i].inputs[j]; 1918 if (tml == NULL) { 1919 printf("NULL\n"); 1920 continue; 1921 } 1922 for (k = 0; k < tml->size; k++) 1923 printf("%s ", uaudio_get_terminal_name 1924 (tml->terminals[k])); 1925 printf("\n"); 1926 } 1927 printf("\toutput: "); 1928 tml = iot[i].output; 1929 for (j = 0; j < tml->size; j++) 1930 printf("%s ", uaudio_get_terminal_name(tml->terminals[j])); 1931 printf("\n"); 1932 } 1933 #endif 1934 1935 for (i = 0; i < ndps; i++) { 1936 dp = iot[i].d.desc; 1937 if (dp == NULL) 1938 continue; 1939 DPRINTF(("uaudio_identify_ac: id=%d subtype=%d\n", 1940 i, dp->bDescriptorSubtype)); 1941 switch (dp->bDescriptorSubtype) { 1942 case UDESCSUB_AC_HEADER: 1943 printf("uaudio_identify_ac: unexpected AC header\n"); 1944 break; 1945 case UDESCSUB_AC_INPUT: 1946 uaudio_add_input(sc, iot, i); 1947 break; 1948 case UDESCSUB_AC_OUTPUT: 1949 uaudio_add_output(sc, iot, i); 1950 break; 1951 case UDESCSUB_AC_MIXER: 1952 uaudio_add_mixer(sc, iot, i); 1953 break; 1954 case UDESCSUB_AC_SELECTOR: 1955 uaudio_add_selector(sc, iot, i); 1956 break; 1957 case UDESCSUB_AC_FEATURE: 1958 uaudio_add_feature(sc, iot, i); 1959 break; 1960 case UDESCSUB_AC_PROCESSING: 1961 uaudio_add_processing(sc, iot, i); 1962 break; 1963 case UDESCSUB_AC_EXTENSION: 1964 uaudio_add_extension(sc, iot, i); 1965 break; 1966 default: 1967 printf("uaudio_identify_ac: bad AC desc subtype=0x%02x\n", 1968 dp->bDescriptorSubtype); 1969 break; 1970 } 1971 } 1972 1973 /* delete io_terminal */ 1974 for (i = 0; i < 256; i++) { 1975 if (iot[i].d.desc == NULL) 1976 continue; 1977 if (iot[i].inputs != NULL) { 1978 for (j = 0; j < iot[i].inputs_size; j++) { 1979 if (iot[i].inputs[j] != NULL) 1980 free(iot[i].inputs[j], M_TEMP, 0); 1981 } 1982 free(iot[i].inputs, M_TEMP, 0); 1983 } 1984 if (iot[i].output != NULL) 1985 free(iot[i].output, M_TEMP, 0); 1986 iot[i].d.desc = NULL; 1987 } 1988 free(iot, M_TEMP, 0); 1989 1990 return (USBD_NORMAL_COMPLETION); 1991 } 1992 1993 int 1994 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi) 1995 { 1996 struct uaudio_softc *sc = addr; 1997 struct mixerctl *mc; 1998 int n, nctls, i; 1999 2000 DPRINTFN(2,("uaudio_query_devinfo: index=%d\n", mi->index)); 2001 if (usbd_is_dying(sc->sc_udev)) 2002 return (EIO); 2003 2004 n = mi->index; 2005 nctls = sc->sc_nctls; 2006 2007 switch (n) { 2008 case UAC_OUTPUT: 2009 mi->type = AUDIO_MIXER_CLASS; 2010 mi->mixer_class = UAC_OUTPUT; 2011 mi->next = mi->prev = AUDIO_MIXER_LAST; 2012 strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name)); 2013 return (0); 2014 case UAC_INPUT: 2015 mi->type = AUDIO_MIXER_CLASS; 2016 mi->mixer_class = UAC_INPUT; 2017 mi->next = mi->prev = AUDIO_MIXER_LAST; 2018 strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name)); 2019 return (0); 2020 case UAC_EQUAL: 2021 mi->type = AUDIO_MIXER_CLASS; 2022 mi->mixer_class = UAC_EQUAL; 2023 mi->next = mi->prev = AUDIO_MIXER_LAST; 2024 strlcpy(mi->label.name, AudioCequalization, 2025 sizeof(mi->label.name)); 2026 return (0); 2027 case UAC_RECORD: 2028 mi->type = AUDIO_MIXER_CLASS; 2029 mi->mixer_class = UAC_RECORD; 2030 mi->next = mi->prev = AUDIO_MIXER_LAST; 2031 strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name)); 2032 return 0; 2033 default: 2034 break; 2035 } 2036 2037 n -= UAC_NCLASSES; 2038 if (n < 0 || n >= nctls) 2039 return (ENXIO); 2040 2041 mc = &sc->sc_ctls[n]; 2042 strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name)); 2043 mi->mixer_class = mc->class; 2044 mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */ 2045 switch (mc->type) { 2046 case MIX_ON_OFF: 2047 mi->type = AUDIO_MIXER_ENUM; 2048 mi->un.e.num_mem = 2; 2049 strlcpy(mi->un.e.member[0].label.name, AudioNoff, 2050 sizeof(mi->un.e.member[0].label.name)); 2051 mi->un.e.member[0].ord = 0; 2052 strlcpy(mi->un.e.member[1].label.name, AudioNon, 2053 sizeof(mi->un.e.member[1].label.name)); 2054 mi->un.e.member[1].ord = 1; 2055 break; 2056 case MIX_SELECTOR: 2057 mi->type = AUDIO_MIXER_ENUM; 2058 mi->un.e.num_mem = mc->maxval - mc->minval + 1; 2059 for (i = 0; i <= mc->maxval - mc->minval; i++) { 2060 snprintf(mi->un.e.member[i].label.name, 2061 sizeof(mi->un.e.member[i].label.name), 2062 "%d", i + mc->minval); 2063 mi->un.e.member[i].ord = i + mc->minval; 2064 } 2065 break; 2066 default: 2067 mi->type = AUDIO_MIXER_VALUE; 2068 strlcpy(mi->un.v.units.name, mc->ctlunit, 2069 sizeof(mi->un.v.units.name)); 2070 mi->un.v.num_channels = mc->nchan; 2071 mi->un.v.delta = mc->delta; 2072 break; 2073 } 2074 return (0); 2075 } 2076 2077 int 2078 uaudio_open(void *addr, int flags) 2079 { 2080 struct uaudio_softc *sc = addr; 2081 2082 DPRINTF(("uaudio_open: sc=%p\n", sc)); 2083 if (usbd_is_dying(sc->sc_udev)) 2084 return (EIO); 2085 2086 if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY)) 2087 return (ENXIO); 2088 if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD)) 2089 return (ENXIO); 2090 2091 return (0); 2092 } 2093 2094 /* 2095 * Close function is called at splaudio(). 2096 */ 2097 void 2098 uaudio_close(void *addr) 2099 { 2100 struct uaudio_softc *sc = addr; 2101 2102 if (sc->sc_playchan.altidx != -1) 2103 uaudio_chan_close(sc, &sc->sc_playchan); 2104 if (sc->sc_recchan.altidx != -1) 2105 uaudio_chan_close(sc, &sc->sc_recchan); 2106 } 2107 2108 int 2109 uaudio_drain(void *addr) 2110 { 2111 struct uaudio_softc *sc = addr; 2112 struct chan *pchan = &sc->sc_playchan; 2113 struct chan *rchan = &sc->sc_recchan; 2114 int ms = 0; 2115 2116 /* Wait for outstanding requests to complete. */ 2117 if (pchan->altidx != -1 && sc->sc_alts[pchan->altidx].sc_busy) 2118 ms = max(ms, pchan->reqms); 2119 if (rchan->altidx != -1 && sc->sc_alts[rchan->altidx].sc_busy) 2120 ms = max(ms, rchan->reqms); 2121 usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * ms); 2122 2123 return (0); 2124 } 2125 2126 int 2127 uaudio_halt_out_dma(void *addr) 2128 { 2129 struct uaudio_softc *sc = addr; 2130 2131 DPRINTF(("uaudio_halt_out_dma: enter\n")); 2132 if (sc->sc_playchan.pipe != NULL) { 2133 uaudio_chan_close(sc, &sc->sc_playchan); 2134 sc->sc_playchan.pipe = NULL; 2135 if (sc->sc_playchan.sync_pipe != NULL) 2136 sc->sc_playchan.sync_pipe = NULL; 2137 uaudio_chan_free_buffers(sc, &sc->sc_playchan); 2138 sc->sc_playchan.intr = NULL; 2139 } 2140 return (0); 2141 } 2142 2143 int 2144 uaudio_halt_in_dma(void *addr) 2145 { 2146 struct uaudio_softc *sc = addr; 2147 2148 DPRINTF(("uaudio_halt_in_dma: enter\n")); 2149 if (sc->sc_recchan.pipe != NULL) { 2150 uaudio_chan_close(sc, &sc->sc_recchan); 2151 sc->sc_recchan.pipe = NULL; 2152 if (sc->sc_recchan.sync_pipe != NULL) 2153 sc->sc_recchan.sync_pipe = NULL; 2154 uaudio_chan_free_buffers(sc, &sc->sc_recchan); 2155 sc->sc_recchan.intr = NULL; 2156 } 2157 return (0); 2158 } 2159 2160 /* 2161 * Make sure the block size is large enough to hold at least 1 transfer. 2162 * Ideally, the block size should be a multiple of the transfer size. 2163 * Currently, the transfer size for play and record can differ, and there's 2164 * no way to round playback and record blocksizes separately. 2165 */ 2166 int 2167 uaudio_round_blocksize(void *addr, int blk) 2168 { 2169 struct uaudio_softc *sc = addr; 2170 int bpf, pbpf, rbpf; 2171 2172 DPRINTF(("uaudio_round_blocksize: p.mbpf=%d r.mbpf=%d\n", 2173 sc->sc_playchan.max_bytes_per_frame, 2174 sc->sc_recchan.max_bytes_per_frame)); 2175 2176 pbpf = rbpf = 0; 2177 if (sc->sc_mode & AUMODE_PLAY) { 2178 pbpf = (sc->sc_playchan.max_bytes_per_frame) * 2179 sc->sc_playchan.nframes; 2180 } 2181 if (sc->sc_mode & AUMODE_RECORD) { 2182 rbpf = (sc->sc_recchan.max_bytes_per_frame) * 2183 sc->sc_recchan.nframes; 2184 } 2185 bpf = max(pbpf, rbpf); 2186 2187 if (blk < bpf) 2188 blk = bpf; 2189 2190 #ifdef DIAGNOSTIC 2191 if (blk <= 0) { 2192 printf("uaudio_round_blocksize: blk=%d\n", blk); 2193 blk = 512; 2194 } 2195 #endif 2196 2197 DPRINTFN(1,("uaudio_round_blocksize: blk=%d\n", blk)); 2198 return (blk); 2199 } 2200 2201 int 2202 uaudio_get_props(void *addr) 2203 { 2204 struct uaudio_softc *sc = addr; 2205 int props = 0; 2206 2207 if (!(sc->sc_quirks & UAUDIO_FLAG_DEPENDENT)) 2208 props |= AUDIO_PROP_INDEPENDENT; 2209 2210 if ((sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD)) == 2211 (AUMODE_PLAY | AUMODE_RECORD)) 2212 props |= AUDIO_PROP_FULLDUPLEX; 2213 2214 return props; 2215 } 2216 2217 int 2218 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue, 2219 int wIndex, int len) 2220 { 2221 usb_device_request_t req; 2222 u_int8_t data[4]; 2223 usbd_status err; 2224 int val; 2225 2226 if (wValue == -1) 2227 return (0); 2228 2229 req.bmRequestType = type; 2230 req.bRequest = which; 2231 USETW(req.wValue, wValue); 2232 USETW(req.wIndex, wIndex); 2233 USETW(req.wLength, len); 2234 DPRINTFN(2,("uaudio_get: type=0x%02x req=0x%02x wValue=0x%04x " 2235 "wIndex=0x%04x len=%d\n", 2236 type, which, wValue, wIndex, len)); 2237 err = usbd_do_request(sc->sc_udev, &req, data); 2238 if (err) { 2239 DPRINTF(("uaudio_get: err=%s\n", usbd_errstr(err))); 2240 return (-1); 2241 } 2242 switch (len) { 2243 case 1: 2244 val = data[0]; 2245 break; 2246 case 2: 2247 val = data[0] | (data[1] << 8); 2248 break; 2249 default: 2250 DPRINTF(("uaudio_get: bad length=%d\n", len)); 2251 return (-1); 2252 } 2253 DPRINTFN(2,("uaudio_get: val=%d\n", val)); 2254 return (val); 2255 } 2256 2257 void 2258 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue, 2259 int wIndex, int len, int val) 2260 { 2261 usb_device_request_t req; 2262 u_int8_t data[4]; 2263 usbd_status err; 2264 2265 if (wValue == -1) 2266 return; 2267 2268 req.bmRequestType = type; 2269 req.bRequest = which; 2270 USETW(req.wValue, wValue); 2271 USETW(req.wIndex, wIndex); 2272 USETW(req.wLength, len); 2273 switch (len) { 2274 case 1: 2275 data[0] = val; 2276 break; 2277 case 2: 2278 data[0] = val; 2279 data[1] = val >> 8; 2280 break; 2281 default: 2282 return; 2283 } 2284 DPRINTFN(2,("uaudio_set: type=0x%02x req=0x%02x wValue=0x%04x " 2285 "wIndex=0x%04x len=%d, val=%d\n", 2286 type, which, wValue, wIndex, len, val & 0xffff)); 2287 err = usbd_do_request(sc->sc_udev, &req, data); 2288 #ifdef UAUDIO_DEBUG 2289 if (err) 2290 DPRINTF(("uaudio_set: err=%d\n", err)); 2291 #endif 2292 } 2293 2294 int 2295 uaudio_signext(int type, int val) 2296 { 2297 if (!MIX_UNSIGNED(type)) { 2298 if (MIX_SIZE(type) == 2) 2299 val = (int16_t)val; 2300 else 2301 val = (int8_t)val; 2302 } 2303 return (val); 2304 } 2305 2306 int 2307 uaudio_unsignext(int type, int val) 2308 { 2309 if (!MIX_UNSIGNED(type)) { 2310 if (MIX_SIZE(type) == 2) 2311 val = (u_int16_t)val; 2312 else 2313 val = (u_int8_t)val; 2314 } 2315 return (val); 2316 } 2317 2318 int 2319 uaudio_value2bsd(struct mixerctl *mc, int val) 2320 { 2321 int range; 2322 DPRINTFN(5, ("uaudio_value2bsd: type=%03x val=%d min=%d max=%d ", 2323 mc->type, val, mc->minval, mc->maxval)); 2324 if (mc->type == MIX_ON_OFF) { 2325 val = (val != 0); 2326 } else if (mc->type == MIX_SELECTOR) { 2327 if (val < mc->minval || val > mc->maxval) 2328 val = mc->minval; 2329 } else { 2330 range = mc->maxval - mc->minval; 2331 if (range == 0) 2332 val = 0; 2333 else 2334 val = 255 * (uaudio_signext(mc->type, val) - 2335 mc->minval) / range; 2336 } 2337 DPRINTFN(5, ("val'=%d\n", val)); 2338 return (val); 2339 } 2340 2341 int 2342 uaudio_bsd2value(struct mixerctl *mc, int val) 2343 { 2344 DPRINTFN(5,("uaudio_bsd2value: type=%03x val=%d min=%d max=%d ", 2345 mc->type, val, mc->minval, mc->maxval)); 2346 if (mc->type == MIX_ON_OFF) { 2347 val = (val != 0); 2348 } else if (mc->type == MIX_SELECTOR) { 2349 if (val < mc->minval || val > mc->maxval) 2350 val = mc->minval; 2351 } else 2352 val = uaudio_unsignext(mc->type, 2353 val * (mc->maxval - mc->minval) / 255 + mc->minval); 2354 DPRINTFN(5, ("val'=%d\n", val)); 2355 return (val); 2356 } 2357 2358 int 2359 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc, 2360 int chan) 2361 { 2362 int val; 2363 2364 DPRINTFN(5,("uaudio_ctl_get: which=%d chan=%d\n", which, chan)); 2365 val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan], 2366 mc->wIndex, MIX_SIZE(mc->type)); 2367 return (uaudio_value2bsd(mc, val)); 2368 } 2369 2370 void 2371 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc, 2372 int chan, int val) 2373 { 2374 val = uaudio_bsd2value(mc, val); 2375 uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan], 2376 mc->wIndex, MIX_SIZE(mc->type), val); 2377 } 2378 2379 int 2380 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp) 2381 { 2382 struct uaudio_softc *sc = addr; 2383 struct mixerctl *mc; 2384 int i, n, vals[MIX_MAX_CHAN], val; 2385 2386 DPRINTFN(2,("uaudio_mixer_get_port: index=%d\n", cp->dev)); 2387 2388 if (usbd_is_dying(sc->sc_udev)) 2389 return (EIO); 2390 2391 n = cp->dev - UAC_NCLASSES; 2392 if (n < 0 || n >= sc->sc_nctls) 2393 return (ENXIO); 2394 mc = &sc->sc_ctls[n]; 2395 2396 if (mc->type == MIX_ON_OFF) { 2397 if (cp->type != AUDIO_MIXER_ENUM) 2398 return (EINVAL); 2399 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0); 2400 } else if (mc->type == MIX_SELECTOR) { 2401 if (cp->type != AUDIO_MIXER_ENUM) 2402 return (EINVAL); 2403 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0); 2404 } else { 2405 if (cp->type != AUDIO_MIXER_VALUE) 2406 return (EINVAL); 2407 if (cp->un.value.num_channels != 1 && 2408 cp->un.value.num_channels != mc->nchan) 2409 return (EINVAL); 2410 for (i = 0; i < mc->nchan; i++) 2411 vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i); 2412 if (cp->un.value.num_channels == 1 && mc->nchan != 1) { 2413 for (val = 0, i = 0; i < mc->nchan; i++) 2414 val += vals[i]; 2415 vals[0] = val / mc->nchan; 2416 } 2417 for (i = 0; i < cp->un.value.num_channels; i++) 2418 cp->un.value.level[i] = vals[i]; 2419 } 2420 2421 return (0); 2422 } 2423 2424 int 2425 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp) 2426 { 2427 struct uaudio_softc *sc = addr; 2428 struct mixerctl *mc; 2429 int i, n, vals[MIX_MAX_CHAN]; 2430 2431 DPRINTFN(2,("uaudio_mixer_set_port: index = %d\n", cp->dev)); 2432 if (usbd_is_dying(sc->sc_udev)) 2433 return (EIO); 2434 2435 n = cp->dev - UAC_NCLASSES; 2436 if (n < 0 || n >= sc->sc_nctls) 2437 return (ENXIO); 2438 mc = &sc->sc_ctls[n]; 2439 2440 if (mc->type == MIX_ON_OFF) { 2441 if (cp->type != AUDIO_MIXER_ENUM) 2442 return (EINVAL); 2443 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord); 2444 } else if (mc->type == MIX_SELECTOR) { 2445 if (cp->type != AUDIO_MIXER_ENUM) 2446 return (EINVAL); 2447 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord); 2448 } else { 2449 if (cp->type != AUDIO_MIXER_VALUE) 2450 return (EINVAL); 2451 if (cp->un.value.num_channels == 1) 2452 for (i = 0; i < mc->nchan; i++) 2453 vals[i] = cp->un.value.level[0]; 2454 else if (cp->un.value.num_channels == mc->nchan) 2455 for (i = 0; i < mc->nchan; i++) 2456 vals[i] = cp->un.value.level[i]; 2457 else 2458 return (EINVAL); 2459 for (i = 0; i < mc->nchan; i++) 2460 uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]); 2461 } 2462 return (0); 2463 } 2464 2465 int 2466 uaudio_trigger_input(void *addr, void *start, void *end, int blksize, 2467 void (*intr)(void *), void *arg, 2468 struct audio_params *param) 2469 { 2470 struct uaudio_softc *sc = addr; 2471 struct chan *ch = &sc->sc_recchan; 2472 usbd_status err; 2473 int i, s; 2474 2475 if (usbd_is_dying(sc->sc_udev)) 2476 return (EIO); 2477 2478 DPRINTFN(3,("uaudio_trigger_input: sc=%p start=%p end=%p " 2479 "blksize=%d\n", sc, start, end, blksize)); 2480 2481 uaudio_chan_set_param(ch, start, end, blksize); 2482 DPRINTFN(3,("uaudio_trigger_input: sample_size=%d bytes/frame=%d " 2483 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame, 2484 ch->fraction)); 2485 2486 err = uaudio_chan_alloc_buffers(sc, ch); 2487 if (err) 2488 return (EIO); 2489 2490 err = uaudio_chan_open(sc, ch); 2491 if (err) { 2492 uaudio_chan_free_buffers(sc, ch); 2493 return (EIO); 2494 } 2495 2496 ch->intr = intr; 2497 ch->arg = arg; 2498 2499 s = splusb(); 2500 for (i = 0; i < UAUDIO_NCHANBUFS; i++) 2501 uaudio_chan_rtransfer(ch); 2502 splx(s); 2503 2504 return (0); 2505 } 2506 2507 int 2508 uaudio_trigger_output(void *addr, void *start, void *end, int blksize, 2509 void (*intr)(void *), void *arg, 2510 struct audio_params *param) 2511 { 2512 struct uaudio_softc *sc = addr; 2513 struct chan *ch = &sc->sc_playchan; 2514 usbd_status err; 2515 int i, s; 2516 2517 if (usbd_is_dying(sc->sc_udev)) 2518 return (EIO); 2519 2520 DPRINTFN(3,("uaudio_trigger_output: sc=%p start=%p end=%p " 2521 "blksize=%d\n", sc, start, end, blksize)); 2522 2523 uaudio_chan_set_param(ch, start, end, blksize); 2524 DPRINTFN(3,("uaudio_trigger_output: sample_size=%d bytes/frame=%d " 2525 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame, 2526 ch->fraction)); 2527 2528 err = uaudio_chan_alloc_buffers(sc, ch); 2529 if (err) 2530 return (EIO); 2531 2532 err = uaudio_chan_open(sc, ch); 2533 if (err) { 2534 uaudio_chan_free_buffers(sc, ch); 2535 return (EIO); 2536 } 2537 2538 ch->intr = intr; 2539 ch->arg = arg; 2540 2541 s = splusb(); 2542 for (i = 0; i < UAUDIO_NCHANBUFS; i++) 2543 uaudio_chan_ptransfer(ch); 2544 if (ch->sync_pipe) { 2545 for (i = 0; i < UAUDIO_NSYNCBUFS; i++) 2546 uaudio_chan_psync_transfer(ch); 2547 } 2548 splx(s); 2549 2550 return (0); 2551 } 2552 2553 /* Set up a pipe for a channel. */ 2554 usbd_status 2555 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch) 2556 { 2557 struct as_info *as = &sc->sc_alts[ch->altidx]; 2558 int endpt = as->edesc->bEndpointAddress; 2559 usbd_status err; 2560 2561 DPRINTF(("uaudio_chan_open: endpt=0x%02x, speed=%d, alt=%d\n", 2562 endpt, ch->sample_rate, as->alt)); 2563 2564 /* Set alternate interface corresponding to the mode. */ 2565 err = usbd_set_interface(as->ifaceh, as->alt); 2566 if (err) { 2567 DPRINTF(("%s: usbd_set_interface failed\n", __func__)); 2568 return (err); 2569 } 2570 2571 /* 2572 * If just one sampling rate is supported, 2573 * no need to call uaudio_set_speed(). 2574 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request. 2575 */ 2576 if (as->asf1desc->bSamFreqType != 1) { 2577 err = uaudio_set_speed(sc, endpt, ch->sample_rate); 2578 if (err) 2579 DPRINTF(("uaudio_chan_open: set_speed failed err=%s\n", 2580 usbd_errstr(err))); 2581 } 2582 2583 if (sc->sc_quirks & UAUDIO_FLAG_EMU0202) 2584 uaudio_set_speed_emu0202(ch); 2585 2586 ch->pipe = 0; 2587 ch->sync_pipe = 0; 2588 DPRINTF(("uaudio_chan_open: create pipe to 0x%02x\n", endpt)); 2589 err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->pipe); 2590 if (err) { 2591 printf("%s: error creating pipe: err=%s endpt=0x%02x\n", 2592 __func__, usbd_errstr(err), endpt); 2593 return err; 2594 } 2595 if (as->edesc1 != NULL) { 2596 endpt = as->edesc1->bEndpointAddress; 2597 DPRINTF(("uaudio_chan_open: create sync-pipe to 0x%02x\n", endpt)); 2598 err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->sync_pipe); 2599 if (err) { 2600 printf("%s: error creating sync-pipe: err=%s endpt=0x%02x\n", 2601 __func__, usbd_errstr(err), endpt); 2602 } 2603 } 2604 return err; 2605 } 2606 2607 void 2608 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch) 2609 { 2610 struct as_info *as = &sc->sc_alts[ch->altidx]; 2611 2612 as->sc_busy = 0; 2613 if (sc->sc_nullalt >= 0) { 2614 DPRINTF(("uaudio_chan_close: set null alt=%d\n", 2615 sc->sc_nullalt)); 2616 usbd_set_interface(as->ifaceh, sc->sc_nullalt); 2617 } 2618 if (ch->pipe) { 2619 usbd_abort_pipe(ch->pipe); 2620 usbd_close_pipe(ch->pipe); 2621 } 2622 if (ch->sync_pipe) { 2623 usbd_abort_pipe(ch->sync_pipe); 2624 usbd_close_pipe(ch->sync_pipe); 2625 } 2626 } 2627 2628 usbd_status 2629 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch) 2630 { 2631 struct as_info *as = &sc->sc_alts[ch->altidx]; 2632 struct usbd_xfer *xfer; 2633 void *buf; 2634 int i, size; 2635 2636 DPRINTF(("%s: max_bytes_per_frame=%d nframes=%d\n", __func__, 2637 ch->max_bytes_per_frame, ch->nframes)); 2638 2639 size = ch->max_bytes_per_frame * ch->nframes; 2640 for (i = 0; i < UAUDIO_NCHANBUFS; i++) { 2641 xfer = usbd_alloc_xfer(sc->sc_udev); 2642 if (xfer == 0) 2643 goto bad; 2644 ch->chanbufs[i].xfer = xfer; 2645 buf = usbd_alloc_buffer(xfer, size); 2646 if (buf == 0) { 2647 i++; 2648 goto bad; 2649 } 2650 ch->chanbufs[i].buffer = buf; 2651 ch->chanbufs[i].chan = ch; 2652 } 2653 if (as->edesc1 != NULL) { 2654 size = (ch->hi_speed ? 4 : 3) * ch->nsync_frames; 2655 for (i = 0; i < UAUDIO_NSYNCBUFS; i++) { 2656 xfer = usbd_alloc_xfer(sc->sc_udev); 2657 if (xfer == 0) 2658 goto bad_sync; 2659 ch->syncbufs[i].xfer = xfer; 2660 buf = usbd_alloc_buffer(xfer, size); 2661 if (buf == 0) { 2662 i++; 2663 goto bad_sync; 2664 } 2665 ch->syncbufs[i].buffer = buf; 2666 ch->syncbufs[i].chan = ch; 2667 } 2668 } 2669 2670 return (USBD_NORMAL_COMPLETION); 2671 2672 bad: 2673 while (--i >= 0) 2674 /* implicit buffer free */ 2675 usbd_free_xfer(ch->chanbufs[i].xfer); 2676 return (USBD_NOMEM); 2677 2678 bad_sync: 2679 while (--i >= 0) 2680 /* implicit buffer free */ 2681 usbd_free_xfer(ch->syncbufs[i].xfer); 2682 return (USBD_NOMEM); 2683 2684 } 2685 2686 void 2687 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch) 2688 { 2689 struct as_info *as = &sc->sc_alts[ch->altidx]; 2690 int i; 2691 2692 for (i = 0; i < UAUDIO_NCHANBUFS; i++) 2693 usbd_free_xfer(ch->chanbufs[i].xfer); 2694 if (as->edesc1 != NULL) { 2695 for (i = 0; i < UAUDIO_NSYNCBUFS; i++) 2696 usbd_free_xfer(ch->syncbufs[i].xfer); 2697 } 2698 } 2699 2700 /* Called at splusb() */ 2701 void 2702 uaudio_chan_ptransfer(struct chan *ch) 2703 { 2704 struct chanbuf *cb; 2705 u_char *pos; 2706 int i, n, size, residue, total; 2707 2708 if (usbd_is_dying(ch->sc->sc_udev)) 2709 return; 2710 2711 /* Pick the next channel buffer. */ 2712 cb = &ch->chanbufs[ch->curchanbuf]; 2713 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS) 2714 ch->curchanbuf = 0; 2715 2716 /* Compute the size of each frame in the next transfer. */ 2717 residue = ch->residue; 2718 total = 0; 2719 for (i = 0; i < ch->nframes; i++) { 2720 size = ch->bytes_per_frame; 2721 residue += ch->fraction; 2722 if (residue >= ch->frac_denom) { 2723 if ((ch->sc->sc_altflags & UA_NOFRAC) == 0) 2724 size += ch->sample_size; 2725 residue -= ch->frac_denom; 2726 } 2727 cb->sizes[i] = size; 2728 total += size; 2729 } 2730 ch->residue = residue; 2731 cb->size = total; 2732 2733 /* 2734 * Transfer data from upper layer buffer to channel buffer. Be sure 2735 * to let the upper layer know each time a block is moved, so it can 2736 * add more. 2737 */ 2738 pos = cb->buffer; 2739 while (total > 0) { 2740 n = min(total, ch->end - ch->cur); 2741 n = min(n, ch->blksize - ch->transferred); 2742 memcpy(pos, ch->cur, n); 2743 total -= n; 2744 pos += n; 2745 ch->cur += n; 2746 if (ch->cur >= ch->end) 2747 ch->cur = ch->start; 2748 2749 ch->transferred += n; 2750 /* Call back to upper layer */ 2751 if (ch->transferred >= ch->blksize) { 2752 DPRINTFN(5,("uaudio_chan_ptransfer: call %p(%p)\n", 2753 ch->intr, ch->arg)); 2754 mtx_enter(&audio_lock); 2755 ch->intr(ch->arg); 2756 mtx_leave(&audio_lock); 2757 ch->transferred -= ch->blksize; 2758 } 2759 } 2760 2761 #ifdef UAUDIO_DEBUG 2762 if (uaudiodebug > 8) { 2763 DPRINTF(("uaudio_chan_ptransfer: buffer=%p, residue=0.%03d\n", 2764 cb->buffer, ch->residue)); 2765 for (i = 0; i < ch->nframes; i++) { 2766 DPRINTF((" [%d] length %d\n", i, cb->sizes[i])); 2767 } 2768 } 2769 #endif 2770 2771 DPRINTFN(5,("uaudio_chan_ptransfer: transfer xfer=%p\n", cb->xfer)); 2772 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes, ch->nframes, 2773 USBD_NO_COPY | USBD_SHORT_XFER_OK, uaudio_chan_pintr); 2774 2775 (void)usbd_transfer(cb->xfer); 2776 } 2777 2778 void 2779 uaudio_chan_pintr(struct usbd_xfer *xfer, void *priv, 2780 usbd_status status) 2781 { 2782 struct chanbuf *cb = priv; 2783 struct chan *ch = cb->chan; 2784 u_int32_t count; 2785 2786 /* Return if we are aborting. */ 2787 if (status == USBD_CANCELLED) 2788 return; 2789 2790 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 2791 DPRINTFN(5,("uaudio_chan_pintr: count=%d, transferred=%d\n", 2792 count, ch->transferred)); 2793 #ifdef UAUDIO_DEBUG 2794 if (count != cb->size) { 2795 printf("uaudio_chan_pintr: count(%d) != size(%d)\n", 2796 count, cb->size); 2797 } 2798 #endif 2799 2800 /* start next transfer */ 2801 uaudio_chan_ptransfer(ch); 2802 } 2803 2804 /* Called at splusb() */ 2805 void 2806 uaudio_chan_psync_transfer(struct chan *ch) 2807 { 2808 struct syncbuf *sb; 2809 int i, size, total = 0; 2810 2811 if (usbd_is_dying(ch->sc->sc_udev)) 2812 return; 2813 2814 /* Pick the next sync buffer. */ 2815 sb = &ch->syncbufs[ch->cursyncbuf]; 2816 if (++ch->cursyncbuf >= UAUDIO_NSYNCBUFS) 2817 ch->cursyncbuf = 0; 2818 2819 size = ch->hi_speed ? 4 : 3; 2820 for (i = 0; i < ch->nsync_frames; i++) { 2821 sb->sizes[i] = size; 2822 sb->offsets[i] = total; 2823 total += size; 2824 } 2825 sb->size = total; 2826 2827 DPRINTFN(5,("%s: transfer xfer=%p\n", __func__, sb->xfer)); 2828 usbd_setup_isoc_xfer(sb->xfer, ch->sync_pipe, sb, sb->sizes, 2829 ch->nsync_frames, USBD_NO_COPY | USBD_SHORT_XFER_OK, 2830 uaudio_chan_psync_intr); 2831 2832 (void)usbd_transfer(sb->xfer); 2833 } 2834 2835 void 2836 uaudio_chan_psync_intr(struct usbd_xfer *xfer, void *priv, 2837 usbd_status status) 2838 { 2839 struct syncbuf *sb = priv; 2840 struct chan *ch = sb->chan; 2841 u_int32_t count, tmp; 2842 u_int32_t freq, freq_w, freq_f; 2843 int i, pos, size; 2844 2845 /* Return if we are aborting. */ 2846 if (status == USBD_CANCELLED) 2847 return; 2848 2849 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 2850 DPRINTFN(5,("%s: count=%d\n", __func__, count)); 2851 2852 size = ch->hi_speed ? 4 : 3; 2853 for (i = 0; count > 0 && i < ch->nsync_frames; i++) { 2854 if (sb->sizes[i] != size) 2855 continue; 2856 count -= size; 2857 pos = sb->offsets[i]; 2858 if (ch->hi_speed) { 2859 /* 16.16 (12.13) -> 16.16 (12.16) */ 2860 freq = sb->buffer[pos+3] << 24 | 2861 sb->buffer[pos+2] << 16 | 2862 sb->buffer[pos+1] << 8 | 2863 sb->buffer[pos]; 2864 } else { 2865 /* 10.14 (10.10) -> 16.16 (10.16) */ 2866 freq = sb->buffer[pos+2] << 18 | 2867 sb->buffer[pos+1] << 10 | 2868 sb->buffer[pos] << 2; 2869 } 2870 freq_w = (freq >> 16) & (ch->hi_speed ? 0x0fff : 0x03ff); 2871 freq_f = freq & 0xffff; 2872 DPRINTFN(5,("%s: freq = %d %d/%d\n", __func__, freq_w, freq_f, 2873 ch->frac_denom)); 2874 tmp = freq_w * ch->sample_size; 2875 if (tmp + (freq_f ? ch->sample_size : 0) > 2876 ch->max_bytes_per_frame) { 2877 DPRINTF(("%s: packet size request too large: %d/%d/%d\n", 2878 __func__, tmp, ch->max_bytes_per_frame, ch->maxpktsize)); 2879 } else { 2880 ch->bytes_per_frame = tmp; 2881 ch->fraction = freq_f; 2882 } 2883 } 2884 2885 /* start next transfer */ 2886 uaudio_chan_psync_transfer(ch); 2887 } 2888 2889 /* Called at splusb() */ 2890 void 2891 uaudio_chan_rtransfer(struct chan *ch) 2892 { 2893 struct chanbuf *cb; 2894 int i, size, residue, total; 2895 2896 if (usbd_is_dying(ch->sc->sc_udev)) 2897 return; 2898 2899 /* Pick the next channel buffer. */ 2900 cb = &ch->chanbufs[ch->curchanbuf]; 2901 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS) 2902 ch->curchanbuf = 0; 2903 2904 /* Compute the size of each frame in the next transfer. */ 2905 residue = ch->residue; 2906 total = 0; 2907 for (i = 0; i < ch->nframes; i++) { 2908 size = ch->bytes_per_frame; 2909 cb->sizes[i] = size; 2910 cb->offsets[i] = total; 2911 total += size; 2912 } 2913 ch->residue = residue; 2914 cb->size = total; 2915 2916 #ifdef UAUDIO_DEBUG 2917 if (uaudiodebug > 8) { 2918 DPRINTF(("uaudio_chan_rtransfer: buffer=%p, residue=0.%03d\n", 2919 cb->buffer, ch->residue)); 2920 for (i = 0; i < ch->nframes; i++) { 2921 DPRINTF((" [%d] length %d\n", i, cb->sizes[i])); 2922 } 2923 } 2924 #endif 2925 2926 DPRINTFN(5,("uaudio_chan_rtransfer: transfer xfer=%p\n", cb->xfer)); 2927 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes, ch->nframes, 2928 USBD_NO_COPY | USBD_SHORT_XFER_OK, uaudio_chan_rintr); 2929 2930 (void)usbd_transfer(cb->xfer); 2931 } 2932 2933 void 2934 uaudio_chan_rintr(struct usbd_xfer *xfer, void *priv, 2935 usbd_status status) 2936 { 2937 struct chanbuf *cb = priv; 2938 struct chan *ch = cb->chan; 2939 u_int16_t pos; 2940 u_int32_t count; 2941 int i, n, frsize; 2942 2943 /* Return if we are aborting. */ 2944 if (status == USBD_CANCELLED) 2945 return; 2946 2947 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 2948 DPRINTFN(5,("uaudio_chan_rintr: count=%d, transferred=%d\n", 2949 count, ch->transferred)); 2950 2951 /* count < cb->size is normal for asynchronous source */ 2952 #ifdef DIAGNOSTIC 2953 if (count > cb->size) { 2954 printf("uaudio_chan_rintr: count(%d) > size(%d)\n", 2955 count, cb->size); 2956 } 2957 #endif 2958 2959 /* 2960 * Transfer data from channel buffer to upper layer buffer, taking 2961 * care of wrapping the upper layer buffer. 2962 */ 2963 for (i = 0; i < ch->nframes; i++) { 2964 frsize = cb->sizes[i]; 2965 pos = cb->offsets[i]; 2966 while (frsize > 0) { 2967 n = min(frsize, ch->end - ch->cur); 2968 n = min(n, ch->blksize - ch->transferred); 2969 memcpy(ch->cur, cb->buffer + pos, n); 2970 frsize -= n; 2971 pos += n; 2972 ch->cur += n; 2973 if (ch->cur >= ch->end) 2974 ch->cur = ch->start; 2975 2976 ch->transferred += n; 2977 /* Call back to upper layer */ 2978 if (ch->transferred >= ch->blksize) { 2979 DPRINTFN(5,("uaudio_chan_rintr: call %p(%p)\n", 2980 ch->intr, ch->arg)); 2981 mtx_enter(&audio_lock); 2982 ch->intr(ch->arg); 2983 mtx_leave(&audio_lock); 2984 ch->transferred -= ch->blksize; 2985 } 2986 if (count < n) 2987 printf("%s: count < n\n", __func__); 2988 else 2989 count -= n; 2990 } 2991 } 2992 if (count != 0) { 2993 printf("%s: transfer count - frame total = %d\n", 2994 __func__, count); 2995 } 2996 2997 /* start next transfer */ 2998 uaudio_chan_rtransfer(ch); 2999 } 3000 3001 void 3002 uaudio_chan_init(struct chan *ch, int mode, int altidx, 3003 const struct audio_params *param) 3004 { 3005 struct as_info *ai = &ch->sc->sc_alts[altidx]; 3006 int samples_per_frame, ival, use_maxpkt = 0; 3007 3008 if (ai->attributes & UA_SED_MAXPACKETSONLY) { 3009 DPRINTF(("%s: alt %d needs maxpktsize packets\n", 3010 __func__, altidx)); 3011 use_maxpkt = 1; 3012 } 3013 if (mode == AUMODE_RECORD) { 3014 DPRINTF(("%s: using maxpktsize packets for record channel\n", 3015 __func__)); 3016 use_maxpkt = 1; 3017 } 3018 3019 ch->altidx = altidx; 3020 ch->maxpktsize = UGETW(ai->edesc->wMaxPacketSize); 3021 ch->sample_rate = param->sample_rate; 3022 ch->sample_size = param->channels * param->bps; 3023 ch->usb_fps = USB_FRAMES_PER_SECOND; 3024 ch->hi_speed = ch->sc->sc_udev->speed == USB_SPEED_HIGH; 3025 if (ch->hi_speed) { 3026 ch->usb_fps *= 8; 3027 /* 3028 * Polling interval is considered a frame, as opposed to 3029 * micro-frame being a frame. 3030 */ 3031 ival = ch->sc->sc_alts[altidx].edesc->bInterval; 3032 if (ival > 0 && ival <= 4) 3033 ch->usb_fps >>= (ival - 1); 3034 DPRINTF(("%s: detected USB high-speed with ival %d\n", 3035 __func__, ival)); 3036 } 3037 3038 /* 3039 * Use UAUDIO_MIN_FRAMES here, so uaudio_round_blocksize() can 3040 * make sure the blocksize duration will be > 1 USB frame. 3041 */ 3042 samples_per_frame = ch->sample_rate / ch->usb_fps; 3043 if (!use_maxpkt) { 3044 ch->fraction = ch->sample_rate % ch->usb_fps; 3045 if (samples_per_frame * ch->sample_size > ch->maxpktsize) { 3046 DPRINTF(("%s: packet size %d too big, max %d\n", 3047 __func__, ch->bytes_per_frame, ch->maxpktsize)); 3048 samples_per_frame = ch->maxpktsize / ch->sample_size; 3049 } 3050 ch->bytes_per_frame = samples_per_frame * ch->sample_size; 3051 ch->nframes = UAUDIO_MIN_FRAMES; 3052 } else { 3053 ch->fraction = 0; 3054 ch->bytes_per_frame = ch->maxpktsize; 3055 ch->nframes = UAUDIO_MIN_FRAMES * samples_per_frame * 3056 ch->sample_size / ch->maxpktsize; 3057 } 3058 if (ch->nframes > UAUDIO_MAX_FRAMES) 3059 ch->nframes = UAUDIO_MAX_FRAMES; 3060 else if (ch->nframes < 1) 3061 ch->nframes = 1; 3062 3063 ch->max_bytes_per_frame = ch->bytes_per_frame; 3064 if (!use_maxpkt) 3065 ch->max_bytes_per_frame += ch->sample_size; 3066 if (ch->max_bytes_per_frame > ch->maxpktsize) 3067 ch->max_bytes_per_frame = ch->maxpktsize; 3068 3069 ch->residue = 0; 3070 ch->frac_denom = ch->usb_fps; 3071 if (ai->edesc1 != NULL) { 3072 /* 3073 * The lower 16-bits of the sync request represent 3074 * fractional samples. Scale up the fraction here once 3075 * so all fractions are using the same denominator. 3076 */ 3077 ch->frac_denom = 1 << 16; 3078 ch->fraction = (ch->fraction * ch->frac_denom) / ch->usb_fps; 3079 3080 /* 3081 * Have to set nsync_frames somewhere. We can request 3082 * a lot of sync data; the device will reply when it's 3083 * ready, with empty frames meaning to keep using the 3084 * current rate. 3085 */ 3086 ch->nsync_frames = UAUDIO_MAX_FRAMES; 3087 } 3088 DPRINTF(("%s: residual sample fraction: %d/%d\n", __func__, 3089 ch->fraction, ch->frac_denom)); 3090 } 3091 3092 void 3093 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize) 3094 { 3095 ch->start = start; 3096 ch->end = end; 3097 ch->cur = start; 3098 ch->transferred = 0; 3099 ch->curchanbuf = 0; 3100 ch->blksize = blksize; 3101 3102 /* 3103 * Recompute nframes based on blksize, but make sure nframes 3104 * is not longer in time duration than blksize. 3105 */ 3106 ch->nframes = ch->blksize * ch->usb_fps / 3107 (ch->bytes_per_frame * ch->usb_fps + 3108 ch->sample_size * ch->fraction); 3109 if (ch->nframes > UAUDIO_MAX_FRAMES) 3110 ch->nframes = UAUDIO_MAX_FRAMES; 3111 else if (ch->nframes < 1) 3112 ch->nframes = 1; 3113 3114 ch->reqms = ch->bytes_per_frame / ch->sample_size * 3115 ch->nframes * 1000 / ch->sample_rate; 3116 3117 DPRINTF(("%s: alt=%d blk=%d maxpkt=%u bpf=%u rate=%u nframes=%u reqms=%u\n", 3118 __func__, ch->altidx, ch->blksize, ch->maxpktsize, 3119 ch->bytes_per_frame, ch->sample_rate, ch->nframes, ch->reqms)); 3120 } 3121 3122 int 3123 uaudio_match_alt_rate(void *addr, int alt, int rate) 3124 { 3125 struct uaudio_softc *sc = addr; 3126 const struct usb_audio_streaming_type1_descriptor *a1d; 3127 int i, j, r; 3128 3129 a1d = sc->sc_alts[alt].asf1desc; 3130 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) { 3131 if ((UA_SAMP_LO(a1d) <= rate) && 3132 (UA_SAMP_HI(a1d) >= rate)) { 3133 return rate; 3134 } else { 3135 if (UA_SAMP_LO(a1d) > rate) 3136 return UA_SAMP_LO(a1d); 3137 else 3138 return UA_SAMP_HI(a1d); 3139 } 3140 } else { 3141 for (i = 0; i < 100; i++) { 3142 for (j = 0; j < a1d->bSamFreqType; j++) { 3143 r = UA_GETSAMP(a1d, j); 3144 if ((r - (500 * i) <= rate) && 3145 (r + (500 * i) >= rate)) 3146 return r; 3147 } 3148 } 3149 /* assumes rates are listed in order from lowest to highest */ 3150 if (rate < UA_GETSAMP(a1d, 0)) 3151 j = 0; 3152 else 3153 j = a1d->bSamFreqType - 1; 3154 return UA_GETSAMP(a1d, j); 3155 } 3156 DPRINTF(("%s: could not match rate\n", __func__)); 3157 return rate; 3158 } 3159 3160 int 3161 uaudio_match_alt(void *addr, struct audio_params *p, int mode) 3162 { 3163 struct uaudio_softc *sc = addr; 3164 const struct usb_audio_streaming_type1_descriptor *a1d; 3165 int i, j, dir, rate; 3166 int alts_eh, alts_ch, ualt; 3167 3168 DPRINTF(("%s: mode=%s rate=%ld ch=%d pre=%d bps=%d enc=%d\n", 3169 __func__, mode == AUMODE_RECORD ? "rec" : "play", p->sample_rate, 3170 p->channels, p->precision, p->bps, p->encoding)); 3171 3172 alts_eh = 0; 3173 for (i = 0; i < sc->sc_nalts; i++) { 3174 dir = UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress); 3175 if ((mode == AUMODE_RECORD && dir != UE_DIR_IN) || 3176 (mode == AUMODE_PLAY && dir == UE_DIR_IN)) 3177 continue; 3178 DPRINTFN(6,("%s: matched %s alt %d for direction\n", __func__, 3179 mode == AUMODE_RECORD ? "rec" : "play", i)); 3180 if (sc->sc_alts[i].encoding != p->encoding) 3181 continue; 3182 a1d = sc->sc_alts[i].asf1desc; 3183 if (a1d->bBitResolution != p->precision) 3184 continue; 3185 alts_eh |= 1 << i; 3186 DPRINTFN(6,("%s: matched %s alt %d for enc/pre\n", __func__, 3187 mode == AUMODE_RECORD ? "rec" : "play", i)); 3188 } 3189 if (alts_eh == 0) { 3190 DPRINTF(("%s: could not match dir/enc/prec\n", __func__)); 3191 return -1; 3192 } 3193 3194 alts_ch = 0; 3195 for (i = 0; i < 3; i++) { 3196 for (j = 0; j < sc->sc_nalts; j++) { 3197 if (!(alts_eh & (1 << j))) 3198 continue; 3199 a1d = sc->sc_alts[j].asf1desc; 3200 if (a1d->bNrChannels == p->channels) { 3201 alts_ch |= 1 << j; 3202 DPRINTFN(6,("%s: matched alt %d for channels\n", 3203 __func__, j)); 3204 } 3205 } 3206 if (alts_ch) 3207 break; 3208 if (p->channels == 2) 3209 p->channels = 1; 3210 else 3211 p->channels = 2; 3212 } 3213 if (!alts_ch) { 3214 /* just use the first alt that matched the encoding */ 3215 for (i = 0; i < sc->sc_nalts; i++) 3216 if (alts_eh & (1 << i)) 3217 break; 3218 alts_ch = 1 << i; 3219 a1d = sc->sc_alts[i].asf1desc; 3220 p->channels = a1d->bNrChannels; 3221 } 3222 3223 ualt = -1; 3224 for (i = 0; i < sc->sc_nalts; i++) { 3225 if (alts_ch & (1 << i)) { 3226 rate = uaudio_match_alt_rate(sc, i, p->sample_rate); 3227 if (rate - 50 <= p->sample_rate && 3228 rate + 50 >= p->sample_rate) { 3229 DPRINTFN(6,("%s: alt %d matched rate %ld with %d\n", 3230 __func__, i, p->sample_rate, rate)); 3231 p->sample_rate = rate; 3232 break; 3233 } 3234 } 3235 } 3236 if (i < sc->sc_nalts) { 3237 ualt = i; 3238 } else { 3239 for (i = 0; i < sc->sc_nalts; i++) { 3240 if (alts_ch & (1 << i)) { 3241 ualt = i; 3242 p->sample_rate = uaudio_match_alt_rate(sc, 3243 i, p->sample_rate); 3244 break; 3245 } 3246 } 3247 } 3248 3249 return ualt; 3250 } 3251 3252 int 3253 uaudio_set_params(void *addr, int setmode, int usemode, 3254 struct audio_params *play, struct audio_params *rec) 3255 { 3256 struct uaudio_softc *sc = addr; 3257 int flags = sc->sc_altflags; 3258 int i; 3259 int paltidx = -1, raltidx = -1; 3260 struct audio_params *p; 3261 int mode; 3262 3263 if (usbd_is_dying(sc->sc_udev)) 3264 return (EIO); 3265 3266 if (((usemode & AUMODE_PLAY) && sc->sc_playchan.pipe != NULL) || 3267 ((usemode & AUMODE_RECORD) && sc->sc_recchan.pipe != NULL)) 3268 return (EBUSY); 3269 3270 if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) 3271 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0; 3272 if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) 3273 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0; 3274 3275 /* Some uaudio devices are unidirectional. Don't try to find a 3276 matching mode for the unsupported direction. */ 3277 setmode &= sc->sc_mode; 3278 3279 for (mode = AUMODE_RECORD; mode != -1; 3280 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 3281 if ((setmode & mode) == 0) 3282 continue; 3283 3284 p = (mode == AUMODE_PLAY) ? play : rec; 3285 3286 switch (p->precision) { 3287 case 24: 3288 if (!(flags & HAS_24)) { 3289 if (flags & HAS_16) 3290 p->precision = 16; 3291 else 3292 p->precision = 8; 3293 } 3294 break; 3295 case 16: 3296 if (!(flags & HAS_16)) { 3297 if (flags & HAS_24) 3298 p->precision = 24; 3299 else 3300 p->precision = 8; 3301 } 3302 break; 3303 case 8: 3304 if (!(flags & HAS_8) && !(flags & HAS_8U)) { 3305 if (flags & HAS_16) 3306 p->precision = 16; 3307 else 3308 p->precision = 24; 3309 } 3310 break; 3311 } 3312 3313 i = uaudio_match_alt(sc, p, mode); 3314 if (i < 0) { 3315 DPRINTF(("%s: uaudio_match_alt failed for %s\n", 3316 __func__, mode == AUMODE_RECORD ? "rec" : "play")); 3317 continue; 3318 } 3319 3320 p->bps = sc->sc_alts[i].asf1desc->bSubFrameSize; 3321 p->msb = 1; 3322 3323 if (mode == AUMODE_PLAY) 3324 paltidx = i; 3325 else 3326 raltidx = i; 3327 } 3328 3329 if (setmode & AUMODE_PLAY) { 3330 if (paltidx == -1) { 3331 DPRINTF(("%s: did not find alt for playback\n", 3332 __func__)); 3333 return (EINVAL); 3334 } 3335 /* XXX abort transfer if currently happening? */ 3336 uaudio_chan_init(&sc->sc_playchan, AUMODE_PLAY, paltidx, play); 3337 } 3338 if (setmode & AUMODE_RECORD) { 3339 if (raltidx == -1) { 3340 DPRINTF(("%s: did not find alt for recording\n", 3341 __func__)); 3342 return (EINVAL); 3343 } 3344 /* XXX abort transfer if currently happening? */ 3345 uaudio_chan_init(&sc->sc_recchan, AUMODE_RECORD, raltidx, rec); 3346 } 3347 3348 if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) 3349 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1; 3350 if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) 3351 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1; 3352 3353 DPRINTF(("uaudio_set_params: use altidx=p%d/r%d, altno=p%d/r%d\n", 3354 sc->sc_playchan.altidx, sc->sc_recchan.altidx, 3355 (sc->sc_playchan.altidx >= 0) 3356 ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting 3357 : -1, 3358 (sc->sc_recchan.altidx >= 0) 3359 ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting 3360 : -1)); 3361 3362 return (0); 3363 } 3364 3365 usbd_status 3366 uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed) 3367 { 3368 usb_device_request_t req; 3369 u_int8_t data[3]; 3370 3371 DPRINTFN(5,("uaudio_set_speed: endpt=%d speed=%u\n", endpt, speed)); 3372 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT; 3373 req.bRequest = SET_CUR; 3374 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0); 3375 USETW(req.wIndex, endpt); 3376 USETW(req.wLength, 3); 3377 data[0] = speed; 3378 data[1] = speed >> 8; 3379 data[2] = speed >> 16; 3380 3381 return (usbd_do_request(sc->sc_udev, &req, data)); 3382 } 3383 3384 void 3385 uaudio_set_speed_emu0202(struct chan *ch) 3386 { 3387 usb_device_request_t req; 3388 int rates[6] = { 44100, 48000, 88200, 96000, 176400, 192000 }; 3389 int i; 3390 u_int8_t data[1]; 3391 3392 for (i = 0; i < 6; i++) 3393 if (rates[i] >= ch->sample_rate) 3394 break; 3395 if (i >= 6) { 3396 DPRINTF(("%s: unhandled rate %d\n", __func__, ch->sample_rate)); 3397 i = 0; 3398 } 3399 3400 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 3401 req.bRequest = SET_CUR; 3402 USETW2(req.wValue, 0x03, 0); 3403 USETW2(req.wIndex, 12, ch->sc->sc_ac_iface); 3404 USETW(req.wLength, 1); 3405 data[0] = i; 3406 3407 usbd_do_request(ch->sc->sc_udev, &req, data); 3408 } 3409