format.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/usb.h>
  19. #include <linux/usb/audio.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include "usbaudio.h"
  23. #include "card.h"
  24. #include "quirks.h"
  25. #include "helper.h"
  26. #include "debug.h"
  27. /*
  28. * parse the audio format type I descriptor
  29. * and returns the corresponding pcm format
  30. *
  31. * @dev: usb device
  32. * @fp: audioformat record
  33. * @format: the format tag (wFormatTag)
  34. * @fmt: the format type descriptor
  35. */
  36. static int parse_audio_format_i_type(struct snd_usb_audio *chip,
  37. struct audioformat *fp,
  38. int format, void *_fmt,
  39. int protocol)
  40. {
  41. int pcm_format, i;
  42. int sample_width, sample_bytes;
  43. switch (protocol) {
  44. case UAC_VERSION_1: {
  45. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  46. sample_width = fmt->bBitResolution;
  47. sample_bytes = fmt->bSubframeSize;
  48. break;
  49. }
  50. case UAC_VERSION_2: {
  51. struct uac_format_type_i_ext_descriptor *fmt = _fmt;
  52. sample_width = fmt->bBitResolution;
  53. sample_bytes = fmt->bSubslotSize;
  54. /*
  55. * FIXME
  56. * USB audio class v2 devices specify a bitmap of possible
  57. * audio formats rather than one fix value. For now, we just
  58. * pick one of them and report that as the only possible
  59. * value for this setting.
  60. * The bit allocation map is in fact compatible to the
  61. * wFormatTag of the v1 AS streaming descriptors, which is why
  62. * we can simply map the matrix.
  63. */
  64. for (i = 0; i < 5; i++)
  65. if (format & (1UL << i)) {
  66. format = i + 1;
  67. break;
  68. }
  69. break;
  70. }
  71. default:
  72. return -EINVAL;
  73. }
  74. /* FIXME: correct endianess and sign? */
  75. pcm_format = -1;
  76. switch (format) {
  77. case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */
  78. snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
  79. chip->dev->devnum, fp->iface, fp->altsetting);
  80. /* fall-through */
  81. case UAC_FORMAT_TYPE_I_PCM:
  82. if (sample_width > sample_bytes * 8) {
  83. snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
  84. chip->dev->devnum, fp->iface, fp->altsetting,
  85. sample_width, sample_bytes);
  86. }
  87. /* check the format byte size */
  88. switch (sample_bytes) {
  89. case 1:
  90. pcm_format = SNDRV_PCM_FORMAT_S8;
  91. break;
  92. case 2:
  93. if (snd_usb_is_big_endian_format(chip, fp))
  94. pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
  95. else
  96. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  97. break;
  98. case 3:
  99. if (snd_usb_is_big_endian_format(chip, fp))
  100. pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
  101. else
  102. pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
  103. break;
  104. case 4:
  105. pcm_format = SNDRV_PCM_FORMAT_S32_LE;
  106. break;
  107. default:
  108. snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
  109. chip->dev->devnum, fp->iface, fp->altsetting,
  110. sample_width, sample_bytes);
  111. break;
  112. }
  113. break;
  114. case UAC_FORMAT_TYPE_I_PCM8:
  115. pcm_format = SNDRV_PCM_FORMAT_U8;
  116. /* Dallas DS4201 workaround: it advertises U8 format, but really
  117. supports S8. */
  118. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  119. pcm_format = SNDRV_PCM_FORMAT_S8;
  120. break;
  121. case UAC_FORMAT_TYPE_I_IEEE_FLOAT:
  122. pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
  123. break;
  124. case UAC_FORMAT_TYPE_I_ALAW:
  125. pcm_format = SNDRV_PCM_FORMAT_A_LAW;
  126. break;
  127. case UAC_FORMAT_TYPE_I_MULAW:
  128. pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
  129. break;
  130. default:
  131. snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
  132. chip->dev->devnum, fp->iface, fp->altsetting, format);
  133. break;
  134. }
  135. return pcm_format;
  136. }
  137. /*
  138. * parse the format descriptor and stores the possible sample rates
  139. * on the audioformat table (audio class v1).
  140. *
  141. * @dev: usb device
  142. * @fp: audioformat record
  143. * @fmt: the format descriptor
  144. * @offset: the start offset of descriptor pointing the rate type
  145. * (7 for type I and II, 8 for type II)
  146. */
  147. static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  148. unsigned char *fmt, int offset)
  149. {
  150. int nr_rates = fmt[offset];
  151. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  152. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  153. chip->dev->devnum, fp->iface, fp->altsetting);
  154. return -1;
  155. }
  156. if (nr_rates) {
  157. /*
  158. * build the rate table and bitmap flags
  159. */
  160. int r, idx;
  161. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  162. if (fp->rate_table == NULL) {
  163. snd_printk(KERN_ERR "cannot malloc\n");
  164. return -1;
  165. }
  166. fp->nr_rates = 0;
  167. fp->rate_min = fp->rate_max = 0;
  168. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  169. unsigned int rate = combine_triple(&fmt[idx]);
  170. if (!rate)
  171. continue;
  172. /* C-Media CM6501 mislabels its 96 kHz altsetting */
  173. if (rate == 48000 && nr_rates == 1 &&
  174. (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
  175. chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
  176. fp->altsetting == 5 && fp->maxpacksize == 392)
  177. rate = 96000;
  178. /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
  179. if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
  180. rate = 8000;
  181. fp->rate_table[fp->nr_rates] = rate;
  182. if (!fp->rate_min || rate < fp->rate_min)
  183. fp->rate_min = rate;
  184. if (!fp->rate_max || rate > fp->rate_max)
  185. fp->rate_max = rate;
  186. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  187. fp->nr_rates++;
  188. }
  189. if (!fp->nr_rates) {
  190. hwc_debug("All rates were zero. Skipping format!\n");
  191. return -1;
  192. }
  193. } else {
  194. /* continuous rates */
  195. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  196. fp->rate_min = combine_triple(&fmt[offset + 1]);
  197. fp->rate_max = combine_triple(&fmt[offset + 4]);
  198. }
  199. return 0;
  200. }
  201. /*
  202. * parse the format descriptor and stores the possible sample rates
  203. * on the audioformat table (audio class v2).
  204. */
  205. static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
  206. struct audioformat *fp,
  207. struct usb_host_interface *iface)
  208. {
  209. struct usb_device *dev = chip->dev;
  210. unsigned char tmp[2], *data;
  211. int i, nr_rates, data_size, ret = 0;
  212. /* get the number of sample rates first by only fetching 2 bytes */
  213. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  214. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  215. 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);
  216. if (ret < 0) {
  217. snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");
  218. goto err;
  219. }
  220. nr_rates = (tmp[1] << 8) | tmp[0];
  221. data_size = 2 + 12 * nr_rates;
  222. data = kzalloc(data_size, GFP_KERNEL);
  223. if (!data) {
  224. ret = -ENOMEM;
  225. goto err;
  226. }
  227. /* now get the full information */
  228. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  229. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  230. 0x0100, chip->clock_id << 8, data, data_size, 1000);
  231. if (ret < 0) {
  232. snd_printk(KERN_ERR "unable to retrieve sample rate range\n");
  233. ret = -EINVAL;
  234. goto err_free;
  235. }
  236. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  237. if (!fp->rate_table) {
  238. ret = -ENOMEM;
  239. goto err_free;
  240. }
  241. fp->nr_rates = 0;
  242. fp->rate_min = fp->rate_max = 0;
  243. for (i = 0; i < nr_rates; i++) {
  244. int rate = combine_quad(&data[2 + 12 * i]);
  245. fp->rate_table[fp->nr_rates] = rate;
  246. if (!fp->rate_min || rate < fp->rate_min)
  247. fp->rate_min = rate;
  248. if (!fp->rate_max || rate > fp->rate_max)
  249. fp->rate_max = rate;
  250. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  251. fp->nr_rates++;
  252. }
  253. err_free:
  254. kfree(data);
  255. err:
  256. return ret;
  257. }
  258. /*
  259. * parse the format type I and III descriptors
  260. */
  261. static int parse_audio_format_i(struct snd_usb_audio *chip,
  262. struct audioformat *fp,
  263. int format, void *_fmt,
  264. struct usb_host_interface *iface)
  265. {
  266. struct usb_interface_descriptor *altsd = get_iface_desc(iface);
  267. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  268. int protocol = altsd->bInterfaceProtocol;
  269. int pcm_format, ret;
  270. if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
  271. /* FIXME: the format type is really IECxxx
  272. * but we give normal PCM format to get the existing
  273. * apps working...
  274. */
  275. switch (chip->usb_id) {
  276. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  277. if (chip->setup == 0x00 &&
  278. fp->altsetting == 6)
  279. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  280. else
  281. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  282. break;
  283. default:
  284. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  285. }
  286. } else {
  287. pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);
  288. if (pcm_format < 0)
  289. return -1;
  290. }
  291. fp->formats = 1uLL << pcm_format;
  292. /* gather possible sample rates */
  293. /* audio class v1 reports possible sample rates as part of the
  294. * proprietary class specific descriptor.
  295. * audio class v2 uses class specific EP0 range requests for that.
  296. */
  297. switch (protocol) {
  298. case UAC_VERSION_1:
  299. fp->channels = fmt->bNrChannels;
  300. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);
  301. break;
  302. case UAC_VERSION_2:
  303. /* fp->channels is already set in this case */
  304. ret = parse_audio_format_rates_v2(chip, fp, iface);
  305. break;
  306. }
  307. if (fp->channels < 1) {
  308. snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
  309. chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
  310. return -1;
  311. }
  312. return ret;
  313. }
  314. /*
  315. * parse the format type II descriptor
  316. */
  317. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  318. struct audioformat *fp,
  319. int format, void *_fmt,
  320. struct usb_host_interface *iface)
  321. {
  322. int brate, framesize, ret;
  323. struct usb_interface_descriptor *altsd = get_iface_desc(iface);
  324. int protocol = altsd->bInterfaceProtocol;
  325. switch (format) {
  326. case UAC_FORMAT_TYPE_II_AC3:
  327. /* FIXME: there is no AC3 format defined yet */
  328. // fp->formats = SNDRV_PCM_FMTBIT_AC3;
  329. fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
  330. break;
  331. case UAC_FORMAT_TYPE_II_MPEG:
  332. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  333. break;
  334. default:
  335. snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
  336. chip->dev->devnum, fp->iface, fp->altsetting, format);
  337. fp->formats = SNDRV_PCM_FMTBIT_MPEG;
  338. break;
  339. }
  340. fp->channels = 1;
  341. switch (protocol) {
  342. case UAC_VERSION_1: {
  343. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  344. brate = le16_to_cpu(fmt->wMaxBitRate);
  345. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  346. snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  347. fp->frame_size = framesize;
  348. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  349. break;
  350. }
  351. case UAC_VERSION_2: {
  352. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  353. brate = le16_to_cpu(fmt->wMaxBitRate);
  354. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  355. snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  356. fp->frame_size = framesize;
  357. ret = parse_audio_format_rates_v2(chip, fp, iface);
  358. break;
  359. }
  360. }
  361. return ret;
  362. }
  363. int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
  364. int format, unsigned char *fmt, int stream,
  365. struct usb_host_interface *iface)
  366. {
  367. int err;
  368. switch (fmt[3]) {
  369. case UAC_FORMAT_TYPE_I:
  370. case UAC_FORMAT_TYPE_III:
  371. err = parse_audio_format_i(chip, fp, format, fmt, iface);
  372. break;
  373. case UAC_FORMAT_TYPE_II:
  374. err = parse_audio_format_ii(chip, fp, format, fmt, iface);
  375. break;
  376. default:
  377. snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
  378. chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
  379. return -1;
  380. }
  381. fp->fmt_type = fmt[3];
  382. if (err < 0)
  383. return err;
  384. #if 1
  385. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  386. /* extigy apparently supports sample rates other than 48k
  387. * but not in ordinary way. so we enable only 48k atm.
  388. */
  389. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  390. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  391. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  392. if (fmt[3] == UAC_FORMAT_TYPE_I &&
  393. fp->rates != SNDRV_PCM_RATE_48000 &&
  394. fp->rates != SNDRV_PCM_RATE_96000)
  395. return -1;
  396. }
  397. #endif
  398. return 0;
  399. }