1 #include <media/saa7146_vv.h>
3 static int max_memory
= 32;
5 module_param(max_memory
, int, 0644);
6 MODULE_PARM_DESC(max_memory
, "maximum memory usage for capture buffers (default: 32Mb)");
8 #define IS_CAPTURE_ACTIVE(fh) \
9 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
11 #define IS_OVERLAY_ACTIVE(fh) \
12 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
14 /* format descriptions for capture and preview */
15 static struct saa7146_format formats
[] = {
17 .name
= "RGB-8 (3-3-2)",
18 .pixelformat
= V4L2_PIX_FMT_RGB332
,
19 .trans
= RGB08_COMPOSED
,
23 .name
= "RGB-16 (5/B-6/G-5/R)",
24 .pixelformat
= V4L2_PIX_FMT_RGB565
,
25 .trans
= RGB16_COMPOSED
,
29 .name
= "RGB-24 (B-G-R)",
30 .pixelformat
= V4L2_PIX_FMT_BGR24
,
31 .trans
= RGB24_COMPOSED
,
35 .name
= "RGB-32 (B-G-R)",
36 .pixelformat
= V4L2_PIX_FMT_BGR32
,
37 .trans
= RGB32_COMPOSED
,
41 .name
= "RGB-32 (R-G-B)",
42 .pixelformat
= V4L2_PIX_FMT_RGB32
,
43 .trans
= RGB32_COMPOSED
,
48 .name
= "Greyscale-8",
49 .pixelformat
= V4L2_PIX_FMT_GREY
,
54 .name
= "YUV 4:2:2 planar (Y-Cb-Cr)",
55 .pixelformat
= V4L2_PIX_FMT_YUV422P
,
56 .trans
= YUV422_DECOMPOSED
,
58 .flags
= FORMAT_BYTE_SWAP
|FORMAT_IS_PLANAR
,
60 .name
= "YVU 4:2:0 planar (Y-Cb-Cr)",
61 .pixelformat
= V4L2_PIX_FMT_YVU420
,
62 .trans
= YUV420_DECOMPOSED
,
64 .flags
= FORMAT_BYTE_SWAP
|FORMAT_IS_PLANAR
,
66 .name
= "YUV 4:2:0 planar (Y-Cb-Cr)",
67 .pixelformat
= V4L2_PIX_FMT_YUV420
,
68 .trans
= YUV420_DECOMPOSED
,
70 .flags
= FORMAT_IS_PLANAR
,
72 .name
= "YUV 4:2:2 (U-Y-V-Y)",
73 .pixelformat
= V4L2_PIX_FMT_UYVY
,
74 .trans
= YUV422_COMPOSED
,
80 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
81 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
82 (like V4L2_PIX_FMT_YUYV) ... 8-( */
84 static int NUM_FORMATS
= sizeof(formats
)/sizeof(struct saa7146_format
);
86 struct saa7146_format
* format_by_fourcc(struct saa7146_dev
*dev
, int fourcc
)
88 int i
, j
= NUM_FORMATS
;
90 for (i
= 0; i
< j
; i
++) {
91 if (formats
[i
].pixelformat
== fourcc
) {
96 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc
));
100 static int g_fmt(struct saa7146_fh
*fh
, struct v4l2_format
*f
)
102 struct saa7146_dev
*dev
= fh
->dev
;
103 DEB_EE(("dev:%p, fh:%p\n",dev
,fh
));
106 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
107 f
->fmt
.pix
= fh
->video_fmt
;
109 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
110 f
->fmt
.win
= fh
->ov
.win
;
112 case V4L2_BUF_TYPE_VBI_CAPTURE
:
114 f
->fmt
.vbi
= fh
->vbi_fmt
;
118 DEB_D(("invalid format type '%d'.\n",f
->type
));
123 static int try_win(struct saa7146_dev
*dev
, struct v4l2_window
*win
)
125 struct saa7146_vv
*vv
= dev
->vv_data
;
126 enum v4l2_field field
;
129 DEB_EE(("dev:%p\n",dev
));
131 if (NULL
== vv
->ov_fb
.base
) {
132 DEB_D(("no fb base set.\n"));
135 if (NULL
== vv
->ov_fmt
) {
136 DEB_D(("no fb fmt set.\n"));
139 if (win
->w
.width
< 48 || win
->w
.height
< 32) {
140 DEB_D(("min width/height. (%d,%d)\n",win
->w
.width
,win
->w
.height
));
143 if (win
->clipcount
> 16) {
144 DEB_D(("clipcount too big.\n"));
149 maxw
= vv
->standard
->h_max_out
;
150 maxh
= vv
->standard
->v_max_out
;
152 if (V4L2_FIELD_ANY
== field
) {
153 field
= (win
->w
.height
> maxh
/2)
154 ? V4L2_FIELD_INTERLACED
159 case V4L2_FIELD_BOTTOM
:
160 case V4L2_FIELD_ALTERNATE
:
163 case V4L2_FIELD_INTERLACED
:
166 DEB_D(("no known field mode '%d'.\n",field
));
172 if (win
->w
.width
> maxw
)
174 if (win
->w
.height
> maxh
)
175 win
->w
.height
= maxh
;
180 static int try_fmt(struct saa7146_fh
*fh
, struct v4l2_format
*f
)
182 struct saa7146_dev
*dev
= fh
->dev
;
183 struct saa7146_vv
*vv
= dev
->vv_data
;
187 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
189 struct saa7146_format
*fmt
;
190 enum v4l2_field field
;
194 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev
,fh
));
196 fmt
= format_by_fourcc(dev
,f
->fmt
.pix
.pixelformat
);
201 field
= f
->fmt
.pix
.field
;
202 maxw
= vv
->standard
->h_max_out
;
203 maxh
= vv
->standard
->v_max_out
;
205 if (V4L2_FIELD_ANY
== field
) {
206 field
= (f
->fmt
.pix
.height
> maxh
/2)
207 ? V4L2_FIELD_INTERLACED
211 case V4L2_FIELD_ALTERNATE
: {
212 vv
->last_field
= V4L2_FIELD_TOP
;
217 case V4L2_FIELD_BOTTOM
:
218 vv
->last_field
= V4L2_FIELD_INTERLACED
;
221 case V4L2_FIELD_INTERLACED
:
222 vv
->last_field
= V4L2_FIELD_INTERLACED
;
225 DEB_D(("no known field mode '%d'.\n",field
));
230 f
->fmt
.pix
.field
= field
;
231 if (f
->fmt
.pix
.width
> maxw
)
232 f
->fmt
.pix
.width
= maxw
;
233 if (f
->fmt
.pix
.height
> maxh
)
234 f
->fmt
.pix
.height
= maxh
;
236 calc_bpl
= (f
->fmt
.pix
.width
* fmt
->depth
)/8;
238 if (f
->fmt
.pix
.bytesperline
< calc_bpl
)
239 f
->fmt
.pix
.bytesperline
= calc_bpl
;
241 if (f
->fmt
.pix
.bytesperline
> (2*PAGE_SIZE
* fmt
->depth
)/8) /* arbitrary constraint */
242 f
->fmt
.pix
.bytesperline
= calc_bpl
;
244 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* f
->fmt
.pix
.height
;
245 DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",f
->fmt
.pix
.width
,f
->fmt
.pix
.height
,f
->fmt
.pix
.bytesperline
,f
->fmt
.pix
.sizeimage
));
249 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
250 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev
,fh
));
251 err
= try_win(dev
,&f
->fmt
.win
);
257 DEB_EE(("unknown format type '%d'\n",f
->type
));
262 int saa7146_start_preview(struct saa7146_fh
*fh
)
264 struct saa7146_dev
*dev
= fh
->dev
;
265 struct saa7146_vv
*vv
= dev
->vv_data
;
266 int ret
= 0, err
= 0;
268 DEB_EE(("dev:%p, fh:%p\n",dev
,fh
));
270 /* check if we have overlay informations */
271 if( NULL
== fh
->ov
.fh
) {
272 DEB_D(("no overlay data available. try S_FMT first.\n"));
276 /* check if streaming capture is running */
277 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
278 DEB_D(("streaming capture is active.\n"));
282 /* check if overlay is running */
283 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
284 if (vv
->video_fh
== fh
) {
285 DEB_D(("overlay is already active.\n"));
288 DEB_D(("overlay is already active in another open.\n"));
292 if (0 == saa7146_res_get(fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
)) {
293 DEB_D(("cannot get necessary overlay resources\n"));
297 err
= try_win(dev
,&fh
->ov
.win
);
299 saa7146_res_free(vv
->video_fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
303 vv
->ov_data
= &fh
->ov
;
305 DEB_D(("%dx%d+%d+%d %s field=%s\n",
306 fh
->ov
.win
.w
.width
,fh
->ov
.win
.w
.height
,
307 fh
->ov
.win
.w
.left
,fh
->ov
.win
.w
.top
,
308 vv
->ov_fmt
->name
,v4l2_field_names
[fh
->ov
.win
.field
]));
310 if (0 != (ret
= saa7146_enable_overlay(fh
))) {
311 DEB_D(("enabling overlay failed: %d\n",ret
));
312 saa7146_res_free(vv
->video_fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
316 vv
->video_status
= STATUS_OVERLAY
;
322 int saa7146_stop_preview(struct saa7146_fh
*fh
)
324 struct saa7146_dev
*dev
= fh
->dev
;
325 struct saa7146_vv
*vv
= dev
->vv_data
;
327 DEB_EE(("dev:%p, fh:%p\n",dev
,fh
));
329 /* check if streaming capture is running */
330 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
331 DEB_D(("streaming capture is active.\n"));
335 /* check if overlay is running at all */
336 if ((vv
->video_status
& STATUS_OVERLAY
) == 0) {
337 DEB_D(("no active overlay.\n"));
341 if (vv
->video_fh
!= fh
) {
342 DEB_D(("overlay is active, but in another open.\n"));
346 vv
->video_status
= 0;
349 saa7146_disable_overlay(fh
);
351 saa7146_res_free(fh
, RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
);
356 static int s_fmt(struct saa7146_fh
*fh
, struct v4l2_format
*f
)
358 struct saa7146_dev
*dev
= fh
->dev
;
359 struct saa7146_vv
*vv
= dev
->vv_data
;
364 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
365 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev
,fh
));
366 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
367 DEB_EE(("streaming capture is active\n"));
373 fh
->video_fmt
= f
->fmt
.pix
;
374 DEB_EE(("set to pixelformat '%4.4s'\n",(char *)&fh
->video_fmt
.pixelformat
));
376 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
377 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev
,fh
));
378 err
= try_win(dev
,&f
->fmt
.win
);
382 fh
->ov
.win
= f
->fmt
.win
;
383 fh
->ov
.nclips
= f
->fmt
.win
.clipcount
;
384 if (fh
->ov
.nclips
> 16)
386 if (copy_from_user(fh
->ov
.clips
,f
->fmt
.win
.clips
,sizeof(struct v4l2_clip
)*fh
->ov
.nclips
)) {
391 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
396 /* check if our current overlay is active */
397 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
398 saa7146_stop_preview(fh
);
399 saa7146_start_preview(fh
);
403 DEB_D(("unknown format type '%d'\n",f
->type
));
408 /********************************************************************************/
409 /* device controls */
411 static struct v4l2_queryctrl controls
[] = {
413 .id
= V4L2_CID_BRIGHTNESS
,
414 .name
= "Brightness",
418 .default_value
= 128,
419 .type
= V4L2_CTRL_TYPE_INTEGER
,
421 .id
= V4L2_CID_CONTRAST
,
427 .type
= V4L2_CTRL_TYPE_INTEGER
,
429 .id
= V4L2_CID_SATURATION
,
430 .name
= "Saturation",
435 .type
= V4L2_CTRL_TYPE_INTEGER
,
437 .id
= V4L2_CID_VFLIP
,
438 .name
= "Vertical flip",
441 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
443 .id
= V4L2_CID_HFLIP
,
444 .name
= "Horizontal flip",
447 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
450 static int NUM_CONTROLS
= sizeof(controls
)/sizeof(struct v4l2_queryctrl
);
452 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
454 static struct v4l2_queryctrl
* ctrl_by_id(int id
)
458 for (i
= 0; i
< NUM_CONTROLS
; i
++)
459 if (controls
[i
].id
== id
)
464 static int get_control(struct saa7146_fh
*fh
, struct v4l2_control
*c
)
466 struct saa7146_dev
*dev
= fh
->dev
;
467 struct saa7146_vv
*vv
= dev
->vv_data
;
469 const struct v4l2_queryctrl
* ctrl
;
472 ctrl
= ctrl_by_id(c
->id
);
476 case V4L2_CID_BRIGHTNESS
:
477 value
= saa7146_read(dev
, BCS_CTRL
);
478 c
->value
= 0xff & (value
>> 24);
479 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n",c
->value
));
481 case V4L2_CID_CONTRAST
:
482 value
= saa7146_read(dev
, BCS_CTRL
);
483 c
->value
= 0x7f & (value
>> 16);
484 DEB_D(("V4L2_CID_CONTRAST: %d\n",c
->value
));
486 case V4L2_CID_SATURATION
:
487 value
= saa7146_read(dev
, BCS_CTRL
);
488 c
->value
= 0x7f & (value
>> 0);
489 DEB_D(("V4L2_CID_SATURATION: %d\n",c
->value
));
492 c
->value
= vv
->vflip
;
493 DEB_D(("V4L2_CID_VFLIP: %d\n",c
->value
));
496 c
->value
= vv
->hflip
;
497 DEB_D(("V4L2_CID_HFLIP: %d\n",c
->value
));
506 static int set_control(struct saa7146_fh
*fh
, struct v4l2_control
*c
)
508 struct saa7146_dev
*dev
= fh
->dev
;
509 struct saa7146_vv
*vv
= dev
->vv_data
;
511 const struct v4l2_queryctrl
* ctrl
;
513 ctrl
= ctrl_by_id(c
->id
);
515 DEB_D(("unknown control %d\n",c
->id
));
521 switch (ctrl
->type
) {
522 case V4L2_CTRL_TYPE_BOOLEAN
:
523 case V4L2_CTRL_TYPE_MENU
:
524 case V4L2_CTRL_TYPE_INTEGER
:
525 if (c
->value
< ctrl
->minimum
)
526 c
->value
= ctrl
->minimum
;
527 if (c
->value
> ctrl
->maximum
)
528 c
->value
= ctrl
->maximum
;
535 case V4L2_CID_BRIGHTNESS
: {
536 u32 value
= saa7146_read(dev
, BCS_CTRL
);
538 value
|= (c
->value
<< 24);
539 saa7146_write(dev
, BCS_CTRL
, value
);
540 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
543 case V4L2_CID_CONTRAST
: {
544 u32 value
= saa7146_read(dev
, BCS_CTRL
);
546 value
|= (c
->value
<< 16);
547 saa7146_write(dev
, BCS_CTRL
, value
);
548 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
551 case V4L2_CID_SATURATION
: {
552 u32 value
= saa7146_read(dev
, BCS_CTRL
);
554 value
|= (c
->value
<< 0);
555 saa7146_write(dev
, BCS_CTRL
, value
);
556 saa7146_write(dev
, MC2
, MASK_22
| MASK_06
);
560 /* fixme: we can support changing VFLIP and HFLIP here... */
561 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
562 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
566 vv
->hflip
= c
->value
;
569 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
570 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
574 vv
->vflip
= c
->value
;
582 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
583 saa7146_stop_preview(fh
);
584 saa7146_start_preview(fh
);
589 /********************************************************************************/
590 /* common pagetable functions */
592 static int saa7146_pgtable_build(struct saa7146_dev
*dev
, struct saa7146_buf
*buf
)
594 struct pci_dev
*pci
= dev
->pci
;
595 struct scatterlist
*list
= buf
->vb
.dma
.sglist
;
596 int length
= buf
->vb
.dma
.sglen
;
597 struct saa7146_format
*sfmt
= format_by_fourcc(dev
,buf
->fmt
->pixelformat
);
599 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev
,buf
,length
));
601 if( 0 != IS_PLANAR(sfmt
->trans
)) {
602 struct saa7146_pgtable
*pt1
= &buf
->pt
[0];
603 struct saa7146_pgtable
*pt2
= &buf
->pt
[1];
604 struct saa7146_pgtable
*pt3
= &buf
->pt
[2];
605 u32
*ptr1
, *ptr2
, *ptr3
;
608 int size
= buf
->fmt
->width
*buf
->fmt
->height
;
609 int i
,p
,m1
,m2
,m3
,o1
,o2
;
611 switch( sfmt
->depth
) {
613 /* create some offsets inside the page table */
614 m1
= ((size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
615 m2
= ((size
+(size
/4)+PAGE_SIZE
)/PAGE_SIZE
)-1;
616 m3
= ((size
+(size
/2)+PAGE_SIZE
)/PAGE_SIZE
)-1;
618 o2
= (size
+(size
/4))%PAGE_SIZE
;
619 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size
,m1
,m2
,m3
,o1
,o2
));
623 /* create some offsets inside the page table */
624 m1
= ((size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
625 m2
= ((size
+(size
/2)+PAGE_SIZE
)/PAGE_SIZE
)-1;
626 m3
= ((2*size
+PAGE_SIZE
)/PAGE_SIZE
)-1;
628 o2
= (size
+(size
/2))%PAGE_SIZE
;
629 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size
,m1
,m2
,m3
,o1
,o2
));
641 /* walk all pages, copy all page addresses to ptr1 */
642 for (i
= 0; i
< length
; i
++, list
++) {
643 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr1
++) {
644 *ptr1
= cpu_to_le32(sg_dma_address(list
) - list
->offset
);
650 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
654 /* if we have a user buffer, the first page may not be
655 aligned to a page boundary. */
656 pt1
->offset
= buf
->vb
.dma
.sglist
->offset
;
657 pt2
->offset
= pt1
->offset
+o1
;
658 pt3
->offset
= pt1
->offset
+o2
;
660 /* create video-dma2 page table */
662 for(i
= m1
; i
<= m2
; i
++, ptr2
++) {
666 for(;i
<1024;i
++,ptr2
++) {
669 /* create video-dma3 page table */
671 for(i
= m2
; i
<= m3
; i
++,ptr3
++) {
675 for(;i
<1024;i
++,ptr3
++) {
678 /* finally: finish up video-dma1 page table */
681 for(i
=m1
;i
<1024;i
++,ptr1
++) {
689 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
692 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
695 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
699 struct saa7146_pgtable
*pt
= &buf
->pt
[0];
700 return saa7146_pgtable_build_single(pci
, pt
, list
, length
);
707 /********************************************************************************/
708 /* file operations */
710 static int video_begin(struct saa7146_fh
*fh
)
712 struct saa7146_dev
*dev
= fh
->dev
;
713 struct saa7146_vv
*vv
= dev
->vv_data
;
714 struct saa7146_format
*fmt
= NULL
;
715 unsigned int resource
;
716 int ret
= 0, err
= 0;
718 DEB_EE(("dev:%p, fh:%p\n",dev
,fh
));
720 if ((vv
->video_status
& STATUS_CAPTURE
) != 0) {
721 if (vv
->video_fh
== fh
) {
722 DEB_S(("already capturing.\n"));
725 DEB_S(("already capturing in another open.\n"));
729 if ((vv
->video_status
& STATUS_OVERLAY
) != 0) {
730 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
731 vv
->ov_suspend
= vv
->video_fh
;
732 err
= saa7146_stop_preview(vv
->video_fh
); /* side effect: video_status is now 0, video_fh is NULL */
734 DEB_D(("suspending video failed. aborting\n"));
739 fmt
= format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
740 /* we need to have a valid format set here */
743 if (0 != (fmt
->flags
& FORMAT_IS_PLANAR
)) {
744 resource
= RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
|RESOURCE_DMA3_BRS
;
746 resource
= RESOURCE_DMA1_HPS
;
749 ret
= saa7146_res_get(fh
, resource
);
751 DEB_S(("cannot get capture resource %d\n",resource
));
752 if (vv
->ov_suspend
!= NULL
) {
753 saa7146_start_preview(vv
->ov_suspend
);
754 vv
->ov_suspend
= NULL
;
759 /* clear out beginning of streaming bit (rps register 0)*/
760 saa7146_write(dev
, MC2
, MASK_27
);
762 /* enable rps0 irqs */
763 SAA7146_IER_ENABLE(dev
, MASK_27
);
766 vv
->video_status
= STATUS_CAPTURE
;
771 static int video_end(struct saa7146_fh
*fh
, struct file
*file
)
773 struct saa7146_dev
*dev
= fh
->dev
;
774 struct saa7146_vv
*vv
= dev
->vv_data
;
775 struct saa7146_format
*fmt
= NULL
;
777 unsigned int resource
;
779 DEB_EE(("dev:%p, fh:%p\n",dev
,fh
));
781 if ((vv
->video_status
& STATUS_CAPTURE
) != STATUS_CAPTURE
) {
782 DEB_S(("not capturing.\n"));
786 if (vv
->video_fh
!= fh
) {
787 DEB_S(("capturing, but in another open.\n"));
791 fmt
= format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
792 /* we need to have a valid format set here */
795 if (0 != (fmt
->flags
& FORMAT_IS_PLANAR
)) {
796 resource
= RESOURCE_DMA1_HPS
|RESOURCE_DMA2_CLP
|RESOURCE_DMA3_BRS
;
797 dmas
= MASK_22
| MASK_21
| MASK_20
;
799 resource
= RESOURCE_DMA1_HPS
;
802 spin_lock_irqsave(&dev
->slock
,flags
);
805 saa7146_write(dev
, MC1
, MASK_28
);
807 /* disable rps0 irqs */
808 SAA7146_IER_DISABLE(dev
, MASK_27
);
810 /* shut down all used video dma transfers */
811 saa7146_write(dev
, MC1
, dmas
);
813 spin_unlock_irqrestore(&dev
->slock
, flags
);
816 vv
->video_status
= 0;
818 saa7146_res_free(fh
, resource
);
820 if (vv
->ov_suspend
!= NULL
) {
821 saa7146_start_preview(vv
->ov_suspend
);
822 vv
->ov_suspend
= NULL
;
829 * This function is _not_ called directly, but from
830 * video_generic_ioctl (and maybe others). userspace
831 * copying is done already, arg is a kernel pointer.
834 int saa7146_video_do_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, void *arg
)
836 struct saa7146_fh
*fh
= file
->private_data
;
837 struct saa7146_dev
*dev
= fh
->dev
;
838 struct saa7146_vv
*vv
= dev
->vv_data
;
840 int err
= 0, result
= 0, ee
= 0;
842 struct saa7146_use_ops
*ops
;
843 struct videobuf_queue
*q
;
845 /* check if extension handles the command */
846 for(ee
= 0; dev
->ext_vv_data
->ioctls
[ee
].flags
!= 0; ee
++) {
847 if( cmd
== dev
->ext_vv_data
->ioctls
[ee
].cmd
)
851 if( 0 != (dev
->ext_vv_data
->ioctls
[ee
].flags
& SAA7146_EXCLUSIVE
) ) {
852 DEB_D(("extension handles ioctl exclusive.\n"));
853 result
= dev
->ext_vv_data
->ioctl(fh
, cmd
, arg
);
856 if( 0 != (dev
->ext_vv_data
->ioctls
[ee
].flags
& SAA7146_BEFORE
) ) {
857 DEB_D(("extension handles ioctl before.\n"));
858 result
= dev
->ext_vv_data
->ioctl(fh
, cmd
, arg
);
859 if( -EAGAIN
!= result
) {
864 /* fixme: add handle "after" case (is it still needed?) */
867 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
868 ops
= &saa7146_video_uops
;
872 case V4L2_BUF_TYPE_VBI_CAPTURE
: {
873 ops
= &saa7146_vbi_uops
;
883 case VIDIOC_QUERYCAP
:
885 struct v4l2_capability
*cap
= arg
;
886 memset(cap
,0,sizeof(*cap
));
888 DEB_EE(("VIDIOC_QUERYCAP\n"));
890 strcpy(cap
->driver
, "saa7146 v4l2");
891 strlcpy(cap
->card
, dev
->ext
->name
, sizeof(cap
->card
));
892 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
893 cap
->version
= SAA7146_VERSION_CODE
;
895 V4L2_CAP_VIDEO_CAPTURE
|
896 V4L2_CAP_VIDEO_OVERLAY
|
899 cap
->capabilities
|= dev
->ext_vv_data
->capabilities
;
904 struct v4l2_framebuffer
*fb
= arg
;
906 DEB_EE(("VIDIOC_G_FBUF\n"));
909 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
914 struct v4l2_framebuffer
*fb
= arg
;
915 struct saa7146_format
*fmt
;
917 DEB_EE(("VIDIOC_S_FBUF\n"));
919 if(!capable(CAP_SYS_ADMIN
) &&
920 !capable(CAP_SYS_RAWIO
))
924 fmt
= format_by_fourcc(dev
,fb
->fmt
.pixelformat
);
929 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
930 if (0 != (fmt
->flags
& FORMAT_IS_PLANAR
)) {
931 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",(char *)&fmt
->pixelformat
));
934 /* check if overlay is running */
935 if (IS_OVERLAY_ACTIVE(fh
) != 0) {
936 if (vv
->video_fh
!= fh
) {
937 DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
947 if (0 == vv
->ov_fb
.fmt
.bytesperline
)
948 vv
->ov_fb
.fmt
.bytesperline
=
949 vv
->ov_fb
.fmt
.width
*fmt
->depth
/8;
955 case VIDIOC_ENUM_FMT
:
957 struct v4l2_fmtdesc
*f
= arg
;
961 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
962 case V4L2_BUF_TYPE_VIDEO_OVERLAY
: {
964 if (index
< 0 || index
>= NUM_FORMATS
) {
967 memset(f
,0,sizeof(*f
));
969 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
970 f
->pixelformat
= formats
[index
].pixelformat
;
977 DEB_EE(("VIDIOC_ENUM_FMT: type:%d, index:%d\n",f
->type
,f
->index
));
980 case VIDIOC_QUERYCTRL
:
982 const struct v4l2_queryctrl
*ctrl
;
983 struct v4l2_queryctrl
*c
= arg
;
985 if ((c
->id
< V4L2_CID_BASE
||
986 c
->id
>= V4L2_CID_LASTP1
) &&
987 (c
->id
< V4L2_CID_PRIVATE_BASE
||
988 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
991 ctrl
= ctrl_by_id(c
->id
);
995 c->flags = V4L2_CTRL_FLAG_DISABLED;
1000 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n",c
->id
));
1004 case VIDIOC_G_CTRL
: {
1005 DEB_EE(("VIDIOC_G_CTRL\n"));
1006 return get_control(fh
,arg
);
1010 DEB_EE(("VIDIOC_S_CTRL\n"));
1011 err
= set_control(fh
,arg
);
1016 struct v4l2_streamparm
*parm
= arg
;
1017 if( parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1020 memset(&parm
->parm
.capture
,0,sizeof(struct v4l2_captureparm
));
1021 parm
->parm
.capture
.readbuffers
= 1;
1022 // fixme: only for PAL!
1023 parm
->parm
.capture
.timeperframe
.numerator
= 1;
1024 parm
->parm
.capture
.timeperframe
.denominator
= 25;
1029 struct v4l2_format
*f
= arg
;
1030 DEB_EE(("VIDIOC_G_FMT\n"));
1035 struct v4l2_format
*f
= arg
;
1036 DEB_EE(("VIDIOC_S_FMT\n"));
1039 case VIDIOC_TRY_FMT
:
1041 struct v4l2_format
*f
= arg
;
1042 DEB_EE(("VIDIOC_TRY_FMT\n"));
1043 return try_fmt(fh
,f
);
1047 v4l2_std_id
*id
= arg
;
1048 DEB_EE(("VIDIOC_G_STD\n"));
1049 *id
= vv
->standard
->id
;
1052 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
1053 PAL / NTSC / SECAM. if your hardware does not (or does more)
1054 -- override this function in your extension */
1055 case VIDIOC_ENUMSTD
:
1057 struct v4l2_standard
*e
= arg
;
1060 if( e
->index
< dev
->ext_vv_data
->num_stds
) {
1061 DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e
->index
));
1062 v4l2_video_std_construct(e
, dev
->ext_vv_data
->stds
[e
->index
].id
, dev
->ext_vv_data
->stds
[e
->index
].name
);
1069 v4l2_std_id
*id
= arg
;
1073 DEB_EE(("VIDIOC_S_STD\n"));
1075 if ((vv
->video_status
& STATUS_CAPTURE
) == STATUS_CAPTURE
) {
1076 DEB_D(("cannot change video standard while streaming capture is active\n"));
1080 if ((vv
->video_status
& STATUS_OVERLAY
) != 0) {
1081 vv
->ov_suspend
= vv
->video_fh
;
1082 err
= saa7146_stop_preview(vv
->video_fh
); /* side effect: video_status is now 0, video_fh is NULL */
1084 DEB_D(("suspending video failed. aborting\n"));
1091 for(i
= 0; i
< dev
->ext_vv_data
->num_stds
; i
++)
1092 if (*id
& dev
->ext_vv_data
->stds
[i
].id
)
1094 if (i
!= dev
->ext_vv_data
->num_stds
) {
1095 vv
->standard
= &dev
->ext_vv_data
->stds
[i
];
1096 if( NULL
!= dev
->ext_vv_data
->std_callback
)
1097 dev
->ext_vv_data
->std_callback(dev
, vv
->standard
);
1103 if (vv
->ov_suspend
!= NULL
) {
1104 saa7146_start_preview(vv
->ov_suspend
);
1105 vv
->ov_suspend
= NULL
;
1109 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
1113 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n",vv
->standard
->name
));
1116 case VIDIOC_OVERLAY
:
1122 int on
= *(int *)arg
;
1125 DEB_D(("VIDIOC_OVERLAY on:%d\n",on
));
1127 err
= saa7146_start_preview(fh
);
1129 err
= saa7146_stop_preview(fh
);
1133 case VIDIOC_REQBUFS
: {
1134 struct v4l2_requestbuffers
*req
= arg
;
1135 DEB_D(("VIDIOC_REQBUFS, type:%d\n",req
->type
));
1136 return videobuf_reqbufs(q
,req
);
1138 case VIDIOC_QUERYBUF
: {
1139 struct v4l2_buffer
*buf
= arg
;
1140 DEB_D(("VIDIOC_QUERYBUF, type:%d, offset:%d\n",buf
->type
,buf
->m
.offset
));
1141 return videobuf_querybuf(q
,buf
);
1144 struct v4l2_buffer
*buf
= arg
;
1146 ret
= videobuf_qbuf(q
,buf
);
1147 DEB_D(("VIDIOC_QBUF: ret:%d, index:%d\n",ret
,buf
->index
));
1150 case VIDIOC_DQBUF
: {
1151 struct v4l2_buffer
*buf
= arg
;
1153 ret
= videobuf_dqbuf(q
,buf
,file
->f_flags
& O_NONBLOCK
);
1154 DEB_D(("VIDIOC_DQBUF: ret:%d, index:%d\n",ret
,buf
->index
));
1157 case VIDIOC_STREAMON
: {
1159 DEB_D(("VIDIOC_STREAMON, type:%d\n",*type
));
1161 err
= video_begin(fh
);
1165 err
= videobuf_streamon(q
);
1168 case VIDIOC_STREAMOFF
: {
1171 DEB_D(("VIDIOC_STREAMOFF, type:%d\n",*type
));
1173 /* ugly: we need to copy some checks from video_end(),
1174 because videobuf_streamoff() relies on the capture running.
1175 check and fix this */
1176 if ((vv
->video_status
& STATUS_CAPTURE
) != STATUS_CAPTURE
) {
1177 DEB_S(("not capturing.\n"));
1181 if (vv
->video_fh
!= fh
) {
1182 DEB_S(("capturing, but in another open.\n"));
1186 err
= videobuf_streamoff(q
);
1188 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1189 video_end(fh
, file
);
1191 err
= video_end(fh
, file
);
1197 struct video_mbuf
*mbuf
= arg
;
1198 struct videobuf_queue
*q
;
1201 /* fixme: number of capture buffers and sizes for v4l apps */
1203 int gbufsize
= 768*576*4;
1205 DEB_D(("VIDIOCGMBUF \n"));
1209 err
= videobuf_mmap_setup(q
,gbuffers
,gbufsize
,
1215 memset(mbuf
,0,sizeof(*mbuf
));
1216 mbuf
->frames
= gbuffers
;
1217 mbuf
->size
= gbuffers
* gbufsize
;
1218 for (i
= 0; i
< gbuffers
; i
++)
1219 mbuf
->offsets
[i
] = i
* gbufsize
;
1224 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1225 saa7146_video_do_ioctl
);
1230 /*********************************************************************************/
1231 /* buffer handling functions */
1233 static int buffer_activate (struct saa7146_dev
*dev
,
1234 struct saa7146_buf
*buf
,
1235 struct saa7146_buf
*next
)
1237 struct saa7146_vv
*vv
= dev
->vv_data
;
1239 buf
->vb
.state
= STATE_ACTIVE
;
1240 saa7146_set_capture(dev
,buf
,next
);
1242 mod_timer(&vv
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1246 static int buffer_prepare(struct videobuf_queue
*q
,
1247 struct videobuf_buffer
*vb
, enum v4l2_field field
)
1249 struct file
*file
= q
->priv_data
;
1250 struct saa7146_fh
*fh
= file
->private_data
;
1251 struct saa7146_dev
*dev
= fh
->dev
;
1252 struct saa7146_vv
*vv
= dev
->vv_data
;
1253 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1256 DEB_CAP(("vbuf:%p\n",vb
));
1259 if (fh
->video_fmt
.width
< 48 ||
1260 fh
->video_fmt
.height
< 32 ||
1261 fh
->video_fmt
.width
> vv
->standard
->h_max_out
||
1262 fh
->video_fmt
.height
> vv
->standard
->v_max_out
) {
1263 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh
->video_fmt
.width
,fh
->video_fmt
.height
));
1267 size
= fh
->video_fmt
.sizeimage
;
1268 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
) {
1269 DEB_D(("size mismatch.\n"));
1273 DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1274 fh
->video_fmt
.width
,fh
->video_fmt
.height
,size
,v4l2_field_names
[fh
->video_fmt
.field
]));
1275 if (buf
->vb
.width
!= fh
->video_fmt
.width
||
1276 buf
->vb
.bytesperline
!= fh
->video_fmt
.bytesperline
||
1277 buf
->vb
.height
!= fh
->video_fmt
.height
||
1278 buf
->vb
.size
!= size
||
1279 buf
->vb
.field
!= field
||
1280 buf
->vb
.field
!= fh
->video_fmt
.field
||
1281 buf
->fmt
!= &fh
->video_fmt
) {
1282 saa7146_dma_free(dev
,buf
);
1285 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
1286 struct saa7146_format
*sfmt
;
1288 buf
->vb
.bytesperline
= fh
->video_fmt
.bytesperline
;
1289 buf
->vb
.width
= fh
->video_fmt
.width
;
1290 buf
->vb
.height
= fh
->video_fmt
.height
;
1291 buf
->vb
.size
= size
;
1292 buf
->vb
.field
= field
;
1293 buf
->fmt
= &fh
->video_fmt
;
1294 buf
->vb
.field
= fh
->video_fmt
.field
;
1296 sfmt
= format_by_fourcc(dev
,buf
->fmt
->pixelformat
);
1298 if( 0 != IS_PLANAR(sfmt
->trans
)) {
1299 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[0]);
1300 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[1]);
1301 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[2]);
1303 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[0]);
1304 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[1]);
1305 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[2]);
1307 saa7146_pgtable_free(dev
->pci
, &buf
->pt
[0]);
1308 saa7146_pgtable_alloc(dev
->pci
, &buf
->pt
[0]);
1311 err
= videobuf_iolock(dev
->pci
,&buf
->vb
, &vv
->ov_fb
);
1314 err
= saa7146_pgtable_build(dev
,buf
);
1318 buf
->vb
.state
= STATE_PREPARED
;
1319 buf
->activate
= buffer_activate
;
1324 DEB_D(("error out.\n"));
1325 saa7146_dma_free(dev
,buf
);
1330 static int buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1332 struct file
*file
= q
->priv_data
;
1333 struct saa7146_fh
*fh
= file
->private_data
;
1335 if (0 == *count
|| *count
> MAX_SAA7146_CAPTURE_BUFFERS
)
1336 *count
= MAX_SAA7146_CAPTURE_BUFFERS
;
1338 *size
= fh
->video_fmt
.sizeimage
;
1340 /* check if we exceed the "max_memory" parameter */
1341 if( (*count
* *size
) > (max_memory
*1048576) ) {
1342 *count
= (max_memory
*1048576) / *size
;
1345 DEB_CAP(("%d buffers, %d bytes each.\n",*count
,*size
));
1350 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1352 struct file
*file
= q
->priv_data
;
1353 struct saa7146_fh
*fh
= file
->private_data
;
1354 struct saa7146_dev
*dev
= fh
->dev
;
1355 struct saa7146_vv
*vv
= dev
->vv_data
;
1356 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1358 DEB_CAP(("vbuf:%p\n",vb
));
1359 saa7146_buffer_queue(fh
->dev
,&vv
->video_q
,buf
);
1363 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1365 struct file
*file
= q
->priv_data
;
1366 struct saa7146_fh
*fh
= file
->private_data
;
1367 struct saa7146_dev
*dev
= fh
->dev
;
1368 struct saa7146_buf
*buf
= (struct saa7146_buf
*)vb
;
1370 DEB_CAP(("vbuf:%p\n",vb
));
1371 saa7146_dma_free(dev
,buf
);
1374 static struct videobuf_queue_ops video_qops
= {
1375 .buf_setup
= buffer_setup
,
1376 .buf_prepare
= buffer_prepare
,
1377 .buf_queue
= buffer_queue
,
1378 .buf_release
= buffer_release
,
1381 /********************************************************************************/
1382 /* file operations */
1384 static void video_init(struct saa7146_dev
*dev
, struct saa7146_vv
*vv
)
1386 INIT_LIST_HEAD(&vv
->video_q
.queue
);
1388 init_timer(&vv
->video_q
.timeout
);
1389 vv
->video_q
.timeout
.function
= saa7146_buffer_timeout
;
1390 vv
->video_q
.timeout
.data
= (unsigned long)(&vv
->video_q
);
1391 vv
->video_q
.dev
= dev
;
1393 /* set some default values */
1394 vv
->standard
= &dev
->ext_vv_data
->stds
[0];
1396 /* FIXME: what's this? */
1397 vv
->current_hps_source
= SAA7146_HPS_SOURCE_PORT_A
;
1398 vv
->current_hps_sync
= SAA7146_HPS_SYNC_PORT_A
;
1402 static int video_open(struct saa7146_dev
*dev
, struct file
*file
)
1404 struct saa7146_fh
*fh
= (struct saa7146_fh
*)file
->private_data
;
1405 struct saa7146_format
*sfmt
;
1407 fh
->video_fmt
.width
= 384;
1408 fh
->video_fmt
.height
= 288;
1409 fh
->video_fmt
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1410 fh
->video_fmt
.bytesperline
= 0;
1411 fh
->video_fmt
.field
= V4L2_FIELD_ANY
;
1412 sfmt
= format_by_fourcc(dev
,fh
->video_fmt
.pixelformat
);
1413 fh
->video_fmt
.sizeimage
= (fh
->video_fmt
.width
* fh
->video_fmt
.height
* sfmt
->depth
)/8;
1415 videobuf_queue_init(&fh
->video_q
, &video_qops
,
1416 dev
->pci
, &dev
->slock
,
1417 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1418 V4L2_FIELD_INTERLACED
,
1419 sizeof(struct saa7146_buf
),
1422 init_MUTEX(&fh
->video_q
.lock
);
1428 static void video_close(struct saa7146_dev
*dev
, struct file
*file
)
1430 struct saa7146_fh
*fh
= (struct saa7146_fh
*)file
->private_data
;
1431 struct saa7146_vv
*vv
= dev
->vv_data
;
1434 if (IS_CAPTURE_ACTIVE(fh
) != 0) {
1435 err
= video_end(fh
, file
);
1436 } else if (IS_OVERLAY_ACTIVE(fh
) != 0) {
1437 err
= saa7146_stop_preview(fh
);
1440 /* hmm, why is this function declared void? */
1445 static void video_irq_done(struct saa7146_dev
*dev
, unsigned long st
)
1447 struct saa7146_vv
*vv
= dev
->vv_data
;
1448 struct saa7146_dmaqueue
*q
= &vv
->video_q
;
1450 spin_lock(&dev
->slock
);
1451 DEB_CAP(("called.\n"));
1453 /* only finish the buffer if we have one... */
1454 if( NULL
!= q
->curr
) {
1455 saa7146_buffer_finish(dev
,q
,STATE_DONE
);
1457 saa7146_buffer_next(dev
,q
,0);
1459 spin_unlock(&dev
->slock
);
1462 static ssize_t
video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1464 struct saa7146_fh
*fh
= file
->private_data
;
1465 struct saa7146_dev
*dev
= fh
->dev
;
1466 struct saa7146_vv
*vv
= dev
->vv_data
;
1469 DEB_EE(("called.\n"));
1471 if ((vv
->video_status
& STATUS_CAPTURE
) != 0) {
1472 /* fixme: should we allow read() captures while streaming capture? */
1473 if (vv
->video_fh
== fh
) {
1474 DEB_S(("already capturing.\n"));
1477 DEB_S(("already capturing in another open.\n"));
1481 ret
= video_begin(fh
);
1486 ret
= videobuf_read_one(&fh
->video_q
, data
, count
, ppos
,
1487 file
->f_flags
& O_NONBLOCK
);
1489 video_end(fh
, file
);
1491 ret
= video_end(fh
, file
);
1494 /* restart overlay if it was active before */
1495 if (vv
->ov_suspend
!= NULL
) {
1496 saa7146_start_preview(vv
->ov_suspend
);
1497 vv
->ov_suspend
= NULL
;
1503 struct saa7146_use_ops saa7146_video_uops
= {
1506 .release
= video_close
,
1507 .irq_done
= video_irq_done
,