u_uac1.c 7.2 KB

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