stream.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/usb.h>
  7. #include <linux/usb/audio.h>
  8. #include <linux/usb/audio-v2.h>
  9. #include <linux/usb/audio-v3.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/control.h>
  13. #include <sound/tlv.h>
  14. #include "usbaudio.h"
  15. #include "card.h"
  16. #include "proc.h"
  17. #include "quirks.h"
  18. #include "endpoint.h"
  19. #include "pcm.h"
  20. #include "helper.h"
  21. #include "format.h"
  22. #include "clock.h"
  23. #include "stream.h"
  24. #include "power.h"
  25. #include "media.h"
  26. static void audioformat_free(struct audioformat *fp)
  27. {
  28. list_del(&fp->list); /* unlink for avoiding double-free */
  29. kfree(fp->rate_table);
  30. kfree(fp->chmap);
  31. kfree(fp);
  32. }
  33. /*
  34. * free a substream
  35. */
  36. static void free_substream(struct snd_usb_substream *subs)
  37. {
  38. struct audioformat *fp, *n;
  39. if (!subs->num_formats)
  40. return; /* not initialized */
  41. list_for_each_entry_safe(fp, n, &subs->fmt_list, list)
  42. audioformat_free(fp);
  43. kfree(subs->str_pd);
  44. snd_media_stream_delete(subs);
  45. }
  46. /*
  47. * free a usb stream instance
  48. */
  49. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  50. {
  51. free_substream(&stream->substream[0]);
  52. free_substream(&stream->substream[1]);
  53. list_del(&stream->list);
  54. kfree(stream);
  55. }
  56. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  57. {
  58. struct snd_usb_stream *stream = pcm->private_data;
  59. if (stream) {
  60. stream->pcm = NULL;
  61. snd_usb_audio_stream_free(stream);
  62. }
  63. }
  64. /*
  65. * initialize the substream instance.
  66. */
  67. static void snd_usb_init_substream(struct snd_usb_stream *as,
  68. int stream,
  69. struct audioformat *fp,
  70. struct snd_usb_power_domain *pd)
  71. {
  72. struct snd_usb_substream *subs = &as->substream[stream];
  73. INIT_LIST_HEAD(&subs->fmt_list);
  74. spin_lock_init(&subs->lock);
  75. subs->stream = as;
  76. subs->direction = stream;
  77. subs->dev = as->chip->dev;
  78. subs->txfr_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_ALIGN_TRANSFER);
  79. subs->tx_length_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_TX_LENGTH);
  80. subs->speed = snd_usb_get_speed(subs->dev);
  81. subs->pkt_offset_adj = 0;
  82. subs->stream_offset_adj = 0;
  83. snd_usb_set_pcm_ops(as->pcm, stream);
  84. list_add_tail(&fp->list, &subs->fmt_list);
  85. subs->formats |= fp->formats;
  86. subs->num_formats++;
  87. subs->fmt_type = fp->fmt_type;
  88. subs->ep_num = fp->endpoint;
  89. if (fp->channels > subs->channels_max)
  90. subs->channels_max = fp->channels;
  91. if (pd) {
  92. subs->str_pd = pd;
  93. /* Initialize Power Domain to idle status D1 */
  94. snd_usb_power_domain_set(subs->stream->chip, pd,
  95. UAC3_PD_STATE_D1);
  96. }
  97. snd_usb_preallocate_buffer(subs);
  98. }
  99. /* kctl callbacks for usb-audio channel maps */
  100. static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  101. struct snd_ctl_elem_info *uinfo)
  102. {
  103. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  104. struct snd_usb_substream *subs = info->private_data;
  105. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  106. uinfo->count = subs->channels_max;
  107. uinfo->value.integer.min = 0;
  108. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  109. return 0;
  110. }
  111. /* check whether a duplicated entry exists in the audiofmt list */
  112. static bool have_dup_chmap(struct snd_usb_substream *subs,
  113. struct audioformat *fp)
  114. {
  115. struct audioformat *prev = fp;
  116. list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
  117. if (prev->chmap &&
  118. !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
  119. return true;
  120. }
  121. return false;
  122. }
  123. static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  124. unsigned int size, unsigned int __user *tlv)
  125. {
  126. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  127. struct snd_usb_substream *subs = info->private_data;
  128. struct audioformat *fp;
  129. unsigned int __user *dst;
  130. int count = 0;
  131. if (size < 8)
  132. return -ENOMEM;
  133. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  134. return -EFAULT;
  135. size -= 8;
  136. dst = tlv + 2;
  137. list_for_each_entry(fp, &subs->fmt_list, list) {
  138. int i, ch_bytes;
  139. if (!fp->chmap)
  140. continue;
  141. if (have_dup_chmap(subs, fp))
  142. continue;
  143. /* copy the entry */
  144. ch_bytes = fp->chmap->channels * 4;
  145. if (size < 8 + ch_bytes)
  146. return -ENOMEM;
  147. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  148. put_user(ch_bytes, dst + 1))
  149. return -EFAULT;
  150. dst += 2;
  151. for (i = 0; i < fp->chmap->channels; i++, dst++) {
  152. if (put_user(fp->chmap->map[i], dst))
  153. return -EFAULT;
  154. }
  155. count += 8 + ch_bytes;
  156. size -= 8 + ch_bytes;
  157. }
  158. if (put_user(count, tlv + 1))
  159. return -EFAULT;
  160. return 0;
  161. }
  162. static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  163. struct snd_ctl_elem_value *ucontrol)
  164. {
  165. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  166. struct snd_usb_substream *subs = info->private_data;
  167. struct snd_pcm_chmap_elem *chmap = NULL;
  168. int i = 0;
  169. if (subs->cur_audiofmt)
  170. chmap = subs->cur_audiofmt->chmap;
  171. if (chmap) {
  172. for (i = 0; i < chmap->channels; i++)
  173. ucontrol->value.integer.value[i] = chmap->map[i];
  174. }
  175. for (; i < subs->channels_max; i++)
  176. ucontrol->value.integer.value[i] = 0;
  177. return 0;
  178. }
  179. /* create a chmap kctl assigned to the given USB substream */
  180. static int add_chmap(struct snd_pcm *pcm, int stream,
  181. struct snd_usb_substream *subs)
  182. {
  183. struct audioformat *fp;
  184. struct snd_pcm_chmap *chmap;
  185. struct snd_kcontrol *kctl;
  186. int err;
  187. list_for_each_entry(fp, &subs->fmt_list, list)
  188. if (fp->chmap)
  189. goto ok;
  190. /* no chmap is found */
  191. return 0;
  192. ok:
  193. err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
  194. if (err < 0)
  195. return err;
  196. /* override handlers */
  197. chmap->private_data = subs;
  198. kctl = chmap->kctl;
  199. kctl->info = usb_chmap_ctl_info;
  200. kctl->get = usb_chmap_ctl_get;
  201. kctl->tlv.c = usb_chmap_ctl_tlv;
  202. return 0;
  203. }
  204. /* convert from USB ChannelConfig bits to ALSA chmap element */
  205. static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
  206. int protocol)
  207. {
  208. static const unsigned int uac1_maps[] = {
  209. SNDRV_CHMAP_FL, /* left front */
  210. SNDRV_CHMAP_FR, /* right front */
  211. SNDRV_CHMAP_FC, /* center front */
  212. SNDRV_CHMAP_LFE, /* LFE */
  213. SNDRV_CHMAP_RL, /* left surround */
  214. SNDRV_CHMAP_RR, /* right surround */
  215. SNDRV_CHMAP_FLC, /* left of center */
  216. SNDRV_CHMAP_FRC, /* right of center */
  217. SNDRV_CHMAP_RC, /* surround */
  218. SNDRV_CHMAP_SL, /* side left */
  219. SNDRV_CHMAP_SR, /* side right */
  220. SNDRV_CHMAP_TC, /* top */
  221. 0 /* terminator */
  222. };
  223. static const unsigned int uac2_maps[] = {
  224. SNDRV_CHMAP_FL, /* front left */
  225. SNDRV_CHMAP_FR, /* front right */
  226. SNDRV_CHMAP_FC, /* front center */
  227. SNDRV_CHMAP_LFE, /* LFE */
  228. SNDRV_CHMAP_RL, /* back left */
  229. SNDRV_CHMAP_RR, /* back right */
  230. SNDRV_CHMAP_FLC, /* front left of center */
  231. SNDRV_CHMAP_FRC, /* front right of center */
  232. SNDRV_CHMAP_RC, /* back center */
  233. SNDRV_CHMAP_SL, /* side left */
  234. SNDRV_CHMAP_SR, /* side right */
  235. SNDRV_CHMAP_TC, /* top center */
  236. SNDRV_CHMAP_TFL, /* top front left */
  237. SNDRV_CHMAP_TFC, /* top front center */
  238. SNDRV_CHMAP_TFR, /* top front right */
  239. SNDRV_CHMAP_TRL, /* top back left */
  240. SNDRV_CHMAP_TRC, /* top back center */
  241. SNDRV_CHMAP_TRR, /* top back right */
  242. SNDRV_CHMAP_TFLC, /* top front left of center */
  243. SNDRV_CHMAP_TFRC, /* top front right of center */
  244. SNDRV_CHMAP_LLFE, /* left LFE */
  245. SNDRV_CHMAP_RLFE, /* right LFE */
  246. SNDRV_CHMAP_TSL, /* top side left */
  247. SNDRV_CHMAP_TSR, /* top side right */
  248. SNDRV_CHMAP_BC, /* bottom center */
  249. SNDRV_CHMAP_RLC, /* back left of center */
  250. SNDRV_CHMAP_RRC, /* back right of center */
  251. 0 /* terminator */
  252. };
  253. struct snd_pcm_chmap_elem *chmap;
  254. const unsigned int *maps;
  255. int c;
  256. if (channels > ARRAY_SIZE(chmap->map))
  257. return NULL;
  258. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  259. if (!chmap)
  260. return NULL;
  261. maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
  262. chmap->channels = channels;
  263. c = 0;
  264. if (bits) {
  265. for (; bits && *maps; maps++, bits >>= 1) {
  266. if (bits & 1)
  267. chmap->map[c++] = *maps;
  268. if (c == chmap->channels)
  269. break;
  270. }
  271. } else {
  272. /* If we're missing wChannelConfig, then guess something
  273. to make sure the channel map is not skipped entirely */
  274. if (channels == 1)
  275. chmap->map[c++] = SNDRV_CHMAP_MONO;
  276. else
  277. for (; c < channels && *maps; maps++)
  278. chmap->map[c++] = *maps;
  279. }
  280. for (; c < channels; c++)
  281. chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
  282. return chmap;
  283. }
  284. /* UAC3 device stores channels information in Cluster Descriptors */
  285. static struct
  286. snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
  287. *cluster)
  288. {
  289. unsigned int channels = cluster->bNrChannels;
  290. struct snd_pcm_chmap_elem *chmap;
  291. void *p = cluster;
  292. int len, c;
  293. if (channels > ARRAY_SIZE(chmap->map))
  294. return NULL;
  295. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  296. if (!chmap)
  297. return NULL;
  298. len = le16_to_cpu(cluster->wLength);
  299. c = 0;
  300. p += sizeof(*cluster);
  301. len -= sizeof(*cluster);
  302. while (len > 0 && (c < channels)) {
  303. struct uac3_cluster_segment_descriptor *cs_desc = p;
  304. u16 cs_len;
  305. u8 cs_type;
  306. if (len < sizeof(*cs_desc))
  307. break;
  308. cs_len = le16_to_cpu(cs_desc->wLength);
  309. if (len < cs_len)
  310. break;
  311. cs_type = cs_desc->bSegmentType;
  312. if (cs_type == UAC3_CHANNEL_INFORMATION) {
  313. struct uac3_cluster_information_segment_descriptor *is = p;
  314. unsigned char map;
  315. if (cs_len < sizeof(*is))
  316. break;
  317. /*
  318. * TODO: this conversion is not complete, update it
  319. * after adding UAC3 values to asound.h
  320. */
  321. switch (is->bChRelationship) {
  322. case UAC3_CH_MONO:
  323. map = SNDRV_CHMAP_MONO;
  324. break;
  325. case UAC3_CH_LEFT:
  326. case UAC3_CH_FRONT_LEFT:
  327. case UAC3_CH_HEADPHONE_LEFT:
  328. map = SNDRV_CHMAP_FL;
  329. break;
  330. case UAC3_CH_RIGHT:
  331. case UAC3_CH_FRONT_RIGHT:
  332. case UAC3_CH_HEADPHONE_RIGHT:
  333. map = SNDRV_CHMAP_FR;
  334. break;
  335. case UAC3_CH_FRONT_CENTER:
  336. map = SNDRV_CHMAP_FC;
  337. break;
  338. case UAC3_CH_FRONT_LEFT_OF_CENTER:
  339. map = SNDRV_CHMAP_FLC;
  340. break;
  341. case UAC3_CH_FRONT_RIGHT_OF_CENTER:
  342. map = SNDRV_CHMAP_FRC;
  343. break;
  344. case UAC3_CH_SIDE_LEFT:
  345. map = SNDRV_CHMAP_SL;
  346. break;
  347. case UAC3_CH_SIDE_RIGHT:
  348. map = SNDRV_CHMAP_SR;
  349. break;
  350. case UAC3_CH_BACK_LEFT:
  351. map = SNDRV_CHMAP_RL;
  352. break;
  353. case UAC3_CH_BACK_RIGHT:
  354. map = SNDRV_CHMAP_RR;
  355. break;
  356. case UAC3_CH_BACK_CENTER:
  357. map = SNDRV_CHMAP_RC;
  358. break;
  359. case UAC3_CH_BACK_LEFT_OF_CENTER:
  360. map = SNDRV_CHMAP_RLC;
  361. break;
  362. case UAC3_CH_BACK_RIGHT_OF_CENTER:
  363. map = SNDRV_CHMAP_RRC;
  364. break;
  365. case UAC3_CH_TOP_CENTER:
  366. map = SNDRV_CHMAP_TC;
  367. break;
  368. case UAC3_CH_TOP_FRONT_LEFT:
  369. map = SNDRV_CHMAP_TFL;
  370. break;
  371. case UAC3_CH_TOP_FRONT_RIGHT:
  372. map = SNDRV_CHMAP_TFR;
  373. break;
  374. case UAC3_CH_TOP_FRONT_CENTER:
  375. map = SNDRV_CHMAP_TFC;
  376. break;
  377. case UAC3_CH_TOP_FRONT_LOC:
  378. map = SNDRV_CHMAP_TFLC;
  379. break;
  380. case UAC3_CH_TOP_FRONT_ROC:
  381. map = SNDRV_CHMAP_TFRC;
  382. break;
  383. case UAC3_CH_TOP_SIDE_LEFT:
  384. map = SNDRV_CHMAP_TSL;
  385. break;
  386. case UAC3_CH_TOP_SIDE_RIGHT:
  387. map = SNDRV_CHMAP_TSR;
  388. break;
  389. case UAC3_CH_TOP_BACK_LEFT:
  390. map = SNDRV_CHMAP_TRL;
  391. break;
  392. case UAC3_CH_TOP_BACK_RIGHT:
  393. map = SNDRV_CHMAP_TRR;
  394. break;
  395. case UAC3_CH_TOP_BACK_CENTER:
  396. map = SNDRV_CHMAP_TRC;
  397. break;
  398. case UAC3_CH_BOTTOM_CENTER:
  399. map = SNDRV_CHMAP_BC;
  400. break;
  401. case UAC3_CH_LOW_FREQUENCY_EFFECTS:
  402. map = SNDRV_CHMAP_LFE;
  403. break;
  404. case UAC3_CH_LFE_LEFT:
  405. map = SNDRV_CHMAP_LLFE;
  406. break;
  407. case UAC3_CH_LFE_RIGHT:
  408. map = SNDRV_CHMAP_RLFE;
  409. break;
  410. case UAC3_CH_RELATIONSHIP_UNDEFINED:
  411. default:
  412. map = SNDRV_CHMAP_UNKNOWN;
  413. break;
  414. }
  415. chmap->map[c++] = map;
  416. }
  417. p += cs_len;
  418. len -= cs_len;
  419. }
  420. if (channels < c)
  421. pr_err("%s: channel number mismatch\n", __func__);
  422. chmap->channels = channels;
  423. for (; c < channels; c++)
  424. chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
  425. return chmap;
  426. }
  427. /*
  428. * add this endpoint to the chip instance.
  429. * if a stream with the same endpoint already exists, append to it.
  430. * if not, create a new pcm stream. note, fp is added to the substream
  431. * fmt_list and will be freed on the chip instance release. do not free
  432. * fp or do remove it from the substream fmt_list to avoid double-free.
  433. */
  434. static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  435. int stream,
  436. struct audioformat *fp,
  437. struct snd_usb_power_domain *pd)
  438. {
  439. struct snd_usb_stream *as;
  440. struct snd_usb_substream *subs;
  441. struct snd_pcm *pcm;
  442. int err;
  443. list_for_each_entry(as, &chip->pcm_list, list) {
  444. if (as->fmt_type != fp->fmt_type)
  445. continue;
  446. subs = &as->substream[stream];
  447. if (subs->ep_num == fp->endpoint) {
  448. list_add_tail(&fp->list, &subs->fmt_list);
  449. subs->num_formats++;
  450. subs->formats |= fp->formats;
  451. return 0;
  452. }
  453. }
  454. if (chip->card->registered)
  455. chip->need_delayed_register = true;
  456. /* look for an empty stream */
  457. list_for_each_entry(as, &chip->pcm_list, list) {
  458. if (as->fmt_type != fp->fmt_type)
  459. continue;
  460. subs = &as->substream[stream];
  461. if (subs->ep_num)
  462. continue;
  463. err = snd_pcm_new_stream(as->pcm, stream, 1);
  464. if (err < 0)
  465. return err;
  466. snd_usb_init_substream(as, stream, fp, pd);
  467. return add_chmap(as->pcm, stream, subs);
  468. }
  469. /* create a new pcm */
  470. as = kzalloc(sizeof(*as), GFP_KERNEL);
  471. if (!as)
  472. return -ENOMEM;
  473. as->pcm_index = chip->pcm_devs;
  474. as->chip = chip;
  475. as->fmt_type = fp->fmt_type;
  476. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  477. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  478. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  479. &pcm);
  480. if (err < 0) {
  481. kfree(as);
  482. return err;
  483. }
  484. as->pcm = pcm;
  485. pcm->private_data = as;
  486. pcm->private_free = snd_usb_audio_pcm_free;
  487. pcm->info_flags = 0;
  488. if (chip->pcm_devs > 0)
  489. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  490. else
  491. strcpy(pcm->name, "USB Audio");
  492. snd_usb_init_substream(as, stream, fp, pd);
  493. /*
  494. * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
  495. * fix to swap capture stream order in conf/cards/USB-audio.conf
  496. */
  497. if (chip->usb_id == USB_ID(0x0763, 0x2003))
  498. list_add(&as->list, &chip->pcm_list);
  499. else
  500. list_add_tail(&as->list, &chip->pcm_list);
  501. chip->pcm_devs++;
  502. snd_usb_proc_pcm_format_add(as);
  503. return add_chmap(pcm, stream, &as->substream[stream]);
  504. }
  505. int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  506. int stream,
  507. struct audioformat *fp)
  508. {
  509. return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
  510. }
  511. static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
  512. int stream,
  513. struct audioformat *fp,
  514. struct snd_usb_power_domain *pd)
  515. {
  516. return __snd_usb_add_audio_stream(chip, stream, fp, pd);
  517. }
  518. static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  519. struct usb_host_interface *alts,
  520. int protocol, int iface_no)
  521. {
  522. /* parsed with a v1 header here. that's ok as we only look at the
  523. * header first which is the same for both versions */
  524. struct uac_iso_endpoint_descriptor *csep;
  525. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  526. int attributes = 0;
  527. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  528. /* Creamware Noah has this descriptor after the 2nd endpoint */
  529. if (!csep && altsd->bNumEndpoints >= 2)
  530. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  531. /*
  532. * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
  533. * bytes after the first endpoint, go search the entire interface.
  534. * Some devices have it directly *before* the standard endpoint.
  535. */
  536. if (!csep)
  537. csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
  538. if (!csep || csep->bLength < 7 ||
  539. csep->bDescriptorSubtype != UAC_EP_GENERAL)
  540. goto error;
  541. if (protocol == UAC_VERSION_1) {
  542. attributes = csep->bmAttributes;
  543. } else if (protocol == UAC_VERSION_2) {
  544. struct uac2_iso_endpoint_descriptor *csep2 =
  545. (struct uac2_iso_endpoint_descriptor *) csep;
  546. if (csep2->bLength < sizeof(*csep2))
  547. goto error;
  548. attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  549. /* emulate the endpoint attributes of a v1 device */
  550. if (csep2->bmControls & UAC2_CONTROL_PITCH)
  551. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  552. } else { /* UAC_VERSION_3 */
  553. struct uac3_iso_endpoint_descriptor *csep3 =
  554. (struct uac3_iso_endpoint_descriptor *) csep;
  555. if (csep3->bLength < sizeof(*csep3))
  556. goto error;
  557. /* emulate the endpoint attributes of a v1 device */
  558. if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
  559. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  560. }
  561. return attributes;
  562. error:
  563. usb_audio_warn(chip,
  564. "%u:%d : no or invalid class specific endpoint descriptor\n",
  565. iface_no, altsd->bAlternateSetting);
  566. return 0;
  567. }
  568. /* find an input terminal descriptor (either UAC1 or UAC2) with the given
  569. * terminal id
  570. */
  571. static void *
  572. snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  573. int terminal_id, int protocol)
  574. {
  575. struct uac2_input_terminal_descriptor *term = NULL;
  576. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  577. ctrl_iface->extralen,
  578. term, UAC_INPUT_TERMINAL))) {
  579. if (!snd_usb_validate_audio_desc(term, protocol))
  580. continue;
  581. if (term->bTerminalID == terminal_id)
  582. return term;
  583. }
  584. return NULL;
  585. }
  586. static void *
  587. snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  588. int terminal_id, int protocol)
  589. {
  590. /* OK to use with both UAC2 and UAC3 */
  591. struct uac2_output_terminal_descriptor *term = NULL;
  592. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  593. ctrl_iface->extralen,
  594. term, UAC_OUTPUT_TERMINAL))) {
  595. if (!snd_usb_validate_audio_desc(term, protocol))
  596. continue;
  597. if (term->bTerminalID == terminal_id)
  598. return term;
  599. }
  600. return NULL;
  601. }
  602. static struct audioformat *
  603. audio_format_alloc_init(struct snd_usb_audio *chip,
  604. struct usb_host_interface *alts,
  605. int protocol, int iface_no, int altset_idx,
  606. int altno, int num_channels, int clock)
  607. {
  608. struct audioformat *fp;
  609. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  610. if (!fp)
  611. return NULL;
  612. fp->iface = iface_no;
  613. fp->altsetting = altno;
  614. fp->altset_idx = altset_idx;
  615. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  616. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  617. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  618. fp->protocol = protocol;
  619. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  620. fp->channels = num_channels;
  621. if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH)
  622. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  623. * (fp->maxpacksize & 0x7ff);
  624. fp->clock = clock;
  625. INIT_LIST_HEAD(&fp->list);
  626. return fp;
  627. }
  628. static struct audioformat *
  629. snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
  630. struct usb_host_interface *alts,
  631. int protocol, int iface_no, int altset_idx,
  632. int altno, int stream, int bm_quirk)
  633. {
  634. struct usb_device *dev = chip->dev;
  635. struct uac_format_type_i_continuous_descriptor *fmt;
  636. unsigned int num_channels = 0, chconfig = 0;
  637. struct usb_host_interface *ctrl_intf;
  638. struct audioformat *fp;
  639. int clock = 0;
  640. u64 format;
  641. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  642. /* get audio formats */
  643. if (protocol == UAC_VERSION_1) {
  644. struct uac1_as_header_descriptor *as =
  645. snd_usb_find_csint_desc(alts->extra, alts->extralen,
  646. NULL, UAC_AS_GENERAL);
  647. struct uac_input_terminal_descriptor *iterm;
  648. if (!as) {
  649. dev_err(&dev->dev,
  650. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  651. iface_no, altno);
  652. return NULL;
  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. return NULL;
  659. }
  660. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  661. iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  662. as->bTerminalLink,
  663. protocol);
  664. if (iterm) {
  665. num_channels = iterm->bNrChannels;
  666. chconfig = le16_to_cpu(iterm->wChannelConfig);
  667. }
  668. } else { /* UAC_VERSION_2 */
  669. struct uac2_input_terminal_descriptor *input_term;
  670. struct uac2_output_terminal_descriptor *output_term;
  671. struct uac2_as_header_descriptor *as =
  672. snd_usb_find_csint_desc(alts->extra, alts->extralen,
  673. NULL, UAC_AS_GENERAL);
  674. if (!as) {
  675. dev_err(&dev->dev,
  676. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  677. iface_no, altno);
  678. return NULL;
  679. }
  680. if (as->bLength < sizeof(*as)) {
  681. dev_err(&dev->dev,
  682. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  683. iface_no, altno);
  684. return NULL;
  685. }
  686. num_channels = as->bNrChannels;
  687. format = le32_to_cpu(as->bmFormats);
  688. chconfig = le32_to_cpu(as->bmChannelConfig);
  689. /*
  690. * lookup the terminal associated to this interface
  691. * to extract the clock
  692. */
  693. input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  694. as->bTerminalLink,
  695. protocol);
  696. if (input_term) {
  697. clock = input_term->bCSourceID;
  698. if (!chconfig && (num_channels == input_term->bNrChannels))
  699. chconfig = le32_to_cpu(input_term->bmChannelConfig);
  700. goto found_clock;
  701. }
  702. output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
  703. as->bTerminalLink,
  704. protocol);
  705. if (output_term) {
  706. clock = output_term->bCSourceID;
  707. goto found_clock;
  708. }
  709. dev_err(&dev->dev,
  710. "%u:%d : bogus bTerminalLink %d\n",
  711. iface_no, altno, as->bTerminalLink);
  712. return NULL;
  713. }
  714. found_clock:
  715. /* get format type */
  716. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  717. NULL, UAC_FORMAT_TYPE);
  718. if (!fmt) {
  719. dev_err(&dev->dev,
  720. "%u:%d : no UAC_FORMAT_TYPE desc\n",
  721. iface_no, altno);
  722. return NULL;
  723. }
  724. if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
  725. || ((protocol == UAC_VERSION_2) &&
  726. (fmt->bLength < 6))) {
  727. dev_err(&dev->dev,
  728. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  729. iface_no, altno);
  730. return NULL;
  731. }
  732. /*
  733. * Blue Microphones workaround: The last altsetting is
  734. * identical with the previous one, except for a larger
  735. * packet size, but is actually a mislabeled two-channel
  736. * setting; ignore it.
  737. *
  738. * Part 2: analyze quirk flag and format
  739. */
  740. if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
  741. return NULL;
  742. fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
  743. altset_idx, altno, num_channels, clock);
  744. if (!fp)
  745. return ERR_PTR(-ENOMEM);
  746. fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
  747. iface_no);
  748. /* some quirks for attributes here */
  749. snd_usb_audioformat_attributes_quirk(chip, fp, stream);
  750. /* ok, let's parse further... */
  751. if (snd_usb_parse_audio_format(chip, fp, format,
  752. fmt, stream) < 0) {
  753. audioformat_free(fp);
  754. return NULL;
  755. }
  756. /* Create chmap */
  757. if (fp->channels != num_channels)
  758. chconfig = 0;
  759. fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
  760. return fp;
  761. }
  762. static struct audioformat *
  763. snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
  764. struct usb_host_interface *alts,
  765. struct snd_usb_power_domain **pd_out,
  766. int iface_no, int altset_idx,
  767. int altno, int stream)
  768. {
  769. struct usb_device *dev = chip->dev;
  770. struct uac3_input_terminal_descriptor *input_term;
  771. struct uac3_output_terminal_descriptor *output_term;
  772. struct uac3_cluster_header_descriptor *cluster;
  773. struct uac3_as_header_descriptor *as = NULL;
  774. struct uac3_hc_descriptor_header hc_header;
  775. struct usb_host_interface *ctrl_intf;
  776. struct snd_pcm_chmap_elem *chmap;
  777. struct snd_usb_power_domain *pd;
  778. unsigned char badd_profile;
  779. u64 badd_formats = 0;
  780. unsigned int num_channels;
  781. struct audioformat *fp;
  782. u16 cluster_id, wLength, cluster_wLength;
  783. int clock = 0;
  784. int err;
  785. badd_profile = chip->badd_profile;
  786. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  787. if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  788. unsigned int maxpacksize =
  789. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  790. switch (maxpacksize) {
  791. default:
  792. dev_err(&dev->dev,
  793. "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
  794. iface_no, altno);
  795. return NULL;
  796. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
  797. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
  798. badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
  799. num_channels = 1;
  800. break;
  801. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
  802. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
  803. badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
  804. num_channels = 1;
  805. break;
  806. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
  807. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
  808. badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
  809. num_channels = 2;
  810. break;
  811. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
  812. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
  813. badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
  814. num_channels = 2;
  815. break;
  816. }
  817. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  818. if (!chmap)
  819. return ERR_PTR(-ENOMEM);
  820. if (num_channels == 1) {
  821. chmap->map[0] = SNDRV_CHMAP_MONO;
  822. } else {
  823. chmap->map[0] = SNDRV_CHMAP_FL;
  824. chmap->map[1] = SNDRV_CHMAP_FR;
  825. }
  826. chmap->channels = num_channels;
  827. clock = UAC3_BADD_CS_ID9;
  828. goto found_clock;
  829. }
  830. as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  831. NULL, UAC_AS_GENERAL);
  832. if (!as) {
  833. dev_err(&dev->dev,
  834. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  835. iface_no, altno);
  836. return NULL;
  837. }
  838. if (as->bLength < sizeof(*as)) {
  839. dev_err(&dev->dev,
  840. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  841. iface_no, altno);
  842. return NULL;
  843. }
  844. cluster_id = le16_to_cpu(as->wClusterDescrID);
  845. if (!cluster_id) {
  846. dev_err(&dev->dev,
  847. "%u:%d : no cluster descriptor\n",
  848. iface_no, altno);
  849. return NULL;
  850. }
  851. /*
  852. * Get number of channels and channel map through
  853. * High Capability Cluster Descriptor
  854. *
  855. * First step: get High Capability header and
  856. * read size of Cluster Descriptor
  857. */
  858. err = snd_usb_ctl_msg(chip->dev,
  859. usb_rcvctrlpipe(chip->dev, 0),
  860. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  861. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  862. cluster_id,
  863. snd_usb_ctrl_intf(ctrl_intf),
  864. &hc_header, sizeof(hc_header));
  865. if (err < 0)
  866. return ERR_PTR(err);
  867. else if (err != sizeof(hc_header)) {
  868. dev_err(&dev->dev,
  869. "%u:%d : can't get High Capability descriptor\n",
  870. iface_no, altno);
  871. return ERR_PTR(-EIO);
  872. }
  873. /*
  874. * Second step: allocate needed amount of memory
  875. * and request Cluster Descriptor
  876. */
  877. wLength = le16_to_cpu(hc_header.wLength);
  878. if (wLength < sizeof(cluster))
  879. return NULL;
  880. cluster = kzalloc(wLength, GFP_KERNEL);
  881. if (!cluster)
  882. return ERR_PTR(-ENOMEM);
  883. err = snd_usb_ctl_msg(chip->dev,
  884. usb_rcvctrlpipe(chip->dev, 0),
  885. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  886. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  887. cluster_id,
  888. snd_usb_ctrl_intf(ctrl_intf),
  889. cluster, wLength);
  890. if (err < 0) {
  891. kfree(cluster);
  892. return ERR_PTR(err);
  893. } else if (err != wLength) {
  894. dev_err(&dev->dev,
  895. "%u:%d : can't get Cluster Descriptor\n",
  896. iface_no, altno);
  897. kfree(cluster);
  898. return ERR_PTR(-EIO);
  899. }
  900. cluster_wLength = le16_to_cpu(cluster->wLength);
  901. if (cluster_wLength < sizeof(*cluster) ||
  902. cluster_wLength > wLength) {
  903. dev_err(&dev->dev,
  904. "%u:%d : invalid Cluster Descriptor size\n",
  905. iface_no, altno);
  906. kfree(cluster);
  907. return ERR_PTR(-EIO);
  908. }
  909. num_channels = cluster->bNrChannels;
  910. chmap = convert_chmap_v3(cluster);
  911. kfree(cluster);
  912. /*
  913. * lookup the terminal associated to this interface
  914. * to extract the clock
  915. */
  916. input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  917. as->bTerminalLink,
  918. UAC_VERSION_3);
  919. if (input_term) {
  920. clock = input_term->bCSourceID;
  921. goto found_clock;
  922. }
  923. output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
  924. as->bTerminalLink,
  925. UAC_VERSION_3);
  926. if (output_term) {
  927. clock = output_term->bCSourceID;
  928. goto found_clock;
  929. }
  930. dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
  931. iface_no, altno, as->bTerminalLink);
  932. kfree(chmap);
  933. return NULL;
  934. found_clock:
  935. fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
  936. altset_idx, altno, num_channels, clock);
  937. if (!fp) {
  938. kfree(chmap);
  939. return ERR_PTR(-ENOMEM);
  940. }
  941. fp->chmap = chmap;
  942. if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  943. fp->attributes = 0; /* No attributes */
  944. fp->fmt_type = UAC_FORMAT_TYPE_I;
  945. fp->formats = badd_formats;
  946. fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */
  947. fp->rate_min = UAC3_BADD_SAMPLING_RATE;
  948. fp->rate_max = UAC3_BADD_SAMPLING_RATE;
  949. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  950. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  951. if (!pd) {
  952. audioformat_free(fp);
  953. return NULL;
  954. }
  955. pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  956. UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
  957. pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
  958. pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
  959. pd->ctrl_iface = ctrl_intf;
  960. } else {
  961. fp->attributes = parse_uac_endpoint_attributes(chip, alts,
  962. UAC_VERSION_3,
  963. iface_no);
  964. pd = snd_usb_find_power_domain(ctrl_intf,
  965. as->bTerminalLink);
  966. /* ok, let's parse further... */
  967. if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
  968. kfree(pd);
  969. audioformat_free(fp);
  970. return NULL;
  971. }
  972. }
  973. if (pd)
  974. *pd_out = pd;
  975. return fp;
  976. }
  977. static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
  978. int iface_no,
  979. bool *has_non_pcm, bool non_pcm)
  980. {
  981. struct usb_device *dev;
  982. struct usb_interface *iface;
  983. struct usb_host_interface *alts;
  984. struct usb_interface_descriptor *altsd;
  985. int i, altno, err, stream;
  986. struct audioformat *fp = NULL;
  987. struct snd_usb_power_domain *pd = NULL;
  988. bool set_iface_first;
  989. int num, protocol;
  990. dev = chip->dev;
  991. /* parse the interface's altsettings */
  992. iface = usb_ifnum_to_if(dev, iface_no);
  993. num = iface->num_altsetting;
  994. /*
  995. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  996. * one misses syncpipe, and does not produce any sound.
  997. */
  998. if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
  999. num = 4;
  1000. for (i = 0; i < num; i++) {
  1001. alts = &iface->altsetting[i];
  1002. altsd = get_iface_desc(alts);
  1003. protocol = altsd->bInterfaceProtocol;
  1004. /* skip invalid one */
  1005. if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
  1006. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  1007. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
  1008. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  1009. altsd->bNumEndpoints < 1 ||
  1010. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  1011. continue;
  1012. /* must be isochronous */
  1013. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1014. USB_ENDPOINT_XFER_ISOC)
  1015. continue;
  1016. /* check direction */
  1017. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  1018. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  1019. altno = altsd->bAlternateSetting;
  1020. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  1021. continue;
  1022. /*
  1023. * Roland audio streaming interfaces are marked with protocols
  1024. * 0/1/2, but are UAC 1 compatible.
  1025. */
  1026. if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
  1027. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  1028. protocol <= 2)
  1029. protocol = UAC_VERSION_1;
  1030. switch (protocol) {
  1031. default:
  1032. dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
  1033. iface_no, altno, protocol);
  1034. protocol = UAC_VERSION_1;
  1035. fallthrough;
  1036. case UAC_VERSION_1:
  1037. case UAC_VERSION_2: {
  1038. int bm_quirk = 0;
  1039. /*
  1040. * Blue Microphones workaround: The last altsetting is
  1041. * identical with the previous one, except for a larger
  1042. * packet size, but is actually a mislabeled two-channel
  1043. * setting; ignore it.
  1044. *
  1045. * Part 1: prepare quirk flag
  1046. */
  1047. if (altno == 2 && num == 3 &&
  1048. fp && fp->altsetting == 1 && fp->channels == 1 &&
  1049. fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  1050. protocol == UAC_VERSION_1 &&
  1051. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  1052. fp->maxpacksize * 2)
  1053. bm_quirk = 1;
  1054. fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
  1055. iface_no, i, altno,
  1056. stream, bm_quirk);
  1057. break;
  1058. }
  1059. case UAC_VERSION_3:
  1060. fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
  1061. iface_no, i, altno, stream);
  1062. break;
  1063. }
  1064. if (!fp)
  1065. continue;
  1066. else if (IS_ERR(fp))
  1067. return PTR_ERR(fp);
  1068. if (fp->fmt_type != UAC_FORMAT_TYPE_I)
  1069. *has_non_pcm = true;
  1070. if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
  1071. audioformat_free(fp);
  1072. kfree(pd);
  1073. fp = NULL;
  1074. pd = NULL;
  1075. continue;
  1076. }
  1077. snd_usb_audioformat_set_sync_ep(chip, fp);
  1078. dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
  1079. if (protocol == UAC_VERSION_3)
  1080. err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
  1081. else
  1082. err = snd_usb_add_audio_stream(chip, stream, fp);
  1083. if (err < 0) {
  1084. audioformat_free(fp);
  1085. kfree(pd);
  1086. return err;
  1087. }
  1088. /* add endpoints */
  1089. err = snd_usb_add_endpoint(chip, fp->endpoint,
  1090. SND_USB_ENDPOINT_TYPE_DATA);
  1091. if (err < 0)
  1092. return err;
  1093. if (fp->sync_ep) {
  1094. err = snd_usb_add_endpoint(chip, fp->sync_ep,
  1095. fp->implicit_fb ?
  1096. SND_USB_ENDPOINT_TYPE_DATA :
  1097. SND_USB_ENDPOINT_TYPE_SYNC);
  1098. if (err < 0)
  1099. return err;
  1100. }
  1101. set_iface_first = false;
  1102. if (protocol == UAC_VERSION_1 ||
  1103. (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
  1104. set_iface_first = true;
  1105. /* try to set the interface... */
  1106. usb_set_interface(chip->dev, iface_no, 0);
  1107. if (set_iface_first)
  1108. usb_set_interface(chip->dev, iface_no, altno);
  1109. snd_usb_init_pitch(chip, fp);
  1110. snd_usb_init_sample_rate(chip, fp, fp->rate_max);
  1111. if (!set_iface_first)
  1112. usb_set_interface(chip->dev, iface_no, altno);
  1113. }
  1114. return 0;
  1115. }
  1116. int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  1117. {
  1118. int err;
  1119. bool has_non_pcm = false;
  1120. /* parse PCM formats */
  1121. err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
  1122. if (err < 0)
  1123. return err;
  1124. if (has_non_pcm) {
  1125. /* parse non-PCM formats */
  1126. err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
  1127. if (err < 0)
  1128. return err;
  1129. }
  1130. return 0;
  1131. }