saa7134-vbi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * $Id: saa7134-vbi.c,v 1.7 2005/05/24 23:13:06 nsh Exp $
  3. *
  4. * device driver for philips saa7134 based TV cards
  5. * video4linux video interface
  6. *
  7. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include "saa7134-reg.h"
  30. #include "saa7134.h"
  31. /* ------------------------------------------------------------------ */
  32. static unsigned int vbi_debug = 0;
  33. module_param(vbi_debug, int, 0644);
  34. MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
  35. static unsigned int vbibufs = 4;
  36. module_param(vbibufs, int, 0444);
  37. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
  38. #define dprintk(fmt, arg...) if (vbi_debug) \
  39. printk(KERN_DEBUG "%s/vbi: " fmt, dev->name , ## arg)
  40. /* ------------------------------------------------------------------ */
  41. #define VBI_LINE_COUNT 16
  42. #define VBI_LINE_LENGTH 2048
  43. #define VBI_SCALE 0x200
  44. static void task_init(struct saa7134_dev *dev, struct saa7134_buf *buf,
  45. int task)
  46. {
  47. struct saa7134_tvnorm *norm = dev->tvnorm;
  48. /* setup video scaler */
  49. saa_writeb(SAA7134_VBI_H_START1(task), norm->h_start & 0xff);
  50. saa_writeb(SAA7134_VBI_H_START2(task), norm->h_start >> 8);
  51. saa_writeb(SAA7134_VBI_H_STOP1(task), norm->h_stop & 0xff);
  52. saa_writeb(SAA7134_VBI_H_STOP2(task), norm->h_stop >> 8);
  53. saa_writeb(SAA7134_VBI_V_START1(task), norm->vbi_v_start_0 & 0xff);
  54. saa_writeb(SAA7134_VBI_V_START2(task), norm->vbi_v_start_0 >> 8);
  55. saa_writeb(SAA7134_VBI_V_STOP1(task), norm->vbi_v_stop_0 & 0xff);
  56. saa_writeb(SAA7134_VBI_V_STOP2(task), norm->vbi_v_stop_0 >> 8);
  57. saa_writeb(SAA7134_VBI_H_SCALE_INC1(task), VBI_SCALE & 0xff);
  58. saa_writeb(SAA7134_VBI_H_SCALE_INC2(task), VBI_SCALE >> 8);
  59. saa_writeb(SAA7134_VBI_PHASE_OFFSET_LUMA(task), 0x00);
  60. saa_writeb(SAA7134_VBI_PHASE_OFFSET_CHROMA(task), 0x00);
  61. saa_writeb(SAA7134_VBI_H_LEN1(task), buf->vb.width & 0xff);
  62. saa_writeb(SAA7134_VBI_H_LEN2(task), buf->vb.width >> 8);
  63. saa_writeb(SAA7134_VBI_V_LEN1(task), buf->vb.height & 0xff);
  64. saa_writeb(SAA7134_VBI_V_LEN2(task), buf->vb.height >> 8);
  65. saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00);
  66. }
  67. /* ------------------------------------------------------------------ */
  68. static int buffer_activate(struct saa7134_dev *dev,
  69. struct saa7134_buf *buf,
  70. struct saa7134_buf *next)
  71. {
  72. unsigned long control,base;
  73. dprintk("buffer_activate [%p]\n",buf);
  74. buf->vb.state = STATE_ACTIVE;
  75. buf->top_seen = 0;
  76. task_init(dev,buf,TASK_A);
  77. task_init(dev,buf,TASK_B);
  78. saa_writeb(SAA7134_OFMT_DATA_A, 0x06);
  79. saa_writeb(SAA7134_OFMT_DATA_B, 0x06);
  80. /* DMA: setup channel 2+3 (= VBI Task A+B) */
  81. base = saa7134_buffer_base(buf);
  82. control = SAA7134_RS_CONTROL_BURST_16 |
  83. SAA7134_RS_CONTROL_ME |
  84. (buf->pt->dma >> 12);
  85. saa_writel(SAA7134_RS_BA1(2),base);
  86. saa_writel(SAA7134_RS_BA2(2),base + buf->vb.size/2);
  87. saa_writel(SAA7134_RS_PITCH(2),buf->vb.width);
  88. saa_writel(SAA7134_RS_CONTROL(2),control);
  89. saa_writel(SAA7134_RS_BA1(3),base);
  90. saa_writel(SAA7134_RS_BA2(3),base + buf->vb.size/2);
  91. saa_writel(SAA7134_RS_PITCH(3),buf->vb.width);
  92. saa_writel(SAA7134_RS_CONTROL(3),control);
  93. /* start DMA */
  94. saa7134_set_dmabits(dev);
  95. mod_timer(&dev->vbi_q.timeout, jiffies+BUFFER_TIMEOUT);
  96. return 0;
  97. }
  98. static int buffer_prepare(struct videobuf_queue *q,
  99. struct videobuf_buffer *vb,
  100. enum v4l2_field field)
  101. {
  102. struct saa7134_fh *fh = q->priv_data;
  103. struct saa7134_dev *dev = fh->dev;
  104. struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
  105. struct saa7134_tvnorm *norm = dev->tvnorm;
  106. unsigned int lines, llength, size;
  107. int err;
  108. lines = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
  109. if (lines > VBI_LINE_COUNT)
  110. lines = VBI_LINE_COUNT;
  111. llength = VBI_LINE_LENGTH;
  112. size = lines * llength * 2;
  113. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  114. return -EINVAL;
  115. if (buf->vb.size != size)
  116. saa7134_dma_free(dev,buf);
  117. if (STATE_NEEDS_INIT == buf->vb.state) {
  118. buf->vb.width = llength;
  119. buf->vb.height = lines;
  120. buf->vb.size = size;
  121. buf->pt = &fh->pt_vbi;
  122. err = videobuf_iolock(dev->pci,&buf->vb,NULL);
  123. if (err)
  124. goto oops;
  125. err = saa7134_pgtable_build(dev->pci,buf->pt,
  126. buf->vb.dma.sglist,
  127. buf->vb.dma.sglen,
  128. saa7134_buffer_startpage(buf));
  129. if (err)
  130. goto oops;
  131. }
  132. buf->vb.state = STATE_PREPARED;
  133. buf->activate = buffer_activate;
  134. buf->vb.field = field;
  135. return 0;
  136. oops:
  137. saa7134_dma_free(dev,buf);
  138. return err;
  139. }
  140. static int
  141. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  142. {
  143. struct saa7134_fh *fh = q->priv_data;
  144. struct saa7134_dev *dev = fh->dev;
  145. int llength,lines;
  146. lines = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 +1;
  147. llength = VBI_LINE_LENGTH;
  148. *size = lines * llength * 2;
  149. if (0 == *count)
  150. *count = vbibufs;
  151. *count = saa7134_buffer_count(*size,*count);
  152. return 0;
  153. }
  154. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  155. {
  156. struct saa7134_fh *fh = q->priv_data;
  157. struct saa7134_dev *dev = fh->dev;
  158. struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
  159. saa7134_buffer_queue(dev,&dev->vbi_q,buf);
  160. }
  161. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  162. {
  163. struct saa7134_fh *fh = q->priv_data;
  164. struct saa7134_dev *dev = fh->dev;
  165. struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
  166. saa7134_dma_free(dev,buf);
  167. }
  168. struct videobuf_queue_ops saa7134_vbi_qops = {
  169. .buf_setup = buffer_setup,
  170. .buf_prepare = buffer_prepare,
  171. .buf_queue = buffer_queue,
  172. .buf_release = buffer_release,
  173. };
  174. /* ------------------------------------------------------------------ */
  175. int saa7134_vbi_init1(struct saa7134_dev *dev)
  176. {
  177. INIT_LIST_HEAD(&dev->vbi_q.queue);
  178. init_timer(&dev->vbi_q.timeout);
  179. dev->vbi_q.timeout.function = saa7134_buffer_timeout;
  180. dev->vbi_q.timeout.data = (unsigned long)(&dev->vbi_q);
  181. dev->vbi_q.dev = dev;
  182. if (vbibufs < 2)
  183. vbibufs = 2;
  184. if (vbibufs > VIDEO_MAX_FRAME)
  185. vbibufs = VIDEO_MAX_FRAME;
  186. return 0;
  187. }
  188. int saa7134_vbi_fini(struct saa7134_dev *dev)
  189. {
  190. /* nothing */
  191. return 0;
  192. }
  193. void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
  194. {
  195. spin_lock(&dev->slock);
  196. if (dev->vbi_q.curr) {
  197. dev->vbi_fieldcount++;
  198. /* make sure we have seen both fields */
  199. if ((status & 0x10) == 0x00) {
  200. dev->vbi_q.curr->top_seen = 1;
  201. goto done;
  202. }
  203. if (!dev->vbi_q.curr->top_seen)
  204. goto done;
  205. dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount;
  206. saa7134_buffer_finish(dev,&dev->vbi_q,STATE_DONE);
  207. }
  208. saa7134_buffer_next(dev,&dev->vbi_q);
  209. done:
  210. spin_unlock(&dev->slock);
  211. }
  212. /* ----------------------------------------------------------- */
  213. /*
  214. * Local variables:
  215. * c-basic-offset: 8
  216. * End:
  217. */