f_uac2.c 41 KB

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