f_uac2.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 __devexit 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. agdev->out_ep->driver_data = agdev;
  801. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  802. if (!agdev->in_ep)
  803. dev_err(&uac2->pdev.dev,
  804. "%s:%d Error!\n", __func__, __LINE__);
  805. agdev->in_ep->driver_data = agdev;
  806. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  807. hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
  808. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  809. hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
  810. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
  811. if (ret)
  812. goto err;
  813. prm = &agdev->uac2.c_prm;
  814. prm->max_psize = hs_epout_desc.wMaxPacketSize;
  815. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  816. if (!prm->rbuf) {
  817. prm->max_psize = 0;
  818. dev_err(&uac2->pdev.dev,
  819. "%s:%d Error!\n", __func__, __LINE__);
  820. }
  821. prm = &agdev->uac2.p_prm;
  822. prm->max_psize = hs_epin_desc.wMaxPacketSize;
  823. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  824. if (!prm->rbuf) {
  825. prm->max_psize = 0;
  826. dev_err(&uac2->pdev.dev,
  827. "%s:%d Error!\n", __func__, __LINE__);
  828. }
  829. ret = alsa_uac2_init(agdev);
  830. if (ret)
  831. goto err;
  832. return 0;
  833. err:
  834. kfree(agdev->uac2.p_prm.rbuf);
  835. kfree(agdev->uac2.c_prm.rbuf);
  836. usb_free_all_descriptors(fn);
  837. if (agdev->in_ep)
  838. agdev->in_ep->driver_data = NULL;
  839. if (agdev->out_ep)
  840. agdev->out_ep->driver_data = NULL;
  841. return -EINVAL;
  842. }
  843. static void
  844. afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
  845. {
  846. struct audio_dev *agdev = func_to_agdev(fn);
  847. struct uac2_rtd_params *prm;
  848. alsa_uac2_exit(agdev);
  849. prm = &agdev->uac2.p_prm;
  850. kfree(prm->rbuf);
  851. prm = &agdev->uac2.c_prm;
  852. kfree(prm->rbuf);
  853. usb_free_all_descriptors(fn);
  854. if (agdev->in_ep)
  855. agdev->in_ep->driver_data = NULL;
  856. if (agdev->out_ep)
  857. agdev->out_ep->driver_data = NULL;
  858. }
  859. static int
  860. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  861. {
  862. struct usb_composite_dev *cdev = fn->config->cdev;
  863. struct audio_dev *agdev = func_to_agdev(fn);
  864. struct snd_uac2_chip *uac2 = &agdev->uac2;
  865. struct usb_gadget *gadget = cdev->gadget;
  866. struct usb_request *req;
  867. struct usb_ep *ep;
  868. struct uac2_rtd_params *prm;
  869. int i;
  870. /* No i/f has more than 2 alt settings */
  871. if (alt > 1) {
  872. dev_err(&uac2->pdev.dev,
  873. "%s:%d Error!\n", __func__, __LINE__);
  874. return -EINVAL;
  875. }
  876. if (intf == INTF_GET(agdev->ac_alt)) {
  877. /* Control I/f has only 1 AltSetting - 0 */
  878. if (alt) {
  879. dev_err(&uac2->pdev.dev,
  880. "%s:%d Error!\n", __func__, __LINE__);
  881. return -EINVAL;
  882. }
  883. return 0;
  884. }
  885. if (intf == INTF_GET(agdev->as_out_alt)) {
  886. ep = agdev->out_ep;
  887. prm = &uac2->c_prm;
  888. config_ep_by_speed(gadget, fn, ep);
  889. ALT_SET(agdev->as_out_alt, alt);
  890. } else if (intf == INTF_GET(agdev->as_in_alt)) {
  891. ep = agdev->in_ep;
  892. prm = &uac2->p_prm;
  893. config_ep_by_speed(gadget, fn, ep);
  894. ALT_SET(agdev->as_in_alt, alt);
  895. } else {
  896. dev_err(&uac2->pdev.dev,
  897. "%s:%d Error!\n", __func__, __LINE__);
  898. return -EINVAL;
  899. }
  900. if (alt == 0) {
  901. free_ep(prm, ep);
  902. return 0;
  903. }
  904. prm->ep_enabled = true;
  905. usb_ep_enable(ep);
  906. for (i = 0; i < USB_XFERS; i++) {
  907. if (prm->ureq[i].req) {
  908. if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
  909. dev_err(&uac2->pdev.dev, "%d Error!\n",
  910. __LINE__);
  911. continue;
  912. }
  913. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  914. if (req == NULL) {
  915. dev_err(&uac2->pdev.dev,
  916. "%s:%d Error!\n", __func__, __LINE__);
  917. return -EINVAL;
  918. }
  919. prm->ureq[i].req = req;
  920. prm->ureq[i].pp = prm;
  921. req->zero = 0;
  922. req->dma = DMA_ADDR_INVALID;
  923. req->context = &prm->ureq[i];
  924. req->length = prm->max_psize;
  925. req->complete = agdev_iso_complete;
  926. req->buf = prm->rbuf + i * req->length;
  927. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  928. dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
  929. }
  930. return 0;
  931. }
  932. static int
  933. afunc_get_alt(struct usb_function *fn, unsigned intf)
  934. {
  935. struct audio_dev *agdev = func_to_agdev(fn);
  936. struct snd_uac2_chip *uac2 = &agdev->uac2;
  937. if (intf == INTF_GET(agdev->ac_alt))
  938. return ALT_GET(agdev->ac_alt);
  939. else if (intf == INTF_GET(agdev->as_out_alt))
  940. return ALT_GET(agdev->as_out_alt);
  941. else if (intf == INTF_GET(agdev->as_in_alt))
  942. return ALT_GET(agdev->as_in_alt);
  943. else
  944. dev_err(&uac2->pdev.dev,
  945. "%s:%d Invalid Interface %d!\n",
  946. __func__, __LINE__, intf);
  947. return -EINVAL;
  948. }
  949. static void
  950. afunc_disable(struct usb_function *fn)
  951. {
  952. struct audio_dev *agdev = func_to_agdev(fn);
  953. struct snd_uac2_chip *uac2 = &agdev->uac2;
  954. free_ep(&uac2->p_prm, agdev->in_ep);
  955. ALT_SET(agdev->as_in_alt, 0);
  956. free_ep(&uac2->c_prm, agdev->out_ep);
  957. ALT_SET(agdev->as_out_alt, 0);
  958. }
  959. static int
  960. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  961. {
  962. struct usb_request *req = fn->config->cdev->req;
  963. struct audio_dev *agdev = func_to_agdev(fn);
  964. struct snd_uac2_chip *uac2 = &agdev->uac2;
  965. u16 w_length = le16_to_cpu(cr->wLength);
  966. u16 w_index = le16_to_cpu(cr->wIndex);
  967. u16 w_value = le16_to_cpu(cr->wValue);
  968. u8 entity_id = (w_index >> 8) & 0xff;
  969. u8 control_selector = w_value >> 8;
  970. int value = -EOPNOTSUPP;
  971. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  972. struct cntrl_cur_lay3 c;
  973. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  974. if (entity_id == USB_IN_CLK_ID)
  975. c.dCUR = p_srate;
  976. else if (entity_id == USB_OUT_CLK_ID)
  977. c.dCUR = c_srate;
  978. value = min_t(unsigned, w_length, sizeof c);
  979. memcpy(req->buf, &c, value);
  980. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  981. *(u8 *)req->buf = 1;
  982. value = min_t(unsigned, w_length, 1);
  983. } else {
  984. dev_err(&uac2->pdev.dev,
  985. "%s:%d control_selector=%d TODO!\n",
  986. __func__, __LINE__, control_selector);
  987. }
  988. return value;
  989. }
  990. static int
  991. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  992. {
  993. struct usb_request *req = fn->config->cdev->req;
  994. struct audio_dev *agdev = func_to_agdev(fn);
  995. struct snd_uac2_chip *uac2 = &agdev->uac2;
  996. u16 w_length = le16_to_cpu(cr->wLength);
  997. u16 w_index = le16_to_cpu(cr->wIndex);
  998. u16 w_value = le16_to_cpu(cr->wValue);
  999. u8 entity_id = (w_index >> 8) & 0xff;
  1000. u8 control_selector = w_value >> 8;
  1001. struct cntrl_range_lay3 r;
  1002. int value = -EOPNOTSUPP;
  1003. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1004. if (entity_id == USB_IN_CLK_ID)
  1005. r.dMIN = p_srate;
  1006. else if (entity_id == USB_OUT_CLK_ID)
  1007. r.dMIN = c_srate;
  1008. else
  1009. return -EOPNOTSUPP;
  1010. r.dMAX = r.dMIN;
  1011. r.dRES = 0;
  1012. r.wNumSubRanges = 1;
  1013. value = min_t(unsigned, w_length, sizeof r);
  1014. memcpy(req->buf, &r, value);
  1015. } else {
  1016. dev_err(&uac2->pdev.dev,
  1017. "%s:%d control_selector=%d TODO!\n",
  1018. __func__, __LINE__, control_selector);
  1019. }
  1020. return value;
  1021. }
  1022. static int
  1023. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1024. {
  1025. if (cr->bRequest == UAC2_CS_CUR)
  1026. return in_rq_cur(fn, cr);
  1027. else if (cr->bRequest == UAC2_CS_RANGE)
  1028. return in_rq_range(fn, cr);
  1029. else
  1030. return -EOPNOTSUPP;
  1031. }
  1032. static int
  1033. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1034. {
  1035. u16 w_length = le16_to_cpu(cr->wLength);
  1036. u16 w_value = le16_to_cpu(cr->wValue);
  1037. u8 control_selector = w_value >> 8;
  1038. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  1039. return w_length;
  1040. return -EOPNOTSUPP;
  1041. }
  1042. static int
  1043. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1044. {
  1045. struct audio_dev *agdev = func_to_agdev(fn);
  1046. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1047. u16 w_index = le16_to_cpu(cr->wIndex);
  1048. u8 intf = w_index & 0xff;
  1049. if (intf != INTF_GET(agdev->ac_alt)) {
  1050. dev_err(&uac2->pdev.dev,
  1051. "%s:%d Error!\n", __func__, __LINE__);
  1052. return -EOPNOTSUPP;
  1053. }
  1054. if (cr->bRequestType & USB_DIR_IN)
  1055. return ac_rq_in(fn, cr);
  1056. else if (cr->bRequest == UAC2_CS_CUR)
  1057. return out_rq_cur(fn, cr);
  1058. return -EOPNOTSUPP;
  1059. }
  1060. static int
  1061. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1062. {
  1063. struct usb_composite_dev *cdev = fn->config->cdev;
  1064. struct audio_dev *agdev = func_to_agdev(fn);
  1065. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1066. struct usb_request *req = cdev->req;
  1067. u16 w_length = le16_to_cpu(cr->wLength);
  1068. int value = -EOPNOTSUPP;
  1069. /* Only Class specific requests are supposed to reach here */
  1070. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  1071. return -EOPNOTSUPP;
  1072. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  1073. value = setup_rq_inf(fn, cr);
  1074. else
  1075. dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
  1076. if (value >= 0) {
  1077. req->length = value;
  1078. req->zero = value < w_length;
  1079. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1080. if (value < 0) {
  1081. dev_err(&uac2->pdev.dev,
  1082. "%s:%d Error!\n", __func__, __LINE__);
  1083. req->status = 0;
  1084. }
  1085. }
  1086. return value;
  1087. }
  1088. static int audio_bind_config(struct usb_configuration *cfg)
  1089. {
  1090. int id, res;
  1091. agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
  1092. if (agdev_g == NULL) {
  1093. printk(KERN_ERR "Unable to allocate audio gadget\n");
  1094. return -ENOMEM;
  1095. }
  1096. id = usb_string_id(cfg->cdev);
  1097. if (id < 0)
  1098. return id;
  1099. strings_fn[STR_ASSOC].id = id;
  1100. iad_desc.iFunction = id,
  1101. id = usb_string_id(cfg->cdev);
  1102. if (id < 0)
  1103. return id;
  1104. strings_fn[STR_IF_CTRL].id = id;
  1105. std_ac_if_desc.iInterface = id,
  1106. id = usb_string_id(cfg->cdev);
  1107. if (id < 0)
  1108. return id;
  1109. strings_fn[STR_CLKSRC_IN].id = id;
  1110. in_clk_src_desc.iClockSource = id,
  1111. id = usb_string_id(cfg->cdev);
  1112. if (id < 0)
  1113. return id;
  1114. strings_fn[STR_CLKSRC_OUT].id = id;
  1115. out_clk_src_desc.iClockSource = id,
  1116. id = usb_string_id(cfg->cdev);
  1117. if (id < 0)
  1118. return id;
  1119. strings_fn[STR_USB_IT].id = id;
  1120. usb_out_it_desc.iTerminal = id,
  1121. id = usb_string_id(cfg->cdev);
  1122. if (id < 0)
  1123. return id;
  1124. strings_fn[STR_IO_IT].id = id;
  1125. io_in_it_desc.iTerminal = id;
  1126. id = usb_string_id(cfg->cdev);
  1127. if (id < 0)
  1128. return id;
  1129. strings_fn[STR_USB_OT].id = id;
  1130. usb_in_ot_desc.iTerminal = id;
  1131. id = usb_string_id(cfg->cdev);
  1132. if (id < 0)
  1133. return id;
  1134. strings_fn[STR_IO_OT].id = id;
  1135. io_out_ot_desc.iTerminal = id;
  1136. id = usb_string_id(cfg->cdev);
  1137. if (id < 0)
  1138. return id;
  1139. strings_fn[STR_AS_OUT_ALT0].id = id;
  1140. std_as_out_if0_desc.iInterface = id;
  1141. id = usb_string_id(cfg->cdev);
  1142. if (id < 0)
  1143. return id;
  1144. strings_fn[STR_AS_OUT_ALT1].id = id;
  1145. std_as_out_if1_desc.iInterface = id;
  1146. id = usb_string_id(cfg->cdev);
  1147. if (id < 0)
  1148. return id;
  1149. strings_fn[STR_AS_IN_ALT0].id = id;
  1150. std_as_in_if0_desc.iInterface = id;
  1151. id = usb_string_id(cfg->cdev);
  1152. if (id < 0)
  1153. return id;
  1154. strings_fn[STR_AS_IN_ALT1].id = id;
  1155. std_as_in_if1_desc.iInterface = id;
  1156. agdev_g->func.name = "uac2_func";
  1157. agdev_g->func.strings = fn_strings;
  1158. agdev_g->func.bind = afunc_bind;
  1159. agdev_g->func.unbind = afunc_unbind;
  1160. agdev_g->func.set_alt = afunc_set_alt;
  1161. agdev_g->func.get_alt = afunc_get_alt;
  1162. agdev_g->func.disable = afunc_disable;
  1163. agdev_g->func.setup = afunc_setup;
  1164. /* Initialize the configurable parameters */
  1165. usb_out_it_desc.bNrChannels = num_channels(c_chmask);
  1166. usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask);
  1167. io_in_it_desc.bNrChannels = num_channels(p_chmask);
  1168. io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask);
  1169. as_out_hdr_desc.bNrChannels = num_channels(c_chmask);
  1170. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask);
  1171. as_in_hdr_desc.bNrChannels = num_channels(p_chmask);
  1172. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask);
  1173. as_out_fmt1_desc.bSubslotSize = c_ssize;
  1174. as_out_fmt1_desc.bBitResolution = c_ssize * 8;
  1175. as_in_fmt1_desc.bSubslotSize = p_ssize;
  1176. as_in_fmt1_desc.bBitResolution = p_ssize * 8;
  1177. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate);
  1178. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate);
  1179. res = usb_add_function(cfg, &agdev_g->func);
  1180. if (res < 0)
  1181. kfree(agdev_g);
  1182. return res;
  1183. }
  1184. static void
  1185. uac2_unbind_config(struct usb_configuration *cfg)
  1186. {
  1187. kfree(agdev_g);
  1188. agdev_g = NULL;
  1189. }