stream.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 <linux/usb/audio-v3.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/control.h>
  25. #include <sound/tlv.h>
  26. #include "usbaudio.h"
  27. #include "card.h"
  28. #include "proc.h"
  29. #include "quirks.h"
  30. #include "endpoint.h"
  31. #include "pcm.h"
  32. #include "helper.h"
  33. #include "format.h"
  34. #include "clock.h"
  35. #include "stream.h"
  36. /*
  37. * free a substream
  38. */
  39. static void free_substream(struct snd_usb_substream *subs)
  40. {
  41. struct audioformat *fp, *n;
  42. if (!subs->num_formats)
  43. return; /* not initialized */
  44. list_for_each_entry_safe(fp, n, &subs->fmt_list, list) {
  45. kfree(fp->rate_table);
  46. kfree(fp->chmap);
  47. kfree(fp);
  48. }
  49. kfree(subs->rate_list.list);
  50. }
  51. /*
  52. * free a usb stream instance
  53. */
  54. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  55. {
  56. free_substream(&stream->substream[0]);
  57. free_substream(&stream->substream[1]);
  58. list_del(&stream->list);
  59. kfree(stream);
  60. }
  61. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  62. {
  63. struct snd_usb_stream *stream = pcm->private_data;
  64. if (stream) {
  65. stream->pcm = NULL;
  66. snd_usb_audio_stream_free(stream);
  67. }
  68. }
  69. /*
  70. * initialize the substream instance.
  71. */
  72. static void snd_usb_init_substream(struct snd_usb_stream *as,
  73. int stream,
  74. struct audioformat *fp)
  75. {
  76. struct snd_usb_substream *subs = &as->substream[stream];
  77. INIT_LIST_HEAD(&subs->fmt_list);
  78. spin_lock_init(&subs->lock);
  79. subs->stream = as;
  80. subs->direction = stream;
  81. subs->dev = as->chip->dev;
  82. subs->txfr_quirk = as->chip->txfr_quirk;
  83. subs->tx_length_quirk = as->chip->tx_length_quirk;
  84. subs->speed = snd_usb_get_speed(subs->dev);
  85. subs->pkt_offset_adj = 0;
  86. snd_usb_set_pcm_ops(as->pcm, stream);
  87. list_add_tail(&fp->list, &subs->fmt_list);
  88. subs->formats |= fp->formats;
  89. subs->num_formats++;
  90. subs->fmt_type = fp->fmt_type;
  91. subs->ep_num = fp->endpoint;
  92. if (fp->channels > subs->channels_max)
  93. subs->channels_max = fp->channels;
  94. }
  95. /* kctl callbacks for usb-audio channel maps */
  96. static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  97. struct snd_ctl_elem_info *uinfo)
  98. {
  99. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  100. struct snd_usb_substream *subs = info->private_data;
  101. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  102. uinfo->count = subs->channels_max;
  103. uinfo->value.integer.min = 0;
  104. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  105. return 0;
  106. }
  107. /* check whether a duplicated entry exists in the audiofmt list */
  108. static bool have_dup_chmap(struct snd_usb_substream *subs,
  109. struct audioformat *fp)
  110. {
  111. struct audioformat *prev = fp;
  112. list_for_each_entry_continue_reverse(prev, &subs->fmt_list, 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. /* UAC3 device stores channels information in Cluster Descriptors */
  278. static struct
  279. snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
  280. *cluster)
  281. {
  282. unsigned int channels = cluster->bNrChannels;
  283. struct snd_pcm_chmap_elem *chmap;
  284. void *p = cluster;
  285. int len, c;
  286. if (channels > ARRAY_SIZE(chmap->map))
  287. return NULL;
  288. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  289. if (!chmap)
  290. return NULL;
  291. len = le16_to_cpu(cluster->wLength);
  292. c = 0;
  293. p += sizeof(struct uac3_cluster_header_descriptor);
  294. while (((p - (void *)cluster) < len) && (c < channels)) {
  295. struct uac3_cluster_segment_descriptor *cs_desc = p;
  296. u16 cs_len;
  297. u8 cs_type;
  298. cs_len = le16_to_cpu(cs_desc->wLength);
  299. cs_type = cs_desc->bSegmentType;
  300. if (cs_type == UAC3_CHANNEL_INFORMATION) {
  301. struct uac3_cluster_information_segment_descriptor *is = p;
  302. unsigned char map;
  303. /*
  304. * TODO: this conversion is not complete, update it
  305. * after adding UAC3 values to asound.h
  306. */
  307. switch (is->bChPurpose) {
  308. case UAC3_CH_MONO:
  309. map = SNDRV_CHMAP_MONO;
  310. break;
  311. case UAC3_CH_LEFT:
  312. case UAC3_CH_FRONT_LEFT:
  313. case UAC3_CH_HEADPHONE_LEFT:
  314. map = SNDRV_CHMAP_FL;
  315. break;
  316. case UAC3_CH_RIGHT:
  317. case UAC3_CH_FRONT_RIGHT:
  318. case UAC3_CH_HEADPHONE_RIGHT:
  319. map = SNDRV_CHMAP_FR;
  320. break;
  321. case UAC3_CH_FRONT_CENTER:
  322. map = SNDRV_CHMAP_FC;
  323. break;
  324. case UAC3_CH_FRONT_LEFT_OF_CENTER:
  325. map = SNDRV_CHMAP_FLC;
  326. break;
  327. case UAC3_CH_FRONT_RIGHT_OF_CENTER:
  328. map = SNDRV_CHMAP_FRC;
  329. break;
  330. case UAC3_CH_SIDE_LEFT:
  331. map = SNDRV_CHMAP_SL;
  332. break;
  333. case UAC3_CH_SIDE_RIGHT:
  334. map = SNDRV_CHMAP_SR;
  335. break;
  336. case UAC3_CH_BACK_LEFT:
  337. map = SNDRV_CHMAP_RL;
  338. break;
  339. case UAC3_CH_BACK_RIGHT:
  340. map = SNDRV_CHMAP_RR;
  341. break;
  342. case UAC3_CH_BACK_CENTER:
  343. map = SNDRV_CHMAP_RC;
  344. break;
  345. case UAC3_CH_BACK_LEFT_OF_CENTER:
  346. map = SNDRV_CHMAP_RLC;
  347. break;
  348. case UAC3_CH_BACK_RIGHT_OF_CENTER:
  349. map = SNDRV_CHMAP_RRC;
  350. break;
  351. case UAC3_CH_TOP_CENTER:
  352. map = SNDRV_CHMAP_TC;
  353. break;
  354. case UAC3_CH_TOP_FRONT_LEFT:
  355. map = SNDRV_CHMAP_TFL;
  356. break;
  357. case UAC3_CH_TOP_FRONT_RIGHT:
  358. map = SNDRV_CHMAP_TFR;
  359. break;
  360. case UAC3_CH_TOP_FRONT_CENTER:
  361. map = SNDRV_CHMAP_TFC;
  362. break;
  363. case UAC3_CH_TOP_FRONT_LOC:
  364. map = SNDRV_CHMAP_TFLC;
  365. break;
  366. case UAC3_CH_TOP_FRONT_ROC:
  367. map = SNDRV_CHMAP_TFRC;
  368. break;
  369. case UAC3_CH_TOP_SIDE_LEFT:
  370. map = SNDRV_CHMAP_TSL;
  371. break;
  372. case UAC3_CH_TOP_SIDE_RIGHT:
  373. map = SNDRV_CHMAP_TSR;
  374. break;
  375. case UAC3_CH_TOP_BACK_LEFT:
  376. map = SNDRV_CHMAP_TRL;
  377. break;
  378. case UAC3_CH_TOP_BACK_RIGHT:
  379. map = SNDRV_CHMAP_TRR;
  380. break;
  381. case UAC3_CH_TOP_BACK_CENTER:
  382. map = SNDRV_CHMAP_TRC;
  383. break;
  384. case UAC3_CH_BOTTOM_CENTER:
  385. map = SNDRV_CHMAP_BC;
  386. break;
  387. case UAC3_CH_LOW_FREQUENCY_EFFECTS:
  388. map = SNDRV_CHMAP_LFE;
  389. break;
  390. case UAC3_CH_LFE_LEFT:
  391. map = SNDRV_CHMAP_LLFE;
  392. break;
  393. case UAC3_CH_LFE_RIGHT:
  394. map = SNDRV_CHMAP_RLFE;
  395. break;
  396. case UAC3_CH_RELATIONSHIP_UNDEFINED:
  397. default:
  398. map = SNDRV_CHMAP_UNKNOWN;
  399. break;
  400. }
  401. chmap->map[c++] = map;
  402. }
  403. p += cs_len;
  404. }
  405. if (channels < c)
  406. pr_err("%s: channel number mismatch\n", __func__);
  407. chmap->channels = channels;
  408. for (; c < channels; c++)
  409. chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
  410. return chmap;
  411. }
  412. /*
  413. * add this endpoint to the chip instance.
  414. * if a stream with the same endpoint already exists, append to it.
  415. * if not, create a new pcm stream. note, fp is added to the substream
  416. * fmt_list and will be freed on the chip instance release. do not free
  417. * fp or do remove it from the substream fmt_list to avoid double-free.
  418. */
  419. int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  420. int stream,
  421. struct audioformat *fp)
  422. {
  423. struct snd_usb_stream *as;
  424. struct snd_usb_substream *subs;
  425. struct snd_pcm *pcm;
  426. int err;
  427. list_for_each_entry(as, &chip->pcm_list, list) {
  428. if (as->fmt_type != fp->fmt_type)
  429. continue;
  430. subs = &as->substream[stream];
  431. if (subs->ep_num == fp->endpoint) {
  432. list_add_tail(&fp->list, &subs->fmt_list);
  433. subs->num_formats++;
  434. subs->formats |= fp->formats;
  435. return 0;
  436. }
  437. }
  438. /* look for an empty stream */
  439. list_for_each_entry(as, &chip->pcm_list, list) {
  440. if (as->fmt_type != fp->fmt_type)
  441. continue;
  442. subs = &as->substream[stream];
  443. if (subs->ep_num)
  444. continue;
  445. err = snd_pcm_new_stream(as->pcm, stream, 1);
  446. if (err < 0)
  447. return err;
  448. snd_usb_init_substream(as, stream, fp);
  449. return add_chmap(as->pcm, stream, subs);
  450. }
  451. /* create a new pcm */
  452. as = kzalloc(sizeof(*as), GFP_KERNEL);
  453. if (!as)
  454. return -ENOMEM;
  455. as->pcm_index = chip->pcm_devs;
  456. as->chip = chip;
  457. as->fmt_type = fp->fmt_type;
  458. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  459. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  460. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  461. &pcm);
  462. if (err < 0) {
  463. kfree(as);
  464. return err;
  465. }
  466. as->pcm = pcm;
  467. pcm->private_data = as;
  468. pcm->private_free = snd_usb_audio_pcm_free;
  469. pcm->info_flags = 0;
  470. if (chip->pcm_devs > 0)
  471. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  472. else
  473. strcpy(pcm->name, "USB Audio");
  474. snd_usb_init_substream(as, stream, fp);
  475. /*
  476. * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
  477. * fix to swap capture stream order in conf/cards/USB-audio.conf
  478. */
  479. if (chip->usb_id == USB_ID(0x0763, 0x2003))
  480. list_add(&as->list, &chip->pcm_list);
  481. else
  482. list_add_tail(&as->list, &chip->pcm_list);
  483. chip->pcm_devs++;
  484. snd_usb_proc_pcm_format_add(as);
  485. return add_chmap(pcm, stream, &as->substream[stream]);
  486. }
  487. static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  488. struct usb_host_interface *alts,
  489. int protocol, int iface_no)
  490. {
  491. /* parsed with a v1 header here. that's ok as we only look at the
  492. * header first which is the same for both versions */
  493. struct uac_iso_endpoint_descriptor *csep;
  494. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  495. int attributes = 0;
  496. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  497. /* Creamware Noah has this descriptor after the 2nd endpoint */
  498. if (!csep && altsd->bNumEndpoints >= 2)
  499. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  500. /*
  501. * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
  502. * bytes after the first endpoint, go search the entire interface.
  503. * Some devices have it directly *before* the standard endpoint.
  504. */
  505. if (!csep)
  506. csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
  507. if (!csep || csep->bLength < 7 ||
  508. csep->bDescriptorSubtype != UAC_EP_GENERAL) {
  509. usb_audio_warn(chip,
  510. "%u:%d : no or invalid class specific endpoint descriptor\n",
  511. iface_no, altsd->bAlternateSetting);
  512. return 0;
  513. }
  514. if (protocol == UAC_VERSION_1) {
  515. attributes = csep->bmAttributes;
  516. } else {
  517. struct uac2_iso_endpoint_descriptor *csep2 =
  518. (struct uac2_iso_endpoint_descriptor *) csep;
  519. attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  520. /* emulate the endpoint attributes of a v1 device */
  521. if (csep2->bmControls & UAC2_CONTROL_PITCH)
  522. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  523. }
  524. return attributes;
  525. }
  526. /* find an input terminal descriptor (either UAC1 or UAC2) with the given
  527. * terminal id
  528. */
  529. static void *
  530. snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  531. int terminal_id)
  532. {
  533. struct uac2_input_terminal_descriptor *term = NULL;
  534. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  535. ctrl_iface->extralen,
  536. term, UAC_INPUT_TERMINAL))) {
  537. if (term->bTerminalID == terminal_id)
  538. return term;
  539. }
  540. return NULL;
  541. }
  542. static void *
  543. snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  544. int terminal_id)
  545. {
  546. /* OK to use with both UAC2 and UAC3 */
  547. struct uac2_output_terminal_descriptor *term = NULL;
  548. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  549. ctrl_iface->extralen,
  550. term, UAC_OUTPUT_TERMINAL))) {
  551. if (term->bTerminalID == terminal_id)
  552. return term;
  553. }
  554. return NULL;
  555. }
  556. int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  557. {
  558. struct usb_device *dev;
  559. struct usb_interface *iface;
  560. struct usb_host_interface *alts;
  561. struct usb_interface_descriptor *altsd;
  562. int i, altno, err, stream;
  563. u64 format = 0;
  564. unsigned int num_channels = 0;
  565. struct audioformat *fp = NULL;
  566. int num, protocol, clock = 0;
  567. struct uac_format_type_i_continuous_descriptor *fmt = NULL;
  568. struct snd_pcm_chmap_elem *chmap_v3 = NULL;
  569. unsigned int chconfig;
  570. dev = chip->dev;
  571. /* parse the interface's altsettings */
  572. iface = usb_ifnum_to_if(dev, iface_no);
  573. num = iface->num_altsetting;
  574. /*
  575. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  576. * one misses syncpipe, and does not produce any sound.
  577. */
  578. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  579. num = 4;
  580. for (i = 0; i < num; i++) {
  581. alts = &iface->altsetting[i];
  582. altsd = get_iface_desc(alts);
  583. protocol = altsd->bInterfaceProtocol;
  584. /* skip invalid one */
  585. if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
  586. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  587. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
  588. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  589. altsd->bNumEndpoints < 1 ||
  590. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  591. continue;
  592. /* must be isochronous */
  593. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  594. USB_ENDPOINT_XFER_ISOC)
  595. continue;
  596. /* check direction */
  597. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  598. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  599. altno = altsd->bAlternateSetting;
  600. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  601. continue;
  602. /*
  603. * Roland audio streaming interfaces are marked with protocols
  604. * 0/1/2, but are UAC 1 compatible.
  605. */
  606. if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
  607. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  608. protocol <= 2)
  609. protocol = UAC_VERSION_1;
  610. chconfig = 0;
  611. /* get audio formats */
  612. switch (protocol) {
  613. default:
  614. dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
  615. iface_no, altno, protocol);
  616. protocol = UAC_VERSION_1;
  617. /* fall through */
  618. case UAC_VERSION_1: {
  619. struct uac1_as_header_descriptor *as =
  620. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  621. struct uac_input_terminal_descriptor *iterm;
  622. if (!as) {
  623. dev_err(&dev->dev,
  624. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  625. iface_no, altno);
  626. continue;
  627. }
  628. if (as->bLength < sizeof(*as)) {
  629. dev_err(&dev->dev,
  630. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  631. iface_no, altno);
  632. continue;
  633. }
  634. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  635. iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  636. as->bTerminalLink);
  637. if (iterm) {
  638. num_channels = iterm->bNrChannels;
  639. chconfig = le16_to_cpu(iterm->wChannelConfig);
  640. }
  641. break;
  642. }
  643. case UAC_VERSION_2: {
  644. struct uac2_input_terminal_descriptor *input_term;
  645. struct uac2_output_terminal_descriptor *output_term;
  646. struct uac2_as_header_descriptor *as =
  647. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  648. if (!as) {
  649. dev_err(&dev->dev,
  650. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  651. iface_no, altno);
  652. continue;
  653. }
  654. if (as->bLength < sizeof(*as)) {
  655. dev_err(&dev->dev,
  656. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  657. iface_no, altno);
  658. continue;
  659. }
  660. num_channels = as->bNrChannels;
  661. format = le32_to_cpu(as->bmFormats);
  662. chconfig = le32_to_cpu(as->bmChannelConfig);
  663. /* lookup the terminal associated to this interface
  664. * to extract the clock */
  665. input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  666. as->bTerminalLink);
  667. if (input_term) {
  668. clock = input_term->bCSourceID;
  669. if (!chconfig && (num_channels == input_term->bNrChannels))
  670. chconfig = le32_to_cpu(input_term->bmChannelConfig);
  671. break;
  672. }
  673. output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  674. as->bTerminalLink);
  675. if (output_term) {
  676. clock = output_term->bCSourceID;
  677. break;
  678. }
  679. dev_err(&dev->dev,
  680. "%u:%d : bogus bTerminalLink %d\n",
  681. iface_no, altno, as->bTerminalLink);
  682. continue;
  683. }
  684. case UAC_VERSION_3: {
  685. struct uac3_input_terminal_descriptor *input_term;
  686. struct uac3_output_terminal_descriptor *output_term;
  687. struct uac3_as_header_descriptor *as;
  688. struct uac3_cluster_header_descriptor *cluster;
  689. struct uac3_hc_descriptor_header hc_header;
  690. u16 cluster_id, wLength;
  691. as = snd_usb_find_csint_desc(alts->extra,
  692. alts->extralen,
  693. NULL, UAC_AS_GENERAL);
  694. if (!as) {
  695. dev_err(&dev->dev,
  696. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  697. iface_no, altno);
  698. continue;
  699. }
  700. if (as->bLength < sizeof(*as)) {
  701. dev_err(&dev->dev,
  702. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  703. iface_no, altno);
  704. continue;
  705. }
  706. cluster_id = le16_to_cpu(as->wClusterDescrID);
  707. if (!cluster_id) {
  708. dev_err(&dev->dev,
  709. "%u:%d : no cluster descriptor\n",
  710. iface_no, altno);
  711. continue;
  712. }
  713. /*
  714. * Get number of channels and channel map through
  715. * High Capability Cluster Descriptor
  716. *
  717. * First step: get High Capability header and
  718. * read size of Cluster Descriptor
  719. */
  720. err = snd_usb_ctl_msg(chip->dev,
  721. usb_rcvctrlpipe(chip->dev, 0),
  722. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  723. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  724. cluster_id,
  725. snd_usb_ctrl_intf(chip),
  726. &hc_header, sizeof(hc_header));
  727. if (err < 0)
  728. return err;
  729. else if (err != sizeof(hc_header)) {
  730. dev_err(&dev->dev,
  731. "%u:%d : can't get High Capability descriptor\n",
  732. iface_no, altno);
  733. return -EIO;
  734. }
  735. /*
  736. * Second step: allocate needed amount of memory
  737. * and request Cluster Descriptor
  738. */
  739. wLength = le16_to_cpu(hc_header.wLength);
  740. cluster = kzalloc(wLength, GFP_KERNEL);
  741. if (!cluster)
  742. return -ENOMEM;
  743. err = snd_usb_ctl_msg(chip->dev,
  744. usb_rcvctrlpipe(chip->dev, 0),
  745. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  746. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  747. cluster_id,
  748. snd_usb_ctrl_intf(chip),
  749. cluster, wLength);
  750. if (err < 0) {
  751. kfree(cluster);
  752. return err;
  753. } else if (err != wLength) {
  754. dev_err(&dev->dev,
  755. "%u:%d : can't get Cluster Descriptor\n",
  756. iface_no, altno);
  757. kfree(cluster);
  758. return -EIO;
  759. }
  760. num_channels = cluster->bNrChannels;
  761. chmap_v3 = convert_chmap_v3(cluster);
  762. kfree(cluster);
  763. format = le64_to_cpu(as->bmFormats);
  764. /* lookup the terminal associated to this interface
  765. * to extract the clock */
  766. input_term = snd_usb_find_input_terminal_descriptor(
  767. chip->ctrl_intf,
  768. as->bTerminalLink);
  769. if (input_term) {
  770. clock = input_term->bCSourceID;
  771. break;
  772. }
  773. output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  774. as->bTerminalLink);
  775. if (output_term) {
  776. clock = output_term->bCSourceID;
  777. break;
  778. }
  779. dev_err(&dev->dev,
  780. "%u:%d : bogus bTerminalLink %d\n",
  781. iface_no, altno, as->bTerminalLink);
  782. continue;
  783. }
  784. }
  785. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  786. /* get format type */
  787. fmt = snd_usb_find_csint_desc(alts->extra,
  788. alts->extralen,
  789. NULL, UAC_FORMAT_TYPE);
  790. if (!fmt) {
  791. dev_err(&dev->dev,
  792. "%u:%d : no UAC_FORMAT_TYPE desc\n",
  793. iface_no, altno);
  794. continue;
  795. }
  796. if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
  797. || ((protocol == UAC_VERSION_2) &&
  798. (fmt->bLength < 6))) {
  799. dev_err(&dev->dev,
  800. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  801. iface_no, altno);
  802. continue;
  803. }
  804. /*
  805. * Blue Microphones workaround: The last altsetting is
  806. * identical with the previous one, except for a larger
  807. * packet size, but is actually a mislabeled two-channel
  808. * setting; ignore it.
  809. */
  810. if (fmt->bNrChannels == 1 &&
  811. fmt->bSubframeSize == 2 &&
  812. altno == 2 && num == 3 &&
  813. fp && fp->altsetting == 1 && fp->channels == 1 &&
  814. fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  815. protocol == UAC_VERSION_1 &&
  816. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  817. fp->maxpacksize * 2)
  818. continue;
  819. }
  820. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  821. if (!fp)
  822. return -ENOMEM;
  823. fp->iface = iface_no;
  824. fp->altsetting = altno;
  825. fp->altset_idx = i;
  826. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  827. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  828. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  829. fp->protocol = protocol;
  830. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  831. fp->channels = num_channels;
  832. if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  833. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  834. * (fp->maxpacksize & 0x7ff);
  835. fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
  836. fp->clock = clock;
  837. INIT_LIST_HEAD(&fp->list);
  838. /* some quirks for attributes here */
  839. snd_usb_audioformat_attributes_quirk(chip, fp, stream);
  840. /* ok, let's parse further... */
  841. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  842. if (snd_usb_parse_audio_format(chip, fp, format,
  843. fmt, stream) < 0) {
  844. kfree(fp->rate_table);
  845. kfree(fp);
  846. fp = NULL;
  847. continue;
  848. }
  849. } else {
  850. struct uac3_as_header_descriptor *as;
  851. as = snd_usb_find_csint_desc(alts->extra,
  852. alts->extralen,
  853. NULL, UAC_AS_GENERAL);
  854. if (snd_usb_parse_audio_format_v3(chip, fp, as,
  855. stream) < 0) {
  856. kfree(fp->rate_table);
  857. kfree(fp);
  858. fp = NULL;
  859. continue;
  860. }
  861. }
  862. /* Create chmap */
  863. if (fp->channels != num_channels)
  864. chconfig = 0;
  865. if (protocol == UAC_VERSION_3)
  866. fp->chmap = chmap_v3;
  867. else
  868. fp->chmap = convert_chmap(fp->channels, chconfig,
  869. protocol);
  870. dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
  871. err = snd_usb_add_audio_stream(chip, stream, fp);
  872. if (err < 0) {
  873. list_del(&fp->list); /* unlink for avoiding double-free */
  874. kfree(fp->rate_table);
  875. kfree(fp->chmap);
  876. kfree(fp);
  877. return err;
  878. }
  879. /* try to set the interface... */
  880. usb_set_interface(chip->dev, iface_no, altno);
  881. snd_usb_init_pitch(chip, iface_no, alts, fp);
  882. snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
  883. }
  884. return 0;
  885. }