1 /* $NetBSD: uaudio.c,v 1.67 2003/05/03 18:11:41 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * USB audio specs: http://www.usb.org/developers/data/devclass/audio10.pdf 42 * http://www.usb.org/developers/data/devclass/frmts10.pdf 43 * http://www.usb.org/developers/data/devclass/termt10.pdf 44 */ 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: uaudio.c,v 1.67 2003/05/03 18:11:41 wiz Exp $"); 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/malloc.h> 53 #include <sys/device.h> 54 #include <sys/ioctl.h> 55 #include <sys/tty.h> 56 #include <sys/file.h> 57 #include <sys/reboot.h> /* for bootverbose */ 58 #include <sys/select.h> 59 #include <sys/proc.h> 60 #include <sys/vnode.h> 61 #include <sys/device.h> 62 #include <sys/poll.h> 63 64 #include <sys/audioio.h> 65 #include <dev/audio_if.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/usbdi_util.h> 72 #include <dev/usb/usb_quirks.h> 73 74 #include <dev/usb/uaudioreg.h> 75 76 #ifdef UAUDIO_DEBUG 77 #define DPRINTF(x) if (uaudiodebug) logprintf x 78 #define DPRINTFN(n,x) if (uaudiodebug>(n)) logprintf x 79 int uaudiodebug = 0; 80 #else 81 #define DPRINTF(x) 82 #define DPRINTFN(n,x) 83 #endif 84 85 #define UAUDIO_NCHANBUFS 6 /* number of outstanding request */ 86 #define UAUDIO_NFRAMES 10 /* ms of sound in each request */ 87 88 89 #define MIX_MAX_CHAN 8 90 struct mixerctl { 91 u_int16_t wValue[MIX_MAX_CHAN]; /* using nchan */ 92 u_int16_t wIndex; 93 u_int8_t nchan; 94 u_int8_t type; 95 #define MIX_ON_OFF 1 96 #define MIX_SIGNED_16 2 97 #define MIX_UNSIGNED_16 3 98 #define MIX_SIGNED_8 4 99 #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1) 100 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16) 101 int minval, maxval; 102 u_int delta; 103 u_int mul; 104 u_int8_t class; 105 char ctlname[MAX_AUDIO_DEV_LEN]; 106 char *ctlunit; 107 }; 108 #define MAKE(h,l) (((h) << 8) | (l)) 109 110 struct as_info { 111 u_int8_t alt; 112 u_int8_t encoding; 113 u_int8_t attributes; /* Copy of bmAttributes of 114 * usb_audio_streaming_endpoint_descriptor 115 */ 116 usbd_interface_handle ifaceh; 117 usb_interface_descriptor_t *idesc; 118 usb_endpoint_descriptor_audio_t *edesc; 119 struct usb_audio_streaming_type1_descriptor *asf1desc; 120 int sc_busy; /* currently used */ 121 }; 122 123 struct chan { 124 void (*intr)(void *); /* DMA completion intr handler */ 125 void *arg; /* arg for intr() */ 126 usbd_pipe_handle pipe; 127 128 u_int sample_size; 129 u_int sample_rate; 130 u_int bytes_per_frame; 131 u_int fraction; /* fraction/1000 is the extra samples/frame */ 132 u_int residue; /* accumulates the fractional samples */ 133 134 u_char *start; /* upper layer buffer start */ 135 u_char *end; /* upper layer buffer end */ 136 u_char *cur; /* current position in upper layer buffer */ 137 int blksize; /* chunk size to report up */ 138 int transferred; /* transferred bytes not reported up */ 139 140 int altidx; /* currently used altidx */ 141 142 int curchanbuf; 143 struct chanbuf { 144 struct chan *chan; 145 usbd_xfer_handle xfer; 146 u_char *buffer; 147 u_int16_t sizes[UAUDIO_NFRAMES]; 148 u_int16_t offsets[UAUDIO_NFRAMES]; 149 u_int16_t size; 150 } chanbufs[UAUDIO_NCHANBUFS]; 151 152 struct uaudio_softc *sc; /* our softc */ 153 }; 154 155 struct uaudio_softc { 156 USBBASEDEVICE sc_dev; /* base device */ 157 usbd_device_handle sc_udev; /* USB device */ 158 159 int sc_ac_iface; /* Audio Control interface */ 160 usbd_interface_handle sc_ac_ifaceh; 161 162 struct chan sc_playchan; /* play channel */ 163 struct chan sc_recchan; /* record channel */ 164 165 int sc_nullalt; 166 167 int sc_audio_rev; 168 169 struct as_info *sc_alts; 170 int sc_nalts; 171 172 int sc_altflags; 173 #define HAS_8 0x01 174 #define HAS_16 0x02 175 #define HAS_8U 0x04 176 #define HAS_ALAW 0x08 177 #define HAS_MULAW 0x10 178 #define UA_NOFRAC 0x20 /* don't do sample rate adjustment */ 179 #define HAS_24 0x40 180 181 int sc_mode; /* play/record capability */ 182 183 struct mixerctl *sc_ctls; 184 int sc_nctls; 185 186 device_ptr_t sc_audiodev; 187 char sc_dying; 188 }; 189 190 #define UAC_OUTPUT 0 191 #define UAC_INPUT 1 192 #define UAC_EQUAL 2 193 #define UAC_NCLASSES 3 194 195 Static usbd_status uaudio_identify_ac(struct uaudio_softc *sc, 196 usb_config_descriptor_t *cdesc); 197 Static usbd_status uaudio_identify_as(struct uaudio_softc *sc, 198 usb_config_descriptor_t *cdesc); 199 Static usbd_status uaudio_process_as(struct uaudio_softc *sc, 200 char *buf, int *offsp, int size, 201 usb_interface_descriptor_t *id); 202 203 Static void uaudio_add_alt(struct uaudio_softc *sc, 204 struct as_info *ai); 205 Static void uaudio_mixer_alias_ctl(struct uaudio_softc *sc, 206 struct mixerctl *mp, const char *ctl); 207 208 Static usb_interface_descriptor_t *uaudio_find_iface(char *buf, 209 int size, int *offsp, int subtype); 210 211 Static void uaudio_mixer_add_ctl(struct uaudio_softc *sc, 212 struct mixerctl *mp); 213 Static char *uaudio_id_name(struct uaudio_softc *sc, 214 usb_descriptor_t **dps, int id); 215 Static struct usb_audio_cluster uaudio_get_cluster(int id, 216 usb_descriptor_t **dps); 217 Static void uaudio_add_input(struct uaudio_softc *sc, 218 usb_descriptor_t *v, usb_descriptor_t **dps); 219 Static void uaudio_add_output(struct uaudio_softc *sc, 220 usb_descriptor_t *v, usb_descriptor_t **dps); 221 Static void uaudio_add_mixer(struct uaudio_softc *sc, 222 usb_descriptor_t *v, usb_descriptor_t **dps); 223 Static void uaudio_add_selector(struct uaudio_softc *sc, 224 usb_descriptor_t *v, usb_descriptor_t **dps); 225 Static void uaudio_add_feature(struct uaudio_softc *sc, 226 usb_descriptor_t *v, usb_descriptor_t **dps); 227 Static void uaudio_add_processing_updown(struct uaudio_softc *sc, 228 usb_descriptor_t *v, usb_descriptor_t **dps); 229 Static void uaudio_add_processing(struct uaudio_softc *sc, 230 usb_descriptor_t *v, usb_descriptor_t **dps); 231 Static void uaudio_add_extension(struct uaudio_softc *sc, 232 usb_descriptor_t *v, usb_descriptor_t **dps); 233 Static usbd_status uaudio_identify(struct uaudio_softc *sc, 234 usb_config_descriptor_t *cdesc); 235 236 Static int uaudio_signext(int type, int val); 237 Static int uaudio_value2bsd(struct mixerctl *mc, int val); 238 Static int uaudio_bsd2value(struct mixerctl *mc, int val); 239 Static int uaudio_get(struct uaudio_softc *sc, int type, 240 int which, int wValue, int wIndex, int len); 241 Static int uaudio_ctl_get(struct uaudio_softc *sc, int which, 242 struct mixerctl *mc, int chan); 243 Static void uaudio_set(struct uaudio_softc *sc, int type, 244 int which, int wValue, int wIndex, int l, int v); 245 Static void uaudio_ctl_set(struct uaudio_softc *sc, int which, 246 struct mixerctl *mc, int chan, int val); 247 248 Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int); 249 250 Static usbd_status uaudio_chan_open(struct uaudio_softc *sc, 251 struct chan *ch); 252 Static void uaudio_chan_close(struct uaudio_softc *sc, 253 struct chan *ch); 254 Static usbd_status uaudio_chan_alloc_buffers(struct uaudio_softc *, 255 struct chan *); 256 Static void uaudio_chan_free_buffers(struct uaudio_softc *, 257 struct chan *); 258 Static void uaudio_chan_init(struct chan *, int, 259 const struct audio_params *, int); 260 Static void uaudio_chan_set_param(struct chan *ch, u_char *start, 261 u_char *end, int blksize); 262 Static void uaudio_chan_ptransfer(struct chan *ch); 263 Static void uaudio_chan_pintr(usbd_xfer_handle xfer, 264 usbd_private_handle priv, usbd_status status); 265 266 Static void uaudio_chan_rtransfer(struct chan *ch); 267 Static void uaudio_chan_rintr(usbd_xfer_handle xfer, 268 usbd_private_handle priv, usbd_status status); 269 270 Static int uaudio_open(void *, int); 271 Static void uaudio_close(void *); 272 Static int uaudio_drain(void *); 273 Static int uaudio_query_encoding(void *, struct audio_encoding *); 274 Static void uaudio_get_minmax_rates(int, const struct as_info *, 275 const struct audio_params *, 276 int, u_long *, u_long *); 277 Static int uaudio_match_alt_sub(int, const struct as_info *, 278 const struct audio_params *, 279 int, u_long); 280 Static int uaudio_match_alt_chan(int, const struct as_info *, 281 struct audio_params *, int); 282 Static int uaudio_match_alt(int, const struct as_info *, 283 struct audio_params *, int); 284 Static int uaudio_set_params(void *, int, int, 285 struct audio_params *, struct audio_params *); 286 Static int uaudio_round_blocksize(void *, int); 287 Static int uaudio_trigger_output(void *, void *, void *, 288 int, void (*)(void *), void *, 289 struct audio_params *); 290 Static int uaudio_trigger_input (void *, void *, void *, 291 int, void (*)(void *), void *, 292 struct audio_params *); 293 Static int uaudio_halt_in_dma(void *); 294 Static int uaudio_halt_out_dma(void *); 295 Static int uaudio_getdev(void *, struct audio_device *); 296 Static int uaudio_mixer_set_port(void *, mixer_ctrl_t *); 297 Static int uaudio_mixer_get_port(void *, mixer_ctrl_t *); 298 Static int uaudio_query_devinfo(void *, mixer_devinfo_t *); 299 Static int uaudio_get_props(void *); 300 301 Static struct audio_hw_if uaudio_hw_if = { 302 uaudio_open, 303 uaudio_close, 304 uaudio_drain, 305 uaudio_query_encoding, 306 uaudio_set_params, 307 uaudio_round_blocksize, 308 NULL, 309 NULL, 310 NULL, 311 NULL, 312 NULL, 313 uaudio_halt_out_dma, 314 uaudio_halt_in_dma, 315 NULL, 316 uaudio_getdev, 317 NULL, 318 uaudio_mixer_set_port, 319 uaudio_mixer_get_port, 320 uaudio_query_devinfo, 321 NULL, 322 NULL, 323 NULL, 324 NULL, 325 uaudio_get_props, 326 uaudio_trigger_output, 327 uaudio_trigger_input, 328 NULL, 329 }; 330 331 Static struct audio_device uaudio_device = { 332 "USB audio", 333 "", 334 "uaudio" 335 }; 336 337 USB_DECLARE_DRIVER(uaudio); 338 339 USB_MATCH(uaudio) 340 { 341 USB_MATCH_START(uaudio, uaa); 342 usb_interface_descriptor_t *id; 343 344 if (uaa->iface == NULL) 345 return (UMATCH_NONE); 346 347 id = usbd_get_interface_descriptor(uaa->iface); 348 /* Trigger on the control interface. */ 349 if (id == NULL || 350 id->bInterfaceClass != UICLASS_AUDIO || 351 id->bInterfaceSubClass != UISUBCLASS_AUDIOCONTROL || 352 (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_AUDIO)) 353 return (UMATCH_NONE); 354 355 return (UMATCH_IFACECLASS_IFACESUBCLASS); 356 } 357 358 USB_ATTACH(uaudio) 359 { 360 USB_ATTACH_START(uaudio, sc, uaa); 361 usb_interface_descriptor_t *id; 362 usb_config_descriptor_t *cdesc; 363 char devinfo[1024]; 364 usbd_status err; 365 int i, j, found; 366 367 usbd_devinfo(uaa->device, 0, devinfo); 368 printf(": %s\n", devinfo); 369 370 sc->sc_udev = uaa->device; 371 372 cdesc = usbd_get_config_descriptor(sc->sc_udev); 373 if (cdesc == NULL) { 374 printf("%s: failed to get configuration descriptor\n", 375 USBDEVNAME(sc->sc_dev)); 376 USB_ATTACH_ERROR_RETURN; 377 } 378 379 err = uaudio_identify(sc, cdesc); 380 if (err) { 381 printf("%s: audio descriptors make no sense, error=%d\n", 382 USBDEVNAME(sc->sc_dev), err); 383 USB_ATTACH_ERROR_RETURN; 384 } 385 386 sc->sc_ac_ifaceh = uaa->iface; 387 /* Pick up the AS interface. */ 388 for (i = 0; i < uaa->nifaces; i++) { 389 if (uaa->ifaces[i] == NULL) 390 continue; 391 id = usbd_get_interface_descriptor(uaa->ifaces[i]); 392 if (id == NULL) 393 continue; 394 found = 0; 395 for (j = 0; j < sc->sc_nalts; j++) { 396 if (id->bInterfaceNumber == 397 sc->sc_alts[j].idesc->bInterfaceNumber) { 398 sc->sc_alts[j].ifaceh = uaa->ifaces[i]; 399 found = 1; 400 } 401 } 402 if (found) 403 uaa->ifaces[i] = NULL; 404 } 405 406 for (j = 0; j < sc->sc_nalts; j++) { 407 if (sc->sc_alts[j].ifaceh == NULL) { 408 printf("%s: alt %d missing AS interface(s)\n", 409 USBDEVNAME(sc->sc_dev), j); 410 USB_ATTACH_ERROR_RETURN; 411 } 412 } 413 414 printf("%s: audio rev %d.%02x\n", USBDEVNAME(sc->sc_dev), 415 sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff); 416 417 sc->sc_playchan.sc = sc->sc_recchan.sc = sc; 418 sc->sc_playchan.altidx = -1; 419 sc->sc_recchan.altidx = -1; 420 421 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC) 422 sc->sc_altflags |= UA_NOFRAC; 423 424 #ifndef UAUDIO_DEBUG 425 if (bootverbose) 426 #endif 427 printf("%s: %d mixer controls\n", USBDEVNAME(sc->sc_dev), 428 sc->sc_nctls); 429 430 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 431 USBDEV(sc->sc_dev)); 432 433 DPRINTF(("uaudio_attach: doing audio_attach_mi\n")); 434 #if defined(__OpenBSD__) 435 audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev); 436 #else 437 sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev); 438 #endif 439 440 USB_ATTACH_SUCCESS_RETURN; 441 } 442 443 int 444 uaudio_activate(device_ptr_t self, enum devact act) 445 { 446 struct uaudio_softc *sc = (struct uaudio_softc *)self; 447 int rv = 0; 448 449 switch (act) { 450 case DVACT_ACTIVATE: 451 return (EOPNOTSUPP); 452 break; 453 454 case DVACT_DEACTIVATE: 455 if (sc->sc_audiodev != NULL) 456 rv = config_deactivate(sc->sc_audiodev); 457 sc->sc_dying = 1; 458 break; 459 } 460 return (rv); 461 } 462 463 int 464 uaudio_detach(device_ptr_t self, int flags) 465 { 466 struct uaudio_softc *sc = (struct uaudio_softc *)self; 467 int rv = 0; 468 469 /* Wait for outstanding requests to complete. */ 470 usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES); 471 472 if (sc->sc_audiodev != NULL) 473 rv = config_detach(sc->sc_audiodev, flags); 474 475 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 476 USBDEV(sc->sc_dev)); 477 478 return (rv); 479 } 480 481 int 482 uaudio_query_encoding(void *addr, struct audio_encoding *fp) 483 { 484 struct uaudio_softc *sc = addr; 485 int flags = sc->sc_altflags; 486 int idx; 487 488 if (sc->sc_dying) 489 return (EIO); 490 491 if (sc->sc_nalts == 0 || flags == 0) 492 return (ENXIO); 493 494 idx = fp->index; 495 switch (idx) { 496 case 0: 497 strcpy(fp->name, AudioEulinear); 498 fp->encoding = AUDIO_ENCODING_ULINEAR; 499 fp->precision = 8; 500 fp->flags = flags&HAS_8U ? 0 : AUDIO_ENCODINGFLAG_EMULATED; 501 return (0); 502 case 1: 503 strcpy(fp->name, AudioEmulaw); 504 fp->encoding = AUDIO_ENCODING_ULAW; 505 fp->precision = 8; 506 fp->flags = flags&HAS_MULAW ? 0 : AUDIO_ENCODINGFLAG_EMULATED; 507 return (0); 508 case 2: 509 strcpy(fp->name, AudioEalaw); 510 fp->encoding = AUDIO_ENCODING_ALAW; 511 fp->precision = 8; 512 fp->flags = flags&HAS_ALAW ? 0 : AUDIO_ENCODINGFLAG_EMULATED; 513 return (0); 514 case 3: 515 strcpy(fp->name, AudioEslinear); 516 fp->encoding = AUDIO_ENCODING_SLINEAR; 517 fp->precision = 8; 518 fp->flags = flags&HAS_8 ? 0 : AUDIO_ENCODINGFLAG_EMULATED; 519 return (0); 520 case 4: 521 strcpy(fp->name, AudioEslinear_le); 522 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 523 fp->precision = 16; 524 fp->flags = 0; 525 return (0); 526 case 5: 527 strcpy(fp->name, AudioEulinear_le); 528 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 529 fp->precision = 16; 530 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 531 return (0); 532 case 6: 533 strcpy(fp->name, AudioEslinear_be); 534 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 535 fp->precision = 16; 536 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 537 return (0); 538 case 7: 539 strcpy(fp->name, AudioEulinear_be); 540 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 541 fp->precision = 16; 542 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 543 return (0); 544 default: 545 return (EINVAL); 546 } 547 } 548 549 usb_interface_descriptor_t * 550 uaudio_find_iface(char *buf, int size, int *offsp, int subtype) 551 { 552 usb_interface_descriptor_t *d; 553 554 while (*offsp < size) { 555 d = (void *)(buf + *offsp); 556 *offsp += d->bLength; 557 if (d->bDescriptorType == UDESC_INTERFACE && 558 d->bInterfaceClass == UICLASS_AUDIO && 559 d->bInterfaceSubClass == subtype) 560 return (d); 561 } 562 return (NULL); 563 } 564 565 void 566 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc) 567 { 568 int res; 569 size_t len = sizeof(*mc) * (sc->sc_nctls + 1); 570 struct mixerctl *nmc = sc->sc_nctls == 0 ? 571 malloc(len, M_USBDEV, M_NOWAIT) : 572 realloc(sc->sc_ctls, len, M_USBDEV, M_NOWAIT); 573 574 if (nmc == NULL) { 575 printf("uaudio_mixer_add_ctl: no memory\n"); 576 return; 577 } 578 sc->sc_ctls = nmc; 579 580 mc->delta = 0; 581 if (mc->type != MIX_ON_OFF) { 582 /* Determine min and max values. */ 583 mc->minval = uaudio_signext(mc->type, 584 uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE, 585 mc->wValue[0], mc->wIndex, 586 MIX_SIZE(mc->type))); 587 mc->maxval = 1 + uaudio_signext(mc->type, 588 uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE, 589 mc->wValue[0], mc->wIndex, 590 MIX_SIZE(mc->type))); 591 mc->mul = mc->maxval - mc->minval; 592 if (mc->mul == 0) 593 mc->mul = 1; 594 res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE, 595 mc->wValue[0], mc->wIndex, 596 MIX_SIZE(mc->type)); 597 if (res > 0) 598 mc->delta = (res * 256 + mc->mul/2) / mc->mul; 599 } else { 600 mc->minval = 0; 601 mc->maxval = 1; 602 } 603 604 sc->sc_ctls[sc->sc_nctls++] = *mc; 605 606 #ifdef UAUDIO_DEBUG 607 if (uaudiodebug > 2) { 608 int i; 609 DPRINTF(("uaudio_mixer_add_ctl: wValue=%04x",mc->wValue[0])); 610 for (i = 1; i < mc->nchan; i++) 611 DPRINTF((",%04x", mc->wValue[i])); 612 DPRINTF((" wIndex=%04x type=%d name='%s' unit='%s' " 613 "min=%d max=%d\n", 614 mc->wIndex, mc->type, mc->ctlname, mc->ctlunit, 615 mc->minval, mc->maxval)); 616 } 617 #endif 618 } 619 620 void 621 uaudio_mixer_alias_ctl(struct uaudio_softc *sc, struct mixerctl *mc, 622 const char *name) 623 { 624 /* XXX mark as alias? */ 625 strcpy(mc->ctlname, name); 626 uaudio_mixer_add_ctl(sc, mc); 627 } 628 629 char * 630 uaudio_id_name(struct uaudio_softc *sc, usb_descriptor_t **dps, int id) 631 { 632 static char buf[32]; 633 sprintf(buf, "i%d", id); 634 return (buf); 635 } 636 637 struct usb_audio_cluster 638 uaudio_get_cluster(int id, usb_descriptor_t **dps) 639 { 640 struct usb_audio_cluster r; 641 usb_descriptor_t *dp; 642 int i; 643 644 for (i = 0; i < 25; i++) { /* avoid infinite loops */ 645 dp = dps[id]; 646 if (dp == 0) 647 goto bad; 648 switch (dp->bDescriptorSubtype) { 649 case UDESCSUB_AC_INPUT: 650 #define p ((struct usb_audio_input_terminal *)dp) 651 r.bNrChannels = p->bNrChannels; 652 USETW(r.wChannelConfig, UGETW(p->wChannelConfig)); 653 r.iChannelNames = p->iChannelNames; 654 #undef p 655 return (r); 656 case UDESCSUB_AC_OUTPUT: 657 #define p ((struct usb_audio_output_terminal *)dp) 658 id = p->bSourceId; 659 #undef p 660 break; 661 case UDESCSUB_AC_MIXER: 662 #define p ((struct usb_audio_mixer_unit *)dp) 663 r = *(struct usb_audio_cluster *) 664 &p->baSourceId[p->bNrInPins]; 665 #undef p 666 return (r); 667 case UDESCSUB_AC_SELECTOR: 668 /* XXX This is not really right */ 669 #define p ((struct usb_audio_selector_unit *)dp) 670 id = p->baSourceId[0]; 671 #undef p 672 break; 673 case UDESCSUB_AC_FEATURE: 674 #define p ((struct usb_audio_feature_unit *)dp) 675 id = p->bSourceId; 676 #undef p 677 break; 678 case UDESCSUB_AC_PROCESSING: 679 #define p ((struct usb_audio_processing_unit *)dp) 680 r = *(struct usb_audio_cluster *) 681 &p->baSourceId[p->bNrInPins]; 682 #undef p 683 return (r); 684 case UDESCSUB_AC_EXTENSION: 685 #define p ((struct usb_audio_extension_unit *)dp) 686 r = *(struct usb_audio_cluster *) 687 &p->baSourceId[p->bNrInPins]; 688 #undef p 689 return (r); 690 default: 691 goto bad; 692 } 693 } 694 bad: 695 printf("uaudio_get_cluster: bad data\n"); 696 memset(&r, 0, sizeof r); 697 return (r); 698 699 } 700 701 void 702 uaudio_add_input(struct uaudio_softc *sc, usb_descriptor_t *v, 703 usb_descriptor_t **dps) 704 { 705 #ifdef UAUDIO_DEBUG 706 struct usb_audio_input_terminal *d = 707 (struct usb_audio_input_terminal *)v; 708 709 DPRINTFN(2,("uaudio_add_input: bTerminalId=%d wTerminalType=0x%04x " 710 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d " 711 "iChannelNames=%d iTerminal=%d\n", 712 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 713 d->bNrChannels, UGETW(d->wChannelConfig), 714 d->iChannelNames, d->iTerminal)); 715 #endif 716 } 717 718 void 719 uaudio_add_output(struct uaudio_softc *sc, usb_descriptor_t *v, 720 usb_descriptor_t **dps) 721 { 722 #ifdef UAUDIO_DEBUG 723 struct usb_audio_output_terminal *d = 724 (struct usb_audio_output_terminal *)v; 725 726 DPRINTFN(2,("uaudio_add_output: bTerminalId=%d wTerminalType=0x%04x " 727 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n", 728 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal, 729 d->bSourceId, d->iTerminal)); 730 #endif 731 } 732 733 void 734 uaudio_add_mixer(struct uaudio_softc *sc, usb_descriptor_t *v, 735 usb_descriptor_t **dps) 736 { 737 struct usb_audio_mixer_unit *d = (struct usb_audio_mixer_unit *)v; 738 struct usb_audio_mixer_unit_1 *d1; 739 int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k; 740 uByte *bm; 741 struct mixerctl mix; 742 743 DPRINTFN(2,("uaudio_add_mixer: bUnitId=%d bNrInPins=%d\n", 744 d->bUnitId, d->bNrInPins)); 745 746 /* Compute the number of input channels */ 747 ichs = 0; 748 for (i = 0; i < d->bNrInPins; i++) 749 ichs += uaudio_get_cluster(d->baSourceId[i], dps).bNrChannels; 750 751 /* and the number of output channels */ 752 d1 = (struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins]; 753 ochs = d1->bNrChannels; 754 DPRINTFN(2,("uaudio_add_mixer: ichs=%d ochs=%d\n", ichs, ochs)); 755 756 bm = d1->bmControls; 757 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 758 mix.class = -1; 759 mix.type = MIX_SIGNED_16; 760 mix.ctlunit = AudioNvolume; 761 #define BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1) 762 for (p = i = 0; i < d->bNrInPins; i++) { 763 chs = uaudio_get_cluster(d->baSourceId[i], dps).bNrChannels; 764 mc = 0; 765 for (c = 0; c < chs; c++) { 766 mo = 0; 767 for (o = 0; o < ochs; o++) { 768 bno = (p + c) * ochs + o; 769 if (BIT(bno)) 770 mo++; 771 } 772 if (mo == 1) 773 mc++; 774 } 775 if (mc == chs && chs <= MIX_MAX_CHAN) { 776 k = 0; 777 for (c = 0; c < chs; c++) 778 for (o = 0; o < ochs; o++) { 779 bno = (p + c) * ochs + o; 780 if (BIT(bno)) 781 mix.wValue[k++] = 782 MAKE(p+c+1, o+1); 783 } 784 sprintf(mix.ctlname, "mix%d-%s", d->bUnitId, 785 uaudio_id_name(sc, dps, d->baSourceId[i])); 786 mix.nchan = chs; 787 uaudio_mixer_add_ctl(sc, &mix); 788 } else { 789 /* XXX */ 790 } 791 #undef BIT 792 p += chs; 793 } 794 795 } 796 797 void 798 uaudio_add_selector(struct uaudio_softc *sc, usb_descriptor_t *v, 799 usb_descriptor_t **dps) 800 { 801 #ifdef UAUDIO_DEBUG 802 struct usb_audio_selector_unit *d = 803 (struct usb_audio_selector_unit *)v; 804 805 DPRINTFN(2,("uaudio_add_selector: bUnitId=%d bNrInPins=%d\n", 806 d->bUnitId, d->bNrInPins)); 807 #endif 808 printf("uaudio_add_selector: NOT IMPLEMENTED\n"); 809 } 810 811 void 812 uaudio_add_feature(struct uaudio_softc *sc, usb_descriptor_t *v, 813 usb_descriptor_t **dps) 814 { 815 struct usb_audio_feature_unit *d = (struct usb_audio_feature_unit *)v; 816 uByte *ctls = d->bmaControls; 817 int ctlsize = d->bControlSize; 818 int nchan = (d->bLength - 7) / ctlsize; 819 int srcId = d->bSourceId; 820 u_int fumask, mmask, cmask; 821 struct mixerctl mix; 822 int chan, ctl, i, unit; 823 824 #define GET(i) (ctls[(i)*ctlsize] | \ 825 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0)) 826 827 mmask = GET(0); 828 /* Figure out what we can control */ 829 for (cmask = 0, chan = 1; chan < nchan; chan++) { 830 DPRINTFN(9,("uaudio_add_feature: chan=%d mask=%x\n", 831 chan, GET(chan))); 832 cmask |= GET(chan); 833 } 834 835 DPRINTFN(1,("uaudio_add_feature: bUnitId=%d bSourceId=%d, " 836 "%d channels, mmask=0x%04x, cmask=0x%04x\n", 837 d->bUnitId, srcId, nchan, mmask, cmask)); 838 839 if (nchan > MIX_MAX_CHAN) 840 nchan = MIX_MAX_CHAN; 841 unit = d->bUnitId; 842 mix.wIndex = MAKE(unit, sc->sc_ac_iface); 843 for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) { 844 fumask = FU_MASK(ctl); 845 DPRINTFN(4,("uaudio_add_feature: ctl=%d fumask=0x%04x\n", 846 ctl, fumask)); 847 if (mmask & fumask) { 848 mix.nchan = 1; 849 mix.wValue[0] = MAKE(ctl, 0); 850 } else if (cmask & fumask) { 851 mix.nchan = nchan - 1; 852 for (i = 1; i < nchan; i++) { 853 if (GET(i) & fumask) 854 mix.wValue[i-1] = MAKE(ctl, i); 855 else 856 mix.wValue[i-1] = -1; 857 } 858 } else { 859 continue; 860 } 861 #undef GET 862 mix.class = UAC_OUTPUT; /* XXX we don't really know this */ 863 switch (ctl) { 864 case MUTE_CONTROL: 865 mix.type = MIX_ON_OFF; 866 mix.ctlunit = ""; 867 uaudio_mixer_alias_ctl(sc, &mix, AudioNmute); 868 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 869 uaudio_id_name(sc, dps, srcId), 870 AudioNmute); 871 break; 872 case VOLUME_CONTROL: 873 mix.type = MIX_SIGNED_16; 874 mix.ctlunit = AudioNvolume; 875 uaudio_mixer_alias_ctl(sc, &mix, AudioNmaster); 876 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 877 uaudio_id_name(sc, dps, srcId), 878 AudioNmaster); 879 break; 880 case BASS_CONTROL: 881 mix.type = MIX_SIGNED_8; 882 mix.ctlunit = AudioNbass; 883 uaudio_mixer_alias_ctl(sc, &mix, AudioNbass); 884 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 885 uaudio_id_name(sc, dps, srcId), 886 AudioNbass); 887 break; 888 case MID_CONTROL: 889 mix.type = MIX_SIGNED_8; 890 mix.ctlunit = AudioNmid; 891 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 892 uaudio_id_name(sc, dps, srcId), 893 AudioNmid); 894 break; 895 case TREBLE_CONTROL: 896 mix.type = MIX_SIGNED_8; 897 mix.ctlunit = AudioNtreble; 898 uaudio_mixer_alias_ctl(sc, &mix, AudioNtreble); 899 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 900 uaudio_id_name(sc, dps, srcId), 901 AudioNtreble); 902 break; 903 case GRAPHIC_EQUALIZER_CONTROL: 904 continue; /* XXX don't add anything */ 905 break; 906 case AGC_CONTROL: 907 mix.type = MIX_ON_OFF; 908 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 909 uaudio_id_name(sc, dps, srcId), 910 AudioNagc); 911 mix.ctlunit = ""; 912 break; 913 case DELAY_CONTROL: 914 mix.type = MIX_UNSIGNED_16; 915 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 916 uaudio_id_name(sc, dps, srcId), 917 AudioNdelay); 918 mix.ctlunit = "4 ms"; 919 break; 920 case BASS_BOOST_CONTROL: 921 mix.type = MIX_ON_OFF; 922 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 923 uaudio_id_name(sc, dps, srcId), 924 AudioNbassboost); 925 mix.ctlunit = ""; 926 break; 927 case LOUDNESS_CONTROL: 928 mix.type = MIX_ON_OFF; 929 sprintf(mix.ctlname, "fea%d-%s-%s", unit, 930 uaudio_id_name(sc, dps, srcId), 931 AudioNloudness); 932 mix.ctlunit = ""; 933 break; 934 } 935 uaudio_mixer_add_ctl(sc, &mix); 936 } 937 } 938 939 void 940 uaudio_add_processing_updown(struct uaudio_softc *sc, usb_descriptor_t *v, 941 usb_descriptor_t **dps) 942 { 943 struct usb_audio_processing_unit *d = 944 (struct usb_audio_processing_unit *)v; 945 struct usb_audio_processing_unit_1 *d1 = 946 (struct usb_audio_processing_unit_1 *)&d->baSourceId[d->bNrInPins]; 947 struct usb_audio_processing_unit_updown *ud = 948 (struct usb_audio_processing_unit_updown *) 949 &d1->bmControls[d1->bControlSize]; 950 struct mixerctl mix; 951 int i; 952 953 DPRINTFN(2,("uaudio_add_processing_updown: bUnitId=%d bNrModes=%d\n", 954 d->bUnitId, ud->bNrModes)); 955 956 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) { 957 DPRINTF(("uaudio_add_processing_updown: no mode select\n")); 958 return; 959 } 960 961 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 962 mix.nchan = 1; 963 mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0); 964 mix.class = -1; 965 mix.type = MIX_ON_OFF; /* XXX */ 966 mix.ctlunit = ""; 967 sprintf(mix.ctlname, "pro%d-mode", d->bUnitId); 968 969 for (i = 0; i < ud->bNrModes; i++) { 970 DPRINTFN(2,("uaudio_add_processing_updown: i=%d bm=0x%x\n", 971 i, UGETW(ud->waModes[i]))); 972 /* XXX */ 973 } 974 uaudio_mixer_add_ctl(sc, &mix); 975 } 976 977 void 978 uaudio_add_processing(struct uaudio_softc *sc, usb_descriptor_t *v, 979 usb_descriptor_t **dps) 980 { 981 struct usb_audio_processing_unit *d = 982 (struct usb_audio_processing_unit *)v; 983 struct usb_audio_processing_unit_1 *d1 = 984 (struct usb_audio_processing_unit_1 *)&d->baSourceId[d->bNrInPins]; 985 int ptype = UGETW(d->wProcessType); 986 struct mixerctl mix; 987 988 DPRINTFN(2,("uaudio_add_processing: wProcessType=%d bUnitId=%d " 989 "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins)); 990 991 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) { 992 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 993 mix.nchan = 1; 994 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0); 995 mix.class = -1; 996 mix.type = MIX_ON_OFF; 997 mix.ctlunit = ""; 998 sprintf(mix.ctlname, "pro%d.%d-enable", d->bUnitId, ptype); 999 uaudio_mixer_add_ctl(sc, &mix); 1000 } 1001 1002 switch(ptype) { 1003 case UPDOWNMIX_PROCESS: 1004 uaudio_add_processing_updown(sc, v, dps); 1005 break; 1006 case DOLBY_PROLOGIC_PROCESS: 1007 case P3D_STEREO_EXTENDER_PROCESS: 1008 case REVERBATION_PROCESS: 1009 case CHORUS_PROCESS: 1010 case DYN_RANGE_COMP_PROCESS: 1011 default: 1012 #ifdef UAUDIO_DEBUG 1013 printf("uaudio_add_processing: unit %d, type=%d not impl.\n", 1014 d->bUnitId, ptype); 1015 #endif 1016 break; 1017 } 1018 } 1019 1020 void 1021 uaudio_add_extension(struct uaudio_softc *sc, usb_descriptor_t *v, 1022 usb_descriptor_t **dps) 1023 { 1024 struct usb_audio_extension_unit *d = 1025 (struct usb_audio_extension_unit *)v; 1026 struct usb_audio_extension_unit_1 *d1 = 1027 (struct usb_audio_extension_unit_1 *)&d->baSourceId[d->bNrInPins]; 1028 struct mixerctl mix; 1029 1030 DPRINTFN(2,("uaudio_add_extension: bUnitId=%d bNrInPins=%d\n", 1031 d->bUnitId, d->bNrInPins)); 1032 1033 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU) 1034 return; 1035 1036 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) { 1037 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface); 1038 mix.nchan = 1; 1039 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0); 1040 mix.class = -1; 1041 mix.type = MIX_ON_OFF; 1042 mix.ctlunit = ""; 1043 sprintf(mix.ctlname, "ext%d-enable", d->bUnitId); 1044 uaudio_mixer_add_ctl(sc, &mix); 1045 } 1046 } 1047 1048 usbd_status 1049 uaudio_identify(struct uaudio_softc *sc, usb_config_descriptor_t *cdesc) 1050 { 1051 usbd_status err; 1052 1053 err = uaudio_identify_ac(sc, cdesc); 1054 if (err) 1055 return (err); 1056 return (uaudio_identify_as(sc, cdesc)); 1057 } 1058 1059 void 1060 uaudio_add_alt(struct uaudio_softc *sc, struct as_info *ai) 1061 { 1062 size_t len = sizeof(*ai) * (sc->sc_nalts + 1); 1063 struct as_info *nai = (sc->sc_nalts == 0) ? 1064 malloc(len, M_USBDEV, M_NOWAIT) : 1065 realloc(sc->sc_alts, len, M_USBDEV, M_NOWAIT); 1066 1067 if (nai == NULL) { 1068 printf("uaudio_add_alt: no memory\n"); 1069 return; 1070 } 1071 1072 sc->sc_alts = nai; 1073 DPRINTFN(2,("uaudio_add_alt: adding alt=%d, enc=%d\n", 1074 ai->alt, ai->encoding)); 1075 sc->sc_alts[sc->sc_nalts++] = *ai; 1076 } 1077 1078 usbd_status 1079 uaudio_process_as(struct uaudio_softc *sc, char *buf, int *offsp, 1080 int size, usb_interface_descriptor_t *id) 1081 #define offs (*offsp) 1082 { 1083 struct usb_audio_streaming_interface_descriptor *asid; 1084 struct usb_audio_streaming_type1_descriptor *asf1d; 1085 usb_endpoint_descriptor_audio_t *ed; 1086 struct usb_audio_streaming_endpoint_descriptor *sed; 1087 int format, chan, prec, enc; 1088 int dir, type; 1089 struct as_info ai; 1090 1091 asid = (void *)(buf + offs); 1092 if (asid->bDescriptorType != UDESC_CS_INTERFACE || 1093 asid->bDescriptorSubtype != AS_GENERAL) 1094 return (USBD_INVAL); 1095 offs += asid->bLength; 1096 if (offs > size) 1097 return (USBD_INVAL); 1098 asf1d = (void *)(buf + offs); 1099 if (asf1d->bDescriptorType != UDESC_CS_INTERFACE || 1100 asf1d->bDescriptorSubtype != FORMAT_TYPE) 1101 return (USBD_INVAL); 1102 offs += asf1d->bLength; 1103 if (offs > size) 1104 return (USBD_INVAL); 1105 1106 if (asf1d->bFormatType != FORMAT_TYPE_I) { 1107 printf("%s: ignored setting with type %d format\n", 1108 USBDEVNAME(sc->sc_dev), UGETW(asid->wFormatTag)); 1109 return (USBD_NORMAL_COMPLETION); 1110 } 1111 1112 ed = (void *)(buf + offs); 1113 if (ed->bDescriptorType != UDESC_ENDPOINT) 1114 return (USBD_INVAL); 1115 DPRINTF(("uaudio_process_as: endpoint bLength=%d bDescriptorType=%d " 1116 "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d " 1117 "bInterval=%d bRefresh=%d bSynchAddress=%d\n", 1118 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress, 1119 ed->bmAttributes, UGETW(ed->wMaxPacketSize), 1120 ed->bInterval, ed->bRefresh, ed->bSynchAddress)); 1121 offs += ed->bLength; 1122 if (offs > size) 1123 return (USBD_INVAL); 1124 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS) 1125 return (USBD_INVAL); 1126 1127 dir = UE_GET_DIR(ed->bEndpointAddress); 1128 type = UE_GET_ISO_TYPE(ed->bmAttributes); 1129 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) && 1130 dir == UE_DIR_IN && type == UE_ISO_ADAPT) 1131 type = UE_ISO_ASYNC; 1132 1133 /* We can't handle endpoints that need a sync pipe yet. */ 1134 if (dir == UE_DIR_IN ? type == UE_ISO_ADAPT : type == UE_ISO_ASYNC) { 1135 printf("%s: ignored %sput endpoint of type %s\n", 1136 USBDEVNAME(sc->sc_dev), 1137 dir == UE_DIR_IN ? "in" : "out", 1138 dir == UE_DIR_IN ? "adaptive" : "async"); 1139 return (USBD_NORMAL_COMPLETION); 1140 } 1141 1142 sed = (void *)(buf + offs); 1143 if (sed->bDescriptorType != UDESC_CS_ENDPOINT || 1144 sed->bDescriptorSubtype != AS_GENERAL) 1145 return (USBD_INVAL); 1146 offs += sed->bLength; 1147 if (offs > size) 1148 return (USBD_INVAL); 1149 1150 format = UGETW(asid->wFormatTag); 1151 chan = asf1d->bNrChannels; 1152 prec = asf1d->bBitResolution; 1153 if (prec != 8 && prec != 16 && prec != 24) { 1154 printf("%s: ignored setting with precision %d\n", 1155 USBDEVNAME(sc->sc_dev), prec); 1156 return (USBD_NORMAL_COMPLETION); 1157 } 1158 switch (format) { 1159 case UA_FMT_PCM: 1160 if (prec == 8) { 1161 sc->sc_altflags |= HAS_8; 1162 } else if (prec == 16) { 1163 sc->sc_altflags |= HAS_16; 1164 } else if (prec == 24) { 1165 sc->sc_altflags |= HAS_24; 1166 } 1167 enc = AUDIO_ENCODING_SLINEAR_LE; 1168 break; 1169 case UA_FMT_PCM8: 1170 enc = AUDIO_ENCODING_ULINEAR_LE; 1171 sc->sc_altflags |= HAS_8U; 1172 break; 1173 case UA_FMT_ALAW: 1174 enc = AUDIO_ENCODING_ALAW; 1175 sc->sc_altflags |= HAS_ALAW; 1176 break; 1177 case UA_FMT_MULAW: 1178 enc = AUDIO_ENCODING_ULAW; 1179 sc->sc_altflags |= HAS_MULAW; 1180 break; 1181 default: 1182 printf("%s: ignored setting with format %d\n", 1183 USBDEVNAME(sc->sc_dev), format); 1184 return (USBD_NORMAL_COMPLETION); 1185 } 1186 DPRINTFN(1, ("uaudio_process_as: alt=%d enc=%d chan=%d prec=%d\n", 1187 id->bAlternateSetting, enc, chan, prec)); 1188 ai.alt = id->bAlternateSetting; 1189 ai.encoding = enc; 1190 ai.attributes = sed->bmAttributes; 1191 ai.idesc = id; 1192 ai.edesc = ed; 1193 ai.asf1desc = asf1d; 1194 ai.sc_busy = 0; 1195 uaudio_add_alt(sc, &ai); 1196 #ifdef UAUDIO_DEBUG 1197 { 1198 int j; 1199 if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) { 1200 DPRINTFN(1, ("uaudio_process_as: rate=%d-%d\n", 1201 UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d))); 1202 } else { 1203 DPRINTFN(1, ("uaudio_process_as: ")); 1204 for (j = 0; j < asf1d->bSamFreqType; j++) 1205 DPRINTFN(1, (" %d", UA_GETSAMP(asf1d, j))); 1206 DPRINTFN(1, ("\n")); 1207 } 1208 if (ai.attributes & UA_SED_FREQ_CONTROL) 1209 DPRINTFN(1, ("uaudio_process_as: FREQ_CONTROL\n")); 1210 if (ai.attributes & UA_SED_PITCH_CONTROL) 1211 DPRINTFN(1, ("uaudio_process_as: PITCH_CONTROL\n")); 1212 } 1213 #endif 1214 sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD; 1215 1216 return (USBD_NORMAL_COMPLETION); 1217 } 1218 #undef offs 1219 1220 usbd_status 1221 uaudio_identify_as(struct uaudio_softc *sc, usb_config_descriptor_t *cdesc) 1222 { 1223 usb_interface_descriptor_t *id; 1224 char *buf; 1225 int size, offs; 1226 1227 size = UGETW(cdesc->wTotalLength); 1228 buf = (char *)cdesc; 1229 1230 /* Locate the AudioStreaming interface descriptor. */ 1231 offs = 0; 1232 id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOSTREAM); 1233 if (id == NULL) 1234 return (USBD_INVAL); 1235 1236 /* Loop through all the alternate settings. */ 1237 while (offs <= size) { 1238 DPRINTFN(2, ("uaudio_identify: interface %d\n", 1239 id->bInterfaceNumber)); 1240 switch (id->bNumEndpoints) { 1241 case 0: 1242 DPRINTFN(2, ("uaudio_identify: AS null alt=%d\n", 1243 id->bAlternateSetting)); 1244 sc->sc_nullalt = id->bAlternateSetting; 1245 break; 1246 case 1: 1247 uaudio_process_as(sc, buf, &offs, size, id); 1248 break; 1249 default: 1250 #ifdef UAUDIO_DEBUG 1251 printf("%s: ignored audio interface with %d " 1252 "endpoints\n", 1253 USBDEVNAME(sc->sc_dev), id->bNumEndpoints); 1254 #endif 1255 break; 1256 } 1257 id = uaudio_find_iface(buf, size, &offs,UISUBCLASS_AUDIOSTREAM); 1258 if (id == NULL) 1259 break; 1260 } 1261 if (offs > size) 1262 return (USBD_INVAL); 1263 DPRINTF(("uaudio_identify_as: %d alts available\n", sc->sc_nalts)); 1264 1265 if ((sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD)) == 0) { 1266 printf("%s: no usable endpoint found\n", 1267 USBDEVNAME(sc->sc_dev)); 1268 return (USBD_INVAL); 1269 } 1270 1271 return (USBD_NORMAL_COMPLETION); 1272 } 1273 1274 usbd_status 1275 uaudio_identify_ac(struct uaudio_softc *sc, usb_config_descriptor_t *cdesc) 1276 { 1277 usb_interface_descriptor_t *id; 1278 struct usb_audio_control_descriptor *acdp; 1279 usb_descriptor_t *dp, *dps[256]; 1280 char *buf, *ibuf, *ibufend; 1281 int size, offs, aclen, ndps, i; 1282 1283 size = UGETW(cdesc->wTotalLength); 1284 buf = (char *)cdesc; 1285 1286 /* Locate the AudioControl interface descriptor. */ 1287 offs = 0; 1288 id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOCONTROL); 1289 if (id == NULL) 1290 return (USBD_INVAL); 1291 if (offs + sizeof *acdp > size) 1292 return (USBD_INVAL); 1293 sc->sc_ac_iface = id->bInterfaceNumber; 1294 DPRINTFN(2,("uaudio_identify: AC interface is %d\n", sc->sc_ac_iface)); 1295 1296 /* A class-specific AC interface header should follow. */ 1297 ibuf = buf + offs; 1298 acdp = (struct usb_audio_control_descriptor *)ibuf; 1299 if (acdp->bDescriptorType != UDESC_CS_INTERFACE || 1300 acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER) 1301 return (USBD_INVAL); 1302 aclen = UGETW(acdp->wTotalLength); 1303 if (offs + aclen > size) 1304 return (USBD_INVAL); 1305 1306 if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC) && 1307 UGETW(acdp->bcdADC) != UAUDIO_VERSION) 1308 return (USBD_INVAL); 1309 1310 sc->sc_audio_rev = UGETW(acdp->bcdADC); 1311 DPRINTFN(2,("uaudio_identify: found AC header, vers=%03x, len=%d\n", 1312 sc->sc_audio_rev, aclen)); 1313 1314 sc->sc_nullalt = -1; 1315 1316 /* Scan through all the AC specific descriptors */ 1317 ibufend = ibuf + aclen; 1318 dp = (usb_descriptor_t *)ibuf; 1319 ndps = 0; 1320 memset(dps, 0, sizeof dps); 1321 for (;;) { 1322 ibuf += dp->bLength; 1323 if (ibuf >= ibufend) 1324 break; 1325 dp = (usb_descriptor_t *)ibuf; 1326 if (ibuf + dp->bLength > ibufend) 1327 return (USBD_INVAL); 1328 if (dp->bDescriptorType != UDESC_CS_INTERFACE) { 1329 printf("uaudio_identify: skip desc type=0x%02x\n", 1330 dp->bDescriptorType); 1331 continue; 1332 } 1333 i = ((struct usb_audio_input_terminal *)dp)->bTerminalId; 1334 dps[i] = dp; 1335 if (i > ndps) 1336 ndps = i; 1337 } 1338 ndps++; 1339 1340 for (i = 0; i < ndps; i++) { 1341 dp = dps[i]; 1342 if (dp == NULL) 1343 continue; 1344 DPRINTF(("uaudio_identify: subtype=%d\n", 1345 dp->bDescriptorSubtype)); 1346 switch (dp->bDescriptorSubtype) { 1347 case UDESCSUB_AC_HEADER: 1348 printf("uaudio_identify: unexpected AC header\n"); 1349 break; 1350 case UDESCSUB_AC_INPUT: 1351 uaudio_add_input(sc, dp, dps); 1352 break; 1353 case UDESCSUB_AC_OUTPUT: 1354 uaudio_add_output(sc, dp, dps); 1355 break; 1356 case UDESCSUB_AC_MIXER: 1357 uaudio_add_mixer(sc, dp, dps); 1358 break; 1359 case UDESCSUB_AC_SELECTOR: 1360 uaudio_add_selector(sc, dp, dps); 1361 break; 1362 case UDESCSUB_AC_FEATURE: 1363 uaudio_add_feature(sc, dp, dps); 1364 break; 1365 case UDESCSUB_AC_PROCESSING: 1366 uaudio_add_processing(sc, dp, dps); 1367 break; 1368 case UDESCSUB_AC_EXTENSION: 1369 uaudio_add_extension(sc, dp, dps); 1370 break; 1371 default: 1372 printf("uaudio_identify: bad AC desc subtype=0x%02x\n", 1373 dp->bDescriptorSubtype); 1374 break; 1375 } 1376 } 1377 return (USBD_NORMAL_COMPLETION); 1378 } 1379 1380 int 1381 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi) 1382 { 1383 struct uaudio_softc *sc = addr; 1384 struct mixerctl *mc; 1385 int n, nctls; 1386 1387 DPRINTFN(2,("uaudio_query_devinfo: index=%d\n", mi->index)); 1388 if (sc->sc_dying) 1389 return (EIO); 1390 1391 n = mi->index; 1392 nctls = sc->sc_nctls; 1393 1394 switch (n) { 1395 case UAC_OUTPUT: 1396 mi->type = AUDIO_MIXER_CLASS; 1397 mi->mixer_class = UAC_OUTPUT; 1398 mi->next = mi->prev = AUDIO_MIXER_LAST; 1399 strcpy(mi->label.name, AudioCoutputs); 1400 return (0); 1401 case UAC_INPUT: 1402 mi->type = AUDIO_MIXER_CLASS; 1403 mi->mixer_class = UAC_INPUT; 1404 mi->next = mi->prev = AUDIO_MIXER_LAST; 1405 strcpy(mi->label.name, AudioCinputs); 1406 return (0); 1407 case UAC_EQUAL: 1408 mi->type = AUDIO_MIXER_CLASS; 1409 mi->mixer_class = UAC_EQUAL; 1410 mi->next = mi->prev = AUDIO_MIXER_LAST; 1411 strcpy(mi->label.name, AudioCequalization); 1412 return (0); 1413 default: 1414 break; 1415 } 1416 1417 n -= UAC_NCLASSES; 1418 if (n < 0 || n >= nctls) 1419 return (ENXIO); 1420 1421 mc = &sc->sc_ctls[n]; 1422 strncpy(mi->label.name, mc->ctlname, MAX_AUDIO_DEV_LEN); 1423 mi->mixer_class = mc->class; 1424 mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */ 1425 switch (mc->type) { 1426 case MIX_ON_OFF: 1427 mi->type = AUDIO_MIXER_ENUM; 1428 mi->un.e.num_mem = 2; 1429 strcpy(mi->un.e.member[0].label.name, AudioNoff); 1430 mi->un.e.member[0].ord = 0; 1431 strcpy(mi->un.e.member[1].label.name, AudioNon); 1432 mi->un.e.member[1].ord = 1; 1433 break; 1434 default: 1435 mi->type = AUDIO_MIXER_VALUE; 1436 strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN); 1437 mi->un.v.num_channels = mc->nchan; 1438 mi->un.v.delta = mc->delta; 1439 break; 1440 } 1441 return (0); 1442 } 1443 1444 int 1445 uaudio_open(void *addr, int flags) 1446 { 1447 struct uaudio_softc *sc = addr; 1448 1449 DPRINTF(("uaudio_open: sc=%p\n", sc)); 1450 if (sc->sc_dying) 1451 return (EIO); 1452 1453 if (sc->sc_mode == 0) 1454 return (ENXIO); 1455 1456 if (flags & FREAD) { 1457 if ((sc->sc_mode & AUMODE_RECORD) == 0) 1458 return (EACCES); 1459 sc->sc_recchan.intr = NULL; 1460 } 1461 1462 if (flags & FWRITE) { 1463 if ((sc->sc_mode & AUMODE_PLAY) == 0) 1464 return (EACCES); 1465 sc->sc_playchan.intr = NULL; 1466 } 1467 1468 return (0); 1469 } 1470 1471 /* 1472 * Close function is called at splaudio(). 1473 */ 1474 void 1475 uaudio_close(void *addr) 1476 { 1477 struct uaudio_softc *sc = addr; 1478 1479 DPRINTF(("uaudio_close: sc=%p\n", sc)); 1480 uaudio_halt_in_dma(sc); 1481 uaudio_halt_out_dma(sc); 1482 1483 sc->sc_playchan.intr = sc->sc_recchan.intr = NULL; 1484 } 1485 1486 int 1487 uaudio_drain(void *addr) 1488 { 1489 struct uaudio_softc *sc = addr; 1490 1491 usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES); 1492 1493 return (0); 1494 } 1495 1496 int 1497 uaudio_halt_out_dma(void *addr) 1498 { 1499 struct uaudio_softc *sc = addr; 1500 1501 DPRINTF(("uaudio_halt_out_dma: enter\n")); 1502 if (sc->sc_playchan.pipe != NULL) { 1503 uaudio_chan_close(sc, &sc->sc_playchan); 1504 sc->sc_playchan.pipe = NULL; 1505 uaudio_chan_free_buffers(sc, &sc->sc_playchan); 1506 } 1507 return (0); 1508 } 1509 1510 int 1511 uaudio_halt_in_dma(void *addr) 1512 { 1513 struct uaudio_softc *sc = addr; 1514 1515 DPRINTF(("uaudio_halt_in_dma: enter\n")); 1516 if (sc->sc_recchan.pipe != NULL) { 1517 uaudio_chan_close(sc, &sc->sc_recchan); 1518 sc->sc_recchan.pipe = NULL; 1519 uaudio_chan_free_buffers(sc, &sc->sc_recchan); 1520 } 1521 return (0); 1522 } 1523 1524 int 1525 uaudio_getdev(void *addr, struct audio_device *retp) 1526 { 1527 struct uaudio_softc *sc = addr; 1528 1529 DPRINTF(("uaudio_mixer_getdev:\n")); 1530 if (sc->sc_dying) 1531 return (EIO); 1532 1533 *retp = uaudio_device; 1534 return (0); 1535 } 1536 1537 /* 1538 * Make sure the block size is large enough to hold all outstanding transfers. 1539 */ 1540 int 1541 uaudio_round_blocksize(void *addr, int blk) 1542 { 1543 struct uaudio_softc *sc = addr; 1544 int bpf; 1545 1546 DPRINTF(("uaudio_round_blocksize: p.bpf=%d r.bpf=%d\n", 1547 sc->sc_playchan.bytes_per_frame, 1548 sc->sc_recchan.bytes_per_frame)); 1549 if (sc->sc_playchan.bytes_per_frame > sc->sc_recchan.bytes_per_frame) { 1550 bpf = sc->sc_playchan.bytes_per_frame 1551 + sc->sc_playchan.sample_size; 1552 } else { 1553 bpf = sc->sc_recchan.bytes_per_frame 1554 + sc->sc_recchan.sample_size; 1555 } 1556 /* XXX */ 1557 bpf *= UAUDIO_NFRAMES * UAUDIO_NCHANBUFS; 1558 1559 bpf = (bpf + 15) &~ 15; 1560 1561 if (blk < bpf) 1562 blk = bpf; 1563 1564 #ifdef DIAGNOSTIC 1565 if (blk <= 0) { 1566 printf("uaudio_round_blocksize: blk=%d\n", blk); 1567 blk = 512; 1568 } 1569 #endif 1570 1571 DPRINTFN(1,("uaudio_round_blocksize: blk=%d\n", blk)); 1572 return (blk); 1573 } 1574 1575 int 1576 uaudio_get_props(void *addr) 1577 { 1578 return (AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT); 1579 1580 } 1581 1582 int 1583 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue, 1584 int wIndex, int len) 1585 { 1586 usb_device_request_t req; 1587 u_int8_t data[4]; 1588 usbd_status err; 1589 int val; 1590 1591 if (wValue == -1) 1592 return (0); 1593 1594 req.bmRequestType = type; 1595 req.bRequest = which; 1596 USETW(req.wValue, wValue); 1597 USETW(req.wIndex, wIndex); 1598 USETW(req.wLength, len); 1599 DPRINTFN(2,("uaudio_get: type=0x%02x req=0x%02x wValue=0x%04x " 1600 "wIndex=0x%04x len=%d\n", 1601 type, which, wValue, wIndex, len)); 1602 err = usbd_do_request(sc->sc_udev, &req, data); 1603 if (err) { 1604 DPRINTF(("uaudio_get: err=%s\n", usbd_errstr(err))); 1605 return (-1); 1606 } 1607 switch (len) { 1608 case 1: 1609 val = data[0]; 1610 break; 1611 case 2: 1612 val = data[0] | (data[1] << 8); 1613 break; 1614 default: 1615 DPRINTF(("uaudio_get: bad length=%d\n", len)); 1616 return (-1); 1617 } 1618 DPRINTFN(2,("uaudio_get: val=%d\n", val)); 1619 return (val); 1620 } 1621 1622 void 1623 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue, 1624 int wIndex, int len, int val) 1625 { 1626 usb_device_request_t req; 1627 u_int8_t data[4]; 1628 usbd_status err; 1629 1630 if (wValue == -1) 1631 return; 1632 1633 req.bmRequestType = type; 1634 req.bRequest = which; 1635 USETW(req.wValue, wValue); 1636 USETW(req.wIndex, wIndex); 1637 USETW(req.wLength, len); 1638 switch (len) { 1639 case 1: 1640 data[0] = val; 1641 break; 1642 case 2: 1643 data[0] = val; 1644 data[1] = val >> 8; 1645 break; 1646 default: 1647 return; 1648 } 1649 DPRINTFN(2,("uaudio_set: type=0x%02x req=0x%02x wValue=0x%04x " 1650 "wIndex=0x%04x len=%d, val=%d\n", 1651 type, which, wValue, wIndex, len, val & 0xffff)); 1652 err = usbd_do_request(sc->sc_udev, &req, data); 1653 #ifdef UAUDIO_DEBUG 1654 if (err) 1655 DPRINTF(("uaudio_set: err=%d\n", err)); 1656 #endif 1657 } 1658 1659 int 1660 uaudio_signext(int type, int val) 1661 { 1662 if (!MIX_UNSIGNED(type)) { 1663 if (MIX_SIZE(type) == 2) 1664 val = (int16_t)val; 1665 else 1666 val = (int8_t)val; 1667 } 1668 return (val); 1669 } 1670 1671 int 1672 uaudio_value2bsd(struct mixerctl *mc, int val) 1673 { 1674 DPRINTFN(5, ("uaudio_value2bsd: type=%03x val=%d min=%d max=%d ", 1675 mc->type, val, mc->minval, mc->maxval)); 1676 if (mc->type == MIX_ON_OFF) 1677 val = (val != 0); 1678 else 1679 val = ((uaudio_signext(mc->type, val) - mc->minval) * 256 1680 + mc->mul/2) / mc->mul; 1681 DPRINTFN(5, ("val'=%d\n", val)); 1682 return (val); 1683 } 1684 1685 int 1686 uaudio_bsd2value(struct mixerctl *mc, int val) 1687 { 1688 DPRINTFN(5,("uaudio_bsd2value: type=%03x val=%d min=%d max=%d ", 1689 mc->type, val, mc->minval, mc->maxval)); 1690 if (mc->type == MIX_ON_OFF) 1691 val = (val != 0); 1692 else 1693 val = (val + mc->delta/2) * mc->mul / 256 + mc->minval; 1694 DPRINTFN(5, ("val'=%d\n", val)); 1695 return (val); 1696 } 1697 1698 int 1699 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc, 1700 int chan) 1701 { 1702 int val; 1703 1704 DPRINTFN(5,("uaudio_ctl_get: which=%d chan=%d\n", which, chan)); 1705 val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan], 1706 mc->wIndex, MIX_SIZE(mc->type)); 1707 return (uaudio_value2bsd(mc, val)); 1708 } 1709 1710 void 1711 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc, 1712 int chan, int val) 1713 { 1714 val = uaudio_bsd2value(mc, val); 1715 uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan], 1716 mc->wIndex, MIX_SIZE(mc->type), val); 1717 } 1718 1719 int 1720 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp) 1721 { 1722 struct uaudio_softc *sc = addr; 1723 struct mixerctl *mc; 1724 int i, n, vals[MIX_MAX_CHAN], val; 1725 1726 DPRINTFN(2,("uaudio_mixer_get_port: index=%d\n", cp->dev)); 1727 1728 if (sc->sc_dying) 1729 return (EIO); 1730 1731 n = cp->dev - UAC_NCLASSES; 1732 if (n < 0 || n >= sc->sc_nctls) 1733 return (ENXIO); 1734 mc = &sc->sc_ctls[n]; 1735 1736 if (mc->type == MIX_ON_OFF) { 1737 if (cp->type != AUDIO_MIXER_ENUM) 1738 return (EINVAL); 1739 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0); 1740 } else { 1741 if (cp->type != AUDIO_MIXER_VALUE) 1742 return (EINVAL); 1743 if (cp->un.value.num_channels != 1 && 1744 cp->un.value.num_channels != mc->nchan) 1745 return (EINVAL); 1746 for (i = 0; i < mc->nchan; i++) 1747 vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i); 1748 if (cp->un.value.num_channels == 1 && mc->nchan != 1) { 1749 for (val = 0, i = 0; i < mc->nchan; i++) 1750 val += vals[i]; 1751 vals[0] = val / mc->nchan; 1752 } 1753 for (i = 0; i < cp->un.value.num_channels; i++) 1754 cp->un.value.level[i] = vals[i]; 1755 } 1756 1757 return (0); 1758 } 1759 1760 int 1761 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp) 1762 { 1763 struct uaudio_softc *sc = addr; 1764 struct mixerctl *mc; 1765 int i, n, vals[MIX_MAX_CHAN]; 1766 1767 DPRINTFN(2,("uaudio_mixer_set_port: index = %d\n", cp->dev)); 1768 if (sc->sc_dying) 1769 return (EIO); 1770 1771 n = cp->dev - UAC_NCLASSES; 1772 if (n < 0 || n >= sc->sc_nctls) 1773 return (ENXIO); 1774 mc = &sc->sc_ctls[n]; 1775 1776 if (mc->type == MIX_ON_OFF) { 1777 if (cp->type != AUDIO_MIXER_ENUM) 1778 return (EINVAL); 1779 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord); 1780 } else { 1781 if (cp->type != AUDIO_MIXER_VALUE) 1782 return (EINVAL); 1783 if (cp->un.value.num_channels == 1) 1784 for (i = 0; i < mc->nchan; i++) 1785 vals[i] = cp->un.value.level[0]; 1786 else if (cp->un.value.num_channels == mc->nchan) 1787 for (i = 0; i < mc->nchan; i++) 1788 vals[i] = cp->un.value.level[i]; 1789 else 1790 return (EINVAL); 1791 for (i = 0; i < mc->nchan; i++) 1792 uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]); 1793 } 1794 return (0); 1795 } 1796 1797 int 1798 uaudio_trigger_input(void *addr, void *start, void *end, int blksize, 1799 void (*intr)(void *), void *arg, 1800 struct audio_params *param) 1801 { 1802 struct uaudio_softc *sc = addr; 1803 struct chan *ch = &sc->sc_recchan; 1804 usbd_status err; 1805 int i, s; 1806 1807 if (sc->sc_dying) 1808 return (EIO); 1809 1810 DPRINTFN(3,("uaudio_trigger_input: sc=%p start=%p end=%p " 1811 "blksize=%d\n", sc, start, end, blksize)); 1812 1813 uaudio_chan_set_param(ch, start, end, blksize); 1814 DPRINTFN(3,("uaudio_trigger_input: sample_size=%d bytes/frame=%d " 1815 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame, 1816 ch->fraction)); 1817 1818 err = uaudio_chan_alloc_buffers(sc, ch); 1819 if (err) 1820 return (EIO); 1821 1822 err = uaudio_chan_open(sc, ch); 1823 if (err) { 1824 uaudio_chan_free_buffers(sc, ch); 1825 return (EIO); 1826 } 1827 1828 ch->intr = intr; 1829 ch->arg = arg; 1830 1831 s = splusb(); 1832 for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX -1 shouldn't be needed */ 1833 uaudio_chan_rtransfer(ch); 1834 splx(s); 1835 1836 return (0); 1837 } 1838 1839 int 1840 uaudio_trigger_output(void *addr, void *start, void *end, int blksize, 1841 void (*intr)(void *), void *arg, 1842 struct audio_params *param) 1843 { 1844 struct uaudio_softc *sc = addr; 1845 struct chan *ch = &sc->sc_playchan; 1846 usbd_status err; 1847 int i, s; 1848 1849 if (sc->sc_dying) 1850 return (EIO); 1851 1852 DPRINTFN(3,("uaudio_trigger_output: sc=%p start=%p end=%p " 1853 "blksize=%d\n", sc, start, end, blksize)); 1854 1855 uaudio_chan_set_param(ch, start, end, blksize); 1856 DPRINTFN(3,("uaudio_trigger_output: sample_size=%d bytes/frame=%d " 1857 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame, 1858 ch->fraction)); 1859 1860 err = uaudio_chan_alloc_buffers(sc, ch); 1861 if (err) 1862 return (EIO); 1863 1864 err = uaudio_chan_open(sc, ch); 1865 if (err) { 1866 uaudio_chan_free_buffers(sc, ch); 1867 return (EIO); 1868 } 1869 1870 ch->intr = intr; 1871 ch->arg = arg; 1872 1873 s = splusb(); 1874 for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX */ 1875 uaudio_chan_ptransfer(ch); 1876 splx(s); 1877 1878 return (0); 1879 } 1880 1881 /* Set up a pipe for a channel. */ 1882 usbd_status 1883 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch) 1884 { 1885 struct as_info *as = &sc->sc_alts[ch->altidx]; 1886 int endpt = as->edesc->bEndpointAddress; 1887 usbd_status err; 1888 1889 DPRINTF(("uaudio_chan_open: endpt=0x%02x, speed=%d, alt=%d\n", 1890 endpt, ch->sample_rate, as->alt)); 1891 1892 /* Set alternate interface corresponding to the mode. */ 1893 err = usbd_set_interface(as->ifaceh, as->alt); 1894 if (err) 1895 return (err); 1896 1897 /* Some devices do not support this request, so ignore errors. */ 1898 #ifdef UAUDIO_DEBUG 1899 err = uaudio_set_speed(sc, endpt, ch->sample_rate); 1900 if (err) 1901 DPRINTF(("uaudio_chan_open: set_speed failed err=%s\n", 1902 usbd_errstr(err))); 1903 #else 1904 (void)uaudio_set_speed(sc, endpt, ch->sample_rate); 1905 #endif 1906 1907 DPRINTF(("uaudio_chan_open: create pipe to 0x%02x\n", endpt)); 1908 err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->pipe); 1909 return (err); 1910 } 1911 1912 void 1913 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch) 1914 { 1915 struct as_info *as = &sc->sc_alts[ch->altidx]; 1916 1917 as->sc_busy = 0; 1918 if (sc->sc_nullalt >= 0) { 1919 DPRINTF(("uaudio_chan_close: set null alt=%d\n", 1920 sc->sc_nullalt)); 1921 usbd_set_interface(as->ifaceh, sc->sc_nullalt); 1922 } 1923 usbd_abort_pipe(ch->pipe); 1924 usbd_close_pipe(ch->pipe); 1925 } 1926 1927 usbd_status 1928 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch) 1929 { 1930 usbd_xfer_handle xfer; 1931 void *buf; 1932 int i, size; 1933 1934 size = (ch->bytes_per_frame + ch->sample_size) * UAUDIO_NFRAMES; 1935 for (i = 0; i < UAUDIO_NCHANBUFS; i++) { 1936 xfer = usbd_alloc_xfer(sc->sc_udev); 1937 if (xfer == 0) 1938 goto bad; 1939 ch->chanbufs[i].xfer = xfer; 1940 buf = usbd_alloc_buffer(xfer, size); 1941 if (buf == 0) { 1942 i++; 1943 goto bad; 1944 } 1945 ch->chanbufs[i].buffer = buf; 1946 ch->chanbufs[i].chan = ch; 1947 } 1948 1949 return (USBD_NORMAL_COMPLETION); 1950 1951 bad: 1952 while (--i >= 0) 1953 /* implicit buffer free */ 1954 usbd_free_xfer(ch->chanbufs[i].xfer); 1955 return (USBD_NOMEM); 1956 } 1957 1958 void 1959 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch) 1960 { 1961 int i; 1962 1963 for (i = 0; i < UAUDIO_NCHANBUFS; i++) 1964 usbd_free_xfer(ch->chanbufs[i].xfer); 1965 } 1966 1967 /* Called at splusb() */ 1968 void 1969 uaudio_chan_ptransfer(struct chan *ch) 1970 { 1971 struct chanbuf *cb; 1972 int i, n, size, residue, total; 1973 1974 if (ch->sc->sc_dying) 1975 return; 1976 1977 /* Pick the next channel buffer. */ 1978 cb = &ch->chanbufs[ch->curchanbuf]; 1979 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS) 1980 ch->curchanbuf = 0; 1981 1982 /* Compute the size of each frame in the next transfer. */ 1983 residue = ch->residue; 1984 total = 0; 1985 for (i = 0; i < UAUDIO_NFRAMES; i++) { 1986 size = ch->bytes_per_frame; 1987 residue += ch->fraction; 1988 if (residue >= USB_FRAMES_PER_SECOND) { 1989 if ((ch->sc->sc_altflags & UA_NOFRAC) == 0) 1990 size += ch->sample_size; 1991 residue -= USB_FRAMES_PER_SECOND; 1992 } 1993 cb->sizes[i] = size; 1994 total += size; 1995 } 1996 ch->residue = residue; 1997 cb->size = total; 1998 1999 /* 2000 * Transfer data from upper layer buffer to channel buffer, taking 2001 * care of wrapping the upper layer buffer. 2002 */ 2003 n = min(total, ch->end - ch->cur); 2004 memcpy(cb->buffer, ch->cur, n); 2005 ch->cur += n; 2006 if (ch->cur >= ch->end) 2007 ch->cur = ch->start; 2008 if (total > n) { 2009 total -= n; 2010 memcpy(cb->buffer + n, ch->cur, total); 2011 ch->cur += total; 2012 } 2013 2014 #ifdef UAUDIO_DEBUG 2015 if (uaudiodebug > 8) { 2016 DPRINTF(("uaudio_chan_ptransfer: buffer=%p, residue=0.%03d\n", 2017 cb->buffer, ch->residue)); 2018 for (i = 0; i < UAUDIO_NFRAMES; i++) { 2019 DPRINTF((" [%d] length %d\n", i, cb->sizes[i])); 2020 } 2021 } 2022 #endif 2023 2024 DPRINTFN(5,("uaudio_chan_transfer: ptransfer xfer=%p\n", cb->xfer)); 2025 /* Fill the request */ 2026 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes, 2027 UAUDIO_NFRAMES, USBD_NO_COPY, 2028 uaudio_chan_pintr); 2029 2030 (void)usbd_transfer(cb->xfer); 2031 } 2032 2033 void 2034 uaudio_chan_pintr(usbd_xfer_handle xfer, usbd_private_handle priv, 2035 usbd_status status) 2036 { 2037 struct chanbuf *cb = priv; 2038 struct chan *ch = cb->chan; 2039 u_int32_t count; 2040 int s; 2041 2042 /* Return if we are aborting. */ 2043 if (status == USBD_CANCELLED) 2044 return; 2045 2046 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 2047 DPRINTFN(5,("uaudio_chan_pintr: count=%d, transferred=%d\n", 2048 count, ch->transferred)); 2049 #ifdef DIAGNOSTIC 2050 if (count != cb->size) { 2051 printf("uaudio_chan_pintr: count(%d) != size(%d)\n", 2052 count, cb->size); 2053 } 2054 #endif 2055 2056 ch->transferred += cb->size; 2057 s = splaudio(); 2058 /* Call back to upper layer */ 2059 while (ch->transferred >= ch->blksize) { 2060 ch->transferred -= ch->blksize; 2061 DPRINTFN(5,("uaudio_chan_pintr: call %p(%p)\n", 2062 ch->intr, ch->arg)); 2063 ch->intr(ch->arg); 2064 } 2065 splx(s); 2066 2067 /* start next transfer */ 2068 uaudio_chan_ptransfer(ch); 2069 } 2070 2071 /* Called at splusb() */ 2072 void 2073 uaudio_chan_rtransfer(struct chan *ch) 2074 { 2075 struct chanbuf *cb; 2076 int i, size, residue, total; 2077 2078 if (ch->sc->sc_dying) 2079 return; 2080 2081 /* Pick the next channel buffer. */ 2082 cb = &ch->chanbufs[ch->curchanbuf]; 2083 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS) 2084 ch->curchanbuf = 0; 2085 2086 /* Compute the size of each frame in the next transfer. */ 2087 residue = ch->residue; 2088 total = 0; 2089 for (i = 0; i < UAUDIO_NFRAMES; i++) { 2090 size = ch->bytes_per_frame; 2091 cb->sizes[i] = size; 2092 cb->offsets[i] = total; 2093 total += size; 2094 } 2095 ch->residue = residue; 2096 cb->size = total; 2097 2098 #ifdef UAUDIO_DEBUG 2099 if (uaudiodebug > 8) { 2100 DPRINTF(("uaudio_chan_rtransfer: buffer=%p, residue=0.%03d\n", 2101 cb->buffer, ch->residue)); 2102 for (i = 0; i < UAUDIO_NFRAMES; i++) { 2103 DPRINTF((" [%d] length %d\n", i, cb->sizes[i])); 2104 } 2105 } 2106 #endif 2107 2108 DPRINTFN(5,("uaudio_chan_rtransfer: transfer xfer=%p\n", cb->xfer)); 2109 /* Fill the request */ 2110 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes, 2111 UAUDIO_NFRAMES, USBD_NO_COPY, 2112 uaudio_chan_rintr); 2113 2114 (void)usbd_transfer(cb->xfer); 2115 } 2116 2117 void 2118 uaudio_chan_rintr(usbd_xfer_handle xfer, usbd_private_handle priv, 2119 usbd_status status) 2120 { 2121 struct chanbuf *cb = priv; 2122 struct chan *ch = cb->chan; 2123 u_int32_t count; 2124 int s, i, n, frsize; 2125 2126 /* Return if we are aborting. */ 2127 if (status == USBD_CANCELLED) 2128 return; 2129 2130 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 2131 DPRINTFN(5,("uaudio_chan_rintr: count=%d, transferred=%d\n", 2132 count, ch->transferred)); 2133 2134 /* count < cb->size is normal for asynchronous source */ 2135 #ifdef DIAGNOSTIC 2136 if (count > cb->size) { 2137 printf("uaudio_chan_rintr: count(%d) > size(%d)\n", 2138 count, cb->size); 2139 } 2140 #endif 2141 2142 /* 2143 * Transfer data from channel buffer to upper layer buffer, taking 2144 * care of wrapping the upper layer buffer. 2145 */ 2146 for(i = 0; i < UAUDIO_NFRAMES; i++) { 2147 frsize = cb->sizes[i]; 2148 n = min(frsize, ch->end - ch->cur); 2149 memcpy(ch->cur, cb->buffer + cb->offsets[i], n); 2150 ch->cur += n; 2151 if (ch->cur >= ch->end) 2152 ch->cur = ch->start; 2153 if (frsize > n) { 2154 memcpy(ch->cur, cb->buffer + cb->offsets[i] + n, 2155 frsize - n); 2156 ch->cur += frsize - n; 2157 } 2158 } 2159 2160 /* Call back to upper layer */ 2161 ch->transferred += count; 2162 s = splaudio(); 2163 while (ch->transferred >= ch->blksize) { 2164 ch->transferred -= ch->blksize; 2165 DPRINTFN(5,("uaudio_chan_rintr: call %p(%p)\n", 2166 ch->intr, ch->arg)); 2167 ch->intr(ch->arg); 2168 } 2169 splx(s); 2170 2171 /* start next transfer */ 2172 uaudio_chan_rtransfer(ch); 2173 } 2174 2175 void 2176 uaudio_chan_init(struct chan *ch, int altidx, const struct audio_params *param, 2177 int maxpktsize) 2178 { 2179 int samples_per_frame, sample_size; 2180 2181 ch->altidx = altidx; 2182 sample_size = param->precision * param->factor * param->hw_channels / 8; 2183 samples_per_frame = param->hw_sample_rate / USB_FRAMES_PER_SECOND; 2184 ch->sample_size = sample_size; 2185 ch->sample_rate = param->hw_sample_rate; 2186 if (maxpktsize == 0) { 2187 ch->fraction = param->hw_sample_rate % USB_FRAMES_PER_SECOND; 2188 ch->bytes_per_frame = samples_per_frame * sample_size; 2189 } else { 2190 ch->fraction = 0; 2191 ch->bytes_per_frame = maxpktsize; 2192 } 2193 ch->residue = 0; 2194 } 2195 2196 void 2197 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize) 2198 { 2199 ch->start = start; 2200 ch->end = end; 2201 ch->cur = start; 2202 ch->blksize = blksize; 2203 ch->transferred = 0; 2204 2205 ch->curchanbuf = 0; 2206 } 2207 2208 void 2209 uaudio_get_minmax_rates(int nalts, const struct as_info *alts, 2210 const struct audio_params *p, int mode, 2211 u_long *min, u_long *max) 2212 { 2213 int i, j; 2214 struct usb_audio_streaming_type1_descriptor *a1d; 2215 2216 *min = ULONG_MAX; 2217 *max = 0; 2218 for (i = 0; i < nalts; i++) { 2219 a1d = alts[i].asf1desc; 2220 if (alts[i].sc_busy) 2221 continue; 2222 if (p->hw_channels != a1d->bNrChannels) 2223 continue; 2224 if (p->hw_precision != a1d->bBitResolution) 2225 continue; 2226 if (p->hw_encoding != alts[i].encoding) 2227 continue; 2228 if (mode != UE_GET_DIR(alts[i].edesc->bEndpointAddress)) 2229 continue; 2230 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) { 2231 DPRINTFN(2,("uaudio_get_minmax_rates: cont %d-%d\n", 2232 UA_SAMP_LO(a1d), UA_SAMP_HI(a1d))); 2233 if (UA_SAMP_LO(a1d) < *min) 2234 *min = UA_SAMP_LO(a1d); 2235 if (UA_SAMP_HI(a1d) > *max) 2236 *max = UA_SAMP_HI(a1d); 2237 } else { 2238 for (j = 0; j < a1d->bSamFreqType; j++) { 2239 DPRINTFN(2,("uaudio_get_minmax_rates: disc #%d: %d\n", 2240 j, UA_GETSAMP(a1d, j))); 2241 if (UA_GETSAMP(a1d, j) < *min) 2242 *min = UA_GETSAMP(a1d, j); 2243 if (UA_GETSAMP(a1d, j) > *max) 2244 *max = UA_GETSAMP(a1d, j); 2245 } 2246 } 2247 } 2248 } 2249 2250 int 2251 uaudio_match_alt_sub(int nalts, const struct as_info *alts, 2252 const struct audio_params *p, int mode, u_long rate) 2253 { 2254 int i, j; 2255 struct usb_audio_streaming_type1_descriptor *a1d; 2256 2257 DPRINTF(("uaudio_match_alt_sub: search for %luHz %dch\n", 2258 rate, p->hw_channels)); 2259 for (i = 0; i < nalts; i++) { 2260 a1d = alts[i].asf1desc; 2261 if (alts[i].sc_busy) 2262 continue; 2263 if (p->hw_channels != a1d->bNrChannels) 2264 continue; 2265 if (p->hw_precision != a1d->bBitResolution) 2266 continue; 2267 if (p->hw_encoding != alts[i].encoding) 2268 continue; 2269 if (mode != UE_GET_DIR(alts[i].edesc->bEndpointAddress)) 2270 continue; 2271 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) { 2272 DPRINTFN(2,("uaudio_match_alt_sub: cont %d-%d\n", 2273 UA_SAMP_LO(a1d), UA_SAMP_HI(a1d))); 2274 if (UA_SAMP_LO(a1d) <= rate && rate <= UA_SAMP_HI(a1d)) 2275 return i; 2276 } else { 2277 for (j = 0; j < a1d->bSamFreqType; j++) { 2278 DPRINTFN(2,("uaudio_match_alt_sub: disc #%d: %d\n", 2279 j, UA_GETSAMP(a1d, j))); 2280 /* XXX allow for some slack */ 2281 if (UA_GETSAMP(a1d, j) == rate) 2282 return i; 2283 } 2284 } 2285 } 2286 return -1; 2287 } 2288 2289 int 2290 uaudio_match_alt_chan(int nalts, const struct as_info *alts, 2291 struct audio_params *p, int mode) 2292 { 2293 int i, n; 2294 u_long min, max; 2295 u_long rate; 2296 2297 /* Exact match */ 2298 DPRINTF(("uaudio_match_alt_chan: examine %ldHz %dch %dbit.\n", 2299 p->sample_rate, p->hw_channels, p->hw_precision)); 2300 i = uaudio_match_alt_sub(nalts, alts, p, mode, p->sample_rate); 2301 if (i >= 0) 2302 return i; 2303 2304 uaudio_get_minmax_rates(nalts, alts, p, mode, &min, &max); 2305 DPRINTF(("uaudio_match_alt_chan: min=%lu max=%lu\n", min, max)); 2306 if (max <= 0) 2307 return -1; 2308 /* Search for biggers */ 2309 n = 2; 2310 while ((rate = p->sample_rate * n++) <= max) { 2311 i = uaudio_match_alt_sub(nalts, alts, p, mode, rate); 2312 if (i >= 0) { 2313 p->hw_sample_rate = rate; 2314 return i; 2315 } 2316 } 2317 if (p->sample_rate >= min) { 2318 i = uaudio_match_alt_sub(nalts, alts, p, mode, max); 2319 if (i >= 0) { 2320 p->hw_sample_rate = max; 2321 return i; 2322 } 2323 } else { 2324 i = uaudio_match_alt_sub(nalts, alts, p, mode, min); 2325 if (i >= 0) { 2326 p->hw_sample_rate = min; 2327 return i; 2328 } 2329 } 2330 return -1; 2331 } 2332 2333 int 2334 uaudio_match_alt(int nalts, const struct as_info *alts, 2335 struct audio_params *p, int mode) 2336 { 2337 int i, n; 2338 2339 mode = mode == AUMODE_PLAY ? UE_DIR_OUT : UE_DIR_IN; 2340 i = uaudio_match_alt_chan(nalts, alts, p, mode); 2341 if (i >= 0) 2342 return i; 2343 2344 for (n = p->channels + 1; n <= AUDIO_MAX_CHANNELS; n++) { 2345 p->hw_channels = n; 2346 i = uaudio_match_alt_chan(nalts, alts, p, mode); 2347 if (i >= 0) 2348 return i; 2349 } 2350 2351 if (p->channels != 2) 2352 return -1; 2353 p->hw_channels = 1; 2354 return uaudio_match_alt_chan(nalts, alts, p, mode); 2355 } 2356 2357 int 2358 uaudio_set_params(void *addr, int setmode, int usemode, 2359 struct audio_params *play, struct audio_params *rec) 2360 { 2361 struct uaudio_softc *sc = addr; 2362 int flags = sc->sc_altflags; 2363 int factor; 2364 int enc, i; 2365 int paltidx=-1, raltidx=-1; 2366 void (*swcode)(void *, u_char *buf, int cnt); 2367 struct audio_params *p; 2368 int mode; 2369 2370 if (sc->sc_dying) 2371 return (EIO); 2372 2373 if ((usemode == AUMODE_RECORD && sc->sc_recchan.pipe != NULL) 2374 || (usemode == AUMODE_PLAY && sc->sc_playchan.pipe != NULL)) 2375 return (EBUSY); 2376 2377 if (usemode & AUMODE_PLAY && sc->sc_playchan.altidx != -1) 2378 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0; 2379 if (usemode & AUMODE_RECORD && sc->sc_recchan.altidx != -1) 2380 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0; 2381 2382 for (mode = AUMODE_RECORD; mode != -1; 2383 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 2384 if ((setmode & mode) == 0) 2385 continue; 2386 2387 if ((sc->sc_mode & mode) == 0) 2388 continue; 2389 2390 p = (mode == AUMODE_PLAY) ? play : rec; 2391 2392 factor = 1; 2393 swcode = 0; 2394 enc = p->encoding; 2395 switch (enc) { 2396 case AUDIO_ENCODING_SLINEAR_BE: 2397 /* FALLTHROUGH */ 2398 case AUDIO_ENCODING_SLINEAR_LE: 2399 if (enc == AUDIO_ENCODING_SLINEAR_BE 2400 && p->precision == 16 && (flags & HAS_16)) { 2401 swcode = swap_bytes; 2402 enc = AUDIO_ENCODING_SLINEAR_LE; 2403 } else if (p->precision == 8) { 2404 if (flags & HAS_8) { 2405 /* No conversion */ 2406 } else if (flags & HAS_8U) { 2407 swcode = change_sign8; 2408 enc = AUDIO_ENCODING_ULINEAR_LE; 2409 } else if (flags & HAS_16) { 2410 factor = 2; 2411 p->hw_precision = 16; 2412 if (mode == AUMODE_PLAY) 2413 swcode = linear8_to_linear16_le; 2414 else 2415 swcode = linear16_to_linear8_le; 2416 } 2417 } 2418 break; 2419 case AUDIO_ENCODING_ULINEAR_BE: 2420 /* FALLTHROUGH */ 2421 case AUDIO_ENCODING_ULINEAR_LE: 2422 if (p->precision == 16) { 2423 if (enc == AUDIO_ENCODING_ULINEAR_LE) 2424 swcode = change_sign16_le; 2425 else if (mode == AUMODE_PLAY) 2426 swcode = swap_bytes_change_sign16_le; 2427 else 2428 swcode = change_sign16_swap_bytes_le; 2429 enc = AUDIO_ENCODING_SLINEAR_LE; 2430 } else if (p->precision == 8) { 2431 if (flags & HAS_8U) { 2432 /* No conversion */ 2433 } else if (flags & HAS_8) { 2434 swcode = change_sign8; 2435 enc = AUDIO_ENCODING_SLINEAR_LE; 2436 } else if (flags & HAS_16) { 2437 factor = 2; 2438 p->hw_precision = 16; 2439 enc = AUDIO_ENCODING_SLINEAR_LE; 2440 if (mode == AUMODE_PLAY) 2441 swcode = ulinear8_to_slinear16_le; 2442 else 2443 swcode = slinear16_to_ulinear8_le; 2444 } 2445 } 2446 break; 2447 case AUDIO_ENCODING_ULAW: 2448 if (flags & HAS_MULAW) 2449 break; 2450 if (flags & HAS_16) { 2451 if (mode == AUMODE_PLAY) 2452 swcode = mulaw_to_slinear16_le; 2453 else 2454 swcode = slinear16_to_mulaw_le; 2455 factor = 2; 2456 enc = AUDIO_ENCODING_SLINEAR_LE; 2457 p->hw_precision = 16; 2458 } else if (flags & HAS_8U) { 2459 if (mode == AUMODE_PLAY) 2460 swcode = mulaw_to_ulinear8; 2461 else 2462 swcode = ulinear8_to_mulaw; 2463 enc = AUDIO_ENCODING_ULINEAR_LE; 2464 } else if (flags & HAS_8) { 2465 if (mode == AUMODE_PLAY) 2466 swcode = mulaw_to_slinear8; 2467 else 2468 swcode = slinear8_to_mulaw; 2469 enc = AUDIO_ENCODING_SLINEAR_LE; 2470 } else 2471 return (EINVAL); 2472 break; 2473 case AUDIO_ENCODING_ALAW: 2474 if (flags & HAS_ALAW) 2475 break; 2476 if (mode == AUMODE_PLAY && (flags & HAS_16)) { 2477 swcode = alaw_to_slinear16_le; 2478 factor = 2; 2479 enc = AUDIO_ENCODING_SLINEAR_LE; 2480 p->hw_precision = 16; 2481 } else if (flags & HAS_8U) { 2482 if (mode == AUMODE_PLAY) 2483 swcode = alaw_to_ulinear8; 2484 else 2485 swcode = ulinear8_to_alaw; 2486 enc = AUDIO_ENCODING_ULINEAR_LE; 2487 } else if (flags & HAS_8) { 2488 if (mode == AUMODE_PLAY) 2489 swcode = alaw_to_slinear8; 2490 else 2491 swcode = slinear8_to_alaw; 2492 enc = AUDIO_ENCODING_SLINEAR_LE; 2493 } else 2494 return (EINVAL); 2495 break; 2496 default: 2497 return (EINVAL); 2498 } 2499 /* XXX do some other conversions... */ 2500 2501 DPRINTF(("uaudio_set_params: chan=%d prec=%d enc=%d rate=%ld\n", 2502 p->channels, p->hw_precision, enc, p->sample_rate)); 2503 2504 p->hw_encoding = enc; 2505 i = uaudio_match_alt(sc->sc_nalts, sc->sc_alts, p, mode); 2506 if (i < 0) 2507 return (EINVAL); 2508 2509 p->sw_code = swcode; 2510 p->factor = factor; 2511 if (usemode & mode) { 2512 if (mode == AUMODE_PLAY) { 2513 paltidx = i; 2514 sc->sc_alts[i].sc_busy = 1; 2515 } else { 2516 raltidx = i; 2517 sc->sc_alts[i].sc_busy = 1; 2518 } 2519 } 2520 } 2521 2522 if ((usemode & AUMODE_PLAY) /*&& paltidx != sc->sc_playchan.altidx*/) { 2523 /* XXX abort transfer if currently happening? */ 2524 uaudio_chan_init(&sc->sc_playchan, paltidx, play, 0); 2525 } 2526 if ((usemode & AUMODE_RECORD) /*&& raltidx != sc->sc_recchan.altidx*/) { 2527 /* XXX abort transfer if currently happening? */ 2528 uaudio_chan_init(&sc->sc_recchan, raltidx, rec, 2529 UGETW(sc->sc_alts[raltidx].edesc->wMaxPacketSize)); 2530 } 2531 2532 DPRINTF(("uaudio_set_params: use altidx=p%d/r%d, altno=p%d/r%d\n", 2533 sc->sc_playchan.altidx, sc->sc_recchan.altidx, 2534 (sc->sc_playchan.altidx >= 0) 2535 ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting 2536 : -1, 2537 (sc->sc_recchan.altidx >= 0) 2538 ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting 2539 : -1)); 2540 2541 return (0); 2542 } 2543 2544 usbd_status 2545 uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed) 2546 { 2547 usb_device_request_t req; 2548 u_int8_t data[3]; 2549 2550 DPRINTFN(5,("uaudio_set_speed: endpt=%d speed=%u\n", endpt, speed)); 2551 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT; 2552 req.bRequest = SET_CUR; 2553 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0); 2554 USETW(req.wIndex, endpt); 2555 USETW(req.wLength, 3); 2556 data[0] = speed; 2557 data[1] = speed >> 8; 2558 data[2] = speed >> 16; 2559 2560 return (usbd_do_request(sc->sc_udev, &req, data)); 2561 } 2562