format.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/audio.h>
  21. #include <linux/usb/audio-v2.h>
  22. #include <linux/usb/audio-v3.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include "usbaudio.h"
  26. #include "card.h"
  27. #include "quirks.h"
  28. #include "helper.h"
  29. #include "debug.h"
  30. #include "clock.h"
  31. #include "format.h"
  32. /*
  33. * parse the audio format type I descriptor
  34. * and returns the corresponding pcm format
  35. *
  36. * @dev: usb device
  37. * @fp: audioformat record
  38. * @format: the format tag (wFormatTag)
  39. * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
  40. */
  41. static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
  42. struct audioformat *fp,
  43. u64 format, void *_fmt)
  44. {
  45. int sample_width, sample_bytes;
  46. u64 pcm_formats = 0;
  47. switch (fp->protocol) {
  48. case UAC_VERSION_1:
  49. default: {
  50. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  51. sample_width = fmt->bBitResolution;
  52. sample_bytes = fmt->bSubframeSize;
  53. format = 1ULL << format;
  54. break;
  55. }
  56. case UAC_VERSION_2: {
  57. struct uac_format_type_i_ext_descriptor *fmt = _fmt;
  58. sample_width = fmt->bBitResolution;
  59. sample_bytes = fmt->bSubslotSize;
  60. if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
  61. pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  62. /* flag potentially raw DSD capable altsettings */
  63. fp->dsd_raw = true;
  64. }
  65. format <<= 1;
  66. break;
  67. }
  68. case UAC_VERSION_3: {
  69. struct uac3_as_header_descriptor *as = _fmt;
  70. sample_width = as->bBitResolution;
  71. sample_bytes = as->bSubslotSize;
  72. if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
  73. pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  74. format <<= 1;
  75. break;
  76. }
  77. }
  78. if ((pcm_formats == 0) &&
  79. (format == 0 || format == (1 << 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 = 1 << UAC_FORMAT_TYPE_I_PCM;
  84. }
  85. if (format & (1 << 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 & (1 << 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 & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
  134. pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  135. }
  136. if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
  137. pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
  138. }
  139. if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
  140. pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
  141. }
  142. if (format & ~0x3f) {
  143. usb_audio_info(chip,
  144. "%u:%d : unsupported format bits %#llx\n",
  145. fp->iface, fp->altsetting, format);
  146. }
  147. pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
  148. return pcm_formats;
  149. }
  150. /*
  151. * parse the format descriptor and stores the possible sample rates
  152. * on the audioformat table (audio class v1).
  153. *
  154. * @dev: usb device
  155. * @fp: audioformat record
  156. * @fmt: the format descriptor
  157. * @offset: the start offset of descriptor pointing the rate type
  158. * (7 for type I and II, 8 for type II)
  159. */
  160. static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  161. unsigned char *fmt, int offset)
  162. {
  163. int nr_rates = fmt[offset];
  164. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  165. usb_audio_err(chip,
  166. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  167. fp->iface, fp->altsetting);
  168. return -EINVAL;
  169. }
  170. if (nr_rates) {
  171. /*
  172. * build the rate table and bitmap flags
  173. */
  174. int r, idx;
  175. fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
  176. GFP_KERNEL);
  177. if (fp->rate_table == NULL)
  178. return -ENOMEM;
  179. fp->nr_rates = 0;
  180. fp->rate_min = fp->rate_max = 0;
  181. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  182. unsigned int rate = combine_triple(&fmt[idx]);
  183. if (!rate)
  184. continue;
  185. /* C-Media CM6501 mislabels its 96 kHz altsetting */
  186. /* Terratec Aureon 7.1 USB C-Media 6206, too */
  187. if (rate == 48000 && nr_rates == 1 &&
  188. (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
  189. chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
  190. chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
  191. fp->altsetting == 5 && fp->maxpacksize == 392)
  192. rate = 96000;
  193. /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
  194. if (rate == 16000 &&
  195. (chip->usb_id == USB_ID(0x041e, 0x4064) ||
  196. chip->usb_id == USB_ID(0x041e, 0x4068)))
  197. rate = 8000;
  198. fp->rate_table[fp->nr_rates] = rate;
  199. if (!fp->rate_min || rate < fp->rate_min)
  200. fp->rate_min = rate;
  201. if (!fp->rate_max || rate > fp->rate_max)
  202. fp->rate_max = rate;
  203. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  204. fp->nr_rates++;
  205. }
  206. if (!fp->nr_rates) {
  207. hwc_debug("All rates were zero. Skipping format!\n");
  208. return -EINVAL;
  209. }
  210. } else {
  211. /* continuous rates */
  212. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  213. fp->rate_min = combine_triple(&fmt[offset + 1]);
  214. fp->rate_max = combine_triple(&fmt[offset + 4]);
  215. }
  216. return 0;
  217. }
  218. /*
  219. * Helper function to walk the array of sample rate triplets reported by
  220. * the device. The problem is that we need to parse whole array first to
  221. * get to know how many sample rates we have to expect.
  222. * Then fp->rate_table can be allocated and filled.
  223. */
  224. static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
  225. struct audioformat *fp, int nr_triplets,
  226. const unsigned char *data)
  227. {
  228. int i, nr_rates = 0;
  229. fp->rates = fp->rate_min = fp->rate_max = 0;
  230. for (i = 0; i < nr_triplets; i++) {
  231. int min = combine_quad(&data[2 + 12 * i]);
  232. int max = combine_quad(&data[6 + 12 * i]);
  233. int res = combine_quad(&data[10 + 12 * i]);
  234. unsigned int rate;
  235. if ((max < 0) || (min < 0) || (res < 0) || (max < min))
  236. continue;
  237. /*
  238. * for ranges with res == 1, we announce a continuous sample
  239. * rate range, and this function should return 0 for no further
  240. * parsing.
  241. */
  242. if (res == 1) {
  243. fp->rate_min = min;
  244. fp->rate_max = max;
  245. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  246. return 0;
  247. }
  248. for (rate = min; rate <= max; rate += res) {
  249. if (fp->rate_table)
  250. fp->rate_table[nr_rates] = rate;
  251. if (!fp->rate_min || rate < fp->rate_min)
  252. fp->rate_min = rate;
  253. if (!fp->rate_max || rate > fp->rate_max)
  254. fp->rate_max = rate;
  255. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  256. nr_rates++;
  257. if (nr_rates >= MAX_NR_RATES) {
  258. usb_audio_err(chip, "invalid uac2 rates\n");
  259. break;
  260. }
  261. /* avoid endless loop */
  262. if (res == 0)
  263. break;
  264. }
  265. }
  266. return nr_rates;
  267. }
  268. /*
  269. * parse the format descriptor and stores the possible sample rates
  270. * on the audioformat table (audio class v2 and v3).
  271. */
  272. static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
  273. struct audioformat *fp)
  274. {
  275. struct usb_device *dev = chip->dev;
  276. unsigned char tmp[2], *data;
  277. int nr_triplets, data_size, ret = 0;
  278. int clock = snd_usb_clock_find_source(chip, fp->protocol,
  279. fp->clock, false);
  280. if (clock < 0) {
  281. dev_err(&dev->dev,
  282. "%s(): unable to find clock source (clock %d)\n",
  283. __func__, clock);
  284. goto err;
  285. }
  286. /* get the number of sample rates first by only fetching 2 bytes */
  287. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  288. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  289. UAC2_CS_CONTROL_SAM_FREQ << 8,
  290. snd_usb_ctrl_intf(chip) | (clock << 8),
  291. tmp, sizeof(tmp));
  292. if (ret < 0) {
  293. dev_err(&dev->dev,
  294. "%s(): unable to retrieve number of sample rates (clock %d)\n",
  295. __func__, clock);
  296. goto err;
  297. }
  298. nr_triplets = (tmp[1] << 8) | tmp[0];
  299. data_size = 2 + 12 * nr_triplets;
  300. data = kzalloc(data_size, GFP_KERNEL);
  301. if (!data) {
  302. ret = -ENOMEM;
  303. goto err;
  304. }
  305. /* now get the full information */
  306. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  307. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  308. UAC2_CS_CONTROL_SAM_FREQ << 8,
  309. snd_usb_ctrl_intf(chip) | (clock << 8),
  310. data, data_size);
  311. if (ret < 0) {
  312. dev_err(&dev->dev,
  313. "%s(): unable to retrieve sample rate range (clock %d)\n",
  314. __func__, clock);
  315. ret = -EINVAL;
  316. goto err_free;
  317. }
  318. /* Call the triplet parser, and make sure fp->rate_table is NULL.
  319. * We just use the return value to know how many sample rates we
  320. * will have to deal with. */
  321. kfree(fp->rate_table);
  322. fp->rate_table = NULL;
  323. fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  324. if (fp->nr_rates == 0) {
  325. /* SNDRV_PCM_RATE_CONTINUOUS */
  326. ret = 0;
  327. goto err_free;
  328. }
  329. fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
  330. if (!fp->rate_table) {
  331. ret = -ENOMEM;
  332. goto err_free;
  333. }
  334. /* Call the triplet parser again, but this time, fp->rate_table is
  335. * allocated, so the rates will be stored */
  336. parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  337. err_free:
  338. kfree(data);
  339. err:
  340. return ret;
  341. }
  342. /*
  343. * parse the format type I and III descriptors
  344. */
  345. static int parse_audio_format_i(struct snd_usb_audio *chip,
  346. struct audioformat *fp, u64 format,
  347. void *_fmt)
  348. {
  349. snd_pcm_format_t pcm_format;
  350. unsigned int fmt_type;
  351. int ret;
  352. switch (fp->protocol) {
  353. default:
  354. case UAC_VERSION_1:
  355. case UAC_VERSION_2: {
  356. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  357. fmt_type = fmt->bFormatType;
  358. break;
  359. }
  360. case UAC_VERSION_3: {
  361. /* fp->fmt_type is already set in this case */
  362. fmt_type = fp->fmt_type;
  363. break;
  364. }
  365. }
  366. if (fmt_type == UAC_FORMAT_TYPE_III) {
  367. /* FIXME: the format type is really IECxxx
  368. * but we give normal PCM format to get the existing
  369. * apps working...
  370. */
  371. switch (chip->usb_id) {
  372. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  373. if (chip->setup == 0x00 &&
  374. fp->altsetting == 6)
  375. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  376. else
  377. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  378. break;
  379. default:
  380. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  381. }
  382. fp->formats = pcm_format_to_bits(pcm_format);
  383. } else {
  384. fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
  385. if (!fp->formats)
  386. return -EINVAL;
  387. }
  388. /* gather possible sample rates */
  389. /* audio class v1 reports possible sample rates as part of the
  390. * proprietary class specific descriptor.
  391. * audio class v2 uses class specific EP0 range requests for that.
  392. */
  393. switch (fp->protocol) {
  394. default:
  395. case UAC_VERSION_1: {
  396. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  397. fp->channels = fmt->bNrChannels;
  398. ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
  399. break;
  400. }
  401. case UAC_VERSION_2:
  402. case UAC_VERSION_3: {
  403. /* fp->channels is already set in this case */
  404. ret = parse_audio_format_rates_v2v3(chip, fp);
  405. break;
  406. }
  407. }
  408. if (fp->channels < 1) {
  409. usb_audio_err(chip,
  410. "%u:%d : invalid channels %d\n",
  411. fp->iface, fp->altsetting, fp->channels);
  412. return -EINVAL;
  413. }
  414. return ret;
  415. }
  416. /*
  417. * parse the format type II descriptor
  418. */
  419. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  420. struct audioformat *fp,
  421. u64 format, void *_fmt)
  422. {
  423. int brate, framesize, ret;
  424. switch (format) {
  425. case UAC_FORMAT_TYPE_II_AC3:
  426. /* FIXME: there is no AC3 format defined yet */
  427. // fp->formats = SNDRV_PCM_FMTBIT_AC3;
  428. fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
  429. break;
  430. case UAC_FORMAT_TYPE_II_MPEG:
  431. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  432. break;
  433. default:
  434. usb_audio_info(chip,
  435. "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n",
  436. fp->iface, fp->altsetting, format);
  437. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  438. break;
  439. }
  440. fp->channels = 1;
  441. switch (fp->protocol) {
  442. default:
  443. case UAC_VERSION_1: {
  444. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  445. brate = le16_to_cpu(fmt->wMaxBitRate);
  446. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  447. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  448. fp->frame_size = framesize;
  449. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  450. break;
  451. }
  452. case UAC_VERSION_2: {
  453. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  454. brate = le16_to_cpu(fmt->wMaxBitRate);
  455. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  456. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  457. fp->frame_size = framesize;
  458. ret = parse_audio_format_rates_v2v3(chip, fp);
  459. break;
  460. }
  461. }
  462. return ret;
  463. }
  464. int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
  465. struct audioformat *fp, u64 format,
  466. struct uac_format_type_i_continuous_descriptor *fmt,
  467. int stream)
  468. {
  469. int err;
  470. switch (fmt->bFormatType) {
  471. case UAC_FORMAT_TYPE_I:
  472. case UAC_FORMAT_TYPE_III:
  473. err = parse_audio_format_i(chip, fp, format, fmt);
  474. break;
  475. case UAC_FORMAT_TYPE_II:
  476. err = parse_audio_format_ii(chip, fp, format, fmt);
  477. break;
  478. default:
  479. usb_audio_info(chip,
  480. "%u:%d : format type %d is not supported yet\n",
  481. fp->iface, fp->altsetting,
  482. fmt->bFormatType);
  483. return -ENOTSUPP;
  484. }
  485. fp->fmt_type = fmt->bFormatType;
  486. if (err < 0)
  487. return err;
  488. #if 1
  489. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  490. /* extigy apparently supports sample rates other than 48k
  491. * but not in ordinary way. so we enable only 48k atm.
  492. */
  493. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  494. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  495. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  496. if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
  497. fp->rates != SNDRV_PCM_RATE_48000 &&
  498. fp->rates != SNDRV_PCM_RATE_96000)
  499. return -ENOTSUPP;
  500. }
  501. #endif
  502. return 0;
  503. }
  504. int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
  505. struct audioformat *fp,
  506. struct uac3_as_header_descriptor *as,
  507. int stream)
  508. {
  509. u64 format = le64_to_cpu(as->bmFormats);
  510. int err;
  511. /*
  512. * Type I format bits are D0..D6
  513. * This test works because type IV is not supported
  514. */
  515. if (format & 0x7f)
  516. fp->fmt_type = UAC_FORMAT_TYPE_I;
  517. else
  518. fp->fmt_type = UAC_FORMAT_TYPE_III;
  519. err = parse_audio_format_i(chip, fp, format, as);
  520. if (err < 0)
  521. return err;
  522. return 0;
  523. }