cx18-alsa-pcm.c 8.8 KB

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