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