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