include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / media / common / saa7146_video.c
blob3a00253fe1ee8bc6ab5e0e3fb89ef8cd451feef2
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,
22 .depth = 8,
23 .flags = 0,
24 }, {
25 .name = "RGB-16 (5/B-6/G-5/R)",
26 .pixelformat = V4L2_PIX_FMT_RGB565,
27 .trans = RGB16_COMPOSED,
28 .depth = 16,
29 .flags = 0,
30 }, {
31 .name = "RGB-24 (B-G-R)",
32 .pixelformat = V4L2_PIX_FMT_BGR24,
33 .trans = RGB24_COMPOSED,
34 .depth = 24,
35 .flags = 0,
36 }, {
37 .name = "RGB-32 (B-G-R)",
38 .pixelformat = V4L2_PIX_FMT_BGR32,
39 .trans = RGB32_COMPOSED,
40 .depth = 32,
41 .flags = 0,
42 }, {
43 .name = "RGB-32 (R-G-B)",
44 .pixelformat = V4L2_PIX_FMT_RGB32,
45 .trans = RGB32_COMPOSED,
46 .depth = 32,
47 .flags = 0,
48 .swap = 0x2,
49 }, {
50 .name = "Greyscale-8",
51 .pixelformat = V4L2_PIX_FMT_GREY,
52 .trans = Y8,
53 .depth = 8,
54 .flags = 0,
55 }, {
56 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
57 .pixelformat = V4L2_PIX_FMT_YUV422P,
58 .trans = YUV422_DECOMPOSED,
59 .depth = 16,
60 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
61 }, {
62 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
63 .pixelformat = V4L2_PIX_FMT_YVU420,
64 .trans = YUV420_DECOMPOSED,
65 .depth = 12,
66 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
67 }, {
68 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
69 .pixelformat = V4L2_PIX_FMT_YUV420,
70 .trans = YUV420_DECOMPOSED,
71 .depth = 12,
72 .flags = FORMAT_IS_PLANAR,
73 }, {
74 .name = "YUV 4:2:2 (U-Y-V-Y)",
75 .pixelformat = V4L2_PIX_FMT_UYVY,
76 .trans = YUV422_COMPOSED,
77 .depth = 16,
78 .flags = 0,
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) {
94 return formats+i;
98 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
99 return NULL;
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"));
116 return -EAGAIN;
119 /* check if streaming capture is running */
120 if (IS_CAPTURE_ACTIVE(fh) != 0) {
121 DEB_D(("streaming capture is active.\n"));
122 return -EBUSY;
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"));
129 return 0;
131 DEB_D(("overlay is already active in another open.\n"));
132 return -EBUSY;
135 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
136 DEB_D(("cannot get necessary overlay resources\n"));
137 return -EBUSY;
140 fmt.fmt.win = fh->ov.win;
141 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
142 if (0 != err) {
143 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
144 return -EBUSY;
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);
157 return ret;
160 vv->video_status = STATUS_OVERLAY;
161 vv->video_fh = fh;
163 return 0;
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"));
177 return -EBUSY;
180 /* check if overlay is running at all */
181 if ((vv->video_status & STATUS_OVERLAY) == 0) {
182 DEB_D(("no active overlay.\n"));
183 return 0;
186 if (vv->video_fh != fh) {
187 DEB_D(("overlay is active, but in another open.\n"));
188 return -EBUSY;
191 vv->video_status = 0;
192 vv->video_fh = NULL;
194 saa7146_disable_overlay(fh);
196 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
198 return 0;
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",
209 .minimum = 0,
210 .maximum = 255,
211 .step = 1,
212 .default_value = 128,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .flags = V4L2_CTRL_FLAG_SLIDER,
216 .id = V4L2_CID_CONTRAST,
217 .name = "Contrast",
218 .minimum = 0,
219 .maximum = 127,
220 .step = 1,
221 .default_value = 64,
222 .type = V4L2_CTRL_TYPE_INTEGER,
223 .flags = V4L2_CTRL_FLAG_SLIDER,
225 .id = V4L2_CID_SATURATION,
226 .name = "Saturation",
227 .minimum = 0,
228 .maximum = 127,
229 .step = 1,
230 .default_value = 64,
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 .flags = V4L2_CTRL_FLAG_SLIDER,
234 .id = V4L2_CID_VFLIP,
235 .name = "Vertical Flip",
236 .minimum = 0,
237 .maximum = 1,
238 .type = V4L2_CTRL_TYPE_BOOLEAN,
240 .id = V4L2_CID_HFLIP,
241 .name = "Horizontal Flip",
242 .minimum = 0,
243 .maximum = 1,
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)
253 int i;
255 for (i = 0; i < NUM_CONTROLS; i++)
256 if (controls[i].id == id)
257 return controls+i;
258 return NULL;
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;
279 __le32 fill;
281 int size = buf->fmt->width*buf->fmt->height;
282 int i,p,m1,m2,m3,o1,o2;
284 switch( sfmt->depth ) {
285 case 12: {
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;
290 o1 = size%PAGE_SIZE;
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));
293 break;
295 case 16: {
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;
300 o1 = size%PAGE_SIZE;
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));
303 break;
305 default: {
306 return -1;
310 ptr1 = pt1->cpu;
311 ptr2 = pt2->cpu;
312 ptr3 = pt3->cpu;
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);
321 ptr1 = pt1->cpu;
322 for(j=0;j<40;j++) {
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 */
334 ptr1 = pt1->cpu;
335 for(i = m1; i <= m2 ; i++, ptr2++) {
336 *ptr2 = ptr1[i];
338 fill = *(ptr2-1);
339 for(;i<1024;i++,ptr2++) {
340 *ptr2 = fill;
342 /* create video-dma3 page table */
343 ptr1 = pt1->cpu;
344 for(i = m2; i <= m3; i++,ptr3++) {
345 *ptr3 = ptr1[i];
347 fill = *(ptr3-1);
348 for(;i<1024;i++,ptr3++) {
349 *ptr3 = fill;
351 /* finally: finish up video-dma1 page table */
352 ptr1 = pt1->cpu+m1;
353 fill = pt1->cpu[m1];
354 for(i=m1;i<1024;i++,ptr1++) {
355 *ptr1 = fill;
358 ptr1 = pt1->cpu;
359 ptr2 = pt2->cpu;
360 ptr3 = pt3->cpu;
361 for(j=0;j<40;j++) {
362 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
364 for(j=0;j<40;j++) {
365 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
367 for(j=0;j<40;j++) {
368 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
371 } else {
372 struct saa7146_pgtable *pt = &buf->pt[0];
373 return saa7146_pgtable_build_single(pci, pt, list, length);
376 return 0;
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"));
396 return 0;
398 DEB_S(("already capturing in another open.\n"));
399 return -EBUSY;
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 */
406 if (0 != err) {
407 DEB_D(("suspending video failed. aborting\n"));
408 return err;
412 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
413 /* we need to have a valid format set here */
414 BUG_ON(NULL == fmt);
416 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
417 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
418 } else {
419 resource = RESOURCE_DMA1_HPS;
422 ret = saa7146_res_get(fh, resource);
423 if (0 == ret) {
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;
429 return -EBUSY;
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);
438 vv->video_fh = fh;
439 vv->video_status = STATUS_CAPTURE;
441 return 0;
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;
449 unsigned long flags;
450 unsigned int resource;
451 u32 dmas = 0;
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"));
456 return 0;
459 if (vv->video_fh != fh) {
460 DEB_S(("capturing, but in another open.\n"));
461 return -EBUSY;
464 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
465 /* we need to have a valid format set here */
466 BUG_ON(NULL == fmt);
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;
471 } else {
472 resource = RESOURCE_DMA1_HPS;
473 dmas = MASK_22;
475 spin_lock_irqsave(&dev->slock,flags);
477 /* disable rps0 */
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);
488 vv->video_fh = NULL;
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;
498 return 0;
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;
509 cap->capabilities =
510 V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_VIDEO_OVERLAY |
512 V4L2_CAP_READWRITE |
513 V4L2_CAP_STREAMING;
514 cap->capabilities |= dev->ext_vv_data->capabilities;
515 return 0;
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;
523 *fb = vv->ov_fb;
524 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
525 return 0;
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))
537 return -EPERM;
539 /* check args */
540 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
541 if (NULL == fmt)
542 return -EINVAL;
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"));
553 return -EBUSY;
557 /* ok, accept it */
558 vv->ov_fb = *fb;
559 vv->ov_fmt = fmt;
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));
565 return 0;
568 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
570 if (f->index >= NUM_FORMATS)
571 return -EINVAL;
572 strlcpy((char *)f->description, formats[f->index].name,
573 sizeof(f->description));
574 f->pixelformat = formats[f->index].pixelformat;
575 return 0;
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))
586 return -EINVAL;
588 ctrl = ctrl_by_id(c->id);
589 if (ctrl == NULL)
590 return -EINVAL;
592 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n", c->id));
593 *c = *ctrl;
594 return 0;
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;
602 u32 value = 0;
604 ctrl = ctrl_by_id(c->id);
605 if (NULL == ctrl)
606 return -EINVAL;
607 switch (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));
612 break;
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));
617 break;
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));
622 break;
623 case V4L2_CID_VFLIP:
624 c->value = vv->vflip;
625 DEB_D(("V4L2_CID_VFLIP: %d\n", c->value));
626 break;
627 case V4L2_CID_HFLIP:
628 c->value = vv->hflip;
629 DEB_D(("V4L2_CID_HFLIP: %d\n", c->value));
630 break;
631 default:
632 return -EINVAL;
634 return 0;
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);
644 if (NULL == ctrl) {
645 DEB_D(("unknown control %d\n", c->id));
646 return -EINVAL;
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;
657 break;
658 default:
659 /* nothing */;
662 switch (c->id) {
663 case V4L2_CID_BRIGHTNESS: {
664 u32 value = saa7146_read(dev, BCS_CTRL);
665 value &= 0x00ffffff;
666 value |= (c->value << 24);
667 saa7146_write(dev, BCS_CTRL, value);
668 saa7146_write(dev, MC2, MASK_22 | MASK_06);
669 break;
671 case V4L2_CID_CONTRAST: {
672 u32 value = saa7146_read(dev, BCS_CTRL);
673 value &= 0xff00ffff;
674 value |= (c->value << 16);
675 saa7146_write(dev, BCS_CTRL, value);
676 saa7146_write(dev, MC2, MASK_22 | MASK_06);
677 break;
679 case V4L2_CID_SATURATION: {
680 u32 value = saa7146_read(dev, BCS_CTRL);
681 value &= 0xffffff00;
682 value |= (c->value << 0);
683 saa7146_write(dev, BCS_CTRL, value);
684 saa7146_write(dev, MC2, MASK_22 | MASK_06);
685 break;
687 case V4L2_CID_HFLIP:
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"));
691 return -EBUSY;
693 vv->hflip = c->value;
694 break;
695 case V4L2_CID_VFLIP:
696 if (IS_CAPTURE_ACTIVE(fh) != 0) {
697 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
698 return -EBUSY;
700 vv->vflip = c->value;
701 break;
702 default:
703 return -EINVAL;
706 if (IS_OVERLAY_ACTIVE(fh) != 0) {
707 saa7146_stop_preview(fh);
708 saa7146_start_preview(fh);
710 return 0;
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);
722 return 0;
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;
728 return 0;
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;
734 return 0;
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;
740 return 0;
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;
749 int maxw, maxh;
750 int calc_bpl;
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);
755 if (NULL == fmt)
756 return -EINVAL;
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
765 : V4L2_FIELD_BOTTOM;
767 switch (field) {
768 case V4L2_FIELD_ALTERNATE:
769 vv->last_field = V4L2_FIELD_TOP;
770 maxh = maxh / 2;
771 break;
772 case V4L2_FIELD_TOP:
773 case V4L2_FIELD_BOTTOM:
774 vv->last_field = V4L2_FIELD_INTERLACED;
775 maxh = maxh / 2;
776 break;
777 case V4L2_FIELD_INTERLACED:
778 vv->last_field = V4L2_FIELD_INTERLACED;
779 break;
780 default:
781 DEB_D(("no known field mode '%d'.\n", field));
782 return -EINVAL;
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));
803 return 0;
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;
813 int maxw, maxh;
815 DEB_EE(("dev:%p\n", dev));
817 if (NULL == vv->ov_fb.base) {
818 DEB_D(("no fb base set.\n"));
819 return -EINVAL;
821 if (NULL == vv->ov_fmt) {
822 DEB_D(("no fb fmt set.\n"));
823 return -EINVAL;
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));
827 return -EINVAL;
829 if (win->clipcount > 16) {
830 DEB_D(("clipcount too big.\n"));
831 return -EINVAL;
834 field = win->field;
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
841 : V4L2_FIELD_TOP;
843 switch (field) {
844 case V4L2_FIELD_TOP:
845 case V4L2_FIELD_BOTTOM:
846 case V4L2_FIELD_ALTERNATE:
847 maxh = maxh / 2;
848 break;
849 case V4L2_FIELD_INTERLACED:
850 break;
851 default:
852 DEB_D(("no known field mode '%d'.\n", field));
853 return -EINVAL;
856 win->field = field;
857 if (win->w.width > maxw)
858 win->w.width = maxw;
859 if (win->w.height > maxh)
860 win->w.height = maxh;
862 return 0;
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;
870 int err;
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"));
875 return -EBUSY;
877 err = vidioc_try_fmt_vid_cap(file, fh, f);
878 if (0 != err)
879 return err;
880 fh->video_fmt = f->fmt.pix;
881 DEB_EE(("set to pixelformat '%4.4s'\n", (char *)&fh->video_fmt.pixelformat));
882 return 0;
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;
890 int err;
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);
894 if (0 != err)
895 return err;
896 fh->ov.win = f->fmt.win;
897 fh->ov.nclips = f->fmt.win.clipcount;
898 if (fh->ov.nclips > 16)
899 fh->ov.nclips = 16;
900 if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
901 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
902 return -EFAULT;
905 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
906 fh->ov.fh = fh;
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);
913 return 0;
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;
922 return 0;
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 */
929 case VIDIOC_ENUMSTD:
931 struct v4l2_standard *e = arg;
932 if (e->index < 0 )
933 return -EINVAL;
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);
937 return 0;
939 return -EINVAL;
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;
947 int found = 0;
948 int err, i;
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"));
954 return -EBUSY;
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 */
960 if (0 != err) {
961 DEB_D(("suspending video failed. aborting\n"));
962 return err;
966 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
967 if (*id & dev->ext_vv_data->stds[i].id)
968 break;
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);
973 found = 1;
976 if (vv->ov_suspend != NULL) {
977 saa7146_start_preview(vv->ov_suspend);
978 vv->ov_suspend = NULL;
981 if (!found) {
982 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
983 return -EINVAL;
986 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name));
987 return 0;
990 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
992 int err;
994 DEB_D(("VIDIOC_OVERLAY on:%d\n", on));
995 if (on)
996 err = saa7146_start_preview(fh);
997 else
998 err = saa7146_stop_preview(fh);
999 return err;
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);
1010 return -EINVAL;
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);
1021 return -EINVAL;
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);
1032 return -EINVAL;
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);
1043 return -EINVAL;
1046 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
1048 struct saa7146_fh *fh = __fh;
1049 int err;
1051 DEB_D(("VIDIOC_STREAMON, type:%d\n", type));
1053 err = video_begin(fh);
1054 if (err)
1055 return err;
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);
1060 return -EINVAL;
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;
1068 int err;
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"));
1077 return 0;
1080 if (vv->video_fh != fh) {
1081 DEB_S(("capturing, but in another open.\n"));
1082 return -EBUSY;
1085 err = -EINVAL;
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);
1090 if (0 != err) {
1091 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1092 video_end(fh, file);
1093 } else {
1094 err = video_end(fh, file);
1096 return err;
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;
1106 chip->revision = 0;
1107 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
1108 chip->ident = V4L2_IDENT_SAA7146;
1109 return 0;
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);
1158 return 0;
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;
1176 int size,err = 0;
1178 DEB_CAP(("vbuf:%p\n",vb));
1180 /* sanity checks */
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));
1186 return -EINVAL;
1189 size = fh->video_fmt.sizeimage;
1190 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1191 DEB_D(("size mismatch.\n"));
1192 return -EINVAL;
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]);
1225 } else {
1226 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1229 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1230 if (err)
1231 goto oops;
1232 err = saa7146_pgtable_build(dev,buf);
1233 if (err)
1234 goto oops;
1236 buf->vb.state = VIDEOBUF_PREPARED;
1237 buf->activate = buffer_activate;
1239 return 0;
1241 oops:
1242 DEB_D(("error out.\n"));
1243 saa7146_dma_free(dev,q,buf);
1245 return err;
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));
1265 return 0;
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);
1342 return 0;
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;
1351 int err;
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);
1359 videobuf_stop(q);
1361 /* hmm, why is this function declared void? */
1362 /* return err */
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;
1388 ssize_t ret = 0;
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"));
1396 return -EBUSY;
1398 DEB_S(("already capturing in another open.\n"));
1399 return -EBUSY;
1402 ret = video_begin(fh);
1403 if( 0 != ret) {
1404 goto out;
1407 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1408 file->f_flags & O_NONBLOCK);
1409 if (ret != 0) {
1410 video_end(fh, file);
1411 } else {
1412 ret = video_end(fh, file);
1414 out:
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;
1421 return ret;
1424 struct saa7146_use_ops saa7146_video_uops = {
1425 .init = video_init,
1426 .open = video_open,
1427 .release = video_close,
1428 .irq_done = video_irq_done,
1429 .read = video_read,