stream.c 33 KB

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