blob: 6d14785d47471fd1c290c5ee43cf792ac30245f7 [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 Verkuilab49ae02012-05-01 12:57:57 -0300449 struct video_device *vdev = video_devdata(file);
Hans Verkuilb9600742009-01-18 19:59:11 -0300450 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
451
452 strcpy((char *)cap->driver, "saa7146 v4l2");
453 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
454 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
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;
Hans Verkuila299e402012-05-06 13:31:27 -0300461 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Hans Verkuilab49ae02012-05-01 12:57:57 -0300462 if (vdev->vfl_type == VFL_TYPE_GRABBER)
463 cap->device_caps &=
464 ~(V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT);
465 else
466 cap->device_caps &=
Hans Verkuila299e402012-05-06 13:31:27 -0300467 ~(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_AUDIO);
Hans Verkuilb9600742009-01-18 19:59:11 -0300468 return 0;
469}
470
471static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
472{
473 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct saa7146_vv *vv = dev->vv_data;
475
Hans Verkuilb9600742009-01-18 19:59:11 -0300476 *fb = vv->ov_fb;
477 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
Hans Verkuil5da545a2012-05-01 11:06:44 -0300478 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
Hans Verkuilb9600742009-01-18 19:59:11 -0300479 return 0;
480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Hans Verkuilb9600742009-01-18 19:59:11 -0300482static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
483{
484 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
485 struct saa7146_vv *vv = dev->vv_data;
486 struct saa7146_format *fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Joe Perches44d0b802011-08-21 19:56:44 -0300488 DEB_EE("VIDIOC_S_FBUF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Hans Verkuilb9600742009-01-18 19:59:11 -0300490 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
491 return -EPERM;
492
493 /* check args */
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200494 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300495 if (NULL == fmt)
496 return -EINVAL;
497
498 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
499 if (fmt->flags & FORMAT_IS_PLANAR)
Joe Perches44d0b802011-08-21 19:56:44 -0300500 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
501 (char *)&fmt->pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300502
503 /* check if overlay is running */
504 if (IS_OVERLAY_ACTIVE(fh) != 0) {
505 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300506 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300507 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510
Hans Verkuilb9600742009-01-18 19:59:11 -0300511 /* ok, accept it */
512 vv->ov_fb = *fb;
513 vv->ov_fmt = fmt;
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300514
515 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
516 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
Joe Perches44d0b802011-08-21 19:56:44 -0300517 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300518 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300519 return 0;
520}
521
522static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
523{
524 if (f->index >= NUM_FORMATS)
525 return -EINVAL;
526 strlcpy((char *)f->description, formats[f->index].name,
527 sizeof(f->description));
528 f->pixelformat = formats[f->index].pixelformat;
529 return 0;
530}
531
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300532int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilb9600742009-01-18 19:59:11 -0300533{
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300534 struct saa7146_dev *dev = container_of(ctrl->handler,
535 struct saa7146_dev, ctrl_handler);
Hans Verkuilb9600742009-01-18 19:59:11 -0300536 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300537 u32 val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300538
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300539 switch (ctrl->id) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300540 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300541 val = saa7146_read(dev, BCS_CTRL);
542 val &= 0x00ffffff;
543 val |= (ctrl->val << 24);
544 saa7146_write(dev, BCS_CTRL, val);
545 saa7146_write(dev, MC2, MASK_22 | MASK_06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300547
Hans Verkuilb9600742009-01-18 19:59:11 -0300548 case V4L2_CID_CONTRAST:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300549 val = saa7146_read(dev, BCS_CTRL);
550 val &= 0xff00ffff;
551 val |= (ctrl->val << 16);
552 saa7146_write(dev, BCS_CTRL, val);
553 saa7146_write(dev, MC2, MASK_22 | MASK_06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300555
Hans Verkuilb9600742009-01-18 19:59:11 -0300556 case V4L2_CID_SATURATION:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300557 val = saa7146_read(dev, BCS_CTRL);
558 val &= 0xffffff00;
559 val |= (ctrl->val << 0);
560 saa7146_write(dev, BCS_CTRL, val);
Hans Verkuilb9600742009-01-18 19:59:11 -0300561 saa7146_write(dev, MC2, MASK_22 | MASK_06);
562 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300563
Hans Verkuilb9600742009-01-18 19:59:11 -0300564 case V4L2_CID_HFLIP:
565 /* fixme: we can support changing VFLIP and HFLIP here... */
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->hflip = 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 case V4L2_CID_VFLIP:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300572 if ((vv->video_status & STATUS_CAPTURE))
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300573 return -EBUSY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300574 vv->vflip = ctrl->val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300575 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300576
Hans Verkuilb9600742009-01-18 19:59:11 -0300577 default:
Hans Verkuilb9600742009-01-18 19:59:11 -0300578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300581 if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
582 struct saa7146_fh *fh = vv->video_fh;
583
Hans Verkuilb9600742009-01-18 19:59:11 -0300584 saa7146_stop_preview(fh);
585 saa7146_start_preview(fh);
586 }
587 return 0;
588}
589
590static int vidioc_g_parm(struct file *file, void *fh,
591 struct v4l2_streamparm *parm)
592{
Trent Piepho717167e2009-03-04 01:21:03 -0300593 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
594 struct saa7146_vv *vv = dev->vv_data;
595
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300596 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
597 return -EINVAL;
Hans Verkuilb9600742009-01-18 19:59:11 -0300598 parm->parm.capture.readbuffers = 1;
Trent Piepho717167e2009-03-04 01:21:03 -0300599 v4l2_video_std_frame_period(vv->standard->id,
600 &parm->parm.capture.timeperframe);
Hans Verkuilb9600742009-01-18 19:59:11 -0300601 return 0;
602}
603
604static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
605{
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300606 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
607 struct saa7146_vv *vv = dev->vv_data;
608
609 f->fmt.pix = vv->video_fmt;
Hans Verkuilb9600742009-01-18 19:59:11 -0300610 return 0;
611}
612
613static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
614{
Hans Verkuil5da545a2012-05-01 11:06:44 -0300615 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
616 struct saa7146_vv *vv = dev->vv_data;
617
618 f->fmt.win = vv->ov.win;
Hans Verkuilb9600742009-01-18 19:59:11 -0300619 return 0;
620}
621
622static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
623{
Hans Verkuilfca34692012-05-01 11:28:20 -0300624 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
625 struct saa7146_vv *vv = dev->vv_data;
626
627 f->fmt.vbi = vv->vbi_fmt;
Hans Verkuilb9600742009-01-18 19:59:11 -0300628 return 0;
629}
630
631static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
632{
633 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
634 struct saa7146_vv *vv = dev->vv_data;
635 struct saa7146_format *fmt;
636 enum v4l2_field field;
637 int maxw, maxh;
638 int calc_bpl;
639
Joe Perches44d0b802011-08-21 19:56:44 -0300640 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300641
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200642 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300643 if (NULL == fmt)
644 return -EINVAL;
645
646 field = f->fmt.pix.field;
647 maxw = vv->standard->h_max_out;
648 maxh = vv->standard->v_max_out;
649
650 if (V4L2_FIELD_ANY == field) {
651 field = (f->fmt.pix.height > maxh / 2)
652 ? V4L2_FIELD_INTERLACED
653 : V4L2_FIELD_BOTTOM;
654 }
655 switch (field) {
656 case V4L2_FIELD_ALTERNATE:
657 vv->last_field = V4L2_FIELD_TOP;
658 maxh = maxh / 2;
659 break;
660 case V4L2_FIELD_TOP:
661 case V4L2_FIELD_BOTTOM:
662 vv->last_field = V4L2_FIELD_INTERLACED;
663 maxh = maxh / 2;
664 break;
665 case V4L2_FIELD_INTERLACED:
666 vv->last_field = V4L2_FIELD_INTERLACED;
667 break;
668 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300669 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300670 return -EINVAL;
671 }
672
673 f->fmt.pix.field = field;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300674 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuilb9600742009-01-18 19:59:11 -0300675 if (f->fmt.pix.width > maxw)
676 f->fmt.pix.width = maxw;
677 if (f->fmt.pix.height > maxh)
678 f->fmt.pix.height = maxh;
679
680 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
681
682 if (f->fmt.pix.bytesperline < calc_bpl)
683 f->fmt.pix.bytesperline = calc_bpl;
684
685 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
686 f->fmt.pix.bytesperline = calc_bpl;
687
688 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
Joe Perches44d0b802011-08-21 19:56:44 -0300689 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
690 f->fmt.pix.width, f->fmt.pix.height,
691 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
Hans Verkuilb9600742009-01-18 19:59:11 -0300692
693 return 0;
694}
695
696
697static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
698{
699 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
700 struct saa7146_vv *vv = dev->vv_data;
701 struct v4l2_window *win = &f->fmt.win;
702 enum v4l2_field field;
703 int maxw, maxh;
704
Joe Perches44d0b802011-08-21 19:56:44 -0300705 DEB_EE("dev:%p\n", dev);
Hans Verkuilb9600742009-01-18 19:59:11 -0300706
707 if (NULL == vv->ov_fb.base) {
Joe Perches44d0b802011-08-21 19:56:44 -0300708 DEB_D("no fb base set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300709 return -EINVAL;
710 }
711 if (NULL == vv->ov_fmt) {
Joe Perches44d0b802011-08-21 19:56:44 -0300712 DEB_D("no fb fmt set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300713 return -EINVAL;
714 }
715 if (win->w.width < 48 || win->w.height < 32) {
Joe Perches44d0b802011-08-21 19:56:44 -0300716 DEB_D("min width/height. (%d,%d)\n",
717 win->w.width, win->w.height);
Hans Verkuilb9600742009-01-18 19:59:11 -0300718 return -EINVAL;
719 }
720 if (win->clipcount > 16) {
Joe Perches44d0b802011-08-21 19:56:44 -0300721 DEB_D("clipcount too big\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300722 return -EINVAL;
723 }
724
725 field = win->field;
726 maxw = vv->standard->h_max_out;
727 maxh = vv->standard->v_max_out;
728
729 if (V4L2_FIELD_ANY == field) {
730 field = (win->w.height > maxh / 2)
731 ? V4L2_FIELD_INTERLACED
732 : V4L2_FIELD_TOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300734 switch (field) {
735 case V4L2_FIELD_TOP:
736 case V4L2_FIELD_BOTTOM:
737 case V4L2_FIELD_ALTERNATE:
738 maxh = maxh / 2;
739 break;
740 case V4L2_FIELD_INTERLACED:
741 break;
742 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300743 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Hans Verkuilb9600742009-01-18 19:59:11 -0300747 win->field = field;
748 if (win->w.width > maxw)
749 win->w.width = maxw;
750 if (win->w.height > maxh)
751 win->w.height = maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Hans Verkuilb9600742009-01-18 19:59:11 -0300753 return 0;
754}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Hans Verkuilb9600742009-01-18 19:59:11 -0300756static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
757{
758 struct saa7146_fh *fh = __fh;
759 struct saa7146_dev *dev = fh->dev;
760 struct saa7146_vv *vv = dev->vv_data;
761 int err;
762
Joe Perches44d0b802011-08-21 19:56:44 -0300763 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300764 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300765 DEB_EE("streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300766 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300768 err = vidioc_try_fmt_vid_cap(file, fh, f);
769 if (0 != err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return err;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300771 vv->video_fmt = f->fmt.pix;
Joe Perches44d0b802011-08-21 19:56:44 -0300772 DEB_EE("set to pixelformat '%4.4s'\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300773 (char *)&vv->video_fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300774 return 0;
775}
776
777static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
778{
779 struct saa7146_fh *fh = __fh;
780 struct saa7146_dev *dev = fh->dev;
781 struct saa7146_vv *vv = dev->vv_data;
782 int err;
783
Joe Perches44d0b802011-08-21 19:56:44 -0300784 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300785 err = vidioc_try_fmt_vid_overlay(file, fh, f);
786 if (0 != err)
787 return err;
Hans Verkuil5da545a2012-05-01 11:06:44 -0300788 vv->ov.win = f->fmt.win;
789 vv->ov.nclips = f->fmt.win.clipcount;
790 if (vv->ov.nclips > 16)
791 vv->ov.nclips = 16;
792 if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
793 sizeof(struct v4l2_clip) * vv->ov.nclips)) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300794 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300796
Hans Verkuil5da545a2012-05-01 11:06:44 -0300797 /* vv->ov.fh is used to indicate that we have valid overlay informations, too */
798 vv->ov.fh = fh;
Hans Verkuilb9600742009-01-18 19:59:11 -0300799
Hans Verkuilb9600742009-01-18 19:59:11 -0300800 /* check if our current overlay is active */
801 if (IS_OVERLAY_ACTIVE(fh) != 0) {
802 saa7146_stop_preview(fh);
803 saa7146_start_preview(fh);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800804 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300805 return 0;
806}
807
808static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
809{
810 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
811 struct saa7146_vv *vv = dev->vv_data;
812
813 *norm = vv->standard->id;
814 return 0;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
818 PAL / NTSC / SECAM. if your hardware does not (or does more)
819 -- override this function in your extension */
Hans Verkuilb9600742009-01-18 19:59:11 -0300820/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 case VIDIOC_ENUMSTD:
822 {
823 struct v4l2_standard *e = arg;
824 if (e->index < 0 )
825 return -EINVAL;
826 if( e->index < dev->ext_vv_data->num_stds ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300827 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
829 return 0;
830 }
831 return -EINVAL;
832 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Hans Verkuilb9600742009-01-18 19:59:11 -0300835static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
836{
837 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
838 struct saa7146_vv *vv = dev->vv_data;
839 int found = 0;
840 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Joe Perches44d0b802011-08-21 19:56:44 -0300842 DEB_EE("VIDIOC_S_STD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Hans Verkuilb9600742009-01-18 19:59:11 -0300844 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300845 DEB_D("cannot change video standard while streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300846 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Hans Verkuilb9600742009-01-18 19:59:11 -0300849 if ((vv->video_status & STATUS_OVERLAY) != 0) {
850 vv->ov_suspend = vv->video_fh;
851 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 -0700852 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300853 DEB_D("suspending video failed. aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300855 }
856 }
Brandon Philips49ee7182007-10-05 16:26:27 -0300857
Hans Verkuilb9600742009-01-18 19:59:11 -0300858 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
859 if (*id & dev->ext_vv_data->stds[i].id)
860 break;
861 if (i != dev->ext_vv_data->num_stds) {
862 vv->standard = &dev->ext_vv_data->stds[i];
863 if (NULL != dev->ext_vv_data->std_callback)
864 dev->ext_vv_data->std_callback(dev, vv->standard);
865 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300867
Hans Verkuilb9600742009-01-18 19:59:11 -0300868 if (vv->ov_suspend != NULL) {
869 saa7146_start_preview(vv->ov_suspend);
870 vv->ov_suspend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300872
873 if (!found) {
Joe Perches44d0b802011-08-21 19:56:44 -0300874 DEB_EE("VIDIOC_S_STD: standard not found\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300875 return -EINVAL;
876 }
877
Joe Perches44d0b802011-08-21 19:56:44 -0300878 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881
Hans Verkuilb9600742009-01-18 19:59:11 -0300882static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
883{
884 int err;
885
Joe Perches44d0b802011-08-21 19:56:44 -0300886 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
Hans Verkuilb9600742009-01-18 19:59:11 -0300887 if (on)
888 err = saa7146_start_preview(fh);
889 else
890 err = saa7146_stop_preview(fh);
891 return err;
892}
893
894static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
895{
896 struct saa7146_fh *fh = __fh;
897
898 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
899 return videobuf_reqbufs(&fh->video_q, b);
900 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
901 return videobuf_reqbufs(&fh->vbi_q, b);
902 return -EINVAL;
903}
904
905static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
906{
907 struct saa7146_fh *fh = __fh;
908
909 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
910 return videobuf_querybuf(&fh->video_q, buf);
911 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
912 return videobuf_querybuf(&fh->vbi_q, buf);
913 return -EINVAL;
914}
915
916static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
917{
918 struct saa7146_fh *fh = __fh;
919
920 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
921 return videobuf_qbuf(&fh->video_q, buf);
922 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
923 return videobuf_qbuf(&fh->vbi_q, buf);
924 return -EINVAL;
925}
926
927static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
928{
929 struct saa7146_fh *fh = __fh;
930
931 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
933 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
934 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
935 return -EINVAL;
936}
937
938static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
939{
940 struct saa7146_fh *fh = __fh;
941 int err;
942
Joe Perches44d0b802011-08-21 19:56:44 -0300943 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300944
945 err = video_begin(fh);
946 if (err)
947 return err;
948 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
949 return videobuf_streamon(&fh->video_q);
950 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
951 return videobuf_streamon(&fh->vbi_q);
952 return -EINVAL;
953}
954
955static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
956{
957 struct saa7146_fh *fh = __fh;
958 struct saa7146_dev *dev = fh->dev;
959 struct saa7146_vv *vv = dev->vv_data;
960 int err;
961
Joe Perches44d0b802011-08-21 19:56:44 -0300962 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300963
964 /* ugly: we need to copy some checks from video_end(),
965 because videobuf_streamoff() relies on the capture running.
966 check and fix this */
967 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300968 DEB_S("not capturing\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300969 return 0;
970 }
971
972 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300973 DEB_S("capturing, but in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300974 return -EBUSY;
975 }
976
977 err = -EINVAL;
978 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
979 err = videobuf_streamoff(&fh->video_q);
980 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
981 err = videobuf_streamoff(&fh->vbi_q);
982 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300983 DEB_D("warning: videobuf_streamoff() failed\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300984 video_end(fh, file);
985 } else {
986 err = video_end(fh, file);
987 }
988 return err;
989}
990
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300991static int vidioc_g_chip_ident(struct file *file, void *__fh,
992 struct v4l2_dbg_chip_ident *chip)
993{
994 struct saa7146_fh *fh = __fh;
995 struct saa7146_dev *dev = fh->dev;
996
997 chip->ident = V4L2_IDENT_NONE;
998 chip->revision = 0;
Hans Verkuilab49ae02012-05-01 12:57:57 -0300999 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1000 if (v4l2_chip_match_host(&chip->match))
1001 chip->ident = V4L2_IDENT_SAA7146;
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001002 return 0;
1003 }
Hans Verkuilab49ae02012-05-01 12:57:57 -03001004 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1005 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1006 return -EINVAL;
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001007 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1008 core, g_chip_ident, chip);
1009}
1010
Hans Verkuilb9600742009-01-18 19:59:11 -03001011const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1012 .vidioc_querycap = vidioc_querycap,
1013 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Trent Piepho717167e2009-03-04 01:21:03 -03001014 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
Hans Verkuilb9600742009-01-18 19:59:11 -03001015 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1016 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1017 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1018 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1019 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1020 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001021 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuilb9600742009-01-18 19:59:11 -03001022
1023 .vidioc_overlay = vidioc_overlay,
1024 .vidioc_g_fbuf = vidioc_g_fbuf,
1025 .vidioc_s_fbuf = vidioc_s_fbuf,
1026 .vidioc_reqbufs = vidioc_reqbufs,
1027 .vidioc_querybuf = vidioc_querybuf,
1028 .vidioc_qbuf = vidioc_qbuf,
1029 .vidioc_dqbuf = vidioc_dqbuf,
1030 .vidioc_g_std = vidioc_g_std,
1031 .vidioc_s_std = vidioc_s_std,
Hans Verkuilb9600742009-01-18 19:59:11 -03001032 .vidioc_streamon = vidioc_streamon,
1033 .vidioc_streamoff = vidioc_streamoff,
1034 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuil537fa492012-05-01 12:04:52 -03001035 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1036 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilb9600742009-01-18 19:59:11 -03001037};
1038
Hans Verkuilab49ae02012-05-01 12:57:57 -03001039const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
1040 .vidioc_querycap = vidioc_querycap,
1041 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1042 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1043
1044 .vidioc_reqbufs = vidioc_reqbufs,
1045 .vidioc_querybuf = vidioc_querybuf,
1046 .vidioc_qbuf = vidioc_qbuf,
1047 .vidioc_dqbuf = vidioc_dqbuf,
1048 .vidioc_g_std = vidioc_g_std,
1049 .vidioc_s_std = vidioc_s_std,
1050 .vidioc_streamon = vidioc_streamon,
1051 .vidioc_streamoff = vidioc_streamoff,
1052 .vidioc_g_parm = vidioc_g_parm,
1053 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1054 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1055};
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/*********************************************************************************/
1058/* buffer handling functions */
1059
1060static int buffer_activate (struct saa7146_dev *dev,
1061 struct saa7146_buf *buf,
1062 struct saa7146_buf *next)
1063{
1064 struct saa7146_vv *vv = dev->vv_data;
1065
Brandon Philips0fc06862007-11-06 20:02:36 -03001066 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 saa7146_set_capture(dev,buf,next);
1068
Hans Verkuil9bb60192012-05-01 11:45:27 -03001069 mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071}
1072
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001073static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1074{
1075 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1076 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1077 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080static int buffer_prepare(struct videobuf_queue *q,
1081 struct videobuf_buffer *vb, enum v4l2_field field)
1082{
1083 struct file *file = q->priv_data;
1084 struct saa7146_fh *fh = file->private_data;
1085 struct saa7146_dev *dev = fh->dev;
1086 struct saa7146_vv *vv = dev->vv_data;
1087 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1088 int size,err = 0;
1089
Joe Perches44d0b802011-08-21 19:56:44 -03001090 DEB_CAP("vbuf:%p\n", vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* sanity checks */
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001093 if (vv->video_fmt.width < 48 ||
1094 vv->video_fmt.height < 32 ||
1095 vv->video_fmt.width > vv->standard->h_max_out ||
1096 vv->video_fmt.height > vv->standard->v_max_out) {
Joe Perches44d0b802011-08-21 19:56:44 -03001097 DEB_D("w (%d) / h (%d) out of bounds\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001098 vv->video_fmt.width, vv->video_fmt.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -EINVAL;
1100 }
1101
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001102 size = vv->video_fmt.sizeimage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
Joe Perches44d0b802011-08-21 19:56:44 -03001104 DEB_D("size mismatch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EINVAL;
1106 }
1107
Joe Perches44d0b802011-08-21 19:56:44 -03001108 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001109 vv->video_fmt.width, vv->video_fmt.height,
1110 size, v4l2_field_names[vv->video_fmt.field]);
1111 if (buf->vb.width != vv->video_fmt.width ||
1112 buf->vb.bytesperline != vv->video_fmt.bytesperline ||
1113 buf->vb.height != vv->video_fmt.height ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buf->vb.size != size ||
1115 buf->vb.field != field ||
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001116 buf->vb.field != vv->video_fmt.field ||
1117 buf->fmt != &vv->video_fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001118 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
Brandon Philips0fc06862007-11-06 20:02:36 -03001121 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct saa7146_format *sfmt;
1123
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001124 buf->vb.bytesperline = vv->video_fmt.bytesperline;
1125 buf->vb.width = vv->video_fmt.width;
1126 buf->vb.height = vv->video_fmt.height;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 buf->vb.size = size;
1128 buf->vb.field = field;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001129 buf->fmt = &vv->video_fmt;
1130 buf->vb.field = vv->video_fmt.field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001132 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001134 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if( 0 != IS_PLANAR(sfmt->trans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1137 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1138 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1139 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1141 }
1142
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001143 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (err)
1145 goto oops;
1146 err = saa7146_pgtable_build(dev,buf);
1147 if (err)
1148 goto oops;
1149 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001150 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 buf->activate = buffer_activate;
1152
1153 return 0;
1154
1155 oops:
Joe Perches44d0b802011-08-21 19:56:44 -03001156 DEB_D("error out\n");
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001157 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return err;
1160}
1161
1162static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1163{
1164 struct file *file = q->priv_data;
1165 struct saa7146_fh *fh = file->private_data;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001166 struct saa7146_vv *vv = fh->dev->vv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1169 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1170
Hans Verkuilfd74d6e2012-05-01 11:17:35 -03001171 *size = vv->video_fmt.sizeimage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* check if we exceed the "max_memory" parameter */
1174 if( (*count * *size) > (max_memory*1048576) ) {
1175 *count = (max_memory*1048576) / *size;
1176 }
1177
Joe Perches44d0b802011-08-21 19:56:44 -03001178 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return 0;
1181}
1182
1183static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1184{
1185 struct file *file = q->priv_data;
1186 struct saa7146_fh *fh = file->private_data;
1187 struct saa7146_dev *dev = fh->dev;
1188 struct saa7146_vv *vv = dev->vv_data;
1189 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1190
Joe Perches44d0b802011-08-21 19:56:44 -03001191 DEB_CAP("vbuf:%p\n", vb);
Hans Verkuil9bb60192012-05-01 11:45:27 -03001192 saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1196{
1197 struct file *file = q->priv_data;
1198 struct saa7146_fh *fh = file->private_data;
1199 struct saa7146_dev *dev = fh->dev;
1200 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1201
Joe Perches44d0b802011-08-21 19:56:44 -03001202 DEB_CAP("vbuf:%p\n", vb);
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001203
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001204 saa7146_dma_free(dev,q,buf);
Mauro Carvalho Chehabba9e9f32010-02-01 21:23:46 -02001205
1206 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static struct videobuf_queue_ops video_qops = {
1210 .buf_setup = buffer_setup,
1211 .buf_prepare = buffer_prepare,
1212 .buf_queue = buffer_queue,
1213 .buf_release = buffer_release,
1214};
1215
1216/********************************************************************************/
1217/* file operations */
1218
1219static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1220{
Hans Verkuil9bb60192012-05-01 11:45:27 -03001221 INIT_LIST_HEAD(&vv->video_dmaq.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Hans Verkuil9bb60192012-05-01 11:45:27 -03001223 init_timer(&vv->video_dmaq.timeout);
1224 vv->video_dmaq.timeout.function = saa7146_buffer_timeout;
1225 vv->video_dmaq.timeout.data = (unsigned long)(&vv->video_dmaq);
1226 vv->video_dmaq.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* set some default values */
1229 vv->standard = &dev->ext_vv_data->stds[0];
1230
1231 /* FIXME: what's this? */
1232 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1233 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1234}
1235
1236
1237static int video_open(struct saa7146_dev *dev, struct file *file)
1238{
Joe Perchesabf84382010-07-12 17:50:03 -03001239 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001241 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1242 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1244 V4L2_FIELD_INTERLACED,
1245 sizeof(struct saa7146_buf),
Hans Verkuil9af39712010-12-18 09:20:59 -03001246 file, &dev->v4l2_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
1249}
1250
1251
1252static void video_close(struct saa7146_dev *dev, struct file *file)
1253{
Joe Perchesabf84382010-07-12 17:50:03 -03001254 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct saa7146_vv *vv = dev->vv_data;
Hartmut Birr2970c492007-04-22 06:57:26 -03001256 struct videobuf_queue *q = &fh->video_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Hans Verkuil7b4668e2011-08-25 09:54:30 -03001258 if (IS_CAPTURE_ACTIVE(fh) != 0)
1259 video_end(fh, file);
1260 else if (IS_OVERLAY_ACTIVE(fh) != 0)
1261 saa7146_stop_preview(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Brandon Philips19bc5132007-11-13 20:05:38 -03001263 videobuf_stop(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* hmm, why is this function declared void? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267
1268static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1269{
1270 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuil9bb60192012-05-01 11:45:27 -03001271 struct saa7146_dmaqueue *q = &vv->video_dmaq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 spin_lock(&dev->slock);
Joe Perches44d0b802011-08-21 19:56:44 -03001274 DEB_CAP("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* only finish the buffer if we have one... */
1277 if( NULL != q->curr ) {
Brandon Philips0fc06862007-11-06 20:02:36 -03001278 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 saa7146_buffer_next(dev,q,0);
1281
1282 spin_unlock(&dev->slock);
1283}
1284
1285static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1286{
1287 struct saa7146_fh *fh = file->private_data;
1288 struct saa7146_dev *dev = fh->dev;
1289 struct saa7146_vv *vv = dev->vv_data;
1290 ssize_t ret = 0;
1291
Joe Perches44d0b802011-08-21 19:56:44 -03001292 DEB_EE("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1295 /* fixme: should we allow read() captures while streaming capture? */
1296 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -03001297 DEB_S("already capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EBUSY;
1299 }
Joe Perches44d0b802011-08-21 19:56:44 -03001300 DEB_S("already capturing in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return -EBUSY;
1302 }
1303
1304 ret = video_begin(fh);
1305 if( 0 != ret) {
1306 goto out;
1307 }
1308
1309 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1310 file->f_flags & O_NONBLOCK);
1311 if (ret != 0) {
1312 video_end(fh, file);
1313 } else {
1314 ret = video_end(fh, file);
1315 }
1316out:
1317 /* restart overlay if it was active before */
1318 if (vv->ov_suspend != NULL) {
1319 saa7146_start_preview(vv->ov_suspend);
1320 vv->ov_suspend = NULL;
1321 }
1322
1323 return ret;
1324}
1325
1326struct saa7146_use_ops saa7146_video_uops = {
1327 .init = video_init,
1328 .open = video_open,
1329 .release = video_close,
1330 .irq_done = video_irq_done,
1331 .read = video_read,
1332};