bttv-vbi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. bttv - Bt848 frame grabber driver
  3. vbi interface
  4. (c) 2002 Gerd Knorr <kraxel@bytesex.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kdev_t.h>
  25. #include <asm/io.h>
  26. #include "bttvp.h"
  27. /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
  28. in fCLKx2 pixels. According to the datasheet, VBI capture starts
  29. VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
  30. is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
  31. (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
  32. Just Plain Wrong. The real value appears to be different for
  33. different revisions of the bt8x8 chips, and to be affected by the
  34. horizontal scaling factor. Experimentally, the value is measured
  35. to be about 244. */
  36. #define VBI_OFFSET 244
  37. #define VBI_DEFLINES 16
  38. #define VBI_MAXLINES 32
  39. static unsigned int vbibufs = 4;
  40. static unsigned int vbi_debug = 0;
  41. module_param(vbibufs, int, 0444);
  42. module_param(vbi_debug, int, 0644);
  43. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
  44. MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
  45. #ifdef dprintk
  46. # undef dprintk
  47. #endif
  48. #define dprintk(fmt, arg...) if (vbi_debug) \
  49. printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
  50. /* ----------------------------------------------------------------------- */
  51. /* vbi risc code + mm */
  52. static int
  53. vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
  54. {
  55. int bpl = 2048;
  56. bttv_risc_packed(btv, &buf->top, buf->vb.dma.sglist,
  57. 0, bpl-4, 4, lines);
  58. bttv_risc_packed(btv, &buf->bottom, buf->vb.dma.sglist,
  59. lines * bpl, bpl-4, 4, lines);
  60. return 0;
  61. }
  62. static int vbi_buffer_setup(struct videobuf_queue *q,
  63. unsigned int *count, unsigned int *size)
  64. {
  65. struct bttv_fh *fh = q->priv_data;
  66. struct bttv *btv = fh->btv;
  67. if (0 == *count)
  68. *count = vbibufs;
  69. *size = fh->lines * 2 * 2048;
  70. dprintk("setup: lines=%d\n",fh->lines);
  71. return 0;
  72. }
  73. static int vbi_buffer_prepare(struct videobuf_queue *q,
  74. struct videobuf_buffer *vb,
  75. enum v4l2_field field)
  76. {
  77. struct bttv_fh *fh = q->priv_data;
  78. struct bttv *btv = fh->btv;
  79. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  80. int rc;
  81. buf->vb.size = fh->lines * 2 * 2048;
  82. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  83. return -EINVAL;
  84. if (STATE_NEEDS_INIT == buf->vb.state) {
  85. if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
  86. goto fail;
  87. if (0 != (rc = vbi_buffer_risc(btv,buf,fh->lines)))
  88. goto fail;
  89. }
  90. buf->vb.state = STATE_PREPARED;
  91. buf->vb.field = field;
  92. dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
  93. vb, &buf->top, &buf->bottom,
  94. v4l2_field_names[buf->vb.field]);
  95. return 0;
  96. fail:
  97. bttv_dma_free(q,btv,buf);
  98. return rc;
  99. }
  100. static void
  101. vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  102. {
  103. struct bttv_fh *fh = q->priv_data;
  104. struct bttv *btv = fh->btv;
  105. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  106. dprintk("queue %p\n",vb);
  107. buf->vb.state = STATE_QUEUED;
  108. list_add_tail(&buf->vb.queue,&btv->vcapture);
  109. if (NULL == btv->cvbi) {
  110. fh->btv->loop_irq |= 4;
  111. bttv_set_dma(btv,0x0c);
  112. }
  113. }
  114. static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  115. {
  116. struct bttv_fh *fh = q->priv_data;
  117. struct bttv *btv = fh->btv;
  118. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  119. dprintk("free %p\n",vb);
  120. bttv_dma_free(&fh->cap,fh->btv,buf);
  121. }
  122. struct videobuf_queue_ops bttv_vbi_qops = {
  123. .buf_setup = vbi_buffer_setup,
  124. .buf_prepare = vbi_buffer_prepare,
  125. .buf_queue = vbi_buffer_queue,
  126. .buf_release = vbi_buffer_release,
  127. };
  128. /* ----------------------------------------------------------------------- */
  129. void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
  130. {
  131. int vdelay;
  132. if (lines < 1)
  133. lines = 1;
  134. if (lines > VBI_MAXLINES)
  135. lines = VBI_MAXLINES;
  136. fh->lines = lines;
  137. vdelay = btread(BT848_E_VDELAY_LO);
  138. if (vdelay < lines*2) {
  139. vdelay = lines*2;
  140. btwrite(vdelay,BT848_E_VDELAY_LO);
  141. btwrite(vdelay,BT848_O_VDELAY_LO);
  142. }
  143. }
  144. void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  145. {
  146. const struct bttv_tvnorm *tvnorm;
  147. s64 count0,count1,count;
  148. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  149. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  150. f->fmt.vbi.sampling_rate = tvnorm->Fsc;
  151. f->fmt.vbi.samples_per_line = 2048;
  152. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  153. f->fmt.vbi.offset = VBI_OFFSET;
  154. f->fmt.vbi.flags = 0;
  155. /* s64 to prevent overflow. */
  156. count0 = (s64) f->fmt.vbi.start[0] + f->fmt.vbi.count[0]
  157. - tvnorm->vbistart[0];
  158. count1 = (s64) f->fmt.vbi.start[1] + f->fmt.vbi.count[1]
  159. - tvnorm->vbistart[1];
  160. count = clamp (max (count0, count1), (s64) 1, (s64) VBI_MAXLINES);
  161. f->fmt.vbi.start[0] = tvnorm->vbistart[0];
  162. f->fmt.vbi.start[1] = tvnorm->vbistart[1];
  163. f->fmt.vbi.count[0] = count;
  164. f->fmt.vbi.count[1] = count;
  165. f->fmt.vbi.reserved[0] = 0;
  166. f->fmt.vbi.reserved[1] = 0;
  167. }
  168. void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  169. {
  170. const struct bttv_tvnorm *tvnorm;
  171. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  172. memset(f,0,sizeof(*f));
  173. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  174. f->fmt.vbi.sampling_rate = tvnorm->Fsc;
  175. f->fmt.vbi.samples_per_line = 2048;
  176. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  177. f->fmt.vbi.offset = VBI_OFFSET;
  178. f->fmt.vbi.start[0] = tvnorm->vbistart[0];
  179. f->fmt.vbi.start[1] = tvnorm->vbistart[1];
  180. f->fmt.vbi.count[0] = fh->lines;
  181. f->fmt.vbi.count[1] = fh->lines;
  182. f->fmt.vbi.flags = 0;
  183. }
  184. /* ----------------------------------------------------------------------- */
  185. /*
  186. * Local variables:
  187. * c-basic-offset: 8
  188. * End:
  189. */