f7ad2078c0cf78adb27cef7a6107d3ab900e120e
[deliverable/linux.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 * Audio Advantage Micro II support added by:
13 * Przemek Rudy (prudy1@o2.pl)
14 *
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.
19 *
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.
24 *
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
28 */
29
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/usb.h>
33 #include <linux/usb/audio.h>
34
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
41 #include "usbaudio.h"
42 #include "mixer.h"
43 #include "mixer_quirks.h"
44 #include "mixer_scarlett.h"
45 #include "helper.h"
46
47 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
48
49 struct std_mono_table {
50 unsigned int unitid, control, cmask;
51 int val_type;
52 const char *name;
53 snd_kcontrol_tlv_rw_t *tlv_callback;
54 };
55
56 /* This function allows for the creation of standard UAC controls.
57 * See the quirks for M-Audio FTUs or Ebox-44.
58 * If you don't want to set a TLV callback pass NULL.
59 *
60 * Since there doesn't seem to be a devices that needs a multichannel
61 * version, we keep it mono for simplicity.
62 */
63 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
64 unsigned int unitid,
65 unsigned int control,
66 unsigned int cmask,
67 int val_type,
68 unsigned int idx_off,
69 const char *name,
70 snd_kcontrol_tlv_rw_t *tlv_callback)
71 {
72 struct usb_mixer_elem_info *cval;
73 struct snd_kcontrol *kctl;
74
75 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
76 if (!cval)
77 return -ENOMEM;
78
79 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
80 cval->val_type = val_type;
81 cval->channels = 1;
82 cval->control = control;
83 cval->cmask = cmask;
84 cval->idx_off = idx_off;
85
86 /* get_min_max() is called only for integer volumes later,
87 * so provide a short-cut for booleans */
88 cval->min = 0;
89 cval->max = 1;
90 cval->res = 0;
91 cval->dBmin = 0;
92 cval->dBmax = 0;
93
94 /* Create control */
95 kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
96 if (!kctl) {
97 kfree(cval);
98 return -ENOMEM;
99 }
100
101 /* Set name */
102 snprintf(kctl->id.name, sizeof(kctl->id.name), name);
103 kctl->private_free = snd_usb_mixer_elem_free;
104
105 /* set TLV */
106 if (tlv_callback) {
107 kctl->tlv.c = tlv_callback;
108 kctl->vd[0].access |=
109 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
110 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
111 }
112 /* Add control to mixer */
113 return snd_usb_mixer_add_control(&cval->head, kctl);
114 }
115
116 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
117 unsigned int unitid,
118 unsigned int control,
119 unsigned int cmask,
120 int val_type,
121 const char *name,
122 snd_kcontrol_tlv_rw_t *tlv_callback)
123 {
124 return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
125 val_type, 0 /* Offset */, name, tlv_callback);
126 }
127
128 /*
129 * Create a set of standard UAC controls from a table
130 */
131 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
132 struct std_mono_table *t)
133 {
134 int err;
135
136 while (t->name != NULL) {
137 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
138 t->cmask, t->val_type, t->name, t->tlv_callback);
139 if (err < 0)
140 return err;
141 t++;
142 }
143
144 return 0;
145 }
146
147 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
148 int id,
149 usb_mixer_elem_resume_func_t resume,
150 const struct snd_kcontrol_new *knew,
151 struct usb_mixer_elem_list **listp)
152 {
153 struct usb_mixer_elem_list *list;
154 struct snd_kcontrol *kctl;
155
156 list = kzalloc(sizeof(*list), GFP_KERNEL);
157 if (!list)
158 return -ENOMEM;
159 if (listp)
160 *listp = list;
161 list->mixer = mixer;
162 list->id = id;
163 list->resume = resume;
164 kctl = snd_ctl_new1(knew, list);
165 if (!kctl) {
166 kfree(list);
167 return -ENOMEM;
168 }
169 kctl->private_free = snd_usb_mixer_elem_free;
170 return snd_usb_mixer_add_control(list, kctl);
171 }
172
173 /*
174 * Sound Blaster remote control configuration
175 *
176 * format of remote control data:
177 * Extigy: xx 00
178 * Audigy 2 NX: 06 80 xx 00 00 00
179 * Live! 24-bit: 06 80 xx yy 22 83
180 */
181 static const struct rc_config {
182 u32 usb_id;
183 u8 offset;
184 u8 length;
185 u8 packet_length;
186 u8 min_packet_length; /* minimum accepted length of the URB result */
187 u8 mute_mixer_id;
188 u32 mute_code;
189 } rc_configs[] = {
190 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
191 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
192 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
193 { USB_ID(0x041e, 0x3042), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 */
194 { USB_ID(0x041e, 0x30df), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 Pro */
195 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
196 };
197
198 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
199 {
200 struct usb_mixer_interface *mixer = urb->context;
201 const struct rc_config *rc = mixer->rc_cfg;
202 u32 code;
203
204 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
205 return;
206
207 code = mixer->rc_buffer[rc->offset];
208 if (rc->length == 2)
209 code |= mixer->rc_buffer[rc->offset + 1] << 8;
210
211 /* the Mute button actually changes the mixer control */
212 if (code == rc->mute_code)
213 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
214 mixer->rc_code = code;
215 wmb();
216 wake_up(&mixer->rc_waitq);
217 }
218
219 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
220 long count, loff_t *offset)
221 {
222 struct usb_mixer_interface *mixer = hw->private_data;
223 int err;
224 u32 rc_code;
225
226 if (count != 1 && count != 4)
227 return -EINVAL;
228 err = wait_event_interruptible(mixer->rc_waitq,
229 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
230 if (err == 0) {
231 if (count == 1)
232 err = put_user(rc_code, buf);
233 else
234 err = put_user(rc_code, (u32 __user *)buf);
235 }
236 return err < 0 ? err : count;
237 }
238
239 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
240 poll_table *wait)
241 {
242 struct usb_mixer_interface *mixer = hw->private_data;
243
244 poll_wait(file, &mixer->rc_waitq, wait);
245 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
246 }
247
248 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
249 {
250 struct snd_hwdep *hwdep;
251 int err, len, i;
252
253 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
254 if (rc_configs[i].usb_id == mixer->chip->usb_id)
255 break;
256 if (i >= ARRAY_SIZE(rc_configs))
257 return 0;
258 mixer->rc_cfg = &rc_configs[i];
259
260 len = mixer->rc_cfg->packet_length;
261
262 init_waitqueue_head(&mixer->rc_waitq);
263 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
264 if (err < 0)
265 return err;
266 snprintf(hwdep->name, sizeof(hwdep->name),
267 "%s remote control", mixer->chip->card->shortname);
268 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
269 hwdep->private_data = mixer;
270 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
271 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
272 hwdep->exclusive = 1;
273
274 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
275 if (!mixer->rc_urb)
276 return -ENOMEM;
277 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
278 if (!mixer->rc_setup_packet) {
279 usb_free_urb(mixer->rc_urb);
280 mixer->rc_urb = NULL;
281 return -ENOMEM;
282 }
283 mixer->rc_setup_packet->bRequestType =
284 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
285 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
286 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
287 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
288 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
289 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
290 usb_rcvctrlpipe(mixer->chip->dev, 0),
291 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
292 snd_usb_soundblaster_remote_complete, mixer);
293 return 0;
294 }
295
296 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
297
298 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
299 {
300 ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
301 return 0;
302 }
303
304 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
305 int value, int index)
306 {
307 struct snd_usb_audio *chip = mixer->chip;
308 int err;
309
310 down_read(&chip->shutdown_rwsem);
311 if (chip->shutdown) {
312 err = -ENODEV;
313 goto out;
314 }
315 if (chip->usb_id == USB_ID(0x041e, 0x3042))
316 err = snd_usb_ctl_msg(chip->dev,
317 usb_sndctrlpipe(chip->dev, 0), 0x24,
318 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
319 !value, 0, NULL, 0);
320 /* USB X-Fi S51 Pro */
321 if (chip->usb_id == USB_ID(0x041e, 0x30df))
322 err = snd_usb_ctl_msg(chip->dev,
323 usb_sndctrlpipe(chip->dev, 0), 0x24,
324 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
325 !value, 0, NULL, 0);
326 else
327 err = snd_usb_ctl_msg(chip->dev,
328 usb_sndctrlpipe(chip->dev, 0), 0x24,
329 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
330 value, index + 2, NULL, 0);
331 out:
332 up_read(&chip->shutdown_rwsem);
333 return err;
334 }
335
336 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
337 struct snd_ctl_elem_value *ucontrol)
338 {
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 int value = ucontrol->value.integer.value[0];
343 int old_value = kcontrol->private_value >> 8;
344 int err;
345
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;
353 }
354
355 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
356 {
357 int priv_value = list->kctl->private_value;
358
359 return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
360 priv_value & 0xff);
361 }
362
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,
369 };
370
371 static const char * const snd_audigy2nx_led_names[] = {
372 "CMSS LED Switch",
373 "Power LED Switch",
374 "Dolby Digital LED Switch",
375 };
376
377 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
378 {
379 int i, err;
380
381 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
382 struct snd_kcontrol_new knew;
383
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;
396
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;
405 }
406 return 0;
407 }
408
409 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
410 struct snd_info_buffer *buffer)
411 {
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}
426 };
427 const struct sb_jack *jacks;
428 struct usb_mixer_interface *mixer = entry->private_data;
429 int i, err;
430 u8 buf[3];
431
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;
440
441 for (i = 0; jacks[i].name; ++i) {
442 snd_iprintf(buffer, "%s: ", jacks[i].name);
443 down_read(&mixer->chip->shutdown_rwsem);
444 if (mixer->chip->shutdown)
445 err = 0;
446 else
447 err = snd_usb_ctl_msg(mixer->chip->dev,
448 usb_rcvctrlpipe(mixer->chip->dev, 0),
449 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
450 USB_RECIP_INTERFACE, 0,
451 jacks[i].unitid << 8, buf, 3);
452 up_read(&mixer->chip->shutdown_rwsem);
453 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
454 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
455 else
456 snd_iprintf(buffer, "?\n");
457 }
458 }
459
460 /* EMU0204 */
461 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
462 struct snd_ctl_elem_info *uinfo)
463 {
464 static const char * const texts[2] = {"1/2", "3/4"};
465
466 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
467 }
468
469 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
470 struct snd_ctl_elem_value *ucontrol)
471 {
472 ucontrol->value.enumerated.item[0] = kcontrol->private_value;
473 return 0;
474 }
475
476 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
477 int value)
478 {
479 struct snd_usb_audio *chip = mixer->chip;
480 int err;
481 unsigned char buf[2];
482
483 down_read(&chip->shutdown_rwsem);
484 if (mixer->chip->shutdown) {
485 err = -ENODEV;
486 goto out;
487 }
488
489 buf[0] = 0x01;
490 buf[1] = value ? 0x02 : 0x01;
491 err = snd_usb_ctl_msg(chip->dev,
492 usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
493 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
494 0x0400, 0x0e00, buf, 2);
495 out:
496 up_read(&chip->shutdown_rwsem);
497 return err;
498 }
499
500 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
501 struct snd_ctl_elem_value *ucontrol)
502 {
503 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
504 struct usb_mixer_interface *mixer = list->mixer;
505 unsigned int value = ucontrol->value.enumerated.item[0];
506 int err;
507
508 if (value > 1)
509 return -EINVAL;
510
511 if (value == kcontrol->private_value)
512 return 0;
513
514 kcontrol->private_value = value;
515 err = snd_emu0204_ch_switch_update(mixer, value);
516 return err < 0 ? err : 1;
517 }
518
519 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
520 {
521 return snd_emu0204_ch_switch_update(list->mixer,
522 list->kctl->private_value);
523 }
524
525 static struct snd_kcontrol_new snd_emu0204_control = {
526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
527 .name = "Front Jack Channels",
528 .info = snd_emu0204_ch_switch_info,
529 .get = snd_emu0204_ch_switch_get,
530 .put = snd_emu0204_ch_switch_put,
531 .private_value = 0,
532 };
533
534 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
535 {
536 return add_single_ctl_with_resume(mixer, 0,
537 snd_emu0204_ch_switch_resume,
538 &snd_emu0204_control, NULL);
539 }
540
541 /* ASUS Xonar U1 / U3 controls */
542
543 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
544 struct snd_ctl_elem_value *ucontrol)
545 {
546 ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
547 return 0;
548 }
549
550 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
551 unsigned char status)
552 {
553 struct snd_usb_audio *chip = mixer->chip;
554 int err;
555
556 down_read(&chip->shutdown_rwsem);
557 if (chip->shutdown)
558 err = -ENODEV;
559 else
560 err = snd_usb_ctl_msg(chip->dev,
561 usb_sndctrlpipe(chip->dev, 0), 0x08,
562 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
563 50, 0, &status, 1);
564 up_read(&chip->shutdown_rwsem);
565 return err;
566 }
567
568 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
569 struct snd_ctl_elem_value *ucontrol)
570 {
571 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
572 u8 old_status, new_status;
573 int err;
574
575 old_status = kcontrol->private_value;
576 if (ucontrol->value.integer.value[0])
577 new_status = old_status | 0x02;
578 else
579 new_status = old_status & ~0x02;
580 if (new_status == old_status)
581 return 0;
582
583 kcontrol->private_value = new_status;
584 err = snd_xonar_u1_switch_update(list->mixer, new_status);
585 return err < 0 ? err : 1;
586 }
587
588 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
589 {
590 return snd_xonar_u1_switch_update(list->mixer,
591 list->kctl->private_value);
592 }
593
594 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
595 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
596 .name = "Digital Playback Switch",
597 .info = snd_ctl_boolean_mono_info,
598 .get = snd_xonar_u1_switch_get,
599 .put = snd_xonar_u1_switch_put,
600 .private_value = 0x05,
601 };
602
603 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
604 {
605 return add_single_ctl_with_resume(mixer, 0,
606 snd_xonar_u1_switch_resume,
607 &snd_xonar_u1_output_switch, NULL);
608 }
609
610 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
611
612 static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
613 struct snd_ctl_elem_value *ucontrol)
614 {
615 ucontrol->value.enumerated.item[0] = kctl->private_value;
616 return 0;
617 }
618
619 static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
620 {
621 struct snd_usb_audio *chip = mixer->chip;
622 int err;
623 unsigned char buff[3];
624
625 down_read(&chip->shutdown_rwsem);
626 if (chip->shutdown) {
627 err = -ENODEV;
628 goto err;
629 }
630
631 /* Prepare for magic command to toggle clock source */
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_INTERFACE, 0x00, 0x500, buff, 1);
637 if (err < 0)
638 goto err;
639 err = snd_usb_ctl_msg(chip->dev,
640 usb_rcvctrlpipe(chip->dev, 0), 0x81,
641 USB_DIR_IN |
642 USB_TYPE_CLASS |
643 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
644 if (err < 0)
645 goto err;
646
647 /* 2 possibilities: Internal -> send sample rate
648 * S/PDIF sync -> send zeroes
649 * NB: Sample rate locked to 48kHz on purpose to
650 * prevent user from resetting the sample rate
651 * while S/PDIF sync is enabled and confusing
652 * this configuration.
653 */
654 if (val == 0) {
655 buff[0] = 0x80;
656 buff[1] = 0xbb;
657 buff[2] = 0x00;
658 } else {
659 buff[0] = buff[1] = buff[2] = 0x00;
660 }
661
662 /* Send the magic command to toggle the clock source */
663 err = snd_usb_ctl_msg(chip->dev,
664 usb_sndctrlpipe(chip->dev, 0), 0x1,
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, 0x81, buff, 3);
674 if (err < 0)
675 goto err;
676 err = snd_usb_ctl_msg(chip->dev,
677 usb_rcvctrlpipe(chip->dev, 0), 0x81,
678 USB_DIR_IN |
679 USB_TYPE_CLASS |
680 USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
681 if (err < 0)
682 goto err;
683
684 err:
685 up_read(&chip->shutdown_rwsem);
686 return err;
687 }
688
689 static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
690 struct snd_ctl_elem_value *ucontrol)
691 {
692 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
693 struct usb_mixer_interface *mixer = list->mixer;
694 int err;
695 bool cur_val, new_val;
696
697 cur_val = kctl->private_value;
698 new_val = ucontrol->value.enumerated.item[0];
699 if (cur_val == new_val)
700 return 0;
701
702 kctl->private_value = new_val;
703 err = snd_mbox1_switch_update(mixer, new_val);
704 return err < 0 ? err : 1;
705 }
706
707 static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
708 struct snd_ctl_elem_info *uinfo)
709 {
710 static const char *const texts[2] = {
711 "Internal",
712 "S/PDIF"
713 };
714
715 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
716 }
717
718 static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
719 {
720 return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
721 }
722
723 static struct snd_kcontrol_new snd_mbox1_switch = {
724 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
725 .name = "Clock Source",
726 .index = 0,
727 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
728 .info = snd_mbox1_switch_info,
729 .get = snd_mbox1_switch_get,
730 .put = snd_mbox1_switch_put,
731 .private_value = 0
732 };
733
734 static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
735 {
736 return add_single_ctl_with_resume(mixer, 0,
737 snd_mbox1_switch_resume,
738 &snd_mbox1_switch, NULL);
739 }
740
741 /* Native Instruments device quirks */
742
743 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
744
745 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
746 struct snd_ctl_elem_value *ucontrol)
747 {
748 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
749 struct usb_device *dev = mixer->chip->dev;
750 u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
751 u16 wIndex = kcontrol->private_value & 0xffff;
752 u8 tmp;
753 int ret;
754
755 down_read(&mixer->chip->shutdown_rwsem);
756 if (mixer->chip->shutdown)
757 ret = -ENODEV;
758 else
759 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
760 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
761 0, wIndex,
762 &tmp, sizeof(tmp));
763 up_read(&mixer->chip->shutdown_rwsem);
764
765 if (ret < 0) {
766 dev_err(&dev->dev,
767 "unable to issue vendor read request (ret = %d)", ret);
768 return ret;
769 }
770
771 ucontrol->value.integer.value[0] = tmp;
772
773 return 0;
774 }
775
776 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
777 struct snd_ctl_elem_value *ucontrol)
778 {
779 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
780 struct usb_device *dev = mixer->chip->dev;
781 u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
782 u16 wIndex = kcontrol->private_value & 0xffff;
783 u16 wValue = ucontrol->value.integer.value[0];
784 int ret;
785
786 down_read(&mixer->chip->shutdown_rwsem);
787 if (mixer->chip->shutdown)
788 ret = -ENODEV;
789 else
790 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
791 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
792 wValue, wIndex,
793 NULL, 0, 1000);
794 up_read(&mixer->chip->shutdown_rwsem);
795
796 if (ret < 0) {
797 dev_err(&dev->dev,
798 "unable to issue vendor write request (ret = %d)", ret);
799 return ret;
800 }
801
802 return 0;
803 }
804
805 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
806 {
807 .name = "Direct Thru Channel A",
808 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
809 },
810 {
811 .name = "Direct Thru Channel B",
812 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
813 },
814 {
815 .name = "Phono Input Channel A",
816 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
817 },
818 {
819 .name = "Phono Input Channel B",
820 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
821 },
822 };
823
824 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
825 {
826 .name = "Direct Thru Channel A",
827 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
828 },
829 {
830 .name = "Direct Thru Channel B",
831 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
832 },
833 {
834 .name = "Direct Thru Channel C",
835 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
836 },
837 {
838 .name = "Direct Thru Channel D",
839 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
840 },
841 {
842 .name = "Phono Input Channel A",
843 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
844 },
845 {
846 .name = "Phono Input Channel B",
847 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
848 },
849 {
850 .name = "Phono Input Channel C",
851 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
852 },
853 {
854 .name = "Phono Input Channel D",
855 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
856 },
857 };
858
859 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
860 const struct snd_kcontrol_new *kc,
861 unsigned int count)
862 {
863 int i, err = 0;
864 struct snd_kcontrol_new template = {
865 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
866 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
867 .get = snd_nativeinstruments_control_get,
868 .put = snd_nativeinstruments_control_put,
869 .info = snd_ctl_boolean_mono_info,
870 };
871
872 for (i = 0; i < count; i++) {
873 struct snd_kcontrol *c;
874
875 template.name = kc[i].name;
876 template.private_value = kc[i].private_value;
877
878 c = snd_ctl_new1(&template, mixer);
879 err = snd_ctl_add(mixer->chip->card, c);
880
881 if (err < 0)
882 break;
883 }
884
885 return err;
886 }
887
888 /* M-Audio FastTrack Ultra quirks */
889 /* FTU Effect switch (also used by C400/C600) */
890 struct snd_ftu_eff_switch_priv_val {
891 struct usb_mixer_interface *mixer;
892 int cached_value;
893 int is_cached;
894 int bUnitID;
895 int validx;
896 };
897
898 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_info *uinfo)
900 {
901 static const char *const texts[8] = {
902 "Room 1", "Room 2", "Room 3", "Hall 1",
903 "Hall 2", "Plate", "Delay", "Echo"
904 };
905
906 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
907 }
908
909 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
910 struct snd_ctl_elem_value *ucontrol)
911 {
912 struct snd_usb_audio *chip;
913 struct usb_mixer_interface *mixer;
914 struct snd_ftu_eff_switch_priv_val *pval;
915 int err;
916 unsigned char value[2];
917 int id, validx;
918
919 const int val_len = 2;
920
921 value[0] = 0x00;
922 value[1] = 0x00;
923
924 pval = (struct snd_ftu_eff_switch_priv_val *)
925 kctl->private_value;
926
927 if (pval->is_cached) {
928 ucontrol->value.enumerated.item[0] = pval->cached_value;
929 return 0;
930 }
931
932 mixer = (struct usb_mixer_interface *) pval->mixer;
933 if (snd_BUG_ON(!mixer))
934 return -EINVAL;
935
936 chip = (struct snd_usb_audio *) mixer->chip;
937 if (snd_BUG_ON(!chip))
938 return -EINVAL;
939
940 id = pval->bUnitID;
941 validx = pval->validx;
942
943 down_read(&mixer->chip->shutdown_rwsem);
944 if (mixer->chip->shutdown)
945 err = -ENODEV;
946 else
947 err = snd_usb_ctl_msg(chip->dev,
948 usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
949 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
950 validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
951 value, val_len);
952 up_read(&mixer->chip->shutdown_rwsem);
953 if (err < 0)
954 return err;
955
956 ucontrol->value.enumerated.item[0] = value[0];
957 pval->cached_value = value[0];
958 pval->is_cached = 1;
959
960 return 0;
961 }
962
963 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
964 struct snd_ctl_elem_value *ucontrol)
965 {
966 struct snd_usb_audio *chip;
967 struct snd_ftu_eff_switch_priv_val *pval;
968
969 struct usb_mixer_interface *mixer;
970 int changed, cur_val, err, new_val;
971 unsigned char value[2];
972 int id, validx;
973
974 const int val_len = 2;
975
976 changed = 0;
977
978 pval = (struct snd_ftu_eff_switch_priv_val *)
979 kctl->private_value;
980 cur_val = pval->cached_value;
981 new_val = ucontrol->value.enumerated.item[0];
982
983 mixer = (struct usb_mixer_interface *) pval->mixer;
984 if (snd_BUG_ON(!mixer))
985 return -EINVAL;
986
987 chip = (struct snd_usb_audio *) mixer->chip;
988 if (snd_BUG_ON(!chip))
989 return -EINVAL;
990
991 id = pval->bUnitID;
992 validx = pval->validx;
993
994 if (!pval->is_cached) {
995 /* Read current value */
996 down_read(&mixer->chip->shutdown_rwsem);
997 if (mixer->chip->shutdown)
998 err = -ENODEV;
999 else
1000 err = snd_usb_ctl_msg(chip->dev,
1001 usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
1002 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1003 validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
1004 value, val_len);
1005 up_read(&mixer->chip->shutdown_rwsem);
1006 if (err < 0)
1007 return err;
1008
1009 cur_val = value[0];
1010 pval->cached_value = cur_val;
1011 pval->is_cached = 1;
1012 }
1013 /* update value if needed */
1014 if (cur_val != new_val) {
1015 value[0] = new_val;
1016 value[1] = 0;
1017 down_read(&mixer->chip->shutdown_rwsem);
1018 if (mixer->chip->shutdown)
1019 err = -ENODEV;
1020 else
1021 err = snd_usb_ctl_msg(chip->dev,
1022 usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
1023 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1024 validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
1025 value, val_len);
1026 up_read(&mixer->chip->shutdown_rwsem);
1027 if (err < 0)
1028 return err;
1029
1030 pval->cached_value = new_val;
1031 pval->is_cached = 1;
1032 changed = 1;
1033 }
1034
1035 return changed;
1036 }
1037
1038 static void kctl_private_value_free(struct snd_kcontrol *kctl)
1039 {
1040 kfree((void *)kctl->private_value);
1041 }
1042
1043 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
1044 int validx, int bUnitID)
1045 {
1046 static struct snd_kcontrol_new template = {
1047 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1048 .name = "Effect Program Switch",
1049 .index = 0,
1050 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1051 .info = snd_ftu_eff_switch_info,
1052 .get = snd_ftu_eff_switch_get,
1053 .put = snd_ftu_eff_switch_put
1054 };
1055
1056 int err;
1057 struct snd_kcontrol *kctl;
1058 struct snd_ftu_eff_switch_priv_val *pval;
1059
1060 pval = kzalloc(sizeof(*pval), GFP_KERNEL);
1061 if (!pval)
1062 return -ENOMEM;
1063
1064 pval->cached_value = 0;
1065 pval->is_cached = 0;
1066 pval->mixer = mixer;
1067 pval->bUnitID = bUnitID;
1068 pval->validx = validx;
1069
1070 template.private_value = (unsigned long) pval;
1071 kctl = snd_ctl_new1(&template, mixer->chip);
1072 if (!kctl) {
1073 kfree(pval);
1074 return -ENOMEM;
1075 }
1076
1077 kctl->private_free = kctl_private_value_free;
1078 err = snd_ctl_add(mixer->chip->card, kctl);
1079 if (err < 0)
1080 return err;
1081
1082 return 0;
1083 }
1084
1085 /* Create volume controls for FTU devices*/
1086 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
1087 {
1088 char name[64];
1089 unsigned int control, cmask;
1090 int in, out, err;
1091
1092 const unsigned int id = 5;
1093 const int val_type = USB_MIXER_S16;
1094
1095 for (out = 0; out < 8; out++) {
1096 control = out + 1;
1097 for (in = 0; in < 8; in++) {
1098 cmask = 1 << in;
1099 snprintf(name, sizeof(name),
1100 "AIn%d - Out%d Capture Volume",
1101 in + 1, out + 1);
1102 err = snd_create_std_mono_ctl(mixer, id, control,
1103 cmask, val_type, name,
1104 &snd_usb_mixer_vol_tlv);
1105 if (err < 0)
1106 return err;
1107 }
1108 for (in = 8; in < 16; in++) {
1109 cmask = 1 << in;
1110 snprintf(name, sizeof(name),
1111 "DIn%d - Out%d Playback Volume",
1112 in - 7, out + 1);
1113 err = snd_create_std_mono_ctl(mixer, id, control,
1114 cmask, val_type, name,
1115 &snd_usb_mixer_vol_tlv);
1116 if (err < 0)
1117 return err;
1118 }
1119 }
1120
1121 return 0;
1122 }
1123
1124 /* This control needs a volume quirk, see mixer.c */
1125 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1126 {
1127 static const char name[] = "Effect Volume";
1128 const unsigned int id = 6;
1129 const int val_type = USB_MIXER_U8;
1130 const unsigned int control = 2;
1131 const unsigned int cmask = 0;
1132
1133 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1134 name, snd_usb_mixer_vol_tlv);
1135 }
1136
1137 /* This control needs a volume quirk, see mixer.c */
1138 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1139 {
1140 static const char name[] = "Effect Duration";
1141 const unsigned int id = 6;
1142 const int val_type = USB_MIXER_S16;
1143 const unsigned int control = 3;
1144 const unsigned int cmask = 0;
1145
1146 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1147 name, snd_usb_mixer_vol_tlv);
1148 }
1149
1150 /* This control needs a volume quirk, see mixer.c */
1151 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1152 {
1153 static const char name[] = "Effect Feedback Volume";
1154 const unsigned int id = 6;
1155 const int val_type = USB_MIXER_U8;
1156 const unsigned int control = 4;
1157 const unsigned int cmask = 0;
1158
1159 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1160 name, NULL);
1161 }
1162
1163 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1164 {
1165 unsigned int cmask;
1166 int err, ch;
1167 char name[48];
1168
1169 const unsigned int id = 7;
1170 const int val_type = USB_MIXER_S16;
1171 const unsigned int control = 7;
1172
1173 for (ch = 0; ch < 4; ++ch) {
1174 cmask = 1 << ch;
1175 snprintf(name, sizeof(name),
1176 "Effect Return %d Volume", ch + 1);
1177 err = snd_create_std_mono_ctl(mixer, id, control,
1178 cmask, val_type, name,
1179 snd_usb_mixer_vol_tlv);
1180 if (err < 0)
1181 return err;
1182 }
1183
1184 return 0;
1185 }
1186
1187 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1188 {
1189 unsigned int cmask;
1190 int err, ch;
1191 char name[48];
1192
1193 const unsigned int id = 5;
1194 const int val_type = USB_MIXER_S16;
1195 const unsigned int control = 9;
1196
1197 for (ch = 0; ch < 8; ++ch) {
1198 cmask = 1 << ch;
1199 snprintf(name, sizeof(name),
1200 "Effect Send AIn%d Volume", ch + 1);
1201 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1202 val_type, name,
1203 snd_usb_mixer_vol_tlv);
1204 if (err < 0)
1205 return err;
1206 }
1207 for (ch = 8; ch < 16; ++ch) {
1208 cmask = 1 << ch;
1209 snprintf(name, sizeof(name),
1210 "Effect Send DIn%d Volume", ch - 7);
1211 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1212 val_type, name,
1213 snd_usb_mixer_vol_tlv);
1214 if (err < 0)
1215 return err;
1216 }
1217 return 0;
1218 }
1219
1220 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1221 {
1222 int err;
1223
1224 err = snd_ftu_create_volume_ctls(mixer);
1225 if (err < 0)
1226 return err;
1227
1228 err = snd_ftu_create_effect_switch(mixer, 1, 6);
1229 if (err < 0)
1230 return err;
1231
1232 err = snd_ftu_create_effect_volume_ctl(mixer);
1233 if (err < 0)
1234 return err;
1235
1236 err = snd_ftu_create_effect_duration_ctl(mixer);
1237 if (err < 0)
1238 return err;
1239
1240 err = snd_ftu_create_effect_feedback_ctl(mixer);
1241 if (err < 0)
1242 return err;
1243
1244 err = snd_ftu_create_effect_return_ctls(mixer);
1245 if (err < 0)
1246 return err;
1247
1248 err = snd_ftu_create_effect_send_ctls(mixer);
1249 if (err < 0)
1250 return err;
1251
1252 return 0;
1253 }
1254
1255 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1256 unsigned char samplerate_id)
1257 {
1258 struct usb_mixer_interface *mixer;
1259 struct usb_mixer_elem_info *cval;
1260 int unitid = 12; /* SamleRate ExtensionUnit ID */
1261
1262 list_for_each_entry(mixer, &chip->mixer_list, list) {
1263 cval = (struct usb_mixer_elem_info *)mixer->id_elems[unitid];
1264 if (cval) {
1265 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1266 cval->control << 8,
1267 samplerate_id);
1268 snd_usb_mixer_notify_id(mixer, unitid);
1269 }
1270 break;
1271 }
1272 }
1273
1274 /* M-Audio Fast Track C400/C600 */
1275 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1276 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1277 {
1278 char name[64];
1279 unsigned int cmask, offset;
1280 int out, chan, err;
1281 int num_outs = 0;
1282 int num_ins = 0;
1283
1284 const unsigned int id = 0x40;
1285 const int val_type = USB_MIXER_S16;
1286 const int control = 1;
1287
1288 switch (mixer->chip->usb_id) {
1289 case USB_ID(0x0763, 0x2030):
1290 num_outs = 6;
1291 num_ins = 4;
1292 break;
1293 case USB_ID(0x0763, 0x2031):
1294 num_outs = 8;
1295 num_ins = 6;
1296 break;
1297 }
1298
1299 for (chan = 0; chan < num_outs + num_ins; chan++) {
1300 for (out = 0; out < num_outs; out++) {
1301 if (chan < num_outs) {
1302 snprintf(name, sizeof(name),
1303 "PCM%d-Out%d Playback Volume",
1304 chan + 1, out + 1);
1305 } else {
1306 snprintf(name, sizeof(name),
1307 "In%d-Out%d Playback Volume",
1308 chan - num_outs + 1, out + 1);
1309 }
1310
1311 cmask = (out == 0) ? 0 : 1 << (out - 1);
1312 offset = chan * num_outs;
1313 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1314 cmask, val_type, offset, name,
1315 &snd_usb_mixer_vol_tlv);
1316 if (err < 0)
1317 return err;
1318 }
1319 }
1320
1321 return 0;
1322 }
1323
1324 /* This control needs a volume quirk, see mixer.c */
1325 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1326 {
1327 static const char name[] = "Effect Volume";
1328 const unsigned int id = 0x43;
1329 const int val_type = USB_MIXER_U8;
1330 const unsigned int control = 3;
1331 const unsigned int cmask = 0;
1332
1333 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1334 name, snd_usb_mixer_vol_tlv);
1335 }
1336
1337 /* This control needs a volume quirk, see mixer.c */
1338 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1339 {
1340 static const char name[] = "Effect Duration";
1341 const unsigned int id = 0x43;
1342 const int val_type = USB_MIXER_S16;
1343 const unsigned int control = 4;
1344 const unsigned int cmask = 0;
1345
1346 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1347 name, snd_usb_mixer_vol_tlv);
1348 }
1349
1350 /* This control needs a volume quirk, see mixer.c */
1351 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1352 {
1353 static const char name[] = "Effect Feedback Volume";
1354 const unsigned int id = 0x43;
1355 const int val_type = USB_MIXER_U8;
1356 const unsigned int control = 5;
1357 const unsigned int cmask = 0;
1358
1359 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1360 name, NULL);
1361 }
1362
1363 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1364 {
1365 char name[64];
1366 unsigned int cmask;
1367 int chan, err;
1368 int num_outs = 0;
1369 int num_ins = 0;
1370
1371 const unsigned int id = 0x42;
1372 const int val_type = USB_MIXER_S16;
1373 const int control = 1;
1374
1375 switch (mixer->chip->usb_id) {
1376 case USB_ID(0x0763, 0x2030):
1377 num_outs = 6;
1378 num_ins = 4;
1379 break;
1380 case USB_ID(0x0763, 0x2031):
1381 num_outs = 8;
1382 num_ins = 6;
1383 break;
1384 }
1385
1386 for (chan = 0; chan < num_outs + num_ins; chan++) {
1387 if (chan < num_outs) {
1388 snprintf(name, sizeof(name),
1389 "Effect Send DOut%d",
1390 chan + 1);
1391 } else {
1392 snprintf(name, sizeof(name),
1393 "Effect Send AIn%d",
1394 chan - num_outs + 1);
1395 }
1396
1397 cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1398 err = snd_create_std_mono_ctl(mixer, id, control,
1399 cmask, val_type, name,
1400 &snd_usb_mixer_vol_tlv);
1401 if (err < 0)
1402 return err;
1403 }
1404
1405 return 0;
1406 }
1407
1408 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1409 {
1410 char name[64];
1411 unsigned int cmask;
1412 int chan, err;
1413 int num_outs = 0;
1414 int offset = 0;
1415
1416 const unsigned int id = 0x40;
1417 const int val_type = USB_MIXER_S16;
1418 const int control = 1;
1419
1420 switch (mixer->chip->usb_id) {
1421 case USB_ID(0x0763, 0x2030):
1422 num_outs = 6;
1423 offset = 0x3c;
1424 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1425 break;
1426 case USB_ID(0x0763, 0x2031):
1427 num_outs = 8;
1428 offset = 0x70;
1429 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1430 break;
1431 }
1432
1433 for (chan = 0; chan < num_outs; chan++) {
1434 snprintf(name, sizeof(name),
1435 "Effect Return %d",
1436 chan + 1);
1437
1438 cmask = (chan == 0) ? 0 :
1439 1 << (chan + (chan % 2) * num_outs - 1);
1440 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1441 cmask, val_type, offset, name,
1442 &snd_usb_mixer_vol_tlv);
1443 if (err < 0)
1444 return err;
1445 }
1446
1447 return 0;
1448 }
1449
1450 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1451 {
1452 int err;
1453
1454 err = snd_c400_create_vol_ctls(mixer);
1455 if (err < 0)
1456 return err;
1457
1458 err = snd_c400_create_effect_vol_ctls(mixer);
1459 if (err < 0)
1460 return err;
1461
1462 err = snd_c400_create_effect_ret_vol_ctls(mixer);
1463 if (err < 0)
1464 return err;
1465
1466 err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1467 if (err < 0)
1468 return err;
1469
1470 err = snd_c400_create_effect_volume_ctl(mixer);
1471 if (err < 0)
1472 return err;
1473
1474 err = snd_c400_create_effect_duration_ctl(mixer);
1475 if (err < 0)
1476 return err;
1477
1478 err = snd_c400_create_effect_feedback_ctl(mixer);
1479 if (err < 0)
1480 return err;
1481
1482 return 0;
1483 }
1484
1485 /*
1486 * The mixer units for Ebox-44 are corrupt, and even where they
1487 * are valid they presents mono controls as L and R channels of
1488 * stereo. So we provide a good mixer here.
1489 */
1490 static struct std_mono_table ebox44_table[] = {
1491 {
1492 .unitid = 4,
1493 .control = 1,
1494 .cmask = 0x0,
1495 .val_type = USB_MIXER_INV_BOOLEAN,
1496 .name = "Headphone Playback Switch"
1497 },
1498 {
1499 .unitid = 4,
1500 .control = 2,
1501 .cmask = 0x1,
1502 .val_type = USB_MIXER_S16,
1503 .name = "Headphone A Mix Playback Volume"
1504 },
1505 {
1506 .unitid = 4,
1507 .control = 2,
1508 .cmask = 0x2,
1509 .val_type = USB_MIXER_S16,
1510 .name = "Headphone B Mix Playback Volume"
1511 },
1512
1513 {
1514 .unitid = 7,
1515 .control = 1,
1516 .cmask = 0x0,
1517 .val_type = USB_MIXER_INV_BOOLEAN,
1518 .name = "Output Playback Switch"
1519 },
1520 {
1521 .unitid = 7,
1522 .control = 2,
1523 .cmask = 0x1,
1524 .val_type = USB_MIXER_S16,
1525 .name = "Output A Playback Volume"
1526 },
1527 {
1528 .unitid = 7,
1529 .control = 2,
1530 .cmask = 0x2,
1531 .val_type = USB_MIXER_S16,
1532 .name = "Output B Playback Volume"
1533 },
1534
1535 {
1536 .unitid = 10,
1537 .control = 1,
1538 .cmask = 0x0,
1539 .val_type = USB_MIXER_INV_BOOLEAN,
1540 .name = "Input Capture Switch"
1541 },
1542 {
1543 .unitid = 10,
1544 .control = 2,
1545 .cmask = 0x1,
1546 .val_type = USB_MIXER_S16,
1547 .name = "Input A Capture Volume"
1548 },
1549 {
1550 .unitid = 10,
1551 .control = 2,
1552 .cmask = 0x2,
1553 .val_type = USB_MIXER_S16,
1554 .name = "Input B Capture Volume"
1555 },
1556
1557 {}
1558 };
1559
1560 /* Audio Advantage Micro II findings:
1561 *
1562 * Mapping spdif AES bits to vendor register.bit:
1563 * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1564 * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1565 * AES2: [0 0 0 0 0 0 0 0]
1566 * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1567 * (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1568 *
1569 * power on values:
1570 * r2: 0x10
1571 * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1572 * just after it to 0xa0, presumably it disables/mutes some analog
1573 * parts when there is no audio.)
1574 * r9: 0x28
1575 *
1576 * Optical transmitter on/off:
1577 * vendor register.bit: 9.1
1578 * 0 - on (0x28 register value)
1579 * 1 - off (0x2a register value)
1580 *
1581 */
1582 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1583 struct snd_ctl_elem_info *uinfo)
1584 {
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1586 uinfo->count = 1;
1587 return 0;
1588 }
1589
1590 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1592 {
1593 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1594 int err;
1595 struct usb_interface *iface;
1596 struct usb_host_interface *alts;
1597 unsigned int ep;
1598 unsigned char data[3];
1599 int rate;
1600
1601 ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1602 ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1603 ucontrol->value.iec958.status[2] = 0x00;
1604
1605 /* use known values for that card: interface#1 altsetting#1 */
1606 iface = usb_ifnum_to_if(mixer->chip->dev, 1);
1607 alts = &iface->altsetting[1];
1608 ep = get_endpoint(alts, 0)->bEndpointAddress;
1609
1610 err = snd_usb_ctl_msg(mixer->chip->dev,
1611 usb_rcvctrlpipe(mixer->chip->dev, 0),
1612 UAC_GET_CUR,
1613 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1614 UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1615 ep,
1616 data,
1617 sizeof(data));
1618 if (err < 0)
1619 goto end;
1620
1621 rate = data[0] | (data[1] << 8) | (data[2] << 16);
1622 ucontrol->value.iec958.status[3] = (rate == 48000) ?
1623 IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1624
1625 err = 0;
1626 end:
1627 return err;
1628 }
1629
1630 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
1632 {
1633 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1634 int err;
1635 u8 reg;
1636 unsigned long priv_backup = kcontrol->private_value;
1637
1638 reg = ((ucontrol->value.iec958.status[1] & 0x0f) << 4) |
1639 (ucontrol->value.iec958.status[0] & 0x0f);
1640 err = snd_usb_ctl_msg(mixer->chip->dev,
1641 usb_sndctrlpipe(mixer->chip->dev, 0),
1642 UAC_SET_CUR,
1643 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1644 reg,
1645 2,
1646 NULL,
1647 0);
1648 if (err < 0)
1649 goto end;
1650
1651 kcontrol->private_value &= 0xfffff0f0;
1652 kcontrol->private_value |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1653 kcontrol->private_value |= (ucontrol->value.iec958.status[0] & 0x0f);
1654
1655 reg = (ucontrol->value.iec958.status[0] & IEC958_AES0_NONAUDIO) ?
1656 0xa0 : 0x20;
1657 reg |= (ucontrol->value.iec958.status[1] >> 4) & 0x0f;
1658 err = snd_usb_ctl_msg(mixer->chip->dev,
1659 usb_sndctrlpipe(mixer->chip->dev, 0),
1660 UAC_SET_CUR,
1661 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1662 reg,
1663 3,
1664 NULL,
1665 0);
1666 if (err < 0)
1667 goto end;
1668
1669 kcontrol->private_value &= 0xffff0fff;
1670 kcontrol->private_value |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1671
1672 /* The frequency bits in AES3 cannot be set via register access. */
1673
1674 /* Silently ignore any bits from the request that cannot be set. */
1675
1676 err = (priv_backup != kcontrol->private_value);
1677 end:
1678 return err;
1679 }
1680
1681 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1682 struct snd_ctl_elem_value *ucontrol)
1683 {
1684 ucontrol->value.iec958.status[0] = 0x0f;
1685 ucontrol->value.iec958.status[1] = 0xff;
1686 ucontrol->value.iec958.status[2] = 0x00;
1687 ucontrol->value.iec958.status[3] = 0x00;
1688
1689 return 0;
1690 }
1691
1692 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
1694 {
1695 ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1696
1697 return 0;
1698 }
1699
1700 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol)
1702 {
1703 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1704 int err;
1705 u8 reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1706
1707 err = snd_usb_ctl_msg(mixer->chip->dev,
1708 usb_sndctrlpipe(mixer->chip->dev, 0),
1709 UAC_SET_CUR,
1710 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1711 reg,
1712 9,
1713 NULL,
1714 0);
1715
1716 if (!err) {
1717 err = (reg != (kcontrol->private_value & 0x0ff));
1718 if (err)
1719 kcontrol->private_value = reg;
1720 }
1721
1722 return err;
1723 }
1724
1725 static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1726 {
1727 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1728 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1729 .info = snd_microii_spdif_info,
1730 .get = snd_microii_spdif_default_get,
1731 .put = snd_microii_spdif_default_put,
1732 .private_value = 0x00000100UL,/* reset value */
1733 },
1734 {
1735 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1736 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1737 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1738 .info = snd_microii_spdif_info,
1739 .get = snd_microii_spdif_mask_get,
1740 },
1741 {
1742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1744 .info = snd_ctl_boolean_mono_info,
1745 .get = snd_microii_spdif_switch_get,
1746 .put = snd_microii_spdif_switch_put,
1747 .private_value = 0x00000028UL,/* reset value */
1748 }
1749 };
1750
1751 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1752 {
1753 int err, i;
1754
1755 for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1756 err = snd_ctl_add(mixer->chip->card,
1757 snd_ctl_new1(&snd_microii_mixer_spdif[i], mixer));
1758 if (err < 0)
1759 return err;
1760 }
1761
1762 return 0;
1763 }
1764
1765 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
1766 {
1767 int err = 0;
1768 struct snd_info_entry *entry;
1769
1770 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1771 return err;
1772
1773 switch (mixer->chip->usb_id) {
1774 case USB_ID(0x041e, 0x3020):
1775 case USB_ID(0x041e, 0x3040):
1776 case USB_ID(0x041e, 0x3042):
1777 case USB_ID(0x041e, 0x30df):
1778 case USB_ID(0x041e, 0x3048):
1779 err = snd_audigy2nx_controls_create(mixer);
1780 if (err < 0)
1781 break;
1782 if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1783 snd_info_set_text_ops(entry, mixer,
1784 snd_audigy2nx_proc_read);
1785 break;
1786
1787 /* EMU0204 */
1788 case USB_ID(0x041e, 0x3f19):
1789 err = snd_emu0204_controls_create(mixer);
1790 if (err < 0)
1791 break;
1792 break;
1793
1794 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1795 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
1796 err = snd_c400_create_mixer(mixer);
1797 break;
1798
1799 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1800 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1801 err = snd_ftu_create_mixer(mixer);
1802 break;
1803
1804 case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
1805 case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
1806 case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
1807 err = snd_xonar_u1_controls_create(mixer);
1808 break;
1809
1810 case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
1811 err = snd_microii_controls_create(mixer);
1812 break;
1813
1814 case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
1815 err = snd_mbox1_create_sync_switch(mixer);
1816 break;
1817
1818 case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1819 err = snd_nativeinstruments_create_mixer(mixer,
1820 snd_nativeinstruments_ta6_mixers,
1821 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1822 break;
1823
1824 case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1825 err = snd_nativeinstruments_create_mixer(mixer,
1826 snd_nativeinstruments_ta10_mixers,
1827 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1828 break;
1829
1830 case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1831 /* detection is disabled in mixer_maps.c */
1832 err = snd_create_std_mono_table(mixer, ebox44_table);
1833 break;
1834
1835 case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
1836 case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
1837 case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
1838 case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
1839 case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
1840 err = snd_scarlett_controls_create(mixer);
1841 break;
1842 }
1843
1844 return err;
1845 }
1846
1847 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1848 int unitid)
1849 {
1850 if (!mixer->rc_cfg)
1851 return;
1852 /* unit ids specific to Extigy/Audigy 2 NX: */
1853 switch (unitid) {
1854 case 0: /* remote control */
1855 mixer->rc_urb->dev = mixer->chip->dev;
1856 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1857 break;
1858 case 4: /* digital in jack */
1859 case 7: /* line in jacks */
1860 case 19: /* speaker out jacks */
1861 case 20: /* headphones out jack */
1862 break;
1863 /* live24ext: 4 = line-in jack */
1864 case 3: /* hp-out jack (may actuate Mute) */
1865 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1866 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1867 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1868 break;
1869 default:
1870 usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
1871 break;
1872 }
1873 }
1874
This page took 0.075549 seconds and 4 git commands to generate.