stream.c 14 KB

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