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