1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/delay.h>
36#include <linux/slab.h>
37#include <linux/pci.h>
38#include <linux/wait.h>
39
40#include <linux/interrupt.h>
41#include <linux/i2c.h>
42#include <linux/i2c-algo-bit.h>
43
44#include <linux/spinlock.h>
45
46#include <linux/videodev2.h>
47#include <media/v4l2-common.h>
48#include <media/v4l2-ioctl.h>
49#include <media/v4l2-event.h>
50#include "videocodec.h"
51
52#include <linux/io.h>
53#include <linux/uaccess.h>
54
55#include <linux/mutex.h>
56#include "zoran.h"
57#include "zoran_device.h"
58#include "zoran_card.h"
59
60const struct zoran_format zoran_formats[] = {
61 {
62 .name = "15-bit RGB LE",
63 .fourcc = V4L2_PIX_FMT_RGB555,
64 .colorspace = V4L2_COLORSPACE_SRGB,
65 .depth = 15,
66 .flags = ZORAN_FORMAT_CAPTURE,
67 .vfespfr = ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ERR_DIF |
68 ZR36057_VFESPFR_LITTLE_ENDIAN,
69 }, {
70 .name = "15-bit RGB BE",
71 .fourcc = V4L2_PIX_FMT_RGB555X,
72 .colorspace = V4L2_COLORSPACE_SRGB,
73 .depth = 15,
74 .flags = ZORAN_FORMAT_CAPTURE,
75 .vfespfr = ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ERR_DIF,
76 }, {
77 .name = "16-bit RGB LE",
78 .fourcc = V4L2_PIX_FMT_RGB565,
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .depth = 16,
81 .flags = ZORAN_FORMAT_CAPTURE,
82 .vfespfr = ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ERR_DIF |
83 ZR36057_VFESPFR_LITTLE_ENDIAN,
84 }, {
85 .name = "16-bit RGB BE",
86 .fourcc = V4L2_PIX_FMT_RGB565X,
87 .colorspace = V4L2_COLORSPACE_SRGB,
88 .depth = 16,
89 .flags = ZORAN_FORMAT_CAPTURE,
90 .vfespfr = ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ERR_DIF,
91 }, {
92 .name = "24-bit RGB",
93 .fourcc = V4L2_PIX_FMT_BGR24,
94 .colorspace = V4L2_COLORSPACE_SRGB,
95 .depth = 24,
96 .flags = ZORAN_FORMAT_CAPTURE,
97 .vfespfr = ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_PACK24,
98 }, {
99 .name = "32-bit RGB LE",
100 .fourcc = V4L2_PIX_FMT_BGR32,
101 .colorspace = V4L2_COLORSPACE_SRGB,
102 .depth = 32,
103 .flags = ZORAN_FORMAT_CAPTURE,
104 .vfespfr = ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_LITTLE_ENDIAN,
105 }, {
106 .name = "32-bit RGB BE",
107 .fourcc = V4L2_PIX_FMT_RGB32,
108 .colorspace = V4L2_COLORSPACE_SRGB,
109 .depth = 32,
110 .flags = ZORAN_FORMAT_CAPTURE,
111 .vfespfr = ZR36057_VFESPFR_RGB888,
112 }, {
113 .name = "4:2:2, packed, YUYV",
114 .fourcc = V4L2_PIX_FMT_YUYV,
115 .colorspace = V4L2_COLORSPACE_SMPTE170M,
116 .depth = 16,
117 .flags = ZORAN_FORMAT_CAPTURE,
118 .vfespfr = ZR36057_VFESPFR_YUV422,
119 }, {
120 .name = "4:2:2, packed, UYVY",
121 .fourcc = V4L2_PIX_FMT_UYVY,
122 .colorspace = V4L2_COLORSPACE_SMPTE170M,
123 .depth = 16,
124 .flags = ZORAN_FORMAT_CAPTURE,
125 .vfespfr = ZR36057_VFESPFR_YUV422 | ZR36057_VFESPFR_LITTLE_ENDIAN,
126 }, {
127 .name = "Hardware-encoded Motion-JPEG",
128 .fourcc = V4L2_PIX_FMT_MJPEG,
129 .colorspace = V4L2_COLORSPACE_SMPTE170M,
130 .depth = 0,
131 .flags = ZORAN_FORMAT_CAPTURE |
132 ZORAN_FORMAT_PLAYBACK |
133 ZORAN_FORMAT_COMPRESSED,
134 }
135};
136
137#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
138
139
140
141
142
143
144static __u32 zoran_v4l2_calc_bufsize(struct zoran_jpg_settings *settings)
145{
146 __u8 div = settings->ver_dcm * settings->hor_dcm * settings->tmp_dcm;
147 __u32 num = (1024 * 512) / (div);
148 __u32 result = 2;
149
150 num--;
151 while (num) {
152 num >>= 1;
153 result <<= 1;
154 }
155
156 if (result > jpg_bufsize)
157 return jpg_bufsize;
158 if (result < 8192)
159 return 8192;
160
161 return result;
162}
163
164
165
166
167static int zoran_v4l_set_format(struct zoran *zr, int width, int height,
168 const struct zoran_format *format)
169{
170 int bpp;
171
172
173
174 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
175 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
176 pci_err(zr->pci_dev, "%s - wrong frame size (%dx%d)\n", __func__, width, height);
177 return -EINVAL;
178 }
179
180 bpp = (format->depth + 7) / 8;
181
182 zr->buffer_size = height * width * bpp;
183
184
185 if (height * width * bpp > zr->buffer_size) {
186 pci_err(zr->pci_dev, "%s - video buffer size (%d kB) is too small\n",
187 __func__, zr->buffer_size >> 10);
188 return -EINVAL;
189 }
190
191
192
193 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
194 pci_err(zr->pci_dev, "%s - wrong frame alignment\n", __func__);
195 return -EINVAL;
196 }
197
198 zr->v4l_settings.width = width;
199 zr->v4l_settings.height = height;
200 zr->v4l_settings.format = format;
201 zr->v4l_settings.bytesperline = bpp * zr->v4l_settings.width;
202
203 return 0;
204}
205
206static int zoran_set_norm(struct zoran *zr, v4l2_std_id norm)
207{
208
209 if (!(norm & zr->card.norms)) {
210 pci_err(zr->pci_dev, "%s - unsupported norm %llx\n", __func__, norm);
211 return -EINVAL;
212 }
213
214 if (norm & V4L2_STD_SECAM)
215 zr->timing = zr->card.tvn[ZR_NORM_SECAM];
216 else if (norm & V4L2_STD_NTSC)
217 zr->timing = zr->card.tvn[ZR_NORM_NTSC];
218 else
219 zr->timing = zr->card.tvn[ZR_NORM_PAL];
220
221 decoder_call(zr, video, s_std, norm);
222 encoder_call(zr, video, s_std_output, norm);
223
224
225 zr->norm = norm;
226
227 return 0;
228}
229
230static int zoran_set_input(struct zoran *zr, int input)
231{
232 if (input == zr->input)
233 return 0;
234
235 if (input < 0 || input >= zr->card.inputs) {
236 pci_err(zr->pci_dev, "%s - unsupported input %d\n", __func__, input);
237 return -EINVAL;
238 }
239
240 zr->input = input;
241
242 decoder_call(zr, video, s_routing, zr->card.input[input].muxsel, 0, 0);
243
244 return 0;
245}
246
247
248
249
250
251static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
252{
253 struct zoran *zr = video_drvdata(file);
254
255 strscpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
256 strscpy(cap->driver, "zoran", sizeof(cap->driver));
257 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", pci_name(zr->pci_dev));
258 cap->device_caps = zr->video_dev->device_caps;
259 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
260 return 0;
261}
262
263static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
264{
265 unsigned int num, i;
266
267 if (fmt->index >= ARRAY_SIZE(zoran_formats))
268 return -EINVAL;
269 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
270 return -EINVAL;
271
272 for (num = i = 0; i < NUM_FORMATS; i++) {
273 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
274 strscpy(fmt->description, zoran_formats[i].name,
275 sizeof(fmt->description));
276
277 fmt->pixelformat = zoran_formats[i].fourcc;
278 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
279 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
280 return 0;
281 }
282 }
283 return -EINVAL;
284}
285
286static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
287 struct v4l2_fmtdesc *f)
288{
289 struct zoran *zr = video_drvdata(file);
290
291 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
292}
293
294#if 0
295
296static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
297 struct v4l2_fmtdesc *f)
298{
299 struct zoran *zr = video_drvdata(file);
300
301 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
302}
303#endif
304
305static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
306 struct v4l2_format *fmt)
307{
308 struct zoran *zr = video_drvdata(file);
309
310 fmt->fmt.pix.width = zr->jpg_settings.img_width / zr->jpg_settings.hor_dcm;
311 fmt->fmt.pix.height = zr->jpg_settings.img_height * 2 /
312 (zr->jpg_settings.ver_dcm * zr->jpg_settings.tmp_dcm);
313 fmt->fmt.pix.sizeimage = zr->buffer_size;
314 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
315 if (zr->jpg_settings.tmp_dcm == 1)
316 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
317 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
318 else
319 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
320 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
321 fmt->fmt.pix.bytesperline = 0;
322 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
323
324 return 0;
325}
326
327static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
328 struct v4l2_format *fmt)
329{
330 struct zoran *zr = video_drvdata(file);
331
332 if (zr->map_mode != ZORAN_MAP_MODE_RAW)
333 return zoran_g_fmt_vid_out(file, __fh, fmt);
334 fmt->fmt.pix.width = zr->v4l_settings.width;
335 fmt->fmt.pix.height = zr->v4l_settings.height;
336 fmt->fmt.pix.sizeimage = zr->buffer_size;
337 fmt->fmt.pix.pixelformat = zr->v4l_settings.format->fourcc;
338 fmt->fmt.pix.colorspace = zr->v4l_settings.format->colorspace;
339 fmt->fmt.pix.bytesperline = zr->v4l_settings.bytesperline;
340 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
341 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
342 else
343 fmt->fmt.pix.field = V4L2_FIELD_TOP;
344 return 0;
345}
346
347static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
348 struct v4l2_format *fmt)
349{
350 struct zoran *zr = video_drvdata(file);
351 struct zoran_jpg_settings settings;
352 int res = 0;
353
354 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
355 return -EINVAL;
356
357 settings = zr->jpg_settings;
358
359
360 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
361 settings.tmp_dcm = 1;
362 else
363 settings.tmp_dcm = 2;
364 settings.decimation = 0;
365 if (fmt->fmt.pix.height <= zr->jpg_settings.img_height / 2)
366 settings.ver_dcm = 2;
367 else
368 settings.ver_dcm = 1;
369 if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 4)
370 settings.hor_dcm = 4;
371 else if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 2)
372 settings.hor_dcm = 2;
373 else
374 settings.hor_dcm = 1;
375 if (settings.tmp_dcm == 1)
376 settings.field_per_buff = 2;
377 else
378 settings.field_per_buff = 1;
379
380 if (settings.hor_dcm > 1) {
381 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
382 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
383 } else {
384 settings.img_x = 0;
385 settings.img_width = BUZ_MAX_WIDTH;
386 }
387
388
389 res = zoran_check_jpg_settings(zr, &settings, 1);
390 if (res)
391 return res;
392
393
394 fmt->fmt.pix.width = settings.img_width / settings.hor_dcm;
395 fmt->fmt.pix.height = settings.img_height * 2 /
396 (settings.tmp_dcm * settings.ver_dcm);
397 if (settings.tmp_dcm == 1)
398 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
399 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
400 else
401 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
402 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
403
404 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
405 zr->buffer_size = fmt->fmt.pix.sizeimage;
406 fmt->fmt.pix.bytesperline = 0;
407 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
408 return res;
409}
410
411static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
412 struct v4l2_format *fmt)
413{
414 struct zoran *zr = video_drvdata(file);
415 int bpp;
416 int i;
417
418 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
419 return zoran_try_fmt_vid_out(file, __fh, fmt);
420
421 for (i = 0; i < NUM_FORMATS; i++)
422 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
423 break;
424
425 if (i == NUM_FORMATS) {
426
427 return -EINVAL;
428 }
429
430 fmt->fmt.pix.pixelformat = zoran_formats[i].fourcc;
431 fmt->fmt.pix.colorspace = zoran_formats[i].colorspace;
432 if (BUZ_MAX_HEIGHT < (fmt->fmt.pix.height * 2))
433 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
434 else
435 fmt->fmt.pix.field = V4L2_FIELD_TOP;
436
437 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
438 v4l_bound_align_image(&fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
439 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
440 return 0;
441}
442
443static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
444 struct v4l2_format *fmt)
445{
446 struct zoran *zr = video_drvdata(file);
447 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
448 struct zoran_jpg_settings settings;
449 int res = 0;
450
451 pci_dbg(zr->pci_dev, "size=%dx%d, fmt=0x%x (%4.4s)\n",
452 fmt->fmt.pix.width, fmt->fmt.pix.height,
453 fmt->fmt.pix.pixelformat,
454 (char *)&printformat);
455 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
456 return -EINVAL;
457
458 if (!fmt->fmt.pix.height || !fmt->fmt.pix.width)
459 return -EINVAL;
460
461 settings = zr->jpg_settings;
462
463
464 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
465 settings.tmp_dcm = 1;
466 else
467 settings.tmp_dcm = 2;
468 settings.decimation = 0;
469 if (fmt->fmt.pix.height <= zr->jpg_settings.img_height / 2)
470 settings.ver_dcm = 2;
471 else
472 settings.ver_dcm = 1;
473 if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 4)
474 settings.hor_dcm = 4;
475 else if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 2)
476 settings.hor_dcm = 2;
477 else
478 settings.hor_dcm = 1;
479 if (settings.tmp_dcm == 1)
480 settings.field_per_buff = 2;
481 else
482 settings.field_per_buff = 1;
483
484 if (settings.hor_dcm > 1) {
485 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
486 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
487 } else {
488 settings.img_x = 0;
489 settings.img_width = BUZ_MAX_WIDTH;
490 }
491
492
493 res = zoran_check_jpg_settings(zr, &settings, 0);
494 if (res)
495 return res;
496
497
498 zr->jpg_settings = settings;
499
500 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
501 zr->map_mode = ZORAN_MAP_MODE_JPG_REC;
502 else
503 zr->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
504
505 zr->buffer_size = zoran_v4l2_calc_bufsize(&zr->jpg_settings);
506
507
508 fmt->fmt.pix.width = settings.img_width / settings.hor_dcm;
509 fmt->fmt.pix.height = settings.img_height * 2 /
510 (settings.tmp_dcm * settings.ver_dcm);
511 if (settings.tmp_dcm == 1)
512 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
513 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
514 else
515 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
516 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
517 fmt->fmt.pix.bytesperline = 0;
518 fmt->fmt.pix.sizeimage = zr->buffer_size;
519 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
520 return res;
521}
522
523static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
524 struct v4l2_format *fmt)
525{
526 struct zoran *zr = video_drvdata(file);
527 struct zoran_fh *fh = __fh;
528 int i;
529 int res = 0;
530
531 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
532 return zoran_s_fmt_vid_out(file, fh, fmt);
533
534 for (i = 0; i < NUM_FORMATS; i++)
535 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
536 break;
537 if (i == NUM_FORMATS) {
538 pci_err(zr->pci_dev, "VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
539 fmt->fmt.pix.pixelformat);
540
541 return -EINVAL;
542 }
543
544 fmt->fmt.pix.pixelformat = zoran_formats[i].fourcc;
545 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
546 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
547 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
548 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
549 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
550 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
551 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
552 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
553
554 zr->map_mode = ZORAN_MAP_MODE_RAW;
555
556 res = zoran_v4l_set_format(zr, fmt->fmt.pix.width, fmt->fmt.pix.height,
557 &zoran_formats[i]);
558 if (res)
559 return res;
560
561
562 fmt->fmt.pix.bytesperline = zr->v4l_settings.bytesperline;
563 fmt->fmt.pix.sizeimage = zr->buffer_size;
564 fmt->fmt.pix.colorspace = zr->v4l_settings.format->colorspace;
565 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
566 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
567 else
568 fmt->fmt.pix.field = V4L2_FIELD_TOP;
569 return res;
570}
571
572static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
573{
574 struct zoran *zr = video_drvdata(file);
575
576 *std = zr->norm;
577 return 0;
578}
579
580static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id std)
581{
582 struct zoran *zr = video_drvdata(file);
583 int res = 0;
584
585 if (zr->running != ZORAN_MAP_MODE_NONE)
586 return -EBUSY;
587
588 res = zoran_set_norm(zr, std);
589 return res;
590}
591
592static int zoran_enum_input(struct file *file, void *__fh,
593 struct v4l2_input *inp)
594{
595 struct zoran *zr = video_drvdata(file);
596
597 if (inp->index >= zr->card.inputs)
598 return -EINVAL;
599
600 strscpy(inp->name, zr->card.input[inp->index].name, sizeof(inp->name));
601 inp->type = V4L2_INPUT_TYPE_CAMERA;
602 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
603
604
605 decoder_call(zr, video, g_input_status, &inp->status);
606 return 0;
607}
608
609static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
610{
611 struct zoran *zr = video_drvdata(file);
612
613 *input = zr->input;
614
615 return 0;
616}
617
618static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
619{
620 struct zoran *zr = video_drvdata(file);
621 int res;
622
623 if (zr->running != ZORAN_MAP_MODE_NONE)
624 return -EBUSY;
625
626 res = zoran_set_input(zr, input);
627 return res;
628}
629
630#if 0
631
632static int zoran_enum_output(struct file *file, void *__fh,
633 struct v4l2_output *outp)
634{
635 if (outp->index != 0)
636 return -EINVAL;
637
638 outp->index = 0;
639 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
640 outp->std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
641 outp->capabilities = V4L2_OUT_CAP_STD;
642 strscpy(outp->name, "Autodetect", sizeof(outp->name));
643
644 return 0;
645}
646static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
647{
648 *output = 0;
649
650 return 0;
651}
652
653static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
654{
655 if (output != 0)
656 return -EINVAL;
657
658 return 0;
659}
660#endif
661
662
663static int zoran_g_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
664{
665 struct zoran *zr = video_drvdata(file);
666
667 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
668 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
669 pci_err(zr->pci_dev, "%s invalid selection type combination\n", __func__);
670 return -EINVAL;
671 }
672
673 switch (sel->target) {
674 case V4L2_SEL_TGT_CROP:
675 sel->r.top = zr->jpg_settings.img_y;
676 sel->r.left = zr->jpg_settings.img_x;
677 sel->r.width = zr->jpg_settings.img_width;
678 sel->r.height = zr->jpg_settings.img_height;
679 break;
680 case V4L2_SEL_TGT_CROP_DEFAULT:
681 sel->r.top = sel->r.left = 0;
682 sel->r.width = BUZ_MIN_WIDTH;
683 sel->r.height = BUZ_MIN_HEIGHT;
684 break;
685 case V4L2_SEL_TGT_CROP_BOUNDS:
686 sel->r.top = sel->r.left = 0;
687 sel->r.width = BUZ_MAX_WIDTH;
688 sel->r.height = BUZ_MAX_HEIGHT;
689 break;
690 default:
691 return -EINVAL;
692 }
693 return 0;
694}
695
696static int zoran_s_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
697{
698 struct zoran *zr = video_drvdata(file);
699 struct zoran_jpg_settings settings;
700 int res;
701
702 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
703 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
704 return -EINVAL;
705
706 if (!sel->r.width || !sel->r.height)
707 return -EINVAL;
708
709 if (sel->target != V4L2_SEL_TGT_CROP)
710 return -EINVAL;
711
712 if (zr->map_mode == ZORAN_MAP_MODE_RAW) {
713 pci_err(zr->pci_dev, "VIDIOC_S_SELECTION - subcapture only supported for compressed capture\n");
714 return -EINVAL;
715 }
716
717 settings = zr->jpg_settings;
718
719
720 settings.img_x = sel->r.left;
721 settings.img_y = sel->r.top;
722 settings.img_width = sel->r.width;
723 settings.img_height = sel->r.height;
724
725
726 res = zoran_check_jpg_settings(zr, &settings, 0);
727 if (res)
728 return res;
729
730
731 zr->jpg_settings = settings;
732 return res;
733}
734
735static int zoran_g_parm(struct file *file, void *priv, struct v4l2_streamparm *parm)
736{
737 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
738 return -EINVAL;
739
740 return 0;
741}
742
743
744
745
746
747
748static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
749 .vidioc_querycap = zoran_querycap,
750 .vidioc_g_parm = zoran_g_parm,
751 .vidioc_s_selection = zoran_s_selection,
752 .vidioc_g_selection = zoran_g_selection,
753 .vidioc_enum_input = zoran_enum_input,
754 .vidioc_g_input = zoran_g_input,
755 .vidioc_s_input = zoran_s_input,
756
757
758
759 .vidioc_g_std = zoran_g_std,
760 .vidioc_s_std = zoran_s_std,
761 .vidioc_create_bufs = vb2_ioctl_create_bufs,
762 .vidioc_reqbufs = vb2_ioctl_reqbufs,
763 .vidioc_querybuf = vb2_ioctl_querybuf,
764 .vidioc_qbuf = vb2_ioctl_qbuf,
765 .vidioc_dqbuf = vb2_ioctl_dqbuf,
766 .vidioc_expbuf = vb2_ioctl_expbuf,
767 .vidioc_streamon = vb2_ioctl_streamon,
768 .vidioc_streamoff = vb2_ioctl_streamoff,
769 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
770
771 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
772
773 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
774
775 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
776
777 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
778 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
779};
780
781static const struct v4l2_file_operations zoran_fops = {
782 .owner = THIS_MODULE,
783 .unlocked_ioctl = video_ioctl2,
784 .open = v4l2_fh_open,
785 .release = vb2_fop_release,
786 .read = vb2_fop_read,
787 .write = vb2_fop_write,
788 .mmap = vb2_fop_mmap,
789 .poll = vb2_fop_poll,
790};
791
792const struct video_device zoran_template = {
793 .name = ZORAN_NAME,
794 .fops = &zoran_fops,
795 .ioctl_ops = &zoran_ioctl_ops,
796 .release = &zoran_vdev_release,
797 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
798};
799
800static int zr_vb2_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes,
801 unsigned int sizes[], struct device *alloc_devs[])
802{
803 struct zoran *zr = vb2_get_drv_priv(vq);
804 unsigned int size = zr->buffer_size;
805
806 pci_dbg(zr->pci_dev, "%s nbuf=%u nplanes=%u", __func__, *nbuffers, *nplanes);
807
808 zr->buf_in_reserve = 0;
809
810 if (*nbuffers < vq->min_buffers_needed)
811 *nbuffers = vq->min_buffers_needed;
812
813 if (*nplanes) {
814 if (sizes[0] < size)
815 return -EINVAL;
816 else
817 return 0;
818 }
819
820 *nplanes = 1;
821 sizes[0] = size;
822
823 return 0;
824}
825
826static void zr_vb2_queue(struct vb2_buffer *vb)
827{
828 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
829 struct zr_buffer *buf = vb2_to_zr_buffer(vb);
830 unsigned long flags;
831
832 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
833 list_add_tail(&buf->queue, &zr->queued_bufs);
834 zr->buf_in_reserve++;
835 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
836 if (zr->running == ZORAN_MAP_MODE_JPG_REC)
837 zoran_feed_stat_com(zr);
838 zr->queued++;
839}
840
841static int zr_vb2_prepare(struct vb2_buffer *vb)
842{
843 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
844
845 if (vb2_plane_size(vb, 0) < zr->buffer_size)
846 return -EINVAL;
847 zr->prepared++;
848
849 return 0;
850}
851
852int zr_set_buf(struct zoran *zr)
853{
854 struct zr_buffer *buf;
855 struct vb2_v4l2_buffer *vbuf;
856 dma_addr_t phys_addr;
857 unsigned long flags;
858 u32 reg;
859
860 if (zr->running == ZORAN_MAP_MODE_NONE)
861 return 0;
862
863 if (zr->inuse[0]) {
864 buf = zr->inuse[0];
865 buf->vbuf.vb2_buf.timestamp = ktime_get_ns();
866 buf->vbuf.sequence = zr->vbseq++;
867 vbuf = &buf->vbuf;
868
869 buf->vbuf.field = V4L2_FIELD_INTERLACED;
870 vb2_set_plane_payload(&buf->vbuf.vb2_buf, 0, zr->buffer_size);
871 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
872 zr->inuse[0] = NULL;
873 }
874
875 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
876 if (list_empty(&zr->queued_bufs)) {
877 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
878 vb2_queue_error(zr->video_dev->queue);
879 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
880 return -EINVAL;
881 }
882 buf = list_first_entry_or_null(&zr->queued_bufs, struct zr_buffer, queue);
883 if (!buf) {
884 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
885 vb2_queue_error(zr->video_dev->queue);
886 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
887 return -EINVAL;
888 }
889 list_del(&buf->queue);
890 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
891
892 vbuf = &buf->vbuf;
893 vbuf->vb2_buf.state = VB2_BUF_STATE_ACTIVE;
894 phys_addr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
895
896 if (!phys_addr)
897 return -EINVAL;
898
899 zr->inuse[0] = buf;
900
901 reg = phys_addr;
902 btwrite(reg, ZR36057_VDTR);
903 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
904 reg += zr->v4l_settings.bytesperline;
905 btwrite(reg, ZR36057_VDBR);
906
907 reg = 0;
908 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
909 reg += zr->v4l_settings.bytesperline;
910 reg = (reg << ZR36057_VSSFGR_DISP_STRIDE);
911 reg |= ZR36057_VSSFGR_VID_OVF;
912 reg |= ZR36057_VSSFGR_SNAP_SHOT;
913 reg |= ZR36057_VSSFGR_FRAME_GRAB;
914 btwrite(reg, ZR36057_VSSFGR);
915
916 btor(ZR36057_VDCR_VID_EN, ZR36057_VDCR);
917 return 0;
918}
919
920static int zr_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
921{
922 struct zoran *zr = vq->drv_priv;
923 int j;
924
925 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
926 zr->stat_com[j] = cpu_to_le32(1);
927 zr->inuse[j] = NULL;
928 }
929
930 if (zr->map_mode != ZORAN_MAP_MODE_RAW) {
931 pci_info(zr->pci_dev, "START JPG\n");
932 zr36057_restart(zr);
933 zoran_init_hardware(zr);
934 if (zr->map_mode == ZORAN_MAP_MODE_JPG_REC)
935 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_DECOMPRESS);
936 else
937 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_COMPRESS);
938 zoran_feed_stat_com(zr);
939 jpeg_start(zr);
940 zr->running = zr->map_mode;
941 btor(ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
942 return 0;
943 }
944
945 pci_info(zr->pci_dev, "START RAW\n");
946 zr36057_restart(zr);
947 zoran_init_hardware(zr);
948
949 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
950 zr36057_set_memgrab(zr, 1);
951 zr->running = zr->map_mode;
952 btor(ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
953 return 0;
954}
955
956static void zr_vb2_stop_streaming(struct vb2_queue *vq)
957{
958 struct zoran *zr = vq->drv_priv;
959 struct zr_buffer *buf;
960 unsigned long flags;
961 int j;
962
963 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
964 if (zr->map_mode != ZORAN_MAP_MODE_RAW)
965 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
966 zr36057_set_memgrab(zr, 0);
967 zr->running = ZORAN_MAP_MODE_NONE;
968
969 zoran_set_pci_master(zr, 0);
970
971 if (!pass_through) {
972 decoder_call(zr, video, s_stream, 0);
973 encoder_call(zr, video, s_routing, 2, 0, 0);
974 }
975
976 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
977 zr->stat_com[j] = cpu_to_le32(1);
978 if (!zr->inuse[j])
979 continue;
980 buf = zr->inuse[j];
981 pci_dbg(zr->pci_dev, "%s clean buf %d\n", __func__, j);
982 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
983 zr->inuse[j] = NULL;
984 }
985
986 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
987 while (!list_empty(&zr->queued_bufs)) {
988 buf = list_entry(zr->queued_bufs.next, struct zr_buffer, queue);
989 list_del(&buf->queue);
990 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
991 zr->buf_in_reserve--;
992 }
993 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
994 if (zr->buf_in_reserve)
995 pci_err(zr->pci_dev, "Buffer remaining %d\n", zr->buf_in_reserve);
996 zr->map_mode = ZORAN_MAP_MODE_RAW;
997}
998
999static const struct vb2_ops zr_video_qops = {
1000 .queue_setup = zr_vb2_queue_setup,
1001 .buf_queue = zr_vb2_queue,
1002 .buf_prepare = zr_vb2_prepare,
1003 .start_streaming = zr_vb2_start_streaming,
1004 .stop_streaming = zr_vb2_stop_streaming,
1005 .wait_prepare = vb2_ops_wait_prepare,
1006 .wait_finish = vb2_ops_wait_finish,
1007};
1008
1009int zoran_queue_init(struct zoran *zr, struct vb2_queue *vq)
1010{
1011 int err;
1012
1013 spin_lock_init(&zr->queued_bufs_lock);
1014 INIT_LIST_HEAD(&zr->queued_bufs);
1015
1016 vq->dev = &zr->pci_dev->dev;
1017 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1018 vq->io_modes = VB2_USERPTR | VB2_DMABUF | VB2_MMAP | VB2_READ | VB2_WRITE;
1019 vq->drv_priv = zr;
1020 vq->buf_struct_size = sizeof(struct zr_buffer);
1021 vq->ops = &zr_video_qops;
1022 vq->mem_ops = &vb2_dma_contig_memops;
1023 vq->gfp_flags = GFP_DMA32;
1024 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1025 vq->min_buffers_needed = 9;
1026 vq->lock = &zr->lock;
1027 err = vb2_queue_init(vq);
1028 if (err)
1029 return err;
1030 zr->video_dev->queue = vq;
1031 return 0;
1032}
1033
1034void zoran_queue_exit(struct zoran *zr)
1035{
1036 vb2_queue_release(zr->video_dev->queue);
1037}
1038