Back to home page

OSCL-LXR

 
 

    


0001 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0002 
0003 #include <media/drv-intf/saa7146_vv.h>
0004 #include <media/v4l2-event.h>
0005 #include <media/v4l2-ctrls.h>
0006 #include <linux/module.h>
0007 #include <linux/kernel.h>
0008 
0009 static int max_memory = 32;
0010 
0011 module_param(max_memory, int, 0644);
0012 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
0013 
0014 #define IS_CAPTURE_ACTIVE(fh) \
0015     (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
0016 
0017 #define IS_OVERLAY_ACTIVE(fh) \
0018     (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
0019 
0020 /* format descriptions for capture and preview */
0021 static struct saa7146_format formats[] = {
0022     {
0023         .pixelformat    = V4L2_PIX_FMT_RGB332,
0024         .trans      = RGB08_COMPOSED,
0025         .depth      = 8,
0026         .flags      = 0,
0027     }, {
0028         .pixelformat    = V4L2_PIX_FMT_RGB565,
0029         .trans      = RGB16_COMPOSED,
0030         .depth      = 16,
0031         .flags      = 0,
0032     }, {
0033         .pixelformat    = V4L2_PIX_FMT_BGR24,
0034         .trans      = RGB24_COMPOSED,
0035         .depth      = 24,
0036         .flags      = 0,
0037     }, {
0038         .pixelformat    = V4L2_PIX_FMT_BGR32,
0039         .trans      = RGB32_COMPOSED,
0040         .depth      = 32,
0041         .flags      = 0,
0042     }, {
0043         .pixelformat    = V4L2_PIX_FMT_RGB32,
0044         .trans      = RGB32_COMPOSED,
0045         .depth      = 32,
0046         .flags      = 0,
0047         .swap       = 0x2,
0048     }, {
0049         .pixelformat    = V4L2_PIX_FMT_GREY,
0050         .trans      = Y8,
0051         .depth      = 8,
0052         .flags      = 0,
0053     }, {
0054         .pixelformat    = V4L2_PIX_FMT_YUV422P,
0055         .trans      = YUV422_DECOMPOSED,
0056         .depth      = 16,
0057         .flags      = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
0058     }, {
0059         .pixelformat    = V4L2_PIX_FMT_YVU420,
0060         .trans      = YUV420_DECOMPOSED,
0061         .depth      = 12,
0062         .flags      = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
0063     }, {
0064         .pixelformat    = V4L2_PIX_FMT_YUV420,
0065         .trans      = YUV420_DECOMPOSED,
0066         .depth      = 12,
0067         .flags      = FORMAT_IS_PLANAR,
0068     }, {
0069         .pixelformat    = V4L2_PIX_FMT_UYVY,
0070         .trans      = YUV422_COMPOSED,
0071         .depth      = 16,
0072         .flags      = 0,
0073     }
0074 };
0075 
0076 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
0077    due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
0078    (like V4L2_PIX_FMT_YUYV) ... 8-( */
0079 
0080 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
0081 {
0082     int i;
0083 
0084     for (i = 0; i < ARRAY_SIZE(formats); i++) {
0085         if (formats[i].pixelformat == fourcc) {
0086             return formats+i;
0087         }
0088     }
0089 
0090     DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
0091     return NULL;
0092 }
0093 
0094 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
0095 
0096 int saa7146_start_preview(struct saa7146_fh *fh)
0097 {
0098     struct saa7146_dev *dev = fh->dev;
0099     struct saa7146_vv *vv = dev->vv_data;
0100     struct v4l2_format fmt;
0101     int ret = 0, err = 0;
0102 
0103     DEB_EE("dev:%p, fh:%p\n", dev, fh);
0104 
0105     /* check if we have overlay information */
0106     if (vv->ov.fh == NULL) {
0107         DEB_D("no overlay data available. try S_FMT first.\n");
0108         return -EAGAIN;
0109     }
0110 
0111     /* check if streaming capture is running */
0112     if (IS_CAPTURE_ACTIVE(fh) != 0) {
0113         DEB_D("streaming capture is active\n");
0114         return -EBUSY;
0115     }
0116 
0117     /* check if overlay is running */
0118     if (IS_OVERLAY_ACTIVE(fh) != 0) {
0119         if (vv->video_fh == fh) {
0120             DEB_D("overlay is already active\n");
0121             return 0;
0122         }
0123         DEB_D("overlay is already active in another open\n");
0124         return -EBUSY;
0125     }
0126 
0127     if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
0128         DEB_D("cannot get necessary overlay resources\n");
0129         return -EBUSY;
0130     }
0131 
0132     fmt.fmt.win = vv->ov.win;
0133     err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
0134     if (0 != err) {
0135         saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
0136         return -EBUSY;
0137     }
0138     vv->ov.win = fmt.fmt.win;
0139 
0140     DEB_D("%dx%d+%d+%d 0x%08x field=%s\n",
0141           vv->ov.win.w.width, vv->ov.win.w.height,
0142           vv->ov.win.w.left, vv->ov.win.w.top,
0143           vv->ov_fmt->pixelformat, v4l2_field_names[vv->ov.win.field]);
0144 
0145     if (0 != (ret = saa7146_enable_overlay(fh))) {
0146         DEB_D("enabling overlay failed: %d\n", ret);
0147         saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
0148         return ret;
0149     }
0150 
0151     vv->video_status = STATUS_OVERLAY;
0152     vv->video_fh = fh;
0153 
0154     return 0;
0155 }
0156 EXPORT_SYMBOL_GPL(saa7146_start_preview);
0157 
0158 int saa7146_stop_preview(struct saa7146_fh *fh)
0159 {
0160     struct saa7146_dev *dev = fh->dev;
0161     struct saa7146_vv *vv = dev->vv_data;
0162 
0163     DEB_EE("dev:%p, fh:%p\n", dev, fh);
0164 
0165     /* check if streaming capture is running */
0166     if (IS_CAPTURE_ACTIVE(fh) != 0) {
0167         DEB_D("streaming capture is active\n");
0168         return -EBUSY;
0169     }
0170 
0171     /* check if overlay is running at all */
0172     if ((vv->video_status & STATUS_OVERLAY) == 0) {
0173         DEB_D("no active overlay\n");
0174         return 0;
0175     }
0176 
0177     if (vv->video_fh != fh) {
0178         DEB_D("overlay is active, but in another open\n");
0179         return -EBUSY;
0180     }
0181 
0182     vv->video_status = 0;
0183     vv->video_fh = NULL;
0184 
0185     saa7146_disable_overlay(fh);
0186 
0187     saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
0188 
0189     return 0;
0190 }
0191 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
0192 
0193 /********************************************************************************/
0194 /* common pagetable functions */
0195 
0196 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
0197 {
0198     struct pci_dev *pci = dev->pci;
0199     struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
0200     struct scatterlist *list = dma->sglist;
0201     int length = dma->sglen;
0202     struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
0203 
0204     DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
0205 
0206     if( 0 != IS_PLANAR(sfmt->trans)) {
0207         struct saa7146_pgtable *pt1 = &buf->pt[0];
0208         struct saa7146_pgtable *pt2 = &buf->pt[1];
0209         struct saa7146_pgtable *pt3 = &buf->pt[2];
0210         __le32  *ptr1, *ptr2, *ptr3;
0211         __le32 fill;
0212 
0213         int size = buf->fmt->width*buf->fmt->height;
0214         int i,p,m1,m2,m3,o1,o2;
0215 
0216         switch( sfmt->depth ) {
0217             case 12: {
0218                 /* create some offsets inside the page table */
0219                 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
0220                 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
0221                 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
0222                 o1 = size%PAGE_SIZE;
0223                 o2 = (size+(size/4))%PAGE_SIZE;
0224                 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
0225                     size, m1, m2, m3, o1, o2);
0226                 break;
0227             }
0228             case 16: {
0229                 /* create some offsets inside the page table */
0230                 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
0231                 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
0232                 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
0233                 o1 = size%PAGE_SIZE;
0234                 o2 = (size+(size/2))%PAGE_SIZE;
0235                 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
0236                     size, m1, m2, m3, o1, o2);
0237                 break;
0238             }
0239             default: {
0240                 return -1;
0241             }
0242         }
0243 
0244         ptr1 = pt1->cpu;
0245         ptr2 = pt2->cpu;
0246         ptr3 = pt3->cpu;
0247 
0248         /* walk all pages, copy all page addresses to ptr1 */
0249         for (i = 0; i < length; i++, list++) {
0250             for (p = 0; p * 4096 < sg_dma_len(list); p++, ptr1++)
0251                 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
0252         }
0253 /*
0254         ptr1 = pt1->cpu;
0255         for(j=0;j<40;j++) {
0256             printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
0257         }
0258 */
0259 
0260         /* if we have a user buffer, the first page may not be
0261            aligned to a page boundary. */
0262         pt1->offset = dma->sglist->offset;
0263         pt2->offset = pt1->offset+o1;
0264         pt3->offset = pt1->offset+o2;
0265 
0266         /* create video-dma2 page table */
0267         ptr1 = pt1->cpu;
0268         for(i = m1; i <= m2 ; i++, ptr2++) {
0269             *ptr2 = ptr1[i];
0270         }
0271         fill = *(ptr2-1);
0272         for(;i<1024;i++,ptr2++) {
0273             *ptr2 = fill;
0274         }
0275         /* create video-dma3 page table */
0276         ptr1 = pt1->cpu;
0277         for(i = m2; i <= m3; i++,ptr3++) {
0278             *ptr3 = ptr1[i];
0279         }
0280         fill = *(ptr3-1);
0281         for(;i<1024;i++,ptr3++) {
0282             *ptr3 = fill;
0283         }
0284         /* finally: finish up video-dma1 page table */
0285         ptr1 = pt1->cpu+m1;
0286         fill = pt1->cpu[m1];
0287         for(i=m1;i<1024;i++,ptr1++) {
0288             *ptr1 = fill;
0289         }
0290 /*
0291         ptr1 = pt1->cpu;
0292         ptr2 = pt2->cpu;
0293         ptr3 = pt3->cpu;
0294         for(j=0;j<40;j++) {
0295             printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
0296         }
0297         for(j=0;j<40;j++) {
0298             printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
0299         }
0300         for(j=0;j<40;j++) {
0301             printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
0302         }
0303 */
0304     } else {
0305         struct saa7146_pgtable *pt = &buf->pt[0];
0306         return saa7146_pgtable_build_single(pci, pt, list, length);
0307     }
0308 
0309     return 0;
0310 }
0311 
0312 
0313 /********************************************************************************/
0314 /* file operations */
0315 
0316 static int video_begin(struct saa7146_fh *fh)
0317 {
0318     struct saa7146_dev *dev = fh->dev;
0319     struct saa7146_vv *vv = dev->vv_data;
0320     struct saa7146_format *fmt = NULL;
0321     unsigned int resource;
0322     int ret = 0, err = 0;
0323 
0324     DEB_EE("dev:%p, fh:%p\n", dev, fh);
0325 
0326     if ((vv->video_status & STATUS_CAPTURE) != 0) {
0327         if (vv->video_fh == fh) {
0328             DEB_S("already capturing\n");
0329             return 0;
0330         }
0331         DEB_S("already capturing in another open\n");
0332         return -EBUSY;
0333     }
0334 
0335     if ((vv->video_status & STATUS_OVERLAY) != 0) {
0336         DEB_S("warning: suspending overlay video for streaming capture\n");
0337         vv->ov_suspend = vv->video_fh;
0338         err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
0339         if (0 != err) {
0340             DEB_D("suspending video failed. aborting\n");
0341             return err;
0342         }
0343     }
0344 
0345     fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
0346     /* we need to have a valid format set here */
0347     if (!fmt)
0348         return -EINVAL;
0349 
0350     if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
0351         resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
0352     } else {
0353         resource = RESOURCE_DMA1_HPS;
0354     }
0355 
0356     ret = saa7146_res_get(fh, resource);
0357     if (0 == ret) {
0358         DEB_S("cannot get capture resource %d\n", resource);
0359         if (vv->ov_suspend != NULL) {
0360             saa7146_start_preview(vv->ov_suspend);
0361             vv->ov_suspend = NULL;
0362         }
0363         return -EBUSY;
0364     }
0365 
0366     /* clear out beginning of streaming bit (rps register 0)*/
0367     saa7146_write(dev, MC2, MASK_27 );
0368 
0369     /* enable rps0 irqs */
0370     SAA7146_IER_ENABLE(dev, MASK_27);
0371 
0372     vv->video_fh = fh;
0373     vv->video_status = STATUS_CAPTURE;
0374 
0375     return 0;
0376 }
0377 
0378 static int video_end(struct saa7146_fh *fh, struct file *file)
0379 {
0380     struct saa7146_dev *dev = fh->dev;
0381     struct saa7146_vv *vv = dev->vv_data;
0382     struct saa7146_dmaqueue *q = &vv->video_dmaq;
0383     struct saa7146_format *fmt = NULL;
0384     unsigned long flags;
0385     unsigned int resource;
0386     u32 dmas = 0;
0387     DEB_EE("dev:%p, fh:%p\n", dev, fh);
0388 
0389     if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
0390         DEB_S("not capturing\n");
0391         return 0;
0392     }
0393 
0394     if (vv->video_fh != fh) {
0395         DEB_S("capturing, but in another open\n");
0396         return -EBUSY;
0397     }
0398 
0399     fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
0400     /* we need to have a valid format set here */
0401     if (!fmt)
0402         return -EINVAL;
0403 
0404     if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
0405         resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
0406         dmas = MASK_22 | MASK_21 | MASK_20;
0407     } else {
0408         resource = RESOURCE_DMA1_HPS;
0409         dmas = MASK_22;
0410     }
0411     spin_lock_irqsave(&dev->slock,flags);
0412 
0413     /* disable rps0  */
0414     saa7146_write(dev, MC1, MASK_28);
0415 
0416     /* disable rps0 irqs */
0417     SAA7146_IER_DISABLE(dev, MASK_27);
0418 
0419     /* shut down all used video dma transfers */
0420     saa7146_write(dev, MC1, dmas);
0421 
0422     if (q->curr)
0423         saa7146_buffer_finish(dev, q, VIDEOBUF_DONE);
0424 
0425     spin_unlock_irqrestore(&dev->slock, flags);
0426 
0427     vv->video_fh = NULL;
0428     vv->video_status = 0;
0429 
0430     saa7146_res_free(fh, resource);
0431 
0432     if (vv->ov_suspend != NULL) {
0433         saa7146_start_preview(vv->ov_suspend);
0434         vv->ov_suspend = NULL;
0435     }
0436 
0437     return 0;
0438 }
0439 
0440 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
0441 {
0442     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0443 
0444     strscpy((char *)cap->driver, "saa7146 v4l2", sizeof(cap->driver));
0445     strscpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
0446     cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
0447                 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
0448                 V4L2_CAP_DEVICE_CAPS;
0449     cap->capabilities |= dev->ext_vv_data->capabilities;
0450     return 0;
0451 }
0452 
0453 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
0454 {
0455     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0456     struct saa7146_vv *vv = dev->vv_data;
0457 
0458     *fb = vv->ov_fb;
0459     fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
0460     fb->flags = V4L2_FBUF_FLAG_PRIMARY;
0461     return 0;
0462 }
0463 
0464 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
0465 {
0466     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0467     struct saa7146_vv *vv = dev->vv_data;
0468     struct saa7146_format *fmt;
0469 
0470     DEB_EE("VIDIOC_S_FBUF\n");
0471 
0472     if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
0473         return -EPERM;
0474 
0475     /* check args */
0476     fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
0477     if (NULL == fmt)
0478         return -EINVAL;
0479 
0480     /* planar formats are not allowed for overlay video, clipping and video dma would clash */
0481     if (fmt->flags & FORMAT_IS_PLANAR)
0482         DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
0483               (char *)&fmt->pixelformat);
0484 
0485     /* check if overlay is running */
0486     if (IS_OVERLAY_ACTIVE(fh) != 0) {
0487         if (vv->video_fh != fh) {
0488             DEB_D("refusing to change framebuffer information while overlay is active in another open\n");
0489             return -EBUSY;
0490         }
0491     }
0492 
0493     /* ok, accept it */
0494     vv->ov_fb = *fb;
0495     vv->ov_fmt = fmt;
0496 
0497     if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
0498         vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
0499         DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
0500     }
0501     return 0;
0502 }
0503 
0504 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
0505 {
0506     if (f->index >= ARRAY_SIZE(formats))
0507         return -EINVAL;
0508     f->pixelformat = formats[f->index].pixelformat;
0509     return 0;
0510 }
0511 
0512 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
0513 {
0514     struct saa7146_dev *dev = container_of(ctrl->handler,
0515                 struct saa7146_dev, ctrl_handler);
0516     struct saa7146_vv *vv = dev->vv_data;
0517     u32 val;
0518 
0519     switch (ctrl->id) {
0520     case V4L2_CID_BRIGHTNESS:
0521         val = saa7146_read(dev, BCS_CTRL);
0522         val &= 0x00ffffff;
0523         val |= (ctrl->val << 24);
0524         saa7146_write(dev, BCS_CTRL, val);
0525         saa7146_write(dev, MC2, MASK_22 | MASK_06);
0526         break;
0527 
0528     case V4L2_CID_CONTRAST:
0529         val = saa7146_read(dev, BCS_CTRL);
0530         val &= 0xff00ffff;
0531         val |= (ctrl->val << 16);
0532         saa7146_write(dev, BCS_CTRL, val);
0533         saa7146_write(dev, MC2, MASK_22 | MASK_06);
0534         break;
0535 
0536     case V4L2_CID_SATURATION:
0537         val = saa7146_read(dev, BCS_CTRL);
0538         val &= 0xffffff00;
0539         val |= (ctrl->val << 0);
0540         saa7146_write(dev, BCS_CTRL, val);
0541         saa7146_write(dev, MC2, MASK_22 | MASK_06);
0542         break;
0543 
0544     case V4L2_CID_HFLIP:
0545         /* fixme: we can support changing VFLIP and HFLIP here... */
0546         if ((vv->video_status & STATUS_CAPTURE))
0547             return -EBUSY;
0548         vv->hflip = ctrl->val;
0549         break;
0550 
0551     case V4L2_CID_VFLIP:
0552         if ((vv->video_status & STATUS_CAPTURE))
0553             return -EBUSY;
0554         vv->vflip = ctrl->val;
0555         break;
0556 
0557     default:
0558         return -EINVAL;
0559     }
0560 
0561     if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
0562         struct saa7146_fh *fh = vv->video_fh;
0563 
0564         saa7146_stop_preview(fh);
0565         saa7146_start_preview(fh);
0566     }
0567     return 0;
0568 }
0569 
0570 static int vidioc_g_parm(struct file *file, void *fh,
0571         struct v4l2_streamparm *parm)
0572 {
0573     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0574     struct saa7146_vv *vv = dev->vv_data;
0575 
0576     if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
0577         return -EINVAL;
0578     parm->parm.capture.readbuffers = 1;
0579     v4l2_video_std_frame_period(vv->standard->id,
0580                     &parm->parm.capture.timeperframe);
0581     return 0;
0582 }
0583 
0584 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
0585 {
0586     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0587     struct saa7146_vv *vv = dev->vv_data;
0588 
0589     f->fmt.pix = vv->video_fmt;
0590     return 0;
0591 }
0592 
0593 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
0594 {
0595     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0596     struct saa7146_vv *vv = dev->vv_data;
0597 
0598     f->fmt.win = vv->ov.win;
0599     return 0;
0600 }
0601 
0602 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
0603 {
0604     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0605     struct saa7146_vv *vv = dev->vv_data;
0606 
0607     f->fmt.vbi = vv->vbi_fmt;
0608     return 0;
0609 }
0610 
0611 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
0612 {
0613     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0614     struct saa7146_vv *vv = dev->vv_data;
0615     struct saa7146_format *fmt;
0616     enum v4l2_field field;
0617     int maxw, maxh;
0618     int calc_bpl;
0619 
0620     DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
0621 
0622     fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
0623     if (NULL == fmt)
0624         return -EINVAL;
0625 
0626     field = f->fmt.pix.field;
0627     maxw  = vv->standard->h_max_out;
0628     maxh  = vv->standard->v_max_out;
0629 
0630     if (V4L2_FIELD_ANY == field) {
0631         field = (f->fmt.pix.height > maxh / 2)
0632             ? V4L2_FIELD_INTERLACED
0633             : V4L2_FIELD_BOTTOM;
0634     }
0635     switch (field) {
0636     case V4L2_FIELD_ALTERNATE:
0637         vv->last_field = V4L2_FIELD_TOP;
0638         maxh = maxh / 2;
0639         break;
0640     case V4L2_FIELD_TOP:
0641     case V4L2_FIELD_BOTTOM:
0642         vv->last_field = V4L2_FIELD_INTERLACED;
0643         maxh = maxh / 2;
0644         break;
0645     case V4L2_FIELD_INTERLACED:
0646         vv->last_field = V4L2_FIELD_INTERLACED;
0647         break;
0648     default:
0649         DEB_D("no known field mode '%d'\n", field);
0650         return -EINVAL;
0651     }
0652 
0653     f->fmt.pix.field = field;
0654     f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
0655     if (f->fmt.pix.width > maxw)
0656         f->fmt.pix.width = maxw;
0657     if (f->fmt.pix.height > maxh)
0658         f->fmt.pix.height = maxh;
0659 
0660     calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
0661 
0662     if (f->fmt.pix.bytesperline < calc_bpl)
0663         f->fmt.pix.bytesperline = calc_bpl;
0664 
0665     if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
0666         f->fmt.pix.bytesperline = calc_bpl;
0667 
0668     f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
0669     DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
0670           f->fmt.pix.width, f->fmt.pix.height,
0671           f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
0672 
0673     return 0;
0674 }
0675 
0676 
0677 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
0678 {
0679     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0680     struct saa7146_vv *vv = dev->vv_data;
0681     struct v4l2_window *win = &f->fmt.win;
0682     enum v4l2_field field;
0683     int maxw, maxh;
0684 
0685     DEB_EE("dev:%p\n", dev);
0686 
0687     if (NULL == vv->ov_fb.base) {
0688         DEB_D("no fb base set\n");
0689         return -EINVAL;
0690     }
0691     if (NULL == vv->ov_fmt) {
0692         DEB_D("no fb fmt set\n");
0693         return -EINVAL;
0694     }
0695     if (win->w.width < 48 || win->w.height < 32) {
0696         DEB_D("min width/height. (%d,%d)\n",
0697               win->w.width, win->w.height);
0698         return -EINVAL;
0699     }
0700     if (win->clipcount > 16) {
0701         DEB_D("clipcount too big\n");
0702         return -EINVAL;
0703     }
0704 
0705     field = win->field;
0706     maxw  = vv->standard->h_max_out;
0707     maxh  = vv->standard->v_max_out;
0708 
0709     if (V4L2_FIELD_ANY == field) {
0710         field = (win->w.height > maxh / 2)
0711             ? V4L2_FIELD_INTERLACED
0712             : V4L2_FIELD_TOP;
0713         }
0714     switch (field) {
0715     case V4L2_FIELD_TOP:
0716     case V4L2_FIELD_BOTTOM:
0717     case V4L2_FIELD_ALTERNATE:
0718         maxh = maxh / 2;
0719         break;
0720     case V4L2_FIELD_INTERLACED:
0721         break;
0722     default:
0723         DEB_D("no known field mode '%d'\n", field);
0724         return -EINVAL;
0725     }
0726 
0727     win->field = field;
0728     if (win->w.width > maxw)
0729         win->w.width = maxw;
0730     if (win->w.height > maxh)
0731         win->w.height = maxh;
0732 
0733     return 0;
0734 }
0735 
0736 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
0737 {
0738     struct saa7146_fh *fh = __fh;
0739     struct saa7146_dev *dev = fh->dev;
0740     struct saa7146_vv *vv = dev->vv_data;
0741     int err;
0742 
0743     DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
0744     if (IS_CAPTURE_ACTIVE(fh) != 0) {
0745         DEB_EE("streaming capture is active\n");
0746         return -EBUSY;
0747     }
0748     err = vidioc_try_fmt_vid_cap(file, fh, f);
0749     if (0 != err)
0750         return err;
0751     vv->video_fmt = f->fmt.pix;
0752     DEB_EE("set to pixelformat '%4.4s'\n",
0753            (char *)&vv->video_fmt.pixelformat);
0754     return 0;
0755 }
0756 
0757 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
0758 {
0759     struct saa7146_fh *fh = __fh;
0760     struct saa7146_dev *dev = fh->dev;
0761     struct saa7146_vv *vv = dev->vv_data;
0762     int err;
0763 
0764     DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
0765     err = vidioc_try_fmt_vid_overlay(file, fh, f);
0766     if (0 != err)
0767         return err;
0768     vv->ov.win    = f->fmt.win;
0769     vv->ov.nclips = f->fmt.win.clipcount;
0770     if (vv->ov.nclips > 16)
0771         vv->ov.nclips = 16;
0772     memcpy(vv->ov.clips, f->fmt.win.clips,
0773            sizeof(struct v4l2_clip) * vv->ov.nclips);
0774 
0775     /* vv->ov.fh is used to indicate that we have valid overlay information, too */
0776     vv->ov.fh = fh;
0777 
0778     /* check if our current overlay is active */
0779     if (IS_OVERLAY_ACTIVE(fh) != 0) {
0780         saa7146_stop_preview(fh);
0781         saa7146_start_preview(fh);
0782     }
0783     return 0;
0784 }
0785 
0786 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
0787 {
0788     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0789     struct saa7146_vv *vv = dev->vv_data;
0790 
0791     *norm = vv->standard->id;
0792     return 0;
0793 }
0794 
0795     /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
0796        PAL / NTSC / SECAM. if your hardware does not (or does more)
0797        -- override this function in your extension */
0798 /*
0799     case VIDIOC_ENUMSTD:
0800     {
0801         struct v4l2_standard *e = arg;
0802         if (e->index < 0 )
0803             return -EINVAL;
0804         if( e->index < dev->ext_vv_data->num_stds ) {
0805             DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
0806             v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
0807             return 0;
0808         }
0809         return -EINVAL;
0810     }
0811     */
0812 
0813 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
0814 {
0815     struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
0816     struct saa7146_vv *vv = dev->vv_data;
0817     int found = 0;
0818     int err, i;
0819 
0820     DEB_EE("VIDIOC_S_STD\n");
0821 
0822     if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
0823         DEB_D("cannot change video standard while streaming capture is active\n");
0824         return -EBUSY;
0825     }
0826 
0827     if ((vv->video_status & STATUS_OVERLAY) != 0) {
0828         vv->ov_suspend = vv->video_fh;
0829         err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
0830         if (0 != err) {
0831             DEB_D("suspending video failed. aborting\n");
0832             return err;
0833         }
0834     }
0835 
0836     for (i = 0; i < dev->ext_vv_data->num_stds; i++)
0837         if (id & dev->ext_vv_data->stds[i].id)
0838             break;
0839     if (i != dev->ext_vv_data->num_stds) {
0840         vv->standard = &dev->ext_vv_data->stds[i];
0841         if (NULL != dev->ext_vv_data->std_callback)
0842             dev->ext_vv_data->std_callback(dev, vv->standard);
0843         found = 1;
0844     }
0845 
0846     if (vv->ov_suspend != NULL) {
0847         saa7146_start_preview(vv->ov_suspend);
0848         vv->ov_suspend = NULL;
0849     }
0850 
0851     if (!found) {
0852         DEB_EE("VIDIOC_S_STD: standard not found\n");
0853         return -EINVAL;
0854     }
0855 
0856     DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
0857     return 0;
0858 }
0859 
0860 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
0861 {
0862     int err;
0863 
0864     DEB_D("VIDIOC_OVERLAY on:%d\n", on);
0865     if (on)
0866         err = saa7146_start_preview(fh);
0867     else
0868         err = saa7146_stop_preview(fh);
0869     return err;
0870 }
0871 
0872 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
0873 {
0874     struct saa7146_fh *fh = __fh;
0875 
0876     if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0877         return videobuf_reqbufs(&fh->video_q, b);
0878     if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
0879         return videobuf_reqbufs(&fh->vbi_q, b);
0880     return -EINVAL;
0881 }
0882 
0883 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
0884 {
0885     struct saa7146_fh *fh = __fh;
0886 
0887     if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0888         return videobuf_querybuf(&fh->video_q, buf);
0889     if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
0890         return videobuf_querybuf(&fh->vbi_q, buf);
0891     return -EINVAL;
0892 }
0893 
0894 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
0895 {
0896     struct saa7146_fh *fh = __fh;
0897 
0898     if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0899         return videobuf_qbuf(&fh->video_q, buf);
0900     if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
0901         return videobuf_qbuf(&fh->vbi_q, buf);
0902     return -EINVAL;
0903 }
0904 
0905 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
0906 {
0907     struct saa7146_fh *fh = __fh;
0908 
0909     if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0910         return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
0911     if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
0912         return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
0913     return -EINVAL;
0914 }
0915 
0916 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
0917 {
0918     struct saa7146_fh *fh = __fh;
0919     int err;
0920 
0921     DEB_D("VIDIOC_STREAMON, type:%d\n", type);
0922 
0923     err = video_begin(fh);
0924     if (err)
0925         return err;
0926     if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0927         return videobuf_streamon(&fh->video_q);
0928     if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
0929         return videobuf_streamon(&fh->vbi_q);
0930     return -EINVAL;
0931 }
0932 
0933 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
0934 {
0935     struct saa7146_fh *fh = __fh;
0936     struct saa7146_dev *dev = fh->dev;
0937     struct saa7146_vv *vv = dev->vv_data;
0938     int err;
0939 
0940     DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
0941 
0942     /* ugly: we need to copy some checks from video_end(),
0943        because videobuf_streamoff() relies on the capture running.
0944        check and fix this */
0945     if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
0946         DEB_S("not capturing\n");
0947         return 0;
0948     }
0949 
0950     if (vv->video_fh != fh) {
0951         DEB_S("capturing, but in another open\n");
0952         return -EBUSY;
0953     }
0954 
0955     err = -EINVAL;
0956     if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0957         err = videobuf_streamoff(&fh->video_q);
0958     else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
0959         err = videobuf_streamoff(&fh->vbi_q);
0960     if (0 != err) {
0961         DEB_D("warning: videobuf_streamoff() failed\n");
0962         video_end(fh, file);
0963     } else {
0964         err = video_end(fh, file);
0965     }
0966     return err;
0967 }
0968 
0969 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
0970     .vidioc_querycap             = vidioc_querycap,
0971     .vidioc_enum_fmt_vid_cap     = vidioc_enum_fmt_vid_cap,
0972     .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
0973     .vidioc_g_fmt_vid_cap        = vidioc_g_fmt_vid_cap,
0974     .vidioc_try_fmt_vid_cap      = vidioc_try_fmt_vid_cap,
0975     .vidioc_s_fmt_vid_cap        = vidioc_s_fmt_vid_cap,
0976     .vidioc_g_fmt_vid_overlay    = vidioc_g_fmt_vid_overlay,
0977     .vidioc_try_fmt_vid_overlay  = vidioc_try_fmt_vid_overlay,
0978     .vidioc_s_fmt_vid_overlay    = vidioc_s_fmt_vid_overlay,
0979 
0980     .vidioc_overlay          = vidioc_overlay,
0981     .vidioc_g_fbuf           = vidioc_g_fbuf,
0982     .vidioc_s_fbuf           = vidioc_s_fbuf,
0983     .vidioc_reqbufs              = vidioc_reqbufs,
0984     .vidioc_querybuf             = vidioc_querybuf,
0985     .vidioc_qbuf                 = vidioc_qbuf,
0986     .vidioc_dqbuf                = vidioc_dqbuf,
0987     .vidioc_g_std                = vidioc_g_std,
0988     .vidioc_s_std                = vidioc_s_std,
0989     .vidioc_streamon             = vidioc_streamon,
0990     .vidioc_streamoff            = vidioc_streamoff,
0991     .vidioc_g_parm           = vidioc_g_parm,
0992     .vidioc_subscribe_event      = v4l2_ctrl_subscribe_event,
0993     .vidioc_unsubscribe_event    = v4l2_event_unsubscribe,
0994 };
0995 
0996 const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
0997     .vidioc_querycap             = vidioc_querycap,
0998     .vidioc_g_fmt_vbi_cap        = vidioc_g_fmt_vbi_cap,
0999 
1000     .vidioc_reqbufs              = vidioc_reqbufs,
1001     .vidioc_querybuf             = vidioc_querybuf,
1002     .vidioc_qbuf                 = vidioc_qbuf,
1003     .vidioc_dqbuf                = vidioc_dqbuf,
1004     .vidioc_g_std                = vidioc_g_std,
1005     .vidioc_s_std                = vidioc_s_std,
1006     .vidioc_streamon             = vidioc_streamon,
1007     .vidioc_streamoff            = vidioc_streamoff,
1008     .vidioc_g_parm           = vidioc_g_parm,
1009     .vidioc_subscribe_event      = v4l2_ctrl_subscribe_event,
1010     .vidioc_unsubscribe_event    = v4l2_event_unsubscribe,
1011 };
1012 
1013 /*********************************************************************************/
1014 /* buffer handling functions                                                  */
1015 
1016 static int buffer_activate (struct saa7146_dev *dev,
1017              struct saa7146_buf *buf,
1018              struct saa7146_buf *next)
1019 {
1020     struct saa7146_vv *vv = dev->vv_data;
1021 
1022     buf->vb.state = VIDEOBUF_ACTIVE;
1023     saa7146_set_capture(dev,buf,next);
1024 
1025     mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
1026     return 0;
1027 }
1028 
1029 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1030 {
1031     saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1032     saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1033     saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1034 }
1035 
1036 static int buffer_prepare(struct videobuf_queue *q,
1037               struct videobuf_buffer *vb, enum v4l2_field field)
1038 {
1039     struct file *file = q->priv_data;
1040     struct saa7146_fh *fh = file->private_data;
1041     struct saa7146_dev *dev = fh->dev;
1042     struct saa7146_vv *vv = dev->vv_data;
1043     struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1044     int size,err = 0;
1045 
1046     DEB_CAP("vbuf:%p\n", vb);
1047 
1048     /* sanity checks */
1049     if (vv->video_fmt.width  < 48 ||
1050         vv->video_fmt.height < 32 ||
1051         vv->video_fmt.width  > vv->standard->h_max_out ||
1052         vv->video_fmt.height > vv->standard->v_max_out) {
1053         DEB_D("w (%d) / h (%d) out of bounds\n",
1054               vv->video_fmt.width, vv->video_fmt.height);
1055         return -EINVAL;
1056     }
1057 
1058     size = vv->video_fmt.sizeimage;
1059     if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1060         DEB_D("size mismatch\n");
1061         return -EINVAL;
1062     }
1063 
1064     DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1065         vv->video_fmt.width, vv->video_fmt.height,
1066         size, v4l2_field_names[vv->video_fmt.field]);
1067     if (buf->vb.width  != vv->video_fmt.width  ||
1068         buf->vb.bytesperline != vv->video_fmt.bytesperline ||
1069         buf->vb.height != vv->video_fmt.height ||
1070         buf->vb.size   != size ||
1071         buf->vb.field  != field      ||
1072         buf->vb.field  != vv->video_fmt.field  ||
1073         buf->fmt       != &vv->video_fmt) {
1074         saa7146_dma_free(dev,q,buf);
1075     }
1076 
1077     if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1078         struct saa7146_format *sfmt;
1079 
1080         buf->vb.bytesperline  = vv->video_fmt.bytesperline;
1081         buf->vb.width  = vv->video_fmt.width;
1082         buf->vb.height = vv->video_fmt.height;
1083         buf->vb.size   = size;
1084         buf->vb.field  = field;
1085         buf->fmt       = &vv->video_fmt;
1086         buf->vb.field  = vv->video_fmt.field;
1087 
1088         sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
1089 
1090         release_all_pagetables(dev, buf);
1091         if( 0 != IS_PLANAR(sfmt->trans)) {
1092             saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1093             saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1094             saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1095         } else {
1096             saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1097         }
1098 
1099         err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1100         if (err)
1101             goto oops;
1102         err = saa7146_pgtable_build(dev,buf);
1103         if (err)
1104             goto oops;
1105     }
1106     buf->vb.state = VIDEOBUF_PREPARED;
1107     buf->activate = buffer_activate;
1108 
1109     return 0;
1110 
1111  oops:
1112     DEB_D("error out\n");
1113     saa7146_dma_free(dev,q,buf);
1114 
1115     return err;
1116 }
1117 
1118 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1119 {
1120     struct file *file = q->priv_data;
1121     struct saa7146_fh *fh = file->private_data;
1122     struct saa7146_vv *vv = fh->dev->vv_data;
1123 
1124     if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1125         *count = MAX_SAA7146_CAPTURE_BUFFERS;
1126 
1127     *size = vv->video_fmt.sizeimage;
1128 
1129     /* check if we exceed the "max_memory" parameter */
1130     if( (*count * *size) > (max_memory*1048576) ) {
1131         *count = (max_memory*1048576) / *size;
1132     }
1133 
1134     DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
1135 
1136     return 0;
1137 }
1138 
1139 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1140 {
1141     struct file *file = q->priv_data;
1142     struct saa7146_fh *fh = file->private_data;
1143     struct saa7146_dev *dev = fh->dev;
1144     struct saa7146_vv *vv = dev->vv_data;
1145     struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1146 
1147     DEB_CAP("vbuf:%p\n", vb);
1148     saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
1149 }
1150 
1151 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1152 {
1153     struct file *file = q->priv_data;
1154     struct saa7146_fh *fh = file->private_data;
1155     struct saa7146_dev *dev = fh->dev;
1156     struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1157 
1158     DEB_CAP("vbuf:%p\n", vb);
1159 
1160     saa7146_dma_free(dev,q,buf);
1161 
1162     release_all_pagetables(dev, buf);
1163 }
1164 
1165 static const struct videobuf_queue_ops video_qops = {
1166     .buf_setup    = buffer_setup,
1167     .buf_prepare  = buffer_prepare,
1168     .buf_queue    = buffer_queue,
1169     .buf_release  = buffer_release,
1170 };
1171 
1172 /********************************************************************************/
1173 /* file operations */
1174 
1175 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1176 {
1177     INIT_LIST_HEAD(&vv->video_dmaq.queue);
1178 
1179     timer_setup(&vv->video_dmaq.timeout, saa7146_buffer_timeout, 0);
1180     vv->video_dmaq.dev              = dev;
1181 
1182     /* set some default values */
1183     vv->standard = &dev->ext_vv_data->stds[0];
1184 
1185     /* FIXME: what's this? */
1186     vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1187     vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1188 }
1189 
1190 
1191 static int video_open(struct saa7146_dev *dev, struct file *file)
1192 {
1193     struct saa7146_fh *fh = file->private_data;
1194 
1195     videobuf_queue_sg_init(&fh->video_q, &video_qops,
1196                 &dev->pci->dev, &dev->slock,
1197                 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1198                 V4L2_FIELD_INTERLACED,
1199                 sizeof(struct saa7146_buf),
1200                 file, &dev->v4l2_lock);
1201 
1202     return 0;
1203 }
1204 
1205 
1206 static void video_close(struct saa7146_dev *dev, struct file *file)
1207 {
1208     struct saa7146_fh *fh = file->private_data;
1209     struct saa7146_vv *vv = dev->vv_data;
1210     struct videobuf_queue *q = &fh->video_q;
1211 
1212     if (IS_CAPTURE_ACTIVE(fh) != 0)
1213         video_end(fh, file);
1214     else if (IS_OVERLAY_ACTIVE(fh) != 0)
1215         saa7146_stop_preview(fh);
1216 
1217     videobuf_stop(q);
1218     /* hmm, why is this function declared void? */
1219 }
1220 
1221 
1222 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1223 {
1224     struct saa7146_vv *vv = dev->vv_data;
1225     struct saa7146_dmaqueue *q = &vv->video_dmaq;
1226 
1227     spin_lock(&dev->slock);
1228     DEB_CAP("called\n");
1229 
1230     /* only finish the buffer if we have one... */
1231     if( NULL != q->curr ) {
1232         saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1233     }
1234     saa7146_buffer_next(dev,q,0);
1235 
1236     spin_unlock(&dev->slock);
1237 }
1238 
1239 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1240 {
1241     struct saa7146_fh *fh = file->private_data;
1242     struct saa7146_dev *dev = fh->dev;
1243     struct saa7146_vv *vv = dev->vv_data;
1244     ssize_t ret = 0;
1245 
1246     DEB_EE("called\n");
1247 
1248     if ((vv->video_status & STATUS_CAPTURE) != 0) {
1249         /* fixme: should we allow read() captures while streaming capture? */
1250         if (vv->video_fh == fh) {
1251             DEB_S("already capturing\n");
1252             return -EBUSY;
1253         }
1254         DEB_S("already capturing in another open\n");
1255         return -EBUSY;
1256     }
1257 
1258     ret = video_begin(fh);
1259     if( 0 != ret) {
1260         goto out;
1261     }
1262 
1263     ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1264                 file->f_flags & O_NONBLOCK);
1265     if (ret != 0) {
1266         video_end(fh, file);
1267     } else {
1268         ret = video_end(fh, file);
1269     }
1270 out:
1271     /* restart overlay if it was active before */
1272     if (vv->ov_suspend != NULL) {
1273         saa7146_start_preview(vv->ov_suspend);
1274         vv->ov_suspend = NULL;
1275     }
1276 
1277     return ret;
1278 }
1279 
1280 const struct saa7146_use_ops saa7146_video_uops = {
1281     .init = video_init,
1282     .open = video_open,
1283     .release = video_close,
1284     .irq_done = video_irq_done,
1285     .read = video_read,
1286 };