cx23885-vbi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include "cx23885.h"
  26. static unsigned int vbibufs = 4;
  27. module_param(vbibufs, int, 0644);
  28. MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
  29. static unsigned int vbi_debug;
  30. module_param(vbi_debug, int, 0644);
  31. MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
  32. #define dprintk(level, fmt, arg...)\
  33. do { if (vbi_debug >= level)\
  34. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
  35. } while (0)
  36. /* ------------------------------------------------------------------ */
  37. #define VBI_LINE_LENGTH 1440
  38. #define NTSC_VBI_START_LINE 10 /* line 10 - 21 */
  39. #define NTSC_VBI_END_LINE 21
  40. #define NTSC_VBI_LINES (NTSC_VBI_END_LINE - NTSC_VBI_START_LINE + 1)
  41. int cx23885_vbi_fmt(struct file *file, void *priv,
  42. struct v4l2_format *f)
  43. {
  44. struct cx23885_fh *fh = priv;
  45. struct cx23885_dev *dev = fh->dev;
  46. if (dev->tvnorm & V4L2_STD_525_60) {
  47. /* ntsc */
  48. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  49. f->fmt.vbi.sampling_rate = 27000000;
  50. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  51. f->fmt.vbi.offset = 0;
  52. f->fmt.vbi.flags = 0;
  53. f->fmt.vbi.start[0] = 10;
  54. f->fmt.vbi.count[0] = 17;
  55. f->fmt.vbi.start[1] = 263 + 10 + 1;
  56. f->fmt.vbi.count[1] = 17;
  57. } else if (dev->tvnorm & V4L2_STD_625_50) {
  58. /* pal */
  59. f->fmt.vbi.sampling_rate = 35468950;
  60. f->fmt.vbi.start[0] = 7 - 1;
  61. f->fmt.vbi.start[1] = 319 - 1;
  62. }
  63. return 0;
  64. }
  65. /* We're given the Video Interrupt status register.
  66. * The cx23885_video_irq() func has already validated
  67. * the potential error bits, we just need to
  68. * deal with vbi payload and return indication if
  69. * we actually processed any payload.
  70. */
  71. int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
  72. {
  73. u32 count;
  74. int handled = 0;
  75. if (status & VID_BC_MSK_VBI_RISCI1) {
  76. dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
  77. spin_lock(&dev->slock);
  78. count = cx_read(VID_A_GPCNT);
  79. cx23885_video_wakeup(dev, &dev->vbiq, count);
  80. spin_unlock(&dev->slock);
  81. handled++;
  82. }
  83. if (status & VID_BC_MSK_VBI_RISCI2) {
  84. dprintk(1, "%s() VID_BC_MSK_VBI_RISCI2\n", __func__);
  85. dprintk(2, "stopper vbi\n");
  86. spin_lock(&dev->slock);
  87. cx23885_restart_vbi_queue(dev, &dev->vbiq);
  88. spin_unlock(&dev->slock);
  89. handled++;
  90. }
  91. return handled;
  92. }
  93. static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
  94. struct cx23885_dmaqueue *q,
  95. struct cx23885_buffer *buf)
  96. {
  97. dprintk(1, "%s()\n", __func__);
  98. /* setup fifo + format */
  99. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
  100. buf->vb.width, buf->risc.dma);
  101. /* reset counter */
  102. cx_write(VID_A_GPCNT_CTL, 3);
  103. cx_write(VID_A_VBI_CTRL, 3);
  104. cx_write(VBI_A_GPCNT_CTL, 3);
  105. q->count = 1;
  106. /* enable irq */
  107. cx23885_irq_add_enable(dev, 0x01);
  108. cx_set(VID_A_INT_MSK, 0x000022);
  109. /* start dma */
  110. cx_set(DEV_CNTRL2, (1<<5));
  111. cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
  112. return 0;
  113. }
  114. int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
  115. struct cx23885_dmaqueue *q)
  116. {
  117. struct cx23885_buffer *buf;
  118. struct list_head *item;
  119. if (list_empty(&q->active))
  120. return 0;
  121. buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
  122. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  123. buf, buf->vb.i);
  124. cx23885_start_vbi_dma(dev, q, buf);
  125. list_for_each(item, &q->active) {
  126. buf = list_entry(item, struct cx23885_buffer, vb.queue);
  127. buf->count = q->count++;
  128. }
  129. mod_timer(&q->timeout, jiffies + (BUFFER_TIMEOUT / 30));
  130. return 0;
  131. }
  132. void cx23885_vbi_timeout(unsigned long data)
  133. {
  134. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  135. struct cx23885_dmaqueue *q = &dev->vbiq;
  136. struct cx23885_buffer *buf;
  137. unsigned long flags;
  138. /* Stop the VBI engine */
  139. cx_clear(VID_A_DMA_CTL, 0x22);
  140. spin_lock_irqsave(&dev->slock, flags);
  141. while (!list_empty(&q->active)) {
  142. buf = list_entry(q->active.next, struct cx23885_buffer,
  143. vb.queue);
  144. list_del(&buf->vb.queue);
  145. buf->vb.state = VIDEOBUF_ERROR;
  146. wake_up(&buf->vb.done);
  147. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
  148. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  149. }
  150. cx23885_restart_vbi_queue(dev, q);
  151. spin_unlock_irqrestore(&dev->slock, flags);
  152. }
  153. /* ------------------------------------------------------------------ */
  154. #define VBI_LINE_LENGTH 1440
  155. #define VBI_LINE_COUNT 17
  156. static int
  157. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  158. {
  159. *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  160. if (0 == *count)
  161. *count = vbibufs;
  162. if (*count < 2)
  163. *count = 2;
  164. if (*count > 32)
  165. *count = 32;
  166. return 0;
  167. }
  168. static int
  169. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  170. enum v4l2_field field)
  171. {
  172. struct cx23885_fh *fh = q->priv_data;
  173. struct cx23885_dev *dev = fh->dev;
  174. struct cx23885_buffer *buf = container_of(vb,
  175. struct cx23885_buffer, vb);
  176. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  177. unsigned int size;
  178. int rc;
  179. size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  180. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  181. return -EINVAL;
  182. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  183. buf->vb.width = VBI_LINE_LENGTH;
  184. buf->vb.height = VBI_LINE_COUNT;
  185. buf->vb.size = size;
  186. buf->vb.field = V4L2_FIELD_SEQ_TB;
  187. rc = videobuf_iolock(q, &buf->vb, NULL);
  188. if (0 != rc)
  189. goto fail;
  190. cx23885_risc_vbibuffer(dev->pci, &buf->risc,
  191. dma->sglist,
  192. 0, buf->vb.width * buf->vb.height,
  193. buf->vb.width, 0,
  194. buf->vb.height);
  195. }
  196. buf->vb.state = VIDEOBUF_PREPARED;
  197. return 0;
  198. fail:
  199. cx23885_free_buffer(q, buf);
  200. return rc;
  201. }
  202. static void
  203. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  204. {
  205. struct cx23885_buffer *buf =
  206. container_of(vb, struct cx23885_buffer, vb);
  207. struct cx23885_buffer *prev;
  208. struct cx23885_fh *fh = vq->priv_data;
  209. struct cx23885_dev *dev = fh->dev;
  210. struct cx23885_dmaqueue *q = &dev->vbiq;
  211. /* add jump to stopper */
  212. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  213. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  214. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  215. if (list_empty(&q->active)) {
  216. list_add_tail(&buf->vb.queue, &q->active);
  217. cx23885_start_vbi_dma(dev, q, buf);
  218. buf->vb.state = VIDEOBUF_ACTIVE;
  219. buf->count = q->count++;
  220. mod_timer(&q->timeout, jiffies + (BUFFER_TIMEOUT / 30));
  221. dprintk(2, "[%p/%d] vbi_queue - first active\n",
  222. buf, buf->vb.i);
  223. } else {
  224. prev = list_entry(q->active.prev, struct cx23885_buffer,
  225. vb.queue);
  226. list_add_tail(&buf->vb.queue, &q->active);
  227. buf->vb.state = VIDEOBUF_ACTIVE;
  228. buf->count = q->count++;
  229. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  230. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
  231. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  232. buf, buf->vb.i);
  233. }
  234. }
  235. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  236. {
  237. struct cx23885_buffer *buf =
  238. container_of(vb, struct cx23885_buffer, vb);
  239. cx23885_free_buffer(q, buf);
  240. }
  241. struct videobuf_queue_ops cx23885_vbi_qops = {
  242. .buf_setup = vbi_setup,
  243. .buf_prepare = vbi_prepare,
  244. .buf_queue = vbi_queue,
  245. .buf_release = vbi_release,
  246. };
  247. /* ------------------------------------------------------------------ */
  248. /*
  249. * Local variables:
  250. * c-basic-offset: 8
  251. * End:
  252. */