2 * Copyright (c) 2013,2016 Lubomir Rintel
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL").
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.
30 * Fushicai USBTV007 Audio-Video Grabber Driver
33 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
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.
41 * No physical hardware was harmed running Windows during the
42 * reverse-engineering activity
45 #include <media/v4l2-ioctl.h>
46 #include <media/videobuf2-v4l2.h>
50 static struct usbtv_norm_params norm_params
[] = {
52 .norm
= V4L2_STD_525_60
,
63 static int usbtv_configure_for_norm(struct usbtv
*usbtv
, v4l2_std_id norm
)
66 struct usbtv_norm_params
*params
= NULL
;
68 for (i
= 0; i
< ARRAY_SIZE(norm_params
); i
++) {
69 if (norm_params
[i
].norm
& norm
) {
70 params
= &norm_params
[i
];
76 usbtv
->width
= params
->cap_width
;
77 usbtv
->height
= params
->cap_height
;
78 usbtv
->n_chunks
= usbtv
->width
* usbtv
->height
80 usbtv
->norm
= params
->norm
;
87 static int usbtv_select_input(struct usbtv
*usbtv
, int input
)
91 static const u16 composite
[][2] = {
92 { USBTV_BASE
+ 0x0105, 0x0060 },
93 { USBTV_BASE
+ 0x011f, 0x00f2 },
94 { USBTV_BASE
+ 0x0127, 0x0060 },
95 { USBTV_BASE
+ 0x00ae, 0x0010 },
96 { USBTV_BASE
+ 0x0239, 0x0060 },
99 static const u16 svideo
[][2] = {
100 { USBTV_BASE
+ 0x0105, 0x0010 },
101 { USBTV_BASE
+ 0x011f, 0x00ff },
102 { USBTV_BASE
+ 0x0127, 0x0060 },
103 { USBTV_BASE
+ 0x00ae, 0x0030 },
104 { USBTV_BASE
+ 0x0239, 0x0060 },
108 case USBTV_COMPOSITE_INPUT
:
109 ret
= usbtv_set_regs(usbtv
, composite
, ARRAY_SIZE(composite
));
111 case USBTV_SVIDEO_INPUT
:
112 ret
= usbtv_set_regs(usbtv
, svideo
, ARRAY_SIZE(svideo
));
119 usbtv
->input
= input
;
124 static int usbtv_select_norm(struct usbtv
*usbtv
, v4l2_std_id norm
)
127 static const u16 pal
[][2] = {
128 { USBTV_BASE
+ 0x001a, 0x0068 },
129 { USBTV_BASE
+ 0x010e, 0x0072 },
130 { USBTV_BASE
+ 0x010f, 0x00a2 },
131 { USBTV_BASE
+ 0x0112, 0x00b0 },
132 { USBTV_BASE
+ 0x0117, 0x0001 },
133 { USBTV_BASE
+ 0x0118, 0x002c },
134 { USBTV_BASE
+ 0x012d, 0x0010 },
135 { USBTV_BASE
+ 0x012f, 0x0020 },
136 { USBTV_BASE
+ 0x024f, 0x0002 },
137 { USBTV_BASE
+ 0x0254, 0x0059 },
138 { USBTV_BASE
+ 0x025a, 0x0016 },
139 { USBTV_BASE
+ 0x025b, 0x0035 },
140 { USBTV_BASE
+ 0x0263, 0x0017 },
141 { USBTV_BASE
+ 0x0266, 0x0016 },
142 { USBTV_BASE
+ 0x0267, 0x0036 }
145 static const u16 ntsc
[][2] = {
146 { USBTV_BASE
+ 0x001a, 0x0079 },
147 { USBTV_BASE
+ 0x010e, 0x0068 },
148 { USBTV_BASE
+ 0x010f, 0x009c },
149 { USBTV_BASE
+ 0x0112, 0x00f0 },
150 { USBTV_BASE
+ 0x0117, 0x0000 },
151 { USBTV_BASE
+ 0x0118, 0x00fc },
152 { USBTV_BASE
+ 0x012d, 0x0004 },
153 { USBTV_BASE
+ 0x012f, 0x0008 },
154 { USBTV_BASE
+ 0x024f, 0x0001 },
155 { USBTV_BASE
+ 0x0254, 0x005f },
156 { USBTV_BASE
+ 0x025a, 0x0012 },
157 { USBTV_BASE
+ 0x025b, 0x0001 },
158 { USBTV_BASE
+ 0x0263, 0x001c },
159 { USBTV_BASE
+ 0x0266, 0x0011 },
160 { USBTV_BASE
+ 0x0267, 0x0005 }
163 ret
= usbtv_configure_for_norm(usbtv
, norm
);
166 if (norm
& V4L2_STD_525_60
)
167 ret
= usbtv_set_regs(usbtv
, ntsc
, ARRAY_SIZE(ntsc
));
168 else if (norm
& V4L2_STD_PAL
)
169 ret
= usbtv_set_regs(usbtv
, pal
, ARRAY_SIZE(pal
));
175 static int usbtv_setup_capture(struct usbtv
*usbtv
)
178 static const u16 setup
[][2] = {
179 /* These seem to enable the device. */
180 { USBTV_BASE
+ 0x0008, 0x0001 },
181 { USBTV_BASE
+ 0x01d0, 0x00ff },
182 { USBTV_BASE
+ 0x01d9, 0x0002 },
184 /* These seem to influence color parameters, such as
185 * brightness, etc. */
186 { USBTV_BASE
+ 0x0239, 0x0040 },
187 { USBTV_BASE
+ 0x0240, 0x0000 },
188 { USBTV_BASE
+ 0x0241, 0x0000 },
189 { USBTV_BASE
+ 0x0242, 0x0002 },
190 { USBTV_BASE
+ 0x0243, 0x0080 },
191 { USBTV_BASE
+ 0x0244, 0x0012 },
192 { USBTV_BASE
+ 0x0245, 0x0090 },
193 { USBTV_BASE
+ 0x0246, 0x0000 },
195 { USBTV_BASE
+ 0x0278, 0x002d },
196 { USBTV_BASE
+ 0x0279, 0x000a },
197 { USBTV_BASE
+ 0x027a, 0x0032 },
201 { USBTV_BASE
+ 0x00ac, 0x00c0 },
202 { USBTV_BASE
+ 0x00ad, 0x0000 },
203 { USBTV_BASE
+ 0x00a2, 0x0012 },
204 { USBTV_BASE
+ 0x00a3, 0x00e0 },
205 { USBTV_BASE
+ 0x00a4, 0x0028 },
206 { USBTV_BASE
+ 0x00a5, 0x0082 },
207 { USBTV_BASE
+ 0x00a7, 0x0080 },
208 { USBTV_BASE
+ 0x0000, 0x0014 },
209 { USBTV_BASE
+ 0x0006, 0x0003 },
210 { USBTV_BASE
+ 0x0090, 0x0099 },
211 { USBTV_BASE
+ 0x0091, 0x0090 },
212 { USBTV_BASE
+ 0x0094, 0x0068 },
213 { USBTV_BASE
+ 0x0095, 0x0070 },
214 { USBTV_BASE
+ 0x009c, 0x0030 },
215 { USBTV_BASE
+ 0x009d, 0x00c0 },
216 { USBTV_BASE
+ 0x009e, 0x00e0 },
217 { USBTV_BASE
+ 0x0019, 0x0006 },
218 { USBTV_BASE
+ 0x008c, 0x00ba },
219 { USBTV_BASE
+ 0x0101, 0x00ff },
220 { USBTV_BASE
+ 0x010c, 0x00b3 },
221 { USBTV_BASE
+ 0x01b2, 0x0080 },
222 { USBTV_BASE
+ 0x01b4, 0x00a0 },
223 { USBTV_BASE
+ 0x014c, 0x00ff },
224 { USBTV_BASE
+ 0x014d, 0x00ca },
225 { USBTV_BASE
+ 0x0113, 0x0053 },
226 { USBTV_BASE
+ 0x0119, 0x008a },
227 { USBTV_BASE
+ 0x013c, 0x0003 },
228 { USBTV_BASE
+ 0x0150, 0x009c },
229 { USBTV_BASE
+ 0x0151, 0x0071 },
230 { USBTV_BASE
+ 0x0152, 0x00c6 },
231 { USBTV_BASE
+ 0x0153, 0x0084 },
232 { USBTV_BASE
+ 0x0154, 0x00bc },
233 { USBTV_BASE
+ 0x0155, 0x00a0 },
234 { USBTV_BASE
+ 0x0156, 0x00a0 },
235 { USBTV_BASE
+ 0x0157, 0x009c },
236 { USBTV_BASE
+ 0x0158, 0x001f },
237 { USBTV_BASE
+ 0x0159, 0x0006 },
238 { USBTV_BASE
+ 0x015d, 0x0000 },
240 { USBTV_BASE
+ 0x0003, 0x0004 },
241 { USBTV_BASE
+ 0x0100, 0x00d3 },
242 { USBTV_BASE
+ 0x0115, 0x0015 },
243 { USBTV_BASE
+ 0x0220, 0x002e },
244 { USBTV_BASE
+ 0x0225, 0x0008 },
245 { USBTV_BASE
+ 0x024e, 0x0002 },
246 { USBTV_BASE
+ 0x024e, 0x0002 },
247 { USBTV_BASE
+ 0x024f, 0x0002 },
250 ret
= usbtv_set_regs(usbtv
, setup
, ARRAY_SIZE(setup
));
254 ret
= usbtv_select_norm(usbtv
, usbtv
->norm
);
258 ret
= usbtv_select_input(usbtv
, usbtv
->input
);
262 ret
= v4l2_ctrl_handler_setup(&usbtv
->ctrl
);
269 /* Copy data from chunk into a frame buffer, deinterlacing the data
270 * into every second line. Unfortunately, they don't align nicely into
271 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
272 * Therefore, we break down the chunk into two halves before copying,
273 * so that we can interleave a line if needed.
275 * Each "chunk" is 240 words; a word in this context equals 4 bytes.
276 * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
277 * pixels, the Cr and Cb shared between the two pixels, but each having
278 * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
279 * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
280 * The image is interlaced, so there is a "scan" of odd lines, followed
281 * by "scan" of even numbered lines.
283 * Following code is writing the chunks in correct sequence, skipping
284 * the rows based on "odd" value.
285 * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
286 * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
289 static void usbtv_chunk_to_vbuf(u32
*frame
, __be32
*src
, int chunk_no
, int odd
)
293 for (half
= 0; half
< 2; half
++) {
294 int part_no
= chunk_no
* 2 + half
;
295 int line
= part_no
/ 3;
296 int part_index
= (line
* 2 + !odd
) * 3 + (part_no
% 3);
298 u32
*dst
= &frame
[part_index
* USBTV_CHUNK
/2];
300 memcpy(dst
, src
, USBTV_CHUNK
/2 * sizeof(*src
));
301 src
+= USBTV_CHUNK
/2;
305 /* Called for each 256-byte image chunk.
306 * First word identifies the chunk, followed by 240 words of image
307 * data and padding. */
308 static void usbtv_image_chunk(struct usbtv
*usbtv
, __be32
*chunk
)
310 int frame_id
, odd
, chunk_no
;
312 struct usbtv_buf
*buf
;
315 /* Ignore corrupted lines. */
316 if (!USBTV_MAGIC_OK(chunk
))
318 frame_id
= USBTV_FRAME_ID(chunk
);
319 odd
= USBTV_ODD(chunk
);
320 chunk_no
= USBTV_CHUNK_NO(chunk
);
321 if (chunk_no
>= usbtv
->n_chunks
)
324 /* Beginning of a frame. */
326 usbtv
->frame_id
= frame_id
;
327 usbtv
->chunks_done
= 0;
330 if (usbtv
->frame_id
!= frame_id
)
333 spin_lock_irqsave(&usbtv
->buflock
, flags
);
334 if (list_empty(&usbtv
->bufs
)) {
335 /* No free buffers. Userspace likely too slow. */
336 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
340 /* First available buffer. */
341 buf
= list_first_entry(&usbtv
->bufs
, struct usbtv_buf
, list
);
342 frame
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
344 /* Copy the chunk data. */
345 usbtv_chunk_to_vbuf(frame
, &chunk
[1], chunk_no
, odd
);
346 usbtv
->chunks_done
++;
348 /* Last chunk in a field */
349 if (chunk_no
== usbtv
->n_chunks
-1) {
350 /* Last chunk in a frame, signalling an end */
351 if (odd
&& !usbtv
->last_odd
) {
352 int size
= vb2_plane_size(&buf
->vb
.vb2_buf
, 0);
353 enum vb2_buffer_state state
= usbtv
->chunks_done
==
358 buf
->vb
.field
= V4L2_FIELD_INTERLACED
;
359 buf
->vb
.sequence
= usbtv
->sequence
++;
360 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
361 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, size
);
362 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
363 list_del(&buf
->list
);
365 usbtv
->last_odd
= odd
;
368 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
371 /* Got image data. Each packet contains a number of 256-word chunks we
372 * compose the image from. */
373 static void usbtv_iso_cb(struct urb
*ip
)
377 struct usbtv
*usbtv
= (struct usbtv
*)ip
->context
;
379 switch (ip
->status
) {
383 /* Device disconnected or capture stopped? */
389 /* Unknown error. Retry. */
391 dev_warn(usbtv
->dev
, "Bad response for ISO request.\n");
395 for (i
= 0; i
< ip
->number_of_packets
; i
++) {
396 int size
= ip
->iso_frame_desc
[i
].actual_length
;
397 unsigned char *data
= ip
->transfer_buffer
+
398 ip
->iso_frame_desc
[i
].offset
;
401 for (offset
= 0; USBTV_CHUNK_SIZE
* offset
< size
; offset
++)
402 usbtv_image_chunk(usbtv
,
403 (__be32
*)&data
[USBTV_CHUNK_SIZE
* offset
]);
407 ret
= usb_submit_urb(ip
, GFP_ATOMIC
);
409 dev_warn(usbtv
->dev
, "Could not resubmit ISO URB\n");
412 static struct urb
*usbtv_setup_iso_transfer(struct usbtv
*usbtv
)
415 int size
= usbtv
->iso_size
;
418 ip
= usb_alloc_urb(USBTV_ISOC_PACKETS
, GFP_KERNEL
);
422 ip
->dev
= usbtv
->udev
;
424 ip
->pipe
= usb_rcvisocpipe(usbtv
->udev
, USBTV_VIDEO_ENDP
);
426 ip
->transfer_flags
= URB_ISO_ASAP
;
427 ip
->transfer_buffer
= kzalloc(size
* USBTV_ISOC_PACKETS
,
429 if (!ip
->transfer_buffer
) {
433 ip
->complete
= usbtv_iso_cb
;
434 ip
->number_of_packets
= USBTV_ISOC_PACKETS
;
435 ip
->transfer_buffer_length
= size
* USBTV_ISOC_PACKETS
;
436 for (i
= 0; i
< USBTV_ISOC_PACKETS
; i
++) {
437 ip
->iso_frame_desc
[i
].offset
= size
* i
;
438 ip
->iso_frame_desc
[i
].length
= size
;
444 static void usbtv_stop(struct usbtv
*usbtv
)
449 /* Cancel running transfers. */
450 for (i
= 0; i
< USBTV_ISOC_TRANSFERS
; i
++) {
451 struct urb
*ip
= usbtv
->isoc_urbs
[i
];
456 kfree(ip
->transfer_buffer
);
458 usbtv
->isoc_urbs
[i
] = NULL
;
461 /* Return buffers to userspace. */
462 spin_lock_irqsave(&usbtv
->buflock
, flags
);
463 while (!list_empty(&usbtv
->bufs
)) {
464 struct usbtv_buf
*buf
= list_first_entry(&usbtv
->bufs
,
465 struct usbtv_buf
, list
);
466 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
467 list_del(&buf
->list
);
469 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
472 static int usbtv_start(struct usbtv
*usbtv
)
477 usbtv_audio_suspend(usbtv
);
479 ret
= usb_set_interface(usbtv
->udev
, 0, 0);
483 ret
= usbtv_setup_capture(usbtv
);
487 ret
= usb_set_interface(usbtv
->udev
, 0, 1);
491 usbtv_audio_resume(usbtv
);
493 for (i
= 0; i
< USBTV_ISOC_TRANSFERS
; i
++) {
496 ip
= usbtv_setup_iso_transfer(usbtv
);
501 usbtv
->isoc_urbs
[i
] = ip
;
503 ret
= usb_submit_urb(ip
, GFP_KERNEL
);
515 static int usbtv_querycap(struct file
*file
, void *priv
,
516 struct v4l2_capability
*cap
)
518 struct usbtv
*dev
= video_drvdata(file
);
520 strlcpy(cap
->driver
, "usbtv", sizeof(cap
->driver
));
521 strlcpy(cap
->card
, "usbtv", sizeof(cap
->card
));
522 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
523 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
;
524 cap
->device_caps
|= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
525 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
529 static int usbtv_enum_input(struct file
*file
, void *priv
,
530 struct v4l2_input
*i
)
532 struct usbtv
*dev
= video_drvdata(file
);
535 case USBTV_COMPOSITE_INPUT
:
536 strlcpy(i
->name
, "Composite", sizeof(i
->name
));
538 case USBTV_SVIDEO_INPUT
:
539 strlcpy(i
->name
, "S-Video", sizeof(i
->name
));
545 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
546 i
->std
= dev
->vdev
.tvnorms
;
550 static int usbtv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
551 struct v4l2_fmtdesc
*f
)
556 strlcpy(f
->description
, "16 bpp YUY2, 4:2:2, packed",
557 sizeof(f
->description
));
558 f
->pixelformat
= V4L2_PIX_FMT_YUYV
;
562 static int usbtv_fmt_vid_cap(struct file
*file
, void *priv
,
563 struct v4l2_format
*f
)
565 struct usbtv
*usbtv
= video_drvdata(file
);
567 f
->fmt
.pix
.width
= usbtv
->width
;
568 f
->fmt
.pix
.height
= usbtv
->height
;
569 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
570 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
571 f
->fmt
.pix
.bytesperline
= usbtv
->width
* 2;
572 f
->fmt
.pix
.sizeimage
= (f
->fmt
.pix
.bytesperline
* f
->fmt
.pix
.height
);
573 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
578 static int usbtv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
580 struct usbtv
*usbtv
= video_drvdata(file
);
585 static int usbtv_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
588 struct usbtv
*usbtv
= video_drvdata(file
);
590 if ((norm
& V4L2_STD_525_60
) || (norm
& V4L2_STD_PAL
))
591 ret
= usbtv_select_norm(usbtv
, norm
);
596 static int usbtv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
598 struct usbtv
*usbtv
= video_drvdata(file
);
603 static int usbtv_s_input(struct file
*file
, void *priv
, unsigned int i
)
605 struct usbtv
*usbtv
= video_drvdata(file
);
607 return usbtv_select_input(usbtv
, i
);
610 static struct v4l2_ioctl_ops usbtv_ioctl_ops
= {
611 .vidioc_querycap
= usbtv_querycap
,
612 .vidioc_enum_input
= usbtv_enum_input
,
613 .vidioc_enum_fmt_vid_cap
= usbtv_enum_fmt_vid_cap
,
614 .vidioc_g_fmt_vid_cap
= usbtv_fmt_vid_cap
,
615 .vidioc_try_fmt_vid_cap
= usbtv_fmt_vid_cap
,
616 .vidioc_s_fmt_vid_cap
= usbtv_fmt_vid_cap
,
617 .vidioc_g_std
= usbtv_g_std
,
618 .vidioc_s_std
= usbtv_s_std
,
619 .vidioc_g_input
= usbtv_g_input
,
620 .vidioc_s_input
= usbtv_s_input
,
622 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
623 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
624 .vidioc_querybuf
= vb2_ioctl_querybuf
,
625 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
626 .vidioc_qbuf
= vb2_ioctl_qbuf
,
627 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
628 .vidioc_streamon
= vb2_ioctl_streamon
,
629 .vidioc_streamoff
= vb2_ioctl_streamoff
,
632 static const struct v4l2_file_operations usbtv_fops
= {
633 .owner
= THIS_MODULE
,
634 .unlocked_ioctl
= video_ioctl2
,
635 .mmap
= vb2_fop_mmap
,
636 .open
= v4l2_fh_open
,
637 .release
= vb2_fop_release
,
638 .read
= vb2_fop_read
,
639 .poll
= vb2_fop_poll
,
642 static int usbtv_queue_setup(struct vb2_queue
*vq
,
643 unsigned int *nbuffers
,
644 unsigned int *nplanes
, unsigned int sizes
[], struct device
*alloc_devs
[])
646 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
647 unsigned size
= USBTV_CHUNK
* usbtv
->n_chunks
* 2 * sizeof(u32
);
649 if (vq
->num_buffers
+ *nbuffers
< 2)
650 *nbuffers
= 2 - vq
->num_buffers
;
652 return sizes
[0] < size
? -EINVAL
: 0;
659 static void usbtv_buf_queue(struct vb2_buffer
*vb
)
661 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
662 struct usbtv
*usbtv
= vb2_get_drv_priv(vb
->vb2_queue
);
663 struct usbtv_buf
*buf
= container_of(vbuf
, struct usbtv_buf
, vb
);
666 if (usbtv
->udev
== NULL
) {
667 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
671 spin_lock_irqsave(&usbtv
->buflock
, flags
);
672 list_add_tail(&buf
->list
, &usbtv
->bufs
);
673 spin_unlock_irqrestore(&usbtv
->buflock
, flags
);
676 static int usbtv_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
678 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
680 if (usbtv
->udev
== NULL
)
685 return usbtv_start(usbtv
);
688 static void usbtv_stop_streaming(struct vb2_queue
*vq
)
690 struct usbtv
*usbtv
= vb2_get_drv_priv(vq
);
696 static const struct vb2_ops usbtv_vb2_ops
= {
697 .queue_setup
= usbtv_queue_setup
,
698 .buf_queue
= usbtv_buf_queue
,
699 .start_streaming
= usbtv_start_streaming
,
700 .stop_streaming
= usbtv_stop_streaming
,
703 static int usbtv_s_ctrl(struct v4l2_ctrl
*ctrl
)
705 struct usbtv
*usbtv
= container_of(ctrl
->handler
, struct usbtv
,
711 data
= kmalloc(3, GFP_KERNEL
);
716 * Read in the current brightness/contrast registers. We need them
717 * both, because the values are for some reason interleaved.
719 if (ctrl
->id
== V4L2_CID_BRIGHTNESS
|| ctrl
->id
== V4L2_CID_CONTRAST
) {
720 ret
= usb_control_msg(usbtv
->udev
,
721 usb_rcvctrlpipe(usbtv
->udev
, 0), USBTV_CONTROL_REG
,
722 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
723 0, USBTV_BASE
+ 0x0244, (void *)data
, 3, 0);
729 case V4L2_CID_BRIGHTNESS
:
730 index
= USBTV_BASE
+ 0x0244;
733 data
[0] |= (ctrl
->val
>> 8) & 0xf;
734 data
[2] = ctrl
->val
& 0xff;
736 case V4L2_CID_CONTRAST
:
737 index
= USBTV_BASE
+ 0x0244;
740 data
[0] |= (ctrl
->val
>> 4) & 0xf0;
741 data
[1] = ctrl
->val
& 0xff;
743 case V4L2_CID_SATURATION
:
744 index
= USBTV_BASE
+ 0x0242;
745 data
[0] = ctrl
->val
>> 8;
746 data
[1] = ctrl
->val
& 0xff;
750 index
= USBTV_BASE
+ 0x0240;
753 data
[0] = 0x92 + (ctrl
->val
>> 8);
754 data
[1] = ctrl
->val
& 0xff;
756 data
[0] = 0x82 + (-ctrl
->val
>> 8);
757 data
[1] = -ctrl
->val
& 0xff;
760 case V4L2_CID_SHARPNESS
:
761 index
= USBTV_BASE
+ 0x0239;
771 ret
= usb_control_msg(usbtv
->udev
, usb_sndctrlpipe(usbtv
->udev
, 0),
773 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
774 0, index
, (void *)data
, size
, 0);
778 dev_warn(usbtv
->dev
, "Failed to submit a control request.\n");
784 static const struct v4l2_ctrl_ops usbtv_ctrl_ops
= {
785 .s_ctrl
= usbtv_s_ctrl
,
788 static void usbtv_release(struct v4l2_device
*v4l2_dev
)
790 struct usbtv
*usbtv
= container_of(v4l2_dev
, struct usbtv
, v4l2_dev
);
792 v4l2_device_unregister(&usbtv
->v4l2_dev
);
793 v4l2_ctrl_handler_free(&usbtv
->ctrl
);
794 vb2_queue_release(&usbtv
->vb2q
);
798 int usbtv_video_init(struct usbtv
*usbtv
)
802 (void)usbtv_configure_for_norm(usbtv
, V4L2_STD_525_60
);
804 spin_lock_init(&usbtv
->buflock
);
805 mutex_init(&usbtv
->v4l2_lock
);
806 mutex_init(&usbtv
->vb2q_lock
);
807 INIT_LIST_HEAD(&usbtv
->bufs
);
809 /* videobuf2 structure */
810 usbtv
->vb2q
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
811 usbtv
->vb2q
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
812 usbtv
->vb2q
.drv_priv
= usbtv
;
813 usbtv
->vb2q
.buf_struct_size
= sizeof(struct usbtv_buf
);
814 usbtv
->vb2q
.ops
= &usbtv_vb2_ops
;
815 usbtv
->vb2q
.mem_ops
= &vb2_vmalloc_memops
;
816 usbtv
->vb2q
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
817 usbtv
->vb2q
.lock
= &usbtv
->vb2q_lock
;
818 ret
= vb2_queue_init(&usbtv
->vb2q
);
820 dev_warn(usbtv
->dev
, "Could not initialize videobuf2 queue\n");
825 v4l2_ctrl_handler_init(&usbtv
->ctrl
, 4);
826 v4l2_ctrl_new_std(&usbtv
->ctrl
, &usbtv_ctrl_ops
,
827 V4L2_CID_CONTRAST
, 0, 0x3ff, 1, 0x1d0);
828 v4l2_ctrl_new_std(&usbtv
->ctrl
, &usbtv_ctrl_ops
,
829 V4L2_CID_BRIGHTNESS
, 0, 0x3ff, 1, 0x1c0);
830 v4l2_ctrl_new_std(&usbtv
->ctrl
, &usbtv_ctrl_ops
,
831 V4L2_CID_SATURATION
, 0, 0x3ff, 1, 0x200);
832 v4l2_ctrl_new_std(&usbtv
->ctrl
, &usbtv_ctrl_ops
,
833 V4L2_CID_HUE
, -0xdff, 0xdff, 1, 0x000);
834 v4l2_ctrl_new_std(&usbtv
->ctrl
, &usbtv_ctrl_ops
,
835 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x60);
836 ret
= usbtv
->ctrl
.error
;
838 dev_warn(usbtv
->dev
, "Could not initialize controls\n");
843 usbtv
->v4l2_dev
.ctrl_handler
= &usbtv
->ctrl
;
844 usbtv
->v4l2_dev
.release
= usbtv_release
;
845 ret
= v4l2_device_register(usbtv
->dev
, &usbtv
->v4l2_dev
);
847 dev_warn(usbtv
->dev
, "Could not register v4l2 device\n");
851 /* Video structure */
852 strlcpy(usbtv
->vdev
.name
, "usbtv", sizeof(usbtv
->vdev
.name
));
853 usbtv
->vdev
.v4l2_dev
= &usbtv
->v4l2_dev
;
854 usbtv
->vdev
.release
= video_device_release_empty
;
855 usbtv
->vdev
.fops
= &usbtv_fops
;
856 usbtv
->vdev
.ioctl_ops
= &usbtv_ioctl_ops
;
857 usbtv
->vdev
.tvnorms
= USBTV_TV_STD
;
858 usbtv
->vdev
.queue
= &usbtv
->vb2q
;
859 usbtv
->vdev
.lock
= &usbtv
->v4l2_lock
;
860 video_set_drvdata(&usbtv
->vdev
, usbtv
);
861 ret
= video_register_device(&usbtv
->vdev
, VFL_TYPE_GRABBER
, -1);
863 dev_warn(usbtv
->dev
, "Could not register video device\n");
870 v4l2_device_unregister(&usbtv
->v4l2_dev
);
873 v4l2_ctrl_handler_free(&usbtv
->ctrl
);
874 vb2_queue_release(&usbtv
->vb2q
);
879 void usbtv_video_free(struct usbtv
*usbtv
)
881 mutex_lock(&usbtv
->vb2q_lock
);
882 mutex_lock(&usbtv
->v4l2_lock
);
885 video_unregister_device(&usbtv
->vdev
);
886 v4l2_device_disconnect(&usbtv
->v4l2_dev
);
888 mutex_unlock(&usbtv
->v4l2_lock
);
889 mutex_unlock(&usbtv
->vb2q_lock
);
891 v4l2_device_put(&usbtv
->v4l2_dev
);