stream.c 21 KB

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