u_audio.c 7.6 KB

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