1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zoran zr36057/zr36067 PCI controller driver, for the
4 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
5 * Media Labs LML33/LML33R10.
7 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
11 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
15 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
20 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
22 * Iomega Buz driver version 1.0
23 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
26 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
28 * bttv - Bt848 frame grabber driver
29 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
30 * & Marcus Metzler (mocm@thp.uni-koeln.de)
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>
40 #include <linux/interrupt.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-algo-bit.h>
44 #include <linux/spinlock.h>
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"
53 #include <linux/uaccess.h>
55 #include <linux/mutex.h>
57 #include "zoran_device.h"
58 #include "zoran_card.h"
60 const struct zoran_format zoran_formats[] = {
62 .name = "15-bit RGB LE",
63 .fourcc = V4L2_PIX_FMT_RGB555,
64 .colorspace = V4L2_COLORSPACE_SRGB,
66 .flags = ZORAN_FORMAT_CAPTURE,
67 .vfespfr = ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ERR_DIF |
68 ZR36057_VFESPFR_LITTLE_ENDIAN,
70 .name = "15-bit RGB BE",
71 .fourcc = V4L2_PIX_FMT_RGB555X,
72 .colorspace = V4L2_COLORSPACE_SRGB,
74 .flags = ZORAN_FORMAT_CAPTURE,
75 .vfespfr = ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ERR_DIF,
77 .name = "16-bit RGB LE",
78 .fourcc = V4L2_PIX_FMT_RGB565,
79 .colorspace = V4L2_COLORSPACE_SRGB,
81 .flags = ZORAN_FORMAT_CAPTURE,
82 .vfespfr = ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ERR_DIF |
83 ZR36057_VFESPFR_LITTLE_ENDIAN,
85 .name = "16-bit RGB BE",
86 .fourcc = V4L2_PIX_FMT_RGB565X,
87 .colorspace = V4L2_COLORSPACE_SRGB,
89 .flags = ZORAN_FORMAT_CAPTURE,
90 .vfespfr = ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ERR_DIF,
93 .fourcc = V4L2_PIX_FMT_BGR24,
94 .colorspace = V4L2_COLORSPACE_SRGB,
96 .flags = ZORAN_FORMAT_CAPTURE,
97 .vfespfr = ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_PACK24,
99 .name = "32-bit RGB LE",
100 .fourcc = V4L2_PIX_FMT_BGR32,
101 .colorspace = V4L2_COLORSPACE_SRGB,
103 .flags = ZORAN_FORMAT_CAPTURE,
104 .vfespfr = ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_LITTLE_ENDIAN,
106 .name = "32-bit RGB BE",
107 .fourcc = V4L2_PIX_FMT_RGB32,
108 .colorspace = V4L2_COLORSPACE_SRGB,
110 .flags = ZORAN_FORMAT_CAPTURE,
111 .vfespfr = ZR36057_VFESPFR_RGB888,
113 .name = "4:2:2, packed, YUYV",
114 .fourcc = V4L2_PIX_FMT_YUYV,
115 .colorspace = V4L2_COLORSPACE_SMPTE170M,
117 .flags = ZORAN_FORMAT_CAPTURE,
118 .vfespfr = ZR36057_VFESPFR_YUV422,
120 .name = "4:2:2, packed, UYVY",
121 .fourcc = V4L2_PIX_FMT_UYVY,
122 .colorspace = V4L2_COLORSPACE_SMPTE170M,
124 .flags = ZORAN_FORMAT_CAPTURE,
125 .vfespfr = ZR36057_VFESPFR_YUV422 | ZR36057_VFESPFR_LITTLE_ENDIAN,
127 .name = "Hardware-encoded Motion-JPEG",
128 .fourcc = V4L2_PIX_FMT_MJPEG,
129 .colorspace = V4L2_COLORSPACE_SMPTE170M,
131 .flags = ZORAN_FORMAT_CAPTURE |
132 ZORAN_FORMAT_PLAYBACK |
133 ZORAN_FORMAT_COMPRESSED,
137 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
140 * small helper function for calculating buffersizes for v4l2
141 * we calculate the nearest higher power-of-two, which
142 * will be the recommended buffersize
144 static __u32 zoran_v4l2_calc_bufsize(struct zoran_jpg_settings *settings)
146 __u8 div = settings->ver_dcm * settings->hor_dcm * settings->tmp_dcm;
147 __u32 num = (1024 * 512) / (div);
156 if (result > jpg_bufsize)
165 * V4L Buffer grabbing
167 static int zoran_v4l_set_format(struct zoran *zr, int width, int height,
168 const struct zoran_format *format)
172 /* Check size and format of the grab wanted */
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);
180 bpp = (format->depth + 7) / 8;
182 zr->buffer_size = height * width * bpp;
184 /* Check against available buffer size */
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);
191 /* The video front end needs 4-byte alinged line sizes */
193 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
194 pci_err(zr->pci_dev, "%s - wrong frame alignment\n", __func__);
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;
206 static int zoran_set_norm(struct zoran *zr, v4l2_std_id norm)
209 if (!(norm & zr->card.norms)) {
210 pci_err(zr->pci_dev, "%s - unsupported norm %llx\n", __func__, norm);
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];
219 zr->timing = zr->card.tvn[ZR_NORM_PAL];
221 decoder_call(zr, video, s_std, norm);
222 encoder_call(zr, video, s_std_output, norm);
224 /* Make sure the changes come into effect */
230 static int zoran_set_input(struct zoran *zr, int input)
232 if (input == zr->input)
235 if (input < 0 || input >= zr->card.inputs) {
236 pci_err(zr->pci_dev, "%s - unsupported input %d\n", __func__, input);
242 decoder_call(zr, video, s_routing, zr->card.input[input].muxsel, 0, 0);
251 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
253 struct zoran *zr = video_drvdata(file);
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));
261 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
265 if (fmt->index >= ARRAY_SIZE(zoran_formats))
267 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
270 for (num = i = 0; i < NUM_FORMATS; i++) {
271 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
272 strscpy(fmt->description, zoran_formats[i].name,
273 sizeof(fmt->description));
274 /* fmt struct pre-zeroed, so adding '\0' not needed */
275 fmt->pixelformat = zoran_formats[i].fourcc;
276 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
277 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
284 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
285 struct v4l2_fmtdesc *f)
287 struct zoran *zr = video_drvdata(file);
289 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
293 /* TODO: output does not work yet */
294 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
295 struct v4l2_fmtdesc *f)
297 struct zoran *zr = video_drvdata(file);
299 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
303 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
304 struct v4l2_format *fmt)
306 struct zoran *zr = video_drvdata(file);
308 fmt->fmt.pix.width = zr->jpg_settings.img_width / zr->jpg_settings.hor_dcm;
309 fmt->fmt.pix.height = zr->jpg_settings.img_height * 2 /
310 (zr->jpg_settings.ver_dcm * zr->jpg_settings.tmp_dcm);
311 fmt->fmt.pix.sizeimage = zr->buffer_size;
312 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
313 if (zr->jpg_settings.tmp_dcm == 1)
314 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
315 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
317 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
318 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
319 fmt->fmt.pix.bytesperline = 0;
320 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
325 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
326 struct v4l2_format *fmt)
328 struct zoran *zr = video_drvdata(file);
330 if (zr->map_mode != ZORAN_MAP_MODE_RAW)
331 return zoran_g_fmt_vid_out(file, __fh, fmt);
332 fmt->fmt.pix.width = zr->v4l_settings.width;
333 fmt->fmt.pix.height = zr->v4l_settings.height;
334 fmt->fmt.pix.sizeimage = zr->buffer_size;
335 fmt->fmt.pix.pixelformat = zr->v4l_settings.format->fourcc;
336 fmt->fmt.pix.colorspace = zr->v4l_settings.format->colorspace;
337 fmt->fmt.pix.bytesperline = zr->v4l_settings.bytesperline;
338 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
339 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
341 fmt->fmt.pix.field = V4L2_FIELD_TOP;
345 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
346 struct v4l2_format *fmt)
348 struct zoran *zr = video_drvdata(file);
349 struct zoran_jpg_settings settings;
352 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
355 settings = zr->jpg_settings;
357 /* we actually need to set 'real' parameters now */
358 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
359 settings.tmp_dcm = 1;
361 settings.tmp_dcm = 2;
362 settings.decimation = 0;
363 if (fmt->fmt.pix.height <= zr->jpg_settings.img_height / 2)
364 settings.ver_dcm = 2;
366 settings.ver_dcm = 1;
367 if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 4)
368 settings.hor_dcm = 4;
369 else if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 2)
370 settings.hor_dcm = 2;
372 settings.hor_dcm = 1;
373 if (settings.tmp_dcm == 1)
374 settings.field_per_buff = 2;
376 settings.field_per_buff = 1;
378 if (settings.hor_dcm > 1) {
379 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
380 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
383 settings.img_width = BUZ_MAX_WIDTH;
387 res = zoran_check_jpg_settings(zr, &settings, 1);
391 /* tell the user what we actually did */
392 fmt->fmt.pix.width = settings.img_width / settings.hor_dcm;
393 fmt->fmt.pix.height = settings.img_height * 2 /
394 (settings.tmp_dcm * settings.ver_dcm);
395 if (settings.tmp_dcm == 1)
396 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
397 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
399 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
400 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
402 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
403 zr->buffer_size = fmt->fmt.pix.sizeimage;
404 fmt->fmt.pix.bytesperline = 0;
405 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
409 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
410 struct v4l2_format *fmt)
412 struct zoran *zr = video_drvdata(file);
416 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
417 return zoran_try_fmt_vid_out(file, __fh, fmt);
419 for (i = 0; i < NUM_FORMATS; i++)
420 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
423 if (i == NUM_FORMATS) {
424 /* TODO do not return here to fix the TRY_FMT cannot handle an invalid pixelformat*/
428 fmt->fmt.pix.pixelformat = zoran_formats[i].fourcc;
429 fmt->fmt.pix.colorspace = zoran_formats[i].colorspace;
430 if (BUZ_MAX_HEIGHT < (fmt->fmt.pix.height * 2))
431 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
433 fmt->fmt.pix.field = V4L2_FIELD_TOP;
435 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
436 v4l_bound_align_image(&fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
437 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
441 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
442 struct v4l2_format *fmt)
444 struct zoran *zr = video_drvdata(file);
445 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
446 struct zoran_jpg_settings settings;
449 pci_dbg(zr->pci_dev, "size=%dx%d, fmt=0x%x (%4.4s)\n",
450 fmt->fmt.pix.width, fmt->fmt.pix.height,
451 fmt->fmt.pix.pixelformat,
452 (char *)&printformat);
453 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
456 if (!fmt->fmt.pix.height || !fmt->fmt.pix.width)
459 settings = zr->jpg_settings;
461 /* we actually need to set 'real' parameters now */
462 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
463 settings.tmp_dcm = 1;
465 settings.tmp_dcm = 2;
466 settings.decimation = 0;
467 if (fmt->fmt.pix.height <= zr->jpg_settings.img_height / 2)
468 settings.ver_dcm = 2;
470 settings.ver_dcm = 1;
471 if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 4)
472 settings.hor_dcm = 4;
473 else if (fmt->fmt.pix.width <= zr->jpg_settings.img_width / 2)
474 settings.hor_dcm = 2;
476 settings.hor_dcm = 1;
477 if (settings.tmp_dcm == 1)
478 settings.field_per_buff = 2;
480 settings.field_per_buff = 1;
482 if (settings.hor_dcm > 1) {
483 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
484 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
487 settings.img_width = BUZ_MAX_WIDTH;
491 res = zoran_check_jpg_settings(zr, &settings, 0);
495 /* it's ok, so set them */
496 zr->jpg_settings = settings;
498 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
499 zr->map_mode = ZORAN_MAP_MODE_JPG_REC;
501 zr->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
503 zr->buffer_size = zoran_v4l2_calc_bufsize(&zr->jpg_settings);
505 /* tell the user what we actually did */
506 fmt->fmt.pix.width = settings.img_width / settings.hor_dcm;
507 fmt->fmt.pix.height = settings.img_height * 2 /
508 (settings.tmp_dcm * settings.ver_dcm);
509 if (settings.tmp_dcm == 1)
510 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
511 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
513 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
514 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
515 fmt->fmt.pix.bytesperline = 0;
516 fmt->fmt.pix.sizeimage = zr->buffer_size;
517 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
521 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
522 struct v4l2_format *fmt)
524 struct zoran *zr = video_drvdata(file);
525 struct zoran_fh *fh = __fh;
529 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
530 return zoran_s_fmt_vid_out(file, fh, fmt);
532 for (i = 0; i < NUM_FORMATS; i++)
533 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
535 if (i == NUM_FORMATS) {
536 pci_err(zr->pci_dev, "VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
537 fmt->fmt.pix.pixelformat);
538 /* TODO do not return here to fix the TRY_FMT cannot handle an invalid pixelformat*/
542 fmt->fmt.pix.pixelformat = zoran_formats[i].fourcc;
543 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
544 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
545 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
546 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
547 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
548 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
549 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
550 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
552 zr->map_mode = ZORAN_MAP_MODE_RAW;
554 res = zoran_v4l_set_format(zr, fmt->fmt.pix.width, fmt->fmt.pix.height,
559 /* tell the user the results/missing stuff */
560 fmt->fmt.pix.bytesperline = zr->v4l_settings.bytesperline;
561 fmt->fmt.pix.sizeimage = zr->buffer_size;
562 fmt->fmt.pix.colorspace = zr->v4l_settings.format->colorspace;
563 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
564 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
566 fmt->fmt.pix.field = V4L2_FIELD_TOP;
570 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
572 struct zoran *zr = video_drvdata(file);
578 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id std)
580 struct zoran *zr = video_drvdata(file);
586 if (zr->running != ZORAN_MAP_MODE_NONE)
589 res = zoran_set_norm(zr, std);
593 static int zoran_enum_input(struct file *file, void *__fh,
594 struct v4l2_input *inp)
596 struct zoran *zr = video_drvdata(file);
598 if (inp->index >= zr->card.inputs)
601 strscpy(inp->name, zr->card.input[inp->index].name, sizeof(inp->name));
602 inp->type = V4L2_INPUT_TYPE_CAMERA;
603 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
605 /* Get status of video decoder */
606 decoder_call(zr, video, g_input_status, &inp->status);
610 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
612 struct zoran *zr = video_drvdata(file);
619 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
621 struct zoran *zr = video_drvdata(file);
624 if (zr->running != ZORAN_MAP_MODE_NONE)
627 res = zoran_set_input(zr, input);
632 /* TODO: output does not work yet */
633 static int zoran_enum_output(struct file *file, void *__fh,
634 struct v4l2_output *outp)
636 if (outp->index != 0)
640 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
641 outp->std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
642 outp->capabilities = V4L2_OUT_CAP_STD;
643 strscpy(outp->name, "Autodetect", sizeof(outp->name));
647 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
654 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
663 /* cropping (sub-frame capture) */
664 static int zoran_g_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
666 struct zoran *zr = video_drvdata(file);
668 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
669 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
670 pci_err(zr->pci_dev, "%s invalid combinaison\n", __func__);
674 switch (sel->target) {
675 case V4L2_SEL_TGT_CROP:
676 sel->r.top = zr->jpg_settings.img_y;
677 sel->r.left = zr->jpg_settings.img_x;
678 sel->r.width = zr->jpg_settings.img_width;
679 sel->r.height = zr->jpg_settings.img_height;
681 case V4L2_SEL_TGT_CROP_DEFAULT:
682 sel->r.top = sel->r.left = 0;
683 sel->r.width = BUZ_MIN_WIDTH;
684 sel->r.height = BUZ_MIN_HEIGHT;
686 case V4L2_SEL_TGT_CROP_BOUNDS:
687 sel->r.top = sel->r.left = 0;
688 sel->r.width = BUZ_MAX_WIDTH;
689 sel->r.height = BUZ_MAX_HEIGHT;
697 static int zoran_s_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
699 struct zoran *zr = video_drvdata(file);
700 struct zoran_jpg_settings settings;
703 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
704 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
707 if (!sel->r.width || !sel->r.height)
710 if (sel->target != V4L2_SEL_TGT_CROP)
713 if (zr->map_mode == ZORAN_MAP_MODE_RAW) {
714 pci_err(zr->pci_dev, "VIDIOC_S_SELECTION - subcapture only supported for compressed capture\n");
718 settings = zr->jpg_settings;
720 /* move into a form that we understand */
721 settings.img_x = sel->r.left;
722 settings.img_y = sel->r.top;
723 settings.img_width = sel->r.width;
724 settings.img_height = sel->r.height;
727 res = zoran_check_jpg_settings(zr, &settings, 0);
732 zr->jpg_settings = settings;
736 static int zoran_g_parm(struct file *file, void *priv, struct v4l2_streamparm *parm)
738 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
741 parm->parm.capture.readbuffers = 9;
746 * Output is disabled temporarily
747 * Zoran is picky about jpeg data it accepts. At least it seems to unsupport COM and APPn.
748 * So until a way to filter data will be done, disable output.
750 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
751 .vidioc_querycap = zoran_querycap,
752 .vidioc_g_parm = zoran_g_parm,
753 .vidioc_s_selection = zoran_s_selection,
754 .vidioc_g_selection = zoran_g_selection,
755 .vidioc_enum_input = zoran_enum_input,
756 .vidioc_g_input = zoran_g_input,
757 .vidioc_s_input = zoran_s_input,
758 /* .vidioc_enum_output = zoran_enum_output,
759 .vidioc_g_output = zoran_g_output,
760 .vidioc_s_output = zoran_s_output,*/
761 .vidioc_g_std = zoran_g_std,
762 .vidioc_s_std = zoran_s_std,
763 .vidioc_create_bufs = vb2_ioctl_create_bufs,
764 .vidioc_reqbufs = vb2_ioctl_reqbufs,
765 .vidioc_querybuf = vb2_ioctl_querybuf,
766 .vidioc_qbuf = vb2_ioctl_qbuf,
767 .vidioc_dqbuf = vb2_ioctl_dqbuf,
768 .vidioc_expbuf = vb2_ioctl_expbuf,
769 .vidioc_streamon = vb2_ioctl_streamon,
770 .vidioc_streamoff = vb2_ioctl_streamoff,
771 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
772 /* .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,*/
773 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
774 /* .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,*/
775 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
776 /* .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,*/
777 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
778 /* .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,*/
779 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
780 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
783 static const struct v4l2_file_operations zoran_fops = {
784 .owner = THIS_MODULE,
785 .unlocked_ioctl = video_ioctl2,
786 .open = v4l2_fh_open,
787 .release = vb2_fop_release,
788 .read = vb2_fop_read,
789 .write = vb2_fop_write,
790 .mmap = vb2_fop_mmap,
791 .poll = vb2_fop_poll,
794 const struct video_device zoran_template = {
797 .ioctl_ops = &zoran_ioctl_ops,
798 .release = &zoran_vdev_release,
799 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
802 static int zr_vb2_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes,
803 unsigned int sizes[], struct device *alloc_devs[])
805 struct zoran *zr = vb2_get_drv_priv(vq);
806 unsigned int size = zr->buffer_size;
808 pci_dbg(zr->pci_dev, "%s nbuf=%u nplanes=%u", __func__, *nbuffers, *nplanes);
810 zr->buf_in_reserve = 0;
812 if (*nbuffers < vq->min_buffers_needed)
813 *nbuffers = vq->min_buffers_needed;
828 static void zr_vb2_queue(struct vb2_buffer *vb)
830 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
831 struct zr_buffer *buf = vb2_to_zr_buffer(vb);
834 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
835 list_add_tail(&buf->queue, &zr->queued_bufs);
836 zr->buf_in_reserve++;
837 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
838 if (zr->running == ZORAN_MAP_MODE_JPG_REC)
839 zoran_feed_stat_com(zr);
843 static int zr_vb2_prepare(struct vb2_buffer *vb)
845 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
847 if (vb2_plane_size(vb, 0) < zr->buffer_size)
854 int zr_set_buf(struct zoran *zr)
856 struct zr_buffer *buf;
857 struct vb2_v4l2_buffer *vbuf;
858 dma_addr_t phys_addr;
862 if (zr->running == ZORAN_MAP_MODE_NONE)
867 buf->vbuf.vb2_buf.timestamp = ktime_get_ns();
868 buf->vbuf.sequence = zr->vbseq++;
871 buf->vbuf.field = V4L2_FIELD_INTERLACED;
872 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
873 buf->vbuf.field = V4L2_FIELD_INTERLACED;
875 buf->vbuf.field = V4L2_FIELD_TOP;
876 vb2_set_plane_payload(&buf->vbuf.vb2_buf, 0, zr->buffer_size);
877 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
881 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
882 if (list_empty(&zr->queued_bufs)) {
883 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
884 vb2_queue_error(zr->video_dev->queue);
885 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
888 buf = list_first_entry_or_null(&zr->queued_bufs, struct zr_buffer, queue);
890 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
891 vb2_queue_error(zr->video_dev->queue);
892 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
895 list_del(&buf->queue);
896 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
899 vbuf->vb2_buf.state = VB2_BUF_STATE_ACTIVE;
900 phys_addr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
908 btwrite(reg, ZR36057_VDTR);
909 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
910 reg += zr->v4l_settings.bytesperline;
911 btwrite(reg, ZR36057_VDBR);
914 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
915 reg += zr->v4l_settings.bytesperline;
916 reg = (reg << ZR36057_VSSFGR_DISP_STRIDE);
917 reg |= ZR36057_VSSFGR_VID_OVF;
918 reg |= ZR36057_VSSFGR_SNAP_SHOT;
919 reg |= ZR36057_VSSFGR_FRAME_GRAB;
920 btwrite(reg, ZR36057_VSSFGR);
922 btor(ZR36057_VDCR_VID_EN, ZR36057_VDCR);
926 static int zr_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
928 struct zoran *zr = vq->drv_priv;
931 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
932 zr->stat_com[j] = cpu_to_le32(1);
937 if (zr->map_mode != ZORAN_MAP_MODE_RAW) {
938 pci_info(zr->pci_dev, "START JPG\n");
940 zoran_init_hardware(zr);
941 if (zr->map_mode == ZORAN_MAP_MODE_JPG_REC)
942 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_DECOMPRESS);
944 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_COMPRESS);
945 zoran_feed_stat_com(zr);
947 zr->running = zr->map_mode;
948 btor(ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
952 pci_info(zr->pci_dev, "START RAW\n");
954 zoran_init_hardware(zr);
956 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
957 zr36057_set_memgrab(zr, 1);
958 zr->running = zr->map_mode;
959 btor(ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
963 static void zr_vb2_stop_streaming(struct vb2_queue *vq)
965 struct zoran *zr = vq->drv_priv;
966 struct zr_buffer *buf;
970 btand(~ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
971 if (zr->map_mode != ZORAN_MAP_MODE_RAW)
972 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
973 zr36057_set_memgrab(zr, 0);
974 zr->running = ZORAN_MAP_MODE_NONE;
976 zoran_set_pci_master(zr, 0);
978 if (!pass_through) { /* Switch to color bar */
979 decoder_call(zr, video, s_stream, 0);
980 encoder_call(zr, video, s_routing, 2, 0, 0);
983 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
984 zr->stat_com[j] = cpu_to_le32(1);
988 pci_dbg(zr->pci_dev, "%s clean buf %d\n", __func__, j);
989 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
993 spin_lock_irqsave(&zr->queued_bufs_lock, flags);
994 while (!list_empty(&zr->queued_bufs)) {
995 buf = list_entry(zr->queued_bufs.next, struct zr_buffer, queue);
996 list_del(&buf->queue);
997 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
998 zr->buf_in_reserve--;
1000 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
1001 if (zr->buf_in_reserve)
1002 pci_err(zr->pci_dev, "Buffer remaining %d\n", zr->buf_in_reserve);
1003 zr->map_mode = ZORAN_MAP_MODE_RAW;
1006 static const struct vb2_ops zr_video_qops = {
1007 .queue_setup = zr_vb2_queue_setup,
1008 .buf_queue = zr_vb2_queue,
1009 .buf_prepare = zr_vb2_prepare,
1010 .start_streaming = zr_vb2_start_streaming,
1011 .stop_streaming = zr_vb2_stop_streaming,
1012 .wait_prepare = vb2_ops_wait_prepare,
1013 .wait_finish = vb2_ops_wait_finish,
1016 int zoran_queue_init(struct zoran *zr, struct vb2_queue *vq, int dir)
1020 spin_lock_init(&zr->queued_bufs_lock);
1021 INIT_LIST_HEAD(&zr->queued_bufs);
1023 vq->dev = &zr->pci_dev->dev;
1026 vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ | VB2_WRITE;
1028 vq->buf_struct_size = sizeof(struct zr_buffer);
1029 vq->ops = &zr_video_qops;
1030 vq->mem_ops = &vb2_dma_contig_memops;
1031 vq->gfp_flags = GFP_DMA32,
1032 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1033 vq->min_buffers_needed = 9;
1034 vq->lock = &zr->lock;
1035 err = vb2_queue_init(vq);
1038 zr->video_dev->queue = vq;
1042 void zoran_queue_exit(struct zoran *zr)
1044 vb2_queue_release(zr->video_dev->queue);