📦 barneyman / somagic

📄 smi2021_v4l2.c · 291 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291/************************************************************************
 * smi2021_v4l2.c							*
 *									*
 * USB Driver for smi2021 - EasyCap					*
 * **********************************************************************
 *
 * Copyright 2011-2013 Jon Arne Jørgensen
 * <jonjon.arnearne--a.t--gmail.com>
 *
 * Copyright 2011, 2012 Tony Brown, Michal Demin, Jeffry Johnston
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * This driver is heavily influensed by the STK1160 driver.
 * Copyright (C) 2012 Ezequiel Garcia
 * <elezegarcia--a.t--gmail.com>
 *
 */

#include "smi2021.h"

static int vidioc_querycap(struct file *file, void *priv,
			struct v4l2_capability *cap)
{
	struct smi2021 *smi2021 = video_drvdata(file);

	strlcpy(cap->driver, "smi2021", sizeof(cap->driver));
	strlcpy(cap->card, "smi2021", sizeof(cap->card));
	usb_make_path(smi2021->udev, cap->bus_info, sizeof(cap->bus_info));
	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
			   V4L2_CAP_STREAMING |
			   V4L2_CAP_READWRITE;
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
	return 0;
}

static int vidioc_enum_input(struct file *file, void *priv,
				struct v4l2_input *i)
{
	struct smi2021 *smi2021 = video_drvdata(file);

	if (i->index >= smi2021->vid_input_count)
		return -EINVAL;

	strlcpy(i->name, smi2021->vid_inputs[i->index].name, sizeof(i->name));
	i->type = V4L2_INPUT_TYPE_CAMERA;
	i->std = smi2021->vdev.tvnorms;
	return 0;
}

static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
			struct v4l2_fmtdesc *f)
{
	if (f->index != 0)
		return -EINVAL;

	strlcpy(f->description, "16bpp YU2, 4:2:2, packed",
					sizeof(f->description));
	f->pixelformat = V4L2_PIX_FMT_UYVY;
	return 0;
}

static int vidioc_fmt_vid_cap(struct file *file, void *priv,
			struct v4l2_format *f)
{
	struct smi2021 *smi2021 = video_drvdata(file);

	f->fmt.pix.width = SMI2021_BYTES_PER_LINE / 2;
	f->fmt.pix.height = smi2021->currentFrameHeight;
	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
	f->fmt.pix.bytesperline = SMI2021_BYTES_PER_LINE;
	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
	f->fmt.pix.priv = 0;
	return 0;
}

static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
{
	struct smi2021 *smi2021 = video_drvdata(file);
	*norm = smi2021->cur_norm;
	return 0;
}

static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
	struct smi2021 *smi2021 = video_drvdata(file);
	*i = smi2021->cur_input;
	return 0;
}


static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
{
	struct smi2021 *smi2021 = video_drvdata(file);

	if (vb2_is_busy(&smi2021->vb2q))
		return -EBUSY;

	if(!smi2021_setSourceSignalFormat(smi2021,norm))
	{
	//smi2021->cur_norm = norm;
	//if (norm & V4L2_STD_525_60)
	//	smi2021->cur_height = SMI2021_NTSC_LINES;
	//else if (norm & V4L2_STD_625_50)
	//	smi2021->cur_height = SMI2021_PAL_LINES;
	//else
		return -EINVAL;
	}

	v4l2_device_call_all(&smi2021->v4l2_dev, 0, core, s_std,
			     smi2021->cur_norm);
	return 0;
}

static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
	struct smi2021 *smi2021 = video_drvdata(file);
	if (i >= smi2021->vid_input_count)
		return -EINVAL;

	v4l2_device_call_all(&smi2021->v4l2_dev, 0, video, s_routing,
		smi2021->vid_inputs[i].type, 0, 0);
	smi2021->cur_input = i;

	return 0;
}

static const struct v4l2_ioctl_ops smi2021_ioctl_ops = {
	.vidioc_querycap		= vidioc_querycap,
	.vidioc_enum_input		= vidioc_enum_input,
	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap		= vidioc_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap		= vidioc_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap		= vidioc_fmt_vid_cap,
	.vidioc_g_std			= vidioc_g_std,
	.vidioc_s_std			= vidioc_s_std,
	.vidioc_g_input			= vidioc_g_input,
	.vidioc_s_input			= vidioc_s_input,

	/* vb2 handle these */
	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
	.vidioc_querybuf		= vb2_ioctl_querybuf,
	.vidioc_qbuf			= vb2_ioctl_qbuf,
	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
	.vidioc_streamon		= vb2_ioctl_streamon,
	.vidioc_streamoff		= vb2_ioctl_streamoff,

	/* v4l2-event and v4l2-cntrl handle these */
	.vidioc_log_status		= v4l2_ctrl_log_status,
	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
};

