stream.c 33 KB

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