1 /*
2  * Copyright (c) 2013,2016 Lubomir Rintel
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * Alternatively, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL").
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * Fushicai USBTV007 Audio-Video Grabber Driver
31  *
32  * Product web site:
33  * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
34  *
35  * Following LWN articles were very useful in construction of this driver:
36  * Video4Linux2 API series: http://lwn.net/Articles/203924/
37  * videobuf2 API explanation: http://lwn.net/Articles/447435/
38  * Thanks go to Jonathan Corbet for providing this quality documentation.
39  * He is awesome.
40  *
41  * No physical hardware was harmed running Windows during the
42  * reverse-engineering activity
43  */
44 
45 #include <media/v4l2-ioctl.h>
46 #include <media/videobuf2-v4l2.h>
47 
48 #include "usbtv.h"
49 
50 static struct usbtv_norm_params norm_params[] = {
51 	{
52 		.norm = V4L2_STD_525_60,
53 		.cap_width = 720,
54 		.cap_height = 480,
55 	},
56 	{
57 		.norm = V4L2_STD_PAL,
58 		.cap_width = 720,
59 		.cap_height = 576,
60 	}
61 };
62 
63 static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
64 {
65 	int i, ret = 0;
66 	struct usbtv_norm_params *params = NULL;
67 
68 	for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
69 		if (norm_params[i].norm & norm) {
70 			params = &norm_params[i];
71 			break;
72 		}
73 	}
74 
75 	if (params) {
76 		usbtv->width = params->cap_width;
77 		usbtv->height = params->cap_height;
78 		usbtv->n_chunks = usbtv->width * usbtv->height
79 						/ 4 / USBTV_CHUNK;
80 		usbtv->norm = params->norm;
81 	} else
82 		ret = -EINVAL;
83 
84 	return ret;
85 }
86 
87 static int usbtv_select_input(struct usbtv *usbtv, int input)
88 {
89 	int ret;
90 
91 	static const u16 composite[][2] = {
92 		{ USBTV_BASE + 0x0105, 0x0060 },
93 		{ USBTV_BASE + 0x011f, 0x00f2 },
94 		{ USBTV_BASE + 0x0127, 0x0060 },
95 		{ USBTV_BASE + 0x00ae, 0x0010 },
96 		{ USBTV_BASE + 0x0239, 0x0060 },
97 	};
98 
99 	static const u16 svideo[][2] = {
100 		{ USBTV_BASE + 0x0105, 0x0010 },
101 		{ USBTV_BASE + 0x011f, 0x00ff },
102 		{ USBTV_BASE + 0x0127, 0x0060 },
103 		{ USBTV_BASE + 0x00ae, 0x0030 },
104 		{ USBTV_BASE + 0x0239, 0x0060 },
105 	};
106 
107 	switch (input) {
108 	case USBTV_COMPOSITE_INPUT:
109 		ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
110 		break;
111 	case USBTV_SVIDEO_INPUT:
112 		ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
113 		break;
114 	default:
115 		ret = -EINVAL;
116 	}
117 
118 	if (!ret)
119 		usbtv->input = input;
120 
121 	return ret;
122 }
123 
124 static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
125 {
126 	int ret;
127 	static const u16 pal[][2] = {
128 		{ USBTV_BASE + 0x001a, 0x0068 },
129 		{ USBTV_BASE + 0x010e, 0x0072 },
130 		{ USBTV_BASE + 0x010f, 0x00a2 },
131 		{ USBTV_BASE + 0x0112, 0x00b0 },
132 		{ USBTV_BASE + 0x0117, 0x0001 },
133 		{ USBTV_BASE + 0x0118, 0x002c },
134 		{ USBTV_BASE + 0x012d, 0x0010 },
135 		{ USBTV_BASE + 0x012f, 0x0020 },
136 		{ USBTV_BASE + 0x024f, 0x0002 },
137 		{ USBTV_BASE + 0x0254, 0x0059 },
138 		{ USBTV_BASE + 0x025a, 0x0016 },
139 		{ USBTV_BASE + 0x025b, 0x0035 },
140 		{ USBTV_BASE + 0x0263, 0x0017 },
141 		{ USBTV_BASE + 0x0266, 0x0016 },
142 		{ USBTV_BASE + 0x0267, 0x0036 }
143 	};
144 
145 	static const u16 ntsc[][2] = {
146 		{ USBTV_BASE + 0x001a, 0x0079 },
147 		{ USBTV_BASE + 0x010e, 0x0068 },
148 		{ USBTV_BASE + 0x010f, 0x009c },
149 		{ USBTV_BASE + 0x0112, 0x00f0 },
150 		{ USBTV_BASE + 0x0117, 0x0000 },
151 		{ USBTV_BASE + 0x0118, 0x00fc },
152 		{ USBTV_BASE + 0x012d, 0x0004 },
153 		{ USBTV_BASE + 0x012f, 0x0008 },
154 		{ USBTV_BASE + 0x024f, 0x0001 },
155 		{ USBTV_BASE + 0x0254, 0x005f },
156 		{ USBTV_BASE + 0x025a, 0x0012 },
157 		{ USBTV_BASE + 0x025b, 0x0001 },
158 		{ USBTV_BASE + 0x0263, 0x001c },
159 		{ USBTV_BASE + 0x0266, 0x0011 },
160 		{ USBTV_BASE + 0x0267, 0x0005 }
161 	};
162 
163 	ret = usbtv_configure_for_norm(usbtv, norm);
164 
165 	if (!ret) {
166 		if (norm & V4L2_STD_525_60)
167 			ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
168 		else if (norm & V4L2_STD_PAL)
169 			ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
170 	}
171 
172 	return ret;
173 }
174 
175 static int usbtv_setup_capture(struct usbtv *usbtv)
176 {
177 	int ret;
178 	static const u16 setup[][2] = {
179 		/* These seem to enable the device. */
180 		{ USBTV_BASE + 0x0008, 0x0001 },
181 		{ USBTV_BASE + 0x01d0, 0x00ff },
182 		{ USBTV_BASE + 0x01d9, 0x0002 },
183 
184 		/* These seem to influence color parameters, such as
185 		 * brightness, etc. */
186 		{ USBTV_BASE + 0x0239, 0x0040 },
187 		{ USBTV_BASE + 0x0240, 0x0000 },
188 		{ USBTV_BASE + 0x0241, 0x0000 },
189 		{ USBTV_BASE + 0x0242, 0x0002 },
190 		{ USBTV_BASE + 0x0243, 0x0080 },
191 		{ USBTV_BASE + 0x0244, 0x0012 },
192 		{ USBTV_BASE + 0x0245, 0x0090 },
193 		{ USBTV_BASE + 0x0246, 0x0000 },
194 
195 		{ USBTV_BASE + 0x0278, 0x002d },
196 		{ USBTV_BASE + 0x0279, 0x000a },
197 		{ USBTV_BASE + 0x027a, 0x0032 },
198 		{ 0xf890, 0x000c },
199 		{ 0xf894, 0x0086 },
200 
201 		{ USBTV_BASE + 0x00ac, 0x00c0 },
202 		{ USBTV_BASE + 0x00ad, 0x0000 },
203 		{ USBTV_BASE + 0x00a2, 0x0012 },
204 		{ USBTV_BASE + 0x00a3, 0x00e0 },
205 		{ USBTV_BASE + 0x00a4, 0x0028 },
206 		{ USBTV_BASE + 0x00a5, 0x0082 },
207 		{ USBTV_BASE + 0x00a7, 0x0080 },
208 		{ USBTV_BASE + 0x0000, 0x0014 },
209 		{ USBTV_BASE + 0x0006, 0x0003 },
210 		{ USBTV_BASE + 0x0090, 0x0099 },
211 		{ USBTV_BASE + 0x0091, 0x0090 },
212 		{ USBTV_BASE + 0x0094, 0x0068 },
213 		{ USBTV_BASE + 0x0095, 0x0070 },
214 		{ USBTV_BASE + 0x009c, 0x0030 },
215 		{ USBTV_BASE + 0x009d, 0x00c0 },
216 		{ USBTV_BASE + 0x009e, 0x00e0 },
217 		{ USBTV_BASE + 0x0019, 0x0006 },
218 		{ USBTV_BASE + 0x008c, 0x00ba },
219 		{ USBTV_BASE + 0x0101, 0x00ff },
220 		{ USBTV_BASE + 0x010c, 0x00b3 },
221 		{ USBTV_BASE + 0x01b2, 0x0080 },
222 		{ USBTV_BASE + 0x01b4, 0x00a0 },
223 		{ USBTV_BASE + 0x014c, 0x00ff },
224 		{ USBTV_BASE + 0x014d, 0x00ca },
225 		{ USBTV_BASE + 0x0113, 0x0053 },
226 		{ USBTV_BASE + 0x0119, 0x008a },
227 		{ USBTV_BASE + 0x013c, 0x0003 },
228 		{ USBTV_BASE + 0x0150, 0x009c },
229 		{ USBTV_BASE + 0x0151, 0x0071 },
230 		{ USBTV_BASE + 0x0152, 0x00c6 },
231 		{ USBTV_BASE + 0x0153, 0x0084 },
232 		{ USBTV_BASE + 0x0154, 0x00bc },
233 		{ USBTV_BASE + 0x0155, 0x00a0 },
234 		{ USBTV_BASE + 0x0156, 0x00a0 },
235 		{ USBTV_BASE + 0x0157, 0x009c },
236 		{ USBTV_BASE + 0x0158, 0x001f },
237 		{ USBTV_BASE + 0x0159, 0x0006 },
238 		{ USBTV_BASE + 0x015d, 0x0000 },
239 
240 		{ USBTV_BASE + 0x0003, 0x0004 },
241 		{ USBTV_BASE + 0x0100, 0x00d3 },
242 		{ USBTV_BASE + 0x0115, 0x0015 },
243 		{ USBTV_BASE + 0x0220, 0x002e },
244 		{ USBTV_BASE + 0x0225, 0x0008 },
245 		{ USBTV_BASE + 0x024e, 0x0002 },
246 		{ USBTV_BASE + 0x024e, 0x0002 },
247 		{ USBTV_BASE + 0x024f, 0x0002 },
248 	};
249 
250 	ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
251 	if (ret)
252 		return ret;
253 
254 	ret = usbtv_select_norm(usbtv, usbtv->norm);
255 	if (ret)
256 		return ret;
257 
258 	ret = usbtv_select_input(usbtv, usbtv->input);
259 	if (ret)
260 		return ret;
261 
262 	ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
263 	if (ret)
264 		return ret;
265 
266 	return 0;
267 }
268 
269 /* Copy data from chunk into a frame buffer, deinterlacing the data
270  * into every second line. Unfortunately, they don't align nicely into
271  * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
272  * Therefore, we break down the chunk into two halves before copying,
273  * so that we can interleave a line if needed.
274  *
275  * Each "chunk" is 240 words; a word in this context equals 4 bytes.
276  * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
277  * pixels, the Cr and Cb shared between the two pixels, but each having
278  * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
279  * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
280  * The image is interlaced, so there is a "scan" of odd lines, followed
281  * by "scan" of even numbered lines.
282  *
283  * Following code is writing the chunks in correct sequence, skipping
284  * the rows based on "odd" value.
285  * line 1: chunk[0][  0..479] chunk[0][480..959] chunk[1][  0..479]
286  * line 3: chunk[1][480..959] chunk[2][  0..479] chunk[2][480..959]
287  * ...etc.
288  */
289 static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
290 {
291 	int half;
292 
293 	for (half = 0; half < 2; half++) {
294 		int part_no = chunk_no * 2 + half;
295 		int line = part_no / 3;
296 		int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
297 
298 		u32 *dst = &frame[part_index * USBTV_CHUNK/2];
299 
300 		memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
301 		src += USBTV_CHUNK/2;
302 	}
303 }
304 
305 /* Called for each 256-byte image chunk.
306  * First word identifies the chunk, followed by 240 words of image
307  * data and padding. */
308 static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
309 {
310 	int frame_id, odd, chunk_no;
311 	u32 *frame;
312 	struct usbtv_buf *buf;
313 	unsigned long flags;
314 
315 	/* Ignore corrupted lines. */
316 	if (!USBTV_MAGIC_OK(chunk))
317 		return;
318 	frame_id = USBTV_FRAME_ID(chunk);
319 	odd = USBTV_ODD(chunk);
320 	chunk_no = USBTV_CHUNK_NO(chunk);
321 	if (chunk_no >= usbtv->n_chunks)
322 		return;
323 
324 	/* Beginning of a frame. */
325 	if (chunk_no == 0) {
326 		usbtv->frame_id = frame_id;
327 		usbtv->chunks_done = 0;
328 	}
329 
330 	if (usbtv->frame_id != frame_id)
331 		return;
332 
333 	spin_lock_irqsave(&usbtv->buflock, flags);
334 	if (list_empty(&usbtv->bufs)) {
335 		/* No free buffers. Userspace likely too slow. */
336 		spin_unlock_irqrestore(&usbtv->buflock, flags);
337 		return;
338 	}
339 
340 	/* First available buffer. */
341 	buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
342 	frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
343 
344 	/* Copy the chunk data. */
345 	usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
346 	usbtv->chunks_done++;
347 
348 	/* Last chunk in a field */
349 	if (chunk_no == usbtv->n_chunks-1) {
350 		/* Last chunk in a frame, signalling an end */
351 		if (odd && !usbtv->last_odd) {
352 			int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
353 			enum vb2_buffer_state state = usbtv->chunks_done ==
354 				usbtv->n_chunks ?
355 				VB2_BUF_STATE_DONE :
356 				VB2_BUF_STATE_ERROR;
357 
358 			buf->vb.field = V4L2_FIELD_INTERLACED;
359 			buf->vb.sequence = usbtv->sequence++;
360 			buf->vb.vb2_buf.timestamp = ktime_get_ns();
361 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
362 			vb2_buffer_done(&buf->vb.vb2_buf, state);
363 			list_del(&buf->list);
364 		}
365 		usbtv->last_odd = odd;
366 	}
367 
368 	spin_unlock_irqrestore(&usbtv->buflock, flags);
369 }
370 
371 /* Got image data. Each packet contains a number of 256-word chunks we
372  * compose the image from. */
373 static void usbtv_iso_cb(struct urb *ip)
374 {
375 	int ret;
376 	int i;
377 	struct usbtv *usbtv = (struct usbtv *)ip->context;
378 
379 	switch (ip->status) {
380 	/* All fine. */
381 	case 0:
382 		break;
383 	/* Device disconnected or capture stopped? */
384 	case -ENODEV:
385 	case -ENOENT:
386 	case -ECONNRESET:
387 	case -ESHUTDOWN:
388 		return;
389 	/* Unknown error. Retry. */
390 	default:
391 		dev_warn(usbtv->dev, "Bad response for ISO request.\n");
392 		goto resubmit;
393 	}
394 
395 	for (i = 0; i < ip->number_of_packets; i++) {
396 		int size = ip->iso_frame_desc[i].actual_length;
397 		unsigned char *data = ip->transfer_buffer +
398 				ip->iso_frame_desc[i].offset;
399 		int offset;
400 
401 		for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
402 			usbtv_image_chunk(usbtv,
403 				(__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
404 	}
405 
406 resubmit:
407 	ret = usb_submit_urb(ip, GFP_ATOMIC);
408 	if (ret < 0)
409 		dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
410 }
411 
412 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
413 {
414 	struct urb *ip;
415 	int size = usbtv->iso_size;
416 	int i;
417 
418 	ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
419 	if (ip == NULL)
420 		return NULL;
421 
422 	ip->dev = usbtv->udev;
423 	ip->context = usbtv;
424 	ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
425 	ip->interval = 1;
426 	ip->transfer_flags = URB_ISO_ASAP;
427 	ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
428 						GFP_KERNEL);
429 	if (!ip->transfer_buffer) {
430 		usb_free_urb(ip);
431 		return NULL;
432 	}
433 	ip->complete = usbtv_iso_cb;
434 	ip->number_of_packets = USBTV_ISOC_PACKETS;
435 	ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
436 	for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
437 		ip->iso_frame_desc[i].offset = size * i;
438 		ip->iso_frame_desc[i].length = size;
439 	}
440 
441 	return ip;
442 }
443 
444 static void usbtv_stop(struct usbtv *usbtv)
445 {
446 	int i;
447 	unsigned long flags;
448 
449 	/* Cancel running transfers. */
450 	for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
451 		struct urb *ip = usbtv->isoc_urbs[i];
452 
453 		if (ip == NULL)
454 			continue;
455 		usb_kill_urb(ip);
456 		kfree(ip->transfer_buffer);
457 		usb_free_urb(ip);
458 		usbtv->isoc_urbs[i] = NULL;
459 	}
460 
461 	/* Return buffers to userspace. */
462 	spin_lock_irqsave(&usbtv->buflock, flags);
463 	while (!list_empty(&usbtv->bufs)) {
464 		struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
465 						struct usbtv_buf, list);
466 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
467 		list_del(&buf->list);
468 	}
469 	spin_unlock_irqrestore(&usbtv->buflock, flags);
470 }
471 
472 static int usbtv_start(struct usbtv *usbtv)
473 {
474 	int i;
475 	int ret;
476 
477 	usbtv_audio_suspend(usbtv);
478 
479 	ret = usb_set_interface(usbtv->udev, 0, 0);
480 	if (ret < 0)
481 		return ret;
482 
483 	ret = usbtv_setup_capture(usbtv);
484 	if (ret < 0)
485 		return ret;
486 
487 	ret = usb_set_interface(usbtv->udev, 0, 1);
488 	if (ret < 0)
489 		return ret;
490 
491 	usbtv_audio_resume(usbtv);
492 
493 	for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
494 		struct urb *ip;
495 
496 		ip = usbtv_setup_iso_transfer(usbtv);
497 		if (ip == NULL) {
498 			ret = -ENOMEM;
499 			goto start_fail;
500 		}
501 		usbtv->isoc_urbs[i] = ip;
502 
503 		ret = usb_submit_urb(ip, GFP_KERNEL);
504 		if (ret < 0)
505 			goto start_fail;
506 	}
507 
508 	return 0;
509 
510 start_fail:
511 	usbtv_stop(usbtv);
512 	return ret;
513 }
514 
515 static int usbtv_querycap(struct file *file, void *priv,
516 				struct v4l2_capability *cap)
517 {
518 	struct usbtv *dev = video_drvdata(file);
519 
520 	strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
521 	strlcpy(cap->card, "usbtv", sizeof(cap->card));
522 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
523 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
524 	cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
525 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
526 	return 0;
527 }
528 
529 static int usbtv_enum_input(struct file *file, void *priv,
530 					struct v4l2_input *i)
531 {
532 	struct usbtv *dev = video_drvdata(file);
533 
534 	switch (i->index) {
535 	case USBTV_COMPOSITE_INPUT:
536 		strlcpy(i->name, "Composite", sizeof(i->name));
537 		break;
538 	case USBTV_SVIDEO_INPUT:
539 		strlcpy(i->name, "S-Video", sizeof(i->name));
540 		break;
541 	default:
542 		return -EINVAL;
543 	}
544 
545 	i->type = V4L2_INPUT_TYPE_CAMERA;
546 	i->std = dev->vdev.tvnorms;
547 	return 0;
548 }
549 
550 static int usbtv_enum_fmt_vid_cap(struct file *file, void  *priv,
551 					struct v4l2_fmtdesc *f)
552 {
553 	if (f->index > 0)
554 		return -EINVAL;
555 
556 	strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
557 					sizeof(f->description));
558 	f->pixelformat = V4L2_PIX_FMT_YUYV;
559 	return 0;
560 }
561 
562 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
563 					struct v4l2_format *f)
564 {
565 	struct usbtv *usbtv = video_drvdata(file);
566 
567 	f->fmt.pix.width = usbtv->width;
568 	f->fmt.pix.height = usbtv->height;
569 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
570 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
571 	f->fmt.pix.bytesperline = usbtv->width * 2;
572 	f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
573 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
574 
575 	return 0;
576 }
577 
578 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
579 {
580 	struct usbtv *usbtv = video_drvdata(file);
581 	*norm = usbtv->norm;
582 	return 0;
583 }
584 
585 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
586 {
587 	int ret = -EINVAL;
588 	struct usbtv *usbtv = video_drvdata(file);
589 
590 	if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
591 		ret = usbtv_select_norm(usbtv, norm);
592 
593 	return ret;
594 }
595 
596 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
597 {
598 	struct usbtv *usbtv = video_drvdata(file);
599 	*i = usbtv->input;
600 	return 0;
601 }
602 
603 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
604 {
605 	struct usbtv *usbtv = video_drvdata(file);
606 
607 	return usbtv_select_input(usbtv, i);
608 }
609 
610 static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
611 	.vidioc_querycap = usbtv_querycap,
612 	.vidioc_enum_input = usbtv_enum_input,
613 	.vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
614 	.vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
615 	.vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
616 	.vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
617 	.vidioc_g_std = usbtv_g_std,
618 	.vidioc_s_std = usbtv_s_std,
619 	.vidioc_g_input = usbtv_g_input,
620 	.vidioc_s_input = usbtv_s_input,
621 
622 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
623 	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
624 	.vidioc_querybuf = vb2_ioctl_querybuf,
625 	.vidioc_create_bufs = vb2_ioctl_create_bufs,
626 	.vidioc_qbuf = vb2_ioctl_qbuf,
627 	.vidioc_dqbuf = vb2_ioctl_dqbuf,
628 	.vidioc_streamon = vb2_ioctl_streamon,
629 	.vidioc_streamoff = vb2_ioctl_streamoff,
630 };
631 
632 static struct v4l2_file_operations usbtv_fops = {
633 	.owner = THIS_MODULE,
634 	.unlocked_ioctl = video_ioctl2,
635 	.mmap = vb2_fop_mmap,
636 	.open = v4l2_fh_open,
637 	.release = vb2_fop_release,
638 	.read = vb2_fop_read,
639 	.poll = vb2_fop_poll,
640 };
641 
642 static int usbtv_queue_setup(struct vb2_queue *vq,
643 	unsigned int *nbuffers,
644 	unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
645 {
646 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
647 	unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
648 
649 	if (vq->num_buffers + *nbuffers < 2)
650 		*nbuffers = 2 - vq->num_buffers;
651 	if (*nplanes)
652 		return sizes[0] < size ? -EINVAL : 0;
653 	*nplanes = 1;
654 	sizes[0] = size;
655 
656 	return 0;
657 }
658 
659 static void usbtv_buf_queue(struct vb2_buffer *vb)
660 {
661 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
662 	struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
663 	struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
664 	unsigned long flags;
665 
666 	if (usbtv->udev == NULL) {
667 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
668 		return;
669 	}
670 
671 	spin_lock_irqsave(&usbtv->buflock, flags);
672 	list_add_tail(&buf->list, &usbtv->bufs);
673 	spin_unlock_irqrestore(&usbtv->buflock, flags);
674 }
675 
676 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
677 {
678 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
679 
680 	if (usbtv->udev == NULL)
681 		return -ENODEV;
682 
683 	usbtv->last_odd = 1;
684 	usbtv->sequence = 0;
685 	return usbtv_start(usbtv);
686 }
687 
688 static void usbtv_stop_streaming(struct vb2_queue *vq)
689 {
690 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
691 
692 	if (usbtv->udev)
693 		usbtv_stop(usbtv);
694 }
695 
696 static const struct vb2_ops usbtv_vb2_ops = {
697 	.queue_setup = usbtv_queue_setup,
698 	.buf_queue = usbtv_buf_queue,
699 	.start_streaming = usbtv_start_streaming,
700 	.stop_streaming = usbtv_stop_streaming,
701 };
702 
703 static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
704 {
705 	struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
706 								ctrl);
707 	u8 *data;
708 	u16 index, size;
709 	int ret;
710 
711 	data = kmalloc(3, GFP_KERNEL);
712 	if (!data)
713 		return -ENOMEM;
714 
715 	/*
716 	 * Read in the current brightness/contrast registers. We need them
717 	 * both, because the values are for some reason interleaved.
718 	 */
719 	if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
720 		ret = usb_control_msg(usbtv->udev,
721 			usb_sndctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
722 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
723 			0, USBTV_BASE + 0x0244, (void *)data, 3, 0);
724 		if (ret < 0)
725 			goto error;
726 	}
727 
728 	switch (ctrl->id) {
729 	case V4L2_CID_BRIGHTNESS:
730 		index = USBTV_BASE + 0x0244;
731 		size = 3;
732 		data[0] &= 0xf0;
733 		data[0] |= (ctrl->val >> 8) & 0xf;
734 		data[2] = ctrl->val & 0xff;
735 		break;
736 	case V4L2_CID_CONTRAST:
737 		index = USBTV_BASE + 0x0244;
738 		size = 3;
739 		data[0] &= 0x0f;
740 		data[0] |= (ctrl->val >> 4) & 0xf0;
741 		data[1] = ctrl->val & 0xff;
742 		break;
743 	case V4L2_CID_SATURATION:
744 		index = USBTV_BASE + 0x0242;
745 		data[0] = ctrl->val >> 8;
746 		data[1] = ctrl->val & 0xff;
747 		size = 2;
748 		break;
749 	case V4L2_CID_HUE:
750 		index = USBTV_BASE + 0x0240;
751 		size = 2;
752 		if (ctrl->val > 0) {
753 			data[0] = 0x92 + (ctrl->val >> 8);
754 			data[1] = ctrl->val & 0xff;
755 		} else {
756 			data[0] = 0x82 + (-ctrl->val >> 8);
757 			data[1] = -ctrl->val & 0xff;
758 		}
759 		break;
760 	case V4L2_CID_SHARPNESS:
761 		index = USBTV_BASE + 0x0239;
762 		data[0] = 0;
763 		data[1] = ctrl->val;
764 		size = 2;
765 		break;
766 	default:
767 		kfree(data);
768 		return -EINVAL;
769 	}
770 
771 	ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
772 			USBTV_CONTROL_REG,
773 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
774 			0, index, (void *)data, size, 0);
775 
776 error:
777 	if (ret < 0)
778 		dev_warn(usbtv->dev, "Failed to submit a control request.\n");
779 
780 	kfree(data);
781 	return ret;
782 }
783 
784 static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
785 	.s_ctrl = usbtv_s_ctrl,
786 };
787 
788 static void usbtv_release(struct v4l2_device *v4l2_dev)
789 {
790 	struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
791 
792 	v4l2_device_unregister(&usbtv->v4l2_dev);
793 	v4l2_ctrl_handler_free(&usbtv->ctrl);
794 	vb2_queue_release(&usbtv->vb2q);
795 	kfree(usbtv);
796 }
797 
798 int usbtv_video_init(struct usbtv *usbtv)
799 {
800 	int ret;
801 
802 	(void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
803 
804 	spin_lock_init(&usbtv->buflock);
805 	mutex_init(&usbtv->v4l2_lock);
806 	mutex_init(&usbtv->vb2q_lock);
807 	INIT_LIST_HEAD(&usbtv->bufs);
808 
809 	/* videobuf2 structure */
810 	usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
811 	usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
812 	usbtv->vb2q.drv_priv = usbtv;
813 	usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
814 	usbtv->vb2q.ops = &usbtv_vb2_ops;
815 	usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
816 	usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
817 	usbtv->vb2q.lock = &usbtv->vb2q_lock;
818 	ret = vb2_queue_init(&usbtv->vb2q);
819 	if (ret < 0) {
820 		dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
821 		return ret;
822 	}
823 
824 	/* controls */
825 	v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
826 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
827 			V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
828 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
829 			V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
830 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
831 			V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
832 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
833 			V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
834 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
835 			V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
836 	ret = usbtv->ctrl.error;
837 	if (ret < 0) {
838 		dev_warn(usbtv->dev, "Could not initialize controls\n");
839 		goto ctrl_fail;
840 	}
841 
842 	/* v4l2 structure */
843 	usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
844 	usbtv->v4l2_dev.release = usbtv_release;
845 	ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
846 	if (ret < 0) {
847 		dev_warn(usbtv->dev, "Could not register v4l2 device\n");
848 		goto v4l2_fail;
849 	}
850 
851 	/* Video structure */
852 	strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
853 	usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
854 	usbtv->vdev.release = video_device_release_empty;
855 	usbtv->vdev.fops = &usbtv_fops;
856 	usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
857 	usbtv->vdev.tvnorms = USBTV_TV_STD;
858 	usbtv->vdev.queue = &usbtv->vb2q;
859 	usbtv->vdev.lock = &usbtv->v4l2_lock;
860 	video_set_drvdata(&usbtv->vdev, usbtv);
861 	ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
862 	if (ret < 0) {
863 		dev_warn(usbtv->dev, "Could not register video device\n");
864 		goto vdev_fail;
865 	}
866 
867 	return 0;
868 
869 vdev_fail:
870 	v4l2_device_unregister(&usbtv->v4l2_dev);
871 v4l2_fail:
872 ctrl_fail:
873 	v4l2_ctrl_handler_free(&usbtv->ctrl);
874 	vb2_queue_release(&usbtv->vb2q);
875 
876 	return ret;
877 }
878 
879 void usbtv_video_free(struct usbtv *usbtv)
880 {
881 	mutex_lock(&usbtv->vb2q_lock);
882 	mutex_lock(&usbtv->v4l2_lock);
883 
884 	usbtv_stop(usbtv);
885 	video_unregister_device(&usbtv->vdev);
886 	v4l2_device_disconnect(&usbtv->v4l2_dev);
887 
888 	mutex_unlock(&usbtv->v4l2_lock);
889 	mutex_unlock(&usbtv->vb2q_lock);
890 
891 	v4l2_device_put(&usbtv->v4l2_dev);
892 }
893