f_uac2.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /*
  2. * f_uac2.c -- USB Audio Class 2.0 Function
  3. *
  4. * Copyright (C) 2011
  5. * Yadwinder Singh (yadi.brar01@gmail.com)
  6. * Jaswinder Singh (jaswinder.singh@linaro.org)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/usb/audio.h>
  14. #include <linux/usb/audio-v2.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/module.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include "u_uac2.h"
  21. /* Keep everyone on toes */
  22. #define USB_XFERS 2
  23. /*
  24. * The driver implements a simple UAC_2 topology.
  25. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
  26. * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
  27. * Capture and Playback sampling rates are independently
  28. * controlled by two clock sources :
  29. * CLK_5 := c_srate, and CLK_6 := p_srate
  30. */
  31. #define USB_OUT_IT_ID 1
  32. #define IO_IN_IT_ID 2
  33. #define IO_OUT_OT_ID 3
  34. #define USB_IN_OT_ID 4
  35. #define USB_OUT_CLK_ID 5
  36. #define USB_IN_CLK_ID 6
  37. #define CONTROL_ABSENT 0
  38. #define CONTROL_RDONLY 1
  39. #define CONTROL_RDWR 3
  40. #define CLK_FREQ_CTRL 0
  41. #define CLK_VLD_CTRL 2
  42. #define COPY_CTRL 0
  43. #define CONN_CTRL 2
  44. #define OVRLD_CTRL 4
  45. #define CLSTR_CTRL 6
  46. #define UNFLW_CTRL 8
  47. #define OVFLW_CTRL 10
  48. static const char *uac2_name = "snd_uac2";
  49. struct uac2_req {
  50. struct uac2_rtd_params *pp; /* parent param */
  51. struct usb_request *req;
  52. };
  53. struct uac2_rtd_params {
  54. struct snd_uac2_chip *uac2; /* parent chip */
  55. bool ep_enabled; /* if the ep is enabled */
  56. /* Size of the ring buffer */
  57. size_t dma_bytes;
  58. unsigned char *dma_area;
  59. struct snd_pcm_substream *ss;
  60. /* Ring buffer */
  61. ssize_t hw_ptr;
  62. void *rbuf;
  63. size_t period_size;
  64. unsigned max_psize;
  65. struct uac2_req ureq[USB_XFERS];
  66. spinlock_t lock;
  67. };
  68. struct snd_uac2_chip {
  69. struct platform_device pdev;
  70. struct platform_driver pdrv;
  71. struct uac2_rtd_params p_prm;
  72. struct uac2_rtd_params c_prm;
  73. struct snd_card *card;
  74. struct snd_pcm *pcm;
  75. /* timekeeping for the playback endpoint */
  76. unsigned int p_interval;
  77. unsigned int p_residue;
  78. /* pre-calculated values for playback iso completion */
  79. unsigned int p_pktsize;
  80. unsigned int p_pktsize_residue;
  81. unsigned int p_framesize;
  82. };
  83. #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
  84. #define PRD_SIZE_MAX PAGE_SIZE
  85. #define MIN_PERIODS 4
  86. static struct snd_pcm_hardware uac2_pcm_hardware = {
  87. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
  88. | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
  89. | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  90. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  91. .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
  92. .buffer_bytes_max = BUFF_SIZE_MAX,
  93. .period_bytes_max = PRD_SIZE_MAX,
  94. .periods_min = MIN_PERIODS,
  95. };
  96. struct audio_dev {
  97. u8 ac_intf, ac_alt;
  98. u8 as_out_intf, as_out_alt;
  99. u8 as_in_intf, as_in_alt;
  100. struct usb_ep *in_ep, *out_ep;
  101. struct usb_function func;
  102. /* The ALSA Sound Card it represents on the USB-Client side */
  103. struct snd_uac2_chip uac2;
  104. };
  105. static inline
  106. struct audio_dev *func_to_agdev(struct usb_function *f)
  107. {
  108. return container_of(f, struct audio_dev, func);
  109. }
  110. static inline
  111. struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
  112. {
  113. return container_of(u, struct audio_dev, uac2);
  114. }
  115. static inline
  116. struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
  117. {
  118. return container_of(p, struct snd_uac2_chip, pdev);
  119. }
  120. static inline
  121. struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev)
  122. {
  123. return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
  124. }
  125. static inline
  126. uint num_channels(uint chanmask)
  127. {
  128. uint num = 0;
  129. while (chanmask) {
  130. num += (chanmask & 1);
  131. chanmask >>= 1;
  132. }
  133. return num;
  134. }
  135. static void
  136. agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
  137. {
  138. unsigned pending;
  139. unsigned long flags;
  140. unsigned int hw_ptr;
  141. bool update_alsa = false;
  142. int status = req->status;
  143. struct uac2_req *ur = req->context;
  144. struct snd_pcm_substream *substream;
  145. struct uac2_rtd_params *prm = ur->pp;
  146. struct snd_uac2_chip *uac2 = prm->uac2;
  147. /* i/f shutting down */
  148. if (!prm->ep_enabled || req->status == -ESHUTDOWN)
  149. return;
  150. /*
  151. * We can't really do much about bad xfers.
  152. * Afterall, the ISOCH xfers could fail legitimately.
  153. */
  154. if (status)
  155. pr_debug("%s: iso_complete status(%d) %d/%d\n",
  156. __func__, status, req->actual, req->length);
  157. substream = prm->ss;
  158. /* Do nothing if ALSA isn't active */
  159. if (!substream)
  160. goto exit;
  161. spin_lock_irqsave(&prm->lock, flags);
  162. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  163. /*
  164. * For each IN packet, take the quotient of the current data
  165. * rate and the endpoint's interval as the base packet size.
  166. * If there is a residue from this division, add it to the
  167. * residue accumulator.
  168. */
  169. req->length = uac2->p_pktsize;
  170. uac2->p_residue += uac2->p_pktsize_residue;
  171. /*
  172. * Whenever there are more bytes in the accumulator than we
  173. * need to add one more sample frame, increase this packet's
  174. * size and decrease the accumulator.
  175. */
  176. if (uac2->p_residue / uac2->p_interval >= uac2->p_framesize) {
  177. req->length += uac2->p_framesize;
  178. uac2->p_residue -= uac2->p_framesize *
  179. uac2->p_interval;
  180. }
  181. req->actual = req->length;
  182. }
  183. pending = prm->hw_ptr % prm->period_size;
  184. pending += req->actual;
  185. if (pending >= prm->period_size)
  186. update_alsa = true;
  187. hw_ptr = prm->hw_ptr;
  188. prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
  189. spin_unlock_irqrestore(&prm->lock, flags);
  190. /* Pack USB load in ALSA ring buffer */
  191. pending = prm->dma_bytes - hw_ptr;
  192. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  193. if (unlikely(pending < req->actual)) {
  194. memcpy(req->buf, prm->dma_area + hw_ptr, pending);
  195. memcpy(req->buf + pending, prm->dma_area,
  196. req->actual - pending);
  197. } else {
  198. memcpy(req->buf, prm->dma_area + hw_ptr, req->actual);
  199. }
  200. } else {
  201. if (unlikely(pending < req->actual)) {
  202. memcpy(prm->dma_area + hw_ptr, req->buf, pending);
  203. memcpy(prm->dma_area, req->buf + pending,
  204. req->actual - pending);
  205. } else {
  206. memcpy(prm->dma_area + hw_ptr, req->buf, req->actual);
  207. }
  208. }
  209. exit:
  210. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  211. dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
  212. if (update_alsa)
  213. snd_pcm_period_elapsed(substream);
  214. return;
  215. }
  216. static int
  217. uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  218. {
  219. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  220. struct uac2_rtd_params *prm;
  221. unsigned long flags;
  222. int err = 0;
  223. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  224. prm = &uac2->p_prm;
  225. else
  226. prm = &uac2->c_prm;
  227. spin_lock_irqsave(&prm->lock, flags);
  228. /* Reset */
  229. prm->hw_ptr = 0;
  230. switch (cmd) {
  231. case SNDRV_PCM_TRIGGER_START:
  232. case SNDRV_PCM_TRIGGER_RESUME:
  233. prm->ss = substream;
  234. break;
  235. case SNDRV_PCM_TRIGGER_STOP:
  236. case SNDRV_PCM_TRIGGER_SUSPEND:
  237. prm->ss = NULL;
  238. break;
  239. default:
  240. err = -EINVAL;
  241. }
  242. spin_unlock_irqrestore(&prm->lock, flags);
  243. /* Clear buffer after Play stops */
  244. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
  245. memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
  246. return err;
  247. }
  248. static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
  249. {
  250. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  251. struct uac2_rtd_params *prm;
  252. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  253. prm = &uac2->p_prm;
  254. else
  255. prm = &uac2->c_prm;
  256. return bytes_to_frames(substream->runtime, prm->hw_ptr);
  257. }
  258. static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
  259. struct snd_pcm_hw_params *hw_params)
  260. {
  261. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  262. struct uac2_rtd_params *prm;
  263. int err;
  264. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  265. prm = &uac2->p_prm;
  266. else
  267. prm = &uac2->c_prm;
  268. err = snd_pcm_lib_malloc_pages(substream,
  269. params_buffer_bytes(hw_params));
  270. if (err >= 0) {
  271. prm->dma_bytes = substream->runtime->dma_bytes;
  272. prm->dma_area = substream->runtime->dma_area;
  273. prm->period_size = params_period_bytes(hw_params);
  274. }
  275. return err;
  276. }
  277. static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
  278. {
  279. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  280. struct uac2_rtd_params *prm;
  281. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  282. prm = &uac2->p_prm;
  283. else
  284. prm = &uac2->c_prm;
  285. prm->dma_area = NULL;
  286. prm->dma_bytes = 0;
  287. prm->period_size = 0;
  288. return snd_pcm_lib_free_pages(substream);
  289. }
  290. static int uac2_pcm_open(struct snd_pcm_substream *substream)
  291. {
  292. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  293. struct snd_pcm_runtime *runtime = substream->runtime;
  294. struct audio_dev *audio_dev;
  295. struct f_uac2_opts *opts;
  296. int p_ssize, c_ssize;
  297. int p_srate, c_srate;
  298. int p_chmask, c_chmask;
  299. audio_dev = uac2_to_agdev(uac2);
  300. opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
  301. p_ssize = opts->p_ssize;
  302. c_ssize = opts->c_ssize;
  303. p_srate = opts->p_srate;
  304. c_srate = opts->c_srate;
  305. p_chmask = opts->p_chmask;
  306. c_chmask = opts->c_chmask;
  307. uac2->p_residue = 0;
  308. runtime->hw = uac2_pcm_hardware;
  309. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  310. spin_lock_init(&uac2->p_prm.lock);
  311. runtime->hw.rate_min = p_srate;
  312. switch (p_ssize) {
  313. case 3:
  314. runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
  315. break;
  316. case 4:
  317. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
  318. break;
  319. default:
  320. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
  321. break;
  322. }
  323. runtime->hw.channels_min = num_channels(p_chmask);
  324. runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
  325. / runtime->hw.periods_min;
  326. } else {
  327. spin_lock_init(&uac2->c_prm.lock);
  328. runtime->hw.rate_min = c_srate;
  329. switch (c_ssize) {
  330. case 3:
  331. runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
  332. break;
  333. case 4:
  334. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
  335. break;
  336. default:
  337. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
  338. break;
  339. }
  340. runtime->hw.channels_min = num_channels(c_chmask);
  341. runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
  342. / runtime->hw.periods_min;
  343. }
  344. runtime->hw.rate_max = runtime->hw.rate_min;
  345. runtime->hw.channels_max = runtime->hw.channels_min;
  346. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  347. return 0;
  348. }
  349. /* ALSA cries without these function pointers */
  350. static int uac2_pcm_null(struct snd_pcm_substream *substream)
  351. {
  352. return 0;
  353. }
  354. static struct snd_pcm_ops uac2_pcm_ops = {
  355. .open = uac2_pcm_open,
  356. .close = uac2_pcm_null,
  357. .ioctl = snd_pcm_lib_ioctl,
  358. .hw_params = uac2_pcm_hw_params,
  359. .hw_free = uac2_pcm_hw_free,
  360. .trigger = uac2_pcm_trigger,
  361. .pointer = uac2_pcm_pointer,
  362. .prepare = uac2_pcm_null,
  363. };
  364. static int snd_uac2_probe(struct platform_device *pdev)
  365. {
  366. struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
  367. struct snd_card *card;
  368. struct snd_pcm *pcm;
  369. struct audio_dev *audio_dev;
  370. struct f_uac2_opts *opts;
  371. int err;
  372. int p_chmask, c_chmask;
  373. audio_dev = uac2_to_agdev(uac2);
  374. opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
  375. p_chmask = opts->p_chmask;
  376. c_chmask = opts->c_chmask;
  377. /* Choose any slot, with no id */
  378. err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
  379. if (err < 0)
  380. return err;
  381. uac2->card = card;
  382. /*
  383. * Create first PCM device
  384. * Create a substream only for non-zero channel streams
  385. */
  386. err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
  387. p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
  388. if (err < 0)
  389. goto snd_fail;
  390. strcpy(pcm->name, "UAC2 PCM");
  391. pcm->private_data = uac2;
  392. uac2->pcm = pcm;
  393. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
  394. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
  395. strcpy(card->driver, "UAC2_Gadget");
  396. strcpy(card->shortname, "UAC2_Gadget");
  397. sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
  398. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  399. snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
  400. err = snd_card_register(card);
  401. if (!err) {
  402. platform_set_drvdata(pdev, card);
  403. return 0;
  404. }
  405. snd_fail:
  406. snd_card_free(card);
  407. uac2->pcm = NULL;
  408. uac2->card = NULL;
  409. return err;
  410. }
  411. static int snd_uac2_remove(struct platform_device *pdev)
  412. {
  413. struct snd_card *card = platform_get_drvdata(pdev);
  414. if (card)
  415. return snd_card_free(card);
  416. return 0;
  417. }
  418. static void snd_uac2_release(struct device *dev)
  419. {
  420. dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
  421. }
  422. static int alsa_uac2_init(struct audio_dev *agdev)
  423. {
  424. struct snd_uac2_chip *uac2 = &agdev->uac2;
  425. int err;
  426. uac2->pdrv.probe = snd_uac2_probe;
  427. uac2->pdrv.remove = snd_uac2_remove;
  428. uac2->pdrv.driver.name = uac2_name;
  429. uac2->pdev.id = 0;
  430. uac2->pdev.name = uac2_name;
  431. uac2->pdev.dev.release = snd_uac2_release;
  432. /* Register snd_uac2 driver */
  433. err = platform_driver_register(&uac2->pdrv);
  434. if (err)
  435. return err;
  436. /* Register snd_uac2 device */
  437. err = platform_device_register(&uac2->pdev);
  438. if (err)
  439. platform_driver_unregister(&uac2->pdrv);
  440. return err;
  441. }
  442. static void alsa_uac2_exit(struct audio_dev *agdev)
  443. {
  444. struct snd_uac2_chip *uac2 = &agdev->uac2;
  445. platform_driver_unregister(&uac2->pdrv);
  446. platform_device_unregister(&uac2->pdev);
  447. }
  448. /* --------- USB Function Interface ------------- */
  449. enum {
  450. STR_ASSOC,
  451. STR_IF_CTRL,
  452. STR_CLKSRC_IN,
  453. STR_CLKSRC_OUT,
  454. STR_USB_IT,
  455. STR_IO_IT,
  456. STR_USB_OT,
  457. STR_IO_OT,
  458. STR_AS_OUT_ALT0,
  459. STR_AS_OUT_ALT1,
  460. STR_AS_IN_ALT0,
  461. STR_AS_IN_ALT1,
  462. };
  463. static char clksrc_in[8];
  464. static char clksrc_out[8];
  465. static struct usb_string strings_fn[] = {
  466. [STR_ASSOC].s = "Source/Sink",
  467. [STR_IF_CTRL].s = "Topology Control",
  468. [STR_CLKSRC_IN].s = clksrc_in,
  469. [STR_CLKSRC_OUT].s = clksrc_out,
  470. [STR_USB_IT].s = "USBH Out",
  471. [STR_IO_IT].s = "USBD Out",
  472. [STR_USB_OT].s = "USBH In",
  473. [STR_IO_OT].s = "USBD In",
  474. [STR_AS_OUT_ALT0].s = "Playback Inactive",
  475. [STR_AS_OUT_ALT1].s = "Playback Active",
  476. [STR_AS_IN_ALT0].s = "Capture Inactive",
  477. [STR_AS_IN_ALT1].s = "Capture Active",
  478. { },
  479. };
  480. static struct usb_gadget_strings str_fn = {
  481. .language = 0x0409, /* en-us */
  482. .strings = strings_fn,
  483. };
  484. static struct usb_gadget_strings *fn_strings[] = {
  485. &str_fn,
  486. NULL,
  487. };
  488. static struct usb_qualifier_descriptor devqual_desc = {
  489. .bLength = sizeof devqual_desc,
  490. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  491. .bcdUSB = cpu_to_le16(0x200),
  492. .bDeviceClass = USB_CLASS_MISC,
  493. .bDeviceSubClass = 0x02,
  494. .bDeviceProtocol = 0x01,
  495. .bNumConfigurations = 1,
  496. .bRESERVED = 0,
  497. };
  498. static struct usb_interface_assoc_descriptor iad_desc = {
  499. .bLength = sizeof iad_desc,
  500. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  501. .bFirstInterface = 0,
  502. .bInterfaceCount = 3,
  503. .bFunctionClass = USB_CLASS_AUDIO,
  504. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  505. .bFunctionProtocol = UAC_VERSION_2,
  506. };
  507. /* Audio Control Interface */
  508. static struct usb_interface_descriptor std_ac_if_desc = {
  509. .bLength = sizeof std_ac_if_desc,
  510. .bDescriptorType = USB_DT_INTERFACE,
  511. .bAlternateSetting = 0,
  512. .bNumEndpoints = 0,
  513. .bInterfaceClass = USB_CLASS_AUDIO,
  514. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  515. .bInterfaceProtocol = UAC_VERSION_2,
  516. };
  517. /* Clock source for IN traffic */
  518. static struct uac_clock_source_descriptor in_clk_src_desc = {
  519. .bLength = sizeof in_clk_src_desc,
  520. .bDescriptorType = USB_DT_CS_INTERFACE,
  521. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  522. .bClockID = USB_IN_CLK_ID,
  523. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  524. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  525. .bAssocTerminal = 0,
  526. };
  527. /* Clock source for OUT traffic */
  528. static struct uac_clock_source_descriptor out_clk_src_desc = {
  529. .bLength = sizeof out_clk_src_desc,
  530. .bDescriptorType = USB_DT_CS_INTERFACE,
  531. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  532. .bClockID = USB_OUT_CLK_ID,
  533. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  534. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  535. .bAssocTerminal = 0,
  536. };
  537. /* Input Terminal for USB_OUT */
  538. static struct uac2_input_terminal_descriptor usb_out_it_desc = {
  539. .bLength = sizeof usb_out_it_desc,
  540. .bDescriptorType = USB_DT_CS_INTERFACE,
  541. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  542. .bTerminalID = USB_OUT_IT_ID,
  543. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  544. .bAssocTerminal = 0,
  545. .bCSourceID = USB_OUT_CLK_ID,
  546. .iChannelNames = 0,
  547. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  548. };
  549. /* Input Terminal for I/O-In */
  550. static struct uac2_input_terminal_descriptor io_in_it_desc = {
  551. .bLength = sizeof io_in_it_desc,
  552. .bDescriptorType = USB_DT_CS_INTERFACE,
  553. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  554. .bTerminalID = IO_IN_IT_ID,
  555. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
  556. .bAssocTerminal = 0,
  557. .bCSourceID = USB_IN_CLK_ID,
  558. .iChannelNames = 0,
  559. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  560. };
  561. /* Ouput Terminal for USB_IN */
  562. static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  563. .bLength = sizeof usb_in_ot_desc,
  564. .bDescriptorType = USB_DT_CS_INTERFACE,
  565. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  566. .bTerminalID = USB_IN_OT_ID,
  567. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  568. .bAssocTerminal = 0,
  569. .bSourceID = IO_IN_IT_ID,
  570. .bCSourceID = USB_IN_CLK_ID,
  571. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  572. };
  573. /* Ouput Terminal for I/O-Out */
  574. static struct uac2_output_terminal_descriptor io_out_ot_desc = {
  575. .bLength = sizeof io_out_ot_desc,
  576. .bDescriptorType = USB_DT_CS_INTERFACE,
  577. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  578. .bTerminalID = IO_OUT_OT_ID,
  579. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
  580. .bAssocTerminal = 0,
  581. .bSourceID = USB_OUT_IT_ID,
  582. .bCSourceID = USB_OUT_CLK_ID,
  583. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  584. };
  585. static struct uac2_ac_header_descriptor ac_hdr_desc = {
  586. .bLength = sizeof ac_hdr_desc,
  587. .bDescriptorType = USB_DT_CS_INTERFACE,
  588. .bDescriptorSubtype = UAC_MS_HEADER,
  589. .bcdADC = cpu_to_le16(0x200),
  590. .bCategory = UAC2_FUNCTION_IO_BOX,
  591. .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
  592. + sizeof usb_out_it_desc + sizeof io_in_it_desc
  593. + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
  594. .bmControls = 0,
  595. };
  596. /* Audio Streaming OUT Interface - Alt0 */
  597. static struct usb_interface_descriptor std_as_out_if0_desc = {
  598. .bLength = sizeof std_as_out_if0_desc,
  599. .bDescriptorType = USB_DT_INTERFACE,
  600. .bAlternateSetting = 0,
  601. .bNumEndpoints = 0,
  602. .bInterfaceClass = USB_CLASS_AUDIO,
  603. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  604. .bInterfaceProtocol = UAC_VERSION_2,
  605. };
  606. /* Audio Streaming OUT Interface - Alt1 */
  607. static struct usb_interface_descriptor std_as_out_if1_desc = {
  608. .bLength = sizeof std_as_out_if1_desc,
  609. .bDescriptorType = USB_DT_INTERFACE,
  610. .bAlternateSetting = 1,
  611. .bNumEndpoints = 1,
  612. .bInterfaceClass = USB_CLASS_AUDIO,
  613. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  614. .bInterfaceProtocol = UAC_VERSION_2,
  615. };
  616. /* Audio Stream OUT Intface Desc */
  617. static struct uac2_as_header_descriptor as_out_hdr_desc = {
  618. .bLength = sizeof as_out_hdr_desc,
  619. .bDescriptorType = USB_DT_CS_INTERFACE,
  620. .bDescriptorSubtype = UAC_AS_GENERAL,
  621. .bTerminalLink = USB_OUT_IT_ID,
  622. .bmControls = 0,
  623. .bFormatType = UAC_FORMAT_TYPE_I,
  624. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  625. .iChannelNames = 0,
  626. };
  627. /* Audio USB_OUT Format */
  628. static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  629. .bLength = sizeof as_out_fmt1_desc,
  630. .bDescriptorType = USB_DT_CS_INTERFACE,
  631. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  632. .bFormatType = UAC_FORMAT_TYPE_I,
  633. };
  634. /* STD AS ISO OUT Endpoint */
  635. static struct usb_endpoint_descriptor fs_epout_desc = {
  636. .bLength = USB_DT_ENDPOINT_SIZE,
  637. .bDescriptorType = USB_DT_ENDPOINT,
  638. .bEndpointAddress = USB_DIR_OUT,
  639. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  640. .wMaxPacketSize = cpu_to_le16(1023),
  641. .bInterval = 1,
  642. };
  643. static struct usb_endpoint_descriptor hs_epout_desc = {
  644. .bLength = USB_DT_ENDPOINT_SIZE,
  645. .bDescriptorType = USB_DT_ENDPOINT,
  646. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  647. .wMaxPacketSize = cpu_to_le16(1024),
  648. .bInterval = 4,
  649. };
  650. /* CS AS ISO OUT Endpoint */
  651. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  652. .bLength = sizeof as_iso_out_desc,
  653. .bDescriptorType = USB_DT_CS_ENDPOINT,
  654. .bDescriptorSubtype = UAC_EP_GENERAL,
  655. .bmAttributes = 0,
  656. .bmControls = 0,
  657. .bLockDelayUnits = 0,
  658. .wLockDelay = 0,
  659. };
  660. /* Audio Streaming IN Interface - Alt0 */
  661. static struct usb_interface_descriptor std_as_in_if0_desc = {
  662. .bLength = sizeof std_as_in_if0_desc,
  663. .bDescriptorType = USB_DT_INTERFACE,
  664. .bAlternateSetting = 0,
  665. .bNumEndpoints = 0,
  666. .bInterfaceClass = USB_CLASS_AUDIO,
  667. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  668. .bInterfaceProtocol = UAC_VERSION_2,
  669. };
  670. /* Audio Streaming IN Interface - Alt1 */
  671. static struct usb_interface_descriptor std_as_in_if1_desc = {
  672. .bLength = sizeof std_as_in_if1_desc,
  673. .bDescriptorType = USB_DT_INTERFACE,
  674. .bAlternateSetting = 1,
  675. .bNumEndpoints = 1,
  676. .bInterfaceClass = USB_CLASS_AUDIO,
  677. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  678. .bInterfaceProtocol = UAC_VERSION_2,
  679. };
  680. /* Audio Stream IN Intface Desc */
  681. static struct uac2_as_header_descriptor as_in_hdr_desc = {
  682. .bLength = sizeof as_in_hdr_desc,
  683. .bDescriptorType = USB_DT_CS_INTERFACE,
  684. .bDescriptorSubtype = UAC_AS_GENERAL,
  685. .bTerminalLink = USB_IN_OT_ID,
  686. .bmControls = 0,
  687. .bFormatType = UAC_FORMAT_TYPE_I,
  688. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  689. .iChannelNames = 0,
  690. };
  691. /* Audio USB_IN Format */
  692. static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  693. .bLength = sizeof as_in_fmt1_desc,
  694. .bDescriptorType = USB_DT_CS_INTERFACE,
  695. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  696. .bFormatType = UAC_FORMAT_TYPE_I,
  697. };
  698. /* STD AS ISO IN Endpoint */
  699. static struct usb_endpoint_descriptor fs_epin_desc = {
  700. .bLength = USB_DT_ENDPOINT_SIZE,
  701. .bDescriptorType = USB_DT_ENDPOINT,
  702. .bEndpointAddress = USB_DIR_IN,
  703. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  704. .wMaxPacketSize = cpu_to_le16(1023),
  705. .bInterval = 1,
  706. };
  707. static struct usb_endpoint_descriptor hs_epin_desc = {
  708. .bLength = USB_DT_ENDPOINT_SIZE,
  709. .bDescriptorType = USB_DT_ENDPOINT,
  710. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  711. .wMaxPacketSize = cpu_to_le16(1024),
  712. .bInterval = 4,
  713. };
  714. /* CS AS ISO IN Endpoint */
  715. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  716. .bLength = sizeof as_iso_in_desc,
  717. .bDescriptorType = USB_DT_CS_ENDPOINT,
  718. .bDescriptorSubtype = UAC_EP_GENERAL,
  719. .bmAttributes = 0,
  720. .bmControls = 0,
  721. .bLockDelayUnits = 0,
  722. .wLockDelay = 0,
  723. };
  724. static struct usb_descriptor_header *fs_audio_desc[] = {
  725. (struct usb_descriptor_header *)&iad_desc,
  726. (struct usb_descriptor_header *)&std_ac_if_desc,
  727. (struct usb_descriptor_header *)&ac_hdr_desc,
  728. (struct usb_descriptor_header *)&in_clk_src_desc,
  729. (struct usb_descriptor_header *)&out_clk_src_desc,
  730. (struct usb_descriptor_header *)&usb_out_it_desc,
  731. (struct usb_descriptor_header *)&io_in_it_desc,
  732. (struct usb_descriptor_header *)&usb_in_ot_desc,
  733. (struct usb_descriptor_header *)&io_out_ot_desc,
  734. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  735. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  736. (struct usb_descriptor_header *)&as_out_hdr_desc,
  737. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  738. (struct usb_descriptor_header *)&fs_epout_desc,
  739. (struct usb_descriptor_header *)&as_iso_out_desc,
  740. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  741. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  742. (struct usb_descriptor_header *)&as_in_hdr_desc,
  743. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  744. (struct usb_descriptor_header *)&fs_epin_desc,
  745. (struct usb_descriptor_header *)&as_iso_in_desc,
  746. NULL,
  747. };
  748. static struct usb_descriptor_header *hs_audio_desc[] = {
  749. (struct usb_descriptor_header *)&iad_desc,
  750. (struct usb_descriptor_header *)&std_ac_if_desc,
  751. (struct usb_descriptor_header *)&ac_hdr_desc,
  752. (struct usb_descriptor_header *)&in_clk_src_desc,
  753. (struct usb_descriptor_header *)&out_clk_src_desc,
  754. (struct usb_descriptor_header *)&usb_out_it_desc,
  755. (struct usb_descriptor_header *)&io_in_it_desc,
  756. (struct usb_descriptor_header *)&usb_in_ot_desc,
  757. (struct usb_descriptor_header *)&io_out_ot_desc,
  758. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  759. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  760. (struct usb_descriptor_header *)&as_out_hdr_desc,
  761. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  762. (struct usb_descriptor_header *)&hs_epout_desc,
  763. (struct usb_descriptor_header *)&as_iso_out_desc,
  764. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  765. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  766. (struct usb_descriptor_header *)&as_in_hdr_desc,
  767. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  768. (struct usb_descriptor_header *)&hs_epin_desc,
  769. (struct usb_descriptor_header *)&as_iso_in_desc,
  770. NULL,
  771. };
  772. struct cntrl_cur_lay3 {
  773. __u32 dCUR;
  774. };
  775. struct cntrl_range_lay3 {
  776. __u16 wNumSubRanges;
  777. __u32 dMIN;
  778. __u32 dMAX;
  779. __u32 dRES;
  780. } __packed;
  781. static inline void
  782. free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
  783. {
  784. struct snd_uac2_chip *uac2 = prm->uac2;
  785. int i;
  786. if (!prm->ep_enabled)
  787. return;
  788. prm->ep_enabled = false;
  789. for (i = 0; i < USB_XFERS; i++) {
  790. if (prm->ureq[i].req) {
  791. usb_ep_dequeue(ep, prm->ureq[i].req);
  792. usb_ep_free_request(ep, prm->ureq[i].req);
  793. prm->ureq[i].req = NULL;
  794. }
  795. }
  796. if (usb_ep_disable(ep))
  797. dev_err(&uac2->pdev.dev,
  798. "%s:%d Error!\n", __func__, __LINE__);
  799. }
  800. static int
  801. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  802. {
  803. struct audio_dev *agdev = func_to_agdev(fn);
  804. struct snd_uac2_chip *uac2 = &agdev->uac2;
  805. struct usb_composite_dev *cdev = cfg->cdev;
  806. struct usb_gadget *gadget = cdev->gadget;
  807. struct device *dev = &uac2->pdev.dev;
  808. struct uac2_rtd_params *prm;
  809. struct f_uac2_opts *uac2_opts;
  810. struct usb_string *us;
  811. int ret;
  812. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  813. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  814. if (IS_ERR(us))
  815. return PTR_ERR(us);
  816. iad_desc.iFunction = us[STR_ASSOC].id;
  817. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  818. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  819. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  820. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  821. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  822. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  823. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  824. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  825. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  826. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  827. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  828. /* Initialize the configurable parameters */
  829. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  830. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  831. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  832. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  833. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  834. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  835. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  836. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  837. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  838. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  839. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  840. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  841. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  842. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  843. ret = usb_interface_id(cfg, fn);
  844. if (ret < 0) {
  845. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  846. return ret;
  847. }
  848. std_ac_if_desc.bInterfaceNumber = ret;
  849. agdev->ac_intf = ret;
  850. agdev->ac_alt = 0;
  851. ret = usb_interface_id(cfg, fn);
  852. if (ret < 0) {
  853. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  854. return ret;
  855. }
  856. std_as_out_if0_desc.bInterfaceNumber = ret;
  857. std_as_out_if1_desc.bInterfaceNumber = ret;
  858. agdev->as_out_intf = ret;
  859. agdev->as_out_alt = 0;
  860. ret = usb_interface_id(cfg, fn);
  861. if (ret < 0) {
  862. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  863. return ret;
  864. }
  865. std_as_in_if0_desc.bInterfaceNumber = ret;
  866. std_as_in_if1_desc.bInterfaceNumber = ret;
  867. agdev->as_in_intf = ret;
  868. agdev->as_in_alt = 0;
  869. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  870. if (!agdev->out_ep) {
  871. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  872. goto err;
  873. }
  874. agdev->out_ep->driver_data = agdev;
  875. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  876. if (!agdev->in_ep) {
  877. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  878. goto err;
  879. }
  880. agdev->in_ep->driver_data = agdev;
  881. uac2->p_prm.uac2 = uac2;
  882. uac2->c_prm.uac2 = uac2;
  883. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  884. hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
  885. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  886. hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
  887. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
  888. if (ret)
  889. goto err;
  890. prm = &agdev->uac2.c_prm;
  891. prm->max_psize = hs_epout_desc.wMaxPacketSize;
  892. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  893. if (!prm->rbuf) {
  894. prm->max_psize = 0;
  895. goto err_free_descs;
  896. }
  897. prm = &agdev->uac2.p_prm;
  898. prm->max_psize = hs_epin_desc.wMaxPacketSize;
  899. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  900. if (!prm->rbuf) {
  901. prm->max_psize = 0;
  902. goto err_free_descs;
  903. }
  904. ret = alsa_uac2_init(agdev);
  905. if (ret)
  906. goto err_free_descs;
  907. return 0;
  908. err_free_descs:
  909. usb_free_all_descriptors(fn);
  910. err:
  911. kfree(agdev->uac2.p_prm.rbuf);
  912. kfree(agdev->uac2.c_prm.rbuf);
  913. if (agdev->in_ep)
  914. agdev->in_ep->driver_data = NULL;
  915. if (agdev->out_ep)
  916. agdev->out_ep->driver_data = NULL;
  917. return -EINVAL;
  918. }
  919. static int
  920. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  921. {
  922. struct usb_composite_dev *cdev = fn->config->cdev;
  923. struct audio_dev *agdev = func_to_agdev(fn);
  924. struct snd_uac2_chip *uac2 = &agdev->uac2;
  925. struct usb_gadget *gadget = cdev->gadget;
  926. struct device *dev = &uac2->pdev.dev;
  927. struct usb_request *req;
  928. struct usb_ep *ep;
  929. struct uac2_rtd_params *prm;
  930. int req_len, i;
  931. /* No i/f has more than 2 alt settings */
  932. if (alt > 1) {
  933. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  934. return -EINVAL;
  935. }
  936. if (intf == agdev->ac_intf) {
  937. /* Control I/f has only 1 AltSetting - 0 */
  938. if (alt) {
  939. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  940. return -EINVAL;
  941. }
  942. return 0;
  943. }
  944. if (intf == agdev->as_out_intf) {
  945. ep = agdev->out_ep;
  946. prm = &uac2->c_prm;
  947. config_ep_by_speed(gadget, fn, ep);
  948. agdev->as_out_alt = alt;
  949. req_len = prm->max_psize;
  950. } else if (intf == agdev->as_in_intf) {
  951. struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev);
  952. unsigned int factor, rate;
  953. struct usb_endpoint_descriptor *ep_desc;
  954. ep = agdev->in_ep;
  955. prm = &uac2->p_prm;
  956. config_ep_by_speed(gadget, fn, ep);
  957. agdev->as_in_alt = alt;
  958. /* pre-calculate the playback endpoint's interval */
  959. if (gadget->speed == USB_SPEED_FULL) {
  960. ep_desc = &fs_epin_desc;
  961. factor = 1000;
  962. } else {
  963. ep_desc = &hs_epin_desc;
  964. factor = 125;
  965. }
  966. /* pre-compute some values for iso_complete() */
  967. uac2->p_framesize = opts->p_ssize *
  968. num_channels(opts->p_chmask);
  969. rate = opts->p_srate * uac2->p_framesize;
  970. uac2->p_interval = (1 << (ep_desc->bInterval - 1)) * factor;
  971. uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval,
  972. prm->max_psize);
  973. if (uac2->p_pktsize < prm->max_psize)
  974. uac2->p_pktsize_residue = rate % uac2->p_interval;
  975. else
  976. uac2->p_pktsize_residue = 0;
  977. req_len = uac2->p_pktsize;
  978. uac2->p_residue = 0;
  979. } else {
  980. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  981. return -EINVAL;
  982. }
  983. if (alt == 0) {
  984. free_ep(prm, ep);
  985. return 0;
  986. }
  987. prm->ep_enabled = true;
  988. usb_ep_enable(ep);
  989. for (i = 0; i < USB_XFERS; i++) {
  990. if (!prm->ureq[i].req) {
  991. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  992. if (req == NULL)
  993. return -ENOMEM;
  994. prm->ureq[i].req = req;
  995. prm->ureq[i].pp = prm;
  996. req->zero = 0;
  997. req->context = &prm->ureq[i];
  998. req->length = req_len;
  999. req->complete = agdev_iso_complete;
  1000. req->buf = prm->rbuf + i * prm->max_psize;
  1001. }
  1002. if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
  1003. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1004. }
  1005. return 0;
  1006. }
  1007. static int
  1008. afunc_get_alt(struct usb_function *fn, unsigned intf)
  1009. {
  1010. struct audio_dev *agdev = func_to_agdev(fn);
  1011. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1012. if (intf == agdev->ac_intf)
  1013. return agdev->ac_alt;
  1014. else if (intf == agdev->as_out_intf)
  1015. return agdev->as_out_alt;
  1016. else if (intf == agdev->as_in_intf)
  1017. return agdev->as_in_alt;
  1018. else
  1019. dev_err(&uac2->pdev.dev,
  1020. "%s:%d Invalid Interface %d!\n",
  1021. __func__, __LINE__, intf);
  1022. return -EINVAL;
  1023. }
  1024. static void
  1025. afunc_disable(struct usb_function *fn)
  1026. {
  1027. struct audio_dev *agdev = func_to_agdev(fn);
  1028. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1029. free_ep(&uac2->p_prm, agdev->in_ep);
  1030. agdev->as_in_alt = 0;
  1031. free_ep(&uac2->c_prm, agdev->out_ep);
  1032. agdev->as_out_alt = 0;
  1033. }
  1034. static int
  1035. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1036. {
  1037. struct usb_request *req = fn->config->cdev->req;
  1038. struct audio_dev *agdev = func_to_agdev(fn);
  1039. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1040. struct f_uac2_opts *opts;
  1041. u16 w_length = le16_to_cpu(cr->wLength);
  1042. u16 w_index = le16_to_cpu(cr->wIndex);
  1043. u16 w_value = le16_to_cpu(cr->wValue);
  1044. u8 entity_id = (w_index >> 8) & 0xff;
  1045. u8 control_selector = w_value >> 8;
  1046. int value = -EOPNOTSUPP;
  1047. int p_srate, c_srate;
  1048. opts = agdev_to_uac2_opts(agdev);
  1049. p_srate = opts->p_srate;
  1050. c_srate = opts->c_srate;
  1051. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1052. struct cntrl_cur_lay3 c;
  1053. if (entity_id == USB_IN_CLK_ID)
  1054. c.dCUR = p_srate;
  1055. else if (entity_id == USB_OUT_CLK_ID)
  1056. c.dCUR = c_srate;
  1057. value = min_t(unsigned, w_length, sizeof c);
  1058. memcpy(req->buf, &c, value);
  1059. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  1060. *(u8 *)req->buf = 1;
  1061. value = min_t(unsigned, w_length, 1);
  1062. } else {
  1063. dev_err(&uac2->pdev.dev,
  1064. "%s:%d control_selector=%d TODO!\n",
  1065. __func__, __LINE__, control_selector);
  1066. }
  1067. return value;
  1068. }
  1069. static int
  1070. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1071. {
  1072. struct usb_request *req = fn->config->cdev->req;
  1073. struct audio_dev *agdev = func_to_agdev(fn);
  1074. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1075. struct f_uac2_opts *opts;
  1076. u16 w_length = le16_to_cpu(cr->wLength);
  1077. u16 w_index = le16_to_cpu(cr->wIndex);
  1078. u16 w_value = le16_to_cpu(cr->wValue);
  1079. u8 entity_id = (w_index >> 8) & 0xff;
  1080. u8 control_selector = w_value >> 8;
  1081. struct cntrl_range_lay3 r;
  1082. int value = -EOPNOTSUPP;
  1083. int p_srate, c_srate;
  1084. opts = agdev_to_uac2_opts(agdev);
  1085. p_srate = opts->p_srate;
  1086. c_srate = opts->c_srate;
  1087. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1088. if (entity_id == USB_IN_CLK_ID)
  1089. r.dMIN = p_srate;
  1090. else if (entity_id == USB_OUT_CLK_ID)
  1091. r.dMIN = c_srate;
  1092. else
  1093. return -EOPNOTSUPP;
  1094. r.dMAX = r.dMIN;
  1095. r.dRES = 0;
  1096. r.wNumSubRanges = 1;
  1097. value = min_t(unsigned, w_length, sizeof r);
  1098. memcpy(req->buf, &r, value);
  1099. } else {
  1100. dev_err(&uac2->pdev.dev,
  1101. "%s:%d control_selector=%d TODO!\n",
  1102. __func__, __LINE__, control_selector);
  1103. }
  1104. return value;
  1105. }
  1106. static int
  1107. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1108. {
  1109. if (cr->bRequest == UAC2_CS_CUR)
  1110. return in_rq_cur(fn, cr);
  1111. else if (cr->bRequest == UAC2_CS_RANGE)
  1112. return in_rq_range(fn, cr);
  1113. else
  1114. return -EOPNOTSUPP;
  1115. }
  1116. static int
  1117. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1118. {
  1119. u16 w_length = le16_to_cpu(cr->wLength);
  1120. u16 w_value = le16_to_cpu(cr->wValue);
  1121. u8 control_selector = w_value >> 8;
  1122. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  1123. return w_length;
  1124. return -EOPNOTSUPP;
  1125. }
  1126. static int
  1127. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1128. {
  1129. struct audio_dev *agdev = func_to_agdev(fn);
  1130. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1131. u16 w_index = le16_to_cpu(cr->wIndex);
  1132. u8 intf = w_index & 0xff;
  1133. if (intf != agdev->ac_intf) {
  1134. dev_err(&uac2->pdev.dev,
  1135. "%s:%d Error!\n", __func__, __LINE__);
  1136. return -EOPNOTSUPP;
  1137. }
  1138. if (cr->bRequestType & USB_DIR_IN)
  1139. return ac_rq_in(fn, cr);
  1140. else if (cr->bRequest == UAC2_CS_CUR)
  1141. return out_rq_cur(fn, cr);
  1142. return -EOPNOTSUPP;
  1143. }
  1144. static int
  1145. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1146. {
  1147. struct usb_composite_dev *cdev = fn->config->cdev;
  1148. struct audio_dev *agdev = func_to_agdev(fn);
  1149. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1150. struct usb_request *req = cdev->req;
  1151. u16 w_length = le16_to_cpu(cr->wLength);
  1152. int value = -EOPNOTSUPP;
  1153. /* Only Class specific requests are supposed to reach here */
  1154. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  1155. return -EOPNOTSUPP;
  1156. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  1157. value = setup_rq_inf(fn, cr);
  1158. else
  1159. dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
  1160. if (value >= 0) {
  1161. req->length = value;
  1162. req->zero = value < w_length;
  1163. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1164. if (value < 0) {
  1165. dev_err(&uac2->pdev.dev,
  1166. "%s:%d Error!\n", __func__, __LINE__);
  1167. req->status = 0;
  1168. }
  1169. }
  1170. return value;
  1171. }
  1172. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  1173. {
  1174. return container_of(to_config_group(item), struct f_uac2_opts,
  1175. func_inst.group);
  1176. }
  1177. CONFIGFS_ATTR_STRUCT(f_uac2_opts);
  1178. CONFIGFS_ATTR_OPS(f_uac2_opts);
  1179. static void f_uac2_attr_release(struct config_item *item)
  1180. {
  1181. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  1182. usb_put_function_instance(&opts->func_inst);
  1183. }
  1184. static struct configfs_item_operations f_uac2_item_ops = {
  1185. .release = f_uac2_attr_release,
  1186. .show_attribute = f_uac2_opts_attr_show,
  1187. .store_attribute = f_uac2_opts_attr_store,
  1188. };
  1189. #define UAC2_ATTRIBUTE(name) \
  1190. static ssize_t f_uac2_opts_##name##_show(struct f_uac2_opts *opts, \
  1191. char *page) \
  1192. { \
  1193. int result; \
  1194. \
  1195. mutex_lock(&opts->lock); \
  1196. result = sprintf(page, "%u\n", opts->name); \
  1197. mutex_unlock(&opts->lock); \
  1198. \
  1199. return result; \
  1200. } \
  1201. \
  1202. static ssize_t f_uac2_opts_##name##_store(struct f_uac2_opts *opts, \
  1203. const char *page, size_t len) \
  1204. { \
  1205. int ret; \
  1206. u32 num; \
  1207. \
  1208. mutex_lock(&opts->lock); \
  1209. if (opts->refcnt) { \
  1210. ret = -EBUSY; \
  1211. goto end; \
  1212. } \
  1213. \
  1214. ret = kstrtou32(page, 0, &num); \
  1215. if (ret) \
  1216. goto end; \
  1217. \
  1218. opts->name = num; \
  1219. ret = len; \
  1220. \
  1221. end: \
  1222. mutex_unlock(&opts->lock); \
  1223. return ret; \
  1224. } \
  1225. \
  1226. static struct f_uac2_opts_attribute f_uac2_opts_##name = \
  1227. __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
  1228. f_uac2_opts_##name##_show, \
  1229. f_uac2_opts_##name##_store)
  1230. UAC2_ATTRIBUTE(p_chmask);
  1231. UAC2_ATTRIBUTE(p_srate);
  1232. UAC2_ATTRIBUTE(p_ssize);
  1233. UAC2_ATTRIBUTE(c_chmask);
  1234. UAC2_ATTRIBUTE(c_srate);
  1235. UAC2_ATTRIBUTE(c_ssize);
  1236. static struct configfs_attribute *f_uac2_attrs[] = {
  1237. &f_uac2_opts_p_chmask.attr,
  1238. &f_uac2_opts_p_srate.attr,
  1239. &f_uac2_opts_p_ssize.attr,
  1240. &f_uac2_opts_c_chmask.attr,
  1241. &f_uac2_opts_c_srate.attr,
  1242. &f_uac2_opts_c_ssize.attr,
  1243. NULL,
  1244. };
  1245. static struct config_item_type f_uac2_func_type = {
  1246. .ct_item_ops = &f_uac2_item_ops,
  1247. .ct_attrs = f_uac2_attrs,
  1248. .ct_owner = THIS_MODULE,
  1249. };
  1250. static void afunc_free_inst(struct usb_function_instance *f)
  1251. {
  1252. struct f_uac2_opts *opts;
  1253. opts = container_of(f, struct f_uac2_opts, func_inst);
  1254. kfree(opts);
  1255. }
  1256. static struct usb_function_instance *afunc_alloc_inst(void)
  1257. {
  1258. struct f_uac2_opts *opts;
  1259. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1260. if (!opts)
  1261. return ERR_PTR(-ENOMEM);
  1262. mutex_init(&opts->lock);
  1263. opts->func_inst.free_func_inst = afunc_free_inst;
  1264. config_group_init_type_name(&opts->func_inst.group, "",
  1265. &f_uac2_func_type);
  1266. opts->p_chmask = UAC2_DEF_PCHMASK;
  1267. opts->p_srate = UAC2_DEF_PSRATE;
  1268. opts->p_ssize = UAC2_DEF_PSSIZE;
  1269. opts->c_chmask = UAC2_DEF_CCHMASK;
  1270. opts->c_srate = UAC2_DEF_CSRATE;
  1271. opts->c_ssize = UAC2_DEF_CSSIZE;
  1272. return &opts->func_inst;
  1273. }
  1274. static void afunc_free(struct usb_function *f)
  1275. {
  1276. struct audio_dev *agdev;
  1277. struct f_uac2_opts *opts;
  1278. agdev = func_to_agdev(f);
  1279. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  1280. kfree(agdev);
  1281. mutex_lock(&opts->lock);
  1282. --opts->refcnt;
  1283. mutex_unlock(&opts->lock);
  1284. }
  1285. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  1286. {
  1287. struct audio_dev *agdev = func_to_agdev(f);
  1288. struct uac2_rtd_params *prm;
  1289. alsa_uac2_exit(agdev);
  1290. prm = &agdev->uac2.p_prm;
  1291. kfree(prm->rbuf);
  1292. prm = &agdev->uac2.c_prm;
  1293. kfree(prm->rbuf);
  1294. usb_free_all_descriptors(f);
  1295. if (agdev->in_ep)
  1296. agdev->in_ep->driver_data = NULL;
  1297. if (agdev->out_ep)
  1298. agdev->out_ep->driver_data = NULL;
  1299. }
  1300. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  1301. {
  1302. struct audio_dev *agdev;
  1303. struct f_uac2_opts *opts;
  1304. agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
  1305. if (agdev == NULL)
  1306. return ERR_PTR(-ENOMEM);
  1307. opts = container_of(fi, struct f_uac2_opts, func_inst);
  1308. mutex_lock(&opts->lock);
  1309. ++opts->refcnt;
  1310. mutex_unlock(&opts->lock);
  1311. agdev->func.name = "uac2_func";
  1312. agdev->func.bind = afunc_bind;
  1313. agdev->func.unbind = afunc_unbind;
  1314. agdev->func.set_alt = afunc_set_alt;
  1315. agdev->func.get_alt = afunc_get_alt;
  1316. agdev->func.disable = afunc_disable;
  1317. agdev->func.setup = afunc_setup;
  1318. agdev->func.free_func = afunc_free;
  1319. return &agdev->func;
  1320. }
  1321. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  1322. MODULE_LICENSE("GPL");
  1323. MODULE_AUTHOR("Yadwinder Singh");
  1324. MODULE_AUTHOR("Jaswinder Singh");