Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 *   This program is free software; you can redistribute it and/or modify
  3 *   it under the terms of the GNU General Public License as published by
  4 *   the Free Software Foundation; either version 2 of the License, or
  5 *   (at your option) any later version.
  6 *
  7 *   This program is distributed in the hope that it will be useful,
  8 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  9 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10 *   GNU General Public License for more details.
 11 *
 12 *   You should have received a copy of the GNU General Public License
 13 *   along with this program; if not, write to the Free Software
 14 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 15 */
 16
 17#include <linux/init.h>
 18#include <linux/slab.h>
 19#include <linux/usb.h>
 20#include <linux/usb/audio.h>
 21
 22#include <sound/control.h>
 23#include <sound/core.h>
 24#include <sound/info.h>
 25#include <sound/pcm.h>
 26
 27#include "usbaudio.h"
 28#include "card.h"
 29#include "mixer.h"
 30#include "mixer_quirks.h"
 31#include "midi.h"
 32#include "quirks.h"
 33#include "helper.h"
 34#include "endpoint.h"
 35#include "pcm.h"
 36#include "clock.h"
 
 37
 38/*
 39 * handle the quirks for the contained interfaces
 40 */
 41static int create_composite_quirk(struct snd_usb_audio *chip,
 42				  struct usb_interface *iface,
 43				  struct usb_driver *driver,
 44				  const struct snd_usb_audio_quirk *quirk)
 45{
 46	int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
 47	int err;
 48
 49	for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
 50		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
 51		if (!iface)
 52			continue;
 53		if (quirk->ifnum != probed_ifnum &&
 54		    usb_interface_claimed(iface))
 55			continue;
 56		err = snd_usb_create_quirk(chip, iface, driver, quirk);
 57		if (err < 0)
 58			return err;
 59		if (quirk->ifnum != probed_ifnum)
 60			usb_driver_claim_interface(driver, iface, (void *)-1L);
 61	}
 62	return 0;
 63}
 64
 65static int ignore_interface_quirk(struct snd_usb_audio *chip,
 66				  struct usb_interface *iface,
 67				  struct usb_driver *driver,
 68				  const struct snd_usb_audio_quirk *quirk)
 69{
 70	return 0;
 71}
 72
 73
 74/*
 75 * Allow alignment on audio sub-slot (channel samples) rather than
 76 * on audio slots (audio frames)
 77 */
 78static int create_align_transfer_quirk(struct snd_usb_audio *chip,
 79				       struct usb_interface *iface,
 80				       struct usb_driver *driver,
 81				       const struct snd_usb_audio_quirk *quirk)
 82{
 83	chip->txfr_quirk = 1;
 84	return 1;	/* Continue with creating streams and mixer */
 85}
 86
 87static int create_any_midi_quirk(struct snd_usb_audio *chip,
 88				 struct usb_interface *intf,
 89				 struct usb_driver *driver,
 90				 const struct snd_usb_audio_quirk *quirk)
 91{
 92	return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
 93}
 94
 95/*
 96 * create a stream for an interface with proper descriptors
 97 */
 98static int create_standard_audio_quirk(struct snd_usb_audio *chip,
 99				       struct usb_interface *iface,
100				       struct usb_driver *driver,
101				       const struct snd_usb_audio_quirk *quirk)
102{
103	struct usb_host_interface *alts;
104	struct usb_interface_descriptor *altsd;
105	int err;
106
107	alts = &iface->altsetting[0];
108	altsd = get_iface_desc(alts);
109	err = snd_usb_parse_audio_endpoints(chip, altsd->bInterfaceNumber);
110	if (err < 0) {
111		snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
112			   altsd->bInterfaceNumber, err);
113		return err;
114	}
115	/* reset the current interface */
116	usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
117	return 0;
118}
119
120/*
121 * create a stream for an endpoint/altsetting without proper descriptors
122 */
123static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
124				     struct usb_interface *iface,
125				     struct usb_driver *driver,
126				     const struct snd_usb_audio_quirk *quirk)
127{
128	struct audioformat *fp;
129	struct usb_host_interface *alts;
130	int stream, err;
131	unsigned *rate_table = NULL;
132
133	fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
134	if (! fp) {
135		snd_printk(KERN_ERR "cannot memdup\n");
136		return -ENOMEM;
137	}
 
 
 
 
138	if (fp->nr_rates > 0) {
139		rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
 
140		if (!rate_table) {
141			kfree(fp);
142			return -ENOMEM;
143		}
144		memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
145		fp->rate_table = rate_table;
146	}
147
148	stream = (fp->endpoint & USB_DIR_IN)
149		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
150	err = snd_usb_add_audio_endpoint(chip, stream, fp);
151	if (err < 0) {
152		kfree(fp);
153		kfree(rate_table);
154		return err;
155	}
156	if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
157	    fp->altset_idx >= iface->num_altsetting) {
158		kfree(fp);
159		kfree(rate_table);
160		return -EINVAL;
161	}
162	alts = &iface->altsetting[fp->altset_idx];
163	fp->datainterval = snd_usb_parse_datainterval(chip, alts);
164	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
165	usb_set_interface(chip->dev, fp->iface, 0);
166	snd_usb_init_pitch(chip, fp->iface, alts, fp);
167	snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
168	return 0;
169}
170
171/*
172 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
173 * The only way to detect the sample rate is by looking at wMaxPacketSize.
174 */
175static int create_uaxx_quirk(struct snd_usb_audio *chip,
176			     struct usb_interface *iface,
177			     struct usb_driver *driver,
178			     const struct snd_usb_audio_quirk *quirk)
179{
180	static const struct audioformat ua_format = {
181		.formats = SNDRV_PCM_FMTBIT_S24_3LE,
182		.channels = 2,
183		.fmt_type = UAC_FORMAT_TYPE_I,
184		.altsetting = 1,
185		.altset_idx = 1,
186		.rates = SNDRV_PCM_RATE_CONTINUOUS,
187	};
188	struct usb_host_interface *alts;
189	struct usb_interface_descriptor *altsd;
190	struct audioformat *fp;
191	int stream, err;
192
193	/* both PCM and MIDI interfaces have 2 or more altsettings */
194	if (iface->num_altsetting < 2)
195		return -ENXIO;
196	alts = &iface->altsetting[1];
197	altsd = get_iface_desc(alts);
198
199	if (altsd->bNumEndpoints == 2) {
200		static const struct snd_usb_midi_endpoint_info ua700_ep = {
201			.out_cables = 0x0003,
202			.in_cables  = 0x0003
203		};
204		static const struct snd_usb_audio_quirk ua700_quirk = {
205			.type = QUIRK_MIDI_FIXED_ENDPOINT,
206			.data = &ua700_ep
207		};
208		static const struct snd_usb_midi_endpoint_info uaxx_ep = {
209			.out_cables = 0x0001,
210			.in_cables  = 0x0001
211		};
212		static const struct snd_usb_audio_quirk uaxx_quirk = {
213			.type = QUIRK_MIDI_FIXED_ENDPOINT,
214			.data = &uaxx_ep
215		};
216		const struct snd_usb_audio_quirk *quirk =
217			chip->usb_id == USB_ID(0x0582, 0x002b)
218			? &ua700_quirk : &uaxx_quirk;
219		return snd_usbmidi_create(chip->card, iface,
220					  &chip->midi_list, quirk);
221	}
222
223	if (altsd->bNumEndpoints != 1)
224		return -ENXIO;
225
226	fp = kmalloc(sizeof(*fp), GFP_KERNEL);
227	if (!fp)
228		return -ENOMEM;
229	memcpy(fp, &ua_format, sizeof(*fp));
230
231	fp->iface = altsd->bInterfaceNumber;
232	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
233	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
234	fp->datainterval = 0;
235	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
236
237	switch (fp->maxpacksize) {
238	case 0x120:
239		fp->rate_max = fp->rate_min = 44100;
240		break;
241	case 0x138:
242	case 0x140:
243		fp->rate_max = fp->rate_min = 48000;
244		break;
245	case 0x258:
246	case 0x260:
247		fp->rate_max = fp->rate_min = 96000;
248		break;
249	default:
250		snd_printk(KERN_ERR "unknown sample rate\n");
251		kfree(fp);
252		return -ENXIO;
253	}
254
255	stream = (fp->endpoint & USB_DIR_IN)
256		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
257	err = snd_usb_add_audio_endpoint(chip, stream, fp);
258	if (err < 0) {
259		kfree(fp);
260		return err;
261	}
262	usb_set_interface(chip->dev, fp->iface, 0);
263	return 0;
264}
265
266/*
267 * Create a standard mixer for the specified interface.
268 */
269static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
270				       struct usb_interface *iface,
271				       struct usb_driver *driver,
272				       const struct snd_usb_audio_quirk *quirk)
273{
274	if (quirk->ifnum < 0)
275		return 0;
276
277	return snd_usb_create_mixer(chip, quirk->ifnum, 0);
278}
279
280/*
281 * audio-interface quirks
282 *
283 * returns zero if no standard audio/MIDI parsing is needed.
284 * returns a positive value if standard audio/midi interfaces are parsed
285 * after this.
286 * returns a negative value at error.
287 */
288int snd_usb_create_quirk(struct snd_usb_audio *chip,
289			 struct usb_interface *iface,
290			 struct usb_driver *driver,
291			 const struct snd_usb_audio_quirk *quirk)
292{
293	typedef int (*quirk_func_t)(struct snd_usb_audio *,
294				    struct usb_interface *,
295				    struct usb_driver *,
296				    const struct snd_usb_audio_quirk *);
297	static const quirk_func_t quirk_funcs[] = {
298		[QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
299		[QUIRK_COMPOSITE] = create_composite_quirk,
300		[QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
301		[QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
302		[QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
303		[QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
304		[QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
305		[QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
306		[QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
307		[QUIRK_MIDI_CME] = create_any_midi_quirk,
308		[QUIRK_MIDI_AKAI] = create_any_midi_quirk,
 
309		[QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
310		[QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
311		[QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
312		[QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
313		[QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
314	};
315
316	if (quirk->type < QUIRK_TYPE_COUNT) {
317		return quirk_funcs[quirk->type](chip, iface, driver, quirk);
318	} else {
319		snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
320		return -ENXIO;
321	}
322}
323
324/*
325 * boot quirks
326 */
327
328#define EXTIGY_FIRMWARE_SIZE_OLD 794
329#define EXTIGY_FIRMWARE_SIZE_NEW 483
330
331static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
332{
333	struct usb_host_config *config = dev->actconfig;
334	int err;
335
336	if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
337	    le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
338		snd_printdd("sending Extigy boot sequence...\n");
339		/* Send message to force it to reconnect with full interface. */
340		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
341				      0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
342		if (err < 0) snd_printdd("error sending boot message: %d\n", err);
343		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
344				&dev->descriptor, sizeof(dev->descriptor));
345		config = dev->actconfig;
346		if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
347		err = usb_reset_configuration(dev);
348		if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
349		snd_printdd("extigy_boot: new boot length = %d\n",
350			    le16_to_cpu(get_cfg_desc(config)->wTotalLength));
351		return -ENODEV; /* quit this anyway */
352	}
353	return 0;
354}
355
356static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
357{
358	u8 buf = 1;
359
360	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
361			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
362			0, 0, &buf, 1, 1000);
363	if (buf == 0) {
364		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
365				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
366				1, 2000, NULL, 0, 1000);
367		return -ENODEV;
368	}
369	return 0;
370}
371
372static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
373{
374	int err;
375
376	if (dev->actconfig->desc.bConfigurationValue == 1) {
377		snd_printk(KERN_INFO "usb-audio: "
378			   "Fast Track Pro switching to config #2\n");
379		/* This function has to be available by the usb core module.
380		 * if it is not avialable the boot quirk has to be left out
381		 * and the configuration has to be set by udev or hotplug
382		 * rules
383		 */
384		err = usb_driver_set_configuration(dev, 2);
385		if (err < 0) {
386			snd_printdd("error usb_driver_set_configuration: %d\n",
387				    err);
388			return -ENODEV;
389		}
390	} else
391		snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
392
393	return 0;
394}
395
396/*
397 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
398 * documented in the device's data sheet.
399 */
400static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
401{
402	u8 buf[4];
403	buf[0] = 0x20;
404	buf[1] = value & 0xff;
405	buf[2] = (value >> 8) & 0xff;
406	buf[3] = reg;
407	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
408			       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
409			       0, 0, &buf, 4, 1000);
410}
411
412static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
413{
414	/*
415	 * Enable line-out driver mode, set headphone source to front
416	 * channels, enable stereo mic.
417	 */
418	return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
419}
420
421/*
422 * C-Media CM6206 is based on CM106 with two additional
423 * registers that are not documented in the data sheet.
424 * Values here are chosen based on sniffing USB traffic
425 * under Windows.
426 */
427static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
428{
429	int err  = 0, reg;
430	int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
431
432	for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
433		err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
434		if (err < 0)
435			return err;
436	}
437
438	return err;
439}
440
441/*
442 * This call will put the synth in "USB send" mode, i.e it will send MIDI
443 * messages through USB (this is disabled at startup). The synth will
444 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
445 * sign on its LCD. Values here are chosen based on sniffing USB traffic
446 * under Windows.
447 */
448static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
449{
450	int err, actual_length;
451
452	/* "midi send" enable */
453	static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
454
455	void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
456	if (!buf)
457		return -ENOMEM;
458	err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
459			ARRAY_SIZE(seq), &actual_length, 1000);
460	kfree(buf);
461	if (err < 0)
462		return err;
463
464	return 0;
465}
466
467/*
468 * Some sound cards from Native Instruments are in fact compliant to the USB
469 * audio standard of version 2 and other approved USB standards, even though
470 * they come up as vendor-specific device when first connected.
471 *
472 * However, they can be told to come up with a new set of descriptors
473 * upon their next enumeration, and the interfaces announced by the new
474 * descriptors will then be handled by the kernel's class drivers. As the
475 * product ID will also change, no further checks are required.
476 */
477
478static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
479{
480	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
481				  0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
482				  cpu_to_le16(1), 0, NULL, 0, 1000);
483
484	if (ret < 0)
485		return ret;
486
487	usb_reset_device(dev);
488
489	/* return -EAGAIN, so the creation of an audio interface for this
490	 * temporary device is aborted. The device will reconnect with a
491	 * new product ID */
492	return -EAGAIN;
493}
494
495/*
496 * Setup quirks
497 */
498#define MAUDIO_SET		0x01 /* parse device_setup */
499#define MAUDIO_SET_COMPATIBLE	0x80 /* use only "win-compatible" interfaces */
500#define MAUDIO_SET_DTS		0x02 /* enable DTS Digital Output */
501#define MAUDIO_SET_96K		0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
502#define MAUDIO_SET_24B		0x08 /* 24bits sample if set, 16bits otherwise */
503#define MAUDIO_SET_DI		0x10 /* enable Digital Input */
504#define MAUDIO_SET_MASK		0x1f /* bit mask for setup value */
505#define MAUDIO_SET_24B_48K_DI	 0x19 /* 24bits+48KHz+Digital Input */
506#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
507#define MAUDIO_SET_16B_48K_DI	 0x11 /* 16bits+48KHz+Digital Input */
508#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
509
510static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
511				      int iface, int altno)
512{
513	/* Reset ALL ifaces to 0 altsetting.
514	 * Call it for every possible altsetting of every interface.
515	 */
516	usb_set_interface(chip->dev, iface, 0);
517	if (chip->setup & MAUDIO_SET) {
518		if (chip->setup & MAUDIO_SET_COMPATIBLE) {
519			if (iface != 1 && iface != 2)
520				return 1; /* skip all interfaces but 1 and 2 */
521		} else {
522			unsigned int mask;
523			if (iface == 1 || iface == 2)
524				return 1; /* skip interfaces 1 and 2 */
525			if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
526				return 1; /* skip this altsetting */
527			mask = chip->setup & MAUDIO_SET_MASK;
528			if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
529				return 1; /* skip this altsetting */
530			if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
531				return 1; /* skip this altsetting */
532			if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
533				return 1; /* skip this altsetting */
534		}
535	}
536	snd_printdd(KERN_INFO
537		    "using altsetting %d for interface %d config %d\n",
538		    altno, iface, chip->setup);
539	return 0; /* keep this altsetting */
540}
541
542static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
543					 int iface,
544					 int altno)
545{
546	/* Reset ALL ifaces to 0 altsetting.
547	 * Call it for every possible altsetting of every interface.
548	 */
549	usb_set_interface(chip->dev, iface, 0);
550
551	if (chip->setup & MAUDIO_SET) {
552		unsigned int mask;
553		if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
554			return 1; /* skip this altsetting */
555		if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
556			return 1; /* skip this altsetting */
557		mask = chip->setup & MAUDIO_SET_MASK;
558		if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
559			return 1; /* skip this altsetting */
560		if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
561			return 1; /* skip this altsetting */
562		if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
563			return 1; /* skip this altsetting */
564		if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
565			return 1; /* skip this altsetting */
566	}
567
568	return 0; /* keep this altsetting */
569}
570
571
572static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
573					   int iface, int altno)
574{
575	/* Reset ALL ifaces to 0 altsetting.
576	 * Call it for every possible altsetting of every interface.
577	 */
578	usb_set_interface(chip->dev, iface, 0);
579
580	/* possible configuration where both inputs and only one output is
581	 *used is not supported by the current setup
582	 */
583	if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
584		if (chip->setup & MAUDIO_SET_96K) {
585			if (altno != 3 && altno != 6)
586				return 1;
587		} else if (chip->setup & MAUDIO_SET_DI) {
588			if (iface == 4)
589				return 1; /* no analog input */
590			if (altno != 2 && altno != 5)
591				return 1; /* enable only altsets 2 and 5 */
592		} else {
593			if (iface == 5)
594				return 1; /* disable digialt input */
595			if (altno != 2 && altno != 5)
596				return 1; /* enalbe only altsets 2 and 5 */
597		}
598	} else {
599		/* keep only 16-Bit mode */
600		if (altno != 1)
601			return 1;
602	}
603
604	snd_printdd(KERN_INFO
605		    "using altsetting %d for interface %d config %d\n",
606		    altno, iface, chip->setup);
607	return 0; /* keep this altsetting */
608}
609
610int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
611				  int iface,
612				  int altno)
613{
614	/* audiophile usb: skip altsets incompatible with device_setup */
615	if (chip->usb_id == USB_ID(0x0763, 0x2003))
616		return audiophile_skip_setting_quirk(chip, iface, altno);
617	/* quattro usb: skip altsets incompatible with device_setup */
618	if (chip->usb_id == USB_ID(0x0763, 0x2001))
619		return quattro_skip_setting_quirk(chip, iface, altno);
620	/* fasttrackpro usb: skip altsets incompatible with device_setup */
621	if (chip->usb_id == USB_ID(0x0763, 0x2012))
622		return fasttrackpro_skip_setting_quirk(chip, iface, altno);
623
624	return 0;
625}
626
627int snd_usb_apply_boot_quirk(struct usb_device *dev,
628			     struct usb_interface *intf,
629			     const struct snd_usb_audio_quirk *quirk)
630{
631	u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
632			le16_to_cpu(dev->descriptor.idProduct));
633
634	switch (id) {
635	case USB_ID(0x041e, 0x3000):
636		/* SB Extigy needs special boot-up sequence */
637		/* if more models come, this will go to the quirk list. */
638		return snd_usb_extigy_boot_quirk(dev, intf);
639
640	case USB_ID(0x041e, 0x3020):
641		/* SB Audigy 2 NX needs its own boot-up magic, too */
642		return snd_usb_audigy2nx_boot_quirk(dev);
643
644	case USB_ID(0x10f5, 0x0200):
645		/* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
646		return snd_usb_cm106_boot_quirk(dev);
647
648	case USB_ID(0x0d8c, 0x0102):
649		/* C-Media CM6206 / CM106-Like Sound Device */
650	case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
651		return snd_usb_cm6206_boot_quirk(dev);
652
653	case USB_ID(0x133e, 0x0815):
654		/* Access Music VirusTI Desktop */
655		return snd_usb_accessmusic_boot_quirk(dev);
656
657	case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
658	case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
659	case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
660		return snd_usb_nativeinstruments_boot_quirk(dev);
661	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
662		return snd_usb_fasttrackpro_boot_quirk(dev);
663	}
664
665	return 0;
666}
667
668/*
669 * check if the device uses big-endian samples
670 */
671int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
672{
673	/* it depends on altsetting wether the device is big-endian or not */
674	switch (chip->usb_id) {
675	case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
676		if (fp->altsetting == 2 || fp->altsetting == 3 ||
677			fp->altsetting == 5 || fp->altsetting == 6)
678			return 1;
679		break;
680	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
681		if (chip->setup == 0x00 ||
682			fp->altsetting == 1 || fp->altsetting == 2 ||
683			fp->altsetting == 3)
684			return 1;
685		break;
686	case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
687		if (fp->altsetting == 2 || fp->altsetting == 3 ||
688			fp->altsetting == 5 || fp->altsetting == 6)
689			return 1;
690		break;
691	}
692	return 0;
693}
694
695/*
696 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
697 * not for interface.
698 */
699
700enum {
701	EMU_QUIRK_SR_44100HZ = 0,
702	EMU_QUIRK_SR_48000HZ,
703	EMU_QUIRK_SR_88200HZ,
704	EMU_QUIRK_SR_96000HZ,
705	EMU_QUIRK_SR_176400HZ,
706	EMU_QUIRK_SR_192000HZ
707};
708
709static void set_format_emu_quirk(struct snd_usb_substream *subs,
710				 struct audioformat *fmt)
711{
712	unsigned char emu_samplerate_id = 0;
713
714	/* When capture is active
715	 * sample rate shouldn't be changed
716	 * by playback substream
717	 */
718	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
719		if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
720			return;
721	}
722
723	switch (fmt->rate_min) {
724	case 48000:
725		emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
726		break;
727	case 88200:
728		emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
729		break;
730	case 96000:
731		emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
732		break;
733	case 176400:
734		emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
735		break;
736	case 192000:
737		emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
738		break;
739	default:
740		emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
741		break;
742	}
743	snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
744}
745
746void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
747			      struct audioformat *fmt)
748{
749	switch (subs->stream->chip->usb_id) {
750	case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
751	case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
752	case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
753	case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
754		set_format_emu_quirk(subs, fmt);
755		break;
756	}
757}
758
v3.5.6
  1/*
  2 *   This program is free software; you can redistribute it and/or modify
  3 *   it under the terms of the GNU General Public License as published by
  4 *   the Free Software Foundation; either version 2 of the License, or
  5 *   (at your option) any later version.
  6 *
  7 *   This program is distributed in the hope that it will be useful,
  8 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  9 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10 *   GNU General Public License for more details.
 11 *
 12 *   You should have received a copy of the GNU General Public License
 13 *   along with this program; if not, write to the Free Software
 14 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 15 */
 16
 17#include <linux/init.h>
 18#include <linux/slab.h>
 19#include <linux/usb.h>
 20#include <linux/usb/audio.h>
 21
 22#include <sound/control.h>
 23#include <sound/core.h>
 24#include <sound/info.h>
 25#include <sound/pcm.h>
 26
 27#include "usbaudio.h"
 28#include "card.h"
 29#include "mixer.h"
 30#include "mixer_quirks.h"
 31#include "midi.h"
 32#include "quirks.h"
 33#include "helper.h"
 34#include "endpoint.h"
 35#include "pcm.h"
 36#include "clock.h"
 37#include "stream.h"
 38
 39/*
 40 * handle the quirks for the contained interfaces
 41 */
 42static int create_composite_quirk(struct snd_usb_audio *chip,
 43				  struct usb_interface *iface,
 44				  struct usb_driver *driver,
 45				  const struct snd_usb_audio_quirk *quirk)
 46{
 47	int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
 48	int err;
 49
 50	for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
 51		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
 52		if (!iface)
 53			continue;
 54		if (quirk->ifnum != probed_ifnum &&
 55		    usb_interface_claimed(iface))
 56			continue;
 57		err = snd_usb_create_quirk(chip, iface, driver, quirk);
 58		if (err < 0)
 59			return err;
 60		if (quirk->ifnum != probed_ifnum)
 61			usb_driver_claim_interface(driver, iface, (void *)-1L);
 62	}
 63	return 0;
 64}
 65
 66static int ignore_interface_quirk(struct snd_usb_audio *chip,
 67				  struct usb_interface *iface,
 68				  struct usb_driver *driver,
 69				  const struct snd_usb_audio_quirk *quirk)
 70{
 71	return 0;
 72}
 73
 74
 75/*
 76 * Allow alignment on audio sub-slot (channel samples) rather than
 77 * on audio slots (audio frames)
 78 */
 79static int create_align_transfer_quirk(struct snd_usb_audio *chip,
 80				       struct usb_interface *iface,
 81				       struct usb_driver *driver,
 82				       const struct snd_usb_audio_quirk *quirk)
 83{
 84	chip->txfr_quirk = 1;
 85	return 1;	/* Continue with creating streams and mixer */
 86}
 87
 88static int create_any_midi_quirk(struct snd_usb_audio *chip,
 89				 struct usb_interface *intf,
 90				 struct usb_driver *driver,
 91				 const struct snd_usb_audio_quirk *quirk)
 92{
 93	return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
 94}
 95
 96/*
 97 * create a stream for an interface with proper descriptors
 98 */
 99static int create_standard_audio_quirk(struct snd_usb_audio *chip,
100				       struct usb_interface *iface,
101				       struct usb_driver *driver,
102				       const struct snd_usb_audio_quirk *quirk)
103{
104	struct usb_host_interface *alts;
105	struct usb_interface_descriptor *altsd;
106	int err;
107
108	alts = &iface->altsetting[0];
109	altsd = get_iface_desc(alts);
110	err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
111	if (err < 0) {
112		snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
113			   altsd->bInterfaceNumber, err);
114		return err;
115	}
116	/* reset the current interface */
117	usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
118	return 0;
119}
120
121/*
122 * create a stream for an endpoint/altsetting without proper descriptors
123 */
124static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
125				     struct usb_interface *iface,
126				     struct usb_driver *driver,
127				     const struct snd_usb_audio_quirk *quirk)
128{
129	struct audioformat *fp;
130	struct usb_host_interface *alts;
131	int stream, err;
132	unsigned *rate_table = NULL;
133
134	fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
135	if (!fp) {
136		snd_printk(KERN_ERR "cannot memdup\n");
137		return -ENOMEM;
138	}
139	if (fp->nr_rates > MAX_NR_RATES) {
140		kfree(fp);
141		return -EINVAL;
142	}
143	if (fp->nr_rates > 0) {
144		rate_table = kmemdup(fp->rate_table,
145				     sizeof(int) * fp->nr_rates, GFP_KERNEL);
146		if (!rate_table) {
147			kfree(fp);
148			return -ENOMEM;
149		}
 
150		fp->rate_table = rate_table;
151	}
152
153	stream = (fp->endpoint & USB_DIR_IN)
154		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
155	err = snd_usb_add_audio_stream(chip, stream, fp);
156	if (err < 0) {
157		kfree(fp);
158		kfree(rate_table);
159		return err;
160	}
161	if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
162	    fp->altset_idx >= iface->num_altsetting) {
163		kfree(fp);
164		kfree(rate_table);
165		return -EINVAL;
166	}
167	alts = &iface->altsetting[fp->altset_idx];
168	fp->datainterval = snd_usb_parse_datainterval(chip, alts);
169	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
170	usb_set_interface(chip->dev, fp->iface, 0);
171	snd_usb_init_pitch(chip, fp->iface, alts, fp);
172	snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
173	return 0;
174}
175
176/*
177 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
178 * The only way to detect the sample rate is by looking at wMaxPacketSize.
179 */
180static int create_uaxx_quirk(struct snd_usb_audio *chip,
181			     struct usb_interface *iface,
182			     struct usb_driver *driver,
183			     const struct snd_usb_audio_quirk *quirk)
184{
185	static const struct audioformat ua_format = {
186		.formats = SNDRV_PCM_FMTBIT_S24_3LE,
187		.channels = 2,
188		.fmt_type = UAC_FORMAT_TYPE_I,
189		.altsetting = 1,
190		.altset_idx = 1,
191		.rates = SNDRV_PCM_RATE_CONTINUOUS,
192	};
193	struct usb_host_interface *alts;
194	struct usb_interface_descriptor *altsd;
195	struct audioformat *fp;
196	int stream, err;
197
198	/* both PCM and MIDI interfaces have 2 or more altsettings */
199	if (iface->num_altsetting < 2)
200		return -ENXIO;
201	alts = &iface->altsetting[1];
202	altsd = get_iface_desc(alts);
203
204	if (altsd->bNumEndpoints == 2) {
205		static const struct snd_usb_midi_endpoint_info ua700_ep = {
206			.out_cables = 0x0003,
207			.in_cables  = 0x0003
208		};
209		static const struct snd_usb_audio_quirk ua700_quirk = {
210			.type = QUIRK_MIDI_FIXED_ENDPOINT,
211			.data = &ua700_ep
212		};
213		static const struct snd_usb_midi_endpoint_info uaxx_ep = {
214			.out_cables = 0x0001,
215			.in_cables  = 0x0001
216		};
217		static const struct snd_usb_audio_quirk uaxx_quirk = {
218			.type = QUIRK_MIDI_FIXED_ENDPOINT,
219			.data = &uaxx_ep
220		};
221		const struct snd_usb_audio_quirk *quirk =
222			chip->usb_id == USB_ID(0x0582, 0x002b)
223			? &ua700_quirk : &uaxx_quirk;
224		return snd_usbmidi_create(chip->card, iface,
225					  &chip->midi_list, quirk);
226	}
227
228	if (altsd->bNumEndpoints != 1)
229		return -ENXIO;
230
231	fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
232	if (!fp)
233		return -ENOMEM;
 
234
235	fp->iface = altsd->bInterfaceNumber;
236	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
237	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
238	fp->datainterval = 0;
239	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
240
241	switch (fp->maxpacksize) {
242	case 0x120:
243		fp->rate_max = fp->rate_min = 44100;
244		break;
245	case 0x138:
246	case 0x140:
247		fp->rate_max = fp->rate_min = 48000;
248		break;
249	case 0x258:
250	case 0x260:
251		fp->rate_max = fp->rate_min = 96000;
252		break;
253	default:
254		snd_printk(KERN_ERR "unknown sample rate\n");
255		kfree(fp);
256		return -ENXIO;
257	}
258
259	stream = (fp->endpoint & USB_DIR_IN)
260		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
261	err = snd_usb_add_audio_stream(chip, stream, fp);
262	if (err < 0) {
263		kfree(fp);
264		return err;
265	}
266	usb_set_interface(chip->dev, fp->iface, 0);
267	return 0;
268}
269
270/*
271 * Create a standard mixer for the specified interface.
272 */
273static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
274				       struct usb_interface *iface,
275				       struct usb_driver *driver,
276				       const struct snd_usb_audio_quirk *quirk)
277{
278	if (quirk->ifnum < 0)
279		return 0;
280
281	return snd_usb_create_mixer(chip, quirk->ifnum, 0);
282}
283
284/*
285 * audio-interface quirks
286 *
287 * returns zero if no standard audio/MIDI parsing is needed.
288 * returns a positive value if standard audio/midi interfaces are parsed
289 * after this.
290 * returns a negative value at error.
291 */
292int snd_usb_create_quirk(struct snd_usb_audio *chip,
293			 struct usb_interface *iface,
294			 struct usb_driver *driver,
295			 const struct snd_usb_audio_quirk *quirk)
296{
297	typedef int (*quirk_func_t)(struct snd_usb_audio *,
298				    struct usb_interface *,
299				    struct usb_driver *,
300				    const struct snd_usb_audio_quirk *);
301	static const quirk_func_t quirk_funcs[] = {
302		[QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
303		[QUIRK_COMPOSITE] = create_composite_quirk,
304		[QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
305		[QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
306		[QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
307		[QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
308		[QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
309		[QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
310		[QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
311		[QUIRK_MIDI_CME] = create_any_midi_quirk,
312		[QUIRK_MIDI_AKAI] = create_any_midi_quirk,
313		[QUIRK_MIDI_FTDI] = create_any_midi_quirk,
314		[QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
315		[QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
316		[QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
317		[QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
318		[QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
319	};
320
321	if (quirk->type < QUIRK_TYPE_COUNT) {
322		return quirk_funcs[quirk->type](chip, iface, driver, quirk);
323	} else {
324		snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
325		return -ENXIO;
326	}
327}
328
329/*
330 * boot quirks
331 */
332
333#define EXTIGY_FIRMWARE_SIZE_OLD 794
334#define EXTIGY_FIRMWARE_SIZE_NEW 483
335
336static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
337{
338	struct usb_host_config *config = dev->actconfig;
339	int err;
340
341	if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
342	    le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
343		snd_printdd("sending Extigy boot sequence...\n");
344		/* Send message to force it to reconnect with full interface. */
345		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
346				      0x10, 0x43, 0x0001, 0x000a, NULL, 0);
347		if (err < 0) snd_printdd("error sending boot message: %d\n", err);
348		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
349				&dev->descriptor, sizeof(dev->descriptor));
350		config = dev->actconfig;
351		if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
352		err = usb_reset_configuration(dev);
353		if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
354		snd_printdd("extigy_boot: new boot length = %d\n",
355			    le16_to_cpu(get_cfg_desc(config)->wTotalLength));
356		return -ENODEV; /* quit this anyway */
357	}
358	return 0;
359}
360
361static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
362{
363	u8 buf = 1;
364
365	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
366			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
367			0, 0, &buf, 1);
368	if (buf == 0) {
369		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
370				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
371				1, 2000, NULL, 0);
372		return -ENODEV;
373	}
374	return 0;
375}
376
377static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
378{
379	int err;
380
381	if (dev->actconfig->desc.bConfigurationValue == 1) {
382		snd_printk(KERN_INFO "usb-audio: "
383			   "Fast Track Pro switching to config #2\n");
384		/* This function has to be available by the usb core module.
385		 * if it is not avialable the boot quirk has to be left out
386		 * and the configuration has to be set by udev or hotplug
387		 * rules
388		 */
389		err = usb_driver_set_configuration(dev, 2);
390		if (err < 0) {
391			snd_printdd("error usb_driver_set_configuration: %d\n",
392				    err);
393			return -ENODEV;
394		}
395	} else
396		snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
397
398	return 0;
399}
400
401/*
402 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
403 * documented in the device's data sheet.
404 */
405static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
406{
407	u8 buf[4];
408	buf[0] = 0x20;
409	buf[1] = value & 0xff;
410	buf[2] = (value >> 8) & 0xff;
411	buf[3] = reg;
412	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
413			       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
414			       0, 0, &buf, 4);
415}
416
417static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
418{
419	/*
420	 * Enable line-out driver mode, set headphone source to front
421	 * channels, enable stereo mic.
422	 */
423	return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
424}
425
426/*
427 * C-Media CM6206 is based on CM106 with two additional
428 * registers that are not documented in the data sheet.
429 * Values here are chosen based on sniffing USB traffic
430 * under Windows.
431 */
432static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
433{
434	int err  = 0, reg;
435	int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
436
437	for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
438		err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
439		if (err < 0)
440			return err;
441	}
442
443	return err;
444}
445
446/*
447 * This call will put the synth in "USB send" mode, i.e it will send MIDI
448 * messages through USB (this is disabled at startup). The synth will
449 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
450 * sign on its LCD. Values here are chosen based on sniffing USB traffic
451 * under Windows.
452 */
453static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
454{
455	int err, actual_length;
456
457	/* "midi send" enable */
458	static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
459
460	void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
461	if (!buf)
462		return -ENOMEM;
463	err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
464			ARRAY_SIZE(seq), &actual_length, 1000);
465	kfree(buf);
466	if (err < 0)
467		return err;
468
469	return 0;
470}
471
472/*
473 * Some sound cards from Native Instruments are in fact compliant to the USB
474 * audio standard of version 2 and other approved USB standards, even though
475 * they come up as vendor-specific device when first connected.
476 *
477 * However, they can be told to come up with a new set of descriptors
478 * upon their next enumeration, and the interfaces announced by the new
479 * descriptors will then be handled by the kernel's class drivers. As the
480 * product ID will also change, no further checks are required.
481 */
482
483static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
484{
485	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
486				  0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
487				  cpu_to_le16(1), 0, NULL, 0, 1000);
488
489	if (ret < 0)
490		return ret;
491
492	usb_reset_device(dev);
493
494	/* return -EAGAIN, so the creation of an audio interface for this
495	 * temporary device is aborted. The device will reconnect with a
496	 * new product ID */
497	return -EAGAIN;
498}
499
500/*
501 * Setup quirks
502 */
503#define MAUDIO_SET		0x01 /* parse device_setup */
504#define MAUDIO_SET_COMPATIBLE	0x80 /* use only "win-compatible" interfaces */
505#define MAUDIO_SET_DTS		0x02 /* enable DTS Digital Output */
506#define MAUDIO_SET_96K		0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
507#define MAUDIO_SET_24B		0x08 /* 24bits sample if set, 16bits otherwise */
508#define MAUDIO_SET_DI		0x10 /* enable Digital Input */
509#define MAUDIO_SET_MASK		0x1f /* bit mask for setup value */
510#define MAUDIO_SET_24B_48K_DI	 0x19 /* 24bits+48KHz+Digital Input */
511#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
512#define MAUDIO_SET_16B_48K_DI	 0x11 /* 16bits+48KHz+Digital Input */
513#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
514
515static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
516				      int iface, int altno)
517{
518	/* Reset ALL ifaces to 0 altsetting.
519	 * Call it for every possible altsetting of every interface.
520	 */
521	usb_set_interface(chip->dev, iface, 0);
522	if (chip->setup & MAUDIO_SET) {
523		if (chip->setup & MAUDIO_SET_COMPATIBLE) {
524			if (iface != 1 && iface != 2)
525				return 1; /* skip all interfaces but 1 and 2 */
526		} else {
527			unsigned int mask;
528			if (iface == 1 || iface == 2)
529				return 1; /* skip interfaces 1 and 2 */
530			if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
531				return 1; /* skip this altsetting */
532			mask = chip->setup & MAUDIO_SET_MASK;
533			if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
534				return 1; /* skip this altsetting */
535			if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
536				return 1; /* skip this altsetting */
537			if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
538				return 1; /* skip this altsetting */
539		}
540	}
541	snd_printdd(KERN_INFO
542		    "using altsetting %d for interface %d config %d\n",
543		    altno, iface, chip->setup);
544	return 0; /* keep this altsetting */
545}
546
547static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
548					 int iface,
549					 int altno)
550{
551	/* Reset ALL ifaces to 0 altsetting.
552	 * Call it for every possible altsetting of every interface.
553	 */
554	usb_set_interface(chip->dev, iface, 0);
555
556	if (chip->setup & MAUDIO_SET) {
557		unsigned int mask;
558		if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
559			return 1; /* skip this altsetting */
560		if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
561			return 1; /* skip this altsetting */
562		mask = chip->setup & MAUDIO_SET_MASK;
563		if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
564			return 1; /* skip this altsetting */
565		if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
566			return 1; /* skip this altsetting */
567		if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
568			return 1; /* skip this altsetting */
569		if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
570			return 1; /* skip this altsetting */
571	}
572
573	return 0; /* keep this altsetting */
574}
575
576
577static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
578					   int iface, int altno)
579{
580	/* Reset ALL ifaces to 0 altsetting.
581	 * Call it for every possible altsetting of every interface.
582	 */
583	usb_set_interface(chip->dev, iface, 0);
584
585	/* possible configuration where both inputs and only one output is
586	 *used is not supported by the current setup
587	 */
588	if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
589		if (chip->setup & MAUDIO_SET_96K) {
590			if (altno != 3 && altno != 6)
591				return 1;
592		} else if (chip->setup & MAUDIO_SET_DI) {
593			if (iface == 4)
594				return 1; /* no analog input */
595			if (altno != 2 && altno != 5)
596				return 1; /* enable only altsets 2 and 5 */
597		} else {
598			if (iface == 5)
599				return 1; /* disable digialt input */
600			if (altno != 2 && altno != 5)
601				return 1; /* enalbe only altsets 2 and 5 */
602		}
603	} else {
604		/* keep only 16-Bit mode */
605		if (altno != 1)
606			return 1;
607	}
608
609	snd_printdd(KERN_INFO
610		    "using altsetting %d for interface %d config %d\n",
611		    altno, iface, chip->setup);
612	return 0; /* keep this altsetting */
613}
614
615int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
616				  int iface,
617				  int altno)
618{
619	/* audiophile usb: skip altsets incompatible with device_setup */
620	if (chip->usb_id == USB_ID(0x0763, 0x2003))
621		return audiophile_skip_setting_quirk(chip, iface, altno);
622	/* quattro usb: skip altsets incompatible with device_setup */
623	if (chip->usb_id == USB_ID(0x0763, 0x2001))
624		return quattro_skip_setting_quirk(chip, iface, altno);
625	/* fasttrackpro usb: skip altsets incompatible with device_setup */
626	if (chip->usb_id == USB_ID(0x0763, 0x2012))
627		return fasttrackpro_skip_setting_quirk(chip, iface, altno);
628
629	return 0;
630}
631
632int snd_usb_apply_boot_quirk(struct usb_device *dev,
633			     struct usb_interface *intf,
634			     const struct snd_usb_audio_quirk *quirk)
635{
636	u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
637			le16_to_cpu(dev->descriptor.idProduct));
638
639	switch (id) {
640	case USB_ID(0x041e, 0x3000):
641		/* SB Extigy needs special boot-up sequence */
642		/* if more models come, this will go to the quirk list. */
643		return snd_usb_extigy_boot_quirk(dev, intf);
644
645	case USB_ID(0x041e, 0x3020):
646		/* SB Audigy 2 NX needs its own boot-up magic, too */
647		return snd_usb_audigy2nx_boot_quirk(dev);
648
649	case USB_ID(0x10f5, 0x0200):
650		/* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
651		return snd_usb_cm106_boot_quirk(dev);
652
653	case USB_ID(0x0d8c, 0x0102):
654		/* C-Media CM6206 / CM106-Like Sound Device */
655	case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
656		return snd_usb_cm6206_boot_quirk(dev);
657
658	case USB_ID(0x133e, 0x0815):
659		/* Access Music VirusTI Desktop */
660		return snd_usb_accessmusic_boot_quirk(dev);
661
662	case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
663	case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
664	case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
665		return snd_usb_nativeinstruments_boot_quirk(dev);
666	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
667		return snd_usb_fasttrackpro_boot_quirk(dev);
668	}
669
670	return 0;
671}
672
673/*
674 * check if the device uses big-endian samples
675 */
676int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
677{
678	/* it depends on altsetting wether the device is big-endian or not */
679	switch (chip->usb_id) {
680	case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
681		if (fp->altsetting == 2 || fp->altsetting == 3 ||
682			fp->altsetting == 5 || fp->altsetting == 6)
683			return 1;
684		break;
685	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
686		if (chip->setup == 0x00 ||
687			fp->altsetting == 1 || fp->altsetting == 2 ||
688			fp->altsetting == 3)
689			return 1;
690		break;
691	case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
692		if (fp->altsetting == 2 || fp->altsetting == 3 ||
693			fp->altsetting == 5 || fp->altsetting == 6)
694			return 1;
695		break;
696	}
697	return 0;
698}
699
700/*
701 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
702 * not for interface.
703 */
704
705enum {
706	EMU_QUIRK_SR_44100HZ = 0,
707	EMU_QUIRK_SR_48000HZ,
708	EMU_QUIRK_SR_88200HZ,
709	EMU_QUIRK_SR_96000HZ,
710	EMU_QUIRK_SR_176400HZ,
711	EMU_QUIRK_SR_192000HZ
712};
713
714static void set_format_emu_quirk(struct snd_usb_substream *subs,
715				 struct audioformat *fmt)
716{
717	unsigned char emu_samplerate_id = 0;
718
719	/* When capture is active
720	 * sample rate shouldn't be changed
721	 * by playback substream
722	 */
723	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
724		if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
725			return;
726	}
727
728	switch (fmt->rate_min) {
729	case 48000:
730		emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
731		break;
732	case 88200:
733		emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
734		break;
735	case 96000:
736		emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
737		break;
738	case 176400:
739		emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
740		break;
741	case 192000:
742		emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
743		break;
744	default:
745		emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
746		break;
747	}
748	snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
749}
750
751void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
752			      struct audioformat *fmt)
753{
754	switch (subs->stream->chip->usb_id) {
755	case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
756	case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
757	case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
758	case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
759		set_format_emu_quirk(subs, fmt);
760		break;
761	}
762}
763