1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 
3 #include <media/drv-intf/saa7146_vv.h>
4 #include <media/v4l2-event.h>
5 #include <media/v4l2-ctrls.h>
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 
9 static int max_memory = 32;
10 
11 module_param(max_memory, int, 0644);
12 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
13 
14 #define IS_CAPTURE_ACTIVE(fh) \
15 	(((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
16 
17 #define IS_OVERLAY_ACTIVE(fh) \
18 	(((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
19 
20 /* format descriptions for capture and preview */
21 static struct saa7146_format formats[] = {
22 	{
23 		.name		= "RGB-8 (3-3-2)",
24 		.pixelformat	= V4L2_PIX_FMT_RGB332,
25 		.trans		= RGB08_COMPOSED,
26 		.depth		= 8,
27 		.flags		= 0,
28 	}, {
29 		.name		= "RGB-16 (5/B-6/G-5/R)",
30 		.pixelformat	= V4L2_PIX_FMT_RGB565,
31 		.trans		= RGB16_COMPOSED,
32 		.depth		= 16,
33 		.flags		= 0,
34 	}, {
35 		.name		= "RGB-24 (B-G-R)",
36 		.pixelformat	= V4L2_PIX_FMT_BGR24,
37 		.trans		= RGB24_COMPOSED,
38 		.depth		= 24,
39 		.flags		= 0,
40 	}, {
41 		.name		= "RGB-32 (B-G-R)",
42 		.pixelformat	= V4L2_PIX_FMT_BGR32,
43 		.trans		= RGB32_COMPOSED,
44 		.depth		= 32,
45 		.flags		= 0,
46 	}, {
47 		.name		= "RGB-32 (R-G-B)",
48 		.pixelformat	= V4L2_PIX_FMT_RGB32,
49 		.trans		= RGB32_COMPOSED,
50 		.depth		= 32,
51 		.flags		= 0,
52 		.swap		= 0x2,
53 	}, {
54 		.name		= "Greyscale-8",
55 		.pixelformat	= V4L2_PIX_FMT_GREY,
56 		.trans		= Y8,
57 		.depth		= 8,
58 		.flags		= 0,
59 	}, {
60 		.name		= "YUV 4:2:2 planar (Y-Cb-Cr)",
61 		.pixelformat	= V4L2_PIX_FMT_YUV422P,
62 		.trans		= YUV422_DECOMPOSED,
63 		.depth		= 16,
64 		.flags		= FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
65 	}, {
66 		.name		= "YVU 4:2:0 planar (Y-Cb-Cr)",
67 		.pixelformat	= V4L2_PIX_FMT_YVU420,
68 		.trans		= YUV420_DECOMPOSED,
69 		.depth		= 12,
70 		.flags		= FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
71 	}, {
72 		.name		= "YUV 4:2:0 planar (Y-Cb-Cr)",
73 		.pixelformat	= V4L2_PIX_FMT_YUV420,
74 		.trans		= YUV420_DECOMPOSED,
75 		.depth		= 12,
76 		.flags		= FORMAT_IS_PLANAR,
77 	}, {
78 		.name		= "YUV 4:2:2 (U-Y-V-Y)",
79 		.pixelformat	= V4L2_PIX_FMT_UYVY,
80 		.trans		= YUV422_COMPOSED,
81 		.depth		= 16,
82 		.flags		= 0,
83 	}
84 };
85 
86 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
87    due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
88    (like V4L2_PIX_FMT_YUYV) ... 8-( */
89 
90 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
91 {
92 	int i;
93 
94 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
95 		if (formats[i].pixelformat == fourcc) {
96 			return formats+i;
97 		}
98 	}
99 
100 	DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
101 	return NULL;
102 }
103 
104 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
105 
106 int saa7146_start_preview(struct saa7146_fh *fh)
107 {
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;
112 
113 	DEB_EE("dev:%p, fh:%p\n", dev, fh);
114 
115 	/* check if we have overlay information */
116 	if (vv->ov.fh == NULL) {
117 		DEB_D("no overlay data available. try S_FMT first.\n");
118 		return -EAGAIN;
119 	}
120 
121 	/* check if streaming capture is running */
122 	if (IS_CAPTURE_ACTIVE(fh) != 0) {
123 		DEB_D("streaming capture is active\n");
124 		return -EBUSY;
125 	}
126 
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");
131 			return 0;
132 		}
133 		DEB_D("overlay is already active in another open\n");
134 		return -EBUSY;
135 	}
136 
137 	if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
138 		DEB_D("cannot get necessary overlay resources\n");
139 		return -EBUSY;
140 	}
141 
142 	fmt.fmt.win = vv->ov.win;
143 	err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
144 	if (0 != err) {
145 		saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
146 		return -EBUSY;
147 	}
148 	vv->ov.win = fmt.fmt.win;
149 
150 	DEB_D("%dx%d+%d+%d %s field=%s\n",
151 	      vv->ov.win.w.width, vv->ov.win.w.height,
152 	      vv->ov.win.w.left, vv->ov.win.w.top,
153 	      vv->ov_fmt->name, v4l2_field_names[vv->ov.win.field]);
154 
155 	if (0 != (ret = saa7146_enable_overlay(fh))) {
156 		DEB_D("enabling overlay failed: %d\n", ret);
157 		saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
158 		return ret;
159 	}
160 
161 	vv->video_status = STATUS_OVERLAY;
162 	vv->video_fh = fh;
163 
164 	return 0;
165 }
166 EXPORT_SYMBOL_GPL(saa7146_start_preview);
167 
168 int saa7146_stop_preview(struct saa7146_fh *fh)
169 {
170 	struct saa7146_dev *dev = fh->dev;
171 	struct saa7146_vv *vv = dev->vv_data;
172 
173 	DEB_EE("dev:%p, fh:%p\n", dev, fh);
174 
175 	/* check if streaming capture is running */
176 	if (IS_CAPTURE_ACTIVE(fh) != 0) {
177 		DEB_D("streaming capture is active\n");
178 		return -EBUSY;
179 	}
180 
181 	/* check if overlay is running at all */
182 	if ((vv->video_status & STATUS_OVERLAY) == 0) {
183 		DEB_D("no active overlay\n");
184 		return 0;
185 	}
186 
187 	if (vv->video_fh != fh) {
188 		DEB_D("overlay is active, but in another open\n");
189 		return -EBUSY;
190 	}
191 
192 	vv->video_status = 0;
193 	vv->video_fh = NULL;
194 
195 	saa7146_disable_overlay(fh);
196 
197 	saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
198 
199 	return 0;
200 }
201 EXPORT_SYMBOL_GPL(saa7146_stop_preview);
202 
203 /********************************************************************************/
204 /* common pagetable functions */
205 
206 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
207 {
208 	struct pci_dev *pci = dev->pci;
209 	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
210 	struct scatterlist *list = dma->sglist;
211 	int length = dma->sglen;
212 	struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
213 
214 	DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
215 
216 	if( 0 != IS_PLANAR(sfmt->trans)) {
217 		struct saa7146_pgtable *pt1 = &buf->pt[0];
218 		struct saa7146_pgtable *pt2 = &buf->pt[1];
219 		struct saa7146_pgtable *pt3 = &buf->pt[2];
220 		__le32  *ptr1, *ptr2, *ptr3;
221 		__le32 fill;
222 
223 		int size = buf->fmt->width*buf->fmt->height;
224 		int i,p,m1,m2,m3,o1,o2;
225 
226 		switch( sfmt->depth ) {
227 			case 12: {
228 				/* create some offsets inside the page table */
229 				m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
230 				m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
231 				m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
232 				o1 = size%PAGE_SIZE;
233 				o2 = (size+(size/4))%PAGE_SIZE;
234 				DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
235 					size, m1, m2, m3, o1, o2);
236 				break;
237 			}
238 			case 16: {
239 				/* create some offsets inside the page table */
240 				m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
241 				m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
242 				m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
243 				o1 = size%PAGE_SIZE;
244 				o2 = (size+(size/2))%PAGE_SIZE;
245 				DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
246 					size, m1, m2, m3, o1, o2);
247 				break;
248 			}
249 			default: {
250 				return -1;
251 			}
252 		}
253 
254 		ptr1 = pt1->cpu;
255 		ptr2 = pt2->cpu;
256 		ptr3 = pt3->cpu;
257 
258 		/* walk all pages, copy all page addresses to ptr1 */
259 		for (i = 0; i < length; i++, list++) {
260 			for (p = 0; p * 4096 < list->length; p++, ptr1++) {
261 				*ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
262 			}
263 		}
264 /*
265 		ptr1 = pt1->cpu;
266 		for(j=0;j<40;j++) {
267 			printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
268 		}
269 */
270 
271 		/* if we have a user buffer, the first page may not be
272 		   aligned to a page boundary. */
273 		pt1->offset = dma->sglist->offset;
274 		pt2->offset = pt1->offset+o1;
275 		pt3->offset = pt1->offset+o2;
276 
277 		/* create video-dma2 page table */
278 		ptr1 = pt1->cpu;
279 		for(i = m1; i <= m2 ; i++, ptr2++) {
280 			*ptr2 = ptr1[i];
281 		}
282 		fill = *(ptr2-1);
283 		for(;i<1024;i++,ptr2++) {
284 			*ptr2 = fill;
285 		}
286 		/* create video-dma3 page table */
287 		ptr1 = pt1->cpu;
288 		for(i = m2; i <= m3; i++,ptr3++) {
289 			*ptr3 = ptr1[i];
290 		}
291 		fill = *(ptr3-1);
292 		for(;i<1024;i++,ptr3++) {
293 			*ptr3 = fill;
294 		}
295 		/* finally: finish up video-dma1 page table */
296 		ptr1 = pt1->cpu+m1;
297 		fill = pt1->cpu[m1];
298 		for(i=m1;i<1024;i++,ptr1++) {
299 			*ptr1 = fill;
300 		}
301 /*
302 		ptr1 = pt1->cpu;
303 		ptr2 = pt2->cpu;
304 		ptr3 = pt3->cpu;
305 		for(j=0;j<40;j++) {
306 			printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
307 		}
308 		for(j=0;j<40;j++) {
309 			printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
310 		}
311 		for(j=0;j<40;j++) {
312 			printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
313 		}
314 */
315 	} else {
316 		struct saa7146_pgtable *pt = &buf->pt[0];
317 		return saa7146_pgtable_build_single(pci, pt, list, length);
318 	}
319 
320 	return 0;
321 }
322 
323 
324 /********************************************************************************/
325 /* file operations */
326 
327 static int video_begin(struct saa7146_fh *fh)
328 {
329 	struct saa7146_dev *dev = fh->dev;
330 	struct saa7146_vv *vv = dev->vv_data;
331 	struct saa7146_format *fmt = NULL;
332 	unsigned int resource;
333 	int ret = 0, err = 0;
334 
335 	DEB_EE("dev:%p, fh:%p\n", dev, fh);
336 
337 	if ((vv->video_status & STATUS_CAPTURE) != 0) {
338 		if (vv->video_fh == fh) {
339 			DEB_S("already capturing\n");
340 			return 0;
341 		}
342 		DEB_S("already capturing in another open\n");
343 		return -EBUSY;
344 	}
345 
346 	if ((vv->video_status & STATUS_OVERLAY) != 0) {
347 		DEB_S("warning: suspending overlay video for streaming capture\n");
348 		vv->ov_suspend = vv->video_fh;
349 		err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
350 		if (0 != err) {
351 			DEB_D("suspending video failed. aborting\n");
352 			return err;
353 		}
354 	}
355 
356 	fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
357 	/* we need to have a valid format set here */
358 	BUG_ON(NULL == fmt);
359 
360 	if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
361 		resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
362 	} else {
363 		resource = RESOURCE_DMA1_HPS;
364 	}
365 
366 	ret = saa7146_res_get(fh, resource);
367 	if (0 == ret) {
368 		DEB_S("cannot get capture resource %d\n", resource);
369 		if (vv->ov_suspend != NULL) {
370 			saa7146_start_preview(vv->ov_suspend);
371 			vv->ov_suspend = NULL;
372 		}
373 		return -EBUSY;
374 	}
375 
376 	/* clear out beginning of streaming bit (rps register 0)*/
377 	saa7146_write(dev, MC2, MASK_27 );
378 
379 	/* enable rps0 irqs */
380 	SAA7146_IER_ENABLE(dev, MASK_27);
381 
382 	vv->video_fh = fh;
383 	vv->video_status = STATUS_CAPTURE;
384 
385 	return 0;
386 }
387 
388 static int video_end(struct saa7146_fh *fh, struct file *file)
389 {
390 	struct saa7146_dev *dev = fh->dev;
391 	struct saa7146_vv *vv = dev->vv_data;
392 	struct saa7146_dmaqueue *q = &vv->video_dmaq;
393 	struct saa7146_format *fmt = NULL;
394 	unsigned long flags;
395 	unsigned int resource;
396 	u32 dmas = 0;
397 	DEB_EE("dev:%p, fh:%p\n", dev, fh);
398 
399 	if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
400 		DEB_S("not capturing\n");
401 		return 0;
402 	}
403 
404 	if (vv->video_fh != fh) {
405 		DEB_S("capturing, but in another open\n");
406 		return -EBUSY;
407 	}
408 
409 	fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
410 	/* we need to have a valid format set here */
411 	BUG_ON(NULL == fmt);
412 
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;
416 	} else {
417 		resource = RESOURCE_DMA1_HPS;
418 		dmas = MASK_22;
419 	}
420 	spin_lock_irqsave(&dev->slock,flags);
421 
422 	/* disable rps0  */
423 	saa7146_write(dev, MC1, MASK_28);
424 
425 	/* disable rps0 irqs */
426 	SAA7146_IER_DISABLE(dev, MASK_27);
427 
428 	/* shut down all used video dma transfers */
429 	saa7146_write(dev, MC1, dmas);
430 
431 	if (q->curr)
432 		saa7146_buffer_finish(dev, q, VIDEOBUF_DONE);
433 
434 	spin_unlock_irqrestore(&dev->slock, flags);
435 
436 	vv->video_fh = NULL;
437 	vv->video_status = 0;
438 
439 	saa7146_res_free(fh, resource);
440 
441 	if (vv->ov_suspend != NULL) {
442 		saa7146_start_preview(vv->ov_suspend);
443 		vv->ov_suspend = NULL;
444 	}
445 
446 	return 0;
447 }
448 
449 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
450 {
451 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
452 
453 	strscpy((char *)cap->driver, "saa7146 v4l2", sizeof(cap->driver));
454 	strscpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
455 	sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
456 	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
457 			    V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
458 			    V4L2_CAP_DEVICE_CAPS;
459 	cap->capabilities |= dev->ext_vv_data->capabilities;
460 	return 0;
461 }
462 
463 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
464 {
465 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
466 	struct saa7146_vv *vv = dev->vv_data;
467 
468 	*fb = vv->ov_fb;
469 	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
470 	fb->flags = V4L2_FBUF_FLAG_PRIMARY;
471 	return 0;
472 }
473 
474 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
475 {
476 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
477 	struct saa7146_vv *vv = dev->vv_data;
478 	struct saa7146_format *fmt;
479 
480 	DEB_EE("VIDIOC_S_FBUF\n");
481 
482 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
483 		return -EPERM;
484 
485 	/* check args */
486 	fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
487 	if (NULL == fmt)
488 		return -EINVAL;
489 
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);
494 
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 information while overlay is active in another open\n");
499 			return -EBUSY;
500 		}
501 	}
502 
503 	/* ok, accept it */
504 	vv->ov_fb = *fb;
505 	vv->ov_fmt = fmt;
506 
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);
510 	}
511 	return 0;
512 }
513 
514 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
515 {
516 	if (f->index >= ARRAY_SIZE(formats))
517 		return -EINVAL;
518 	strscpy((char *)f->description, formats[f->index].name,
519 		sizeof(f->description));
520 	f->pixelformat = formats[f->index].pixelformat;
521 	return 0;
522 }
523 
524 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
525 {
526 	struct saa7146_dev *dev = container_of(ctrl->handler,
527 				struct saa7146_dev, ctrl_handler);
528 	struct saa7146_vv *vv = dev->vv_data;
529 	u32 val;
530 
531 	switch (ctrl->id) {
532 	case V4L2_CID_BRIGHTNESS:
533 		val = saa7146_read(dev, BCS_CTRL);
534 		val &= 0x00ffffff;
535 		val |= (ctrl->val << 24);
536 		saa7146_write(dev, BCS_CTRL, val);
537 		saa7146_write(dev, MC2, MASK_22 | MASK_06);
538 		break;
539 
540 	case V4L2_CID_CONTRAST:
541 		val = saa7146_read(dev, BCS_CTRL);
542 		val &= 0xff00ffff;
543 		val |= (ctrl->val << 16);
544 		saa7146_write(dev, BCS_CTRL, val);
545 		saa7146_write(dev, MC2, MASK_22 | MASK_06);
546 		break;
547 
548 	case V4L2_CID_SATURATION:
549 		val = saa7146_read(dev, BCS_CTRL);
550 		val &= 0xffffff00;
551 		val |= (ctrl->val << 0);
552 		saa7146_write(dev, BCS_CTRL, val);
553 		saa7146_write(dev, MC2, MASK_22 | MASK_06);
554 		break;
555 
556 	case V4L2_CID_HFLIP:
557 		/* fixme: we can support changing VFLIP and HFLIP here... */
558 		if ((vv->video_status & STATUS_CAPTURE))
559 			return -EBUSY;
560 		vv->hflip = ctrl->val;
561 		break;
562 
563 	case V4L2_CID_VFLIP:
564 		if ((vv->video_status & STATUS_CAPTURE))
565 			return -EBUSY;
566 		vv->vflip = ctrl->val;
567 		break;
568 
569 	default:
570 		return -EINVAL;
571 	}
572 
573 	if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
574 		struct saa7146_fh *fh = vv->video_fh;
575 
576 		saa7146_stop_preview(fh);
577 		saa7146_start_preview(fh);
578 	}
579 	return 0;
580 }
581 
582 static int vidioc_g_parm(struct file *file, void *fh,
583 		struct v4l2_streamparm *parm)
584 {
585 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
586 	struct saa7146_vv *vv = dev->vv_data;
587 
588 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
589 		return -EINVAL;
590 	parm->parm.capture.readbuffers = 1;
591 	v4l2_video_std_frame_period(vv->standard->id,
592 				    &parm->parm.capture.timeperframe);
593 	return 0;
594 }
595 
596 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
597 {
598 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
599 	struct saa7146_vv *vv = dev->vv_data;
600 
601 	f->fmt.pix = vv->video_fmt;
602 	return 0;
603 }
604 
605 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
606 {
607 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
608 	struct saa7146_vv *vv = dev->vv_data;
609 
610 	f->fmt.win = vv->ov.win;
611 	return 0;
612 }
613 
614 static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
615 {
616 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
617 	struct saa7146_vv *vv = dev->vv_data;
618 
619 	f->fmt.vbi = vv->vbi_fmt;
620 	return 0;
621 }
622 
623 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
624 {
625 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
626 	struct saa7146_vv *vv = dev->vv_data;
627 	struct saa7146_format *fmt;
628 	enum v4l2_field field;
629 	int maxw, maxh;
630 	int calc_bpl;
631 
632 	DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
633 
634 	fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
635 	if (NULL == fmt)
636 		return -EINVAL;
637 
638 	field = f->fmt.pix.field;
639 	maxw  = vv->standard->h_max_out;
640 	maxh  = vv->standard->v_max_out;
641 
642 	if (V4L2_FIELD_ANY == field) {
643 		field = (f->fmt.pix.height > maxh / 2)
644 			? V4L2_FIELD_INTERLACED
645 			: V4L2_FIELD_BOTTOM;
646 	}
647 	switch (field) {
648 	case V4L2_FIELD_ALTERNATE:
649 		vv->last_field = V4L2_FIELD_TOP;
650 		maxh = maxh / 2;
651 		break;
652 	case V4L2_FIELD_TOP:
653 	case V4L2_FIELD_BOTTOM:
654 		vv->last_field = V4L2_FIELD_INTERLACED;
655 		maxh = maxh / 2;
656 		break;
657 	case V4L2_FIELD_INTERLACED:
658 		vv->last_field = V4L2_FIELD_INTERLACED;
659 		break;
660 	default:
661 		DEB_D("no known field mode '%d'\n", field);
662 		return -EINVAL;
663 	}
664 
665 	f->fmt.pix.field = field;
666 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
667 	if (f->fmt.pix.width > maxw)
668 		f->fmt.pix.width = maxw;
669 	if (f->fmt.pix.height > maxh)
670 		f->fmt.pix.height = maxh;
671 
672 	calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
673 
674 	if (f->fmt.pix.bytesperline < calc_bpl)
675 		f->fmt.pix.bytesperline = calc_bpl;
676 
677 	if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
678 		f->fmt.pix.bytesperline = calc_bpl;
679 
680 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
681 	DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
682 	      f->fmt.pix.width, f->fmt.pix.height,
683 	      f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
684 
685 	return 0;
686 }
687 
688 
689 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
690 {
691 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
692 	struct saa7146_vv *vv = dev->vv_data;
693 	struct v4l2_window *win = &f->fmt.win;
694 	enum v4l2_field field;
695 	int maxw, maxh;
696 
697 	DEB_EE("dev:%p\n", dev);
698 
699 	if (NULL == vv->ov_fb.base) {
700 		DEB_D("no fb base set\n");
701 		return -EINVAL;
702 	}
703 	if (NULL == vv->ov_fmt) {
704 		DEB_D("no fb fmt set\n");
705 		return -EINVAL;
706 	}
707 	if (win->w.width < 48 || win->w.height < 32) {
708 		DEB_D("min width/height. (%d,%d)\n",
709 		      win->w.width, win->w.height);
710 		return -EINVAL;
711 	}
712 	if (win->clipcount > 16) {
713 		DEB_D("clipcount too big\n");
714 		return -EINVAL;
715 	}
716 
717 	field = win->field;
718 	maxw  = vv->standard->h_max_out;
719 	maxh  = vv->standard->v_max_out;
720 
721 	if (V4L2_FIELD_ANY == field) {
722 		field = (win->w.height > maxh / 2)
723 			? V4L2_FIELD_INTERLACED
724 			: V4L2_FIELD_TOP;
725 		}
726 	switch (field) {
727 	case V4L2_FIELD_TOP:
728 	case V4L2_FIELD_BOTTOM:
729 	case V4L2_FIELD_ALTERNATE:
730 		maxh = maxh / 2;
731 		break;
732 	case V4L2_FIELD_INTERLACED:
733 		break;
734 	default:
735 		DEB_D("no known field mode '%d'\n", field);
736 		return -EINVAL;
737 	}
738 
739 	win->field = field;
740 	if (win->w.width > maxw)
741 		win->w.width = maxw;
742 	if (win->w.height > maxh)
743 		win->w.height = maxh;
744 
745 	return 0;
746 }
747 
748 static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
749 {
750 	struct saa7146_fh *fh = __fh;
751 	struct saa7146_dev *dev = fh->dev;
752 	struct saa7146_vv *vv = dev->vv_data;
753 	int err;
754 
755 	DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
756 	if (IS_CAPTURE_ACTIVE(fh) != 0) {
757 		DEB_EE("streaming capture is active\n");
758 		return -EBUSY;
759 	}
760 	err = vidioc_try_fmt_vid_cap(file, fh, f);
761 	if (0 != err)
762 		return err;
763 	vv->video_fmt = f->fmt.pix;
764 	DEB_EE("set to pixelformat '%4.4s'\n",
765 	       (char *)&vv->video_fmt.pixelformat);
766 	return 0;
767 }
768 
769 static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
770 {
771 	struct saa7146_fh *fh = __fh;
772 	struct saa7146_dev *dev = fh->dev;
773 	struct saa7146_vv *vv = dev->vv_data;
774 	int err;
775 
776 	DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
777 	err = vidioc_try_fmt_vid_overlay(file, fh, f);
778 	if (0 != err)
779 		return err;
780 	vv->ov.win    = f->fmt.win;
781 	vv->ov.nclips = f->fmt.win.clipcount;
782 	if (vv->ov.nclips > 16)
783 		vv->ov.nclips = 16;
784 	if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
785 				sizeof(struct v4l2_clip) * vv->ov.nclips)) {
786 		return -EFAULT;
787 	}
788 
789 	/* vv->ov.fh is used to indicate that we have valid overlay information, too */
790 	vv->ov.fh = fh;
791 
792 	/* check if our current overlay is active */
793 	if (IS_OVERLAY_ACTIVE(fh) != 0) {
794 		saa7146_stop_preview(fh);
795 		saa7146_start_preview(fh);
796 	}
797 	return 0;
798 }
799 
800 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
801 {
802 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
803 	struct saa7146_vv *vv = dev->vv_data;
804 
805 	*norm = vv->standard->id;
806 	return 0;
807 }
808 
809 	/* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
810 	   PAL / NTSC / SECAM. if your hardware does not (or does more)
811 	   -- override this function in your extension */
812 /*
813 	case VIDIOC_ENUMSTD:
814 	{
815 		struct v4l2_standard *e = arg;
816 		if (e->index < 0 )
817 			return -EINVAL;
818 		if( e->index < dev->ext_vv_data->num_stds ) {
819 			DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
820 			v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
821 			return 0;
822 		}
823 		return -EINVAL;
824 	}
825 	*/
826 
827 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
828 {
829 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
830 	struct saa7146_vv *vv = dev->vv_data;
831 	int found = 0;
832 	int err, i;
833 
834 	DEB_EE("VIDIOC_S_STD\n");
835 
836 	if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
837 		DEB_D("cannot change video standard while streaming capture is active\n");
838 		return -EBUSY;
839 	}
840 
841 	if ((vv->video_status & STATUS_OVERLAY) != 0) {
842 		vv->ov_suspend = vv->video_fh;
843 		err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
844 		if (0 != err) {
845 			DEB_D("suspending video failed. aborting\n");
846 			return err;
847 		}
848 	}
849 
850 	for (i = 0; i < dev->ext_vv_data->num_stds; i++)
851 		if (id & dev->ext_vv_data->stds[i].id)
852 			break;
853 	if (i != dev->ext_vv_data->num_stds) {
854 		vv->standard = &dev->ext_vv_data->stds[i];
855 		if (NULL != dev->ext_vv_data->std_callback)
856 			dev->ext_vv_data->std_callback(dev, vv->standard);
857 		found = 1;
858 	}
859 
860 	if (vv->ov_suspend != NULL) {
861 		saa7146_start_preview(vv->ov_suspend);
862 		vv->ov_suspend = NULL;
863 	}
864 
865 	if (!found) {
866 		DEB_EE("VIDIOC_S_STD: standard not found\n");
867 		return -EINVAL;
868 	}
869 
870 	DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
871 	return 0;
872 }
873 
874 static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
875 {
876 	int err;
877 
878 	DEB_D("VIDIOC_OVERLAY on:%d\n", on);
879 	if (on)
880 		err = saa7146_start_preview(fh);
881 	else
882 		err = saa7146_stop_preview(fh);
883 	return err;
884 }
885 
886 static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
887 {
888 	struct saa7146_fh *fh = __fh;
889 
890 	if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 		return videobuf_reqbufs(&fh->video_q, b);
892 	if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
893 		return videobuf_reqbufs(&fh->vbi_q, b);
894 	return -EINVAL;
895 }
896 
897 static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
898 {
899 	struct saa7146_fh *fh = __fh;
900 
901 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
902 		return videobuf_querybuf(&fh->video_q, buf);
903 	if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
904 		return videobuf_querybuf(&fh->vbi_q, buf);
905 	return -EINVAL;
906 }
907 
908 static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
909 {
910 	struct saa7146_fh *fh = __fh;
911 
912 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
913 		return videobuf_qbuf(&fh->video_q, buf);
914 	if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
915 		return videobuf_qbuf(&fh->vbi_q, buf);
916 	return -EINVAL;
917 }
918 
919 static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
920 {
921 	struct saa7146_fh *fh = __fh;
922 
923 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
924 		return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
925 	if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
926 		return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
927 	return -EINVAL;
928 }
929 
930 static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
931 {
932 	struct saa7146_fh *fh = __fh;
933 	int err;
934 
935 	DEB_D("VIDIOC_STREAMON, type:%d\n", type);
936 
937 	err = video_begin(fh);
938 	if (err)
939 		return err;
940 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
941 		return videobuf_streamon(&fh->video_q);
942 	if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
943 		return videobuf_streamon(&fh->vbi_q);
944 	return -EINVAL;
945 }
946 
947 static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
948 {
949 	struct saa7146_fh *fh = __fh;
950 	struct saa7146_dev *dev = fh->dev;
951 	struct saa7146_vv *vv = dev->vv_data;
952 	int err;
953 
954 	DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
955 
956 	/* ugly: we need to copy some checks from video_end(),
957 	   because videobuf_streamoff() relies on the capture running.
958 	   check and fix this */
959 	if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
960 		DEB_S("not capturing\n");
961 		return 0;
962 	}
963 
964 	if (vv->video_fh != fh) {
965 		DEB_S("capturing, but in another open\n");
966 		return -EBUSY;
967 	}
968 
969 	err = -EINVAL;
970 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
971 		err = videobuf_streamoff(&fh->video_q);
972 	else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
973 		err = videobuf_streamoff(&fh->vbi_q);
974 	if (0 != err) {
975 		DEB_D("warning: videobuf_streamoff() failed\n");
976 		video_end(fh, file);
977 	} else {
978 		err = video_end(fh, file);
979 	}
980 	return err;
981 }
982 
983 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
984 	.vidioc_querycap             = vidioc_querycap,
985 	.vidioc_enum_fmt_vid_cap     = vidioc_enum_fmt_vid_cap,
986 	.vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
987 	.vidioc_g_fmt_vid_cap        = vidioc_g_fmt_vid_cap,
988 	.vidioc_try_fmt_vid_cap      = vidioc_try_fmt_vid_cap,
989 	.vidioc_s_fmt_vid_cap        = vidioc_s_fmt_vid_cap,
990 	.vidioc_g_fmt_vid_overlay    = vidioc_g_fmt_vid_overlay,
991 	.vidioc_try_fmt_vid_overlay  = vidioc_try_fmt_vid_overlay,
992 	.vidioc_s_fmt_vid_overlay    = vidioc_s_fmt_vid_overlay,
993 
994 	.vidioc_overlay		     = vidioc_overlay,
995 	.vidioc_g_fbuf		     = vidioc_g_fbuf,
996 	.vidioc_s_fbuf		     = vidioc_s_fbuf,
997 	.vidioc_reqbufs              = vidioc_reqbufs,
998 	.vidioc_querybuf             = vidioc_querybuf,
999 	.vidioc_qbuf                 = vidioc_qbuf,
1000 	.vidioc_dqbuf                = vidioc_dqbuf,
1001 	.vidioc_g_std                = vidioc_g_std,
1002 	.vidioc_s_std                = vidioc_s_std,
1003 	.vidioc_streamon             = vidioc_streamon,
1004 	.vidioc_streamoff            = vidioc_streamoff,
1005 	.vidioc_g_parm		     = vidioc_g_parm,
1006 	.vidioc_subscribe_event      = v4l2_ctrl_subscribe_event,
1007 	.vidioc_unsubscribe_event    = v4l2_event_unsubscribe,
1008 };
1009 
1010 const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
1011 	.vidioc_querycap             = vidioc_querycap,
1012 	.vidioc_g_fmt_vbi_cap        = vidioc_g_fmt_vbi_cap,
1013 
1014 	.vidioc_reqbufs              = vidioc_reqbufs,
1015 	.vidioc_querybuf             = vidioc_querybuf,
1016 	.vidioc_qbuf                 = vidioc_qbuf,
1017 	.vidioc_dqbuf                = vidioc_dqbuf,
1018 	.vidioc_g_std                = vidioc_g_std,
1019 	.vidioc_s_std                = vidioc_s_std,
1020 	.vidioc_streamon             = vidioc_streamon,
1021 	.vidioc_streamoff            = vidioc_streamoff,
1022 	.vidioc_g_parm		     = vidioc_g_parm,
1023 	.vidioc_subscribe_event      = v4l2_ctrl_subscribe_event,
1024 	.vidioc_unsubscribe_event    = v4l2_event_unsubscribe,
1025 };
1026 
1027 /*********************************************************************************/
1028 /* buffer handling functions                                                  */
1029 
1030 static int buffer_activate (struct saa7146_dev *dev,
1031 		     struct saa7146_buf *buf,
1032 		     struct saa7146_buf *next)
1033 {
1034 	struct saa7146_vv *vv = dev->vv_data;
1035 
1036 	buf->vb.state = VIDEOBUF_ACTIVE;
1037 	saa7146_set_capture(dev,buf,next);
1038 
1039 	mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
1040 	return 0;
1041 }
1042 
1043 static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1044 {
1045 	saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1046 	saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1047 	saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1048 }
1049 
1050 static int buffer_prepare(struct videobuf_queue *q,
1051 			  struct videobuf_buffer *vb, enum v4l2_field field)
1052 {
1053 	struct file *file = q->priv_data;
1054 	struct saa7146_fh *fh = file->private_data;
1055 	struct saa7146_dev *dev = fh->dev;
1056 	struct saa7146_vv *vv = dev->vv_data;
1057 	struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1058 	int size,err = 0;
1059 
1060 	DEB_CAP("vbuf:%p\n", vb);
1061 
1062 	/* sanity checks */
1063 	if (vv->video_fmt.width  < 48 ||
1064 	    vv->video_fmt.height < 32 ||
1065 	    vv->video_fmt.width  > vv->standard->h_max_out ||
1066 	    vv->video_fmt.height > vv->standard->v_max_out) {
1067 		DEB_D("w (%d) / h (%d) out of bounds\n",
1068 		      vv->video_fmt.width, vv->video_fmt.height);
1069 		return -EINVAL;
1070 	}
1071 
1072 	size = vv->video_fmt.sizeimage;
1073 	if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1074 		DEB_D("size mismatch\n");
1075 		return -EINVAL;
1076 	}
1077 
1078 	DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1079 		vv->video_fmt.width, vv->video_fmt.height,
1080 		size, v4l2_field_names[vv->video_fmt.field]);
1081 	if (buf->vb.width  != vv->video_fmt.width  ||
1082 	    buf->vb.bytesperline != vv->video_fmt.bytesperline ||
1083 	    buf->vb.height != vv->video_fmt.height ||
1084 	    buf->vb.size   != size ||
1085 	    buf->vb.field  != field      ||
1086 	    buf->vb.field  != vv->video_fmt.field  ||
1087 	    buf->fmt       != &vv->video_fmt) {
1088 		saa7146_dma_free(dev,q,buf);
1089 	}
1090 
1091 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1092 		struct saa7146_format *sfmt;
1093 
1094 		buf->vb.bytesperline  = vv->video_fmt.bytesperline;
1095 		buf->vb.width  = vv->video_fmt.width;
1096 		buf->vb.height = vv->video_fmt.height;
1097 		buf->vb.size   = size;
1098 		buf->vb.field  = field;
1099 		buf->fmt       = &vv->video_fmt;
1100 		buf->vb.field  = vv->video_fmt.field;
1101 
1102 		sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
1103 
1104 		release_all_pagetables(dev, buf);
1105 		if( 0 != IS_PLANAR(sfmt->trans)) {
1106 			saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1107 			saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1108 			saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1109 		} else {
1110 			saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1111 		}
1112 
1113 		err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
1114 		if (err)
1115 			goto oops;
1116 		err = saa7146_pgtable_build(dev,buf);
1117 		if (err)
1118 			goto oops;
1119 	}
1120 	buf->vb.state = VIDEOBUF_PREPARED;
1121 	buf->activate = buffer_activate;
1122 
1123 	return 0;
1124 
1125  oops:
1126 	DEB_D("error out\n");
1127 	saa7146_dma_free(dev,q,buf);
1128 
1129 	return err;
1130 }
1131 
1132 static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1133 {
1134 	struct file *file = q->priv_data;
1135 	struct saa7146_fh *fh = file->private_data;
1136 	struct saa7146_vv *vv = fh->dev->vv_data;
1137 
1138 	if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1139 		*count = MAX_SAA7146_CAPTURE_BUFFERS;
1140 
1141 	*size = vv->video_fmt.sizeimage;
1142 
1143 	/* check if we exceed the "max_memory" parameter */
1144 	if( (*count * *size) > (max_memory*1048576) ) {
1145 		*count = (max_memory*1048576) / *size;
1146 	}
1147 
1148 	DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
1149 
1150 	return 0;
1151 }
1152 
1153 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1154 {
1155 	struct file *file = q->priv_data;
1156 	struct saa7146_fh *fh = file->private_data;
1157 	struct saa7146_dev *dev = fh->dev;
1158 	struct saa7146_vv *vv = dev->vv_data;
1159 	struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1160 
1161 	DEB_CAP("vbuf:%p\n", vb);
1162 	saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
1163 }
1164 
1165 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1166 {
1167 	struct file *file = q->priv_data;
1168 	struct saa7146_fh *fh = file->private_data;
1169 	struct saa7146_dev *dev = fh->dev;
1170 	struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1171 
1172 	DEB_CAP("vbuf:%p\n", vb);
1173 
1174 	saa7146_dma_free(dev,q,buf);
1175 
1176 	release_all_pagetables(dev, buf);
1177 }
1178 
1179 static const struct videobuf_queue_ops video_qops = {
1180 	.buf_setup    = buffer_setup,
1181 	.buf_prepare  = buffer_prepare,
1182 	.buf_queue    = buffer_queue,
1183 	.buf_release  = buffer_release,
1184 };
1185 
1186 /********************************************************************************/
1187 /* file operations */
1188 
1189 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1190 {
1191 	INIT_LIST_HEAD(&vv->video_dmaq.queue);
1192 
1193 	timer_setup(&vv->video_dmaq.timeout, saa7146_buffer_timeout, 0);
1194 	vv->video_dmaq.dev              = dev;
1195 
1196 	/* set some default values */
1197 	vv->standard = &dev->ext_vv_data->stds[0];
1198 
1199 	/* FIXME: what's this? */
1200 	vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1201 	vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1202 }
1203 
1204 
1205 static int video_open(struct saa7146_dev *dev, struct file *file)
1206 {
1207 	struct saa7146_fh *fh = file->private_data;
1208 
1209 	videobuf_queue_sg_init(&fh->video_q, &video_qops,
1210 			    &dev->pci->dev, &dev->slock,
1211 			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
1212 			    V4L2_FIELD_INTERLACED,
1213 			    sizeof(struct saa7146_buf),
1214 			    file, &dev->v4l2_lock);
1215 
1216 	return 0;
1217 }
1218 
1219 
1220 static void video_close(struct saa7146_dev *dev, struct file *file)
1221 {
1222 	struct saa7146_fh *fh = file->private_data;
1223 	struct saa7146_vv *vv = dev->vv_data;
1224 	struct videobuf_queue *q = &fh->video_q;
1225 
1226 	if (IS_CAPTURE_ACTIVE(fh) != 0)
1227 		video_end(fh, file);
1228 	else if (IS_OVERLAY_ACTIVE(fh) != 0)
1229 		saa7146_stop_preview(fh);
1230 
1231 	videobuf_stop(q);
1232 	/* hmm, why is this function declared void? */
1233 }
1234 
1235 
1236 static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1237 {
1238 	struct saa7146_vv *vv = dev->vv_data;
1239 	struct saa7146_dmaqueue *q = &vv->video_dmaq;
1240 
1241 	spin_lock(&dev->slock);
1242 	DEB_CAP("called\n");
1243 
1244 	/* only finish the buffer if we have one... */
1245 	if( NULL != q->curr ) {
1246 		saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
1247 	}
1248 	saa7146_buffer_next(dev,q,0);
1249 
1250 	spin_unlock(&dev->slock);
1251 }
1252 
1253 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1254 {
1255 	struct saa7146_fh *fh = file->private_data;
1256 	struct saa7146_dev *dev = fh->dev;
1257 	struct saa7146_vv *vv = dev->vv_data;
1258 	ssize_t ret = 0;
1259 
1260 	DEB_EE("called\n");
1261 
1262 	if ((vv->video_status & STATUS_CAPTURE) != 0) {
1263 		/* fixme: should we allow read() captures while streaming capture? */
1264 		if (vv->video_fh == fh) {
1265 			DEB_S("already capturing\n");
1266 			return -EBUSY;
1267 		}
1268 		DEB_S("already capturing in another open\n");
1269 		return -EBUSY;
1270 	}
1271 
1272 	ret = video_begin(fh);
1273 	if( 0 != ret) {
1274 		goto out;
1275 	}
1276 
1277 	ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1278 				file->f_flags & O_NONBLOCK);
1279 	if (ret != 0) {
1280 		video_end(fh, file);
1281 	} else {
1282 		ret = video_end(fh, file);
1283 	}
1284 out:
1285 	/* restart overlay if it was active before */
1286 	if (vv->ov_suspend != NULL) {
1287 		saa7146_start_preview(vv->ov_suspend);
1288 		vv->ov_suspend = NULL;
1289 	}
1290 
1291 	return ret;
1292 }
1293 
1294 const struct saa7146_use_ops saa7146_video_uops = {
1295 	.init = video_init,
1296 	.open = video_open,
1297 	.release = video_close,
1298 	.irq_done = video_irq_done,
1299 	.read = video_read,
1300 };
1301