cx88-vbi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/moduleparam.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include "cx88.h"
  9. static unsigned int vbibufs = 4;
  10. module_param(vbibufs,int,0644);
  11. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
  12. static unsigned int vbi_debug = 0;
  13. module_param(vbi_debug,int,0644);
  14. MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
  15. #define dprintk(level,fmt, arg...) if (vbi_debug >= level) \
  16. printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
  17. /* ------------------------------------------------------------------ */
  18. int cx8800_vbi_fmt (struct file *file, void *priv,
  19. struct v4l2_format *f)
  20. {
  21. struct cx8800_fh *fh = priv;
  22. struct cx8800_dev *dev = fh->dev;
  23. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  24. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  25. f->fmt.vbi.offset = 244;
  26. f->fmt.vbi.count[0] = VBI_LINE_COUNT;
  27. f->fmt.vbi.count[1] = VBI_LINE_COUNT;
  28. if (dev->core->tvnorm & V4L2_STD_525_60) {
  29. /* ntsc */
  30. f->fmt.vbi.sampling_rate = 28636363;
  31. f->fmt.vbi.start[0] = 10;
  32. f->fmt.vbi.start[1] = 273;
  33. } else if (dev->core->tvnorm & V4L2_STD_625_50) {
  34. /* pal */
  35. f->fmt.vbi.sampling_rate = 35468950;
  36. f->fmt.vbi.start[0] = 7 -1;
  37. f->fmt.vbi.start[1] = 319 -1;
  38. }
  39. return 0;
  40. }
  41. static int cx8800_start_vbi_dma(struct cx8800_dev *dev,
  42. struct cx88_dmaqueue *q,
  43. struct cx88_buffer *buf)
  44. {
  45. struct cx88_core *core = dev->core;
  46. /* setup fifo + format */
  47. cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH24],
  48. buf->vb.width, buf->risc.dma);
  49. cx_write(MO_VBOS_CONTROL, ( (1 << 18) | // comb filter delay fixup
  50. (1 << 15) | // enable vbi capture
  51. (1 << 11) ));
  52. /* reset counter */
  53. cx_write(MO_VBI_GPCNTRL, GP_COUNT_CONTROL_RESET);
  54. q->count = 1;
  55. /* enable irqs */
  56. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
  57. cx_set(MO_VID_INTMSK, 0x0f0088);
  58. /* enable capture */
  59. cx_set(VID_CAPTURE_CONTROL,0x18);
  60. /* start dma */
  61. cx_set(MO_DEV_CNTRL2, (1<<5));
  62. cx_set(MO_VID_DMACNTRL, 0x88);
  63. return 0;
  64. }
  65. int cx8800_stop_vbi_dma(struct cx8800_dev *dev)
  66. {
  67. struct cx88_core *core = dev->core;
  68. /* stop dma */
  69. cx_clear(MO_VID_DMACNTRL, 0x88);
  70. /* disable capture */
  71. cx_clear(VID_CAPTURE_CONTROL,0x18);
  72. /* disable irqs */
  73. cx_clear(MO_PCI_INTMSK, 0x000001);
  74. cx_clear(MO_VID_INTMSK, 0x0f0088);
  75. return 0;
  76. }
  77. int cx8800_restart_vbi_queue(struct cx8800_dev *dev,
  78. struct cx88_dmaqueue *q)
  79. {
  80. struct cx88_buffer *buf;
  81. struct list_head *item;
  82. if (list_empty(&q->active))
  83. return 0;
  84. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  85. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  86. buf, buf->vb.i);
  87. cx8800_start_vbi_dma(dev, q, buf);
  88. list_for_each(item,&q->active) {
  89. buf = list_entry(item, struct cx88_buffer, vb.queue);
  90. buf->count = q->count++;
  91. }
  92. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  93. return 0;
  94. }
  95. void cx8800_vbi_timeout(unsigned long data)
  96. {
  97. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  98. struct cx88_core *core = dev->core;
  99. struct cx88_dmaqueue *q = &dev->vbiq;
  100. struct cx88_buffer *buf;
  101. unsigned long flags;
  102. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH24]);
  103. cx_clear(MO_VID_DMACNTRL, 0x88);
  104. cx_clear(VID_CAPTURE_CONTROL, 0x18);
  105. spin_lock_irqsave(&dev->slock,flags);
  106. while (!list_empty(&q->active)) {
  107. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  108. list_del(&buf->vb.queue);
  109. buf->vb.state = STATE_ERROR;
  110. wake_up(&buf->vb.done);
  111. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name,
  112. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  113. }
  114. cx8800_restart_vbi_queue(dev,q);
  115. spin_unlock_irqrestore(&dev->slock,flags);
  116. }
  117. /* ------------------------------------------------------------------ */
  118. static int
  119. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  120. {
  121. *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  122. if (0 == *count)
  123. *count = vbibufs;
  124. if (*count < 2)
  125. *count = 2;
  126. if (*count > 32)
  127. *count = 32;
  128. return 0;
  129. }
  130. static int
  131. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  132. enum v4l2_field field)
  133. {
  134. struct cx8800_fh *fh = q->priv_data;
  135. struct cx8800_dev *dev = fh->dev;
  136. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  137. unsigned int size;
  138. int rc;
  139. size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  140. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  141. return -EINVAL;
  142. if (STATE_NEEDS_INIT == buf->vb.state) {
  143. buf->vb.width = VBI_LINE_LENGTH;
  144. buf->vb.height = VBI_LINE_COUNT;
  145. buf->vb.size = size;
  146. buf->vb.field = V4L2_FIELD_SEQ_TB;
  147. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  148. goto fail;
  149. cx88_risc_buffer(dev->pci, &buf->risc,
  150. buf->vb.dma.sglist,
  151. 0, buf->vb.width * buf->vb.height,
  152. buf->vb.width, 0,
  153. buf->vb.height);
  154. }
  155. buf->vb.state = STATE_PREPARED;
  156. return 0;
  157. fail:
  158. cx88_free_buffer(q,buf);
  159. return rc;
  160. }
  161. static void
  162. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  163. {
  164. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  165. struct cx88_buffer *prev;
  166. struct cx8800_fh *fh = vq->priv_data;
  167. struct cx8800_dev *dev = fh->dev;
  168. struct cx88_dmaqueue *q = &dev->vbiq;
  169. /* add jump to stopper */
  170. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  171. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  172. if (list_empty(&q->active)) {
  173. list_add_tail(&buf->vb.queue,&q->active);
  174. cx8800_start_vbi_dma(dev, q, buf);
  175. buf->vb.state = STATE_ACTIVE;
  176. buf->count = q->count++;
  177. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  178. dprintk(2,"[%p/%d] vbi_queue - first active\n",
  179. buf, buf->vb.i);
  180. } else {
  181. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  182. list_add_tail(&buf->vb.queue,&q->active);
  183. buf->vb.state = STATE_ACTIVE;
  184. buf->count = q->count++;
  185. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  186. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  187. buf, buf->vb.i);
  188. }
  189. }
  190. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  191. {
  192. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  193. cx88_free_buffer(q,buf);
  194. }
  195. struct videobuf_queue_ops cx8800_vbi_qops = {
  196. .buf_setup = vbi_setup,
  197. .buf_prepare = vbi_prepare,
  198. .buf_queue = vbi_queue,
  199. .buf_release = vbi_release,
  200. };
  201. /* ------------------------------------------------------------------ */
  202. /*
  203. * Local variables:
  204. * c-basic-offset: 8
  205. * End:
  206. */