stream.c 30 KB

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