quirks.c 38 KB

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