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 
43 extern 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  */
53 static 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 
snd_usb_soundblaster_remote_complete(struct urb * urb)70 static 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 
snd_usb_sbrc_hwdep_read(struct snd_hwdep * hw,char __user * buf,long count,loff_t * offset)91 static 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 
snd_usb_sbrc_hwdep_poll(struct snd_hwdep * hw,struct file * file,poll_table * wait)111 static 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 
snd_usb_soundblaster_remote_init(struct usb_mixer_interface * mixer)120 static 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 
snd_audigy2nx_led_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)170 static 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 
snd_audigy2nx_led_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)179 static 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 	down_read(&mixer->chip->shutdown_rwsem);
190 	if (mixer->chip->shutdown) {
191 		err = -ENODEV;
192 		goto out;
193 	}
194 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
195 		err = snd_usb_ctl_msg(mixer->chip->dev,
196 			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
197 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
198 			      !value, 0, NULL, 0);
199 	/* USB X-Fi S51 Pro */
200 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df))
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, 0, NULL, 0);
205 	else
206 		err = snd_usb_ctl_msg(mixer->chip->dev,
207 			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
208 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
209 			      value, index + 2, NULL, 0);
210  out:
211 	up_read(&mixer->chip->shutdown_rwsem);
212 	if (err < 0)
213 		return err;
214 	mixer->audigy2nx_leds[index] = value;
215 	return changed;
216 }
217 
218 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
219 	{
220 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
221 		.name = "CMSS LED Switch",
222 		.info = snd_audigy2nx_led_info,
223 		.get = snd_audigy2nx_led_get,
224 		.put = snd_audigy2nx_led_put,
225 		.private_value = 0,
226 	},
227 	{
228 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
229 		.name = "Power LED Switch",
230 		.info = snd_audigy2nx_led_info,
231 		.get = snd_audigy2nx_led_get,
232 		.put = snd_audigy2nx_led_put,
233 		.private_value = 1,
234 	},
235 	{
236 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
237 		.name = "Dolby Digital LED Switch",
238 		.info = snd_audigy2nx_led_info,
239 		.get = snd_audigy2nx_led_get,
240 		.put = snd_audigy2nx_led_put,
241 		.private_value = 2,
242 	},
243 };
244 
snd_audigy2nx_controls_create(struct usb_mixer_interface * mixer)245 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
246 {
247 	int i, err;
248 
249 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
250 		/* USB X-Fi S51 doesn't have a CMSS LED */
251 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
252 			continue;
253 		/* USB X-Fi S51 Pro doesn't have one either */
254 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
255 			continue;
256 		if (i > 1 && /* Live24ext has 2 LEDs only */
257 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
258 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
259 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
260 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
261 			break;
262 		err = snd_ctl_add(mixer->chip->card,
263 				  snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
264 		if (err < 0)
265 			return err;
266 	}
267 	mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
268 	return 0;
269 }
270 
snd_audigy2nx_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)271 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
272 				    struct snd_info_buffer *buffer)
273 {
274 	static const struct sb_jack {
275 		int unitid;
276 		const char *name;
277 	}  jacks_audigy2nx[] = {
278 		{4,  "dig in "},
279 		{7,  "line in"},
280 		{19, "spk out"},
281 		{20, "hph out"},
282 		{-1, NULL}
283 	}, jacks_live24ext[] = {
284 		{4,  "line in"}, /* &1=Line, &2=Mic*/
285 		{3,  "hph out"}, /* headphones */
286 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
287 		{-1, NULL}
288 	};
289 	const struct sb_jack *jacks;
290 	struct usb_mixer_interface *mixer = entry->private_data;
291 	int i, err;
292 	u8 buf[3];
293 
294 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
295 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
296 		jacks = jacks_audigy2nx;
297 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
298 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
299 		jacks = jacks_live24ext;
300 	else
301 		return;
302 
303 	for (i = 0; jacks[i].name; ++i) {
304 		snd_iprintf(buffer, "%s: ", jacks[i].name);
305 		down_read(&mixer->chip->shutdown_rwsem);
306 		if (mixer->chip->shutdown)
307 			err = 0;
308 		else
309 			err = snd_usb_ctl_msg(mixer->chip->dev,
310 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
311 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
312 				      USB_RECIP_INTERFACE, 0,
313 				      jacks[i].unitid << 8, buf, 3);
314 		up_read(&mixer->chip->shutdown_rwsem);
315 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
316 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
317 		else
318 			snd_iprintf(buffer, "?\n");
319 	}
320 }
321 
snd_xonar_u1_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)322 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
323 				   struct snd_ctl_elem_value *ucontrol)
324 {
325 	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
326 
327 	ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
328 	return 0;
329 }
330 
snd_xonar_u1_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)331 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
332 				   struct snd_ctl_elem_value *ucontrol)
333 {
334 	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
335 	u8 old_status, new_status;
336 	int err, changed;
337 
338 	old_status = mixer->xonar_u1_status;
339 	if (ucontrol->value.integer.value[0])
340 		new_status = old_status | 0x02;
341 	else
342 		new_status = old_status & ~0x02;
343 	changed = new_status != old_status;
344 	down_read(&mixer->chip->shutdown_rwsem);
345 	if (mixer->chip->shutdown)
346 		err = -ENODEV;
347 	else
348 		err = snd_usb_ctl_msg(mixer->chip->dev,
349 			      usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
350 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
351 			      50, 0, &new_status, 1);
352 	up_read(&mixer->chip->shutdown_rwsem);
353 	if (err < 0)
354 		return err;
355 	mixer->xonar_u1_status = new_status;
356 	return changed;
357 }
358 
359 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
360 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
361 	.name = "Digital Playback Switch",
362 	.info = snd_ctl_boolean_mono_info,
363 	.get = snd_xonar_u1_switch_get,
364 	.put = snd_xonar_u1_switch_put,
365 };
366 
snd_xonar_u1_controls_create(struct usb_mixer_interface * mixer)367 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
368 {
369 	int err;
370 
371 	err = snd_ctl_add(mixer->chip->card,
372 			  snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
373 	if (err < 0)
374 		return err;
375 	mixer->xonar_u1_status = 0x05;
376 	return 0;
377 }
378 
379 /* Native Instruments device quirks */
380 
381 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
382 
snd_nativeinstruments_control_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)383 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
384 					     struct snd_ctl_elem_value *ucontrol)
385 {
386 	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
387 	struct usb_device *dev = mixer->chip->dev;
388 	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
389 	u16 wIndex = kcontrol->private_value & 0xffff;
390 	u8 tmp;
391 	int ret;
392 
393 	down_read(&mixer->chip->shutdown_rwsem);
394 	if (mixer->chip->shutdown)
395 		ret = -ENODEV;
396 	else
397 		ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
398 				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
399 				  0, wIndex,
400 				  &tmp, sizeof(tmp), 1000);
401 	up_read(&mixer->chip->shutdown_rwsem);
402 
403 	if (ret < 0) {
404 		snd_printk(KERN_ERR
405 			   "unable to issue vendor read request (ret = %d)", ret);
406 		return ret;
407 	}
408 
409 	ucontrol->value.integer.value[0] = tmp;
410 
411 	return 0;
412 }
413 
snd_nativeinstruments_control_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)414 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
415 					     struct snd_ctl_elem_value *ucontrol)
416 {
417 	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
418 	struct usb_device *dev = mixer->chip->dev;
419 	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
420 	u16 wIndex = kcontrol->private_value & 0xffff;
421 	u16 wValue = ucontrol->value.integer.value[0];
422 	int ret;
423 
424 	down_read(&mixer->chip->shutdown_rwsem);
425 	if (mixer->chip->shutdown)
426 		ret = -ENODEV;
427 	else
428 		ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
429 				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
430 				  wValue, wIndex,
431 				  NULL, 0, 1000);
432 	up_read(&mixer->chip->shutdown_rwsem);
433 
434 	if (ret < 0) {
435 		snd_printk(KERN_ERR
436 			   "unable to issue vendor write request (ret = %d)", ret);
437 		return ret;
438 	}
439 
440 	return 0;
441 }
442 
443 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
444 	{
445 		.name = "Direct Thru Channel A",
446 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
447 	},
448 	{
449 		.name = "Direct Thru Channel B",
450 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
451 	},
452 	{
453 		.name = "Phono Input Channel A",
454 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
455 	},
456 	{
457 		.name = "Phono Input Channel B",
458 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
459 	},
460 };
461 
462 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
463 	{
464 		.name = "Direct Thru Channel A",
465 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
466 	},
467 	{
468 		.name = "Direct Thru Channel B",
469 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
470 	},
471 	{
472 		.name = "Direct Thru Channel C",
473 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
474 	},
475 	{
476 		.name = "Direct Thru Channel D",
477 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
478 	},
479 	{
480 		.name = "Phono Input Channel A",
481 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
482 	},
483 	{
484 		.name = "Phono Input Channel B",
485 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
486 	},
487 	{
488 		.name = "Phono Input Channel C",
489 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
490 	},
491 	{
492 		.name = "Phono Input Channel D",
493 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
494 	},
495 };
496 
snd_nativeinstruments_create_mixer(struct usb_mixer_interface * mixer,const struct snd_kcontrol_new * kc,unsigned int count)497 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
498 					      const struct snd_kcontrol_new *kc,
499 					      unsigned int count)
500 {
501 	int i, err = 0;
502 	struct snd_kcontrol_new template = {
503 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
504 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
505 		.get = snd_nativeinstruments_control_get,
506 		.put = snd_nativeinstruments_control_put,
507 		.info = snd_ctl_boolean_mono_info,
508 	};
509 
510 	for (i = 0; i < count; i++) {
511 		struct snd_kcontrol *c;
512 
513 		template.name = kc[i].name;
514 		template.private_value = kc[i].private_value;
515 
516 		c = snd_ctl_new1(&template, mixer);
517 		err = snd_ctl_add(mixer->chip->card, c);
518 
519 		if (err < 0)
520 			break;
521 	}
522 
523 	return err;
524 }
525 
526 /* M-Audio FastTrack Ultra quirks */
527 
528 /* private_free callback */
usb_mixer_elem_free(struct snd_kcontrol * kctl)529 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
530 {
531 	kfree(kctl->private_data);
532 	kctl->private_data = NULL;
533 }
534 
snd_maudio_ftu_create_ctl(struct usb_mixer_interface * mixer,int in,int out,const char * name)535 static int snd_maudio_ftu_create_ctl(struct usb_mixer_interface *mixer,
536 				     int in, int out, const char *name)
537 {
538 	struct usb_mixer_elem_info *cval;
539 	struct snd_kcontrol *kctl;
540 
541 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
542 	if (!cval)
543 		return -ENOMEM;
544 
545 	cval->id = 5;
546 	cval->mixer = mixer;
547 	cval->val_type = USB_MIXER_S16;
548 	cval->channels = 1;
549 	cval->control = out + 1;
550 	cval->cmask = 1 << in;
551 
552 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
553 	if (!kctl) {
554 		kfree(cval);
555 		return -ENOMEM;
556 	}
557 
558 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
559 	kctl->private_free = usb_mixer_elem_free;
560 	return snd_usb_mixer_add_control(mixer, kctl);
561 }
562 
snd_maudio_ftu_create_mixer(struct usb_mixer_interface * mixer)563 static int snd_maudio_ftu_create_mixer(struct usb_mixer_interface *mixer)
564 {
565 	char name[64];
566 	int in, out, err;
567 
568 	for (out = 0; out < 8; out++) {
569 		for (in = 0; in < 8; in++) {
570 			snprintf(name, sizeof(name),
571 				 "AIn%d - Out%d Capture Volume", in  + 1, out + 1);
572 			err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
573 			if (err < 0)
574 				return err;
575 		}
576 
577 		for (in = 8; in < 16; in++) {
578 			snprintf(name, sizeof(name),
579 				 "DIn%d - Out%d Playback Volume", in - 7, out + 1);
580 			err = snd_maudio_ftu_create_ctl(mixer, in, out, name);
581 			if (err < 0)
582 				return err;
583 		}
584 	}
585 
586 	return 0;
587 }
588 
snd_emuusb_set_samplerate(struct snd_usb_audio * chip,unsigned char samplerate_id)589 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
590 			       unsigned char samplerate_id)
591 {
592 	struct usb_mixer_interface *mixer;
593 	struct usb_mixer_elem_info *cval;
594 	int unitid = 12; /* SamleRate ExtensionUnit ID */
595 
596 	list_for_each_entry(mixer, &chip->mixer_list, list) {
597 		cval = mixer->id_elems[unitid];
598 		if (cval) {
599 			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
600 						    cval->control << 8,
601 						    samplerate_id);
602 			snd_usb_mixer_notify_id(mixer, unitid);
603 		}
604 		break;
605 	}
606 }
607 
snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface * mixer)608 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
609 {
610 	int err = 0;
611 	struct snd_info_entry *entry;
612 
613 	if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
614 		return err;
615 
616 	switch (mixer->chip->usb_id) {
617 	case USB_ID(0x041e, 0x3020):
618 	case USB_ID(0x041e, 0x3040):
619 	case USB_ID(0x041e, 0x3042):
620 	case USB_ID(0x041e, 0x30df):
621 	case USB_ID(0x041e, 0x3048):
622 		err = snd_audigy2nx_controls_create(mixer);
623 		if (err < 0)
624 			break;
625 		if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
626 			snd_info_set_text_ops(entry, mixer,
627 					      snd_audigy2nx_proc_read);
628 		break;
629 
630 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
631 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
632 		err = snd_maudio_ftu_create_mixer(mixer);
633 		break;
634 
635 	case USB_ID(0x0b05, 0x1739):
636 	case USB_ID(0x0b05, 0x1743):
637 		err = snd_xonar_u1_controls_create(mixer);
638 		break;
639 
640 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
641 		err = snd_nativeinstruments_create_mixer(mixer,
642 				snd_nativeinstruments_ta6_mixers,
643 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
644 		break;
645 
646 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
647 		err = snd_nativeinstruments_create_mixer(mixer,
648 				snd_nativeinstruments_ta10_mixers,
649 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
650 		break;
651 	}
652 
653 	return err;
654 }
655 
snd_usb_mixer_rc_memory_change(struct usb_mixer_interface * mixer,int unitid)656 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
657 				    int unitid)
658 {
659 	if (!mixer->rc_cfg)
660 		return;
661 	/* unit ids specific to Extigy/Audigy 2 NX: */
662 	switch (unitid) {
663 	case 0: /* remote control */
664 		mixer->rc_urb->dev = mixer->chip->dev;
665 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
666 		break;
667 	case 4: /* digital in jack */
668 	case 7: /* line in jacks */
669 	case 19: /* speaker out jacks */
670 	case 20: /* headphones out jack */
671 		break;
672 	/* live24ext: 4 = line-in jack */
673 	case 3:	/* hp-out jack (may actuate Mute) */
674 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
675 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
676 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
677 		break;
678 	default:
679 		snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
680 		break;
681 	}
682 }
683 
684