cx18-alsa-pcm.c 8.9 KB

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