format.c 22 KB

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