bttv-vbi.c 6.1 KB

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