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