format.c 12 KB

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