]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/usb/usbtv/usbtv-video.c
Merge remote-tracking branch 'usb/usb-next'
[karo-tx-linux.git] / drivers / media / usb / usbtv / usbtv-video.c
1 /*
2  * Fushicai USBTV007 Audio-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 <media/v4l2-ioctl.h>
32 #include <media/videobuf2-v4l2.h>
33
34 #include "usbtv.h"
35
36 static struct usbtv_norm_params norm_params[] = {
37         {
38                 .norm = V4L2_STD_525_60,
39                 .cap_width = 720,
40                 .cap_height = 480,
41         },
42         {
43                 .norm = V4L2_STD_PAL,
44                 .cap_width = 720,
45                 .cap_height = 576,
46         }
47 };
48
49 static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
50 {
51         int i, ret = 0;
52         struct usbtv_norm_params *params = NULL;
53
54         for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
55                 if (norm_params[i].norm & norm) {
56                         params = &norm_params[i];
57                         break;
58                 }
59         }
60
61         if (params) {
62                 usbtv->width = params->cap_width;
63                 usbtv->height = params->cap_height;
64                 usbtv->n_chunks = usbtv->width * usbtv->height
65                                                 / 4 / USBTV_CHUNK;
66                 usbtv->norm = params->norm;
67         } else
68                 ret = -EINVAL;
69
70         return ret;
71 }
72
73 static int usbtv_select_input(struct usbtv *usbtv, int input)
74 {
75         int ret;
76
77         static const u16 composite[][2] = {
78                 { USBTV_BASE + 0x0105, 0x0060 },
79                 { USBTV_BASE + 0x011f, 0x00f2 },
80                 { USBTV_BASE + 0x0127, 0x0060 },
81                 { USBTV_BASE + 0x00ae, 0x0010 },
82                 { USBTV_BASE + 0x0239, 0x0060 },
83         };
84
85         static const u16 svideo[][2] = {
86                 { USBTV_BASE + 0x0105, 0x0010 },
87                 { USBTV_BASE + 0x011f, 0x00ff },
88                 { USBTV_BASE + 0x0127, 0x0060 },
89                 { USBTV_BASE + 0x00ae, 0x0030 },
90                 { USBTV_BASE + 0x0239, 0x0060 },
91         };
92
93         switch (input) {
94         case USBTV_COMPOSITE_INPUT:
95                 ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
96                 break;
97         case USBTV_SVIDEO_INPUT:
98                 ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
99                 break;
100         default:
101                 ret = -EINVAL;
102         }
103
104         if (!ret)
105                 usbtv->input = input;
106
107         return ret;
108 }
109
110 static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
111 {
112         int ret;
113         static const u16 pal[][2] = {
114                 { USBTV_BASE + 0x001a, 0x0068 },
115                 { USBTV_BASE + 0x010e, 0x0072 },
116                 { USBTV_BASE + 0x010f, 0x00a2 },
117                 { USBTV_BASE + 0x0112, 0x00b0 },
118                 { USBTV_BASE + 0x0117, 0x0001 },
119                 { USBTV_BASE + 0x0118, 0x002c },
120                 { USBTV_BASE + 0x012d, 0x0010 },
121                 { USBTV_BASE + 0x012f, 0x0020 },
122                 { USBTV_BASE + 0x024f, 0x0002 },
123                 { USBTV_BASE + 0x0254, 0x0059 },
124                 { USBTV_BASE + 0x025a, 0x0016 },
125                 { USBTV_BASE + 0x025b, 0x0035 },
126                 { USBTV_BASE + 0x0263, 0x0017 },
127                 { USBTV_BASE + 0x0266, 0x0016 },
128                 { USBTV_BASE + 0x0267, 0x0036 }
129         };
130
131         static const u16 ntsc[][2] = {
132                 { USBTV_BASE + 0x001a, 0x0079 },
133                 { USBTV_BASE + 0x010e, 0x0068 },
134                 { USBTV_BASE + 0x010f, 0x009c },
135                 { USBTV_BASE + 0x0112, 0x00f0 },
136                 { USBTV_BASE + 0x0117, 0x0000 },
137                 { USBTV_BASE + 0x0118, 0x00fc },
138                 { USBTV_BASE + 0x012d, 0x0004 },
139                 { USBTV_BASE + 0x012f, 0x0008 },
140                 { USBTV_BASE + 0x024f, 0x0001 },
141                 { USBTV_BASE + 0x0254, 0x005f },
142                 { USBTV_BASE + 0x025a, 0x0012 },
143                 { USBTV_BASE + 0x025b, 0x0001 },
144                 { USBTV_BASE + 0x0263, 0x001c },
145                 { USBTV_BASE + 0x0266, 0x0011 },
146                 { USBTV_BASE + 0x0267, 0x0005 }
147         };
148
149         ret = usbtv_configure_for_norm(usbtv, norm);
150
151         if (!ret) {
152                 if (norm & V4L2_STD_525_60)
153                         ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
154                 else if (norm & V4L2_STD_PAL)
155                         ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
156         }
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 + 0x0003, 0x0004 },
227                 { USBTV_BASE + 0x0100, 0x00d3 },
228                 { USBTV_BASE + 0x0115, 0x0015 },
229                 { USBTV_BASE + 0x0220, 0x002e },
230                 { USBTV_BASE + 0x0225, 0x0008 },
231                 { USBTV_BASE + 0x024e, 0x0002 },
232                 { USBTV_BASE + 0x024e, 0x0002 },
233                 { USBTV_BASE + 0x024f, 0x0002 },
234         };
235
236         ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
237         if (ret)
238                 return ret;
239
240         ret = usbtv_select_norm(usbtv, usbtv->norm);
241         if (ret)
242                 return ret;
243
244         ret = usbtv_select_input(usbtv, usbtv->input);
245         if (ret)
246                 return ret;
247
248         return 0;
249 }
250
251 /* Copy data from chunk into a frame buffer, deinterlacing the data
252  * into every second line. Unfortunately, they don't align nicely into
253  * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
254  * Therefore, we break down the chunk into two halves before copyting,
255  * so that we can interleave a line if needed. */
256 static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
257 {
258         int half;
259
260         for (half = 0; half < 2; half++) {
261                 int part_no = chunk_no * 2 + half;
262                 int line = part_no / 3;
263                 int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
264
265                 u32 *dst = &frame[part_index * USBTV_CHUNK/2];
266
267                 memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
268                 src += USBTV_CHUNK/2;
269         }
270 }
271
272 /* Called for each 256-byte image chunk.
273  * First word identifies the chunk, followed by 240 words of image
274  * data and padding. */
275 static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
276 {
277         int frame_id, odd, chunk_no;
278         u32 *frame;
279         struct usbtv_buf *buf;
280         unsigned long flags;
281
282         /* Ignore corrupted lines. */
283         if (!USBTV_MAGIC_OK(chunk))
284                 return;
285         frame_id = USBTV_FRAME_ID(chunk);
286         odd = USBTV_ODD(chunk);
287         chunk_no = USBTV_CHUNK_NO(chunk);
288         if (chunk_no >= usbtv->n_chunks)
289                 return;
290
291         /* Beginning of a frame. */
292         if (chunk_no == 0) {
293                 usbtv->frame_id = frame_id;
294                 usbtv->chunks_done = 0;
295         }
296
297         if (usbtv->frame_id != frame_id)
298                 return;
299
300         spin_lock_irqsave(&usbtv->buflock, flags);
301         if (list_empty(&usbtv->bufs)) {
302                 /* No free buffers. Userspace likely too slow. */
303                 spin_unlock_irqrestore(&usbtv->buflock, flags);
304                 return;
305         }
306
307         /* First available buffer. */
308         buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
309         frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
310
311         /* Copy the chunk data. */
312         usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
313         usbtv->chunks_done++;
314
315         /* Last chunk in a field */
316         if (chunk_no == usbtv->n_chunks-1) {
317                 /* Last chunk in a frame, signalling an end */
318                 if (odd && !usbtv->last_odd) {
319                         int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
320                         enum vb2_buffer_state state = usbtv->chunks_done ==
321                                 usbtv->n_chunks ?
322                                 VB2_BUF_STATE_DONE :
323                                 VB2_BUF_STATE_ERROR;
324
325                         buf->vb.field = V4L2_FIELD_INTERLACED;
326                         buf->vb.sequence = usbtv->sequence++;
327                         buf->vb.vb2_buf.timestamp = ktime_get_ns();
328                         vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
329                         vb2_buffer_done(&buf->vb.vb2_buf, state);
330                         list_del(&buf->list);
331                 }
332                 usbtv->last_odd = odd;
333         }
334
335         spin_unlock_irqrestore(&usbtv->buflock, flags);
336 }
337
338 /* Got image data. Each packet contains a number of 256-word chunks we
339  * compose the image from. */
340 static void usbtv_iso_cb(struct urb *ip)
341 {
342         int ret;
343         int i;
344         struct usbtv *usbtv = (struct usbtv *)ip->context;
345
346         switch (ip->status) {
347         /* All fine. */
348         case 0:
349                 break;
350         /* Device disconnected or capture stopped? */
351         case -ENODEV:
352         case -ENOENT:
353         case -ECONNRESET:
354         case -ESHUTDOWN:
355                 return;
356         /* Unknown error. Retry. */
357         default:
358                 dev_warn(usbtv->dev, "Bad response for ISO request.\n");
359                 goto resubmit;
360         }
361
362         for (i = 0; i < ip->number_of_packets; i++) {
363                 int size = ip->iso_frame_desc[i].actual_length;
364                 unsigned char *data = ip->transfer_buffer +
365                                 ip->iso_frame_desc[i].offset;
366                 int offset;
367
368                 for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
369                         usbtv_image_chunk(usbtv,
370                                 (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
371         }
372
373 resubmit:
374         ret = usb_submit_urb(ip, GFP_ATOMIC);
375         if (ret < 0)
376                 dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
377 }
378
379 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
380 {
381         struct urb *ip;
382         int size = usbtv->iso_size;
383         int i;
384
385         ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
386         if (ip == NULL)
387                 return NULL;
388
389         ip->dev = usbtv->udev;
390         ip->context = usbtv;
391         ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
392         ip->interval = 1;
393         ip->transfer_flags = URB_ISO_ASAP;
394         ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
395                                                 GFP_KERNEL);
396         if (!ip->transfer_buffer) {
397                 usb_free_urb(ip);
398                 return NULL;
399         }
400         ip->complete = usbtv_iso_cb;
401         ip->number_of_packets = USBTV_ISOC_PACKETS;
402         ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
403         for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
404                 ip->iso_frame_desc[i].offset = size * i;
405                 ip->iso_frame_desc[i].length = size;
406         }
407
408         return ip;
409 }
410
411 static void usbtv_stop(struct usbtv *usbtv)
412 {
413         int i;
414         unsigned long flags;
415
416         /* Cancel running transfers. */
417         for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
418                 struct urb *ip = usbtv->isoc_urbs[i];
419
420                 if (ip == NULL)
421                         continue;
422                 usb_kill_urb(ip);
423                 kfree(ip->transfer_buffer);
424                 usb_free_urb(ip);
425                 usbtv->isoc_urbs[i] = NULL;
426         }
427
428         /* Return buffers to userspace. */
429         spin_lock_irqsave(&usbtv->buflock, flags);
430         while (!list_empty(&usbtv->bufs)) {
431                 struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
432                                                 struct usbtv_buf, list);
433                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
434                 list_del(&buf->list);
435         }
436         spin_unlock_irqrestore(&usbtv->buflock, flags);
437 }
438
439 static int usbtv_start(struct usbtv *usbtv)
440 {
441         int i;
442         int ret;
443
444         usbtv_audio_suspend(usbtv);
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         usbtv_audio_resume(usbtv);
459
460         for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
461                 struct urb *ip;
462
463                 ip = usbtv_setup_iso_transfer(usbtv);
464                 if (ip == NULL) {
465                         ret = -ENOMEM;
466                         goto start_fail;
467                 }
468                 usbtv->isoc_urbs[i] = ip;
469
470                 ret = usb_submit_urb(ip, GFP_KERNEL);
471                 if (ret < 0)
472                         goto start_fail;
473         }
474
475         return 0;
476
477 start_fail:
478         usbtv_stop(usbtv);
479         return ret;
480 }
481
482 static int usbtv_querycap(struct file *file, void *priv,
483                                 struct v4l2_capability *cap)
484 {
485         struct usbtv *dev = video_drvdata(file);
486
487         strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
488         strlcpy(cap->card, "usbtv", sizeof(cap->card));
489         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
490         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
491         cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
492         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
493         return 0;
494 }
495
496 static int usbtv_enum_input(struct file *file, void *priv,
497                                         struct v4l2_input *i)
498 {
499         struct usbtv *dev = video_drvdata(file);
500
501         switch (i->index) {
502         case USBTV_COMPOSITE_INPUT:
503                 strlcpy(i->name, "Composite", sizeof(i->name));
504                 break;
505         case USBTV_SVIDEO_INPUT:
506                 strlcpy(i->name, "S-Video", sizeof(i->name));
507                 break;
508         default:
509                 return -EINVAL;
510         }
511
512         i->type = V4L2_INPUT_TYPE_CAMERA;
513         i->std = dev->vdev.tvnorms;
514         return 0;
515 }
516
517 static int usbtv_enum_fmt_vid_cap(struct file *file, void  *priv,
518                                         struct v4l2_fmtdesc *f)
519 {
520         if (f->index > 0)
521                 return -EINVAL;
522
523         strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
524                                         sizeof(f->description));
525         f->pixelformat = V4L2_PIX_FMT_YUYV;
526         return 0;
527 }
528
529 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
530                                         struct v4l2_format *f)
531 {
532         struct usbtv *usbtv = video_drvdata(file);
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
542         return 0;
543 }
544
545 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
546 {
547         struct usbtv *usbtv = video_drvdata(file);
548         *norm = usbtv->norm;
549         return 0;
550 }
551
552 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
553 {
554         int ret = -EINVAL;
555         struct usbtv *usbtv = video_drvdata(file);
556
557         if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
558                 ret = usbtv_select_norm(usbtv, norm);
559
560         return ret;
561 }
562
563 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
564 {
565         struct usbtv *usbtv = video_drvdata(file);
566         *i = usbtv->input;
567         return 0;
568 }
569
570 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
571 {
572         struct usbtv *usbtv = video_drvdata(file);
573
574         return usbtv_select_input(usbtv, i);
575 }
576
577 static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
578         .vidioc_querycap = usbtv_querycap,
579         .vidioc_enum_input = usbtv_enum_input,
580         .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
581         .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
582         .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
583         .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
584         .vidioc_g_std = usbtv_g_std,
585         .vidioc_s_std = usbtv_s_std,
586         .vidioc_g_input = usbtv_g_input,
587         .vidioc_s_input = usbtv_s_input,
588
589         .vidioc_reqbufs = vb2_ioctl_reqbufs,
590         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
591         .vidioc_querybuf = vb2_ioctl_querybuf,
592         .vidioc_create_bufs = vb2_ioctl_create_bufs,
593         .vidioc_qbuf = vb2_ioctl_qbuf,
594         .vidioc_dqbuf = vb2_ioctl_dqbuf,
595         .vidioc_streamon = vb2_ioctl_streamon,
596         .vidioc_streamoff = vb2_ioctl_streamoff,
597 };
598
599 static struct v4l2_file_operations usbtv_fops = {
600         .owner = THIS_MODULE,
601         .unlocked_ioctl = video_ioctl2,
602         .mmap = vb2_fop_mmap,
603         .open = v4l2_fh_open,
604         .release = vb2_fop_release,
605         .read = vb2_fop_read,
606         .poll = vb2_fop_poll,
607 };
608
609 static int usbtv_queue_setup(struct vb2_queue *vq,
610         unsigned int *nbuffers,
611         unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
612 {
613         struct usbtv *usbtv = vb2_get_drv_priv(vq);
614         unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
615
616         if (vq->num_buffers + *nbuffers < 2)
617                 *nbuffers = 2 - vq->num_buffers;
618         if (*nplanes)
619                 return sizes[0] < size ? -EINVAL : 0;
620         *nplanes = 1;
621         sizes[0] = size;
622
623         return 0;
624 }
625
626 static void usbtv_buf_queue(struct vb2_buffer *vb)
627 {
628         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
629         struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
630         struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
631         unsigned long flags;
632
633         if (usbtv->udev == NULL) {
634                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
635                 return;
636         }
637
638         spin_lock_irqsave(&usbtv->buflock, flags);
639         list_add_tail(&buf->list, &usbtv->bufs);
640         spin_unlock_irqrestore(&usbtv->buflock, flags);
641 }
642
643 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
644 {
645         struct usbtv *usbtv = vb2_get_drv_priv(vq);
646
647         if (usbtv->udev == NULL)
648                 return -ENODEV;
649
650         usbtv->last_odd = 1;
651         usbtv->sequence = 0;
652         return usbtv_start(usbtv);
653 }
654
655 static void usbtv_stop_streaming(struct vb2_queue *vq)
656 {
657         struct usbtv *usbtv = vb2_get_drv_priv(vq);
658
659         if (usbtv->udev)
660                 usbtv_stop(usbtv);
661 }
662
663 static struct vb2_ops usbtv_vb2_ops = {
664         .queue_setup = usbtv_queue_setup,
665         .buf_queue = usbtv_buf_queue,
666         .start_streaming = usbtv_start_streaming,
667         .stop_streaming = usbtv_stop_streaming,
668 };
669
670 static void usbtv_release(struct v4l2_device *v4l2_dev)
671 {
672         struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
673
674         v4l2_device_unregister(&usbtv->v4l2_dev);
675         vb2_queue_release(&usbtv->vb2q);
676         kfree(usbtv);
677 }
678
679 int usbtv_video_init(struct usbtv *usbtv)
680 {
681         int ret;
682
683         (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
684
685         spin_lock_init(&usbtv->buflock);
686         mutex_init(&usbtv->v4l2_lock);
687         mutex_init(&usbtv->vb2q_lock);
688         INIT_LIST_HEAD(&usbtv->bufs);
689
690         /* videobuf2 structure */
691         usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
692         usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
693         usbtv->vb2q.drv_priv = usbtv;
694         usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
695         usbtv->vb2q.ops = &usbtv_vb2_ops;
696         usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
697         usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
698         usbtv->vb2q.lock = &usbtv->vb2q_lock;
699         ret = vb2_queue_init(&usbtv->vb2q);
700         if (ret < 0) {
701                 dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
702                 return ret;
703         }
704
705         /* v4l2 structure */
706         usbtv->v4l2_dev.release = usbtv_release;
707         ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
708         if (ret < 0) {
709                 dev_warn(usbtv->dev, "Could not register v4l2 device\n");
710                 goto v4l2_fail;
711         }
712
713         /* Video structure */
714         strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
715         usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
716         usbtv->vdev.release = video_device_release_empty;
717         usbtv->vdev.fops = &usbtv_fops;
718         usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
719         usbtv->vdev.tvnorms = USBTV_TV_STD;
720         usbtv->vdev.queue = &usbtv->vb2q;
721         usbtv->vdev.lock = &usbtv->v4l2_lock;
722         video_set_drvdata(&usbtv->vdev, usbtv);
723         ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
724         if (ret < 0) {
725                 dev_warn(usbtv->dev, "Could not register video device\n");
726                 goto vdev_fail;
727         }
728
729         return 0;
730
731 vdev_fail:
732         v4l2_device_unregister(&usbtv->v4l2_dev);
733 v4l2_fail:
734         vb2_queue_release(&usbtv->vb2q);
735
736         return ret;
737 }
738
739 void usbtv_video_free(struct usbtv *usbtv)
740 {
741         mutex_lock(&usbtv->vb2q_lock);
742         mutex_lock(&usbtv->v4l2_lock);
743
744         usbtv_stop(usbtv);
745         video_unregister_device(&usbtv->vdev);
746         v4l2_device_disconnect(&usbtv->v4l2_dev);
747
748         mutex_unlock(&usbtv->v4l2_lock);
749         mutex_unlock(&usbtv->vb2q_lock);
750
751         v4l2_device_put(&usbtv->v4l2_dev);
752 }