Add linux-next specific files for 20110421
[linux-2.6/next.git] / sound / usb / mixer_quirks.c
blob4a7ad7ed62f7691bf175345467a40a15ad3e77f8
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)
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.
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.
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
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/usb.h>
31 #include <linux/usb/audio.h>
33 #include <sound/core.h>
34 #include <sound/control.h>
35 #include <sound/hwdep.h>
36 #include <sound/info.h>
38 #include "usbaudio.h"
39 #include "mixer.h"
40 #include "mixer_quirks.h"
41 #include "helper.h"
44 * Sound Blaster remote control configuration
46 * format of remote control data:
47 * Extigy: xx 00
48 * Audigy 2 NX: 06 80 xx 00 00 00
49 * Live! 24-bit: 06 80 xx yy 22 83
51 static const struct rc_config {
52 u32 usb_id;
53 u8 offset;
54 u8 length;
55 u8 packet_length;
56 u8 min_packet_length; /* minimum accepted length of the URB result */
57 u8 mute_mixer_id;
58 u32 mute_code;
59 } rc_configs[] = {
60 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
61 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
62 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
63 { USB_ID(0x041e, 0x3042), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 */
64 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
67 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
69 struct usb_mixer_interface *mixer = urb->context;
70 const struct rc_config *rc = mixer->rc_cfg;
71 u32 code;
73 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
74 return;
76 code = mixer->rc_buffer[rc->offset];
77 if (rc->length == 2)
78 code |= mixer->rc_buffer[rc->offset + 1] << 8;
80 /* the Mute button actually changes the mixer control */
81 if (code == rc->mute_code)
82 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
83 mixer->rc_code = code;
84 wmb();
85 wake_up(&mixer->rc_waitq);
88 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
89 long count, loff_t *offset)
91 struct usb_mixer_interface *mixer = hw->private_data;
92 int err;
93 u32 rc_code;
95 if (count != 1 && count != 4)
96 return -EINVAL;
97 err = wait_event_interruptible(mixer->rc_waitq,
98 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
99 if (err == 0) {
100 if (count == 1)
101 err = put_user(rc_code, buf);
102 else
103 err = put_user(rc_code, (u32 __user *)buf);
105 return err < 0 ? err : count;
108 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
109 poll_table *wait)
111 struct usb_mixer_interface *mixer = hw->private_data;
113 poll_wait(file, &mixer->rc_waitq, wait);
114 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
117 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
119 struct snd_hwdep *hwdep;
120 int err, len, i;
122 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
123 if (rc_configs[i].usb_id == mixer->chip->usb_id)
124 break;
125 if (i >= ARRAY_SIZE(rc_configs))
126 return 0;
127 mixer->rc_cfg = &rc_configs[i];
129 len = mixer->rc_cfg->packet_length;
131 init_waitqueue_head(&mixer->rc_waitq);
132 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
133 if (err < 0)
134 return err;
135 snprintf(hwdep->name, sizeof(hwdep->name),
136 "%s remote control", mixer->chip->card->shortname);
137 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
138 hwdep->private_data = mixer;
139 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
140 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
141 hwdep->exclusive = 1;
143 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
144 if (!mixer->rc_urb)
145 return -ENOMEM;
146 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
147 if (!mixer->rc_setup_packet) {
148 usb_free_urb(mixer->rc_urb);
149 mixer->rc_urb = NULL;
150 return -ENOMEM;
152 mixer->rc_setup_packet->bRequestType =
153 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
154 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
155 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
156 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
157 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
158 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
159 usb_rcvctrlpipe(mixer->chip->dev, 0),
160 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
161 snd_usb_soundblaster_remote_complete, mixer);
162 return 0;
165 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
167 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
169 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
170 int index = kcontrol->private_value;
172 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
173 return 0;
176 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
178 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
179 int index = kcontrol->private_value;
180 int value = ucontrol->value.integer.value[0];
181 int err, changed;
183 if (value > 1)
184 return -EINVAL;
185 changed = value != mixer->audigy2nx_leds[index];
186 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
187 err = snd_usb_ctl_msg(mixer->chip->dev,
188 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
189 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
190 !value, 0, NULL, 0, 100);
191 else
192 err = snd_usb_ctl_msg(mixer->chip->dev,
193 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
195 value, index + 2, NULL, 0, 100);
196 if (err < 0)
197 return err;
198 mixer->audigy2nx_leds[index] = value;
199 return changed;
202 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
204 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
205 .name = "CMSS LED Switch",
206 .info = snd_audigy2nx_led_info,
207 .get = snd_audigy2nx_led_get,
208 .put = snd_audigy2nx_led_put,
209 .private_value = 0,
212 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
213 .name = "Power LED Switch",
214 .info = snd_audigy2nx_led_info,
215 .get = snd_audigy2nx_led_get,
216 .put = snd_audigy2nx_led_put,
217 .private_value = 1,
220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
221 .name = "Dolby Digital LED Switch",
222 .info = snd_audigy2nx_led_info,
223 .get = snd_audigy2nx_led_get,
224 .put = snd_audigy2nx_led_put,
225 .private_value = 2,
229 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
231 int i, err;
233 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
234 /* USB X-Fi S51 doesn't have a CMSS LED */
235 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
236 continue;
237 if (i > 1 && /* Live24ext has 2 LEDs only */
238 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
239 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
240 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
241 break;
242 err = snd_ctl_add(mixer->chip->card,
243 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
244 if (err < 0)
245 return err;
247 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
248 return 0;
251 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
252 struct snd_info_buffer *buffer)
254 static const struct sb_jack {
255 int unitid;
256 const char *name;
257 } jacks_audigy2nx[] = {
258 {4, "dig in "},
259 {7, "line in"},
260 {19, "spk out"},
261 {20, "hph out"},
262 {-1, NULL}
263 }, jacks_live24ext[] = {
264 {4, "line in"}, /* &1=Line, &2=Mic*/
265 {3, "hph out"}, /* headphones */
266 {0, "RC "}, /* last command, 6 bytes see rc_config above */
267 {-1, NULL}
269 const struct sb_jack *jacks;
270 struct usb_mixer_interface *mixer = entry->private_data;
271 int i, err;
272 u8 buf[3];
274 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
275 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
276 jacks = jacks_audigy2nx;
277 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
278 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
279 jacks = jacks_live24ext;
280 else
281 return;
283 for (i = 0; jacks[i].name; ++i) {
284 snd_iprintf(buffer, "%s: ", jacks[i].name);
285 err = snd_usb_ctl_msg(mixer->chip->dev,
286 usb_rcvctrlpipe(mixer->chip->dev, 0),
287 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
288 USB_RECIP_INTERFACE, 0,
289 jacks[i].unitid << 8, buf, 3, 100);
290 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
291 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
292 else
293 snd_iprintf(buffer, "?\n");
297 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_value *ucontrol)
300 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
302 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
303 return 0;
306 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
307 struct snd_ctl_elem_value *ucontrol)
309 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
310 u8 old_status, new_status;
311 int err, changed;
313 old_status = mixer->xonar_u1_status;
314 if (ucontrol->value.integer.value[0])
315 new_status = old_status | 0x02;
316 else
317 new_status = old_status & ~0x02;
318 changed = new_status != old_status;
319 err = snd_usb_ctl_msg(mixer->chip->dev,
320 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
321 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
322 50, 0, &new_status, 1, 100);
323 if (err < 0)
324 return err;
325 mixer->xonar_u1_status = new_status;
326 return changed;
329 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
330 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
331 .name = "Digital Playback Switch",
332 .info = snd_ctl_boolean_mono_info,
333 .get = snd_xonar_u1_switch_get,
334 .put = snd_xonar_u1_switch_put,
337 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
339 int err;
341 err = snd_ctl_add(mixer->chip->card,
342 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
343 if (err < 0)
344 return err;
345 mixer->xonar_u1_status = 0x05;
346 return 0;
349 /* Native Instruments device quirks */
351 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
353 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
354 struct snd_ctl_elem_value *ucontrol)
356 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
357 struct usb_device *dev = mixer->chip->dev;
358 u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
359 u16 wIndex = kcontrol->private_value & 0xffff;
360 u8 tmp;
362 int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
363 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
364 0, cpu_to_le16(wIndex),
365 &tmp, sizeof(tmp), 1000);
367 if (ret < 0) {
368 snd_printk(KERN_ERR
369 "unable to issue vendor read request (ret = %d)", ret);
370 return ret;
373 ucontrol->value.integer.value[0] = tmp;
375 return 0;
378 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
381 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
382 struct usb_device *dev = mixer->chip->dev;
383 u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
384 u16 wIndex = kcontrol->private_value & 0xffff;
385 u16 wValue = ucontrol->value.integer.value[0];
387 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
388 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
389 cpu_to_le16(wValue), cpu_to_le16(wIndex),
390 NULL, 0, 1000);
392 if (ret < 0) {
393 snd_printk(KERN_ERR
394 "unable to issue vendor write request (ret = %d)", ret);
395 return ret;
398 return 0;
401 static struct snd_kcontrol_new snd_nativeinstruments_ak6_mixers[] = {
403 .name = "Direct Monitor Channel 1+2",
404 .private_value = _MAKE_NI_CONTROL(0x03, 0x03),
407 .name = "Direct Monitor Channel 3+4",
408 .private_value = _MAKE_NI_CONTROL(0x03, 0x05),
412 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
414 .name = "Direct Thru Channel A",
415 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
418 .name = "Direct Thru Channel B",
419 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
422 .name = "Phono Input Channel A",
423 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
426 .name = "Phono Input Channel B",
427 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
431 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
433 .name = "Direct Thru Channel A",
434 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
437 .name = "Direct Thru Channel B",
438 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
441 .name = "Direct Thru Channel C",
442 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
445 .name = "Direct Thru Channel D",
446 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
449 .name = "Phono Input Channel A",
450 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
453 .name = "Phono Input Channel B",
454 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
457 .name = "Phono Input Channel C",
458 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
461 .name = "Phono Input Channel D",
462 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
466 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
467 const struct snd_kcontrol_new *kc,
468 unsigned int count)
470 int i, err = 0;
471 struct snd_kcontrol_new template = {
472 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
473 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
474 .get = snd_nativeinstruments_control_get,
475 .put = snd_nativeinstruments_control_put,
476 .info = snd_ctl_boolean_mono_info,
479 for (i = 0; i < count; i++) {
480 struct snd_kcontrol *c;
482 template.name = kc[i].name;
483 template.private_value = kc[i].private_value;
485 c = snd_ctl_new1(&template, mixer);
486 err = snd_ctl_add(mixer->chip->card, c);
488 if (err < 0)
489 break;
492 return err;
495 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
496 unsigned char samplerate_id)
498 struct usb_mixer_interface *mixer;
499 struct usb_mixer_elem_info *cval;
500 int unitid = 12; /* SamleRate ExtensionUnit ID */
502 list_for_each_entry(mixer, &chip->mixer_list, list) {
503 cval = mixer->id_elems[unitid];
504 if (cval) {
505 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
506 cval->control << 8,
507 samplerate_id);
508 snd_usb_mixer_notify_id(mixer, unitid);
510 break;
514 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
516 int err = 0;
517 struct snd_info_entry *entry;
519 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
520 return err;
522 switch (mixer->chip->usb_id) {
523 case USB_ID(0x041e, 0x3020):
524 case USB_ID(0x041e, 0x3040):
525 case USB_ID(0x041e, 0x3042):
526 case USB_ID(0x041e, 0x3048):
527 err = snd_audigy2nx_controls_create(mixer);
528 if (err < 0)
529 break;
530 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
531 snd_info_set_text_ops(entry, mixer,
532 snd_audigy2nx_proc_read);
533 break;
535 case USB_ID(0x0b05, 0x1739):
536 case USB_ID(0x0b05, 0x1743):
537 err = snd_xonar_u1_controls_create(mixer);
538 break;
540 case USB_ID(0x17cc, 0x1001): /* Audio Kontrol 6 */
541 err = snd_nativeinstruments_create_mixer(mixer,
542 snd_nativeinstruments_ak6_mixers,
543 ARRAY_SIZE(snd_nativeinstruments_ak6_mixers));
544 break;
546 case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
547 err = snd_nativeinstruments_create_mixer(mixer,
548 snd_nativeinstruments_ta6_mixers,
549 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
550 break;
552 case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
553 err = snd_nativeinstruments_create_mixer(mixer,
554 snd_nativeinstruments_ta10_mixers,
555 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
556 break;
559 return err;
562 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
563 int unitid)
565 if (!mixer->rc_cfg)
566 return;
567 /* unit ids specific to Extigy/Audigy 2 NX: */
568 switch (unitid) {
569 case 0: /* remote control */
570 mixer->rc_urb->dev = mixer->chip->dev;
571 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
572 break;
573 case 4: /* digital in jack */
574 case 7: /* line in jacks */
575 case 19: /* speaker out jacks */
576 case 20: /* headphones out jack */
577 break;
578 /* live24ext: 4 = line-in jack */
579 case 3: /* hp-out jack (may actuate Mute) */
580 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
581 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
582 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
583 break;
584 default:
585 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
586 break;