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