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