1 #include <media/saa7146_vv.h>
2
3 static int max_memory = 32;
4
5 module_param(max_memory, int, 0644);
6 MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
7
8 #define IS_CAPTURE_ACTIVE(fh) \
9 (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
10
11 #define IS_OVERLAY_ACTIVE(fh) \
12 (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
13
14 /* format descriptions for capture and preview */
15 static struct saa7146_format formats[] = {
16 {
17 .name = "RGB-8 (3-3-2)",
18 .pixelformat = V4L2_PIX_FMT_RGB332,
19 .trans = RGB08_COMPOSED,
20 .depth = 8,
21 .flags = 0,
22 }, {
23 .name = "RGB-16 (5/B-6/G-5/R)",
24 .pixelformat = V4L2_PIX_FMT_RGB565,
25 .trans = RGB16_COMPOSED,
26 .depth = 16,
27 .flags = 0,
28 }, {
29 .name = "RGB-24 (B-G-R)",
30 .pixelformat = V4L2_PIX_FMT_BGR24,
31 .trans = RGB24_COMPOSED,
32 .depth = 24,
33 .flags = 0,
34 }, {
35 .name = "RGB-32 (B-G-R)",
36 .pixelformat = V4L2_PIX_FMT_BGR32,
37 .trans = RGB32_COMPOSED,
38 .depth = 32,
39 .flags = 0,
40 }, {
41 .name = "RGB-32 (R-G-B)",
42 .pixelformat = V4L2_PIX_FMT_RGB32,
43 .trans = RGB32_COMPOSED,
44 .depth = 32,
45 .flags = 0,
46 .swap = 0x2,
47 }, {
48 .name = "Greyscale-8",
49 .pixelformat = V4L2_PIX_FMT_GREY,
50 .trans = Y8,
51 .depth = 8,
52 .flags = 0,
53 }, {
54 .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
55 .pixelformat = V4L2_PIX_FMT_YUV422P,
56 .trans = YUV422_DECOMPOSED,
57 .depth = 16,
58 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
59 }, {
60 .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
61 .pixelformat = V4L2_PIX_FMT_YVU420,
62 .trans = YUV420_DECOMPOSED,
63 .depth = 12,
64 .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
65 }, {
66 .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
67 .pixelformat = V4L2_PIX_FMT_YUV420,
68 .trans = YUV420_DECOMPOSED,
69 .depth = 12,
70 .flags = FORMAT_IS_PLANAR,
71 }, {
72 .name = "YUV 4:2:2 (U-Y-V-Y)",
73 .pixelformat = V4L2_PIX_FMT_UYVY,
74 .trans = YUV422_COMPOSED,
75 .depth = 16,
76 .flags = 0,
77 }
78 };
79
80 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
81 due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
82 (like V4L2_PIX_FMT_YUYV) ... 8-( */
83
84 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
85
86 struct saa7146_format* format_by_fourcc(struct saa7146_dev *dev, int fourcc)
87 {
88 int i, j = NUM_FORMATS;
89
90 for (i = 0; i < j; i++) {
91 if (formats[i].pixelformat == fourcc) {
92 return formats+i;
93 }
94 }
95
96 DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
97 return NULL;
98 }
99
100 static int g_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
101 {
102 struct saa7146_dev *dev = fh->dev;
103 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
104
105 switch (f->type) {
106 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
107 f->fmt.pix = fh->video_fmt;
108 return 0;
109 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
110 f->fmt.win = fh->ov.win;
111 return 0;
112 case V4L2_BUF_TYPE_VBI_CAPTURE:
113 {
114 f->fmt.vbi = fh->vbi_fmt;
115 return 0;
116 }
117 default:
118 DEB_D(("invalid format type '%d'.\n",f->type));
119 return -EINVAL;
120 }
121 }
122
123 static int try_win(struct saa7146_dev *dev, struct v4l2_window *win)
124 {
125 struct saa7146_vv *vv = dev->vv_data;
126 enum v4l2_field field;
127 int maxw, maxh;
128
129 DEB_EE(("dev:%p\n",dev));
130
131 if (NULL == vv->ov_fb.base) {
132 DEB_D(("no fb base set.\n"));
133 return -EINVAL;
134 }
135 if (NULL == vv->ov_fmt) {
136 DEB_D(("no fb fmt set.\n"));
137 return -EINVAL;
138 }
139 if (win->w.width < 48 || win->w.height < 32) {
140 DEB_D(("min width/height. (%d,%d)\n",win->w.width,win->w.height));
141 return -EINVAL;
142 }
143 if (win->clipcount > 16) {
144 DEB_D(("clipcount too big.\n"));
145 return -EINVAL;
146 }
147
148 field = win->field;
149 maxw = vv->standard->h_max_out;
150 maxh = vv->standard->v_max_out;
151
152 if (V4L2_FIELD_ANY == field) {
153 field = (win->w.height > maxh/2)
154 ? V4L2_FIELD_INTERLACED
155 : V4L2_FIELD_TOP;
156 }
157 switch (field) {
158 case V4L2_FIELD_TOP:
159 case V4L2_FIELD_BOTTOM:
160 case V4L2_FIELD_ALTERNATE:
161 maxh = maxh / 2;
162 break;
163 case V4L2_FIELD_INTERLACED:
164 break;
165 default: {
166 DEB_D(("no known field mode '%d'.\n",field));
167 return -EINVAL;
168 }
169 }
170
171 win->field = field;
172 if (win->w.width > maxw)
173 win->w.width = maxw;
174 if (win->w.height > maxh)
175 win->w.height = maxh;
176
177 return 0;
178 }
179
180 static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
181 {
182 struct saa7146_dev *dev = fh->dev;
183 struct saa7146_vv *vv = dev->vv_data;
184 int err;
185
186 switch (f->type) {
187 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
188 {
189 struct saa7146_format *fmt;
190 enum v4l2_field field;
191 int maxw, maxh;
192 int calc_bpl;
193
194 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
195
196 fmt = format_by_fourcc(dev,f->fmt.pix.pixelformat);
197 if (NULL == fmt) {
198 return -EINVAL;
199 }
200
201 field = f->fmt.pix.field;
202 maxw = vv->standard->h_max_out;
203 maxh = vv->standard->v_max_out;
204
205 if (V4L2_FIELD_ANY == field) {
206 field = (f->fmt.pix.height > maxh/2)
207 ? V4L2_FIELD_INTERLACED
208 : V4L2_FIELD_BOTTOM;
209 }
210 switch (field) {
211 case V4L2_FIELD_ALTERNATE: {
212 vv->last_field = V4L2_FIELD_TOP;
213 maxh = maxh / 2;
214 break;
215 }
216 case V4L2_FIELD_TOP:
217 case V4L2_FIELD_BOTTOM:
218 vv->last_field = V4L2_FIELD_INTERLACED;
219 maxh = maxh / 2;
220 break;
221 case V4L2_FIELD_INTERLACED:
222 vv->last_field = V4L2_FIELD_INTERLACED;
223 break;
224 default: {
225 DEB_D(("no known field mode '%d'.\n",field));
226 return -EINVAL;
227 }
228 }
229
230 f->fmt.pix.field = field;
231 if (f->fmt.pix.width > maxw)
232 f->fmt.pix.width = maxw;
233 if (f->fmt.pix.height > maxh)
234 f->fmt.pix.height = maxh;
235
236 calc_bpl = (f->fmt.pix.width * fmt->depth)/8;
237
238 if (f->fmt.pix.bytesperline < calc_bpl)
239 f->fmt.pix.bytesperline = calc_bpl;
240
241 if (f->fmt.pix.bytesperline > (2*PAGE_SIZE * fmt->depth)/8) /* arbitrary constraint */
242 f->fmt.pix.bytesperline = calc_bpl;
243
244 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
245 DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",f->fmt.pix.width,f->fmt.pix.height,f->fmt.pix.bytesperline,f->fmt.pix.sizeimage));
246
247 return 0;
248 }
249 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
250 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
251 err = try_win(dev,&f->fmt.win);
252 if (0 != err) {
253 return err;
254 }
255 return 0;
256 default:
257 DEB_EE(("unknown format type '%d'\n",f->type));
258 return -EINVAL;
259 }
260 }
261
262 int saa7146_start_preview(struct saa7146_fh *fh)
263 {
264 struct saa7146_dev *dev = fh->dev;
265 struct saa7146_vv *vv = dev->vv_data;
266 int ret = 0, err = 0;
267
268 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
269
270 /* check if we have overlay informations */
271 if( NULL == fh->ov.fh ) {
272 DEB_D(("no overlay data available. try S_FMT first.\n"));
273 return -EAGAIN;
274 }
275
276 /* check if streaming capture is running */
277 if (IS_CAPTURE_ACTIVE(fh) != 0) {
278 DEB_D(("streaming capture is active.\n"));
279 return -EBUSY;
280 }
281
282 /* check if overlay is running */
283 if (IS_OVERLAY_ACTIVE(fh) != 0) {
284 if (vv->video_fh == fh) {
285 DEB_D(("overlay is already active.\n"));
286 return 0;
287 }
288 DEB_D(("overlay is already active in another open.\n"));
289 return -EBUSY;
290 }
291
292 if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
293 DEB_D(("cannot get necessary overlay resources\n"));
294 return -EBUSY;
295 }
296
297 err = try_win(dev,&fh->ov.win);
298 if (0 != err) {
299 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
300 return -EBUSY;
301 }
302
303 vv->ov_data = &fh->ov;
304
305 DEB_D(("%dx%d+%d+%d %s field=%s\n",
306 fh->ov.win.w.width,fh->ov.win.w.height,
307 fh->ov.win.w.left,fh->ov.win.w.top,
308 vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
309
310 if (0 != (ret = saa7146_enable_overlay(fh))) {
311 DEB_D(("enabling overlay failed: %d\n",ret));
312 saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
313 return ret;
314 }
315
316 vv->video_status = STATUS_OVERLAY;
317 vv->video_fh = fh;
318
319 return 0;
320 }
321
322 int saa7146_stop_preview(struct saa7146_fh *fh)
323 {
324 struct saa7146_dev *dev = fh->dev;
325 struct saa7146_vv *vv = dev->vv_data;
326
327 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
328
329 /* check if streaming capture is running */
330 if (IS_CAPTURE_ACTIVE(fh) != 0) {
331 DEB_D(("streaming capture is active.\n"));
332 return -EBUSY;
333 }
334
335 /* check if overlay is running at all */
336 if ((vv->video_status & STATUS_OVERLAY) == 0) {
337 DEB_D(("no active overlay.\n"));
338 return 0;
339 }
340
341 if (vv->video_fh != fh) {
342 DEB_D(("overlay is active, but in another open.\n"));
343 return -EBUSY;
344 }
345
346 vv->video_status = 0;
347 vv->video_fh = NULL;
348
349 saa7146_disable_overlay(fh);
350
351 saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
352
353 return 0;
354 }
355
356 static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
357 {
358 struct saa7146_dev *dev = fh->dev;
359 struct saa7146_vv *vv = dev->vv_data;
360
361 int err;
362
363 switch (f->type) {
364 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
365 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
366 if (IS_CAPTURE_ACTIVE(fh) != 0) {
367 DEB_EE(("streaming capture is active\n"));
368 return -EBUSY;
369 }
370 err = try_fmt(fh,f);
371 if (0 != err)
372 return err;
373 fh->video_fmt = f->fmt.pix;
374 DEB_EE(("set to pixelformat '%4.4s'\n",(char *)&fh->video_fmt.pixelformat));
375 return 0;
376 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
377 DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
378 err = try_win(dev,&f->fmt.win);
379 if (0 != err)
380 return err;
381 mutex_lock(&dev->lock);
382 fh->ov.win = f->fmt.win;
383 fh->ov.nclips = f->fmt.win.clipcount;
384 if (fh->ov.nclips > 16)
385 fh->ov.nclips = 16;
386 if (copy_from_user(fh->ov.clips,f->fmt.win.clips,sizeof(struct v4l2_clip)*fh->ov.nclips)) {
387 mutex_unlock(&dev->lock);
388 return -EFAULT;
389 }
390
391 /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
392 fh->ov.fh = fh;
393
394 mutex_unlock(&dev->lock);
395
396 /* check if our current overlay is active */
397 if (IS_OVERLAY_ACTIVE(fh) != 0) {
398 saa7146_stop_preview(fh);
399 saa7146_start_preview(fh);
400 }
401 return 0;
402 default:
403 DEB_D(("unknown format type '%d'\n",f->type));
404 return -EINVAL;
405 }
406 }
407
408 /********************************************************************************/
409 /* device controls */
410
411 static struct v4l2_queryctrl controls[] = {
412 {
413 .id = V4L2_CID_BRIGHTNESS,
414 .name = "Brightness",
415 .minimum = 0,
416 .maximum = 255,
417 .step = 1,
418 .default_value = 128,
419 .type = V4L2_CTRL_TYPE_INTEGER,
420 },{
421 .id = V4L2_CID_CONTRAST,
422 .name = "Contrast",
423 .minimum = 0,
424 .maximum = 127,
425 .step = 1,
426 .default_value = 64,
427 .type = V4L2_CTRL_TYPE_INTEGER,
428 },{
429 .id = V4L2_CID_SATURATION,
430 .name = "Saturation",
431 .minimum = 0,
432 .maximum = 127,
433 .step = 1,
434 .default_value = 64,
435 .type = V4L2_CTRL_TYPE_INTEGER,
436 },{
437 .id = V4L2_CID_VFLIP,
438 .name = "Vertical flip",
439 .minimum = 0,
440 .maximum = 1,
441 .type = V4L2_CTRL_TYPE_BOOLEAN,
442 },{
443 .id = V4L2_CID_HFLIP,
444 .name = "Horizontal flip",
445 .minimum = 0,
446 .maximum = 1,
447 .type = V4L2_CTRL_TYPE_BOOLEAN,
448 },
449 };
450 static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
451
452 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
453
454 static struct v4l2_queryctrl* ctrl_by_id(int id)
455 {
456 int i;
457
458 for (i = 0; i < NUM_CONTROLS; i++)
459 if (controls[i].id == id)
460 return controls+i;
461 return NULL;
462 }
463
464 static int get_control(struct saa7146_fh *fh, struct v4l2_control *c)
465 {
466 struct saa7146_dev *dev = fh->dev;
467 struct saa7146_vv *vv = dev->vv_data;
468
469 const struct v4l2_queryctrl* ctrl;
470 u32 value = 0;
471
472 ctrl = ctrl_by_id(c->id);
473 if (NULL == ctrl)
474 return -EINVAL;
475 switch (c->id) {
476 case V4L2_CID_BRIGHTNESS:
477 value = saa7146_read(dev, BCS_CTRL);
478 c->value = 0xff & (value >> 24);
479 DEB_D(("V4L2_CID_BRIGHTNESS: %d\n",c->value));
480 break;
481 case V4L2_CID_CONTRAST:
482 value = saa7146_read(dev, BCS_CTRL);
483 c->value = 0x7f & (value >> 16);
484 DEB_D(("V4L2_CID_CONTRAST: %d\n",c->value));
485 break;
486 case V4L2_CID_SATURATION:
487 value = saa7146_read(dev, BCS_CTRL);
488 c->value = 0x7f & (value >> 0);
489 DEB_D(("V4L2_CID_SATURATION: %d\n",c->value));
490 break;
491 case V4L2_CID_VFLIP:
492 c->value = vv->vflip;
493 DEB_D(("V4L2_CID_VFLIP: %d\n",c->value));
494 break;
495 case V4L2_CID_HFLIP:
496 c->value = vv->hflip;
497 DEB_D(("V4L2_CID_HFLIP: %d\n",c->value));
498 break;
499 default:
500 return -EINVAL;
501 }
502
503 return 0;
504 }
505
506 static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
507 {
508 struct saa7146_dev *dev = fh->dev;
509 struct saa7146_vv *vv = dev->vv_data;
510
511 const struct v4l2_queryctrl* ctrl;
512
513 ctrl = ctrl_by_id(c->id);
514 if (NULL == ctrl) {
515 DEB_D(("unknown control %d\n",c->id));
516 return -EINVAL;
517 }
518
519 mutex_lock(&dev->lock);
520
521 switch (ctrl->type) {
522 case V4L2_CTRL_TYPE_BOOLEAN:
523 case V4L2_CTRL_TYPE_MENU:
524 case V4L2_CTRL_TYPE_INTEGER:
525 if (c->value < ctrl->minimum)
526 c->value = ctrl->minimum;
527 if (c->value > ctrl->maximum)
528 c->value = ctrl->maximum;
529 break;
530 default:
531 /* nothing */;
532 };
533
534 switch (c->id) {
535 case V4L2_CID_BRIGHTNESS: {
536 u32 value = saa7146_read(dev, BCS_CTRL);
537 value &= 0x00ffffff;
538 value |= (c->value << 24);
539 saa7146_write(dev, BCS_CTRL, value);
540 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
541 break;
542 }
543 case V4L2_CID_CONTRAST: {
544 u32 value = saa7146_read(dev, BCS_CTRL);
545 value &= 0xff00ffff;
546 value |= (c->value << 16);
547 saa7146_write(dev, BCS_CTRL, value);
548 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
549 break;
550 }
551 case V4L2_CID_SATURATION: {
552 u32 value = saa7146_read(dev, BCS_CTRL);
553 value &= 0xffffff00;
554 value |= (c->value << 0);
555 saa7146_write(dev, BCS_CTRL, value);
556 saa7146_write(dev, MC2, MASK_22 | MASK_06 );
557 break;
558 }
559 case V4L2_CID_HFLIP:
560 /* fixme: we can support changing VFLIP and HFLIP here... */
561 if (IS_CAPTURE_ACTIVE(fh) != 0) {
562 DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
563 mutex_unlock(&dev->lock);
564 return -EINVAL;
565 }
566 vv->hflip = c->value;
567 break;
568 case V4L2_CID_VFLIP:
569 if (IS_CAPTURE_ACTIVE(fh) != 0) {
570 DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
571 mutex_unlock(&dev->lock);
572 return -EINVAL;
573 }
574 vv->vflip = c->value;
575 break;
576 default: {
577 return -EINVAL;
578 }
579 }
580 mutex_unlock(&dev->lock);
581
582 if (IS_OVERLAY_ACTIVE(fh) != 0) {
583 saa7146_stop_preview(fh);
584 saa7146_start_preview(fh);
585 }
586 return 0;
587 }
588
589 /********************************************************************************/
590 /* common pagetable functions */
591
592 static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
593 {
594 struct pci_dev *pci = dev->pci;
595 struct scatterlist *list = buf->vb.dma.sglist;
596 int length = buf->vb.dma.sglen;
597 struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
598
599 DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
600
601 if( 0 != IS_PLANAR(sfmt->trans)) {
602 struct saa7146_pgtable *pt1 = &buf->pt[0];
603 struct saa7146_pgtable *pt2 = &buf->pt[1];
604 struct saa7146_pgtable *pt3 = &buf->pt[2];
605 u32 *ptr1, *ptr2, *ptr3;
606 u32 fill;
607
608 int size = buf->fmt->width*buf->fmt->height;
609 int i,p,m1,m2,m3,o1,o2;
610
611 switch( sfmt->depth ) {
612 case 12: {
613 /* create some offsets inside the page table */
614 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
615 m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
616 m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
617 o1 = size%PAGE_SIZE;
618 o2 = (size+(size/4))%PAGE_SIZE;
619 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
620 break;
621 }
622 case 16: {
623 /* create some offsets inside the page table */
624 m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
625 m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
626 m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
627 o1 = size%PAGE_SIZE;
628 o2 = (size+(size/2))%PAGE_SIZE;
629 DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
630 break;
631 }
632 default: {
633 return -1;
634 }
635 }
636
637 ptr1 = pt1->cpu;
638 ptr2 = pt2->cpu;
639 ptr3 = pt3->cpu;
640
641 /* walk all pages, copy all page addresses to ptr1 */
642 for (i = 0; i < length; i++, list++) {
643 for (p = 0; p * 4096 < list->length; p++, ptr1++) {
644 *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
645 }
646 }
647 /*
648 ptr1 = pt1->cpu;
649 for(j=0;j<40;j++) {
650 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
651 }
652 */
653
654 /* if we have a user buffer, the first page may not be
655 aligned to a page boundary. */
656 pt1->offset = buf->vb.dma.sglist->offset;
657 pt2->offset = pt1->offset+o1;
658 pt3->offset = pt1->offset+o2;
659
660 /* create video-dma2 page table */
661 ptr1 = pt1->cpu;
662 for(i = m1; i <= m2 ; i++, ptr2++) {
663 *ptr2 = ptr1[i];
664 }
665 fill = *(ptr2-1);
666 for(;i<1024;i++,ptr2++) {
667 *ptr2 = fill;
668 }
669 /* create video-dma3 page table */
670 ptr1 = pt1->cpu;
671 for(i = m2; i <= m3; i++,ptr3++) {
672 *ptr3 = ptr1[i];
673 }
674 fill = *(ptr3-1);
675 for(;i<1024;i++,ptr3++) {
676 *ptr3 = fill;
677 }
678 /* finally: finish up video-dma1 page table */
679 ptr1 = pt1->cpu+m1;
680 fill = pt1->cpu[m1];
681 for(i=m1;i<1024;i++,ptr1++) {
682 *ptr1 = fill;
683 }
684 /*
685 ptr1 = pt1->cpu;
686 ptr2 = pt2->cpu;
687 ptr3 = pt3->cpu;
688 for(j=0;j<40;j++) {
689 printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
690 }
691 for(j=0;j<40;j++) {
692 printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
693 }
694 for(j=0;j<40;j++) {
695 printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
696 }
697 */
698 } else {
699 struct saa7146_pgtable *pt = &buf->pt[0];
700 return saa7146_pgtable_build_single(pci, pt, list, length);
701 }
702
703 return 0;
704 }
705
706
707 /********************************************************************************/
708 /* file operations */
709
710 static int video_begin(struct saa7146_fh *fh)
711 {
712 struct saa7146_dev *dev = fh->dev;
713 struct saa7146_vv *vv = dev->vv_data;
714 struct saa7146_format *fmt = NULL;
715 unsigned int resource;
716 int ret = 0, err = 0;
717
718 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
719
720 if ((vv->video_status & STATUS_CAPTURE) != 0) {
721 if (vv->video_fh == fh) {
722 DEB_S(("already capturing.\n"));
723 return 0;
724 }
725 DEB_S(("already capturing in another open.\n"));
726 return -EBUSY;
727 }
728
729 if ((vv->video_status & STATUS_OVERLAY) != 0) {
730 DEB_S(("warning: suspending overlay video for streaming capture.\n"));
731 vv->ov_suspend = vv->video_fh;
732 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
733 if (0 != err) {
734 DEB_D(("suspending video failed. aborting\n"));
735 return err;
736 }
737 }
738
739 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
740 /* we need to have a valid format set here */
741 BUG_ON(NULL == fmt);
742
743 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
744 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
745 } else {
746 resource = RESOURCE_DMA1_HPS;
747 }
748
749 ret = saa7146_res_get(fh, resource);
750 if (0 == ret) {
751 DEB_S(("cannot get capture resource %d\n",resource));
752 if (vv->ov_suspend != NULL) {
753 saa7146_start_preview(vv->ov_suspend);
754 vv->ov_suspend = NULL;
755 }
756 return -EBUSY;
757 }
758
759 /* clear out beginning of streaming bit (rps register 0)*/
760 saa7146_write(dev, MC2, MASK_27 );
761
762 /* enable rps0 irqs */
763 SAA7146_IER_ENABLE(dev, MASK_27);
764
765 vv->video_fh = fh;
766 vv->video_status = STATUS_CAPTURE;
767
768 return 0;
769 }
770
771 static int video_end(struct saa7146_fh *fh, struct file *file)
772 {
773 struct saa7146_dev *dev = fh->dev;
774 struct saa7146_vv *vv = dev->vv_data;
775 struct saa7146_format *fmt = NULL;
776 unsigned long flags;
777 unsigned int resource;
778 u32 dmas = 0;
779 DEB_EE(("dev:%p, fh:%p\n",dev,fh));
780
781 if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
782 DEB_S(("not capturing.\n"));
783 return 0;
784 }
785
786 if (vv->video_fh != fh) {
787 DEB_S(("capturing, but in another open.\n"));
788 return -EBUSY;
789 }
790
791 fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
792 /* we need to have a valid format set here */
793 BUG_ON(NULL == fmt);
794
795 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
796 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
797 dmas = MASK_22 | MASK_21 | MASK_20;
798 } else {
799 resource = RESOURCE_DMA1_HPS;
800 dmas = MASK_22;
801 }
802 spin_lock_irqsave(&dev->slock,flags);
803
804 /* disable rps0 */
805 saa7146_write(dev, MC1, MASK_28);
806
807 /* disable rps0 irqs */
808 SAA7146_IER_DISABLE(dev, MASK_27);
809
810 /* shut down all used video dma transfers */
811 saa7146_write(dev, MC1, dmas);
812
813 spin_unlock_irqrestore(&dev->slock, flags);
814
815 vv->video_fh = NULL;
816 vv->video_status = 0;
817
818 saa7146_res_free(fh, resource);
819
820 if (vv->ov_suspend != NULL) {
821 saa7146_start_preview(vv->ov_suspend);
822 vv->ov_suspend = NULL;
823 }
824
825 return 0;
826 }
827
828 /*
829 * This function is _not_ called directly, but from
830 * video_generic_ioctl (and maybe others). userspace
831 * copying is done already, arg is a kernel pointer.
832 */
833
834 int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
835 {
836 struct saa7146_fh *fh = file->private_data;
837 struct saa7146_dev *dev = fh->dev;
838 struct saa7146_vv *vv = dev->vv_data;
839
840 int err = 0, result = 0, ee = 0;
841
842 struct saa7146_use_ops *ops;
843 struct videobuf_queue *q;
844
845 /* check if extension handles the command */
846 for(ee = 0; dev->ext_vv_data->ioctls[ee].flags != 0; ee++) {
847 if( cmd == dev->ext_vv_data->ioctls[ee].cmd )
848 break;
849 }
850
851 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_EXCLUSIVE) ) {
852 DEB_D(("extension handles ioctl exclusive.\n"));
853 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
854 return result;
855 }
856 if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_BEFORE) ) {
857 DEB_D(("extension handles ioctl before.\n"));
858 result = dev->ext_vv_data->ioctl(fh, cmd, arg);
859 if( -EAGAIN != result ) {
860 return result;
861 }
862 }
863
864 /* fixme: add handle "after" case (is it still needed?) */
865
866 switch (fh->type) {
867 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {