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