Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / staging / 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/init.h>
20 #include <linux/version.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/spinlock.h>
24 #include <linux/fs.h>
25 #include <linux/unistd.h>
26 #include <linux/time.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/videodev2.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-subdev.h>
33 #include <linux/i2c.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <asm/system.h>
37
38 #include "go7007.h"
39 #include "go7007-priv.h"
40 #include "wis-i2c.h"
41
42 /* Temporary defines until accepted in v4l-dvb */
43 #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
44 #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM   6 /* MPEG elementary stream */
45 #endif
46 #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
47 #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4   3
48 #endif
49
50 #define call_all(dev, o, f, args...) \
51         v4l2_device_call_until_err(dev, 0, o, f, ##args)
52
53 static void deactivate_buffer(struct go7007_buffer *gobuf)
54 {
55         int i;
56
57         if (gobuf->state != BUF_STATE_IDLE) {
58                 list_del(&gobuf->stream);
59                 gobuf->state = BUF_STATE_IDLE;
60         }
61         if (gobuf->page_count > 0) {
62                 for (i = 0; i < gobuf->page_count; ++i)
63                         page_cache_release(gobuf->pages[i]);
64                 gobuf->page_count = 0;
65         }
66 }
67
68 static void abort_queued(struct go7007 *go)
69 {
70         struct go7007_buffer *gobuf, *next;
71
72         list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
73                 deactivate_buffer(gobuf);
74         }
75 }
76
77 static int go7007_streamoff(struct go7007 *go)
78 {
79         int retval = -EINVAL;
80         unsigned long flags;
81
82         mutex_lock(&go->hw_lock);
83         if (go->streaming) {
84                 go->streaming = 0;
85                 go7007_stream_stop(go);
86                 spin_lock_irqsave(&go->spinlock, flags);
87                 abort_queued(go);
88                 spin_unlock_irqrestore(&go->spinlock, flags);
89                 go7007_reset_encoder(go);
90                 retval = 0;
91         }
92         mutex_unlock(&go->hw_lock);
93         return 0;
94 }
95
96 static int go7007_open(struct file *file)
97 {
98         struct go7007 *go = video_get_drvdata(video_devdata(file));
99         struct go7007_file *gofh;
100
101         if (go->status != STATUS_ONLINE)
102                 return -EBUSY;
103         gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
104         if (gofh == NULL)
105                 return -ENOMEM;
106         ++go->ref_count;
107         gofh->go = go;
108         mutex_init(&gofh->lock);
109         gofh->buf_count = 0;
110         file->private_data = gofh;
111         return 0;
112 }
113
114 static int go7007_release(struct file *file)
115 {
116         struct go7007_file *gofh = file->private_data;
117         struct go7007 *go = gofh->go;
118
119         if (gofh->buf_count > 0) {
120                 go7007_streamoff(go);
121                 go->in_use = 0;
122                 kfree(gofh->bufs);
123                 gofh->buf_count = 0;
124         }
125         kfree(gofh);
126         if (--go->ref_count == 0)
127                 kfree(go);
128         file->private_data = NULL;
129         return 0;
130 }
131
132 static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
133 {
134         u8 *f = page_address(gobuf->pages[0]);
135
136         switch (format) {
137         case GO7007_FORMAT_MJPEG:
138                 return V4L2_BUF_FLAG_KEYFRAME;
139         case GO7007_FORMAT_MPEG4:
140                 switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
141                 case 0:
142                         return V4L2_BUF_FLAG_KEYFRAME;
143                 case 1:
144                         return V4L2_BUF_FLAG_PFRAME;
145                 case 2:
146                         return V4L2_BUF_FLAG_BFRAME;
147                 default:
148                         return 0;
149                 }
150         case GO7007_FORMAT_MPEG1:
151         case GO7007_FORMAT_MPEG2:
152                 switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
153                 case 1:
154                         return V4L2_BUF_FLAG_KEYFRAME;
155                 case 2:
156                         return V4L2_BUF_FLAG_PFRAME;
157                 case 3:
158                         return V4L2_BUF_FLAG_BFRAME;
159                 default:
160                         return 0;
161                 }
162         }
163
164         return 0;
165 }
166
167 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
168 {
169         int sensor_height = 0, sensor_width = 0;
170         int width, height, i;
171
172         if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
173                         fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
174                         fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
175                 return -EINVAL;
176
177         switch (go->standard) {
178         case GO7007_STD_NTSC:
179                 sensor_width = 720;
180                 sensor_height = 480;
181                 break;
182         case GO7007_STD_PAL:
183                 sensor_width = 720;
184                 sensor_height = 576;
185                 break;
186         case GO7007_STD_OTHER:
187                 sensor_width = go->board_info->sensor_width;
188                 sensor_height = go->board_info->sensor_height;
189                 break;
190         }
191
192         if (fmt == NULL) {
193                 width = sensor_width;
194                 height = sensor_height;
195         } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
196                 if (fmt->fmt.pix.width > sensor_width)
197                         width = sensor_width;
198                 else if (fmt->fmt.pix.width < 144)
199                         width = 144;
200                 else
201                         width = fmt->fmt.pix.width & ~0x0f;
202
203                 if (fmt->fmt.pix.height > sensor_height)
204                         height = sensor_height;
205                 else if (fmt->fmt.pix.height < 96)
206                         height = 96;
207                 else
208                         height = fmt->fmt.pix.height & ~0x0f;
209         } else {
210                 int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
211                 int sensor_size = sensor_width * sensor_height;
212
213                 if (64 * requested_size < 9 * sensor_size) {
214                         width = sensor_width / 4;
215                         height = sensor_height / 4;
216                 } else if (64 * requested_size < 36 * sensor_size) {
217                         width = sensor_width / 2;
218                         height = sensor_height / 2;
219                 } else {
220                         width = sensor_width;
221                         height = sensor_height;
222                 }
223                 width &= ~0xf;
224                 height &= ~0xf;
225         }
226
227         if (fmt != NULL) {
228                 u32 pixelformat = fmt->fmt.pix.pixelformat;
229
230                 memset(fmt, 0, sizeof(*fmt));
231                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
232                 fmt->fmt.pix.width = width;
233                 fmt->fmt.pix.height = height;
234                 fmt->fmt.pix.pixelformat = pixelformat;
235                 fmt->fmt.pix.field = V4L2_FIELD_NONE;
236                 fmt->fmt.pix.bytesperline = 0;
237                 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
238                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
239         }
240
241         if (try)
242                 return 0;
243
244         go->width = width;
245         go->height = height;
246         go->encoder_h_offset = go->board_info->sensor_h_offset;
247         go->encoder_v_offset = go->board_info->sensor_v_offset;
248         for (i = 0; i < 4; ++i)
249                 go->modet[i].enable = 0;
250         for (i = 0; i < 1624; ++i)
251                 go->modet_map[i] = 0;
252
253         if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
254                 struct v4l2_format res;
255
256                 if (fmt != NULL) {
257                         res = *fmt;
258                 } else {
259                         res.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
260                         res.fmt.pix.width = width;
261                 }
262
263                 if (height > sensor_height / 2) {
264                         res.fmt.pix.height = height / 2;
265                         go->encoder_v_halve = 0;
266                 } else {
267                         res.fmt.pix.height = height;
268                         go->encoder_v_halve = 1;
269                 }
270                 call_all(&go->v4l2_dev, video, s_fmt, &res);
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
287         if (fmt == NULL)
288                 return 0;
289
290         switch (fmt->fmt.pix.pixelformat) {
291         case V4L2_PIX_FMT_MPEG:
292                 if (go->format == GO7007_FORMAT_MPEG1 ||
293                                 go->format == GO7007_FORMAT_MPEG2 ||
294                                 go->format == GO7007_FORMAT_MPEG4)
295                         break;
296                 go->format = GO7007_FORMAT_MPEG1;
297                 go->pali = 0;
298                 go->aspect_ratio = GO7007_RATIO_1_1;
299                 go->gop_size = go->sensor_framerate / 1000;
300                 go->ipb = 0;
301                 go->closed_gop = 1;
302                 go->repeat_seqhead = 1;
303                 go->seq_header_enable = 1;
304                 go->gop_header_enable = 1;
305                 go->dvd_mode = 0;
306                 break;
307         /* Backwards compatibility only! */
308         case V4L2_PIX_FMT_MPEG4:
309                 if (go->format == GO7007_FORMAT_MPEG4)
310                         break;
311                 go->format = GO7007_FORMAT_MPEG4;
312                 go->pali = 0xf5;
313                 go->aspect_ratio = GO7007_RATIO_1_1;
314                 go->gop_size = go->sensor_framerate / 1000;
315                 go->ipb = 0;
316                 go->closed_gop = 1;
317                 go->repeat_seqhead = 1;
318                 go->seq_header_enable = 1;
319                 go->gop_header_enable = 1;
320                 go->dvd_mode = 0;
321                 break;
322         case V4L2_PIX_FMT_MJPEG:
323                 go->format = GO7007_FORMAT_MJPEG;
324                 go->pali = 0;
325                 go->aspect_ratio = GO7007_RATIO_1_1;
326                 go->gop_size = 0;
327                 go->ipb = 0;
328                 go->closed_gop = 0;
329                 go->repeat_seqhead = 0;
330                 go->seq_header_enable = 0;
331                 go->gop_header_enable = 0;
332                 go->dvd_mode = 0;
333                 break;
334         }
335         return 0;
336 }
337
338 #if 0
339 static int clip_to_modet_map(struct go7007 *go, int region,
340                 struct v4l2_clip *clip_list)
341 {
342         struct v4l2_clip clip, *clip_ptr;
343         int x, y, mbnum;
344
345         /* Check if coordinates are OK and if any macroblocks are already
346          * used by other regions (besides 0) */
347         clip_ptr = clip_list;
348         while (clip_ptr) {
349                 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
350                         return -EFAULT;
351                 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
352                                 clip.c.width <= 0 || (clip.c.width & 0xF))
353                         return -EINVAL;
354                 if (clip.c.left + clip.c.width > go->width)
355                         return -EINVAL;
356                 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
357                                 clip.c.height <= 0 || (clip.c.height & 0xF))
358                         return -EINVAL;
359                 if (clip.c.top + clip.c.height > go->height)
360                         return -EINVAL;
361                 for (y = 0; y < clip.c.height; y += 16)
362                         for (x = 0; x < clip.c.width; x += 16) {
363                                 mbnum = (go->width >> 4) *
364                                                 ((clip.c.top + y) >> 4) +
365                                         ((clip.c.left + x) >> 4);
366                                 if (go->modet_map[mbnum] != 0 &&
367                                                 go->modet_map[mbnum] != region)
368                                         return -EBUSY;
369                         }
370                 clip_ptr = clip.next;
371         }
372
373         /* Clear old region macroblocks */
374         for (mbnum = 0; mbnum < 1624; ++mbnum)
375                 if (go->modet_map[mbnum] == region)
376                         go->modet_map[mbnum] = 0;
377
378         /* Claim macroblocks in this list */
379         clip_ptr = clip_list;
380         while (clip_ptr) {
381                 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
382                         return -EFAULT;
383                 for (y = 0; y < clip.c.height; y += 16)
384                         for (x = 0; x < clip.c.width; x += 16) {
385                                 mbnum = (go->width >> 4) *
386                                                 ((clip.c.top + y) >> 4) +
387                                         ((clip.c.left + x) >> 4);
388                                 go->modet_map[mbnum] = region;
389                         }
390                 clip_ptr = clip.next;
391         }
392         return 0;
393 }
394 #endif
395
396 static int mpeg_query_ctrl(struct v4l2_queryctrl *ctrl)
397 {
398         static const u32 mpeg_ctrls[] = {
399                 V4L2_CID_MPEG_CLASS,
400                 V4L2_CID_MPEG_STREAM_TYPE,
401                 V4L2_CID_MPEG_VIDEO_ENCODING,
402                 V4L2_CID_MPEG_VIDEO_ASPECT,
403                 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
404                 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
405                 V4L2_CID_MPEG_VIDEO_BITRATE,
406                 0
407         };
408         static const u32 *ctrl_classes[] = {
409                 mpeg_ctrls,
410                 NULL
411         };
412
413         ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
414
415         switch (ctrl->id) {
416         case V4L2_CID_MPEG_CLASS:
417                 return v4l2_ctrl_query_fill(ctrl, 0, 0, 0, 0);
418         case V4L2_CID_MPEG_STREAM_TYPE:
419                 return v4l2_ctrl_query_fill(ctrl,
420                                 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
421                                 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
422                                 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
423         case V4L2_CID_MPEG_VIDEO_ENCODING:
424                 return v4l2_ctrl_query_fill(ctrl,
425                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
426                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
427                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
428         case V4L2_CID_MPEG_VIDEO_ASPECT:
429                 return v4l2_ctrl_query_fill(ctrl,
430                                 V4L2_MPEG_VIDEO_ASPECT_1x1,
431                                 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
432                                 V4L2_MPEG_VIDEO_ASPECT_1x1);
433         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
434                 return v4l2_ctrl_query_fill(ctrl, 0, 34, 1, 15);
435         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
436                 return v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
437         case V4L2_CID_MPEG_VIDEO_BITRATE:
438                 return v4l2_ctrl_query_fill(ctrl,
439                                 64000,
440                                 10000000, 1,
441                                 1500000);
442         default:
443                 return -EINVAL;
444         }
445         return 0;
446 }
447
448 static int mpeg_s_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
449 {
450         /* pretty sure we can't change any of these while streaming */
451         if (go->streaming)
452                 return -EBUSY;
453
454         switch (ctrl->id) {
455         case V4L2_CID_MPEG_STREAM_TYPE:
456                 switch (ctrl->value) {
457                 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
458                         go->format = GO7007_FORMAT_MPEG2;
459                         go->bitrate = 9800000;
460                         go->gop_size = 15;
461                         go->pali = 0x48;
462                         go->closed_gop = 1;
463                         go->repeat_seqhead = 0;
464                         go->seq_header_enable = 1;
465                         go->gop_header_enable = 1;
466                         go->dvd_mode = 1;
467                         break;
468                 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
469                         /* todo: */
470                         break;
471                 default:
472                         return -EINVAL;
473                 }
474                 break;
475         case V4L2_CID_MPEG_VIDEO_ENCODING:
476                 switch (ctrl->value) {
477                 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
478                         go->format = GO7007_FORMAT_MPEG1;
479                         go->pali = 0;
480                         break;
481                 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
482                         go->format = GO7007_FORMAT_MPEG2;
483                         /*if (mpeg->pali >> 24 == 2)
484                                 go->pali = mpeg->pali & 0xff;
485                         else*/
486                                 go->pali = 0x48;
487                         break;
488                 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
489                         go->format = GO7007_FORMAT_MPEG4;
490                         /*if (mpeg->pali >> 24 == 4)
491                                 go->pali = mpeg->pali & 0xff;
492                         else*/
493                                 go->pali = 0xf5;
494                         break;
495                 default:
496                         return -EINVAL;
497                 }
498                 go->gop_header_enable =
499                         /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
500                         ? 0 :*/ 1;
501                 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
502                         go->repeat_seqhead = 1;
503                 else*/
504                         go->repeat_seqhead = 0;
505                 go->dvd_mode = 0;
506                 break;
507         case V4L2_CID_MPEG_VIDEO_ASPECT:
508                 if (go->format == GO7007_FORMAT_MJPEG)
509                         return -EINVAL;
510                 switch (ctrl->value) {
511                 case V4L2_MPEG_VIDEO_ASPECT_1x1:
512                         go->aspect_ratio = GO7007_RATIO_1_1;
513                         break;
514                 case V4L2_MPEG_VIDEO_ASPECT_4x3:
515                         go->aspect_ratio = GO7007_RATIO_4_3;
516                         break;
517                 case V4L2_MPEG_VIDEO_ASPECT_16x9:
518                         go->aspect_ratio = GO7007_RATIO_16_9;
519                         break;
520                 case V4L2_MPEG_VIDEO_ASPECT_221x100:
521                 default:
522                         return -EINVAL;
523                 }
524                 break;
525         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
526                 if (ctrl->value < 0 || ctrl->value > 34)
527                         return -EINVAL;
528                 go->gop_size = ctrl->value;
529                 break;
530         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
531                 if (ctrl->value != 0 && ctrl->value != 1)
532                         return -EINVAL;
533                 go->closed_gop = ctrl->value;
534                 break;
535         case V4L2_CID_MPEG_VIDEO_BITRATE:
536                 /* Upper bound is kind of arbitrary here */
537                 if (ctrl->value < 64000 || ctrl->value > 10000000)
538                         return -EINVAL;
539                 go->bitrate = ctrl->value;
540                 break;
541         default:
542                 return -EINVAL;
543         }
544         return 0;
545 }
546
547 static int mpeg_g_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
548 {
549         switch (ctrl->id) {
550         case V4L2_CID_MPEG_STREAM_TYPE:
551                 if (go->dvd_mode)
552                         ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
553                 else
554                         ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
555                 break;
556         case V4L2_CID_MPEG_VIDEO_ENCODING:
557                 switch (go->format) {
558                 case GO7007_FORMAT_MPEG1:
559                         ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
560                         break;
561                 case GO7007_FORMAT_MPEG2:
562                         ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
563                         break;
564                 case GO7007_FORMAT_MPEG4:
565                         ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
566                         break;
567                 default:
568                         return -EINVAL;
569                 }
570                 break;
571         case V4L2_CID_MPEG_VIDEO_ASPECT:
572                 switch (go->aspect_ratio) {
573                 case GO7007_RATIO_1_1:
574                         ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
575                         break;
576                 case GO7007_RATIO_4_3:
577                         ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
578                         break;
579                 case GO7007_RATIO_16_9:
580                         ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
581                         break;
582                 default:
583                         return -EINVAL;
584                 }
585                 break;
586         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
587                 ctrl->value = go->gop_size;
588                 break;
589         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
590                 ctrl->value = go->closed_gop;
591                 break;
592         case V4L2_CID_MPEG_VIDEO_BITRATE:
593                 ctrl->value = go->bitrate;
594                 break;
595         default:
596                 return -EINVAL;
597         }
598         return 0;
599 }
600
601 static int vidioc_querycap(struct file *file, void  *priv,
602                                         struct v4l2_capability *cap)
603 {
604         struct go7007 *go = ((struct go7007_file *) priv)->go;
605
606         strlcpy(cap->driver, "go7007", sizeof(cap->driver));
607         strlcpy(cap->card, go->name, sizeof(cap->card));
608 #if 0
609         strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
610 #endif
611
612         cap->version = KERNEL_VERSION(0, 9, 8);
613
614         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
615                             V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
616
617         if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
618                 cap->capabilities |= V4L2_CAP_TUNER;
619
620         return 0;
621 }
622
623 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
624                                         struct v4l2_fmtdesc *fmt)
625 {
626         char *desc = NULL;
627
628         switch (fmt->index) {
629         case 0:
630                 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
631                 desc = "Motion-JPEG";
632                 break;
633         case 1:
634                 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
635                 desc = "MPEG1/MPEG2/MPEG4";
636                 break;
637         default:
638                 return -EINVAL;
639         }
640         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
641         fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
642
643         strncpy(fmt->description, desc, sizeof(fmt->description));
644
645         return 0;
646 }
647
648 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
649                                         struct v4l2_format *fmt)
650 {
651         struct go7007 *go = ((struct go7007_file *) priv)->go;
652
653         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
654         fmt->fmt.pix.width = go->width;
655         fmt->fmt.pix.height = go->height;
656         fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
657                                    V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
658         fmt->fmt.pix.field = V4L2_FIELD_NONE;
659         fmt->fmt.pix.bytesperline = 0;
660         fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
661         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
662
663         return 0;
664 }
665
666 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
667                         struct v4l2_format *fmt)
668 {
669         struct go7007 *go = ((struct go7007_file *) priv)->go;
670
671         return set_capture_size(go, fmt, 1);
672 }
673
674 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
675                         struct v4l2_format *fmt)
676 {
677         struct go7007 *go = ((struct go7007_file *) priv)->go;
678
679         if (go->streaming)
680                 return -EBUSY;
681
682         return set_capture_size(go, fmt, 0);
683 }
684
685 static int vidioc_reqbufs(struct file *file, void *priv,
686                           struct v4l2_requestbuffers *req)
687 {
688         struct go7007_file *gofh = priv;
689         struct go7007 *go = gofh->go;
690         int retval = -EBUSY;
691         unsigned int count, i;
692
693         if (go->streaming)
694                 return retval;
695
696         if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
697                         req->memory != V4L2_MEMORY_MMAP)
698                 return -EINVAL;
699
700         mutex_lock(&gofh->lock);
701         for (i = 0; i < gofh->buf_count; ++i)
702                 if (gofh->bufs[i].mapped > 0)
703                         goto unlock_and_return;
704
705         mutex_lock(&go->hw_lock);
706         if (go->in_use > 0 && gofh->buf_count == 0) {
707                 mutex_unlock(&go->hw_lock);
708                 goto unlock_and_return;
709         }
710
711         if (gofh->buf_count > 0)
712                 kfree(gofh->bufs);
713
714         retval = -ENOMEM;
715         count = req->count;
716         if (count > 0) {
717                 if (count < 2)
718                         count = 2;
719                 if (count > 32)
720                         count = 32;
721
722                 gofh->bufs = kmalloc(count * sizeof(struct go7007_buffer),
723                                      GFP_KERNEL);
724
725                 if (!gofh->bufs) {
726                         mutex_unlock(&go->hw_lock);
727                         goto unlock_and_return;
728                 }
729
730                 memset(gofh->bufs, 0, count * sizeof(struct go7007_buffer));
731
732                 for (i = 0; i < count; ++i) {
733                         gofh->bufs[i].go = go;
734                         gofh->bufs[i].index = i;
735                         gofh->bufs[i].state = BUF_STATE_IDLE;
736                         gofh->bufs[i].mapped = 0;
737                 }
738
739                 go->in_use = 1;
740         } else {
741                 go->in_use = 0;
742         }
743
744         gofh->buf_count = count;
745         mutex_unlock(&go->hw_lock);
746         mutex_unlock(&gofh->lock);
747
748         memset(req, 0, sizeof(*req));
749
750         req->count = count;
751         req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
752         req->memory = V4L2_MEMORY_MMAP;
753
754         return 0;
755
756 unlock_and_return:
757         mutex_unlock(&gofh->lock);
758         return retval;
759 }
760
761 static int vidioc_querybuf(struct file *file, void *priv,
762                            struct v4l2_buffer *buf)
763 {
764         struct go7007_file *gofh = priv;
765         int retval = -EINVAL;
766         unsigned int index;
767
768         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
769                 return retval;
770
771         index = buf->index;
772
773         mutex_lock(&gofh->lock);
774         if (index >= gofh->buf_count)
775                 goto unlock_and_return;
776
777         memset(buf, 0, sizeof(*buf));
778         buf->index = index;
779         buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
780
781         switch (gofh->bufs[index].state) {
782         case BUF_STATE_QUEUED:
783                 buf->flags = V4L2_BUF_FLAG_QUEUED;
784                 break;
785         case BUF_STATE_DONE:
786                 buf->flags = V4L2_BUF_FLAG_DONE;
787                 break;
788         default:
789                 buf->flags = 0;
790         }
791
792         if (gofh->bufs[index].mapped)
793                 buf->flags |= V4L2_BUF_FLAG_MAPPED;
794         buf->memory = V4L2_MEMORY_MMAP;
795         buf->m.offset = index * GO7007_BUF_SIZE;
796         buf->length = GO7007_BUF_SIZE;
797         mutex_unlock(&gofh->lock);
798
799         return 0;
800
801 unlock_and_return:
802         mutex_unlock(&gofh->lock);
803         return retval;
804 }
805
806 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
807 {
808         struct go7007_file *gofh = priv;
809         struct go7007 *go = gofh->go;
810         struct go7007_buffer *gobuf;
811         unsigned long flags;
812         int retval = -EINVAL;
813         int ret;
814
815         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
816                         buf->memory != V4L2_MEMORY_MMAP)
817                 return retval;
818
819         mutex_lock(&gofh->lock);
820         if (buf->index < 0 || buf->index >= gofh->buf_count)
821                 goto unlock_and_return;
822
823         gobuf = &gofh->bufs[buf->index];
824         if (!gobuf->mapped)
825                 goto unlock_and_return;
826
827         retval = -EBUSY;
828         if (gobuf->state != BUF_STATE_IDLE)
829                 goto unlock_and_return;
830
831         /* offset will be 0 until we really support USERPTR streaming */
832         gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
833         gobuf->bytesused = 0;
834         gobuf->frame_offset = 0;
835         gobuf->modet_active = 0;
836         if (gobuf->offset > 0)
837                 gobuf->page_count = GO7007_BUF_PAGES + 1;
838         else
839                 gobuf->page_count = GO7007_BUF_PAGES;
840
841         retval = -ENOMEM;
842         down_read(&current->mm->mmap_sem);
843         ret = get_user_pages(current, current->mm,
844                         gobuf->user_addr & PAGE_MASK, gobuf->page_count,
845                         1, 1, gobuf->pages, NULL);
846         up_read(&current->mm->mmap_sem);
847
848         if (ret != gobuf->page_count) {
849                 int i;
850                 for (i = 0; i < ret; ++i)
851                         page_cache_release(gobuf->pages[i]);
852                 gobuf->page_count = 0;
853                 goto unlock_and_return;
854         }
855
856         gobuf->state = BUF_STATE_QUEUED;
857         spin_lock_irqsave(&go->spinlock, flags);
858         list_add_tail(&gobuf->stream, &go->stream);
859         spin_unlock_irqrestore(&go->spinlock, flags);
860         mutex_unlock(&gofh->lock);
861
862         return 0;
863
864 unlock_and_return:
865         mutex_unlock(&gofh->lock);
866         return retval;
867 }
868
869
870 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
871 {
872         struct go7007_file *gofh = priv;
873         struct go7007 *go = gofh->go;
874         struct go7007_buffer *gobuf;
875         int retval = -EINVAL;
876         unsigned long flags;
877         u32 frame_type_flag;
878         DEFINE_WAIT(wait);
879
880         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
881                 return retval;
882         if (buf->memory != V4L2_MEMORY_MMAP)
883                 return retval;
884
885         mutex_lock(&gofh->lock);
886         if (list_empty(&go->stream))
887                 goto unlock_and_return;
888         gobuf = list_entry(go->stream.next,
889                         struct go7007_buffer, stream);
890
891         retval = -EAGAIN;
892         if (gobuf->state != BUF_STATE_DONE &&
893                         !(file->f_flags & O_NONBLOCK)) {
894                 for (;;) {
895                         prepare_to_wait(&go->frame_waitq, &wait,
896                                         TASK_INTERRUPTIBLE);
897                         if (gobuf->state == BUF_STATE_DONE)
898                                 break;
899                         if (signal_pending(current)) {
900                                 retval = -ERESTARTSYS;
901                                 break;
902                         }
903                         schedule();
904                 }
905                 finish_wait(&go->frame_waitq, &wait);
906         }
907         if (gobuf->state != BUF_STATE_DONE)
908                 goto unlock_and_return;
909
910         spin_lock_irqsave(&go->spinlock, flags);
911         deactivate_buffer(gobuf);
912         spin_unlock_irqrestore(&go->spinlock, flags);
913         frame_type_flag = get_frame_type_flag(gobuf, go->format);
914         gobuf->state = BUF_STATE_IDLE;
915
916         memset(buf, 0, sizeof(*buf));
917         buf->index = gobuf->index;
918         buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
919         buf->bytesused = gobuf->bytesused;
920         buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
921         buf->field = V4L2_FIELD_NONE;
922         buf->timestamp = gobuf->timestamp;
923         buf->sequence = gobuf->seq;
924         buf->memory = V4L2_MEMORY_MMAP;
925         buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
926         buf->length = GO7007_BUF_SIZE;
927         buf->reserved = gobuf->modet_active;
928
929         mutex_unlock(&gofh->lock);
930         return 0;
931
932 unlock_and_return:
933         mutex_unlock(&gofh->lock);
934         return retval;
935 }
936
937 static int vidioc_streamon(struct file *file, void *priv,
938                                         enum v4l2_buf_type type)
939 {
940         struct go7007_file *gofh = priv;
941         struct go7007 *go = gofh->go;
942         int retval = 0;
943
944         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945                 return -EINVAL;
946
947         mutex_lock(&gofh->lock);
948         mutex_lock(&go->hw_lock);
949
950         if (!go->streaming) {
951                 go->streaming = 1;
952                 go->next_seq = 0;
953                 go->active_buf = NULL;
954                 if (go7007_start_encoder(go) < 0)
955                         retval = -EIO;
956                 else
957                         retval = 0;
958         }
959         mutex_unlock(&go->hw_lock);
960         mutex_unlock(&gofh->lock);
961
962         return retval;
963 }
964
965 static int vidioc_streamoff(struct file *file, void *priv,
966                                         enum v4l2_buf_type type)
967 {
968         struct go7007_file *gofh = priv;
969         struct go7007 *go = gofh->go;
970
971         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
972                 return -EINVAL;
973         mutex_lock(&gofh->lock);
974         go7007_streamoff(go);
975         mutex_unlock(&gofh->lock);
976
977         return 0;
978 }
979
980 static int vidioc_queryctrl(struct file *file, void *priv,
981                            struct v4l2_queryctrl *query)
982 {
983         struct go7007 *go = ((struct go7007_file *) priv)->go;
984         int id = query->id;
985
986         if (0 == call_all(&go->v4l2_dev, core, queryctrl, query))
987                 return 0;
988
989         query->id = id;
990         return mpeg_query_ctrl(query);
991 }
992
993 static int vidioc_g_ctrl(struct file *file, void *priv,
994                                 struct v4l2_control *ctrl)
995 {
996         struct go7007 *go = ((struct go7007_file *) priv)->go;
997
998         if (0 == call_all(&go->v4l2_dev, core, g_ctrl, ctrl))
999                 return 0;
1000
1001         return mpeg_g_ctrl(ctrl, go);
1002 }
1003
1004 static int vidioc_s_ctrl(struct file *file, void *priv,
1005                                 struct v4l2_control *ctrl)
1006 {
1007         struct go7007 *go = ((struct go7007_file *) priv)->go;
1008
1009         if (0 == call_all(&go->v4l2_dev, core, s_ctrl, ctrl))
1010                 return 0;
1011
1012         return mpeg_s_ctrl(ctrl, go);
1013 }
1014
1015 static int vidioc_g_parm(struct file *filp, void *priv,
1016                 struct v4l2_streamparm *parm)
1017 {
1018         struct go7007 *go = ((struct go7007_file *) priv)->go;
1019         struct v4l2_fract timeperframe = {
1020                 .numerator = 1001 *  go->fps_scale,
1021                 .denominator = go->sensor_framerate,
1022         };
1023
1024         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1025                 return -EINVAL;
1026
1027         parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1028         parm->parm.capture.timeperframe = timeperframe;
1029
1030         return 0;
1031 }
1032
1033 static int vidioc_s_parm(struct file *filp, void *priv,
1034                 struct v4l2_streamparm *parm)
1035 {
1036         struct go7007 *go = ((struct go7007_file *) priv)->go;
1037         unsigned int n, d;
1038
1039         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040                 return -EINVAL;
1041         if (parm->parm.capture.capturemode != 0)
1042                 return -EINVAL;
1043
1044         n = go->sensor_framerate *
1045                 parm->parm.capture.timeperframe.numerator;
1046         d = 1001 * parm->parm.capture.timeperframe.denominator;
1047         if (n != 0 && d != 0 && n > d)
1048                 go->fps_scale = (n + d/2) / d;
1049         else
1050                 go->fps_scale = 1;
1051
1052         return 0;
1053 }
1054
1055 /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
1056    its resolution, when the device is not connected to TV.
1057    This were an API abuse, probably used by the lack of specific IOCTL's to
1058    enumberate it, by the time the driver were written.
1059
1060    However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1061    and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1062
1063    The two functions bellow implements the newer ioctls
1064 */
1065 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1066                                   struct v4l2_frmsizeenum *fsize)
1067 {
1068         struct go7007 *go = ((struct go7007_file *) priv)->go;
1069
1070         /* Return -EINVAL, if it is a TV board */
1071         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1072             (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1073                 return -EINVAL;
1074
1075         if (fsize->index > 0)
1076                 return -EINVAL;
1077
1078         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1079         fsize->discrete.width = go->board_info->sensor_width;
1080         fsize->discrete.height = go->board_info->sensor_height;
1081
1082         return 0;
1083 }
1084
1085 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1086                                       struct v4l2_frmivalenum *fival)
1087 {
1088         struct go7007 *go = ((struct go7007_file *) priv)->go;
1089
1090         /* Return -EINVAL, if it is a TV board */
1091         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1092             (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1093                 return -EINVAL;
1094
1095         if (fival->index > 0)
1096                 return -EINVAL;
1097
1098         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1099         fival->discrete.numerator = 1001;
1100         fival->discrete.denominator = go->board_info->sensor_framerate;
1101
1102         return 0;
1103 }
1104
1105 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
1106 {
1107         struct go7007 *go = ((struct go7007_file *) priv)->go;
1108
1109         switch (go->standard) {
1110         case GO7007_STD_NTSC:
1111                 *std = V4L2_STD_NTSC;
1112                 break;
1113         case GO7007_STD_PAL:
1114                 *std = V4L2_STD_PAL;
1115                 break;
1116         default:
1117                 return -EINVAL;
1118         }
1119
1120         return 0;
1121 }
1122
1123 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1124 {
1125         struct go7007 *go = ((struct go7007_file *) priv)->go;
1126
1127         if (go->streaming)
1128                 return -EBUSY;
1129
1130         if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && *std != 0)
1131                 return -EINVAL;
1132
1133         if (*std == 0)
1134                 return -EINVAL;
1135
1136         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1137                         go->input == go->board_info->num_inputs - 1) {
1138                 if (!go->i2c_adapter_online)
1139                         return -EIO;
1140                 if (call_all(&go->v4l2_dev, core, s_std, *std) < 0)
1141                         return -EINVAL;
1142         }
1143
1144         if (*std & V4L2_STD_NTSC) {
1145                 go->standard = GO7007_STD_NTSC;
1146                 go->sensor_framerate = 30000;
1147         } else if (*std & V4L2_STD_PAL) {
1148                 go->standard = GO7007_STD_PAL;
1149                 go->sensor_framerate = 25025;
1150         } else if (*std & V4L2_STD_SECAM) {
1151                 go->standard = GO7007_STD_PAL;
1152                 go->sensor_framerate = 25025;
1153         } else
1154                 return -EINVAL;
1155
1156         call_all(&go->v4l2_dev, core, s_std, *std);
1157         set_capture_size(go, NULL, 0);
1158
1159         return 0;
1160 }
1161
1162 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
1163 {
1164         struct go7007 *go = ((struct go7007_file *) priv)->go;
1165
1166         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1167                         go->input == go->board_info->num_inputs - 1) {
1168                 if (!go->i2c_adapter_online)
1169                         return -EIO;
1170                 return call_all(&go->v4l2_dev, video, querystd, std);
1171         } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1172                 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1173         else
1174                 *std = 0;
1175
1176         return 0;
1177 }
1178
1179 static int vidioc_enum_input(struct file *file, void *priv,
1180                                 struct v4l2_input *inp)
1181 {
1182         struct go7007 *go = ((struct go7007_file *) priv)->go;
1183
1184         if (inp->index >= go->board_info->num_inputs)
1185                 return -EINVAL;
1186
1187         strncpy(inp->name, go->board_info->inputs[inp->index].name,
1188                         sizeof(inp->name));
1189
1190         /* If this board has a tuner, it will be the last input */
1191         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1192                         inp->index == go->board_info->num_inputs - 1)
1193                 inp->type = V4L2_INPUT_TYPE_TUNER;
1194         else
1195                 inp->type = V4L2_INPUT_TYPE_CAMERA;
1196
1197         inp->audioset = 0;
1198         inp->tuner = 0;
1199         if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1200                 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1201                                                 V4L2_STD_SECAM;
1202         else
1203                 inp->std = 0;
1204
1205         return 0;
1206 }
1207
1208
1209 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1210 {
1211         struct go7007 *go = ((struct go7007_file *) priv)->go;
1212
1213         *input = go->input;
1214
1215         return 0;
1216 }
1217
1218 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1219 {
1220         struct go7007 *go = ((struct go7007_file *) priv)->go;
1221
1222         if (input >= go->board_info->num_inputs)
1223                 return -EINVAL;
1224         if (go->streaming)
1225                 return -EBUSY;
1226
1227         go->input = input;
1228
1229         return call_all(&go->v4l2_dev, video, s_routing, input, 0, 0);
1230 }
1231
1232 static int vidioc_g_tuner(struct file *file, void *priv,
1233                                 struct v4l2_tuner *t)
1234 {
1235         struct go7007 *go = ((struct go7007_file *) priv)->go;
1236
1237         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1238                 return -EINVAL;
1239         if (t->index != 0)
1240                 return -EINVAL;
1241         if (!go->i2c_adapter_online)
1242                 return -EIO;
1243
1244         return call_all(&go->v4l2_dev, tuner, g_tuner, t);
1245 }
1246
1247 static int vidioc_s_tuner(struct file *file, void *priv,
1248                                 struct v4l2_tuner *t)
1249 {
1250         struct go7007 *go = ((struct go7007_file *) priv)->go;
1251
1252         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1253                 return -EINVAL;
1254         if (t->index != 0)
1255                 return -EINVAL;
1256         if (!go->i2c_adapter_online)
1257                 return -EIO;
1258
1259         switch (go->board_id) {
1260         case GO7007_BOARDID_PX_TV402U_NA:
1261         case GO7007_BOARDID_PX_TV402U_JP:
1262                 /* No selectable options currently */
1263                 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1264                         return -EINVAL;
1265                 break;
1266         }
1267
1268         return call_all(&go->v4l2_dev, tuner, s_tuner, t);
1269 }
1270
1271 static int vidioc_g_frequency(struct file *file, void *priv,
1272                                 struct v4l2_frequency *f)
1273 {
1274         struct go7007 *go = ((struct go7007_file *) priv)->go;
1275
1276         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1277                 return -EINVAL;
1278         if (!go->i2c_adapter_online)
1279                 return -EIO;
1280
1281         f->type = V4L2_TUNER_ANALOG_TV;
1282
1283         return call_all(&go->v4l2_dev, tuner, g_frequency, f);
1284 }
1285
1286 static int vidioc_s_frequency(struct file *file, void *priv,
1287                                 struct v4l2_frequency *f)
1288 {
1289         struct go7007 *go = ((struct go7007_file *) priv)->go;
1290
1291         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1292                 return -EINVAL;
1293         if (!go->i2c_adapter_online)
1294                 return -EIO;
1295
1296         return call_all(&go->v4l2_dev, tuner, s_frequency, f);
1297 }
1298
1299 static int vidioc_cropcap(struct file *file, void *priv,
1300                                         struct v4l2_cropcap *cropcap)
1301 {
1302         struct go7007 *go = ((struct go7007_file *) priv)->go;
1303
1304         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1305                 return -EINVAL;
1306
1307         /* These specify the raw input of the sensor */
1308         switch (go->standard) {
1309         case GO7007_STD_NTSC:
1310                 cropcap->bounds.top = 0;
1311                 cropcap->bounds.left = 0;
1312                 cropcap->bounds.width = 720;
1313                 cropcap->bounds.height = 480;
1314                 cropcap->defrect.top = 0;
1315                 cropcap->defrect.left = 0;
1316                 cropcap->defrect.width = 720;
1317                 cropcap->defrect.height = 480;
1318                 break;
1319         case GO7007_STD_PAL:
1320                 cropcap->bounds.top = 0;
1321                 cropcap->bounds.left = 0;
1322                 cropcap->bounds.width = 720;
1323                 cropcap->bounds.height = 576;
1324                 cropcap->defrect.top = 0;
1325                 cropcap->defrect.left = 0;
1326                 cropcap->defrect.width = 720;
1327                 cropcap->defrect.height = 576;
1328                 break;
1329         case GO7007_STD_OTHER:
1330                 cropcap->bounds.top = 0;
1331                 cropcap->bounds.left = 0;
1332                 cropcap->bounds.width = go->board_info->sensor_width;
1333                 cropcap->bounds.height = go->board_info->sensor_height;
1334                 cropcap->defrect.top = 0;
1335                 cropcap->defrect.left = 0;
1336                 cropcap->defrect.width = go->board_info->sensor_width;
1337                 cropcap->defrect.height = go->board_info->sensor_height;
1338                 break;
1339         }
1340
1341         return 0;
1342 }
1343
1344 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1345 {
1346         struct go7007 *go = ((struct go7007_file *) priv)->go;
1347
1348         if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1349                 return -EINVAL;
1350
1351         crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1352
1353         /* These specify the raw input of the sensor */
1354         switch (go->standard) {
1355         case GO7007_STD_NTSC:
1356                 crop->c.top = 0;
1357                 crop->c.left = 0;
1358                 crop->c.width = 720;
1359                 crop->c.height = 480;
1360                 break;
1361         case GO7007_STD_PAL:
1362                 crop->c.top = 0;
1363                 crop->c.left = 0;
1364                 crop->c.width = 720;
1365                 crop->c.height = 576;
1366                 break;
1367         case GO7007_STD_OTHER:
1368                 crop->c.top = 0;
1369                 crop->c.left = 0;
1370                 crop->c.width = go->board_info->sensor_width;
1371                 crop->c.height = go->board_info->sensor_height;
1372                 break;
1373         }
1374
1375         return 0;
1376 }
1377
1378 /* FIXME: vidioc_s_crop is not really implemented!!!
1379  */
1380 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1381 {
1382         if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1383                 return -EINVAL;
1384
1385         return 0;
1386 }
1387
1388 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1389                          struct v4l2_jpegcompression *params)
1390 {
1391         memset(params, 0, sizeof(*params));
1392         params->quality = 50; /* ?? */
1393         params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1394                                 V4L2_JPEG_MARKER_DQT;
1395
1396         return 0;
1397 }
1398
1399 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1400                          struct v4l2_jpegcompression *params)
1401 {
1402         if (params->quality != 50 ||
1403                         params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1404                                                 V4L2_JPEG_MARKER_DQT))
1405                 return -EINVAL;
1406
1407         return 0;
1408 }
1409
1410 /* FIXME:
1411         Those ioctls are private, and not needed, since several standard
1412         extended controls already provide streaming control.
1413         So, those ioctls should be converted into vidioc_g_ext_ctrls()
1414         and vidioc_s_ext_ctrls()
1415  */
1416
1417 #if 0
1418         /* Temporary ioctls for controlling compression characteristics */
1419         case GO7007IOC_S_BITRATE:
1420         {
1421                 int *bitrate = arg;
1422
1423                 if (go->streaming)
1424                         return -EINVAL;
1425                 /* Upper bound is kind of arbitrary here */
1426                 if (*bitrate < 64000 || *bitrate > 10000000)
1427                         return -EINVAL;
1428                 go->bitrate = *bitrate;
1429                 return 0;
1430         }
1431         case GO7007IOC_G_BITRATE:
1432         {
1433                 int *bitrate = arg;
1434
1435                 *bitrate = go->bitrate;
1436                 return 0;
1437         }
1438         case GO7007IOC_S_COMP_PARAMS:
1439         {
1440                 struct go7007_comp_params *comp = arg;
1441
1442                 if (go->format == GO7007_FORMAT_MJPEG)
1443                         return -EINVAL;
1444                 if (comp->gop_size > 0)
1445                         go->gop_size = comp->gop_size;
1446                 else
1447                         go->gop_size = go->sensor_framerate / 1000;
1448                 if (go->gop_size != 15)
1449                         go->dvd_mode = 0;
1450                 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1451                 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1452                         switch (comp->aspect_ratio) {
1453                         case GO7007_ASPECT_RATIO_4_3_NTSC:
1454                         case GO7007_ASPECT_RATIO_4_3_PAL:
1455                                 go->aspect_ratio = GO7007_RATIO_4_3;
1456                                 break;
1457                         case GO7007_ASPECT_RATIO_16_9_NTSC:
1458                         case GO7007_ASPECT_RATIO_16_9_PAL:
1459                                 go->aspect_ratio = GO7007_RATIO_16_9;
1460                                 break;
1461                         default:
1462                                 go->aspect_ratio = GO7007_RATIO_1_1;
1463                                 break;
1464                         }
1465                 }
1466                 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1467                         go->dvd_mode = 0;
1468                         go->seq_header_enable = 0;
1469                 } else {
1470                         go->seq_header_enable = 1;
1471                 }
1472                 /* fall-through */
1473         }
1474         case GO7007IOC_G_COMP_PARAMS:
1475         {
1476                 struct go7007_comp_params *comp = arg;
1477
1478                 if (go->format == GO7007_FORMAT_MJPEG)
1479                         return -EINVAL;
1480                 memset(comp, 0, sizeof(*comp));
1481                 comp->gop_size = go->gop_size;
1482                 comp->max_b_frames = go->ipb ? 2 : 0;
1483                 switch (go->aspect_ratio) {
1484                 case GO7007_RATIO_4_3:
1485                         if (go->standard == GO7007_STD_NTSC)
1486                                 comp->aspect_ratio =
1487                                         GO7007_ASPECT_RATIO_4_3_NTSC;
1488                         else
1489                                 comp->aspect_ratio =
1490                                         GO7007_ASPECT_RATIO_4_3_PAL;
1491                         break;
1492                 case GO7007_RATIO_16_9:
1493                         if (go->standard == GO7007_STD_NTSC)
1494                                 comp->aspect_ratio =
1495                                         GO7007_ASPECT_RATIO_16_9_NTSC;
1496                         else
1497                                 comp->aspect_ratio =
1498                                         GO7007_ASPECT_RATIO_16_9_PAL;
1499                         break;
1500                 default:
1501                         comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1502                         break;
1503                 }
1504                 if (go->closed_gop)
1505                         comp->flags |= GO7007_COMP_CLOSED_GOP;
1506                 if (!go->seq_header_enable)
1507                         comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1508                 return 0;
1509         }
1510         case GO7007IOC_S_MPEG_PARAMS:
1511         {
1512                 struct go7007_mpeg_params *mpeg = arg;
1513
1514                 if (go->format != GO7007_FORMAT_MPEG1 &&
1515                                 go->format != GO7007_FORMAT_MPEG2 &&
1516                                 go->format != GO7007_FORMAT_MPEG4)
1517                         return -EINVAL;
1518
1519                 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1520                         go->format = GO7007_FORMAT_MPEG2;
1521                         go->bitrate = 9800000;
1522                         go->gop_size = 15;
1523                         go->pali = 0x48;
1524                         go->closed_gop = 1;
1525                         go->repeat_seqhead = 0;
1526                         go->seq_header_enable = 1;
1527                         go->gop_header_enable = 1;
1528                         go->dvd_mode = 1;
1529                 } else {
1530                         switch (mpeg->mpeg_video_standard) {
1531                         case GO7007_MPEG_VIDEO_MPEG1:
1532                                 go->format = GO7007_FORMAT_MPEG1;
1533                                 go->pali = 0;
1534                                 break;
1535                         case GO7007_MPEG_VIDEO_MPEG2:
1536                                 go->format = GO7007_FORMAT_MPEG2;
1537                                 if (mpeg->pali >> 24 == 2)
1538                                         go->pali = mpeg->pali & 0xff;
1539                                 else
1540                                         go->pali = 0x48;
1541                                 break;
1542                         case GO7007_MPEG_VIDEO_MPEG4:
1543                                 go->format = GO7007_FORMAT_MPEG4;
1544                                 if (mpeg->pali >> 24 == 4)
1545                                         go->pali = mpeg->pali & 0xff;
1546                                 else
1547                                         go->pali = 0xf5;
1548                                 break;
1549                         default:
1550                                 return -EINVAL;
1551                         }
1552                         go->gop_header_enable =
1553                                 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1554                                 ? 0 : 1;
1555                         if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1556                                 go->repeat_seqhead = 1;
1557                         else
1558                                 go->repeat_seqhead = 0;
1559                         go->dvd_mode = 0;
1560                 }
1561                 /* fall-through */
1562         }
1563         case GO7007IOC_G_MPEG_PARAMS:
1564         {
1565                 struct go7007_mpeg_params *mpeg = arg;
1566
1567                 memset(mpeg, 0, sizeof(*mpeg));
1568                 switch (go->format) {
1569                 case GO7007_FORMAT_MPEG1:
1570                         mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1571                         mpeg->pali = 0;
1572                         break;
1573                 case GO7007_FORMAT_MPEG2:
1574                         mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1575                         mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1576                         break;
1577                 case GO7007_FORMAT_MPEG4:
1578                         mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1579                         mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1580                         break;
1581                 default:
1582                         return -EINVAL;
1583                 }
1584                 if (!go->gop_header_enable)
1585                         mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1586                 if (go->repeat_seqhead)
1587                         mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1588                 if (go->dvd_mode)
1589                         mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1590                 return 0;
1591         }
1592         case GO7007IOC_S_MD_PARAMS:
1593         {
1594                 struct go7007_md_params *mdp = arg;
1595
1596                 if (mdp->region > 3)
1597                         return -EINVAL;
1598                 if (mdp->trigger > 0) {
1599                         go->modet[mdp->region].pixel_threshold =
1600                                         mdp->pixel_threshold >> 1;
1601                         go->modet[mdp->region].motion_threshold =
1602                                         mdp->motion_threshold >> 1;
1603                         go->modet[mdp->region].mb_threshold =
1604                                         mdp->trigger >> 1;
1605                         go->modet[mdp->region].enable = 1;
1606                 } else
1607                         go->modet[mdp->region].enable = 0;
1608                 /* fall-through */
1609         }
1610         case GO7007IOC_G_MD_PARAMS:
1611         {
1612                 struct go7007_md_params *mdp = arg;
1613                 int region = mdp->region;
1614
1615                 if (mdp->region > 3)
1616                         return -EINVAL;
1617                 memset(mdp, 0, sizeof(struct go7007_md_params));
1618                 mdp->region = region;
1619                 if (!go->modet[region].enable)
1620                         return 0;
1621                 mdp->pixel_threshold =
1622                         (go->modet[region].pixel_threshold << 1) + 1;
1623                 mdp->motion_threshold =
1624                         (go->modet[region].motion_threshold << 1) + 1;
1625                 mdp->trigger =
1626                         (go->modet[region].mb_threshold << 1) + 1;
1627                 return 0;
1628         }
1629         case GO7007IOC_S_MD_REGION:
1630         {
1631                 struct go7007_md_region *region = arg;
1632
1633                 if (region->region < 1 || region->region > 3)
1634                         return -EINVAL;
1635                 return clip_to_modet_map(go, region->region, region->clips);
1636         }
1637 #endif
1638
1639 static ssize_t go7007_read(struct file *file, char __user *data,
1640                 size_t count, loff_t *ppos)
1641 {
1642         return -EINVAL;
1643 }
1644
1645 static void go7007_vm_open(struct vm_area_struct *vma)
1646 {
1647         struct go7007_buffer *gobuf = vma->vm_private_data;
1648
1649         ++gobuf->mapped;
1650 }
1651
1652 static void go7007_vm_close(struct vm_area_struct *vma)
1653 {
1654         struct go7007_buffer *gobuf = vma->vm_private_data;
1655         unsigned long flags;
1656
1657         if (--gobuf->mapped == 0) {
1658                 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1659                 deactivate_buffer(gobuf);
1660                 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1661         }
1662 }
1663
1664 /* Copied from videobuf-dma-sg.c */
1665 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1666 {
1667         struct page *page;
1668
1669         page = alloc_page(GFP_USER | __GFP_DMA32);
1670         if (!page)
1671                 return VM_FAULT_OOM;
1672         clear_user_highpage(page, (unsigned long)vmf->virtual_address);
1673         vmf->page = page;
1674         return 0;
1675 }
1676
1677 static struct vm_operations_struct go7007_vm_ops = {
1678         .open   = go7007_vm_open,
1679         .close  = go7007_vm_close,
1680         .fault  = go7007_vm_fault,
1681 };
1682
1683 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1684 {
1685         struct go7007_file *gofh = file->private_data;
1686         unsigned int index;
1687
1688         if (gofh->go->status != STATUS_ONLINE)
1689                 return -EIO;
1690         if (!(vma->vm_flags & VM_SHARED))
1691                 return -EINVAL; /* only support VM_SHARED mapping */
1692         if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1693                 return -EINVAL; /* must map exactly one full buffer */
1694         mutex_lock(&gofh->lock);
1695         index = vma->vm_pgoff / GO7007_BUF_PAGES;
1696         if (index >= gofh->buf_count) {
1697                 mutex_unlock(&gofh->lock);
1698                 return -EINVAL; /* trying to map beyond requested buffers */
1699         }
1700         if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1701                 mutex_unlock(&gofh->lock);
1702                 return -EINVAL; /* offset is not aligned on buffer boundary */
1703         }
1704         if (gofh->bufs[index].mapped > 0) {
1705                 mutex_unlock(&gofh->lock);
1706                 return -EBUSY;
1707         }
1708         gofh->bufs[index].mapped = 1;
1709         gofh->bufs[index].user_addr = vma->vm_start;
1710         vma->vm_ops = &go7007_vm_ops;
1711         vma->vm_flags |= VM_DONTEXPAND;
1712         vma->vm_flags &= ~VM_IO;
1713         vma->vm_private_data = &gofh->bufs[index];
1714         mutex_unlock(&gofh->lock);
1715         return 0;
1716 }
1717
1718 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1719 {
1720         struct go7007_file *gofh = file->private_data;
1721         struct go7007_buffer *gobuf;
1722
1723         if (list_empty(&gofh->go->stream))
1724                 return POLLERR;
1725         gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1726         poll_wait(file, &gofh->go->frame_waitq, wait);
1727         if (gobuf->state == BUF_STATE_DONE)
1728                 return POLLIN | POLLRDNORM;
1729         return 0;
1730 }
1731
1732 static void go7007_vfl_release(struct video_device *vfd)
1733 {
1734         struct go7007 *go = video_get_drvdata(vfd);
1735
1736         video_device_release(vfd);
1737         if (--go->ref_count == 0)
1738                 kfree(go);
1739 }
1740
1741 static struct v4l2_file_operations go7007_fops = {
1742         .owner          = THIS_MODULE,
1743         .open           = go7007_open,
1744         .release        = go7007_release,
1745         .ioctl          = video_ioctl2,
1746         .read           = go7007_read,
1747         .mmap           = go7007_mmap,
1748         .poll           = go7007_poll,
1749 };
1750
1751 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1752         .vidioc_querycap          = vidioc_querycap,
1753         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1754         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1755         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1756         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1757         .vidioc_reqbufs           = vidioc_reqbufs,
1758         .vidioc_querybuf          = vidioc_querybuf,
1759         .vidioc_qbuf              = vidioc_qbuf,
1760         .vidioc_dqbuf             = vidioc_dqbuf,
1761         .vidioc_g_std             = vidioc_g_std,
1762         .vidioc_s_std             = vidioc_s_std,
1763         .vidioc_querystd          = vidioc_querystd,
1764         .vidioc_enum_input        = vidioc_enum_input,
1765         .vidioc_g_input           = vidioc_g_input,
1766         .vidioc_s_input           = vidioc_s_input,
1767         .vidioc_queryctrl         = vidioc_queryctrl,
1768         .vidioc_g_ctrl            = vidioc_g_ctrl,
1769         .vidioc_s_ctrl            = vidioc_s_ctrl,
1770         .vidioc_streamon          = vidioc_streamon,
1771         .vidioc_streamoff         = vidioc_streamoff,
1772         .vidioc_g_tuner           = vidioc_g_tuner,
1773         .vidioc_s_tuner           = vidioc_s_tuner,
1774         .vidioc_g_frequency       = vidioc_g_frequency,
1775         .vidioc_s_frequency       = vidioc_s_frequency,
1776         .vidioc_g_parm            = vidioc_g_parm,
1777         .vidioc_s_parm            = vidioc_s_parm,
1778         .vidioc_enum_framesizes   = vidioc_enum_framesizes,
1779         .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1780         .vidioc_cropcap           = vidioc_cropcap,
1781         .vidioc_g_crop            = vidioc_g_crop,
1782         .vidioc_s_crop            = vidioc_s_crop,
1783         .vidioc_g_jpegcomp        = vidioc_g_jpegcomp,
1784         .vidioc_s_jpegcomp        = vidioc_s_jpegcomp,
1785 };
1786
1787 static struct video_device go7007_template = {
1788         .name           = "go7007",
1789         .fops           = &go7007_fops,
1790         .release        = go7007_vfl_release,
1791         .ioctl_ops      = &video_ioctl_ops,
1792         .tvnorms        = V4L2_STD_ALL,
1793         .current_norm   = V4L2_STD_NTSC,
1794 };
1795
1796 int go7007_v4l2_init(struct go7007 *go)
1797 {
1798         int rv;
1799
1800         go->video_dev = video_device_alloc();
1801         if (go->video_dev == NULL)
1802                 return -ENOMEM;
1803         *go->video_dev = go7007_template;
1804         go->video_dev->parent = go->dev;
1805         rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1806         if (rv < 0) {
1807                 video_device_release(go->video_dev);
1808                 go->video_dev = NULL;
1809                 return rv;
1810         }
1811         rv = v4l2_device_register(go->dev, &go->v4l2_dev);
1812         if (rv < 0) {
1813                 video_device_release(go->video_dev);
1814                 go->video_dev = NULL;
1815                 return rv;
1816         }
1817         video_set_drvdata(go->video_dev, go);
1818         ++go->ref_count;
1819         printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1820                go->video_dev->name, video_device_node_name(go->video_dev));
1821
1822         return 0;
1823 }
1824
1825 void go7007_v4l2_remove(struct go7007 *go)
1826 {
1827         unsigned long flags;
1828
1829         mutex_lock(&go->hw_lock);
1830         if (go->streaming) {
1831                 go->streaming = 0;
1832                 go7007_stream_stop(go);
1833                 spin_lock_irqsave(&go->spinlock, flags);
1834                 abort_queued(go);
1835                 spin_unlock_irqrestore(&go->spinlock, flags);
1836         }
1837         mutex_unlock(&go->hw_lock);
1838         if (go->video_dev)
1839                 video_unregister_device(go->video_dev);
1840         v4l2_device_unregister(&go->v4l2_dev);
1841 }