saa7134-vbi.c 7.3 KB

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