1 /*
2    em28xx-vbi.c - VBI driver for em28xx
3 
4    Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
5 
6    This work was sponsored by EyeMagnet Limited.
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., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22  */
23 
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/hardirq.h>
27 #include <linux/init.h>
28 
29 #include "em28xx.h"
30 #include "em28xx-v4l.h"
31 
32 static unsigned int vbibufs = 5;
33 module_param(vbibufs, int, 0644);
34 MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
35 
36 static unsigned int vbi_debug;
37 module_param(vbi_debug, int, 0644);
38 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
39 
40 #define dprintk(level, fmt, arg...)	if (vbi_debug >= level) \
41 	printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
42 
43 /* ------------------------------------------------------------------ */
44 
45 static int vbi_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
46 			   unsigned int *nbuffers, unsigned int *nplanes,
47 			   unsigned int sizes[], void *alloc_ctxs[])
48 {
49 	struct em28xx *dev = vb2_get_drv_priv(vq);
50 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
51 	unsigned long size;
52 
53 	if (fmt)
54 		size = fmt->fmt.pix.sizeimage;
55 	else
56 		size = v4l2->vbi_width * v4l2->vbi_height * 2;
57 
58 	if (0 == *nbuffers)
59 		*nbuffers = 32;
60 	if (*nbuffers < 2)
61 		*nbuffers = 2;
62 	if (*nbuffers > 32)
63 		*nbuffers = 32;
64 
65 	*nplanes = 1;
66 	sizes[0] = size;
67 
68 	return 0;
69 }
70 
71 static int vbi_buffer_prepare(struct vb2_buffer *vb)
72 {
73 	struct em28xx        *dev  = vb2_get_drv_priv(vb->vb2_queue);
74 	struct em28xx_v4l2   *v4l2 = dev->v4l2;
75 	struct em28xx_buffer *buf  = container_of(vb, struct em28xx_buffer, vb);
76 	unsigned long        size;
77 
78 	size = v4l2->vbi_width * v4l2->vbi_height * 2;
79 
80 	if (vb2_plane_size(vb, 0) < size) {
81 		printk(KERN_INFO "%s data will not fit into plane (%lu < %lu)\n",
82 		       __func__, vb2_plane_size(vb, 0), size);
83 		return -EINVAL;
84 	}
85 	vb2_set_plane_payload(&buf->vb, 0, size);
86 
87 	return 0;
88 }
89 
90 static void
91 vbi_buffer_queue(struct vb2_buffer *vb)
92 {
93 	struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
94 	struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
95 	struct em28xx_dmaqueue *vbiq = &dev->vbiq;
96 	unsigned long flags = 0;
97 
98 	buf->mem = vb2_plane_vaddr(vb, 0);
99 	buf->length = vb2_plane_size(vb, 0);
100 
101 	spin_lock_irqsave(&dev->slock, flags);
102 	list_add_tail(&buf->list, &vbiq->active);
103 	spin_unlock_irqrestore(&dev->slock, flags);
104 }
105 
106 
107 struct vb2_ops em28xx_vbi_qops = {
108 	.queue_setup    = vbi_queue_setup,
109 	.buf_prepare    = vbi_buffer_prepare,
110 	.buf_queue      = vbi_buffer_queue,
111 	.start_streaming = em28xx_start_analog_streaming,
112 	.stop_streaming = em28xx_stop_vbi_streaming,
113 	.wait_prepare   = vb2_ops_wait_prepare,
114 	.wait_finish    = vb2_ops_wait_finish,
115 };
116