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 | ||
29 | #include <sound/driver.h> | |
30 | #include <linux/bitops.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/list.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/string.h> | |
35 | #include <linux/usb.h> | |
36 | #include <sound/core.h> | |
37 | #include <sound/control.h> | |
b259b10c | 38 | #include <sound/hwdep.h> |
1da177e4 LT |
39 | |
40 | #include "usbaudio.h" | |
41 | ||
1da177e4 LT |
42 | /* |
43 | */ | |
44 | ||
45 | /* ignore error from controls - for debugging */ | |
46 | /* #define IGNORE_CTL_ERROR */ | |
47 | ||
48 | typedef struct usb_mixer_build mixer_build_t; | |
49 | typedef struct usb_audio_term usb_audio_term_t; | |
50 | typedef struct usb_mixer_elem_info usb_mixer_elem_info_t; | |
51 | ||
52 | ||
84957a8a CL |
53 | struct usb_mixer_interface { |
54 | snd_usb_audio_t *chip; | |
55 | unsigned int ctrlif; | |
56 | struct list_head list; | |
57 | unsigned int ignore_ctl_error; | |
6639b6c2 CL |
58 | struct urb *urb; |
59 | usb_mixer_elem_info_t **id_elems; /* array[256], indexed by unit id */ | |
b259b10c CL |
60 | |
61 | /* Sound Blaster remote control stuff */ | |
62 | enum { | |
63 | RC_NONE, | |
64 | RC_EXTIGY, | |
65 | RC_AUDIGY2NX, | |
66 | } rc_type; | |
67 | unsigned long rc_hwdep_open; | |
68 | u32 rc_code; | |
69 | wait_queue_head_t rc_waitq; | |
70 | struct urb *rc_urb; | |
71 | struct usb_ctrlrequest *rc_setup_packet; | |
72 | u8 rc_buffer[6]; | |
93446edc CL |
73 | |
74 | u8 audigy2nx_leds[3]; | |
84957a8a CL |
75 | }; |
76 | ||
77 | ||
1da177e4 LT |
78 | struct usb_audio_term { |
79 | int id; | |
80 | int type; | |
81 | int channels; | |
82 | unsigned int chconfig; | |
83 | int name; | |
84 | }; | |
85 | ||
86 | struct usbmix_name_map; | |
87 | ||
88 | struct usb_mixer_build { | |
89 | snd_usb_audio_t *chip; | |
84957a8a | 90 | struct usb_mixer_interface *mixer; |
1da177e4 LT |
91 | unsigned char *buffer; |
92 | unsigned int buflen; | |
707e6073 | 93 | DECLARE_BITMAP(unitbitmap, 256); |
1da177e4 LT |
94 | usb_audio_term_t oterm; |
95 | const struct usbmix_name_map *map; | |
8e062ec7 | 96 | const struct usbmix_selector_map *selector_map; |
1da177e4 LT |
97 | }; |
98 | ||
99 | struct usb_mixer_elem_info { | |
84957a8a | 100 | struct usb_mixer_interface *mixer; |
6639b6c2 CL |
101 | usb_mixer_elem_info_t *next_id_elem; /* list of controls with same id */ |
102 | snd_ctl_elem_id_t *elem_id; | |
1da177e4 LT |
103 | unsigned int id; |
104 | unsigned int control; /* CS or ICN (high byte) */ | |
105 | unsigned int cmask; /* channel mask bitmap: 0 = master */ | |
106 | int channels; | |
107 | int val_type; | |
108 | int min, max, res; | |
6639b6c2 | 109 | u8 initialized; |
1da177e4 LT |
110 | }; |
111 | ||
112 | ||
113 | enum { | |
114 | USB_FEATURE_NONE = 0, | |
115 | USB_FEATURE_MUTE = 1, | |
116 | USB_FEATURE_VOLUME, | |
117 | USB_FEATURE_BASS, | |
118 | USB_FEATURE_MID, | |
119 | USB_FEATURE_TREBLE, | |
120 | USB_FEATURE_GEQ, | |
121 | USB_FEATURE_AGC, | |
122 | USB_FEATURE_DELAY, | |
123 | USB_FEATURE_BASSBOOST, | |
124 | USB_FEATURE_LOUDNESS | |
125 | }; | |
126 | ||
127 | enum { | |
128 | USB_MIXER_BOOLEAN, | |
129 | USB_MIXER_INV_BOOLEAN, | |
130 | USB_MIXER_S8, | |
131 | USB_MIXER_U8, | |
132 | USB_MIXER_S16, | |
133 | USB_MIXER_U16, | |
134 | }; | |
135 | ||
136 | enum { | |
137 | USB_PROC_UPDOWN = 1, | |
138 | USB_PROC_UPDOWN_SWITCH = 1, | |
139 | USB_PROC_UPDOWN_MODE_SEL = 2, | |
140 | ||
141 | USB_PROC_PROLOGIC = 2, | |
142 | USB_PROC_PROLOGIC_SWITCH = 1, | |
143 | USB_PROC_PROLOGIC_MODE_SEL = 2, | |
144 | ||
145 | USB_PROC_3DENH = 3, | |
146 | USB_PROC_3DENH_SWITCH = 1, | |
147 | USB_PROC_3DENH_SPACE = 2, | |
148 | ||
149 | USB_PROC_REVERB = 4, | |
150 | USB_PROC_REVERB_SWITCH = 1, | |
151 | USB_PROC_REVERB_LEVEL = 2, | |
152 | USB_PROC_REVERB_TIME = 3, | |
153 | USB_PROC_REVERB_DELAY = 4, | |
154 | ||
155 | USB_PROC_CHORUS = 5, | |
156 | USB_PROC_CHORUS_SWITCH = 1, | |
157 | USB_PROC_CHORUS_LEVEL = 2, | |
158 | USB_PROC_CHORUS_RATE = 3, | |
159 | USB_PROC_CHORUS_DEPTH = 4, | |
160 | ||
161 | USB_PROC_DCR = 6, | |
162 | USB_PROC_DCR_SWITCH = 1, | |
163 | USB_PROC_DCR_RATIO = 2, | |
164 | USB_PROC_DCR_MAX_AMP = 3, | |
165 | USB_PROC_DCR_THRESHOLD = 4, | |
166 | USB_PROC_DCR_ATTACK = 5, | |
167 | USB_PROC_DCR_RELEASE = 6, | |
168 | }; | |
169 | ||
170 | #define MAX_CHANNELS 10 /* max logical channels */ | |
171 | ||
172 | ||
173 | /* | |
174 | * manual mapping of mixer names | |
175 | * if the mixer topology is too complicated and the parsed names are | |
176 | * ambiguous, add the entries in usbmixer_maps.c. | |
177 | */ | |
178 | #include "usbmixer_maps.c" | |
179 | ||
180 | /* get the mapped name if the unit matches */ | |
181 | static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen) | |
182 | { | |
183 | const struct usbmix_name_map *p; | |
184 | ||
185 | if (! state->map) | |
186 | return 0; | |
187 | ||
188 | for (p = state->map; p->id; p++) { | |
189 | if (p->id == unitid && p->name && | |
190 | (! control || ! p->control || control == p->control)) { | |
191 | buflen--; | |
192 | return strlcpy(buf, p->name, buflen); | |
193 | } | |
194 | } | |
195 | return 0; | |
196 | } | |
197 | ||
198 | /* check whether the control should be ignored */ | |
199 | static int check_ignored_ctl(mixer_build_t *state, int unitid, int control) | |
200 | { | |
201 | const struct usbmix_name_map *p; | |
202 | ||
203 | if (! state->map) | |
204 | return 0; | |
205 | for (p = state->map; p->id; p++) { | |
206 | if (p->id == unitid && ! p->name && | |
207 | (! control || ! p->control || control == p->control)) { | |
208 | // printk("ignored control %d:%d\n", unitid, control); | |
209 | return 1; | |
210 | } | |
211 | } | |
212 | return 0; | |
213 | } | |
214 | ||
8e062ec7 CL |
215 | /* get the mapped selector source name */ |
216 | static int check_mapped_selector_name(mixer_build_t *state, int unitid, | |
217 | int index, char *buf, int buflen) | |
218 | { | |
219 | const struct usbmix_selector_map *p; | |
220 | ||
221 | if (! state->selector_map) | |
222 | return 0; | |
223 | for (p = state->selector_map; p->id; p++) { | |
224 | if (p->id == unitid && index < p->count) | |
225 | return strlcpy(buf, p->names[index], buflen); | |
226 | } | |
227 | return 0; | |
228 | } | |
229 | ||
1da177e4 LT |
230 | /* |
231 | * find an audio control unit with the given unit id | |
232 | */ | |
233 | static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit) | |
234 | { | |
235 | unsigned char *p; | |
236 | ||
237 | p = NULL; | |
238 | while ((p = snd_usb_find_desc(state->buffer, state->buflen, p, | |
239 | USB_DT_CS_INTERFACE)) != NULL) { | |
240 | if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit) | |
241 | return p; | |
242 | } | |
243 | return NULL; | |
244 | } | |
245 | ||
246 | ||
247 | /* | |
248 | * copy a string with the given id | |
249 | */ | |
250 | static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen) | |
251 | { | |
252 | int len = usb_string(state->chip->dev, index, buf, maxlen - 1); | |
253 | buf[len] = 0; | |
254 | return len; | |
255 | } | |
256 | ||
257 | /* | |
258 | * convert from the byte/word on usb descriptor to the zero-based integer | |
259 | */ | |
260 | static int convert_signed_value(usb_mixer_elem_info_t *cval, int val) | |
261 | { | |
262 | switch (cval->val_type) { | |
263 | case USB_MIXER_BOOLEAN: | |
264 | return !!val; | |
265 | case USB_MIXER_INV_BOOLEAN: | |
266 | return !val; | |
267 | case USB_MIXER_U8: | |
268 | val &= 0xff; | |
269 | break; | |
270 | case USB_MIXER_S8: | |
271 | val &= 0xff; | |
272 | if (val >= 0x80) | |
273 | val -= 0x100; | |
274 | break; | |
275 | case USB_MIXER_U16: | |
276 | val &= 0xffff; | |
277 | break; | |
278 | case USB_MIXER_S16: | |
279 | val &= 0xffff; | |
280 | if (val >= 0x8000) | |
281 | val -= 0x10000; | |
282 | break; | |
283 | } | |
284 | return val; | |
285 | } | |
286 | ||
287 | /* | |
288 | * convert from the zero-based int to the byte/word for usb descriptor | |
289 | */ | |
290 | static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val) | |
291 | { | |
292 | switch (cval->val_type) { | |
293 | case USB_MIXER_BOOLEAN: | |
294 | return !!val; | |
295 | case USB_MIXER_INV_BOOLEAN: | |
296 | return !val; | |
297 | case USB_MIXER_S8: | |
298 | case USB_MIXER_U8: | |
299 | return val & 0xff; | |
300 | case USB_MIXER_S16: | |
301 | case USB_MIXER_U16: | |
302 | return val & 0xffff; | |
303 | } | |
304 | return 0; /* not reached */ | |
305 | } | |
306 | ||
307 | static int get_relative_value(usb_mixer_elem_info_t *cval, int val) | |
308 | { | |
309 | if (! cval->res) | |
310 | cval->res = 1; | |
311 | if (val < cval->min) | |
312 | return 0; | |
313 | else if (val > cval->max) | |
314 | return (cval->max - cval->min) / cval->res; | |
315 | else | |
316 | return (val - cval->min) / cval->res; | |
317 | } | |
318 | ||
319 | static int get_abs_value(usb_mixer_elem_info_t *cval, int val) | |
320 | { | |
321 | if (val < 0) | |
322 | return cval->min; | |
323 | if (! cval->res) | |
324 | cval->res = 1; | |
325 | val *= cval->res; | |
326 | val += cval->min; | |
327 | if (val > cval->max) | |
328 | return cval->max; | |
329 | return val; | |
330 | } | |
331 | ||
332 | ||
333 | /* | |
334 | * retrieve a mixer value | |
335 | */ | |
336 | ||
337 | static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret) | |
338 | { | |
339 | unsigned char buf[2]; | |
340 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; | |
341 | int timeout = 10; | |
342 | ||
343 | while (timeout-- > 0) { | |
84957a8a CL |
344 | if (snd_usb_ctl_msg(cval->mixer->chip->dev, |
345 | usb_rcvctrlpipe(cval->mixer->chip->dev, 0), | |
1da177e4 LT |
346 | request, |
347 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
84957a8a | 348 | validx, cval->mixer->ctrlif | (cval->id << 8), |
1da177e4 LT |
349 | buf, val_len, 100) >= 0) { |
350 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); | |
351 | return 0; | |
352 | } | |
353 | } | |
84957a8a CL |
354 | snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
355 | request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type); | |
1da177e4 LT |
356 | return -EINVAL; |
357 | } | |
358 | ||
359 | static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value) | |
360 | { | |
361 | return get_ctl_value(cval, GET_CUR, validx, value); | |
362 | } | |
363 | ||
364 | /* channel = 0: master, 1 = first channel */ | |
365 | inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value) | |
366 | { | |
367 | return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value); | |
368 | } | |
369 | ||
370 | /* | |
371 | * set a mixer value | |
372 | */ | |
373 | ||
374 | static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set) | |
375 | { | |
376 | unsigned char buf[2]; | |
377 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; | |
378 | int timeout = 10; | |
379 | ||
380 | value_set = convert_bytes_value(cval, value_set); | |
381 | buf[0] = value_set & 0xff; | |
382 | buf[1] = (value_set >> 8) & 0xff; | |
383 | while (timeout -- > 0) | |
84957a8a CL |
384 | if (snd_usb_ctl_msg(cval->mixer->chip->dev, |
385 | usb_sndctrlpipe(cval->mixer->chip->dev, 0), | |
1da177e4 LT |
386 | request, |
387 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | |
84957a8a | 388 | validx, cval->mixer->ctrlif | (cval->id << 8), |
1da177e4 LT |
389 | buf, val_len, 100) >= 0) |
390 | return 0; | |
84957a8a CL |
391 | snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", |
392 | request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]); | |
1da177e4 LT |
393 | return -EINVAL; |
394 | } | |
395 | ||
396 | static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value) | |
397 | { | |
398 | return set_ctl_value(cval, SET_CUR, validx, value); | |
399 | } | |
400 | ||
401 | inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value) | |
402 | { | |
403 | return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value); | |
404 | } | |
405 | ||
406 | ||
407 | /* | |
408 | * parser routines begin here... | |
409 | */ | |
410 | ||
411 | static int parse_audio_unit(mixer_build_t *state, int unitid); | |
412 | ||
413 | ||
414 | /* | |
415 | * check if the input/output channel routing is enabled on the given bitmap. | |
416 | * used for mixer unit parser | |
417 | */ | |
418 | static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs) | |
419 | { | |
420 | int idx = ich * num_outs + och; | |
421 | return bmap[idx >> 3] & (0x80 >> (idx & 7)); | |
422 | } | |
423 | ||
424 | ||
425 | /* | |
426 | * add an alsa control element | |
427 | * search and increment the index until an empty slot is found. | |
428 | * | |
429 | * if failed, give up and free the control instance. | |
430 | */ | |
431 | ||
84957a8a | 432 | static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl) |
1da177e4 | 433 | { |
6639b6c2 | 434 | usb_mixer_elem_info_t *cval = kctl->private_data; |
1da177e4 | 435 | int err; |
84957a8a CL |
436 | |
437 | while (snd_ctl_find_id(state->chip->card, &kctl->id)) | |
1da177e4 | 438 | kctl->id.index++; |
84957a8a | 439 | if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) { |
1da177e4 LT |
440 | snd_printd(KERN_ERR "cannot add control (err = %d)\n", err); |
441 | snd_ctl_free_one(kctl); | |
6639b6c2 | 442 | return err; |
1da177e4 | 443 | } |
6639b6c2 CL |
444 | cval->elem_id = &kctl->id; |
445 | cval->next_id_elem = state->mixer->id_elems[cval->id]; | |
446 | state->mixer->id_elems[cval->id] = cval; | |
447 | return 0; | |
1da177e4 LT |
448 | } |
449 | ||
450 | ||
451 | /* | |
452 | * get a terminal name string | |
453 | */ | |
454 | ||
455 | static struct iterm_name_combo { | |
456 | int type; | |
457 | char *name; | |
458 | } iterm_names[] = { | |
459 | { 0x0300, "Output" }, | |
460 | { 0x0301, "Speaker" }, | |
461 | { 0x0302, "Headphone" }, | |
462 | { 0x0303, "HMD Audio" }, | |
463 | { 0x0304, "Desktop Speaker" }, | |
464 | { 0x0305, "Room Speaker" }, | |
465 | { 0x0306, "Com Speaker" }, | |
466 | { 0x0307, "LFE" }, | |
467 | { 0x0600, "External In" }, | |
468 | { 0x0601, "Analog In" }, | |
469 | { 0x0602, "Digital In" }, | |
470 | { 0x0603, "Line" }, | |
471 | { 0x0604, "Legacy In" }, | |
472 | { 0x0605, "IEC958 In" }, | |
473 | { 0x0606, "1394 DA Stream" }, | |
474 | { 0x0607, "1394 DV Stream" }, | |
475 | { 0x0700, "Embedded" }, | |
476 | { 0x0701, "Noise Source" }, | |
477 | { 0x0702, "Equalization Noise" }, | |
478 | { 0x0703, "CD" }, | |
479 | { 0x0704, "DAT" }, | |
480 | { 0x0705, "DCC" }, | |
481 | { 0x0706, "MiniDisk" }, | |
482 | { 0x0707, "Analog Tape" }, | |
483 | { 0x0708, "Phonograph" }, | |
484 | { 0x0709, "VCR Audio" }, | |
485 | { 0x070a, "Video Disk Audio" }, | |
486 | { 0x070b, "DVD Audio" }, | |
487 | { 0x070c, "TV Tuner Audio" }, | |
488 | { 0x070d, "Satellite Rec Audio" }, | |
489 | { 0x070e, "Cable Tuner Audio" }, | |
490 | { 0x070f, "DSS Audio" }, | |
491 | { 0x0710, "Radio Receiver" }, | |
492 | { 0x0711, "Radio Transmitter" }, | |
493 | { 0x0712, "Multi-Track Recorder" }, | |
494 | { 0x0713, "Synthesizer" }, | |
495 | { 0 }, | |
496 | }; | |
497 | ||
498 | static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm, | |
499 | unsigned char *name, int maxlen, int term_only) | |
500 | { | |
501 | struct iterm_name_combo *names; | |
502 | ||
503 | if (iterm->name) | |
504 | return snd_usb_copy_string_desc(state, iterm->name, name, maxlen); | |
505 | ||
506 | /* virtual type - not a real terminal */ | |
507 | if (iterm->type >> 16) { | |
508 | if (term_only) | |
509 | return 0; | |
510 | switch (iterm->type >> 16) { | |
511 | case SELECTOR_UNIT: | |
512 | strcpy(name, "Selector"); return 8; | |
513 | case PROCESSING_UNIT: | |
514 | strcpy(name, "Process Unit"); return 12; | |
515 | case EXTENSION_UNIT: | |
516 | strcpy(name, "Ext Unit"); return 8; | |
517 | case MIXER_UNIT: | |
518 | strcpy(name, "Mixer"); return 5; | |
519 | default: | |
520 | return sprintf(name, "Unit %d", iterm->id); | |
521 | } | |
522 | } | |
523 | ||
524 | switch (iterm->type & 0xff00) { | |
525 | case 0x0100: | |
526 | strcpy(name, "PCM"); return 3; | |
527 | case 0x0200: | |
528 | strcpy(name, "Mic"); return 3; | |
529 | case 0x0400: | |
530 | strcpy(name, "Headset"); return 7; | |
531 | case 0x0500: | |
532 | strcpy(name, "Phone"); return 5; | |
533 | } | |
534 | ||
535 | for (names = iterm_names; names->type; names++) | |
536 | if (names->type == iterm->type) { | |
537 | strcpy(name, names->name); | |
538 | return strlen(names->name); | |
539 | } | |
540 | return 0; | |
541 | } | |
542 | ||
543 | ||
544 | /* | |
545 | * parse the source unit recursively until it reaches to a terminal | |
546 | * or a branched unit. | |
547 | */ | |
548 | static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term) | |
549 | { | |
550 | unsigned char *p1; | |
551 | ||
552 | memset(term, 0, sizeof(*term)); | |
553 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { | |
554 | term->id = id; | |
555 | switch (p1[2]) { | |
556 | case INPUT_TERMINAL: | |
557 | term->type = combine_word(p1 + 4); | |
558 | term->channels = p1[7]; | |
559 | term->chconfig = combine_word(p1 + 8); | |
560 | term->name = p1[11]; | |
561 | return 0; | |
562 | case FEATURE_UNIT: | |
563 | id = p1[4]; | |
564 | break; /* continue to parse */ | |
565 | case MIXER_UNIT: | |
566 | term->type = p1[2] << 16; /* virtual type */ | |
567 | term->channels = p1[5 + p1[4]]; | |
568 | term->chconfig = combine_word(p1 + 6 + p1[4]); | |
569 | term->name = p1[p1[0] - 1]; | |
570 | return 0; | |
571 | case SELECTOR_UNIT: | |
572 | /* call recursively to retrieve the channel info */ | |
573 | if (check_input_term(state, p1[5], term) < 0) | |
574 | return -ENODEV; | |
575 | term->type = p1[2] << 16; /* virtual type */ | |
576 | term->id = id; | |
577 | term->name = p1[9 + p1[0] - 1]; | |
578 | return 0; | |
579 | case PROCESSING_UNIT: | |
580 | case EXTENSION_UNIT: | |
581 | if (p1[6] == 1) { | |
582 | id = p1[7]; | |
583 | break; /* continue to parse */ | |
584 | } | |
585 | term->type = p1[2] << 16; /* virtual type */ | |
586 | term->channels = p1[7 + p1[6]]; | |
587 | term->chconfig = combine_word(p1 + 8 + p1[6]); | |
588 | term->name = p1[12 + p1[6] + p1[11 + p1[6]]]; | |
589 | return 0; | |
590 | default: | |
591 | return -ENODEV; | |
592 | } | |
593 | } | |
594 | return -ENODEV; | |
595 | } | |
596 | ||
597 | ||
598 | /* | |
599 | * Feature Unit | |
600 | */ | |
601 | ||
602 | /* feature unit control information */ | |
603 | struct usb_feature_control_info { | |
604 | const char *name; | |
605 | unsigned int type; /* control type (mute, volume, etc.) */ | |
606 | }; | |
607 | ||
608 | static struct usb_feature_control_info audio_feature_info[] = { | |
609 | { "Mute", USB_MIXER_INV_BOOLEAN }, | |
610 | { "Volume", USB_MIXER_S16 }, | |
611 | { "Tone Control - Bass", USB_MIXER_S8 }, | |
612 | { "Tone Control - Mid", USB_MIXER_S8 }, | |
613 | { "Tone Control - Treble", USB_MIXER_S8 }, | |
614 | { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */ | |
615 | { "Auto Gain Control", USB_MIXER_BOOLEAN }, | |
616 | { "Delay Control", USB_MIXER_U16 }, | |
617 | { "Bass Boost", USB_MIXER_BOOLEAN }, | |
618 | { "Loudness", USB_MIXER_BOOLEAN }, | |
619 | }; | |
620 | ||
621 | ||
622 | /* private_free callback */ | |
623 | static void usb_mixer_elem_free(snd_kcontrol_t *kctl) | |
624 | { | |
625 | if (kctl->private_data) { | |
626 | kfree(kctl->private_data); | |
627 | kctl->private_data = NULL; | |
628 | } | |
629 | } | |
630 | ||
631 | ||
632 | /* | |
633 | * interface to ALSA control for feature/mixer units | |
634 | */ | |
635 | ||
636 | /* | |
637 | * retrieve the minimum and maximum values for the specified control | |
638 | */ | |
639 | static int get_min_max(usb_mixer_elem_info_t *cval, int default_min) | |
640 | { | |
641 | /* for failsafe */ | |
642 | cval->min = default_min; | |
643 | cval->max = cval->min + 1; | |
644 | cval->res = 1; | |
645 | ||
646 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
647 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
648 | cval->initialized = 1; | |
649 | } else { | |
650 | int minchn = 0; | |
651 | if (cval->cmask) { | |
652 | int i; | |
653 | for (i = 0; i < MAX_CHANNELS; i++) | |
654 | if (cval->cmask & (1 << i)) { | |
655 | minchn = i + 1; | |
656 | break; | |
657 | } | |
658 | } | |
659 | if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || | |
660 | get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { | |
84957a8a CL |
661 | snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", |
662 | cval->id, cval->mixer->ctrlif, cval->control, cval->id); | |
1da177e4 LT |
663 | return -EINVAL; |
664 | } | |
665 | if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) { | |
666 | cval->res = 1; | |
667 | } else { | |
668 | int last_valid_res = cval->res; | |
669 | ||
670 | while (cval->res > 1) { | |
671 | if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0) | |
672 | break; | |
673 | cval->res /= 2; | |
674 | } | |
675 | if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) | |
676 | cval->res = last_valid_res; | |
677 | } | |
678 | if (cval->res == 0) | |
679 | cval->res = 1; | |
680 | cval->initialized = 1; | |
681 | } | |
682 | return 0; | |
683 | } | |
684 | ||
685 | ||
686 | /* get a feature/mixer unit info */ | |
687 | static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo) | |
688 | { | |
689 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
690 | ||
691 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
692 | cval->val_type == USB_MIXER_INV_BOOLEAN) | |
693 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
694 | else | |
695 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
696 | uinfo->count = cval->channels; | |
697 | if (cval->val_type == USB_MIXER_BOOLEAN || | |
698 | cval->val_type == USB_MIXER_INV_BOOLEAN) { | |
699 | uinfo->value.integer.min = 0; | |
700 | uinfo->value.integer.max = 1; | |
701 | } else { | |
702 | if (! cval->initialized) | |
703 | get_min_max(cval, 0); | |
704 | uinfo->value.integer.min = 0; | |
705 | uinfo->value.integer.max = (cval->max - cval->min) / cval->res; | |
706 | } | |
707 | return 0; | |
708 | } | |
709 | ||
710 | /* get the current value from feature/mixer unit */ | |
711 | static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
712 | { | |
713 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
714 | int c, cnt, val, err; | |
715 | ||
716 | if (cval->cmask) { | |
717 | cnt = 0; | |
718 | for (c = 0; c < MAX_CHANNELS; c++) { | |
719 | if (cval->cmask & (1 << c)) { | |
720 | err = get_cur_mix_value(cval, c + 1, &val); | |
721 | if (err < 0) { | |
84957a8a | 722 | if (cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
723 | ucontrol->value.integer.value[0] = cval->min; |
724 | return 0; | |
725 | } | |
726 | snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err); | |
727 | return err; | |
728 | } | |
729 | val = get_relative_value(cval, val); | |
730 | ucontrol->value.integer.value[cnt] = val; | |
731 | cnt++; | |
732 | } | |
733 | } | |
734 | } else { | |
735 | /* master channel */ | |
736 | err = get_cur_mix_value(cval, 0, &val); | |
737 | if (err < 0) { | |
84957a8a | 738 | if (cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
739 | ucontrol->value.integer.value[0] = cval->min; |
740 | return 0; | |
741 | } | |
742 | snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err); | |
743 | return err; | |
744 | } | |
745 | val = get_relative_value(cval, val); | |
746 | ucontrol->value.integer.value[0] = val; | |
747 | } | |
748 | return 0; | |
749 | } | |
750 | ||
751 | /* put the current value to feature/mixer unit */ | |
752 | static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
753 | { | |
754 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
755 | int c, cnt, val, oval, err; | |
756 | int changed = 0; | |
757 | ||
758 | if (cval->cmask) { | |
759 | cnt = 0; | |
760 | for (c = 0; c < MAX_CHANNELS; c++) { | |
761 | if (cval->cmask & (1 << c)) { | |
762 | err = get_cur_mix_value(cval, c + 1, &oval); | |
763 | if (err < 0) { | |
84957a8a | 764 | if (cval->mixer->ignore_ctl_error) |
1da177e4 LT |
765 | return 0; |
766 | return err; | |
767 | } | |
768 | val = ucontrol->value.integer.value[cnt]; | |
769 | val = get_abs_value(cval, val); | |
770 | if (oval != val) { | |
771 | set_cur_mix_value(cval, c + 1, val); | |
772 | changed = 1; | |
773 | } | |
774 | get_cur_mix_value(cval, c + 1, &val); | |
775 | cnt++; | |
776 | } | |
777 | } | |
778 | } else { | |
779 | /* master channel */ | |
780 | err = get_cur_mix_value(cval, 0, &oval); | |
84957a8a | 781 | if (err < 0 && cval->mixer->ignore_ctl_error) |
1da177e4 LT |
782 | return 0; |
783 | if (err < 0) | |
784 | return err; | |
785 | val = ucontrol->value.integer.value[0]; | |
786 | val = get_abs_value(cval, val); | |
787 | if (val != oval) { | |
788 | set_cur_mix_value(cval, 0, val); | |
789 | changed = 1; | |
790 | } | |
791 | } | |
792 | return changed; | |
793 | } | |
794 | ||
795 | static snd_kcontrol_new_t usb_feature_unit_ctl = { | |
796 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
797 | .name = "", /* will be filled later manually */ | |
798 | .info = mixer_ctl_feature_info, | |
799 | .get = mixer_ctl_feature_get, | |
800 | .put = mixer_ctl_feature_put, | |
801 | }; | |
802 | ||
803 | ||
804 | /* | |
805 | * build a feature control | |
806 | */ | |
807 | ||
808 | static void build_feature_ctl(mixer_build_t *state, unsigned char *desc, | |
809 | unsigned int ctl_mask, int control, | |
810 | usb_audio_term_t *iterm, int unitid) | |
811 | { | |
812 | unsigned int len = 0; | |
813 | int mapped_name = 0; | |
814 | int nameid = desc[desc[0] - 1]; | |
815 | snd_kcontrol_t *kctl; | |
816 | usb_mixer_elem_info_t *cval; | |
817 | ||
818 | control++; /* change from zero-based to 1-based value */ | |
819 | ||
820 | if (control == USB_FEATURE_GEQ) { | |
821 | /* FIXME: not supported yet */ | |
822 | return; | |
823 | } | |
824 | ||
825 | if (check_ignored_ctl(state, unitid, control)) | |
826 | return; | |
827 | ||
828 | cval = kcalloc(1, sizeof(*cval), GFP_KERNEL); | |
829 | if (! cval) { | |
830 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
831 | return; | |
832 | } | |
84957a8a | 833 | cval->mixer = state->mixer; |
1da177e4 LT |
834 | cval->id = unitid; |
835 | cval->control = control; | |
836 | cval->cmask = ctl_mask; | |
837 | cval->val_type = audio_feature_info[control-1].type; | |
838 | if (ctl_mask == 0) | |
839 | cval->channels = 1; /* master channel */ | |
840 | else { | |
841 | int i, c = 0; | |
842 | for (i = 0; i < 16; i++) | |
843 | if (ctl_mask & (1 << i)) | |
844 | c++; | |
845 | cval->channels = c; | |
846 | } | |
847 | ||
848 | /* get min/max values */ | |
849 | get_min_max(cval, 0); | |
850 | ||
851 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
852 | if (! kctl) { | |
853 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
854 | kfree(cval); | |
855 | return; | |
856 | } | |
857 | kctl->private_free = usb_mixer_elem_free; | |
858 | ||
859 | len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name)); | |
860 | mapped_name = len != 0; | |
861 | if (! len && nameid) | |
862 | len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); | |
863 | ||
864 | switch (control) { | |
865 | case USB_FEATURE_MUTE: | |
866 | case USB_FEATURE_VOLUME: | |
867 | /* determine the control name. the rule is: | |
868 | * - if a name id is given in descriptor, use it. | |
869 | * - if the connected input can be determined, then use the name | |
870 | * of terminal type. | |
871 | * - if the connected output can be determined, use it. | |
872 | * - otherwise, anonymous name. | |
873 | */ | |
874 | if (! len) { | |
875 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1); | |
876 | if (! len) | |
877 | len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1); | |
878 | if (! len) | |
879 | len = snprintf(kctl->id.name, sizeof(kctl->id.name), | |
880 | "Feature %d", unitid); | |
881 | } | |
882 | /* determine the stream direction: | |
883 | * if the connected output is USB stream, then it's likely a | |
884 | * capture stream. otherwise it should be playback (hopefully :) | |
885 | */ | |
886 | if (! mapped_name && ! (state->oterm.type >> 16)) { | |
887 | if ((state->oterm.type & 0xff00) == 0x0100) { | |
888 | len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name)); | |
889 | } else { | |
890 | len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name)); | |
891 | } | |
892 | } | |
893 | strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume", | |
894 | sizeof(kctl->id.name)); | |
895 | break; | |
896 | ||
897 | default: | |
898 | if (! len) | |
899 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, | |
900 | sizeof(kctl->id.name)); | |
901 | break; | |
902 | } | |
903 | ||
904 | /* quirk for UDA1321/N101 */ | |
905 | /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */ | |
906 | /* is not very clear from datasheets */ | |
907 | /* I hope that the min value is -15360 for newer firmware --jk */ | |
27d10f56 CL |
908 | switch (state->chip->usb_id) { |
909 | case USB_ID(0x0471, 0x0101): | |
910 | case USB_ID(0x0471, 0x0104): | |
911 | case USB_ID(0x0471, 0x0105): | |
912 | case USB_ID(0x0672, 0x1041): | |
913 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && | |
914 | cval->min == -15616) { | |
915 | snd_printk("using volume control quirk for the UDA1321/N101 chip\n"); | |
916 | cval->max = -256; | |
917 | } | |
1da177e4 LT |
918 | } |
919 | ||
920 | snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", | |
921 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res); | |
84957a8a | 922 | add_control_to_empty(state, kctl); |
1da177e4 LT |
923 | } |
924 | ||
925 | ||
926 | ||
927 | /* | |
928 | * parse a feature unit | |
929 | * | |
930 | * most of controlls are defined here. | |
931 | */ | |
932 | static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr) | |
933 | { | |
934 | int channels, i, j; | |
935 | usb_audio_term_t iterm; | |
936 | unsigned int master_bits, first_ch_bits; | |
937 | int err, csize; | |
938 | ||
939 | if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) { | |
940 | snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid); | |
941 | return -EINVAL; | |
942 | } | |
943 | ||
944 | /* parse the source unit */ | |
945 | if ((err = parse_audio_unit(state, ftr[4])) < 0) | |
946 | return err; | |
947 | ||
948 | /* determine the input source type and name */ | |
949 | if (check_input_term(state, ftr[4], &iterm) < 0) | |
950 | return -EINVAL; | |
951 | ||
952 | channels = (ftr[0] - 7) / csize - 1; | |
953 | ||
954 | master_bits = snd_usb_combine_bytes(ftr + 6, csize); | |
955 | if (channels > 0) | |
956 | first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize); | |
957 | else | |
958 | first_ch_bits = 0; | |
959 | /* check all control types */ | |
960 | for (i = 0; i < 10; i++) { | |
961 | unsigned int ch_bits = 0; | |
962 | for (j = 0; j < channels; j++) { | |
963 | unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize); | |
964 | if (mask & (1 << i)) | |
965 | ch_bits |= (1 << j); | |
966 | } | |
967 | if (ch_bits & 1) /* the first channel must be set (for ease of programming) */ | |
968 | build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid); | |
969 | if (master_bits & (1 << i)) | |
970 | build_feature_ctl(state, ftr, 0, i, &iterm, unitid); | |
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | ||
977 | /* | |
978 | * Mixer Unit | |
979 | */ | |
980 | ||
981 | /* | |
982 | * build a mixer unit control | |
983 | * | |
984 | * the callbacks are identical with feature unit. | |
985 | * input channel number (zero based) is given in control field instead. | |
986 | */ | |
987 | ||
988 | static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc, | |
989 | int in_pin, int in_ch, int unitid, | |
990 | usb_audio_term_t *iterm) | |
991 | { | |
992 | usb_mixer_elem_info_t *cval; | |
993 | unsigned int input_pins = desc[4]; | |
994 | unsigned int num_outs = desc[5 + input_pins]; | |
995 | unsigned int i, len; | |
996 | snd_kcontrol_t *kctl; | |
997 | ||
998 | if (check_ignored_ctl(state, unitid, 0)) | |
999 | return; | |
1000 | ||
1001 | cval = kcalloc(1, sizeof(*cval), GFP_KERNEL); | |
1002 | if (! cval) | |
1003 | return; | |
1004 | ||
84957a8a | 1005 | cval->mixer = state->mixer; |
1da177e4 LT |
1006 | cval->id = unitid; |
1007 | cval->control = in_ch + 1; /* based on 1 */ | |
1008 | cval->val_type = USB_MIXER_S16; | |
1009 | for (i = 0; i < num_outs; i++) { | |
1010 | if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) { | |
1011 | cval->cmask |= (1 << i); | |
1012 | cval->channels++; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | /* get min/max values */ | |
1017 | get_min_max(cval, 0); | |
1018 | ||
1019 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); | |
1020 | if (! kctl) { | |
1021 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1022 | kfree(cval); | |
1023 | return; | |
1024 | } | |
1025 | kctl->private_free = usb_mixer_elem_free; | |
1026 | ||
1027 | len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name)); | |
1028 | if (! len) | |
1029 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0); | |
1030 | if (! len) | |
1031 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); | |
1032 | strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name)); | |
1033 | ||
1034 | snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n", | |
1035 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); | |
84957a8a | 1036 | add_control_to_empty(state, kctl); |
1da177e4 LT |
1037 | } |
1038 | ||
1039 | ||
1040 | /* | |
1041 | * parse a mixer unit | |
1042 | */ | |
1043 | static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc) | |
1044 | { | |
1045 | usb_audio_term_t iterm; | |
1046 | int input_pins, num_ins, num_outs; | |
1047 | int pin, ich, err; | |
1048 | ||
1049 | if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) { | |
1050 | snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid); | |
1051 | return -EINVAL; | |
1052 | } | |
1053 | /* no bmControls field (e.g. Maya44) -> ignore */ | |
1054 | if (desc[0] <= 10 + input_pins) { | |
1055 | snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid); | |
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | num_ins = 0; | |
1060 | ich = 0; | |
1061 | for (pin = 0; pin < input_pins; pin++) { | |
1062 | err = parse_audio_unit(state, desc[5 + pin]); | |
1063 | if (err < 0) | |
1064 | return err; | |
1065 | err = check_input_term(state, desc[5 + pin], &iterm); | |
1066 | if (err < 0) | |
1067 | return err; | |
1068 | num_ins += iterm.channels; | |
1069 | for (; ich < num_ins; ++ich) { | |
1070 | int och, ich_has_controls = 0; | |
1071 | ||
1072 | for (och = 0; och < num_outs; ++och) { | |
1073 | if (check_matrix_bitmap(desc + 9 + input_pins, | |
1074 | ich, och, num_outs)) { | |
1075 | ich_has_controls = 1; | |
1076 | break; | |
1077 | } | |
1078 | } | |
1079 | if (ich_has_controls) | |
1080 | build_mixer_unit_ctl(state, desc, pin, ich, | |
1081 | unitid, &iterm); | |
1082 | } | |
1083 | } | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | ||
1088 | /* | |
1089 | * Processing Unit / Extension Unit | |
1090 | */ | |
1091 | ||
1092 | /* get callback for processing/extension unit */ | |
1093 | static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1094 | { | |
1095 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
1096 | int err, val; | |
1097 | ||
1098 | err = get_cur_ctl_value(cval, cval->control << 8, &val); | |
84957a8a | 1099 | if (err < 0 && cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
1100 | ucontrol->value.integer.value[0] = cval->min; |
1101 | return 0; | |
1102 | } | |
1103 | if (err < 0) | |
1104 | return err; | |
1105 | val = get_relative_value(cval, val); | |
1106 | ucontrol->value.integer.value[0] = val; | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | /* put callback for processing/extension unit */ | |
1111 | static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1112 | { | |
1113 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
1114 | int val, oval, err; | |
1115 | ||
1116 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); | |
1117 | if (err < 0) { | |
84957a8a | 1118 | if (cval->mixer->ignore_ctl_error) |
1da177e4 LT |
1119 | return 0; |
1120 | return err; | |
1121 | } | |
1122 | val = ucontrol->value.integer.value[0]; | |
1123 | val = get_abs_value(cval, val); | |
1124 | if (val != oval) { | |
1125 | set_cur_ctl_value(cval, cval->control << 8, val); | |
1126 | return 1; | |
1127 | } | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | /* alsa control interface for processing/extension unit */ | |
1132 | static snd_kcontrol_new_t mixer_procunit_ctl = { | |
1133 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1134 | .name = "", /* will be filled later */ | |
1135 | .info = mixer_ctl_feature_info, | |
1136 | .get = mixer_ctl_procunit_get, | |
1137 | .put = mixer_ctl_procunit_put, | |
1138 | }; | |
1139 | ||
1140 | ||
1141 | /* | |
1142 | * predefined data for processing units | |
1143 | */ | |
1144 | struct procunit_value_info { | |
1145 | int control; | |
1146 | char *suffix; | |
1147 | int val_type; | |
1148 | int min_value; | |
1149 | }; | |
1150 | ||
1151 | struct procunit_info { | |
1152 | int type; | |
1153 | char *name; | |
1154 | struct procunit_value_info *values; | |
1155 | }; | |
1156 | ||
1157 | static struct procunit_value_info updown_proc_info[] = { | |
1158 | { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1159 | { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 }, | |
1160 | { 0 } | |
1161 | }; | |
1162 | static struct procunit_value_info prologic_proc_info[] = { | |
1163 | { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1164 | { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 }, | |
1165 | { 0 } | |
1166 | }; | |
1167 | static struct procunit_value_info threed_enh_proc_info[] = { | |
1168 | { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1169 | { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 }, | |
1170 | { 0 } | |
1171 | }; | |
1172 | static struct procunit_value_info reverb_proc_info[] = { | |
1173 | { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1174 | { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, | |
1175 | { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 }, | |
1176 | { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 }, | |
1177 | { 0 } | |
1178 | }; | |
1179 | static struct procunit_value_info chorus_proc_info[] = { | |
1180 | { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1181 | { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, | |
1182 | { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, | |
1183 | { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, | |
1184 | { 0 } | |
1185 | }; | |
1186 | static struct procunit_value_info dcr_proc_info[] = { | |
1187 | { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN }, | |
1188 | { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 }, | |
1189 | { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 }, | |
1190 | { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, | |
1191 | { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 }, | |
1192 | { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 }, | |
1193 | { 0 } | |
1194 | }; | |
1195 | ||
1196 | static struct procunit_info procunits[] = { | |
1197 | { USB_PROC_UPDOWN, "Up Down", updown_proc_info }, | |
1198 | { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info }, | |
1199 | { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info }, | |
1200 | { USB_PROC_REVERB, "Reverb", reverb_proc_info }, | |
1201 | { USB_PROC_CHORUS, "Chorus", chorus_proc_info }, | |
1202 | { USB_PROC_DCR, "DCR", dcr_proc_info }, | |
1203 | { 0 }, | |
1204 | }; | |
1205 | ||
1206 | /* | |
1207 | * build a processing/extension unit | |
1208 | */ | |
1209 | static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name) | |
1210 | { | |
1211 | int num_ins = dsc[6]; | |
1212 | usb_mixer_elem_info_t *cval; | |
1213 | snd_kcontrol_t *kctl; | |
1214 | int i, err, nameid, type, len; | |
1215 | struct procunit_info *info; | |
1216 | struct procunit_value_info *valinfo; | |
1217 | static struct procunit_value_info default_value_info[] = { | |
1218 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, | |
1219 | { 0 } | |
1220 | }; | |
1221 | static struct procunit_info default_info = { | |
1222 | 0, NULL, default_value_info | |
1223 | }; | |
1224 | ||
1225 | if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) { | |
1226 | snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid); | |
1227 | return -EINVAL; | |
1228 | } | |
1229 | ||
1230 | for (i = 0; i < num_ins; i++) { | |
1231 | if ((err = parse_audio_unit(state, dsc[7 + i])) < 0) | |
1232 | return err; | |
1233 | } | |
1234 | ||
1235 | type = combine_word(&dsc[4]); | |
1da177e4 LT |
1236 | for (info = list; info && info->type; info++) |
1237 | if (info->type == type) | |
1238 | break; | |
1239 | if (! info || ! info->type) | |
1240 | info = &default_info; | |
1241 | ||
1242 | for (valinfo = info->values; valinfo->control; valinfo++) { | |
1243 | /* FIXME: bitmap might be longer than 8bit */ | |
1244 | if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1)))) | |
1245 | continue; | |
1246 | if (check_ignored_ctl(state, unitid, valinfo->control)) | |
1247 | continue; | |
1248 | cval = kcalloc(1, sizeof(*cval), GFP_KERNEL); | |
1249 | if (! cval) { | |
1250 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1251 | return -ENOMEM; | |
1252 | } | |
84957a8a | 1253 | cval->mixer = state->mixer; |
1da177e4 LT |
1254 | cval->id = unitid; |
1255 | cval->control = valinfo->control; | |
1256 | cval->val_type = valinfo->val_type; | |
1257 | cval->channels = 1; | |
1258 | ||
1259 | /* get min/max values */ | |
1260 | if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) { | |
1261 | /* FIXME: hard-coded */ | |
1262 | cval->min = 1; | |
1263 | cval->max = dsc[15]; | |
1264 | cval->res = 1; | |
1265 | cval->initialized = 1; | |
1266 | } else | |
1267 | get_min_max(cval, valinfo->min_value); | |
1268 | ||
1269 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); | |
1270 | if (! kctl) { | |
1271 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1272 | kfree(cval); | |
1273 | return -ENOMEM; | |
1274 | } | |
1275 | kctl->private_free = usb_mixer_elem_free; | |
1276 | ||
1277 | if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name))) | |
1278 | ; | |
1279 | else if (info->name) | |
1280 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); | |
1281 | else { | |
1282 | nameid = dsc[12 + num_ins + dsc[11 + num_ins]]; | |
1283 | len = 0; | |
1284 | if (nameid) | |
1285 | len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); | |
1286 | if (! len) | |
1287 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); | |
1288 | } | |
1289 | strlcat(kctl->id.name, " ", sizeof(kctl->id.name)); | |
1290 | strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name)); | |
1291 | ||
1292 | snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n", | |
1293 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); | |
84957a8a | 1294 | if ((err = add_control_to_empty(state, kctl)) < 0) |
1da177e4 LT |
1295 | return err; |
1296 | } | |
1297 | return 0; | |
1298 | } | |
1299 | ||
1300 | ||
1301 | static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc) | |
1302 | { | |
1303 | return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit"); | |
1304 | } | |
1305 | ||
1306 | static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc) | |
1307 | { | |
1308 | return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit"); | |
1309 | } | |
1310 | ||
1311 | ||
1312 | /* | |
1313 | * Selector Unit | |
1314 | */ | |
1315 | ||
1316 | /* info callback for selector unit | |
1317 | * use an enumerator type for routing | |
1318 | */ | |
1319 | static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo) | |
1320 | { | |
1321 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
1322 | char **itemlist = (char **)kcontrol->private_value; | |
1323 | ||
1324 | snd_assert(itemlist, return -EINVAL); | |
1325 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1326 | uinfo->count = 1; | |
1327 | uinfo->value.enumerated.items = cval->max; | |
1328 | if ((int)uinfo->value.enumerated.item >= cval->max) | |
1329 | uinfo->value.enumerated.item = cval->max - 1; | |
1330 | strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]); | |
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | /* get callback for selector unit */ | |
1335 | static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1336 | { | |
1337 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
1338 | int val, err; | |
1339 | ||
1340 | err = get_cur_ctl_value(cval, 0, &val); | |
1341 | if (err < 0) { | |
84957a8a | 1342 | if (cval->mixer->ignore_ctl_error) { |
1da177e4 LT |
1343 | ucontrol->value.enumerated.item[0] = 0; |
1344 | return 0; | |
1345 | } | |
1346 | return err; | |
1347 | } | |
1348 | val = get_relative_value(cval, val); | |
1349 | ucontrol->value.enumerated.item[0] = val; | |
1350 | return 0; | |
1351 | } | |
1352 | ||
1353 | /* put callback for selector unit */ | |
1354 | static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1355 | { | |
1356 | usb_mixer_elem_info_t *cval = kcontrol->private_data; | |
1357 | int val, oval, err; | |
1358 | ||
1359 | err = get_cur_ctl_value(cval, 0, &oval); | |
1360 | if (err < 0) { | |
84957a8a | 1361 | if (cval->mixer->ignore_ctl_error) |
1da177e4 LT |
1362 | return 0; |
1363 | return err; | |
1364 | } | |
1365 | val = ucontrol->value.enumerated.item[0]; | |
1366 | val = get_abs_value(cval, val); | |
1367 | if (val != oval) { | |
1368 | set_cur_ctl_value(cval, 0, val); | |
1369 | return 1; | |
1370 | } | |
1371 | return 0; | |
1372 | } | |
1373 | ||
1374 | /* alsa control interface for selector unit */ | |
1375 | static snd_kcontrol_new_t mixer_selectunit_ctl = { | |
1376 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1377 | .name = "", /* will be filled later */ | |
1378 | .info = mixer_ctl_selector_info, | |
1379 | .get = mixer_ctl_selector_get, | |
1380 | .put = mixer_ctl_selector_put, | |
1381 | }; | |
1382 | ||
1383 | ||
1384 | /* private free callback. | |
1385 | * free both private_data and private_value | |
1386 | */ | |
1387 | static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl) | |
1388 | { | |
1389 | int i, num_ins = 0; | |
1390 | ||
1391 | if (kctl->private_data) { | |
1392 | usb_mixer_elem_info_t *cval = kctl->private_data; | |
1393 | num_ins = cval->max; | |
1394 | kfree(cval); | |
1395 | kctl->private_data = NULL; | |
1396 | } | |
1397 | if (kctl->private_value) { | |
1398 | char **itemlist = (char **)kctl->private_value; | |
1399 | for (i = 0; i < num_ins; i++) | |
1400 | kfree(itemlist[i]); | |
1401 | kfree(itemlist); | |
1402 | kctl->private_value = 0; | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * parse a selector unit | |
1408 | */ | |
1409 | static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc) | |
1410 | { | |
1411 | unsigned int num_ins = desc[4]; | |
1412 | unsigned int i, nameid, len; | |
1413 | int err; | |
1414 | usb_mixer_elem_info_t *cval; | |
1415 | snd_kcontrol_t *kctl; | |
1416 | char **namelist; | |
1417 | ||
1418 | if (! num_ins || desc[0] < 6 + num_ins) { | |
1419 | snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid); | |
1420 | return -EINVAL; | |
1421 | } | |
1422 | ||
1423 | for (i = 0; i < num_ins; i++) { | |
1424 | if ((err = parse_audio_unit(state, desc[5 + i])) < 0) | |
1425 | return err; | |
1426 | } | |
1427 | ||
1428 | if (num_ins == 1) /* only one ? nonsense! */ | |
1429 | return 0; | |
1430 | ||
1431 | if (check_ignored_ctl(state, unitid, 0)) | |
1432 | return 0; | |
1433 | ||
1434 | cval = kcalloc(1, sizeof(*cval), GFP_KERNEL); | |
1435 | if (! cval) { | |
1436 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1437 | return -ENOMEM; | |
1438 | } | |
84957a8a | 1439 | cval->mixer = state->mixer; |
1da177e4 LT |
1440 | cval->id = unitid; |
1441 | cval->val_type = USB_MIXER_U8; | |
1442 | cval->channels = 1; | |
1443 | cval->min = 1; | |
1444 | cval->max = num_ins; | |
1445 | cval->res = 1; | |
1446 | cval->initialized = 1; | |
1447 | ||
1448 | namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL); | |
1449 | if (! namelist) { | |
1450 | snd_printk(KERN_ERR "cannot malloc\n"); | |
1451 | kfree(cval); | |
1452 | return -ENOMEM; | |
1453 | } | |
1454 | #define MAX_ITEM_NAME_LEN 64 | |
1455 | for (i = 0; i < num_ins; i++) { | |
1456 | usb_audio_term_t iterm; | |
1457 | len = 0; | |
1458 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); | |
1459 | if (! namelist[i]) { | |
1460 | snd_printk(KERN_ERR "cannot malloc\n"); | |
1461 | while (--i > 0) | |
1462 | kfree(namelist[i]); | |
1463 | kfree(namelist); | |
1464 | kfree(cval); | |
1465 | return -ENOMEM; | |
1466 | } | |
8e062ec7 CL |
1467 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
1468 | MAX_ITEM_NAME_LEN); | |
1469 | if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0) | |
1da177e4 LT |
1470 | len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0); |
1471 | if (! len) | |
1472 | sprintf(namelist[i], "Input %d", i); | |
1473 | } | |
1474 | ||
1475 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); | |
1476 | if (! kctl) { | |
1477 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); | |
1478 | kfree(cval); | |
1479 | return -ENOMEM; | |
1480 | } | |
1481 | kctl->private_value = (unsigned long)namelist; | |
1482 | kctl->private_free = usb_mixer_selector_elem_free; | |
1483 | ||
1484 | nameid = desc[desc[0] - 1]; | |
1485 | len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name)); | |
1486 | if (len) | |
1487 | ; | |
1488 | else if (nameid) | |
1489 | snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); | |
1490 | else { | |
1491 | len = get_term_name(state, &state->oterm, | |
1492 | kctl->id.name, sizeof(kctl->id.name), 0); | |
1493 | if (! len) | |
1494 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); | |
1495 | ||
1496 | if ((state->oterm.type & 0xff00) == 0x0100) | |
1497 | strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name)); | |
1498 | else | |
1499 | strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name)); | |
1500 | } | |
1501 | ||
1502 | snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n", | |
1503 | cval->id, kctl->id.name, num_ins); | |
84957a8a | 1504 | if ((err = add_control_to_empty(state, kctl)) < 0) |
1da177e4 LT |
1505 | return err; |
1506 | ||
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | ||
1511 | /* | |
1512 | * parse an audio unit recursively | |
1513 | */ | |
1514 | ||
1515 | static int parse_audio_unit(mixer_build_t *state, int unitid) | |
1516 | { | |
1517 | unsigned char *p1; | |
1518 | ||
1519 | if (test_and_set_bit(unitid, state->unitbitmap)) | |
1520 | return 0; /* the unit already visited */ | |
1521 | ||
1522 | p1 = find_audio_control_unit(state, unitid); | |
1523 | if (!p1) { | |
1524 | snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid); | |
1525 | return -EINVAL; | |
1526 | } | |
1527 | ||
1528 | switch (p1[2]) { | |
1529 | case INPUT_TERMINAL: | |
1530 | return 0; /* NOP */ | |
1531 | case MIXER_UNIT: | |
1532 | return parse_audio_mixer_unit(state, unitid, p1); | |
1533 | case SELECTOR_UNIT: | |
1534 | return parse_audio_selector_unit(state, unitid, p1); | |
1535 | case FEATURE_UNIT: | |
1536 | return parse_audio_feature_unit(state, unitid, p1); | |
1537 | case PROCESSING_UNIT: | |
1538 | return parse_audio_processing_unit(state, unitid, p1); | |
1539 | case EXTENSION_UNIT: | |
1540 | return parse_audio_extension_unit(state, unitid, p1); | |
1541 | default: | |
1542 | snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]); | |
1543 | return -EINVAL; | |
1544 | } | |
1545 | } | |
1546 | ||
84957a8a CL |
1547 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
1548 | { | |
6639b6c2 CL |
1549 | kfree(mixer->id_elems); |
1550 | if (mixer->urb) { | |
1551 | kfree(mixer->urb->transfer_buffer); | |
1552 | usb_free_urb(mixer->urb); | |
1553 | } | |
b259b10c CL |
1554 | if (mixer->rc_urb) |
1555 | usb_free_urb(mixer->rc_urb); | |
1556 | kfree(mixer->rc_setup_packet); | |
84957a8a CL |
1557 | kfree(mixer); |
1558 | } | |
1559 | ||
1560 | static int snd_usb_mixer_dev_free(snd_device_t *device) | |
1561 | { | |
1562 | struct usb_mixer_interface *mixer = device->device_data; | |
1563 | snd_usb_mixer_free(mixer); | |
1564 | return 0; | |
1565 | } | |
1566 | ||
1da177e4 LT |
1567 | /* |
1568 | * create mixer controls | |
1569 | * | |
1570 | * walk through all OUTPUT_TERMINAL descriptors to search for mixers | |
1571 | */ | |
84957a8a | 1572 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
1da177e4 LT |
1573 | { |
1574 | unsigned char *desc; | |
1575 | mixer_build_t state; | |
1576 | int err; | |
1577 | const struct usbmix_ctl_map *map; | |
84957a8a | 1578 | struct usb_host_interface *hostif; |
1da177e4 | 1579 | |
84957a8a | 1580 | hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0]; |
1da177e4 | 1581 | memset(&state, 0, sizeof(state)); |
84957a8a CL |
1582 | state.chip = mixer->chip; |
1583 | state.mixer = mixer; | |
1da177e4 LT |
1584 | state.buffer = hostif->extra; |
1585 | state.buflen = hostif->extralen; | |
1da177e4 LT |
1586 | |
1587 | /* check the mapping table */ | |
27d10f56 CL |
1588 | for (map = usbmix_ctl_maps; map->id; map++) { |
1589 | if (map->id == state.chip->usb_id) { | |
1da177e4 | 1590 | state.map = map->map; |
8e062ec7 | 1591 | state.selector_map = map->selector_map; |
84957a8a | 1592 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
1da177e4 LT |
1593 | break; |
1594 | } | |
1595 | } | |
1da177e4 LT |
1596 | |
1597 | desc = NULL; | |
1598 | while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) { | |
1599 | if (desc[0] < 9) | |
1600 | continue; /* invalid descriptor? */ | |
1601 | set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */ | |
1602 | state.oterm.id = desc[3]; | |
1603 | state.oterm.type = combine_word(&desc[4]); | |
1604 | state.oterm.name = desc[8]; | |
1605 | err = parse_audio_unit(&state, desc[7]); | |
1606 | if (err < 0) | |
1607 | return err; | |
1608 | } | |
1609 | return 0; | |
1610 | } | |
84957a8a | 1611 | |
6639b6c2 CL |
1612 | static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, |
1613 | int unitid) | |
1614 | { | |
1615 | usb_mixer_elem_info_t *info; | |
1616 | ||
1617 | for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) | |
1618 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
1619 | info->elem_id); | |
1620 | } | |
1621 | ||
b259b10c CL |
1622 | static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer, |
1623 | int unitid) | |
1624 | { | |
1625 | /* SB remote control */ | |
1626 | if (mixer->rc_type != RC_NONE && unitid == 0) { | |
1627 | /* read control code from device memory */ | |
1628 | mixer->rc_urb->dev = mixer->chip->dev; | |
1629 | usb_submit_urb(mixer->rc_urb, GFP_ATOMIC); | |
1630 | } | |
1631 | } | |
1632 | ||
6639b6c2 CL |
1633 | static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs) |
1634 | { | |
1635 | struct usb_mixer_interface *mixer = urb->context; | |
1636 | ||
1637 | if (urb->status == 0) { | |
1638 | u8 *buf = urb->transfer_buffer; | |
1639 | int i; | |
1640 | ||
1641 | for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) { | |
1642 | snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n", | |
1643 | buf[0], buf[1]); | |
1644 | /* ignore any notifications not from the control interface */ | |
1645 | if ((buf[0] & 0x0f) != 0) | |
1646 | continue; | |
1647 | if (!(buf[0] & 0x40)) | |
1648 | snd_usb_mixer_notify_id(mixer, buf[1]); | |
b259b10c CL |
1649 | else |
1650 | snd_usb_mixer_memory_change(mixer, buf[1]); | |
6639b6c2 CL |
1651 | } |
1652 | } | |
1653 | if (urb->status != -ENOENT && urb->status != -ECONNRESET) { | |
1654 | urb->dev = mixer->chip->dev; | |
1655 | usb_submit_urb(urb, GFP_ATOMIC); | |
1656 | } | |
1657 | } | |
1658 | ||
1659 | /* create the handler for the optional status interrupt endpoint */ | |
1660 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) | |
1661 | { | |
1662 | struct usb_host_interface *hostif; | |
1663 | struct usb_endpoint_descriptor *ep; | |
1664 | void *transfer_buffer; | |
1665 | int buffer_length; | |
1666 | unsigned int epnum; | |
1667 | ||
1668 | hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0]; | |
1669 | /* we need one interrupt input endpoint */ | |
1670 | if (get_iface_desc(hostif)->bNumEndpoints < 1) | |
1671 | return 0; | |
1672 | ep = get_endpoint(hostif, 0); | |
1673 | if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN || | |
1674 | (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1675 | return 0; | |
1676 | ||
1677 | epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1678 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); | |
1679 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); | |
1680 | if (!transfer_buffer) | |
1681 | return -ENOMEM; | |
1682 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); | |
1683 | if (!mixer->urb) { | |
1684 | kfree(transfer_buffer); | |
1685 | return -ENOMEM; | |
1686 | } | |
1687 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, | |
1688 | usb_rcvintpipe(mixer->chip->dev, epnum), | |
1689 | transfer_buffer, buffer_length, | |
1690 | snd_usb_mixer_status_complete, mixer, ep->bInterval); | |
1691 | usb_submit_urb(mixer->urb, GFP_KERNEL); | |
1692 | return 0; | |
1693 | } | |
1694 | ||
b259b10c CL |
1695 | static void snd_usb_soundblaster_remote_complete(struct urb *urb, |
1696 | struct pt_regs *regs) | |
1697 | { | |
1698 | struct usb_mixer_interface *mixer = urb->context; | |
1699 | /* | |
1700 | * format of remote control data: | |
1701 | * Extigy: xx 00 | |
1702 | * Audigy 2 NX: 06 80 xx 00 00 00 | |
1703 | */ | |
1704 | int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2; | |
1705 | u32 code; | |
1706 | ||
1707 | if (urb->status < 0 || urb->actual_length <= offset) | |
1708 | return; | |
1709 | code = mixer->rc_buffer[offset]; | |
1710 | /* the Mute button actually changes the mixer control */ | |
1711 | if (code == 13) | |
1712 | snd_usb_mixer_notify_id(mixer, 18); | |
1713 | mixer->rc_code = code; | |
1714 | wmb(); | |
1715 | wake_up(&mixer->rc_waitq); | |
1716 | } | |
1717 | ||
1718 | static int snd_usb_sbrc_hwdep_open(snd_hwdep_t *hw, struct file *file) | |
1719 | { | |
1720 | struct usb_mixer_interface *mixer = hw->private_data; | |
1721 | ||
1722 | if (test_and_set_bit(0, &mixer->rc_hwdep_open)) | |
1723 | return -EBUSY; | |
1724 | return 0; | |
1725 | } | |
1726 | ||
1727 | static int snd_usb_sbrc_hwdep_release(snd_hwdep_t *hw, struct file *file) | |
1728 | { | |
1729 | struct usb_mixer_interface *mixer = hw->private_data; | |
1730 | ||
1731 | clear_bit(0, &mixer->rc_hwdep_open); | |
1732 | smp_mb__after_clear_bit(); | |
1733 | return 0; | |
1734 | } | |
1735 | ||
1736 | static long snd_usb_sbrc_hwdep_read(snd_hwdep_t *hw, char __user *buf, | |
1737 | long count, loff_t *offset) | |
1738 | { | |
1739 | struct usb_mixer_interface *mixer = hw->private_data; | |
1740 | int err; | |
1741 | u32 rc_code; | |
1742 | ||
434b7f56 | 1743 | if (count != 1 && count != 4) |
b259b10c CL |
1744 | return -EINVAL; |
1745 | err = wait_event_interruptible(mixer->rc_waitq, | |
1746 | (rc_code = xchg(&mixer->rc_code, 0)) != 0); | |
1747 | if (err == 0) { | |
434b7f56 CL |
1748 | if (count == 1) |
1749 | err = put_user(rc_code, buf); | |
1750 | else | |
1751 | err = put_user(rc_code, (u32 __user *)buf); | |
b259b10c CL |
1752 | } |
1753 | return err < 0 ? err : count; | |
1754 | } | |
1755 | ||
1756 | static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t *hw, struct file *file, | |
1757 | poll_table *wait) | |
1758 | { | |
1759 | struct usb_mixer_interface *mixer = hw->private_data; | |
1760 | ||
1761 | poll_wait(file, &mixer->rc_waitq, wait); | |
1762 | return mixer->rc_code ? POLLIN | POLLRDNORM : 0; | |
1763 | } | |
1764 | ||
1765 | static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer) | |
1766 | { | |
1767 | snd_hwdep_t *hwdep; | |
1768 | int err, len; | |
1769 | ||
27d10f56 CL |
1770 | switch (mixer->chip->usb_id) { |
1771 | case USB_ID(0x041e, 0x3000): | |
b259b10c CL |
1772 | mixer->rc_type = RC_EXTIGY; |
1773 | len = 2; | |
1774 | break; | |
27d10f56 | 1775 | case USB_ID(0x041e, 0x3020): |
b259b10c CL |
1776 | mixer->rc_type = RC_AUDIGY2NX; |
1777 | len = 6; | |
1778 | break; | |
1779 | default: | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | init_waitqueue_head(&mixer->rc_waitq); | |
1784 | err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep); | |
1785 | if (err < 0) | |
1786 | return err; | |
1787 | snprintf(hwdep->name, sizeof(hwdep->name), | |
1788 | "%s remote control", mixer->chip->card->shortname); | |
1789 | hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC; | |
1790 | hwdep->private_data = mixer; | |
1791 | hwdep->ops.read = snd_usb_sbrc_hwdep_read; | |
1792 | hwdep->ops.open = snd_usb_sbrc_hwdep_open; | |
1793 | hwdep->ops.release = snd_usb_sbrc_hwdep_release; | |
1794 | hwdep->ops.poll = snd_usb_sbrc_hwdep_poll; | |
1795 | ||
1796 | mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL); | |
1797 | if (!mixer->rc_urb) | |
1798 | return -ENOMEM; | |
1799 | mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL); | |
1800 | if (!mixer->rc_setup_packet) { | |
1801 | usb_free_urb(mixer->rc_urb); | |
1802 | mixer->rc_urb = NULL; | |
1803 | return -ENOMEM; | |
1804 | } | |
1805 | mixer->rc_setup_packet->bRequestType = | |
1806 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; | |
1807 | mixer->rc_setup_packet->bRequest = GET_MEM; | |
1808 | mixer->rc_setup_packet->wValue = cpu_to_le16(0); | |
1809 | mixer->rc_setup_packet->wIndex = cpu_to_le16(0); | |
1810 | mixer->rc_setup_packet->wLength = cpu_to_le16(len); | |
1811 | usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev, | |
1812 | usb_rcvctrlpipe(mixer->chip->dev, 0), | |
1813 | (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len, | |
1814 | snd_usb_soundblaster_remote_complete, mixer); | |
1815 | return 0; | |
1816 | } | |
1817 | ||
93446edc CL |
1818 | static int snd_audigy2nx_led_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo) |
1819 | { | |
1820 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1821 | uinfo->count = 1; | |
1822 | uinfo->value.integer.min = 0; | |
1823 | uinfo->value.integer.max = 1; | |
1824 | return 0; | |
1825 | } | |
1826 | ||
1827 | static int snd_audigy2nx_led_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1828 | { | |
1829 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); | |
1830 | int index = kcontrol->private_value; | |
1831 | ||
1832 | ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index]; | |
1833 | return 0; | |
1834 | } | |
1835 | ||
1836 | static int snd_audigy2nx_led_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol) | |
1837 | { | |
1838 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); | |
1839 | int index = kcontrol->private_value; | |
1840 | int value = ucontrol->value.integer.value[0]; | |
1841 | int err, changed; | |
1842 | ||
1843 | if (value > 1) | |
1844 | return -EINVAL; | |
1845 | changed = value != mixer->audigy2nx_leds[index]; | |
1846 | err = snd_usb_ctl_msg(mixer->chip->dev, | |
1847 | usb_sndctrlpipe(mixer->chip->dev, 0), 0x24, | |
1848 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, | |
1849 | value, index + 2, NULL, 0, 100); | |
1850 | if (err < 0) | |
1851 | return err; | |
1852 | mixer->audigy2nx_leds[index] = value; | |
1853 | return changed; | |
1854 | } | |
1855 | ||
1856 | static snd_kcontrol_new_t snd_audigy2nx_controls[] = { | |
1857 | { | |
1858 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1859 | .name = "CMSS LED Switch", | |
1860 | .info = snd_audigy2nx_led_info, | |
1861 | .get = snd_audigy2nx_led_get, | |
1862 | .put = snd_audigy2nx_led_put, | |
1863 | .private_value = 0, | |
1864 | }, | |
1865 | { | |
1866 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1867 | .name = "Power LED Switch", | |
1868 | .info = snd_audigy2nx_led_info, | |
1869 | .get = snd_audigy2nx_led_get, | |
1870 | .put = snd_audigy2nx_led_put, | |
1871 | .private_value = 1, | |
1872 | }, | |
1873 | { | |
1874 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1875 | .name = "Dolby Digital LED Switch", | |
1876 | .info = snd_audigy2nx_led_info, | |
1877 | .get = snd_audigy2nx_led_get, | |
1878 | .put = snd_audigy2nx_led_put, | |
1879 | .private_value = 2, | |
1880 | }, | |
1881 | }; | |
1882 | ||
1883 | static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer) | |
1884 | { | |
1885 | int i, err; | |
1886 | ||
1887 | for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) { | |
1888 | err = snd_ctl_add(mixer->chip->card, | |
1889 | snd_ctl_new1(&snd_audigy2nx_controls[i], mixer)); | |
1890 | if (err < 0) | |
1891 | return err; | |
1892 | } | |
1893 | mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */ | |
1894 | return 0; | |
1895 | } | |
1896 | ||
84957a8a CL |
1897 | int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif) |
1898 | { | |
1899 | static snd_device_ops_t dev_ops = { | |
1900 | .dev_free = snd_usb_mixer_dev_free | |
1901 | }; | |
1902 | struct usb_mixer_interface *mixer; | |
1903 | int err; | |
1904 | ||
1905 | strcpy(chip->card->mixername, "USB Mixer"); | |
1906 | ||
1907 | mixer = kcalloc(1, sizeof(*mixer), GFP_KERNEL); | |
1908 | if (!mixer) | |
1909 | return -ENOMEM; | |
1910 | mixer->chip = chip; | |
1911 | mixer->ctrlif = ctrlif; | |
1912 | #ifdef IGNORE_CTL_ERROR | |
1913 | mixer->ignore_ctl_error = 1; | |
1914 | #endif | |
6639b6c2 CL |
1915 | mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL); |
1916 | if (!mixer->id_elems) { | |
1917 | kfree(mixer); | |
1918 | return -ENOMEM; | |
1919 | } | |
84957a8a | 1920 | |
6639b6c2 | 1921 | if ((err = snd_usb_mixer_controls(mixer)) < 0 || |
93446edc CL |
1922 | (err = snd_usb_mixer_status_create(mixer)) < 0) |
1923 | goto _error; | |
84957a8a | 1924 | |
93446edc CL |
1925 | if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0) |
1926 | goto _error; | |
1927 | ||
1928 | if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) { | |
1929 | if ((err = snd_audigy2nx_controls_create(mixer)) < 0) | |
1930 | goto _error; | |
b259b10c CL |
1931 | } |
1932 | ||
84957a8a | 1933 | err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops); |
93446edc CL |
1934 | if (err < 0) |
1935 | goto _error; | |
84957a8a CL |
1936 | list_add(&mixer->list, &chip->mixer_list); |
1937 | return 0; | |
93446edc CL |
1938 | |
1939 | _error: | |
1940 | snd_usb_mixer_free(mixer); | |
1941 | return err; | |
84957a8a CL |
1942 | } |
1943 | ||
1944 | void snd_usb_mixer_disconnect(struct list_head *p) | |
1945 | { | |
6639b6c2 CL |
1946 | struct usb_mixer_interface *mixer; |
1947 | ||
1948 | mixer = list_entry(p, struct usb_mixer_interface, list); | |
1949 | if (mixer->urb) | |
1950 | usb_kill_urb(mixer->urb); | |
b259b10c CL |
1951 | if (mixer->rc_urb) |
1952 | usb_kill_urb(mixer->rc_urb); | |
84957a8a | 1953 | } |