2 * Fushicai USBTV007 Video Grabber Driver
5 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
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.
13 * Copyright (c) 2013 Lubomir Rintel
14 * All rights reserved.
15 * No physical hardware was harmed running Windows during the
16 * reverse-engineering activity
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL").
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/usb.h>
36 #include <linux/videodev2.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/videobuf2-core.h>
41 #include <media/videobuf2-vmalloc.h>
44 #define USBTV_VIDEO_ENDP 0x81
45 #define USBTV_BASE 0xc000
46 #define USBTV_REQUEST_REG 12
48 /* Number of concurrent isochronous urbs submitted.
49 * Higher numbers was seen to overly saturate the USB bus. */
50 #define USBTV_ISOC_TRANSFERS 16
51 #define USBTV_ISOC_PACKETS 8
53 #define USBTV_CHUNK_SIZE 256
54 #define USBTV_CHUNK 240
57 #define USBTV_MAGIC_OK(chunk) ((be32_to_cpu(chunk[0]) & 0xff000000) \
59 #define USBTV_FRAME_ID(chunk) ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16)
60 #define USBTV_ODD(chunk) ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
61 #define USBTV_CHUNK_NO(chunk) (be32_to_cpu(chunk[0]) & 0x00000fff)
63 #define USBTV_TV_STD (V4L2_STD_525_60 | V4L2_STD_PAL)
65 /* parameters for supported TV norms */
66 struct usbtv_norm_params
{
68 int cap_width
, cap_height
;
71 static struct usbtv_norm_params norm_params
[] = {
73 .norm
= V4L2_STD_525_60
,
84 /* A single videobuf2 frame buffer. */
87 struct list_head list
;
90 /* Per-device structure. */
93 struct usb_device
*udev
;
94 struct v4l2_device v4l2_dev
;
95 struct video_device vdev
;
96 struct vb2_queue vb2q
;
97 struct mutex v4l2_lock
;
98 struct mutex vb2q_lock
;
100 /* List of videobuf2 buffers protected by a lock. */
102 struct list_head bufs
;
104 /* Number of currently processed frame, useful find
105 * out when a new one begins. */
110 USBTV_COMPOSITE_INPUT
,
117 unsigned int sequence
;
118 struct urb
*isoc_urbs
[USBTV_ISOC_TRANSFERS
];
121 static int usbtv_configure_for_norm(struct usbtv
*usbtv
, v4l2_std_id norm
)
124 struct usbtv_norm_params
*params
= NULL
;
126 for (i
= 0; i
< ARRAY_SIZE(norm_params
); i
++) {
127 if (norm_params
[i
].norm
& norm
) {
128 params
= &norm_params
[i
];
134 usbtv
->width
= params
->cap_width
;
135 usbtv
->height
= params
->cap_height
;
136 usbtv
->n_chunks
= usbtv
->width
* usbtv
->height
138 usbtv
->norm
= params
->norm
;
145 static int usbtv_set_regs(struct usbtv
*usbtv
, const u16 regs
[][2], int size
)
148 int pipe
= usb_rcvctrlpipe(usbtv
->udev
, 0);
151 for (i
= 0; i
< size
; i
++) {
152 u16 index
= regs
[i
][0];
153 u16 value
= regs
[i
][1];
155 ret
= usb_control_msg(usbtv
->udev
, pipe
, USBTV_REQUEST_REG
,
156 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
157 value
, index
, NULL
, 0, 0);
165 static int usbtv_select_input(struct usbtv
*usbtv
, int input
)
169 static const u16 composite
[][2] = {
170 { USBTV_BASE
+ 0x0105, 0x0060 },
171 { USBTV_BASE
+ 0x011f, 0x00f2 },
172 { USBTV_BASE
+ 0x0127, 0x0060 },
173 { USBTV_BASE
+ 0x00ae, 0x0010 },
174 { USBTV_BASE
+ 0x0284, 0x00aa },
175 { USBTV_BASE
+ 0x0239, 0x0060 },
178 static const u16 svideo
[][2] = {
179 { USBTV_BASE
+ 0x0105, 0x0010 },
180 { USBTV_BASE
+ 0x011f, 0x00ff },
181 { USBTV_BASE
+ 0x0127, 0x0060 },
182 { USBTV_BASE
+ 0x00ae, 0x0030 },
183 { USBTV_BASE
+ 0x0284, 0x0088 },
184 { USBTV_BASE
+ 0x0239, 0x0060 },
188 case USBTV_COMPOSITE_INPUT
:
189 ret
= usbtv_set_regs(usbtv
, composite
, ARRAY_SIZE(composite
));
191 case USBTV_SVIDEO_INPUT
:
192 ret
= usbtv_set_regs(usbtv
, svideo
, ARRAY_SIZE(svideo
));
199 usbtv
->input
= input
;
204 static int usbtv_select_norm(struct usbtv
*usbtv
, v4l2_std_id norm
)
207 static const u16 pal
[][2] = {
208 { USBTV_BASE
+ 0x001a, 0x0068 },
209 { USBTV_BASE
+ 0x010e, 0x0072 },
210 { USBTV_BASE
+ 0x010f, 0x00a2 },
211 { USBTV_BASE
+ 0x0112, 0x00b0 },
212 { USBTV_BASE
+ 0x0117, 0x0001 },
213 { USBTV_BASE
+ 0x0118, 0x002c },
214 { USBTV_BASE
+ 0x012d, 0x0010 },
215 { USBTV_BASE
+ 0x012f, 0x0020 },
216 { USBTV_BASE
+ 0x024f, 0x0002 },
217 { USBTV_BASE
+ 0x0254, 0x0059 },
218 { USBTV_BASE
+ 0x025a, 0x0016 },
219 { USBTV_BASE
+ 0x025b, 0x0035 },
220 { USBTV_BASE
+ 0x0263, 0x0017 },
221 { USBTV_BASE
+ 0x0266, 0x0016 },
222 { USBTV_BASE
+ 0x0267, 0x0036 }
225 static const u16 ntsc
[][2] = {
226 { USBTV_BASE
+ 0x001a, 0x0079 },
227 { USBTV_BASE
+ 0x010e, 0x0068 },
228 { USBTV_BASE
+ 0x010f, 0x009c },
229 { USBTV_BASE
+ 0x0112, 0x00f0 },
230 { USBTV_BASE
+ 0x0117, 0x0000 },
231 { USBTV_BASE
+ 0x0118, 0x00fc },
232 { USBTV_BASE
+ 0x012d, 0x0004 },
233 { USBTV_BASE
+ 0x012f, 0x0008 },
234 { USBTV_BASE
+ 0x024f, 0x0001 },
235 { USBTV_BASE
+ 0x0254, 0x005f },
236 { USBTV_BASE
+ 0x025a, 0x0012 },
237 { USBTV_BASE
+ 0x025b, 0x0001 },
238 { USBTV_BASE
+ 0x0263, 0x001c },
239 { USBTV_BASE
+ 0x0266, 0x0011 },
240 { USBTV_BASE
+ 0x0267, 0x0005 }
243 ret
= usbtv_configure_for_norm(usbtv
, norm
);
246 if (norm
& V4L2_STD_525_60
)
247 ret
= usbtv_set_regs(usbtv
, ntsc
, ARRAY_SIZE(ntsc
));
248 else if (norm
& V4L2_STD_PAL
)
249 ret
= usbtv_set_regs(usbtv
, pal
, ARRAY_SIZE(pal
));
255 static int usbtv_setup_capture(struct usbtv
*usbtv
)
258 static const u16 setup
[][2] = {
259 /* These seem to enable the device. */
260 { USBTV_BASE
+ 0x0008, 0x0001 },
261 { USBTV_BASE
+ 0x01d0, 0x00ff },
262 { USBTV_BASE
+ 0x01d9, 0x0002 },
264 /* These seem to influence color parameters, such as
265 * brightness, etc. */
266 { USBTV_BASE
+ 0x0239, 0x0040 },
267 { USBTV_BASE
+ 0x0240, 0x0000 },
268 { USBTV_BASE
+ 0x0241, 0x0000 },
269 { USBTV_BASE
+ 0x0242, 0x0002 },
270 { USBTV_BASE
+ 0x0243, 0x0080 },
271 { USBTV_BASE
+ 0x0244, 0x0012 },
272 { USBTV_BASE
+ 0x0245, 0x0090 },
273 { USBTV_BASE
+ 0x0246, 0x0000 },
275 { USBTV_BASE
+ 0x0278, 0x002d },
276 { USBTV_BASE
+ 0x0279, 0x000a },
277 { USBTV_BASE
+ 0x027a, 0x0032 },
281 { USBTV_BASE
+ 0x00ac, 0x00c0 },
282 { USBTV_BASE
+ 0x00ad, 0x0000 },
283 { USBTV_BASE
+ 0x00a2, 0x0012 },
284 { USBTV_BASE
+ 0x00a3, 0x00e0 },
285 { USBTV_BASE
+ 0x00a4, 0x0028 },
286 { USBTV_BASE
+ 0x00a5, 0x0082 },
287 { USBTV_BASE
+ 0x00a7, 0x0080 },
288 { USBTV_BASE
+ 0x0000, 0x0014 },
289 { USBTV_BASE
+ 0x0006, 0x0003 },
290 { USBTV_BASE
+ 0x0090, 0x0099 },
291 { USBTV_BASE
+ 0x0091, 0x0090 },
292 { USBTV_BASE
+ 0x0094, 0x0068 },
293 { USBTV_BASE
+ 0x0095, 0x0070 },
294 { USBTV_BASE
+ 0x009c, 0x0030 },
295 { USBTV_BASE
+ 0x009d, 0x00c0 },
296 { USBTV_BASE
+ 0x009e, 0x00e0 },
297 { USBTV_BASE
+ 0x0019, 0x0006 },
298 { USBTV_BASE
+ 0x008c, 0x00ba },
299 { USBTV_BASE
+ 0x0101, 0x00ff },
300 { USBTV_BASE
+ 0x010c, 0x00b3 },
301 { USBTV_BASE
+ 0x01b2, 0x0080 },
302 { USBTV_BASE
+ 0x01b4, 0x00a0 },
303 { USBTV_BASE
+ 0x014c, 0x00ff },
304 { USBTV_BASE
+ 0x014d, 0x00ca },
305 { USBTV_BASE
+ 0x0113, 0x0053 },
306 { USBTV_BASE
+ 0x0119, 0x008a },
307 { USBTV_BASE
+ 0x013c, 0x0003 },
308 { USBTV_BASE
+ 0x0150, 0x009c },
309 { USBTV_BASE
+ 0x0151, 0x0071 },
310 { USBTV_BASE
+ 0x0152, 0x00c6 },
311 { USBTV_BASE
+ 0x0153, 0x0084 },
312 { USBTV_BASE
+ 0x0154, 0x00bc },
313 { USBTV_BASE
+ 0x0155, 0x00a0 },
314 { USBTV_BASE
+ 0x0156, 0x00a0 },
315 { USBTV_BASE
+ 0x0157, 0x009c },
316 { USBTV_BASE
+ 0x0158, 0x001f },
317 { USBTV_BASE
+ 0x0159, 0x0006 },
318 { USBTV_BASE
+ 0x015d, 0x0000 },
320 { USBTV_BASE
+ 0x0284, 0x0088 },
321 { USBTV_BASE
+ 0x0003, 0x0004 },
322 { USBTV_BASE
+ 0x0100, 0x00d3 },
323 { USBTV_BASE
+ 0x0115, 0x0015 },
324 { USBTV_BASE
+ 0x0220, 0x002e },
325 { USBTV_BASE
+ 0x0225, 0x0008 },
326 { USBTV_BASE
+ 0x024e, 0x0002 },
327 { USBTV_BASE
+ 0x024e, 0x0002 },
328 { USBTV_BASE
+ 0x024f, 0x0002 },
331 ret
= usbtv_set_regs(usbtv
, setup
, ARRAY_SIZE(setup
));
335 ret
= usbtv_select_norm(usbtv
, usbtv
->norm
);
339 ret
= usbtv_select_input(usbtv
, usbtv
->input
);
346 /* Copy data from chunk into a frame buffer, deinterlacing the data
347 * into every second line. Unfortunately, they don't align nicely into
348 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
349 * Therefore, we break down the chunk into two halves before copyting,
350 * so that we can interleave a line if needed. */
351 static void usbtv_chunk_to_vbuf(u32
*frame
, u32
*src
, int chunk_no
, int odd
)
355 for (half
= 0; half
< 2; half
++) {
356 int part_no
= chunk_no
* 2 + half
;
357 int line
= part_no
/ 3;
358 int part_index
= (line
* 2 + !odd
) * 3 + (part_no
% 3);
360 u32
*dst
= &frame
[part_index
* USBTV_CHUNK
/2];
361 memcpy(dst
, src
, USBTV_CHUNK
/2 * sizeof(*src
));
362 src
+= USBTV_CHUNK
/2;
366 /* Called for each 256-byte image chunk.
367 * First word identifies the chunk, followed by 240 words of image
368 * data and padding. */
369 static void usbtv_image_chunk(struct usbtv
*usbtv
, u32
*chunk
)
371 int frame_id
, odd
, chunk_no
;
373 struct usbtv_buf
*buf
;
376 /* Ignore corrupted lines. */
377 if (!USBTV_MAGIC_OK(chunk
))
379 frame_id
= USBTV_FRAME_ID(chunk
);
380 odd
= USBTV_ODD(chunk
);
381 chunk_no
= USBTV_CHUNK_NO(chunk
);
382 if (chunk_no
>= usbtv
->n_chunks
)
385 /* Beginning of a frame. */
387 usbtv
->frame_id
= frame_id
;
388 usbtv
->chunks_done
= 0;
391 if (usbtv
->frame_id
!= frame_id
)
394 spin_lock_irqsave(&usbtv
->buflock
, flags
);
395 if (list_empty(&usbtv
->bufs
)) {
396 /* No free buffers. Userspace likely too slow. */
397 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
401 /* First available buffer. */
402 buf
= list_first_entry(&usbtv
->bufs
, struct usbtv_buf
, list
);
403 frame
= vb2_plane_vaddr(&buf
->vb
, 0);
405 /* Copy the chunk data. */
406 usbtv_chunk_to_vbuf(frame
, &chunk
[1], chunk_no
, odd
);
407 usbtv
->chunks_done
++;
409 /* Last chunk in a frame, signalling an end */
410 if (odd
&& chunk_no
== usbtv
->n_chunks
-1) {
411 int size
= vb2_plane_size(&buf
->vb
, 0);
412 enum vb2_buffer_state state
= usbtv
->chunks_done
==
417 buf
->vb
.v4l2_buf
.field
= V4L2_FIELD_INTERLACED
;
418 buf
->vb
.v4l2_buf
.sequence
= usbtv
->sequence
++;
419 v4l2_get_timestamp(&buf
->vb
.v4l2_buf
.timestamp
);
420 vb2_set_plane_payload(&buf
->vb
, 0, size
);
421 vb2_buffer_done(&buf
->vb
, state
);
422 list_del(&buf
->list
);
425 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
428 /* Got image data. Each packet contains a number of 256-word chunks we
429 * compose the image from. */
430 static void usbtv_iso_cb(struct urb
*ip
)
434 struct usbtv
*usbtv
= (struct usbtv
*)ip
->context
;
436 switch (ip
->status
) {
440 /* Device disconnected or capture stopped? */
446 /* Unknown error. Retry. */
448 dev_warn(usbtv
->dev
, "Bad response for ISO request.\n");
452 for (i
= 0; i
< ip
->number_of_packets
; i
++) {
453 int size
= ip
->iso_frame_desc
[i
].actual_length
;
454 unsigned char *data
= ip
->transfer_buffer
+
455 ip
->iso_frame_desc
[i
].offset
;
458 for (offset
= 0; USBTV_CHUNK_SIZE
* offset
< size
; offset
++)
459 usbtv_image_chunk(usbtv
,
460 (u32
*)&data
[USBTV_CHUNK_SIZE
* offset
]);
464 ret
= usb_submit_urb(ip
, GFP_ATOMIC
);
466 dev_warn(usbtv
->dev
, "Could not resubmit ISO URB\n");
469 static struct urb
*usbtv_setup_iso_transfer(struct usbtv
*usbtv
)
472 int size
= usbtv
->iso_size
;
475 ip
= usb_alloc_urb(USBTV_ISOC_PACKETS
, GFP_KERNEL
);
479 ip
->dev
= usbtv
->udev
;
481 ip
->pipe
= usb_rcvisocpipe(usbtv
->udev
, USBTV_VIDEO_ENDP
);
483 ip
->transfer_flags
= URB_ISO_ASAP
;
484 ip
->transfer_buffer
= kzalloc(size
* USBTV_ISOC_PACKETS
,
486 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
;
497 static void usbtv_stop(struct usbtv
*usbtv
)
502 /* Cancel running transfers. */
503 for (i
= 0; i
< USBTV_ISOC_TRANSFERS
; i
++) {
504 struct urb
*ip
= usbtv
->isoc_urbs
[i
];
508 kfree(ip
->transfer_buffer
);
510 usbtv
->isoc_urbs
[i
] = NULL
;
513 /* Return buffers to userspace. */
514 spin_lock_irqsave(&usbtv
->buflock
, flags
);
515 while (!list_empty(&usbtv
->bufs
)) {
516 struct usbtv_buf
*buf
= list_first_entry(&usbtv
->bufs
,
517 struct usbtv_buf
, list
);
518 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
519 list_del(&buf
->list
);
521 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
524 static int usbtv_start(struct usbtv
*usbtv
)
529 ret
= usb_set_interface(usbtv
->udev
, 0, 0);
533 ret
= usbtv_setup_capture(usbtv
);
537 ret
= usb_set_interface(usbtv
->udev
, 0, 1);
541 for (i
= 0; i
< USBTV_ISOC_TRANSFERS
; i
++) {
544 ip
= usbtv_setup_iso_transfer(usbtv
);
549 usbtv
->isoc_urbs
[i
] = ip
;
551 ret
= usb_submit_urb(ip
, GFP_KERNEL
);
563 struct usb_device_id usbtv_id_table
[] = {
564 { USB_DEVICE(0x1b71, 0x3002) },
567 MODULE_DEVICE_TABLE(usb
, usbtv_id_table
);
569 static int usbtv_querycap(struct file
*file
, void *priv
,
570 struct v4l2_capability
*cap
)
572 struct usbtv
*dev
= video_drvdata(file
);
574 strlcpy(cap
->driver
, "usbtv", sizeof(cap
->driver
));
575 strlcpy(cap
->card
, "usbtv", sizeof(cap
->card
));
576 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
577 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
;
578 cap
->device_caps
|= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
579 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
583 static int usbtv_enum_input(struct file
*file
, void *priv
,
584 struct v4l2_input
*i
)
586 struct usbtv
*dev
= video_drvdata(file
);
589 case USBTV_COMPOSITE_INPUT
:
590 strlcpy(i
->name
, "Composite", sizeof(i
->name
));
592 case USBTV_SVIDEO_INPUT
:
593 strlcpy(i
->name
, "S-Video", sizeof(i
->name
));
599 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
600 i
->std
= dev
->vdev
.tvnorms
;
604 static int usbtv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
605 struct v4l2_fmtdesc
*f
)
610 strlcpy(f
->description
, "16 bpp YUY2, 4:2:2, packed",
611 sizeof(f
->description
));
612 f
->pixelformat
= V4L2_PIX_FMT_YUYV
;
616 static int usbtv_fmt_vid_cap(struct file
*file
, void *priv
,
617 struct v4l2_format
*f
)
619 struct usbtv
*usbtv
= video_drvdata(file
);
621 f
->fmt
.pix
.width
= usbtv
->width
;
622 f
->fmt
.pix
.height
= usbtv
->height
;
623 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
624 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
625 f
->fmt
.pix
.bytesperline
= usbtv
->width
* 2;
626 f
->fmt
.pix
.sizeimage
= (f
->fmt
.pix
.bytesperline
* f
->fmt
.pix
.height
);
627 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
632 static int usbtv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
634 struct usbtv
*usbtv
= video_drvdata(file
);
639 static int usbtv_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
642 struct usbtv
*usbtv
= video_drvdata(file
);
644 if ((norm
& V4L2_STD_525_60
) || (norm
& V4L2_STD_PAL
))
645 ret
= usbtv_select_norm(usbtv
, norm
);
650 static int usbtv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
652 struct usbtv
*usbtv
= video_drvdata(file
);
657 static int usbtv_s_input(struct file
*file
, void *priv
, unsigned int i
)
659 struct usbtv
*usbtv
= video_drvdata(file
);
660 return usbtv_select_input(usbtv
, i
);
663 struct v4l2_ioctl_ops usbtv_ioctl_ops
= {
664 .vidioc_querycap
= usbtv_querycap
,
665 .vidioc_enum_input
= usbtv_enum_input
,
666 .vidioc_enum_fmt_vid_cap
= usbtv_enum_fmt_vid_cap
,
667 .vidioc_g_fmt_vid_cap
= usbtv_fmt_vid_cap
,
668 .vidioc_try_fmt_vid_cap
= usbtv_fmt_vid_cap
,
669 .vidioc_s_fmt_vid_cap
= usbtv_fmt_vid_cap
,
670 .vidioc_g_std
= usbtv_g_std
,
671 .vidioc_s_std
= usbtv_s_std
,
672 .vidioc_g_input
= usbtv_g_input
,
673 .vidioc_s_input
= usbtv_s_input
,
675 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
676 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
677 .vidioc_querybuf
= vb2_ioctl_querybuf
,
678 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
679 .vidioc_qbuf
= vb2_ioctl_qbuf
,
680 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
681 .vidioc_streamon
= vb2_ioctl_streamon
,
682 .vidioc_streamoff
= vb2_ioctl_streamoff
,
685 struct v4l2_file_operations usbtv_fops
= {
686 .owner
= THIS_MODULE
,
687 .unlocked_ioctl
= video_ioctl2
,
688 .mmap
= vb2_fop_mmap
,
689 .open
= v4l2_fh_open
,
690 .release
= vb2_fop_release
,
691 .read
= vb2_fop_read
,
692 .poll
= vb2_fop_poll
,
695 static int usbtv_queue_setup(struct vb2_queue
*vq
,
696 const struct v4l2_format
*v4l_fmt
, unsigned int *nbuffers
,
697 unsigned int *nplanes
, unsigned int sizes
[], void *alloc_ctxs
[])
699 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
704 sizes
[0] = USBTV_CHUNK
* usbtv
->n_chunks
* 2 * sizeof(u32
);
709 static void usbtv_buf_queue(struct vb2_buffer
*vb
)
711 struct usbtv
*usbtv
= vb2_get_drv_priv(vb
->vb2_queue
);
712 struct usbtv_buf
*buf
= container_of(vb
, struct usbtv_buf
, vb
);
715 if (usbtv
->udev
== NULL
) {
716 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
720 spin_lock_irqsave(&usbtv
->buflock
, flags
);
721 list_add_tail(&buf
->list
, &usbtv
->bufs
);
722 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
725 static int usbtv_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
727 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
729 if (usbtv
->udev
== NULL
)
732 return usbtv_start(usbtv
);
735 static int usbtv_stop_streaming(struct vb2_queue
*vq
)
737 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
739 if (usbtv
->udev
== NULL
)
746 struct vb2_ops usbtv_vb2_ops
= {
747 .queue_setup
= usbtv_queue_setup
,
748 .buf_queue
= usbtv_buf_queue
,
749 .start_streaming
= usbtv_start_streaming
,
750 .stop_streaming
= usbtv_stop_streaming
,
753 static void usbtv_release(struct v4l2_device
*v4l2_dev
)
755 struct usbtv
*usbtv
= container_of(v4l2_dev
, struct usbtv
, v4l2_dev
);
757 v4l2_device_unregister(&usbtv
->v4l2_dev
);
758 vb2_queue_release(&usbtv
->vb2q
);
762 static int usbtv_probe(struct usb_interface
*intf
,
763 const struct usb_device_id
*id
)
767 struct device
*dev
= &intf
->dev
;
770 /* Checks that the device is what we think it is. */
771 if (intf
->num_altsetting
!= 2)
773 if (intf
->altsetting
[1].desc
.bNumEndpoints
!= 4)
776 /* Packet size is split into 11 bits of base size and count of
777 * extra multiplies of it.*/
778 size
= usb_endpoint_maxp(&intf
->altsetting
[1].endpoint
[0].desc
);
779 size
= (size
& 0x07ff) * (((size
& 0x1800) >> 11) + 1);
781 /* Device structure */
782 usbtv
= kzalloc(sizeof(struct usbtv
), GFP_KERNEL
);
786 usbtv
->udev
= usb_get_dev(interface_to_usbdev(intf
));
788 usbtv
->iso_size
= size
;
790 (void)usbtv_configure_for_norm(usbtv
, V4L2_STD_525_60
);
792 spin_lock_init(&usbtv
->buflock
);
793 mutex_init(&usbtv
->v4l2_lock
);
794 mutex_init(&usbtv
->vb2q_lock
);
795 INIT_LIST_HEAD(&usbtv
->bufs
);
797 /* videobuf2 structure */
798 usbtv
->vb2q
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
799 usbtv
->vb2q
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
800 usbtv
->vb2q
.drv_priv
= usbtv
;
801 usbtv
->vb2q
.buf_struct_size
= sizeof(struct usbtv_buf
);
802 usbtv
->vb2q
.ops
= &usbtv_vb2_ops
;
803 usbtv
->vb2q
.mem_ops
= &vb2_vmalloc_memops
;
804 usbtv
->vb2q
.timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
805 usbtv
->vb2q
.lock
= &usbtv
->vb2q_lock
;
806 ret
= vb2_queue_init(&usbtv
->vb2q
);
808 dev_warn(dev
, "Could not initialize videobuf2 queue\n");
813 usbtv
->v4l2_dev
.release
= usbtv_release
;
814 ret
= v4l2_device_register(dev
, &usbtv
->v4l2_dev
);
816 dev_warn(dev
, "Could not register v4l2 device\n");
820 usb_set_intfdata(intf
, usbtv
);
822 /* Video structure */
823 strlcpy(usbtv
->vdev
.name
, "usbtv", sizeof(usbtv
->vdev
.name
));
824 usbtv
->vdev
.v4l2_dev
= &usbtv
->v4l2_dev
;
825 usbtv
->vdev
.release
= video_device_release_empty
;
826 usbtv
->vdev
.fops
= &usbtv_fops
;
827 usbtv
->vdev
.ioctl_ops
= &usbtv_ioctl_ops
;
828 usbtv
->vdev
.tvnorms
= USBTV_TV_STD
;
829 usbtv
->vdev
.queue
= &usbtv
->vb2q
;
830 usbtv
->vdev
.lock
= &usbtv
->v4l2_lock
;
831 set_bit(V4L2_FL_USE_FH_PRIO
, &usbtv
->vdev
.flags
);
832 video_set_drvdata(&usbtv
->vdev
, usbtv
);
833 ret
= video_register_device(&usbtv
->vdev
, VFL_TYPE_GRABBER
, -1);
835 dev_warn(dev
, "Could not register video device\n");
839 dev_info(dev
, "Fushicai USBTV007 Video Grabber\n");
843 v4l2_device_unregister(&usbtv
->v4l2_dev
);
845 vb2_queue_release(&usbtv
->vb2q
);
852 static void usbtv_disconnect(struct usb_interface
*intf
)
854 struct usbtv
*usbtv
= usb_get_intfdata(intf
);
856 mutex_lock(&usbtv
->vb2q_lock
);
857 mutex_lock(&usbtv
->v4l2_lock
);
860 usb_set_intfdata(intf
, NULL
);
861 video_unregister_device(&usbtv
->vdev
);
862 v4l2_device_disconnect(&usbtv
->v4l2_dev
);
863 usb_put_dev(usbtv
->udev
);
866 mutex_unlock(&usbtv
->v4l2_lock
);
867 mutex_unlock(&usbtv
->vb2q_lock
);
869 v4l2_device_put(&usbtv
->v4l2_dev
);
872 MODULE_AUTHOR("Lubomir Rintel");
873 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver");
874 MODULE_LICENSE("Dual BSD/GPL");
876 struct usb_driver usbtv_usb_driver
= {
878 .id_table
= usbtv_id_table
,
879 .probe
= usbtv_probe
,
880 .disconnect
= usbtv_disconnect
,
883 module_usb_driver(usbtv_usb_driver
);