cx18-alsa-pcm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * ALSA PCM device for the
  3. * ALSA interface to cx18 PCM capture streams
  4. *
  5. * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
  6. * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
  7. *
  8. * Portions of this work were sponsored by ONELAN Limited.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/vmalloc.h>
  23. #include <media/v4l2-device.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include "cx18-driver.h"
  27. #include "cx18-queue.h"
  28. #include "cx18-streams.h"
  29. #include "cx18-fileops.h"
  30. #include "cx18-alsa.h"
  31. #include "cx18-alsa-pcm.h"
  32. static unsigned int pcm_debug;
  33. module_param(pcm_debug, int, 0644);
  34. MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
  35. #define dprintk(fmt, arg...) do { \
  36. if (pcm_debug) \
  37. printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
  38. __func__, ##arg); \
  39. } while (0)
  40. static struct snd_pcm_hardware snd_cx18_hw_capture = {
  41. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  42. SNDRV_PCM_INFO_MMAP |
  43. SNDRV_PCM_INFO_INTERLEAVED |
  44. SNDRV_PCM_INFO_MMAP_VALID,
  45. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  46. .rates = SNDRV_PCM_RATE_48000,
  47. .rate_min = 48000,
  48. .rate_max = 48000,
  49. .channels_min = 2,
  50. .channels_max = 2,
  51. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  52. .period_bytes_min = 64, /* 12544/2, */
  53. .period_bytes_max = 12544,
  54. .periods_min = 2,
  55. .periods_max = 98, /* 12544, */
  56. };
  57. void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
  58. size_t num_bytes)
  59. {
  60. struct snd_pcm_substream *substream;
  61. struct snd_pcm_runtime *runtime;
  62. unsigned int oldptr;
  63. unsigned int stride;
  64. int period_elapsed = 0;
  65. int length;
  66. dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%zu\n", cxsc,
  67. pcm_data, num_bytes);
  68. substream = cxsc->capture_pcm_substream;
  69. if (substream == NULL) {
  70. dprintk("substream was NULL\n");
  71. return;
  72. }
  73. runtime = substream->runtime;
  74. if (runtime == NULL) {
  75. dprintk("runtime was NULL\n");
  76. return;
  77. }
  78. stride = runtime->frame_bits >> 3;
  79. if (stride == 0) {
  80. dprintk("stride is zero\n");
  81. return;
  82. }
  83. length = num_bytes / stride;
  84. if (length == 0) {
  85. dprintk("%s: length was zero\n", __func__);
  86. return;
  87. }
  88. if (runtime->dma_area == NULL) {
  89. dprintk("dma area was NULL - ignoring\n");
  90. return;
  91. }
  92. oldptr = cxsc->hwptr_done_capture;
  93. if (oldptr + length >= runtime->buffer_size) {
  94. unsigned int cnt =
  95. runtime->buffer_size - oldptr;
  96. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  97. cnt * stride);
  98. memcpy(runtime->dma_area, pcm_data + cnt * stride,
  99. length * stride - cnt * stride);
  100. } else {
  101. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  102. length * stride);
  103. }
  104. snd_pcm_stream_lock(substream);
  105. cxsc->hwptr_done_capture += length;
  106. if (cxsc->hwptr_done_capture >=
  107. runtime->buffer_size)
  108. cxsc->hwptr_done_capture -=
  109. runtime->buffer_size;
  110. cxsc->capture_transfer_done += length;
  111. if (cxsc->capture_transfer_done >=
  112. runtime->period_size) {
  113. cxsc->capture_transfer_done -=
  114. runtime->period_size;
  115. period_elapsed = 1;
  116. }
  117. snd_pcm_stream_unlock(substream);
  118. if (period_elapsed)
  119. snd_pcm_period_elapsed(substream);
  120. }
  121. static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
  122. {
  123. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
  126. struct cx18 *cx = to_cx18(v4l2_dev);
  127. struct cx18_stream *s;
  128. struct cx18_open_id item;
  129. int ret;
  130. /* Instruct the cx18 to start sending packets */
  131. snd_cx18_lock(cxsc);
  132. s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
  133. item.cx = cx;
  134. item.type = s->type;
  135. item.open_id = cx->open_id++;
  136. /* See if the stream is available */
  137. if (cx18_claim_stream(&item, item.type)) {
  138. /* No, it's already in use */
  139. snd_cx18_unlock(cxsc);
  140. return -EBUSY;
  141. }
  142. if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
  143. test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  144. /* We're already streaming. No additional action required */
  145. snd_cx18_unlock(cxsc);
  146. return 0;
  147. }
  148. runtime->hw = snd_cx18_hw_capture;
  149. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  150. cxsc->capture_pcm_substream = substream;
  151. runtime->private_data = cx;
  152. cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
  153. /* Not currently streaming, so start it up */
  154. set_bit(CX18_F_S_STREAMING, &s->s_flags);
  155. ret = cx18_start_v4l2_encode_stream(s);
  156. snd_cx18_unlock(cxsc);
  157. return ret;
  158. }
  159. static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
  160. {
  161. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  162. struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
  163. struct cx18 *cx = to_cx18(v4l2_dev);
  164. struct cx18_stream *s;
  165. /* Instruct the cx18 to stop sending packets */
  166. snd_cx18_lock(cxsc);
  167. s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
  168. cx18_stop_v4l2_encode_stream(s, 0);
  169. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  170. cx18_release_stream(s);
  171. cx->pcm_announce_callback = NULL;
  172. snd_cx18_unlock(cxsc);
  173. return 0;
  174. }
  175. static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream,
  176. unsigned int cmd, void *arg)
  177. {
  178. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  179. int ret;
  180. snd_cx18_lock(cxsc);
  181. ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  182. snd_cx18_unlock(cxsc);
  183. return ret;
  184. }
  185. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  186. size_t size)
  187. {
  188. struct snd_pcm_runtime *runtime = subs->runtime;
  189. dprintk("Allocating vbuffer\n");
  190. if (runtime->dma_area) {
  191. if (runtime->dma_bytes > size)
  192. return 0;
  193. vfree(runtime->dma_area);
  194. }
  195. runtime->dma_area = vmalloc(size);
  196. if (!runtime->dma_area)
  197. return -ENOMEM;
  198. runtime->dma_bytes = size;
  199. return 0;
  200. }
  201. static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params)
  203. {
  204. dprintk("%s called\n", __func__);
  205. return snd_pcm_alloc_vmalloc_buffer(substream,
  206. params_buffer_bytes(params));
  207. }
  208. static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream)
  209. {
  210. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  211. unsigned long flags;
  212. spin_lock_irqsave(&cxsc->slock, flags);
  213. if (substream->runtime->dma_area) {
  214. dprintk("freeing pcm capture region\n");
  215. vfree(substream->runtime->dma_area);
  216. substream->runtime->dma_area = NULL;
  217. }
  218. spin_unlock_irqrestore(&cxsc->slock, flags);
  219. return 0;
  220. }
  221. static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
  222. {
  223. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  224. cxsc->hwptr_done_capture = 0;
  225. cxsc->capture_transfer_done = 0;
  226. return 0;
  227. }
  228. static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  229. {
  230. return 0;
  231. }
  232. static
  233. snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
  234. {
  235. unsigned long flags;
  236. snd_pcm_uframes_t hwptr_done;
  237. struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
  238. spin_lock_irqsave(&cxsc->slock, flags);
  239. hwptr_done = cxsc->hwptr_done_capture;
  240. spin_unlock_irqrestore(&cxsc->slock, flags);
  241. return hwptr_done;
  242. }
  243. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  244. unsigned long offset)
  245. {
  246. void *pageptr = subs->runtime->dma_area + offset;
  247. return vmalloc_to_page(pageptr);
  248. }
  249. static const struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
  250. .open = snd_cx18_pcm_capture_open,
  251. .close = snd_cx18_pcm_capture_close,
  252. .ioctl = snd_cx18_pcm_ioctl,
  253. .hw_params = snd_cx18_pcm_hw_params,
  254. .hw_free = snd_cx18_pcm_hw_free,
  255. .prepare = snd_cx18_pcm_prepare,
  256. .trigger = snd_cx18_pcm_trigger,
  257. .pointer = snd_cx18_pcm_pointer,
  258. .page = snd_pcm_get_vmalloc_page,
  259. };
  260. int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
  261. {
  262. struct snd_pcm *sp;
  263. struct snd_card *sc = cxsc->sc;
  264. struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
  265. struct cx18 *cx = to_cx18(v4l2_dev);
  266. int ret;
  267. ret = snd_pcm_new(sc, "CX23418 PCM",
  268. 0, /* PCM device 0, the only one for this card */
  269. 0, /* 0 playback substreams */
  270. 1, /* 1 capture substream */
  271. &sp);
  272. if (ret) {
  273. CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
  274. __func__, ret);
  275. goto err_exit;
  276. }
  277. spin_lock_init(&cxsc->slock);
  278. snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
  279. &snd_cx18_pcm_capture_ops);
  280. sp->info_flags = 0;
  281. sp->private_data = cxsc;
  282. strlcpy(sp->name, cx->card_name, sizeof(sp->name));
  283. return 0;
  284. err_exit:
  285. return ret;
  286. }