Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.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/hid.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/usb.h>
34 #include <linux/usb/audio.h>
35
36 #include <sound/asoundef.h>
37 #include <sound/core.h>
38 #include <sound/control.h>
39 #include <sound/hwdep.h>
40 #include <sound/info.h>
41 #include <sound/tlv.h>
42
43 #include "usbaudio.h"
44 #include "mixer.h"
45 #include "mixer_quirks.h"
46 #include "mixer_scarlett.h"
47 #include "mixer_us16x08.h"
48 #include "helper.h"
49
50 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
51
52 struct std_mono_table {
53         unsigned int unitid, control, cmask;
54         int val_type;
55         const char *name;
56         snd_kcontrol_tlv_rw_t *tlv_callback;
57 };
58
59 /* This function allows for the creation of standard UAC controls.
60  * See the quirks for M-Audio FTUs or Ebox-44.
61  * If you don't want to set a TLV callback pass NULL.
62  *
63  * Since there doesn't seem to be a devices that needs a multichannel
64  * version, we keep it mono for simplicity.
65  */
66 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
67                                 unsigned int unitid,
68                                 unsigned int control,
69                                 unsigned int cmask,
70                                 int val_type,
71                                 unsigned int idx_off,
72                                 const char *name,
73                                 snd_kcontrol_tlv_rw_t *tlv_callback)
74 {
75         struct usb_mixer_elem_info *cval;
76         struct snd_kcontrol *kctl;
77
78         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
79         if (!cval)
80                 return -ENOMEM;
81
82         snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
83         cval->val_type = val_type;
84         cval->channels = 1;
85         cval->control = control;
86         cval->cmask = cmask;
87         cval->idx_off = idx_off;
88
89         /* get_min_max() is called only for integer volumes later,
90          * so provide a short-cut for booleans */
91         cval->min = 0;
92         cval->max = 1;
93         cval->res = 0;
94         cval->dBmin = 0;
95         cval->dBmax = 0;
96
97         /* Create control */
98         kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
99         if (!kctl) {
100                 kfree(cval);
101                 return -ENOMEM;
102         }
103
104         /* Set name */
105         snprintf(kctl->id.name, sizeof(kctl->id.name), name);
106         kctl->private_free = snd_usb_mixer_elem_free;
107
108         /* set TLV */
109         if (tlv_callback) {
110                 kctl->tlv.c = tlv_callback;
111                 kctl->vd[0].access |=
112                         SNDRV_CTL_ELEM_ACCESS_TLV_READ |
113                         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
114         }
115         /* Add control to mixer */
116         return snd_usb_mixer_add_control(&cval->head, kctl);
117 }
118
119 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
120                                 unsigned int unitid,
121                                 unsigned int control,
122                                 unsigned int cmask,
123                                 int val_type,
124                                 const char *name,
125                                 snd_kcontrol_tlv_rw_t *tlv_callback)
126 {
127         return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
128                 val_type, 0 /* Offset */, name, tlv_callback);
129 }
130
131 /*
132  * Create a set of standard UAC controls from a table
133  */
134 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
135                                 struct std_mono_table *t)
136 {
137         int err;
138
139         while (t->name != NULL) {
140                 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
141                                 t->cmask, t->val_type, t->name, t->tlv_callback);
142                 if (err < 0)
143                         return err;
144                 t++;
145         }
146
147         return 0;
148 }
149
150 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
151                                       int id,
152                                       usb_mixer_elem_resume_func_t resume,
153                                       const struct snd_kcontrol_new *knew,
154                                       struct usb_mixer_elem_list **listp)
155 {
156         struct usb_mixer_elem_list *list;
157         struct snd_kcontrol *kctl;
158
159         list = kzalloc(sizeof(*list), GFP_KERNEL);
160         if (!list)
161                 return -ENOMEM;
162         if (listp)
163                 *listp = list;
164         list->mixer = mixer;
165         list->id = id;
166         list->resume = resume;
167         kctl = snd_ctl_new1(knew, list);
168         if (!kctl) {
169                 kfree(list);
170                 return -ENOMEM;
171         }
172         kctl->private_free = snd_usb_mixer_elem_free;
173         return snd_usb_mixer_add_control(list, kctl);
174 }
175
176 /*
177  * Sound Blaster remote control configuration
178  *
179  * format of remote control data:
180  * Extigy:       xx 00
181  * Audigy 2 NX:  06 80 xx 00 00 00
182  * Live! 24-bit: 06 80 xx yy 22 83
183  */
184 static const struct rc_config {
185         u32 usb_id;
186         u8  offset;
187         u8  length;
188         u8  packet_length;
189         u8  min_packet_length; /* minimum accepted length of the URB result */
190         u8  mute_mixer_id;
191         u32 mute_code;
192 } rc_configs[] = {
193         { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
194         { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
195         { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
196         { USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
197         { USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
198         { USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
199         { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
200 };
201
202 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
203 {
204         struct usb_mixer_interface *mixer = urb->context;
205         const struct rc_config *rc = mixer->rc_cfg;
206         u32 code;
207
208         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
209                 return;
210
211         code = mixer->rc_buffer[rc->offset];
212         if (rc->length == 2)
213                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
214
215         /* the Mute button actually changes the mixer control */
216         if (code == rc->mute_code)
217                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
218         mixer->rc_code = code;
219         wmb();
220         wake_up(&mixer->rc_waitq);
221 }
222
223 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
224                                      long count, loff_t *offset)
225 {
226         struct usb_mixer_interface *mixer = hw->private_data;
227         int err;
228         u32 rc_code;
229
230         if (count != 1 && count != 4)
231                 return -EINVAL;
232         err = wait_event_interruptible(mixer->rc_waitq,
233                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
234         if (err == 0) {
235                 if (count == 1)
236                         err = put_user(rc_code, buf);
237                 else
238                         err = put_user(rc_code, (u32 __user *)buf);
239         }
240         return err < 0 ? err : count;
241 }
242
243 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
244                                             poll_table *wait)
245 {
246         struct usb_mixer_interface *mixer = hw->private_data;
247
248         poll_wait(file, &mixer->rc_waitq, wait);
249         return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
250 }
251
252 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
253 {
254         struct snd_hwdep *hwdep;
255         int err, len, i;
256
257         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
258                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
259                         break;
260         if (i >= ARRAY_SIZE(rc_configs))
261                 return 0;
262         mixer->rc_cfg = &rc_configs[i];
263
264         len = mixer->rc_cfg->packet_length;
265
266         init_waitqueue_head(&mixer->rc_waitq);
267         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
268         if (err < 0)
269                 return err;
270         snprintf(hwdep->name, sizeof(hwdep->name),
271                  "%s remote control", mixer->chip->card->shortname);
272         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
273         hwdep->private_data = mixer;
274         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
275         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
276         hwdep->exclusive = 1;
277
278         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
279         if (!mixer->rc_urb)
280                 return -ENOMEM;
281         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
282         if (!mixer->rc_setup_packet) {
283                 usb_free_urb(mixer->rc_urb);
284                 mixer->rc_urb = NULL;
285                 return -ENOMEM;
286         }
287         mixer->rc_setup_packet->bRequestType =
288                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
289         mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
290         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
291         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
292         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
293         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
294                              usb_rcvctrlpipe(mixer->chip->dev, 0),
295                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
296                              snd_usb_soundblaster_remote_complete, mixer);
297         return 0;
298 }
299
300 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
301
302 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
303 {
304         ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
305         return 0;
306 }
307
308 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
309                                     int value, int index)
310 {
311         struct snd_usb_audio *chip = mixer->chip;
312         int err;
313
314         err = snd_usb_lock_shutdown(chip);
315         if (err < 0)
316                 return err;
317
318         if (chip->usb_id == USB_ID(0x041e, 0x3042))
319                 err = snd_usb_ctl_msg(chip->dev,
320                               usb_sndctrlpipe(chip->dev, 0), 0x24,
321                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
322                               !value, 0, NULL, 0);
323         /* USB X-Fi S51 Pro */
324         if (chip->usb_id == USB_ID(0x041e, 0x30df))
325                 err = snd_usb_ctl_msg(chip->dev,
326                               usb_sndctrlpipe(chip->dev, 0), 0x24,
327                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
328                               !value, 0, NULL, 0);
329         else
330                 err = snd_usb_ctl_msg(chip->dev,
331                               usb_sndctrlpipe(chip->dev, 0), 0x24,
332                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
333                               value, index + 2, NULL, 0);
334         snd_usb_unlock_shutdown(chip);
335         return err;
336 }
337
338 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
339                                  struct snd_ctl_elem_value *ucontrol)
340 {
341         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
342         struct usb_mixer_interface *mixer = list->mixer;
343         int index = kcontrol->private_value & 0xff;
344         unsigned int value = ucontrol->value.integer.value[0];
345         int old_value = kcontrol->private_value >> 8;
346         int err;
347
348         if (value > 1)
349                 return -EINVAL;
350         if (value == old_value)
351                 return 0;
352         kcontrol->private_value = (value << 8) | index;
353         err = snd_audigy2nx_led_update(mixer, value, index);
354         return err < 0 ? err : 1;
355 }
356
357 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
358 {
359         int priv_value = list->kctl->private_value;
360
361         return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
362                                         priv_value & 0xff);
363 }
364
365 /* name and private_value are set dynamically */
366 static const struct snd_kcontrol_new snd_audigy2nx_control = {
367         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
368         .info = snd_audigy2nx_led_info,
369         .get = snd_audigy2nx_led_get,
370         .put = snd_audigy2nx_led_put,
371 };
372
373 static const char * const snd_audigy2nx_led_names[] = {
374         "CMSS LED Switch",
375         "Power LED Switch",
376         "Dolby Digital LED Switch",
377 };
378
379 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
380 {
381         int i, err;
382
383         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
384                 struct snd_kcontrol_new knew;
385
386                 /* USB X-Fi S51 doesn't have a CMSS LED */
387                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
388                         continue;
389                 /* USB X-Fi S51 Pro doesn't have one either */
390                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
391                         continue;
392                 if (i > 1 && /* Live24ext has 2 LEDs only */
393                         (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
394                          mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
395                          mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
396                          mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
397                         break; 
398
399                 knew = snd_audigy2nx_control;
400                 knew.name = snd_audigy2nx_led_names[i];
401                 knew.private_value = (1 << 8) | i; /* LED on as default */
402                 err = add_single_ctl_with_resume(mixer, 0,
403                                                  snd_audigy2nx_led_resume,
404                                                  &knew, NULL);
405                 if (err < 0)
406                         return err;
407         }
408         return 0;
409 }
410
411 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
412                                     struct snd_info_buffer *buffer)
413 {
414         static const struct sb_jack {
415                 int unitid;
416                 const char *name;
417         }  jacks_audigy2nx[] = {
418                 {4,  "dig in "},
419                 {7,  "line in"},
420                 {19, "spk out"},
421                 {20, "hph out"},
422                 {-1, NULL}
423         }, jacks_live24ext[] = {
424                 {4,  "line in"}, /* &1=Line, &2=Mic*/
425                 {3,  "hph out"}, /* headphones */
426                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
427                 {-1, NULL}
428         };
429         const struct sb_jack *jacks;
430         struct usb_mixer_interface *mixer = entry->private_data;
431         int i, err;
432         u8 buf[3];
433
434         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
435         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
436                 jacks = jacks_audigy2nx;
437         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
438                  mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
439                 jacks = jacks_live24ext;
440         else
441                 return;
442
443         for (i = 0; jacks[i].name; ++i) {
444                 snd_iprintf(buffer, "%s: ", jacks[i].name);
445                 err = snd_usb_lock_shutdown(mixer->chip);
446                 if (err < 0)
447                         return;
448                 err = snd_usb_ctl_msg(mixer->chip->dev,
449                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
450                                       UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
451                                       USB_RECIP_INTERFACE, 0,
452                                       jacks[i].unitid << 8, buf, 3);
453                 snd_usb_unlock_shutdown(mixer->chip);
454                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
455                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
456                 else
457                         snd_iprintf(buffer, "?\n");
458         }
459 }
460
461 /* EMU0204 */
462 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
463                                       struct snd_ctl_elem_info *uinfo)
464 {
465         static const char * const texts[2] = {"1/2", "3/4"};
466
467         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
468 }
469
470 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
471                                      struct snd_ctl_elem_value *ucontrol)
472 {
473         ucontrol->value.enumerated.item[0] = kcontrol->private_value;
474         return 0;
475 }
476
477 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
478                                         int value)
479 {
480         struct snd_usb_audio *chip = mixer->chip;
481         int err;
482         unsigned char buf[2];
483
484         err = snd_usb_lock_shutdown(chip);
485         if (err < 0)
486                 return err;
487
488         buf[0] = 0x01;
489         buf[1] = value ? 0x02 : 0x01;
490         err = snd_usb_ctl_msg(chip->dev,
491                       usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
492                       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
493                       0x0400, 0x0e00, buf, 2);
494         snd_usb_unlock_shutdown(chip);
495         return err;
496 }
497
498 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
499                                      struct snd_ctl_elem_value *ucontrol)
500 {
501         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
502         struct usb_mixer_interface *mixer = list->mixer;
503         unsigned int value = ucontrol->value.enumerated.item[0];
504         int err;
505
506         if (value > 1)
507                 return -EINVAL;
508
509         if (value == kcontrol->private_value)
510                 return 0;
511
512         kcontrol->private_value = value;
513         err = snd_emu0204_ch_switch_update(mixer, value);
514         return err < 0 ? err : 1;
515 }
516
517 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
518 {
519         return snd_emu0204_ch_switch_update(list->mixer,
520                                             list->kctl->private_value);
521 }
522
523 static struct snd_kcontrol_new snd_emu0204_control = {
524         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
525         .name = "Front Jack Channels",
526         .info = snd_emu0204_ch_switch_info,
527         .get = snd_emu0204_ch_switch_get,
528         .put = snd_emu0204_ch_switch_put,
529         .private_value = 0,
530 };
531
532 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
533 {
534         return add_single_ctl_with_resume(mixer, 0,
535                                           snd_emu0204_ch_switch_resume,
536                                           &snd_emu0204_control, NULL);
537 }
538
539 /* ASUS Xonar U1 / U3 controls */
540
541 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
542                                    struct snd_ctl_elem_value *ucontrol)
543 {
544         ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
545         return 0;
546 }
547
548 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
549                                       unsigned char status)
550 {
551         struct snd_usb_audio *chip = mixer->chip;
552         int err;
553
554         err = snd_usb_lock_shutdown(chip);
555         if (err < 0)
556                 return err;
557         err = snd_usb_ctl_msg(chip->dev,
558                               usb_sndctrlpipe(chip->dev, 0), 0x08,
559                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
560                               50, 0, &status, 1);
561         snd_usb_unlock_shutdown(chip);
562         return err;
563 }
564
565 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
566                                    struct snd_ctl_elem_value *ucontrol)
567 {
568         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
569         u8 old_status, new_status;
570         int err;
571
572         old_status = kcontrol->private_value;
573         if (ucontrol->value.integer.value[0])
574                 new_status = old_status | 0x02;
575         else
576                 new_status = old_status & ~0x02;
577         if (new_status == old_status)
578                 return 0;
579
580         kcontrol->private_value = new_status;
581         err = snd_xonar_u1_switch_update(list->mixer, new_status);
582         return err < 0 ? err : 1;
583 }
584
585 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
586 {
587         return snd_xonar_u1_switch_update(list->mixer,
588                                           list->kctl->private_value);
589 }
590
591 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
592         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
593         .name = "Digital Playback Switch",
594         .info = snd_ctl_boolean_mono_info,
595         .get = snd_xonar_u1_switch_get,
596         .put = snd_xonar_u1_switch_put,
597         .private_value = 0x05,
598 };
599
600 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
601 {
602         return add_single_ctl_with_resume(mixer, 0,
603                                           snd_xonar_u1_switch_resume,
604                                           &snd_xonar_u1_output_switch, NULL);
605 }
606
607 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
608
609 static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
610                                 struct snd_ctl_elem_value *ucontrol)
611 {
612         ucontrol->value.enumerated.item[0] = kctl->private_value;
613         return 0;
614 }
615
616 static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
617 {
618         struct snd_usb_audio *chip = mixer->chip;
619         int err;
620         unsigned char buff[3];
621
622         err = snd_usb_lock_shutdown(chip);
623         if (err < 0)
624                 return err;
625
626         /* Prepare for magic command to toggle clock source */
627         err = snd_usb_ctl_msg(chip->dev,
628                                 usb_rcvctrlpipe(chip->dev, 0), 0x81,
629                                 USB_DIR_IN |
630                                 USB_TYPE_CLASS |
631                                 USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
632         if (err < 0)
633                 goto err;
634         err = snd_usb_ctl_msg(chip->dev,
635                                 usb_rcvctrlpipe(chip->dev, 0), 0x81,
636                                 USB_DIR_IN |
637                                 USB_TYPE_CLASS |
638                                 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
639         if (err < 0)
640                 goto err;
641
642         /* 2 possibilities:     Internal    -> send sample rate
643          *                      S/PDIF sync -> send zeroes
644          * NB: Sample rate locked to 48kHz on purpose to
645          *     prevent user from resetting the sample rate
646          *     while S/PDIF sync is enabled and confusing
647          *     this configuration.
648          */
649         if (val == 0) {
650                 buff[0] = 0x80;
651                 buff[1] = 0xbb;
652                 buff[2] = 0x00;
653         } else {
654                 buff[0] = buff[1] = buff[2] = 0x00;
655         }
656
657         /* Send the magic command to toggle the clock source */
658         err = snd_usb_ctl_msg(chip->dev,
659                                 usb_sndctrlpipe(chip->dev, 0), 0x1,
660                                 USB_TYPE_CLASS |
661                                 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
662         if (err < 0)
663                 goto err;
664         err = snd_usb_ctl_msg(chip->dev,
665                                 usb_rcvctrlpipe(chip->dev, 0), 0x81,
666                                 USB_DIR_IN |
667                                 USB_TYPE_CLASS |
668                                 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
669         if (err < 0)
670                 goto err;
671         err = snd_usb_ctl_msg(chip->dev,
672                                 usb_rcvctrlpipe(chip->dev, 0), 0x81,
673                                 USB_DIR_IN |
674                                 USB_TYPE_CLASS |
675                                 USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
676         if (err < 0)
677                 goto err;
678
679 err:
680         snd_usb_unlock_shutdown(chip);
681         return err;
682 }
683
684 static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
685                                 struct snd_ctl_elem_value *ucontrol)
686 {
687         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
688         struct usb_mixer_interface *mixer = list->mixer;
689         int err;
690         bool cur_val, new_val;
691
692         cur_val = kctl->private_value;
693         new_val = ucontrol->value.enumerated.item[0];
694         if (cur_val == new_val)
695                 return 0;
696
697         kctl->private_value = new_val;
698         err = snd_mbox1_switch_update(mixer, new_val);
699         return err < 0 ? err : 1;
700 }
701
702 static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
703                                  struct snd_ctl_elem_info *uinfo)
704 {
705         static const char *const texts[2] = {
706                 "Internal",
707                 "S/PDIF"
708         };
709
710         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
711 }
712
713 static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
714 {
715         return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
716 }
717
718 static struct snd_kcontrol_new snd_mbox1_switch = {
719         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
720         .name = "Clock Source",
721         .index = 0,
722         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
723         .info = snd_mbox1_switch_info,
724         .get = snd_mbox1_switch_get,
725         .put = snd_mbox1_switch_put,
726         .private_value = 0
727 };
728
729 static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
730 {
731         return add_single_ctl_with_resume(mixer, 0,
732                                           snd_mbox1_switch_resume,
733                                           &snd_mbox1_switch, NULL);
734 }
735
736 /* Native Instruments device quirks */
737
738 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
739
740 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
741                                    struct snd_kcontrol *kctl)
742 {
743         struct usb_device *dev = mixer->chip->dev;
744         unsigned int pval = kctl->private_value;
745         u8 value;
746         int err;
747
748         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
749                               (pval >> 16) & 0xff,
750                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
751                               0, pval & 0xffff, &value, 1);
752         if (err < 0) {
753                 dev_err(&dev->dev,
754                         "unable to issue vendor read request (ret = %d)", err);
755                 return err;
756         }
757
758         kctl->private_value |= (value << 24);
759         return 0;
760 }
761
762 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
763                                              struct snd_ctl_elem_value *ucontrol)
764 {
765         ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
766         return 0;
767 }
768
769 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
770 {
771         struct snd_usb_audio *chip = list->mixer->chip;
772         unsigned int pval = list->kctl->private_value;
773         int err;
774
775         err = snd_usb_lock_shutdown(chip);
776         if (err < 0)
777                 return err;
778         err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
779                               (pval >> 16) & 0xff,
780                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
781                               pval >> 24, pval & 0xffff, NULL, 0, 1000);
782         snd_usb_unlock_shutdown(chip);
783         return err;
784 }
785
786 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
787                                              struct snd_ctl_elem_value *ucontrol)
788 {
789         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
790         u8 oldval = (kcontrol->private_value >> 24) & 0xff;
791         u8 newval = ucontrol->value.integer.value[0];
792         int err;
793
794         if (oldval == newval)
795                 return 0;
796
797         kcontrol->private_value &= ~(0xff << 24);
798         kcontrol->private_value |= (unsigned int)newval << 24;
799         err = snd_ni_update_cur_val(list);
800         return err < 0 ? err : 1;
801 }
802
803 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
804         {
805                 .name = "Direct Thru Channel A",
806                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
807         },
808         {
809                 .name = "Direct Thru Channel B",
810                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
811         },
812         {
813                 .name = "Phono Input Channel A",
814                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
815         },
816         {
817                 .name = "Phono Input Channel B",
818                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
819         },
820 };
821
822 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
823         {
824                 .name = "Direct Thru Channel A",
825                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
826         },
827         {
828                 .name = "Direct Thru Channel B",
829                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
830         },
831         {
832                 .name = "Direct Thru Channel C",
833                 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
834         },
835         {
836                 .name = "Direct Thru Channel D",
837                 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
838         },
839         {
840                 .name = "Phono Input Channel A",
841                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
842         },
843         {
844                 .name = "Phono Input Channel B",
845                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
846         },
847         {
848                 .name = "Phono Input Channel C",
849                 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
850         },
851         {
852                 .name = "Phono Input Channel D",
853                 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
854         },
855 };
856
857 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
858                                               const struct snd_kcontrol_new *kc,
859                                               unsigned int count)
860 {
861         int i, err = 0;
862         struct snd_kcontrol_new template = {
863                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
864                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
865                 .get = snd_nativeinstruments_control_get,
866                 .put = snd_nativeinstruments_control_put,
867                 .info = snd_ctl_boolean_mono_info,
868         };
869
870         for (i = 0; i < count; i++) {
871                 struct usb_mixer_elem_list *list;
872
873                 template.name = kc[i].name;
874                 template.private_value = kc[i].private_value;
875
876                 err = add_single_ctl_with_resume(mixer, 0,
877                                                  snd_ni_update_cur_val,
878                                                  &template, &list);
879                 if (err < 0)
880                         break;
881                 snd_ni_control_init_val(mixer, list->kctl);
882         }
883
884         return err;
885 }
886
887 /* M-Audio FastTrack Ultra quirks */
888 /* FTU Effect switch (also used by C400/C600) */
889 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
890                                         struct snd_ctl_elem_info *uinfo)
891 {
892         static const char *const texts[8] = {
893                 "Room 1", "Room 2", "Room 3", "Hall 1",
894                 "Hall 2", "Plate", "Delay", "Echo"
895         };
896
897         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
898 }
899
900 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
901                                    struct snd_kcontrol *kctl)
902 {
903         struct usb_device *dev = mixer->chip->dev;
904         unsigned int pval = kctl->private_value;
905         int err;
906         unsigned char value[2];
907
908         value[0] = 0x00;
909         value[1] = 0x00;
910
911         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
912                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
913                               pval & 0xff00,
914                               snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
915                               value, 2);
916         if (err < 0)
917                 return err;
918
919         kctl->private_value |= value[0] << 24;
920         return 0;
921 }
922
923 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
924                                         struct snd_ctl_elem_value *ucontrol)
925 {
926         ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
927         return 0;
928 }
929
930 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
931 {
932         struct snd_usb_audio *chip = list->mixer->chip;
933         unsigned int pval = list->kctl->private_value;
934         unsigned char value[2];
935         int err;
936
937         value[0] = pval >> 24;
938         value[1] = 0;
939
940         err = snd_usb_lock_shutdown(chip);
941         if (err < 0)
942                 return err;
943         err = snd_usb_ctl_msg(chip->dev,
944                               usb_sndctrlpipe(chip->dev, 0),
945                               UAC_SET_CUR,
946                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
947                               pval & 0xff00,
948                               snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
949                               value, 2);
950         snd_usb_unlock_shutdown(chip);
951         return err;
952 }
953
954 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
955                                         struct snd_ctl_elem_value *ucontrol)
956 {
957         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
958         unsigned int pval = list->kctl->private_value;
959         int cur_val, err, new_val;
960
961         cur_val = pval >> 24;
962         new_val = ucontrol->value.enumerated.item[0];
963         if (cur_val == new_val)
964                 return 0;
965
966         kctl->private_value &= ~(0xff << 24);
967         kctl->private_value |= new_val << 24;
968         err = snd_ftu_eff_switch_update(list);
969         return err < 0 ? err : 1;
970 }
971
972 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
973         int validx, int bUnitID)
974 {
975         static struct snd_kcontrol_new template = {
976                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
977                 .name = "Effect Program Switch",
978                 .index = 0,
979                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
980                 .info = snd_ftu_eff_switch_info,
981                 .get = snd_ftu_eff_switch_get,
982                 .put = snd_ftu_eff_switch_put
983         };
984         struct usb_mixer_elem_list *list;
985         int err;
986
987         err = add_single_ctl_with_resume(mixer, bUnitID,
988                                          snd_ftu_eff_switch_update,
989                                          &template, &list);
990         if (err < 0)
991                 return err;
992         list->kctl->private_value = (validx << 8) | bUnitID;
993         snd_ftu_eff_switch_init(mixer, list->kctl);
994         return 0;
995 }
996
997 /* Create volume controls for FTU devices*/
998 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
999 {
1000         char name[64];
1001         unsigned int control, cmask;
1002         int in, out, err;
1003
1004         const unsigned int id = 5;
1005         const int val_type = USB_MIXER_S16;
1006
1007         for (out = 0; out < 8; out++) {
1008                 control = out + 1;
1009                 for (in = 0; in < 8; in++) {
1010                         cmask = 1 << in;
1011                         snprintf(name, sizeof(name),
1012                                 "AIn%d - Out%d Capture Volume",
1013                                 in  + 1, out + 1);
1014                         err = snd_create_std_mono_ctl(mixer, id, control,
1015                                                         cmask, val_type, name,
1016                                                         &snd_usb_mixer_vol_tlv);
1017                         if (err < 0)
1018                                 return err;
1019                 }
1020                 for (in = 8; in < 16; in++) {
1021                         cmask = 1 << in;
1022                         snprintf(name, sizeof(name),
1023                                 "DIn%d - Out%d Playback Volume",
1024                                 in - 7, out + 1);
1025                         err = snd_create_std_mono_ctl(mixer, id, control,
1026                                                         cmask, val_type, name,
1027                                                         &snd_usb_mixer_vol_tlv);
1028                         if (err < 0)
1029                                 return err;
1030                 }
1031         }
1032
1033         return 0;
1034 }
1035
1036 /* This control needs a volume quirk, see mixer.c */
1037 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1038 {
1039         static const char name[] = "Effect Volume";
1040         const unsigned int id = 6;
1041         const int val_type = USB_MIXER_U8;
1042         const unsigned int control = 2;
1043         const unsigned int cmask = 0;
1044
1045         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1046                                         name, snd_usb_mixer_vol_tlv);
1047 }
1048
1049 /* This control needs a volume quirk, see mixer.c */
1050 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1051 {
1052         static const char name[] = "Effect Duration";
1053         const unsigned int id = 6;
1054         const int val_type = USB_MIXER_S16;
1055         const unsigned int control = 3;
1056         const unsigned int cmask = 0;
1057
1058         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1059                                         name, snd_usb_mixer_vol_tlv);
1060 }
1061
1062 /* This control needs a volume quirk, see mixer.c */
1063 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1064 {
1065         static const char name[] = "Effect Feedback Volume";
1066         const unsigned int id = 6;
1067         const int val_type = USB_MIXER_U8;
1068         const unsigned int control = 4;
1069         const unsigned int cmask = 0;
1070
1071         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1072                                         name, NULL);
1073 }
1074
1075 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1076 {
1077         unsigned int cmask;
1078         int err, ch;
1079         char name[48];
1080
1081         const unsigned int id = 7;
1082         const int val_type = USB_MIXER_S16;
1083         const unsigned int control = 7;
1084
1085         for (ch = 0; ch < 4; ++ch) {
1086                 cmask = 1 << ch;
1087                 snprintf(name, sizeof(name),
1088                         "Effect Return %d Volume", ch + 1);
1089                 err = snd_create_std_mono_ctl(mixer, id, control,
1090                                                 cmask, val_type, name,
1091                                                 snd_usb_mixer_vol_tlv);
1092                 if (err < 0)
1093                         return err;
1094         }
1095
1096         return 0;
1097 }
1098
1099 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1100 {
1101         unsigned int  cmask;
1102         int err, ch;
1103         char name[48];
1104
1105         const unsigned int id = 5;
1106         const int val_type = USB_MIXER_S16;
1107         const unsigned int control = 9;
1108
1109         for (ch = 0; ch < 8; ++ch) {
1110                 cmask = 1 << ch;
1111                 snprintf(name, sizeof(name),
1112                         "Effect Send AIn%d Volume", ch + 1);
1113                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1114                                                 val_type, name,
1115                                                 snd_usb_mixer_vol_tlv);
1116                 if (err < 0)
1117                         return err;
1118         }
1119         for (ch = 8; ch < 16; ++ch) {
1120                 cmask = 1 << ch;
1121                 snprintf(name, sizeof(name),
1122                         "Effect Send DIn%d Volume", ch - 7);
1123                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1124                                                 val_type, name,
1125                                                 snd_usb_mixer_vol_tlv);
1126                 if (err < 0)
1127                         return err;
1128         }
1129         return 0;
1130 }
1131
1132 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1133 {
1134         int err;
1135
1136         err = snd_ftu_create_volume_ctls(mixer);
1137         if (err < 0)
1138                 return err;
1139
1140         err = snd_ftu_create_effect_switch(mixer, 1, 6);
1141         if (err < 0)
1142                 return err;
1143
1144         err = snd_ftu_create_effect_volume_ctl(mixer);
1145         if (err < 0)
1146                 return err;
1147
1148         err = snd_ftu_create_effect_duration_ctl(mixer);
1149         if (err < 0)
1150                 return err;
1151
1152         err = snd_ftu_create_effect_feedback_ctl(mixer);
1153         if (err < 0)
1154                 return err;
1155
1156         err = snd_ftu_create_effect_return_ctls(mixer);
1157         if (err < 0)
1158                 return err;
1159
1160         err = snd_ftu_create_effect_send_ctls(mixer);
1161         if (err < 0)
1162                 return err;
1163
1164         return 0;
1165 }
1166
1167 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1168                                unsigned char samplerate_id)
1169 {
1170         struct usb_mixer_interface *mixer;
1171         struct usb_mixer_elem_info *cval;
1172         int unitid = 12; /* SamleRate ExtensionUnit ID */
1173
1174         list_for_each_entry(mixer, &chip->mixer_list, list) {
1175                 cval = (struct usb_mixer_elem_info *)mixer->id_elems[unitid];
1176                 if (cval) {
1177                         snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1178                                                     cval->control << 8,
1179                                                     samplerate_id);
1180                         snd_usb_mixer_notify_id(mixer, unitid);
1181                 }
1182                 break;
1183         }
1184 }
1185
1186 /* M-Audio Fast Track C400/C600 */
1187 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1188 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1189 {
1190         char name[64];
1191         unsigned int cmask, offset;
1192         int out, chan, err;
1193         int num_outs = 0;
1194         int num_ins = 0;
1195
1196         const unsigned int id = 0x40;
1197         const int val_type = USB_MIXER_S16;
1198         const int control = 1;
1199
1200         switch (mixer->chip->usb_id) {
1201         case USB_ID(0x0763, 0x2030):
1202                 num_outs = 6;
1203                 num_ins = 4;
1204                 break;
1205         case USB_ID(0x0763, 0x2031):
1206                 num_outs = 8;
1207                 num_ins = 6;
1208                 break;
1209         }
1210
1211         for (chan = 0; chan < num_outs + num_ins; chan++) {
1212                 for (out = 0; out < num_outs; out++) {
1213                         if (chan < num_outs) {
1214                                 snprintf(name, sizeof(name),
1215                                         "PCM%d-Out%d Playback Volume",
1216                                         chan + 1, out + 1);
1217                         } else {
1218                                 snprintf(name, sizeof(name),
1219                                         "In%d-Out%d Playback Volume",
1220                                         chan - num_outs + 1, out + 1);
1221                         }
1222
1223                         cmask = (out == 0) ? 0 : 1 << (out - 1);
1224                         offset = chan * num_outs;
1225                         err = snd_create_std_mono_ctl_offset(mixer, id, control,
1226                                                 cmask, val_type, offset, name,
1227                                                 &snd_usb_mixer_vol_tlv);
1228                         if (err < 0)
1229                                 return err;
1230                 }
1231         }
1232
1233         return 0;
1234 }
1235
1236 /* This control needs a volume quirk, see mixer.c */
1237 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1238 {
1239         static const char name[] = "Effect Volume";
1240         const unsigned int id = 0x43;
1241         const int val_type = USB_MIXER_U8;
1242         const unsigned int control = 3;
1243         const unsigned int cmask = 0;
1244
1245         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1246                                         name, snd_usb_mixer_vol_tlv);
1247 }
1248
1249 /* This control needs a volume quirk, see mixer.c */
1250 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1251 {
1252         static const char name[] = "Effect Duration";
1253         const unsigned int id = 0x43;
1254         const int val_type = USB_MIXER_S16;
1255         const unsigned int control = 4;
1256         const unsigned int cmask = 0;
1257
1258         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1259                                         name, snd_usb_mixer_vol_tlv);
1260 }
1261
1262 /* This control needs a volume quirk, see mixer.c */
1263 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1264 {
1265         static const char name[] = "Effect Feedback Volume";
1266         const unsigned int id = 0x43;
1267         const int val_type = USB_MIXER_U8;
1268         const unsigned int control = 5;
1269         const unsigned int cmask = 0;
1270
1271         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1272                                         name, NULL);
1273 }
1274
1275 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1276 {
1277         char name[64];
1278         unsigned int cmask;
1279         int chan, err;
1280         int num_outs = 0;
1281         int num_ins = 0;
1282
1283         const unsigned int id = 0x42;
1284         const int val_type = USB_MIXER_S16;
1285         const int control = 1;
1286
1287         switch (mixer->chip->usb_id) {
1288         case USB_ID(0x0763, 0x2030):
1289                 num_outs = 6;
1290                 num_ins = 4;
1291                 break;
1292         case USB_ID(0x0763, 0x2031):
1293                 num_outs = 8;
1294                 num_ins = 6;
1295                 break;
1296         }
1297
1298         for (chan = 0; chan < num_outs + num_ins; chan++) {
1299                 if (chan < num_outs) {
1300                         snprintf(name, sizeof(name),
1301                                 "Effect Send DOut%d",
1302                                 chan + 1);
1303                 } else {
1304                         snprintf(name, sizeof(name),
1305                                 "Effect Send AIn%d",
1306                                 chan - num_outs + 1);
1307                 }
1308
1309                 cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1310                 err = snd_create_std_mono_ctl(mixer, id, control,
1311                                                 cmask, val_type, name,
1312                                                 &snd_usb_mixer_vol_tlv);
1313                 if (err < 0)
1314                         return err;
1315         }
1316
1317         return 0;
1318 }
1319
1320 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1321 {
1322         char name[64];
1323         unsigned int cmask;
1324         int chan, err;
1325         int num_outs = 0;
1326         int offset = 0;
1327
1328         const unsigned int id = 0x40;
1329         const int val_type = USB_MIXER_S16;
1330         const int control = 1;
1331
1332         switch (mixer->chip->usb_id) {
1333         case USB_ID(0x0763, 0x2030):
1334                 num_outs = 6;
1335                 offset = 0x3c;
1336                 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1337                 break;
1338         case USB_ID(0x0763, 0x2031):
1339                 num_outs = 8;
1340                 offset = 0x70;
1341                 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1342                 break;
1343         }
1344
1345         for (chan = 0; chan < num_outs; chan++) {
1346                 snprintf(name, sizeof(name),
1347                         "Effect Return %d",
1348                         chan + 1);
1349
1350                 cmask = (chan == 0) ? 0 :
1351                         1 << (chan + (chan % 2) * num_outs - 1);
1352                 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1353                                                 cmask, val_type, offset, name,
1354                                                 &snd_usb_mixer_vol_tlv);
1355                 if (err < 0)
1356                         return err;
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1363 {
1364         int err;
1365
1366         err = snd_c400_create_vol_ctls(mixer);
1367         if (err < 0)
1368                 return err;
1369
1370         err = snd_c400_create_effect_vol_ctls(mixer);
1371         if (err < 0)
1372                 return err;
1373
1374         err = snd_c400_create_effect_ret_vol_ctls(mixer);
1375         if (err < 0)
1376                 return err;
1377
1378         err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1379         if (err < 0)
1380                 return err;
1381
1382         err = snd_c400_create_effect_volume_ctl(mixer);
1383         if (err < 0)
1384                 return err;
1385
1386         err = snd_c400_create_effect_duration_ctl(mixer);
1387         if (err < 0)
1388                 return err;
1389
1390         err = snd_c400_create_effect_feedback_ctl(mixer);
1391         if (err < 0)
1392                 return err;
1393
1394         return 0;
1395 }
1396
1397 /*
1398  * The mixer units for Ebox-44 are corrupt, and even where they
1399  * are valid they presents mono controls as L and R channels of
1400  * stereo. So we provide a good mixer here.
1401  */
1402 static struct std_mono_table ebox44_table[] = {
1403         {
1404                 .unitid = 4,
1405                 .control = 1,
1406                 .cmask = 0x0,
1407                 .val_type = USB_MIXER_INV_BOOLEAN,
1408                 .name = "Headphone Playback Switch"
1409         },
1410         {
1411                 .unitid = 4,
1412                 .control = 2,
1413                 .cmask = 0x1,
1414                 .val_type = USB_MIXER_S16,
1415                 .name = "Headphone A Mix Playback Volume"
1416         },
1417         {
1418                 .unitid = 4,
1419                 .control = 2,
1420                 .cmask = 0x2,
1421                 .val_type = USB_MIXER_S16,
1422                 .name = "Headphone B Mix Playback Volume"
1423         },
1424
1425         {
1426                 .unitid = 7,
1427                 .control = 1,
1428                 .cmask = 0x0,
1429                 .val_type = USB_MIXER_INV_BOOLEAN,
1430                 .name = "Output Playback Switch"
1431         },
1432         {
1433                 .unitid = 7,
1434                 .control = 2,
1435                 .cmask = 0x1,
1436                 .val_type = USB_MIXER_S16,
1437                 .name = "Output A Playback Volume"
1438         },
1439         {
1440                 .unitid = 7,
1441                 .control = 2,
1442                 .cmask = 0x2,
1443                 .val_type = USB_MIXER_S16,
1444                 .name = "Output B Playback Volume"
1445         },
1446
1447         {
1448                 .unitid = 10,
1449                 .control = 1,
1450                 .cmask = 0x0,
1451                 .val_type = USB_MIXER_INV_BOOLEAN,
1452                 .name = "Input Capture Switch"
1453         },
1454         {
1455                 .unitid = 10,
1456                 .control = 2,
1457                 .cmask = 0x1,
1458                 .val_type = USB_MIXER_S16,
1459                 .name = "Input A Capture Volume"
1460         },
1461         {
1462                 .unitid = 10,
1463                 .control = 2,
1464                 .cmask = 0x2,
1465                 .val_type = USB_MIXER_S16,
1466                 .name = "Input B Capture Volume"
1467         },
1468
1469         {}
1470 };
1471
1472 /* Audio Advantage Micro II findings:
1473  *
1474  * Mapping spdif AES bits to vendor register.bit:
1475  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1476  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1477  * AES2: [0 0 0 0 0 0 0 0]
1478  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1479  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1480  *
1481  * power on values:
1482  * r2: 0x10
1483  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1484  *           just after it to 0xa0, presumably it disables/mutes some analog
1485  *           parts when there is no audio.)
1486  * r9: 0x28
1487  *
1488  * Optical transmitter on/off:
1489  * vendor register.bit: 9.1
1490  * 0 - on (0x28 register value)
1491  * 1 - off (0x2a register value)
1492  *
1493  */
1494 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1495         struct snd_ctl_elem_info *uinfo)
1496 {
1497         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1498         uinfo->count = 1;
1499         return 0;
1500 }
1501
1502 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1503         struct snd_ctl_elem_value *ucontrol)
1504 {
1505         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1506         struct snd_usb_audio *chip = list->mixer->chip;
1507         int err;
1508         struct usb_interface *iface;
1509         struct usb_host_interface *alts;
1510         unsigned int ep;
1511         unsigned char data[3];
1512         int rate;
1513
1514         err = snd_usb_lock_shutdown(chip);
1515         if (err < 0)
1516                 return err;
1517
1518         ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1519         ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1520         ucontrol->value.iec958.status[2] = 0x00;
1521
1522         /* use known values for that card: interface#1 altsetting#1 */
1523         iface = usb_ifnum_to_if(chip->dev, 1);
1524         if (!iface || iface->num_altsetting < 2)
1525                 return -EINVAL;
1526         alts = &iface->altsetting[1];
1527         if (get_iface_desc(alts)->bNumEndpoints < 1)
1528                 return -EINVAL;
1529         ep = get_endpoint(alts, 0)->bEndpointAddress;
1530
1531         err = snd_usb_ctl_msg(chip->dev,
1532                         usb_rcvctrlpipe(chip->dev, 0),
1533                         UAC_GET_CUR,
1534                         USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1535                         UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1536                         ep,
1537                         data,
1538                         sizeof(data));
1539         if (err < 0)
1540                 goto end;
1541
1542         rate = data[0] | (data[1] << 8) | (data[2] << 16);
1543         ucontrol->value.iec958.status[3] = (rate == 48000) ?
1544                         IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1545
1546         err = 0;
1547  end:
1548         snd_usb_unlock_shutdown(chip);
1549         return err;
1550 }
1551
1552 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1553 {
1554         struct snd_usb_audio *chip = list->mixer->chip;
1555         unsigned int pval = list->kctl->private_value;
1556         u8 reg;
1557         int err;
1558
1559         err = snd_usb_lock_shutdown(chip);
1560         if (err < 0)
1561                 return err;
1562
1563         reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1564         err = snd_usb_ctl_msg(chip->dev,
1565                         usb_sndctrlpipe(chip->dev, 0),
1566                         UAC_SET_CUR,
1567                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1568                         reg,
1569                         2,
1570                         NULL,
1571                         0);
1572         if (err < 0)
1573                 goto end;
1574
1575         reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1576         reg |= (pval >> 12) & 0x0f;
1577         err = snd_usb_ctl_msg(chip->dev,
1578                         usb_sndctrlpipe(chip->dev, 0),
1579                         UAC_SET_CUR,
1580                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1581                         reg,
1582                         3,
1583                         NULL,
1584                         0);
1585         if (err < 0)
1586                 goto end;
1587
1588  end:
1589         snd_usb_unlock_shutdown(chip);
1590         return err;
1591 }
1592
1593 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1594         struct snd_ctl_elem_value *ucontrol)
1595 {
1596         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1597         unsigned int pval, pval_old;
1598         int err;
1599
1600         pval = pval_old = kcontrol->private_value;
1601         pval &= 0xfffff0f0;
1602         pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1603         pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1604
1605         pval &= 0xffff0fff;
1606         pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1607
1608         /* The frequency bits in AES3 cannot be set via register access. */
1609
1610         /* Silently ignore any bits from the request that cannot be set. */
1611
1612         if (pval == pval_old)
1613                 return 0;
1614
1615         kcontrol->private_value = pval;
1616         err = snd_microii_spdif_default_update(list);
1617         return err < 0 ? err : 1;
1618 }
1619
1620 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1621         struct snd_ctl_elem_value *ucontrol)
1622 {
1623         ucontrol->value.iec958.status[0] = 0x0f;
1624         ucontrol->value.iec958.status[1] = 0xff;
1625         ucontrol->value.iec958.status[2] = 0x00;
1626         ucontrol->value.iec958.status[3] = 0x00;
1627
1628         return 0;
1629 }
1630
1631 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1632         struct snd_ctl_elem_value *ucontrol)
1633 {
1634         ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1635
1636         return 0;
1637 }
1638
1639 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1640 {
1641         struct snd_usb_audio *chip = list->mixer->chip;
1642         u8 reg = list->kctl->private_value;
1643         int err;
1644
1645         err = snd_usb_lock_shutdown(chip);
1646         if (err < 0)
1647                 return err;
1648
1649         err = snd_usb_ctl_msg(chip->dev,
1650                         usb_sndctrlpipe(chip->dev, 0),
1651                         UAC_SET_CUR,
1652                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1653                         reg,
1654                         9,
1655                         NULL,
1656                         0);
1657
1658         snd_usb_unlock_shutdown(chip);
1659         return err;
1660 }
1661
1662 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1663         struct snd_ctl_elem_value *ucontrol)
1664 {
1665         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1666         u8 reg;
1667         int err;
1668
1669         reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1670         if (reg != list->kctl->private_value)
1671                 return 0;
1672
1673         kcontrol->private_value = reg;
1674         err = snd_microii_spdif_switch_update(list);
1675         return err < 0 ? err : 1;
1676 }
1677
1678 static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1679         {
1680                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1681                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1682                 .info =     snd_microii_spdif_info,
1683                 .get =      snd_microii_spdif_default_get,
1684                 .put =      snd_microii_spdif_default_put,
1685                 .private_value = 0x00000100UL,/* reset value */
1686         },
1687         {
1688                 .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1689                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1690                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1691                 .info =     snd_microii_spdif_info,
1692                 .get =      snd_microii_spdif_mask_get,
1693         },
1694         {
1695                 .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1696                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1697                 .info =     snd_ctl_boolean_mono_info,
1698                 .get =      snd_microii_spdif_switch_get,
1699                 .put =      snd_microii_spdif_switch_put,
1700                 .private_value = 0x00000028UL,/* reset value */
1701         }
1702 };
1703
1704 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1705 {
1706         int err, i;
1707         static usb_mixer_elem_resume_func_t resume_funcs[] = {
1708                 snd_microii_spdif_default_update,
1709                 NULL,
1710                 snd_microii_spdif_switch_update
1711         };
1712
1713         for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1714                 err = add_single_ctl_with_resume(mixer, 0,
1715                                                  resume_funcs[i],
1716                                                  &snd_microii_mixer_spdif[i],
1717                                                  NULL);
1718                 if (err < 0)
1719                         return err;
1720         }
1721
1722         return 0;
1723 }
1724
1725 /* Creative Sound Blaster E1 */
1726
1727 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1728                                           struct snd_ctl_elem_value *ucontrol)
1729 {
1730         ucontrol->value.integer.value[0] = kcontrol->private_value;
1731         return 0;
1732 }
1733
1734 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1735                                              unsigned char state)
1736 {
1737         struct snd_usb_audio *chip = mixer->chip;
1738         int err;
1739         unsigned char buff[2];
1740
1741         buff[0] = 0x02;
1742         buff[1] = state ? 0x02 : 0x00;
1743
1744         err = snd_usb_lock_shutdown(chip);
1745         if (err < 0)
1746                 return err;
1747         err = snd_usb_ctl_msg(chip->dev,
1748                         usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1749                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1750                         0x0202, 3, buff, 2);
1751         snd_usb_unlock_shutdown(chip);
1752         return err;
1753 }
1754
1755 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1756                                           struct snd_ctl_elem_value *ucontrol)
1757 {
1758         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1759         unsigned char value = !!ucontrol->value.integer.value[0];
1760         int err;
1761
1762         if (kcontrol->private_value == value)
1763                 return 0;
1764         kcontrol->private_value = value;
1765         err = snd_soundblaster_e1_switch_update(list->mixer, value);
1766         return err < 0 ? err : 1;
1767 }
1768
1769 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1770 {
1771         return snd_soundblaster_e1_switch_update(list->mixer,
1772                                                  list->kctl->private_value);
1773 }
1774
1775 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1776                                            struct snd_ctl_elem_info *uinfo)
1777 {
1778         static const char *const texts[2] = {
1779                 "Mic", "Aux"
1780         };
1781
1782         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1783 }
1784
1785 static struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
1786         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1787         .name = "Input Source",
1788         .info = snd_soundblaster_e1_switch_info,
1789         .get = snd_soundblaster_e1_switch_get,
1790         .put = snd_soundblaster_e1_switch_put,
1791         .private_value = 0,
1792 };
1793
1794 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1795 {
1796         return add_single_ctl_with_resume(mixer, 0,
1797                                           snd_soundblaster_e1_switch_resume,
1798                                           &snd_soundblaster_e1_input_switch,
1799                                           NULL);
1800 }
1801
1802 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
1803 {
1804         int err = 0;
1805         struct snd_info_entry *entry;
1806
1807         if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1808                 return err;
1809
1810         switch (mixer->chip->usb_id) {
1811         /* Tascam US-16x08 */
1812         case USB_ID(0x0644, 0x8047):
1813                 err = snd_us16x08_controls_create(mixer);
1814                 break;
1815         case USB_ID(0x041e, 0x3020):
1816         case USB_ID(0x041e, 0x3040):
1817         case USB_ID(0x041e, 0x3042):
1818         case USB_ID(0x041e, 0x30df):
1819         case USB_ID(0x041e, 0x3048):
1820                 err = snd_audigy2nx_controls_create(mixer);
1821                 if (err < 0)
1822                         break;
1823                 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1824                         snd_info_set_text_ops(entry, mixer,
1825                                               snd_audigy2nx_proc_read);
1826                 break;
1827
1828         /* EMU0204 */
1829         case USB_ID(0x041e, 0x3f19):
1830                 err = snd_emu0204_controls_create(mixer);
1831                 if (err < 0)
1832                         break;
1833                 break;
1834
1835         case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1836         case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
1837                 err = snd_c400_create_mixer(mixer);
1838                 break;
1839
1840         case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1841         case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1842                 err = snd_ftu_create_mixer(mixer);
1843                 break;
1844
1845         case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
1846         case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
1847         case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
1848                 err = snd_xonar_u1_controls_create(mixer);
1849                 break;
1850
1851         case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
1852                 err = snd_microii_controls_create(mixer);
1853                 break;
1854
1855         case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
1856                 err = snd_mbox1_create_sync_switch(mixer);
1857                 break;
1858
1859         case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1860                 err = snd_nativeinstruments_create_mixer(mixer,
1861                                 snd_nativeinstruments_ta6_mixers,
1862                                 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1863                 break;
1864
1865         case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1866                 err = snd_nativeinstruments_create_mixer(mixer,
1867                                 snd_nativeinstruments_ta10_mixers,
1868                                 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1869                 break;
1870
1871         case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1872                 /* detection is disabled in mixer_maps.c */
1873                 err = snd_create_std_mono_table(mixer, ebox44_table);
1874                 break;
1875
1876         case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
1877         case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
1878         case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
1879         case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
1880         case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
1881                 err = snd_scarlett_controls_create(mixer);
1882                 break;
1883
1884         case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
1885                 err = snd_soundblaster_e1_switch_create(mixer);
1886                 break;
1887         }
1888
1889         return err;
1890 }
1891
1892 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1893                                     int unitid)
1894 {
1895         if (!mixer->rc_cfg)
1896                 return;
1897         /* unit ids specific to Extigy/Audigy 2 NX: */
1898         switch (unitid) {
1899         case 0: /* remote control */
1900                 mixer->rc_urb->dev = mixer->chip->dev;
1901                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1902                 break;
1903         case 4: /* digital in jack */
1904         case 7: /* line in jacks */
1905         case 19: /* speaker out jacks */
1906         case 20: /* headphones out jack */
1907                 break;
1908         /* live24ext: 4 = line-in jack */
1909         case 3: /* hp-out jack (may actuate Mute) */
1910                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1911                     mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1912                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1913                 break;
1914         default:
1915                 usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
1916                 break;
1917         }
1918 }
1919
1920 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
1921                                          struct usb_mixer_elem_info *cval,
1922                                          struct snd_kcontrol *kctl)
1923 {
1924         /* Approximation using 10 ranges based on output measurement on hw v1.2.
1925          * This seems close to the cubic mapping e.g. alsamixer uses. */
1926         static const DECLARE_TLV_DB_RANGE(scale,
1927                  0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
1928                  2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
1929                  6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
1930                  8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
1931                 15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
1932                 17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
1933                 20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
1934                 27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
1935                 32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
1936                 41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
1937         );
1938
1939         if (cval->min == 0 && cval->max == 50) {
1940                 usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
1941                 kctl->tlv.p = scale;
1942                 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1943                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1944
1945         } else if (cval->min == 0 && cval->max <= 1000) {
1946                 /* Some other clearly broken DragonFly variant.
1947                  * At least a 0..53 variant (hw v1.0) exists.
1948                  */
1949                 usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
1950                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1951         }
1952 }
1953
1954 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
1955                                   struct usb_mixer_elem_info *cval, int unitid,
1956                                   struct snd_kcontrol *kctl)
1957 {
1958         switch (mixer->chip->usb_id) {
1959         case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
1960                 if (unitid == 7 && cval->control == UAC_FU_VOLUME)
1961                         snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
1962                 break;
1963         /* lowest playback value is muted on C-Media devices */
1964         case USB_ID(0x0d8c, 0x000c):
1965         case USB_ID(0x0d8c, 0x0014):
1966                 if (strstr(kctl->id.name, "Playback"))
1967                         cval->min_mute = 1;
1968                 break;
1969         }
1970 }
1971