1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <media/saa7146_vv.h>
4 #include <media/v4l2-chip-ident.h>
5 #include <media/v4l2-event.h>
6 #include <media/v4l2-ctrls.h>
7 #include <linux/module.h>
9 static int max_memory = 32;
11 module_param(max_memory, int, 0644);
12 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
14 #define IS_CAPTURE_ACTIVE(fh) \
15 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
17 #define IS_OVERLAY_ACTIVE(fh) \
18 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
20 /* format descriptions for capture and preview */
21 static struct saa7146_format formats[] = {
23 .name = "RGB-8 (3-3-2)",
24 .pixelformat = V4L2_PIX_FMT_RGB332,
25 .trans = RGB08_COMPOSED,
29 .name = "RGB-16 (5/B-6/G-5/R)",
30 .pixelformat = V4L2_PIX_FMT_RGB565,
31 .trans = RGB16_COMPOSED,
35 .name = "RGB-24 (B-G-R)",
36 .pixelformat = V4L2_PIX_FMT_BGR24,
37 .trans = RGB24_COMPOSED,
41 .name = "RGB-32 (B-G-R)",
42 .pixelformat = V4L2_PIX_FMT_BGR32,
43 .trans = RGB32_COMPOSED,
47 .name = "RGB-32 (R-G-B)",
48 .pixelformat = V4L2_PIX_FMT_RGB32,
49 .trans = RGB32_COMPOSED,
54 .name = "Greyscale-8",
55 .pixelformat = V4L2_PIX_FMT_GREY,
60 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
61 .pixelformat = V4L2_PIX_FMT_YUV422P,
62 .trans = YUV422_DECOMPOSED,
64 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
66 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
67 .pixelformat = V4L2_PIX_FMT_YVU420,
68 .trans = YUV420_DECOMPOSED,
70 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
72 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
73 .pixelformat = V4L2_PIX_FMT_YUV420,
74 .trans = YUV420_DECOMPOSED,
76 .flags = FORMAT_IS_PLANAR,
78 .name = "YUV 4:2:2 (U-Y-V-Y)",
79 .pixelformat = V4L2_PIX_FMT_UYVY,
80 .trans = YUV422_COMPOSED,
86 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
87 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
88 (like V4L2_PIX_FMT_YUYV) ... 8-( */
90 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
92 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
94 int i, j = NUM_FORMATS;
96 for (i = 0; i < j; i++) {
97 if (formats[i].pixelformat == fourcc) {
102 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
106 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
108 int saa7146_start_preview(struct saa7146_fh *fh)
110 struct saa7146_dev *dev = fh->dev;
111 struct saa7146_vv *vv = dev->vv_data;
112 struct v4l2_format fmt;
113 int ret = 0, err = 0;
115 DEB_EE("dev:%p, fh:%p\n", dev, fh);
117 /* check if we have overlay information */
118 if (vv->ov.fh == NULL) {
119 DEB_D("no overlay data available. try S_FMT first.\n");
123 /* check if streaming capture is running */
124 if (IS_CAPTURE_ACTIVE(fh) != 0) {
125 DEB_D("streaming capture is active\n");
129 /* check if overlay is running */
130 if (IS_OVERLAY_ACTIVE(fh) != 0) {
131 if (vv->video_fh == fh) {
132 DEB_D("overlay is already active\n");
135 DEB_D("overlay is already active in another open\n");
139 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
140 DEB_D("cannot get necessary overlay resources\n");
144 fmt.fmt.win = vv->ov.win;
145 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
147 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
150 vv->ov.win = fmt.fmt.win;
152 DEB_D("%dx%d+%d+%d %s field=%s\n",
153 vv->ov.win.w.width, vv->ov.win.w.height,
154 vv->ov.win.w.left, vv->ov.win.w.top,
155 vv->ov_fmt->name, v4l2_field_names[vv->ov.win.field]);
157 if (0 != (ret = saa7146_enable_overlay(fh))) {
158 DEB_D("enabling overlay failed: %d\n", ret);
159 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
163 vv->video_status = STATUS_OVERLAY;
168 EXPORT_SYMBOL_GPL(saa7146_start_preview);
170 int saa7146_stop_preview(struct saa7146_fh *fh)
172 struct saa7146_dev *dev = fh->dev;
173 struct saa7146_vv *vv = dev->vv_data;
175 DEB_EE("dev:%p, fh:%p\n", dev, fh);
177 /* check if streaming capture is running */
178 if (IS_CAPTURE_ACTIVE(fh) != 0) {
179 DEB_D("streaming capture is active\n");
183 /* check if overlay is running at all */
184 if ((vv->video_status & STATUS_OVERLAY) == 0) {
185 DEB_D("no active overlay\n");
189 if (vv->video_fh != fh) {
190 DEB_D("overlay is active, but in another open\n");
194 vv->video_status = 0;
197 saa7146_disable_overlay(fh);
199 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
203 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
205 /********************************************************************************/
206 /* common pagetable functions */
208 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
210 struct pci_dev *pci = dev->pci;
211 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
212 struct scatterlist *list = dma->sglist;
213 int length = dma->sglen;
214 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
216 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
218 if( 0 != IS_PLANAR(sfmt->trans)) {
219 struct saa7146_pgtable *pt1 = &buf->pt[0];
220 struct saa7146_pgtable *pt2 = &buf->pt[1];
221 struct saa7146_pgtable *pt3 = &buf->pt[2];
222 __le32 *ptr1, *ptr2, *ptr3;
225 int size = buf->fmt->width*buf->fmt->height;
226 int i,p,m1,m2,m3,o1,o2;
228 switch( sfmt->depth ) {
230 /* create some offsets inside the page table */
231 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
232 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
233 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
235 o2 = (size+(size/4))%PAGE_SIZE;
236 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
237 size, m1, m2, m3, o1, o2);
241 /* create some offsets inside the page table */
242 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
243 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
244 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
246 o2 = (size+(size/2))%PAGE_SIZE;
247 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
248 size, m1, m2, m3, o1, o2);
260 /* walk all pages, copy all page addresses to ptr1 */
261 for (i = 0; i < length; i++, list++) {
262 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
263 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
269 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
273 /* if we have a user buffer, the first page may not be
274 aligned to a page boundary. */
275 pt1->offset = dma->sglist->offset;
276 pt2->offset = pt1->offset+o1;
277 pt3->offset = pt1->offset+o2;
279 /* create video-dma2 page table */
281 for(i = m1; i <= m2 ; i++, ptr2++) {
285 for(;i<1024;i++,ptr2++) {
288 /* create video-dma3 page table */
290 for(i = m2; i <= m3; i++,ptr3++) {
294 for(;i<1024;i++,ptr3++) {
297 /* finally: finish up video-dma1 page table */
300 for(i=m1;i<1024;i++,ptr1++) {
308 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
311 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
314 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
318 struct saa7146_pgtable *pt = &buf->pt[0];
319 return saa7146_pgtable_build_single(pci, pt, list, length);
326 /********************************************************************************/
327 /* file operations */
329 static int video_begin(struct saa7146_fh *fh)
331 struct saa7146_dev *dev = fh->dev;
332 struct saa7146_vv *vv = dev->vv_data;
333 struct saa7146_format *fmt = NULL;
334 unsigned int resource;
335 int ret = 0, err = 0;
337 DEB_EE("dev:%p, fh:%p\n", dev, fh);
339 if ((vv->video_status & STATUS_CAPTURE) != 0) {
340 if (vv->video_fh == fh) {
341 DEB_S("already capturing\n");
344 DEB_S("already capturing in another open\n");
348 if ((vv->video_status & STATUS_OVERLAY) != 0) {
349 DEB_S("warning: suspending overlay video for streaming capture\n");
350 vv->ov_suspend = vv->video_fh;
351 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
353 DEB_D("suspending video failed. aborting\n");
358 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
359 /* we need to have a valid format set here */
362 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
363 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
365 resource = RESOURCE_DMA1_HPS;
368 ret = saa7146_res_get(fh, resource);
370 DEB_S("cannot get capture resource %d\n", resource);
371 if (vv->ov_suspend != NULL) {
372 saa7146_start_preview(vv->ov_suspend);
373 vv->ov_suspend = NULL;
378 /* clear out beginning of streaming bit (rps register 0)*/
379 saa7146_write(dev, MC2, MASK_27 );
381 /* enable rps0 irqs */
382 SAA7146_IER_ENABLE(dev, MASK_27);
385 vv->video_status = STATUS_CAPTURE;
390 static int video_end(struct saa7146_fh *fh, struct file *file)
392 struct saa7146_dev *dev = fh->dev;
393 struct saa7146_vv *vv = dev->vv_data;
394 struct saa7146_format *fmt = NULL;
396 unsigned int resource;
398 DEB_EE("dev:%p, fh:%p\n", dev, fh);
400 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
401 DEB_S("not capturing\n");
405 if (vv->video_fh != fh) {
406 DEB_S("capturing, but in another open\n");
410 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
411 /* we need to have a valid format set here */
414 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
415 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
416 dmas = MASK_22 | MASK_21 | MASK_20;
418 resource = RESOURCE_DMA1_HPS;
421 spin_lock_irqsave(&dev->slock,flags);
424 saa7146_write(dev, MC1, MASK_28);
426 /* disable rps0 irqs */
427 SAA7146_IER_DISABLE(dev, MASK_27);
429 /* shut down all used video dma transfers */
430 saa7146_write(dev, MC1, dmas);
432 spin_unlock_irqrestore(&dev->slock, flags);
435 vv->video_status = 0;
437 saa7146_res_free(fh, resource);
439 if (vv->ov_suspend != NULL) {
440 saa7146_start_preview(vv->ov_suspend);
441 vv->ov_suspend = NULL;
447 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
449 struct video_device *vdev = video_devdata(file);
450 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
452 strcpy((char *)cap->driver, "saa7146 v4l2");
453 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
454 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
456 V4L2_CAP_VIDEO_CAPTURE |
457 V4L2_CAP_VIDEO_OVERLAY |
460 cap->device_caps |= dev->ext_vv_data->capabilities;
461 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
462 if (vdev->vfl_type == VFL_TYPE_GRABBER)
464 ~(V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT);
467 ~(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_AUDIO);
471 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
473 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
474 struct saa7146_vv *vv = dev->vv_data;
477 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
478 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
482 static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
484 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
485 struct saa7146_vv *vv = dev->vv_data;
486 struct saa7146_format *fmt;
488 DEB_EE("VIDIOC_S_FBUF\n");
490 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
494 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
498 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
499 if (fmt->flags & FORMAT_IS_PLANAR)
500 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
501 (char *)&fmt->pixelformat);
503 /* check if overlay is running */
504 if (IS_OVERLAY_ACTIVE(fh) != 0) {
505 if (vv->video_fh != fh) {
506 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
515 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
516 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
517 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
522 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
524 if (f->index >= NUM_FORMATS)
526 strlcpy((char *)f->description, formats[f->index].name,
527 sizeof(f->description));
528 f->pixelformat = formats[f->index].pixelformat;
532 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
534 struct saa7146_dev *dev = container_of(ctrl->handler,
535 struct saa7146_dev, ctrl_handler);
536 struct saa7146_vv *vv = dev->vv_data;
540 case V4L2_CID_BRIGHTNESS:
541 val = saa7146_read(dev, BCS_CTRL);
543 val |= (ctrl->val << 24);
544 saa7146_write(dev, BCS_CTRL, val);
545 saa7146_write(dev, MC2, MASK_22 | MASK_06);
548 case V4L2_CID_CONTRAST:
549 val = saa7146_read(dev, BCS_CTRL);
551 val |= (ctrl->val << 16);
552 saa7146_write(dev, BCS_CTRL, val);
553 saa7146_write(dev, MC2, MASK_22 | MASK_06);
556 case V4L2_CID_SATURATION:
557 val = saa7146_read(dev, BCS_CTRL);
559 val |= (ctrl->val << 0);
560 saa7146_write(dev, BCS_CTRL, val);
561 saa7146_write(dev, MC2, MASK_22 | MASK_06);
565 /* fixme: we can support changing VFLIP and HFLIP here... */
566 if ((vv->video_status & STATUS_CAPTURE))
568 vv->hflip = ctrl->val;
572 if ((vv->video_status & STATUS_CAPTURE))
574 vv->vflip = ctrl->val;
581 if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
582 struct saa7146_fh *fh = vv->video_fh;
584 saa7146_stop_preview(fh);
585 saa7146_start_preview(fh);
590 static int vidioc_g_parm(struct file *file, void *fh,
591 struct v4l2_streamparm *parm)
593 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
594 struct saa7146_vv *vv = dev->vv_data;
596 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
598 parm->parm.capture.readbuffers = 1;
599 v4l2_video_std_frame_period(vv->standard->id,
600 &parm->parm.capture.timeperframe);
604 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
606 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
607 struct saa7146_vv *vv = dev->vv_data;
609 f->fmt.pix = vv->video_fmt;
613 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
615 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
616 struct saa7146_vv *vv = dev->vv_data;
618 f->fmt.win = vv->ov.win;
622 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
624 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
625 struct saa7146_vv *vv = dev->vv_data;
627 f->fmt.vbi = vv->vbi_fmt;
631 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
633 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
634 struct saa7146_vv *vv = dev->vv_data;
635 struct saa7146_format *fmt;
636 enum v4l2_field field;
640 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
642 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
646 field = f->fmt.pix.field;
647 maxw = vv->standard->h_max_out;
648 maxh = vv->standard->v_max_out;
650 if (V4L2_FIELD_ANY == field) {
651 field = (f->fmt.pix.height > maxh / 2)
652 ? V4L2_FIELD_INTERLACED
656 case V4L2_FIELD_ALTERNATE:
657 vv->last_field = V4L2_FIELD_TOP;
661 case V4L2_FIELD_BOTTOM:
662 vv->last_field = V4L2_FIELD_INTERLACED;
665 case V4L2_FIELD_INTERLACED:
666 vv->last_field = V4L2_FIELD_INTERLACED;
669 DEB_D("no known field mode '%d'\n", field);
673 f->fmt.pix.field = field;
674 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
675 if (f->fmt.pix.width > maxw)
676 f->fmt.pix.width = maxw;
677 if (f->fmt.pix.height > maxh)
678 f->fmt.pix.height = maxh;
680 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
682 if (f->fmt.pix.bytesperline < calc_bpl)
683 f->fmt.pix.bytesperline = calc_bpl;
685 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
686 f->fmt.pix.bytesperline = calc_bpl;
688 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
689 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
690 f->fmt.pix.width, f->fmt.pix.height,
691 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
697 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
699 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
700 struct saa7146_vv *vv = dev->vv_data;
701 struct v4l2_window *win = &f->fmt.win;
702 enum v4l2_field field;
705 DEB_EE("dev:%p\n", dev);
707 if (NULL == vv->ov_fb.base) {
708 DEB_D("no fb base set\n");
711 if (NULL == vv->ov_fmt) {
712 DEB_D("no fb fmt set\n");
715 if (win->w.width < 48 || win->w.height < 32) {
716 DEB_D("min width/height. (%d,%d)\n",
717 win->w.width, win->w.height);
720 if (win->clipcount > 16) {
721 DEB_D("clipcount too big\n");
726 maxw = vv->standard->h_max_out;
727 maxh = vv->standard->v_max_out;
729 if (V4L2_FIELD_ANY == field) {
730 field = (win->w.height > maxh / 2)
731 ? V4L2_FIELD_INTERLACED
736 case V4L2_FIELD_BOTTOM:
737 case V4L2_FIELD_ALTERNATE:
740 case V4L2_FIELD_INTERLACED:
743 DEB_D("no known field mode '%d'\n", field);
748 if (win->w.width > maxw)
750 if (win->w.height > maxh)
751 win->w.height = maxh;
756 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
758 struct saa7146_fh *fh = __fh;
759 struct saa7146_dev *dev = fh->dev;
760 struct saa7146_vv *vv = dev->vv_data;
763 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
764 if (IS_CAPTURE_ACTIVE(fh) != 0) {
765 DEB_EE("streaming capture is active\n");
768 err = vidioc_try_fmt_vid_cap(file, fh, f);
771 vv->video_fmt = f->fmt.pix;
772 DEB_EE("set to pixelformat '%4.4s'\n",
773 (char *)&vv->video_fmt.pixelformat);
777 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
779 struct saa7146_fh *fh = __fh;
780 struct saa7146_dev *dev = fh->dev;
781 struct saa7146_vv *vv = dev->vv_data;
784 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
785 err = vidioc_try_fmt_vid_overlay(file, fh, f);
788 vv->ov.win = f->fmt.win;
789 vv->ov.nclips = f->fmt.win.clipcount;
790 if (vv->ov.nclips > 16)
792 if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
793 sizeof(struct v4l2_clip) * vv->ov.nclips)) {
797 /* vv->ov.fh is used to indicate that we have valid overlay informations, too */
800 /* check if our current overlay is active */
801 if (IS_OVERLAY_ACTIVE(fh) != 0) {
802 saa7146_stop_preview(fh);
803 saa7146_start_preview(fh);
808 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
810 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
811 struct saa7146_vv *vv = dev->vv_data;
813 *norm = vv->standard->id;
817 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
818 PAL / NTSC / SECAM. if your hardware does not (or does more)
819 -- override this function in your extension */
823 struct v4l2_standard *e = arg;
826 if( e->index < dev->ext_vv_data->num_stds ) {
827 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
828 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
835 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
837 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
838 struct saa7146_vv *vv = dev->vv_data;
842 DEB_EE("VIDIOC_S_STD\n");
844 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
845 DEB_D("cannot change video standard while streaming capture is active\n");
849 if ((vv->video_status & STATUS_OVERLAY) != 0) {
850 vv->ov_suspend = vv->video_fh;
851 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
853 DEB_D("suspending video failed. aborting\n");
858 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
859 if (*id & dev->ext_vv_data->stds[i].id)
861 if (i != dev->ext_vv_data->num_stds) {
862 vv->standard = &dev->ext_vv_data->stds[i];
863 if (NULL != dev->ext_vv_data->std_callback)
864 dev->ext_vv_data->std_callback(dev, vv->standard);
868 if (vv->ov_suspend != NULL) {
869 saa7146_start_preview(vv->ov_suspend);
870 vv->ov_suspend = NULL;
874 DEB_EE("VIDIOC_S_STD: standard not found\n");
878 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
882 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
886 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
888 err = saa7146_start_preview(fh);
890 err = saa7146_stop_preview(fh);
894 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
896 struct saa7146_fh *fh = __fh;
898 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
899 return videobuf_reqbufs(&fh->video_q, b);
900 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
901 return videobuf_reqbufs(&fh->vbi_q, b);
905 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
907 struct saa7146_fh *fh = __fh;
909 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
910 return videobuf_querybuf(&fh->video_q, buf);
911 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
912 return videobuf_querybuf(&fh->vbi_q, buf);
916 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
918 struct saa7146_fh *fh = __fh;
920 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
921 return videobuf_qbuf(&fh->video_q, buf);
922 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
923 return videobuf_qbuf(&fh->vbi_q, buf);
927 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
929 struct saa7146_fh *fh = __fh;
931 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
933 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
934 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
938 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
940 struct saa7146_fh *fh = __fh;
943 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
945 err = video_begin(fh);
948 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
949 return videobuf_streamon(&fh->video_q);
950 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
951 return videobuf_streamon(&fh->vbi_q);
955 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
957 struct saa7146_fh *fh = __fh;
958 struct saa7146_dev *dev = fh->dev;
959 struct saa7146_vv *vv = dev->vv_data;
962 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
964 /* ugly: we need to copy some checks from video_end(),
965 because videobuf_streamoff() relies on the capture running.
966 check and fix this */
967 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
968 DEB_S("not capturing\n");
972 if (vv->video_fh != fh) {
973 DEB_S("capturing, but in another open\n");
978 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
979 err = videobuf_streamoff(&fh->video_q);
980 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
981 err = videobuf_streamoff(&fh->vbi_q);
983 DEB_D("warning: videobuf_streamoff() failed\n");
986 err = video_end(fh, file);
991 static int vidioc_g_chip_ident(struct file *file, void *__fh,
992 struct v4l2_dbg_chip_ident *chip)
994 struct saa7146_fh *fh = __fh;
995 struct saa7146_dev *dev = fh->dev;
997 chip->ident = V4L2_IDENT_NONE;
999 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1000 if (v4l2_chip_match_host(&chip->match))
1001 chip->ident = V4L2_IDENT_SAA7146;
1004 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1005 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1007 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1008 core, g_chip_ident, chip);
1011 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1012 .vidioc_querycap = vidioc_querycap,
1013 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1014 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
1015 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1016 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1017 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1018 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1019 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1020 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1021 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1023 .vidioc_overlay = vidioc_overlay,
1024 .vidioc_g_fbuf = vidioc_g_fbuf,
1025 .vidioc_s_fbuf = vidioc_s_fbuf,
1026 .vidioc_reqbufs = vidioc_reqbufs,
1027 .vidioc_querybuf = vidioc_querybuf,
1028 .vidioc_qbuf = vidioc_qbuf,
1029 .vidioc_dqbuf = vidioc_dqbuf,
1030 .vidioc_g_std = vidioc_g_std,
1031 .vidioc_s_std = vidioc_s_std,
1032 .vidioc_streamon = vidioc_streamon,
1033 .vidioc_streamoff = vidioc_streamoff,
1034 .vidioc_g_parm = vidioc_g_parm,
1035 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1036 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1039 const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
1040 .vidioc_querycap = vidioc_querycap,
1041 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1042 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1044 .vidioc_reqbufs = vidioc_reqbufs,
1045 .vidioc_querybuf = vidioc_querybuf,
1046 .vidioc_qbuf = vidioc_qbuf,
1047 .vidioc_dqbuf = vidioc_dqbuf,
1048 .vidioc_g_std = vidioc_g_std,
1049 .vidioc_s_std = vidioc_s_std,
1050 .vidioc_streamon = vidioc_streamon,
1051 .vidioc_streamoff = vidioc_streamoff,
1052 .vidioc_g_parm = vidioc_g_parm,
1053 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1054 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1057 /*********************************************************************************/
1058 /* buffer handling functions */
1060 static int buffer_activate (struct saa7146_dev *dev,
1061 struct saa7146_buf *buf,
1062 struct saa7146_buf *next)
1064 struct saa7146_vv *vv = dev->vv_data;
1066 buf->vb.state = VIDEOBUF_ACTIVE;
1067 saa7146_set_capture(dev,buf,next);
1069 mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
1073 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1075 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1076 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1077 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1080 static int buffer_prepare(struct videobuf_queue *q,
1081 struct videobuf_buffer *vb, enum v4l2_field field)
1083 struct file *file = q->priv_data;
1084 struct saa7146_fh *fh = file->private_data;
1085 struct saa7146_dev *dev = fh->dev;
1086 struct saa7146_vv *vv = dev->vv_data;
1087 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1090 DEB_CAP("vbuf:%p\n", vb);
1093 if (vv->video_fmt.width < 48 ||
1094 vv->video_fmt.height < 32 ||
1095 vv->video_fmt.width > vv->standard->h_max_out ||
1096 vv->video_fmt.height > vv->standard->v_max_out) {
1097 DEB_D("w (%d) / h (%d) out of bounds\n",
1098 vv->video_fmt.width, vv->video_fmt.height);
1102 size = vv->video_fmt.sizeimage;
1103 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1104 DEB_D("size mismatch\n");
1108 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1109 vv->video_fmt.width, vv->video_fmt.height,
1110 size, v4l2_field_names[vv->video_fmt.field]);
1111 if (buf->vb.width != vv->video_fmt.width ||
1112 buf->vb.bytesperline != vv->video_fmt.bytesperline ||
1113 buf->vb.height != vv->video_fmt.height ||
1114 buf->vb.size != size ||
1115 buf->vb.field != field ||
1116 buf->vb.field != vv->video_fmt.field ||
1117 buf->fmt != &vv->video_fmt) {
1118 saa7146_dma_free(dev,q,buf);
1121 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1122 struct saa7146_format *sfmt;
1124 buf->vb.bytesperline = vv->video_fmt.bytesperline;
1125 buf->vb.width = vv->video_fmt.width;
1126 buf->vb.height = vv->video_fmt.height;
1127 buf->vb.size = size;
1128 buf->vb.field = field;
1129 buf->fmt = &vv->video_fmt;
1130 buf->vb.field = vv->video_fmt.field;
1132 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
1134 release_all_pagetables(dev, buf);
1135 if( 0 != IS_PLANAR(sfmt->trans)) {
1136 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1137 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1138 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1140 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1143 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1146 err = saa7146_pgtable_build(dev,buf);
1150 buf->vb.state = VIDEOBUF_PREPARED;
1151 buf->activate = buffer_activate;
1156 DEB_D("error out\n");
1157 saa7146_dma_free(dev,q,buf);
1162 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1164 struct file *file = q->priv_data;
1165 struct saa7146_fh *fh = file->private_data;
1166 struct saa7146_vv *vv = fh->dev->vv_data;
1168 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1169 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1171 *size = vv->video_fmt.sizeimage;
1173 /* check if we exceed the "max_memory" parameter */
1174 if( (*count * *size) > (max_memory*1048576) ) {
1175 *count = (max_memory*1048576) / *size;
1178 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
1183 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1185 struct file *file = q->priv_data;
1186 struct saa7146_fh *fh = file->private_data;
1187 struct saa7146_dev *dev = fh->dev;
1188 struct saa7146_vv *vv = dev->vv_data;
1189 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1191 DEB_CAP("vbuf:%p\n", vb);
1192 saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
1195 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1197 struct file *file = q->priv_data;
1198 struct saa7146_fh *fh = file->private_data;
1199 struct saa7146_dev *dev = fh->dev;
1200 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1202 DEB_CAP("vbuf:%p\n", vb);
1204 saa7146_dma_free(dev,q,buf);
1206 release_all_pagetables(dev, buf);
1209 static struct videobuf_queue_ops video_qops = {
1210 .buf_setup = buffer_setup,
1211 .buf_prepare = buffer_prepare,
1212 .buf_queue = buffer_queue,
1213 .buf_release = buffer_release,
1216 /********************************************************************************/
1217 /* file operations */
1219 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1221 INIT_LIST_HEAD(&vv->video_dmaq.queue);
1223 init_timer(&vv->video_dmaq.timeout);
1224 vv->video_dmaq.timeout.function = saa7146_buffer_timeout;
1225 vv->video_dmaq.timeout.data = (unsigned long)(&vv->video_dmaq);
1226 vv->video_dmaq.dev = dev;
1228 /* set some default values */
1229 vv->standard = &dev->ext_vv_data->stds[0];
1231 /* FIXME: what's this? */
1232 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1233 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1237 static int video_open(struct saa7146_dev *dev, struct file *file)
1239 struct saa7146_fh *fh = file->private_data;
1241 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1242 &dev->pci->dev, &dev->slock,
1243 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1244 V4L2_FIELD_INTERLACED,
1245 sizeof(struct saa7146_buf),
1246 file, &dev->v4l2_lock);
1252 static void video_close(struct saa7146_dev *dev, struct file *file)
1254 struct saa7146_fh *fh = file->private_data;
1255 struct saa7146_vv *vv = dev->vv_data;
1256 struct videobuf_queue *q = &fh->video_q;
1258 if (IS_CAPTURE_ACTIVE(fh) != 0)
1259 video_end(fh, file);
1260 else if (IS_OVERLAY_ACTIVE(fh) != 0)
1261 saa7146_stop_preview(fh);
1264 /* hmm, why is this function declared void? */
1268 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1270 struct saa7146_vv *vv = dev->vv_data;
1271 struct saa7146_dmaqueue *q = &vv->video_dmaq;
1273 spin_lock(&dev->slock);
1274 DEB_CAP("called\n");
1276 /* only finish the buffer if we have one... */
1277 if( NULL != q->curr ) {
1278 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1280 saa7146_buffer_next(dev,q,0);
1282 spin_unlock(&dev->slock);
1285 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1287 struct saa7146_fh *fh = file->private_data;
1288 struct saa7146_dev *dev = fh->dev;
1289 struct saa7146_vv *vv = dev->vv_data;
1294 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1295 /* fixme: should we allow read() captures while streaming capture? */
1296 if (vv->video_fh == fh) {
1297 DEB_S("already capturing\n");
1300 DEB_S("already capturing in another open\n");
1304 ret = video_begin(fh);
1309 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1310 file->f_flags & O_NONBLOCK);
1312 video_end(fh, file);
1314 ret = video_end(fh, file);
1317 /* restart overlay if it was active before */
1318 if (vv->ov_suspend != NULL) {
1319 saa7146_start_preview(vv->ov_suspend);
1320 vv->ov_suspend = NULL;
1326 struct saa7146_use_ops saa7146_video_uops = {
1329 .release = video_close,
1330 .irq_done = video_irq_done,