format.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. format <<= 1;
  63. break;
  64. }
  65. case UAC_VERSION_3: {
  66. struct uac3_as_header_descriptor *as = _fmt;
  67. sample_width = as->bBitResolution;
  68. sample_bytes = as->bSubslotSize;
  69. if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
  70. pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  71. format <<= 1;
  72. break;
  73. }
  74. }
  75. if ((pcm_formats == 0) &&
  76. (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
  77. /* some devices don't define this correctly... */
  78. usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
  79. fp->iface, fp->altsetting);
  80. format = 1 << UAC_FORMAT_TYPE_I_PCM;
  81. }
  82. if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
  83. if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
  84. /* Edirol SD-90 */
  85. (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
  86. /* Roland SC-D70 */
  87. sample_width == 24 && sample_bytes == 2)
  88. sample_bytes = 3;
  89. else if (sample_width > sample_bytes * 8) {
  90. usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
  91. fp->iface, fp->altsetting,
  92. sample_width, sample_bytes);
  93. }
  94. /* check the format byte size */
  95. switch (sample_bytes) {
  96. case 1:
  97. pcm_formats |= SNDRV_PCM_FMTBIT_S8;
  98. break;
  99. case 2:
  100. if (snd_usb_is_big_endian_format(chip, fp))
  101. pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
  102. else
  103. pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
  104. break;
  105. case 3:
  106. if (snd_usb_is_big_endian_format(chip, fp))
  107. pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
  108. else
  109. pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
  110. break;
  111. case 4:
  112. pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
  113. break;
  114. default:
  115. usb_audio_info(chip,
  116. "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
  117. fp->iface, fp->altsetting,
  118. sample_width, sample_bytes);
  119. break;
  120. }
  121. }
  122. if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
  123. /* Dallas DS4201 workaround: it advertises U8 format, but really
  124. supports S8. */
  125. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  126. pcm_formats |= SNDRV_PCM_FMTBIT_S8;
  127. else
  128. pcm_formats |= SNDRV_PCM_FMTBIT_U8;
  129. }
  130. if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
  131. pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  132. }
  133. if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
  134. pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
  135. }
  136. if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
  137. pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
  138. }
  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. pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
  145. return pcm_formats;
  146. }
  147. /*
  148. * parse the format descriptor and stores the possible sample rates
  149. * on the audioformat table (audio class v1).
  150. *
  151. * @dev: usb device
  152. * @fp: audioformat record
  153. * @fmt: the format descriptor
  154. * @offset: the start offset of descriptor pointing the rate type
  155. * (7 for type I and II, 8 for type II)
  156. */
  157. static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  158. unsigned char *fmt, int offset)
  159. {
  160. int nr_rates = fmt[offset];
  161. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  162. usb_audio_err(chip,
  163. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  164. fp->iface, fp->altsetting);
  165. return -EINVAL;
  166. }
  167. if (nr_rates) {
  168. /*
  169. * build the rate table and bitmap flags
  170. */
  171. int r, idx;
  172. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  173. if (fp->rate_table == NULL)
  174. return -ENOMEM;
  175. fp->nr_rates = 0;
  176. fp->rate_min = fp->rate_max = 0;
  177. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  178. unsigned int rate = combine_triple(&fmt[idx]);
  179. if (!rate)
  180. continue;
  181. /* C-Media CM6501 mislabels its 96 kHz altsetting */
  182. /* Terratec Aureon 7.1 USB C-Media 6206, too */
  183. if (rate == 48000 && nr_rates == 1 &&
  184. (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
  185. chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
  186. chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
  187. fp->altsetting == 5 && fp->maxpacksize == 392)
  188. rate = 96000;
  189. /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
  190. if (rate == 16000 &&
  191. (chip->usb_id == USB_ID(0x041e, 0x4064) ||
  192. chip->usb_id == USB_ID(0x041e, 0x4068)))
  193. rate = 8000;
  194. fp->rate_table[fp->nr_rates] = rate;
  195. if (!fp->rate_min || rate < fp->rate_min)
  196. fp->rate_min = rate;
  197. if (!fp->rate_max || rate > fp->rate_max)
  198. fp->rate_max = rate;
  199. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  200. fp->nr_rates++;
  201. }
  202. if (!fp->nr_rates) {
  203. hwc_debug("All rates were zero. Skipping format!\n");
  204. return -EINVAL;
  205. }
  206. } else {
  207. /* continuous rates */
  208. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  209. fp->rate_min = combine_triple(&fmt[offset + 1]);
  210. fp->rate_max = combine_triple(&fmt[offset + 4]);
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Helper function to walk the array of sample rate triplets reported by
  216. * the device. The problem is that we need to parse whole array first to
  217. * get to know how many sample rates we have to expect.
  218. * Then fp->rate_table can be allocated and filled.
  219. */
  220. static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
  221. struct audioformat *fp, int nr_triplets,
  222. const unsigned char *data)
  223. {
  224. int i, nr_rates = 0;
  225. fp->rates = fp->rate_min = fp->rate_max = 0;
  226. for (i = 0; i < nr_triplets; i++) {
  227. int min = combine_quad(&data[2 + 12 * i]);
  228. int max = combine_quad(&data[6 + 12 * i]);
  229. int res = combine_quad(&data[10 + 12 * i]);
  230. unsigned int rate;
  231. if ((max < 0) || (min < 0) || (res < 0) || (max < min))
  232. continue;
  233. /*
  234. * for ranges with res == 1, we announce a continuous sample
  235. * rate range, and this function should return 0 for no further
  236. * parsing.
  237. */
  238. if (res == 1) {
  239. fp->rate_min = min;
  240. fp->rate_max = max;
  241. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  242. return 0;
  243. }
  244. for (rate = min; rate <= max; rate += res) {
  245. if (fp->rate_table)
  246. fp->rate_table[nr_rates] = rate;
  247. if (!fp->rate_min || rate < fp->rate_min)
  248. fp->rate_min = rate;
  249. if (!fp->rate_max || rate > fp->rate_max)
  250. fp->rate_max = rate;
  251. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  252. nr_rates++;
  253. if (nr_rates >= MAX_NR_RATES) {
  254. usb_audio_err(chip, "invalid uac2 rates\n");
  255. break;
  256. }
  257. /* avoid endless loop */
  258. if (res == 0)
  259. break;
  260. }
  261. }
  262. return nr_rates;
  263. }
  264. /*
  265. * parse the format descriptor and stores the possible sample rates
  266. * on the audioformat table (audio class v2 and v3).
  267. */
  268. static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
  269. struct audioformat *fp)
  270. {
  271. struct usb_device *dev = chip->dev;
  272. unsigned char tmp[2], *data;
  273. int nr_triplets, data_size, ret = 0;
  274. int clock = snd_usb_clock_find_source(chip, fp->protocol,
  275. fp->clock, false);
  276. if (clock < 0) {
  277. dev_err(&dev->dev,
  278. "%s(): unable to find clock source (clock %d)\n",
  279. __func__, clock);
  280. goto err;
  281. }
  282. /* get the number of sample rates first by only fetching 2 bytes */
  283. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  284. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  285. UAC2_CS_CONTROL_SAM_FREQ << 8,
  286. snd_usb_ctrl_intf(chip) | (clock << 8),
  287. tmp, sizeof(tmp));
  288. if (ret < 0) {
  289. dev_err(&dev->dev,
  290. "%s(): unable to retrieve number of sample rates (clock %d)\n",
  291. __func__, clock);
  292. goto err;
  293. }
  294. nr_triplets = (tmp[1] << 8) | tmp[0];
  295. data_size = 2 + 12 * nr_triplets;
  296. data = kzalloc(data_size, GFP_KERNEL);
  297. if (!data) {
  298. ret = -ENOMEM;
  299. goto err;
  300. }
  301. /* now get the full information */
  302. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  303. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  304. UAC2_CS_CONTROL_SAM_FREQ << 8,
  305. snd_usb_ctrl_intf(chip) | (clock << 8),
  306. data, data_size);
  307. if (ret < 0) {
  308. dev_err(&dev->dev,
  309. "%s(): unable to retrieve sample rate range (clock %d)\n",
  310. __func__, clock);
  311. ret = -EINVAL;
  312. goto err_free;
  313. }
  314. /* Call the triplet parser, and make sure fp->rate_table is NULL.
  315. * We just use the return value to know how many sample rates we
  316. * will have to deal with. */
  317. kfree(fp->rate_table);
  318. fp->rate_table = NULL;
  319. fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  320. if (fp->nr_rates == 0) {
  321. /* SNDRV_PCM_RATE_CONTINUOUS */
  322. ret = 0;
  323. goto err_free;
  324. }
  325. fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
  326. if (!fp->rate_table) {
  327. ret = -ENOMEM;
  328. goto err_free;
  329. }
  330. /* Call the triplet parser again, but this time, fp->rate_table is
  331. * allocated, so the rates will be stored */
  332. parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
  333. err_free:
  334. kfree(data);
  335. err:
  336. return ret;
  337. }
  338. /*
  339. * parse the format type I and III descriptors
  340. */
  341. static int parse_audio_format_i(struct snd_usb_audio *chip,
  342. struct audioformat *fp, u64 format,
  343. void *_fmt)
  344. {
  345. snd_pcm_format_t pcm_format;
  346. unsigned int fmt_type;
  347. int ret;
  348. switch (fp->protocol) {
  349. default:
  350. case UAC_VERSION_1:
  351. case UAC_VERSION_2: {
  352. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  353. fmt_type = fmt->bFormatType;
  354. break;
  355. }
  356. case UAC_VERSION_3: {
  357. /* fp->fmt_type is already set in this case */
  358. fmt_type = fp->fmt_type;
  359. break;
  360. }
  361. }
  362. if (fmt_type == UAC_FORMAT_TYPE_III) {
  363. /* FIXME: the format type is really IECxxx
  364. * but we give normal PCM format to get the existing
  365. * apps working...
  366. */
  367. switch (chip->usb_id) {
  368. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  369. if (chip->setup == 0x00 &&
  370. fp->altsetting == 6)
  371. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  372. else
  373. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  374. break;
  375. default:
  376. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  377. }
  378. fp->formats = pcm_format_to_bits(pcm_format);
  379. } else {
  380. fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
  381. if (!fp->formats)
  382. return -EINVAL;
  383. }
  384. /* gather possible sample rates */
  385. /* audio class v1 reports possible sample rates as part of the
  386. * proprietary class specific descriptor.
  387. * audio class v2 uses class specific EP0 range requests for that.
  388. */
  389. switch (fp->protocol) {
  390. default:
  391. case UAC_VERSION_1: {
  392. struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  393. fp->channels = fmt->bNrChannels;
  394. ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
  395. break;
  396. }
  397. case UAC_VERSION_2:
  398. case UAC_VERSION_3: {
  399. /* fp->channels is already set in this case */
  400. ret = parse_audio_format_rates_v2v3(chip, fp);
  401. break;
  402. }
  403. }
  404. if (fp->channels < 1) {
  405. usb_audio_err(chip,
  406. "%u:%d : invalid channels %d\n",
  407. fp->iface, fp->altsetting, fp->channels);
  408. return -EINVAL;
  409. }
  410. return ret;
  411. }
  412. /*
  413. * parse the format type II descriptor
  414. */
  415. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  416. struct audioformat *fp,
  417. u64 format, void *_fmt)
  418. {
  419. int brate, framesize, ret;
  420. switch (format) {
  421. case UAC_FORMAT_TYPE_II_AC3:
  422. /* FIXME: there is no AC3 format defined yet */
  423. // fp->formats = SNDRV_PCM_FMTBIT_AC3;
  424. fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
  425. break;
  426. case UAC_FORMAT_TYPE_II_MPEG:
  427. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  428. break;
  429. default:
  430. usb_audio_info(chip,
  431. "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n",
  432. fp->iface, fp->altsetting, format);
  433. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  434. break;
  435. }
  436. fp->channels = 1;
  437. switch (fp->protocol) {
  438. default:
  439. case UAC_VERSION_1: {
  440. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  441. brate = le16_to_cpu(fmt->wMaxBitRate);
  442. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  443. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  444. fp->frame_size = framesize;
  445. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  446. break;
  447. }
  448. case UAC_VERSION_2: {
  449. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  450. brate = le16_to_cpu(fmt->wMaxBitRate);
  451. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  452. usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  453. fp->frame_size = framesize;
  454. ret = parse_audio_format_rates_v2v3(chip, fp);
  455. break;
  456. }
  457. }
  458. return ret;
  459. }
  460. int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
  461. struct audioformat *fp, u64 format,
  462. struct uac_format_type_i_continuous_descriptor *fmt,
  463. int stream)
  464. {
  465. int err;
  466. switch (fmt->bFormatType) {
  467. case UAC_FORMAT_TYPE_I:
  468. case UAC_FORMAT_TYPE_III:
  469. err = parse_audio_format_i(chip, fp, format, fmt);
  470. break;
  471. case UAC_FORMAT_TYPE_II:
  472. err = parse_audio_format_ii(chip, fp, format, fmt);
  473. break;
  474. default:
  475. usb_audio_info(chip,
  476. "%u:%d : format type %d is not supported yet\n",
  477. fp->iface, fp->altsetting,
  478. fmt->bFormatType);
  479. return -ENOTSUPP;
  480. }
  481. fp->fmt_type = fmt->bFormatType;
  482. if (err < 0)
  483. return err;
  484. #if 1
  485. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  486. /* extigy apparently supports sample rates other than 48k
  487. * but not in ordinary way. so we enable only 48k atm.
  488. */
  489. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  490. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  491. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  492. if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
  493. fp->rates != SNDRV_PCM_RATE_48000 &&
  494. fp->rates != SNDRV_PCM_RATE_96000)
  495. return -ENOTSUPP;
  496. }
  497. #endif
  498. return 0;
  499. }
  500. int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
  501. struct audioformat *fp,
  502. struct uac3_as_header_descriptor *as,
  503. int stream)
  504. {
  505. u64 format = le64_to_cpu(as->bmFormats);
  506. int err;
  507. /*
  508. * Type I format bits are D0..D6
  509. * This test works because type IV is not supported
  510. */
  511. if (format & 0x7f)
  512. fp->fmt_type = UAC_FORMAT_TYPE_I;
  513. else
  514. fp->fmt_type = UAC_FORMAT_TYPE_III;
  515. err = parse_audio_format_i(chip, fp, format, as);
  516. if (err < 0)
  517. return err;
  518. return 0;
  519. }