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