Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 *   USB Audio Driver for ALSA
  3 *
  4 *   Quirks and vendor-specific extensions for mixer interfaces
  5 *
  6 *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7 *
  8 *   Many codes borrowed from audio.c by
  9 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
 10 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
 11 *
 12 *
 13 *   This program is free software; you can redistribute it and/or modify
 14 *   it under the terms of the GNU General Public License as published by
 15 *   the Free Software Foundation; either version 2 of the License, or
 16 *   (at your option) any later version.
 17 *
 18 *   This program is distributed in the hope that it will be useful,
 19 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 20 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21 *   GNU General Public License for more details.
 22 *
 23 *   You should have received a copy of the GNU General Public License
 24 *   along with this program; if not, write to the Free Software
 25 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 26 */
 27
 28#include <linux/init.h>
 29#include <linux/slab.h>
 30#include <linux/usb.h>
 31#include <linux/usb/audio.h>
 32
 33#include <sound/core.h>
 34#include <sound/control.h>
 35#include <sound/hwdep.h>
 36#include <sound/info.h>
 37
 38#include "usbaudio.h"
 39#include "mixer.h"
 40#include "mixer_quirks.h"
 41#include "helper.h"
 42
 43extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
 44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45/*
 46 * Sound Blaster remote control configuration
 47 *
 48 * format of remote control data:
 49 * Extigy:       xx 00
 50 * Audigy 2 NX:  06 80 xx 00 00 00
 51 * Live! 24-bit: 06 80 xx yy 22 83
 52 */
 53static const struct rc_config {
 54	u32 usb_id;
 55	u8  offset;
 56	u8  length;
 57	u8  packet_length;
 58	u8  min_packet_length; /* minimum accepted length of the URB result */
 59	u8  mute_mixer_id;
 60	u32 mute_code;
 61} rc_configs[] = {
 62	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
 63	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
 64	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
 65	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
 66	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
 67	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
 68};
 69
 70static void snd_usb_soundblaster_remote_complete(struct urb *urb)
 71{
 72	struct usb_mixer_interface *mixer = urb->context;
 73	const struct rc_config *rc = mixer->rc_cfg;
 74	u32 code;
 75
 76	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
 77		return;
 78
 79	code = mixer->rc_buffer[rc->offset];
 80	if (rc->length == 2)
 81		code |= mixer->rc_buffer[rc->offset + 1] << 8;
 82
 83	/* the Mute button actually changes the mixer control */
 84	if (code == rc->mute_code)
 85		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
 86	mixer->rc_code = code;
 87	wmb();
 88	wake_up(&mixer->rc_waitq);
 89}
 90
 91static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
 92				     long count, loff_t *offset)
 93{
 94	struct usb_mixer_interface *mixer = hw->private_data;
 95	int err;
 96	u32 rc_code;
 97
 98	if (count != 1 && count != 4)
 99		return -EINVAL;
100	err = wait_event_interruptible(mixer->rc_waitq,
101				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
102	if (err == 0) {
103		if (count == 1)
104			err = put_user(rc_code, buf);
105		else
106			err = put_user(rc_code, (u32 __user *)buf);
107	}
108	return err < 0 ? err : count;
109}
110
111static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
112					    poll_table *wait)
113{
114	struct usb_mixer_interface *mixer = hw->private_data;
115
116	poll_wait(file, &mixer->rc_waitq, wait);
117	return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
118}
119
120static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
121{
122	struct snd_hwdep *hwdep;
123	int err, len, i;
124
125	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
126		if (rc_configs[i].usb_id == mixer->chip->usb_id)
127			break;
128	if (i >= ARRAY_SIZE(rc_configs))
129		return 0;
130	mixer->rc_cfg = &rc_configs[i];
131
132	len = mixer->rc_cfg->packet_length;
133
134	init_waitqueue_head(&mixer->rc_waitq);
135	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
136	if (err < 0)
137		return err;
138	snprintf(hwdep->name, sizeof(hwdep->name),
139		 "%s remote control", mixer->chip->card->shortname);
140	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
141	hwdep->private_data = mixer;
142	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
143	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
144	hwdep->exclusive = 1;
145
146	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
147	if (!mixer->rc_urb)
148		return -ENOMEM;
149	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
150	if (!mixer->rc_setup_packet) {
151		usb_free_urb(mixer->rc_urb);
152		mixer->rc_urb = NULL;
153		return -ENOMEM;
154	}
155	mixer->rc_setup_packet->bRequestType =
156		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
157	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
158	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
159	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
160	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
161	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
162			     usb_rcvctrlpipe(mixer->chip->dev, 0),
163			     (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
164			     snd_usb_soundblaster_remote_complete, mixer);
165	return 0;
166}
167
168#define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
169
170static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
171{
172	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
173	int index = kcontrol->private_value;
174
175	ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
176	return 0;
177}
178
179static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
180{
181	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
182	int index = kcontrol->private_value;
183	int value = ucontrol->value.integer.value[0];
184	int err, changed;
185
186	if (value > 1)
187		return -EINVAL;
188	changed = value != mixer->audigy2nx_leds[index];
189	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
190		err = snd_usb_ctl_msg(mixer->chip->dev,
191			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
192			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
193			      !value, 0, NULL, 0, 100);
194	/* USB X-Fi S51 Pro */
195	if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
196		err = snd_usb_ctl_msg(mixer->chip->dev,
197			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
198			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
199			      !value, 0, NULL, 0, 100);
200	else
201		err = snd_usb_ctl_msg(mixer->chip->dev,
202			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
203			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
204			      value, index + 2, NULL, 0, 100);
205	if (err < 0)
206		return err;
207	mixer->audigy2nx_leds[index] = value;
208	return changed;
209}
210
211static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
212	{
213		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
214		.name = "CMSS LED Switch",
215		.info = snd_audigy2nx_led_info,
216		.get = snd_audigy2nx_led_get,
217		.put = snd_audigy2nx_led_put,
218		.private_value = 0,
219	},
220	{
221		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
222		.name = "Power LED Switch",
223		.info = snd_audigy2nx_led_info,
224		.get = snd_audigy2nx_led_get,
225		.put = snd_audigy2nx_led_put,
226		.private_value = 1,
227	},
228	{
229		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
230		.name = "Dolby Digital LED Switch",
231		.info = snd_audigy2nx_led_info,
232		.get = snd_audigy2nx_led_get,
233		.put = snd_audigy2nx_led_put,
234		.private_value = 2,
235	},
236};
237
238static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
239{
240	int i, err;
241
242	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
243		/* USB X-Fi S51 doesn't have a CMSS LED */
244		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
245			continue;
246		/* USB X-Fi S51 Pro doesn't have one either */
247		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
248			continue;
249		if (i > 1 && /* Live24ext has 2 LEDs only */
250			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
251			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
252			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
253			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
254			break; 
255		err = snd_ctl_add(mixer->chip->card,
256				  snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
257		if (err < 0)
258			return err;
259	}
260	mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
261	return 0;
262}
263
264static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
265				    struct snd_info_buffer *buffer)
266{
267	static const struct sb_jack {
268		int unitid;
269		const char *name;
270	}  jacks_audigy2nx[] = {
271		{4,  "dig in "},
272		{7,  "line in"},
273		{19, "spk out"},
274		{20, "hph out"},
275		{-1, NULL}
276	}, jacks_live24ext[] = {
277		{4,  "line in"}, /* &1=Line, &2=Mic*/
278		{3,  "hph out"}, /* headphones */
279		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
280		{-1, NULL}
281	};
282	const struct sb_jack *jacks;
283	struct usb_mixer_interface *mixer = entry->private_data;
284	int i, err;
285	u8 buf[3];
286
287	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
288	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
289		jacks = jacks_audigy2nx;
290	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
291		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
292		jacks = jacks_live24ext;
293	else
294		return;
295
296	for (i = 0; jacks[i].name; ++i) {
297		snd_iprintf(buffer, "%s: ", jacks[i].name);
298		err = snd_usb_ctl_msg(mixer->chip->dev,
299				      usb_rcvctrlpipe(mixer->chip->dev, 0),
300				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
301				      USB_RECIP_INTERFACE, 0,
302				      jacks[i].unitid << 8, buf, 3, 100);
303		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
304			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
305		else
306			snd_iprintf(buffer, "?\n");
307	}
308}
309
310static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
311				   struct snd_ctl_elem_value *ucontrol)
312{
313	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
314
315	ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
316	return 0;
317}
318
319static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
320				   struct snd_ctl_elem_value *ucontrol)
321{
322	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
323	u8 old_status, new_status;
324	int err, changed;
325
326	old_status = mixer->xonar_u1_status;
327	if (ucontrol->value.integer.value[0])
328		new_status = old_status | 0x02;
329	else
330		new_status = old_status & ~0x02;
331	changed = new_status != old_status;
332	err = snd_usb_ctl_msg(mixer->chip->dev,
333			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
334			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
335			      50, 0, &new_status, 1, 100);
336	if (err < 0)
337		return err;
338	mixer->xonar_u1_status = new_status;
339	return changed;
340}
341
342static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
343	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
344	.name = "Digital Playback Switch",
345	.info = snd_ctl_boolean_mono_info,
346	.get = snd_xonar_u1_switch_get,
347	.put = snd_xonar_u1_switch_put,
348};
349
350static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
351{
352	int err;
353
354	err = snd_ctl_add(mixer->chip->card,
355			  snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
356	if (err < 0)
357		return err;
358	mixer->xonar_u1_status = 0x05;
359	return 0;
360}
361
362/* Native Instruments device quirks */
363
364#define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
365
366static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
367					     struct snd_ctl_elem_value *ucontrol)
368{
369	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
370	struct usb_device *dev = mixer->chip->dev;
371	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
372	u16 wIndex = kcontrol->private_value & 0xffff;
373	u8 tmp;
374
375	int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
376				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
377				  0, cpu_to_le16(wIndex),
378				  &tmp, sizeof(tmp), 1000);
379
380	if (ret < 0) {
381		snd_printk(KERN_ERR
382			   "unable to issue vendor read request (ret = %d)", ret);
383		return ret;
384	}
385
386	ucontrol->value.integer.value[0] = tmp;
387
388	return 0;
389}
390
391static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
392					     struct snd_ctl_elem_value *ucontrol)
393{
394	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
395	struct usb_device *dev = mixer->chip->dev;
396	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
397	u16 wIndex = kcontrol->private_value & 0xffff;
398	u16 wValue = ucontrol->value.integer.value[0];
399
400	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
401				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
402				  cpu_to_le16(wValue), cpu_to_le16(wIndex),
403				  NULL, 0, 1000);
404
405	if (ret < 0) {
406		snd_printk(KERN_ERR
407			   "unable to issue vendor write request (ret = %d)", ret);
408		return ret;
409	}
410
411	return 0;
412}
413
414static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
415	{
416		.name = "Direct Thru Channel A",
417		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
418	},
419	{
420		.name = "Direct Thru Channel B",
421		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
422	},
423	{
424		.name = "Phono Input Channel A",
425		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
426	},
427	{
428		.name = "Phono Input Channel B",
429		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
430	},
431};
432
433static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
434	{
435		.name = "Direct Thru Channel A",
436		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
437	},
438	{
439		.name = "Direct Thru Channel B",
440		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
441	},
442	{
443		.name = "Direct Thru Channel C",
444		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
445	},
446	{
447		.name = "Direct Thru Channel D",
448		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
449	},
450	{
451		.name = "Phono Input Channel A",
452		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
453	},
454	{
455		.name = "Phono Input Channel B",
456		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
457	},
458	{
459		.name = "Phono Input Channel C",
460		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
461	},
462	{
463		.name = "Phono Input Channel D",
464		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
465	},
466};
467
468static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
469					      const struct snd_kcontrol_new *kc,
470					      unsigned int count)
471{
472	int i, err = 0;
473	struct snd_kcontrol_new template = {
474		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
475		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
476		.get = snd_nativeinstruments_control_get,
477		.put = snd_nativeinstruments_control_put,
478		.info = snd_ctl_boolean_mono_info,
479	};
480
481	for (i = 0; i < count; i++) {
482		struct snd_kcontrol *c;
483
484		template.name = kc[i].name;
485		template.private_value = kc[i].private_value;
486
487		c = snd_ctl_new1(&template, mixer);
488		err = snd_ctl_add(mixer->chip->card, c);
489
490		if (err < 0)
491			break;
492	}
493
494	return err;
495}
496
497/* M-Audio FastTrack Ultra quirks */
 
 
 
 
 
 
498
499/* private_free callback */
500static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
501{
502	kfree(kctl->private_data);
503	kctl->private_data = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504}
505
506static int snd_maudio_ftu_create_ctl(struct usb_mixer_interface *mixer,
507				     int in, int out, const char *name)
508{
509	struct usb_mixer_elem_info *cval;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510	struct snd_kcontrol *kctl;
 
511
512	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
513	if (!cval)
514		return -ENOMEM;
515
516	cval->id = 5;
517	cval->mixer = mixer;
518	cval->val_type = USB_MIXER_S16;
519	cval->channels = 1;
520	cval->control = out + 1;
521	cval->cmask = 1 << in;
522
523	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
 
524	if (!kctl) {
525		kfree(cval);
526		return -ENOMEM;
527	}
528
529	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
530	kctl->private_free = usb_mixer_elem_free;
531	return snd_usb_mixer_add_control(mixer, kctl);
 
 
532}
533
534static int snd_maudio_ftu_create_mixer(struct usb_mixer_interface *mixer)
 
