ivtv-alsa-pcm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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=%zu\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. if (ivtv_init_on_first_open(itv)) {
  139. snd_ivtv_unlock(itvsc);
  140. return -ENXIO;
  141. }
  142. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  143. v4l2_fh_init(&item.fh, &s->vdev);
  144. item.itv = itv;
  145. item.type = s->type;
  146. /* See if the stream is available */
  147. if (ivtv_claim_stream(&item, item.type)) {
  148. /* No, it's already in use */
  149. snd_ivtv_unlock(itvsc);
  150. return -EBUSY;
  151. }
  152. if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
  153. test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  154. /* We're already streaming. No additional action required */
  155. snd_ivtv_unlock(itvsc);
  156. return 0;
  157. }
  158. runtime->hw = snd_ivtv_hw_capture;
  159. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  160. itvsc->capture_pcm_substream = substream;
  161. runtime->private_data = itv;
  162. itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
  163. /* Not currently streaming, so start it up */
  164. set_bit(IVTV_F_S_STREAMING, &s->s_flags);
  165. ret = ivtv_start_v4l2_encode_stream(s);
  166. snd_ivtv_unlock(itvsc);
  167. return ret;
  168. }
  169. static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
  170. {
  171. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  172. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  173. struct ivtv *itv = to_ivtv(v4l2_dev);
  174. struct ivtv_stream *s;
  175. /* Instruct the ivtv to stop sending packets */
  176. snd_ivtv_lock(itvsc);
  177. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  178. ivtv_stop_v4l2_encode_stream(s, 0);
  179. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  180. ivtv_release_stream(s);
  181. itv->pcm_announce_callback = NULL;
  182. snd_ivtv_unlock(itvsc);
  183. return 0;
  184. }
  185. static int snd_ivtv_pcm_ioctl(struct snd_pcm_substream *substream,
  186. unsigned int cmd, void *arg)
  187. {
  188. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  189. int ret;
  190. snd_ivtv_lock(itvsc);
  191. ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  192. snd_ivtv_unlock(itvsc);
  193. return ret;
  194. }
  195. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  196. size_t size)
  197. {
  198. struct snd_pcm_runtime *runtime = subs->runtime;
  199. dprintk("Allocating vbuffer\n");
  200. if (runtime->dma_area) {
  201. if (runtime->dma_bytes > size)
  202. return 0;
  203. vfree(runtime->dma_area);
  204. }
  205. runtime->dma_area = vmalloc(size);
  206. if (!runtime->dma_area)
  207. return -ENOMEM;
  208. runtime->dma_bytes = size;
  209. return 0;
  210. }
  211. static int snd_ivtv_pcm_hw_params(struct snd_pcm_substream *substream,
  212. struct snd_pcm_hw_params *params)
  213. {
  214. dprintk("%s called\n", __func__);
  215. return snd_pcm_alloc_vmalloc_buffer(substream,
  216. params_buffer_bytes(params));
  217. }
  218. static int snd_ivtv_pcm_hw_free(struct snd_pcm_substream *substream)
  219. {
  220. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  221. unsigned long flags;
  222. spin_lock_irqsave(&itvsc->slock, flags);
  223. if (substream->runtime->dma_area) {
  224. dprintk("freeing pcm capture region\n");
  225. vfree(substream->runtime->dma_area);
  226. substream->runtime->dma_area = NULL;
  227. }
  228. spin_unlock_irqrestore(&itvsc->slock, flags);
  229. return 0;
  230. }
  231. static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
  232. {
  233. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  234. itvsc->hwptr_done_capture = 0;
  235. itvsc->capture_transfer_done = 0;
  236. return 0;
  237. }
  238. static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  239. {
  240. return 0;
  241. }
  242. static
  243. snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
  244. {
  245. unsigned long flags;
  246. snd_pcm_uframes_t hwptr_done;
  247. struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
  248. spin_lock_irqsave(&itvsc->slock, flags);
  249. hwptr_done = itvsc->hwptr_done_capture;
  250. spin_unlock_irqrestore(&itvsc->slock, flags);
  251. return hwptr_done;
  252. }
  253. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  254. unsigned long offset)
  255. {
  256. void *pageptr = subs->runtime->dma_area + offset;
  257. return vmalloc_to_page(pageptr);
  258. }
  259. static struct snd_pcm_ops snd_ivtv_pcm_capture_ops = {
  260. .open = snd_ivtv_pcm_capture_open,
  261. .close = snd_ivtv_pcm_capture_close,
  262. .ioctl = snd_ivtv_pcm_ioctl,
  263. .hw_params = snd_ivtv_pcm_hw_params,
  264. .hw_free = snd_ivtv_pcm_hw_free,
  265. .prepare = snd_ivtv_pcm_prepare,
  266. .trigger = snd_ivtv_pcm_trigger,
  267. .pointer = snd_ivtv_pcm_pointer,
  268. .page = snd_pcm_get_vmalloc_page,
  269. };
  270. int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
  271. {
  272. struct snd_pcm *sp;
  273. struct snd_card *sc = itvsc->sc;
  274. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  275. struct ivtv *itv = to_ivtv(v4l2_dev);
  276. int ret;
  277. ret = snd_pcm_new(sc, "CX2341[56] PCM",
  278. 0, /* PCM device 0, the only one for this card */
  279. 0, /* 0 playback substreams */
  280. 1, /* 1 capture substream */
  281. &sp);
  282. if (ret) {
  283. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  284. __func__, ret);
  285. goto err_exit;
  286. }
  287. spin_lock_init(&itvsc->slock);
  288. snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
  289. &snd_ivtv_pcm_capture_ops);
  290. sp->info_flags = 0;
  291. sp->private_data = itvsc;
  292. strlcpy(sp->name, itv->card_name, sizeof(sp->name));
  293. return 0;
  294. err_exit:
  295. return ret;
  296. }