blob: 4ca9a2561513878d2c9e6a5d7d27b031be57ffd6 [file] [log] [blame]
Joe Perches44d0b802011-08-21 19:56:44 -03001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <media/saa7146_vv.h>
Hans Verkuil1b8dac12009-02-07 11:18:05 -03004#include <media/v4l2-chip-ident.h>
Hans Verkuil537fa492012-05-01 12:04:52 -03005#include <media/v4l2-event.h>
6#include <media/v4l2-ctrls.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04007#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9static int max_memory = 32;
10
11module_param(max_memory, int, 0644);
12MODULE_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 */
21static 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
90static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
91
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -020092struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 int i, j = NUM_FORMATS;
95
96 for (i = 0; i < j; i++) {
97 if (formats[i].pixelformat == fourcc) {
98 return formats+i;
99 }
100 }
101
Joe Perches44d0b802011-08-21 19:56:44 -0300102 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
104}
105
Hans Verkuilb9600742009-01-18 19:59:11 -0300106static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int saa7146_start_preview(struct saa7146_fh *fh)
109{
110 struct saa7146_dev *dev = fh->dev;
111 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuilb9600742009-01-18 19:59:11 -0300112 struct v4l2_format fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int ret = 0, err = 0;
114
Joe Perches44d0b802011-08-21 19:56:44 -0300115 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Hans Verkuil5da545a2012-05-01 11:06:44 -0300117 /* check if we have overlay information */
118 if (vv->ov.fh == NULL) {
Joe Perches44d0b802011-08-21 19:56:44 -0300119 DEB_D("no overlay data available. try S_FMT first.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EAGAIN;
121 }
122
123 /* check if streaming capture is running */
124 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300125 DEB_D("streaming capture is active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -EBUSY;
127 }
128
129 /* check if overlay is running */
130 if (IS_OVERLAY_ACTIVE(fh) != 0) {
131 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300132 DEB_D("overlay is already active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134 }
Joe Perches44d0b802011-08-21 19:56:44 -0300135 DEB_D("overlay is already active in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EBUSY;
137 }
138
139 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
Joe Perches44d0b802011-08-21 19:56:44 -0300140 DEB_D("cannot get necessary overlay resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EBUSY;
142 }
143
Hans Verkuil5da545a2012-05-01 11:06:44 -0300144 fmt.fmt.win = vv->ov.win;
Hans Verkuilb9600742009-01-18 19:59:11 -0300145 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (0 != err) {
147 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
148 return -EBUSY;
149 }
Hans Verkuil5da545a2012-05-01 11:06:44 -0300150 vv->ov.win = fmt.fmt.win;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Joe Perches44d0b802011-08-21 19:56:44 -0300152 DEB_D("%dx%d+%d+%d %s field=%s\n",
Hans Verkuil5da545a2012-05-01 11:06:44 -0300153 vv->ov.win.w.width, vv->ov.win.w.height,
154 vv->ov.win.w.left, vv->ov.win.w.top,
155 vv->ov_fmt->name, v4l2_field_names[vv->ov.win.field]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (0 != (ret = saa7146_enable_overlay(fh))) {
Joe Perches44d0b802011-08-21 19:56:44 -0300158 DEB_D("enabling overlay failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
160 return ret;
161 }
162
163 vv->video_status = STATUS_OVERLAY;
164 vv->video_fh = fh;
165
166 return 0;
167}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300168EXPORT_SYMBOL_GPL(saa7146_start_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170int saa7146_stop_preview(struct saa7146_fh *fh)
171{
172 struct saa7146_dev *dev = fh->dev;
173 struct saa7146_vv *vv = dev->vv_data;
174
Joe Perches44d0b802011-08-21 19:56:44 -0300175 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* check if streaming capture is running */
178 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300179 DEB_D("streaming capture is active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EBUSY;
181 }
182
183 /* check if overlay is running at all */
184 if ((vv->video_status & STATUS_OVERLAY) == 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300185 DEB_D("no active overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187 }
188
189 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300190 DEB_D("overlay is active, but in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EBUSY;
192 }
193
194 vv->video_status = 0;
195 vv->video_fh = NULL;
196
197 saa7146_disable_overlay(fh);
198
199 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
200
201 return 0;
202}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300203EXPORT_SYMBOL_GPL(saa7146_stop_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/********************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* common pagetable functions */
207
208static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
209{
210 struct pci_dev *pci = dev->pci;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300211 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
212 struct scatterlist *list = dma->sglist;
213 int length = dma->sglen;
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200214 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Joe Perches44d0b802011-08-21 19:56:44 -0300216 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if( 0 != IS_PLANAR(sfmt->trans)) {
219 struct saa7146_pgtable *pt1 = &buf->pt[0];
220 struct saa7146_pgtable *pt2 = &buf->pt[1];
221 struct saa7146_pgtable *pt3 = &buf->pt[2];
Al Viroa36ef6b2008-06-22 14:19:19 -0300222 __le32 *ptr1, *ptr2, *ptr3;
223 __le32 fill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 int size = buf->fmt->width*buf->fmt->height;
226 int i,p,m1,m2,m3,o1,o2;
227
228 switch( sfmt->depth ) {
229 case 12: {
230 /* create some offsets inside the page table */
231 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
232 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
233 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
234 o1 = size%PAGE_SIZE;
235 o2 = (size+(size/4))%PAGE_SIZE;
Joe Perches44d0b802011-08-21 19:56:44 -0300236 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
237 size, m1, m2, m3, o1, o2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239 }
240 case 16: {
241 /* create some offsets inside the page table */
242 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
243 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
244 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
245 o1 = size%PAGE_SIZE;
246 o2 = (size+(size/2))%PAGE_SIZE;
Joe Perches44d0b802011-08-21 19:56:44 -0300247 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
248 size, m1, m2, m3, o1, o2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 }
251 default: {
252 return -1;
253 }
254 }
255
256 ptr1 = pt1->cpu;
257 ptr2 = pt2->cpu;
258 ptr3 = pt3->cpu;
259
260 /* walk all pages, copy all page addresses to ptr1 */
261 for (i = 0; i < length; i++, list++) {
262 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
263 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
264 }
265 }
266/*
267 ptr1 = pt1->cpu;
268 for(j=0;j<40;j++) {
269 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
270 }
271*/
272
273 /* if we have a user buffer, the first page may not be
274 aligned to a page boundary. */
Hans Verkuil429e9082008-07-27 14:08:54 -0300275 pt1->offset = dma->sglist->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 pt2->offset = pt1->offset+o1;
277 pt3->offset = pt1->offset+o2;
278
279 /* create video-dma2 page table */
280 ptr1 = pt1->cpu;
281 for(i = m1; i <= m2 ; i++, ptr2++) {
282 *ptr2 = ptr1[i];
283 }
284 fill = *(ptr2-1);
285 for(;i<1024;i++,ptr2++) {
286 *ptr2 = fill;
287 }
288 /* create video-dma3 page table */
289 ptr1 = pt1->cpu;
290 for(i = m2; i <= m3; i++,ptr3++) {
291 *ptr3 = ptr1[i];
292 }
293 fill = *(ptr3-1);
294 for(;i<1024;i++,ptr3++) {
295 *ptr3 = fill;
296 }
297 /* finally: finish up video-dma1 page table */
298 ptr1 = pt1->cpu+m1;
299 fill = pt1->cpu[m1];
300 for(i=m1;i<1024;i++,ptr1++) {
301 *ptr1 = fill;
302 }
303/*
304 ptr1 = pt1->cpu;
305 ptr2 = pt2->cpu;
306 ptr3 = pt3->cpu;
307 for(j=0;j<40;j++) {
308 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
309 }
310 for(j=0;j<40;j++) {
311 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
312 }
313 for(j=0;j<40;j++) {
314 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
315 }
316*/
317 } else {
318 struct saa7146_pgtable *pt = &buf->pt[0];
319 return saa7146_pgtable_build_single(pci, pt, list, length);
320 }
321
322 return 0;
323}
324
325
326/********************************************************************************/
327/* file operations */
328
329static int video_begin(struct saa7146_fh *fh)
330{
331 struct saa7146_dev *dev = fh->dev;
332 struct saa7146_vv *vv = dev->vv_data;
333 struct saa7146_format *fmt = NULL;
334 unsigned int resource;
335 int ret = 0, err = 0;
336
Joe Perches44d0b802011-08-21 19:56:44 -0300337 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if ((vv->video_status & STATUS_CAPTURE) != 0) {
340 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300341 DEB_S("already capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343 }
Joe Perches44d0b802011-08-21 19:56:44 -0300344 DEB_S("already capturing in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EBUSY;
346 }
347
348 if ((vv->video_status & STATUS_OVERLAY) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300349 DEB_S("warning: suspending overlay video for streaming capture\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 vv->ov_suspend = vv->video_fh;
351 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
352 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300353 DEB_D("suspending video failed. aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return err;
355 }
356 }
357
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300358 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* we need to have a valid format set here */
360 BUG_ON(NULL == fmt);
361
362 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
363 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
364 } else {
365 resource = RESOURCE_DMA1_HPS;
366 }
367
368 ret = saa7146_res_get(fh, resource);
369 if (0 == ret) {
Joe Perches44d0b802011-08-21 19:56:44 -0300370 DEB_S("cannot get capture resource %d\n", resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (vv->ov_suspend != NULL) {
372 saa7146_start_preview(vv->ov_suspend);
373 vv->ov_suspend = NULL;
374 }
375 return -EBUSY;
376 }
377
378 /* clear out beginning of streaming bit (rps register 0)*/
379 saa7146_write(dev, MC2, MASK_27 );
380
381 /* enable rps0 irqs */
382 SAA7146_IER_ENABLE(dev, MASK_27);
383
384 vv->video_fh = fh;
385 vv->video_status = STATUS_CAPTURE;
386
387 return 0;
388}
389
390static int video_end(struct saa7146_fh *fh, struct file *file)
391{
392 struct saa7146_dev *dev = fh->dev;
393 struct saa7146_vv *vv = dev->vv_data;
394 struct saa7146_format *fmt = NULL;
395 unsigned long flags;
396 unsigned int resource;
397 u32 dmas = 0;
Joe Perches44d0b802011-08-21 19:56:44 -0300398 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300401 DEB_S("not capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403 }
404
405 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300406 DEB_S("capturing, but in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EBUSY;
408 }
409
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300410 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* we need to have a valid format set here */
412 BUG_ON(NULL == fmt);
413
414 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
415 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
416 dmas = MASK_22 | MASK_21 | MASK_20;
417 } else {
418 resource = RESOURCE_DMA1_HPS;
419 dmas = MASK_22;
420 }
421 spin_lock_irqsave(&dev->slock,flags);
422
423 /* disable rps0 */
424 saa7146_write(dev, MC1, MASK_28);
425
426 /* disable rps0 irqs */
427 SAA7146_IER_DISABLE(dev, MASK_27);
428
429 /* shut down all used video dma transfers */
430 saa7146_write(dev, MC1, dmas);
431
432 spin_unlock_irqrestore(&dev->slock, flags);
433
434 vv->video_fh = NULL;
435 vv->video_status = 0;
436
437 saa7146_res_free(fh, resource);
438
439 if (vv->ov_suspend != NULL) {
440 saa7146_start_preview(vv->ov_suspend);
441 vv->ov_suspend = NULL;
442 }
443
444 return 0;
445}
446
Hans Verkuilb9600742009-01-18 19:59:11 -0300447static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Hans Verkuilb9600742009-01-18 19:59:11 -0300449 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
450
451 strcpy((char *)cap->driver, "saa7146 v4l2");
452 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
453 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
454 cap->version = SAA7146_VERSION_CODE;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300455 cap->device_caps =
Hans Verkuilb9600742009-01-18 19:59:11 -0300456 V4L2_CAP_VIDEO_CAPTURE |
457 V4L2_CAP_VIDEO_OVERLAY |
458 V4L2_CAP_READWRITE |
459 V4L2_CAP_STREAMING;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300460 cap->device_caps |= dev->ext_vv_data->capabilities;
461 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Hans Verkuilb9600742009-01-18 19:59:11 -0300462 return 0;
463}
464
465static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
466{
467 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct saa7146_vv *vv = dev->vv_data;
469
Hans Verkuilb9600742009-01-18 19:59:11 -0300470 *fb = vv->ov_fb;
471 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
Hans Verkuil5da545a2012-05-01 11:06:44 -0300472 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
Hans Verkuilb9600742009-01-18 19:59:11 -0300473 return 0;
474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Hans Verkuilb9600742009-01-18 19:59:11 -0300476static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
477{
478 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
479 struct saa7146_vv *vv = dev->vv_data;
480 struct saa7146_format *fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Joe Perches44d0b802011-08-21 19:56:44 -0300482 DEB_EE("VIDIOC_S_FBUF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Hans Verkuilb9600742009-01-18 19:59:11 -0300484 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
485 return -EPERM;
486
487 /* check args */
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200488 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300489 if (NULL == fmt)
490 return -EINVAL;
491
492 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
493 if (fmt->flags & FORMAT_IS_PLANAR)
Joe Perches44d0b802011-08-21 19:56:44 -0300494 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
495 (char *)&fmt->pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300496
497 /* check if overlay is running */
498 if (IS_OVERLAY_ACTIVE(fh) != 0) {
499 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300500 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300501 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504
Hans Verkuilb9600742009-01-18 19:59:11 -0300505 /* ok, accept it */
506 vv->ov_fb = *fb;
507 vv->ov_fmt = fmt;
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300508
509 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
510 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
Joe Perches44d0b802011-08-21 19:56:44 -0300511 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300512 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300513 return 0;
514}
515
516static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
517{
518 if (f->index >= NUM_FORMATS)
519 return -EINVAL;
520 strlcpy((char *)f->description, formats[f->index].name,
521 sizeof(f->description));
522 f->pixelformat = formats[f->index].pixelformat;
523 return 0;
524}
525
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300526int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilb9600742009-01-18 19:59:11 -0300527{
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300528 struct saa7146_dev *dev = container_of(ctrl->handler,
529 struct saa7146_dev, ctrl_handler);
Hans Verkuilb9600742009-01-18 19:59:11 -0300530 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300531 u32 val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300532
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300533 switch (ctrl->id) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300534 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300535 val = saa7146_read(dev, BCS_CTRL);
536 val &= 0x00ffffff;
537 val |= (ctrl->val << 24);
538 saa7146_write(dev, BCS_CTRL, val);
539 saa7146_write(dev, MC2, MASK_22 | MASK_06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300541
Hans Verkuilb9600742009-01-18 19:59:11 -0300542 case V4L2_CID_CONTRAST:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300543 val = saa7146_read(dev, BCS_CTRL);
544 val &= 0xff00ffff;
545 val |= (ctrl->val << 16);
546 saa7146_write(dev, BCS_CTRL, val);
547 saa7146_write(dev, MC2, MASK_22 | MASK_06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300549
Hans Verkuilb9600742009-01-18 19:59:11 -0300550 case V4L2_CID_SATURATION:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300551 val = saa7146_read(dev, BCS_CTRL);
552 val &= 0xffffff00;
553 val |= (ctrl->val << 0);
554 saa7146_write(dev, BCS_CTRL, val);
Hans Verkuilb9600742009-01-18 19:59:11 -0300555 saa7146_write(dev, MC2, MASK_22 | MASK_06);
556 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300557
Hans Verkuilb9600742009-01-18 19:59:11 -0300558 case V4L2_CID_HFLIP:
559 /* fixme: we can support changing VFLIP and HFLIP here... */
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300560 if ((vv->video_status & STATUS_CAPTURE))
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300561 return -EBUSY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300562 vv->hflip = ctrl->val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300563 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300564
Hans Verkuilb9600742009-01-18 19:59:11 -0300565 case V4L2_CID_VFLIP:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300566 if ((vv->video_status & STATUS_CAPTURE))
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300567 return -EBUSY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300568 vv->vflip = ctrl->val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300569 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300570
Hans Verkuilb9600742009-01-18 19:59:11 -0300571 default:
Hans Verkuilb9600742009-01-18 19:59:11 -0300572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300575 if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
576 struct saa7146_fh *fh = vv->video_fh;
577
Hans Verkuilb9600742009-01-18 19:59:11 -0300578 saa7146_stop_preview(fh);
579 saa7146_start_preview(fh);
580 }
581 return 0;
582}
583
584static int vidioc_g_parm(struct file *file, void *fh,
585 struct v4l2_streamparm *parm)
586{
Trent Piepho717167e2009-03-04 01:21:03 -0300587 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
588 struct saa7146_vv *vv = dev->vv_data;
589
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300590 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
591 return -EINVAL;
Hans Verkuilb9600742009-01-18 19:59:11 -0300592 parm->parm.capture.readbuffers = 1;
Trent Piepho717167e2009-03-04 01:21:03 -0300593 v4l2_video_std_frame_period(vv->standard->id,
594 &parm->parm.capture.timeperframe);
Hans Verkuilb9600742009-01-18 19:59:11 -0300595 return 0;
596}
597
598static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
599{
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300600 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
601 struct saa7146_vv *vv = dev->vv_data;
602
603 f->fmt.pix = vv->video_fmt;
Hans Verkuilb9600742009-01-18 19:59:11 -0300604 return 0;
605}
606
607static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
608{
Hans Verkuil5da545a2012-05-01 11:06:44 -0300609 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
610 struct saa7146_vv *vv = dev->vv_data;
611
612 f->fmt.win = vv->ov.win;
Hans Verkuilb9600742009-01-18 19:59:11 -0300613 return 0;
614}
615
616static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
617{
Hans Verkuilfca34692012-05-01 11:28:20 -0300618 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
619 struct saa7146_vv *vv = dev->vv_data;
620
621 f->fmt.vbi = vv->vbi_fmt;
Hans Verkuilb9600742009-01-18 19:59:11 -0300622 return 0;
623}
624
625static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
626{
627 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
628 struct saa7146_vv *vv = dev->vv_data;
629 struct saa7146_format *fmt;
630 enum v4l2_field field;
631 int maxw, maxh;
632 int calc_bpl;
633
Joe Perches44d0b802011-08-21 19:56:44 -0300634 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300635
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200636 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300637 if (NULL == fmt)
638 return -EINVAL;
639
640 field = f->fmt.pix.field;
641 maxw = vv->standard->h_max_out;
642 maxh = vv->standard->v_max_out;
643
644 if (V4L2_FIELD_ANY == field) {
645 field = (f->fmt.pix.height > maxh / 2)
646 ? V4L2_FIELD_INTERLACED
647 : V4L2_FIELD_BOTTOM;
648 }
649 switch (field) {
650 case V4L2_FIELD_ALTERNATE:
651 vv->last_field = V4L2_FIELD_TOP;
652 maxh = maxh / 2;
653 break;
654 case V4L2_FIELD_TOP:
655 case V4L2_FIELD_BOTTOM:
656 vv->last_field = V4L2_FIELD_INTERLACED;
657 maxh = maxh / 2;
658 break;
659 case V4L2_FIELD_INTERLACED:
660 vv->last_field = V4L2_FIELD_INTERLACED;
661 break;
662 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300663 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300664 return -EINVAL;
665 }
666
667 f->fmt.pix.field = field;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300668 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuilb9600742009-01-18 19:59:11 -0300669 if (f->fmt.pix.width > maxw)
670 f->fmt.pix.width = maxw;
671 if (f->fmt.pix.height > maxh)
672 f->fmt.pix.height = maxh;
673
674 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
675
676 if (f->fmt.pix.bytesperline < calc_bpl)
677 f->fmt.pix.bytesperline = calc_bpl;
678
679 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
680 f->fmt.pix.bytesperline = calc_bpl;
681
682 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
Joe Perches44d0b802011-08-21 19:56:44 -0300683 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
684 f->fmt.pix.width, f->fmt.pix.height,
685 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
Hans Verkuilb9600742009-01-18 19:59:11 -0300686
687 return 0;
688}
689
690
691static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
692{
693 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
694 struct saa7146_vv *vv = dev->vv_data;
695 struct v4l2_window *win = &f->fmt.win;
696 enum v4l2_field field;
697 int maxw, maxh;
698
Joe Perches44d0b802011-08-21 19:56:44 -0300699 DEB_EE("dev:%p\n", dev);
Hans Verkuilb9600742009-01-18 19:59:11 -0300700
701 if (NULL == vv->ov_fb.base) {
Joe Perches44d0b802011-08-21 19:56:44 -0300702 DEB_D("no fb base set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300703 return -EINVAL;
704 }
705 if (NULL == vv->ov_fmt) {
Joe Perches44d0b802011-08-21 19:56:44 -0300706 DEB_D("no fb fmt set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300707 return -EINVAL;
708 }
709 if (win->w.width < 48 || win->w.height < 32) {
Joe Perches44d0b802011-08-21 19:56:44 -0300710 DEB_D("min width/height. (%d,%d)\n",
711 win->w.width, win->w.height);
Hans Verkuilb9600742009-01-18 19:59:11 -0300712 return -EINVAL;
713 }
714 if (win->clipcount > 16) {
Joe Perches44d0b802011-08-21 19:56:44 -0300715 DEB_D("clipcount too big\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300716 return -EINVAL;
717 }
718
719 field = win->field;
720 maxw = vv->standard->h_max_out;
721 maxh = vv->standard->v_max_out;
722
723 if (V4L2_FIELD_ANY == field) {
724 field = (win->w.height > maxh / 2)
725 ? V4L2_FIELD_INTERLACED
726 : V4L2_FIELD_TOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300728 switch (field) {
729 case V4L2_FIELD_TOP:
730 case V4L2_FIELD_BOTTOM:
731 case V4L2_FIELD_ALTERNATE:
732 maxh = maxh / 2;
733 break;
734 case V4L2_FIELD_INTERLACED:
735 break;
736 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300737 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Hans Verkuilb9600742009-01-18 19:59:11 -0300741 win->field = field;
742 if (win->w.width > maxw)
743 win->w.width = maxw;
744 if (win->w.height > maxh)
745 win->w.height = maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Hans Verkuilb9600742009-01-18 19:59:11 -0300747 return 0;
748}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Hans Verkuilb9600742009-01-18 19:59:11 -0300750static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
751{
752 struct saa7146_fh *fh = __fh;
753 struct saa7146_dev *dev = fh->dev;
754 struct saa7146_vv *vv = dev->vv_data;
755 int err;
756
Joe Perches44d0b802011-08-21 19:56:44 -0300757 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300758 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300759 DEB_EE("streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300760 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300762 err = vidioc_try_fmt_vid_cap(file, fh, f);
763 if (0 != err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return err;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300765 vv->video_fmt = f->fmt.pix;
Joe Perches44d0b802011-08-21 19:56:44 -0300766 DEB_EE("set to pixelformat '%4.4s'\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300767 (char *)&vv->video_fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300768 return 0;
769}
770
771static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
772{
773 struct saa7146_fh *fh = __fh;
774 struct saa7146_dev *dev = fh->dev;
775 struct saa7146_vv *vv = dev->vv_data;
776 int err;
777
Joe Perches44d0b802011-08-21 19:56:44 -0300778 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300779 err = vidioc_try_fmt_vid_overlay(file, fh, f);
780 if (0 != err)
781 return err;
Hans Verkuil5da545a2012-05-01 11:06:44 -0300782 vv->ov.win = f->fmt.win;
783 vv->ov.nclips = f->fmt.win.clipcount;
784 if (vv->ov.nclips > 16)
785 vv->ov.nclips = 16;
786 if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
787 sizeof(struct v4l2_clip) * vv->ov.nclips)) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300788 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300790
Hans Verkuil5da545a2012-05-01 11:06:44 -0300791 /* vv->ov.fh is used to indicate that we have valid overlay informations, too */
792 vv->ov.fh = fh;
Hans Verkuilb9600742009-01-18 19:59:11 -0300793
Hans Verkuilb9600742009-01-18 19:59:11 -0300794 /* check if our current overlay is active */
795 if (IS_OVERLAY_ACTIVE(fh) != 0) {
796 saa7146_stop_preview(fh);
797 saa7146_start_preview(fh);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800798 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300799 return 0;
800}
801
802static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
803{
804 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
805 struct saa7146_vv *vv = dev->vv_data;
806
807 *norm = vv->standard->id;
808 return 0;
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
812 PAL / NTSC / SECAM. if your hardware does not (or does more)
813 -- override this function in your extension */
Hans Verkuilb9600742009-01-18 19:59:11 -0300814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case VIDIOC_ENUMSTD:
816 {
817 struct v4l2_standard *e = arg;
818 if (e->index < 0 )
819 return -EINVAL;
820 if( e->index < dev->ext_vv_data->num_stds ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300821 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
823 return 0;
824 }
825 return -EINVAL;
826 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Hans Verkuilb9600742009-01-18 19:59:11 -0300829static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
830{
831 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
832 struct saa7146_vv *vv = dev->vv_data;
833 int found = 0;
834 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Joe Perches44d0b802011-08-21 19:56:44 -0300836 DEB_EE("VIDIOC_S_STD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Hans Verkuilb9600742009-01-18 19:59:11 -0300838 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300839 DEB_D("cannot change video standard while streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300840 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Hans Verkuilb9600742009-01-18 19:59:11 -0300843 if ((vv->video_status & STATUS_OVERLAY) != 0) {
844 vv->ov_suspend = vv->video_fh;
845 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300847 DEB_D("suspending video failed. aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300849 }
850 }
Brandon Philips49ee7182007-10-05 16:26:27 -0300851
Hans Verkuilb9600742009-01-18 19:59:11 -0300852 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
853 if (*id & dev->ext_vv_data->stds[i].id)
854 break;
855 if (i != dev->ext_vv_data->num_stds) {
856 vv->standard = &dev->ext_vv_data->stds[i];
857 if (NULL != dev->ext_vv_data->std_callback)
858 dev->ext_vv_data->std_callback(dev, vv->standard);
859 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300861
Hans Verkuilb9600742009-01-18 19:59:11 -0300862 if (vv->ov_suspend != NULL) {
863 saa7146_start_preview(vv->ov_suspend);
864 vv->ov_suspend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300866
867 if (!found) {
Joe Perches44d0b802011-08-21 19:56:44 -0300868 DEB_EE("VIDIOC_S_STD: standard not found\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300869 return -EINVAL;
870 }
871
Joe Perches44d0b802011-08-21 19:56:44 -0300872 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
874}
875
Hans Verkuilb9600742009-01-18 19:59:11 -0300876static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
877{
878 int err;
879
Joe Perches44d0b802011-08-21 19:56:44 -0300880 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
Hans Verkuilb9600742009-01-18 19:59:11 -0300881 if (on)
882 err = saa7146_start_preview(fh);
883 else
884 err = saa7146_stop_preview(fh);
885 return err;
886}
887
888static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
889{
890 struct saa7146_fh *fh = __fh;
891
892 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
893 return videobuf_reqbufs(&fh->video_q, b);
894 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
895 return videobuf_reqbufs(&fh->vbi_q, b);
896 return -EINVAL;
897}
898
899static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
900{
901 struct saa7146_fh *fh = __fh;
902
903 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 return videobuf_querybuf(&fh->video_q, buf);
905 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
906 return videobuf_querybuf(&fh->vbi_q, buf);
907 return -EINVAL;
908}
909
910static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
911{
912 struct saa7146_fh *fh = __fh;
913
914 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
915 return videobuf_qbuf(&fh->video_q, buf);
916 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
917 return videobuf_qbuf(&fh->vbi_q, buf);
918 return -EINVAL;
919}
920
921static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
922{
923 struct saa7146_fh *fh = __fh;
924
925 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
927 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
928 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
929 return -EINVAL;
930}
931
932static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
933{
934 struct saa7146_fh *fh = __fh;
935 int err;
936
Joe Perches44d0b802011-08-21 19:56:44 -0300937 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300938
939 err = video_begin(fh);
940 if (err)
941 return err;
942 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
943 return videobuf_streamon(&fh->video_q);
944 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
945 return videobuf_streamon(&fh->vbi_q);
946 return -EINVAL;
947}
948
949static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
950{
951 struct saa7146_fh *fh = __fh;
952 struct saa7146_dev *dev = fh->dev;
953 struct saa7146_vv *vv = dev->vv_data;
954 int err;
955
Joe Perches44d0b802011-08-21 19:56:44 -0300956 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300957
958 /* ugly: we need to copy some checks from video_end(),
959 because videobuf_streamoff() relies on the capture running.
960 check and fix this */
961 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300962 DEB_S("not capturing\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300963 return 0;
964 }
965
966 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300967 DEB_S("capturing, but in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300968 return -EBUSY;
969 }
970
971 err = -EINVAL;
972 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
973 err = videobuf_streamoff(&fh->video_q);
974 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
975 err = videobuf_streamoff(&fh->vbi_q);
976 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300977 DEB_D("warning: videobuf_streamoff() failed\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300978 video_end(fh, file);
979 } else {
980 err = video_end(fh, file);
981 }
982 return err;
983}
984
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300985static int vidioc_g_chip_ident(struct file *file, void *__fh,
986 struct v4l2_dbg_chip_ident *chip)
987{
988 struct saa7146_fh *fh = __fh;
989 struct saa7146_dev *dev = fh->dev;
990
991 chip->ident = V4L2_IDENT_NONE;
992 chip->revision = 0;
Hans Verkuileae4d692009-02-07 20:15:22 -0300993 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300994 chip->ident = V4L2_IDENT_SAA7146;
995 return 0;
996 }
997 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
998 core, g_chip_ident, chip);
999}
1000
Hans Verkuilb9600742009-01-18 19:59:11 -03001001const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1002 .vidioc_querycap = vidioc_querycap,
1003 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Trent Piepho717167e2009-03-04 01:21:03 -03001004 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
Hans Verkuilb9600742009-01-18 19:59:11 -03001005 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1006 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1007 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1008 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1009 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1010 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1011 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001012 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuilb9600742009-01-18 19:59:11 -03001013
1014 .vidioc_overlay = vidioc_overlay,
1015 .vidioc_g_fbuf = vidioc_g_fbuf,
1016 .vidioc_s_fbuf = vidioc_s_fbuf,
1017 .vidioc_reqbufs = vidioc_reqbufs,
1018 .vidioc_querybuf = vidioc_querybuf,
1019 .vidioc_qbuf = vidioc_qbuf,
1020 .vidioc_dqbuf = vidioc_dqbuf,
1021 .vidioc_g_std = vidioc_g_std,
1022 .vidioc_s_std = vidioc_s_std,
Hans Verkuilb9600742009-01-18 19:59:11 -03001023 .vidioc_streamon = vidioc_streamon,
1024 .vidioc_streamoff = vidioc_streamoff,
1025 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuil537fa492012-05-01 12:04:52 -03001026 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1027 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilb9600742009-01-18 19:59:11 -03001028};
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*********************************************************************************/
1031/* buffer handling functions */
1032
1033static int buffer_activate (struct saa7146_dev *dev,
1034 struct saa7146_buf *buf,
1035 struct saa7146_buf *next)
1036{
1037 struct saa7146_vv *vv = dev->vv_data;
1038
Brandon Philips0fc06862007-11-06 20:02:36 -03001039 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 saa7146_set_capture(dev,buf,next);
1041
Hans Verkuil9bb60192012-05-01 11:45:27 -03001042 mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 0;
1044}
1045
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001046static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1047{
1048 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1049 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1050 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static int buffer_prepare(struct videobuf_queue *q,
1054 struct videobuf_buffer *vb, enum v4l2_field field)
1055{
1056 struct file *file = q->priv_data;
1057 struct saa7146_fh *fh = file->private_data;
1058 struct saa7146_dev *dev = fh->dev;
1059 struct saa7146_vv *vv = dev->vv_data;
1060 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1061 int size,err = 0;
1062
Joe Perches44d0b802011-08-21 19:56:44 -03001063 DEB_CAP("vbuf:%p\n", vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* sanity checks */
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001066 if (vv->video_fmt.width < 48 ||
1067 vv->video_fmt.height < 32 ||
1068 vv->video_fmt.width > vv->standard->h_max_out ||
1069 vv->video_fmt.height > vv->standard->v_max_out) {
Joe Perches44d0b802011-08-21 19:56:44 -03001070 DEB_D("w (%d) / h (%d) out of bounds\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001071 vv->video_fmt.width, vv->video_fmt.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -EINVAL;
1073 }
1074
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001075 size = vv->video_fmt.sizeimage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
Joe Perches44d0b802011-08-21 19:56:44 -03001077 DEB_D("size mismatch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -EINVAL;
1079 }
1080
Joe Perches44d0b802011-08-21 19:56:44 -03001081 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001082 vv->video_fmt.width, vv->video_fmt.height,
1083 size, v4l2_field_names[vv->video_fmt.field]);
1084 if (buf->vb.width != vv->video_fmt.width ||
1085 buf->vb.bytesperline != vv->video_fmt.bytesperline ||
1086 buf->vb.height != vv->video_fmt.height ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 buf->vb.size != size ||
1088 buf->vb.field != field ||
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001089 buf->vb.field != vv->video_fmt.field ||
1090 buf->fmt != &vv->video_fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001091 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Brandon Philips0fc06862007-11-06 20:02:36 -03001094 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct saa7146_format *sfmt;
1096
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001097 buf->vb.bytesperline = vv->video_fmt.bytesperline;
1098 buf->vb.width = vv->video_fmt.width;
1099 buf->vb.height = vv->video_fmt.height;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 buf->vb.size = size;
1101 buf->vb.field = field;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001102 buf->fmt = &vv->video_fmt;
1103 buf->vb.field = vv->video_fmt.field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001105 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001107 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if( 0 != IS_PLANAR(sfmt->trans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1110 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1111 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1112 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1114 }
1115
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001116 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (err)
1118 goto oops;
1119 err = saa7146_pgtable_build(dev,buf);
1120 if (err)
1121 goto oops;
1122 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001123 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 buf->activate = buffer_activate;
1125
1126 return 0;
1127
1128 oops:
Joe Perches44d0b802011-08-21 19:56:44 -03001129 DEB_D("error out\n");
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001130 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 return err;
1133}
1134
1135static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1136{
1137 struct file *file = q->priv_data;
1138 struct saa7146_fh *fh = file->private_data;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001139 struct saa7146_vv *vv = fh->dev->vv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1142 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1143
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001144 *size = vv->video_fmt.sizeimage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* check if we exceed the "max_memory" parameter */
1147 if( (*count * *size) > (max_memory*1048576) ) {
1148 *count = (max_memory*1048576) / *size;
1149 }
1150
Joe Perches44d0b802011-08-21 19:56:44 -03001151 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 return 0;
1154}
1155
1156static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1157{
1158 struct file *file = q->priv_data;
1159 struct saa7146_fh *fh = file->private_data;
1160 struct saa7146_dev *dev = fh->dev;
1161 struct saa7146_vv *vv = dev->vv_data;
1162 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1163
Joe Perches44d0b802011-08-21 19:56:44 -03001164 DEB_CAP("vbuf:%p\n", vb);
Hans Verkuil9bb60192012-05-01 11:45:27 -03001165 saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1169{
1170 struct file *file = q->priv_data;
1171 struct saa7146_fh *fh = file->private_data;
1172 struct saa7146_dev *dev = fh->dev;
1173 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1174
Joe Perches44d0b802011-08-21 19:56:44 -03001175 DEB_CAP("vbuf:%p\n", vb);
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001176
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001177 saa7146_dma_free(dev,q,buf);
Mauro Carvalho Chehabba9e9f32010-02-01 21:23:46 -02001178
1179 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static struct videobuf_queue_ops video_qops = {
1183 .buf_setup = buffer_setup,
1184 .buf_prepare = buffer_prepare,
1185 .buf_queue = buffer_queue,
1186 .buf_release = buffer_release,
1187};
1188
1189/********************************************************************************/
1190/* file operations */
1191
1192static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1193{
Hans Verkuil9bb60192012-05-01 11:45:27 -03001194 INIT_LIST_HEAD(&vv->video_dmaq.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Hans Verkuil9bb60192012-05-01 11:45:27 -03001196 init_timer(&vv->video_dmaq.timeout);
1197 vv->video_dmaq.timeout.function = saa7146_buffer_timeout;
1198 vv->video_dmaq.timeout.data = (unsigned long)(&vv->video_dmaq);
1199 vv->video_dmaq.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* set some default values */
1202 vv->standard = &dev->ext_vv_data->stds[0];
1203
1204 /* FIXME: what's this? */
1205 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1206 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1207}
1208
1209
1210static int video_open(struct saa7146_dev *dev, struct file *file)
1211{
Joe Perchesabf84382010-07-12 17:50:03 -03001212 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001214 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1215 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1217 V4L2_FIELD_INTERLACED,
1218 sizeof(struct saa7146_buf),
Hans Verkuil9af39712010-12-18 09:20:59 -03001219 file, &dev->v4l2_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222}
1223
1224
1225static void video_close(struct saa7146_dev *dev, struct file *file)
1226{
Joe Perchesabf84382010-07-12 17:50:03 -03001227 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct saa7146_vv *vv = dev->vv_data;
Hartmut Birr2970c492007-04-22 06:57:26 -03001229 struct videobuf_queue *q = &fh->video_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Hans Verkuil7b4668e2011-08-25 09:54:30 -03001231 if (IS_CAPTURE_ACTIVE(fh) != 0)
1232 video_end(fh, file);
1233 else if (IS_OVERLAY_ACTIVE(fh) != 0)
1234 saa7146_stop_preview(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Brandon Philips19bc5132007-11-13 20:05:38 -03001236 videobuf_stop(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* hmm, why is this function declared void? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240
1241static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1242{
1243 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuil9bb60192012-05-01 11:45:27 -03001244 struct saa7146_dmaqueue *q = &vv->video_dmaq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 spin_lock(&dev->slock);
Joe Perches44d0b802011-08-21 19:56:44 -03001247 DEB_CAP("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* only finish the buffer if we have one... */
1250 if( NULL != q->curr ) {
Brandon Philips0fc06862007-11-06 20:02:36 -03001251 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 saa7146_buffer_next(dev,q,0);
1254
1255 spin_unlock(&dev->slock);
1256}
1257
1258static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1259{
1260 struct saa7146_fh *fh = file->private_data;
1261 struct saa7146_dev *dev = fh->dev;
1262 struct saa7146_vv *vv = dev->vv_data;
1263 ssize_t ret = 0;
1264
Joe Perches44d0b802011-08-21 19:56:44 -03001265 DEB_EE("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1268 /* fixme: should we allow read() captures while streaming capture? */
1269 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -03001270 DEB_S("already capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -EBUSY;
1272 }
Joe Perches44d0b802011-08-21 19:56:44 -03001273 DEB_S("already capturing in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EBUSY;
1275 }
1276
1277 ret = video_begin(fh);
1278 if( 0 != ret) {
1279 goto out;
1280 }
1281
1282 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1283 file->f_flags & O_NONBLOCK);
1284 if (ret != 0) {
1285 video_end(fh, file);
1286 } else {
1287 ret = video_end(fh, file);
1288 }
1289out:
1290 /* restart overlay if it was active before */
1291 if (vv->ov_suspend != NULL) {
1292 saa7146_start_preview(vv->ov_suspend);
1293 vv->ov_suspend = NULL;
1294 }
1295
1296 return ret;
1297}
1298
1299struct saa7146_use_ops saa7146_video_uops = {
1300 .init = video_init,
1301 .open = video_open,
1302 .release = video_close,
1303 .irq_done = video_irq_done,
1304 .read = video_read,
1305};