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