endpoint.c 13 KB

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