stream.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 <sound/control.h>
  24. #include <sound/tlv.h>
  25. #include "usbaudio.h"
  26. #include "card.h"
  27. #include "proc.h"
  28. #include "quirks.h"
  29. #include "endpoint.h"
  30. #include "pcm.h"
  31. #include "helper.h"
  32. #include "format.h"
  33. #include "clock.h"
  34. #include "stream.h"
  35. /*
  36. * free a substream
  37. */
  38. static void free_substream(struct snd_usb_substream *subs)
  39. {
  40. struct audioformat *fp, *n;
  41. if (!subs->num_formats)
  42. return; /* not initialized */
  43. list_for_each_entry_safe(fp, n, &subs->fmt_list, list) {
  44. kfree(fp->rate_table);
  45. kfree(fp->chmap);
  46. kfree(fp);
  47. }
  48. kfree(subs->rate_list.list);
  49. }
  50. /*
  51. * free a usb stream instance
  52. */
  53. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  54. {
  55. free_substream(&stream->substream[0]);
  56. free_substream(&stream->substream[1]);
  57. list_del(&stream->list);
  58. kfree(stream);
  59. }
  60. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  61. {
  62. struct snd_usb_stream *stream = pcm->private_data;
  63. if (stream) {
  64. stream->pcm = NULL;
  65. snd_usb_audio_stream_free(stream);
  66. }
  67. }
  68. /*
  69. * initialize the substream instance.
  70. */
  71. static void snd_usb_init_substream(struct snd_usb_stream *as,
  72. int stream,
  73. struct audioformat *fp)
  74. {
  75. struct snd_usb_substream *subs = &as->substream[stream];
  76. INIT_LIST_HEAD(&subs->fmt_list);
  77. spin_lock_init(&subs->lock);
  78. subs->stream = as;
  79. subs->direction = stream;
  80. subs->dev = as->chip->dev;
  81. subs->txfr_quirk = as->chip->txfr_quirk;
  82. subs->speed = snd_usb_get_speed(subs->dev);
  83. snd_usb_set_pcm_ops(as->pcm, stream);
  84. list_add_tail(&fp->list, &subs->fmt_list);
  85. subs->formats |= fp->formats;
  86. subs->num_formats++;
  87. subs->fmt_type = fp->fmt_type;
  88. subs->ep_num = fp->endpoint;
  89. if (fp->channels > subs->channels_max)
  90. subs->channels_max = fp->channels;
  91. }
  92. /* kctl callbacks for usb-audio channel maps */
  93. static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  94. struct snd_ctl_elem_info *uinfo)
  95. {
  96. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  97. struct snd_usb_substream *subs = info->private_data;
  98. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  99. uinfo->count = subs->channels_max;
  100. uinfo->value.integer.min = 0;
  101. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  102. return 0;
  103. }
  104. /* check whether a duplicated entry exists in the audiofmt list */
  105. static bool have_dup_chmap(struct snd_usb_substream *subs,
  106. struct audioformat *fp)
  107. {
  108. struct list_head *p;
  109. for (p = fp->list.prev; p != &subs->fmt_list; p = p->prev) {
  110. struct audioformat *prev;
  111. prev = list_entry(p, struct audioformat, list);
  112. if (prev->chmap &&
  113. !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
  114. return true;
  115. }
  116. return false;
  117. }
  118. static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  119. unsigned int size, unsigned int __user *tlv)
  120. {
  121. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  122. struct snd_usb_substream *subs = info->private_data;
  123. struct audioformat *fp;
  124. unsigned int __user *dst;
  125. int count = 0;
  126. if (size < 8)
  127. return -ENOMEM;
  128. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  129. return -EFAULT;
  130. size -= 8;
  131. dst = tlv + 2;
  132. list_for_each_entry(fp, &subs->fmt_list, list) {
  133. int i, ch_bytes;
  134. if (!fp->chmap)
  135. continue;
  136. if (have_dup_chmap(subs, fp))
  137. continue;
  138. /* copy the entry */
  139. ch_bytes = fp->chmap->channels * 4;
  140. if (size < 8 + ch_bytes)
  141. return -ENOMEM;
  142. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  143. put_user(ch_bytes, dst + 1))
  144. return -EFAULT;
  145. dst += 2;
  146. for (i = 0; i < fp->chmap->channels; i++, dst++) {
  147. if (put_user(fp->chmap->map[i], dst))
  148. return -EFAULT;
  149. }
  150. count += 8 + ch_bytes;
  151. size -= 8 + ch_bytes;
  152. }
  153. if (put_user(count, tlv + 1))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_value *ucontrol)
  159. {
  160. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  161. struct snd_usb_substream *subs = info->private_data;
  162. struct snd_pcm_chmap_elem *chmap = NULL;
  163. int i;
  164. memset(ucontrol->value.integer.value, 0,
  165. sizeof(ucontrol->value.integer.value));
  166. if (subs->cur_audiofmt)
  167. chmap = subs->cur_audiofmt->chmap;
  168. if (chmap) {
  169. for (i = 0; i < chmap->channels; i++)
  170. ucontrol->value.integer.value[i] = chmap->map[i];
  171. }
  172. return 0;
  173. }
  174. /* create a chmap kctl assigned to the given USB substream */
  175. static int add_chmap(struct snd_pcm *pcm, int stream,
  176. struct snd_usb_substream *subs)
  177. {
  178. struct audioformat *fp;
  179. struct snd_pcm_chmap *chmap;
  180. struct snd_kcontrol *kctl;
  181. int err;
  182. list_for_each_entry(fp, &subs->fmt_list, list)
  183. if (fp->chmap)
  184. goto ok;
  185. /* no chmap is found */
  186. return 0;
  187. ok:
  188. err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
  189. if (err < 0)
  190. return err;
  191. /* override handlers */
  192. chmap->private_data = subs;
  193. kctl = chmap->kctl;
  194. kctl->info = usb_chmap_ctl_info;
  195. kctl->get = usb_chmap_ctl_get;
  196. kctl->tlv.c = usb_chmap_ctl_tlv;
  197. return 0;
  198. }
  199. /* convert from USB ChannelConfig bits to ALSA chmap element */
  200. static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
  201. int protocol)
  202. {
  203. static unsigned int uac1_maps[] = {
  204. SNDRV_CHMAP_FL, /* left front */
  205. SNDRV_CHMAP_FR, /* right front */
  206. SNDRV_CHMAP_FC, /* center front */
  207. SNDRV_CHMAP_LFE, /* LFE */
  208. SNDRV_CHMAP_SL, /* left surround */
  209. SNDRV_CHMAP_SR, /* right surround */
  210. SNDRV_CHMAP_FLC, /* left of center */
  211. SNDRV_CHMAP_FRC, /* right of center */
  212. SNDRV_CHMAP_RC, /* surround */
  213. SNDRV_CHMAP_SL, /* side left */
  214. SNDRV_CHMAP_SR, /* side right */
  215. SNDRV_CHMAP_TC, /* top */
  216. 0 /* terminator */
  217. };
  218. static unsigned int uac2_maps[] = {
  219. SNDRV_CHMAP_FL, /* front left */
  220. SNDRV_CHMAP_FR, /* front right */
  221. SNDRV_CHMAP_FC, /* front center */
  222. SNDRV_CHMAP_LFE, /* LFE */
  223. SNDRV_CHMAP_RL, /* back left */
  224. SNDRV_CHMAP_RR, /* back right */
  225. SNDRV_CHMAP_FLC, /* front left of center */
  226. SNDRV_CHMAP_FRC, /* front right of center */
  227. SNDRV_CHMAP_RC, /* back center */
  228. SNDRV_CHMAP_SL, /* side left */
  229. SNDRV_CHMAP_SR, /* side right */
  230. SNDRV_CHMAP_TC, /* top center */
  231. SNDRV_CHMAP_TFL, /* top front left */
  232. SNDRV_CHMAP_TFC, /* top front center */
  233. SNDRV_CHMAP_TFR, /* top front right */
  234. SNDRV_CHMAP_TRL, /* top back left */
  235. SNDRV_CHMAP_TRC, /* top back center */
  236. SNDRV_CHMAP_TRR, /* top back right */
  237. SNDRV_CHMAP_TFLC, /* top front left of center */
  238. SNDRV_CHMAP_TFRC, /* top front right of center */
  239. SNDRV_CHMAP_LLFE, /* left LFE */
  240. SNDRV_CHMAP_RLFE, /* right LFE */
  241. SNDRV_CHMAP_TSL, /* top side left */
  242. SNDRV_CHMAP_TSR, /* top side right */
  243. SNDRV_CHMAP_BC, /* bottom center */
  244. SNDRV_CHMAP_BLC, /* bottom left center */
  245. SNDRV_CHMAP_BRC, /* bottom right center */
  246. 0 /* terminator */
  247. };
  248. struct snd_pcm_chmap_elem *chmap;
  249. const unsigned int *maps;
  250. int c;
  251. if (!bits)
  252. return NULL;
  253. if (channels > ARRAY_SIZE(chmap->map))
  254. return NULL;
  255. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  256. if (!chmap)
  257. return NULL;
  258. maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
  259. chmap->channels = channels;
  260. c = 0;
  261. for (; bits && *maps; maps++, bits >>= 1) {
  262. if (bits & 1)
  263. chmap->map[c++] = *maps;
  264. }
  265. for (; c < channels; c++)
  266. chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
  267. return chmap;
  268. }
  269. /*
  270. * add this endpoint to the chip instance.
  271. * if a stream with the same endpoint already exists, append to it.
  272. * if not, create a new pcm stream.
  273. */
  274. int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  275. int stream,
  276. struct audioformat *fp)
  277. {
  278. struct snd_usb_stream *as;
  279. struct snd_usb_substream *subs;
  280. struct snd_pcm *pcm;
  281. int err;
  282. list_for_each_entry(as, &chip->pcm_list, list) {
  283. if (as->fmt_type != fp->fmt_type)
  284. continue;
  285. subs = &as->substream[stream];
  286. if (subs->ep_num == fp->endpoint) {
  287. list_add_tail(&fp->list, &subs->fmt_list);
  288. subs->num_formats++;
  289. subs->formats |= fp->formats;
  290. return 0;
  291. }
  292. }
  293. /* look for an empty stream */
  294. list_for_each_entry(as, &chip->pcm_list, list) {
  295. if (as->fmt_type != fp->fmt_type)
  296. continue;
  297. subs = &as->substream[stream];
  298. if (subs->ep_num)
  299. continue;
  300. err = snd_pcm_new_stream(as->pcm, stream, 1);
  301. if (err < 0)
  302. return err;
  303. snd_usb_init_substream(as, stream, fp);
  304. return add_chmap(as->pcm, stream, subs);
  305. }
  306. /* create a new pcm */
  307. as = kzalloc(sizeof(*as), GFP_KERNEL);
  308. if (!as)
  309. return -ENOMEM;
  310. as->pcm_index = chip->pcm_devs;
  311. as->chip = chip;
  312. as->fmt_type = fp->fmt_type;
  313. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  314. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  315. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  316. &pcm);
  317. if (err < 0) {
  318. kfree(as);
  319. return err;
  320. }
  321. as->pcm = pcm;
  322. pcm->private_data = as;
  323. pcm->private_free = snd_usb_audio_pcm_free;
  324. pcm->info_flags = 0;
  325. if (chip->pcm_devs > 0)
  326. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  327. else
  328. strcpy(pcm->name, "USB Audio");
  329. snd_usb_init_substream(as, stream, fp);
  330. list_add(&as->list, &chip->pcm_list);
  331. chip->pcm_devs++;
  332. snd_usb_proc_pcm_format_add(as);
  333. return add_chmap(pcm, stream, &as->substream[stream]);
  334. }
  335. static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  336. struct usb_host_interface *alts,
  337. int protocol, int iface_no)
  338. {
  339. /* parsed with a v1 header here. that's ok as we only look at the
  340. * header first which is the same for both versions */
  341. struct uac_iso_endpoint_descriptor *csep;
  342. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  343. int attributes = 0;
  344. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  345. /* Creamware Noah has this descriptor after the 2nd endpoint */
  346. if (!csep && altsd->bNumEndpoints >= 2)
  347. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  348. if (!csep || csep->bLength < 7 ||
  349. csep->bDescriptorSubtype != UAC_EP_GENERAL) {
  350. snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  351. " class specific endpoint descriptor\n",
  352. chip->dev->devnum, iface_no,
  353. altsd->bAlternateSetting);
  354. return 0;
  355. }
  356. if (protocol == UAC_VERSION_1) {
  357. attributes = csep->bmAttributes;
  358. } else {
  359. struct uac2_iso_endpoint_descriptor *csep2 =
  360. (struct uac2_iso_endpoint_descriptor *) csep;
  361. attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  362. /* emulate the endpoint attributes of a v1 device */
  363. if (csep2->bmControls & UAC2_CONTROL_PITCH)
  364. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  365. }
  366. return attributes;
  367. }
  368. /* find an input terminal descriptor (either UAC1 or UAC2) with the given
  369. * terminal id
  370. */
  371. static void *
  372. snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  373. int terminal_id)
  374. {
  375. struct uac2_input_terminal_descriptor *term = NULL;
  376. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  377. ctrl_iface->extralen,
  378. term, UAC_INPUT_TERMINAL))) {
  379. if (term->bTerminalID == terminal_id)
  380. return term;
  381. }
  382. return NULL;
  383. }
  384. static struct uac2_output_terminal_descriptor *
  385. snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  386. int terminal_id)
  387. {
  388. struct uac2_output_terminal_descriptor *term = NULL;
  389. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  390. ctrl_iface->extralen,
  391. term, UAC_OUTPUT_TERMINAL))) {
  392. if (term->bTerminalID == terminal_id)
  393. return term;
  394. }
  395. return NULL;
  396. }
  397. int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  398. {
  399. struct usb_device *dev;
  400. struct usb_interface *iface;
  401. struct usb_host_interface *alts;
  402. struct usb_interface_descriptor *altsd;
  403. int i, altno, err, stream;
  404. unsigned int format = 0, num_channels = 0;
  405. struct audioformat *fp = NULL;
  406. int num, protocol, clock = 0;
  407. struct uac_format_type_i_continuous_descriptor *fmt;
  408. unsigned int chconfig;
  409. dev = chip->dev;
  410. /* parse the interface's altsettings */
  411. iface = usb_ifnum_to_if(dev, iface_no);
  412. num = iface->num_altsetting;
  413. /*
  414. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  415. * one misses syncpipe, and does not produce any sound.
  416. */
  417. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  418. num = 4;
  419. for (i = 0; i < num; i++) {
  420. alts = &iface->altsetting[i];
  421. altsd = get_iface_desc(alts);
  422. protocol = altsd->bInterfaceProtocol;
  423. /* skip invalid one */
  424. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  425. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  426. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  427. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  428. altsd->bNumEndpoints < 1 ||
  429. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  430. continue;
  431. /* must be isochronous */
  432. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  433. USB_ENDPOINT_XFER_ISOC)
  434. continue;
  435. /* check direction */
  436. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  437. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  438. altno = altsd->bAlternateSetting;
  439. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  440. continue;
  441. chconfig = 0;
  442. /* get audio formats */
  443. switch (protocol) {
  444. default:
  445. snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
  446. dev->devnum, iface_no, altno, protocol);
  447. protocol = UAC_VERSION_1;
  448. /* fall through */
  449. case UAC_VERSION_1: {
  450. struct uac1_as_header_descriptor *as =
  451. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  452. struct uac_input_terminal_descriptor *iterm;
  453. if (!as) {
  454. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  455. dev->devnum, iface_no, altno);
  456. continue;
  457. }
  458. if (as->bLength < sizeof(*as)) {
  459. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  460. dev->devnum, iface_no, altno);
  461. continue;
  462. }
  463. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  464. iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  465. as->bTerminalLink);
  466. if (iterm) {
  467. num_channels = iterm->bNrChannels;
  468. chconfig = le16_to_cpu(iterm->wChannelConfig);
  469. }
  470. break;
  471. }
  472. case UAC_VERSION_2: {
  473. struct uac2_input_terminal_descriptor *input_term;
  474. struct uac2_output_terminal_descriptor *output_term;
  475. struct uac2_as_header_descriptor *as =
  476. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  477. if (!as) {
  478. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  479. dev->devnum, iface_no, altno);
  480. continue;
  481. }
  482. if (as->bLength < sizeof(*as)) {
  483. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  484. dev->devnum, iface_no, altno);
  485. continue;
  486. }
  487. num_channels = as->bNrChannels;
  488. format = le32_to_cpu(as->bmFormats);
  489. /* lookup the terminal associated to this interface
  490. * to extract the clock */
  491. input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  492. as->bTerminalLink);
  493. if (input_term) {
  494. clock = input_term->bCSourceID;
  495. chconfig = le32_to_cpu(input_term->bmChannelConfig);
  496. break;
  497. }
  498. output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  499. as->bTerminalLink);
  500. if (output_term) {
  501. clock = output_term->bCSourceID;
  502. break;
  503. }
  504. snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
  505. dev->devnum, iface_no, altno, as->bTerminalLink);
  506. continue;
  507. }
  508. }
  509. /* get format type */
  510. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  511. if (!fmt) {
  512. snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  513. dev->devnum, iface_no, altno);
  514. continue;
  515. }
  516. if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
  517. ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
  518. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  519. dev->devnum, iface_no, altno);
  520. continue;
  521. }
  522. /*
  523. * Blue Microphones workaround: The last altsetting is identical
  524. * with the previous one, except for a larger packet size, but
  525. * is actually a mislabeled two-channel setting; ignore it.
  526. */
  527. if (fmt->bNrChannels == 1 &&
  528. fmt->bSubframeSize == 2 &&
  529. altno == 2 && num == 3 &&
  530. fp && fp->altsetting == 1 && fp->channels == 1 &&
  531. fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  532. protocol == UAC_VERSION_1 &&
  533. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  534. fp->maxpacksize * 2)
  535. continue;
  536. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  537. if (! fp) {
  538. snd_printk(KERN_ERR "cannot malloc\n");
  539. return -ENOMEM;
  540. }
  541. fp->iface = iface_no;
  542. fp->altsetting = altno;
  543. fp->altset_idx = i;
  544. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  545. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  546. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  547. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  548. fp->channels = num_channels;
  549. if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  550. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  551. * (fp->maxpacksize & 0x7ff);
  552. fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
  553. fp->clock = clock;
  554. fp->chmap = convert_chmap(num_channels, chconfig, protocol);
  555. /* some quirks for attributes here */
  556. switch (chip->usb_id) {
  557. case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  558. /* Optoplay sets the sample rate attribute although
  559. * it seems not supporting it in fact.
  560. */
  561. fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  562. break;
  563. case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  564. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  565. /* doesn't set the sample rate attribute, but supports it */
  566. fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  567. break;
  568. case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
  569. case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
  570. case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  571. case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  572. an older model 77d:223) */
  573. /*
  574. * plantronics headset and Griffin iMic have set adaptive-in
  575. * although it's really not...
  576. */
  577. fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  578. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  579. fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  580. else
  581. fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  582. break;
  583. }
  584. /* ok, let's parse further... */
  585. if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  586. kfree(fp->rate_table);
  587. kfree(fp->chmap);
  588. kfree(fp);
  589. fp = NULL;
  590. continue;
  591. }
  592. snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  593. err = snd_usb_add_audio_stream(chip, stream, fp);
  594. if (err < 0) {
  595. kfree(fp->rate_table);
  596. kfree(fp->chmap);
  597. kfree(fp);
  598. return err;
  599. }
  600. /* try to set the interface... */
  601. usb_set_interface(chip->dev, iface_no, altno);
  602. snd_usb_init_pitch(chip, iface_no, alts, fp);
  603. snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
  604. }
  605. return 0;
  606. }