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