f_uac2.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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. /*
  22. * The driver implements a simple UAC_2 topology.
  23. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
  24. * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
  25. * Capture and Playback sampling rates are independently
  26. * controlled by two clock sources :
  27. * CLK_5 := c_srate, and CLK_6 := p_srate
  28. */
  29. #define USB_OUT_IT_ID 1
  30. #define IO_IN_IT_ID 2
  31. #define IO_OUT_OT_ID 3
  32. #define USB_IN_OT_ID 4
  33. #define USB_OUT_CLK_ID 5
  34. #define USB_IN_CLK_ID 6
  35. #define CONTROL_ABSENT 0
  36. #define CONTROL_RDONLY 1
  37. #define CONTROL_RDWR 3
  38. #define CLK_FREQ_CTRL 0
  39. #define CLK_VLD_CTRL 2
  40. #define COPY_CTRL 0
  41. #define CONN_CTRL 2
  42. #define OVRLD_CTRL 4
  43. #define CLSTR_CTRL 6
  44. #define UNFLW_CTRL 8
  45. #define OVFLW_CTRL 10
  46. static const char *uac2_name = "snd_uac2";
  47. struct uac2_req {
  48. struct uac2_rtd_params *pp; /* parent param */
  49. struct usb_request *req;
  50. };
  51. struct uac2_rtd_params {
  52. struct snd_uac2_chip *uac2; /* parent chip */
  53. bool ep_enabled; /* if the ep is enabled */
  54. /* Size of the ring buffer */
  55. size_t dma_bytes;
  56. unsigned char *dma_area;
  57. struct snd_pcm_substream *ss;
  58. /* Ring buffer */
  59. ssize_t hw_ptr;
  60. void *rbuf;
  61. size_t period_size;
  62. unsigned max_psize;
  63. struct uac2_req *ureq;
  64. spinlock_t lock;
  65. };
  66. struct snd_uac2_chip {
  67. struct platform_device pdev;
  68. struct platform_driver pdrv;
  69. struct uac2_rtd_params p_prm;
  70. struct uac2_rtd_params c_prm;
  71. struct snd_card *card;
  72. struct snd_pcm *pcm;
  73. /* timekeeping for the playback endpoint */
  74. unsigned int p_interval;
  75. unsigned int p_residue;
  76. /* pre-calculated values for playback iso completion */
  77. unsigned int p_pktsize;
  78. unsigned int p_pktsize_residue;
  79. unsigned int p_framesize;
  80. };
  81. #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
  82. #define PRD_SIZE_MAX PAGE_SIZE
  83. #define MIN_PERIODS 4
  84. static struct snd_pcm_hardware uac2_pcm_hardware = {
  85. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
  86. | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
  87. | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  88. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  89. .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
  90. .buffer_bytes_max = BUFF_SIZE_MAX,
  91. .period_bytes_max = PRD_SIZE_MAX,
  92. .periods_min = MIN_PERIODS,
  93. };
  94. struct audio_dev {
  95. u8 ac_intf, ac_alt;
  96. u8 as_out_intf, as_out_alt;
  97. u8 as_in_intf, as_in_alt;
  98. struct usb_ep *in_ep, *out_ep;
  99. struct usb_function func;
  100. /* The ALSA Sound Card it represents on the USB-Client side */
  101. struct snd_uac2_chip uac2;
  102. };
  103. static inline
  104. struct audio_dev *func_to_agdev(struct usb_function *f)
  105. {
  106. return container_of(f, struct audio_dev, func);
  107. }
  108. static inline
  109. struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
  110. {
  111. return container_of(u, struct audio_dev, uac2);
  112. }
  113. static inline
  114. struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
  115. {
  116. return container_of(p, struct snd_uac2_chip, pdev);
  117. }
  118. static inline
  119. struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev)
  120. {
  121. return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
  122. }
  123. static inline
  124. uint num_channels(uint chanmask)
  125. {
  126. uint num = 0;
  127. while (chanmask) {
  128. num += (chanmask & 1);
  129. chanmask >>= 1;
  130. }
  131. return num;
  132. }
  133. static void
  134. agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
  135. {
  136. unsigned pending;
  137. unsigned long flags;
  138. unsigned int hw_ptr;
  139. bool update_alsa = false;
  140. int status = req->status;
  141. struct uac2_req *ur = req->context;
  142. struct snd_pcm_substream *substream;
  143. struct uac2_rtd_params *prm = ur->pp;
  144. struct snd_uac2_chip *uac2 = prm->uac2;
  145. /* i/f shutting down */
  146. if (!prm->ep_enabled || req->status == -ESHUTDOWN)
  147. return;
  148. /*
  149. * We can't really do much about bad xfers.
  150. * Afterall, the ISOCH xfers could fail legitimately.
  151. */
  152. if (status)
  153. pr_debug("%s: iso_complete status(%d) %d/%d\n",
  154. __func__, status, req->actual, req->length);
  155. substream = prm->ss;
  156. /* Do nothing if ALSA isn't active */
  157. if (!substream)
  158. goto exit;
  159. spin_lock_irqsave(&prm->lock, flags);
  160. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  161. /*
  162. * For each IN packet, take the quotient of the current data
  163. * rate and the endpoint's interval as the base packet size.
  164. * If there is a residue from this division, add it to the
  165. * residue accumulator.
  166. */
  167. req->length = uac2->p_pktsize;
  168. uac2->p_residue += uac2->p_pktsize_residue;
  169. /*
  170. * Whenever there are more bytes in the accumulator than we
  171. * need to add one more sample frame, increase this packet's
  172. * size and decrease the accumulator.
  173. */
  174. if (uac2->p_residue / uac2->p_interval >= uac2->p_framesize) {
  175. req->length += uac2->p_framesize;
  176. uac2->p_residue -= uac2->p_framesize *
  177. uac2->p_interval;
  178. }
  179. req->actual = req->length;
  180. }
  181. pending = prm->hw_ptr % prm->period_size;
  182. pending += req->actual;
  183. if (pending >= prm->period_size)
  184. update_alsa = true;
  185. hw_ptr = prm->hw_ptr;
  186. prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
  187. spin_unlock_irqrestore(&prm->lock, flags);
  188. /* Pack USB load in ALSA ring buffer */
  189. pending = prm->dma_bytes - hw_ptr;
  190. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  191. if (unlikely(pending < req->actual)) {
  192. memcpy(req->buf, prm->dma_area + hw_ptr, pending);
  193. memcpy(req->buf + pending, prm->dma_area,
  194. req->actual - pending);
  195. } else {
  196. memcpy(req->buf, prm->dma_area + hw_ptr, req->actual);
  197. }
  198. } else {
  199. if (unlikely(pending < req->actual)) {
  200. memcpy(prm->dma_area + hw_ptr, req->buf, pending);
  201. memcpy(prm->dma_area, req->buf + pending,
  202. req->actual - pending);
  203. } else {
  204. memcpy(prm->dma_area + hw_ptr, req->buf, req->actual);
  205. }
  206. }
  207. exit:
  208. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  209. dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
  210. if (update_alsa)
  211. snd_pcm_period_elapsed(substream);
  212. return;
  213. }
  214. static int
  215. uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  216. {
  217. struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
  218. struct audio_dev *agdev = uac2_to_agdev(uac2);
  219. struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev);
  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 * uac2_opts->req_number);
  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. struct audio_dev *agdev = uac2_to_agdev(uac2);
  776. struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev);
  777. int i;
  778. if (!prm->ep_enabled)
  779. return;
  780. prm->ep_enabled = false;
  781. for (i = 0; i < uac2_opts->req_number; i++) {
  782. if (prm->ureq[i].req) {
  783. usb_ep_dequeue(ep, prm->ureq[i].req);
  784. usb_ep_free_request(ep, prm->ureq[i].req);
  785. prm->ureq[i].req = NULL;
  786. }
  787. }
  788. if (usb_ep_disable(ep))
  789. dev_err(&uac2->pdev.dev,
  790. "%s:%d Error!\n", __func__, __LINE__);
  791. }
  792. static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
  793. struct usb_endpoint_descriptor *ep_desc,
  794. unsigned int factor, bool is_playback)
  795. {
  796. int chmask, srate, ssize;
  797. u16 max_packet_size;
  798. if (is_playback) {
  799. chmask = uac2_opts->p_chmask;
  800. srate = uac2_opts->p_srate;
  801. ssize = uac2_opts->p_ssize;
  802. } else {
  803. chmask = uac2_opts->c_chmask;
  804. srate = uac2_opts->c_srate;
  805. ssize = uac2_opts->c_ssize;
  806. }
  807. max_packet_size = num_channels(chmask) * ssize *
  808. DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
  809. ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
  810. le16_to_cpu(ep_desc->wMaxPacketSize)));
  811. }
  812. static int
  813. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  814. {
  815. struct audio_dev *agdev = func_to_agdev(fn);
  816. struct snd_uac2_chip *uac2 = &agdev->uac2;
  817. struct usb_composite_dev *cdev = cfg->cdev;
  818. struct usb_gadget *gadget = cdev->gadget;
  819. struct device *dev = &uac2->pdev.dev;
  820. struct uac2_rtd_params *prm;
  821. struct f_uac2_opts *uac2_opts;
  822. struct usb_string *us;
  823. int ret;
  824. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  825. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  826. if (IS_ERR(us))
  827. return PTR_ERR(us);
  828. iad_desc.iFunction = us[STR_ASSOC].id;
  829. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  830. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  831. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  832. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  833. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  834. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  835. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  836. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  837. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  838. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  839. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  840. /* Initialize the configurable parameters */
  841. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  842. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  843. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  844. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  845. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  846. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  847. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  848. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  849. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  850. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  851. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  852. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  853. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  854. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  855. ret = usb_interface_id(cfg, fn);
  856. if (ret < 0) {
  857. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  858. return ret;
  859. }
  860. std_ac_if_desc.bInterfaceNumber = ret;
  861. agdev->ac_intf = ret;
  862. agdev->ac_alt = 0;
  863. ret = usb_interface_id(cfg, fn);
  864. if (ret < 0) {
  865. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  866. return ret;
  867. }
  868. std_as_out_if0_desc.bInterfaceNumber = ret;
  869. std_as_out_if1_desc.bInterfaceNumber = ret;
  870. agdev->as_out_intf = ret;
  871. agdev->as_out_alt = 0;
  872. ret = usb_interface_id(cfg, fn);
  873. if (ret < 0) {
  874. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  875. return ret;
  876. }
  877. std_as_in_if0_desc.bInterfaceNumber = ret;
  878. std_as_in_if1_desc.bInterfaceNumber = ret;
  879. agdev->as_in_intf = ret;
  880. agdev->as_in_alt = 0;
  881. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  882. if (!agdev->out_ep) {
  883. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  884. return ret;
  885. }
  886. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  887. if (!agdev->in_ep) {
  888. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  889. return ret;
  890. }
  891. uac2->p_prm.uac2 = uac2;
  892. uac2->c_prm.uac2 = uac2;
  893. /* Calculate wMaxPacketSize according to audio bandwidth */
  894. set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
  895. set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
  896. set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
  897. set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
  898. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  899. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  900. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
  901. NULL);
  902. if (ret)
  903. return ret;
  904. prm = &agdev->uac2.c_prm;
  905. prm->max_psize = hs_epout_desc.wMaxPacketSize;
  906. prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req),
  907. GFP_KERNEL);
  908. if (!prm->ureq) {
  909. ret = -ENOMEM;
  910. goto err_free_descs;
  911. }
  912. prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL);
  913. if (!prm->rbuf) {
  914. prm->max_psize = 0;
  915. ret = -ENOMEM;
  916. goto err_free_descs;
  917. }
  918. prm = &agdev->uac2.p_prm;
  919. prm->max_psize = hs_epin_desc.wMaxPacketSize;
  920. prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req),
  921. GFP_KERNEL);
  922. if (!prm->ureq) {
  923. ret = -ENOMEM;
  924. goto err_free_descs;
  925. }
  926. prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL);
  927. if (!prm->rbuf) {
  928. prm->max_psize = 0;
  929. ret = -ENOMEM;
  930. goto err_no_memory;
  931. }
  932. ret = alsa_uac2_init(agdev);
  933. if (ret)
  934. goto err_no_memory;
  935. return 0;
  936. err_no_memory:
  937. kfree(agdev->uac2.p_prm.ureq);
  938. kfree(agdev->uac2.c_prm.ureq);
  939. kfree(agdev->uac2.p_prm.rbuf);
  940. kfree(agdev->uac2.c_prm.rbuf);
  941. err_free_descs:
  942. usb_free_all_descriptors(fn);
  943. return ret;
  944. }
  945. static int
  946. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  947. {
  948. struct usb_composite_dev *cdev = fn->config->cdev;
  949. struct audio_dev *agdev = func_to_agdev(fn);
  950. struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev);
  951. struct snd_uac2_chip *uac2 = &agdev->uac2;
  952. struct usb_gadget *gadget = cdev->gadget;
  953. struct device *dev = &uac2->pdev.dev;
  954. struct usb_request *req;
  955. struct usb_ep *ep;
  956. struct uac2_rtd_params *prm;
  957. int req_len, i;
  958. /* No i/f has more than 2 alt settings */
  959. if (alt > 1) {
  960. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  961. return -EINVAL;
  962. }
  963. if (intf == agdev->ac_intf) {
  964. /* Control I/f has only 1 AltSetting - 0 */
  965. if (alt) {
  966. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  967. return -EINVAL;
  968. }
  969. return 0;
  970. }
  971. if (intf == agdev->as_out_intf) {
  972. ep = agdev->out_ep;
  973. prm = &uac2->c_prm;
  974. config_ep_by_speed(gadget, fn, ep);
  975. agdev->as_out_alt = alt;
  976. req_len = prm->max_psize;
  977. } else if (intf == agdev->as_in_intf) {
  978. unsigned int factor, rate;
  979. struct usb_endpoint_descriptor *ep_desc;
  980. ep = agdev->in_ep;
  981. prm = &uac2->p_prm;
  982. config_ep_by_speed(gadget, fn, ep);
  983. agdev->as_in_alt = alt;
  984. /* pre-calculate the playback endpoint's interval */
  985. if (gadget->speed == USB_SPEED_FULL) {
  986. ep_desc = &fs_epin_desc;
  987. factor = 1000;
  988. } else {
  989. ep_desc = &hs_epin_desc;
  990. factor = 8000;
  991. }
  992. /* pre-compute some values for iso_complete() */
  993. uac2->p_framesize = opts->p_ssize *
  994. num_channels(opts->p_chmask);
  995. rate = opts->p_srate * uac2->p_framesize;
  996. uac2->p_interval = factor / (1 << (ep_desc->bInterval - 1));
  997. uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval,
  998. prm->max_psize);
  999. if (uac2->p_pktsize < prm->max_psize)
  1000. uac2->p_pktsize_residue = rate % uac2->p_interval;
  1001. else
  1002. uac2->p_pktsize_residue = 0;
  1003. req_len = uac2->p_pktsize;
  1004. uac2->p_residue = 0;
  1005. } else {
  1006. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1007. return -EINVAL;
  1008. }
  1009. if (alt == 0) {
  1010. free_ep(prm, ep);
  1011. return 0;
  1012. }
  1013. prm->ep_enabled = true;
  1014. usb_ep_enable(ep);
  1015. for (i = 0; i < opts->req_number; i++) {
  1016. if (!prm->ureq[i].req) {
  1017. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  1018. if (req == NULL)
  1019. return -ENOMEM;
  1020. prm->ureq[i].req = req;
  1021. prm->ureq[i].pp = prm;
  1022. req->zero = 0;
  1023. req->context = &prm->ureq[i];
  1024. req->length = req_len;
  1025. req->complete = agdev_iso_complete;
  1026. req->buf = prm->rbuf + i * prm->max_psize;
  1027. }
  1028. if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
  1029. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1030. }
  1031. return 0;
  1032. }
  1033. static int
  1034. afunc_get_alt(struct usb_function *fn, unsigned intf)
  1035. {
  1036. struct audio_dev *agdev = func_to_agdev(fn);
  1037. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1038. if (intf == agdev->ac_intf)
  1039. return agdev->ac_alt;
  1040. else if (intf == agdev->as_out_intf)
  1041. return agdev->as_out_alt;
  1042. else if (intf == agdev->as_in_intf)
  1043. return agdev->as_in_alt;
  1044. else
  1045. dev_err(&uac2->pdev.dev,
  1046. "%s:%d Invalid Interface %d!\n",
  1047. __func__, __LINE__, intf);
  1048. return -EINVAL;
  1049. }
  1050. static void
  1051. afunc_disable(struct usb_function *fn)
  1052. {
  1053. struct audio_dev *agdev = func_to_agdev(fn);
  1054. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1055. free_ep(&uac2->p_prm, agdev->in_ep);
  1056. agdev->as_in_alt = 0;
  1057. free_ep(&uac2->c_prm, agdev->out_ep);
  1058. agdev->as_out_alt = 0;
  1059. }
  1060. static int
  1061. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1062. {
  1063. struct usb_request *req = fn->config->cdev->req;
  1064. struct audio_dev *agdev = func_to_agdev(fn);
  1065. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1066. struct f_uac2_opts *opts;
  1067. u16 w_length = le16_to_cpu(cr->wLength);
  1068. u16 w_index = le16_to_cpu(cr->wIndex);
  1069. u16 w_value = le16_to_cpu(cr->wValue);
  1070. u8 entity_id = (w_index >> 8) & 0xff;
  1071. u8 control_selector = w_value >> 8;
  1072. int value = -EOPNOTSUPP;
  1073. int p_srate, c_srate;
  1074. opts = agdev_to_uac2_opts(agdev);
  1075. p_srate = opts->p_srate;
  1076. c_srate = opts->c_srate;
  1077. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1078. struct cntrl_cur_lay3 c;
  1079. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  1080. if (entity_id == USB_IN_CLK_ID)
  1081. c.dCUR = p_srate;
  1082. else if (entity_id == USB_OUT_CLK_ID)
  1083. c.dCUR = c_srate;
  1084. value = min_t(unsigned, w_length, sizeof c);
  1085. memcpy(req->buf, &c, value);
  1086. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  1087. *(u8 *)req->buf = 1;
  1088. value = min_t(unsigned, w_length, 1);
  1089. } else {
  1090. dev_err(&uac2->pdev.dev,
  1091. "%s:%d control_selector=%d TODO!\n",
  1092. __func__, __LINE__, control_selector);
  1093. }
  1094. return value;
  1095. }
  1096. static int
  1097. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1098. {
  1099. struct usb_request *req = fn->config->cdev->req;
  1100. struct audio_dev *agdev = func_to_agdev(fn);
  1101. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1102. struct f_uac2_opts *opts;
  1103. u16 w_length = le16_to_cpu(cr->wLength);
  1104. u16 w_index = le16_to_cpu(cr->wIndex);
  1105. u16 w_value = le16_to_cpu(cr->wValue);
  1106. u8 entity_id = (w_index >> 8) & 0xff;
  1107. u8 control_selector = w_value >> 8;
  1108. struct cntrl_range_lay3 r;
  1109. int value = -EOPNOTSUPP;
  1110. int p_srate, c_srate;
  1111. opts = agdev_to_uac2_opts(agdev);
  1112. p_srate = opts->p_srate;
  1113. c_srate = opts->c_srate;
  1114. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1115. if (entity_id == USB_IN_CLK_ID)
  1116. r.dMIN = p_srate;
  1117. else if (entity_id == USB_OUT_CLK_ID)
  1118. r.dMIN = c_srate;
  1119. else
  1120. return -EOPNOTSUPP;
  1121. r.dMAX = r.dMIN;
  1122. r.dRES = 0;
  1123. r.wNumSubRanges = 1;
  1124. value = min_t(unsigned, w_length, sizeof r);
  1125. memcpy(req->buf, &r, value);
  1126. } else {
  1127. dev_err(&uac2->pdev.dev,
  1128. "%s:%d control_selector=%d TODO!\n",
  1129. __func__, __LINE__, control_selector);
  1130. }
  1131. return value;
  1132. }
  1133. static int
  1134. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1135. {
  1136. if (cr->bRequest == UAC2_CS_CUR)
  1137. return in_rq_cur(fn, cr);
  1138. else if (cr->bRequest == UAC2_CS_RANGE)
  1139. return in_rq_range(fn, cr);
  1140. else
  1141. return -EOPNOTSUPP;
  1142. }
  1143. static int
  1144. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1145. {
  1146. u16 w_length = le16_to_cpu(cr->wLength);
  1147. u16 w_value = le16_to_cpu(cr->wValue);
  1148. u8 control_selector = w_value >> 8;
  1149. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  1150. return w_length;
  1151. return -EOPNOTSUPP;
  1152. }
  1153. static int
  1154. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1155. {
  1156. struct audio_dev *agdev = func_to_agdev(fn);
  1157. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1158. u16 w_index = le16_to_cpu(cr->wIndex);
  1159. u8 intf = w_index & 0xff;
  1160. if (intf != agdev->ac_intf) {
  1161. dev_err(&uac2->pdev.dev,
  1162. "%s:%d Error!\n", __func__, __LINE__);
  1163. return -EOPNOTSUPP;
  1164. }
  1165. if (cr->bRequestType & USB_DIR_IN)
  1166. return ac_rq_in(fn, cr);
  1167. else if (cr->bRequest == UAC2_CS_CUR)
  1168. return out_rq_cur(fn, cr);
  1169. return -EOPNOTSUPP;
  1170. }
  1171. static int
  1172. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1173. {
  1174. struct usb_composite_dev *cdev = fn->config->cdev;
  1175. struct audio_dev *agdev = func_to_agdev(fn);
  1176. struct snd_uac2_chip *uac2 = &agdev->uac2;
  1177. struct usb_request *req = cdev->req;
  1178. u16 w_length = le16_to_cpu(cr->wLength);
  1179. int value = -EOPNOTSUPP;
  1180. /* Only Class specific requests are supposed to reach here */
  1181. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  1182. return -EOPNOTSUPP;
  1183. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  1184. value = setup_rq_inf(fn, cr);
  1185. else
  1186. dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
  1187. if (value >= 0) {
  1188. req->length = value;
  1189. req->zero = value < w_length;
  1190. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1191. if (value < 0) {
  1192. dev_err(&uac2->pdev.dev,
  1193. "%s:%d Error!\n", __func__, __LINE__);
  1194. req->status = 0;
  1195. }
  1196. }
  1197. return value;
  1198. }
  1199. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  1200. {
  1201. return container_of(to_config_group(item), struct f_uac2_opts,
  1202. func_inst.group);
  1203. }
  1204. static void f_uac2_attr_release(struct config_item *item)
  1205. {
  1206. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  1207. usb_put_function_instance(&opts->func_inst);
  1208. }
  1209. static struct configfs_item_operations f_uac2_item_ops = {
  1210. .release = f_uac2_attr_release,
  1211. };
  1212. #define UAC2_ATTRIBUTE(name) \
  1213. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1214. char *page) \
  1215. { \
  1216. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1217. int result; \
  1218. \
  1219. mutex_lock(&opts->lock); \
  1220. result = sprintf(page, "%u\n", opts->name); \
  1221. mutex_unlock(&opts->lock); \
  1222. \
  1223. return result; \
  1224. } \
  1225. \
  1226. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1227. const char *page, size_t len) \
  1228. { \
  1229. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1230. int ret; \
  1231. u32 num; \
  1232. \
  1233. mutex_lock(&opts->lock); \
  1234. if (opts->refcnt) { \
  1235. ret = -EBUSY; \
  1236. goto end; \
  1237. } \
  1238. \
  1239. ret = kstrtou32(page, 0, &num); \
  1240. if (ret) \
  1241. goto end; \
  1242. \
  1243. opts->name = num; \
  1244. ret = len; \
  1245. \
  1246. end: \
  1247. mutex_unlock(&opts->lock); \
  1248. return ret; \
  1249. } \
  1250. \
  1251. CONFIGFS_ATTR(f_uac2_opts_, name)
  1252. UAC2_ATTRIBUTE(p_chmask);
  1253. UAC2_ATTRIBUTE(p_srate);
  1254. UAC2_ATTRIBUTE(p_ssize);
  1255. UAC2_ATTRIBUTE(c_chmask);
  1256. UAC2_ATTRIBUTE(c_srate);
  1257. UAC2_ATTRIBUTE(c_ssize);
  1258. UAC2_ATTRIBUTE(req_number);
  1259. static struct configfs_attribute *f_uac2_attrs[] = {
  1260. &f_uac2_opts_attr_p_chmask,
  1261. &f_uac2_opts_attr_p_srate,
  1262. &f_uac2_opts_attr_p_ssize,
  1263. &f_uac2_opts_attr_c_chmask,
  1264. &f_uac2_opts_attr_c_srate,
  1265. &f_uac2_opts_attr_c_ssize,
  1266. &f_uac2_opts_attr_req_number,
  1267. NULL,
  1268. };
  1269. static struct config_item_type f_uac2_func_type = {
  1270. .ct_item_ops = &f_uac2_item_ops,
  1271. .ct_attrs = f_uac2_attrs,
  1272. .ct_owner = THIS_MODULE,
  1273. };
  1274. static void afunc_free_inst(struct usb_function_instance *f)
  1275. {
  1276. struct f_uac2_opts *opts;
  1277. opts = container_of(f, struct f_uac2_opts, func_inst);
  1278. kfree(opts);
  1279. }
  1280. static struct usb_function_instance *afunc_alloc_inst(void)
  1281. {
  1282. struct f_uac2_opts *opts;
  1283. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1284. if (!opts)
  1285. return ERR_PTR(-ENOMEM);
  1286. mutex_init(&opts->lock);
  1287. opts->func_inst.free_func_inst = afunc_free_inst;
  1288. config_group_init_type_name(&opts->func_inst.group, "",
  1289. &f_uac2_func_type);
  1290. opts->p_chmask = UAC2_DEF_PCHMASK;
  1291. opts->p_srate = UAC2_DEF_PSRATE;
  1292. opts->p_ssize = UAC2_DEF_PSSIZE;
  1293. opts->c_chmask = UAC2_DEF_CCHMASK;
  1294. opts->c_srate = UAC2_DEF_CSRATE;
  1295. opts->c_ssize = UAC2_DEF_CSSIZE;
  1296. opts->req_number = UAC2_DEF_REQ_NUM;
  1297. return &opts->func_inst;
  1298. }
  1299. static void afunc_free(struct usb_function *f)
  1300. {
  1301. struct audio_dev *agdev;
  1302. struct f_uac2_opts *opts;
  1303. agdev = func_to_agdev(f);
  1304. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  1305. kfree(agdev);
  1306. mutex_lock(&opts->lock);
  1307. --opts->refcnt;
  1308. mutex_unlock(&opts->lock);
  1309. }
  1310. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  1311. {
  1312. struct audio_dev *agdev = func_to_agdev(f);
  1313. struct uac2_rtd_params *prm;
  1314. alsa_uac2_exit(agdev);
  1315. prm = &agdev->uac2.p_prm;
  1316. kfree(prm->rbuf);
  1317. prm = &agdev->uac2.c_prm;
  1318. kfree(prm->rbuf);
  1319. kfree(prm->ureq);
  1320. usb_free_all_descriptors(f);
  1321. }
  1322. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  1323. {
  1324. struct audio_dev *agdev;
  1325. struct f_uac2_opts *opts;
  1326. agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
  1327. if (agdev == NULL)
  1328. return ERR_PTR(-ENOMEM);
  1329. opts = container_of(fi, struct f_uac2_opts, func_inst);
  1330. mutex_lock(&opts->lock);
  1331. ++opts->refcnt;
  1332. mutex_unlock(&opts->lock);
  1333. agdev->func.name = "uac2_func";
  1334. agdev->func.bind = afunc_bind;
  1335. agdev->func.unbind = afunc_unbind;
  1336. agdev->func.set_alt = afunc_set_alt;
  1337. agdev->func.get_alt = afunc_get_alt;
  1338. agdev->func.disable = afunc_disable;
  1339. agdev->func.setup = afunc_setup;
  1340. agdev->func.free_func = afunc_free;
  1341. return &agdev->func;
  1342. }
  1343. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  1344. MODULE_LICENSE("GPL");
  1345. MODULE_AUTHOR("Yadwinder Singh");
  1346. MODULE_AUTHOR("Jaswinder Singh");