endpoint.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/usb.h>
  19. #include <linux/usb/audio.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include "usbaudio.h"
  23. #include "card.h"
  24. #include "proc.h"
  25. #include "quirks.h"
  26. #include "endpoint.h"
  27. #include "urb.h"
  28. #include "pcm.h"
  29. #include "helper.h"
  30. #include "format.h"
  31. /*
  32. * free a substream
  33. */
  34. static void free_substream(struct snd_usb_substream *subs)
  35. {
  36. struct list_head *p, *n;
  37. if (!subs->num_formats)
  38. return; /* not initialized */
  39. list_for_each_safe(p, n, &subs->fmt_list) {
  40. struct audioformat *fp = list_entry(p, struct audioformat, list);
  41. kfree(fp->rate_table);
  42. kfree(fp);
  43. }
  44. kfree(subs->rate_list.list);
  45. }
  46. /*
  47. * free a usb stream instance
  48. */
  49. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  50. {
  51. free_substream(&stream->substream[0]);
  52. free_substream(&stream->substream[1]);
  53. list_del(&stream->list);
  54. kfree(stream);
  55. }
  56. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  57. {
  58. struct snd_usb_stream *stream = pcm->private_data;
  59. if (stream) {
  60. stream->pcm = NULL;
  61. snd_usb_audio_stream_free(stream);
  62. }
  63. }
  64. /*
  65. * add this endpoint to the chip instance.
  66. * if a stream with the same endpoint already exists, append to it.
  67. * if not, create a new pcm stream.
  68. */
  69. int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
  70. {
  71. struct list_head *p;
  72. struct snd_usb_stream *as;
  73. struct snd_usb_substream *subs;
  74. struct snd_pcm *pcm;
  75. int err;
  76. list_for_each(p, &chip->pcm_list) {
  77. as = list_entry(p, struct snd_usb_stream, list);
  78. if (as->fmt_type != fp->fmt_type)
  79. continue;
  80. subs = &as->substream[stream];
  81. if (!subs->endpoint)
  82. continue;
  83. if (subs->endpoint == fp->endpoint) {
  84. list_add_tail(&fp->list, &subs->fmt_list);
  85. subs->num_formats++;
  86. subs->formats |= 1ULL << fp->format;
  87. return 0;
  88. }
  89. }
  90. /* look for an empty stream */
  91. list_for_each(p, &chip->pcm_list) {
  92. as = list_entry(p, struct snd_usb_stream, list);
  93. if (as->fmt_type != fp->fmt_type)
  94. continue;
  95. subs = &as->substream[stream];
  96. if (subs->endpoint)
  97. continue;
  98. err = snd_pcm_new_stream(as->pcm, stream, 1);
  99. if (err < 0)
  100. return err;
  101. snd_usb_init_substream(as, stream, fp);
  102. return 0;
  103. }
  104. /* create a new pcm */
  105. as = kzalloc(sizeof(*as), GFP_KERNEL);
  106. if (!as)
  107. return -ENOMEM;
  108. as->pcm_index = chip->pcm_devs;
  109. as->chip = chip;
  110. as->fmt_type = fp->fmt_type;
  111. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  112. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  113. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  114. &pcm);
  115. if (err < 0) {
  116. kfree(as);
  117. return err;
  118. }
  119. as->pcm = pcm;
  120. pcm->private_data = as;
  121. pcm->private_free = snd_usb_audio_pcm_free;
  122. pcm->info_flags = 0;
  123. if (chip->pcm_devs > 0)
  124. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  125. else
  126. strcpy(pcm->name, "USB Audio");
  127. snd_usb_init_substream(as, stream, fp);
  128. list_add(&as->list, &chip->pcm_list);
  129. chip->pcm_devs++;
  130. snd_usb_proc_pcm_format_add(as);
  131. return 0;
  132. }
  133. int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
  134. {
  135. struct usb_device *dev;
  136. struct usb_interface *iface;
  137. struct usb_host_interface *alts;
  138. struct usb_interface_descriptor *altsd;
  139. int i, altno, err, stream;
  140. int format = 0, num_channels = 0;
  141. struct audioformat *fp = NULL;
  142. unsigned char *fmt, *csep;
  143. int num, protocol;
  144. dev = chip->dev;
  145. /* parse the interface's altsettings */
  146. iface = usb_ifnum_to_if(dev, iface_no);
  147. num = iface->num_altsetting;
  148. /*
  149. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  150. * one misses syncpipe, and does not produce any sound.
  151. */
  152. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  153. num = 4;
  154. for (i = 0; i < num; i++) {
  155. alts = &iface->altsetting[i];
  156. altsd = get_iface_desc(alts);
  157. protocol = altsd->bInterfaceProtocol;
  158. /* skip invalid one */
  159. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  160. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  161. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  162. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  163. altsd->bNumEndpoints < 1 ||
  164. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  165. continue;
  166. /* must be isochronous */
  167. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  168. USB_ENDPOINT_XFER_ISOC)
  169. continue;
  170. /* check direction */
  171. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  172. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  173. altno = altsd->bAlternateSetting;
  174. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  175. continue;
  176. /* get audio formats */
  177. switch (protocol) {
  178. case UAC_VERSION_1: {
  179. struct uac_as_header_descriptor_v1 *as =
  180. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  181. if (!as) {
  182. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  183. dev->devnum, iface_no, altno);
  184. continue;
  185. }
  186. if (as->bLength < sizeof(*as)) {
  187. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  188. dev->devnum, iface_no, altno);
  189. continue;
  190. }
  191. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  192. break;
  193. }
  194. case UAC_VERSION_2: {
  195. struct uac_as_header_descriptor_v2 *as =
  196. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  197. if (!as) {
  198. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  199. dev->devnum, iface_no, altno);
  200. continue;
  201. }
  202. if (as->bLength < sizeof(*as)) {
  203. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  204. dev->devnum, iface_no, altno);
  205. continue;
  206. }
  207. num_channels = as->bNrChannels;
  208. format = le32_to_cpu(as->bmFormats);
  209. break;
  210. }
  211. default:
  212. snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",
  213. dev->devnum, iface_no, altno, protocol);
  214. continue;
  215. }
  216. /* get format type */
  217. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  218. if (!fmt) {
  219. snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  220. dev->devnum, iface_no, altno);
  221. continue;
  222. }
  223. if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||
  224. ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {
  225. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  226. dev->devnum, iface_no, altno);
  227. continue;
  228. }
  229. /*
  230. * Blue Microphones workaround: The last altsetting is identical
  231. * with the previous one, except for a larger packet size, but
  232. * is actually a mislabeled two-channel setting; ignore it.
  233. */
  234. if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
  235. fp && fp->altsetting == 1 && fp->channels == 1 &&
  236. fp->format == SNDRV_PCM_FORMAT_S16_LE &&
  237. protocol == UAC_VERSION_1 &&
  238. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  239. fp->maxpacksize * 2)
  240. continue;
  241. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  242. /* Creamware Noah has this descriptor after the 2nd endpoint */
  243. if (!csep && altsd->bNumEndpoints >= 2)
  244. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  245. if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {
  246. snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  247. " class specific endpoint descriptor\n",
  248. dev->devnum, iface_no, altno);
  249. csep = NULL;
  250. }
  251. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  252. if (! fp) {
  253. snd_printk(KERN_ERR "cannot malloc\n");
  254. return -ENOMEM;
  255. }
  256. fp->iface = iface_no;
  257. fp->altsetting = altno;
  258. fp->altset_idx = i;
  259. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  260. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  261. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  262. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  263. /* num_channels is only set for v2 interfaces */
  264. fp->channels = num_channels;
  265. if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  266. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  267. * (fp->maxpacksize & 0x7ff);
  268. fp->attributes = csep ? csep[3] : 0;
  269. /* some quirks for attributes here */
  270. switch (chip->usb_id) {
  271. case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  272. /* Optoplay sets the sample rate attribute although
  273. * it seems not supporting it in fact.
  274. */
  275. fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  276. break;
  277. case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  278. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  279. /* doesn't set the sample rate attribute, but supports it */
  280. fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  281. break;
  282. case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  283. case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  284. an older model 77d:223) */
  285. /*
  286. * plantronics headset and Griffin iMic have set adaptive-in
  287. * although it's really not...
  288. */
  289. fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  290. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  291. fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  292. else
  293. fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  294. break;
  295. }
  296. /* ok, let's parse further... */
  297. if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  298. kfree(fp->rate_table);
  299. kfree(fp);
  300. continue;
  301. }
  302. snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  303. err = snd_usb_add_audio_endpoint(chip, stream, fp);
  304. if (err < 0) {
  305. kfree(fp->rate_table);
  306. kfree(fp);
  307. return err;
  308. }
  309. /* try to set the interface... */
  310. usb_set_interface(chip->dev, iface_no, altno);
  311. snd_usb_init_pitch(chip->dev, iface_no, alts, fp);
  312. snd_usb_init_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
  313. }
  314. return 0;
  315. }