u_audio.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
  4. *
  5. * Copyright (C) 2016
  6. * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
  7. *
  8. * Sound card implementation was cut-and-pasted with changes
  9. * from f_uac2.c and has:
  10. * Copyright (C) 2011
  11. * Yadwinder Singh (yadi.brar01@gmail.com)
  12. * Jaswinder Singh (jaswinder.singh@linaro.org)
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/control.h>
  20. #include <sound/tlv.h>
  21. #include <linux/usb/audio.h>
  22. #include "u_audio.h"
  23. #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
  24. #define PRD_SIZE_MAX PAGE_SIZE
  25. #define MIN_PERIODS 4
  26. enum {
  27. UAC_FBACK_CTRL,
  28. UAC_P_PITCH_CTRL,
  29. UAC_MUTE_CTRL,
  30. UAC_VOLUME_CTRL,
  31. UAC_RATE_CTRL,
  32. };
  33. /* Runtime data params for one stream */
  34. struct uac_rtd_params {
  35. struct snd_uac_chip *uac; /* parent chip */
  36. bool ep_enabled; /* if the ep is enabled */
  37. struct snd_pcm_substream *ss;
  38. /* Ring buffer */
  39. ssize_t hw_ptr;
  40. void *rbuf;
  41. unsigned int pitch; /* Stream pitch ratio to 1000000 */
  42. unsigned int max_psize; /* MaxPacketSize of endpoint */
  43. struct usb_request **reqs;
  44. struct usb_request *req_fback; /* Feedback endpoint request */
  45. bool fb_ep_enabled; /* if the ep is enabled */
  46. /* Volume/Mute controls and their state */
  47. int fu_id; /* Feature Unit ID */
  48. struct snd_ctl_elem_id snd_kctl_volume_id;
  49. struct snd_ctl_elem_id snd_kctl_mute_id;
  50. s16 volume_min, volume_max, volume_res;
  51. s16 volume;
  52. int mute;
  53. struct snd_ctl_elem_id snd_kctl_rate_id; /* read-only current rate */
  54. int srate; /* selected samplerate */
  55. int active; /* playback/capture running */
  56. spinlock_t lock; /* lock for control transfers */
  57. };
  58. struct snd_uac_chip {
  59. struct g_audio *audio_dev;
  60. struct uac_rtd_params p_prm;
  61. struct uac_rtd_params c_prm;
  62. struct snd_card *card;
  63. struct snd_pcm *pcm;
  64. /* pre-calculated values for playback iso completion */
  65. unsigned long long p_residue_mil;
  66. unsigned int p_interval;
  67. unsigned int p_framesize;
  68. };
  69. static const struct snd_pcm_hardware uac_pcm_hardware = {
  70. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
  71. | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
  72. | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  73. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  74. .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
  75. .buffer_bytes_max = BUFF_SIZE_MAX,
  76. .period_bytes_max = PRD_SIZE_MAX,
  77. .periods_min = MIN_PERIODS,
  78. };
  79. static void u_audio_set_fback_frequency(enum usb_device_speed speed,
  80. struct usb_ep *out_ep,
  81. unsigned long long freq,
  82. unsigned int pitch,
  83. void *buf)
  84. {
  85. u32 ff = 0;
  86. const struct usb_endpoint_descriptor *ep_desc;
  87. /*
  88. * Because the pitch base is 1000000, the final divider here
  89. * will be 1000 * 1000000 = 1953125 << 9
  90. *
  91. * Instead of dealing with big numbers lets fold this 9 left shift
  92. */
  93. if (speed == USB_SPEED_FULL) {
  94. /*
  95. * Full-speed feedback endpoints report frequency
  96. * in samples/frame
  97. * Format is encoded in Q10.10 left-justified in the 24 bits,
  98. * so that it has a Q10.14 format.
  99. *
  100. * ff = (freq << 14) / 1000
  101. */
  102. freq <<= 5;
  103. } else {
  104. /*
  105. * High-speed feedback endpoints report frequency
  106. * in samples/microframe.
  107. * Format is encoded in Q12.13 fitted into four bytes so that
  108. * the binary point is located between the second and the third
  109. * byte fromat (that is Q16.16)
  110. *
  111. * ff = (freq << 16) / 8000
  112. *
  113. * Win10 and OSX UAC2 drivers require number of samples per packet
  114. * in order to honor the feedback value.
  115. * Linux snd-usb-audio detects the applied bit-shift automatically.
  116. */
  117. ep_desc = out_ep->desc;
  118. freq <<= 4 + (ep_desc->bInterval - 1);
  119. }
  120. ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
  121. *(__le32 *)buf = cpu_to_le32(ff);
  122. }
  123. static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
  124. {
  125. unsigned int pending;
  126. unsigned int hw_ptr;
  127. int status = req->status;
  128. struct snd_pcm_substream *substream;
  129. struct snd_pcm_runtime *runtime;
  130. struct uac_rtd_params *prm = req->context;
  131. struct snd_uac_chip *uac = prm->uac;
  132. unsigned int frames, p_pktsize;
  133. unsigned long long pitched_rate_mil, p_pktsize_residue_mil,
  134. residue_frames_mil, div_result;
  135. /* i/f shutting down */
  136. if (!prm->ep_enabled) {
  137. usb_ep_free_request(ep, req);
  138. return;
  139. }
  140. if (req->status == -ESHUTDOWN)
  141. return;
  142. /*
  143. * We can't really do much about bad xfers.
  144. * Afterall, the ISOCH xfers could fail legitimately.
  145. */
  146. if (status)
  147. pr_debug("%s: iso_complete status(%d) %d/%d\n",
  148. __func__, status, req->actual, req->length);
  149. substream = prm->ss;
  150. /* Do nothing if ALSA isn't active */
  151. if (!substream)
  152. goto exit;
  153. snd_pcm_stream_lock(substream);
  154. runtime = substream->runtime;
  155. if (!runtime || !snd_pcm_running(substream)) {
  156. snd_pcm_stream_unlock(substream);
  157. goto exit;
  158. }
  159. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  160. /*
  161. * For each IN packet, take the quotient of the current data
  162. * rate and the endpoint's interval as the base packet size.
  163. * If there is a residue from this division, add it to the
  164. * residue accumulator.
  165. */
  166. unsigned long long p_interval_mil = uac->p_interval * 1000000ULL;
  167. pitched_rate_mil = (unsigned long long) prm->srate * prm->pitch;
  168. div_result = pitched_rate_mil;
  169. do_div(div_result, uac->p_interval);
  170. do_div(div_result, 1000000);
  171. frames = (unsigned int) div_result;
  172. pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n",
  173. prm->srate, prm->pitch, p_interval_mil, frames);
  174. p_pktsize = min_t(unsigned int,
  175. uac->p_framesize * frames,
  176. ep->maxpacket);
  177. if (p_pktsize < ep->maxpacket) {
  178. residue_frames_mil = pitched_rate_mil - frames * p_interval_mil;
  179. p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil;
  180. } else
  181. p_pktsize_residue_mil = 0;
  182. req->length = p_pktsize;
  183. uac->p_residue_mil += p_pktsize_residue_mil;
  184. /*
  185. * Whenever there are more bytes in the accumulator p_residue_mil than we
  186. * need to add one more sample frame, increase this packet's
  187. * size and decrease the accumulator.
  188. */
  189. div_result = uac->p_residue_mil;
  190. do_div(div_result, uac->p_interval);
  191. do_div(div_result, 1000000);
  192. if ((unsigned int) div_result >= uac->p_framesize) {
  193. req->length += uac->p_framesize;
  194. uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
  195. pr_debug("increased req length to %d\n", req->length);
  196. }
  197. pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
  198. req->actual = req->length;
  199. }
  200. hw_ptr = prm->hw_ptr;
  201. /* Pack USB load in ALSA ring buffer */
  202. pending = runtime->dma_bytes - hw_ptr;
  203. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  204. if (unlikely(pending < req->actual)) {
  205. memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
  206. memcpy(req->buf + pending, runtime->dma_area,
  207. req->actual - pending);
  208. } else {
  209. memcpy(req->buf, runtime->dma_area + hw_ptr,
  210. req->actual);
  211. }
  212. } else {
  213. if (unlikely(pending < req->actual)) {
  214. memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
  215. memcpy(runtime->dma_area, req->buf + pending,
  216. req->actual - pending);
  217. } else {
  218. memcpy(runtime->dma_area + hw_ptr, req->buf,
  219. req->actual);
  220. }
  221. }
  222. /* update hw_ptr after data is copied to memory */
  223. prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
  224. hw_ptr = prm->hw_ptr;
  225. snd_pcm_stream_unlock(substream);
  226. if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
  227. snd_pcm_period_elapsed(substream);
  228. exit:
  229. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  230. dev_err(uac->card->dev, "%d Error!\n", __LINE__);
  231. }
  232. static void u_audio_iso_fback_complete(struct usb_ep *ep,
  233. struct usb_request *req)
  234. {
  235. struct uac_rtd_params *prm = req->context;
  236. struct snd_uac_chip *uac = prm->uac;
  237. struct g_audio *audio_dev = uac->audio_dev;
  238. int status = req->status;
  239. /* i/f shutting down */
  240. if (!prm->fb_ep_enabled) {
  241. kfree(req->buf);
  242. usb_ep_free_request(ep, req);
  243. return;
  244. }
  245. if (req->status == -ESHUTDOWN)
  246. return;
  247. /*
  248. * We can't really do much about bad xfers.
  249. * Afterall, the ISOCH xfers could fail legitimately.
  250. */
  251. if (status)
  252. pr_debug("%s: iso_complete status(%d) %d/%d\n",
  253. __func__, status, req->actual, req->length);
  254. u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep,
  255. prm->srate, prm->pitch,
  256. req->buf);
  257. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  258. dev_err(uac->card->dev, "%d Error!\n", __LINE__);
  259. }
  260. static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  261. {
  262. struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
  263. struct uac_rtd_params *prm;
  264. struct g_audio *audio_dev;
  265. struct uac_params *params;
  266. int err = 0;
  267. audio_dev = uac->audio_dev;
  268. params = &audio_dev->params;
  269. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  270. prm = &uac->p_prm;
  271. else
  272. prm = &uac->c_prm;
  273. /* Reset */
  274. prm->hw_ptr = 0;
  275. switch (cmd) {
  276. case SNDRV_PCM_TRIGGER_START:
  277. case SNDRV_PCM_TRIGGER_RESUME:
  278. prm->ss = substream;
  279. break;
  280. case SNDRV_PCM_TRIGGER_STOP:
  281. case SNDRV_PCM_TRIGGER_SUSPEND:
  282. prm->ss = NULL;
  283. break;
  284. default:
  285. err = -EINVAL;
  286. }
  287. /* Clear buffer after Play stops */
  288. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
  289. memset(prm->rbuf, 0, prm->max_psize * params->req_number);
  290. return err;
  291. }
  292. static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
  293. {
  294. struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
  295. struct uac_rtd_params *prm;
  296. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  297. prm = &uac->p_prm;
  298. else
  299. prm = &uac->c_prm;
  300. return bytes_to_frames(substream->runtime, prm->hw_ptr);
  301. }
  302. static u64 uac_ssize_to_fmt(int ssize)
  303. {
  304. u64 ret;
  305. switch (ssize) {
  306. case 3:
  307. ret = SNDRV_PCM_FMTBIT_S24_3LE;
  308. break;
  309. case 4:
  310. ret = SNDRV_PCM_FMTBIT_S32_LE;
  311. break;
  312. default:
  313. ret = SNDRV_PCM_FMTBIT_S16_LE;
  314. break;
  315. }
  316. return ret;
  317. }
  318. static int uac_pcm_open(struct snd_pcm_substream *substream)
  319. {
  320. struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
  321. struct snd_pcm_runtime *runtime = substream->runtime;
  322. struct g_audio *audio_dev;
  323. struct uac_params *params;
  324. struct uac_rtd_params *prm;
  325. int p_ssize, c_ssize;
  326. int p_chmask, c_chmask;
  327. audio_dev = uac->audio_dev;
  328. params = &audio_dev->params;
  329. p_ssize = params->p_ssize;
  330. c_ssize = params->c_ssize;
  331. p_chmask = params->p_chmask;
  332. c_chmask = params->c_chmask;
  333. uac->p_residue_mil = 0;
  334. runtime->hw = uac_pcm_hardware;
  335. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  336. runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
  337. runtime->hw.channels_min = num_channels(p_chmask);
  338. prm = &uac->p_prm;
  339. } else {
  340. runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
  341. runtime->hw.channels_min = num_channels(c_chmask);
  342. prm = &uac->c_prm;
  343. }
  344. runtime->hw.period_bytes_min = 2 * prm->max_psize
  345. / runtime->hw.periods_min;
  346. runtime->hw.rate_min = prm->srate;
  347. runtime->hw.rate_max = runtime->hw.rate_min;
  348. runtime->hw.channels_max = runtime->hw.channels_min;
  349. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  350. return 0;
  351. }
  352. /* ALSA cries without these function pointers */
  353. static int uac_pcm_null(struct snd_pcm_substream *substream)
  354. {
  355. return 0;
  356. }
  357. static const struct snd_pcm_ops uac_pcm_ops = {
  358. .open = uac_pcm_open,
  359. .close = uac_pcm_null,
  360. .trigger = uac_pcm_trigger,
  361. .pointer = uac_pcm_pointer,
  362. .prepare = uac_pcm_null,
  363. };
  364. static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
  365. {
  366. struct snd_uac_chip *uac = prm->uac;
  367. struct g_audio *audio_dev;
  368. struct uac_params *params;
  369. int i;
  370. if (!prm->ep_enabled)
  371. return;
  372. audio_dev = uac->audio_dev;
  373. params = &audio_dev->params;
  374. for (i = 0; i < params->req_number; i++) {
  375. if (prm->reqs[i]) {
  376. if (usb_ep_dequeue(ep, prm->reqs[i]))
  377. usb_ep_free_request(ep, prm->reqs[i]);
  378. /*
  379. * If usb_ep_dequeue() cannot successfully dequeue the
  380. * request, the request will be freed by the completion
  381. * callback.
  382. */
  383. prm->reqs[i] = NULL;
  384. }
  385. }
  386. prm->ep_enabled = false;
  387. if (usb_ep_disable(ep))
  388. dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
  389. }
  390. static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
  391. {
  392. struct snd_uac_chip *uac = prm->uac;
  393. if (!prm->fb_ep_enabled)
  394. return;
  395. if (prm->req_fback) {
  396. if (usb_ep_dequeue(ep, prm->req_fback)) {
  397. kfree(prm->req_fback->buf);
  398. usb_ep_free_request(ep, prm->req_fback);
  399. }
  400. prm->req_fback = NULL;
  401. }
  402. prm->fb_ep_enabled = false;
  403. if (usb_ep_disable(ep))
  404. dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
  405. }
  406. static void set_active(struct uac_rtd_params *prm, bool active)
  407. {
  408. // notifying through the Rate ctrl
  409. unsigned long flags;
  410. spin_lock_irqsave(&prm->lock, flags);
  411. if (prm->active != active) {
  412. prm->active = active;
  413. snd_ctl_notify(prm->uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
  414. &prm->snd_kctl_rate_id);
  415. }
  416. spin_unlock_irqrestore(&prm->lock, flags);
  417. }
  418. int u_audio_set_capture_srate(struct g_audio *audio_dev, int srate)
  419. {
  420. struct uac_params *params = &audio_dev->params;
  421. struct snd_uac_chip *uac = audio_dev->uac;
  422. struct uac_rtd_params *prm;
  423. int i;
  424. unsigned long flags;
  425. dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate);
  426. prm = &uac->c_prm;
  427. for (i = 0; i < UAC_MAX_RATES; i++) {
  428. if (params->c_srates[i] == srate) {
  429. spin_lock_irqsave(&prm->lock, flags);
  430. prm->srate = srate;
  431. spin_unlock_irqrestore(&prm->lock, flags);
  432. return 0;
  433. }
  434. if (params->c_srates[i] == 0)
  435. break;
  436. }
  437. return -EINVAL;
  438. }
  439. EXPORT_SYMBOL_GPL(u_audio_set_capture_srate);
  440. int u_audio_get_capture_srate(struct g_audio *audio_dev, u32 *val)
  441. {
  442. struct snd_uac_chip *uac = audio_dev->uac;
  443. struct uac_rtd_params *prm;
  444. unsigned long flags;
  445. prm = &uac->c_prm;
  446. spin_lock_irqsave(&prm->lock, flags);
  447. *val = prm->srate;
  448. spin_unlock_irqrestore(&prm->lock, flags);
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(u_audio_get_capture_srate);
  452. int u_audio_set_playback_srate(struct g_audio *audio_dev, int srate)
  453. {
  454. struct uac_params *params = &audio_dev->params;
  455. struct snd_uac_chip *uac = audio_dev->uac;
  456. struct uac_rtd_params *prm;
  457. int i;
  458. unsigned long flags;
  459. dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate);
  460. prm = &uac->p_prm;
  461. for (i = 0; i < UAC_MAX_RATES; i++) {
  462. if (params->p_srates[i] == srate) {
  463. spin_lock_irqsave(&prm->lock, flags);
  464. prm->srate = srate;
  465. spin_unlock_irqrestore(&prm->lock, flags);
  466. return 0;
  467. }
  468. if (params->p_srates[i] == 0)
  469. break;
  470. }
  471. return -EINVAL;
  472. }
  473. EXPORT_SYMBOL_GPL(u_audio_set_playback_srate);
  474. int u_audio_get_playback_srate(struct g_audio *audio_dev, u32 *val)
  475. {
  476. struct snd_uac_chip *uac = audio_dev->uac;
  477. struct uac_rtd_params *prm;
  478. unsigned long flags;
  479. prm = &uac->p_prm;
  480. spin_lock_irqsave(&prm->lock, flags);
  481. *val = prm->srate;
  482. spin_unlock_irqrestore(&prm->lock, flags);
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(u_audio_get_playback_srate);
  486. int u_audio_start_capture(struct g_audio *audio_dev)
  487. {
  488. struct snd_uac_chip *uac = audio_dev->uac;
  489. struct usb_gadget *gadget = audio_dev->gadget;
  490. struct device *dev = &gadget->dev;
  491. struct usb_request *req, *req_fback;
  492. struct usb_ep *ep, *ep_fback;
  493. struct uac_rtd_params *prm;
  494. struct uac_params *params = &audio_dev->params;
  495. int req_len, i, ret;
  496. prm = &uac->c_prm;
  497. dev_dbg(dev, "start capture with rate %d\n", prm->srate);
  498. ep = audio_dev->out_ep;
  499. ret = config_ep_by_speed(gadget, &audio_dev->func, ep);
  500. if (ret < 0) {
  501. dev_err(dev, "config_ep_by_speed for out_ep failed (%d)\n", ret);
  502. return ret;
  503. }
  504. req_len = ep->maxpacket;
  505. prm->ep_enabled = true;
  506. ret = usb_ep_enable(ep);
  507. if (ret < 0) {
  508. dev_err(dev, "usb_ep_enable failed for out_ep (%d)\n", ret);
  509. return ret;
  510. }
  511. for (i = 0; i < params->req_number; i++) {
  512. if (!prm->reqs[i]) {
  513. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  514. if (req == NULL)
  515. return -ENOMEM;
  516. prm->reqs[i] = req;
  517. req->zero = 0;
  518. req->context = prm;
  519. req->length = req_len;
  520. req->complete = u_audio_iso_complete;
  521. req->buf = prm->rbuf + i * ep->maxpacket;
  522. }
  523. if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
  524. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  525. }
  526. set_active(&uac->c_prm, true);
  527. ep_fback = audio_dev->in_ep_fback;
  528. if (!ep_fback)
  529. return 0;
  530. /* Setup feedback endpoint */
  531. ret = config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
  532. if (ret < 0) {
  533. dev_err(dev, "config_ep_by_speed in_ep_fback failed (%d)\n", ret);
  534. return ret; // TODO: Clean up out_ep
  535. }
  536. prm->fb_ep_enabled = true;
  537. ret = usb_ep_enable(ep_fback);
  538. if (ret < 0) {
  539. dev_err(dev, "usb_ep_enable failed for in_ep_fback (%d)\n", ret);
  540. return ret; // TODO: Clean up out_ep
  541. }
  542. req_len = ep_fback->maxpacket;
  543. req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
  544. if (req_fback == NULL)
  545. return -ENOMEM;
  546. prm->req_fback = req_fback;
  547. req_fback->zero = 0;
  548. req_fback->context = prm;
  549. req_fback->length = req_len;
  550. req_fback->complete = u_audio_iso_fback_complete;
  551. req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
  552. if (!req_fback->buf)
  553. return -ENOMEM;
  554. /*
  555. * Configure the feedback endpoint's reported frequency.
  556. * Always start with original frequency since its deviation can't
  557. * be meauserd at start of playback
  558. */
  559. prm->pitch = 1000000;
  560. u_audio_set_fback_frequency(audio_dev->gadget->speed, ep,
  561. prm->srate, prm->pitch,
  562. req_fback->buf);
  563. if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
  564. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(u_audio_start_capture);
  568. void u_audio_stop_capture(struct g_audio *audio_dev)
  569. {
  570. struct snd_uac_chip *uac = audio_dev->uac;
  571. set_active(&uac->c_prm, false);
  572. if (audio_dev->in_ep_fback)
  573. free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
  574. free_ep(&uac->c_prm, audio_dev->out_ep);
  575. }
  576. EXPORT_SYMBOL_GPL(u_audio_stop_capture);
  577. int u_audio_start_playback(struct g_audio *audio_dev)
  578. {
  579. struct snd_uac_chip *uac = audio_dev->uac;
  580. struct usb_gadget *gadget = audio_dev->gadget;
  581. struct device *dev = &gadget->dev;
  582. struct usb_request *req;
  583. struct usb_ep *ep;
  584. struct uac_rtd_params *prm;
  585. struct uac_params *params = &audio_dev->params;
  586. unsigned int factor;
  587. const struct usb_endpoint_descriptor *ep_desc;
  588. int req_len, i, ret;
  589. unsigned int p_pktsize;
  590. prm = &uac->p_prm;
  591. dev_dbg(dev, "start playback with rate %d\n", prm->srate);
  592. ep = audio_dev->in_ep;
  593. ret = config_ep_by_speed(gadget, &audio_dev->func, ep);
  594. if (ret < 0) {
  595. dev_err(dev, "config_ep_by_speed for in_ep failed (%d)\n", ret);
  596. return ret;
  597. }
  598. ep_desc = ep->desc;
  599. /*
  600. * Always start with original frequency
  601. */
  602. prm->pitch = 1000000;
  603. /* pre-calculate the playback endpoint's interval */
  604. if (gadget->speed == USB_SPEED_FULL)
  605. factor = 1000;
  606. else
  607. factor = 8000;
  608. /* pre-compute some values for iso_complete() */
  609. uac->p_framesize = params->p_ssize *
  610. num_channels(params->p_chmask);
  611. uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
  612. p_pktsize = min_t(unsigned int,
  613. uac->p_framesize *
  614. (prm->srate / uac->p_interval),
  615. ep->maxpacket);
  616. req_len = p_pktsize;
  617. uac->p_residue_mil = 0;
  618. prm->ep_enabled = true;
  619. ret = usb_ep_enable(ep);
  620. if (ret < 0) {
  621. dev_err(dev, "usb_ep_enable failed for in_ep (%d)\n", ret);
  622. return ret;
  623. }
  624. for (i = 0; i < params->req_number; i++) {
  625. if (!prm->reqs[i]) {
  626. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  627. if (req == NULL)
  628. return -ENOMEM;
  629. prm->reqs[i] = req;
  630. req->zero = 0;
  631. req->context = prm;
  632. req->length = req_len;
  633. req->complete = u_audio_iso_complete;
  634. req->buf = prm->rbuf + i * ep->maxpacket;
  635. }
  636. if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
  637. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  638. }
  639. set_active(&uac->p_prm, true);
  640. return 0;
  641. }
  642. EXPORT_SYMBOL_GPL(u_audio_start_playback);
  643. void u_audio_stop_playback(struct g_audio *audio_dev)
  644. {
  645. struct snd_uac_chip *uac = audio_dev->uac;
  646. set_active(&uac->p_prm, false);
  647. free_ep(&uac->p_prm, audio_dev->in_ep);
  648. }
  649. EXPORT_SYMBOL_GPL(u_audio_stop_playback);
  650. void u_audio_suspend(struct g_audio *audio_dev)
  651. {
  652. struct snd_uac_chip *uac = audio_dev->uac;
  653. set_active(&uac->p_prm, false);
  654. set_active(&uac->c_prm, false);
  655. }
  656. EXPORT_SYMBOL_GPL(u_audio_suspend);
  657. int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val)
  658. {
  659. struct snd_uac_chip *uac = audio_dev->uac;
  660. struct uac_rtd_params *prm;
  661. unsigned long flags;
  662. if (playback)
  663. prm = &uac->p_prm;
  664. else
  665. prm = &uac->c_prm;
  666. spin_lock_irqsave(&prm->lock, flags);
  667. *val = prm->volume;
  668. spin_unlock_irqrestore(&prm->lock, flags);
  669. return 0;
  670. }
  671. EXPORT_SYMBOL_GPL(u_audio_get_volume);
  672. int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val)
  673. {
  674. struct snd_uac_chip *uac = audio_dev->uac;
  675. struct uac_rtd_params *prm;
  676. unsigned long flags;
  677. int change = 0;
  678. if (playback)
  679. prm = &uac->p_prm;
  680. else
  681. prm = &uac->c_prm;
  682. spin_lock_irqsave(&prm->lock, flags);
  683. val = clamp(val, prm->volume_min, prm->volume_max);
  684. if (prm->volume != val) {
  685. prm->volume = val;
  686. change = 1;
  687. }
  688. spin_unlock_irqrestore(&prm->lock, flags);
  689. if (change)
  690. snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
  691. &prm->snd_kctl_volume_id);
  692. return 0;
  693. }
  694. EXPORT_SYMBOL_GPL(u_audio_set_volume);
  695. int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val)
  696. {
  697. struct snd_uac_chip *uac = audio_dev->uac;
  698. struct uac_rtd_params *prm;
  699. unsigned long flags;
  700. if (playback)
  701. prm = &uac->p_prm;
  702. else
  703. prm = &uac->c_prm;
  704. spin_lock_irqsave(&prm->lock, flags);
  705. *val = prm->mute;
  706. spin_unlock_irqrestore(&prm->lock, flags);
  707. return 0;
  708. }
  709. EXPORT_SYMBOL_GPL(u_audio_get_mute);
  710. int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val)
  711. {
  712. struct snd_uac_chip *uac = audio_dev->uac;
  713. struct uac_rtd_params *prm;
  714. unsigned long flags;
  715. int change = 0;
  716. int mute;
  717. if (playback)
  718. prm = &uac->p_prm;
  719. else
  720. prm = &uac->c_prm;
  721. mute = val ? 1 : 0;
  722. spin_lock_irqsave(&prm->lock, flags);
  723. if (prm->mute != mute) {
  724. prm->mute = mute;
  725. change = 1;
  726. }
  727. spin_unlock_irqrestore(&prm->lock, flags);
  728. if (change)
  729. snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
  730. &prm->snd_kctl_mute_id);
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(u_audio_set_mute);
  734. static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
  735. struct snd_ctl_elem_info *uinfo)
  736. {
  737. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  738. struct snd_uac_chip *uac = prm->uac;
  739. struct g_audio *audio_dev = uac->audio_dev;
  740. struct uac_params *params = &audio_dev->params;
  741. unsigned int pitch_min, pitch_max;
  742. pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
  743. pitch_max = (1000 + params->fb_max) * 1000;
  744. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  745. uinfo->count = 1;
  746. uinfo->value.integer.min = pitch_min;
  747. uinfo->value.integer.max = pitch_max;
  748. uinfo->value.integer.step = 1;
  749. return 0;
  750. }
  751. static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
  752. struct snd_ctl_elem_value *ucontrol)
  753. {
  754. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  755. ucontrol->value.integer.value[0] = prm->pitch;
  756. return 0;
  757. }
  758. static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
  759. struct snd_ctl_elem_value *ucontrol)
  760. {
  761. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  762. struct snd_uac_chip *uac = prm->uac;
  763. struct g_audio *audio_dev = uac->audio_dev;
  764. struct uac_params *params = &audio_dev->params;
  765. unsigned int val;
  766. unsigned int pitch_min, pitch_max;
  767. int change = 0;
  768. pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
  769. pitch_max = (1000 + params->fb_max) * 1000;
  770. val = ucontrol->value.integer.value[0];
  771. if (val < pitch_min)
  772. val = pitch_min;
  773. if (val > pitch_max)
  774. val = pitch_max;
  775. if (prm->pitch != val) {
  776. prm->pitch = val;
  777. change = 1;
  778. }
  779. return change;
  780. }
  781. static int u_audio_mute_info(struct snd_kcontrol *kcontrol,
  782. struct snd_ctl_elem_info *uinfo)
  783. {
  784. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  785. uinfo->count = 1;
  786. uinfo->value.integer.min = 0;
  787. uinfo->value.integer.max = 1;
  788. uinfo->value.integer.step = 1;
  789. return 0;
  790. }
  791. static int u_audio_mute_get(struct snd_kcontrol *kcontrol,
  792. struct snd_ctl_elem_value *ucontrol)
  793. {
  794. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  795. unsigned long flags;
  796. spin_lock_irqsave(&prm->lock, flags);
  797. ucontrol->value.integer.value[0] = !prm->mute;
  798. spin_unlock_irqrestore(&prm->lock, flags);
  799. return 0;
  800. }
  801. static int u_audio_mute_put(struct snd_kcontrol *kcontrol,
  802. struct snd_ctl_elem_value *ucontrol)
  803. {
  804. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  805. struct snd_uac_chip *uac = prm->uac;
  806. struct g_audio *audio_dev = uac->audio_dev;
  807. unsigned int val;
  808. unsigned long flags;
  809. int change = 0;
  810. val = !ucontrol->value.integer.value[0];
  811. spin_lock_irqsave(&prm->lock, flags);
  812. if (val != prm->mute) {
  813. prm->mute = val;
  814. change = 1;
  815. }
  816. spin_unlock_irqrestore(&prm->lock, flags);
  817. if (change && audio_dev->notify)
  818. audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE);
  819. return change;
  820. }
  821. /*
  822. * TLV callback for mixer volume controls
  823. */
  824. static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  825. unsigned int size, unsigned int __user *_tlv)
  826. {
  827. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  828. DECLARE_TLV_DB_MINMAX(scale, 0, 0);
  829. if (size < sizeof(scale))
  830. return -ENOMEM;
  831. /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */
  832. scale[2] = (prm->volume_min * 100) / 256;
  833. scale[3] = (prm->volume_max * 100) / 256;
  834. if (copy_to_user(_tlv, scale, sizeof(scale)))
  835. return -EFAULT;
  836. return 0;
  837. }
  838. static int u_audio_volume_info(struct snd_kcontrol *kcontrol,
  839. struct snd_ctl_elem_info *uinfo)
  840. {
  841. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  842. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  843. uinfo->count = 1;
  844. uinfo->value.integer.min = 0;
  845. uinfo->value.integer.max =
  846. (prm->volume_max - prm->volume_min + prm->volume_res - 1)
  847. / prm->volume_res;
  848. uinfo->value.integer.step = 1;
  849. return 0;
  850. }
  851. static int u_audio_volume_get(struct snd_kcontrol *kcontrol,
  852. struct snd_ctl_elem_value *ucontrol)
  853. {
  854. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  855. unsigned long flags;
  856. spin_lock_irqsave(&prm->lock, flags);
  857. ucontrol->value.integer.value[0] =
  858. (prm->volume - prm->volume_min) / prm->volume_res;
  859. spin_unlock_irqrestore(&prm->lock, flags);
  860. return 0;
  861. }
  862. static int u_audio_volume_put(struct snd_kcontrol *kcontrol,
  863. struct snd_ctl_elem_value *ucontrol)
  864. {
  865. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  866. struct snd_uac_chip *uac = prm->uac;
  867. struct g_audio *audio_dev = uac->audio_dev;
  868. unsigned int val;
  869. s16 volume;
  870. unsigned long flags;
  871. int change = 0;
  872. val = ucontrol->value.integer.value[0];
  873. spin_lock_irqsave(&prm->lock, flags);
  874. volume = (val * prm->volume_res) + prm->volume_min;
  875. volume = clamp(volume, prm->volume_min, prm->volume_max);
  876. if (volume != prm->volume) {
  877. prm->volume = volume;
  878. change = 1;
  879. }
  880. spin_unlock_irqrestore(&prm->lock, flags);
  881. if (change && audio_dev->notify)
  882. audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME);
  883. return change;
  884. }
  885. static int get_max_srate(const int *srates)
  886. {
  887. int i, max_srate = 0;
  888. for (i = 0; i < UAC_MAX_RATES; i++) {
  889. if (srates[i] == 0)
  890. break;
  891. if (srates[i] > max_srate)
  892. max_srate = srates[i];
  893. }
  894. return max_srate;
  895. }
  896. static int get_min_srate(const int *srates)
  897. {
  898. int i, min_srate = INT_MAX;
  899. for (i = 0; i < UAC_MAX_RATES; i++) {
  900. if (srates[i] == 0)
  901. break;
  902. if (srates[i] < min_srate)
  903. min_srate = srates[i];
  904. }
  905. return min_srate;
  906. }
  907. static int u_audio_rate_info(struct snd_kcontrol *kcontrol,
  908. struct snd_ctl_elem_info *uinfo)
  909. {
  910. const int *srates;
  911. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  912. struct snd_uac_chip *uac = prm->uac;
  913. struct g_audio *audio_dev = uac->audio_dev;
  914. struct uac_params *params = &audio_dev->params;
  915. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  916. uinfo->count = 1;
  917. if (prm == &uac->c_prm)
  918. srates = params->c_srates;
  919. else
  920. srates = params->p_srates;
  921. uinfo->value.integer.min = get_min_srate(srates);
  922. uinfo->value.integer.max = get_max_srate(srates);
  923. return 0;
  924. }
  925. static int u_audio_rate_get(struct snd_kcontrol *kcontrol,
  926. struct snd_ctl_elem_value *ucontrol)
  927. {
  928. struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
  929. unsigned long flags;
  930. spin_lock_irqsave(&prm->lock, flags);
  931. if (prm->active)
  932. ucontrol->value.integer.value[0] = prm->srate;
  933. else
  934. /* not active: reporting zero rate */
  935. ucontrol->value.integer.value[0] = 0;
  936. spin_unlock_irqrestore(&prm->lock, flags);
  937. return 0;
  938. }
  939. static struct snd_kcontrol_new u_audio_controls[] = {
  940. [UAC_FBACK_CTRL] = {
  941. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  942. .name = "Capture Pitch 1000000",
  943. .info = u_audio_pitch_info,
  944. .get = u_audio_pitch_get,
  945. .put = u_audio_pitch_put,
  946. },
  947. [UAC_P_PITCH_CTRL] = {
  948. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  949. .name = "Playback Pitch 1000000",
  950. .info = u_audio_pitch_info,
  951. .get = u_audio_pitch_get,
  952. .put = u_audio_pitch_put,
  953. },
  954. [UAC_MUTE_CTRL] = {
  955. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  956. .name = "", /* will be filled later */
  957. .info = u_audio_mute_info,
  958. .get = u_audio_mute_get,
  959. .put = u_audio_mute_put,
  960. },
  961. [UAC_VOLUME_CTRL] = {
  962. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  963. .name = "", /* will be filled later */
  964. .info = u_audio_volume_info,
  965. .get = u_audio_volume_get,
  966. .put = u_audio_volume_put,
  967. },
  968. [UAC_RATE_CTRL] = {
  969. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  970. .name = "", /* will be filled later */
  971. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  972. .info = u_audio_rate_info,
  973. .get = u_audio_rate_get,
  974. },
  975. };
  976. int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
  977. const char *card_name)
  978. {
  979. struct snd_uac_chip *uac;
  980. struct snd_card *card;
  981. struct snd_pcm *pcm;
  982. struct snd_kcontrol *kctl;
  983. struct uac_params *params;
  984. int p_chmask, c_chmask;
  985. int i, err;
  986. if (!g_audio)
  987. return -EINVAL;
  988. uac = kzalloc(sizeof(*uac), GFP_KERNEL);
  989. if (!uac)
  990. return -ENOMEM;
  991. g_audio->uac = uac;
  992. uac->audio_dev = g_audio;
  993. params = &g_audio->params;
  994. p_chmask = params->p_chmask;
  995. c_chmask = params->c_chmask;
  996. if (c_chmask) {
  997. struct uac_rtd_params *prm = &uac->c_prm;
  998. spin_lock_init(&prm->lock);
  999. uac->c_prm.uac = uac;
  1000. prm->max_psize = g_audio->out_ep_maxpsize;
  1001. prm->srate = params->c_srates[0];
  1002. prm->reqs = kcalloc(params->req_number,
  1003. sizeof(struct usb_request *),
  1004. GFP_KERNEL);
  1005. if (!prm->reqs) {
  1006. err = -ENOMEM;
  1007. goto fail;
  1008. }
  1009. prm->rbuf = kcalloc(params->req_number, prm->max_psize,
  1010. GFP_KERNEL);
  1011. if (!prm->rbuf) {
  1012. prm->max_psize = 0;
  1013. err = -ENOMEM;
  1014. goto fail;
  1015. }
  1016. }
  1017. if (p_chmask) {
  1018. struct uac_rtd_params *prm = &uac->p_prm;
  1019. spin_lock_init(&prm->lock);
  1020. uac->p_prm.uac = uac;
  1021. prm->max_psize = g_audio->in_ep_maxpsize;
  1022. prm->srate = params->p_srates[0];
  1023. prm->reqs = kcalloc(params->req_number,
  1024. sizeof(struct usb_request *),
  1025. GFP_KERNEL);
  1026. if (!prm->reqs) {
  1027. err = -ENOMEM;
  1028. goto fail;
  1029. }
  1030. prm->rbuf = kcalloc(params->req_number, prm->max_psize,
  1031. GFP_KERNEL);
  1032. if (!prm->rbuf) {
  1033. prm->max_psize = 0;
  1034. err = -ENOMEM;
  1035. goto fail;
  1036. }
  1037. }
  1038. /* Choose any slot, with no id */
  1039. err = snd_card_new(&g_audio->gadget->dev,
  1040. -1, NULL, THIS_MODULE, 0, &card);
  1041. if (err < 0)
  1042. goto fail;
  1043. uac->card = card;
  1044. /*
  1045. * Create first PCM device
  1046. * Create a substream only for non-zero channel streams
  1047. */
  1048. err = snd_pcm_new(uac->card, pcm_name, 0,
  1049. p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
  1050. if (err < 0)
  1051. goto snd_fail;
  1052. strscpy(pcm->name, pcm_name);
  1053. pcm->private_data = uac;
  1054. uac->pcm = pcm;
  1055. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
  1056. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
  1057. /*
  1058. * Create mixer and controls
  1059. * Create only if it's required on USB side
  1060. */
  1061. if ((c_chmask && g_audio->in_ep_fback)
  1062. || (p_chmask && params->p_fu.id)
  1063. || (c_chmask && params->c_fu.id))
  1064. strscpy(card->mixername, card_name);
  1065. if (c_chmask && g_audio->in_ep_fback) {
  1066. kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL],
  1067. &uac->c_prm);
  1068. if (!kctl) {
  1069. err = -ENOMEM;
  1070. goto snd_fail;
  1071. }
  1072. kctl->id.device = pcm->device;
  1073. kctl->id.subdevice = 0;
  1074. err = snd_ctl_add(card, kctl);
  1075. if (err < 0)
  1076. goto snd_fail;
  1077. }
  1078. if (p_chmask) {
  1079. kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL],
  1080. &uac->p_prm);
  1081. if (!kctl) {
  1082. err = -ENOMEM;
  1083. goto snd_fail;
  1084. }
  1085. kctl->id.device = pcm->device;
  1086. kctl->id.subdevice = 0;
  1087. err = snd_ctl_add(card, kctl);
  1088. if (err < 0)
  1089. goto snd_fail;
  1090. }
  1091. for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) {
  1092. struct uac_rtd_params *prm;
  1093. struct uac_fu_params *fu;
  1094. char ctrl_name[24];
  1095. char *direction;
  1096. if (!pcm->streams[i].substream_count)
  1097. continue;
  1098. if (i == SNDRV_PCM_STREAM_PLAYBACK) {
  1099. prm = &uac->p_prm;
  1100. fu = &params->p_fu;
  1101. direction = "Playback";
  1102. } else {
  1103. prm = &uac->c_prm;
  1104. fu = &params->c_fu;
  1105. direction = "Capture";
  1106. }
  1107. prm->fu_id = fu->id;
  1108. if (fu->mute_present) {
  1109. snprintf(ctrl_name, sizeof(ctrl_name),
  1110. "PCM %s Switch", direction);
  1111. u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name;
  1112. kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL],
  1113. prm);
  1114. if (!kctl) {
  1115. err = -ENOMEM;
  1116. goto snd_fail;
  1117. }
  1118. kctl->id.device = pcm->device;
  1119. kctl->id.subdevice = 0;
  1120. err = snd_ctl_add(card, kctl);
  1121. if (err < 0)
  1122. goto snd_fail;
  1123. prm->snd_kctl_mute_id = kctl->id;
  1124. prm->mute = 0;
  1125. }
  1126. if (fu->volume_present) {
  1127. snprintf(ctrl_name, sizeof(ctrl_name),
  1128. "PCM %s Volume", direction);
  1129. u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name;
  1130. kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL],
  1131. prm);
  1132. if (!kctl) {
  1133. err = -ENOMEM;
  1134. goto snd_fail;
  1135. }
  1136. kctl->id.device = pcm->device;
  1137. kctl->id.subdevice = 0;
  1138. kctl->tlv.c = u_audio_volume_tlv;
  1139. kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1140. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1141. err = snd_ctl_add(card, kctl);
  1142. if (err < 0)
  1143. goto snd_fail;
  1144. prm->snd_kctl_volume_id = kctl->id;
  1145. prm->volume = fu->volume_max;
  1146. prm->volume_max = fu->volume_max;
  1147. prm->volume_min = fu->volume_min;
  1148. prm->volume_res = fu->volume_res;
  1149. }
  1150. /* Add rate control */
  1151. snprintf(ctrl_name, sizeof(ctrl_name),
  1152. "%s Rate", direction);
  1153. u_audio_controls[UAC_RATE_CTRL].name = ctrl_name;
  1154. kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm);
  1155. if (!kctl) {
  1156. err = -ENOMEM;
  1157. goto snd_fail;
  1158. }
  1159. kctl->id.device = pcm->device;
  1160. kctl->id.subdevice = 0;
  1161. err = snd_ctl_add(card, kctl);
  1162. if (err < 0)
  1163. goto snd_fail;
  1164. prm->snd_kctl_rate_id = kctl->id;
  1165. }
  1166. strscpy(card->driver, card_name);
  1167. strscpy(card->shortname, card_name);
  1168. snprintf(card->longname, sizeof(card->longname), "%s %i",
  1169. card_name, card->dev->id);
  1170. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1171. NULL, 0, BUFF_SIZE_MAX);
  1172. err = snd_card_register(card);
  1173. if (!err)
  1174. return 0;
  1175. snd_fail:
  1176. snd_card_free(card);
  1177. fail:
  1178. kfree(uac->p_prm.reqs);
  1179. kfree(uac->c_prm.reqs);
  1180. kfree(uac->p_prm.rbuf);
  1181. kfree(uac->c_prm.rbuf);
  1182. kfree(uac);
  1183. return err;
  1184. }
  1185. EXPORT_SYMBOL_GPL(g_audio_setup);
  1186. void g_audio_cleanup(struct g_audio *g_audio)
  1187. {
  1188. struct snd_uac_chip *uac;
  1189. struct snd_card *card;
  1190. if (!g_audio || !g_audio->uac)
  1191. return;
  1192. uac = g_audio->uac;
  1193. g_audio->uac = NULL;
  1194. card = uac->card;
  1195. if (card)
  1196. snd_card_free_when_closed(card);
  1197. kfree(uac->p_prm.reqs);
  1198. kfree(uac->c_prm.reqs);
  1199. kfree(uac->p_prm.rbuf);
  1200. kfree(uac->c_prm.rbuf);
  1201. kfree(uac);
  1202. }
  1203. EXPORT_SYMBOL_GPL(g_audio_cleanup);
  1204. MODULE_LICENSE("GPL");
  1205. MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
  1206. MODULE_AUTHOR("Ruslan Bilovol");