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