Merge tag 'nfs-for-6.12-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[linux-2.6-block.git] / sound / usb / mixer_quirks.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   USB Audio Driver for ALSA
4  *
5  *   Quirks and vendor-specific extensions for mixer interfaces
6  *
7  *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8  *
9  *   Many codes borrowed from audio.c by
10  *          Alan Cox (alan@lxorguk.ukuu.org.uk)
11  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
12  *
13  *   Audio Advantage Micro II support added by:
14  *          Przemek Rudy (prudy1@o2.pl)
15  */
16
17 #include <linux/bitfield.h>
18 #include <linux/hid.h>
19 #include <linux/init.h>
20 #include <linux/math64.h>
21 #include <linux/slab.h>
22 #include <linux/usb.h>
23 #include <linux/usb/audio.h>
24
25 #include <sound/asoundef.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/hda_verbs.h>
29 #include <sound/hwdep.h>
30 #include <sound/info.h>
31 #include <sound/tlv.h>
32
33 #include "usbaudio.h"
34 #include "mixer.h"
35 #include "mixer_quirks.h"
36 #include "mixer_scarlett.h"
37 #include "mixer_scarlett2.h"
38 #include "mixer_us16x08.h"
39 #include "mixer_s1810c.h"
40 #include "helper.h"
41
42 struct std_mono_table {
43         unsigned int unitid, control, cmask;
44         int val_type;
45         const char *name;
46         snd_kcontrol_tlv_rw_t *tlv_callback;
47 };
48
49 /* This function allows for the creation of standard UAC controls.
50  * See the quirks for M-Audio FTUs or Ebox-44.
51  * If you don't want to set a TLV callback pass NULL.
52  *
53  * Since there doesn't seem to be a devices that needs a multichannel
54  * version, we keep it mono for simplicity.
55  */
56 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
57                                 unsigned int unitid,
58                                 unsigned int control,
59                                 unsigned int cmask,
60                                 int val_type,
61                                 unsigned int idx_off,
62                                 const char *name,
63                                 snd_kcontrol_tlv_rw_t *tlv_callback)
64 {
65         struct usb_mixer_elem_info *cval;
66         struct snd_kcontrol *kctl;
67
68         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
69         if (!cval)
70                 return -ENOMEM;
71
72         snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
73         cval->val_type = val_type;
74         cval->channels = 1;
75         cval->control = control;
76         cval->cmask = cmask;
77         cval->idx_off = idx_off;
78
79         /* get_min_max() is called only for integer volumes later,
80          * so provide a short-cut for booleans */
81         cval->min = 0;
82         cval->max = 1;
83         cval->res = 0;
84         cval->dBmin = 0;
85         cval->dBmax = 0;
86
87         /* Create control */
88         kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
89         if (!kctl) {
90                 kfree(cval);
91                 return -ENOMEM;
92         }
93
94         /* Set name */
95         snprintf(kctl->id.name, sizeof(kctl->id.name), name);
96         kctl->private_free = snd_usb_mixer_elem_free;
97
98         /* set TLV */
99         if (tlv_callback) {
100                 kctl->tlv.c = tlv_callback;
101                 kctl->vd[0].access |=
102                         SNDRV_CTL_ELEM_ACCESS_TLV_READ |
103                         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
104         }
105         /* Add control to mixer */
106         return snd_usb_mixer_add_control(&cval->head, kctl);
107 }
108
109 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
110                                 unsigned int unitid,
111                                 unsigned int control,
112                                 unsigned int cmask,
113                                 int val_type,
114                                 const char *name,
115                                 snd_kcontrol_tlv_rw_t *tlv_callback)
116 {
117         return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
118                 val_type, 0 /* Offset */, name, tlv_callback);
119 }
120
121 /*
122  * Create a set of standard UAC controls from a table
123  */
124 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
125                                      const struct std_mono_table *t)
126 {
127         int err;
128
129         while (t->name != NULL) {
130                 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
131                                 t->cmask, t->val_type, t->name, t->tlv_callback);
132                 if (err < 0)
133                         return err;
134                 t++;
135         }
136
137         return 0;
138 }
139
140 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
141                                       int id,
142                                       usb_mixer_elem_resume_func_t resume,
143                                       const struct snd_kcontrol_new *knew,
144                                       struct usb_mixer_elem_list **listp)
145 {
146         struct usb_mixer_elem_list *list;
147         struct snd_kcontrol *kctl;
148
149         list = kzalloc(sizeof(*list), GFP_KERNEL);
150         if (!list)
151                 return -ENOMEM;
152         if (listp)
153                 *listp = list;
154         list->mixer = mixer;
155         list->id = id;
156         list->resume = resume;
157         kctl = snd_ctl_new1(knew, list);
158         if (!kctl) {
159                 kfree(list);
160                 return -ENOMEM;
161         }
162         kctl->private_free = snd_usb_mixer_elem_free;
163         /* don't use snd_usb_mixer_add_control() here, this is a special list element */
164         return snd_usb_mixer_add_list(list, kctl, false);
165 }
166
167 /*
168  * Sound Blaster remote control configuration
169  *
170  * format of remote control data:
171  * Extigy:       xx 00
172  * Audigy 2 NX:  06 80 xx 00 00 00
173  * Live! 24-bit: 06 80 xx yy 22 83
174  */
175 static const struct rc_config {
176         u32 usb_id;
177         u8  offset;
178         u8  length;
179         u8  packet_length;
180         u8  min_packet_length; /* minimum accepted length of the URB result */
181         u8  mute_mixer_id;
182         u32 mute_code;
183 } rc_configs[] = {
184         { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
185         { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
186         { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
187         { USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
188         { USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
189         { USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
190         { USB_ID(0x041e, 0x3263), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
191         { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
192 };
193
194 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
195 {
196         struct usb_mixer_interface *mixer = urb->context;
197         const struct rc_config *rc = mixer->rc_cfg;
198         u32 code;
199
200         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
201                 return;
202
203         code = mixer->rc_buffer[rc->offset];
204         if (rc->length == 2)
205                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
206
207         /* the Mute button actually changes the mixer control */
208         if (code == rc->mute_code)
209                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
210         mixer->rc_code = code;
211         wmb();
212         wake_up(&mixer->rc_waitq);
213 }
214
215 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
216                                      long count, loff_t *offset)
217 {
218         struct usb_mixer_interface *mixer = hw->private_data;
219         int err;
220         u32 rc_code;
221
222         if (count != 1 && count != 4)
223                 return -EINVAL;
224         err = wait_event_interruptible(mixer->rc_waitq,
225                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
226         if (err == 0) {
227                 if (count == 1)
228                         err = put_user(rc_code, buf);
229                 else
230                         err = put_user(rc_code, (u32 __user *)buf);
231         }
232         return err < 0 ? err : count;
233 }
234
235 static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
236                                             poll_table *wait)
237 {
238         struct usb_mixer_interface *mixer = hw->private_data;
239
240         poll_wait(file, &mixer->rc_waitq, wait);
241         return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
242 }
243
244 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
245 {
246         struct snd_hwdep *hwdep;
247         int err, len, i;
248
249         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
250                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
251                         break;
252         if (i >= ARRAY_SIZE(rc_configs))
253                 return 0;
254         mixer->rc_cfg = &rc_configs[i];
255
256         len = mixer->rc_cfg->packet_length;
257
258         init_waitqueue_head(&mixer->rc_waitq);
259         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
260         if (err < 0)
261                 return err;
262         snprintf(hwdep->name, sizeof(hwdep->name),
263                  "%s remote control", mixer->chip->card->shortname);
264         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
265         hwdep->private_data = mixer;
266         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
267         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
268         hwdep->exclusive = 1;
269
270         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
271         if (!mixer->rc_urb)
272                 return -ENOMEM;
273         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
274         if (!mixer->rc_setup_packet) {
275                 usb_free_urb(mixer->rc_urb);
276                 mixer->rc_urb = NULL;
277                 return -ENOMEM;
278         }
279         mixer->rc_setup_packet->bRequestType =
280                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
281         mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
282         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
283         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
284         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
285         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
286                              usb_rcvctrlpipe(mixer->chip->dev, 0),
287                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
288                              snd_usb_soundblaster_remote_complete, mixer);
289         return 0;
290 }
291
292 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
293
294 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
295 {
296         ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
297         return 0;
298 }
299
300 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
301                                     int value, int index)
302 {
303         struct snd_usb_audio *chip = mixer->chip;
304         int err;
305
306         err = snd_usb_lock_shutdown(chip);
307         if (err < 0)
308                 return err;
309
310         if (chip->usb_id == USB_ID(0x041e, 0x3042))
311                 err = snd_usb_ctl_msg(chip->dev,
312                               usb_sndctrlpipe(chip->dev, 0), 0x24,
313                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
314                               !value, 0, NULL, 0);
315         /* USB X-Fi S51 Pro */
316         if (chip->usb_id == USB_ID(0x041e, 0x30df))
317                 err = snd_usb_ctl_msg(chip->dev,
318                               usb_sndctrlpipe(chip->dev, 0), 0x24,
319                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
320                               !value, 0, NULL, 0);
321         else
322                 err = snd_usb_ctl_msg(chip->dev,
323                               usb_sndctrlpipe(chip->dev, 0), 0x24,
324                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
325                               value, index + 2, NULL, 0);
326         snd_usb_unlock_shutdown(chip);
327         return err;
328 }
329
330 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
331                                  struct snd_ctl_elem_value *ucontrol)
332 {
333         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
334         struct usb_mixer_interface *mixer = list->mixer;
335         int index = kcontrol->private_value & 0xff;
336         unsigned int value = ucontrol->value.integer.value[0];
337         int old_value = kcontrol->private_value >> 8;
338         int err;
339
340         if (value > 1)
341                 return -EINVAL;
342         if (value == old_value)
343                 return 0;
344         kcontrol->private_value = (value << 8) | index;
345         err = snd_audigy2nx_led_update(mixer, value, index);
346         return err < 0 ? err : 1;
347 }
348
349 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
350 {
351         int priv_value = list->kctl->private_value;
352
353         return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
354                                         priv_value & 0xff);
355 }
356
357 /* name and private_value are set dynamically */
358 static const struct snd_kcontrol_new snd_audigy2nx_control = {
359         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
360         .info = snd_audigy2nx_led_info,
361         .get = snd_audigy2nx_led_get,
362         .put = snd_audigy2nx_led_put,
363 };
364
365 static const char * const snd_audigy2nx_led_names[] = {
366         "CMSS LED Switch",
367         "Power LED Switch",
368         "Dolby Digital LED Switch",
369 };
370
371 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
372 {
373         int i, err;
374
375         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
376                 struct snd_kcontrol_new knew;
377
378                 /* USB X-Fi S51 doesn't have a CMSS LED */
379                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
380                         continue;
381                 /* USB X-Fi S51 Pro doesn't have one either */
382                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
383                         continue;
384                 if (i > 1 && /* Live24ext has 2 LEDs only */
385                         (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
386                          mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
387                          mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
388                          mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
389                         break; 
390
391                 knew = snd_audigy2nx_control;
392                 knew.name = snd_audigy2nx_led_names[i];
393                 knew.private_value = (1 << 8) | i; /* LED on as default */
394                 err = add_single_ctl_with_resume(mixer, 0,
395                                                  snd_audigy2nx_led_resume,
396                                                  &knew, NULL);
397                 if (err < 0)
398                         return err;
399         }
400         return 0;
401 }
402
403 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
404                                     struct snd_info_buffer *buffer)
405 {
406         static const struct sb_jack {
407                 int unitid;
408                 const char *name;
409         }  jacks_audigy2nx[] = {
410                 {4,  "dig in "},
411                 {7,  "line in"},
412                 {19, "spk out"},
413                 {20, "hph out"},
414                 {-1, NULL}
415         }, jacks_live24ext[] = {
416                 {4,  "line in"}, /* &1=Line, &2=Mic*/
417                 {3,  "hph out"}, /* headphones */
418                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
419                 {-1, NULL}
420         };
421         const struct sb_jack *jacks;
422         struct usb_mixer_interface *mixer = entry->private_data;
423         int i, err;
424         u8 buf[3];
425
426         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
427         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
428                 jacks = jacks_audigy2nx;
429         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
430                  mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
431                 jacks = jacks_live24ext;
432         else
433                 return;
434
435         for (i = 0; jacks[i].name; ++i) {
436                 snd_iprintf(buffer, "%s: ", jacks[i].name);
437                 err = snd_usb_lock_shutdown(mixer->chip);
438                 if (err < 0)
439                         return;
440                 err = snd_usb_ctl_msg(mixer->chip->dev,
441                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
442                                       UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
443                                       USB_RECIP_INTERFACE, 0,
444                                       jacks[i].unitid << 8, buf, 3);
445                 snd_usb_unlock_shutdown(mixer->chip);
446                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
447                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
448                 else
449                         snd_iprintf(buffer, "?\n");
450         }
451 }
452
453 /* EMU0204 */
454 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
455                                       struct snd_ctl_elem_info *uinfo)
456 {
457         static const char * const texts[2] = {"1/2", "3/4"};
458
459         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
460 }
461
462 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
463                                      struct snd_ctl_elem_value *ucontrol)
464 {
465         ucontrol->value.enumerated.item[0] = kcontrol->private_value;
466         return 0;
467 }
468
469 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
470                                         int value)
471 {
472         struct snd_usb_audio *chip = mixer->chip;
473         int err;
474         unsigned char buf[2];
475
476         err = snd_usb_lock_shutdown(chip);
477         if (err < 0)
478                 return err;
479
480         buf[0] = 0x01;
481         buf[1] = value ? 0x02 : 0x01;
482         err = snd_usb_ctl_msg(chip->dev,
483                       usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
484                       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
485                       0x0400, 0x0e00, buf, 2);
486         snd_usb_unlock_shutdown(chip);
487         return err;
488 }
489
490 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
491                                      struct snd_ctl_elem_value *ucontrol)
492 {
493         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
494         struct usb_mixer_interface *mixer = list->mixer;
495         unsigned int value = ucontrol->value.enumerated.item[0];
496         int err;
497
498         if (value > 1)
499                 return -EINVAL;
500
501         if (value == kcontrol->private_value)
502                 return 0;
503
504         kcontrol->private_value = value;
505         err = snd_emu0204_ch_switch_update(mixer, value);
506         return err < 0 ? err : 1;
507 }
508
509 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
510 {
511         return snd_emu0204_ch_switch_update(list->mixer,
512                                             list->kctl->private_value);
513 }
514
515 static const struct snd_kcontrol_new snd_emu0204_control = {
516         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
517         .name = "Front Jack Channels",
518         .info = snd_emu0204_ch_switch_info,
519         .get = snd_emu0204_ch_switch_get,
520         .put = snd_emu0204_ch_switch_put,
521         .private_value = 0,
522 };
523
524 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
525 {
526         return add_single_ctl_with_resume(mixer, 0,
527                                           snd_emu0204_ch_switch_resume,
528                                           &snd_emu0204_control, NULL);
529 }
530
531 /* ASUS Xonar U1 / U3 controls */
532
533 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
534                                    struct snd_ctl_elem_value *ucontrol)
535 {
536         ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
537         return 0;
538 }
539
540 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
541                                       unsigned char status)
542 {
543         struct snd_usb_audio *chip = mixer->chip;
544         int err;
545
546         err = snd_usb_lock_shutdown(chip);
547         if (err < 0)
548                 return err;
549         err = snd_usb_ctl_msg(chip->dev,
550                               usb_sndctrlpipe(chip->dev, 0), 0x08,
551                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
552                               50, 0, &status, 1);
553         snd_usb_unlock_shutdown(chip);
554         return err;
555 }
556
557 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
558                                    struct snd_ctl_elem_value *ucontrol)
559 {
560         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
561         u8 old_status, new_status;
562         int err;
563
564         old_status = kcontrol->private_value;
565         if (ucontrol->value.integer.value[0])
566                 new_status = old_status | 0x02;
567         else
568                 new_status = old_status & ~0x02;
569         if (new_status == old_status)
570                 return 0;
571
572         kcontrol->private_value = new_status;
573         err = snd_xonar_u1_switch_update(list->mixer, new_status);
574         return err < 0 ? err : 1;
575 }
576
577 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
578 {
579         return snd_xonar_u1_switch_update(list->mixer,
580                                           list->kctl->private_value);
581 }
582
583 static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
584         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
585         .name = "Digital Playback Switch",
586         .info = snd_ctl_boolean_mono_info,
587         .get = snd_xonar_u1_switch_get,
588         .put = snd_xonar_u1_switch_put,
589         .private_value = 0x05,
590 };
591
592 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
593 {
594         return add_single_ctl_with_resume(mixer, 0,
595                                           snd_xonar_u1_switch_resume,
596                                           &snd_xonar_u1_output_switch, NULL);
597 }
598
599 /* Digidesign Mbox 1 helper functions */
600
601 static int snd_mbox1_is_spdif_synced(struct snd_usb_audio *chip)
602 {
603         unsigned char buff[3];
604         int err;
605         int is_spdif_synced;
606
607         /* Read clock source */
608         err = snd_usb_ctl_msg(chip->dev,
609                               usb_rcvctrlpipe(chip->dev, 0), 0x81,
610                               USB_DIR_IN |
611                               USB_TYPE_CLASS |
612                               USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
613         if (err < 0)
614                 return err;
615
616         /* spdif sync: buff is all zeroes */
617         is_spdif_synced = !(buff[0] | buff[1] | buff[2]);
618         return is_spdif_synced;
619 }
620
621 static int snd_mbox1_set_clk_source(struct snd_usb_audio *chip, int rate_or_zero)
622 {
623         /* 2 possibilities:     Internal    -> expects sample rate
624          *                      S/PDIF sync -> expects rate = 0
625          */
626         unsigned char buff[3];
627
628         buff[0] = (rate_or_zero >>  0) & 0xff;
629         buff[1] = (rate_or_zero >>  8) & 0xff;
630         buff[2] = (rate_or_zero >> 16) & 0xff;
631
632         /* Set clock source */
633         return snd_usb_ctl_msg(chip->dev,
634                                usb_sndctrlpipe(chip->dev, 0), 0x1,
635                                USB_TYPE_CLASS |
636                                USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
637 }
638
639 static int snd_mbox1_is_spdif_input(struct snd_usb_audio *chip)
640 {
641         /* Hardware gives 2 possibilities:      ANALOG Source  -> 0x01
642          *                                      S/PDIF Source  -> 0x02
643          */
644         int err;
645         unsigned char source[1];
646
647         /* Read input source */
648         err = snd_usb_ctl_msg(chip->dev,
649                               usb_rcvctrlpipe(chip->dev, 0), 0x81,
650                               USB_DIR_IN |
651                               USB_TYPE_CLASS |
652                               USB_RECIP_INTERFACE, 0x00, 0x500, source, 1);
653         if (err < 0)
654                 return err;
655
656         return (source[0] == 2);
657 }
658
659 static int snd_mbox1_set_input_source(struct snd_usb_audio *chip, int is_spdif)
660 {
661         /* NB: Setting the input source to S/PDIF resets the clock source to S/PDIF
662          * Hardware expects 2 possibilities:    ANALOG Source  -> 0x01
663          *                                      S/PDIF Source  -> 0x02
664          */
665         unsigned char buff[1];
666
667         buff[0] = (is_spdif & 1) + 1;
668
669         /* Set input source */
670         return snd_usb_ctl_msg(chip->dev,
671                                usb_sndctrlpipe(chip->dev, 0), 0x1,
672                                USB_TYPE_CLASS |
673                                USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
674 }
675
676 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
677
678 static int snd_mbox1_clk_switch_get(struct snd_kcontrol *kctl,
679                                     struct snd_ctl_elem_value *ucontrol)
680 {
681         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
682         struct snd_usb_audio *chip = list->mixer->chip;
683         int err;
684
685         err = snd_usb_lock_shutdown(chip);
686         if (err < 0)
687                 goto err;
688
689         err = snd_mbox1_is_spdif_synced(chip);
690         if (err < 0)
691                 goto err;
692
693         kctl->private_value = err;
694         err = 0;
695         ucontrol->value.enumerated.item[0] = kctl->private_value;
696 err:
697         snd_usb_unlock_shutdown(chip);
698         return err;
699 }
700
701 static int snd_mbox1_clk_switch_update(struct usb_mixer_interface *mixer, int is_spdif_sync)
702 {
703         struct snd_usb_audio *chip = mixer->chip;
704         int err;
705
706         err = snd_usb_lock_shutdown(chip);
707         if (err < 0)
708                 return err;
709
710         err = snd_mbox1_is_spdif_input(chip);
711         if (err < 0)
712                 goto err;
713
714         err = snd_mbox1_is_spdif_synced(chip);
715         if (err < 0)
716                 goto err;
717
718         /* FIXME: hardcoded sample rate */
719         err = snd_mbox1_set_clk_source(chip, is_spdif_sync ? 0 : 48000);
720         if (err < 0)
721                 goto err;
722
723         err = snd_mbox1_is_spdif_synced(chip);
724 err:
725         snd_usb_unlock_shutdown(chip);
726         return err;
727 }
728
729 static int snd_mbox1_clk_switch_put(struct snd_kcontrol *kctl,
730                                     struct snd_ctl_elem_value *ucontrol)
731 {
732         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
733         struct usb_mixer_interface *mixer = list->mixer;
734         int err;
735         bool cur_val, new_val;
736
737         cur_val = kctl->private_value;
738         new_val = ucontrol->value.enumerated.item[0];
739         if (cur_val == new_val)
740                 return 0;
741
742         kctl->private_value = new_val;
743         err = snd_mbox1_clk_switch_update(mixer, new_val);
744         return err < 0 ? err : 1;
745 }
746
747 static int snd_mbox1_clk_switch_info(struct snd_kcontrol *kcontrol,
748                                      struct snd_ctl_elem_info *uinfo)
749 {
750         static const char *const texts[2] = {
751                 "Internal",
752                 "S/PDIF"
753         };
754
755         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
756 }
757
758 static int snd_mbox1_clk_switch_resume(struct usb_mixer_elem_list *list)
759 {
760         return snd_mbox1_clk_switch_update(list->mixer, list->kctl->private_value);
761 }
762
763 /* Digidesign Mbox 1 input source switch (analog/spdif) */
764
765 static int snd_mbox1_src_switch_get(struct snd_kcontrol *kctl,
766                                     struct snd_ctl_elem_value *ucontrol)
767 {
768         ucontrol->value.enumerated.item[0] = kctl->private_value;
769         return 0;
770 }
771
772 static int snd_mbox1_src_switch_update(struct usb_mixer_interface *mixer, int is_spdif_input)
773 {
774         struct snd_usb_audio *chip = mixer->chip;
775         int err;
776
777         err = snd_usb_lock_shutdown(chip);
778         if (err < 0)
779                 return err;
780
781         err = snd_mbox1_is_spdif_input(chip);
782         if (err < 0)
783                 goto err;
784
785         err = snd_mbox1_set_input_source(chip, is_spdif_input);
786         if (err < 0)
787                 goto err;
788
789         err = snd_mbox1_is_spdif_input(chip);
790         if (err < 0)
791                 goto err;
792
793         err = snd_mbox1_is_spdif_synced(chip);
794 err:
795         snd_usb_unlock_shutdown(chip);
796         return err;
797 }
798
799 static int snd_mbox1_src_switch_put(struct snd_kcontrol *kctl,
800                                     struct snd_ctl_elem_value *ucontrol)
801 {
802         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
803         struct usb_mixer_interface *mixer = list->mixer;
804         int err;
805         bool cur_val, new_val;
806
807         cur_val = kctl->private_value;
808         new_val = ucontrol->value.enumerated.item[0];
809         if (cur_val == new_val)
810                 return 0;
811
812         kctl->private_value = new_val;
813         err = snd_mbox1_src_switch_update(mixer, new_val);
814         return err < 0 ? err : 1;
815 }
816
817 static int snd_mbox1_src_switch_info(struct snd_kcontrol *kcontrol,
818                                      struct snd_ctl_elem_info *uinfo)
819 {
820         static const char *const texts[2] = {
821                 "Analog",
822                 "S/PDIF"
823         };
824
825         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
826 }
827
828 static int snd_mbox1_src_switch_resume(struct usb_mixer_elem_list *list)
829 {
830         return snd_mbox1_src_switch_update(list->mixer, list->kctl->private_value);
831 }
832
833 static const struct snd_kcontrol_new snd_mbox1_clk_switch = {
834         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835         .name = "Clock Source",
836         .index = 0,
837         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
838         .info = snd_mbox1_clk_switch_info,
839         .get = snd_mbox1_clk_switch_get,
840         .put = snd_mbox1_clk_switch_put,
841         .private_value = 0
842 };
843
844 static const struct snd_kcontrol_new snd_mbox1_src_switch = {
845         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
846         .name = "Input Source",
847         .index = 1,
848         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
849         .info = snd_mbox1_src_switch_info,
850         .get = snd_mbox1_src_switch_get,
851         .put = snd_mbox1_src_switch_put,
852         .private_value = 0
853 };
854
855 static int snd_mbox1_controls_create(struct usb_mixer_interface *mixer)
856 {
857         int err;
858         err = add_single_ctl_with_resume(mixer, 0,
859                                          snd_mbox1_clk_switch_resume,
860                                          &snd_mbox1_clk_switch, NULL);
861         if (err < 0)
862                 return err;
863
864         return add_single_ctl_with_resume(mixer, 1,
865                                           snd_mbox1_src_switch_resume,
866                                           &snd_mbox1_src_switch, NULL);
867 }
868
869 /* Native Instruments device quirks */
870
871 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
872
873 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
874                                    struct snd_kcontrol *kctl)
875 {
876         struct usb_device *dev = mixer->chip->dev;
877         unsigned int pval = kctl->private_value;
878         u8 value;
879         int err;
880
881         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
882                               (pval >> 16) & 0xff,
883                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
884                               0, pval & 0xffff, &value, 1);
885         if (err < 0) {
886                 dev_err(&dev->dev,
887                         "unable to issue vendor read request (ret = %d)", err);
888                 return err;
889         }
890
891         kctl->private_value |= ((unsigned int)value << 24);
892         return 0;
893 }
894
895 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
896                                              struct snd_ctl_elem_value *ucontrol)
897 {
898         ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
899         return 0;
900 }
901
902 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
903 {
904         struct snd_usb_audio *chip = list->mixer->chip;
905         unsigned int pval = list->kctl->private_value;
906         int err;
907
908         err = snd_usb_lock_shutdown(chip);
909         if (err < 0)
910                 return err;
911         err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
912                               (pval >> 16) & 0xff,
913                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
914                               pval >> 24, pval & 0xffff, NULL, 0, 1000);
915         snd_usb_unlock_shutdown(chip);
916         return err;
917 }
918
919 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
920                                              struct snd_ctl_elem_value *ucontrol)
921 {
922         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
923         u8 oldval = (kcontrol->private_value >> 24) & 0xff;
924         u8 newval = ucontrol->value.integer.value[0];
925         int err;
926
927         if (oldval == newval)
928                 return 0;
929
930         kcontrol->private_value &= ~(0xff << 24);
931         kcontrol->private_value |= (unsigned int)newval << 24;
932         err = snd_ni_update_cur_val(list);
933         return err < 0 ? err : 1;
934 }
935
936 static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
937         {
938                 .name = "Direct Thru Channel A",
939                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
940         },
941         {
942                 .name = "Direct Thru Channel B",
943                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
944         },
945         {
946                 .name = "Phono Input Channel A",
947                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
948         },
949         {
950                 .name = "Phono Input Channel B",
951                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
952         },
953 };
954
955 static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
956         {
957                 .name = "Direct Thru Channel A",
958                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
959         },
960         {
961                 .name = "Direct Thru Channel B",
962                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
963         },
964         {
965                 .name = "Direct Thru Channel C",
966                 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
967         },
968         {
969                 .name = "Direct Thru Channel D",
970                 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
971         },
972         {
973                 .name = "Phono Input Channel A",
974                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
975         },
976         {
977                 .name = "Phono Input Channel B",
978                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
979         },
980         {
981                 .name = "Phono Input Channel C",
982                 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
983         },
984         {
985                 .name = "Phono Input Channel D",
986                 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
987         },
988 };
989
990 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
991                                               const struct snd_kcontrol_new *kc,
992                                               unsigned int count)
993 {
994         int i, err = 0;
995         struct snd_kcontrol_new template = {
996                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
997                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
998                 .get = snd_nativeinstruments_control_get,
999                 .put = snd_nativeinstruments_control_put,
1000                 .info = snd_ctl_boolean_mono_info,
1001         };
1002
1003         for (i = 0; i < count; i++) {
1004                 struct usb_mixer_elem_list *list;
1005
1006                 template.name = kc[i].name;
1007                 template.private_value = kc[i].private_value;
1008
1009                 err = add_single_ctl_with_resume(mixer, 0,
1010                                                  snd_ni_update_cur_val,
1011                                                  &template, &list);
1012                 if (err < 0)
1013                         break;
1014                 snd_ni_control_init_val(mixer, list->kctl);
1015         }
1016
1017         return err;
1018 }
1019
1020 /* M-Audio FastTrack Ultra quirks */
1021 /* FTU Effect switch (also used by C400/C600) */
1022 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
1023                                         struct snd_ctl_elem_info *uinfo)
1024 {
1025         static const char *const texts[8] = {
1026                 "Room 1", "Room 2", "Room 3", "Hall 1",
1027                 "Hall 2", "Plate", "Delay", "Echo"
1028         };
1029
1030         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1031 }
1032
1033 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
1034                                    struct snd_kcontrol *kctl)
1035 {
1036         struct usb_device *dev = mixer->chip->dev;
1037         unsigned int pval = kctl->private_value;
1038         int err;
1039         unsigned char value[2];
1040
1041         value[0] = 0x00;
1042         value[1] = 0x00;
1043
1044         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1045                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1046                               pval & 0xff00,
1047                               snd_usb_ctrl_intf(mixer->hostif) | ((pval & 0xff) << 8),
1048                               value, 2);
1049         if (err < 0)
1050                 return err;
1051
1052         kctl->private_value |= (unsigned int)value[0] << 24;
1053         return 0;
1054 }
1055
1056 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
1057                                         struct snd_ctl_elem_value *ucontrol)
1058 {
1059         ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
1060         return 0;
1061 }
1062
1063 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
1064 {
1065         struct snd_usb_audio *chip = list->mixer->chip;
1066         unsigned int pval = list->kctl->private_value;
1067         unsigned char value[2];
1068         int err;
1069
1070         value[0] = pval >> 24;
1071         value[1] = 0;
1072
1073         err = snd_usb_lock_shutdown(chip);
1074         if (err < 0)
1075                 return err;
1076         err = snd_usb_ctl_msg(chip->dev,
1077                               usb_sndctrlpipe(chip->dev, 0),
1078                               UAC_SET_CUR,
1079                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1080                               pval & 0xff00,
1081                               snd_usb_ctrl_intf(list->mixer->hostif) | ((pval & 0xff) << 8),
1082                               value, 2);
1083         snd_usb_unlock_shutdown(chip);
1084         return err;
1085 }
1086
1087 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
1088                                         struct snd_ctl_elem_value *ucontrol)
1089 {
1090         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1091         unsigned int pval = list->kctl->private_value;
1092         int cur_val, err, new_val;
1093
1094         cur_val = pval >> 24;
1095         new_val = ucontrol->value.enumerated.item[0];
1096         if (cur_val == new_val)
1097                 return 0;
1098
1099         kctl->private_value &= ~(0xff << 24);
1100         kctl->private_value |= new_val << 24;
1101         err = snd_ftu_eff_switch_update(list);
1102         return err < 0 ? err : 1;
1103 }
1104
1105 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
1106         int validx, int bUnitID)
1107 {
1108         static struct snd_kcontrol_new template = {
1109                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1110                 .name = "Effect Program Switch",
1111                 .index = 0,
1112                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1113                 .info = snd_ftu_eff_switch_info,
1114                 .get = snd_ftu_eff_switch_get,
1115                 .put = snd_ftu_eff_switch_put
1116         };
1117         struct usb_mixer_elem_list *list;
1118         int err;
1119
1120         err = add_single_ctl_with_resume(mixer, bUnitID,
1121                                          snd_ftu_eff_switch_update,
1122                                          &template, &list);
1123         if (err < 0)
1124                 return err;
1125         list->kctl->private_value = (validx << 8) | bUnitID;
1126         snd_ftu_eff_switch_init(mixer, list->kctl);
1127         return 0;
1128 }
1129
1130 /* Create volume controls for FTU devices*/
1131 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
1132 {
1133         char name[64];
1134         unsigned int control, cmask;
1135         int in, out, err;
1136
1137         const unsigned int id = 5;
1138         const int val_type = USB_MIXER_S16;
1139
1140         for (out = 0; out < 8; out++) {
1141                 control = out + 1;
1142                 for (in = 0; in < 8; in++) {
1143                         cmask = BIT(in);
1144                         snprintf(name, sizeof(name),
1145                                 "AIn%d - Out%d Capture Volume",
1146                                 in  + 1, out + 1);
1147                         err = snd_create_std_mono_ctl(mixer, id, control,
1148                                                         cmask, val_type, name,
1149                                                         &snd_usb_mixer_vol_tlv);
1150                         if (err < 0)
1151                                 return err;
1152                 }
1153                 for (in = 8; in < 16; in++) {
1154                         cmask = BIT(in);
1155                         snprintf(name, sizeof(name),
1156                                 "DIn%d - Out%d Playback Volume",
1157                                 in - 7, out + 1);
1158                         err = snd_create_std_mono_ctl(mixer, id, control,
1159                                                         cmask, val_type, name,
1160                                                         &snd_usb_mixer_vol_tlv);
1161                         if (err < 0)
1162                                 return err;
1163                 }
1164         }
1165
1166         return 0;
1167 }
1168
1169 /* This control needs a volume quirk, see mixer.c */
1170 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1171 {
1172         static const char name[] = "Effect Volume";
1173         const unsigned int id = 6;
1174         const int val_type = USB_MIXER_U8;
1175         const unsigned int control = 2;
1176         const unsigned int cmask = 0;
1177
1178         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1179                                         name, snd_usb_mixer_vol_tlv);
1180 }
1181
1182 /* This control needs a volume quirk, see mixer.c */
1183 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1184 {
1185         static const char name[] = "Effect Duration";
1186         const unsigned int id = 6;
1187         const int val_type = USB_MIXER_S16;
1188         const unsigned int control = 3;
1189         const unsigned int cmask = 0;
1190
1191         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1192                                         name, snd_usb_mixer_vol_tlv);
1193 }
1194
1195 /* This control needs a volume quirk, see mixer.c */
1196 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1197 {
1198         static const char name[] = "Effect Feedback Volume";
1199         const unsigned int id = 6;
1200         const int val_type = USB_MIXER_U8;
1201         const unsigned int control = 4;
1202         const unsigned int cmask = 0;
1203
1204         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1205                                         name, NULL);
1206 }
1207
1208 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1209 {
1210         unsigned int cmask;
1211         int err, ch;
1212         char name[48];
1213
1214         const unsigned int id = 7;
1215         const int val_type = USB_MIXER_S16;
1216         const unsigned int control = 7;
1217
1218         for (ch = 0; ch < 4; ++ch) {
1219                 cmask = BIT(ch);
1220                 snprintf(name, sizeof(name),
1221                         "Effect Return %d Volume", ch + 1);
1222                 err = snd_create_std_mono_ctl(mixer, id, control,
1223                                                 cmask, val_type, name,
1224                                                 snd_usb_mixer_vol_tlv);
1225                 if (err < 0)
1226                         return err;
1227         }
1228
1229         return 0;
1230 }
1231
1232 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1233 {
1234         unsigned int  cmask;
1235         int err, ch;
1236         char name[48];
1237
1238         const unsigned int id = 5;
1239         const int val_type = USB_MIXER_S16;
1240         const unsigned int control = 9;
1241
1242         for (ch = 0; ch < 8; ++ch) {
1243                 cmask = BIT(ch);
1244                 snprintf(name, sizeof(name),
1245                         "Effect Send AIn%d Volume", ch + 1);
1246                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1247                                                 val_type, name,
1248                                                 snd_usb_mixer_vol_tlv);
1249                 if (err < 0)
1250                         return err;
1251         }
1252         for (ch = 8; ch < 16; ++ch) {
1253                 cmask = BIT(ch);
1254                 snprintf(name, sizeof(name),
1255                         "Effect Send DIn%d Volume", ch - 7);
1256                 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1257                                                 val_type, name,
1258                                                 snd_usb_mixer_vol_tlv);
1259                 if (err < 0)
1260                         return err;
1261         }
1262         return 0;
1263 }
1264
1265 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1266 {
1267         int err;
1268
1269         err = snd_ftu_create_volume_ctls(mixer);
1270         if (err < 0)
1271                 return err;
1272
1273         err = snd_ftu_create_effect_switch(mixer, 1, 6);
1274         if (err < 0)
1275                 return err;
1276
1277         err = snd_ftu_create_effect_volume_ctl(mixer);
1278         if (err < 0)
1279                 return err;
1280
1281         err = snd_ftu_create_effect_duration_ctl(mixer);
1282         if (err < 0)
1283                 return err;
1284
1285         err = snd_ftu_create_effect_feedback_ctl(mixer);
1286         if (err < 0)
1287                 return err;
1288
1289         err = snd_ftu_create_effect_return_ctls(mixer);
1290         if (err < 0)
1291                 return err;
1292
1293         err = snd_ftu_create_effect_send_ctls(mixer);
1294         if (err < 0)
1295                 return err;
1296
1297         return 0;
1298 }
1299
1300 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1301                                unsigned char samplerate_id)
1302 {
1303         struct usb_mixer_interface *mixer;
1304         struct usb_mixer_elem_info *cval;
1305         int unitid = 12; /* SampleRate ExtensionUnit ID */
1306
1307         list_for_each_entry(mixer, &chip->mixer_list, list) {
1308                 if (mixer->id_elems[unitid]) {
1309                         cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1310                         snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1311                                                     cval->control << 8,
1312                                                     samplerate_id);
1313                         snd_usb_mixer_notify_id(mixer, unitid);
1314                         break;
1315                 }
1316         }
1317 }
1318
1319 /* M-Audio Fast Track C400/C600 */
1320 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1321 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1322 {
1323         char name[64];
1324         unsigned int cmask, offset;
1325         int out, chan, err;
1326         int num_outs = 0;
1327         int num_ins = 0;
1328
1329         const unsigned int id = 0x40;
1330         const int val_type = USB_MIXER_S16;
1331         const int control = 1;
1332
1333         switch (mixer->chip->usb_id) {
1334         case USB_ID(0x0763, 0x2030):
1335                 num_outs = 6;
1336                 num_ins = 4;
1337                 break;
1338         case USB_ID(0x0763, 0x2031):
1339                 num_outs = 8;
1340                 num_ins = 6;
1341                 break;
1342         }
1343
1344         for (chan = 0; chan < num_outs + num_ins; chan++) {
1345                 for (out = 0; out < num_outs; out++) {
1346                         if (chan < num_outs) {
1347                                 snprintf(name, sizeof(name),
1348                                         "PCM%d-Out%d Playback Volume",
1349                                         chan + 1, out + 1);
1350                         } else {
1351                                 snprintf(name, sizeof(name),
1352                                         "In%d-Out%d Playback Volume",
1353                                         chan - num_outs + 1, out + 1);
1354                         }
1355
1356                         cmask = (out == 0) ? 0 : BIT(out - 1);
1357                         offset = chan * num_outs;
1358                         err = snd_create_std_mono_ctl_offset(mixer, id, control,
1359                                                 cmask, val_type, offset, name,
1360                                                 &snd_usb_mixer_vol_tlv);
1361                         if (err < 0)
1362                                 return err;
1363                 }
1364         }
1365
1366         return 0;
1367 }
1368
1369 /* This control needs a volume quirk, see mixer.c */
1370 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1371 {
1372         static const char name[] = "Effect Volume";
1373         const unsigned int id = 0x43;
1374         const int val_type = USB_MIXER_U8;
1375         const unsigned int control = 3;
1376         const unsigned int cmask = 0;
1377
1378         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1379                                         name, snd_usb_mixer_vol_tlv);
1380 }
1381
1382 /* This control needs a volume quirk, see mixer.c */
1383 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1384 {
1385         static const char name[] = "Effect Duration";
1386         const unsigned int id = 0x43;
1387         const int val_type = USB_MIXER_S16;
1388         const unsigned int control = 4;
1389         const unsigned int cmask = 0;
1390
1391         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1392                                         name, snd_usb_mixer_vol_tlv);
1393 }
1394
1395 /* This control needs a volume quirk, see mixer.c */
1396 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1397 {
1398         static const char name[] = "Effect Feedback Volume";
1399         const unsigned int id = 0x43;
1400         const int val_type = USB_MIXER_U8;
1401         const unsigned int control = 5;
1402         const unsigned int cmask = 0;
1403
1404         return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1405                                         name, NULL);
1406 }
1407
1408 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1409 {
1410         char name[64];
1411         unsigned int cmask;
1412         int chan, err;
1413         int num_outs = 0;
1414         int num_ins = 0;
1415
1416         const unsigned int id = 0x42;
1417         const int val_type = USB_MIXER_S16;
1418         const int control = 1;
1419
1420         switch (mixer->chip->usb_id) {
1421         case USB_ID(0x0763, 0x2030):
1422                 num_outs = 6;
1423                 num_ins = 4;
1424                 break;
1425         case USB_ID(0x0763, 0x2031):
1426                 num_outs = 8;
1427                 num_ins = 6;
1428                 break;
1429         }
1430
1431         for (chan = 0; chan < num_outs + num_ins; chan++) {
1432                 if (chan < num_outs) {
1433                         snprintf(name, sizeof(name),
1434                                 "Effect Send DOut%d",
1435                                 chan + 1);
1436                 } else {
1437                         snprintf(name, sizeof(name),
1438                                 "Effect Send AIn%d",
1439                                 chan - num_outs + 1);
1440                 }
1441
1442                 cmask = (chan == 0) ? 0 : BIT(chan - 1);
1443                 err = snd_create_std_mono_ctl(mixer, id, control,
1444                                                 cmask, val_type, name,
1445                                                 &snd_usb_mixer_vol_tlv);
1446                 if (err < 0)
1447                         return err;
1448         }
1449
1450         return 0;
1451 }
1452
1453 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1454 {
1455         char name[64];
1456         unsigned int cmask;
1457         int chan, err;
1458         int num_outs = 0;
1459         int offset = 0;
1460
1461         const unsigned int id = 0x40;
1462         const int val_type = USB_MIXER_S16;
1463         const int control = 1;
1464
1465         switch (mixer->chip->usb_id) {
1466         case USB_ID(0x0763, 0x2030):
1467                 num_outs = 6;
1468                 offset = 0x3c;
1469                 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1470                 break;
1471         case USB_ID(0x0763, 0x2031):
1472                 num_outs = 8;
1473                 offset = 0x70;
1474                 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1475                 break;
1476         }
1477
1478         for (chan = 0; chan < num_outs; chan++) {
1479                 snprintf(name, sizeof(name),
1480                         "Effect Return %d",
1481                         chan + 1);
1482
1483                 cmask = (chan == 0) ? 0 :
1484                         BIT(chan + (chan % 2) * num_outs - 1);
1485                 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1486                                                 cmask, val_type, offset, name,
1487                                                 &snd_usb_mixer_vol_tlv);
1488                 if (err < 0)
1489                         return err;
1490         }
1491
1492         return 0;
1493 }
1494
1495 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1496 {
1497         int err;
1498
1499         err = snd_c400_create_vol_ctls(mixer);
1500         if (err < 0)
1501                 return err;
1502
1503         err = snd_c400_create_effect_vol_ctls(mixer);
1504         if (err < 0)
1505                 return err;
1506
1507         err = snd_c400_create_effect_ret_vol_ctls(mixer);
1508         if (err < 0)
1509                 return err;
1510
1511         err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1512         if (err < 0)
1513                 return err;
1514
1515         err = snd_c400_create_effect_volume_ctl(mixer);
1516         if (err < 0)
1517                 return err;
1518
1519         err = snd_c400_create_effect_duration_ctl(mixer);
1520         if (err < 0)
1521                 return err;
1522
1523         err = snd_c400_create_effect_feedback_ctl(mixer);
1524         if (err < 0)
1525                 return err;
1526
1527         return 0;
1528 }
1529
1530 /*
1531  * The mixer units for Ebox-44 are corrupt, and even where they
1532  * are valid they presents mono controls as L and R channels of
1533  * stereo. So we provide a good mixer here.
1534  */
1535 static const struct std_mono_table ebox44_table[] = {
1536         {
1537                 .unitid = 4,
1538                 .control = 1,
1539                 .cmask = 0x0,
1540                 .val_type = USB_MIXER_INV_BOOLEAN,
1541                 .name = "Headphone Playback Switch"
1542         },
1543         {
1544                 .unitid = 4,
1545                 .control = 2,
1546                 .cmask = 0x1,
1547                 .val_type = USB_MIXER_S16,
1548                 .name = "Headphone A Mix Playback Volume"
1549         },
1550         {
1551                 .unitid = 4,
1552                 .control = 2,
1553                 .cmask = 0x2,
1554                 .val_type = USB_MIXER_S16,
1555                 .name = "Headphone B Mix Playback Volume"
1556         },
1557
1558         {
1559                 .unitid = 7,
1560                 .control = 1,
1561                 .cmask = 0x0,
1562                 .val_type = USB_MIXER_INV_BOOLEAN,
1563                 .name = "Output Playback Switch"
1564         },
1565         {
1566                 .unitid = 7,
1567                 .control = 2,
1568                 .cmask = 0x1,
1569                 .val_type = USB_MIXER_S16,
1570                 .name = "Output A Playback Volume"
1571         },
1572         {
1573                 .unitid = 7,
1574                 .control = 2,
1575                 .cmask = 0x2,
1576                 .val_type = USB_MIXER_S16,
1577                 .name = "Output B Playback Volume"
1578         },
1579
1580         {
1581                 .unitid = 10,
1582                 .control = 1,
1583                 .cmask = 0x0,
1584                 .val_type = USB_MIXER_INV_BOOLEAN,
1585                 .name = "Input Capture Switch"
1586         },
1587         {
1588                 .unitid = 10,
1589                 .control = 2,
1590                 .cmask = 0x1,
1591                 .val_type = USB_MIXER_S16,
1592                 .name = "Input A Capture Volume"
1593         },
1594         {
1595                 .unitid = 10,
1596                 .control = 2,
1597                 .cmask = 0x2,
1598                 .val_type = USB_MIXER_S16,
1599                 .name = "Input B Capture Volume"
1600         },
1601
1602         {}
1603 };
1604
1605 /* Audio Advantage Micro II findings:
1606  *
1607  * Mapping spdif AES bits to vendor register.bit:
1608  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1609  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1610  * AES2: [0 0 0 0 0 0 0 0]
1611  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1612  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1613  *
1614  * power on values:
1615  * r2: 0x10
1616  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1617  *           just after it to 0xa0, presumably it disables/mutes some analog
1618  *           parts when there is no audio.)
1619  * r9: 0x28
1620  *
1621  * Optical transmitter on/off:
1622  * vendor register.bit: 9.1
1623  * 0 - on (0x28 register value)
1624  * 1 - off (0x2a register value)
1625  *
1626  */
1627 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1628         struct snd_ctl_elem_info *uinfo)
1629 {
1630         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1631         uinfo->count = 1;
1632         return 0;
1633 }
1634
1635 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1636         struct snd_ctl_elem_value *ucontrol)
1637 {
1638         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1639         struct snd_usb_audio *chip = list->mixer->chip;
1640         int err;
1641         struct usb_interface *iface;
1642         struct usb_host_interface *alts;
1643         unsigned int ep;
1644         unsigned char data[3];
1645         int rate;
1646
1647         err = snd_usb_lock_shutdown(chip);
1648         if (err < 0)
1649                 return err;
1650
1651         ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1652         ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1653         ucontrol->value.iec958.status[2] = 0x00;
1654
1655         /* use known values for that card: interface#1 altsetting#1 */
1656         iface = usb_ifnum_to_if(chip->dev, 1);
1657         if (!iface || iface->num_altsetting < 2) {
1658                 err = -EINVAL;
1659                 goto end;
1660         }
1661         alts = &iface->altsetting[1];
1662         if (get_iface_desc(alts)->bNumEndpoints < 1) {
1663                 err = -EINVAL;
1664                 goto end;
1665         }
1666         ep = get_endpoint(alts, 0)->bEndpointAddress;
1667
1668         err = snd_usb_ctl_msg(chip->dev,
1669                         usb_rcvctrlpipe(chip->dev, 0),
1670                         UAC_GET_CUR,
1671                         USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1672                         UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1673                         ep,
1674                         data,
1675                         sizeof(data));
1676         if (err < 0)
1677                 goto end;
1678
1679         rate = data[0] | (data[1] << 8) | (data[2] << 16);
1680         ucontrol->value.iec958.status[3] = (rate == 48000) ?
1681                         IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1682
1683         err = 0;
1684  end:
1685         snd_usb_unlock_shutdown(chip);
1686         return err;
1687 }
1688
1689 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1690 {
1691         struct snd_usb_audio *chip = list->mixer->chip;
1692         unsigned int pval = list->kctl->private_value;
1693         u8 reg;
1694         int err;
1695
1696         err = snd_usb_lock_shutdown(chip);
1697         if (err < 0)
1698                 return err;
1699
1700         reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1701         err = snd_usb_ctl_msg(chip->dev,
1702                         usb_sndctrlpipe(chip->dev, 0),
1703                         UAC_SET_CUR,
1704                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1705                         reg,
1706                         2,
1707                         NULL,
1708                         0);
1709         if (err < 0)
1710                 goto end;
1711
1712         reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1713         reg |= (pval >> 12) & 0x0f;
1714         err = snd_usb_ctl_msg(chip->dev,
1715                         usb_sndctrlpipe(chip->dev, 0),
1716                         UAC_SET_CUR,
1717                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1718                         reg,
1719                         3,
1720                         NULL,
1721                         0);
1722         if (err < 0)
1723                 goto end;
1724
1725  end:
1726         snd_usb_unlock_shutdown(chip);
1727         return err;
1728 }
1729
1730 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1731         struct snd_ctl_elem_value *ucontrol)
1732 {
1733         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1734         unsigned int pval, pval_old;
1735         int err;
1736
1737         pval = pval_old = kcontrol->private_value;
1738         pval &= 0xfffff0f0;
1739         pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1740         pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1741
1742         pval &= 0xffff0fff;
1743         pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1744
1745         /* The frequency bits in AES3 cannot be set via register access. */
1746
1747         /* Silently ignore any bits from the request that cannot be set. */
1748
1749         if (pval == pval_old)
1750                 return 0;
1751
1752         kcontrol->private_value = pval;
1753         err = snd_microii_spdif_default_update(list);
1754         return err < 0 ? err : 1;
1755 }
1756
1757 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1758         struct snd_ctl_elem_value *ucontrol)
1759 {
1760         ucontrol->value.iec958.status[0] = 0x0f;
1761         ucontrol->value.iec958.status[1] = 0xff;
1762         ucontrol->value.iec958.status[2] = 0x00;
1763         ucontrol->value.iec958.status[3] = 0x00;
1764
1765         return 0;
1766 }
1767
1768 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1769         struct snd_ctl_elem_value *ucontrol)
1770 {
1771         ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1772
1773         return 0;
1774 }
1775
1776 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1777 {
1778         struct snd_usb_audio *chip = list->mixer->chip;
1779         u8 reg = list->kctl->private_value;
1780         int err;
1781
1782         err = snd_usb_lock_shutdown(chip);
1783         if (err < 0)
1784                 return err;
1785
1786         err = snd_usb_ctl_msg(chip->dev,
1787                         usb_sndctrlpipe(chip->dev, 0),
1788                         UAC_SET_CUR,
1789                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1790                         reg,
1791                         9,
1792                         NULL,
1793                         0);
1794
1795         snd_usb_unlock_shutdown(chip);
1796         return err;
1797 }
1798
1799 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1800         struct snd_ctl_elem_value *ucontrol)
1801 {
1802         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1803         u8 reg;
1804         int err;
1805
1806         reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1807         if (reg != list->kctl->private_value)
1808                 return 0;
1809
1810         kcontrol->private_value = reg;
1811         err = snd_microii_spdif_switch_update(list);
1812         return err < 0 ? err : 1;
1813 }
1814
1815 static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1816         {
1817                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1818                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1819                 .info =     snd_microii_spdif_info,
1820                 .get =      snd_microii_spdif_default_get,
1821                 .put =      snd_microii_spdif_default_put,
1822                 .private_value = 0x00000100UL,/* reset value */
1823         },
1824         {
1825                 .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1826                 .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1827                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1828                 .info =     snd_microii_spdif_info,
1829                 .get =      snd_microii_spdif_mask_get,
1830         },
1831         {
1832                 .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1833                 .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1834                 .info =     snd_ctl_boolean_mono_info,
1835                 .get =      snd_microii_spdif_switch_get,
1836                 .put =      snd_microii_spdif_switch_put,
1837                 .private_value = 0x00000028UL,/* reset value */
1838         }
1839 };
1840
1841 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1842 {
1843         int err, i;
1844         static const usb_mixer_elem_resume_func_t resume_funcs[] = {
1845                 snd_microii_spdif_default_update,
1846                 NULL,
1847                 snd_microii_spdif_switch_update
1848         };
1849
1850         for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1851                 err = add_single_ctl_with_resume(mixer, 0,
1852                                                  resume_funcs[i],
1853                                                  &snd_microii_mixer_spdif[i],
1854                                                  NULL);
1855                 if (err < 0)
1856                         return err;
1857         }
1858
1859         return 0;
1860 }
1861
1862 /* Creative Sound Blaster E1 */
1863
1864 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1865                                           struct snd_ctl_elem_value *ucontrol)
1866 {
1867         ucontrol->value.integer.value[0] = kcontrol->private_value;
1868         return 0;
1869 }
1870
1871 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1872                                              unsigned char state)
1873 {
1874         struct snd_usb_audio *chip = mixer->chip;
1875         int err;
1876         unsigned char buff[2];
1877
1878         buff[0] = 0x02;
1879         buff[1] = state ? 0x02 : 0x00;
1880
1881         err = snd_usb_lock_shutdown(chip);
1882         if (err < 0)
1883                 return err;
1884         err = snd_usb_ctl_msg(chip->dev,
1885                         usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1886                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1887                         0x0202, 3, buff, 2);
1888         snd_usb_unlock_shutdown(chip);
1889         return err;
1890 }
1891
1892 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1893                                           struct snd_ctl_elem_value *ucontrol)
1894 {
1895         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1896         unsigned char value = !!ucontrol->value.integer.value[0];
1897         int err;
1898
1899         if (kcontrol->private_value == value)
1900                 return 0;
1901         kcontrol->private_value = value;
1902         err = snd_soundblaster_e1_switch_update(list->mixer, value);
1903         return err < 0 ? err : 1;
1904 }
1905
1906 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1907 {
1908         return snd_soundblaster_e1_switch_update(list->mixer,
1909                                                  list->kctl->private_value);
1910 }
1911
1912 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1913                                            struct snd_ctl_elem_info *uinfo)
1914 {
1915         static const char *const texts[2] = {
1916                 "Mic", "Aux"
1917         };
1918
1919         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1920 }
1921
1922 static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
1923         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1924         .name = "Input Source",
1925         .info = snd_soundblaster_e1_switch_info,
1926         .get = snd_soundblaster_e1_switch_get,
1927         .put = snd_soundblaster_e1_switch_put,
1928         .private_value = 0,
1929 };
1930
1931 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1932 {
1933         return add_single_ctl_with_resume(mixer, 0,
1934                                           snd_soundblaster_e1_switch_resume,
1935                                           &snd_soundblaster_e1_input_switch,
1936                                           NULL);
1937 }
1938
1939 /*
1940  * Dell WD15 dock jack detection
1941  *
1942  * The WD15 contains an ALC4020 USB audio controller and ALC3263 audio codec
1943  * from Realtek. It is a UAC 1 device, and UAC 1 does not support jack
1944  * detection. Instead, jack detection works by sending HD Audio commands over
1945  * vendor-type USB messages.
1946  */
1947
1948 #define HDA_VERB_CMD(V, N, D) (((N) << 20) | ((V) << 8) | (D))
1949
1950 #define REALTEK_HDA_VALUE 0x0038
1951
1952 #define REALTEK_HDA_SET         62
1953 #define REALTEK_MANUAL_MODE     72
1954 #define REALTEK_HDA_GET_OUT     88
1955 #define REALTEK_HDA_GET_IN      89
1956
1957 #define REALTEK_AUDIO_FUNCTION_GROUP    0x01
1958 #define REALTEK_LINE1                   0x1a
1959 #define REALTEK_VENDOR_REGISTERS        0x20
1960 #define REALTEK_HP_OUT                  0x21
1961
1962 #define REALTEK_CBJ_CTRL2 0x50
1963
1964 #define REALTEK_JACK_INTERRUPT_NODE 5
1965
1966 #define REALTEK_MIC_FLAG 0x100
1967
1968 static int realtek_hda_set(struct snd_usb_audio *chip, u32 cmd)
1969 {
1970         struct usb_device *dev = chip->dev;
1971         __be32 buf = cpu_to_be32(cmd);
1972
1973         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_SET,
1974                                USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
1975                                REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1976 }
1977
1978 static int realtek_hda_get(struct snd_usb_audio *chip, u32 cmd, u32 *value)
1979 {
1980         struct usb_device *dev = chip->dev;
1981         int err;
1982         __be32 buf = cpu_to_be32(cmd);
1983
1984         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_GET_OUT,
1985                               USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
1986                               REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1987         if (err < 0)
1988                 return err;
1989         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), REALTEK_HDA_GET_IN,
1990                               USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
1991                               REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1992         if (err < 0)
1993                 return err;
1994
1995         *value = be32_to_cpu(buf);
1996         return 0;
1997 }
1998
1999 static int realtek_ctl_connector_get(struct snd_kcontrol *kcontrol,
2000                                      struct snd_ctl_elem_value *ucontrol)
2001 {
2002         struct usb_mixer_elem_info *cval = kcontrol->private_data;
2003         struct snd_usb_audio *chip = cval->head.mixer->chip;
2004         u32 pv = kcontrol->private_value;
2005         u32 node_id = pv & 0xff;
2006         u32 sense;
2007         u32 cbj_ctrl2;
2008         bool presence;
2009         int err;
2010
2011         err = snd_usb_lock_shutdown(chip);
2012         if (err < 0)
2013                 return err;
2014         err = realtek_hda_get(chip,
2015                               HDA_VERB_CMD(AC_VERB_GET_PIN_SENSE, node_id, 0),
2016                               &sense);
2017         if (err < 0)
2018                 goto err;
2019         if (pv & REALTEK_MIC_FLAG) {
2020                 err = realtek_hda_set(chip,
2021                                       HDA_VERB_CMD(AC_VERB_SET_COEF_INDEX,
2022                                                    REALTEK_VENDOR_REGISTERS,
2023                                                    REALTEK_CBJ_CTRL2));
2024                 if (err < 0)
2025                         goto err;
2026                 err = realtek_hda_get(chip,
2027                                       HDA_VERB_CMD(AC_VERB_GET_PROC_COEF,
2028                                                    REALTEK_VENDOR_REGISTERS, 0),
2029                                       &cbj_ctrl2);
2030                 if (err < 0)
2031                         goto err;
2032         }
2033 err:
2034         snd_usb_unlock_shutdown(chip);
2035         if (err < 0)
2036                 return err;
2037
2038         presence = sense & AC_PINSENSE_PRESENCE;
2039         if (pv & REALTEK_MIC_FLAG)
2040                 presence = presence && (cbj_ctrl2 & 0x0070) == 0x0070;
2041         ucontrol->value.integer.value[0] = presence;
2042         return 0;
2043 }
2044
2045 static const struct snd_kcontrol_new realtek_connector_ctl_ro = {
2046         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2047         .name = "", /* will be filled later manually */
2048         .access = SNDRV_CTL_ELEM_ACCESS_READ,
2049         .info = snd_ctl_boolean_mono_info,
2050         .get = realtek_ctl_connector_get,
2051 };
2052
2053 static int realtek_resume_jack(struct usb_mixer_elem_list *list)
2054 {
2055         snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2056                        &list->kctl->id);
2057         return 0;
2058 }
2059
2060 static int realtek_add_jack(struct usb_mixer_interface *mixer,
2061                             char *name, u32 val)
2062 {
2063         struct usb_mixer_elem_info *cval;
2064         struct snd_kcontrol *kctl;
2065
2066         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2067         if (!cval)
2068                 return -ENOMEM;
2069         snd_usb_mixer_elem_init_std(&cval->head, mixer,
2070                                     REALTEK_JACK_INTERRUPT_NODE);
2071         cval->head.resume = realtek_resume_jack;
2072         cval->val_type = USB_MIXER_BOOLEAN;
2073         cval->channels = 1;
2074         cval->min = 0;
2075         cval->max = 1;
2076         kctl = snd_ctl_new1(&realtek_connector_ctl_ro, cval);
2077         if (!kctl) {
2078                 kfree(cval);
2079                 return -ENOMEM;
2080         }
2081         kctl->private_value = val;
2082         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2083         kctl->private_free = snd_usb_mixer_elem_free;
2084         return snd_usb_mixer_add_control(&cval->head, kctl);
2085 }
2086
2087 static int dell_dock_mixer_create(struct usb_mixer_interface *mixer)
2088 {
2089         int err;
2090         struct usb_device *dev = mixer->chip->dev;
2091
2092         /* Power down the audio codec to avoid loud pops in the next step. */
2093         realtek_hda_set(mixer->chip,
2094                         HDA_VERB_CMD(AC_VERB_SET_POWER_STATE,
2095                                      REALTEK_AUDIO_FUNCTION_GROUP,
2096                                      AC_PWRST_D3));
2097
2098         /*
2099          * Turn off 'manual mode' in case it was enabled. This removes the need
2100          * to power cycle the dock after it was attached to a Windows machine.
2101          */
2102         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_MANUAL_MODE,
2103                         USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2104                         0, 0, NULL, 0);
2105
2106         err = realtek_add_jack(mixer, "Line Out Jack", REALTEK_LINE1);
2107         if (err < 0)
2108                 return err;
2109         err = realtek_add_jack(mixer, "Headphone Jack", REALTEK_HP_OUT);
2110         if (err < 0)
2111                 return err;
2112         err = realtek_add_jack(mixer, "Headset Mic Jack",
2113                                REALTEK_HP_OUT | REALTEK_MIC_FLAG);
2114         if (err < 0)
2115                 return err;
2116         return 0;
2117 }
2118
2119 static void dell_dock_init_vol(struct usb_mixer_interface *mixer, int ch, int id)
2120 {
2121         struct snd_usb_audio *chip = mixer->chip;
2122         u16 buf = 0;
2123
2124         snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
2125                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
2126                         (UAC_FU_VOLUME << 8) | ch,
2127                         snd_usb_ctrl_intf(mixer->hostif) | (id << 8),
2128                         &buf, 2);
2129 }
2130
2131 static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
2132 {
2133         /* fix to 0dB playback volumes */
2134         dell_dock_init_vol(mixer, 1, 16);
2135         dell_dock_init_vol(mixer, 2, 16);
2136         dell_dock_init_vol(mixer, 1, 19);
2137         dell_dock_init_vol(mixer, 2, 19);
2138         return 0;
2139 }
2140
2141 /* RME Class Compliant device quirks */
2142
2143 #define SND_RME_GET_STATUS1                     23
2144 #define SND_RME_GET_CURRENT_FREQ                17
2145 #define SND_RME_CLK_SYSTEM_SHIFT                16
2146 #define SND_RME_CLK_SYSTEM_MASK                 0x1f
2147 #define SND_RME_CLK_AES_SHIFT                   8
2148 #define SND_RME_CLK_SPDIF_SHIFT                 12
2149 #define SND_RME_CLK_AES_SPDIF_MASK              0xf
2150 #define SND_RME_CLK_SYNC_SHIFT                  6
2151 #define SND_RME_CLK_SYNC_MASK                   0x3
2152 #define SND_RME_CLK_FREQMUL_SHIFT               18
2153 #define SND_RME_CLK_FREQMUL_MASK                0x7
2154 #define SND_RME_CLK_SYSTEM(x) \
2155         ((x >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
2156 #define SND_RME_CLK_AES(x) \
2157         ((x >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2158 #define SND_RME_CLK_SPDIF(x) \
2159         ((x >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2160 #define SND_RME_CLK_SYNC(x) \
2161         ((x >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
2162 #define SND_RME_CLK_FREQMUL(x) \
2163         ((x >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
2164 #define SND_RME_CLK_AES_LOCK                    0x1
2165 #define SND_RME_CLK_AES_SYNC                    0x4
2166 #define SND_RME_CLK_SPDIF_LOCK                  0x2
2167 #define SND_RME_CLK_SPDIF_SYNC                  0x8
2168 #define SND_RME_SPDIF_IF_SHIFT                  4
2169 #define SND_RME_SPDIF_FORMAT_SHIFT              5
2170 #define SND_RME_BINARY_MASK                     0x1
2171 #define SND_RME_SPDIF_IF(x) \
2172         ((x >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
2173 #define SND_RME_SPDIF_FORMAT(x) \
2174         ((x >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
2175
2176 static const u32 snd_rme_rate_table[] = {
2177         32000, 44100, 48000, 50000,
2178         64000, 88200, 96000, 100000,
2179         128000, 176400, 192000, 200000,
2180         256000, 352800, 384000, 400000,
2181         512000, 705600, 768000, 800000
2182 };
2183 /* maximum number of items for AES and S/PDIF rates for above table */
2184 #define SND_RME_RATE_IDX_AES_SPDIF_NUM          12
2185
2186 enum snd_rme_domain {
2187         SND_RME_DOMAIN_SYSTEM,
2188         SND_RME_DOMAIN_AES,
2189         SND_RME_DOMAIN_SPDIF
2190 };
2191
2192 enum snd_rme_clock_status {
2193         SND_RME_CLOCK_NOLOCK,
2194         SND_RME_CLOCK_LOCK,
2195         SND_RME_CLOCK_SYNC
2196 };
2197
2198 static int snd_rme_read_value(struct snd_usb_audio *chip,
2199                               unsigned int item,
2200                               u32 *value)
2201 {
2202         struct usb_device *dev = chip->dev;
2203         int err;
2204
2205         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
2206                               item,
2207                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2208                               0, 0,
2209                               value, sizeof(*value));
2210         if (err < 0)
2211                 dev_err(&dev->dev,
2212                         "unable to issue vendor read request %d (ret = %d)",
2213                         item, err);
2214         return err;
2215 }
2216
2217 static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
2218                                u32 *status1)
2219 {
2220         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2221         struct snd_usb_audio *chip = list->mixer->chip;
2222         int err;
2223
2224         err = snd_usb_lock_shutdown(chip);
2225         if (err < 0)
2226                 return err;
2227         err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
2228         snd_usb_unlock_shutdown(chip);
2229         return err;
2230 }
2231
2232 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
2233                             struct snd_ctl_elem_value *ucontrol)
2234 {
2235         u32 status1;
2236         u32 rate = 0;
2237         int idx;
2238         int err;
2239
2240         err = snd_rme_get_status1(kcontrol, &status1);
2241         if (err < 0)
2242                 return err;
2243         switch (kcontrol->private_value) {
2244         case SND_RME_DOMAIN_SYSTEM:
2245                 idx = SND_RME_CLK_SYSTEM(status1);
2246                 if (idx < ARRAY_SIZE(snd_rme_rate_table))
2247                         rate = snd_rme_rate_table[idx];
2248                 break;
2249         case SND_RME_DOMAIN_AES:
2250                 idx = SND_RME_CLK_AES(status1);
2251                 if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2252                         rate = snd_rme_rate_table[idx];
2253                 break;
2254         case SND_RME_DOMAIN_SPDIF:
2255                 idx = SND_RME_CLK_SPDIF(status1);
2256                 if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2257                         rate = snd_rme_rate_table[idx];
2258                 break;
2259         default:
2260                 return -EINVAL;
2261         }
2262         ucontrol->value.integer.value[0] = rate;
2263         return 0;
2264 }
2265
2266 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
2267                                   struct snd_ctl_elem_value *ucontrol)
2268 {
2269         u32 status1;
2270         int idx = SND_RME_CLOCK_NOLOCK;
2271         int err;
2272
2273         err = snd_rme_get_status1(kcontrol, &status1);
2274         if (err < 0)
2275                 return err;
2276         switch (kcontrol->private_value) {
2277         case SND_RME_DOMAIN_AES:  /* AES */
2278                 if (status1 & SND_RME_CLK_AES_SYNC)
2279                         idx = SND_RME_CLOCK_SYNC;
2280                 else if (status1 & SND_RME_CLK_AES_LOCK)
2281                         idx = SND_RME_CLOCK_LOCK;
2282                 break;
2283         case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
2284                 if (status1 & SND_RME_CLK_SPDIF_SYNC)
2285                         idx = SND_RME_CLOCK_SYNC;
2286                 else if (status1 & SND_RME_CLK_SPDIF_LOCK)
2287                         idx = SND_RME_CLOCK_LOCK;
2288                 break;
2289         default:
2290                 return -EINVAL;
2291         }
2292         ucontrol->value.enumerated.item[0] = idx;
2293         return 0;
2294 }
2295
2296 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
2297                                 struct snd_ctl_elem_value *ucontrol)
2298 {
2299         u32 status1;
2300         int err;
2301
2302         err = snd_rme_get_status1(kcontrol, &status1);
2303         if (err < 0)
2304                 return err;
2305         ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
2306         return 0;
2307 }
2308
2309 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
2310                                     struct snd_ctl_elem_value *ucontrol)
2311 {
2312         u32 status1;
2313         int err;
2314
2315         err = snd_rme_get_status1(kcontrol, &status1);
2316         if (err < 0)
2317                 return err;
2318         ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
2319         return 0;
2320 }
2321
2322 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2323                                    struct snd_ctl_elem_value *ucontrol)
2324 {
2325         u32 status1;
2326         int err;
2327
2328         err = snd_rme_get_status1(kcontrol, &status1);
2329         if (err < 0)
2330                 return err;
2331         ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2332         return 0;
2333 }
2334
2335 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2336                                     struct snd_ctl_elem_value *ucontrol)
2337 {
2338         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2339         struct snd_usb_audio *chip = list->mixer->chip;
2340         u32 status1;
2341         const u64 num = 104857600000000ULL;
2342         u32 den;
2343         unsigned int freq;
2344         int err;
2345
2346         err = snd_usb_lock_shutdown(chip);
2347         if (err < 0)
2348                 return err;
2349         err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2350         if (err < 0)
2351                 goto end;
2352         err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2353         if (err < 0)
2354                 goto end;
2355         freq = (den == 0) ? 0 : div64_u64(num, den);
2356         freq <<= SND_RME_CLK_FREQMUL(status1);
2357         ucontrol->value.integer.value[0] = freq;
2358
2359 end:
2360         snd_usb_unlock_shutdown(chip);
2361         return err;
2362 }
2363
2364 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2365                              struct snd_ctl_elem_info *uinfo)
2366 {
2367         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2368         uinfo->count = 1;
2369         switch (kcontrol->private_value) {
2370         case SND_RME_DOMAIN_SYSTEM:
2371                 uinfo->value.integer.min = 32000;
2372                 uinfo->value.integer.max = 800000;
2373                 break;
2374         case SND_RME_DOMAIN_AES:
2375         case SND_RME_DOMAIN_SPDIF:
2376         default:
2377                 uinfo->value.integer.min = 0;
2378                 uinfo->value.integer.max = 200000;
2379         }
2380         uinfo->value.integer.step = 0;
2381         return 0;
2382 }
2383
2384 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2385                                    struct snd_ctl_elem_info *uinfo)
2386 {
2387         static const char *const sync_states[] = {
2388                 "No Lock", "Lock", "Sync"
2389         };
2390
2391         return snd_ctl_enum_info(uinfo, 1,
2392                                  ARRAY_SIZE(sync_states), sync_states);
2393 }
2394
2395 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2396                                  struct snd_ctl_elem_info *uinfo)
2397 {
2398         static const char *const spdif_if[] = {
2399                 "Coaxial", "Optical"
2400         };
2401
2402         return snd_ctl_enum_info(uinfo, 1,
2403                                  ARRAY_SIZE(spdif_if), spdif_if);
2404 }
2405
2406 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2407                                      struct snd_ctl_elem_info *uinfo)
2408 {
2409         static const char *const optical_type[] = {
2410                 "Consumer", "Professional"
2411         };
2412
2413         return snd_ctl_enum_info(uinfo, 1,
2414                                  ARRAY_SIZE(optical_type), optical_type);
2415 }
2416
2417 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2418                                     struct snd_ctl_elem_info *uinfo)
2419 {
2420         static const char *const sync_sources[] = {
2421                 "Internal", "AES", "SPDIF", "Internal"
2422         };
2423
2424         return snd_ctl_enum_info(uinfo, 1,
2425                                  ARRAY_SIZE(sync_sources), sync_sources);
2426 }
2427
2428 static const struct snd_kcontrol_new snd_rme_controls[] = {
2429         {
2430                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2431                 .name = "AES Rate",
2432                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2433                 .info = snd_rme_rate_info,
2434                 .get = snd_rme_rate_get,
2435                 .private_value = SND_RME_DOMAIN_AES
2436         },
2437         {
2438                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2439                 .name = "AES Sync",
2440                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2441                 .info = snd_rme_sync_state_info,
2442                 .get = snd_rme_sync_state_get,
2443                 .private_value = SND_RME_DOMAIN_AES
2444         },
2445         {
2446                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2447                 .name = "SPDIF Rate",
2448                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2449                 .info = snd_rme_rate_info,
2450                 .get = snd_rme_rate_get,
2451                 .private_value = SND_RME_DOMAIN_SPDIF
2452         },
2453         {
2454                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2455                 .name = "SPDIF Sync",
2456                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2457                 .info = snd_rme_sync_state_info,
2458                 .get = snd_rme_sync_state_get,
2459                 .private_value = SND_RME_DOMAIN_SPDIF
2460         },
2461         {
2462                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2463                 .name = "SPDIF Interface",
2464                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2465                 .info = snd_rme_spdif_if_info,
2466                 .get = snd_rme_spdif_if_get,
2467         },
2468         {
2469                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2470                 .name = "SPDIF Format",
2471                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2472                 .info = snd_rme_spdif_format_info,
2473                 .get = snd_rme_spdif_format_get,
2474         },
2475         {
2476                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2477                 .name = "Sync Source",
2478                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2479                 .info = snd_rme_sync_source_info,
2480                 .get = snd_rme_sync_source_get
2481         },
2482         {
2483                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2484                 .name = "System Rate",
2485                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2486                 .info = snd_rme_rate_info,
2487                 .get = snd_rme_rate_get,
2488                 .private_value = SND_RME_DOMAIN_SYSTEM
2489         },
2490         {
2491                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2492                 .name = "Current Frequency",
2493                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2494                 .info = snd_rme_rate_info,
2495                 .get = snd_rme_current_freq_get
2496         }
2497 };
2498
2499 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2500 {
2501         int err, i;
2502
2503         for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2504                 err = add_single_ctl_with_resume(mixer, 0,
2505                                                  NULL,
2506                                                  &snd_rme_controls[i],
2507                                                  NULL);
2508                 if (err < 0)
2509                         return err;
2510         }
2511
2512         return 0;
2513 }
2514
2515 /*
2516  * RME Babyface Pro (FS)
2517  *
2518  * These devices exposes a couple of DSP functions via request to EP0.
2519  * Switches are available via control registers, while routing is controlled
2520  * by controlling the volume on each possible crossing point.
2521  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2522  * 0dB being at dec. 32768.
2523  */
2524 enum {
2525         SND_BBFPRO_CTL_REG1 = 0,
2526         SND_BBFPRO_CTL_REG2
2527 };
2528
2529 #define SND_BBFPRO_CTL_REG_MASK 1
2530 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2531 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2532 #define SND_BBFPRO_CTL_VAL_MASK 1
2533 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2534 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2535 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2536 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2537 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2538 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2539 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2540 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2541 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2542 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2543 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2544 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2545
2546 #define SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET 992
2547 #define SND_BBFPRO_MIXER_IDX_MASK 0x3ff
2548 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2549 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2550 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2551 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2552
2553 #define SND_BBFPRO_GAIN_CHANNEL_MASK 0x03
2554 #define SND_BBFPRO_GAIN_CHANNEL_SHIFT 7
2555 #define SND_BBFPRO_GAIN_VAL_MASK 0x7f
2556 #define SND_BBFPRO_GAIN_VAL_MIN 0
2557 #define SND_BBFPRO_GAIN_VAL_MIC_MAX 65
2558 #define SND_BBFPRO_GAIN_VAL_LINE_MAX 18 // 9db in 0.5db incraments
2559
2560 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2561 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2562 #define SND_BBFPRO_USBREQ_GAIN 0x1a
2563 #define SND_BBFPRO_USBREQ_MIXER 0x12
2564
2565 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2566                                  u8 index, u8 value)
2567 {
2568         int err;
2569         u16 usb_req, usb_idx, usb_val;
2570         struct snd_usb_audio *chip = mixer->chip;
2571
2572         err = snd_usb_lock_shutdown(chip);
2573         if (err < 0)
2574                 return err;
2575
2576         if (reg == SND_BBFPRO_CTL_REG1) {
2577                 usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2578                 if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2579                         usb_idx = 3;
2580                         usb_val = value ? 3 : 0;
2581                 } else {
2582                         usb_idx = BIT(index);
2583                         usb_val = value ? usb_idx : 0;
2584                 }
2585         } else {
2586                 usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2587                 usb_idx = BIT(index);
2588                 usb_val = value ? usb_idx : 0;
2589         }
2590
2591         err = snd_usb_ctl_msg(chip->dev,
2592                               usb_sndctrlpipe(chip->dev, 0), usb_req,
2593                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2594                               usb_val, usb_idx, NULL, 0);
2595
2596         snd_usb_unlock_shutdown(chip);
2597         return err;
2598 }
2599
2600 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2601                               struct snd_ctl_elem_value *ucontrol)
2602 {
2603         u8 reg, idx, val;
2604         int pv;
2605
2606         pv = kcontrol->private_value;
2607         reg = pv & SND_BBFPRO_CTL_REG_MASK;
2608         idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2609         val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2610
2611         if ((reg == SND_BBFPRO_CTL_REG1 &&
2612              idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2613             (reg == SND_BBFPRO_CTL_REG2 &&
2614             (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2615              idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2616                 ucontrol->value.enumerated.item[0] = val;
2617         } else {
2618                 ucontrol->value.integer.value[0] = val;
2619         }
2620         return 0;
2621 }
2622
2623 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2624                                struct snd_ctl_elem_info *uinfo)
2625 {
2626         u8 reg, idx;
2627         int pv;
2628
2629         pv = kcontrol->private_value;
2630         reg = pv & SND_BBFPRO_CTL_REG_MASK;
2631         idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2632
2633         if (reg == SND_BBFPRO_CTL_REG1 &&
2634             idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2635                 static const char * const texts[2] = {
2636                         "AutoSync",
2637                         "Internal"
2638                 };
2639                 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2640         } else if (reg == SND_BBFPRO_CTL_REG2 &&
2641                    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2642                     idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
2643                 static const char * const texts[2] = {
2644                         "-10dBV",
2645                         "+4dBu"
2646                 };
2647                 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2648         }
2649
2650         uinfo->count = 1;
2651         uinfo->value.integer.min = 0;
2652         uinfo->value.integer.max = 1;
2653         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2654         return 0;
2655 }
2656
2657 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
2658                               struct snd_ctl_elem_value *ucontrol)
2659 {
2660         int err;
2661         u8 reg, idx;
2662         int old_value, pv, val;
2663
2664         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2665         struct usb_mixer_interface *mixer = list->mixer;
2666
2667         pv = kcontrol->private_value;
2668         reg = pv & SND_BBFPRO_CTL_REG_MASK;
2669         idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2670         old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2671
2672         if ((reg == SND_BBFPRO_CTL_REG1 &&
2673              idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2674             (reg == SND_BBFPRO_CTL_REG2 &&
2675             (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2676              idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2677                 val = ucontrol->value.enumerated.item[0];
2678         } else {
2679                 val = ucontrol->value.integer.value[0];
2680         }
2681
2682         if (val > 1)
2683                 return -EINVAL;
2684
2685         if (val == old_value)
2686                 return 0;
2687
2688         kcontrol->private_value = reg
2689                 | ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
2690                 | ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
2691
2692         err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
2693         return err < 0 ? err : 1;
2694 }
2695
2696 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
2697 {
2698         u8 reg, idx;
2699         int value, pv;
2700
2701         pv = list->kctl->private_value;
2702         reg = pv & SND_BBFPRO_CTL_REG_MASK;
2703         idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2704         value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2705
2706         return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
2707 }
2708
2709 static int snd_bbfpro_gain_update(struct usb_mixer_interface *mixer,
2710                                   u8 channel, u8 gain)
2711 {
2712         int err;
2713         struct snd_usb_audio *chip = mixer->chip;
2714
2715         if (channel < 2) {
2716                 // XLR preamp: 3-bit fine, 5-bit coarse; special case >60
2717                 if (gain < 60)
2718                         gain = ((gain % 3) << 5) | (gain / 3);
2719                 else
2720                         gain = ((gain % 6) << 5) | (60 / 3);
2721         }
2722
2723         err = snd_usb_lock_shutdown(chip);
2724         if (err < 0)
2725                 return err;
2726
2727         err = snd_usb_ctl_msg(chip->dev,
2728                               usb_sndctrlpipe(chip->dev, 0),
2729                               SND_BBFPRO_USBREQ_GAIN,
2730                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2731                               gain, channel, NULL, 0);
2732
2733         snd_usb_unlock_shutdown(chip);
2734         return err;
2735 }
2736
2737 static int snd_bbfpro_gain_get(struct snd_kcontrol *kcontrol,
2738                                struct snd_ctl_elem_value *ucontrol)
2739 {
2740         int value = kcontrol->private_value & SND_BBFPRO_GAIN_VAL_MASK;
2741
2742         ucontrol->value.integer.value[0] = value;
2743         return 0;
2744 }
2745
2746 static int snd_bbfpro_gain_info(struct snd_kcontrol *kcontrol,
2747                                 struct snd_ctl_elem_info *uinfo)
2748 {
2749         int pv, channel;
2750
2751         pv = kcontrol->private_value;
2752         channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
2753                 SND_BBFPRO_GAIN_CHANNEL_MASK;
2754
2755         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2756         uinfo->count = 1;
2757         uinfo->value.integer.min = SND_BBFPRO_GAIN_VAL_MIN;
2758
2759         if (channel < 2)
2760                 uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_MIC_MAX;
2761         else
2762                 uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_LINE_MAX;
2763
2764         return 0;
2765 }
2766
2767 static int snd_bbfpro_gain_put(struct snd_kcontrol *kcontrol,
2768                                struct snd_ctl_elem_value *ucontrol)
2769 {
2770         int pv, channel, old_value, value, err;
2771
2772         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2773         struct usb_mixer_interface *mixer = list->mixer;
2774
2775         pv = kcontrol->private_value;
2776         channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
2777                 SND_BBFPRO_GAIN_CHANNEL_MASK;
2778         old_value = pv & SND_BBFPRO_GAIN_VAL_MASK;
2779         value = ucontrol->value.integer.value[0];
2780
2781         if (value < SND_BBFPRO_GAIN_VAL_MIN)
2782                 return -EINVAL;
2783
2784         if (channel < 2) {
2785                 if (value > SND_BBFPRO_GAIN_VAL_MIC_MAX)
2786                         return -EINVAL;
2787         } else {
2788                 if (value > SND_BBFPRO_GAIN_VAL_LINE_MAX)
2789                         return -EINVAL;
2790         }
2791
2792         if (value == old_value)
2793                 return 0;
2794
2795         err = snd_bbfpro_gain_update(mixer, channel, value);
2796         if (err < 0)
2797                 return err;
2798
2799         kcontrol->private_value =
2800                 (channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT) | value;
2801         return 1;
2802 }
2803
2804 static int snd_bbfpro_gain_resume(struct usb_mixer_elem_list *list)
2805 {
2806         int pv, channel, value;
2807         struct snd_kcontrol *kctl = list->kctl;
2808
2809         pv = kctl->private_value;
2810         channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
2811                 SND_BBFPRO_GAIN_CHANNEL_MASK;
2812         value = pv & SND_BBFPRO_GAIN_VAL_MASK;
2813
2814         return snd_bbfpro_gain_update(list->mixer, channel, value);
2815 }
2816
2817 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
2818                                  u32 value)
2819 {
2820         struct snd_usb_audio *chip = mixer->chip;
2821         int err;
2822         u16 idx;
2823         u16 usb_idx, usb_val;
2824         u32 v;
2825
2826         err = snd_usb_lock_shutdown(chip);
2827         if (err < 0)
2828                 return err;
2829
2830         idx = index & SND_BBFPRO_MIXER_IDX_MASK;
2831         // 18 bit linear volume, split so 2 bits end up in index.
2832         v = value & SND_BBFPRO_MIXER_VAL_MASK;
2833         usb_idx = idx | (v & 0x3) << 14;
2834         usb_val = (v >> 2) & 0xffff;
2835
2836         err = snd_usb_ctl_msg(chip->dev,
2837                               usb_sndctrlpipe(chip->dev, 0),
2838                               SND_BBFPRO_USBREQ_MIXER,
2839                               USB_DIR_OUT | USB_TYPE_VENDOR |
2840                               USB_RECIP_DEVICE,
2841                               usb_val, usb_idx, NULL, 0);
2842
2843         snd_usb_unlock_shutdown(chip);
2844         return err;
2845 }
2846
2847 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
2848                               struct snd_ctl_elem_value *ucontrol)
2849 {
2850         ucontrol->value.integer.value[0] =
2851                 kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2852         return 0;
2853 }
2854
2855 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
2856                                struct snd_ctl_elem_info *uinfo)
2857 {
2858         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2859         uinfo->count = 1;
2860         uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
2861         uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
2862         return 0;
2863 }
2864
2865 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
2866                               struct snd_ctl_elem_value *ucontrol)
2867 {
2868         int err;
2869         u16 idx;
2870         u32 new_val, old_value, uvalue;
2871         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2872         struct usb_mixer_interface *mixer = list->mixer;
2873
2874         uvalue = ucontrol->value.integer.value[0];
2875         idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
2876         old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2877
2878         if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
2879                 return -EINVAL;
2880
2881         if (uvalue == old_value)
2882                 return 0;
2883
2884         new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
2885
2886         kcontrol->private_value = idx
2887                 | (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
2888
2889         err = snd_bbfpro_vol_update(mixer, idx, new_val);
2890         return err < 0 ? err : 1;
2891 }
2892
2893 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
2894 {
2895         int pv = list->kctl->private_value;
2896         u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
2897         u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
2898                 & SND_BBFPRO_MIXER_VAL_MASK;
2899         return snd_bbfpro_vol_update(list->mixer, idx, val);
2900 }
2901
2902 // Predfine elements
2903 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
2904         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2905         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2906         .index = 0,
2907         .info = snd_bbfpro_ctl_info,
2908         .get = snd_bbfpro_ctl_get,
2909         .put = snd_bbfpro_ctl_put
2910 };
2911
2912 static const struct snd_kcontrol_new snd_bbfpro_gain_control = {
2913         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2914         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2915         .index = 0,
2916         .info = snd_bbfpro_gain_info,
2917         .get = snd_bbfpro_gain_get,
2918         .put = snd_bbfpro_gain_put
2919 };
2920
2921 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
2922         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2923         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2924         .index = 0,
2925         .info = snd_bbfpro_vol_info,
2926         .get = snd_bbfpro_vol_get,
2927         .put = snd_bbfpro_vol_put
2928 };
2929
2930 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
2931                               u8 index, char *name)
2932 {
2933         struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
2934
2935         knew.name = name;
2936         knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
2937                 | ((index & SND_BBFPRO_CTL_IDX_MASK)
2938                         << SND_BBFPRO_CTL_IDX_SHIFT);
2939
2940         return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
2941                 &knew, NULL);
2942 }
2943
2944 static int snd_bbfpro_gain_add(struct usb_mixer_interface *mixer, u8 channel,
2945                                char *name)
2946 {
2947         struct snd_kcontrol_new knew = snd_bbfpro_gain_control;
2948
2949         knew.name = name;
2950         knew.private_value = channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT;
2951
2952         return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_gain_resume,
2953                 &knew, NULL);
2954 }
2955
2956 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
2957                               char *name)
2958 {
2959         struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
2960
2961         knew.name = name;
2962         knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
2963
2964         return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
2965                 &knew, NULL);
2966 }
2967
2968 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
2969 {
2970         int err, i, o;
2971         char name[48];
2972
2973         static const char * const input[] = {
2974                 "AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
2975                 "ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2976
2977         static const char * const output[] = {
2978                 "AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
2979                 "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2980
2981         for (o = 0 ; o < 12 ; ++o) {
2982                 for (i = 0 ; i < 12 ; ++i) {
2983                         // Line routing
2984                         snprintf(name, sizeof(name),
2985                                  "%s-%s-%s Playback Volume",
2986                                  (i < 2 ? "Mic" : "Line"),
2987                                  input[i], output[o]);
2988                         err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
2989                         if (err < 0)
2990                                 return err;
2991
2992                         // PCM routing... yes, it is output remapping
2993                         snprintf(name, sizeof(name),
2994                                  "PCM-%s-%s Playback Volume",
2995                                  output[i], output[o]);
2996                         err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
2997                                                  name);
2998                         if (err < 0)
2999                                 return err;
3000                 }
3001         }
3002
3003         // Main out volume
3004         for (i = 0 ; i < 12 ; ++i) {
3005                 snprintf(name, sizeof(name), "Main-Out %s", output[i]);
3006                 // Main outs are offset to 992
3007                 err = snd_bbfpro_vol_add(mixer,
3008                                          i + SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET,
3009                                          name);
3010                 if (err < 0)
3011                         return err;
3012         }
3013
3014         // Input gain
3015         for (i = 0 ; i < 4 ; ++i) {
3016                 if (i < 2)
3017                         snprintf(name, sizeof(name), "Mic-%s Gain", input[i]);
3018                 else
3019                         snprintf(name, sizeof(name), "Line-%s Gain", input[i]);
3020
3021                 err = snd_bbfpro_gain_add(mixer, i, name);
3022                 if (err < 0)
3023                         return err;
3024         }
3025
3026         // Control Reg 1
3027         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3028                                  SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
3029                                  "Sample Clock Source");
3030         if (err < 0)
3031                 return err;
3032
3033         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3034                                  SND_BBFPRO_CTL_REG1_SPDIF_PRO,
3035                                  "IEC958 Pro Mask");
3036         if (err < 0)
3037                 return err;
3038
3039         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3040                                  SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
3041                                  "IEC958 Emphasis");
3042         if (err < 0)
3043                 return err;
3044
3045         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3046                                  SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
3047                                  "IEC958 Switch");
3048         if (err < 0)
3049                 return err;
3050
3051         // Control Reg 2
3052         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3053                                  SND_BBFPRO_CTL_REG2_48V_AN1,
3054                                  "Mic-AN1 48V");
3055         if (err < 0)
3056                 return err;
3057
3058         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3059                                  SND_BBFPRO_CTL_REG2_48V_AN2,
3060                                  "Mic-AN2 48V");
3061         if (err < 0)
3062                 return err;
3063
3064         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3065                                  SND_BBFPRO_CTL_REG2_SENS_IN3,
3066                                  "Line-IN3 Sens.");
3067         if (err < 0)
3068                 return err;
3069
3070         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3071                                  SND_BBFPRO_CTL_REG2_SENS_IN4,
3072                                  "Line-IN4 Sens.");
3073         if (err < 0)
3074                 return err;
3075
3076         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3077                                  SND_BBFPRO_CTL_REG2_PAD_AN1,
3078                                  "Mic-AN1 PAD");
3079         if (err < 0)
3080                 return err;
3081
3082         err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3083                                  SND_BBFPRO_CTL_REG2_PAD_AN2,
3084                                  "Mic-AN2 PAD");
3085         if (err < 0)
3086                 return err;
3087
3088         return 0;
3089 }
3090
3091 /*
3092  * RME Digiface USB
3093  */
3094
3095 #define RME_DIGIFACE_READ_STATUS 17
3096 #define RME_DIGIFACE_STATUS_REG0L 0
3097 #define RME_DIGIFACE_STATUS_REG0H 1
3098 #define RME_DIGIFACE_STATUS_REG1L 2
3099 #define RME_DIGIFACE_STATUS_REG1H 3
3100 #define RME_DIGIFACE_STATUS_REG2L 4
3101 #define RME_DIGIFACE_STATUS_REG2H 5
3102 #define RME_DIGIFACE_STATUS_REG3L 6
3103 #define RME_DIGIFACE_STATUS_REG3H 7
3104
3105 #define RME_DIGIFACE_CTL_REG1 16
3106 #define RME_DIGIFACE_CTL_REG2 18
3107
3108 /* Reg is overloaded, 0-7 for status halfwords or 16 or 18 for control registers */
3109 #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
3110 #define RME_DIGIFACE_INVERT BIT(31)
3111
3112 /* Nonconst helpers */
3113 #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
3114 #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
3115
3116 static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
3117 {
3118         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3119         struct snd_usb_audio *chip = list->mixer->chip;
3120         struct usb_device *dev = chip->dev;
3121         int err;
3122
3123         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
3124                               item,
3125                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3126                               val, mask, NULL, 0);
3127         if (err < 0)
3128                 dev_err(&dev->dev,
3129                         "unable to issue control set request %d (ret = %d)",
3130                         item, err);
3131         return err;
3132 }
3133
3134 static int snd_rme_digiface_read_status(struct snd_kcontrol *kcontrol, u32 status[4])
3135 {
3136         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3137         struct snd_usb_audio *chip = list->mixer->chip;
3138         struct usb_device *dev = chip->dev;
3139         __le32 buf[4];
3140         int err;
3141
3142         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
3143                               RME_DIGIFACE_READ_STATUS,
3144                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3145                               0, 0,
3146                               buf, sizeof(buf));
3147         if (err < 0) {
3148                 dev_err(&dev->dev,
3149                         "unable to issue status read request (ret = %d)",
3150                         err);
3151         } else {
3152                 for (int i = 0; i < ARRAY_SIZE(buf); i++)
3153                         status[i] = le32_to_cpu(buf[i]);
3154         }
3155         return err;
3156 }
3157
3158 static int snd_rme_digiface_get_status_val(struct snd_kcontrol *kcontrol)
3159 {
3160         int err;
3161         u32 status[4];
3162         bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3163         u8 reg = (kcontrol->private_value >> 16) & 0xff;
3164         u16 mask = kcontrol->private_value & 0xffff;
3165         u16 val;
3166
3167         err = snd_rme_digiface_read_status(kcontrol, status);
3168         if (err < 0)
3169                 return err;
3170
3171         switch (reg) {
3172         /* Status register halfwords */
3173         case RME_DIGIFACE_STATUS_REG0L ... RME_DIGIFACE_STATUS_REG3H:
3174                 break;
3175         case RME_DIGIFACE_CTL_REG1: /* Control register 1, present in halfword 3L */
3176                 reg = RME_DIGIFACE_STATUS_REG3L;
3177                 break;
3178         case RME_DIGIFACE_CTL_REG2: /* Control register 2, present in halfword 3H */
3179                 reg = RME_DIGIFACE_STATUS_REG3H;
3180                 break;
3181         default:
3182                 return -EINVAL;
3183         }
3184
3185         if (reg & 1)
3186                 val = status[reg >> 1] >> 16;
3187         else
3188                 val = status[reg >> 1] & 0xffff;
3189
3190         if (invert)
3191                 val ^= mask;
3192
3193         return field_get(mask, val);
3194 }
3195
3196 static int snd_rme_digiface_rate_get(struct snd_kcontrol *kcontrol,
3197                                      struct snd_ctl_elem_value *ucontrol)
3198 {
3199         int freq = snd_rme_digiface_get_status_val(kcontrol);
3200
3201         if (freq < 0)
3202                 return freq;
3203         if (freq >= ARRAY_SIZE(snd_rme_rate_table))
3204                 return -EIO;
3205
3206         ucontrol->value.integer.value[0] = snd_rme_rate_table[freq];
3207         return 0;
3208 }
3209
3210 static int snd_rme_digiface_enum_get(struct snd_kcontrol *kcontrol,
3211                                      struct snd_ctl_elem_value *ucontrol)
3212 {
3213         int val = snd_rme_digiface_get_status_val(kcontrol);
3214
3215         if (val < 0)
3216                 return val;
3217
3218         ucontrol->value.enumerated.item[0] = val;
3219         return 0;
3220 }
3221
3222 static int snd_rme_digiface_enum_put(struct snd_kcontrol *kcontrol,
3223                                      struct snd_ctl_elem_value *ucontrol)
3224 {
3225         bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3226         u8 reg = (kcontrol->private_value >> 16) & 0xff;
3227         u16 mask = kcontrol->private_value & 0xffff;
3228         u16 val = field_prep(mask, ucontrol->value.enumerated.item[0]);
3229
3230         if (invert)
3231                 val ^= mask;
3232
3233         return snd_rme_digiface_write_reg(kcontrol, reg, mask, val);
3234 }
3235
3236 static int snd_rme_digiface_current_sync_get(struct snd_kcontrol *kcontrol,
3237                                      struct snd_ctl_elem_value *ucontrol)
3238 {
3239         int ret = snd_rme_digiface_enum_get(kcontrol, ucontrol);
3240
3241         /* 7 means internal for current sync */
3242         if (ucontrol->value.enumerated.item[0] == 7)
3243                 ucontrol->value.enumerated.item[0] = 0;
3244
3245         return ret;
3246 }
3247
3248 static int snd_rme_digiface_sync_state_get(struct snd_kcontrol *kcontrol,
3249                                            struct snd_ctl_elem_value *ucontrol)
3250 {
3251         u32 status[4];
3252         int err;
3253         bool valid, sync;
3254
3255         err = snd_rme_digiface_read_status(kcontrol, status);
3256         if (err < 0)
3257                 return err;
3258
3259         valid = status[0] & BIT(kcontrol->private_value);
3260         sync = status[0] & BIT(5 + kcontrol->private_value);
3261
3262         if (!valid)
3263                 ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_NOLOCK;
3264         else if (!sync)
3265                 ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_LOCK;
3266         else
3267                 ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_SYNC;
3268         return 0;
3269 }
3270
3271
3272 static int snd_rme_digiface_format_info(struct snd_kcontrol *kcontrol,
3273                                         struct snd_ctl_elem_info *uinfo)
3274 {
3275         static const char *const format[] = {
3276                 "ADAT", "S/PDIF"
3277         };
3278
3279         return snd_ctl_enum_info(uinfo, 1,
3280                                  ARRAY_SIZE(format), format);
3281 }
3282
3283
3284 static int snd_rme_digiface_sync_source_info(struct snd_kcontrol *kcontrol,
3285                                              struct snd_ctl_elem_info *uinfo)
3286 {
3287         static const char *const sync_sources[] = {
3288                 "Internal", "Input 1", "Input 2", "Input 3", "Input 4"
3289         };
3290
3291         return snd_ctl_enum_info(uinfo, 1,
3292                                  ARRAY_SIZE(sync_sources), sync_sources);
3293 }
3294
3295 static int snd_rme_digiface_rate_info(struct snd_kcontrol *kcontrol,
3296                                       struct snd_ctl_elem_info *uinfo)
3297 {
3298         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3299         uinfo->count = 1;
3300         uinfo->value.integer.min = 0;
3301         uinfo->value.integer.max = 200000;
3302         uinfo->value.integer.step = 0;
3303         return 0;
3304 }
3305
3306 static const struct snd_kcontrol_new snd_rme_digiface_controls[] = {
3307         {
3308                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3309                 .name = "Input 1 Sync",
3310                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3311                 .info = snd_rme_sync_state_info,
3312                 .get = snd_rme_digiface_sync_state_get,
3313                 .private_value = 0,
3314         },
3315         {
3316                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3317                 .name = "Input 1 Format",
3318                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3319                 .info = snd_rme_digiface_format_info,
3320                 .get = snd_rme_digiface_enum_get,
3321                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0H, BIT(0)) |
3322                         RME_DIGIFACE_INVERT,
3323         },
3324         {
3325                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3326                 .name = "Input 1 Rate",
3327                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3328                 .info = snd_rme_digiface_rate_info,
3329                 .get = snd_rme_digiface_rate_get,
3330                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3331         },
3332         {
3333                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3334                 .name = "Input 2 Sync",
3335                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3336                 .info = snd_rme_sync_state_info,
3337                 .get = snd_rme_digiface_sync_state_get,
3338                 .private_value = 1,
3339         },
3340         {
3341                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3342                 .name = "Input 2 Format",
3343                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3344                 .info = snd_rme_digiface_format_info,
3345                 .get = snd_rme_digiface_enum_get,
3346                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(13)) |
3347                         RME_DIGIFACE_INVERT,
3348         },
3349         {
3350                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3351                 .name = "Input 2 Rate",
3352                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3353                 .info = snd_rme_digiface_rate_info,
3354                 .get = snd_rme_digiface_rate_get,
3355                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(7, 4)),
3356         },
3357         {
3358                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3359                 .name = "Input 3 Sync",
3360                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3361                 .info = snd_rme_sync_state_info,
3362                 .get = snd_rme_digiface_sync_state_get,
3363                 .private_value = 2,
3364         },
3365         {
3366                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3367                 .name = "Input 3 Format",
3368                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3369                 .info = snd_rme_digiface_format_info,
3370                 .get = snd_rme_digiface_enum_get,
3371                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(14)) |
3372                         RME_DIGIFACE_INVERT,
3373         },
3374         {
3375                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3376                 .name = "Input 3 Rate",
3377                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3378                 .info = snd_rme_digiface_rate_info,
3379                 .get = snd_rme_digiface_rate_get,
3380                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(11, 8)),
3381         },
3382         {
3383                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3384                 .name = "Input 4 Sync",
3385                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3386                 .info = snd_rme_sync_state_info,
3387                 .get = snd_rme_digiface_sync_state_get,
3388                 .private_value = 3,
3389         },
3390         {
3391                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3392                 .name = "Input 4 Format",
3393                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3394                 .info = snd_rme_digiface_format_info,
3395                 .get = snd_rme_digiface_enum_get,
3396                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(15, 12)) |
3397                         RME_DIGIFACE_INVERT,
3398         },
3399         {
3400                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3401                 .name = "Input 4 Rate",
3402                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3403                 .info = snd_rme_digiface_rate_info,
3404                 .get = snd_rme_digiface_rate_get,
3405                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3406         },
3407         {
3408                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3409                 .name = "Output 1 Format",
3410                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3411                 .info = snd_rme_digiface_format_info,
3412                 .get = snd_rme_digiface_enum_get,
3413                 .put = snd_rme_digiface_enum_put,
3414                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(0)),
3415         },
3416         {
3417                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3418                 .name = "Output 2 Format",
3419                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3420                 .info = snd_rme_digiface_format_info,
3421                 .get = snd_rme_digiface_enum_get,
3422                 .put = snd_rme_digiface_enum_put,
3423                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(1)),
3424         },
3425         {
3426                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3427                 .name = "Output 3 Format",
3428                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3429                 .info = snd_rme_digiface_format_info,
3430                 .get = snd_rme_digiface_enum_get,
3431                 .put = snd_rme_digiface_enum_put,
3432                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(3)),
3433         },
3434         {
3435                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3436                 .name = "Output 4 Format",
3437                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3438                 .info = snd_rme_digiface_format_info,
3439                 .get = snd_rme_digiface_enum_get,
3440                 .put = snd_rme_digiface_enum_put,
3441                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(4)),
3442         },
3443         {
3444                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3445                 .name = "Sync Source",
3446                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3447                 .info = snd_rme_digiface_sync_source_info,
3448                 .get = snd_rme_digiface_enum_get,
3449                 .put = snd_rme_digiface_enum_put,
3450                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(2, 0)),
3451         },
3452         {
3453                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3454                 .name = "Current Sync Source",
3455                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3456                 .info = snd_rme_digiface_sync_source_info,
3457                 .get = snd_rme_digiface_current_sync_get,
3458                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(12, 10)),
3459         },
3460         {
3461                 /*
3462                  * This is writeable, but it is only set by the PCM rate.
3463                  * Mixer apps currently need to drive the mixer using raw USB requests,
3464                  * so they can also change this that way to configure the rate for
3465                  * stand-alone operation when the PCM is closed.
3466                  */
3467                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3468                 .name = "System Rate",
3469                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3470                 .info = snd_rme_rate_info,
3471                 .get = snd_rme_digiface_rate_get,
3472                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(6, 3)),
3473         },
3474         {
3475                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3476                 .name = "Current Rate",
3477                 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3478                 .info = snd_rme_rate_info,
3479                 .get = snd_rme_digiface_rate_get,
3480                 .private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1H, GENMASK(7, 4)),
3481         }
3482 };
3483
3484 static int snd_rme_digiface_controls_create(struct usb_mixer_interface *mixer)
3485 {
3486         int err, i;
3487
3488         for (i = 0; i < ARRAY_SIZE(snd_rme_digiface_controls); ++i) {
3489                 err = add_single_ctl_with_resume(mixer, 0,
3490                                                  NULL,
3491                                                  &snd_rme_digiface_controls[i],
3492                                                  NULL);
3493                 if (err < 0)
3494                         return err;
3495         }
3496
3497         return 0;
3498 }
3499
3500 /*
3501  * Pioneer DJ DJM Mixers
3502  *
3503  * These devices generally have options for soft-switching the playback and
3504  * capture sources in addition to the recording level. Although different
3505  * devices have different configurations, there seems to be canonical values
3506  * for specific capture/playback types:  See the definitions of these below.
3507  *
3508  * The wValue is masked with the stereo channel number. e.g. Setting Ch2 to
3509  * capture phono would be 0x0203. Capture, playback and capture level have
3510  * different wIndexes.
3511  */
3512
3513 // Capture types
3514 #define SND_DJM_CAP_LINE        0x00
3515 #define SND_DJM_CAP_CDLINE      0x01
3516 #define SND_DJM_CAP_DIGITAL     0x02
3517 #define SND_DJM_CAP_PHONO       0x03
3518 #define SND_DJM_CAP_PFADER      0x06
3519 #define SND_DJM_CAP_XFADERA     0x07
3520 #define SND_DJM_CAP_XFADERB     0x08
3521 #define SND_DJM_CAP_MIC         0x09
3522 #define SND_DJM_CAP_AUX         0x0d
3523 #define SND_DJM_CAP_RECOUT      0x0a
3524 #define SND_DJM_CAP_NONE        0x0f
3525 #define SND_DJM_CAP_CH1PFADER   0x11
3526 #define SND_DJM_CAP_CH2PFADER   0x12
3527 #define SND_DJM_CAP_CH3PFADER   0x13
3528 #define SND_DJM_CAP_CH4PFADER   0x14
3529
3530 // Playback types
3531 #define SND_DJM_PB_CH1          0x00
3532 #define SND_DJM_PB_CH2          0x01
3533 #define SND_DJM_PB_AUX          0x04
3534
3535 #define SND_DJM_WINDEX_CAP      0x8002
3536 #define SND_DJM_WINDEX_CAPLVL   0x8003
3537 #define SND_DJM_WINDEX_PB       0x8016
3538
3539 // kcontrol->private_value layout
3540 #define SND_DJM_VALUE_MASK      0x0000ffff
3541 #define SND_DJM_GROUP_MASK      0x00ff0000
3542 #define SND_DJM_DEVICE_MASK     0xff000000
3543 #define SND_DJM_GROUP_SHIFT     16
3544 #define SND_DJM_DEVICE_SHIFT    24
3545
3546 // device table index
3547 // used for the snd_djm_devices table, so please update accordingly
3548 #define SND_DJM_250MK2_IDX      0x0
3549 #define SND_DJM_750_IDX         0x1
3550 #define SND_DJM_850_IDX         0x2
3551 #define SND_DJM_900NXS2_IDX     0x3
3552 #define SND_DJM_750MK2_IDX      0x4
3553 #define SND_DJM_450_IDX         0x5
3554
3555
3556 #define SND_DJM_CTL(_name, suffix, _default_value, _windex) { \
3557         .name = _name, \
3558         .options = snd_djm_opts_##suffix, \
3559         .noptions = ARRAY_SIZE(snd_djm_opts_##suffix), \
3560         .default_value = _default_value, \
3561         .wIndex = _windex }
3562
3563 #define SND_DJM_DEVICE(suffix) { \
3564         .controls = snd_djm_ctls_##suffix, \
3565         .ncontrols = ARRAY_SIZE(snd_djm_ctls_##suffix) }
3566
3567
3568 struct snd_djm_device {
3569         const char *name;
3570         const struct snd_djm_ctl *controls;
3571         size_t ncontrols;
3572 };
3573
3574 struct snd_djm_ctl {
3575         const char *name;
3576         const u16 *options;
3577         size_t noptions;
3578         u16 default_value;
3579         u16 wIndex;
3580 };
3581
3582 static const char *snd_djm_get_label_caplevel(u16 wvalue)
3583 {
3584         switch (wvalue) {
3585         case 0x0000:    return "-19dB";
3586         case 0x0100:    return "-15dB";
3587         case 0x0200:    return "-10dB";
3588         case 0x0300:    return "-5dB";
3589         default:        return NULL;
3590         }
3591 };
3592
3593 static const char *snd_djm_get_label_cap_common(u16 wvalue)
3594 {
3595         switch (wvalue & 0x00ff) {
3596         case SND_DJM_CAP_LINE:          return "Control Tone LINE";
3597         case SND_DJM_CAP_CDLINE:        return "Control Tone CD/LINE";
3598         case SND_DJM_CAP_DIGITAL:       return "Control Tone DIGITAL";
3599         case SND_DJM_CAP_PHONO:         return "Control Tone PHONO";
3600         case SND_DJM_CAP_PFADER:        return "Post Fader";
3601         case SND_DJM_CAP_XFADERA:       return "Cross Fader A";
3602         case SND_DJM_CAP_XFADERB:       return "Cross Fader B";
3603         case SND_DJM_CAP_MIC:           return "Mic";
3604         case SND_DJM_CAP_RECOUT:        return "Rec Out";
3605         case SND_DJM_CAP_AUX:           return "Aux";
3606         case SND_DJM_CAP_NONE:          return "None";
3607         case SND_DJM_CAP_CH1PFADER:     return "Post Fader Ch1";
3608         case SND_DJM_CAP_CH2PFADER:     return "Post Fader Ch2";
3609         case SND_DJM_CAP_CH3PFADER:     return "Post Fader Ch3";
3610         case SND_DJM_CAP_CH4PFADER:     return "Post Fader Ch4";
3611         default:                        return NULL;
3612         }
3613 };
3614
3615 // The DJM-850 has different values for CD/LINE and LINE capture
3616 // control options than the other DJM declared in this file.
3617 static const char *snd_djm_get_label_cap_850(u16 wvalue)
3618 {
3619         switch (wvalue & 0x00ff) {
3620         case 0x00:              return "Control Tone CD/LINE";
3621         case 0x01:              return "Control Tone LINE";
3622         default:                return snd_djm_get_label_cap_common(wvalue);
3623         }
3624 };
3625
3626 static const char *snd_djm_get_label_cap(u8 device_idx, u16 wvalue)
3627 {
3628         switch (device_idx) {
3629         case SND_DJM_850_IDX:           return snd_djm_get_label_cap_850(wvalue);
3630         default:                        return snd_djm_get_label_cap_common(wvalue);
3631         }
3632 };
3633
3634 static const char *snd_djm_get_label_pb(u16 wvalue)
3635 {
3636         switch (wvalue & 0x00ff) {
3637         case SND_DJM_PB_CH1:    return "Ch1";
3638         case SND_DJM_PB_CH2:    return "Ch2";
3639         case SND_DJM_PB_AUX:    return "Aux";
3640         default:                return NULL;
3641         }
3642 };
3643
3644 static const char *snd_djm_get_label(u8 device_idx, u16 wvalue, u16 windex)
3645 {
3646         switch (windex) {
3647         case SND_DJM_WINDEX_CAPLVL:     return snd_djm_get_label_caplevel(wvalue);
3648         case SND_DJM_WINDEX_CAP:        return snd_djm_get_label_cap(device_idx, wvalue);
3649         case SND_DJM_WINDEX_PB:         return snd_djm_get_label_pb(wvalue);
3650         default:                        return NULL;
3651         }
3652 };
3653
3654 // common DJM capture level option values
3655 static const u16 snd_djm_opts_cap_level[] = {
3656         0x0000, 0x0100, 0x0200, 0x0300 };
3657
3658
3659 // DJM-250MK2
3660 static const u16 snd_djm_opts_250mk2_cap1[] = {
3661         0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3662
3663 static const u16 snd_djm_opts_250mk2_cap2[] = {
3664         0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3665
3666 static const u16 snd_djm_opts_250mk2_cap3[] = {
3667         0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3668
3669 static const u16 snd_djm_opts_250mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
3670 static const u16 snd_djm_opts_250mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
3671 static const u16 snd_djm_opts_250mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
3672
3673 static const struct snd_djm_ctl snd_djm_ctls_250mk2[] = {
3674         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3675         SND_DJM_CTL("Ch1 Input",   250mk2_cap1, 2, SND_DJM_WINDEX_CAP),
3676         SND_DJM_CTL("Ch2 Input",   250mk2_cap2, 2, SND_DJM_WINDEX_CAP),
3677         SND_DJM_CTL("Ch3 Input",   250mk2_cap3, 0, SND_DJM_WINDEX_CAP),
3678         SND_DJM_CTL("Ch1 Output",   250mk2_pb1, 0, SND_DJM_WINDEX_PB),
3679         SND_DJM_CTL("Ch2 Output",   250mk2_pb2, 1, SND_DJM_WINDEX_PB),
3680         SND_DJM_CTL("Ch3 Output",   250mk2_pb3, 2, SND_DJM_WINDEX_PB)
3681 };
3682
3683
3684 // DJM-450
3685 static const u16 snd_djm_opts_450_cap1[] = {
3686         0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3687
3688 static const u16 snd_djm_opts_450_cap2[] = {
3689         0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3690
3691 static const u16 snd_djm_opts_450_cap3[] = {
3692         0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3693
3694 static const u16 snd_djm_opts_450_pb1[] = { 0x0100, 0x0101, 0x0104 };
3695 static const u16 snd_djm_opts_450_pb2[] = { 0x0200, 0x0201, 0x0204 };
3696 static const u16 snd_djm_opts_450_pb3[] = { 0x0300, 0x0301, 0x0304 };
3697
3698 static const struct snd_djm_ctl snd_djm_ctls_450[] = {
3699         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3700         SND_DJM_CTL("Ch1 Input",   450_cap1, 2, SND_DJM_WINDEX_CAP),
3701         SND_DJM_CTL("Ch2 Input",   450_cap2, 2, SND_DJM_WINDEX_CAP),
3702         SND_DJM_CTL("Ch3 Input",   450_cap3, 0, SND_DJM_WINDEX_CAP),
3703         SND_DJM_CTL("Ch1 Output",   450_pb1, 0, SND_DJM_WINDEX_PB),
3704         SND_DJM_CTL("Ch2 Output",   450_pb2, 1, SND_DJM_WINDEX_PB),
3705         SND_DJM_CTL("Ch3 Output",   450_pb3, 2, SND_DJM_WINDEX_PB)
3706 };
3707
3708
3709 // DJM-750
3710 static const u16 snd_djm_opts_750_cap1[] = {
3711         0x0101, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
3712 static const u16 snd_djm_opts_750_cap2[] = {
3713         0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
3714 static const u16 snd_djm_opts_750_cap3[] = {
3715         0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
3716 static const u16 snd_djm_opts_750_cap4[] = {
3717         0x0401, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
3718
3719 static const struct snd_djm_ctl snd_djm_ctls_750[] = {
3720         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3721         SND_DJM_CTL("Ch1 Input",   750_cap1, 2, SND_DJM_WINDEX_CAP),
3722         SND_DJM_CTL("Ch2 Input",   750_cap2, 2, SND_DJM_WINDEX_CAP),
3723         SND_DJM_CTL("Ch3 Input",   750_cap3, 0, SND_DJM_WINDEX_CAP),
3724         SND_DJM_CTL("Ch4 Input",   750_cap4, 0, SND_DJM_WINDEX_CAP)
3725 };
3726
3727
3728 // DJM-850
3729 static const u16 snd_djm_opts_850_cap1[] = {
3730         0x0100, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
3731 static const u16 snd_djm_opts_850_cap2[] = {
3732         0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
3733 static const u16 snd_djm_opts_850_cap3[] = {
3734         0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
3735 static const u16 snd_djm_opts_850_cap4[] = {
3736         0x0400, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
3737
3738 static const struct snd_djm_ctl snd_djm_ctls_850[] = {
3739         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3740         SND_DJM_CTL("Ch1 Input",   850_cap1, 1, SND_DJM_WINDEX_CAP),
3741         SND_DJM_CTL("Ch2 Input",   850_cap2, 0, SND_DJM_WINDEX_CAP),
3742         SND_DJM_CTL("Ch3 Input",   850_cap3, 0, SND_DJM_WINDEX_CAP),
3743         SND_DJM_CTL("Ch4 Input",   850_cap4, 1, SND_DJM_WINDEX_CAP)
3744 };
3745
3746
3747 // DJM-900NXS2
3748 static const u16 snd_djm_opts_900nxs2_cap1[] = {
3749         0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
3750 static const u16 snd_djm_opts_900nxs2_cap2[] = {
3751         0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
3752 static const u16 snd_djm_opts_900nxs2_cap3[] = {
3753         0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
3754 static const u16 snd_djm_opts_900nxs2_cap4[] = {
3755         0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
3756 static const u16 snd_djm_opts_900nxs2_cap5[] = {
3757         0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
3758
3759 static const struct snd_djm_ctl snd_djm_ctls_900nxs2[] = {
3760         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3761         SND_DJM_CTL("Ch1 Input",   900nxs2_cap1, 2, SND_DJM_WINDEX_CAP),
3762         SND_DJM_CTL("Ch2 Input",   900nxs2_cap2, 2, SND_DJM_WINDEX_CAP),
3763         SND_DJM_CTL("Ch3 Input",   900nxs2_cap3, 2, SND_DJM_WINDEX_CAP),
3764         SND_DJM_CTL("Ch4 Input",   900nxs2_cap4, 2, SND_DJM_WINDEX_CAP),
3765         SND_DJM_CTL("Ch5 Input",   900nxs2_cap5, 3, SND_DJM_WINDEX_CAP)
3766 };
3767
3768 // DJM-750MK2
3769 static const u16 snd_djm_opts_750mk2_cap1[] = {
3770         0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
3771 static const u16 snd_djm_opts_750mk2_cap2[] = {
3772         0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
3773 static const u16 snd_djm_opts_750mk2_cap3[] = {
3774         0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
3775 static const u16 snd_djm_opts_750mk2_cap4[] = {
3776         0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
3777 static const u16 snd_djm_opts_750mk2_cap5[] = {
3778         0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
3779
3780 static const u16 snd_djm_opts_750mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
3781 static const u16 snd_djm_opts_750mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
3782 static const u16 snd_djm_opts_750mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
3783
3784
3785 static const struct snd_djm_ctl snd_djm_ctls_750mk2[] = {
3786         SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3787         SND_DJM_CTL("Ch1 Input",   750mk2_cap1, 2, SND_DJM_WINDEX_CAP),
3788         SND_DJM_CTL("Ch2 Input",   750mk2_cap2, 2, SND_DJM_WINDEX_CAP),
3789         SND_DJM_CTL("Ch3 Input",   750mk2_cap3, 2, SND_DJM_WINDEX_CAP),
3790         SND_DJM_CTL("Ch4 Input",   750mk2_cap4, 2, SND_DJM_WINDEX_CAP),
3791         SND_DJM_CTL("Ch5 Input",   750mk2_cap5, 3, SND_DJM_WINDEX_CAP),
3792         SND_DJM_CTL("Ch1 Output",   750mk2_pb1, 0, SND_DJM_WINDEX_PB),
3793         SND_DJM_CTL("Ch2 Output",   750mk2_pb2, 1, SND_DJM_WINDEX_PB),
3794         SND_DJM_CTL("Ch3 Output",   750mk2_pb3, 2, SND_DJM_WINDEX_PB)
3795 };
3796
3797
3798 static const struct snd_djm_device snd_djm_devices[] = {
3799         [SND_DJM_250MK2_IDX] = SND_DJM_DEVICE(250mk2),
3800         [SND_DJM_750_IDX] = SND_DJM_DEVICE(750),
3801         [SND_DJM_850_IDX] = SND_DJM_DEVICE(850),
3802         [SND_DJM_900NXS2_IDX] = SND_DJM_DEVICE(900nxs2),
3803         [SND_DJM_750MK2_IDX] = SND_DJM_DEVICE(750mk2),
3804         [SND_DJM_450_IDX] = SND_DJM_DEVICE(450),
3805 };
3806
3807
3808 static int snd_djm_controls_info(struct snd_kcontrol *kctl,
3809                                 struct snd_ctl_elem_info *info)
3810 {
3811         unsigned long private_value = kctl->private_value;
3812         u8 device_idx = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3813         u8 ctl_idx = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3814         const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3815         const char *name;
3816         const struct snd_djm_ctl *ctl;
3817         size_t noptions;
3818
3819         if (ctl_idx >= device->ncontrols)
3820                 return -EINVAL;
3821
3822         ctl = &device->controls[ctl_idx];
3823         noptions = ctl->noptions;
3824         if (info->value.enumerated.item >= noptions)
3825                 info->value.enumerated.item = noptions - 1;
3826
3827         name = snd_djm_get_label(device_idx,
3828                                 ctl->options[info->value.enumerated.item],
3829                                 ctl->wIndex);
3830         if (!name)
3831                 return -EINVAL;
3832
3833         strscpy(info->value.enumerated.name, name, sizeof(info->value.enumerated.name));
3834         info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3835         info->count = 1;
3836         info->value.enumerated.items = noptions;
3837         return 0;
3838 }
3839
3840 static int snd_djm_controls_update(struct usb_mixer_interface *mixer,
3841                                 u8 device_idx, u8 group, u16 value)
3842 {
3843         int err;
3844         const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3845
3846         if ((group >= device->ncontrols) || value >= device->controls[group].noptions)
3847                 return -EINVAL;
3848
3849         err = snd_usb_lock_shutdown(mixer->chip);
3850         if (err)
3851                 return err;
3852
3853         err = snd_usb_ctl_msg(
3854                 mixer->chip->dev, usb_sndctrlpipe(mixer->chip->dev, 0),
3855                 USB_REQ_SET_FEATURE,
3856                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3857                 device->controls[group].options[value],
3858                 device->controls[group].wIndex,
3859                 NULL, 0);
3860
3861         snd_usb_unlock_shutdown(mixer->chip);
3862         return err;
3863 }
3864
3865 static int snd_djm_controls_get(struct snd_kcontrol *kctl,
3866                                 struct snd_ctl_elem_value *elem)
3867 {
3868         elem->value.enumerated.item[0] = kctl->private_value & SND_DJM_VALUE_MASK;
3869         return 0;
3870 }
3871
3872 static int snd_djm_controls_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *elem)
3873 {
3874         struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
3875         struct usb_mixer_interface *mixer = list->mixer;
3876         unsigned long private_value = kctl->private_value;
3877
3878         u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3879         u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3880         u16 value = elem->value.enumerated.item[0];
3881
3882         kctl->private_value = (((unsigned long)device << SND_DJM_DEVICE_SHIFT) |
3883                               (group << SND_DJM_GROUP_SHIFT) |
3884                               value);
3885
3886         return snd_djm_controls_update(mixer, device, group, value);
3887 }
3888
3889 static int snd_djm_controls_resume(struct usb_mixer_elem_list *list)
3890 {
3891         unsigned long private_value = list->kctl->private_value;
3892         u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3893         u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3894         u16 value = (private_value & SND_DJM_VALUE_MASK);
3895
3896         return snd_djm_controls_update(list->mixer, device, group, value);
3897 }
3898
3899 static int snd_djm_controls_create(struct usb_mixer_interface *mixer,
3900                 const u8 device_idx)
3901 {
3902         int err, i;
3903         u16 value;
3904
3905         const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3906
3907         struct snd_kcontrol_new knew = {
3908                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
3909                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3910                 .index = 0,
3911                 .info = snd_djm_controls_info,
3912                 .get  = snd_djm_controls_get,
3913                 .put  = snd_djm_controls_put
3914         };
3915
3916         for (i = 0; i < device->ncontrols; i++) {
3917                 value = device->controls[i].default_value;
3918                 knew.name = device->controls[i].name;
3919                 knew.private_value = (
3920                         ((unsigned long)device_idx << SND_DJM_DEVICE_SHIFT) |
3921                         (i << SND_DJM_GROUP_SHIFT) |
3922                         value);
3923                 err = snd_djm_controls_update(mixer, device_idx, i, value);
3924                 if (err)
3925                         return err;
3926                 err = add_single_ctl_with_resume(mixer, 0, snd_djm_controls_resume,
3927                                                  &knew, NULL);
3928                 if (err)
3929                         return err;
3930         }
3931         return 0;
3932 }
3933
3934 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
3935 {
3936         int err = 0;
3937
3938         err = snd_usb_soundblaster_remote_init(mixer);
3939         if (err < 0)
3940                 return err;
3941
3942         switch (mixer->chip->usb_id) {
3943         /* Tascam US-16x08 */
3944         case USB_ID(0x0644, 0x8047):
3945                 err = snd_us16x08_controls_create(mixer);
3946                 break;
3947         case USB_ID(0x041e, 0x3020):
3948         case USB_ID(0x041e, 0x3040):
3949         case USB_ID(0x041e, 0x3042):
3950         case USB_ID(0x041e, 0x30df):
3951         case USB_ID(0x041e, 0x3048):
3952                 err = snd_audigy2nx_controls_create(mixer);
3953                 if (err < 0)
3954                         break;
3955                 snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
3956                                      mixer, snd_audigy2nx_proc_read);
3957                 break;
3958
3959         /* EMU0204 */
3960         case USB_ID(0x041e, 0x3f19):
3961                 err = snd_emu0204_controls_create(mixer);
3962                 break;
3963
3964         case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
3965         case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
3966                 err = snd_c400_create_mixer(mixer);
3967                 break;
3968
3969         case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
3970         case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
3971                 err = snd_ftu_create_mixer(mixer);
3972                 break;
3973
3974         case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
3975         case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
3976         case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
3977                 err = snd_xonar_u1_controls_create(mixer);
3978                 break;
3979
3980         case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
3981                 err = snd_microii_controls_create(mixer);
3982                 break;
3983
3984         case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
3985                 err = snd_mbox1_controls_create(mixer);
3986                 break;
3987
3988         case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
3989                 err = snd_nativeinstruments_create_mixer(mixer,
3990                                 snd_nativeinstruments_ta6_mixers,
3991                                 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
3992                 break;
3993
3994         case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
3995                 err = snd_nativeinstruments_create_mixer(mixer,
3996                                 snd_nativeinstruments_ta10_mixers,
3997                                 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
3998                 break;
3999
4000         case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
4001                 /* detection is disabled in mixer_maps.c */
4002                 err = snd_create_std_mono_table(mixer, ebox44_table);
4003                 break;
4004
4005         case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
4006         case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
4007         case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
4008         case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
4009         case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
4010                 err = snd_scarlett_controls_create(mixer);
4011                 break;
4012
4013         case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
4014         case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
4015         case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
4016         case USB_ID(0x1235, 0x8211): /* Focusrite Scarlett Solo 3rd Gen */
4017         case USB_ID(0x1235, 0x8210): /* Focusrite Scarlett 2i2 3rd Gen */
4018         case USB_ID(0x1235, 0x8212): /* Focusrite Scarlett 4i4 3rd Gen */
4019         case USB_ID(0x1235, 0x8213): /* Focusrite Scarlett 8i6 3rd Gen */
4020         case USB_ID(0x1235, 0x8214): /* Focusrite Scarlett 18i8 3rd Gen */
4021         case USB_ID(0x1235, 0x8215): /* Focusrite Scarlett 18i20 3rd Gen */
4022         case USB_ID(0x1235, 0x8216): /* Focusrite Vocaster One */
4023         case USB_ID(0x1235, 0x8217): /* Focusrite Vocaster Two */
4024         case USB_ID(0x1235, 0x8218): /* Focusrite Scarlett Solo 4th Gen */
4025         case USB_ID(0x1235, 0x8219): /* Focusrite Scarlett 2i2 4th Gen */
4026         case USB_ID(0x1235, 0x821a): /* Focusrite Scarlett 4i4 4th Gen */
4027         case USB_ID(0x1235, 0x8206): /* Focusrite Clarett 2Pre USB */
4028         case USB_ID(0x1235, 0x8207): /* Focusrite Clarett 4Pre USB */
4029         case USB_ID(0x1235, 0x8208): /* Focusrite Clarett 8Pre USB */
4030         case USB_ID(0x1235, 0x820a): /* Focusrite Clarett+ 2Pre */
4031         case USB_ID(0x1235, 0x820b): /* Focusrite Clarett+ 4Pre */
4032         case USB_ID(0x1235, 0x820c): /* Focusrite Clarett+ 8Pre */
4033                 err = snd_scarlett2_init(mixer);
4034                 break;
4035
4036         case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
4037                 err = snd_soundblaster_e1_switch_create(mixer);
4038                 break;
4039         case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4040                 err = dell_dock_mixer_create(mixer);
4041                 if (err < 0)
4042                         break;
4043                 err = dell_dock_mixer_init(mixer);
4044                 break;
4045
4046         case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
4047         case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
4048         case USB_ID(0x2a39, 0x3fd4): /* RME */
4049                 err = snd_rme_controls_create(mixer);
4050                 break;
4051
4052         case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
4053                 err = snd_sc1810_init_mixer(mixer);
4054                 break;
4055         case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
4056                 err = snd_bbfpro_controls_create(mixer);
4057                 break;
4058         case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
4059                 err = snd_rme_digiface_controls_create(mixer);
4060                 break;
4061         case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
4062                 err = snd_djm_controls_create(mixer, SND_DJM_250MK2_IDX);
4063                 break;
4064         case USB_ID(0x2b73, 0x0013): /* Pioneer DJ DJM-450 */
4065                 err = snd_djm_controls_create(mixer, SND_DJM_450_IDX);
4066                 break;
4067         case USB_ID(0x08e4, 0x017f): /* Pioneer DJ DJM-750 */
4068                 err = snd_djm_controls_create(mixer, SND_DJM_750_IDX);
4069                 break;
4070         case USB_ID(0x2b73, 0x001b): /* Pioneer DJ DJM-750MK2 */
4071                 err = snd_djm_controls_create(mixer, SND_DJM_750MK2_IDX);
4072                 break;
4073         case USB_ID(0x08e4, 0x0163): /* Pioneer DJ DJM-850 */
4074                 err = snd_djm_controls_create(mixer, SND_DJM_850_IDX);
4075                 break;
4076         case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
4077                 err = snd_djm_controls_create(mixer, SND_DJM_900NXS2_IDX);
4078                 break;
4079         }
4080
4081         return err;
4082 }
4083
4084 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
4085 {
4086         switch (mixer->chip->usb_id) {
4087         case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4088                 dell_dock_mixer_init(mixer);
4089                 break;
4090         }
4091 }
4092
4093 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
4094                                     int unitid)
4095 {
4096         if (!mixer->rc_cfg)
4097                 return;
4098         /* unit ids specific to Extigy/Audigy 2 NX: */
4099         switch (unitid) {
4100         case 0: /* remote control */
4101                 mixer->rc_urb->dev = mixer->chip->dev;
4102                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
4103                 break;
4104         case 4: /* digital in jack */
4105         case 7: /* line in jacks */
4106         case 19: /* speaker out jacks */
4107         case 20: /* headphones out jack */
4108                 break;
4109         /* live24ext: 4 = line-in jack */
4110         case 3: /* hp-out jack (may actuate Mute) */
4111                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
4112                     mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
4113                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
4114                 break;
4115         default:
4116                 usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
4117                 break;
4118         }
4119 }
4120
4121 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
4122                                          struct usb_mixer_elem_info *cval,
4123                                          struct snd_kcontrol *kctl)
4124 {
4125         /* Approximation using 10 ranges based on output measurement on hw v1.2.
4126          * This seems close to the cubic mapping e.g. alsamixer uses. */
4127         static const DECLARE_TLV_DB_RANGE(scale,
4128                  0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
4129                  2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
4130                  6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
4131                  8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
4132                 15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
4133                 17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
4134                 20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
4135                 27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
4136                 32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
4137                 41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
4138         );
4139
4140         if (cval->min == 0 && cval->max == 50) {
4141                 usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
4142                 kctl->tlv.p = scale;
4143                 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4144                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4145
4146         } else if (cval->min == 0 && cval->max <= 1000) {
4147                 /* Some other clearly broken DragonFly variant.
4148                  * At least a 0..53 variant (hw v1.0) exists.
4149                  */
4150                 usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
4151                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4152         }
4153 }
4154
4155 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
4156                                   struct usb_mixer_elem_info *cval, int unitid,
4157                                   struct snd_kcontrol *kctl)
4158 {
4159         switch (mixer->chip->usb_id) {
4160         case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
4161                 if (unitid == 7 && cval->control == UAC_FU_VOLUME)
4162                         snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
4163                 break;
4164         /* lowest playback value is muted on some devices */
4165         case USB_ID(0x0d8c, 0x000c): /* C-Media */
4166         case USB_ID(0x0d8c, 0x0014): /* C-Media */
4167         case USB_ID(0x19f7, 0x0003): /* RODE NT-USB */
4168                 if (strstr(kctl->id.name, "Playback"))
4169                         cval->min_mute = 1;
4170                 break;
4171         }
4172 }
4173