RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / em28xx / em28xx-vbi.c
blob7f1c4a2173b68fb4bb6450129c383f8f30fa99d7
1 /*
2 em28xx-vbi.c - VBI driver for em28xx
4 Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
6 This work was sponsored by EyeMagnet Limited.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
28 #include "em28xx.h"
30 static unsigned int vbibufs = 5;
31 module_param(vbibufs, int, 0644);
32 MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
34 static unsigned int vbi_debug;
35 module_param(vbi_debug, int, 0644);
36 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
38 #define dprintk(level, fmt, arg...) if (vbi_debug >= level) \
39 printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
41 /* ------------------------------------------------------------------ */
43 static void
44 free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
46 struct em28xx_fh *fh = vq->priv_data;
47 struct em28xx *dev = fh->dev;
48 unsigned long flags = 0;
49 if (in_interrupt())
50 BUG();
52 /* We used to wait for the buffer to finish here, but this didn't work
53 because, as we were keeping the state as VIDEOBUF_QUEUED,
54 videobuf_queue_cancel marked it as finished for us.
55 (Also, it could wedge forever if the hardware was misconfigured.)
57 This should be safe; by the time we get here, the buffer isn't
58 queued anymore. If we ever start marking the buffers as
59 VIDEOBUF_ACTIVE, it won't be, though.
61 spin_lock_irqsave(&dev->slock, flags);
62 if (dev->isoc_ctl.vbi_buf == buf)
63 dev->isoc_ctl.vbi_buf = NULL;
64 spin_unlock_irqrestore(&dev->slock, flags);
66 videobuf_vmalloc_free(&buf->vb);
67 buf->vb.state = VIDEOBUF_NEEDS_INIT;
70 static int
71 vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
73 struct em28xx_fh *fh = q->priv_data;
74 struct em28xx *dev = fh->dev;
76 *size = dev->vbi_width * dev->vbi_height * 2;
78 if (0 == *count)
79 *count = vbibufs;
80 if (*count < 2)
81 *count = 2;
82 if (*count > 32)
83 *count = 32;
84 return 0;
87 static int
88 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
89 enum v4l2_field field)
91 struct em28xx_fh *fh = q->priv_data;
92 struct em28xx *dev = fh->dev;
93 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
94 int rc = 0;
96 buf->vb.size = dev->vbi_width * dev->vbi_height * 2;
98 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
99 return -EINVAL;
101 buf->vb.width = dev->vbi_width;
102 buf->vb.height = dev->vbi_height;
103 buf->vb.field = field;
105 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
106 rc = videobuf_iolock(q, &buf->vb, NULL);
107 if (rc < 0)
108 goto fail;
111 buf->vb.state = VIDEOBUF_PREPARED;
112 return 0;
114 fail:
115 free_buffer(q, buf);
116 return rc;
119 static void
120 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
122 struct em28xx_buffer *buf = container_of(vb,
123 struct em28xx_buffer,
124 vb);
125 struct em28xx_fh *fh = vq->priv_data;
126 struct em28xx *dev = fh->dev;
127 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
129 buf->vb.state = VIDEOBUF_QUEUED;
130 list_add_tail(&buf->vb.queue, &vbiq->active);
133 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
135 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
136 free_buffer(q, buf);
139 struct videobuf_queue_ops em28xx_vbi_qops = {
140 .buf_setup = vbi_setup,
141 .buf_prepare = vbi_prepare,
142 .buf_queue = vbi_queue,
143 .buf_release = vbi_release,