stream.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/audio.h>
  20. #include <linux/usb/audio-v2.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include "usbaudio.h"
  24. #include "card.h"
  25. #include "proc.h"
  26. #include "quirks.h"
  27. #include "endpoint.h"
  28. #include "pcm.h"
  29. #include "helper.h"
  30. #include "format.h"
  31. #include "clock.h"
  32. #include "stream.h"
  33. /*
  34. * free a substream
  35. */
  36. static void free_substream(struct snd_usb_substream *subs)
  37. {
  38. struct list_head *p, *n;
  39. if (!subs->num_formats)
  40. return; /* not initialized */
  41. list_for_each_safe(p, n, &subs->fmt_list) {
  42. struct audioformat *fp = list_entry(p, struct audioformat, list);
  43. kfree(fp->rate_table);
  44. kfree(fp);
  45. }
  46. kfree(subs->rate_list.list);
  47. }
  48. /*
  49. * free a usb stream instance
  50. */
  51. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  52. {
  53. free_substream(&stream->substream[0]);
  54. free_substream(&stream->substream[1]);
  55. list_del(&stream->list);
  56. kfree(stream);
  57. }
  58. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  59. {
  60. struct snd_usb_stream *stream = pcm->private_data;
  61. if (stream) {
  62. stream->pcm = NULL;
  63. snd_usb_audio_stream_free(stream);
  64. }
  65. }
  66. /*
  67. * add this endpoint to the chip instance.
  68. * if a stream with the same endpoint already exists, append to it.
  69. * if not, create a new pcm stream. note, fp is added to the substream
  70. * fmt_list and will be freed on the chip instance release. do not free
  71. * fp or do remove it from the substream fmt_list to avoid double-free.
  72. */
  73. int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  74. int stream,
  75. struct audioformat *fp)
  76. {
  77. struct list_head *p;
  78. struct snd_usb_stream *as;
  79. struct snd_usb_substream *subs;
  80. struct snd_pcm *pcm;
  81. int err;
  82. list_for_each(p, &chip->pcm_list) {
  83. as = list_entry(p, struct snd_usb_stream, list);
  84. if (as->fmt_type != fp->fmt_type)
  85. continue;
  86. subs = &as->substream[stream];
  87. if (!subs->endpoint)
  88. continue;
  89. if (subs->endpoint == fp->endpoint) {
  90. list_add_tail(&fp->list, &subs->fmt_list);
  91. subs->num_formats++;
  92. subs->formats |= fp->formats;
  93. return 0;
  94. }
  95. }
  96. /* look for an empty stream */
  97. list_for_each(p, &chip->pcm_list) {
  98. as = list_entry(p, struct snd_usb_stream, list);
  99. if (as->fmt_type != fp->fmt_type)
  100. continue;
  101. subs = &as->substream[stream];
  102. if (subs->endpoint)
  103. continue;
  104. err = snd_pcm_new_stream(as->pcm, stream, 1);
  105. if (err < 0)
  106. return err;
  107. snd_usb_init_substream(as, stream, fp);
  108. return 0;
  109. }
  110. /* create a new pcm */
  111. as = kzalloc(sizeof(*as), GFP_KERNEL);
  112. if (!as)
  113. return -ENOMEM;
  114. as->pcm_index = chip->pcm_devs;
  115. as->chip = chip;
  116. as->fmt_type = fp->fmt_type;
  117. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  118. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  119. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  120. &pcm);
  121. if (err < 0) {
  122. kfree(as);
  123. return err;
  124. }
  125. as->pcm = pcm;
  126. pcm->private_data = as;
  127. pcm->private_free = snd_usb_audio_pcm_free;
  128. pcm->info_flags = 0;
  129. if (chip->pcm_devs > 0)
  130. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  131. else
  132. strcpy(pcm->name, "USB Audio");
  133. snd_usb_init_substream(as, stream, fp);
  134. list_add(&as->list, &chip->pcm_list);
  135. chip->pcm_devs++;
  136. snd_usb_proc_pcm_format_add(as);
  137. return 0;
  138. }
  139. static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  140. struct usb_host_interface *alts,
  141. int protocol, int iface_no)
  142. {
  143. /* parsed with a v1 header here. that's ok as we only look at the
  144. * header first which is the same for both versions */
  145. struct uac_iso_endpoint_descriptor *csep;
  146. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  147. int attributes = 0;
  148. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  149. /* Creamware Noah has this descriptor after the 2nd endpoint */
  150. if (!csep && altsd->bNumEndpoints >= 2)
  151. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  152. /*
  153. * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
  154. * bytes after the first endpoint, go search the entire interface.
  155. * Some devices have it directly *before* the standard endpoint.
  156. */
  157. if (!csep)
  158. csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
  159. if (!csep || csep->bLength < 7 ||
  160. csep->bDescriptorSubtype != UAC_EP_GENERAL) {
  161. snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  162. " class specific endpoint descriptor\n",
  163. chip->dev->devnum, iface_no,
  164. altsd->bAlternateSetting);
  165. return 0;
  166. }
  167. if (protocol == UAC_VERSION_1) {
  168. attributes = csep->bmAttributes;
  169. } else {
  170. struct uac2_iso_endpoint_descriptor *csep2 =
  171. (struct uac2_iso_endpoint_descriptor *) csep;
  172. attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  173. /* emulate the endpoint attributes of a v1 device */
  174. if (csep2->bmControls & UAC2_CONTROL_PITCH)
  175. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  176. }
  177. return attributes;
  178. }
  179. static struct uac2_input_terminal_descriptor *
  180. snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  181. int terminal_id)
  182. {
  183. struct uac2_input_terminal_descriptor *term = NULL;
  184. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  185. ctrl_iface->extralen,
  186. term, UAC_INPUT_TERMINAL))) {
  187. if (term->bTerminalID == terminal_id)
  188. return term;
  189. }
  190. return NULL;
  191. }
  192. static struct uac2_output_terminal_descriptor *
  193. snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  194. int terminal_id)
  195. {
  196. struct uac2_output_terminal_descriptor *term = NULL;
  197. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  198. ctrl_iface->extralen,
  199. term, UAC_OUTPUT_TERMINAL))) {
  200. if (term->bTerminalID == terminal_id)
  201. return term;
  202. }
  203. return NULL;
  204. }
  205. int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  206. {
  207. struct usb_device *dev;
  208. struct usb_interface *iface;
  209. struct usb_host_interface *alts;
  210. struct usb_interface_descriptor *altsd;
  211. int i, altno, err, stream;
  212. int format = 0, num_channels = 0;
  213. struct audioformat *fp = NULL;
  214. int num, protocol, clock = 0;
  215. struct uac_format_type_i_continuous_descriptor *fmt;
  216. dev = chip->dev;
  217. /* parse the interface's altsettings */
  218. iface = usb_ifnum_to_if(dev, iface_no);
  219. num = iface->num_altsetting;
  220. /*
  221. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  222. * one misses syncpipe, and does not produce any sound.
  223. */
  224. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  225. num = 4;
  226. for (i = 0; i < num; i++) {
  227. alts = &iface->altsetting[i];
  228. altsd = get_iface_desc(alts);
  229. protocol = altsd->bInterfaceProtocol;
  230. /* skip invalid one */
  231. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  232. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  233. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  234. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  235. altsd->bNumEndpoints < 1 ||
  236. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  237. continue;
  238. /* must be isochronous */
  239. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  240. USB_ENDPOINT_XFER_ISOC)
  241. continue;
  242. /* check direction */
  243. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  244. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  245. altno = altsd->bAlternateSetting;
  246. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  247. continue;
  248. /* get audio formats */
  249. switch (protocol) {
  250. default:
  251. snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
  252. dev->devnum, iface_no, altno, protocol);
  253. protocol = UAC_VERSION_1;
  254. /* fall through */
  255. case UAC_VERSION_1: {
  256. struct uac1_as_header_descriptor *as =
  257. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  258. if (!as) {
  259. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  260. dev->devnum, iface_no, altno);
  261. continue;
  262. }
  263. if (as->bLength < sizeof(*as)) {
  264. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  265. dev->devnum, iface_no, altno);
  266. continue;
  267. }
  268. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  269. break;
  270. }
  271. case UAC_VERSION_2: {
  272. struct uac2_input_terminal_descriptor *input_term;
  273. struct uac2_output_terminal_descriptor *output_term;
  274. struct uac2_as_header_descriptor *as =
  275. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  276. if (!as) {
  277. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  278. dev->devnum, iface_no, altno);
  279. continue;
  280. }
  281. if (as->bLength < sizeof(*as)) {
  282. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  283. dev->devnum, iface_no, altno);
  284. continue;
  285. }
  286. num_channels = as->bNrChannels;
  287. format = le32_to_cpu(as->bmFormats);
  288. /* lookup the terminal associated to this interface
  289. * to extract the clock */
  290. input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  291. as->bTerminalLink);
  292. if (input_term) {
  293. clock = input_term->bCSourceID;
  294. break;
  295. }
  296. output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  297. as->bTerminalLink);
  298. if (output_term) {
  299. clock = output_term->bCSourceID;
  300. break;
  301. }
  302. snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
  303. dev->devnum, iface_no, altno, as->bTerminalLink);
  304. continue;
  305. }
  306. }
  307. /* get format type */
  308. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  309. if (!fmt) {
  310. snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  311. dev->devnum, iface_no, altno);
  312. continue;
  313. }
  314. if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
  315. ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
  316. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  317. dev->devnum, iface_no, altno);
  318. continue;
  319. }
  320. /*
  321. * Blue Microphones workaround: The last altsetting is identical
  322. * with the previous one, except for a larger packet size, but
  323. * is actually a mislabeled two-channel setting; ignore it.
  324. */
  325. if (fmt->bNrChannels == 1 &&
  326. fmt->bSubframeSize == 2 &&
  327. altno == 2 && num == 3 &&
  328. fp && fp->altsetting == 1 && fp->channels == 1 &&
  329. fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  330. protocol == UAC_VERSION_1 &&
  331. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  332. fp->maxpacksize * 2)
  333. continue;
  334. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  335. if (! fp) {
  336. snd_printk(KERN_ERR "cannot malloc\n");
  337. return -ENOMEM;
  338. }
  339. fp->iface = iface_no;
  340. fp->altsetting = altno;
  341. fp->altset_idx = i;
  342. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  343. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  344. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  345. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  346. /* num_channels is only set for v2 interfaces */
  347. fp->channels = num_channels;
  348. if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  349. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  350. * (fp->maxpacksize & 0x7ff);
  351. fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
  352. fp->clock = clock;
  353. INIT_LIST_HEAD(&fp->list);
  354. /* some quirks for attributes here */
  355. switch (chip->usb_id) {
  356. case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  357. /* Optoplay sets the sample rate attribute although
  358. * it seems not supporting it in fact.
  359. */
  360. fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  361. break;
  362. case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  363. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  364. /* doesn't set the sample rate attribute, but supports it */
  365. fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  366. break;
  367. case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
  368. case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
  369. case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  370. case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  371. an older model 77d:223) */
  372. /*
  373. * plantronics headset and Griffin iMic have set adaptive-in
  374. * although it's really not...
  375. */
  376. fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  377. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  378. fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  379. else
  380. fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  381. break;
  382. }
  383. /* ok, let's parse further... */
  384. if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  385. kfree(fp->rate_table);
  386. kfree(fp);
  387. fp = NULL;
  388. continue;
  389. }
  390. snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  391. err = snd_usb_add_audio_stream(chip, stream, fp);
  392. if (err < 0) {
  393. list_del(&fp->list); /* unlink for avoiding double-free */
  394. kfree(fp->rate_table);
  395. kfree(fp);
  396. return err;
  397. }
  398. /* try to set the interface... */
  399. usb_set_interface(chip->dev, iface_no, altno);
  400. snd_usb_init_pitch(chip, iface_no, alts, fp);
  401. snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
  402. }
  403. return 0;
  404. }