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