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