stream.c 20 KB

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