saa7134-vbi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #if 1
  112. llength = VBI_LINE_LENGTH;
  113. #else
  114. llength = (norm->h_stop - norm->h_start +1) * 2;
  115. if (llength > VBI_LINE_LENGTH)
  116. llength = VBI_LINE_LENGTH;
  117. #endif
  118. size = lines * llength * 2;
  119. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  120. return -EINVAL;
  121. if (buf->vb.size != size)
  122. saa7134_dma_free(dev,buf);
  123. if (STATE_NEEDS_INIT == buf->vb.state) {
  124. buf->vb.width = llength;
  125. buf->vb.height = lines;
  126. buf->vb.size = size;
  127. buf->pt = &fh->pt_vbi;
  128. err = videobuf_iolock(dev->pci,&buf->vb,NULL);
  129. if (err)
  130. goto oops;
  131. err = saa7134_pgtable_build(dev->pci,buf->pt,
  132. buf->vb.dma.sglist,
  133. buf->vb.dma.sglen,
  134. saa7134_buffer_startpage(buf));
  135. if (err)
  136. goto oops;
  137. }
  138. buf->vb.state = STATE_PREPARED;
  139. buf->activate = buffer_activate;
  140. buf->vb.field = field;
  141. return 0;
  142. oops:
  143. saa7134_dma_free(dev,buf);
  144. return err;
  145. }
  146. static int
  147. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  148. {
  149. struct saa7134_fh *fh = q->priv_data;
  150. struct saa7134_dev *dev = fh->dev;
  151. int llength,lines;
  152. lines = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 +1;
  153. #if 1
  154. llength = VBI_LINE_LENGTH;
  155. #else
  156. llength = (norm->h_stop - norm->h_start +1) * 2;
  157. if (llength > VBI_LINE_LENGTH)
  158. llength = VBI_LINE_LENGTH;
  159. #endif
  160. *size = lines * llength * 2;
  161. if (0 == *count)
  162. *count = vbibufs;
  163. *count = saa7134_buffer_count(*size,*count);
  164. return 0;
  165. }
  166. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  167. {
  168. struct saa7134_fh *fh = q->priv_data;
  169. struct saa7134_dev *dev = fh->dev;
  170. struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
  171. saa7134_buffer_queue(dev,&dev->vbi_q,buf);
  172. }
  173. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  174. {
  175. struct saa7134_fh *fh = q->priv_data;
  176. struct saa7134_dev *dev = fh->dev;
  177. struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
  178. saa7134_dma_free(dev,buf);
  179. }
  180. struct videobuf_queue_ops saa7134_vbi_qops = {
  181. .buf_setup = buffer_setup,
  182. .buf_prepare = buffer_prepare,
  183. .buf_queue = buffer_queue,
  184. .buf_release = buffer_release,
  185. };
  186. /* ------------------------------------------------------------------ */
  187. int saa7134_vbi_init1(struct saa7134_dev *dev)
  188. {
  189. INIT_LIST_HEAD(&dev->vbi_q.queue);
  190. init_timer(&dev->vbi_q.timeout);
  191. dev->vbi_q.timeout.function = saa7134_buffer_timeout;
  192. dev->vbi_q.timeout.data = (unsigned long)(&dev->vbi_q);
  193. dev->vbi_q.dev = dev;
  194. if (vbibufs < 2)
  195. vbibufs = 2;
  196. if (vbibufs > VIDEO_MAX_FRAME)
  197. vbibufs = VIDEO_MAX_FRAME;
  198. return 0;
  199. }
  200. int saa7134_vbi_fini(struct saa7134_dev *dev)
  201. {
  202. /* nothing */
  203. return 0;
  204. }
  205. void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
  206. {
  207. spin_lock(&dev->slock);
  208. if (dev->vbi_q.curr) {
  209. dev->vbi_fieldcount++;
  210. /* make sure we have seen both fields */
  211. if ((status & 0x10) == 0x00) {
  212. dev->vbi_q.curr->top_seen = 1;
  213. goto done;
  214. }
  215. if (!dev->vbi_q.curr->top_seen)
  216. goto done;
  217. dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount;
  218. saa7134_buffer_finish(dev,&dev->vbi_q,STATE_DONE);
  219. }
  220. saa7134_buffer_next(dev,&dev->vbi_q);
  221. done:
  222. spin_unlock(&dev->slock);
  223. }
  224. /* ----------------------------------------------------------- */
  225. /*
  226. * Local variables:
  227. * c-basic-offset: 8
  228. * End:
  229. */