56b665938b1b500f153ce5a7e6589c028993d3d2
[linux-2.6.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/usb.h>
30 #include <linux/usb/audio.h>
31
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/hwdep.h>
35 #include <sound/info.h>
36
37 #include "usbaudio.h"
38 #include "mixer.h"
39 #include "mixer_quirks.h"
40 #include "helper.h"
41
42 /*
43  * Sound Blaster remote control configuration
44  *
45  * format of remote control data:
46  * Extigy:       xx 00
47  * Audigy 2 NX:  06 80 xx 00 00 00
48  * Live! 24-bit: 06 80 xx yy 22 83
49  */
50 static const struct rc_config {
51         u32 usb_id;
52         u8  offset;
53         u8  length;
54         u8  packet_length;
55         u8  min_packet_length; /* minimum accepted length of the URB result */
56         u8  mute_mixer_id;
57         u32 mute_code;
58 } rc_configs[] = {
59         { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
60         { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
61         { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
62         { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
63 };
64
65 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
66 {
67         struct usb_mixer_interface *mixer = urb->context;
68         const struct rc_config *rc = mixer->rc_cfg;
69         u32 code;
70
71         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
72                 return;
73
74         code = mixer->rc_buffer[rc->offset];
75         if (rc->length == 2)
76                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
77
78         /* the Mute button actually changes the mixer control */
79         if (code == rc->mute_code)
80                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
81         mixer->rc_code = code;
82         wmb();
83         wake_up(&mixer->rc_waitq);
84 }
85
86 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
87                                      long count, loff_t *offset)
88 {
89         struct usb_mixer_interface *mixer = hw->private_data;
90         int err;
91         u32 rc_code;
92
93         if (count != 1 && count != 4)
94                 return -EINVAL;
95         err = wait_event_interruptible(mixer->rc_waitq,
96                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
97         if (err == 0) {
98                 if (count == 1)
99                         err = put_user(rc_code, buf);
100                 else
101                         err = put_user(rc_code, (u32 __user *)buf);
102         }
103         return err < 0 ? err : count;
104 }
105
106 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
107                                             poll_table *wait)
108 {
109         struct usb_mixer_interface *mixer = hw->private_data;
110
111         poll_wait(file, &mixer->rc_waitq, wait);
112         return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
113 }
114
115 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
116 {
117         struct snd_hwdep *hwdep;
118         int err, len, i;
119
120         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
121                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
122                         break;
123         if (i >= ARRAY_SIZE(rc_configs))
124                 return 0;
125         mixer->rc_cfg = &rc_configs[i];
126
127         len = mixer->rc_cfg->packet_length;
128
129         init_waitqueue_head(&mixer->rc_waitq);
130         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
131         if (err < 0)
132                 return err;
133         snprintf(hwdep->name, sizeof(hwdep->name),
134                  "%s remote control", mixer->chip->card->shortname);
135         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
136         hwdep->private_data = mixer;
137         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
138         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
139         hwdep->exclusive = 1;
140
141         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
142         if (!mixer->rc_urb)
143                 return -ENOMEM;
144         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
145         if (!mixer->rc_setup_packet) {
146                 usb_free_urb(mixer->rc_urb);
147                 mixer->rc_urb = NULL;
148                 return -ENOMEM;
149         }
150         mixer->rc_setup_packet->bRequestType =
151                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
152         mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
153         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
154         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
155         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
156         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
157                              usb_rcvctrlpipe(mixer->chip->dev, 0),
158                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
159                              snd_usb_soundblaster_remote_complete, mixer);
160         return 0;
161 }
162
163 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
164
165 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
166 {
167         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
168         int index = kcontrol->private_value;
169
170         ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
171         return 0;
172 }
173
174 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
175 {
176         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
177         int index = kcontrol->private_value;
178         int value = ucontrol->value.integer.value[0];
179         int err, changed;
180
181         if (value > 1)
182                 return -EINVAL;
183         changed = value != mixer->audigy2nx_leds[index];
184         err = snd_usb_ctl_msg(mixer->chip->dev,
185                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
186                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
187                               value, index + 2, NULL, 0, 100);
188         if (err < 0)
189                 return err;
190         mixer->audigy2nx_leds[index] = value;
191         return changed;
192 }
193
194 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
195         {
196                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
197                 .name = "CMSS LED Switch",
198                 .info = snd_audigy2nx_led_info,
199                 .get = snd_audigy2nx_led_get,
200                 .put = snd_audigy2nx_led_put,
201                 .private_value = 0,
202         },
203         {
204                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
205                 .name = "Power LED Switch",
206                 .info = snd_audigy2nx_led_info,
207                 .get = snd_audigy2nx_led_get,
208                 .put = snd_audigy2nx_led_put,
209                 .private_value = 1,
210         },
211         {
212                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
213                 .name = "Dolby Digital LED Switch",
214                 .info = snd_audigy2nx_led_info,
215                 .get = snd_audigy2nx_led_get,
216                 .put = snd_audigy2nx_led_put,
217                 .private_value = 2,
218         },
219 };
220
221 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
222 {
223         int i, err;
224
225         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
226                 if (i > 1 && /* Live24ext has 2 LEDs only */
227                         (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
228                          mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
229                         break; 
230                 err = snd_ctl_add(mixer->chip->card,
231                                   snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
232                 if (err < 0)
233                         return err;
234         }
235         mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
236         return 0;
237 }
238
239 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
240                                     struct snd_info_buffer *buffer)
241 {
242         static const struct sb_jack {
243                 int unitid;
244                 const char *name;
245         }  jacks_audigy2nx[] = {
246                 {4,  "dig in "},
247                 {7,  "line in"},
248                 {19, "spk out"},
249                 {20, "hph out"},
250                 {-1, NULL}
251         }, jacks_live24ext[] = {
252                 {4,  "line in"}, /* &1=Line, &2=Mic*/
253                 {3,  "hph out"}, /* headphones */
254                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
255                 {-1, NULL}
256         };
257         const struct sb_jack *jacks;
258         struct usb_mixer_interface *mixer = entry->private_data;
259         int i, err;
260         u8 buf[3];
261
262         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
263         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
264                 jacks = jacks_audigy2nx;
265         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
266                  mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
267                 jacks = jacks_live24ext;
268         else
269                 return;
270
271         for (i = 0; jacks[i].name; ++i) {
272                 snd_iprintf(buffer, "%s: ", jacks[i].name);
273                 err = snd_usb_ctl_msg(mixer->chip->dev,
274                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
275                                       UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
276                                       USB_RECIP_INTERFACE, 0,
277                                       jacks[i].unitid << 8, buf, 3, 100);
278                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
279                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
280                 else
281                         snd_iprintf(buffer, "?\n");
282         }
283 }
284
285 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
286                                    struct snd_ctl_elem_value *ucontrol)
287 {
288         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
289
290         ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
291         return 0;
292 }
293
294 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
295                                    struct snd_ctl_elem_value *ucontrol)
296 {
297         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
298         u8 old_status, new_status;
299         int err, changed;
300
301         old_status = mixer->xonar_u1_status;
302         if (ucontrol->value.integer.value[0])
303                 new_status = old_status | 0x02;
304         else
305                 new_status = old_status & ~0x02;
306         changed = new_status != old_status;
307         err = snd_usb_ctl_msg(mixer->chip->dev,
308                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
309                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
310                               50, 0, &new_status, 1, 100);
311         if (err < 0)
312                 return err;
313         mixer->xonar_u1_status = new_status;
314         return changed;
315 }
316
317 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
318         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
319         .name = "Digital Playback Switch",
320         .info = snd_ctl_boolean_mono_info,
321         .get = snd_xonar_u1_switch_get,
322         .put = snd_xonar_u1_switch_put,
323 };
324
325 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
326 {
327         int err;
328
329         err = snd_ctl_add(mixer->chip->card,
330                           snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
331         if (err < 0)
332                 return err;
333         mixer->xonar_u1_status = 0x05;
334         return 0;
335 }
336
337 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
338                                unsigned char samplerate_id)
339 {
340         struct usb_mixer_interface *mixer;
341         struct usb_mixer_elem_info *cval;
342         int unitid = 12; /* SamleRate ExtensionUnit ID */
343
344         list_for_each_entry(mixer, &chip->mixer_list, list) {
345                 cval = mixer->id_elems[unitid];
346                 if (cval) {
347                         snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
348                                                     cval->control << 8,
349                                                     samplerate_id);
350                         snd_usb_mixer_notify_id(mixer, unitid);
351                 }
352                 break;
353         }
354 }
355
356 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
357 {
358         int err;
359         struct snd_info_entry *entry;
360
361         if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
362                 return err;
363
364         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
365             mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
366             mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
367                 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
368                         return err;
369                 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
370                         snd_info_set_text_ops(entry, mixer,
371                                               snd_audigy2nx_proc_read);
372         }
373
374         if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
375             mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
376                 err = snd_xonar_u1_controls_create(mixer);
377                 if (err < 0)
378                         return err;
379         }
380
381         return 0;
382 }
383
384 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
385                                     int unitid)
386 {
387         if (!mixer->rc_cfg)
388                 return;
389         /* unit ids specific to Extigy/Audigy 2 NX: */
390         switch (unitid) {
391         case 0: /* remote control */
392                 mixer->rc_urb->dev = mixer->chip->dev;
393                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
394                 break;
395         case 4: /* digital in jack */
396         case 7: /* line in jacks */
397         case 19: /* speaker out jacks */
398         case 20: /* headphones out jack */
399                 break;
400         /* live24ext: 4 = line-in jack */
401         case 3: /* hp-out jack (may actuate Mute) */
402                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
403                     mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
404                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
405                 break;
406         default:
407                 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
408                 break;
409         }
410 }
411