Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * (Tentative) USB Audio Driver for ALSA | |
3 | * | |
4 | * Mixer control part | |
5 | * | |
6 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> | |
7 | * | |
8 | * Many codes borrowed from audio.c by | |
9 | * Alan Cox (alan@lxorguk.ukuu.org.uk) | |
10 | * Thomas Sailer (sailer@ife.ee.ethz.ch) | |
11 | * | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
26 | * | |
27 | */ | |
28 | ||
157a57b6 DM |
29 | /* |
30 | * TODOs, for both the mixer and the streaming interfaces: | |
31 | * | |
32 | * - support for UAC2 effect units | |
33 | * - support for graphical equalizers | |
34 | * - RANGE and MEM set commands (UAC2) | |
35 | * - RANGE and MEM interrupt dispatchers (UAC2) | |
36 | * - audio channel clustering (UAC2) | |
37 | * - audio sample rate converter units (UAC2) | |
38 | * - proper handling of clock multipliers (UAC2) | |
39 | * - dispatch clock change notifications (UAC2) | |
40 | * - stop PCM streams which use a clock that became invalid | |
41 | * - stop PCM streams which use a clock selector that has changed | |
42 | * - parse available sample rates again when clock sources changed | |
43 | */ | |
44 | ||
1da177e4 LT |
45 | #include <linux/bitops.h> |
46 | #include <linux/init.h> | |
47 | #include <linux/list.h> | |
48 | #include <linux/slab.h> | |
49 | #include <linux/string.h> | |
50 | #include <linux/usb.h> | |
28e1b773 | 51 | #include <linux/usb/audio.h> |
23caaf19 | 52 | #include <linux/usb/audio-v2.h> |
28e1b773 | 53 | |
1da177e4 LT |
54 | #include <sound/core.h> |
55 | #include <sound/control.h> | |
b259b10c | 56 | #include <sound/hwdep.h> |
aafad562 | 57 | #include <sound/info.h> |
7bc5ba7e | 58 | #include <sound/tlv.h> |
1da177e4 LT |
59 | |
60 | #include "usbaudio.h" | |
f0b5e634 | 61 | #include "mixer.h" |
e5779998 | 62 | #include "helper.h" |
7b1eda22 | 63 | #include "mixer_quirks.h" |
88a8516a | 64 | #include "power.h" |
4d1a70da | 65 | |
ebfdeea3 JK |
66 | #define MAX_ID_ELEMS 256 |
67 | ||
1da177e4 LT |
68 | struct usb_audio_term { |
69 | int id; | |
70 | int type; | |
71 | int channels; | |
72 | unsigned int chconfig; | |
73 | int name; | |
74 | }; | |
75 | ||
76 | struct usbmix_name_map; | |
77 | ||
86e07d34 TI |
78 | struct mixer_build { |
79 | struct snd_usb_audio *chip; | |
84957a8a | 80 | struct usb_mixer_interface *mixer; |
1da177e4 LT |
81 | unsigned char *buffer; |
82 | unsigned int buflen; | |
291186e0 | 83 | DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); |
86e07d34 | 84 | struct usb_audio_term oterm; |
1da177e4 | 85 | const struct usbmix_name_map *map; |
8e062ec7 | 86 | const struct usbmix_selector_map *selector_map; |
1da177e4 LT |
87 | }; |
88 | ||
1da177e4 LT |
89 | enum { |
90 | USB_MIXER_BOOLEAN, | |
91 | USB_MIXER_INV_BOOLEAN, | |
92 | USB_MIXER_S8, | |
93 | USB_MIXER_U8, | |
94 | USB_MIXER_S16, | |
95 | USB_MIXER_U16, | |
96 | }; | |
97 | ||
1da177e4 | 98 | |
1cdfa9f3 | 99 | /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/ |
7d2b451e SK |
100 | enum { |
101 | USB_XU_CLOCK_RATE = 0xe301, | |
102 | USB_XU_CLOCK_SOURCE = 0xe302, | |
103 | USB_XU_DIGITAL_IO_STATUS = 0xe303, | |
104 | USB_XU_DEVICE_OPTIONS = 0xe304, | |
105 | USB_XU_DIRECT_MONITORING = 0xe305, | |
106 | USB_XU_METERING = 0xe306 | |
107 | }; | |
108 | enum { | |
109 | USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/ | |
110 | USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */ | |
111 | USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */ | |
112 | USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */ | |
113 | }; | |
1da177e4 LT |
114 | |
115 | /* | |
116 | * manual mapping of mixer names | |
117 | * if the mixer topology is too complicated and the parsed names are | |
118 | * ambiguous, add the entries in usbmixer_maps.c. | |
119 | */ | |
f0b5e634 | 120 | #include "mixer_maps.c" |
1da177e4 | 121 | |
c3a3e040 JK |
122 | static const struct usbmix_name_map * |
123 | find_map(struct mixer_build *state, int unitid, int control) | |
1da177e4 | 124 | { |
c3a3e040 | 125 | const struct usbmix_name_map *p = state->map; |
1da177e4 | 126 | |
c3a3e040 JK |
127 | if (!p) |
128 | return NULL; | |
1da177e4 LT |
129 | |
130 | for (p = state->map; p->id; p++) { | |
c3a3e040 JK |
131 | if (p->id == unitid && |
132 | (!control || !p->control || control == p->control)) | |
133 | return p; | |
1da177e4 | 134 | } |
c3a3e040 | 135 | return NULL; |
1da177e4 LT |
136 | } |
137 | ||
c3a3e040 JK |
138 | /* get the mapped name if the unit matches */ |
139 | static int | |
140 | check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) | |
1da177e4 | 141 | { |
c3a3e040 JK |
142 | if (!p || !p->name) |
143 | return 0; | |
1da177e4 | 144 | |
c3a3e040 JK |
145 | buflen--; |
146 | return strlcpy(buf, p->name, buflen); | |
147 | } | |
148 | ||
149 | /* check whether the control should be ignored */ | |
150 | static inline int | |
151 | check_ignored_ctl(const struct usbmix_name_map *p) | |
152 | { | |
153 | if (!p || p->name || p->dB) | |
1da177e4 | 154 | return 0; |
c3a3e040 JK |
155 | return 1; |
156 | } | |
157 | ||
158 | /* dB mapping */ | |
159 | static inline void check_mapped_dB(const struct usbmix_name_map *p, | |
160 | struct usb_mixer_elem_info *cval) | |
161 | { | |
162 | if (p && p->dB) { | |
163 | cval->dBmin = p->dB->min; | |
164 | cval->dBmax = p->dB->max; | |
1da177e4 | 165 | } |
1da177e4 LT |
166 | } |
167 | ||
8e062ec7 | 168 | /* get the mapped selector source name */ |
86e07d34 | 169 | static int check_mapped_selector_name(struct mixer_build *state, int unitid, |
8e062ec7 CL |
170 | int index, char *buf, int buflen) |
171 | { | |
172 | const struct usbmix_selector_map *p; | |
173 | ||
174 | if (! state->selector_map) | |
175 | return 0; | |
176 | for (p = state->selector_map; p->id; p++) { | |
177 | if (p->id == unitid && index < p->count) | |
178 | return strlcpy(buf, p->names[index], buflen); | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
1da177e4 LT |
183 | /* |
184 | * find an audio control unit with the given unit id | |
185 | */ | |
86e07d34 | 186 | static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit) |
1da177e4 | 187 | { |
67e1daa0 DM |
188 | /* we just parse the header */ |
189 | struct uac_feature_unit_descriptor *hdr = NULL; | |
190 | ||
191 | while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr, | |
192 | USB_DT_CS_INTERFACE)) != NULL) { | |
193 | if (hdr->bLength >= 4 && | |
194 | hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL && | |
195 | hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER && | |
196 | hdr->bUnitID == unit) | |
197 | return hdr; | |
1da177e4 | 198 | } |
67e1daa0 | 199 | |
1da177e4 LT |
200 | return NULL; |
201 | } | |
202 | ||
1da177e4 LT |
203 | /* |
204 | * copy a string with the given id | |
205 | */ | |
86e07d34 | 206 | static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen) |
1da177e4 LT |
207 | { |
208 | int len = usb_string(state->chip->dev, index, buf, maxlen - 1); | |
209 | buf[len] = 0; | |
210 | return len; | |
211 | } | |
212 | ||
213 | /* | |
214 | * convert from the byte/word on usb descriptor to the zero-based integer | |
215 | */ | |
86e07d34 | 216 | static int convert_signed_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
217 | { |
218 | switch (cval->val_type) { | |
219 | case USB_MIXER_BOOLEAN: | |
220 | return !!val; | |
221 | case USB_MIXER_INV_BOOLEAN: | |
222 | return !val; | |
223 | case USB_MIXER_U8: | |
224 | val &= 0xff; | |
225 | break; | |
226 | case USB_MIXER_S8: | |
227 | val &= 0xff; | |
228 | if (val >= 0x80) | |
229 | val -= 0x100; | |
230 | break; | |
231 | case USB_MIXER_U16: | |
232 | val &= 0xffff; | |
233 | break; | |
234 | case USB_MIXER_S16: | |
235 | val &= 0xffff; | |
236 | if (val >= 0x8000) | |
237 | val -= 0x10000; | |
238 | break; | |
239 | } | |
240 | return val; | |
241 | } | |
242 | ||
243 | /* | |
244 | * convert from the zero-based int to the byte/word for usb descriptor | |
245 | */ | |
86e07d34 | 246 | static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
247 | { |
248 | switch (cval->val_type) { | |
249 | case USB_MIXER_BOOLEAN: | |
250 | return !!val; | |
251 | case USB_MIXER_INV_BOOLEAN: | |
252 | return !val; | |
253 | case USB_MIXER_S8: | |
254 | case USB_MIXER_U8: | |
255 | return val & 0xff; | |
256 | case USB_MIXER_S16: | |
257 | case USB_MIXER_U16: | |
258 | return val & 0xffff; | |
259 | } | |
260 | return 0; /* not reached */ | |
261 | } | |
262 | ||
86e07d34 | 263 | static int get_relative_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
264 | { |
265 | if (! cval->res) | |
266 | cval->res = 1; | |
267 | if (val < cval->min) | |
268 | return 0; | |
14790f1c TI |
269 | else if (val >= cval->max) |
270 | return (cval->max - cval->min + cval->res - 1) / cval->res; | |
1da177e4 LT |
271 | else |
272 | return (val - cval->min) / cval->res; | |
273 | } | |
274 | ||
86e07d34 | 275 | static int get_abs_value(struct usb_mixer_elem_info *cval, int val) |
1da177e4 LT |
276 | { |
277 | if (val < 0) | |
278 | return cval->min; | |
279 | if (! cval->res) | |
280 | cval->res = 1; | |
281 | val *= cval->res; | |
282 | val += cval->min; | |
283 | if (val > cval->max) | |
284 | return cval->max; | |
285 | return val; | |
286 | } | |
287 | ||
288 | ||
289 | /* | |
290 | * retrieve a mixer value | |
291 | */ | |
292 | ||
23caaf19 | 293 | static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) |
1da177e4 | 294 | { |
3d8d4dcf | 295 | struct snd_usb_audio *chip = cval->mixer->chip; |
1da177e4 LT |
296 | unsigned char buf[2]; |
297 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; | |
298 | int timeout = 10; | |
88a8516a | 299 | int err; |
1da177e4 | 300 | |
88a8516a ON |
301 | err = snd_usb_autoresume(cval->mixer->chip); |
302 | if (err < 0) | |
303 | return -EIO; | |
1da177e4 | 304 | while (timeout-- > 0) { |
3d8d4dcf | 305 | if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, |
1da177e4 | 306 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
3d8d4dcf | 307 | validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), |
a04395ea | 308 | buf, val_len, 100) >= val_len) { |
1da177e4 | 309 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); |
88a8516a | 310 | snd_usb_autosuspend(cval->mixer->chip); |
1da177e4 LT |
311 | return 0; |
312 | } | |
313 | } | |
88a8516a | 314 | snd_usb_autosuspend(cval->mixer->chip); |
84957a8a | 315 | snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
b415ec70 | 316 | request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type); |
1da177e4 LT |
317 | return -EINVAL; |
318 | } | |
319 | ||
23caaf19 DM |
320 | static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) |
321 | { | |
3d8d4dcf | 322 | struct snd_usb_audio *chip = cval->mixer->chip; |
e8bdb6bb | 323 | unsigned char buf[2 + 3*sizeof(__u16)]; /* enough space for one range */ |
23caaf19 | 324 | unsigned char *val; |
e8bdb6bb | 325 | int ret, size; |
23caaf19 DM |
326 | __u8 bRequest; |
327 | ||
e8bdb6bb DM |
328 | if (request == UAC_GET_CUR) { |
329 | bRequest = UAC2_CS_CUR; | |
330 | size = sizeof(__u16); | |
331 | } else { | |
332 | bRequest = UAC2_CS_RANGE; | |
333 | size = sizeof(buf); | |
334 | } | |
335 | ||
336 | memset(buf, 0, sizeof(buf)); | |
23caaf19 | 337 | |
88a8516a ON |
338 | ret = snd_usb_autoresume(chip) ? -EIO : 0; |
339 | if (ret) | |
340 | goto error; | |
341 | ||
3d8d4dcf | 342 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest, |
23caaf19 | 343 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
3d8d4dcf | 344 | validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), |
e8bdb6bb | 345 | buf, size, 1000); |
88a8516a | 346 | snd_usb_autosuspend(chip); |
23caaf19 DM |
347 | |
348 | if (ret < 0) { | |
88a8516a | 349 | error: |
09414207 | 350 | snd_printk(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
3d8d4dcf | 351 | request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type); |
23caaf19 DM |
352 | return ret; |
353 | } | |
354 | ||
e8bdb6bb DM |
355 | /* FIXME: how should we handle multiple triplets here? */ |
356 | ||
23caaf19 DM |
357 | switch (request) { |
358 | case UAC_GET_CUR: | |
359 | val = buf; | |
360 | break; | |
361 | case UAC_GET_MIN: | |
362 | val = buf + sizeof(__u16); | |
363 | break; | |
364 | case UAC_GET_MAX: | |
365 | val = buf + sizeof(__u16) * 2; | |
366 | break; | |
367 | case UAC_GET_RES: | |
368 | val = buf + sizeof(__u16) * 3; | |
369 | break; | |
370 | default: | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
374 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16))); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) | |
380 | { | |
381 | return (cval->mixer->protocol == UAC_VERSION_1) ? | |
382 | get_ctl_value_v1(cval, request, validx, value_ret) : | |
383 | get_ctl_value_v2(cval, request, validx, value_ret); | |
384 | } | |
385 | ||
86e07d34 | 386 | static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value) |
1da177e4 | 387 | { |
de48c7bc | 388 | return get_ctl_value(cval, UAC_GET_CUR, validx, value); |
1da177e4 LT |
389 | } |
390 | ||
391 | /* channel = 0: master, 1 = first channel */ | |
641b4879 TI |
392 | static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, |
393 | int channel, int *value) | |
1da177e4 | 394 | { |
de48c7bc | 395 | return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value); |
1da177e4 LT |
396 | } |
397 | ||
641b4879 TI |
398 | static int get_cur_mix_value(struct usb_mixer_elem_info *cval, |
399 | int channel, int index, int *value) | |
400 | { | |
401 | int err; | |
402 | ||
403 | if (cval->cached & (1 << channel)) { | |
404 | *value = cval->cache_val[index]; | |
405 | return 0; | |
406 | } | |
407 | err = get_cur_mix_raw(cval, channel, value); | |
408 | if (err < 0) { | |
409 | if (!cval->mixer->ignore_ctl_error) | |
23caaf19 | 410 | snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", |
641b4879 TI |
411 | cval->control, channel, err); |
412 | return err; | |
413 | } | |
414 | cval->cached |= 1 << channel; | |
415 | cval->cache_val[index] = *value; | |
416 | return 0; | |
417 | } | |
418 | ||
419 | ||
1da177e4 LT |
420 | /* |
421 | * set a mixer value | |
422 | */ | |
423 | ||
7b1eda22 DM |
424 | int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, |
425 | int request, int validx, int value_set) | |
1da177e4 | 426 | { |
3d8d4dcf | 427 | struct snd_usb_audio *chip = cval->mixer->chip; |
1da177e4 | 428 | unsigned char buf[2]; |
88a8516a | 429 | int val_len, err, timeout = 10; |
23caaf19 DM |
430 | |
431 | if (cval->mixer->protocol == UAC_VERSION_1) { | |
432 | val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; | |
433 | } else { /* UAC_VERSION_2 */ | |
434 | /* audio class v2 controls are always 2 bytes in size */ | |
435 | val_len = sizeof(__u16); | |
436 | ||
437 | /* FIXME */ | |
438 | if (request != UAC_SET_CUR) { | |
439 | snd_printdd(KERN_WARNING "RANGE setting not yet supported\n"); | |
440 | return -EINVAL; | |
441 | } | |
442 | ||
443 | request = UAC2_CS_CUR; | |
444 | } | |
1da177e4 LT |
445 | |
446 | value_set = convert_bytes_value(cval, value_set); | |
447 | buf[0] = value_set & 0xff; | |
448 | buf[1] = (value_set >> 8) & 0xff; | |
88a8516a ON |
449 | err = snd_usb_autoresume(chip); |
450 | if (err < 0) | |
451 | return -EIO; | |
cf3f9130 | 452 | while (timeout-- > 0) |
3d8d4dcf DM |
453 | if (snd_usb_ctl_msg(chip->dev, |
454 | usb_sndctrlpipe(chip->dev, 0), request, | |
1da177e4 | 455 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, |
3d8d4dcf | 456 | validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), |
88a8516a ON |
457 | buf, val_len, 100) >= 0) { |
458 | snd_usb_autosuspend(chip); | |
1da177e4 | 459 | return 0; |
88a8516a ON |
460 | } |
461 | snd_usb_autosuspend(chip); | |
84957a8a | 462 | snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", |
3d8d4dcf | 463 | request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type, buf[0], buf[1]); |
1da177e4 LT |
464 | return -EINVAL; |
465 | } | |
466 | ||
86e07d34 | 467 | static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value) |
1da177e4 | 468 | { |
7b1eda22 | 469 | return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); |
1da177e4 LT |
470 | } |
471 | ||
641b4879 TI |
472 | static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, |
473 | int index, int value) | |
1da177e4 | 474 | { |
641b4879 | 475 | int err; |
a6a33259 DM |
476 | unsigned int read_only = (channel == 0) ? |
477 | cval->master_readonly : | |
478 | cval->ch_readonly & (1 << (channel - 1)); | |
479 | ||
480 | if (read_only) { | |
481 | snd_printdd(KERN_INFO "%s(): channel %d of control %d is read_only\n", | |
482 | __func__, channel, cval->control); | |
483 | return 0; | |
484 | } | |
485 | ||
7b1eda22 | 486 | err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel, |
641b4879 TI |
487 | value); |
488 | if (err < 0) | |
489 | return err; | |
490 | cval->cached |= 1 << channel; | |
491 | cval->cache_val[index] = value; | |
492 | return 0; | |
1da177e4 LT |
493 | } |
494 | ||
7bc5ba7e TI |
495 | /* |
496 | * TLV callback for mixer volume controls | |
497 | */ | |
498 | static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, | |
499 | unsigned int size, unsigned int __user *_tlv) | |
500 | { | |
501 | struct usb_mixer_elem_info *cval = kcontrol->private_data; | |
b8e1c73f | 502 | DECLARE_TLV_DB_MINMAX(scale, 0, 0); |
7bc5ba7e TI |
503 | |
504 | if (size < sizeof(scale)) | |
505 | return -ENOMEM; | |
c3a3e040 JK |
506 | scale[2] = cval->dBmin; |
507 | scale[3] = cval->dBmax; | |
7bc5ba7e TI |
508 | if (copy_to_user(_tlv, scale, sizeof(scale))) |
509 | return -EFAULT; | |
510 | return 0; | |
511 | } | |
1da177e4 LT |
512 | |
513 | /* | |
514 | * parser routines begin here... | |
515 | */ | |
516 | ||
86e07d34 | 517 | static int parse_audio_unit(struct mixer_build *state, int unitid); |
1da177e4 LT |
518 | |
519 | ||
520 | /* | |
521 | * check if the input/output channel routing is enabled on the given bitmap. | |
522 | * used for mixer unit parser | |
523 | */ | |
524 | static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs) | |
525 | { | |
526 | int idx = ich * num_outs + och; | |
527 | return bmap[idx >> 3] & (0x80 >> (idx & 7)); | |
528 | } | |
529 | ||
530 | ||
531 | /* | |
532 | * add an alsa control element | |
533 | * search and increment the index until an empty slot is found. | |
534 | * | |
535 | * if failed, give up and free the control instance. | |
536 | */ | |
537 | ||
ef9d5970 DM |
538 | int snd_usb_mixer_add_control(struct usb_mixer_interface *mixer, |
539 | struct snd_kcontrol *kctl) | |
1da177e4 | 540 | { |
86e07d34 | 541 | struct usb_mixer_elem_info *cval = kctl->private_data; |
1da177e4 | 542 | int err; |
84957a8a | 543 | |
ef9d5970 | 544 | while (snd_ctl_find_id(mixer->chip->card, &kctl->id)) |
1da177e4 | 545 | kctl->id.index++; |
ef9d5970 | 546 | if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) { |
1da177e4 | 547 | snd_printd(KERN_ERR "cannot add control (err = %d)\n", err); |
6639b6c2 | 548 | return err; |
1da177e4 | 549 | } |
6639b6c2 | 550 | cval->elem_id = &kctl->id; |
ef9d5970 DM |
551 | cval->next_id_elem = mixer->id_elems[cval->id]; |
552 | mixer->id_elems[cval->id] = cval; | |
6639b6c2 | 553 | return 0; |
1da177e4 LT |
554 | } |
555 | ||
556 | ||
557 | /* | |
558 | * get a terminal name string | |
559 | */ | |
560 | ||
561 | static struct iterm_name_combo { | |
562 | int type; | |
563 | char *name; | |
564 | } iterm_names[] = { | |
565 | { 0x0300, "Output" }, | |
566 | { 0x0301, "Speaker" }, | |
567 | { 0x0302, "Headphone" }, | |
568 | { 0x0303, "HMD Audio" }, | |
569 | { 0x0304, "Desktop Speaker" }, | |
570 | { 0x0305, "Room Speaker" }, | |
571 | { 0x0306, "Com Speaker" }, | |
572 | { 0x0307, "LFE" }, | |
573 | { 0x0600, "External In" }, | |
574 | { 0x0601, "Analog In" }, | |
575 | { 0x0602, "Digital In" }, | |
576 | { 0x0603, "Line" }, | |
577 | { 0x0604, "Legacy In" }, | |
578 | { 0x0605, "IEC958 In" }, | |
579 | { 0x0606, "1394 DA Stream" }, | |
580 | { 0x0607, "1394 DV Stream" }, | |
581 | { 0x0700, "Embedded" }, | |
582 | { 0x0701, "Noise Source" }, | |
583 | { 0x0702, "Equalization Noise" }, | |
584 | { 0x0703, "CD" }, | |
585 | { 0x0704, "DAT" }, | |
586 | { 0x0705, "DCC" }, | |
587 | { 0x0706, "MiniDisk" }, | |
588 | { 0x0707, "Analog Tape" }, | |
589 | { 0x0708, "Phonograph" }, | |
590 | { 0x0709, "VCR Audio" }, | |
591 | { 0x070a, "Video Disk Audio" }, | |
592 | { 0x070b, "DVD Audio" }, | |
593 | { 0x070c, "TV Tuner Audio" }, | |
594 | { 0x070d, "Satellite Rec Audio" }, | |
595 | { 0x070e, "Cable Tuner Audio" }, | |
596 | { 0x070f, "DSS Audio" }, | |
597 | { 0x0710, "Radio Receiver" }, | |
598 | { 0x0711, "Radio Transmitter" }, | |
599 | { 0x0712, "Multi-Track Recorder" }, | |
600 | { 0x0713, "Synthesizer" }, | |
601 | { 0 }, | |
602 | }; | |
603 | ||
86e07d34 | 604 | static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm, |
1da177e4 LT |
605 | unsigned char *name, int maxlen, int term_only) |
606 | { | |
607 | struct iterm_name_combo *names; | |
608 | ||
609 | if (iterm->name) | |
610 | return snd_usb_copy_string_desc(state, iterm->name, name, maxlen); | |
611 | ||
612 | /* virtual type - not a real terminal */ | |
613 | if (iterm->type >> 16) { | |
614 | if (term_only) | |
615 | return 0; | |
616 | switch (iterm->type >> 16) { | |
de48c7bc | 617 | case UAC_SELECTOR_UNIT: |
1da177e4 | 618 | strcpy(name, "Selector"); return 8; |
69da9bcb | 619 | case UAC1_PROCESSING_UNIT: |
1da177e4 | 620 | strcpy(name, "Process Unit"); return 12; |
69da9bcb | 621 | case UAC1_EXTENSION_UNIT: |
1da177e4 | 622 | strcpy(name, "Ext Unit"); return 8; |
de48c7bc | 623 | case UAC_MIXER_UNIT: |
1da177e4 LT |
624 | strcpy(name, "Mixer"); return 5; |
625 | default: | |
626 | return sprintf(name, "Unit %d", iterm->id); | |
627 | } | |
628 | } | |
629 | ||
630 | switch (iterm->type & 0xff00) { | |
631 | case 0x0100: | |
632 | strcpy(name, "PCM"); return 3; | |
633 | case 0x0200: | |
634 | strcpy(name, "Mic"); return 3; | |
635 | case 0x0400: | |
636 | strcpy(name, "Headset"); return 7; | |
637 | case 0x0500: | |
638 | strcpy(name, "Phone"); return 5; | |
639 | } | |
640 | ||
641 | for (names = iterm_names; names->type; names++) | |
642 | if (names->type == iterm->type) { | |
643 | strcpy(name, names->name); | |
644 | return strlen(names->name); | |
645 | } | |
646 | return 0; | |
647 | } | |
648 | ||
649 | ||
650 | /* | |
651 | * parse the source unit recursively until it reaches to a terminal | |
652 | * or a branched unit. | |
653 | */ | |
86e07d34 | 654 | static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term) |
1da177e4 | 655 | { |
09414207 | 656 | int err; |
23caaf19 | 657 | void *p1; |
1da177e4 LT |
658 | |
659 | memset(term, 0, sizeof(*term)); | |
660 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { | |
23caaf19 | 661 | unsigned char *hdr = p1; |
1da177e4 | 662 | term->id = id; |
23caaf19 | 663 | switch (hdr[2]) { |
de48c7bc | 664 | case UAC_INPUT_TERMINAL: |
23caaf19 DM |
665 | if (state->mixer->protocol == UAC_VERSION_1) { |
666 | struct uac_input_terminal_descriptor *d = p1; | |
667 | term->type = le16_to_cpu(d->wTerminalType); | |
668 | term->channels = d->bNrChannels; | |
669 | term->chconfig = le16_to_cpu(d->wChannelConfig); | |
670 | term->name = d->iTerminal; | |
671 | } else { /* UAC_VERSION_2 */ | |
672 | struct uac2_input_terminal_descriptor *d = p1; | |
673 | term->type = le16_to_cpu(d->wTerminalType); | |
674 | term->channels = d->bNrChannels; | |
675 | term->chconfig = le32_to_cpu(d->bmChannelConfig); | |
676 | term->name = d->iTerminal; | |
09414207 DM |
677 | |
678 | /* call recursively to get the clock selectors */ | |
679 | err = check_input_term(state, d->bCSourceID, term); | |
680 | if (err < 0) | |
681 | return err; | |
23caaf19 | 682 | } |
1da177e4 | 683 | return 0; |
23caaf19 DM |
684 | case UAC_FEATURE_UNIT: { |
685 | /* the header is the same for v1 and v2 */ | |
686 | struct uac_feature_unit_descriptor *d = p1; | |
5e688883 | 687 | id = d->bSourceID; |
1da177e4 | 688 | break; /* continue to parse */ |
23caaf19 DM |
689 | } |
690 | case UAC_MIXER_UNIT: { | |
691 | struct uac_mixer_unit_descriptor *d = p1; | |
692 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ | |
693 | term->channels = uac_mixer_unit_bNrChannels(d); | |
694 | term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol); | |
695 | term->name = uac_mixer_unit_iMixer(d); | |
1da177e4 | 696 | return 0; |
23caaf19 | 697 | } |
09414207 DM |
698 | case UAC_SELECTOR_UNIT: |
699 | case UAC2_CLOCK_SELECTOR: { | |
23caaf19 | 700 | struct uac_selector_unit_descriptor *d = p1; |
1da177e4 | 701 | /* call recursively to retrieve the channel info */ |
23caaf19 | 702 | if (check_input_term(state, d->baSourceID[0], term) < 0) |
1da177e4 | 703 | return -ENODEV; |
23caaf19 | 704 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
1da177e4 | 705 | term->id = id; |
23caaf19 | 706 | term->name = uac_selector_unit_iSelector(d); |
1da177e4 | 707 | return 0; |
23caaf19 | 708 | } |
69da9bcb DM |
709 | case UAC1_PROCESSING_UNIT: |
710 | case UAC1_EXTENSION_UNIT: { | |
23caaf19 DM |
711 | struct uac_processing_unit_descriptor *d = p1; |
712 | if (d->bNrInPins) { | |
713 | id = d->baSourceID[0]; | |
1da177e4 LT |
714 | break; /* continue to parse */ |
715 | } | |
23caaf19 DM |
716 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
717 | term->channels = uac_processing_unit_bNrChannels(d); | |
718 | term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol); | |
719 | term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol); | |
1da177e4 | 720 | return 0; |
23caaf19 | 721 | } |
09414207 DM |
722 | case UAC2_CLOCK_SOURCE: { |
723 | struct uac_clock_source_descriptor *d = p1; | |
724 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ | |
725 | term->id = id; | |
726 | term->name = d->iClockSource; | |
727 | return 0; | |
728 | } | |
1da177e4 LT |
729 | default: |
730 | return -ENODEV; | |
731 | } | |
732 | } | |
733 | return -ENODEV; | |
734 | } | |
735 | ||
736 | ||
737 | /* | |
738 | * Feature Unit | |
739 | */ | |
740 | ||
741 | /* feature unit control information */ | |
742 | struct usb_feature_control_info { | |
743 | const char *name; | |
744 | unsigned int type; /* control type (mute, volume, etc.) */ | |
745 | }; | |
746 | ||
747 | static struct usb_feature_control_info audio_feature_info[] = { | |
2e0281d1 DM |
748 | { "Mute", USB_MIXER_INV_BOOLEAN }, |
749 | { "Volume", USB_MIXER_S16 }, | |
1da177e4 LT |
750 | { "Tone Control - Bass", USB_MIXER_S8 }, |
751 | { "Tone Control - Mid", USB_MIXER_S8 }, | |
752 | { "Tone Control - Treble", USB_MIXER_S8 }, | |
753 | { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */ | |
2e0281d1 DM |
754 | { "Auto Gain Control", USB_MIXER_BOOLEAN }, |
755 | { "Delay Control", USB_MIXER_U16 }, | |
756 | { "Bass Boost", USB_MIXER_BOOLEAN }, | |
757 | { "Loudness", USB_MIXER_BOOLEAN }, | |
758 | /* UAC2 specific */ | |
759 | { "Input Gain Control", USB_MIXER_U16 }, | |
760 | { "Input Gain Pad Control", USB_MIXER_BOOLEAN }, | |
761 | { "Phase Inverter Control", USB_MIXER_BOOLEAN }, | |
1da177e4 LT |
762 | }; |
763 | ||
764 | ||
765 | /* private_free callback */ | |
86e07d34 | 766 | static void usb_mixer_elem_free(struct snd_kcontrol *kctl) |
1da177e4 | 767 | { |
4d572776 JJ |
768 | kfree(kctl->private_data); |
769 | kctl->private_data = NULL; | |
1da177e4 LT |
770 | } |
771 | ||
772 | ||
773 | /* | |
774 | * interface to ALSA control for feature/mixer units | |
775 | */ | |
776 | ||
777 | /* | |
778 | * retrieve the minimum and maximum values for the specified control | |
779 | */ | |
86e07d34 | 780 | static int get_min_max(struct usb_mixer_elem_info *cval, int default_min) |
1da177e4 LT |
781 | { |
782 | /* for failsafe */ | |
783 | cval->min = default_min; | |
784 | cval->max = cval->min + 1; | |
785 | cval->res = 1; | |
c3a3e040 | 786 | cval->dBmin = cval->dBmax = 0; |
1da177e4 LT |
787 | |
788 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
789 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
790 | cval->initialized = 1; | |
791 | } else { | |
792 | int minchn = 0; | |
793 | if (cval->cmask) { | |
794 | int i; | |
795 | for (i = 0; i < MAX_CHANNELS; i++) | |
796 | if (cval->cmask & (1 << i)) { | |
797 | minchn = i + 1; | |
798 | break; | |
799 | } | |
800 | } | |
de48c7bc DM |
801 | if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || |
802 | get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { | |
84957a8a | 803 | snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", |
1a4e34e6 | 804 | cval->id, snd_usb_ctrl_intf(cval->mixer->chip), cval->control, cval->id); |
1da177e4 LT |
805 | return -EINVAL; |
806 | } | |
de48c7bc | 807 | if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) { |
1da177e4 LT |
808 | cval->res = 1; |
809 | } else { | |
810 | int last_valid_res = cval->res; | |
811 | ||
812 | while (cval->res > 1) { | |
7b1eda22 DM |
813 | if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, |
814 | (cval->control << 8) | minchn, cval->res / 2) < 0) | |
1da177e4 LT |
815 | break; |
816 | cval->res /= 2; | |
817 | } | |
de48c7bc | 818 | if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) |
1da177e4 LT |
819 | cval->res = last_valid_res; |
820 | } | |
821 | if (cval->res == 0) | |
822 | cval->res = 1; | |
14790f1c TI |
823 | |
824 | /* Additional checks for the proper resolution | |
825 | * | |
826 | * Some devices report smaller resolutions than actually | |
827 | * reacting. They don't return errors but simply clip | |
828 | * to the lower aligned value. | |
829 | */ | |
830 | if (cval->min + cval->res < cval->max) { | |
831 | int last_valid_res = cval->res; | |
832 | int saved, test, check; | |
641b4879 | 833 | get_cur_mix_raw(cval, minchn, &saved); |
14790f1c TI |
834 | for (;;) { |
835 | test = saved; | |
836 | if (test < cval->max) | |
837 | test += cval->res; | |
838 | else | |
839 | test -= cval->res; | |
840 | if (test < cval->min || test > cval->max || | |
641b4879 TI |
841 | set_cur_mix_value(cval, minchn, 0, test) || |
842 | get_cur_mix_raw(cval, minchn, &check)) { | |
14790f1c TI |
843 | cval->res = last_valid_res; |
844 | break; | |
845 | } | |
846 | if (test == check) | |
847 | break; | |
848 | cval->res *= 2; | |
849 | } | |
641b4879 | 850 | set_cur_mix_value(cval, minchn, 0, saved); |
14790f1c TI |
851 | } |
852 | ||
1da177e4 LT |
853 | cval->initialized = 1; |
854 | } | |
c3a3e040 JK |
855 | |
856 | /* USB descriptions contain the dB scale in 1/256 dB unit | |
857 | * while ALSA TLV contains in 1/100 dB unit | |
858 | */ | |
859 | cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; | |
860 | cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; | |
861 | if (cval->dBmin > cval->dBmax) { | |
862 | /* something is wrong; assume it's either from/to 0dB */ | |
863 | if (cval->dBmin < 0) | |
864 | cval->dBmax = 0; | |
865 | else if (cval->dBmin > 0) | |
866 | cval->dBmin = 0; | |
867 | if (cval->dBmin > cval->dBmax) { | |
868 | /* totally crap, return an error */ | |
869 | return -EINVAL; | |
870 | } | |
871 | } | |
872 | ||
1da177e4 LT |
873 | return 0; |
874 | } | |
875 | ||
876 | ||
877 | /* get a feature/mixer unit info */ | |
86e07d34 | 878 | static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 | 879 | { |
86e07d34 | 880 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
881 | |
882 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
883 | cval->val_type == USB_MIXER_INV_BOOLEAN) | |
884 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
885 | else | |
886 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
887 | uinfo->count = cval->channels; | |
888 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
889 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
890 | uinfo->value.integer.min = 0; | |
891 | uinfo->value.integer.max = 1; | |
892 | } else { | |
893 | if (! cval->initialized) | |
894 | get_min_max(cval, 0); | |
895 | uinfo->value.integer.min = 0; | |
14790f1c TI |
896 | uinfo->value.integer.max = |
897 | (cval->max - cval->min + cval->res - 1) / cval->res; | |
1da177e4 LT |
898 | } |
899 | return 0; | |
900 | } | |
901 | ||
902 | /* get the current value from feature/mixer unit */ | |
86e07d34 | 903 | static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 904 | { |
86e07d34 | 905 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
906 | int c, cnt, val, err; |
907 | ||
641b4879 | 908 | ucontrol->value.integer.value[0] = cval->min; |
1da177e4 LT |
909 | if (cval->cmask) { |
910 | cnt = 0; | |
911 | for (c = 0; c < MAX_CHANNELS; c++) { | |
641b4879 TI |
912 | if (!(cval->cmask & (1 << c))) |
913 | continue; | |
914 | err = get_cur_mix_value(cval, c + 1, cnt, &val); | |
915 | if (err < 0) | |
916 | return cval->mixer->ignore_ctl_error ? 0 : err; | |
917 | val = get_relative_value(cval, val); | |
918 | ucontrol->value.integer.value[cnt] = val; | |
919 | cnt++; | |
1da177e4 | 920 | } |
641b4879 | 921 | return 0; |
1da177e4 LT |
922 | } else { |
923 | /* master channel */ | |
641b4879 TI |
924 | err = get_cur_mix_value(cval, 0, 0, &val); |
925 | if (err < 0) | |
926 | return cval->mixer->ignore_ctl_error ? 0 : err; | |
1da177e4 LT |
927 | val = get_relative_value(cval, val); |
928 | ucontrol->value.integer.value[0] = val; | |
929 | } | |
930 | return 0; | |
931 | } | |
932 | ||
933 | /* put the current value to feature/mixer unit */ | |
86e07d34 | 934 | static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 935 | { |
86e07d34 | 936 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
937 | int c, cnt, val, oval, err; |
938 | int changed = 0; | |
939 | ||
940 | if (cval->cmask) { | |
941 | cnt = 0; | |
942 | for (c = 0; c < MAX_CHANNELS; c++) { | |
641b4879 TI |
943 | if (!(cval->cmask & (1 << c))) |
944 | continue; | |
945 | err = get_cur_mix_value(cval, c + 1, cnt, &oval); | |
946 | if (err < 0) | |
947 | return cval->mixer->ignore_ctl_error ? 0 : err; | |
948 | val = ucontrol->value.integer.value[cnt]; | |
949 | val = get_abs_value(cval, val); | |
950 | if (oval != val) { | |
951 | set_cur_mix_value(cval, c + 1, cnt, val); | |
952 | changed = 1; | |
1da177e4 | 953 | } |
641b4879 | 954 | cnt++; |
1da177e4 LT |
955 | } |
956 | } else { | |
957 | /* master channel */ | |
641b4879 | 958 | err = get_cur_mix_value(cval, 0, 0, &oval); |
1da177e4 | 959 | if (err < 0) |
641b4879 | 960 | return cval->mixer->ignore_ctl_error ? 0 : err; |
1da177e4 LT |
961 | val = ucontrol->value.integer.value[0]; |
962 | val = get_abs_value(cval, val); | |
963 | if (val != oval) { | |
641b4879 | 964 | set_cur_mix_value(cval, 0, 0, val); |
1da177e4 LT |
965 | changed = 1; |
966 | } | |
967 | } | |
968 | return changed; | |
969 | } | |
970 | ||
86e07d34 | 971 | static struct snd_kcontrol_new usb_feature_unit_ctl = { |
1da177e4 LT |
972 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
973 | .name = "", /* will be filled later manually */ | |
974 | .info = mixer_ctl_feature_info, | |
975 | .get = mixer_ctl_feature_get, | |
976 | .put = mixer_ctl_feature_put, | |
977 | }; | |
978 | ||
23caaf19 DM |
979 | /* the read-only variant */ |
980 | static struct snd_kcontrol_new usb_feature_unit_ctl_ro = { | |
981 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
982 | .name = "", /* will be filled later manually */ | |
983 | .info = mixer_ctl_feature_info, | |
984 | .get = mixer_ctl_feature_get, | |
985 | .put = NULL, | |
986 | }; | |
987 | ||
1da177e4 LT |
988 | |
989 | /* | |
990 | * build a feature control | |
991 | */ | |
992 | ||
08d1e635 TI |
993 | static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) |
994 | { | |
995 | return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); | |
996 | } | |
997 | ||
99fc8645 | 998 | static void build_feature_ctl(struct mixer_build *state, void *raw_desc, |
1da177e4 | 999 | unsigned int ctl_mask, int control, |
23caaf19 | 1000 | struct usb_audio_term *iterm, int unitid, |
a6a33259 | 1001 | int readonly_mask) |
1da177e4 | 1002 | { |
99fc8645 | 1003 | struct uac_feature_unit_descriptor *desc = raw_desc; |
1da177e4 LT |
1004 | unsigned int len = 0; |
1005 | int mapped_name = 0; | |
99fc8645 | 1006 | int nameid = uac_feature_unit_iFeature(desc); |
86e07d34 TI |
1007 | struct snd_kcontrol *kctl; |
1008 | struct usb_mixer_elem_info *cval; | |
c3a3e040 | 1009 | const struct usbmix_name_map *map; |
80acefff | 1010 | unsigned int range; |
1da177e4 LT |
1011 | |
1012 | control++; /* change from zero-based to 1-based value */ | |
1013 | ||
65f25da4 | 1014 | if (control == UAC_FU_GRAPHIC_EQUALIZER) { |
1da177e4 LT |
1015 | /* FIXME: not supported yet */ |
1016 | return; | |
1017 | } | |
1018 | ||
c3a3e040 JK |
1019 | map = find_map(state, unitid, control); |
1020 | if (check_ignored_ctl(map)) | |
1da177e4 LT |
1021 | return; |
1022 | ||
561b220a | 1023 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
1da177e4 LT |
1024 | if (! cval) { |
1025 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1026 | return; | |
1027 | } | |
84957a8a | 1028 | cval->mixer = state->mixer; |
1da177e4 LT |
1029 | cval->id = unitid; |
1030 | cval->control = control; | |
1031 | cval->cmask = ctl_mask; | |
1032 | cval->val_type = audio_feature_info[control-1].type; | |
a6a33259 | 1033 | if (ctl_mask == 0) { |
1da177e4 | 1034 | cval->channels = 1; /* master channel */ |
a6a33259 DM |
1035 | cval->master_readonly = readonly_mask; |
1036 | } else { | |
1da177e4 LT |
1037 | int i, c = 0; |
1038 | for (i = 0; i < 16; i++) | |
1039 | if (ctl_mask & (1 << i)) | |
1040 | c++; | |
1041 | cval->channels = c; | |
a6a33259 | 1042 | cval->ch_readonly = readonly_mask; |
1da177e4 LT |
1043 | } |
1044 | ||
1045 | /* get min/max values */ | |
1046 | get_min_max(cval, 0); | |
1047 | ||
a6a33259 DM |
1048 | /* if all channels in the mask are marked read-only, make the control |
1049 | * read-only. set_cur_mix_value() will check the mask again and won't | |
1050 | * issue write commands to read-only channels. */ | |
1051 | if (cval->channels == readonly_mask) | |
23caaf19 DM |
1052 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
1053 | else | |
1054 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
1055 | ||
1da177e4 LT |
1056 | if (! kctl) { |
1057 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1058 | kfree(cval); | |
1059 | return; | |
1060 | } | |
1061 | kctl->private_free = usb_mixer_elem_free; | |
1062 | ||
c3a3e040 | 1063 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
1da177e4 LT |
1064 | mapped_name = len != 0; |
1065 | if (! len && nameid) | |
c3a3e040 JK |
1066 | len = snd_usb_copy_string_desc(state, nameid, |
1067 | kctl->id.name, sizeof(kctl->id.name)); | |
1da177e4 LT |
1068 | |
1069 | switch (control) { | |
65f25da4 DM |
1070 | case UAC_FU_MUTE: |
1071 | case UAC_FU_VOLUME: | |
1da177e4 LT |
1072 | /* determine the control name. the rule is: |
1073 | * - if a name id is given in descriptor, use it. | |
1074 | * - if the connected input can be determined, then use the name | |
1075 | * of terminal type. | |
1076 | * - if the connected output can be determined, use it. | |
1077 | * - otherwise, anonymous name. | |
1078 | */ | |
1079 | if (! len) { | |
1080 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1); | |
1081 | if (! len) | |
1082 | len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1); | |
1083 | if (! len) | |
1084 | len = snprintf(kctl->id.name, sizeof(kctl->id.name), | |
1085 | "Feature %d", unitid); | |
1086 | } | |
1087 | /* determine the stream direction: | |
1088 | * if the connected output is USB stream, then it's likely a | |
1089 | * capture stream. otherwise it should be playback (hopefully :) | |
1090 | */ | |
1091 | if (! mapped_name && ! (state->oterm.type >> 16)) { | |
1092 | if ((state->oterm.type & 0xff00) == 0x0100) { | |
08d1e635 | 1093 | len = append_ctl_name(kctl, " Capture"); |
1da177e4 | 1094 | } else { |
08d1e635 | 1095 | len = append_ctl_name(kctl, " Playback"); |
1da177e4 LT |
1096 | } |
1097 | } | |
65f25da4 | 1098 | append_ctl_name(kctl, control == UAC_FU_MUTE ? |
08d1e635 | 1099 | " Switch" : " Volume"); |
65f25da4 | 1100 | if (control == UAC_FU_VOLUME) { |
c3a3e040 | 1101 | check_mapped_dB(map, cval); |
59bb7f0e TI |
1102 | if (cval->dBmin < cval->dBmax) { |
1103 | kctl->tlv.c = mixer_vol_tlv; | |
1104 | kctl->vd[0].access |= | |
1105 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | | |
1106 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; | |
1107 | } | |
7bc5ba7e | 1108 | } |
1da177e4 LT |
1109 | break; |
1110 | ||
1111 | default: | |
1112 | if (! len) | |
1113 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, | |
1114 | sizeof(kctl->id.name)); | |
1115 | break; | |
1116 | } | |
1117 | ||
2cf313ee | 1118 | /* volume control quirks */ |
27d10f56 CL |
1119 | switch (state->chip->usb_id) { |
1120 | case USB_ID(0x0471, 0x0101): | |
1121 | case USB_ID(0x0471, 0x0104): | |
1122 | case USB_ID(0x0471, 0x0105): | |
1123 | case USB_ID(0x0672, 0x1041): | |
2cf313ee AF |
1124 | /* quirk for UDA1321/N101. |
1125 | * note that detection between firmware 2.1.1.7 (N101) | |
1126 | * and later 2.1.1.21 is not very clear from datasheets. | |
1127 | * I hope that the min value is -15360 for newer firmware --jk | |
1128 | */ | |
27d10f56 CL |
1129 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && |
1130 | cval->min == -15616) { | |
2cf313ee AF |
1131 | snd_printk(KERN_INFO |
1132 | "set volume quirk for UDA1321/N101 chip\n"); | |
27d10f56 CL |
1133 | cval->max = -256; |
1134 | } | |
2cf313ee AF |
1135 | break; |
1136 | ||
1137 | case USB_ID(0x046d, 0x09a4): | |
1138 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { | |
1139 | snd_printk(KERN_INFO | |
1140 | "set volume quirk for QuickCam E3500\n"); | |
1141 | cval->min = 6080; | |
1142 | cval->max = 8768; | |
1143 | cval->res = 192; | |
1144 | } | |
1145 | break; | |
1146 | ||
bc3a8a01 | 1147 | case USB_ID(0x046d, 0x0808): |
a5c7d797 AF |
1148 | case USB_ID(0x046d, 0x0809): |
1149 | case USB_ID(0x046d, 0x0991): | |
1150 | /* Most audio usb devices lie about volume resolution. | |
1151 | * Most Logitech webcams have res = 384. | |
1152 | * Proboly there is some logitech magic behind this number --fishor | |
1153 | */ | |
1154 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { | |
1155 | snd_printk(KERN_INFO | |
1156 | "set resolution quirk: cval->res = 384\n"); | |
1157 | cval->res = 384; | |
1158 | } | |
1159 | break; | |
1160 | ||
1da177e4 LT |
1161 | } |
1162 | ||
80acefff AF |
1163 | range = (cval->max - cval->min) / cval->res; |
1164 | /* Are there devices with volume range more than 255? I use a bit more | |
1165 | * to be sure. 384 is a resolution magic number found on Logitech | |
1166 | * devices. It will definitively catch all buggy Logitech devices. | |
1167 | */ | |
1168 | if (range > 384) { | |
1169 | snd_printk(KERN_WARNING "usb_audio: Warning! Unlikely big " | |
1170 | "volume range (=%u), cval->res is probably wrong.", | |
1171 | range); | |
1172 | snd_printk(KERN_WARNING "usb_audio: [%d] FU [%s] ch = %d, " | |
1173 | "val = %d/%d/%d", cval->id, | |
1174 | kctl->id.name, cval->channels, | |
1175 | cval->min, cval->max, cval->res); | |
1176 | } | |
1177 | ||
1da177e4 LT |
1178 | snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", |
1179 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res); | |
ef9d5970 | 1180 | snd_usb_mixer_add_control(state->mixer, kctl); |
1da177e4 LT |
1181 | } |
1182 | ||
1183 | ||
1184 | ||
1185 | /* | |
1186 | * parse a feature unit | |
1187 | * | |
25985edc | 1188 | * most of controls are defined here. |
1da177e4 | 1189 | */ |
28e1b773 | 1190 | static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr) |
1da177e4 LT |
1191 | { |
1192 | int channels, i, j; | |
86e07d34 | 1193 | struct usb_audio_term iterm; |
1da177e4 LT |
1194 | unsigned int master_bits, first_ch_bits; |
1195 | int err, csize; | |
23caaf19 DM |
1196 | struct uac_feature_unit_descriptor *hdr = _ftr; |
1197 | __u8 *bmaControls; | |
1198 | ||
1199 | if (state->mixer->protocol == UAC_VERSION_1) { | |
1200 | csize = hdr->bControlSize; | |
1201 | channels = (hdr->bLength - 7) / csize - 1; | |
1202 | bmaControls = hdr->bmaControls; | |
1203 | } else { | |
1204 | struct uac2_feature_unit_descriptor *ftr = _ftr; | |
1205 | csize = 4; | |
e8d0fee7 | 1206 | channels = (hdr->bLength - 6) / 4 - 1; |
23caaf19 DM |
1207 | bmaControls = ftr->bmaControls; |
1208 | } | |
1da177e4 | 1209 | |
23caaf19 | 1210 | if (hdr->bLength < 7 || !csize || hdr->bLength < 7 + csize) { |
de48c7bc | 1211 | snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid); |
1da177e4 LT |
1212 | return -EINVAL; |
1213 | } | |
1214 | ||
1215 | /* parse the source unit */ | |
23caaf19 | 1216 | if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0) |
1da177e4 LT |
1217 | return err; |
1218 | ||
1219 | /* determine the input source type and name */ | |
23caaf19 | 1220 | if (check_input_term(state, hdr->bSourceID, &iterm) < 0) |
1da177e4 LT |
1221 | return -EINVAL; |
1222 | ||
23caaf19 | 1223 | master_bits = snd_usb_combine_bytes(bmaControls, csize); |
0c3cee57 JK |
1224 | /* master configuration quirks */ |
1225 | switch (state->chip->usb_id) { | |
1226 | case USB_ID(0x08bb, 0x2702): | |
1227 | snd_printk(KERN_INFO | |
1228 | "usbmixer: master volume quirk for PCM2702 chip\n"); | |
1229 | /* disable non-functional volume control */ | |
65f25da4 | 1230 | master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); |
0c3cee57 JK |
1231 | break; |
1232 | } | |
1da177e4 | 1233 | if (channels > 0) |
23caaf19 | 1234 | first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize); |
1da177e4 LT |
1235 | else |
1236 | first_ch_bits = 0; | |
23caaf19 DM |
1237 | |
1238 | if (state->mixer->protocol == UAC_VERSION_1) { | |
1239 | /* check all control types */ | |
1240 | for (i = 0; i < 10; i++) { | |
1241 | unsigned int ch_bits = 0; | |
1242 | for (j = 0; j < channels; j++) { | |
1243 | unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize); | |
1244 | if (mask & (1 << i)) | |
1245 | ch_bits |= (1 << j); | |
1246 | } | |
1247 | /* audio class v1 controls are never read-only */ | |
1248 | if (ch_bits & 1) /* the first channel must be set (for ease of programming) */ | |
1249 | build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0); | |
1250 | if (master_bits & (1 << i)) | |
1251 | build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0); | |
1252 | } | |
1253 | } else { /* UAC_VERSION_2 */ | |
1254 | for (i = 0; i < 30/2; i++) { | |
23caaf19 DM |
1255 | unsigned int ch_bits = 0; |
1256 | unsigned int ch_read_only = 0; | |
1257 | ||
1258 | for (j = 0; j < channels; j++) { | |
1259 | unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize); | |
dcbe7bcf | 1260 | if (uac2_control_is_readable(mask, i)) { |
23caaf19 | 1261 | ch_bits |= (1 << j); |
dcbe7bcf | 1262 | if (!uac2_control_is_writeable(mask, i)) |
23caaf19 DM |
1263 | ch_read_only |= (1 << j); |
1264 | } | |
1265 | } | |
1266 | ||
a6a33259 DM |
1267 | /* NOTE: build_feature_ctl() will mark the control read-only if all channels |
1268 | * are marked read-only in the descriptors. Otherwise, the control will be | |
1269 | * reported as writeable, but the driver will not actually issue a write | |
1270 | * command for read-only channels */ | |
23caaf19 | 1271 | if (ch_bits & 1) /* the first channel must be set (for ease of programming) */ |
a6a33259 | 1272 | build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, ch_read_only); |
dcbe7bcf | 1273 | if (uac2_control_is_readable(master_bits, i)) |
23caaf19 | 1274 | build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, |
dcbe7bcf | 1275 | !uac2_control_is_writeable(master_bits, i)); |
1da177e4 | 1276 | } |
1da177e4 LT |
1277 | } |
1278 | ||
1279 | return 0; | |
1280 | } | |
1281 | ||
1282 | ||
1283 | /* | |
1284 | * Mixer Unit | |
1285 | */ | |
1286 | ||
1287 | /* | |
1288 | * build a mixer unit control | |
1289 | * | |
1290 | * the callbacks are identical with feature unit. | |
1291 | * input channel number (zero based) is given in control field instead. | |
1292 | */ | |
1293 | ||
99fc8645 DM |
1294 | static void build_mixer_unit_ctl(struct mixer_build *state, |
1295 | struct uac_mixer_unit_descriptor *desc, | |
1da177e4 | 1296 | int in_pin, int in_ch, int unitid, |
86e07d34 | 1297 | struct usb_audio_term *iterm) |
1da177e4 | 1298 | { |
86e07d34 | 1299 | struct usb_mixer_elem_info *cval; |
99fc8645 | 1300 | unsigned int num_outs = uac_mixer_unit_bNrChannels(desc); |
1da177e4 | 1301 | unsigned int i, len; |
86e07d34 | 1302 | struct snd_kcontrol *kctl; |
c3a3e040 | 1303 | const struct usbmix_name_map *map; |
1da177e4 | 1304 | |
c3a3e040 JK |
1305 | map = find_map(state, unitid, 0); |
1306 | if (check_ignored_ctl(map)) | |
1da177e4 LT |
1307 | return; |
1308 | ||
561b220a | 1309 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
1da177e4 LT |
1310 | if (! cval) |
1311 | return; | |
1312 | ||
84957a8a | 1313 | cval->mixer = state->mixer; |
1da177e4 LT |
1314 | cval->id = unitid; |
1315 | cval->control = in_ch + 1; /* based on 1 */ | |
1316 | cval->val_type = USB_MIXER_S16; | |
1317 | for (i = 0; i < num_outs; i++) { | |
23caaf19 | 1318 | if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) { |
1da177e4 LT |
1319 | cval->cmask |= (1 << i); |
1320 | cval->channels++; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | /* get min/max values */ | |
1325 | get_min_max(cval, 0); | |
1326 | ||
1327 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
1328 | if (! kctl) { | |
1329 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1330 | kfree(cval); | |
1331 | return; | |
1332 | } | |
1333 | kctl->private_free = usb_mixer_elem_free; | |
1334 | ||
c3a3e040 | 1335 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
1da177e4 LT |
1336 | if (! len) |
1337 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0); | |
1338 | if (! len) | |
1339 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); | |
08d1e635 | 1340 | append_ctl_name(kctl, " Volume"); |
1da177e4 LT |
1341 | |
1342 | snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n", | |
1343 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); | |
ef9d5970 | 1344 | snd_usb_mixer_add_control(state->mixer, kctl); |
1da177e4 LT |
1345 | } |
1346 | ||
1347 | ||
1348 | /* | |
1349 | * parse a mixer unit | |
1350 | */ | |
99fc8645 | 1351 | static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc) |
1da177e4 | 1352 | { |
99fc8645 | 1353 | struct uac_mixer_unit_descriptor *desc = raw_desc; |
86e07d34 | 1354 | struct usb_audio_term iterm; |
1da177e4 LT |
1355 | int input_pins, num_ins, num_outs; |
1356 | int pin, ich, err; | |
1357 | ||
99fc8645 | 1358 | if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) { |
1da177e4 LT |
1359 | snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid); |
1360 | return -EINVAL; | |
1361 | } | |
1362 | /* no bmControls field (e.g. Maya44) -> ignore */ | |
99fc8645 | 1363 | if (desc->bLength <= 10 + input_pins) { |
1da177e4 LT |
1364 | snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid); |
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | num_ins = 0; | |
1369 | ich = 0; | |
1370 | for (pin = 0; pin < input_pins; pin++) { | |
99fc8645 | 1371 | err = parse_audio_unit(state, desc->baSourceID[pin]); |
1da177e4 LT |
1372 | if (err < 0) |
1373 | return err; | |
99fc8645 | 1374 | err = check_input_term(state, desc->baSourceID[pin], &iterm); |
1da177e4 LT |
1375 | if (err < 0) |
1376 | return err; | |
1377 | num_ins += iterm.channels; | |
1378 | for (; ich < num_ins; ++ich) { | |
1379 | int och, ich_has_controls = 0; | |
1380 | ||
1381 | for (och = 0; och < num_outs; ++och) { | |
23caaf19 | 1382 | if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), |
1da177e4 LT |
1383 | ich, och, num_outs)) { |
1384 | ich_has_controls = 1; | |
1385 | break; | |
1386 | } | |
1387 | } | |
1388 | if (ich_has_controls) | |
1389 | build_mixer_unit_ctl(state, desc, pin, ich, | |
1390 | unitid, &iterm); | |
1391 | } | |
1392 | } | |
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | ||
1397 | /* | |
1398 | * Processing Unit / Extension Unit | |
1399 | */ | |
1400 | ||
1401 | /* get callback for processing/extension unit */ | |
86e07d34 | 1402 | static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1403 | { |
86e07d34 | 1404 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1405 | int err, val; |
1406 | ||
1407 | err = get_cur_ctl_value(cval, cval->control << 8, &val); | |
84957a8a | 1408 | if (err < 0 && cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
1409 | ucontrol->value.integer.value[0] = cval->min; |
1410 | return 0; | |
1411 | } | |
1412 | if (err < 0) | |
1413 | return err; | |
1414 | val = get_relative_value(cval, val); | |
1415 | ucontrol->value.integer.value[0] = val; | |
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | /* put callback for processing/extension unit */ | |
86e07d34 | 1420 | static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1421 | { |
86e07d34 | 1422 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1423 | int val, oval, err; |
1424 | ||
1425 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); | |
1426 | if (err < 0) { | |
84957a8a | 1427 | if (cval->mixer->ignore_ctl_error) |
1da177e4 LT |
1428 | return 0; |
1429 | return err; | |
1430 | } | |
1431 | val = ucontrol->value.integer.value[0]; | |
1432 | val = get_abs_value(cval, val); | |
1433 | if (val != oval) { | |
1434 | set_cur_ctl_value(cval, cval->control << 8, val); | |
1435 | return 1; | |
1436 | } | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | /* alsa control interface for processing/extension unit */ | |
86e07d34 | 1441 | static struct snd_kcontrol_new mixer_procunit_ctl = { |
1da177e4 LT |
1442 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1443 | .name = "", /* will be filled later */ | |
1444 | .info = mixer_ctl_feature_info, | |
1445 | .get = mixer_ctl_procunit_get, | |
1446 | .put = mixer_ctl_procunit_put, | |
1447 | }; | |
1448 | ||
1449 | ||
1450 | /* | |
1451 | * predefined data for processing units | |
1452 | */ | |
1453 | struct procunit_value_info { | |
1454 | int control; | |
1455 | char *suffix; | |
1456 | int val_type; | |
1457 | int min_value; | |
1458 | }; | |
1459 | ||
1460 | struct procunit_info { | |
1461 | int type; | |
1462 | char *name; | |
1463 | struct procunit_value_info *values; | |
1464 | }; | |
1465 | ||
1466 | static struct procunit_value_info updown_proc_info[] = { | |
65f25da4 DM |
1467 | { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1468 | { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, | |
1da177e4 LT |
1469 | { 0 } |
1470 | }; | |
1471 | static struct procunit_value_info prologic_proc_info[] = { | |
65f25da4 DM |
1472 | { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1473 | { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, | |
1da177e4 LT |
1474 | { 0 } |
1475 | }; | |
1476 | static struct procunit_value_info threed_enh_proc_info[] = { | |
65f25da4 DM |
1477 | { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1478 | { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, | |
1da177e4 LT |
1479 | { 0 } |
1480 | }; | |
1481 | static struct procunit_value_info reverb_proc_info[] = { | |
65f25da4 DM |
1482 | { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1483 | { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, | |
1484 | { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, | |
1485 | { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, | |
1da177e4 LT |
1486 | { 0 } |
1487 | }; | |
1488 | static struct procunit_value_info chorus_proc_info[] = { | |
65f25da4 DM |
1489 | { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1490 | { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, | |
1491 | { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, | |
1492 | { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, | |
1da177e4 LT |
1493 | { 0 } |
1494 | }; | |
1495 | static struct procunit_value_info dcr_proc_info[] = { | |
65f25da4 DM |
1496 | { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
1497 | { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, | |
1498 | { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, | |
1499 | { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, | |
1500 | { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, | |
1501 | { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, | |
1da177e4 LT |
1502 | { 0 } |
1503 | }; | |
1504 | ||
1505 | static struct procunit_info procunits[] = { | |
65f25da4 DM |
1506 | { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, |
1507 | { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, | |
1508 | { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, | |
1509 | { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, | |
1510 | { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, | |
1511 | { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, | |
1da177e4 LT |
1512 | { 0 }, |
1513 | }; | |
7d2b451e SK |
1514 | /* |
1515 | * predefined data for extension units | |
1516 | */ | |
1517 | static struct procunit_value_info clock_rate_xu_info[] = { | |
e213e9cf DM |
1518 | { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, |
1519 | { 0 } | |
7d2b451e SK |
1520 | }; |
1521 | static struct procunit_value_info clock_source_xu_info[] = { | |
1522 | { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, | |
1523 | { 0 } | |
1524 | }; | |
1525 | static struct procunit_value_info spdif_format_xu_info[] = { | |
1526 | { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, | |
1527 | { 0 } | |
1528 | }; | |
1529 | static struct procunit_value_info soft_limit_xu_info[] = { | |
1530 | { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, | |
1531 | { 0 } | |
1532 | }; | |
1533 | static struct procunit_info extunits[] = { | |
1534 | { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, | |
1535 | { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, | |
1536 | { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, | |
1537 | { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, | |
1538 | { 0 } | |
1539 | }; | |
1da177e4 LT |
1540 | /* |
1541 | * build a processing/extension unit | |
1542 | */ | |
99fc8645 | 1543 | static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name) |
1da177e4 | 1544 | { |
99fc8645 DM |
1545 | struct uac_processing_unit_descriptor *desc = raw_desc; |
1546 | int num_ins = desc->bNrInPins; | |
86e07d34 TI |
1547 | struct usb_mixer_elem_info *cval; |
1548 | struct snd_kcontrol *kctl; | |
1da177e4 LT |
1549 | int i, err, nameid, type, len; |
1550 | struct procunit_info *info; | |
1551 | struct procunit_value_info *valinfo; | |
c3a3e040 | 1552 | const struct usbmix_name_map *map; |
1da177e4 LT |
1553 | static struct procunit_value_info default_value_info[] = { |
1554 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, | |
1555 | { 0 } | |
1556 | }; | |
1557 | static struct procunit_info default_info = { | |
1558 | 0, NULL, default_value_info | |
1559 | }; | |
1560 | ||
23caaf19 DM |
1561 | if (desc->bLength < 13 || desc->bLength < 13 + num_ins || |
1562 | desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { | |
1da177e4 LT |
1563 | snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid); |
1564 | return -EINVAL; | |
1565 | } | |
1566 | ||
1567 | for (i = 0; i < num_ins; i++) { | |
99fc8645 | 1568 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) |
1da177e4 LT |
1569 | return err; |
1570 | } | |
1571 | ||
99fc8645 | 1572 | type = le16_to_cpu(desc->wProcessType); |
1da177e4 LT |
1573 | for (info = list; info && info->type; info++) |
1574 | if (info->type == type) | |
1575 | break; | |
1576 | if (! info || ! info->type) | |
1577 | info = &default_info; | |
1578 | ||
1579 | for (valinfo = info->values; valinfo->control; valinfo++) { | |
23caaf19 | 1580 | __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); |
99fc8645 DM |
1581 | |
1582 | if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1)))) | |
1da177e4 | 1583 | continue; |
c3a3e040 JK |
1584 | map = find_map(state, unitid, valinfo->control); |
1585 | if (check_ignored_ctl(map)) | |
1da177e4 | 1586 | continue; |
561b220a | 1587 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
1da177e4 LT |
1588 | if (! cval) { |
1589 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1590 | return -ENOMEM; | |
1591 | } | |
84957a8a | 1592 | cval->mixer = state->mixer; |
1da177e4 LT |
1593 | cval->id = unitid; |
1594 | cval->control = valinfo->control; | |
1595 | cval->val_type = valinfo->val_type; | |
1596 | cval->channels = 1; | |
1597 | ||
1598 | /* get min/max values */ | |
65f25da4 | 1599 | if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) { |
23caaf19 | 1600 | __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol); |
1da177e4 LT |
1601 | /* FIXME: hard-coded */ |
1602 | cval->min = 1; | |
99fc8645 | 1603 | cval->max = control_spec[0]; |
1da177e4 LT |
1604 | cval->res = 1; |
1605 | cval->initialized = 1; | |
7d2b451e SK |
1606 | } else { |
1607 | if (type == USB_XU_CLOCK_RATE) { | |
1cdfa9f3 | 1608 | /* E-Mu USB 0404/0202/TrackerPre/0204 |
7d2b451e SK |
1609 | * samplerate control quirk |
1610 | */ | |
1611 | cval->min = 0; | |
1612 | cval->max = 5; | |
1613 | cval->res = 1; | |
1614 | cval->initialized = 1; | |
1615 | } else | |
1616 | get_min_max(cval, valinfo->min_value); | |
1617 | } | |
1da177e4 LT |
1618 | |
1619 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); | |
1620 | if (! kctl) { | |
1621 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1622 | kfree(cval); | |
1623 | return -ENOMEM; | |
1624 | } | |
1625 | kctl->private_free = usb_mixer_elem_free; | |
1626 | ||
c3a3e040 JK |
1627 | if (check_mapped_name(map, kctl->id.name, |
1628 | sizeof(kctl->id.name))) | |
1629 | /* nothing */ ; | |
1da177e4 LT |
1630 | else if (info->name) |
1631 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); | |
1632 | else { | |
23caaf19 | 1633 | nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); |
1da177e4 LT |
1634 | len = 0; |
1635 | if (nameid) | |
1636 | len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); | |
1637 | if (! len) | |
1638 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); | |
1639 | } | |
08d1e635 TI |
1640 | append_ctl_name(kctl, " "); |
1641 | append_ctl_name(kctl, valinfo->suffix); | |
1da177e4 LT |
1642 | |
1643 | snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n", | |
1644 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); | |
ef9d5970 | 1645 | if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0) |
1da177e4 LT |
1646 | return err; |
1647 | } | |
1648 | return 0; | |
1649 | } | |
1650 | ||
1651 | ||
99fc8645 | 1652 | static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc) |
1da177e4 | 1653 | { |
99fc8645 | 1654 | return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit"); |
1da177e4 LT |
1655 | } |
1656 | ||
99fc8645 | 1657 | static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc) |
1da177e4 | 1658 | { |
99fc8645 DM |
1659 | /* Note that we parse extension units with processing unit descriptors. |
1660 | * That's ok as the layout is the same */ | |
1661 | return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit"); | |
1da177e4 LT |
1662 | } |
1663 | ||
1664 | ||
1665 | /* | |
1666 | * Selector Unit | |
1667 | */ | |
1668 | ||
1669 | /* info callback for selector unit | |
1670 | * use an enumerator type for routing | |
1671 | */ | |
86e07d34 | 1672 | static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 | 1673 | { |
86e07d34 | 1674 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
2a1803a7 | 1675 | const char **itemlist = (const char **)kcontrol->private_value; |
1da177e4 | 1676 | |
5e246b85 TI |
1677 | if (snd_BUG_ON(!itemlist)) |
1678 | return -EINVAL; | |
2a1803a7 | 1679 | return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); |
1da177e4 LT |
1680 | } |
1681 | ||
1682 | /* get callback for selector unit */ | |
86e07d34 | 1683 | static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1684 | { |
86e07d34 | 1685 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1686 | int val, err; |
1687 | ||
09414207 | 1688 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
1da177e4 | 1689 | if (err < 0) { |
84957a8a | 1690 | if (cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
1691 | ucontrol->value.enumerated.item[0] = 0; |
1692 | return 0; | |
1693 | } | |
1694 | return err; | |
1695 | } | |
1696 | val = get_relative_value(cval, val); | |
1697 | ucontrol->value.enumerated.item[0] = val; | |
1698 | return 0; | |
1699 | } | |
1700 | ||
1701 | /* put callback for selector unit */ | |
86e07d34 | 1702 | static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1703 | { |
86e07d34 | 1704 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
1da177e4 LT |
1705 | int val, oval, err; |
1706 | ||
09414207 | 1707 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
1da177e4 | 1708 | if (err < 0) { |
84957a8a | 1709 | if (cval->mixer->ignore_ctl_error) |
1da177e4 LT |
1710 | return 0; |
1711 | return err; | |
1712 | } | |
1713 | val = ucontrol->value.enumerated.item[0]; | |
1714 | val = get_abs_value(cval, val); | |
1715 | if (val != oval) { | |
09414207 | 1716 | set_cur_ctl_value(cval, cval->control << 8, val); |
1da177e4 LT |
1717 | return 1; |
1718 | } | |
1719 | return 0; | |
1720 | } | |
1721 | ||
1722 | /* alsa control interface for selector unit */ | |
86e07d34 | 1723 | static struct snd_kcontrol_new mixer_selectunit_ctl = { |
1da177e4 LT |
1724 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1725 | .name = "", /* will be filled later */ | |
1726 | .info = mixer_ctl_selector_info, | |
1727 | .get = mixer_ctl_selector_get, | |
1728 | .put = mixer_ctl_selector_put, | |
1729 | }; | |
1730 | ||
1731 | ||
1732 | /* private free callback. | |
1733 | * free both private_data and private_value | |
1734 | */ | |
86e07d34 | 1735 | static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) |
1da177e4 LT |
1736 | { |
1737 | int i, num_ins = 0; | |
1738 | ||
1739 | if (kctl->private_data) { | |
86e07d34 | 1740 | struct usb_mixer_elem_info *cval = kctl->private_data; |
1da177e4 LT |
1741 | num_ins = cval->max; |
1742 | kfree(cval); | |
1743 | kctl->private_data = NULL; | |
1744 | } | |
1745 | if (kctl->private_value) { | |
1746 | char **itemlist = (char **)kctl->private_value; | |
1747 | for (i = 0; i < num_ins; i++) | |
1748 | kfree(itemlist[i]); | |
1749 | kfree(itemlist); | |
1750 | kctl->private_value = 0; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * parse a selector unit | |
1756 | */ | |
99fc8645 | 1757 | static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc) |
1da177e4 | 1758 | { |
99fc8645 | 1759 | struct uac_selector_unit_descriptor *desc = raw_desc; |
1da177e4 LT |
1760 | unsigned int i, nameid, len; |
1761 | int err; | |
86e07d34 TI |
1762 | struct usb_mixer_elem_info *cval; |
1763 | struct snd_kcontrol *kctl; | |
c3a3e040 | 1764 | const struct usbmix_name_map *map; |
1da177e4 LT |
1765 | char **namelist; |
1766 | ||
99fc8645 | 1767 | if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) { |
1da177e4 LT |
1768 | snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid); |
1769 | return -EINVAL; | |
1770 | } | |
1771 | ||
99fc8645 DM |
1772 | for (i = 0; i < desc->bNrInPins; i++) { |
1773 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) | |
1da177e4 LT |
1774 | return err; |
1775 | } | |
1776 | ||
99fc8645 | 1777 | if (desc->bNrInPins == 1) /* only one ? nonsense! */ |
1da177e4 LT |
1778 | return 0; |
1779 | ||
c3a3e040 JK |
1780 | map = find_map(state, unitid, 0); |
1781 | if (check_ignored_ctl(map)) | |
1da177e4 LT |
1782 | return 0; |
1783 | ||
561b220a | 1784 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
1da177e4 LT |
1785 | if (! cval) { |
1786 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1787 | return -ENOMEM; | |
1788 | } | |
84957a8a | 1789 | cval->mixer = state->mixer; |
1da177e4 LT |
1790 | cval->id = unitid; |
1791 | cval->val_type = USB_MIXER_U8; | |
1792 | cval->channels = 1; | |
1793 | cval->min = 1; | |
99fc8645 | 1794 | cval->max = desc->bNrInPins; |
1da177e4 LT |
1795 | cval->res = 1; |
1796 | cval->initialized = 1; | |
1797 | ||
09414207 DM |
1798 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) |
1799 | cval->control = UAC2_CX_CLOCK_SELECTOR; | |
1800 | else | |
1801 | cval->control = 0; | |
1802 | ||
99fc8645 | 1803 | namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL); |
1da177e4 LT |
1804 | if (! namelist) { |
1805 | snd_printk(KERN_ERR "cannot malloc\n"); | |
1806 | kfree(cval); | |
1807 | return -ENOMEM; | |
1808 | } | |
1809 | #define MAX_ITEM_NAME_LEN 64 | |
99fc8645 | 1810 | for (i = 0; i < desc->bNrInPins; i++) { |
86e07d34 | 1811 | struct usb_audio_term iterm; |
1da177e4 LT |
1812 | len = 0; |
1813 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); | |
1814 | if (! namelist[i]) { | |
1815 | snd_printk(KERN_ERR "cannot malloc\n"); | |
7fbe3ca5 | 1816 | while (i--) |
1da177e4 LT |
1817 | kfree(namelist[i]); |
1818 | kfree(namelist); | |
1819 | kfree(cval); | |
1820 | return -ENOMEM; | |
1821 | } | |
8e062ec7 CL |
1822 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
1823 | MAX_ITEM_NAME_LEN); | |
99fc8645 | 1824 | if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) |
1da177e4 LT |
1825 | len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0); |
1826 | if (! len) | |
1827 | sprintf(namelist[i], "Input %d", i); | |
1828 | } | |
1829 | ||
1830 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); | |
1831 | if (! kctl) { | |
1832 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
878b4789 | 1833 | kfree(namelist); |
1da177e4 LT |
1834 | kfree(cval); |
1835 | return -ENOMEM; | |
1836 | } | |
1837 | kctl->private_value = (unsigned long)namelist; | |
1838 | kctl->private_free = usb_mixer_selector_elem_free; | |
1839 | ||
99fc8645 | 1840 | nameid = uac_selector_unit_iSelector(desc); |
c3a3e040 | 1841 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
1da177e4 LT |
1842 | if (len) |
1843 | ; | |
1844 | else if (nameid) | |
1845 | snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); | |
1846 | else { | |
1847 | len = get_term_name(state, &state->oterm, | |
1848 | kctl->id.name, sizeof(kctl->id.name), 0); | |
1849 | if (! len) | |
1850 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); | |
1851 | ||
09414207 DM |
1852 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) |
1853 | append_ctl_name(kctl, " Clock Source"); | |
1854 | else if ((state->oterm.type & 0xff00) == 0x0100) | |
08d1e635 | 1855 | append_ctl_name(kctl, " Capture Source"); |
1da177e4 | 1856 | else |
08d1e635 | 1857 | append_ctl_name(kctl, " Playback Source"); |
1da177e4 LT |
1858 | } |
1859 | ||
1860 | snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n", | |
99fc8645 | 1861 | cval->id, kctl->id.name, desc->bNrInPins); |
ef9d5970 | 1862 | if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0) |
1da177e4 LT |
1863 | return err; |
1864 | ||
1865 | return 0; | |
1866 | } | |
1867 | ||
1868 | ||
1869 | /* | |
1870 | * parse an audio unit recursively | |
1871 | */ | |
1872 | ||
86e07d34 | 1873 | static int parse_audio_unit(struct mixer_build *state, int unitid) |
1da177e4 LT |
1874 | { |
1875 | unsigned char *p1; | |
1876 | ||
1877 | if (test_and_set_bit(unitid, state->unitbitmap)) | |
1878 | return 0; /* the unit already visited */ | |
1879 | ||
1880 | p1 = find_audio_control_unit(state, unitid); | |
1881 | if (!p1) { | |
1882 | snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid); | |
1883 | return -EINVAL; | |
1884 | } | |
1885 | ||
1886 | switch (p1[2]) { | |
de48c7bc | 1887 | case UAC_INPUT_TERMINAL: |
09414207 | 1888 | case UAC2_CLOCK_SOURCE: |
1da177e4 | 1889 | return 0; /* NOP */ |
de48c7bc | 1890 | case UAC_MIXER_UNIT: |
1da177e4 | 1891 | return parse_audio_mixer_unit(state, unitid, p1); |
de48c7bc | 1892 | case UAC_SELECTOR_UNIT: |
09414207 | 1893 | case UAC2_CLOCK_SELECTOR: |
1da177e4 | 1894 | return parse_audio_selector_unit(state, unitid, p1); |
de48c7bc | 1895 | case UAC_FEATURE_UNIT: |
1da177e4 | 1896 | return parse_audio_feature_unit(state, unitid, p1); |
69da9bcb | 1897 | case UAC1_PROCESSING_UNIT: |
23caaf19 DM |
1898 | /* UAC2_EFFECT_UNIT has the same value */ |
1899 | if (state->mixer->protocol == UAC_VERSION_1) | |
1900 | return parse_audio_processing_unit(state, unitid, p1); | |
1901 | else | |
1902 | return 0; /* FIXME - effect units not implemented yet */ | |
69da9bcb | 1903 | case UAC1_EXTENSION_UNIT: |
23caaf19 DM |
1904 | /* UAC2_PROCESSING_UNIT_V2 has the same value */ |
1905 | if (state->mixer->protocol == UAC_VERSION_1) | |
1906 | return parse_audio_extension_unit(state, unitid, p1); | |
1907 | else /* UAC_VERSION_2 */ | |
1908 | return parse_audio_processing_unit(state, unitid, p1); | |
1da177e4 LT |
1909 | default: |
1910 | snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]); | |
1911 | return -EINVAL; | |
1912 | } | |
1913 | } | |
1914 | ||
84957a8a CL |
1915 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
1916 | { | |
6639b6c2 CL |
1917 | kfree(mixer->id_elems); |
1918 | if (mixer->urb) { | |
1919 | kfree(mixer->urb->transfer_buffer); | |
1920 | usb_free_urb(mixer->urb); | |
1921 | } | |
68df9de1 | 1922 | usb_free_urb(mixer->rc_urb); |
b259b10c | 1923 | kfree(mixer->rc_setup_packet); |
84957a8a CL |
1924 | kfree(mixer); |
1925 | } | |
1926 | ||
86e07d34 | 1927 | static int snd_usb_mixer_dev_free(struct snd_device *device) |
84957a8a CL |
1928 | { |
1929 | struct usb_mixer_interface *mixer = device->device_data; | |
1930 | snd_usb_mixer_free(mixer); | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1da177e4 LT |
1934 | /* |
1935 | * create mixer controls | |
1936 | * | |
de48c7bc | 1937 | * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers |
1da177e4 | 1938 | */ |
84957a8a | 1939 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
1da177e4 | 1940 | { |
86e07d34 | 1941 | struct mixer_build state; |
1da177e4 LT |
1942 | int err; |
1943 | const struct usbmix_ctl_map *map; | |
84957a8a | 1944 | struct usb_host_interface *hostif; |
23caaf19 | 1945 | void *p; |
1da177e4 | 1946 | |
3d8d4dcf | 1947 | hostif = mixer->chip->ctrl_intf; |
1da177e4 | 1948 | memset(&state, 0, sizeof(state)); |
84957a8a CL |
1949 | state.chip = mixer->chip; |
1950 | state.mixer = mixer; | |
1da177e4 LT |
1951 | state.buffer = hostif->extra; |
1952 | state.buflen = hostif->extralen; | |
1da177e4 LT |
1953 | |
1954 | /* check the mapping table */ | |
27d10f56 CL |
1955 | for (map = usbmix_ctl_maps; map->id; map++) { |
1956 | if (map->id == state.chip->usb_id) { | |
1da177e4 | 1957 | state.map = map->map; |
8e062ec7 | 1958 | state.selector_map = map->selector_map; |
84957a8a | 1959 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
1da177e4 LT |
1960 | break; |
1961 | } | |
1962 | } | |
1da177e4 | 1963 | |
23caaf19 DM |
1964 | p = NULL; |
1965 | while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) { | |
1966 | if (mixer->protocol == UAC_VERSION_1) { | |
69da9bcb | 1967 | struct uac1_output_terminal_descriptor *desc = p; |
23caaf19 DM |
1968 | |
1969 | if (desc->bLength < sizeof(*desc)) | |
1970 | continue; /* invalid descriptor? */ | |
1971 | set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */ | |
1972 | state.oterm.id = desc->bTerminalID; | |
1973 | state.oterm.type = le16_to_cpu(desc->wTerminalType); | |
1974 | state.oterm.name = desc->iTerminal; | |
1975 | err = parse_audio_unit(&state, desc->bSourceID); | |
1976 | if (err < 0) | |
1977 | return err; | |
1978 | } else { /* UAC_VERSION_2 */ | |
1979 | struct uac2_output_terminal_descriptor *desc = p; | |
1980 | ||
1981 | if (desc->bLength < sizeof(*desc)) | |
1982 | continue; /* invalid descriptor? */ | |
1983 | set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */ | |
1984 | state.oterm.id = desc->bTerminalID; | |
1985 | state.oterm.type = le16_to_cpu(desc->wTerminalType); | |
1986 | state.oterm.name = desc->iTerminal; | |
1987 | err = parse_audio_unit(&state, desc->bSourceID); | |
1988 | if (err < 0) | |
1989 | return err; | |
09414207 DM |
1990 | |
1991 | /* for UAC2, use the same approach to also add the clock selectors */ | |
1992 | err = parse_audio_unit(&state, desc->bCSourceID); | |
1993 | if (err < 0) | |
1994 | return err; | |
23caaf19 | 1995 | } |
1da177e4 | 1996 | } |
23caaf19 | 1997 | |
1da177e4 LT |
1998 | return 0; |
1999 | } | |
84957a8a | 2000 | |
7b1eda22 | 2001 | void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) |
6639b6c2 | 2002 | { |
86e07d34 | 2003 | struct usb_mixer_elem_info *info; |
6639b6c2 CL |
2004 | |
2005 | for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) | |
2006 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
2007 | info->elem_id); | |
2008 | } | |
2009 | ||
ebfdeea3 JK |
2010 | static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, |
2011 | int unitid, | |
2012 | struct usb_mixer_elem_info *cval) | |
2013 | { | |
2014 | static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", | |
2015 | "S8", "U8", "S16", "U16"}; | |
2016 | snd_iprintf(buffer, " Unit: %i\n", unitid); | |
2017 | if (cval->elem_id) | |
2018 | snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n", | |
2019 | cval->elem_id->name, cval->elem_id->index); | |
2020 | snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " | |
2021 | "channels=%i, type=\"%s\"\n", cval->id, | |
2022 | cval->control, cval->cmask, cval->channels, | |
2023 | val_types[cval->val_type]); | |
2024 | snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", | |
2025 | cval->min, cval->max, cval->dBmin, cval->dBmax); | |
2026 | } | |
2027 | ||
2028 | static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, | |
2029 | struct snd_info_buffer *buffer) | |
2030 | { | |
2031 | struct snd_usb_audio *chip = entry->private_data; | |
2032 | struct usb_mixer_interface *mixer; | |
2033 | struct usb_mixer_elem_info *cval; | |
2034 | int unitid; | |
2035 | ||
2036 | list_for_each_entry(mixer, &chip->mixer_list, list) { | |
2037 | snd_iprintf(buffer, | |
7affdc17 | 2038 | "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", |
3d8d4dcf | 2039 | chip->usb_id, snd_usb_ctrl_intf(chip), |
7affdc17 | 2040 | mixer->ignore_ctl_error); |
ebfdeea3 JK |
2041 | snd_iprintf(buffer, "Card: %s\n", chip->card->longname); |
2042 | for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { | |
2043 | for (cval = mixer->id_elems[unitid]; cval; | |
2044 | cval = cval->next_id_elem) | |
2045 | snd_usb_mixer_dump_cval(buffer, unitid, cval); | |
2046 | } | |
2047 | } | |
2048 | } | |
2049 | ||
e213e9cf DM |
2050 | static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, |
2051 | int attribute, int value, int index) | |
2052 | { | |
2053 | struct usb_mixer_elem_info *info; | |
2054 | __u8 unitid = (index >> 8) & 0xff; | |
2055 | __u8 control = (value >> 8) & 0xff; | |
2056 | __u8 channel = value & 0xff; | |
2057 | ||
2058 | if (channel >= MAX_CHANNELS) { | |
2059 | snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n", | |
2060 | __func__, channel); | |
2061 | return; | |
2062 | } | |
2063 | ||
2064 | for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) { | |
2065 | if (info->control != control) | |
2066 | continue; | |
2067 | ||
2068 | switch (attribute) { | |
2069 | case UAC2_CS_CUR: | |
2070 | /* invalidate cache, so the value is read from the device */ | |
2071 | if (channel) | |
2072 | info->cached &= ~(1 << channel); | |
2073 | else /* master channel */ | |
2074 | info->cached = 0; | |
2075 | ||
2076 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
2077 | info->elem_id); | |
2078 | break; | |
2079 | ||
2080 | case UAC2_CS_RANGE: | |
2081 | /* TODO */ | |
2082 | break; | |
2083 | ||
2084 | case UAC2_CS_MEM: | |
2085 | /* TODO */ | |
2086 | break; | |
2087 | ||
2088 | default: | |
2089 | snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n", | |
2090 | attribute); | |
2091 | break; | |
2092 | } /* switch */ | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | static void snd_usb_mixer_interrupt(struct urb *urb) | |
6639b6c2 CL |
2097 | { |
2098 | struct usb_mixer_interface *mixer = urb->context; | |
e213e9cf | 2099 | int len = urb->actual_length; |
edf7de31 | 2100 | int ustatus = urb->status; |
e213e9cf | 2101 | |
edf7de31 | 2102 | if (ustatus != 0) |
e213e9cf | 2103 | goto requeue; |
6639b6c2 | 2104 | |
e213e9cf DM |
2105 | if (mixer->protocol == UAC_VERSION_1) { |
2106 | struct uac1_status_word *status; | |
6639b6c2 | 2107 | |
e213e9cf DM |
2108 | for (status = urb->transfer_buffer; |
2109 | len >= sizeof(*status); | |
2110 | len -= sizeof(*status), status++) { | |
6639b6c2 | 2111 | snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n", |
e213e9cf DM |
2112 | status->bStatusType, |
2113 | status->bOriginator); | |
2114 | ||
6639b6c2 | 2115 | /* ignore any notifications not from the control interface */ |
e213e9cf DM |
2116 | if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != |
2117 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) | |
6639b6c2 | 2118 | continue; |
e213e9cf DM |
2119 | |
2120 | if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) | |
2121 | snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); | |
b259b10c | 2122 | else |
e213e9cf DM |
2123 | snd_usb_mixer_notify_id(mixer, status->bOriginator); |
2124 | } | |
2125 | } else { /* UAC_VERSION_2 */ | |
2126 | struct uac2_interrupt_data_msg *msg; | |
2127 | ||
2128 | for (msg = urb->transfer_buffer; | |
2129 | len >= sizeof(*msg); | |
2130 | len -= sizeof(*msg), msg++) { | |
2131 | /* drop vendor specific and endpoint requests */ | |
2132 | if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || | |
2133 | (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) | |
2134 | continue; | |
2135 | ||
2136 | snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, | |
2137 | le16_to_cpu(msg->wValue), | |
2138 | le16_to_cpu(msg->wIndex)); | |
6639b6c2 CL |
2139 | } |
2140 | } | |
e213e9cf DM |
2141 | |
2142 | requeue: | |
edf7de31 | 2143 | if (ustatus != -ENOENT && ustatus != -ECONNRESET && ustatus != -ESHUTDOWN) { |
6639b6c2 CL |
2144 | urb->dev = mixer->chip->dev; |
2145 | usb_submit_urb(urb, GFP_ATOMIC); | |
2146 | } | |
2147 | } | |
2148 | ||
edf7de31 ON |
2149 | /* stop any bus activity of a mixer */ |
2150 | void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) | |
2151 | { | |
2152 | usb_kill_urb(mixer->urb); | |
2153 | usb_kill_urb(mixer->rc_urb); | |
2154 | } | |
2155 | ||
2156 | int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) | |
2157 | { | |
2158 | int err; | |
2159 | ||
2160 | if (mixer->urb) { | |
2161 | err = usb_submit_urb(mixer->urb, GFP_NOIO); | |
2162 | if (err < 0) | |
2163 | return err; | |
2164 | } | |
2165 | ||
2166 | return 0; | |
2167 | } | |
2168 | ||
6639b6c2 CL |
2169 | /* create the handler for the optional status interrupt endpoint */ |
2170 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) | |
2171 | { | |
2172 | struct usb_host_interface *hostif; | |
2173 | struct usb_endpoint_descriptor *ep; | |
2174 | void *transfer_buffer; | |
2175 | int buffer_length; | |
2176 | unsigned int epnum; | |
2177 | ||
3d8d4dcf | 2178 | hostif = mixer->chip->ctrl_intf; |
6639b6c2 CL |
2179 | /* we need one interrupt input endpoint */ |
2180 | if (get_iface_desc(hostif)->bNumEndpoints < 1) | |
2181 | return 0; | |
2182 | ep = get_endpoint(hostif, 0); | |
913ae5a2 | 2183 | if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) |
6639b6c2 CL |
2184 | return 0; |
2185 | ||
42a6e66f | 2186 | epnum = usb_endpoint_num(ep); |
6639b6c2 CL |
2187 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); |
2188 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); | |
2189 | if (!transfer_buffer) | |
2190 | return -ENOMEM; | |
2191 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); | |
2192 | if (!mixer->urb) { | |
2193 | kfree(transfer_buffer); | |
2194 | return -ENOMEM; | |
2195 | } | |
2196 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, | |
2197 | usb_rcvintpipe(mixer->chip->dev, epnum), | |
2198 | transfer_buffer, buffer_length, | |
e213e9cf | 2199 | snd_usb_mixer_interrupt, mixer, ep->bInterval); |
6639b6c2 CL |
2200 | usb_submit_urb(mixer->urb, GFP_KERNEL); |
2201 | return 0; | |
2202 | } | |
2203 | ||
7a9b8063 TI |
2204 | int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, |
2205 | int ignore_error) | |
84957a8a | 2206 | { |
86e07d34 | 2207 | static struct snd_device_ops dev_ops = { |
84957a8a CL |
2208 | .dev_free = snd_usb_mixer_dev_free |
2209 | }; | |
2210 | struct usb_mixer_interface *mixer; | |
ebfdeea3 | 2211 | struct snd_info_entry *entry; |
7b8a043f | 2212 | struct usb_host_interface *host_iface; |
23caaf19 | 2213 | int err; |
84957a8a CL |
2214 | |
2215 | strcpy(chip->card->mixername, "USB Mixer"); | |
2216 | ||
561b220a | 2217 | mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); |
84957a8a CL |
2218 | if (!mixer) |
2219 | return -ENOMEM; | |
2220 | mixer->chip = chip; | |
7a9b8063 | 2221 | mixer->ignore_ctl_error = ignore_error; |
291186e0 JK |
2222 | mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), |
2223 | GFP_KERNEL); | |
6639b6c2 CL |
2224 | if (!mixer->id_elems) { |
2225 | kfree(mixer); | |
2226 | return -ENOMEM; | |
2227 | } | |
84957a8a | 2228 | |
7b8a043f | 2229 | host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; |
a2acad82 CL |
2230 | switch (get_iface_desc(host_iface)->bInterfaceProtocol) { |
2231 | case UAC_VERSION_1: | |
2232 | default: | |
2233 | mixer->protocol = UAC_VERSION_1; | |
2234 | break; | |
2235 | case UAC_VERSION_2: | |
2236 | mixer->protocol = UAC_VERSION_2; | |
2237 | break; | |
2238 | } | |
7b8a043f | 2239 | |
6639b6c2 | 2240 | if ((err = snd_usb_mixer_controls(mixer)) < 0 || |
93446edc CL |
2241 | (err = snd_usb_mixer_status_create(mixer)) < 0) |
2242 | goto _error; | |
84957a8a | 2243 | |
7b1eda22 | 2244 | snd_usb_mixer_apply_create_quirk(mixer); |
468b8fde | 2245 | |
84957a8a | 2246 | err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops); |
93446edc CL |
2247 | if (err < 0) |
2248 | goto _error; | |
ebfdeea3 JK |
2249 | |
2250 | if (list_empty(&chip->mixer_list) && | |
2251 | !snd_card_proc_new(chip->card, "usbmixer", &entry)) | |
2252 | snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read); | |
2253 | ||
84957a8a CL |
2254 | list_add(&mixer->list, &chip->mixer_list); |
2255 | return 0; | |
93446edc CL |
2256 | |
2257 | _error: | |
2258 | snd_usb_mixer_free(mixer); | |
2259 | return err; | |
84957a8a CL |
2260 | } |
2261 | ||
2262 | void snd_usb_mixer_disconnect(struct list_head *p) | |
2263 | { | |
6639b6c2 | 2264 | struct usb_mixer_interface *mixer; |
7b1eda22 | 2265 | |
6639b6c2 | 2266 | mixer = list_entry(p, struct usb_mixer_interface, list); |
68df9de1 MK |
2267 | usb_kill_urb(mixer->urb); |
2268 | usb_kill_urb(mixer->rc_urb); | |
84957a8a | 2269 | } |