V4L/DVB (10271): saa7146: convert to video_ioctl2.
[pandora-kernel.git] / drivers / media / common / saa7146_video.c
1 #include <media/saa7146_vv.h>
2
3 static int max_memory = 32;
4
5 module_param(max_memory, int, 0644);
6 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
7
8 #define IS_CAPTURE_ACTIVE(fh) \
9         (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
10
11 #define IS_OVERLAY_ACTIVE(fh) \
12         (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
13
14 /* format descriptions for capture and preview */
15 static struct saa7146_format formats[] = {
16         {
17                 .name           = "RGB-8 (3-3-2)",
18                 .pixelformat    = V4L2_PIX_FMT_RGB332,
19                 .trans          = RGB08_COMPOSED,
20                 .depth          = 8,
21                 .flags          = 0,
22         }, {
23                 .name           = "RGB-16 (5/B-6/G-5/R)",
24                 .pixelformat    = V4L2_PIX_FMT_RGB565,
25                 .trans          = RGB16_COMPOSED,
26                 .depth          = 16,
27                 .flags          = 0,
28         }, {
29                 .name           = "RGB-24 (B-G-R)",
30                 .pixelformat    = V4L2_PIX_FMT_BGR24,
31                 .trans          = RGB24_COMPOSED,
32                 .depth          = 24,
33                 .flags          = 0,
34         }, {
35                 .name           = "RGB-32 (B-G-R)",
36                 .pixelformat    = V4L2_PIX_FMT_BGR32,
37                 .trans          = RGB32_COMPOSED,
38                 .depth          = 32,
39                 .flags          = 0,
40         }, {
41                 .name           = "RGB-32 (R-G-B)",
42                 .pixelformat    = V4L2_PIX_FMT_RGB32,
43                 .trans          = RGB32_COMPOSED,
44                 .depth          = 32,
45                 .flags          = 0,
46                 .swap           = 0x2,
47         }, {
48                 .name           = "Greyscale-8",
49                 .pixelformat    = V4L2_PIX_FMT_GREY,
50                 .trans          = Y8,
51                 .depth          = 8,
52                 .flags          = 0,
53         }, {
54                 .name           = "YUV 4:2:2 planar (Y-Cb-Cr)",
55                 .pixelformat    = V4L2_PIX_FMT_YUV422P,
56                 .trans          = YUV422_DECOMPOSED,
57                 .depth          = 16,
58                 .flags          = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
59         }, {
60                 .name           = "YVU 4:2:0 planar (Y-Cb-Cr)",
61                 .pixelformat    = V4L2_PIX_FMT_YVU420,
62                 .trans          = YUV420_DECOMPOSED,
63                 .depth          = 12,
64                 .flags          = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
65         }, {
66                 .name           = "YUV 4:2:0 planar (Y-Cb-Cr)",
67                 .pixelformat    = V4L2_PIX_FMT_YUV420,
68                 .trans          = YUV420_DECOMPOSED,
69                 .depth          = 12,
70                 .flags          = FORMAT_IS_PLANAR,
71         }, {
72                 .name           = "YUV 4:2:2 (U-Y-V-Y)",
73                 .pixelformat    = V4L2_PIX_FMT_UYVY,
74                 .trans          = YUV422_COMPOSED,
75                 .depth          = 16,
76                 .flags          = 0,
77         }
78 };
79
80 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
81    due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
82    (like V4L2_PIX_FMT_YUYV) ... 8-( */
83
84 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
85
86 struct saa7146_format* format_by_fourcc(struct saa7146_dev *dev, int fourcc)
87 {
88         int i, j = NUM_FORMATS;
89
90         for (i = 0; i < j; i++) {
91                 if (formats[i].pixelformat == fourcc) {
92                         return formats+i;
93                 }
94         }
95
96         DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
97         return NULL;
98 }
99
100 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
101
102 int saa7146_start_preview(struct saa7146_fh *fh)
103 {
104         struct saa7146_dev *dev = fh->dev;
105         struct saa7146_vv *vv = dev->vv_data;
106         struct v4l2_format fmt;
107         int ret = 0, err = 0;
108
109         DEB_EE(("dev:%p, fh:%p\n",dev,fh));
110
111         /* check if we have overlay informations */
112         if( NULL == fh->ov.fh ) {
113                 DEB_D(("no overlay data available. try S_FMT first.\n"));
114                 return -EAGAIN;
115         }
116
117         /* check if streaming capture is running */
118         if (IS_CAPTURE_ACTIVE(fh) != 0) {
119                 DEB_D(("streaming capture is active.\n"));
120                 return -EBUSY;
121         }
122
123         /* check if overlay is running */
124         if (IS_OVERLAY_ACTIVE(fh) != 0) {
125                 if (vv->video_fh == fh) {
126                         DEB_D(("overlay is already active.\n"));
127                         return 0;
128                 }
129                 DEB_D(("overlay is already active in another open.\n"));
130                 return -EBUSY;
131         }
132
133         if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
134                 DEB_D(("cannot get necessary overlay resources\n"));
135                 return -EBUSY;
136         }
137
138         fmt.fmt.win = fh->ov.win;
139         err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
140         if (0 != err) {
141                 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
142                 return -EBUSY;
143         }
144         fh->ov.win = fmt.fmt.win;
145         vv->ov_data = &fh->ov;
146
147         DEB_D(("%dx%d+%d+%d %s field=%s\n",
148                 fh->ov.win.w.width,fh->ov.win.w.height,
149                 fh->ov.win.w.left,fh->ov.win.w.top,
150                 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
151
152         if (0 != (ret = saa7146_enable_overlay(fh))) {
153                 DEB_D(("enabling overlay failed: %d\n",ret));
154                 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
155                 return ret;
156         }
157
158         vv->video_status = STATUS_OVERLAY;
159         vv->video_fh = fh;
160
161         return 0;
162 }
163 EXPORT_SYMBOL_GPL(saa7146_start_preview);
164
165 int saa7146_stop_preview(struct saa7146_fh *fh)
166 {
167         struct saa7146_dev *dev = fh->dev;
168         struct saa7146_vv *vv = dev->vv_data;
169
170         DEB_EE(("dev:%p, fh:%p\n",dev,fh));
171
172         /* check if streaming capture is running */
173         if (IS_CAPTURE_ACTIVE(fh) != 0) {
174                 DEB_D(("streaming capture is active.\n"));
175                 return -EBUSY;
176         }
177
178         /* check if overlay is running at all */
179         if ((vv->video_status & STATUS_OVERLAY) == 0) {
180                 DEB_D(("no active overlay.\n"));
181                 return 0;
182         }
183
184         if (vv->video_fh != fh) {
185                 DEB_D(("overlay is active, but in another open.\n"));
186                 return -EBUSY;
187         }
188
189         vv->video_status = 0;
190         vv->video_fh = NULL;
191
192         saa7146_disable_overlay(fh);
193
194         saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
195
196         return 0;
197 }
198 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
199
200 /********************************************************************************/
201 /* device controls */
202
203 static struct v4l2_queryctrl controls[] = {
204         {
205                 .id             = V4L2_CID_BRIGHTNESS,
206                 .name           = "Brightness",
207                 .minimum        = 0,
208                 .maximum        = 255,
209                 .step           = 1,
210                 .default_value  = 128,
211                 .type           = V4L2_CTRL_TYPE_INTEGER,
212         },{
213                 .id             = V4L2_CID_CONTRAST,
214                 .name           = "Contrast",
215                 .minimum        = 0,
216                 .maximum        = 127,
217                 .step           = 1,
218                 .default_value  = 64,
219                 .type           = V4L2_CTRL_TYPE_INTEGER,
220         },{
221                 .id             = V4L2_CID_SATURATION,
222                 .name           = "Saturation",
223                 .minimum        = 0,
224                 .maximum        = 127,
225                 .step           = 1,
226                 .default_value  = 64,
227                 .type           = V4L2_CTRL_TYPE_INTEGER,
228         },{
229                 .id             = V4L2_CID_VFLIP,
230                 .name           = "Vertical flip",
231                 .minimum        = 0,
232                 .maximum        = 1,
233                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
234         },{
235                 .id             = V4L2_CID_HFLIP,
236                 .name           = "Horizontal flip",
237                 .minimum        = 0,
238                 .maximum        = 1,
239                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
240         },
241 };
242 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
243
244 #define V4L2_CID_PRIVATE_LASTP1      (V4L2_CID_PRIVATE_BASE + 0)
245
246 static struct v4l2_queryctrl* ctrl_by_id(int id)
247 {
248         int i;
249
250         for (i = 0; i < NUM_CONTROLS; i++)
251                 if (controls[i].id == id)
252                         return controls+i;
253         return NULL;
254 }
255
256 /********************************************************************************/
257 /* common pagetable functions */
258
259 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
260 {
261         struct pci_dev *pci = dev->pci;
262         struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
263         struct scatterlist *list = dma->sglist;
264         int length = dma->sglen;
265         struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
266
267         DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
268
269         if( 0 != IS_PLANAR(sfmt->trans)) {
270                 struct saa7146_pgtable *pt1 = &buf->pt[0];
271                 struct saa7146_pgtable *pt2 = &buf->pt[1];
272                 struct saa7146_pgtable *pt3 = &buf->pt[2];
273                 __le32  *ptr1, *ptr2, *ptr3;
274                 __le32 fill;
275
276                 int size = buf->fmt->width*buf->fmt->height;
277                 int i,p,m1,m2,m3,o1,o2;
278
279                 switch( sfmt->depth ) {
280                         case 12: {
281                                 /* create some offsets inside the page table */
282                                 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
283                                 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
284                                 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
285                                 o1 = size%PAGE_SIZE;
286                                 o2 = (size+(size/4))%PAGE_SIZE;
287                                 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
288                                 break;
289                         }
290                         case 16: {
291                                 /* create some offsets inside the page table */
292                                 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
293                                 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
294                                 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
295                                 o1 = size%PAGE_SIZE;
296                                 o2 = (size+(size/2))%PAGE_SIZE;
297                                 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
298                                 break;
299                         }
300                         default: {
301                                 return -1;
302                         }
303                 }
304
305                 ptr1 = pt1->cpu;
306                 ptr2 = pt2->cpu;
307                 ptr3 = pt3->cpu;
308
309                 /* walk all pages, copy all page addresses to ptr1 */
310                 for (i = 0; i < length; i++, list++) {
311                         for (p = 0; p * 4096 < list->length; p++, ptr1++) {
312                                 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
313                         }
314                 }
315 /*
316                 ptr1 = pt1->cpu;
317                 for(j=0;j<40;j++) {
318                         printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
319                 }
320 */
321
322                 /* if we have a user buffer, the first page may not be
323                    aligned to a page boundary. */
324                 pt1->offset = dma->sglist->offset;
325                 pt2->offset = pt1->offset+o1;
326                 pt3->offset = pt1->offset+o2;
327
328                 /* create video-dma2 page table */
329                 ptr1 = pt1->cpu;
330                 for(i = m1; i <= m2 ; i++, ptr2++) {
331                         *ptr2 = ptr1[i];
332                 }
333                 fill = *(ptr2-1);
334                 for(;i<1024;i++,ptr2++) {
335                         *ptr2 = fill;
336                 }
337                 /* create video-dma3 page table */
338                 ptr1 = pt1->cpu;
339                 for(i = m2; i <= m3; i++,ptr3++) {
340                         *ptr3 = ptr1[i];
341                 }
342                 fill = *(ptr3-1);
343                 for(;i<1024;i++,ptr3++) {
344                         *ptr3 = fill;
345                 }
346                 /* finally: finish up video-dma1 page table */
347                 ptr1 = pt1->cpu+m1;
348                 fill = pt1->cpu[m1];
349                 for(i=m1;i<1024;i++,ptr1++) {
350                         *ptr1 = fill;
351                 }
352 /*
353                 ptr1 = pt1->cpu;
354                 ptr2 = pt2->cpu;
355                 ptr3 = pt3->cpu;
356                 for(j=0;j<40;j++) {
357                         printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
358                 }
359                 for(j=0;j<40;j++) {
360                         printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
361                 }
362                 for(j=0;j<40;j++) {
363                         printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
364                 }
365 */
366         } else {
367                 struct saa7146_pgtable *pt = &buf->pt[0];
368                 return saa7146_pgtable_build_single(pci, pt, list, length);
369         }
370
371         return 0;
372 }
373
374
375 /********************************************************************************/
376 /* file operations */
377
378 static int video_begin(struct saa7146_fh *fh)
379 {
380         struct saa7146_dev *dev = fh->dev;
381         struct saa7146_vv *vv = dev->vv_data;
382         struct saa7146_format *fmt = NULL;
383         unsigned int resource;
384         int ret = 0, err = 0;
385
386         DEB_EE(("dev:%p, fh:%p\n",dev,fh));
387
388         if ((vv->video_status & STATUS_CAPTURE) != 0) {
389                 if (vv->video_fh == fh) {
390                         DEB_S(("already capturing.\n"));
391                         return 0;
392                 }
393                 DEB_S(("already capturing in another open.\n"));
394                 return -EBUSY;
395         }
396
397         if ((vv->video_status & STATUS_OVERLAY) != 0) {
398                 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
399                 vv->ov_suspend = vv->video_fh;
400                 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
401                 if (0 != err) {
402                         DEB_D(("suspending video failed. aborting\n"));
403                         return err;
404                 }
405         }
406
407         fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
408         /* we need to have a valid format set here */
409         BUG_ON(NULL == fmt);
410
411         if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
412                 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
413         } else {
414                 resource = RESOURCE_DMA1_HPS;
415         }
416
417         ret = saa7146_res_get(fh, resource);
418         if (0 == ret) {
419                 DEB_S(("cannot get capture resource %d\n",resource));
420                 if (vv->ov_suspend != NULL) {
421                         saa7146_start_preview(vv->ov_suspend);
422                         vv->ov_suspend = NULL;
423                 }
424                 return -EBUSY;
425         }
426
427         /* clear out beginning of streaming bit (rps register 0)*/
428         saa7146_write(dev, MC2, MASK_27 );
429
430         /* enable rps0 irqs */
431         SAA7146_IER_ENABLE(dev, MASK_27);
432
433         vv->video_fh = fh;
434         vv->video_status = STATUS_CAPTURE;
435
436         return 0;
437 }
438
439 static int video_end(struct saa7146_fh *fh, struct file *file)
440 {
441         struct saa7146_dev *dev = fh->dev;
442         struct saa7146_vv *vv = dev->vv_data;
443         struct saa7146_format *fmt = NULL;
444         unsigned long flags;
445         unsigned int resource;
446         u32 dmas = 0;
447         DEB_EE(("dev:%p, fh:%p\n",dev,fh));
448
449         if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
450                 DEB_S(("not capturing.\n"));
451                 return 0;
452         }
453
454         if (vv->video_fh != fh) {
455                 DEB_S(("capturing, but in another open.\n"));
456                 return -EBUSY;
457         }
458
459         fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
460         /* we need to have a valid format set here */
461         BUG_ON(NULL == fmt);
462
463         if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
464                 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
465                 dmas = MASK_22 | MASK_21 | MASK_20;
466         } else {
467                 resource = RESOURCE_DMA1_HPS;
468                 dmas = MASK_22;
469         }
470         spin_lock_irqsave(&dev->slock,flags);
471
472         /* disable rps0  */
473         saa7146_write(dev, MC1, MASK_28);
474
475         /* disable rps0 irqs */
476         SAA7146_IER_DISABLE(dev, MASK_27);
477
478         /* shut down all used video dma transfers */
479         saa7146_write(dev, MC1, dmas);
480
481         spin_unlock_irqrestore(&dev->slock, flags);
482
483         vv->video_fh = NULL;
484         vv->video_status = 0;
485
486         saa7146_res_free(fh, resource);
487
488         if (vv->ov_suspend != NULL) {
489                 saa7146_start_preview(vv->ov_suspend);
490                 vv->ov_suspend = NULL;
491         }
492
493         return 0;
494 }
495
496 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
497 {
498         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
499
500         strcpy((char *)cap->driver, "saa7146 v4l2");
501         strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
502         sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
503         cap->version = SAA7146_VERSION_CODE;
504         cap->capabilities =
505                 V4L2_CAP_VIDEO_CAPTURE |
506                 V4L2_CAP_VIDEO_OVERLAY |
507                 V4L2_CAP_READWRITE |
508                 V4L2_CAP_STREAMING;
509         cap->capabilities |= dev->ext_vv_data->capabilities;
510         return 0;
511 }
512
513 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
514 {
515         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
516         struct saa7146_vv *vv = dev->vv_data;
517
518         *fb = vv->ov_fb;
519         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
520         return 0;
521 }
522
523 static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
524 {
525         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
526         struct saa7146_vv *vv = dev->vv_data;
527         struct saa7146_format *fmt;
528
529         DEB_EE(("VIDIOC_S_FBUF\n"));
530
531         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
532                 return -EPERM;
533
534         /* check args */
535         fmt = format_by_fourcc(dev, fb->fmt.pixelformat);
536         if (NULL == fmt)
537                 return -EINVAL;
538
539         /* planar formats are not allowed for overlay video, clipping and video dma would clash */
540         if (fmt->flags & FORMAT_IS_PLANAR)
541                 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",
542                                         (char *)&fmt->pixelformat));
543
544         /* check if overlay is running */
545         if (IS_OVERLAY_ACTIVE(fh) != 0) {
546                 if (vv->video_fh != fh) {
547                         DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
548                         return -EBUSY;
549                 }
550         }
551
552         mutex_lock(&dev->lock);
553
554         /* ok, accept it */
555         vv->ov_fb = *fb;
556         vv->ov_fmt = fmt;
557         if (0 == vv->ov_fb.fmt.bytesperline)
558                 vv->ov_fb.fmt.bytesperline =
559                         vv->ov_fb.fmt.width * fmt->depth / 8;
560
561         mutex_unlock(&dev->lock);
562         return 0;
563 }
564
565 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
566 {
567         if (f->index >= NUM_FORMATS)
568                 return -EINVAL;
569         strlcpy((char *)f->description, formats[f->index].name,
570                         sizeof(f->description));
571         f->pixelformat = formats[f->index].pixelformat;
572         return 0;
573 }
574
575 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_fmtdesc *f)
576 {
577         return vidioc_enum_fmt_vid_cap(file, fh, f);
578 }
579
580 static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
581 {
582         const struct v4l2_queryctrl *ctrl;
583
584         if ((c->id <  V4L2_CID_BASE ||
585              c->id >= V4L2_CID_LASTP1) &&
586             (c->id <  V4L2_CID_PRIVATE_BASE ||
587              c->id >= V4L2_CID_PRIVATE_LASTP1))
588                 return -EINVAL;
589
590         ctrl = ctrl_by_id(c->id);
591         if (ctrl == NULL)
592                 return -EINVAL;
593
594         DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n", c->id));
595         *c = *ctrl;
596         return 0;
597 }
598
599 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
600 {
601         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
602         struct saa7146_vv *vv = dev->vv_data;
603         const struct v4l2_queryctrl *ctrl;
604         u32 value = 0;
605
606         ctrl = ctrl_by_id(c->id);
607         if (NULL == ctrl)
608                 return -EINVAL;
609         switch (c->id) {
610         case V4L2_CID_BRIGHTNESS:
611                 value = saa7146_read(dev, BCS_CTRL);
612                 c->value = 0xff & (value >> 24);
613                 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n", c->value));
614                 break;
615         case V4L2_CID_CONTRAST:
616                 value = saa7146_read(dev, BCS_CTRL);
617                 c->value = 0x7f & (value >> 16);
618                 DEB_D(("V4L2_CID_CONTRAST: %d\n", c->value));
619                 break;
620         case V4L2_CID_SATURATION:
621                 value = saa7146_read(dev, BCS_CTRL);
622                 c->value = 0x7f & (value >> 0);
623                 DEB_D(("V4L2_CID_SATURATION: %d\n", c->value));
624                 break;
625         case V4L2_CID_VFLIP:
626                 c->value = vv->vflip;
627                 DEB_D(("V4L2_CID_VFLIP: %d\n", c->value));
628                 break;
629         case V4L2_CID_HFLIP:
630                 c->value = vv->hflip;
631                 DEB_D(("V4L2_CID_HFLIP: %d\n", c->value));
632                 break;
633         default:
634                 return -EINVAL;
635         }
636         return 0;
637 }
638
639 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
640 {
641         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
642         struct saa7146_vv *vv = dev->vv_data;
643         const struct v4l2_queryctrl *ctrl;
644
645         ctrl = ctrl_by_id(c->id);
646         if (NULL == ctrl) {
647                 DEB_D(("unknown control %d\n", c->id));
648                 return -EINVAL;
649         }
650
651         mutex_lock(&dev->lock);
652
653         switch (ctrl->type) {
654         case V4L2_CTRL_TYPE_BOOLEAN:
655         case V4L2_CTRL_TYPE_MENU:
656         case V4L2_CTRL_TYPE_INTEGER:
657                 if (c->value < ctrl->minimum)
658                         c->value = ctrl->minimum;
659                 if (c->value > ctrl->maximum)
660                         c->value = ctrl->maximum;
661                 break;
662         default:
663                 /* nothing */;
664         }
665
666         switch (c->id) {
667         case V4L2_CID_BRIGHTNESS: {
668                 u32 value = saa7146_read(dev, BCS_CTRL);
669                 value &= 0x00ffffff;
670                 value |= (c->value << 24);
671                 saa7146_write(dev, BCS_CTRL, value);
672                 saa7146_write(dev, MC2, MASK_22 | MASK_06);
673                 break;
674         }
675         case V4L2_CID_CONTRAST: {
676                 u32 value = saa7146_read(dev, BCS_CTRL);
677                 value &= 0xff00ffff;
678                 value |= (c->value << 16);
679                 saa7146_write(dev, BCS_CTRL, value);
680                 saa7146_write(dev, MC2, MASK_22 | MASK_06);
681                 break;
682         }
683         case V4L2_CID_SATURATION: {
684                 u32 value = saa7146_read(dev, BCS_CTRL);
685                 value &= 0xffffff00;
686                 value |= (c->value << 0);
687                 saa7146_write(dev, BCS_CTRL, value);
688                 saa7146_write(dev, MC2, MASK_22 | MASK_06);
689                 break;
690         }
691         case V4L2_CID_HFLIP:
692                 /* fixme: we can support changing VFLIP and HFLIP here... */
693                 if (IS_CAPTURE_ACTIVE(fh) != 0) {
694                         DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
695                         mutex_unlock(&dev->lock);
696                         return -EINVAL;
697                 }
698                 vv->hflip = c->value;
699                 break;
700         case V4L2_CID_VFLIP:
701                 if (IS_CAPTURE_ACTIVE(fh) != 0) {
702                         DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
703                         mutex_unlock(&dev->lock);
704                         return -EINVAL;
705                 }
706                 vv->vflip = c->value;
707                 break;
708         default:
709                 mutex_unlock(&dev->lock);
710                 return -EINVAL;
711         }
712         mutex_unlock(&dev->lock);
713
714         if (IS_OVERLAY_ACTIVE(fh) != 0) {
715                 saa7146_stop_preview(fh);
716                 saa7146_start_preview(fh);
717         }
718         return 0;
719 }
720
721 static int vidioc_g_parm(struct file *file, void *fh,
722                 struct v4l2_streamparm *parm)
723 {
724         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
725                 return -EINVAL;
726         parm->parm.capture.readbuffers = 1;
727         /* fixme: only for PAL! */
728         parm->parm.capture.timeperframe.numerator = 1;
729         parm->parm.capture.timeperframe.denominator = 25;
730         return 0;
731 }
732
733 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
734 {
735         f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
736         return 0;
737 }
738
739 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
740 {
741         f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
742         return 0;
743 }
744
745 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
746 {
747         f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
748         return 0;
749 }
750
751 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
752 {
753         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
754         struct saa7146_vv *vv = dev->vv_data;
755         struct saa7146_format *fmt;
756         enum v4l2_field field;
757         int maxw, maxh;
758         int calc_bpl;
759
760         DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
761
762         fmt = format_by_fourcc(dev, f->fmt.pix.pixelformat);
763         if (NULL == fmt)
764                 return -EINVAL;
765
766         field = f->fmt.pix.field;
767         maxw  = vv->standard->h_max_out;
768         maxh  = vv->standard->v_max_out;
769
770         if (V4L2_FIELD_ANY == field) {
771                 field = (f->fmt.pix.height > maxh / 2)
772                         ? V4L2_FIELD_INTERLACED
773                         : V4L2_FIELD_BOTTOM;
774         }
775         switch (field) {
776         case V4L2_FIELD_ALTERNATE:
777                 vv->last_field = V4L2_FIELD_TOP;
778                 maxh = maxh / 2;
779                 break;
780         case V4L2_FIELD_TOP:
781         case V4L2_FIELD_BOTTOM:
782                 vv->last_field = V4L2_FIELD_INTERLACED;
783                 maxh = maxh / 2;
784                 break;
785         case V4L2_FIELD_INTERLACED:
786                 vv->last_field = V4L2_FIELD_INTERLACED;
787                 break;
788         default:
789                 DEB_D(("no known field mode '%d'.\n", field));
790                 return -EINVAL;
791         }
792
793         f->fmt.pix.field = field;
794         if (f->fmt.pix.width > maxw)
795                 f->fmt.pix.width = maxw;
796         if (f->fmt.pix.height > maxh)
797                 f->fmt.pix.height = maxh;
798
799         calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
800
801         if (f->fmt.pix.bytesperline < calc_bpl)
802                 f->fmt.pix.bytesperline = calc_bpl;
803
804         if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
805                 f->fmt.pix.bytesperline = calc_bpl;
806
807         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
808         DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n", f->fmt.pix.width,
809                         f->fmt.pix.height, f->fmt.pix.bytesperline, f->fmt.pix.sizeimage));
810
811         return 0;
812 }
813
814
815 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
816 {
817         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
818         struct saa7146_vv *vv = dev->vv_data;
819         struct v4l2_window *win = &f->fmt.win;
820         enum v4l2_field field;
821         int maxw, maxh;
822
823         DEB_EE(("dev:%p\n", dev));
824
825         if (NULL == vv->ov_fb.base) {
826                 DEB_D(("no fb base set.\n"));
827                 return -EINVAL;
828         }
829         if (NULL == vv->ov_fmt) {
830                 DEB_D(("no fb fmt set.\n"));
831                 return -EINVAL;
832         }
833         if (win->w.width < 48 || win->w.height < 32) {
834                 DEB_D(("min width/height. (%d,%d)\n", win->w.width, win->w.height));
835                 return -EINVAL;
836         }
837         if (win->clipcount > 16) {
838                 DEB_D(("clipcount too big.\n"));
839                 return -EINVAL;
840         }
841
842         field = win->field;
843         maxw  = vv->standard->h_max_out;
844         maxh  = vv->standard->v_max_out;
845
846         if (V4L2_FIELD_ANY == field) {
847                 field = (win->w.height > maxh / 2)
848                         ? V4L2_FIELD_INTERLACED
849                         : V4L2_FIELD_TOP;
850                 }
851         switch (field) {
852         case V4L2_FIELD_TOP:
853         case V4L2_FIELD_BOTTOM:
854         case V4L2_FIELD_ALTERNATE:
855                 maxh = maxh / 2;
856                 break;
857         case V4L2_FIELD_INTERLACED:
858                 break;
859         default:
860                 DEB_D(("no known field mode '%d'.\n", field));
861                 return -EINVAL;
862         }
863
864         win->field = field;
865         if (win->w.width > maxw)
866                 win->w.width = maxw;
867         if (win->w.height > maxh)
868                 win->w.height = maxh;
869
870         return 0;
871 }
872
873 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
874 {
875         struct saa7146_fh *fh = __fh;
876         struct saa7146_dev *dev = fh->dev;
877         struct saa7146_vv *vv = dev->vv_data;
878         int err;
879
880         DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
881         if (IS_CAPTURE_ACTIVE(fh) != 0) {
882                 DEB_EE(("streaming capture is active\n"));
883                 return -EBUSY;
884         }
885         err = vidioc_try_fmt_vid_cap(file, fh, f);
886         if (0 != err)
887                 return err;
888         fh->video_fmt = f->fmt.pix;
889         DEB_EE(("set to pixelformat '%4.4s'\n", (char *)&fh->video_fmt.pixelformat));
890         return 0;
891 }
892
893 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
894 {
895         struct saa7146_fh *fh = __fh;
896         struct saa7146_dev *dev = fh->dev;
897         struct saa7146_vv *vv = dev->vv_data;
898         int err;
899
900         DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh));
901         err = vidioc_try_fmt_vid_overlay(file, fh, f);
902         if (0 != err)
903                 return err;
904         mutex_lock(&dev->lock);
905         fh->ov.win    = f->fmt.win;
906         fh->ov.nclips = f->fmt.win.clipcount;
907         if (fh->ov.nclips > 16)
908                 fh->ov.nclips = 16;
909         if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
910                                 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
911                 mutex_unlock(&dev->lock);
912                 return -EFAULT;
913         }
914
915         /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
916         fh->ov.fh = fh;
917
918         mutex_unlock(&dev->lock);
919
920         /* check if our current overlay is active */
921         if (IS_OVERLAY_ACTIVE(fh) != 0) {
922                 saa7146_stop_preview(fh);
923                 saa7146_start_preview(fh);
924         }
925         return 0;
926 }
927
928 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
929 {
930         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
931         struct saa7146_vv *vv = dev->vv_data;
932
933         *norm = vv->standard->id;
934         return 0;
935 }
936
937         /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
938            PAL / NTSC / SECAM. if your hardware does not (or does more)
939            -- override this function in your extension */
940 /*
941         case VIDIOC_ENUMSTD:
942         {
943                 struct v4l2_standard *e = arg;
944                 if (e->index < 0 )
945                         return -EINVAL;
946                 if( e->index < dev->ext_vv_data->num_stds ) {
947                         DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
948                         v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
949                         return 0;
950                 }
951                 return -EINVAL;
952         }
953         */
954
955 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
956 {
957         struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
958         struct saa7146_vv *vv = dev->vv_data;
959         int found = 0;
960         int err, i;
961
962         DEB_EE(("VIDIOC_S_STD\n"));
963
964         if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
965                 DEB_D(("cannot change video standard while streaming capture is active\n"));
966                 return -EBUSY;
967         }
968
969         if ((vv->video_status & STATUS_OVERLAY) != 0) {
970                 vv->ov_suspend = vv->video_fh;
971                 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
972                 if (0 != err) {
973                         DEB_D(("suspending video failed. aborting\n"));
974                         return err;
975                 }
976         }
977
978         mutex_lock(&dev->lock);
979
980         for (i = 0; i < dev->ext_vv_data->num_stds; i++)
981                 if (*id & dev->ext_vv_data->stds[i].id)
982                         break;
983         if (i != dev->ext_vv_data->num_stds) {
984                 vv->standard = &dev->ext_vv_data->stds[i];
985                 if (NULL != dev->ext_vv_data->std_callback)
986                         dev->ext_vv_data->std_callback(dev, vv->standard);
987                 found = 1;
988         }
989
990         mutex_unlock(&dev->lock);
991
992         if (vv->ov_suspend != NULL) {
993                 saa7146_start_preview(vv->ov_suspend);
994                 vv->ov_suspend = NULL;
995         }
996
997         if (!found) {
998                 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
999                 return -EINVAL;
1000         }
1001
1002         DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name));
1003         return 0;
1004 }
1005
1006 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
1007 {
1008         int err;
1009
1010         DEB_D(("VIDIOC_OVERLAY on:%d\n", on));
1011         if (on)
1012                 err = saa7146_start_preview(fh);
1013         else
1014                 err = saa7146_stop_preview(fh);
1015         return err;
1016 }
1017
1018 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
1019 {
1020         struct saa7146_fh *fh = __fh;
1021
1022         if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1023                 return videobuf_reqbufs(&fh->video_q, b);
1024         if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1025                 return videobuf_reqbufs(&fh->vbi_q, b);
1026         return -EINVAL;
1027 }
1028
1029 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1030 {
1031         struct saa7146_fh *fh = __fh;
1032
1033         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1034                 return videobuf_querybuf(&fh->video_q, buf);
1035         if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1036                 return videobuf_querybuf(&fh->vbi_q, buf);
1037         return -EINVAL;
1038 }
1039
1040 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1041 {
1042         struct saa7146_fh *fh = __fh;
1043
1044         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1045                 return videobuf_qbuf(&fh->video_q, buf);
1046         if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1047                 return videobuf_qbuf(&fh->vbi_q, buf);
1048         return -EINVAL;
1049 }
1050
1051 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1052 {
1053         struct saa7146_fh *fh = __fh;
1054
1055         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1056                 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
1057         if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1058                 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
1059         return -EINVAL;
1060 }
1061
1062 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
1063 {
1064         struct saa7146_fh *fh = __fh;
1065         int err;
1066
1067         DEB_D(("VIDIOC_STREAMON, type:%d\n", type));
1068
1069         err = video_begin(fh);
1070         if (err)
1071                 return err;
1072         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1073                 return videobuf_streamon(&fh->video_q);
1074         if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1075                 return videobuf_streamon(&fh->vbi_q);
1076         return -EINVAL;
1077 }
1078
1079 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
1080 {
1081         struct saa7146_fh *fh = __fh;
1082         struct saa7146_dev *dev = fh->dev;
1083         struct saa7146_vv *vv = dev->vv_data;
1084         int err;
1085
1086         DEB_D(("VIDIOC_STREAMOFF, type:%d\n", type));
1087
1088         /* ugly: we need to copy some checks from video_end(),
1089            because videobuf_streamoff() relies on the capture running.
1090            check and fix this */
1091         if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
1092                 DEB_S(("not capturing.\n"));
1093                 return 0;
1094         }
1095
1096         if (vv->video_fh != fh) {
1097                 DEB_S(("capturing, but in another open.\n"));
1098                 return -EBUSY;
1099         }
1100
1101         err = -EINVAL;
1102         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1103                 err = videobuf_streamoff(&fh->video_q);
1104         else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1105                 err = videobuf_streamoff(&fh->vbi_q);
1106         if (0 != err) {
1107                 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1108                 video_end(fh, file);
1109         } else {
1110                 err = video_end(fh, file);
1111         }
1112         return err;
1113 }
1114
1115 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1116 static int vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *mbuf)
1117 {
1118         struct saa7146_fh *fh = __fh;
1119         struct videobuf_queue *q = &fh->video_q;
1120         int err, i;
1121
1122         /* fixme: number of capture buffers and sizes for v4l apps */
1123         int gbuffers = 2;
1124         int gbufsize = 768 * 576 * 4;
1125
1126         DEB_D(("VIDIOCGMBUF \n"));
1127
1128         q = &fh->video_q;
1129         err = videobuf_mmap_setup(q, gbuffers, gbufsize,
1130                         V4L2_MEMORY_MMAP);
1131         if (err < 0)
1132                 return err;
1133
1134         gbuffers = err;
1135         memset(mbuf, 0, sizeof(*mbuf));
1136         mbuf->frames = gbuffers;
1137         mbuf->size   = gbuffers * gbufsize;
1138         for (i = 0; i < gbuffers; i++)
1139                 mbuf->offsets[i] = i * gbufsize;
1140         return 0;
1141 }
1142 #endif
1143
1144 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1145         .vidioc_querycap             = vidioc_querycap,
1146         .vidioc_enum_fmt_vid_cap     = vidioc_enum_fmt_vid_cap,
1147         .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1148         .vidioc_g_fmt_vid_cap        = vidioc_g_fmt_vid_cap,
1149         .vidioc_try_fmt_vid_cap      = vidioc_try_fmt_vid_cap,
1150         .vidioc_s_fmt_vid_cap        = vidioc_s_fmt_vid_cap,
1151         .vidioc_g_fmt_vid_overlay    = vidioc_g_fmt_vid_overlay,
1152         .vidioc_try_fmt_vid_overlay  = vidioc_try_fmt_vid_overlay,
1153         .vidioc_s_fmt_vid_overlay    = vidioc_s_fmt_vid_overlay,
1154         .vidioc_g_fmt_vbi_cap        = vidioc_g_fmt_vbi_cap,
1155
1156         .vidioc_overlay              = vidioc_overlay,
1157         .vidioc_g_fbuf               = vidioc_g_fbuf,
1158         .vidioc_s_fbuf               = vidioc_s_fbuf,
1159         .vidioc_reqbufs              = vidioc_reqbufs,
1160         .vidioc_querybuf             = vidioc_querybuf,
1161         .vidioc_qbuf                 = vidioc_qbuf,
1162         .vidioc_dqbuf                = vidioc_dqbuf,
1163         .vidioc_g_std                = vidioc_g_std,
1164         .vidioc_s_std                = vidioc_s_std,
1165         .vidioc_queryctrl            = vidioc_queryctrl,
1166         .vidioc_g_ctrl               = vidioc_g_ctrl,
1167         .vidioc_s_ctrl               = vidioc_s_ctrl,
1168         .vidioc_streamon             = vidioc_streamon,
1169         .vidioc_streamoff            = vidioc_streamoff,
1170         .vidioc_g_parm               = vidioc_g_parm,
1171 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1172         .vidiocgmbuf                 = vidiocgmbuf,
1173 #endif
1174 };
1175
1176 /*********************************************************************************/
1177 /* buffer handling functions                                                  */
1178
1179 static int buffer_activate (struct saa7146_dev *dev,
1180                      struct saa7146_buf *buf,
1181                      struct saa7146_buf *next)
1182 {
1183         struct saa7146_vv *vv = dev->vv_data;
1184
1185         buf->vb.state = VIDEOBUF_ACTIVE;
1186         saa7146_set_capture(dev,buf,next);
1187
1188         mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1189         return 0;
1190 }
1191
1192 static int buffer_prepare(struct videobuf_queue *q,
1193                           struct videobuf_buffer *vb, enum v4l2_field field)
1194 {
1195         struct file *file = q->priv_data;
1196         struct saa7146_fh *fh = file->private_data;
1197         struct saa7146_dev *dev = fh->dev;
1198         struct saa7146_vv *vv = dev->vv_data;
1199         struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1200         int size,err = 0;
1201
1202         DEB_CAP(("vbuf:%p\n",vb));
1203
1204         /* sanity checks */
1205         if (fh->video_fmt.width  < 48 ||
1206             fh->video_fmt.height < 32 ||
1207             fh->video_fmt.width  > vv->standard->h_max_out ||
1208             fh->video_fmt.height > vv->standard->v_max_out) {
1209                 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
1210                 return -EINVAL;
1211         }
1212
1213         size = fh->video_fmt.sizeimage;
1214         if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1215                 DEB_D(("size mismatch.\n"));
1216                 return -EINVAL;
1217         }
1218
1219         DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1220                 fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
1221         if (buf->vb.width  != fh->video_fmt.width  ||
1222             buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1223             buf->vb.height != fh->video_fmt.height ||
1224             buf->vb.size   != size ||
1225             buf->vb.field  != field      ||
1226             buf->vb.field  != fh->video_fmt.field  ||
1227             buf->fmt       != &fh->video_fmt) {
1228                 saa7146_dma_free(dev,q,buf);
1229         }
1230
1231         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1232                 struct saa7146_format *sfmt;
1233
1234                 buf->vb.bytesperline  = fh->video_fmt.bytesperline;
1235                 buf->vb.width  = fh->video_fmt.width;
1236                 buf->vb.height = fh->video_fmt.height;
1237                 buf->vb.size   = size;
1238                 buf->vb.field  = field;
1239                 buf->fmt       = &fh->video_fmt;
1240                 buf->vb.field  = fh->video_fmt.field;
1241
1242                 sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
1243
1244                 if( 0 != IS_PLANAR(sfmt->trans)) {
1245                         saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1246                         saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1247                         saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1248
1249                         saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1250                         saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1251                         saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1252                 } else {
1253                         saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1254                         saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1255                 }
1256
1257                 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1258                 if (err)
1259                         goto oops;
1260                 err = saa7146_pgtable_build(dev,buf);
1261                 if (err)
1262                         goto oops;
1263         }
1264         buf->vb.state = VIDEOBUF_PREPARED;
1265         buf->activate = buffer_activate;
1266
1267         return 0;
1268
1269  oops:
1270         DEB_D(("error out.\n"));
1271         saa7146_dma_free(dev,q,buf);
1272
1273         return err;
1274 }
1275
1276 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1277 {
1278         struct file *file = q->priv_data;
1279         struct saa7146_fh *fh = file->private_data;
1280
1281         if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1282                 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1283
1284         *size = fh->video_fmt.sizeimage;
1285
1286         /* check if we exceed the "max_memory" parameter */
1287         if( (*count * *size) > (max_memory*1048576) ) {
1288                 *count = (max_memory*1048576) / *size;
1289         }
1290
1291         DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
1292
1293         return 0;
1294 }
1295
1296 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1297 {
1298         struct file *file = q->priv_data;
1299         struct saa7146_fh *fh = file->private_data;
1300         struct saa7146_dev *dev = fh->dev;
1301         struct saa7146_vv *vv = dev->vv_data;
1302         struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1303
1304         DEB_CAP(("vbuf:%p\n",vb));
1305         saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1306 }
1307
1308 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1309 {
1310         struct file *file = q->priv_data;
1311         struct saa7146_fh *fh = file->private_data;
1312         struct saa7146_dev *dev = fh->dev;
1313         struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1314
1315         DEB_CAP(("vbuf:%p\n",vb));
1316         saa7146_dma_free(dev,q,buf);
1317 }
1318
1319 static struct videobuf_queue_ops video_qops = {
1320         .buf_setup    = buffer_setup,
1321         .buf_prepare  = buffer_prepare,
1322         .buf_queue    = buffer_queue,
1323         .buf_release  = buffer_release,
1324 };
1325
1326 /********************************************************************************/
1327 /* file operations */
1328
1329 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1330 {
1331         INIT_LIST_HEAD(&vv->video_q.queue);
1332
1333         init_timer(&vv->video_q.timeout);
1334         vv->video_q.timeout.function = saa7146_buffer_timeout;
1335         vv->video_q.timeout.data     = (unsigned long)(&vv->video_q);
1336         vv->video_q.dev              = dev;
1337
1338         /* set some default values */
1339         vv->standard = &dev->ext_vv_data->stds[0];
1340
1341         /* FIXME: what's this? */
1342         vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1343         vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1344 }
1345
1346
1347 static int video_open(struct saa7146_dev *dev, struct file *file)
1348 {
1349         struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1350         struct saa7146_format *sfmt;
1351
1352         fh->video_fmt.width = 384;
1353         fh->video_fmt.height = 288;
1354         fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1355         fh->video_fmt.bytesperline = 0;
1356         fh->video_fmt.field = V4L2_FIELD_ANY;
1357         sfmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
1358         fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1359
1360         videobuf_queue_sg_init(&fh->video_q, &video_qops,
1361                             &dev->pci->dev, &dev->slock,
1362                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1363                             V4L2_FIELD_INTERLACED,
1364                             sizeof(struct saa7146_buf),
1365                             file);
1366
1367         return 0;
1368 }
1369
1370
1371 static void video_close(struct saa7146_dev *dev, struct file *file)
1372 {
1373         struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1374         struct saa7146_vv *vv = dev->vv_data;
1375         struct videobuf_queue *q = &fh->video_q;
1376         int err;
1377
1378         if (IS_CAPTURE_ACTIVE(fh) != 0) {
1379                 err = video_end(fh, file);
1380         } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
1381                 err = saa7146_stop_preview(fh);
1382         }
1383
1384         videobuf_stop(q);
1385
1386         /* hmm, why is this function declared void? */
1387         /* return err */
1388 }
1389
1390
1391 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1392 {
1393         struct saa7146_vv *vv = dev->vv_data;
1394         struct saa7146_dmaqueue *q = &vv->video_q;
1395
1396         spin_lock(&dev->slock);
1397         DEB_CAP(("called.\n"));
1398
1399         /* only finish the buffer if we have one... */
1400         if( NULL != q->curr ) {
1401                 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1402         }
1403         saa7146_buffer_next(dev,q,0);
1404
1405         spin_unlock(&dev->slock);
1406 }
1407
1408 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1409 {
1410         struct saa7146_fh *fh = file->private_data;
1411         struct saa7146_dev *dev = fh->dev;
1412         struct saa7146_vv *vv = dev->vv_data;
1413         ssize_t ret = 0;
1414
1415         DEB_EE(("called.\n"));
1416
1417         if ((vv->video_status & STATUS_CAPTURE) != 0) {
1418                 /* fixme: should we allow read() captures while streaming capture? */
1419                 if (vv->video_fh == fh) {
1420                         DEB_S(("already capturing.\n"));
1421                         return -EBUSY;
1422                 }
1423                 DEB_S(("already capturing in another open.\n"));
1424                 return -EBUSY;
1425         }
1426
1427         ret = video_begin(fh);
1428         if( 0 != ret) {
1429                 goto out;
1430         }
1431
1432         ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1433                                 file->f_flags & O_NONBLOCK);
1434         if (ret != 0) {
1435                 video_end(fh, file);
1436         } else {
1437                 ret = video_end(fh, file);
1438         }
1439 out:
1440         /* restart overlay if it was active before */
1441         if (vv->ov_suspend != NULL) {
1442                 saa7146_start_preview(vv->ov_suspend);
1443                 vv->ov_suspend = NULL;
1444         }
1445
1446         return ret;
1447 }
1448
1449 struct saa7146_use_ops saa7146_video_uops = {
1450         .init = video_init,
1451         .open = video_open,
1452         .release = video_close,
1453         .irq_done = video_irq_done,
1454         .read = video_read,
1455 };