stream.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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_obj(*chmap);
  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_obj(*chmap);
  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_obj(*as);
  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. scnprintf(pcm->name, sizeof(pcm->name), "USB Audio #%d",
  490. chip->pcm_devs);
  491. else
  492. strscpy(pcm->name, "USB Audio");
  493. snd_usb_init_substream(as, stream, fp, pd);
  494. /*
  495. * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
  496. * fix to swap capture stream order in conf/cards/USB-audio.conf
  497. */
  498. if (chip->usb_id == USB_ID(0x0763, 0x2003))
  499. list_add(&as->list, &chip->pcm_list);
  500. else
  501. list_add_tail(&as->list, &chip->pcm_list);
  502. chip->pcm_devs++;
  503. snd_usb_proc_pcm_format_add(as);
  504. return add_chmap(pcm, stream, &as->substream[stream]);
  505. }
  506. int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  507. int stream,
  508. struct audioformat *fp)
  509. {
  510. return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
  511. }
  512. static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
  513. int stream,
  514. struct audioformat *fp,
  515. struct snd_usb_power_domain *pd)
  516. {
  517. return __snd_usb_add_audio_stream(chip, stream, fp, pd);
  518. }
  519. static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  520. struct usb_host_interface *alts,
  521. int protocol, int iface_no)
  522. {
  523. /* parsed with a v1 header here. that's ok as we only look at the
  524. * header first which is the same for both versions */
  525. struct uac_iso_endpoint_descriptor *csep;
  526. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  527. int attributes = 0;
  528. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  529. /* Creamware Noah has this descriptor after the 2nd endpoint */
  530. if (!csep && altsd->bNumEndpoints >= 2)
  531. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  532. /*
  533. * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
  534. * bytes after the first endpoint, go search the entire interface.
  535. * Some devices have it directly *before* the standard endpoint.
  536. */
  537. if (!csep)
  538. csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
  539. if (!csep || csep->bLength < 7 ||
  540. csep->bDescriptorSubtype != UAC_EP_GENERAL)
  541. goto error;
  542. if (protocol == UAC_VERSION_1) {
  543. attributes = csep->bmAttributes;
  544. } else if (protocol == UAC_VERSION_2) {
  545. struct uac2_iso_endpoint_descriptor *csep2 =
  546. (struct uac2_iso_endpoint_descriptor *) csep;
  547. if (csep2->bLength < sizeof(*csep2))
  548. goto error;
  549. attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  550. /* emulate the endpoint attributes of a v1 device */
  551. if (csep2->bmControls & UAC2_CONTROL_PITCH)
  552. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  553. } else { /* UAC_VERSION_3 */
  554. struct uac3_iso_endpoint_descriptor *csep3 =
  555. (struct uac3_iso_endpoint_descriptor *) csep;
  556. if (csep3->bLength < sizeof(*csep3))
  557. goto error;
  558. /* emulate the endpoint attributes of a v1 device */
  559. if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
  560. attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  561. }
  562. return attributes;
  563. error:
  564. usb_audio_warn(chip,
  565. "%u:%d : no or invalid class specific endpoint descriptor\n",
  566. iface_no, altsd->bAlternateSetting);
  567. return 0;
  568. }
  569. /* find an input terminal descriptor (either UAC1 or UAC2) with the given
  570. * terminal id
  571. */
  572. static void *
  573. snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  574. int terminal_id, int protocol)
  575. {
  576. struct uac2_input_terminal_descriptor *term = NULL;
  577. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  578. ctrl_iface->extralen,
  579. term, UAC_INPUT_TERMINAL))) {
  580. if (!snd_usb_validate_audio_desc(term, protocol))
  581. continue;
  582. if (term->bTerminalID == terminal_id)
  583. return term;
  584. }
  585. return NULL;
  586. }
  587. static void *
  588. snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  589. int terminal_id, int protocol)
  590. {
  591. /* OK to use with both UAC2 and UAC3 */
  592. struct uac2_output_terminal_descriptor *term = NULL;
  593. while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  594. ctrl_iface->extralen,
  595. term, UAC_OUTPUT_TERMINAL))) {
  596. if (!snd_usb_validate_audio_desc(term, protocol))
  597. continue;
  598. if (term->bTerminalID == terminal_id)
  599. return term;
  600. }
  601. return NULL;
  602. }
  603. static struct audioformat *
  604. audio_format_alloc_init(struct snd_usb_audio *chip,
  605. struct usb_host_interface *alts,
  606. int protocol, int iface_no, int altset_idx,
  607. int altno, int num_channels, int clock)
  608. {
  609. struct usb_host_endpoint *ep = &alts->endpoint[0];
  610. struct audioformat *fp;
  611. fp = kzalloc_obj(*fp);
  612. if (!fp)
  613. return NULL;
  614. fp->iface = iface_no;
  615. fp->altsetting = altno;
  616. fp->altset_idx = altset_idx;
  617. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  618. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  619. fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  620. fp->protocol = protocol;
  621. fp->maxpacksize = usb_endpoint_max_periodic_payload(chip->dev, ep);
  622. fp->channels = num_channels;
  623. fp->clock = clock;
  624. INIT_LIST_HEAD(&fp->list);
  625. return fp;
  626. }
  627. static struct audioformat *
  628. snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
  629. struct usb_host_interface *alts,
  630. int protocol, int iface_no, int altset_idx,
  631. int altno, int stream, int bm_quirk)
  632. {
  633. struct usb_device *dev = chip->dev;
  634. struct uac_format_type_i_continuous_descriptor *fmt;
  635. unsigned int num_channels = 0, chconfig = 0;
  636. struct usb_host_interface *ctrl_intf;
  637. struct audioformat *fp;
  638. int clock = 0;
  639. u64 format;
  640. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  641. /* get audio formats */
  642. if (protocol == UAC_VERSION_1) {
  643. struct uac1_as_header_descriptor *as =
  644. snd_usb_find_csint_desc(alts->extra, alts->extralen,
  645. NULL, UAC_AS_GENERAL);
  646. struct uac_input_terminal_descriptor *iterm;
  647. if (!as) {
  648. dev_err(&dev->dev,
  649. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  650. iface_no, altno);
  651. return NULL;
  652. }
  653. if (as->bLength < sizeof(*as)) {
  654. dev_err(&dev->dev,
  655. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  656. iface_no, altno);
  657. return NULL;
  658. }
  659. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  660. iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  661. as->bTerminalLink,
  662. protocol);
  663. if (iterm) {
  664. num_channels = iterm->bNrChannels;
  665. chconfig = le16_to_cpu(iterm->wChannelConfig);
  666. }
  667. } else { /* UAC_VERSION_2 */
  668. struct uac2_input_terminal_descriptor *input_term;
  669. struct uac2_output_terminal_descriptor *output_term;
  670. struct uac2_as_header_descriptor *as =
  671. snd_usb_find_csint_desc(alts->extra, alts->extralen,
  672. NULL, UAC_AS_GENERAL);
  673. if (!as) {
  674. dev_err(&dev->dev,
  675. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  676. iface_no, altno);
  677. return NULL;
  678. }
  679. if (as->bLength < sizeof(*as)) {
  680. dev_err(&dev->dev,
  681. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  682. iface_no, altno);
  683. return NULL;
  684. }
  685. num_channels = as->bNrChannels;
  686. format = le32_to_cpu(as->bmFormats);
  687. chconfig = le32_to_cpu(as->bmChannelConfig);
  688. /*
  689. * lookup the terminal associated to this interface
  690. * to extract the clock
  691. */
  692. input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  693. as->bTerminalLink,
  694. protocol);
  695. if (input_term) {
  696. clock = input_term->bCSourceID;
  697. if (!chconfig && (num_channels == input_term->bNrChannels))
  698. chconfig = le32_to_cpu(input_term->bmChannelConfig);
  699. goto found_clock;
  700. }
  701. output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
  702. as->bTerminalLink,
  703. protocol);
  704. if (output_term) {
  705. clock = output_term->bCSourceID;
  706. goto found_clock;
  707. }
  708. dev_err(&dev->dev,
  709. "%u:%d : bogus bTerminalLink %d\n",
  710. iface_no, altno, as->bTerminalLink);
  711. return NULL;
  712. }
  713. found_clock:
  714. /* get format type */
  715. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  716. NULL, UAC_FORMAT_TYPE);
  717. if (!fmt) {
  718. dev_err(&dev->dev,
  719. "%u:%d : no UAC_FORMAT_TYPE desc\n",
  720. iface_no, altno);
  721. return NULL;
  722. }
  723. if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
  724. || ((protocol == UAC_VERSION_2) &&
  725. (fmt->bLength < 6))) {
  726. dev_err(&dev->dev,
  727. "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  728. iface_no, altno);
  729. return NULL;
  730. }
  731. /*
  732. * Blue Microphones workaround: The last altsetting is
  733. * identical with the previous one, except for a larger
  734. * packet size, but is actually a mislabeled two-channel
  735. * setting; ignore it.
  736. *
  737. * Part 2: analyze quirk flag and format
  738. */
  739. if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
  740. return NULL;
  741. fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
  742. altset_idx, altno, num_channels, clock);
  743. if (!fp)
  744. return ERR_PTR(-ENOMEM);
  745. fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
  746. iface_no);
  747. /* some quirks for attributes here */
  748. snd_usb_audioformat_attributes_quirk(chip, fp, stream);
  749. /* ok, let's parse further... */
  750. if (snd_usb_parse_audio_format(chip, fp, format,
  751. fmt, stream) < 0) {
  752. audioformat_free(fp);
  753. return NULL;
  754. }
  755. /* Create chmap */
  756. if (fp->channels != num_channels)
  757. chconfig = 0;
  758. fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
  759. return fp;
  760. }
  761. static struct audioformat *
  762. snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
  763. struct usb_host_interface *alts,
  764. struct snd_usb_power_domain **pd_out,
  765. int iface_no, int altset_idx,
  766. int altno, int stream)
  767. {
  768. struct usb_device *dev = chip->dev;
  769. struct uac3_input_terminal_descriptor *input_term;
  770. struct uac3_output_terminal_descriptor *output_term;
  771. struct uac3_cluster_header_descriptor *cluster;
  772. struct uac3_as_header_descriptor *as = NULL;
  773. struct uac3_hc_descriptor_header hc_header;
  774. struct usb_host_interface *ctrl_intf;
  775. struct snd_pcm_chmap_elem *chmap;
  776. struct snd_usb_power_domain *pd;
  777. unsigned char badd_profile;
  778. u64 badd_formats = 0;
  779. unsigned int num_channels;
  780. struct audioformat *fp;
  781. u16 cluster_id, wLength, cluster_wLength;
  782. int clock = 0;
  783. int err;
  784. badd_profile = chip->badd_profile;
  785. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  786. if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  787. unsigned int maxpacksize =
  788. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  789. switch (maxpacksize) {
  790. default:
  791. dev_err(&dev->dev,
  792. "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
  793. iface_no, altno);
  794. return NULL;
  795. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
  796. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
  797. badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
  798. num_channels = 1;
  799. break;
  800. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
  801. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
  802. badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
  803. num_channels = 1;
  804. break;
  805. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
  806. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
  807. badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
  808. num_channels = 2;
  809. break;
  810. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
  811. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
  812. badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
  813. num_channels = 2;
  814. break;
  815. }
  816. chmap = kzalloc_obj(*chmap);
  817. if (!chmap)
  818. return ERR_PTR(-ENOMEM);
  819. if (num_channels == 1) {
  820. chmap->map[0] = SNDRV_CHMAP_MONO;
  821. } else {
  822. chmap->map[0] = SNDRV_CHMAP_FL;
  823. chmap->map[1] = SNDRV_CHMAP_FR;
  824. }
  825. chmap->channels = num_channels;
  826. clock = UAC3_BADD_CS_ID9;
  827. goto found_clock;
  828. }
  829. as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  830. NULL, UAC_AS_GENERAL);
  831. if (!as) {
  832. dev_err(&dev->dev,
  833. "%u:%d : UAC_AS_GENERAL descriptor not found\n",
  834. iface_no, altno);
  835. return NULL;
  836. }
  837. if (as->bLength < sizeof(*as)) {
  838. dev_err(&dev->dev,
  839. "%u:%d : invalid UAC_AS_GENERAL desc\n",
  840. iface_no, altno);
  841. return NULL;
  842. }
  843. cluster_id = le16_to_cpu(as->wClusterDescrID);
  844. if (!cluster_id) {
  845. dev_err(&dev->dev,
  846. "%u:%d : no cluster descriptor\n",
  847. iface_no, altno);
  848. return NULL;
  849. }
  850. /*
  851. * Get number of channels and channel map through
  852. * High Capability Cluster Descriptor
  853. *
  854. * First step: get High Capability header and
  855. * read size of Cluster Descriptor
  856. */
  857. err = snd_usb_ctl_msg(chip->dev,
  858. usb_rcvctrlpipe(chip->dev, 0),
  859. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  860. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  861. cluster_id,
  862. snd_usb_ctrl_intf(ctrl_intf),
  863. &hc_header, sizeof(hc_header));
  864. if (err < 0)
  865. return ERR_PTR(err);
  866. else if (err != sizeof(hc_header)) {
  867. dev_err(&dev->dev,
  868. "%u:%d : can't get High Capability descriptor\n",
  869. iface_no, altno);
  870. return ERR_PTR(-EIO);
  871. }
  872. /*
  873. * Second step: allocate needed amount of memory
  874. * and request Cluster Descriptor
  875. */
  876. wLength = le16_to_cpu(hc_header.wLength);
  877. if (wLength < sizeof(cluster))
  878. return NULL;
  879. cluster = kzalloc(wLength, GFP_KERNEL);
  880. if (!cluster)
  881. return ERR_PTR(-ENOMEM);
  882. err = snd_usb_ctl_msg(chip->dev,
  883. usb_rcvctrlpipe(chip->dev, 0),
  884. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  885. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  886. cluster_id,
  887. snd_usb_ctrl_intf(ctrl_intf),
  888. cluster, wLength);
  889. if (err < 0) {
  890. kfree(cluster);
  891. return ERR_PTR(err);
  892. } else if (err != wLength) {
  893. dev_err(&dev->dev,
  894. "%u:%d : can't get Cluster Descriptor\n",
  895. iface_no, altno);
  896. kfree(cluster);
  897. return ERR_PTR(-EIO);
  898. }
  899. cluster_wLength = le16_to_cpu(cluster->wLength);
  900. if (cluster_wLength < sizeof(*cluster) ||
  901. cluster_wLength > wLength) {
  902. dev_err(&dev->dev,
  903. "%u:%d : invalid Cluster Descriptor size\n",
  904. iface_no, altno);
  905. kfree(cluster);
  906. return ERR_PTR(-EIO);
  907. }
  908. num_channels = cluster->bNrChannels;
  909. chmap = convert_chmap_v3(cluster);
  910. kfree(cluster);
  911. /*
  912. * lookup the terminal associated to this interface
  913. * to extract the clock
  914. */
  915. input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
  916. as->bTerminalLink,
  917. UAC_VERSION_3);
  918. if (input_term) {
  919. clock = input_term->bCSourceID;
  920. goto found_clock;
  921. }
  922. output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
  923. as->bTerminalLink,
  924. UAC_VERSION_3);
  925. if (output_term) {
  926. clock = output_term->bCSourceID;
  927. goto found_clock;
  928. }
  929. dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
  930. iface_no, altno, as->bTerminalLink);
  931. kfree(chmap);
  932. return NULL;
  933. found_clock:
  934. fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
  935. altset_idx, altno, num_channels, clock);
  936. if (!fp) {
  937. kfree(chmap);
  938. return ERR_PTR(-ENOMEM);
  939. }
  940. fp->chmap = chmap;
  941. if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  942. fp->attributes = 0; /* No attributes */
  943. fp->fmt_type = UAC_FORMAT_TYPE_I;
  944. fp->formats = badd_formats;
  945. fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */
  946. fp->rate_min = UAC3_BADD_SAMPLING_RATE;
  947. fp->rate_max = UAC3_BADD_SAMPLING_RATE;
  948. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  949. pd = kzalloc_obj(*pd);
  950. if (!pd) {
  951. audioformat_free(fp);
  952. return NULL;
  953. }
  954. pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  955. UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
  956. pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
  957. pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
  958. pd->ctrl_iface = ctrl_intf;
  959. } else {
  960. fp->attributes = parse_uac_endpoint_attributes(chip, alts,
  961. UAC_VERSION_3,
  962. iface_no);
  963. pd = snd_usb_find_power_domain(ctrl_intf,
  964. as->bTerminalLink);
  965. /* ok, let's parse further... */
  966. if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
  967. kfree(pd);
  968. audioformat_free(fp);
  969. return NULL;
  970. }
  971. }
  972. if (pd)
  973. *pd_out = pd;
  974. return fp;
  975. }
  976. static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
  977. int iface_no,
  978. bool *has_non_pcm, bool non_pcm)
  979. {
  980. struct usb_device *dev;
  981. struct usb_interface *iface;
  982. struct usb_host_interface *alts;
  983. struct usb_interface_descriptor *altsd;
  984. int i, altno, err, stream;
  985. struct audioformat *fp = NULL;
  986. struct snd_usb_power_domain *pd = NULL;
  987. bool set_iface_first;
  988. int num, protocol;
  989. dev = chip->dev;
  990. /* parse the interface's altsettings */
  991. iface = usb_ifnum_to_if(dev, iface_no);
  992. num = iface->num_altsetting;
  993. /*
  994. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  995. * one misses syncpipe, and does not produce any sound.
  996. */
  997. if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
  998. num = 4;
  999. for (i = 0; i < num; i++) {
  1000. alts = &iface->altsetting[i];
  1001. altsd = get_iface_desc(alts);
  1002. protocol = altsd->bInterfaceProtocol;
  1003. /* skip invalid one */
  1004. if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
  1005. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  1006. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
  1007. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  1008. altsd->bNumEndpoints < 1 ||
  1009. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  1010. continue;
  1011. /* must be isochronous */
  1012. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1013. USB_ENDPOINT_XFER_ISOC)
  1014. continue;
  1015. /* check direction */
  1016. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  1017. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  1018. altno = altsd->bAlternateSetting;
  1019. if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  1020. continue;
  1021. /*
  1022. * Roland audio streaming interfaces are marked with protocols
  1023. * 0/1/2, but are UAC 1 compatible.
  1024. */
  1025. if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
  1026. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  1027. protocol <= 2)
  1028. protocol = UAC_VERSION_1;
  1029. switch (protocol) {
  1030. default:
  1031. dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
  1032. iface_no, altno, protocol);
  1033. protocol = UAC_VERSION_1;
  1034. fallthrough;
  1035. case UAC_VERSION_1:
  1036. case UAC_VERSION_2: {
  1037. int bm_quirk = 0;
  1038. /*
  1039. * Blue Microphones workaround: The last altsetting is
  1040. * identical with the previous one, except for a larger
  1041. * packet size, but is actually a mislabeled two-channel
  1042. * setting; ignore it.
  1043. *
  1044. * Part 1: prepare quirk flag
  1045. */
  1046. if (altno == 2 && num == 3 &&
  1047. fp && fp->altsetting == 1 && fp->channels == 1 &&
  1048. fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  1049. protocol == UAC_VERSION_1 &&
  1050. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  1051. fp->maxpacksize * 2)
  1052. bm_quirk = 1;
  1053. fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
  1054. iface_no, i, altno,
  1055. stream, bm_quirk);
  1056. break;
  1057. }
  1058. case UAC_VERSION_3:
  1059. fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
  1060. iface_no, i, altno, stream);
  1061. break;
  1062. }
  1063. if (!fp)
  1064. continue;
  1065. else if (IS_ERR(fp))
  1066. return PTR_ERR(fp);
  1067. if (fp->fmt_type != UAC_FORMAT_TYPE_I)
  1068. *has_non_pcm = true;
  1069. if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
  1070. audioformat_free(fp);
  1071. kfree(pd);
  1072. fp = NULL;
  1073. pd = NULL;
  1074. continue;
  1075. }
  1076. snd_usb_audioformat_set_sync_ep(chip, fp);
  1077. dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
  1078. if (protocol == UAC_VERSION_3)
  1079. err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
  1080. else
  1081. err = snd_usb_add_audio_stream(chip, stream, fp);
  1082. if (err < 0) {
  1083. audioformat_free(fp);
  1084. kfree(pd);
  1085. return err;
  1086. }
  1087. /* add endpoints */
  1088. err = snd_usb_add_endpoint(chip, fp->endpoint,
  1089. SND_USB_ENDPOINT_TYPE_DATA);
  1090. if (err < 0)
  1091. return err;
  1092. if (fp->sync_ep) {
  1093. err = snd_usb_add_endpoint(chip, fp->sync_ep,
  1094. fp->implicit_fb ?
  1095. SND_USB_ENDPOINT_TYPE_DATA :
  1096. SND_USB_ENDPOINT_TYPE_SYNC);
  1097. if (err < 0)
  1098. return err;
  1099. }
  1100. set_iface_first = false;
  1101. if (protocol == UAC_VERSION_1 ||
  1102. (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
  1103. set_iface_first = true;
  1104. /* try to set the interface... */
  1105. if (chip->quirk_flags & QUIRK_FLAG_SKIP_IFACE_SETUP)
  1106. continue;
  1107. usb_set_interface(chip->dev, iface_no, 0);
  1108. if (set_iface_first)
  1109. usb_set_interface(chip->dev, iface_no, altno);
  1110. snd_usb_init_pitch(chip, fp);
  1111. snd_usb_init_sample_rate(chip, fp, fp->rate_max);
  1112. if (!set_iface_first)
  1113. usb_set_interface(chip->dev, iface_no, altno);
  1114. }
  1115. return 0;
  1116. }
  1117. int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  1118. {
  1119. int err;
  1120. bool has_non_pcm = false;
  1121. /* parse PCM formats */
  1122. err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
  1123. if (err < 0)
  1124. return err;
  1125. if (has_non_pcm) {
  1126. /* parse non-PCM formats */
  1127. err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
  1128. if (err < 0)
  1129. return err;
  1130. }
  1131. return 0;
  1132. }