f_uac2.c 38 KB

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