Bug Summary

File:dev/usb/uaudio.c
Warning:line 2518, column 9
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name uaudio.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/dev/usb/uaudio.c
1/* $OpenBSD: uaudio.c,v 1.174 2023/12/10 06:32:14 ratchov Exp $ */
2/*
3 * Copyright (c) 2018 Alexandre Ratchov <alex@caoua.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17/*
18 * The USB Audio Class (UAC) defines what is an audio device and how
19 * to use it. There are two versions of the UAC: v1.0 and v2.0. They
20 * are not compatible with each other but they are close enough to
21 * attempt to have the same driver for both.
22 *
23 */
24#include <sys/param.h>
25#include <sys/types.h>
26#include <sys/device.h>
27#include <sys/errno.h>
28#include <sys/fcntl.h>
29#include <sys/malloc.h>
30#include <sys/systm.h>
31#include <sys/time.h>
32#include <sys/audioio.h>
33#include <machine/bus.h>
34#include <dev/audio_if.h>
35#include <dev/usb/usb.h>
36#include <dev/usb/usbdi.h>
37#include <dev/usb/usbdivar.h>
38#include <dev/usb/usb_mem.h>
39
40#ifdef UAUDIO_DEBUG
41#define DPRINTF(...)do {} while(0) \
42 do { \
43 if (uaudio_debug) \
44 printf(__VA_ARGS__); \
45 } while (0)
46#else
47#define DPRINTF(...)do {} while(0) do {} while(0)
48#endif
49
50#define DEVNAME(sc)((sc)->dev.dv_xname) ((sc)->dev.dv_xname)
51
52/*
53 * Isochronous endpoint usage (XXX: these belong to dev/usb/usb.h).
54 */
55#define UE_ISO_USAGE0x30 0x30
56#define UE_ISO_USAGE_DATA0x00 0x00
57#define UE_ISO_USAGE_FEEDBACK0x10 0x10
58#define UE_ISO_USAGE_IMPL0x20 0x20
59#define UE_GET_ISO_USAGE(a)((a) & 0x30) ((a) & UE_ISO_USAGE0x30)
60
61/*
62 * Max length of unit names
63 */
64#define UAUDIO_NAMEMAX16 MAX_AUDIO_DEV_LEN16
65
66/*
67 * USB audio class versions
68 */
69#define UAUDIO_V10x100 0x100
70#define UAUDIO_V20x200 0x200
71
72/*
73 * AC class-specific descriptor interface sub-type
74 */
75#define UAUDIO_AC_HEADER0x1 0x1
76#define UAUDIO_AC_INPUT0x2 0x2
77#define UAUDIO_AC_OUTPUT0x3 0x3
78#define UAUDIO_AC_MIXER0x4 0x4
79#define UAUDIO_AC_SELECTOR0x5 0x5
80#define UAUDIO_AC_FEATURE0x6 0x6
81#define UAUDIO_AC_EFFECT0x7 0x7
82#define UAUDIO_AC_PROCESSING0x8 0x8
83#define UAUDIO_AC_EXTENSION0x9 0x9
84#define UAUDIO_AC_CLKSRC0xa 0xa
85#define UAUDIO_AC_CLKSEL0xb 0xb
86#define UAUDIO_AC_CLKMULT0xc 0xc
87#define UAUDIO_AC_RATECONV0xd 0xd
88
89/*
90 * AS class-specific interface sub-types
91 */
92#define UAUDIO_AS_GENERAL0x1 0x1
93#define UAUDIO_AS_FORMAT0x2 0x2
94
95/*
96 * AS class-specific endpoint sub-type
97 */
98#define UAUDIO_EP_GENERAL0x1 0x1
99
100/*
101 * UAC v1 formats, wFormatTag is an enum
102 */
103#define UAUDIO_V1_FMT_PCM0x1 0x1
104#define UAUDIO_V1_FMT_PCM80x2 0x2
105#define UAUDIO_V1_FMT_FLOAT0x3 0x3
106#define UAUDIO_V1_FMT_ALAW0x4 0x4
107#define UAUDIO_V1_FMT_MULAW0x5 0x5
108
109/*
110 * UAC v2 formats, bmFormats is a bitmap
111 */
112#define UAUDIO_V2_FMT_PCM0x01 0x01
113#define UAUDIO_V2_FMT_PCM80x02 0x02
114#define UAUDIO_V2_FMT_FLOAT0x04 0x04
115#define UAUDIO_V2_FMT_ALAW0x08 0x08
116#define UAUDIO_V2_FMT_MULAW0x10 0x10
117
118/*
119 * AC requests
120 */
121#define UAUDIO_V1_REQ_SET_CUR0x01 0x01
122#define UAUDIO_V1_REQ_SET_MIN0x02 0x02
123#define UAUDIO_V1_REQ_SET_MAX0x03 0x03
124#define UAUDIO_V1_REQ_SET_RES0x04 0x04
125#define UAUDIO_V1_REQ_GET_CUR0x81 0x81
126#define UAUDIO_V1_REQ_GET_MIN0x82 0x82
127#define UAUDIO_V1_REQ_GET_MAX0x83 0x83
128#define UAUDIO_V1_REQ_GET_RES0x84 0x84
129#define UAUDIO_V2_REQ_CUR1 1
130#define UAUDIO_V2_REQ_RANGES2 2
131
132/*
133 * AC request "selector control"
134 */
135#define UAUDIO_V2_REQSEL_CLKFREQ1 1
136#define UAUDIO_V2_REQSEL_CLKSEL1 1
137
138/*
139 * AS class-specific endpoint attributes
140 */
141#define UAUDIO_EP_FREQCTL0x01 0x01
142
143/*
144 * AC feature control selectors (aka wValue in the request)
145 */
146#define UAUDIO_REQSEL_MUTE0x01 0x01
147#define UAUDIO_REQSEL_VOLUME0x02 0x02
148#define UAUDIO_REQSEL_BASS0x03 0x03
149#define UAUDIO_REQSEL_MID0x04 0x04
150#define UAUDIO_REQSEL_TREBLE0x05 0x05
151#define UAUDIO_REQSEL_EQ0x06 0x06
152#define UAUDIO_REQSEL_AGC0x07 0x07
153#define UAUDIO_REQSEL_DELAY0x08 0x08
154#define UAUDIO_REQSEL_BASSBOOST0x09 0x09
155#define UAUDIO_REQSEL_LOUDNESS0x0a 0x0a
156#define UAUDIO_REQSEL_GAIN0x0b 0x0b
157#define UAUDIO_REQSEL_GAINPAD0x0c 0x0c
158#define UAUDIO_REQSEL_PHASEINV0x0d 0x0d
159
160/*
161 * Endpoint (UAC v1) or clock-source unit (UAC v2) sample rate control
162 */
163#define UAUDIO_REQSEL_RATE0x01 0x01
164
165/*
166 * Samples-per-frame are fractions. UAC v2.0 requires the denominator to
167 * be multiple of 2^16, as used in the sync pipe. On the other hand, to
168 * represent sample-per-frame of all rates we support, we need the
169 * denominator to be such that (rate / 1000) can be represented exactly,
170 * 80 works. So we use the least common multiplier of both.
171 */
172#define UAUDIO_SPF_DIV327680 327680
173
174/*
175 * names of DAC and ADC unit names
176 */
177#define UAUDIO_NAME_PLAY"dac" "dac"
178#define UAUDIO_NAME_REC"record" "record"
179
180/*
181 * read/write pointers for secure sequential access of binary data,
182 * ex. usb descriptors, tables and alike. Bytes are read using the
183 * read pointer up to the write pointer.
184 */
185struct uaudio_blob {
186 unsigned char *rptr, *wptr;
187};
188
189/*
190 * Ranges of integer values used to represent controls values and
191 * sample frequencies.
192 */
193struct uaudio_ranges {
194 unsigned int nval;
195 struct uaudio_ranges_el {
196 struct uaudio_ranges_el *next;
197 int min, max, res;
198 } *el;
199};
200
201struct uaudio_softc {
202 struct device dev;
203 struct usbd_device *udev;
204 int version;
205
206 /*
207 * UAC exposes the device as a circuit of units. Input and
208 * output jacks are known as terminal units, others are
209 * processing units. The purpose of this driver is to give
210 * them reasonable names and expose them as mixer(1)
211 * controls. Control names are derived from the type of the
212 * unit and its role in the circuit.
213 *
214 * UAC v2.0 exposes also the clock circuitry using units, so
215 * selecting the sample rate also involves units usage.
216 */
217 struct uaudio_unit {
218 struct uaudio_unit *unit_next, *src_next, *dst_next;
219 struct uaudio_unit *src_list, *dst_list;
220 char name[UAUDIO_NAMEMAX16];
221 unsigned int nch;
222 int type, id;
223
224 /* terminal or clock type */
225 unsigned int term;
226
227 /* clock source, if a terminal or selector */
228 struct uaudio_unit *clock;
229
230 /* sample rates, if this is a clock source */
231 struct uaudio_ranges rates;
232
233 /* mixer(4) bits */
234#define UAUDIO_CLASS_OUT0 0
235#define UAUDIO_CLASS_IN1 1
236#define UAUDIO_CLASS_COUNT2 2
237 int mixer_class;
238 struct uaudio_mixent {
239 struct uaudio_mixent *next;
240 char *fname;
241#define UAUDIO_MIX_SW0 0
242#define UAUDIO_MIX_NUM1 1
243#define UAUDIO_MIX_ENUM2 2
244 int type;
245 int chan;
246 int req_sel;
247 struct uaudio_ranges ranges;
248 } *mixent_list;
249 } *unit_list;
250
251 /*
252 * Current clock, UAC v2.0 only
253 */
254 struct uaudio_unit *clock;
255
256 /*
257 * Number of input and output terminals
258 */
259 unsigned int nin, nout;
260
261 /*
262 * When unique names are needed, they are generated using a
263 * base string suffixed with a number. Ex. "spkr5". The
264 * following structure is used to keep track of strings we
265 * allocated.
266 */
267 struct uaudio_name {
268 struct uaudio_name *next;
269 char *templ;
270 unsigned int unit;
271 } *names;
272
273 /*
274 * Audio streaming (AS) alternate settings, i.e. stream format
275 * and USB-related parameters to use it.
276 */
277 struct uaudio_alt {
278 struct uaudio_alt *next;
279 int ifnum, altnum;
280 int mode; /* one of AUMODE_{RECORD,PLAY} */
281 int data_addr; /* data endpoint address */
282 int sync_addr; /* feedback endpoint address */
283 int maxpkt; /* max supported bytes per frame */
284 int fps; /* USB (micro-)frames per second */
285 int bps, bits, nch; /* audio encoding */
286 int v1_rates; /* if UAC 1.0, bitmap of rates */
287 } *alts;
288
289 /*
290 * Audio parameters: play and record stream formats usable
291 * together.
292 */
293 struct uaudio_params {
294 struct uaudio_params *next;
295 struct uaudio_alt *palt, *ralt;
296 int v1_rates;
297 } *params_list, *params;
298
299 /*
300 * One direction audio stream, aka "DMA" in progress
301 */
302 struct uaudio_stream {
303#define UAUDIO_NXFERS_MIN2 2
304#define UAUDIO_NXFERS_MAX8 8
305 struct uaudio_xfer {
306 struct usbd_xfer *usb_xfer;
307 unsigned char *buf;
308 uint16_t *sizes;
309 unsigned int size; /* bytes requested */
310 unsigned int nframes; /* frames requested */
311 } data_xfers[UAUDIO_NXFERS_MAX8], sync_xfers[UAUDIO_NXFERS_MAX8];
312
313 /*
314 * We don't use all the data_xfers[] entries because
315 * we can't schedule too many frames in the usb
316 * controller.
317 */
318 unsigned int nxfers;
319
320 unsigned int spf_remain; /* frac sample left */
321 unsigned int spf; /* avg samples per frame */
322 unsigned int spf_min, spf_max; /* allowed boundaries */
323
324 /*
325 * The max frame size we'll need (which may be lower
326 * than the maxpkt the usb pipe supports).
327 */
328 unsigned int maxpkt;
329
330 /*
331 * max number of frames per xfer we'll need
332 */
333 unsigned int nframes_max;
334
335 /*
336 * At usb2.0 speed, the number of (micro-)frames per
337 * transfer must correspond to 1ms, which is the usb1.1
338 * frame duration. This is required by lower level usb
339 * drivers.
340 *
341 * The nframes_mask variable is used to test if the
342 * number of frames per transfer is usable (by checking
343 * that least significant bits are zero). For instance,
344 * nframes_mask will be set to 0x0 on usb1.1 device and
345 * 0x7 on usb2.0 devices running at 8000 fps.
346 */
347 unsigned int nframes_mask;
348
349 unsigned int data_nextxfer, sync_nextxfer;
350 struct usbd_pipe *data_pipe;
351 struct usbd_pipe *sync_pipe;
352 void (*intr)(void *);
353 void *arg;
354
355 /* audio ring extents, passed to trigger() methods */
356 unsigned char *ring_start, *ring_end;
357
358 /* pointer to first byte available */
359 unsigned char *ring_pos;
360
361 /* audio(9) block size in bytes */
362 int ring_blksz;
363
364 /* xfer position relative to block boundary */
365 int ring_offs;
366
367 /*
368 * As USB sample-per-frame is not constant, we must
369 * schedule transfers slightly larger that one audio
370 * block. This is the "safe" block size, that ensures
371 * the transfer will cross the audio block boundary.
372 */
373 int safe_blksz;
374
375 /*
376 * Number of bytes completed, when it reaches a
377 * block size, we fire an audio(9) interrupt.
378 */
379 int ring_icnt;
380
381 /*
382 * USB transfers are used as a FIFO which is the
383 * concatenation of all transfers. This is the write
384 * (read) position of the play (rec) stream
385 */
386 unsigned int ubuf_xfer; /* xfer index */
387 unsigned int ubuf_pos; /* offset in bytes */
388 } pstream, rstream;
389
390 int ctl_ifnum; /* aka AC interface */
391
392 int mode; /* open() mode */
393 int trigger_mode; /* trigger() mode */
394
395 unsigned int rate; /* current sample rate */
396 unsigned int ufps; /* USB frames per second */
397 unsigned int sync_pktsz; /* size of sync packet */
398 unsigned int host_nframes; /* max frames we can schedule */
399
400 int diff_nsamp; /* samples play is ahead of rec */
401 int diff_nframes; /* frames play is ahead of rec */
402 unsigned int adjspf_age; /* frames since last uaudio_adjspf */
403
404 /*
405 * bytes pending to be copied to transfer buffer. This is play
406 * only, as recorded frames are copied as soon they are
407 * received.
408 */
409 size_t copy_todo;
410};
411
412int uaudio_match(struct device *, void *, void *);
413void uaudio_attach(struct device *, struct device *, void *);
414int uaudio_detach(struct device *, int);
415
416int uaudio_open(void *, int);
417void uaudio_close(void *);
418int uaudio_set_params(void *, int, int, struct audio_params *,
419 struct audio_params *);
420unsigned int uaudio_set_blksz(void *, int,
421 struct audio_params *, struct audio_params *, unsigned int);
422int uaudio_trigger_output(void *, void *, void *, int,
423 void (*)(void *), void *, struct audio_params *);
424int uaudio_trigger_input(void *, void *, void *, int,
425 void (*)(void *), void *, struct audio_params *);
426void uaudio_copy_output(void *, size_t);
427void uaudio_underrun(void *);
428int uaudio_halt_output(void *);
429int uaudio_halt_input(void *);
430int uaudio_query_devinfo(void *, struct mixer_devinfo *);
431int uaudio_get_port(void *, struct mixer_ctrl *);
432int uaudio_set_port(void *, struct mixer_ctrl *);
433
434int uaudio_process_unit(struct uaudio_softc *,
435 struct uaudio_unit *, int,
436 struct uaudio_blob,
437 struct uaudio_unit **);
438
439void uaudio_pdata_intr(struct usbd_xfer *, void *, usbd_status);
440void uaudio_rdata_intr(struct usbd_xfer *, void *, usbd_status);
441void uaudio_psync_intr(struct usbd_xfer *, void *, usbd_status);
442
443#ifdef UAUDIO_DEBUG
444char *uaudio_isoname(int isotype);
445char *uaudio_modename(int mode);
446char *uaudio_usagename(int usage);
447void uaudio_rates_print(int rates);
448void uaudio_ranges_print(struct uaudio_ranges *r);
449void uaudio_print_unit(struct uaudio_softc *sc, struct uaudio_unit *u);
450void uaudio_mixer_print(struct uaudio_softc *sc);
451void uaudio_conf_print(struct uaudio_softc *sc);
452
453/*
454 * 0 - nothing, same as if UAUDIO_DEBUG isn't defined
455 * 1 - initialisations & setup
456 * 2 - audio(4) calls
457 * 3 - transfers
458 */
459int uaudio_debug = 1;
460#endif
461
462struct cfdriver uaudio_cd = {
463 NULL((void *)0), "uaudio", DV_DULL
464};
465
466const struct cfattach uaudio_ca = {
467 sizeof(struct uaudio_softc), uaudio_match, uaudio_attach, uaudio_detach
468};
469
470const struct audio_hw_if uaudio_hw_if = {
471 .open = uaudio_open,
472 .close = uaudio_close,
473 .set_params = uaudio_set_params,
474 .halt_output = uaudio_halt_output,
475 .halt_input = uaudio_halt_input,
476 .set_port = uaudio_set_port,
477 .get_port = uaudio_get_port,
478 .query_devinfo = uaudio_query_devinfo,
479 .trigger_output = uaudio_trigger_output,
480 .trigger_input = uaudio_trigger_input,
481 .copy_output = uaudio_copy_output,
482 .underrun = uaudio_underrun,
483 .set_blksz = uaudio_set_blksz,
484};
485
486/*
487 * To keep things simple, we support only the following rates, we
488 * don't care about continuous sample rates or other "advanced"
489 * features which complicate implementation.
490 */
491const int uaudio_rates[] = {
492 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
493 64000, 88200, 96000, 128000, 176400, 192000
494};
495
496/*
497 * Convert 8, 16, or 24-bit signed value to an int by expanding the
498 * sign bit.
499 */
500int
501uaudio_sign_expand(unsigned int val, int opsize)
502{
503 unsigned int s;
504
505 s = 1 << (8 * opsize - 1);
506 return (val ^ s) - s;
507}
508
509int
510uaudio_req(struct uaudio_softc *sc,
511 unsigned int type,
512 unsigned int req,
513 unsigned int sel,
514 unsigned int chan,
515 unsigned int ifnum,
516 unsigned int id,
517 unsigned char *buf,
518 size_t size)
519{
520 struct usb_device_request r;
521 int err;
522
523 r.bmRequestType = type;
524 r.bRequest = req;
525 USETW(r.wValue, sel << 8 | chan)(*(u_int16_t *)(r.wValue) = (sel << 8 | chan));
526 USETW(r.wIndex, id << 8 | ifnum)(*(u_int16_t *)(r.wIndex) = (id << 8 | ifnum));
527 USETW(r.wLength, size)(*(u_int16_t *)(r.wLength) = (size));
528
529 DPRINTF("%s: type = 0x%x, req = 0x%x, val = 0x%x, "do {} while(0)
530 "index = 0x%x, size = %d\n", __func__,do {} while(0)
531 type, req, UGETW(r.wValue), UGETW(r.wIndex), UGETW(r.wLength))do {} while(0);
532
533 err = usbd_do_request(sc->udev, &r, buf);
534 if (err) {
535 DPRINTF("%s: failed: %s\n", __func__, usbd_errstr(err))do {} while(0);
536 return 0;
537 }
538 return 1;
539}
540
541/*
542 * Read a number of the given size (in bytes) from the given
543 * blob. Return 0 on error.
544 */
545int
546uaudio_getnum(struct uaudio_blob *p, unsigned int size, unsigned int *ret)
547{
548 unsigned int i, num = 0;
549
550 if (p->wptr - p->rptr < size) {
551 DPRINTF("%s: %d: too small\n", __func__, size)do {} while(0);
552 return 0;
553 }
554
555 for (i = 0; i < size; i++)
556 num |= *p->rptr++ << (8 * i);
557
558 if (ret)
559 *ret = num;
560 return 1;
561}
562
563/*
564 * Read a USB descriptor from the given blob. Return 0 on error.
565 */
566int
567uaudio_getdesc(struct uaudio_blob *p, struct uaudio_blob *ret)
568{
569 unsigned int size;
570
571 if (!uaudio_getnum(p, 1, &size))
572 return 0;
573 if (size-- == 0) {
574 DPRINTF("%s: zero sized desc\n", __func__)do {} while(0);
575 return 0;
576 }
577 if (p->wptr - p->rptr < size) {
578 DPRINTF("%s: too small\n", __func__)do {} while(0);
579 return 0;
580 }
581 ret->rptr = p->rptr;
582 ret->wptr = p->rptr + size;
583 p->rptr += size;
584 return 1;
585}
586
587/*
588 * Find the unit with the given id, return NULL if not found.
589 */
590struct uaudio_unit *
591uaudio_unit_byid(struct uaudio_softc *sc, unsigned int id)
592{
593 struct uaudio_unit *u;
594
595 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
596 if (u->id == id)
597 break;
598 }
599 return u;
600}
601
602/*
603 * Return a terminal name for the given terminal type.
604 */
605char *
606uaudio_tname(struct uaudio_softc *sc, unsigned int type, int isout)
607{
608 unsigned int hi, lo;
609 char *name;
610
611 hi = type >> 8;
612 lo = type & 0xff;
613
614 /* usb data stream */
615 if (hi == 1)
616 return isout ? UAUDIO_NAME_REC"record" : UAUDIO_NAME_PLAY"dac";
617
618 /* if there is only one input (output) use "input" ("output") */
619 if (isout) {
620 if (sc->nout == 1)
621 return "output";
622 } else {
623 if (sc->nin == 1)
624 return "input";
625 }
626
627 /* determine name from USB terminal type */
628 switch (hi) {
629 case 2:
630 /* embedded inputs */
631 name = isout ? "mic-out" : "mic";
632 break;
633 case 3:
634 /* embedded outputs, mostly speakers, except 0x302 */
635 switch (lo) {
636 case 0x02:
637 name = isout ? "hp" : "hp-in";
638 break;
639 default:
640 name = isout ? "spkr" : "spkr-in";
641 break;
642 }
643 break;
644 case 4:
645 /* handsets and headset */
646 name = isout ? "spkr" : "mic";
647 break;
648 case 5:
649 /* phone line */
650 name = isout ? "phone-in" : "phone-out";
651 break;
652 case 6:
653 /* external sources/sinks */
654 switch (lo) {
655 case 0x02:
656 case 0x05:
657 case 0x06:
658 case 0x07:
659 case 0x09:
660 case 0x0a:
661 name = isout ? "dig-out" : "dig-in";
662 break;
663 default:
664 name = isout ? "line-out" : "line-in";
665 break;
666 }
667 break;
668 case 7:
669 /* internal devices */
670 name = isout ? "int-out" : "int-in";
671 break;
672 default:
673 name = isout ? "unk-out" : "unk-in";
674 }
675 return name;
676}
677
678/*
679 * Return a clock name for the given clock type.
680 */
681char *
682uaudio_clkname(unsigned int attr)
683{
684 static char *names[] = {"ext", "fixed", "var", "prog"};
685
686 return names[attr & 3];
687}
688
689/*
690 * Return an unique name for the given template.
691 */
692void
693uaudio_mkname(struct uaudio_softc *sc, char *templ, char *res)
694{
695 struct uaudio_name *n;
696 char *sep;
697
698 /*
699 * if this is not a terminal name (i.e. there's a underscore
700 * in the name, like in "spkr2_mic3"), then use underscore as
701 * separator to avoid concatenating two numbers
702 */
703 sep = strchr(templ, '_') != NULL((void *)0) ? "_" : "";
704
705 n = sc->names;
706 while (1) {
707 if (n == NULL((void *)0)) {
708 n = malloc(sizeof(struct uaudio_name),
709 M_USBDEV102, M_WAITOK0x0001);
710 n->templ = templ;
711 n->unit = 0;
712 n->next = sc->names;
713 sc->names = n;
714 }
715 if (strcmp(n->templ, templ) == 0)
716 break;
717 n = n->next;
718 }
719 if (n->unit == 0)
720 snprintf(res, UAUDIO_NAMEMAX16, "%s", templ);
721 else
722 snprintf(res, UAUDIO_NAMEMAX16, "%s%s%u", templ, sep, n->unit);
723 n->unit++;
724}
725
726/*
727 * Convert UAC v1.0 feature bitmap to UAC v2.0 feature bitmap.
728 */
729unsigned int
730uaudio_feature_fixup(struct uaudio_softc *sc, unsigned int ctl)
731{
732 int i;
733 unsigned int bits, n;
734
735 switch (sc->version) {
736 case UAUDIO_V10x100:
737 n = 0;
738 for (i = 0; i < 16; i++) {
739 bits = (ctl >> i) & 1;
740 if (bits)
741 bits |= 2;
742 n |= bits << (2 * i);
743 }
744 return n;
745 case UAUDIO_V20x200:
746 break;
747 }
748 return ctl;
749}
750
751/*
752 * Initialize a uaudio_ranges to the empty set
753 */
754void
755uaudio_ranges_init(struct uaudio_ranges *r)
756{
757 r->el = NULL((void *)0);
758 r->nval = 0;
759}
760
761/*
762 * Add the given range to the uaudio_ranges structures. Ranges are
763 * not supposed to overlap (required by USB spec). If they do we just
764 * return.
765 */
766void
767uaudio_ranges_add(struct uaudio_ranges *r, int min, int max, int res)
768{
769 struct uaudio_ranges_el *e, **pe;
770
771 if (min > max) {
772 DPRINTF("%s: [%d:%d]/%d: bad range\n", __func__,do {} while(0)
773 min, max, res)do {} while(0);
774 return;
775 }
776
777 for (pe = &r->el; (e = *pe) != NULL((void *)0); pe = &e->next) {
778 if (min <= e->max && max >= e->min) {
779 DPRINTF("%s: overlapping ranges\n", __func__)do {} while(0);
780 return;
781 }
782 if (min < e->max)
783 break;
784 }
785
786 /* XXX: use 'res' here */
787 r->nval += max - min + 1;
788
789 e = malloc(sizeof(struct uaudio_ranges_el), M_USBDEV102, M_WAITOK0x0001);
790 e->min = min;
791 e->max = max;
792 e->res = res;
793 e->next = *pe;
794 *pe = e;
795}
796
797/*
798 * Free all ranges making the uaudio_ranges the empty set
799 */
800void
801uaudio_ranges_clear(struct uaudio_ranges *r)
802{
803 struct uaudio_ranges_el *e;
804
805 while ((e = r->el) != NULL((void *)0)) {
806 r->el = e->next;
807 free(e, M_USBDEV102, sizeof(struct uaudio_ranges_el));
808 }
809 r->nval = 0;
810}
811
812/*
813 * Convert a value in the given uaudio_ranges, into a 0..255 integer
814 * suitable for mixer usage
815 */
816int
817uaudio_ranges_decode(struct uaudio_ranges *r, int val)
818{
819 struct uaudio_ranges_el *e;
820 int diff, pos;
821
822 pos = 0;
823
824 for (e = r->el; e != NULL((void *)0); e = e->next) {
825 if (val >= e->min && val <= e->max) {
826 pos += val - e->min;
827 return (r->nval == 1) ? 0 :
828 (pos * 255 + (r->nval - 1) / 2) / (r->nval - 1);
829 }
830 diff = e->max - e->min + 1;
831 pos += diff;
832 }
833 return 0;
834}
835
836/*
837 * Convert a 0..255 to a value in the uaudio_ranges suitable for a USB
838 * request.
839 */
840unsigned int
841uaudio_ranges_encode(struct uaudio_ranges *r, int val)
842{
843 struct uaudio_ranges_el *e;
844 int diff, pos;
845
846 pos = (val * (r->nval - 1) + 127) / 255;
847
848 for (e = r->el; e != NULL((void *)0); e = e->next) {
849 diff = e->max - e->min + 1;
850 if (pos < diff)
851 return e->min + pos;
852 pos -= diff;
853 }
854 return 0;
855}
856
857/*
858 * Return the bitmap of supported rates included in the given ranges.
859 * This is not a mixer thing, UAC v2.0 uses ranges to report sample
860 * rates.
861 */
862int
863uaudio_ranges_getrates(struct uaudio_ranges *r,
864 unsigned int mult, unsigned int div)
865{
866 struct uaudio_ranges_el *e;
867 int rates, i, v;
868
869 rates = 0;
870
871 for (e = r->el; e != NULL((void *)0); e = e->next) {
872 for (i = 0; i < nitems(uaudio_rates)(sizeof((uaudio_rates)) / sizeof((uaudio_rates)[0])); i++) {
873 v = (unsigned long long)uaudio_rates[i] * mult / div;
874 if (v < e->min || v > e->max)
875 continue;
876 if (e->res == 0 || v - e->min % e->res == 0)
877 rates |= 1 << i;
878 }
879 }
880
881 return rates;
882}
883
884/*
885 * Return the index in the uaudio_rates[] array of rate closest to the
886 * given rate in Hz.
887 */
888int
889uaudio_rates_indexof(int mask, int rate)
890{
891 int i, diff, best_index, best_diff;
892
893 best_index = -1;
894 best_diff = INT_MAX0x7fffffff;
895 for (i = 0; i < nitems(uaudio_rates)(sizeof((uaudio_rates)) / sizeof((uaudio_rates)[0])); i++) {
896 if ((mask & (1 << i)) == 0)
897 continue;
898 diff = uaudio_rates[i] - rate;
899 if (diff < 0)
900 diff = -diff;
901 if (diff < best_diff) {
902 best_index = i;
903 best_diff = diff;
904 }
905 }
906 return best_index;
907}
908
909/*
910 * Do a request that results in a uaudio_ranges. On UAC v1.0, this is
911 * simply a min/max/res triplet. On UAC v2.0, this is an array of
912 * min/max/res triplets.
913 */
914int
915uaudio_req_ranges(struct uaudio_softc *sc,
916 unsigned int opsize,
917 unsigned int sel,
918 unsigned int chan,
919 unsigned int ifnum,
920 unsigned int id,
921 struct uaudio_ranges *r)
922{
923 unsigned char req_buf[16], *req = NULL((void *)0);
924 size_t req_size;
925 struct uaudio_blob p;
926 unsigned int count, min, max, res;
927 int i;
928
929 switch (sc->version) {
930 case UAUDIO_V10x100:
931 count = 1;
932 req = req_buf;
933 p.rptr = p.wptr = req;
934 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
935 UAUDIO_V1_REQ_GET_MIN0x82, sel, chan,
936 ifnum, id, p.wptr, opsize))
937 return 0;
938 p.wptr += opsize;
939 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
940 UAUDIO_V1_REQ_GET_MAX0x83, sel, chan,
941 ifnum, id, p.wptr, opsize))
942 return 0;
943 p.wptr += opsize;
944 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
945 UAUDIO_V1_REQ_GET_RES0x84, sel, chan,
946 ifnum, id, p.wptr, opsize))
947 return 0;
948 p.wptr += opsize;
949 break;
950 case UAUDIO_V20x200:
951 /* fetch the ranges count only (first 2 bytes) */
952 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
953 UAUDIO_V2_REQ_RANGES2, sel, chan,
954 ifnum, id, req_buf, 2))
955 return 0;
956
957 /* count is at most 65535 */
958 count = req_buf[0] | req_buf[1] << 8;
959
960 /* restart the request on a large enough buffer */
961 req_size = 2 + 3 * opsize * count;
962 if (sizeof(req_buf) >= req_size)
963 req = req_buf;
964 else
965 req = malloc(req_size, M_USBDEV102, M_WAITOK0x0001);
966
967 p.rptr = p.wptr = req;
968 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
969 UAUDIO_V2_REQ_RANGES2, sel, chan,
970 ifnum, id, p.wptr, req_size))
971 return 0;
972 p.wptr += req_size;
973
974 /* skip initial 2 bytes of count */
975 p.rptr += 2;
976 break;
977 }
978
979 for (i = 0; i < count; i++) {
980 if (!uaudio_getnum(&p, opsize, &min))
981 return 0;
982 if (!uaudio_getnum(&p, opsize, &max))
983 return 0;
984 if (!uaudio_getnum(&p, opsize, &res))
985 return 0;
986 uaudio_ranges_add(r,
987 uaudio_sign_expand(min, opsize),
988 uaudio_sign_expand(max, opsize),
989 uaudio_sign_expand(res, opsize));
990 }
991
992 if (req != req_buf)
993 free(req, M_USBDEV102, req_size);
994
995 return 1;
996}
997
998/*
999 * Return the rates bitmap of the given interface alt setting
1000 */
1001int
1002uaudio_alt_getrates(struct uaudio_softc *sc, struct uaudio_alt *p)
1003{
1004 struct uaudio_unit *u;
1005 unsigned int mult = 1, div = 1;
1006
1007 switch (sc->version) {
1008 case UAUDIO_V10x100:
1009 return p->v1_rates;
1010 case UAUDIO_V20x200:
1011 u = sc->clock;
1012 while (1) {
1013 switch (u->type) {
1014 case UAUDIO_AC_CLKSRC0xa:
1015 return uaudio_ranges_getrates(&u->rates,
1016 mult, div);
1017 case UAUDIO_AC_CLKSEL0xb:
1018 u = u->clock;
1019 break;
1020 case UAUDIO_AC_CLKMULT0xc:
1021 case UAUDIO_AC_RATECONV0xd:
1022 /* XXX: adjust rate with multiplier */
1023 u = u->src_list;
1024 break;
1025 default:
1026 DPRINTF("%s: no clock\n", __func__)do {} while(0);
1027 return 0;
1028 }
1029 }
1030 }
1031 return 0;
1032}
1033
1034/*
1035 * return the clock unit of the given terminal unit (v2 only)
1036 */
1037int
1038uaudio_clock_id(struct uaudio_softc *sc)
1039{
1040 struct uaudio_unit *u;
1041
1042 u = sc->clock;
1043 while (1) {
1044 if (u == NULL((void *)0)) {
1045 DPRINTF("%s: NULL clock pointer\n", __func__)do {} while(0);
1046 return -1;
1047 }
1048 switch (u->type) {
1049 case UAUDIO_AC_CLKSRC0xa:
1050 return u->id;
1051 case UAUDIO_AC_CLKSEL0xb:
1052 u = u->clock;
1053 break;
1054 case UAUDIO_AC_CLKMULT0xc:
1055 case UAUDIO_AC_RATECONV0xd:
1056 u = u->src_list;
1057 break;
1058 default:
1059 DPRINTF("%s: no clock\n", __func__)do {} while(0);
1060 return -1;
1061 }
1062 }
1063}
1064
1065/*
1066 * Return the rates bitmap of the given parameters setting
1067 */
1068int
1069uaudio_getrates(struct uaudio_softc *sc, struct uaudio_params *p)
1070{
1071 switch (sc->version) {
1072 case UAUDIO_V10x100:
1073 return p->v1_rates;
1074 case UAUDIO_V20x200:
1075 return uaudio_alt_getrates(sc, p->palt ? p->palt : p->ralt);
1076 }
1077 return 0;
1078}
1079
1080/*
1081 * Add the given feature (aka mixer control) to the given unit.
1082 */
1083void
1084uaudio_feature_addent(struct uaudio_softc *sc,
1085 struct uaudio_unit *u, int uac_type, int chan)
1086{
1087 static struct {
1088 char *name;
1089 int mix_type;
1090 int req_sel;
1091 } features[] = {
1092 {"mute", UAUDIO_MIX_SW0, UAUDIO_REQSEL_MUTE0x01},
1093 {"level", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_VOLUME0x02},
1094 {"bass", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_BASS0x03},
1095 {"mid", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_MID0x04},
1096 {"treble", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_TREBLE0x05},
1097 {"eq", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_EQ0x06},
1098 {"agc", UAUDIO_MIX_SW0, UAUDIO_REQSEL_AGC0x07},
1099 {NULL((void *)0), -1, -1}, /* delay */
1100 {"bassboost", UAUDIO_MIX_SW0, UAUDIO_REQSEL_BASSBOOST0x09},
1101 {"loud", UAUDIO_MIX_SW0, UAUDIO_REQSEL_LOUDNESS0x0a},
1102 {"gain", UAUDIO_MIX_NUM1, UAUDIO_REQSEL_GAIN0x0b},
1103 {"gainpad", UAUDIO_MIX_SW0, UAUDIO_REQSEL_GAINPAD0x0c},
1104 {"phase", UAUDIO_MIX_SW0, UAUDIO_REQSEL_PHASEINV0x0d},
1105 {NULL((void *)0), -1, -1}, /* underflow */
1106 {NULL((void *)0), -1, -1} /* overflow */
1107 };
1108 struct uaudio_mixent *m, *i, **pi;
1109 int cmp;
1110
1111 if (uac_type >= sizeof(features) / sizeof(features[0])) {
1112 printf("%s: skipped unknown feature\n", DEVNAME(sc)((sc)->dev.dv_xname));
1113 return;
1114 }
1115
1116 m = malloc(sizeof(struct uaudio_mixent), M_USBDEV102, M_WAITOK0x0001);
1117 m->chan = chan;
1118 m->fname = features[uac_type].name;
1119 m->type = features[uac_type].mix_type;
1120 m->req_sel = features[uac_type].req_sel;
1121 uaudio_ranges_init(&m->ranges);
1122
1123 if (m->type == UAUDIO_MIX_NUM1) {
1124 if (!uaudio_req_ranges(sc, 2,
1125 m->req_sel, chan < 0 ? 0 : chan + 1,
1126 sc->ctl_ifnum, u->id,
1127 &m->ranges)) {
1128 printf("%s: failed to get ranges for %s control\n",
1129 DEVNAME(sc)((sc)->dev.dv_xname), m->fname);
1130 free(m, M_USBDEV102, sizeof(struct uaudio_mixent));
1131 return;
1132 }
1133 if (m->ranges.el == NULL((void *)0)) {
1134 printf("%s: skipped %s control with empty range\n",
1135 DEVNAME(sc)((sc)->dev.dv_xname), m->fname);
1136 free(m, M_USBDEV102, sizeof(struct uaudio_mixent));
1137 return;
1138 }
1139#ifdef UAUDIO_DEBUG
1140 if (uaudio_debug)
1141 uaudio_ranges_print(&m->ranges);
1142#endif
1143 }
1144
1145 /*
1146 * Add to unit's mixer controls list, sorting entries by name
1147 * and increasing channel number.
1148 */
1149 for (pi = &u->mixent_list; (i = *pi) != NULL((void *)0); pi = &i->next) {
1150 cmp = strcmp(i->fname, m->fname);
1151 if (cmp == 0)
1152 cmp = i->chan - m->chan;
1153 if (cmp == 0) {
1154 DPRINTF("%02u: %s.%s: duplicate feature for chan %d\n",do {} while(0)
1155 u->id, u->name, m->fname, m->chan)do {} while(0);
1156 free(m, M_USBDEV102, sizeof(struct uaudio_mixent));
1157 return;
1158 }
1159 if (cmp > 0)
1160 break;
1161 }
1162 m->next = *pi;
1163 *pi = m;
1164
1165 DPRINTF("\t%s[%d]\n", m->fname, m->chan)do {} while(0);
1166}
1167
1168/*
1169 * For the given unit, parse the list of its sources and recursively
1170 * call uaudio_process_unit() for each.
1171 */
1172int
1173uaudio_process_srcs(struct uaudio_softc *sc,
1174 struct uaudio_unit *u, struct uaudio_blob units,
1175 struct uaudio_blob *p)
1176{
1177 struct uaudio_unit *s, **ps;
1178 unsigned int i, npin, sid;
1179
1180 if (!uaudio_getnum(p, 1, &npin))
1181 return 0;
1182 ps = &u->src_list;
1183 for (i = 0; i < npin; i++) {
1184 if (!uaudio_getnum(p, 1, &sid))
1185 return 0;
1186 if (!uaudio_process_unit(sc, u, sid, units, &s))
1187 return 0;
1188 s->src_next = NULL((void *)0);
1189 *ps = s;
1190 ps = &s->src_next;
1191 }
1192 return 1;
1193}
1194
1195/*
1196 * Parse the number of channels.
1197 */
1198int
1199uaudio_process_nch(struct uaudio_softc *sc,
1200 struct uaudio_unit *u, struct uaudio_blob *p)
1201{
1202 if (!uaudio_getnum(p, 1, &u->nch))
1203 return 0;
1204 /* skip junk */
1205 switch (sc->version) {
1206 case UAUDIO_V10x100:
1207 if (!uaudio_getnum(p, 2, NULL((void *)0))) /* bmChannelConfig */
1208 return 0;
1209 break;
1210 case UAUDIO_V20x200:
1211 if (!uaudio_getnum(p, 4, NULL((void *)0))) /* wChannelConfig */
1212 return 0;
1213 break;
1214 }
1215 if (!uaudio_getnum(p, 1, NULL((void *)0))) /* iChannelNames */
1216 return 0;
1217 return 1;
1218}
1219
1220/*
1221 * Find the AC class-specific descriptor for this unit id.
1222 */
1223int
1224uaudio_unit_getdesc(struct uaudio_softc *sc, int id,
1225 struct uaudio_blob units,
1226 struct uaudio_blob *p,
1227 unsigned int *rtype)
1228{
1229 unsigned int i, type, subtype;
1230
1231 /*
1232 * Find the usb descriptor for this id.
1233 */
1234 while (1) {
1235 if (units.rptr == units.wptr) {
1236 DPRINTF("%s: %02u: not found\n", __func__, id)do {} while(0);
1237 return 0;
1238 }
1239 if (!uaudio_getdesc(&units, p))
1240 return 0;
1241 if (!uaudio_getnum(p, 1, &type))
1242 return 0;
1243 if (!uaudio_getnum(p, 1, &subtype))
1244 return 0;
1245 if (!uaudio_getnum(p, 1, &i))
1246 return 0;
1247 if (i == id)
1248 break;
1249 }
1250 *rtype = subtype;
1251 return 1;
1252}
1253
1254/*
1255 * Parse a unit, possibly calling uaudio_process_unit() for each of
1256 * its sources.
1257 */
1258int
1259uaudio_process_unit(struct uaudio_softc *sc,
1260 struct uaudio_unit *dest, int id,
1261 struct uaudio_blob units,
1262 struct uaudio_unit **rchild)
1263{
1264 struct uaudio_blob p;
1265 struct uaudio_unit *u, *s;
1266 unsigned int i, j, size, ctl, type, subtype, assoc, clk;
1267#ifdef UAUDIO_DEBUG
1268 unsigned int bit;
1269#endif
1270
1271 if (!uaudio_unit_getdesc(sc, id, units, &p, &subtype))
1272 return 0;
1273
1274 /*
1275 * find this unit on the list as it may be already processed as
1276 * the source of another destination
1277 */
1278 u = uaudio_unit_byid(sc, id);
1279 if (u == NULL((void *)0)) {
1280 u = malloc(sizeof(struct uaudio_unit), M_USBDEV102, M_WAITOK0x0001);
1281 u->id = id;
1282 u->type = subtype;
1283 u->term = 0;
1284 u->src_list = NULL((void *)0);
1285 u->dst_list = NULL((void *)0);
1286 u->clock = NULL((void *)0);
1287 u->mixent_list = NULL((void *)0);
1288 u->nch = 0;
1289 u->name[0] = 0;
1290 uaudio_ranges_init(&u->rates);
1291 u->unit_next = sc->unit_list;
1292 sc->unit_list = u;
1293 } else {
1294 switch (u->type) {
1295 case UAUDIO_AC_CLKSRC0xa:
1296 case UAUDIO_AC_CLKSEL0xb:
1297 case UAUDIO_AC_CLKMULT0xc:
1298 case UAUDIO_AC_RATECONV0xd:
1299 /* not using 'dest' list */
1300 *rchild = u;
1301 return 1;
1302 }
1303 }
1304
1305 if (dest) {
1306 dest->dst_next = u->dst_list;
1307 u->dst_list = dest;
1308 if (dest->dst_next != NULL((void *)0)) {
1309 /* already seen */
1310 *rchild = u;
1311 return 1;
1312 }
1313 }
1314
1315 switch (u->type) {
1316 case UAUDIO_AC_INPUT0x2:
1317 if (!uaudio_getnum(&p, 2, &u->term))
1318 return 0;
1319 if (!uaudio_getnum(&p, 1, &assoc))
1320 return 0;
1321 if (u->term >> 8 != 1)
1322 sc->nin++;
1323 switch (sc->version) {
1324 case UAUDIO_V10x100:
1325 break;
1326 case UAUDIO_V20x200:
1327 if (!uaudio_getnum(&p, 1, &clk))
1328 return 0;
1329 if (!uaudio_process_unit(sc, NULL((void *)0),
1330 clk, units, &u->clock))
1331 return 0;
1332 break;
1333 }
1334 if (!uaudio_getnum(&p, 1, &u->nch))
1335 return 0;
1336 DPRINTF("%02u: "do {} while(0)
1337 "in, nch = %d, term = 0x%x, assoc = %d\n",do {} while(0)
1338 u->id, u->nch, u->term, assoc)do {} while(0);
1339 break;
1340 case UAUDIO_AC_OUTPUT0x3:
1341 if (!uaudio_getnum(&p, 2, &u->term))
1342 return 0;
1343 if (!uaudio_getnum(&p, 1, &assoc))
1344 return 0;
1345 if (!uaudio_getnum(&p, 1, &id))
1346 return 0;
1347 if (!uaudio_process_unit(sc, u, id, units, &s))
1348 return 0;
1349 if (u->term >> 8 != 1)
1350 sc->nout++;
1351 switch (sc->version) {
1352 case UAUDIO_V10x100:
1353 break;
1354 case UAUDIO_V20x200:
1355 if (!uaudio_getnum(&p, 1, &clk))
1356 return 0;
1357 if (!uaudio_process_unit(sc, NULL((void *)0),
1358 clk, units, &u->clock))
1359 return 0;
1360 break;
1361 }
1362 u->src_list = s;
1363 s->src_next = NULL((void *)0);
1364 u->nch = s->nch;
1365 DPRINTF("%02u: "do {} while(0)
1366 "out, id = %d, nch = %d, term = 0x%x, assoc = %d\n",do {} while(0)
1367 u->id, id, u->nch, u->term, assoc)do {} while(0);
1368 break;
1369 case UAUDIO_AC_MIXER0x4:
1370 if (!uaudio_process_srcs(sc, u, units, &p))
1371 return 0;
1372 if (!uaudio_process_nch(sc, u, &p))
1373 return 0;
1374 DPRINTF("%02u: mixer, nch = %u:\n", u->id, u->nch)do {} while(0);
1375
1376#ifdef UAUDIO_DEBUG
1377 /*
1378 * Print the list of available mixer's unit knobs (a bit
1379 * matrix). Matrix mixers are rare because levels are
1380 * already controlled by feature units, making the mixer
1381 * knobs redundant with the feature's knobs. So, for
1382 * now, we don't add clutter to the mixer(4) interface
1383 * and ignore all knobs. Other popular OSes doesn't
1384 * seem to expose them either.
1385 */
1386 bit = 0;
1387 for (s = u->src_list; s != NULL((void *)0); s = s->src_next) {
1388 for (i = 0; i < s->nch; i++) {
1389 for (j = 0; j < u->nch; j++) {
1390 if ((bit++ & 7) == 0) {
1391 if (!uaudio_getnum(&p, 1, &ctl))
1392 return 0;
1393 }
1394 if (ctl & 0x80)
1395 DPRINTF("\t%02u[%d] -> [%d]\n",do {} while(0)
1396 s->id, i, j)do {} while(0);
1397 ctl <<= 1;
1398 }
1399 }
1400 }
1401#endif
1402 break;
1403 case UAUDIO_AC_SELECTOR0x5:
1404 /*
1405 * Selectors are extremely rare, so not supported yet.
1406 */
1407 if (!uaudio_process_srcs(sc, u, units, &p))
1408 return 0;
1409 if (u->src_list == NULL((void *)0)) {
1410 printf("%s: selector %02u has no sources\n",
1411 DEVNAME(sc)((sc)->dev.dv_xname), u->id);
1412 return 0;
1413 }
1414 u->nch = u->src_list->nch;
1415 DPRINTF("%02u: selector, nch = %u\n", u->id, u->nch)do {} while(0);
1416 break;
1417 case UAUDIO_AC_FEATURE0x6:
1418 if (!uaudio_getnum(&p, 1, &id))
1419 return 0;
1420 if (!uaudio_process_unit(sc, u, id, units, &s))
1421 return 0;
1422 s->src_next = u->src_list;
1423 u->src_list = s;
1424 u->nch = s->nch;
1425 switch (sc->version) {
1426 case UAUDIO_V10x100:
1427 if (!uaudio_getnum(&p, 1, &size))
1428 return 0;
1429 break;
1430 case UAUDIO_V20x200:
1431 size = 4;
1432 break;
1433 }
1434 DPRINTF("%02d: feature id = %d, nch = %d, size = %d\n",do {} while(0)
1435 u->id, id, u->nch, size)do {} while(0);
1436
1437 if (!uaudio_getnum(&p, size, &ctl))
1438 return 0;
1439 ctl = uaudio_feature_fixup(sc, ctl);
1440 for (i = 0; i < 16; i++) {
1441 if ((ctl & 3) == 3)
1442 uaudio_feature_addent(sc, u, i, -1);
1443 ctl >>= 2;
1444 }
1445
1446 /*
1447 * certain devices provide no per-channel control descriptors
1448 */
1449 if (p.wptr - p.rptr == 1)
1450 break;
1451
1452 for (j = 0; j < u->nch; j++) {
1453 if (!uaudio_getnum(&p, size, &ctl))
1454 return 0;
1455 ctl = uaudio_feature_fixup(sc, ctl);
1456 for (i = 0; i < 16; i++) {
1457 if ((ctl & 3) == 3)
1458 uaudio_feature_addent(sc, u, i, j);
1459 ctl >>= 2;
1460 }
1461 }
1462 break;
1463 case UAUDIO_AC_EFFECT0x7:
1464 if (!uaudio_getnum(&p, 2, &type))
1465 return 0;
1466 if (!uaudio_getnum(&p, 1, &id))
1467 return 0;
1468 if (!uaudio_process_unit(sc, u, id, units, &s))
1469 return 0;
1470 s->src_next = u->src_list;
1471 u->src_list = s;
1472 u->nch = s->nch;
1473 DPRINTF("%02d: effect, type = %u, id = %d, nch = %d\n",do {} while(0)
1474 u->id, type, id, u->nch)do {} while(0);
1475 break;
1476 case UAUDIO_AC_PROCESSING0x8:
1477 case UAUDIO_AC_EXTENSION0x9:
1478 if (!uaudio_getnum(&p, 2, &type))
1479 return 0;
1480 if (!uaudio_process_srcs(sc, u, units, &p))
1481 return 0;
1482 if (!uaudio_process_nch(sc, u, &p))
1483 return 0;
1484 DPRINTF("%02u: proc/ext, type = 0x%x, nch = %u\n",do {} while(0)
1485 u->id, type, u->nch)do {} while(0);
1486 for (s = u->src_list; s != NULL((void *)0); s = s->src_next) {
1487 DPRINTF("%u:\tpin %u:\n", u->id, s->id)do {} while(0);
1488 }
1489 break;
1490 case UAUDIO_AC_CLKSRC0xa:
1491 if (!uaudio_getnum(&p, 1, &u->term))
1492 return 0;
1493 if (!uaudio_getnum(&p, 1, &ctl))
1494 return 0;
1495 DPRINTF("%02u: clock source, attr = 0x%x, ctl = 0x%x\n",do {} while(0)
1496 u->id, u->term, ctl)do {} while(0);
1497 break;
1498 case UAUDIO_AC_CLKSEL0xb:
1499 DPRINTF("%02u: clock sel\n", u->id)do {} while(0);
1500 if (!uaudio_process_srcs(sc, u, units, &p))
1501 return 0;
1502 if (u->src_list == NULL((void *)0)) {
1503 printf("%s: clock selector %02u with no srcs\n",
1504 DEVNAME(sc)((sc)->dev.dv_xname), u->id);
1505 return 0;
1506 }
1507 break;
1508 case UAUDIO_AC_CLKMULT0xc:
1509 DPRINTF("%02u: clock mult\n", u->id)do {} while(0);
1510
1511 /* XXX: fetch multiplier */
1512 printf("%s: clock multiplier not supported\n", DEVNAME(sc)((sc)->dev.dv_xname));
1513 break;
1514 case UAUDIO_AC_RATECONV0xd:
1515 DPRINTF("%02u: rate conv\n", u->id)do {} while(0);
1516
1517 /* XXX: fetch multiplier */
1518 printf("%s: rate converter not supported\n", DEVNAME(sc)((sc)->dev.dv_xname));
1519 break;
1520 }
1521 if (rchild)
1522 *rchild = u;
1523 return 1;
1524}
1525
1526/*
1527 * Try to set the unit name to the name of its destination terminal. If
1528 * the name is ambiguous (already given to another source unit or having
1529 * multiple destinations) then return 0.
1530 */
1531int
1532uaudio_setname_dsts(struct uaudio_softc *sc, struct uaudio_unit *u, char *name)
1533{
1534 struct uaudio_unit *d = u;
1535
1536 while (d != NULL((void *)0)) {
1537 if (d->dst_list == NULL((void *)0) || d->dst_list->dst_next != NULL((void *)0))
1538 break;
1539 d = d->dst_list;
1540 if (d->src_list == NULL((void *)0) || d->src_list->src_next != NULL((void *)0))
1541 break;
1542 if (d->name[0] != '\0') {
1543 if (name != NULL((void *)0) && strcmp(name, d->name) != 0)
1544 break;
1545 strlcpy(u->name, d->name, UAUDIO_NAMEMAX16);
1546 return 1;
1547 }
1548 }
1549 return 0;
1550}
1551
1552/*
1553 * Try to set the unit name to the name of its source terminal. If the
1554 * name is ambiguous (already given to another destination unit or
1555 * having multiple sources) then return 0.
1556 */
1557int
1558uaudio_setname_srcs(struct uaudio_softc *sc, struct uaudio_unit *u, char *name)
1559{
1560 struct uaudio_unit *s = u;
1561
1562 while (s != NULL((void *)0)) {
1563 if (s->src_list == NULL((void *)0) || s->src_list->src_next != NULL((void *)0))
1564 break;
1565 s = s->src_list;
1566 if (s->dst_list == NULL((void *)0) || s->dst_list->dst_next != NULL((void *)0))
1567 break;
1568 if (s->name[0] != '\0') {
1569 if (name != NULL((void *)0) && strcmp(name, s->name) != 0)
1570 break;
1571 strlcpy(u->name, s->name, UAUDIO_NAMEMAX16);
1572 return 1;
1573 }
1574 }
1575 return 0;
1576}
1577
1578/*
1579 * Set the name of the given unit by using both its source and
1580 * destination units. This is naming scheme is only useful to units
1581 * that would have ambiguous names if only sources or only destination
1582 * were used.
1583 */
1584void
1585uaudio_setname_middle(struct uaudio_softc *sc, struct uaudio_unit *u)
1586{
1587 struct uaudio_unit *s, *d;
1588 char name[UAUDIO_NAMEMAX16];
1589
1590 s = u->src_list;
1591 while (1) {
1592 if (s == NULL((void *)0)) {
1593 DPRINTF("%s: %02u: has no srcs\n",do {} while(0)
1594 __func__, u->id)do {} while(0);
1595 return;
1596 }
1597 if (s->name[0] != '\0')
1598 break;
1599 s = s->src_list;
1600 }
1601
1602 d = u->dst_list;
1603 while (1) {
1604 if (d == NULL((void *)0)) {
1605 DPRINTF("%s: %02u: has no dests\n",do {} while(0)
1606 __func__, u->id)do {} while(0);
1607 return;
1608 }
1609 if (d->name[0] != '\0')
1610 break;
1611 d = d->dst_list;
1612 }
1613
1614 snprintf(name, UAUDIO_NAMEMAX16, "%s_%s", d->name, s->name);
1615 uaudio_mkname(sc, name, u->name);
1616}
1617
1618#ifdef UAUDIO_DEBUG
1619/*
1620 * Return the synchronization type name, for debug purposes only.
1621 */
1622char *
1623uaudio_isoname(int isotype)
1624{
1625 switch (isotype) {
1626 case UE_ISO_ASYNC0x04:
1627 return "async";
1628 case UE_ISO_ADAPT0x08:
1629 return "adapt";
1630 case UE_ISO_SYNC0x0c:
1631 return "sync";
1632 default:
1633 return "unk";
1634 }
1635}
1636
1637/*
1638 * Return the name of the given mode, debug only
1639 */
1640char *
1641uaudio_modename(int mode)
1642{
1643 switch (mode) {
1644 case 0:
1645 return "none";
1646 case AUMODE_PLAY0x01:
1647 return "play";
1648 case AUMODE_RECORD0x02:
1649 return "rec";
1650 case AUMODE_PLAY0x01 | AUMODE_RECORD0x02:
1651 return "duplex";
1652 default:
1653 return "unk";
1654 }
1655}
1656
1657/*
1658 * Return UAC v2.0 endpoint usage, debug only
1659 */
1660char *
1661uaudio_usagename(int usage)
1662{
1663 switch (usage) {
1664 case UE_ISO_USAGE_DATA0x00:
1665 return "data";
1666 case UE_ISO_USAGE_FEEDBACK0x10:
1667 return "feed";
1668 case UE_ISO_USAGE_IMPL0x20:
1669 return "impl";
1670 default:
1671 return "unk";
1672 }
1673}
1674
1675/*
1676 * Print a bitmap of rates on the console.
1677 */
1678void
1679uaudio_rates_print(int rates)
1680{
1681 unsigned int i;
1682
1683 for (i = 0; i < nitems(uaudio_rates)(sizeof((uaudio_rates)) / sizeof((uaudio_rates)[0])); i++) {
1684 if (rates & (1 << i))
1685 printf(" %d", uaudio_rates[i]);
1686 }
1687 printf("\n");
1688}
1689
1690
1691/*
1692 * Print uaudio_ranges to console.
1693 */
1694void
1695uaudio_ranges_print(struct uaudio_ranges *r)
1696{
1697 struct uaudio_ranges_el *e;
1698 int more = 0;
1699
1700 for (e = r->el; e != NULL((void *)0); e = e->next) {
1701 if (more)
1702 printf(", ");
1703 if (e->min == e->max)
1704 printf("%d", e->min);
1705 else
1706 printf("[%d:%d]/%d", e->min, e->max, e->res);
1707 more = 1;
1708 }
1709 printf(" (%d vals)\n", r->nval);
1710}
1711
1712/*
1713 * Print unit to the console.
1714 */
1715void
1716uaudio_print_unit(struct uaudio_softc *sc, struct uaudio_unit *u)
1717{
1718 struct uaudio_unit *s;
1719
1720 switch (u->type) {
1721 case UAUDIO_AC_INPUT0x2:
1722 printf("%02u: input <%s>, dest = %02u <%s>\n",
1723 u->id, u->name, u->dst_list->id, u->dst_list->name);
1724 break;
1725 case UAUDIO_AC_OUTPUT0x3:
1726 printf("%02u: output <%s>, source = %02u <%s>\n",
1727 u->id, u->name, u->src_list->id, u->src_list->name);
1728 break;
1729 case UAUDIO_AC_MIXER0x4:
1730 printf("%02u: mixer <%s>:\n", u->id, u->name);
1731 for (s = u->src_list; s != NULL((void *)0); s = s->src_next)
1732 printf("%02u:\tsource %u <%s>:\n",
1733 u->id, s->id, s->name);
1734 break;
1735 case UAUDIO_AC_SELECTOR0x5:
1736 printf("%02u: selector <%s>:\n", u->id, u->name);
1737 for (s = u->src_list; s != NULL((void *)0); s = s->src_next)
1738 printf("%02u:\tsource %u <%s>:\n",
1739 u->id, s->id, s->name);
1740 break;
1741 case UAUDIO_AC_FEATURE0x6:
1742 printf("%02u: feature <%s>, "
1743 "src = %02u <%s>, dst = %02u <%s>, cls = %d\n",
1744 u->id, u->name,
1745 u->src_list->id, u->src_list->name,
1746 u->dst_list->id, u->dst_list->name, u->mixer_class);
1747 break;
1748 case UAUDIO_AC_EFFECT0x7:
1749 printf("%02u: effect <%s>, "
1750 "src = %02u <%s>, dst = %02u <%s>\n",
1751 u->id, u->name,
1752 u->src_list->id, u->src_list->name,
1753 u->dst_list->id, u->dst_list->name);
1754 break;
1755 case UAUDIO_AC_PROCESSING0x8:
1756 case UAUDIO_AC_EXTENSION0x9:
1757 printf("%02u: proc/ext <%s>:\n", u->id, u->name);
1758 for (s = u->src_list; s != NULL((void *)0); s = s->src_next)
1759 printf("%02u:\tsource %u <%s>:\n",
1760 u->id, s->id, s->name);
1761 break;
1762 case UAUDIO_AC_CLKSRC0xa:
1763 printf("%02u: clock source <%s>\n", u->id, u->name);
1764 break;
1765 case UAUDIO_AC_CLKSEL0xb:
1766 printf("%02u: clock sel <%s>\n", u->id, u->name);
1767 break;
1768 case UAUDIO_AC_CLKMULT0xc:
1769 printf("%02u: clock mult\n", u->id);
1770 break;
1771 case UAUDIO_AC_RATECONV0xd:
1772 printf("%02u: rate conv\n", u->id);
1773 break;
1774 }
1775}
1776
1777/*
1778 * Print the full mixer on the console.
1779 */
1780void
1781uaudio_mixer_print(struct uaudio_softc *sc)
1782{
1783 struct uaudio_mixent *m;
1784 struct uaudio_unit *u;
1785
1786 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
1787 for (m = u->mixent_list; m != NULL((void *)0); m = m->next) {
1788 printf("%02u:\t%s.%s",
1789 u->id, u->name, m->fname);
1790 if (m->chan >= 0)
1791 printf("[%u]", m->chan);
1792 printf("\n");
1793 }
1794 }
1795}
1796
1797/*
1798 * Print the full device configuration on the console.
1799 */
1800void
1801uaudio_conf_print(struct uaudio_softc *sc)
1802{
1803 struct uaudio_alt *a;
1804 struct uaudio_params *p;
1805 struct mixer_devinfo mi;
1806 struct mixer_ctrl ctl;
1807 int i, rates;
1808
1809 mi.index = 0;
1810 while (1) {
1811 if (uaudio_query_devinfo(sc, &mi) != 0)
1812 break;
1813
1814 if (mi.type != AUDIO_MIXER_CLASS0) {
1815 ctl.dev = mi.index;
1816 if (uaudio_get_port(sc, &ctl) != 0) {
1817 printf("%02u: failed to get port\n", mi.index);
1818 memset(&ctl.un, 0, sizeof(ctl.un))__builtin_memset((&ctl.un), (0), (sizeof(ctl.un)));
1819 }
1820 }
1821
1822 printf("%02u: <%s>, next = %d, prev = %d, class = %d",
1823 mi.index, mi.label.name, mi.next, mi.prev, mi.mixer_class);
1824
1825 switch (mi.type) {
1826 case AUDIO_MIXER_CLASS0:
1827 break;
1828 case AUDIO_MIXER_VALUE3:
1829 printf(", nch = %d, delta = %d",
1830 mi.un.v.num_channels, mi.un.v.delta);
1831 printf(", val =");
1832 for (i = 0; i < mi.un.v.num_channels; i++)
1833 printf(" %d", ctl.un.value.level[i]);
1834 break;
1835 case AUDIO_MIXER_ENUM1:
1836 printf(", members:");
1837 for (i = 0; i != mi.un.e.num_mem; i++) {
1838 printf(" %s(=%d)",
1839 mi.un.e.member[i].label.name,
1840 mi.un.e.member[i].ord);
1841 }
1842 printf(", val = %d", ctl.un.ord);
1843 break;
1844 }
1845
1846 printf("\n");
1847 mi.index++;
1848 }
1849
1850 printf("%d controls\n", mi.index);
1851
1852 printf("alts:\n");
1853 for (a = sc->alts; a != NULL((void *)0); a = a->next) {
1854 rates = uaudio_alt_getrates(sc, a);
1855 printf("mode = %s, ifnum = %d, altnum = %d, "
1856 "addr = 0x%x, maxpkt = %d, sync = 0x%x, "
1857 "nch = %d, fmt = s%dle%d, rates:",
1858 uaudio_modename(a->mode),
1859 a->ifnum, a->altnum,
1860 a->data_addr, a->maxpkt,
1861 a->sync_addr,
1862 a->nch, a->bits, a->bps);
1863 uaudio_rates_print(rates);
1864 }
1865
1866 printf("parameters:\n");
1867 for (p = sc->params_list; p != NULL((void *)0); p = p->next) {
1868 switch (sc->version) {
1869 case UAUDIO_V10x100:
1870 rates = p->v1_rates;
1871 break;
1872 case UAUDIO_V20x200:
1873 rates = uaudio_getrates(sc, p);
1874 break;
1875 }
1876 printf("pchan = %d, s%dle%d, rchan = %d, s%dle%d, rates:",
1877 p->palt ? p->palt->nch : 0,
1878 p->palt ? p->palt->bits : 0,
1879 p->palt ? p->palt->bps : 0,
1880 p->ralt ? p->ralt->nch : 0,
1881 p->ralt ? p->ralt->bits : 0,
1882 p->ralt ? p->ralt->bps : 0);
1883 uaudio_rates_print(rates);
1884 }
1885}
1886#endif
1887
1888/*
1889 * Return the number of mixer controls that have the same name but
1890 * control different channels of the same stream.
1891 */
1892int
1893uaudio_mixer_nchan(struct uaudio_mixent *m, struct uaudio_mixent **rnext)
1894{
1895 char *name;
1896 int i;
1897
1898 i = 0;
1899 name = m->fname;
1900 while (m != NULL((void *)0) && strcmp(name, m->fname) == 0) {
1901 m = m->next;
1902 i++;
1903 }
1904 if (rnext)
1905 *rnext = m;
1906 return i;
1907}
1908
1909/*
1910 * Skip redundant mixer entries that we don't want to expose to userland.
1911 * For instance if there is a mute-all-channels control and per-channel
1912 * mute controls, we don't want both (we expose the per-channel mute)
1913 */
1914void
1915uaudio_mixer_skip(struct uaudio_mixent **pm)
1916{
1917 struct uaudio_mixent *m = *pm;
1918
1919 if (m != NULL((void *)0) &&
1920 m->chan == -1 &&
1921 m->next != NULL((void *)0) &&
1922 strcmp(m->fname, m->next->fname) == 0)
1923 m = m->next;
1924
1925 *pm = m;
1926}
1927
1928/*
1929 * Return pointer to the unit and mixer entry which have the given
1930 * index exposed by the mixer(4) API.
1931 */
1932int
1933uaudio_mixer_byindex(struct uaudio_softc *sc, int index,
1934 struct uaudio_unit **ru, struct uaudio_mixent **rm)
1935{
1936 struct uaudio_unit *u;
1937 struct uaudio_mixent *m;
1938 char *name;
1939 int i;
1940
1941 i = UAUDIO_CLASS_COUNT2;
1942 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
1943 m = u->mixent_list;
1944 while (1) {
1945 uaudio_mixer_skip(&m);
1946 if (m == NULL((void *)0))
1947 break;
1948 if (index == i) {
1949 *ru = u;
1950 *rm = m;
1951 return 1;
1952 }
1953 if (m->type == UAUDIO_MIX_NUM1) {
1954 name = m->fname;
1955 while (m != NULL((void *)0) &&
1956 strcmp(name, m->fname) == 0)
1957 m = m->next;
1958 } else
1959 m = m->next;
1960 i++;
1961 }
1962 }
1963 return 0;
1964}
1965
1966/*
1967 * Parse AC header descriptor, we use it only to determine UAC
1968 * version. Other properties (like wTotalLength) can be determined
1969 * using other descriptors, so we try to no rely on them to avoid
1970 * inconsistencies and the need for certain quirks.
1971 */
1972int
1973uaudio_process_header(struct uaudio_softc *sc, struct uaudio_blob *p)
1974{
1975 struct uaudio_blob ph;
1976 unsigned int type, subtype;
1977
1978 if (!uaudio_getdesc(p, &ph))
1979 return 0;
1980 if (!uaudio_getnum(&ph, 1, &type))
1981 return 0;
1982 if (type != UDESC_CS_INTERFACE0x24) {
1983 DPRINTF("%s: expected cs iface desc\n", __func__)do {} while(0);
1984 return 0;
1985 }
1986 if (!uaudio_getnum(&ph, 1, &subtype))
1987 return 0;
1988 if (subtype != UAUDIO_AC_HEADER0x1) {
1989 DPRINTF("%s: expected header desc\n", __func__)do {} while(0);
1990 return 0;
1991 }
1992 if (!uaudio_getnum(&ph, 2, &sc->version))
1993 return 0;
1994
1995 DPRINTF("%s: version 0x%x\n", __func__, sc->version)do {} while(0);
1996 return 1;
1997}
1998
1999/*
2000 * Process AC interrupt endpoint descriptor, this is mainly to skip
2001 * the descriptor as we use neither of its properties. Our mixer
2002 * interface doesn't support unsolicitated state changes, so we've no
2003 * use of it yet.
2004 */
2005int
2006uaudio_process_ac_ep(struct uaudio_softc *sc, struct uaudio_blob *p)
2007{
2008#ifdef UAUDIO_DEBUG
2009 static const char *xfer[] = {
2010 "ctl", "iso", "bulk", "intr"
2011 };
2012#endif
2013 struct uaudio_blob dp;
2014 unsigned int type, addr, attr, maxpkt, ival;
2015 unsigned char *savepos;
2016
2017 /*
2018 * parse optional interrupt endpoint descriptor
2019 */
2020 if (p->rptr == p->wptr)
2021 return 1;
2022 savepos = p->rptr;
2023 if (!uaudio_getdesc(p, &dp))
2024 return 0;
2025 if (!uaudio_getnum(&dp, 1, &type))
2026 return 0;
2027 if (type != UDESC_ENDPOINT0x05) {
2028 p->rptr = savepos;
2029 return 1;
2030 }
2031
2032 if (!uaudio_getnum(&dp, 1, &addr))
2033 return 0;
2034 if (!uaudio_getnum(&dp, 1, &attr))
2035 return 0;
2036 if (!uaudio_getnum(&dp, 2, &maxpkt))
2037 return 0;
2038 if (!uaudio_getnum(&dp, 1, &ival))
2039 return 0;
2040
2041 DPRINTF("%s: addr = 0x%x, type = %s, maxpkt = %d, ival = %d\n",do {} while(0)
2042 __func__, addr, xfer[UE_GET_XFERTYPE(attr)],do {} while(0)
2043 UE_GET_SIZE(maxpkt), ival)do {} while(0);
2044
2045 return 1;
2046}
2047
2048/*
2049 * Process the AC interface descriptors: mainly build the mixer and,
2050 * for UAC v2.0, find the clock source.
2051 *
2052 * The audio device exposes an audio control (AC) interface with a big
2053 * set of USB descriptors which expose the complete circuit the
2054 * device. The circuit describes how the signal flows between the USB
2055 * streaming interfaces to the terminal connectors (jacks, speakers,
2056 * mics, ...). The circuit is build of mixers, source selectors, gain
2057 * controls, mutters, processors, and alike; each comes with its own
2058 * set of controls. Most of the boring driver work is to parse the
2059 * circuit and build a human-usable set of controls that could be
2060 * exposed through the mixer(4) interface.
2061 */
2062int
2063uaudio_process_ac(struct uaudio_softc *sc, struct uaudio_blob *p, int ifnum)
2064{
2065 struct uaudio_blob units, pu;
2066 struct uaudio_unit *u, *v;
2067 unsigned char *savepos;
2068 unsigned int type, subtype, id;
2069 char *name, val;
2070
2071 DPRINTF("%s: ifnum = %d, %zd bytes to process\n", __func__,do {} while(0)
2072 ifnum, p->wptr - p->rptr)do {} while(0);
2073
2074 sc->ctl_ifnum = ifnum;
2075
2076 /* The first AC class-specific descriptor is the AC header */
2077 if (!uaudio_process_header(sc, p))
2078 return 0;
2079
2080 /*
2081 * Determine the size of the AC descriptors array: scan
2082 * descriptors until we get the first non-class-specific
2083 * descriptor. This avoids relying on the wTotalLength field.
2084 */
2085 savepos = p->rptr;
2086 units.rptr = units.wptr = p->rptr;
2087 while (p->rptr != p->wptr) {
2088 if (!uaudio_getdesc(p, &pu))
2089 return 0;
2090 if (!uaudio_getnum(&pu, 1, &type))
2091 return 0;
2092 if (type != UDESC_CS_INTERFACE0x24)
2093 break;
2094 units.wptr = p->rptr;
2095 }
2096 p->rptr = savepos;
2097
2098 /*
2099 * Load units, walking from outputs to inputs, as
2100 * the usb audio class spec requires.
2101 */
2102 while (p->rptr != units.wptr) {
2103 if (!uaudio_getdesc(p, &pu))
2104 return 0;
2105 if (!uaudio_getnum(&pu, 1, &type))
2106 return 0;
2107 if (!uaudio_getnum(&pu, 1, &subtype))
2108 return 0;
2109 if (subtype == UAUDIO_AC_OUTPUT0x3) {
2110 if (!uaudio_getnum(&pu, 1, &id))
2111 return 0;
2112 if (!uaudio_process_unit(sc, NULL((void *)0), id, units, NULL((void *)0)))
2113 return 0;
2114 }
2115 }
2116
2117 /*
2118 * set terminal, effect, and processor unit names
2119 */
2120 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
2121 switch (u->type) {
2122 case UAUDIO_AC_INPUT0x2:
2123 uaudio_mkname(sc, uaudio_tname(sc, u->term, 0), u->name);
2124 break;
2125 case UAUDIO_AC_OUTPUT0x3:
2126 uaudio_mkname(sc, uaudio_tname(sc, u->term, 1), u->name);
2127 break;
2128 case UAUDIO_AC_CLKSRC0xa:
2129 uaudio_mkname(sc, uaudio_clkname(u->term), u->name);
2130 break;
2131 case UAUDIO_AC_CLKSEL0xb:
2132 uaudio_mkname(sc, "clksel", u->name);
2133 break;
2134 case UAUDIO_AC_EFFECT0x7:
2135 uaudio_mkname(sc, "fx", u->name);
2136 break;
2137 case UAUDIO_AC_PROCESSING0x8:
2138 uaudio_mkname(sc, "proc", u->name);
2139 break;
2140 case UAUDIO_AC_EXTENSION0x9:
2141 uaudio_mkname(sc, "ext", u->name);
2142 break;
2143 }
2144 }
2145
2146 /*
2147 * set mixer/selector unit names
2148 */
2149 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
2150 if (u->type != UAUDIO_AC_MIXER0x4 &&
2151 u->type != UAUDIO_AC_SELECTOR0x5)
2152 continue;
2153 if (!uaudio_setname_dsts(sc, u, NULL((void *)0))) {
2154 switch (u->type) {
2155 case UAUDIO_AC_MIXER0x4:
2156 name = "mix";
2157 break;
2158 case UAUDIO_AC_SELECTOR0x5:
2159 name = "sel";
2160 break;
2161 }
2162 uaudio_mkname(sc, name, u->name);
2163 }
2164 }
2165
2166 /*
2167 * set feature unit names and classes
2168 */
2169 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
2170 if (u->type != UAUDIO_AC_FEATURE0x6)
2171 continue;
2172 if (uaudio_setname_dsts(sc, u, UAUDIO_NAME_REC"record")) {
2173 u->mixer_class = UAUDIO_CLASS_IN1;
2174 continue;
2175 }
2176 if (uaudio_setname_srcs(sc, u, UAUDIO_NAME_PLAY"dac")) {
2177 u->mixer_class = UAUDIO_CLASS_OUT0;
2178 continue;
2179 }
2180 if (uaudio_setname_dsts(sc, u, NULL((void *)0))) {
2181 u->mixer_class = UAUDIO_CLASS_OUT0;
2182 continue;
2183 }
2184 if (uaudio_setname_srcs(sc, u, NULL((void *)0))) {
2185 u->mixer_class = UAUDIO_CLASS_IN1;
2186 continue;
2187 }
2188 uaudio_setname_middle(sc, u);
2189 u->mixer_class = UAUDIO_CLASS_IN1;
2190 }
2191
2192#ifdef UAUDIO_DEBUG
2193 if (uaudio_debug) {
2194 printf("%s: units list:\n", DEVNAME(sc)((sc)->dev.dv_xname));
2195 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next)
2196 uaudio_print_unit(sc, u);
2197
2198 printf("%s: mixer controls:\n", DEVNAME(sc)((sc)->dev.dv_xname));
2199 uaudio_mixer_print(sc);
2200 }
2201#endif
2202
2203 /* follows optional interrupt endpoint descriptor */
2204 if (!uaudio_process_ac_ep(sc, p))
2205 return 0;
2206
2207 /* fetch clock source rates */
2208 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
2209 switch (u->type) {
2210 case UAUDIO_AC_CLKSRC0xa:
2211 if (!uaudio_req_ranges(sc, 4,
2212 UAUDIO_V2_REQSEL_CLKFREQ1,
2213 0, /* channel (not used) */
2214 sc->ctl_ifnum,
2215 u->id,
2216 &u->rates)) {
2217 printf("%s: failed to read clock rates\n",
2218 DEVNAME(sc)((sc)->dev.dv_xname));
2219 return 0;
2220 }
2221#ifdef UAUDIO_DEBUG
2222 if (uaudio_debug) {
2223 printf("%02u: clock rates: ", u->id);
2224 uaudio_ranges_print(&u->rates);
2225 }
2226#endif
2227 break;
2228 case UAUDIO_AC_CLKSEL0xb:
2229 if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
2230 UAUDIO_V2_REQ_CUR1,
2231 UAUDIO_V2_REQSEL_CLKSEL1, 0,
2232 sc->ctl_ifnum, u->id,
2233 &val, 1)) {
2234 printf("%s: failed to read clock selector\n",
2235 DEVNAME(sc)((sc)->dev.dv_xname));
2236 return 0;
2237 }
2238 for (v = u->src_list; v != NULL((void *)0); v = v->src_next) {
2239 if (--val == 0)
2240 break;
2241 }
2242 u->clock = v;
2243 break;
2244 }
2245 }
2246
2247 if (sc->version == UAUDIO_V20x200) {
2248 /*
2249 * Find common clock unit. We assume all terminals
2250 * belong to the same clock domain (ie are connected
2251 * to the same source)
2252 */
2253 sc->clock = NULL((void *)0);
2254 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
2255 if (u->type != UAUDIO_AC_INPUT0x2 &&
2256 u->type != UAUDIO_AC_OUTPUT0x3)
2257 continue;
2258 if (sc->clock == NULL((void *)0)) {
2259 if (u->clock == NULL((void *)0)) {
2260 printf("%s: terminal with no clock\n",
2261 DEVNAME(sc)((sc)->dev.dv_xname));
2262 return 0;
2263 }
2264 sc->clock = u->clock;
2265 } else if (u->clock != sc->clock) {
2266 printf("%s: only one clock domain supported\n",
2267 DEVNAME(sc)((sc)->dev.dv_xname));
2268 return 0;
2269 }
2270 }
2271
2272 if (sc->clock == NULL((void *)0)) {
2273 printf("%s: no clock found\n", DEVNAME(sc)((sc)->dev.dv_xname));
2274 return 0;
2275 }
2276 }
2277 return 1;
2278}
2279
2280/*
2281 * Parse endpoint descriptor with the following format:
2282 *
2283 * For playback there's a output data endpoint, of the
2284 * following types:
2285 *
2286 * type sync descr
2287 * -------------------------------------------------------
2288 * async: Yes the device uses its own clock but
2289 * sends feedback on a (input) sync endpoint
2290 * for the host to adjust next packet size
2291 *
2292 * sync: - data rate is constant, and device
2293 * is clocked to the usb bus.
2294 *
2295 * adapt: - the device adapts to data rate of the
2296 * host. If fixed packet size is used,
2297 * data rate is equivalent to the usb clock
2298 * so this mode is the same as the
2299 * sync mode.
2300 *
2301 * For recording there's and input data endpoint, of
2302 * the following types:
2303 *
2304 * type sync descr
2305 * -------------------------------------------------------
2306 * async: - the device uses its own clock and
2307 * adjusts packet sizes.
2308 *
2309 * sync: - the device uses usb clock rate
2310 *
2311 * adapt: Yes the device uses host's feedback (on
2312 * on a dedicated (output) sync endpoint
2313 * to adapt to software's desired rate
2314 *
2315 *
2316 * For usb1.1 ival is cardcoded to 1 for isochronous
2317 * transfers, which means one transfer every ms. I.e one
2318 * transfer every frame period.
2319 *
2320 * For usb2, ival the poll interval is:
2321 *
2322 * frame_period * 2^(ival - 1)
2323 *
2324 * so, if we use this formula, we get something working in all
2325 * cases.
2326 *
2327 * The MaxPacketsOnly attribute is used only by "Type II" encodings,
2328 * so we don't care about it.
2329 */
2330int
2331uaudio_process_as_ep(struct uaudio_softc *sc,
2332 struct uaudio_blob *p, struct uaudio_alt *a, int nep)
2333{
2334 unsigned int addr, attr, maxpkt, isotype, ival;
2335
2336 if (!uaudio_getnum(p, 1, &addr))
2337 return 0;
2338 if (!uaudio_getnum(p, 1, &attr))
2339 return 0;
2340 if (!uaudio_getnum(p, 2, &maxpkt))
2341 return 0;
2342 if (!uaudio_getnum(p, 1, &ival)) /* bInterval */
2343 return 0;
2344
2345 DPRINTF("%s: addr = 0x%x, %s/%s, "do {} while(0)
2346 "maxpktsz = %d, ival = %d\n",do {} while(0)
2347 __func__, addr,do {} while(0)
2348 uaudio_isoname(UE_GET_ISO_TYPE(attr)),do {} while(0)
2349 uaudio_usagename(UE_GET_ISO_USAGE(attr)),do {} while(0)
2350 maxpkt, ival)do {} while(0);
2351
2352 if (UE_GET_XFERTYPE(attr)((attr) & 0x03) != UE_ISOCHRONOUS0x01) {
2353 printf("%s: skipped non-isoc endpt.\n", DEVNAME(sc)((sc)->dev.dv_xname));
2354 return 1;
2355 }
2356
2357 /*
2358 * For each AS interface setting, there's a single data
2359 * endpoint and an optional feedback endpoint. The
2360 * synchronization type is non-zero and must be set in the data
2361 * endpoints.
2362 *
2363 * However, the isoc sync type field of the attribute can't be
2364 * trusted: a lot of devices have it wrong. If the isoc sync
2365 * type is set it's necessarily a data endpoint, if it's not,
2366 * then if it is the only endpoint, it necessarily the data
2367 * endpoint.
2368 */
2369 isotype = UE_GET_ISO_TYPE(attr)((attr) & 0x0c);
2370 if (isotype || nep == 1) {
2371 /* this is the data endpoint */
2372
2373 if (a->data_addr && addr != a->data_addr) {
2374 printf("%s: skipped extra data endpt.\n", DEVNAME(sc)((sc)->dev.dv_xname));
2375 return 1;
2376 }
2377
2378 a->mode = (UE_GET_DIR(addr)((addr) & 0x80) == UE_DIR_IN0x80) ?
2379 AUMODE_RECORD0x02 : AUMODE_PLAY0x01;
2380 a->data_addr = addr;
2381 a->fps = sc->ufps / (1 << (ival - 1));
2382 a->maxpkt = UE_GET_SIZE(maxpkt)((maxpkt) & 0x7ff);
2383 } else {
2384 /* this is the sync endpoint */
2385
2386 if (a->sync_addr && addr != a->sync_addr) {
2387 printf("%s: skipped extra sync endpt.\n", DEVNAME(sc)((sc)->dev.dv_xname));
2388 return 1;
2389 }
2390 a->sync_addr = addr;
2391 }
2392
2393 return 1;
2394}
2395
2396/*
2397 * Parse AS general descriptor. Non-PCM interfaces are skipped. UAC
2398 * v2.0 report the number of channels. For UAC v1.0 we set the number
2399 * of channels to zero, it will be determined later from the format
2400 * descriptor.
2401 */
2402int
2403uaudio_process_as_general(struct uaudio_softc *sc,
2404 struct uaudio_blob *p, int *rispcm, struct uaudio_alt *a)
2405{
2406 unsigned int term, fmt, ctl, fmt_type, fmt_map, nch;
2407
2408 if (!uaudio_getnum(p, 1, &term))
2409 return 0;
2410 switch (sc->version) {
2411 case UAUDIO_V10x100:
2412 if (!uaudio_getnum(p, 1, NULL((void *)0))) /* bDelay */
2413 return 0;
2414 if (!uaudio_getnum(p, 1, &fmt))
2415 return 0;
2416 *rispcm = (fmt == UAUDIO_V1_FMT_PCM0x1);
2417 break;
2418 case UAUDIO_V20x200:
2419 /* XXX: should we check if alt setting control is valid ? */
2420 if (!uaudio_getnum(p, 1, &ctl))
2421 return 0;
2422 if (!uaudio_getnum(p, 1, &fmt_type))
2423 return 0;
2424 if (!uaudio_getnum(p, 4, &fmt_map))
2425 return 0;
2426 if (!uaudio_getnum(p, 1, &nch))
2427 return 0;
2428 a->nch = nch;
2429 *rispcm = (fmt_type == 1) && (fmt_map & UAUDIO_V2_FMT_PCM0x01);
2430 }
2431 return 1;
2432}
2433
2434/*
2435 * Parse AS format descriptor: we support only "Type 1" formats, aka
2436 * PCM. Other formats are not really audio, they are data-only
2437 * interfaces that we don't want to support: ethernet is much better
2438 * for raw data transfers.
2439 *
2440 * XXX: handle ieee 754 32-bit floating point formats.
2441 */
2442int
2443uaudio_process_as_format(struct uaudio_softc *sc,
2444 struct uaudio_blob *p, struct uaudio_alt *a, int *ispcm)
2445{
2446 unsigned int type, bps, bits, nch, nrates, rate_min, rate_max, rates;
34
'bps' declared without an initial value
2447 int i, j;
2448
2449 switch (sc->version) {
35
'Default' branch taken. Execution continues on line 2518
2450 case UAUDIO_V10x100:
2451 if (!uaudio_getnum(p, 1, &type))
2452 return 0;
2453 if (type != 1) {
2454 DPRINTF("%s: class v1: "do {} while(0)
2455 "skipped unsupported type = %d\n", __func__, type)do {} while(0);
2456 *ispcm = 0;
2457 return 1;
2458 }
2459 if (!uaudio_getnum(p, 1, &nch))
2460 return 0;
2461 if (!uaudio_getnum(p, 1, &bps))
2462 return 0;
2463 if (!uaudio_getnum(p, 1, &bits))
2464 return 0;
2465 if (!uaudio_getnum(p, 1, &nrates))
2466 return 0;
2467 rates = 0;
2468 if (nrates == 0) {
2469 if (!uaudio_getnum(p, 3, &rate_min))
2470 return 0;
2471 if (!uaudio_getnum(p, 3, &rate_max))
2472 return 0;
2473 for (i = 0; i < nitems(uaudio_rates)(sizeof((uaudio_rates)) / sizeof((uaudio_rates)[0])); i++) {
2474 if (uaudio_rates[i] >= rate_min &&
2475 uaudio_rates[i] <= rate_max)
2476 rates |= 1 << i;
2477 }
2478 } else {
2479 for (j = 0; j < nrates; j++) {
2480 if (!uaudio_getnum(p, 3, &rate_min))
2481 return 0;
2482 for (i = 0; i < nitems(uaudio_rates)(sizeof((uaudio_rates)) / sizeof((uaudio_rates)[0])); i++) {
2483 if (uaudio_rates[i] == rate_min)
2484 rates |= 1 << i;
2485 }
2486 }
2487 }
2488 a->v1_rates = rates;
2489 a->nch = nch;
2490 break;
2491 case UAUDIO_V20x200:
2492 /*
2493 * sample rate ranges are obtained with requests to
2494 * the clock source, as defined by the clock source
2495 * descriptor
2496 *
2497 * the number of channels is in the GENERAL descriptor
2498 */
2499 if (!uaudio_getnum(p, 1, &type))
2500 return 0;
2501 if (type != 1) {
2502 DPRINTF("%s: class v2: "do {} while(0)
2503 "skipped unsupported type = %d\n", __func__, type)do {} while(0);
2504 *ispcm = 0;
2505 return 1;
2506 }
2507 if (!uaudio_getnum(p, 1, &bps))
2508 return 0;
2509 if (!uaudio_getnum(p, 1, &bits))
2510 return 0;
2511
2512 /*
2513 * nch is in the v2 general desc, rates come from the
2514 * clock source, so we're done.
2515 */
2516 break;
2517 }
2518 a->bps = bps;
36
Assigned value is garbage or undefined
2519 a->bits = bits;
2520 *ispcm = 1;
2521 return 1;
2522}
2523
2524/*
2525 * Parse AS descriptors.
2526 *
2527 * The audio streaming (AS) interfaces are used to move data between
2528 * the host and the device. On the one hand, the device has
2529 * analog-to-digital (ADC) and digital-to-analog (DAC) converters
2530 * which have their own low-jitter clock source. On other hand, the
2531 * USB host runs a bus clock using another clock source. So both
2532 * drift. That's why, the device sends feedback to the driver for the
2533 * host to adjust continuously its data rate, hence the need for sync
2534 * endpoints.
2535 */
2536int
2537uaudio_process_as(struct uaudio_softc *sc,
2538 struct uaudio_blob *p, int ifnum, int altnum, int nep)
2539{
2540 struct uaudio_alt *a, *anext, **pa;
2541 struct uaudio_blob dp;
2542 unsigned char *savep;
2543 unsigned int type, subtype;
2544 int ispcm = 0;
2545
2546 a = malloc(sizeof(struct uaudio_alt), M_USBDEV102, M_WAITOK0x0001);
2547 a->mode = 0;
2548 a->nch = 0;
2549 a->v1_rates = 0;
2550 a->data_addr = 0;
2551 a->sync_addr = 0;
2552 a->ifnum = ifnum;
2553 a->altnum = altnum;
2554
2555 while (p->rptr != p->wptr) {
25
Assuming field 'rptr' is not equal to field 'wptr'
26
Loop condition is true. Entering loop body
2556 savep = p->rptr;
2557 if (!uaudio_getdesc(p, &dp))
27
Taking false branch
2558 goto failed;
2559 if (!uaudio_getnum(&dp, 1, &type))
28
Taking false branch
2560 goto failed;
2561 if (type != UDESC_CS_INTERFACE0x24) {
29
Assuming 'type' is equal to UDESC_CS_INTERFACE
30
Taking false branch
2562 p->rptr = savep;
2563 break;
2564 }
2565 if (!uaudio_getnum(&dp, 1, &subtype))
31
Taking false branch
2566 goto failed;
2567 switch (subtype) {
32
Control jumps to 'case 2:' at line 2572
2568 case UAUDIO_AS_GENERAL0x1:
2569 if (!uaudio_process_as_general(sc, &dp, &ispcm, a))
2570 goto failed;
2571 break;
2572 case UAUDIO_AS_FORMAT0x2:
2573 if (!uaudio_process_as_format(sc, &dp, a, &ispcm))
33
Calling 'uaudio_process_as_format'
2574 goto failed;
2575 break;
2576 default:
2577 DPRINTF("%s: unknown desc\n", __func__)do {} while(0);
2578 continue;
2579 }
2580 if (!ispcm) {
2581 DPRINTF("%s: non-pcm iface\n", __func__)do {} while(0);
2582 free(a, M_USBDEV102, sizeof(struct uaudio_alt));
2583 return 1;
2584 }
2585 }
2586
2587 while (p->rptr != p->wptr) {
2588 savep = p->rptr;
2589 if (!uaudio_getdesc(p, &dp))
2590 goto failed;
2591 if (!uaudio_getnum(&dp, 1, &type))
2592 goto failed;
2593 if (type == UDESC_CS_ENDPOINT0x25)
2594 continue;
2595 if (type != UDESC_ENDPOINT0x05) {
2596 p->rptr = savep;
2597 break;
2598 }
2599 if (!uaudio_process_as_ep(sc, &dp, a, nep))
2600 goto failed;
2601 }
2602
2603 if (a->mode == 0) {
2604 printf("%s: no data endpoints found\n", DEVNAME(sc)((sc)->dev.dv_xname));
2605 free(a, M_USBDEV102, sizeof(struct uaudio_alt));
2606 return 1;
2607 }
2608
2609 /*
2610 * Append to list of alts, but keep the list sorted by number
2611 * of channels, bits and rate. From the most capable to the
2612 * less capable.
2613 */
2614 pa = &sc->alts;
2615 while (1) {
2616 if ((anext = *pa) == NULL((void *)0))
2617 break;
2618 if (a->nch > anext->nch)
2619 break;
2620 else if (a->nch == anext->nch) {
2621 if (a->bits > anext->bits)
2622 break;
2623 else if (sc->version == UAUDIO_V10x100 &&
2624 a->v1_rates > anext->v1_rates)
2625 break;
2626 }
2627 pa = &anext->next;
2628 }
2629 a->next = *pa;
2630 *pa = a;
2631 return 1;
2632failed:
2633 free(a, M_USBDEV102, sizeof(struct uaudio_alt));
2634 return 0;
2635}
2636
2637/*
2638 * Populate the sc->params_list with combinations of play and rec alt
2639 * settings that work together in full-duplex.
2640 */
2641void
2642uaudio_fixup_params(struct uaudio_softc *sc)
2643{
2644 struct uaudio_alt *ap, *ar, *a;
2645 struct uaudio_params *p, **pp;
2646 int rates;
2647
2648 /*
2649 * Add full-duplex parameter combinations.
2650 */
2651 pp = &sc->params_list;
2652 for (ap = sc->alts; ap != NULL((void *)0); ap = ap->next) {
2653 if (ap->mode != AUMODE_PLAY0x01)
2654 continue;
2655 for (ar = sc->alts; ar != NULL((void *)0); ar = ar->next) {
2656 if (ar->mode != AUMODE_RECORD0x02)
2657 continue;
2658 if (ar->bps != ap->bps || ar->bits != ap->bits)
2659 continue;
2660 switch (sc->version) {
2661 case UAUDIO_V10x100:
2662 rates = ap->v1_rates & ar->v1_rates;
2663 if (rates == 0)
2664 continue;
2665 break;
2666 case UAUDIO_V20x200:
2667 /* UAC v2.0 common rates */
2668 rates = 0;
2669 break;
2670 }
2671 p = malloc(sizeof(struct uaudio_params),
2672 M_USBDEV102, M_WAITOK0x0001);
2673 p->palt = ap;
2674 p->ralt = ar;
2675 p->v1_rates = rates;
2676 p->next = NULL((void *)0);
2677 *pp = p;
2678 pp = &p->next;
2679 }
2680 }
2681
2682 /*
2683 * For unidirectional devices, add play-only and or rec-only
2684 * parameters.
2685 */
2686 if (sc->params_list == NULL((void *)0)) {
2687 for (a = sc->alts; a != NULL((void *)0); a = a->next) {
2688 p = malloc(sizeof(struct uaudio_params),
2689 M_USBDEV102, M_WAITOK0x0001);
2690 if (a->mode == AUMODE_PLAY0x01) {
2691 p->palt = a;
2692 p->ralt = NULL((void *)0);
2693 } else {
2694 p->palt = NULL((void *)0);
2695 p->ralt = a;
2696 }
2697 p->v1_rates = a->v1_rates;
2698 p->next = NULL((void *)0);
2699 *pp = p;
2700 pp = &p->next;
2701 }
2702 }
2703}
2704
2705/*
2706 * Parse all descriptors and build configuration of the device.
2707 */
2708int
2709uaudio_process_conf(struct uaudio_softc *sc, struct uaudio_blob *p)
2710{
2711 struct uaudio_blob dp;
2712 struct uaudio_alt *a;
2713 unsigned int type, ifnum, altnum, nep, class, subclass;
2714
2715 while (p->rptr != p->wptr) {
6
Assuming field 'rptr' is not equal to field 'wptr'
7
Loop condition is true. Entering loop body
2716 if (!uaudio_getdesc(p, &dp))
8
Taking false branch
2717 return 0;
2718 if (!uaudio_getnum(&dp, 1, &type))
9
Taking false branch
2719 return 0;
2720 if (type != UDESC_INTERFACE0x04)
10
Assuming 'type' is equal to UDESC_INTERFACE
11
Taking false branch
2721 continue;
2722 if (!uaudio_getnum(&dp, 1, &ifnum))
12
Taking false branch
2723 return 0;
2724 if (!uaudio_getnum(&dp, 1, &altnum))
13
Taking false branch
2725 return 0;
2726 if (!uaudio_getnum(&dp, 1, &nep))
14
Taking false branch
2727 return 0;
2728 if (!uaudio_getnum(&dp, 1, &class))
15
Taking false branch
2729 return 0;
2730 if (!uaudio_getnum(&dp, 1, &subclass))
16
Taking false branch
2731 return 0;
2732 if (class != UICLASS_AUDIO0x01) {
17
Assuming 'class' is equal to UICLASS_AUDIO
18
Taking false branch
2733 DPRINTF("%s: skipped iface\n", __func__)do {} while(0);
2734 continue;
2735 }
2736
2737 switch (subclass) {
19
Control jumps to 'case 2:' at line 2750
2738 case UISUBCLASS_AUDIOCONTROL1:
2739 if (usbd_iface_claimed(sc->udev, ifnum)) {
2740 DPRINTF("%s: %d: AC already claimed\n", __func__, ifnum)do {} while(0);
2741 break;
2742 }
2743 if (sc->unit_list != NULL((void *)0)) {
2744 DPRINTF("%s: >1 AC ifaces\n", __func__)do {} while(0);
2745 goto done;
2746 }
2747 if (!uaudio_process_ac(sc, p, ifnum))
2748 return 0;
2749 break;
2750 case UISUBCLASS_AUDIOSTREAM2:
2751 if (usbd_iface_claimed(sc->udev, ifnum)) {
20
Assuming the condition is false
21
Taking false branch
2752 DPRINTF("%s: %d: AS already claimed\n", __func__, ifnum)do {} while(0);
2753 break;
2754 }
2755 if (nep == 0) {
22
Assuming 'nep' is not equal to 0
23
Taking false branch
2756 DPRINTF("%s: "do {} while(0)
2757 "stop altnum %d\n", __func__, altnum)do {} while(0);
2758 break; /* 0 is "stop sound", skip it */
2759 }
2760 if (!uaudio_process_as(sc, p, ifnum, altnum, nep))
24
Calling 'uaudio_process_as'
2761 return 0;
2762 }
2763 }
2764done:
2765 uaudio_fixup_params(sc);
2766
2767 /*
2768 * Claim all interfaces we use. This prevents other uaudio(4)
2769 * devices from trying to use them.
2770 */
2771 for (a = sc->alts; a != NULL((void *)0); a = a->next)
2772 usbd_claim_iface(sc->udev, a->ifnum);
2773
2774 usbd_claim_iface(sc->udev, sc->ctl_ifnum);
2775
2776 return 1;
2777}
2778
2779/*
2780 * Allocate a isochronous transfer and its bounce-buffers with the
2781 * given maximum framesize and maximum frames per transfer.
2782 */
2783int
2784uaudio_xfer_alloc(struct uaudio_softc *sc, struct uaudio_xfer *xfer,
2785 unsigned int framesize, unsigned int count)
2786{
2787 xfer->usb_xfer = usbd_alloc_xfer(sc->udev);
2788 if (xfer->usb_xfer == NULL((void *)0))
2789 return ENOMEM12;
2790
2791 xfer->buf = usbd_alloc_buffer(xfer->usb_xfer, framesize * count);
2792 if (xfer->buf == NULL((void *)0))
2793 return ENOMEM12;
2794
2795 xfer->sizes = mallocarray(count,
2796 sizeof(xfer->sizes[0]), M_USBDEV102, M_WAITOK0x0001);
2797 if (xfer->sizes == NULL((void *)0))
2798 return ENOMEM12;
2799
2800 return 0;
2801}
2802
2803/*
2804 * Free a isochronous transfer and its bounce-buffers.
2805 */
2806void
2807uaudio_xfer_free(struct uaudio_softc *sc, struct uaudio_xfer *xfer,
2808 unsigned int count)
2809{
2810 if (xfer->usb_xfer != NULL((void *)0)) {
2811 /* frees request buffer as well */
2812 usbd_free_xfer(xfer->usb_xfer);
2813 xfer->usb_xfer = NULL((void *)0);
2814 }
2815 if (xfer->sizes != NULL((void *)0)) {
2816 free(xfer->sizes, M_USBDEV102,
2817 sizeof(xfer->sizes[0]) * count);
2818 xfer->sizes = NULL((void *)0);
2819 }
2820}
2821
2822/*
2823 * Close a stream and free all associated resources
2824 */
2825void
2826uaudio_stream_close(struct uaudio_softc *sc, int dir)
2827{
2828 struct uaudio_stream *s = &sc->pstream;
2829 struct uaudio_alt *a = sc->params->palt;
2830 struct usbd_interface *iface;
2831 int err, i;
2832
2833 if (dir == AUMODE_PLAY0x01) {
2834 s = &sc->pstream;
2835 a = sc->params->palt;
2836 } else {
2837 s = &sc->rstream;
2838 a = sc->params->ralt;
2839 }
2840
2841 if (s->data_pipe) {
2842 usbd_close_pipe(s->data_pipe);
2843 s->data_pipe = NULL((void *)0);
2844 }
2845
2846 if (s->sync_pipe) {
2847 usbd_close_pipe(s->sync_pipe);
2848 s->sync_pipe = NULL((void *)0);
2849 }
2850
2851 err = usbd_device2interface_handle(sc->udev, a->ifnum, &iface);
2852 if (err)
2853 printf("%s: can't get iface handle\n", DEVNAME(sc)((sc)->dev.dv_xname));
2854 else {
2855 err = usbd_set_interface(iface, 0);
2856 if (err)
2857 printf("%s: can't reset interface\n", DEVNAME(sc)((sc)->dev.dv_xname));
2858 }
2859
2860 for (i = 0; i < UAUDIO_NXFERS_MAX8; i++) {
2861 uaudio_xfer_free(sc, s->data_xfers + i, s->nframes_max);
2862 uaudio_xfer_free(sc, s->sync_xfers + i, 1);
2863 }
2864}
2865
2866/*
2867 * Open a stream with the given buffer settings and set the current
2868 * interface alt setting.
2869 */
2870int
2871uaudio_stream_open(struct uaudio_softc *sc, int dir,
2872 void *start, void *end, size_t blksz, void (*intr)(void *), void *arg)
2873{
2874 struct uaudio_stream *s;
2875 struct uaudio_alt *a;
2876 struct usbd_interface *iface;
2877 unsigned char req_buf[4];
2878 unsigned int bpa, spf_max, min_blksz;
2879 int err, clock_id, i;
2880
2881 if (dir == AUMODE_PLAY0x01) {
2882 s = &sc->pstream;
2883 a = sc->params->palt;
2884 } else {
2885 s = &sc->rstream;
2886 a = sc->params->ralt;
2887 }
2888
2889 for (i = 0; i < UAUDIO_NXFERS_MAX8; i++) {
2890 s->data_xfers[i].usb_xfer = NULL((void *)0);
2891 s->data_xfers[i].sizes = NULL((void *)0);
2892 s->sync_xfers[i].usb_xfer = NULL((void *)0);
2893 s->sync_xfers[i].sizes = NULL((void *)0);
2894 }
2895 s->data_pipe = NULL((void *)0);
2896 s->sync_pipe = NULL((void *)0);
2897
2898 s->nframes_mask = 0;
2899 i = a->fps;
2900 while (i > 1000) {
2901 s->nframes_mask = (s->nframes_mask << 1) | 1;
2902 i >>= 1;
2903 }
2904
2905 /* bytes per audio frame */
2906 bpa = a->bps * a->nch;
2907
2908 /* ideal samples per usb frame, fixed-point */
2909 s->spf = (uint64_t)sc->rate * UAUDIO_SPF_DIV327680 / a->fps;
2910
2911 /*
2912 * UAC2.0 spec allows 1000PPM tolerance in sample frequency,
2913 * while USB1.1 requires 1Hz, which is 125PPM at 8kHz. We
2914 * accept as much as 1/256, which is 2500PPM.
2915 */
2916 s->spf_min = (uint64_t)s->spf * 255 / 256;
2917 s->spf_max = (uint64_t)s->spf * 257 / 256;
2918
2919 /* max spf can't exceed the device usb packet size */
2920 spf_max = (a->maxpkt / bpa) * UAUDIO_SPF_DIV327680;
2921 if (s->spf > spf_max) {
2922 printf("%s: samples per frame too large\n", DEVNAME(sc)((sc)->dev.dv_xname));
2923 return EIO5;
2924 }
2925 if (s->spf_max > spf_max)
2926 s->spf_max = spf_max;
2927
2928 /*
2929 * Upon transfer completion the device must reach the audio
2930 * block boundary, which is propagated to upper layers. In the
2931 * worst case, we schedule only frames of spf_max samples, but
2932 * the device returns only frames of spf_min samples; in this
2933 * case the amount actually transferred is at least:
2934 *
2935 * min_blksz = blksz / spf_max * spf_min
2936 *
2937 * As we've UAUDIO_NXFERS outstanding blocks, worst-case
2938 * remaining bytes is at most:
2939 *
2940 * UAUDIO_NXFERS * (blksz - min_blksz)
2941 */
2942 min_blksz = (((uint64_t)blksz << 32) / s->spf_max * s->spf_min) >> 32;
2943
2944 /* round to sample size */
2945 min_blksz -= min_blksz % bpa;
2946
2947 /* finally this is what ensures we cross block boundary */
2948 s->safe_blksz = blksz + UAUDIO_NXFERS_MAX8 * (blksz - min_blksz);
2949
2950 /* max number of (micro-)frames we'll ever use */
2951 s->nframes_max = (uint64_t)(s->safe_blksz / bpa) *
2952 UAUDIO_SPF_DIV327680 / s->spf_min + 1;
2953
2954 /* round to next usb1.1 frame */
2955 s->nframes_max = (s->nframes_max + s->nframes_mask) &
2956 ~s->nframes_mask;
2957
2958 /* this is the max packet size we'll ever need */
2959 s->maxpkt = bpa *
2960 ((s->spf_max + UAUDIO_SPF_DIV327680 - 1) / UAUDIO_SPF_DIV327680);
2961
2962 /* how many xfers we need to fill sc->host_nframes */
2963 s->nxfers = sc->host_nframes / s->nframes_max;
2964 if (s->nxfers > UAUDIO_NXFERS_MAX8)
2965 s->nxfers = UAUDIO_NXFERS_MAX8;
2966
2967 DPRINTF("%s: %s: blksz = %zu, rate = %u, fps = %u\n", __func__,do {} while(0)
2968 dir == AUMODE_PLAY ? "play" : "rec", blksz, sc->rate, a->fps)do {} while(0);
2969 DPRINTF("%s: spf = 0x%x in [0x%x:0x%x]\n", __func__,do {} while(0)
2970 s->spf, s->spf_min, s->spf_max)do {} while(0);
2971 DPRINTF("%s: nframes_max = %u, nframes_mask = %u, maxpkt = %u\n",do {} while(0)
2972 __func__, s->nframes_max, s->nframes_mask, s->maxpkt)do {} while(0);
2973 DPRINTF("%s: safe_blksz = %d, nxfers = %d\n", __func__,do {} while(0)
2974 s->safe_blksz, s->nxfers)do {} while(0);
2975
2976 if (s->nxfers < UAUDIO_NXFERS_MIN2) {
2977 printf("%s: block size too large\n", DEVNAME(sc)((sc)->dev.dv_xname));
2978 return EIO5;
2979 }
2980
2981 /*
2982 * Require at least 2ms block size to ensure no
2983 * transfer exceeds two blocks.
2984 *
2985 * XXX: use s->nframes_mask instead of 1000
2986 */
2987 if (1000 * blksz < 2 * sc->rate * bpa) {
2988 printf("%s: audio block too small\n", DEVNAME(sc)((sc)->dev.dv_xname));
2989 return EIO5;
2990 }
2991
2992 for (i = 0; i < s->nxfers; i++) {
2993 err = uaudio_xfer_alloc(sc, s->data_xfers + i,
2994 s->maxpkt, s->nframes_max);
2995 if (err)
2996 goto failed;
2997 if (a->sync_addr) {
2998 err = uaudio_xfer_alloc(sc, s->sync_xfers + i,
2999 sc->sync_pktsz, 1);
3000 if (err)
3001 goto failed;
3002 }
3003 }
3004
3005 err = usbd_device2interface_handle(sc->udev, a->ifnum, &iface);
3006 if (err) {
3007 printf("%s: can't get iface handle\n", DEVNAME(sc)((sc)->dev.dv_xname));
3008 goto failed;
3009 }
3010
3011 err = usbd_set_interface(iface, a->altnum);
3012 if (err) {
3013 printf("%s: can't set interface\n", DEVNAME(sc)((sc)->dev.dv_xname));
3014 goto failed;
3015 }
3016
3017 /*
3018 * Set the sample rate.
3019 *
3020 * Certain devices are able to lock their clock to the data
3021 * rate and expose no frequency control. In this case, the
3022 * request to set the frequency will fail, but this error is
3023 * safe to ignore.
3024 *
3025 * Such devices expose this capability in the class-specific
3026 * endpoint descriptor (UAC v1.0) or in the clock unit
3027 * descriptor (UAC v2.0) but we don't want to use them for now
3028 * as certain devices have them wrong, missing or misplaced.
3029 */
3030 switch (sc->version) {
3031 case UAUDIO_V10x100:
3032 req_buf[0] = sc->rate;
3033 req_buf[1] = sc->rate >> 8;
3034 req_buf[2] = sc->rate >> 16;
3035 if (!uaudio_req(sc, UT_WRITE_CLASS_ENDPOINT(0x00 | 0x20 | 0x02),
3036 UAUDIO_V1_REQ_SET_CUR0x01, UAUDIO_REQSEL_RATE0x01, 0,
3037 a->data_addr, 0, req_buf, 3)) {
3038 DPRINTF("%s: not setting endpoint rate\n", __func__)do {} while(0);
3039 }
3040 break;
3041 case UAUDIO_V20x200:
3042 req_buf[0] = sc->rate;
3043 req_buf[1] = sc->rate >> 8;
3044 req_buf[2] = sc->rate >> 16;
3045 req_buf[3] = sc->rate >> 24;
3046 clock_id = uaudio_clock_id(sc);
3047 if (clock_id < 0) {
3048 printf("%s: can't get clock id\n", DEVNAME(sc)((sc)->dev.dv_xname));
3049 goto failed;
3050 }
3051 if (!uaudio_req(sc, UT_WRITE_CLASS_INTERFACE(0x00 | 0x20 | 0x01),
3052 UAUDIO_V2_REQ_CUR1, UAUDIO_REQSEL_RATE0x01, 0,
3053 sc->ctl_ifnum, clock_id, req_buf, 4)) {
3054 DPRINTF("%s: not setting clock rate\n", __func__)do {} while(0);
3055 }
3056 break;
3057 }
3058
3059 err = usbd_open_pipe(iface, a->data_addr, 0, &s->data_pipe);
3060 if (err) {
3061 printf("%s: can't open data pipe\n", DEVNAME(sc)((sc)->dev.dv_xname));
3062 goto failed;
3063 }
3064
3065 if (a->sync_addr) {
3066 err = usbd_open_pipe(iface, a->sync_addr, 0, &s->sync_pipe);
3067 if (err) {
3068 printf("%s: can't open sync pipe\n", DEVNAME(sc)((sc)->dev.dv_xname));
3069 goto failed;
3070 }
3071 }
3072
3073 s->data_nextxfer = 0;
3074 s->sync_nextxfer = 0;
3075 s->spf_remain = 0;
3076
3077 s->intr = intr;
3078 s->arg = arg;
3079 s->ring_start = start;
3080 s->ring_end = end;
3081 s->ring_blksz = blksz;
3082
3083 s->ring_pos = s->ring_start;
3084 s->ring_offs = 0;
3085 s->ring_icnt = 0;
3086
3087 s->ubuf_xfer = 0;
3088 s->ubuf_pos = 0;
3089 return 0;
3090
3091failed:
3092 uaudio_stream_close(sc, dir);
3093 return ENOMEM12;
3094}
3095
3096/*
3097 * Adjust play samples-per-frame to keep play and rec streams in sync.
3098 */
3099void
3100uaudio_adjspf(struct uaudio_softc *sc)
3101{
3102 struct uaudio_stream *s = &sc->pstream;
3103 int diff;
3104
3105 if (sc->mode != (AUMODE_RECORD0x02 | AUMODE_PLAY0x01))
3106 return;
3107 if (s->sync_pipe != NULL((void *)0))
3108 return;
3109
3110 /*
3111 * number of samples play stream is ahead of record stream.
3112 */
3113 diff = sc->diff_nsamp;
3114 if (sc->diff_nframes > 0) {
3115 diff -= (uint64_t)sc->pstream.spf *
3116 sc->diff_nframes / UAUDIO_SPF_DIV327680;
3117 } else {
3118 diff += (uint64_t)sc->rstream.spf *
3119 -sc->diff_nframes / UAUDIO_SPF_DIV327680;
3120 }
3121
3122 /*
3123 * adjust samples-per-frames to resync within the next second
3124 */
3125 s->spf = (uint64_t)(sc->rate - diff) * UAUDIO_SPF_DIV327680 / sc->ufps;
3126 if (s->spf > s->spf_max)
3127 s->spf = s->spf_max;
3128 else if (s->spf < s->spf_min)
3129 s->spf = s->spf_min;
3130#ifdef UAUDIO_DEBUG
3131 if (uaudio_debug >= 2)
3132 printf("%s: diff = %d, spf = 0x%x\n", __func__, diff, s->spf);
3133#endif
3134}
3135
3136/*
3137 * Copy one audio block to the xfer buffer.
3138 */
3139void
3140uaudio_pdata_copy(struct uaudio_softc *sc)
3141{
3142 struct uaudio_stream *s = &sc->pstream;
3143 struct uaudio_xfer *xfer;
3144 size_t count, avail;
3145 int index;
3146#ifdef UAUDIO_DEBUG
3147 struct timeval tv;
3148
3149 getmicrotime(&tv);
3150#endif
3151 while (sc->copy_todo > 0 && s->ubuf_xfer < s->nxfers) {
3152 index = s->data_nextxfer + s->ubuf_xfer;
3153 if (index >= s->nxfers)
3154 index -= s->nxfers;
3155 xfer = s->data_xfers + index;
3156 avail = s->ring_end - s->ring_pos;
3157 count = xfer->size - s->ubuf_pos;
3158 if (count > avail)
3159 count = avail;
3160 if (count > sc->copy_todo)
3161 count = sc->copy_todo;
3162#ifdef UAUDIO_DEBUG
3163 if (uaudio_debug >= 2) {
3164 printf("%s: %llu.%06lu: %zd..%zd -> %u:%u..%zu\n",
3165 __func__, tv.tv_sec, tv.tv_usec,
3166 s->ring_pos - s->ring_start,
3167 s->ring_pos - s->ring_start + count,
3168 s->ubuf_xfer, s->ubuf_pos, s->ubuf_pos + count);
3169 }
3170#endif
3171 memcpy(xfer->buf + s->ubuf_pos, s->ring_pos, count)__builtin_memcpy((xfer->buf + s->ubuf_pos), (s->ring_pos
), (count))
;
3172 sc->copy_todo -= count;
3173 s->ring_pos += count;
3174 if (s->ring_pos == s->ring_end) {
3175 s->ring_pos = s->ring_start;
3176 }
3177 s->ubuf_pos += count;
3178 if (s->ubuf_pos == xfer->size) {
3179 usb_syncmem(&xfer->usb_xfer->dmabuf, 0, xfer->size,
3180 BUS_DMASYNC_PREWRITE0x04);
3181 s->ubuf_pos = 0;
3182#ifdef DIAGNOSTIC1
3183 if (s->ubuf_xfer == s->nxfers) {
3184 printf("%s: overflow\n", __func__);
3185 return;
3186 }
3187#endif
3188 s->ubuf_xfer++;
3189 }
3190 }
3191}
3192
3193/*
3194 * Calculate and fill xfer frames sizes.
3195 */
3196void
3197uaudio_pdata_calcsizes(struct uaudio_softc *sc, struct uaudio_xfer *xfer)
3198{
3199#ifdef UAUDIO_DEBUG
3200 struct timeval tv;
3201#endif
3202 struct uaudio_stream *s = &sc->pstream;
3203 struct uaudio_alt *a = sc->params->palt;
3204 unsigned int fsize, bpf;
3205 int done;
3206
3207 bpf = a->bps * a->nch;
3208 done = s->ring_offs;
3209 xfer->nframes = 0;
3210
3211 while (1) {
3212 /*
3213 * if we crossed the next block boundary, we're done
3214 */
3215 if ((xfer->nframes & s->nframes_mask) == 0 &&
3216 done > s->safe_blksz)
3217 break;
3218
3219 /*
3220 * this can't happen, debug only
3221 */
3222 if (xfer->nframes == s->nframes_max) {
3223 printf("%s: too many frames for play xfer: "
3224 "done = %u, blksz = %d\n",
3225 DEVNAME(sc)((sc)->dev.dv_xname), done, s->ring_blksz);
3226 break;
3227 }
3228
3229 /*
3230 * calculate frame size and adjust state
3231 */
3232 s->spf_remain += s->spf;
3233 fsize = s->spf_remain / UAUDIO_SPF_DIV327680 * bpf;
3234 s->spf_remain %= UAUDIO_SPF_DIV327680;
3235 done += fsize;
3236 xfer->sizes[xfer->nframes] = fsize;
3237 xfer->nframes++;
3238 }
3239
3240 xfer->size = done - s->ring_offs;
3241 s->ring_offs = done - s->ring_blksz;
3242
3243#ifdef UAUDIO_DEBUG
3244 if (uaudio_debug >= 3) {
3245 getmicrotime(&tv);
3246 printf("%s: size = %d, offs -> %d\n", __func__,
3247 xfer->size, s->ring_offs);
3248 }
3249#endif
3250 memset(xfer->buf, 0, xfer->size)__builtin_memset((xfer->buf), (0), (xfer->size));
3251}
3252
3253/*
3254 * Submit a play data transfer to the USB driver.
3255 */
3256void
3257uaudio_pdata_xfer(struct uaudio_softc *sc)
3258{
3259#ifdef UAUDIO_DEBUG
3260 struct timeval tv;
3261#endif
3262 struct uaudio_stream *s = &sc->pstream;
3263 struct uaudio_xfer *xfer;
3264 int err;
3265
3266 xfer = s->data_xfers + s->data_nextxfer;
3267
3268#ifdef UAUDIO_DEBUG
3269 if (uaudio_debug >= 3) {
3270 getmicrotime(&tv);
3271 printf("%s: %llu.%06lu: "
3272 "%d bytes, %u frames, remain = 0x%x, offs = %d\n",
3273 __func__, tv.tv_sec, tv.tv_usec,
3274 xfer->size, xfer->nframes,
3275 s->spf_remain, s->ring_offs);
3276 }
3277#endif
3278
3279 /* this can't happen, debug only */
3280 if (xfer->nframes == 0) {
3281 printf("%s: zero frame play xfer\n", DEVNAME(sc)((sc)->dev.dv_xname));
3282 return;
3283 }
3284
3285 /*
3286 * We accept short transfers because in case of babble/stale frames
3287 * the transfer will be short
3288 */
3289 usbd_setup_isoc_xfer(xfer->usb_xfer, s->data_pipe, sc,
3290 xfer->sizes, xfer->nframes,
3291 USBD_NO_COPY0x01 | USBD_SHORT_XFER_OK0x04,
3292 uaudio_pdata_intr);
3293
3294 err = usbd_transfer(xfer->usb_xfer);
3295 if (err != 0 && err != USBD_IN_PROGRESS)
3296 printf("%s: play xfer, err = %d\n", DEVNAME(sc)((sc)->dev.dv_xname), err);
3297
3298 if (++s->data_nextxfer == s->nxfers)
3299 s->data_nextxfer = 0;
3300}
3301
3302/*
3303 * Callback called by the USB driver upon completion of play data transfer.
3304 */
3305void
3306uaudio_pdata_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status)
3307{
3308#ifdef UAUDIO_DEBUG
3309 struct timeval tv;
3310#endif
3311 struct uaudio_softc *sc = arg;
3312 struct uaudio_stream *s = &sc->pstream;
3313 struct uaudio_xfer *xfer;
3314 uint32_t size;
3315 int nintr;
3316
3317 if (status != 0 && status != USBD_IOERROR) {
3318 DPRINTF("%s: xfer status = %d\n", __func__, status)do {} while(0);
3319 return;
3320 }
3321
3322 xfer = s->data_xfers + s->data_nextxfer;
3323 if (xfer->usb_xfer != usb_xfer) {
3324 DPRINTF("%s: wrong xfer\n", __func__)do {} while(0);
3325 return;
3326 }
3327
3328 sc->diff_nsamp += xfer->size /
3329 (sc->params->palt->nch * sc->params->palt->bps);
3330 sc->diff_nframes += xfer->nframes;
3331
3332#ifdef UAUDIO_DEBUG
3333 if (uaudio_debug >= 2) {
3334 getmicrotime(&tv);
3335 printf("%s: %llu.%06lu: %u: %u bytes\n",
3336 __func__, tv.tv_sec, tv.tv_usec,
3337 s->data_nextxfer, xfer->size);
3338 }
3339#endif
3340 usbd_get_xfer_status(usb_xfer, NULL((void *)0), NULL((void *)0), &size, NULL((void *)0));
3341 if (size != xfer->size) {
3342 DPRINTF("%s: %u bytes out of %u: incomplete play xfer\n",do {} while(0)
3343 DEVNAME(sc), size, xfer->size)do {} while(0);
3344 }
3345
3346 /*
3347 * Upper layer call-back may call uaudio_underrun(), which
3348 * needs the current size of this transfer. So, don't
3349 * recalculate the sizes and don't schedule the transfer yet.
3350 */
3351 s->ring_icnt += xfer->size;
3352 nintr = 0;
3353 mtx_enter(&audio_lock);
3354 while (s->ring_icnt >= s->ring_blksz) {
3355 s->intr(s->arg);
3356 s->ring_icnt -= s->ring_blksz;
3357 nintr++;
3358 }
3359 mtx_leave(&audio_lock);
3360 if (nintr != 1)
3361 printf("%s: %d: bad play intr count\n", __func__, nintr);
3362
3363 uaudio_pdata_calcsizes(sc, xfer);
3364 uaudio_pdata_xfer(sc);
3365#ifdef DIAGNOSTIC1
3366 if (s->ubuf_xfer == 0) {
3367 printf("%s: underflow\n", __func__);
3368 return;
3369 }
3370#endif
3371 s->ubuf_xfer--;
3372 uaudio_pdata_copy(sc);
3373}
3374
3375/*
3376 * Submit a play sync transfer to the USB driver.
3377 */
3378void
3379uaudio_psync_xfer(struct uaudio_softc *sc)
3380{
3381#ifdef UAUDIO_DEBUG
3382 struct timeval tv;
3383#endif
3384 struct uaudio_stream *s = &sc->pstream;
3385 struct uaudio_xfer *xfer;
3386 unsigned int i;
3387 int err;
3388
3389 xfer = s->sync_xfers + s->sync_nextxfer;
3390 xfer->nframes = 1;
3391
3392 for (i = 0; i < xfer->nframes; i++)
3393 xfer->sizes[i] = sc->sync_pktsz;
3394
3395 xfer->size = xfer->nframes * sc->sync_pktsz;
3396
3397#ifdef UAUDIO_DEBUG
3398 memset(xfer->buf, 0xd0, sc->sync_pktsz * xfer->nframes)__builtin_memset((xfer->buf), (0xd0), (sc->sync_pktsz *
xfer->nframes))
;
3399#endif
3400
3401 usbd_setup_isoc_xfer(xfer->usb_xfer, s->sync_pipe, sc,
3402 xfer->sizes, xfer->nframes,
3403 USBD_NO_COPY0x01 | USBD_SHORT_XFER_OK0x04,
3404 uaudio_psync_intr);
3405
3406 err = usbd_transfer(xfer->usb_xfer);
3407 if (err != 0 && err != USBD_IN_PROGRESS)
3408 printf("%s: sync play xfer, err = %d\n", DEVNAME(sc)((sc)->dev.dv_xname), err);
3409
3410 if (++s->sync_nextxfer == s->nxfers)
3411 s->sync_nextxfer = 0;
3412
3413#ifdef UAUDIO_DEBUG
3414 if (uaudio_debug >= 3) {
3415 getmicrotime(&tv);
3416 printf("%s: %llu.%06lu: %dB, %d fr\n", __func__,
3417 tv.tv_sec, tv.tv_usec, sc->sync_pktsz, xfer->nframes);
3418 }
3419#endif
3420}
3421
3422/*
3423 * Callback called by the USB driver upon completion of play sync transfer.
3424 */
3425void
3426uaudio_psync_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status)
3427{
3428#ifdef UAUDIO_DEBUG
3429 struct timeval tv;
3430#endif
3431 struct uaudio_softc *sc = arg;
3432 struct uaudio_stream *s = &sc->pstream;
3433 struct uaudio_xfer *xfer;
3434 unsigned char *buf;
3435 unsigned int i;
3436 int32_t val;
3437
3438 if (status != 0) {
3439 DPRINTF("%s: xfer status = %d\n", __func__, status)do {} while(0);
3440 return;
3441 }
3442
3443 xfer = s->sync_xfers + s->sync_nextxfer;
3444 if (xfer->usb_xfer != usb_xfer) {
3445 DPRINTF("%s: wrong xfer\n", __func__)do {} while(0);
3446 return;
3447 }
3448
3449 /* XXX: there's only one frame, the loop is not necessary */
3450
3451 buf = xfer->buf;
3452 for (i = 0; i < xfer->nframes; i++) {
3453 if (xfer->sizes[i] == sc->sync_pktsz) {
3454 val = buf[0] | buf[1] << 8 | buf[2] << 16;
3455 if (sc->sync_pktsz == 4)
3456 val |= xfer->buf[3] << 24;
3457 else
3458 val <<= 2;
3459 val *= UAUDIO_SPF_DIV327680 / (1 << 16);
3460#ifdef UAUDIO_DEBUG
3461 if (uaudio_debug >= 2) {
3462 getmicrotime(&tv);
3463 printf("%s: %llu.%06lu: spf: %08x\n",
3464 __func__, tv.tv_sec, tv.tv_usec, val);
3465 }
3466#endif
3467 if (val > s->spf_max)
3468 s->spf = s->spf_max;
3469 else if (val < s->spf_min)
3470 s->spf = s->spf_min;
3471 else
3472 s->spf = val;
3473 }
3474 buf += sc->sync_pktsz;
3475 }
3476
3477 uaudio_psync_xfer(sc);
3478}
3479
3480/*
3481 * Submit a rec data transfer to the USB driver.
3482 */
3483void
3484uaudio_rdata_xfer(struct uaudio_softc *sc)
3485{
3486#ifdef UAUDIO_DEBUG
3487 struct timeval tv;
3488#endif
3489 struct uaudio_stream *s = &sc->rstream;
3490 struct uaudio_alt *a = sc->params->ralt;
3491 struct uaudio_xfer *xfer;
3492 unsigned int fsize, bpf;
3493 int done;
3494 int err;
3495
3496 xfer = s->data_xfers + s->data_nextxfer;
3497 bpf = a->bps * a->nch;
3498 xfer->nframes = 0;
3499 done = s->ring_offs;
3500
3501 while (1) {
3502 /*
3503 * if we crossed the next block boundary, we're done
3504 */
3505 if ((xfer->nframes & s->nframes_mask) == 0 &&
3506 done > s->safe_blksz) {
3507 done:
3508 xfer->size = done - s->ring_offs;
3509 s->ring_offs = done - s->ring_blksz;
3510 break;
3511 }
3512
3513 /*
3514 * this can't happen, debug only
3515 */
3516 if (xfer->nframes == s->nframes_max) {
3517 printf("%s: too many frames for rec xfer: "
3518 "done = %d, blksz = %d\n",
3519 DEVNAME(sc)((sc)->dev.dv_xname), done, s->ring_blksz);
3520 goto done;
3521 }
3522
3523 /*
3524 * estimate next block using s->spf, but allow
3525 * transfers up to maxpkt
3526 */
3527 s->spf_remain += s->spf;
3528 fsize = s->spf_remain / UAUDIO_SPF_DIV327680 * bpf;
3529 s->spf_remain %= UAUDIO_SPF_DIV327680;
3530 done += fsize;
3531 xfer->sizes[xfer->nframes] = s->maxpkt;
3532 xfer->nframes++;
3533 }
3534
3535#ifdef UAUDIO_DEBUG
3536 if (uaudio_debug >= 3) {
3537 getmicrotime(&tv);
3538 printf("%s: %llu.%06lu: "
3539 "%u fr, %d bytes (max %d), offs = %d\n",
3540 __func__, tv.tv_sec, tv.tv_usec,
3541 xfer->nframes, xfer->size,
3542 s->maxpkt * xfer->nframes, s->ring_offs);
3543 }
3544#endif
3545
3546 /* this can't happen, debug only */
3547 if (xfer->nframes == 0) {
3548 printf("%s: zero frame rec xfer\n", DEVNAME(sc)((sc)->dev.dv_xname));
3549 return;
3550 }
3551
3552#ifdef UAUDIO_DEBUG
3553 memset(xfer->buf, 0xd0, s->maxpkt * xfer->nframes)__builtin_memset((xfer->buf), (0xd0), (s->maxpkt * xfer
->nframes))
;
3554#endif
3555 usbd_setup_isoc_xfer(xfer->usb_xfer, s->data_pipe, sc,
3556 xfer->sizes, xfer->nframes, USBD_NO_COPY0x01 | USBD_SHORT_XFER_OK0x04,
3557 uaudio_rdata_intr);
3558
3559 err = usbd_transfer(xfer->usb_xfer);
3560 if (err != 0 && err != USBD_IN_PROGRESS)
3561 printf("%s: rec xfer, err = %d\n", DEVNAME(sc)((sc)->dev.dv_xname), err);
3562
3563 if (++s->data_nextxfer == s->nxfers)
3564 s->data_nextxfer = 0;
3565}
3566
3567/*
3568 * Callback called by the USB driver upon completion of rec data transfer.
3569 */
3570void
3571uaudio_rdata_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status)
3572{
3573#ifdef UAUDIO_DEBUG
3574 struct timeval tv;
3575#endif
3576 struct uaudio_softc *sc = arg;
3577 struct uaudio_stream *s = &sc->rstream;
3578 struct uaudio_alt *a = sc->params->ralt;
3579 struct uaudio_xfer *xfer;
3580 unsigned char *buf, *framebuf;
3581 unsigned int count, fsize, fsize_min, nframes, bpf;
3582 unsigned int data_size, null_count;
3583 unsigned int nintr;
3584
3585 if (status != 0) {
3586 DPRINTF("%s: xfer status = %d\n", __func__, status)do {} while(0);
3587 return;
3588 }
3589
3590 xfer = s->data_xfers + s->data_nextxfer;
3591 if (xfer->usb_xfer != usb_xfer) {
3592 DPRINTF("%s: wrong xfer\n", __func__)do {} while(0);
3593 return;
3594 }
3595
3596 bpf = a->bps * a->nch;
3597 framebuf = xfer->buf;
3598 nframes = 0;
3599 null_count = 0;
3600 data_size = 0;
3601 fsize_min = s->spf_min / UAUDIO_SPF_DIV327680;
3602 for (nframes = 0; nframes < xfer->nframes; nframes++) {
3603
3604 /*
3605 * Device clock may take some time to lock during which
3606 * we'd receive empty or incomplete packets for which we
3607 * need to generate silence.
3608 */
3609 fsize = xfer->sizes[nframes];
3610 if (fsize < fsize_min) {
3611 s->spf_remain += s->spf;
3612 fsize = s->spf_remain / UAUDIO_SPF_DIV327680 * bpf;
3613 s->spf_remain %= UAUDIO_SPF_DIV327680;
3614 memset(framebuf, 0, fsize)__builtin_memset((framebuf), (0), (fsize));
3615 null_count++;
3616 }
3617 data_size += fsize;
3618
3619 /*
3620 * fill ring from frame buffer, handling
3621 * boundary conditions
3622 */
3623 buf = framebuf;
3624 while (fsize > 0) {
3625 count = s->ring_end - s->ring_pos;
3626 if (count > fsize)
3627 count = fsize;
3628 memcpy(s->ring_pos, buf, count)__builtin_memcpy((s->ring_pos), (buf), (count));
3629 s->ring_pos += count;
3630 if (s->ring_pos == s->ring_end)
3631 s->ring_pos = s->ring_start;
3632 buf += count;
3633 fsize -= count;
3634 }
3635
3636 framebuf += s->maxpkt;
3637 }
3638
3639 s->ring_offs += data_size - xfer->size;
3640 s->ring_icnt += data_size;
3641
3642 sc->diff_nsamp -= data_size /
3643 (sc->params->ralt->nch * sc->params->ralt->bps);
3644 sc->diff_nframes -= xfer->nframes;
3645
3646 sc->adjspf_age += xfer->nframes;
3647 if (sc->adjspf_age >= sc->ufps / 8) {
3648 sc->adjspf_age -= sc->ufps / 8;
3649 uaudio_adjspf(sc);
3650 }
3651
3652#ifdef UAUDIO_DEBUG
3653 if (uaudio_debug >= 2) {
3654 getmicrotime(&tv);
3655 printf("%s: %llu.%06lu: %u: "
3656 "%u bytes of %u, offs -> %d\n",
3657 __func__, tv.tv_sec, tv.tv_usec,
3658 s->data_nextxfer, data_size, xfer->size, s->ring_offs);
3659 }
3660 if (null_count > 0) {
3661 DPRINTF("%s: %u null frames out of %u: incomplete record xfer\n",do {} while(0)
3662 DEVNAME(sc), null_count, xfer->nframes)do {} while(0);
3663 }
3664#endif
3665 uaudio_rdata_xfer(sc);
3666
3667 nintr = 0;
3668 mtx_enter(&audio_lock);
3669 while (s->ring_icnt >= s->ring_blksz) {
3670 s->intr(s->arg);
3671 s->ring_icnt -= s->ring_blksz;
3672 nintr++;
3673 }
3674 mtx_leave(&audio_lock);
3675 if (nintr != 1)
3676 printf("%s: %u: bad rec intr count\n", DEVNAME(sc)((sc)->dev.dv_xname), nintr);
3677}
3678
3679/*
3680 * Start simultaneously playback and recording, unless trigger_input()
3681 * and trigger_output() were not both called yet.
3682 */
3683void
3684uaudio_trigger(struct uaudio_softc *sc)
3685{
3686 int i, s;
3687
3688 if (sc->mode != sc->trigger_mode)
3689 return;
3690
3691 DPRINTF("%s: preparing\n", __func__)do {} while(0);
3692 if (sc->mode & AUMODE_PLAY0x01) {
3693 for (i = 0; i < sc->pstream.nxfers; i++)
3694 uaudio_pdata_calcsizes(sc, sc->pstream.data_xfers + i);
3695
3696 uaudio_pdata_copy(sc);
3697 }
3698
3699 sc->diff_nsamp = 0;
3700 sc->diff_nframes = 0;
3701 sc->adjspf_age = 0;
3702
3703 DPRINTF("%s: starting\n", __func__)do {} while(0);
3704 s = splusb()splraise(0x2);
3705 for (i = 0; i < UAUDIO_NXFERS_MAX8; i++) {
3706 if ((sc->mode & AUMODE_PLAY0x01) && i < sc->pstream.nxfers) {
3707 if (sc->pstream.sync_pipe)
3708 uaudio_psync_xfer(sc);
3709 uaudio_pdata_xfer(sc);
3710 }
3711 if ((sc->mode & AUMODE_RECORD0x02) && i < sc->rstream.nxfers)
3712 uaudio_rdata_xfer(sc);
3713 }
3714 splx(s)spllower(s);
3715}
3716
3717void
3718uaudio_print(struct uaudio_softc *sc)
3719{
3720 struct uaudio_unit *u;
3721 struct uaudio_mixent *m;
3722 struct uaudio_params *p;
3723 int pchan = 0, rchan = 0, async = 0;
3724 int nctl = 0;
3725
3726 for (u = sc->unit_list; u != NULL((void *)0); u = u->unit_next) {
3727 m = u->mixent_list;
3728 while (1) {
3729 uaudio_mixer_skip(&m);
3730 if (m == NULL((void *)0))
3731 break;
3732 m = m->next;
3733 nctl++;
3734 }
3735 }
3736
3737 for (p = sc->params_list; p != NULL((void *)0); p = p->next) {
3738 if (p->palt && p->palt->nch > pchan)
3739 pchan = p->palt->nch;
3740 if (p->ralt && p->ralt->nch > rchan)
3741 rchan = p->ralt->nch;
3742 if (p->palt && p->palt->sync_addr)
3743 async = 1;
3744 if (p->ralt && p->ralt->sync_addr)
3745 async = 1;
3746 }
3747
3748 printf("%s: class v%d, %s, %s, channels: %d play, %d rec, %d ctls\n",
3749 DEVNAME(sc)((sc)->dev.dv_xname),
3750 sc->version >> 8,
3751 sc->ufps == 1000 ? "full-speed" : "high-speed",
3752 async ? "async" : "sync",
3753 pchan, rchan, nctl);
3754}
3755
3756int
3757uaudio_match(struct device *parent, void *match, void *aux)
3758{
3759 struct usb_attach_arg *arg = aux;
3760 struct usb_interface_descriptor *idesc;
3761
3762 if (arg->iface == NULL((void *)0) || arg->device == NULL((void *)0))
3763 return UMATCH_NONE0;
3764
3765 idesc = usbd_get_interface_descriptor(arg->iface);
3766 if (idesc == NULL((void *)0)) {
3767 DPRINTF("%s: couldn't get idesc\n", __func__)do {} while(0);
3768 return UMATCH_NONE0;
3769 }
3770
3771 if (idesc->bInterfaceClass != UICLASS_AUDIO0x01 ||
3772 idesc->bInterfaceSubClass != UISUBCLASS_AUDIOSTREAM2)
3773 return UMATCH_NONE0;
3774
3775 return UMATCH_VENDOR_PRODUCT_CONF_IFACE8;
3776}
3777
3778void
3779uaudio_attach(struct device *parent, struct device *self, void *aux)
3780{
3781 struct uaudio_softc *sc = (struct uaudio_softc *)self;
3782 struct usb_attach_arg *arg = aux;
3783 struct usb_config_descriptor *cdesc;
3784 struct uaudio_blob desc;
3785
3786 /*
3787 * this device has audio AC or AS or MS interface, get the
3788 * full config descriptor and attach audio devices
3789 */
3790
3791 cdesc = usbd_get_config_descriptor(arg->device);
3792 if (cdesc == NULL((void *)0))
1
Assuming 'cdesc' is not equal to NULL
2
Taking false branch
3793 return;
3794
3795 desc.rptr = (unsigned char *)cdesc;
3796 desc.wptr = desc.rptr + UGETW(cdesc->wTotalLength)(*(u_int16_t *)(cdesc->wTotalLength));
3797
3798 sc->udev = arg->device;
3799 sc->unit_list = NULL((void *)0);
3800 sc->names = NULL((void *)0);
3801 sc->alts = NULL((void *)0);
3802 sc->params_list = NULL((void *)0);
3803 sc->clock = NULL((void *)0);
3804 sc->params = NULL((void *)0);
3805 sc->rate = 0;
3806 sc->mode = 0;
3807 sc->trigger_mode = 0;
3808 sc->copy_todo = 0;
3809
3810 /*
3811 * Ideally the USB host controller should expose the number of
3812 * frames we're allowed to schedule, but there's no such
3813 * interface. The uhci(4) driver can buffer up to 128 frames
3814 * (or it crashes), ehci(4) starts recording null frames if we
3815 * exceed 256 (micro-)frames, ohci(4) works with at most 50
3816 * frames.
3817 */
3818 switch (sc->udev->speed) {
3
Control jumps to 'case 3:' at line 3825
3819 case USB_SPEED_LOW1:
3820 case USB_SPEED_FULL2:
3821 sc->ufps = 1000;
3822 sc->sync_pktsz = 3;
3823 sc->host_nframes = 50;
3824 break;
3825 case USB_SPEED_HIGH3:
3826 case USB_SPEED_SUPER4:
3827 sc->ufps = 8000;
3828 sc->sync_pktsz = 4;
3829 sc->host_nframes = 240;
3830 break;
3831 default:
3832 printf("%s: unsupported bus speed\n", DEVNAME(sc)((sc)->dev.dv_xname));
3833 return;
3834 }
3835
3836 if (!uaudio_process_conf(sc, &desc))
4
Execution continues on line 3836
5
Calling 'uaudio_process_conf'
3837 return;
3838
3839#ifdef UAUDIO_DEBUG
3840 if (uaudio_debug)
3841 uaudio_conf_print(sc);
3842#endif
3843 /* print a nice uaudio attach line */
3844 uaudio_print(sc);
3845
3846 audio_attach_mi(&uaudio_hw_if, sc, arg->cookie, &sc->dev);
3847}
3848
3849int
3850uaudio_detach(struct device *self, int flags)
3851{
3852 struct uaudio_softc *sc = (struct uaudio_softc *)self;
3853 struct uaudio_unit *unit;
3854 struct uaudio_params *params;
3855 struct uaudio_alt *alt;
3856 struct uaudio_name *name;
3857 struct uaudio_mixent *mixent;
3858 int rv;
3859
3860 rv = config_detach_children(self, flags);
3861
3862 usbd_ref_wait(sc->udev);
3863
3864 while ((alt = sc->alts) != NULL((void *)0)) {
3865 sc->alts = alt->next;
3866 free(alt, M_USBDEV102, sizeof(struct uaudio_alt));
3867 }
3868
3869 while ((params = sc->params_list) != NULL((void *)0)) {
3870 sc->params_list = params->next;
3871 free(params, M_USBDEV102, sizeof(struct uaudio_params));
3872 }
3873
3874 while ((unit = sc->unit_list) != NULL((void *)0)) {
3875 sc->unit_list = unit->unit_next;
3876 while ((mixent = unit->mixent_list) != NULL((void *)0)) {
3877 unit->mixent_list = mixent->next;
3878 uaudio_ranges_clear(&mixent->ranges);
3879 free(mixent, M_USBDEV102, sizeof(struct uaudio_mixent));
3880 }
3881 uaudio_ranges_clear(&unit->rates);
3882 free(unit, M_USBDEV102, sizeof(struct uaudio_unit));
3883 }
3884
3885 while ((name = sc->names)) {
3886 sc->names = name->next;
3887 free(name, M_USBDEV102, sizeof(struct uaudio_name));
3888 }
3889
3890 return rv;
3891}
3892
3893int
3894uaudio_open(void *self, int flags)
3895{
3896 struct uaudio_softc *sc = self;
3897 struct uaudio_params *p;
3898
3899 if (usbd_is_dying(sc->udev))
3900 return EIO5;
3901
3902 usbd_ref_incr(sc->udev);
3903
3904 flags &= (FREAD0x0001 | FWRITE0x0002);
3905
3906 for (p = sc->params_list; p != NULL((void *)0); p = p->next) {
3907 switch (flags) {
3908 case FWRITE0x0002:
3909 if (!p->palt)
3910 break;
3911 sc->mode = AUMODE_PLAY0x01;
3912 return 0;
3913 case FREAD0x0001:
3914 if (!p->ralt)
3915 break;
3916 sc->mode = AUMODE_RECORD0x02;
3917 return 0;
3918 case FREAD0x0001 | FWRITE0x0002:
3919 if (!(p->ralt && p->palt))
3920 break;
3921 sc->mode = AUMODE_RECORD0x02 | AUMODE_PLAY0x01;
3922 return 0;
3923 }
3924 }
3925
3926 usbd_ref_decr(sc->udev);
3927 return ENXIO6;
3928}
3929
3930void
3931uaudio_close(void *self)
3932{
3933 struct uaudio_softc *sc = self;
3934
3935 sc->mode = 0;
3936 usbd_ref_decr(sc->udev);
3937}
3938
3939int
3940uaudio_set_params(void *self, int setmode, int usemode,
3941 struct audio_params *ap, struct audio_params *ar)
3942{
3943 struct uaudio_softc *sc = (struct uaudio_softc *)self;
3944 struct uaudio_params *p, *best_mode, *best_rate, *best_nch;
3945 int rate, rateindex;
3946
3947#ifdef DIAGNOSTIC1
3948 if (setmode != usemode || setmode != sc->mode) {
3949 printf("%s: bad call to uaudio_set_params()\n", DEVNAME(sc)((sc)->dev.dv_xname));
3950 return EINVAL22;
3951 }
3952 if (sc->mode == 0) {
3953 printf("%s: uaudio_set_params(): not open\n", DEVNAME(sc)((sc)->dev.dv_xname));
3954 return EINVAL22;
3955 }
3956#endif
3957 /*
3958 * audio(4) layer requests equal play and record rates
3959 */
3960 rate = (sc->mode & AUMODE_PLAY0x01) ? ap->sample_rate : ar->sample_rate;
3961 rateindex = uaudio_rates_indexof(~0, rate);
3962
3963 DPRINTF("%s: rate %d -> %d (index %d)\n", __func__,do {} while(0)
3964 rate, uaudio_rates[rateindex], rateindex)do {} while(0);
3965
3966 best_mode = best_rate = best_nch = NULL((void *)0);
3967
3968 for (p = sc->params_list; p != NULL((void *)0); p = p->next) {
3969
3970 /* test if params match the requested mode */
3971 if (sc->mode & AUMODE_PLAY0x01) {
3972 if (p->palt == NULL((void *)0))
3973 continue;
3974 }
3975 if (sc->mode & AUMODE_RECORD0x02) {
3976 if (p->ralt == NULL((void *)0))
3977 continue;
3978 }
3979 if (best_mode == NULL((void *)0))
3980 best_mode = p;
3981
3982 /* test if params match the requested rate */
3983 if ((uaudio_getrates(sc, p) & (1 << rateindex)) == 0)
3984 continue;
3985 if (best_rate == NULL((void *)0))
3986 best_rate = p;
3987
3988 /* test if params match the requested channel counts */
3989 if (sc->mode & AUMODE_PLAY0x01) {
3990 if (p->palt->nch != ap->channels)
3991 continue;
3992 }
3993 if (sc->mode & AUMODE_RECORD0x02) {
3994 if (p->ralt->nch != ar->channels)
3995 continue;
3996 }
3997 if (best_nch == NULL((void *)0))
3998 best_nch = p;
3999
4000 /* test if params match the requested precision */
4001 if (sc->mode & AUMODE_PLAY0x01) {
4002 if (p->palt->bits != ap->precision)
4003 continue;
4004 }
4005 if (sc->mode & AUMODE_RECORD0x02) {
4006 if (p->ralt->bits != ar->precision)
4007 continue;
4008 }
4009
4010 /* everything matched, we're done */
4011 break;
4012 }
4013
4014 if (p == NULL((void *)0)) {
4015 if (best_nch)
4016 p = best_nch;
4017 else if (best_rate)
4018 p = best_rate;
4019 else if (best_mode)
4020 p = best_mode;
4021 else
4022 return ENOTTY25;
4023 }
4024
4025 /*
4026 * Recalculate rate index, because the chosen parameters
4027 * may not support the requested one
4028 */
4029 rateindex = uaudio_rates_indexof(uaudio_getrates(sc, p), rate);
4030 if (rateindex < 0)
4031 return ENOTTY25;
4032
4033 sc->params = p;
4034 sc->rate = uaudio_rates[rateindex];
4035
4036 DPRINTF("%s: rate = %u\n", __func__, sc->rate)do {} while(0);
4037
4038 if (sc->mode & AUMODE_PLAY0x01) {
4039 ap->sample_rate = sc->rate;
4040 ap->precision = p->palt->bits;
4041 ap->encoding = AUDIO_ENCODING_SLINEAR_LE6;
4042 ap->bps = p->palt->bps;
4043 ap->msb = 1;
4044 ap->channels = p->palt->nch;
4045 }
4046 if (sc->mode & AUMODE_RECORD0x02) {
4047 ar->sample_rate = sc->rate;
4048 ar->precision = p->ralt->bits;
4049 ar->encoding = AUDIO_ENCODING_SLINEAR_LE6;
4050 ar->bps = p->ralt->bps;
4051 ar->msb = 1;
4052 ar->channels = p->ralt->nch;
4053 }
4054
4055 return 0;
4056}
4057
4058unsigned int
4059uaudio_set_blksz(void *self, int mode,
4060 struct audio_params *p, struct audio_params *r, unsigned int blksz)
4061{
4062 struct uaudio_softc *sc = self;
4063 unsigned int fps, fps_min;
4064 unsigned int blksz_max, blksz_min;
4065
4066 /*
4067 * minimum block size is two transfers, see uaudio_stream_open()
4068 */
4069 fps_min = sc->ufps;
4070 if (mode & AUMODE_PLAY0x01) {
4071 fps = sc->params->palt->fps;
4072 if (fps_min > fps)
4073 fps_min = fps;
4074 }
4075 if (mode & AUMODE_RECORD0x02) {
4076 fps = sc->params->ralt->fps;
4077 if (fps_min > fps)
4078 fps_min = fps;
4079 }
4080 blksz_min = (sc->rate * 2 + fps_min - 1) / fps_min;
4081
4082 /*
4083 * max block size is only limited by the number of frames the
4084 * host can schedule
4085 */
4086 blksz_max = sc->rate * (sc->host_nframes / UAUDIO_NXFERS_MIN2) /
4087 sc->ufps * 85 / 100;
4088
4089 if (blksz > blksz_max)
4090 blksz = blksz_max;
4091 else if (blksz < blksz_min)
4092 blksz = blksz_min;
4093
4094 return blksz;
4095}
4096
4097int
4098uaudio_trigger_output(void *self, void *start, void *end, int blksz,
4099 void (*intr)(void *), void *arg, struct audio_params *param)
4100{
4101 struct uaudio_softc *sc = self;
4102 int err;
4103
4104 err = uaudio_stream_open(sc,
4105 AUMODE_PLAY0x01, start, end, blksz, intr, arg);
4106 if (err)
4107 return err;
4108
4109 sc->trigger_mode |= AUMODE_PLAY0x01;
4110 uaudio_trigger(sc);
4111 return 0;
4112}
4113
4114int
4115uaudio_trigger_input(void *self, void *start, void *end, int blksz,
4116 void (*intr)(void *), void *arg, struct audio_params *param)
4117{
4118 struct uaudio_softc *sc = self;
4119 int err;
4120
4121 err = uaudio_stream_open(sc,
4122 AUMODE_RECORD0x02, start, end, blksz, intr, arg);
4123 if (err)
4124 return err;
4125
4126 sc->trigger_mode |= AUMODE_RECORD0x02;
4127 uaudio_trigger(sc);
4128 return 0;
4129}
4130
4131void
4132uaudio_copy_output(void *self, size_t todo)
4133{
4134 struct uaudio_softc *sc = (struct uaudio_softc *)self;
4135 int s;
4136
4137 s = splusb()splraise(0x2);
4138 sc->copy_todo += todo;
4139
4140#ifdef UAUDIO_DEBUG
4141 if (uaudio_debug >= 3) {
4142 printf("%s: copy_todo -> %zd (+%zd)\n", __func__,
4143 sc->copy_todo, todo);
4144 }
4145#endif
4146
4147 if (sc->mode == sc->trigger_mode)
4148 uaudio_pdata_copy(sc);
4149 splx(s)spllower(s);
4150}
4151
4152void
4153uaudio_underrun(void *self)
4154{
4155 struct uaudio_softc *sc = (struct uaudio_softc *)self;
4156 struct uaudio_stream *s = &sc->pstream;
4157
4158 sc->copy_todo += s->ring_blksz;
4159
4160#ifdef UAUDIO_DEBUG
4161 if (uaudio_debug >= 3)
4162 printf("%s: copy_todo -> %zd\n", __func__, sc->copy_todo);
4163#endif
4164
4165 /* copy data (actually silence) produced by the audio(4) layer */
4166 uaudio_pdata_copy(sc);
4167}
4168
4169int
4170uaudio_halt_output(void *self)
4171{
4172 struct uaudio_softc *sc = (struct uaudio_softc *)self;
4173
4174 uaudio_stream_close(sc, AUMODE_PLAY0x01);
4175 sc->trigger_mode &= ~AUMODE_PLAY0x01;
4176 sc->copy_todo = 0;
4177 return 0;
4178}
4179
4180int
4181uaudio_halt_input(void *self)
4182{
4183 struct uaudio_softc *sc = (struct uaudio_softc *)self;
4184
4185 uaudio_stream_close(sc, AUMODE_RECORD0x02);
4186 sc->trigger_mode &= ~AUMODE_RECORD0x02;
4187 return 0;
4188}
4189
4190int
4191uaudio_get_port_do(struct uaudio_softc *sc, struct mixer_ctrl *ctl)
4192{
4193 struct uaudio_unit *u;
4194 struct uaudio_mixent *m;
4195 unsigned char req_buf[4];
4196 struct uaudio_blob p;
4197 int i, nch, val, req_num;
4198
4199 if (!uaudio_mixer_byindex(sc, ctl->dev, &u, &m))
4200 return ENOENT2;
4201
4202 switch (sc->version) {
4203 case UAUDIO_V10x100:
4204 req_num = UAUDIO_V1_REQ_GET_CUR0x81;
4205 break;
4206 case UAUDIO_V20x200:
4207 req_num = UAUDIO_V2_REQ_CUR1;
4208 }
4209
4210 switch (m->type) {
4211 case UAUDIO_MIX_SW0:
4212 p.rptr = p.wptr = req_buf;
4213 if (!uaudio_req(sc,
4214 UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
4215 req_num,
4216 m->req_sel,
4217 m->chan < 0 ? 0 : m->chan,
4218 sc->ctl_ifnum,
4219 u->id,
4220 req_buf,
4221 1))
4222 return EIO5;
4223 p.wptr++;
4224 if (!uaudio_getnum(&p, 1, &val))
4225 return EIO5;
4226 ctl->un.ord = !!val;
4227 break;
4228 case UAUDIO_MIX_NUM1:
4229 nch = uaudio_mixer_nchan(m, NULL((void *)0));
4230 ctl->un.value.num_channels = nch;
4231 for (i = 0; i < nch; i++) {
4232 p.rptr = p.wptr = req_buf;
4233 if (!uaudio_req(sc,
4234 UT_READ_CLASS_INTERFACE(0x80 | 0x20 | 0x01),
4235 req_num,
4236 m->req_sel,
4237 m->chan < 0 ? 0 : i + 1,
4238 sc->ctl_ifnum,
4239 u->id,
4240 req_buf,
4241 2))
4242 return EIO5;
4243 p.wptr += 2;
4244 if (!uaudio_getnum(&p, 2, &val))
4245 return EIO5;
4246 ctl->un.value.level[i] =
4247 uaudio_ranges_decode(&m->ranges,
4248 uaudio_sign_expand(val, 2));
4249 m = m->next;
4250 }
4251 break;
4252 case UAUDIO_MIX_ENUM2:
4253 /* XXX: not used yet */
4254 break;
4255 }
4256 return 0;
4257}
4258
4259int
4260uaudio_set_port_do(struct uaudio_softc *sc, struct mixer_ctrl *ctl)
4261{
4262 struct uaudio_unit *u;
4263 struct uaudio_mixent *m;
4264 unsigned char req_buf[4];
4265 unsigned int val;
4266 int i, nch;
4267
4268 if (!uaudio_mixer_byindex(sc, ctl->dev, &u, &m))
4269 return ENOENT2;
4270
4271 switch (m->type) {
4272 case UAUDIO_MIX_SW0:
4273 if (ctl->un.ord < 0 || ctl->un.ord > 1)
4274 return EINVAL22;
4275 req_buf[0] = ctl->un.ord;
4276 if (!uaudio_req(sc,
4277 UT_WRITE_CLASS_INTERFACE(0x00 | 0x20 | 0x01),
4278 UAUDIO_V1_REQ_SET_CUR0x01,
4279 m->req_sel,
4280 m->chan < 0 ? 0 : m->chan,
4281 sc->ctl_ifnum,
4282 u->id,
4283 req_buf,
4284 1))
4285 return EIO5;
4286 break;
4287 case UAUDIO_MIX_NUM1:
4288 nch = uaudio_mixer_nchan(m, NULL((void *)0));
4289 ctl->un.value.num_channels = nch;
4290 for (i = 0; i < nch; i++) {
4291 val = uaudio_ranges_encode(&m->ranges,
4292 ctl->un.value.level[i]);
4293 DPRINTF("%s: ch %d, ctl %d, num val %d\n", __func__,do {} while(0)
4294 i, ctl->un.value.level[i], val)do {} while(0);
4295 req_buf[0] = val;
4296 req_buf[1] = val >> 8;
4297 if (!uaudio_req(sc,
4298 UT_WRITE_CLASS_INTERFACE(0x00 | 0x20 | 0x01),
4299 UAUDIO_V1_REQ_SET_CUR0x01,
4300 m->req_sel,
4301 m->chan < 0 ? 0 : i + 1,
4302 sc->ctl_ifnum,
4303 u->id,
4304 req_buf,
4305 2))
4306 return EIO5;
4307 m = m->next;
4308 }
4309 break;
4310 case UAUDIO_MIX_ENUM2:
4311 /* XXX: not used yet */
4312 break;
4313 }
4314 return 0;
4315}
4316
4317int
4318uaudio_query_devinfo_do(struct uaudio_softc *sc, struct mixer_devinfo *devinfo)
4319{
4320 struct uaudio_unit *u;
4321 struct uaudio_mixent *m;
4322
4323 devinfo->next = -1;
4324 devinfo->prev = -1;
4325 switch (devinfo->index) {
4326 case UAUDIO_CLASS_IN1:
4327 strlcpy(devinfo->label.name, AudioCinputs"inputs", MAX_AUDIO_DEV_LEN16);
4328 devinfo->type = AUDIO_MIXER_CLASS0;
4329 devinfo->mixer_class = -1;
4330 return 0;
4331 case UAUDIO_CLASS_OUT0:
4332 strlcpy(devinfo->label.name, AudioCoutputs"outputs", MAX_AUDIO_DEV_LEN16);
4333 devinfo->type = AUDIO_MIXER_CLASS0;
4334 devinfo->mixer_class = -1;
4335 return 0;
4336 }
4337
4338 /*
4339 * find the unit & mixent structure for the given index
4340 */
4341 if (!uaudio_mixer_byindex(sc, devinfo->index, &u, &m))
4342 return ENOENT2;
4343
4344 if (strcmp(m->fname, "level") == 0) {
4345 /*
4346 * mixer(4) interface doesn't give a names to level
4347 * controls
4348 */
4349 strlcpy(devinfo->label.name, u->name, MAX_AUDIO_DEV_LEN16);
4350 } else {
4351 if (m->chan == -1) {
4352 snprintf(devinfo->label.name, MAX_AUDIO_DEV_LEN16,
4353 "%s_%s", u->name, m->fname);
4354 } else {
4355 snprintf(devinfo->label.name, MAX_AUDIO_DEV_LEN16,
4356 "%s_%s%u", u->name, m->fname, m->chan);
4357 }
4358 }
4359
4360 devinfo->mixer_class = u->mixer_class;
4361 switch (m->type) {
4362 case UAUDIO_MIX_SW0:
4363 devinfo->type = AUDIO_MIXER_ENUM1;
4364 devinfo->un.e.num_mem = 2;
4365 devinfo->un.e.member[0].ord = 0;
4366 strlcpy(devinfo->un.e.member[0].label.name, "off",
4367 MAX_AUDIO_DEV_LEN16);
4368 devinfo->un.e.member[1].ord = 1;
4369 strlcpy(devinfo->un.e.member[1].label.name, "on",
4370 MAX_AUDIO_DEV_LEN16);
4371 break;
4372 case UAUDIO_MIX_NUM1:
4373 devinfo->type = AUDIO_MIXER_VALUE3;
4374 devinfo->un.v.num_channels = uaudio_mixer_nchan(m, NULL((void *)0));
4375 devinfo->un.v.delta = 1;
4376 break;
4377 case UAUDIO_MIX_ENUM2:
4378 /* XXX: not used yet */
4379 devinfo->type = AUDIO_MIXER_ENUM1;
4380 devinfo->un.e.num_mem = 0;
4381 break;
4382 }
4383 return 0;
4384}
4385
4386int
4387uaudio_get_port(void *arg, struct mixer_ctrl *ctl)
4388{
4389 struct uaudio_softc *sc = arg;
4390 int rc;
4391
4392 usbd_ref_incr(sc->udev);
4393 rc = uaudio_get_port_do(sc, ctl);
4394 usbd_ref_decr(sc->udev);
4395 return rc;
4396}
4397
4398int
4399uaudio_set_port(void *arg, struct mixer_ctrl *ctl)
4400{
4401 struct uaudio_softc *sc = arg;
4402 int rc;
4403
4404 usbd_ref_incr(sc->udev);
4405 rc = uaudio_set_port_do(sc, ctl);
4406 usbd_ref_decr(sc->udev);
4407 return rc;
4408}
4409
4410int
4411uaudio_query_devinfo(void *arg, struct mixer_devinfo *devinfo)
4412{
4413 struct uaudio_softc *sc = arg;
4414 int rc;
4415
4416 usbd_ref_incr(sc->udev);
4417 rc = uaudio_query_devinfo_do(sc, devinfo);
4418 usbd_ref_decr(sc->udev);
4419 return rc;
4420}