cfg80211/mac80211: fix BSS leaks when abandoning assoc attempts
[linux/fpc-iii.git] / sound / usb / mixer_quirks.c
blob04991b00913222f07f4d6dbcb0dc6b955db4f5a5
1 /*
2 * USB Audio Driver for ALSA
4 * Quirks and vendor-specific extensions for mixer interfaces
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
12 * Audio Advantage Micro II support added by:
13 * Przemek Rudy (prudy1@o2.pl)
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/usb.h>
33 #include <linux/usb/audio.h>
35 #include <sound/asoundef.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/hwdep.h>
39 #include <sound/info.h>
40 #include <sound/tlv.h>
42 #include "usbaudio.h"
43 #include "mixer.h"
44 #include "mixer_quirks.h"
45 #include "mixer_scarlett.h"
46 #include "helper.h"
48 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
50 struct std_mono_table {
51 unsigned int unitid, control, cmask;
52 int val_type;
53 const char *name;
54 snd_kcontrol_tlv_rw_t *tlv_callback;
57 /* This function allows for the creation of standard UAC controls.
58 * See the quirks for M-Audio FTUs or Ebox-44.
59 * If you don't want to set a TLV callback pass NULL.
61 * Since there doesn't seem to be a devices that needs a multichannel
62 * version, we keep it mono for simplicity.
64 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
65 unsigned int unitid,
66 unsigned int control,
67 unsigned int cmask,
68 int val_type,
69 unsigned int idx_off,
70 const char *name,
71 snd_kcontrol_tlv_rw_t *tlv_callback)
73 struct usb_mixer_elem_info *cval;
74 struct snd_kcontrol *kctl;
76 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
77 if (!cval)
78 return -ENOMEM;
80 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
81 cval->val_type = val_type;
82 cval->channels = 1;
83 cval->control = control;
84 cval->cmask = cmask;
85 cval->idx_off = idx_off;
87 /* get_min_max() is called only for integer volumes later,
88 * so provide a short-cut for booleans */
89 cval->min = 0;
90 cval->max = 1;
91 cval->res = 0;
92 cval->dBmin = 0;
93 cval->dBmax = 0;
95 /* Create control */
96 kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
97 if (!kctl) {
98 kfree(cval);
99 return -ENOMEM;
102 /* Set name */
103 snprintf(kctl->id.name, sizeof(kctl->id.name), name);
104 kctl->private_free = snd_usb_mixer_elem_free;
106 /* set TLV */
107 if (tlv_callback) {
108 kctl->tlv.c = tlv_callback;
109 kctl->vd[0].access |=
110 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
111 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
113 /* Add control to mixer */
114 return snd_usb_mixer_add_control(&cval->head, kctl);
117 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
118 unsigned int unitid,
119 unsigned int control,
120 unsigned int cmask,
121 int val_type,
122 const char *name,
123 snd_kcontrol_tlv_rw_t *tlv_callback)
125 return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
126 val_type, 0 /* Offset */, name, tlv_callback);
130 * Create a set of standard UAC controls from a table
132 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
133 struct std_mono_table *t)
135 int err;
137 while (t->name != NULL) {
138 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
139 t->cmask, t->val_type, t->name, t->tlv_callback);
140 if (err < 0)
141 return err;
142 t++;
145 return 0;
148 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
149 int id,
150 usb_mixer_elem_resume_func_t resume,
151 const struct snd_kcontrol_new *knew,
152 struct usb_mixer_elem_list **listp)
154 struct usb_mixer_elem_list *list;
155 struct snd_kcontrol *kctl;
157 list = kzalloc(sizeof(*list), GFP_KERNEL);
158 if (!list)
159 return -ENOMEM;
160 if (listp)
161 *listp = list;
162 list->mixer = mixer;
163 list->id = id;
164 list->resume = resume;
165 kctl = snd_ctl_new1(knew, list);
166 if (!kctl) {
167 kfree(list);
168 return -ENOMEM;
170 kctl->private_free = snd_usb_mixer_elem_free;
171 return snd_usb_mixer_add_control(list, kctl);
175 * Sound Blaster remote control configuration
177 * format of remote control data:
178 * Extigy: xx 00
179 * Audigy 2 NX: 06 80 xx 00 00 00
180 * Live! 24-bit: 06 80 xx yy 22 83
182 static const struct rc_config {
183 u32 usb_id;
184 u8 offset;
185 u8 length;
186 u8 packet_length;
187 u8 min_packet_length; /* minimum accepted length of the URB result */
188 u8 mute_mixer_id;
189 u32 mute_code;
190 } rc_configs[] = {
191 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
192 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
193 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
194 { USB_ID(0x041e, 0x3042), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 */
195 { USB_ID(0x041e, 0x30df), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 Pro */
196 { USB_ID(0x041e, 0x3237), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 Pro */
197 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
200 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
202 struct usb_mixer_interface *mixer = urb->context;
203 const struct rc_config *rc = mixer->rc_cfg;
204 u32 code;
206 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
207 return;
209 code = mixer->rc_buffer[rc->offset];
210 if (rc->length == 2)
211 code |= mixer->rc_buffer[rc->offset + 1] << 8;
213 /* the Mute button actually changes the mixer control */
214 if (code == rc->mute_code)
215 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
216 mixer->rc_code = code;
217 wmb();
218 wake_up(&mixer->rc_waitq);
221 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
222 long count, loff_t *offset)
224 struct usb_mixer_interface *mixer = hw->private_data;
225 int err;
226 u32 rc_code;
228 if (count != 1 && count != 4)
229 return -EINVAL;
230 err = wait_event_interruptible(mixer->rc_waitq,
231 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
232 if (err == 0) {
233 if (count == 1)
234 err = put_user(rc_code, buf);
235 else
236 err = put_user(rc_code, (u32 __user *)buf);
238 return err < 0 ? err : count;
241 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
242 poll_table *wait)
244 struct usb_mixer_interface *mixer = hw->private_data;
246 poll_wait(file, &mixer->rc_waitq, wait);
247 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
250 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
252 struct snd_hwdep *hwdep;
253 int err, len, i;
255 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
256 if (rc_configs[i].usb_id == mixer->chip->usb_id)
257 break;
258 if (i >= ARRAY_SIZE(rc_configs))
259 return 0;
260 mixer->rc_cfg = &rc_configs[i];
262 len = mixer->rc_cfg->packet_length;
264 init_waitqueue_head(&mixer->rc_waitq);
265 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
266 if (err < 0)
267 return err;
268 snprintf(hwdep->name, sizeof(hwdep->name),
269 "%s remote control", mixer->chip->card->shortname);
270 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
271 hwdep->private_data = mixer;
272 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
273 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
274 hwdep->exclusive = 1;
276 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
277 if (!mixer->rc_urb)
278 return -ENOMEM;
279 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
280 if (!mixer->rc_setup_packet) {
281 usb_free_urb(mixer->rc_urb);
282 mixer->rc_urb = NULL;
283 return -ENOMEM;
285 mixer->rc_setup_packet->bRequestType =
286 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
287 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
288 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
289 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
290 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
291 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
292 usb_rcvctrlpipe(mixer->chip->dev, 0),
293 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
294 snd_usb_soundblaster_remote_complete, mixer);
295 return 0;
298 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
300 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
302 ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
303 return 0;
306 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
307 int value, int index)
309 struct snd_usb_audio *chip = mixer->chip;
310 int err;
312 err = snd_usb_lock_shutdown(chip);
313 if (err < 0)
314 return err;
316 if (chip->usb_id == USB_ID(0x041e, 0x3042))
317 err = snd_usb_ctl_msg(chip->dev,
318 usb_sndctrlpipe(chip->dev, 0), 0x24,
319 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
320 !value, 0, NULL, 0);
321 /* USB X-Fi S51 Pro */
322 if (chip->usb_id == USB_ID(0x041e, 0x30df))
323 err = snd_usb_ctl_msg(chip->dev,
324 usb_sndctrlpipe(chip->dev, 0), 0x24,
325 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
326 !value, 0, NULL, 0);
327 else
328 err = snd_usb_ctl_msg(chip->dev,
329 usb_sndctrlpipe(chip->dev, 0), 0x24,
330 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
331 value, index + 2, NULL, 0);
332 snd_usb_unlock_shutdown(chip);
333 return err;
336 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
337 struct snd_ctl_elem_value *ucontrol)
339 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
340 struct usb_mixer_interface *mixer = list->mixer;
341 int index = kcontrol->private_value & 0xff;
342 unsigned int value = ucontrol->value.integer.value[0];
343 int old_value = kcontrol->private_value >> 8;
344 int err;
346 if (value > 1)
347 return -EINVAL;
348 if (value == old_value)
349 return 0;
350 kcontrol->private_value = (value << 8) | index;
351 err = snd_audigy2nx_led_update(mixer, value, index);
352 return err < 0 ? err : 1;
355 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
357 int priv_value = list->kctl->private_value;
359 return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
360 priv_value & 0xff);
363 /* name and private_value are set dynamically */
364 static struct snd_kcontrol_new snd_audigy2nx_control = {
365 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
366 .info = snd_audigy2nx_led_info,
367 .get = snd_audigy2nx_led_get,
368 .put = snd_audigy2nx_led_put,
371 static const char * const snd_audigy2nx_led_names[] = {
372 "CMSS LED Switch",
373 "Power LED Switch",
374 "Dolby Digital LED Switch",
377 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
379 int i, err;
381 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
382 struct snd_kcontrol_new knew;
384 /* USB X-Fi S51 doesn't have a CMSS LED */
385 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
386 continue;
387 /* USB X-Fi S51 Pro doesn't have one either */
388 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
389 continue;
390 if (i > 1 && /* Live24ext has 2 LEDs only */
391 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
392 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
393 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
394 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
395 break;
397 knew = snd_audigy2nx_control;
398 knew.name = snd_audigy2nx_led_names[i];
399 knew.private_value = (1 << 8) | i; /* LED on as default */
400 err = add_single_ctl_with_resume(mixer, 0,
401 snd_audigy2nx_led_resume,
402 &knew, NULL);
403 if (err < 0)
404 return err;
406 return 0;
409 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
410 struct snd_info_buffer *buffer)
412 static const struct sb_jack {
413 int unitid;
414 const char *name;
415 } jacks_audigy2nx[] = {
416 {4, "dig in "},
417 {7, "line in"},
418 {19, "spk out"},
419 {20, "hph out"},
420 {-1, NULL}
421 }, jacks_live24ext[] = {
422 {4, "line in"}, /* &1=Line, &2=Mic*/
423 {3, "hph out"}, /* headphones */
424 {0, "RC "}, /* last command, 6 bytes see rc_config above */
425 {-1, NULL}
427 const struct sb_jack *jacks;
428 struct usb_mixer_interface *mixer = entry->private_data;
429 int i, err;
430 u8 buf[3];
432 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
433 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
434 jacks = jacks_audigy2nx;
435 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
436 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
437 jacks = jacks_live24ext;
438 else
439 return;
441 for (i = 0; jacks[i].name; ++i) {
442 snd_iprintf(buffer, "%s: ", jacks[i].name);
443 err = snd_usb_lock_shutdown(mixer->chip);
444 if (err < 0)
445 return;
446 err = snd_usb_ctl_msg(mixer->chip->dev,
447 usb_rcvctrlpipe(mixer->chip->dev, 0),
448 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
449 USB_RECIP_INTERFACE, 0,
450 jacks[i].unitid << 8, buf, 3);
451 snd_usb_unlock_shutdown(mixer->chip);
452 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
453 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
454 else
455 snd_iprintf(buffer, "?\n");
459 /* EMU0204 */
460 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
461 struct snd_ctl_elem_info *uinfo)
463 static const char * const texts[2] = {"1/2", "3/4"};
465 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
468 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
469 struct snd_ctl_elem_value *ucontrol)
471 ucontrol->value.enumerated.item[0] = kcontrol->private_value;
472 return 0;
475 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
476 int value)
478 struct snd_usb_audio *chip = mixer->chip;
479 int err;
480 unsigned char buf[2];
482 err = snd_usb_lock_shutdown(chip);
483 if (err < 0)
484 return err;
486 buf[0] = 0x01;
487 buf[1] = value ? 0x02 : 0x01;
488 err = snd_usb_ctl_msg(chip->dev,
489 usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
490 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
491 0x0400, 0x0e00, buf, 2);
492 snd_usb_unlock_shutdown(chip);
493 return err;
496 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
497 struct snd_ctl_elem_value *ucontrol)
499 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
500 struct usb_mixer_interface *mixer = list->mixer;
501 unsigned int value = ucontrol->value.enumerated.item[0];
502 int err;
504 if (value > 1)
505 return -EINVAL;
507 if (value == kcontrol->private_value)
508 return 0;
510 kcontrol->private_value = value;
511 err = snd_emu0204_ch_switch_update(mixer, value);
512 return err < 0 ? err : 1;
515 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
517 return snd_emu0204_ch_switch_update(list->mixer,
518 list->kctl->private_value);
521 static struct snd_kcontrol_new snd_emu0204_control = {
522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
523 .name = "Front Jack Channels",
524 .info = snd_emu0204_ch_switch_info,
525 .get = snd_emu0204_ch_switch_get,
526 .put = snd_emu0204_ch_switch_put,
527 .private_value = 0,
530 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
532 return add_single_ctl_with_resume(mixer, 0,
533 snd_emu0204_ch_switch_resume,
534 &snd_emu0204_control, NULL);
537 /* ASUS Xonar U1 / U3 controls */
539 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
540 struct snd_ctl_elem_value *ucontrol)
542 ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
543 return 0;
546 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
547 unsigned char status)
549 struct snd_usb_audio *chip = mixer->chip;
550 int err;
552 err = snd_usb_lock_shutdown(chip);
553 if (err < 0)
554 return err;
555 err = snd_usb_ctl_msg(chip->dev,
556 usb_sndctrlpipe(chip->dev, 0), 0x08,
557 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
558 50, 0, &status, 1);
559 snd_usb_unlock_shutdown(chip);
560 return err;
563 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
564 struct snd_ctl_elem_value *ucontrol)
566 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
567 u8 old_status, new_status;
568 int err;
570 old_status = kcontrol->private_value;
571 if (ucontrol->value.integer.value[0])
572 new_status = old_status | 0x02;
573 else
574 new_status = old_status & ~0x02;
575 if (new_status == old_status)
576 return 0;
578 kcontrol->private_value = new_status;
579 err = snd_xonar_u1_switch_update(list->mixer, new_status);
580 return err < 0 ? err : 1;
583 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
585 return snd_xonar_u1_switch_update(list->mixer,
586 list->kctl->private_value);
589 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
591 .name = "Digital Playback Switch",
592 .info = snd_ctl_boolean_mono_info,
593 .get = snd_xonar_u1_switch_get,
594 .put = snd_xonar_u1_switch_put,
595 .private_value = 0x05,
598 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
600 return add_single_ctl_with_resume(mixer, 0,
601 snd_xonar_u1_switch_resume,
602 &snd_xonar_u1_output_switch, NULL);
605 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
607 static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
608 struct snd_ctl_elem_value *ucontrol)
610 ucontrol->value.enumerated.item[0] = kctl->private_value;
611 return 0;
614 static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
616 struct snd_usb_audio *chip = mixer->chip;
617 int err;
618 unsigned char buff[3];
620 err = snd_usb_lock_shutdown(chip);
621 if (err < 0)
622 return err;
624 /* Prepare for magic command to toggle clock source */
625 err = snd_usb_ctl_msg(chip->dev,
626 usb_rcvctrlpipe(chip->dev, 0), 0x81,
627 USB_DIR_IN |
628 USB_TYPE_CLASS |
629 USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
630 if (err < 0)
631 goto err;
632 err = snd_usb_ctl_msg(chip->dev,
633 usb_rcvctrlpipe(chip->dev, 0), 0x81,
634 USB_DIR_IN |
635 USB_TYPE_CLASS |
636 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
637 if (err < 0)
638 goto err;
640 /* 2 possibilities: Internal -> send sample rate
641 * S/PDIF sync -> send zeroes
642 * NB: Sample rate locked to 48kHz on purpose to
643 * prevent user from resetting the sample rate
644 * while S/PDIF sync is enabled and confusing
645 * this configuration.
647 if (val == 0) {
648 buff[0] = 0x80;
649 buff[1] = 0xbb;
650 buff[2] = 0x00;
651 } else {
652 buff[0] = buff[1] = buff[2] = 0x00;
655 /* Send the magic command to toggle the clock source */
656 err = snd_usb_ctl_msg(chip->dev,
657 usb_sndctrlpipe(chip->dev, 0), 0x1,
658 USB_TYPE_CLASS |
659 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
660 if (err < 0)
661 goto err;
662 err = snd_usb_ctl_msg(chip->dev,
663 usb_rcvctrlpipe(chip->dev, 0), 0x81,
664 USB_DIR_IN |
665 USB_TYPE_CLASS |
666 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
667 if (err < 0)
668 goto err;
669 err = snd_usb_ctl_msg(chip->dev,
670 usb_rcvctrlpipe(chip->dev, 0), 0x81,
671 USB_DIR_IN |
672 USB_TYPE_CLASS |
673 USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
674 if (err < 0)
675 goto err;
677 err:
678 snd_usb_unlock_shutdown(chip);
679 return err;
682 static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
683 struct snd_ctl_elem_value *ucontrol)
685 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
686 struct usb_mixer_interface *mixer = list->mixer;
687 int err;
688 bool cur_val, new_val;
690 cur_val = kctl->private_value;
691 new_val = ucontrol->value.enumerated.item[0];
692 if (cur_val == new_val)
693 return 0;
695 kctl->private_value = new_val;
696 err = snd_mbox1_switch_update(mixer, new_val);
697 return err < 0 ? err : 1;
700 static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
701 struct snd_ctl_elem_info *uinfo)
703 static const char *const texts[2] = {
704 "Internal",
705 "S/PDIF"
708 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
711 static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
713 return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
716 static struct snd_kcontrol_new snd_mbox1_switch = {
717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
718 .name = "Clock Source",
719 .index = 0,
720 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
721 .info = snd_mbox1_switch_info,
722 .get = snd_mbox1_switch_get,
723 .put = snd_mbox1_switch_put,
724 .private_value = 0
727 static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
729 return add_single_ctl_with_resume(mixer, 0,
730 snd_mbox1_switch_resume,
731 &snd_mbox1_switch, NULL);
734 /* Native Instruments device quirks */
736 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
738 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
739 struct snd_kcontrol *kctl)
741 struct usb_device *dev = mixer->chip->dev;
742 unsigned int pval = kctl->private_value;
743 u8 value;
744 int err;
746 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
747 (pval >> 16) & 0xff,
748 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
749 0, pval & 0xffff, &value, 1);
750 if (err < 0) {
751 dev_err(&dev->dev,
752 "unable to issue vendor read request (ret = %d)", err);
753 return err;
756 kctl->private_value |= (value << 24);
757 return 0;
760 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_value *ucontrol)
763 ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
764 return 0;
767 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
769 struct snd_usb_audio *chip = list->mixer->chip;
770 unsigned int pval = list->kctl->private_value;
771 int err;
773 err = snd_usb_lock_shutdown(chip);
774 if (err < 0)
775 return err;
776 err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
777 (pval >> 16) & 0xff,
778 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
779 pval >> 24, pval & 0xffff, NULL, 0, 1000);
780 snd_usb_unlock_shutdown(chip);
781 return err;
784 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
785 struct snd_ctl_elem_value *ucontrol)
787 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
788 u8 oldval = (kcontrol->private_value >> 24) & 0xff;
789 u8 newval = ucontrol->value.integer.value[0];
790 int err;
792 if (oldval == newval)
793 return 0;
795 kcontrol->private_value &= ~(0xff << 24);
796 kcontrol->private_value |= (unsigned int)newval << 24;
797 err = snd_ni_update_cur_val(list);
798 return err < 0 ? err : 1;
801 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
803 .name = "Direct Thru Channel A",
804 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
807 .name = "Direct Thru Channel B",
808 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
811 .name = "Phono Input Channel A",
812 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
815 .name = "Phono Input Channel B",
816 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
820 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
822 .name = "Direct Thru Channel A",
823 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
826 .name = "Direct Thru Channel B",
827 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
830 .name = "Direct Thru Channel C",
831 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
834 .name = "Direct Thru Channel D",
835 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
838 .name = "Phono Input Channel A",
839 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
842 .name = "Phono Input Channel B",
843 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
846 .name = "Phono Input Channel C",
847 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
850 .name = "Phono Input Channel D",
851 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
855 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
856 const struct snd_kcontrol_new *kc,
857 unsigned int count)
859 int i, err = 0;
860 struct snd_kcontrol_new template = {
861 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
862 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
863 .get = snd_nativeinstruments_control_get,
864 .put = snd_nativeinstruments_control_put,
865 .info = snd_ctl_boolean_mono_info,
868 for (i = 0; i < count; i++) {
869 struct usb_mixer_elem_list *list;
871 template.name = kc[i].name;
872 template.private_value = kc[i].private_value;
874 err = add_single_ctl_with_resume(mixer, 0,
875 snd_ni_update_cur_val,
876 &template, &list);
877 if (err < 0)
878 break;
879 snd_ni_control_init_val(mixer, list->kctl);
882 return err;
885 /* M-Audio FastTrack Ultra quirks */
886 /* FTU Effect switch (also used by C400/C600) */
887 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
888 struct snd_ctl_elem_info *uinfo)
890 static const char *const texts[8] = {
891 "Room 1", "Room 2", "Room 3", "Hall 1",
892 "Hall 2", "Plate", "Delay", "Echo"
895 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
898 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
899 struct snd_kcontrol *kctl)
901 struct usb_device *dev = mixer->chip->dev;
902 unsigned int pval = kctl->private_value;
903 int err;
904 unsigned char value[2];
906 value[0] = 0x00;
907 value[1] = 0x00;
909 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
910 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
911 pval & 0xff00,
912 snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
913 value, 2);
914 if (err < 0)
915 return err;
917 kctl->private_value |= value[0] << 24;
918 return 0;
921 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
922 struct snd_ctl_elem_value *ucontrol)
924 ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
925 return 0;
928 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
930 struct snd_usb_audio *chip = list->mixer->chip;
931 unsigned int pval = list->kctl->private_value;
932 unsigned char value[2];
933 int err;
935 value[0] = pval >> 24;
936 value[1] = 0;
938 err = snd_usb_lock_shutdown(chip);
939 if (err < 0)
940 return err;
941 err = snd_usb_ctl_msg(chip->dev,
942 usb_sndctrlpipe(chip->dev, 0),
943 UAC_SET_CUR,
944 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
945 pval & 0xff00,
946 snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
947 value, 2);
948 snd_usb_unlock_shutdown(chip);
949 return err;
952 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
953 struct snd_ctl_elem_value *ucontrol)
955 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
956 unsigned int pval = list->kctl->private_value;
957 int cur_val, err, new_val;
959 cur_val = pval >> 24;
960 new_val = ucontrol->value.enumerated.item[0];
961 if (cur_val == new_val)
962 return 0;
964 kctl->private_value &= ~(0xff << 24);
965 kctl->private_value |= new_val << 24;
966 err = snd_ftu_eff_switch_update(list);
967 return err < 0 ? err : 1;
970 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
971 int validx, int bUnitID)
973 static struct snd_kcontrol_new template = {
974 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
975 .name = "Effect Program Switch",
976 .index = 0,
977 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
978 .info = snd_ftu_eff_switch_info,
979 .get = snd_ftu_eff_switch_get,
980 .put = snd_ftu_eff_switch_put
982 struct usb_mixer_elem_list *list;
983 int err;
985 err = add_single_ctl_with_resume(mixer, bUnitID,
986 snd_ftu_eff_switch_update,
987 &template, &list);
988 if (err < 0)
989 return err;
990 list->kctl->private_value = (validx << 8) | bUnitID;
991 snd_ftu_eff_switch_init(mixer, list->kctl);
992 return 0;
995 /* Create volume controls for FTU devices*/
996 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
998 char name[64];
999 unsigned int control, cmask;
1000 int in, out, err;
1002 const unsigned int id = 5;
1003 const int val_type = USB_MIXER_S16;
1005 for (out = 0; out < 8; out++) {
1006 control = out + 1;
1007 for (in = 0; in < 8; in++) {
1008 cmask = 1 << in;
1009 snprintf(name, sizeof(name),
1010 "AIn%d - Out%d Capture Volume",
1011 in + 1, 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;
1018 for (in = 8; in < 16; in++) {
1019 cmask = 1 << in;
1020 snprintf(name, sizeof(name),
1021 "DIn%d - Out%d Playback Volume",
1022 in - 7, out + 1);
1023 err = snd_create_std_mono_ctl(mixer, id, control,
1024 cmask, val_type, name,
1025 &snd_usb_mixer_vol_tlv);
1026 if (err < 0)
1027 return err;
1031 return 0;
1034 /* This control needs a volume quirk, see mixer.c */
1035 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1037 static const char name[] = "Effect Volume";
1038 const unsigned int id = 6;
1039 const int val_type = USB_MIXER_U8;
1040 const unsigned int control = 2;
1041 const unsigned int cmask = 0;
1043 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1044 name, snd_usb_mixer_vol_tlv);
1047 /* This control needs a volume quirk, see mixer.c */
1048 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1050 static const char name[] = "Effect Duration";
1051 const unsigned int id = 6;
1052 const int val_type = USB_MIXER_S16;
1053 const unsigned int control = 3;
1054 const unsigned int cmask = 0;
1056 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1057 name, snd_usb_mixer_vol_tlv);
1060 /* This control needs a volume quirk, see mixer.c */
1061 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1063 static const char name[] = "Effect Feedback Volume";
1064 const unsigned int id = 6;
1065 const int val_type = USB_MIXER_U8;
1066 const unsigned int control = 4;
1067 const unsigned int cmask = 0;
1069 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1070 name, NULL);
1073 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1075 unsigned int cmask;
1076 int err, ch;
1077 char name[48];
1079 const unsigned int id = 7;
1080 const int val_type = USB_MIXER_S16;
1081 const unsigned int control = 7;
1083 for (ch = 0; ch < 4; ++ch) {
1084 cmask = 1 << ch;
1085 snprintf(name, sizeof(name),
1086 "Effect Return %d Volume", ch + 1);
1087 err = snd_create_std_mono_ctl(mixer, id, control,
1088 cmask, val_type, name,
1089 snd_usb_mixer_vol_tlv);
1090 if (err < 0)
1091 return err;
1094 return 0;
1097 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1099 unsigned int cmask;
1100 int err, ch;
1101 char name[48];
1103 const unsigned int id = 5;
1104 const int val_type = USB_MIXER_S16;
1105 const unsigned int control = 9;
1107 for (ch = 0; ch < 8; ++ch) {
1108 cmask = 1 << ch;
1109 snprintf(name, sizeof(name),
1110 "Effect Send AIn%d Volume", ch + 1);
1111 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1112 val_type, name,
1113 snd_usb_mixer_vol_tlv);
1114 if (err < 0)
1115 return err;
1117 for (ch = 8; ch < 16; ++ch) {
1118 cmask = 1 << ch;
1119 snprintf(name, sizeof(name),
1120 "Effect Send DIn%d Volume", ch - 7);
1121 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1122 val_type, name,
1123 snd_usb_mixer_vol_tlv);
1124 if (err < 0)
1125 return err;
1127 return 0;
1130 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1132 int err;
1134 err = snd_ftu_create_volume_ctls(mixer);
1135 if (err < 0)
1136 return err;
1138 err = snd_ftu_create_effect_switch(mixer, 1, 6);
1139 if (err < 0)
1140 return err;
1142 err = snd_ftu_create_effect_volume_ctl(mixer);
1143 if (err < 0)
1144 return err;
1146 err = snd_ftu_create_effect_duration_ctl(mixer);
1147 if (err < 0)
1148 return err;
1150 err = snd_ftu_create_effect_feedback_ctl(mixer);
1151 if (err < 0)
1152 return err;
1154 err = snd_ftu_create_effect_return_ctls(mixer);
1155 if (err < 0)
1156 return err;
1158 err = snd_ftu_create_effect_send_ctls(mixer);
1159 if (err < 0)
1160 return err;
1162 return 0;
1165 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1166 unsigned char samplerate_id)
1168 struct usb_mixer_interface *mixer;
1169 struct usb_mixer_elem_info *cval;
1170 int unitid = 12; /* SamleRate ExtensionUnit ID */
1172 list_for_each_entry(mixer, &chip->mixer_list, list) {
1173 cval = (struct usb_mixer_elem_info *)mixer->id_elems[unitid];
1174 if (cval) {
1175 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1176 cval->control << 8,
1177 samplerate_id);
1178 snd_usb_mixer_notify_id(mixer, unitid);
1180 break;
1184 /* M-Audio Fast Track C400/C600 */
1185 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1186 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1188 char name[64];
1189 unsigned int cmask, offset;
1190 int out, chan, err;
1191 int num_outs = 0;
1192 int num_ins = 0;
1194 const unsigned int id = 0x40;
1195 const int val_type = USB_MIXER_S16;
1196 const int control = 1;
1198 switch (mixer->chip->usb_id) {
1199 case USB_ID(0x0763, 0x2030):
1200 num_outs = 6;
1201 num_ins = 4;
1202 break;
1203 case USB_ID(0x0763, 0x2031):
1204 num_outs = 8;
1205 num_ins = 6;
1206 break;
1209 for (chan = 0; chan < num_outs + num_ins; chan++) {
1210 for (out = 0; out < num_outs; out++) {
1211 if (chan < num_outs) {
1212 snprintf(name, sizeof(name),
1213 "PCM%d-Out%d Playback Volume",
1214 chan + 1, out + 1);
1215 } else {
1216 snprintf(name, sizeof(name),
1217 "In%d-Out%d Playback Volume",
1218 chan - num_outs + 1, out + 1);
1221 cmask = (out == 0) ? 0 : 1 << (out - 1);
1222 offset = chan * num_outs;
1223 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1224 cmask, val_type, offset, name,
1225 &snd_usb_mixer_vol_tlv);
1226 if (err < 0)
1227 return err;
1231 return 0;
1234 /* This control needs a volume quirk, see mixer.c */
1235 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1237 static const char name[] = "Effect Volume";
1238 const unsigned int id = 0x43;
1239 const int val_type = USB_MIXER_U8;
1240 const unsigned int control = 3;
1241 const unsigned int cmask = 0;
1243 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1244 name, snd_usb_mixer_vol_tlv);
1247 /* This control needs a volume quirk, see mixer.c */
1248 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1250 static const char name[] = "Effect Duration";
1251 const unsigned int id = 0x43;
1252 const int val_type = USB_MIXER_S16;
1253 const unsigned int control = 4;
1254 const unsigned int cmask = 0;
1256 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1257 name, snd_usb_mixer_vol_tlv);
1260 /* This control needs a volume quirk, see mixer.c */
1261 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1263 static const char name[] = "Effect Feedback Volume";
1264 const unsigned int id = 0x43;
1265 const int val_type = USB_MIXER_U8;
1266 const unsigned int control = 5;
1267 const unsigned int cmask = 0;
1269 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1270 name, NULL);
1273 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1275 char name[64];
1276 unsigned int cmask;
1277 int chan, err;
1278 int num_outs = 0;
1279 int num_ins = 0;
1281 const unsigned int id = 0x42;
1282 const int val_type = USB_MIXER_S16;
1283 const int control = 1;
1285 switch (mixer->chip->usb_id) {
1286 case USB_ID(0x0763, 0x2030):
1287 num_outs = 6;
1288 num_ins = 4;
1289 break;
1290 case USB_ID(0x0763, 0x2031):
1291 num_outs = 8;
1292 num_ins = 6;
1293 break;
1296 for (chan = 0; chan < num_outs + num_ins; chan++) {
1297 if (chan < num_outs) {
1298 snprintf(name, sizeof(name),
1299 "Effect Send DOut%d",
1300 chan + 1);
1301 } else {
1302 snprintf(name, sizeof(name),
1303 "Effect Send AIn%d",
1304 chan - num_outs + 1);
1307 cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1308 err = snd_create_std_mono_ctl(mixer, id, control,
1309 cmask, val_type, name,
1310 &snd_usb_mixer_vol_tlv);
1311 if (err < 0)
1312 return err;
1315 return 0;
1318 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1320 char name[64];
1321 unsigned int cmask;
1322 int chan, err;
1323 int num_outs = 0;
1324 int offset = 0;
1326 const unsigned int id = 0x40;
1327 const int val_type = USB_MIXER_S16;
1328 const int control = 1;
1330 switch (mixer->chip->usb_id) {
1331 case USB_ID(0x0763, 0x2030):
1332 num_outs = 6;
1333 offset = 0x3c;
1334 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1335 break;
1336 case USB_ID(0x0763, 0x2031):
1337 num_outs = 8;
1338 offset = 0x70;
1339 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1340 break;
1343 for (chan = 0; chan < num_outs; chan++) {
1344 snprintf(name, sizeof(name),
1345 "Effect Return %d",
1346 chan + 1);
1348 cmask = (chan == 0) ? 0 :
1349 1 << (chan + (chan % 2) * num_outs - 1);
1350 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1351 cmask, val_type, offset, name,
1352 &snd_usb_mixer_vol_tlv);
1353 if (err < 0)
1354 return err;
1357 return 0;
1360 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1362 int err;
1364 err = snd_c400_create_vol_ctls(mixer);
1365 if (err < 0)
1366 return err;
1368 err = snd_c400_create_effect_vol_ctls(mixer);
1369 if (err < 0)
1370 return err;
1372 err = snd_c400_create_effect_ret_vol_ctls(mixer);
1373 if (err < 0)
1374 return err;
1376 err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1377 if (err < 0)
1378 return err;
1380 err = snd_c400_create_effect_volume_ctl(mixer);
1381 if (err < 0)
1382 return err;
1384 err = snd_c400_create_effect_duration_ctl(mixer);
1385 if (err < 0)
1386 return err;
1388 err = snd_c400_create_effect_feedback_ctl(mixer);
1389 if (err < 0)
1390 return err;
1392 return 0;
1396 * The mixer units for Ebox-44 are corrupt, and even where they
1397 * are valid they presents mono controls as L and R channels of
1398 * stereo. So we provide a good mixer here.
1400 static struct std_mono_table ebox44_table[] = {
1402 .unitid = 4,
1403 .control = 1,
1404 .cmask = 0x0,
1405 .val_type = USB_MIXER_INV_BOOLEAN,
1406 .name = "Headphone Playback Switch"
1409 .unitid = 4,
1410 .control = 2,
1411 .cmask = 0x1,
1412 .val_type = USB_MIXER_S16,
1413 .name = "Headphone A Mix Playback Volume"
1416 .unitid = 4,
1417 .control = 2,
1418 .cmask = 0x2,
1419 .val_type = USB_MIXER_S16,
1420 .name = "Headphone B Mix Playback Volume"
1424 .unitid = 7,
1425 .control = 1,
1426 .cmask = 0x0,
1427 .val_type = USB_MIXER_INV_BOOLEAN,
1428 .name = "Output Playback Switch"
1431 .unitid = 7,
1432 .control = 2,
1433 .cmask = 0x1,
1434 .val_type = USB_MIXER_S16,
1435 .name = "Output A Playback Volume"
1438 .unitid = 7,
1439 .control = 2,
1440 .cmask = 0x2,
1441 .val_type = USB_MIXER_S16,
1442 .name = "Output B Playback Volume"
1446 .unitid = 10,
1447 .control = 1,
1448 .cmask = 0x0,
1449 .val_type = USB_MIXER_INV_BOOLEAN,
1450 .name = "Input Capture Switch"
1453 .unitid = 10,
1454 .control = 2,
1455 .cmask = 0x1,
1456 .val_type = USB_MIXER_S16,
1457 .name = "Input A Capture Volume"
1460 .unitid = 10,
1461 .control = 2,
1462 .cmask = 0x2,
1463 .val_type = USB_MIXER_S16,
1464 .name = "Input B Capture Volume"
1470 /* Audio Advantage Micro II findings:
1472 * Mapping spdif AES bits to vendor register.bit:
1473 * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1474 * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1475 * AES2: [0 0 0 0 0 0 0 0]
1476 * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1477 * (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1479 * power on values:
1480 * r2: 0x10
1481 * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1482 * just after it to 0xa0, presumably it disables/mutes some analog
1483 * parts when there is no audio.)
1484 * r9: 0x28
1486 * Optical transmitter on/off:
1487 * vendor register.bit: 9.1
1488 * 0 - on (0x28 register value)
1489 * 1 - off (0x2a register value)
1492 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_info *uinfo)
1495 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1496 uinfo->count = 1;
1497 return 0;
1500 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1503 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1504 struct snd_usb_audio *chip = list->mixer->chip;
1505 int err;
1506 struct usb_interface *iface;
1507 struct usb_host_interface *alts;
1508 unsigned int ep;
1509 unsigned char data[3];
1510 int rate;
1512 err = snd_usb_lock_shutdown(chip);
1513 if (err < 0)
1514 return err;
1516 ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1517 ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1518 ucontrol->value.iec958.status[2] = 0x00;
1520 /* use known values for that card: interface#1 altsetting#1 */
1521 iface = usb_ifnum_to_if(chip->dev, 1);
1522 if (!iface || iface->num_altsetting < 2)
1523 return -EINVAL;
1524 alts = &iface->altsetting[1];
1525 if (get_iface_desc(alts)->bNumEndpoints < 1)
1526 return -EINVAL;
1527 ep = get_endpoint(alts, 0)->bEndpointAddress;
1529 err = snd_usb_ctl_msg(chip->dev,
1530 usb_rcvctrlpipe(chip->dev, 0),
1531 UAC_GET_CUR,
1532 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1533 UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1535 data,
1536 sizeof(data));
1537 if (err < 0)
1538 goto end;
1540 rate = data[0] | (data[1] << 8) | (data[2] << 16);
1541 ucontrol->value.iec958.status[3] = (rate == 48000) ?
1542 IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1544 err = 0;
1545 end:
1546 snd_usb_unlock_shutdown(chip);
1547 return err;
1550 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1552 struct snd_usb_audio *chip = list->mixer->chip;
1553 unsigned int pval = list->kctl->private_value;
1554 u8 reg;
1555 int err;
1557 err = snd_usb_lock_shutdown(chip);
1558 if (err < 0)
1559 return err;
1561 reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1562 err = snd_usb_ctl_msg(chip->dev,
1563 usb_sndctrlpipe(chip->dev, 0),
1564 UAC_SET_CUR,
1565 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1566 reg,
1568 NULL,
1570 if (err < 0)
1571 goto end;
1573 reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1574 reg |= (pval >> 12) & 0x0f;
1575 err = snd_usb_ctl_msg(chip->dev,
1576 usb_sndctrlpipe(chip->dev, 0),
1577 UAC_SET_CUR,
1578 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1579 reg,
1581 NULL,
1583 if (err < 0)
1584 goto end;
1586 end:
1587 snd_usb_unlock_shutdown(chip);
1588 return err;
1591 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
1594 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1595 unsigned int pval, pval_old;
1596 int err;
1598 pval = pval_old = kcontrol->private_value;
1599 pval &= 0xfffff0f0;
1600 pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1601 pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1603 pval &= 0xffff0fff;
1604 pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1606 /* The frequency bits in AES3 cannot be set via register access. */
1608 /* Silently ignore any bits from the request that cannot be set. */
1610 if (pval == pval_old)
1611 return 0;
1613 kcontrol->private_value = pval;
1614 err = snd_microii_spdif_default_update(list);
1615 return err < 0 ? err : 1;
1618 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1621 ucontrol->value.iec958.status[0] = 0x0f;
1622 ucontrol->value.iec958.status[1] = 0xff;
1623 ucontrol->value.iec958.status[2] = 0x00;
1624 ucontrol->value.iec958.status[3] = 0x00;
1626 return 0;
1629 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1630 struct snd_ctl_elem_value *ucontrol)
1632 ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1634 return 0;
1637 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1639 struct snd_usb_audio *chip = list->mixer->chip;
1640 u8 reg = list->kctl->private_value;
1641 int err;
1643 err = snd_usb_lock_shutdown(chip);
1644 if (err < 0)
1645 return err;
1647 err = snd_usb_ctl_msg(chip->dev,
1648 usb_sndctrlpipe(chip->dev, 0),
1649 UAC_SET_CUR,
1650 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1651 reg,
1653 NULL,
1656 snd_usb_unlock_shutdown(chip);
1657 return err;
1660 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1661 struct snd_ctl_elem_value *ucontrol)
1663 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1664 u8 reg;
1665 int err;
1667 reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1668 if (reg != list->kctl->private_value)
1669 return 0;
1671 kcontrol->private_value = reg;
1672 err = snd_microii_spdif_switch_update(list);
1673 return err < 0 ? err : 1;
1676 static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1678 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1679 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1680 .info = snd_microii_spdif_info,
1681 .get = snd_microii_spdif_default_get,
1682 .put = snd_microii_spdif_default_put,
1683 .private_value = 0x00000100UL,/* reset value */
1686 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1687 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1688 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1689 .info = snd_microii_spdif_info,
1690 .get = snd_microii_spdif_mask_get,
1693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1694 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1695 .info = snd_ctl_boolean_mono_info,
1696 .get = snd_microii_spdif_switch_get,
1697 .put = snd_microii_spdif_switch_put,
1698 .private_value = 0x00000028UL,/* reset value */
1702 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1704 int err, i;
1705 static usb_mixer_elem_resume_func_t resume_funcs[] = {
1706 snd_microii_spdif_default_update,
1707 NULL,
1708 snd_microii_spdif_switch_update
1711 for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1712 err = add_single_ctl_with_resume(mixer, 0,
1713 resume_funcs[i],
1714 &snd_microii_mixer_spdif[i],
1715 NULL);
1716 if (err < 0)
1717 return err;
1720 return 0;
1723 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
1725 int err = 0;
1726 struct snd_info_entry *entry;
1728 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1729 return err;
1731 switch (mixer->chip->usb_id) {
1732 case USB_ID(0x041e, 0x3020):
1733 case USB_ID(0x041e, 0x3040):
1734 case USB_ID(0x041e, 0x3042):
1735 case USB_ID(0x041e, 0x30df):
1736 case USB_ID(0x041e, 0x3048):
1737 err = snd_audigy2nx_controls_create(mixer);
1738 if (err < 0)
1739 break;
1740 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1741 snd_info_set_text_ops(entry, mixer,
1742 snd_audigy2nx_proc_read);
1743 break;
1745 /* EMU0204 */
1746 case USB_ID(0x041e, 0x3f19):
1747 err = snd_emu0204_controls_create(mixer);
1748 if (err < 0)
1749 break;
1750 break;
1752 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1753 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
1754 err = snd_c400_create_mixer(mixer);
1755 break;
1757 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1758 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1759 err = snd_ftu_create_mixer(mixer);
1760 break;
1762 case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
1763 case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
1764 case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
1765 err = snd_xonar_u1_controls_create(mixer);
1766 break;
1768 case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
1769 err = snd_microii_controls_create(mixer);
1770 break;
1772 case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
1773 err = snd_mbox1_create_sync_switch(mixer);
1774 break;
1776 case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1777 err = snd_nativeinstruments_create_mixer(mixer,
1778 snd_nativeinstruments_ta6_mixers,
1779 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1780 break;
1782 case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1783 err = snd_nativeinstruments_create_mixer(mixer,
1784 snd_nativeinstruments_ta10_mixers,
1785 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1786 break;
1788 case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1789 /* detection is disabled in mixer_maps.c */
1790 err = snd_create_std_mono_table(mixer, ebox44_table);
1791 break;
1793 case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
1794 case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
1795 case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
1796 case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
1797 case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
1798 err = snd_scarlett_controls_create(mixer);
1799 break;
1802 return err;
1805 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1806 int unitid)
1808 if (!mixer->rc_cfg)
1809 return;
1810 /* unit ids specific to Extigy/Audigy 2 NX: */
1811 switch (unitid) {
1812 case 0: /* remote control */
1813 mixer->rc_urb->dev = mixer->chip->dev;
1814 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1815 break;
1816 case 4: /* digital in jack */
1817 case 7: /* line in jacks */
1818 case 19: /* speaker out jacks */
1819 case 20: /* headphones out jack */
1820 break;
1821 /* live24ext: 4 = line-in jack */
1822 case 3: /* hp-out jack (may actuate Mute) */
1823 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1824 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1825 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1826 break;
1827 default:
1828 usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
1829 break;
1833 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
1834 struct usb_mixer_elem_info *cval,
1835 struct snd_kcontrol *kctl)
1837 /* Approximation using 10 ranges based on output measurement on hw v1.2.
1838 * This seems close to the cubic mapping e.g. alsamixer uses. */
1839 static const DECLARE_TLV_DB_RANGE(scale,
1840 0, 1, TLV_DB_MINMAX_ITEM(-5300, -4970),
1841 2, 5, TLV_DB_MINMAX_ITEM(-4710, -4160),
1842 6, 7, TLV_DB_MINMAX_ITEM(-3884, -3710),
1843 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
1844 15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
1845 17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
1846 20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
1847 27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
1848 32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
1849 41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
1852 if (cval->min == 0 && cval->max == 50) {
1853 usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
1854 kctl->tlv.p = scale;
1855 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1856 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1858 } else if (cval->min == 0 && cval->max <= 1000) {
1859 /* Some other clearly broken DragonFly variant.
1860 * At least a 0..53 variant (hw v1.0) exists.
1862 usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
1863 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1867 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
1868 struct usb_mixer_elem_info *cval, int unitid,
1869 struct snd_kcontrol *kctl)
1871 switch (mixer->chip->usb_id) {
1872 case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
1873 if (unitid == 7 && cval->control == UAC_FU_VOLUME)
1874 snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
1875 break;