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