ivtv-alsa-pcm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #include "ivtv-driver.h"
  21. #include "ivtv-queue.h"
  22. #include "ivtv-streams.h"
  23. #include "ivtv-fileops.h"
  24. #include "ivtv-alsa.h"
  25. #include "ivtv-alsa-pcm.h"
  26. #include <linux/vmalloc.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. static unsigned int pcm_debug;
  30. module_param(pcm_debug, int, 0644);
  31. MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
  32. #define dprintk(fmt, arg...) \
  33. do { \
  34. if (pcm_debug) \
  35. pr_info("ivtv-alsa-pcm %s: " fmt, __func__, ##arg); \
  36. } while (0)
  37. static const struct snd_pcm_hardware snd_ivtv_hw_capture = {
  38. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  39. SNDRV_PCM_INFO_MMAP |
  40. SNDRV_PCM_INFO_INTERLEAVED |
  41. SNDRV_PCM_INFO_MMAP_VALID,
  42. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  43. .rates = SNDRV_PCM_RATE_48000,
  44. .rate_min = 48000,
  45. .rate_max = 48000,
  46. .channels_min = 2,
  47. .channels_max = 2,
  48. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  49. .period_bytes_min = 64, /* 12544/2, */
  50. .period_bytes_max = 12544,
  51. .periods_min = 2,
  52. .periods_max = 98, /* 12544, */
  53. };
  54. static void ivtv_alsa_announce_pcm_data(struct snd_ivtv_card *itvsc,
  55. u8 *pcm_data,
  56. size_t num_bytes)
  57. {
  58. struct snd_pcm_substream *substream;
  59. struct snd_pcm_runtime *runtime;
  60. unsigned int oldptr;
  61. unsigned int stride;
  62. int period_elapsed = 0;
  63. int length;
  64. dprintk("ivtv alsa announce ptr=%p data=%p num_bytes=%zu\n", itvsc,
  65. pcm_data, num_bytes);
  66. substream = itvsc->capture_pcm_substream;
  67. if (substream == NULL) {
  68. dprintk("substream was NULL\n");
  69. return;
  70. }
  71. runtime = substream->runtime;
  72. if (runtime == NULL) {
  73. dprintk("runtime was NULL\n");
  74. return;
  75. }
  76. stride = runtime->frame_bits >> 3;
  77. if (stride == 0) {
  78. dprintk("stride is zero\n");
  79. return;
  80. }
  81. length = num_bytes / stride;
  82. if (length == 0) {
  83. dprintk("%s: length was zero\n", __func__);
  84. return;
  85. }
  86. if (runtime->dma_area == NULL) {
  87. dprintk("dma area was NULL - ignoring\n");
  88. return;
  89. }
  90. oldptr = itvsc->hwptr_done_capture;
  91. if (oldptr + length >= runtime->buffer_size) {
  92. unsigned int cnt =
  93. runtime->buffer_size - oldptr;
  94. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  95. cnt * stride);
  96. memcpy(runtime->dma_area, pcm_data + cnt * stride,
  97. length * stride - cnt * stride);
  98. } else {
  99. memcpy(runtime->dma_area + oldptr * stride, pcm_data,
  100. length * stride);
  101. }
  102. snd_pcm_stream_lock(substream);
  103. itvsc->hwptr_done_capture += length;
  104. if (itvsc->hwptr_done_capture >=
  105. runtime->buffer_size)
  106. itvsc->hwptr_done_capture -=
  107. runtime->buffer_size;
  108. itvsc->capture_transfer_done += length;
  109. if (itvsc->capture_transfer_done >=
  110. runtime->period_size) {
  111. itvsc->capture_transfer_done -=
  112. runtime->period_size;
  113. period_elapsed = 1;
  114. }
  115. snd_pcm_stream_unlock(substream);
  116. if (period_elapsed)
  117. snd_pcm_period_elapsed(substream);
  118. }
  119. static int snd_ivtv_pcm_capture_open(struct snd_pcm_substream *substream)
  120. {
  121. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  122. struct snd_pcm_runtime *runtime = substream->runtime;
  123. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  124. struct ivtv *itv = to_ivtv(v4l2_dev);
  125. struct ivtv_stream *s;
  126. struct ivtv_open_id item;
  127. int ret;
  128. /* Instruct the CX2341[56] to start sending packets */
  129. snd_ivtv_lock(itvsc);
  130. if (ivtv_init_on_first_open(itv)) {
  131. snd_ivtv_unlock(itvsc);
  132. return -ENXIO;
  133. }
  134. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  135. v4l2_fh_init(&item.fh, &s->vdev);
  136. item.itv = itv;
  137. item.type = s->type;
  138. /* See if the stream is available */
  139. if (ivtv_claim_stream(&item, item.type)) {
  140. /* No, it's already in use */
  141. v4l2_fh_exit(&item.fh);
  142. snd_ivtv_unlock(itvsc);
  143. return -EBUSY;
  144. }
  145. if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
  146. test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  147. /* We're already streaming. No additional action required */
  148. snd_ivtv_unlock(itvsc);
  149. return 0;
  150. }
  151. runtime->hw = snd_ivtv_hw_capture;
  152. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  153. itvsc->capture_pcm_substream = substream;
  154. runtime->private_data = itv;
  155. itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
  156. /* Not currently streaming, so start it up */
  157. set_bit(IVTV_F_S_STREAMING, &s->s_flags);
  158. ret = ivtv_start_v4l2_encode_stream(s);
  159. snd_ivtv_unlock(itvsc);
  160. return ret;
  161. }
  162. static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
  163. {
  164. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  165. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  166. struct ivtv *itv = to_ivtv(v4l2_dev);
  167. struct ivtv_stream *s;
  168. /* Instruct the ivtv to stop sending packets */
  169. snd_ivtv_lock(itvsc);
  170. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  171. ivtv_stop_v4l2_encode_stream(s, 0);
  172. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  173. ivtv_release_stream(s);
  174. itv->pcm_announce_callback = NULL;
  175. snd_ivtv_unlock(itvsc);
  176. return 0;
  177. }
  178. static int snd_ivtv_pcm_ioctl(struct snd_pcm_substream *substream,
  179. unsigned int cmd, void *arg)
  180. {
  181. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  182. int ret;
  183. snd_ivtv_lock(itvsc);
  184. ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  185. snd_ivtv_unlock(itvsc);
  186. return ret;
  187. }
  188. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  189. size_t size)
  190. {
  191. struct snd_pcm_runtime *runtime = subs->runtime;
  192. dprintk("Allocating vbuffer\n");
  193. if (runtime->dma_area) {
  194. if (runtime->dma_bytes > size)
  195. return 0;
  196. vfree(runtime->dma_area);
  197. }
  198. runtime->dma_area = vmalloc(size);
  199. if (!runtime->dma_area)
  200. return -ENOMEM;
  201. runtime->dma_bytes = size;
  202. return 0;
  203. }
  204. static int snd_ivtv_pcm_hw_params(struct snd_pcm_substream *substream,
  205. struct snd_pcm_hw_params *params)
  206. {
  207. dprintk("%s called\n", __func__);
  208. return snd_pcm_alloc_vmalloc_buffer(substream,
  209. params_buffer_bytes(params));
  210. }
  211. static int snd_ivtv_pcm_hw_free(struct snd_pcm_substream *substream)
  212. {
  213. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  214. unsigned long flags;
  215. unsigned char *dma_area = NULL;
  216. spin_lock_irqsave(&itvsc->slock, flags);
  217. if (substream->runtime->dma_area) {
  218. dprintk("freeing pcm capture region\n");
  219. dma_area = substream->runtime->dma_area;
  220. substream->runtime->dma_area = NULL;
  221. }
  222. spin_unlock_irqrestore(&itvsc->slock, flags);
  223. vfree(dma_area);
  224. return 0;
  225. }
  226. static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
  227. {
  228. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  229. itvsc->hwptr_done_capture = 0;
  230. itvsc->capture_transfer_done = 0;
  231. return 0;
  232. }
  233. static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  234. {
  235. return 0;
  236. }
  237. static
  238. snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
  239. {
  240. unsigned long flags;
  241. snd_pcm_uframes_t hwptr_done;
  242. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  243. spin_lock_irqsave(&itvsc->slock, flags);
  244. hwptr_done = itvsc->hwptr_done_capture;
  245. spin_unlock_irqrestore(&itvsc->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_ivtv_pcm_capture_ops = {
  255. .open = snd_ivtv_pcm_capture_open,
  256. .close = snd_ivtv_pcm_capture_close,
  257. .ioctl = snd_ivtv_pcm_ioctl,
  258. .hw_params = snd_ivtv_pcm_hw_params,
  259. .hw_free = snd_ivtv_pcm_hw_free,
  260. .prepare = snd_ivtv_pcm_prepare,
  261. .trigger = snd_ivtv_pcm_trigger,
  262. .pointer = snd_ivtv_pcm_pointer,
  263. .page = snd_pcm_get_vmalloc_page,
  264. };
  265. int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
  266. {
  267. struct snd_pcm *sp;
  268. struct snd_card *sc = itvsc->sc;
  269. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  270. struct ivtv *itv = to_ivtv(v4l2_dev);
  271. int ret;
  272. ret = snd_pcm_new(sc, "CX2341[56] 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. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  279. __func__, ret);
  280. goto err_exit;
  281. }
  282. spin_lock_init(&itvsc->slock);
  283. snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
  284. &snd_ivtv_pcm_capture_ops);
  285. sp->info_flags = 0;
  286. sp->private_data = itvsc;
  287. strlcpy(sp->name, itv->card_name, sizeof(sp->name));
  288. return 0;
  289. err_exit:
  290. return ret;
  291. }