]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/media/go7007/go7007-v4l2.c
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[karo-tx-linux.git] / drivers / staging / media / go7007 / go7007-v4l2.c
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16  */
17
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/sched.h>
21 #include <linux/spinlock.h>
22 #include <linux/slab.h>
23 #include <linux/fs.h>
24 #include <linux/unistd.h>
25 #include <linux/time.h>
26 #include <linux/vmalloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/i2c.h>
29 #include <linux/mutex.h>
30 #include <linux/uaccess.h>
31 #include <linux/videodev2.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-subdev.h>
35 #include <media/v4l2-event.h>
36 #include <media/videobuf2-vmalloc.h>
37 #include <media/saa7115.h>
38
39 #include "go7007.h"
40 #include "go7007-priv.h"
41
42 #define call_all(dev, o, f, args...) \
43         v4l2_device_call_until_err(dev, 0, o, f, ##args)
44
45 static bool valid_pixelformat(u32 pixelformat)
46 {
47         switch (pixelformat) {
48         case V4L2_PIX_FMT_MJPEG:
49         case V4L2_PIX_FMT_MPEG1:
50         case V4L2_PIX_FMT_MPEG2:
51         case V4L2_PIX_FMT_MPEG4:
52                 return true;
53         default:
54                 return false;
55         }
56 }
57
58 static u32 get_frame_type_flag(struct go7007_buffer *vb, int format)
59 {
60         u8 *ptr = vb2_plane_vaddr(&vb->vb, 0);
61
62         switch (format) {
63         case V4L2_PIX_FMT_MJPEG:
64                 return V4L2_BUF_FLAG_KEYFRAME;
65         case V4L2_PIX_FMT_MPEG4:
66                 switch ((ptr[vb->frame_offset + 4] >> 6) & 0x3) {
67                 case 0:
68                         return V4L2_BUF_FLAG_KEYFRAME;
69                 case 1:
70                         return V4L2_BUF_FLAG_PFRAME;
71                 case 2:
72                         return V4L2_BUF_FLAG_BFRAME;
73                 default:
74                         return 0;
75                 }
76         case V4L2_PIX_FMT_MPEG1:
77         case V4L2_PIX_FMT_MPEG2:
78                 switch ((ptr[vb->frame_offset + 5] >> 3) & 0x7) {
79                 case 1:
80                         return V4L2_BUF_FLAG_KEYFRAME;
81                 case 2:
82                         return V4L2_BUF_FLAG_PFRAME;
83                 case 3:
84                         return V4L2_BUF_FLAG_BFRAME;
85                 default:
86                         return 0;
87                 }
88         }
89
90         return 0;
91 }
92
93 static void get_resolution(struct go7007 *go, int *width, int *height)
94 {
95         switch (go->standard) {
96         case GO7007_STD_NTSC:
97                 *width = 720;
98                 *height = 480;
99                 break;
100         case GO7007_STD_PAL:
101                 *width = 720;
102                 *height = 576;
103                 break;
104         case GO7007_STD_OTHER:
105         default:
106                 *width = go->board_info->sensor_width;
107                 *height = go->board_info->sensor_height;
108                 break;
109         }
110 }
111
112 static void set_formatting(struct go7007 *go)
113 {
114         if (go->format == V4L2_PIX_FMT_MJPEG) {
115                 go->pali = 0;
116                 go->aspect_ratio = GO7007_RATIO_1_1;
117                 go->gop_size = 0;
118                 go->ipb = 0;
119                 go->closed_gop = 0;
120                 go->repeat_seqhead = 0;
121                 go->seq_header_enable = 0;
122                 go->gop_header_enable = 0;
123                 go->dvd_mode = 0;
124                 return;
125         }
126
127         switch (go->format) {
128         case V4L2_PIX_FMT_MPEG1:
129                 go->pali = 0;
130                 break;
131         default:
132         case V4L2_PIX_FMT_MPEG2:
133                 go->pali = 0x48;
134                 break;
135         case V4L2_PIX_FMT_MPEG4:
136                 /* For future reference: this is the list of MPEG4
137                  * profiles that are available, although they are
138                  * untested:
139                  *
140                  * Profile              pali
141                  * --------------       ----
142                  * PROFILE_S_L0         0x08
143                  * PROFILE_S_L1         0x01
144                  * PROFILE_S_L2         0x02
145                  * PROFILE_S_L3         0x03
146                  * PROFILE_ARTS_L1      0x91
147                  * PROFILE_ARTS_L2      0x92
148                  * PROFILE_ARTS_L3      0x93
149                  * PROFILE_ARTS_L4      0x94
150                  * PROFILE_AS_L0        0xf0
151                  * PROFILE_AS_L1        0xf1
152                  * PROFILE_AS_L2        0xf2
153                  * PROFILE_AS_L3        0xf3
154                  * PROFILE_AS_L4        0xf4
155                  * PROFILE_AS_L5        0xf5
156                  */
157                 go->pali = 0xf5;
158                 break;
159         }
160         go->gop_size = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_size);
161         go->closed_gop = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_closure);
162         go->ipb = v4l2_ctrl_g_ctrl(go->mpeg_video_b_frames) != 0;
163         go->bitrate = v4l2_ctrl_g_ctrl(go->mpeg_video_bitrate);
164         go->repeat_seqhead = v4l2_ctrl_g_ctrl(go->mpeg_video_rep_seqheader);
165         go->gop_header_enable = 1;
166         go->dvd_mode = 0;
167         if (go->format == V4L2_PIX_FMT_MPEG2)
168                 go->dvd_mode =
169                         go->bitrate == 9800000 &&
170                         go->gop_size == 15 &&
171                         go->ipb == 0 &&
172                         go->repeat_seqhead == 1 &&
173                         go->closed_gop;
174
175         switch (v4l2_ctrl_g_ctrl(go->mpeg_video_aspect_ratio)) {
176         default:
177         case V4L2_MPEG_VIDEO_ASPECT_1x1:
178                 go->aspect_ratio = GO7007_RATIO_1_1;
179                 break;
180         case V4L2_MPEG_VIDEO_ASPECT_4x3:
181                 go->aspect_ratio = GO7007_RATIO_4_3;
182                 break;
183         case V4L2_MPEG_VIDEO_ASPECT_16x9:
184                 go->aspect_ratio = GO7007_RATIO_16_9;
185                 break;
186         }
187 }
188
189 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
190 {
191         int sensor_height = 0, sensor_width = 0;
192         int width, height, i;
193
194         if (fmt != NULL && !valid_pixelformat(fmt->fmt.pix.pixelformat))
195                 return -EINVAL;
196
197         get_resolution(go, &sensor_width, &sensor_height);
198
199         if (fmt == NULL) {
200                 width = sensor_width;
201                 height = sensor_height;
202         } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
203                 if (fmt->fmt.pix.width > sensor_width)
204                         width = sensor_width;
205                 else if (fmt->fmt.pix.width < 144)
206                         width = 144;
207                 else
208                         width = fmt->fmt.pix.width & ~0x0f;
209
210                 if (fmt->fmt.pix.height > sensor_height)
211                         height = sensor_height;
212                 else if (fmt->fmt.pix.height < 96)
213                         height = 96;
214                 else
215                         height = fmt->fmt.pix.height & ~0x0f;
216         } else {
217                 width = fmt->fmt.pix.width;
218
219                 if (width <= sensor_width / 4) {
220                         width = sensor_width / 4;
221                         height = sensor_height / 4;
222                 } else if (width <= sensor_width / 2) {
223                         width = sensor_width / 2;
224                         height = sensor_height / 2;
225                 } else {
226                         width = sensor_width;
227                         height = sensor_height;
228                 }
229                 width &= ~0xf;
230                 height &= ~0xf;
231         }
232
233         if (fmt != NULL) {
234                 u32 pixelformat = fmt->fmt.pix.pixelformat;
235
236                 memset(fmt, 0, sizeof(*fmt));
237                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
238                 fmt->fmt.pix.width = width;
239                 fmt->fmt.pix.height = height;
240                 fmt->fmt.pix.pixelformat = pixelformat;
241                 fmt->fmt.pix.field = V4L2_FIELD_NONE;
242                 fmt->fmt.pix.bytesperline = 0;
243                 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
244                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
245         }
246
247         if (try)
248                 return 0;
249
250         if (fmt)
251                 go->format = fmt->fmt.pix.pixelformat;
252         go->width = width;
253         go->height = height;
254         go->encoder_h_offset = go->board_info->sensor_h_offset;
255         go->encoder_v_offset = go->board_info->sensor_v_offset;
256         for (i = 0; i < 4; ++i)
257                 go->modet[i].enable = 0;
258         for (i = 0; i < 1624; ++i)
259                 go->modet_map[i] = 0;
260
261         if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
262                 struct v4l2_mbus_framefmt mbus_fmt;
263
264                 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
265                 mbus_fmt.width = fmt ? fmt->fmt.pix.width : width;
266                 mbus_fmt.height = height;
267                 go->encoder_h_halve = 0;
268                 go->encoder_v_halve = 0;
269                 go->encoder_subsample = 0;
270                 call_all(&go->v4l2_dev, video, s_mbus_fmt, &mbus_fmt);
271         } else {
272                 if (width <= sensor_width / 4) {
273                         go->encoder_h_halve = 1;
274                         go->encoder_v_halve = 1;
275                         go->encoder_subsample = 1;
276                 } else if (width <= sensor_width / 2) {
277                         go->encoder_h_halve = 1;
278                         go->encoder_v_halve = 1;
279                         go->encoder_subsample = 0;
280                 } else {
281                         go->encoder_h_halve = 0;
282                         go->encoder_v_halve = 0;
283                         go->encoder_subsample = 0;
284                 }
285         }
286         return 0;
287 }
288
289 #if 0
290 static int clip_to_modet_map(struct go7007 *go, int region,
291                 struct v4l2_clip *clip_list)
292 {
293         struct v4l2_clip clip, *clip_ptr;
294         int x, y, mbnum;
295
296         /* Check if coordinates are OK and if any macroblocks are already
297          * used by other regions (besides 0) */
298         clip_ptr = clip_list;
299         while (clip_ptr) {
300                 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
301                         return -EFAULT;
302                 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
303                                 clip.c.width <= 0 || (clip.c.width & 0xF))
304                         return -EINVAL;
305                 if (clip.c.left + clip.c.width > go->width)
306                         return -EINVAL;
307                 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
308                                 clip.c.height <= 0 || (clip.c.height & 0xF))
309                         return -EINVAL;
310                 if (clip.c.top + clip.c.height > go->height)
311                         return -EINVAL;
312                 for (y = 0; y < clip.c.height; y += 16)
313                         for (x = 0; x < clip.c.width; x += 16) {
314                                 mbnum = (go->width >> 4) *
315                                                 ((clip.c.top + y) >> 4) +
316                                         ((clip.c.left + x) >> 4);
317                                 if (go->modet_map[mbnum] != 0 &&
318                                                 go->modet_map[mbnum] != region)
319                                         return -EBUSY;
320                         }
321                 clip_ptr = clip.next;
322         }
323
324         /* Clear old region macroblocks */
325         for (mbnum = 0; mbnum < 1624; ++mbnum)
326                 if (go->modet_map[mbnum] == region)
327                         go->modet_map[mbnum] = 0;
328
329         /* Claim macroblocks in this list */
330         clip_ptr = clip_list;
331         while (clip_ptr) {
332                 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
333                         return -EFAULT;
334                 for (y = 0; y < clip.c.height; y += 16)
335                         for (x = 0; x < clip.c.width; x += 16) {
336                                 mbnum = (go->width >> 4) *
337                                                 ((clip.c.top + y) >> 4) +
338                                         ((clip.c.left + x) >> 4);
339                                 go->modet_map[mbnum] = region;
340                         }
341                 clip_ptr = clip.next;
342         }
343         return 0;
344 }
345 #endif
346
347 static int vidioc_querycap(struct file *file, void  *priv,
348                                         struct v4l2_capability *cap)
349 {
350         struct go7007 *go = video_drvdata(file);
351
352         strlcpy(cap->driver, "go7007", sizeof(cap->driver));
353         strlcpy(cap->card, go->name, sizeof(cap->card));
354         strlcpy(cap->bus_info, go->bus_info, sizeof(cap->bus_info));
355
356         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
357                                 V4L2_CAP_STREAMING;
358
359         if (go->board_info->num_aud_inputs)
360                 cap->device_caps |= V4L2_CAP_AUDIO;
361         if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
362                 cap->device_caps |= V4L2_CAP_TUNER;
363         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
364         return 0;
365 }
366
367 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
368                                         struct v4l2_fmtdesc *fmt)
369 {
370         char *desc = NULL;
371
372         switch (fmt->index) {
373         case 0:
374                 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
375                 desc = "Motion JPEG";
376                 break;
377         case 1:
378                 fmt->pixelformat = V4L2_PIX_FMT_MPEG1;
379                 desc = "MPEG-1 ES";
380                 break;
381         case 2:
382                 fmt->pixelformat = V4L2_PIX_FMT_MPEG2;
383                 desc = "MPEG-2 ES";
384                 break;
385         case 3:
386                 fmt->pixelformat = V4L2_PIX_FMT_MPEG4;
387                 desc = "MPEG-4 ES";
388                 break;
389         default:
390                 return -EINVAL;
391         }
392         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
393         fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
394
395         strncpy(fmt->description, desc, sizeof(fmt->description));
396
397         return 0;
398 }
399
400 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
401                                         struct v4l2_format *fmt)
402 {
403         struct go7007 *go = video_drvdata(file);
404
405         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
406         fmt->fmt.pix.width = go->width;
407         fmt->fmt.pix.height = go->height;
408         fmt->fmt.pix.pixelformat = go->format;
409         fmt->fmt.pix.field = V4L2_FIELD_NONE;
410         fmt->fmt.pix.bytesperline = 0;
411         fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
412         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
413
414         return 0;
415 }
416
417 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
418                         struct v4l2_format *fmt)
419 {
420         struct go7007 *go = video_drvdata(file);
421
422         return set_capture_size(go, fmt, 1);
423 }
424
425 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
426                         struct v4l2_format *fmt)
427 {
428         struct go7007 *go = video_drvdata(file);
429
430         if (vb2_is_busy(&go->vidq))
431                 return -EBUSY;
432
433         return set_capture_size(go, fmt, 0);
434 }
435
436 static int go7007_queue_setup(struct vb2_queue *q,
437                 const struct v4l2_format *fmt,
438                 unsigned int *num_buffers, unsigned int *num_planes,
439                 unsigned int sizes[], void *alloc_ctxs[])
440 {
441         sizes[0] = GO7007_BUF_SIZE;
442         *num_planes = 1;
443
444         if (*num_buffers < 2)
445                 *num_buffers = 2;
446
447         return 0;
448 }
449
450 static void go7007_buf_queue(struct vb2_buffer *vb)
451 {
452         struct vb2_queue *vq = vb->vb2_queue;
453         struct go7007 *go = vb2_get_drv_priv(vq);
454         struct go7007_buffer *go7007_vb =
455                 container_of(vb, struct go7007_buffer, vb);
456         unsigned long flags;
457
458         spin_lock_irqsave(&go->spinlock, flags);
459         list_add_tail(&go7007_vb->list, &go->vidq_active);
460         spin_unlock_irqrestore(&go->spinlock, flags);
461 }
462
463 static int go7007_buf_prepare(struct vb2_buffer *vb)
464 {
465         struct go7007_buffer *go7007_vb =
466                 container_of(vb, struct go7007_buffer, vb);
467
468         go7007_vb->modet_active = 0;
469         go7007_vb->frame_offset = 0;
470         vb->v4l2_planes[0].bytesused = 0;
471         return 0;
472 }
473
474 static void go7007_buf_finish(struct vb2_buffer *vb)
475 {
476         struct vb2_queue *vq = vb->vb2_queue;
477         struct go7007 *go = vb2_get_drv_priv(vq);
478         struct go7007_buffer *go7007_vb =
479                 container_of(vb, struct go7007_buffer, vb);
480         u32 frame_type_flag = get_frame_type_flag(go7007_vb, go->format);
481         struct v4l2_buffer *buf = &vb->v4l2_buf;
482
483         buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_BFRAME |
484                         V4L2_BUF_FLAG_PFRAME);
485         buf->flags |= frame_type_flag;
486         buf->field = V4L2_FIELD_NONE;
487 }
488
489 static int go7007_start_streaming(struct vb2_queue *q, unsigned int count)
490 {
491         struct go7007 *go = vb2_get_drv_priv(q);
492         int ret;
493
494         set_formatting(go);
495         mutex_lock(&go->hw_lock);
496         go->next_seq = 0;
497         go->active_buf = NULL;
498         q->streaming = 1;
499         if (go7007_start_encoder(go) < 0)
500                 ret = -EIO;
501         else
502                 ret = 0;
503         mutex_unlock(&go->hw_lock);
504         if (ret) {
505                 q->streaming = 0;
506                 return ret;
507         }
508         call_all(&go->v4l2_dev, video, s_stream, 1);
509         v4l2_ctrl_grab(go->mpeg_video_gop_size, true);
510         v4l2_ctrl_grab(go->mpeg_video_gop_closure, true);
511         v4l2_ctrl_grab(go->mpeg_video_bitrate, true);
512         v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, true);
513         /* Turn on Capture LED */
514         if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
515                 go7007_write_addr(go, 0x3c82, 0x0005);
516         return ret;
517 }
518
519 static int go7007_stop_streaming(struct vb2_queue *q)
520 {
521         struct go7007 *go = vb2_get_drv_priv(q);
522         unsigned long flags;
523
524         q->streaming = 0;
525         go7007_stream_stop(go);
526         mutex_lock(&go->hw_lock);
527         go7007_reset_encoder(go);
528         mutex_unlock(&go->hw_lock);
529         call_all(&go->v4l2_dev, video, s_stream, 0);
530
531         spin_lock_irqsave(&go->spinlock, flags);
532         INIT_LIST_HEAD(&go->vidq_active);
533         spin_unlock_irqrestore(&go->spinlock, flags);
534         v4l2_ctrl_grab(go->mpeg_video_gop_size, false);
535         v4l2_ctrl_grab(go->mpeg_video_gop_closure, false);
536         v4l2_ctrl_grab(go->mpeg_video_bitrate, false);
537         v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, false);
538         /* Turn on Capture LED */
539         if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
540                 go7007_write_addr(go, 0x3c82, 0x000d);
541         return 0;
542 }
543
544 static struct vb2_ops go7007_video_qops = {
545         .queue_setup    = go7007_queue_setup,
546         .buf_queue      = go7007_buf_queue,
547         .buf_prepare    = go7007_buf_prepare,
548         .buf_finish     = go7007_buf_finish,
549         .start_streaming = go7007_start_streaming,
550         .stop_streaming = go7007_stop_streaming,
551         .wait_prepare   = vb2_ops_wait_prepare,
552         .wait_finish    = vb2_ops_wait_finish,
553 };
554
555 static int vidioc_g_parm(struct file *filp, void *priv,
556                 struct v4l2_streamparm *parm)
557 {
558         struct go7007 *go = video_drvdata(filp);
559         struct v4l2_fract timeperframe = {
560                 .numerator = 1001 *  go->fps_scale,
561                 .denominator = go->sensor_framerate,
562         };
563
564         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
565                 return -EINVAL;
566
567         parm->parm.capture.readbuffers = 2;
568         parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
569         parm->parm.capture.timeperframe = timeperframe;
570
571         return 0;
572 }
573
574 static int vidioc_s_parm(struct file *filp, void *priv,
575                 struct v4l2_streamparm *parm)
576 {
577         struct go7007 *go = video_drvdata(filp);
578         unsigned int n, d;
579
580         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
581                 return -EINVAL;
582
583         n = go->sensor_framerate *
584                 parm->parm.capture.timeperframe.numerator;
585         d = 1001 * parm->parm.capture.timeperframe.denominator;
586         if (n != 0 && d != 0 && n > d)
587                 go->fps_scale = (n + d/2) / d;
588         else
589                 go->fps_scale = 1;
590
591         return vidioc_g_parm(filp, priv, parm);
592 }
593
594 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and
595    its resolution, when the device is not connected to TV.
596    This is were an API abuse, probably used by the lack of specific IOCTL's to
597    enumerate it, by the time the driver was written.
598
599    However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
600    and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
601
602    The two functions below implement the newer ioctls
603 */
604 static int vidioc_enum_framesizes(struct file *filp, void *priv,
605                                   struct v4l2_frmsizeenum *fsize)
606 {
607         struct go7007 *go = video_drvdata(filp);
608         int width, height;
609
610         if (fsize->index > 2)
611                 return -EINVAL;
612
613         if (!valid_pixelformat(fsize->pixel_format))
614                 return -EINVAL;
615
616         get_resolution(go, &width, &height);
617         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
618         fsize->discrete.width = (width >> fsize->index) & ~0xf;
619         fsize->discrete.height = (height >> fsize->index) & ~0xf;
620         return 0;
621 }
622
623 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
624                                       struct v4l2_frmivalenum *fival)
625 {
626         struct go7007 *go = video_drvdata(filp);
627         int width, height;
628         int i;
629
630         if (fival->index > 4)
631                 return -EINVAL;
632
633         if (!valid_pixelformat(fival->pixel_format))
634                 return -EINVAL;
635
636         if (!(go->board_info->sensor_flags & GO7007_SENSOR_SCALING)) {
637                 get_resolution(go, &width, &height);
638                 for (i = 0; i <= 2; i++)
639                         if (fival->width == ((width >> i) & ~0xf) &&
640                             fival->height == ((height >> i) & ~0xf))
641                                 break;
642                 if (i > 2)
643                         return -EINVAL;
644         }
645         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
646         fival->discrete.numerator = 1001 * (fival->index + 1);
647         fival->discrete.denominator = go->sensor_framerate;
648         return 0;
649 }
650
651 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
652 {
653         struct go7007 *go = video_drvdata(file);
654
655         *std = go->std;
656         return 0;
657 }
658
659 static int go7007_s_std(struct go7007 *go)
660 {
661         if (go->std & V4L2_STD_625_50) {
662                 go->standard = GO7007_STD_PAL;
663                 go->sensor_framerate = 25025;
664         } else {
665                 go->standard = GO7007_STD_NTSC;
666                 go->sensor_framerate = 30000;
667         }
668
669         call_all(&go->v4l2_dev, core, s_std, go->std);
670         set_capture_size(go, NULL, 0);
671         return 0;
672 }
673
674 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
675 {
676         struct go7007 *go = video_drvdata(file);
677
678         if (vb2_is_busy(&go->vidq))
679                 return -EBUSY;
680
681         go->std = std;
682
683         return go7007_s_std(go);
684 }
685
686 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
687 {
688         struct go7007 *go = video_drvdata(file);
689
690         return call_all(&go->v4l2_dev, video, querystd, std);
691 }
692
693 static int vidioc_enum_input(struct file *file, void *priv,
694                                 struct v4l2_input *inp)
695 {
696         struct go7007 *go = video_drvdata(file);
697
698         if (inp->index >= go->board_info->num_inputs)
699                 return -EINVAL;
700
701         strncpy(inp->name, go->board_info->inputs[inp->index].name,
702                         sizeof(inp->name));
703
704         /* If this board has a tuner, it will be the first input */
705         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
706                         inp->index == 0)
707                 inp->type = V4L2_INPUT_TYPE_TUNER;
708         else
709                 inp->type = V4L2_INPUT_TYPE_CAMERA;
710
711         if (go->board_info->num_aud_inputs)
712                 inp->audioset = (1 << go->board_info->num_aud_inputs) - 1;
713         else
714                 inp->audioset = 0;
715         inp->tuner = 0;
716         if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
717                 inp->std = video_devdata(file)->tvnorms;
718         else
719                 inp->std = 0;
720
721         return 0;
722 }
723
724
725 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
726 {
727         struct go7007 *go = video_drvdata(file);
728
729         *input = go->input;
730
731         return 0;
732 }
733
734 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
735 {
736         struct go7007 *go = video_drvdata(file);
737
738         if (a->index >= go->board_info->num_aud_inputs)
739                 return -EINVAL;
740         strlcpy(a->name, go->board_info->aud_inputs[a->index].name,
741                 sizeof(a->name));
742         a->capability = V4L2_AUDCAP_STEREO;
743         return 0;
744 }
745
746 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
747 {
748         struct go7007 *go = video_drvdata(file);
749
750         a->index = go->aud_input;
751         strlcpy(a->name, go->board_info->aud_inputs[go->aud_input].name,
752                 sizeof(a->name));
753         a->capability = V4L2_AUDCAP_STEREO;
754         return 0;
755 }
756
757 static int vidioc_s_audio(struct file *file, void *fh,
758         const struct v4l2_audio *a)
759 {
760         struct go7007 *go = video_drvdata(file);
761
762         if (a->index >= go->board_info->num_aud_inputs)
763                 return -EINVAL;
764         go->aud_input = a->index;
765         v4l2_subdev_call(go->sd_audio, audio, s_routing,
766                 go->board_info->aud_inputs[go->aud_input].audio_input, 0, 0);
767         return 0;
768 }
769
770 static void go7007_s_input(struct go7007 *go)
771 {
772         unsigned int input = go->input;
773
774         v4l2_subdev_call(go->sd_video, video, s_routing,
775                         go->board_info->inputs[input].video_input, 0,
776                         go->board_info->video_config);
777         if (go->board_info->num_aud_inputs) {
778                 int aud_input = go->board_info->inputs[input].audio_index;
779
780                 v4l2_subdev_call(go->sd_audio, audio, s_routing,
781                         go->board_info->aud_inputs[aud_input].audio_input, 0, 0);
782                 go->aud_input = aud_input;
783         }
784 }
785
786 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
787 {
788         struct go7007 *go = video_drvdata(file);
789
790         if (input >= go->board_info->num_inputs)
791                 return -EINVAL;
792         if (vb2_is_busy(&go->vidq))
793                 return -EBUSY;
794
795         go->input = input;
796         go7007_s_input(go);
797
798         return 0;
799 }
800
801 static int vidioc_g_tuner(struct file *file, void *priv,
802                                 struct v4l2_tuner *t)
803 {
804         struct go7007 *go = video_drvdata(file);
805
806         if (t->index != 0)
807                 return -EINVAL;
808
809         strlcpy(t->name, "Tuner", sizeof(t->name));
810         return call_all(&go->v4l2_dev, tuner, g_tuner, t);
811 }
812
813 static int vidioc_s_tuner(struct file *file, void *priv,
814                                 const struct v4l2_tuner *t)
815 {
816         struct go7007 *go = video_drvdata(file);
817
818         if (t->index != 0)
819                 return -EINVAL;
820
821         return call_all(&go->v4l2_dev, tuner, s_tuner, t);
822 }
823
824 static int vidioc_g_frequency(struct file *file, void *priv,
825                                 struct v4l2_frequency *f)
826 {
827         struct go7007 *go = video_drvdata(file);
828
829         if (f->tuner)
830                 return -EINVAL;
831
832         return call_all(&go->v4l2_dev, tuner, g_frequency, f);
833 }
834
835 static int vidioc_s_frequency(struct file *file, void *priv,
836                                 const struct v4l2_frequency *f)
837 {
838         struct go7007 *go = video_drvdata(file);
839
840         if (f->tuner)
841                 return -EINVAL;
842
843         return call_all(&go->v4l2_dev, tuner, s_frequency, f);
844 }
845
846 static int vidioc_log_status(struct file *file, void *priv)
847 {
848         struct go7007 *go = video_drvdata(file);
849
850         v4l2_ctrl_log_status(file, priv);
851         return call_all(&go->v4l2_dev, core, log_status);
852 }
853
854 /* FIXME:
855         Those ioctls are private, and not needed, since several standard
856         extended controls already provide streaming control.
857         So, those ioctls should be converted into vidioc_g_ext_ctrls()
858         and vidioc_s_ext_ctrls()
859  */
860
861 #if 0
862         case GO7007IOC_S_MD_PARAMS:
863         {
864                 struct go7007_md_params *mdp = arg;
865
866                 if (mdp->region > 3)
867                         return -EINVAL;
868                 if (mdp->trigger > 0) {
869                         go->modet[mdp->region].pixel_threshold =
870                                         mdp->pixel_threshold >> 1;
871                         go->modet[mdp->region].motion_threshold =
872                                         mdp->motion_threshold >> 1;
873                         go->modet[mdp->region].mb_threshold =
874                                         mdp->trigger >> 1;
875                         go->modet[mdp->region].enable = 1;
876                 } else
877                         go->modet[mdp->region].enable = 0;
878                 /* fall-through */
879         }
880         case GO7007IOC_S_MD_REGION:
881         {
882                 struct go7007_md_region *region = arg;
883
884                 if (region->region < 1 || region->region > 3)
885                         return -EINVAL;
886                 return clip_to_modet_map(go, region->region, region->clips);
887         }
888 #endif
889
890 static struct v4l2_file_operations go7007_fops = {
891         .owner          = THIS_MODULE,
892         .open           = v4l2_fh_open,
893         .release        = vb2_fop_release,
894         .unlocked_ioctl = video_ioctl2,
895         .read           = vb2_fop_read,
896         .mmap           = vb2_fop_mmap,
897         .poll           = vb2_fop_poll,
898 };
899
900 static const struct v4l2_ioctl_ops video_ioctl_ops = {
901         .vidioc_querycap          = vidioc_querycap,
902         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
903         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
904         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
905         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
906         .vidioc_reqbufs           = vb2_ioctl_reqbufs,
907         .vidioc_querybuf          = vb2_ioctl_querybuf,
908         .vidioc_qbuf              = vb2_ioctl_qbuf,
909         .vidioc_dqbuf             = vb2_ioctl_dqbuf,
910         .vidioc_g_std             = vidioc_g_std,
911         .vidioc_s_std             = vidioc_s_std,
912         .vidioc_querystd          = vidioc_querystd,
913         .vidioc_enum_input        = vidioc_enum_input,
914         .vidioc_g_input           = vidioc_g_input,
915         .vidioc_s_input           = vidioc_s_input,
916         .vidioc_enumaudio         = vidioc_enumaudio,
917         .vidioc_g_audio           = vidioc_g_audio,
918         .vidioc_s_audio           = vidioc_s_audio,
919         .vidioc_streamon          = vb2_ioctl_streamon,
920         .vidioc_streamoff         = vb2_ioctl_streamoff,
921         .vidioc_g_tuner           = vidioc_g_tuner,
922         .vidioc_s_tuner           = vidioc_s_tuner,
923         .vidioc_g_frequency       = vidioc_g_frequency,
924         .vidioc_s_frequency       = vidioc_s_frequency,
925         .vidioc_g_parm            = vidioc_g_parm,
926         .vidioc_s_parm            = vidioc_s_parm,
927         .vidioc_enum_framesizes   = vidioc_enum_framesizes,
928         .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
929         .vidioc_log_status        = vidioc_log_status,
930         .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
931         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
932 };
933
934 static struct video_device go7007_template = {
935         .name           = "go7007",
936         .fops           = &go7007_fops,
937         .release        = video_device_release_empty,
938         .ioctl_ops      = &video_ioctl_ops,
939         .tvnorms        = V4L2_STD_ALL,
940 };
941
942 int go7007_v4l2_ctrl_init(struct go7007 *go)
943 {
944         struct v4l2_ctrl_handler *hdl = &go->hdl;
945         struct v4l2_ctrl *ctrl;
946
947         v4l2_ctrl_handler_init(hdl, 13);
948         go->mpeg_video_gop_size = v4l2_ctrl_new_std(hdl, NULL,
949                         V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 34, 1, 15);
950         go->mpeg_video_gop_closure = v4l2_ctrl_new_std(hdl, NULL,
951                         V4L2_CID_MPEG_VIDEO_GOP_CLOSURE, 0, 1, 1, 1);
952         go->mpeg_video_bitrate = v4l2_ctrl_new_std(hdl, NULL,
953                         V4L2_CID_MPEG_VIDEO_BITRATE,
954                         64000, 10000000, 1, 9800000);
955         go->mpeg_video_b_frames = v4l2_ctrl_new_std(hdl, NULL,
956                         V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 2, 2, 0);
957         go->mpeg_video_rep_seqheader = v4l2_ctrl_new_std(hdl, NULL,
958                         V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 0, 1, 1, 1);
959
960         go->mpeg_video_aspect_ratio = v4l2_ctrl_new_std_menu(hdl, NULL,
961                         V4L2_CID_MPEG_VIDEO_ASPECT,
962                         V4L2_MPEG_VIDEO_ASPECT_16x9, 0,
963                         V4L2_MPEG_VIDEO_ASPECT_1x1);
964         ctrl = v4l2_ctrl_new_std(hdl, NULL,
965                         V4L2_CID_JPEG_ACTIVE_MARKER, 0,
966                         V4L2_JPEG_ACTIVE_MARKER_DQT |
967                         V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
968                         V4L2_JPEG_ACTIVE_MARKER_DQT |
969                         V4L2_JPEG_ACTIVE_MARKER_DHT);
970         if (ctrl)
971                 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
972         if (hdl->error) {
973                 int rv = hdl->error;
974
975                 v4l2_err(&go->v4l2_dev, "Could not register controls\n");
976                 return rv;
977         }
978         go->v4l2_dev.ctrl_handler = hdl;
979         return 0;
980 }
981
982 int go7007_v4l2_init(struct go7007 *go)
983 {
984         struct video_device *vdev = &go->vdev;
985         int rv;
986
987         mutex_init(&go->serialize_lock);
988         mutex_init(&go->queue_lock);
989
990         INIT_LIST_HEAD(&go->vidq_active);
991         go->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
992         go->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
993         go->vidq.ops = &go7007_video_qops;
994         go->vidq.mem_ops = &vb2_vmalloc_memops;
995         go->vidq.drv_priv = go;
996         go->vidq.buf_struct_size = sizeof(struct go7007_buffer);
997         go->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
998         go->vidq.lock = &go->queue_lock;
999         rv = vb2_queue_init(&go->vidq);
1000         if (rv)
1001                 return rv;
1002         *vdev = go7007_template;
1003         vdev->lock = &go->serialize_lock;
1004         vdev->queue = &go->vidq;
1005         set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1006         video_set_drvdata(vdev, go);
1007         vdev->v4l2_dev = &go->v4l2_dev;
1008         if (!v4l2_device_has_op(&go->v4l2_dev, video, querystd))
1009                 v4l2_disable_ioctl(vdev, VIDIOC_QUERYSTD);
1010         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) {
1011                 v4l2_disable_ioctl(vdev, VIDIOC_S_FREQUENCY);
1012                 v4l2_disable_ioctl(vdev, VIDIOC_G_FREQUENCY);
1013                 v4l2_disable_ioctl(vdev, VIDIOC_S_TUNER);
1014                 v4l2_disable_ioctl(vdev, VIDIOC_G_TUNER);
1015         } else {
1016                 struct v4l2_frequency f = {
1017                         .type = V4L2_TUNER_ANALOG_TV,
1018                         .frequency = 980,
1019                 };
1020
1021                 call_all(&go->v4l2_dev, tuner, s_frequency, &f);
1022         }
1023         if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV)) {
1024                 v4l2_disable_ioctl(vdev, VIDIOC_G_STD);
1025                 v4l2_disable_ioctl(vdev, VIDIOC_S_STD);
1026                 vdev->tvnorms = 0;
1027         }
1028         if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING)
1029                 v4l2_disable_ioctl(vdev, VIDIOC_ENUM_FRAMESIZES);
1030         if (go->board_info->num_aud_inputs == 0) {
1031                 v4l2_disable_ioctl(vdev, VIDIOC_G_AUDIO);
1032                 v4l2_disable_ioctl(vdev, VIDIOC_S_AUDIO);
1033                 v4l2_disable_ioctl(vdev, VIDIOC_ENUMAUDIO);
1034         }
1035         /* Setup correct crystal frequency on this board */
1036         if (go->board_info->sensor_flags & GO7007_SENSOR_SAA7115)
1037                 v4l2_subdev_call(go->sd_video, video, s_crystal_freq,
1038                                 SAA7115_FREQ_24_576_MHZ,
1039                                 SAA7115_FREQ_FL_APLL | SAA7115_FREQ_FL_UCGC |
1040                                 SAA7115_FREQ_FL_DOUBLE_ASCLK);
1041         go7007_s_input(go);
1042         if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1043                 go7007_s_std(go);
1044         rv = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1045         if (rv < 0)
1046                 return rv;
1047         dev_info(go->dev, "registered device %s [v4l2]\n",
1048                  video_device_node_name(vdev));
1049
1050         return 0;
1051 }
1052
1053 void go7007_v4l2_remove(struct go7007 *go)
1054 {
1055         v4l2_ctrl_handler_free(&go->hdl);
1056 }