format.c 25 KB

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