1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <media/drv-intf/saa7146_vv.h>
4 #include <media/v4l2-event.h>
5 #include <media/v4l2-ctrls.h>
6 #include <linux/module.h>
7 #include <linux/kernel.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 .pixelformat = V4L2_PIX_FMT_RGB332,
24 .trans = RGB08_COMPOSED,
28 .pixelformat = V4L2_PIX_FMT_RGB565,
29 .trans = RGB16_COMPOSED,
33 .pixelformat = V4L2_PIX_FMT_BGR24,
34 .trans = RGB24_COMPOSED,
38 .pixelformat = V4L2_PIX_FMT_BGR32,
39 .trans = RGB32_COMPOSED,
43 .pixelformat = V4L2_PIX_FMT_RGB32,
44 .trans = RGB32_COMPOSED,
49 .pixelformat = V4L2_PIX_FMT_GREY,
54 .pixelformat = V4L2_PIX_FMT_YUV422P,
55 .trans = YUV422_DECOMPOSED,
57 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
59 .pixelformat = V4L2_PIX_FMT_YVU420,
60 .trans = YUV420_DECOMPOSED,
62 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
64 .pixelformat = V4L2_PIX_FMT_YUV420,
65 .trans = YUV420_DECOMPOSED,
67 .flags = FORMAT_IS_PLANAR,
69 .pixelformat = V4L2_PIX_FMT_UYVY,
70 .trans = YUV422_COMPOSED,
76 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
77 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
78 (like V4L2_PIX_FMT_YUYV) ... 8-( */
80 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
84 for (i = 0; i < ARRAY_SIZE(formats); i++) {
85 if (formats[i].pixelformat == fourcc) {
90 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
94 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
96 int saa7146_start_preview(struct saa7146_fh *fh)
98 struct saa7146_dev *dev = fh->dev;
99 struct saa7146_vv *vv = dev->vv_data;
100 struct v4l2_format fmt;
101 int ret = 0, err = 0;
103 DEB_EE("dev:%p, fh:%p\n", dev, fh);
105 /* check if we have overlay information */
106 if (vv->ov.fh == NULL) {
107 DEB_D("no overlay data available. try S_FMT first.\n");
111 /* check if streaming capture is running */
112 if (IS_CAPTURE_ACTIVE(fh) != 0) {
113 DEB_D("streaming capture is active\n");
117 /* check if overlay is running */
118 if (IS_OVERLAY_ACTIVE(fh) != 0) {
119 if (vv->video_fh == fh) {
120 DEB_D("overlay is already active\n");
123 DEB_D("overlay is already active in another open\n");
127 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
128 DEB_D("cannot get necessary overlay resources\n");
132 fmt.fmt.win = vv->ov.win;
133 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
135 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
138 vv->ov.win = fmt.fmt.win;
140 DEB_D("%dx%d+%d+%d 0x%08x field=%s\n",
141 vv->ov.win.w.width, vv->ov.win.w.height,
142 vv->ov.win.w.left, vv->ov.win.w.top,
143 vv->ov_fmt->pixelformat, v4l2_field_names[vv->ov.win.field]);
145 if (0 != (ret = saa7146_enable_overlay(fh))) {
146 DEB_D("enabling overlay failed: %d\n", ret);
147 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
151 vv->video_status = STATUS_OVERLAY;
156 EXPORT_SYMBOL_GPL(saa7146_start_preview);
158 int saa7146_stop_preview(struct saa7146_fh *fh)
160 struct saa7146_dev *dev = fh->dev;
161 struct saa7146_vv *vv = dev->vv_data;
163 DEB_EE("dev:%p, fh:%p\n", dev, fh);
165 /* check if streaming capture is running */
166 if (IS_CAPTURE_ACTIVE(fh) != 0) {
167 DEB_D("streaming capture is active\n");
171 /* check if overlay is running at all */
172 if ((vv->video_status & STATUS_OVERLAY) == 0) {
173 DEB_D("no active overlay\n");
177 if (vv->video_fh != fh) {
178 DEB_D("overlay is active, but in another open\n");
182 vv->video_status = 0;
185 saa7146_disable_overlay(fh);
187 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
191 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
193 /********************************************************************************/
194 /* common pagetable functions */
196 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
198 struct pci_dev *pci = dev->pci;
199 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
200 struct scatterlist *list = dma->sglist;
201 int length = dma->sglen;
202 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
204 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
206 if( 0 != IS_PLANAR(sfmt->trans)) {
207 struct saa7146_pgtable *pt1 = &buf->pt[0];
208 struct saa7146_pgtable *pt2 = &buf->pt[1];
209 struct saa7146_pgtable *pt3 = &buf->pt[2];
210 __le32 *ptr1, *ptr2, *ptr3;
213 int size = buf->fmt->width*buf->fmt->height;
214 int i,p,m1,m2,m3,o1,o2;
216 switch( sfmt->depth ) {
218 /* create some offsets inside the page table */
219 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
220 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
221 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
223 o2 = (size+(size/4))%PAGE_SIZE;
224 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
225 size, m1, m2, m3, o1, o2);
229 /* create some offsets inside the page table */
230 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
231 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
232 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
234 o2 = (size+(size/2))%PAGE_SIZE;
235 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
236 size, m1, m2, m3, o1, o2);
248 /* walk all pages, copy all page addresses to ptr1 */
249 for (i = 0; i < length; i++, list++) {
250 for (p = 0; p * 4096 < sg_dma_len(list); p++, ptr1++)
251 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
256 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
260 /* if we have a user buffer, the first page may not be
261 aligned to a page boundary. */
262 pt1->offset = dma->sglist->offset;
263 pt2->offset = pt1->offset+o1;
264 pt3->offset = pt1->offset+o2;
266 /* create video-dma2 page table */
268 for(i = m1; i <= m2 ; i++, ptr2++) {
272 for(;i<1024;i++,ptr2++) {
275 /* create video-dma3 page table */
277 for(i = m2; i <= m3; i++,ptr3++) {
281 for(;i<1024;i++,ptr3++) {
284 /* finally: finish up video-dma1 page table */
287 for(i=m1;i<1024;i++,ptr1++) {
295 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
298 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
301 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
305 struct saa7146_pgtable *pt = &buf->pt[0];
306 return saa7146_pgtable_build_single(pci, pt, list, length);
313 /********************************************************************************/
314 /* file operations */
316 static int video_begin(struct saa7146_fh *fh)
318 struct saa7146_dev *dev = fh->dev;
319 struct saa7146_vv *vv = dev->vv_data;
320 struct saa7146_format *fmt = NULL;
321 unsigned int resource;
322 int ret = 0, err = 0;
324 DEB_EE("dev:%p, fh:%p\n", dev, fh);
326 if ((vv->video_status & STATUS_CAPTURE) != 0) {
327 if (vv->video_fh == fh) {
328 DEB_S("already capturing\n");
331 DEB_S("already capturing in another open\n");
335 if ((vv->video_status & STATUS_OVERLAY) != 0) {
336 DEB_S("warning: suspending overlay video for streaming capture\n");
337 vv->ov_suspend = vv->video_fh;
338 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
340 DEB_D("suspending video failed. aborting\n");
345 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
346 /* we need to have a valid format set here */
350 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
351 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
353 resource = RESOURCE_DMA1_HPS;
356 ret = saa7146_res_get(fh, resource);
358 DEB_S("cannot get capture resource %d\n", resource);
359 if (vv->ov_suspend != NULL) {
360 saa7146_start_preview(vv->ov_suspend);
361 vv->ov_suspend = NULL;
366 /* clear out beginning of streaming bit (rps register 0)*/
367 saa7146_write(dev, MC2, MASK_27 );
369 /* enable rps0 irqs */
370 SAA7146_IER_ENABLE(dev, MASK_27);
373 vv->video_status = STATUS_CAPTURE;
378 static int video_end(struct saa7146_fh *fh, struct file *file)
380 struct saa7146_dev *dev = fh->dev;
381 struct saa7146_vv *vv = dev->vv_data;
382 struct saa7146_dmaqueue *q = &vv->video_dmaq;
383 struct saa7146_format *fmt = NULL;
385 unsigned int resource;
387 DEB_EE("dev:%p, fh:%p\n", dev, fh);
389 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
390 DEB_S("not capturing\n");
394 if (vv->video_fh != fh) {
395 DEB_S("capturing, but in another open\n");
399 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
400 /* we need to have a valid format set here */
404 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
405 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
406 dmas = MASK_22 | MASK_21 | MASK_20;
408 resource = RESOURCE_DMA1_HPS;
411 spin_lock_irqsave(&dev->slock,flags);
414 saa7146_write(dev, MC1, MASK_28);
416 /* disable rps0 irqs */
417 SAA7146_IER_DISABLE(dev, MASK_27);
419 /* shut down all used video dma transfers */
420 saa7146_write(dev, MC1, dmas);
423 saa7146_buffer_finish(dev, q, VIDEOBUF_DONE);
425 spin_unlock_irqrestore(&dev->slock, flags);
428 vv->video_status = 0;
430 saa7146_res_free(fh, resource);
432 if (vv->ov_suspend != NULL) {
433 saa7146_start_preview(vv->ov_suspend);
434 vv->ov_suspend = NULL;
440 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
442 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
444 strscpy((char *)cap->driver, "saa7146 v4l2", sizeof(cap->driver));
445 strscpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
446 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
447 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
448 V4L2_CAP_DEVICE_CAPS;
449 cap->capabilities |= dev->ext_vv_data->capabilities;
453 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
455 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
456 struct saa7146_vv *vv = dev->vv_data;
459 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
460 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
464 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
466 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
467 struct saa7146_vv *vv = dev->vv_data;
468 struct saa7146_format *fmt;
470 DEB_EE("VIDIOC_S_FBUF\n");
472 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
476 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
480 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
481 if (fmt->flags & FORMAT_IS_PLANAR)
482 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
483 (char *)&fmt->pixelformat);
485 /* check if overlay is running */
486 if (IS_OVERLAY_ACTIVE(fh) != 0) {
487 if (vv->video_fh != fh) {
488 DEB_D("refusing to change framebuffer information while overlay is active in another open\n");
497 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
498 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
499 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
504 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
506 if (f->index >= ARRAY_SIZE(formats))
508 f->pixelformat = formats[f->index].pixelformat;
512 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
514 struct saa7146_dev *dev = container_of(ctrl->handler,
515 struct saa7146_dev, ctrl_handler);
516 struct saa7146_vv *vv = dev->vv_data;
520 case V4L2_CID_BRIGHTNESS:
521 val = saa7146_read(dev, BCS_CTRL);
523 val |= (ctrl->val << 24);
524 saa7146_write(dev, BCS_CTRL, val);
525 saa7146_write(dev, MC2, MASK_22 | MASK_06);
528 case V4L2_CID_CONTRAST:
529 val = saa7146_read(dev, BCS_CTRL);
531 val |= (ctrl->val << 16);
532 saa7146_write(dev, BCS_CTRL, val);
533 saa7146_write(dev, MC2, MASK_22 | MASK_06);
536 case V4L2_CID_SATURATION:
537 val = saa7146_read(dev, BCS_CTRL);
539 val |= (ctrl->val << 0);
540 saa7146_write(dev, BCS_CTRL, val);
541 saa7146_write(dev, MC2, MASK_22 | MASK_06);
545 /* fixme: we can support changing VFLIP and HFLIP here... */
546 if ((vv->video_status & STATUS_CAPTURE))
548 vv->hflip = ctrl->val;
552 if ((vv->video_status & STATUS_CAPTURE))
554 vv->vflip = ctrl->val;
561 if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
562 struct saa7146_fh *fh = vv->video_fh;
564 saa7146_stop_preview(fh);
565 saa7146_start_preview(fh);
570 static int vidioc_g_parm(struct file *file, void *fh,
571 struct v4l2_streamparm *parm)
573 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
574 struct saa7146_vv *vv = dev->vv_data;
576 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
578 parm->parm.capture.readbuffers = 1;
579 v4l2_video_std_frame_period(vv->standard->id,
580 &parm->parm.capture.timeperframe);
584 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
586 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
587 struct saa7146_vv *vv = dev->vv_data;
589 f->fmt.pix = vv->video_fmt;
593 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
595 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
596 struct saa7146_vv *vv = dev->vv_data;
598 f->fmt.win = vv->ov.win;
602 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
604 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
605 struct saa7146_vv *vv = dev->vv_data;
607 f->fmt.vbi = vv->vbi_fmt;
611 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
613 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
614 struct saa7146_vv *vv = dev->vv_data;
615 struct saa7146_format *fmt;
616 enum v4l2_field field;
620 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
622 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
626 field = f->fmt.pix.field;
627 maxw = vv->standard->h_max_out;
628 maxh = vv->standard->v_max_out;
630 if (V4L2_FIELD_ANY == field) {
631 field = (f->fmt.pix.height > maxh / 2)
632 ? V4L2_FIELD_INTERLACED
636 case V4L2_FIELD_ALTERNATE:
637 vv->last_field = V4L2_FIELD_TOP;
641 case V4L2_FIELD_BOTTOM:
642 vv->last_field = V4L2_FIELD_INTERLACED;
645 case V4L2_FIELD_INTERLACED:
646 vv->last_field = V4L2_FIELD_INTERLACED;
649 DEB_D("no known field mode '%d'\n", field);
653 f->fmt.pix.field = field;
654 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
655 if (f->fmt.pix.width > maxw)
656 f->fmt.pix.width = maxw;
657 if (f->fmt.pix.height > maxh)
658 f->fmt.pix.height = maxh;
660 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
662 if (f->fmt.pix.bytesperline < calc_bpl)
663 f->fmt.pix.bytesperline = calc_bpl;
665 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
666 f->fmt.pix.bytesperline = calc_bpl;
668 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
669 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
670 f->fmt.pix.width, f->fmt.pix.height,
671 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
677 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
679 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
680 struct saa7146_vv *vv = dev->vv_data;
681 struct v4l2_window *win = &f->fmt.win;
682 enum v4l2_field field;
685 DEB_EE("dev:%p\n", dev);
687 if (NULL == vv->ov_fb.base) {
688 DEB_D("no fb base set\n");
691 if (NULL == vv->ov_fmt) {
692 DEB_D("no fb fmt set\n");
695 if (win->w.width < 48 || win->w.height < 32) {
696 DEB_D("min width/height. (%d,%d)\n",
697 win->w.width, win->w.height);
700 if (win->clipcount > 16) {
701 DEB_D("clipcount too big\n");
706 maxw = vv->standard->h_max_out;
707 maxh = vv->standard->v_max_out;
709 if (V4L2_FIELD_ANY == field) {
710 field = (win->w.height > maxh / 2)
711 ? V4L2_FIELD_INTERLACED
716 case V4L2_FIELD_BOTTOM:
717 case V4L2_FIELD_ALTERNATE:
720 case V4L2_FIELD_INTERLACED:
723 DEB_D("no known field mode '%d'\n", field);
728 if (win->w.width > maxw)
730 if (win->w.height > maxh)
731 win->w.height = maxh;
736 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
738 struct saa7146_fh *fh = __fh;
739 struct saa7146_dev *dev = fh->dev;
740 struct saa7146_vv *vv = dev->vv_data;
743 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
744 if (IS_CAPTURE_ACTIVE(fh) != 0) {
745 DEB_EE("streaming capture is active\n");
748 err = vidioc_try_fmt_vid_cap(file, fh, f);
751 vv->video_fmt = f->fmt.pix;
752 DEB_EE("set to pixelformat '%4.4s'\n",
753 (char *)&vv->video_fmt.pixelformat);
757 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
759 struct saa7146_fh *fh = __fh;
760 struct saa7146_dev *dev = fh->dev;
761 struct saa7146_vv *vv = dev->vv_data;
764 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
765 err = vidioc_try_fmt_vid_overlay(file, fh, f);
768 vv->ov.win = f->fmt.win;
769 vv->ov.nclips = f->fmt.win.clipcount;
770 if (vv->ov.nclips > 16)
772 memcpy(vv->ov.clips, f->fmt.win.clips,
773 sizeof(struct v4l2_clip) * vv->ov.nclips);
775 /* vv->ov.fh is used to indicate that we have valid overlay information, too */
778 /* check if our current overlay is active */
779 if (IS_OVERLAY_ACTIVE(fh) != 0) {
780 saa7146_stop_preview(fh);
781 saa7146_start_preview(fh);
786 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
788 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
789 struct saa7146_vv *vv = dev->vv_data;
791 *norm = vv->standard->id;
795 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
796 PAL / NTSC / SECAM. if your hardware does not (or does more)
797 -- override this function in your extension */
801 struct v4l2_standard *e = arg;
804 if( e->index < dev->ext_vv_data->num_stds ) {
805 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
806 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
813 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
815 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
816 struct saa7146_vv *vv = dev->vv_data;
820 DEB_EE("VIDIOC_S_STD\n");
822 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
823 DEB_D("cannot change video standard while streaming capture is active\n");
827 if ((vv->video_status & STATUS_OVERLAY) != 0) {
828 vv->ov_suspend = vv->video_fh;
829 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
831 DEB_D("suspending video failed. aborting\n");
836 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
837 if (id & dev->ext_vv_data->stds[i].id)
839 if (i != dev->ext_vv_data->num_stds) {
840 vv->standard = &dev->ext_vv_data->stds[i];
841 if (NULL != dev->ext_vv_data->std_callback)
842 dev->ext_vv_data->std_callback(dev, vv->standard);
846 if (vv->ov_suspend != NULL) {
847 saa7146_start_preview(vv->ov_suspend);
848 vv->ov_suspend = NULL;
852 DEB_EE("VIDIOC_S_STD: standard not found\n");
856 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
860 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
864 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
866 err = saa7146_start_preview(fh);
868 err = saa7146_stop_preview(fh);
872 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
874 struct saa7146_fh *fh = __fh;
876 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
877 return videobuf_reqbufs(&fh->video_q, b);
878 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
879 return videobuf_reqbufs(&fh->vbi_q, b);
883 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
885 struct saa7146_fh *fh = __fh;
887 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
888 return videobuf_querybuf(&fh->video_q, buf);
889 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
890 return videobuf_querybuf(&fh->vbi_q, buf);
894 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
896 struct saa7146_fh *fh = __fh;
898 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
899 return videobuf_qbuf(&fh->video_q, buf);
900 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
901 return videobuf_qbuf(&fh->vbi_q, buf);
905 static int vidioc_dqbuf(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_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
911 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
912 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
916 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
918 struct saa7146_fh *fh = __fh;
921 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
923 err = video_begin(fh);
926 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
927 return videobuf_streamon(&fh->video_q);
928 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
929 return videobuf_streamon(&fh->vbi_q);
933 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
935 struct saa7146_fh *fh = __fh;
936 struct saa7146_dev *dev = fh->dev;
937 struct saa7146_vv *vv = dev->vv_data;
940 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
942 /* ugly: we need to copy some checks from video_end(),
943 because videobuf_streamoff() relies on the capture running.
944 check and fix this */
945 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
946 DEB_S("not capturing\n");
950 if (vv->video_fh != fh) {
951 DEB_S("capturing, but in another open\n");
956 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
957 err = videobuf_streamoff(&fh->video_q);
958 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
959 err = videobuf_streamoff(&fh->vbi_q);
961 DEB_D("warning: videobuf_streamoff() failed\n");
964 err = video_end(fh, file);
969 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
970 .vidioc_querycap = vidioc_querycap,
971 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
972 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
973 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
974 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
975 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
976 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
977 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
978 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
980 .vidioc_overlay = vidioc_overlay,
981 .vidioc_g_fbuf = vidioc_g_fbuf,
982 .vidioc_s_fbuf = vidioc_s_fbuf,
983 .vidioc_reqbufs = vidioc_reqbufs,
984 .vidioc_querybuf = vidioc_querybuf,
985 .vidioc_qbuf = vidioc_qbuf,
986 .vidioc_dqbuf = vidioc_dqbuf,
987 .vidioc_g_std = vidioc_g_std,
988 .vidioc_s_std = vidioc_s_std,
989 .vidioc_streamon = vidioc_streamon,
990 .vidioc_streamoff = vidioc_streamoff,
991 .vidioc_g_parm = vidioc_g_parm,
992 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
993 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
996 const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
997 .vidioc_querycap = vidioc_querycap,
998 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
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,
1013 /*********************************************************************************/
1014 /* buffer handling functions */
1016 static int buffer_activate (struct saa7146_dev *dev,
1017 struct saa7146_buf *buf,
1018 struct saa7146_buf *next)
1020 struct saa7146_vv *vv = dev->vv_data;
1022 buf->vb.state = VIDEOBUF_ACTIVE;
1023 saa7146_set_capture(dev,buf,next);
1025 mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
1029 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
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]);
1036 static int buffer_prepare(struct videobuf_queue *q,
1037 struct videobuf_buffer *vb, enum v4l2_field field)
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;
1046 DEB_CAP("vbuf:%p\n", vb);
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);
1058 size = vv->video_fmt.sizeimage;
1059 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1060 DEB_D("size mismatch\n");
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);
1077 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1078 struct saa7146_format *sfmt;
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;
1088 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
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]);
1096 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1099 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1102 err = saa7146_pgtable_build(dev,buf);
1106 buf->vb.state = VIDEOBUF_PREPARED;
1107 buf->activate = buffer_activate;
1112 DEB_D("error out\n");
1113 saa7146_dma_free(dev,q,buf);
1118 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1120 struct file *file = q->priv_data;
1121 struct saa7146_fh *fh = file->private_data;
1122 struct saa7146_vv *vv = fh->dev->vv_data;
1124 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1125 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1127 *size = vv->video_fmt.sizeimage;
1129 /* check if we exceed the "max_memory" parameter */
1130 if( (*count * *size) > (max_memory*1048576) ) {
1131 *count = (max_memory*1048576) / *size;
1134 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
1139 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
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;
1147 DEB_CAP("vbuf:%p\n", vb);
1148 saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
1151 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
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;
1158 DEB_CAP("vbuf:%p\n", vb);
1160 saa7146_dma_free(dev,q,buf);
1162 release_all_pagetables(dev, buf);
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,
1172 /********************************************************************************/
1173 /* file operations */
1175 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1177 INIT_LIST_HEAD(&vv->video_dmaq.queue);
1179 timer_setup(&vv->video_dmaq.timeout, saa7146_buffer_timeout, 0);
1180 vv->video_dmaq.dev = dev;
1182 /* set some default values */
1183 vv->standard = &dev->ext_vv_data->stds[0];
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;
1191 static int video_open(struct saa7146_dev *dev, struct file *file)
1193 struct saa7146_fh *fh = file->private_data;
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);
1206 static void video_close(struct saa7146_dev *dev, struct file *file)
1208 struct saa7146_fh *fh = file->private_data;
1209 struct saa7146_vv *vv = dev->vv_data;
1210 struct videobuf_queue *q = &fh->video_q;
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);
1218 /* hmm, why is this function declared void? */
1222 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1224 struct saa7146_vv *vv = dev->vv_data;
1225 struct saa7146_dmaqueue *q = &vv->video_dmaq;
1227 spin_lock(&dev->slock);
1228 DEB_CAP("called\n");
1230 /* only finish the buffer if we have one... */
1231 if( NULL != q->curr ) {
1232 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1234 saa7146_buffer_next(dev,q,0);
1236 spin_unlock(&dev->slock);
1239 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1241 struct saa7146_fh *fh = file->private_data;
1242 struct saa7146_dev *dev = fh->dev;
1243 struct saa7146_vv *vv = dev->vv_data;
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");
1254 DEB_S("already capturing in another open\n");
1258 ret = video_begin(fh);
1263 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1264 file->f_flags & O_NONBLOCK);
1266 video_end(fh, file);
1268 ret = video_end(fh, file);
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;
1280 const struct saa7146_use_ops saa7146_video_uops = {
1283 .release = video_close,
1284 .irq_done = video_irq_done,