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