stream.c 13 KB

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