quirks.c 41 KB

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