1 #include <media/saa7146_vv.h>
2 #include <media/v4l2-chip-ident.h>
3 #include <linux/module.h>
5 static int max_memory = 32;
7 module_param(max_memory, int, 0644);
8 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
10 #define IS_CAPTURE_ACTIVE(fh) \
11 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
13 #define IS_OVERLAY_ACTIVE(fh) \
14 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
16 /* format descriptions for capture and preview */
17 static struct saa7146_format formats[] = {
19 .name = "RGB-8 (3-3-2)",
20 .pixelformat = V4L2_PIX_FMT_RGB332,
21 .trans = RGB08_COMPOSED,
25 .name = "RGB-16 (5/B-6/G-5/R)",
26 .pixelformat = V4L2_PIX_FMT_RGB565,
27 .trans = RGB16_COMPOSED,
31 .name = "RGB-24 (B-G-R)",
32 .pixelformat = V4L2_PIX_FMT_BGR24,
33 .trans = RGB24_COMPOSED,
37 .name = "RGB-32 (B-G-R)",
38 .pixelformat = V4L2_PIX_FMT_BGR32,
39 .trans = RGB32_COMPOSED,
43 .name = "RGB-32 (R-G-B)",
44 .pixelformat = V4L2_PIX_FMT_RGB32,
45 .trans = RGB32_COMPOSED,
50 .name = "Greyscale-8",
51 .pixelformat = V4L2_PIX_FMT_GREY,
56 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
57 .pixelformat = V4L2_PIX_FMT_YUV422P,
58 .trans = YUV422_DECOMPOSED,
60 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
62 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
63 .pixelformat = V4L2_PIX_FMT_YVU420,
64 .trans = YUV420_DECOMPOSED,
66 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
68 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
69 .pixelformat = V4L2_PIX_FMT_YUV420,
70 .trans = YUV420_DECOMPOSED,
72 .flags = FORMAT_IS_PLANAR,
74 .name = "YUV 4:2:2 (U-Y-V-Y)",
75 .pixelformat = V4L2_PIX_FMT_UYVY,
76 .trans = YUV422_COMPOSED,
82 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
83 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
84 (like V4L2_PIX_FMT_YUYV) ... 8-( */
86 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
88 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
90 int i, j = NUM_FORMATS;
92 for (i = 0; i < j; i++) {
93 if (formats[i].pixelformat == fourcc) {
98 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
102 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
104 int saa7146_start_preview(struct saa7146_fh *fh)
106 struct saa7146_dev *dev = fh->dev;
107 struct saa7146_vv *vv = dev->vv_data;
108 struct v4l2_format fmt;
109 int ret = 0, err = 0;
111 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
113 /* check if we have overlay informations */
114 if( NULL == fh->ov.fh ) {
115 DEB_D(("no overlay data available. try S_FMT first.\n"));
119 /* check if streaming capture is running */
120 if (IS_CAPTURE_ACTIVE(fh) != 0) {
121 DEB_D(("streaming capture is active.\n"));
125 /* check if overlay is running */
126 if (IS_OVERLAY_ACTIVE(fh) != 0) {
127 if (vv->video_fh == fh) {
128 DEB_D(("overlay is already active.\n"));
131 DEB_D(("overlay is already active in another open.\n"));
135 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
136 DEB_D(("cannot get necessary overlay resources\n"));
140 fmt.fmt.win = fh->ov.win;
141 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
143 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
146 fh->ov.win = fmt.fmt.win;
147 vv->ov_data = &fh->ov;
149 DEB_D(("%dx%d+%d+%d %s field=%s\n",
150 fh->ov.win.w.width,fh->ov.win.w.height,
151 fh->ov.win.w.left,fh->ov.win.w.top,
152 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
154 if (0 != (ret = saa7146_enable_overlay(fh))) {
155 DEB_D(("enabling overlay failed: %d\n",ret));
156 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
160 vv->video_status = STATUS_OVERLAY;
165 EXPORT_SYMBOL_GPL(saa7146_start_preview);
167 int saa7146_stop_preview(struct saa7146_fh *fh)
169 struct saa7146_dev *dev = fh->dev;
170 struct saa7146_vv *vv = dev->vv_data;
172 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
174 /* check if streaming capture is running */
175 if (IS_CAPTURE_ACTIVE(fh) != 0) {
176 DEB_D(("streaming capture is active.\n"));
180 /* check if overlay is running at all */
181 if ((vv->video_status & STATUS_OVERLAY) == 0) {
182 DEB_D(("no active overlay.\n"));
186 if (vv->video_fh != fh) {
187 DEB_D(("overlay is active, but in another open.\n"));
191 vv->video_status = 0;
194 saa7146_disable_overlay(fh);
196 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
200 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
202 /********************************************************************************/
203 /* device controls */
205 static struct v4l2_queryctrl controls[] = {
207 .id = V4L2_CID_BRIGHTNESS,
208 .name = "Brightness",
212 .default_value = 128,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .flags = V4L2_CTRL_FLAG_SLIDER,
216 .id = V4L2_CID_CONTRAST,
222 .type = V4L2_CTRL_TYPE_INTEGER,
223 .flags = V4L2_CTRL_FLAG_SLIDER,
225 .id = V4L2_CID_SATURATION,
226 .name = "Saturation",
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 .flags = V4L2_CTRL_FLAG_SLIDER,
234 .id = V4L2_CID_VFLIP,
235 .name = "Vertical Flip",
238 .type = V4L2_CTRL_TYPE_BOOLEAN,
240 .id = V4L2_CID_HFLIP,
241 .name = "Horizontal Flip",
244 .type = V4L2_CTRL_TYPE_BOOLEAN,
247 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
249 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
251 static struct v4l2_queryctrl* ctrl_by_id(int id)
255 for (i = 0; i < NUM_CONTROLS; i++)
256 if (controls[i].id == id)
261 /********************************************************************************/
262 /* common pagetable functions */
264 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
266 struct pci_dev *pci = dev->pci;
267 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
268 struct scatterlist *list = dma->sglist;
269 int length = dma->sglen;
270 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
272 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
274 if( 0 != IS_PLANAR(sfmt->trans)) {
275 struct saa7146_pgtable *pt1 = &buf->pt[0];
276 struct saa7146_pgtable *pt2 = &buf->pt[1];
277 struct saa7146_pgtable *pt3 = &buf->pt[2];
278 __le32 *ptr1, *ptr2, *ptr3;
281 int size = buf->fmt->width*buf->fmt->height;
282 int i,p,m1,m2,m3,o1,o2;
284 switch( sfmt->depth ) {
286 /* create some offsets inside the page table */
287 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
288 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
289 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
291 o2 = (size+(size/4))%PAGE_SIZE;
292 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
296 /* create some offsets inside the page table */
297 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
298 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
299 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
301 o2 = (size+(size/2))%PAGE_SIZE;
302 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
314 /* walk all pages, copy all page addresses to ptr1 */
315 for (i = 0; i < length; i++, list++) {
316 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
317 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
323 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
327 /* if we have a user buffer, the first page may not be
328 aligned to a page boundary. */
329 pt1->offset = dma->sglist->offset;
330 pt2->offset = pt1->offset+o1;
331 pt3->offset = pt1->offset+o2;
333 /* create video-dma2 page table */
335 for(i = m1; i <= m2 ; i++, ptr2++) {
339 for(;i<1024;i++,ptr2++) {
342 /* create video-dma3 page table */
344 for(i = m2; i <= m3; i++,ptr3++) {
348 for(;i<1024;i++,ptr3++) {
351 /* finally: finish up video-dma1 page table */
354 for(i=m1;i<1024;i++,ptr1++) {
362 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
365 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
368 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
372 struct saa7146_pgtable *pt = &buf->pt[0];
373 return saa7146_pgtable_build_single(pci, pt, list, length);
380 /********************************************************************************/
381 /* file operations */
383 static int video_begin(struct saa7146_fh *fh)
385 struct saa7146_dev *dev = fh->dev;
386 struct saa7146_vv *vv = dev->vv_data;
387 struct saa7146_format *fmt = NULL;
388 unsigned int resource;
389 int ret = 0, err = 0;
391 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
393 if ((vv->video_status & STATUS_CAPTURE) != 0) {
394 if (vv->video_fh == fh) {
395 DEB_S(("already capturing.\n"));
398 DEB_S(("already capturing in another open.\n"));
402 if ((vv->video_status & STATUS_OVERLAY) != 0) {
403 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
404 vv->ov_suspend = vv->video_fh;
405 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
407 DEB_D(("suspending video failed. aborting\n"));
412 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
413 /* we need to have a valid format set here */
416 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
417 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
419 resource = RESOURCE_DMA1_HPS;
422 ret = saa7146_res_get(fh, resource);
424 DEB_S(("cannot get capture resource %d\n",resource));
425 if (vv->ov_suspend != NULL) {
426 saa7146_start_preview(vv->ov_suspend);
427 vv->ov_suspend = NULL;
432 /* clear out beginning of streaming bit (rps register 0)*/
433 saa7146_write(dev, MC2, MASK_27 );
435 /* enable rps0 irqs */
436 SAA7146_IER_ENABLE(dev, MASK_27);
439 vv->video_status = STATUS_CAPTURE;
444 static int video_end(struct saa7146_fh *fh, struct file *file)
446 struct saa7146_dev *dev = fh->dev;
447 struct saa7146_vv *vv = dev->vv_data;
448 struct saa7146_format *fmt = NULL;
450 unsigned int resource;
452 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
454 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
455 DEB_S(("not capturing.\n"));
459 if (vv->video_fh != fh) {
460 DEB_S(("capturing, but in another open.\n"));
464 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
465 /* we need to have a valid format set here */
468 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
469 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
470 dmas = MASK_22 | MASK_21 | MASK_20;
472 resource = RESOURCE_DMA1_HPS;
475 spin_lock_irqsave(&dev->slock,flags);
478 saa7146_write(dev, MC1, MASK_28);
480 /* disable rps0 irqs */
481 SAA7146_IER_DISABLE(dev, MASK_27);
483 /* shut down all used video dma transfers */
484 saa7146_write(dev, MC1, dmas);
486 spin_unlock_irqrestore(&dev->slock, flags);
489 vv->video_status = 0;
491 saa7146_res_free(fh, resource);
493 if (vv->ov_suspend != NULL) {
494 saa7146_start_preview(vv->ov_suspend);
495 vv->ov_suspend = NULL;
501 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
503 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
505 strcpy((char *)cap->driver, "saa7146 v4l2");
506 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
507 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
508 cap->version = SAA7146_VERSION_CODE;
510 V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_VIDEO_OVERLAY |
514 cap->capabilities |= dev->ext_vv_data->capabilities;
518 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
520 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
521 struct saa7146_vv *vv = dev->vv_data;
524 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
528 static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
530 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
531 struct saa7146_vv *vv = dev->vv_data;
532 struct saa7146_format *fmt;
534 DEB_EE(("VIDIOC_S_FBUF\n"));
536 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
540 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
544 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
545 if (fmt->flags & FORMAT_IS_PLANAR)
546 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",
547 (char *)&fmt->pixelformat));
549 /* check if overlay is running */
550 if (IS_OVERLAY_ACTIVE(fh) != 0) {
551 if (vv->video_fh != fh) {
552 DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
561 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
562 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
563 DEB_D(("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline));
568 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
570 if (f->index >= NUM_FORMATS)
572 strlcpy((char *)f->description, formats[f->index].name,
573 sizeof(f->description));
574 f->pixelformat = formats[f->index].pixelformat;
578 static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
580 const struct v4l2_queryctrl *ctrl;
582 if ((c->id < V4L2_CID_BASE ||
583 c->id >= V4L2_CID_LASTP1) &&
584 (c->id < V4L2_CID_PRIVATE_BASE ||
585 c->id >= V4L2_CID_PRIVATE_LASTP1))
588 ctrl = ctrl_by_id(c->id);
592 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n", c->id));
597 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
599 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
600 struct saa7146_vv *vv = dev->vv_data;
601 const struct v4l2_queryctrl *ctrl;
604 ctrl = ctrl_by_id(c->id);
608 case V4L2_CID_BRIGHTNESS:
609 value = saa7146_read(dev, BCS_CTRL);
610 c->value = 0xff & (value >> 24);
611 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n", c->value));
613 case V4L2_CID_CONTRAST:
614 value = saa7146_read(dev, BCS_CTRL);
615 c->value = 0x7f & (value >> 16);
616 DEB_D(("V4L2_CID_CONTRAST: %d\n", c->value));
618 case V4L2_CID_SATURATION:
619 value = saa7146_read(dev, BCS_CTRL);
620 c->value = 0x7f & (value >> 0);
621 DEB_D(("V4L2_CID_SATURATION: %d\n", c->value));
624 c->value = vv->vflip;
625 DEB_D(("V4L2_CID_VFLIP: %d\n", c->value));
628 c->value = vv->hflip;
629 DEB_D(("V4L2_CID_HFLIP: %d\n", c->value));
637 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
639 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
640 struct saa7146_vv *vv = dev->vv_data;
641 const struct v4l2_queryctrl *ctrl;
643 ctrl = ctrl_by_id(c->id);
645 DEB_D(("unknown control %d\n", c->id));
649 switch (ctrl->type) {
650 case V4L2_CTRL_TYPE_BOOLEAN:
651 case V4L2_CTRL_TYPE_MENU:
652 case V4L2_CTRL_TYPE_INTEGER:
653 if (c->value < ctrl->minimum)
654 c->value = ctrl->minimum;
655 if (c->value > ctrl->maximum)
656 c->value = ctrl->maximum;
663 case V4L2_CID_BRIGHTNESS: {
664 u32 value = saa7146_read(dev, BCS_CTRL);
666 value |= (c->value << 24);
667 saa7146_write(dev, BCS_CTRL, value);
668 saa7146_write(dev, MC2, MASK_22 | MASK_06);
671 case V4L2_CID_CONTRAST: {
672 u32 value = saa7146_read(dev, BCS_CTRL);
674 value |= (c->value << 16);
675 saa7146_write(dev, BCS_CTRL, value);
676 saa7146_write(dev, MC2, MASK_22 | MASK_06);
679 case V4L2_CID_SATURATION: {
680 u32 value = saa7146_read(dev, BCS_CTRL);
682 value |= (c->value << 0);
683 saa7146_write(dev, BCS_CTRL, value);
684 saa7146_write(dev, MC2, MASK_22 | MASK_06);
688 /* fixme: we can support changing VFLIP and HFLIP here... */
689 if (IS_CAPTURE_ACTIVE(fh) != 0) {
690 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
693 vv->hflip = c->value;
696 if (IS_CAPTURE_ACTIVE(fh) != 0) {
697 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
700 vv->vflip = c->value;
706 if (IS_OVERLAY_ACTIVE(fh) != 0) {
707 saa7146_stop_preview(fh);
708 saa7146_start_preview(fh);
713 static int vidioc_g_parm(struct file *file, void *fh,
714 struct v4l2_streamparm *parm)
716 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
717 struct saa7146_vv *vv = dev->vv_data;
719 parm->parm.capture.readbuffers = 1;
720 v4l2_video_std_frame_period(vv->standard->id,
721 &parm->parm.capture.timeperframe);
725 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
727 f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
731 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
733 f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
737 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
739 f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
743 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
745 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
746 struct saa7146_vv *vv = dev->vv_data;
747 struct saa7146_format *fmt;
748 enum v4l2_field field;
752 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
754 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
758 field = f->fmt.pix.field;
759 maxw = vv->standard->h_max_out;
760 maxh = vv->standard->v_max_out;
762 if (V4L2_FIELD_ANY == field) {
763 field = (f->fmt.pix.height > maxh / 2)
764 ? V4L2_FIELD_INTERLACED
768 case V4L2_FIELD_ALTERNATE:
769 vv->last_field = V4L2_FIELD_TOP;
773 case V4L2_FIELD_BOTTOM:
774 vv->last_field = V4L2_FIELD_INTERLACED;
777 case V4L2_FIELD_INTERLACED:
778 vv->last_field = V4L2_FIELD_INTERLACED;
781 DEB_D(("no known field mode '%d'.\n", field));
785 f->fmt.pix.field = field;
786 if (f->fmt.pix.width > maxw)
787 f->fmt.pix.width = maxw;
788 if (f->fmt.pix.height > maxh)
789 f->fmt.pix.height = maxh;
791 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
793 if (f->fmt.pix.bytesperline < calc_bpl)
794 f->fmt.pix.bytesperline = calc_bpl;
796 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
797 f->fmt.pix.bytesperline = calc_bpl;
799 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
800 DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n", f->fmt.pix.width,
801 f->fmt.pix.height, f->fmt.pix.bytesperline, f->fmt.pix.sizeimage));
807 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
809 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
810 struct saa7146_vv *vv = dev->vv_data;
811 struct v4l2_window *win = &f->fmt.win;
812 enum v4l2_field field;
815 DEB_EE(("dev:%p\n", dev));
817 if (NULL == vv->ov_fb.base) {
818 DEB_D(("no fb base set.\n"));
821 if (NULL == vv->ov_fmt) {
822 DEB_D(("no fb fmt set.\n"));
825 if (win->w.width < 48 || win->w.height < 32) {
826 DEB_D(("min width/height. (%d,%d)\n", win->w.width, win->w.height));
829 if (win->clipcount > 16) {
830 DEB_D(("clipcount too big.\n"));
835 maxw = vv->standard->h_max_out;
836 maxh = vv->standard->v_max_out;
838 if (V4L2_FIELD_ANY == field) {
839 field = (win->w.height > maxh / 2)
840 ? V4L2_FIELD_INTERLACED
845 case V4L2_FIELD_BOTTOM:
846 case V4L2_FIELD_ALTERNATE:
849 case V4L2_FIELD_INTERLACED:
852 DEB_D(("no known field mode '%d'.\n", field));
857 if (win->w.width > maxw)
859 if (win->w.height > maxh)
860 win->w.height = maxh;
865 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
867 struct saa7146_fh *fh = __fh;
868 struct saa7146_dev *dev = fh->dev;
869 struct saa7146_vv *vv = dev->vv_data;
872 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
873 if (IS_CAPTURE_ACTIVE(fh) != 0) {
874 DEB_EE(("streaming capture is active\n"));
877 err = vidioc_try_fmt_vid_cap(file, fh, f);
880 fh->video_fmt = f->fmt.pix;
881 DEB_EE(("set to pixelformat '%4.4s'\n", (char *)&fh->video_fmt.pixelformat));
885 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
887 struct saa7146_fh *fh = __fh;
888 struct saa7146_dev *dev = fh->dev;
889 struct saa7146_vv *vv = dev->vv_data;
892 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh));
893 err = vidioc_try_fmt_vid_overlay(file, fh, f);
896 fh->ov.win = f->fmt.win;
897 fh->ov.nclips = f->fmt.win.clipcount;
898 if (fh->ov.nclips > 16)
900 if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
901 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
905 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
908 /* check if our current overlay is active */
909 if (IS_OVERLAY_ACTIVE(fh) != 0) {
910 saa7146_stop_preview(fh);
911 saa7146_start_preview(fh);
916 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
918 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
919 struct saa7146_vv *vv = dev->vv_data;
921 *norm = vv->standard->id;
925 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
926 PAL / NTSC / SECAM. if your hardware does not (or does more)
927 -- override this function in your extension */
931 struct v4l2_standard *e = arg;
934 if( e->index < dev->ext_vv_data->num_stds ) {
935 DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
936 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
943 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
945 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
946 struct saa7146_vv *vv = dev->vv_data;
950 DEB_EE(("VIDIOC_S_STD\n"));
952 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
953 DEB_D(("cannot change video standard while streaming capture is active\n"));
957 if ((vv->video_status & STATUS_OVERLAY) != 0) {
958 vv->ov_suspend = vv->video_fh;
959 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
961 DEB_D(("suspending video failed. aborting\n"));
966 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
967 if (*id & dev->ext_vv_data->stds[i].id)
969 if (i != dev->ext_vv_data->num_stds) {
970 vv->standard = &dev->ext_vv_data->stds[i];
971 if (NULL != dev->ext_vv_data->std_callback)
972 dev->ext_vv_data->std_callback(dev, vv->standard);
976 if (vv->ov_suspend != NULL) {
977 saa7146_start_preview(vv->ov_suspend);
978 vv->ov_suspend = NULL;
982 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
986 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name));
990 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
994 DEB_D(("VIDIOC_OVERLAY on:%d\n", on));
996 err = saa7146_start_preview(fh);
998 err = saa7146_stop_preview(fh);
1002 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
1004 struct saa7146_fh *fh = __fh;
1006 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1007 return videobuf_reqbufs(&fh->video_q, b);
1008 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1009 return videobuf_reqbufs(&fh->vbi_q, b);
1013 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1015 struct saa7146_fh *fh = __fh;
1017 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1018 return videobuf_querybuf(&fh->video_q, buf);
1019 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1020 return videobuf_querybuf(&fh->vbi_q, buf);
1024 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1026 struct saa7146_fh *fh = __fh;
1028 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1029 return videobuf_qbuf(&fh->video_q, buf);
1030 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1031 return videobuf_qbuf(&fh->vbi_q, buf);
1035 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1037 struct saa7146_fh *fh = __fh;
1039 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
1041 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1042 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
1046 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
1048 struct saa7146_fh *fh = __fh;
1051 DEB_D(("VIDIOC_STREAMON, type:%d\n", type));
1053 err = video_begin(fh);
1056 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1057 return videobuf_streamon(&fh->video_q);
1058 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1059 return videobuf_streamon(&fh->vbi_q);
1063 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
1065 struct saa7146_fh *fh = __fh;
1066 struct saa7146_dev *dev = fh->dev;
1067 struct saa7146_vv *vv = dev->vv_data;
1070 DEB_D(("VIDIOC_STREAMOFF, type:%d\n", type));
1072 /* ugly: we need to copy some checks from video_end(),
1073 because videobuf_streamoff() relies on the capture running.
1074 check and fix this */
1075 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
1076 DEB_S(("not capturing.\n"));
1080 if (vv->video_fh != fh) {
1081 DEB_S(("capturing, but in another open.\n"));
1086 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 err = videobuf_streamoff(&fh->video_q);
1088 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1089 err = videobuf_streamoff(&fh->vbi_q);
1091 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1092 video_end(fh, file);
1094 err = video_end(fh, file);
1099 static int vidioc_g_chip_ident(struct file *file, void *__fh,
1100 struct v4l2_dbg_chip_ident *chip)
1102 struct saa7146_fh *fh = __fh;
1103 struct saa7146_dev *dev = fh->dev;
1105 chip->ident = V4L2_IDENT_NONE;
1107 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
1108 chip->ident = V4L2_IDENT_SAA7146;
1111 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1112 core, g_chip_ident, chip);
1115 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1116 .vidioc_querycap = vidioc_querycap,
1117 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1118 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
1119 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1120 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1121 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1122 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1123 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1124 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1125 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1126 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1128 .vidioc_overlay = vidioc_overlay,
1129 .vidioc_g_fbuf = vidioc_g_fbuf,
1130 .vidioc_s_fbuf = vidioc_s_fbuf,
1131 .vidioc_reqbufs = vidioc_reqbufs,
1132 .vidioc_querybuf = vidioc_querybuf,
1133 .vidioc_qbuf = vidioc_qbuf,
1134 .vidioc_dqbuf = vidioc_dqbuf,
1135 .vidioc_g_std = vidioc_g_std,
1136 .vidioc_s_std = vidioc_s_std,
1137 .vidioc_queryctrl = vidioc_queryctrl,
1138 .vidioc_g_ctrl = vidioc_g_ctrl,
1139 .vidioc_s_ctrl = vidioc_s_ctrl,
1140 .vidioc_streamon = vidioc_streamon,
1141 .vidioc_streamoff = vidioc_streamoff,
1142 .vidioc_g_parm = vidioc_g_parm,
1145 /*********************************************************************************/
1146 /* buffer handling functions */
1148 static int buffer_activate (struct saa7146_dev *dev,
1149 struct saa7146_buf *buf,
1150 struct saa7146_buf *next)
1152 struct saa7146_vv *vv = dev->vv_data;
1154 buf->vb.state = VIDEOBUF_ACTIVE;
1155 saa7146_set_capture(dev,buf,next);
1157 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1161 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1163 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1164 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1165 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1168 static int buffer_prepare(struct videobuf_queue *q,
1169 struct videobuf_buffer *vb, enum v4l2_field field)
1171 struct file *file = q->priv_data;
1172 struct saa7146_fh *fh = file->private_data;
1173 struct saa7146_dev *dev = fh->dev;
1174 struct saa7146_vv *vv = dev->vv_data;
1175 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1178 DEB_CAP(("vbuf:%p\n",vb));
1181 if (fh->video_fmt.width < 48 ||
1182 fh->video_fmt.height < 32 ||
1183 fh->video_fmt.width > vv->standard->h_max_out ||
1184 fh->video_fmt.height > vv->standard->v_max_out) {
1185 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
1189 size = fh->video_fmt.sizeimage;
1190 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1191 DEB_D(("size mismatch.\n"));
1195 DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1196 fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
1197 if (buf->vb.width != fh->video_fmt.width ||
1198 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1199 buf->vb.height != fh->video_fmt.height ||
1200 buf->vb.size != size ||
1201 buf->vb.field != field ||
1202 buf->vb.field != fh->video_fmt.field ||
1203 buf->fmt != &fh->video_fmt) {
1204 saa7146_dma_free(dev,q,buf);
1207 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1208 struct saa7146_format *sfmt;
1210 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1211 buf->vb.width = fh->video_fmt.width;
1212 buf->vb.height = fh->video_fmt.height;
1213 buf->vb.size = size;
1214 buf->vb.field = field;
1215 buf->fmt = &fh->video_fmt;
1216 buf->vb.field = fh->video_fmt.field;
1218 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
1220 release_all_pagetables(dev, buf);
1221 if( 0 != IS_PLANAR(sfmt->trans)) {
1222 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1223 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1224 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1226 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1229 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1232 err = saa7146_pgtable_build(dev,buf);
1236 buf->vb.state = VIDEOBUF_PREPARED;
1237 buf->activate = buffer_activate;
1242 DEB_D(("error out.\n"));
1243 saa7146_dma_free(dev,q,buf);
1248 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1250 struct file *file = q->priv_data;
1251 struct saa7146_fh *fh = file->private_data;
1253 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1254 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1256 *size = fh->video_fmt.sizeimage;
1258 /* check if we exceed the "max_memory" parameter */
1259 if( (*count * *size) > (max_memory*1048576) ) {
1260 *count = (max_memory*1048576) / *size;
1263 DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
1268 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1270 struct file *file = q->priv_data;
1271 struct saa7146_fh *fh = file->private_data;
1272 struct saa7146_dev *dev = fh->dev;
1273 struct saa7146_vv *vv = dev->vv_data;
1274 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1276 DEB_CAP(("vbuf:%p\n",vb));
1277 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1280 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1282 struct file *file = q->priv_data;
1283 struct saa7146_fh *fh = file->private_data;
1284 struct saa7146_dev *dev = fh->dev;
1285 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1287 DEB_CAP(("vbuf:%p\n",vb));
1289 saa7146_dma_free(dev,q,buf);
1291 release_all_pagetables(dev, buf);
1294 static struct videobuf_queue_ops video_qops = {
1295 .buf_setup = buffer_setup,
1296 .buf_prepare = buffer_prepare,
1297 .buf_queue = buffer_queue,
1298 .buf_release = buffer_release,
1301 /********************************************************************************/
1302 /* file operations */
1304 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1306 INIT_LIST_HEAD(&vv->video_q.queue);
1308 init_timer(&vv->video_q.timeout);
1309 vv->video_q.timeout.function = saa7146_buffer_timeout;
1310 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1311 vv->video_q.dev = dev;
1313 /* set some default values */
1314 vv->standard = &dev->ext_vv_data->stds[0];
1316 /* FIXME: what's this? */
1317 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1318 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1322 static int video_open(struct saa7146_dev *dev, struct file *file)
1324 struct saa7146_fh *fh = file->private_data;
1325 struct saa7146_format *sfmt;
1327 fh->video_fmt.width = 384;
1328 fh->video_fmt.height = 288;
1329 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1330 fh->video_fmt.bytesperline = 0;
1331 fh->video_fmt.field = V4L2_FIELD_ANY;
1332 sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
1333 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1335 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1336 &dev->pci->dev, &dev->slock,
1337 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1338 V4L2_FIELD_INTERLACED,
1339 sizeof(struct saa7146_buf),
1340 file, &dev->v4l2_lock);
1346 static void video_close(struct saa7146_dev *dev, struct file *file)
1348 struct saa7146_fh *fh = file->private_data;
1349 struct saa7146_vv *vv = dev->vv_data;
1350 struct videobuf_queue *q = &fh->video_q;
1353 if (IS_CAPTURE_ACTIVE(fh) != 0) {
1354 err = video_end(fh, file);
1355 } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
1356 err = saa7146_stop_preview(fh);
1361 /* hmm, why is this function declared void? */
1366 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1368 struct saa7146_vv *vv = dev->vv_data;
1369 struct saa7146_dmaqueue *q = &vv->video_q;
1371 spin_lock(&dev->slock);
1372 DEB_CAP(("called.\n"));
1374 /* only finish the buffer if we have one... */
1375 if( NULL != q->curr ) {
1376 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1378 saa7146_buffer_next(dev,q,0);
1380 spin_unlock(&dev->slock);
1383 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1385 struct saa7146_fh *fh = file->private_data;
1386 struct saa7146_dev *dev = fh->dev;
1387 struct saa7146_vv *vv = dev->vv_data;
1390 DEB_EE(("called.\n"));
1392 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1393 /* fixme: should we allow read() captures while streaming capture? */
1394 if (vv->video_fh == fh) {
1395 DEB_S(("already capturing.\n"));
1398 DEB_S(("already capturing in another open.\n"));
1402 ret = video_begin(fh);
1407 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1408 file->f_flags & O_NONBLOCK);
1410 video_end(fh, file);
1412 ret = video_end(fh, file);
1415 /* restart overlay if it was active before */
1416 if (vv->ov_suspend != NULL) {
1417 saa7146_start_preview(vv->ov_suspend);
1418 vv->ov_suspend = NULL;
1424 struct saa7146_use_ops saa7146_video_uops = {
1427 .release = video_close,
1428 .irq_done = video_irq_done,