1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zoran zr36057/zr36067 PCI controller driver, for the
4 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
5 * Media Labs LML33/LML33R10.
6 *
7 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 *
9 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 *
11 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 *
13 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 *
15 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
16 *
17 * Based on
18 *
19 * Miro DC10 driver
20 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 *
22 * Iomega Buz driver version 1.0
23 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
24 *
25 * buz.0.0.3
26 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 *
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)
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
60 const 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 * small helper function for calculating buffersizes for v4l2
141 * we calculate the nearest higher power-of-two, which
142 * will be the recommended buffersize
143 */
zoran_v4l2_calc_bufsize(struct zoran_jpg_settings * settings)144 static __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 * V4L Buffer grabbing
166 */
zoran_v4l_set_format(struct zoran * zr,int width,int height,const struct zoran_format * format)167 static int zoran_v4l_set_format(struct zoran *zr, int width, int height,
168 const struct zoran_format *format)
169 {
170 int bpp;
171
172 /* Check size and format of the grab wanted */
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 /* 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);
188 return -EINVAL;
189 }
190
191 /* The video front end needs 4-byte alinged line sizes */
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
zoran_set_norm(struct zoran * zr,v4l2_std_id norm)206 static 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 /* Make sure the changes come into effect */
225 zr->norm = norm;
226
227 return 0;
228 }
229
zoran_set_input(struct zoran * zr,int input)230 static 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 * ioctl routine
249 */
250
zoran_querycap(struct file * file,void * __fh,struct v4l2_capability * cap)251 static 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 return 0;
259 }
260
zoran_enum_fmt(struct zoran * zr,struct v4l2_fmtdesc * fmt,int flag)261 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
262 {
263 unsigned int num, i;
264
265 if (fmt->index >= ARRAY_SIZE(zoran_formats))
266 return -EINVAL;
267 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
268 return -EINVAL;
269
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;
278 return 0;
279 }
280 }
281 return -EINVAL;
282 }
283
zoran_enum_fmt_vid_cap(struct file * file,void * __fh,struct v4l2_fmtdesc * f)284 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
285 struct v4l2_fmtdesc *f)
286 {
287 struct zoran *zr = video_drvdata(file);
288
289 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
290 }
291
292 #if 0
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)
296 {
297 struct zoran *zr = video_drvdata(file);
298
299 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
300 }
301 #endif
302
zoran_g_fmt_vid_out(struct file * file,void * __fh,struct v4l2_format * fmt)303 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
304 struct v4l2_format *fmt)
305 {
306 struct zoran *zr = video_drvdata(file);
307
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);
316 else
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;
321
322 return 0;
323 }
324
zoran_g_fmt_vid_cap(struct file * file,void * __fh,struct v4l2_format * fmt)325 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
326 struct v4l2_format *fmt)
327 {
328 struct zoran *zr = video_drvdata(file);
329
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;
340 else
341 fmt->fmt.pix.field = V4L2_FIELD_TOP;
342 return 0;
343 }
344
zoran_try_fmt_vid_out(struct file * file,void * __fh,struct v4l2_format * fmt)345 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
346 struct v4l2_format *fmt)
347 {
348 struct zoran *zr = video_drvdata(file);
349 struct zoran_jpg_settings settings;
350 int res = 0;
351
352 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
353 return -EINVAL;
354
355 settings = zr->jpg_settings;
356
357 /* we actually need to set 'real' parameters now */
358 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
359 settings.tmp_dcm = 1;
360 else
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;
365 else
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;
371 else
372 settings.hor_dcm = 1;
373 if (settings.tmp_dcm == 1)
374 settings.field_per_buff = 2;
375 else
376 settings.field_per_buff = 1;
377
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;
381 } else {
382 settings.img_x = 0;
383 settings.img_width = BUZ_MAX_WIDTH;
384 }
385
386 /* check */
387 res = zoran_check_jpg_settings(zr, &settings, 1);
388 if (res)
389 return res;
390
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);
398 else
399 fmt->fmt.pix.field = (zr->jpg_settings.odd_even ?
400 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
401
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;
406 return res;
407 }
408
zoran_try_fmt_vid_cap(struct file * file,void * __fh,struct v4l2_format * fmt)409 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
410 struct v4l2_format *fmt)
411 {
412 struct zoran *zr = video_drvdata(file);
413 int bpp;
414 int i;
415
416 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
417 return zoran_try_fmt_vid_out(file, __fh, fmt);
418
419 for (i = 0; i < NUM_FORMATS; i++)
420 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
421 break;
422
423 if (i == NUM_FORMATS) {
424 /* TODO do not return here to fix the TRY_FMT cannot handle an invalid pixelformat*/
425 return -EINVAL;
426 }
427
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;
432 else
433 fmt->fmt.pix.field = V4L2_FIELD_TOP;
434
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);
438 return 0;
439 }
440
zoran_s_fmt_vid_out(struct file * file,void * __fh,struct v4l2_format * fmt)441 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
442 struct v4l2_format *fmt)
443 {
444 struct zoran *zr = video_drvdata(file);
445 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
446 struct zoran_jpg_settings settings;
447 int res = 0;
448
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)
454 return -EINVAL;
455
456 if (!fmt->fmt.pix.height || !fmt->fmt.pix.width)
457 return -EINVAL;
458
459 settings = zr->jpg_settings;
460
461 /* we actually need to set 'real' parameters now */
462 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
463 settings.tmp_dcm = 1;
464 else
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;
469 else
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;
475 else
476 settings.hor_dcm = 1;
477 if (settings.tmp_dcm == 1)
478 settings.field_per_buff = 2;
479 else
480 settings.field_per_buff = 1;
481
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;
485 } else {
486 settings.img_x = 0;
487 settings.img_width = BUZ_MAX_WIDTH;
488 }
489
490 /* check */
491 res = zoran_check_jpg_settings(zr, &settings, 0);
492 if (res)
493 return res;
494
495 /* it's ok, so set them */
496 zr->jpg_settings = settings;
497
498 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
499 zr->map_mode = ZORAN_MAP_MODE_JPG_REC;
500 else
501 zr->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
502
503 zr->buffer_size = zoran_v4l2_calc_bufsize(&zr->jpg_settings);
504
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);
512 else
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;
518 return res;
519 }
520
zoran_s_fmt_vid_cap(struct file * file,void * __fh,struct v4l2_format * fmt)521 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
522 struct v4l2_format *fmt)
523 {
524 struct zoran *zr = video_drvdata(file);
525 struct zoran_fh *fh = __fh;
526 int i;
527 int res = 0;
528
529 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
530 return zoran_s_fmt_vid_out(file, fh, fmt);
531
532 for (i = 0; i < NUM_FORMATS; i++)
533 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
534 break;
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*/
539 return -EINVAL;
540 }
541
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;
551
552 zr->map_mode = ZORAN_MAP_MODE_RAW;
553
554 res = zoran_v4l_set_format(zr, fmt->fmt.pix.width, fmt->fmt.pix.height,
555 &zoran_formats[i]);
556 if (res)
557 return res;
558
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;
565 else
566 fmt->fmt.pix.field = V4L2_FIELD_TOP;
567 return res;
568 }
569
zoran_g_std(struct file * file,void * __fh,v4l2_std_id * std)570 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
571 {
572 struct zoran *zr = video_drvdata(file);
573
574 *std = zr->norm;
575 return 0;
576 }
577
zoran_s_std(struct file * file,void * __fh,v4l2_std_id std)578 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id std)
579 {
580 struct zoran *zr = video_drvdata(file);
581 int res = 0;
582
583 if (zr->norm == std)
584 return 0;
585
586 if (zr->running != ZORAN_MAP_MODE_NONE)
587 return -EBUSY;
588
589 res = zoran_set_norm(zr, std);
590 return res;
591 }
592
zoran_enum_input(struct file * file,void * __fh,struct v4l2_input * inp)593 static int zoran_enum_input(struct file *file, void *__fh,
594 struct v4l2_input *inp)
595 {
596 struct zoran *zr = video_drvdata(file);
597
598 if (inp->index >= zr->card.inputs)
599 return -EINVAL;
600
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;
604
605 /* Get status of video decoder */
606 decoder_call(zr, video, g_input_status, &inp->status);
607 return 0;
608 }
609
zoran_g_input(struct file * file,void * __fh,unsigned int * input)610 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
611 {
612 struct zoran *zr = video_drvdata(file);
613
614 *input = zr->input;
615
616 return 0;
617 }
618
zoran_s_input(struct file * file,void * __fh,unsigned int input)619 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
620 {
621 struct zoran *zr = video_drvdata(file);
622 int res;
623
624 if (zr->running != ZORAN_MAP_MODE_NONE)
625 return -EBUSY;
626
627 res = zoran_set_input(zr, input);
628 return res;
629 }
630
631 #if 0
632 /* TODO: output does not work yet */
633 static int zoran_enum_output(struct file *file, void *__fh,
634 struct v4l2_output *outp)
635 {
636 if (outp->index != 0)
637 return -EINVAL;
638
639 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));
644
645 return 0;
646 }
647 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
648 {
649 *output = 0;
650
651 return 0;
652 }
653
654 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
655 {
656 if (output != 0)
657 return -EINVAL;
658
659 return 0;
660 }
661 #endif
662
663 /* cropping (sub-frame capture) */
zoran_g_selection(struct file * file,void * __fh,struct v4l2_selection * sel)664 static int zoran_g_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
665 {
666 struct zoran *zr = video_drvdata(file);
667
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__);
671 return -EINVAL;
672 }
673
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;
680 break;
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;
685 break;
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;
690 break;
691 default:
692 return -EINVAL;
693 }
694 return 0;
695 }
696
zoran_s_selection(struct file * file,void * __fh,struct v4l2_selection * sel)697 static int zoran_s_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
698 {
699 struct zoran *zr = video_drvdata(file);
700 struct zoran_jpg_settings settings;
701 int res;
702
703 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
704 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
705 return -EINVAL;
706
707 if (!sel->r.width || !sel->r.height)
708 return -EINVAL;
709
710 if (sel->target != V4L2_SEL_TGT_CROP)
711 return -EINVAL;
712
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");
715 return -EINVAL;
716 }
717
718 settings = zr->jpg_settings;
719
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;
725
726 /* check validity */
727 res = zoran_check_jpg_settings(zr, &settings, 0);
728 if (res)
729 return res;
730
731 /* accept */
732 zr->jpg_settings = settings;
733 return res;
734 }
735
zoran_g_parm(struct file * file,void * priv,struct v4l2_streamparm * parm)736 static int zoran_g_parm(struct file *file, void *priv, struct v4l2_streamparm *parm)
737 {
738 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
739 return -EINVAL;
740
741 parm->parm.capture.readbuffers = 9;
742 return 0;
743 }
744
745 /*
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.
749 */
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,
781 };
782
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,
792 };
793
794 const struct video_device zoran_template = {
795 .name = ZORAN_NAME,
796 .fops = &zoran_fops,
797 .ioctl_ops = &zoran_ioctl_ops,
798 .release = &zoran_vdev_release,
799 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
800 };
801
zr_vb2_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])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[])
804 {
805 struct zoran *zr = vb2_get_drv_priv(vq);
806 unsigned int size = zr->buffer_size;
807
808 pci_dbg(zr->pci_dev, "%s nbuf=%u nplanes=%u", __func__, *nbuffers, *nplanes);
809
810 zr->buf_in_reserve = 0;
811
812 if (*nbuffers < vq->min_buffers_needed)
813 *nbuffers = vq->min_buffers_needed;
814
815 if (*nplanes) {
816 if (sizes[0] < size)
817 return -EINVAL;
818 else
819 return 0;
820 }
821
822 *nplanes = 1;
823 sizes[0] = size;
824
825 return 0;
826 }
827
zr_vb2_queue(struct vb2_buffer * vb)828 static void zr_vb2_queue(struct vb2_buffer *vb)
829 {
830 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
831 struct zr_buffer *buf = vb2_to_zr_buffer(vb);
832 unsigned long flags;
833
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);
840 zr->queued++;
841 }
842
zr_vb2_prepare(struct vb2_buffer * vb)843 static int zr_vb2_prepare(struct vb2_buffer *vb)
844 {
845 struct zoran *zr = vb2_get_drv_priv(vb->vb2_queue);
846
847 if (vb2_plane_size(vb, 0) < zr->buffer_size)
848 return -EINVAL;
849 zr->prepared++;
850
851 return 0;
852 }
853
zr_set_buf(struct zoran * zr)854 int zr_set_buf(struct zoran *zr)
855 {
856 struct zr_buffer *buf;
857 struct vb2_v4l2_buffer *vbuf;
858 dma_addr_t phys_addr;
859 unsigned long flags;
860 u32 reg;
861
862 if (zr->running == ZORAN_MAP_MODE_NONE)
863 return 0;
864
865 if (zr->inuse[0]) {
866 buf = zr->inuse[0];
867 buf->vbuf.vb2_buf.timestamp = ktime_get_ns();
868 buf->vbuf.sequence = zr->vbseq++;
869 vbuf = &buf->vbuf;
870
871 buf->vbuf.field = V4L2_FIELD_INTERLACED;
872 if (BUZ_MAX_HEIGHT < (zr->v4l_settings.height * 2))
873 buf->vbuf.field = V4L2_FIELD_INTERLACED;
874 else
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);
878 zr->inuse[0] = NULL;
879 }
880
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);
886 return -EINVAL;
887 }
888 buf = list_first_entry_or_null(&zr->queued_bufs, struct zr_buffer, queue);
889 if (!buf) {
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);
893 return -EINVAL;
894 }
895 list_del(&buf->queue);
896 spin_unlock_irqrestore(&zr->queued_bufs_lock, flags);
897
898 vbuf = &buf->vbuf;
899 vbuf->vb2_buf.state = VB2_BUF_STATE_ACTIVE;
900 phys_addr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
901
902 if (!phys_addr)
903 return -EINVAL;
904
905 zr->inuse[0] = buf;
906
907 reg = phys_addr;
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);
912
913 reg = 0;
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);
921
922 btor(ZR36057_VDCR_VID_EN, ZR36057_VDCR);
923 return 0;
924 }
925
zr_vb2_start_streaming(struct vb2_queue * vq,unsigned int count)926 static int zr_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
927 {
928 struct zoran *zr = vq->drv_priv;
929 int j;
930
931 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
932 zr->stat_com[j] = cpu_to_le32(1);
933 zr->inuse[j] = NULL;
934 }
935 zr->vbseq = 0;
936
937 if (zr->map_mode != ZORAN_MAP_MODE_RAW) {
938 pci_info(zr->pci_dev, "START JPG\n");
939 zr36057_restart(zr);
940 zoran_init_hardware(zr);
941 if (zr->map_mode == ZORAN_MAP_MODE_JPG_REC)
942 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_DECOMPRESS);
943 else
944 zr36057_enable_jpg(zr, BUZ_MODE_MOTION_COMPRESS);
945 zoran_feed_stat_com(zr);
946 jpeg_start(zr);
947 zr->running = zr->map_mode;
948 btor(ZR36057_ICR_INT_PIN_EN, ZR36057_ICR);
949 return 0;
950 }
951
952 pci_info(zr->pci_dev, "START RAW\n");
953 zr36057_restart(zr);
954 zoran_init_hardware(zr);
955
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);
960 return 0;
961 }
962
zr_vb2_stop_streaming(struct vb2_queue * vq)963 static void zr_vb2_stop_streaming(struct vb2_queue *vq)
964 {
965 struct zoran *zr = vq->drv_priv;
966 struct zr_buffer *buf;
967 unsigned long flags;
968 int j;
969
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;
975
976 zoran_set_pci_master(zr, 0);
977
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);
981 }
982
983 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
984 zr->stat_com[j] = cpu_to_le32(1);
985 if (!zr->inuse[j])
986 continue;
987 buf = zr->inuse[j];
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);
990 zr->inuse[j] = NULL;
991 }
992
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--;
999 }
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;
1004 }
1005
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,
1014 };
1015
zoran_queue_init(struct zoran * zr,struct vb2_queue * vq,int dir)1016 int zoran_queue_init(struct zoran *zr, struct vb2_queue *vq, int dir)
1017 {
1018 int err;
1019
1020 spin_lock_init(&zr->queued_bufs_lock);
1021 INIT_LIST_HEAD(&zr->queued_bufs);
1022
1023 vq->dev = &zr->pci_dev->dev;
1024 vq->type = dir;
1025
1026 vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ | VB2_WRITE;
1027 vq->drv_priv = zr;
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);
1036 if (err)
1037 return err;
1038 zr->video_dev->queue = vq;
1039 return 0;
1040 }
1041
zoran_queue_exit(struct zoran * zr)1042 void zoran_queue_exit(struct zoran *zr)
1043 {
1044 vb2_queue_release(zr->video_dev->queue);
1045 }
1046