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