cx23885-vbi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
  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 <linux/slab.h>
  26. #include "cx23885.h"
  27. static unsigned int vbibufs = 4;
  28. module_param(vbibufs, int, 0644);
  29. MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
  30. static unsigned int vbi_debug;
  31. module_param(vbi_debug, int, 0644);
  32. MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
  33. #define dprintk(level, fmt, arg...)\
  34. do { if (vbi_debug >= level)\
  35. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
  36. } while (0)
  37. /* ------------------------------------------------------------------ */
  38. int cx23885_vbi_fmt(struct file *file, void *priv,
  39. struct v4l2_format *f)
  40. {
  41. struct cx23885_fh *fh = priv;
  42. struct cx23885_dev *dev = fh->dev;
  43. if (dev->tvnorm & V4L2_STD_525_60) {
  44. /* ntsc */
  45. f->fmt.vbi.sampling_rate = 28636363;
  46. f->fmt.vbi.start[0] = 10;
  47. f->fmt.vbi.start[1] = 273;
  48. } else if (dev->tvnorm & V4L2_STD_625_50) {
  49. /* pal */
  50. f->fmt.vbi.sampling_rate = 35468950;
  51. f->fmt.vbi.start[0] = 7 - 1;
  52. f->fmt.vbi.start[1] = 319 - 1;
  53. }
  54. return 0;
  55. }
  56. static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
  57. struct cx23885_dmaqueue *q,
  58. struct cx23885_buffer *buf)
  59. {
  60. /* setup fifo + format */
  61. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
  62. buf->vb.width, buf->risc.dma);
  63. /* reset counter */
  64. q->count = 1;
  65. /* enable irqs */
  66. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  67. cx_set(VID_A_INT_MSK, 0x000022);
  68. /* start dma */
  69. cx_set(DEV_CNTRL2, (1<<5));
  70. cx_set(VID_A_DMA_CTL, 0x00000022);
  71. return 0;
  72. }
  73. int cx23885_stop_vbi_dma(struct cx23885_dev *dev)
  74. {
  75. /* stop dma */
  76. cx_clear(VID_A_DMA_CTL, 0x00000022);
  77. /* disable irqs */
  78. cx_clear(PCI_INT_MSK, 0x000001);
  79. cx_clear(VID_A_INT_MSK, 0x00000022);
  80. return 0;
  81. }
  82. int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
  83. struct cx23885_dmaqueue *q)
  84. {
  85. struct cx23885_buffer *buf;
  86. struct list_head *item;
  87. if (list_empty(&q->active))
  88. return 0;
  89. buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
  90. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  91. buf, buf->vb.i);
  92. cx23885_start_vbi_dma(dev, q, buf);
  93. list_for_each(item, &q->active) {
  94. buf = list_entry(item, struct cx23885_buffer, vb.queue);
  95. buf->count = q->count++;
  96. }
  97. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  98. return 0;
  99. }
  100. void cx23885_vbi_timeout(unsigned long data)
  101. {
  102. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  103. struct cx23885_dmaqueue *q = &dev->vbiq;
  104. struct cx23885_buffer *buf;
  105. unsigned long flags;
  106. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
  107. cx_clear(VID_A_DMA_CTL, 0x22);
  108. spin_lock_irqsave(&dev->slock, flags);
  109. while (!list_empty(&q->active)) {
  110. buf = list_entry(q->active.next, struct cx23885_buffer,
  111. vb.queue);
  112. list_del(&buf->vb.queue);
  113. buf->vb.state = VIDEOBUF_ERROR;
  114. wake_up(&buf->vb.done);
  115. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
  116. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  117. }
  118. cx23885_restart_vbi_queue(dev, q);
  119. spin_unlock_irqrestore(&dev->slock, flags);
  120. }
  121. /* ------------------------------------------------------------------ */
  122. #define VBI_LINE_LENGTH 2048
  123. #define VBI_LINE_COUNT 17
  124. static int
  125. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  126. {
  127. *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  128. if (0 == *count)
  129. *count = vbibufs;
  130. if (*count < 2)
  131. *count = 2;
  132. if (*count > 32)
  133. *count = 32;
  134. return 0;
  135. }
  136. static int
  137. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  138. enum v4l2_field field)
  139. {
  140. struct cx23885_fh *fh = q->priv_data;
  141. struct cx23885_dev *dev = fh->dev;
  142. struct cx23885_buffer *buf = container_of(vb,
  143. struct cx23885_buffer, vb);
  144. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  145. unsigned int size;
  146. int rc;
  147. size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  148. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  149. return -EINVAL;
  150. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  151. buf->vb.width = VBI_LINE_LENGTH;
  152. buf->vb.height = VBI_LINE_COUNT;
  153. buf->vb.size = size;
  154. buf->vb.field = V4L2_FIELD_SEQ_TB;
  155. rc = videobuf_iolock(q, &buf->vb, NULL);
  156. if (0 != rc)
  157. goto fail;
  158. cx23885_risc_buffer(dev->pci, &buf->risc,
  159. dma->sglist,
  160. 0, buf->vb.width * buf->vb.height,
  161. buf->vb.width, 0,
  162. buf->vb.height);
  163. }
  164. buf->vb.state = VIDEOBUF_PREPARED;
  165. return 0;
  166. fail:
  167. cx23885_free_buffer(q, buf);
  168. return rc;
  169. }
  170. static void
  171. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  172. {
  173. struct cx23885_buffer *buf =
  174. container_of(vb, struct cx23885_buffer, vb);
  175. struct cx23885_buffer *prev;
  176. struct cx23885_fh *fh = vq->priv_data;
  177. struct cx23885_dev *dev = fh->dev;
  178. struct cx23885_dmaqueue *q = &dev->vbiq;
  179. /* add jump to stopper */
  180. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  181. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  182. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  183. if (list_empty(&q->active)) {
  184. list_add_tail(&buf->vb.queue, &q->active);
  185. cx23885_start_vbi_dma(dev, q, buf);
  186. buf->vb.state = VIDEOBUF_ACTIVE;
  187. buf->count = q->count++;
  188. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  189. dprintk(2, "[%p/%d] vbi_queue - first active\n",
  190. buf, buf->vb.i);
  191. } else {
  192. prev = list_entry(q->active.prev, struct cx23885_buffer,
  193. vb.queue);
  194. list_add_tail(&buf->vb.queue, &q->active);
  195. buf->vb.state = VIDEOBUF_ACTIVE;
  196. buf->count = q->count++;
  197. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  198. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
  199. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  200. buf, buf->vb.i);
  201. }
  202. }
  203. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  204. {
  205. struct cx23885_buffer *buf =
  206. container_of(vb, struct cx23885_buffer, vb);
  207. cx23885_free_buffer(q, buf);
  208. }
  209. struct videobuf_queue_ops cx23885_vbi_qops = {
  210. .buf_setup = vbi_setup,
  211. .buf_prepare = vbi_prepare,
  212. .buf_queue = vbi_queue,
  213. .buf_release = vbi_release,
  214. };
  215. /* ------------------------------------------------------------------ */
  216. /*
  217. * Local variables:
  218. * c-basic-offset: 8
  219. * End:
  220. */