1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <media/saa7146_vv.h>
4 #include <media/v4l2-chip-ident.h>
5 #include <linux/module.h>
7 static int max_memory
= 32;
9 module_param(max_memory
, int, 0644);
10 MODULE_PARM_DESC(max_memory
, "maximum memory usage for capture buffers (default: 32Mb)");
12 #define IS_CAPTURE_ACTIVE(fh) \
13 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
15 #define IS_OVERLAY_ACTIVE(fh) \
16 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
18 /* format descriptions for capture and preview */
19 static struct saa7146_format formats
[] = {
21 .name
= "RGB-8 (3-3-2)",
22 .pixelformat
= V4L2_PIX_FMT_RGB332
,
23 .trans
= RGB08_COMPOSED
,
27 .name
= "RGB-16 (5/B-6/G-5/R)",
28 .pixelformat
= V4L2_PIX_FMT_RGB565
,
29 .trans
= RGB16_COMPOSED
,
33 .name
= "RGB-24 (B-G-R)",
34 .pixelformat
= V4L2_PIX_FMT_BGR24
,
35 .trans
= RGB24_COMPOSED
,
39 .name
= "RGB-32 (B-G-R)",
40 .pixelformat
= V4L2_PIX_FMT_BGR32
,
41 .trans
= RGB32_COMPOSED
,
45 .name
= "RGB-32 (R-G-B)",
46 .pixelformat
= V4L2_PIX_FMT_RGB32
,
47 .trans
= RGB32_COMPOSED
,
52 .name
= "Greyscale-8",
53 .pixelformat
= V4L2_PIX_FMT_GREY
,
58 .name
= "YUV 4:2:2 planar (Y-Cb-Cr)",
59 .pixelformat
= V4L2_PIX_FMT_YUV422P
,
60 .trans
= YUV422_DECOMPOSED
,
62 .flags
= FORMAT_BYTE_SWAP
|FORMAT_IS_PLANAR
,
64 .name
= "YVU 4:2:0 planar (Y-Cb-Cr)",
65 .pixelformat
= V4L2_PIX_FMT_YVU420
,
66 .trans
= YUV420_DECOMPOSED
,
68 .flags
= FORMAT_BYTE_SWAP
|FORMAT_IS_PLANAR
,
70 .name
= "YUV 4:2:0 planar (Y-Cb-Cr)",
71 .pixelformat
= V4L2_PIX_FMT_YUV420
,
72 .trans
= YUV420_DECOMPOSED
,
74 .flags
= FORMAT_IS_PLANAR
,
76 .name
= "YUV 4:2:2 (U-Y-V-Y)",
77 .pixelformat
= V4L2_PIX_FMT_UYVY
,
78 .trans
= YUV422_COMPOSED
,
84 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
85 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
86 (like V4L2_PIX_FMT_YUYV) ... 8-( */
88 static int NUM_FORMATS
= sizeof(formats
)/sizeof(struct saa7146_format
);
90 struct saa7146_format
* saa7146_format_by_fourcc(struct saa7146_dev
*dev
, int fourcc
)
92 int i
, j
= NUM_FORMATS
;
94 for (i
= 0; i
< j
; i
++) {
95 if (formats
[i
].pixelformat
== fourcc
) {
100 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc
);
104 static int vidioc_try_fmt_vid_overlay(struct file
*file
, void *fh
, struct v4l2_format
*f
);
106 int saa7146_start_preview(struct saa7146_fh
*fh
)
108 struct saa7146_dev
*dev
= fh
->dev
;
109 struct saa7146_vv
*vv
= dev
->vv_data
;
110 struct v4l2_format fmt
;
111 int ret
= 0, err
= 0;
113 DEB_EE("dev:%p, fh:%p\n", dev
, fh
);
115 /* check if we have overlay informations */
116 if( NULL
== fh
->ov
.fh
) {
117 DEB_D("no overlay data available. try S_FMT first.\n");
121 /* check if streaming capture is running */
122 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
123 DEB_D("streaming capture is active\n");
127 /* check if overlay is running */
128 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
129 if (vv
->video_fh
== fh
) {
130 DEB_D("overlay is already active\n");
133 DEB_D("overlay is already active in another open\n");
137 if (0 == saa7146_res_get(fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
)) {
138 DEB_D("cannot get necessary overlay resources\n");
142 fmt
.fmt
.win
= fh
->ov
.win
;
143 err
= vidioc_try_fmt_vid_overlay(NULL
, fh
, &fmt
);
145 saa7146_res_free(vv
->video_fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
148 fh
->ov
.win
= fmt
.fmt
.win
;
149 vv
->ov_data
= &fh
->ov
;
151 DEB_D("%dx%d+%d+%d %s field=%s\n",
152 fh
->ov
.win
.w
.width
, fh
->ov
.win
.w
.height
,
153 fh
->ov
.win
.w
.left
, fh
->ov
.win
.w
.top
,
154 vv
->ov_fmt
->name
, v4l2_field_names
[fh
->ov
.win
.field
]);
156 if (0 != (ret
= saa7146_enable_overlay(fh
))) {
157 DEB_D("enabling overlay failed: %d\n", ret
);
158 saa7146_res_free(vv
->video_fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
162 vv
->video_status
= STATUS_OVERLAY
;
167 EXPORT_SYMBOL_GPL(saa7146_start_preview
);
169 int saa7146_stop_preview(struct saa7146_fh
*fh
)
171 struct saa7146_dev
*dev
= fh
->dev
;
172 struct saa7146_vv
*vv
= dev
->vv_data
;
174 DEB_EE("dev:%p, fh:%p\n", dev
, fh
);
176 /* check if streaming capture is running */
177 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
178 DEB_D("streaming capture is active\n");
182 /* check if overlay is running at all */
183 if ((vv
->video_status
& STATUS_OVERLAY
) == 0) {
184 DEB_D("no active overlay\n");
188 if (vv
->video_fh
!= fh
) {
189 DEB_D("overlay is active, but in another open\n");
193 vv
->video_status
= 0;
196 saa7146_disable_overlay(fh
);
198 saa7146_res_free(fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
202 EXPORT_SYMBOL_GPL(saa7146_stop_preview
);
204 /********************************************************************************/
205 /* common pagetable functions */
207 static int saa7146_pgtable_build(struct saa7146_dev
*dev
, struct saa7146_buf
*buf
)
209 struct pci_dev
*pci
= dev
->pci
;
210 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
211 struct scatterlist
*list
= dma
->sglist
;
212 int length
= dma
->sglen
;
213 struct saa7146_format
*sfmt
= saa7146_format_by_fourcc(dev
,buf
->fmt
->pixelformat
);
215 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev
, buf
, length
);
217 if( 0 != IS_PLANAR(sfmt
->trans
)) {
218 struct saa7146_pgtable
*pt1
= &buf
->pt
[0];
219 struct saa7146_pgtable
*pt2
= &buf
->pt
[1];
220 struct saa7146_pgtable
*pt3
= &buf
->pt
[2];
221 __le32
*ptr1
, *ptr2
, *ptr3
;
224 int size
= buf
->fmt
->width
*buf
->fmt
->height
;
225 int i
,p
,m1
,m2
,m3
,o1
,o2
;
227 switch( sfmt
->depth
) {
229 /* create some offsets inside the page table */
230 m1
= ((size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
231 m2
= ((size
+(size
/4)+PAGE_SIZE
)/PAGE_SIZE
)-1;
232 m3
= ((size
+(size
/2)+PAGE_SIZE
)/PAGE_SIZE
)-1;
234 o2
= (size
+(size
/4))%PAGE_SIZE
;
235 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
236 size
, m1
, m2
, m3
, o1
, o2
);
240 /* create some offsets inside the page table */
241 m1
= ((size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
242 m2
= ((size
+(size
/2)+PAGE_SIZE
)/PAGE_SIZE
)-1;
243 m3
= ((2*size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
245 o2
= (size
+(size
/2))%PAGE_SIZE
;
246 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
247 size
, m1
, m2
, m3
, o1
, o2
);
259 /* walk all pages, copy all page addresses to ptr1 */
260 for (i
= 0; i
< length
; i
++, list
++) {
261 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr1
++) {
262 *ptr1
= cpu_to_le32(sg_dma_address(list
) - list
->offset
);
268 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
272 /* if we have a user buffer, the first page may not be
273 aligned to a page boundary. */
274 pt1
->offset
= dma
->sglist
->offset
;
275 pt2
->offset
= pt1
->offset
+o1
;
276 pt3
->offset
= pt1
->offset
+o2
;
278 /* create video-dma2 page table */
280 for(i
= m1
; i
<= m2
; i
++, ptr2
++) {
284 for(;i
<1024;i
++,ptr2
++) {
287 /* create video-dma3 page table */
289 for(i
= m2
; i
<= m3
; i
++,ptr3
++) {
293 for(;i
<1024;i
++,ptr3
++) {
296 /* finally: finish up video-dma1 page table */
299 for(i
=m1
;i
<1024;i
++,ptr1
++) {
307 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
310 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
313 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
317 struct saa7146_pgtable
*pt
= &buf
->pt
[0];
318 return saa7146_pgtable_build_single(pci
, pt
, list
, length
);
325 /********************************************************************************/
326 /* file operations */
328 static int video_begin(struct saa7146_fh
*fh
)
330 struct saa7146_dev
*dev
= fh
->dev
;
331 struct saa7146_vv
*vv
= dev
->vv_data
;
332 struct saa7146_format
*fmt
= NULL
;
333 unsigned int resource
;
334 int ret
= 0, err
= 0;
336 DEB_EE("dev:%p, fh:%p\n", dev
, fh
);
338 if ((vv
->video_status
& STATUS_CAPTURE
) != 0) {
339 if (vv
->video_fh
== fh
) {
340 DEB_S("already capturing\n");
343 DEB_S("already capturing in another open\n");
347 if ((vv
->video_status
& STATUS_OVERLAY
) != 0) {
348 DEB_S("warning: suspending overlay video for streaming capture\n");
349 vv
->ov_suspend
= vv
->video_fh
;
350 err
= saa7146_stop_preview(vv
->video_fh
); /* side effect: video_status is now 0, video_fh is NULL */
352 DEB_D("suspending video failed. aborting\n");
357 fmt
= saa7146_format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
358 /* we need to have a valid format set here */
361 if (0 != (fmt
->flags
& FORMAT_IS_PLANAR
)) {
362 resource
= RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
|RESOURCE_DMA3_BRS
;
364 resource
= RESOURCE_DMA1_HPS
;
367 ret
= saa7146_res_get(fh
, resource
);
369 DEB_S("cannot get capture resource %d\n", resource
);
370 if (vv
->ov_suspend
!= NULL
) {
371 saa7146_start_preview(vv
->ov_suspend
);
372 vv
->ov_suspend
= NULL
;
377 /* clear out beginning of streaming bit (rps register 0)*/
378 saa7146_write(dev
, MC2
, MASK_27
);
380 /* enable rps0 irqs */
381 SAA7146_IER_ENABLE(dev
, MASK_27
);
384 vv
->video_status
= STATUS_CAPTURE
;
389 static int video_end(struct saa7146_fh
*fh
, struct file
*file
)
391 struct saa7146_dev
*dev
= fh
->dev
;
392 struct saa7146_vv
*vv
= dev
->vv_data
;
393 struct saa7146_format
*fmt
= NULL
;
395 unsigned int resource
;
397 DEB_EE("dev:%p, fh:%p\n", dev
, fh
);
399 if ((vv
->video_status
& STATUS_CAPTURE
) != STATUS_CAPTURE
) {
400 DEB_S("not capturing\n");
404 if (vv
->video_fh
!= fh
) {
405 DEB_S("capturing, but in another open\n");
409 fmt
= saa7146_format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
410 /* we need to have a valid format set here */
413 if (0 != (fmt
->flags
& FORMAT_IS_PLANAR
)) {
414 resource
= RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
|RESOURCE_DMA3_BRS
;
415 dmas
= MASK_22
| MASK_21
| MASK_20
;
417 resource
= RESOURCE_DMA1_HPS
;
420 spin_lock_irqsave(&dev
->slock
,flags
);
423 saa7146_write(dev
, MC1
, MASK_28
);
425 /* disable rps0 irqs */
426 SAA7146_IER_DISABLE(dev
, MASK_27
);
428 /* shut down all used video dma transfers */
429 saa7146_write(dev
, MC1
, dmas
);
431 spin_unlock_irqrestore(&dev
->slock
, flags
);
434 vv
->video_status
= 0;
436 saa7146_res_free(fh
, resource
);
438 if (vv
->ov_suspend
!= NULL
) {
439 saa7146_start_preview(vv
->ov_suspend
);
440 vv
->ov_suspend
= NULL
;
446 static int vidioc_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
448 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
450 strcpy((char *)cap
->driver
, "saa7146 v4l2");
451 strlcpy((char *)cap
->card
, dev
->ext
->name
, sizeof(cap
->card
));
452 sprintf((char *)cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
453 cap
->version
= SAA7146_VERSION_CODE
;
455 V4L2_CAP_VIDEO_CAPTURE
|
456 V4L2_CAP_VIDEO_OVERLAY
|
459 cap
->device_caps
|= dev
->ext_vv_data
->capabilities
;
460 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
464 static int vidioc_g_fbuf(struct file
*file
, void *fh
, struct v4l2_framebuffer
*fb
)
466 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
467 struct saa7146_vv
*vv
= dev
->vv_data
;
470 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
474 static int vidioc_s_fbuf(struct file
*file
, void *fh
, struct v4l2_framebuffer
*fb
)
476 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
477 struct saa7146_vv
*vv
= dev
->vv_data
;
478 struct saa7146_format
*fmt
;
480 DEB_EE("VIDIOC_S_FBUF\n");
482 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
486 fmt
= saa7146_format_by_fourcc(dev
, fb
->fmt
.pixelformat
);
490 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
491 if (fmt
->flags
& FORMAT_IS_PLANAR
)
492 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
493 (char *)&fmt
->pixelformat
);
495 /* check if overlay is running */
496 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
497 if (vv
->video_fh
!= fh
) {
498 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
507 if (vv
->ov_fb
.fmt
.bytesperline
< vv
->ov_fb
.fmt
.width
) {
508 vv
->ov_fb
.fmt
.bytesperline
= vv
->ov_fb
.fmt
.width
* fmt
->depth
/ 8;
509 DEB_D("setting bytesperline to %d\n", vv
->ov_fb
.fmt
.bytesperline
);
514 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_fmtdesc
*f
)
516 if (f
->index
>= NUM_FORMATS
)
518 strlcpy((char *)f
->description
, formats
[f
->index
].name
,
519 sizeof(f
->description
));
520 f
->pixelformat
= formats
[f
->index
].pixelformat
;
524 int saa7146_s_ctrl(struct v4l2_ctrl
*ctrl
)
526 struct saa7146_dev
*dev
= container_of(ctrl
->handler
,
527 struct saa7146_dev
, ctrl_handler
);
528 struct saa7146_vv
*vv
= dev
->vv_data
;
532 case V4L2_CID_BRIGHTNESS
:
533 val
= saa7146_read(dev
, BCS_CTRL
);
535 val
|= (ctrl
->val
<< 24);
536 saa7146_write(dev
, BCS_CTRL
, val
);
537 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
540 case V4L2_CID_CONTRAST
:
541 val
= saa7146_read(dev
, BCS_CTRL
);
543 val
|= (ctrl
->val
<< 16);
544 saa7146_write(dev
, BCS_CTRL
, val
);
545 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
548 case V4L2_CID_SATURATION
:
549 val
= saa7146_read(dev
, BCS_CTRL
);
551 val
|= (ctrl
->val
<< 0);
552 saa7146_write(dev
, BCS_CTRL
, val
);
553 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
557 /* fixme: we can support changing VFLIP and HFLIP here... */
558 if ((vv
->video_status
& STATUS_CAPTURE
))
560 vv
->hflip
= ctrl
->val
;
564 if ((vv
->video_status
& STATUS_CAPTURE
))
566 vv
->vflip
= ctrl
->val
;
573 if ((vv
->video_status
& STATUS_OVERLAY
) != 0) { /* CHECK: && (vv->video_fh == fh)) */
574 struct saa7146_fh
*fh
= vv
->video_fh
;
576 saa7146_stop_preview(fh
);
577 saa7146_start_preview(fh
);
582 static int vidioc_g_parm(struct file
*file
, void *fh
,
583 struct v4l2_streamparm
*parm
)
585 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
586 struct saa7146_vv
*vv
= dev
->vv_data
;
588 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
590 parm
->parm
.capture
.readbuffers
= 1;
591 v4l2_video_std_frame_period(vv
->standard
->id
,
592 &parm
->parm
.capture
.timeperframe
);
596 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
598 f
->fmt
.pix
= ((struct saa7146_fh
*)fh
)->video_fmt
;
602 static int vidioc_g_fmt_vid_overlay(struct file
*file
, void *fh
, struct v4l2_format
*f
)
604 f
->fmt
.win
= ((struct saa7146_fh
*)fh
)->ov
.win
;
608 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
610 f
->fmt
.vbi
= ((struct saa7146_fh
*)fh
)->vbi_fmt
;
614 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
616 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
617 struct saa7146_vv
*vv
= dev
->vv_data
;
618 struct saa7146_format
*fmt
;
619 enum v4l2_field field
;
623 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev
, fh
);
625 fmt
= saa7146_format_by_fourcc(dev
, f
->fmt
.pix
.pixelformat
);
629 field
= f
->fmt
.pix
.field
;
630 maxw
= vv
->standard
->h_max_out
;
631 maxh
= vv
->standard
->v_max_out
;
633 if (V4L2_FIELD_ANY
== field
) {
634 field
= (f
->fmt
.pix
.height
> maxh
/ 2)
635 ? V4L2_FIELD_INTERLACED
639 case V4L2_FIELD_ALTERNATE
:
640 vv
->last_field
= V4L2_FIELD_TOP
;
644 case V4L2_FIELD_BOTTOM
:
645 vv
->last_field
= V4L2_FIELD_INTERLACED
;
648 case V4L2_FIELD_INTERLACED
:
649 vv
->last_field
= V4L2_FIELD_INTERLACED
;
652 DEB_D("no known field mode '%d'\n", field
);
656 f
->fmt
.pix
.field
= field
;
657 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
658 if (f
->fmt
.pix
.width
> maxw
)
659 f
->fmt
.pix
.width
= maxw
;
660 if (f
->fmt
.pix
.height
> maxh
)
661 f
->fmt
.pix
.height
= maxh
;
663 calc_bpl
= (f
->fmt
.pix
.width
* fmt
->depth
) / 8;
665 if (f
->fmt
.pix
.bytesperline
< calc_bpl
)
666 f
->fmt
.pix
.bytesperline
= calc_bpl
;
668 if (f
->fmt
.pix
.bytesperline
> (2 * PAGE_SIZE
* fmt
->depth
) / 8) /* arbitrary constraint */
669 f
->fmt
.pix
.bytesperline
= calc_bpl
;
671 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* f
->fmt
.pix
.height
;
672 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
673 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
,
674 f
->fmt
.pix
.bytesperline
, f
->fmt
.pix
.sizeimage
);
680 static int vidioc_try_fmt_vid_overlay(struct file
*file
, void *fh
, struct v4l2_format
*f
)
682 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
683 struct saa7146_vv
*vv
= dev
->vv_data
;
684 struct v4l2_window
*win
= &f
->fmt
.win
;
685 enum v4l2_field field
;
688 DEB_EE("dev:%p\n", dev
);
690 if (NULL
== vv
->ov_fb
.base
) {
691 DEB_D("no fb base set\n");
694 if (NULL
== vv
->ov_fmt
) {
695 DEB_D("no fb fmt set\n");
698 if (win
->w
.width
< 48 || win
->w
.height
< 32) {
699 DEB_D("min width/height. (%d,%d)\n",
700 win
->w
.width
, win
->w
.height
);
703 if (win
->clipcount
> 16) {
704 DEB_D("clipcount too big\n");
709 maxw
= vv
->standard
->h_max_out
;
710 maxh
= vv
->standard
->v_max_out
;
712 if (V4L2_FIELD_ANY
== field
) {
713 field
= (win
->w
.height
> maxh
/ 2)
714 ? V4L2_FIELD_INTERLACED
719 case V4L2_FIELD_BOTTOM
:
720 case V4L2_FIELD_ALTERNATE
:
723 case V4L2_FIELD_INTERLACED
:
726 DEB_D("no known field mode '%d'\n", field
);
731 if (win
->w
.width
> maxw
)
733 if (win
->w
.height
> maxh
)
734 win
->w
.height
= maxh
;
739 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *__fh
, struct v4l2_format
*f
)
741 struct saa7146_fh
*fh
= __fh
;
742 struct saa7146_dev
*dev
= fh
->dev
;
743 struct saa7146_vv
*vv
= dev
->vv_data
;
746 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev
, fh
);
747 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
748 DEB_EE("streaming capture is active\n");
751 err
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
754 fh
->video_fmt
= f
->fmt
.pix
;
755 DEB_EE("set to pixelformat '%4.4s'\n",
756 (char *)&fh
->video_fmt
.pixelformat
);
760 static int vidioc_s_fmt_vid_overlay(struct file
*file
, void *__fh
, struct v4l2_format
*f
)
762 struct saa7146_fh
*fh
= __fh
;
763 struct saa7146_dev
*dev
= fh
->dev
;
764 struct saa7146_vv
*vv
= dev
->vv_data
;
767 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev
, fh
);
768 err
= vidioc_try_fmt_vid_overlay(file
, fh
, f
);
771 fh
->ov
.win
= f
->fmt
.win
;
772 fh
->ov
.nclips
= f
->fmt
.win
.clipcount
;
773 if (fh
->ov
.nclips
> 16)
775 if (copy_from_user(fh
->ov
.clips
, f
->fmt
.win
.clips
,
776 sizeof(struct v4l2_clip
) * fh
->ov
.nclips
)) {
780 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
783 /* check if our current overlay is active */
784 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
785 saa7146_stop_preview(fh
);
786 saa7146_start_preview(fh
);
791 static int vidioc_g_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
793 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
794 struct saa7146_vv
*vv
= dev
->vv_data
;
796 *norm
= vv
->standard
->id
;
800 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
801 PAL / NTSC / SECAM. if your hardware does not (or does more)
802 -- override this function in your extension */
806 struct v4l2_standard *e = arg;
809 if( e->index < dev->ext_vv_data->num_stds ) {
810 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
811 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
818 static int vidioc_s_std(struct file
*file
, void *fh
, v4l2_std_id
*id
)
820 struct saa7146_dev
*dev
= ((struct saa7146_fh
*)fh
)->dev
;
821 struct saa7146_vv
*vv
= dev
->vv_data
;
825 DEB_EE("VIDIOC_S_STD\n");
827 if ((vv
->video_status
& STATUS_CAPTURE
) == STATUS_CAPTURE
) {
828 DEB_D("cannot change video standard while streaming capture is active\n");
832 if ((vv
->video_status
& STATUS_OVERLAY
) != 0) {
833 vv
->ov_suspend
= vv
->video_fh
;
834 err
= saa7146_stop_preview(vv
->video_fh
); /* side effect: video_status is now 0, video_fh is NULL */
836 DEB_D("suspending video failed. aborting\n");
841 for (i
= 0; i
< dev
->ext_vv_data
->num_stds
; i
++)
842 if (*id
& dev
->ext_vv_data
->stds
[i
].id
)
844 if (i
!= dev
->ext_vv_data
->num_stds
) {
845 vv
->standard
= &dev
->ext_vv_data
->stds
[i
];
846 if (NULL
!= dev
->ext_vv_data
->std_callback
)
847 dev
->ext_vv_data
->std_callback(dev
, vv
->standard
);
851 if (vv
->ov_suspend
!= NULL
) {
852 saa7146_start_preview(vv
->ov_suspend
);
853 vv
->ov_suspend
= NULL
;
857 DEB_EE("VIDIOC_S_STD: standard not found\n");
861 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv
->standard
->name
);
865 static int vidioc_overlay(struct file
*file
, void *fh
, unsigned int on
)
869 DEB_D("VIDIOC_OVERLAY on:%d\n", on
);
871 err
= saa7146_start_preview(fh
);
873 err
= saa7146_stop_preview(fh
);
877 static int vidioc_reqbufs(struct file
*file
, void *__fh
, struct v4l2_requestbuffers
*b
)
879 struct saa7146_fh
*fh
= __fh
;
881 if (b
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
882 return videobuf_reqbufs(&fh
->video_q
, b
);
883 if (b
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
884 return videobuf_reqbufs(&fh
->vbi_q
, b
);
888 static int vidioc_querybuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
890 struct saa7146_fh
*fh
= __fh
;
892 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
893 return videobuf_querybuf(&fh
->video_q
, buf
);
894 if (buf
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
895 return videobuf_querybuf(&fh
->vbi_q
, buf
);
899 static int vidioc_qbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
901 struct saa7146_fh
*fh
= __fh
;
903 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
904 return videobuf_qbuf(&fh
->video_q
, buf
);
905 if (buf
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
906 return videobuf_qbuf(&fh
->vbi_q
, buf
);
910 static int vidioc_dqbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
912 struct saa7146_fh
*fh
= __fh
;
914 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
915 return videobuf_dqbuf(&fh
->video_q
, buf
, file
->f_flags
& O_NONBLOCK
);
916 if (buf
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
917 return videobuf_dqbuf(&fh
->vbi_q
, buf
, file
->f_flags
& O_NONBLOCK
);
921 static int vidioc_streamon(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
923 struct saa7146_fh
*fh
= __fh
;
926 DEB_D("VIDIOC_STREAMON, type:%d\n", type
);
928 err
= video_begin(fh
);
931 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
932 return videobuf_streamon(&fh
->video_q
);
933 if (type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
934 return videobuf_streamon(&fh
->vbi_q
);
938 static int vidioc_streamoff(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
940 struct saa7146_fh
*fh
= __fh
;
941 struct saa7146_dev
*dev
= fh
->dev
;
942 struct saa7146_vv
*vv
= dev
->vv_data
;
945 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type
);
947 /* ugly: we need to copy some checks from video_end(),
948 because videobuf_streamoff() relies on the capture running.
949 check and fix this */
950 if ((vv
->video_status
& STATUS_CAPTURE
) != STATUS_CAPTURE
) {
951 DEB_S("not capturing\n");
955 if (vv
->video_fh
!= fh
) {
956 DEB_S("capturing, but in another open\n");
961 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
962 err
= videobuf_streamoff(&fh
->video_q
);
963 else if (type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
964 err
= videobuf_streamoff(&fh
->vbi_q
);
966 DEB_D("warning: videobuf_streamoff() failed\n");
969 err
= video_end(fh
, file
);
974 static int vidioc_g_chip_ident(struct file
*file
, void *__fh
,
975 struct v4l2_dbg_chip_ident
*chip
)
977 struct saa7146_fh
*fh
= __fh
;
978 struct saa7146_dev
*dev
= fh
->dev
;
980 chip
->ident
= V4L2_IDENT_NONE
;
982 if (chip
->match
.type
== V4L2_CHIP_MATCH_HOST
&& !chip
->match
.addr
) {
983 chip
->ident
= V4L2_IDENT_SAA7146
;
986 return v4l2_device_call_until_err(&dev
->v4l2_dev
, 0,
987 core
, g_chip_ident
, chip
);
990 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops
= {
991 .vidioc_querycap
= vidioc_querycap
,
992 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
993 .vidioc_enum_fmt_vid_overlay
= vidioc_enum_fmt_vid_cap
,
994 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
995 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
996 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
997 .vidioc_g_fmt_vid_overlay
= vidioc_g_fmt_vid_overlay
,
998 .vidioc_try_fmt_vid_overlay
= vidioc_try_fmt_vid_overlay
,
999 .vidioc_s_fmt_vid_overlay
= vidioc_s_fmt_vid_overlay
,
1000 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1001 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1003 .vidioc_overlay
= vidioc_overlay
,
1004 .vidioc_g_fbuf
= vidioc_g_fbuf
,
1005 .vidioc_s_fbuf
= vidioc_s_fbuf
,
1006 .vidioc_reqbufs
= vidioc_reqbufs
,
1007 .vidioc_querybuf
= vidioc_querybuf
,
1008 .vidioc_qbuf
= vidioc_qbuf
,
1009 .vidioc_dqbuf
= vidioc_dqbuf
,
1010 .vidioc_g_std
= vidioc_g_std
,
1011 .vidioc_s_std
= vidioc_s_std
,
1012 .vidioc_streamon
= vidioc_streamon
,
1013 .vidioc_streamoff
= vidioc_streamoff
,
1014 .vidioc_g_parm
= vidioc_g_parm
,
1017 /*********************************************************************************/
1018 /* buffer handling functions */
1020 static int buffer_activate (struct saa7146_dev
*dev
,
1021 struct saa7146_buf
*buf
,
1022 struct saa7146_buf
*next
)
1024 struct saa7146_vv
*vv
= dev
->vv_data
;
1026 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
1027 saa7146_set_capture(dev
,buf
,next
);
1029 mod_timer(&vv
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1033 static void release_all_pagetables(struct saa7146_dev
*dev
, struct saa7146_buf
*buf
)
1035 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[0]);
1036 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[1]);
1037 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[2]);
1040 static int buffer_prepare(struct videobuf_queue
*q
,
1041 struct videobuf_buffer
*vb
, enum v4l2_field field
)
1043 struct file
*file
= q
->priv_data
;
1044 struct saa7146_fh
*fh
= file
->private_data
;
1045 struct saa7146_dev
*dev
= fh
->dev
;
1046 struct saa7146_vv
*vv
= dev
->vv_data
;
1047 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1050 DEB_CAP("vbuf:%p\n", vb
);
1053 if (fh
->video_fmt
.width
< 48 ||
1054 fh
->video_fmt
.height
< 32 ||
1055 fh
->video_fmt
.width
> vv
->standard
->h_max_out
||
1056 fh
->video_fmt
.height
> vv
->standard
->v_max_out
) {
1057 DEB_D("w (%d) / h (%d) out of bounds\n",
1058 fh
->video_fmt
.width
, fh
->video_fmt
.height
);
1062 size
= fh
->video_fmt
.sizeimage
;
1063 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
) {
1064 DEB_D("size mismatch\n");
1068 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1069 fh
->video_fmt
.width
, fh
->video_fmt
.height
,
1070 size
, v4l2_field_names
[fh
->video_fmt
.field
]);
1071 if (buf
->vb
.width
!= fh
->video_fmt
.width
||
1072 buf
->vb
.bytesperline
!= fh
->video_fmt
.bytesperline
||
1073 buf
->vb
.height
!= fh
->video_fmt
.height
||
1074 buf
->vb
.size
!= size
||
1075 buf
->vb
.field
!= field
||
1076 buf
->vb
.field
!= fh
->video_fmt
.field
||
1077 buf
->fmt
!= &fh
->video_fmt
) {
1078 saa7146_dma_free(dev
,q
,buf
);
1081 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1082 struct saa7146_format
*sfmt
;
1084 buf
->vb
.bytesperline
= fh
->video_fmt
.bytesperline
;
1085 buf
->vb
.width
= fh
->video_fmt
.width
;
1086 buf
->vb
.height
= fh
->video_fmt
.height
;
1087 buf
->vb
.size
= size
;
1088 buf
->vb
.field
= field
;
1089 buf
->fmt
= &fh
->video_fmt
;
1090 buf
->vb
.field
= fh
->video_fmt
.field
;
1092 sfmt
= saa7146_format_by_fourcc(dev
,buf
->fmt
->pixelformat
);
1094 release_all_pagetables(dev
, buf
);
1095 if( 0 != IS_PLANAR(sfmt
->trans
)) {
1096 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[0]);
1097 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[1]);
1098 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[2]);
1100 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[0]);
1103 err
= videobuf_iolock(q
,&buf
->vb
, &vv
->ov_fb
);
1106 err
= saa7146_pgtable_build(dev
,buf
);
1110 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1111 buf
->activate
= buffer_activate
;
1116 DEB_D("error out\n");
1117 saa7146_dma_free(dev
,q
,buf
);
1122 static int buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1124 struct file
*file
= q
->priv_data
;
1125 struct saa7146_fh
*fh
= file
->private_data
;
1127 if (0 == *count
|| *count
> MAX_SAA7146_CAPTURE_BUFFERS
)
1128 *count
= MAX_SAA7146_CAPTURE_BUFFERS
;
1130 *size
= fh
->video_fmt
.sizeimage
;
1132 /* check if we exceed the "max_memory" parameter */
1133 if( (*count
* *size
) > (max_memory
*1048576) ) {
1134 *count
= (max_memory
*1048576) / *size
;
1137 DEB_CAP("%d buffers, %d bytes each\n", *count
, *size
);
1142 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1144 struct file
*file
= q
->priv_data
;
1145 struct saa7146_fh
*fh
= file
->private_data
;
1146 struct saa7146_dev
*dev
= fh
->dev
;
1147 struct saa7146_vv
*vv
= dev
->vv_data
;
1148 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1150 DEB_CAP("vbuf:%p\n", vb
);
1151 saa7146_buffer_queue(fh
->dev
,&vv
->video_q
,buf
);
1154 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1156 struct file
*file
= q
->priv_data
;
1157 struct saa7146_fh
*fh
= file
->private_data
;
1158 struct saa7146_dev
*dev
= fh
->dev
;
1159 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1161 DEB_CAP("vbuf:%p\n", vb
);
1163 saa7146_dma_free(dev
,q
,buf
);
1165 release_all_pagetables(dev
, buf
);
1168 static struct videobuf_queue_ops video_qops
= {
1169 .buf_setup
= buffer_setup
,
1170 .buf_prepare
= buffer_prepare
,
1171 .buf_queue
= buffer_queue
,
1172 .buf_release
= buffer_release
,
1175 /********************************************************************************/
1176 /* file operations */
1178 static void video_init(struct saa7146_dev
*dev
, struct saa7146_vv
*vv
)
1180 INIT_LIST_HEAD(&vv
->video_q
.queue
);
1182 init_timer(&vv
->video_q
.timeout
);
1183 vv
->video_q
.timeout
.function
= saa7146_buffer_timeout
;
1184 vv
->video_q
.timeout
.data
= (unsigned long)(&vv
->video_q
);
1185 vv
->video_q
.dev
= dev
;
1187 /* set some default values */
1188 vv
->standard
= &dev
->ext_vv_data
->stds
[0];
1190 /* FIXME: what's this? */
1191 vv
->current_hps_source
= SAA7146_HPS_SOURCE_PORT_A
;
1192 vv
->current_hps_sync
= SAA7146_HPS_SYNC_PORT_A
;
1196 static int video_open(struct saa7146_dev
*dev
, struct file
*file
)
1198 struct saa7146_fh
*fh
= file
->private_data
;
1199 struct saa7146_format
*sfmt
;
1201 fh
->video_fmt
.width
= 384;
1202 fh
->video_fmt
.height
= 288;
1203 fh
->video_fmt
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1204 fh
->video_fmt
.bytesperline
= 0;
1205 fh
->video_fmt
.field
= V4L2_FIELD_ANY
;
1206 fh
->video_fmt
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1207 sfmt
= saa7146_format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
1208 fh
->video_fmt
.sizeimage
= (fh
->video_fmt
.width
* fh
->video_fmt
.height
* sfmt
->depth
)/8;
1210 videobuf_queue_sg_init(&fh
->video_q
, &video_qops
,
1211 &dev
->pci
->dev
, &dev
->slock
,
1212 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1213 V4L2_FIELD_INTERLACED
,
1214 sizeof(struct saa7146_buf
),
1215 file
, &dev
->v4l2_lock
);
1221 static void video_close(struct saa7146_dev
*dev
, struct file
*file
)
1223 struct saa7146_fh
*fh
= file
->private_data
;
1224 struct saa7146_vv
*vv
= dev
->vv_data
;
1225 struct videobuf_queue
*q
= &fh
->video_q
;
1227 if (IS_CAPTURE_ACTIVE(fh
) != 0)
1228 video_end(fh
, file
);
1229 else if (IS_OVERLAY_ACTIVE(fh
) != 0)
1230 saa7146_stop_preview(fh
);
1233 /* hmm, why is this function declared void? */
1237 static void video_irq_done(struct saa7146_dev
*dev
, unsigned long st
)
1239 struct saa7146_vv
*vv
= dev
->vv_data
;
1240 struct saa7146_dmaqueue
*q
= &vv
->video_q
;
1242 spin_lock(&dev
->slock
);
1243 DEB_CAP("called\n");
1245 /* only finish the buffer if we have one... */
1246 if( NULL
!= q
->curr
) {
1247 saa7146_buffer_finish(dev
,q
,VIDEOBUF_DONE
);
1249 saa7146_buffer_next(dev
,q
,0);
1251 spin_unlock(&dev
->slock
);
1254 static ssize_t
video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1256 struct saa7146_fh
*fh
= file
->private_data
;
1257 struct saa7146_dev
*dev
= fh
->dev
;
1258 struct saa7146_vv
*vv
= dev
->vv_data
;
1263 if ((vv
->video_status
& STATUS_CAPTURE
) != 0) {
1264 /* fixme: should we allow read() captures while streaming capture? */
1265 if (vv
->video_fh
== fh
) {
1266 DEB_S("already capturing\n");
1269 DEB_S("already capturing in another open\n");
1273 ret
= video_begin(fh
);
1278 ret
= videobuf_read_one(&fh
->video_q
, data
, count
, ppos
,
1279 file
->f_flags
& O_NONBLOCK
);
1281 video_end(fh
, file
);
1283 ret
= video_end(fh
, file
);
1286 /* restart overlay if it was active before */
1287 if (vv
->ov_suspend
!= NULL
) {
1288 saa7146_start_preview(vv
->ov_suspend
);
1289 vv
->ov_suspend
= NULL
;
1295 struct saa7146_use_ops saa7146_video_uops
= {
1298 .release
= video_close
,
1299 .irq_done
= video_irq_done
,