endpoint.c 10 KB

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