Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[sfrench/cifs-2.6.git] / drivers / media / usb / usbtv / usbtv.c
1 /*
2  * Fushicai USBTV007 Video Grabber Driver
3  *
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
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/version.h>
37 #include <linux/videodev2.h>
38
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/videobuf2-core.h>
42 #include <media/videobuf2-vmalloc.h>
43
44 /* Hardware. */
45 #define USBTV_VIDEO_ENDP        0x81
46 #define USBTV_BASE              0xc000
47 #define USBTV_REQUEST_REG       12
48
49 /* Number of concurrent isochronous urbs submitted.
50  * Higher numbers was seen to overly saturate the USB bus. */
51 #define USBTV_ISOC_TRANSFERS    16
52 #define USBTV_ISOC_PACKETS      8
53
54 #define USBTV_WIDTH             720
55 #define USBTV_HEIGHT            480
56
57 #define USBTV_CHUNK_SIZE        256
58 #define USBTV_CHUNK             240
59 #define USBTV_CHUNKS            (USBTV_WIDTH * USBTV_HEIGHT \
60                                         / 4 / USBTV_CHUNK)
61
62 /* Chunk header. */
63 #define USBTV_MAGIC_OK(chunk)   ((be32_to_cpu(chunk[0]) & 0xff000000) \
64                                                         == 0x88000000)
65 #define USBTV_FRAME_ID(chunk)   ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16)
66 #define USBTV_ODD(chunk)        ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
67 #define USBTV_CHUNK_NO(chunk)   (be32_to_cpu(chunk[0]) & 0x00000fff)
68
69 /* A single videobuf2 frame buffer. */
70 struct usbtv_buf {
71         struct vb2_buffer vb;
72         struct list_head list;
73 };
74
75 /* Per-device structure. */
76 struct usbtv {
77         struct device *dev;
78         struct usb_device *udev;
79         struct v4l2_device v4l2_dev;
80         struct video_device vdev;
81         struct vb2_queue vb2q;
82         struct mutex v4l2_lock;
83         struct mutex vb2q_lock;
84
85         /* List of videobuf2 buffers protected by a lock. */
86         spinlock_t buflock;
87         struct list_head bufs;
88
89         /* Number of currently processed frame, useful find
90          * out when a new one begins. */
91         u32 frame_id;
92         int chunks_done;
93
94         int iso_size;
95         unsigned int sequence;
96         struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS];
97 };
98
99 static int usbtv_setup_capture(struct usbtv *usbtv)
100 {
101         int ret;
102         int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
103         int i;
104         static const u16 protoregs[][2] = {
105                 /* These seem to enable the device. */
106                 { USBTV_BASE + 0x0008, 0x0001 },
107                 { USBTV_BASE + 0x01d0, 0x00ff },
108                 { USBTV_BASE + 0x01d9, 0x0002 },
109
110                 /* These seem to influence color parameters, such as
111                  * brightness, etc. */
112                 { USBTV_BASE + 0x0239, 0x0040 },
113                 { USBTV_BASE + 0x0240, 0x0000 },
114                 { USBTV_BASE + 0x0241, 0x0000 },
115                 { USBTV_BASE + 0x0242, 0x0002 },
116                 { USBTV_BASE + 0x0243, 0x0080 },
117                 { USBTV_BASE + 0x0244, 0x0012 },
118                 { USBTV_BASE + 0x0245, 0x0090 },
119                 { USBTV_BASE + 0x0246, 0x0000 },
120
121                 { USBTV_BASE + 0x0278, 0x002d },
122                 { USBTV_BASE + 0x0279, 0x000a },
123                 { USBTV_BASE + 0x027a, 0x0032 },
124                 { 0xf890, 0x000c },
125                 { 0xf894, 0x0086 },
126
127                 { USBTV_BASE + 0x00ac, 0x00c0 },
128                 { USBTV_BASE + 0x00ad, 0x0000 },
129                 { USBTV_BASE + 0x00a2, 0x0012 },
130                 { USBTV_BASE + 0x00a3, 0x00e0 },
131                 { USBTV_BASE + 0x00a4, 0x0028 },
132                 { USBTV_BASE + 0x00a5, 0x0082 },
133                 { USBTV_BASE + 0x00a7, 0x0080 },
134                 { USBTV_BASE + 0x0000, 0x0014 },
135                 { USBTV_BASE + 0x0006, 0x0003 },
136                 { USBTV_BASE + 0x0090, 0x0099 },
137                 { USBTV_BASE + 0x0091, 0x0090 },
138                 { USBTV_BASE + 0x0094, 0x0068 },
139                 { USBTV_BASE + 0x0095, 0x0070 },
140                 { USBTV_BASE + 0x009c, 0x0030 },
141                 { USBTV_BASE + 0x009d, 0x00c0 },
142                 { USBTV_BASE + 0x009e, 0x00e0 },
143                 { USBTV_BASE + 0x0019, 0x0006 },
144                 { USBTV_BASE + 0x008c, 0x00ba },
145                 { USBTV_BASE + 0x0101, 0x00ff },
146                 { USBTV_BASE + 0x010c, 0x00b3 },
147                 { USBTV_BASE + 0x01b2, 0x0080 },
148                 { USBTV_BASE + 0x01b4, 0x00a0 },
149                 { USBTV_BASE + 0x014c, 0x00ff },
150                 { USBTV_BASE + 0x014d, 0x00ca },
151                 { USBTV_BASE + 0x0113, 0x0053 },
152                 { USBTV_BASE + 0x0119, 0x008a },
153                 { USBTV_BASE + 0x013c, 0x0003 },
154                 { USBTV_BASE + 0x0150, 0x009c },
155                 { USBTV_BASE + 0x0151, 0x0071 },
156                 { USBTV_BASE + 0x0152, 0x00c6 },
157                 { USBTV_BASE + 0x0153, 0x0084 },
158                 { USBTV_BASE + 0x0154, 0x00bc },
159                 { USBTV_BASE + 0x0155, 0x00a0 },
160                 { USBTV_BASE + 0x0156, 0x00a0 },
161                 { USBTV_BASE + 0x0157, 0x009c },
162                 { USBTV_BASE + 0x0158, 0x001f },
163                 { USBTV_BASE + 0x0159, 0x0006 },
164                 { USBTV_BASE + 0x015d, 0x0000 },
165
166                 { USBTV_BASE + 0x0284, 0x0088 },
167                 { USBTV_BASE + 0x0003, 0x0004 },
168                 { USBTV_BASE + 0x001a, 0x0079 },
169                 { USBTV_BASE + 0x0100, 0x00d3 },
170                 { USBTV_BASE + 0x010e, 0x0068 },
171                 { USBTV_BASE + 0x010f, 0x009c },
172                 { USBTV_BASE + 0x0112, 0x00f0 },
173                 { USBTV_BASE + 0x0115, 0x0015 },
174                 { USBTV_BASE + 0x0117, 0x0000 },
175                 { USBTV_BASE + 0x0118, 0x00fc },
176                 { USBTV_BASE + 0x012d, 0x0004 },
177                 { USBTV_BASE + 0x012f, 0x0008 },
178                 { USBTV_BASE + 0x0220, 0x002e },
179                 { USBTV_BASE + 0x0225, 0x0008 },
180                 { USBTV_BASE + 0x024e, 0x0002 },
181                 { USBTV_BASE + 0x024f, 0x0001 },
182                 { USBTV_BASE + 0x0254, 0x005f },
183                 { USBTV_BASE + 0x025a, 0x0012 },
184                 { USBTV_BASE + 0x025b, 0x0001 },
185                 { USBTV_BASE + 0x0263, 0x001c },
186                 { USBTV_BASE + 0x0266, 0x0011 },
187                 { USBTV_BASE + 0x0267, 0x0005 },
188                 { USBTV_BASE + 0x024e, 0x0002 },
189                 { USBTV_BASE + 0x024f, 0x0002 },
190         };
191
192         for (i = 0; i < ARRAY_SIZE(protoregs); i++) {
193                 u16 index = protoregs[i][0];
194                 u16 value = protoregs[i][1];
195
196                 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
197                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
198                         value, index, NULL, 0, 0);
199                 if (ret < 0)
200                         return ret;
201         }
202
203         return 0;
204 }
205
206 /* Copy data from chunk into a frame buffer, deinterlacing the data
207  * into every second line. Unfortunately, they don't align nicely into
208  * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
209  * Therefore, we break down the chunk into two halves before copyting,
210  * so that we can interleave a line if needed. */
211 static void usbtv_chunk_to_vbuf(u32 *frame, u32 *src, int chunk_no, int odd)
212 {
213         int half;
214
215         for (half = 0; half < 2; half++) {
216                 int part_no = chunk_no * 2 + half;
217                 int line = part_no / 3;
218                 int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
219
220                 u32 *dst = &frame[part_index * USBTV_CHUNK/2];
221                 memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
222                 src += USBTV_CHUNK/2;
223         }
224 }
225
226 /* Called for each 256-byte image chunk.
227  * First word identifies the chunk, followed by 240 words of image
228  * data and padding. */
229 static void usbtv_image_chunk(struct usbtv *usbtv, u32 *chunk)
230 {
231         int frame_id, odd, chunk_no;
232         u32 *frame;
233         struct usbtv_buf *buf;
234         unsigned long flags;
235
236         /* Ignore corrupted lines. */
237         if (!USBTV_MAGIC_OK(chunk))
238                 return;
239         frame_id = USBTV_FRAME_ID(chunk);
240         odd = USBTV_ODD(chunk);
241         chunk_no = USBTV_CHUNK_NO(chunk);
242         if (chunk_no >= USBTV_CHUNKS)
243                 return;
244
245         /* Beginning of a frame. */
246         if (chunk_no == 0) {
247                 usbtv->frame_id = frame_id;
248                 usbtv->chunks_done = 0;
249         }
250
251         if (usbtv->frame_id != frame_id)
252                 return;
253
254         spin_lock_irqsave(&usbtv->buflock, flags);
255         if (list_empty(&usbtv->bufs)) {
256                 /* No free buffers. Userspace likely too slow. */
257                 spin_unlock_irqrestore(&usbtv->buflock, flags);
258                 return;
259         }
260
261         /* First available buffer. */
262         buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
263         frame = vb2_plane_vaddr(&buf->vb, 0);
264
265         /* Copy the chunk data. */
266         usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
267         usbtv->chunks_done++;
268
269         /* Last chunk in a frame, signalling an end */
270         if (odd && chunk_no == USBTV_CHUNKS-1) {
271                 int size = vb2_plane_size(&buf->vb, 0);
272                 enum vb2_buffer_state state = usbtv->chunks_done ==
273                                                 USBTV_CHUNKS ?
274                                                 VB2_BUF_STATE_DONE :
275                                                 VB2_BUF_STATE_ERROR;
276
277                 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
278                 buf->vb.v4l2_buf.sequence = usbtv->sequence++;
279                 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
280                 vb2_set_plane_payload(&buf->vb, 0, size);
281                 vb2_buffer_done(&buf->vb, state);
282                 list_del(&buf->list);
283         }
284
285         spin_unlock_irqrestore(&usbtv->buflock, flags);
286 }
287
288 /* Got image data. Each packet contains a number of 256-word chunks we
289  * compose the image from. */
290 static void usbtv_iso_cb(struct urb *ip)
291 {
292         int ret;
293         int i;
294         struct usbtv *usbtv = (struct usbtv *)ip->context;
295
296         switch (ip->status) {
297         /* All fine. */
298         case 0:
299                 break;
300         /* Device disconnected or capture stopped? */
301         case -ENODEV:
302         case -ENOENT:
303         case -ECONNRESET:
304         case -ESHUTDOWN:
305                 return;
306         /* Unknown error. Retry. */
307         default:
308                 dev_warn(usbtv->dev, "Bad response for ISO request.\n");
309                 goto resubmit;
310         }
311
312         for (i = 0; i < ip->number_of_packets; i++) {
313                 int size = ip->iso_frame_desc[i].actual_length;
314                 unsigned char *data = ip->transfer_buffer +
315                                 ip->iso_frame_desc[i].offset;
316                 int offset;
317
318                 for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
319                         usbtv_image_chunk(usbtv,
320                                 (u32 *)&data[USBTV_CHUNK_SIZE * offset]);
321         }
322
323 resubmit:
324         ret = usb_submit_urb(ip, GFP_ATOMIC);
325         if (ret < 0)
326                 dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
327 }
328
329 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
330 {
331         struct urb *ip;
332         int size = usbtv->iso_size;
333         int i;
334
335         ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
336         if (ip == NULL)
337                 return NULL;
338
339         ip->dev = usbtv->udev;
340         ip->context = usbtv;
341         ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
342         ip->interval = 1;
343         ip->transfer_flags = URB_ISO_ASAP;
344         ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
345                                                 GFP_KERNEL);
346         ip->complete = usbtv_iso_cb;
347         ip->number_of_packets = USBTV_ISOC_PACKETS;
348         ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
349         for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
350                 ip->iso_frame_desc[i].offset = size * i;
351                 ip->iso_frame_desc[i].length = size;
352         }
353
354         return ip;
355 }
356
357 static void usbtv_stop(struct usbtv *usbtv)
358 {
359         int i;
360         unsigned long flags;
361
362         /* Cancel running transfers. */
363         for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
364                 struct urb *ip = usbtv->isoc_urbs[i];
365                 if (ip == NULL)
366                         continue;
367                 usb_kill_urb(ip);
368                 kfree(ip->transfer_buffer);
369                 usb_free_urb(ip);
370                 usbtv->isoc_urbs[i] = NULL;
371         }
372
373         /* Return buffers to userspace. */
374         spin_lock_irqsave(&usbtv->buflock, flags);
375         while (!list_empty(&usbtv->bufs)) {
376                 struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
377                                                 struct usbtv_buf, list);
378                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
379                 list_del(&buf->list);
380         }
381         spin_unlock_irqrestore(&usbtv->buflock, flags);
382 }
383
384 static int usbtv_start(struct usbtv *usbtv)
385 {
386         int i;
387         int ret;
388
389         ret = usb_set_interface(usbtv->udev, 0, 0);
390         if (ret < 0)
391                 return ret;
392
393         ret = usbtv_setup_capture(usbtv);
394         if (ret < 0)
395                 return ret;
396
397         ret = usb_set_interface(usbtv->udev, 0, 1);
398         if (ret < 0)
399                 return ret;
400
401         for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
402                 struct urb *ip;
403
404                 ip = usbtv_setup_iso_transfer(usbtv);
405                 if (ip == NULL) {
406                         ret = -ENOMEM;
407                         goto start_fail;
408                 }
409                 usbtv->isoc_urbs[i] = ip;
410
411                 ret = usb_submit_urb(ip, GFP_KERNEL);
412                 if (ret < 0)
413                         goto start_fail;
414         }
415
416         return 0;
417
418 start_fail:
419         usbtv_stop(usbtv);
420         return ret;
421 }
422
423 struct usb_device_id usbtv_id_table[] = {
424         { USB_DEVICE(0x1b71, 0x3002) },
425         {}
426 };
427 MODULE_DEVICE_TABLE(usb, usbtv_id_table);
428
429 static int usbtv_querycap(struct file *file, void *priv,
430                                 struct v4l2_capability *cap)
431 {
432         struct usbtv *dev = video_drvdata(file);
433
434         strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
435         strlcpy(cap->card, "usbtv", sizeof(cap->card));
436         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
437         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
438         cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
439         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
440         return 0;
441 }
442
443 static int usbtv_enum_input(struct file *file, void *priv,
444                                         struct v4l2_input *i)
445 {
446         if (i->index > 0)
447                 return -EINVAL;
448
449         strlcpy(i->name, "Composite", sizeof(i->name));
450         i->type = V4L2_INPUT_TYPE_CAMERA;
451         i->std = V4L2_STD_525_60;
452         return 0;
453 }
454
455 static int usbtv_enum_fmt_vid_cap(struct file *file, void  *priv,
456                                         struct v4l2_fmtdesc *f)
457 {
458         if (f->index > 0)
459                 return -EINVAL;
460
461         strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
462                                         sizeof(f->description));
463         f->pixelformat = V4L2_PIX_FMT_YUYV;
464         return 0;
465 }
466
467 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
468                                         struct v4l2_format *f)
469 {
470         f->fmt.pix.width = USBTV_WIDTH;
471         f->fmt.pix.height = USBTV_HEIGHT;
472         f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
473         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
474         f->fmt.pix.bytesperline = USBTV_WIDTH * 2;
475         f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
476         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
477         f->fmt.pix.priv = 0;
478         return 0;
479 }
480
481 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
482 {
483         *norm = V4L2_STD_525_60;
484         return 0;
485 }
486
487 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
488 {
489         *i = 0;
490         return 0;
491 }
492
493 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
494 {
495         if (i > 0)
496                 return -EINVAL;
497         return 0;
498 }
499
500 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
501 {
502         if (norm & V4L2_STD_525_60)
503                 return 0;
504         return -EINVAL;
505 }
506
507 struct v4l2_ioctl_ops usbtv_ioctl_ops = {
508         .vidioc_querycap = usbtv_querycap,
509         .vidioc_enum_input = usbtv_enum_input,
510         .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
511         .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
512         .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
513         .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
514         .vidioc_g_std = usbtv_g_std,
515         .vidioc_s_std = usbtv_s_std,
516         .vidioc_g_input = usbtv_g_input,
517         .vidioc_s_input = usbtv_s_input,
518
519         .vidioc_reqbufs = vb2_ioctl_reqbufs,
520         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
521         .vidioc_querybuf = vb2_ioctl_querybuf,
522         .vidioc_create_bufs = vb2_ioctl_create_bufs,
523         .vidioc_qbuf = vb2_ioctl_qbuf,
524         .vidioc_dqbuf = vb2_ioctl_dqbuf,
525         .vidioc_streamon = vb2_ioctl_streamon,
526         .vidioc_streamoff = vb2_ioctl_streamoff,
527 };
528
529 struct v4l2_file_operations usbtv_fops = {
530         .owner = THIS_MODULE,
531         .unlocked_ioctl = video_ioctl2,
532         .mmap = vb2_fop_mmap,
533         .open = v4l2_fh_open,
534         .release = vb2_fop_release,
535         .read = vb2_fop_read,
536         .poll = vb2_fop_poll,
537 };
538
539 static int usbtv_queue_setup(struct vb2_queue *vq,
540         const struct v4l2_format *v4l_fmt, unsigned int *nbuffers,
541         unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
542 {
543         if (*nbuffers < 2)
544                 *nbuffers = 2;
545         *nplanes = 1;
546         sizes[0] = USBTV_WIDTH * USBTV_HEIGHT / 2 * sizeof(u32);
547
548         return 0;
549 }
550
551 static void usbtv_buf_queue(struct vb2_buffer *vb)
552 {
553         struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
554         struct usbtv_buf *buf = container_of(vb, struct usbtv_buf, vb);
555         unsigned long flags;
556
557         if (usbtv->udev == NULL) {
558                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
559                 return;
560         }
561
562         spin_lock_irqsave(&usbtv->buflock, flags);
563         list_add_tail(&buf->list, &usbtv->bufs);
564         spin_unlock_irqrestore(&usbtv->buflock, flags);
565 }
566
567 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
568 {
569         struct usbtv *usbtv = vb2_get_drv_priv(vq);
570
571         if (usbtv->udev == NULL)
572                 return -ENODEV;
573
574         return usbtv_start(usbtv);
575 }
576
577 static int usbtv_stop_streaming(struct vb2_queue *vq)
578 {
579         struct usbtv *usbtv = vb2_get_drv_priv(vq);
580
581         if (usbtv->udev == NULL)
582                 return -ENODEV;
583
584         usbtv_stop(usbtv);
585         return 0;
586 }
587
588 struct vb2_ops usbtv_vb2_ops = {
589         .queue_setup = usbtv_queue_setup,
590         .buf_queue = usbtv_buf_queue,
591         .start_streaming = usbtv_start_streaming,
592         .stop_streaming = usbtv_stop_streaming,
593 };
594
595 static void usbtv_release(struct v4l2_device *v4l2_dev)
596 {
597         struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
598
599         v4l2_device_unregister(&usbtv->v4l2_dev);
600         vb2_queue_release(&usbtv->vb2q);
601         kfree(usbtv);
602 }
603
604 static int usbtv_probe(struct usb_interface *intf,
605         const struct usb_device_id *id)
606 {
607         int ret;
608         int size;
609         struct device *dev = &intf->dev;
610         struct usbtv *usbtv;
611
612         /* Checks that the device is what we think it is. */
613         if (intf->num_altsetting != 2)
614                 return -ENODEV;
615         if (intf->altsetting[1].desc.bNumEndpoints != 4)
616                 return -ENODEV;
617
618         /* Packet size is split into 11 bits of base size and count of
619          * extra multiplies of it.*/
620         size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
621         size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
622
623         /* Device structure */
624         usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
625         if (usbtv == NULL)
626                 return -ENOMEM;
627         usbtv->dev = dev;
628         usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
629         usbtv->iso_size = size;
630         spin_lock_init(&usbtv->buflock);
631         mutex_init(&usbtv->v4l2_lock);
632         mutex_init(&usbtv->vb2q_lock);
633         INIT_LIST_HEAD(&usbtv->bufs);
634
635         /* videobuf2 structure */
636         usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
637         usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
638         usbtv->vb2q.drv_priv = usbtv;
639         usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
640         usbtv->vb2q.ops = &usbtv_vb2_ops;
641         usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
642         usbtv->vb2q.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
643         usbtv->vb2q.lock = &usbtv->vb2q_lock;
644         ret = vb2_queue_init(&usbtv->vb2q);
645         if (ret < 0) {
646                 dev_warn(dev, "Could not initialize videobuf2 queue\n");
647                 goto usbtv_fail;
648         }
649
650         /* v4l2 structure */
651         usbtv->v4l2_dev.release = usbtv_release;
652         ret = v4l2_device_register(dev, &usbtv->v4l2_dev);
653         if (ret < 0) {
654                 dev_warn(dev, "Could not register v4l2 device\n");
655                 goto v4l2_fail;
656         }
657
658         usb_set_intfdata(intf, usbtv);
659
660         /* Video structure */
661         strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
662         usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
663         usbtv->vdev.release = video_device_release_empty;
664         usbtv->vdev.fops = &usbtv_fops;
665         usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
666         usbtv->vdev.tvnorms = V4L2_STD_525_60;
667         usbtv->vdev.queue = &usbtv->vb2q;
668         usbtv->vdev.lock = &usbtv->v4l2_lock;
669         set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags);
670         video_set_drvdata(&usbtv->vdev, usbtv);
671         ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
672         if (ret < 0) {
673                 dev_warn(dev, "Could not register video device\n");
674                 goto vdev_fail;
675         }
676
677         dev_info(dev, "Fushicai USBTV007 Video Grabber\n");
678         return 0;
679
680 vdev_fail:
681         v4l2_device_unregister(&usbtv->v4l2_dev);
682 v4l2_fail:
683         vb2_queue_release(&usbtv->vb2q);
684 usbtv_fail:
685         kfree(usbtv);
686
687         return ret;
688 }
689
690 static void usbtv_disconnect(struct usb_interface *intf)
691 {
692         struct usbtv *usbtv = usb_get_intfdata(intf);
693
694         mutex_lock(&usbtv->vb2q_lock);
695         mutex_lock(&usbtv->v4l2_lock);
696
697         usbtv_stop(usbtv);
698         usb_set_intfdata(intf, NULL);
699         video_unregister_device(&usbtv->vdev);
700         v4l2_device_disconnect(&usbtv->v4l2_dev);
701         usb_put_dev(usbtv->udev);
702         usbtv->udev = NULL;
703
704         mutex_unlock(&usbtv->v4l2_lock);
705         mutex_unlock(&usbtv->vb2q_lock);
706
707         v4l2_device_put(&usbtv->v4l2_dev);
708 }
709
710 MODULE_AUTHOR("Lubomir Rintel");
711 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver");
712 MODULE_LICENSE("Dual BSD/GPL");
713
714 struct usb_driver usbtv_usb_driver = {
715         .name = "usbtv",
716         .id_table = usbtv_id_table,
717         .probe = usbtv_probe,
718         .disconnect = usbtv_disconnect,
719 };
720
721 module_usb_driver(usbtv_usb_driver);