uaudio.c revision 1.161 1 /* $NetBSD: uaudio.c,v 1.161 2019/06/06 12:59:33 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2012 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, and Matthew R. Green (mrg (at) eterna.com.au).
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * USB audio specs: http://www.usb.org/developers/docs/devclass_docs/audio10.pdf
35 * http://www.usb.org/developers/docs/devclass_docs/frmts10.pdf
36 * http://www.usb.org/developers/docs/devclass_docs/termt10.pdf
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: uaudio.c,v 1.161 2019/06/06 12:59:33 isaki Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_usb.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/file.h>
53 #include <sys/reboot.h> /* for bootverbose */
54 #include <sys/select.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/poll.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #include <sys/cpu.h>
61 #include <sys/atomic.h>
62
63 #include <sys/audioio.h>
64 #include <dev/audio/audio_if.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdivar.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usb_quirks.h>
71
72 #include <dev/usb/usbdevs.h>
73
74 #include <dev/usb/uaudioreg.h>
75
76 /* #define UAUDIO_DEBUG */
77 /* #define UAUDIO_MULTIPLE_ENDPOINTS */
78 #ifdef UAUDIO_DEBUG
79 #define DPRINTF(x,y...) do { \
80 if (uaudiodebug) { \
81 struct lwp *l = curlwp; \
82 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
83 } \
84 } while (0)
85 #define DPRINTFN_CLEAN(n,x...) do { \
86 if (uaudiodebug > (n)) \
87 printf(x); \
88 } while (0)
89 #define DPRINTFN(n,x,y...) do { \
90 if (uaudiodebug > (n)) { \
91 struct lwp *l = curlwp; \
92 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
93 } \
94 } while (0)
95 int uaudiodebug = 0;
96 #else
97 #define DPRINTF(x,y...)
98 #define DPRINTFN_CLEAN(n,x...)
99 #define DPRINTFN(n,x,y...)
100 #endif
101
102 #define UAUDIO_NCHANBUFS 6 /* number of outstanding request */
103 #define UAUDIO_NFRAMES 10 /* ms of sound in each request */
104
105
106 #define MIX_MAX_CHAN 8
107 struct mixerctl {
108 uint16_t wValue[MIX_MAX_CHAN]; /* using nchan */
109 uint16_t wIndex;
110 uint8_t nchan;
111 uint8_t type;
112 #define MIX_ON_OFF 1
113 #define MIX_SIGNED_16 2
114 #define MIX_UNSIGNED_16 3
115 #define MIX_SIGNED_8 4
116 #define MIX_SELECTOR 5
117 #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1)
118 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
119 int minval, maxval;
120 u_int delta;
121 u_int mul;
122 uint8_t class;
123 char ctlname[MAX_AUDIO_DEV_LEN];
124 const char *ctlunit;
125 };
126 #define MAKE(h,l) (((h) << 8) | (l))
127
128 struct as_info {
129 uint8_t alt;
130 uint8_t encoding;
131 uint8_t attributes; /* Copy of bmAttributes of
132 * usb_audio_streaming_endpoint_descriptor
133 */
134 struct usbd_interface * ifaceh;
135 const usb_interface_descriptor_t *idesc;
136 const usb_endpoint_descriptor_audio_t *edesc;
137 const usb_endpoint_descriptor_audio_t *edesc1;
138 const struct usb_audio_streaming_type1_descriptor *asf1desc;
139 struct audio_format *aformat;
140 int sc_busy; /* currently used */
141 };
142
143 struct chan {
144 void (*intr)(void *); /* DMA completion intr handler */
145 void *arg; /* arg for intr() */
146 struct usbd_pipe *pipe;
147 struct usbd_pipe *sync_pipe;
148
149 u_int sample_size;
150 u_int sample_rate;
151 u_int bytes_per_frame;
152 u_int fraction; /* fraction/1000 is the extra samples/frame */
153 u_int residue; /* accumulates the fractional samples */
154
155 u_char *start; /* upper layer buffer start */
156 u_char *end; /* upper layer buffer end */
157 u_char *cur; /* current position in upper layer buffer */
158 int blksize; /* chunk size to report up */
159 int transferred; /* transferred bytes not reported up */
160
161 int altidx; /* currently used altidx */
162
163 int curchanbuf;
164 struct chanbuf {
165 struct chan *chan;
166 struct usbd_xfer *xfer;
167 u_char *buffer;
168 uint16_t sizes[UAUDIO_NFRAMES];
169 uint16_t offsets[UAUDIO_NFRAMES];
170 uint16_t size;
171 } chanbufs[UAUDIO_NCHANBUFS];
172
173 struct uaudio_softc *sc; /* our softc */
174 };
175
176 /*
177 * The MI USB audio subsystem is now MP-SAFE and expects sc_intr_lock to be
178 * held on entry the callbacks passed to uaudio_trigger_{in,out}put
179 */
180 struct uaudio_softc {
181 device_t sc_dev; /* base device */
182 kmutex_t sc_lock;
183 kmutex_t sc_intr_lock;
184 struct usbd_device *sc_udev; /* USB device */
185 int sc_ac_iface; /* Audio Control interface */
186 struct usbd_interface * sc_ac_ifaceh;
187 struct chan sc_playchan; /* play channel */
188 struct chan sc_recchan; /* record channel */
189 int sc_nullalt;
190 int sc_audio_rev;
191 struct as_info *sc_alts; /* alternate settings */
192 int sc_nalts; /* # of alternate settings */
193 int sc_altflags;
194 #define HAS_8 0x01
195 #define HAS_16 0x02
196 #define HAS_8U 0x04
197 #define HAS_ALAW 0x08
198 #define HAS_MULAW 0x10
199 #define UA_NOFRAC 0x20 /* don't do sample rate adjustment */
200 #define HAS_24 0x40
201 int sc_mode; /* play/record capability */
202 struct mixerctl *sc_ctls; /* mixer controls */
203 int sc_nctls; /* # of mixer controls */
204 device_t sc_audiodev;
205 struct audio_format *sc_formats;
206 int sc_nformats;
207 u_int sc_channel_config;
208 char sc_dying;
209 struct audio_device sc_adev;
210 };
211
212 struct terminal_list {
213 int size;
214 uint16_t terminals[1];
215 };
216 #define TERMINAL_LIST_SIZE(N) (offsetof(struct terminal_list, terminals) \
217 + sizeof(uint16_t) * (N))
218
219 struct io_terminal {
220 union {
221 const uaudio_cs_descriptor_t *desc;
222 const struct usb_audio_input_terminal *it;
223 const struct usb_audio_output_terminal *ot;
224 const struct usb_audio_mixer_unit *mu;
225 const struct usb_audio_selector_unit *su;
226 const struct usb_audio_feature_unit *fu;
227 const struct usb_audio_processing_unit *pu;
228 const struct usb_audio_extension_unit *eu;
229 } d;
230 int inputs_size;
231 struct terminal_list **inputs; /* list of source input terminals */
232 struct terminal_list *output; /* list of destination output terminals */
233 int direct; /* directly connected to an output terminal */
234 };
235
236 #define UAC_OUTPUT 0
237 #define UAC_INPUT 1
238 #define UAC_EQUAL 2
239 #define UAC_RECORD 3
240 #define UAC_NCLASSES 4
241 #ifdef UAUDIO_DEBUG
242 Static const char *uac_names[] = {
243 AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord,
244 };
245 #endif
246
247 #ifdef UAUDIO_DEBUG
248 Static void uaudio_dump_tml
249 (struct terminal_list *tml);
250 #endif
251 Static usbd_status uaudio_identify_ac
252 (struct uaudio_softc *, const usb_config_descriptor_t *);
253 Static usbd_status uaudio_identify_as
254 (struct uaudio_softc *, const usb_config_descriptor_t *);
255 Static usbd_status uaudio_process_as
256 (struct uaudio_softc *, const char *, int *, int,
257 const usb_interface_descriptor_t *);
258
259 Static void uaudio_add_alt(struct uaudio_softc *, const struct as_info *);
260
261 Static const usb_interface_descriptor_t *uaudio_find_iface
262 (const char *, int, int *, int);
263
264 Static void uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *);
265 Static char *uaudio_id_name
266 (struct uaudio_softc *, const struct io_terminal *, int);
267 #ifdef UAUDIO_DEBUG
268 Static void uaudio_dump_cluster(const struct usb_audio_cluster *);
269 #endif
270 Static struct usb_audio_cluster uaudio_get_cluster
271 (int, const struct io_terminal *);
272 Static void uaudio_add_input
273 (struct uaudio_softc *, const struct io_terminal *, int);
274 Static void uaudio_add_output
275 (struct uaudio_softc *, const struct io_terminal *, int);
276 Static void uaudio_add_mixer
277 (struct uaudio_softc *, const struct io_terminal *, int);
278 Static void uaudio_add_selector
279 (struct uaudio_softc *, const struct io_terminal *, int);
280 #ifdef UAUDIO_DEBUG
281 Static const char *uaudio_get_terminal_name(int);
282 #endif
283 Static int uaudio_determine_class
284 (const struct io_terminal *, struct mixerctl *);
285 Static const char *uaudio_feature_name
286 (const struct io_terminal *, struct mixerctl *);
287 Static void uaudio_add_feature
288 (struct uaudio_softc *, const struct io_terminal *, int);
289 Static void uaudio_add_processing_updown
290 (struct uaudio_softc *, const struct io_terminal *, int);
291 Static void uaudio_add_processing
292 (struct uaudio_softc *, const struct io_terminal *, int);
293 Static void uaudio_add_extension
294 (struct uaudio_softc *, const struct io_terminal *, int);
295 Static struct terminal_list *uaudio_merge_terminal_list
296 (const struct io_terminal *);
297 Static struct terminal_list *uaudio_io_terminaltype
298 (int, struct io_terminal *, int);
299 Static usbd_status uaudio_identify
300 (struct uaudio_softc *, const usb_config_descriptor_t *);
301
302 Static int uaudio_signext(int, int);
303 Static int uaudio_value2bsd(struct mixerctl *, int);
304 Static int uaudio_bsd2value(struct mixerctl *, int);
305 Static int uaudio_get(struct uaudio_softc *, int, int, int, int, int);
306 Static int uaudio_ctl_get
307 (struct uaudio_softc *, int, struct mixerctl *, int);
308 Static void uaudio_set
309 (struct uaudio_softc *, int, int, int, int, int, int);
310 Static void uaudio_ctl_set
311 (struct uaudio_softc *, int, struct mixerctl *, int, int);
312
313 Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int);
314
315 Static usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *);
316 Static void uaudio_chan_abort(struct uaudio_softc *, struct chan *);
317 Static void uaudio_chan_close(struct uaudio_softc *, struct chan *);
318 Static usbd_status uaudio_chan_alloc_buffers
319 (struct uaudio_softc *, struct chan *);
320 Static void uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *);
321 Static void uaudio_chan_init
322 (struct chan *, int, const struct audio_params *, int);
323 Static void uaudio_chan_set_param(struct chan *, u_char *, u_char *, int);
324 Static void uaudio_chan_ptransfer(struct chan *);
325 Static void uaudio_chan_pintr
326 (struct usbd_xfer *, void *, usbd_status);
327
328 Static void uaudio_chan_rtransfer(struct chan *);
329 Static void uaudio_chan_rintr
330 (struct usbd_xfer *, void *, usbd_status);
331
332 Static int uaudio_open(void *, int);
333 Static int uaudio_query_format(void *, audio_format_query_t *);
334 Static int uaudio_set_format
335 (void *, int, const audio_params_t *, const audio_params_t *,
336 audio_filter_reg_t *, audio_filter_reg_t *);
337 Static int uaudio_round_blocksize(void *, int, int, const audio_params_t *);
338 Static int uaudio_trigger_output
339 (void *, void *, void *, int, void (*)(void *), void *,
340 const audio_params_t *);
341 Static int uaudio_trigger_input
342 (void *, void *, void *, int, void (*)(void *), void *,
343 const audio_params_t *);
344 Static int uaudio_halt_in_dma(void *);
345 Static int uaudio_halt_out_dma(void *);
346 Static int uaudio_getdev(void *, struct audio_device *);
347 Static int uaudio_mixer_set_port(void *, mixer_ctrl_t *);
348 Static int uaudio_mixer_get_port(void *, mixer_ctrl_t *);
349 Static int uaudio_query_devinfo(void *, mixer_devinfo_t *);
350 Static int uaudio_get_props(void *);
351 Static void uaudio_get_locks(void *, kmutex_t **, kmutex_t **);
352
353 Static const struct audio_hw_if uaudio_hw_if = {
354 .open = uaudio_open,
355 .query_format = uaudio_query_format,
356 .set_format = uaudio_set_format,
357 .round_blocksize = uaudio_round_blocksize,
358 .halt_output = uaudio_halt_out_dma,
359 .halt_input = uaudio_halt_in_dma,
360 .getdev = uaudio_getdev,
361 .set_port = uaudio_mixer_set_port,
362 .get_port = uaudio_mixer_get_port,
363 .query_devinfo = uaudio_query_devinfo,
364 .get_props = uaudio_get_props,
365 .trigger_output = uaudio_trigger_output,
366 .trigger_input = uaudio_trigger_input,
367 .get_locks = uaudio_get_locks,
368 };
369
370 int uaudio_match(device_t, cfdata_t, void *);
371 void uaudio_attach(device_t, device_t, void *);
372 int uaudio_detach(device_t, int);
373 void uaudio_childdet(device_t, device_t);
374 int uaudio_activate(device_t, enum devact);
375
376
377
378 CFATTACH_DECL2_NEW(uaudio, sizeof(struct uaudio_softc),
379 uaudio_match, uaudio_attach, uaudio_detach, uaudio_activate, NULL,
380 uaudio_childdet);
381
382 int
383 uaudio_match(device_t parent, cfdata_t match, void *aux)
384 {
385 struct usbif_attach_arg *uiaa = aux;
386
387 /* Trigger on the control interface. */
388 if (uiaa->uiaa_class != UICLASS_AUDIO ||
389 uiaa->uiaa_subclass != UISUBCLASS_AUDIOCONTROL ||
390 (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_BAD_AUDIO))
391 return UMATCH_NONE;
392
393 return UMATCH_IFACECLASS_IFACESUBCLASS;
394 }
395
396 void
397 uaudio_attach(device_t parent, device_t self, void *aux)
398 {
399 struct uaudio_softc *sc = device_private(self);
400 struct usbif_attach_arg *uiaa = aux;
401 usb_interface_descriptor_t *id;
402 usb_config_descriptor_t *cdesc;
403 char *devinfop;
404 usbd_status err;
405 int i, j, found;
406
407 sc->sc_dev = self;
408 sc->sc_udev = uiaa->uiaa_device;
409 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
410 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
411
412 strlcpy(sc->sc_adev.name, "USB audio", sizeof(sc->sc_adev.name));
413 strlcpy(sc->sc_adev.version, "", sizeof(sc->sc_adev.version));
414 snprintf(sc->sc_adev.config, sizeof(sc->sc_adev.config), "usb:%08x",
415 sc->sc_udev->ud_cookie.cookie);
416
417 aprint_naive("\n");
418 aprint_normal("\n");
419
420 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
421 aprint_normal_dev(self, "%s\n", devinfop);
422 usbd_devinfo_free(devinfop);
423
424 cdesc = usbd_get_config_descriptor(sc->sc_udev);
425 if (cdesc == NULL) {
426 aprint_error_dev(self,
427 "failed to get configuration descriptor\n");
428 return;
429 }
430
431 err = uaudio_identify(sc, cdesc);
432 if (err) {
433 aprint_error_dev(self,
434 "audio descriptors make no sense, error=%d\n", err);
435 return;
436 }
437
438 sc->sc_ac_ifaceh = uiaa->uiaa_iface;
439 /* Pick up the AS interface. */
440 for (i = 0; i < uiaa->uiaa_nifaces; i++) {
441 if (uiaa->uiaa_ifaces[i] == NULL)
442 continue;
443 id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
444 if (id == NULL)
445 continue;
446 found = 0;
447 for (j = 0; j < sc->sc_nalts; j++) {
448 if (id->bInterfaceNumber ==
449 sc->sc_alts[j].idesc->bInterfaceNumber) {
450 sc->sc_alts[j].ifaceh = uiaa->uiaa_ifaces[i];
451 found = 1;
452 }
453 }
454 if (found)
455 uiaa->uiaa_ifaces[i] = NULL;
456 }
457
458 for (j = 0; j < sc->sc_nalts; j++) {
459 if (sc->sc_alts[j].ifaceh == NULL) {
460 aprint_error_dev(self,
461 "alt %d missing AS interface(s)\n", j);
462 return;
463 }
464 }
465
466 aprint_normal_dev(self, "audio rev %d.%02x\n",
467 sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff);
468
469 sc->sc_playchan.sc = sc->sc_recchan.sc = sc;
470 sc->sc_playchan.altidx = -1;
471 sc->sc_recchan.altidx = -1;
472
473 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC)
474 sc->sc_altflags |= UA_NOFRAC;
475
476 #ifndef UAUDIO_DEBUG
477 if (bootverbose)
478 #endif
479 aprint_normal_dev(self, "%d mixer controls\n",
480 sc->sc_nctls);
481
482 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
483
484 DPRINTF("%s", "doing audio_attach_mi\n");
485 sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, sc->sc_dev);
486
487 if (!pmf_device_register(self, NULL, NULL))
488 aprint_error_dev(self, "couldn't establish power handler\n");
489
490 return;
491 }
492
493 int
494 uaudio_activate(device_t self, enum devact act)
495 {
496 struct uaudio_softc *sc = device_private(self);
497
498 switch (act) {
499 case DVACT_DEACTIVATE:
500 sc->sc_dying = 1;
501 return 0;
502 default:
503 return EOPNOTSUPP;
504 }
505 }
506
507 void
508 uaudio_childdet(device_t self, device_t child)
509 {
510 struct uaudio_softc *sc = device_private(self);
511
512 KASSERT(sc->sc_audiodev == child);
513 sc->sc_audiodev = NULL;
514 }
515
516 int
517 uaudio_detach(device_t self, int flags)
518 {
519 struct uaudio_softc *sc = device_private(self);
520 int rv = 0;
521
522 pmf_device_deregister(self);
523
524 /* Wait for outstanding requests to complete. */
525 usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
526
527 if (sc->sc_audiodev != NULL)
528 rv = config_detach(sc->sc_audiodev, flags);
529
530 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
531
532 if (sc->sc_formats != NULL)
533 kmem_free(sc->sc_formats,
534 sizeof(struct audio_format) * sc->sc_nformats);
535
536 mutex_destroy(&sc->sc_lock);
537 mutex_destroy(&sc->sc_intr_lock);
538
539 return rv;
540 }
541
542 Static int
543 uaudio_query_format(void *addr, audio_format_query_t *afp)
544 {
545 struct uaudio_softc *sc;
546
547 sc = addr;
548 return audio_query_format(sc->sc_formats, sc->sc_nformats, afp);
549 }
550
551 Static const usb_interface_descriptor_t *
552 uaudio_find_iface(const char *tbuf, int size, int *offsp, int subtype)
553 {
554 const usb_interface_descriptor_t *d;
555
556 while (*offsp < size) {
557 d = (const void *)(tbuf + *offsp);
558 *offsp += d->bLength;
559 if (d->bDescriptorType == UDESC_INTERFACE &&
560 d->bInterfaceClass == UICLASS_AUDIO &&
561 d->bInterfaceSubClass == subtype)
562 return d;
563 }
564 return NULL;
565 }
566
567 Static void
568 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc)
569 {
570 int res;
571 size_t len;
572 struct mixerctl *nmc;
573
574 if (mc->class < UAC_NCLASSES) {
575 DPRINTF("adding %s.%s\n", uac_names[mc->class], mc->ctlname);
576 } else {
577 DPRINTF("adding %s\n", mc->ctlname);
578 }
579 len = sizeof(*mc) * (sc->sc_nctls + 1);
580 nmc = kmem_alloc(len, KM_SLEEP);
581 /* Copy old data, if there was any */
582 if (sc->sc_nctls != 0) {
583 memcpy(nmc, sc->sc_ctls, sizeof(*mc) * (sc->sc_nctls));
584 kmem_free(sc->sc_ctls, sizeof(*mc) * sc->sc_nctls);
585 }
586 sc->sc_ctls = nmc;
587
588 mc->delta = 0;
589 if (mc->type == MIX_ON_OFF) {
590 mc->minval = 0;
591 mc->maxval = 1;
592 } else if (mc->type == MIX_SELECTOR) {
593 ;
594 } else {
595 /* Determine min and max values. */
596 mc->minval = uaudio_signext(mc->type,
597 uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE,
598 mc->wValue[0], mc->wIndex,
599 MIX_SIZE(mc->type)));
600 mc->maxval = 1 + uaudio_signext(mc->type,
601 uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE,
602 mc->wValue[0], mc->wIndex,
603 MIX_SIZE(mc->type)));
604 mc->mul = mc->maxval - mc->minval;
605 if (mc->mul == 0)
606 mc->mul = 1;
607 res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE,
608 mc->wValue[0], mc->wIndex,
609 MIX_SIZE(mc->type));
610 if (res > 0)
611 mc->delta = (res * 255 + mc->mul/2) / mc->mul;
612 }
613
614 sc->sc_ctls[sc->sc_nctls++] = *mc;
615
616 #ifdef UAUDIO_DEBUG
617 if (uaudiodebug > 2) {
618 int i;
619
620 DPRINTFN_CLEAN(2, "wValue=%04x", mc->wValue[0]);
621 for (i = 1; i < mc->nchan; i++)
622 DPRINTFN_CLEAN(2, ",%04x", mc->wValue[i]);
623 DPRINTFN_CLEAN(2, " wIndex=%04x type=%d name='%s' unit='%s' "
624 "min=%d max=%d\n",
625 mc->wIndex, mc->type, mc->ctlname, mc->ctlunit,
626 mc->minval, mc->maxval);
627 }
628 #endif
629 }
630
631 Static char *
632 uaudio_id_name(struct uaudio_softc *sc,
633 const struct io_terminal *iot, int id)
634 {
635 static char tbuf[32];
636
637 snprintf(tbuf, sizeof(tbuf), "i%d", id);
638 return tbuf;
639 }
640
641 #ifdef UAUDIO_DEBUG
642 Static void
643 uaudio_dump_cluster(const struct usb_audio_cluster *cl)
644 {
645 static const char *channel_names[16] = {
646 "LEFT", "RIGHT", "CENTER", "LFE",
647 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
648 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
649 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
650 };
651 int cc, i, first;
652
653 cc = UGETW(cl->wChannelConfig);
654 printf("cluster: bNrChannels=%u wChannelConfig=0x%.4x",
655 cl->bNrChannels, cc);
656 first = TRUE;
657 for (i = 0; cc != 0; i++) {
658 if (cc & 1) {
659 printf("%c%s", first ? '<' : ',', channel_names[i]);
660 first = FALSE;
661 }
662 cc = cc >> 1;
663 }
664 printf("> iChannelNames=%u", cl->iChannelNames);
665 }
666 #endif
667
668 Static struct usb_audio_cluster
669 uaudio_get_cluster(int id, const struct io_terminal *iot)
670 {
671 struct usb_audio_cluster r;
672 const uaudio_cs_descriptor_t *dp;
673 int i;
674
675 for (i = 0; i < 25; i++) { /* avoid infinite loops */
676 dp = iot[id].d.desc;
677 if (dp == 0)
678 goto bad;
679 switch (dp->bDescriptorSubtype) {
680 case UDESCSUB_AC_INPUT:
681 r.bNrChannels = iot[id].d.it->bNrChannels;
682 USETW(r.wChannelConfig, UGETW(iot[id].d.it->wChannelConfig));
683 r.iChannelNames = iot[id].d.it->iChannelNames;
684 return r;
685 case UDESCSUB_AC_OUTPUT:
686 id = iot[id].d.ot->bSourceId;
687 break;
688 case UDESCSUB_AC_MIXER:
689 r = *(const struct usb_audio_cluster *)
690 &iot[id].d.mu->baSourceId[iot[id].d.mu->bNrInPins];
691 return r;
692 case UDESCSUB_AC_SELECTOR:
693 /* XXX This is not really right */
694 id = iot[id].d.su->baSourceId[0];
695 break;
696 case UDESCSUB_AC_FEATURE:
697 id = iot[id].d.fu->bSourceId;
698 break;
699 case UDESCSUB_AC_PROCESSING:
700 r = *(const struct usb_audio_cluster *)
701 &iot[id].d.pu->baSourceId[iot[id].d.pu->bNrInPins];
702 return r;
703 case UDESCSUB_AC_EXTENSION:
704 r = *(const struct usb_audio_cluster *)
705 &iot[id].d.eu->baSourceId[iot[id].d.eu->bNrInPins];
706 return r;
707 default:
708 goto bad;
709 }
710 }
711 bad:
712 aprint_error("uaudio_get_cluster: bad data\n");
713 memset(&r, 0, sizeof(r));
714 return r;
715
716 }
717
718 Static void
719 uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
720 {
721 const struct usb_audio_input_terminal *d;
722
723 d = iot[id].d.it;
724 #ifdef UAUDIO_DEBUG
725 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
726 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
727 "iChannelNames=%d iTerminal=%d\n",
728 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
729 d->bNrChannels, UGETW(d->wChannelConfig),
730 d->iChannelNames, d->iTerminal);
731 #endif
732 /* If USB input terminal, record wChannelConfig */
733 if ((UGETW(d->wTerminalType) & 0xff00) != 0x0100)
734 return;
735 sc->sc_channel_config = UGETW(d->wChannelConfig);
736 }
737
738 Static void
739 uaudio_add_output(struct uaudio_softc *sc,
740 const struct io_terminal *iot, int id)
741 {
742 #ifdef UAUDIO_DEBUG
743 const struct usb_audio_output_terminal *d;
744
745 d = iot[id].d.ot;
746 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
747 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
748 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
749 d->bSourceId, d->iTerminal);
750 #endif
751 }
752
753 Static void
754 uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
755 {
756 const struct usb_audio_mixer_unit *d;
757 const struct usb_audio_mixer_unit_1 *d1;
758 int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k;
759 const uByte *bm;
760 struct mixerctl mix;
761
762 d = iot[id].d.mu;
763 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
764 d->bUnitId, d->bNrInPins);
765
766 /* Compute the number of input channels */
767 ichs = 0;
768 for (i = 0; i < d->bNrInPins; i++)
769 ichs += uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
770
771 /* and the number of output channels */
772 d1 = (const struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins];
773 ochs = d1->bNrChannels;
774 DPRINTFN(2,"ichs=%d ochs=%d\n", ichs, ochs);
775
776 bm = d1->bmControls;
777 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
778 uaudio_determine_class(&iot[id], &mix);
779 mix.type = MIX_SIGNED_16;
780 mix.ctlunit = AudioNvolume;
781 #define _BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1)
782 for (p = i = 0; i < d->bNrInPins; i++) {
783 chs = uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
784 mc = 0;
785 for (c = 0; c < chs; c++) {
786 mo = 0;
787 for (o = 0; o < ochs; o++) {
788 bno = (p + c) * ochs + o;
789 if (_BIT(bno))
790 mo++;
791 }
792 if (mo == 1)
793 mc++;
794 }
795 if (mc == chs && chs <= MIX_MAX_CHAN) {
796 k = 0;
797 for (c = 0; c < chs; c++)
798 for (o = 0; o < ochs; o++) {
799 bno = (p + c) * ochs + o;
800 if (_BIT(bno))
801 mix.wValue[k++] =
802 MAKE(p+c+1, o+1);
803 }
804 snprintf(mix.ctlname, sizeof(mix.ctlname), "mix%d-%s",
805 d->bUnitId, uaudio_id_name(sc, iot,
806 d->baSourceId[i]));
807 mix.nchan = chs;
808 uaudio_mixer_add_ctl(sc, &mix);
809 } else {
810 /* XXX */
811 }
812 #undef _BIT
813 p += chs;
814 }
815
816 }
817
818 Static void
819 uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
820 {
821 const struct usb_audio_selector_unit *d;
822 struct mixerctl mix;
823 int i, wp;
824
825 d = iot[id].d.su;
826 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
827 d->bUnitId, d->bNrInPins);
828 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
829 mix.wValue[0] = MAKE(0, 0);
830 uaudio_determine_class(&iot[id], &mix);
831 mix.nchan = 1;
832 mix.type = MIX_SELECTOR;
833 mix.ctlunit = "";
834 mix.minval = 1;
835 mix.maxval = d->bNrInPins;
836 mix.mul = mix.maxval - mix.minval;
837 wp = snprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId);
838 for (i = 1; i <= d->bNrInPins; i++) {
839 wp += snprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp,
840 "i%d", d->baSourceId[i - 1]);
841 if (wp > MAX_AUDIO_DEV_LEN - 1)
842 break;
843 }
844 uaudio_mixer_add_ctl(sc, &mix);
845 }
846
847 #ifdef UAUDIO_DEBUG
848 Static const char *
849 uaudio_get_terminal_name(int terminal_type)
850 {
851 static char tbuf[100];
852
853 switch (terminal_type) {
854 /* USB terminal types */
855 case UAT_UNDEFINED: return "UAT_UNDEFINED";
856 case UAT_STREAM: return "UAT_STREAM";
857 case UAT_VENDOR: return "UAT_VENDOR";
858 /* input terminal types */
859 case UATI_UNDEFINED: return "UATI_UNDEFINED";
860 case UATI_MICROPHONE: return "UATI_MICROPHONE";
861 case UATI_DESKMICROPHONE: return "UATI_DESKMICROPHONE";
862 case UATI_PERSONALMICROPHONE: return "UATI_PERSONALMICROPHONE";
863 case UATI_OMNIMICROPHONE: return "UATI_OMNIMICROPHONE";
864 case UATI_MICROPHONEARRAY: return "UATI_MICROPHONEARRAY";
865 case UATI_PROCMICROPHONEARR: return "UATI_PROCMICROPHONEARR";
866 /* output terminal types */
867 case UATO_UNDEFINED: return "UATO_UNDEFINED";
868 case UATO_SPEAKER: return "UATO_SPEAKER";
869 case UATO_HEADPHONES: return "UATO_HEADPHONES";
870 case UATO_DISPLAYAUDIO: return "UATO_DISPLAYAUDIO";
871 case UATO_DESKTOPSPEAKER: return "UATO_DESKTOPSPEAKER";
872 case UATO_ROOMSPEAKER: return "UATO_ROOMSPEAKER";
873 case UATO_COMMSPEAKER: return "UATO_COMMSPEAKER";
874 case UATO_SUBWOOFER: return "UATO_SUBWOOFER";
875 /* bidir terminal types */
876 case UATB_UNDEFINED: return "UATB_UNDEFINED";
877 case UATB_HANDSET: return "UATB_HANDSET";
878 case UATB_HEADSET: return "UATB_HEADSET";
879 case UATB_SPEAKERPHONE: return "UATB_SPEAKERPHONE";
880 case UATB_SPEAKERPHONEESUP: return "UATB_SPEAKERPHONEESUP";
881 case UATB_SPEAKERPHONEECANC: return "UATB_SPEAKERPHONEECANC";
882 /* telephony terminal types */
883 case UATT_UNDEFINED: return "UATT_UNDEFINED";
884 case UATT_PHONELINE: return "UATT_PHONELINE";
885 case UATT_TELEPHONE: return "UATT_TELEPHONE";
886 case UATT_DOWNLINEPHONE: return "UATT_DOWNLINEPHONE";
887 /* external terminal types */
888 case UATE_UNDEFINED: return "UATE_UNDEFINED";
889 case UATE_ANALOGCONN: return "UATE_ANALOGCONN";
890 case UATE_LINECONN: return "UATE_LINECONN";
891 case UATE_LEGACYCONN: return "UATE_LEGACYCONN";
892 case UATE_DIGITALAUIFC: return "UATE_DIGITALAUIFC";
893 case UATE_SPDIF: return "UATE_SPDIF";
894 case UATE_1394DA: return "UATE_1394DA";
895 case UATE_1394DV: return "UATE_1394DV";
896 /* embedded function terminal types */
897 case UATF_UNDEFINED: return "UATF_UNDEFINED";
898 case UATF_CALIBNOISE: return "UATF_CALIBNOISE";
899 case UATF_EQUNOISE: return "UATF_EQUNOISE";
900 case UATF_CDPLAYER: return "UATF_CDPLAYER";
901 case UATF_DAT: return "UATF_DAT";
902 case UATF_DCC: return "UATF_DCC";
903 case UATF_MINIDISK: return "UATF_MINIDISK";
904 case UATF_ANALOGTAPE: return "UATF_ANALOGTAPE";
905 case UATF_PHONOGRAPH: return "UATF_PHONOGRAPH";
906 case UATF_VCRAUDIO: return "UATF_VCRAUDIO";
907 case UATF_VIDEODISCAUDIO: return "UATF_VIDEODISCAUDIO";
908 case UATF_DVDAUDIO: return "UATF_DVDAUDIO";
909 case UATF_TVTUNERAUDIO: return "UATF_TVTUNERAUDIO";
910 case UATF_SATELLITE: return "UATF_SATELLITE";
911 case UATF_CABLETUNER: return "UATF_CABLETUNER";
912 case UATF_DSS: return "UATF_DSS";
913 case UATF_RADIORECV: return "UATF_RADIORECV";
914 case UATF_RADIOXMIT: return "UATF_RADIOXMIT";
915 case UATF_MULTITRACK: return "UATF_MULTITRACK";
916 case UATF_SYNTHESIZER: return "UATF_SYNTHESIZER";
917 default:
918 snprintf(tbuf, sizeof(tbuf), "unknown type (0x%.4x)", terminal_type);
919 return tbuf;
920 }
921 }
922 #endif
923
924 Static int
925 uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix)
926 {
927 int terminal_type;
928
929 if (iot == NULL || iot->output == NULL) {
930 mix->class = UAC_OUTPUT;
931 return 0;
932 }
933 terminal_type = 0;
934 if (iot->output->size == 1)
935 terminal_type = iot->output->terminals[0];
936 /*
937 * If the only output terminal is USB,
938 * the class is UAC_RECORD.
939 */
940 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
941 mix->class = UAC_RECORD;
942 if (iot->inputs_size == 1
943 && iot->inputs[0] != NULL
944 && iot->inputs[0]->size == 1)
945 return iot->inputs[0]->terminals[0];
946 else
947 return 0;
948 }
949 /*
950 * If the ultimate destination of the unit is just one output
951 * terminal and the unit is connected to the output terminal
952 * directly, the class is UAC_OUTPUT.
953 */
954 if (terminal_type != 0 && iot->direct) {
955 mix->class = UAC_OUTPUT;
956 return terminal_type;
957 }
958 /*
959 * If the unit is connected to just one input terminal,
960 * the class is UAC_INPUT.
961 */
962 if (iot->inputs_size == 1 && iot->inputs[0] != NULL
963 && iot->inputs[0]->size == 1) {
964 mix->class = UAC_INPUT;
965 return iot->inputs[0]->terminals[0];
966 }
967 /*
968 * Otherwise, the class is UAC_OUTPUT.
969 */
970 mix->class = UAC_OUTPUT;
971 return terminal_type;
972 }
973
974 Static const char *
975 uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix)
976 {
977 int terminal_type;
978
979 terminal_type = uaudio_determine_class(iot, mix);
980 if (mix->class == UAC_RECORD && terminal_type == 0)
981 return AudioNmixerout;
982 DPRINTF("terminal_type=%s\n", uaudio_get_terminal_name(terminal_type));
983 switch (terminal_type) {
984 case UAT_STREAM:
985 return AudioNdac;
986
987 case UATI_MICROPHONE:
988 case UATI_DESKMICROPHONE:
989 case UATI_PERSONALMICROPHONE:
990 case UATI_OMNIMICROPHONE:
991 case UATI_MICROPHONEARRAY:
992 case UATI_PROCMICROPHONEARR:
993 return AudioNmicrophone;
994
995 case UATO_SPEAKER:
996 case UATO_DESKTOPSPEAKER:
997 case UATO_ROOMSPEAKER:
998 case UATO_COMMSPEAKER:
999 return AudioNspeaker;
1000
1001 case UATO_HEADPHONES:
1002 return AudioNheadphone;
1003
1004 case UATO_SUBWOOFER:
1005 return AudioNlfe;
1006
1007 /* telephony terminal types */
1008 case UATT_UNDEFINED:
1009 case UATT_PHONELINE:
1010 case UATT_TELEPHONE:
1011 case UATT_DOWNLINEPHONE:
1012 return "phone";
1013
1014 case UATE_ANALOGCONN:
1015 case UATE_LINECONN:
1016 case UATE_LEGACYCONN:
1017 return AudioNline;
1018
1019 case UATE_DIGITALAUIFC:
1020 case UATE_SPDIF:
1021 case UATE_1394DA:
1022 case UATE_1394DV:
1023 return AudioNaux;
1024
1025 case UATF_CDPLAYER:
1026 return AudioNcd;
1027
1028 case UATF_SYNTHESIZER:
1029 return AudioNfmsynth;
1030
1031 case UATF_VIDEODISCAUDIO:
1032 case UATF_DVDAUDIO:
1033 case UATF_TVTUNERAUDIO:
1034 return AudioNvideo;
1035
1036 case UAT_UNDEFINED:
1037 case UAT_VENDOR:
1038 case UATI_UNDEFINED:
1039 /* output terminal types */
1040 case UATO_UNDEFINED:
1041 case UATO_DISPLAYAUDIO:
1042 /* bidir terminal types */
1043 case UATB_UNDEFINED:
1044 case UATB_HANDSET:
1045 case UATB_HEADSET:
1046 case UATB_SPEAKERPHONE:
1047 case UATB_SPEAKERPHONEESUP:
1048 case UATB_SPEAKERPHONEECANC:
1049 /* external terminal types */
1050 case UATE_UNDEFINED:
1051 /* embedded function terminal types */
1052 case UATF_UNDEFINED:
1053 case UATF_CALIBNOISE:
1054 case UATF_EQUNOISE:
1055 case UATF_DAT:
1056 case UATF_DCC:
1057 case UATF_MINIDISK:
1058 case UATF_ANALOGTAPE:
1059 case UATF_PHONOGRAPH:
1060 case UATF_VCRAUDIO:
1061 case UATF_SATELLITE:
1062 case UATF_CABLETUNER:
1063 case UATF_DSS:
1064 case UATF_RADIORECV:
1065 case UATF_RADIOXMIT:
1066 case UATF_MULTITRACK:
1067 case 0xffff:
1068 default:
1069 DPRINTF("'master' for 0x%.4x\n", terminal_type);
1070 return AudioNmaster;
1071 }
1072 return AudioNmaster;
1073 }
1074
1075 Static void
1076 uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1077 {
1078 const struct usb_audio_feature_unit *d;
1079 const uByte *ctls;
1080 int ctlsize;
1081 int nchan;
1082 u_int fumask, mmask, cmask;
1083 struct mixerctl mix;
1084 int chan, ctl, i, unit;
1085 const char *mixername;
1086
1087 #define GET(i) (ctls[(i)*ctlsize] | \
1088 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0))
1089 d = iot[id].d.fu;
1090 ctls = d->bmaControls;
1091 ctlsize = d->bControlSize;
1092 if (ctlsize == 0) {
1093 DPRINTF("ignoring feature %d with controlSize of zero\n", id);
1094 return;
1095 }
1096 nchan = (d->bLength - 7) / ctlsize;
1097 mmask = GET(0);
1098 /* Figure out what we can control */
1099 for (cmask = 0, chan = 1; chan < nchan; chan++) {
1100 DPRINTFN(9,"chan=%d mask=%x\n",
1101 chan, GET(chan));
1102 cmask |= GET(chan);
1103 }
1104
1105 DPRINTFN(1,"bUnitId=%d, "
1106 "%d channels, mmask=0x%04x, cmask=0x%04x\n",
1107 d->bUnitId, nchan, mmask, cmask);
1108
1109 if (nchan > MIX_MAX_CHAN)
1110 nchan = MIX_MAX_CHAN;
1111 unit = d->bUnitId;
1112 mix.wIndex = MAKE(unit, sc->sc_ac_iface);
1113 for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) {
1114 fumask = FU_MASK(ctl);
1115 DPRINTFN(4,"ctl=%d fumask=0x%04x\n",
1116 ctl, fumask);
1117 if (mmask & fumask) {
1118 mix.nchan = 1;
1119 mix.wValue[0] = MAKE(ctl, 0);
1120 } else if (cmask & fumask) {
1121 mix.nchan = nchan - 1;
1122 for (i = 1; i < nchan; i++) {
1123 if (GET(i) & fumask)
1124 mix.wValue[i-1] = MAKE(ctl, i);
1125 else
1126 mix.wValue[i-1] = -1;
1127 }
1128 } else {
1129 continue;
1130 }
1131 #undef GET
1132 mixername = uaudio_feature_name(&iot[id], &mix);
1133 switch (ctl) {
1134 case MUTE_CONTROL:
1135 mix.type = MIX_ON_OFF;
1136 mix.ctlunit = "";
1137 snprintf(mix.ctlname, sizeof(mix.ctlname),
1138 "%s.%s", mixername, AudioNmute);
1139 break;
1140 case VOLUME_CONTROL:
1141 mix.type = MIX_SIGNED_16;
1142 mix.ctlunit = AudioNvolume;
1143 strlcpy(mix.ctlname, mixername, sizeof(mix.ctlname));
1144 break;
1145 case BASS_CONTROL:
1146 mix.type = MIX_SIGNED_8;
1147 mix.ctlunit = AudioNbass;
1148 snprintf(mix.ctlname, sizeof(mix.ctlname),
1149 "%s.%s", mixername, AudioNbass);
1150 break;
1151 case MID_CONTROL:
1152 mix.type = MIX_SIGNED_8;
1153 mix.ctlunit = AudioNmid;
1154 snprintf(mix.ctlname, sizeof(mix.ctlname),
1155 "%s.%s", mixername, AudioNmid);
1156 break;
1157 case TREBLE_CONTROL:
1158 mix.type = MIX_SIGNED_8;
1159 mix.ctlunit = AudioNtreble;
1160 snprintf(mix.ctlname, sizeof(mix.ctlname),
1161 "%s.%s", mixername, AudioNtreble);
1162 break;
1163 case GRAPHIC_EQUALIZER_CONTROL:
1164 continue; /* XXX don't add anything */
1165 break;
1166 case AGC_CONTROL:
1167 mix.type = MIX_ON_OFF;
1168 mix.ctlunit = "";
1169 snprintf(mix.ctlname, sizeof(mix.ctlname), "%s.%s",
1170 mixername, AudioNagc);
1171 break;
1172 case DELAY_CONTROL:
1173 mix.type = MIX_UNSIGNED_16;
1174 mix.ctlunit = "4 ms";
1175 snprintf(mix.ctlname, sizeof(mix.ctlname),
1176 "%s.%s", mixername, AudioNdelay);
1177 break;
1178 case BASS_BOOST_CONTROL:
1179 mix.type = MIX_ON_OFF;
1180 mix.ctlunit = "";
1181 snprintf(mix.ctlname, sizeof(mix.ctlname),
1182 "%s.%s", mixername, AudioNbassboost);
1183 break;
1184 case LOUDNESS_CONTROL:
1185 mix.type = MIX_ON_OFF;
1186 mix.ctlunit = "";
1187 snprintf(mix.ctlname, sizeof(mix.ctlname),
1188 "%s.%s", mixername, AudioNloudness);
1189 break;
1190 }
1191 uaudio_mixer_add_ctl(sc, &mix);
1192 }
1193 }
1194
1195 Static void
1196 uaudio_add_processing_updown(struct uaudio_softc *sc,
1197 const struct io_terminal *iot, int id)
1198 {
1199 const struct usb_audio_processing_unit *d;
1200 const struct usb_audio_processing_unit_1 *d1;
1201 const struct usb_audio_processing_unit_updown *ud;
1202 struct mixerctl mix;
1203 int i;
1204
1205 d = iot[id].d.pu;
1206 d1 = (const struct usb_audio_processing_unit_1 *)
1207 &d->baSourceId[d->bNrInPins];
1208 ud = (const struct usb_audio_processing_unit_updown *)
1209 &d1->bmControls[d1->bControlSize];
1210 DPRINTFN(2,"bUnitId=%d bNrModes=%d\n",
1211 d->bUnitId, ud->bNrModes);
1212
1213 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
1214 DPRINTF("%s", "no mode select\n");
1215 return;
1216 }
1217
1218 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1219 mix.nchan = 1;
1220 mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0);
1221 uaudio_determine_class(&iot[id], &mix);
1222 mix.type = MIX_ON_OFF; /* XXX */
1223 mix.ctlunit = "";
1224 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId);
1225
1226 for (i = 0; i < ud->bNrModes; i++) {
1227 DPRINTFN(2,"i=%d bm=0x%x\n",
1228 i, UGETW(ud->waModes[i]));
1229 /* XXX */
1230 }
1231 uaudio_mixer_add_ctl(sc, &mix);
1232 }
1233
1234 Static void
1235 uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1236 {
1237 const struct usb_audio_processing_unit *d;
1238 const struct usb_audio_processing_unit_1 *d1;
1239 int ptype;
1240 struct mixerctl mix;
1241
1242 d = iot[id].d.pu;
1243 d1 = (const struct usb_audio_processing_unit_1 *)
1244 &d->baSourceId[d->bNrInPins];
1245 ptype = UGETW(d->wProcessType);
1246 DPRINTFN(2,"wProcessType=%d bUnitId=%d "
1247 "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins);
1248
1249 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
1250 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1251 mix.nchan = 1;
1252 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0);
1253 uaudio_determine_class(&iot[id], &mix);
1254 mix.type = MIX_ON_OFF;
1255 mix.ctlunit = "";
1256 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable",
1257 d->bUnitId, ptype);
1258 uaudio_mixer_add_ctl(sc, &mix);
1259 }
1260
1261 switch(ptype) {
1262 case UPDOWNMIX_PROCESS:
1263 uaudio_add_processing_updown(sc, iot, id);
1264 break;
1265 case DOLBY_PROLOGIC_PROCESS:
1266 case P3D_STEREO_EXTENDER_PROCESS:
1267 case REVERBATION_PROCESS:
1268 case CHORUS_PROCESS:
1269 case DYN_RANGE_COMP_PROCESS:
1270 default:
1271 #ifdef UAUDIO_DEBUG
1272 aprint_debug(
1273 "uaudio_add_processing: unit %d, type=%d not impl.\n",
1274 d->bUnitId, ptype);
1275 #endif
1276 break;
1277 }
1278 }
1279
1280 Static void
1281 uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1282 {
1283 const struct usb_audio_extension_unit *d;
1284 const struct usb_audio_extension_unit_1 *d1;
1285 struct mixerctl mix;
1286
1287 d = iot[id].d.eu;
1288 d1 = (const struct usb_audio_extension_unit_1 *)
1289 &d->baSourceId[d->bNrInPins];
1290 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
1291 d->bUnitId, d->bNrInPins);
1292
1293 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU)
1294 return;
1295
1296 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
1297 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1298 mix.nchan = 1;
1299 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0);
1300 uaudio_determine_class(&iot[id], &mix);
1301 mix.type = MIX_ON_OFF;
1302 mix.ctlunit = "";
1303 snprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable",
1304 d->bUnitId);
1305 uaudio_mixer_add_ctl(sc, &mix);
1306 }
1307 }
1308
1309 Static struct terminal_list*
1310 uaudio_merge_terminal_list(const struct io_terminal *iot)
1311 {
1312 struct terminal_list *tml;
1313 uint16_t *ptm;
1314 int i, len;
1315
1316 len = 0;
1317 if (iot->inputs == NULL)
1318 return NULL;
1319 for (i = 0; i < iot->inputs_size; i++) {
1320 if (iot->inputs[i] != NULL)
1321 len += iot->inputs[i]->size;
1322 }
1323 tml = malloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT);
1324 if (tml == NULL) {
1325 aprint_error("uaudio_merge_terminal_list: no memory\n");
1326 return NULL;
1327 }
1328 tml->size = 0;
1329 ptm = tml->terminals;
1330 for (i = 0; i < iot->inputs_size; i++) {
1331 if (iot->inputs[i] == NULL)
1332 continue;
1333 if (iot->inputs[i]->size > len)
1334 break;
1335 memcpy(ptm, iot->inputs[i]->terminals,
1336 iot->inputs[i]->size * sizeof(uint16_t));
1337 tml->size += iot->inputs[i]->size;
1338 ptm += iot->inputs[i]->size;
1339 len -= iot->inputs[i]->size;
1340 }
1341 return tml;
1342 }
1343
1344 Static struct terminal_list *
1345 uaudio_io_terminaltype(int outtype, struct io_terminal *iot, int id)
1346 {
1347 struct terminal_list *tml;
1348 struct io_terminal *it;
1349 int src_id, i;
1350
1351 it = &iot[id];
1352 if (it->output != NULL) {
1353 /* already has outtype? */
1354 for (i = 0; i < it->output->size; i++)
1355 if (it->output->terminals[i] == outtype)
1356 return uaudio_merge_terminal_list(it);
1357 tml = malloc(TERMINAL_LIST_SIZE(it->output->size + 1),
1358 M_TEMP, M_NOWAIT);
1359 if (tml == NULL) {
1360 aprint_error("uaudio_io_terminaltype: no memory\n");
1361 return uaudio_merge_terminal_list(it);
1362 }
1363 memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size));
1364 tml->terminals[it->output->size] = outtype;
1365 tml->size++;
1366 free(it->output, M_TEMP);
1367 it->output = tml;
1368 if (it->inputs != NULL) {
1369 for (i = 0; i < it->inputs_size; i++)
1370 if (it->inputs[i] != NULL)
1371 free(it->inputs[i], M_TEMP);
1372 free(it->inputs, M_TEMP);
1373 }
1374 it->inputs_size = 0;
1375 it->inputs = NULL;
1376 } else { /* end `iot[id] != NULL' */
1377 it->inputs_size = 0;
1378 it->inputs = NULL;
1379 it->output = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1380 if (it->output == NULL) {
1381 aprint_error("uaudio_io_terminaltype: no memory\n");
1382 return NULL;
1383 }
1384 it->output->terminals[0] = outtype;
1385 it->output->size = 1;
1386 it->direct = FALSE;
1387 }
1388
1389 switch (it->d.desc->bDescriptorSubtype) {
1390 case UDESCSUB_AC_INPUT:
1391 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1392 if (it->inputs == NULL) {
1393 aprint_error("uaudio_io_terminaltype: no memory\n");
1394 return NULL;
1395 }
1396 tml = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1397 if (tml == NULL) {
1398 aprint_error("uaudio_io_terminaltype: no memory\n");
1399 free(it->inputs, M_TEMP);
1400 it->inputs = NULL;
1401 return NULL;
1402 }
1403 it->inputs[0] = tml;
1404 tml->terminals[0] = UGETW(it->d.it->wTerminalType);
1405 tml->size = 1;
1406 it->inputs_size = 1;
1407 return uaudio_merge_terminal_list(it);
1408 case UDESCSUB_AC_FEATURE:
1409 src_id = it->d.fu->bSourceId;
1410 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1411 if (it->inputs == NULL) {
1412 aprint_error("uaudio_io_terminaltype: no memory\n");
1413 return uaudio_io_terminaltype(outtype, iot, src_id);
1414 }
1415 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1416 it->inputs_size = 1;
1417 return uaudio_merge_terminal_list(it);
1418 case UDESCSUB_AC_OUTPUT:
1419 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1420 if (it->inputs == NULL) {
1421 aprint_error("uaudio_io_terminaltype: no memory\n");
1422 return NULL;
1423 }
1424 src_id = it->d.ot->bSourceId;
1425 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1426 it->inputs_size = 1;
1427 iot[src_id].direct = TRUE;
1428 return NULL;
1429 case UDESCSUB_AC_MIXER:
1430 it->inputs_size = 0;
1431 it->inputs = malloc(sizeof(struct terminal_list *)
1432 * it->d.mu->bNrInPins, M_TEMP, M_NOWAIT);
1433 if (it->inputs == NULL) {
1434 aprint_error("uaudio_io_terminaltype: no memory\n");
1435 return NULL;
1436 }
1437 for (i = 0; i < it->d.mu->bNrInPins; i++) {
1438 src_id = it->d.mu->baSourceId[i];
1439 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1440 src_id);
1441 it->inputs_size++;
1442 }
1443 return uaudio_merge_terminal_list(it);
1444 case UDESCSUB_AC_SELECTOR:
1445 it->inputs_size = 0;
1446 it->inputs = malloc(sizeof(struct terminal_list *)
1447 * it->d.su->bNrInPins, M_TEMP, M_NOWAIT);
1448 if (it->inputs == NULL) {
1449 aprint_error("uaudio_io_terminaltype: no memory\n");
1450 return NULL;
1451 }
1452 for (i = 0; i < it->d.su->bNrInPins; i++) {
1453 src_id = it->d.su->baSourceId[i];
1454 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1455 src_id);
1456 it->inputs_size++;
1457 }
1458 return uaudio_merge_terminal_list(it);
1459 case UDESCSUB_AC_PROCESSING:
1460 it->inputs_size = 0;
1461 it->inputs = malloc(sizeof(struct terminal_list *)
1462 * it->d.pu->bNrInPins, M_TEMP, M_NOWAIT);
1463 if (it->inputs == NULL) {
1464 aprint_error("uaudio_io_terminaltype: no memory\n");
1465 return NULL;
1466 }
1467 for (i = 0; i < it->d.pu->bNrInPins; i++) {
1468 src_id = it->d.pu->baSourceId[i];
1469 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1470 src_id);
1471 it->inputs_size++;
1472 }
1473 return uaudio_merge_terminal_list(it);
1474 case UDESCSUB_AC_EXTENSION:
1475 it->inputs_size = 0;
1476 it->inputs = malloc(sizeof(struct terminal_list *)
1477 * it->d.eu->bNrInPins, M_TEMP, M_NOWAIT);
1478 if (it->inputs == NULL) {
1479 aprint_error("uaudio_io_terminaltype: no memory\n");
1480 return NULL;
1481 }
1482 for (i = 0; i < it->d.eu->bNrInPins; i++) {
1483 src_id = it->d.eu->baSourceId[i];
1484 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1485 src_id);
1486 it->inputs_size++;
1487 }
1488 return uaudio_merge_terminal_list(it);
1489 case UDESCSUB_AC_HEADER:
1490 default:
1491 return NULL;
1492 }
1493 }
1494
1495 Static usbd_status
1496 uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
1497 {
1498 usbd_status err;
1499
1500 err = uaudio_identify_ac(sc, cdesc);
1501 if (err)
1502 return err;
1503 return uaudio_identify_as(sc, cdesc);
1504 }
1505
1506 Static void
1507 uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai)
1508 {
1509 size_t len;
1510 struct as_info *nai;
1511
1512 len = sizeof(*ai) * (sc->sc_nalts + 1);
1513 nai = kmem_alloc(len, KM_SLEEP);
1514 /* Copy old data, if there was any */
1515 if (sc->sc_nalts != 0) {
1516 memcpy(nai, sc->sc_alts, sizeof(*ai) * (sc->sc_nalts));
1517 kmem_free(sc->sc_alts, sizeof(*ai) * sc->sc_nalts);
1518 }
1519 sc->sc_alts = nai;
1520 DPRINTFN(2,"adding alt=%d, enc=%d\n",
1521 ai->alt, ai->encoding);
1522 sc->sc_alts[sc->sc_nalts++] = *ai;
1523 }
1524
1525 Static usbd_status
1526 uaudio_process_as(struct uaudio_softc *sc, const char *tbuf, int *offsp,
1527 int size, const usb_interface_descriptor_t *id)
1528 #define offs (*offsp)
1529 {
1530 const struct usb_audio_streaming_interface_descriptor *asid;
1531 const struct usb_audio_streaming_type1_descriptor *asf1d;
1532 const usb_endpoint_descriptor_audio_t *ed;
1533 const usb_endpoint_descriptor_audio_t *epdesc1;
1534 const struct usb_audio_streaming_endpoint_descriptor *sed;
1535 int format, chan __unused, prec, enc;
1536 int dir, type, sync;
1537 struct as_info ai;
1538 const char *format_str __unused;
1539
1540 asid = (const void *)(tbuf + offs);
1541 if (asid->bDescriptorType != UDESC_CS_INTERFACE ||
1542 asid->bDescriptorSubtype != AS_GENERAL)
1543 return USBD_INVAL;
1544 DPRINTF("asid: bTerminakLink=%d wFormatTag=%d\n",
1545 asid->bTerminalLink, UGETW(asid->wFormatTag));
1546 offs += asid->bLength;
1547 if (offs > size)
1548 return USBD_INVAL;
1549
1550 asf1d = (const void *)(tbuf + offs);
1551 if (asf1d->bDescriptorType != UDESC_CS_INTERFACE ||
1552 asf1d->bDescriptorSubtype != FORMAT_TYPE)
1553 return USBD_INVAL;
1554 offs += asf1d->bLength;
1555 if (offs > size)
1556 return USBD_INVAL;
1557
1558 if (asf1d->bFormatType != FORMAT_TYPE_I) {
1559 aprint_normal_dev(sc->sc_dev,
1560 "ignored setting with type %d format\n", UGETW(asid->wFormatTag));
1561 return USBD_NORMAL_COMPLETION;
1562 }
1563
1564 ed = (const void *)(tbuf + offs);
1565 if (ed->bDescriptorType != UDESC_ENDPOINT)
1566 return USBD_INVAL;
1567 DPRINTF("endpoint[0] bLength=%d bDescriptorType=%d "
1568 "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d "
1569 "bInterval=%d bRefresh=%d bSynchAddress=%d\n",
1570 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress,
1571 ed->bmAttributes, UGETW(ed->wMaxPacketSize),
1572 ed->bInterval, ed->bRefresh, ed->bSynchAddress);
1573 offs += ed->bLength;
1574 if (offs > size)
1575 return USBD_INVAL;
1576 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
1577 return USBD_INVAL;
1578
1579 dir = UE_GET_DIR(ed->bEndpointAddress);
1580 type = UE_GET_ISO_TYPE(ed->bmAttributes);
1581 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) &&
1582 dir == UE_DIR_IN && type == UE_ISO_ADAPT)
1583 type = UE_ISO_ASYNC;
1584
1585 /* We can't handle endpoints that need a sync pipe yet. */
1586 sync = FALSE;
1587 if (dir == UE_DIR_IN && type == UE_ISO_ADAPT) {
1588 sync = TRUE;
1589 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1590 aprint_normal_dev(sc->sc_dev,
1591 "ignored input endpoint of type adaptive\n");
1592 return USBD_NORMAL_COMPLETION;
1593 #endif
1594 }
1595 if (dir != UE_DIR_IN && type == UE_ISO_ASYNC) {
1596 sync = TRUE;
1597 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1598 aprint_normal_dev(sc->sc_dev,
1599 "ignored output endpoint of type async\n");
1600 return USBD_NORMAL_COMPLETION;
1601 #endif
1602 }
1603
1604 sed = (const void *)(tbuf + offs);
1605 if (sed->bDescriptorType != UDESC_CS_ENDPOINT ||
1606 sed->bDescriptorSubtype != AS_GENERAL)
1607 return USBD_INVAL;
1608 DPRINTF(" streadming_endpoint: offset=%d bLength=%d\n", offs, sed->bLength);
1609 offs += sed->bLength;
1610 if (offs > size)
1611 return USBD_INVAL;
1612
1613 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
1614 if (sync && id->bNumEndpoints <= 1) {
1615 aprint_error_dev(sc->sc_dev,
1616 "a sync-pipe endpoint but no other endpoint\n");
1617 return USBD_INVAL;
1618 }
1619 #endif
1620 if (!sync && id->bNumEndpoints > 1) {
1621 aprint_error_dev(sc->sc_dev,
1622 "non sync-pipe endpoint but multiple endpoints\n");
1623 return USBD_INVAL;
1624 }
1625 epdesc1 = NULL;
1626 if (id->bNumEndpoints > 1) {
1627 epdesc1 = (const void*)(tbuf + offs);
1628 if (epdesc1->bDescriptorType != UDESC_ENDPOINT)
1629 return USBD_INVAL;
1630 DPRINTF("endpoint[1] bLength=%d "
1631 "bDescriptorType=%d bEndpointAddress=%d "
1632 "bmAttributes=0x%x wMaxPacketSize=%d bInterval=%d "
1633 "bRefresh=%d bSynchAddress=%d\n",
1634 epdesc1->bLength, epdesc1->bDescriptorType,
1635 epdesc1->bEndpointAddress, epdesc1->bmAttributes,
1636 UGETW(epdesc1->wMaxPacketSize), epdesc1->bInterval,
1637 epdesc1->bRefresh, epdesc1->bSynchAddress);
1638 offs += epdesc1->bLength;
1639 if (offs > size)
1640 return USBD_INVAL;
1641 if (epdesc1->bSynchAddress != 0) {
1642 aprint_error_dev(sc->sc_dev,
1643 "invalid endpoint: bSynchAddress=0\n");
1644 return USBD_INVAL;
1645 }
1646 if (UE_GET_XFERTYPE(epdesc1->bmAttributes) != UE_ISOCHRONOUS) {
1647 aprint_error_dev(sc->sc_dev,
1648 "invalid endpoint: bmAttributes=0x%x\n",
1649 epdesc1->bmAttributes);
1650 return USBD_INVAL;
1651 }
1652 if (epdesc1->bEndpointAddress != ed->bSynchAddress) {
1653 aprint_error_dev(sc->sc_dev,
1654 "invalid endpoint addresses: "
1655 "ep[0]->bSynchAddress=0x%x "
1656 "ep[1]->bEndpointAddress=0x%x\n",
1657 ed->bSynchAddress, epdesc1->bEndpointAddress);
1658 return USBD_INVAL;
1659 }
1660 /* UE_GET_ADDR(epdesc1->bEndpointAddress), and epdesc1->bRefresh */
1661 }
1662
1663 format = UGETW(asid->wFormatTag);
1664 chan = asf1d->bNrChannels;
1665 prec = asf1d->bBitResolution;
1666 if (prec != 8 && prec != 16 && prec != 24) {
1667 aprint_normal_dev(sc->sc_dev,
1668 "ignored setting with precision %d\n", prec);
1669 return USBD_NORMAL_COMPLETION;
1670 }
1671 switch (format) {
1672 case UA_FMT_PCM:
1673 if (prec == 8) {
1674 sc->sc_altflags |= HAS_8;
1675 } else if (prec == 16) {
1676 sc->sc_altflags |= HAS_16;
1677 } else if (prec == 24) {
1678 sc->sc_altflags |= HAS_24;
1679 }
1680 enc = AUDIO_ENCODING_SLINEAR_LE;
1681 format_str = "pcm";
1682 break;
1683 case UA_FMT_PCM8:
1684 enc = AUDIO_ENCODING_ULINEAR_LE;
1685 sc->sc_altflags |= HAS_8U;
1686 format_str = "pcm8";
1687 break;
1688 case UA_FMT_ALAW:
1689 enc = AUDIO_ENCODING_ALAW;
1690 sc->sc_altflags |= HAS_ALAW;
1691 format_str = "alaw";
1692 break;
1693 case UA_FMT_MULAW:
1694 enc = AUDIO_ENCODING_ULAW;
1695 sc->sc_altflags |= HAS_MULAW;
1696 format_str = "mulaw";
1697 break;
1698 case UA_FMT_IEEE_FLOAT:
1699 default:
1700 aprint_normal_dev(sc->sc_dev,
1701 "ignored setting with format %d\n", format);
1702 return USBD_NORMAL_COMPLETION;
1703 }
1704 #ifdef UAUDIO_DEBUG
1705 aprint_debug_dev(sc->sc_dev, "%s: %dch, %d/%dbit, %s,",
1706 dir == UE_DIR_IN ? "recording" : "playback",
1707 chan, prec, asf1d->bSubFrameSize * 8, format_str);
1708 if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
1709 aprint_debug(" %d-%dHz\n", UA_SAMP_LO(asf1d),
1710 UA_SAMP_HI(asf1d));
1711 } else {
1712 int r;
1713 aprint_debug(" %d", UA_GETSAMP(asf1d, 0));
1714 for (r = 1; r < asf1d->bSamFreqType; r++)
1715 aprint_debug(",%d", UA_GETSAMP(asf1d, r));
1716 aprint_debug("Hz\n");
1717 }
1718 #endif
1719 ai.alt = id->bAlternateSetting;
1720 ai.encoding = enc;
1721 ai.attributes = sed->bmAttributes;
1722 ai.idesc = id;
1723 ai.edesc = ed;
1724 ai.edesc1 = epdesc1;
1725 ai.asf1desc = asf1d;
1726 ai.sc_busy = 0;
1727 ai.aformat = NULL;
1728 ai.ifaceh = NULL;
1729 uaudio_add_alt(sc, &ai);
1730 #ifdef UAUDIO_DEBUG
1731 if (ai.attributes & UA_SED_FREQ_CONTROL)
1732 DPRINTFN(1, "%s", "FREQ_CONTROL\n");
1733 if (ai.attributes & UA_SED_PITCH_CONTROL)
1734 DPRINTFN(1, "%s", "PITCH_CONTROL\n");
1735 #endif
1736 sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD;
1737
1738 return USBD_NORMAL_COMPLETION;
1739 }
1740 #undef offs
1741
1742 Static usbd_status
1743 uaudio_identify_as(struct uaudio_softc *sc,
1744 const usb_config_descriptor_t *cdesc)
1745 {
1746 const usb_interface_descriptor_t *id;
1747 const char *tbuf;
1748 struct audio_format *auf;
1749 const struct usb_audio_streaming_type1_descriptor *t1desc;
1750 int size, offs;
1751 int i, j;
1752
1753 size = UGETW(cdesc->wTotalLength);
1754 tbuf = (const char *)cdesc;
1755
1756 /* Locate the AudioStreaming interface descriptor. */
1757 offs = 0;
1758 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOSTREAM);
1759 if (id == NULL)
1760 return USBD_INVAL;
1761
1762 /* Loop through all the alternate settings. */
1763 while (offs <= size) {
1764 DPRINTFN(2, "interface=%d offset=%d\n",
1765 id->bInterfaceNumber, offs);
1766 switch (id->bNumEndpoints) {
1767 case 0:
1768 DPRINTFN(2, "AS null alt=%d\n",
1769 id->bAlternateSetting);
1770 sc->sc_nullalt = id->bAlternateSetting;
1771 break;
1772 case 1:
1773 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
1774 case 2:
1775 #endif
1776 uaudio_process_as(sc, tbuf, &offs, size, id);
1777 break;
1778 default:
1779 aprint_error_dev(sc->sc_dev,
1780 "ignored audio interface with %d endpoints\n",
1781 id->bNumEndpoints);
1782 break;
1783 }
1784 id = uaudio_find_iface(tbuf, size, &offs,UISUBCLASS_AUDIOSTREAM);
1785 if (id == NULL)
1786 break;
1787 }
1788 if (offs > size)
1789 return USBD_INVAL;
1790 DPRINTF("%d alts available\n", sc->sc_nalts);
1791
1792 if (sc->sc_mode == 0) {
1793 aprint_error_dev(sc->sc_dev, "no usable endpoint found\n");
1794 return USBD_INVAL;
1795 }
1796
1797 /* build audio_format array */
1798 sc->sc_formats = kmem_alloc(sizeof(struct audio_format) * sc->sc_nalts,
1799 KM_SLEEP);
1800 sc->sc_nformats = sc->sc_nalts;
1801 for (i = 0; i < sc->sc_nalts; i++) {
1802 auf = &sc->sc_formats[i];
1803 t1desc = sc->sc_alts[i].asf1desc;
1804 auf->driver_data = NULL;
1805 if (UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress) == UE_DIR_OUT)
1806 auf->mode = AUMODE_PLAY;
1807 else
1808 auf->mode = AUMODE_RECORD;
1809 auf->encoding = sc->sc_alts[i].encoding;
1810 auf->validbits = t1desc->bBitResolution;
1811 auf->precision = t1desc->bSubFrameSize * 8;
1812 auf->channels = t1desc->bNrChannels;
1813 auf->channel_mask = sc->sc_channel_config;
1814 auf->frequency_type = t1desc->bSamFreqType;
1815 if (t1desc->bSamFreqType == UA_SAMP_CONTNUOUS) {
1816 auf->frequency[0] = UA_SAMP_LO(t1desc);
1817 auf->frequency[1] = UA_SAMP_HI(t1desc);
1818 } else {
1819 for (j = 0; j < t1desc->bSamFreqType; j++) {
1820 if (j >= AUFMT_MAX_FREQUENCIES) {
1821 aprint_error("%s: please increase "
1822 "AUFMT_MAX_FREQUENCIES to %d\n",
1823 __func__, t1desc->bSamFreqType);
1824 auf->frequency_type =
1825 AUFMT_MAX_FREQUENCIES;
1826 break;
1827 }
1828 auf->frequency[j] = UA_GETSAMP(t1desc, j);
1829 }
1830 }
1831 sc->sc_alts[i].aformat = auf;
1832 }
1833
1834 return USBD_NORMAL_COMPLETION;
1835 }
1836
1837 #ifdef UAUDIO_DEBUG
1838 Static void
1839 uaudio_dump_tml(struct terminal_list *tml) {
1840 if (tml == NULL) {
1841 printf("NULL");
1842 } else {
1843 int i;
1844 for (i = 0; i < tml->size; i++)
1845 printf("%s ", uaudio_get_terminal_name
1846 (tml->terminals[i]));
1847 }
1848 printf("\n");
1849 }
1850 #endif
1851
1852 Static usbd_status
1853 uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
1854 {
1855 struct io_terminal* iot;
1856 const usb_interface_descriptor_t *id;
1857 const struct usb_audio_control_descriptor *acdp;
1858 const uaudio_cs_descriptor_t *dp;
1859 const struct usb_audio_output_terminal *pot;
1860 struct terminal_list *tml;
1861 const char *tbuf, *ibuf, *ibufend;
1862 int size, offs, ndps, i, j;
1863
1864 size = UGETW(cdesc->wTotalLength);
1865 tbuf = (const char *)cdesc;
1866
1867 /* Locate the AudioControl interface descriptor. */
1868 offs = 0;
1869 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOCONTROL);
1870 if (id == NULL)
1871 return USBD_INVAL;
1872 if (offs + sizeof(*acdp) > size)
1873 return USBD_INVAL;
1874 sc->sc_ac_iface = id->bInterfaceNumber;
1875 DPRINTFN(2,"AC interface is %d\n", sc->sc_ac_iface);
1876
1877 /* A class-specific AC interface header should follow. */
1878 ibuf = tbuf + offs;
1879 ibufend = tbuf + size;
1880 acdp = (const struct usb_audio_control_descriptor *)ibuf;
1881 if (acdp->bDescriptorType != UDESC_CS_INTERFACE ||
1882 acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)
1883 return USBD_INVAL;
1884
1885 if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC) &&
1886 UGETW(acdp->bcdADC) != UAUDIO_VERSION)
1887 return USBD_INVAL;
1888
1889 sc->sc_audio_rev = UGETW(acdp->bcdADC);
1890 DPRINTFN(2, "found AC header, vers=%03x\n", sc->sc_audio_rev);
1891
1892 sc->sc_nullalt = -1;
1893
1894 /* Scan through all the AC specific descriptors */
1895 dp = (const uaudio_cs_descriptor_t *)ibuf;
1896 ndps = 0;
1897 iot = malloc(sizeof(struct io_terminal) * 256, M_TEMP, M_NOWAIT | M_ZERO);
1898 if (iot == NULL) {
1899 aprint_error("%s: no memory\n", __func__);
1900 return USBD_NOMEM;
1901 }
1902 for (;;) {
1903 ibuf += dp->bLength;
1904 if (ibuf >= ibufend)
1905 break;
1906 dp = (const uaudio_cs_descriptor_t *)ibuf;
1907 if (ibuf + dp->bLength > ibufend) {
1908 free(iot, M_TEMP);
1909 return USBD_INVAL;
1910 }
1911 if (dp->bDescriptorType != UDESC_CS_INTERFACE)
1912 break;
1913 i = ((const struct usb_audio_input_terminal *)dp)->bTerminalId;
1914 iot[i].d.desc = dp;
1915 if (i > ndps)
1916 ndps = i;
1917 }
1918 ndps++;
1919
1920 /* construct io_terminal */
1921 for (i = 0; i < ndps; i++) {
1922 dp = iot[i].d.desc;
1923 if (dp == NULL)
1924 continue;
1925 if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT)
1926 continue;
1927 pot = iot[i].d.ot;
1928 tml = uaudio_io_terminaltype(UGETW(pot->wTerminalType), iot, i);
1929 if (tml != NULL)
1930 free(tml, M_TEMP);
1931 }
1932
1933 #ifdef UAUDIO_DEBUG
1934 for (i = 0; i < 256; i++) {
1935 struct usb_audio_cluster cluster;
1936
1937 if (iot[i].d.desc == NULL)
1938 continue;
1939 printf("id %d:\t", i);
1940 switch (iot[i].d.desc->bDescriptorSubtype) {
1941 case UDESCSUB_AC_INPUT:
1942 printf("AC_INPUT type=%s\n", uaudio_get_terminal_name
1943 (UGETW(iot[i].d.it->wTerminalType)));
1944 printf("\t");
1945 cluster = uaudio_get_cluster(i, iot);
1946 uaudio_dump_cluster(&cluster);
1947 printf("\n");
1948 break;
1949 case UDESCSUB_AC_OUTPUT:
1950 printf("AC_OUTPUT type=%s ", uaudio_get_terminal_name
1951 (UGETW(iot[i].d.ot->wTerminalType)));
1952 printf("src=%d\n", iot[i].d.ot->bSourceId);
1953 break;
1954 case UDESCSUB_AC_MIXER:
1955 printf("AC_MIXER src=");
1956 for (j = 0; j < iot[i].d.mu->bNrInPins; j++)
1957 printf("%d ", iot[i].d.mu->baSourceId[j]);
1958 printf("\n\t");
1959 cluster = uaudio_get_cluster(i, iot);
1960 uaudio_dump_cluster(&cluster);
1961 printf("\n");
1962 break;
1963 case UDESCSUB_AC_SELECTOR:
1964 printf("AC_SELECTOR src=");
1965 for (j = 0; j < iot[i].d.su->bNrInPins; j++)
1966 printf("%d ", iot[i].d.su->baSourceId[j]);
1967 printf("\n");
1968 break;
1969 case UDESCSUB_AC_FEATURE:
1970 printf("AC_FEATURE src=%d\n", iot[i].d.fu->bSourceId);
1971 break;
1972 case UDESCSUB_AC_PROCESSING:
1973 printf("AC_PROCESSING src=");
1974 for (j = 0; j < iot[i].d.pu->bNrInPins; j++)
1975 printf("%d ", iot[i].d.pu->baSourceId[j]);
1976 printf("\n\t");
1977 cluster = uaudio_get_cluster(i, iot);
1978 uaudio_dump_cluster(&cluster);
1979 printf("\n");
1980 break;
1981 case UDESCSUB_AC_EXTENSION:
1982 printf("AC_EXTENSION src=");
1983 for (j = 0; j < iot[i].d.eu->bNrInPins; j++)
1984 printf("%d ", iot[i].d.eu->baSourceId[j]);
1985 printf("\n\t");
1986 cluster = uaudio_get_cluster(i, iot);
1987 uaudio_dump_cluster(&cluster);
1988 printf("\n");
1989 break;
1990 default:
1991 printf("unknown audio control (subtype=%d)\n",
1992 iot[i].d.desc->bDescriptorSubtype);
1993 }
1994 for (j = 0; j < iot[i].inputs_size; j++) {
1995 printf("\tinput%d: ", j);
1996 uaudio_dump_tml(iot[i].inputs[j]);
1997 }
1998 printf("\toutput: ");
1999 uaudio_dump_tml(iot[i].output);
2000 }
2001 #endif
2002
2003 for (i = 0; i < ndps; i++) {
2004 dp = iot[i].d.desc;
2005 if (dp == NULL)
2006 continue;
2007 DPRINTF("id=%d subtype=%d\n", i, dp->bDescriptorSubtype);
2008 switch (dp->bDescriptorSubtype) {
2009 case UDESCSUB_AC_HEADER:
2010 aprint_error("uaudio_identify_ac: unexpected AC header\n");
2011 break;
2012 case UDESCSUB_AC_INPUT:
2013 uaudio_add_input(sc, iot, i);
2014 break;
2015 case UDESCSUB_AC_OUTPUT:
2016 uaudio_add_output(sc, iot, i);
2017 break;
2018 case UDESCSUB_AC_MIXER:
2019 uaudio_add_mixer(sc, iot, i);
2020 break;
2021 case UDESCSUB_AC_SELECTOR:
2022 uaudio_add_selector(sc, iot, i);
2023 break;
2024 case UDESCSUB_AC_FEATURE:
2025 uaudio_add_feature(sc, iot, i);
2026 break;
2027 case UDESCSUB_AC_PROCESSING:
2028 uaudio_add_processing(sc, iot, i);
2029 break;
2030 case UDESCSUB_AC_EXTENSION:
2031 uaudio_add_extension(sc, iot, i);
2032 break;
2033 default:
2034 aprint_error(
2035 "uaudio_identify_ac: bad AC desc subtype=0x%02x\n",
2036 dp->bDescriptorSubtype);
2037 break;
2038 }
2039 }
2040
2041 /* delete io_terminal */
2042 for (i = 0; i < 256; i++) {
2043 if (iot[i].d.desc == NULL)
2044 continue;
2045 if (iot[i].inputs != NULL) {
2046 for (j = 0; j < iot[i].inputs_size; j++) {
2047 if (iot[i].inputs[j] != NULL)
2048 free(iot[i].inputs[j], M_TEMP);
2049 }
2050 free(iot[i].inputs, M_TEMP);
2051 }
2052 if (iot[i].output != NULL)
2053 free(iot[i].output, M_TEMP);
2054 iot[i].d.desc = NULL;
2055 }
2056 free(iot, M_TEMP);
2057
2058 return USBD_NORMAL_COMPLETION;
2059 }
2060
2061 Static int
2062 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi)
2063 {
2064 struct uaudio_softc *sc;
2065 struct mixerctl *mc;
2066 int n, nctls, i;
2067
2068 DPRINTFN(7, "index=%d\n", mi->index);
2069 sc = addr;
2070 if (sc->sc_dying)
2071 return EIO;
2072
2073 n = mi->index;
2074 nctls = sc->sc_nctls;
2075
2076 switch (n) {
2077 case UAC_OUTPUT:
2078 mi->type = AUDIO_MIXER_CLASS;
2079 mi->mixer_class = UAC_OUTPUT;
2080 mi->next = mi->prev = AUDIO_MIXER_LAST;
2081 strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name));
2082 return 0;
2083 case UAC_INPUT:
2084 mi->type = AUDIO_MIXER_CLASS;
2085 mi->mixer_class = UAC_INPUT;
2086 mi->next = mi->prev = AUDIO_MIXER_LAST;
2087 strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name));
2088 return 0;
2089 case UAC_EQUAL:
2090 mi->type = AUDIO_MIXER_CLASS;
2091 mi->mixer_class = UAC_EQUAL;
2092 mi->next = mi->prev = AUDIO_MIXER_LAST;
2093 strlcpy(mi->label.name, AudioCequalization,
2094 sizeof(mi->label.name));
2095 return 0;
2096 case UAC_RECORD:
2097 mi->type = AUDIO_MIXER_CLASS;
2098 mi->mixer_class = UAC_RECORD;
2099 mi->next = mi->prev = AUDIO_MIXER_LAST;
2100 strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name));
2101 return 0;
2102 default:
2103 break;
2104 }
2105
2106 n -= UAC_NCLASSES;
2107 if (n < 0 || n >= nctls)
2108 return ENXIO;
2109
2110 mc = &sc->sc_ctls[n];
2111 strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name));
2112 mi->mixer_class = mc->class;
2113 mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */
2114 switch (mc->type) {
2115 case MIX_ON_OFF:
2116 mi->type = AUDIO_MIXER_ENUM;
2117 mi->un.e.num_mem = 2;
2118 strlcpy(mi->un.e.member[0].label.name, AudioNoff,
2119 sizeof(mi->un.e.member[0].label.name));
2120 mi->un.e.member[0].ord = 0;
2121 strlcpy(mi->un.e.member[1].label.name, AudioNon,
2122 sizeof(mi->un.e.member[1].label.name));
2123 mi->un.e.member[1].ord = 1;
2124 break;
2125 case MIX_SELECTOR:
2126 mi->type = AUDIO_MIXER_ENUM;
2127 mi->un.e.num_mem = mc->maxval - mc->minval + 1;
2128 for (i = 0; i <= mc->maxval - mc->minval; i++) {
2129 snprintf(mi->un.e.member[i].label.name,
2130 sizeof(mi->un.e.member[i].label.name),
2131 "%d", i + mc->minval);
2132 mi->un.e.member[i].ord = i + mc->minval;
2133 }
2134 break;
2135 default:
2136 mi->type = AUDIO_MIXER_VALUE;
2137 strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN);
2138 mi->un.v.num_channels = mc->nchan;
2139 mi->un.v.delta = mc->delta;
2140 break;
2141 }
2142 return 0;
2143 }
2144
2145 Static int
2146 uaudio_open(void *addr, int flags)
2147 {
2148 struct uaudio_softc *sc;
2149
2150 sc = addr;
2151 DPRINTF("sc=%p\n", sc);
2152 if (sc->sc_dying)
2153 return EIO;
2154
2155 if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY))
2156 return EACCES;
2157 if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD))
2158 return EACCES;
2159
2160 return 0;
2161 }
2162
2163 Static int
2164 uaudio_halt_out_dma(void *addr)
2165 {
2166 struct uaudio_softc *sc = addr;
2167
2168 DPRINTF("%s", "enter\n");
2169
2170 mutex_exit(&sc->sc_intr_lock);
2171 if (sc->sc_playchan.pipe != NULL) {
2172 uaudio_chan_abort(sc, &sc->sc_playchan);
2173 uaudio_chan_free_buffers(sc, &sc->sc_playchan);
2174 uaudio_chan_close(sc, &sc->sc_playchan);
2175 sc->sc_playchan.intr = NULL;
2176 }
2177 mutex_enter(&sc->sc_intr_lock);
2178
2179 return 0;
2180 }
2181
2182 Static int
2183 uaudio_halt_in_dma(void *addr)
2184 {
2185 struct uaudio_softc *sc = addr;
2186
2187 DPRINTF("%s", "enter\n");
2188
2189 mutex_exit(&sc->sc_intr_lock);
2190 if (sc->sc_recchan.pipe != NULL) {
2191 uaudio_chan_abort(sc, &sc->sc_recchan);
2192 uaudio_chan_free_buffers(sc, &sc->sc_recchan);
2193 uaudio_chan_close(sc, &sc->sc_recchan);
2194 sc->sc_recchan.intr = NULL;
2195 }
2196 mutex_enter(&sc->sc_intr_lock);
2197
2198 return 0;
2199 }
2200
2201 Static int
2202 uaudio_getdev(void *addr, struct audio_device *retp)
2203 {
2204 struct uaudio_softc *sc;
2205
2206 DPRINTF("%s", "\n");
2207 sc = addr;
2208 if (sc->sc_dying)
2209 return EIO;
2210
2211 *retp = sc->sc_adev;
2212 return 0;
2213 }
2214
2215 /*
2216 * Make sure the block size is large enough to hold all outstanding transfers.
2217 */
2218 Static int
2219 uaudio_round_blocksize(void *addr, int blk,
2220 int mode, const audio_params_t *param)
2221 {
2222 struct uaudio_softc *sc;
2223 int b;
2224
2225 sc = addr;
2226 DPRINTF("blk=%d mode=%s\n", blk,
2227 mode == AUMODE_PLAY ? "AUMODE_PLAY" : "AUMODE_RECORD");
2228
2229 /* chan.bytes_per_frame can be 0. */
2230 if (mode == AUMODE_PLAY || sc->sc_recchan.bytes_per_frame <= 0) {
2231 b = param->sample_rate * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2232
2233 /*
2234 * This does not make accurate value in the case
2235 * of b % USB_FRAMES_PER_SECOND != 0
2236 */
2237 b /= USB_FRAMES_PER_SECOND;
2238
2239 b *= param->precision / 8 * param->channels;
2240 } else {
2241 /*
2242 * use wMaxPacketSize in bytes_per_frame.
2243 * See uaudio_set_format() and uaudio_chan_init()
2244 */
2245 b = sc->sc_recchan.bytes_per_frame
2246 * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2247 }
2248
2249 if (b <= 0)
2250 b = 1;
2251 blk = blk <= b ? b : blk / b * b;
2252
2253 #ifdef DIAGNOSTIC
2254 if (blk <= 0) {
2255 aprint_debug("uaudio_round_blocksize: blk=%d\n", blk);
2256 blk = 512;
2257 }
2258 #endif
2259
2260 DPRINTF("resultant blk=%d\n", blk);
2261 return blk;
2262 }
2263
2264 Static int
2265 uaudio_get_props(void *addr)
2266 {
2267 struct uaudio_softc *sc;
2268 int props;
2269
2270 sc = addr;
2271 props = 0;
2272 if ((sc->sc_mode & AUMODE_PLAY))
2273 props |= AUDIO_PROP_PLAYBACK;
2274 if ((sc->sc_mode & AUMODE_RECORD))
2275 props |= AUDIO_PROP_CAPTURE;
2276
2277 /* XXX I'm not sure all bidirectional devices support FULLDUP&INDEP */
2278 if (props == (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE))
2279 props |= AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
2280
2281 return props;
2282 }
2283
2284 Static void
2285 uaudio_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
2286 {
2287 struct uaudio_softc *sc;
2288
2289 sc = addr;
2290 *intr = &sc->sc_intr_lock;
2291 *thread = &sc->sc_lock;
2292 }
2293
2294 Static int
2295 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue,
2296 int wIndex, int len)
2297 {
2298 usb_device_request_t req;
2299 uint8_t data[4];
2300 usbd_status err;
2301 int val;
2302
2303 if (wValue == -1)
2304 return 0;
2305
2306 req.bmRequestType = type;
2307 req.bRequest = which;
2308 USETW(req.wValue, wValue);
2309 USETW(req.wIndex, wIndex);
2310 USETW(req.wLength, len);
2311 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
2312 "wIndex=0x%04x len=%d\n",
2313 type, which, wValue, wIndex, len);
2314 err = usbd_do_request(sc->sc_udev, &req, data);
2315 if (err) {
2316 DPRINTF("err=%s\n", usbd_errstr(err));
2317 return -1;
2318 }
2319 switch (len) {
2320 case 1:
2321 val = data[0];
2322 break;
2323 case 2:
2324 val = data[0] | (data[1] << 8);
2325 break;
2326 default:
2327 DPRINTF("bad length=%d\n", len);
2328 return -1;
2329 }
2330 DPRINTFN(2,"val=%d\n", val);
2331 return val;
2332 }
2333
2334 Static void
2335 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue,
2336 int wIndex, int len, int val)
2337 {
2338 usb_device_request_t req;
2339 uint8_t data[4];
2340 int err __unused;
2341
2342 if (wValue == -1)
2343 return;
2344
2345 req.bmRequestType = type;
2346 req.bRequest = which;
2347 USETW(req.wValue, wValue);
2348 USETW(req.wIndex, wIndex);
2349 USETW(req.wLength, len);
2350 switch (len) {
2351 case 1:
2352 data[0] = val;
2353 break;
2354 case 2:
2355 data[0] = val;
2356 data[1] = val >> 8;
2357 break;
2358 default:
2359 return;
2360 }
2361 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
2362 "wIndex=0x%04x len=%d, val=%d\n",
2363 type, which, wValue, wIndex, len, val & 0xffff);
2364 err = usbd_do_request(sc->sc_udev, &req, data);
2365 #ifdef UAUDIO_DEBUG
2366 if (err)
2367 DPRINTF("err=%d\n", err);
2368 #endif
2369 }
2370
2371 Static int
2372 uaudio_signext(int type, int val)
2373 {
2374 if (!MIX_UNSIGNED(type)) {
2375 if (MIX_SIZE(type) == 2)
2376 val = (int16_t)val;
2377 else
2378 val = (int8_t)val;
2379 }
2380 return val;
2381 }
2382
2383 Static int
2384 uaudio_value2bsd(struct mixerctl *mc, int val)
2385 {
2386 DPRINTFN(5, "type=%03x val=%d min=%d max=%d ",
2387 mc->type, val, mc->minval, mc->maxval);
2388 if (mc->type == MIX_ON_OFF) {
2389 val = (val != 0);
2390 } else if (mc->type == MIX_SELECTOR) {
2391 if (val < mc->minval || val > mc->maxval)
2392 val = mc->minval;
2393 } else
2394 val = ((uaudio_signext(mc->type, val) - mc->minval) * 255
2395 + mc->mul/2) / mc->mul;
2396 DPRINTFN_CLEAN(5, "val'=%d\n", val);
2397 return val;
2398 }
2399
2400 int
2401 uaudio_bsd2value(struct mixerctl *mc, int val)
2402 {
2403 DPRINTFN(5,"type=%03x val=%d min=%d max=%d ",
2404 mc->type, val, mc->minval, mc->maxval);
2405 if (mc->type == MIX_ON_OFF) {
2406 val = (val != 0);
2407 } else if (mc->type == MIX_SELECTOR) {
2408 if (val < mc->minval || val > mc->maxval)
2409 val = mc->minval;
2410 } else
2411 val = (val + mc->delta/2) * mc->mul / 255 + mc->minval;
2412 DPRINTFN_CLEAN(5, "val'=%d\n", val);
2413 return val;
2414 }
2415
2416 Static int
2417 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2418 int chan)
2419 {
2420 int val;
2421
2422 DPRINTFN(5,"which=%d chan=%d\n", which, chan);
2423 mutex_exit(&sc->sc_lock);
2424 val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan],
2425 mc->wIndex, MIX_SIZE(mc->type));
2426 mutex_enter(&sc->sc_lock);
2427 return uaudio_value2bsd(mc, val);
2428 }
2429
2430 Static void
2431 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2432 int chan, int val)
2433 {
2434
2435 val = uaudio_bsd2value(mc, val);
2436 mutex_exit(&sc->sc_lock);
2437 uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan],
2438 mc->wIndex, MIX_SIZE(mc->type), val);
2439 mutex_enter(&sc->sc_lock);
2440 }
2441
2442 Static int
2443 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp)
2444 {
2445 struct uaudio_softc *sc;
2446 struct mixerctl *mc;
2447 int i, n, vals[MIX_MAX_CHAN], val;
2448
2449 DPRINTFN(2, "index=%d\n", cp->dev);
2450 sc = addr;
2451 if (sc->sc_dying)
2452 return EIO;
2453
2454 n = cp->dev - UAC_NCLASSES;
2455 if (n < 0 || n >= sc->sc_nctls)
2456 return ENXIO;
2457 mc = &sc->sc_ctls[n];
2458
2459 if (mc->type == MIX_ON_OFF) {
2460 if (cp->type != AUDIO_MIXER_ENUM)
2461 return EINVAL;
2462 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2463 } else if (mc->type == MIX_SELECTOR) {
2464 if (cp->type != AUDIO_MIXER_ENUM)
2465 return EINVAL;
2466 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2467 } else {
2468 if (cp->type != AUDIO_MIXER_VALUE)
2469 return EINVAL;
2470 if (cp->un.value.num_channels != 1 &&
2471 cp->un.value.num_channels != mc->nchan)
2472 return EINVAL;
2473 for (i = 0; i < mc->nchan; i++)
2474 vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i);
2475 if (cp->un.value.num_channels == 1 && mc->nchan != 1) {
2476 for (val = 0, i = 0; i < mc->nchan; i++)
2477 val += vals[i];
2478 vals[0] = val / mc->nchan;
2479 }
2480 for (i = 0; i < cp->un.value.num_channels; i++)
2481 cp->un.value.level[i] = vals[i];
2482 }
2483
2484 return 0;
2485 }
2486
2487 Static int
2488 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp)
2489 {
2490 struct uaudio_softc *sc;
2491 struct mixerctl *mc;
2492 int i, n, vals[MIX_MAX_CHAN];
2493
2494 DPRINTFN(2, "index = %d\n", cp->dev);
2495 sc = addr;
2496 if (sc->sc_dying)
2497 return EIO;
2498
2499 n = cp->dev - UAC_NCLASSES;
2500 if (n < 0 || n >= sc->sc_nctls)
2501 return ENXIO;
2502 mc = &sc->sc_ctls[n];
2503
2504 if (mc->type == MIX_ON_OFF) {
2505 if (cp->type != AUDIO_MIXER_ENUM)
2506 return EINVAL;
2507 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2508 } else if (mc->type == MIX_SELECTOR) {
2509 if (cp->type != AUDIO_MIXER_ENUM)
2510 return EINVAL;
2511 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2512 } else {
2513 if (cp->type != AUDIO_MIXER_VALUE)
2514 return EINVAL;
2515 if (cp->un.value.num_channels == 1)
2516 for (i = 0; i < mc->nchan; i++)
2517 vals[i] = cp->un.value.level[0];
2518 else if (cp->un.value.num_channels == mc->nchan)
2519 for (i = 0; i < mc->nchan; i++)
2520 vals[i] = cp->un.value.level[i];
2521 else
2522 return EINVAL;
2523 for (i = 0; i < mc->nchan; i++)
2524 uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]);
2525 }
2526 return 0;
2527 }
2528
2529 Static int
2530 uaudio_trigger_input(void *addr, void *start, void *end, int blksize,
2531 void (*intr)(void *), void *arg,
2532 const audio_params_t *param)
2533 {
2534 struct uaudio_softc *sc;
2535 struct chan *ch;
2536 usbd_status err;
2537 int i;
2538
2539 sc = addr;
2540 if (sc->sc_dying)
2541 return EIO;
2542
2543 DPRINTFN(3, "sc=%p start=%p end=%p "
2544 "blksize=%d\n", sc, start, end, blksize);
2545 ch = &sc->sc_recchan;
2546 uaudio_chan_set_param(ch, start, end, blksize);
2547 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
2548 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2549 ch->fraction);
2550
2551 err = uaudio_chan_open(sc, ch);
2552 if (err) {
2553 return EIO;
2554 }
2555
2556 err = uaudio_chan_alloc_buffers(sc, ch);
2557 if (err) {
2558 uaudio_chan_close(sc, ch);
2559 return EIO;
2560 }
2561
2562
2563 ch->intr = intr;
2564 ch->arg = arg;
2565
2566 /*
2567 * Start as half as many channels for recording as for playback.
2568 * This stops playback from stuttering in full-duplex operation.
2569 */
2570 for (i = 0; i < UAUDIO_NCHANBUFS / 2; i++) {
2571 uaudio_chan_rtransfer(ch);
2572 }
2573
2574 return 0;
2575 }
2576
2577 Static int
2578 uaudio_trigger_output(void *addr, void *start, void *end, int blksize,
2579 void (*intr)(void *), void *arg,
2580 const audio_params_t *param)
2581 {
2582 struct uaudio_softc *sc;
2583 struct chan *ch;
2584 usbd_status err;
2585 int i;
2586
2587 sc = addr;
2588 if (sc->sc_dying)
2589 return EIO;
2590
2591 DPRINTFN(3, "sc=%p start=%p end=%p "
2592 "blksize=%d\n", sc, start, end, blksize);
2593 ch = &sc->sc_playchan;
2594 uaudio_chan_set_param(ch, start, end, blksize);
2595 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
2596 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2597 ch->fraction);
2598
2599 err = uaudio_chan_open(sc, ch);
2600 if (err) {
2601 return EIO;
2602 }
2603
2604 err = uaudio_chan_alloc_buffers(sc, ch);
2605 if (err) {
2606 uaudio_chan_close(sc, ch);
2607 return EIO;
2608 }
2609
2610 ch->intr = intr;
2611 ch->arg = arg;
2612
2613 for (i = 0; i < UAUDIO_NCHANBUFS; i++)
2614 uaudio_chan_ptransfer(ch);
2615
2616 return 0;
2617 }
2618
2619 /* Set up a pipe for a channel. */
2620 Static usbd_status
2621 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch)
2622 {
2623 struct as_info *as;
2624 usb_device_descriptor_t *ddesc;
2625 int endpt;
2626 usbd_status err;
2627
2628 as = &sc->sc_alts[ch->altidx];
2629 endpt = as->edesc->bEndpointAddress;
2630 DPRINTF("endpt=0x%02x, speed=%d, alt=%d\n",
2631 endpt, ch->sample_rate, as->alt);
2632
2633 /* Set alternate interface corresponding to the mode. */
2634 err = usbd_set_interface(as->ifaceh, as->alt);
2635 if (err)
2636 return err;
2637
2638 /*
2639 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request.
2640 */
2641 ddesc = usbd_get_device_descriptor(sc->sc_udev);
2642 if ((UGETW(ddesc->idVendor) != USB_VENDOR_ROLAND) &&
2643 (UGETW(ddesc->idProduct) != USB_PRODUCT_ROLAND_SD90)) {
2644 err = uaudio_set_speed(sc, endpt, ch->sample_rate);
2645 if (err) {
2646 DPRINTF("set_speed failed err=%s\n", usbd_errstr(err));
2647 }
2648 }
2649
2650 DPRINTF("create pipe to 0x%02x\n", endpt);
2651 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE, &ch->pipe);
2652 if (err)
2653 return err;
2654 if (as->edesc1 != NULL) {
2655 endpt = as->edesc1->bEndpointAddress;
2656 DPRINTF("create sync-pipe to 0x%02x\n", endpt);
2657 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE,
2658 &ch->sync_pipe);
2659 }
2660 return err;
2661 }
2662
2663 Static void
2664 uaudio_chan_abort(struct uaudio_softc *sc, struct chan *ch)
2665 {
2666 struct usbd_pipe *pipe;
2667 struct as_info *as;
2668
2669 as = &sc->sc_alts[ch->altidx];
2670 as->sc_busy = 0;
2671 if (sc->sc_nullalt >= 0) {
2672 DPRINTF("set null alt=%d\n", sc->sc_nullalt);
2673 usbd_set_interface(as->ifaceh, sc->sc_nullalt);
2674 }
2675 pipe = ch->pipe;
2676 if (pipe) {
2677 usbd_abort_pipe(pipe);
2678 }
2679 pipe = ch->sync_pipe;
2680 if (pipe) {
2681 usbd_abort_pipe(pipe);
2682 }
2683 }
2684
2685 Static void
2686 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch)
2687 {
2688 struct usbd_pipe *pipe;
2689
2690 pipe = atomic_swap_ptr(&ch->pipe, NULL);
2691 if (pipe) {
2692 usbd_close_pipe(pipe);
2693 }
2694 pipe = atomic_swap_ptr(&ch->sync_pipe, NULL);
2695 if (pipe) {
2696 usbd_close_pipe(pipe);
2697 }
2698 }
2699
2700 Static usbd_status
2701 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch)
2702 {
2703 int i, size;
2704
2705 size = (ch->bytes_per_frame + ch->sample_size) * UAUDIO_NFRAMES;
2706 for (i = 0; i < UAUDIO_NCHANBUFS; i++) {
2707 struct usbd_xfer *xfer;
2708
2709 int err = usbd_create_xfer(ch->pipe, size, 0, UAUDIO_NFRAMES,
2710 &xfer);
2711 if (err)
2712 goto bad;
2713
2714 ch->chanbufs[i].xfer = xfer;
2715 ch->chanbufs[i].buffer = usbd_get_buffer(xfer);
2716 ch->chanbufs[i].chan = ch;
2717 }
2718
2719 return USBD_NORMAL_COMPLETION;
2720
2721 bad:
2722 while (--i >= 0)
2723 /* implicit buffer free */
2724 usbd_destroy_xfer(ch->chanbufs[i].xfer);
2725 return USBD_NOMEM;
2726 }
2727
2728 Static void
2729 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch)
2730 {
2731 int i;
2732
2733 for (i = 0; i < UAUDIO_NCHANBUFS; i++)
2734 usbd_destroy_xfer(ch->chanbufs[i].xfer);
2735 }
2736
2737 Static void
2738 uaudio_chan_ptransfer(struct chan *ch)
2739 {
2740 struct chanbuf *cb;
2741 int i, n, size, residue, total;
2742
2743 if (ch->sc->sc_dying)
2744 return;
2745
2746 /* Pick the next channel buffer. */
2747 cb = &ch->chanbufs[ch->curchanbuf];
2748 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
2749 ch->curchanbuf = 0;
2750
2751 /* Compute the size of each frame in the next transfer. */
2752 residue = ch->residue;
2753 total = 0;
2754 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2755 size = ch->bytes_per_frame;
2756 residue += ch->fraction;
2757 if (residue >= USB_FRAMES_PER_SECOND) {
2758 if ((ch->sc->sc_altflags & UA_NOFRAC) == 0)
2759 size += ch->sample_size;
2760 residue -= USB_FRAMES_PER_SECOND;
2761 }
2762 cb->sizes[i] = size;
2763 total += size;
2764 }
2765 ch->residue = residue;
2766 cb->size = total;
2767
2768 /*
2769 * Transfer data from upper layer buffer to channel buffer, taking
2770 * care of wrapping the upper layer buffer.
2771 */
2772 n = uimin(total, ch->end - ch->cur);
2773 memcpy(cb->buffer, ch->cur, n);
2774 ch->cur += n;
2775 if (ch->cur >= ch->end)
2776 ch->cur = ch->start;
2777 if (total > n) {
2778 total -= n;
2779 memcpy(cb->buffer + n, ch->cur, total);
2780 ch->cur += total;
2781 }
2782
2783 #ifdef UAUDIO_DEBUG
2784 if (uaudiodebug > 8) {
2785 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
2786 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2787 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
2788 }
2789 }
2790 #endif
2791
2792 //DPRINTFN(5, "ptransfer xfer=%p\n", cb->xfer);
2793 /* Fill the request */
2794 usbd_setup_isoc_xfer(cb->xfer, cb, cb->sizes, UAUDIO_NFRAMES, 0,
2795 uaudio_chan_pintr);
2796
2797 (void)usbd_transfer(cb->xfer);
2798 }
2799
2800 Static void
2801 uaudio_chan_pintr(struct usbd_xfer *xfer, void *priv,
2802 usbd_status status)
2803 {
2804 struct chanbuf *cb;
2805 struct chan *ch;
2806 uint32_t count;
2807
2808 cb = priv;
2809 ch = cb->chan;
2810 /* Return if we are aborting. */
2811 if (status == USBD_CANCELLED)
2812 return;
2813
2814 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
2815 DPRINTFN(5, "count=%d, transferred=%d\n",
2816 count, ch->transferred);
2817 #ifdef DIAGNOSTIC
2818 if (count != cb->size) {
2819 aprint_error("uaudio_chan_pintr: count(%d) != size(%d)\n",
2820 count, cb->size);
2821 }
2822 #endif
2823
2824 mutex_enter(&ch->sc->sc_intr_lock);
2825 ch->transferred += cb->size;
2826 /* Call back to upper layer */
2827 while (ch->transferred >= ch->blksize) {
2828 ch->transferred -= ch->blksize;
2829 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
2830 ch->intr(ch->arg);
2831 }
2832 mutex_exit(&ch->sc->sc_intr_lock);
2833
2834 /* start next transfer */
2835 uaudio_chan_ptransfer(ch);
2836 }
2837
2838 Static void
2839 uaudio_chan_rtransfer(struct chan *ch)
2840 {
2841 struct chanbuf *cb;
2842 int i, size, residue, total;
2843
2844 if (ch->sc->sc_dying)
2845 return;
2846
2847 /* Pick the next channel buffer. */
2848 cb = &ch->chanbufs[ch->curchanbuf];
2849 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
2850 ch->curchanbuf = 0;
2851
2852 /* Compute the size of each frame in the next transfer. */
2853 residue = ch->residue;
2854 total = 0;
2855 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2856 size = ch->bytes_per_frame;
2857 cb->sizes[i] = size;
2858 cb->offsets[i] = total;
2859 total += size;
2860 }
2861 ch->residue = residue;
2862 cb->size = total;
2863
2864 #ifdef UAUDIO_DEBUG
2865 if (uaudiodebug > 8) {
2866 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
2867 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2868 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
2869 }
2870 }
2871 #endif
2872
2873 DPRINTFN(5, "transfer xfer=%p\n", cb->xfer);
2874 /* Fill the request */
2875 usbd_setup_isoc_xfer(cb->xfer, cb, cb->sizes, UAUDIO_NFRAMES, 0,
2876 uaudio_chan_rintr);
2877
2878 (void)usbd_transfer(cb->xfer);
2879 }
2880
2881 Static void
2882 uaudio_chan_rintr(struct usbd_xfer *xfer, void *priv,
2883 usbd_status status)
2884 {
2885 struct chanbuf *cb;
2886 struct chan *ch;
2887 uint32_t count;
2888 int i, n, frsize;
2889
2890 cb = priv;
2891 ch = cb->chan;
2892 /* Return if we are aborting. */
2893 if (status == USBD_CANCELLED)
2894 return;
2895
2896 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
2897 DPRINTFN(5, "count=%d, transferred=%d\n", count, ch->transferred);
2898
2899 /* count < cb->size is normal for asynchronous source */
2900 #ifdef DIAGNOSTIC
2901 if (count > cb->size) {
2902 aprint_error("uaudio_chan_rintr: count(%d) > size(%d)\n",
2903 count, cb->size);
2904 }
2905 #endif
2906
2907 /*
2908 * Transfer data from channel buffer to upper layer buffer, taking
2909 * care of wrapping the upper layer buffer.
2910 */
2911 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2912 frsize = cb->sizes[i];
2913 n = uimin(frsize, ch->end - ch->cur);
2914 memcpy(ch->cur, cb->buffer + cb->offsets[i], n);
2915 ch->cur += n;
2916 if (ch->cur >= ch->end)
2917 ch->cur = ch->start;
2918 if (frsize > n) {
2919 memcpy(ch->cur, cb->buffer + cb->offsets[i] + n,
2920 frsize - n);
2921 ch->cur += frsize - n;
2922 }
2923 }
2924
2925 /* Call back to upper layer */
2926 mutex_enter(&ch->sc->sc_intr_lock);
2927 ch->transferred += count;
2928 while (ch->transferred >= ch->blksize) {
2929 ch->transferred -= ch->blksize;
2930 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
2931 ch->intr(ch->arg);
2932 }
2933 mutex_exit(&ch->sc->sc_intr_lock);
2934
2935 /* start next transfer */
2936 uaudio_chan_rtransfer(ch);
2937 }
2938
2939 Static void
2940 uaudio_chan_init(struct chan *ch, int altidx, const struct audio_params *param,
2941 int maxpktsize)
2942 {
2943 int samples_per_frame, sample_size;
2944
2945 ch->altidx = altidx;
2946 sample_size = param->precision * param->channels / 8;
2947 samples_per_frame = param->sample_rate / USB_FRAMES_PER_SECOND;
2948 ch->sample_size = sample_size;
2949 ch->sample_rate = param->sample_rate;
2950 if (maxpktsize == 0) {
2951 ch->fraction = param->sample_rate % USB_FRAMES_PER_SECOND;
2952 ch->bytes_per_frame = samples_per_frame * sample_size;
2953 } else {
2954 ch->fraction = 0;
2955 ch->bytes_per_frame = maxpktsize;
2956 }
2957 ch->residue = 0;
2958 }
2959
2960 Static void
2961 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize)
2962 {
2963
2964 ch->start = start;
2965 ch->end = end;
2966 ch->cur = start;
2967 ch->blksize = blksize;
2968 ch->transferred = 0;
2969 ch->curchanbuf = 0;
2970 }
2971
2972 Static int
2973 uaudio_set_format(void *addr, int setmode,
2974 const audio_params_t *play, const audio_params_t *rec,
2975 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
2976 {
2977 struct uaudio_softc *sc;
2978 int paltidx, raltidx;
2979
2980 sc = addr;
2981 paltidx = -1;
2982 raltidx = -1;
2983 if (sc->sc_dying)
2984 return EIO;
2985
2986 if ((setmode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
2987 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0;
2988 }
2989 if ((setmode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
2990 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0;
2991 }
2992
2993 /* Some uaudio devices are unidirectional. Don't try to find a
2994 matching mode for the unsupported direction. */
2995 setmode &= sc->sc_mode;
2996
2997 if ((setmode & AUMODE_PLAY)) {
2998 paltidx = audio_indexof_format(sc->sc_formats, sc->sc_nformats,
2999 AUMODE_PLAY, play);
3000 /* Transfer should have halted */
3001 uaudio_chan_init(&sc->sc_playchan, paltidx, play, 0);
3002 }
3003 if ((setmode & AUMODE_RECORD)) {
3004 raltidx = audio_indexof_format(sc->sc_formats, sc->sc_nformats,
3005 AUMODE_RECORD, rec);
3006 /* Transfer should have halted */
3007 uaudio_chan_init(&sc->sc_recchan, raltidx, rec, 0);
3008 }
3009
3010 if ((setmode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
3011 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1;
3012 }
3013 if ((setmode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
3014 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1;
3015 }
3016
3017 DPRINTF("use altidx=p%d/r%d, altno=p%d/r%d\n",
3018 sc->sc_playchan.altidx, sc->sc_recchan.altidx,
3019 (sc->sc_playchan.altidx >= 0)
3020 ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting
3021 : -1,
3022 (sc->sc_recchan.altidx >= 0)
3023 ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting
3024 : -1);
3025
3026 return 0;
3027 }
3028
3029 Static usbd_status
3030 uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed)
3031 {
3032 usb_device_request_t req;
3033 usbd_status err;
3034 uint8_t data[3];
3035
3036 DPRINTFN(5, "endpt=%d speed=%u\n", endpt, speed);
3037 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
3038 req.bRequest = SET_CUR;
3039 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
3040 USETW(req.wIndex, endpt);
3041 USETW(req.wLength, 3);
3042 data[0] = speed;
3043 data[1] = speed >> 8;
3044 data[2] = speed >> 16;
3045
3046 err = usbd_do_request(sc->sc_udev, &req, data);
3047
3048 return err;
3049 }
3050
3051 #ifdef _MODULE
3052
3053 MODULE(MODULE_CLASS_DRIVER, uaudio, NULL);
3054
3055 static const struct cfiattrdata audiobuscf_iattrdata = {
3056 "audiobus", 0, { { NULL, NULL, 0 }, }
3057 };
3058 static const struct cfiattrdata * const uaudio_attrs[] = {
3059 &audiobuscf_iattrdata, NULL
3060 };
3061 CFDRIVER_DECL(uaudio, DV_DULL, uaudio_attrs);
3062 extern struct cfattach uaudio_ca;
3063 static int uaudioloc[6/*USBIFIFCF_NLOCS*/] = {
3064 -1/*USBIFIFCF_PORT_DEFAULT*/,
3065 -1/*USBIFIFCF_CONFIGURATION_DEFAULT*/,
3066 -1/*USBIFIFCF_INTERFACE_DEFAULT*/,
3067 -1/*USBIFIFCF_VENDOR_DEFAULT*/,
3068 -1/*USBIFIFCF_PRODUCT_DEFAULT*/,
3069 -1/*USBIFIFCF_RELEASE_DEFAULT*/};
3070 static struct cfparent uhubparent = {
3071 "usbifif", NULL, DVUNIT_ANY
3072 };
3073 static struct cfdata uaudio_cfdata[] = {
3074 {
3075 .cf_name = "uaudio",
3076 .cf_atname = "uaudio",
3077 .cf_unit = 0,
3078 .cf_fstate = FSTATE_STAR,
3079 .cf_loc = uaudioloc,
3080 .cf_flags = 0,
3081 .cf_pspec = &uhubparent,
3082 },
3083 { NULL }
3084 };
3085
3086 static int
3087 uaudio_modcmd(modcmd_t cmd, void *arg)
3088 {
3089 int err;
3090
3091 switch (cmd) {
3092 case MODULE_CMD_INIT:
3093 err = config_cfdriver_attach(&uaudio_cd);
3094 if (err) {
3095 return err;
3096 }
3097 err = config_cfattach_attach("uaudio", &uaudio_ca);
3098 if (err) {
3099 config_cfdriver_detach(&uaudio_cd);
3100 return err;
3101 }
3102 err = config_cfdata_attach(uaudio_cfdata, 1);
3103 if (err) {
3104 config_cfattach_detach("uaudio", &uaudio_ca);
3105 config_cfdriver_detach(&uaudio_cd);
3106 return err;
3107 }
3108 return 0;
3109 case MODULE_CMD_FINI:
3110 err = config_cfdata_detach(uaudio_cfdata);
3111 if (err)
3112 return err;
3113 config_cfattach_detach("uaudio", &uaudio_ca);
3114 config_cfdriver_detach(&uaudio_cd);
3115 return 0;
3116 default:
3117 return ENOTTY;
3118 }
3119 }
3120
3121 #endif
3122