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