u_audio.c 7.6 KB

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