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