quirks.c 28 KB

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