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