f_uac2.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. /* Playback(USB-IN) Default Stereo - Fl/Fr */
  21. static int p_chmask = 0x3;
  22. module_param(p_chmask, uint, S_IRUGO);
  23. MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
  24. /* Playback Default 48 KHz */
  25. static int p_srate = 48000;
  26. module_param(p_srate, uint, S_IRUGO);
  27. MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
  28. /* Playback Default 16bits/sample */
  29. static int p_ssize = 2;
  30. module_param(p_ssize, uint, S_IRUGO);
  31. MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
  32. /* Capture(USB-OUT) Default Stereo - Fl/Fr */
  33. static int c_chmask = 0x3;
  34. module_param(c_chmask, uint, S_IRUGO);
  35. MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
  36. /* Capture Default 64 KHz */
  37. static int c_srate = 64000;
  38. module_param(c_srate, uint, S_IRUGO);
  39. MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
  40. /* Capture Default 16bits/sample */
  41. static int c_ssize = 2;
  42. module_param(c_ssize, uint, S_IRUGO);
  43. MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
  44. #define ALT_SET(x, a) do {(x) &= ~0xff; (x) |= (a); } while (0)
  45. #define ALT_GET(x) ((x) & 0xff)
  46. #define INTF_SET(x, i) do {(x) &= 0xff; (x) |= ((i) << 8); } while (0)
  47. #define INTF_GET(x) ((x >> 8) & 0xff)
  48. /* Keep everyone on toes */
  49. #define USB_XFERS 2
  50. /*
  51. * The driver implements a simple UAC_2 topology.
  52. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
  53. * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
  54. * Capture and Playback sampling rates are independently
  55. * controlled by two clock sources :
  56. * CLK_5 := c_srate, and CLK_6 := p_srate
  57. */
  58. #define USB_OUT_IT_ID 1
  59. #define IO_IN_IT_ID 2
  60. #define IO_OUT_OT_ID 3
  61. #define USB_IN_OT_ID 4
  62. #define USB_OUT_CLK_ID 5
  63. #define USB_IN_CLK_ID 6
  64. #define CONTROL_ABSENT 0
  65. #define CONTROL_RDONLY 1
  66. #define CONTROL_RDWR 3
  67. #define CLK_FREQ_CTRL 0
  68. #define CLK_VLD_CTRL 2
  69. #define COPY_CTRL 0
  70. #define CONN_CTRL 2
  71. #define OVRLD_CTRL 4
  72. #define CLSTR_CTRL 6
  73. #define UNFLW_CTRL 8
  74. #define OVFLW_CTRL 10
  75. const char *uac2_name = "snd_uac2";
  76. struct uac2_req {
  77. struct uac2_rtd_params *pp; /* parent param */
  78. struct usb_request *req;
  79. };
  80. struct uac2_rtd_params {
  81. bool ep_enabled; /* if the ep is enabled */
  82. /* Size of the ring buffer */
  83. size_t dma_bytes;
  84. unsigned char *dma_area;
  85. struct snd_pcm_substream *ss;
  86. /* Ring buffer */
  87. ssize_t hw_ptr;
  88. void *rbuf;
  89. size_t period_size;
  90. unsigned max_psize;
  91. struct uac2_req ureq[USB_XFERS];
  92. spinlock_t lock;
  93. };
  94. struct snd_uac2_chip {
  95. struct platform_device pdev;
  96. struct platform_driver pdrv;
  97. struct uac2_rtd_params p_prm;
  98. struct uac2_rtd_params c_prm;
  99. struct snd_card *card;
  100. struct snd_pcm *pcm;
  101. };
  102. #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
  103. #define PRD_SIZE_MAX PAGE_SIZE
  104. #define MIN_PERIODS 4
  105. static struct snd_pcm_hardware uac2_pcm_hardware = {
  106. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
  107. | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
  108. | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  109. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  110. .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
  111. .buffer_bytes_max = BUFF_SIZE_MAX,
  112. .period_bytes_max = PRD_SIZE_MAX,
  113. .periods_min = MIN_PERIODS,
  114. };
  115. struct audio_dev {
  116. /* Currently active {Interface[15:8] | AltSettings[7:0]} */
  117. __u16 ac_alt, as_out_alt, as_in_alt;
  118. struct usb_ep *in_ep, *out_ep;
  119. struct usb_function func;
  120. /* The ALSA Sound Card it represents on the USB-Client side */
  121. struct snd_uac2_chip uac2;
  122. };
  123. static struct audio_dev *agdev_g;
  124. static inline
  125. struct audio_dev *func_to_agdev(struct usb_function *f)
  126. {
  127. return container_of(f, struct audio_dev, func);
  128. }
  129. static inline
  130. struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
  131. {
  132. return container_of(u, struct audio_dev, uac2);
  133. }
  134. static inline
  135. struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
  136. {
  137. return container_of(p, struct snd_uac2_chip, pdev);
  138. }
  139. static inline
  140. struct snd_uac2_chip *prm_to_uac2(struct uac2_rtd_params *r)
  141. {
  142. struct snd_uac2_chip *uac2 = container_of(r,
  143. struct snd_uac2_chip, c_prm);
  144. if (&uac2->c_prm != r)
  145. uac2 = container_of(r, struct snd_uac2_chip, p_prm);
  146. return uac2;
  147. }
  148. static inline
  149. uint num_channels(uint chanmask)
  150. {
  151. uint num = 0;
  152. while (chanmask) {
  153. num += (chanmask & 1);
  154. chanmask >>= 1;
  155. }
  156. return num;
  157. }
  158. static void
  159. agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
  160. {
  161. unsigned pending;
  162. unsigned long flags;
  163. bool update_alsa = false;
  164. unsigned char *src, *dst;
  165. int status = req->status;
  166. struct uac2_req *ur = req->context;
  167. struct snd_pcm_substream *substream;
  168. struct uac2_rtd_params *prm = ur->pp;
  169. struct snd_uac2_chip *uac2 = prm_to_uac2(prm);
  170. /* i/f shutting down */
  171. if (!prm->ep_enabled)
  172. return;
  173. /*
  174. * We can't really do much about bad xfers.
  175. * Afterall, the ISOCH xfers could fail legitimately.
  176. */
  177. if (status)
  178. pr_debug("%s: iso_complete status(%d) %d/%d\n",
  179. __func__, status, req->actual, req->length);
  180. substream = prm->ss;
  181. /* Do nothing if ALSA isn't active */
  182. if (!substream)
  183. goto exit;
  184. spin_lock_irqsave(&prm->lock, flags);
  185. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  186. src = prm->dma_area + prm->hw_ptr;
  187. req->actual = req->length;
  188. dst = req->buf;
  189. } else {
  190. dst = prm->dma_area + prm->hw_ptr;
  191. src = req->buf;
  192. }
  193. pending = prm->hw_ptr % prm->period_size;
  194. pending += req->actual;
  195. if (pending >= prm->period_size)
  196. update_alsa = true;
  197. prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
  198. spin_unlock_irqrestore(&prm->lock, flags);
  199. /* Pack USB load in ALSA ring buffer */
  200. memcpy(dst, src, req->actual);
  201. exit:
  202. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  203. dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
  204. if (update_alsa)
  205. snd_pcm_period_elapsed(substream);
  206. return;
  207. }
  208. static int
  209. uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  210. {
  211. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  212. struct audio_dev *agdev = uac2_to_agdev(uac2);
  213. struct uac2_rtd_params *prm;
  214. unsigned long flags;
  215. struct usb_ep *ep;
  216. int err = 0;
  217. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  218. ep = agdev->in_ep;
  219. prm = &uac2->p_prm;
  220. } else {
  221. ep = agdev->out_ep;
  222. prm = &uac2->c_prm;
  223. }
  224. spin_lock_irqsave(&prm->lock, flags);
  225. /* Reset */
  226. prm->hw_ptr = 0;
  227. switch (cmd) {
  228. case SNDRV_PCM_TRIGGER_START:
  229. case SNDRV_PCM_TRIGGER_RESUME:
  230. prm->ss = substream;
  231. break;
  232. case SNDRV_PCM_TRIGGER_STOP:
  233. case SNDRV_PCM_TRIGGER_SUSPEND:
  234. prm->ss = NULL;
  235. break;
  236. default:
  237. err = -EINVAL;
  238. }
  239. spin_unlock_irqrestore(&prm->lock, flags);
  240. /* Clear buffer after Play stops */
  241. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
  242. memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
  243. return err;
  244. }
  245. static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
  246. {
  247. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  248. struct uac2_rtd_params *prm;
  249. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  250. prm = &uac2->p_prm;
  251. else
  252. prm = &uac2->c_prm;
  253. return bytes_to_frames(substream->runtime, prm->hw_ptr);
  254. }
  255. static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
  256. struct snd_pcm_hw_params *hw_params)
  257. {
  258. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  259. struct uac2_rtd_params *prm;
  260. int err;
  261. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  262. prm = &uac2->p_prm;
  263. else
  264. prm = &uac2->c_prm;
  265. err = snd_pcm_lib_malloc_pages(substream,
  266. params_buffer_bytes(hw_params));
  267. if (err >= 0) {
  268. prm->dma_bytes = substream->runtime->dma_bytes;
  269. prm->dma_area = substream->runtime->dma_area;
  270. prm->period_size = params_period_bytes(hw_params);
  271. }
  272. return err;
  273. }
  274. static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
  275. {
  276. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  277. struct uac2_rtd_params *prm;
  278. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  279. prm = &uac2->p_prm;
  280. else
  281. prm = &uac2->c_prm;
  282. prm->dma_area = NULL;
  283. prm->dma_bytes = 0;
  284. prm->period_size = 0;
  285. return snd_pcm_lib_free_pages(substream);
  286. }
  287. static int uac2_pcm_open(struct snd_pcm_substream *substream)
  288. {
  289. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  290. struct snd_pcm_runtime *runtime = substream->runtime;
  291. runtime->hw = uac2_pcm_hardware;
  292. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  293. spin_lock_init(&uac2->p_prm.lock);
  294. runtime->hw.rate_min = p_srate;
  295. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! p_ssize ! */
  296. runtime->hw.channels_min = num_channels(p_chmask);
  297. runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
  298. / runtime->hw.periods_min;
  299. } else {
  300. spin_lock_init(&uac2->c_prm.lock);
  301. runtime->hw.rate_min = c_srate;
  302. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! c_ssize ! */
  303. runtime->hw.channels_min = num_channels(c_chmask);
  304. runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
  305. / runtime->hw.periods_min;
  306. }
  307. runtime->hw.rate_max = runtime->hw.rate_min;
  308. runtime->hw.channels_max = runtime->hw.channels_min;
  309. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  310. return 0;
  311. }
  312. /* ALSA cries without these function pointers */
  313. static int uac2_pcm_null(struct snd_pcm_substream *substream)
  314. {
  315. return 0;
  316. }
  317. static struct snd_pcm_ops uac2_pcm_ops = {
  318. .open = uac2_pcm_open,
  319. .close = uac2_pcm_null,
  320. .ioctl = snd_pcm_lib_ioctl,
  321. .hw_params = uac2_pcm_hw_params,
  322. .hw_free = uac2_pcm_hw_free,
  323. .trigger = uac2_pcm_trigger,
  324. .pointer = uac2_pcm_pointer,
  325. .prepare = uac2_pcm_null,
  326. };
  327. static int __devinit snd_uac2_probe(struct platform_device *pdev)
  328. {
  329. struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
  330. struct snd_card *card;
  331. struct snd_pcm *pcm;
  332. int err;
  333. /* Choose any slot, with no id */
  334. err = snd_card_create(-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_card_set_dev(card, &pdev->dev);
  355. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  356. snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
  357. err = snd_card_register(card);
  358. if (!err) {
  359. platform_set_drvdata(pdev, card);
  360. return 0;
  361. }
  362. snd_fail:
  363. snd_card_free(card);
  364. uac2->pcm = NULL;
  365. uac2->card = NULL;
  366. return err;
  367. }
  368. static int snd_uac2_remove(struct platform_device *pdev)
  369. {
  370. struct snd_card *card = platform_get_drvdata(pdev);
  371. platform_set_drvdata(pdev, NULL);
  372. if (card)
  373. return snd_card_free(card);
  374. return 0;
  375. }
  376. static int alsa_uac2_init(struct audio_dev *agdev)
  377. {
  378. struct snd_uac2_chip *uac2 = &agdev->uac2;
  379. int err;
  380. uac2->pdrv.probe = snd_uac2_probe;
  381. uac2->pdrv.remove = snd_uac2_remove;
  382. uac2->pdrv.driver.name = uac2_name;
  383. uac2->pdev.id = 0;
  384. uac2->pdev.name = uac2_name;
  385. /* Register snd_uac2 driver */
  386. err = platform_driver_register(&uac2->pdrv);
  387. if (err)
  388. return err;
  389. /* Register snd_uac2 device */
  390. err = platform_device_register(&uac2->pdev);
  391. if (err)
  392. platform_driver_unregister(&uac2->pdrv);
  393. return err;
  394. }
  395. static void alsa_uac2_exit(struct audio_dev *agdev)
  396. {
  397. struct snd_uac2_chip *uac2 = &agdev->uac2;
  398. platform_driver_unregister(&uac2->pdrv);
  399. platform_device_unregister(&uac2->pdev);
  400. }
  401. /* --------- USB Function Interface ------------- */
  402. enum {
  403. STR_ASSOC,
  404. STR_IF_CTRL,
  405. STR_CLKSRC_IN,
  406. STR_CLKSRC_OUT,
  407. STR_USB_IT,
  408. STR_IO_IT,
  409. STR_USB_OT,
  410. STR_IO_OT,
  411. STR_AS_OUT_ALT0,
  412. STR_AS_OUT_ALT1,
  413. STR_AS_IN_ALT0,
  414. STR_AS_IN_ALT1,
  415. };
  416. static const char ifassoc[] = "Source/Sink";
  417. static const char ifctrl[] = "Topology Control";
  418. static char clksrc_in[8];
  419. static char clksrc_out[8];
  420. static const char usb_it[] = "USBH Out";
  421. static const char io_it[] = "USBD Out";
  422. static const char usb_ot[] = "USBH In";
  423. static const char io_ot[] = "USBD In";
  424. static const char out_alt0[] = "Playback Inactive";
  425. static const char out_alt1[] = "Playback Active";
  426. static const char in_alt0[] = "Capture Inactive";
  427. static const char in_alt1[] = "Capture Active";
  428. static struct usb_string strings_fn[] = {
  429. [STR_ASSOC].s = ifassoc,
  430. [STR_IF_CTRL].s = ifctrl,
  431. [STR_CLKSRC_IN].s = clksrc_in,
  432. [STR_CLKSRC_OUT].s = clksrc_out,
  433. [STR_USB_IT].s = usb_it,
  434. [STR_IO_IT].s = io_it,
  435. [STR_USB_OT].s = usb_ot,
  436. [STR_IO_OT].s = io_ot,
  437. [STR_AS_OUT_ALT0].s = out_alt0,
  438. [STR_AS_OUT_ALT1].s = out_alt1,
  439. [STR_AS_IN_ALT0].s = in_alt0,
  440. [STR_AS_IN_ALT1].s = in_alt1,
  441. { },
  442. };
  443. static struct usb_gadget_strings str_fn = {
  444. .language = 0x0409, /* en-us */
  445. .strings = strings_fn,
  446. };
  447. static struct usb_gadget_strings *fn_strings[] = {
  448. &str_fn,
  449. NULL,
  450. };
  451. static struct usb_qualifier_descriptor devqual_desc = {
  452. .bLength = sizeof devqual_desc,
  453. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  454. .bcdUSB = cpu_to_le16(0x200),
  455. .bDeviceClass = USB_CLASS_MISC,
  456. .bDeviceSubClass = 0x02,
  457. .bDeviceProtocol = 0x01,
  458. .bNumConfigurations = 1,
  459. .bRESERVED = 0,
  460. };
  461. static struct usb_interface_assoc_descriptor iad_desc = {
  462. .bLength = sizeof iad_desc,
  463. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  464. .bFirstInterface = 0,
  465. .bInterfaceCount = 3,
  466. .bFunctionClass = USB_CLASS_AUDIO,
  467. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  468. .bFunctionProtocol = UAC_VERSION_2,
  469. };
  470. /* Audio Control Interface */
  471. static struct usb_interface_descriptor std_ac_if_desc = {
  472. .bLength = sizeof std_ac_if_desc,
  473. .bDescriptorType = USB_DT_INTERFACE,
  474. .bAlternateSetting = 0,
  475. .bNumEndpoints = 0,
  476. .bInterfaceClass = USB_CLASS_AUDIO,
  477. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  478. .bInterfaceProtocol = UAC_VERSION_2,
  479. };
  480. /* Clock source for IN traffic */
  481. struct uac_clock_source_descriptor in_clk_src_desc = {
  482. .bLength = sizeof in_clk_src_desc,
  483. .bDescriptorType = USB_DT_CS_INTERFACE,
  484. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  485. .bClockID = USB_IN_CLK_ID,
  486. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  487. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  488. .bAssocTerminal = 0,
  489. };
  490. /* Clock source for OUT traffic */
  491. struct uac_clock_source_descriptor out_clk_src_desc = {
  492. .bLength = sizeof out_clk_src_desc,
  493. .bDescriptorType = USB_DT_CS_INTERFACE,
  494. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  495. .bClockID = USB_OUT_CLK_ID,
  496. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  497. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  498. .bAssocTerminal = 0,
  499. };
  500. /* Input Terminal for USB_OUT */
  501. struct uac2_input_terminal_descriptor usb_out_it_desc = {
  502. .bLength = sizeof usb_out_it_desc,
  503. .bDescriptorType = USB_DT_CS_INTERFACE,
  504. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  505. .bTerminalID = USB_OUT_IT_ID,
  506. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  507. .bAssocTerminal = 0,
  508. .bCSourceID = USB_OUT_CLK_ID,
  509. .iChannelNames = 0,
  510. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  511. };
  512. /* Input Terminal for I/O-In */
  513. struct uac2_input_terminal_descriptor io_in_it_desc = {
  514. .bLength = sizeof io_in_it_desc,
  515. .bDescriptorType = USB_DT_CS_INTERFACE,
  516. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  517. .bTerminalID = IO_IN_IT_ID,
  518. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
  519. .bAssocTerminal = 0,
  520. .bCSourceID = USB_IN_CLK_ID,
  521. .iChannelNames = 0,
  522. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  523. };
  524. /* Ouput Terminal for USB_IN */
  525. struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  526. .bLength = sizeof usb_in_ot_desc,
  527. .bDescriptorType = USB_DT_CS_INTERFACE,
  528. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  529. .bTerminalID = USB_IN_OT_ID,
  530. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  531. .bAssocTerminal = 0,
  532. .bSourceID = IO_IN_IT_ID,
  533. .bCSourceID = USB_IN_CLK_ID,
  534. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  535. };
  536. /* Ouput Terminal for I/O-Out */
  537. struct uac2_output_terminal_descriptor io_out_ot_desc = {
  538. .bLength = sizeof io_out_ot_desc,
  539. .bDescriptorType = USB_DT_CS_INTERFACE,
  540. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  541. .bTerminalID = IO_OUT_OT_ID,
  542. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
  543. .bAssocTerminal = 0,
  544. .bSourceID = USB_OUT_IT_ID,
  545. .bCSourceID = USB_OUT_CLK_ID,
  546. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  547. };
  548. struct uac2_ac_header_descriptor ac_hdr_desc = {
  549. .bLength = sizeof ac_hdr_desc,
  550. .bDescriptorType = USB_DT_CS_INTERFACE,
  551. .bDescriptorSubtype = UAC_MS_HEADER,
  552. .bcdADC = cpu_to_le16(0x200),
  553. .bCategory = UAC2_FUNCTION_IO_BOX,
  554. .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
  555. + sizeof usb_out_it_desc + sizeof io_in_it_desc
  556. + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
  557. .bmControls = 0,
  558. };
  559. /* Audio Streaming OUT Interface - Alt0 */
  560. static struct usb_interface_descriptor std_as_out_if0_desc = {
  561. .bLength = sizeof std_as_out_if0_desc,
  562. .bDescriptorType = USB_DT_INTERFACE,
  563. .bAlternateSetting = 0,
  564. .bNumEndpoints = 0,
  565. .bInterfaceClass = USB_CLASS_AUDIO,
  566. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  567. .bInterfaceProtocol = UAC_VERSION_2,
  568. };
  569. /* Audio Streaming OUT Interface - Alt1 */
  570. static struct usb_interface_descriptor std_as_out_if1_desc = {
  571. .bLength = sizeof std_as_out_if1_desc,
  572. .bDescriptorType = USB_DT_INTERFACE,
  573. .bAlternateSetting = 1,
  574. .bNumEndpoints = 1,
  575. .bInterfaceClass = USB_CLASS_AUDIO,
  576. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  577. .bInterfaceProtocol = UAC_VERSION_2,
  578. };
  579. /* Audio Stream OUT Intface Desc */
  580. struct uac2_as_header_descriptor as_out_hdr_desc = {
  581. .bLength = sizeof as_out_hdr_desc,
  582. .bDescriptorType = USB_DT_CS_INTERFACE,
  583. .bDescriptorSubtype = UAC_AS_GENERAL,
  584. .bTerminalLink = USB_OUT_IT_ID,
  585. .bmControls = 0,
  586. .bFormatType = UAC_FORMAT_TYPE_I,
  587. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  588. .iChannelNames = 0,
  589. };
  590. /* Audio USB_OUT Format */
  591. struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  592. .bLength = sizeof as_out_fmt1_desc,
  593. .bDescriptorType = USB_DT_CS_INTERFACE,
  594. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  595. .bFormatType = UAC_FORMAT_TYPE_I,
  596. };
  597. /* STD AS ISO OUT Endpoint */
  598. struct usb_endpoint_descriptor fs_epout_desc = {
  599. .bLength = USB_DT_ENDPOINT_SIZE,
  600. .bDescriptorType = USB_DT_ENDPOINT,
  601. .bEndpointAddress = USB_DIR_OUT,
  602. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  603. .bInterval = 1,
  604. };
  605. struct usb_endpoint_descriptor hs_epout_desc = {
  606. .bLength = USB_DT_ENDPOINT_SIZE,
  607. .bDescriptorType = USB_DT_ENDPOINT,
  608. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  609. .bInterval = 4,
  610. };
  611. /* CS AS ISO OUT Endpoint */
  612. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  613. .bLength = sizeof as_iso_out_desc,
  614. .bDescriptorType = USB_DT_CS_ENDPOINT,
  615. .bDescriptorSubtype = UAC_EP_GENERAL,
  616. .bmAttributes = 0,
  617. .bmControls = 0,
  618. .bLockDelayUnits = 0,
  619. .wLockDelay = 0,
  620. };
  621. /* Audio Streaming IN Interface - Alt0 */
  622. static struct usb_interface_descriptor std_as_in_if0_desc = {
  623. .bLength = sizeof std_as_in_if0_desc,
  624. .bDescriptorType = USB_DT_INTERFACE,
  625. .bAlternateSetting = 0,
  626. .bNumEndpoints = 0,
  627. .bInterfaceClass = USB_CLASS_AUDIO,
  628. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  629. .bInterfaceProtocol = UAC_VERSION_2,
  630. };
  631. /* Audio Streaming IN Interface - Alt1 */
  632. static struct usb_interface_descriptor std_as_in_if1_desc = {
  633. .bLength = sizeof std_as_in_if1_desc,
  634. .bDescriptorType = USB_DT_INTERFACE,
  635. .bAlternateSetting = 1,
  636. .bNumEndpoints = 1,
  637. .bInterfaceClass = USB_CLASS_AUDIO,
  638. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  639. .bInterfaceProtocol = UAC_VERSION_2,
  640. };
  641. /* Audio Stream IN Intface Desc */
  642. struct uac2_as_header_descriptor as_in_hdr_desc = {
  643. .bLength = sizeof as_in_hdr_desc,
  644. .bDescriptorType = USB_DT_CS_INTERFACE,
  645. .bDescriptorSubtype = UAC_AS_GENERAL,
  646. .bTerminalLink = USB_IN_OT_ID,
  647. .bmControls = 0,
  648. .bFormatType = UAC_FORMAT_TYPE_I,
  649. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  650. .iChannelNames = 0,
  651. };
  652. /* Audio USB_IN Format */
  653. struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  654. .bLength = sizeof as_in_fmt1_desc,
  655. .bDescriptorType = USB_DT_CS_INTERFACE,
  656. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  657. .bFormatType = UAC_FORMAT_TYPE_I,
  658. };
  659. /* STD AS ISO IN Endpoint */
  660. struct usb_endpoint_descriptor fs_epin_desc = {
  661. .bLength = USB_DT_ENDPOINT_SIZE,
  662. .bDescriptorType = USB_DT_ENDPOINT,
  663. .bEndpointAddress = USB_DIR_IN,
  664. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  665. .bInterval = 1,
  666. };
  667. struct usb_endpoint_descriptor hs_epin_desc = {
  668. .bLength = USB_DT_ENDPOINT_SIZE,
  669. .bDescriptorType = USB_DT_ENDPOINT,
  670. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  671. .bInterval = 4,
  672. };
  673. /* CS AS ISO IN Endpoint */
  674. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  675. .bLength = sizeof as_iso_in_desc,
  676. .bDescriptorType = USB_DT_CS_ENDPOINT,
  677. .bDescriptorSubtype = UAC_EP_GENERAL,
  678. .bmAttributes = 0,
  679. .bmControls = 0,
  680. .bLockDelayUnits = 0,
  681. .wLockDelay = 0,
  682. };
  683. static struct usb_descriptor_header *fs_audio_desc[] = {
  684. (struct usb_descriptor_header *)&iad_desc,
  685. (struct usb_descriptor_header *)&std_ac_if_desc,
  686. (struct usb_descriptor_header *)&ac_hdr_desc,
  687. (struct usb_descriptor_header *)&in_clk_src_desc,
  688. (struct usb_descriptor_header *)&out_clk_src_desc,
  689. (struct usb_descriptor_header *)&usb_out_it_desc,
  690. (struct usb_descriptor_header *)&io_in_it_desc,
  691. (struct usb_descriptor_header *)&usb_in_ot_desc,
  692. (struct usb_descriptor_header *)&io_out_ot_desc,
  693. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  694. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  695. (struct usb_descriptor_header *)&as_out_hdr_desc,
  696. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  697. (struct usb_descriptor_header *)&fs_epout_desc,
  698. (struct usb_descriptor_header *)&as_iso_out_desc,
  699. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  700. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  701. (struct usb_descriptor_header *)&as_in_hdr_desc,
  702. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  703. (struct usb_descriptor_header *)&fs_epin_desc,
  704. (struct usb_descriptor_header *)&as_iso_in_desc,
  705. NULL,
  706. };
  707. static struct usb_descriptor_header *hs_audio_desc[] = {
  708. (struct usb_descriptor_header *)&iad_desc,
  709. (struct usb_descriptor_header *)&std_ac_if_desc,
  710. (struct usb_descriptor_header *)&ac_hdr_desc,
  711. (struct usb_descriptor_header *)&in_clk_src_desc,
  712. (struct usb_descriptor_header *)&out_clk_src_desc,
  713. (struct usb_descriptor_header *)&usb_out_it_desc,
  714. (struct usb_descriptor_header *)&io_in_it_desc,
  715. (struct usb_descriptor_header *)&usb_in_ot_desc,
  716. (struct usb_descriptor_header *)&io_out_ot_desc,
  717. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  718. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  719. (struct usb_descriptor_header *)&as_out_hdr_desc,
  720. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  721. (struct usb_descriptor_header *)&hs_epout_desc,
  722. (struct usb_descriptor_header *)&as_iso_out_desc,
  723. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  724. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  725. (struct usb_descriptor_header *)&as_in_hdr_desc,
  726. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  727. (struct usb_descriptor_header *)&hs_epin_desc,
  728. (struct usb_descriptor_header *)&as_iso_in_desc,
  729. NULL,
  730. };
  731. struct cntrl_cur_lay3 {
  732. __u32 dCUR;
  733. };
  734. struct cntrl_range_lay3 {
  735. __u16 wNumSubRanges;
  736. __u32 dMIN;
  737. __u32 dMAX;
  738. __u32 dRES;
  739. } __packed;
  740. static inline void
  741. free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
  742. {
  743. struct snd_uac2_chip *uac2 = prm_to_uac2(prm);
  744. int i;
  745. prm->ep_enabled = false;
  746. for (i = 0; i < USB_XFERS; i++) {
  747. if (prm->ureq[i].req) {
  748. usb_ep_dequeue(ep, prm->ureq[i].req);
  749. usb_ep_free_request(ep, prm->ureq[i].req);
  750. prm->ureq[i].req = NULL;
  751. }
  752. }
  753. if (usb_ep_disable(ep))
  754. dev_err(&uac2->pdev.dev,
  755. "%s:%d Error!\n", __func__, __LINE__);
  756. }
  757. static int __init
  758. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  759. {
  760. struct audio_dev *agdev = func_to_agdev(fn);
  761. struct snd_uac2_chip *uac2 = &agdev->uac2;
  762. struct usb_composite_dev *cdev = cfg->cdev;
  763. struct usb_gadget *gadget = cdev->gadget;
  764. struct uac2_rtd_params *prm;
  765. int ret;
  766. ret = usb_interface_id(cfg, fn);
  767. if (ret < 0) {
  768. dev_err(&uac2->pdev.dev,
  769. "%s:%d Error!\n", __func__, __LINE__);
  770. return ret;
  771. }
  772. std_ac_if_desc.bInterfaceNumber = ret;
  773. ALT_SET(agdev->ac_alt, 0);
  774. INTF_SET(agdev->ac_alt, ret);
  775. ret = usb_interface_id(cfg, fn);
  776. if (ret < 0) {
  777. dev_err(&uac2->pdev.dev,
  778. "%s:%d Error!\n", __func__, __LINE__);
  779. return ret;
  780. }
  781. std_as_out_if0_desc.bInterfaceNumber = ret;
  782. std_as_out_if1_desc.bInterfaceNumber = ret;
  783. ALT_SET(agdev->as_out_alt, 0);
  784. INTF_SET(agdev->as_out_alt, ret);
  785. ret = usb_interface_id(cfg, fn);
  786. if (ret < 0) {
  787. dev_err(&uac2->pdev.dev,
  788. "%s:%d Error!\n", __func__, __LINE__);
  789. return ret;
  790. }
  791. std_as_in_if0_desc.bInterfaceNumber = ret;
  792. std_as_in_if1_desc.bInterfaceNumber = ret;
  793. ALT_SET(agdev->as_in_alt, 0);
  794. INTF_SET(agdev->as_in_alt, ret);
  795. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  796. if (!agdev->out_ep) {
  797. dev_err(&uac2->pdev.dev,
  798. "%s:%d Error!\n", __func__, __LINE__);
  799. goto err;
  800. }
  801. agdev->out_ep->driver_data = agdev;
  802. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  803. if (!agdev->in_ep) {
  804. dev_err(&uac2->pdev.dev,
  805. "%s:%d Error!\n", __func__, __LINE__);
  806. goto err;
  807. }
  808. agdev->in_ep->driver_data = agdev;
  809. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  810. hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
  811. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  812. hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
  813. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
  814. if (ret)
  815. goto err;
  816. prm = &agdev->uac2.c_prm;
  817. prm->max_psize = hs_epout_desc.wMaxPacketSize;
  818. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  819. if (!prm->rbuf) {
  820. prm->max_psize = 0;
  821. dev_err(&uac2->pdev.dev,
  822. "%s:%d Error!\n", __func__, __LINE__);
  823. goto err;
  824. }
  825. prm = &agdev->uac2.p_prm;
  826. prm->max_psize = hs_epin_desc.wMaxPacketSize;
  827. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  828. if (!prm->rbuf) {
  829. prm->max_psize = 0;
  830. dev_err(&uac2->pdev.dev,
  831. "%s:%d Error!\n", __func__, __LINE__);
  832. goto err;
  833. }
  834. ret = alsa_uac2_init(agdev);
  835. if (ret)
  836. goto err;
  837. return 0;
  838. err:
  839. kfree(agdev->uac2.p_prm.rbuf);
  840. kfree(agdev->uac2.c_prm.rbuf);
  841. usb_free_all_descriptors(fn);
  842. if (agdev->in_ep)
  843. agdev->in_ep->driver_data = NULL;
  844. if (agdev->out_ep)
  845. agdev->out_ep->driver_data = NULL;
  846. return -EINVAL;
  847. }
  848. static void
  849. afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
  850. {
  851. struct audio_dev *agdev = func_to_agdev(fn);
  852. struct uac2_rtd_params *prm;
  853. alsa_uac2_exit(agdev);
  854. prm = &agdev->uac2.p_prm;
  855. kfree(prm->rbuf);
  856. prm = &agdev->uac2.c_prm;
  857. kfree(prm->rbuf);
  858. usb_free_all_descriptors(fn);
  859. if (agdev->in_ep)
  860. agdev->in_ep->driver_data = NULL;
  861. if (agdev->out_ep)
  862. agdev->out_ep->driver_data = NULL;
  863. }
  864. static int
  865. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  866. {
  867. struct usb_composite_dev *cdev = fn->config->cdev;
  868. struct audio_dev *agdev = func_to_agdev(fn);
  869. struct snd_uac2_chip *uac2 = &agdev->uac2;
  870. struct usb_gadget *gadget = cdev->gadget;
  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(&uac2->pdev.dev,
  878. "%s:%d Error!\n", __func__, __LINE__);
  879. return -EINVAL;
  880. }
  881. if (intf == INTF_GET(agdev->ac_alt)) {
  882. /* Control I/f has only 1 AltSetting - 0 */
  883. if (alt) {
  884. dev_err(&uac2->pdev.dev,
  885. "%s:%d Error!\n", __func__, __LINE__);
  886. return -EINVAL;
  887. }
  888. return 0;
  889. }
  890. if (intf == INTF_GET(agdev->as_out_alt)) {
  891. ep = agdev->out_ep;
  892. prm = &uac2->c_prm;
  893. config_ep_by_speed(gadget, fn, ep);
  894. ALT_SET(agdev->as_out_alt, alt);
  895. } else if (intf == INTF_GET(agdev->as_in_alt)) {
  896. ep = agdev->in_ep;
  897. prm = &uac2->p_prm;
  898. config_ep_by_speed(gadget, fn, ep);
  899. ALT_SET(agdev->as_in_alt, alt);
  900. } else {
  901. dev_err(&uac2->pdev.dev,
  902. "%s:%d Error!\n", __func__, __LINE__);
  903. return -EINVAL;
  904. }
  905. if (alt == 0) {
  906. free_ep(prm, ep);
  907. return 0;
  908. }
  909. prm->ep_enabled = true;
  910. usb_ep_enable(ep);
  911. for (i = 0; i < USB_XFERS; i++) {
  912. if (prm->ureq[i].req) {
  913. if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
  914. dev_err(&uac2->pdev.dev, "%d Error!\n",
  915. __LINE__);
  916. continue;
  917. }
  918. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  919. if (req == NULL) {
  920. dev_err(&uac2->pdev.dev,
  921. "%s:%d Error!\n", __func__, __LINE__);
  922. return -EINVAL;
  923. }
  924. prm->ureq[i].req = req;
  925. prm->ureq[i].pp = prm;
  926. req->zero = 0;
  927. req->context = &prm->ureq[i];
  928. req->length = prm->max_psize;
  929. req->complete = agdev_iso_complete;
  930. req->buf = prm->rbuf + i * req->length;
  931. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  932. dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
  933. }
  934. return 0;
  935. }
  936. static int
  937. afunc_get_alt(struct usb_function *fn, unsigned intf)
  938. {
  939. struct audio_dev *agdev = func_to_agdev(fn);
  940. struct snd_uac2_chip *uac2 = &agdev->uac2;
  941. if (intf == INTF_GET(agdev->ac_alt))
  942. return ALT_GET(agdev->ac_alt);
  943. else if (intf == INTF_GET(agdev->as_out_alt))
  944. return ALT_GET(agdev->as_out_alt);
  945. else if (intf == INTF_GET(agdev->as_in_alt))
  946. return ALT_GET(agdev->as_in_alt);
  947. else
  948. dev_err(&uac2->pdev.dev,
  949. "%s:%d Invalid Interface %d!\n",
  950. __func__, __LINE__, intf);
  951. return -EINVAL;
  952. }
  953. static void
  954. afunc_disable(struct usb_function *fn)
  955. {
  956. struct audio_dev *agdev = func_to_agdev(fn);
  957. struct snd_uac2_chip *uac2 = &agdev->uac2;
  958. free_ep(&uac2->p_prm, agdev->in_ep);
  959. ALT_SET(agdev->as_in_alt, 0);
  960. free_ep(&uac2->c_prm, agdev->out_ep);
  961. ALT_SET(agdev->as_out_alt, 0);
  962. }
  963. static int
  964. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  965. {
  966. struct usb_request *req = fn->config->cdev->req;
  967. struct audio_dev *agdev = func_to_agdev(fn);
  968. struct snd_uac2_chip *uac2 = &agdev->uac2;
  969. u16 w_length = le16_to_cpu(cr->wLength);
  970. u16 w_index = le16_to_cpu(cr->wIndex);
  971. u16 w_value = le16_to_cpu(cr->wValue);
  972. u8 entity_id = (w_index >> 8) & 0xff;
  973. u8 control_selector = w_value >> 8;
  974. int value = -EOPNOTSUPP;
  975. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  976. struct cntrl_cur_lay3 c;
  977. if (entity_id == USB_IN_CLK_ID)
  978. c.dCUR = p_srate;
  979. else if (entity_id == USB_OUT_CLK_ID)
  980. c.dCUR = c_srate;
  981. value = min_t(unsigned, w_length, sizeof c);
  982. memcpy(req->buf, &c, value);
  983. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  984. *(u8 *)req->buf = 1;
  985. value = min_t(unsigned, w_length, 1);
  986. } else {
  987. dev_err(&uac2->pdev.dev,
  988. "%s:%d control_selector=%d TODO!\n",
  989. __func__, __LINE__, control_selector);
  990. }
  991. return value;
  992. }
  993. static int
  994. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  995. {
  996. struct usb_request *req = fn->config->cdev->req;
  997. struct audio_dev *agdev = func_to_agdev(fn);
  998. struct snd_uac2_chip *uac2 = &agdev->uac2;
  999. u16 w_length = le16_to_cpu(cr->wLength);
  1000. u16 w_index = le16_to_cpu(cr->wIndex);
  1001. u16 w_value = le16_to_cpu(cr->wValue);
  1002. u8 entity_id = (w_index >> 8) & 0xff;
  1003. u8 control_selector = w_value >> 8;
  1004. struct cntrl_range_lay3 r;
  1005. int value = -EOPNOTSUPP;
  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 != INTF_GET(agdev->ac_alt)) {
  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 int audio_bind_config(struct usb_configuration *cfg)
  1092. {
  1093. int id, res;
  1094. agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
  1095. if (agdev_g == NULL) {
  1096. printk(KERN_ERR "Unable to allocate audio gadget\n");
  1097. return -ENOMEM;
  1098. }
  1099. id = usb_string_id(cfg->cdev);
  1100. if (id < 0)
  1101. return id;
  1102. strings_fn[STR_ASSOC].id = id;
  1103. iad_desc.iFunction = id,
  1104. id = usb_string_id(cfg->cdev);
  1105. if (id < 0)
  1106. return id;
  1107. strings_fn[STR_IF_CTRL].id = id;
  1108. std_ac_if_desc.iInterface = id,
  1109. id = usb_string_id(cfg->cdev);
  1110. if (id < 0)
  1111. return id;
  1112. strings_fn[STR_CLKSRC_IN].id = id;
  1113. in_clk_src_desc.iClockSource = id,
  1114. id = usb_string_id(cfg->cdev);
  1115. if (id < 0)
  1116. return id;
  1117. strings_fn[STR_CLKSRC_OUT].id = id;
  1118. out_clk_src_desc.iClockSource = id,
  1119. id = usb_string_id(cfg->cdev);
  1120. if (id < 0)
  1121. return id;
  1122. strings_fn[STR_USB_IT].id = id;
  1123. usb_out_it_desc.iTerminal = id,
  1124. id = usb_string_id(cfg->cdev);
  1125. if (id < 0)
  1126. return id;
  1127. strings_fn[STR_IO_IT].id = id;
  1128. io_in_it_desc.iTerminal = id;
  1129. id = usb_string_id(cfg->cdev);
  1130. if (id < 0)
  1131. return id;
  1132. strings_fn[STR_USB_OT].id = id;
  1133. usb_in_ot_desc.iTerminal = id;
  1134. id = usb_string_id(cfg->cdev);
  1135. if (id < 0)
  1136. return id;
  1137. strings_fn[STR_IO_OT].id = id;
  1138. io_out_ot_desc.iTerminal = id;
  1139. id = usb_string_id(cfg->cdev);
  1140. if (id < 0)
  1141. return id;
  1142. strings_fn[STR_AS_OUT_ALT0].id = id;
  1143. std_as_out_if0_desc.iInterface = id;
  1144. id = usb_string_id(cfg->cdev);
  1145. if (id < 0)
  1146. return id;
  1147. strings_fn[STR_AS_OUT_ALT1].id = id;
  1148. std_as_out_if1_desc.iInterface = id;
  1149. id = usb_string_id(cfg->cdev);
  1150. if (id < 0)
  1151. return id;
  1152. strings_fn[STR_AS_IN_ALT0].id = id;
  1153. std_as_in_if0_desc.iInterface = id;
  1154. id = usb_string_id(cfg->cdev);
  1155. if (id < 0)
  1156. return id;
  1157. strings_fn[STR_AS_IN_ALT1].id = id;
  1158. std_as_in_if1_desc.iInterface = id;
  1159. agdev_g->func.name = "uac2_func";
  1160. agdev_g->func.strings = fn_strings;
  1161. agdev_g->func.bind = afunc_bind;
  1162. agdev_g->func.unbind = afunc_unbind;
  1163. agdev_g->func.set_alt = afunc_set_alt;
  1164. agdev_g->func.get_alt = afunc_get_alt;
  1165. agdev_g->func.disable = afunc_disable;
  1166. agdev_g->func.setup = afunc_setup;
  1167. /* Initialize the configurable parameters */
  1168. usb_out_it_desc.bNrChannels = num_channels(c_chmask);
  1169. usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask);
  1170. io_in_it_desc.bNrChannels = num_channels(p_chmask);
  1171. io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask);
  1172. as_out_hdr_desc.bNrChannels = num_channels(c_chmask);
  1173. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask);
  1174. as_in_hdr_desc.bNrChannels = num_channels(p_chmask);
  1175. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask);
  1176. as_out_fmt1_desc.bSubslotSize = c_ssize;
  1177. as_out_fmt1_desc.bBitResolution = c_ssize * 8;
  1178. as_in_fmt1_desc.bSubslotSize = p_ssize;
  1179. as_in_fmt1_desc.bBitResolution = p_ssize * 8;
  1180. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate);
  1181. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate);
  1182. res = usb_add_function(cfg, &agdev_g->func);
  1183. if (res < 0)
  1184. kfree(agdev_g);
  1185. return res;
  1186. }
  1187. static void
  1188. uac2_unbind_config(struct usb_configuration *cfg)
  1189. {
  1190. kfree(agdev_g);
  1191. agdev_g = NULL;
  1192. }