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