1 #include <media/saa7146_vv.h>
3 static int max_memory = 32;
5 module_param(max_memory, int, 0644);
6 MODULE_PARM_DESC(max_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 = "RGB-32 (R-G-B)",
42 .pixelformat = V4L2_PIX_FMT_RGB32,
43 .trans = RGB32_COMPOSED,
48 .name = "Greyscale-8",
49 .pixelformat = V4L2_PIX_FMT_GREY,
54 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
55 .pixelformat = V4L2_PIX_FMT_YUV422P,
56 .trans = YUV422_DECOMPOSED,
58 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
60 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
61 .pixelformat = V4L2_PIX_FMT_YVU420,
62 .trans = YUV420_DECOMPOSED,
64 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
66 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
67 .pixelformat = V4L2_PIX_FMT_YUV420,
68 .trans = YUV420_DECOMPOSED,
70 .flags = FORMAT_IS_PLANAR,
72 .name = "YUV 4:2:2 (U-Y-V-Y)",
73 .pixelformat = V4L2_PIX_FMT_UYVY,
74 .trans = YUV422_COMPOSED,
80 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
81 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
82 (like V4L2_PIX_FMT_YUYV) ... 8-( */
84 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
86 struct saa7146_format* format_by_fourcc(struct saa7146_dev *dev, int fourcc)
88 int i, j = NUM_FORMATS;
90 for (i = 0; i < j; i++) {
91 if (formats[i].pixelformat == fourcc) {
96 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
100 static int g_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
102 struct saa7146_dev *dev = fh->dev;
103 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
106 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
107 f->fmt.pix = fh->video_fmt;
109 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
110 f->fmt.win = fh->ov.win;
112 case V4L2_BUF_TYPE_VBI_CAPTURE:
114 f->fmt.vbi = fh->vbi_fmt;
118 DEB_D(("invalid format type '%d'.\n",f->type));
123 static int try_win(struct saa7146_dev *dev, struct v4l2_window *win)
125 struct saa7146_vv *vv = dev->vv_data;
126 enum v4l2_field field;
129 DEB_EE(("dev:%p\n",dev));
131 if (NULL == vv->ov_fb.base) {
132 DEB_D(("no fb base set.\n"));
135 if (NULL == vv->ov_fmt) {
136 DEB_D(("no fb fmt set.\n"));
139 if (win->w.width < 48 || win->w.height < 32) {
140 DEB_D(("min width/height. (%d,%d)\n",win->w.width,win->w.height));
143 if (win->clipcount > 16) {
144 DEB_D(("clipcount too big.\n"));
149 maxw = vv->standard->h_max_out;
150 maxh = vv->standard->v_max_out;
152 if (V4L2_FIELD_ANY == field) {
153 field = (win->w.height > maxh/2)
154 ? V4L2_FIELD_INTERLACED
159 case V4L2_FIELD_BOTTOM:
160 case V4L2_FIELD_ALTERNATE:
163 case V4L2_FIELD_INTERLACED:
166 DEB_D(("no known field mode '%d'.\n",field));
172 if (win->w.width > maxw)
174 if (win->w.height > maxh)
175 win->w.height = maxh;
180 static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
182 struct saa7146_dev *dev = fh->dev;
183 struct saa7146_vv *vv = dev->vv_data;
187 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
189 struct saa7146_format *fmt;
190 enum v4l2_field field;
194 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
196 fmt = format_by_fourcc(dev,f->fmt.pix.pixelformat);
201 field = f->fmt.pix.field;
202 maxw = vv->standard->h_max_out;
203 maxh = vv->standard->v_max_out;
205 if (V4L2_FIELD_ANY == field) {
206 field = (f->fmt.pix.height > maxh/2)
207 ? V4L2_FIELD_INTERLACED
211 case V4L2_FIELD_ALTERNATE: {
212 vv->last_field = V4L2_FIELD_TOP;
217 case V4L2_FIELD_BOTTOM:
218 vv->last_field = V4L2_FIELD_INTERLACED;
221 case V4L2_FIELD_INTERLACED:
222 vv->last_field = V4L2_FIELD_INTERLACED;
225 DEB_D(("no known field mode '%d'.\n",field));
230 f->fmt.pix.field = field;
231 if (f->fmt.pix.width > maxw)
232 f->fmt.pix.width = maxw;
233 if (f->fmt.pix.height > maxh)
234 f->fmt.pix.height = maxh;
236 calc_bpl = (f->fmt.pix.width * fmt->depth)/8;
238 if (f->fmt.pix.bytesperline < calc_bpl)
239 f->fmt.pix.bytesperline = calc_bpl;
241 if (f->fmt.pix.bytesperline > (2*PAGE_SIZE * fmt->depth)/8) /* arbitrary constraint */
242 f->fmt.pix.bytesperline = calc_bpl;
244 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
245 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));
249 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
250 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
251 err = try_win(dev,&f->fmt.win);
257 DEB_EE(("unknown format type '%d'\n",f->type));
262 int saa7146_start_preview(struct saa7146_fh *fh)
264 struct saa7146_dev *dev = fh->dev;
265 struct saa7146_vv *vv = dev->vv_data;
266 int ret = 0, err = 0;
268 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
270 /* check if we have overlay informations */
271 if( NULL == fh->ov.fh ) {
272 DEB_D(("no overlay data available. try S_FMT first.\n"));
276 /* check if streaming capture is running */
277 if (IS_CAPTURE_ACTIVE(fh) != 0) {
278 DEB_D(("streaming capture is active.\n"));
282 /* check if overlay is running */
283 if (IS_OVERLAY_ACTIVE(fh) != 0) {
284 if (vv->video_fh == fh) {
285 DEB_D(("overlay is already active.\n"));
288 DEB_D(("overlay is already active in another open.\n"));
292 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
293 DEB_D(("cannot get necessary overlay resources\n"));
297 err = try_win(dev,&fh->ov.win);
299 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
303 vv->ov_data = &fh->ov;
305 DEB_D(("%dx%d+%d+%d %s field=%s\n",
306 fh->ov.win.w.width,fh->ov.win.w.height,
307 fh->ov.win.w.left,fh->ov.win.w.top,
308 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
310 if (0 != (ret = saa7146_enable_overlay(fh))) {
311 DEB_D(("enabling overlay failed: %d\n",ret));
312 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
316 vv->video_status = STATUS_OVERLAY;
321 EXPORT_SYMBOL_GPL(saa7146_start_preview);
323 int saa7146_stop_preview(struct saa7146_fh *fh)
325 struct saa7146_dev *dev = fh->dev;
326 struct saa7146_vv *vv = dev->vv_data;
328 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
330 /* check if streaming capture is running */
331 if (IS_CAPTURE_ACTIVE(fh) != 0) {
332 DEB_D(("streaming capture is active.\n"));
336 /* check if overlay is running at all */
337 if ((vv->video_status & STATUS_OVERLAY) == 0) {
338 DEB_D(("no active overlay.\n"));
342 if (vv->video_fh != fh) {
343 DEB_D(("overlay is active, but in another open.\n"));
347 vv->video_status = 0;
350 saa7146_disable_overlay(fh);
352 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
356 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
358 static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
360 struct saa7146_dev *dev = fh->dev;
361 struct saa7146_vv *vv = dev->vv_data;
366 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
367 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
368 if (IS_CAPTURE_ACTIVE(fh) != 0) {
369 DEB_EE(("streaming capture is active\n"));
375 fh->video_fmt = f->fmt.pix;
376 DEB_EE(("set to pixelformat '%4.4s'\n",(char *)&fh->video_fmt.pixelformat));
378 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
379 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
380 err = try_win(dev,&f->fmt.win);
383 mutex_lock(&dev->lock);
384 fh->ov.win = f->fmt.win;
385 fh->ov.nclips = f->fmt.win.clipcount;
386 if (fh->ov.nclips > 16)
388 if (copy_from_user(fh->ov.clips,f->fmt.win.clips,sizeof(struct v4l2_clip)*fh->ov.nclips)) {
389 mutex_unlock(&dev->lock);
393 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
396 mutex_unlock(&dev->lock);
398 /* check if our current overlay is active */
399 if (IS_OVERLAY_ACTIVE(fh) != 0) {
400 saa7146_stop_preview(fh);
401 saa7146_start_preview(fh);
405 DEB_D(("unknown format type '%d'\n",f->type));
410 /********************************************************************************/
411 /* device controls */
413 static struct v4l2_queryctrl controls[] = {
415 .id = V4L2_CID_BRIGHTNESS,
416 .name = "Brightness",
420 .default_value = 128,
421 .type = V4L2_CTRL_TYPE_INTEGER,
423 .id = V4L2_CID_CONTRAST,
429 .type = V4L2_CTRL_TYPE_INTEGER,
431 .id = V4L2_CID_SATURATION,
432 .name = "Saturation",
437 .type = V4L2_CTRL_TYPE_INTEGER,
439 .id = V4L2_CID_VFLIP,
440 .name = "Vertical flip",
443 .type = V4L2_CTRL_TYPE_BOOLEAN,
445 .id = V4L2_CID_HFLIP,
446 .name = "Horizontal flip",
449 .type = V4L2_CTRL_TYPE_BOOLEAN,
452 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
454 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
456 static struct v4l2_queryctrl* ctrl_by_id(int id)
460 for (i = 0; i < NUM_CONTROLS; i++)
461 if (controls[i].id == id)
466 static int get_control(struct saa7146_fh *fh, struct v4l2_control *c)
468 struct saa7146_dev *dev = fh->dev;
469 struct saa7146_vv *vv = dev->vv_data;
471 const struct v4l2_queryctrl* ctrl;
474 ctrl = ctrl_by_id(c->id);
478 case V4L2_CID_BRIGHTNESS:
479 value = saa7146_read(dev, BCS_CTRL);
480 c->value = 0xff & (value >> 24);
481 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n",c->value));
483 case V4L2_CID_CONTRAST:
484 value = saa7146_read(dev, BCS_CTRL);
485 c->value = 0x7f & (value >> 16);
486 DEB_D(("V4L2_CID_CONTRAST: %d\n",c->value));
488 case V4L2_CID_SATURATION:
489 value = saa7146_read(dev, BCS_CTRL);
490 c->value = 0x7f & (value >> 0);
491 DEB_D(("V4L2_CID_SATURATION: %d\n",c->value));
494 c->value = vv->vflip;
495 DEB_D(("V4L2_CID_VFLIP: %d\n",c->value));
498 c->value = vv->hflip;
499 DEB_D(("V4L2_CID_HFLIP: %d\n",c->value));
508 static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
510 struct saa7146_dev *dev = fh->dev;
511 struct saa7146_vv *vv = dev->vv_data;
513 const struct v4l2_queryctrl* ctrl;
515 ctrl = ctrl_by_id(c->id);
517 DEB_D(("unknown control %d\n",c->id));
521 mutex_lock(&dev->lock);
523 switch (ctrl->type) {
524 case V4L2_CTRL_TYPE_BOOLEAN:
525 case V4L2_CTRL_TYPE_MENU:
526 case V4L2_CTRL_TYPE_INTEGER:
527 if (c->value < ctrl->minimum)
528 c->value = ctrl->minimum;
529 if (c->value > ctrl->maximum)
530 c->value = ctrl->maximum;
537 case V4L2_CID_BRIGHTNESS: {
538 u32 value = saa7146_read(dev, BCS_CTRL);
540 value |= (c->value << 24);
541 saa7146_write(dev, BCS_CTRL, value);
542 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
545 case V4L2_CID_CONTRAST: {
546 u32 value = saa7146_read(dev, BCS_CTRL);
548 value |= (c->value << 16);
549 saa7146_write(dev, BCS_CTRL, value);
550 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
553 case V4L2_CID_SATURATION: {
554 u32 value = saa7146_read(dev, BCS_CTRL);
556 value |= (c->value << 0);
557 saa7146_write(dev, BCS_CTRL, value);
558 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
562 /* fixme: we can support changing VFLIP and HFLIP here... */
563 if (IS_CAPTURE_ACTIVE(fh) != 0) {
564 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
565 mutex_unlock(&dev->lock);
568 vv->hflip = c->value;
571 if (IS_CAPTURE_ACTIVE(fh) != 0) {
572 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
573 mutex_unlock(&dev->lock);
576 vv->vflip = c->value;
579 mutex_unlock(&dev->lock);
583 mutex_unlock(&dev->lock);
585 if (IS_OVERLAY_ACTIVE(fh) != 0) {
586 saa7146_stop_preview(fh);
587 saa7146_start_preview(fh);
592 /********************************************************************************/
593 /* common pagetable functions */
595 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
597 struct pci_dev *pci = dev->pci;
598 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
599 struct scatterlist *list = dma->sglist;
600 int length = dma->sglen;
601 struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
603 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
605 if( 0 != IS_PLANAR(sfmt->trans)) {
606 struct saa7146_pgtable *pt1 = &buf->pt[0];
607 struct saa7146_pgtable *pt2 = &buf->pt[1];
608 struct saa7146_pgtable *pt3 = &buf->pt[2];
609 __le32 *ptr1, *ptr2, *ptr3;
612 int size = buf->fmt->width*buf->fmt->height;
613 int i,p,m1,m2,m3,o1,o2;
615 switch( sfmt->depth ) {
617 /* create some offsets inside the page table */
618 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
619 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
620 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
622 o2 = (size+(size/4))%PAGE_SIZE;
623 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
627 /* create some offsets inside the page table */
628 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
629 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
630 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
632 o2 = (size+(size/2))%PAGE_SIZE;
633 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
645 /* walk all pages, copy all page addresses to ptr1 */
646 for (i = 0; i < length; i++, list++) {
647 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
648 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
654 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
658 /* if we have a user buffer, the first page may not be
659 aligned to a page boundary. */
660 pt1->offset = dma->sglist->offset;
661 pt2->offset = pt1->offset+o1;
662 pt3->offset = pt1->offset+o2;
664 /* create video-dma2 page table */
666 for(i = m1; i <= m2 ; i++, ptr2++) {
670 for(;i<1024;i++,ptr2++) {
673 /* create video-dma3 page table */
675 for(i = m2; i <= m3; i++,ptr3++) {
679 for(;i<1024;i++,ptr3++) {
682 /* finally: finish up video-dma1 page table */
685 for(i=m1;i<1024;i++,ptr1++) {
693 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
696 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
699 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
703 struct saa7146_pgtable *pt = &buf->pt[0];
704 return saa7146_pgtable_build_single(pci, pt, list, length);
711 /********************************************************************************/
712 /* file operations */
714 static int video_begin(struct saa7146_fh *fh)
716 struct saa7146_dev *dev = fh->dev;
717 struct saa7146_vv *vv = dev->vv_data;
718 struct saa7146_format *fmt = NULL;
719 unsigned int resource;
720 int ret = 0, err = 0;
722 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
724 if ((vv->video_status & STATUS_CAPTURE) != 0) {
725 if (vv->video_fh == fh) {
726 DEB_S(("already capturing.\n"));
729 DEB_S(("already capturing in another open.\n"));
733 if ((vv->video_status & STATUS_OVERLAY) != 0) {
734 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
735 vv->ov_suspend = vv->video_fh;
736 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
738 DEB_D(("suspending video failed. aborting\n"));
743 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
744 /* we need to have a valid format set here */
747 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
748 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
750 resource = RESOURCE_DMA1_HPS;
753 ret = saa7146_res_get(fh, resource);
755 DEB_S(("cannot get capture resource %d\n",resource));
756 if (vv->ov_suspend != NULL) {
757 saa7146_start_preview(vv->ov_suspend);
758 vv->ov_suspend = NULL;
763 /* clear out beginning of streaming bit (rps register 0)*/
764 saa7146_write(dev, MC2, MASK_27 );
766 /* enable rps0 irqs */
767 SAA7146_IER_ENABLE(dev, MASK_27);
770 vv->video_status = STATUS_CAPTURE;
775 static int video_end(struct saa7146_fh *fh, struct file *file)
777 struct saa7146_dev *dev = fh->dev;
778 struct saa7146_vv *vv = dev->vv_data;
779 struct saa7146_format *fmt = NULL;
781 unsigned int resource;
783 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
785 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
786 DEB_S(("not capturing.\n"));
790 if (vv->video_fh != fh) {
791 DEB_S(("capturing, but in another open.\n"));
795 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
796 /* we need to have a valid format set here */
799 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
800 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
801 dmas = MASK_22 | MASK_21 | MASK_20;
803 resource = RESOURCE_DMA1_HPS;
806 spin_lock_irqsave(&dev->slock,flags);
809 saa7146_write(dev, MC1, MASK_28);
811 /* disable rps0 irqs */
812 SAA7146_IER_DISABLE(dev, MASK_27);
814 /* shut down all used video dma transfers */
815 saa7146_write(dev, MC1, dmas);
817 spin_unlock_irqrestore(&dev->slock, flags);
820 vv->video_status = 0;
822 saa7146_res_free(fh, resource);
824 if (vv->ov_suspend != NULL) {
825 saa7146_start_preview(vv->ov_suspend);
826 vv->ov_suspend = NULL;
833 * This function is _not_ called directly, but from
834 * video_generic_ioctl (and maybe others). userspace
835 * copying is done already, arg is a kernel pointer.
838 long saa7146_video_do_ioctl(struct file *file, unsigned int cmd, void *arg)
840 struct saa7146_fh *fh = file->private_data;
841 struct saa7146_dev *dev = fh->dev;
842 struct saa7146_vv *vv = dev->vv_data;
845 int result = 0, ee = 0;
847 struct saa7146_use_ops *ops;
848 struct videobuf_queue *q;
850 /* check if extension handles the command */
851 for(ee = 0; dev->ext_vv_data->ioctls[ee].flags != 0; ee++) {
852 if( cmd == dev->ext_vv_data->ioctls[ee].cmd )
856 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_EXCLUSIVE) ) {
857 DEB_D(("extension handles ioctl exclusive.\n"));
858 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
861 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_BEFORE) ) {
862 DEB_D(("extension handles ioctl before.\n"));
863 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
864 if( -EAGAIN != result ) {
869 /* fixme: add handle "after" case (is it still needed?) */
872 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
873 ops = &saa7146_video_uops;
877 case V4L2_BUF_TYPE_VBI_CAPTURE: {
878 ops = &saa7146_vbi_uops;
888 case VIDIOC_QUERYCAP:
890 struct v4l2_capability *cap = arg;
891 memset(cap,0,sizeof(*cap));
893 DEB_EE(("VIDIOC_QUERYCAP\n"));
895 strcpy((char *)cap->driver, "saa7146 v4l2");
896 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
897 sprintf((char *)cap->bus_info,"PCI:%s", pci_name(dev->pci));
898 cap->version = SAA7146_VERSION_CODE;
900 V4L2_CAP_VIDEO_CAPTURE |
901 V4L2_CAP_VIDEO_OVERLAY |
904 cap->capabilities |= dev->ext_vv_data->capabilities;
909 struct v4l2_framebuffer *fb = arg;
911 DEB_EE(("VIDIOC_G_FBUF\n"));
914 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
919 struct v4l2_framebuffer *fb = arg;
920 struct saa7146_format *fmt;
922 DEB_EE(("VIDIOC_S_FBUF\n"));
924 if(!capable(CAP_SYS_ADMIN) &&
925 !capable(CAP_SYS_RAWIO))
929 fmt = format_by_fourcc(dev,fb->fmt.pixelformat);
934 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
935 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
936 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",(char *)&fmt->pixelformat));
939 /* check if overlay is running */
940 if (IS_OVERLAY_ACTIVE(fh) != 0) {
941 if (vv->video_fh != fh) {
942 DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
947 mutex_lock(&dev->lock);
952 if (0 == vv->ov_fb.fmt.bytesperline)
953 vv->ov_fb.fmt.bytesperline =
954 vv->ov_fb.fmt.width*fmt->depth/8;
956 mutex_unlock(&dev->lock);
960 case VIDIOC_ENUM_FMT:
962 struct v4l2_fmtdesc *f = arg;
965 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
966 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
967 if (f->index >= NUM_FORMATS)
969 strlcpy((char *)f->description, formats[f->index].name,
970 sizeof(f->description));
971 f->pixelformat = formats[f->index].pixelformat;
973 memset(f->reserved, 0, sizeof(f->reserved));
979 DEB_EE(("VIDIOC_ENUM_FMT: type:%d, index:%d\n",f->type,f->index));
982 case VIDIOC_QUERYCTRL:
984 const struct v4l2_queryctrl *ctrl;
985 struct v4l2_queryctrl *c = arg;
987 if ((c->id < V4L2_CID_BASE ||
988 c->id >= V4L2_CID_LASTP1) &&
989 (c->id < V4L2_CID_PRIVATE_BASE ||
990 c->id >= V4L2_CID_PRIVATE_LASTP1))
993 ctrl = ctrl_by_id(c->id);
997 c->flags = V4L2_CTRL_FLAG_DISABLED;
1002 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n",c->id));
1006 case VIDIOC_G_CTRL: {
1007 DEB_EE(("VIDIOC_G_CTRL\n"));
1008 return get_control(fh,arg);
1012 DEB_EE(("VIDIOC_S_CTRL\n"));
1013 err = set_control(fh,arg);
1018 struct v4l2_streamparm *parm = arg;
1019 if( parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ) {
1022 memset(&parm->parm.capture,0,sizeof(struct v4l2_captureparm));
1023 parm->parm.capture.readbuffers = 1;
1024 // fixme: only for PAL!
1025 parm->parm.capture.timeperframe.numerator = 1;
1026 parm->parm.capture.timeperframe.denominator = 25;
1031 struct v4l2_format *f = arg;
1032 DEB_EE(("VIDIOC_G_FMT\n"));
1037 struct v4l2_format *f = arg;
1038 DEB_EE(("VIDIOC_S_FMT\n"));
1041 case VIDIOC_TRY_FMT:
1043 struct v4l2_format *f = arg;
1044 DEB_EE(("VIDIOC_TRY_FMT\n"));
1045 return try_fmt(fh,f);
1049 v4l2_std_id *id = arg;
1050 DEB_EE(("VIDIOC_G_STD\n"));
1051 *id = vv->standard->id;
1054 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
1055 PAL / NTSC / SECAM. if your hardware does not (or does more)
1056 -- override this function in your extension */
1057 case VIDIOC_ENUMSTD:
1059 struct v4l2_standard *e = arg;
1062 if( e->index < dev->ext_vv_data->num_stds ) {
1063 DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
1064 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
1071 v4l2_std_id *id = arg;
1075 DEB_EE(("VIDIOC_S_STD\n"));
1077 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
1078 DEB_D(("cannot change video standard while streaming capture is active\n"));
1082 if ((vv->video_status & STATUS_OVERLAY) != 0) {
1083 vv->ov_suspend = vv->video_fh;
1084 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
1086 DEB_D(("suspending video failed. aborting\n"));
1091 mutex_lock(&dev->lock);
1093 for(i = 0; i < dev->ext_vv_data->num_stds; i++)
1094 if (*id & dev->ext_vv_data->stds[i].id)
1096 if (i != dev->ext_vv_data->num_stds) {
1097 vv->standard = &dev->ext_vv_data->stds[i];
1098 if( NULL != dev->ext_vv_data->std_callback )
1099 dev->ext_vv_data->std_callback(dev, vv->standard);
1103 mutex_unlock(&dev->lock);
1105 if (vv->ov_suspend != NULL) {
1106 saa7146_start_preview(vv->ov_suspend);
1107 vv->ov_suspend = NULL;
1111 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
1115 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n",vv->standard->name));
1118 case VIDIOC_OVERLAY:
1120 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(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(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(q,buf,file->f_flags & O_NONBLOCK);
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(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(q);
1185 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1186 video_end(fh, file);
1188 err = video_end(fh, file);
1192 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1195 struct video_mbuf *mbuf = arg;
1198 /* fixme: number of capture buffers and sizes for v4l apps */
1200 int gbufsize = 768*576*4;
1202 DEB_D(("VIDIOCGMBUF \n"));
1205 err = videobuf_mmap_setup(q,gbuffers,gbufsize,
1211 memset(mbuf,0,sizeof(*mbuf));
1212 mbuf->frames = gbuffers;
1213 mbuf->size = gbuffers * gbufsize;
1214 for (i = 0; i < gbuffers; i++)
1215 mbuf->offsets[i] = i * gbufsize;
1220 return v4l_compat_translate_ioctl(file, cmd, arg,
1221 saa7146_video_do_ioctl);
1226 /*********************************************************************************/
1227 /* buffer handling functions */
1229 static int buffer_activate (struct saa7146_dev *dev,
1230 struct saa7146_buf *buf,
1231 struct saa7146_buf *next)
1233 struct saa7146_vv *vv = dev->vv_data;
1235 buf->vb.state = VIDEOBUF_ACTIVE;
1236 saa7146_set_capture(dev,buf,next);
1238 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1242 static int buffer_prepare(struct videobuf_queue *q,
1243 struct videobuf_buffer *vb, enum v4l2_field field)
1245 struct file *file = q->priv_data;
1246 struct saa7146_fh *fh = file->private_data;
1247 struct saa7146_dev *dev = fh->dev;
1248 struct saa7146_vv *vv = dev->vv_data;
1249 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1252 DEB_CAP(("vbuf:%p\n",vb));
1255 if (fh->video_fmt.width < 48 ||
1256 fh->video_fmt.height < 32 ||
1257 fh->video_fmt.width > vv->standard->h_max_out ||
1258 fh->video_fmt.height > vv->standard->v_max_out) {
1259 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
1263 size = fh->video_fmt.sizeimage;
1264 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1265 DEB_D(("size mismatch.\n"));
1269 DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1270 fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
1271 if (buf->vb.width != fh->video_fmt.width ||
1272 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1273 buf->vb.height != fh->video_fmt.height ||
1274 buf->vb.size != size ||
1275 buf->vb.field != field ||
1276 buf->vb.field != fh->video_fmt.field ||
1277 buf->fmt != &fh->video_fmt) {
1278 saa7146_dma_free(dev,q,buf);
1281 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1282 struct saa7146_format *sfmt;
1284 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1285 buf->vb.width = fh->video_fmt.width;
1286 buf->vb.height = fh->video_fmt.height;
1287 buf->vb.size = size;
1288 buf->vb.field = field;
1289 buf->fmt = &fh->video_fmt;
1290 buf->vb.field = fh->video_fmt.field;
1292 sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
1294 if( 0 != IS_PLANAR(sfmt->trans)) {
1295 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1296 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1297 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1299 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1300 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1301 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1303 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1304 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1307 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1310 err = saa7146_pgtable_build(dev,buf);
1314 buf->vb.state = VIDEOBUF_PREPARED;
1315 buf->activate = buffer_activate;
1320 DEB_D(("error out.\n"));
1321 saa7146_dma_free(dev,q,buf);
1326 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1328 struct file *file = q->priv_data;
1329 struct saa7146_fh *fh = file->private_data;
1331 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1332 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1334 *size = fh->video_fmt.sizeimage;
1336 /* check if we exceed the "max_memory" parameter */
1337 if( (*count * *size) > (max_memory*1048576) ) {
1338 *count = (max_memory*1048576) / *size;
1341 DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
1346 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1348 struct file *file = q->priv_data;
1349 struct saa7146_fh *fh = file->private_data;
1350 struct saa7146_dev *dev = fh->dev;
1351 struct saa7146_vv *vv = dev->vv_data;
1352 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1354 DEB_CAP(("vbuf:%p\n",vb));
1355 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1358 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1360 struct file *file = q->priv_data;
1361 struct saa7146_fh *fh = file->private_data;
1362 struct saa7146_dev *dev = fh->dev;
1363 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1365 DEB_CAP(("vbuf:%p\n",vb));
1366 saa7146_dma_free(dev,q,buf);
1369 static struct videobuf_queue_ops video_qops = {
1370 .buf_setup = buffer_setup,
1371 .buf_prepare = buffer_prepare,
1372 .buf_queue = buffer_queue,
1373 .buf_release = buffer_release,
1376 /********************************************************************************/
1377 /* file operations */
1379 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1381 INIT_LIST_HEAD(&vv->video_q.queue);
1383 init_timer(&vv->video_q.timeout);
1384 vv->video_q.timeout.function = saa7146_buffer_timeout;
1385 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1386 vv->video_q.dev = dev;
1388 /* set some default values */
1389 vv->standard = &dev->ext_vv_data->stds[0];
1391 /* FIXME: what's this? */
1392 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1393 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1397 static int video_open(struct saa7146_dev *dev, struct file *file)
1399 struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1400 struct saa7146_format *sfmt;
1402 fh->video_fmt.width = 384;
1403 fh->video_fmt.height = 288;
1404 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1405 fh->video_fmt.bytesperline = 0;
1406 fh->video_fmt.field = V4L2_FIELD_ANY;
1407 sfmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
1408 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1410 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1411 &dev->pci->dev, &dev->slock,
1412 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1413 V4L2_FIELD_INTERLACED,
1414 sizeof(struct saa7146_buf),
1421 static void video_close(struct saa7146_dev *dev, struct file *file)
1423 struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
1424 struct saa7146_vv *vv = dev->vv_data;
1425 struct videobuf_queue *q = &fh->video_q;
1428 if (IS_CAPTURE_ACTIVE(fh) != 0) {
1429 err = video_end(fh, file);
1430 } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
1431 err = saa7146_stop_preview(fh);
1436 /* hmm, why is this function declared void? */
1441 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1443 struct saa7146_vv *vv = dev->vv_data;
1444 struct saa7146_dmaqueue *q = &vv->video_q;
1446 spin_lock(&dev->slock);
1447 DEB_CAP(("called.\n"));
1449 /* only finish the buffer if we have one... */
1450 if( NULL != q->curr ) {
1451 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1453 saa7146_buffer_next(dev,q,0);
1455 spin_unlock(&dev->slock);
1458 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1460 struct saa7146_fh *fh = file->private_data;
1461 struct saa7146_dev *dev = fh->dev;
1462 struct saa7146_vv *vv = dev->vv_data;
1465 DEB_EE(("called.\n"));
1467 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1468 /* fixme: should we allow read() captures while streaming capture? */
1469 if (vv->video_fh == fh) {
1470 DEB_S(("already capturing.\n"));
1473 DEB_S(("already capturing in another open.\n"));
1477 ret = video_begin(fh);
1482 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1483 file->f_flags & O_NONBLOCK);
1485 video_end(fh, file);
1487 ret = video_end(fh, file);
1490 /* restart overlay if it was active before */
1491 if (vv->ov_suspend != NULL) {
1492 saa7146_start_preview(vv->ov_suspend);
1493 vv->ov_suspend = NULL;
1499 struct saa7146_use_ops saa7146_video_uops = {
1502 .release = video_close,
1503 .irq_done = video_irq_done,