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