Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * (Tentative) USB Audio Driver for ALSA | |
4 | * | |
5 | * Mixer control part | |
6 | * | |
7 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> | |
8 | * | |
9 | * Many codes borrowed from audio.c by | |
10 | * Alan Cox (alan@lxorguk.ukuu.org.uk) | |
11 | * Thomas Sailer (sailer@ife.ee.ethz.ch) | |
1da177e4 LT |
12 | */ |
13 | ||
157a57b6 DM |
14 | /* |
15 | * TODOs, for both the mixer and the streaming interfaces: | |
16 | * | |
17 | * - support for UAC2 effect units | |
18 | * - support for graphical equalizers | |
19 | * - RANGE and MEM set commands (UAC2) | |
20 | * - RANGE and MEM interrupt dispatchers (UAC2) | |
21 | * - audio channel clustering (UAC2) | |
22 | * - audio sample rate converter units (UAC2) | |
23 | * - proper handling of clock multipliers (UAC2) | |
24 | * - dispatch clock change notifications (UAC2) | |
25 | * - stop PCM streams which use a clock that became invalid | |
26 | * - stop PCM streams which use a clock selector that has changed | |
27 | * - parse available sample rates again when clock sources changed | |
28 | */ | |
29 | ||
1da177e4 LT |
30 | #include <linux/bitops.h> |
31 | #include <linux/init.h> | |
32 | #include <linux/list.h> | |
cddaafb9 | 33 | #include <linux/log2.h> |
1da177e4 LT |
34 | #include <linux/slab.h> |
35 | #include <linux/string.h> | |
36 | #include <linux/usb.h> | |
28e1b773 | 37 | #include <linux/usb/audio.h> |
23caaf19 | 38 | #include <linux/usb/audio-v2.h> |
9a2fe9b8 | 39 | #include <linux/usb/audio-v3.h> |
28e1b773 | 40 | |
1da177e4 LT |
41 | #include <sound/core.h> |
42 | #include <sound/control.h> | |
b259b10c | 43 | #include <sound/hwdep.h> |
aafad562 | 44 | #include <sound/info.h> |
7bc5ba7e | 45 | #include <sound/tlv.h> |
1da177e4 LT |
46 | |
47 | #include "usbaudio.h" | |
f0b5e634 | 48 | #include "mixer.h" |
e5779998 | 49 | #include "helper.h" |
7b1eda22 | 50 | #include "mixer_quirks.h" |
88a8516a | 51 | #include "power.h" |
4d1a70da | 52 | |
ebfdeea3 JK |
53 | #define MAX_ID_ELEMS 256 |
54 | ||
1da177e4 LT |
55 | struct usb_audio_term { |
56 | int id; | |
57 | int type; | |
58 | int channels; | |
59 | unsigned int chconfig; | |
60 | int name; | |
61 | }; | |
62 | ||
63 | struct usbmix_name_map; | |
64 | ||
86e07d34 TI |
65 | struct mixer_build { |
66 | struct snd_usb_audio *chip; | |
84957a8a | 67 | struct usb_mixer_interface *mixer; |
1da177e4 LT |
68 | unsigned char *buffer; |
69 | unsigned int buflen; | |
291186e0 | 70 | DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); |
86e07d34 | 71 | struct usb_audio_term oterm; |
1da177e4 | 72 | const struct usbmix_name_map *map; |
8e062ec7 | 73 | const struct usbmix_selector_map *selector_map; |
1da177e4 LT |
74 | }; |
75 | ||
1cdfa9f3 | 76 | /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/ |
7d2b451e SK |
77 | enum { |
78 | USB_XU_CLOCK_RATE = 0xe301, | |
79 | USB_XU_CLOCK_SOURCE = 0xe302, | |
80 | USB_XU_DIGITAL_IO_STATUS = 0xe303, | |
81 | USB_XU_DEVICE_OPTIONS = 0xe304, | |
82 | USB_XU_DIRECT_MONITORING = 0xe305, | |
83 | USB_XU_METERING = 0xe306 | |
84 | }; | |
85 | enum { | |
86 | USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/ | |
87 | USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */ | |
88 | USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */ | |
89 | USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */ | |
90 | }; | |
1da177e4 LT |
91 | |
92 | /* | |
93 | * manual mapping of mixer names | |
94 | * if the mixer topology is too complicated and the parsed names are | |
95 | * ambiguous, add the entries in usbmixer_maps.c. | |
96 | */ | |
f0b5e634 | 97 | #include "mixer_maps.c" |
1da177e4 | 98 | |
c3a3e040 | 99 | static const struct usbmix_name_map * |
17156f23 | 100 | find_map(const struct usbmix_name_map *p, int unitid, int control) |
1da177e4 | 101 | { |
c3a3e040 JK |
102 | if (!p) |
103 | return NULL; | |
1da177e4 | 104 | |
17156f23 | 105 | for (; p->id; p++) { |
c3a3e040 JK |
106 | if (p->id == unitid && |
107 | (!control || !p->control || control == p->control)) | |
108 | return p; | |
1da177e4 | 109 | } |
c3a3e040 | 110 | return NULL; |
1da177e4 LT |
111 | } |
112 | ||
c3a3e040 JK |
113 | /* get the mapped name if the unit matches */ |
114 | static int | |
115 | check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) | |
1da177e4 | 116 | { |
c3a3e040 JK |
117 | if (!p || !p->name) |
118 | return 0; | |
1da177e4 | 119 | |
c3a3e040 JK |
120 | buflen--; |
121 | return strlcpy(buf, p->name, buflen); | |
122 | } | |
123 | ||
5aeee342 TI |
124 | /* ignore the error value if ignore_ctl_error flag is set */ |
125 | #define filter_error(cval, err) \ | |
3360b84b | 126 | ((cval)->head.mixer->ignore_ctl_error ? 0 : (err)) |
5aeee342 | 127 | |
c3a3e040 JK |
128 | /* check whether the control should be ignored */ |
129 | static inline int | |
130 | check_ignored_ctl(const struct usbmix_name_map *p) | |
131 | { | |
132 | if (!p || p->name || p->dB) | |
1da177e4 | 133 | return 0; |
c3a3e040 JK |
134 | return 1; |
135 | } | |
136 | ||
137 | /* dB mapping */ | |
138 | static inline void check_mapped_dB(const struct usbmix_name_map *p, | |
139 | struct usb_mixer_elem_info *cval) | |
140 | { | |
141 | if (p && p->dB) { | |
142 | cval->dBmin = p->dB->min; | |
143 | cval->dBmax = p->dB->max; | |
38b65190 | 144 | cval->initialized = 1; |
1da177e4 | 145 | } |
1da177e4 LT |
146 | } |
147 | ||
8e062ec7 | 148 | /* get the mapped selector source name */ |
86e07d34 | 149 | static int check_mapped_selector_name(struct mixer_build *state, int unitid, |
8e062ec7 CL |
150 | int index, char *buf, int buflen) |
151 | { | |
152 | const struct usbmix_selector_map *p; | |
153 | ||
6bc170e4 | 154 | if (!state->selector_map) |
8e062ec7 CL |
155 | return 0; |
156 | for (p = state->selector_map; p->id; p++) { | |
157 | if (p->id == unitid && index < p->count) | |
158 | return strlcpy(buf, p->names[index], buflen); | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | ||
1da177e4 LT |
163 | /* |
164 | * find an audio control unit with the given unit id | |
165 | */ | |
6bc170e4 DM |
166 | static void *find_audio_control_unit(struct mixer_build *state, |
167 | unsigned char unit) | |
1da177e4 | 168 | { |
67e1daa0 DM |
169 | /* we just parse the header */ |
170 | struct uac_feature_unit_descriptor *hdr = NULL; | |
171 | ||
172 | while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr, | |
173 | USB_DT_CS_INTERFACE)) != NULL) { | |
174 | if (hdr->bLength >= 4 && | |
175 | hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL && | |
9a2fe9b8 | 176 | hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER && |
67e1daa0 DM |
177 | hdr->bUnitID == unit) |
178 | return hdr; | |
1da177e4 | 179 | } |
67e1daa0 | 180 | |
1da177e4 LT |
181 | return NULL; |
182 | } | |
183 | ||
1da177e4 LT |
184 | /* |
185 | * copy a string with the given id | |
186 | */ | |
eccfc1b8 | 187 | static int snd_usb_copy_string_desc(struct snd_usb_audio *chip, |
6bc170e4 | 188 | int index, char *buf, int maxlen) |
1da177e4 | 189 | { |
eccfc1b8 | 190 | int len = usb_string(chip->dev, index, buf, maxlen - 1); |
251552a2 JK |
191 | |
192 | if (len < 0) | |
193 | return 0; | |
194 | ||
1da177e4 LT |
195 | buf[len] = 0; |
196 | return len; | |
197 | } | |
198 | ||
199 | /* | |
200 | * convert from the byte/word on usb descriptor to the zero-based integer | |
201 | */ | |
86e07d34 | 202 | static int convert_signed_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
203 | { |
204 | switch (cval->val_type) { | |
205 | case USB_MIXER_BOOLEAN: | |
206 | return !!val; | |
207 | case USB_MIXER_INV_BOOLEAN: | |
208 | return !val; | |
209 | case USB_MIXER_U8: | |
210 | val &= 0xff; | |
211 | break; | |
212 | case USB_MIXER_S8: | |
213 | val &= 0xff; | |
214 | if (val >= 0x80) | |
215 | val -= 0x100; | |
216 | break; | |
217 | case USB_MIXER_U16: | |
218 | val &= 0xffff; | |
219 | break; | |
220 | case USB_MIXER_S16: | |
221 | val &= 0xffff; | |
222 | if (val >= 0x8000) | |
223 | val -= 0x10000; | |
224 | break; | |
225 | } | |
226 | return val; | |
227 | } | |
228 | ||
229 | /* | |
230 | * convert from the zero-based int to the byte/word for usb descriptor | |
231 | */ | |
86e07d34 | 232 | static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
233 | { |
234 | switch (cval->val_type) { | |
235 | case USB_MIXER_BOOLEAN: | |
236 | return !!val; | |
237 | case USB_MIXER_INV_BOOLEAN: | |
238 | return !val; | |
239 | case USB_MIXER_S8: | |
240 | case USB_MIXER_U8: | |
241 | return val & 0xff; | |
242 | case USB_MIXER_S16: | |
243 | case USB_MIXER_U16: | |
244 | return val & 0xffff; | |
245 | } | |
246 | return 0; /* not reached */ | |
247 | } | |
248 | ||
86e07d34 | 249 | static int get_relative_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 | 250 | { |
6bc170e4 | 251 | if (!cval->res) |
1da177e4 LT |
252 | cval->res = 1; |
253 | if (val < cval->min) | |
254 | return 0; | |
14790f1c TI |
255 | else if (val >= cval->max) |
256 | return (cval->max - cval->min + cval->res - 1) / cval->res; | |
1da177e4 LT |
257 | else |
258 | return (val - cval->min) / cval->res; | |
259 | } | |
260 | ||
86e07d34 | 261 | static int get_abs_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
262 | { |
263 | if (val < 0) | |
264 | return cval->min; | |
6bc170e4 | 265 | if (!cval->res) |
1da177e4 LT |
266 | cval->res = 1; |
267 | val *= cval->res; | |
268 | val += cval->min; | |
269 | if (val > cval->max) | |
270 | return cval->max; | |
271 | return val; | |
272 | } | |
273 | ||
bc18e31c JS |
274 | static int uac2_ctl_value_size(int val_type) |
275 | { | |
276 | switch (val_type) { | |
277 | case USB_MIXER_S32: | |
278 | case USB_MIXER_U32: | |
279 | return 4; | |
280 | case USB_MIXER_S16: | |
281 | case USB_MIXER_U16: | |
282 | return 2; | |
283 | default: | |
284 | return 1; | |
285 | } | |
286 | return 0; /* unreachable */ | |
287 | } | |
288 | ||
1da177e4 LT |
289 | |
290 | /* | |
291 | * retrieve a mixer value | |
292 | */ | |
293 | ||
6bc170e4 DM |
294 | static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, |
295 | int validx, int *value_ret) | |
1da177e4 | 296 | { |
3360b84b | 297 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
1da177e4 LT |
298 | unsigned char buf[2]; |
299 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; | |
300 | int timeout = 10; | |
978520b7 | 301 | int idx = 0, err; |
1da177e4 | 302 | |
47ab1545 | 303 | err = snd_usb_lock_shutdown(chip); |
88a8516a ON |
304 | if (err < 0) |
305 | return -EIO; | |
6bc170e4 | 306 | |
1da177e4 | 307 | while (timeout-- > 0) { |
3360b84b | 308 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
5a9a8eca SB |
309 | err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, |
310 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
311 | validx, idx, buf, val_len); | |
312 | if (err >= val_len) { | |
1da177e4 | 313 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); |
978520b7 TI |
314 | err = 0; |
315 | goto out; | |
5a9a8eca SB |
316 | } else if (err == -ETIMEDOUT) { |
317 | goto out; | |
1da177e4 LT |
318 | } |
319 | } | |
0ba41d91 TI |
320 | usb_audio_dbg(chip, |
321 | "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", | |
322 | request, validx, idx, cval->val_type); | |
978520b7 TI |
323 | err = -EINVAL; |
324 | ||
325 | out: | |
47ab1545 | 326 | snd_usb_unlock_shutdown(chip); |
978520b7 | 327 | return err; |
1da177e4 LT |
328 | } |
329 | ||
6bc170e4 DM |
330 | static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, |
331 | int validx, int *value_ret) | |
23caaf19 | 332 | { |
3360b84b | 333 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
447cae58 KM |
334 | /* enough space for one range */ |
335 | unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)]; | |
23caaf19 | 336 | unsigned char *val; |
447cae58 | 337 | int idx = 0, ret, val_size, size; |
23caaf19 DM |
338 | __u8 bRequest; |
339 | ||
447cae58 KM |
340 | val_size = uac2_ctl_value_size(cval->val_type); |
341 | ||
e8bdb6bb DM |
342 | if (request == UAC_GET_CUR) { |
343 | bRequest = UAC2_CS_CUR; | |
447cae58 | 344 | size = val_size; |
e8bdb6bb DM |
345 | } else { |
346 | bRequest = UAC2_CS_RANGE; | |
447cae58 | 347 | size = sizeof(__u16) + 3 * val_size; |
e8bdb6bb DM |
348 | } |
349 | ||
350 | memset(buf, 0, sizeof(buf)); | |
23caaf19 | 351 | |
47ab1545 | 352 | ret = snd_usb_lock_shutdown(chip) ? -EIO : 0; |
88a8516a ON |
353 | if (ret) |
354 | goto error; | |
355 | ||
47ab1545 TI |
356 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
357 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest, | |
23caaf19 | 358 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
978520b7 | 359 | validx, idx, buf, size); |
47ab1545 | 360 | snd_usb_unlock_shutdown(chip); |
23caaf19 DM |
361 | |
362 | if (ret < 0) { | |
88a8516a | 363 | error: |
0ba41d91 TI |
364 | usb_audio_err(chip, |
365 | "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", | |
366 | request, validx, idx, cval->val_type); | |
23caaf19 DM |
367 | return ret; |
368 | } | |
369 | ||
e8bdb6bb DM |
370 | /* FIXME: how should we handle multiple triplets here? */ |
371 | ||
23caaf19 DM |
372 | switch (request) { |
373 | case UAC_GET_CUR: | |
374 | val = buf; | |
375 | break; | |
376 | case UAC_GET_MIN: | |
377 | val = buf + sizeof(__u16); | |
378 | break; | |
379 | case UAC_GET_MAX: | |
447cae58 | 380 | val = buf + sizeof(__u16) + val_size; |
23caaf19 DM |
381 | break; |
382 | case UAC_GET_RES: | |
447cae58 | 383 | val = buf + sizeof(__u16) + val_size * 2; |
23caaf19 DM |
384 | break; |
385 | default: | |
386 | return -EINVAL; | |
387 | } | |
388 | ||
447cae58 KM |
389 | *value_ret = convert_signed_value(cval, |
390 | snd_usb_combine_bytes(val, val_size)); | |
23caaf19 DM |
391 | |
392 | return 0; | |
393 | } | |
394 | ||
6bc170e4 DM |
395 | static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, |
396 | int validx, int *value_ret) | |
23caaf19 | 397 | { |
9f814105 EZ |
398 | validx += cval->idx_off; |
399 | ||
3360b84b | 400 | return (cval->head.mixer->protocol == UAC_VERSION_1) ? |
23caaf19 DM |
401 | get_ctl_value_v1(cval, request, validx, value_ret) : |
402 | get_ctl_value_v2(cval, request, validx, value_ret); | |
403 | } | |
404 | ||
6bc170e4 DM |
405 | static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, |
406 | int validx, int *value) | |
1da177e4 | 407 | { |
de48c7bc | 408 | return get_ctl_value(cval, UAC_GET_CUR, validx, value); |
1da177e4 LT |
409 | } |
410 | ||
411 | /* channel = 0: master, 1 = first channel */ | |
641b4879 TI |
412 | static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, |
413 | int channel, int *value) | |
1da177e4 | 414 | { |
6bc170e4 DM |
415 | return get_ctl_value(cval, UAC_GET_CUR, |
416 | (cval->control << 8) | channel, | |
417 | value); | |
1da177e4 LT |
418 | } |
419 | ||
eef90451 | 420 | int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval, |
641b4879 TI |
421 | int channel, int index, int *value) |
422 | { | |
423 | int err; | |
424 | ||
425 | if (cval->cached & (1 << channel)) { | |
426 | *value = cval->cache_val[index]; | |
427 | return 0; | |
428 | } | |
429 | err = get_cur_mix_raw(cval, channel, value); | |
430 | if (err < 0) { | |
3360b84b TI |
431 | if (!cval->head.mixer->ignore_ctl_error) |
432 | usb_audio_dbg(cval->head.mixer->chip, | |
0ba41d91 | 433 | "cannot get current value for control %d ch %d: err = %d\n", |
6bc170e4 | 434 | cval->control, channel, err); |
641b4879 TI |
435 | return err; |
436 | } | |
437 | cval->cached |= 1 << channel; | |
438 | cval->cache_val[index] = *value; | |
439 | return 0; | |
440 | } | |
441 | ||
1da177e4 LT |
442 | /* |
443 | * set a mixer value | |
444 | */ | |
445 | ||
7b1eda22 DM |
446 | int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, |
447 | int request, int validx, int value_set) | |
1da177e4 | 448 | { |
3360b84b | 449 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
bc18e31c | 450 | unsigned char buf[4]; |
978520b7 | 451 | int idx = 0, val_len, err, timeout = 10; |
23caaf19 | 452 | |
9f814105 EZ |
453 | validx += cval->idx_off; |
454 | ||
9a2fe9b8 | 455 | |
3360b84b | 456 | if (cval->head.mixer->protocol == UAC_VERSION_1) { |
23caaf19 | 457 | val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; |
9a2fe9b8 | 458 | } else { /* UAC_VERSION_2/3 */ |
bc18e31c | 459 | val_len = uac2_ctl_value_size(cval->val_type); |
23caaf19 DM |
460 | |
461 | /* FIXME */ | |
462 | if (request != UAC_SET_CUR) { | |
0ba41d91 | 463 | usb_audio_dbg(chip, "RANGE setting not yet supported\n"); |
23caaf19 DM |
464 | return -EINVAL; |
465 | } | |
466 | ||
467 | request = UAC2_CS_CUR; | |
468 | } | |
1da177e4 LT |
469 | |
470 | value_set = convert_bytes_value(cval, value_set); | |
471 | buf[0] = value_set & 0xff; | |
472 | buf[1] = (value_set >> 8) & 0xff; | |
bc18e31c JS |
473 | buf[2] = (value_set >> 16) & 0xff; |
474 | buf[3] = (value_set >> 24) & 0xff; | |
47ab1545 TI |
475 | |
476 | err = snd_usb_lock_shutdown(chip); | |
88a8516a ON |
477 | if (err < 0) |
478 | return -EIO; | |
47ab1545 | 479 | |
978520b7 | 480 | while (timeout-- > 0) { |
3360b84b | 481 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
5a9a8eca SB |
482 | err = snd_usb_ctl_msg(chip->dev, |
483 | usb_sndctrlpipe(chip->dev, 0), request, | |
484 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | |
485 | validx, idx, buf, val_len); | |
486 | if (err >= 0) { | |
978520b7 TI |
487 | err = 0; |
488 | goto out; | |
5a9a8eca SB |
489 | } else if (err == -ETIMEDOUT) { |
490 | goto out; | |
88a8516a | 491 | } |
978520b7 | 492 | } |
0ba41d91 | 493 | usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", |
6bc170e4 | 494 | request, validx, idx, cval->val_type, buf[0], buf[1]); |
978520b7 TI |
495 | err = -EINVAL; |
496 | ||
497 | out: | |
47ab1545 | 498 | snd_usb_unlock_shutdown(chip); |
978520b7 | 499 | return err; |
1da177e4 LT |
500 | } |
501 | ||
6bc170e4 DM |
502 | static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, |
503 | int validx, int value) | |
1da177e4 | 504 | { |
7b1eda22 | 505 | return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); |
1da177e4 LT |
506 | } |
507 | ||
eef90451 | 508 | int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, |
641b4879 | 509 | int index, int value) |
1da177e4 | 510 | { |
641b4879 | 511 | int err; |
a6a33259 DM |
512 | unsigned int read_only = (channel == 0) ? |
513 | cval->master_readonly : | |
514 | cval->ch_readonly & (1 << (channel - 1)); | |
515 | ||
516 | if (read_only) { | |
3360b84b | 517 | usb_audio_dbg(cval->head.mixer->chip, |
0ba41d91 | 518 | "%s(): channel %d of control %d is read_only\n", |
a6a33259 DM |
519 | __func__, channel, cval->control); |
520 | return 0; | |
521 | } | |
522 | ||
6bc170e4 DM |
523 | err = snd_usb_mixer_set_ctl_value(cval, |
524 | UAC_SET_CUR, (cval->control << 8) | channel, | |
525 | value); | |
641b4879 TI |
526 | if (err < 0) |
527 | return err; | |
528 | cval->cached |= 1 << channel; | |
529 | cval->cache_val[index] = value; | |
530 | return 0; | |
1da177e4 LT |
531 | } |
532 | ||
7bc5ba7e TI |
533 | /* |
534 | * TLV callback for mixer volume controls | |
535 | */ | |
285de9c0 | 536 | int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
7bc5ba7e TI |
537 | unsigned int size, unsigned int __user *_tlv) |
538 | { | |
539 | struct usb_mixer_elem_info *cval = kcontrol->private_data; | |
b8e1c73f | 540 | DECLARE_TLV_DB_MINMAX(scale, 0, 0); |
7bc5ba7e TI |
541 | |
542 | if (size < sizeof(scale)) | |
543 | return -ENOMEM; | |
0f174b35 TI |
544 | if (cval->min_mute) |
545 | scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE; | |
c3a3e040 JK |
546 | scale[2] = cval->dBmin; |
547 | scale[3] = cval->dBmax; | |
7bc5ba7e TI |
548 | if (copy_to_user(_tlv, scale, sizeof(scale))) |
549 | return -EFAULT; | |
550 | return 0; | |
551 | } | |
1da177e4 LT |
552 | |
553 | /* | |
554 | * parser routines begin here... | |
555 | */ | |
556 | ||
86e07d34 | 557 | static int parse_audio_unit(struct mixer_build *state, int unitid); |
1da177e4 LT |
558 | |
559 | ||
560 | /* | |
561 | * check if the input/output channel routing is enabled on the given bitmap. | |
562 | * used for mixer unit parser | |
563 | */ | |
6bc170e4 DM |
564 | static int check_matrix_bitmap(unsigned char *bmap, |
565 | int ich, int och, int num_outs) | |
1da177e4 LT |
566 | { |
567 | int idx = ich * num_outs + och; | |
568 | return bmap[idx >> 3] & (0x80 >> (idx & 7)); | |
569 | } | |
570 | ||
1da177e4 LT |
571 | /* |
572 | * add an alsa control element | |
573 | * search and increment the index until an empty slot is found. | |
574 | * | |
575 | * if failed, give up and free the control instance. | |
576 | */ | |
577 | ||
3360b84b | 578 | int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list, |
ef9d5970 | 579 | struct snd_kcontrol *kctl) |
1da177e4 | 580 | { |
3360b84b | 581 | struct usb_mixer_interface *mixer = list->mixer; |
1da177e4 | 582 | int err; |
84957a8a | 583 | |
ef9d5970 | 584 | while (snd_ctl_find_id(mixer->chip->card, &kctl->id)) |
1da177e4 | 585 | kctl->id.index++; |
f25ecf8f TI |
586 | err = snd_ctl_add(mixer->chip->card, kctl); |
587 | if (err < 0) { | |
6bc170e4 DM |
588 | usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n", |
589 | err); | |
6639b6c2 | 590 | return err; |
1da177e4 | 591 | } |
3360b84b TI |
592 | list->kctl = kctl; |
593 | list->next_id_elem = mixer->id_elems[list->id]; | |
594 | mixer->id_elems[list->id] = list; | |
6639b6c2 | 595 | return 0; |
1da177e4 LT |
596 | } |
597 | ||
1da177e4 LT |
598 | /* |
599 | * get a terminal name string | |
600 | */ | |
601 | ||
602 | static struct iterm_name_combo { | |
603 | int type; | |
604 | char *name; | |
605 | } iterm_names[] = { | |
606 | { 0x0300, "Output" }, | |
607 | { 0x0301, "Speaker" }, | |
608 | { 0x0302, "Headphone" }, | |
609 | { 0x0303, "HMD Audio" }, | |
610 | { 0x0304, "Desktop Speaker" }, | |
611 | { 0x0305, "Room Speaker" }, | |
612 | { 0x0306, "Com Speaker" }, | |
613 | { 0x0307, "LFE" }, | |
614 | { 0x0600, "External In" }, | |
615 | { 0x0601, "Analog In" }, | |
616 | { 0x0602, "Digital In" }, | |
617 | { 0x0603, "Line" }, | |
618 | { 0x0604, "Legacy In" }, | |
619 | { 0x0605, "IEC958 In" }, | |
620 | { 0x0606, "1394 DA Stream" }, | |
621 | { 0x0607, "1394 DV Stream" }, | |
622 | { 0x0700, "Embedded" }, | |
623 | { 0x0701, "Noise Source" }, | |
624 | { 0x0702, "Equalization Noise" }, | |
625 | { 0x0703, "CD" }, | |
626 | { 0x0704, "DAT" }, | |
627 | { 0x0705, "DCC" }, | |
628 | { 0x0706, "MiniDisk" }, | |
629 | { 0x0707, "Analog Tape" }, | |
630 | { 0x0708, "Phonograph" }, | |
631 | { 0x0709, "VCR Audio" }, | |
632 | { 0x070a, "Video Disk Audio" }, | |
633 | { 0x070b, "DVD Audio" }, | |
634 | { 0x070c, "TV Tuner Audio" }, | |
635 | { 0x070d, "Satellite Rec Audio" }, | |
636 | { 0x070e, "Cable Tuner Audio" }, | |
637 | { 0x070f, "DSS Audio" }, | |
638 | { 0x0710, "Radio Receiver" }, | |
639 | { 0x0711, "Radio Transmitter" }, | |
640 | { 0x0712, "Multi-Track Recorder" }, | |
641 | { 0x0713, "Synthesizer" }, | |
642 | { 0 }, | |
643 | }; | |
644 | ||
eccfc1b8 | 645 | static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm, |
1da177e4 LT |
646 | unsigned char *name, int maxlen, int term_only) |
647 | { | |
648 | struct iterm_name_combo *names; | |
56a23ee5 | 649 | int len; |
1da177e4 | 650 | |
56a23ee5 | 651 | if (iterm->name) { |
eccfc1b8 | 652 | len = snd_usb_copy_string_desc(chip, iterm->name, |
6bc170e4 | 653 | name, maxlen); |
56a23ee5 TI |
654 | if (len) |
655 | return len; | |
656 | } | |
1da177e4 LT |
657 | |
658 | /* virtual type - not a real terminal */ | |
659 | if (iterm->type >> 16) { | |
660 | if (term_only) | |
661 | return 0; | |
662 | switch (iterm->type >> 16) { | |
8b3a087f | 663 | case UAC3_SELECTOR_UNIT: |
6bc170e4 DM |
664 | strcpy(name, "Selector"); |
665 | return 8; | |
8b3a087f | 666 | case UAC3_PROCESSING_UNIT: |
6bc170e4 DM |
667 | strcpy(name, "Process Unit"); |
668 | return 12; | |
8b3a087f | 669 | case UAC3_EXTENSION_UNIT: |
6bc170e4 DM |
670 | strcpy(name, "Ext Unit"); |
671 | return 8; | |
8b3a087f | 672 | case UAC3_MIXER_UNIT: |
6bc170e4 DM |
673 | strcpy(name, "Mixer"); |
674 | return 5; | |
1da177e4 LT |
675 | default: |
676 | return sprintf(name, "Unit %d", iterm->id); | |
677 | } | |
678 | } | |
679 | ||
680 | switch (iterm->type & 0xff00) { | |
681 | case 0x0100: | |
6bc170e4 DM |
682 | strcpy(name, "PCM"); |
683 | return 3; | |
1da177e4 | 684 | case 0x0200: |
6bc170e4 DM |
685 | strcpy(name, "Mic"); |
686 | return 3; | |
1da177e4 | 687 | case 0x0400: |
6bc170e4 DM |
688 | strcpy(name, "Headset"); |
689 | return 7; | |
1da177e4 | 690 | case 0x0500: |
6bc170e4 DM |
691 | strcpy(name, "Phone"); |
692 | return 5; | |
1da177e4 LT |
693 | } |
694 | ||
6bc170e4 | 695 | for (names = iterm_names; names->type; names++) { |
1da177e4 LT |
696 | if (names->type == iterm->type) { |
697 | strcpy(name, names->name); | |
698 | return strlen(names->name); | |
699 | } | |
6bc170e4 DM |
700 | } |
701 | ||
1da177e4 LT |
702 | return 0; |
703 | } | |
704 | ||
6cfd839a JS |
705 | /* |
706 | * Get logical cluster information for UAC3 devices. | |
707 | */ | |
708 | static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id) | |
709 | { | |
710 | struct uac3_cluster_header_descriptor c_header; | |
711 | int err; | |
712 | ||
713 | err = snd_usb_ctl_msg(state->chip->dev, | |
714 | usb_rcvctrlpipe(state->chip->dev, 0), | |
715 | UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR, | |
716 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
717 | cluster_id, | |
718 | snd_usb_ctrl_intf(state->chip), | |
719 | &c_header, sizeof(c_header)); | |
720 | if (err < 0) | |
721 | goto error; | |
722 | if (err != sizeof(c_header)) { | |
723 | err = -EIO; | |
724 | goto error; | |
725 | } | |
726 | ||
727 | return c_header.bNrChannels; | |
728 | ||
729 | error: | |
730 | usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err); | |
731 | return err; | |
732 | } | |
733 | ||
734 | /* | |
735 | * Get number of channels for a Mixer Unit. | |
736 | */ | |
737 | static int uac_mixer_unit_get_channels(struct mixer_build *state, | |
738 | struct uac_mixer_unit_descriptor *desc) | |
739 | { | |
740 | int mu_channels; | |
0bfe5e43 | 741 | void *c; |
6cfd839a | 742 | |
0bfe5e43 | 743 | if (desc->bLength < sizeof(*desc)) |
6cfd839a JS |
744 | return -EINVAL; |
745 | if (!desc->bNrInPins) | |
746 | return -EINVAL; | |
747 | ||
748 | switch (state->mixer->protocol) { | |
749 | case UAC_VERSION_1: | |
750 | case UAC_VERSION_2: | |
751 | default: | |
0bfe5e43 TI |
752 | if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1) |
753 | return 0; /* no bmControls -> skip */ | |
6cfd839a JS |
754 | mu_channels = uac_mixer_unit_bNrChannels(desc); |
755 | break; | |
756 | case UAC_VERSION_3: | |
757 | mu_channels = get_cluster_channels_v3(state, | |
758 | uac3_mixer_unit_wClusterDescrID(desc)); | |
759 | break; | |
760 | } | |
761 | ||
762 | if (!mu_channels) | |
0bfe5e43 TI |
763 | return 0; |
764 | ||
765 | c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); | |
766 | if (c - (void *)desc + (mu_channels - 1) / 8 >= desc->bLength) | |
767 | return 0; /* no bmControls -> skip */ | |
6cfd839a JS |
768 | |
769 | return mu_channels; | |
770 | } | |
771 | ||
1da177e4 LT |
772 | /* |
773 | * parse the source unit recursively until it reaches to a terminal | |
774 | * or a branched unit. | |
775 | */ | |
6bc170e4 DM |
776 | static int check_input_term(struct mixer_build *state, int id, |
777 | struct usb_audio_term *term) | |
1da177e4 | 778 | { |
9a2fe9b8 | 779 | int protocol = state->mixer->protocol; |
09414207 | 780 | int err; |
23caaf19 | 781 | void *p1; |
1da177e4 LT |
782 | |
783 | memset(term, 0, sizeof(*term)); | |
784 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { | |
23caaf19 | 785 | unsigned char *hdr = p1; |
1da177e4 | 786 | term->id = id; |
9a2fe9b8 RB |
787 | |
788 | if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) { | |
789 | switch (hdr[2]) { | |
790 | case UAC_INPUT_TERMINAL: | |
791 | if (protocol == UAC_VERSION_1) { | |
792 | struct uac_input_terminal_descriptor *d = p1; | |
793 | ||
794 | term->type = le16_to_cpu(d->wTerminalType); | |
795 | term->channels = d->bNrChannels; | |
796 | term->chconfig = le16_to_cpu(d->wChannelConfig); | |
797 | term->name = d->iTerminal; | |
798 | } else { /* UAC_VERSION_2 */ | |
799 | struct uac2_input_terminal_descriptor *d = p1; | |
800 | ||
801 | /* call recursively to verify that the | |
802 | * referenced clock entity is valid */ | |
803 | err = check_input_term(state, d->bCSourceID, term); | |
804 | if (err < 0) | |
805 | return err; | |
806 | ||
807 | /* save input term properties after recursion, | |
808 | * to ensure they are not overriden by the | |
809 | * recursion calls */ | |
810 | term->id = id; | |
811 | term->type = le16_to_cpu(d->wTerminalType); | |
812 | term->channels = d->bNrChannels; | |
813 | term->chconfig = le32_to_cpu(d->bmChannelConfig); | |
814 | term->name = d->iTerminal; | |
815 | } | |
816 | return 0; | |
817 | case UAC_FEATURE_UNIT: { | |
818 | /* the header is the same for v1 and v2 */ | |
819 | struct uac_feature_unit_descriptor *d = p1; | |
820 | ||
821 | id = d->bSourceID; | |
822 | break; /* continue to parse */ | |
823 | } | |
824 | case UAC_MIXER_UNIT: { | |
825 | struct uac_mixer_unit_descriptor *d = p1; | |
826 | ||
8b3a087f | 827 | term->type = UAC3_MIXER_UNIT << 16; /* virtual type */ |
9a2fe9b8 RB |
828 | term->channels = uac_mixer_unit_bNrChannels(d); |
829 | term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol); | |
830 | term->name = uac_mixer_unit_iMixer(d); | |
831 | return 0; | |
832 | } | |
833 | case UAC_SELECTOR_UNIT: | |
834 | case UAC2_CLOCK_SELECTOR: { | |
835 | struct uac_selector_unit_descriptor *d = p1; | |
836 | /* call recursively to retrieve the channel info */ | |
837 | err = check_input_term(state, d->baSourceID[0], term); | |
838 | if (err < 0) | |
839 | return err; | |
8b3a087f | 840 | term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */ |
9a2fe9b8 RB |
841 | term->id = id; |
842 | term->name = uac_selector_unit_iSelector(d); | |
843 | return 0; | |
844 | } | |
845 | case UAC1_PROCESSING_UNIT: | |
8b3a087f JS |
846 | /* UAC2_EFFECT_UNIT */ |
847 | if (protocol == UAC_VERSION_1) | |
848 | term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */ | |
849 | else /* UAC_VERSION_2 */ | |
850 | term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */ | |
91c6e15e | 851 | /* fall through */ |
9a2fe9b8 RB |
852 | case UAC1_EXTENSION_UNIT: |
853 | /* UAC2_PROCESSING_UNIT_V2 */ | |
8b3a087f JS |
854 | if (protocol == UAC_VERSION_1 && !term->type) |
855 | term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */ | |
856 | else if (protocol == UAC_VERSION_2 && !term->type) | |
857 | term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */ | |
91c6e15e | 858 | /* fall through */ |
9a2fe9b8 RB |
859 | case UAC2_EXTENSION_UNIT_V2: { |
860 | struct uac_processing_unit_descriptor *d = p1; | |
861 | ||
862 | if (protocol == UAC_VERSION_2 && | |
863 | hdr[2] == UAC2_EFFECT_UNIT) { | |
864 | /* UAC2/UAC1 unit IDs overlap here in an | |
865 | * uncompatible way. Ignore this unit for now. | |
866 | */ | |
867 | return 0; | |
868 | } | |
869 | ||
870 | if (d->bNrInPins) { | |
871 | id = d->baSourceID[0]; | |
872 | break; /* continue to parse */ | |
873 | } | |
8b3a087f JS |
874 | if (!term->type) |
875 | term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */ | |
876 | ||
9a2fe9b8 RB |
877 | term->channels = uac_processing_unit_bNrChannels(d); |
878 | term->chconfig = uac_processing_unit_wChannelConfig(d, protocol); | |
879 | term->name = uac_processing_unit_iProcessing(d, protocol); | |
880 | return 0; | |
881 | } | |
882 | case UAC2_CLOCK_SOURCE: { | |
883 | struct uac_clock_source_descriptor *d = p1; | |
884 | ||
8b3a087f | 885 | term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */ |
9a2fe9b8 RB |
886 | term->id = id; |
887 | term->name = d->iClockSource; | |
888 | return 0; | |
889 | } | |
890 | default: | |
891 | return -ENODEV; | |
892 | } | |
893 | } else { /* UAC_VERSION_3 */ | |
894 | switch (hdr[2]) { | |
895 | case UAC_INPUT_TERMINAL: { | |
896 | struct uac3_input_terminal_descriptor *d = p1; | |
09414207 | 897 | |
9430e547 JS |
898 | /* call recursively to verify that the |
899 | * referenced clock entity is valid */ | |
09414207 DM |
900 | err = check_input_term(state, d->bCSourceID, term); |
901 | if (err < 0) | |
902 | return err; | |
9430e547 JS |
903 | |
904 | /* save input term properties after recursion, | |
905 | * to ensure they are not overriden by the | |
906 | * recursion calls */ | |
907 | term->id = id; | |
908 | term->type = le16_to_cpu(d->wTerminalType); | |
9a2fe9b8 | 909 | |
71066945 JS |
910 | err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID)); |
911 | if (err < 0) | |
912 | return err; | |
913 | term->channels = err; | |
914 | ||
915 | /* REVISIT: UAC3 IT doesn't have channels cfg */ | |
9a2fe9b8 RB |
916 | term->chconfig = 0; |
917 | ||
918 | term->name = le16_to_cpu(d->wTerminalDescrStr); | |
5dae5fd2 EZ |
919 | return 0; |
920 | } | |
9a2fe9b8 RB |
921 | case UAC3_FEATURE_UNIT: { |
922 | struct uac3_feature_unit_descriptor *d = p1; | |
5dae5fd2 | 923 | |
9a2fe9b8 | 924 | id = d->bSourceID; |
1da177e4 LT |
925 | break; /* continue to parse */ |
926 | } | |
9a2fe9b8 RB |
927 | case UAC3_CLOCK_SOURCE: { |
928 | struct uac3_clock_source_descriptor *d = p1; | |
929 | ||
8b3a087f | 930 | term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */ |
9a2fe9b8 RB |
931 | term->id = id; |
932 | term->name = le16_to_cpu(d->wClockSourceStr); | |
933 | return 0; | |
934 | } | |
6cfd839a JS |
935 | case UAC3_MIXER_UNIT: { |
936 | struct uac_mixer_unit_descriptor *d = p1; | |
937 | ||
938 | err = uac_mixer_unit_get_channels(state, d); | |
0bfe5e43 | 939 | if (err <= 0) |
6cfd839a JS |
940 | return err; |
941 | ||
942 | term->channels = err; | |
8b3a087f | 943 | term->type = UAC3_MIXER_UNIT << 16; /* virtual type */ |
6cfd839a JS |
944 | |
945 | return 0; | |
946 | } | |
c77e1ef1 JS |
947 | case UAC3_SELECTOR_UNIT: |
948 | case UAC3_CLOCK_SELECTOR: { | |
949 | struct uac_selector_unit_descriptor *d = p1; | |
950 | /* call recursively to retrieve the channel info */ | |
951 | err = check_input_term(state, d->baSourceID[0], term); | |
952 | if (err < 0) | |
953 | return err; | |
8b3a087f | 954 | term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */ |
c77e1ef1 JS |
955 | term->id = id; |
956 | term->name = 0; /* TODO: UAC3 Class-specific strings */ | |
957 | ||
958 | return 0; | |
959 | } | |
0f292f02 JS |
960 | case UAC3_PROCESSING_UNIT: { |
961 | struct uac_processing_unit_descriptor *d = p1; | |
962 | ||
963 | if (!d->bNrInPins) | |
964 | return -EINVAL; | |
965 | ||
966 | /* call recursively to retrieve the channel info */ | |
967 | err = check_input_term(state, d->baSourceID[0], term); | |
968 | if (err < 0) | |
969 | return err; | |
970 | ||
8b3a087f | 971 | term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */ |
0f292f02 JS |
972 | term->id = id; |
973 | term->name = 0; /* TODO: UAC3 Class-specific strings */ | |
974 | ||
975 | return 0; | |
976 | } | |
9a2fe9b8 RB |
977 | default: |
978 | return -ENODEV; | |
979 | } | |
1da177e4 LT |
980 | } |
981 | } | |
982 | return -ENODEV; | |
983 | } | |
984 | ||
1da177e4 LT |
985 | /* |
986 | * Feature Unit | |
987 | */ | |
988 | ||
989 | /* feature unit control information */ | |
990 | struct usb_feature_control_info { | |
21e9b3e9 | 991 | int control; |
1da177e4 | 992 | const char *name; |
bc18e31c JS |
993 | int type; /* data type for uac1 */ |
994 | int type_uac2; /* data type for uac2 if different from uac1, else -1 */ | |
1da177e4 LT |
995 | }; |
996 | ||
997 | static struct usb_feature_control_info audio_feature_info[] = { | |
21e9b3e9 AC |
998 | { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 }, |
999 | { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 }, | |
1000 | { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 }, | |
1001 | { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 }, | |
1002 | { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 }, | |
1003 | { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */ | |
1004 | { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 }, | |
1005 | { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 }, | |
1006 | { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 }, | |
1007 | { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 }, | |
2e0281d1 | 1008 | /* UAC2 specific */ |
21e9b3e9 AC |
1009 | { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 }, |
1010 | { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 }, | |
1011 | { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 }, | |
1da177e4 LT |
1012 | }; |
1013 | ||
1da177e4 | 1014 | /* private_free callback */ |
eef90451 | 1015 | void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl) |
1da177e4 | 1016 | { |
4d572776 JJ |
1017 | kfree(kctl->private_data); |
1018 | kctl->private_data = NULL; | |
1da177e4 LT |
1019 | } |
1020 | ||
1da177e4 LT |
1021 | /* |
1022 | * interface to ALSA control for feature/mixer units | |
1023 | */ | |
1024 | ||
dcaaf9f2 TI |
1025 | /* volume control quirks */ |
1026 | static void volume_control_quirks(struct usb_mixer_elem_info *cval, | |
1027 | struct snd_kcontrol *kctl) | |
1028 | { | |
3360b84b | 1029 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
0ba41d91 | 1030 | switch (chip->usb_id) { |
d50ed624 | 1031 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
e9a25e04 | 1032 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
d50ed624 EZ |
1033 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { |
1034 | cval->min = 0x0000; | |
1035 | cval->max = 0xffff; | |
1036 | cval->res = 0x00e6; | |
1037 | break; | |
1038 | } | |
1039 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || | |
1040 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { | |
1041 | cval->min = 0x00; | |
1042 | cval->max = 0xff; | |
1043 | break; | |
1044 | } | |
1045 | if (strstr(kctl->id.name, "Effect Return") != NULL) { | |
1046 | cval->min = 0xb706; | |
1047 | cval->max = 0xff7b; | |
1048 | cval->res = 0x0073; | |
1049 | break; | |
1050 | } | |
1051 | if ((strstr(kctl->id.name, "Playback Volume") != NULL) || | |
1052 | (strstr(kctl->id.name, "Effect Send") != NULL)) { | |
1053 | cval->min = 0xb5fb; /* -73 dB = 0xb6ff */ | |
1054 | cval->max = 0xfcfe; | |
1055 | cval->res = 0x0073; | |
1056 | } | |
1057 | break; | |
1058 | ||
d34bf148 FH |
1059 | case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */ |
1060 | case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */ | |
1061 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { | |
0ba41d91 TI |
1062 | usb_audio_info(chip, |
1063 | "set quirk for FTU Effect Duration\n"); | |
d34bf148 FH |
1064 | cval->min = 0x0000; |
1065 | cval->max = 0x7f00; | |
1066 | cval->res = 0x0100; | |
1067 | break; | |
1068 | } | |
1069 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || | |
1070 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { | |
0ba41d91 TI |
1071 | usb_audio_info(chip, |
1072 | "set quirks for FTU Effect Feedback/Volume\n"); | |
d34bf148 FH |
1073 | cval->min = 0x00; |
1074 | cval->max = 0x7f; | |
1075 | break; | |
1076 | } | |
1077 | break; | |
1078 | ||
21493316 FC |
1079 | case USB_ID(0x0d8c, 0x0103): |
1080 | if (!strcmp(kctl->id.name, "PCM Playback Volume")) { | |
1081 | usb_audio_info(chip, | |
1082 | "set volume quirk for CM102-A+/102S+\n"); | |
1083 | cval->min = -256; | |
1084 | } | |
1085 | break; | |
1086 | ||
dcaaf9f2 TI |
1087 | case USB_ID(0x0471, 0x0101): |
1088 | case USB_ID(0x0471, 0x0104): | |
1089 | case USB_ID(0x0471, 0x0105): | |
1090 | case USB_ID(0x0672, 0x1041): | |
1091 | /* quirk for UDA1321/N101. | |
1092 | * note that detection between firmware 2.1.1.7 (N101) | |
1093 | * and later 2.1.1.21 is not very clear from datasheets. | |
1094 | * I hope that the min value is -15360 for newer firmware --jk | |
1095 | */ | |
1096 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && | |
1097 | cval->min == -15616) { | |
0ba41d91 | 1098 | usb_audio_info(chip, |
dcaaf9f2 TI |
1099 | "set volume quirk for UDA1321/N101 chip\n"); |
1100 | cval->max = -256; | |
1101 | } | |
1102 | break; | |
1103 | ||
1104 | case USB_ID(0x046d, 0x09a4): | |
1105 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { | |
0ba41d91 | 1106 | usb_audio_info(chip, |
dcaaf9f2 TI |
1107 | "set volume quirk for QuickCam E3500\n"); |
1108 | cval->min = 6080; | |
1109 | cval->max = 8768; | |
1110 | cval->res = 192; | |
1111 | } | |
1112 | break; | |
1113 | ||
e805ca8b | 1114 | case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */ |
dcaaf9f2 TI |
1115 | case USB_ID(0x046d, 0x0808): |
1116 | case USB_ID(0x046d, 0x0809): | |
64559311 | 1117 | case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */ |
36691e1b | 1118 | case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */ |
55c0008b | 1119 | case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */ |
11e7064f | 1120 | case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */ |
140d37de | 1121 | case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */ |
1ef9f058 | 1122 | case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */ |
dcaaf9f2 | 1123 | case USB_ID(0x046d, 0x0991): |
82ffb6fc | 1124 | case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */ |
dcaaf9f2 TI |
1125 | /* Most audio usb devices lie about volume resolution. |
1126 | * Most Logitech webcams have res = 384. | |
82ffb6fc | 1127 | * Probably there is some logitech magic behind this number --fishor |
dcaaf9f2 TI |
1128 | */ |
1129 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { | |
0ba41d91 | 1130 | usb_audio_info(chip, |
dcaaf9f2 TI |
1131 | "set resolution quirk: cval->res = 384\n"); |
1132 | cval->res = 384; | |
1133 | } | |
1134 | break; | |
dcaaf9f2 TI |
1135 | } |
1136 | } | |
1137 | ||
1da177e4 LT |
1138 | /* |
1139 | * retrieve the minimum and maximum values for the specified control | |
1140 | */ | |
dcaaf9f2 TI |
1141 | static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval, |
1142 | int default_min, struct snd_kcontrol *kctl) | |
1da177e4 LT |
1143 | { |
1144 | /* for failsafe */ | |
1145 | cval->min = default_min; | |
1146 | cval->max = cval->min + 1; | |
1147 | cval->res = 1; | |
c3a3e040 | 1148 | cval->dBmin = cval->dBmax = 0; |
1da177e4 LT |
1149 | |
1150 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
1151 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
1152 | cval->initialized = 1; | |
1153 | } else { | |
1154 | int minchn = 0; | |
1155 | if (cval->cmask) { | |
1156 | int i; | |
1157 | for (i = 0; i < MAX_CHANNELS; i++) | |
1158 | if (cval->cmask & (1 << i)) { | |
1159 | minchn = i + 1; | |
1160 | break; | |
1161 | } | |
1162 | } | |
de48c7bc DM |
1163 | if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || |
1164 | get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { | |
3360b84b | 1165 | usb_audio_err(cval->head.mixer->chip, |
0ba41d91 | 1166 | "%d:%d: cannot get min/max values for control %d (id %d)\n", |
3360b84b TI |
1167 | cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip), |
1168 | cval->control, cval->head.id); | |
1da177e4 LT |
1169 | return -EINVAL; |
1170 | } | |
6bc170e4 DM |
1171 | if (get_ctl_value(cval, UAC_GET_RES, |
1172 | (cval->control << 8) | minchn, | |
1173 | &cval->res) < 0) { | |
1da177e4 LT |
1174 | cval->res = 1; |
1175 | } else { | |
1176 | int last_valid_res = cval->res; | |
1177 | ||
1178 | while (cval->res > 1) { | |
7b1eda22 | 1179 | if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, |
6bc170e4 DM |
1180 | (cval->control << 8) | minchn, |
1181 | cval->res / 2) < 0) | |
1da177e4 LT |
1182 | break; |
1183 | cval->res /= 2; | |
1184 | } | |
6bc170e4 DM |
1185 | if (get_ctl_value(cval, UAC_GET_RES, |
1186 | (cval->control << 8) | minchn, &cval->res) < 0) | |
1da177e4 LT |
1187 | cval->res = last_valid_res; |
1188 | } | |
1189 | if (cval->res == 0) | |
1190 | cval->res = 1; | |
14790f1c TI |
1191 | |
1192 | /* Additional checks for the proper resolution | |
1193 | * | |
1194 | * Some devices report smaller resolutions than actually | |
1195 | * reacting. They don't return errors but simply clip | |
1196 | * to the lower aligned value. | |
1197 | */ | |
1198 | if (cval->min + cval->res < cval->max) { | |
1199 | int last_valid_res = cval->res; | |
1200 | int saved, test, check; | |
641b4879 | 1201 | get_cur_mix_raw(cval, minchn, &saved); |
14790f1c TI |
1202 | for (;;) { |
1203 | test = saved; | |
1204 | if (test < cval->max) | |
1205 | test += cval->res; | |
1206 | else | |
1207 | test -= cval->res; | |
1208 | if (test < cval->min || test > cval->max || | |
eef90451 | 1209 | snd_usb_set_cur_mix_value(cval, minchn, 0, test) || |
641b4879 | 1210 | get_cur_mix_raw(cval, minchn, &check)) { |
14790f1c TI |
1211 | cval->res = last_valid_res; |
1212 | break; | |
1213 | } | |
1214 | if (test == check) | |
1215 | break; | |
1216 | cval->res *= 2; | |
1217 | } | |
eef90451 | 1218 | snd_usb_set_cur_mix_value(cval, minchn, 0, saved); |
14790f1c TI |
1219 | } |
1220 | ||
1da177e4 LT |
1221 | cval->initialized = 1; |
1222 | } | |
c3a3e040 | 1223 | |
dcaaf9f2 TI |
1224 | if (kctl) |
1225 | volume_control_quirks(cval, kctl); | |
1226 | ||
c3a3e040 JK |
1227 | /* USB descriptions contain the dB scale in 1/256 dB unit |
1228 | * while ALSA TLV contains in 1/100 dB unit | |
1229 | */ | |
1230 | cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; | |
1231 | cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; | |
1232 | if (cval->dBmin > cval->dBmax) { | |
1233 | /* something is wrong; assume it's either from/to 0dB */ | |
1234 | if (cval->dBmin < 0) | |
1235 | cval->dBmax = 0; | |
1236 | else if (cval->dBmin > 0) | |
1237 | cval->dBmin = 0; | |
1238 | if (cval->dBmin > cval->dBmax) { | |
1239 | /* totally crap, return an error */ | |
1240 | return -EINVAL; | |
1241 | } | |
1242 | } | |
1243 | ||
1da177e4 LT |
1244 | return 0; |
1245 | } | |
1246 | ||
dcaaf9f2 | 1247 | #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL) |
1da177e4 LT |
1248 | |
1249 | /* get a feature/mixer unit info */ | |
6bc170e4 DM |
1250 | static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, |
1251 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 | 1252 | { |
86e07d34 | 1253 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1254 | |
1255 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
1256 | cval->val_type == USB_MIXER_INV_BOOLEAN) | |
1257 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1258 | else | |
1259 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1260 | uinfo->count = cval->channels; | |
1261 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
1262 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
1263 | uinfo->value.integer.min = 0; | |
1264 | uinfo->value.integer.max = 1; | |
1265 | } else { | |
9fcd0ab1 | 1266 | if (!cval->initialized) { |
dcaaf9f2 | 1267 | get_min_max_with_quirks(cval, 0, kcontrol); |
9fcd0ab1 TI |
1268 | if (cval->initialized && cval->dBmin >= cval->dBmax) { |
1269 | kcontrol->vd[0].access &= | |
1270 | ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ | | |
1271 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK); | |
3360b84b | 1272 | snd_ctl_notify(cval->head.mixer->chip->card, |
9fcd0ab1 TI |
1273 | SNDRV_CTL_EVENT_MASK_INFO, |
1274 | &kcontrol->id); | |
1275 | } | |
1276 | } | |
1da177e4 | 1277 | uinfo->value.integer.min = 0; |
14790f1c TI |
1278 | uinfo->value.integer.max = |
1279 | (cval->max - cval->min + cval->res - 1) / cval->res; | |
1da177e4 LT |
1280 | } |
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | /* get the current value from feature/mixer unit */ | |
6bc170e4 DM |
1285 | static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, |
1286 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1287 | { |
86e07d34 | 1288 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1289 | int c, cnt, val, err; |
1290 | ||
641b4879 | 1291 | ucontrol->value.integer.value[0] = cval->min; |
1da177e4 LT |
1292 | if (cval->cmask) { |
1293 | cnt = 0; | |
1294 | for (c = 0; c < MAX_CHANNELS; c++) { | |
641b4879 TI |
1295 | if (!(cval->cmask & (1 << c))) |
1296 | continue; | |
eef90451 | 1297 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val); |
641b4879 | 1298 | if (err < 0) |
5aeee342 | 1299 | return filter_error(cval, err); |
641b4879 TI |
1300 | val = get_relative_value(cval, val); |
1301 | ucontrol->value.integer.value[cnt] = val; | |
1302 | cnt++; | |
1da177e4 | 1303 | } |
641b4879 | 1304 | return 0; |
1da177e4 LT |
1305 | } else { |
1306 | /* master channel */ | |
eef90451 | 1307 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); |
641b4879 | 1308 | if (err < 0) |
5aeee342 | 1309 | return filter_error(cval, err); |
1da177e4 LT |
1310 | val = get_relative_value(cval, val); |
1311 | ucontrol->value.integer.value[0] = val; | |
1312 | } | |
1313 | return 0; | |
1314 | } | |
1315 | ||
1316 | /* put the current value to feature/mixer unit */ | |
6bc170e4 DM |
1317 | static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, |
1318 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1319 | { |
86e07d34 | 1320 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1321 | int c, cnt, val, oval, err; |
1322 | int changed = 0; | |
1323 | ||
1324 | if (cval->cmask) { | |
1325 | cnt = 0; | |
1326 | for (c = 0; c < MAX_CHANNELS; c++) { | |
641b4879 TI |
1327 | if (!(cval->cmask & (1 << c))) |
1328 | continue; | |
eef90451 | 1329 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval); |
641b4879 | 1330 | if (err < 0) |
5aeee342 | 1331 | return filter_error(cval, err); |
641b4879 TI |
1332 | val = ucontrol->value.integer.value[cnt]; |
1333 | val = get_abs_value(cval, val); | |
1334 | if (oval != val) { | |
eef90451 | 1335 | snd_usb_set_cur_mix_value(cval, c + 1, cnt, val); |
641b4879 | 1336 | changed = 1; |
1da177e4 | 1337 | } |
641b4879 | 1338 | cnt++; |
1da177e4 LT |
1339 | } |
1340 | } else { | |
1341 | /* master channel */ | |
eef90451 | 1342 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); |
1da177e4 | 1343 | if (err < 0) |
5aeee342 | 1344 | return filter_error(cval, err); |
1da177e4 LT |
1345 | val = ucontrol->value.integer.value[0]; |
1346 | val = get_abs_value(cval, val); | |
1347 | if (val != oval) { | |
eef90451 | 1348 | snd_usb_set_cur_mix_value(cval, 0, 0, val); |
1da177e4 LT |
1349 | changed = 1; |
1350 | } | |
1351 | } | |
1352 | return changed; | |
1353 | } | |
1354 | ||
568fa7e0 AC |
1355 | /* get the boolean value from the master channel of a UAC control */ |
1356 | static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol, | |
1357 | struct snd_ctl_elem_value *ucontrol) | |
5a222e84 AC |
1358 | { |
1359 | struct usb_mixer_elem_info *cval = kcontrol->private_data; | |
1360 | int val, err; | |
1361 | ||
1362 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); | |
1363 | if (err < 0) | |
1364 | return filter_error(cval, err); | |
1365 | val = (val != 0); | |
1366 | ucontrol->value.integer.value[0] = val; | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1d38f5d8 JS |
1370 | /* get the connectors status and report it as boolean type */ |
1371 | static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol, | |
1372 | struct snd_ctl_elem_value *ucontrol) | |
1373 | { | |
1374 | struct usb_mixer_elem_info *cval = kcontrol->private_data; | |
1375 | struct snd_usb_audio *chip = cval->head.mixer->chip; | |
1376 | int idx = 0, validx, ret, val; | |
1377 | ||
1378 | validx = cval->control << 8 | 0; | |
1379 | ||
1380 | ret = snd_usb_lock_shutdown(chip) ? -EIO : 0; | |
1381 | if (ret) | |
1382 | goto error; | |
1383 | ||
1384 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); | |
1385 | if (cval->head.mixer->protocol == UAC_VERSION_2) { | |
1386 | struct uac2_connectors_ctl_blk uac2_conn; | |
1387 | ||
1388 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, | |
1389 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
1390 | validx, idx, &uac2_conn, sizeof(uac2_conn)); | |
1391 | val = !!uac2_conn.bNrChannels; | |
1392 | } else { /* UAC_VERSION_3 */ | |
1393 | struct uac3_insertion_ctl_blk uac3_conn; | |
1394 | ||
1395 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, | |
1396 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
1397 | validx, idx, &uac3_conn, sizeof(uac3_conn)); | |
1398 | val = !!uac3_conn.bmConInserted; | |
1399 | } | |
1400 | ||
1401 | snd_usb_unlock_shutdown(chip); | |
1402 | ||
1403 | if (ret < 0) { | |
1404 | error: | |
1405 | usb_audio_err(chip, | |
1406 | "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", | |
1407 | UAC_GET_CUR, validx, idx, cval->val_type); | |
1408 | return ret; | |
1409 | } | |
1410 | ||
1411 | ucontrol->value.integer.value[0] = val; | |
1412 | return 0; | |
1413 | } | |
1414 | ||
86e07d34 | 1415 | static struct snd_kcontrol_new usb_feature_unit_ctl = { |
1da177e4 LT |
1416 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1417 | .name = "", /* will be filled later manually */ | |
1418 | .info = mixer_ctl_feature_info, | |
1419 | .get = mixer_ctl_feature_get, | |
1420 | .put = mixer_ctl_feature_put, | |
1421 | }; | |
1422 | ||
23caaf19 | 1423 | /* the read-only variant */ |
8fdaebbb | 1424 | static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = { |
23caaf19 DM |
1425 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1426 | .name = "", /* will be filled later manually */ | |
1427 | .info = mixer_ctl_feature_info, | |
1428 | .get = mixer_ctl_feature_get, | |
1429 | .put = NULL, | |
1430 | }; | |
1431 | ||
568fa7e0 AC |
1432 | /* |
1433 | * A control which shows the boolean value from reading a UAC control on | |
1434 | * the master channel. | |
1435 | */ | |
1436 | static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = { | |
5a222e84 AC |
1437 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, |
1438 | .name = "", /* will be filled later manually */ | |
1439 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1440 | .info = snd_ctl_boolean_mono_info, | |
568fa7e0 | 1441 | .get = mixer_ctl_master_bool_get, |
5a222e84 AC |
1442 | .put = NULL, |
1443 | }; | |
1444 | ||
1d38f5d8 JS |
1445 | static const struct snd_kcontrol_new usb_connector_ctl_ro = { |
1446 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, | |
1447 | .name = "", /* will be filled later manually */ | |
1448 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1449 | .info = snd_ctl_boolean_mono_info, | |
1450 | .get = mixer_ctl_connector_get, | |
1451 | .put = NULL, | |
1452 | }; | |
1453 | ||
6bc170e4 DM |
1454 | /* |
1455 | * This symbol is exported in order to allow the mixer quirks to | |
1456 | * hook up to the standard feature unit control mechanism | |
1457 | */ | |
9e38658f | 1458 | struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl; |
1da177e4 LT |
1459 | |
1460 | /* | |
1461 | * build a feature control | |
1462 | */ | |
08d1e635 TI |
1463 | static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) |
1464 | { | |
1465 | return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); | |
1466 | } | |
1467 | ||
6bc170e4 DM |
1468 | /* |
1469 | * A lot of headsets/headphones have a "Speaker" mixer. Make sure we | |
1470 | * rename it to "Headphone". We determine if something is a headphone | |
1471 | * similar to how udev determines form factor. | |
1472 | */ | |
9b4ef977 DH |
1473 | static void check_no_speaker_on_headset(struct snd_kcontrol *kctl, |
1474 | struct snd_card *card) | |
1475 | { | |
1476 | const char *names_to_check[] = { | |
1477 | "Headset", "headset", "Headphone", "headphone", NULL}; | |
1478 | const char **s; | |
e0f17c75 | 1479 | bool found = false; |
9b4ef977 DH |
1480 | |
1481 | if (strcmp("Speaker", kctl->id.name)) | |
1482 | return; | |
1483 | ||
1484 | for (s = names_to_check; *s; s++) | |
1485 | if (strstr(card->shortname, *s)) { | |
e0f17c75 | 1486 | found = true; |
9b4ef977 DH |
1487 | break; |
1488 | } | |
1489 | ||
1490 | if (!found) | |
1491 | return; | |
1492 | ||
1493 | strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name)); | |
1494 | } | |
1495 | ||
21e9b3e9 AC |
1496 | static struct usb_feature_control_info *get_feature_control_info(int control) |
1497 | { | |
1498 | int i; | |
1499 | ||
1500 | for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) { | |
1501 | if (audio_feature_info[i].control == control) | |
1502 | return &audio_feature_info[i]; | |
1503 | } | |
1504 | return NULL; | |
1505 | } | |
1506 | ||
17156f23 RB |
1507 | static void __build_feature_ctl(struct usb_mixer_interface *mixer, |
1508 | const struct usbmix_name_map *imap, | |
1509 | unsigned int ctl_mask, int control, | |
1510 | struct usb_audio_term *iterm, | |
1511 | struct usb_audio_term *oterm, | |
1512 | int unitid, int nameid, int readonly_mask) | |
1da177e4 | 1513 | { |
bc18e31c | 1514 | struct usb_feature_control_info *ctl_info; |
1da177e4 LT |
1515 | unsigned int len = 0; |
1516 | int mapped_name = 0; | |
86e07d34 TI |
1517 | struct snd_kcontrol *kctl; |
1518 | struct usb_mixer_elem_info *cval; | |
c3a3e040 | 1519 | const struct usbmix_name_map *map; |
80acefff | 1520 | unsigned int range; |
1da177e4 | 1521 | |
65f25da4 | 1522 | if (control == UAC_FU_GRAPHIC_EQUALIZER) { |
1da177e4 LT |
1523 | /* FIXME: not supported yet */ |
1524 | return; | |
1525 | } | |
1526 | ||
17156f23 | 1527 | map = find_map(imap, unitid, control); |
c3a3e040 | 1528 | if (check_ignored_ctl(map)) |
1da177e4 LT |
1529 | return; |
1530 | ||
561b220a | 1531 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
a860d95f | 1532 | if (!cval) |
1da177e4 | 1533 | return; |
17156f23 | 1534 | snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid); |
1da177e4 LT |
1535 | cval->control = control; |
1536 | cval->cmask = ctl_mask; | |
21e9b3e9 AC |
1537 | |
1538 | ctl_info = get_feature_control_info(control); | |
62376025 CIK |
1539 | if (!ctl_info) { |
1540 | kfree(cval); | |
21e9b3e9 | 1541 | return; |
62376025 | 1542 | } |
17156f23 | 1543 | if (mixer->protocol == UAC_VERSION_1) |
bc18e31c JS |
1544 | cval->val_type = ctl_info->type; |
1545 | else /* UAC_VERSION_2 */ | |
1546 | cval->val_type = ctl_info->type_uac2 >= 0 ? | |
1547 | ctl_info->type_uac2 : ctl_info->type; | |
1548 | ||
a6a33259 | 1549 | if (ctl_mask == 0) { |
1da177e4 | 1550 | cval->channels = 1; /* master channel */ |
a6a33259 DM |
1551 | cval->master_readonly = readonly_mask; |
1552 | } else { | |
1da177e4 LT |
1553 | int i, c = 0; |
1554 | for (i = 0; i < 16; i++) | |
1555 | if (ctl_mask & (1 << i)) | |
1556 | c++; | |
1557 | cval->channels = c; | |
a6a33259 | 1558 | cval->ch_readonly = readonly_mask; |
1da177e4 LT |
1559 | } |
1560 | ||
6bc170e4 DM |
1561 | /* |
1562 | * If all channels in the mask are marked read-only, make the control | |
eef90451 | 1563 | * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't |
6bc170e4 DM |
1564 | * issue write commands to read-only channels. |
1565 | */ | |
a6a33259 | 1566 | if (cval->channels == readonly_mask) |
23caaf19 DM |
1567 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
1568 | else | |
1569 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
1570 | ||
6bc170e4 | 1571 | if (!kctl) { |
17156f23 | 1572 | usb_audio_err(mixer->chip, "cannot malloc kcontrol\n"); |
1da177e4 LT |
1573 | kfree(cval); |
1574 | return; | |
1575 | } | |
eef90451 | 1576 | kctl->private_free = snd_usb_mixer_elem_free; |
1da177e4 | 1577 | |
c3a3e040 | 1578 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
1da177e4 | 1579 | mapped_name = len != 0; |
6bc170e4 | 1580 | if (!len && nameid) |
17156f23 | 1581 | len = snd_usb_copy_string_desc(mixer->chip, nameid, |
c3a3e040 | 1582 | kctl->id.name, sizeof(kctl->id.name)); |
1da177e4 LT |
1583 | |
1584 | switch (control) { | |
65f25da4 DM |
1585 | case UAC_FU_MUTE: |
1586 | case UAC_FU_VOLUME: | |
6bc170e4 DM |
1587 | /* |
1588 | * determine the control name. the rule is: | |
1da177e4 LT |
1589 | * - if a name id is given in descriptor, use it. |
1590 | * - if the connected input can be determined, then use the name | |
1591 | * of terminal type. | |
1592 | * - if the connected output can be determined, use it. | |
1593 | * - otherwise, anonymous name. | |
1594 | */ | |
6bc170e4 | 1595 | if (!len) { |
17156f23 RB |
1596 | if (iterm) |
1597 | len = get_term_name(mixer->chip, iterm, | |
1598 | kctl->id.name, | |
1599 | sizeof(kctl->id.name), 1); | |
1600 | if (!len && oterm) | |
1601 | len = get_term_name(mixer->chip, oterm, | |
6bc170e4 DM |
1602 | kctl->id.name, |
1603 | sizeof(kctl->id.name), 1); | |
1604 | if (!len) | |
49fd46d2 DM |
1605 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
1606 | "Feature %d", unitid); | |
1da177e4 | 1607 | } |
9b4ef977 DH |
1608 | |
1609 | if (!mapped_name) | |
17156f23 | 1610 | check_no_speaker_on_headset(kctl, mixer->chip->card); |
9b4ef977 | 1611 | |
6bc170e4 DM |
1612 | /* |
1613 | * determine the stream direction: | |
1da177e4 LT |
1614 | * if the connected output is USB stream, then it's likely a |
1615 | * capture stream. otherwise it should be playback (hopefully :) | |
1616 | */ | |
17156f23 RB |
1617 | if (!mapped_name && oterm && !(oterm->type >> 16)) { |
1618 | if ((oterm->type & 0xff00) == 0x0100) | |
49fd46d2 | 1619 | append_ctl_name(kctl, " Capture"); |
6bc170e4 | 1620 | else |
49fd46d2 | 1621 | append_ctl_name(kctl, " Playback"); |
1da177e4 | 1622 | } |
65f25da4 | 1623 | append_ctl_name(kctl, control == UAC_FU_MUTE ? |
08d1e635 | 1624 | " Switch" : " Volume"); |
1da177e4 | 1625 | break; |
1da177e4 | 1626 | default: |
6bc170e4 | 1627 | if (!len) |
1da177e4 LT |
1628 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, |
1629 | sizeof(kctl->id.name)); | |
1630 | break; | |
1631 | } | |
1632 | ||
e182534d TI |
1633 | /* get min/max values */ |
1634 | get_min_max_with_quirks(cval, 0, kctl); | |
1635 | ||
1636 | if (control == UAC_FU_VOLUME) { | |
1637 | check_mapped_dB(map, cval); | |
1638 | if (cval->dBmin < cval->dBmax || !cval->initialized) { | |
1639 | kctl->tlv.c = snd_usb_mixer_vol_tlv; | |
1640 | kctl->vd[0].access |= | |
1641 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | | |
1642 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; | |
1643 | } | |
1644 | } | |
1645 | ||
17156f23 | 1646 | snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl); |
42e3121d | 1647 | |
80acefff | 1648 | range = (cval->max - cval->min) / cval->res; |
6bc170e4 DM |
1649 | /* |
1650 | * Are there devices with volume range more than 255? I use a bit more | |
80acefff AF |
1651 | * to be sure. 384 is a resolution magic number found on Logitech |
1652 | * devices. It will definitively catch all buggy Logitech devices. | |
1653 | */ | |
1654 | if (range > 384) { | |
17156f23 | 1655 | usb_audio_warn(mixer->chip, |
82c1cf0a | 1656 | "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.", |
6bc170e4 | 1657 | range); |
17156f23 | 1658 | usb_audio_warn(mixer->chip, |
82c1cf0a | 1659 | "[%d] FU [%s] ch = %d, val = %d/%d/%d", |
3360b84b | 1660 | cval->head.id, kctl->id.name, cval->channels, |
6bc170e4 | 1661 | cval->min, cval->max, cval->res); |
80acefff AF |
1662 | } |
1663 | ||
17156f23 | 1664 | usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", |
3360b84b | 1665 | cval->head.id, kctl->id.name, cval->channels, |
6bc170e4 | 1666 | cval->min, cval->max, cval->res); |
3360b84b | 1667 | snd_usb_mixer_add_control(&cval->head, kctl); |
1da177e4 LT |
1668 | } |
1669 | ||
17156f23 RB |
1670 | static void build_feature_ctl(struct mixer_build *state, void *raw_desc, |
1671 | unsigned int ctl_mask, int control, | |
1672 | struct usb_audio_term *iterm, int unitid, | |
1673 | int readonly_mask) | |
1674 | { | |
1675 | struct uac_feature_unit_descriptor *desc = raw_desc; | |
1676 | int nameid = uac_feature_unit_iFeature(desc); | |
1677 | ||
1678 | __build_feature_ctl(state->mixer, state->map, ctl_mask, control, | |
1679 | iterm, &state->oterm, unitid, nameid, readonly_mask); | |
1680 | } | |
1681 | ||
1682 | static void build_feature_ctl_badd(struct usb_mixer_interface *mixer, | |
1683 | unsigned int ctl_mask, int control, int unitid, | |
1684 | const struct usbmix_name_map *badd_map) | |
1685 | { | |
1686 | __build_feature_ctl(mixer, badd_map, ctl_mask, control, | |
1687 | NULL, NULL, unitid, 0, 0); | |
1688 | } | |
1689 | ||
167e1fb1 | 1690 | static void get_connector_control_name(struct usb_mixer_interface *mixer, |
5a222e84 AC |
1691 | struct usb_audio_term *term, |
1692 | bool is_input, char *name, int name_size) | |
1693 | { | |
167e1fb1 | 1694 | int name_len = get_term_name(mixer->chip, term, name, name_size, 0); |
5a222e84 AC |
1695 | |
1696 | if (name_len == 0) | |
1697 | strlcpy(name, "Unknown", name_size); | |
1698 | ||
1699 | /* | |
1700 | * sound/core/ctljack.c has a convention of naming jack controls | |
1701 | * by ending in " Jack". Make it slightly more useful by | |
1702 | * indicating Input or Output after the terminal name. | |
1703 | */ | |
1704 | if (is_input) | |
1705 | strlcat(name, " - Input Jack", name_size); | |
1706 | else | |
1707 | strlcat(name, " - Output Jack", name_size); | |
1708 | } | |
1709 | ||
1710 | /* Build a mixer control for a UAC connector control (jack-detect) */ | |
167e1fb1 | 1711 | static void build_connector_control(struct usb_mixer_interface *mixer, |
5a222e84 AC |
1712 | struct usb_audio_term *term, bool is_input) |
1713 | { | |
1714 | struct snd_kcontrol *kctl; | |
1715 | struct usb_mixer_elem_info *cval; | |
1716 | ||
1717 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); | |
1718 | if (!cval) | |
1719 | return; | |
167e1fb1 | 1720 | snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id); |
568fa7e0 | 1721 | /* |
1d38f5d8 JS |
1722 | * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the |
1723 | * number of channels connected. | |
1724 | * | |
1725 | * UAC3: The first byte specifies size of bitmap for the inserted controls. The | |
1726 | * following byte(s) specifies which connectors are inserted. | |
1727 | * | |
1728 | * This boolean ctl will simply report if any channels are connected | |
1729 | * or not. | |
568fa7e0 | 1730 | */ |
167e1fb1 | 1731 | if (mixer->protocol == UAC_VERSION_2) |
1d38f5d8 JS |
1732 | cval->control = UAC2_TE_CONNECTOR; |
1733 | else /* UAC_VERSION_3 */ | |
1734 | cval->control = UAC3_TE_INSERTION; | |
1735 | ||
5a222e84 AC |
1736 | cval->val_type = USB_MIXER_BOOLEAN; |
1737 | cval->channels = 1; /* report true if any channel is connected */ | |
1738 | cval->min = 0; | |
1739 | cval->max = 1; | |
1d38f5d8 | 1740 | kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval); |
5a222e84 | 1741 | if (!kctl) { |
167e1fb1 | 1742 | usb_audio_err(mixer->chip, "cannot malloc kcontrol\n"); |
5a222e84 AC |
1743 | kfree(cval); |
1744 | return; | |
1745 | } | |
167e1fb1 | 1746 | get_connector_control_name(mixer, term, is_input, kctl->id.name, |
5a222e84 AC |
1747 | sizeof(kctl->id.name)); |
1748 | kctl->private_free = snd_usb_mixer_elem_free; | |
1749 | snd_usb_mixer_add_control(&cval->head, kctl); | |
1750 | } | |
1751 | ||
cddaafb9 DM |
1752 | static int parse_clock_source_unit(struct mixer_build *state, int unitid, |
1753 | void *_ftr) | |
1754 | { | |
1755 | struct uac_clock_source_descriptor *hdr = _ftr; | |
1756 | struct usb_mixer_elem_info *cval; | |
1757 | struct snd_kcontrol *kctl; | |
1758 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; | |
1759 | int ret; | |
1760 | ||
1761 | if (state->mixer->protocol != UAC_VERSION_2) | |
1762 | return -EINVAL; | |
1763 | ||
1764 | if (hdr->bLength != sizeof(*hdr)) { | |
1765 | usb_audio_dbg(state->chip, | |
1766 | "Bogus clock source descriptor length of %d, ignoring.\n", | |
1767 | hdr->bLength); | |
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | /* | |
1772 | * The only property of this unit we are interested in is the | |
1773 | * clock source validity. If that isn't readable, just bail out. | |
1774 | */ | |
9a2fe9b8 | 1775 | if (!uac_v2v3_control_is_readable(hdr->bmControls, |
21e9b3e9 | 1776 | UAC2_CS_CONTROL_CLOCK_VALID)) |
cddaafb9 DM |
1777 | return 0; |
1778 | ||
1779 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); | |
1780 | if (!cval) | |
1781 | return -ENOMEM; | |
1782 | ||
1783 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID); | |
1784 | ||
1785 | cval->min = 0; | |
1786 | cval->max = 1; | |
1787 | cval->channels = 1; | |
1788 | cval->val_type = USB_MIXER_BOOLEAN; | |
1789 | cval->control = UAC2_CS_CONTROL_CLOCK_VALID; | |
1790 | ||
568fa7e0 AC |
1791 | cval->master_readonly = 1; |
1792 | /* From UAC2 5.2.5.1.2 "Only the get request is supported." */ | |
1793 | kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval); | |
cddaafb9 DM |
1794 | |
1795 | if (!kctl) { | |
1796 | kfree(cval); | |
1797 | return -ENOMEM; | |
1798 | } | |
1799 | ||
1800 | kctl->private_free = snd_usb_mixer_elem_free; | |
eccfc1b8 | 1801 | ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource, |
cddaafb9 DM |
1802 | name, sizeof(name)); |
1803 | if (ret > 0) | |
1804 | snprintf(kctl->id.name, sizeof(kctl->id.name), | |
1805 | "%s Validity", name); | |
1806 | else | |
1807 | snprintf(kctl->id.name, sizeof(kctl->id.name), | |
1808 | "Clock Source %d Validity", hdr->bClockID); | |
1809 | ||
1810 | return snd_usb_mixer_add_control(&cval->head, kctl); | |
1811 | } | |
1812 | ||
1da177e4 LT |
1813 | /* |
1814 | * parse a feature unit | |
1815 | * | |
25985edc | 1816 | * most of controls are defined here. |
1da177e4 | 1817 | */ |
6bc170e4 DM |
1818 | static int parse_audio_feature_unit(struct mixer_build *state, int unitid, |
1819 | void *_ftr) | |
1da177e4 LT |
1820 | { |
1821 | int channels, i, j; | |
86e07d34 | 1822 | struct usb_audio_term iterm; |
36c346e1 | 1823 | unsigned int master_bits; |
1da177e4 | 1824 | int err, csize; |
23caaf19 DM |
1825 | struct uac_feature_unit_descriptor *hdr = _ftr; |
1826 | __u8 *bmaControls; | |
1827 | ||
1828 | if (state->mixer->protocol == UAC_VERSION_1) { | |
d937cd67 TI |
1829 | if (hdr->bLength < 7) { |
1830 | usb_audio_err(state->chip, | |
1831 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", | |
1832 | unitid); | |
1833 | return -EINVAL; | |
1834 | } | |
23caaf19 | 1835 | csize = hdr->bControlSize; |
3c02a6d9 | 1836 | if (!csize) { |
0ba41d91 | 1837 | usb_audio_dbg(state->chip, |
3c02a6d9 | 1838 | "unit %u: invalid bControlSize == 0\n", |
0ba41d91 | 1839 | unitid); |
60c961a9 NK |
1840 | return -EINVAL; |
1841 | } | |
23caaf19 DM |
1842 | channels = (hdr->bLength - 7) / csize - 1; |
1843 | bmaControls = hdr->bmaControls; | |
d56268fb | 1844 | if (hdr->bLength < 7 + csize) { |
0ba41d91 TI |
1845 | usb_audio_err(state->chip, |
1846 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", | |
1847 | unitid); | |
d56268fb CL |
1848 | return -EINVAL; |
1849 | } | |
9a2fe9b8 | 1850 | } else if (state->mixer->protocol == UAC_VERSION_2) { |
23caaf19 | 1851 | struct uac2_feature_unit_descriptor *ftr = _ftr; |
d937cd67 TI |
1852 | if (hdr->bLength < 6) { |
1853 | usb_audio_err(state->chip, | |
1854 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", | |
1855 | unitid); | |
1856 | return -EINVAL; | |
1857 | } | |
23caaf19 | 1858 | csize = 4; |
e8d0fee7 | 1859 | channels = (hdr->bLength - 6) / 4 - 1; |
23caaf19 | 1860 | bmaControls = ftr->bmaControls; |
d56268fb | 1861 | if (hdr->bLength < 6 + csize) { |
0ba41d91 TI |
1862 | usb_audio_err(state->chip, |
1863 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", | |
1864 | unitid); | |
d56268fb CL |
1865 | return -EINVAL; |
1866 | } | |
9a2fe9b8 RB |
1867 | } else { /* UAC_VERSION_3 */ |
1868 | struct uac3_feature_unit_descriptor *ftr = _ftr; | |
1869 | ||
1870 | if (hdr->bLength < 7) { | |
1871 | usb_audio_err(state->chip, | |
1872 | "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n", | |
1873 | unitid); | |
1874 | return -EINVAL; | |
1875 | } | |
1876 | csize = 4; | |
1877 | channels = (ftr->bLength - 7) / 4 - 1; | |
1878 | bmaControls = ftr->bmaControls; | |
1879 | if (hdr->bLength < 7 + csize) { | |
1880 | usb_audio_err(state->chip, | |
1881 | "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n", | |
1882 | unitid); | |
1883 | return -EINVAL; | |
1884 | } | |
1da177e4 LT |
1885 | } |
1886 | ||
1887 | /* parse the source unit */ | |
f25ecf8f TI |
1888 | err = parse_audio_unit(state, hdr->bSourceID); |
1889 | if (err < 0) | |
1da177e4 LT |
1890 | return err; |
1891 | ||
1892 | /* determine the input source type and name */ | |
4d7b86c9 DM |
1893 | err = check_input_term(state, hdr->bSourceID, &iterm); |
1894 | if (err < 0) | |
1895 | return err; | |
1da177e4 | 1896 | |
23caaf19 | 1897 | master_bits = snd_usb_combine_bytes(bmaControls, csize); |
0c3cee57 JK |
1898 | /* master configuration quirks */ |
1899 | switch (state->chip->usb_id) { | |
1900 | case USB_ID(0x08bb, 0x2702): | |
0ba41d91 TI |
1901 | usb_audio_info(state->chip, |
1902 | "usbmixer: master volume quirk for PCM2702 chip\n"); | |
0c3cee57 | 1903 | /* disable non-functional volume control */ |
65f25da4 | 1904 | master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); |
0c3cee57 | 1905 | break; |
c1051439 | 1906 | case USB_ID(0x1130, 0xf211): |
0ba41d91 TI |
1907 | usb_audio_info(state->chip, |
1908 | "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n"); | |
c1051439 DH |
1909 | /* disable non-functional volume control */ |
1910 | channels = 0; | |
1911 | break; | |
1912 | ||
0c3cee57 | 1913 | } |
23caaf19 DM |
1914 | |
1915 | if (state->mixer->protocol == UAC_VERSION_1) { | |
1916 | /* check all control types */ | |
1917 | for (i = 0; i < 10; i++) { | |
1918 | unsigned int ch_bits = 0; | |
21e9b3e9 AC |
1919 | int control = audio_feature_info[i].control; |
1920 | ||
23caaf19 | 1921 | for (j = 0; j < channels; j++) { |
6bc170e4 DM |
1922 | unsigned int mask; |
1923 | ||
1924 | mask = snd_usb_combine_bytes(bmaControls + | |
1925 | csize * (j+1), csize); | |
23caaf19 DM |
1926 | if (mask & (1 << i)) |
1927 | ch_bits |= (1 << j); | |
1928 | } | |
1929 | /* audio class v1 controls are never read-only */ | |
6bc170e4 DM |
1930 | |
1931 | /* | |
1932 | * The first channel must be set | |
1933 | * (for ease of programming). | |
1934 | */ | |
1935 | if (ch_bits & 1) | |
21e9b3e9 | 1936 | build_feature_ctl(state, _ftr, ch_bits, control, |
6bc170e4 | 1937 | &iterm, unitid, 0); |
23caaf19 | 1938 | if (master_bits & (1 << i)) |
21e9b3e9 AC |
1939 | build_feature_ctl(state, _ftr, 0, control, |
1940 | &iterm, unitid, 0); | |
23caaf19 | 1941 | } |
9a2fe9b8 | 1942 | } else { /* UAC_VERSION_2/3 */ |
d09c06c6 | 1943 | for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) { |
23caaf19 DM |
1944 | unsigned int ch_bits = 0; |
1945 | unsigned int ch_read_only = 0; | |
21e9b3e9 | 1946 | int control = audio_feature_info[i].control; |
23caaf19 DM |
1947 | |
1948 | for (j = 0; j < channels; j++) { | |
6bc170e4 DM |
1949 | unsigned int mask; |
1950 | ||
1951 | mask = snd_usb_combine_bytes(bmaControls + | |
1952 | csize * (j+1), csize); | |
21e9b3e9 | 1953 | if (uac_v2v3_control_is_readable(mask, control)) { |
23caaf19 | 1954 | ch_bits |= (1 << j); |
21e9b3e9 | 1955 | if (!uac_v2v3_control_is_writeable(mask, control)) |
23caaf19 DM |
1956 | ch_read_only |= (1 << j); |
1957 | } | |
1958 | } | |
1959 | ||
6bc170e4 DM |
1960 | /* |
1961 | * NOTE: build_feature_ctl() will mark the control | |
1962 | * read-only if all channels are marked read-only in | |
1963 | * the descriptors. Otherwise, the control will be | |
1964 | * reported as writeable, but the driver will not | |
1965 | * actually issue a write command for read-only | |
1966 | * channels. | |
1967 | */ | |
1968 | ||
1969 | /* | |
1970 | * The first channel must be set | |
1971 | * (for ease of programming). | |
1972 | */ | |
1973 | if (ch_bits & 1) | |
21e9b3e9 | 1974 | build_feature_ctl(state, _ftr, ch_bits, control, |
6bc170e4 | 1975 | &iterm, unitid, ch_read_only); |
21e9b3e9 | 1976 | if (uac_v2v3_control_is_readable(master_bits, control)) |
2de841ef TI |
1977 | build_feature_ctl(state, _ftr, 0, control, |
1978 | &iterm, unitid, | |
21e9b3e9 AC |
1979 | !uac_v2v3_control_is_writeable(master_bits, |
1980 | control)); | |
1da177e4 | 1981 | } |
1da177e4 LT |
1982 | } |
1983 | ||
1984 | return 0; | |
1985 | } | |
1986 | ||
1da177e4 LT |
1987 | /* |
1988 | * Mixer Unit | |
1989 | */ | |
1990 | ||
1991 | /* | |
1992 | * build a mixer unit control | |
1993 | * | |
1994 | * the callbacks are identical with feature unit. | |
1995 | * input channel number (zero based) is given in control field instead. | |
1996 | */ | |
99fc8645 DM |
1997 | static void build_mixer_unit_ctl(struct mixer_build *state, |
1998 | struct uac_mixer_unit_descriptor *desc, | |
6cfd839a JS |
1999 | int in_pin, int in_ch, int num_outs, |
2000 | int unitid, struct usb_audio_term *iterm) | |
1da177e4 | 2001 | { |
86e07d34 | 2002 | struct usb_mixer_elem_info *cval; |
1da177e4 | 2003 | unsigned int i, len; |
86e07d34 | 2004 | struct snd_kcontrol *kctl; |
c3a3e040 | 2005 | const struct usbmix_name_map *map; |
1da177e4 | 2006 | |
17156f23 | 2007 | map = find_map(state->map, unitid, 0); |
c3a3e040 | 2008 | if (check_ignored_ctl(map)) |
1da177e4 LT |
2009 | return; |
2010 | ||
561b220a | 2011 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
6bc170e4 | 2012 | if (!cval) |
1da177e4 LT |
2013 | return; |
2014 | ||
3360b84b | 2015 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
1da177e4 LT |
2016 | cval->control = in_ch + 1; /* based on 1 */ |
2017 | cval->val_type = USB_MIXER_S16; | |
2018 | for (i = 0; i < num_outs; i++) { | |
6bc170e4 DM |
2019 | __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); |
2020 | ||
2021 | if (check_matrix_bitmap(c, in_ch, i, num_outs)) { | |
1da177e4 LT |
2022 | cval->cmask |= (1 << i); |
2023 | cval->channels++; | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | /* get min/max values */ | |
2028 | get_min_max(cval, 0); | |
2029 | ||
2030 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
6bc170e4 | 2031 | if (!kctl) { |
0ba41d91 | 2032 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
1da177e4 LT |
2033 | kfree(cval); |
2034 | return; | |
2035 | } | |
eef90451 | 2036 | kctl->private_free = snd_usb_mixer_elem_free; |
1da177e4 | 2037 | |
c3a3e040 | 2038 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
6bc170e4 | 2039 | if (!len) |
eccfc1b8 | 2040 | len = get_term_name(state->chip, iterm, kctl->id.name, |
6bc170e4 DM |
2041 | sizeof(kctl->id.name), 0); |
2042 | if (!len) | |
1da177e4 | 2043 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); |
08d1e635 | 2044 | append_ctl_name(kctl, " Volume"); |
1da177e4 | 2045 | |
0ba41d91 | 2046 | usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n", |
3360b84b TI |
2047 | cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max); |
2048 | snd_usb_mixer_add_control(&cval->head, kctl); | |
1da177e4 LT |
2049 | } |
2050 | ||
5a222e84 AC |
2051 | static int parse_audio_input_terminal(struct mixer_build *state, int unitid, |
2052 | void *raw_desc) | |
2053 | { | |
2054 | struct usb_audio_term iterm; | |
1d38f5d8 | 2055 | unsigned int control, bmctls, term_id; |
5a222e84 | 2056 | |
5a222e84 | 2057 | if (state->mixer->protocol == UAC_VERSION_2) { |
1d38f5d8 | 2058 | struct uac2_input_terminal_descriptor *d_v2 = raw_desc; |
3e96d728 TI |
2059 | if (d_v2->bLength < sizeof(*d_v2)) |
2060 | return -EINVAL; | |
1d38f5d8 JS |
2061 | control = UAC2_TE_CONNECTOR; |
2062 | term_id = d_v2->bTerminalID; | |
2063 | bmctls = le16_to_cpu(d_v2->bmControls); | |
2064 | } else if (state->mixer->protocol == UAC_VERSION_3) { | |
2065 | struct uac3_input_terminal_descriptor *d_v3 = raw_desc; | |
3e96d728 TI |
2066 | if (d_v3->bLength < sizeof(*d_v3)) |
2067 | return -EINVAL; | |
1d38f5d8 JS |
2068 | control = UAC3_TE_INSERTION; |
2069 | term_id = d_v3->bTerminalID; | |
2070 | bmctls = le32_to_cpu(d_v3->bmControls); | |
2071 | } else { | |
2072 | return 0; /* UAC1. No Insertion control */ | |
5a222e84 | 2073 | } |
1d38f5d8 JS |
2074 | |
2075 | check_input_term(state, term_id, &iterm); | |
2076 | ||
2077 | /* Check for jack detection. */ | |
2078 | if (uac_v2v3_control_is_readable(bmctls, control)) | |
167e1fb1 | 2079 | build_connector_control(state->mixer, &iterm, true); |
1d38f5d8 | 2080 | |
5a222e84 AC |
2081 | return 0; |
2082 | } | |
2083 | ||
1da177e4 LT |
2084 | /* |
2085 | * parse a mixer unit | |
2086 | */ | |
6bc170e4 DM |
2087 | static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, |
2088 | void *raw_desc) | |
1da177e4 | 2089 | { |
99fc8645 | 2090 | struct uac_mixer_unit_descriptor *desc = raw_desc; |
86e07d34 | 2091 | struct usb_audio_term iterm; |
1da177e4 LT |
2092 | int input_pins, num_ins, num_outs; |
2093 | int pin, ich, err; | |
2094 | ||
6cfd839a JS |
2095 | err = uac_mixer_unit_get_channels(state, desc); |
2096 | if (err < 0) { | |
6bc170e4 DM |
2097 | usb_audio_err(state->chip, |
2098 | "invalid MIXER UNIT descriptor %d\n", | |
2099 | unitid); | |
6cfd839a | 2100 | return err; |
1da177e4 | 2101 | } |
1da177e4 | 2102 | |
6cfd839a JS |
2103 | num_outs = err; |
2104 | input_pins = desc->bNrInPins; | |
2105 | ||
1da177e4 LT |
2106 | num_ins = 0; |
2107 | ich = 0; | |
2108 | for (pin = 0; pin < input_pins; pin++) { | |
99fc8645 | 2109 | err = parse_audio_unit(state, desc->baSourceID[pin]); |
1da177e4 | 2110 | if (err < 0) |
284a8dd6 | 2111 | continue; |
ea114fc2 | 2112 | /* no bmControls field (e.g. Maya44) -> ignore */ |
0bfe5e43 | 2113 | if (!num_outs) |
ea114fc2 | 2114 | continue; |
99fc8645 | 2115 | err = check_input_term(state, desc->baSourceID[pin], &iterm); |
1da177e4 LT |
2116 | if (err < 0) |
2117 | return err; | |
2118 | num_ins += iterm.channels; | |
6bc170e4 | 2119 | for (; ich < num_ins; ich++) { |
1da177e4 LT |
2120 | int och, ich_has_controls = 0; |
2121 | ||
6bc170e4 DM |
2122 | for (och = 0; och < num_outs; och++) { |
2123 | __u8 *c = uac_mixer_unit_bmControls(desc, | |
2124 | state->mixer->protocol); | |
2125 | ||
2126 | if (check_matrix_bitmap(c, ich, och, num_outs)) { | |
1da177e4 LT |
2127 | ich_has_controls = 1; |
2128 | break; | |
2129 | } | |
2130 | } | |
2131 | if (ich_has_controls) | |
6cfd839a | 2132 | build_mixer_unit_ctl(state, desc, pin, ich, num_outs, |
1da177e4 LT |
2133 | unitid, &iterm); |
2134 | } | |
2135 | } | |
2136 | return 0; | |
2137 | } | |
2138 | ||
1da177e4 LT |
2139 | /* |
2140 | * Processing Unit / Extension Unit | |
2141 | */ | |
2142 | ||
2143 | /* get callback for processing/extension unit */ | |
6bc170e4 DM |
2144 | static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, |
2145 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 2146 | { |
86e07d34 | 2147 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
2148 | int err, val; |
2149 | ||
2150 | err = get_cur_ctl_value(cval, cval->control << 8, &val); | |
5aeee342 | 2151 | if (err < 0) { |
1da177e4 | 2152 | ucontrol->value.integer.value[0] = cval->min; |
5aeee342 | 2153 | return filter_error(cval, err); |
1da177e4 | 2154 | } |
1da177e4 LT |
2155 | val = get_relative_value(cval, val); |
2156 | ucontrol->value.integer.value[0] = val; | |
2157 | return 0; | |
2158 | } | |
2159 | ||
2160 | /* put callback for processing/extension unit */ | |
6bc170e4 DM |
2161 | static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, |
2162 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 2163 | { |
86e07d34 | 2164 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
2165 | int val, oval, err; |
2166 | ||
2167 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); | |
5aeee342 TI |
2168 | if (err < 0) |
2169 | return filter_error(cval, err); | |
1da177e4 LT |
2170 | val = ucontrol->value.integer.value[0]; |
2171 | val = get_abs_value(cval, val); | |
2172 | if (val != oval) { | |
2173 | set_cur_ctl_value(cval, cval->control << 8, val); | |
2174 | return 1; | |
2175 | } | |
2176 | return 0; | |
2177 | } | |
2178 | ||
2179 | /* alsa control interface for processing/extension unit */ | |
8fdaebbb | 2180 | static const struct snd_kcontrol_new mixer_procunit_ctl = { |
1da177e4 LT |
2181 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
2182 | .name = "", /* will be filled later */ | |
2183 | .info = mixer_ctl_feature_info, | |
2184 | .get = mixer_ctl_procunit_get, | |
2185 | .put = mixer_ctl_procunit_put, | |
2186 | }; | |
2187 | ||
1da177e4 LT |
2188 | /* |
2189 | * predefined data for processing units | |
2190 | */ | |
2191 | struct procunit_value_info { | |
2192 | int control; | |
2193 | char *suffix; | |
2194 | int val_type; | |
2195 | int min_value; | |
2196 | }; | |
2197 | ||
2198 | struct procunit_info { | |
2199 | int type; | |
2200 | char *name; | |
2201 | struct procunit_value_info *values; | |
2202 | }; | |
2203 | ||
0f292f02 JS |
2204 | static struct procunit_value_info undefined_proc_info[] = { |
2205 | { 0x00, "Control Undefined", 0 }, | |
2206 | { 0 } | |
2207 | }; | |
2208 | ||
1da177e4 | 2209 | static struct procunit_value_info updown_proc_info[] = { |
65f25da4 DM |
2210 | { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2211 | { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, | |
1da177e4 LT |
2212 | { 0 } |
2213 | }; | |
2214 | static struct procunit_value_info prologic_proc_info[] = { | |
65f25da4 DM |
2215 | { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2216 | { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, | |
1da177e4 LT |
2217 | { 0 } |
2218 | }; | |
2219 | static struct procunit_value_info threed_enh_proc_info[] = { | |
65f25da4 DM |
2220 | { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2221 | { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, | |
1da177e4 LT |
2222 | { 0 } |
2223 | }; | |
2224 | static struct procunit_value_info reverb_proc_info[] = { | |
65f25da4 DM |
2225 | { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2226 | { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, | |
2227 | { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, | |
2228 | { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, | |
1da177e4 LT |
2229 | { 0 } |
2230 | }; | |
2231 | static struct procunit_value_info chorus_proc_info[] = { | |
65f25da4 DM |
2232 | { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2233 | { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, | |
2234 | { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, | |
2235 | { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, | |
1da177e4 LT |
2236 | { 0 } |
2237 | }; | |
2238 | static struct procunit_value_info dcr_proc_info[] = { | |
65f25da4 DM |
2239 | { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
2240 | { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, | |
2241 | { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, | |
2242 | { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, | |
2243 | { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, | |
2244 | { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, | |
1da177e4 LT |
2245 | { 0 } |
2246 | }; | |
2247 | ||
2248 | static struct procunit_info procunits[] = { | |
65f25da4 DM |
2249 | { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, |
2250 | { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, | |
2251 | { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, | |
2252 | { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, | |
2253 | { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, | |
2254 | { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, | |
1da177e4 LT |
2255 | { 0 }, |
2256 | }; | |
0f292f02 JS |
2257 | |
2258 | static struct procunit_value_info uac3_updown_proc_info[] = { | |
2259 | { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, | |
2260 | { 0 } | |
2261 | }; | |
2262 | static struct procunit_value_info uac3_stereo_ext_proc_info[] = { | |
2263 | { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 }, | |
2264 | { 0 } | |
2265 | }; | |
2266 | ||
2267 | static struct procunit_info uac3_procunits[] = { | |
2268 | { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info }, | |
2269 | { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info }, | |
2270 | { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info }, | |
2271 | { 0 }, | |
2272 | }; | |
2273 | ||
7d2b451e SK |
2274 | /* |
2275 | * predefined data for extension units | |
2276 | */ | |
2277 | static struct procunit_value_info clock_rate_xu_info[] = { | |
e213e9cf DM |
2278 | { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, |
2279 | { 0 } | |
7d2b451e SK |
2280 | }; |
2281 | static struct procunit_value_info clock_source_xu_info[] = { | |
2282 | { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, | |
2283 | { 0 } | |
2284 | }; | |
2285 | static struct procunit_value_info spdif_format_xu_info[] = { | |
2286 | { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, | |
2287 | { 0 } | |
2288 | }; | |
2289 | static struct procunit_value_info soft_limit_xu_info[] = { | |
2290 | { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, | |
2291 | { 0 } | |
2292 | }; | |
2293 | static struct procunit_info extunits[] = { | |
2294 | { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, | |
2295 | { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, | |
2296 | { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, | |
2297 | { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, | |
2298 | { 0 } | |
2299 | }; | |
6bc170e4 | 2300 | |
1da177e4 LT |
2301 | /* |
2302 | * build a processing/extension unit | |
2303 | */ | |
6bc170e4 DM |
2304 | static int build_audio_procunit(struct mixer_build *state, int unitid, |
2305 | void *raw_desc, struct procunit_info *list, | |
2306 | char *name) | |
1da177e4 | 2307 | { |
99fc8645 | 2308 | struct uac_processing_unit_descriptor *desc = raw_desc; |
f4351a19 | 2309 | int num_ins; |
86e07d34 TI |
2310 | struct usb_mixer_elem_info *cval; |
2311 | struct snd_kcontrol *kctl; | |
1da177e4 LT |
2312 | int i, err, nameid, type, len; |
2313 | struct procunit_info *info; | |
2314 | struct procunit_value_info *valinfo; | |
c3a3e040 | 2315 | const struct usbmix_name_map *map; |
1da177e4 LT |
2316 | static struct procunit_value_info default_value_info[] = { |
2317 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, | |
2318 | { 0 } | |
2319 | }; | |
2320 | static struct procunit_info default_info = { | |
2321 | 0, NULL, default_value_info | |
2322 | }; | |
2323 | ||
f4351a19 TI |
2324 | if (desc->bLength < 13) { |
2325 | usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid); | |
2326 | return -EINVAL; | |
2327 | } | |
2328 | ||
2329 | num_ins = desc->bNrInPins; | |
2330 | if (desc->bLength < 13 + num_ins || | |
23caaf19 | 2331 | desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { |
0ba41d91 | 2332 | usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid); |
1da177e4 LT |
2333 | return -EINVAL; |
2334 | } | |
2335 | ||
2336 | for (i = 0; i < num_ins; i++) { | |
f25ecf8f TI |
2337 | err = parse_audio_unit(state, desc->baSourceID[i]); |
2338 | if (err < 0) | |
1da177e4 LT |
2339 | return err; |
2340 | } | |
2341 | ||
99fc8645 | 2342 | type = le16_to_cpu(desc->wProcessType); |
1da177e4 LT |
2343 | for (info = list; info && info->type; info++) |
2344 | if (info->type == type) | |
2345 | break; | |
6bc170e4 | 2346 | if (!info || !info->type) |
1da177e4 LT |
2347 | info = &default_info; |
2348 | ||
2349 | for (valinfo = info->values; valinfo->control; valinfo++) { | |
23caaf19 | 2350 | __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); |
99fc8645 | 2351 | |
4e887af3 JS |
2352 | if (state->mixer->protocol == UAC_VERSION_1) { |
2353 | if (!(controls[valinfo->control / 8] & | |
2354 | (1 << ((valinfo->control % 8) - 1)))) | |
2355 | continue; | |
2356 | } else { /* UAC_VERSION_2/3 */ | |
2357 | if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8], | |
2358 | valinfo->control)) | |
2359 | continue; | |
2360 | } | |
2361 | ||
17156f23 | 2362 | map = find_map(state->map, unitid, valinfo->control); |
c3a3e040 | 2363 | if (check_ignored_ctl(map)) |
1da177e4 | 2364 | continue; |
561b220a | 2365 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
a860d95f | 2366 | if (!cval) |
1da177e4 | 2367 | return -ENOMEM; |
3360b84b | 2368 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
1da177e4 LT |
2369 | cval->control = valinfo->control; |
2370 | cval->val_type = valinfo->val_type; | |
2371 | cval->channels = 1; | |
2372 | ||
4e887af3 JS |
2373 | if (state->mixer->protocol > UAC_VERSION_1 && |
2374 | !uac_v2v3_control_is_writeable(controls[valinfo->control / 8], | |
2375 | valinfo->control)) | |
2376 | cval->master_readonly = 1; | |
2377 | ||
1da177e4 | 2378 | /* get min/max values */ |
55b8cb46 JS |
2379 | switch (type) { |
2380 | case UAC_PROCESS_UP_DOWNMIX: { | |
2381 | bool mode_sel = false; | |
2382 | ||
2383 | switch (state->mixer->protocol) { | |
2384 | case UAC_VERSION_1: | |
2385 | case UAC_VERSION_2: | |
2386 | default: | |
2387 | if (cval->control == UAC_UD_MODE_SELECT) | |
2388 | mode_sel = true; | |
2389 | break; | |
2390 | case UAC_VERSION_3: | |
2391 | if (cval->control == UAC3_UD_MODE_SELECT) | |
2392 | mode_sel = true; | |
2393 | break; | |
2394 | } | |
2395 | ||
2396 | if (mode_sel) { | |
2397 | __u8 *control_spec = uac_processing_unit_specific(desc, | |
2398 | state->mixer->protocol); | |
2399 | cval->min = 1; | |
2400 | cval->max = control_spec[0]; | |
7d2b451e SK |
2401 | cval->res = 1; |
2402 | cval->initialized = 1; | |
55b8cb46 JS |
2403 | break; |
2404 | } | |
2405 | ||
2406 | get_min_max(cval, valinfo->min_value); | |
2407 | break; | |
2408 | } | |
2409 | case USB_XU_CLOCK_RATE: | |
2410 | /* | |
2411 | * E-Mu USB 0404/0202/TrackerPre/0204 | |
2412 | * samplerate control quirk | |
2413 | */ | |
2414 | cval->min = 0; | |
2415 | cval->max = 5; | |
2416 | cval->res = 1; | |
2417 | cval->initialized = 1; | |
2418 | break; | |
2419 | default: | |
2420 | get_min_max(cval, valinfo->min_value); | |
2421 | break; | |
7d2b451e | 2422 | } |
1da177e4 LT |
2423 | |
2424 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); | |
6bc170e4 | 2425 | if (!kctl) { |
1da177e4 LT |
2426 | kfree(cval); |
2427 | return -ENOMEM; | |
2428 | } | |
eef90451 | 2429 | kctl->private_free = snd_usb_mixer_elem_free; |
1da177e4 | 2430 | |
6bc170e4 | 2431 | if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) { |
c3a3e040 | 2432 | /* nothing */ ; |
6bc170e4 | 2433 | } else if (info->name) { |
1da177e4 | 2434 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); |
6bc170e4 | 2435 | } else { |
23caaf19 | 2436 | nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); |
1da177e4 LT |
2437 | len = 0; |
2438 | if (nameid) | |
eccfc1b8 RB |
2439 | len = snd_usb_copy_string_desc(state->chip, |
2440 | nameid, | |
6bc170e4 DM |
2441 | kctl->id.name, |
2442 | sizeof(kctl->id.name)); | |
2443 | if (!len) | |
1da177e4 LT |
2444 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); |
2445 | } | |
08d1e635 TI |
2446 | append_ctl_name(kctl, " "); |
2447 | append_ctl_name(kctl, valinfo->suffix); | |
1da177e4 | 2448 | |
0ba41d91 | 2449 | usb_audio_dbg(state->chip, |
6bc170e4 | 2450 | "[%d] PU [%s] ch = %d, val = %d/%d\n", |
3360b84b | 2451 | cval->head.id, kctl->id.name, cval->channels, |
6bc170e4 DM |
2452 | cval->min, cval->max); |
2453 | ||
3360b84b | 2454 | err = snd_usb_mixer_add_control(&cval->head, kctl); |
6bc170e4 | 2455 | if (err < 0) |
1da177e4 LT |
2456 | return err; |
2457 | } | |
2458 | return 0; | |
2459 | } | |
2460 | ||
6bc170e4 DM |
2461 | static int parse_audio_processing_unit(struct mixer_build *state, int unitid, |
2462 | void *raw_desc) | |
1da177e4 | 2463 | { |
0f292f02 JS |
2464 | switch (state->mixer->protocol) { |
2465 | case UAC_VERSION_1: | |
2466 | case UAC_VERSION_2: | |
2467 | default: | |
2468 | return build_audio_procunit(state, unitid, raw_desc, | |
2469 | procunits, "Processing Unit"); | |
2470 | case UAC_VERSION_3: | |
2471 | return build_audio_procunit(state, unitid, raw_desc, | |
2472 | uac3_procunits, "Processing Unit"); | |
2473 | } | |
1da177e4 LT |
2474 | } |
2475 | ||
6bc170e4 DM |
2476 | static int parse_audio_extension_unit(struct mixer_build *state, int unitid, |
2477 | void *raw_desc) | |
1da177e4 | 2478 | { |
6bc170e4 DM |
2479 | /* |
2480 | * Note that we parse extension units with processing unit descriptors. | |
2481 | * That's ok as the layout is the same. | |
2482 | */ | |
2483 | return build_audio_procunit(state, unitid, raw_desc, | |
2484 | extunits, "Extension Unit"); | |
1da177e4 LT |
2485 | } |
2486 | ||
1da177e4 LT |
2487 | /* |
2488 | * Selector Unit | |
2489 | */ | |
2490 | ||
6bc170e4 DM |
2491 | /* |
2492 | * info callback for selector unit | |
1da177e4 LT |
2493 | * use an enumerator type for routing |
2494 | */ | |
6bc170e4 DM |
2495 | static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, |
2496 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 | 2497 | { |
86e07d34 | 2498 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
2a1803a7 | 2499 | const char **itemlist = (const char **)kcontrol->private_value; |
1da177e4 | 2500 | |
5e246b85 TI |
2501 | if (snd_BUG_ON(!itemlist)) |
2502 | return -EINVAL; | |
2a1803a7 | 2503 | return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); |
1da177e4 LT |
2504 | } |
2505 | ||
2506 | /* get callback for selector unit */ | |
6bc170e4 DM |
2507 | static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, |
2508 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 2509 | { |
86e07d34 | 2510 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
2511 | int val, err; |
2512 | ||
09414207 | 2513 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
1da177e4 | 2514 | if (err < 0) { |
5aeee342 TI |
2515 | ucontrol->value.enumerated.item[0] = 0; |
2516 | return filter_error(cval, err); | |
1da177e4 LT |
2517 | } |
2518 | val = get_relative_value(cval, val); | |
2519 | ucontrol->value.enumerated.item[0] = val; | |
2520 | return 0; | |
2521 | } | |
2522 | ||
2523 | /* put callback for selector unit */ | |
6bc170e4 DM |
2524 | static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, |
2525 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 2526 | { |
86e07d34 | 2527 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
2528 | int val, oval, err; |
2529 | ||
09414207 | 2530 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
5aeee342 TI |
2531 | if (err < 0) |
2532 | return filter_error(cval, err); | |
1da177e4 LT |
2533 | val = ucontrol->value.enumerated.item[0]; |
2534 | val = get_abs_value(cval, val); | |
2535 | if (val != oval) { | |
09414207 | 2536 | set_cur_ctl_value(cval, cval->control << 8, val); |
1da177e4 LT |
2537 | return 1; |
2538 | } | |
2539 | return 0; | |
2540 | } | |
2541 | ||
2542 | /* alsa control interface for selector unit */ | |
8fdaebbb | 2543 | static const struct snd_kcontrol_new mixer_selectunit_ctl = { |
1da177e4 LT |
2544 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
2545 | .name = "", /* will be filled later */ | |
2546 | .info = mixer_ctl_selector_info, | |
2547 | .get = mixer_ctl_selector_get, | |
2548 | .put = mixer_ctl_selector_put, | |
2549 | }; | |
2550 | ||
6bc170e4 DM |
2551 | /* |
2552 | * private free callback. | |
1da177e4 LT |
2553 | * free both private_data and private_value |
2554 | */ | |
86e07d34 | 2555 | static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) |
1da177e4 LT |
2556 | { |
2557 | int i, num_ins = 0; | |
2558 | ||
2559 | if (kctl->private_data) { | |
86e07d34 | 2560 | struct usb_mixer_elem_info *cval = kctl->private_data; |
1da177e4 LT |
2561 | num_ins = cval->max; |
2562 | kfree(cval); | |
2563 | kctl->private_data = NULL; | |
2564 | } | |
2565 | if (kctl->private_value) { | |
2566 | char **itemlist = (char **)kctl->private_value; | |
2567 | for (i = 0; i < num_ins; i++) | |
2568 | kfree(itemlist[i]); | |
2569 | kfree(itemlist); | |
2570 | kctl->private_value = 0; | |
2571 | } | |
2572 | } | |
2573 | ||
2574 | /* | |
2575 | * parse a selector unit | |
2576 | */ | |
6bc170e4 DM |
2577 | static int parse_audio_selector_unit(struct mixer_build *state, int unitid, |
2578 | void *raw_desc) | |
1da177e4 | 2579 | { |
99fc8645 | 2580 | struct uac_selector_unit_descriptor *desc = raw_desc; |
1da177e4 LT |
2581 | unsigned int i, nameid, len; |
2582 | int err; | |
86e07d34 TI |
2583 | struct usb_mixer_elem_info *cval; |
2584 | struct snd_kcontrol *kctl; | |
c3a3e040 | 2585 | const struct usbmix_name_map *map; |
1da177e4 LT |
2586 | char **namelist; |
2587 | ||
f658f17b TI |
2588 | if (desc->bLength < 5 || !desc->bNrInPins || |
2589 | desc->bLength < 5 + desc->bNrInPins) { | |
0ba41d91 TI |
2590 | usb_audio_err(state->chip, |
2591 | "invalid SELECTOR UNIT descriptor %d\n", unitid); | |
1da177e4 LT |
2592 | return -EINVAL; |
2593 | } | |
2594 | ||
99fc8645 | 2595 | for (i = 0; i < desc->bNrInPins; i++) { |
f25ecf8f TI |
2596 | err = parse_audio_unit(state, desc->baSourceID[i]); |
2597 | if (err < 0) | |
1da177e4 LT |
2598 | return err; |
2599 | } | |
2600 | ||
99fc8645 | 2601 | if (desc->bNrInPins == 1) /* only one ? nonsense! */ |
1da177e4 LT |
2602 | return 0; |
2603 | ||
17156f23 | 2604 | map = find_map(state->map, unitid, 0); |
c3a3e040 | 2605 | if (check_ignored_ctl(map)) |
1da177e4 LT |
2606 | return 0; |
2607 | ||
561b220a | 2608 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
a860d95f | 2609 | if (!cval) |
1da177e4 | 2610 | return -ENOMEM; |
3360b84b | 2611 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
1da177e4 LT |
2612 | cval->val_type = USB_MIXER_U8; |
2613 | cval->channels = 1; | |
2614 | cval->min = 1; | |
99fc8645 | 2615 | cval->max = desc->bNrInPins; |
1da177e4 LT |
2616 | cval->res = 1; |
2617 | cval->initialized = 1; | |
2618 | ||
c77e1ef1 JS |
2619 | switch (state->mixer->protocol) { |
2620 | case UAC_VERSION_1: | |
2621 | default: | |
09414207 | 2622 | cval->control = 0; |
c77e1ef1 JS |
2623 | break; |
2624 | case UAC_VERSION_2: | |
2625 | case UAC_VERSION_3: | |
2626 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR || | |
2627 | desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR) | |
2628 | cval->control = UAC2_CX_CLOCK_SELECTOR; | |
2629 | else /* UAC2/3_SELECTOR_UNIT */ | |
2630 | cval->control = UAC2_SU_SELECTOR; | |
2631 | break; | |
2632 | } | |
09414207 | 2633 | |
6da2ec56 | 2634 | namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL); |
6bc170e4 | 2635 | if (!namelist) { |
1da177e4 LT |
2636 | kfree(cval); |
2637 | return -ENOMEM; | |
2638 | } | |
2639 | #define MAX_ITEM_NAME_LEN 64 | |
99fc8645 | 2640 | for (i = 0; i < desc->bNrInPins; i++) { |
86e07d34 | 2641 | struct usb_audio_term iterm; |
1da177e4 LT |
2642 | len = 0; |
2643 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); | |
6bc170e4 | 2644 | if (!namelist[i]) { |
7fbe3ca5 | 2645 | while (i--) |
1da177e4 LT |
2646 | kfree(namelist[i]); |
2647 | kfree(namelist); | |
2648 | kfree(cval); | |
2649 | return -ENOMEM; | |
2650 | } | |
8e062ec7 CL |
2651 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
2652 | MAX_ITEM_NAME_LEN); | |
99fc8645 | 2653 | if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) |
eccfc1b8 RB |
2654 | len = get_term_name(state->chip, &iterm, namelist[i], |
2655 | MAX_ITEM_NAME_LEN, 0); | |
1da177e4 | 2656 | if (! len) |
af831eef | 2657 | sprintf(namelist[i], "Input %u", i); |
1da177e4 LT |
2658 | } |
2659 | ||
2660 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); | |
2661 | if (! kctl) { | |
0ba41d91 | 2662 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
cb517359 WW |
2663 | for (i = 0; i < desc->bNrInPins; i++) |
2664 | kfree(namelist[i]); | |
878b4789 | 2665 | kfree(namelist); |
1da177e4 LT |
2666 | kfree(cval); |
2667 | return -ENOMEM; | |
2668 | } | |
2669 | kctl->private_value = (unsigned long)namelist; | |
2670 | kctl->private_free = usb_mixer_selector_elem_free; | |
2671 | ||
5a15f289 | 2672 | /* check the static mapping table at first */ |
c3a3e040 | 2673 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
89b89d12 | 2674 | if (!len) { |
5a15f289 | 2675 | /* no mapping ? */ |
c77e1ef1 JS |
2676 | switch (state->mixer->protocol) { |
2677 | case UAC_VERSION_1: | |
2678 | case UAC_VERSION_2: | |
2679 | default: | |
5a15f289 | 2680 | /* if iSelector is given, use it */ |
c77e1ef1 JS |
2681 | nameid = uac_selector_unit_iSelector(desc); |
2682 | if (nameid) | |
2683 | len = snd_usb_copy_string_desc(state->chip, | |
2684 | nameid, kctl->id.name, | |
2685 | sizeof(kctl->id.name)); | |
2686 | break; | |
2687 | case UAC_VERSION_3: | |
2688 | /* TODO: Class-Specific strings not yet supported */ | |
2689 | break; | |
2690 | } | |
2691 | ||
5a15f289 TI |
2692 | /* ... or pick up the terminal name at next */ |
2693 | if (!len) | |
eccfc1b8 | 2694 | len = get_term_name(state->chip, &state->oterm, |
1da177e4 | 2695 | kctl->id.name, sizeof(kctl->id.name), 0); |
5a15f289 | 2696 | /* ... or use the fixed string "USB" as the last resort */ |
6bc170e4 | 2697 | if (!len) |
1da177e4 LT |
2698 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); |
2699 | ||
5a15f289 | 2700 | /* and add the proper suffix */ |
c77e1ef1 JS |
2701 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR || |
2702 | desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR) | |
09414207 DM |
2703 | append_ctl_name(kctl, " Clock Source"); |
2704 | else if ((state->oterm.type & 0xff00) == 0x0100) | |
08d1e635 | 2705 | append_ctl_name(kctl, " Capture Source"); |
1da177e4 | 2706 | else |
08d1e635 | 2707 | append_ctl_name(kctl, " Playback Source"); |
1da177e4 LT |
2708 | } |
2709 | ||
0ba41d91 | 2710 | usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n", |
3360b84b TI |
2711 | cval->head.id, kctl->id.name, desc->bNrInPins); |
2712 | return snd_usb_mixer_add_control(&cval->head, kctl); | |
1da177e4 LT |
2713 | } |
2714 | ||
1da177e4 LT |
2715 | /* |
2716 | * parse an audio unit recursively | |
2717 | */ | |
2718 | ||
86e07d34 | 2719 | static int parse_audio_unit(struct mixer_build *state, int unitid) |
1da177e4 LT |
2720 | { |
2721 | unsigned char *p1; | |
9a2fe9b8 | 2722 | int protocol = state->mixer->protocol; |
1da177e4 LT |
2723 | |
2724 | if (test_and_set_bit(unitid, state->unitbitmap)) | |
2725 | return 0; /* the unit already visited */ | |
2726 | ||
2727 | p1 = find_audio_control_unit(state, unitid); | |
2728 | if (!p1) { | |
0ba41d91 | 2729 | usb_audio_err(state->chip, "unit %d not found!\n", unitid); |
1da177e4 LT |
2730 | return -EINVAL; |
2731 | } | |
2732 | ||
9a2fe9b8 RB |
2733 | if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) { |
2734 | switch (p1[2]) { | |
2735 | case UAC_INPUT_TERMINAL: | |
5a222e84 | 2736 | return parse_audio_input_terminal(state, unitid, p1); |
9a2fe9b8 RB |
2737 | case UAC_MIXER_UNIT: |
2738 | return parse_audio_mixer_unit(state, unitid, p1); | |
2739 | case UAC2_CLOCK_SOURCE: | |
2740 | return parse_clock_source_unit(state, unitid, p1); | |
2741 | case UAC_SELECTOR_UNIT: | |
2742 | case UAC2_CLOCK_SELECTOR: | |
2743 | return parse_audio_selector_unit(state, unitid, p1); | |
2744 | case UAC_FEATURE_UNIT: | |
2745 | return parse_audio_feature_unit(state, unitid, p1); | |
2746 | case UAC1_PROCESSING_UNIT: | |
2747 | /* UAC2_EFFECT_UNIT has the same value */ | |
2748 | if (protocol == UAC_VERSION_1) | |
2749 | return parse_audio_processing_unit(state, unitid, p1); | |
2750 | else | |
2751 | return 0; /* FIXME - effect units not implemented yet */ | |
2752 | case UAC1_EXTENSION_UNIT: | |
2753 | /* UAC2_PROCESSING_UNIT_V2 has the same value */ | |
2754 | if (protocol == UAC_VERSION_1) | |
2755 | return parse_audio_extension_unit(state, unitid, p1); | |
2756 | else /* UAC_VERSION_2 */ | |
2757 | return parse_audio_processing_unit(state, unitid, p1); | |
2758 | case UAC2_EXTENSION_UNIT_V2: | |
23caaf19 | 2759 | return parse_audio_extension_unit(state, unitid, p1); |
9a2fe9b8 RB |
2760 | default: |
2761 | usb_audio_err(state->chip, | |
2762 | "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); | |
2763 | return -EINVAL; | |
2764 | } | |
2765 | } else { /* UAC_VERSION_3 */ | |
2766 | switch (p1[2]) { | |
2767 | case UAC_INPUT_TERMINAL: | |
1d38f5d8 | 2768 | return parse_audio_input_terminal(state, unitid, p1); |
9a2fe9b8 RB |
2769 | case UAC3_MIXER_UNIT: |
2770 | return parse_audio_mixer_unit(state, unitid, p1); | |
2771 | case UAC3_CLOCK_SOURCE: | |
2772 | return parse_clock_source_unit(state, unitid, p1); | |
c77e1ef1 | 2773 | case UAC3_SELECTOR_UNIT: |
9a2fe9b8 RB |
2774 | case UAC3_CLOCK_SELECTOR: |
2775 | return parse_audio_selector_unit(state, unitid, p1); | |
2776 | case UAC3_FEATURE_UNIT: | |
2777 | return parse_audio_feature_unit(state, unitid, p1); | |
2778 | case UAC3_EFFECT_UNIT: | |
2779 | return 0; /* FIXME - effect units not implemented yet */ | |
2780 | case UAC3_PROCESSING_UNIT: | |
23caaf19 | 2781 | return parse_audio_processing_unit(state, unitid, p1); |
9a2fe9b8 RB |
2782 | case UAC3_EXTENSION_UNIT: |
2783 | return parse_audio_extension_unit(state, unitid, p1); | |
2784 | default: | |
2785 | usb_audio_err(state->chip, | |
2786 | "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); | |
2787 | return -EINVAL; | |
2788 | } | |
1da177e4 LT |
2789 | } |
2790 | } | |
2791 | ||
84957a8a CL |
2792 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
2793 | { | |
124751d5 TI |
2794 | /* kill pending URBs */ |
2795 | snd_usb_mixer_disconnect(mixer); | |
2796 | ||
6639b6c2 CL |
2797 | kfree(mixer->id_elems); |
2798 | if (mixer->urb) { | |
2799 | kfree(mixer->urb->transfer_buffer); | |
2800 | usb_free_urb(mixer->urb); | |
2801 | } | |
68df9de1 | 2802 | usb_free_urb(mixer->rc_urb); |
b259b10c | 2803 | kfree(mixer->rc_setup_packet); |
84957a8a CL |
2804 | kfree(mixer); |
2805 | } | |
2806 | ||
86e07d34 | 2807 | static int snd_usb_mixer_dev_free(struct snd_device *device) |
84957a8a CL |
2808 | { |
2809 | struct usb_mixer_interface *mixer = device->device_data; | |
2810 | snd_usb_mixer_free(mixer); | |
2811 | return 0; | |
2812 | } | |
2813 | ||
17156f23 RB |
2814 | /* UAC3 predefined channels configuration */ |
2815 | struct uac3_badd_profile { | |
2816 | int subclass; | |
2817 | const char *name; | |
2818 | int c_chmask; /* capture channels mask */ | |
2819 | int p_chmask; /* playback channels mask */ | |
2820 | int st_chmask; /* side tone mixing channel mask */ | |
2821 | }; | |
2822 | ||
2823 | static struct uac3_badd_profile uac3_badd_profiles[] = { | |
2824 | { | |
2825 | /* | |
2826 | * BAIF, BAOF or combination of both | |
2827 | * IN: Mono or Stereo cfg, Mono alt possible | |
2828 | * OUT: Mono or Stereo cfg, Mono alt possible | |
2829 | */ | |
2830 | .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO, | |
2831 | .name = "GENERIC IO", | |
2832 | .c_chmask = -1, /* dynamic channels */ | |
2833 | .p_chmask = -1, /* dynamic channels */ | |
2834 | }, | |
2835 | { | |
2836 | /* BAOF; Stereo only cfg, Mono alt possible */ | |
2837 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE, | |
2838 | .name = "HEADPHONE", | |
2839 | .p_chmask = 3, | |
2840 | }, | |
2841 | { | |
2842 | /* BAOF; Mono or Stereo cfg, Mono alt possible */ | |
2843 | .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER, | |
2844 | .name = "SPEAKER", | |
2845 | .p_chmask = -1, /* dynamic channels */ | |
2846 | }, | |
2847 | { | |
2848 | /* BAIF; Mono or Stereo cfg, Mono alt possible */ | |
2849 | .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE, | |
2850 | .name = "MICROPHONE", | |
2851 | .c_chmask = -1, /* dynamic channels */ | |
2852 | }, | |
2853 | { | |
2854 | /* | |
2855 | * BAIOF topology | |
2856 | * IN: Mono only | |
2857 | * OUT: Mono or Stereo cfg, Mono alt possible | |
2858 | */ | |
2859 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET, | |
2860 | .name = "HEADSET", | |
2861 | .c_chmask = 1, | |
2862 | .p_chmask = -1, /* dynamic channels */ | |
2863 | .st_chmask = 1, | |
2864 | }, | |
2865 | { | |
2866 | /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */ | |
2867 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER, | |
2868 | .name = "HEADSET ADAPTER", | |
2869 | .c_chmask = 1, | |
2870 | .p_chmask = 3, | |
2871 | .st_chmask = 1, | |
2872 | }, | |
2873 | { | |
2874 | /* BAIF + BAOF; IN: Mono only; OUT: Mono only */ | |
2875 | .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE, | |
2876 | .name = "SPEAKERPHONE", | |
2877 | .c_chmask = 1, | |
2878 | .p_chmask = 1, | |
2879 | }, | |
2880 | { 0 } /* terminator */ | |
2881 | }; | |
2882 | ||
2883 | static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer, | |
2884 | struct uac3_badd_profile *f, | |
2885 | int c_chmask, int p_chmask) | |
2886 | { | |
2887 | /* | |
2888 | * If both playback/capture channels are dynamic, make sure | |
2889 | * at least one channel is present | |
2890 | */ | |
2891 | if (f->c_chmask < 0 && f->p_chmask < 0) { | |
2892 | if (!c_chmask && !p_chmask) { | |
2893 | usb_audio_warn(mixer->chip, "BAAD %s: no channels?", | |
2894 | f->name); | |
2895 | return false; | |
2896 | } | |
2897 | return true; | |
2898 | } | |
2899 | ||
2900 | if ((f->c_chmask < 0 && !c_chmask) || | |
2901 | (f->c_chmask >= 0 && f->c_chmask != c_chmask)) { | |
2902 | usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch", | |
2903 | f->name); | |
2904 | return false; | |
2905 | } | |
2906 | if ((f->p_chmask < 0 && !p_chmask) || | |
2907 | (f->p_chmask >= 0 && f->p_chmask != p_chmask)) { | |
2908 | usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch", | |
2909 | f->name); | |
2910 | return false; | |
2911 | } | |
2912 | return true; | |
2913 | } | |
2914 | ||
2915 | /* | |
2916 | * create mixer controls for UAC3 BADD profiles | |
2917 | * | |
2918 | * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything | |
2919 | * | |
2920 | * BADD device may contain Mixer Unit, which doesn't have any controls, skip it | |
2921 | */ | |
2922 | static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer, | |
2923 | int ctrlif) | |
2924 | { | |
2925 | struct usb_device *dev = mixer->chip->dev; | |
2926 | struct usb_interface_assoc_descriptor *assoc; | |
2927 | int badd_profile = mixer->chip->badd_profile; | |
2928 | struct uac3_badd_profile *f; | |
2929 | const struct usbmix_ctl_map *map; | |
2930 | int p_chmask = 0, c_chmask = 0, st_chmask = 0; | |
2931 | int i; | |
2932 | ||
2933 | assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc; | |
2934 | ||
2935 | /* Detect BADD capture/playback channels from AS EP descriptors */ | |
2936 | for (i = 0; i < assoc->bInterfaceCount; i++) { | |
2937 | int intf = assoc->bFirstInterface + i; | |
2938 | ||
2939 | struct usb_interface *iface; | |
2940 | struct usb_host_interface *alts; | |
2941 | struct usb_interface_descriptor *altsd; | |
2942 | unsigned int maxpacksize; | |
2943 | char dir_in; | |
2944 | int chmask, num; | |
2945 | ||
2946 | if (intf == ctrlif) | |
2947 | continue; | |
2948 | ||
2949 | iface = usb_ifnum_to_if(dev, intf); | |
2950 | num = iface->num_altsetting; | |
2951 | ||
2952 | if (num < 2) | |
2953 | return -EINVAL; | |
2954 | ||
2955 | /* | |
2956 | * The number of Channels in an AudioStreaming interface | |
2957 | * and the audio sample bit resolution (16 bits or 24 | |
2958 | * bits) can be derived from the wMaxPacketSize field in | |
2959 | * the Standard AS Audio Data Endpoint descriptor in | |
2960 | * Alternate Setting 1 | |
2961 | */ | |
2962 | alts = &iface->altsetting[1]; | |
2963 | altsd = get_iface_desc(alts); | |
2964 | ||
2965 | if (altsd->bNumEndpoints < 1) | |
2966 | return -EINVAL; | |
2967 | ||
2968 | /* check direction */ | |
2969 | dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); | |
2970 | maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); | |
2971 | ||
2972 | switch (maxpacksize) { | |
2973 | default: | |
2974 | usb_audio_err(mixer->chip, | |
2975 | "incorrect wMaxPacketSize 0x%x for BADD profile\n", | |
2976 | maxpacksize); | |
2977 | return -EINVAL; | |
2978 | case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: | |
2979 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: | |
2980 | case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: | |
2981 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: | |
2982 | chmask = 1; | |
2983 | break; | |
2984 | case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: | |
2985 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: | |
2986 | case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: | |
2987 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: | |
2988 | chmask = 3; | |
2989 | break; | |
2990 | } | |
2991 | ||
2992 | if (dir_in) | |
2993 | c_chmask = chmask; | |
2994 | else | |
2995 | p_chmask = chmask; | |
2996 | } | |
2997 | ||
2998 | usb_audio_dbg(mixer->chip, | |
2999 | "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n", | |
3000 | badd_profile, c_chmask, p_chmask); | |
3001 | ||
3002 | /* check the mapping table */ | |
3003 | for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) { | |
3004 | if (map->id == badd_profile) | |
3005 | break; | |
3006 | } | |
3007 | ||
3008 | if (!map->id) | |
3009 | return -EINVAL; | |
3010 | ||
3011 | for (f = uac3_badd_profiles; f->name; f++) { | |
3012 | if (badd_profile == f->subclass) | |
3013 | break; | |
3014 | } | |
3015 | if (!f->name) | |
3016 | return -EINVAL; | |
3017 | if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask)) | |
3018 | return -EINVAL; | |
3019 | st_chmask = f->st_chmask; | |
3020 | ||
3021 | /* Playback */ | |
3022 | if (p_chmask) { | |
3023 | /* Master channel, always writable */ | |
3024 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, | |
3025 | UAC3_BADD_FU_ID2, map->map); | |
3026 | /* Mono/Stereo volume channels, always writable */ | |
3027 | build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME, | |
3028 | UAC3_BADD_FU_ID2, map->map); | |
3029 | } | |
3030 | ||
3031 | /* Capture */ | |
3032 | if (c_chmask) { | |
3033 | /* Master channel, always writable */ | |
3034 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, | |
3035 | UAC3_BADD_FU_ID5, map->map); | |
3036 | /* Mono/Stereo volume channels, always writable */ | |
3037 | build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME, | |
3038 | UAC3_BADD_FU_ID5, map->map); | |
3039 | } | |
3040 | ||
3041 | /* Side tone-mixing */ | |
3042 | if (st_chmask) { | |
3043 | /* Master channel, always writable */ | |
3044 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, | |
3045 | UAC3_BADD_FU_ID7, map->map); | |
3046 | /* Mono volume channel, always writable */ | |
3047 | build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME, | |
3048 | UAC3_BADD_FU_ID7, map->map); | |
3049 | } | |
3050 | ||
3528cd8f JS |
3051 | /* Insertion Control */ |
3052 | if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) { | |
3053 | struct usb_audio_term iterm, oterm; | |
3054 | ||
3055 | /* Input Term - Insertion control */ | |
3056 | memset(&iterm, 0, sizeof(iterm)); | |
3057 | iterm.id = UAC3_BADD_IT_ID4; | |
3058 | iterm.type = UAC_BIDIR_TERMINAL_HEADSET; | |
3059 | build_connector_control(mixer, &iterm, true); | |
3060 | ||
3061 | /* Output Term - Insertion control */ | |
3062 | memset(&oterm, 0, sizeof(oterm)); | |
3063 | oterm.id = UAC3_BADD_OT_ID3; | |
3064 | oterm.type = UAC_BIDIR_TERMINAL_HEADSET; | |
3065 | build_connector_control(mixer, &oterm, false); | |
3066 | } | |
3067 | ||
17156f23 RB |
3068 | return 0; |
3069 | } | |
3070 | ||
1da177e4 LT |
3071 | /* |
3072 | * create mixer controls | |
3073 | * | |
de48c7bc | 3074 | * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers |
1da177e4 | 3075 | */ |
84957a8a | 3076 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
1da177e4 | 3077 | { |
86e07d34 | 3078 | struct mixer_build state; |
1da177e4 LT |
3079 | int err; |
3080 | const struct usbmix_ctl_map *map; | |
23caaf19 | 3081 | void *p; |
1da177e4 LT |
3082 | |
3083 | memset(&state, 0, sizeof(state)); | |
84957a8a CL |
3084 | state.chip = mixer->chip; |
3085 | state.mixer = mixer; | |
1faa5d07 DM |
3086 | state.buffer = mixer->hostif->extra; |
3087 | state.buflen = mixer->hostif->extralen; | |
1da177e4 LT |
3088 | |
3089 | /* check the mapping table */ | |
27d10f56 CL |
3090 | for (map = usbmix_ctl_maps; map->id; map++) { |
3091 | if (map->id == state.chip->usb_id) { | |
1da177e4 | 3092 | state.map = map->map; |
8e062ec7 | 3093 | state.selector_map = map->selector_map; |
84957a8a | 3094 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
1da177e4 LT |
3095 | break; |
3096 | } | |
3097 | } | |
1da177e4 | 3098 | |
23caaf19 | 3099 | p = NULL; |
6bc170e4 DM |
3100 | while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, |
3101 | mixer->hostif->extralen, | |
1faa5d07 | 3102 | p, UAC_OUTPUT_TERMINAL)) != NULL) { |
23caaf19 | 3103 | if (mixer->protocol == UAC_VERSION_1) { |
69da9bcb | 3104 | struct uac1_output_terminal_descriptor *desc = p; |
23caaf19 DM |
3105 | |
3106 | if (desc->bLength < sizeof(*desc)) | |
3107 | continue; /* invalid descriptor? */ | |
6bc170e4 DM |
3108 | /* mark terminal ID as visited */ |
3109 | set_bit(desc->bTerminalID, state.unitbitmap); | |
23caaf19 DM |
3110 | state.oterm.id = desc->bTerminalID; |
3111 | state.oterm.type = le16_to_cpu(desc->wTerminalType); | |
3112 | state.oterm.name = desc->iTerminal; | |
3113 | err = parse_audio_unit(&state, desc->bSourceID); | |
83ea5d18 | 3114 | if (err < 0 && err != -EINVAL) |
23caaf19 | 3115 | return err; |
9a2fe9b8 | 3116 | } else if (mixer->protocol == UAC_VERSION_2) { |
23caaf19 DM |
3117 | struct uac2_output_terminal_descriptor *desc = p; |
3118 | ||
3119 | if (desc->bLength < sizeof(*desc)) | |
3120 | continue; /* invalid descriptor? */ | |
6bc170e4 DM |
3121 | /* mark terminal ID as visited */ |
3122 | set_bit(desc->bTerminalID, state.unitbitmap); | |
23caaf19 DM |
3123 | state.oterm.id = desc->bTerminalID; |
3124 | state.oterm.type = le16_to_cpu(desc->wTerminalType); | |
3125 | state.oterm.name = desc->iTerminal; | |
3126 | err = parse_audio_unit(&state, desc->bSourceID); | |
83ea5d18 | 3127 | if (err < 0 && err != -EINVAL) |
23caaf19 | 3128 | return err; |
09414207 | 3129 | |
6bc170e4 DM |
3130 | /* |
3131 | * For UAC2, use the same approach to also add the | |
3132 | * clock selectors | |
3133 | */ | |
09414207 | 3134 | err = parse_audio_unit(&state, desc->bCSourceID); |
83ea5d18 | 3135 | if (err < 0 && err != -EINVAL) |
09414207 | 3136 | return err; |
5a222e84 | 3137 | |
2b54f785 | 3138 | if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls), |
5a222e84 | 3139 | UAC2_TE_CONNECTOR)) { |
167e1fb1 | 3140 | build_connector_control(state.mixer, &state.oterm, |
5a222e84 AC |
3141 | false); |
3142 | } | |
9a2fe9b8 RB |
3143 | } else { /* UAC_VERSION_3 */ |
3144 | struct uac3_output_terminal_descriptor *desc = p; | |
3145 | ||
3146 | if (desc->bLength < sizeof(*desc)) | |
3147 | continue; /* invalid descriptor? */ | |
3148 | /* mark terminal ID as visited */ | |
3149 | set_bit(desc->bTerminalID, state.unitbitmap); | |
3150 | state.oterm.id = desc->bTerminalID; | |
3151 | state.oterm.type = le16_to_cpu(desc->wTerminalType); | |
3152 | state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr); | |
3153 | err = parse_audio_unit(&state, desc->bSourceID); | |
3154 | if (err < 0 && err != -EINVAL) | |
3155 | return err; | |
3156 | ||
3157 | /* | |
3158 | * For UAC3, use the same approach to also add the | |
3159 | * clock selectors | |
3160 | */ | |
3161 | err = parse_audio_unit(&state, desc->bCSourceID); | |
3162 | if (err < 0 && err != -EINVAL) | |
3163 | return err; | |
1d38f5d8 JS |
3164 | |
3165 | if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls), | |
3166 | UAC3_TE_INSERTION)) { | |
167e1fb1 | 3167 | build_connector_control(state.mixer, &state.oterm, |
1d38f5d8 JS |
3168 | false); |
3169 | } | |
23caaf19 | 3170 | } |
1da177e4 | 3171 | } |
23caaf19 | 3172 | |
1da177e4 LT |
3173 | return 0; |
3174 | } | |
84957a8a | 3175 | |
7b1eda22 | 3176 | void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) |
6639b6c2 | 3177 | { |
3360b84b | 3178 | struct usb_mixer_elem_list *list; |
6639b6c2 | 3179 | |
8c558076 | 3180 | for_each_mixer_elem(list, mixer, unitid) { |
b2500b58 | 3181 | struct usb_mixer_elem_info *info = |
8c558076 | 3182 | mixer_elem_list_to_info(list); |
b2500b58 JS |
3183 | /* invalidate cache, so the value is read from the device */ |
3184 | info->cached = 0; | |
6639b6c2 | 3185 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
3360b84b | 3186 | &list->kctl->id); |
b2500b58 | 3187 | } |
6639b6c2 CL |
3188 | } |
3189 | ||
ebfdeea3 | 3190 | static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, |
3360b84b | 3191 | struct usb_mixer_elem_list *list) |
ebfdeea3 | 3192 | { |
8c558076 | 3193 | struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); |
ebfdeea3 JK |
3194 | static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", |
3195 | "S8", "U8", "S16", "U16"}; | |
ebfdeea3 | 3196 | snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " |
3360b84b | 3197 | "channels=%i, type=\"%s\"\n", cval->head.id, |
ebfdeea3 JK |
3198 | cval->control, cval->cmask, cval->channels, |
3199 | val_types[cval->val_type]); | |
3200 | snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", | |
3201 | cval->min, cval->max, cval->dBmin, cval->dBmax); | |
3202 | } | |
3203 | ||
3204 | static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, | |
3205 | struct snd_info_buffer *buffer) | |
3206 | { | |
3207 | struct snd_usb_audio *chip = entry->private_data; | |
3208 | struct usb_mixer_interface *mixer; | |
3360b84b | 3209 | struct usb_mixer_elem_list *list; |
ebfdeea3 JK |
3210 | int unitid; |
3211 | ||
3212 | list_for_each_entry(mixer, &chip->mixer_list, list) { | |
3213 | snd_iprintf(buffer, | |
7affdc17 | 3214 | "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", |
3d8d4dcf | 3215 | chip->usb_id, snd_usb_ctrl_intf(chip), |
7affdc17 | 3216 | mixer->ignore_ctl_error); |
ebfdeea3 JK |
3217 | snd_iprintf(buffer, "Card: %s\n", chip->card->longname); |
3218 | for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { | |
8c558076 | 3219 | for_each_mixer_elem(list, mixer, unitid) { |
3360b84b TI |
3220 | snd_iprintf(buffer, " Unit: %i\n", list->id); |
3221 | if (list->kctl) | |
3222 | snd_iprintf(buffer, | |
3223 | " Control: name=\"%s\", index=%i\n", | |
3224 | list->kctl->id.name, | |
3225 | list->kctl->id.index); | |
3226 | if (list->dump) | |
3227 | list->dump(buffer, list); | |
3228 | } | |
ebfdeea3 JK |
3229 | } |
3230 | } | |
3231 | } | |
3232 | ||
e213e9cf DM |
3233 | static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, |
3234 | int attribute, int value, int index) | |
3235 | { | |
3360b84b | 3236 | struct usb_mixer_elem_list *list; |
e213e9cf DM |
3237 | __u8 unitid = (index >> 8) & 0xff; |
3238 | __u8 control = (value >> 8) & 0xff; | |
3239 | __u8 channel = value & 0xff; | |
191227d9 | 3240 | unsigned int count = 0; |
e213e9cf DM |
3241 | |
3242 | if (channel >= MAX_CHANNELS) { | |
0ba41d91 TI |
3243 | usb_audio_dbg(mixer->chip, |
3244 | "%s(): bogus channel number %d\n", | |
3245 | __func__, channel); | |
e213e9cf DM |
3246 | return; |
3247 | } | |
3248 | ||
8c558076 | 3249 | for_each_mixer_elem(list, mixer, unitid) |
191227d9 DM |
3250 | count++; |
3251 | ||
3252 | if (count == 0) | |
3253 | return; | |
3254 | ||
8c558076 | 3255 | for_each_mixer_elem(list, mixer, unitid) { |
3360b84b TI |
3256 | struct usb_mixer_elem_info *info; |
3257 | ||
3258 | if (!list->kctl) | |
3259 | continue; | |
3260 | ||
8c558076 | 3261 | info = mixer_elem_list_to_info(list); |
191227d9 | 3262 | if (count > 1 && info->control != control) |
e213e9cf DM |
3263 | continue; |
3264 | ||
3265 | switch (attribute) { | |
3266 | case UAC2_CS_CUR: | |
3267 | /* invalidate cache, so the value is read from the device */ | |
3268 | if (channel) | |
3269 | info->cached &= ~(1 << channel); | |
3270 | else /* master channel */ | |
3271 | info->cached = 0; | |
3272 | ||
3273 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
3360b84b | 3274 | &info->head.kctl->id); |
e213e9cf DM |
3275 | break; |
3276 | ||
3277 | case UAC2_CS_RANGE: | |
3278 | /* TODO */ | |
3279 | break; | |
3280 | ||
3281 | case UAC2_CS_MEM: | |
3282 | /* TODO */ | |
3283 | break; | |
3284 | ||
3285 | default: | |
0ba41d91 TI |
3286 | usb_audio_dbg(mixer->chip, |
3287 | "unknown attribute %d in interrupt\n", | |
3288 | attribute); | |
e213e9cf DM |
3289 | break; |
3290 | } /* switch */ | |
3291 | } | |
3292 | } | |
3293 | ||
3294 | static void snd_usb_mixer_interrupt(struct urb *urb) | |
6639b6c2 CL |
3295 | { |
3296 | struct usb_mixer_interface *mixer = urb->context; | |
e213e9cf | 3297 | int len = urb->actual_length; |
edf7de31 | 3298 | int ustatus = urb->status; |
e213e9cf | 3299 | |
edf7de31 | 3300 | if (ustatus != 0) |
e213e9cf | 3301 | goto requeue; |
6639b6c2 | 3302 | |
e213e9cf DM |
3303 | if (mixer->protocol == UAC_VERSION_1) { |
3304 | struct uac1_status_word *status; | |
6639b6c2 | 3305 | |
e213e9cf DM |
3306 | for (status = urb->transfer_buffer; |
3307 | len >= sizeof(*status); | |
3308 | len -= sizeof(*status), status++) { | |
0ba41d91 | 3309 | dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n", |
e213e9cf DM |
3310 | status->bStatusType, |
3311 | status->bOriginator); | |
3312 | ||
6639b6c2 | 3313 | /* ignore any notifications not from the control interface */ |
e213e9cf DM |
3314 | if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != |
3315 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) | |
6639b6c2 | 3316 | continue; |
e213e9cf DM |
3317 | |
3318 | if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) | |
3319 | snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); | |
b259b10c | 3320 | else |
e213e9cf DM |
3321 | snd_usb_mixer_notify_id(mixer, status->bOriginator); |
3322 | } | |
3323 | } else { /* UAC_VERSION_2 */ | |
3324 | struct uac2_interrupt_data_msg *msg; | |
3325 | ||
3326 | for (msg = urb->transfer_buffer; | |
3327 | len >= sizeof(*msg); | |
3328 | len -= sizeof(*msg), msg++) { | |
3329 | /* drop vendor specific and endpoint requests */ | |
3330 | if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || | |
3331 | (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) | |
3332 | continue; | |
3333 | ||
3334 | snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, | |
3335 | le16_to_cpu(msg->wValue), | |
3336 | le16_to_cpu(msg->wIndex)); | |
6639b6c2 CL |
3337 | } |
3338 | } | |
e213e9cf DM |
3339 | |
3340 | requeue: | |
6bc170e4 DM |
3341 | if (ustatus != -ENOENT && |
3342 | ustatus != -ECONNRESET && | |
3343 | ustatus != -ESHUTDOWN) { | |
6639b6c2 CL |
3344 | urb->dev = mixer->chip->dev; |
3345 | usb_submit_urb(urb, GFP_ATOMIC); | |
3346 | } | |
3347 | } | |
3348 | ||
3349 | /* create the handler for the optional status interrupt endpoint */ | |
3350 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) | |
3351 | { | |
6639b6c2 CL |
3352 | struct usb_endpoint_descriptor *ep; |
3353 | void *transfer_buffer; | |
3354 | int buffer_length; | |
3355 | unsigned int epnum; | |
3356 | ||
6639b6c2 | 3357 | /* we need one interrupt input endpoint */ |
1faa5d07 | 3358 | if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1) |
6639b6c2 | 3359 | return 0; |
1faa5d07 | 3360 | ep = get_endpoint(mixer->hostif, 0); |
913ae5a2 | 3361 | if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) |
6639b6c2 CL |
3362 | return 0; |
3363 | ||
42a6e66f | 3364 | epnum = usb_endpoint_num(ep); |
6639b6c2 CL |
3365 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); |
3366 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); | |
3367 | if (!transfer_buffer) | |
3368 | return -ENOMEM; | |
3369 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); | |
3370 | if (!mixer->urb) { | |
3371 | kfree(transfer_buffer); | |
3372 | return -ENOMEM; | |
3373 | } | |
3374 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, | |
3375 | usb_rcvintpipe(mixer->chip->dev, epnum), | |
3376 | transfer_buffer, buffer_length, | |
e213e9cf | 3377 | snd_usb_mixer_interrupt, mixer, ep->bInterval); |
6639b6c2 CL |
3378 | usb_submit_urb(mixer->urb, GFP_KERNEL); |
3379 | return 0; | |
3380 | } | |
3381 | ||
4120fbed TI |
3382 | static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol, |
3383 | struct snd_ctl_elem_value *ucontrol) | |
3384 | { | |
3385 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); | |
3386 | ||
3387 | ucontrol->value.integer.value[0] = mixer->chip->keep_iface; | |
3388 | return 0; | |
3389 | } | |
3390 | ||
3391 | static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol, | |
3392 | struct snd_ctl_elem_value *ucontrol) | |
3393 | { | |
3394 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); | |
3395 | bool keep_iface = !!ucontrol->value.integer.value[0]; | |
3396 | ||
3397 | if (mixer->chip->keep_iface == keep_iface) | |
3398 | return 0; | |
3399 | mixer->chip->keep_iface = keep_iface; | |
3400 | return 1; | |
3401 | } | |
3402 | ||
3403 | static const struct snd_kcontrol_new keep_iface_ctl = { | |
3404 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, | |
3405 | .name = "Keep Interface", | |
3406 | .info = snd_ctl_boolean_mono_info, | |
3407 | .get = keep_iface_ctl_get, | |
3408 | .put = keep_iface_ctl_put, | |
3409 | }; | |
3410 | ||
3411 | static int create_keep_iface_ctl(struct usb_mixer_interface *mixer) | |
3412 | { | |
3413 | struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer); | |
3414 | ||
3415 | /* need only one control per card */ | |
3416 | if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) { | |
3417 | snd_ctl_free_one(kctl); | |
3418 | return 0; | |
3419 | } | |
3420 | ||
3421 | return snd_ctl_add(mixer->chip->card, kctl); | |
3422 | } | |
3423 | ||
7a9b8063 TI |
3424 | int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, |
3425 | int ignore_error) | |
84957a8a | 3426 | { |
86e07d34 | 3427 | static struct snd_device_ops dev_ops = { |
84957a8a CL |
3428 | .dev_free = snd_usb_mixer_dev_free |
3429 | }; | |
3430 | struct usb_mixer_interface *mixer; | |
23caaf19 | 3431 | int err; |
84957a8a CL |
3432 | |
3433 | strcpy(chip->card->mixername, "USB Mixer"); | |
3434 | ||
561b220a | 3435 | mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); |
84957a8a CL |
3436 | if (!mixer) |
3437 | return -ENOMEM; | |
3438 | mixer->chip = chip; | |
7a9b8063 | 3439 | mixer->ignore_ctl_error = ignore_error; |
291186e0 JK |
3440 | mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), |
3441 | GFP_KERNEL); | |
6639b6c2 CL |
3442 | if (!mixer->id_elems) { |
3443 | kfree(mixer); | |
3444 | return -ENOMEM; | |
3445 | } | |
84957a8a | 3446 | |
1faa5d07 DM |
3447 | mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; |
3448 | switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) { | |
a2acad82 CL |
3449 | case UAC_VERSION_1: |
3450 | default: | |
3451 | mixer->protocol = UAC_VERSION_1; | |
3452 | break; | |
3453 | case UAC_VERSION_2: | |
3454 | mixer->protocol = UAC_VERSION_2; | |
3455 | break; | |
9a2fe9b8 RB |
3456 | case UAC_VERSION_3: |
3457 | mixer->protocol = UAC_VERSION_3; | |
3458 | break; | |
a2acad82 | 3459 | } |
7b8a043f | 3460 | |
17156f23 RB |
3461 | if (mixer->protocol == UAC_VERSION_3 && |
3462 | chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { | |
f25ecf8f TI |
3463 | err = snd_usb_mixer_controls_badd(mixer, ctrlif); |
3464 | if (err < 0) | |
3465 | goto _error; | |
3466 | } else { | |
3467 | err = snd_usb_mixer_controls(mixer); | |
3468 | if (err < 0) | |
3469 | goto _error; | |
17156f23 | 3470 | } |
ad6baae6 JS |
3471 | |
3472 | err = snd_usb_mixer_status_create(mixer); | |
3473 | if (err < 0) | |
3474 | goto _error; | |
3475 | ||
4120fbed TI |
3476 | err = create_keep_iface_ctl(mixer); |
3477 | if (err < 0) | |
3478 | goto _error; | |
84957a8a | 3479 | |
328e9f69 TI |
3480 | err = snd_usb_mixer_apply_create_quirk(mixer); |
3481 | if (err < 0) | |
3482 | goto _error; | |
468b8fde | 3483 | |
9cbb2808 | 3484 | err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops); |
93446edc CL |
3485 | if (err < 0) |
3486 | goto _error; | |
ebfdeea3 | 3487 | |
7449054a TI |
3488 | if (list_empty(&chip->mixer_list)) |
3489 | snd_card_ro_proc_new(chip->card, "usbmixer", chip, | |
3490 | snd_usb_mixer_proc_read); | |
ebfdeea3 | 3491 | |
84957a8a CL |
3492 | list_add(&mixer->list, &chip->mixer_list); |
3493 | return 0; | |
93446edc CL |
3494 | |
3495 | _error: | |
3496 | snd_usb_mixer_free(mixer); | |
3497 | return err; | |
84957a8a CL |
3498 | } |
3499 | ||
a6cece9d | 3500 | void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) |
84957a8a | 3501 | { |
124751d5 TI |
3502 | if (mixer->disconnected) |
3503 | return; | |
3504 | if (mixer->urb) | |
3505 | usb_kill_urb(mixer->urb); | |
3506 | if (mixer->rc_urb) | |
3507 | usb_kill_urb(mixer->rc_urb); | |
3508 | mixer->disconnected = true; | |
84957a8a | 3509 | } |
400362f1 TI |
3510 | |
3511 | #ifdef CONFIG_PM | |
3512 | /* stop any bus activity of a mixer */ | |
3513 | static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) | |
3514 | { | |
3515 | usb_kill_urb(mixer->urb); | |
3516 | usb_kill_urb(mixer->rc_urb); | |
3517 | } | |
3518 | ||
3519 | static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) | |
3520 | { | |
3521 | int err; | |
3522 | ||
3523 | if (mixer->urb) { | |
3524 | err = usb_submit_urb(mixer->urb, GFP_NOIO); | |
3525 | if (err < 0) | |
3526 | return err; | |
3527 | } | |
3528 | ||
3529 | return 0; | |
3530 | } | |
3531 | ||
3532 | int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) | |
3533 | { | |
3534 | snd_usb_mixer_inactivate(mixer); | |
3535 | return 0; | |
3536 | } | |
3537 | ||
3360b84b | 3538 | static int restore_mixer_value(struct usb_mixer_elem_list *list) |
400362f1 | 3539 | { |
8c558076 | 3540 | struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); |
400362f1 TI |
3541 | int c, err, idx; |
3542 | ||
3543 | if (cval->cmask) { | |
3544 | idx = 0; | |
3545 | for (c = 0; c < MAX_CHANNELS; c++) { | |
3546 | if (!(cval->cmask & (1 << c))) | |
3547 | continue; | |
6aa6925c | 3548 | if (cval->cached & (1 << (c + 1))) { |
eef90451 | 3549 | err = snd_usb_set_cur_mix_value(cval, c + 1, idx, |
400362f1 TI |
3550 | cval->cache_val[idx]); |
3551 | if (err < 0) | |
3552 | return err; | |
3553 | } | |
3554 | idx++; | |
3555 | } | |
3556 | } else { | |
3557 | /* master */ | |
3558 | if (cval->cached) { | |
eef90451 | 3559 | err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val); |
400362f1 TI |
3560 | if (err < 0) |
3561 | return err; | |
3562 | } | |
3563 | } | |
3564 | ||
3565 | return 0; | |
3566 | } | |
3567 | ||
3568 | int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume) | |
3569 | { | |
3360b84b | 3570 | struct usb_mixer_elem_list *list; |
400362f1 TI |
3571 | int id, err; |
3572 | ||
400362f1 TI |
3573 | if (reset_resume) { |
3574 | /* restore cached mixer values */ | |
3575 | for (id = 0; id < MAX_ID_ELEMS; id++) { | |
8c558076 | 3576 | for_each_mixer_elem(list, mixer, id) { |
3360b84b TI |
3577 | if (list->resume) { |
3578 | err = list->resume(list); | |
3579 | if (err < 0) | |
3580 | return err; | |
3581 | } | |
400362f1 TI |
3582 | } |
3583 | } | |
3584 | } | |
3585 | ||
964af639 TI |
3586 | snd_usb_mixer_resume_quirk(mixer); |
3587 | ||
400362f1 TI |
3588 | return snd_usb_mixer_activate(mixer); |
3589 | } | |
3590 | #endif | |
3360b84b TI |
3591 | |
3592 | void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list, | |
3593 | struct usb_mixer_interface *mixer, | |
3594 | int unitid) | |
3595 | { | |
3596 | list->mixer = mixer; | |
3597 | list->id = unitid; | |
3598 | list->dump = snd_usb_mixer_dump_cval; | |
3599 | #ifdef CONFIG_PM | |
3600 | list->resume = restore_mixer_value; | |
3601 | #endif | |
3602 | } |