pandora: defconfig: update
[pandora-kernel.git] / sound / usb / mixer_quirks.c
1 /*
2  *   USB Audio Driver for ALSA
3  *
4  *   Quirks and vendor-specific extensions for mixer interfaces
5  *
6  *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
8  *   Many codes borrowed from audio.c by
9  *          Alan Cox (alan@lxorguk.ukuu.org.uk)
10  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
11  *
12  *
13  *   This program is free software; you can redistribute it and/or modify
14  *   it under the terms of the GNU General Public License as published by
15  *   the Free Software Foundation; either version 2 of the License, or
16  *   (at your option) any later version.
17  *
18  *   This program is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with this program; if not, write to the Free Software
25  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  */
27
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/usb.h>
31 #include <linux/usb/audio.h>
32
33 #include <sound/core.h>
34 #include <sound/control.h>
35 #include <sound/hwdep.h>
36 #include <sound/info.h>
37 #include <sound/tlv.h>
38
39 #include "usbaudio.h"
40 #include "mixer.h"
41 #include "mixer_quirks.h"
42 #include "helper.h"
43
44 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
45
46 /*
47  * Sound Blaster remote control configuration
48  *
49  * format of remote control data:
50  * Extigy:       xx 00
51  * Audigy 2 NX:  06 80 xx 00 00 00
52  * Live! 24-bit: 06 80 xx yy 22 83
53  */
54 static const struct rc_config {
55         u32 usb_id;
56         u8  offset;
57         u8  length;
58         u8  packet_length;
59         u8  min_packet_length; /* minimum accepted length of the URB result */
60         u8  mute_mixer_id;
61         u32 mute_code;
62 } rc_configs[] = {
63         { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
64         { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
65         { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
66         { USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
67         { USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
68         { USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
69         { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
70 };
71
72 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
73 {
74         struct usb_mixer_interface *mixer = urb->context;
75         const struct rc_config *rc = mixer->rc_cfg;
76         u32 code;
77
78         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
79                 return;
80
81         code = mixer->rc_buffer[rc->offset];
82         if (rc->length == 2)
83                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
84
85         /* the Mute button actually changes the mixer control */
86         if (code == rc->mute_code)
87                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
88         mixer->rc_code = code;
89         wmb();
90         wake_up(&mixer->rc_waitq);
91 }
92
93 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
94                                      long count, loff_t *offset)
95 {
96         struct usb_mixer_interface *mixer = hw->private_data;
97         int err;
98         u32 rc_code;
99
100         if (count != 1 && count != 4)
101                 return -EINVAL;
102         err = wait_event_interruptible(mixer->rc_waitq,
103                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
104         if (err == 0) {
105                 if (count == 1)
106                         err = put_user(rc_code, buf);
107                 else
108                         err = put_user(rc_code, (u32 __user *)buf);
109         }
110         return err < 0 ? err : count;
111 }
112
113 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
114                                             poll_table *wait)
115 {
116         struct usb_mixer_interface *mixer = hw->private_data;
117
118         poll_wait(file, &mixer->rc_waitq, wait);
119         return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
120 }
121
122 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
123 {
124         struct snd_hwdep *hwdep;
125         int err, len, i;
126
127         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
128                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
129                         break;
130         if (i >= ARRAY_SIZE(rc_configs))
131                 return 0;
132         mixer->rc_cfg = &rc_configs[i];
133
134         len = mixer->rc_cfg->packet_length;
135
136         init_waitqueue_head(&mixer->rc_waitq);
137         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
138         if (err < 0)
139                 return err;
140         snprintf(hwdep->name, sizeof(hwdep->name),
141                  "%s remote control", mixer->chip->card->shortname);
142         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
143         hwdep->private_data = mixer;
144         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
145         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
146         hwdep->exclusive = 1;
147
148         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
149         if (!mixer->rc_urb)
150                 return -ENOMEM;
151         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
152         if (!mixer->rc_setup_packet) {
153                 usb_free_urb(mixer->rc_urb);
154                 mixer->rc_urb = NULL;
155                 return -ENOMEM;
156         }
157         mixer->rc_setup_packet->bRequestType =
158                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
159         mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
160         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
161         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
162         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
163         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
164                              usb_rcvctrlpipe(mixer->chip->dev, 0),
165                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
166                              snd_usb_soundblaster_remote_complete, mixer);
167         return 0;
168 }
169
170 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
171
172 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
173 {
174         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
175         int index = kcontrol->private_value;
176
177         ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
178         return 0;
179 }
180
181 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
182 {
183         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
184         int index = kcontrol->private_value;
185         int value = ucontrol->value.integer.value[0];
186         int err, changed;
187
188         if (value > 1)
189                 return -EINVAL;
190         changed = value != mixer->audigy2nx_leds[index];
191         down_read(&mixer->chip->shutdown_rwsem);
192         if (mixer->chip->shutdown) {
193                 err = -ENODEV;
194                 goto out;
195         }
196         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
197                 err = snd_usb_ctl_msg(mixer->chip->dev,
198                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
199                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
200                               !value, 0, NULL, 0);
201         /* USB X-Fi S51 Pro */
202         if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
203                 err = snd_usb_ctl_msg(mixer->chip->dev,
204                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
205                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
206                               !value, 0, NULL, 0);
207         else
208                 err = snd_usb_ctl_msg(mixer->chip->dev,
209                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
210                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
211                               value, index + 2, NULL, 0);
212  out:
213         up_read(&mixer->chip->shutdown_rwsem);
214         if (err < 0)
215                 return err;
216         mixer->audigy2nx_leds[index] = value;
217         return changed;
218 }
219
220 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
221         {
222                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
223                 .name = "CMSS LED Switch",
224                 .info = snd_audigy2nx_led_info,
225                 .get = snd_audigy2nx_led_get,
226                 .put = snd_audigy2nx_led_put,
227                 .private_value = 0,
228         },
229         {
230                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
231                 .name = "Power LED Switch",
232                 .info = snd_audigy2nx_led_info,
233                 .get = snd_audigy2nx_led_get,
234                 .put = snd_audigy2nx_led_put,
235                 .private_value = 1,
236         },
237         {
238                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
239                 .name = "Dolby Digital LED Switch",
240                 .info = snd_audigy2nx_led_info,
241                 .get = snd_audigy2nx_led_get,
242                 .put = snd_audigy2nx_led_put,
243                 .private_value = 2,
244         },
245 };
246
247 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
248 {
249         int i, err;
250
251         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
252                 /* USB X-Fi S51 doesn't have a CMSS LED */
253                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
254                         continue;
255                 /* USB X-Fi S51 Pro doesn't have one either */
256                 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
257                         continue;
258                 if (i > 1 && /* Live24ext has 2 LEDs only */
259                         (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
260                          mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
261                          mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
262                          mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
263                         break; 
264                 err = snd_ctl_add(mixer->chip->card,
265                                   snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
266                 if (err < 0)
267                         return err;
268         }
269         mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
270         return 0;
271 }
272
273 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
274                                     struct snd_info_buffer *buffer)
275 {
276         static const struct sb_jack {
277                 int unitid;
278                 const char *name;
279         }  jacks_audigy2nx[] = {
280                 {4,  "dig in "},
281                 {7,  "line in"},
282                 {19, "spk out"},
283                 {20, "hph out"},
284                 {-1, NULL}
285         }, jacks_live24ext[] = {
286                 {4,  "line in"}, /* &1=Line, &2=Mic*/
287                 {3,  "hph out"}, /* headphones */
288                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
289                 {-1, NULL}
290         };
291         const struct sb_jack *jacks;
292         struct usb_mixer_interface *mixer = entry->private_data;
293         int i, err;
294         u8 buf[3];
295
296         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
297         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
298                 jacks = jacks_audigy2nx;
299         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
300                  mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
301                 jacks = jacks_live24ext;
302         else
303                 return;
304
305         for (i = 0; jacks[i].name; ++i) {
306                 snd_iprintf(buffer, "%s: ", jacks[i].name);
307                 down_read(&mixer->chip->shutdown_rwsem);
308                 if (mixer->chip->shutdown)
309                         err = 0;
310                 else
311                         err = snd_usb_ctl_msg(mixer->chip->dev,
312                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
313                                       UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
314                                       USB_RECIP_INTERFACE, 0,
315                                       jacks[i].unitid << 8, buf, 3);
316                 up_read(&mixer->chip->shutdown_rwsem);
317                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
318                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
319                 else
320                         snd_iprintf(buffer, "?\n");
321         }
322 }
323
324 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
325                                    struct snd_ctl_elem_value *ucontrol)
326 {
327         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
328
329         ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
330         return 0;
331 }
332
333 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
334                                    struct snd_ctl_elem_value *ucontrol)
335 {
336         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
337         u8 old_status, new_status;
338         int err, changed;
339
340         old_status = mixer->xonar_u1_status;
341         if (ucontrol->value.integer.value[0])
342                 new_status = old_status | 0x02;
343         else
344                 new_status = old_status & ~0x02;
345         changed = new_status != old_status;
346         down_read(&mixer->chip->shutdown_rwsem);
347         if (mixer->chip->shutdown)
348                 err = -ENODEV;
349         else
350                 err = snd_usb_ctl_msg(mixer->chip->dev,
351                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
352                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
353                               50, 0, &new_status, 1);
354         up_read(&mixer->chip->shutdown_rwsem);
355         if (err < 0)
356                 return err;
357         mixer->xonar_u1_status = new_status;
358         return changed;
359 }
360
361 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
362         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
363         .name = "Digital Playback Switch",
364         .info = snd_ctl_boolean_mono_info,
365         .get = snd_xonar_u1_switch_get,
366         .put = snd_xonar_u1_switch_put,
367 };
368
369 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
370 {
371         int err;
372
373         err = snd_ctl_add(mixer->chip->card,
374                           snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
375         if (err < 0)
376                 return err;
377         mixer->xonar_u1_status = 0x05;
378         return 0;
379 }
380
381 /* Native Instruments device quirks */
382
383 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
384
385 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
386                                              struct snd_ctl_elem_value *ucontrol)
387 {
388         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
389         struct usb_device *dev = mixer->chip->dev;
390         u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
391         u16 wIndex = kcontrol->private_value & 0xffff;
392         u8 tmp;
393         int ret;
394
395         down_read(&mixer->chip->shutdown_rwsem);
396         if (mixer->chip->shutdown)
397                 ret = -ENODEV;
398         else
399                 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
400                                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
401                                   0, wIndex,
402                                   &tmp, sizeof(tmp), 1000);
403         up_read(&mixer->chip->shutdown_rwsem);
404
405         if (ret < 0) {
406                 snd_printk(KERN_ERR
407                            "unable to issue vendor read request (ret = %d)", ret);
408                 return ret;
409         }
410
411         ucontrol->value.integer.value[0] = tmp;
412
413         return 0;
414 }
415
416 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
417                                              struct snd_ctl_elem_value *ucontrol)
418 {
419         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
420         struct usb_device *dev = mixer->chip->dev;
421         u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
422         u16 wIndex = kcontrol->private_value & 0xffff;
423         u16 wValue = ucontrol->value.integer.value[0];
424         int ret;
425
426         down_read(&mixer->chip->shutdown_rwsem);
427         if (mixer->chip->shutdown)
428                 ret = -ENODEV;
429         else
430                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
431                                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
432                                   wValue, wIndex,
433                                   NULL, 0, 1000);
434         up_read(&mixer->chip->shutdown_rwsem);
435
436         if (ret < 0) {
437                 snd_printk(KERN_ERR
438                            "unable to issue vendor write request (ret = %d)", ret);
439                 return ret;
440         }
441
442         return 0;
443 }
444
445 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
446         {
447                 .name = "Direct Thru Channel A",
448                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
449         },
450         {
451                 .name = "Direct Thru Channel B",
452                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
453         },
454         {
455                 .name = "Phono Input Channel A",
456                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
457         },
458         {
459                 .name = "Phono Input Channel B",
460                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
461         },
462 };
463
464 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
465         {
466                 .name = "Direct Thru Channel A",
467                 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
468         },
469         {
470                 .name = "Direct Thru Channel B",
471                 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
472         },
473         {
474                 .name = "Direct Thru Channel C",
475                 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
476         },
477         {
478                 .name = "Direct Thru Channel D",
479                 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
480         },
481         {
482                 .name = "Phono Input Channel A",
483                 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
484         },
485         {
486                 .name = "Phono Input Channel B",
487                 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
488         },
489         {
490                 .name = "Phono Input Channel C",
491                 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
492         },
493         {
494                 .name = "Phono Input Channel D",
495                 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
496         },
497 };
498
499 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
500                                               const struct snd_kcontrol_new *kc,
501                                               unsigned int count)
502 {
503         int i, err = 0;
504         struct snd_kcontrol_new template = {
505                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
506                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
507                 .get = snd_nativeinstruments_control_get,
508                 .put = snd_nativeinstruments_control_put,
509                 .info = snd_ctl_boolean_mono_info,
510         };
511
512         for (i = 0; i < count; i++) {
513                 struct snd_kcontrol *c;
514
515                 template.name = kc[i].name;
516                 template.private_value = kc[i].private_value;
517
518                 c = snd_ctl_new1(&template, mixer);
519                 err = snd_ctl_add(mixer->chip->card, c);
520
521                 if (err < 0)
522                         break;
523         }
524
525         return err;
526 }
527
528 /* M-Audio FastTrack Ultra quirks */
529
530 /* private_free callback */
531 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
532 {
533         kfree(kctl->private_data);
534         kctl->private_data = NULL;
535 }
536
537 static int snd_maudio_ftu_create_ctl(struct usb_mixer_interface *mixer,
538                                      int in, int out, const char *name)
539 {
540         struct usb_mixer_elem_info *cval;
541         struct snd_kcontrol *kctl;
542
543         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
544         if (!cval)
545                 return -ENOMEM;
546
547         cval->id = 5;
548         cval->mixer = mixer;
549         cval->val_type = USB_MIXER_S16;
550         cval->channels = 1;
551         cval->control = out + 1;
552         cval->cmask = 1 << in;
553
554         kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
555         if (!kctl) {
556                 kfree(cval);
557                 return -ENOMEM;
558         }
559
560         snprintf(kctl->id.name, sizeof(kctl->id.name), name);
561         kctl->private_free = usb_mixer_elem_free;
562         return snd_usb_mixer_add_control(mixer, kctl);
563 }
564
565 static int snd_maudio_ftu_create_mixer(struct usb_mixer_interface *mixer)
566 {
567         char name[64];
568         int in, out, err;
569
570         for (out = 0; out < 8; out++) {
571                 for (in = 0; in < 8; in++) {
572                         snprintf(name, sizeof(name),
573                                  "AIn%d - Out%d Capture Volume", in  + 1, out + 1);
574                         err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
575                         if (err < 0)
576                                 return err;
577                 }
578
579                 for (in = 8; in < 16; in++) {
580                         snprintf(name, sizeof(name),
581                                  "DIn%d - Out%d Playback Volume", in - 7, out + 1);
582                         err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
583                         if (err < 0)
584                                 return err;
585                 }
586         }
587
588         return 0;
589 }
590
591 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
592                                unsigned char samplerate_id)
593 {
594         struct usb_mixer_interface *mixer;
595         struct usb_mixer_elem_info *cval;
596         int unitid = 12; /* SamleRate ExtensionUnit ID */
597
598         list_for_each_entry(mixer, &chip->mixer_list, list) {
599                 cval = mixer->id_elems[unitid];
600                 if (cval) {
601                         snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
602                                                     cval->control << 8,
603                                                     samplerate_id);
604                         snd_usb_mixer_notify_id(mixer, unitid);
605                 }
606                 break;
607         }
608 }
609
610 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
611 {
612         int err = 0;
613         struct snd_info_entry *entry;
614
615         if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
616                 return err;
617
618         switch (mixer->chip->usb_id) {
619         case USB_ID(0x041e, 0x3020):
620         case USB_ID(0x041e, 0x3040):
621         case USB_ID(0x041e, 0x3042):
622         case USB_ID(0x041e, 0x30df):
623         case USB_ID(0x041e, 0x3048):
624                 err = snd_audigy2nx_controls_create(mixer);
625                 if (err < 0)
626                         break;
627                 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
628                         snd_info_set_text_ops(entry, mixer,
629                                               snd_audigy2nx_proc_read);
630                 break;
631
632         case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
633         case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
634                 err = snd_maudio_ftu_create_mixer(mixer);
635                 break;
636
637         case USB_ID(0x0b05, 0x1739):
638         case USB_ID(0x0b05, 0x1743):
639                 err = snd_xonar_u1_controls_create(mixer);
640                 break;
641
642         case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
643                 err = snd_nativeinstruments_create_mixer(mixer,
644                                 snd_nativeinstruments_ta6_mixers,
645                                 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
646                 break;
647
648         case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
649                 err = snd_nativeinstruments_create_mixer(mixer,
650                                 snd_nativeinstruments_ta10_mixers,
651                                 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
652                 break;
653         }
654
655         return err;
656 }
657
658 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
659                                     int unitid)
660 {
661         if (!mixer->rc_cfg)
662                 return;
663         /* unit ids specific to Extigy/Audigy 2 NX: */
664         switch (unitid) {
665         case 0: /* remote control */
666                 mixer->rc_urb->dev = mixer->chip->dev;
667                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
668                 break;
669         case 4: /* digital in jack */
670         case 7: /* line in jacks */
671         case 19: /* speaker out jacks */
672         case 20: /* headphones out jack */
673                 break;
674         /* live24ext: 4 = line-in jack */
675         case 3: /* hp-out jack (may actuate Mute) */
676                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
677                     mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
678                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
679                 break;
680         default:
681                 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
682                 break;
683         }
684 }
685
686 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
687                                          struct usb_mixer_elem_info *cval,
688                                          struct snd_kcontrol *kctl)
689 {
690         /* Approximation using 10 ranges based on output measurement on hw v1.2.
691          * This seems close to the cubic mapping e.g. alsamixer uses. */
692         static const DECLARE_TLV_DB_RANGE(scale,
693                  0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
694                  2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
695                  6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
696                  8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
697                 15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
698                 17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
699                 20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
700                 27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
701                 32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
702                 41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
703         );
704
705         if (cval->min == 0 && cval->max == 50) {
706                 dev_info(&mixer->chip->dev->dev, "applying DragonFly dB scale quirk (0-50 variant)\n");
707                 kctl->tlv.p = scale;
708                 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
709                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
710
711         } else if (cval->min == 0 && cval->max <= 1000) {
712                 /* Some other clearly broken DragonFly variant.
713                  * At least a 0..53 variant (hw v1.0) exists.
714                  */
715                 dev_info(&mixer->chip->dev->dev, "ignoring too narrow dB range on a DragonFly device");
716                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
717         }
718 }
719
720 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
721                                   struct usb_mixer_elem_info *cval, int unitid,
722                                   struct snd_kcontrol *kctl)
723 {
724         switch (mixer->chip->usb_id) {
725         case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
726                 if (unitid == 7 && cval->control == UAC_FU_VOLUME)
727                         snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
728                 break;
729         /* lowest playback value is muted on C-Media devices */
730         case USB_ID(0x0d8c, 0x000c):
731         case USB_ID(0x0d8c, 0x0014):
732                 if (strstr(kctl->id.name, "Playback"))
733                         cval->min_mute = 1;
734                 break;
735         }
736 }
737