blob: 3a00253fe1ee8bc6ab5e0e3fb89ef8cd451feef2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <media/saa7146_vv.h>
Hans Verkuil1b8dac12009-02-07 11:18:05 -03002#include <media/v4l2-chip-ident.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04003#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5static int max_memory = 32;
6
7module_param(max_memory, int, 0644);
8MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
9
10#define IS_CAPTURE_ACTIVE(fh) \
11 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
12
13#define IS_OVERLAY_ACTIVE(fh) \
14 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
15
16/* format descriptions for capture and preview */
17static struct saa7146_format formats[] = {
18 {
19 .name = "RGB-8 (3-3-2)",
20 .pixelformat = V4L2_PIX_FMT_RGB332,
21 .trans = RGB08_COMPOSED,
22 .depth = 8,
23 .flags = 0,
24 }, {
25 .name = "RGB-16 (5/B-6/G-5/R)",
26 .pixelformat = V4L2_PIX_FMT_RGB565,
27 .trans = RGB16_COMPOSED,
28 .depth = 16,
29 .flags = 0,
30 }, {
31 .name = "RGB-24 (B-G-R)",
32 .pixelformat = V4L2_PIX_FMT_BGR24,
33 .trans = RGB24_COMPOSED,
34 .depth = 24,
35 .flags = 0,
36 }, {
37 .name = "RGB-32 (B-G-R)",
38 .pixelformat = V4L2_PIX_FMT_BGR32,
39 .trans = RGB32_COMPOSED,
40 .depth = 32,
41 .flags = 0,
42 }, {
43 .name = "RGB-32 (R-G-B)",
44 .pixelformat = V4L2_PIX_FMT_RGB32,
45 .trans = RGB32_COMPOSED,
46 .depth = 32,
47 .flags = 0,
48 .swap = 0x2,
49 }, {
50 .name = "Greyscale-8",
51 .pixelformat = V4L2_PIX_FMT_GREY,
52 .trans = Y8,
53 .depth = 8,
54 .flags = 0,
55 }, {
56 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
57 .pixelformat = V4L2_PIX_FMT_YUV422P,
58 .trans = YUV422_DECOMPOSED,
59 .depth = 16,
60 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
61 }, {
62 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
63 .pixelformat = V4L2_PIX_FMT_YVU420,
64 .trans = YUV420_DECOMPOSED,
65 .depth = 12,
66 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
67 }, {
68 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
69 .pixelformat = V4L2_PIX_FMT_YUV420,
70 .trans = YUV420_DECOMPOSED,
71 .depth = 12,
72 .flags = FORMAT_IS_PLANAR,
73 }, {
74 .name = "YUV 4:2:2 (U-Y-V-Y)",
75 .pixelformat = V4L2_PIX_FMT_UYVY,
76 .trans = YUV422_COMPOSED,
77 .depth = 16,
78 .flags = 0,
79 }
80};
81
82/* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
83 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
84 (like V4L2_PIX_FMT_YUYV) ... 8-( */
85
86static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
87
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -020088struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int i, j = NUM_FORMATS;
91
92 for (i = 0; i < j; i++) {
93 if (formats[i].pixelformat == fourcc) {
94 return formats+i;
95 }
96 }
97
98 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
99 return NULL;
100}
101
Hans Verkuilb9600742009-01-18 19:59:11 -0300102static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104int saa7146_start_preview(struct saa7146_fh *fh)
105{
106 struct saa7146_dev *dev = fh->dev;
107 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuilb9600742009-01-18 19:59:11 -0300108 struct v4l2_format fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int ret = 0, err = 0;
110
111 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
112
113 /* check if we have overlay informations */
114 if( NULL == fh->ov.fh ) {
115 DEB_D(("no overlay data available. try S_FMT first.\n"));
116 return -EAGAIN;
117 }
118
119 /* check if streaming capture is running */
120 if (IS_CAPTURE_ACTIVE(fh) != 0) {
121 DEB_D(("streaming capture is active.\n"));
122 return -EBUSY;
123 }
124
125 /* check if overlay is running */
126 if (IS_OVERLAY_ACTIVE(fh) != 0) {
127 if (vv->video_fh == fh) {
128 DEB_D(("overlay is already active.\n"));
129 return 0;
130 }
131 DEB_D(("overlay is already active in another open.\n"));
132 return -EBUSY;
133 }
134
135 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
136 DEB_D(("cannot get necessary overlay resources\n"));
137 return -EBUSY;
138 }
139
Hans Verkuilb9600742009-01-18 19:59:11 -0300140 fmt.fmt.win = fh->ov.win;
141 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (0 != err) {
143 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
144 return -EBUSY;
145 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300146 fh->ov.win = fmt.fmt.win;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 vv->ov_data = &fh->ov;
148
149 DEB_D(("%dx%d+%d+%d %s field=%s\n",
150 fh->ov.win.w.width,fh->ov.win.w.height,
151 fh->ov.win.w.left,fh->ov.win.w.top,
152 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
153
154 if (0 != (ret = saa7146_enable_overlay(fh))) {
155 DEB_D(("enabling overlay failed: %d\n",ret));
156 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
157 return ret;
158 }
159
160 vv->video_status = STATUS_OVERLAY;
161 vv->video_fh = fh;
162
163 return 0;
164}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300165EXPORT_SYMBOL_GPL(saa7146_start_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167int saa7146_stop_preview(struct saa7146_fh *fh)
168{
169 struct saa7146_dev *dev = fh->dev;
170 struct saa7146_vv *vv = dev->vv_data;
171
172 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
173
174 /* check if streaming capture is running */
175 if (IS_CAPTURE_ACTIVE(fh) != 0) {
176 DEB_D(("streaming capture is active.\n"));
177 return -EBUSY;
178 }
179
180 /* check if overlay is running at all */
181 if ((vv->video_status & STATUS_OVERLAY) == 0) {
182 DEB_D(("no active overlay.\n"));
183 return 0;
184 }
185
186 if (vv->video_fh != fh) {
187 DEB_D(("overlay is active, but in another open.\n"));
188 return -EBUSY;
189 }
190
191 vv->video_status = 0;
192 vv->video_fh = NULL;
193
194 saa7146_disable_overlay(fh);
195
196 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
197
198 return 0;
199}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300200EXPORT_SYMBOL_GPL(saa7146_stop_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/********************************************************************************/
203/* device controls */
204
205static struct v4l2_queryctrl controls[] = {
206 {
207 .id = V4L2_CID_BRIGHTNESS,
208 .name = "Brightness",
209 .minimum = 0,
210 .maximum = 255,
211 .step = 1,
212 .default_value = 128,
213 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300214 .flags = V4L2_CTRL_FLAG_SLIDER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 },{
216 .id = V4L2_CID_CONTRAST,
217 .name = "Contrast",
218 .minimum = 0,
219 .maximum = 127,
220 .step = 1,
221 .default_value = 64,
222 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300223 .flags = V4L2_CTRL_FLAG_SLIDER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 },{
225 .id = V4L2_CID_SATURATION,
226 .name = "Saturation",
227 .minimum = 0,
228 .maximum = 127,
229 .step = 1,
230 .default_value = 64,
231 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300232 .flags = V4L2_CTRL_FLAG_SLIDER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 },{
234 .id = V4L2_CID_VFLIP,
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300235 .name = "Vertical Flip",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .minimum = 0,
237 .maximum = 1,
238 .type = V4L2_CTRL_TYPE_BOOLEAN,
239 },{
240 .id = V4L2_CID_HFLIP,
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300241 .name = "Horizontal Flip",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .minimum = 0,
243 .maximum = 1,
244 .type = V4L2_CTRL_TYPE_BOOLEAN,
245 },
246};
247static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
248
249#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
250
251static struct v4l2_queryctrl* ctrl_by_id(int id)
252{
253 int i;
254
255 for (i = 0; i < NUM_CONTROLS; i++)
256 if (controls[i].id == id)
257 return controls+i;
258 return NULL;
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/********************************************************************************/
262/* common pagetable functions */
263
264static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
265{
266 struct pci_dev *pci = dev->pci;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300267 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
268 struct scatterlist *list = dma->sglist;
269 int length = dma->sglen;
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200270 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
273
274 if( 0 != IS_PLANAR(sfmt->trans)) {
275 struct saa7146_pgtable *pt1 = &buf->pt[0];
276 struct saa7146_pgtable *pt2 = &buf->pt[1];
277 struct saa7146_pgtable *pt3 = &buf->pt[2];
Al Viroa36ef6b2008-06-22 14:19:19 -0300278 __le32 *ptr1, *ptr2, *ptr3;
279 __le32 fill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 int size = buf->fmt->width*buf->fmt->height;
282 int i,p,m1,m2,m3,o1,o2;
283
284 switch( sfmt->depth ) {
285 case 12: {
286 /* create some offsets inside the page table */
287 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
288 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
289 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
290 o1 = size%PAGE_SIZE;
291 o2 = (size+(size/4))%PAGE_SIZE;
292 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
293 break;
294 }
295 case 16: {
296 /* create some offsets inside the page table */
297 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
298 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
299 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
300 o1 = size%PAGE_SIZE;
301 o2 = (size+(size/2))%PAGE_SIZE;
302 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
303 break;
304 }
305 default: {
306 return -1;
307 }
308 }
309
310 ptr1 = pt1->cpu;
311 ptr2 = pt2->cpu;
312 ptr3 = pt3->cpu;
313
314 /* walk all pages, copy all page addresses to ptr1 */
315 for (i = 0; i < length; i++, list++) {
316 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
317 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
318 }
319 }
320/*
321 ptr1 = pt1->cpu;
322 for(j=0;j<40;j++) {
323 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
324 }
325*/
326
327 /* if we have a user buffer, the first page may not be
328 aligned to a page boundary. */
Hans Verkuil429e9082008-07-27 14:08:54 -0300329 pt1->offset = dma->sglist->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 pt2->offset = pt1->offset+o1;
331 pt3->offset = pt1->offset+o2;
332
333 /* create video-dma2 page table */
334 ptr1 = pt1->cpu;
335 for(i = m1; i <= m2 ; i++, ptr2++) {
336 *ptr2 = ptr1[i];
337 }
338 fill = *(ptr2-1);
339 for(;i<1024;i++,ptr2++) {
340 *ptr2 = fill;
341 }
342 /* create video-dma3 page table */
343 ptr1 = pt1->cpu;
344 for(i = m2; i <= m3; i++,ptr3++) {
345 *ptr3 = ptr1[i];
346 }
347 fill = *(ptr3-1);
348 for(;i<1024;i++,ptr3++) {
349 *ptr3 = fill;
350 }
351 /* finally: finish up video-dma1 page table */
352 ptr1 = pt1->cpu+m1;
353 fill = pt1->cpu[m1];
354 for(i=m1;i<1024;i++,ptr1++) {
355 *ptr1 = fill;
356 }
357/*
358 ptr1 = pt1->cpu;
359 ptr2 = pt2->cpu;
360 ptr3 = pt3->cpu;
361 for(j=0;j<40;j++) {
362 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
363 }
364 for(j=0;j<40;j++) {
365 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
366 }
367 for(j=0;j<40;j++) {
368 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
369 }
370*/
371 } else {
372 struct saa7146_pgtable *pt = &buf->pt[0];
373 return saa7146_pgtable_build_single(pci, pt, list, length);
374 }
375
376 return 0;
377}
378
379
380/********************************************************************************/
381/* file operations */
382
383static int video_begin(struct saa7146_fh *fh)
384{
385 struct saa7146_dev *dev = fh->dev;
386 struct saa7146_vv *vv = dev->vv_data;
387 struct saa7146_format *fmt = NULL;
388 unsigned int resource;
389 int ret = 0, err = 0;
390
391 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
392
393 if ((vv->video_status & STATUS_CAPTURE) != 0) {
394 if (vv->video_fh == fh) {
395 DEB_S(("already capturing.\n"));
396 return 0;
397 }
398 DEB_S(("already capturing in another open.\n"));
399 return -EBUSY;
400 }
401
402 if ((vv->video_status & STATUS_OVERLAY) != 0) {
403 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
404 vv->ov_suspend = vv->video_fh;
405 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
406 if (0 != err) {
407 DEB_D(("suspending video failed. aborting\n"));
408 return err;
409 }
410 }
411
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200412 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* we need to have a valid format set here */
414 BUG_ON(NULL == fmt);
415
416 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
417 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
418 } else {
419 resource = RESOURCE_DMA1_HPS;
420 }
421
422 ret = saa7146_res_get(fh, resource);
423 if (0 == ret) {
424 DEB_S(("cannot get capture resource %d\n",resource));
425 if (vv->ov_suspend != NULL) {
426 saa7146_start_preview(vv->ov_suspend);
427 vv->ov_suspend = NULL;
428 }
429 return -EBUSY;
430 }
431
432 /* clear out beginning of streaming bit (rps register 0)*/
433 saa7146_write(dev, MC2, MASK_27 );
434
435 /* enable rps0 irqs */
436 SAA7146_IER_ENABLE(dev, MASK_27);
437
438 vv->video_fh = fh;
439 vv->video_status = STATUS_CAPTURE;
440
441 return 0;
442}
443
444static int video_end(struct saa7146_fh *fh, struct file *file)
445{
446 struct saa7146_dev *dev = fh->dev;
447 struct saa7146_vv *vv = dev->vv_data;
448 struct saa7146_format *fmt = NULL;
449 unsigned long flags;
450 unsigned int resource;
451 u32 dmas = 0;
452 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
453
454 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
455 DEB_S(("not capturing.\n"));
456 return 0;
457 }
458
459 if (vv->video_fh != fh) {
460 DEB_S(("capturing, but in another open.\n"));
461 return -EBUSY;
462 }
463
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200464 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* we need to have a valid format set here */
466 BUG_ON(NULL == fmt);
467
468 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
469 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
470 dmas = MASK_22 | MASK_21 | MASK_20;
471 } else {
472 resource = RESOURCE_DMA1_HPS;
473 dmas = MASK_22;
474 }
475 spin_lock_irqsave(&dev->slock,flags);
476
477 /* disable rps0 */
478 saa7146_write(dev, MC1, MASK_28);
479
480 /* disable rps0 irqs */
481 SAA7146_IER_DISABLE(dev, MASK_27);
482
483 /* shut down all used video dma transfers */
484 saa7146_write(dev, MC1, dmas);
485
486 spin_unlock_irqrestore(&dev->slock, flags);
487
488 vv->video_fh = NULL;
489 vv->video_status = 0;
490
491 saa7146_res_free(fh, resource);
492
493 if (vv->ov_suspend != NULL) {
494 saa7146_start_preview(vv->ov_suspend);
495 vv->ov_suspend = NULL;
496 }
497
498 return 0;
499}
500
Hans Verkuilb9600742009-01-18 19:59:11 -0300501static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Hans Verkuilb9600742009-01-18 19:59:11 -0300503 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
504
505 strcpy((char *)cap->driver, "saa7146 v4l2");
506 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
507 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
508 cap->version = SAA7146_VERSION_CODE;
509 cap->capabilities =
510 V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_VIDEO_OVERLAY |
512 V4L2_CAP_READWRITE |
513 V4L2_CAP_STREAMING;
514 cap->capabilities |= dev->ext_vv_data->capabilities;
515 return 0;
516}
517
518static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
519{
520 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct saa7146_vv *vv = dev->vv_data;
522
Hans Verkuilb9600742009-01-18 19:59:11 -0300523 *fb = vv->ov_fb;
524 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
525 return 0;
526}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Hans Verkuilb9600742009-01-18 19:59:11 -0300528static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
529{
530 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
531 struct saa7146_vv *vv = dev->vv_data;
532 struct saa7146_format *fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Hans Verkuilb9600742009-01-18 19:59:11 -0300534 DEB_EE(("VIDIOC_S_FBUF\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Hans Verkuilb9600742009-01-18 19:59:11 -0300536 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
537 return -EPERM;
538
539 /* check args */
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200540 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300541 if (NULL == fmt)
542 return -EINVAL;
543
544 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
545 if (fmt->flags & FORMAT_IS_PLANAR)
546 DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",
547 (char *)&fmt->pixelformat));
548
549 /* check if overlay is running */
550 if (IS_OVERLAY_ACTIVE(fh) != 0) {
551 if (vv->video_fh != fh) {
552 DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
553 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 }
556
Hans Verkuilb9600742009-01-18 19:59:11 -0300557 /* ok, accept it */
558 vv->ov_fb = *fb;
559 vv->ov_fmt = fmt;
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300560
561 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
562 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
563 DEB_D(("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline));
564 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300565 return 0;
566}
567
568static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
569{
570 if (f->index >= NUM_FORMATS)
571 return -EINVAL;
572 strlcpy((char *)f->description, formats[f->index].name,
573 sizeof(f->description));
574 f->pixelformat = formats[f->index].pixelformat;
575 return 0;
576}
577
Hans Verkuilb9600742009-01-18 19:59:11 -0300578static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
579{
580 const struct v4l2_queryctrl *ctrl;
581
582 if ((c->id < V4L2_CID_BASE ||
583 c->id >= V4L2_CID_LASTP1) &&
584 (c->id < V4L2_CID_PRIVATE_BASE ||
585 c->id >= V4L2_CID_PRIVATE_LASTP1))
586 return -EINVAL;
587
588 ctrl = ctrl_by_id(c->id);
589 if (ctrl == NULL)
590 return -EINVAL;
591
592 DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n", c->id));
593 *c = *ctrl;
594 return 0;
595}
596
597static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
598{
599 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
600 struct saa7146_vv *vv = dev->vv_data;
601 const struct v4l2_queryctrl *ctrl;
602 u32 value = 0;
603
604 ctrl = ctrl_by_id(c->id);
605 if (NULL == ctrl)
606 return -EINVAL;
607 switch (c->id) {
608 case V4L2_CID_BRIGHTNESS:
609 value = saa7146_read(dev, BCS_CTRL);
610 c->value = 0xff & (value >> 24);
611 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n", c->value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
Hans Verkuilb9600742009-01-18 19:59:11 -0300613 case V4L2_CID_CONTRAST:
614 value = saa7146_read(dev, BCS_CTRL);
615 c->value = 0x7f & (value >> 16);
616 DEB_D(("V4L2_CID_CONTRAST: %d\n", c->value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Hans Verkuilb9600742009-01-18 19:59:11 -0300618 case V4L2_CID_SATURATION:
619 value = saa7146_read(dev, BCS_CTRL);
620 c->value = 0x7f & (value >> 0);
621 DEB_D(("V4L2_CID_SATURATION: %d\n", c->value));
622 break;
623 case V4L2_CID_VFLIP:
624 c->value = vv->vflip;
625 DEB_D(("V4L2_CID_VFLIP: %d\n", c->value));
626 break;
627 case V4L2_CID_HFLIP:
628 c->value = vv->hflip;
629 DEB_D(("V4L2_CID_HFLIP: %d\n", c->value));
630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 default:
Hans Verkuilb9600742009-01-18 19:59:11 -0300632 return -EINVAL;
633 }
634 return 0;
635}
636
637static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
638{
639 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
640 struct saa7146_vv *vv = dev->vv_data;
641 const struct v4l2_queryctrl *ctrl;
642
643 ctrl = ctrl_by_id(c->id);
644 if (NULL == ctrl) {
645 DEB_D(("unknown control %d\n", c->id));
646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Hans Verkuilb9600742009-01-18 19:59:11 -0300649 switch (ctrl->type) {
650 case V4L2_CTRL_TYPE_BOOLEAN:
651 case V4L2_CTRL_TYPE_MENU:
652 case V4L2_CTRL_TYPE_INTEGER:
653 if (c->value < ctrl->minimum)
654 c->value = ctrl->minimum;
655 if (c->value > ctrl->maximum)
656 c->value = ctrl->maximum;
657 break;
658 default:
659 /* nothing */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Hans Verkuilb9600742009-01-18 19:59:11 -0300662 switch (c->id) {
663 case V4L2_CID_BRIGHTNESS: {
664 u32 value = saa7146_read(dev, BCS_CTRL);
665 value &= 0x00ffffff;
666 value |= (c->value << 24);
667 saa7146_write(dev, BCS_CTRL, value);
668 saa7146_write(dev, MC2, MASK_22 | MASK_06);
669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300671 case V4L2_CID_CONTRAST: {
672 u32 value = saa7146_read(dev, BCS_CTRL);
673 value &= 0xff00ffff;
674 value |= (c->value << 16);
675 saa7146_write(dev, BCS_CTRL, value);
676 saa7146_write(dev, MC2, MASK_22 | MASK_06);
677 break;
678 }
679 case V4L2_CID_SATURATION: {
680 u32 value = saa7146_read(dev, BCS_CTRL);
681 value &= 0xffffff00;
682 value |= (c->value << 0);
683 saa7146_write(dev, BCS_CTRL, value);
684 saa7146_write(dev, MC2, MASK_22 | MASK_06);
685 break;
686 }
687 case V4L2_CID_HFLIP:
688 /* fixme: we can support changing VFLIP and HFLIP here... */
689 if (IS_CAPTURE_ACTIVE(fh) != 0) {
690 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300691 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300693 vv->hflip = c->value;
694 break;
695 case V4L2_CID_VFLIP:
696 if (IS_CAPTURE_ACTIVE(fh) != 0) {
697 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300698 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300700 vv->vflip = c->value;
701 break;
702 default:
Hans Verkuilb9600742009-01-18 19:59:11 -0300703 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Hans Verkuilb9600742009-01-18 19:59:11 -0300706 if (IS_OVERLAY_ACTIVE(fh) != 0) {
707 saa7146_stop_preview(fh);
708 saa7146_start_preview(fh);
709 }
710 return 0;
711}
712
713static int vidioc_g_parm(struct file *file, void *fh,
714 struct v4l2_streamparm *parm)
715{
Trent Piepho717167e2009-03-04 01:21:03 -0300716 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
717 struct saa7146_vv *vv = dev->vv_data;
718
Hans Verkuilb9600742009-01-18 19:59:11 -0300719 parm->parm.capture.readbuffers = 1;
Trent Piepho717167e2009-03-04 01:21:03 -0300720 v4l2_video_std_frame_period(vv->standard->id,
721 &parm->parm.capture.timeperframe);
Hans Verkuilb9600742009-01-18 19:59:11 -0300722 return 0;
723}
724
725static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
726{
727 f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
728 return 0;
729}
730
731static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
732{
733 f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
734 return 0;
735}
736
737static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
738{
739 f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
740 return 0;
741}
742
743static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
744{
745 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
746 struct saa7146_vv *vv = dev->vv_data;
747 struct saa7146_format *fmt;
748 enum v4l2_field field;
749 int maxw, maxh;
750 int calc_bpl;
751
752 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
753
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200754 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300755 if (NULL == fmt)
756 return -EINVAL;
757
758 field = f->fmt.pix.field;
759 maxw = vv->standard->h_max_out;
760 maxh = vv->standard->v_max_out;
761
762 if (V4L2_FIELD_ANY == field) {
763 field = (f->fmt.pix.height > maxh / 2)
764 ? V4L2_FIELD_INTERLACED
765 : V4L2_FIELD_BOTTOM;
766 }
767 switch (field) {
768 case V4L2_FIELD_ALTERNATE:
769 vv->last_field = V4L2_FIELD_TOP;
770 maxh = maxh / 2;
771 break;
772 case V4L2_FIELD_TOP:
773 case V4L2_FIELD_BOTTOM:
774 vv->last_field = V4L2_FIELD_INTERLACED;
775 maxh = maxh / 2;
776 break;
777 case V4L2_FIELD_INTERLACED:
778 vv->last_field = V4L2_FIELD_INTERLACED;
779 break;
780 default:
781 DEB_D(("no known field mode '%d'.\n", field));
782 return -EINVAL;
783 }
784
785 f->fmt.pix.field = field;
786 if (f->fmt.pix.width > maxw)
787 f->fmt.pix.width = maxw;
788 if (f->fmt.pix.height > maxh)
789 f->fmt.pix.height = maxh;
790
791 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
792
793 if (f->fmt.pix.bytesperline < calc_bpl)
794 f->fmt.pix.bytesperline = calc_bpl;
795
796 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
797 f->fmt.pix.bytesperline = calc_bpl;
798
799 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
800 DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n", f->fmt.pix.width,
801 f->fmt.pix.height, f->fmt.pix.bytesperline, f->fmt.pix.sizeimage));
802
803 return 0;
804}
805
806
807static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
808{
809 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
810 struct saa7146_vv *vv = dev->vv_data;
811 struct v4l2_window *win = &f->fmt.win;
812 enum v4l2_field field;
813 int maxw, maxh;
814
815 DEB_EE(("dev:%p\n", dev));
816
817 if (NULL == vv->ov_fb.base) {
818 DEB_D(("no fb base set.\n"));
819 return -EINVAL;
820 }
821 if (NULL == vv->ov_fmt) {
822 DEB_D(("no fb fmt set.\n"));
823 return -EINVAL;
824 }
825 if (win->w.width < 48 || win->w.height < 32) {
826 DEB_D(("min width/height. (%d,%d)\n", win->w.width, win->w.height));
827 return -EINVAL;
828 }
829 if (win->clipcount > 16) {
830 DEB_D(("clipcount too big.\n"));
831 return -EINVAL;
832 }
833
834 field = win->field;
835 maxw = vv->standard->h_max_out;
836 maxh = vv->standard->v_max_out;
837
838 if (V4L2_FIELD_ANY == field) {
839 field = (win->w.height > maxh / 2)
840 ? V4L2_FIELD_INTERLACED
841 : V4L2_FIELD_TOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300843 switch (field) {
844 case V4L2_FIELD_TOP:
845 case V4L2_FIELD_BOTTOM:
846 case V4L2_FIELD_ALTERNATE:
847 maxh = maxh / 2;
848 break;
849 case V4L2_FIELD_INTERLACED:
850 break;
851 default:
852 DEB_D(("no known field mode '%d'.\n", field));
853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Hans Verkuilb9600742009-01-18 19:59:11 -0300856 win->field = field;
857 if (win->w.width > maxw)
858 win->w.width = maxw;
859 if (win->w.height > maxh)
860 win->w.height = maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Hans Verkuilb9600742009-01-18 19:59:11 -0300862 return 0;
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Hans Verkuilb9600742009-01-18 19:59:11 -0300865static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
866{
867 struct saa7146_fh *fh = __fh;
868 struct saa7146_dev *dev = fh->dev;
869 struct saa7146_vv *vv = dev->vv_data;
870 int err;
871
872 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
873 if (IS_CAPTURE_ACTIVE(fh) != 0) {
874 DEB_EE(("streaming capture is active\n"));
875 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300877 err = vidioc_try_fmt_vid_cap(file, fh, f);
878 if (0 != err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300880 fh->video_fmt = f->fmt.pix;
881 DEB_EE(("set to pixelformat '%4.4s'\n", (char *)&fh->video_fmt.pixelformat));
882 return 0;
883}
884
885static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
886{
887 struct saa7146_fh *fh = __fh;
888 struct saa7146_dev *dev = fh->dev;
889 struct saa7146_vv *vv = dev->vv_data;
890 int err;
891
892 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh));
893 err = vidioc_try_fmt_vid_overlay(file, fh, f);
894 if (0 != err)
895 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300896 fh->ov.win = f->fmt.win;
897 fh->ov.nclips = f->fmt.win.clipcount;
898 if (fh->ov.nclips > 16)
899 fh->ov.nclips = 16;
900 if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
901 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300902 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300904
905 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
906 fh->ov.fh = fh;
907
Hans Verkuilb9600742009-01-18 19:59:11 -0300908 /* check if our current overlay is active */
909 if (IS_OVERLAY_ACTIVE(fh) != 0) {
910 saa7146_stop_preview(fh);
911 saa7146_start_preview(fh);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800912 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300913 return 0;
914}
915
916static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
917{
918 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
919 struct saa7146_vv *vv = dev->vv_data;
920
921 *norm = vv->standard->id;
922 return 0;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
926 PAL / NTSC / SECAM. if your hardware does not (or does more)
927 -- override this function in your extension */
Hans Verkuilb9600742009-01-18 19:59:11 -0300928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 case VIDIOC_ENUMSTD:
930 {
931 struct v4l2_standard *e = arg;
932 if (e->index < 0 )
933 return -EINVAL;
934 if( e->index < dev->ext_vv_data->num_stds ) {
935 DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
936 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
937 return 0;
938 }
939 return -EINVAL;
940 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Hans Verkuilb9600742009-01-18 19:59:11 -0300943static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
944{
945 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
946 struct saa7146_vv *vv = dev->vv_data;
947 int found = 0;
948 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Hans Verkuilb9600742009-01-18 19:59:11 -0300950 DEB_EE(("VIDIOC_S_STD\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Hans Verkuilb9600742009-01-18 19:59:11 -0300952 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
953 DEB_D(("cannot change video standard while streaming capture is active\n"));
954 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Hans Verkuilb9600742009-01-18 19:59:11 -0300957 if ((vv->video_status & STATUS_OVERLAY) != 0) {
958 vv->ov_suspend = vv->video_fh;
959 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (0 != err) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300961 DEB_D(("suspending video failed. aborting\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300963 }
964 }
Brandon Philips49ee7182007-10-05 16:26:27 -0300965
Hans Verkuilb9600742009-01-18 19:59:11 -0300966 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
967 if (*id & dev->ext_vv_data->stds[i].id)
968 break;
969 if (i != dev->ext_vv_data->num_stds) {
970 vv->standard = &dev->ext_vv_data->stds[i];
971 if (NULL != dev->ext_vv_data->std_callback)
972 dev->ext_vv_data->std_callback(dev, vv->standard);
973 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300975
Hans Verkuilb9600742009-01-18 19:59:11 -0300976 if (vv->ov_suspend != NULL) {
977 saa7146_start_preview(vv->ov_suspend);
978 vv->ov_suspend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300980
981 if (!found) {
982 DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
983 return -EINVAL;
984 }
985
986 DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988}
989
Hans Verkuilb9600742009-01-18 19:59:11 -0300990static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
991{
992 int err;
993
994 DEB_D(("VIDIOC_OVERLAY on:%d\n", on));
995 if (on)
996 err = saa7146_start_preview(fh);
997 else
998 err = saa7146_stop_preview(fh);
999 return err;
1000}
1001
1002static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
1003{
1004 struct saa7146_fh *fh = __fh;
1005
1006 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1007 return videobuf_reqbufs(&fh->video_q, b);
1008 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1009 return videobuf_reqbufs(&fh->vbi_q, b);
1010 return -EINVAL;
1011}
1012
1013static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1014{
1015 struct saa7146_fh *fh = __fh;
1016
1017 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1018 return videobuf_querybuf(&fh->video_q, buf);
1019 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1020 return videobuf_querybuf(&fh->vbi_q, buf);
1021 return -EINVAL;
1022}
1023
1024static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1025{
1026 struct saa7146_fh *fh = __fh;
1027
1028 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1029 return videobuf_qbuf(&fh->video_q, buf);
1030 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1031 return videobuf_qbuf(&fh->vbi_q, buf);
1032 return -EINVAL;
1033}
1034
1035static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
1036{
1037 struct saa7146_fh *fh = __fh;
1038
1039 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
1041 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1042 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
1043 return -EINVAL;
1044}
1045
1046static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
1047{
1048 struct saa7146_fh *fh = __fh;
1049 int err;
1050
1051 DEB_D(("VIDIOC_STREAMON, type:%d\n", type));
1052
1053 err = video_begin(fh);
1054 if (err)
1055 return err;
1056 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1057 return videobuf_streamon(&fh->video_q);
1058 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1059 return videobuf_streamon(&fh->vbi_q);
1060 return -EINVAL;
1061}
1062
1063static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
1064{
1065 struct saa7146_fh *fh = __fh;
1066 struct saa7146_dev *dev = fh->dev;
1067 struct saa7146_vv *vv = dev->vv_data;
1068 int err;
1069
1070 DEB_D(("VIDIOC_STREAMOFF, type:%d\n", type));
1071
1072 /* ugly: we need to copy some checks from video_end(),
1073 because videobuf_streamoff() relies on the capture running.
1074 check and fix this */
1075 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
1076 DEB_S(("not capturing.\n"));
1077 return 0;
1078 }
1079
1080 if (vv->video_fh != fh) {
1081 DEB_S(("capturing, but in another open.\n"));
1082 return -EBUSY;
1083 }
1084
1085 err = -EINVAL;
1086 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 err = videobuf_streamoff(&fh->video_q);
1088 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
1089 err = videobuf_streamoff(&fh->vbi_q);
1090 if (0 != err) {
1091 DEB_D(("warning: videobuf_streamoff() failed.\n"));
1092 video_end(fh, file);
1093 } else {
1094 err = video_end(fh, file);
1095 }
1096 return err;
1097}
1098
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001099static int vidioc_g_chip_ident(struct file *file, void *__fh,
1100 struct v4l2_dbg_chip_ident *chip)
1101{
1102 struct saa7146_fh *fh = __fh;
1103 struct saa7146_dev *dev = fh->dev;
1104
1105 chip->ident = V4L2_IDENT_NONE;
1106 chip->revision = 0;
Hans Verkuileae4d692009-02-07 20:15:22 -03001107 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001108 chip->ident = V4L2_IDENT_SAA7146;
1109 return 0;
1110 }
1111 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1112 core, g_chip_ident, chip);
1113}
1114
Hans Verkuilb9600742009-01-18 19:59:11 -03001115const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
1116 .vidioc_querycap = vidioc_querycap,
1117 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Trent Piepho717167e2009-03-04 01:21:03 -03001118 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
Hans Verkuilb9600742009-01-18 19:59:11 -03001119 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1120 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1121 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1122 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1123 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1124 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1125 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001126 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuilb9600742009-01-18 19:59:11 -03001127
1128 .vidioc_overlay = vidioc_overlay,
1129 .vidioc_g_fbuf = vidioc_g_fbuf,
1130 .vidioc_s_fbuf = vidioc_s_fbuf,
1131 .vidioc_reqbufs = vidioc_reqbufs,
1132 .vidioc_querybuf = vidioc_querybuf,
1133 .vidioc_qbuf = vidioc_qbuf,
1134 .vidioc_dqbuf = vidioc_dqbuf,
1135 .vidioc_g_std = vidioc_g_std,
1136 .vidioc_s_std = vidioc_s_std,
1137 .vidioc_queryctrl = vidioc_queryctrl,
1138 .vidioc_g_ctrl = vidioc_g_ctrl,
1139 .vidioc_s_ctrl = vidioc_s_ctrl,
1140 .vidioc_streamon = vidioc_streamon,
1141 .vidioc_streamoff = vidioc_streamoff,
1142 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuilb9600742009-01-18 19:59:11 -03001143};
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*********************************************************************************/
1146/* buffer handling functions */
1147
1148static int buffer_activate (struct saa7146_dev *dev,
1149 struct saa7146_buf *buf,
1150 struct saa7146_buf *next)
1151{
1152 struct saa7146_vv *vv = dev->vv_data;
1153
Brandon Philips0fc06862007-11-06 20:02:36 -03001154 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 saa7146_set_capture(dev,buf,next);
1156
1157 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1158 return 0;
1159}
1160
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001161static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1162{
1163 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1164 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1165 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static int buffer_prepare(struct videobuf_queue *q,
1169 struct videobuf_buffer *vb, enum v4l2_field field)
1170{
1171 struct file *file = q->priv_data;
1172 struct saa7146_fh *fh = file->private_data;
1173 struct saa7146_dev *dev = fh->dev;
1174 struct saa7146_vv *vv = dev->vv_data;
1175 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1176 int size,err = 0;
1177
1178 DEB_CAP(("vbuf:%p\n",vb));
1179
1180 /* sanity checks */
1181 if (fh->video_fmt.width < 48 ||
1182 fh->video_fmt.height < 32 ||
1183 fh->video_fmt.width > vv->standard->h_max_out ||
1184 fh->video_fmt.height > vv->standard->v_max_out) {
1185 DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
1186 return -EINVAL;
1187 }
1188
1189 size = fh->video_fmt.sizeimage;
1190 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
1191 DEB_D(("size mismatch.\n"));
1192 return -EINVAL;
1193 }
1194
1195 DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1196 fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
1197 if (buf->vb.width != fh->video_fmt.width ||
1198 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1199 buf->vb.height != fh->video_fmt.height ||
1200 buf->vb.size != size ||
1201 buf->vb.field != field ||
1202 buf->vb.field != fh->video_fmt.field ||
1203 buf->fmt != &fh->video_fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001204 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
Brandon Philips0fc06862007-11-06 20:02:36 -03001207 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct saa7146_format *sfmt;
1209
1210 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1211 buf->vb.width = fh->video_fmt.width;
1212 buf->vb.height = fh->video_fmt.height;
1213 buf->vb.size = size;
1214 buf->vb.field = field;
1215 buf->fmt = &fh->video_fmt;
1216 buf->vb.field = fh->video_fmt.field;
1217
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001218 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001220 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if( 0 != IS_PLANAR(sfmt->trans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1223 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1224 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1225 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1227 }
1228
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001229 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (err)
1231 goto oops;
1232 err = saa7146_pgtable_build(dev,buf);
1233 if (err)
1234 goto oops;
1235 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001236 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 buf->activate = buffer_activate;
1238
1239 return 0;
1240
1241 oops:
1242 DEB_D(("error out.\n"));
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001243 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 return err;
1246}
1247
1248static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1249{
1250 struct file *file = q->priv_data;
1251 struct saa7146_fh *fh = file->private_data;
1252
1253 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1254 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1255
1256 *size = fh->video_fmt.sizeimage;
1257
1258 /* check if we exceed the "max_memory" parameter */
1259 if( (*count * *size) > (max_memory*1048576) ) {
1260 *count = (max_memory*1048576) / *size;
1261 }
1262
1263 DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
1264
1265 return 0;
1266}
1267
1268static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1269{
1270 struct file *file = q->priv_data;
1271 struct saa7146_fh *fh = file->private_data;
1272 struct saa7146_dev *dev = fh->dev;
1273 struct saa7146_vv *vv = dev->vv_data;
1274 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1275
1276 DEB_CAP(("vbuf:%p\n",vb));
1277 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1281{
1282 struct file *file = q->priv_data;
1283 struct saa7146_fh *fh = file->private_data;
1284 struct saa7146_dev *dev = fh->dev;
1285 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1286
1287 DEB_CAP(("vbuf:%p\n",vb));
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001288
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001289 saa7146_dma_free(dev,q,buf);
Mauro Carvalho Chehabba9e9f32010-02-01 21:23:46 -02001290
1291 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static struct videobuf_queue_ops video_qops = {
1295 .buf_setup = buffer_setup,
1296 .buf_prepare = buffer_prepare,
1297 .buf_queue = buffer_queue,
1298 .buf_release = buffer_release,
1299};
1300
1301/********************************************************************************/
1302/* file operations */
1303
1304static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1305{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001306 INIT_LIST_HEAD(&vv->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 init_timer(&vv->video_q.timeout);
1309 vv->video_q.timeout.function = saa7146_buffer_timeout;
1310 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1311 vv->video_q.dev = dev;
1312
1313 /* set some default values */
1314 vv->standard = &dev->ext_vv_data->stds[0];
1315
1316 /* FIXME: what's this? */
1317 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1318 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1319}
1320
1321
1322static int video_open(struct saa7146_dev *dev, struct file *file)
1323{
Joe Perchesabf84382010-07-12 17:50:03 -03001324 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct saa7146_format *sfmt;
1326
1327 fh->video_fmt.width = 384;
1328 fh->video_fmt.height = 288;
1329 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1330 fh->video_fmt.bytesperline = 0;
1331 fh->video_fmt.field = V4L2_FIELD_ANY;
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001332 sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1334
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001335 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1336 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1338 V4L2_FIELD_INTERLACED,
1339 sizeof(struct saa7146_buf),
Hans Verkuil9af39712010-12-18 09:20:59 -03001340 file, &dev->v4l2_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343}
1344
1345
1346static void video_close(struct saa7146_dev *dev, struct file *file)
1347{
Joe Perchesabf84382010-07-12 17:50:03 -03001348 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct saa7146_vv *vv = dev->vv_data;
Hartmut Birr2970c492007-04-22 06:57:26 -03001350 struct videobuf_queue *q = &fh->video_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int err;
1352
1353 if (IS_CAPTURE_ACTIVE(fh) != 0) {
1354 err = video_end(fh, file);
1355 } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
1356 err = saa7146_stop_preview(fh);
1357 }
1358
Brandon Philips19bc5132007-11-13 20:05:38 -03001359 videobuf_stop(q);
Hartmut Birr2970c492007-04-22 06:57:26 -03001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* hmm, why is this function declared void? */
1362 /* return err */
1363}
1364
1365
1366static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1367{
1368 struct saa7146_vv *vv = dev->vv_data;
1369 struct saa7146_dmaqueue *q = &vv->video_q;
1370
1371 spin_lock(&dev->slock);
1372 DEB_CAP(("called.\n"));
1373
1374 /* only finish the buffer if we have one... */
1375 if( NULL != q->curr ) {
Brandon Philips0fc06862007-11-06 20:02:36 -03001376 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 saa7146_buffer_next(dev,q,0);
1379
1380 spin_unlock(&dev->slock);
1381}
1382
1383static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1384{
1385 struct saa7146_fh *fh = file->private_data;
1386 struct saa7146_dev *dev = fh->dev;
1387 struct saa7146_vv *vv = dev->vv_data;
1388 ssize_t ret = 0;
1389
1390 DEB_EE(("called.\n"));
1391
1392 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1393 /* fixme: should we allow read() captures while streaming capture? */
1394 if (vv->video_fh == fh) {
1395 DEB_S(("already capturing.\n"));
1396 return -EBUSY;
1397 }
1398 DEB_S(("already capturing in another open.\n"));
1399 return -EBUSY;
1400 }
1401
1402 ret = video_begin(fh);
1403 if( 0 != ret) {
1404 goto out;
1405 }
1406
1407 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1408 file->f_flags & O_NONBLOCK);
1409 if (ret != 0) {
1410 video_end(fh, file);
1411 } else {
1412 ret = video_end(fh, file);
1413 }
1414out:
1415 /* restart overlay if it was active before */
1416 if (vv->ov_suspend != NULL) {
1417 saa7146_start_preview(vv->ov_suspend);
1418 vv->ov_suspend = NULL;
1419 }
1420
1421 return ret;
1422}
1423
1424struct saa7146_use_ops saa7146_video_uops = {
1425 .init = video_init,
1426 .open = video_open,
1427 .release = video_close,
1428 .irq_done = video_irq_done,
1429 .read = video_read,
1430};