u_uac1_legacy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_uac1.c -- ALSA audio utilities for Gadget stack
  4. *
  5. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  6. * Copyright (C) 2008 Analog Devices, Inc
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/device.h>
  12. #include <linux/delay.h>
  13. #include <linux/ctype.h>
  14. #include <linux/random.h>
  15. #include <linux/syscalls.h>
  16. #include "u_uac1_legacy.h"
  17. /*
  18. * This component encapsulates the ALSA devices for USB audio gadget
  19. */
  20. /*-------------------------------------------------------------------------*/
  21. /**
  22. * Some ALSA internal helper functions
  23. */
  24. static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
  25. {
  26. struct snd_interval t;
  27. t.empty = 0;
  28. t.min = t.max = val;
  29. t.openmin = t.openmax = 0;
  30. t.integer = 1;
  31. return snd_interval_refine(i, &t);
  32. }
  33. static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
  34. snd_pcm_hw_param_t var, unsigned int val,
  35. int dir)
  36. {
  37. int changed;
  38. if (hw_is_mask(var)) {
  39. struct snd_mask *m = hw_param_mask(params, var);
  40. if (val == 0 && dir < 0) {
  41. changed = -EINVAL;
  42. snd_mask_none(m);
  43. } else {
  44. if (dir > 0)
  45. val++;
  46. else if (dir < 0)
  47. val--;
  48. changed = snd_mask_refine_set(
  49. hw_param_mask(params, var), val);
  50. }
  51. } else if (hw_is_interval(var)) {
  52. struct snd_interval *i = hw_param_interval(params, var);
  53. if (val == 0 && dir < 0) {
  54. changed = -EINVAL;
  55. snd_interval_none(i);
  56. } else if (dir == 0)
  57. changed = snd_interval_refine_set(i, val);
  58. else {
  59. struct snd_interval t;
  60. t.openmin = 1;
  61. t.openmax = 1;
  62. t.empty = 0;
  63. t.integer = 0;
  64. if (dir < 0) {
  65. t.min = val - 1;
  66. t.max = val;
  67. } else {
  68. t.min = val;
  69. t.max = val+1;
  70. }
  71. changed = snd_interval_refine(i, &t);
  72. }
  73. } else
  74. return -EINVAL;
  75. if (changed) {
  76. params->cmask |= 1 << var;
  77. params->rmask |= 1 << var;
  78. }
  79. return changed;
  80. }
  81. /*-------------------------------------------------------------------------*/
  82. /**
  83. * Set default hardware params
  84. */
  85. static int playback_default_hw_params(struct gaudio_snd_dev *snd)
  86. {
  87. struct snd_pcm_substream *substream = snd->substream;
  88. struct snd_pcm_hw_params *params;
  89. snd_pcm_sframes_t result;
  90. /*
  91. * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
  92. * SNDRV_PCM_FORMAT_S16_LE
  93. * CHANNELS: 2
  94. * RATE: 48000
  95. */
  96. snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  97. snd->format = SNDRV_PCM_FORMAT_S16_LE;
  98. snd->channels = 2;
  99. snd->rate = 48000;
  100. params = kzalloc(sizeof(*params), GFP_KERNEL);
  101. if (!params)
  102. return -ENOMEM;
  103. _snd_pcm_hw_params_any(params);
  104. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
  105. snd->access, 0);
  106. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
  107. snd->format, 0);
  108. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
  109. snd->channels, 0);
  110. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
  111. snd->rate, 0);
  112. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  113. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params);
  114. result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
  115. if (result < 0) {
  116. ERROR(snd->card,
  117. "Preparing sound card failed: %d\n", (int)result);
  118. kfree(params);
  119. return result;
  120. }
  121. /* Store the hardware parameters */
  122. snd->access = params_access(params);
  123. snd->format = params_format(params);
  124. snd->channels = params_channels(params);
  125. snd->rate = params_rate(params);
  126. kfree(params);
  127. INFO(snd->card,
  128. "Hardware params: access %x, format %x, channels %d, rate %d\n",
  129. snd->access, snd->format, snd->channels, snd->rate);
  130. return 0;
  131. }
  132. /**
  133. * Playback audio buffer data by ALSA PCM device
  134. */
  135. size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
  136. {
  137. struct gaudio_snd_dev *snd = &card->playback;
  138. struct snd_pcm_substream *substream = snd->substream;
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. ssize_t result;
  141. snd_pcm_sframes_t frames;
  142. try_again:
  143. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  144. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  145. result = snd_pcm_kernel_ioctl(substream,
  146. SNDRV_PCM_IOCTL_PREPARE, NULL);
  147. if (result < 0) {
  148. ERROR(card, "Preparing sound card failed: %d\n",
  149. (int)result);
  150. return result;
  151. }
  152. }
  153. frames = bytes_to_frames(runtime, count);
  154. result = snd_pcm_kernel_write(snd->substream, buf, frames);
  155. if (result != frames) {
  156. ERROR(card, "Playback error: %d\n", (int)result);
  157. goto try_again;
  158. }
  159. return 0;
  160. }
  161. int u_audio_get_playback_channels(struct gaudio *card)
  162. {
  163. return card->playback.channels;
  164. }
  165. int u_audio_get_playback_rate(struct gaudio *card)
  166. {
  167. return card->playback.rate;
  168. }
  169. /**
  170. * Open ALSA PCM and control device files
  171. * Initial the PCM or control device
  172. */
  173. static int gaudio_open_snd_dev(struct gaudio *card)
  174. {
  175. struct snd_pcm_file *pcm_file;
  176. struct gaudio_snd_dev *snd;
  177. struct f_uac1_legacy_opts *opts;
  178. char *fn_play, *fn_cap, *fn_cntl;
  179. opts = container_of(card->func.fi, struct f_uac1_legacy_opts,
  180. func_inst);
  181. fn_play = opts->fn_play;
  182. fn_cap = opts->fn_cap;
  183. fn_cntl = opts->fn_cntl;
  184. /* Open control device */
  185. snd = &card->control;
  186. snd->filp = filp_open(fn_cntl, O_RDWR, 0);
  187. if (IS_ERR(snd->filp)) {
  188. int ret = PTR_ERR(snd->filp);
  189. ERROR(card, "unable to open sound control device file: %s\n",
  190. fn_cntl);
  191. snd->filp = NULL;
  192. return ret;
  193. }
  194. snd->card = card;
  195. /* Open PCM playback device and setup substream */
  196. snd = &card->playback;
  197. snd->filp = filp_open(fn_play, O_WRONLY, 0);
  198. if (IS_ERR(snd->filp)) {
  199. int ret = PTR_ERR(snd->filp);
  200. ERROR(card, "No such PCM playback device: %s\n", fn_play);
  201. snd->filp = NULL;
  202. return ret;
  203. }
  204. pcm_file = snd->filp->private_data;
  205. snd->substream = pcm_file->substream;
  206. snd->card = card;
  207. playback_default_hw_params(snd);
  208. /* Open PCM capture device and setup substream */
  209. snd = &card->capture;
  210. snd->filp = filp_open(fn_cap, O_RDONLY, 0);
  211. if (IS_ERR(snd->filp)) {
  212. ERROR(card, "No such PCM capture device: %s\n", fn_cap);
  213. snd->substream = NULL;
  214. snd->card = NULL;
  215. snd->filp = NULL;
  216. } else {
  217. pcm_file = snd->filp->private_data;
  218. snd->substream = pcm_file->substream;
  219. snd->card = card;
  220. }
  221. return 0;
  222. }
  223. /**
  224. * Close ALSA PCM and control device files
  225. */
  226. static int gaudio_close_snd_dev(struct gaudio *gau)
  227. {
  228. struct gaudio_snd_dev *snd;
  229. /* Close control device */
  230. snd = &gau->control;
  231. if (snd->filp)
  232. filp_close(snd->filp, NULL);
  233. /* Close PCM playback device and setup substream */
  234. snd = &gau->playback;
  235. if (snd->filp)
  236. filp_close(snd->filp, NULL);
  237. /* Close PCM capture device and setup substream */
  238. snd = &gau->capture;
  239. if (snd->filp)
  240. filp_close(snd->filp, NULL);
  241. return 0;
  242. }
  243. /**
  244. * gaudio_setup - setup ALSA interface and preparing for USB transfer
  245. *
  246. * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
  247. *
  248. * Returns negative errno, or zero on success
  249. */
  250. int gaudio_setup(struct gaudio *card)
  251. {
  252. int ret;
  253. ret = gaudio_open_snd_dev(card);
  254. if (ret)
  255. ERROR(card, "we need at least one control device\n");
  256. return ret;
  257. }
  258. /**
  259. * gaudio_cleanup - remove ALSA device interface
  260. *
  261. * This is called to free all resources allocated by @gaudio_setup().
  262. */
  263. void gaudio_cleanup(struct gaudio *the_card)
  264. {
  265. if (the_card)
  266. gaudio_close_snd_dev(the_card);
  267. }