format.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/usb.h>
  7. #include <linux/usb/audio.h>
  8. #include <linux/usb/audio-v2.h>
  9. #include <linux/usb/audio-v3.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include "usbaudio.h"
  13. #include "card.h"
  14. #include "quirks.h"
  15. #include "helper.h"
  16. #include "clock.h"
  17. #include "format.h"
  18. /*
  19. * parse the audio format type I descriptor
  20. * and returns the corresponding pcm format
  21. *
  22. * @dev: usb device
  23. * @fp: audioformat record
  24. * @format: the format tag (wFormatTag)
  25. * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
  26. */
  27. static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
  28. struct audioformat *fp,
  29. u64 format, void *_fmt)
  30. {
  31. int sample_width, sample_bytes;
  32. u64 pcm_formats = 0;
  33. switch (fp->protocol) {
  34. case UAC_VERSION_1:
  35. default: {
  36. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  37. if (format >= 64) {
  38. usb_audio_info(chip,
  39. "%u:%d: invalid format type 0x%llx is detected, processed as PCM\n",
  40. fp->iface, fp->altsetting, format);
  41. format = UAC_FORMAT_TYPE_I_PCM;
  42. }
  43. sample_width = fmt->bBitResolution;
  44. sample_bytes = fmt->bSubframeSize;
  45. format = 1ULL << format;
  46. break;
  47. }
  48. case UAC_VERSION_2: {
  49. struct uac_format_type_i_ext_descriptor *fmt = _fmt;
  50. sample_width = fmt->bBitResolution;
  51. sample_bytes = fmt->bSubslotSize;
  52. if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
  53. pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  54. /* flag potentially raw DSD capable altsettings */
  55. fp->dsd_raw = true;
  56. /* clear special format bit to avoid "unsupported format" msg below */
  57. format &= ~UAC2_FORMAT_TYPE_I_RAW_DATA;
  58. }
  59. format <<= 1;
  60. break;
  61. }
  62. case UAC_VERSION_3: {
  63. struct uac3_as_header_descriptor *as = _fmt;
  64. sample_width = as->bBitResolution;
  65. sample_bytes = as->bSubslotSize;
  66. if (format & UAC3_FORMAT_TYPE_I_RAW_DATA) {
  67. pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  68. /* clear special format bit to avoid "unsupported format" msg below */
  69. format &= ~UAC3_FORMAT_TYPE_I_RAW_DATA;
  70. }
  71. format <<= 1;
  72. break;
  73. }
  74. }
  75. fp->fmt_bits = sample_width;
  76. if ((pcm_formats == 0) &&
  77. (format == 0 || format == BIT(UAC_FORMAT_TYPE_I_UNDEFINED))) {
  78. /* some devices don't define this correctly... */
  79. usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
  80. fp->iface, fp->altsetting);
  81. format = BIT(UAC_FORMAT_TYPE_I_PCM);
  82. }
  83. if (format & BIT(UAC_FORMAT_TYPE_I_PCM)) {
  84. if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
  85. /* Edirol SD-90 */
  86. (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
  87. /* Roland SC-D70 */
  88. sample_width == 24 && sample_bytes == 2)
  89. sample_bytes = 3;
  90. else if (sample_width > sample_bytes * 8) {
  91. usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
  92. fp->iface, fp->altsetting,
  93. sample_width, sample_bytes);
  94. }
  95. /* check the format byte size */
  96. switch (sample_bytes) {
  97. case 1:
  98. pcm_formats |= SNDRV_PCM_FMTBIT_S8;
  99. break;
  100. case 2:
  101. if (snd_usb_is_big_endian_format(chip, fp))
  102. pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
  103. else
  104. pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
  105. break;
  106. case 3:
  107. if (snd_usb_is_big_endian_format(chip, fp))
  108. pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
  109. else
  110. pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
  111. break;
  112. case 4:
  113. pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
  114. break;
  115. default:
  116. usb_audio_info(chip,
  117. "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
  118. fp->iface, fp->altsetting,
  119. sample_width, sample_bytes);
  120. break;
  121. }
  122. }
  123. if (format & BIT(UAC_FORMAT_TYPE_I_PCM8)) {
  124. /* Dallas DS4201 workaround: it advertises U8 format, but really
  125. supports S8. */
  126. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  127. pcm_formats |= SNDRV_PCM_FMTBIT_S8;
  128. else
  129. pcm_formats |= SNDRV_PCM_FMTBIT_U8;
  130. }
  131. if (format & BIT(UAC_FORMAT_TYPE_I_IEEE_FLOAT))
  132. pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  133. if (format & BIT(UAC_FORMAT_TYPE_I_ALAW))
  134. pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
  135. if (format & BIT(UAC_FORMAT_TYPE_I_MULAW))
  136. pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
  137. if (format & ~0x3f) {
  138. usb_audio_info(chip,
  139. "%u:%d : unsupported format bits %#llx\n",
  140. fp->iface, fp->altsetting, format);
  141. }
  142. pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
  143. return pcm_formats;
  144. }
  145. static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits)
  146. {
  147. kfree(fp->rate_table);
  148. fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
  149. if (!fp->rate_table)
  150. return -ENOMEM;
  151. fp->nr_rates = 1;
  152. fp->rate_min = rate;
  153. fp->rate_max = rate;
  154. fp->rates = rate_bits;
  155. fp->rate_table[0] = rate;
  156. return 0;
  157. }
  158. /* set up rate_min, rate_max and rates from the rate table */
  159. static void set_rate_table_min_max(struct audioformat *fp)
  160. {
  161. unsigned int rate;
  162. int i;
  163. fp->rate_min = INT_MAX;
  164. fp->rate_max = 0;
  165. fp->rates = 0;
  166. for (i = 0; i < fp->nr_rates; i++) {
  167. rate = fp->rate_table[i];
  168. fp->rate_min = min(fp->rate_min, rate);
  169. fp->rate_max = max(fp->rate_max, rate);
  170. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  171. }
  172. }
  173. /*
  174. * parse the format descriptor and stores the possible sample rates
  175. * on the audioformat table (audio class v1).
  176. *
  177. * @dev: usb device
  178. * @fp: audioformat record
  179. * @fmt: the format descriptor
  180. * @offset: the start offset of descriptor pointing the rate type
  181. * (7 for type I and II, 8 for type II)
  182. */
  183. static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  184. unsigned char *fmt, int offset)
  185. {
  186. int nr_rates = fmt[offset];
  187. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  188. usb_audio_err(chip,
  189. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  190. fp->iface, fp->altsetting);
  191. return -EINVAL;
  192. }
  193. if (nr_rates) {
  194. /*
  195. * build the rate table and bitmap flags
  196. */
  197. int r, idx;
  198. fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
  199. GFP_KERNEL);
  200. if (fp->rate_table == NULL)
  201. return -ENOMEM;
  202. fp->nr_rates = 0;
  203. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  204. unsigned int rate = combine_triple(&fmt[idx]);
  205. if (!rate)
  206. continue;
  207. /* C-Media CM6501 mislabels its 96 kHz altsetting */
  208. /* Terratec Aureon 7.1 USB C-Media 6206, too */
  209. /* Ozone Z90 USB C-Media, too */
  210. if (rate == 48000 && nr_rates == 1 &&
  211. (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
  212. chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
  213. chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
  214. chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
  215. fp->altsetting == 5 && fp->maxpacksize == 392)
  216. rate = 96000;
  217. /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
  218. if (rate == 16000 &&
  219. (chip->usb_id == USB_ID(0x041e, 0x4064) ||
  220. chip->usb_id == USB_ID(0x041e, 0x4068)))
  221. rate = 8000;
  222. fp->rate_table[fp->nr_rates++] = rate;
  223. }
  224. if (!fp->nr_rates) {
  225. usb_audio_info(chip,
  226. "%u:%d: All rates were zero\n",
  227. fp->iface, fp->altsetting);
  228. return -EINVAL;
  229. }
  230. set_rate_table_min_max(fp);
  231. } else {
  232. /* continuous rates */
  233. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  234. fp->rate_min = combine_triple(&fmt[offset + 1]);
  235. fp->rate_max = combine_triple(&fmt[offset + 4]);
  236. }
  237. /* Jabra Evolve 65 headset */
  238. if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) {
  239. /* only 48kHz for playback while keeping 16kHz for capture */
  240. if (fp->nr_rates != 1)
  241. return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
  242. }
  243. return 0;
  244. }
  245. /*
  246. * Presonus Studio 1810c supports a limited set of sampling
  247. * rates per altsetting but reports the full set each time.
  248. * If we don't filter out the unsupported rates and attempt
  249. * to configure the card, it will hang refusing to do any
  250. * further audio I/O until a hard reset is performed.
  251. *
  252. * The list of supported rates per altsetting (set of available
  253. * I/O channels) is described in the owner's manual, section 2.2.
  254. */
  255. static bool s1810c_valid_sample_rate(struct audioformat *fp,
  256. unsigned int rate)
  257. {
  258. switch (fp->altsetting) {
  259. case 1:
  260. /* All ADAT ports available */
  261. return rate <= 48000;
  262. case 2:
  263. /* Half of ADAT ports available */
  264. return (rate == 88200 || rate == 96000);
  265. case 3:
  266. /* Analog I/O only (no S/PDIF nor ADAT) */
  267. return rate >= 176400;
  268. default:
  269. return false;
  270. }
  271. return false;
  272. }
  273. /*
  274. * Many Focusrite devices supports a limited set of sampling rates per
  275. * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
  276. * descriptor which has a non-standard bLength = 10.
  277. */
  278. static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
  279. struct audioformat *fp,
  280. unsigned int rate)
  281. {
  282. struct usb_interface *iface;
  283. struct usb_host_interface *alts;
  284. unsigned char *fmt;
  285. unsigned int max_rate;
  286. iface = usb_ifnum_to_if(chip->dev, fp->iface);
  287. if (!iface)
  288. return true;
  289. alts = &iface->altsetting[fp->altset_idx];
  290. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  291. NULL, UAC_FORMAT_TYPE);
  292. if (!fmt)
  293. return true;
  294. if (fmt[0] == 10) { /* bLength */
  295. max_rate = combine_quad(&fmt[6]);
  296. /* Validate max rate */
  297. if (max_rate != 48000 &&
  298. max_rate != 96000 &&
  299. max_rate != 192000 &&
  300. max_rate != 384000) {
  301. usb_audio_info(chip,
  302. "%u:%d : unexpected max rate: %u\n",
  303. fp->iface, fp->altsetting, max_rate);
  304. return true;
  305. }
  306. return rate <= max_rate;
  307. }
  308. return true;
  309. }
  310. /*
  311. * Helper function to walk the array of sample rate triplets reported by
  312. * the device. The problem is that we need to parse whole array first to
  313. * get to know how many sample rates we have to expect.
  314. * Then fp->rate_table can be allocated and filled.
  315. */
  316. static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
  317. struct audioformat *fp, int nr_triplets,
  318. const unsigned char *data)
  319. {
  320. int i, nr_rates = 0;
  321. for (i = 0; i < nr_triplets; i++) {
  322. int min = combine_quad(&data[2 + 12 * i]);
  323. int max = combine_quad(&data[6 + 12 * i]);
  324. int res = combine_quad(&data[10 + 12 * i]);
  325. unsigned int rate;
  326. if ((max < 0) || (min < 0) || (res < 0) || (max < min))
  327. continue;
  328. /*
  329. * for ranges with res == 1, we announce a continuous sample
  330. * rate range, and this function should return 0 for no further
  331. * parsing.
  332. */
  333. if (res == 1) {
  334. fp->rate_min = min;
  335. fp->rate_max = max;
  336. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  337. return 0;
  338. }
  339. for (rate = min; rate <= max; rate += res) {
  340. /* Filter out invalid rates on Presonus Studio 1810c */
  341. if (chip->usb_id == USB_ID(0x194f, 0x010c) &&
  342. !s1810c_valid_sample_rate(fp, rate))
  343. goto skip_rate;
  344. /* Filter out invalid rates on Focusrite devices */
  345. if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
  346. !focusrite_valid_sample_rate(chip, fp, rate))
  347. goto skip_rate;
  348. if (fp->rate_table)
  349. fp->rate_table[nr_rates] = rate;
  350. nr_rates++;
  351. if (nr_rates >= MAX_NR_RATES) {
  352. usb_audio_err(chip, "invalid uac2 rates\n");
  353. break;
  354. }
  355. skip_rate:
  356. /* avoid endless loop */
  357. if (res == 0)
  358. break;
  359. }
  360. }
  361. return nr_rates;
  362. }
  363. /* Line6 Helix series and the Rode Rodecaster Pro don't support the
  364. * UAC2_CS_RANGE usb function call. Return a static table of known
  365. * clock rates.
  366. */
  367. static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
  368. struct audioformat *fp)
  369. {
  370. switch (chip->usb_id) {
  371. case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
  372. case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
  373. case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
  374. case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
  375. case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */
  376. case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
  377. case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
  378. case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
  379. case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
  380. case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */
  381. case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
  382. return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
  383. }
  384. return -ENODEV;
  385. }
  386. /* check whether the given altsetting is supported for the already set rate */
  387. static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
  388. int altsetting)
  389. {
  390. struct usb_device *dev = chip->dev;
  391. __le64 raw_data = 0;
  392. u64 data;
  393. int err;
  394. /* we assume 64bit is enough for any altsettings */
  395. if (snd_BUG_ON(altsetting >= 64 - 8))
  396. return false;
  397. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  398. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  399. UAC2_AS_VAL_ALT_SETTINGS << 8,
  400. iface, &raw_data, sizeof(raw_data));
  401. if (err < 0)
  402. return false;
  403. data = le64_to_cpu(raw_data);
  404. /* first byte contains the bitmap size */
  405. if ((data & 0xff) * 8 < altsetting)
  406. return false;
  407. if (data & (1ULL << (altsetting + 8)))
  408. return true;
  409. return false;
  410. }
  411. /*
  412. * Validate each sample rate with the altsetting
  413. * Rebuild the rate table if only partial values are valid
  414. */
  415. static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
  416. struct audioformat *fp,
  417. int clock)
  418. {
  419. struct usb_device *dev = chip->dev;
  420. struct usb_host_interface *alts;
  421. unsigned int *table;
  422. unsigned int nr_rates;
  423. int i, err;
  424. u32 bmControls;
  425. /* performing the rate verification may lead to unexpected USB bus
  426. * behavior afterwards by some unknown reason. Do this only for the
  427. * known devices.
  428. */
  429. if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES))
  430. return 0; /* don't perform the validation as default */
  431. alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting);
  432. if (!alts)
  433. return 0;
  434. if (fp->protocol == UAC_VERSION_3) {
  435. struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc(
  436. alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  437. bmControls = le32_to_cpu(as->bmControls);
  438. } else {
  439. struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc(
  440. alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  441. bmControls = as->bmControls;
  442. }
  443. if (!uac_v2v3_control_is_readable(bmControls,
  444. UAC2_AS_VAL_ALT_SETTINGS))
  445. return 0;
  446. table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
  447. if (!table)
  448. return -ENOMEM;
  449. /* clear the interface altsetting at first */
  450. usb_set_interface(dev, fp->iface, 0);
  451. nr_rates = 0;
  452. for (i = 0; i < fp->nr_rates; i++) {
  453. err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
  454. fp->rate_table[i]);
  455. if (err < 0)
  456. continue;
  457. if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
  458. table[nr_rates++] = fp->rate_table[i];
  459. }
  460. if (!nr_rates) {
  461. usb_audio_dbg(chip,
  462. "No valid sample rate available for %d:%d, assuming a firmware bug\n",
  463. fp->iface, fp->altsetting);
  464. nr_rates = fp->nr_rates; /* continue as is */
  465. }
  466. if (fp->nr_rates == nr_rates) {
  467. kfree(table);
  468. return 0;
  469. }
  470. kfree(fp->rate_table);
  471. fp->rate_table = table;
  472. fp->nr_rates = nr_rates;
  473. return 0;
  474. }
  475. /*
  476. * parse the format descriptor and stores the possible sample rates
  477. * on the audioformat table (audio class v2 and v3).
  478. */
  479. static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
  480. struct audioformat *fp)
  481. {
  482. struct usb_device *dev = chip->dev;
  483. unsigned char tmp[2], *data;
  484. int nr_triplets, data_size, ret = 0, ret_l6;
  485. int clock = snd_usb_clock_find_source(chip, fp, false);
  486. struct usb_host_interface *ctrl_intf;
  487. ctrl_intf = snd_usb_find_ctrl_interface(chip, fp->iface);
  488. if (clock < 0) {
  489. dev_err(&dev->dev,
  490. "%s(): unable to find clock source (clock %d)\n",
  491. __func__, clock);
  492. goto err;
  493. }
  494. /* get the number of sample rates first by only fetching 2 bytes */
  495. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  496. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  497. UAC2_CS_CONTROL_SAM_FREQ << 8,
  498. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  499. tmp, sizeof(tmp));
  500. if (ret < 0) {
  501. /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
  502. ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
  503. if (ret_l6 == -ENODEV) {
  504. /* no line6 device found continue showing the error */
  505. dev_err(&dev->dev,
  506. "%s(): unable to retrieve number of sample rates (clock %d)\n",
  507. __func__, clock);
  508. goto err;
  509. }
  510. if (ret_l6 == 0) {
  511. dev_info(&dev->dev,
  512. "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
  513. __func__, clock);
  514. return 0;
  515. }
  516. ret = ret_l6;
  517. goto err;
  518. }
  519. nr_triplets = (tmp[1] << 8) | tmp[0];
  520. data_size = 2 + 12 * nr_triplets;
  521. data = kzalloc(data_size, GFP_KERNEL);
  522. if (!data) {
  523. ret = -ENOMEM;
  524. goto err;
  525. }
  526. /* now get the full information */
  527. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  528. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  529. UAC2_CS_CONTROL_SAM_FREQ << 8,
  530. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  531. data, data_size);
  532. if (ret < 0) {
  533. dev_err(&dev->dev,
  534. "%s(): unable to retrieve sample rate range (clock %d)\n",
  535. __func__, clock);
  536. ret = -EINVAL;
  537. goto err_free;
  538. }
  539. /* Call the triplet parser, and make sure fp->rate_table is NULL.
  540. * We just use the return value to know how many sample rates we
  541. * will have to deal with. */
  542. kfree(fp->rate_table);
  543. fp->rate_table = NULL;
  544. fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  545. if (fp->nr_rates == 0) {
  546. /* SNDRV_PCM_RATE_CONTINUOUS */
  547. ret = 0;
  548. goto err_free;
  549. }
  550. fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
  551. if (!fp->rate_table) {
  552. ret = -ENOMEM;
  553. goto err_free;
  554. }
  555. /* Call the triplet parser again, but this time, fp->rate_table is
  556. * allocated, so the rates will be stored */
  557. parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  558. ret = validate_sample_rate_table_v2v3(chip, fp, clock);
  559. if (ret < 0)
  560. goto err_free;
  561. set_rate_table_min_max(fp);
  562. err_free:
  563. kfree(data);
  564. err:
  565. return ret;
  566. }
  567. /*
  568. * parse the format type I and III descriptors
  569. */
  570. static int parse_audio_format_i(struct snd_usb_audio *chip,
  571. struct audioformat *fp, u64 format,
  572. void *_fmt)
  573. {
  574. snd_pcm_format_t pcm_format;
  575. unsigned int fmt_type;
  576. int ret;
  577. switch (fp->protocol) {
  578. default:
  579. case UAC_VERSION_1:
  580. case UAC_VERSION_2: {
  581. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  582. fmt_type = fmt->bFormatType;
  583. break;
  584. }
  585. case UAC_VERSION_3: {
  586. /* fp->fmt_type is already set in this case */
  587. fmt_type = fp->fmt_type;
  588. break;
  589. }
  590. }
  591. if (fmt_type == UAC_FORMAT_TYPE_III) {
  592. /* FIXME: the format type is really IECxxx
  593. * but we give normal PCM format to get the existing
  594. * apps working...
  595. */
  596. switch (chip->usb_id) {
  597. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  598. if (chip->setup == 0x00 &&
  599. fp->altsetting == 6)
  600. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  601. else
  602. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  603. break;
  604. default:
  605. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  606. }
  607. fp->formats = pcm_format_to_bits(pcm_format);
  608. } else {
  609. fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
  610. if (!fp->formats)
  611. return -EINVAL;
  612. }
  613. /* gather possible sample rates */
  614. /* audio class v1 reports possible sample rates as part of the
  615. * proprietary class specific descriptor.
  616. * audio class v2 uses class specific EP0 range requests for that.
  617. */
  618. switch (fp->protocol) {
  619. default:
  620. case UAC_VERSION_1: {
  621. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  622. fp->channels = fmt->bNrChannels;
  623. ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
  624. break;
  625. }
  626. case UAC_VERSION_2:
  627. case UAC_VERSION_3: {
  628. /* fp->channels is already set in this case */
  629. ret = parse_audio_format_rates_v2v3(chip, fp);
  630. break;
  631. }
  632. }
  633. if (fp->channels < 1) {
  634. usb_audio_err(chip,
  635. "%u:%d : invalid channels %d\n",
  636. fp->iface, fp->altsetting, fp->channels);
  637. return -EINVAL;
  638. }
  639. return ret;
  640. }
  641. /*
  642. * parse the format type II descriptor
  643. */
  644. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  645. struct audioformat *fp,
  646. u64 format, void *_fmt)
  647. {
  648. int brate, framesize, ret;
  649. switch (format) {
  650. case UAC_FORMAT_TYPE_II_AC3:
  651. /* FIXME: there is no AC3 format defined yet */
  652. // fp->formats = SNDRV_PCM_FMTBIT_AC3;
  653. fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
  654. break;
  655. case UAC_FORMAT_TYPE_II_MPEG:
  656. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  657. break;
  658. default:
  659. usb_audio_info(chip,
  660. "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n",
  661. fp->iface, fp->altsetting, format);
  662. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  663. break;
  664. }
  665. fp->channels = 1;
  666. switch (fp->protocol) {
  667. default:
  668. case UAC_VERSION_1: {
  669. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  670. brate = le16_to_cpu(fmt->wMaxBitRate);
  671. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  672. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  673. fp->frame_size = framesize;
  674. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  675. break;
  676. }
  677. case UAC_VERSION_2: {
  678. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  679. brate = le16_to_cpu(fmt->wMaxBitRate);
  680. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  681. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  682. fp->frame_size = framesize;
  683. ret = parse_audio_format_rates_v2v3(chip, fp);
  684. break;
  685. }
  686. }
  687. return ret;
  688. }
  689. int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
  690. struct audioformat *fp, u64 format,
  691. struct uac_format_type_i_continuous_descriptor *fmt,
  692. int stream)
  693. {
  694. int err;
  695. switch (fmt->bFormatType) {
  696. case UAC_FORMAT_TYPE_I:
  697. case UAC_FORMAT_TYPE_III:
  698. err = parse_audio_format_i(chip, fp, format, fmt);
  699. break;
  700. case UAC_FORMAT_TYPE_II:
  701. err = parse_audio_format_ii(chip, fp, format, fmt);
  702. break;
  703. default:
  704. usb_audio_info(chip,
  705. "%u:%d : format type %d is not supported yet\n",
  706. fp->iface, fp->altsetting,
  707. fmt->bFormatType);
  708. return -ENOTSUPP;
  709. }
  710. fp->fmt_type = fmt->bFormatType;
  711. if (err < 0)
  712. return err;
  713. #if 1
  714. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  715. /* extigy apparently supports sample rates other than 48k
  716. * but not in ordinary way. so we enable only 48k atm.
  717. */
  718. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  719. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  720. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  721. if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
  722. fp->rates != SNDRV_PCM_RATE_48000 &&
  723. fp->rates != SNDRV_PCM_RATE_96000)
  724. return -ENOTSUPP;
  725. }
  726. #endif
  727. return 0;
  728. }
  729. int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
  730. struct audioformat *fp,
  731. struct uac3_as_header_descriptor *as,
  732. int stream)
  733. {
  734. u64 format = le64_to_cpu(as->bmFormats);
  735. int err;
  736. /*
  737. * Type I format bits are D0..D6
  738. * This test works because type IV is not supported
  739. */
  740. if (format & 0x7f)
  741. fp->fmt_type = UAC_FORMAT_TYPE_I;
  742. else
  743. fp->fmt_type = UAC_FORMAT_TYPE_III;
  744. err = parse_audio_format_i(chip, fp, format, as);
  745. if (err < 0)
  746. return err;
  747. return 0;
  748. }