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