ivtv-alsa-pcm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * ALSA PCM device for the
  3. * ALSA interface to ivtv PCM capture streams
  4. *
  5. * Copyright (C) 2009,2012 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 for the cx18 driver
  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 "ivtv-driver.h"
  32. #include "ivtv-queue.h"
  33. #include "ivtv-streams.h"
  34. #include "ivtv-fileops.h"
  35. #include "ivtv-alsa.h"
  36. #include "ivtv-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...) \
  41. do { \
  42. if (pcm_debug) \
  43. pr_info("ivtv-alsa-pcm %s: " fmt, __func__, ##arg); \
  44. } while (0)
  45. static struct snd_pcm_hardware snd_ivtv_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. static void ivtv_alsa_announce_pcm_data(struct snd_ivtv_card *itvsc,
  63. u8 *pcm_data,
  64. size_t num_bytes)
  65. {
  66. struct snd_pcm_substream *substream;
  67. struct snd_pcm_runtime *runtime;
  68. unsigned int oldptr;
  69. unsigned int stride;
  70. int period_elapsed = 0;
  71. int length;
  72. dprintk("ivtv alsa announce ptr=%p data=%p num_bytes=%zd\n", itvsc,
  73. pcm_data, num_bytes);
  74. substream = itvsc->capture_pcm_substream;
  75. if (substream == NULL) {
  76. dprintk("substream was NULL\n");
  77. return;
  78. }
  79. runtime = substream->runtime;
  80. if (runtime == NULL) {
  81. dprintk("runtime was NULL\n");
  82. return;
  83. }
  84. stride = runtime->frame_bits >> 3;
  85. if (stride == 0) {
  86. dprintk("stride is zero\n");
  87. return;
  88. }
  89. length = num_bytes / stride;
  90. if (length == 0) {
  91. dprintk("%s: length was zero\n", __func__);
  92. return;
  93. }
  94. if (runtime->dma_area == NULL) {
  95. dprintk("dma area was NULL - ignoring\n");
  96. return;
  97. }
  98. oldptr = itvsc->hwptr_done_capture;
  99. if (oldptr + length >= runtime->buffer_size) {
  100. unsigned int cnt =
  101. runtime->buffer_size - oldptr;
  102. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  103. cnt * stride);
  104. memcpy(runtime->dma_area, pcm_data + cnt * stride,
  105. length * stride - cnt * stride);
  106. } else {
  107. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  108. length * stride);
  109. }
  110. snd_pcm_stream_lock(substream);
  111. itvsc->hwptr_done_capture += length;
  112. if (itvsc->hwptr_done_capture >=
  113. runtime->buffer_size)
  114. itvsc->hwptr_done_capture -=
  115. runtime->buffer_size;
  116. itvsc->capture_transfer_done += length;
  117. if (itvsc->capture_transfer_done >=
  118. runtime->period_size) {
  119. itvsc->capture_transfer_done -=
  120. runtime->period_size;
  121. period_elapsed = 1;
  122. }
  123. snd_pcm_stream_unlock(substream);
  124. if (period_elapsed)
  125. snd_pcm_period_elapsed(substream);
  126. }
  127. static int snd_ivtv_pcm_capture_open(struct snd_pcm_substream *substream)
  128. {
  129. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  130. struct snd_pcm_runtime *runtime = substream->runtime;
  131. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  132. struct ivtv *itv = to_ivtv(v4l2_dev);
  133. struct ivtv_stream *s;
  134. struct ivtv_open_id item;
  135. int ret;
  136. /* Instruct the CX2341[56] to start sending packets */
  137. snd_ivtv_lock(itvsc);
  138. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  139. v4l2_fh_init(&item.fh, s->vdev);
  140. item.itv = itv;
  141. item.type = s->type;
  142. /* See if the stream is available */
  143. if (ivtv_claim_stream(&item, item.type)) {
  144. /* No, it's already in use */
  145. snd_ivtv_unlock(itvsc);
  146. return -EBUSY;
  147. }
  148. if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
  149. test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  150. /* We're already streaming. No additional action required */
  151. snd_ivtv_unlock(itvsc);
  152. return 0;
  153. }
  154. runtime->hw = snd_ivtv_hw_capture;
  155. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  156. itvsc->capture_pcm_substream = substream;
  157. runtime->private_data = itv;
  158. itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
  159. /* Not currently streaming, so start it up */
  160. set_bit(IVTV_F_S_STREAMING, &s->s_flags);
  161. ret = ivtv_start_v4l2_encode_stream(s);
  162. snd_ivtv_unlock(itvsc);
  163. return ret;
  164. }
  165. static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
  166. {
  167. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  168. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  169. struct ivtv *itv = to_ivtv(v4l2_dev);
  170. struct ivtv_stream *s;
  171. /* Instruct the ivtv to stop sending packets */
  172. snd_ivtv_lock(itvsc);
  173. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  174. ivtv_stop_v4l2_encode_stream(s, 0);
  175. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  176. ivtv_release_stream(s);
  177. itv->pcm_announce_callback = NULL;
  178. snd_ivtv_unlock(itvsc);
  179. return 0;
  180. }
  181. static int snd_ivtv_pcm_ioctl(struct snd_pcm_substream *substream,
  182. unsigned int cmd, void *arg)
  183. {
  184. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  185. int ret;
  186. snd_ivtv_lock(itvsc);
  187. ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  188. snd_ivtv_unlock(itvsc);
  189. return ret;
  190. }
  191. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  192. size_t size)
  193. {
  194. struct snd_pcm_runtime *runtime = subs->runtime;
  195. dprintk("Allocating vbuffer\n");
  196. if (runtime->dma_area) {
  197. if (runtime->dma_bytes > size)
  198. return 0;
  199. vfree(runtime->dma_area);
  200. }
  201. runtime->dma_area = vmalloc(size);
  202. if (!runtime->dma_area)
  203. return -ENOMEM;
  204. runtime->dma_bytes = size;
  205. return 0;
  206. }
  207. static int snd_ivtv_pcm_hw_params(struct snd_pcm_substream *substream,
  208. struct snd_pcm_hw_params *params)
  209. {
  210. dprintk("%s called\n", __func__);
  211. return snd_pcm_alloc_vmalloc_buffer(substream,
  212. params_buffer_bytes(params));
  213. }
  214. static int snd_ivtv_pcm_hw_free(struct snd_pcm_substream *substream)
  215. {
  216. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  217. unsigned long flags;
  218. spin_lock_irqsave(&itvsc->slock, flags);
  219. if (substream->runtime->dma_area) {
  220. dprintk("freeing pcm capture region\n");
  221. vfree(substream->runtime->dma_area);
  222. substream->runtime->dma_area = NULL;
  223. }
  224. spin_unlock_irqrestore(&itvsc->slock, flags);
  225. return 0;
  226. }
  227. static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
  228. {
  229. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  230. itvsc->hwptr_done_capture = 0;
  231. itvsc->capture_transfer_done = 0;
  232. return 0;
  233. }
  234. static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  235. {
  236. return 0;
  237. }
  238. static
  239. snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
  240. {
  241. unsigned long flags;
  242. snd_pcm_uframes_t hwptr_done;
  243. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  244. spin_lock_irqsave(&itvsc->slock, flags);
  245. hwptr_done = itvsc->hwptr_done_capture;
  246. spin_unlock_irqrestore(&itvsc->slock, flags);
  247. return hwptr_done;
  248. }
  249. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  250. unsigned long offset)
  251. {
  252. void *pageptr = subs->runtime->dma_area + offset;
  253. return vmalloc_to_page(pageptr);
  254. }
  255. static struct snd_pcm_ops snd_ivtv_pcm_capture_ops = {
  256. .open = snd_ivtv_pcm_capture_open,
  257. .close = snd_ivtv_pcm_capture_close,
  258. .ioctl = snd_ivtv_pcm_ioctl,
  259. .hw_params = snd_ivtv_pcm_hw_params,
  260. .hw_free = snd_ivtv_pcm_hw_free,
  261. .prepare = snd_ivtv_pcm_prepare,
  262. .trigger = snd_ivtv_pcm_trigger,
  263. .pointer = snd_ivtv_pcm_pointer,
  264. .page = snd_pcm_get_vmalloc_page,
  265. };
  266. int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
  267. {
  268. struct snd_pcm *sp;
  269. struct snd_card *sc = itvsc->sc;
  270. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  271. struct ivtv *itv = to_ivtv(v4l2_dev);
  272. int ret;
  273. ret = snd_pcm_new(sc, "CX2341[56] PCM",
  274. 0, /* PCM device 0, the only one for this card */
  275. 0, /* 0 playback substreams */
  276. 1, /* 1 capture substream */
  277. &sp);
  278. if (ret) {
  279. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  280. __func__, ret);
  281. goto err_exit;
  282. }
  283. spin_lock_init(&itvsc->slock);
  284. snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
  285. &snd_ivtv_pcm_capture_ops);
  286. sp->info_flags = 0;
  287. sp->private_data = itvsc;
  288. strlcpy(sp->name, itv->card_name, sizeof(sp->name));
  289. return 0;
  290. err_exit:
  291. return ret;
  292. }