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