1 #include <media/saa7146_vv.h>
3 static int memory = 32;
5 MODULE_PARM(memory,"i");
6 MODULE_PARM_DESC(memory, "maximum memory usage for capture buffers (default: 32Mb)");
8 #define IS_CAPTURE_ACTIVE(fh) \
9 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
11 #define IS_OVERLAY_ACTIVE(fh) \
12 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
14 /* format descriptions for capture and preview */
15 static struct saa7146_format formats[] = {
17 .name = "RGB-8 (3-3-2)",
18 .pixelformat = V4L2_PIX_FMT_RGB332,
19 .trans = RGB08_COMPOSED,
23 .name = "RGB-16 (5/B-6/G-5/R)",
24 .pixelformat = V4L2_PIX_FMT_RGB565,
25 .trans = RGB16_COMPOSED,
29 .name = "RGB-24 (B-G-R)",
30 .pixelformat = V4L2_PIX_FMT_BGR24,
31 .trans = RGB24_COMPOSED,
35 .name = "RGB-32 (B-G-R)",
36 .pixelformat = V4L2_PIX_FMT_BGR32,
37 .trans = RGB32_COMPOSED,
41 .name = "Greyscale-8",
42 .pixelformat = V4L2_PIX_FMT_GREY,
47 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
48 .pixelformat = V4L2_PIX_FMT_YUV422P,
49 .trans = YUV422_DECOMPOSED,
51 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
53 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
54 .pixelformat = V4L2_PIX_FMT_YVU420,
55 .trans = YUV420_DECOMPOSED,
57 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
59 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
60 .pixelformat = V4L2_PIX_FMT_YUV420,
61 .trans = YUV420_DECOMPOSED,
63 .flags = FORMAT_IS_PLANAR,
65 .name = "YUV 4:2:2 (U-Y-V-Y)",
66 .pixelformat = V4L2_PIX_FMT_UYVY,
67 .trans = YUV422_COMPOSED,
73 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
74 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
75 (like V4L2_PIX_FMT_YUYV) ... 8-( */
77 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
79 struct saa7146_format* format_by_fourcc(struct saa7146_dev *dev, int fourcc)
81 int i, j = NUM_FORMATS;
83 for (i = 0; i < j; i++) {
84 if (formats[i].pixelformat == fourcc) {
89 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
93 static int g_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
95 struct saa7146_dev *dev = fh->dev;
96 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
99 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
100 f->fmt.pix = fh->video_fmt;
102 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
103 f->fmt.win = fh->ov.win;
105 case V4L2_BUF_TYPE_VBI_CAPTURE:
107 f->fmt.vbi = fh->vbi_fmt;
111 DEB_D(("invalid format type '%d'.\n",f->type));
116 static int try_win(struct saa7146_dev *dev, struct v4l2_window *win)
118 struct saa7146_vv *vv = dev->vv_data;
119 enum v4l2_field field;
122 DEB_EE(("dev:%p\n",dev));
124 if (NULL == vv->ov_fb.base) {
125 DEB_D(("no fb base set.\n"));
128 if (NULL == vv->ov_fmt) {
129 DEB_D(("no fb fmt set.\n"));
132 if (win->w.width < 48 || win->w.height < 32) {
133 DEB_D(("min width/height. (%d,%d)\n",win->w.width,win->w.height));
136 if (win->clipcount > 16) {
137 DEB_D(("clipcount too big.\n"));
142 maxw = vv->standard->h_max_out;
143 maxh = vv->standard->v_max_out;
145 if (V4L2_FIELD_ANY == field) {
146 field = (win->w.height > maxh/2)
147 ? V4L2_FIELD_INTERLACED
152 case V4L2_FIELD_BOTTOM:
153 case V4L2_FIELD_ALTERNATE:
156 case V4L2_FIELD_INTERLACED:
159 DEB_D(("no known field mode '%d'.\n",field));
165 if (win->w.width > maxw)
167 if (win->w.height > maxh)
168 win->w.height = maxh;
173 static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
175 struct saa7146_dev *dev = fh->dev;
176 struct saa7146_vv *vv = dev->vv_data;
180 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
182 struct saa7146_format *fmt;
183 enum v4l2_field field;
187 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
189 fmt = format_by_fourcc(dev,f->fmt.pix.pixelformat);
194 field = f->fmt.pix.field;
195 maxw = vv->standard->h_max_out;
196 maxh = vv->standard->v_max_out;
198 if (V4L2_FIELD_ANY == field) {
199 field = (f->fmt.pix.height > maxh/2)
200 ? V4L2_FIELD_INTERLACED
204 case V4L2_FIELD_ALTERNATE: {
205 vv->last_field = V4L2_FIELD_TOP;
210 case V4L2_FIELD_BOTTOM:
211 vv->last_field = V4L2_FIELD_INTERLACED;
214 case V4L2_FIELD_INTERLACED:
215 vv->last_field = V4L2_FIELD_INTERLACED;
218 DEB_D(("no known field mode '%d'.\n",field));
223 f->fmt.pix.field = field;
224 if (f->fmt.pix.width > maxw)
225 f->fmt.pix.width = maxw;
226 if (f->fmt.pix.height > maxh)
227 f->fmt.pix.height = maxh;
229 calc_bpl = (f->fmt.pix.width * fmt->depth)/8;
231 if (f->fmt.pix.bytesperline < calc_bpl)
232 f->fmt.pix.bytesperline = calc_bpl;
234 if (f->fmt.pix.bytesperline > (2*PAGE_SIZE * fmt->depth)/8) /* arbitrary constraint */
235 f->fmt.pix.bytesperline = calc_bpl;
237 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
238 DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",f->fmt.pix.width,f->fmt.pix.height,f->fmt.pix.bytesperline,f->fmt.pix.sizeimage));
242 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
243 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
244 err = try_win(dev,&f->fmt.win);
250 DEB_EE(("unknown format type '%d'\n",f->type));
255 int saa7146_start_preview(struct saa7146_fh *fh)
257 struct saa7146_dev *dev = fh->dev;
258 struct saa7146_vv *vv = dev->vv_data;
259 int ret = 0, err = 0;
261 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
263 /* check if we have overlay informations */
264 if( NULL == fh->ov.fh ) {
265 DEB_D(("no overlay data available. try S_FMT first.\n"));
269 /* check if streaming capture is running */
270 if (IS_CAPTURE_ACTIVE(fh) != 0) {
271 DEB_D(("streaming capture is active.\n"));
275 /* check if overlay is running */
276 if (IS_OVERLAY_ACTIVE(fh) != 0) {
277 if (vv->video_fh == fh) {
278 DEB_D(("overlay is already active.\n"));
281 DEB_D(("overlay is already active in another open.\n"));
285 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
286 DEB_D(("cannot get necessary overlay resources\n"));
290 err = try_win(dev,&fh->ov.win);
292 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
296 vv->ov_data = &fh->ov;
298 DEB_D(("%dx%d+%d+%d %s field=%s\n",
299 fh->ov.win.w.width,fh->ov.win.w.height,
300 fh->ov.win.w.left,fh->ov.win.w.top,
301 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
303 if (0 != (ret = saa7146_enable_overlay(fh))) {
304 DEB_D(("enabling overlay failed: %d\n",ret));
305 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
309 vv->video_status = STATUS_OVERLAY;
315 int saa7146_stop_preview(struct saa7146_fh *fh)
317 struct saa7146_dev *dev = fh->dev;
318 struct saa7146_vv *vv = dev->vv_data;
320 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
322 /* check if streaming capture is running */
323 if (IS_CAPTURE_ACTIVE(fh) != 0) {
324 DEB_D(("streaming capture is active.\n"));
328 /* check if overlay is running at all */
329 if ((vv->video_status & STATUS_OVERLAY) == 0) {
330 DEB_D(("no active overlay.\n"));
334 if (vv->video_fh != fh) {
335 DEB_D(("overlay is active, but in another open.\n"));
339 vv->video_status = 0;
342 saa7146_disable_overlay(fh);
344 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
349 static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
351 struct saa7146_dev *dev = fh->dev;
352 struct saa7146_vv *vv = dev->vv_data;
357 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
358 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
359 if (IS_CAPTURE_ACTIVE(fh) != 0) {
360 DEB_EE(("streaming capture is active\n"));
366 fh->video_fmt = f->fmt.pix;
367 DEB_EE(("set to pixelformat '%4.4s'\n",(char *)&fh->video_fmt.pixelformat));
369 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
370 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
371 err = try_win(dev,&f->fmt.win);
375 fh->ov.win = f->fmt.win;
376 fh->ov.nclips = f->fmt.win.clipcount;
377 if (fh->ov.nclips > 16)
379 if (copy_from_user(fh->ov.clips,f->fmt.win.clips,sizeof(struct v4l2_clip)*fh->ov.nclips)) {
384 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
389 /* check if our current overlay is active */
390 if (IS_OVERLAY_ACTIVE(fh) != 0) {
391 saa7146_stop_preview(fh);
392 saa7146_start_preview(fh);
396 DEB_D(("unknown format type '%d'\n",f->type));
401 /********************************************************************************/
402 /* device controls */
404 static struct v4l2_queryctrl controls[] = {
406 .id = V4L2_CID_BRIGHTNESS,
407 .name = "Brightness",
411 .default_value = 128,
412 .type = V4L2_CTRL_TYPE_INTEGER,
414 .id = V4L2_CID_CONTRAST,
420 .type = V4L2_CTRL_TYPE_INTEGER,
422 .id = V4L2_CID_SATURATION,
423 .name = "Saturation",
428 .type = V4L2_CTRL_TYPE_INTEGER,
430 .id = V4L2_CID_VFLIP,
431 .name = "Vertical flip",
434 .type = V4L2_CTRL_TYPE_BOOLEAN,
436 .id = V4L2_CID_HFLIP,
437 .name = "Horizontal flip",
440 .type = V4L2_CTRL_TYPE_BOOLEAN,
443 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
445 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
447 static struct v4l2_queryctrl* ctrl_by_id(int id)
451 for (i = 0; i < NUM_CONTROLS; i++)
452 if (controls[i].id == id)
457 static int get_control(struct saa7146_fh *fh, struct v4l2_control *c)
459 struct saa7146_dev *dev = fh->dev;
460 struct saa7146_vv *vv = dev->vv_data;
462 const struct v4l2_queryctrl* ctrl;
465 ctrl = ctrl_by_id(c->id);
469 case V4L2_CID_BRIGHTNESS:
470 value = saa7146_read(dev, BCS_CTRL);
471 c->value = 0xff & (value >> 24);
472 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n",c->value));
474 case V4L2_CID_CONTRAST:
475 value = saa7146_read(dev, BCS_CTRL);
476 c->value = 0x7f & (value >> 16);
477 DEB_D(("V4L2_CID_CONTRAST: %d\n",c->value));
479 case V4L2_CID_SATURATION:
480 value = saa7146_read(dev, BCS_CTRL);
481 c->value = 0x7f & (value >> 0);
482 DEB_D(("V4L2_CID_SATURATION: %d\n",c->value));
485 c->value = vv->vflip;
486 DEB_D(("V4L2_CID_VFLIP: %d\n",c->value));
489 c->value = vv->hflip;
490 DEB_D(("V4L2_CID_HFLIP: %d\n",c->value));
499 static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
501 struct saa7146_dev *dev = fh->dev;
502 struct saa7146_vv *vv = dev->vv_data;
504 const struct v4l2_queryctrl* ctrl;
506 ctrl = ctrl_by_id(c->id);
508 DEB_D(("unknown control %d\n",c->id));
514 switch (ctrl->type) {
515 case V4L2_CTRL_TYPE_BOOLEAN:
516 case V4L2_CTRL_TYPE_MENU:
517 case V4L2_CTRL_TYPE_INTEGER:
518 if (c->value < ctrl->minimum)
519 c->value = ctrl->minimum;
520 if (c->value > ctrl->maximum)
521 c->value = ctrl->maximum;
528 case V4L2_CID_BRIGHTNESS: {
529 u32 value = saa7146_read(dev, BCS_CTRL);
531 value |= (c->value << 24);
532 saa7146_write(dev, BCS_CTRL, value);
533 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
536 case V4L2_CID_CONTRAST: {
537 u32 value = saa7146_read(dev, BCS_CTRL);
539 value |= (c->value << 16);
540 saa7146_write(dev, BCS_CTRL, value);
541 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
544 case V4L2_CID_SATURATION: {
545 u32 value = saa7146_read(dev, BCS_CTRL);
547 value |= (c->value << 0);
548 saa7146_write(dev, BCS_CTRL, value);
549 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
553 /* fixme: we can support changing VFLIP and HFLIP here... */
554 if (IS_CAPTURE_ACTIVE(fh) != 0) {
555 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
559 vv->hflip = c->value;
562 if (IS_CAPTURE_ACTIVE(fh) != 0) {
563 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
567 vv->vflip = c->value;
575 if (IS_OVERLAY_ACTIVE(fh) != 0) {
576 saa7146_stop_preview(fh);
577 saa7146_start_preview(fh);
582 /********************************************************************************/
583 /* common pagetable functions */
585 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
587 struct pci_dev *pci = dev->pci;
588 struct scatterlist *list = buf->vb.dma.sglist;
589 int length = buf->vb.dma.sglen;
590 struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
592 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
594 if( 0 != IS_PLANAR(sfmt->trans)) {
595 struct saa7146_pgtable *pt1 = &buf->pt[0];
596 struct saa7146_pgtable *pt2 = &buf->pt[1];
597 struct saa7146_pgtable *pt3 = &buf->pt[2];
598 u32 *ptr1, *ptr2, *ptr3;
601 int size = buf->fmt->width*buf->fmt->height;
602 int i,p,m1,m2,m3,o1,o2;
604 switch( sfmt->depth ) {
606 /* create some offsets inside the page table */
607 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
608 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
609 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
611 o2 = (size+(size/4))%PAGE_SIZE;
612 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
616 /* create some offsets inside the page table */
617 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
618 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
619 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
621 o2 = (size+(size/2))%PAGE_SIZE;
622 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
634 /* walk all pages, copy all page addresses to ptr1 */
635 for (i = 0; i < length; i++, list++) {
636 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
637 *ptr1 = sg_dma_address(list) - list->offset;
643 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
647 /* if we have a user buffer, the first page may not be
648 aligned to a page boundary. */
649 pt1->offset = buf->vb.dma.sglist->offset;
650 pt2->offset = pt1->offset+o1;
651 pt3->offset = pt1->offset+o2;
653 /* create video-dma2 page table */
655 for(i = m1; i <= m2 ; i++, ptr2++) {
659 for(;i<1024;i++,ptr2++) {
662 /* create video-dma3 page table */
664 for(i = m2; i <= m3; i++,ptr3++) {
668 for(;i<1024;i++,ptr3++) {
671 /* finally: finish up video-dma1 page table */
674 for(i=m1;i<1024;i++,ptr1++) {
682 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
685 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
688 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
692 struct saa7146_pgtable *pt = &buf->pt[0];
693 return saa7146_pgtable_build_single(pci, pt, list, length);
700 /********************************************************************************/
701 /* file operations */
703 static int video_begin(struct saa7146_fh *fh)
705 struct saa7146_dev *dev = fh->dev;
706 struct saa7146_vv *vv = dev->vv_data;
707 struct saa7146_format *fmt = NULL;
708 unsigned int resource;
709 int ret = 0, err = 0;
711 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
713 if ((vv->video_status & STATUS_CAPTURE) != 0) {
714 if (vv->video_fh == fh) {
715 DEB_S(("already capturing.\n"));
718 DEB_S(("already capturing in another open.\n"));
722 if ((vv->video_status & STATUS_OVERLAY) != 0) {
723 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
724 vv->ov_suspend = vv->video_fh;
725 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
727 DEB_D(("suspending video failed. aborting\n"));
732 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
733 /* we need to have a valid format set here */
736 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
737 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
739 resource = RESOURCE_DMA1_HPS;
742 ret = saa7146_res_get(fh, resource);
744 DEB_S(("cannot get capture resource %d\n",resource));
745 if (vv->ov_suspend != NULL) {
746 saa7146_start_preview(vv->ov_suspend);
747 vv->ov_suspend = NULL;
752 /* clear out beginning of streaming bit (rps register 0)*/
753 saa7146_write(dev, MC2, MASK_27 );
755 /* enable rps0 irqs */
756 IER_ENABLE(dev, MASK_27);
759 vv->video_status = STATUS_CAPTURE;
764 static int video_end(struct saa7146_fh *fh, struct file *file)
766 struct saa7146_dev *dev = fh->dev;
767 struct saa7146_vv *vv = dev->vv_data;
768 struct saa7146_format *fmt = NULL;
770 unsigned int resource;
772 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
774 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
775 DEB_S(("not capturing.\n"));
779 if (vv->video_fh != fh) {
780 DEB_S(("capturing, but in another open.\n"));
784 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
785 /* we need to have a valid format set here */
788 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
789 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
790 dmas = MASK_22 | MASK_21 | MASK_20;
792 resource = RESOURCE_DMA1_HPS;
795 spin_lock_irqsave(&dev->slock,flags);
798 saa7146_write(dev, MC1, MASK_28);
800 /* disable rps0 irqs */
801 IER_DISABLE(dev, MASK_27);
803 /* shut down all used video dma transfers */
804 saa7146_write(dev, MC1, dmas);
806 spin_unlock_irqrestore(&dev->slock, flags);
809 vv->video_status = 0;
811 saa7146_res_free(fh, resource);
813 if (vv->ov_suspend != NULL) {
814 saa7146_start_preview(vv->ov_suspend);
815 vv->ov_suspend = NULL;
822 * This function is _not_ called directly, but from
823 * video_generic_ioctl (and maybe others). userspace
824 * copying is done already, arg is a kernel pointer.
827 int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
829 struct saa7146_fh *fh = file->private_data;
830 struct saa7146_dev *dev = fh->dev;
831 struct saa7146_vv *vv = dev->vv_data;
833 int err = 0, result = 0, ee = 0;
835 struct saa7146_use_ops *ops;
836 struct videobuf_queue *q;
838 /* check if extension handles the command */
839 for(ee = 0; dev->ext_vv_data->ioctls[ee].flags != 0; ee++) {
840 if( cmd == dev->ext_vv_data->ioctls[ee].cmd )
844 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_EXCLUSIVE) ) {
845 DEB_D(("extension handles ioctl exclusive.\n"));
846 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
849 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_BEFORE) ) {
850 DEB_D(("extension handles ioctl before.\n"));
851 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
852 if( -EAGAIN != result ) {
857 /* fixme: add handle "after" case (is it still needed?) */
860 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
861 ops = &saa7146_video_uops;
865 case V4L2_BUF_TYPE_VBI_CAPTURE: {
866 ops = &saa7146_vbi_uops;
876 case VIDIOC_QUERYCAP:
878 struct v4l2_capability *cap = arg;
879 memset(cap,0,sizeof(*cap));
881 DEB_EE(("VIDIOC_QUERYCAP\n"));
883 strcpy(cap->driver, "saa7146 v4l2");
884 strlcpy(cap->card, dev->ext->name, sizeof(cap->card));
885 sprintf(cap->bus_info,"PCI:%s",dev->pci->slot_name);
886 cap->version = SAA7146_VERSION_CODE;
888 V4L2_CAP_VIDEO_CAPTURE |
889 V4L2_CAP_VIDEO_OVERLAY |
892 cap->capabilities |= dev->ext_vv_data->capabilities;
897 struct v4l2_framebuffer *fb = arg;
899 DEB_EE(("VIDIOC_G_FBUF\n"));
902 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
907 struct v4l2_framebuffer *fb = arg;
908 struct saa7146_format *fmt;
910 DEB_EE(("VIDIOC_S_FBUF\n"));
912 if(!capable(CAP_SYS_ADMIN) &&
913 !capable(CAP_SYS_RAWIO))
917 fmt = format_by_fourcc(dev,fb->fmt.pixelformat);
922 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
923 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
924 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",(char *)&fmt->pixelformat));
927 /* check if overlay is running */
928 if (IS_OVERLAY_ACTIVE(fh) != 0) {
929 if (vv->video_fh != fh) {
930 DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
940 if (0 == vv->ov_fb.fmt.bytesperline)
941 vv->ov_fb.fmt.bytesperline =
942 vv->ov_fb.fmt.width*fmt->depth/8;
948 case VIDIOC_ENUM_FMT:
950 struct v4l2_fmtdesc *f = arg;
954 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
955 case V4L2_BUF_TYPE_VIDEO_OVERLAY: {
957 if (index < 0 || index >= NUM_FORMATS) {
960 memset(f,0,sizeof(*f));
962 strlcpy(f->description,formats[index].name,sizeof(f->description));
963 f->pixelformat = formats[index].pixelformat;
970 DEB_EE(("VIDIOC_ENUM_FMT: type:%d, index:%d\n",f->type,f->index));
973 case VIDIOC_QUERYCTRL:
975 const struct v4l2_queryctrl *ctrl;
976 struct v4l2_queryctrl *c = arg;
978 if ((c->id < V4L2_CID_BASE ||
979 c->id >= V4L2_CID_LASTP1) &&
980 (c->id < V4L2_CID_PRIVATE_BASE ||
981 c->id >= V4L2_CID_PRIVATE_LASTP1))
984 ctrl = ctrl_by_id(c->id);
988 c->flags = V4L2_CTRL_FLAG_DISABLED;
993 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n",c->id));
997 case VIDIOC_G_CTRL: {
998 DEB_EE(("VIDIOC_G_CTRL\n"));
999 return get_control(fh,arg);
1007 DEB_EE(("VIDIOC_S_CTRL\n"));
1008 err = set_control(fh,arg);
1013 struct v4l2_streamparm *parm = arg;
1014 if( parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ) {
1017 memset(&parm->parm.capture,0,sizeof(struct v4l2_captureparm));
1018 parm->parm.capture.readbuffers = 1;
1019 // fixme: only for PAL!
1020 parm->parm.capture.timeperframe.numerator = 1;
1021 parm->parm.capture.timeperframe.denominator = 25;
1026 struct v4l2_format *f = arg;
1027 DEB_EE(("VIDIOC_G_FMT\n"));
1032 struct v4l2_format *f = arg;
1033 DEB_EE(("VIDIOC_S_FMT\n"));
1036 case VIDIOC_TRY_FMT:
1038 struct v4l2_format *f = arg;
1039 DEB_EE(("VIDIOC_TRY_FMT\n"));
1040 return try_fmt(fh,f);
1044 v4l2_std_id *id = arg;
1045 DEB_EE(("VIDIOC_G_STD\n"));
1046 *id = vv->standard->id;
1049 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
1050 PAL / NTSC / SECAM. if your hardware does not (or does more)
1051 -- override this function in your extension */
1052 case VIDIOC_ENUMSTD:
1054 struct v4l2_standard *e = arg;
1057 if( e->index < dev->ext_vv_data->num_stds ) {
1058 DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
1059 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
1066 v4l2_std_id *id = arg;
1070 DEB_EE(("VIDIOC_S_STD\n"));
1072 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
1073 DEB_D(("cannot change video standard while streaming capture is active\n"));
1077 if ((vv->video_status & STATUS_OVERLAY) != 0) {
1078 vv->ov_suspend = vv->video_fh;
1079 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
1081 DEB_D(("suspending video failed. aborting\n"));
1088 for(i = 0; i < dev->ext_vv_data->num_stds; i++)
1089 if (*id & dev->ext_vv_data->stds[i].id)
1091 if (i != dev->ext_vv_data->num_stds) {
1092 vv->standard = &dev->ext_vv_data->stds[i];
1093 if( NULL != dev->ext_vv_data->std_callback )
1094 dev->ext_vv_data->std_callback(dev, vv->standard);
1100 if (vv->ov_suspend != NULL) {
1101 saa7146_start_preview(vv->ov_suspend);
1102 vv->ov_suspend = NULL;
1106 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
1110 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n",vv->standard->name));
1113 case VIDIOC_OVERLAY:
1119 int on = *(int *)arg;
1122 DEB_D(("VIDIOC_OVERLAY on:%d\n",on));
1124 err = saa7146_start_preview(fh);
1126 err = saa7146_stop_preview(fh);
1130 case VIDIOC_REQBUFS: {
1131 struct v4l2_requestbuffers *req = arg;
1132 DEB_D(("VIDIOC_REQBUFS, type:%d\n",req->type));
1133 return videobuf_reqbufs(file,q,req);
1135 case VIDIOC_QUERYBUF: {
1136 struct v4l2_buffer *buf = arg;
1137 DEB_D(("VIDIOC_QUERYBUF, type:%d, offset:%d\n",buf->type,buf->m.offset));
1138 return videobuf_querybuf(q,buf);
1141 struct v4l2_buffer *buf = arg;
1143 ret = videobuf_qbuf(file,q,buf);
1144 DEB_D(("VIDIOC_QBUF: ret:%d, index:%d\n",ret,buf->index));
1147 case VIDIOC_DQBUF: {
1148 struct v4l2_buffer *buf = arg;
1150 ret = videobuf_dqbuf(file,q,buf);
1151 DEB_D(("VIDIOC_DQBUF: ret:%d, index:%d\n",ret,buf->index));
1154 case VIDIOC_STREAMON: {
1156 DEB_D(("VIDIOC_STREAMON, type:%d\n",*type));
1158 err = video_begin(fh);
1162 err = videobuf_streamon(file,q);
1165 case VIDIOC_STREAMOFF: {
1168 DEB_D(("VIDIOC_STREAMOFF, type:%d\n",*type));
1170 /* ugly: we need to copy some checks from video_end(),
1171 because videobuf_streamoff() relies on the capture running.
1172 check and fix this */
1173 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
1174 DEB_S(("not capturing.\n"));
1178 if (vv->video_fh != fh) {
1179 DEB_S(("capturing, but in another open.\n"));
1183 err = videobuf_streamoff(file,q);
1185 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1186 video_end(fh, file);
1188 err = video_end(fh, file);
1194 struct video_mbuf *mbuf = arg;
1195 struct videobuf_queue *q;
1198 /* fixme: number of capture buffers and sizes for v4l apps */
1200 int gbufsize = 768*576*4;
1202 DEB_D(("VIDIOCGMBUF \n"));
1206 err = videobuf_mmap_setup(file,q,gbuffers,gbufsize,
1212 memset(mbuf,0,sizeof(*mbuf));
1213 mbuf->frames = gbuffers;
1214 mbuf->size = gbuffers * gbufsize;
1215 for (i = 0; i < gbuffers; i++)
1216 mbuf->offsets[i] = i * gbufsize;
1221 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1222 saa7146_video_do_ioctl);
1227 /*********************************************************************************/
1228 /* buffer handling functions */
1230 static int buffer_activate (struct saa7146_dev *dev,
1231 struct saa7146_buf *buf,
1232 struct saa7146_buf *next)
1234 struct saa7146_vv *vv = dev->vv_data;
1236 buf->vb.state = STATE_ACTIVE;
1237 saa7146_set_capture(dev,buf,next);
1239 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1243 static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4l2_field field)
1245 struct saa7146_fh *fh = file->private_data;
1246 struct saa7146_dev *dev = fh->dev;
1247 struct saa7146_vv *vv = dev->vv_data;
1248 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1251 DEB_CAP(("vbuf:%p\n",vb));
1254 if (fh->video_fmt.width < 48 ||
1255 fh->video_fmt.height < 32 ||
1256 fh->video_fmt.width > vv->standard->h_max_out ||
1257 fh->video_fmt.height > vv->standard->v_max_out) {
1258 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
1262 size = fh->video_fmt.sizeimage;
1263 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1264 DEB_D(("size mismatch.\n"));
1268 DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1269 fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
1270 if (buf->vb.width != fh->video_fmt.width ||
1271 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1272 buf->vb.height != fh->video_fmt.height ||
1273 buf->vb.size != size ||
1274 buf->vb.field != field ||
1275 buf->vb.field != fh->video_fmt.field ||
1276 buf->fmt != &fh->video_fmt) {
1277 saa7146_dma_free(dev,buf);
1280 if (STATE_NEEDS_INIT == buf->vb.state) {
1281 struct saa7146_format *sfmt;
1283 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1284 buf->vb.width = fh->video_fmt.width;
1285 buf->vb.height = fh->video_fmt.height;
1286 buf->vb.size = size;
1287 buf->vb.field = field;
1288 buf->fmt = &fh->video_fmt;
1289 buf->vb.field = fh->video_fmt.field;
1291 sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
1293 if( 0 != IS_PLANAR(sfmt->trans)) {
1294 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1295 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1296 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1298 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1299 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1300 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1302 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1303 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1306 err = videobuf_iolock(dev->pci,&buf->vb, &vv->ov_fb);
1309 err = saa7146_pgtable_build(dev,buf);
1313 buf->vb.state = STATE_PREPARED;
1314 buf->activate = buffer_activate;
1319 DEB_D(("error out.\n"));
1320 saa7146_dma_free(dev,buf);
1325 static int buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
1327 struct saa7146_fh *fh = file->private_data;
1329 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1330 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1332 *size = fh->video_fmt.sizeimage;
1334 /* check if we exceed the "memory" parameter */
1335 if( (*count * *size) > (memory*1048576) ) {
1336 *count = (memory*1048576) / *size;
1339 DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
1344 static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
1346 struct saa7146_fh *fh = file->private_data;
1347 struct saa7146_dev *dev = fh->dev;
1348 struct saa7146_vv *vv = dev->vv_data;
1349 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1351 DEB_CAP(("vbuf:%p\n",vb));
1352 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1356 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
1358 struct saa7146_fh *fh = file->private_data;
1359 struct saa7146_dev *dev = fh->dev;
1360 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1362 DEB_CAP(("vbuf:%p\n",vb));
1363 saa7146_dma_free(dev,buf);
1366 static struct videobuf_queue_ops video_qops = {
1367 .buf_setup = buffer_setup,
1368 .buf_prepare = buffer_prepare,
1369 .buf_queue = buffer_queue,
1370 .buf_release = buffer_release,
1373 /********************************************************************************/
1374 /* file operations */
1376 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1378 INIT_LIST_HEAD(&vv->video_q.queue);
1380 init_timer(&vv->video_q.timeout);
1381 vv->video_q.timeout.function = saa7146_buffer_timeout;
1382 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1383 vv->video_q.dev = dev;
1385 /* set some default values */
1386 vv->standard = &dev->ext_vv_data->stds[0];
1388 /* FIXME: what's this? */
1389 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1390 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1394 static int video_open(struct saa7146_dev *dev, struct file *file)
1396 struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1397 struct saa7146_format *sfmt;
1399 fh->video_fmt.width = 384;
1400 fh->video_fmt.height = 288;
1401 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1402 fh->video_fmt.bytesperline = 0;
1403 fh->video_fmt.field = V4L2_FIELD_ANY;
1404 sfmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
1405 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1407 videobuf_queue_init(&fh->video_q, &video_qops,
1408 dev->pci, &dev->slock,
1409 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1410 V4L2_FIELD_INTERLACED,
1411 sizeof(struct saa7146_buf));
1413 init_MUTEX(&fh->video_q.lock);
1419 static void video_close(struct saa7146_dev *dev, struct file *file)
1421 struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1422 struct saa7146_vv *vv = dev->vv_data;
1425 if (IS_CAPTURE_ACTIVE(fh) != 0) {
1426 err = video_end(fh, file);
1427 } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
1428 err = saa7146_stop_preview(fh);
1431 /* hmm, why is this function declared void? */
1436 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1438 struct saa7146_vv *vv = dev->vv_data;
1439 struct saa7146_dmaqueue *q = &vv->video_q;
1441 spin_lock(&dev->slock);
1442 DEB_CAP(("called.\n"));
1444 /* only finish the buffer if we have one... */
1445 if( NULL != q->curr ) {
1446 saa7146_buffer_finish(dev,q,STATE_DONE);
1448 saa7146_buffer_next(dev,q,0);
1450 spin_unlock(&dev->slock);
1453 static ssize_t video_read(struct file *file, char *data, size_t count, loff_t *ppos)
1455 struct saa7146_fh *fh = file->private_data;
1456 struct saa7146_dev *dev = fh->dev;
1457 struct saa7146_vv *vv = dev->vv_data;
1460 DEB_EE(("called.\n"));
1462 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1463 /* fixme: should we allow read() captures while streaming capture? */
1464 if (vv->video_fh == fh) {
1465 DEB_S(("already capturing.\n"));
1468 DEB_S(("already capturing in another open.\n"));
1472 ret = video_begin(fh);
1477 ret = videobuf_read_one(file,&fh->video_q , data, count, ppos);
1479 video_end(fh, file);
1481 ret = video_end(fh, file);
1484 /* restart overlay if it was active before */
1485 if (vv->ov_suspend != NULL) {
1486 saa7146_start_preview(vv->ov_suspend);
1487 vv->ov_suspend = NULL;
1493 struct saa7146_use_ops saa7146_video_uops = {
1496 .release = video_close,
1497 .irq_done = video_irq_done,