blob: 8818e661a42f136b230df33e9b5aef7f4ec4921c [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>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04005#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7static int max_memory = 32;
8
9module_param(max_memory, int, 0644);
10MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
11
12#define IS_CAPTURE_ACTIVE(fh) \
13 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
14
15#define IS_OVERLAY_ACTIVE(fh) \
16 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
17
18/* format descriptions for capture and preview */
19static struct saa7146_format formats[] = {
20 {
21 .name = "RGB-8 (3-3-2)",
22 .pixelformat = V4L2_PIX_FMT_RGB332,
23 .trans = RGB08_COMPOSED,
24 .depth = 8,
25 .flags = 0,
26 }, {
27 .name = "RGB-16 (5/B-6/G-5/R)",
28 .pixelformat = V4L2_PIX_FMT_RGB565,
29 .trans = RGB16_COMPOSED,
30 .depth = 16,
31 .flags = 0,
32 }, {
33 .name = "RGB-24 (B-G-R)",
34 .pixelformat = V4L2_PIX_FMT_BGR24,
35 .trans = RGB24_COMPOSED,
36 .depth = 24,
37 .flags = 0,
38 }, {
39 .name = "RGB-32 (B-G-R)",
40 .pixelformat = V4L2_PIX_FMT_BGR32,
41 .trans = RGB32_COMPOSED,
42 .depth = 32,
43 .flags = 0,
44 }, {
45 .name = "RGB-32 (R-G-B)",
46 .pixelformat = V4L2_PIX_FMT_RGB32,
47 .trans = RGB32_COMPOSED,
48 .depth = 32,
49 .flags = 0,
50 .swap = 0x2,
51 }, {
52 .name = "Greyscale-8",
53 .pixelformat = V4L2_PIX_FMT_GREY,
54 .trans = Y8,
55 .depth = 8,
56 .flags = 0,
57 }, {
58 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
59 .pixelformat = V4L2_PIX_FMT_YUV422P,
60 .trans = YUV422_DECOMPOSED,
61 .depth = 16,
62 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
63 }, {
64 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
65 .pixelformat = V4L2_PIX_FMT_YVU420,
66 .trans = YUV420_DECOMPOSED,
67 .depth = 12,
68 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
69 }, {
70 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
71 .pixelformat = V4L2_PIX_FMT_YUV420,
72 .trans = YUV420_DECOMPOSED,
73 .depth = 12,
74 .flags = FORMAT_IS_PLANAR,
75 }, {
76 .name = "YUV 4:2:2 (U-Y-V-Y)",
77 .pixelformat = V4L2_PIX_FMT_UYVY,
78 .trans = YUV422_COMPOSED,
79 .depth = 16,
80 .flags = 0,
81 }
82};
83
84/* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
85 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
86 (like V4L2_PIX_FMT_YUYV) ... 8-( */
87
88static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
89
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -020090struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int i, j = NUM_FORMATS;
93
94 for (i = 0; i < j; i++) {
95 if (formats[i].pixelformat == fourcc) {
96 return formats+i;
97 }
98 }
99
Joe Perches44d0b802011-08-21 19:56:44 -0300100 DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return NULL;
102}
103
Hans Verkuilb9600742009-01-18 19:59:11 -0300104static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106int saa7146_start_preview(struct saa7146_fh *fh)
107{
108 struct saa7146_dev *dev = fh->dev;
109 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuilb9600742009-01-18 19:59:11 -0300110 struct v4l2_format fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int ret = 0, err = 0;
112
Joe Perches44d0b802011-08-21 19:56:44 -0300113 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* check if we have overlay informations */
116 if( NULL == fh->ov.fh ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300117 DEB_D("no overlay data available. try S_FMT first.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -EAGAIN;
119 }
120
121 /* check if streaming capture is running */
122 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300123 DEB_D("streaming capture is active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EBUSY;
125 }
126
127 /* check if overlay is running */
128 if (IS_OVERLAY_ACTIVE(fh) != 0) {
129 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300130 DEB_D("overlay is already active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132 }
Joe Perches44d0b802011-08-21 19:56:44 -0300133 DEB_D("overlay is already active in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EBUSY;
135 }
136
137 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
Joe Perches44d0b802011-08-21 19:56:44 -0300138 DEB_D("cannot get necessary overlay resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return -EBUSY;
140 }
141
Hans Verkuilb9600742009-01-18 19:59:11 -0300142 fmt.fmt.win = fh->ov.win;
143 err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (0 != err) {
145 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
146 return -EBUSY;
147 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300148 fh->ov.win = fmt.fmt.win;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 vv->ov_data = &fh->ov;
150
Joe Perches44d0b802011-08-21 19:56:44 -0300151 DEB_D("%dx%d+%d+%d %s field=%s\n",
152 fh->ov.win.w.width, fh->ov.win.w.height,
153 fh->ov.win.w.left, fh->ov.win.w.top,
154 vv->ov_fmt->name, v4l2_field_names[fh->ov.win.field]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (0 != (ret = saa7146_enable_overlay(fh))) {
Joe Perches44d0b802011-08-21 19:56:44 -0300157 DEB_D("enabling overlay failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
159 return ret;
160 }
161
162 vv->video_status = STATUS_OVERLAY;
163 vv->video_fh = fh;
164
165 return 0;
166}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300167EXPORT_SYMBOL_GPL(saa7146_start_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169int saa7146_stop_preview(struct saa7146_fh *fh)
170{
171 struct saa7146_dev *dev = fh->dev;
172 struct saa7146_vv *vv = dev->vv_data;
173
Joe Perches44d0b802011-08-21 19:56:44 -0300174 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* check if streaming capture is running */
177 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300178 DEB_D("streaming capture is active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EBUSY;
180 }
181
182 /* check if overlay is running at all */
183 if ((vv->video_status & STATUS_OVERLAY) == 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300184 DEB_D("no active overlay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186 }
187
188 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300189 DEB_D("overlay is active, but in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EBUSY;
191 }
192
193 vv->video_status = 0;
194 vv->video_fh = NULL;
195
196 saa7146_disable_overlay(fh);
197
198 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
199
200 return 0;
201}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300202EXPORT_SYMBOL_GPL(saa7146_stop_preview);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/********************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* common pagetable functions */
206
207static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
208{
209 struct pci_dev *pci = dev->pci;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300210 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
211 struct scatterlist *list = dma->sglist;
212 int length = dma->sglen;
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200213 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Joe Perches44d0b802011-08-21 19:56:44 -0300215 DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if( 0 != IS_PLANAR(sfmt->trans)) {
218 struct saa7146_pgtable *pt1 = &buf->pt[0];
219 struct saa7146_pgtable *pt2 = &buf->pt[1];
220 struct saa7146_pgtable *pt3 = &buf->pt[2];
Al Viroa36ef6b2008-06-22 14:19:19 -0300221 __le32 *ptr1, *ptr2, *ptr3;
222 __le32 fill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 int size = buf->fmt->width*buf->fmt->height;
225 int i,p,m1,m2,m3,o1,o2;
226
227 switch( sfmt->depth ) {
228 case 12: {
229 /* create some offsets inside the page table */
230 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
231 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
232 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
233 o1 = size%PAGE_SIZE;
234 o2 = (size+(size/4))%PAGE_SIZE;
Joe Perches44d0b802011-08-21 19:56:44 -0300235 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
236 size, m1, m2, m3, o1, o2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 }
239 case 16: {
240 /* create some offsets inside the page table */
241 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
242 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
243 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
244 o1 = size%PAGE_SIZE;
245 o2 = (size+(size/2))%PAGE_SIZE;
Joe Perches44d0b802011-08-21 19:56:44 -0300246 DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
247 size, m1, m2, m3, o1, o2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 }
250 default: {
251 return -1;
252 }
253 }
254
255 ptr1 = pt1->cpu;
256 ptr2 = pt2->cpu;
257 ptr3 = pt3->cpu;
258
259 /* walk all pages, copy all page addresses to ptr1 */
260 for (i = 0; i < length; i++, list++) {
261 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
262 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
263 }
264 }
265/*
266 ptr1 = pt1->cpu;
267 for(j=0;j<40;j++) {
268 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
269 }
270*/
271
272 /* if we have a user buffer, the first page may not be
273 aligned to a page boundary. */
Hans Verkuil429e9082008-07-27 14:08:54 -0300274 pt1->offset = dma->sglist->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 pt2->offset = pt1->offset+o1;
276 pt3->offset = pt1->offset+o2;
277
278 /* create video-dma2 page table */
279 ptr1 = pt1->cpu;
280 for(i = m1; i <= m2 ; i++, ptr2++) {
281 *ptr2 = ptr1[i];
282 }
283 fill = *(ptr2-1);
284 for(;i<1024;i++,ptr2++) {
285 *ptr2 = fill;
286 }
287 /* create video-dma3 page table */
288 ptr1 = pt1->cpu;
289 for(i = m2; i <= m3; i++,ptr3++) {
290 *ptr3 = ptr1[i];
291 }
292 fill = *(ptr3-1);
293 for(;i<1024;i++,ptr3++) {
294 *ptr3 = fill;
295 }
296 /* finally: finish up video-dma1 page table */
297 ptr1 = pt1->cpu+m1;
298 fill = pt1->cpu[m1];
299 for(i=m1;i<1024;i++,ptr1++) {
300 *ptr1 = fill;
301 }
302/*
303 ptr1 = pt1->cpu;
304 ptr2 = pt2->cpu;
305 ptr3 = pt3->cpu;
306 for(j=0;j<40;j++) {
307 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
308 }
309 for(j=0;j<40;j++) {
310 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
311 }
312 for(j=0;j<40;j++) {
313 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
314 }
315*/
316 } else {
317 struct saa7146_pgtable *pt = &buf->pt[0];
318 return saa7146_pgtable_build_single(pci, pt, list, length);
319 }
320
321 return 0;
322}
323
324
325/********************************************************************************/
326/* file operations */
327
328static int video_begin(struct saa7146_fh *fh)
329{
330 struct saa7146_dev *dev = fh->dev;
331 struct saa7146_vv *vv = dev->vv_data;
332 struct saa7146_format *fmt = NULL;
333 unsigned int resource;
334 int ret = 0, err = 0;
335
Joe Perches44d0b802011-08-21 19:56:44 -0300336 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if ((vv->video_status & STATUS_CAPTURE) != 0) {
339 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300340 DEB_S("already capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342 }
Joe Perches44d0b802011-08-21 19:56:44 -0300343 DEB_S("already capturing in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EBUSY;
345 }
346
347 if ((vv->video_status & STATUS_OVERLAY) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300348 DEB_S("warning: suspending overlay video for streaming capture\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 vv->ov_suspend = vv->video_fh;
350 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
351 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300352 DEB_D("suspending video failed. aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354 }
355 }
356
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200357 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* we need to have a valid format set here */
359 BUG_ON(NULL == fmt);
360
361 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
362 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
363 } else {
364 resource = RESOURCE_DMA1_HPS;
365 }
366
367 ret = saa7146_res_get(fh, resource);
368 if (0 == ret) {
Joe Perches44d0b802011-08-21 19:56:44 -0300369 DEB_S("cannot get capture resource %d\n", resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (vv->ov_suspend != NULL) {
371 saa7146_start_preview(vv->ov_suspend);
372 vv->ov_suspend = NULL;
373 }
374 return -EBUSY;
375 }
376
377 /* clear out beginning of streaming bit (rps register 0)*/
378 saa7146_write(dev, MC2, MASK_27 );
379
380 /* enable rps0 irqs */
381 SAA7146_IER_ENABLE(dev, MASK_27);
382
383 vv->video_fh = fh;
384 vv->video_status = STATUS_CAPTURE;
385
386 return 0;
387}
388
389static int video_end(struct saa7146_fh *fh, struct file *file)
390{
391 struct saa7146_dev *dev = fh->dev;
392 struct saa7146_vv *vv = dev->vv_data;
393 struct saa7146_format *fmt = NULL;
394 unsigned long flags;
395 unsigned int resource;
396 u32 dmas = 0;
Joe Perches44d0b802011-08-21 19:56:44 -0300397 DEB_EE("dev:%p, fh:%p\n", dev, fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300400 DEB_S("not capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402 }
403
404 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300405 DEB_S("capturing, but in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EBUSY;
407 }
408
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200409 fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* we need to have a valid format set here */
411 BUG_ON(NULL == fmt);
412
413 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
414 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
415 dmas = MASK_22 | MASK_21 | MASK_20;
416 } else {
417 resource = RESOURCE_DMA1_HPS;
418 dmas = MASK_22;
419 }
420 spin_lock_irqsave(&dev->slock,flags);
421
422 /* disable rps0 */
423 saa7146_write(dev, MC1, MASK_28);
424
425 /* disable rps0 irqs */
426 SAA7146_IER_DISABLE(dev, MASK_27);
427
428 /* shut down all used video dma transfers */
429 saa7146_write(dev, MC1, dmas);
430
431 spin_unlock_irqrestore(&dev->slock, flags);
432
433 vv->video_fh = NULL;
434 vv->video_status = 0;
435
436 saa7146_res_free(fh, resource);
437
438 if (vv->ov_suspend != NULL) {
439 saa7146_start_preview(vv->ov_suspend);
440 vv->ov_suspend = NULL;
441 }
442
443 return 0;
444}
445
Hans Verkuilb9600742009-01-18 19:59:11 -0300446static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Hans Verkuilb9600742009-01-18 19:59:11 -0300448 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
449
450 strcpy((char *)cap->driver, "saa7146 v4l2");
451 strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
452 sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
453 cap->version = SAA7146_VERSION_CODE;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300454 cap->device_caps =
Hans Verkuilb9600742009-01-18 19:59:11 -0300455 V4L2_CAP_VIDEO_CAPTURE |
456 V4L2_CAP_VIDEO_OVERLAY |
457 V4L2_CAP_READWRITE |
458 V4L2_CAP_STREAMING;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300459 cap->device_caps |= dev->ext_vv_data->capabilities;
460 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Hans Verkuilb9600742009-01-18 19:59:11 -0300461 return 0;
462}
463
464static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
465{
466 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct saa7146_vv *vv = dev->vv_data;
468
Hans Verkuilb9600742009-01-18 19:59:11 -0300469 *fb = vv->ov_fb;
470 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
471 return 0;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Hans Verkuilb9600742009-01-18 19:59:11 -0300474static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
475{
476 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
477 struct saa7146_vv *vv = dev->vv_data;
478 struct saa7146_format *fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Joe Perches44d0b802011-08-21 19:56:44 -0300480 DEB_EE("VIDIOC_S_FBUF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Hans Verkuilb9600742009-01-18 19:59:11 -0300482 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
483 return -EPERM;
484
485 /* check args */
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200486 fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300487 if (NULL == fmt)
488 return -EINVAL;
489
490 /* planar formats are not allowed for overlay video, clipping and video dma would clash */
491 if (fmt->flags & FORMAT_IS_PLANAR)
Joe Perches44d0b802011-08-21 19:56:44 -0300492 DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
493 (char *)&fmt->pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300494
495 /* check if overlay is running */
496 if (IS_OVERLAY_ACTIVE(fh) != 0) {
497 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300498 DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300499 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502
Hans Verkuilb9600742009-01-18 19:59:11 -0300503 /* ok, accept it */
504 vv->ov_fb = *fb;
505 vv->ov_fmt = fmt;
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300506
507 if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
508 vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
Joe Perches44d0b802011-08-21 19:56:44 -0300509 DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
Michael Hunold84a1d9c2010-03-13 11:45:46 -0300510 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300511 return 0;
512}
513
514static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
515{
516 if (f->index >= NUM_FORMATS)
517 return -EINVAL;
518 strlcpy((char *)f->description, formats[f->index].name,
519 sizeof(f->description));
520 f->pixelformat = formats[f->index].pixelformat;
521 return 0;
522}
523
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300524int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilb9600742009-01-18 19:59:11 -0300525{
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300526 struct saa7146_dev *dev = container_of(ctrl->handler,
527 struct saa7146_dev, ctrl_handler);
Hans Verkuilb9600742009-01-18 19:59:11 -0300528 struct saa7146_vv *vv = dev->vv_data;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300529 u32 val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300530
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300531 switch (ctrl->id) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300532 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300533 val = saa7146_read(dev, BCS_CTRL);
534 val &= 0x00ffffff;
535 val |= (ctrl->val << 24);
536 saa7146_write(dev, BCS_CTRL, val);
537 saa7146_write(dev, MC2, MASK_22 | MASK_06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300539
Hans Verkuilb9600742009-01-18 19:59:11 -0300540 case V4L2_CID_CONTRAST:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300541 val = saa7146_read(dev, BCS_CTRL);
542 val &= 0xff00ffff;
543 val |= (ctrl->val << 16);
544 saa7146_write(dev, BCS_CTRL, val);
545 saa7146_write(dev, MC2, MASK_22 | MASK_06);
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_SATURATION:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300549 val = saa7146_read(dev, BCS_CTRL);
550 val &= 0xffffff00;
551 val |= (ctrl->val << 0);
552 saa7146_write(dev, BCS_CTRL, val);
Hans Verkuilb9600742009-01-18 19:59:11 -0300553 saa7146_write(dev, MC2, MASK_22 | MASK_06);
554 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300555
Hans Verkuilb9600742009-01-18 19:59:11 -0300556 case V4L2_CID_HFLIP:
557 /* fixme: we can support changing VFLIP and HFLIP here... */
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300558 if ((vv->video_status & STATUS_CAPTURE))
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300559 return -EBUSY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300560 vv->hflip = ctrl->val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300561 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300562
Hans Verkuilb9600742009-01-18 19:59:11 -0300563 case V4L2_CID_VFLIP:
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300564 if ((vv->video_status & STATUS_CAPTURE))
Hans Verkuil5a5b9642009-02-07 11:25:05 -0300565 return -EBUSY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300566 vv->vflip = ctrl->val;
Hans Verkuilb9600742009-01-18 19:59:11 -0300567 break;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300568
Hans Verkuilb9600742009-01-18 19:59:11 -0300569 default:
Hans Verkuilb9600742009-01-18 19:59:11 -0300570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300573 if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
574 struct saa7146_fh *fh = vv->video_fh;
575
Hans Verkuilb9600742009-01-18 19:59:11 -0300576 saa7146_stop_preview(fh);
577 saa7146_start_preview(fh);
578 }
579 return 0;
580}
581
582static int vidioc_g_parm(struct file *file, void *fh,
583 struct v4l2_streamparm *parm)
584{
Trent Piepho717167e2009-03-04 01:21:03 -0300585 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
586 struct saa7146_vv *vv = dev->vv_data;
587
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300588 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
589 return -EINVAL;
Hans Verkuilb9600742009-01-18 19:59:11 -0300590 parm->parm.capture.readbuffers = 1;
Trent Piepho717167e2009-03-04 01:21:03 -0300591 v4l2_video_std_frame_period(vv->standard->id,
592 &parm->parm.capture.timeperframe);
Hans Verkuilb9600742009-01-18 19:59:11 -0300593 return 0;
594}
595
596static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
597{
598 f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
599 return 0;
600}
601
602static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
603{
604 f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
605 return 0;
606}
607
608static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
609{
610 f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
611 return 0;
612}
613
614static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
615{
616 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
617 struct saa7146_vv *vv = dev->vv_data;
618 struct saa7146_format *fmt;
619 enum v4l2_field field;
620 int maxw, maxh;
621 int calc_bpl;
622
Joe Perches44d0b802011-08-21 19:56:44 -0300623 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300624
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -0200625 fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300626 if (NULL == fmt)
627 return -EINVAL;
628
629 field = f->fmt.pix.field;
630 maxw = vv->standard->h_max_out;
631 maxh = vv->standard->v_max_out;
632
633 if (V4L2_FIELD_ANY == field) {
634 field = (f->fmt.pix.height > maxh / 2)
635 ? V4L2_FIELD_INTERLACED
636 : V4L2_FIELD_BOTTOM;
637 }
638 switch (field) {
639 case V4L2_FIELD_ALTERNATE:
640 vv->last_field = V4L2_FIELD_TOP;
641 maxh = maxh / 2;
642 break;
643 case V4L2_FIELD_TOP:
644 case V4L2_FIELD_BOTTOM:
645 vv->last_field = V4L2_FIELD_INTERLACED;
646 maxh = maxh / 2;
647 break;
648 case V4L2_FIELD_INTERLACED:
649 vv->last_field = V4L2_FIELD_INTERLACED;
650 break;
651 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300652 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300653 return -EINVAL;
654 }
655
656 f->fmt.pix.field = field;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300657 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuilb9600742009-01-18 19:59:11 -0300658 if (f->fmt.pix.width > maxw)
659 f->fmt.pix.width = maxw;
660 if (f->fmt.pix.height > maxh)
661 f->fmt.pix.height = maxh;
662
663 calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
664
665 if (f->fmt.pix.bytesperline < calc_bpl)
666 f->fmt.pix.bytesperline = calc_bpl;
667
668 if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
669 f->fmt.pix.bytesperline = calc_bpl;
670
671 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
Joe Perches44d0b802011-08-21 19:56:44 -0300672 DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
673 f->fmt.pix.width, f->fmt.pix.height,
674 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
Hans Verkuilb9600742009-01-18 19:59:11 -0300675
676 return 0;
677}
678
679
680static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
681{
682 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
683 struct saa7146_vv *vv = dev->vv_data;
684 struct v4l2_window *win = &f->fmt.win;
685 enum v4l2_field field;
686 int maxw, maxh;
687
Joe Perches44d0b802011-08-21 19:56:44 -0300688 DEB_EE("dev:%p\n", dev);
Hans Verkuilb9600742009-01-18 19:59:11 -0300689
690 if (NULL == vv->ov_fb.base) {
Joe Perches44d0b802011-08-21 19:56:44 -0300691 DEB_D("no fb base set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300692 return -EINVAL;
693 }
694 if (NULL == vv->ov_fmt) {
Joe Perches44d0b802011-08-21 19:56:44 -0300695 DEB_D("no fb fmt set\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300696 return -EINVAL;
697 }
698 if (win->w.width < 48 || win->w.height < 32) {
Joe Perches44d0b802011-08-21 19:56:44 -0300699 DEB_D("min width/height. (%d,%d)\n",
700 win->w.width, win->w.height);
Hans Verkuilb9600742009-01-18 19:59:11 -0300701 return -EINVAL;
702 }
703 if (win->clipcount > 16) {
Joe Perches44d0b802011-08-21 19:56:44 -0300704 DEB_D("clipcount too big\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300705 return -EINVAL;
706 }
707
708 field = win->field;
709 maxw = vv->standard->h_max_out;
710 maxh = vv->standard->v_max_out;
711
712 if (V4L2_FIELD_ANY == field) {
713 field = (win->w.height > maxh / 2)
714 ? V4L2_FIELD_INTERLACED
715 : V4L2_FIELD_TOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300717 switch (field) {
718 case V4L2_FIELD_TOP:
719 case V4L2_FIELD_BOTTOM:
720 case V4L2_FIELD_ALTERNATE:
721 maxh = maxh / 2;
722 break;
723 case V4L2_FIELD_INTERLACED:
724 break;
725 default:
Joe Perches44d0b802011-08-21 19:56:44 -0300726 DEB_D("no known field mode '%d'\n", field);
Hans Verkuilb9600742009-01-18 19:59:11 -0300727 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Hans Verkuilb9600742009-01-18 19:59:11 -0300730 win->field = field;
731 if (win->w.width > maxw)
732 win->w.width = maxw;
733 if (win->w.height > maxh)
734 win->w.height = maxh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Hans Verkuilb9600742009-01-18 19:59:11 -0300736 return 0;
737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Hans Verkuilb9600742009-01-18 19:59:11 -0300739static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
740{
741 struct saa7146_fh *fh = __fh;
742 struct saa7146_dev *dev = fh->dev;
743 struct saa7146_vv *vv = dev->vv_data;
744 int err;
745
Joe Perches44d0b802011-08-21 19:56:44 -0300746 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300747 if (IS_CAPTURE_ACTIVE(fh) != 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300748 DEB_EE("streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300749 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300751 err = vidioc_try_fmt_vid_cap(file, fh, f);
752 if (0 != err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300754 fh->video_fmt = f->fmt.pix;
Joe Perches44d0b802011-08-21 19:56:44 -0300755 DEB_EE("set to pixelformat '%4.4s'\n",
756 (char *)&fh->video_fmt.pixelformat);
Hans Verkuilb9600742009-01-18 19:59:11 -0300757 return 0;
758}
759
760static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
761{
762 struct saa7146_fh *fh = __fh;
763 struct saa7146_dev *dev = fh->dev;
764 struct saa7146_vv *vv = dev->vv_data;
765 int err;
766
Joe Perches44d0b802011-08-21 19:56:44 -0300767 DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
Hans Verkuilb9600742009-01-18 19:59:11 -0300768 err = vidioc_try_fmt_vid_overlay(file, fh, f);
769 if (0 != err)
770 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300771 fh->ov.win = f->fmt.win;
772 fh->ov.nclips = f->fmt.win.clipcount;
773 if (fh->ov.nclips > 16)
774 fh->ov.nclips = 16;
775 if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
776 sizeof(struct v4l2_clip) * fh->ov.nclips)) {
Hans Verkuilb9600742009-01-18 19:59:11 -0300777 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300779
780 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
781 fh->ov.fh = fh;
782
Hans Verkuilb9600742009-01-18 19:59:11 -0300783 /* check if our current overlay is active */
784 if (IS_OVERLAY_ACTIVE(fh) != 0) {
785 saa7146_stop_preview(fh);
786 saa7146_start_preview(fh);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800787 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300788 return 0;
789}
790
791static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
792{
793 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
794 struct saa7146_vv *vv = dev->vv_data;
795
796 *norm = vv->standard->id;
797 return 0;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
801 PAL / NTSC / SECAM. if your hardware does not (or does more)
802 -- override this function in your extension */
Hans Verkuilb9600742009-01-18 19:59:11 -0300803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case VIDIOC_ENUMSTD:
805 {
806 struct v4l2_standard *e = arg;
807 if (e->index < 0 )
808 return -EINVAL;
809 if( e->index < dev->ext_vv_data->num_stds ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300810 DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
812 return 0;
813 }
814 return -EINVAL;
815 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Hans Verkuilb9600742009-01-18 19:59:11 -0300818static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
819{
820 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
821 struct saa7146_vv *vv = dev->vv_data;
822 int found = 0;
823 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Joe Perches44d0b802011-08-21 19:56:44 -0300825 DEB_EE("VIDIOC_S_STD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Hans Verkuilb9600742009-01-18 19:59:11 -0300827 if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300828 DEB_D("cannot change video standard while streaming capture is active\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300829 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Hans Verkuilb9600742009-01-18 19:59:11 -0300832 if ((vv->video_status & STATUS_OVERLAY) != 0) {
833 vv->ov_suspend = vv->video_fh;
834 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 -0700835 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300836 DEB_D("suspending video failed. aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300838 }
839 }
Brandon Philips49ee7182007-10-05 16:26:27 -0300840
Hans Verkuilb9600742009-01-18 19:59:11 -0300841 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
842 if (*id & dev->ext_vv_data->stds[i].id)
843 break;
844 if (i != dev->ext_vv_data->num_stds) {
845 vv->standard = &dev->ext_vv_data->stds[i];
846 if (NULL != dev->ext_vv_data->std_callback)
847 dev->ext_vv_data->std_callback(dev, vv->standard);
848 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300850
Hans Verkuilb9600742009-01-18 19:59:11 -0300851 if (vv->ov_suspend != NULL) {
852 saa7146_start_preview(vv->ov_suspend);
853 vv->ov_suspend = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300855
856 if (!found) {
Joe Perches44d0b802011-08-21 19:56:44 -0300857 DEB_EE("VIDIOC_S_STD: standard not found\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300858 return -EINVAL;
859 }
860
Joe Perches44d0b802011-08-21 19:56:44 -0300861 DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863}
864
Hans Verkuilb9600742009-01-18 19:59:11 -0300865static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
866{
867 int err;
868
Joe Perches44d0b802011-08-21 19:56:44 -0300869 DEB_D("VIDIOC_OVERLAY on:%d\n", on);
Hans Verkuilb9600742009-01-18 19:59:11 -0300870 if (on)
871 err = saa7146_start_preview(fh);
872 else
873 err = saa7146_stop_preview(fh);
874 return err;
875}
876
877static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
878{
879 struct saa7146_fh *fh = __fh;
880
881 if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 return videobuf_reqbufs(&fh->video_q, b);
883 if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
884 return videobuf_reqbufs(&fh->vbi_q, b);
885 return -EINVAL;
886}
887
888static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
889{
890 struct saa7146_fh *fh = __fh;
891
892 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
893 return videobuf_querybuf(&fh->video_q, buf);
894 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
895 return videobuf_querybuf(&fh->vbi_q, buf);
896 return -EINVAL;
897}
898
899static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
900{
901 struct saa7146_fh *fh = __fh;
902
903 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 return videobuf_qbuf(&fh->video_q, buf);
905 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
906 return videobuf_qbuf(&fh->vbi_q, buf);
907 return -EINVAL;
908}
909
910static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
911{
912 struct saa7146_fh *fh = __fh;
913
914 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
915 return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
916 if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
917 return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
918 return -EINVAL;
919}
920
921static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
922{
923 struct saa7146_fh *fh = __fh;
924 int err;
925
Joe Perches44d0b802011-08-21 19:56:44 -0300926 DEB_D("VIDIOC_STREAMON, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300927
928 err = video_begin(fh);
929 if (err)
930 return err;
931 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 return videobuf_streamon(&fh->video_q);
933 if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
934 return videobuf_streamon(&fh->vbi_q);
935 return -EINVAL;
936}
937
938static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
939{
940 struct saa7146_fh *fh = __fh;
941 struct saa7146_dev *dev = fh->dev;
942 struct saa7146_vv *vv = dev->vv_data;
943 int err;
944
Joe Perches44d0b802011-08-21 19:56:44 -0300945 DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
Hans Verkuilb9600742009-01-18 19:59:11 -0300946
947 /* ugly: we need to copy some checks from video_end(),
948 because videobuf_streamoff() relies on the capture running.
949 check and fix this */
950 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
Joe Perches44d0b802011-08-21 19:56:44 -0300951 DEB_S("not capturing\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300952 return 0;
953 }
954
955 if (vv->video_fh != fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300956 DEB_S("capturing, but in another open\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300957 return -EBUSY;
958 }
959
960 err = -EINVAL;
961 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
962 err = videobuf_streamoff(&fh->video_q);
963 else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
964 err = videobuf_streamoff(&fh->vbi_q);
965 if (0 != err) {
Joe Perches44d0b802011-08-21 19:56:44 -0300966 DEB_D("warning: videobuf_streamoff() failed\n");
Hans Verkuilb9600742009-01-18 19:59:11 -0300967 video_end(fh, file);
968 } else {
969 err = video_end(fh, file);
970 }
971 return err;
972}
973
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300974static int vidioc_g_chip_ident(struct file *file, void *__fh,
975 struct v4l2_dbg_chip_ident *chip)
976{
977 struct saa7146_fh *fh = __fh;
978 struct saa7146_dev *dev = fh->dev;
979
980 chip->ident = V4L2_IDENT_NONE;
981 chip->revision = 0;
Hans Verkuileae4d692009-02-07 20:15:22 -0300982 if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
Hans Verkuil1b8dac12009-02-07 11:18:05 -0300983 chip->ident = V4L2_IDENT_SAA7146;
984 return 0;
985 }
986 return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
987 core, g_chip_ident, chip);
988}
989
Hans Verkuilb9600742009-01-18 19:59:11 -0300990const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
991 .vidioc_querycap = vidioc_querycap,
992 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Trent Piepho717167e2009-03-04 01:21:03 -0300993 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
Hans Verkuilb9600742009-01-18 19:59:11 -0300994 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
995 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
996 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
997 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
998 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
999 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1000 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil1b8dac12009-02-07 11:18:05 -03001001 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuilb9600742009-01-18 19:59:11 -03001002
1003 .vidioc_overlay = vidioc_overlay,
1004 .vidioc_g_fbuf = vidioc_g_fbuf,
1005 .vidioc_s_fbuf = vidioc_s_fbuf,
1006 .vidioc_reqbufs = vidioc_reqbufs,
1007 .vidioc_querybuf = vidioc_querybuf,
1008 .vidioc_qbuf = vidioc_qbuf,
1009 .vidioc_dqbuf = vidioc_dqbuf,
1010 .vidioc_g_std = vidioc_g_std,
1011 .vidioc_s_std = vidioc_s_std,
Hans Verkuilb9600742009-01-18 19:59:11 -03001012 .vidioc_streamon = vidioc_streamon,
1013 .vidioc_streamoff = vidioc_streamoff,
1014 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuilb9600742009-01-18 19:59:11 -03001015};
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*********************************************************************************/
1018/* buffer handling functions */
1019
1020static int buffer_activate (struct saa7146_dev *dev,
1021 struct saa7146_buf *buf,
1022 struct saa7146_buf *next)
1023{
1024 struct saa7146_vv *vv = dev->vv_data;
1025
Brandon Philips0fc06862007-11-06 20:02:36 -03001026 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 saa7146_set_capture(dev,buf,next);
1028
1029 mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1030 return 0;
1031}
1032
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001033static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
1034{
1035 saa7146_pgtable_free(dev->pci, &buf->pt[0]);
1036 saa7146_pgtable_free(dev->pci, &buf->pt[1]);
1037 saa7146_pgtable_free(dev->pci, &buf->pt[2]);
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int buffer_prepare(struct videobuf_queue *q,
1041 struct videobuf_buffer *vb, enum v4l2_field field)
1042{
1043 struct file *file = q->priv_data;
1044 struct saa7146_fh *fh = file->private_data;
1045 struct saa7146_dev *dev = fh->dev;
1046 struct saa7146_vv *vv = dev->vv_data;
1047 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1048 int size,err = 0;
1049
Joe Perches44d0b802011-08-21 19:56:44 -03001050 DEB_CAP("vbuf:%p\n", vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* sanity checks */
1053 if (fh->video_fmt.width < 48 ||
1054 fh->video_fmt.height < 32 ||
1055 fh->video_fmt.width > vv->standard->h_max_out ||
1056 fh->video_fmt.height > vv->standard->v_max_out) {
Joe Perches44d0b802011-08-21 19:56:44 -03001057 DEB_D("w (%d) / h (%d) out of bounds\n",
1058 fh->video_fmt.width, fh->video_fmt.height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EINVAL;
1060 }
1061
1062 size = fh->video_fmt.sizeimage;
1063 if (0 != buf->vb.baddr && buf->vb.bsize < size) {
Joe Perches44d0b802011-08-21 19:56:44 -03001064 DEB_D("size mismatch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINVAL;
1066 }
1067
Joe Perches44d0b802011-08-21 19:56:44 -03001068 DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
1069 fh->video_fmt.width, fh->video_fmt.height,
1070 size, v4l2_field_names[fh->video_fmt.field]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (buf->vb.width != fh->video_fmt.width ||
1072 buf->vb.bytesperline != fh->video_fmt.bytesperline ||
1073 buf->vb.height != fh->video_fmt.height ||
1074 buf->vb.size != size ||
1075 buf->vb.field != field ||
1076 buf->vb.field != fh->video_fmt.field ||
1077 buf->fmt != &fh->video_fmt) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001078 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Brandon Philips0fc06862007-11-06 20:02:36 -03001081 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct saa7146_format *sfmt;
1083
1084 buf->vb.bytesperline = fh->video_fmt.bytesperline;
1085 buf->vb.width = fh->video_fmt.width;
1086 buf->vb.height = fh->video_fmt.height;
1087 buf->vb.size = size;
1088 buf->vb.field = field;
1089 buf->fmt = &fh->video_fmt;
1090 buf->vb.field = fh->video_fmt.field;
1091
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001092 sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001094 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if( 0 != IS_PLANAR(sfmt->trans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1097 saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
1098 saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
1099 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
1101 }
1102
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001103 err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (err)
1105 goto oops;
1106 err = saa7146_pgtable_build(dev,buf);
1107 if (err)
1108 goto oops;
1109 }
Brandon Philips0fc06862007-11-06 20:02:36 -03001110 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 buf->activate = buffer_activate;
1112
1113 return 0;
1114
1115 oops:
Joe Perches44d0b802011-08-21 19:56:44 -03001116 DEB_D("error out\n");
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001117 saa7146_dma_free(dev,q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return err;
1120}
1121
1122static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1123{
1124 struct file *file = q->priv_data;
1125 struct saa7146_fh *fh = file->private_data;
1126
1127 if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
1128 *count = MAX_SAA7146_CAPTURE_BUFFERS;
1129
1130 *size = fh->video_fmt.sizeimage;
1131
1132 /* check if we exceed the "max_memory" parameter */
1133 if( (*count * *size) > (max_memory*1048576) ) {
1134 *count = (max_memory*1048576) / *size;
1135 }
1136
Joe Perches44d0b802011-08-21 19:56:44 -03001137 DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return 0;
1140}
1141
1142static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1143{
1144 struct file *file = q->priv_data;
1145 struct saa7146_fh *fh = file->private_data;
1146 struct saa7146_dev *dev = fh->dev;
1147 struct saa7146_vv *vv = dev->vv_data;
1148 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1149
Joe Perches44d0b802011-08-21 19:56:44 -03001150 DEB_CAP("vbuf:%p\n", vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1155{
1156 struct file *file = q->priv_data;
1157 struct saa7146_fh *fh = file->private_data;
1158 struct saa7146_dev *dev = fh->dev;
1159 struct saa7146_buf *buf = (struct saa7146_buf *)vb;
1160
Joe Perches44d0b802011-08-21 19:56:44 -03001161 DEB_CAP("vbuf:%p\n", vb);
Johann Friedrichs311c70e2009-10-07 04:41:37 -03001162
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001163 saa7146_dma_free(dev,q,buf);
Mauro Carvalho Chehabba9e9f32010-02-01 21:23:46 -02001164
1165 release_all_pagetables(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168static struct videobuf_queue_ops video_qops = {
1169 .buf_setup = buffer_setup,
1170 .buf_prepare = buffer_prepare,
1171 .buf_queue = buffer_queue,
1172 .buf_release = buffer_release,
1173};
1174
1175/********************************************************************************/
1176/* file operations */
1177
1178static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
1179{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001180 INIT_LIST_HEAD(&vv->video_q.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 init_timer(&vv->video_q.timeout);
1183 vv->video_q.timeout.function = saa7146_buffer_timeout;
1184 vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
1185 vv->video_q.dev = dev;
1186
1187 /* set some default values */
1188 vv->standard = &dev->ext_vv_data->stds[0];
1189
1190 /* FIXME: what's this? */
1191 vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
1192 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
1193}
1194
1195
1196static int video_open(struct saa7146_dev *dev, struct file *file)
1197{
Joe Perchesabf84382010-07-12 17:50:03 -03001198 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct saa7146_format *sfmt;
1200
1201 fh->video_fmt.width = 384;
1202 fh->video_fmt.height = 288;
1203 fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
1204 fh->video_fmt.bytesperline = 0;
1205 fh->video_fmt.field = V4L2_FIELD_ANY;
Hans Verkuil6e65ca92012-04-29 16:47:47 -03001206 fh->video_fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehaba757ee22010-12-02 01:57:03 -02001207 sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
1209
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03001210 videobuf_queue_sg_init(&fh->video_q, &video_qops,
1211 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1213 V4L2_FIELD_INTERLACED,
1214 sizeof(struct saa7146_buf),
Hans Verkuil9af39712010-12-18 09:20:59 -03001215 file, &dev->v4l2_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
1218}
1219
1220
1221static void video_close(struct saa7146_dev *dev, struct file *file)
1222{
Joe Perchesabf84382010-07-12 17:50:03 -03001223 struct saa7146_fh *fh = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct saa7146_vv *vv = dev->vv_data;
Hartmut Birr2970c492007-04-22 06:57:26 -03001225 struct videobuf_queue *q = &fh->video_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Hans Verkuil7b4668e2011-08-25 09:54:30 -03001227 if (IS_CAPTURE_ACTIVE(fh) != 0)
1228 video_end(fh, file);
1229 else if (IS_OVERLAY_ACTIVE(fh) != 0)
1230 saa7146_stop_preview(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Brandon Philips19bc5132007-11-13 20:05:38 -03001232 videobuf_stop(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* hmm, why is this function declared void? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236
1237static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
1238{
1239 struct saa7146_vv *vv = dev->vv_data;
1240 struct saa7146_dmaqueue *q = &vv->video_q;
1241
1242 spin_lock(&dev->slock);
Joe Perches44d0b802011-08-21 19:56:44 -03001243 DEB_CAP("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* only finish the buffer if we have one... */
1246 if( NULL != q->curr ) {
Brandon Philips0fc06862007-11-06 20:02:36 -03001247 saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 saa7146_buffer_next(dev,q,0);
1250
1251 spin_unlock(&dev->slock);
1252}
1253
1254static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1255{
1256 struct saa7146_fh *fh = file->private_data;
1257 struct saa7146_dev *dev = fh->dev;
1258 struct saa7146_vv *vv = dev->vv_data;
1259 ssize_t ret = 0;
1260
Joe Perches44d0b802011-08-21 19:56:44 -03001261 DEB_EE("called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if ((vv->video_status & STATUS_CAPTURE) != 0) {
1264 /* fixme: should we allow read() captures while streaming capture? */
1265 if (vv->video_fh == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -03001266 DEB_S("already capturing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EBUSY;
1268 }
Joe Perches44d0b802011-08-21 19:56:44 -03001269 DEB_S("already capturing in another open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return -EBUSY;
1271 }
1272
1273 ret = video_begin(fh);
1274 if( 0 != ret) {
1275 goto out;
1276 }
1277
1278 ret = videobuf_read_one(&fh->video_q , data, count, ppos,
1279 file->f_flags & O_NONBLOCK);
1280 if (ret != 0) {
1281 video_end(fh, file);
1282 } else {
1283 ret = video_end(fh, file);
1284 }
1285out:
1286 /* restart overlay if it was active before */
1287 if (vv->ov_suspend != NULL) {
1288 saa7146_start_preview(vv->ov_suspend);
1289 vv->ov_suspend = NULL;
1290 }
1291
1292 return ret;
1293}
1294
1295struct saa7146_use_ops saa7146_video_uops = {
1296 .init = video_init,
1297 .open = video_open,
1298 .release = video_close,
1299 .irq_done = video_irq_done,
1300 .read = video_read,
1301};