quirks.c 38 KB

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