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