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_625_50,
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 = 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 uint16_t usbtv_norm_to_16f_reg(v4l2_std_id norm)
125 {
126 	/* NTSC M/M-JP/M-KR */
127 	if (norm & V4L2_STD_NTSC)
128 		return 0x00b8;
129 	/* PAL BG/DK/H/I */
130 	if (norm & V4L2_STD_PAL)
131 		return 0x00ee;
132 	/* SECAM B/D/G/H/K/K1/L/Lc */
133 	if (norm & V4L2_STD_SECAM)
134 		return 0x00ff;
135 	if (norm & V4L2_STD_NTSC_443)
136 		return 0x00a8;
137 	if (norm & (V4L2_STD_PAL_M | V4L2_STD_PAL_60))
138 		return 0x00bc;
139 	/* Fallback to automatic detection for other standards */
140 	return 0x0000;
141 }
142 
143 static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
144 {
145 	int ret;
146 	/* These are the series of register values used to configure the
147 	 * decoder for a specific standard.
148 	 * The first 21 register writes are copied from the
149 	 * Settings\DecoderDefaults registry keys present in the Windows driver
150 	 * .INF file, and control various image tuning parameters (color
151 	 * correction, sharpness, ...).
152 	 */
153 	static const u16 pal[][2] = {
154 		/* "AVPAL" tuning sequence from .INF file */
155 		{ USBTV_BASE + 0x0003, 0x0004 },
156 		{ USBTV_BASE + 0x001a, 0x0068 },
157 		{ USBTV_BASE + 0x0100, 0x00d3 },
158 		{ USBTV_BASE + 0x010e, 0x0072 },
159 		{ USBTV_BASE + 0x010f, 0x00a2 },
160 		{ USBTV_BASE + 0x0112, 0x00b0 },
161 		{ USBTV_BASE + 0x0115, 0x0015 },
162 		{ USBTV_BASE + 0x0117, 0x0001 },
163 		{ USBTV_BASE + 0x0118, 0x002c },
164 		{ USBTV_BASE + 0x012d, 0x0010 },
165 		{ USBTV_BASE + 0x012f, 0x0020 },
166 		{ USBTV_BASE + 0x0220, 0x002e },
167 		{ USBTV_BASE + 0x0225, 0x0008 },
168 		{ USBTV_BASE + 0x024e, 0x0002 },
169 		{ USBTV_BASE + 0x024f, 0x0002 },
170 		{ USBTV_BASE + 0x0254, 0x0059 },
171 		{ USBTV_BASE + 0x025a, 0x0016 },
172 		{ USBTV_BASE + 0x025b, 0x0035 },
173 		{ USBTV_BASE + 0x0263, 0x0017 },
174 		{ USBTV_BASE + 0x0266, 0x0016 },
175 		{ USBTV_BASE + 0x0267, 0x0036 },
176 		/* End image tuning */
177 		{ USBTV_BASE + 0x024e, 0x0002 },
178 		{ USBTV_BASE + 0x024f, 0x0002 },
179 	};
180 
181 	static const u16 ntsc[][2] = {
182 		/* "AVNTSC" tuning sequence from .INF file */
183 		{ USBTV_BASE + 0x0003, 0x0004 },
184 		{ USBTV_BASE + 0x001a, 0x0079 },
185 		{ USBTV_BASE + 0x0100, 0x00d3 },
186 		{ USBTV_BASE + 0x010e, 0x0068 },
187 		{ USBTV_BASE + 0x010f, 0x009c },
188 		{ USBTV_BASE + 0x0112, 0x00f0 },
189 		{ USBTV_BASE + 0x0115, 0x0015 },
190 		{ USBTV_BASE + 0x0117, 0x0000 },
191 		{ USBTV_BASE + 0x0118, 0x00fc },
192 		{ USBTV_BASE + 0x012d, 0x0004 },
193 		{ USBTV_BASE + 0x012f, 0x0008 },
194 		{ USBTV_BASE + 0x0220, 0x002e },
195 		{ USBTV_BASE + 0x0225, 0x0008 },
196 		{ USBTV_BASE + 0x024e, 0x0002 },
197 		{ USBTV_BASE + 0x024f, 0x0001 },
198 		{ USBTV_BASE + 0x0254, 0x005f },
199 		{ USBTV_BASE + 0x025a, 0x0012 },
200 		{ USBTV_BASE + 0x025b, 0x0001 },
201 		{ USBTV_BASE + 0x0263, 0x001c },
202 		{ USBTV_BASE + 0x0266, 0x0011 },
203 		{ USBTV_BASE + 0x0267, 0x0005 },
204 		/* End image tuning */
205 		{ USBTV_BASE + 0x024e, 0x0002 },
206 		{ USBTV_BASE + 0x024f, 0x0002 },
207 	};
208 
209 	static const u16 secam[][2] = {
210 		/* "AVSECAM" tuning sequence from .INF file */
211 		{ USBTV_BASE + 0x0003, 0x0004 },
212 		{ USBTV_BASE + 0x001a, 0x0073 },
213 		{ USBTV_BASE + 0x0100, 0x00dc },
214 		{ USBTV_BASE + 0x010e, 0x0072 },
215 		{ USBTV_BASE + 0x010f, 0x00a2 },
216 		{ USBTV_BASE + 0x0112, 0x0090 },
217 		{ USBTV_BASE + 0x0115, 0x0035 },
218 		{ USBTV_BASE + 0x0117, 0x0001 },
219 		{ USBTV_BASE + 0x0118, 0x0030 },
220 		{ USBTV_BASE + 0x012d, 0x0004 },
221 		{ USBTV_BASE + 0x012f, 0x0008 },
222 		{ USBTV_BASE + 0x0220, 0x002d },
223 		{ USBTV_BASE + 0x0225, 0x0028 },
224 		{ USBTV_BASE + 0x024e, 0x0008 },
225 		{ USBTV_BASE + 0x024f, 0x0002 },
226 		{ USBTV_BASE + 0x0254, 0x0069 },
227 		{ USBTV_BASE + 0x025a, 0x0016 },
228 		{ USBTV_BASE + 0x025b, 0x0035 },
229 		{ USBTV_BASE + 0x0263, 0x0021 },
230 		{ USBTV_BASE + 0x0266, 0x0016 },
231 		{ USBTV_BASE + 0x0267, 0x0036 },
232 		/* End image tuning */
233 		{ USBTV_BASE + 0x024e, 0x0002 },
234 		{ USBTV_BASE + 0x024f, 0x0002 },
235 	};
236 
237 	ret = usbtv_configure_for_norm(usbtv, norm);
238 
239 	if (!ret) {
240 		/* Masks for norms using a NTSC or PAL color encoding. */
241 		static const v4l2_std_id ntsc_mask =
242 			V4L2_STD_NTSC | V4L2_STD_NTSC_443;
243 		static const v4l2_std_id pal_mask =
244 			V4L2_STD_PAL | V4L2_STD_PAL_60 | V4L2_STD_PAL_M;
245 
246 		if (norm & ntsc_mask)
247 			ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
248 		else if (norm & pal_mask)
249 			ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
250 		else if (norm & V4L2_STD_SECAM)
251 			ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam));
252 		else
253 			ret = -EINVAL;
254 	}
255 
256 	if (!ret) {
257 		/* Configure the decoder for the color standard */
258 		const u16 cfg[][2] = {
259 			{ USBTV_BASE + 0x016f, usbtv_norm_to_16f_reg(norm) }
260 		};
261 		ret = usbtv_set_regs(usbtv, cfg, ARRAY_SIZE(cfg));
262 	}
263 
264 	return ret;
265 }
266 
267 static int usbtv_setup_capture(struct usbtv *usbtv)
268 {
269 	int ret;
270 	static const u16 setup[][2] = {
271 		/* These seem to enable the device. */
272 		{ USBTV_BASE + 0x0008, 0x0001 },
273 		{ USBTV_BASE + 0x01d0, 0x00ff },
274 		{ USBTV_BASE + 0x01d9, 0x0002 },
275 
276 		/* These seem to influence color parameters, such as
277 		 * brightness, etc. */
278 		{ USBTV_BASE + 0x0239, 0x0040 },
279 		{ USBTV_BASE + 0x0240, 0x0000 },
280 		{ USBTV_BASE + 0x0241, 0x0000 },
281 		{ USBTV_BASE + 0x0242, 0x0002 },
282 		{ USBTV_BASE + 0x0243, 0x0080 },
283 		{ USBTV_BASE + 0x0244, 0x0012 },
284 		{ USBTV_BASE + 0x0245, 0x0090 },
285 		{ USBTV_BASE + 0x0246, 0x0000 },
286 
287 		{ USBTV_BASE + 0x0278, 0x002d },
288 		{ USBTV_BASE + 0x0279, 0x000a },
289 		{ USBTV_BASE + 0x027a, 0x0032 },
290 		{ 0xf890, 0x000c },
291 		{ 0xf894, 0x0086 },
292 
293 		{ USBTV_BASE + 0x00ac, 0x00c0 },
294 		{ USBTV_BASE + 0x00ad, 0x0000 },
295 		{ USBTV_BASE + 0x00a2, 0x0012 },
296 		{ USBTV_BASE + 0x00a3, 0x00e0 },
297 		{ USBTV_BASE + 0x00a4, 0x0028 },
298 		{ USBTV_BASE + 0x00a5, 0x0082 },
299 		{ USBTV_BASE + 0x00a7, 0x0080 },
300 		{ USBTV_BASE + 0x0000, 0x0014 },
301 		{ USBTV_BASE + 0x0006, 0x0003 },
302 		{ USBTV_BASE + 0x0090, 0x0099 },
303 		{ USBTV_BASE + 0x0091, 0x0090 },
304 		{ USBTV_BASE + 0x0094, 0x0068 },
305 		{ USBTV_BASE + 0x0095, 0x0070 },
306 		{ USBTV_BASE + 0x009c, 0x0030 },
307 		{ USBTV_BASE + 0x009d, 0x00c0 },
308 		{ USBTV_BASE + 0x009e, 0x00e0 },
309 		{ USBTV_BASE + 0x0019, 0x0006 },
310 		{ USBTV_BASE + 0x008c, 0x00ba },
311 		{ USBTV_BASE + 0x0101, 0x00ff },
312 		{ USBTV_BASE + 0x010c, 0x00b3 },
313 		{ USBTV_BASE + 0x01b2, 0x0080 },
314 		{ USBTV_BASE + 0x01b4, 0x00a0 },
315 		{ USBTV_BASE + 0x014c, 0x00ff },
316 		{ USBTV_BASE + 0x014d, 0x00ca },
317 		{ USBTV_BASE + 0x0113, 0x0053 },
318 		{ USBTV_BASE + 0x0119, 0x008a },
319 		{ USBTV_BASE + 0x013c, 0x0003 },
320 		{ USBTV_BASE + 0x0150, 0x009c },
321 		{ USBTV_BASE + 0x0151, 0x0071 },
322 		{ USBTV_BASE + 0x0152, 0x00c6 },
323 		{ USBTV_BASE + 0x0153, 0x0084 },
324 		{ USBTV_BASE + 0x0154, 0x00bc },
325 		{ USBTV_BASE + 0x0155, 0x00a0 },
326 		{ USBTV_BASE + 0x0156, 0x00a0 },
327 		{ USBTV_BASE + 0x0157, 0x009c },
328 		{ USBTV_BASE + 0x0158, 0x001f },
329 		{ USBTV_BASE + 0x0159, 0x0006 },
330 		{ USBTV_BASE + 0x015d, 0x0000 },
331 	};
332 
333 	ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
334 	if (ret)
335 		return ret;
336 
337 	ret = usbtv_select_norm(usbtv, usbtv->norm);
338 	if (ret)
339 		return ret;
340 
341 	ret = usbtv_select_input(usbtv, usbtv->input);
342 	if (ret)
343 		return ret;
344 
345 	ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
346 	if (ret)
347 		return ret;
348 
349 	return 0;
350 }
351 
352 /* Copy data from chunk into a frame buffer, deinterlacing the data
353  * into every second line. Unfortunately, they don't align nicely into
354  * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
355  * Therefore, we break down the chunk into two halves before copying,
356  * so that we can interleave a line if needed.
357  *
358  * Each "chunk" is 240 words; a word in this context equals 4 bytes.
359  * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
360  * pixels, the Cr and Cb shared between the two pixels, but each having
361  * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
362  * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
363  * The image is interlaced, so there is a "scan" of odd lines, followed
364  * by "scan" of even numbered lines.
365  *
366  * Following code is writing the chunks in correct sequence, skipping
367  * the rows based on "odd" value.
368  * line 1: chunk[0][  0..479] chunk[0][480..959] chunk[1][  0..479]
369  * line 3: chunk[1][480..959] chunk[2][  0..479] chunk[2][480..959]
370  * ...etc.
371  */
372 static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
373 {
374 	int half;
375 
376 	for (half = 0; half < 2; half++) {
377 		int part_no = chunk_no * 2 + half;
378 		int line = part_no / 3;
379 		int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
380 
381 		u32 *dst = &frame[part_index * USBTV_CHUNK/2];
382 
383 		memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
384 		src += USBTV_CHUNK/2;
385 	}
386 }
387 
388 /* Called for each 256-byte image chunk.
389  * First word identifies the chunk, followed by 240 words of image
390  * data and padding. */
391 static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
392 {
393 	int frame_id, odd, chunk_no;
394 	u32 *frame;
395 	struct usbtv_buf *buf;
396 	unsigned long flags;
397 
398 	/* Ignore corrupted lines. */
399 	if (!USBTV_MAGIC_OK(chunk))
400 		return;
401 	frame_id = USBTV_FRAME_ID(chunk);
402 	odd = USBTV_ODD(chunk);
403 	chunk_no = USBTV_CHUNK_NO(chunk);
404 	if (chunk_no >= usbtv->n_chunks)
405 		return;
406 
407 	/* Beginning of a frame. */
408 	if (chunk_no == 0) {
409 		usbtv->frame_id = frame_id;
410 		usbtv->chunks_done = 0;
411 	}
412 
413 	if (usbtv->frame_id != frame_id)
414 		return;
415 
416 	spin_lock_irqsave(&usbtv->buflock, flags);
417 	if (list_empty(&usbtv->bufs)) {
418 		/* No free buffers. Userspace likely too slow. */
419 		spin_unlock_irqrestore(&usbtv->buflock, flags);
420 		return;
421 	}
422 
423 	/* First available buffer. */
424 	buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
425 	frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
426 
427 	/* Copy the chunk data. */
428 	usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
429 	usbtv->chunks_done++;
430 
431 	/* Last chunk in a field */
432 	if (chunk_no == usbtv->n_chunks-1) {
433 		/* Last chunk in a frame, signalling an end */
434 		if (odd && !usbtv->last_odd) {
435 			int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
436 			enum vb2_buffer_state state = usbtv->chunks_done ==
437 				usbtv->n_chunks ?
438 				VB2_BUF_STATE_DONE :
439 				VB2_BUF_STATE_ERROR;
440 
441 			buf->vb.field = V4L2_FIELD_INTERLACED;
442 			buf->vb.sequence = usbtv->sequence++;
443 			buf->vb.vb2_buf.timestamp = ktime_get_ns();
444 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
445 			vb2_buffer_done(&buf->vb.vb2_buf, state);
446 			list_del(&buf->list);
447 		}
448 		usbtv->last_odd = odd;
449 	}
450 
451 	spin_unlock_irqrestore(&usbtv->buflock, flags);
452 }
453 
454 /* Got image data. Each packet contains a number of 256-word chunks we
455  * compose the image from. */
456 static void usbtv_iso_cb(struct urb *ip)
457 {
458 	int ret;
459 	int i;
460 	struct usbtv *usbtv = (struct usbtv *)ip->context;
461 
462 	switch (ip->status) {
463 	/* All fine. */
464 	case 0:
465 		break;
466 	/* Device disconnected or capture stopped? */
467 	case -ENODEV:
468 	case -ENOENT:
469 	case -ECONNRESET:
470 	case -ESHUTDOWN:
471 		return;
472 	/* Unknown error. Retry. */
473 	default:
474 		dev_warn(usbtv->dev, "Bad response for ISO request.\n");
475 		goto resubmit;
476 	}
477 
478 	for (i = 0; i < ip->number_of_packets; i++) {
479 		int size = ip->iso_frame_desc[i].actual_length;
480 		unsigned char *data = ip->transfer_buffer +
481 				ip->iso_frame_desc[i].offset;
482 		int offset;
483 
484 		for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
485 			usbtv_image_chunk(usbtv,
486 				(__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
487 	}
488 
489 resubmit:
490 	ret = usb_submit_urb(ip, GFP_ATOMIC);
491 	if (ret < 0)
492 		dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
493 }
494 
495 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
496 {
497 	struct urb *ip;
498 	int size = usbtv->iso_size;
499 	int i;
500 
501 	ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
502 	if (ip == NULL)
503 		return NULL;
504 
505 	ip->dev = usbtv->udev;
506 	ip->context = usbtv;
507 	ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
508 	ip->interval = 1;
509 	ip->transfer_flags = URB_ISO_ASAP;
510 	ip->transfer_buffer = kcalloc(USBTV_ISOC_PACKETS, size,
511 						GFP_KERNEL);
512 	if (!ip->transfer_buffer) {
513 		usb_free_urb(ip);
514 		return NULL;
515 	}
516 	ip->complete = usbtv_iso_cb;
517 	ip->number_of_packets = USBTV_ISOC_PACKETS;
518 	ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
519 	for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
520 		ip->iso_frame_desc[i].offset = size * i;
521 		ip->iso_frame_desc[i].length = size;
522 	}
523 
524 	return ip;
525 }
526 
527 static void usbtv_stop(struct usbtv *usbtv)
528 {
529 	int i;
530 	unsigned long flags;
531 
532 	/* Cancel running transfers. */
533 	for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
534 		struct urb *ip = usbtv->isoc_urbs[i];
535 
536 		if (ip == NULL)
537 			continue;
538 		usb_kill_urb(ip);
539 		kfree(ip->transfer_buffer);
540 		usb_free_urb(ip);
541 		usbtv->isoc_urbs[i] = NULL;
542 	}
543 
544 	/* Return buffers to userspace. */
545 	spin_lock_irqsave(&usbtv->buflock, flags);
546 	while (!list_empty(&usbtv->bufs)) {
547 		struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
548 						struct usbtv_buf, list);
549 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
550 		list_del(&buf->list);
551 	}
552 	spin_unlock_irqrestore(&usbtv->buflock, flags);
553 }
554 
555 static int usbtv_start(struct usbtv *usbtv)
556 {
557 	int i;
558 	int ret;
559 
560 	usbtv_audio_suspend(usbtv);
561 
562 	ret = usb_set_interface(usbtv->udev, 0, 0);
563 	if (ret < 0)
564 		return ret;
565 
566 	ret = usbtv_setup_capture(usbtv);
567 	if (ret < 0)
568 		return ret;
569 
570 	ret = usb_set_interface(usbtv->udev, 0, 1);
571 	if (ret < 0)
572 		return ret;
573 
574 	usbtv_audio_resume(usbtv);
575 
576 	for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
577 		struct urb *ip;
578 
579 		ip = usbtv_setup_iso_transfer(usbtv);
580 		if (ip == NULL) {
581 			ret = -ENOMEM;
582 			goto start_fail;
583 		}
584 		usbtv->isoc_urbs[i] = ip;
585 
586 		ret = usb_submit_urb(ip, GFP_KERNEL);
587 		if (ret < 0)
588 			goto start_fail;
589 	}
590 
591 	return 0;
592 
593 start_fail:
594 	usbtv_stop(usbtv);
595 	return ret;
596 }
597 
598 static int usbtv_querycap(struct file *file, void *priv,
599 				struct v4l2_capability *cap)
600 {
601 	struct usbtv *dev = video_drvdata(file);
602 
603 	strscpy(cap->driver, "usbtv", sizeof(cap->driver));
604 	strscpy(cap->card, "usbtv", sizeof(cap->card));
605 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
606 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
607 	cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
608 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
609 	return 0;
610 }
611 
612 static int usbtv_enum_input(struct file *file, void *priv,
613 					struct v4l2_input *i)
614 {
615 	struct usbtv *dev = video_drvdata(file);
616 
617 	switch (i->index) {
618 	case USBTV_COMPOSITE_INPUT:
619 		strscpy(i->name, "Composite", sizeof(i->name));
620 		break;
621 	case USBTV_SVIDEO_INPUT:
622 		strscpy(i->name, "S-Video", sizeof(i->name));
623 		break;
624 	default:
625 		return -EINVAL;
626 	}
627 
628 	i->type = V4L2_INPUT_TYPE_CAMERA;
629 	i->std = dev->vdev.tvnorms;
630 	return 0;
631 }
632 
633 static int usbtv_enum_fmt_vid_cap(struct file *file, void  *priv,
634 					struct v4l2_fmtdesc *f)
635 {
636 	if (f->index > 0)
637 		return -EINVAL;
638 
639 	strscpy(f->description, "16 bpp YUY2, 4:2:2, packed",
640 		sizeof(f->description));
641 	f->pixelformat = V4L2_PIX_FMT_YUYV;
642 	return 0;
643 }
644 
645 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
646 					struct v4l2_format *f)
647 {
648 	struct usbtv *usbtv = video_drvdata(file);
649 
650 	f->fmt.pix.width = usbtv->width;
651 	f->fmt.pix.height = usbtv->height;
652 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
653 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
654 	f->fmt.pix.bytesperline = usbtv->width * 2;
655 	f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
656 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
657 
658 	return 0;
659 }
660 
661 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
662 {
663 	struct usbtv *usbtv = video_drvdata(file);
664 	*norm = usbtv->norm;
665 	return 0;
666 }
667 
668 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
669 {
670 	int ret = -EINVAL;
671 	struct usbtv *usbtv = video_drvdata(file);
672 
673 	if (norm & USBTV_TV_STD)
674 		ret = usbtv_select_norm(usbtv, norm);
675 
676 	return ret;
677 }
678 
679 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
680 {
681 	struct usbtv *usbtv = video_drvdata(file);
682 	*i = usbtv->input;
683 	return 0;
684 }
685 
686 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
687 {
688 	struct usbtv *usbtv = video_drvdata(file);
689 
690 	return usbtv_select_input(usbtv, i);
691 }
692 
693 static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
694 	.vidioc_querycap = usbtv_querycap,
695 	.vidioc_enum_input = usbtv_enum_input,
696 	.vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
697 	.vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
698 	.vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
699 	.vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
700 	.vidioc_g_std = usbtv_g_std,
701 	.vidioc_s_std = usbtv_s_std,
702 	.vidioc_g_input = usbtv_g_input,
703 	.vidioc_s_input = usbtv_s_input,
704 
705 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
706 	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
707 	.vidioc_querybuf = vb2_ioctl_querybuf,
708 	.vidioc_create_bufs = vb2_ioctl_create_bufs,
709 	.vidioc_qbuf = vb2_ioctl_qbuf,
710 	.vidioc_dqbuf = vb2_ioctl_dqbuf,
711 	.vidioc_streamon = vb2_ioctl_streamon,
712 	.vidioc_streamoff = vb2_ioctl_streamoff,
713 };
714 
715 static const struct v4l2_file_operations usbtv_fops = {
716 	.owner = THIS_MODULE,
717 	.unlocked_ioctl = video_ioctl2,
718 	.mmap = vb2_fop_mmap,
719 	.open = v4l2_fh_open,
720 	.release = vb2_fop_release,
721 	.read = vb2_fop_read,
722 	.poll = vb2_fop_poll,
723 };
724 
725 static int usbtv_queue_setup(struct vb2_queue *vq,
726 	unsigned int *nbuffers,
727 	unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
728 {
729 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
730 	unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
731 
732 	if (vq->num_buffers + *nbuffers < 2)
733 		*nbuffers = 2 - vq->num_buffers;
734 	if (*nplanes)
735 		return sizes[0] < size ? -EINVAL : 0;
736 	*nplanes = 1;
737 	sizes[0] = size;
738 
739 	return 0;
740 }
741 
742 static void usbtv_buf_queue(struct vb2_buffer *vb)
743 {
744 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
745 	struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
746 	struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
747 	unsigned long flags;
748 
749 	if (usbtv->udev == NULL) {
750 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
751 		return;
752 	}
753 
754 	spin_lock_irqsave(&usbtv->buflock, flags);
755 	list_add_tail(&buf->list, &usbtv->bufs);
756 	spin_unlock_irqrestore(&usbtv->buflock, flags);
757 }
758 
759 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
760 {
761 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
762 
763 	if (usbtv->udev == NULL)
764 		return -ENODEV;
765 
766 	usbtv->last_odd = 1;
767 	usbtv->sequence = 0;
768 	return usbtv_start(usbtv);
769 }
770 
771 static void usbtv_stop_streaming(struct vb2_queue *vq)
772 {
773 	struct usbtv *usbtv = vb2_get_drv_priv(vq);
774 
775 	if (usbtv->udev)
776 		usbtv_stop(usbtv);
777 }
778 
779 static const struct vb2_ops usbtv_vb2_ops = {
780 	.queue_setup = usbtv_queue_setup,
781 	.buf_queue = usbtv_buf_queue,
782 	.start_streaming = usbtv_start_streaming,
783 	.stop_streaming = usbtv_stop_streaming,
784 	.wait_prepare = vb2_ops_wait_prepare,
785 	.wait_finish = vb2_ops_wait_finish,
786 };
787 
788 static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
789 {
790 	struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
791 								ctrl);
792 	u8 *data;
793 	u16 index, size;
794 	int ret;
795 
796 	data = kmalloc(3, GFP_KERNEL);
797 	if (!data)
798 		return -ENOMEM;
799 
800 	/*
801 	 * Read in the current brightness/contrast registers. We need them
802 	 * both, because the values are for some reason interleaved.
803 	 */
804 	if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
805 		ret = usb_control_msg(usbtv->udev,
806 			usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
807 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
808 			0, USBTV_BASE + 0x0244, (void *)data, 3, 0);
809 		if (ret < 0)
810 			goto error;
811 	}
812 
813 	switch (ctrl->id) {
814 	case V4L2_CID_BRIGHTNESS:
815 		index = USBTV_BASE + 0x0244;
816 		size = 3;
817 		data[0] &= 0xf0;
818 		data[0] |= (ctrl->val >> 8) & 0xf;
819 		data[2] = ctrl->val & 0xff;
820 		break;
821 	case V4L2_CID_CONTRAST:
822 		index = USBTV_BASE + 0x0244;
823 		size = 3;
824 		data[0] &= 0x0f;
825 		data[0] |= (ctrl->val >> 4) & 0xf0;
826 		data[1] = ctrl->val & 0xff;
827 		break;
828 	case V4L2_CID_SATURATION:
829 		index = USBTV_BASE + 0x0242;
830 		data[0] = ctrl->val >> 8;
831 		data[1] = ctrl->val & 0xff;
832 		size = 2;
833 		break;
834 	case V4L2_CID_HUE:
835 		index = USBTV_BASE + 0x0240;
836 		size = 2;
837 		if (ctrl->val > 0) {
838 			data[0] = 0x92 + (ctrl->val >> 8);
839 			data[1] = ctrl->val & 0xff;
840 		} else {
841 			data[0] = 0x82 + (-ctrl->val >> 8);
842 			data[1] = -ctrl->val & 0xff;
843 		}
844 		break;
845 	case V4L2_CID_SHARPNESS:
846 		index = USBTV_BASE + 0x0239;
847 		data[0] = 0;
848 		data[1] = ctrl->val;
849 		size = 2;
850 		break;
851 	default:
852 		kfree(data);
853 		return -EINVAL;
854 	}
855 
856 	ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
857 			USBTV_CONTROL_REG,
858 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
859 			0, index, (void *)data, size, 0);
860 
861 error:
862 	if (ret < 0)
863 		dev_warn(usbtv->dev, "Failed to submit a control request.\n");
864 
865 	kfree(data);
866 	return ret;
867 }
868 
869 static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
870 	.s_ctrl = usbtv_s_ctrl,
871 };
872 
873 static void usbtv_release(struct v4l2_device *v4l2_dev)
874 {
875 	struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
876 
877 	v4l2_device_unregister(&usbtv->v4l2_dev);
878 	v4l2_ctrl_handler_free(&usbtv->ctrl);
879 	vb2_queue_release(&usbtv->vb2q);
880 	kfree(usbtv);
881 }
882 
883 int usbtv_video_init(struct usbtv *usbtv)
884 {
885 	int ret;
886 
887 	(void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
888 
889 	spin_lock_init(&usbtv->buflock);
890 	mutex_init(&usbtv->v4l2_lock);
891 	mutex_init(&usbtv->vb2q_lock);
892 	INIT_LIST_HEAD(&usbtv->bufs);
893 
894 	/* videobuf2 structure */
895 	usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
896 	usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
897 	usbtv->vb2q.drv_priv = usbtv;
898 	usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
899 	usbtv->vb2q.ops = &usbtv_vb2_ops;
900 	usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
901 	usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
902 	usbtv->vb2q.lock = &usbtv->vb2q_lock;
903 	ret = vb2_queue_init(&usbtv->vb2q);
904 	if (ret < 0) {
905 		dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
906 		return ret;
907 	}
908 
909 	/* controls */
910 	v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
911 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
912 			V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
913 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
914 			V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
915 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
916 			V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
917 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
918 			V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
919 	v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
920 			V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
921 	ret = usbtv->ctrl.error;
922 	if (ret < 0) {
923 		dev_warn(usbtv->dev, "Could not initialize controls\n");
924 		goto ctrl_fail;
925 	}
926 
927 	/* v4l2 structure */
928 	usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
929 	usbtv->v4l2_dev.release = usbtv_release;
930 	ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
931 	if (ret < 0) {
932 		dev_warn(usbtv->dev, "Could not register v4l2 device\n");
933 		goto v4l2_fail;
934 	}
935 
936 	/* Video structure */
937 	strscpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
938 	usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
939 	usbtv->vdev.release = video_device_release_empty;
940 	usbtv->vdev.fops = &usbtv_fops;
941 	usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
942 	usbtv->vdev.tvnorms = USBTV_TV_STD;
943 	usbtv->vdev.queue = &usbtv->vb2q;
944 	usbtv->vdev.lock = &usbtv->v4l2_lock;
945 	video_set_drvdata(&usbtv->vdev, usbtv);
946 	ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
947 	if (ret < 0) {
948 		dev_warn(usbtv->dev, "Could not register video device\n");
949 		goto vdev_fail;
950 	}
951 
952 	return 0;
953 
954 vdev_fail:
955 	v4l2_device_unregister(&usbtv->v4l2_dev);
956 v4l2_fail:
957 ctrl_fail:
958 	v4l2_ctrl_handler_free(&usbtv->ctrl);
959 	vb2_queue_release(&usbtv->vb2q);
960 
961 	return ret;
962 }
963 
964 void usbtv_video_free(struct usbtv *usbtv)
965 {
966 	mutex_lock(&usbtv->vb2q_lock);
967 	mutex_lock(&usbtv->v4l2_lock);
968 
969 	usbtv_stop(usbtv);
970 	video_unregister_device(&usbtv->vdev);
971 	v4l2_device_disconnect(&usbtv->v4l2_dev);
972 
973 	mutex_unlock(&usbtv->v4l2_lock);
974 	mutex_unlock(&usbtv->vb2q_lock);
975 
976 	v4l2_device_put(&usbtv->v4l2_dev);
977 }
978