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