[media] usbtv: remove unused including <linux/version.h>
[linux-2.6/btrfs-unstable.git] / drivers / media / usb / usbtv / usbtv.c
blob095231608d257db42cc0d7f30cb8ff82f5184a7a
1 /*
2 * Fushicai USBTV007 Video Grabber Driver
4 * Product web site:
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.
11 * He is awesome.
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
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.
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>
43 /* Hardware. */
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_WIDTH 720
54 #define USBTV_HEIGHT 480
56 #define USBTV_CHUNK_SIZE 256
57 #define USBTV_CHUNK 240
58 #define USBTV_CHUNKS (USBTV_WIDTH * USBTV_HEIGHT \
59 / 2 / USBTV_CHUNK)
61 /* Chunk header. */
62 #define USBTV_MAGIC_OK(chunk) ((be32_to_cpu(chunk[0]) & 0xff000000) \
63 == 0x88000000)
64 #define USBTV_FRAME_ID(chunk) ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16)
65 #define USBTV_ODD(chunk) ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
66 #define USBTV_CHUNK_NO(chunk) (be32_to_cpu(chunk[0]) & 0x00000fff)
68 /* A single videobuf2 frame buffer. */
69 struct usbtv_buf {
70 struct vb2_buffer vb;
71 struct list_head list;
74 /* Per-device structure. */
75 struct usbtv {
76 struct device *dev;
77 struct usb_device *udev;
78 struct v4l2_device v4l2_dev;
79 struct video_device vdev;
80 struct vb2_queue vb2q;
81 struct mutex v4l2_lock;
82 struct mutex vb2q_lock;
84 /* List of videobuf2 buffers protected by a lock. */
85 spinlock_t buflock;
86 struct list_head bufs;
88 /* Number of currently processed frame, useful find
89 * out when a new one begins. */
90 u32 frame_id;
92 enum {
93 USBTV_COMPOSITE_INPUT,
94 USBTV_SVIDEO_INPUT,
95 } input;
96 int iso_size;
97 unsigned int sequence;
98 struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS];
101 static int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
103 int ret;
104 int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
105 int i;
107 for (i = 0; i < size; i++) {
108 u16 index = regs[i][0];
109 u16 value = regs[i][1];
111 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
112 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113 value, index, NULL, 0, 0);
114 if (ret < 0)
115 return ret;
118 return 0;
121 static int usbtv_select_input(struct usbtv *usbtv, int input)
123 int ret;
125 static const u16 composite[][2] = {
126 { USBTV_BASE + 0x0105, 0x0060 },
127 { USBTV_BASE + 0x011f, 0x00f2 },
128 { USBTV_BASE + 0x0127, 0x0060 },
129 { USBTV_BASE + 0x00ae, 0x0010 },
130 { USBTV_BASE + 0x0284, 0x00aa },
131 { USBTV_BASE + 0x0239, 0x0060 },
134 static const u16 svideo[][2] = {
135 { USBTV_BASE + 0x0105, 0x0010 },
136 { USBTV_BASE + 0x011f, 0x00ff },
137 { USBTV_BASE + 0x0127, 0x0060 },
138 { USBTV_BASE + 0x00ae, 0x0030 },
139 { USBTV_BASE + 0x0284, 0x0088 },
140 { USBTV_BASE + 0x0239, 0x0060 },
143 switch (input) {
144 case USBTV_COMPOSITE_INPUT:
145 ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
146 break;
147 case USBTV_SVIDEO_INPUT:
148 ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
149 break;
150 default:
151 ret = -EINVAL;
154 if (!ret)
155 usbtv->input = input;
157 return ret;
160 static int usbtv_setup_capture(struct usbtv *usbtv)
162 int ret;
163 static const u16 setup[][2] = {
164 /* These seem to enable the device. */
165 { USBTV_BASE + 0x0008, 0x0001 },
166 { USBTV_BASE + 0x01d0, 0x00ff },
167 { USBTV_BASE + 0x01d9, 0x0002 },
169 /* These seem to influence color parameters, such as
170 * brightness, etc. */
171 { USBTV_BASE + 0x0239, 0x0040 },
172 { USBTV_BASE + 0x0240, 0x0000 },
173 { USBTV_BASE + 0x0241, 0x0000 },
174 { USBTV_BASE + 0x0242, 0x0002 },
175 { USBTV_BASE + 0x0243, 0x0080 },
176 { USBTV_BASE + 0x0244, 0x0012 },
177 { USBTV_BASE + 0x0245, 0x0090 },
178 { USBTV_BASE + 0x0246, 0x0000 },
180 { USBTV_BASE + 0x0278, 0x002d },
181 { USBTV_BASE + 0x0279, 0x000a },
182 { USBTV_BASE + 0x027a, 0x0032 },
183 { 0xf890, 0x000c },
184 { 0xf894, 0x0086 },
186 { USBTV_BASE + 0x00ac, 0x00c0 },
187 { USBTV_BASE + 0x00ad, 0x0000 },
188 { USBTV_BASE + 0x00a2, 0x0012 },
189 { USBTV_BASE + 0x00a3, 0x00e0 },
190 { USBTV_BASE + 0x00a4, 0x0028 },
191 { USBTV_BASE + 0x00a5, 0x0082 },
192 { USBTV_BASE + 0x00a7, 0x0080 },
193 { USBTV_BASE + 0x0000, 0x0014 },
194 { USBTV_BASE + 0x0006, 0x0003 },
195 { USBTV_BASE + 0x0090, 0x0099 },
196 { USBTV_BASE + 0x0091, 0x0090 },
197 { USBTV_BASE + 0x0094, 0x0068 },
198 { USBTV_BASE + 0x0095, 0x0070 },
199 { USBTV_BASE + 0x009c, 0x0030 },
200 { USBTV_BASE + 0x009d, 0x00c0 },
201 { USBTV_BASE + 0x009e, 0x00e0 },
202 { USBTV_BASE + 0x0019, 0x0006 },
203 { USBTV_BASE + 0x008c, 0x00ba },
204 { USBTV_BASE + 0x0101, 0x00ff },
205 { USBTV_BASE + 0x010c, 0x00b3 },
206 { USBTV_BASE + 0x01b2, 0x0080 },
207 { USBTV_BASE + 0x01b4, 0x00a0 },
208 { USBTV_BASE + 0x014c, 0x00ff },
209 { USBTV_BASE + 0x014d, 0x00ca },
210 { USBTV_BASE + 0x0113, 0x0053 },
211 { USBTV_BASE + 0x0119, 0x008a },
212 { USBTV_BASE + 0x013c, 0x0003 },
213 { USBTV_BASE + 0x0150, 0x009c },
214 { USBTV_BASE + 0x0151, 0x0071 },
215 { USBTV_BASE + 0x0152, 0x00c6 },
216 { USBTV_BASE + 0x0153, 0x0084 },
217 { USBTV_BASE + 0x0154, 0x00bc },
218 { USBTV_BASE + 0x0155, 0x00a0 },
219 { USBTV_BASE + 0x0156, 0x00a0 },
220 { USBTV_BASE + 0x0157, 0x009c },
221 { USBTV_BASE + 0x0158, 0x001f },
222 { USBTV_BASE + 0x0159, 0x0006 },
223 { USBTV_BASE + 0x015d, 0x0000 },
225 { USBTV_BASE + 0x0284, 0x0088 },
226 { USBTV_BASE + 0x0003, 0x0004 },
227 { USBTV_BASE + 0x001a, 0x0079 },
228 { USBTV_BASE + 0x0100, 0x00d3 },
229 { USBTV_BASE + 0x010e, 0x0068 },
230 { USBTV_BASE + 0x010f, 0x009c },
231 { USBTV_BASE + 0x0112, 0x00f0 },
232 { USBTV_BASE + 0x0115, 0x0015 },
233 { USBTV_BASE + 0x0117, 0x0000 },
234 { USBTV_BASE + 0x0118, 0x00fc },
235 { USBTV_BASE + 0x012d, 0x0004 },
236 { USBTV_BASE + 0x012f, 0x0008 },
237 { USBTV_BASE + 0x0220, 0x002e },
238 { USBTV_BASE + 0x0225, 0x0008 },
239 { USBTV_BASE + 0x024e, 0x0002 },
240 { USBTV_BASE + 0x024f, 0x0001 },
241 { USBTV_BASE + 0x0254, 0x005f },
242 { USBTV_BASE + 0x025a, 0x0012 },
243 { USBTV_BASE + 0x025b, 0x0001 },
244 { USBTV_BASE + 0x0263, 0x001c },
245 { USBTV_BASE + 0x0266, 0x0011 },
246 { USBTV_BASE + 0x0267, 0x0005 },
247 { USBTV_BASE + 0x024e, 0x0002 },
248 { USBTV_BASE + 0x024f, 0x0002 },
251 ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
252 if (ret)
253 return ret;
255 ret = usbtv_select_input(usbtv, usbtv->input);
256 if (ret)
257 return ret;
259 return 0;
262 /* Called for each 256-byte image chunk.
263 * First word identifies the chunk, followed by 240 words of image
264 * data and padding. */
265 static void usbtv_image_chunk(struct usbtv *usbtv, u32 *chunk)
267 int frame_id, odd, chunk_no;
268 u32 *frame;
269 struct usbtv_buf *buf;
270 unsigned long flags;
272 /* Ignore corrupted lines. */
273 if (!USBTV_MAGIC_OK(chunk))
274 return;
275 frame_id = USBTV_FRAME_ID(chunk);
276 odd = USBTV_ODD(chunk);
277 chunk_no = USBTV_CHUNK_NO(chunk);
279 /* Deinterlace. TODO: Use interlaced frame format. */
280 chunk_no = (chunk_no - chunk_no % 3) * 2 + chunk_no % 3;
281 chunk_no += !odd * 3;
283 if (chunk_no >= USBTV_CHUNKS)
284 return;
286 /* Beginning of a frame. */
287 if (chunk_no == 0)
288 usbtv->frame_id = frame_id;
290 spin_lock_irqsave(&usbtv->buflock, flags);
291 if (list_empty(&usbtv->bufs)) {
292 /* No free buffers. Userspace likely too slow. */
293 spin_unlock_irqrestore(&usbtv->buflock, flags);
294 return;
297 /* First available buffer. */
298 buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
299 frame = vb2_plane_vaddr(&buf->vb, 0);
301 /* Copy the chunk. */
302 memcpy(&frame[chunk_no * USBTV_CHUNK], &chunk[1],
303 USBTV_CHUNK * sizeof(chunk[1]));
305 /* Last chunk in a frame, signalling an end */
306 if (usbtv->frame_id && chunk_no == USBTV_CHUNKS-1) {
307 int size = vb2_plane_size(&buf->vb, 0);
309 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
310 buf->vb.v4l2_buf.sequence = usbtv->sequence++;
311 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
312 vb2_set_plane_payload(&buf->vb, 0, size);
313 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
314 list_del(&buf->list);
317 spin_unlock_irqrestore(&usbtv->buflock, flags);
320 /* Got image data. Each packet contains a number of 256-word chunks we
321 * compose the image from. */
322 static void usbtv_iso_cb(struct urb *ip)
324 int ret;
325 int i;
326 struct usbtv *usbtv = (struct usbtv *)ip->context;
328 switch (ip->status) {
329 /* All fine. */
330 case 0:
331 break;
332 /* Device disconnected or capture stopped? */
333 case -ENODEV:
334 case -ENOENT:
335 case -ECONNRESET:
336 case -ESHUTDOWN:
337 return;
338 /* Unknown error. Retry. */
339 default:
340 dev_warn(usbtv->dev, "Bad response for ISO request.\n");
341 goto resubmit;
344 for (i = 0; i < ip->number_of_packets; i++) {
345 int size = ip->iso_frame_desc[i].actual_length;
346 unsigned char *data = ip->transfer_buffer +
347 ip->iso_frame_desc[i].offset;
348 int offset;
350 for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
351 usbtv_image_chunk(usbtv,
352 (u32 *)&data[USBTV_CHUNK_SIZE * offset]);
355 resubmit:
356 ret = usb_submit_urb(ip, GFP_ATOMIC);
357 if (ret < 0)
358 dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
361 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
363 struct urb *ip;
364 int size = usbtv->iso_size;
365 int i;
367 ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
368 if (ip == NULL)
369 return NULL;
371 ip->dev = usbtv->udev;
372 ip->context = usbtv;
373 ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
374 ip->interval = 1;
375 ip->transfer_flags = URB_ISO_ASAP;
376 ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
377 GFP_KERNEL);
378 ip->complete = usbtv_iso_cb;
379 ip->number_of_packets = USBTV_ISOC_PACKETS;
380 ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
381 for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
382 ip->iso_frame_desc[i].offset = size * i;
383 ip->iso_frame_desc[i].length = size;
386 return ip;
389 static void usbtv_stop(struct usbtv *usbtv)
391 int i;
392 unsigned long flags;
394 /* Cancel running transfers. */
395 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
396 struct urb *ip = usbtv->isoc_urbs[i];
397 if (ip == NULL)
398 continue;
399 usb_kill_urb(ip);
400 kfree(ip->transfer_buffer);
401 usb_free_urb(ip);
402 usbtv->isoc_urbs[i] = NULL;
405 /* Return buffers to userspace. */
406 spin_lock_irqsave(&usbtv->buflock, flags);
407 while (!list_empty(&usbtv->bufs)) {
408 struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
409 struct usbtv_buf, list);
410 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
411 list_del(&buf->list);
413 spin_unlock_irqrestore(&usbtv->buflock, flags);
416 static int usbtv_start(struct usbtv *usbtv)
418 int i;
419 int ret;
421 ret = usb_set_interface(usbtv->udev, 0, 0);
422 if (ret < 0)
423 return ret;
425 ret = usbtv_setup_capture(usbtv);
426 if (ret < 0)
427 return ret;
429 ret = usb_set_interface(usbtv->udev, 0, 1);
430 if (ret < 0)
431 return ret;
433 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
434 struct urb *ip;
436 ip = usbtv_setup_iso_transfer(usbtv);
437 if (ip == NULL) {
438 ret = -ENOMEM;
439 goto start_fail;
441 usbtv->isoc_urbs[i] = ip;
443 ret = usb_submit_urb(ip, GFP_KERNEL);
444 if (ret < 0)
445 goto start_fail;
448 return 0;
450 start_fail:
451 usbtv_stop(usbtv);
452 return ret;
455 struct usb_device_id usbtv_id_table[] = {
456 { USB_DEVICE(0x1b71, 0x3002) },
459 MODULE_DEVICE_TABLE(usb, usbtv_id_table);
461 static int usbtv_querycap(struct file *file, void *priv,
462 struct v4l2_capability *cap)
464 struct usbtv *dev = video_drvdata(file);
466 strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
467 strlcpy(cap->card, "usbtv", sizeof(cap->card));
468 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
469 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
470 cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
471 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
472 return 0;
475 static int usbtv_enum_input(struct file *file, void *priv,
476 struct v4l2_input *i)
478 switch (i->index) {
479 case USBTV_COMPOSITE_INPUT:
480 strlcpy(i->name, "Composite", sizeof(i->name));
481 break;
482 case USBTV_SVIDEO_INPUT:
483 strlcpy(i->name, "S-Video", sizeof(i->name));
484 break;
485 default:
486 return -EINVAL;
489 i->type = V4L2_INPUT_TYPE_CAMERA;
490 i->std = V4L2_STD_525_60;
491 return 0;
494 static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
495 struct v4l2_fmtdesc *f)
497 if (f->index > 0)
498 return -EINVAL;
500 strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
501 sizeof(f->description));
502 f->pixelformat = V4L2_PIX_FMT_YUYV;
503 return 0;
506 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
509 f->fmt.pix.width = USBTV_WIDTH;
510 f->fmt.pix.height = USBTV_HEIGHT;
511 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
512 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
513 f->fmt.pix.bytesperline = USBTV_WIDTH * 2;
514 f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
515 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
516 f->fmt.pix.priv = 0;
517 return 0;
520 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
522 *norm = V4L2_STD_525_60;
523 return 0;
526 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
528 struct usbtv *usbtv = video_drvdata(file);
529 *i = usbtv->input;
530 return 0;
533 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
535 struct usbtv *usbtv = video_drvdata(file);
536 return usbtv_select_input(usbtv, i);
539 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
541 if (norm & V4L2_STD_525_60)
542 return 0;
543 return -EINVAL;
546 struct v4l2_ioctl_ops usbtv_ioctl_ops = {
547 .vidioc_querycap = usbtv_querycap,
548 .vidioc_enum_input = usbtv_enum_input,
549 .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
550 .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
551 .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
552 .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
553 .vidioc_g_std = usbtv_g_std,
554 .vidioc_s_std = usbtv_s_std,
555 .vidioc_g_input = usbtv_g_input,
556 .vidioc_s_input = usbtv_s_input,
558 .vidioc_reqbufs = vb2_ioctl_reqbufs,
559 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
560 .vidioc_querybuf = vb2_ioctl_querybuf,
561 .vidioc_create_bufs = vb2_ioctl_create_bufs,
562 .vidioc_qbuf = vb2_ioctl_qbuf,
563 .vidioc_dqbuf = vb2_ioctl_dqbuf,
564 .vidioc_streamon = vb2_ioctl_streamon,
565 .vidioc_streamoff = vb2_ioctl_streamoff,
568 struct v4l2_file_operations usbtv_fops = {
569 .owner = THIS_MODULE,
570 .unlocked_ioctl = video_ioctl2,
571 .mmap = vb2_fop_mmap,
572 .open = v4l2_fh_open,
573 .release = vb2_fop_release,
574 .read = vb2_fop_read,
575 .poll = vb2_fop_poll,
578 static int usbtv_queue_setup(struct vb2_queue *vq,
579 const struct v4l2_format *v4l_fmt, unsigned int *nbuffers,
580 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
582 if (*nbuffers < 2)
583 *nbuffers = 2;
584 *nplanes = 1;
585 sizes[0] = USBTV_CHUNK * USBTV_CHUNKS * sizeof(u32);
587 return 0;
590 static void usbtv_buf_queue(struct vb2_buffer *vb)
592 struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
593 struct usbtv_buf *buf = container_of(vb, struct usbtv_buf, vb);
594 unsigned long flags;
596 if (usbtv->udev == NULL) {
597 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
598 return;
601 spin_lock_irqsave(&usbtv->buflock, flags);
602 list_add_tail(&buf->list, &usbtv->bufs);
603 spin_unlock_irqrestore(&usbtv->buflock, flags);
606 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
608 struct usbtv *usbtv = vb2_get_drv_priv(vq);
610 if (usbtv->udev == NULL)
611 return -ENODEV;
613 return usbtv_start(usbtv);
616 static int usbtv_stop_streaming(struct vb2_queue *vq)
618 struct usbtv *usbtv = vb2_get_drv_priv(vq);
620 if (usbtv->udev == NULL)
621 return -ENODEV;
623 usbtv_stop(usbtv);
624 return 0;
627 struct vb2_ops usbtv_vb2_ops = {
628 .queue_setup = usbtv_queue_setup,
629 .buf_queue = usbtv_buf_queue,
630 .start_streaming = usbtv_start_streaming,
631 .stop_streaming = usbtv_stop_streaming,
634 static void usbtv_release(struct v4l2_device *v4l2_dev)
636 struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
638 v4l2_device_unregister(&usbtv->v4l2_dev);
639 vb2_queue_release(&usbtv->vb2q);
640 kfree(usbtv);
643 static int usbtv_probe(struct usb_interface *intf,
644 const struct usb_device_id *id)
646 int ret;
647 int size;
648 struct device *dev = &intf->dev;
649 struct usbtv *usbtv;
651 /* Checks that the device is what we think it is. */
652 if (intf->num_altsetting != 2)
653 return -ENODEV;
654 if (intf->altsetting[1].desc.bNumEndpoints != 4)
655 return -ENODEV;
657 /* Packet size is split into 11 bits of base size and count of
658 * extra multiplies of it.*/
659 size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
660 size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
662 /* Device structure */
663 usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
664 if (usbtv == NULL)
665 return -ENOMEM;
666 usbtv->dev = dev;
667 usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
668 usbtv->iso_size = size;
669 spin_lock_init(&usbtv->buflock);
670 mutex_init(&usbtv->v4l2_lock);
671 mutex_init(&usbtv->vb2q_lock);
672 INIT_LIST_HEAD(&usbtv->bufs);
674 /* videobuf2 structure */
675 usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
676 usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
677 usbtv->vb2q.drv_priv = usbtv;
678 usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
679 usbtv->vb2q.ops = &usbtv_vb2_ops;
680 usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
681 usbtv->vb2q.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
682 usbtv->vb2q.lock = &usbtv->vb2q_lock;
683 ret = vb2_queue_init(&usbtv->vb2q);
684 if (ret < 0) {
685 dev_warn(dev, "Could not initialize videobuf2 queue\n");
686 goto usbtv_fail;
689 /* v4l2 structure */
690 usbtv->v4l2_dev.release = usbtv_release;
691 ret = v4l2_device_register(dev, &usbtv->v4l2_dev);
692 if (ret < 0) {
693 dev_warn(dev, "Could not register v4l2 device\n");
694 goto v4l2_fail;
697 usb_set_intfdata(intf, usbtv);
699 /* Video structure */
700 strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
701 usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
702 usbtv->vdev.release = video_device_release_empty;
703 usbtv->vdev.fops = &usbtv_fops;
704 usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
705 usbtv->vdev.tvnorms = V4L2_STD_525_60;
706 usbtv->vdev.queue = &usbtv->vb2q;
707 usbtv->vdev.lock = &usbtv->v4l2_lock;
708 set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags);
709 video_set_drvdata(&usbtv->vdev, usbtv);
710 ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
711 if (ret < 0) {
712 dev_warn(dev, "Could not register video device\n");
713 goto vdev_fail;
716 dev_info(dev, "Fushicai USBTV007 Video Grabber\n");
717 return 0;
719 vdev_fail:
720 v4l2_device_unregister(&usbtv->v4l2_dev);
721 v4l2_fail:
722 vb2_queue_release(&usbtv->vb2q);
723 usbtv_fail:
724 kfree(usbtv);
726 return ret;
729 static void usbtv_disconnect(struct usb_interface *intf)
731 struct usbtv *usbtv = usb_get_intfdata(intf);
733 mutex_lock(&usbtv->vb2q_lock);
734 mutex_lock(&usbtv->v4l2_lock);
736 usbtv_stop(usbtv);
737 usb_set_intfdata(intf, NULL);
738 video_unregister_device(&usbtv->vdev);
739 v4l2_device_disconnect(&usbtv->v4l2_dev);
740 usb_put_dev(usbtv->udev);
741 usbtv->udev = NULL;
743 mutex_unlock(&usbtv->v4l2_lock);
744 mutex_unlock(&usbtv->vb2q_lock);
746 v4l2_device_put(&usbtv->v4l2_dev);
749 MODULE_AUTHOR("Lubomir Rintel");
750 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver");
751 MODULE_LICENSE("Dual BSD/GPL");
753 struct usb_driver usbtv_usb_driver = {
754 .name = "usbtv",
755 .id_table = usbtv_id_table,
756 .probe = usbtv_probe,
757 .disconnect = usbtv_disconnect,
760 module_usb_driver(usbtv_usb_driver);