stream.c 31 KB

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