bttv-vbi.c 6.1 KB

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