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