quirks.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 <linux/usb/midi.h>
  21. #include <sound/control.h>
  22. #include <sound/core.h>
  23. #include <sound/info.h>
  24. #include <sound/pcm.h>
  25. #include "usbaudio.h"
  26. #include "card.h"
  27. #include "mixer.h"
  28. #include "mixer_quirks.h"
  29. #include "midi.h"
  30. #include "quirks.h"
  31. #include "helper.h"
  32. #include "endpoint.h"
  33. #include "pcm.h"
  34. #include "clock.h"
  35. #include "stream.h"
  36. /*
  37. * handle the quirks for the contained interfaces
  38. */
  39. static int create_composite_quirk(struct snd_usb_audio *chip,
  40. struct usb_interface *iface,
  41. struct usb_driver *driver,
  42. const struct snd_usb_audio_quirk *quirk_comp)
  43. {
  44. int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
  45. const struct snd_usb_audio_quirk *quirk;
  46. int err;
  47. for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
  48. iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
  49. if (!iface)
  50. continue;
  51. if (quirk->ifnum != probed_ifnum &&
  52. usb_interface_claimed(iface))
  53. continue;
  54. err = snd_usb_create_quirk(chip, iface, driver, quirk);
  55. if (err < 0)
  56. return err;
  57. }
  58. for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
  59. iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
  60. if (!iface)
  61. continue;
  62. if (quirk->ifnum != probed_ifnum &&
  63. !usb_interface_claimed(iface))
  64. usb_driver_claim_interface(driver, iface, (void *)-1L);
  65. }
  66. return 0;
  67. }
  68. static int ignore_interface_quirk(struct snd_usb_audio *chip,
  69. struct usb_interface *iface,
  70. struct usb_driver *driver,
  71. const struct snd_usb_audio_quirk *quirk)
  72. {
  73. return 0;
  74. }
  75. /*
  76. * Allow alignment on audio sub-slot (channel samples) rather than
  77. * on audio slots (audio frames)
  78. */
  79. static 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. static 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. * create a stream for an interface with proper descriptors
  96. */
  97. static int create_standard_audio_quirk(struct snd_usb_audio *chip,
  98. struct usb_interface *iface,
  99. struct usb_driver *driver,
  100. const struct snd_usb_audio_quirk *quirk)
  101. {
  102. struct usb_host_interface *alts;
  103. struct usb_interface_descriptor *altsd;
  104. int err;
  105. alts = &iface->altsetting[0];
  106. altsd = get_iface_desc(alts);
  107. err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
  108. if (err < 0) {
  109. usb_audio_err(chip, "cannot setup if %d: error %d\n",
  110. altsd->bInterfaceNumber, err);
  111. return err;
  112. }
  113. /* reset the current interface */
  114. usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
  115. return 0;
  116. }
  117. /*
  118. * create a stream for an endpoint/altsetting without proper descriptors
  119. */
  120. static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
  121. struct usb_interface *iface,
  122. struct usb_driver *driver,
  123. const struct snd_usb_audio_quirk *quirk)
  124. {
  125. struct audioformat *fp;
  126. struct usb_host_interface *alts;
  127. struct usb_interface_descriptor *altsd;
  128. int stream, err;
  129. unsigned *rate_table = NULL;
  130. fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
  131. if (!fp) {
  132. usb_audio_err(chip, "cannot memdup\n");
  133. return -ENOMEM;
  134. }
  135. if (fp->nr_rates > MAX_NR_RATES) {
  136. kfree(fp);
  137. return -EINVAL;
  138. }
  139. if (fp->nr_rates > 0) {
  140. rate_table = kmemdup(fp->rate_table,
  141. sizeof(int) * fp->nr_rates, GFP_KERNEL);
  142. if (!rate_table) {
  143. kfree(fp);
  144. return -ENOMEM;
  145. }
  146. fp->rate_table = rate_table;
  147. }
  148. stream = (fp->endpoint & USB_DIR_IN)
  149. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  150. err = snd_usb_add_audio_stream(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. altsd = get_iface_desc(alts);
  164. fp->protocol = altsd->bInterfaceProtocol;
  165. if (fp->datainterval == 0)
  166. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  167. if (fp->maxpacksize == 0)
  168. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  169. usb_set_interface(chip->dev, fp->iface, 0);
  170. snd_usb_init_pitch(chip, fp->iface, alts, fp);
  171. snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
  172. return 0;
  173. }
  174. static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
  175. struct usb_interface *iface,
  176. struct usb_driver *driver)
  177. {
  178. struct usb_host_interface *alts;
  179. struct usb_interface_descriptor *altsd;
  180. struct usb_endpoint_descriptor *epd;
  181. struct uac1_as_header_descriptor *ashd;
  182. struct uac_format_type_i_discrete_descriptor *fmtd;
  183. /*
  184. * Most Roland/Yamaha audio streaming interfaces have more or less
  185. * standard descriptors, but older devices might lack descriptors, and
  186. * future ones might change, so ensure that we fail silently if the
  187. * interface doesn't look exactly right.
  188. */
  189. /* must have a non-zero altsetting for streaming */
  190. if (iface->num_altsetting < 2)
  191. return -ENODEV;
  192. alts = &iface->altsetting[1];
  193. altsd = get_iface_desc(alts);
  194. /* must have an isochronous endpoint for streaming */
  195. if (altsd->bNumEndpoints < 1)
  196. return -ENODEV;
  197. epd = get_endpoint(alts, 0);
  198. if (!usb_endpoint_xfer_isoc(epd))
  199. return -ENODEV;
  200. /* must have format descriptors */
  201. ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
  202. UAC_AS_GENERAL);
  203. fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
  204. UAC_FORMAT_TYPE);
  205. if (!ashd || ashd->bLength < 7 ||
  206. !fmtd || fmtd->bLength < 8)
  207. return -ENODEV;
  208. return create_standard_audio_quirk(chip, iface, driver, NULL);
  209. }
  210. static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
  211. struct usb_interface *iface,
  212. struct usb_driver *driver,
  213. struct usb_host_interface *alts)
  214. {
  215. static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
  216. .type = QUIRK_MIDI_YAMAHA
  217. };
  218. struct usb_midi_in_jack_descriptor *injd;
  219. struct usb_midi_out_jack_descriptor *outjd;
  220. /* must have some valid jack descriptors */
  221. injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  222. NULL, USB_MS_MIDI_IN_JACK);
  223. outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  224. NULL, USB_MS_MIDI_OUT_JACK);
  225. if (!injd && !outjd)
  226. return -ENODEV;
  227. if (injd && (injd->bLength < 5 ||
  228. (injd->bJackType != USB_MS_EMBEDDED &&
  229. injd->bJackType != USB_MS_EXTERNAL)))
  230. return -ENODEV;
  231. if (outjd && (outjd->bLength < 6 ||
  232. (outjd->bJackType != USB_MS_EMBEDDED &&
  233. outjd->bJackType != USB_MS_EXTERNAL)))
  234. return -ENODEV;
  235. return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
  236. }
  237. static int create_roland_midi_quirk(struct snd_usb_audio *chip,
  238. struct usb_interface *iface,
  239. struct usb_driver *driver,
  240. struct usb_host_interface *alts)
  241. {
  242. static const struct snd_usb_audio_quirk roland_midi_quirk = {
  243. .type = QUIRK_MIDI_ROLAND
  244. };
  245. u8 *roland_desc = NULL;
  246. /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
  247. for (;;) {
  248. roland_desc = snd_usb_find_csint_desc(alts->extra,
  249. alts->extralen,
  250. roland_desc, 0xf1);
  251. if (!roland_desc)
  252. return -ENODEV;
  253. if (roland_desc[0] < 6 || roland_desc[3] != 2)
  254. continue;
  255. return create_any_midi_quirk(chip, iface, driver,
  256. &roland_midi_quirk);
  257. }
  258. }
  259. static int create_std_midi_quirk(struct snd_usb_audio *chip,
  260. struct usb_interface *iface,
  261. struct usb_driver *driver,
  262. struct usb_host_interface *alts)
  263. {
  264. struct usb_ms_header_descriptor *mshd;
  265. struct usb_ms_endpoint_descriptor *msepd;
  266. /* must have the MIDIStreaming interface header descriptor*/
  267. mshd = (struct usb_ms_header_descriptor *)alts->extra;
  268. if (alts->extralen < 7 ||
  269. mshd->bLength < 7 ||
  270. mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
  271. mshd->bDescriptorSubtype != USB_MS_HEADER)
  272. return -ENODEV;
  273. /* must have the MIDIStreaming endpoint descriptor*/
  274. msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
  275. if (alts->endpoint[0].extralen < 4 ||
  276. msepd->bLength < 4 ||
  277. msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
  278. msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
  279. msepd->bNumEmbMIDIJack < 1 ||
  280. msepd->bNumEmbMIDIJack > 16)
  281. return -ENODEV;
  282. return create_any_midi_quirk(chip, iface, driver, NULL);
  283. }
  284. static int create_auto_midi_quirk(struct snd_usb_audio *chip,
  285. struct usb_interface *iface,
  286. struct usb_driver *driver)
  287. {
  288. struct usb_host_interface *alts;
  289. struct usb_interface_descriptor *altsd;
  290. struct usb_endpoint_descriptor *epd;
  291. int err;
  292. alts = &iface->altsetting[0];
  293. altsd = get_iface_desc(alts);
  294. /* must have at least one bulk/interrupt endpoint for streaming */
  295. if (altsd->bNumEndpoints < 1)
  296. return -ENODEV;
  297. epd = get_endpoint(alts, 0);
  298. if (!usb_endpoint_xfer_bulk(epd) &&
  299. !usb_endpoint_xfer_int(epd))
  300. return -ENODEV;
  301. switch (USB_ID_VENDOR(chip->usb_id)) {
  302. case 0x0499: /* Yamaha */
  303. err = create_yamaha_midi_quirk(chip, iface, driver, alts);
  304. if (err != -ENODEV)
  305. return err;
  306. break;
  307. case 0x0582: /* Roland */
  308. err = create_roland_midi_quirk(chip, iface, driver, alts);
  309. if (err != -ENODEV)
  310. return err;
  311. break;
  312. }
  313. return create_std_midi_quirk(chip, iface, driver, alts);
  314. }
  315. static int create_autodetect_quirk(struct snd_usb_audio *chip,
  316. struct usb_interface *iface,
  317. struct usb_driver *driver)
  318. {
  319. int err;
  320. err = create_auto_pcm_quirk(chip, iface, driver);
  321. if (err == -ENODEV)
  322. err = create_auto_midi_quirk(chip, iface, driver);
  323. return err;
  324. }
  325. static int create_autodetect_quirks(struct snd_usb_audio *chip,
  326. struct usb_interface *iface,
  327. struct usb_driver *driver,
  328. const struct snd_usb_audio_quirk *quirk)
  329. {
  330. int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
  331. int ifcount, ifnum, err;
  332. err = create_autodetect_quirk(chip, iface, driver);
  333. if (err < 0)
  334. return err;
  335. /*
  336. * ALSA PCM playback/capture devices cannot be registered in two steps,
  337. * so we have to claim the other corresponding interface here.
  338. */
  339. ifcount = chip->dev->actconfig->desc.bNumInterfaces;
  340. for (ifnum = 0; ifnum < ifcount; ifnum++) {
  341. if (ifnum == probed_ifnum || quirk->ifnum >= 0)
  342. continue;
  343. iface = usb_ifnum_to_if(chip->dev, ifnum);
  344. if (!iface ||
  345. usb_interface_claimed(iface) ||
  346. get_iface_desc(iface->altsetting)->bInterfaceClass !=
  347. USB_CLASS_VENDOR_SPEC)
  348. continue;
  349. err = create_autodetect_quirk(chip, iface, driver);
  350. if (err >= 0)
  351. usb_driver_claim_interface(driver, iface, (void *)-1L);
  352. }
  353. return 0;
  354. }
  355. /*
  356. * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
  357. * The only way to detect the sample rate is by looking at wMaxPacketSize.
  358. */
  359. static int create_uaxx_quirk(struct snd_usb_audio *chip,
  360. struct usb_interface *iface,
  361. struct usb_driver *driver,
  362. const struct snd_usb_audio_quirk *quirk)
  363. {
  364. static const struct audioformat ua_format = {
  365. .formats = SNDRV_PCM_FMTBIT_S24_3LE,
  366. .channels = 2,
  367. .fmt_type = UAC_FORMAT_TYPE_I,
  368. .altsetting = 1,
  369. .altset_idx = 1,
  370. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  371. };
  372. struct usb_host_interface *alts;
  373. struct usb_interface_descriptor *altsd;
  374. struct audioformat *fp;
  375. int stream, err;
  376. /* both PCM and MIDI interfaces have 2 or more altsettings */
  377. if (iface->num_altsetting < 2)
  378. return -ENXIO;
  379. alts = &iface->altsetting[1];
  380. altsd = get_iface_desc(alts);
  381. if (altsd->bNumEndpoints == 2) {
  382. static const struct snd_usb_midi_endpoint_info ua700_ep = {
  383. .out_cables = 0x0003,
  384. .in_cables = 0x0003
  385. };
  386. static const struct snd_usb_audio_quirk ua700_quirk = {
  387. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  388. .data = &ua700_ep
  389. };
  390. static const struct snd_usb_midi_endpoint_info uaxx_ep = {
  391. .out_cables = 0x0001,
  392. .in_cables = 0x0001
  393. };
  394. static const struct snd_usb_audio_quirk uaxx_quirk = {
  395. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  396. .data = &uaxx_ep
  397. };
  398. const struct snd_usb_audio_quirk *quirk =
  399. chip->usb_id == USB_ID(0x0582, 0x002b)
  400. ? &ua700_quirk : &uaxx_quirk;
  401. return snd_usbmidi_create(chip->card, iface,
  402. &chip->midi_list, quirk);
  403. }
  404. if (altsd->bNumEndpoints != 1)
  405. return -ENXIO;
  406. fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
  407. if (!fp)
  408. return -ENOMEM;
  409. fp->iface = altsd->bInterfaceNumber;
  410. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  411. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  412. fp->datainterval = 0;
  413. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  414. switch (fp->maxpacksize) {
  415. case 0x120:
  416. fp->rate_max = fp->rate_min = 44100;
  417. break;
  418. case 0x138:
  419. case 0x140:
  420. fp->rate_max = fp->rate_min = 48000;
  421. break;
  422. case 0x258:
  423. case 0x260:
  424. fp->rate_max = fp->rate_min = 96000;
  425. break;
  426. default:
  427. usb_audio_err(chip, "unknown sample rate\n");
  428. kfree(fp);
  429. return -ENXIO;
  430. }
  431. stream = (fp->endpoint & USB_DIR_IN)
  432. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  433. err = snd_usb_add_audio_stream(chip, stream, fp);
  434. if (err < 0) {
  435. kfree(fp);
  436. return err;
  437. }
  438. usb_set_interface(chip->dev, fp->iface, 0);
  439. return 0;
  440. }
  441. /*
  442. * Create a standard mixer for the specified interface.
  443. */
  444. static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
  445. struct usb_interface *iface,
  446. struct usb_driver *driver,
  447. const struct snd_usb_audio_quirk *quirk)
  448. {
  449. if (quirk->ifnum < 0)
  450. return 0;
  451. return snd_usb_create_mixer(chip, quirk->ifnum, 0);
  452. }
  453. /*
  454. * audio-interface quirks
  455. *
  456. * returns zero if no standard audio/MIDI parsing is needed.
  457. * returns a positive value if standard audio/midi interfaces are parsed
  458. * after this.
  459. * returns a negative value at error.
  460. */
  461. int snd_usb_create_quirk(struct snd_usb_audio *chip,
  462. struct usb_interface *iface,
  463. struct usb_driver *driver,
  464. const struct snd_usb_audio_quirk *quirk)
  465. {
  466. typedef int (*quirk_func_t)(struct snd_usb_audio *,
  467. struct usb_interface *,
  468. struct usb_driver *,
  469. const struct snd_usb_audio_quirk *);
  470. static const quirk_func_t quirk_funcs[] = {
  471. [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
  472. [QUIRK_COMPOSITE] = create_composite_quirk,
  473. [QUIRK_AUTODETECT] = create_autodetect_quirks,
  474. [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
  475. [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
  476. [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
  477. [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
  478. [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
  479. [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
  480. [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
  481. [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
  482. [QUIRK_MIDI_CME] = create_any_midi_quirk,
  483. [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
  484. [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
  485. [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
  486. [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
  487. [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
  488. [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
  489. [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
  490. };
  491. if (quirk->type < QUIRK_TYPE_COUNT) {
  492. return quirk_funcs[quirk->type](chip, iface, driver, quirk);
  493. } else {
  494. usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
  495. return -ENXIO;
  496. }
  497. }
  498. /*
  499. * boot quirks
  500. */
  501. #define EXTIGY_FIRMWARE_SIZE_OLD 794
  502. #define EXTIGY_FIRMWARE_SIZE_NEW 483
  503. static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
  504. {
  505. struct usb_host_config *config = dev->actconfig;
  506. int err;
  507. if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
  508. le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
  509. dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
  510. /* Send message to force it to reconnect with full interface. */
  511. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
  512. 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
  513. if (err < 0)
  514. dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
  515. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  516. &dev->descriptor, sizeof(dev->descriptor));
  517. config = dev->actconfig;
  518. if (err < 0)
  519. dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
  520. err = usb_reset_configuration(dev);
  521. if (err < 0)
  522. dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
  523. dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
  524. le16_to_cpu(get_cfg_desc(config)->wTotalLength));
  525. return -ENODEV; /* quit this anyway */
  526. }
  527. return 0;
  528. }
  529. static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
  530. {
  531. u8 buf = 1;
  532. snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
  533. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  534. 0, 0, &buf, 1);
  535. if (buf == 0) {
  536. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
  537. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  538. 1, 2000, NULL, 0);
  539. return -ENODEV;
  540. }
  541. return 0;
  542. }
  543. static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
  544. {
  545. int err;
  546. if (dev->actconfig->desc.bConfigurationValue == 1) {
  547. dev_info(&dev->dev,
  548. "Fast Track Pro switching to config #2\n");
  549. /* This function has to be available by the usb core module.
  550. * if it is not avialable the boot quirk has to be left out
  551. * and the configuration has to be set by udev or hotplug
  552. * rules
  553. */
  554. err = usb_driver_set_configuration(dev, 2);
  555. if (err < 0)
  556. dev_dbg(&dev->dev,
  557. "error usb_driver_set_configuration: %d\n",
  558. err);
  559. /* Always return an error, so that we stop creating a device
  560. that will just be destroyed and recreated with a new
  561. configuration */
  562. return -ENODEV;
  563. } else
  564. dev_info(&dev->dev, "Fast Track Pro config OK\n");
  565. return 0;
  566. }
  567. /*
  568. * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
  569. * documented in the device's data sheet.
  570. */
  571. static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
  572. {
  573. u8 buf[4];
  574. buf[0] = 0x20;
  575. buf[1] = value & 0xff;
  576. buf[2] = (value >> 8) & 0xff;
  577. buf[3] = reg;
  578. return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
  579. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  580. 0, 0, &buf, 4);
  581. }
  582. static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
  583. {
  584. /*
  585. * Enable line-out driver mode, set headphone source to front
  586. * channels, enable stereo mic.
  587. */
  588. return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
  589. }
  590. /*
  591. * C-Media CM6206 is based on CM106 with two additional
  592. * registers that are not documented in the data sheet.
  593. * Values here are chosen based on sniffing USB traffic
  594. * under Windows.
  595. */
  596. static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
  597. {
  598. int err = 0, reg;
  599. int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
  600. for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
  601. err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
  602. if (err < 0)
  603. return err;
  604. }
  605. return err;
  606. }
  607. /* quirk for Plantronics GameCom 780 with CM6302 chip */
  608. static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
  609. {
  610. /* set the initial volume and don't change; other values are either
  611. * too loud or silent due to firmware bug (bko#65251)
  612. */
  613. u8 buf[2] = { 0x74, 0xe3 };
  614. return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  615. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  616. UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
  617. }
  618. /*
  619. * Novation Twitch DJ controller
  620. * Focusrite Novation Saffire 6 USB audio card
  621. */
  622. static int snd_usb_novation_boot_quirk(struct usb_device *dev)
  623. {
  624. /* preemptively set up the device because otherwise the
  625. * raw MIDI endpoints are not active */
  626. usb_set_interface(dev, 0, 1);
  627. return 0;
  628. }
  629. /*
  630. * This call will put the synth in "USB send" mode, i.e it will send MIDI
  631. * messages through USB (this is disabled at startup). The synth will
  632. * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
  633. * sign on its LCD. Values here are chosen based on sniffing USB traffic
  634. * under Windows.
  635. */
  636. static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
  637. {
  638. int err, actual_length;
  639. /* "midi send" enable */
  640. static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
  641. void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
  642. if (!buf)
  643. return -ENOMEM;
  644. err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
  645. ARRAY_SIZE(seq), &actual_length, 1000);
  646. kfree(buf);
  647. if (err < 0)
  648. return err;
  649. return 0;
  650. }
  651. /*
  652. * Some sound cards from Native Instruments are in fact compliant to the USB
  653. * audio standard of version 2 and other approved USB standards, even though
  654. * they come up as vendor-specific device when first connected.
  655. *
  656. * However, they can be told to come up with a new set of descriptors
  657. * upon their next enumeration, and the interfaces announced by the new
  658. * descriptors will then be handled by the kernel's class drivers. As the
  659. * product ID will also change, no further checks are required.
  660. */
  661. static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
  662. {
  663. int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  664. 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  665. 1, 0, NULL, 0, 1000);
  666. if (ret < 0)
  667. return ret;
  668. usb_reset_device(dev);
  669. /* return -EAGAIN, so the creation of an audio interface for this
  670. * temporary device is aborted. The device will reconnect with a
  671. * new product ID */
  672. return -EAGAIN;
  673. }
  674. static void mbox2_setup_48_24_magic(struct usb_device *dev)
  675. {
  676. u8 srate[3];
  677. u8 temp[12];
  678. /* Choose 48000Hz permanently */
  679. srate[0] = 0x80;
  680. srate[1] = 0xbb;
  681. srate[2] = 0x00;
  682. /* Send the magic! */
  683. snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
  684. 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
  685. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
  686. 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
  687. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
  688. 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
  689. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
  690. 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
  691. return;
  692. }
  693. /* Digidesign Mbox 2 needs to load firmware onboard
  694. * and driver must wait a few seconds for initialisation.
  695. */
  696. #define MBOX2_FIRMWARE_SIZE 646
  697. #define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
  698. #define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
  699. static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
  700. {
  701. struct usb_host_config *config = dev->actconfig;
  702. int err;
  703. u8 bootresponse[0x12];
  704. int fwsize;
  705. int count;
  706. fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
  707. if (fwsize != MBOX2_FIRMWARE_SIZE) {
  708. dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
  709. return -ENODEV;
  710. }
  711. dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
  712. count = 0;
  713. bootresponse[0] = MBOX2_BOOT_LOADING;
  714. while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
  715. msleep(500); /* 0.5 second delay */
  716. snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
  717. /* Control magic - load onboard firmware */
  718. 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
  719. if (bootresponse[0] == MBOX2_BOOT_READY)
  720. break;
  721. dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
  722. count++;
  723. }
  724. if (bootresponse[0] != MBOX2_BOOT_READY) {
  725. dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
  726. return -ENODEV;
  727. }
  728. dev_dbg(&dev->dev, "device initialised!\n");
  729. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  730. &dev->descriptor, sizeof(dev->descriptor));
  731. config = dev->actconfig;
  732. if (err < 0)
  733. dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
  734. err = usb_reset_configuration(dev);
  735. if (err < 0)
  736. dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
  737. dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
  738. le16_to_cpu(get_cfg_desc(config)->wTotalLength));
  739. mbox2_setup_48_24_magic(dev);
  740. dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
  741. return 0; /* Successful boot */
  742. }
  743. /*
  744. * Setup quirks
  745. */
  746. #define MAUDIO_SET 0x01 /* parse device_setup */
  747. #define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
  748. #define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
  749. #define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
  750. #define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
  751. #define MAUDIO_SET_DI 0x10 /* enable Digital Input */
  752. #define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
  753. #define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
  754. #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
  755. #define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
  756. #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
  757. static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
  758. int iface, int altno)
  759. {
  760. /* Reset ALL ifaces to 0 altsetting.
  761. * Call it for every possible altsetting of every interface.
  762. */
  763. usb_set_interface(chip->dev, iface, 0);
  764. if (chip->setup & MAUDIO_SET) {
  765. if (chip->setup & MAUDIO_SET_COMPATIBLE) {
  766. if (iface != 1 && iface != 2)
  767. return 1; /* skip all interfaces but 1 and 2 */
  768. } else {
  769. unsigned int mask;
  770. if (iface == 1 || iface == 2)
  771. return 1; /* skip interfaces 1 and 2 */
  772. if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
  773. return 1; /* skip this altsetting */
  774. mask = chip->setup & MAUDIO_SET_MASK;
  775. if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
  776. return 1; /* skip this altsetting */
  777. if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
  778. return 1; /* skip this altsetting */
  779. if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
  780. return 1; /* skip this altsetting */
  781. }
  782. }
  783. usb_audio_dbg(chip,
  784. "using altsetting %d for interface %d config %d\n",
  785. altno, iface, chip->setup);
  786. return 0; /* keep this altsetting */
  787. }
  788. static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
  789. int iface,
  790. int altno)
  791. {
  792. /* Reset ALL ifaces to 0 altsetting.
  793. * Call it for every possible altsetting of every interface.
  794. */
  795. usb_set_interface(chip->dev, iface, 0);
  796. if (chip->setup & MAUDIO_SET) {
  797. unsigned int mask;
  798. if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
  799. return 1; /* skip this altsetting */
  800. if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
  801. return 1; /* skip this altsetting */
  802. mask = chip->setup & MAUDIO_SET_MASK;
  803. if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
  804. return 1; /* skip this altsetting */
  805. if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
  806. return 1; /* skip this altsetting */
  807. if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
  808. return 1; /* skip this altsetting */
  809. if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
  810. return 1; /* skip this altsetting */
  811. }
  812. return 0; /* keep this altsetting */
  813. }
  814. static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
  815. int iface, int altno)
  816. {
  817. /* Reset ALL ifaces to 0 altsetting.
  818. * Call it for every possible altsetting of every interface.
  819. */
  820. usb_set_interface(chip->dev, iface, 0);
  821. /* possible configuration where both inputs and only one output is
  822. *used is not supported by the current setup
  823. */
  824. if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
  825. if (chip->setup & MAUDIO_SET_96K) {
  826. if (altno != 3 && altno != 6)
  827. return 1;
  828. } else if (chip->setup & MAUDIO_SET_DI) {
  829. if (iface == 4)
  830. return 1; /* no analog input */
  831. if (altno != 2 && altno != 5)
  832. return 1; /* enable only altsets 2 and 5 */
  833. } else {
  834. if (iface == 5)
  835. return 1; /* disable digialt input */
  836. if (altno != 2 && altno != 5)
  837. return 1; /* enalbe only altsets 2 and 5 */
  838. }
  839. } else {
  840. /* keep only 16-Bit mode */
  841. if (altno != 1)
  842. return 1;
  843. }
  844. usb_audio_dbg(chip,
  845. "using altsetting %d for interface %d config %d\n",
  846. altno, iface, chip->setup);
  847. return 0; /* keep this altsetting */
  848. }
  849. int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
  850. int iface,
  851. int altno)
  852. {
  853. /* audiophile usb: skip altsets incompatible with device_setup */
  854. if (chip->usb_id == USB_ID(0x0763, 0x2003))
  855. return audiophile_skip_setting_quirk(chip, iface, altno);
  856. /* quattro usb: skip altsets incompatible with device_setup */
  857. if (chip->usb_id == USB_ID(0x0763, 0x2001))
  858. return quattro_skip_setting_quirk(chip, iface, altno);
  859. /* fasttrackpro usb: skip altsets incompatible with device_setup */
  860. if (chip->usb_id == USB_ID(0x0763, 0x2012))
  861. return fasttrackpro_skip_setting_quirk(chip, iface, altno);
  862. return 0;
  863. }
  864. int snd_usb_apply_boot_quirk(struct usb_device *dev,
  865. struct usb_interface *intf,
  866. const struct snd_usb_audio_quirk *quirk)
  867. {
  868. u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  869. le16_to_cpu(dev->descriptor.idProduct));
  870. switch (id) {
  871. case USB_ID(0x041e, 0x3000):
  872. /* SB Extigy needs special boot-up sequence */
  873. /* if more models come, this will go to the quirk list. */
  874. return snd_usb_extigy_boot_quirk(dev, intf);
  875. case USB_ID(0x041e, 0x3020):
  876. /* SB Audigy 2 NX needs its own boot-up magic, too */
  877. return snd_usb_audigy2nx_boot_quirk(dev);
  878. case USB_ID(0x10f5, 0x0200):
  879. /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
  880. return snd_usb_cm106_boot_quirk(dev);
  881. case USB_ID(0x0d8c, 0x0102):
  882. /* C-Media CM6206 / CM106-Like Sound Device */
  883. case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
  884. return snd_usb_cm6206_boot_quirk(dev);
  885. case USB_ID(0x0dba, 0x3000):
  886. /* Digidesign Mbox 2 */
  887. return snd_usb_mbox2_boot_quirk(dev);
  888. case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
  889. case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
  890. return snd_usb_novation_boot_quirk(dev);
  891. case USB_ID(0x133e, 0x0815):
  892. /* Access Music VirusTI Desktop */
  893. return snd_usb_accessmusic_boot_quirk(dev);
  894. case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
  895. case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
  896. case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
  897. return snd_usb_nativeinstruments_boot_quirk(dev);
  898. case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
  899. return snd_usb_fasttrackpro_boot_quirk(dev);
  900. case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
  901. return snd_usb_gamecon780_boot_quirk(dev);
  902. }
  903. return 0;
  904. }
  905. /*
  906. * check if the device uses big-endian samples
  907. */
  908. int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
  909. {
  910. /* it depends on altsetting whether the device is big-endian or not */
  911. switch (chip->usb_id) {
  912. case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
  913. if (fp->altsetting == 2 || fp->altsetting == 3 ||
  914. fp->altsetting == 5 || fp->altsetting == 6)
  915. return 1;
  916. break;
  917. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  918. if (chip->setup == 0x00 ||
  919. fp->altsetting == 1 || fp->altsetting == 2 ||
  920. fp->altsetting == 3)
  921. return 1;
  922. break;
  923. case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
  924. if (fp->altsetting == 2 || fp->altsetting == 3 ||
  925. fp->altsetting == 5 || fp->altsetting == 6)
  926. return 1;
  927. break;
  928. }
  929. return 0;
  930. }
  931. /*
  932. * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
  933. * not for interface.
  934. */
  935. enum {
  936. EMU_QUIRK_SR_44100HZ = 0,
  937. EMU_QUIRK_SR_48000HZ,
  938. EMU_QUIRK_SR_88200HZ,
  939. EMU_QUIRK_SR_96000HZ,
  940. EMU_QUIRK_SR_176400HZ,
  941. EMU_QUIRK_SR_192000HZ
  942. };
  943. static void set_format_emu_quirk(struct snd_usb_substream *subs,
  944. struct audioformat *fmt)
  945. {
  946. unsigned char emu_samplerate_id = 0;
  947. /* When capture is active
  948. * sample rate shouldn't be changed
  949. * by playback substream
  950. */
  951. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  952. if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
  953. return;
  954. }
  955. switch (fmt->rate_min) {
  956. case 48000:
  957. emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
  958. break;
  959. case 88200:
  960. emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
  961. break;
  962. case 96000:
  963. emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
  964. break;
  965. case 176400:
  966. emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
  967. break;
  968. case 192000:
  969. emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
  970. break;
  971. default:
  972. emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
  973. break;
  974. }
  975. snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
  976. subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
  977. }
  978. void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
  979. struct audioformat *fmt)
  980. {
  981. switch (subs->stream->chip->usb_id) {
  982. case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
  983. case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
  984. case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
  985. case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
  986. set_format_emu_quirk(subs, fmt);
  987. break;
  988. }
  989. }
  990. bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
  991. {
  992. /* devices which do not support reading the sample rate. */
  993. switch (chip->usb_id) {
  994. case USB_ID(0x045E, 0x075D): /* MS Lifecam Cinema */
  995. case USB_ID(0x045E, 0x076D): /* MS Lifecam HD-5000 */
  996. case USB_ID(0x045E, 0x0772): /* MS Lifecam Studio */
  997. case USB_ID(0x045E, 0x0779): /* MS Lifecam HD-3000 */
  998. case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
  999. case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
  1000. return true;
  1001. }
  1002. return false;
  1003. }
  1004. /* Marantz/Denon USB DACs need a vendor cmd to switch
  1005. * between PCM and native DSD mode
  1006. */
  1007. static bool is_marantz_denon_dac(unsigned int id)
  1008. {
  1009. switch (id) {
  1010. case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
  1011. case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
  1012. case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
  1018. struct audioformat *fmt)
  1019. {
  1020. struct usb_device *dev = subs->dev;
  1021. int err;
  1022. if (is_marantz_denon_dac(subs->stream->chip->usb_id)) {
  1023. /* First switch to alt set 0, otherwise the mode switch cmd
  1024. * will not be accepted by the DAC
  1025. */
  1026. err = usb_set_interface(dev, fmt->iface, 0);
  1027. if (err < 0)
  1028. return err;
  1029. mdelay(20); /* Delay needed after setting the interface */
  1030. switch (fmt->altsetting) {
  1031. case 2: /* DSD mode requested */
  1032. case 1: /* PCM mode requested */
  1033. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
  1034. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
  1035. fmt->altsetting - 1, 1, NULL, 0);
  1036. if (err < 0)
  1037. return err;
  1038. break;
  1039. }
  1040. mdelay(20);
  1041. }
  1042. return 0;
  1043. }
  1044. void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
  1045. {
  1046. /*
  1047. * "Playback Design" products send bogus feedback data at the start
  1048. * of the stream. Ignore them.
  1049. */
  1050. if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
  1051. ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
  1052. ep->skip_packets = 4;
  1053. /*
  1054. * M-Audio Fast Track C400/C600 - when packets are not skipped, real
  1055. * world latency varies by approx. +/- 50 frames (at 96KHz) each time
  1056. * the stream is (re)started. When skipping packets 16 at endpoint
  1057. * start up, the real world latency is stable within +/- 1 frame (also
  1058. * across power cycles).
  1059. */
  1060. if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
  1061. ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
  1062. ep->type == SND_USB_ENDPOINT_TYPE_DATA)
  1063. ep->skip_packets = 16;
  1064. }
  1065. void snd_usb_set_interface_quirk(struct usb_device *dev)
  1066. {
  1067. /*
  1068. * "Playback Design" products need a 50ms delay after setting the
  1069. * USB interface.
  1070. */
  1071. if (le16_to_cpu(dev->descriptor.idVendor) == 0x23ba)
  1072. mdelay(50);
  1073. }
  1074. void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
  1075. __u8 request, __u8 requesttype, __u16 value,
  1076. __u16 index, void *data, __u16 size)
  1077. {
  1078. /*
  1079. * "Playback Design" products need a 20ms delay after each
  1080. * class compliant request
  1081. */
  1082. if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
  1083. (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1084. mdelay(20);
  1085. /* Marantz/Denon devices with USB DAC functionality need a delay
  1086. * after each class compliant request
  1087. */
  1088. if (is_marantz_denon_dac(USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  1089. le16_to_cpu(dev->descriptor.idProduct)))
  1090. && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1091. mdelay(20);
  1092. /* Zoom R16/24 needs a tiny delay here, otherwise requests like
  1093. * get/set frequency return as failed despite actually succeeding.
  1094. */
  1095. if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1686) &&
  1096. (le16_to_cpu(dev->descriptor.idProduct) == 0x00dd) &&
  1097. (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1098. mdelay(1);
  1099. }
  1100. /*
  1101. * snd_usb_interface_dsd_format_quirks() is called from format.c to
  1102. * augment the PCM format bit-field for DSD types. The UAC standards
  1103. * don't have a designated bit field to denote DSD-capable interfaces,
  1104. * hence all hardware that is known to support this format has to be
  1105. * listed here.
  1106. */
  1107. u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
  1108. struct audioformat *fp,
  1109. unsigned int sample_bytes)
  1110. {
  1111. /* Playback Designs */
  1112. if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x23ba) {
  1113. switch (fp->altsetting) {
  1114. case 1:
  1115. fp->dsd_dop = true;
  1116. return SNDRV_PCM_FMTBIT_DSD_U16_LE;
  1117. case 2:
  1118. fp->dsd_bitrev = true;
  1119. return SNDRV_PCM_FMTBIT_DSD_U8;
  1120. case 3:
  1121. fp->dsd_bitrev = true;
  1122. return SNDRV_PCM_FMTBIT_DSD_U16_LE;
  1123. }
  1124. }
  1125. /* XMOS based USB DACs */
  1126. switch (chip->usb_id) {
  1127. case USB_ID(0x20b1, 0x3008): /* iFi Audio micro/nano iDSD */
  1128. case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
  1129. case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
  1130. if (fp->altsetting == 2)
  1131. return SNDRV_PCM_FMTBIT_DSD_U32_BE;
  1132. break;
  1133. case USB_ID(0x20b1, 0x000a): /* Gustard DAC-X20U */
  1134. case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
  1135. case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
  1136. if (fp->altsetting == 3)
  1137. return SNDRV_PCM_FMTBIT_DSD_U32_BE;
  1138. break;
  1139. default:
  1140. break;
  1141. }
  1142. /* Denon/Marantz devices with USB DAC functionality */
  1143. if (is_marantz_denon_dac(chip->usb_id)) {
  1144. if (fp->altsetting == 2)
  1145. return SNDRV_PCM_FMTBIT_DSD_U32_BE;
  1146. }
  1147. return 0;
  1148. }