cx23885-vbi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  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. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include "cx23885.h"
  26. static unsigned int vbibufs = 4;
  27. module_param(vbibufs, int, 0644);
  28. MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
  29. static unsigned int vbi_debug;
  30. module_param(vbi_debug, int, 0644);
  31. MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
  32. #define dprintk(level, fmt, arg...)\
  33. do { if (vbi_debug >= level)\
  34. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
  35. } while (0)
  36. /* ------------------------------------------------------------------ */
  37. int cx23885_vbi_fmt(struct file *file, void *priv,
  38. struct v4l2_format *f)
  39. {
  40. struct cx23885_fh *fh = priv;
  41. struct cx23885_dev *dev = fh->dev;
  42. if (dev->tvnorm & V4L2_STD_525_60) {
  43. /* ntsc */
  44. f->fmt.vbi.sampling_rate = 28636363;
  45. f->fmt.vbi.start[0] = 10;
  46. f->fmt.vbi.start[1] = 273;
  47. } else if (dev->tvnorm & V4L2_STD_625_50) {
  48. /* pal */
  49. f->fmt.vbi.sampling_rate = 35468950;
  50. f->fmt.vbi.start[0] = 7 - 1;
  51. f->fmt.vbi.start[1] = 319 - 1;
  52. }
  53. return 0;
  54. }
  55. static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
  56. struct cx23885_dmaqueue *q,
  57. struct cx23885_buffer *buf)
  58. {
  59. /* setup fifo + format */
  60. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
  61. buf->vb.width, buf->risc.dma);
  62. /* reset counter */
  63. q->count = 1;
  64. /* enable irqs */
  65. cx23885_irq_add_enable(dev, 0x01);
  66. cx_set(VID_A_INT_MSK, 0x000022);
  67. /* start dma */
  68. cx_set(DEV_CNTRL2, (1<<5));
  69. cx_set(VID_A_DMA_CTL, 0x00000022);
  70. return 0;
  71. }
  72. static int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
  73. struct cx23885_dmaqueue *q)
  74. {
  75. struct cx23885_buffer *buf;
  76. struct list_head *item;
  77. if (list_empty(&q->active))
  78. return 0;
  79. buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
  80. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  81. buf, buf->vb.i);
  82. cx23885_start_vbi_dma(dev, q, buf);
  83. list_for_each(item, &q->active) {
  84. buf = list_entry(item, struct cx23885_buffer, vb.queue);
  85. buf->count = q->count++;
  86. }
  87. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  88. return 0;
  89. }
  90. void cx23885_vbi_timeout(unsigned long data)
  91. {
  92. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  93. struct cx23885_dmaqueue *q = &dev->vbiq;
  94. struct cx23885_buffer *buf;
  95. unsigned long flags;
  96. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
  97. cx_clear(VID_A_DMA_CTL, 0x22);
  98. spin_lock_irqsave(&dev->slock, flags);
  99. while (!list_empty(&q->active)) {
  100. buf = list_entry(q->active.next, struct cx23885_buffer,
  101. vb.queue);
  102. list_del(&buf->vb.queue);
  103. buf->vb.state = VIDEOBUF_ERROR;
  104. wake_up(&buf->vb.done);
  105. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
  106. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  107. }
  108. cx23885_restart_vbi_queue(dev, q);
  109. spin_unlock_irqrestore(&dev->slock, flags);
  110. }
  111. /* ------------------------------------------------------------------ */
  112. #define VBI_LINE_LENGTH 2048
  113. #define VBI_LINE_COUNT 17
  114. static int
  115. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  116. {
  117. *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  118. if (0 == *count)
  119. *count = vbibufs;
  120. if (*count < 2)
  121. *count = 2;
  122. if (*count > 32)
  123. *count = 32;
  124. return 0;
  125. }
  126. static int
  127. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  128. enum v4l2_field field)
  129. {
  130. struct cx23885_fh *fh = q->priv_data;
  131. struct cx23885_dev *dev = fh->dev;
  132. struct cx23885_buffer *buf = container_of(vb,
  133. struct cx23885_buffer, vb);
  134. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  135. unsigned int size;
  136. int rc;
  137. size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
  138. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  139. return -EINVAL;
  140. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  141. buf->vb.width = VBI_LINE_LENGTH;
  142. buf->vb.height = VBI_LINE_COUNT;
  143. buf->vb.size = size;
  144. buf->vb.field = V4L2_FIELD_SEQ_TB;
  145. rc = videobuf_iolock(q, &buf->vb, NULL);
  146. if (0 != rc)
  147. goto fail;
  148. cx23885_risc_buffer(dev->pci, &buf->risc,
  149. dma->sglist,
  150. 0, buf->vb.width * buf->vb.height,
  151. buf->vb.width, 0,
  152. buf->vb.height);
  153. }
  154. buf->vb.state = VIDEOBUF_PREPARED;
  155. return 0;
  156. fail:
  157. cx23885_free_buffer(q, buf);
  158. return rc;
  159. }
  160. static void
  161. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  162. {
  163. struct cx23885_buffer *buf =
  164. container_of(vb, struct cx23885_buffer, vb);
  165. struct cx23885_buffer *prev;
  166. struct cx23885_fh *fh = vq->priv_data;
  167. struct cx23885_dev *dev = fh->dev;
  168. struct cx23885_dmaqueue *q = &dev->vbiq;
  169. /* add jump to stopper */
  170. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  171. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  172. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  173. if (list_empty(&q->active)) {
  174. list_add_tail(&buf->vb.queue, &q->active);
  175. cx23885_start_vbi_dma(dev, q, buf);
  176. buf->vb.state = VIDEOBUF_ACTIVE;
  177. buf->count = q->count++;
  178. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  179. dprintk(2, "[%p/%d] vbi_queue - first active\n",
  180. buf, buf->vb.i);
  181. } else {
  182. prev = list_entry(q->active.prev, struct cx23885_buffer,
  183. vb.queue);
  184. list_add_tail(&buf->vb.queue, &q->active);
  185. buf->vb.state = VIDEOBUF_ACTIVE;
  186. buf->count = q->count++;
  187. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  188. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
  189. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  190. buf, buf->vb.i);
  191. }
  192. }
  193. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  194. {
  195. struct cx23885_buffer *buf =
  196. container_of(vb, struct cx23885_buffer, vb);
  197. cx23885_free_buffer(q, buf);
  198. }
  199. struct videobuf_queue_ops cx23885_vbi_qops = {
  200. .buf_setup = vbi_setup,
  201. .buf_prepare = vbi_prepare,
  202. .buf_queue = vbi_queue,
  203. .buf_release = vbi_release,
  204. };
  205. /* ------------------------------------------------------------------ */
  206. /*
  207. * Local variables:
  208. * c-basic-offset: 8
  209. * End:
  210. */