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