quirks.c 44 KB

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