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