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