535{
536	char name[64];
 
537	int in, out, err;
538
 
 
 
539	for (out = 0; out < 8; out++) {
 
540		for (in = 0; in < 8; in++) {
 
541			snprintf(name, sizeof(name),
542				 "AIn%d - Out%d Capture Volume", in  + 1, out + 1);
543			err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
 
 
 
544			if (err < 0)
545				return err;
546		}
547
548		for (in = 8; in < 16; in++) {
 
549			snprintf(name, sizeof(name),
550				 "DIn%d - Out%d Playback Volume", in - 7, out + 1);
551			err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
 
 
 
552			if (err < 0)
553				return err;
554		}
555	}
556
557	return 0;
558}
559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
561			       unsigned char samplerate_id)
562{
563	struct usb_mixer_interface *mixer;
564	struct usb_mixer_elem_info *cval;
565	int unitid = 12; /* SamleRate ExtensionUnit ID */
566
567	list_for_each_entry(mixer, &chip->mixer_list, list) {
568		cval = mixer->id_elems[unitid];
569		if (cval) {
570			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
571						    cval->control << 8,
572						    samplerate_id);
573			snd_usb_mixer_notify_id(mixer, unitid);
574		}
575		break;
576	}
577}
578
579int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
580{
581	int err = 0;
582	struct snd_info_entry *entry;
583
584	if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
585		return err;
586
587	switch (mixer->chip->usb_id) {
588	case USB_ID(0x041e, 0x3020):
589	case USB_ID(0x041e, 0x3040):
590	case USB_ID(0x041e, 0x3042):
591	case USB_ID(0x041e, 0x30df):
592	case USB_ID(0x041e, 0x3048):
593		err = snd_audigy2nx_controls_create(mixer);
594		if (err < 0)
595			break;
596		if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
597			snd_info_set_text_ops(entry, mixer,
598					      snd_audigy2nx_proc_read);
599		break;
600
601	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
602	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
603		err = snd_maudio_ftu_create_mixer(mixer);
604		break;
605
606	case USB_ID(0x0b05, 0x1739):
607	case USB_ID(0x0b05, 0x1743):
608		err = snd_xonar_u1_controls_create(mixer);
609		break;
610
611	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
612		err = snd_nativeinstruments_create_mixer(mixer,
613				snd_nativeinstruments_ta6_mixers,
614				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
615		break;
616
617	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
618		err = snd_nativeinstruments_create_mixer(mixer,
619				snd_nativeinstruments_ta10_mixers,
620				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
 
 
 
 
621		break;
622	}
623
624	return err;
625}
626
627void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
628				    int unitid)
629{
630	if (!mixer->rc_cfg)
631		return;
632	/* unit ids specific to Extigy/Audigy 2 NX: */
633	switch (unitid) {
634	case 0: /* remote control */
635		mixer->rc_urb->dev = mixer->chip->dev;
636		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
637		break;
638	case 4: /* digital in jack */
639	case 7: /* line in jacks */
640	case 19: /* speaker out jacks */
641	case 20: /* headphones out jack */
642		break;
643	/* live24ext: 4 = line-in jack */
644	case 3:	/* hp-out jack (may actuate Mute) */
645		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
646		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
647			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
648		break;
649	default:
650		snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
651		break;
652	}
653}
654
v3.5.6
   1/*
   2 *   USB Audio Driver for ALSA
   3 *
   4 *   Quirks and vendor-specific extensions for mixer interfaces
   5 *
   6 *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
   7 *
   8 *   Many codes borrowed from audio.c by
   9 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
  10 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
  11 *
  12 *
  13 *   This program is free software; you can redistribute it and/or modify
  14 *   it under the terms of the GNU General Public License as published by
  15 *   the Free Software Foundation; either version 2 of the License, or
  16 *   (at your option) any later version.
  17 *
  18 *   This program is distributed in the hope that it will be useful,
  19 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 *   GNU General Public License for more details.
  22 *
  23 *   You should have received a copy of the GNU General Public License
  24 *   along with this program; if not, write to the Free Software
  25 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26 */
  27
  28#include <linux/init.h>
  29#include <linux/slab.h>
  30#include <linux/usb.h>
  31#include <linux/usb/audio.h>
  32
  33#include <sound/core.h>
  34#include <sound/control.h>
  35#include <sound/hwdep.h>
  36#include <sound/info.h>
  37
  38#include "usbaudio.h"
  39#include "mixer.h"
  40#include "mixer_quirks.h"
  41#include "helper.h"
  42
  43extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
  44
  45/* private_free callback */
  46static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
  47{
  48	kfree(kctl->private_data);
  49	kctl->private_data = NULL;
  50}
  51
  52/* This function allows for the creation of standard UAC controls.
  53 * See the quirks for M-Audio FTUs or Ebox-44.
  54 * If you don't want to set a TLV callback pass NULL.
  55 *
  56 * Since there doesn't seem to be a devices that needs a multichannel
  57 * version, we keep it mono for simplicity.
  58 */
  59static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
  60				unsigned int unitid,
  61				unsigned int control,
  62				unsigned int cmask,
  63				int val_type,
  64				const char *name,
  65				snd_kcontrol_tlv_rw_t *tlv_callback)
  66{
  67	int err;
  68	struct usb_mixer_elem_info *cval;
  69	struct snd_kcontrol *kctl;
  70
  71	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  72	if (!cval)
  73		return -ENOMEM;
  74
  75	cval->id = unitid;
  76	cval->mixer = mixer;
  77	cval->val_type = val_type;
  78	cval->channels = 1;
  79	cval->control = control;
  80	cval->cmask = cmask;
  81
  82	/* get_min_max() is called only for integer volumes later,
  83	 * so provide a short-cut for booleans */
  84	cval->min = 0;
  85	cval->max = 1;
  86	cval->res = 0;
  87	cval->dBmin = 0;
  88	cval->dBmax = 0;
  89
  90	/* Create control */
  91	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
  92	if (!kctl) {
  93		kfree(cval);
  94		return -ENOMEM;
  95	}
  96
  97	/* Set name */
  98	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
  99	kctl->private_free = usb_mixer_elem_free;
 100
 101	/* set TLV */
 102	if (tlv_callback) {
 103		kctl->tlv.c = tlv_callback;
 104		kctl->vd[0].access |=
 105			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
 106			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
 107	}
 108	/* Add control to mixer */
 109	err = snd_usb_mixer_add_control(mixer, kctl);
 110	if (err < 0)
 111		return err;
 112
 113	return 0;
 114}
 115
 116/*
 117 * Sound Blaster remote control configuration
 118 *
 119 * format of remote control data:
 120 * Extigy:       xx 00
 121 * Audigy 2 NX:  06 80 xx 00 00 00
 122 * Live! 24-bit: 06 80 xx yy 22 83
 123 */
 124static const struct rc_config {
 125	u32 usb_id;
 126	u8  offset;
 127	u8  length;
 128	u8  packet_length;
 129	u8  min_packet_length; /* minimum accepted length of the URB result */
 130	u8  mute_mixer_id;
 131	u32 mute_code;
 132} rc_configs[] = {
 133	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
 134	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
 135	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
 136	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
 137	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
 138	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
 139};
 140
 141static void snd_usb_soundblaster_remote_complete(struct urb *urb)
 142{
 143	struct usb_mixer_interface *mixer = urb->context;
 144	const struct rc_config *rc = mixer->rc_cfg;
 145	u32 code;
 146
 147	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
 148		return;
 149
 150	code = mixer->rc_buffer[rc->offset];
 151	if (rc->length == 2)
 152		code |= mixer->rc_buffer[rc->offset + 1] << 8;
 153
 154	/* the Mute button actually changes the mixer control */
 155	if (code == rc->mute_code)
 156		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
 157	mixer->rc_code = code;
 158	wmb();
 159	wake_up(&mixer->rc_waitq);
 160}
 161
 162static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
 163				     long count, loff_t *offset)
 164{
 165	struct usb_mixer_interface *mixer = hw->private_data;
 166	int err;
 167	u32 rc_code;
 168
 169	if (count != 1 && count != 4)
 170		return -EINVAL;
 171	err = wait_event_interruptible(mixer->rc_waitq,
 172				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
 173	if (err == 0) {
 174		if (count == 1)
 175			err = put_user(rc_code, buf);
 176		else
 177			err = put_user(rc_code, (u32 __user *)buf);
 178	}
 179	return err < 0 ? err : count;
 180}
 181
 182static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
 183					    poll_table *wait)
 184{
 185	struct usb_mixer_interface *mixer = hw->private_data;
 186
 187	poll_wait(file, &mixer->rc_waitq, wait);
 188	return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
 189}
 190
 191static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
 192{
 193	struct snd_hwdep *hwdep;
 194	int err, len, i;
 195
 196	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
 197		if (rc_configs[i].usb_id == mixer->chip->usb_id)
 198			break;
 199	if (i >= ARRAY_SIZE(rc_configs))
 200		return 0;
 201	mixer->rc_cfg = &rc_configs[i];
 202
 203	len = mixer->rc_cfg->packet_length;
 204
 205	init_waitqueue_head(&mixer->rc_waitq);
 206	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
 207	if (err < 0)
 208		return err;
 209	snprintf(hwdep->name, sizeof(hwdep->name),
 210		 "%s remote control", mixer->chip->card->shortname);
 211	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
 212	hwdep->private_data = mixer;
 213	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
 214	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
 215	hwdep->exclusive = 1;
 216
 217	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
 218	if (!mixer->rc_urb)
 219		return -ENOMEM;
 220	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
 221	if (!mixer->rc_setup_packet) {
 222		usb_free_urb(mixer->rc_urb);
 223		mixer->rc_urb = NULL;
 224		return -ENOMEM;
 225	}
 226	mixer->rc_setup_packet->bRequestType =
 227		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
 228	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
 229	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
 230	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
 231	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
 232	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
 233			     usb_rcvctrlpipe(mixer->chip->dev, 0),
 234			     (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
 235			     snd_usb_soundblaster_remote_complete, mixer);
 236	return 0;
 237}
 238
 239#define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
 240
 241static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
 242{
 243	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 244	int index = kcontrol->private_value;
 245
 246	ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
 247	return 0;
 248}
 249
 250static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
 251{
 252	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 253	int index = kcontrol->private_value;
 254	int value = ucontrol->value.integer.value[0];
 255	int err, changed;
 256
 257	if (value > 1)
 258		return -EINVAL;
 259	changed = value != mixer->audigy2nx_leds[index];
 260	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
 261		err = snd_usb_ctl_msg(mixer->chip->dev,
 262			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
 263			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 264			      !value, 0, NULL, 0);
 265	/* USB X-Fi S51 Pro */
 266	if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
 267		err = snd_usb_ctl_msg(mixer->chip->dev,
 268			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
 269			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 270			      !value, 0, NULL, 0);
 271	else
 272		err = snd_usb_ctl_msg(mixer->chip->dev,
 273			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
 274			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 275			      value, index + 2, NULL, 0);
 276	if (err < 0)
 277		return err;
 278	mixer->audigy2nx_leds[index] = value;
 279	return changed;
 280}
 281
 282static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
 283	{
 284		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 285		.name = "CMSS LED Switch",
 286		.info = snd_audigy2nx_led_info,
 287		.get = snd_audigy2nx_led_get,
 288		.put = snd_audigy2nx_led_put,
 289		.private_value = 0,
 290	},
 291	{
 292		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 293		.name = "Power LED Switch",
 294		.info = snd_audigy2nx_led_info,
 295		.get = snd_audigy2nx_led_get,
 296		.put = snd_audigy2nx_led_put,
 297		.private_value = 1,
 298	},
 299	{
 300		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 301		.name = "Dolby Digital LED Switch",
 302		.info = snd_audigy2nx_led_info,
 303		.get = snd_audigy2nx_led_get,
 304		.put = snd_audigy2nx_led_put,
 305		.private_value = 2,
 306	},
 307};
 308
 309static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
 310{
 311	int i, err;
 312
 313	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
 314		/* USB X-Fi S51 doesn't have a CMSS LED */
 315		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
 316			continue;
 317		/* USB X-Fi S51 Pro doesn't have one either */
 318		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
 319			continue;
 320		if (i > 1 && /* Live24ext has 2 LEDs only */
 321			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
 322			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
 323			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
 324			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
 325			break; 
 326		err = snd_ctl_add(mixer->chip->card,
 327				  snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
 328		if (err < 0)
 329			return err;
 330	}
 331	mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
 332	return 0;
 333}
 334
 335static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
 336				    struct snd_info_buffer *buffer)
 337{
 338	static const struct sb_jack {
 339		int unitid;
 340		const char *name;
 341	}  jacks_audigy2nx[] = {
 342		{4,  "dig in "},
 343		{7,  "line in"},
 344		{19, "spk out"},
 345		{20, "hph out"},
 346		{-1, NULL}
 347	}, jacks_live24ext[] = {
 348		{4,  "line in"}, /* &1=Line, &2=Mic*/
 349		{3,  "hph out"}, /* headphones */
 350		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
 351		{-1, NULL}
 352	};
 353	const struct sb_jack *jacks;
 354	struct usb_mixer_interface *mixer = entry->private_data;
 355	int i, err;
 356	u8 buf[3];
 357
 358	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
 359	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
 360		jacks = jacks_audigy2nx;
 361	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
 362		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
 363		jacks = jacks_live24ext;
 364	else
 365		return;
 366
 367	for (i = 0; jacks[i].name; ++i) {
 368		snd_iprintf(buffer, "%s: ", jacks[i].name);
 369		err = snd_usb_ctl_msg(mixer->chip->dev,
 370				      usb_rcvctrlpipe(mixer->chip->dev, 0),
 371				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
 372				      USB_RECIP_INTERFACE, 0,
 373				      jacks[i].unitid << 8, buf, 3);
 374		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
 375			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
 376		else
 377			snd_iprintf(buffer, "?\n");
 378	}
 379}
 380
 381static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
 382				   struct snd_ctl_elem_value *ucontrol)
 383{
 384	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 385
 386	ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
 387	return 0;
 388}
 389
 390static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
 391				   struct snd_ctl_elem_value *ucontrol)
 392{
 393	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 394	u8 old_status, new_status;
 395	int err, changed;
 396
 397	old_status = mixer->xonar_u1_status;
 398	if (ucontrol->value.integer.value[0])
 399		new_status = old_status | 0x02;
 400	else
 401		new_status = old_status & ~0x02;
 402	changed = new_status != old_status;
 403	err = snd_usb_ctl_msg(mixer->chip->dev,
 404			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
 405			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 406			      50, 0, &new_status, 1);
 407	if (err < 0)
 408		return err;
 409	mixer->xonar_u1_status = new_status;
 410	return changed;
 411}
 412
 413static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
 414	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 415	.name = "Digital Playback Switch",
 416	.info = snd_ctl_boolean_mono_info,
 417	.get = snd_xonar_u1_switch_get,
 418	.put = snd_xonar_u1_switch_put,
 419};
 420
 421static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
 422{
 423	int err;
 424
 425	err = snd_ctl_add(mixer->chip->card,
 426			  snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
 427	if (err < 0)
 428		return err;
 429	mixer->xonar_u1_status = 0x05;
 430	return 0;
 431}
 432
 433/* Native Instruments device quirks */
 434
 435#define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
 436
 437static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
 438					     struct snd_ctl_elem_value *ucontrol)
 439{
 440	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 441	struct usb_device *dev = mixer->chip->dev;
 442	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
 443	u16 wIndex = kcontrol->private_value & 0xffff;
 444	u8 tmp;
 445
 446	int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
 447				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 448				  0, cpu_to_le16(wIndex),
 449				  &tmp, sizeof(tmp), 1000);
 450
 451	if (ret < 0) {
 452		snd_printk(KERN_ERR
 453			   "unable to issue vendor read request (ret = %d)", ret);
 454		return ret;
 455	}
 456
 457	ucontrol->value.integer.value[0] = tmp;
 458
 459	return 0;
 460}
 461
 462static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
 463					     struct snd_ctl_elem_value *ucontrol)
 464{
 465	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
 466	struct usb_device *dev = mixer->chip->dev;
 467	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
 468	u16 wIndex = kcontrol->private_value & 0xffff;
 469	u16 wValue = ucontrol->value.integer.value[0];
 470
 471	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
 472				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 473				  cpu_to_le16(wValue), cpu_to_le16(wIndex),
 474				  NULL, 0, 1000);
 475
 476	if (ret < 0) {
 477		snd_printk(KERN_ERR
 478			   "unable to issue vendor write request (ret = %d)", ret);
 479		return ret;
 480	}
 481
 482	return 0;
 483}
 484
 485static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
 486	{
 487		.name = "Direct Thru Channel A",
 488		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
 489	},
 490	{
 491		.name = "Direct Thru Channel B",
 492		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
 493	},
 494	{
 495		.name = "Phono Input Channel A",
 496		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
 497	},
 498	{
 499		.name = "Phono Input Channel B",
 500		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
 501	},
 502};
 503
 504static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
 505	{
 506		.name = "Direct Thru Channel A",
 507		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
 508	},
 509	{
 510		.name = "Direct Thru Channel B",
 511		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
 512	},
 513	{
 514		.name = "Direct Thru Channel C",
 515		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
 516	},
 517	{
 518		.name = "Direct Thru Channel D",
 519		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
 520	},
 521	{
 522		.name = "Phono Input Channel A",
 523		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
 524	},
 525	{
 526		.name = "Phono Input Channel B",
 527		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
 528	},
 529	{
 530		.name = "Phono Input Channel C",
 531		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
 532	},
 533	{
 534		.name = "Phono Input Channel D",
 535		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
 536	},
 537};
 538
 539static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
 540					      const struct snd_kcontrol_new *kc,
 541					      unsigned int count)
 542{
 543	int i, err = 0;
 544	struct snd_kcontrol_new template = {
 545		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 546		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 547		.get = snd_nativeinstruments_control_get,
 548		.put = snd_nativeinstruments_control_put,
 549		.info = snd_ctl_boolean_mono_info,
 550	};
 551
 552	for (i = 0; i < count; i++) {
 553		struct snd_kcontrol *c;
 554
 555		template.name = kc[i].name;
 556		template.private_value = kc[i].private_value;
 557
 558		c = snd_ctl_new1(&template, mixer);
 559		err = snd_ctl_add(mixer->chip->card, c);
 560
 561		if (err < 0)
 562			break;
 563	}
 564
 565	return err;
 566}
 567
 568/* M-Audio FastTrack Ultra quirks */
 569/* FTU Effect switch */
 570struct snd_ftu_eff_switch_priv_val {
 571	struct usb_mixer_interface *mixer;
 572	int cached_value;
 573	int is_cached;
 574};
 575
 576static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
 577					struct snd_ctl_elem_info *uinfo)
 578{
 579	static const char *texts[8] = {"Room 1",
 580				       "Room 2",
 581				       "Room 3",
 582				       "Hall 1",
 583				       "Hall 2",
 584				       "Plate",
 585				       "Delay",
 586				       "Echo"
 587	};
 588
 589	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 590	uinfo->count = 1;
 591	uinfo->value.enumerated.items = 8;
 592	if (uinfo->value.enumerated.item > 7)
 593		uinfo->value.enumerated.item = 7;
 594	strcpy(uinfo->value.enumerated.name,
 595		texts[uinfo->value.enumerated.item]);
 596
 597	return 0;
 598}
 599
 600static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
 601					struct snd_ctl_elem_value *ucontrol)
 602{
 603	struct snd_usb_audio *chip;
 604	struct usb_mixer_interface *mixer;
 605	struct snd_ftu_eff_switch_priv_val *pval;
 606	int err;
 607	unsigned char value[2];
 608
 609	const int id = 6;
 610	const int validx = 1;
 611	const int val_len = 2;
 612
 613	value[0] = 0x00;
 614	value[1] = 0x00;
 615
 616	pval = (struct snd_ftu_eff_switch_priv_val *)
 617		kctl->private_value;
 618
 619	if (pval->is_cached) {
 620		ucontrol->value.enumerated.item[0] = pval->cached_value;
 621		return 0;
 622	}
 623
 624	mixer = (struct usb_mixer_interface *) pval->mixer;
 625	if (snd_BUG_ON(!mixer))
 626		return -EINVAL;
 627
 628	chip = (struct snd_usb_audio *) mixer->chip;
 629	if (snd_BUG_ON(!chip))
 630		return -EINVAL;
 631
 632
 633	err = snd_usb_ctl_msg(chip->dev,
 634			usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
 635			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
 636			validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
 637			value, val_len);
 638	if (err < 0)
 639		return err;
 640
 641	ucontrol->value.enumerated.item[0] = value[0];
 642	pval->cached_value = value[0];
 643	pval->is_cached = 1;
 644
 645	return 0;
 646}
 647
 648static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
 649					struct snd_ctl_elem_value *ucontrol)
 650{
 651	struct snd_usb_audio *chip;
 652	struct snd_ftu_eff_switch_priv_val *pval;
 653
 654	struct usb_mixer_interface *mixer;
 655	int changed, cur_val, err, new_val;
 656	unsigned char value[2];
 657
 658
 659	const int id = 6;
 660	const int validx = 1;
 661	const int val_len = 2;
 662
 663	changed = 0;
 664
 665	pval = (struct snd_ftu_eff_switch_priv_val *)
 666		kctl->private_value;
 667	cur_val = pval->cached_value;
 668	new_val = ucontrol->value.enumerated.item[0];
 669
 670	mixer = (struct usb_mixer_interface *) pval->mixer;
 671	if (snd_BUG_ON(!mixer))
 672		return -EINVAL;
 673
 674	chip = (struct snd_usb_audio *) mixer->chip;
 675	if (snd_BUG_ON(!chip))
 676		return -EINVAL;
 677
 678	if (!pval->is_cached) {
 679		/* Read current value */
 680		err = snd_usb_ctl_msg(chip->dev,
 681				usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,
 682				USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
 683				validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
 684				value, val_len);
 685		if (err < 0)
 686			return err;
 687
 688		cur_val = value[0];
 689		pval->cached_value = cur_val;
 690		pval->is_cached = 1;
 691	}
 692	/* update value if needed */
 693	if (cur_val != new_val) {
 694		value[0] = new_val;
 695		value[1] = 0;
 696		err = snd_usb_ctl_msg(chip->dev,
 697				usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
 698				USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
 699				validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),
 700				value, val_len);
 701		if (err < 0)
 702			return err;
 703
 704		pval->cached_value = new_val;
 705		pval->is_cached = 1;
 706		changed = 1;
 707	}
 708
 709	return changed;
 710}
 711
 712static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer)
 713{
 714	static struct snd_kcontrol_new template = {
 715		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 716		.name = "Effect Program Switch",
 717		.index = 0,
 718		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 719		.info = snd_ftu_eff_switch_info,
 720		.get = snd_ftu_eff_switch_get,
 721		.put = snd_ftu_eff_switch_put
 722	};
 723
 724	int err;
 725	struct snd_kcontrol *kctl;
 726	struct snd_ftu_eff_switch_priv_val *pval;
 727
 728	pval = kzalloc(sizeof(*pval), GFP_KERNEL);
 729	if (!pval)
 730		return -ENOMEM;
 731
 732	pval->cached_value = 0;
 733	pval->is_cached = 0;
 734	pval->mixer = mixer;
 
 
 
 735
 736	template.private_value = (unsigned long) pval;
 737	kctl = snd_ctl_new1(&template, mixer->chip);
 738	if (!kctl) {
 739		kfree(pval);
 740		return -ENOMEM;
 741	}
 742
 743	err = snd_ctl_add(mixer->chip->card, kctl);
 744	if (err < 0)
 745		return err;
 746
 747	return 0;
 748}
 749
 750/* Create volume controls for FTU devices*/
 751static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
 752{
 753	char name[64];
 754	unsigned int control, cmask;
 755	int in, out, err;
 756
 757	const unsigned int id = 5;
 758	const int val_type = USB_MIXER_S16;
 759
 760	for (out = 0; out < 8; out++) {
 761		control = out + 1;
 762		for (in = 0; in < 8; in++) {
 763			cmask = 1 << in;
 764			snprintf(name, sizeof(name),
 765				"AIn%d - Out%d Capture Volume",
 766				in  + 1, out + 1);
 767			err = snd_create_std_mono_ctl(mixer, id, control,
 768							cmask, val_type, name,
 769							&snd_usb_mixer_vol_tlv);
 770			if (err < 0)
 771				return err;
 772		}
 
 773		for (in = 8; in < 16; in++) {
 774			cmask = 1 << in;
 775			snprintf(name, sizeof(name),
 776				"DIn%d - Out%d Playback Volume",
 777				in - 7, out + 1);
 778			err = snd_create_std_mono_ctl(mixer, id, control,
 779							cmask, val_type, name,
 780							&snd_usb_mixer_vol_tlv);
 781			if (err < 0)
 782				return err;
 783		}
 784	}
 785
 786	return 0;
 787}
 788
 789/* This control needs a volume quirk, see mixer.c */
 790static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
 791{
 792	static const char name[] = "Effect Volume";
 793	const unsigned int id = 6;
 794	const int val_type = USB_MIXER_U8;
 795	const unsigned int control = 2;
 796	const unsigned int cmask = 0;
 797
 798	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
 799					name, snd_usb_mixer_vol_tlv);
 800}
 801
 802/* This control needs a volume quirk, see mixer.c */
 803static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
 804{
 805	static const char name[] = "Effect Duration";
 806	const unsigned int id = 6;
 807	const int val_type = USB_MIXER_S16;
 808	const unsigned int control = 3;
 809	const unsigned int cmask = 0;
 810
 811	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
 812					name, snd_usb_mixer_vol_tlv);
 813}
 814
 815/* This control needs a volume quirk, see mixer.c */
 816static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
 817{
 818	static const char name[] = "Effect Feedback Volume";
 819	const unsigned int id = 6;
 820	const int val_type = USB_MIXER_U8;
 821	const unsigned int control = 4;
 822	const unsigned int cmask = 0;
 823
 824	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
 825					name, NULL);
 826}
 827
 828static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
 829{
 830	unsigned int cmask;
 831	int err, ch;
 832	char name[48];
 833
 834	const unsigned int id = 7;
 835	const int val_type = USB_MIXER_S16;
 836	const unsigned int control = 7;
 837
 838	for (ch = 0; ch < 4; ++ch) {
 839		cmask = 1 << ch;
 840		snprintf(name, sizeof(name),
 841			"Effect Return %d Volume", ch + 1);
 842		err = snd_create_std_mono_ctl(mixer, id, control,
 843						cmask, val_type, name,
 844						snd_usb_mixer_vol_tlv);
 845		if (err < 0)
 846			return err;
 847	}
 848
 849	return 0;
 850}
 851
 852static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
 853{
 854	unsigned int  cmask;
 855	int err, ch;
 856	char name[48];
 857
 858	const unsigned int id = 5;
 859	const int val_type = USB_MIXER_S16;
 860	const unsigned int control = 9;
 861
 862	for (ch = 0; ch < 8; ++ch) {
 863		cmask = 1 << ch;
 864		snprintf(name, sizeof(name),
 865			"Effect Send AIn%d Volume", ch + 1);
 866		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
 867						val_type, name,
 868						snd_usb_mixer_vol_tlv);
 869		if (err < 0)
 870			return err;
 871	}
 872	for (ch = 8; ch < 16; ++ch) {
 873		cmask = 1 << ch;
 874		snprintf(name, sizeof(name),
 875			"Effect Send DIn%d Volume", ch - 7);
 876		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
 877						val_type, name,
 878						snd_usb_mixer_vol_tlv);
 879		if (err < 0)
 880			return err;
 881	}
 882	return 0;
 883}
 884
 885static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
 886{
 887	int err;
 888
 889	err = snd_ftu_create_volume_ctls(mixer);
 890	if (err < 0)
 891		return err;
 892
 893	err = snd_ftu_create_effect_switch(mixer);
 894	if (err < 0)
 895		return err;
 896	err = snd_ftu_create_effect_volume_ctl(mixer);
 897	if (err < 0)
 898		return err;
 899
 900	err = snd_ftu_create_effect_duration_ctl(mixer);
 901	if (err < 0)
 902		return err;
 903
 904	err = snd_ftu_create_effect_feedback_ctl(mixer);
 905	if (err < 0)
 906		return err;
 907
 908	err = snd_ftu_create_effect_return_ctls(mixer);
 909	if (err < 0)
 910		return err;
 911
 912	err = snd_ftu_create_effect_send_ctls(mixer);
 913	if (err < 0)
 914		return err;
 915
 916	return 0;
 917}
 918
 919
 920/*
 921 * Create mixer for Electrix Ebox-44
 922 *
 923 * The mixer units from this device are corrupt, and even where they
 924 * are valid they presents mono controls as L and R channels of
 925 * stereo. So we create a good mixer in code.
 926 */
 927
 928static int snd_ebox44_create_mixer(struct usb_mixer_interface *mixer)
 929{
 930	int err;
 931
 932	err = snd_create_std_mono_ctl(mixer, 4, 1, 0x0, USB_MIXER_INV_BOOLEAN,
 933				"Headphone Playback Switch", NULL);
 934	if (err < 0)
 935		return err;
 936	err = snd_create_std_mono_ctl(mixer, 4, 2, 0x1, USB_MIXER_S16,
 937				"Headphone A Mix Playback Volume", NULL);
 938	if (err < 0)
 939		return err;
 940	err = snd_create_std_mono_ctl(mixer, 4, 2, 0x2, USB_MIXER_S16,
 941				"Headphone B Mix Playback Volume", NULL);
 942	if (err < 0)
 943		return err;
 944
 945	err = snd_create_std_mono_ctl(mixer, 7, 1, 0x0, USB_MIXER_INV_BOOLEAN,
 946				"Output Playback Switch", NULL);
 947	if (err < 0)
 948		return err;
 949	err = snd_create_std_mono_ctl(mixer, 7, 2, 0x1, USB_MIXER_S16,
 950				"Output A Playback Volume", NULL);
 951	if (err < 0)
 952		return err;
 953	err = snd_create_std_mono_ctl(mixer, 7, 2, 0x2, USB_MIXER_S16,
 954				"Output B Playback Volume", NULL);
 955	if (err < 0)
 956		return err;
 957
 958	err = snd_create_std_mono_ctl(mixer, 10, 1, 0x0, USB_MIXER_INV_BOOLEAN,
 959				"Input Capture Switch", NULL);
 960	if (err < 0)
 961		return err;
 962	err = snd_create_std_mono_ctl(mixer, 10, 2, 0x1, USB_MIXER_S16,
 963				"Input A Capture Volume", NULL);
 964	if (err < 0)
 965		return err;
 966	err = snd_create_std_mono_ctl(mixer, 10, 2, 0x2, USB_MIXER_S16,
 967				"Input B Capture Volume", NULL);
 968	if (err < 0)
 969		return err;
 970
 971	return 0;
 972}
 973
 974void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
 975			       unsigned char samplerate_id)
 976{
 977	struct usb_mixer_interface *mixer;
 978	struct usb_mixer_elem_info *cval;
 979	int unitid = 12; /* SamleRate ExtensionUnit ID */
 980
 981	list_for_each_entry(mixer, &chip->mixer_list, list) {
 982		cval = mixer->id_elems[unitid];
 983		if (cval) {
 984			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
 985						    cval->control << 8,
 986						    samplerate_id);
 987			snd_usb_mixer_notify_id(mixer, unitid);
 988		}
 989		break;
 990	}
 991}
 992
 993int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
 994{
 995	int err = 0;
 996	struct snd_info_entry *entry;
 997
 998	if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
 999		return err;
1000
1001	switch (mixer->chip->usb_id) {
1002	case USB_ID(0x041e, 0x3020):
1003	case USB_ID(0x041e, 0x3040):
1004	case USB_ID(0x041e, 0x3042):
1005	case USB_ID(0x041e, 0x30df):
1006	case USB_ID(0x041e, 0x3048):
1007		err = snd_audigy2nx_controls_create(mixer);
1008		if (err < 0)
1009			break;
1010		if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1011			snd_info_set_text_ops(entry, mixer,
1012					      snd_audigy2nx_proc_read);
1013		break;
1014
1015	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1016	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1017		err = snd_ftu_create_mixer(mixer);
1018		break;
1019
1020	case USB_ID(0x0b05, 0x1739):
1021	case USB_ID(0x0b05, 0x1743):
1022		err = snd_xonar_u1_controls_create(mixer);
1023		break;
1024
1025	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1026		err = snd_nativeinstruments_create_mixer(mixer,
1027				snd_nativeinstruments_ta6_mixers,
1028				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1029		break;
1030
1031	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1032		err = snd_nativeinstruments_create_mixer(mixer,
1033				snd_nativeinstruments_ta10_mixers,
1034				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1035		break;
1036
1037	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1038		err = snd_ebox44_create_mixer(mixer);
1039		break;
1040	}
1041
1042	return err;
1043}
1044
1045void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1046				    int unitid)
1047{
1048	if (!mixer->rc_cfg)
1049		return;
1050	/* unit ids specific to Extigy/Audigy 2 NX: */
1051	switch (unitid) {
1052	case 0: /* remote control */
1053		mixer->rc_urb->dev = mixer->chip->dev;
1054		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1055		break;
1056	case 4: /* digital in jack */
1057	case 7: /* line in jacks */
1058	case 19: /* speaker out jacks */
1059	case 20: /* headphones out jack */
1060		break;
1061	/* live24ext: 4 = line-in jack */
1062	case 3:	/* hp-out jack (may actuate Mute) */
1063		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1064		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1065			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1066		break;
1067	default:
1068		snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1069		break;
1070	}
1071}
1072