Merge remote-tracking branch 'asoc/fix/omap' into asoc-linus
[platform/adaptation/renesas_rcar/renesas_kernel.git] / sound / usb / mixer_quirks.c
1 /*
2  *   USB Audio Driver for ALSA
3  *
4  *   Quirks and vendor-specific extensions for mixer interfaces
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  *   Audio Advantage Micro II support added by:
13  *          Przemek Rudy (prudy1@o2.pl)
14  *
15  *   This program is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU General Public License as published by
17  *   the Free Software Foundation; either version 2 of the License, or
18  *   (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU General Public License for more details.
24  *
25  *   You should have received a copy of the GNU General Public License
26  *   along with this program; if not, write to the Free Software
27  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  */
29
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/usb.h>
33 #include <linux/usb/audio.h>
34
35 #include <sound/asoundef.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/hwdep.h>
39 #include <sound/info.h>
40
41 #include "usbaudio.h"
42 #include "mixer.h"
43 #include "mixer_quirks.h"
44 #include "helper.h"
45
46 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
47
48 struct std_mono_table {
49         unsigned int unitid, control, cmask;
50         int val_type;
51         const char *name;
52         snd_kcontrol_tlv_rw_t *tlv_callback;
53 };
54
55 /* private_free callback */
56 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
57 {
58         kfree(kctl->private_data);
59         kctl->private_data = NULL;
60 }
61
62 /* This function allows for the creation of standard UAC controls.
63  * See the quirks for M-Audio FTUs or Ebox-44.
64  * If you don't want to set a TLV callback pass NULL.
65  *
66  * Since there doesn't seem to be a devices that needs a multichannel
67  * version, we keep it mono for simplicity.
68  */
69 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
70                                 unsigned int unitid,
71                                 unsigned int control,
72                                 unsigned int cmask,
73                                 int val_type,
74                                 unsigned int idx_off,
75                                 const char *name,
76                                 snd_kcontrol_tlv_rw_t *tlv_callback)
77 {
78         int err;
79         struct usb_mixer_elem_info *cval;
80         struct snd_kcontrol *kctl;
81
82         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
83         if (!cval)
84                 return -ENOMEM;
85
86         cval->id = unitid;
87         cval->mixer = mixer;
88         cval->val_type = val_type;
89         cval->channels = 1;
90         cval->control = control;
91         cval->cmask = cmask;
92         cval->idx_off = idx_off;
93
94         /* get_min_max() is called only for integer volumes later,
95          * so provide a short-cut for booleans */
96         cval->min = 0;
97         cval->max = 1;
98         cval->res = 0;
99         cval->dBmin = 0;
100         cval->dBmax = 0;
101
102         /* Create control */
103         kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
104         if (!kctl) {
105                 kfree(cval);
106                 return -ENOMEM;
107         }
108
109         /* Set name */
110         snprintf(kctl->id.name, sizeof(kctl->id.name), name);
111         kctl->private_free = usb_mixer_elem_free;
112
113         /* set TLV */
114         if (tlv_callback) {
115                 kctl->tlv.c = tlv_callback;
116                 kctl->vd[0].access |=
117                         SNDRV_CTL_ELEM_ACCESS_TLV_READ |
118                         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
119         }
120         /* Add control to mixer */
121         err = snd_usb_mixer_add_control(mixer, kctl);
122         if (err < 0)
123                 return err;
124
125         return 0;
126 }
127
128 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
129                                 unsigned int unitid,
130                                 unsigned int control,
131                                 unsigned int cmask,
132                                 int val_type,
133                                 const char *name,
134                                 snd_kcontrol_tlv_rw_t *tlv_callback)
135 {
136         return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
137                 val_type, 0 /* Offset */, name, tlv_callback);
138 }
139
140 /*
141  * Create a set of standard UAC controls from a table
142  */
143 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
144                                 struct std_mono_table *t)
145 {
146         int err;
147
148         while (t->name != NULL) {
149                 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
150                                 t->cmask, t->val_type, t->name, t->tlv_callback);
151                 if (err < 0)
152                         return err;
153                 t++;
154         }
155
156         return 0;
157 }
158
159 /*
160  * Sound Blaster remote control configuration
161  *
162  * format of remote control data:
163  * Extigy:       xx 00
164  * Audigy 2 NX:  06 80 xx 00 00 00
165  * Live! 24-bit: 06 80 xx yy 22 83
166  */
167 static const struct rc_config {
168         u32 usb_id;
169         u8  offset;
170         u8  length;
171         u8  packet_length;
172         u8  min_packet_length; /* minimum accepted length of the URB result */
173         u8  mute_mixer_id;
174         u32 mute_code;
175 } rc_configs[] = {
176         { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
177         { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
178         { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
179         { USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
180         { USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
181         { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
182 };
183
184 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
185 {
186         struct usb_mixer_interface *mixer = urb->context;
187         const struct rc_config *rc = mixer->rc_cfg;
188         u32 code;
189
190         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
191                 return;
192
193         code = mixer->rc_buffer[rc->offset];
194         if (rc->length == 2)
195                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
196
197         /* the Mute button actually changes the mixer control */
198         if (code == rc->mute_code)
199                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
200         mixer->rc_code = code;
201         wmb();
202         wake_up(&mixer->rc_waitq);
203 }
204
205 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
206                                      long count, loff_t *offset)
207 {
208         struct usb_mixer_interface *mixer = hw->private_data;
209         int err;
210         u32 rc_code;
211
212         if (count != 1 && count != 4)
213                 return -EINVAL;
214         err = wait_event_interruptible(mixer->rc_waitq,
215                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
216         if (err == 0) {
217                 if (count == 1)
218                         err = put_user(rc_code, buf);
219                 else
220                         err = put_user(rc_code, (u32 __user *)buf);
221         }
222         return err < 0 ? err : count;
223 }
224
225 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
226                                             poll_table *wait)
227 {
228         struct usb_mixer_interface *mixer = hw->private_data;
229
230         poll_wait(file, &mixer->rc_waitq, wait);
231         return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
232 }
233
234 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
235 {
236         struct snd_hwdep *hwdep;
237         int err, len, i;
238
239         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
240                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
241                         break;
242         if (i >= ARRAY_SIZE(rc_configs))
243                 return 0;
244         mixer->rc_cfg = &rc_configs[i];
245
246         len = mixer->rc_cfg->packet_length;
247
248         init_waitqueue_head(&mixer->rc_waitq);
249         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
250         if (err < 0)
251                 return err;
252         snprintf(hwdep->name, sizeof(hwdep->name),
253                  "%s remote control", mixer->chip->card->shortname);
254         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
255         hwdep->private_data = mixer;
256         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
257         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
258         hwdep->exclusive = 1;
259
260         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
261         if (!mixer->rc_urb)
262                 return -ENOMEM;
263         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
264         if (!mixer->rc_setup_packet) {
265                 usb_free_urb(mixer->rc_urb);
266                 mixer->rc_urb = NULL;
267                 return -ENOMEM;
268         }
269         mixer->rc_setup_packet->bRequestType =
270                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
271         mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
272         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
273         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
274         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
275         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
276                              usb_rcvctrlpipe(mixer->chip->dev, 0),
277                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
278                              snd_usb_soundblaster_remote_complete, mixer);
279         return 0;
280 }
281
282 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
283
284 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
285 {
286         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
287         int index = kcontrol->private_value;
288
289         ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
290         return 0;
291 }
292
293 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
294 {
295         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
296         int index = kcontrol->private_value;
297         int value = ucontrol->value.integer.value[0];
298         int err, changed;
299
300         if (value > 1)
301                 return -EINVAL;
302         changed = value != mixer->audigy2nx_leds[index];
303         down_read(&mixer->chip->shutdown_rwsem);
304         if (mixer->chip->shutdown) {
305                 err = -ENODEV;
306                 goto out;
307         }
308         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
309                 err = snd_usb_ctl_msg(mixer->chip->dev,
310                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
311                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
312                               !value, 0, NULL, 0);
313         /* USB X-Fi S51 Pro */
314         if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
315                 err = snd_usb_ctl_msg(mixer->chip->dev,
316                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
317                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
318                               !value, 0, NULL, 0);
319         else
320                 err = snd_usb_ctl_msg(mixer->chip->dev,
321                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
322                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
323                               value, index + 2, NULL, 0);
324  out:
325         up_read(&mixer->chip->shutdown_rwsem);
326         if (err < 0)
327                 return err;
328         mixer->audigy2nx_leds[index] = value;
329         return changed;
330 }
331
332 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
333         {
334                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
335                 .name = "CMSS LED Switch",
336                 .info = snd_audigy2nx_led_info,
337                 .get = snd_audigy2nx_led_get,
338                 .put = snd_audigy2nx_led_put,
339                 .private_value = 0,
340         },
341         {
342                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
343                 .name = "Power LED Switch",
344                 .info = snd_audigy2nx_led_info,
345                 .get = snd_audigy2nx_led_get,
346                 .put = snd_audigy2nx_led_put,
347                 .private_value = 1,
348         },
349         {
350                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
351                 .name = "Dolby Digital LED Switch",
352                 .info = snd_audigy2nx_led_info,
353                 .get = snd_audigy2nx_led_get,
354                 .put = snd_audigy2nx_led_put,
355                 .private_value = 2,
356         },
357 };
358
359 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
360 {
361         int i, err;
362
363         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
364                 /* USB X-Fi S51 doesn't have a CMSS LED */
365                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
366                         continue;
367                 /* USB X-Fi S51 Pro doesn't have one either */
368                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
369                         continue;
370                 if (i > 1 && /* Live24ext has 2 LEDs only */
371                         (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
372                          mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
373                          mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
374                          mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
375                         break; 
376                 err = snd_ctl_add(mixer->chip->card,
377                                   snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
378                 if (err < 0)
379                         return err;
380         }
381         mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
382         return 0;
383 }
384
385 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
386                                     struct snd_info_buffer *buffer)
387 {
388         static const struct sb_jack {
389                 int unitid;
390                 const char *name;
391         }  jacks_audigy2nx[] = {
392                 {4,  "dig in "},
393                 {7,  "line in"},
394                 {19, "spk out"},
395                 {20, "hph out"},
396                 {-1, NULL}
397         }, jacks_live24ext[] = {
398                 {4,  "line in"}, /* &1=Line, &2=Mic*/
399                 {3,  "hph out"}, /* headphones */
400                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
401                 {-1, NULL}
402         };
403         const struct sb_jack *jacks;
404         struct usb_mixer_interface *mixer = entry->private_data;
405         int i, err;
406         u8 buf[3];
407
408         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
409         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
410                 jacks = jacks_audigy2nx;
411         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
412                  mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
413                 jacks = jacks_live24ext;
414         else
415                 return;
416
417         for (i = 0; jacks[i].name; ++i) {
418                 snd_iprintf(buffer, "%s: ", jacks[i].name);
419                 down_read(&mixer->chip->shutdown_rwsem);
420                 if (mixer->chip->shutdown)
421                         err = 0;
422                 else
423                         err = snd_usb_ctl_msg(mixer->chip->dev,
424                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
425                                       UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
426                                       USB_RECIP_INTERFACE, 0,
427                                       jacks[i].unitid << 8, buf, 3);
428                 up_read(&mixer->chip->shutdown_rwsem);
429                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
430                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
431                 else
432                         snd_iprintf(buffer, "?\n");
433         }
434 }
435
436 /* ASUS Xonar U1 / U3 controls */
437
438 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
439                                    struct snd_ctl_elem_value *ucontrol)
440 {
441         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
442
443         ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
444         return 0;
445 }
446
447 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
448                                    struct snd_ctl_elem_value *ucontrol)
449 {
450         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
451         u8 old_status, new_status;
452         int err, changed;
453
454         old_status = mixer->xonar_u1_status;
455         if (ucontrol->value.integer.value[0])
456                 new_status = old_status | 0x02;
457         else
458                 new_status = old_status & ~0x02;
459         changed = new_status != old_status;
460         down_read(&mixer->chip->shutdown_rwsem);
461         if (mixer->chip->shutdown)
462                 err = -ENODEV;
463         else
464                 err = snd_usb_ctl_msg(mixer->chip->dev,
465                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
466                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
467                               50, 0, &new_status, 1);
468         up_read(&mixer->chip->shutdown_rwsem);
469         if (err < 0)
470                 return err;
471         mixer->xonar_u1_status = new_status;
472         return changed;
473 }
474
475 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
476         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
477         .name = "Digital Playback Switch",
478         .info = snd_ctl_boolean_mono_info,
479         .get = snd_xonar_u1_switch_get,
480         .put = snd_xonar_u1_switch_put,
481 };
482
483 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
484 {
485         int err;
486
487         err = snd_ctl_add(mixer->chip->card,
488                           snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
489         if (err < 0)
490                 return err;
491         mixer->xonar_u1_status = 0x05;
492         return 0;
493 }
494
495 /* Native Instruments device quirks */
496
497 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
498
499 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
500                                              struct snd_ctl_elem_value *ucontrol)
501 {
502         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
503         struct usb_device *dev = mixer->chip->dev;
504         u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
505         u16 wIndex = kcontrol->private_value & 0xffff;
506         u8 tmp;
507         int ret;
508
509         down_read(&mixer->chip->shutdown_rwsem);
510         if (mixer->chip->shutdown)
511                 ret = -ENODEV;
512         else
513                 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
514                                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
515                                   0, wIndex,
516                                   &tmp, sizeof(tmp), 1000);
517         up_read(&mixer->chip->shutdown_rwsem);
518
519         if (ret < 0) {
520                 snd_printk(KERN_ERR
521                            "unable to issue vendor read request (ret = %d)", ret);
522                 return ret;
523         }
524
525         ucontrol->value.integer.value[0] = tmp;
526
527         return 0;
528 }
529
530 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
531                                              struct snd_ctl_elem_value *ucontrol)
532 {
533         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
534         struct usb_device *dev = mixer->chip->dev;
535         u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
536         u16 wIndex = kcontrol->private_value & 0xffff;
537         u16 wValue = ucontrol->value.integer.value[0];
538         int ret;
539
540         down_read(&mixer->chip->shutdown_rwsem);
541         if (mixer->chip->shutdown)
542                 ret = -ENODEV;
543         else
544                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
545                                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
546                                   wValue, wIndex,
547                                   NULL, 0, 1000);
548         up_read(&mixer->chip->shutdown_rwsem);
549
550         if (ret < 0) {
551                 snd_printk(KERN_ERR
552                            "unable to issue vendor write request (ret = %d)", ret);
553                 return ret;
554         }
555
556         return 0;
557 }
558
559 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
560         {
561                 .name = "Direct Thru Channel A",
562                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
563         },
564         {
565                 .name = "Direct Thru Channel B",
566                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
567         },
568         {
569                 .name = "Phono Input Channel A",
570                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
571         },
572         {
573                 .name = "Phono Input Channel B",
574                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
575         },
576 };
577
578 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
579         {
580                 .name = "Direct Thru Channel A",
581                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
582         },
583         {
584                 .name = "Direct Thru Channel B",
585                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
586         },
587         {
588                 .name = "Direct Thru Channel C",
589                 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
590         },
591         {
592                 .name = "Direct Thru Channel D",
593                 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
594         },
595         {
596                 .name = "Phono Input Channel A",
597                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
598         },
599         {
600                 .name = "Phono Input Channel B",
601                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
602         },
603         {
604                 .name = "Phono Input Channel C",
605                 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
606         },
607         {
608                 .name = "Phono Input Channel D",
609                 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
610         },
611 };
612
613 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
614                                               const struct snd_kcontrol_new *kc,
615                                               unsigned int count)
616 {
617         int i, err = 0;
618         struct snd_kcontrol_new template = {
619                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
620                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
621                 .get = snd_nativeinstruments_control_get,
622                 .put = snd_nativeinstruments_control_put,
623                 .info = snd_ctl_boolean_mono_info,
624         };
625
626         for (i = 0; i < count; i++) {
627                 struct snd_kcontrol *c;
628
629                 template.name = kc[i].name;
630                 template.private_value = kc[i].private_value;
631
632                 c = snd_ctl_new1(&template, mixer);
633                 err = snd_ctl_add(mixer->chip->card, c);
634
635                 if (err < 0)
636                         break;
637         }
638
639         return err;
640 }
641
642 /* M-Audio FastTrack Ultra quirks */
643 /* FTU Effect switch (also used by C400/C600) */
644 struct snd_ftu_eff_switch_priv_val {
645         struct usb_mixer_interface *mixer;
646         int cached_value;
647         int is_cached;
648         int bUnitID;
649         int validx;
650 };
651
652 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
653                                         struct snd_ctl_elem_info *uinfo)
654 {
655         static const char *texts[8] = {"Room 1",
656                                        "Room 2",
657                                        "Room 3",
658                                        "Hall 1",
659                                        "Hall 2",
660                                        "Plate",
661                                        "Delay",
662                                        "Echo"
663         };
664
665         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
666         uinfo->count = 1;
667         uinfo->value.enumerated.items = 8;
668         if (uinfo->value.enumerated.item > 7)
669                 uinfo->value.enumerated.item = 7;
670         strcpy(uinfo->value.enumerated.name,
671                 texts[uinfo->value.enumerated.item]);
672
673         return 0;
674 }
675
676 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
677                                         struct snd_ctl_elem_value *ucontrol)
678 {
679         struct snd_usb_audio *chip;
680         struct usb_mixer_interface *mixer;
681         struct snd_ftu_eff_switch_priv_val *pval;
682         int err;
683         unsigned char value[2];
684         int id, validx;
685
686         const int val_len = 2;
687
688         value[0] = 0x00;
689         value[1] = 0x00;
690
691         pval = (struct snd_ftu_eff_switch_priv_val *)
692                 kctl->private_value;
693
694         if (pval->is_cached) {
695                 ucontrol->value.enumerated.item[0] = pval->cached_value;
696                 return 0;
697         }
698
699         mixer = (struct usb_mixer_interface *) pval->mixer;
700         if (snd_BUG_ON(!mixer))
701                 return -EINVAL;
702
703         chip = (struct snd_usb_audio *) mixer->chip;
704         if (snd_BUG_ON(!chip))
705                 return -EINVAL;
706
707         id = pval->bUnitID;
708         validx = pval->validx;
709
710         down_read(&mixer->chip->shutdown_rwsem);
711         if (mixer->chip->shutdown)
712                 err = -ENODEV;
713         else
714                 err = snd_usb_ctl_msg(chip->dev,
715                         usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
716                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
717                         validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
718                         value, val_len);
719         up_read(&mixer->chip->shutdown_rwsem);
720         if (err < 0)
721                 return err;
722
723         ucontrol->value.enumerated.item[0] = value[0];
724         pval->cached_value = value[0];
725         pval->is_cached = 1;
726
727         return 0;
728 }
729
730 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
731                                         struct snd_ctl_elem_value *ucontrol)
732 {
733         struct snd_usb_audio *chip;
734         struct snd_ftu_eff_switch_priv_val *pval;
735
736         struct usb_mixer_interface *mixer;
737         int changed, cur_val, err, new_val;
738         unsigned char value[2];
739         int id, validx;
740
741         const int val_len = 2;
742
743         changed = 0;
744
745         pval = (struct snd_ftu_eff_switch_priv_val *)
746                 kctl->private_value;
747         cur_val = pval->cached_value;
748         new_val = ucontrol->value.enumerated.item[0];
749
750         mixer = (struct usb_mixer_interface *) pval->mixer;
751         if (snd_BUG_ON(!mixer))
752                 return -EINVAL;
753
754         chip = (struct snd_usb_audio *) mixer->chip;
755         if (snd_BUG_ON(!chip))
756                 return -EINVAL;
757
758         id = pval->bUnitID;
759         validx = pval->validx;
760
761         if (!pval->is_cached) {
762                 /* Read current value */
763                 down_read(&mixer->chip->shutdown_rwsem);
764                 if (mixer->chip->shutdown)
765                         err = -ENODEV;
766                 else
767                         err = snd_usb_ctl_msg(chip->dev,
768                                 usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
769                                 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
770                                 validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
771                                 value, val_len);
772                 up_read(&mixer->chip->shutdown_rwsem);
773                 if (err < 0)
774                         return err;
775
776                 cur_val = value[0];
777                 pval->cached_value = cur_val;
778                 pval->is_cached = 1;
779         }
780         /* update value if needed */
781         if (cur_val != new_val) {
782                 value[0] = new_val;
783                 value[1] = 0;
784                 down_read(&mixer->chip->shutdown_rwsem);
785                 if (mixer->chip->shutdown)
786                         err = -ENODEV;
787                 else
788                         err = snd_usb_ctl_msg(chip->dev,
789                                 usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
790                                 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
791                                 validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
792                                 value, val_len);
793                 up_read(&mixer->chip->shutdown_rwsem);
794                 if (err < 0)
795                         return err;
796
797                 pval->cached_value = new_val;
798                 pval->is_cached = 1;
799                 changed = 1;
800         }
801
802         return changed;
803 }
804
805 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
806         int validx, int bUnitID)
807 {
808         static struct snd_kcontrol_new template = {
809                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
810                 .name = "Effect Program Switch",
811                 .index = 0,
812                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
813                 .info = snd_ftu_eff_switch_info,
814                 .get = snd_ftu_eff_switch_get,
815                 .put = snd_ftu_eff_switch_put
816         };
817
818         int err;
819         struct snd_kcontrol *kctl;
820         struct snd_ftu_eff_switch_priv_val *pval;
821
822         pval = kzalloc(sizeof(*pval), GFP_KERNEL);
823         if (!pval)
824                 return -ENOMEM;
825
826         pval->cached_value = 0;
827         pval->is_cached = 0;
828         pval->mixer = mixer;
829         pval->bUnitID = bUnitID;
830         pval->validx = validx;
831
832         template.private_value = (unsigned long) pval;
833         kctl = snd_ctl_new1(&template, mixer->chip);
834         if (!kctl) {
835                 kfree(pval);
836                 return -ENOMEM;
837         }
838
839         err = snd_ctl_add(mixer->chip->card, kctl);
840         if (err < 0)
841                 return err;
842
843         return 0;
844 }
845
846 /* Create volume controls for FTU devices*/
847 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
848 {
849         char name[64];
850         unsigned int control, cmask;
851         int in, out, err;
852
853         const unsigned int id = 5;
854         const int val_type = USB_MIXER_S16;
855
856         for (out = 0; out < 8; out++) {
857                 control = out + 1;
858                 for (in = 0; in < 8; in++) {
859                         cmask = 1 << in;
860                         snprintf(name, sizeof(name),
861                                 "AIn%d - Out%d Capture Volume",
862                                 in  + 1, out + 1);
863                         err = snd_create_std_mono_ctl(mixer, id, control,
864                                                         cmask, val_type, name,
865                                                         &snd_usb_mixer_vol_tlv);
866                         if (err < 0)
867                                 return err;
868                 }
869                 for (in = 8; in < 16; in++) {
870                         cmask = 1 << in;
871                         snprintf(name, sizeof(name),
872                                 "DIn%d - Out%d Playback Volume",
873                                 in - 7, out + 1);
874                         err = snd_create_std_mono_ctl(mixer, id, control,
875                                                         cmask, val_type, name,
876                                                         &snd_usb_mixer_vol_tlv);
877                         if (err < 0)
878                                 return err;
879                 }
880         }
881
882         return 0;
883 }
884
885 /* This control needs a volume quirk, see mixer.c */
886 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
887 {
888         static const char name[] = "Effect Volume";
889         const unsigned int id = 6;
890         const int val_type = USB_MIXER_U8;
891         const unsigned int control = 2;
892         const unsigned int cmask = 0;
893
894         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
895                                         name, snd_usb_mixer_vol_tlv);
896 }
897
898 /* This control needs a volume quirk, see mixer.c */
899 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
900 {
901         static const char name[] = "Effect Duration";
902         const unsigned int id = 6;
903         const int val_type = USB_MIXER_S16;
904         const unsigned int control = 3;
905         const unsigned int cmask = 0;
906
907         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
908                                         name, snd_usb_mixer_vol_tlv);
909 }
910
911 /* This control needs a volume quirk, see mixer.c */
912 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
913 {
914         static const char name[] = "Effect Feedback Volume";
915         const unsigned int id = 6;
916         const int val_type = USB_MIXER_U8;
917         const unsigned int control = 4;
918         const unsigned int cmask = 0;
919
920         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
921                                         name, NULL);
922 }
923
924 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
925 {
926         unsigned int cmask;
927         int err, ch;
928         char name[48];
929
930         const unsigned int id = 7;
931         const int val_type = USB_MIXER_S16;
932         const unsigned int control = 7;
933
934         for (ch = 0; ch < 4; ++ch) {
935                 cmask = 1 << ch;
936                 snprintf(name, sizeof(name),
937                         "Effect Return %d Volume", ch + 1);
938                 err = snd_create_std_mono_ctl(mixer, id, control,
939                                                 cmask, val_type, name,
940                                                 snd_usb_mixer_vol_tlv);
941                 if (err < 0)
942                         return err;
943         }
944
945         return 0;
946 }
947
948 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
949 {
950         unsigned int  cmask;
951         int err, ch;
952         char name[48];
953
954         const unsigned int id = 5;
955         const int val_type = USB_MIXER_S16;
956         const unsigned int control = 9;
957
958         for (ch = 0; ch < 8; ++ch) {
959                 cmask = 1 << ch;
960                 snprintf(name, sizeof(name),
961                         "Effect Send AIn%d Volume", ch + 1);
962                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
963                                                 val_type, name,
964                                                 snd_usb_mixer_vol_tlv);
965                 if (err < 0)
966                         return err;
967         }
968         for (ch = 8; ch < 16; ++ch) {
969                 cmask = 1 << ch;
970                 snprintf(name, sizeof(name),
971                         "Effect Send DIn%d Volume", ch - 7);
972                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
973                                                 val_type, name,
974                                                 snd_usb_mixer_vol_tlv);
975                 if (err < 0)
976                         return err;
977         }
978         return 0;
979 }
980
981 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
982 {
983         int err;
984
985         err = snd_ftu_create_volume_ctls(mixer);
986         if (err < 0)
987                 return err;
988
989         err = snd_ftu_create_effect_switch(mixer, 1, 6);
990         if (err < 0)
991                 return err;
992
993         err = snd_ftu_create_effect_volume_ctl(mixer);
994         if (err < 0)
995                 return err;
996
997         err = snd_ftu_create_effect_duration_ctl(mixer);
998         if (err < 0)
999                 return err;
1000
1001         err = snd_ftu_create_effect_feedback_ctl(mixer);
1002         if (err < 0)
1003                 return err;
1004
1005         err = snd_ftu_create_effect_return_ctls(mixer);
1006         if (err < 0)
1007                 return err;
1008
1009         err = snd_ftu_create_effect_send_ctls(mixer);
1010         if (err < 0)
1011                 return err;
1012
1013         return 0;
1014 }
1015
1016 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1017                                unsigned char samplerate_id)
1018 {
1019         struct usb_mixer_interface *mixer;
1020         struct usb_mixer_elem_info *cval;
1021         int unitid = 12; /* SamleRate ExtensionUnit ID */
1022
1023         list_for_each_entry(mixer, &chip->mixer_list, list) {
1024                 cval = mixer->id_elems[unitid];
1025                 if (cval) {
1026                         snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1027                                                     cval->control << 8,
1028                                                     samplerate_id);
1029                         snd_usb_mixer_notify_id(mixer, unitid);
1030                 }
1031                 break;
1032         }
1033 }
1034
1035 /* M-Audio Fast Track C400/C600 */
1036 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1037 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1038 {
1039         char name[64];
1040         unsigned int cmask, offset;
1041         int out, chan, err;
1042         int num_outs = 0;
1043         int num_ins = 0;
1044
1045         const unsigned int id = 0x40;
1046         const int val_type = USB_MIXER_S16;
1047         const int control = 1;
1048
1049         switch (mixer->chip->usb_id) {
1050         case USB_ID(0x0763, 0x2030):
1051                 num_outs = 6;
1052                 num_ins = 4;
1053                 break;
1054         case USB_ID(0x0763, 0x2031):
1055                 num_outs = 8;
1056                 num_ins = 6;
1057                 break;
1058         }
1059
1060         for (chan = 0; chan < num_outs + num_ins; chan++) {
1061                 for (out = 0; out < num_outs; out++) {
1062                         if (chan < num_outs) {
1063                                 snprintf(name, sizeof(name),
1064                                         "PCM%d-Out%d Playback Volume",
1065                                         chan + 1, out + 1);
1066                         } else {
1067                                 snprintf(name, sizeof(name),
1068                                         "In%d-Out%d Playback Volume",
1069                                         chan - num_outs + 1, out + 1);
1070                         }
1071
1072                         cmask = (out == 0) ? 0 : 1 << (out - 1);
1073                         offset = chan * num_outs;
1074                         err = snd_create_std_mono_ctl_offset(mixer, id, control,
1075                                                 cmask, val_type, offset, name,
1076                                                 &snd_usb_mixer_vol_tlv);
1077                         if (err < 0)
1078                                 return err;
1079                 }
1080         }
1081
1082         return 0;
1083 }
1084
1085 /* This control needs a volume quirk, see mixer.c */
1086 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1087 {
1088         static const char name[] = "Effect Volume";
1089         const unsigned int id = 0x43;
1090         const int val_type = USB_MIXER_U8;
1091         const unsigned int control = 3;
1092         const unsigned int cmask = 0;
1093
1094         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1095                                         name, snd_usb_mixer_vol_tlv);
1096 }
1097
1098 /* This control needs a volume quirk, see mixer.c */
1099 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1100 {
1101         static const char name[] = "Effect Duration";
1102         const unsigned int id = 0x43;
1103         const int val_type = USB_MIXER_S16;
1104         const unsigned int control = 4;
1105         const unsigned int cmask = 0;
1106
1107         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1108                                         name, snd_usb_mixer_vol_tlv);
1109 }
1110
1111 /* This control needs a volume quirk, see mixer.c */
1112 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1113 {
1114         static const char name[] = "Effect Feedback Volume";
1115         const unsigned int id = 0x43;
1116         const int val_type = USB_MIXER_U8;
1117         const unsigned int control = 5;
1118         const unsigned int cmask = 0;
1119
1120         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1121                                         name, NULL);
1122 }
1123
1124 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1125 {
1126         char name[64];
1127         unsigned int cmask;
1128         int chan, err;
1129         int num_outs = 0;
1130         int num_ins = 0;
1131
1132         const unsigned int id = 0x42;
1133         const int val_type = USB_MIXER_S16;
1134         const int control = 1;
1135
1136         switch (mixer->chip->usb_id) {
1137         case USB_ID(0x0763, 0x2030):
1138                 num_outs = 6;
1139                 num_ins = 4;
1140                 break;
1141         case USB_ID(0x0763, 0x2031):
1142                 num_outs = 8;
1143                 num_ins = 6;
1144                 break;
1145         }
1146
1147         for (chan = 0; chan < num_outs + num_ins; chan++) {
1148                 if (chan < num_outs) {
1149                         snprintf(name, sizeof(name),
1150                                 "Effect Send DOut%d",
1151                                 chan + 1);
1152                 } else {
1153                         snprintf(name, sizeof(name),
1154                                 "Effect Send AIn%d",
1155                                 chan - num_outs + 1);
1156                 }
1157
1158                 cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1159                 err = snd_create_std_mono_ctl(mixer, id, control,
1160                                                 cmask, val_type, name,
1161                                                 &snd_usb_mixer_vol_tlv);
1162                 if (err < 0)
1163                         return err;
1164         }
1165
1166         return 0;
1167 }
1168
1169 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1170 {
1171         char name[64];
1172         unsigned int cmask;
1173         int chan, err;
1174         int num_outs = 0;
1175         int offset = 0;
1176
1177         const unsigned int id = 0x40;
1178         const int val_type = USB_MIXER_S16;
1179         const int control = 1;
1180
1181         switch (mixer->chip->usb_id) {
1182         case USB_ID(0x0763, 0x2030):
1183                 num_outs = 6;
1184                 offset = 0x3c;
1185                 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1186                 break;
1187         case USB_ID(0x0763, 0x2031):
1188                 num_outs = 8;
1189                 offset = 0x70;
1190                 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1191                 break;
1192         }
1193
1194         for (chan = 0; chan < num_outs; chan++) {
1195                 snprintf(name, sizeof(name),
1196                         "Effect Return %d",
1197                         chan + 1);
1198
1199                 cmask = (chan == 0) ? 0 :
1200                         1 << (chan + (chan % 2) * num_outs - 1);
1201                 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1202                                                 cmask, val_type, offset, name,
1203                                                 &snd_usb_mixer_vol_tlv);
1204                 if (err < 0)
1205                         return err;
1206         }
1207
1208         return 0;
1209 }
1210
1211 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1212 {
1213         int err;
1214
1215         err = snd_c400_create_vol_ctls(mixer);
1216         if (err < 0)
1217                 return err;
1218
1219         err = snd_c400_create_effect_vol_ctls(mixer);
1220         if (err < 0)
1221                 return err;
1222
1223         err = snd_c400_create_effect_ret_vol_ctls(mixer);
1224         if (err < 0)
1225                 return err;
1226
1227         err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1228         if (err < 0)
1229                 return err;
1230
1231         err = snd_c400_create_effect_volume_ctl(mixer);
1232         if (err < 0)
1233                 return err;
1234
1235         err = snd_c400_create_effect_duration_ctl(mixer);
1236         if (err < 0)
1237                 return err;
1238
1239         err = snd_c400_create_effect_feedback_ctl(mixer);
1240         if (err < 0)
1241                 return err;
1242
1243         return 0;
1244 }
1245
1246 /*
1247  * The mixer units for Ebox-44 are corrupt, and even where they
1248  * are valid they presents mono controls as L and R channels of
1249  * stereo. So we provide a good mixer here.
1250  */
1251 static struct std_mono_table ebox44_table[] = {
1252         {
1253                 .unitid = 4,
1254                 .control = 1,
1255                 .cmask = 0x0,
1256                 .val_type = USB_MIXER_INV_BOOLEAN,
1257                 .name = "Headphone Playback Switch"
1258         },
1259         {
1260                 .unitid = 4,
1261                 .control = 2,
1262                 .cmask = 0x1,
1263                 .val_type = USB_MIXER_S16,
1264                 .name = "Headphone A Mix Playback Volume"
1265         },
1266         {
1267                 .unitid = 4,
1268                 .control = 2,
1269                 .cmask = 0x2,
1270                 .val_type = USB_MIXER_S16,
1271                 .name = "Headphone B Mix Playback Volume"
1272         },
1273
1274         {
1275                 .unitid = 7,
1276                 .control = 1,
1277                 .cmask = 0x0,
1278                 .val_type = USB_MIXER_INV_BOOLEAN,
1279                 .name = "Output Playback Switch"
1280         },
1281         {
1282                 .unitid = 7,
1283                 .control = 2,
1284                 .cmask = 0x1,
1285                 .val_type = USB_MIXER_S16,
1286                 .name = "Output A Playback Volume"
1287         },
1288         {
1289                 .unitid = 7,
1290                 .control = 2,
1291                 .cmask = 0x2,
1292                 .val_type = USB_MIXER_S16,
1293                 .name = "Output B Playback Volume"
1294         },
1295
1296         {
1297                 .unitid = 10,
1298                 .control = 1,
1299                 .cmask = 0x0,
1300                 .val_type = USB_MIXER_INV_BOOLEAN,
1301                 .name = "Input Capture Switch"
1302         },
1303         {
1304                 .unitid = 10,
1305                 .control = 2,
1306                 .cmask = 0x1,
1307                 .val_type = USB_MIXER_S16,
1308                 .name = "Input A Capture Volume"
1309         },
1310         {
1311                 .unitid = 10,
1312                 .control = 2,
1313                 .cmask = 0x2,
1314                 .val_type = USB_MIXER_S16,
1315                 .name = "Input B Capture Volume"
1316         },
1317
1318         {}
1319 };
1320
1321 /* Audio Advantage Micro II findings:
1322  *
1323  * Mapping spdif AES bits to vendor register.bit:
1324  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1325  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1326  * AES2: [0 0 0 0 0 0 0 0]
1327  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1328  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1329  *
1330  * power on values:
1331  * r2: 0x10
1332  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1333  *           just after it to 0xa0, presumably it disables/mutes some analog
1334  *           parts when there is no audio.)
1335  * r9: 0x28
1336  *
1337  * Optical transmitter on/off:
1338  * vendor register.bit: 9.1
1339  * 0 - on (0x28 register value)
1340  * 1 - off (0x2a register value)
1341  *
1342  */
1343 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1344         struct snd_ctl_elem_info *uinfo)
1345 {
1346         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1347         uinfo->count = 1;
1348         return 0;
1349 }
1350
1351 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1352         struct snd_ctl_elem_value *ucontrol)
1353 {
1354         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1355         int err;
1356         struct usb_interface *iface;
1357         struct usb_host_interface *alts;
1358         unsigned int ep;
1359         unsigned char data[3];
1360         int rate;
1361
1362         ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1363         ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1364         ucontrol->value.iec958.status[2] = 0x00;
1365
1366         /* use known values for that card: interface#1 altsetting#1 */
1367         iface = usb_ifnum_to_if(mixer->chip->dev, 1);
1368         alts = &iface->altsetting[1];
1369         ep = get_endpoint(alts, 0)->bEndpointAddress;
1370
1371         err = snd_usb_ctl_msg(mixer->chip->dev,
1372                         usb_rcvctrlpipe(mixer->chip->dev, 0),
1373                         UAC_GET_CUR,
1374                         USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1375                         UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1376                         ep,
1377                         data,
1378                         sizeof(data));
1379         if (err < 0)
1380                 goto end;
1381
1382         rate = data[0] | (data[1] << 8) | (data[2] << 16);
1383         ucontrol->value.iec958.status[3] = (rate == 48000) ?
1384                         IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1385
1386         err = 0;
1387 end:
1388         return err;
1389 }
1390
1391 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1392         struct snd_ctl_elem_value *ucontrol)
1393 {
1394         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1395         int err;
1396         u8 reg;
1397         unsigned long priv_backup = kcontrol->private_value;
1398
1399         reg = ((ucontrol->value.iec958.status[1] & 0x0f) << 4) |
1400                         (ucontrol->value.iec958.status[0] & 0x0f);
1401         err = snd_usb_ctl_msg(mixer->chip->dev,
1402                         usb_sndctrlpipe(mixer->chip->dev, 0),
1403                         UAC_SET_CUR,
1404                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1405                         reg,
1406                         2,
1407                         NULL,
1408                         0);
1409         if (err < 0)
1410                 goto end;
1411
1412         kcontrol->private_value &= 0xfffff0f0;
1413         kcontrol->private_value |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1414         kcontrol->private_value |= (ucontrol->value.iec958.status[0] & 0x0f);
1415
1416         reg = (ucontrol->value.iec958.status[0] & IEC958_AES0_NONAUDIO) ?
1417                         0xa0 : 0x20;
1418         reg |= (ucontrol->value.iec958.status[1] >> 4) & 0x0f;
1419         err = snd_usb_ctl_msg(mixer->chip->dev,
1420                         usb_sndctrlpipe(mixer->chip->dev, 0),
1421                         UAC_SET_CUR,
1422                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1423                         reg,
1424                         3,
1425                         NULL,
1426                         0);
1427         if (err < 0)
1428                 goto end;
1429
1430         kcontrol->private_value &= 0xffff0fff;
1431         kcontrol->private_value |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1432
1433         /* The frequency bits in AES3 cannot be set via register access. */
1434
1435         /* Silently ignore any bits from the request that cannot be set. */
1436
1437         err = (priv_backup != kcontrol->private_value);
1438 end:
1439         return err;
1440 }
1441
1442 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1443         struct snd_ctl_elem_value *ucontrol)
1444 {
1445         ucontrol->value.iec958.status[0] = 0x0f;
1446         ucontrol->value.iec958.status[1] = 0xff;
1447         ucontrol->value.iec958.status[2] = 0x00;
1448         ucontrol->value.iec958.status[3] = 0x00;
1449
1450         return 0;
1451 }
1452
1453 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1454         struct snd_ctl_elem_value *ucontrol)
1455 {
1456         ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1457
1458         return 0;
1459 }
1460
1461 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1462         struct snd_ctl_elem_value *ucontrol)
1463 {
1464         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1465         int err;
1466         u8 reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1467
1468         err = snd_usb_ctl_msg(mixer->chip->dev,
1469                         usb_sndctrlpipe(mixer->chip->dev, 0),
1470                         UAC_SET_CUR,
1471                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1472                         reg,
1473                         9,
1474                         NULL,
1475                         0);
1476
1477         if (!err) {
1478                 err = (reg != (kcontrol->private_value & 0x0ff));
1479                 if (err)
1480                         kcontrol->private_value = reg;
1481         }
1482
1483         return err;
1484 }
1485
1486 static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1487         {
1488                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1489                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1490                 .info =     snd_microii_spdif_info,
1491                 .get =      snd_microii_spdif_default_get,
1492                 .put =      snd_microii_spdif_default_put,
1493                 .private_value = 0x00000100UL,/* reset value */
1494         },
1495         {
1496                 .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1497                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1498                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1499                 .info =     snd_microii_spdif_info,
1500                 .get =      snd_microii_spdif_mask_get,
1501         },
1502         {
1503                 .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1504                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1505                 .info =     snd_ctl_boolean_mono_info,
1506                 .get =      snd_microii_spdif_switch_get,
1507                 .put =      snd_microii_spdif_switch_put,
1508                 .private_value = 0x00000028UL,/* reset value */
1509         }
1510 };
1511
1512 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1513 {
1514         int err, i;
1515
1516         for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1517                 err = snd_ctl_add(mixer->chip->card,
1518                         snd_ctl_new1(&snd_microii_mixer_spdif[i], mixer));
1519                 if (err < 0)
1520                         return err;
1521         }
1522
1523         return err;
1524 }
1525
1526 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
1527 {
1528         int err = 0;
1529         struct snd_info_entry *entry;
1530
1531         if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1532                 return err;
1533
1534         switch (mixer->chip->usb_id) {
1535         case USB_ID(0x041e, 0x3020):
1536         case USB_ID(0x041e, 0x3040):
1537         case USB_ID(0x041e, 0x3042):
1538         case USB_ID(0x041e, 0x30df):
1539         case USB_ID(0x041e, 0x3048):
1540                 err = snd_audigy2nx_controls_create(mixer);
1541                 if (err < 0)
1542                         break;
1543                 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1544                         snd_info_set_text_ops(entry, mixer,
1545                                               snd_audigy2nx_proc_read);
1546                 break;
1547
1548         case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1549         case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
1550                 err = snd_c400_create_mixer(mixer);
1551                 break;
1552
1553         case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1554         case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1555                 err = snd_ftu_create_mixer(mixer);
1556                 break;
1557
1558         case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
1559         case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
1560         case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
1561                 err = snd_xonar_u1_controls_create(mixer);
1562                 break;
1563
1564         case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
1565                 err = snd_microii_controls_create(mixer);
1566                 break;
1567
1568         case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1569                 err = snd_nativeinstruments_create_mixer(mixer,
1570                                 snd_nativeinstruments_ta6_mixers,
1571                                 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1572                 break;
1573
1574         case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1575                 err = snd_nativeinstruments_create_mixer(mixer,
1576                                 snd_nativeinstruments_ta10_mixers,
1577                                 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1578                 break;
1579
1580         case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1581                 /* detection is disabled in mixer_maps.c */
1582                 err = snd_create_std_mono_table(mixer, ebox44_table);
1583                 break;
1584         }
1585
1586         return err;
1587 }
1588
1589 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1590                                     int unitid)
1591 {
1592         if (!mixer->rc_cfg)
1593                 return;
1594         /* unit ids specific to Extigy/Audigy 2 NX: */
1595         switch (unitid) {
1596         case 0: /* remote control */
1597                 mixer->rc_urb->dev = mixer->chip->dev;
1598                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1599                 break;
1600         case 4: /* digital in jack */
1601         case 7: /* line in jacks */
1602         case 19: /* speaker out jacks */
1603         case 20: /* headphones out jack */
1604                 break;
1605         /* live24ext: 4 = line-in jack */
1606         case 3: /* hp-out jack (may actuate Mute) */
1607                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1608                     mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1609                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1610                 break;
1611         default:
1612                 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1613                 break;
1614         }
1615 }
1616