static struct v4l2_file_operations smi2021_fops = {
	.owner = THIS_MODULE,
	.open = v4l2_fh_open,
	.release = vb2_fop_release,
	.read = vb2_fop_read,
	.poll = vb2_fop_poll,
	.mmap = vb2_fop_mmap,
	.unlocked_ioctl = video_ioctl2,
};

/************************************************************************
 *									*
 *          Videobuf2 operations					*
 *									*
 ***********************************************************************/
static int queue_setup(struct vb2_queue *vq,
				const struct v4l2_format *v4l2_fmt,
				unsigned int *nbuffers, unsigned int *nplanes,
				unsigned int sizes[], void *alloc_ctxs[])
{
	struct smi2021 *smi2021 = vb2_get_drv_priv(vq);

	*nbuffers = clamp_t(unsigned int, *nbuffers, 16, 32);

	dev_warn(smi2021->dev, "queue_setup (%d buffers)\n", *nbuffers);

	*nplanes = 1;
	sizes[0] = SMI2021_BYTES_PER_LINE * smi2021->currentFrameHeight;

	return 0;
}

static void buffer_queue(struct vb2_buffer *vb)
{
	unsigned long flags;
	struct smi2021 *smi2021 = vb2_get_drv_priv(vb->vb2_queue);
	struct smi2021_buf *buf = container_of(vb, struct smi2021_buf, vb);


	if (smi2021->udev == NULL) 
	{
		dev_err(smi2021->dev, "udev NULL\n");
		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
		return;
	}

	buf->mem = vb2_plane_vaddr(vb, 0);
	buf->length = vb2_plane_size(vb, 0);

	spin_lock_irqsave(&smi2021->buf_lock, flags);

	if (buf->length < smi2021->currentFrameHeight * SMI2021_BYTES_PER_LINE)
	{
		dev_warn(smi2021->dev, "done\n");
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}
	else
	{
		//dev_info(smi2021->dev, "buffer_added\n");
		list_add_tail(&buf->list, &smi2021->bufs);
	}

	spin_unlock_irqrestore(&smi2021->buf_lock, flags);
}

static int start_streaming(struct vb2_queue *vq, unsigned int count)
{
	struct smi2021 *smi2021 = vb2_get_drv_priv(vq);

	if (smi2021->udev == NULL)
		return -ENODEV;

	return smi2021_start(smi2021);
}

static int stop_streaming(struct vb2_queue *vq)
{
	struct smi2021 *smi2021 = vb2_get_drv_priv(vq);

	if (smi2021->udev == NULL)
		return -ENODEV;

	smi2021_stop(smi2021);
	return 0;
}

static struct vb2_ops smi2021_vb2_ops = {
	.queue_setup		= queue_setup,
	.buf_queue			= buffer_queue,
	.start_streaming	= start_streaming,
	.stop_streaming		= stop_streaming,
};

int smi2021_vb2_setup(struct smi2021 *smi2021)
{
	smi2021->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	smi2021->vb2q.io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
	smi2021->vb2q.drv_priv = smi2021;
	smi2021->vb2q.buf_struct_size = sizeof(struct smi2021_buf);
	smi2021->vb2q.ops = &smi2021_vb2_ops;
	smi2021->vb2q.mem_ops = &vb2_vmalloc_memops;
	smi2021->vb2q.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	smi2021->vb2q.lock = &smi2021->vb2q_lock;

	return vb2_queue_init(&smi2021->vb2q);
}

int smi2021_video_register(struct smi2021 *smi2021)
{
	strlcpy(smi2021->vdev.name, "smi2021", sizeof(smi2021->vdev.name));
	smi2021->vdev.v4l2_dev = &smi2021->v4l2_dev;
	smi2021->vdev.release = video_device_release_empty;
	smi2021->vdev.fops = &smi2021_fops;
	smi2021->vdev.ioctl_ops = &smi2021_ioctl_ops;
	smi2021->vdev.tvnorms = V4L2_STD_ALL;
	smi2021->vdev.queue = &smi2021->vb2q;
	smi2021->vdev.lock = &smi2021->v4l2_lock;
	set_bit(V4L2_FL_USE_FH_PRIO, &smi2021->vdev.flags);
	video_set_drvdata(&smi2021->vdev, smi2021);

	return video_register_device(&smi2021->vdev, VFL_TYPE_GRABBER, -1);
}