uaudio.c revision 1.179 1 /* $NetBSD: uaudio.c,v 1.179 2023/04/16 19:26:20 mlelstv 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.179 2023/04/16 19:26:20 mlelstv 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 #include <sys/sysctl.h>
63
64 #include <sys/audioio.h>
65 #include <dev/audio/audio_if.h>
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdivar.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usb_quirks.h>
72
73 #include <dev/usb/usbdevs.h>
74
75 #include <dev/usb/uaudioreg.h>
76
77 /* #define UAUDIO_DEBUG */
78 #define UAUDIO_MULTIPLE_ENDPOINTS
79 #ifdef UAUDIO_DEBUG
80 #define DPRINTF(x,y...) do { \
81 if (uaudiodebug) { \
82 struct lwp *l = curlwp; \
83 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
84 } \
85 } while (0)
86 #define DPRINTFN_CLEAN(n,x...) do { \
87 if (uaudiodebug > (n)) \
88 printf(x); \
89 } while (0)
90 #define DPRINTFN(n,x,y...) do { \
91 if (uaudiodebug > (n)) { \
92 struct lwp *l = curlwp; \
93 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
94 } \
95 } while (0)
96 int uaudiodebug = 0;
97 #else
98 #define DPRINTF(x,y...)
99 #define DPRINTFN_CLEAN(n,x...)
100 #define DPRINTFN(n,x,y...)
101 #endif
102
103 /* number of outstanding requests */
104 #define UAUDIO_NCHANBUFS 6
105 /* number of USB frames per request, also the number of ms */
106 #define UAUDIO_NFRAMES 10
107 /* number of microframes per requewst (high, super) */
108 #define UAUDIO_NFRAMES_HI (UAUDIO_NFRAMES * USB_UFRAMES_PER_FRAME)
109
110
111 #define MIX_MAX_CHAN 8
112 struct range {
113 int minval, maxval, resval;
114 };
115
116 struct mixerctl {
117 uint16_t wValue[MIX_MAX_CHAN]; /* using nchan */
118 uint16_t wIndex;
119 uint8_t nchan;
120 uint8_t type;
121 #define MIX_ON_OFF 0x01
122 #define MIX_SELECTOR 0x02
123 #define MIX_SIGNED_8 0x10
124 #define MIX_UNSIGNED_8 0x18
125 #define MIX_SIGNED_16 0x20
126 #define MIX_UNSIGNED_16 0x28
127 #define MIX_SIGNED_32 0x40
128 #define MIX_UNSIGNED_32 0x48
129 #define MIX_SIZE(n) ( \
130 ((n) == MIX_UNSIGNED_32 || (n) == MIX_SIGNED_32) ? 4 : \
131 ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16) ? 2 : 1 )
132 #define MIX_UNSIGNED(n) ( \
133 (n) == MIX_UNSIGNED_8 || \
134 (n) == MIX_UNSIGNED_16 || \
135 (n) == MIX_UNSIGNED_32 )
136 struct range range0;
137 struct range *ranges;
138 u_int nranges;
139 u_int delta;
140 u_int mul;
141 uint8_t class;
142 char ctlname[MAX_AUDIO_DEV_LEN];
143 const char *ctlunit;
144 };
145 #define MAKE(h,l) (((h) << 8) | (l))
146
147 struct as_info {
148 uint8_t alt;
149 uint8_t encoding;
150 uint8_t nchan;
151 uint8_t attributes; /* Copy of bmAttributes of
152 * usb_audio_streaming_endpoint_descriptor
153 */
154 uint8_t terminal; /* connected Terminal ID */
155 struct usbd_interface * ifaceh;
156 const usb_interface_descriptor_t *idesc;
157 const usb_endpoint_descriptor_audio_t *edesc;
158 const usb_endpoint_descriptor_audio_t *edesc1;
159 const union usb_audio_streaming_type1_descriptor *asf1desc;
160 struct audio_format *aformat;
161 int sc_busy; /* currently used */
162 };
163
164 struct chan {
165 void (*intr)(void *); /* DMA completion intr handler */
166 void *arg; /* arg for intr() */
167 struct usbd_pipe *pipe;
168 struct usbd_pipe *sync_pipe;
169
170 u_int sample_size;
171 u_int sample_rate;
172 u_int bytes_per_frame;
173 u_int fraction; /* fraction/1000 is the extra samples/frame */
174 u_int residue; /* accumulates the fractional samples */
175
176 u_char *start; /* upper layer buffer start */
177 u_char *end; /* upper layer buffer end */
178 u_char *cur; /* current position in upper layer buffer */
179 int blksize; /* chunk size to report up */
180 int transferred; /* transferred bytes not reported up */
181
182 int altidx; /* currently used altidx */
183
184 int curchanbuf;
185 u_int nframes; /* UAUDIO_NFRAMES or UAUDIO_NFRAMES_HI */
186 u_int nchanbufs; /* 1..UAUDIO_NCHANBUFS */
187 struct chanbuf {
188 struct chan *chan;
189 struct usbd_xfer *xfer;
190 u_char *buffer;
191 uint16_t sizes[UAUDIO_NFRAMES_HI];
192 uint16_t offsets[UAUDIO_NFRAMES_HI];
193 uint16_t size;
194 } chanbufs[UAUDIO_NCHANBUFS];
195
196 struct uaudio_softc *sc; /* our softc */
197 };
198
199 /*
200 * The MI USB audio subsystem is now MP-SAFE and expects sc_intr_lock to be
201 * held on entry the callbacks passed to uaudio_trigger_{in,out}put
202 */
203 struct uaudio_softc {
204 device_t sc_dev; /* base device */
205 kmutex_t sc_lock;
206 kmutex_t sc_intr_lock;
207 struct usbd_device *sc_udev; /* USB device */
208 int sc_version;
209 int sc_ac_iface; /* Audio Control interface */
210 struct usbd_interface * sc_ac_ifaceh;
211 struct chan sc_playchan; /* play channel */
212 struct chan sc_recchan; /* record channel */
213 int sc_nullalt;
214 int sc_audio_rev;
215 struct as_info *sc_alts; /* alternate settings */
216 int sc_nalts; /* # of alternate settings */
217 int sc_altflags;
218 #define HAS_8 0x01
219 #define HAS_16 0x02
220 #define HAS_8U 0x04
221 #define HAS_ALAW 0x08
222 #define HAS_MULAW 0x10
223 #define UA_NOFRAC 0x20 /* don't do sample rate adjustment */
224 #define HAS_24 0x40
225 #define HAS_32 0x80
226 int sc_mode; /* play/record capability */
227 struct mixerctl *sc_ctls; /* mixer controls */
228 int sc_nctls; /* # of mixer controls */
229 device_t sc_audiodev;
230 int sc_nratectls; /* V2 sample rates */
231 int sc_ratectls[AUFMT_MAX_FREQUENCIES];
232 int sc_ratemode[AUFMT_MAX_FREQUENCIES];
233 int sc_playclock;
234 int sc_recclock;
235 struct audio_format *sc_formats;
236 int sc_nformats;
237 uint8_t sc_clock[256]; /* map terminals to clocks */
238 u_int sc_channel_config;
239 u_int sc_usb_frames_per_second;
240 char sc_dying;
241 struct audio_device sc_adev;
242 };
243
244 struct terminal_list {
245 int size;
246 uint16_t terminals[1];
247 };
248 #define TERMINAL_LIST_SIZE(N) (offsetof(struct terminal_list, terminals) \
249 + sizeof(uint16_t) * (N))
250
251 struct io_terminal {
252 union {
253 const uaudio_cs_descriptor_t *desc;
254 const union usb_audio_input_terminal *it;
255 const union usb_audio_output_terminal *ot;
256 const struct usb_audio_mixer_unit *mu;
257 const struct usb_audio_selector_unit *su;
258 const union usb_audio_feature_unit *fu;
259 const struct usb_audio_processing_unit *pu;
260 const struct usb_audio_extension_unit *eu;
261 const struct usb_audio_clksrc_unit *cu;
262 const struct usb_audio_clksel_unit *lu;
263 } d;
264 int inputs_size;
265 struct terminal_list **inputs; /* list of source input terminals */
266 struct terminal_list *output; /* list of destination output terminals */
267 int direct; /* directly connected to an output terminal */
268 uint8_t clock;
269 };
270
271 #define UAC_OUTPUT 0
272 #define UAC_INPUT 1
273 #define UAC_EQUAL 2
274 #define UAC_RECORD 3
275 #define UAC_NCLASSES 4
276 #ifdef UAUDIO_DEBUG
277 Static const char *uac_names[] = {
278 AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord
279 };
280 #endif
281
282 #ifdef UAUDIO_DEBUG
283 Static void uaudio_dump_tml
284 (struct terminal_list *tml);
285 #endif
286 Static usbd_status uaudio_identify_ac
287 (struct uaudio_softc *, const usb_config_descriptor_t *);
288 Static usbd_status uaudio_identify_as
289 (struct uaudio_softc *, const usb_config_descriptor_t *);
290 Static usbd_status uaudio_process_as
291 (struct uaudio_softc *, const char *, int *, int,
292 const usb_interface_descriptor_t *);
293
294 Static void uaudio_add_alt(struct uaudio_softc *, const struct as_info *);
295
296 Static const usb_interface_descriptor_t *uaudio_find_iface
297 (const char *, int, int *, int);
298
299 Static void uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *);
300 Static char *uaudio_id_name
301 (struct uaudio_softc *, const struct io_terminal *, uint8_t);
302 #ifdef UAUDIO_DEBUG
303 Static void uaudio_dump_cluster
304 (struct uaudio_softc *, const union usb_audio_cluster *);
305 #endif
306 Static union usb_audio_cluster uaudio_get_cluster
307 (struct uaudio_softc *, int, const struct io_terminal *);
308 Static void uaudio_add_input
309 (struct uaudio_softc *, const struct io_terminal *, int);
310 Static void uaudio_add_output
311 (struct uaudio_softc *, const struct io_terminal *, int);
312 Static void uaudio_add_mixer
313 (struct uaudio_softc *, const struct io_terminal *, int);
314 Static void uaudio_add_selector
315 (struct uaudio_softc *, const struct io_terminal *, int);
316 #ifdef UAUDIO_DEBUG
317 Static const char *uaudio_get_terminal_name(int);
318 #endif
319 Static int uaudio_determine_class
320 (const struct io_terminal *, struct mixerctl *);
321 Static const char *uaudio_feature_name
322 (const struct io_terminal *, uint8_t, int);
323 Static void uaudio_add_feature
324 (struct uaudio_softc *, const struct io_terminal *, int);
325 Static void uaudio_add_processing_updown
326 (struct uaudio_softc *, const struct io_terminal *, int);
327 Static void uaudio_add_processing
328 (struct uaudio_softc *, const struct io_terminal *, int);
329 Static void uaudio_add_effect
330 (struct uaudio_softc *, const struct io_terminal *, int);
331 Static void uaudio_add_extension
332 (struct uaudio_softc *, const struct io_terminal *, int);
333 Static void uaudio_add_clksrc
334 (struct uaudio_softc *, const struct io_terminal *, int);
335 Static void uaudio_add_clksel
336 (struct uaudio_softc *, const struct io_terminal *, int);
337 Static struct terminal_list *uaudio_merge_terminal_list
338 (const struct io_terminal *);
339 Static struct terminal_list *uaudio_io_terminaltype
340 (struct uaudio_softc *, int, struct io_terminal *, int);
341 Static usbd_status uaudio_identify
342 (struct uaudio_softc *, const usb_config_descriptor_t *);
343 Static u_int uaudio_get_rates
344 (struct uaudio_softc *, int, u_int *, u_int);
345 Static void uaudio_build_formats
346 (struct uaudio_softc *);
347
348 Static int uaudio_signext(int, int);
349 Static int uaudio_value2bsd(struct mixerctl *, int);
350 Static int uaudio_bsd2value(struct mixerctl *, int);
351 Static const char *uaudio_clockname(u_int);
352 Static int uaudio_makename
353 (struct uaudio_softc *, uByte, const char *, uByte, char *, size_t);
354 Static int uaudio_get(struct uaudio_softc *, int, int, int, int, int);
355 Static int uaudio_getbuf(struct uaudio_softc *, int, int, int, int, int, uint8_t *);
356 Static int uaudio_ctl_get
357 (struct uaudio_softc *, int, struct mixerctl *, int);
358 Static void uaudio_set
359 (struct uaudio_softc *, int, int, int, int, int, int);
360 Static void uaudio_ctl_set
361 (struct uaudio_softc *, int, struct mixerctl *, int, int);
362
363 Static usbd_status uaudio_speed(struct uaudio_softc *, int, int, uint8_t *, int);
364 Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, int, u_int);
365
366 Static usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *);
367 Static void uaudio_chan_abort(struct uaudio_softc *, struct chan *);
368 Static void uaudio_chan_close(struct uaudio_softc *, struct chan *);
369 Static usbd_status uaudio_chan_alloc_buffers
370 (struct uaudio_softc *, struct chan *);
371 Static void uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *);
372 Static void uaudio_chan_init
373 (struct chan *, int, const struct audio_params *, int, bool);
374 Static void uaudio_chan_set_param(struct chan *, u_char *, u_char *, int);
375 Static void uaudio_chan_ptransfer(struct chan *);
376 Static void uaudio_chan_pintr
377 (struct usbd_xfer *, void *, usbd_status);
378
379 Static void uaudio_chan_rtransfer(struct chan *);
380 Static void uaudio_chan_rintr
381 (struct usbd_xfer *, void *, usbd_status);
382
383 Static int uaudio_open(void *, int);
384 Static int uaudio_query_format(void *, audio_format_query_t *);
385 Static int uaudio_set_format
386 (void *, int, const audio_params_t *, const audio_params_t *,
387 audio_filter_reg_t *, audio_filter_reg_t *);
388 Static int uaudio_round_blocksize(void *, int, int, const audio_params_t *);
389 Static int uaudio_trigger_output
390 (void *, void *, void *, int, void (*)(void *), void *,
391 const audio_params_t *);
392 Static int uaudio_trigger_input
393 (void *, void *, void *, int, void (*)(void *), void *,
394 const audio_params_t *);
395 Static int uaudio_halt_in_dma(void *);
396 Static int uaudio_halt_out_dma(void *);
397 Static void uaudio_halt_in_dma_unlocked(struct uaudio_softc *);
398 Static void uaudio_halt_out_dma_unlocked(struct uaudio_softc *);
399 Static int uaudio_getdev(void *, struct audio_device *);
400 Static int uaudio_mixer_set_port(void *, mixer_ctrl_t *);
401 Static int uaudio_mixer_get_port(void *, mixer_ctrl_t *);
402 Static int uaudio_query_devinfo(void *, mixer_devinfo_t *);
403 Static int uaudio_get_props(void *);
404 Static void uaudio_get_locks(void *, kmutex_t **, kmutex_t **);
405
406 Static const struct audio_hw_if uaudio_hw_if = {
407 .open = uaudio_open,
408 .query_format = uaudio_query_format,
409 .set_format = uaudio_set_format,
410 .round_blocksize = uaudio_round_blocksize,
411 .halt_output = uaudio_halt_out_dma,
412 .halt_input = uaudio_halt_in_dma,
413 .getdev = uaudio_getdev,
414 .set_port = uaudio_mixer_set_port,
415 .get_port = uaudio_mixer_get_port,
416 .query_devinfo = uaudio_query_devinfo,
417 .get_props = uaudio_get_props,
418 .trigger_output = uaudio_trigger_output,
419 .trigger_input = uaudio_trigger_input,
420 .get_locks = uaudio_get_locks,
421 };
422
423 static int uaudio_match(device_t, cfdata_t, void *);
424 static void uaudio_attach(device_t, device_t, void *);
425 static int uaudio_detach(device_t, int);
426 static void uaudio_childdet(device_t, device_t);
427 static int uaudio_activate(device_t, enum devact);
428
429
430 CFATTACH_DECL2_NEW(uaudio, sizeof(struct uaudio_softc),
431 uaudio_match, uaudio_attach, uaudio_detach, uaudio_activate, NULL,
432 uaudio_childdet);
433
434 static int
435 uaudio_match(device_t parent, cfdata_t match, void *aux)
436 {
437 struct usbif_attach_arg *uiaa = aux;
438
439 /* Trigger on the control interface. */
440 if (uiaa->uiaa_class != UICLASS_AUDIO ||
441 uiaa->uiaa_subclass != UISUBCLASS_AUDIOCONTROL ||
442 (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_BAD_AUDIO))
443 return UMATCH_NONE;
444
445 return UMATCH_IFACECLASS_IFACESUBCLASS;
446 }
447
448 static void
449 uaudio_attach(device_t parent, device_t self, void *aux)
450 {
451 struct uaudio_softc *sc = device_private(self);
452 struct usbif_attach_arg *uiaa = aux;
453 usb_interface_descriptor_t *id;
454 usb_config_descriptor_t *cdesc;
455 char *devinfop;
456 usbd_status err;
457 int i, j, found;
458
459 sc->sc_dev = self;
460 sc->sc_udev = uiaa->uiaa_device;
461 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
462 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
463
464 strlcpy(sc->sc_adev.name, "USB audio", sizeof(sc->sc_adev.name));
465 strlcpy(sc->sc_adev.version, "", sizeof(sc->sc_adev.version));
466 snprintf(sc->sc_adev.config, sizeof(sc->sc_adev.config), "usb:%08x",
467 sc->sc_udev->ud_cookie.cookie);
468
469 aprint_naive("\n");
470 aprint_normal("\n");
471
472 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
473 aprint_normal_dev(self, "%s\n", devinfop);
474 usbd_devinfo_free(devinfop);
475
476 cdesc = usbd_get_config_descriptor(sc->sc_udev);
477 if (cdesc == NULL) {
478 aprint_error_dev(self,
479 "failed to get configuration descriptor\n");
480 return;
481 }
482
483 err = uaudio_identify(sc, cdesc);
484 if (err) {
485 aprint_error_dev(self,
486 "audio descriptors make no sense, error=%d\n", err);
487 return;
488 }
489
490 sc->sc_ac_ifaceh = uiaa->uiaa_iface;
491 /* Pick up the AS interface. */
492 for (i = 0; i < uiaa->uiaa_nifaces; i++) {
493 if (uiaa->uiaa_ifaces[i] == NULL)
494 continue;
495 id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
496 if (id == NULL)
497 continue;
498 found = 0;
499 for (j = 0; j < sc->sc_nalts; j++) {
500 if (id->bInterfaceNumber ==
501 sc->sc_alts[j].idesc->bInterfaceNumber) {
502 sc->sc_alts[j].ifaceh = uiaa->uiaa_ifaces[i];
503 found = 1;
504 }
505 }
506 if (found)
507 uiaa->uiaa_ifaces[i] = NULL;
508 }
509
510 for (j = 0; j < sc->sc_nalts; j++) {
511 if (sc->sc_alts[j].ifaceh == NULL) {
512 aprint_error_dev(self,
513 "alt %d missing AS interface(s)\n", j);
514 return;
515 }
516 }
517
518 aprint_normal_dev(self, "audio rev %d.%02x\n",
519 sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff);
520
521 sc->sc_playchan.sc = sc->sc_recchan.sc = sc;
522 sc->sc_playchan.altidx = -1;
523 sc->sc_recchan.altidx = -1;
524
525 switch (sc->sc_udev->ud_speed) {
526 case USB_SPEED_LOW:
527 case USB_SPEED_FULL:
528 sc->sc_usb_frames_per_second = USB_FRAMES_PER_SECOND;
529 sc->sc_playchan.nframes =
530 sc->sc_recchan.nframes = UAUDIO_NFRAMES;
531 break;
532 default: /* HIGH, SUPER, SUPER_PLUS, more ? */
533 sc->sc_usb_frames_per_second = USB_FRAMES_PER_SECOND * USB_UFRAMES_PER_FRAME;
534 sc->sc_playchan.nframes =
535 sc->sc_recchan.nframes = UAUDIO_NFRAMES_HI;
536 break;
537 }
538 sc->sc_playchan.nchanbufs =
539 sc->sc_recchan.nchanbufs = UAUDIO_NCHANBUFS;
540
541 DPRINTF("usb fps %u, max channel frames %u, max channel buffers %u\n",
542 sc->sc_usb_frames_per_second, sc->sc_playchan.nframes, sc->sc_playchan.nchanbufs);
543
544 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC)
545 sc->sc_altflags |= UA_NOFRAC;
546
547 #ifndef UAUDIO_DEBUG
548 if (bootverbose)
549 #endif
550 aprint_normal_dev(self, "%d mixer controls\n",
551 sc->sc_nctls);
552
553 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
554
555 DPRINTF("%s", "doing audio_attach_mi\n");
556 sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, sc->sc_dev);
557
558 if (!pmf_device_register(self, NULL, NULL))
559 aprint_error_dev(self, "couldn't establish power handler\n");
560
561 return;
562 }
563
564 static int
565 uaudio_activate(device_t self, enum devact act)
566 {
567 struct uaudio_softc *sc = device_private(self);
568
569 switch (act) {
570 case DVACT_DEACTIVATE:
571 sc->sc_dying = 1;
572 return 0;
573 default:
574 return EOPNOTSUPP;
575 }
576 }
577
578 static void
579 uaudio_childdet(device_t self, device_t child)
580 {
581 struct uaudio_softc *sc = device_private(self);
582
583 KASSERT(sc->sc_audiodev == child);
584 sc->sc_audiodev = NULL;
585 }
586
587 static int
588 uaudio_detach(device_t self, int flags)
589 {
590 struct uaudio_softc *sc = device_private(self);
591 int rv, i;
592
593 sc->sc_dying = 1;
594
595 pmf_device_deregister(self);
596
597 /* Wait for outstanding requests to complete. */
598 uaudio_halt_out_dma_unlocked(sc);
599 uaudio_halt_in_dma_unlocked(sc);
600
601 if (sc->sc_audiodev != NULL) {
602 rv = config_detach(sc->sc_audiodev, flags);
603 if (rv)
604 return rv;
605 }
606
607 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
608
609 if (sc->sc_formats != NULL)
610 kmem_free(sc->sc_formats,
611 sizeof(struct audio_format) * sc->sc_nformats);
612
613 if (sc->sc_ctls != NULL) {
614 for (i=0; i<sc->sc_nctls; ++i) {
615 if (sc->sc_ctls[i].nranges == 0)
616 continue;
617 kmem_free( sc->sc_ctls[i].ranges,
618 sc->sc_ctls[i].nranges * sizeof(struct range));
619 }
620 kmem_free(sc->sc_ctls, sizeof(struct mixerctl) * sc->sc_nctls);
621 }
622
623 if (sc->sc_alts != NULL)
624 kmem_free(sc->sc_alts, sizeof(struct as_info) * sc->sc_nalts);
625
626 mutex_destroy(&sc->sc_lock);
627 mutex_destroy(&sc->sc_intr_lock);
628
629 return 0;
630 }
631
632 Static int
633 uaudio_query_format(void *addr, audio_format_query_t *afp)
634 {
635 struct uaudio_softc *sc;
636
637 sc = addr;
638 return audio_query_format(sc->sc_formats, sc->sc_nformats, afp);
639 }
640
641 Static const usb_interface_descriptor_t *
642 uaudio_find_iface(const char *tbuf, int size, int *offsp, int subtype)
643 {
644 const usb_interface_descriptor_t *d;
645
646 while (*offsp + sizeof(*d) <= size) {
647 d = (const void *)(tbuf + *offsp);
648 DPRINTFN(3, "%d + %d <= %d type %d class %d/%d iface %d\n",
649 *offsp, d->bLength, size,
650 d->bDescriptorType,
651 d->bInterfaceClass,
652 d->bInterfaceSubClass,
653 d->bInterfaceNumber);
654 *offsp += d->bLength;
655 if (d->bDescriptorType == UDESC_INTERFACE &&
656 d->bInterfaceClass == UICLASS_AUDIO &&
657 d->bInterfaceSubClass == subtype)
658 return d;
659 }
660 return NULL;
661 }
662
663 Static void
664 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc)
665 {
666 int res;
667 size_t len, count, msz;
668 struct mixerctl *nmc;
669 struct range *r;
670 uint8_t *buf, *p;
671 int i;
672
673 if (mc->class < UAC_NCLASSES) {
674 DPRINTF("adding %s.%s\n", uac_names[mc->class], mc->ctlname);
675 } else {
676 DPRINTF("adding %s\n", mc->ctlname);
677 }
678 len = sizeof(*mc) * (sc->sc_nctls + 1);
679 nmc = kmem_alloc(len, KM_SLEEP);
680 /* Copy old data, if there was any */
681 if (sc->sc_nctls != 0) {
682 memcpy(nmc, sc->sc_ctls, sizeof(*mc) * sc->sc_nctls);
683 for (i = 0; i<sc->sc_nctls; ++i) {
684 if (sc->sc_ctls[i].ranges == &sc->sc_ctls[i].range0)
685 nmc[i].ranges = &nmc[i].range0;
686 }
687 kmem_free(sc->sc_ctls, sizeof(*mc) * sc->sc_nctls);
688 }
689 sc->sc_ctls = nmc;
690
691 /*
692 * preset
693 * - mc->class
694 * - mc->ctlname
695 * - mc->ctlunit
696 * - mc->wIndex
697 * - mc->wValue[]
698 * - mc->type
699 * - mc->nchan
700 *
701 * - mc->range0, mc->mul for MIX_SELECTOR
702 */
703 sc->sc_ctls[sc->sc_nctls] = *mc;
704 mc = &sc->sc_ctls[sc->sc_nctls++];
705 msz = MIX_SIZE(mc->type);
706
707 mc->delta = 0;
708 mc->nranges = 0;
709 mc->ranges = r = &mc->range0;
710 mc->mul = 0;
711 if (mc->type == MIX_ON_OFF) {
712 r->minval = 0;
713 r->maxval = 1;
714 r->resval = 1;
715 res = r->resval;
716 } else if (mc->type == MIX_SELECTOR) {
717 /* range0 already set by uaudio_add_selector */
718 res = r->resval;
719 } else if (sc->sc_version == UAUDIO_VERSION1) {
720 /* Determine min and max values. */
721 r->minval = uaudio_signext(mc->type,
722 uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE,
723 mc->wValue[0], mc->wIndex, msz));
724 r->maxval = uaudio_signext(mc->type,
725 uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE,
726 mc->wValue[0], mc->wIndex, msz));
727 r->resval = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE,
728 mc->wValue[0], mc->wIndex, msz);
729 mc->mul = r->maxval - r->minval;
730 res = r->resval;
731 } else { /* UAUDIO_VERSION2 */
732 count = (uint16_t)uaudio_get(sc, V2_RANGES,
733 UT_READ_CLASS_INTERFACE,
734 mc->wValue[0], mc->wIndex, 2);
735
736 if (count == 0 || count == (uint16_t)-1) {
737 DPRINTF("invalid range count %zu\n", count);
738 return;
739 }
740
741 if (count > 1) {
742 r = kmem_alloc(sizeof(struct range) * count,
743 KM_SLEEP);
744 mc->ranges = r;
745 mc->nranges = count;
746 }
747
748 mc->ranges[0].minval = 0;
749 mc->ranges[0].maxval = 0;
750 mc->ranges[0].resval = 1;
751
752 /* again with the required buffer size */
753 len = 2 + count * 3 * msz;
754 buf = kmem_alloc(len, KM_SLEEP);
755 uaudio_getbuf(sc, V2_RANGES, UT_READ_CLASS_INTERFACE,
756 mc->wValue[0], mc->wIndex, len, buf);
757 res = 0;
758 p = &buf[2];
759 for (i=0, p=buf+2; i<count; ++i) {
760 uint32_t minval, maxval, resval;
761 switch (msz) {
762 case 1:
763 minval = *p++;
764 maxval = *p++;
765 resval = *p++;
766 break;
767 case 2:
768 minval = p[0] | p[1] << 8;
769 p += 2;
770 maxval = p[0] | p[1] << 8;
771 p += 2;
772 resval = p[0] | p[1] << 8;
773 p += 2;
774 break;
775 case 3:
776 minval = p[0] | p[1] << 8 | p[2] << 16;
777 p += 3;
778 maxval = p[0] | p[1] << 8 | p[2] << 16;
779 p += 3;
780 resval = p[0] | p[1] << 8 | p[2] << 16;
781 p += 3;
782 break;
783 case 4:
784 minval = p[0] | p[1] << 8 \
785 | p[2] << 16 | p[3] << 24;
786 p += 4;
787 maxval = p[0] | p[1] << 8 \
788 | p[2] << 16 | p[3] << 24;
789 p += 4;
790 resval = p[0] | p[1] << 8 \
791 | p[2] << 16 | p[3] << 24;
792 p += 4;
793 break;
794 default: /* not allowed */
795 minval = maxval = 0;
796 resval = 1;
797 break;
798 }
799 mc->ranges[i].minval = uaudio_signext(mc->type, minval);
800 mc->ranges[i].maxval = uaudio_signext(mc->type, maxval);
801 mc->ranges[i].resval = uaudio_signext(mc->type, resval);
802 if (mc->ranges[i].resval > res)
803 res = mc->ranges[i].resval;
804 }
805 kmem_free(buf, len);
806
807 mc->mul = mc->ranges[count - 1].maxval - mc->ranges[0].minval;
808
809 /*
810 * use resolution 1 (ideally the lcd) for
811 * multiple (valid) resolution values.
812 */
813 if (count > 1 && res > 0)
814 res = 1;
815 }
816
817 if (mc->mul == 0)
818 mc->mul = 1;
819
820 mc->delta = (res * 255 + mc->mul - 1) / mc->mul;
821
822 #ifdef UAUDIO_DEBUG
823 if (uaudiodebug > 2) {
824 DPRINTFN_CLEAN(2, "wValue=%04x", mc->wValue[0]);
825 for (i = 1; i < mc->nchan; i++)
826 DPRINTFN_CLEAN(2, ",%04x", mc->wValue[i]);
827 DPRINTFN_CLEAN(2, "\n");
828 count = mc->nranges > 0 ? mc->nranges : 1;
829 for (i = 0; i < count; i++)
830 DPRINTFN_CLEAN(2, "%d: wIndex=%04x type=%d name='%s' "
831 "unit='%s' min=%d max=%d res=%d\n",
832 i, mc->wIndex, mc->type, mc->ctlname, mc->ctlunit,
833 mc->ranges[i].minval,
834 mc->ranges[i].maxval,
835 mc->ranges[i].resval);
836 }
837 #endif
838 }
839
840 Static char *
841 uaudio_id_name(struct uaudio_softc *sc,
842 const struct io_terminal *iot, uint8_t id)
843 {
844 static char tbuf[32];
845
846 snprintf(tbuf, sizeof(tbuf), "i%u", id);
847
848 return tbuf;
849 }
850
851 #ifdef UAUDIO_DEBUG
852 Static void
853 uaudio_dump_cluster(struct uaudio_softc *sc, const union usb_audio_cluster *cl)
854 {
855 static const char *channel_v1_names[16] = {
856 "LEFT", "RIGHT", "CENTER", "LFE",
857 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
858 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
859 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
860 };
861 static const char *channel_v2_names[32] = {
862 "LEFT", "RIGHT", "CENTER", "LFE",
863 "BACK_LEFT", "BACK_RIGHT", "FLC", "FRC",
864 "BACK_CENTER", "SIDE_LEFT", "SIDE_RIGHT", "TOP CENTER",
865 "TFL", "TFC", "TFR", "TBL", "TBC", "TBR",
866 "TFLC", "TFRC", "LLFE", "RLFE", "TSL", "TSR",
867 "BC", "BLC", "BRC",
868 "RESERVED27", "RESERVED28", "RESERVED29", "RESERVED30",
869 "RAW_DATA"
870 };
871 const char **channel_names;
872 uint32_t cc;
873 int i, first, icn;
874
875 switch (sc->sc_version) {
876 case UAUDIO_VERSION1:
877 channel_names = channel_v1_names;
878 cc = UGETW(cl->v1.wChannelConfig);
879 icn = cl->v1.iChannelNames;
880 printf("cluster: bNrChannels=%u wChannelConfig=%#.4x",
881 cl->v1.bNrChannels, cc);
882 break;
883 case UAUDIO_VERSION2:
884 channel_names = channel_v2_names;
885 cc = UGETDW(cl->v2.bmChannelConfig);
886 icn = cl->v2.iChannelNames;
887 printf("cluster: bNrChannels=%u bmChannelConfig=%#.8x",
888 cl->v2.bNrChannels, cc);
889 break;
890 default:
891 return;
892 }
893
894 first = TRUE;
895 for (i = 0; cc != 0; i++) {
896 if (cc & 1) {
897 printf("%c%s", first ? '<' : ',', channel_names[i]);
898 first = FALSE;
899 }
900 cc = cc >> 1;
901 }
902 printf("> iChannelNames=%u", icn);
903 }
904 #endif
905
906 Static union usb_audio_cluster
907 uaudio_get_cluster(struct uaudio_softc *sc, int id, const struct io_terminal *iot)
908 {
909 union usb_audio_cluster r;
910 const uaudio_cs_descriptor_t *dp;
911 u_int pins;
912 int i;
913
914 for (i = 0; i < 25; i++) { /* avoid infinite loops */
915 dp = iot[id].d.desc;
916 if (dp == 0)
917 goto bad;
918
919 switch (dp->bDescriptorSubtype) {
920 case UDESCSUB_AC_INPUT:
921 switch (sc->sc_version) {
922 case UAUDIO_VERSION1:
923 r.v1.bNrChannels = iot[id].d.it->v1.bNrChannels;
924 USETW(r.v1.wChannelConfig,
925 UGETW(iot[id].d.it->v1.wChannelConfig));
926 r.v1.iChannelNames = iot[id].d.it->v1.iChannelNames;
927 break;
928 case UAUDIO_VERSION2:
929 r.v2.bNrChannels = iot[id].d.it->v2.bNrChannels;
930 USETDW(r.v2.bmChannelConfig,
931 UGETW(iot[id].d.it->v2.bmChannelConfig));
932 r.v2.iChannelNames = iot[id].d.it->v2.iChannelNames;
933 break;
934 }
935 return r;
936 case UDESCSUB_AC_OUTPUT:
937 /* XXX This is not really right */
938 id = iot[id].d.ot->v1.bSourceId;
939 break;
940 case UDESCSUB_AC_MIXER:
941 switch (sc->sc_version) {
942 case UAUDIO_VERSION1:
943 pins = iot[id].d.mu->bNrInPins;
944 r.v1 = *(const struct usb_audio_v1_cluster *)
945 &iot[id].d.mu->baSourceId[pins];
946 break;
947 case UAUDIO_VERSION2:
948 pins = iot[id].d.mu->bNrInPins;
949 r.v2 = *(const struct usb_audio_v2_cluster *)
950 &iot[id].d.mu->baSourceId[pins];
951 break;
952 }
953 return r;
954 case UDESCSUB_AC_SELECTOR:
955 /* XXX This is not really right */
956 id = iot[id].d.su->baSourceId[0];
957 break;
958 case UDESCSUB_AC_FEATURE:
959 /* XXX This is not really right */
960 switch (sc->sc_version) {
961 case UAUDIO_VERSION1:
962 id = iot[id].d.fu->v1.bSourceId;
963 break;
964 case UAUDIO_VERSION2:
965 id = iot[id].d.fu->v2.bSourceId;
966 break;
967 }
968 break;
969 case UDESCSUB_AC_PROCESSING:
970 switch (sc->sc_version) {
971 case UAUDIO_VERSION1:
972 pins = iot[id].d.pu->bNrInPins;
973 r.v1 = *(const struct usb_audio_v1_cluster *)
974 &iot[id].d.pu->baSourceId[pins];
975 break;
976 case UAUDIO_VERSION2:
977 pins = iot[id].d.pu->bNrInPins;
978 r.v2 = *(const struct usb_audio_v2_cluster *)
979 &iot[id].d.pu->baSourceId[pins];
980 break;
981 }
982 return r;
983 case UDESCSUB_AC_EXTENSION:
984 switch (sc->sc_version) {
985 case UAUDIO_VERSION1:
986 pins = iot[id].d.eu->bNrInPins;
987 r.v1 = *(const struct usb_audio_v1_cluster *)
988 &iot[id].d.eu->baSourceId[pins];
989 break;
990 case UAUDIO_VERSION2:
991 pins = iot[id].d.eu->bNrInPins;
992 r.v2 = *(const struct usb_audio_v2_cluster *)
993 &iot[id].d.eu->baSourceId[pins];
994 break;
995 }
996 return r;
997 default:
998 goto bad;
999 }
1000 }
1001 bad:
1002 aprint_error("uaudio_get_cluster: bad data\n");
1003 memset(&r, 0, sizeof(r));
1004 return r;
1005
1006 }
1007
1008 Static void
1009 uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1010 {
1011 const union usb_audio_input_terminal *d;
1012
1013 d = iot[id].d.it;
1014 switch (sc->sc_version) {
1015 case UAUDIO_VERSION1:
1016 #ifdef UAUDIO_DEBUG
1017 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
1018 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
1019 "iChannelNames=%d iTerminal=%d\n",
1020 d->v1.bTerminalId, UGETW(d->v1.wTerminalType), d->v1.bAssocTerminal,
1021 d->v1.bNrChannels, UGETW(d->v1.wChannelConfig),
1022 d->v1.iChannelNames, d->v1.iTerminal);
1023 #endif
1024 /* If USB input terminal, record wChannelConfig */
1025 if ((UGETW(d->v1.wTerminalType) & 0xff00) != UAT_UNDEFINED)
1026 return;
1027 sc->sc_channel_config = UGETW(d->v1.wChannelConfig);
1028 sc->sc_clock[id] = 0;
1029 break;
1030 case UAUDIO_VERSION2:
1031 #ifdef UAUDIO_DEBUG
1032 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
1033 "bAssocTerminal=%d bNrChannels=%d bmChannelConfig=%x "
1034 "iChannelNames=%d bCSourceId=%d iTerminal=%d\n",
1035 d->v2.bTerminalId, UGETW(d->v2.wTerminalType), d->v2.bAssocTerminal,
1036 d->v2.bNrChannels, UGETDW(d->v2.bmChannelConfig),
1037 d->v2.iChannelNames, d->v2.bCSourceId, d->v2.iTerminal);
1038 #endif
1039 /* If USB input terminal, record wChannelConfig */
1040 if ((UGETW(d->v2.wTerminalType) & 0xff00) != UAT_UNDEFINED)
1041 return;
1042 sc->sc_channel_config = UGETDW(d->v2.bmChannelConfig);
1043 sc->sc_clock[id] = d->v2.bCSourceId;
1044 break;
1045 }
1046 }
1047
1048 Static void
1049 uaudio_add_output(struct uaudio_softc *sc,
1050 const struct io_terminal *iot, int id)
1051 {
1052 #ifdef UAUDIO_DEBUG
1053 const union usb_audio_output_terminal *d;
1054
1055 d = iot[id].d.ot;
1056 switch (sc->sc_version) {
1057 case UAUDIO_VERSION1:
1058 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
1059 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
1060 d->v1.bTerminalId, UGETW(d->v1.wTerminalType), d->v1.bAssocTerminal,
1061 d->v1.bSourceId, d->v1.iTerminal);
1062 sc->sc_clock[id] = 0;
1063 break;
1064 case UAUDIO_VERSION2:
1065 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
1066 "bAssocTerminal=%d bSourceId=%d bCSourceId=%d, iTerminal=%d\n",
1067 d->v2.bTerminalId, UGETW(d->v2.wTerminalType), d->v2.bAssocTerminal,
1068 d->v2.bSourceId, d->v2.bCSourceId, d->v2.iTerminal);
1069 sc->sc_clock[id] = d->v2.bCSourceId;
1070 break;
1071 }
1072 #endif
1073 }
1074
1075 Static void
1076 uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1077 {
1078 const struct usb_audio_mixer_unit *d;
1079 const union usb_audio_mixer_unit_1 *d1;
1080 int c, chs, ichs, ochs, nchs, i, o, bno, p, k;
1081 size_t bm_size;
1082 const uByte *bm;
1083 struct mixerctl mix;
1084
1085 d = iot[id].d.mu;
1086 d1 = (const union usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins];
1087 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
1088 d->bUnitId, d->bNrInPins);
1089
1090 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1091 uaudio_determine_class(&iot[id], &mix);
1092 mix.type = MIX_SIGNED_16;
1093 mix.ctlunit = AudioNvolume;
1094
1095 /* Compute the number of input channels */
1096 /* and the number of output channels */
1097 ichs = 0;
1098 switch (sc->sc_version) {
1099 case UAUDIO_VERSION1:
1100 for (i = 0; i < d->bNrInPins; i++)
1101 ichs += uaudio_get_cluster(sc, d->baSourceId[i], iot).v1.bNrChannels;
1102 ochs = d1->v1.bNrChannels;
1103 DPRINTFN(2,"ichs=%d ochs=%d\n", ichs, ochs);
1104 bm = d1->v1.bmControls;
1105 break;
1106 case UAUDIO_VERSION2:
1107 for (i = 0; i < d->bNrInPins; i++)
1108 ichs += uaudio_get_cluster(sc, d->baSourceId[i], iot).v2.bNrChannels;
1109 ochs = d1->v2.bNrChannels;
1110 DPRINTFN(2,"ichs=%d ochs=%d\n", ichs, ochs);
1111 bm = d1->v2.bmMixerControls;
1112 bm_size = ichs * ochs / 8 + ((ichs * ochs % 8) ? 1 : 0);
1113 /* bmControls */
1114 if ((bm[bm_size] & UA_MIX_CLUSTER_MASK) != UA_MIX_CLUSTER_RW)
1115 return;
1116 break;
1117 default:
1118 return;
1119 }
1120
1121 for (p = i = 0; i < d->bNrInPins; i++) {
1122 switch (sc->sc_version) {
1123 case UAUDIO_VERSION1:
1124 chs = uaudio_get_cluster(sc, d->baSourceId[i], iot)
1125 .v1.bNrChannels;
1126 break;
1127 case UAUDIO_VERSION2:
1128 chs = uaudio_get_cluster(sc, d->baSourceId[i], iot)
1129 .v2.bNrChannels;
1130 break;
1131 default:
1132 continue;
1133 }
1134
1135 #define _BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1)
1136
1137 nchs = chs < MIX_MAX_CHAN ? chs : MIX_MAX_CHAN;
1138
1139 k = 0;
1140 for (c = 0; c < nchs; c++) {
1141 for (o = 0; o < ochs; o++) {
1142 bno = (p + c) * ochs + o;
1143 if (_BIT(bno))
1144 mix.wValue[k++] =
1145 MAKE(p+c+1, o+1);
1146 }
1147 }
1148 mix.nchan = nchs;
1149
1150 snprintf(mix.ctlname, sizeof(mix.ctlname),
1151 "mix%d-%s", d->bUnitId,
1152 uaudio_id_name(sc, iot, d->baSourceId[i])
1153 );
1154 uaudio_mixer_add_ctl(sc, &mix);
1155
1156 #undef _BIT
1157
1158 p += chs;
1159 }
1160 }
1161
1162 Static void
1163 uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1164 {
1165 const struct usb_audio_selector_unit *d;
1166 struct mixerctl mix;
1167 int i, wp;
1168
1169 d = iot[id].d.su;
1170 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
1171 d->bUnitId, d->bNrInPins);
1172 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1173 if (sc->sc_version == UAUDIO_VERSION2)
1174 mix.wValue[0] = MAKE(V2_CUR_SELECTOR, 0);
1175 else
1176 mix.wValue[0] = MAKE(0, 0);
1177 uaudio_determine_class(&iot[id], &mix);
1178 mix.nchan = 1;
1179 mix.type = MIX_SELECTOR;
1180 mix.ctlunit = "";
1181 mix.range0.minval = 1;
1182 mix.range0.maxval = d->bNrInPins;
1183 mix.range0.resval = 1;
1184 mix.mul = mix.range0.maxval - mix.range0.minval;
1185 wp = snprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId);
1186 for (i = 1; i <= d->bNrInPins; i++) {
1187 wp += strlcpy(mix.ctlname + wp,
1188 uaudio_id_name(sc, iot, d->baSourceId[i-1]),
1189 MAX_AUDIO_DEV_LEN - wp);
1190 if (wp > MAX_AUDIO_DEV_LEN - 1)
1191 break;
1192 }
1193 uaudio_mixer_add_ctl(sc, &mix);
1194 }
1195
1196 #ifdef UAUDIO_DEBUG
1197 Static const char *
1198 uaudio_get_terminal_name(int terminal_type)
1199 {
1200 static char tbuf[100];
1201
1202 switch (terminal_type) {
1203 /* USB terminal types */
1204 case UAT_UNDEFINED: return "UAT_UNDEFINED";
1205 case UAT_STREAM: return "UAT_STREAM";
1206 case UAT_VENDOR: return "UAT_VENDOR";
1207 /* input terminal types */
1208 case UATI_UNDEFINED: return "UATI_UNDEFINED";
1209 case UATI_MICROPHONE: return "UATI_MICROPHONE";
1210 case UATI_DESKMICROPHONE: return "UATI_DESKMICROPHONE";
1211 case UATI_PERSONALMICROPHONE: return "UATI_PERSONALMICROPHONE";
1212 case UATI_OMNIMICROPHONE: return "UATI_OMNIMICROPHONE";
1213 case UATI_MICROPHONEARRAY: return "UATI_MICROPHONEARRAY";
1214 case UATI_PROCMICROPHONEARR: return "UATI_PROCMICROPHONEARR";
1215 /* output terminal types */
1216 case UATO_UNDEFINED: return "UATO_UNDEFINED";
1217 case UATO_SPEAKER: return "UATO_SPEAKER";
1218 case UATO_HEADPHONES: return "UATO_HEADPHONES";
1219 case UATO_DISPLAYAUDIO: return "UATO_DISPLAYAUDIO";
1220 case UATO_DESKTOPSPEAKER: return "UATO_DESKTOPSPEAKER";
1221 case UATO_ROOMSPEAKER: return "UATO_ROOMSPEAKER";
1222 case UATO_COMMSPEAKER: return "UATO_COMMSPEAKER";
1223 case UATO_SUBWOOFER: return "UATO_SUBWOOFER";
1224 /* bidir terminal types */
1225 case UATB_UNDEFINED: return "UATB_UNDEFINED";
1226 case UATB_HANDSET: return "UATB_HANDSET";
1227 case UATB_HEADSET: return "UATB_HEADSET";
1228 case UATB_SPEAKERPHONE: return "UATB_SPEAKERPHONE";
1229 case UATB_SPEAKERPHONEESUP: return "UATB_SPEAKERPHONEESUP";
1230 case UATB_SPEAKERPHONEECANC: return "UATB_SPEAKERPHONEECANC";
1231 /* telephony terminal types */
1232 case UATT_UNDEFINED: return "UATT_UNDEFINED";
1233 case UATT_PHONELINE: return "UATT_PHONELINE";
1234 case UATT_TELEPHONE: return "UATT_TELEPHONE";
1235 case UATT_DOWNLINEPHONE: return "UATT_DOWNLINEPHONE";
1236 /* external terminal types */
1237 case UATE_UNDEFINED: return "UATE_UNDEFINED";
1238 case UATE_ANALOGCONN: return "UATE_ANALOGCONN";
1239 case UATE_LINECONN: return "UATE_LINECONN";
1240 case UATE_LEGACYCONN: return "UATE_LEGACYCONN";
1241 case UATE_DIGITALAUIFC: return "UATE_DIGITALAUIFC";
1242 case UATE_SPDIF: return "UATE_SPDIF";
1243 case UATE_1394DA: return "UATE_1394DA";
1244 case UATE_1394DV: return "UATE_1394DV";
1245 /* embedded function terminal types */
1246 case UATF_UNDEFINED: return "UATF_UNDEFINED";
1247 case UATF_CALIBNOISE: return "UATF_CALIBNOISE";
1248 case UATF_EQUNOISE: return "UATF_EQUNOISE";
1249 case UATF_CDPLAYER: return "UATF_CDPLAYER";
1250 case UATF_DAT: return "UATF_DAT";
1251 case UATF_DCC: return "UATF_DCC";
1252 case UATF_MINIDISK: return "UATF_MINIDISK";
1253 case UATF_ANALOGTAPE: return "UATF_ANALOGTAPE";
1254 case UATF_PHONOGRAPH: return "UATF_PHONOGRAPH";
1255 case UATF_VCRAUDIO: return "UATF_VCRAUDIO";
1256 case UATF_VIDEODISCAUDIO: return "UATF_VIDEODISCAUDIO";
1257 case UATF_DVDAUDIO: return "UATF_DVDAUDIO";
1258 case UATF_TVTUNERAUDIO: return "UATF_TVTUNERAUDIO";
1259 case UATF_SATELLITE: return "UATF_SATELLITE";
1260 case UATF_CABLETUNER: return "UATF_CABLETUNER";
1261 case UATF_DSS: return "UATF_DSS";
1262 case UATF_RADIORECV: return "UATF_RADIORECV";
1263 case UATF_RADIOXMIT: return "UATF_RADIOXMIT";
1264 case UATF_MULTITRACK: return "UATF_MULTITRACK";
1265 case UATF_SYNTHESIZER: return "UATF_SYNTHESIZER";
1266 default:
1267 snprintf(tbuf, sizeof(tbuf), "unknown type (%#.4x)", terminal_type);
1268 return tbuf;
1269 }
1270 }
1271 #endif
1272
1273 Static int
1274 uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix)
1275 {
1276 int terminal_type;
1277
1278 if (iot == NULL || iot->output == NULL) {
1279 mix->class = UAC_OUTPUT;
1280 return 0;
1281 }
1282 terminal_type = 0;
1283 if (iot->output->size == 1)
1284 terminal_type = iot->output->terminals[0];
1285 /*
1286 * If the only output terminal is USB,
1287 * the class is UAC_RECORD.
1288 */
1289 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
1290 mix->class = UAC_RECORD;
1291 if (iot->inputs_size == 1
1292 && iot->inputs[0] != NULL
1293 && iot->inputs[0]->size == 1)
1294 return iot->inputs[0]->terminals[0];
1295 else
1296 return 0;
1297 }
1298 /*
1299 * If the ultimate destination of the unit is just one output
1300 * terminal and the unit is connected to the output terminal
1301 * directly, the class is UAC_OUTPUT.
1302 */
1303 if (terminal_type != 0 && iot->direct) {
1304 mix->class = UAC_OUTPUT;
1305 return terminal_type;
1306 }
1307 /*
1308 * If the unit is connected to just one input terminal,
1309 * the class is UAC_INPUT.
1310 */
1311 if (iot->inputs_size == 1 && iot->inputs[0] != NULL
1312 && iot->inputs[0]->size == 1) {
1313 mix->class = UAC_INPUT;
1314 return iot->inputs[0]->terminals[0];
1315 }
1316 /*
1317 * Otherwise, the class is UAC_OUTPUT.
1318 */
1319 mix->class = UAC_OUTPUT;
1320 return terminal_type;
1321 }
1322
1323 Static const char *
1324 uaudio_feature_name(const struct io_terminal *iot,
1325 uint8_t class, int terminal_type)
1326 {
1327
1328 if (class == UAC_RECORD && terminal_type == 0)
1329 return AudioNmixerout;
1330
1331 DPRINTF("terminal_type=%s\n", uaudio_get_terminal_name(terminal_type));
1332 switch (terminal_type) {
1333 case UAT_STREAM:
1334 return AudioNdac;
1335
1336 case UATI_MICROPHONE:
1337 case UATI_DESKMICROPHONE:
1338 case UATI_PERSONALMICROPHONE:
1339 case UATI_OMNIMICROPHONE:
1340 case UATI_MICROPHONEARRAY:
1341 case UATI_PROCMICROPHONEARR:
1342 return AudioNmicrophone;
1343
1344 case UATO_SPEAKER:
1345 case UATO_DESKTOPSPEAKER:
1346 case UATO_ROOMSPEAKER:
1347 case UATO_COMMSPEAKER:
1348 return AudioNspeaker;
1349
1350 case UATO_HEADPHONES:
1351 return AudioNheadphone;
1352
1353 case UATO_SUBWOOFER:
1354 return AudioNlfe;
1355
1356 /* telephony terminal types */
1357 case UATT_UNDEFINED:
1358 case UATT_PHONELINE:
1359 case UATT_TELEPHONE:
1360 case UATT_DOWNLINEPHONE:
1361 return "phone";
1362
1363 case UATE_ANALOGCONN:
1364 case UATE_LINECONN:
1365 case UATE_LEGACYCONN:
1366 return AudioNline;
1367
1368 case UATE_DIGITALAUIFC:
1369 case UATE_SPDIF:
1370 case UATE_1394DA:
1371 case UATE_1394DV:
1372 return AudioNaux;
1373
1374 case UATF_CDPLAYER:
1375 return AudioNcd;
1376
1377 case UATF_SYNTHESIZER:
1378 return AudioNfmsynth;
1379
1380 case UATF_VIDEODISCAUDIO:
1381 case UATF_DVDAUDIO:
1382 case UATF_TVTUNERAUDIO:
1383 return AudioNvideo;
1384
1385 case UAT_UNDEFINED:
1386 case UAT_VENDOR:
1387 case UATI_UNDEFINED:
1388 /* output terminal types */
1389 case UATO_UNDEFINED:
1390 case UATO_DISPLAYAUDIO:
1391 /* bidir terminal types */
1392 case UATB_UNDEFINED:
1393 case UATB_HANDSET:
1394 case UATB_HEADSET:
1395 case UATB_SPEAKERPHONE:
1396 case UATB_SPEAKERPHONEESUP:
1397 case UATB_SPEAKERPHONEECANC:
1398 /* external terminal types */
1399 case UATE_UNDEFINED:
1400 /* embedded function terminal types */
1401 case UATF_UNDEFINED:
1402 case UATF_CALIBNOISE:
1403 case UATF_EQUNOISE:
1404 case UATF_DAT:
1405 case UATF_DCC:
1406 case UATF_MINIDISK:
1407 case UATF_ANALOGTAPE:
1408 case UATF_PHONOGRAPH:
1409 case UATF_VCRAUDIO:
1410 case UATF_SATELLITE:
1411 case UATF_CABLETUNER:
1412 case UATF_DSS:
1413 case UATF_RADIORECV:
1414 case UATF_RADIOXMIT:
1415 case UATF_MULTITRACK:
1416 case 0xffff:
1417 default:
1418 DPRINTF("'master' for %#.4x\n", terminal_type);
1419 return AudioNmaster;
1420 }
1421 return AudioNmaster;
1422 }
1423
1424 static void
1425 uaudio_add_feature_mixer(struct uaudio_softc *sc, const struct io_terminal *iot,
1426 int unit, int ctl, struct mixerctl *mc)
1427 {
1428 const char *mixername, *attr = NULL;
1429 int terminal_type;
1430
1431 mc->wIndex = MAKE(unit, sc->sc_ac_iface);
1432 terminal_type = uaudio_determine_class(iot, mc);
1433 mixername = uaudio_feature_name(iot, mc->class, terminal_type);
1434 switch (ctl) {
1435 case MUTE_CONTROL:
1436 mc->type = MIX_ON_OFF;
1437 mc->ctlunit = "";
1438 attr = AudioNmute;
1439 break;
1440 case VOLUME_CONTROL:
1441 mc->type = MIX_SIGNED_16;
1442 mc->ctlunit = AudioNvolume;
1443 attr = NULL;
1444 break;
1445 case BASS_CONTROL:
1446 mc->type = MIX_SIGNED_8;
1447 mc->ctlunit = AudioNbass;
1448 attr = AudioNbass;
1449 break;
1450 case MID_CONTROL:
1451 mc->type = MIX_SIGNED_8;
1452 mc->ctlunit = AudioNmid;
1453 attr = AudioNmid;
1454 break;
1455 case TREBLE_CONTROL:
1456 mc->type = MIX_SIGNED_8;
1457 mc->ctlunit = AudioNtreble;
1458 attr = AudioNtreble;
1459 break;
1460 case GRAPHIC_EQUALIZER_CONTROL:
1461 return; /* XXX don't add anything */
1462 break;
1463 case AGC_CONTROL:
1464 mc->type = MIX_ON_OFF;
1465 mc->ctlunit = "";
1466 attr = AudioNagc;
1467 break;
1468 case DELAY_CONTROL:
1469 mc->type = MIX_UNSIGNED_16;
1470 mc->ctlunit = "4 ms";
1471 attr = AudioNdelay;
1472 break;
1473 case BASS_BOOST_CONTROL:
1474 mc->type = MIX_ON_OFF;
1475 mc->ctlunit = "";
1476 attr = AudioNbassboost;
1477 break;
1478 case LOUDNESS_CONTROL:
1479 mc->type = MIX_ON_OFF;
1480 mc->ctlunit = "";
1481 attr = AudioNloudness;
1482 break;
1483 case GAIN_CONTROL:
1484 mc->type = MIX_SIGNED_16;
1485 mc->ctlunit = "gain";
1486 attr = "gain";;
1487 break;
1488 case GAINPAD_CONTROL:
1489 mc->type = MIX_SIGNED_16;
1490 mc->ctlunit = "gainpad";
1491 attr = "gainpad";;
1492 break;
1493 case PHASEINV_CONTROL:
1494 mc->type = MIX_ON_OFF;
1495 mc->ctlunit = "";
1496 attr = "phaseinv";;
1497 break;
1498 case UNDERFLOW_CONTROL:
1499 mc->type = MIX_ON_OFF;
1500 mc->ctlunit = "";
1501 attr = "underflow";;
1502 break;
1503 case OVERFLOW_CONTROL:
1504 mc->type = MIX_ON_OFF;
1505 mc->ctlunit = "";
1506 attr = "overflow";;
1507 break;
1508 default:
1509 return; /* XXX don't add anything */
1510 break;
1511 }
1512
1513 if (attr != NULL) {
1514 snprintf(mc->ctlname, sizeof(mc->ctlname),
1515 "%s.%s", mixername, attr);
1516 } else {
1517 snprintf(mc->ctlname, sizeof(mc->ctlname),
1518 "%s", mixername);
1519 }
1520
1521 uaudio_mixer_add_ctl(sc, mc);
1522 }
1523
1524 Static void
1525 uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1526 {
1527 const union usb_audio_feature_unit *d;
1528 const uByte *ctls;
1529 const uDWord *ctls2;
1530 int ctlsize;
1531 int nchan;
1532 u_int fumask, mmask, cmask;
1533 struct mixerctl mix;
1534 int chan, ctl, i, unit;
1535
1536 d = iot[id].d.fu;
1537
1538 switch (sc->sc_version) {
1539 case UAUDIO_VERSION1:
1540
1541 #define GETV1(i) (ctls[(i)*ctlsize] | \
1542 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0))
1543
1544 ctls = d->v1.bmaControls;
1545 ctlsize = d->v1.bControlSize;
1546 if (ctlsize == 0) {
1547 DPRINTF("ignoring feature %d with controlSize of zero\n", id);
1548 return;
1549 }
1550
1551 /* offsetof bmaControls + sizeof iFeature == 7 */
1552 nchan = (d->v1.bLength - 7) / ctlsize;
1553 mmask = GETV1(0);
1554 /* Figure out what we can control */
1555 for (cmask = 0, chan = 1; chan < nchan; chan++) {
1556 DPRINTFN(9,"chan=%d mask=%x\n",
1557 chan, GETV1(chan));
1558 cmask |= GETV1(chan);
1559 }
1560
1561 DPRINTFN(1,"bUnitId=%d, "
1562 "%d channels, mmask=0x%04x, cmask=0x%04x\n",
1563 d->v1.bUnitId, nchan, mmask, cmask);
1564
1565 if (nchan > MIX_MAX_CHAN)
1566 nchan = MIX_MAX_CHAN;
1567 unit = d->v1.bUnitId;
1568
1569 for (ctl = MUTE_CONTROL; ctl <= LOUDNESS_CONTROL; ctl++) {
1570 fumask = FU_MASK(ctl);
1571 DPRINTFN(4,"ctl=%d fumask=0x%04x\n",
1572 ctl, fumask);
1573 if (mmask & fumask) {
1574 mix.nchan = 1;
1575 mix.wValue[0] = MAKE(ctl, 0);
1576 } else if (cmask & fumask) {
1577 mix.nchan = nchan - 1;
1578 for (i = 1; i < nchan; i++) {
1579 if (GETV1(i) & fumask)
1580 mix.wValue[i-1] = MAKE(ctl, i);
1581 else
1582 mix.wValue[i-1] = -1;
1583 }
1584 } else {
1585 continue;
1586 }
1587
1588 uaudio_add_feature_mixer(sc, &iot[id], unit, ctl, &mix);
1589 }
1590 #undef GETV1
1591 break;
1592
1593 case UAUDIO_VERSION2:
1594
1595 #define GETV2(i) UGETDW(ctls2[(i)])
1596
1597 ctls2 = d->v2.bmaControls;
1598
1599 /* offsetof bmaControls + sizeof iFeature == 6 */
1600 nchan = (d->v2.bLength - 6) / 4;
1601 if (nchan <= 0) {
1602 DPRINTF("ignoring feature %d with no controls\n", id);
1603 return;
1604 }
1605
1606 mmask = GETV2(0);
1607 /* Figure out what we can control */
1608 for (cmask = 0, chan = 1; chan < nchan; chan++) {
1609 DPRINTFN(9,"chan=%d mask=%x\n",
1610 chan, GETV2(chan));
1611 cmask |= GETV2(chan);
1612 }
1613
1614 DPRINTFN(1,"bUnitId=%d, "
1615 "%d channels, mmask=0x%04x, cmask=0x%04x\n",
1616 d->v2.bUnitId, nchan, mmask, cmask);
1617
1618 if (nchan > MIX_MAX_CHAN)
1619 nchan = MIX_MAX_CHAN;
1620 unit = d->v2.bUnitId;
1621
1622 for (ctl = MUTE_CONTROL; ctl <= OVERFLOW_CONTROL; ctl++) {
1623 fumask = V2_FU_MASK(ctl);
1624 DPRINTFN(4,"ctl=%d fumask=0x%08x\n",
1625 ctl, fumask);
1626
1627 if (mmask & fumask) {
1628 mix.nchan = 1;
1629 mix.wValue[0] = MAKE(ctl, 0);
1630 } else if (cmask & fumask) {
1631 mix.nchan = nchan-1;
1632 for (i = 1; i < nchan; ++i) {
1633 if (GETV2(i) & fumask)
1634 mix.wValue[i-1] = MAKE(ctl, i);
1635 else
1636 mix.wValue[i-1] = -1;
1637 }
1638 } else {
1639 continue;
1640 }
1641
1642 uaudio_add_feature_mixer(sc, &iot[id], unit, ctl, &mix);
1643 }
1644
1645 #undef GETV2
1646 break;
1647 }
1648 }
1649
1650 Static void
1651 uaudio_add_processing_updown(struct uaudio_softc *sc,
1652 const struct io_terminal *iot, int id)
1653 {
1654 const struct usb_audio_processing_unit *d;
1655 const struct usb_audio_processing_unit_1 *d1;
1656 const struct usb_audio_processing_unit_updown *ud;
1657 struct mixerctl mix;
1658 int i;
1659
1660 d = iot[id].d.pu;
1661 d1 = (const struct usb_audio_processing_unit_1 *)
1662 &d->baSourceId[d->bNrInPins];
1663 ud = (const struct usb_audio_processing_unit_updown *)
1664 &d1->bmControls[d1->bControlSize];
1665 DPRINTFN(2,"bUnitId=%d bNrModes=%d\n",
1666 d->bUnitId, ud->bNrModes);
1667
1668 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
1669 DPRINTF("%s", "no mode select\n");
1670 return;
1671 }
1672
1673 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1674 mix.nchan = 1;
1675 mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0);
1676 uaudio_determine_class(&iot[id], &mix);
1677 mix.type = MIX_ON_OFF; /* XXX */
1678 mix.ctlunit = "";
1679 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId);
1680
1681 for (i = 0; i < ud->bNrModes; i++) {
1682 DPRINTFN(2,"i=%d bm=%#x\n",
1683 i, UGETW(ud->waModes[i]));
1684 /* XXX */
1685 }
1686 uaudio_mixer_add_ctl(sc, &mix);
1687 }
1688
1689 Static void
1690 uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1691 {
1692 const struct usb_audio_processing_unit *d;
1693 const struct usb_audio_processing_unit_1 *d1;
1694 int ptype;
1695 struct mixerctl mix;
1696
1697 d = iot[id].d.pu;
1698 d1 = (const struct usb_audio_processing_unit_1 *)
1699 &d->baSourceId[d->bNrInPins];
1700 ptype = UGETW(d->wProcessType);
1701 DPRINTFN(2,"wProcessType=%d bUnitId=%d "
1702 "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins);
1703
1704 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
1705 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1706 mix.nchan = 1;
1707 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0);
1708 uaudio_determine_class(&iot[id], &mix);
1709 mix.type = MIX_ON_OFF;
1710 mix.ctlunit = "";
1711 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable",
1712 d->bUnitId, ptype);
1713 uaudio_mixer_add_ctl(sc, &mix);
1714 }
1715
1716 switch(ptype) {
1717 case UPDOWNMIX_PROCESS:
1718 uaudio_add_processing_updown(sc, iot, id);
1719 break;
1720 case DOLBY_PROLOGIC_PROCESS:
1721 case P3D_STEREO_EXTENDER_PROCESS:
1722 case REVERBATION_PROCESS:
1723 case CHORUS_PROCESS:
1724 case DYN_RANGE_COMP_PROCESS:
1725 default:
1726 #ifdef UAUDIO_DEBUG
1727 aprint_debug(
1728 "uaudio_add_processing: unit %d, type=%d not impl.\n",
1729 d->bUnitId, ptype);
1730 #endif
1731 break;
1732 }
1733 }
1734
1735 Static void
1736 uaudio_add_effect(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1737 {
1738
1739 #ifdef UAUDIO_DEBUG
1740 aprint_debug("uaudio_add_effect: not impl.\n");
1741 #endif
1742 }
1743
1744 Static void
1745 uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1746 {
1747 const struct usb_audio_extension_unit *d;
1748 const struct usb_audio_extension_unit_1 *d1;
1749 struct mixerctl mix;
1750
1751 d = iot[id].d.eu;
1752 d1 = (const struct usb_audio_extension_unit_1 *)
1753 &d->baSourceId[d->bNrInPins];
1754 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
1755 d->bUnitId, d->bNrInPins);
1756
1757 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU)
1758 return;
1759
1760 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
1761 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1762 mix.nchan = 1;
1763 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0);
1764 uaudio_determine_class(&iot[id], &mix);
1765 mix.type = MIX_ON_OFF;
1766 mix.ctlunit = "";
1767 snprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable",
1768 d->bUnitId);
1769 uaudio_mixer_add_ctl(sc, &mix);
1770 }
1771 }
1772
1773 Static void
1774 uaudio_add_clksrc(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1775 {
1776 const struct usb_audio_clksrc_unit *d;
1777 struct mixerctl mix;
1778
1779 d = iot[id].d.cu;
1780 DPRINTFN(2,"bClockId=%d bmAttributes=%d bmControls=%d bAssocTerminal=%d iClockSource=%d\n",
1781 d->bClockId, d->bmAttributes, d->bmControls, d->bAssocTerminal, d->iClockSource);
1782 mix.wIndex = MAKE(d->bClockId, sc->sc_ac_iface);
1783 uaudio_determine_class(&iot[id], &mix);
1784 mix.nchan = 1;
1785 mix.wValue[0] = MAKE(V2_CUR_CLKFREQ, 0);
1786 mix.type = MIX_UNSIGNED_32;
1787 mix.ctlunit = "";
1788
1789 uaudio_makename(sc, d->iClockSource, uaudio_clockname(d->bmAttributes),
1790 d->bClockId, mix.ctlname, sizeof(mix.ctlname));
1791 uaudio_mixer_add_ctl(sc, &mix);
1792 }
1793
1794 Static void
1795 uaudio_add_clksel(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1796 {
1797 const struct usb_audio_clksel_unit *d;
1798 struct mixerctl mix;
1799 int i, wp;
1800 uByte sel;
1801
1802 d = iot[id].d.lu;
1803 sel = ((const uByte *)&d->baCSourceId[d->bNrInPins])[2]; /* iClockSelector */
1804 DPRINTFN(2,"bClockId=%d bNrInPins=%d iClockSelector=%d\n",
1805 d->bClockId, d->bNrInPins, sel);
1806 mix.wIndex = MAKE(d->bClockId, sc->sc_ac_iface);
1807 uaudio_determine_class(&iot[id], &mix);
1808 mix.nchan = 1;
1809 mix.wValue[0] = MAKE(V2_CUR_CLKSEL, 0);
1810 mix.type = MIX_SELECTOR;
1811 mix.ctlunit = "";
1812 mix.range0.minval = 1;
1813 mix.range0.maxval = d->bNrInPins;
1814 mix.range0.resval = 1;
1815 mix.mul = mix.range0.maxval - mix.range0.minval;
1816 wp = uaudio_makename(sc, sel, "clksel", d->bClockId, mix.ctlname, MAX_AUDIO_DEV_LEN);
1817 for (i = 1; i <= d->bNrInPins; i++) {
1818 wp += snprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp,
1819 "%si%d", i == 1 ? "-" : "", d->baCSourceId[i - 1]);
1820 if (wp > MAX_AUDIO_DEV_LEN - 1)
1821 break;
1822 }
1823 uaudio_mixer_add_ctl(sc, &mix);
1824 }
1825
1826 Static struct terminal_list*
1827 uaudio_merge_terminal_list(const struct io_terminal *iot)
1828 {
1829 struct terminal_list *tml;
1830 uint16_t *ptm;
1831 int i, len;
1832
1833 len = 0;
1834 if (iot->inputs == NULL)
1835 return NULL;
1836 for (i = 0; i < iot->inputs_size; i++) {
1837 if (iot->inputs[i] != NULL)
1838 len += iot->inputs[i]->size;
1839 }
1840 tml = malloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT);
1841 if (tml == NULL) {
1842 aprint_error("uaudio_merge_terminal_list: no memory\n");
1843 return NULL;
1844 }
1845 tml->size = 0;
1846 ptm = tml->terminals;
1847 for (i = 0; i < iot->inputs_size; i++) {
1848 if (iot->inputs[i] == NULL)
1849 continue;
1850 if (iot->inputs[i]->size > len)
1851 break;
1852 memcpy(ptm, iot->inputs[i]->terminals,
1853 iot->inputs[i]->size * sizeof(uint16_t));
1854 tml->size += iot->inputs[i]->size;
1855 ptm += iot->inputs[i]->size;
1856 len -= iot->inputs[i]->size;
1857 }
1858 return tml;
1859 }
1860
1861 Static struct terminal_list *
1862 uaudio_io_terminaltype(struct uaudio_softc *sc, int outtype, struct io_terminal *iot, int id)
1863 {
1864 struct terminal_list *tml;
1865 struct io_terminal *it;
1866 int src_id, i;
1867
1868 it = &iot[id];
1869 if (it->output != NULL) {
1870 /* already has outtype? */
1871 for (i = 0; i < it->output->size; i++)
1872 if (it->output->terminals[i] == outtype)
1873 return uaudio_merge_terminal_list(it);
1874 tml = malloc(TERMINAL_LIST_SIZE(it->output->size + 1),
1875 M_TEMP, M_NOWAIT);
1876 if (tml == NULL) {
1877 aprint_error("uaudio_io_terminaltype: no memory\n");
1878 return uaudio_merge_terminal_list(it);
1879 }
1880 memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size));
1881 tml->terminals[it->output->size] = outtype;
1882 tml->size++;
1883 free(it->output, M_TEMP);
1884 it->output = tml;
1885 if (it->inputs != NULL) {
1886 for (i = 0; i < it->inputs_size; i++)
1887 if (it->inputs[i] != NULL)
1888 free(it->inputs[i], M_TEMP);
1889 free(it->inputs, M_TEMP);
1890 }
1891 it->inputs_size = 0;
1892 it->inputs = NULL;
1893 } else { /* end `iot[id] != NULL' */
1894 it->inputs_size = 0;
1895 it->inputs = NULL;
1896 it->output = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1897 if (it->output == NULL) {
1898 aprint_error("uaudio_io_terminaltype: no memory\n");
1899 return NULL;
1900 }
1901 it->output->terminals[0] = outtype;
1902 it->output->size = 1;
1903 it->direct = FALSE;
1904 }
1905
1906 switch (it->d.desc->bDescriptorSubtype) {
1907 case UDESCSUB_AC_INPUT:
1908 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1909 if (it->inputs == NULL) {
1910 aprint_error("uaudio_io_terminaltype: no memory\n");
1911 return NULL;
1912 }
1913 tml = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1914 if (tml == NULL) {
1915 aprint_error("uaudio_io_terminaltype: no memory\n");
1916 free(it->inputs, M_TEMP);
1917 it->inputs = NULL;
1918 return NULL;
1919 }
1920 it->inputs[0] = tml;
1921 switch (sc->sc_version) {
1922 case UAUDIO_VERSION1:
1923 tml->terminals[0] = UGETW(it->d.it->v1.wTerminalType);
1924 break;
1925 case UAUDIO_VERSION2:
1926 tml->terminals[0] = UGETW(it->d.it->v2.wTerminalType);
1927 break;
1928 default:
1929 free(tml, M_TEMP);
1930 free(it->inputs, M_TEMP);
1931 it->inputs = NULL;
1932 return NULL;
1933 }
1934 tml->size = 1;
1935 it->inputs_size = 1;
1936 return uaudio_merge_terminal_list(it);
1937 case UDESCSUB_AC_FEATURE:
1938 switch (sc->sc_version) {
1939 case UAUDIO_VERSION1:
1940 src_id = it->d.fu->v1.bSourceId;
1941 break;
1942 case UAUDIO_VERSION2:
1943 src_id = it->d.fu->v2.bSourceId;
1944 break;
1945 default:
1946 /* cannot happen */
1947 return NULL;
1948 }
1949 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1950 if (it->inputs == NULL) {
1951 aprint_error("uaudio_io_terminaltype: no memory\n");
1952 return uaudio_io_terminaltype(sc, outtype, iot, src_id);
1953 }
1954 it->inputs[0] = uaudio_io_terminaltype(sc, outtype, iot, src_id);
1955 it->inputs_size = 1;
1956 return uaudio_merge_terminal_list(it);
1957 case UDESCSUB_AC_OUTPUT:
1958 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1959 if (it->inputs == NULL) {
1960 aprint_error("uaudio_io_terminaltype: no memory\n");
1961 return NULL;
1962 }
1963 switch (sc->sc_version) {
1964 case UAUDIO_VERSION1:
1965 src_id = it->d.ot->v1.bSourceId;
1966 break;
1967 case UAUDIO_VERSION2:
1968 src_id = it->d.ot->v2.bSourceId;
1969 break;
1970 default:
1971 free(it->inputs, M_TEMP);
1972 it->inputs = NULL;
1973 return NULL;
1974 }
1975 it->inputs[0] = uaudio_io_terminaltype(sc, outtype, iot, src_id);
1976 it->inputs_size = 1;
1977 iot[src_id].direct = TRUE;
1978 return NULL;
1979 case UDESCSUB_AC_MIXER:
1980 it->inputs_size = 0;
1981 it->inputs = malloc(sizeof(struct terminal_list *)
1982 * it->d.mu->bNrInPins, M_TEMP, M_NOWAIT);
1983 if (it->inputs == NULL) {
1984 aprint_error("uaudio_io_terminaltype: no memory\n");
1985 return NULL;
1986 }
1987 for (i = 0; i < it->d.mu->bNrInPins; i++) {
1988 src_id = it->d.mu->baSourceId[i];
1989 it->inputs[i] = uaudio_io_terminaltype(sc, outtype, iot,
1990 src_id);
1991 it->inputs_size++;
1992 }
1993 return uaudio_merge_terminal_list(it);
1994 case UDESCSUB_AC_SELECTOR:
1995 it->inputs_size = 0;
1996 it->inputs = malloc(sizeof(struct terminal_list *)
1997 * it->d.su->bNrInPins, M_TEMP, M_NOWAIT);
1998 if (it->inputs == NULL) {
1999 aprint_error("uaudio_io_terminaltype: no memory\n");
2000 return NULL;
2001 }
2002 for (i = 0; i < it->d.su->bNrInPins; i++) {
2003 src_id = it->d.su->baSourceId[i];
2004 it->inputs[i] = uaudio_io_terminaltype(sc, outtype, iot,
2005 src_id);
2006 it->inputs_size++;
2007 }
2008 return uaudio_merge_terminal_list(it);
2009 case UDESCSUB_AC_PROCESSING:
2010 it->inputs_size = 0;
2011 it->inputs = malloc(sizeof(struct terminal_list *)
2012 * it->d.pu->bNrInPins, M_TEMP, M_NOWAIT);
2013 if (it->inputs == NULL) {
2014 aprint_error("uaudio_io_terminaltype: no memory\n");
2015 return NULL;
2016 }
2017 for (i = 0; i < it->d.pu->bNrInPins; i++) {
2018 src_id = it->d.pu->baSourceId[i];
2019 it->inputs[i] = uaudio_io_terminaltype(sc, outtype, iot,
2020 src_id);
2021 it->inputs_size++;
2022 }
2023 return uaudio_merge_terminal_list(it);
2024 case UDESCSUB_AC_EXTENSION:
2025 it->inputs_size = 0;
2026 it->inputs = malloc(sizeof(struct terminal_list *)
2027 * it->d.eu->bNrInPins, M_TEMP, M_NOWAIT);
2028 if (it->inputs == NULL) {
2029 aprint_error("uaudio_io_terminaltype: no memory\n");
2030 return NULL;
2031 }
2032 for (i = 0; i < it->d.eu->bNrInPins; i++) {
2033 src_id = it->d.eu->baSourceId[i];
2034 it->inputs[i] = uaudio_io_terminaltype(sc, outtype, iot,
2035 src_id);
2036 it->inputs_size++;
2037 }
2038 return uaudio_merge_terminal_list(it);
2039 case UDESCSUB_AC_HEADER:
2040 default:
2041 return NULL;
2042 }
2043 }
2044
2045 Static usbd_status
2046 uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
2047 {
2048 usbd_status err;
2049
2050 err = uaudio_identify_ac(sc, cdesc);
2051 if (err)
2052 return err;
2053 err = uaudio_identify_as(sc, cdesc);
2054 if (err)
2055 return err;
2056
2057 uaudio_build_formats(sc);
2058 return 0;
2059 }
2060
2061 Static void
2062 uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai)
2063 {
2064 size_t len;
2065 struct as_info *nai;
2066
2067 len = sizeof(*ai) * (sc->sc_nalts + 1);
2068 nai = kmem_alloc(len, KM_SLEEP);
2069 /* Copy old data, if there was any */
2070 if (sc->sc_nalts != 0) {
2071 memcpy(nai, sc->sc_alts, sizeof(*ai) * (sc->sc_nalts));
2072 kmem_free(sc->sc_alts, sizeof(*ai) * sc->sc_nalts);
2073 }
2074 sc->sc_alts = nai;
2075 DPRINTFN(2,"adding alt=%d, enc=%d\n",
2076 ai->alt, ai->encoding);
2077 sc->sc_alts[sc->sc_nalts++] = *ai;
2078 }
2079
2080 Static usbd_status
2081 uaudio_process_as(struct uaudio_softc *sc, const char *tbuf, int *offsp,
2082 int size, const usb_interface_descriptor_t *id)
2083 {
2084 const union usb_audio_streaming_interface_descriptor *asid;
2085 const union usb_audio_streaming_type1_descriptor *asf1d;
2086 const usb_endpoint_descriptor_audio_t *ed;
2087 const usb_endpoint_descriptor_audio_t *epdesc1;
2088 const struct usb_audio_streaming_endpoint_descriptor *sed;
2089 int format, chan __unused, prec, bps, enc, terminal;
2090 int dir, type, sync, epcount;
2091 struct as_info ai;
2092 const char *format_str __unused;
2093 const uaudio_cs_descriptor_t *desc;
2094
2095 DPRINTF("offset = %d < %d\n", *offsp, size);
2096
2097 epcount = 0;
2098 asid = NULL;
2099 asf1d = NULL;
2100 ed = NULL;
2101 epdesc1 = NULL;
2102 sed = NULL;
2103
2104 while (*offsp < size) {
2105 desc = (const uaudio_cs_descriptor_t *)(tbuf + *offsp);
2106 if (*offsp + desc->bLength > size)
2107 return USBD_INVAL;
2108
2109 switch (desc->bDescriptorType) {
2110 case UDESC_CS_INTERFACE:
2111 switch (desc->bDescriptorSubtype) {
2112 case AS_GENERAL:
2113 if (asid != NULL)
2114 goto ignore;
2115 asid = (const union usb_audio_streaming_interface_descriptor *) desc;
2116 DPRINTF("asid: bTerminalLink=%d wFormatTag=%d bmFormats=0x%x bLength=%d\n",
2117 asid->v1.bTerminalLink, UGETW(asid->v1.wFormatTag),
2118 UGETDW(asid->v2.bmFormats), asid->v1.bLength);
2119 break;
2120 case FORMAT_TYPE:
2121 if (asf1d != NULL)
2122 goto ignore;
2123 asf1d = (const union usb_audio_streaming_type1_descriptor *) desc;
2124 DPRINTF("asf1d: bDescriptorType=%d bDescriptorSubtype=%d\n",
2125 asf1d->v1.bDescriptorType, asf1d->v1.bDescriptorSubtype);
2126 if (asf1d->v1.bFormatType != FORMAT_TYPE_I) {
2127 aprint_normal_dev(sc->sc_dev,
2128 "ignored setting with type %d format\n", asf1d->v1.bFormatType);
2129 return USBD_NORMAL_COMPLETION;
2130 }
2131 break;
2132 default:
2133 goto ignore;
2134 }
2135 break;
2136 case UDESC_ENDPOINT:
2137 epcount++;
2138 if (epcount > id->bNumEndpoints)
2139 goto ignore;
2140 switch (epcount) {
2141 case 1:
2142 ed = (const usb_endpoint_descriptor_audio_t *) desc;
2143 DPRINTF("endpoint[0] bLength=%d bDescriptorType=%d "
2144 "bEndpointAddress=%d bmAttributes=%#x wMaxPacketSize=%d "
2145 "bInterval=%d bRefresh=%d bSynchAddress=%d\n",
2146 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress,
2147 ed->bmAttributes, UGETW(ed->wMaxPacketSize),
2148 ed->bInterval,
2149 ed->bLength > 7 ? ed->bRefresh : 0,
2150 ed->bLength > 8 ? ed->bSynchAddress : 0);
2151 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
2152 return USBD_INVAL;
2153 break;
2154 case 2:
2155 epdesc1 = (const usb_endpoint_descriptor_audio_t *) desc;
2156 DPRINTF("endpoint[1] bLength=%d "
2157 "bDescriptorType=%d bEndpointAddress=%d "
2158 "bmAttributes=%#x wMaxPacketSize=%d bInterval=%d "
2159 "bRefresh=%d bSynchAddress=%d\n",
2160 epdesc1->bLength, epdesc1->bDescriptorType,
2161 epdesc1->bEndpointAddress, epdesc1->bmAttributes,
2162 UGETW(epdesc1->wMaxPacketSize), epdesc1->bInterval,
2163 epdesc1->bLength > 7 ? epdesc1->bRefresh : 0,
2164 epdesc1->bLength > 8 ? epdesc1->bSynchAddress : 0);
2165 #if 0
2166 if (epdesc1->bLength > 8 && epdesc1->bSynchAddress != 0) {
2167 aprint_error_dev(sc->sc_dev,
2168 "invalid endpoint: bSynchAddress=0\n");
2169 return USBD_INVAL;
2170 }
2171 #endif
2172 if (UE_GET_XFERTYPE(epdesc1->bmAttributes) != UE_ISOCHRONOUS) {
2173 aprint_error_dev(sc->sc_dev,
2174 "invalid endpoint: bmAttributes=%#x\n",
2175 epdesc1->bmAttributes);
2176 return USBD_INVAL;
2177 }
2178 #if 0
2179 if (ed->bLength > 8 && epdesc1->bEndpointAddress != ed->bSynchAddress) {
2180 aprint_error_dev(sc->sc_dev,
2181 "invalid endpoint addresses: "
2182 "ep[0]->bSynchAddress=%#x "
2183 "ep[1]->bEndpointAddress=%#x\n",
2184 ed->bSynchAddress, epdesc1->bEndpointAddress);
2185 return USBD_INVAL;
2186 }
2187 #endif
2188 /* UE_GET_ADDR(epdesc1->bEndpointAddress), and epdesc1->bRefresh */
2189 break;
2190 default:
2191 goto ignore;
2192 }
2193 break;
2194 case UDESC_CS_ENDPOINT:
2195 switch (desc->bDescriptorSubtype) {
2196 case AS_GENERAL:
2197 if (sed != NULL)
2198 goto ignore;
2199 sed = (const struct usb_audio_streaming_endpoint_descriptor *) desc;
2200 DPRINTF(" streadming_endpoint: offset=%d bLength=%d\n", *offsp, sed->bLength);
2201 break;
2202 default:
2203 goto ignore;
2204 }
2205 break;
2206 case UDESC_INTERFACE:
2207 case UDESC_DEVICE:
2208 goto leave;
2209 default:
2210 ignore:
2211 aprint_normal_dev(sc->sc_dev,
2212 "ignored descriptor type %d subtype %d\n",
2213 desc->bDescriptorType, desc->bDescriptorSubtype);
2214 break;
2215 }
2216
2217 *offsp += desc->bLength;
2218 }
2219 leave:
2220
2221 if (asid == NULL) {
2222 DPRINTF("%s", "No streaming interface descriptor found\n");
2223 return USBD_INVAL;
2224 }
2225 if (asf1d == NULL) {
2226 DPRINTF("%s", "No format type descriptor found\n");
2227 return USBD_INVAL;
2228 }
2229 if (ed == NULL) {
2230 DPRINTF("%s", "No endpoint descriptor found\n");
2231 return USBD_INVAL;
2232 }
2233 if (sed == NULL) {
2234 DPRINTF("%s", "No streaming endpoint descriptor found\n");
2235 return USBD_INVAL;
2236 }
2237
2238 dir = UE_GET_DIR(ed->bEndpointAddress);
2239 type = UE_GET_ISO_TYPE(ed->bmAttributes);
2240 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) &&
2241 dir == UE_DIR_IN && type == UE_ISO_ADAPT)
2242 type = UE_ISO_ASYNC;
2243 /* We can't handle endpoints that need a sync pipe yet. */
2244 sync = FALSE;
2245 if (dir == UE_DIR_IN && type == UE_ISO_ADAPT) {
2246 sync = TRUE;
2247 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
2248 aprint_normal_dev(sc->sc_dev,
2249 "ignored input endpoint of type adaptive\n");
2250 return USBD_NORMAL_COMPLETION;
2251 #endif
2252 }
2253 if (dir != UE_DIR_IN && type == UE_ISO_ASYNC) {
2254 sync = TRUE;
2255 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
2256 aprint_normal_dev(sc->sc_dev,
2257 "ignored output endpoint of type async\n");
2258 return USBD_NORMAL_COMPLETION;
2259 #endif
2260 }
2261 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
2262 if (sync && id->bNumEndpoints <= 1) {
2263 aprint_error_dev(sc->sc_dev,
2264 "a sync-pipe endpoint but no other endpoint\n");
2265 return USBD_INVAL;
2266 }
2267 #endif
2268 if (!sync && id->bNumEndpoints > 1) {
2269 aprint_error_dev(sc->sc_dev,
2270 "non sync-pipe endpoint but multiple endpoints\n");
2271 return USBD_INVAL;
2272 }
2273
2274 switch (sc->sc_version) {
2275 case UAUDIO_VERSION1:
2276 format = UGETW(asid->v1.wFormatTag);
2277 chan = asf1d->v1.bNrChannels;
2278 prec = asf1d->v1.bBitResolution;
2279 bps = asf1d->v1.bSubFrameSize;
2280 break;
2281 case UAUDIO_VERSION2:
2282 format = UGETDW(asid->v2.bmFormats);
2283 chan = asid->v2.bNrChannels;
2284 prec = asf1d->v2.bBitResolution;
2285 bps = asf1d->v2.bSubslotSize;
2286 break;
2287 default:
2288 aprint_error_dev(sc->sc_dev,
2289 "Unknown audio class %d\n", sc->sc_version);
2290 return USBD_INVAL;
2291 }
2292 if ((prec != 8 && prec != 16 && prec != 24 && prec != 32) || (bps < 1 || bps > 4)) {
2293 aprint_normal_dev(sc->sc_dev,
2294 "ignored setting with precision %d bps %d\n", prec, bps);
2295 return USBD_NORMAL_COMPLETION;
2296 }
2297 enc = AUDIO_ENCODING_NONE;
2298 switch (sc->sc_version) {
2299 case UAUDIO_VERSION1:
2300 terminal = 0;
2301 switch (format) {
2302 case UA_FMT_PCM:
2303 if (prec == 8) {
2304 sc->sc_altflags |= HAS_8;
2305 } else if (prec == 16) {
2306 sc->sc_altflags |= HAS_16;
2307 } else if (prec == 24) {
2308 sc->sc_altflags |= HAS_24;
2309 } else if (prec == 32) {
2310 sc->sc_altflags |= HAS_32;
2311 }
2312 enc = AUDIO_ENCODING_SLINEAR_LE;
2313 format_str = "pcm";
2314 break;
2315 case UA_FMT_PCM8:
2316 enc = AUDIO_ENCODING_ULINEAR_LE;
2317 sc->sc_altflags |= HAS_8U;
2318 format_str = "pcm8";
2319 break;
2320 case UA_FMT_ALAW:
2321 enc = AUDIO_ENCODING_ALAW;
2322 sc->sc_altflags |= HAS_ALAW;
2323 format_str = "alaw";
2324 break;
2325 case UA_FMT_MULAW:
2326 enc = AUDIO_ENCODING_ULAW;
2327 sc->sc_altflags |= HAS_MULAW;
2328 format_str = "mulaw";
2329 break;
2330 #ifdef notyet
2331 case UA_FMT_IEEE_FLOAT:
2332 break;
2333 #endif
2334 }
2335 break;
2336 case UAUDIO_VERSION2:
2337 terminal = asid->v2.bTerminalLink;
2338 if (format & UA_V2_FMT_PCM) {
2339 if (prec == 8) {
2340 sc->sc_altflags |= HAS_8;
2341 } else if (prec == 16) {
2342 sc->sc_altflags |= HAS_16;
2343 } else if (prec == 24) {
2344 sc->sc_altflags |= HAS_24;
2345 } else if (prec == 32) {
2346 sc->sc_altflags |= HAS_32;
2347 }
2348 enc = AUDIO_ENCODING_SLINEAR_LE;
2349 format_str = "pcm";
2350 } else if (format & UA_V2_FMT_PCM8) {
2351 enc = AUDIO_ENCODING_ULINEAR_LE;
2352 sc->sc_altflags |= HAS_8U;
2353 format_str = "pcm8";
2354 } else if (format & UA_V2_FMT_ALAW) {
2355 enc = AUDIO_ENCODING_ALAW;
2356 sc->sc_altflags |= HAS_ALAW;
2357 format_str = "alaw";
2358 } else if (format & UA_V2_FMT_MULAW) {
2359 enc = AUDIO_ENCODING_ULAW;
2360 sc->sc_altflags |= HAS_MULAW;
2361 format_str = "mulaw";
2362 #ifdef notyet
2363 } else if (format & UA_V2_FMT_IEEE_FLOAT) {
2364 #endif
2365 }
2366 break;
2367 }
2368 if (enc == AUDIO_ENCODING_NONE) {
2369 aprint_normal_dev(sc->sc_dev,
2370 "ignored setting with format 0x%08x\n", format);
2371 return USBD_NORMAL_COMPLETION;
2372 }
2373 #ifdef UAUDIO_DEBUG
2374 aprint_debug_dev(sc->sc_dev, "%s: %dch, %d/%dbit, %s,",
2375 dir == UE_DIR_IN ? "recording" : "playback",
2376 chan, prec, bps * 8, format_str);
2377 switch (sc->sc_version) {
2378 case UAUDIO_VERSION1:
2379 if (asf1d->v1.bSamFreqType == UA_SAMP_CONTINUOUS) {
2380 aprint_debug(" %d-%dHz\n", UA_SAMP_LO(&asf1d->v1),
2381 UA_SAMP_HI(&asf1d->v1));
2382 } else {
2383 int r;
2384 aprint_debug(" %d", UA_GETSAMP(&asf1d->v1, 0));
2385 for (r = 1; r < asf1d->v1.bSamFreqType; r++)
2386 aprint_debug(",%d", UA_GETSAMP(&asf1d->v1, r));
2387 aprint_debug("Hz\n");
2388 }
2389 break;
2390 /* UAUDIO_VERSION2 has no frequency information in the format */
2391 }
2392 #endif
2393 ai.alt = id->bAlternateSetting;
2394 ai.encoding = enc;
2395 ai.attributes = sed->bmAttributes;
2396 ai.idesc = id;
2397 ai.edesc = ed;
2398 ai.edesc1 = epdesc1;
2399 ai.asf1desc = asf1d;
2400 ai.sc_busy = 0;
2401 ai.nchan = chan;
2402 ai.aformat = NULL;
2403 ai.ifaceh = NULL;
2404 ai.terminal = terminal;
2405 uaudio_add_alt(sc, &ai);
2406 #ifdef UAUDIO_DEBUG
2407 if (ai.attributes & UA_SED_FREQ_CONTROL)
2408 DPRINTFN(1, "%s", "FREQ_CONTROL\n");
2409 if (ai.attributes & UA_SED_PITCH_CONTROL)
2410 DPRINTFN(1, "%s", "PITCH_CONTROL\n");
2411 #endif
2412 sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD;
2413
2414 return USBD_NORMAL_COMPLETION;
2415 }
2416
2417 Static usbd_status
2418 uaudio_identify_as(struct uaudio_softc *sc,
2419 const usb_config_descriptor_t *cdesc)
2420 {
2421 const usb_interface_descriptor_t *id;
2422 const char *tbuf;
2423 int size, offs;
2424
2425 size = UGETW(cdesc->wTotalLength);
2426 tbuf = (const char *)cdesc;
2427
2428 /* Locate the AudioStreaming interface descriptor. */
2429 offs = 0;
2430 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOSTREAM);
2431 if (id == NULL)
2432 return USBD_INVAL;
2433
2434 /* Loop through all the alternate settings. */
2435 while (offs <= size) {
2436 DPRINTFN(2, "interface=%d offset=%d\n",
2437 id->bInterfaceNumber, offs);
2438 switch (id->bNumEndpoints) {
2439 case 0:
2440 DPRINTFN(2, "AS null alt=%d\n",
2441 id->bAlternateSetting);
2442 sc->sc_nullalt = id->bAlternateSetting;
2443 break;
2444 case 1:
2445 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
2446 case 2:
2447 #endif
2448 uaudio_process_as(sc, tbuf, &offs, size, id);
2449 break;
2450 default:
2451 aprint_error_dev(sc->sc_dev,
2452 "ignored audio interface with %d endpoints\n",
2453 id->bNumEndpoints);
2454 break;
2455 }
2456 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOSTREAM);
2457 if (id == NULL)
2458 break;
2459 }
2460 if (offs > size)
2461 return USBD_INVAL;
2462 DPRINTF("%d alts available\n", sc->sc_nalts);
2463
2464 if (sc->sc_mode == 0) {
2465 aprint_error_dev(sc->sc_dev, "no usable endpoint found\n");
2466 return USBD_INVAL;
2467 }
2468
2469 if (sc->sc_nalts == 0) {
2470 aprint_error_dev(sc->sc_dev, "no audio formats found\n");
2471 return USBD_INVAL;
2472 }
2473
2474 return USBD_NORMAL_COMPLETION;
2475 }
2476
2477
2478 Static u_int
2479 uaudio_get_rates(struct uaudio_softc *sc, int mode, u_int *freqs, u_int len)
2480 {
2481 struct mixerctl *mc;
2482 u_int n, freq, start, end, step;
2483 int j, k, count;
2484
2485 n = 0;
2486 for (j = 0; j < sc->sc_nratectls; ++j) {
2487
2488 /*
2489 * skip rates not associated with a terminal
2490 * of the required mode (record/play)
2491 */
2492 if ((sc->sc_ratemode[j] & mode) == 0)
2493 continue;
2494
2495 mc = &sc->sc_ctls[sc->sc_ratectls[j]];
2496 count = mc->nranges ? mc->nranges : 1;
2497 for (k = 0; k < count; ++k) {
2498 start = (u_int) mc->ranges[k].minval;
2499 end = (u_int) mc->ranges[k].maxval;
2500 step = (u_int) mc->ranges[k].resval;
2501 for (freq = start; freq <= end; freq += step) {
2502 if (len != 0) {
2503 if (n >= len)
2504 goto done;
2505 freqs[n] = freq;
2506 }
2507 ++n;
2508 if (step == 0)
2509 break;
2510 }
2511 }
2512 }
2513
2514 done:
2515 return n;
2516 }
2517
2518 Static void
2519 uaudio_build_formats(struct uaudio_softc *sc)
2520 {
2521 struct audio_format *auf;
2522 const struct as_info *as;
2523 const union usb_audio_streaming_type1_descriptor *t1desc;
2524 int i, j;
2525
2526 /* build audio_format array */
2527 sc->sc_formats = kmem_zalloc(sizeof(struct audio_format) * sc->sc_nalts,
2528 KM_SLEEP);
2529 sc->sc_nformats = sc->sc_nalts;
2530
2531 for (i = 0; i < sc->sc_nalts; i++) {
2532 auf = &sc->sc_formats[i];
2533 as = &sc->sc_alts[i];
2534 t1desc = as->asf1desc;
2535 if (UE_GET_DIR(as->edesc->bEndpointAddress) == UE_DIR_OUT)
2536 auf->mode = AUMODE_PLAY;
2537 else
2538 auf->mode = AUMODE_RECORD;
2539 auf->encoding = as->encoding;
2540 auf->channel_mask = sc->sc_channel_config;
2541
2542 switch (sc->sc_version) {
2543 case UAUDIO_VERSION1:
2544 auf->validbits = t1desc->v1.bBitResolution;
2545 auf->precision = t1desc->v1.bSubFrameSize * 8;
2546 auf->channels = t1desc->v1.bNrChannels;
2547
2548 auf->frequency_type = t1desc->v1.bSamFreqType;
2549 if (t1desc->v1.bSamFreqType == UA_SAMP_CONTINUOUS) {
2550 auf->frequency[0] = UA_SAMP_LO(&t1desc->v1);
2551 auf->frequency[1] = UA_SAMP_HI(&t1desc->v1);
2552 } else {
2553 for (j = 0; j < t1desc->v1.bSamFreqType; j++) {
2554 if (j >= AUFMT_MAX_FREQUENCIES) {
2555 aprint_error("%s: please increase "
2556 "AUFMT_MAX_FREQUENCIES to %d\n",
2557 __func__, t1desc->v1.bSamFreqType);
2558 auf->frequency_type =
2559 AUFMT_MAX_FREQUENCIES;
2560 break;
2561 }
2562 auf->frequency[j] = UA_GETSAMP(&t1desc->v1, j);
2563 }
2564 }
2565 break;
2566 case UAUDIO_VERSION2:
2567 auf->validbits = t1desc->v2.bBitResolution;
2568 auf->precision = t1desc->v2.bSubslotSize * 8;
2569 auf->channels = as->nchan;
2570
2571 #if 0
2572 auf->frequency_type = uaudio_get_rates(sc, auf->mode, NULL, 0);
2573 if (auf->frequency_type >= AUFMT_MAX_FREQUENCIES) {
2574 aprint_error("%s: please increase "
2575 "AUFMT_MAX_FREQUENCIES to %d\n",
2576 __func__, auf->frequency_type);
2577 }
2578 #endif
2579
2580 auf->frequency_type = uaudio_get_rates(sc,
2581 auf->mode, auf->frequency, AUFMT_MAX_FREQUENCIES);
2582
2583 /*
2584 * if rate query failed, guess a rate
2585 */
2586 if (auf->frequency_type == UA_SAMP_CONTINUOUS) {
2587 auf->frequency[0] = 48000;
2588 auf->frequency[1] = 48000;
2589 }
2590
2591 break;
2592 }
2593
2594 DPRINTF("alt[%d] = %d/%d %dch %u[%u,%u,...] alt %u\n", i,
2595 auf->validbits, auf->precision, auf->channels, auf->frequency_type,
2596 auf->frequency[0], auf->frequency[1],
2597 as->idesc->bAlternateSetting);
2598
2599 sc->sc_alts[i].aformat = auf;
2600 }
2601 }
2602
2603 #ifdef UAUDIO_DEBUG
2604 Static void
2605 uaudio_dump_tml(struct terminal_list *tml) {
2606 if (tml == NULL) {
2607 printf("NULL");
2608 } else {
2609 int i;
2610 for (i = 0; i < tml->size; i++)
2611 printf("%s ", uaudio_get_terminal_name
2612 (tml->terminals[i]));
2613 }
2614 printf("\n");
2615 }
2616 #endif
2617
2618 Static usbd_status
2619 uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
2620 {
2621 struct io_terminal* iot;
2622 const usb_interface_descriptor_t *id;
2623 const struct usb_audio_control_descriptor *acdp;
2624 const uaudio_cs_descriptor_t *dp;
2625 const union usb_audio_output_terminal *pot;
2626 struct terminal_list *tml;
2627 const char *tbuf, *ibuf, *ibufend;
2628 int size, offs, ndps, i, j;
2629
2630 size = UGETW(cdesc->wTotalLength);
2631 tbuf = (const char *)cdesc;
2632
2633 /* Locate the AudioControl interface descriptor. */
2634 offs = 0;
2635 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOCONTROL);
2636 if (id == NULL)
2637 return USBD_INVAL;
2638 if (offs + sizeof(*acdp) > size)
2639 return USBD_INVAL;
2640 sc->sc_ac_iface = id->bInterfaceNumber;
2641 DPRINTFN(2,"AC interface is %d\n", sc->sc_ac_iface);
2642
2643 /* A class-specific AC interface header should follow. */
2644 ibuf = tbuf + offs;
2645 ibufend = tbuf + size;
2646 acdp = (const struct usb_audio_control_descriptor *)ibuf;
2647 if (acdp->bDescriptorType != UDESC_CS_INTERFACE ||
2648 acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)
2649 return USBD_INVAL;
2650
2651 if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC)) {
2652 sc->sc_version = UGETW(acdp->bcdADC);
2653 } else {
2654 sc->sc_version = UAUDIO_VERSION1;
2655 }
2656
2657 switch (sc->sc_version) {
2658 case UAUDIO_VERSION1:
2659 case UAUDIO_VERSION2:
2660 break;
2661 default:
2662 return USBD_INVAL;
2663 }
2664
2665 sc->sc_audio_rev = UGETW(acdp->bcdADC);
2666 DPRINTFN(2, "found AC header, vers=%03x\n", sc->sc_audio_rev);
2667
2668 sc->sc_nullalt = -1;
2669
2670 /* Scan through all the AC specific descriptors */
2671 dp = (const uaudio_cs_descriptor_t *)ibuf;
2672 ndps = 0;
2673 iot = malloc(sizeof(struct io_terminal) * 256, M_TEMP, M_NOWAIT | M_ZERO);
2674 if (iot == NULL) {
2675 aprint_error("%s: no memory\n", __func__);
2676 return USBD_NOMEM;
2677 }
2678 for (;;) {
2679 ibuf += dp->bLength;
2680 if (ibuf >= ibufend)
2681 break;
2682 dp = (const uaudio_cs_descriptor_t *)ibuf;
2683 if (ibuf + dp->bLength > ibufend) {
2684 free(iot, M_TEMP);
2685 return USBD_INVAL;
2686 }
2687 if (dp->bDescriptorType != UDESC_CS_INTERFACE)
2688 break;
2689 switch (sc->sc_version) {
2690 case UAUDIO_VERSION1:
2691 i = ((const union usb_audio_input_terminal *)dp)->v1.bTerminalId;
2692 break;
2693 case UAUDIO_VERSION2:
2694 i = ((const union usb_audio_input_terminal *)dp)->v2.bTerminalId;
2695 break;
2696 default:
2697 free(iot, M_TEMP);
2698 return USBD_INVAL;
2699 }
2700 iot[i].d.desc = dp;
2701 if (i > ndps)
2702 ndps = i;
2703 }
2704 ndps++;
2705
2706 /* construct io_terminal */
2707 for (i = 0; i < ndps; i++) {
2708 dp = iot[i].d.desc;
2709 if (dp == NULL)
2710 continue;
2711 if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT)
2712 continue;
2713 pot = iot[i].d.ot;
2714 switch (sc->sc_version) {
2715 case UAUDIO_VERSION1:
2716 tml = uaudio_io_terminaltype(sc, UGETW(pot->v1.wTerminalType), iot, i);
2717 break;
2718 case UAUDIO_VERSION2:
2719 tml = uaudio_io_terminaltype(sc, UGETW(pot->v2.wTerminalType), iot, i);
2720 break;
2721 default:
2722 tml = NULL;
2723 break;
2724 }
2725 if (tml != NULL)
2726 free(tml, M_TEMP);
2727 }
2728
2729 #ifdef UAUDIO_DEBUG
2730 for (i = 0; i < 256; i++) {
2731 union usb_audio_cluster cluster;
2732
2733 if (iot[i].d.desc == NULL)
2734 continue;
2735 printf("id %d:\t", i);
2736 switch (iot[i].d.desc->bDescriptorSubtype) {
2737 case UDESCSUB_AC_INPUT:
2738 printf("AC_INPUT type=%s\n", uaudio_get_terminal_name
2739 (UGETW(iot[i].d.it->v1.wTerminalType)));
2740 printf("\t");
2741 cluster = uaudio_get_cluster(sc, i, iot);
2742 uaudio_dump_cluster(sc, &cluster);
2743 printf("\n");
2744 break;
2745 case UDESCSUB_AC_OUTPUT:
2746 printf("AC_OUTPUT type=%s ", uaudio_get_terminal_name
2747 (UGETW(iot[i].d.ot->v1.wTerminalType)));
2748 printf("src=%d\n", iot[i].d.ot->v1.bSourceId);
2749 break;
2750 case UDESCSUB_AC_MIXER:
2751 printf("AC_MIXER src=");
2752 for (j = 0; j < iot[i].d.mu->bNrInPins; j++)
2753 printf("%d ", iot[i].d.mu->baSourceId[j]);
2754 printf("\n\t");
2755 cluster = uaudio_get_cluster(sc, i, iot);
2756 uaudio_dump_cluster(sc, &cluster);
2757 printf("\n");
2758 break;
2759 case UDESCSUB_AC_SELECTOR:
2760 printf("AC_SELECTOR src=");
2761 for (j = 0; j < iot[i].d.su->bNrInPins; j++)
2762 printf("%d ", iot[i].d.su->baSourceId[j]);
2763 printf("\n");
2764 break;
2765 case UDESCSUB_AC_FEATURE:
2766 switch (sc->sc_version) {
2767 case UAUDIO_VERSION1:
2768 printf("AC_FEATURE src=%d\n", iot[i].d.fu->v1.bSourceId);
2769 break;
2770 case UAUDIO_VERSION2:
2771 printf("AC_FEATURE src=%d\n", iot[i].d.fu->v2.bSourceId);
2772 break;
2773 }
2774 break;
2775 case UDESCSUB_AC_EFFECT:
2776 switch (sc->sc_version) {
2777 case UAUDIO_VERSION1:
2778 printf("AC_EFFECT src=%d\n", iot[i].d.fu->v1.bSourceId);
2779 break;
2780 case UAUDIO_VERSION2:
2781 printf("AC_EFFECT src=%d\n", iot[i].d.fu->v2.bSourceId);
2782 break;
2783 }
2784 break;
2785 case UDESCSUB_AC_PROCESSING:
2786 printf("AC_PROCESSING src=");
2787 for (j = 0; j < iot[i].d.pu->bNrInPins; j++)
2788 printf("%d ", iot[i].d.pu->baSourceId[j]);
2789 printf("\n\t");
2790 cluster = uaudio_get_cluster(sc, i, iot);
2791 uaudio_dump_cluster(sc, &cluster);
2792 printf("\n");
2793 break;
2794 case UDESCSUB_AC_EXTENSION:
2795 printf("AC_EXTENSION src=");
2796 for (j = 0; j < iot[i].d.eu->bNrInPins; j++)
2797 printf("%d ", iot[i].d.eu->baSourceId[j]);
2798 printf("\n\t");
2799 cluster = uaudio_get_cluster(sc, i, iot);
2800 uaudio_dump_cluster(sc, &cluster);
2801 printf("\n");
2802 break;
2803 case UDESCSUB_AC_CLKSRC:
2804 printf("AC_CLKSRC src=%d\n", iot[i].d.cu->iClockSource);
2805 break;
2806 case UDESCSUB_AC_CLKSEL:
2807 printf("AC_CLKSEL src=");
2808 for (j = 0; j < iot[i].d.su->bNrInPins; j++)
2809 printf("%d ", iot[i].d.su->baSourceId[j]);
2810 printf("\n");
2811 break;
2812 case UDESCSUB_AC_CLKMULT:
2813 printf("AC_CLKMULT not supported\n");
2814 break;
2815 case UDESCSUB_AC_RATECONV:
2816 printf("AC_RATEVONC not supported\n");
2817 break;
2818 default:
2819 printf("unknown audio control (subtype=%d)\n",
2820 iot[i].d.desc->bDescriptorSubtype);
2821 }
2822 for (j = 0; j < iot[i].inputs_size; j++) {
2823 printf("\tinput%d: ", j);
2824 uaudio_dump_tml(iot[i].inputs[j]);
2825 }
2826 printf("\toutput: ");
2827 uaudio_dump_tml(iot[i].output);
2828 }
2829 #endif
2830
2831 sc->sc_nratectls = 0;
2832 for (i = 0; i < ndps; i++) {
2833 dp = iot[i].d.desc;
2834 if (dp == NULL)
2835 continue;
2836 DPRINTF("id=%d subtype=%d\n", i, dp->bDescriptorSubtype);
2837 switch (dp->bDescriptorSubtype) {
2838 case UDESCSUB_AC_HEADER:
2839 aprint_error("uaudio_identify_ac: unexpected AC header\n");
2840 break;
2841 case UDESCSUB_AC_INPUT:
2842 uaudio_add_input(sc, iot, i);
2843 break;
2844 case UDESCSUB_AC_OUTPUT:
2845 uaudio_add_output(sc, iot, i);
2846 break;
2847 case UDESCSUB_AC_MIXER:
2848 uaudio_add_mixer(sc, iot, i);
2849 break;
2850 case UDESCSUB_AC_SELECTOR:
2851 uaudio_add_selector(sc, iot, i);
2852 break;
2853 case UDESCSUB_AC_FEATURE:
2854 uaudio_add_feature(sc, iot, i);
2855 break;
2856 case UDESCSUB_AC_EFFECT:
2857 uaudio_add_effect(sc, iot, i);
2858 break;
2859 case UDESCSUB_AC_PROCESSING:
2860 uaudio_add_processing(sc, iot, i);
2861 break;
2862 case UDESCSUB_AC_EXTENSION:
2863 uaudio_add_extension(sc, iot, i);
2864 break;
2865 case UDESCSUB_AC_CLKSRC:
2866 uaudio_add_clksrc(sc, iot, i);
2867 /* record ids of clock sources */
2868 if (sc->sc_nratectls < AUFMT_MAX_FREQUENCIES)
2869 sc->sc_ratectls[sc->sc_nratectls++] = sc->sc_nctls - 1;
2870 break;
2871 case UDESCSUB_AC_CLKSEL:
2872 uaudio_add_clksel(sc, iot, i);
2873 break;
2874 case UDESCSUB_AC_CLKMULT:
2875 /* not yet */
2876 break;
2877 case UDESCSUB_AC_RATECONV:
2878 /* not yet */
2879 break;
2880 default:
2881 aprint_error(
2882 "uaudio_identify_ac: bad AC desc subtype=0x%02x\n",
2883 dp->bDescriptorSubtype);
2884 break;
2885 }
2886 }
2887
2888 switch (sc->sc_version) {
2889 case UAUDIO_VERSION2:
2890 /*
2891 * UAC2 has separate rate controls which effectively creates
2892 * a set of audio_formats per input and output and their
2893 * associated clock sources.
2894 *
2895 * audio(4) can only handle audio_formats per direction.
2896 * - ignore stream terminals
2897 * - mark rates for record or play if associated with an input
2898 * or output terminal respectively.
2899 */
2900 for (j = 0; j < sc->sc_nratectls; ++j) {
2901 uint16_t wi = sc->sc_ctls[sc->sc_ratectls[j]].wIndex;
2902 sc->sc_ratemode[j] = 0;
2903 for (i = 0; i < ndps; i++) {
2904 dp = iot[i].d.desc;
2905 if (dp == NULL)
2906 continue;
2907 switch (dp->bDescriptorSubtype) {
2908 case UDESCSUB_AC_INPUT:
2909 if (UGETW(iot[i].d.it->v2.wTerminalType) != UAT_STREAM &&
2910 wi == MAKE(iot[i].d.it->v2.bCSourceId, sc->sc_ac_iface)) {
2911 sc->sc_ratemode[j] |= AUMODE_RECORD;
2912 }
2913 break;
2914 case UDESCSUB_AC_OUTPUT:
2915 if (UGETW(iot[i].d.it->v2.wTerminalType) != UAT_STREAM &&
2916 wi == MAKE(iot[i].d.ot->v2.bCSourceId, sc->sc_ac_iface)) {
2917 sc->sc_ratemode[j] |= AUMODE_PLAY;
2918 }
2919 break;
2920 }
2921 }
2922 }
2923 break;
2924 }
2925
2926 /* delete io_terminal */
2927 for (i = 0; i < 256; i++) {
2928 if (iot[i].d.desc == NULL)
2929 continue;
2930 if (iot[i].inputs != NULL) {
2931 for (j = 0; j < iot[i].inputs_size; j++) {
2932 if (iot[i].inputs[j] != NULL)
2933 free(iot[i].inputs[j], M_TEMP);
2934 }
2935 free(iot[i].inputs, M_TEMP);
2936 }
2937 if (iot[i].output != NULL)
2938 free(iot[i].output, M_TEMP);
2939 iot[i].d.desc = NULL;
2940 }
2941 free(iot, M_TEMP);
2942
2943 return USBD_NORMAL_COMPLETION;
2944 }
2945
2946 Static int
2947 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi)
2948 {
2949 struct uaudio_softc *sc;
2950 struct mixerctl *mc;
2951 int n, nctls, i;
2952
2953 DPRINTFN(7, "index=%d\n", mi->index);
2954 sc = addr;
2955 if (sc->sc_dying)
2956 return EIO;
2957
2958 n = mi->index;
2959 nctls = sc->sc_nctls;
2960
2961 switch (n) {
2962 case UAC_OUTPUT:
2963 mi->type = AUDIO_MIXER_CLASS;
2964 mi->mixer_class = UAC_OUTPUT;
2965 mi->next = mi->prev = AUDIO_MIXER_LAST;
2966 strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name));
2967 return 0;
2968 case UAC_INPUT:
2969 mi->type = AUDIO_MIXER_CLASS;
2970 mi->mixer_class = UAC_INPUT;
2971 mi->next = mi->prev = AUDIO_MIXER_LAST;
2972 strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name));
2973 return 0;
2974 case UAC_EQUAL:
2975 mi->type = AUDIO_MIXER_CLASS;
2976 mi->mixer_class = UAC_EQUAL;
2977 mi->next = mi->prev = AUDIO_MIXER_LAST;
2978 strlcpy(mi->label.name, AudioCequalization,
2979 sizeof(mi->label.name));
2980 return 0;
2981 case UAC_RECORD:
2982 mi->type = AUDIO_MIXER_CLASS;
2983 mi->mixer_class = UAC_RECORD;
2984 mi->next = mi->prev = AUDIO_MIXER_LAST;
2985 strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name));
2986 return 0;
2987 default:
2988 break;
2989 }
2990
2991 n -= UAC_NCLASSES;
2992 if (n < 0 || n >= nctls)
2993 return ENXIO;
2994
2995 mc = &sc->sc_ctls[n];
2996 strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name));
2997 mi->mixer_class = mc->class;
2998 mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */
2999 switch (mc->type) {
3000 case MIX_ON_OFF:
3001 mi->type = AUDIO_MIXER_ENUM;
3002 mi->un.e.num_mem = 2;
3003 strlcpy(mi->un.e.member[0].label.name, AudioNoff,
3004 sizeof(mi->un.e.member[0].label.name));
3005 mi->un.e.member[0].ord = 0;
3006 strlcpy(mi->un.e.member[1].label.name, AudioNon,
3007 sizeof(mi->un.e.member[1].label.name));
3008 mi->un.e.member[1].ord = 1;
3009 break;
3010 case MIX_SELECTOR:
3011 n = uimin(mc->ranges[0].maxval - mc->ranges[0].minval + 1,
3012 __arraycount(mi->un.e.member));
3013 mi->type = AUDIO_MIXER_ENUM;
3014 mi->un.e.num_mem = n;
3015 for (i = 0; i < n; i++) {
3016 snprintf(mi->un.e.member[i].label.name,
3017 sizeof(mi->un.e.member[i].label.name),
3018 "%d", i + mc->ranges[0].minval);
3019 mi->un.e.member[i].ord = i + mc->ranges[0].minval;
3020 }
3021 break;
3022 default:
3023 mi->type = AUDIO_MIXER_VALUE;
3024 strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN);
3025 mi->un.v.num_channels = mc->nchan;
3026 mi->un.v.delta = mc->delta;
3027 break;
3028 }
3029 return 0;
3030 }
3031
3032 Static int
3033 uaudio_open(void *addr, int flags)
3034 {
3035 struct uaudio_softc *sc;
3036
3037 sc = addr;
3038 DPRINTF("sc=%p\n", sc);
3039 if (sc->sc_dying)
3040 return EIO;
3041
3042 if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY))
3043 return EACCES;
3044 if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD))
3045 return EACCES;
3046
3047 return 0;
3048 }
3049
3050 Static int
3051 uaudio_halt_out_dma(void *addr)
3052 {
3053 struct uaudio_softc *sc = addr;
3054
3055 DPRINTF("%s", "enter\n");
3056
3057 mutex_exit(&sc->sc_intr_lock);
3058 uaudio_halt_out_dma_unlocked(sc);
3059 mutex_enter(&sc->sc_intr_lock);
3060
3061 return 0;
3062 }
3063
3064 Static void
3065 uaudio_halt_out_dma_unlocked(struct uaudio_softc *sc)
3066 {
3067 if (sc->sc_playchan.pipe != NULL) {
3068 uaudio_chan_abort(sc, &sc->sc_playchan);
3069 uaudio_chan_free_buffers(sc, &sc->sc_playchan);
3070 uaudio_chan_close(sc, &sc->sc_playchan);
3071 sc->sc_playchan.intr = NULL;
3072 }
3073 }
3074
3075 Static int
3076 uaudio_halt_in_dma(void *addr)
3077 {
3078 struct uaudio_softc *sc = addr;
3079
3080 DPRINTF("%s", "enter\n");
3081
3082 mutex_exit(&sc->sc_intr_lock);
3083 uaudio_halt_in_dma_unlocked(sc);
3084 mutex_enter(&sc->sc_intr_lock);
3085
3086 return 0;
3087 }
3088
3089 Static void
3090 uaudio_halt_in_dma_unlocked(struct uaudio_softc *sc)
3091 {
3092 if (sc->sc_recchan.pipe != NULL) {
3093 uaudio_chan_abort(sc, &sc->sc_recchan);
3094 uaudio_chan_free_buffers(sc, &sc->sc_recchan);
3095 uaudio_chan_close(sc, &sc->sc_recchan);
3096 sc->sc_recchan.intr = NULL;
3097 }
3098 }
3099
3100 Static int
3101 uaudio_getdev(void *addr, struct audio_device *retp)
3102 {
3103 struct uaudio_softc *sc;
3104
3105 DPRINTF("%s", "\n");
3106 sc = addr;
3107 if (sc->sc_dying)
3108 return EIO;
3109
3110 *retp = sc->sc_adev;
3111 return 0;
3112 }
3113
3114 /*
3115 * Make sure the block size is large enough to hold all outstanding transfers.
3116 */
3117 Static int
3118 uaudio_round_blocksize(void *addr, int blk,
3119 int mode, const audio_params_t *param)
3120 {
3121 struct uaudio_softc *sc;
3122 int b;
3123
3124 sc = addr;
3125 DPRINTF("blk=%d mode=%s\n", blk,
3126 mode == AUMODE_PLAY ? "AUMODE_PLAY" : "AUMODE_RECORD");
3127
3128 /* chan.bytes_per_frame can be 0. */
3129 if (mode == AUMODE_PLAY || sc->sc_recchan.bytes_per_frame <= 0) {
3130 b = param->sample_rate * sc->sc_recchan.nframes
3131 * sc->sc_recchan.nchanbufs;
3132
3133 /*
3134 * This does not make accurate value in the case
3135 * of b % usb_frames_per_second != 0
3136 */
3137 b /= sc->sc_usb_frames_per_second;
3138
3139 b *= param->precision / 8 * param->channels;
3140 } else {
3141 /*
3142 * use wMaxPacketSize in bytes_per_frame.
3143 * See uaudio_set_format() and uaudio_chan_init()
3144 */
3145 b = sc->sc_recchan.bytes_per_frame
3146 * sc->sc_recchan.nframes * sc->sc_recchan.nchanbufs;
3147 }
3148
3149 if (b <= 0)
3150 b = 1;
3151 blk = blk <= b ? b : blk / b * b;
3152
3153 #ifdef DIAGNOSTIC
3154 if (blk <= 0) {
3155 aprint_debug("uaudio_round_blocksize: blk=%d\n", blk);
3156 blk = 512;
3157 }
3158 #endif
3159
3160 DPRINTF("resultant blk=%d\n", blk);
3161 return blk;
3162 }
3163
3164 Static int
3165 uaudio_get_props(void *addr)
3166 {
3167 struct uaudio_softc *sc;
3168 int props;
3169
3170 sc = addr;
3171 props = 0;
3172 if ((sc->sc_mode & AUMODE_PLAY))
3173 props |= AUDIO_PROP_PLAYBACK;
3174 if ((sc->sc_mode & AUMODE_RECORD))
3175 props |= AUDIO_PROP_CAPTURE;
3176
3177 /* XXX I'm not sure all bidirectional devices support FULLDUP&INDEP */
3178 if (props == (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE))
3179 props |= AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
3180
3181 return props;
3182 }
3183
3184 Static void
3185 uaudio_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
3186 {
3187 struct uaudio_softc *sc;
3188
3189 sc = addr;
3190 *intr = &sc->sc_intr_lock;
3191 *thread = &sc->sc_lock;
3192 }
3193
3194 Static int
3195 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue,
3196 int wIndex, int len)
3197 {
3198 usb_device_request_t req;
3199 uint8_t data[4];
3200 usbd_status err;
3201 int val;
3202
3203 if (wValue == -1)
3204 return 0;
3205
3206 req.bmRequestType = type;
3207 req.bRequest = which;
3208 USETW(req.wValue, wValue);
3209 USETW(req.wIndex, wIndex);
3210 USETW(req.wLength, len);
3211 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
3212 "wIndex=0x%04x len=%d\n",
3213 type, which, wValue, wIndex, len);
3214 err = usbd_do_request(sc->sc_udev, &req, data);
3215 if (err) {
3216 DPRINTF("err=%s\n", usbd_errstr(err));
3217 return -1;
3218 }
3219 switch (len) {
3220 case 1:
3221 val = data[0];
3222 break;
3223 case 2:
3224 val = data[0];
3225 val |= data[1] << 8;
3226 break;
3227 case 3:
3228 val = data[0];
3229 val |= data[1] << 8;
3230 val |= data[2] << 16;
3231 break;
3232 case 4:
3233 val = data[0];
3234 val |= data[1] << 8;
3235 val |= data[2] << 16;
3236 val |= data[3] << 24;
3237 break;
3238 default:
3239 DPRINTF("bad length=%d\n", len);
3240 return -1;
3241 }
3242 DPRINTFN(2,"val=%d\n", val);
3243 return val;
3244 }
3245
3246 Static int
3247 uaudio_getbuf(struct uaudio_softc *sc, int which, int type, int wValue,
3248 int wIndex, int len, uint8_t *data)
3249 {
3250 usb_device_request_t req;
3251 usbd_status err;
3252
3253 req.bmRequestType = type;
3254 req.bRequest = which;
3255 USETW(req.wValue, wValue);
3256 USETW(req.wIndex, wIndex);
3257 USETW(req.wLength, len);
3258 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
3259 "wIndex=0x%04x len=%d\n",
3260 type, which, wValue, wIndex, len);
3261 err = usbd_do_request(sc->sc_udev, &req, data);
3262 if (err) {
3263 DPRINTF("err=%s\n", usbd_errstr(err));
3264 return -1;
3265 }
3266
3267 DPRINTFN(2,"val@%p\n", data);
3268 return 0;
3269 }
3270
3271 Static void
3272 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue,
3273 int wIndex, int len, int val)
3274 {
3275 usb_device_request_t req;
3276 uint8_t data[4];
3277 int err __unused;
3278
3279 if (wValue == -1)
3280 return;
3281
3282 req.bmRequestType = type;
3283 req.bRequest = which;
3284 USETW(req.wValue, wValue);
3285 USETW(req.wIndex, wIndex);
3286 USETW(req.wLength, len);
3287
3288 data[0] = val;
3289 data[1] = val >> 8;
3290 data[2] = val >> 16;
3291 data[3] = val >> 24;
3292
3293 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
3294 "wIndex=0x%04x len=%d, val=%d\n",
3295 type, which, wValue, wIndex, len, val);
3296 err = usbd_do_request(sc->sc_udev, &req, data);
3297 #ifdef UAUDIO_DEBUG
3298 if (err)
3299 DPRINTF("err=%s\n", usbd_errstr(err));
3300 #endif
3301 }
3302
3303 Static int
3304 uaudio_signext(int type, int val)
3305 {
3306 if (MIX_UNSIGNED(type)) {
3307 switch (MIX_SIZE(type)) {
3308 case 1:
3309 val = (uint8_t)val;
3310 break;
3311 case 2:
3312 val = (uint16_t)val;
3313 break;
3314 case 3:
3315 val = ((uint32_t)val << 8) >> 8;
3316 break;
3317 case 4:
3318 val = (uint32_t)val;
3319 break;
3320 }
3321 } else {
3322 switch (MIX_SIZE(type)) {
3323 case 1:
3324 val = (int8_t)val;
3325 break;
3326 case 2:
3327 val = (int16_t)val;
3328 break;
3329 case 3:
3330 val = ((int32_t)val << 8) >> 8;
3331 break;
3332 case 4:
3333 val = (int32_t)val;
3334 break;
3335 }
3336 }
3337 return val;
3338 }
3339
3340 Static int
3341 uaudio_value2bsd(struct mixerctl *mc, int val)
3342 {
3343 DPRINTFN(5, "type=%03x val=%d min=%d max=%d ",
3344 mc->type, val, mc->ranges[0].minval, mc->ranges[0].maxval);
3345 if (mc->type == MIX_ON_OFF) {
3346 val = (val != 0);
3347 } else if (mc->type == MIX_SELECTOR) {
3348 if (val < mc->ranges[0].minval)
3349 val = mc->ranges[0].minval;
3350 if (val > mc->ranges[0].maxval)
3351 val = mc->ranges[0].maxval;
3352 } else if (mc->mul > 0) {
3353 val = ((uaudio_signext(mc->type, val) - mc->ranges[0].minval)
3354 * 255 + mc->mul - 1) / mc->mul;
3355 } else
3356 val = 0;
3357 DPRINTFN_CLEAN(5, "val'=%d\n", val);
3358 return val;
3359 }
3360
3361 Static int
3362 uaudio_bsd2value(struct mixerctl *mc, int val)
3363 {
3364 int i;
3365
3366 DPRINTFN(5,"type=%03x val=%d min=%d max=%d ",
3367 mc->type, val, mc->ranges[0].minval, mc->ranges[0].maxval);
3368 if (mc->type == MIX_ON_OFF) {
3369 val = (val != 0);
3370 } else if (mc->type == MIX_SELECTOR) {
3371 if (val < mc->ranges[0].minval)
3372 val = mc->ranges[0].minval;
3373 if (val > mc->ranges[0].maxval)
3374 val = mc->ranges[0].maxval;
3375 } else {
3376 if (val < 0)
3377 val = 0;
3378 else if (val > 255)
3379 val = 255;
3380
3381 val = val * (mc->mul + 1) / 256 + mc->ranges[0].minval;
3382
3383 for (i=0; i<mc->nranges; ++i) {
3384 struct range *r = &mc->ranges[i];
3385
3386 if (r->resval == 0)
3387 continue;
3388 if (val > r->maxval)
3389 continue;
3390 if (val < r->minval)
3391 val = r->minval;
3392 val = (val - r->minval + r->resval/2)
3393 / r->resval * r->resval
3394 + r->minval;
3395 break;
3396 }
3397 }
3398 DPRINTFN_CLEAN(5, "val'=%d\n", val);
3399 return val;
3400 }
3401
3402 Static const char *
3403 uaudio_clockname(u_int attr)
3404 {
3405 static const char *names[] = {
3406 "clkext",
3407 "clkfixed",
3408 "clkvar",
3409 "clkprog"
3410 };
3411
3412 return names[attr & 3];
3413 }
3414
3415 Static int
3416 uaudio_makename(struct uaudio_softc *sc, uByte idx, const char *defname, uByte id, char *buf, size_t len)
3417 {
3418 char *tmp;
3419 int err, count;
3420
3421 tmp = kmem_alloc(USB_MAX_ENCODED_STRING_LEN, KM_SLEEP);
3422 err = usbd_get_string0(sc->sc_udev, idx, tmp, true);
3423
3424 if (id != 0 || err)
3425 count = snprintf(buf, len, "%s%d", err ? defname : tmp, id);
3426 else
3427 count = snprintf(buf, len, "%s", err ? defname : tmp);
3428
3429 kmem_free(tmp, USB_MAX_ENCODED_STRING_LEN);
3430
3431 return count;
3432 }
3433
3434
3435 Static int
3436 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc,
3437 int chan)
3438 {
3439 int val;
3440
3441 DPRINTFN(5,"which=%d chan=%d ctl=%s type=%d\n", which, chan, mc->ctlname, mc->type);
3442 mutex_exit(&sc->sc_lock);
3443 val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan],
3444 mc->wIndex, MIX_SIZE(mc->type));
3445 mutex_enter(&sc->sc_lock);
3446 return uaudio_value2bsd(mc, val);
3447 }
3448
3449 Static void
3450 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc,
3451 int chan, int val)
3452 {
3453
3454 DPRINTFN(5,"which=%d chan=%d ctl=%s type=%d\n", which, chan, mc->ctlname, mc->type);
3455 val = uaudio_bsd2value(mc, val);
3456 mutex_exit(&sc->sc_lock);
3457 uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan],
3458 mc->wIndex, MIX_SIZE(mc->type), val);
3459 mutex_enter(&sc->sc_lock);
3460 }
3461
3462 Static int
3463 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp)
3464 {
3465 struct uaudio_softc *sc;
3466 struct mixerctl *mc;
3467 int i, n, vals[MIX_MAX_CHAN], val;
3468 int req;
3469
3470 DPRINTFN(2, "index=%d\n", cp->dev);
3471 sc = addr;
3472 if (sc->sc_dying)
3473 return EIO;
3474
3475 req = sc->sc_version == UAUDIO_VERSION2 ? V2_CUR : GET_CUR;
3476
3477 n = cp->dev - UAC_NCLASSES;
3478 if (n < 0 || n >= sc->sc_nctls)
3479 return ENXIO;
3480 mc = &sc->sc_ctls[n];
3481
3482 if (mc->type == MIX_ON_OFF) {
3483 if (cp->type != AUDIO_MIXER_ENUM)
3484 return EINVAL;
3485 cp->un.ord = uaudio_ctl_get(sc, req, mc, 0);
3486 } else if (mc->type == MIX_SELECTOR) {
3487 if (cp->type != AUDIO_MIXER_ENUM)
3488 return EINVAL;
3489 cp->un.ord = uaudio_ctl_get(sc, req, mc, 0);
3490 } else {
3491 if (cp->type != AUDIO_MIXER_VALUE)
3492 return EINVAL;
3493 if (cp->un.value.num_channels != 1 &&
3494 cp->un.value.num_channels != mc->nchan)
3495 return EINVAL;
3496 for (i = 0; i < mc->nchan; i++)
3497 vals[i] = uaudio_ctl_get(sc, req, mc, i);
3498 if (cp->un.value.num_channels == 1 && mc->nchan != 1) {
3499 for (val = 0, i = 0; i < mc->nchan; i++)
3500 val += vals[i];
3501 vals[0] = val / mc->nchan;
3502 }
3503 for (i = 0; i < cp->un.value.num_channels; i++)
3504 cp->un.value.level[i] = vals[i];
3505 }
3506
3507 return 0;
3508 }
3509
3510 Static int
3511 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp)
3512 {
3513 struct uaudio_softc *sc;
3514 struct mixerctl *mc;
3515 int i, n, vals[MIX_MAX_CHAN];
3516 int req;
3517
3518 DPRINTFN(2, "index = %d\n", cp->dev);
3519 sc = addr;
3520 if (sc->sc_dying)
3521 return EIO;
3522
3523 req = sc->sc_version == UAUDIO_VERSION2 ? V2_CUR : SET_CUR;
3524
3525 n = cp->dev - UAC_NCLASSES;
3526 if (n < 0 || n >= sc->sc_nctls)
3527 return ENXIO;
3528 mc = &sc->sc_ctls[n];
3529
3530 if (mc->type == MIX_ON_OFF) {
3531 if (cp->type != AUDIO_MIXER_ENUM)
3532 return EINVAL;
3533 uaudio_ctl_set(sc, req, mc, 0, cp->un.ord);
3534 } else if (mc->type == MIX_SELECTOR) {
3535 if (cp->type != AUDIO_MIXER_ENUM)
3536 return EINVAL;
3537 uaudio_ctl_set(sc, req, mc, 0, cp->un.ord);
3538 } else {
3539 if (cp->type != AUDIO_MIXER_VALUE)
3540 return EINVAL;
3541 if (cp->un.value.num_channels == 1)
3542 for (i = 0; i < mc->nchan; i++)
3543 vals[i] = cp->un.value.level[0];
3544 else if (cp->un.value.num_channels == mc->nchan)
3545 for (i = 0; i < mc->nchan; i++)
3546 vals[i] = cp->un.value.level[i];
3547 else
3548 return EINVAL;
3549 for (i = 0; i < mc->nchan; i++)
3550 uaudio_ctl_set(sc, req, mc, i, vals[i]);
3551 }
3552 return 0;
3553 }
3554
3555 Static int
3556 uaudio_trigger_input(void *addr, void *start, void *end, int blksize,
3557 void (*intr)(void *), void *arg,
3558 const audio_params_t *param)
3559 {
3560 struct uaudio_softc *sc;
3561 struct chan *ch;
3562 usbd_status err;
3563 int i;
3564
3565 sc = addr;
3566 if (sc->sc_dying)
3567 return EIO;
3568
3569 mutex_exit(&sc->sc_intr_lock);
3570
3571 DPRINTFN(3, "sc=%p start=%p end=%p "
3572 "blksize=%d\n", sc, start, end, blksize);
3573 ch = &sc->sc_recchan;
3574 uaudio_chan_set_param(ch, start, end, blksize);
3575 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
3576 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
3577 ch->fraction);
3578
3579 err = uaudio_chan_open(sc, ch);
3580 if (err) {
3581 mutex_enter(&sc->sc_intr_lock);
3582 device_printf(sc->sc_dev,"%s open channel err=%s\n",__func__, usbd_errstr(err));
3583 return EIO;
3584 }
3585
3586 err = uaudio_chan_alloc_buffers(sc, ch);
3587 if (err) {
3588 uaudio_chan_close(sc, ch);
3589 device_printf(sc->sc_dev,"%s alloc buffers err=%s\n",__func__, usbd_errstr(err));
3590 mutex_enter(&sc->sc_intr_lock);
3591 return EIO;
3592 }
3593
3594
3595 ch->intr = intr;
3596 ch->arg = arg;
3597
3598 /*
3599 * Start as half as many channels for recording as for playback.
3600 * This stops playback from stuttering in full-duplex operation.
3601 */
3602 for (i = 0; i < ch->nchanbufs / 2; i++) {
3603 uaudio_chan_rtransfer(ch);
3604 }
3605
3606 mutex_enter(&sc->sc_intr_lock);
3607
3608 return 0;
3609 }
3610
3611 Static int
3612 uaudio_trigger_output(void *addr, void *start, void *end, int blksize,
3613 void (*intr)(void *), void *arg,
3614 const audio_params_t *param)
3615 {
3616 struct uaudio_softc *sc;
3617 struct chan *ch;
3618 usbd_status err;
3619 int i;
3620
3621 sc = addr;
3622 if (sc->sc_dying)
3623 return EIO;
3624
3625 mutex_exit(&sc->sc_intr_lock);
3626
3627 DPRINTFN(3, "sc=%p start=%p end=%p "
3628 "blksize=%d\n", sc, start, end, blksize);
3629 ch = &sc->sc_playchan;
3630 uaudio_chan_set_param(ch, start, end, blksize);
3631 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
3632 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
3633 ch->fraction);
3634
3635 err = uaudio_chan_open(sc, ch);
3636 if (err) {
3637 mutex_enter(&sc->sc_intr_lock);
3638 device_printf(sc->sc_dev,"%s open channel err=%s\n",__func__, usbd_errstr(err));
3639 return EIO;
3640 }
3641
3642 err = uaudio_chan_alloc_buffers(sc, ch);
3643 if (err) {
3644 uaudio_chan_close(sc, ch);
3645 device_printf(sc->sc_dev,"%s alloc buffers err=%s\n",__func__, usbd_errstr(err));
3646 mutex_enter(&sc->sc_intr_lock);
3647 return EIO;
3648 }
3649
3650 ch->intr = intr;
3651 ch->arg = arg;
3652
3653 for (i = 0; i < ch->nchanbufs; i++)
3654 uaudio_chan_ptransfer(ch);
3655
3656 mutex_enter(&sc->sc_intr_lock);
3657
3658 return 0;
3659 }
3660
3661 /* Set up a pipe for a channel. */
3662 Static usbd_status
3663 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch)
3664 {
3665 struct as_info *as;
3666 usb_device_descriptor_t *ddesc;
3667 int endpt, clkid;
3668 usbd_status err;
3669
3670 as = &sc->sc_alts[ch->altidx];
3671 endpt = as->edesc->bEndpointAddress;
3672 clkid = sc->sc_clock[as->terminal];
3673 DPRINTF("endpt=0x%02x, clkid=%d, speed=%d, alt=%d\n",
3674 endpt, clkid, ch->sample_rate, as->alt);
3675
3676 /* Set alternate interface corresponding to the mode. */
3677 err = usbd_set_interface(as->ifaceh, as->alt);
3678 if (err)
3679 return err;
3680
3681 /*
3682 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request.
3683 */
3684 ddesc = usbd_get_device_descriptor(sc->sc_udev);
3685 if ((UGETW(ddesc->idVendor) != USB_VENDOR_ROLAND) &&
3686 (UGETW(ddesc->idProduct) != USB_PRODUCT_ROLAND_SD90)) {
3687 err = uaudio_set_speed(sc, endpt, clkid, ch->sample_rate);
3688 if (err) {
3689 DPRINTF("set_speed failed err=%s\n", usbd_errstr(err));
3690 }
3691 }
3692
3693 DPRINTF("create pipe to 0x%02x\n", endpt);
3694 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE, &ch->pipe);
3695 if (err)
3696 return err;
3697 if (as->edesc1 != NULL) {
3698 endpt = as->edesc1->bEndpointAddress;
3699 if (endpt != 0) {
3700 DPRINTF("create sync-pipe to 0x%02x\n", endpt);
3701 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE,
3702 &ch->sync_pipe);
3703 }
3704 }
3705
3706 return err;
3707 }
3708
3709 Static void
3710 uaudio_chan_abort(struct uaudio_softc *sc, struct chan *ch)
3711 {
3712 struct usbd_pipe *pipe;
3713 struct as_info *as;
3714
3715 as = &sc->sc_alts[ch->altidx];
3716 as->sc_busy = 0;
3717 if (sc->sc_nullalt >= 0) {
3718 DPRINTF("set null alt=%d\n", sc->sc_nullalt);
3719 usbd_set_interface(as->ifaceh, sc->sc_nullalt);
3720 }
3721 pipe = ch->pipe;
3722 if (pipe) {
3723 usbd_abort_pipe(pipe);
3724 }
3725 pipe = ch->sync_pipe;
3726 if (pipe) {
3727 usbd_abort_pipe(pipe);
3728 }
3729 }
3730
3731 Static void
3732 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch)
3733 {
3734 struct usbd_pipe *pipe;
3735
3736 pipe = atomic_swap_ptr(&ch->pipe, NULL);
3737 if (pipe) {
3738 usbd_close_pipe(pipe);
3739 }
3740 pipe = atomic_swap_ptr(&ch->sync_pipe, NULL);
3741 if (pipe) {
3742 usbd_close_pipe(pipe);
3743 }
3744 }
3745
3746 Static usbd_status
3747 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch)
3748 {
3749 int i, size;
3750
3751 size = (ch->bytes_per_frame + ch->sample_size) * ch->nframes;
3752 for (i = 0; i < ch->nchanbufs; i++) {
3753 struct usbd_xfer *xfer;
3754
3755 int err = usbd_create_xfer(ch->pipe, size, 0, ch->nframes,
3756 &xfer);
3757 if (err)
3758 goto bad;
3759
3760 ch->chanbufs[i].xfer = xfer;
3761 ch->chanbufs[i].buffer = usbd_get_buffer(xfer);
3762 ch->chanbufs[i].chan = ch;
3763 }
3764
3765 return USBD_NORMAL_COMPLETION;
3766
3767 bad:
3768 while (--i >= 0)
3769 /* implicit buffer free */
3770 usbd_destroy_xfer(ch->chanbufs[i].xfer);
3771 return USBD_NOMEM;
3772 }
3773
3774 Static void
3775 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch)
3776 {
3777 int i;
3778
3779 for (i = 0; i < ch->nchanbufs; i++)
3780 usbd_destroy_xfer(ch->chanbufs[i].xfer);
3781 }
3782
3783 Static void
3784 uaudio_chan_ptransfer(struct chan *ch)
3785 {
3786 struct uaudio_softc *sc = ch->sc;
3787 struct chanbuf *cb;
3788 int i, n, size, residue, total;
3789
3790 if (sc->sc_dying)
3791 return;
3792
3793 /* Pick the next channel buffer. */
3794 cb = &ch->chanbufs[ch->curchanbuf];
3795 if (++ch->curchanbuf >= ch->nchanbufs)
3796 ch->curchanbuf = 0;
3797
3798 /* Compute the size of each frame in the next transfer. */
3799 residue = ch->residue;
3800 total = 0;
3801 for (i = 0; i < ch->nframes; i++) {
3802 size = ch->bytes_per_frame;
3803 residue += ch->fraction;
3804 if (residue >= sc->sc_usb_frames_per_second) {
3805 if ((sc->sc_altflags & UA_NOFRAC) == 0)
3806 size += ch->sample_size;
3807 residue -= sc->sc_usb_frames_per_second;
3808 }
3809 cb->sizes[i] = size;
3810 total += size;
3811 }
3812 ch->residue = residue;
3813 cb->size = total;
3814
3815 /*
3816 * Transfer data from upper layer buffer to channel buffer, taking
3817 * care of wrapping the upper layer buffer.
3818 */
3819 n = uimin(total, ch->end - ch->cur);
3820 memcpy(cb->buffer, ch->cur, n);
3821 ch->cur += n;
3822 if (ch->cur >= ch->end)
3823 ch->cur = ch->start;
3824 if (total > n) {
3825 total -= n;
3826 memcpy(cb->buffer + n, ch->cur, total);
3827 ch->cur += total;
3828 }
3829
3830 #ifdef UAUDIO_DEBUG
3831 if (uaudiodebug > 8) {
3832 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
3833 for (i = 0; i < ch->nframes; i++) {
3834 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
3835 }
3836 }
3837 #endif
3838
3839 //DPRINTFN(5, "ptransfer xfer=%p\n", cb->xfer);
3840 /* Fill the request */
3841 usbd_setup_isoc_xfer(cb->xfer, cb, cb->sizes, ch->nframes, 0,
3842 uaudio_chan_pintr);
3843
3844 usbd_status err = usbd_transfer(cb->xfer);
3845 if (err != USBD_IN_PROGRESS && err != USBD_NORMAL_COMPLETION)
3846 device_printf(sc->sc_dev, "ptransfer error %d\n", err);
3847 }
3848
3849 Static void
3850 uaudio_chan_pintr(struct usbd_xfer *xfer, void *priv,
3851 usbd_status status)
3852 {
3853 struct uaudio_softc *sc;
3854 struct chanbuf *cb;
3855 struct chan *ch;
3856 uint32_t count;
3857
3858 cb = priv;
3859 ch = cb->chan;
3860 sc = ch->sc;
3861 /* Return if we are aborting. */
3862 if (status == USBD_CANCELLED)
3863 return;
3864
3865 if (status != USBD_NORMAL_COMPLETION)
3866 device_printf(sc->sc_dev, "pintr error: %s\n",
3867 usbd_errstr(status));
3868
3869 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
3870 DPRINTFN(5, "count=%d, transferred=%d\n",
3871 count, ch->transferred);
3872 #ifdef DIAGNOSTIC
3873 if (count != cb->size) {
3874 device_printf(sc->sc_dev,
3875 "uaudio_chan_pintr: count(%d) != size(%d), status(%d)\n",
3876 count, cb->size, status);
3877 }
3878 #endif
3879
3880 mutex_enter(&sc->sc_intr_lock);
3881 ch->transferred += cb->size;
3882 /* Call back to upper layer */
3883 while (ch->transferred >= ch->blksize) {
3884 ch->transferred -= ch->blksize;
3885 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
3886 ch->intr(ch->arg);
3887 }
3888 mutex_exit(&sc->sc_intr_lock);
3889
3890 /* start next transfer */
3891 uaudio_chan_ptransfer(ch);
3892 }
3893
3894 Static void
3895 uaudio_chan_rtransfer(struct chan *ch)
3896 {
3897 struct uaudio_softc *sc = ch->sc;
3898 struct chanbuf *cb;
3899 int i, size, residue, total;
3900
3901 if (sc->sc_dying)
3902 return;
3903
3904 /* Pick the next channel buffer. */
3905 cb = &ch->chanbufs[ch->curchanbuf];
3906 if (++ch->curchanbuf >= ch->nchanbufs)
3907 ch->curchanbuf = 0;
3908
3909 /* Compute the size of each frame in the next transfer. */
3910 residue = ch->residue;
3911 total = 0;
3912 for (i = 0; i < ch->nframes; i++) {
3913 size = ch->bytes_per_frame;
3914 #if 0
3915 residue += ch->fraction;
3916 if (residue >= sc->sc_usb_frames_per_second) {
3917 if ((sc->sc_altflags & UA_NOFRAC) == 0)
3918 size += ch->sample_size;
3919 residue -= sc->sc_usb_frames_per_second;
3920 }
3921 #endif
3922 cb->sizes[i] = size;
3923 cb->offsets[i] = total;
3924 total += size;
3925 }
3926 ch->residue = residue;
3927 cb->size = total;
3928
3929 #ifdef UAUDIO_DEBUG
3930 if (uaudiodebug > 8) {
3931 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
3932 for (i = 0; i < ch->nframes; i++) {
3933 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
3934 }
3935 }
3936 #endif
3937
3938 DPRINTFN(5, "transfer xfer=%p\n", cb->xfer);
3939 /* Fill the request */
3940 usbd_setup_isoc_xfer(cb->xfer, cb, cb->sizes, ch->nframes, 0,
3941 uaudio_chan_rintr);
3942
3943 usbd_status err = usbd_transfer(cb->xfer);
3944 if (err != USBD_IN_PROGRESS && err != USBD_NORMAL_COMPLETION)
3945 device_printf(sc->sc_dev, "rtransfer error %d\n", err);
3946 }
3947
3948 Static void
3949 uaudio_chan_rintr(struct usbd_xfer *xfer, void *priv,
3950 usbd_status status)
3951 {
3952 struct uaudio_softc *sc;
3953 struct chanbuf *cb;
3954 struct chan *ch;
3955 uint32_t count;
3956 int i, n, frsize;
3957
3958 cb = priv;
3959 ch = cb->chan;
3960 sc = ch->sc;
3961 /* Return if we are aborting. */
3962 if (status == USBD_CANCELLED)
3963 return;
3964
3965 if (status != USBD_NORMAL_COMPLETION && status != USBD_SHORT_XFER)
3966 device_printf(sc->sc_dev, "rintr error: %s\n",
3967 usbd_errstr(status));
3968
3969 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
3970 DPRINTFN(5, "count=%d, transferred=%d\n", count, ch->transferred);
3971
3972 /* count < cb->size is normal for asynchronous source */
3973 #ifdef DIAGNOSTIC
3974 if (count > cb->size) {
3975 device_printf(sc->sc_dev,
3976 "uaudio_chan_rintr: count(%d) > size(%d) status(%d)\n",
3977 count, cb->size, status);
3978 }
3979 #endif
3980
3981 /*
3982 * Transfer data from channel buffer to upper layer buffer, taking
3983 * care of wrapping the upper layer buffer.
3984 */
3985 for (i = 0; i < ch->nframes; i++) {
3986 frsize = cb->sizes[i];
3987 n = uimin(frsize, ch->end - ch->cur);
3988 memcpy(ch->cur, cb->buffer + cb->offsets[i], n);
3989 ch->cur += n;
3990 if (ch->cur >= ch->end)
3991 ch->cur = ch->start;
3992 if (frsize > n) {
3993 memcpy(ch->cur, cb->buffer + cb->offsets[i] + n,
3994 frsize - n);
3995 ch->cur += frsize - n;
3996 }
3997 }
3998
3999 /* Call back to upper layer */
4000 mutex_enter(&sc->sc_intr_lock);
4001 ch->transferred += count;
4002 while (ch->transferred >= ch->blksize) {
4003 ch->transferred -= ch->blksize;
4004 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
4005 ch->intr(ch->arg);
4006 }
4007 mutex_exit(&sc->sc_intr_lock);
4008
4009 /* start next transfer */
4010 uaudio_chan_rtransfer(ch);
4011 }
4012
4013 Static void
4014 uaudio_chan_init(struct chan *ch, int altidx,
4015 const struct audio_params *param, int maxpktsize, bool isrecord)
4016 {
4017 struct uaudio_softc *sc = ch->sc;
4018 int samples_per_frame, sample_size;
4019
4020 DPRINTFN(5, "altidx=%d, %d/%d %dch %dHz ufps %u max %d\n",
4021 altidx, param->validbits, param->precision, param->channels,
4022 param->sample_rate, sc->sc_usb_frames_per_second, maxpktsize);
4023
4024 ch->altidx = altidx;
4025 sample_size = param->precision * param->channels / 8;
4026
4027 if (isrecord) {
4028 if (maxpktsize >= sample_size)
4029 samples_per_frame = maxpktsize / sample_size;
4030 else
4031 samples_per_frame = param->sample_rate / sc->sc_usb_frames_per_second
4032 + param->channels;
4033 ch->fraction = 0;
4034 } else {
4035 samples_per_frame = param->sample_rate / sc->sc_usb_frames_per_second;
4036 ch->fraction = param->sample_rate % sc->sc_usb_frames_per_second;
4037 }
4038
4039 ch->sample_size = sample_size;
4040 ch->sample_rate = param->sample_rate;
4041 ch->bytes_per_frame = samples_per_frame * sample_size;
4042
4043 if (maxpktsize > 0 && ch->bytes_per_frame > maxpktsize) {
4044 samples_per_frame = maxpktsize / sample_size;
4045 ch->bytes_per_frame = samples_per_frame * sample_size;
4046 }
4047
4048 ch->residue = 0;
4049 }
4050
4051 Static void
4052 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize)
4053 {
4054
4055 ch->start = start;
4056 ch->end = end;
4057 ch->cur = start;
4058 ch->blksize = blksize;
4059 ch->transferred = 0;
4060 ch->curchanbuf = 0;
4061 }
4062
4063 Static int
4064 uaudio_set_format(void *addr, int setmode,
4065 const audio_params_t *play, const audio_params_t *rec,
4066 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
4067 {
4068 struct uaudio_softc *sc;
4069 int paltidx, raltidx;
4070
4071 sc = addr;
4072 paltidx = -1;
4073 raltidx = -1;
4074 if (sc->sc_dying)
4075 return EIO;
4076
4077 if ((setmode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
4078 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0;
4079 }
4080 if ((setmode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
4081 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0;
4082 }
4083
4084 /* Some uaudio devices are unidirectional. Don't try to find a
4085 matching mode for the unsupported direction. */
4086 setmode &= sc->sc_mode;
4087
4088 if ((setmode & AUMODE_PLAY)) {
4089 paltidx = audio_indexof_format(sc->sc_formats, sc->sc_nformats,
4090 AUMODE_PLAY, play);
4091 /* Transfer should have halted */
4092 uaudio_chan_init(&sc->sc_playchan, paltidx, play,
4093 UGETW(sc->sc_alts[paltidx].edesc->wMaxPacketSize), false);
4094 }
4095 if ((setmode & AUMODE_RECORD)) {
4096 raltidx = audio_indexof_format(sc->sc_formats, sc->sc_nformats,
4097 AUMODE_RECORD, rec);
4098 /* Transfer should have halted */
4099 uaudio_chan_init(&sc->sc_recchan, raltidx, rec,
4100 UGETW(sc->sc_alts[raltidx].edesc->wMaxPacketSize), true);
4101 }
4102
4103 if ((setmode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
4104 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1;
4105 }
4106 if ((setmode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
4107 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1;
4108 }
4109
4110 DPRINTF("use altidx=p%d/r%d, altno=p%d/r%d\n",
4111 sc->sc_playchan.altidx, sc->sc_recchan.altidx,
4112 (sc->sc_playchan.altidx >= 0)
4113 ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting
4114 : -1,
4115 (sc->sc_recchan.altidx >= 0)
4116 ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting
4117 : -1);
4118
4119 return 0;
4120 }
4121
4122 Static usbd_status
4123 uaudio_speed(struct uaudio_softc *sc, int endpt, int clkid,
4124 uint8_t *data, int set)
4125 {
4126 usb_device_request_t req;
4127
4128 switch (sc->sc_version) {
4129 case UAUDIO_VERSION1:
4130 req.bmRequestType = set ?
4131 UT_WRITE_CLASS_ENDPOINT
4132 : UT_READ_CLASS_ENDPOINT;
4133 req.bRequest = set ?
4134 SET_CUR
4135 : GET_CUR;
4136 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
4137 USETW(req.wIndex, endpt);
4138 USETW(req.wLength, 3);
4139 break;
4140 case UAUDIO_VERSION2:
4141 req.bmRequestType = set ?
4142 UT_WRITE_CLASS_INTERFACE
4143 : UT_READ_CLASS_INTERFACE;
4144 req.bRequest = V2_CUR;
4145 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
4146 USETW2(req.wIndex, clkid, sc->sc_ac_iface);
4147 USETW(req.wLength, 4);
4148 break;
4149 }
4150
4151 return usbd_do_request(sc->sc_udev, &req, data);
4152 }
4153
4154 Static usbd_status
4155 uaudio_set_speed(struct uaudio_softc *sc, int endpt, int clkid, u_int speed)
4156 {
4157 uint8_t data[4];
4158
4159 DPRINTFN(5, "endpt=%d clkid=%u speed=%u\n", endpt, clkid, speed);
4160
4161 data[0] = speed;
4162 data[1] = speed >> 8;
4163 data[2] = speed >> 16;
4164 data[3] = speed >> 24;
4165
4166 return uaudio_speed(sc, endpt, clkid, data, 1);
4167 }
4168
4169 #ifdef UAUDIO_DEBUG
4170 SYSCTL_SETUP(sysctl_hw_uaudio_setup, "sysctl hw.uaudio setup")
4171 {
4172 int err;
4173 const struct sysctlnode *rnode;
4174 const struct sysctlnode *cnode;
4175
4176 err = sysctl_createv(clog, 0, NULL, &rnode,
4177 CTLFLAG_PERMANENT, CTLTYPE_NODE, "uaudio",
4178 SYSCTL_DESCR("uaudio global controls"),
4179 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
4180
4181 if (err)
4182 goto fail;
4183
4184 /* control debugging printfs */
4185 err = sysctl_createv(clog, 0, &rnode, &cnode,
4186 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
4187 "debug", SYSCTL_DESCR("Enable debugging output"),
4188 NULL, 0, &uaudiodebug, sizeof(uaudiodebug), CTL_CREATE, CTL_EOL);
4189 if (err)
4190 goto fail;
4191
4192 return;
4193 fail:
4194 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
4195 }
4196 #endif
4197
4198 #ifdef _MODULE
4199
4200 MODULE(MODULE_CLASS_DRIVER, uaudio, NULL);
4201
4202 static const struct cfiattrdata audiobuscf_iattrdata = {
4203 "audiobus", 0, { { NULL, NULL, 0 }, }
4204 };
4205 static const struct cfiattrdata * const uaudio_attrs[] = {
4206 &audiobuscf_iattrdata, NULL
4207 };
4208 CFDRIVER_DECL(uaudio, DV_DULL, uaudio_attrs);
4209 extern struct cfattach uaudio_ca;
4210 static int uaudioloc[6/*USBIFIFCF_NLOCS*/] = {
4211 -1/*USBIFIFCF_PORT_DEFAULT*/,
4212 -1/*USBIFIFCF_CONFIGURATION_DEFAULT*/,
4213 -1/*USBIFIFCF_INTERFACE_DEFAULT*/,
4214 -1/*USBIFIFCF_VENDOR_DEFAULT*/,
4215 -1/*USBIFIFCF_PRODUCT_DEFAULT*/,
4216 -1/*USBIFIFCF_RELEASE_DEFAULT*/};
4217 static struct cfparent uhubparent = {
4218 "usbifif", NULL, DVUNIT_ANY
4219 };
4220 static struct cfdata uaudio_cfdata[] = {
4221 {
4222 .cf_name = "uaudio",
4223 .cf_atname = "uaudio",
4224 .cf_unit = 0,
4225 .cf_fstate = FSTATE_STAR,
4226 .cf_loc = uaudioloc,
4227 .cf_flags = 0,
4228 .cf_pspec = &uhubparent,
4229 },
4230 { NULL }
4231 };
4232
4233 static int
4234 uaudio_modcmd(modcmd_t cmd, void *arg)
4235 {
4236 int err;
4237
4238 switch (cmd) {
4239 case MODULE_CMD_INIT:
4240 err = config_cfdriver_attach(&uaudio_cd);
4241 if (err) {
4242 return err;
4243 }
4244 err = config_cfattach_attach("uaudio", &uaudio_ca);
4245 if (err) {
4246 config_cfdriver_detach(&uaudio_cd);
4247 return err;
4248 }
4249 err = config_cfdata_attach(uaudio_cfdata, 1);
4250 if (err) {
4251 config_cfattach_detach("uaudio", &uaudio_ca);
4252 config_cfdriver_detach(&uaudio_cd);
4253 return err;
4254 }
4255 return 0;
4256 case MODULE_CMD_FINI:
4257 err = config_cfdata_detach(uaudio_cfdata);
4258 if (err)
4259 return err;
4260 config_cfattach_detach("uaudio", &uaudio_ca);
4261 config_cfdriver_detach(&uaudio_cd);
4262 return 0;
4263 default:
4264 return ENOTTY;
4265 }
4266 }
4267
4268 #endif
4269