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