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