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