f_uac2.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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_interface_assoc_descriptor iad_desc = {
  489. .bLength = sizeof iad_desc,
  490. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  491. .bFirstInterface = 0,
  492. .bInterfaceCount = 3,
  493. .bFunctionClass = USB_CLASS_AUDIO,
  494. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  495. .bFunctionProtocol = UAC_VERSION_2,
  496. };
  497. /* Audio Control Interface */
  498. static struct usb_interface_descriptor std_ac_if_desc = {
  499. .bLength = sizeof std_ac_if_desc,
  500. .bDescriptorType = USB_DT_INTERFACE,
  501. .bAlternateSetting = 0,
  502. .bNumEndpoints = 0,
  503. .bInterfaceClass = USB_CLASS_AUDIO,
  504. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  505. .bInterfaceProtocol = UAC_VERSION_2,
  506. };
  507. /* Clock source for IN traffic */
  508. static struct uac_clock_source_descriptor in_clk_src_desc = {
  509. .bLength = sizeof in_clk_src_desc,
  510. .bDescriptorType = USB_DT_CS_INTERFACE,
  511. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  512. .bClockID = USB_IN_CLK_ID,
  513. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  514. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  515. .bAssocTerminal = 0,
  516. };
  517. /* Clock source for OUT traffic */
  518. static struct uac_clock_source_descriptor out_clk_src_desc = {
  519. .bLength = sizeof out_clk_src_desc,
  520. .bDescriptorType = USB_DT_CS_INTERFACE,
  521. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  522. .bClockID = USB_OUT_CLK_ID,
  523. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  524. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  525. .bAssocTerminal = 0,
  526. };
  527. /* Input Terminal for USB_OUT */
  528. static struct uac2_input_terminal_descriptor usb_out_it_desc = {
  529. .bLength = sizeof usb_out_it_desc,
  530. .bDescriptorType = USB_DT_CS_INTERFACE,
  531. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  532. .bTerminalID = USB_OUT_IT_ID,
  533. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  534. .bAssocTerminal = 0,
  535. .bCSourceID = USB_OUT_CLK_ID,
  536. .iChannelNames = 0,
  537. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  538. };
  539. /* Input Terminal for I/O-In */
  540. static struct uac2_input_terminal_descriptor io_in_it_desc = {
  541. .bLength = sizeof io_in_it_desc,
  542. .bDescriptorType = USB_DT_CS_INTERFACE,
  543. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  544. .bTerminalID = IO_IN_IT_ID,
  545. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
  546. .bAssocTerminal = 0,
  547. .bCSourceID = USB_IN_CLK_ID,
  548. .iChannelNames = 0,
  549. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  550. };
  551. /* Ouput Terminal for USB_IN */
  552. static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  553. .bLength = sizeof usb_in_ot_desc,
  554. .bDescriptorType = USB_DT_CS_INTERFACE,
  555. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  556. .bTerminalID = USB_IN_OT_ID,
  557. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  558. .bAssocTerminal = 0,
  559. .bSourceID = IO_IN_IT_ID,
  560. .bCSourceID = USB_IN_CLK_ID,
  561. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  562. };
  563. /* Ouput Terminal for I/O-Out */
  564. static struct uac2_output_terminal_descriptor io_out_ot_desc = {
  565. .bLength = sizeof io_out_ot_desc,
  566. .bDescriptorType = USB_DT_CS_INTERFACE,
  567. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  568. .bTerminalID = IO_OUT_OT_ID,
  569. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
  570. .bAssocTerminal = 0,
  571. .bSourceID = USB_OUT_IT_ID,
  572. .bCSourceID = USB_OUT_CLK_ID,
  573. .bmControls = (CONTROL_RDWR << COPY_CTRL),
  574. };
  575. static struct uac2_ac_header_descriptor ac_hdr_desc = {
  576. .bLength = sizeof ac_hdr_desc,
  577. .bDescriptorType = USB_DT_CS_INTERFACE,
  578. .bDescriptorSubtype = UAC_MS_HEADER,
  579. .bcdADC = cpu_to_le16(0x200),
  580. .bCategory = UAC2_FUNCTION_IO_BOX,
  581. .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
  582. + sizeof usb_out_it_desc + sizeof io_in_it_desc
  583. + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
  584. .bmControls = 0,
  585. };
  586. /* Audio Streaming OUT Interface - Alt0 */
  587. static struct usb_interface_descriptor std_as_out_if0_desc = {
  588. .bLength = sizeof std_as_out_if0_desc,
  589. .bDescriptorType = USB_DT_INTERFACE,
  590. .bAlternateSetting = 0,
  591. .bNumEndpoints = 0,
  592. .bInterfaceClass = USB_CLASS_AUDIO,
  593. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  594. .bInterfaceProtocol = UAC_VERSION_2,
  595. };
  596. /* Audio Streaming OUT Interface - Alt1 */
  597. static struct usb_interface_descriptor std_as_out_if1_desc = {
  598. .bLength = sizeof std_as_out_if1_desc,
  599. .bDescriptorType = USB_DT_INTERFACE,
  600. .bAlternateSetting = 1,
  601. .bNumEndpoints = 1,
  602. .bInterfaceClass = USB_CLASS_AUDIO,
  603. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  604. .bInterfaceProtocol = UAC_VERSION_2,
  605. };
  606. /* Audio Stream OUT Intface Desc */
  607. static struct uac2_as_header_descriptor as_out_hdr_desc = {
  608. .bLength = sizeof as_out_hdr_desc,
  609. .bDescriptorType = USB_DT_CS_INTERFACE,
  610. .bDescriptorSubtype = UAC_AS_GENERAL,
  611. .bTerminalLink = USB_OUT_IT_ID,
  612. .bmControls = 0,
  613. .bFormatType = UAC_FORMAT_TYPE_I,
  614. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  615. .iChannelNames = 0,
  616. };
  617. /* Audio USB_OUT Format */
  618. static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  619. .bLength = sizeof as_out_fmt1_desc,
  620. .bDescriptorType = USB_DT_CS_INTERFACE,
  621. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  622. .bFormatType = UAC_FORMAT_TYPE_I,
  623. };
  624. /* STD AS ISO OUT Endpoint */
  625. static struct usb_endpoint_descriptor fs_epout_desc = {
  626. .bLength = USB_DT_ENDPOINT_SIZE,
  627. .bDescriptorType = USB_DT_ENDPOINT,
  628. .bEndpointAddress = USB_DIR_OUT,
  629. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  630. .wMaxPacketSize = cpu_to_le16(1023),
  631. .bInterval = 1,
  632. };
  633. static struct usb_endpoint_descriptor hs_epout_desc = {
  634. .bLength = USB_DT_ENDPOINT_SIZE,
  635. .bDescriptorType = USB_DT_ENDPOINT,
  636. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  637. .wMaxPacketSize = cpu_to_le16(1024),
  638. .bInterval = 4,
  639. };
  640. /* CS AS ISO OUT Endpoint */
  641. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  642. .bLength = sizeof as_iso_out_desc,
  643. .bDescriptorType = USB_DT_CS_ENDPOINT,
  644. .bDescriptorSubtype = UAC_EP_GENERAL,
  645. .bmAttributes = 0,
  646. .bmControls = 0,
  647. .bLockDelayUnits = 0,
  648. .wLockDelay = 0,
  649. };
  650. /* Audio Streaming IN Interface - Alt0 */
  651. static struct usb_interface_descriptor std_as_in_if0_desc = {
  652. .bLength = sizeof std_as_in_if0_desc,
  653. .bDescriptorType = USB_DT_INTERFACE,
  654. .bAlternateSetting = 0,
  655. .bNumEndpoints = 0,
  656. .bInterfaceClass = USB_CLASS_AUDIO,
  657. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  658. .bInterfaceProtocol = UAC_VERSION_2,
  659. };
  660. /* Audio Streaming IN Interface - Alt1 */
  661. static struct usb_interface_descriptor std_as_in_if1_desc = {
  662. .bLength = sizeof std_as_in_if1_desc,
  663. .bDescriptorType = USB_DT_INTERFACE,
  664. .bAlternateSetting = 1,
  665. .bNumEndpoints = 1,
  666. .bInterfaceClass = USB_CLASS_AUDIO,
  667. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  668. .bInterfaceProtocol = UAC_VERSION_2,
  669. };
  670. /* Audio Stream IN Intface Desc */
  671. static struct uac2_as_header_descriptor as_in_hdr_desc = {
  672. .bLength = sizeof as_in_hdr_desc,
  673. .bDescriptorType = USB_DT_CS_INTERFACE,
  674. .bDescriptorSubtype = UAC_AS_GENERAL,
  675. .bTerminalLink = USB_IN_OT_ID,
  676. .bmControls = 0,
  677. .bFormatType = UAC_FORMAT_TYPE_I,
  678. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  679. .iChannelNames = 0,
  680. };
  681. /* Audio USB_IN Format */
  682. static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  683. .bLength = sizeof as_in_fmt1_desc,
  684. .bDescriptorType = USB_DT_CS_INTERFACE,
  685. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  686. .bFormatType = UAC_FORMAT_TYPE_I,
  687. };
  688. /* STD AS ISO IN Endpoint */
  689. static struct usb_endpoint_descriptor fs_epin_desc = {
  690. .bLength = USB_DT_ENDPOINT_SIZE,
  691. .bDescriptorType = USB_DT_ENDPOINT,
  692. .bEndpointAddress = USB_DIR_IN,
  693. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  694. .wMaxPacketSize = cpu_to_le16(1023),
  695. .bInterval = 1,
  696. };
  697. static struct usb_endpoint_descriptor hs_epin_desc = {
  698. .bLength = USB_DT_ENDPOINT_SIZE,
  699. .bDescriptorType = USB_DT_ENDPOINT,
  700. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  701. .wMaxPacketSize = cpu_to_le16(1024),
  702. .bInterval = 4,
  703. };
  704. /* CS AS ISO IN Endpoint */
  705. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  706. .bLength = sizeof as_iso_in_desc,
  707. .bDescriptorType = USB_DT_CS_ENDPOINT,
  708. .bDescriptorSubtype = UAC_EP_GENERAL,
  709. .bmAttributes = 0,
  710. .bmControls = 0,
  711. .bLockDelayUnits = 0,
  712. .wLockDelay = 0,
  713. };
  714. static struct usb_descriptor_header *fs_audio_desc[] = {
  715. (struct usb_descriptor_header *)&iad_desc,
  716. (struct usb_descriptor_header *)&std_ac_if_desc,
  717. (struct usb_descriptor_header *)&ac_hdr_desc,
  718. (struct usb_descriptor_header *)&in_clk_src_desc,
  719. (struct usb_descriptor_header *)&out_clk_src_desc,
  720. (struct usb_descriptor_header *)&usb_out_it_desc,
  721. (struct usb_descriptor_header *)&io_in_it_desc,
  722. (struct usb_descriptor_header *)&usb_in_ot_desc,
  723. (struct usb_descriptor_header *)&io_out_ot_desc,
  724. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  725. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  726. (struct usb_descriptor_header *)&as_out_hdr_desc,
  727. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  728. (struct usb_descriptor_header *)&fs_epout_desc,
  729. (struct usb_descriptor_header *)&as_iso_out_desc,
  730. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  731. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  732. (struct usb_descriptor_header *)&as_in_hdr_desc,
  733. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  734. (struct usb_descriptor_header *)&fs_epin_desc,
  735. (struct usb_descriptor_header *)&as_iso_in_desc,
  736. NULL,
  737. };
  738. static struct usb_descriptor_header *hs_audio_desc[] = {
  739. (struct usb_descriptor_header *)&iad_desc,
  740. (struct usb_descriptor_header *)&std_ac_if_desc,
  741. (struct usb_descriptor_header *)&ac_hdr_desc,
  742. (struct usb_descriptor_header *)&in_clk_src_desc,
  743. (struct usb_descriptor_header *)&out_clk_src_desc,
  744. (struct usb_descriptor_header *)&usb_out_it_desc,
  745. (struct usb_descriptor_header *)&io_in_it_desc,
  746. (struct usb_descriptor_header *)&usb_in_ot_desc,
  747. (struct usb_descriptor_header *)&io_out_ot_desc,
  748. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  749. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  750. (struct usb_descriptor_header *)&as_out_hdr_desc,
  751. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  752. (struct usb_descriptor_header *)&hs_epout_desc,
  753. (struct usb_descriptor_header *)&as_iso_out_desc,
  754. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  755. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  756. (struct usb_descriptor_header *)&as_in_hdr_desc,
  757. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  758. (struct usb_descriptor_header *)&hs_epin_desc,
  759. (struct usb_descriptor_header *)&as_iso_in_desc,
  760. NULL,
  761. };
  762. struct cntrl_cur_lay3 {
  763. __u32 dCUR;
  764. };
  765. struct cntrl_range_lay3 {
  766. __u16 wNumSubRanges;
  767. __u32 dMIN;
  768. __u32 dMAX;
  769. __u32 dRES;
  770. } __packed;
  771. static inline void
  772. free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
  773. {
  774. struct snd_uac2_chip *uac2 = prm->uac2;
  775. int i;
  776. if (!prm->ep_enabled)
  777. return;
  778. prm->ep_enabled = false;
  779. for (i = 0; i < USB_XFERS; i++) {
  780. if (prm->ureq[i].req) {
  781. usb_ep_dequeue(ep, prm->ureq[i].req);
  782. usb_ep_free_request(ep, prm->ureq[i].req);
  783. prm->ureq[i].req = NULL;
  784. }
  785. }
  786. if (usb_ep_disable(ep))
  787. dev_err(&uac2->pdev.dev,
  788. "%s:%d Error!\n", __func__, __LINE__);
  789. }
  790. static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
  791. struct usb_endpoint_descriptor *ep_desc,
  792. unsigned int factor, bool is_playback)
  793. {
  794. int chmask, srate, ssize;
  795. u16 max_packet_size;
  796. if (is_playback) {
  797. chmask = uac2_opts->p_chmask;
  798. srate = uac2_opts->p_srate;
  799. ssize = uac2_opts->p_ssize;
  800. } else {
  801. chmask = uac2_opts->c_chmask;
  802. srate = uac2_opts->c_srate;
  803. ssize = uac2_opts->c_ssize;
  804. }
  805. max_packet_size = num_channels(chmask) * ssize *
  806. DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
  807. ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
  808. le16_to_cpu(ep_desc->wMaxPacketSize)));
  809. }
  810. static int
  811. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  812. {
  813. struct audio_dev *agdev = func_to_agdev(fn);
  814. struct snd_uac2_chip *uac2 = &agdev->uac2;
  815. struct usb_composite_dev *cdev = cfg->cdev;
  816. struct usb_gadget *gadget = cdev->gadget;
  817. struct device *dev = &uac2->pdev.dev;
  818. struct uac2_rtd_params *prm;
  819. struct f_uac2_opts *uac2_opts;
  820. struct usb_string *us;
  821. int ret;
  822. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  823. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  824. if (IS_ERR(us))
  825. return PTR_ERR(us);
  826. iad_desc.iFunction = us[STR_ASSOC].id;
  827. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  828. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  829. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  830. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  831. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  832. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  833. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  834. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  835. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  836. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  837. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  838. /* Initialize the configurable parameters */
  839. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  840. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  841. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  842. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  843. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  844. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  845. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  846. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  847. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  848. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  849. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  850. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  851. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  852. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  853. ret = usb_interface_id(cfg, fn);
  854. if (ret < 0) {
  855. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  856. return ret;
  857. }
  858. iad_desc.bFirstInterface = ret;
  859. std_ac_if_desc.bInterfaceNumber = ret;
  860. agdev->ac_intf = ret;
  861. agdev->ac_alt = 0;
  862. ret = usb_interface_id(cfg, fn);
  863. if (ret < 0) {
  864. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  865. return ret;
  866. }
  867. std_as_out_if0_desc.bInterfaceNumber = ret;
  868. std_as_out_if1_desc.bInterfaceNumber = ret;
  869. agdev->as_out_intf = ret;
  870. agdev->as_out_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_in_if0_desc.bInterfaceNumber = ret;
  877. std_as_in_if1_desc.bInterfaceNumber = ret;
  878. agdev->as_in_intf = ret;
  879. agdev->as_in_alt = 0;
  880. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  881. if (!agdev->out_ep) {
  882. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  883. return ret;
  884. }
  885. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  886. if (!agdev->in_ep) {
  887. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  888. return ret;
  889. }
  890. uac2->p_prm.uac2 = uac2;
  891. uac2->c_prm.uac2 = uac2;
  892. /* Calculate wMaxPacketSize according to audio bandwidth */
  893. set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
  894. set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
  895. set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
  896. set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
  897. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  898. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  899. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
  900. NULL);
  901. if (ret)
  902. return ret;
  903. prm = &agdev->uac2.c_prm;
  904. prm->max_psize = hs_epout_desc.wMaxPacketSize;
  905. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  906. if (!prm->rbuf) {
  907. prm->max_psize = 0;
  908. goto err_free_descs;
  909. }
  910. prm = &agdev->uac2.p_prm;
  911. prm->max_psize = hs_epin_desc.wMaxPacketSize;
  912. prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
  913. if (!prm->rbuf) {
  914. prm->max_psize = 0;
  915. goto err;
  916. }
  917. ret = alsa_uac2_init(agdev);
  918. if (ret)
  919. goto err;
  920. return 0;
  921. err:
  922. kfree(agdev->uac2.p_prm.rbuf);
  923. kfree(agdev->uac2.c_prm.rbuf);
  924. err_free_descs:
  925. usb_free_all_descriptors(fn);
  926. return -EINVAL;
  927. }
  928. static int
  929. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  930. {
  931. struct usb_composite_dev *cdev = fn->config->cdev;
  932. struct audio_dev *agdev = func_to_agdev(fn);
  933. struct snd_uac2_chip *uac2 = &agdev->uac2;
  934. struct usb_gadget *gadget = cdev->gadget;
  935. struct device *dev = &uac2->pdev.dev;
  936. struct usb_request *req;
  937. struct usb_ep *ep;
  938. struct uac2_rtd_params *prm;
  939. int req_len, i;
  940. /* No i/f has more than 2 alt settings */
  941. if (alt > 1) {
  942. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  943. return -EINVAL;
  944. }
  945. if (intf == agdev->ac_intf) {
  946. /* Control I/f has only 1 AltSetting - 0 */
  947. if (alt) {
  948. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  949. return -EINVAL;
  950. }
  951. return 0;
  952. }
  953. if (intf == agdev->as_out_intf) {
  954. ep = agdev->out_ep;
  955. prm = &uac2->c_prm;
  956. config_ep_by_speed(gadget, fn, ep);
  957. agdev->as_out_alt = alt;
  958. req_len = prm->max_psize;
  959. } else if (intf == agdev->as_in_intf) {
  960. struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev);
  961. unsigned int factor, rate;
  962. struct usb_endpoint_descriptor *ep_desc;
  963. ep = agdev->in_ep;
  964. prm = &uac2->p_prm;
  965. config_ep_by_speed(gadget, fn, ep);
  966. agdev->as_in_alt = alt;
  967. /* pre-calculate the playback endpoint's interval */
  968. if (gadget->speed == USB_SPEED_FULL) {
  969. ep_desc = &fs_epin_desc;
  970. factor = 1000;
  971. } else {
  972. ep_desc = &hs_epin_desc;
  973. factor = 8000;
  974. }
  975. /* pre-compute some values for iso_complete() */
  976. uac2->p_framesize = opts->p_ssize *
  977. num_channels(opts->p_chmask);
  978. rate = opts->p_srate * uac2->p_framesize;
  979. uac2->p_interval = factor / (1 << (ep_desc->bInterval - 1));
  980. uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval,
  981. prm->max_psize);
  982. if (uac2->p_pktsize < prm->max_psize)
  983. uac2->p_pktsize_residue = rate % uac2->p_interval;
  984. else
  985. uac2->p_pktsize_residue = 0;
  986. req_len = uac2->p_pktsize;
  987. uac2->p_residue = 0;
  988. } else {
  989. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  990. return -EINVAL;
  991. }
  992. if (alt == 0) {
  993. free_ep(prm, ep);
  994. return 0;
  995. }
  996. prm->ep_enabled = true;
  997. usb_ep_enable(ep);
  998. for (i = 0; i < USB_XFERS; i++) {
  999. if (!prm->ureq[i].req) {
  1000. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  1001. if (req == NULL)
  1002. return -ENOMEM;
  1003. prm->ureq[i].req = req;
  1004. prm->ureq[i].pp = prm;
  1005. req->zero = 0;
  1006. req->context = &prm->ureq[i];
  1007. req->length = req_len;
  1008. req->complete = agdev_iso_complete;
  1009. req->buf = prm->rbuf + i * prm->max_psize;
  1010. }
  1011. if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
  1012. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1013. }
  1014. return 0;
  1015. }
  1016. static int
  1017. afunc_get_alt(struct usb_function *fn, unsigned intf)
  1018. {
  1019. struct audio_dev *agdev = func_to_agdev(fn);
  1020. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1021. if (intf == agdev->ac_intf)
  1022. return agdev->ac_alt;
  1023. else if (intf == agdev->as_out_intf)
  1024. return agdev->as_out_alt;
  1025. else if (intf == agdev->as_in_intf)
  1026. return agdev->as_in_alt;
  1027. else
  1028. dev_err(&uac2->pdev.dev,
  1029. "%s:%d Invalid Interface %d!\n",
  1030. __func__, __LINE__, intf);
  1031. return -EINVAL;
  1032. }
  1033. static void
  1034. afunc_disable(struct usb_function *fn)
  1035. {
  1036. struct audio_dev *agdev = func_to_agdev(fn);
  1037. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1038. free_ep(&uac2->p_prm, agdev->in_ep);
  1039. agdev->as_in_alt = 0;
  1040. free_ep(&uac2->c_prm, agdev->out_ep);
  1041. agdev->as_out_alt = 0;
  1042. }
  1043. static int
  1044. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1045. {
  1046. struct usb_request *req = fn->config->cdev->req;
  1047. struct audio_dev *agdev = func_to_agdev(fn);
  1048. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1049. struct f_uac2_opts *opts;
  1050. u16 w_length = le16_to_cpu(cr->wLength);
  1051. u16 w_index = le16_to_cpu(cr->wIndex);
  1052. u16 w_value = le16_to_cpu(cr->wValue);
  1053. u8 entity_id = (w_index >> 8) & 0xff;
  1054. u8 control_selector = w_value >> 8;
  1055. int value = -EOPNOTSUPP;
  1056. int p_srate, c_srate;
  1057. opts = agdev_to_uac2_opts(agdev);
  1058. p_srate = opts->p_srate;
  1059. c_srate = opts->c_srate;
  1060. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1061. struct cntrl_cur_lay3 c;
  1062. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  1063. if (entity_id == USB_IN_CLK_ID)
  1064. c.dCUR = p_srate;
  1065. else if (entity_id == USB_OUT_CLK_ID)
  1066. c.dCUR = c_srate;
  1067. value = min_t(unsigned, w_length, sizeof c);
  1068. memcpy(req->buf, &c, value);
  1069. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  1070. *(u8 *)req->buf = 1;
  1071. value = min_t(unsigned, w_length, 1);
  1072. } else {
  1073. dev_err(&uac2->pdev.dev,
  1074. "%s:%d control_selector=%d TODO!\n",
  1075. __func__, __LINE__, control_selector);
  1076. }
  1077. return value;
  1078. }
  1079. static int
  1080. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1081. {
  1082. struct usb_request *req = fn->config->cdev->req;
  1083. struct audio_dev *agdev = func_to_agdev(fn);
  1084. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1085. struct f_uac2_opts *opts;
  1086. u16 w_length = le16_to_cpu(cr->wLength);
  1087. u16 w_index = le16_to_cpu(cr->wIndex);
  1088. u16 w_value = le16_to_cpu(cr->wValue);
  1089. u8 entity_id = (w_index >> 8) & 0xff;
  1090. u8 control_selector = w_value >> 8;
  1091. struct cntrl_range_lay3 r;
  1092. int value = -EOPNOTSUPP;
  1093. int p_srate, c_srate;
  1094. opts = agdev_to_uac2_opts(agdev);
  1095. p_srate = opts->p_srate;
  1096. c_srate = opts->c_srate;
  1097. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1098. if (entity_id == USB_IN_CLK_ID)
  1099. r.dMIN = p_srate;
  1100. else if (entity_id == USB_OUT_CLK_ID)
  1101. r.dMIN = c_srate;
  1102. else
  1103. return -EOPNOTSUPP;
  1104. r.dMAX = r.dMIN;
  1105. r.dRES = 0;
  1106. r.wNumSubRanges = 1;
  1107. value = min_t(unsigned, w_length, sizeof r);
  1108. memcpy(req->buf, &r, value);
  1109. } else {
  1110. dev_err(&uac2->pdev.dev,
  1111. "%s:%d control_selector=%d TODO!\n",
  1112. __func__, __LINE__, control_selector);
  1113. }
  1114. return value;
  1115. }
  1116. static int
  1117. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1118. {
  1119. if (cr->bRequest == UAC2_CS_CUR)
  1120. return in_rq_cur(fn, cr);
  1121. else if (cr->bRequest == UAC2_CS_RANGE)
  1122. return in_rq_range(fn, cr);
  1123. else
  1124. return -EOPNOTSUPP;
  1125. }
  1126. static int
  1127. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1128. {
  1129. u16 w_length = le16_to_cpu(cr->wLength);
  1130. u16 w_value = le16_to_cpu(cr->wValue);
  1131. u8 control_selector = w_value >> 8;
  1132. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  1133. return w_length;
  1134. return -EOPNOTSUPP;
  1135. }
  1136. static int
  1137. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1138. {
  1139. struct audio_dev *agdev = func_to_agdev(fn);
  1140. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1141. u16 w_index = le16_to_cpu(cr->wIndex);
  1142. u8 intf = w_index & 0xff;
  1143. if (intf != agdev->ac_intf) {
  1144. dev_err(&uac2->pdev.dev,
  1145. "%s:%d Error!\n", __func__, __LINE__);
  1146. return -EOPNOTSUPP;
  1147. }
  1148. if (cr->bRequestType & USB_DIR_IN)
  1149. return ac_rq_in(fn, cr);
  1150. else if (cr->bRequest == UAC2_CS_CUR)
  1151. return out_rq_cur(fn, cr);
  1152. return -EOPNOTSUPP;
  1153. }
  1154. static int
  1155. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1156. {
  1157. struct usb_composite_dev *cdev = fn->config->cdev;
  1158. struct audio_dev *agdev = func_to_agdev(fn);
  1159. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1160. struct usb_request *req = cdev->req;
  1161. u16 w_length = le16_to_cpu(cr->wLength);
  1162. int value = -EOPNOTSUPP;
  1163. /* Only Class specific requests are supposed to reach here */
  1164. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  1165. return -EOPNOTSUPP;
  1166. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  1167. value = setup_rq_inf(fn, cr);
  1168. else
  1169. dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
  1170. if (value >= 0) {
  1171. req->length = value;
  1172. req->zero = value < w_length;
  1173. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1174. if (value < 0) {
  1175. dev_err(&uac2->pdev.dev,
  1176. "%s:%d Error!\n", __func__, __LINE__);
  1177. req->status = 0;
  1178. }
  1179. }
  1180. return value;
  1181. }
  1182. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  1183. {
  1184. return container_of(to_config_group(item), struct f_uac2_opts,
  1185. func_inst.group);
  1186. }
  1187. static void f_uac2_attr_release(struct config_item *item)
  1188. {
  1189. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  1190. usb_put_function_instance(&opts->func_inst);
  1191. }
  1192. static struct configfs_item_operations f_uac2_item_ops = {
  1193. .release = f_uac2_attr_release,
  1194. };
  1195. #define UAC2_ATTRIBUTE(name) \
  1196. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1197. char *page) \
  1198. { \
  1199. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1200. int result; \
  1201. \
  1202. mutex_lock(&opts->lock); \
  1203. result = sprintf(page, "%u\n", opts->name); \
  1204. mutex_unlock(&opts->lock); \
  1205. \
  1206. return result; \
  1207. } \
  1208. \
  1209. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1210. const char *page, size_t len) \
  1211. { \
  1212. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1213. int ret; \
  1214. u32 num; \
  1215. \
  1216. mutex_lock(&opts->lock); \
  1217. if (opts->refcnt) { \
  1218. ret = -EBUSY; \
  1219. goto end; \
  1220. } \
  1221. \
  1222. ret = kstrtou32(page, 0, &num); \
  1223. if (ret) \
  1224. goto end; \
  1225. \
  1226. opts->name = num; \
  1227. ret = len; \
  1228. \
  1229. end: \
  1230. mutex_unlock(&opts->lock); \
  1231. return ret; \
  1232. } \
  1233. \
  1234. CONFIGFS_ATTR(f_uac2_opts_, name)
  1235. UAC2_ATTRIBUTE(p_chmask);
  1236. UAC2_ATTRIBUTE(p_srate);
  1237. UAC2_ATTRIBUTE(p_ssize);
  1238. UAC2_ATTRIBUTE(c_chmask);
  1239. UAC2_ATTRIBUTE(c_srate);
  1240. UAC2_ATTRIBUTE(c_ssize);
  1241. static struct configfs_attribute *f_uac2_attrs[] = {
  1242. &f_uac2_opts_attr_p_chmask,
  1243. &f_uac2_opts_attr_p_srate,
  1244. &f_uac2_opts_attr_p_ssize,
  1245. &f_uac2_opts_attr_c_chmask,
  1246. &f_uac2_opts_attr_c_srate,
  1247. &f_uac2_opts_attr_c_ssize,
  1248. NULL,
  1249. };
  1250. static struct config_item_type f_uac2_func_type = {
  1251. .ct_item_ops = &f_uac2_item_ops,
  1252. .ct_attrs = f_uac2_attrs,
  1253. .ct_owner = THIS_MODULE,
  1254. };
  1255. static void afunc_free_inst(struct usb_function_instance *f)
  1256. {
  1257. struct f_uac2_opts *opts;
  1258. opts = container_of(f, struct f_uac2_opts, func_inst);
  1259. kfree(opts);
  1260. }
  1261. static struct usb_function_instance *afunc_alloc_inst(void)
  1262. {
  1263. struct f_uac2_opts *opts;
  1264. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1265. if (!opts)
  1266. return ERR_PTR(-ENOMEM);
  1267. mutex_init(&opts->lock);
  1268. opts->func_inst.free_func_inst = afunc_free_inst;
  1269. config_group_init_type_name(&opts->func_inst.group, "",
  1270. &f_uac2_func_type);
  1271. opts->p_chmask = UAC2_DEF_PCHMASK;
  1272. opts->p_srate = UAC2_DEF_PSRATE;
  1273. opts->p_ssize = UAC2_DEF_PSSIZE;
  1274. opts->c_chmask = UAC2_DEF_CCHMASK;
  1275. opts->c_srate = UAC2_DEF_CSRATE;
  1276. opts->c_ssize = UAC2_DEF_CSSIZE;
  1277. return &opts->func_inst;
  1278. }
  1279. static void afunc_free(struct usb_function *f)
  1280. {
  1281. struct audio_dev *agdev;
  1282. struct f_uac2_opts *opts;
  1283. agdev = func_to_agdev(f);
  1284. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  1285. kfree(agdev);
  1286. mutex_lock(&opts->lock);
  1287. --opts->refcnt;
  1288. mutex_unlock(&opts->lock);
  1289. }
  1290. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  1291. {
  1292. struct audio_dev *agdev = func_to_agdev(f);
  1293. struct uac2_rtd_params *prm;
  1294. alsa_uac2_exit(agdev);
  1295. prm = &agdev->uac2.p_prm;
  1296. kfree(prm->rbuf);
  1297. prm = &agdev->uac2.c_prm;
  1298. kfree(prm->rbuf);
  1299. usb_free_all_descriptors(f);
  1300. }
  1301. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  1302. {
  1303. struct audio_dev *agdev;
  1304. struct f_uac2_opts *opts;
  1305. agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
  1306. if (agdev == NULL)
  1307. return ERR_PTR(-ENOMEM);
  1308. opts = container_of(fi, struct f_uac2_opts, func_inst);
  1309. mutex_lock(&opts->lock);
  1310. ++opts->refcnt;
  1311. mutex_unlock(&opts->lock);
  1312. agdev->func.name = "uac2_func";
  1313. agdev->func.bind = afunc_bind;
  1314. agdev->func.unbind = afunc_unbind;
  1315. agdev->func.set_alt = afunc_set_alt;
  1316. agdev->func.get_alt = afunc_get_alt;
  1317. agdev->func.disable = afunc_disable;
  1318. agdev->func.setup = afunc_setup;
  1319. agdev->func.free_func = afunc_free;
  1320. return &agdev->func;
  1321. }
  1322. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  1323. MODULE_LICENSE("GPL");
  1324. MODULE_AUTHOR("Yadwinder Singh");
  1325. MODULE_AUTHOR("Jaswinder Singh");