1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <media/saa7146_vv.h>
4 #include <media/v4l2-chip-ident.h>
6 static int max_memory = 32;
8 module_param(max_memory, int, 0644);
9 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
11 #define IS_CAPTURE_ACTIVE(fh) \
12 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
14 #define IS_OVERLAY_ACTIVE(fh) \
15 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
17 /* format descriptions for capture and preview */
18 static struct saa7146_format formats[] = {
20 .name = "RGB-8 (3-3-2)",
21 .pixelformat = V4L2_PIX_FMT_RGB332,
22 .trans = RGB08_COMPOSED,
26 .name = "RGB-16 (5/B-6/G-5/R)",
27 .pixelformat = V4L2_PIX_FMT_RGB565,
28 .trans = RGB16_COMPOSED,
32 .name = "RGB-24 (B-G-R)",
33 .pixelformat = V4L2_PIX_FMT_BGR24,
34 .trans = RGB24_COMPOSED,
38 .name = "RGB-32 (B-G-R)",
39 .pixelformat = V4L2_PIX_FMT_BGR32,
40 .trans = RGB32_COMPOSED,
44 .name = "RGB-32 (R-G-B)",
45 .pixelformat = V4L2_PIX_FMT_RGB32,
46 .trans = RGB32_COMPOSED,
51 .name = "Greyscale-8",
52 .pixelformat = V4L2_PIX_FMT_GREY,
57 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
58 .pixelformat = V4L2_PIX_FMT_YUV422P,
59 .trans = YUV422_DECOMPOSED,
61 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
63 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
64 .pixelformat = V4L2_PIX_FMT_YVU420,
65 .trans = YUV420_DECOMPOSED,
67 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
69 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
70 .pixelformat = V4L2_PIX_FMT_YUV420,
71 .trans = YUV420_DECOMPOSED,
73 .flags = FORMAT_IS_PLANAR,
75 .name = "YUV 4:2:2 (U-Y-V-Y)",
76 .pixelformat = V4L2_PIX_FMT_UYVY,
77 .trans = YUV422_COMPOSED,
83 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
84 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
85 (like V4L2_PIX_FMT_YUYV) ... 8-( */
87 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
89 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
91 int i, j = NUM_FORMATS;
93 for (i = 0; i < j; i++) {
94 if (formats[i].pixelformat == fourcc) {
99 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
103 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
105 int saa7146_start_preview(struct saa7146_fh *fh)
107 struct saa7146_dev *dev = fh->dev;
108 struct saa7146_vv *vv = dev->vv_data;
109 struct v4l2_format fmt;
110 int ret = 0, err = 0;
112 DEB_EE("dev:%p, fh:%p\n", dev, fh);
114 /* check if we have overlay informations */
115 if( NULL == fh->ov.fh ) {
116 DEB_D("no overlay data available. try S_FMT first.\n");
120 /* check if streaming capture is running */
121 if (IS_CAPTURE_ACTIVE(fh) != 0) {
122 DEB_D("streaming capture is active\n");
126 /* check if overlay is running */
127 if (IS_OVERLAY_ACTIVE(fh) != 0) {
128 if (vv->video_fh == fh) {
129 DEB_D("overlay is already active\n");
132 DEB_D("overlay is already active in another open\n");
136 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
137 DEB_D("cannot get necessary overlay resources\n");
141 fmt.fmt.win = fh->ov.win;
142 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
144 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
147 fh->ov.win = fmt.fmt.win;
148 vv->ov_data = &fh->ov;
150 DEB_D("%dx%d+%d+%d %s field=%s\n",
151 fh->ov.win.w.width, fh->ov.win.w.height,
152 fh->ov.win.w.left, fh->ov.win.w.top,
153 vv->ov_fmt->name, v4l2_field_names[fh->ov.win.field]);
155 if (0 != (ret = saa7146_enable_overlay(fh))) {
156 DEB_D("enabling overlay failed: %d\n", ret);
157 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
161 vv->video_status = STATUS_OVERLAY;
166 EXPORT_SYMBOL_GPL(saa7146_start_preview);
168 int saa7146_stop_preview(struct saa7146_fh *fh)
170 struct saa7146_dev *dev = fh->dev;
171 struct saa7146_vv *vv = dev->vv_data;
173 DEB_EE("dev:%p, fh:%p\n", dev, fh);
175 /* check if streaming capture is running */
176 if (IS_CAPTURE_ACTIVE(fh) != 0) {
177 DEB_D("streaming capture is active\n");
181 /* check if overlay is running at all */
182 if ((vv->video_status & STATUS_OVERLAY) == 0) {
183 DEB_D("no active overlay\n");
187 if (vv->video_fh != fh) {
188 DEB_D("overlay is active, but in another open\n");
192 vv->video_status = 0;
195 saa7146_disable_overlay(fh);
197 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
201 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
203 /********************************************************************************/
204 /* device controls */
206 static struct v4l2_queryctrl controls[] = {
208 .id = V4L2_CID_BRIGHTNESS,
209 .name = "Brightness",
213 .default_value = 128,
214 .type = V4L2_CTRL_TYPE_INTEGER,
215 .flags = V4L2_CTRL_FLAG_SLIDER,
217 .id = V4L2_CID_CONTRAST,
223 .type = V4L2_CTRL_TYPE_INTEGER,
224 .flags = V4L2_CTRL_FLAG_SLIDER,
226 .id = V4L2_CID_SATURATION,
227 .name = "Saturation",
232 .type = V4L2_CTRL_TYPE_INTEGER,
233 .flags = V4L2_CTRL_FLAG_SLIDER,
235 .id = V4L2_CID_VFLIP,
236 .name = "Vertical Flip",
239 .type = V4L2_CTRL_TYPE_BOOLEAN,
241 .id = V4L2_CID_HFLIP,
242 .name = "Horizontal Flip",
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
248 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
250 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
252 static struct v4l2_queryctrl* ctrl_by_id(int id)
256 for (i = 0; i < NUM_CONTROLS; i++)
257 if (controls[i].id == id)
262 /********************************************************************************/
263 /* common pagetable functions */
265 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
267 struct pci_dev *pci = dev->pci;
268 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
269 struct scatterlist *list = dma->sglist;
270 int length = dma->sglen;
271 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
273 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
275 if( 0 != IS_PLANAR(sfmt->trans)) {
276 struct saa7146_pgtable *pt1 = &buf->pt[0];
277 struct saa7146_pgtable *pt2 = &buf->pt[1];
278 struct saa7146_pgtable *pt3 = &buf->pt[2];
279 __le32 *ptr1, *ptr2, *ptr3;
282 int size = buf->fmt->width*buf->fmt->height;
283 int i,p,m1,m2,m3,o1,o2;
285 switch( sfmt->depth ) {
287 /* create some offsets inside the page table */
288 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
289 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
290 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
292 o2 = (size+(size/4))%PAGE_SIZE;
293 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
294 size, m1, m2, m3, o1, o2);
298 /* create some offsets inside the page table */
299 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
300 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
301 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
303 o2 = (size+(size/2))%PAGE_SIZE;
304 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
305 size, m1, m2, m3, o1, o2);
317 /* walk all pages, copy all page addresses to ptr1 */
318 for (i = 0; i < length; i++, list++) {
319 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
320 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
326 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
330 /* if we have a user buffer, the first page may not be
331 aligned to a page boundary. */
332 pt1->offset = dma->sglist->offset;
333 pt2->offset = pt1->offset+o1;
334 pt3->offset = pt1->offset+o2;
336 /* create video-dma2 page table */
338 for(i = m1; i <= m2 ; i++, ptr2++) {
342 for(;i<1024;i++,ptr2++) {
345 /* create video-dma3 page table */
347 for(i = m2; i <= m3; i++,ptr3++) {
351 for(;i<1024;i++,ptr3++) {
354 /* finally: finish up video-dma1 page table */
357 for(i=m1;i<1024;i++,ptr1++) {
365 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
368 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
371 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
375 struct saa7146_pgtable *pt = &buf->pt[0];
376 return saa7146_pgtable_build_single(pci, pt, list, length);
383 /********************************************************************************/
384 /* file operations */
386 static int video_begin(struct saa7146_fh *fh)
388 struct saa7146_dev *dev = fh->dev;
389 struct saa7146_vv *vv = dev->vv_data;
390 struct saa7146_format *fmt = NULL;
391 unsigned int resource;
392 int ret = 0, err = 0;
394 DEB_EE("dev:%p, fh:%p\n", dev, fh);
396 if ((vv->video_status & STATUS_CAPTURE) != 0) {
397 if (vv->video_fh == fh) {
398 DEB_S("already capturing\n");
401 DEB_S("already capturing in another open\n");
405 if ((vv->video_status & STATUS_OVERLAY) != 0) {
406 DEB_S("warning: suspending overlay video for streaming capture\n");
407 vv->ov_suspend = vv->video_fh;
408 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
410 DEB_D("suspending video failed. aborting\n");
415 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
416 /* we need to have a valid format set here */
419 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
420 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
422 resource = RESOURCE_DMA1_HPS;
425 ret = saa7146_res_get(fh, resource);
427 DEB_S("cannot get capture resource %d\n", resource);
428 if (vv->ov_suspend != NULL) {
429 saa7146_start_preview(vv->ov_suspend);
430 vv->ov_suspend = NULL;
435 /* clear out beginning of streaming bit (rps register 0)*/
436 saa7146_write(dev, MC2, MASK_27 );
438 /* enable rps0 irqs */
439 SAA7146_IER_ENABLE(dev, MASK_27);
442 vv->video_status = STATUS_CAPTURE;
447 static int video_end(struct saa7146_fh *fh, struct file *file)
449 struct saa7146_dev *dev = fh->dev;
450 struct saa7146_vv *vv = dev->vv_data;
451 struct saa7146_format *fmt = NULL;
453 unsigned int resource;
455 DEB_EE("dev:%p, fh:%p\n", dev, fh);
457 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
458 DEB_S("not capturing\n");
462 if (vv->video_fh != fh) {
463 DEB_S("capturing, but in another open\n");
467 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
468 /* we need to have a valid format set here */
471 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
472 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
473 dmas = MASK_22 | MASK_21 | MASK_20;
475 resource = RESOURCE_DMA1_HPS;
478 spin_lock_irqsave(&dev->slock,flags);
481 saa7146_write(dev, MC1, MASK_28);
483 /* disable rps0 irqs */
484 SAA7146_IER_DISABLE(dev, MASK_27);
486 /* shut down all used video dma transfers */
487 saa7146_write(dev, MC1, dmas);
489 spin_unlock_irqrestore(&dev->slock, flags);
492 vv->video_status = 0;
494 saa7146_res_free(fh, resource);
496 if (vv->ov_suspend != NULL) {
497 saa7146_start_preview(vv->ov_suspend);
498 vv->ov_suspend = NULL;
504 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
506 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
508 strcpy((char *)cap->driver, "saa7146 v4l2");
509 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
510 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
511 cap->version = SAA7146_VERSION_CODE;
513 V4L2_CAP_VIDEO_CAPTURE |
514 V4L2_CAP_VIDEO_OVERLAY |
517 cap->capabilities |= dev->ext_vv_data->capabilities;
521 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
523 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
524 struct saa7146_vv *vv = dev->vv_data;
527 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
531 static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
533 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
534 struct saa7146_vv *vv = dev->vv_data;
535 struct saa7146_format *fmt;
537 DEB_EE("VIDIOC_S_FBUF\n");
539 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
543 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
547 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
548 if (fmt->flags & FORMAT_IS_PLANAR)
549 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
550 (char *)&fmt->pixelformat);
552 /* check if overlay is running */
553 if (IS_OVERLAY_ACTIVE(fh) != 0) {
554 if (vv->video_fh != fh) {
555 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
564 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
565 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
566 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
571 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
573 if (f->index >= NUM_FORMATS)
575 strlcpy((char *)f->description, formats[f->index].name,
576 sizeof(f->description));
577 f->pixelformat = formats[f->index].pixelformat;
581 static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
583 const struct v4l2_queryctrl *ctrl;
585 if ((c->id < V4L2_CID_BASE ||
586 c->id >= V4L2_CID_LASTP1) &&
587 (c->id < V4L2_CID_PRIVATE_BASE ||
588 c->id >= V4L2_CID_PRIVATE_LASTP1))
591 ctrl = ctrl_by_id(c->id);
595 DEB_EE("VIDIOC_QUERYCTRL: id:%d\n", c->id);
600 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
602 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
603 struct saa7146_vv *vv = dev->vv_data;
604 const struct v4l2_queryctrl *ctrl;
607 ctrl = ctrl_by_id(c->id);
611 case V4L2_CID_BRIGHTNESS:
612 value = saa7146_read(dev, BCS_CTRL);
613 c->value = 0xff & (value >> 24);
614 DEB_D("V4L2_CID_BRIGHTNESS: %d\n", c->value);
616 case V4L2_CID_CONTRAST:
617 value = saa7146_read(dev, BCS_CTRL);
618 c->value = 0x7f & (value >> 16);
619 DEB_D("V4L2_CID_CONTRAST: %d\n", c->value);
621 case V4L2_CID_SATURATION:
622 value = saa7146_read(dev, BCS_CTRL);
623 c->value = 0x7f & (value >> 0);
624 DEB_D("V4L2_CID_SATURATION: %d\n", c->value);
627 c->value = vv->vflip;
628 DEB_D("V4L2_CID_VFLIP: %d\n", c->value);
631 c->value = vv->hflip;
632 DEB_D("V4L2_CID_HFLIP: %d\n", c->value);
640 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
642 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
643 struct saa7146_vv *vv = dev->vv_data;
644 const struct v4l2_queryctrl *ctrl;
646 ctrl = ctrl_by_id(c->id);
648 DEB_D("unknown control %d\n", c->id);
652 switch (ctrl->type) {
653 case V4L2_CTRL_TYPE_BOOLEAN:
654 case V4L2_CTRL_TYPE_MENU:
655 case V4L2_CTRL_TYPE_INTEGER:
656 if (c->value < ctrl->minimum)
657 c->value = ctrl->minimum;
658 if (c->value > ctrl->maximum)
659 c->value = ctrl->maximum;
666 case V4L2_CID_BRIGHTNESS: {
667 u32 value = saa7146_read(dev, BCS_CTRL);
669 value |= (c->value << 24);
670 saa7146_write(dev, BCS_CTRL, value);
671 saa7146_write(dev, MC2, MASK_22 | MASK_06);
674 case V4L2_CID_CONTRAST: {
675 u32 value = saa7146_read(dev, BCS_CTRL);
677 value |= (c->value << 16);
678 saa7146_write(dev, BCS_CTRL, value);
679 saa7146_write(dev, MC2, MASK_22 | MASK_06);
682 case V4L2_CID_SATURATION: {
683 u32 value = saa7146_read(dev, BCS_CTRL);
685 value |= (c->value << 0);
686 saa7146_write(dev, BCS_CTRL, value);
687 saa7146_write(dev, MC2, MASK_22 | MASK_06);
691 /* fixme: we can support changing VFLIP and HFLIP here... */
692 if (IS_CAPTURE_ACTIVE(fh) != 0) {
693 DEB_D("V4L2_CID_HFLIP while active capture\n");
696 vv->hflip = c->value;
699 if (IS_CAPTURE_ACTIVE(fh) != 0) {
700 DEB_D("V4L2_CID_VFLIP while active capture\n");
703 vv->vflip = c->value;
709 if (IS_OVERLAY_ACTIVE(fh) != 0) {
710 saa7146_stop_preview(fh);
711 saa7146_start_preview(fh);
716 static int vidioc_g_parm(struct file *file, void *fh,
717 struct v4l2_streamparm *parm)
719 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
720 struct saa7146_vv *vv = dev->vv_data;
722 parm->parm.capture.readbuffers = 1;
723 v4l2_video_std_frame_period(vv->standard->id,
724 &parm->parm.capture.timeperframe);
728 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
730 f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
734 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
736 f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
740 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
742 f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
746 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
748 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
749 struct saa7146_vv *vv = dev->vv_data;
750 struct saa7146_format *fmt;
751 enum v4l2_field field;
755 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
757 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
761 field = f->fmt.pix.field;
762 maxw = vv->standard->h_max_out;
763 maxh = vv->standard->v_max_out;
765 if (V4L2_FIELD_ANY == field) {
766 field = (f->fmt.pix.height > maxh / 2)
767 ? V4L2_FIELD_INTERLACED
771 case V4L2_FIELD_ALTERNATE:
772 vv->last_field = V4L2_FIELD_TOP;
776 case V4L2_FIELD_BOTTOM:
777 vv->last_field = V4L2_FIELD_INTERLACED;
780 case V4L2_FIELD_INTERLACED:
781 vv->last_field = V4L2_FIELD_INTERLACED;
784 DEB_D("no known field mode '%d'\n", field);
788 f->fmt.pix.field = field;
789 if (f->fmt.pix.width > maxw)
790 f->fmt.pix.width = maxw;
791 if (f->fmt.pix.height > maxh)
792 f->fmt.pix.height = maxh;
794 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
796 if (f->fmt.pix.bytesperline < calc_bpl)
797 f->fmt.pix.bytesperline = calc_bpl;
799 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
800 f->fmt.pix.bytesperline = calc_bpl;
802 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
803 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
804 f->fmt.pix.width, f->fmt.pix.height,
805 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
811 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
813 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
814 struct saa7146_vv *vv = dev->vv_data;
815 struct v4l2_window *win = &f->fmt.win;
816 enum v4l2_field field;
819 DEB_EE("dev:%p\n", dev);
821 if (NULL == vv->ov_fb.base) {
822 DEB_D("no fb base set\n");
825 if (NULL == vv->ov_fmt) {
826 DEB_D("no fb fmt set\n");
829 if (win->w.width < 48 || win->w.height < 32) {
830 DEB_D("min width/height. (%d,%d)\n",
831 win->w.width, win->w.height);
834 if (win->clipcount > 16) {
835 DEB_D("clipcount too big\n");
840 maxw = vv->standard->h_max_out;
841 maxh = vv->standard->v_max_out;
843 if (V4L2_FIELD_ANY == field) {
844 field = (win->w.height > maxh / 2)
845 ? V4L2_FIELD_INTERLACED
850 case V4L2_FIELD_BOTTOM:
851 case V4L2_FIELD_ALTERNATE:
854 case V4L2_FIELD_INTERLACED:
857 DEB_D("no known field mode '%d'\n", field);
862 if (win->w.width > maxw)
864 if (win->w.height > maxh)
865 win->w.height = maxh;
870 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
872 struct saa7146_fh *fh = __fh;
873 struct saa7146_dev *dev = fh->dev;
874 struct saa7146_vv *vv = dev->vv_data;
877 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
878 if (IS_CAPTURE_ACTIVE(fh) != 0) {
879 DEB_EE("streaming capture is active\n");
882 err = vidioc_try_fmt_vid_cap(file, fh, f);
885 fh->video_fmt = f->fmt.pix;
886 DEB_EE("set to pixelformat '%4.4s'\n",
887 (char *)&fh->video_fmt.pixelformat);
891 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
893 struct saa7146_fh *fh = __fh;
894 struct saa7146_dev *dev = fh->dev;
895 struct saa7146_vv *vv = dev->vv_data;
898 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
899 err = vidioc_try_fmt_vid_overlay(file, fh, f);
902 fh->ov.win = f->fmt.win;
903 fh->ov.nclips = f->fmt.win.clipcount;
904 if (fh->ov.nclips > 16)
906 if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
907 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
911 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
914 /* check if our current overlay is active */
915 if (IS_OVERLAY_ACTIVE(fh) != 0) {
916 saa7146_stop_preview(fh);
917 saa7146_start_preview(fh);
922 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
924 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
925 struct saa7146_vv *vv = dev->vv_data;
927 *norm = vv->standard->id;
931 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
932 PAL / NTSC / SECAM. if your hardware does not (or does more)
933 -- override this function in your extension */
937 struct v4l2_standard *e = arg;
940 if( e->index < dev->ext_vv_data->num_stds ) {
941 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
942 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
949 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
951 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
952 struct saa7146_vv *vv = dev->vv_data;
956 DEB_EE("VIDIOC_S_STD\n");
958 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
959 DEB_D("cannot change video standard while streaming capture is active\n");
963 if ((vv->video_status & STATUS_OVERLAY) != 0) {
964 vv->ov_suspend = vv->video_fh;
965 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
967 DEB_D("suspending video failed. aborting\n");
972 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
973 if (*id & dev->ext_vv_data->stds[i].id)
975 if (i != dev->ext_vv_data->num_stds) {
976 vv->standard = &dev->ext_vv_data->stds[i];
977 if (NULL != dev->ext_vv_data->std_callback)
978 dev->ext_vv_data->std_callback(dev, vv->standard);
982 if (vv->ov_suspend != NULL) {
983 saa7146_start_preview(vv->ov_suspend);
984 vv->ov_suspend = NULL;
988 DEB_EE("VIDIOC_S_STD: standard not found\n");
992 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
996 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
1000 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
1002 err = saa7146_start_preview(fh);
1004 err = saa7146_stop_preview(fh);
1008 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
1010 struct saa7146_fh *fh = __fh;
1012 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1013 return videobuf_reqbufs(&fh->video_q, b);
1014 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1015 return videobuf_reqbufs(&fh->vbi_q, b);
1019 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1021 struct saa7146_fh *fh = __fh;
1023 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 return videobuf_querybuf(&fh->video_q, buf);
1025 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1026 return videobuf_querybuf(&fh->vbi_q, buf);
1030 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1032 struct saa7146_fh *fh = __fh;
1034 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1035 return videobuf_qbuf(&fh->video_q, buf);
1036 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1037 return videobuf_qbuf(&fh->vbi_q, buf);
1041 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1043 struct saa7146_fh *fh = __fh;
1045 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1046 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
1047 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1048 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
1052 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
1054 struct saa7146_fh *fh = __fh;
1057 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
1059 err = video_begin(fh);
1062 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1063 return videobuf_streamon(&fh->video_q);
1064 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1065 return videobuf_streamon(&fh->vbi_q);
1069 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
1071 struct saa7146_fh *fh = __fh;
1072 struct saa7146_dev *dev = fh->dev;
1073 struct saa7146_vv *vv = dev->vv_data;
1076 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
1078 /* ugly: we need to copy some checks from video_end(),
1079 because videobuf_streamoff() relies on the capture running.
1080 check and fix this */
1081 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
1082 DEB_S("not capturing\n");
1086 if (vv->video_fh != fh) {
1087 DEB_S("capturing, but in another open\n");
1092 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1093 err = videobuf_streamoff(&fh->video_q);
1094 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1095 err = videobuf_streamoff(&fh->vbi_q);
1097 DEB_D("warning: videobuf_streamoff() failed\n");
1098 video_end(fh, file);
1100 err = video_end(fh, file);
1105 static int vidioc_g_chip_ident(struct file *file, void *__fh,
1106 struct v4l2_dbg_chip_ident *chip)
1108 struct saa7146_fh *fh = __fh;
1109 struct saa7146_dev *dev = fh->dev;
1111 chip->ident = V4L2_IDENT_NONE;
1113 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
1114 chip->ident = V4L2_IDENT_SAA7146;
1117 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1118 core, g_chip_ident, chip);
1121 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1122 .vidioc_querycap = vidioc_querycap,
1123 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1124 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
1125 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1126 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1127 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1128 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1129 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1130 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1131 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1132 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1134 .vidioc_overlay = vidioc_overlay,
1135 .vidioc_g_fbuf = vidioc_g_fbuf,
1136 .vidioc_s_fbuf = vidioc_s_fbuf,
1137 .vidioc_reqbufs = vidioc_reqbufs,
1138 .vidioc_querybuf = vidioc_querybuf,
1139 .vidioc_qbuf = vidioc_qbuf,
1140 .vidioc_dqbuf = vidioc_dqbuf,
1141 .vidioc_g_std = vidioc_g_std,
1142 .vidioc_s_std = vidioc_s_std,
1143 .vidioc_queryctrl = vidioc_queryctrl,
1144 .vidioc_g_ctrl = vidioc_g_ctrl,
1145 .vidioc_s_ctrl = vidioc_s_ctrl,
1146 .vidioc_streamon = vidioc_streamon,
1147 .vidioc_streamoff = vidioc_streamoff,
1148 .vidioc_g_parm = vidioc_g_parm,
1151 /*********************************************************************************/
1152 /* buffer handling functions */
1154 static int buffer_activate (struct saa7146_dev *dev,
1155 struct saa7146_buf *buf,
1156 struct saa7146_buf *next)
1158 struct saa7146_vv *vv = dev->vv_data;
1160 buf->vb.state = VIDEOBUF_ACTIVE;
1161 saa7146_set_capture(dev,buf,next);
1163 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1167 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1169 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1170 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1171 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1174 static int buffer_prepare(struct videobuf_queue *q,
1175 struct videobuf_buffer *vb, enum v4l2_field field)
1177 struct file *file = q->priv_data;
1178 struct saa7146_fh *fh = file->private_data;
1179 struct saa7146_dev *dev = fh->dev;
1180 struct saa7146_vv *vv = dev->vv_data;
1181 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1184 DEB_CAP("vbuf:%p\n", vb);
1187 if (fh->video_fmt.width < 48 ||
1188 fh->video_fmt.height < 32 ||
1189 fh->video_fmt.width > vv->standard->h_max_out ||
1190 fh->video_fmt.height > vv->standard->v_max_out) {
1191 DEB_D("w (%d) / h (%d) out of bounds\n",
1192 fh->video_fmt.width, fh->video_fmt.height);
1196 size = fh->video_fmt.sizeimage;
1197 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1198 DEB_D("size mismatch\n");
1202 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1203 fh->video_fmt.width, fh->video_fmt.height,
1204 size, v4l2_field_names[fh->video_fmt.field]);
1205 if (buf->vb.width != fh->video_fmt.width ||
1206 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1207 buf->vb.height != fh->video_fmt.height ||
1208 buf->vb.size != size ||
1209 buf->vb.field != field ||
1210 buf->vb.field != fh->video_fmt.field ||
1211 buf->fmt != &fh->video_fmt) {
1212 saa7146_dma_free(dev,q,buf);
1215 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1216 struct saa7146_format *sfmt;
1218 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1219 buf->vb.width = fh->video_fmt.width;
1220 buf->vb.height = fh->video_fmt.height;
1221 buf->vb.size = size;
1222 buf->vb.field = field;
1223 buf->fmt = &fh->video_fmt;
1224 buf->vb.field = fh->video_fmt.field;
1226 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
1228 release_all_pagetables(dev, buf);
1229 if( 0 != IS_PLANAR(sfmt->trans)) {
1230 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1231 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1232 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1234 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1237 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1240 err = saa7146_pgtable_build(dev,buf);
1244 buf->vb.state = VIDEOBUF_PREPARED;
1245 buf->activate = buffer_activate;
1250 DEB_D("error out\n");
1251 saa7146_dma_free(dev,q,buf);
1256 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1258 struct file *file = q->priv_data;
1259 struct saa7146_fh *fh = file->private_data;
1261 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1262 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1264 *size = fh->video_fmt.sizeimage;
1266 /* check if we exceed the "max_memory" parameter */
1267 if( (*count * *size) > (max_memory*1048576) ) {
1268 *count = (max_memory*1048576) / *size;
1271 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
1276 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1278 struct file *file = q->priv_data;
1279 struct saa7146_fh *fh = file->private_data;
1280 struct saa7146_dev *dev = fh->dev;
1281 struct saa7146_vv *vv = dev->vv_data;
1282 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1284 DEB_CAP("vbuf:%p\n", vb);
1285 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1288 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1290 struct file *file = q->priv_data;
1291 struct saa7146_fh *fh = file->private_data;
1292 struct saa7146_dev *dev = fh->dev;
1293 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1295 DEB_CAP("vbuf:%p\n", vb);
1297 saa7146_dma_free(dev,q,buf);
1299 release_all_pagetables(dev, buf);
1302 static struct videobuf_queue_ops video_qops = {
1303 .buf_setup = buffer_setup,
1304 .buf_prepare = buffer_prepare,
1305 .buf_queue = buffer_queue,
1306 .buf_release = buffer_release,
1309 /********************************************************************************/
1310 /* file operations */
1312 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1314 INIT_LIST_HEAD(&vv->video_q.queue);
1316 init_timer(&vv->video_q.timeout);
1317 vv->video_q.timeout.function = saa7146_buffer_timeout;
1318 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1319 vv->video_q.dev = dev;
1321 /* set some default values */
1322 vv->standard = &dev->ext_vv_data->stds[0];
1324 /* FIXME: what's this? */
1325 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1326 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1330 static int video_open(struct saa7146_dev *dev, struct file *file)
1332 struct saa7146_fh *fh = file->private_data;
1333 struct saa7146_format *sfmt;
1335 fh->video_fmt.width = 384;
1336 fh->video_fmt.height = 288;
1337 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1338 fh->video_fmt.bytesperline = 0;
1339 fh->video_fmt.field = V4L2_FIELD_ANY;
1340 sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
1341 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1343 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1344 &dev->pci->dev, &dev->slock,
1345 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1346 V4L2_FIELD_INTERLACED,
1347 sizeof(struct saa7146_buf),
1348 file, &dev->v4l2_lock);
1354 static void video_close(struct saa7146_dev *dev, struct file *file)
1356 struct saa7146_fh *fh = file->private_data;
1357 struct saa7146_vv *vv = dev->vv_data;
1358 struct videobuf_queue *q = &fh->video_q;
1360 if (IS_CAPTURE_ACTIVE(fh) != 0)
1361 video_end(fh, file);
1362 else if (IS_OVERLAY_ACTIVE(fh) != 0)
1363 saa7146_stop_preview(fh);
1366 /* hmm, why is this function declared void? */
1370 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1372 struct saa7146_vv *vv = dev->vv_data;
1373 struct saa7146_dmaqueue *q = &vv->video_q;
1375 spin_lock(&dev->slock);
1376 DEB_CAP("called\n");
1378 /* only finish the buffer if we have one... */
1379 if( NULL != q->curr ) {
1380 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1382 saa7146_buffer_next(dev,q,0);
1384 spin_unlock(&dev->slock);
1387 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1389 struct saa7146_fh *fh = file->private_data;
1390 struct saa7146_dev *dev = fh->dev;
1391 struct saa7146_vv *vv = dev->vv_data;
1396 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1397 /* fixme: should we allow read() captures while streaming capture? */
1398 if (vv->video_fh == fh) {
1399 DEB_S("already capturing\n");
1402 DEB_S("already capturing in another open\n");
1406 ret = video_begin(fh);
1411 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1412 file->f_flags & O_NONBLOCK);
1414 video_end(fh, file);
1416 ret = video_end(fh, file);
1419 /* restart overlay if it was active before */
1420 if (vv->ov_suspend != NULL) {
1421 saa7146_start_preview(vv->ov_suspend);
1422 vv->ov_suspend = NULL;
1428 struct saa7146_use_ops saa7146_video_uops = {
1431 .release = video_close,
1432 .irq_done = video_irq_done,