format.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. chip->usb_id == USB_ID(0x0b0e, 0x030c)) {
  240. /* only 48kHz for playback while keeping 16kHz for capture */
  241. if (fp->nr_rates != 1)
  242. return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
  243. }
  244. return 0;
  245. }
  246. /*
  247. * Presonus Studio 1810c supports a limited set of sampling
  248. * rates per altsetting but reports the full set each time.
  249. * If we don't filter out the unsupported rates and attempt
  250. * to configure the card, it will hang refusing to do any
  251. * further audio I/O until a hard reset is performed.
  252. *
  253. * The list of supported rates per altsetting (set of available
  254. * I/O channels) is described in the owner's manual, section 2.2.
  255. */
  256. static bool s1810c_valid_sample_rate(struct audioformat *fp,
  257. unsigned int rate)
  258. {
  259. switch (fp->altsetting) {
  260. case 1:
  261. /* All ADAT ports available */
  262. return rate <= 48000;
  263. case 2:
  264. /* Half of ADAT ports available */
  265. return (rate == 88200 || rate == 96000);
  266. case 3:
  267. /* Analog I/O only (no S/PDIF nor ADAT) */
  268. return rate >= 176400;
  269. default:
  270. return false;
  271. }
  272. return false;
  273. }
  274. /*
  275. * Many Focusrite devices supports a limited set of sampling rates per
  276. * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
  277. * descriptor which has a non-standard bLength = 10.
  278. */
  279. static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
  280. struct audioformat *fp,
  281. unsigned int rate)
  282. {
  283. struct usb_interface *iface;
  284. struct usb_host_interface *alts;
  285. unsigned char *fmt;
  286. unsigned int max_rate;
  287. iface = usb_ifnum_to_if(chip->dev, fp->iface);
  288. if (!iface)
  289. return true;
  290. alts = &iface->altsetting[fp->altset_idx];
  291. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  292. NULL, UAC_FORMAT_TYPE);
  293. if (!fmt)
  294. return true;
  295. if (fmt[0] == 10) { /* bLength */
  296. max_rate = combine_quad(&fmt[6]);
  297. /* Validate max rate */
  298. if (max_rate != 48000 &&
  299. max_rate != 96000 &&
  300. max_rate != 192000 &&
  301. max_rate != 384000) {
  302. usb_audio_info(chip,
  303. "%u:%d : unexpected max rate: %u\n",
  304. fp->iface, fp->altsetting, max_rate);
  305. return true;
  306. }
  307. return rate <= max_rate;
  308. }
  309. return true;
  310. }
  311. /*
  312. * Helper function to walk the array of sample rate triplets reported by
  313. * the device. The problem is that we need to parse whole array first to
  314. * get to know how many sample rates we have to expect.
  315. * Then fp->rate_table can be allocated and filled.
  316. */
  317. static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
  318. struct audioformat *fp, int nr_triplets,
  319. const unsigned char *data)
  320. {
  321. int i, nr_rates = 0;
  322. for (i = 0; i < nr_triplets; i++) {
  323. int min = combine_quad(&data[2 + 12 * i]);
  324. int max = combine_quad(&data[6 + 12 * i]);
  325. int res = combine_quad(&data[10 + 12 * i]);
  326. unsigned int rate;
  327. if ((max < 0) || (min < 0) || (res < 0) || (max < min))
  328. continue;
  329. /*
  330. * for ranges with res == 1, we announce a continuous sample
  331. * rate range, and this function should return 0 for no further
  332. * parsing.
  333. */
  334. if (res == 1) {
  335. fp->rate_min = min;
  336. fp->rate_max = max;
  337. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  338. return 0;
  339. }
  340. for (rate = min; rate <= max; rate += res) {
  341. /* Filter out invalid rates on Presonus Studio 1810c */
  342. if (chip->usb_id == USB_ID(0x194f, 0x010c) &&
  343. !s1810c_valid_sample_rate(fp, rate))
  344. goto skip_rate;
  345. /* Filter out invalid rates on Focusrite devices */
  346. if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
  347. !focusrite_valid_sample_rate(chip, fp, rate))
  348. goto skip_rate;
  349. if (fp->rate_table)
  350. fp->rate_table[nr_rates] = rate;
  351. nr_rates++;
  352. if (nr_rates >= MAX_NR_RATES) {
  353. usb_audio_err(chip, "invalid uac2 rates\n");
  354. break;
  355. }
  356. skip_rate:
  357. /* avoid endless loop */
  358. if (res == 0)
  359. break;
  360. }
  361. }
  362. return nr_rates;
  363. }
  364. /* Line6 Helix series and the Rode Rodecaster Pro don't support the
  365. * UAC2_CS_RANGE usb function call. Return a static table of known
  366. * clock rates.
  367. */
  368. static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
  369. struct audioformat *fp)
  370. {
  371. switch (chip->usb_id) {
  372. case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
  373. case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
  374. case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
  375. case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
  376. case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */
  377. case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
  378. case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
  379. case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
  380. case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
  381. case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */
  382. case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
  383. return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
  384. }
  385. return -ENODEV;
  386. }
  387. /* check whether the given altsetting is supported for the already set rate */
  388. static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
  389. int altsetting)
  390. {
  391. struct usb_device *dev = chip->dev;
  392. __le64 raw_data = 0;
  393. u64 data;
  394. int err;
  395. /* we assume 64bit is enough for any altsettings */
  396. if (snd_BUG_ON(altsetting >= 64 - 8))
  397. return false;
  398. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  399. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  400. UAC2_AS_VAL_ALT_SETTINGS << 8,
  401. iface, &raw_data, sizeof(raw_data));
  402. if (err < 0)
  403. return false;
  404. data = le64_to_cpu(raw_data);
  405. /* first byte contains the bitmap size */
  406. if ((data & 0xff) * 8 < altsetting)
  407. return false;
  408. if (data & (1ULL << (altsetting + 8)))
  409. return true;
  410. return false;
  411. }
  412. /*
  413. * Validate each sample rate with the altsetting
  414. * Rebuild the rate table if only partial values are valid
  415. */
  416. static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
  417. struct audioformat *fp,
  418. int clock)
  419. {
  420. struct usb_device *dev = chip->dev;
  421. struct usb_host_interface *alts;
  422. unsigned int *table;
  423. unsigned int nr_rates;
  424. int i, err;
  425. u32 bmControls;
  426. /* performing the rate verification may lead to unexpected USB bus
  427. * behavior afterwards by some unknown reason. Do this only for the
  428. * known devices.
  429. */
  430. if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES))
  431. return 0; /* don't perform the validation as default */
  432. alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting);
  433. if (!alts)
  434. return 0;
  435. if (fp->protocol == UAC_VERSION_3) {
  436. struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc(
  437. alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  438. bmControls = le32_to_cpu(as->bmControls);
  439. } else {
  440. struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc(
  441. alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  442. bmControls = as->bmControls;
  443. }
  444. if (!uac_v2v3_control_is_readable(bmControls,
  445. UAC2_AS_VAL_ALT_SETTINGS))
  446. return 0;
  447. table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
  448. if (!table)
  449. return -ENOMEM;
  450. /* clear the interface altsetting at first */
  451. usb_set_interface(dev, fp->iface, 0);
  452. nr_rates = 0;
  453. for (i = 0; i < fp->nr_rates; i++) {
  454. err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
  455. fp->rate_table[i]);
  456. if (err < 0)
  457. continue;
  458. if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
  459. table[nr_rates++] = fp->rate_table[i];
  460. }
  461. if (!nr_rates) {
  462. usb_audio_dbg(chip,
  463. "No valid sample rate available for %d:%d, assuming a firmware bug\n",
  464. fp->iface, fp->altsetting);
  465. nr_rates = fp->nr_rates; /* continue as is */
  466. }
  467. if (fp->nr_rates == nr_rates) {
  468. kfree(table);
  469. return 0;
  470. }
  471. kfree(fp->rate_table);
  472. fp->rate_table = table;
  473. fp->nr_rates = nr_rates;
  474. return 0;
  475. }
  476. /*
  477. * parse the format descriptor and stores the possible sample rates
  478. * on the audioformat table (audio class v2 and v3).
  479. */
  480. static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
  481. struct audioformat *fp)
  482. {
  483. struct usb_device *dev = chip->dev;
  484. unsigned char tmp[2], *data;
  485. int nr_triplets, data_size, ret = 0, ret_l6;
  486. int clock = snd_usb_clock_find_source(chip, fp, false);
  487. struct usb_host_interface *ctrl_intf;
  488. ctrl_intf = snd_usb_find_ctrl_interface(chip, fp->iface);
  489. if (clock < 0) {
  490. dev_err(&dev->dev,
  491. "%s(): unable to find clock source (clock %d)\n",
  492. __func__, clock);
  493. goto err;
  494. }
  495. /* get the number of sample rates first by only fetching 2 bytes */
  496. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  497. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  498. UAC2_CS_CONTROL_SAM_FREQ << 8,
  499. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  500. tmp, sizeof(tmp));
  501. if (ret < 0) {
  502. /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
  503. ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
  504. if (ret_l6 == -ENODEV) {
  505. /* no line6 device found continue showing the error */
  506. dev_err(&dev->dev,
  507. "%s(): unable to retrieve number of sample rates (clock %d)\n",
  508. __func__, clock);
  509. goto err;
  510. }
  511. if (ret_l6 == 0) {
  512. dev_info(&dev->dev,
  513. "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
  514. __func__, clock);
  515. return 0;
  516. }
  517. ret = ret_l6;
  518. goto err;
  519. }
  520. nr_triplets = (tmp[1] << 8) | tmp[0];
  521. data_size = 2 + 12 * nr_triplets;
  522. data = kzalloc(data_size, GFP_KERNEL);
  523. if (!data) {
  524. ret = -ENOMEM;
  525. goto err;
  526. }
  527. /* now get the full information */
  528. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  529. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  530. UAC2_CS_CONTROL_SAM_FREQ << 8,
  531. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  532. data, data_size);
  533. if (ret < 0) {
  534. dev_err(&dev->dev,
  535. "%s(): unable to retrieve sample rate range (clock %d)\n",
  536. __func__, clock);
  537. ret = -EINVAL;
  538. goto err_free;
  539. }
  540. /* Call the triplet parser, and make sure fp->rate_table is NULL.
  541. * We just use the return value to know how many sample rates we
  542. * will have to deal with. */
  543. kfree(fp->rate_table);
  544. fp->rate_table = NULL;
  545. fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  546. if (fp->nr_rates == 0) {
  547. /* SNDRV_PCM_RATE_CONTINUOUS */
  548. ret = 0;
  549. goto err_free;
  550. }
  551. fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
  552. if (!fp->rate_table) {
  553. ret = -ENOMEM;
  554. goto err_free;
  555. }
  556. /* Call the triplet parser again, but this time, fp->rate_table is
  557. * allocated, so the rates will be stored */
  558. parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  559. ret = validate_sample_rate_table_v2v3(chip, fp, clock);
  560. if (ret < 0)
  561. goto err_free;
  562. set_rate_table_min_max(fp);
  563. err_free:
  564. kfree(data);
  565. err:
  566. return ret;
  567. }
  568. /*
  569. * parse the format type I and III descriptors
  570. */
  571. static int parse_audio_format_i(struct snd_usb_audio *chip,
  572. struct audioformat *fp, u64 format,
  573. void *_fmt)
  574. {
  575. snd_pcm_format_t pcm_format;
  576. unsigned int fmt_type;
  577. int ret;
  578. switch (fp->protocol) {
  579. default:
  580. case UAC_VERSION_1:
  581. case UAC_VERSION_2: {
  582. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  583. fmt_type = fmt->bFormatType;
  584. break;
  585. }
  586. case UAC_VERSION_3: {
  587. /* fp->fmt_type is already set in this case */
  588. fmt_type = fp->fmt_type;
  589. break;
  590. }
  591. }
  592. if (fmt_type == UAC_FORMAT_TYPE_III) {
  593. /* FIXME: the format type is really IECxxx
  594. * but we give normal PCM format to get the existing
  595. * apps working...
  596. */
  597. switch (chip->usb_id) {
  598. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  599. if (chip->setup == 0x00 &&
  600. fp->altsetting == 6)
  601. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  602. else
  603. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  604. break;
  605. default:
  606. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  607. }
  608. fp->formats = pcm_format_to_bits(pcm_format);
  609. } else {
  610. fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
  611. if (!fp->formats)
  612. return -EINVAL;
  613. }
  614. /* gather possible sample rates */
  615. /* audio class v1 reports possible sample rates as part of the
  616. * proprietary class specific descriptor.
  617. * audio class v2 uses class specific EP0 range requests for that.
  618. */
  619. switch (fp->protocol) {
  620. default:
  621. case UAC_VERSION_1: {
  622. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  623. fp->channels = fmt->bNrChannels;
  624. ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
  625. break;
  626. }
  627. case UAC_VERSION_2:
  628. case UAC_VERSION_3: {
  629. /* fp->channels is already set in this case */
  630. ret = parse_audio_format_rates_v2v3(chip, fp);
  631. break;
  632. }
  633. }
  634. if (fp->channels < 1) {
  635. usb_audio_err(chip,
  636. "%u:%d : invalid channels %d\n",
  637. fp->iface, fp->altsetting, fp->channels);
  638. return -EINVAL;
  639. }
  640. return ret;
  641. }
  642. /*
  643. * parse the format type II descriptor
  644. */
  645. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  646. struct audioformat *fp,
  647. u64 format, void *_fmt)
  648. {
  649. int brate, framesize, ret;
  650. switch (format) {
  651. case UAC_FORMAT_TYPE_II_AC3:
  652. /* FIXME: there is no AC3 format defined yet */
  653. // fp->formats = SNDRV_PCM_FMTBIT_AC3;
  654. fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
  655. break;
  656. case UAC_FORMAT_TYPE_II_MPEG:
  657. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  658. break;
  659. default:
  660. usb_audio_info(chip,
  661. "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n",
  662. fp->iface, fp->altsetting, format);
  663. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  664. break;
  665. }
  666. fp->channels = 1;
  667. switch (fp->protocol) {
  668. default:
  669. case UAC_VERSION_1: {
  670. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  671. brate = le16_to_cpu(fmt->wMaxBitRate);
  672. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  673. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  674. fp->frame_size = framesize;
  675. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  676. break;
  677. }
  678. case UAC_VERSION_2: {
  679. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  680. brate = le16_to_cpu(fmt->wMaxBitRate);
  681. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  682. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  683. fp->frame_size = framesize;
  684. ret = parse_audio_format_rates_v2v3(chip, fp);
  685. break;
  686. }
  687. }
  688. return ret;
  689. }
  690. int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
  691. struct audioformat *fp, u64 format,
  692. struct uac_format_type_i_continuous_descriptor *fmt,
  693. int stream)
  694. {
  695. int err;
  696. switch (fmt->bFormatType) {
  697. case UAC_FORMAT_TYPE_I:
  698. case UAC_FORMAT_TYPE_III:
  699. err = parse_audio_format_i(chip, fp, format, fmt);
  700. break;
  701. case UAC_FORMAT_TYPE_II:
  702. err = parse_audio_format_ii(chip, fp, format, fmt);
  703. break;
  704. default:
  705. usb_audio_info(chip,
  706. "%u:%d : format type %d is not supported yet\n",
  707. fp->iface, fp->altsetting,
  708. fmt->bFormatType);
  709. return -ENOTSUPP;
  710. }
  711. fp->fmt_type = fmt->bFormatType;
  712. if (err < 0)
  713. return err;
  714. #if 1
  715. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  716. /* extigy apparently supports sample rates other than 48k
  717. * but not in ordinary way. so we enable only 48k atm.
  718. */
  719. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  720. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  721. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  722. if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
  723. fp->rates != SNDRV_PCM_RATE_48000 &&
  724. fp->rates != SNDRV_PCM_RATE_96000)
  725. return -ENOTSUPP;
  726. }
  727. #endif
  728. return 0;
  729. }
  730. int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
  731. struct audioformat *fp,
  732. struct uac3_as_header_descriptor *as,
  733. int stream)
  734. {
  735. u64 format = le64_to_cpu(as->bmFormats);
  736. int err;
  737. /*
  738. * Type I format bits are D0..D6
  739. * This test works because type IV is not supported
  740. */
  741. if (format & 0x7f)
  742. fp->fmt_type = UAC_FORMAT_TYPE_I;
  743. else
  744. fp->fmt_type = UAC_FORMAT_TYPE_III;
  745. err = parse_audio_format_i(chip, fp, format, as);
  746. if (err < 0)
  747. return err;
  748. return 0;
  749. }