bttv-vbi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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) in 1 / sampling_rate:
  28. bt8x8 /HRESET pulse starts at 0H and has length 64 / fCLKx1 (E|O_VTC
  29. HSFMT = 0). VBI_HDELAY (always 0) is an offset from the trailing edge
  30. of /HRESET in 1 / fCLKx1, and the sampling_rate tvnorm->Fsc is fCLKx2. */
  31. #define VBI_OFFSET ((64 + 0) * 2)
  32. #define VBI_DEFLINES 16
  33. #define VBI_MAXLINES 32
  34. static unsigned int vbibufs = 4;
  35. static unsigned int vbi_debug = 0;
  36. module_param(vbibufs, int, 0444);
  37. module_param(vbi_debug, int, 0644);
  38. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
  39. MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
  40. #ifdef dprintk
  41. # undef dprintk
  42. #endif
  43. #define dprintk(fmt, arg...) if (vbi_debug) \
  44. printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
  45. /* ----------------------------------------------------------------------- */
  46. /* vbi risc code + mm */
  47. static int
  48. vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
  49. {
  50. int bpl = 2048;
  51. bttv_risc_packed(btv, &buf->top, buf->vb.dma.sglist,
  52. 0, bpl-4, 4, lines);
  53. bttv_risc_packed(btv, &buf->bottom, buf->vb.dma.sglist,
  54. lines * bpl, bpl-4, 4, lines);
  55. return 0;
  56. }
  57. static int vbi_buffer_setup(struct videobuf_queue *q,
  58. unsigned int *count, unsigned int *size)
  59. {
  60. struct bttv_fh *fh = q->priv_data;
  61. struct bttv *btv = fh->btv;
  62. if (0 == *count)
  63. *count = vbibufs;
  64. *size = fh->lines * 2 * 2048;
  65. dprintk("setup: lines=%d\n",fh->lines);
  66. return 0;
  67. }
  68. static int vbi_buffer_prepare(struct videobuf_queue *q,
  69. struct videobuf_buffer *vb,
  70. enum v4l2_field field)
  71. {
  72. struct bttv_fh *fh = q->priv_data;
  73. struct bttv *btv = fh->btv;
  74. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  75. int rc;
  76. buf->vb.size = fh->lines * 2 * 2048;
  77. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  78. return -EINVAL;
  79. if (STATE_NEEDS_INIT == buf->vb.state) {
  80. if (0 != (rc = videobuf_iolock(btv->c.pci, &buf->vb, NULL)))
  81. goto fail;
  82. if (0 != (rc = vbi_buffer_risc(btv,buf,fh->lines)))
  83. goto fail;
  84. }
  85. buf->vb.state = STATE_PREPARED;
  86. buf->vb.field = field;
  87. dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
  88. vb, &buf->top, &buf->bottom,
  89. v4l2_field_names[buf->vb.field]);
  90. return 0;
  91. fail:
  92. bttv_dma_free(btv,buf);
  93. return rc;
  94. }
  95. static void
  96. vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  97. {
  98. struct bttv_fh *fh = q->priv_data;
  99. struct bttv *btv = fh->btv;
  100. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  101. dprintk("queue %p\n",vb);
  102. buf->vb.state = STATE_QUEUED;
  103. list_add_tail(&buf->vb.queue,&btv->vcapture);
  104. if (NULL == btv->cvbi) {
  105. fh->btv->loop_irq |= 4;
  106. bttv_set_dma(btv,0x0c);
  107. }
  108. }
  109. static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  110. {
  111. struct bttv_fh *fh = q->priv_data;
  112. struct bttv *btv = fh->btv;
  113. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  114. dprintk("free %p\n",vb);
  115. bttv_dma_free(fh->btv,buf);
  116. }
  117. struct videobuf_queue_ops bttv_vbi_qops = {
  118. .buf_setup = vbi_buffer_setup,
  119. .buf_prepare = vbi_buffer_prepare,
  120. .buf_queue = vbi_buffer_queue,
  121. .buf_release = vbi_buffer_release,
  122. };
  123. /* ----------------------------------------------------------------------- */
  124. void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
  125. {
  126. int vdelay;
  127. if (lines < 1)
  128. lines = 1;
  129. if (lines > VBI_MAXLINES)
  130. lines = VBI_MAXLINES;
  131. fh->lines = lines;
  132. vdelay = btread(BT848_E_VDELAY_LO);
  133. if (vdelay < lines*2) {
  134. vdelay = lines*2;
  135. btwrite(vdelay,BT848_E_VDELAY_LO);
  136. btwrite(vdelay,BT848_O_VDELAY_LO);
  137. }
  138. }
  139. void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  140. {
  141. const struct bttv_tvnorm *tvnorm;
  142. s64 count0,count1,count;
  143. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  144. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  145. f->fmt.vbi.sampling_rate = tvnorm->Fsc;
  146. f->fmt.vbi.samples_per_line = 2048;
  147. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  148. f->fmt.vbi.offset = VBI_OFFSET;
  149. f->fmt.vbi.flags = 0;
  150. /* s64 to prevent overflow. */
  151. count0 = (s64) f->fmt.vbi.start[0] + f->fmt.vbi.count[0]
  152. - tvnorm->vbistart[0];
  153. count1 = (s64) f->fmt.vbi.start[1] + f->fmt.vbi.count[1]
  154. - tvnorm->vbistart[1];
  155. count = clamp (max (count0, count1), 1LL, (s64) VBI_MAXLINES);
  156. f->fmt.vbi.start[0] = tvnorm->vbistart[0];
  157. f->fmt.vbi.start[1] = tvnorm->vbistart[1];
  158. f->fmt.vbi.count[0] = count;
  159. f->fmt.vbi.count[1] = count;
  160. f->fmt.vbi.reserved[0] = 0;
  161. f->fmt.vbi.reserved[1] = 0;
  162. }
  163. void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  164. {
  165. const struct bttv_tvnorm *tvnorm;
  166. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  167. memset(f,0,sizeof(*f));
  168. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  169. f->fmt.vbi.sampling_rate = tvnorm->Fsc;
  170. f->fmt.vbi.samples_per_line = 2048;
  171. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  172. f->fmt.vbi.offset = VBI_OFFSET;
  173. f->fmt.vbi.start[0] = tvnorm->vbistart[0];
  174. f->fmt.vbi.start[1] = tvnorm->vbistart[1];
  175. f->fmt.vbi.count[0] = fh->lines;
  176. f->fmt.vbi.count[1] = fh->lines;
  177. f->fmt.vbi.flags = 0;
  178. }
  179. /* ----------------------------------------------------------------------- */
  180. /*
  181. * Local variables:
  182. * c-basic-offset: 8
  183. * End:
  184. */