quirks.c 38 KB

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