u_uac1_legacy.c 7.1 KB

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