1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // em28xx-vbi.c - VBI driver for em28xx
4 //
5 // Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
6 //
7 // This work was sponsored by EyeMagnet Limited.
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 #include "em28xx.h"
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/hardirq.h>
24 #include <linux/init.h>
25 #include <linux/usb.h>
26 
27 #include "em28xx-v4l.h"
28 
29 /* ------------------------------------------------------------------ */
30 
31 static int vbi_queue_setup(struct vb2_queue *vq,
32 			   unsigned int *nbuffers, unsigned int *nplanes,
33 			   unsigned int sizes[], struct device *alloc_devs[])
34 {
35 	struct em28xx *dev = vb2_get_drv_priv(vq);
36 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
37 	unsigned long size = v4l2->vbi_width * v4l2->vbi_height * 2;
38 
39 	if (*nbuffers < 2)
40 		*nbuffers = 2;
41 
42 	if (*nplanes) {
43 		if (sizes[0] < size)
44 			return -EINVAL;
45 		size = sizes[0];
46 	}
47 
48 	*nplanes = 1;
49 	sizes[0] = size;
50 
51 	return 0;
52 }
53 
54 static int vbi_buffer_prepare(struct vb2_buffer *vb)
55 {
56 	struct em28xx        *dev  = vb2_get_drv_priv(vb->vb2_queue);
57 	struct em28xx_v4l2   *v4l2 = dev->v4l2;
58 	unsigned long        size;
59 
60 	size = v4l2->vbi_width * v4l2->vbi_height * 2;
61 
62 	if (vb2_plane_size(vb, 0) < size) {
63 		dev_info(&dev->intf->dev,
64 			 "%s data will not fit into plane (%lu < %lu)\n",
65 			 __func__, vb2_plane_size(vb, 0), size);
66 		return -EINVAL;
67 	}
68 	vb2_set_plane_payload(vb, 0, size);
69 
70 	return 0;
71 }
72 
73 static void
74 vbi_buffer_queue(struct vb2_buffer *vb)
75 {
76 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
77 	struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
78 	struct em28xx_buffer *buf =
79 		container_of(vbuf, struct em28xx_buffer, vb);
80 	struct em28xx_dmaqueue *vbiq = &dev->vbiq;
81 	unsigned long flags = 0;
82 
83 	buf->mem = vb2_plane_vaddr(vb, 0);
84 	buf->length = vb2_plane_size(vb, 0);
85 
86 	spin_lock_irqsave(&dev->slock, flags);
87 	list_add_tail(&buf->list, &vbiq->active);
88 	spin_unlock_irqrestore(&dev->slock, flags);
89 }
90 
91 const struct vb2_ops em28xx_vbi_qops = {
92 	.queue_setup    = vbi_queue_setup,
93 	.buf_prepare    = vbi_buffer_prepare,
94 	.buf_queue      = vbi_buffer_queue,
95 	.start_streaming = em28xx_start_analog_streaming,
96 	.stop_streaming = em28xx_stop_vbi_streaming,
97 	.wait_prepare   = vb2_ops_wait_prepare,
98 	.wait_finish    = vb2_ops_wait_finish,
99 };
100