stream.c 32 KB

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