pd-alsa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <linux/kernel.h>
  2. #include <linux/usb.h>
  3. #include <linux/init.h>
  4. #include <linux/sound.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/soundcard.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/module.h>
  10. #include <linux/gfp.h>
  11. #include <sound/core.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/info.h>
  15. #include <sound/initval.h>
  16. #include <sound/control.h>
  17. #include <media/v4l2-common.h>
  18. #include "pd-common.h"
  19. #include "vendorcmds.h"
  20. static void complete_handler_audio(struct urb *urb);
  21. #define AUDIO_EP (0x83)
  22. #define AUDIO_BUF_SIZE (512)
  23. #define PERIOD_SIZE (1024 * 8)
  24. #define PERIOD_MIN (4)
  25. #define PERIOD_MAX PERIOD_MIN
  26. static struct snd_pcm_hardware snd_pd_hw_capture = {
  27. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  28. SNDRV_PCM_INFO_MMAP |
  29. SNDRV_PCM_INFO_INTERLEAVED |
  30. SNDRV_PCM_INFO_MMAP_VALID,
  31. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  32. .rates = SNDRV_PCM_RATE_48000,
  33. .rate_min = 48000,
  34. .rate_max = 48000,
  35. .channels_min = 2,
  36. .channels_max = 2,
  37. .buffer_bytes_max = PERIOD_SIZE * PERIOD_MIN,
  38. .period_bytes_min = PERIOD_SIZE,
  39. .period_bytes_max = PERIOD_SIZE,
  40. .periods_min = PERIOD_MIN,
  41. .periods_max = PERIOD_MAX,
  42. /*
  43. .buffer_bytes_max = 62720 * 8,
  44. .period_bytes_min = 64,
  45. .period_bytes_max = 12544,
  46. .periods_min = 2,
  47. .periods_max = 98
  48. */
  49. };
  50. static int snd_pd_capture_open(struct snd_pcm_substream *substream)
  51. {
  52. struct poseidon *p = snd_pcm_substream_chip(substream);
  53. struct poseidon_audio *pa = &p->audio;
  54. struct snd_pcm_runtime *runtime = substream->runtime;
  55. if (!p)
  56. return -ENODEV;
  57. pa->users++;
  58. pa->card_close = 0;
  59. pa->capture_pcm_substream = substream;
  60. runtime->private_data = p;
  61. runtime->hw = snd_pd_hw_capture;
  62. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  63. usb_autopm_get_interface(p->interface);
  64. kref_get(&p->kref);
  65. return 0;
  66. }
  67. static int snd_pd_pcm_close(struct snd_pcm_substream *substream)
  68. {
  69. struct poseidon *p = snd_pcm_substream_chip(substream);
  70. struct poseidon_audio *pa = &p->audio;
  71. pa->users--;
  72. pa->card_close = 1;
  73. usb_autopm_put_interface(p->interface);
  74. kref_put(&p->kref, poseidon_delete);
  75. return 0;
  76. }
  77. static int snd_pd_hw_capture_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *hw_params)
  79. {
  80. struct snd_pcm_runtime *runtime = substream->runtime;
  81. unsigned int size;
  82. size = params_buffer_bytes(hw_params);
  83. if (runtime->dma_area) {
  84. if (runtime->dma_bytes > size)
  85. return 0;
  86. vfree(runtime->dma_area);
  87. }
  88. runtime->dma_area = vmalloc(size);
  89. if (!runtime->dma_area)
  90. return -ENOMEM;
  91. else
  92. runtime->dma_bytes = size;
  93. return 0;
  94. }
  95. static int audio_buf_free(struct poseidon *p)
  96. {
  97. struct poseidon_audio *pa = &p->audio;
  98. int i;
  99. for (i = 0; i < AUDIO_BUFS; i++)
  100. if (pa->urb_array[i])
  101. usb_kill_urb(pa->urb_array[i]);
  102. free_all_urb_generic(pa->urb_array, AUDIO_BUFS);
  103. logpm();
  104. return 0;
  105. }
  106. static int snd_pd_hw_capture_free(struct snd_pcm_substream *substream)
  107. {
  108. struct poseidon *p = snd_pcm_substream_chip(substream);
  109. logpm();
  110. audio_buf_free(p);
  111. return 0;
  112. }
  113. static int snd_pd_prepare(struct snd_pcm_substream *substream)
  114. {
  115. return 0;
  116. }
  117. #define AUDIO_TRAILER_SIZE (16)
  118. static inline void handle_audio_data(struct urb *urb, int *period_elapsed)
  119. {
  120. struct poseidon_audio *pa = urb->context;
  121. struct snd_pcm_runtime *runtime = pa->capture_pcm_substream->runtime;
  122. int stride = runtime->frame_bits >> 3;
  123. int len = urb->actual_length / stride;
  124. unsigned char *cp = urb->transfer_buffer;
  125. unsigned int oldptr = pa->rcv_position;
  126. if (urb->actual_length == AUDIO_BUF_SIZE - 4)
  127. len -= (AUDIO_TRAILER_SIZE / stride);
  128. /* do the copy */
  129. if (oldptr + len >= runtime->buffer_size) {
  130. unsigned int cnt = runtime->buffer_size - oldptr;
  131. memcpy(runtime->dma_area + oldptr * stride, cp, cnt * stride);
  132. memcpy(runtime->dma_area, (cp + cnt * stride),
  133. (len * stride - cnt * stride));
  134. } else
  135. memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
  136. /* update the statas */
  137. snd_pcm_stream_lock(pa->capture_pcm_substream);
  138. pa->rcv_position += len;
  139. if (pa->rcv_position >= runtime->buffer_size)
  140. pa->rcv_position -= runtime->buffer_size;
  141. pa->copied_position += (len);
  142. if (pa->copied_position >= runtime->period_size) {
  143. pa->copied_position -= runtime->period_size;
  144. *period_elapsed = 1;
  145. }
  146. snd_pcm_stream_unlock(pa->capture_pcm_substream);
  147. }
  148. static void complete_handler_audio(struct urb *urb)
  149. {
  150. struct poseidon_audio *pa = urb->context;
  151. struct snd_pcm_substream *substream = pa->capture_pcm_substream;
  152. int period_elapsed = 0;
  153. int ret;
  154. if (1 == pa->card_close || pa->capture_stream != STREAM_ON)
  155. return;
  156. if (urb->status != 0) {
  157. /*if (urb->status == -ESHUTDOWN)*/
  158. return;
  159. }
  160. if (substream) {
  161. if (urb->actual_length) {
  162. handle_audio_data(urb, &period_elapsed);
  163. if (period_elapsed)
  164. snd_pcm_period_elapsed(substream);
  165. }
  166. }
  167. ret = usb_submit_urb(urb, GFP_ATOMIC);
  168. if (ret < 0)
  169. log("audio urb failed (errcod = %i)", ret);
  170. return;
  171. }
  172. static int fire_audio_urb(struct poseidon *p)
  173. {
  174. int i, ret = 0;
  175. struct poseidon_audio *pa = &p->audio;
  176. alloc_bulk_urbs_generic(pa->urb_array, AUDIO_BUFS,
  177. p->udev, AUDIO_EP,
  178. AUDIO_BUF_SIZE, GFP_ATOMIC,
  179. complete_handler_audio, pa);
  180. for (i = 0; i < AUDIO_BUFS; i++) {
  181. ret = usb_submit_urb(pa->urb_array[i], GFP_KERNEL);
  182. if (ret)
  183. log("urb err : %d", ret);
  184. }
  185. log();
  186. return ret;
  187. }
  188. static int snd_pd_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  189. {
  190. struct poseidon *p = snd_pcm_substream_chip(substream);
  191. struct poseidon_audio *pa = &p->audio;
  192. if (debug_mode)
  193. log("cmd %d, audio stat : %d\n", cmd, pa->capture_stream);
  194. switch (cmd) {
  195. case SNDRV_PCM_TRIGGER_RESUME:
  196. case SNDRV_PCM_TRIGGER_START:
  197. if (pa->capture_stream == STREAM_ON)
  198. return 0;
  199. pa->rcv_position = pa->copied_position = 0;
  200. pa->capture_stream = STREAM_ON;
  201. if (in_hibernation(p))
  202. return 0;
  203. fire_audio_urb(p);
  204. return 0;
  205. case SNDRV_PCM_TRIGGER_SUSPEND:
  206. pa->capture_stream = STREAM_SUSPEND;
  207. return 0;
  208. case SNDRV_PCM_TRIGGER_STOP:
  209. pa->capture_stream = STREAM_OFF;
  210. return 0;
  211. default:
  212. return -EINVAL;
  213. }
  214. }
  215. static snd_pcm_uframes_t
  216. snd_pd_capture_pointer(struct snd_pcm_substream *substream)
  217. {
  218. struct poseidon *p = snd_pcm_substream_chip(substream);
  219. struct poseidon_audio *pa = &p->audio;
  220. return pa->rcv_position;
  221. }
  222. static struct page *snd_pcm_pd_get_page(struct snd_pcm_substream *subs,
  223. unsigned long offset)
  224. {
  225. void *pageptr = subs->runtime->dma_area + offset;
  226. return vmalloc_to_page(pageptr);
  227. }
  228. static struct snd_pcm_ops pcm_capture_ops = {
  229. .open = snd_pd_capture_open,
  230. .close = snd_pd_pcm_close,
  231. .ioctl = snd_pcm_lib_ioctl,
  232. .hw_params = snd_pd_hw_capture_params,
  233. .hw_free = snd_pd_hw_capture_free,
  234. .prepare = snd_pd_prepare,
  235. .trigger = snd_pd_capture_trigger,
  236. .pointer = snd_pd_capture_pointer,
  237. .page = snd_pcm_pd_get_page,
  238. };
  239. #ifdef CONFIG_PM
  240. int pm_alsa_suspend(struct poseidon *p)
  241. {
  242. logpm(p);
  243. audio_buf_free(p);
  244. return 0;
  245. }
  246. int pm_alsa_resume(struct poseidon *p)
  247. {
  248. logpm(p);
  249. fire_audio_urb(p);
  250. return 0;
  251. }
  252. #endif
  253. int poseidon_audio_init(struct poseidon *p)
  254. {
  255. struct poseidon_audio *pa = &p->audio;
  256. struct snd_card *card;
  257. struct snd_pcm *pcm;
  258. int ret;
  259. ret = snd_card_create(-1, "Telegent", THIS_MODULE, 0, &card);
  260. if (ret != 0)
  261. return ret;
  262. ret = snd_pcm_new(card, "poseidon audio", 0, 0, 1, &pcm);
  263. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  264. pcm->info_flags = 0;
  265. pcm->private_data = p;
  266. strcpy(pcm->name, "poseidon audio capture");
  267. strcpy(card->driver, "ALSA driver");
  268. strcpy(card->shortname, "poseidon Audio");
  269. strcpy(card->longname, "poseidon ALSA Audio");
  270. if (snd_card_register(card)) {
  271. snd_card_free(card);
  272. return -ENOMEM;
  273. }
  274. pa->card = card;
  275. return 0;
  276. }
  277. int poseidon_audio_free(struct poseidon *p)
  278. {
  279. struct poseidon_audio *pa = &p->audio;
  280. if (pa->card)
  281. snd_card_free(pa->card);
  282. return 0;
  283. }