endpoint.c 13 KB

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