f_uac2.c 36 KB

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