blob: 6b0a1017338813b3abb88e7c2141820c5e8760db [file] [log] [blame]
Lubomir Rintelf3d27f32013-06-17 10:29:06 -03001/*
Lubomir Rintelc53a8462016-10-16 07:38:22 -02002 * Copyright (c) 2013,2016 Lubomir Rintel
Lubomir Rintelf3d27f32013-06-17 10:29:06 -03003 * All rights reserved.
Lubomir Rintelf3d27f32013-06-17 10:29:06 -03004 *
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").
Lubomir Rintel5ae1e2b2016-06-01 10:03:44 -030016 *
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
Lubomir Rintelf3d27f32013-06-17 10:29:06 -030043 */
44
Lubomir Rintelf3d27f32013-06-17 10:29:06 -030045#include <media/v4l2-ioctl.h>
Junghak Sungc1399902015-09-22 10:30:29 -030046#include <media/videobuf2-v4l2.h>
Lubomir Rintelf3d27f32013-06-17 10:29:06 -030047
Federico Simoncellia3550ea2014-01-07 19:13:21 -030048#include "usbtv.h"
Georg Kaindl0e0fe392013-10-21 12:01:36 -030049
50static 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,
Hugo Grostabussiatc41e20d2018-04-08 17:11:57 -040060 },
61 {
62 .norm = V4L2_STD_SECAM,
63 .cap_width = 720,
64 .cap_height = 576,
Georg Kaindl0e0fe392013-10-21 12:01:36 -030065 }
66};
67
Georg Kaindl0e0fe392013-10-21 12:01:36 -030068static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
69{
70 int i, ret = 0;
71 struct usbtv_norm_params *params = NULL;
72
73 for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
74 if (norm_params[i].norm & norm) {
75 params = &norm_params[i];
76 break;
77 }
78 }
79
80 if (params) {
81 usbtv->width = params->cap_width;
82 usbtv->height = params->cap_height;
83 usbtv->n_chunks = usbtv->width * usbtv->height
84 / 4 / USBTV_CHUNK;
85 usbtv->norm = params->norm;
86 } else
87 ret = -EINVAL;
88
89 return ret;
90}
91
Lubomir Rintel22061122013-07-12 05:03:03 -030092static int usbtv_select_input(struct usbtv *usbtv, int input)
93{
94 int ret;
95
96 static const u16 composite[][2] = {
97 { USBTV_BASE + 0x0105, 0x0060 },
98 { USBTV_BASE + 0x011f, 0x00f2 },
99 { USBTV_BASE + 0x0127, 0x0060 },
100 { USBTV_BASE + 0x00ae, 0x0010 },
Lubomir Rintel22061122013-07-12 05:03:03 -0300101 { USBTV_BASE + 0x0239, 0x0060 },
102 };
103
104 static const u16 svideo[][2] = {
105 { USBTV_BASE + 0x0105, 0x0010 },
106 { USBTV_BASE + 0x011f, 0x00ff },
107 { USBTV_BASE + 0x0127, 0x0060 },
108 { USBTV_BASE + 0x00ae, 0x0030 },
Lubomir Rintel22061122013-07-12 05:03:03 -0300109 { USBTV_BASE + 0x0239, 0x0060 },
110 };
111
112 switch (input) {
113 case USBTV_COMPOSITE_INPUT:
114 ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
115 break;
116 case USBTV_SVIDEO_INPUT:
117 ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
118 break;
119 default:
120 ret = -EINVAL;
121 }
122
123 if (!ret)
124 usbtv->input = input;
125
126 return ret;
127}
128
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300129static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
130{
131 int ret;
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400132 /* These are the series of register values used to configure the
133 * decoder for a specific standard.
134 * The first 21 register writes are copied from the
135 * Settings\DecoderDefaults registry keys present in the Windows driver
136 * .INF file, and control various image tuning parameters (color
137 * correction, sharpness, ...).
138 */
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300139 static const u16 pal[][2] = {
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400140 /* "AVPAL" tuning sequence from .INF file */
141 { USBTV_BASE + 0x0003, 0x0004 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300142 { USBTV_BASE + 0x001a, 0x0068 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400143 { USBTV_BASE + 0x0100, 0x00d3 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300144 { USBTV_BASE + 0x010e, 0x0072 },
145 { USBTV_BASE + 0x010f, 0x00a2 },
146 { USBTV_BASE + 0x0112, 0x00b0 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400147 { USBTV_BASE + 0x0115, 0x0015 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300148 { USBTV_BASE + 0x0117, 0x0001 },
149 { USBTV_BASE + 0x0118, 0x002c },
150 { USBTV_BASE + 0x012d, 0x0010 },
151 { USBTV_BASE + 0x012f, 0x0020 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400152 { USBTV_BASE + 0x0220, 0x002e },
153 { USBTV_BASE + 0x0225, 0x0008 },
154 { USBTV_BASE + 0x024e, 0x0002 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300155 { USBTV_BASE + 0x024f, 0x0002 },
156 { USBTV_BASE + 0x0254, 0x0059 },
157 { USBTV_BASE + 0x025a, 0x0016 },
158 { USBTV_BASE + 0x025b, 0x0035 },
159 { USBTV_BASE + 0x0263, 0x0017 },
160 { USBTV_BASE + 0x0266, 0x0016 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400161 { USBTV_BASE + 0x0267, 0x0036 },
162 /* Epilog */
163 { USBTV_BASE + 0x024e, 0x0002 },
164 { USBTV_BASE + 0x024f, 0x0002 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300165 };
166
167 static const u16 ntsc[][2] = {
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400168 /* "AVNTSC" tuning sequence from .INF file */
169 { USBTV_BASE + 0x0003, 0x0004 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300170 { USBTV_BASE + 0x001a, 0x0079 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400171 { USBTV_BASE + 0x0100, 0x00d3 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300172 { USBTV_BASE + 0x010e, 0x0068 },
173 { USBTV_BASE + 0x010f, 0x009c },
174 { USBTV_BASE + 0x0112, 0x00f0 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400175 { USBTV_BASE + 0x0115, 0x0015 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300176 { USBTV_BASE + 0x0117, 0x0000 },
177 { USBTV_BASE + 0x0118, 0x00fc },
178 { USBTV_BASE + 0x012d, 0x0004 },
179 { USBTV_BASE + 0x012f, 0x0008 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400180 { USBTV_BASE + 0x0220, 0x002e },
181 { USBTV_BASE + 0x0225, 0x0008 },
182 { USBTV_BASE + 0x024e, 0x0002 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300183 { USBTV_BASE + 0x024f, 0x0001 },
184 { USBTV_BASE + 0x0254, 0x005f },
185 { USBTV_BASE + 0x025a, 0x0012 },
186 { USBTV_BASE + 0x025b, 0x0001 },
187 { USBTV_BASE + 0x0263, 0x001c },
188 { USBTV_BASE + 0x0266, 0x0011 },
Hugo Grostabussiat5c909be2018-04-08 17:11:56 -0400189 { USBTV_BASE + 0x0267, 0x0005 },
190 /* Epilog */
191 { USBTV_BASE + 0x024e, 0x0002 },
192 { USBTV_BASE + 0x024f, 0x0002 },
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300193 };
194
Hugo Grostabussiatc41e20d2018-04-08 17:11:57 -0400195 static const u16 secam[][2] = {
196 /* "AVSECAM" tuning sequence from .INF file */
197 { USBTV_BASE + 0x0003, 0x0004 },
198 { USBTV_BASE + 0x001a, 0x0073 },
199 { USBTV_BASE + 0x0100, 0x00dc },
200 { USBTV_BASE + 0x010e, 0x0072 },
201 { USBTV_BASE + 0x010f, 0x00a2 },
202 { USBTV_BASE + 0x0112, 0x0090 },
203 { USBTV_BASE + 0x0115, 0x0035 },
204 { USBTV_BASE + 0x0117, 0x0001 },
205 { USBTV_BASE + 0x0118, 0x0030 },
206 { USBTV_BASE + 0x012d, 0x0004 },
207 { USBTV_BASE + 0x012f, 0x0008 },
208 { USBTV_BASE + 0x0220, 0x002d },
209 { USBTV_BASE + 0x0225, 0x0028 },
210 { USBTV_BASE + 0x024e, 0x0008 },
211 { USBTV_BASE + 0x024f, 0x0002 },
212 { USBTV_BASE + 0x0254, 0x0069 },
213 { USBTV_BASE + 0x025a, 0x0016 },
214 { USBTV_BASE + 0x025b, 0x0035 },
215 { USBTV_BASE + 0x0263, 0x0021 },
216 { USBTV_BASE + 0x0266, 0x0016 },
217 { USBTV_BASE + 0x0267, 0x0036 },
218 /* Epilog */
219 { USBTV_BASE + 0x024e, 0x0002 },
220 { USBTV_BASE + 0x024f, 0x0002 },
221 };
222
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300223 ret = usbtv_configure_for_norm(usbtv, norm);
224
225 if (!ret) {
226 if (norm & V4L2_STD_525_60)
227 ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
228 else if (norm & V4L2_STD_PAL)
229 ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
Hugo Grostabussiatc41e20d2018-04-08 17:11:57 -0400230 else if (norm & V4L2_STD_SECAM)
231 ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam));
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300232 }
233
234 return ret;
235}
236
Lubomir Rintel22061122013-07-12 05:03:03 -0300237static int usbtv_setup_capture(struct usbtv *usbtv)
238{
239 int ret;
240 static const u16 setup[][2] = {
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300241 /* These seem to enable the device. */
242 { USBTV_BASE + 0x0008, 0x0001 },
243 { USBTV_BASE + 0x01d0, 0x00ff },
244 { USBTV_BASE + 0x01d9, 0x0002 },
245
246 /* These seem to influence color parameters, such as
247 * brightness, etc. */
248 { USBTV_BASE + 0x0239, 0x0040 },
249 { USBTV_BASE + 0x0240, 0x0000 },
250 { USBTV_BASE + 0x0241, 0x0000 },
251 { USBTV_BASE + 0x0242, 0x0002 },
252 { USBTV_BASE + 0x0243, 0x0080 },
253 { USBTV_BASE + 0x0244, 0x0012 },
254 { USBTV_BASE + 0x0245, 0x0090 },
255 { USBTV_BASE + 0x0246, 0x0000 },
256
257 { USBTV_BASE + 0x0278, 0x002d },
258 { USBTV_BASE + 0x0279, 0x000a },
259 { USBTV_BASE + 0x027a, 0x0032 },
260 { 0xf890, 0x000c },
261 { 0xf894, 0x0086 },
262
263 { USBTV_BASE + 0x00ac, 0x00c0 },
264 { USBTV_BASE + 0x00ad, 0x0000 },
265 { USBTV_BASE + 0x00a2, 0x0012 },
266 { USBTV_BASE + 0x00a3, 0x00e0 },
267 { USBTV_BASE + 0x00a4, 0x0028 },
268 { USBTV_BASE + 0x00a5, 0x0082 },
269 { USBTV_BASE + 0x00a7, 0x0080 },
270 { USBTV_BASE + 0x0000, 0x0014 },
271 { USBTV_BASE + 0x0006, 0x0003 },
272 { USBTV_BASE + 0x0090, 0x0099 },
273 { USBTV_BASE + 0x0091, 0x0090 },
274 { USBTV_BASE + 0x0094, 0x0068 },
275 { USBTV_BASE + 0x0095, 0x0070 },
276 { USBTV_BASE + 0x009c, 0x0030 },
277 { USBTV_BASE + 0x009d, 0x00c0 },
278 { USBTV_BASE + 0x009e, 0x00e0 },
279 { USBTV_BASE + 0x0019, 0x0006 },
280 { USBTV_BASE + 0x008c, 0x00ba },
281 { USBTV_BASE + 0x0101, 0x00ff },
282 { USBTV_BASE + 0x010c, 0x00b3 },
283 { USBTV_BASE + 0x01b2, 0x0080 },
284 { USBTV_BASE + 0x01b4, 0x00a0 },
285 { USBTV_BASE + 0x014c, 0x00ff },
286 { USBTV_BASE + 0x014d, 0x00ca },
287 { USBTV_BASE + 0x0113, 0x0053 },
288 { USBTV_BASE + 0x0119, 0x008a },
289 { USBTV_BASE + 0x013c, 0x0003 },
290 { USBTV_BASE + 0x0150, 0x009c },
291 { USBTV_BASE + 0x0151, 0x0071 },
292 { USBTV_BASE + 0x0152, 0x00c6 },
293 { USBTV_BASE + 0x0153, 0x0084 },
294 { USBTV_BASE + 0x0154, 0x00bc },
295 { USBTV_BASE + 0x0155, 0x00a0 },
296 { USBTV_BASE + 0x0156, 0x00a0 },
297 { USBTV_BASE + 0x0157, 0x009c },
298 { USBTV_BASE + 0x0158, 0x001f },
299 { USBTV_BASE + 0x0159, 0x0006 },
300 { USBTV_BASE + 0x015d, 0x0000 },
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300301 };
302
Lubomir Rintel22061122013-07-12 05:03:03 -0300303 ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
304 if (ret)
305 return ret;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300306
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300307 ret = usbtv_select_norm(usbtv, usbtv->norm);
308 if (ret)
309 return ret;
310
Lubomir Rintel22061122013-07-12 05:03:03 -0300311 ret = usbtv_select_input(usbtv, usbtv->input);
312 if (ret)
313 return ret;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300314
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200315 ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
316 if (ret)
317 return ret;
318
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300319 return 0;
320}
321
Lubomir Rintel30800722013-07-02 07:56:38 -0300322/* Copy data from chunk into a frame buffer, deinterlacing the data
323 * into every second line. Unfortunately, they don't align nicely into
324 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
Lubomir Rintel8ed50eb2016-06-01 10:04:07 -0300325 * Therefore, we break down the chunk into two halves before copying,
326 * so that we can interleave a line if needed.
327 *
328 * Each "chunk" is 240 words; a word in this context equals 4 bytes.
329 * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
330 * pixels, the Cr and Cb shared between the two pixels, but each having
331 * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
332 * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
333 * The image is interlaced, so there is a "scan" of odd lines, followed
334 * by "scan" of even numbered lines.
335 *
336 * Following code is writing the chunks in correct sequence, skipping
337 * the rows based on "odd" value.
338 * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
339 * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
340 * ...etc.
341 */
Hans Verkuil6fbf4d02014-08-20 18:03:53 -0300342static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
Lubomir Rintel30800722013-07-02 07:56:38 -0300343{
344 int half;
345
346 for (half = 0; half < 2; half++) {
347 int part_no = chunk_no * 2 + half;
348 int line = part_no / 3;
349 int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
350
351 u32 *dst = &frame[part_index * USBTV_CHUNK/2];
Amber Thrall146af9c2014-09-20 01:03:15 -0300352
Lubomir Rintel30800722013-07-02 07:56:38 -0300353 memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
354 src += USBTV_CHUNK/2;
355 }
356}
357
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300358/* Called for each 256-byte image chunk.
359 * First word identifies the chunk, followed by 240 words of image
360 * data and padding. */
Hans Verkuil6fbf4d02014-08-20 18:03:53 -0300361static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300362{
363 int frame_id, odd, chunk_no;
364 u32 *frame;
365 struct usbtv_buf *buf;
366 unsigned long flags;
367
368 /* Ignore corrupted lines. */
369 if (!USBTV_MAGIC_OK(chunk))
370 return;
371 frame_id = USBTV_FRAME_ID(chunk);
372 odd = USBTV_ODD(chunk);
373 chunk_no = USBTV_CHUNK_NO(chunk);
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300374 if (chunk_no >= usbtv->n_chunks)
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300375 return;
376
377 /* Beginning of a frame. */
Lubomir Rintel6ee0faa2013-07-02 07:56:39 -0300378 if (chunk_no == 0) {
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300379 usbtv->frame_id = frame_id;
Lubomir Rintel6ee0faa2013-07-02 07:56:39 -0300380 usbtv->chunks_done = 0;
381 }
382
383 if (usbtv->frame_id != frame_id)
384 return;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300385
386 spin_lock_irqsave(&usbtv->buflock, flags);
387 if (list_empty(&usbtv->bufs)) {
388 /* No free buffers. Userspace likely too slow. */
389 spin_unlock_irqrestore(&usbtv->buflock, flags);
390 return;
391 }
392
393 /* First available buffer. */
394 buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
Junghak Sung2d700712015-09-22 10:30:30 -0300395 frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300396
Lubomir Rintel30800722013-07-02 07:56:38 -0300397 /* Copy the chunk data. */
398 usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
Lubomir Rintel6ee0faa2013-07-02 07:56:39 -0300399 usbtv->chunks_done++;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300400
Nikola Forró80fa4f02015-12-20 09:57:17 -0200401 /* Last chunk in a field */
402 if (chunk_no == usbtv->n_chunks-1) {
403 /* Last chunk in a frame, signalling an end */
404 if (odd && !usbtv->last_odd) {
405 int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
406 enum vb2_buffer_state state = usbtv->chunks_done ==
407 usbtv->n_chunks ?
408 VB2_BUF_STATE_DONE :
409 VB2_BUF_STATE_ERROR;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300410
Nikola Forró80fa4f02015-12-20 09:57:17 -0200411 buf->vb.field = V4L2_FIELD_INTERLACED;
412 buf->vb.sequence = usbtv->sequence++;
413 buf->vb.vb2_buf.timestamp = ktime_get_ns();
414 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
415 vb2_buffer_done(&buf->vb.vb2_buf, state);
416 list_del(&buf->list);
417 }
418 usbtv->last_odd = odd;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300419 }
420
421 spin_unlock_irqrestore(&usbtv->buflock, flags);
422}
423
424/* Got image data. Each packet contains a number of 256-word chunks we
425 * compose the image from. */
426static void usbtv_iso_cb(struct urb *ip)
427{
428 int ret;
429 int i;
430 struct usbtv *usbtv = (struct usbtv *)ip->context;
431
432 switch (ip->status) {
433 /* All fine. */
434 case 0:
435 break;
436 /* Device disconnected or capture stopped? */
437 case -ENODEV:
438 case -ENOENT:
439 case -ECONNRESET:
440 case -ESHUTDOWN:
441 return;
442 /* Unknown error. Retry. */
443 default:
444 dev_warn(usbtv->dev, "Bad response for ISO request.\n");
445 goto resubmit;
446 }
447
448 for (i = 0; i < ip->number_of_packets; i++) {
449 int size = ip->iso_frame_desc[i].actual_length;
450 unsigned char *data = ip->transfer_buffer +
451 ip->iso_frame_desc[i].offset;
452 int offset;
453
454 for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
455 usbtv_image_chunk(usbtv,
Hans Verkuil6fbf4d02014-08-20 18:03:53 -0300456 (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300457 }
458
459resubmit:
460 ret = usb_submit_urb(ip, GFP_ATOMIC);
461 if (ret < 0)
462 dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
463}
464
465static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
466{
467 struct urb *ip;
468 int size = usbtv->iso_size;
469 int i;
470
471 ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
472 if (ip == NULL)
473 return NULL;
474
475 ip->dev = usbtv->udev;
476 ip->context = usbtv;
477 ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
478 ip->interval = 1;
479 ip->transfer_flags = URB_ISO_ASAP;
480 ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
481 GFP_KERNEL);
Insu Yune22a3b32015-12-29 19:48:29 -0200482 if (!ip->transfer_buffer) {
483 usb_free_urb(ip);
484 return NULL;
485 }
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300486 ip->complete = usbtv_iso_cb;
487 ip->number_of_packets = USBTV_ISOC_PACKETS;
488 ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
489 for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
490 ip->iso_frame_desc[i].offset = size * i;
491 ip->iso_frame_desc[i].length = size;
492 }
493
494 return ip;
495}
496
497static void usbtv_stop(struct usbtv *usbtv)
498{
499 int i;
500 unsigned long flags;
501
502 /* Cancel running transfers. */
503 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
504 struct urb *ip = usbtv->isoc_urbs[i];
Amber Thrall146af9c2014-09-20 01:03:15 -0300505
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300506 if (ip == NULL)
507 continue;
508 usb_kill_urb(ip);
509 kfree(ip->transfer_buffer);
510 usb_free_urb(ip);
511 usbtv->isoc_urbs[i] = NULL;
512 }
513
514 /* Return buffers to userspace. */
515 spin_lock_irqsave(&usbtv->buflock, flags);
516 while (!list_empty(&usbtv->bufs)) {
517 struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
518 struct usbtv_buf, list);
Junghak Sung2d700712015-09-22 10:30:30 -0300519 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300520 list_del(&buf->list);
521 }
522 spin_unlock_irqrestore(&usbtv->buflock, flags);
523}
524
525static int usbtv_start(struct usbtv *usbtv)
526{
527 int i;
528 int ret;
529
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300530 usbtv_audio_suspend(usbtv);
531
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300532 ret = usb_set_interface(usbtv->udev, 0, 0);
533 if (ret < 0)
534 return ret;
535
536 ret = usbtv_setup_capture(usbtv);
537 if (ret < 0)
538 return ret;
539
540 ret = usb_set_interface(usbtv->udev, 0, 1);
541 if (ret < 0)
542 return ret;
543
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300544 usbtv_audio_resume(usbtv);
545
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300546 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
547 struct urb *ip;
548
549 ip = usbtv_setup_iso_transfer(usbtv);
550 if (ip == NULL) {
551 ret = -ENOMEM;
552 goto start_fail;
553 }
554 usbtv->isoc_urbs[i] = ip;
555
556 ret = usb_submit_urb(ip, GFP_KERNEL);
557 if (ret < 0)
558 goto start_fail;
559 }
560
561 return 0;
562
563start_fail:
564 usbtv_stop(usbtv);
565 return ret;
566}
567
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300568static int usbtv_querycap(struct file *file, void *priv,
569 struct v4l2_capability *cap)
570{
571 struct usbtv *dev = video_drvdata(file);
572
573 strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
574 strlcpy(cap->card, "usbtv", sizeof(cap->card));
575 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
576 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
577 cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
578 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
579 return 0;
580}
581
582static int usbtv_enum_input(struct file *file, void *priv,
583 struct v4l2_input *i)
584{
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300585 struct usbtv *dev = video_drvdata(file);
586
Lubomir Rintel22061122013-07-12 05:03:03 -0300587 switch (i->index) {
588 case USBTV_COMPOSITE_INPUT:
589 strlcpy(i->name, "Composite", sizeof(i->name));
590 break;
591 case USBTV_SVIDEO_INPUT:
592 strlcpy(i->name, "S-Video", sizeof(i->name));
593 break;
594 default:
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300595 return -EINVAL;
Lubomir Rintel22061122013-07-12 05:03:03 -0300596 }
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300597
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300598 i->type = V4L2_INPUT_TYPE_CAMERA;
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300599 i->std = dev->vdev.tvnorms;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300600 return 0;
601}
602
603static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
604 struct v4l2_fmtdesc *f)
605{
606 if (f->index > 0)
607 return -EINVAL;
608
609 strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
610 sizeof(f->description));
611 f->pixelformat = V4L2_PIX_FMT_YUYV;
612 return 0;
613}
614
615static int usbtv_fmt_vid_cap(struct file *file, void *priv,
616 struct v4l2_format *f)
617{
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300618 struct usbtv *usbtv = video_drvdata(file);
619
620 f->fmt.pix.width = usbtv->width;
621 f->fmt.pix.height = usbtv->height;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300622 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
623 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300624 f->fmt.pix.bytesperline = usbtv->width * 2;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300625 f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
626 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300627
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300628 return 0;
629}
630
631static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
632{
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300633 struct usbtv *usbtv = video_drvdata(file);
634 *norm = usbtv->norm;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300635 return 0;
636}
637
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300638static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
639{
640 int ret = -EINVAL;
641 struct usbtv *usbtv = video_drvdata(file);
642
Hugo Grostabussiatc41e20d2018-04-08 17:11:57 -0400643 if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL) ||
644 (norm & V4L2_STD_SECAM))
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300645 ret = usbtv_select_norm(usbtv, norm);
646
647 return ret;
648}
649
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300650static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
651{
Lubomir Rintel22061122013-07-12 05:03:03 -0300652 struct usbtv *usbtv = video_drvdata(file);
653 *i = usbtv->input;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300654 return 0;
655}
656
657static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
658{
Lubomir Rintel22061122013-07-12 05:03:03 -0300659 struct usbtv *usbtv = video_drvdata(file);
Amber Thrall146af9c2014-09-20 01:03:15 -0300660
Lubomir Rintel22061122013-07-12 05:03:03 -0300661 return usbtv_select_input(usbtv, i);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300662}
663
Fengguang Wu9b058372014-02-04 06:02:02 -0300664static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300665 .vidioc_querycap = usbtv_querycap,
666 .vidioc_enum_input = usbtv_enum_input,
667 .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
668 .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
669 .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
670 .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
671 .vidioc_g_std = usbtv_g_std,
672 .vidioc_s_std = usbtv_s_std,
673 .vidioc_g_input = usbtv_g_input,
674 .vidioc_s_input = usbtv_s_input,
675
676 .vidioc_reqbufs = vb2_ioctl_reqbufs,
677 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
678 .vidioc_querybuf = vb2_ioctl_querybuf,
679 .vidioc_create_bufs = vb2_ioctl_create_bufs,
680 .vidioc_qbuf = vb2_ioctl_qbuf,
681 .vidioc_dqbuf = vb2_ioctl_dqbuf,
682 .vidioc_streamon = vb2_ioctl_streamon,
683 .vidioc_streamoff = vb2_ioctl_streamoff,
684};
685
Bhumika Goyalfe9619a2017-08-26 02:13:46 -0400686static const struct v4l2_file_operations usbtv_fops = {
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300687 .owner = THIS_MODULE,
688 .unlocked_ioctl = video_ioctl2,
689 .mmap = vb2_fop_mmap,
690 .open = v4l2_fh_open,
691 .release = vb2_fop_release,
692 .read = vb2_fop_read,
693 .poll = vb2_fop_poll,
694};
695
696static int usbtv_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200697 unsigned int *nbuffers,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300698 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300699{
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300700 struct usbtv *usbtv = vb2_get_drv_priv(vq);
Hans Verkuila9884102015-04-24 04:26:01 -0300701 unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300702
Hans Verkuila9884102015-04-24 04:26:01 -0300703 if (vq->num_buffers + *nbuffers < 2)
704 *nbuffers = 2 - vq->num_buffers;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200705 if (*nplanes)
706 return sizes[0] < size ? -EINVAL : 0;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300707 *nplanes = 1;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200708 sizes[0] = size;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300709
710 return 0;
711}
712
713static void usbtv_buf_queue(struct vb2_buffer *vb)
714{
Junghak Sung2d700712015-09-22 10:30:30 -0300715 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300716 struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300717 struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300718 unsigned long flags;
719
720 if (usbtv->udev == NULL) {
721 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
722 return;
723 }
724
725 spin_lock_irqsave(&usbtv->buflock, flags);
726 list_add_tail(&buf->list, &usbtv->bufs);
727 spin_unlock_irqrestore(&usbtv->buflock, flags);
728}
729
730static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
731{
732 struct usbtv *usbtv = vb2_get_drv_priv(vq);
733
734 if (usbtv->udev == NULL)
735 return -ENODEV;
736
Nikola Forró80fa4f02015-12-20 09:57:17 -0200737 usbtv->last_odd = 1;
Hans Verkuila9884102015-04-24 04:26:01 -0300738 usbtv->sequence = 0;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300739 return usbtv_start(usbtv);
740}
741
Hans Verkuile37559b2014-04-17 02:47:21 -0300742static void usbtv_stop_streaming(struct vb2_queue *vq)
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300743{
744 struct usbtv *usbtv = vb2_get_drv_priv(vq);
745
Hans Verkuile37559b2014-04-17 02:47:21 -0300746 if (usbtv->udev)
747 usbtv_stop(usbtv);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300748}
749
Julia Lawall1bc17712016-09-08 20:59:01 -0300750static const struct vb2_ops usbtv_vb2_ops = {
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300751 .queue_setup = usbtv_queue_setup,
752 .buf_queue = usbtv_buf_queue,
753 .start_streaming = usbtv_start_streaming,
754 .stop_streaming = usbtv_stop_streaming,
755};
756
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200757static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
758{
759 struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
760 ctrl);
Mauro Carvalho Chehab62de7d992016-11-16 13:13:02 -0200761 u8 *data;
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200762 u16 index, size;
763 int ret;
764
Mauro Carvalho Chehab62de7d992016-11-16 13:13:02 -0200765 data = kmalloc(3, GFP_KERNEL);
766 if (!data)
767 return -ENOMEM;
768
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200769 /*
770 * Read in the current brightness/contrast registers. We need them
771 * both, because the values are for some reason interleaved.
772 */
773 if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
774 ret = usb_control_msg(usbtv->udev,
Adam Sampsonb3168c82017-10-24 16:14:46 -0400775 usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
776 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200777 0, USBTV_BASE + 0x0244, (void *)data, 3, 0);
Mauro Carvalho Chehab62de7d992016-11-16 13:13:02 -0200778 if (ret < 0)
779 goto error;
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200780 }
781
782 switch (ctrl->id) {
783 case V4L2_CID_BRIGHTNESS:
784 index = USBTV_BASE + 0x0244;
785 size = 3;
786 data[0] &= 0xf0;
787 data[0] |= (ctrl->val >> 8) & 0xf;
788 data[2] = ctrl->val & 0xff;
789 break;
790 case V4L2_CID_CONTRAST:
791 index = USBTV_BASE + 0x0244;
792 size = 3;
793 data[0] &= 0x0f;
794 data[0] |= (ctrl->val >> 4) & 0xf0;
795 data[1] = ctrl->val & 0xff;
796 break;
797 case V4L2_CID_SATURATION:
798 index = USBTV_BASE + 0x0242;
799 data[0] = ctrl->val >> 8;
800 data[1] = ctrl->val & 0xff;
801 size = 2;
802 break;
803 case V4L2_CID_HUE:
804 index = USBTV_BASE + 0x0240;
805 size = 2;
806 if (ctrl->val > 0) {
807 data[0] = 0x92 + (ctrl->val >> 8);
808 data[1] = ctrl->val & 0xff;
809 } else {
810 data[0] = 0x82 + (-ctrl->val >> 8);
811 data[1] = -ctrl->val & 0xff;
812 }
813 break;
Lubomir Rintel9165ba12017-02-06 04:47:36 -0200814 case V4L2_CID_SHARPNESS:
815 index = USBTV_BASE + 0x0239;
816 data[0] = 0;
817 data[1] = ctrl->val;
818 size = 2;
819 break;
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200820 default:
Mauro Carvalho Chehab62de7d992016-11-16 13:13:02 -0200821 kfree(data);
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200822 return -EINVAL;
823 }
824
825 ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
826 USBTV_CONTROL_REG,
827 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
828 0, index, (void *)data, size, 0);
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200829
Mauro Carvalho Chehab62de7d992016-11-16 13:13:02 -0200830error:
831 if (ret < 0)
832 dev_warn(usbtv->dev, "Failed to submit a control request.\n");
833
834 kfree(data);
835 return ret;
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200836}
837
838static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
839 .s_ctrl = usbtv_s_ctrl,
840};
841
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300842static void usbtv_release(struct v4l2_device *v4l2_dev)
843{
844 struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
845
846 v4l2_device_unregister(&usbtv->v4l2_dev);
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200847 v4l2_ctrl_handler_free(&usbtv->ctrl);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300848 vb2_queue_release(&usbtv->vb2q);
849 kfree(usbtv);
850}
851
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300852int usbtv_video_init(struct usbtv *usbtv)
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300853{
854 int ret;
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300855
856 (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
857
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300858 spin_lock_init(&usbtv->buflock);
859 mutex_init(&usbtv->v4l2_lock);
860 mutex_init(&usbtv->vb2q_lock);
861 INIT_LIST_HEAD(&usbtv->bufs);
862
863 /* videobuf2 structure */
864 usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
865 usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
866 usbtv->vb2q.drv_priv = usbtv;
867 usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
868 usbtv->vb2q.ops = &usbtv_vb2_ops;
869 usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300870 usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300871 usbtv->vb2q.lock = &usbtv->vb2q_lock;
872 ret = vb2_queue_init(&usbtv->vb2q);
873 if (ret < 0) {
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300874 dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
875 return ret;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300876 }
877
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200878 /* controls */
879 v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
880 v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
881 V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
882 v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
883 V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
884 v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
885 V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
886 v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
887 V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
Lubomir Rintel9165ba12017-02-06 04:47:36 -0200888 v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
889 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200890 ret = usbtv->ctrl.error;
891 if (ret < 0) {
892 dev_warn(usbtv->dev, "Could not initialize controls\n");
893 goto ctrl_fail;
894 }
895
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300896 /* v4l2 structure */
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200897 usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300898 usbtv->v4l2_dev.release = usbtv_release;
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300899 ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300900 if (ret < 0) {
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300901 dev_warn(usbtv->dev, "Could not register v4l2 device\n");
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300902 goto v4l2_fail;
903 }
904
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300905 /* Video structure */
906 strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
907 usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
908 usbtv->vdev.release = video_device_release_empty;
909 usbtv->vdev.fops = &usbtv_fops;
910 usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
Georg Kaindl0e0fe392013-10-21 12:01:36 -0300911 usbtv->vdev.tvnorms = USBTV_TV_STD;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300912 usbtv->vdev.queue = &usbtv->vb2q;
913 usbtv->vdev.lock = &usbtv->v4l2_lock;
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300914 video_set_drvdata(&usbtv->vdev, usbtv);
915 ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
916 if (ret < 0) {
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300917 dev_warn(usbtv->dev, "Could not register video device\n");
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300918 goto vdev_fail;
919 }
920
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300921 return 0;
922
923vdev_fail:
924 v4l2_device_unregister(&usbtv->v4l2_dev);
925v4l2_fail:
Lubomir Rintelc53a8462016-10-16 07:38:22 -0200926ctrl_fail:
927 v4l2_ctrl_handler_free(&usbtv->ctrl);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300928 vb2_queue_release(&usbtv->vb2q);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300929
930 return ret;
931}
932
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300933void usbtv_video_free(struct usbtv *usbtv)
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300934{
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300935 mutex_lock(&usbtv->vb2q_lock);
936 mutex_lock(&usbtv->v4l2_lock);
937
938 usbtv_stop(usbtv);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300939 video_unregister_device(&usbtv->vdev);
940 v4l2_device_disconnect(&usbtv->v4l2_dev);
Lubomir Rintelf3d27f32013-06-17 10:29:06 -0300941
942 mutex_unlock(&usbtv->v4l2_lock);
943 mutex_unlock(&usbtv->vb2q_lock);
944
945 v4l2_device_put(&usbtv->v4l2_dev);
946}