stream.c 30 KB

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