vino.c 98.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
R
Ralf Baechle 已提交
2
 * Driver for the VINO (Video In No Out) system found in SGI Indys.
L
Linus Torvalds 已提交
3 4 5 6
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
R
Ralf Baechle 已提交
7 8 9
 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
 *
 * Based on the previous version of the driver for 2.4 kernels by:
L
Linus Torvalds 已提交
10
 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
11 12 13 14 15 16
 *
 * v4l2_device/v4l2_subdev conversion by:
 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
 *
 * Note: this conversion is untested! Please contact the linux-media
 * mailinglist if you can test this, together with the test results.
L
Linus Torvalds 已提交
17 18
 */

R
Ralf Baechle 已提交
19 20
/*
 * TODO:
L
Ladislav Michl 已提交
21
 * - remove "mark pages reserved-hacks" from memory allocation code
N
Nick Piggin 已提交
22
 *   and implement fault()
R
Ralf Baechle 已提交
23 24
 * - check decimation, calculating and reporting image size when
 *   using decimation
L
Ladislav Michl 已提交
25
 * - implement read(), user mode buffers and overlay (?)
R
Ralf Baechle 已提交
26 27
 */

L
Linus Torvalds 已提交
28
#include <linux/init.h>
R
Ralf Baechle 已提交
29
#include <linux/module.h>
L
Linus Torvalds 已提交
30
#include <linux/delay.h>
31
#include <linux/dma-mapping.h>
R
Ralf Baechle 已提交
32 33
#include <linux/errno.h>
#include <linux/fs.h>
34
#include <linux/interrupt.h>
R
Ralf Baechle 已提交
35
#include <linux/kernel.h>
36
#include <linux/slab.h>
R
Ralf Baechle 已提交
37
#include <linux/mm.h>
38
#include <linux/time.h>
R
Ralf Baechle 已提交
39 40
#include <linux/kmod.h>

L
Linus Torvalds 已提交
41 42
#include <linux/i2c.h>

43
#include <linux/videodev2.h>
44
#include <media/v4l2-device.h>
45
#include <media/v4l2-ioctl.h>
46
#include <linux/mutex.h>
R
Ralf Baechle 已提交
47

L
Linus Torvalds 已提交
48 49 50 51 52 53
#include <asm/paccess.h>
#include <asm/io.h>
#include <asm/sgi/ip22.h>
#include <asm/sgi/mc.h>

#include "vino.h"
R
Ralf Baechle 已提交
54 55
#include "saa7191.h"
#include "indycam.h"
L
Linus Torvalds 已提交
56

R
Ralf Baechle 已提交
57 58 59 60
/* Uncomment the following line to get lots and lots of (mostly useless)
 * debug info.
 * Note that the debug output also slows down the driver significantly */
// #define VINO_DEBUG
L
Ladislav Michl 已提交
61
// #define VINO_DEBUG_INT
R
Ralf Baechle 已提交
62

63
#define VINO_MODULE_VERSION "0.0.7"
R
Ralf Baechle 已提交
64 65 66 67 68 69 70 71

MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
MODULE_VERSION(VINO_MODULE_VERSION);
MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
MODULE_LICENSE("GPL");

#ifdef VINO_DEBUG
#define dprintk(x...) printk("VINO: " x);
L
Linus Torvalds 已提交
72
#else
R
Ralf Baechle 已提交
73
#define dprintk(x...)
L
Linus Torvalds 已提交
74 75
#endif

R
Ralf Baechle 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
#define VINO_NO_CHANNEL			0
#define VINO_CHANNEL_A			1
#define VINO_CHANNEL_B			2

#define VINO_PAL_WIDTH			768
#define VINO_PAL_HEIGHT			576
#define VINO_NTSC_WIDTH			640
#define VINO_NTSC_HEIGHT		480

#define VINO_MIN_WIDTH			32
#define VINO_MIN_HEIGHT			32

#define VINO_CLIPPING_START_ODD_D1	1
L
Ladislav Michl 已提交
89 90
#define VINO_CLIPPING_START_ODD_PAL	15
#define VINO_CLIPPING_START_ODD_NTSC	12
R
Ralf Baechle 已提交
91 92

#define VINO_CLIPPING_START_EVEN_D1	2
L
Ladislav Michl 已提交
93 94
#define VINO_CLIPPING_START_EVEN_PAL	15
#define VINO_CLIPPING_START_EVEN_NTSC	12
R
Ralf Baechle 已提交
95 96 97

#define VINO_INPUT_CHANNEL_COUNT	3

L
Ladislav Michl 已提交
98
/* the number is the index for vino_inputs */
R
Ralf Baechle 已提交
99 100 101 102 103 104 105
#define VINO_INPUT_NONE			-1
#define VINO_INPUT_COMPOSITE		0
#define VINO_INPUT_SVIDEO		1
#define VINO_INPUT_D1			2

#define VINO_PAGE_RATIO			(PAGE_SIZE / VINO_PAGE_SIZE)

L
Ladislav Michl 已提交
106
#define VINO_FIFO_THRESHOLD_DEFAULT	16
R
Ralf Baechle 已提交
107 108 109 110 111

#define VINO_FRAMEBUFFER_SIZE		((VINO_PAL_WIDTH \
					  * VINO_PAL_HEIGHT * 4 \
					  + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))

L
Ladislav Michl 已提交
112
#define VINO_FRAMEBUFFER_COUNT_MAX	8
R
Ralf Baechle 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

#define VINO_FRAMEBUFFER_UNUSED		0
#define VINO_FRAMEBUFFER_IN_USE		1
#define VINO_FRAMEBUFFER_READY		2

#define VINO_QUEUE_ERROR		-1
#define VINO_QUEUE_MAGIC		0x20050125

#define VINO_MEMORY_NONE		0
#define VINO_MEMORY_MMAP		1
#define VINO_MEMORY_USERPTR		2

#define VINO_DUMMY_DESC_COUNT		4
#define VINO_DESC_FETCH_DELAY		5	/* microseconds */

L
Ladislav Michl 已提交
128 129
#define VINO_MAX_FRAME_SKIP_COUNT	128

R
Ralf Baechle 已提交
130 131 132 133 134 135 136 137 138
/* the number is the index for vino_data_formats */
#define VINO_DATA_FMT_NONE		-1
#define VINO_DATA_FMT_GREY		0
#define VINO_DATA_FMT_RGB332		1
#define VINO_DATA_FMT_RGB32		2
#define VINO_DATA_FMT_YUV		3

#define VINO_DATA_FMT_COUNT		4

L
Ladislav Michl 已提交
139
/* the number is the index for vino_data_norms */
R
Ralf Baechle 已提交
140 141 142 143 144 145 146 147
#define VINO_DATA_NORM_NONE		-1
#define VINO_DATA_NORM_NTSC		0
#define VINO_DATA_NORM_PAL		1
#define VINO_DATA_NORM_SECAM		2
#define VINO_DATA_NORM_D1		3

#define VINO_DATA_NORM_COUNT		4

148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* I2C controller flags */
#define SGI_I2C_FORCE_IDLE		(0 << 0)
#define SGI_I2C_NOT_IDLE		(1 << 0)
#define SGI_I2C_WRITE			(0 << 1)
#define SGI_I2C_READ			(1 << 1)
#define SGI_I2C_RELEASE_BUS		(0 << 2)
#define SGI_I2C_HOLD_BUS		(1 << 2)
#define SGI_I2C_XFER_DONE		(0 << 4)
#define SGI_I2C_XFER_BUSY		(1 << 4)
#define SGI_I2C_ACK			(0 << 5)
#define SGI_I2C_NACK			(1 << 5)
#define SGI_I2C_BUS_OK			(0 << 7)
#define SGI_I2C_BUS_ERR			(1 << 7)

R
Ralf Baechle 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/* Internal data structure definitions */

struct vino_input {
	char *name;
	v4l2_std_id std;
};

struct vino_clipping {
	unsigned int left, right, top, bottom;
};

struct vino_data_format {
	/* the description */
	char *description;
	/* bytes per pixel */
	unsigned int bpp;
	/* V4L2 fourcc code */
	__u32 pixelformat;
	/* V4L2 colorspace (duh!) */
	enum v4l2_colorspace colorspace;
};

struct vino_data_norm {
	char *description;
	unsigned int width, height;
	struct vino_clipping odd;
	struct vino_clipping even;

	v4l2_std_id std;
	unsigned int fps_min, fps_max;
	__u32 framelines;
};

struct vino_descriptor_table {
	/* the number of PAGE_SIZE sized pages in the buffer */
	unsigned int page_count;
	/* virtual (kmalloc'd) pointers to the actual data
	 * (in PAGE_SIZE chunks, used with mmap streaming) */
	unsigned long *virtual;

	/* cpu address for the VINO descriptor table
	 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
	unsigned long *dma_cpu;
	/* dma address for the VINO descriptor table
	 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
	dma_addr_t dma;
};

struct vino_framebuffer {
	/* identifier nubmer */
	unsigned int id;
	/* the length of the whole buffer */
	unsigned int size;
	/* the length of actual data in buffer */
	unsigned int data_size;
	/* the data format */
	unsigned int data_format;
	/* the state of buffer data */
	unsigned int state;
	/* is the buffer mapped in user space? */
	unsigned int map_count;
	/* memory offset for mmap() */
	unsigned int offset;
	/* frame counter */
	unsigned int frame_counter;
	/* timestamp (written when image capture finishes) */
	struct timeval timestamp;

	struct vino_descriptor_table desc_table;

	spinlock_t state_lock;
};

struct vino_framebuffer_fifo {
	unsigned int length;

	unsigned int used;
	unsigned int head;
	unsigned int tail;

L
Ladislav Michl 已提交
242
	unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
R
Ralf Baechle 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
};

struct vino_framebuffer_queue {
	unsigned int magic;

	/* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
	unsigned int type;
	unsigned int length;

	/* data field of in and out contain index numbers for buffer */
	struct vino_framebuffer_fifo in;
	struct vino_framebuffer_fifo out;

L
Ladislav Michl 已提交
256
	struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
L
Linus Torvalds 已提交
257

R
Ralf Baechle 已提交
258
	spinlock_t queue_lock;
259
	struct mutex queue_mutex;
R
Ralf Baechle 已提交
260 261 262
	wait_queue_head_t frame_wait_queue;
};

L
Ladislav Michl 已提交
263 264 265 266 267 268 269
struct vino_interrupt_data {
	struct timeval timestamp;
	unsigned int frame_counter;
	unsigned int skip_count;
	unsigned int skip;
};

R
Ralf Baechle 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
struct vino_channel_settings {
	unsigned int channel;

	int input;
	unsigned int data_format;
	unsigned int data_norm;
	struct vino_clipping clipping;
	unsigned int decimation;
	unsigned int line_size;
	unsigned int alpha;
	unsigned int fps;
	unsigned int framert_reg;

	unsigned int fifo_threshold;

	struct vino_framebuffer_queue fb_queue;

	/* number of the current field */
	unsigned int field;

	/* read in progress */
	int reading;
	/* streaming is active */
	int streaming;
	/* the driver is currently processing the queue */
	int capturing;

297
	struct mutex mutex;
R
Ralf Baechle 已提交
298
	spinlock_t capture_lock;
L
Linus Torvalds 已提交
299

R
Ralf Baechle 已提交
300
	unsigned int users;
L
Linus Torvalds 已提交
301

L
Ladislav Michl 已提交
302 303
	struct vino_interrupt_data int_data;

R
Ralf Baechle 已提交
304
	/* V4L support */
305
	struct video_device *vdev;
L
Linus Torvalds 已提交
306 307
};

R
Ralf Baechle 已提交
308
struct vino_settings {
309
	struct v4l2_device v4l2_dev;
R
Ralf Baechle 已提交
310 311
	struct vino_channel_settings a;
	struct vino_channel_settings b;
L
Linus Torvalds 已提交
312

313 314 315 316 317 318
	/* the channel which owns this client:
	 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
	unsigned int decoder_owner;
	struct v4l2_subdev *decoder;
	unsigned int camera_owner;
	struct v4l2_subdev *camera;
L
Linus Torvalds 已提交
319

R
Ralf Baechle 已提交
320 321 322 323
	/* a lock for vino register access */
	spinlock_t vino_lock;
	/* a lock for channel input changes */
	spinlock_t input_lock;
L
Linus Torvalds 已提交
324 325

	unsigned long dummy_page;
R
Ralf Baechle 已提交
326 327 328 329 330 331
	struct vino_descriptor_table dummy_desc_table;
};

/* Module parameters */

/*
L
Ladislav Michl 已提交
332
 * Using vino_pixel_conversion the ABGR32-format pixels supplied
R
Ralf Baechle 已提交
333 334 335 336 337 338 339 340 341 342 343 344
 * by the VINO chip can be converted to more common formats
 * like RGBA32 (or probably RGB24 in the future). This way we
 * can give out data that can be specified correctly with
 * the V4L2-definitions.
 *
 * The pixel format is specified as RGBA32 when no conversion
 * is used.
 *
 * Note that this only affects the 32-bit bit depth.
 *
 * Use non-zero value to enable conversion.
 */
345
static int vino_pixel_conversion;
L
Ladislav Michl 已提交
346

R
Ralf Baechle 已提交
347
module_param_named(pixelconv, vino_pixel_conversion, int, 0);
L
Ladislav Michl 已提交
348

R
Ralf Baechle 已提交
349 350 351 352 353 354 355 356 357
MODULE_PARM_DESC(pixelconv,
		 "enable pixel conversion (non-zero value enables)");

/* Internal data structures */

static struct sgi_vino *vino;

static struct vino_settings *vino_drvdata;

358 359 360 361 362
#define camera_call(o, f, args...) \
	v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
#define decoder_call(o, f, args...) \
	v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)

R
Ralf Baechle 已提交
363 364 365
static const char *vino_driver_name = "vino";
static const char *vino_driver_description = "SGI VINO";
static const char *vino_bus_name = "GIO64 bus";
366 367
static const char *vino_vdev_name_a = "SGI VINO Channel A";
static const char *vino_vdev_name_b = "SGI VINO Channel B";
R
Ralf Baechle 已提交
368

L
Ladislav Michl 已提交
369 370 371 372 373
static void vino_capture_tasklet(unsigned long channel);

DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);

R
Ralf Baechle 已提交
374 375 376
static const struct vino_input vino_inputs[] = {
	{
		.name		= "Composite",
L
Ladislav Michl 已提交
377 378
		.std		= V4L2_STD_NTSC | V4L2_STD_PAL
		| V4L2_STD_SECAM,
379
	}, {
R
Ralf Baechle 已提交
380
		.name		= "S-Video",
L
Ladislav Michl 已提交
381 382
		.std		= V4L2_STD_NTSC | V4L2_STD_PAL
		| V4L2_STD_SECAM,
383
	}, {
L
Ladislav Michl 已提交
384
		.name		= "D1/IndyCam",
R
Ralf Baechle 已提交
385 386 387 388 389 390 391 392 393 394
		.std		= V4L2_STD_NTSC,
	}
};

static const struct vino_data_format vino_data_formats[] = {
	{
		.description	= "8-bit greyscale",
		.bpp		= 1,
		.pixelformat	= V4L2_PIX_FMT_GREY,
		.colorspace	= V4L2_COLORSPACE_SMPTE170M,
395
	}, {
R
Ralf Baechle 已提交
396 397 398 399
		.description	= "8-bit dithered RGB 3-3-2",
		.bpp		= 1,
		.pixelformat	= V4L2_PIX_FMT_RGB332,
		.colorspace	= V4L2_COLORSPACE_SRGB,
400
	}, {
R
Ralf Baechle 已提交
401 402 403 404
		.description	= "32-bit RGB",
		.bpp		= 4,
		.pixelformat	= V4L2_PIX_FMT_RGB32,
		.colorspace	= V4L2_COLORSPACE_SRGB,
405
	}, {
R
Ralf Baechle 已提交
406
		.description	= "YUV 4:2:2",
L
Ladislav Michl 已提交
407
		.bpp		= 2,
R
Ralf Baechle 已提交
408 409
		.pixelformat	= V4L2_PIX_FMT_YUYV, // XXX: swapped?
		.colorspace	= V4L2_COLORSPACE_SMPTE170M,
L
Ladislav Michl 已提交
410
	}
R
Ralf Baechle 已提交
411 412 413 414 415 416 417 418 419 420 421 422
};

static const struct vino_data_norm vino_data_norms[] = {
	{
		.description	= "NTSC",
		.std		= V4L2_STD_NTSC,
		.fps_min	= 6,
		.fps_max	= 30,
		.framelines	= 525,
		.width		= VINO_NTSC_WIDTH,
		.height		= VINO_NTSC_HEIGHT,
		.odd		= {
L
Ladislav Michl 已提交
423 424
			.top	= VINO_CLIPPING_START_ODD_NTSC,
			.left	= 0,
R
Ralf Baechle 已提交
425 426
			.bottom	= VINO_CLIPPING_START_ODD_NTSC
			+ VINO_NTSC_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
427
			.right	= VINO_NTSC_WIDTH,
R
Ralf Baechle 已提交
428 429
		},
		.even		= {
L
Ladislav Michl 已提交
430 431
			.top	= VINO_CLIPPING_START_EVEN_NTSC,
			.left	= 0,
R
Ralf Baechle 已提交
432 433
			.bottom	= VINO_CLIPPING_START_EVEN_NTSC
			+ VINO_NTSC_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
434
			.right	= VINO_NTSC_WIDTH,
R
Ralf Baechle 已提交
435
		},
436
	}, {
R
Ralf Baechle 已提交
437 438 439 440 441 442 443 444
		.description	= "PAL",
		.std		= V4L2_STD_PAL,
		.fps_min	= 5,
		.fps_max	= 25,
		.framelines	= 625,
		.width		= VINO_PAL_WIDTH,
		.height		= VINO_PAL_HEIGHT,
		.odd		= {
L
Ladislav Michl 已提交
445 446
			.top	= VINO_CLIPPING_START_ODD_PAL,
			.left	= 0,
R
Ralf Baechle 已提交
447 448
			.bottom	= VINO_CLIPPING_START_ODD_PAL
			+ VINO_PAL_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
449
			.right	= VINO_PAL_WIDTH,
R
Ralf Baechle 已提交
450 451
		},
		.even		= {
L
Ladislav Michl 已提交
452 453
			.top	= VINO_CLIPPING_START_EVEN_PAL,
			.left	= 0,
R
Ralf Baechle 已提交
454 455
			.bottom	= VINO_CLIPPING_START_EVEN_PAL
			+ VINO_PAL_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
456
			.right	= VINO_PAL_WIDTH,
R
Ralf Baechle 已提交
457
		},
458
	}, {
R
Ralf Baechle 已提交
459 460 461 462 463 464 465 466
		.description	= "SECAM",
		.std		= V4L2_STD_SECAM,
		.fps_min	= 5,
		.fps_max	= 25,
		.framelines	= 625,
		.width		= VINO_PAL_WIDTH,
		.height		= VINO_PAL_HEIGHT,
		.odd		= {
L
Ladislav Michl 已提交
467 468
			.top	= VINO_CLIPPING_START_ODD_PAL,
			.left	= 0,
R
Ralf Baechle 已提交
469 470
			.bottom	= VINO_CLIPPING_START_ODD_PAL
			+ VINO_PAL_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
471
			.right	= VINO_PAL_WIDTH,
R
Ralf Baechle 已提交
472 473
		},
		.even		= {
L
Ladislav Michl 已提交
474 475
			.top	= VINO_CLIPPING_START_EVEN_PAL,
			.left	= 0,
R
Ralf Baechle 已提交
476 477
			.bottom	= VINO_CLIPPING_START_EVEN_PAL
			+ VINO_PAL_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
478
			.right	= VINO_PAL_WIDTH,
R
Ralf Baechle 已提交
479
		},
480
	}, {
L
Ladislav Michl 已提交
481
		.description	= "NTSC/D1",
R
Ralf Baechle 已提交
482 483 484 485 486 487 488
		.std		= V4L2_STD_NTSC,
		.fps_min	= 6,
		.fps_max	= 30,
		.framelines	= 525,
		.width		= VINO_NTSC_WIDTH,
		.height		= VINO_NTSC_HEIGHT,
		.odd		= {
L
Ladislav Michl 已提交
489 490
			.top	= VINO_CLIPPING_START_ODD_D1,
			.left	= 0,
R
Ralf Baechle 已提交
491 492
			.bottom	= VINO_CLIPPING_START_ODD_D1
			+ VINO_NTSC_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
493
			.right	= VINO_NTSC_WIDTH,
R
Ralf Baechle 已提交
494 495
		},
		.even		= {
L
Ladislav Michl 已提交
496 497
			.top	= VINO_CLIPPING_START_EVEN_D1,
			.left	= 0,
R
Ralf Baechle 已提交
498 499
			.bottom	= VINO_CLIPPING_START_EVEN_D1
			+ VINO_NTSC_HEIGHT / 2 - 1,
L
Ladislav Michl 已提交
500
			.right	= VINO_NTSC_WIDTH,
R
Ralf Baechle 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
		},
	}
};

#define VINO_INDYCAM_V4L2_CONTROL_COUNT		9

struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
	{
		.id = V4L2_CID_AUTOGAIN,
		.type = V4L2_CTRL_TYPE_BOOLEAN,
		.name = "Automatic Gain Control",
		.minimum = 0,
		.maximum = 1,
		.step = 1,
		.default_value = INDYCAM_AGC_DEFAULT,
516
	}, {
R
Ralf Baechle 已提交
517 518 519 520 521 522 523
		.id = V4L2_CID_AUTO_WHITE_BALANCE,
		.type = V4L2_CTRL_TYPE_BOOLEAN,
		.name = "Automatic White Balance",
		.minimum = 0,
		.maximum = 1,
		.step = 1,
		.default_value = INDYCAM_AWB_DEFAULT,
524
	}, {
R
Ralf Baechle 已提交
525 526 527 528 529 530 531
		.id = V4L2_CID_GAIN,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Gain",
		.minimum = INDYCAM_GAIN_MIN,
		.maximum = INDYCAM_GAIN_MAX,
		.step = 1,
		.default_value = INDYCAM_GAIN_DEFAULT,
532 533
	}, {
		.id = INDYCAM_CONTROL_RED_SATURATION,
R
Ralf Baechle 已提交
534 535 536 537 538 539
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Red Saturation",
		.minimum = INDYCAM_RED_SATURATION_MIN,
		.maximum = INDYCAM_RED_SATURATION_MAX,
		.step = 1,
		.default_value = INDYCAM_RED_SATURATION_DEFAULT,
540 541
	}, {
		.id = INDYCAM_CONTROL_BLUE_SATURATION,
R
Ralf Baechle 已提交
542 543 544 545 546 547
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Blue Saturation",
		.minimum = INDYCAM_BLUE_SATURATION_MIN,
		.maximum = INDYCAM_BLUE_SATURATION_MAX,
		.step = 1,
		.default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
548
	}, {
R
Ralf Baechle 已提交
549 550 551 552 553 554 555
		.id = V4L2_CID_RED_BALANCE,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Red Balance",
		.minimum = INDYCAM_RED_BALANCE_MIN,
		.maximum = INDYCAM_RED_BALANCE_MAX,
		.step = 1,
		.default_value = INDYCAM_RED_BALANCE_DEFAULT,
556
	}, {
R
Ralf Baechle 已提交
557 558 559 560 561 562 563
		.id = V4L2_CID_BLUE_BALANCE,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Blue Balance",
		.minimum = INDYCAM_BLUE_BALANCE_MIN,
		.maximum = INDYCAM_BLUE_BALANCE_MAX,
		.step = 1,
		.default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
564
	}, {
R
Ralf Baechle 已提交
565 566 567 568 569 570 571
		.id = V4L2_CID_EXPOSURE,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Shutter Control",
		.minimum = INDYCAM_SHUTTER_MIN,
		.maximum = INDYCAM_SHUTTER_MAX,
		.step = 1,
		.default_value = INDYCAM_SHUTTER_DEFAULT,
572
	}, {
R
Ralf Baechle 已提交
573 574 575 576 577 578 579 580 581 582
		.id = V4L2_CID_GAMMA,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Gamma",
		.minimum = INDYCAM_GAMMA_MIN,
		.maximum = INDYCAM_GAMMA_MAX,
		.step = 1,
		.default_value = INDYCAM_GAMMA_DEFAULT,
	}
};

L
Ladislav Michl 已提交
583
#define VINO_SAA7191_V4L2_CONTROL_COUNT		9
R
Ralf Baechle 已提交
584 585 586 587 588 589 590 591 592 593

struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
	{
		.id = V4L2_CID_HUE,
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Hue",
		.minimum = SAA7191_HUE_MIN,
		.maximum = SAA7191_HUE_MAX,
		.step = 1,
		.default_value = SAA7191_HUE_DEFAULT,
594 595
	}, {
		.id = SAA7191_CONTROL_BANDPASS,
L
Ladislav Michl 已提交
596 597 598 599 600 601
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Luminance Bandpass",
		.minimum = SAA7191_BANDPASS_MIN,
		.maximum = SAA7191_BANDPASS_MAX,
		.step = 1,
		.default_value = SAA7191_BANDPASS_DEFAULT,
602 603
	}, {
		.id = SAA7191_CONTROL_BANDPASS_WEIGHT,
L
Ladislav Michl 已提交
604 605 606 607 608 609
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Luminance Bandpass Weight",
		.minimum = SAA7191_BANDPASS_WEIGHT_MIN,
		.maximum = SAA7191_BANDPASS_WEIGHT_MAX,
		.step = 1,
		.default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
610 611
	}, {
		.id = SAA7191_CONTROL_CORING,
L
Ladislav Michl 已提交
612 613 614 615 616 617
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "HF Luminance Coring",
		.minimum = SAA7191_CORING_MIN,
		.maximum = SAA7191_CORING_MAX,
		.step = 1,
		.default_value = SAA7191_CORING_DEFAULT,
618 619
	}, {
		.id = SAA7191_CONTROL_FORCE_COLOUR,
L
Ladislav Michl 已提交
620 621 622 623 624 625
		.type = V4L2_CTRL_TYPE_BOOLEAN,
		.name = "Force Colour",
		.minimum = SAA7191_FORCE_COLOUR_MIN,
		.maximum = SAA7191_FORCE_COLOUR_MAX,
		.step = 1,
		.default_value = SAA7191_FORCE_COLOUR_DEFAULT,
626 627
	}, {
		.id = SAA7191_CONTROL_CHROMA_GAIN,
L
Ladislav Michl 已提交
628 629 630 631 632 633
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Chrominance Gain Control",
		.minimum = SAA7191_CHROMA_GAIN_MIN,
		.maximum = SAA7191_CHROMA_GAIN_MAX,
		.step = 1,
		.default_value = SAA7191_CHROMA_GAIN_DEFAULT,
634 635
	}, {
		.id = SAA7191_CONTROL_VTRC,
R
Ralf Baechle 已提交
636 637 638 639 640 641
		.type = V4L2_CTRL_TYPE_BOOLEAN,
		.name = "VTR Time Constant",
		.minimum = SAA7191_VTRC_MIN,
		.maximum = SAA7191_VTRC_MAX,
		.step = 1,
		.default_value = SAA7191_VTRC_DEFAULT,
642 643
	}, {
		.id = SAA7191_CONTROL_LUMA_DELAY,
L
Ladislav Michl 已提交
644 645 646 647 648 649
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Luminance Delay Compensation",
		.minimum = SAA7191_LUMA_DELAY_MIN,
		.maximum = SAA7191_LUMA_DELAY_MAX,
		.step = 1,
		.default_value = SAA7191_LUMA_DELAY_DEFAULT,
650 651
	}, {
		.id = SAA7191_CONTROL_VNR,
L
Ladislav Michl 已提交
652 653 654 655 656 657
		.type = V4L2_CTRL_TYPE_INTEGER,
		.name = "Vertical Noise Reduction",
		.minimum = SAA7191_VNR_MIN,
		.maximum = SAA7191_VNR_MAX,
		.step = 1,
		.default_value = SAA7191_VNR_DEFAULT,
R
Ralf Baechle 已提交
658
	}
L
Linus Torvalds 已提交
659 660
};

R
Ralf Baechle 已提交
661 662 663 664
/* VINO framebuffer/DMA descriptor management */

static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
					       unsigned int count)
L
Linus Torvalds 已提交
665
{
R
Ralf Baechle 已提交
666
	unsigned int i;
L
Linus Torvalds 已提交
667

R
Ralf Baechle 已提交
668 669 670
	dprintk("vino_free_buffer_with_count(): count = %d\n", count);

	for (i = 0; i < count; i++) {
671
		ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
R
Ralf Baechle 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
		dma_unmap_single(NULL,
				 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
				 PAGE_SIZE, DMA_FROM_DEVICE);
		free_page(fb->desc_table.virtual[i]);
	}

	dma_free_coherent(NULL,
			  VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
			  sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
			  fb->desc_table.dma);
	kfree(fb->desc_table.virtual);

	memset(fb, 0, sizeof(struct vino_framebuffer));
}

static void vino_free_buffer(struct vino_framebuffer *fb)
{
	vino_free_buffer_with_count(fb, fb->desc_table.page_count);
L
Linus Torvalds 已提交
690 691
}

R
Ralf Baechle 已提交
692 693
static int vino_allocate_buffer(struct vino_framebuffer *fb,
				unsigned int size)
L
Linus Torvalds 已提交
694
{
R
Ralf Baechle 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	unsigned int count, i, j;
	int ret = 0;

	dprintk("vino_allocate_buffer():\n");

	if (size < 1)
		return -EINVAL;

	memset(fb, 0, sizeof(struct vino_framebuffer));

	count = ((size / PAGE_SIZE) + 4) & ~3;

	dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
		size, count);

	/* allocate memory for table with virtual (page) addresses */
	fb->desc_table.virtual = (unsigned long *)
		kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
	if (!fb->desc_table.virtual)
		return -ENOMEM;

	/* allocate memory for table with dma addresses
	 * (has space for four extra descriptors) */
	fb->desc_table.dma_cpu =
		dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
				   sizeof(dma_addr_t), &fb->desc_table.dma,
				   GFP_KERNEL | GFP_DMA);
	if (!fb->desc_table.dma_cpu) {
		ret = -ENOMEM;
		goto out_free_virtual;
	}

	/* allocate pages for the buffer and acquire the according
	 * dma addresses */
	for (i = 0; i < count; i++) {
		dma_addr_t dma_data_addr;

		fb->desc_table.virtual[i] =
			get_zeroed_page(GFP_KERNEL | GFP_DMA);
		if (!fb->desc_table.virtual[i]) {
			ret = -ENOBUFS;
			break;
		}

		dma_data_addr =
			dma_map_single(NULL,
				       (void *)fb->desc_table.virtual[i],
				       PAGE_SIZE, DMA_FROM_DEVICE);

		for (j = 0; j < VINO_PAGE_RATIO; j++) {
			fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
				dma_data_addr + VINO_PAGE_SIZE * j;
		}

749
		SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
R
Ralf Baechle 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	}

	/* page_count needs to be set anyway, because the descriptor table has
	 * been allocated according to this number */
	fb->desc_table.page_count = count;

	if (ret) {
		/* the descriptor with index i doesn't contain
		 * a valid address yet */
		vino_free_buffer_with_count(fb, i);
		return ret;
	}

	//fb->size = size;
	fb->size = count * PAGE_SIZE;
	fb->data_format = VINO_DATA_FMT_NONE;

	/* set the dma stop-bit for the last (count+1)th descriptor */
	fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
	return 0;

 out_free_virtual:
	kfree(fb->desc_table.virtual);
	return ret;
L
Linus Torvalds 已提交
774 775
}

R
Ralf Baechle 已提交
776 777 778 779 780
#if 0
/* user buffers not fully implemented yet */
static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
				     void *user,
				     unsigned int size)
L
Linus Torvalds 已提交
781
{
R
Ralf Baechle 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
	unsigned int count, i, j;
	int ret = 0;

	dprintk("vino_prepare_user_buffer():\n");

	if (size < 1)
		return -EINVAL;

	memset(fb, 0, sizeof(struct vino_framebuffer));

	count = ((size / PAGE_SIZE)) & ~3;

	dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
		size, count);

	/* allocate memory for table with virtual (page) addresses */
	fb->desc_table.virtual = (unsigned long *)
		kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
	if (!fb->desc_table.virtual)
		return -ENOMEM;

	/* allocate memory for table with dma addresses
	 * (has space for four extra descriptors) */
	fb->desc_table.dma_cpu =
		dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
				   sizeof(dma_addr_t), &fb->desc_table.dma,
				   GFP_KERNEL | GFP_DMA);
	if (!fb->desc_table.dma_cpu) {
		ret = -ENOMEM;
		goto out_free_virtual;
	}

	/* allocate pages for the buffer and acquire the according
	 * dma addresses */
	for (i = 0; i < count; i++) {
		dma_addr_t dma_data_addr;

		fb->desc_table.virtual[i] =
			get_zeroed_page(GFP_KERNEL | GFP_DMA);
		if (!fb->desc_table.virtual[i]) {
			ret = -ENOBUFS;
			break;
		}

		dma_data_addr =
			dma_map_single(NULL,
				       (void *)fb->desc_table.virtual[i],
				       PAGE_SIZE, DMA_FROM_DEVICE);

		for (j = 0; j < VINO_PAGE_RATIO; j++) {
			fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
				dma_data_addr + VINO_PAGE_SIZE * j;
		}

836
		SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
R
Ralf Baechle 已提交
837
	}
L
Linus Torvalds 已提交
838

R
Ralf Baechle 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	/* page_count needs to be set anyway, because the descriptor table has
	 * been allocated according to this number */
	fb->desc_table.page_count = count;

	if (ret) {
		/* the descriptor with index i doesn't contain
		 * a valid address yet */
		vino_free_buffer_with_count(fb, i);
		return ret;
	}

	//fb->size = size;
	fb->size = count * PAGE_SIZE;

	/* set the dma stop-bit for the last (count+1)th descriptor */
	fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
	return 0;

 out_free_virtual:
	kfree(fb->desc_table.virtual);
	return ret;
L
Linus Torvalds 已提交
860
}
R
Ralf Baechle 已提交
861
#endif
L
Linus Torvalds 已提交
862

R
Ralf Baechle 已提交
863
static void vino_sync_buffer(struct vino_framebuffer *fb)
L
Linus Torvalds 已提交
864
{
R
Ralf Baechle 已提交
865 866 867
	int i;

	dprintk("vino_sync_buffer():\n");
L
Linus Torvalds 已提交
868

R
Ralf Baechle 已提交
869
	for (i = 0; i < fb->desc_table.page_count; i++)
870 871 872
		dma_sync_single_for_cpu(NULL,
					fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
					PAGE_SIZE, DMA_FROM_DEVICE);
L
Linus Torvalds 已提交
873 874
}

R
Ralf Baechle 已提交
875
/* Framebuffer fifo functions (need to be locked externally) */
L
Linus Torvalds 已提交
876

L
Ladislav Michl 已提交
877
static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
R
Ralf Baechle 已提交
878
			   unsigned int length)
L
Linus Torvalds 已提交
879
{
R
Ralf Baechle 已提交
880 881 882 883
	f->length = 0;
	f->used = 0;
	f->head = 0;
	f->tail = 0;
L
Linus Torvalds 已提交
884

L
Ladislav Michl 已提交
885 886
	if (length > VINO_FRAMEBUFFER_COUNT_MAX)
		length = VINO_FRAMEBUFFER_COUNT_MAX;
L
Linus Torvalds 已提交
887

R
Ralf Baechle 已提交
888 889 890 891
	f->length = length;
}

/* returns true/false */
L
Ladislav Michl 已提交
892 893
static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
				   unsigned int id)
R
Ralf Baechle 已提交
894 895
{
	unsigned int i;
L
Ladislav Michl 已提交
896

R
Ralf Baechle 已提交
897 898 899
	for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
		if (f->data[i] == id)
			return 1;
L
Linus Torvalds 已提交
900 901
	}

R
Ralf Baechle 已提交
902 903
	return 0;
}
L
Linus Torvalds 已提交
904

L
Ladislav Michl 已提交
905
#if 0
R
Ralf Baechle 已提交
906
/* returns true/false */
L
Ladislav Michl 已提交
907
static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
R
Ralf Baechle 已提交
908 909 910
{
	return (f->used == f->length);
}
L
Ladislav Michl 已提交
911
#endif
R
Ralf Baechle 已提交
912

L
Ladislav Michl 已提交
913
static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
R
Ralf Baechle 已提交
914 915 916 917 918 919 920 921
{
	return f->used;
}

static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
{
	if (id >= f->length) {
		return VINO_QUEUE_ERROR;
L
Linus Torvalds 已提交
922
	}
R
Ralf Baechle 已提交
923 924 925

	if (vino_fifo_has_id(f, id)) {
		return VINO_QUEUE_ERROR;
L
Linus Torvalds 已提交
926 927
	}

R
Ralf Baechle 已提交
928 929 930 931 932 933
	if (f->used < f->length) {
		f->data[f->tail] = id;
		f->tail = (f->tail + 1) % f->length;
		f->used++;
	} else {
		return VINO_QUEUE_ERROR;
L
Linus Torvalds 已提交
934 935
	}

R
Ralf Baechle 已提交
936 937 938 939 940 941 942 943 944
	return 0;
}

static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
{
	if (f->used > 0) {
		*id = f->data[f->head];
	} else {
		return VINO_QUEUE_ERROR;
L
Linus Torvalds 已提交
945 946
	}

R
Ralf Baechle 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	return 0;
}

static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
{
	if (f->used > 0) {
		*id = f->data[f->head];
		f->head = (f->head + 1) % f->length;
		f->used--;
	} else {
		return VINO_QUEUE_ERROR;
	}

	return 0;
}
L
Linus Torvalds 已提交
962

R
Ralf Baechle 已提交
963
/* Framebuffer queue functions */
L
Linus Torvalds 已提交
964

R
Ralf Baechle 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978
/* execute with queue_lock locked */
static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
				       unsigned int length)
{
	unsigned int i;

	q->length = 0;
	memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
	memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
	for (i = 0; i < length; i++) {
		dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
			i);
		vino_free_buffer(q->buffer[i]);
		kfree(q->buffer[i]);
L
Linus Torvalds 已提交
979 980
	}

R
Ralf Baechle 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993
	q->type = VINO_MEMORY_NONE;
	q->magic = 0;
}

static void vino_queue_free(struct vino_framebuffer_queue *q)
{
	dprintk("vino_queue_free():\n");

	if (q->magic != VINO_QUEUE_MAGIC)
		return;
	if (q->type != VINO_MEMORY_MMAP)
		return;

994
	mutex_lock(&q->queue_mutex);
R
Ralf Baechle 已提交
995 996 997

	vino_queue_free_with_count(q, q->length);

998
	mutex_unlock(&q->queue_mutex);
R
Ralf Baechle 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
}

static int vino_queue_init(struct vino_framebuffer_queue *q,
			   unsigned int *length)
{
	unsigned int i;
	int ret = 0;

	dprintk("vino_queue_init(): length = %d\n", *length);

	if (q->magic == VINO_QUEUE_MAGIC) {
		dprintk("vino_queue_init(): queue already initialized!\n");
		return -EINVAL;
L
Linus Torvalds 已提交
1012 1013
	}

R
Ralf Baechle 已提交
1014 1015 1016
	if (q->type != VINO_MEMORY_NONE) {
		dprintk("vino_queue_init(): queue already initialized!\n");
		return -EINVAL;
L
Linus Torvalds 已提交
1017
	}
R
Ralf Baechle 已提交
1018 1019 1020 1021

	if (*length < 1)
		return -EINVAL;

1022
	mutex_lock(&q->queue_mutex);
R
Ralf Baechle 已提交
1023

L
Ladislav Michl 已提交
1024 1025
	if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
		*length = VINO_FRAMEBUFFER_COUNT_MAX;
R
Ralf Baechle 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

	q->length = 0;

	for (i = 0; i < *length; i++) {
		dprintk("vino_queue_init(): allocating buffer %d\n", i);
		q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
				       GFP_KERNEL);
		if (!q->buffer[i]) {
			dprintk("vino_queue_init(): kmalloc() failed\n");
			ret = -ENOMEM;
			break;
		}

		ret = vino_allocate_buffer(q->buffer[i],
					   VINO_FRAMEBUFFER_SIZE);
		if (ret) {
			kfree(q->buffer[i]);
			dprintk("vino_queue_init(): "
				"vino_allocate_buffer() failed\n");
			break;
		}

		q->buffer[i]->id = i;
		if (i > 0) {
			q->buffer[i]->offset = q->buffer[i - 1]->offset +
				q->buffer[i - 1]->size;
		} else {
			q->buffer[i]->offset = 0;
		}

		spin_lock_init(&q->buffer[i]->state_lock);

		dprintk("vino_queue_init(): buffer = %d, offset = %d, "
			"size = %d\n", i, q->buffer[i]->offset,
			q->buffer[i]->size);
L
Linus Torvalds 已提交
1061 1062
	}

R
Ralf Baechle 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if (ret) {
		vino_queue_free_with_count(q, i);
		*length = 0;
	} else {
		q->length = *length;
		vino_fifo_init(&q->in, q->length);
		vino_fifo_init(&q->out, q->length);
		q->type = VINO_MEMORY_MMAP;
		q->magic = VINO_QUEUE_MAGIC;
	}
L
Linus Torvalds 已提交
1073

1074
	mutex_unlock(&q->queue_mutex);
L
Linus Torvalds 已提交
1075 1076 1077 1078

	return ret;
}

R
Ralf Baechle 已提交
1079 1080 1081
static struct vino_framebuffer *vino_queue_add(struct
					       vino_framebuffer_queue *q,
					       unsigned int id)
L
Linus Torvalds 已提交
1082
{
R
Ralf Baechle 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	struct vino_framebuffer *ret = NULL;
	unsigned int total;
	unsigned long flags;

	dprintk("vino_queue_add(): id = %d\n", id);

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	if (id >= q->length)
		goto out;

	/* not needed?: if (vino_fifo_full(&q->out)) {
		goto out;
		}*/
	/* check that outgoing queue isn't already full
	 * (or that it won't become full) */
	total = vino_fifo_get_used(&q->in) +
		vino_fifo_get_used(&q->out);
	if (total >= q->length)
		goto out;

	if (vino_fifo_enqueue(&q->in, id))
		goto out;

	ret = q->buffer[id];

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
L
Linus Torvalds 已提交
1120 1121
}

R
Ralf Baechle 已提交
1122 1123 1124 1125 1126 1127 1128
static struct vino_framebuffer *vino_queue_transfer(struct
						    vino_framebuffer_queue *q)
{
	struct vino_framebuffer *ret = NULL;
	struct vino_framebuffer *fb;
	int id;
	unsigned long flags;
L
Linus Torvalds 已提交
1129

R
Ralf Baechle 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	dprintk("vino_queue_transfer():\n");

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	// now this actually removes an entry from the incoming queue
	if (vino_fifo_dequeue(&q->in, &id)) {
		goto out;
	}

	dprintk("vino_queue_transfer(): id = %d\n", id);
	fb = q->buffer[id];

	// we have already checked that the outgoing queue is not full, but...
	if (vino_fifo_enqueue(&q->out, id)) {
		printk(KERN_ERR "vino_queue_transfer(): "
		       "outgoing queue is full, this shouldn't happen!\n");
		goto out;
	}

	ret = fb;
out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

/* returns true/false */
static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
					unsigned int id)
{
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	ret = vino_fifo_has_id(&q->in, id);

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

/* returns true/false */
static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
					unsigned int id)
{
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	ret = vino_fifo_has_id(&q->out, id);

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
				   unsigned int *used)
{
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return VINO_QUEUE_ERROR;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0) {
		ret = VINO_QUEUE_ERROR;
		goto out;
	}

	*used = vino_fifo_get_used(&q->in);

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
				   unsigned int *used)
{
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return VINO_QUEUE_ERROR;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0) {
		ret = VINO_QUEUE_ERROR;
		goto out;
	}

	*used = vino_fifo_get_used(&q->out);

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

L
Ladislav Michl 已提交
1261
#if 0
R
Ralf Baechle 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
static int vino_queue_get_total(struct vino_framebuffer_queue *q,
				unsigned int *total)
{
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return VINO_QUEUE_ERROR;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0) {
		ret = VINO_QUEUE_ERROR;
		goto out;
	}

	*total = vino_fifo_get_used(&q->in) +
		vino_fifo_get_used(&q->out);

out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}
L
Ladislav Michl 已提交
1287
#endif
R
Ralf Baechle 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416

static struct vino_framebuffer *vino_queue_peek(struct
						vino_framebuffer_queue *q,
						unsigned int *id)
{
	struct vino_framebuffer *ret = NULL;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	if (vino_fifo_peek(&q->in, id)) {
		goto out;
	}

	ret = q->buffer[*id];
out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

static struct vino_framebuffer *vino_queue_remove(struct
						  vino_framebuffer_queue *q,
						  unsigned int *id)
{
	struct vino_framebuffer *ret = NULL;
	unsigned long flags;
	dprintk("vino_queue_remove():\n");

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	if (vino_fifo_dequeue(&q->out, id)) {
		goto out;
	}

	dprintk("vino_queue_remove(): id = %d\n", *id);
	ret = q->buffer[*id];
out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

static struct
vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
					unsigned int id)
{
	struct vino_framebuffer *ret = NULL;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);

	if (q->length == 0)
		goto out;

	if (id >= q->length)
		goto out;

	ret = q->buffer[id];
 out:
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
{
	unsigned int length = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return length;
	}

	spin_lock_irqsave(&q->queue_lock, flags);
	length = q->length;
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return length;
}

static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
{
	unsigned int i;
	int ret = 0;
	unsigned long flags;

	if (q->magic != VINO_QUEUE_MAGIC) {
		return ret;
	}

	spin_lock_irqsave(&q->queue_lock, flags);
	for (i = 0; i < q->length; i++) {
		if (q->buffer[i]->map_count > 0) {
			ret = 1;
			break;
		}
	}
	spin_unlock_irqrestore(&q->queue_lock, flags);

	return ret;
}

/* VINO functions */

/* execute with input_lock locked */
static void vino_update_line_size(struct vino_channel_settings *vcs)
{
	unsigned int w = vcs->clipping.right - vcs->clipping.left;
	unsigned int d = vcs->decimation;
	unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1417
	unsigned int lsize;
R
Ralf Baechle 已提交
1418 1419 1420

	dprintk("update_line_size(): before: w = %d, d = %d, "
		"line_size = %d\n", w, d, vcs->line_size);
L
Ladislav Michl 已提交
1421

1422
	/* line size must be multiple of 8 bytes */
R
Ralf Baechle 已提交
1423 1424 1425 1426 1427
	lsize = (bpp * (w / d)) & ~7;
	w = (lsize / bpp) * d;

	vcs->clipping.right = vcs->clipping.left + w;
	vcs->line_size = lsize;
L
Ladislav Michl 已提交
1428

R
Ralf Baechle 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	dprintk("update_line_size(): after: w = %d, d = %d, "
		"line_size = %d\n", w, d, vcs->line_size);
}

/* execute with input_lock locked */
static void vino_set_clipping(struct vino_channel_settings *vcs,
			      unsigned int x, unsigned int y,
			      unsigned int w, unsigned int h)
{
	unsigned int maxwidth, maxheight;
	unsigned int d;

	maxwidth = vino_data_norms[vcs->data_norm].width;
	maxheight = vino_data_norms[vcs->data_norm].height;
	d = vcs->decimation;

	y &= ~1;	/* odd/even fields */

	if (x > maxwidth) {
		x = 0;
	}
	if (y > maxheight) {
		y = 0;
	}

	if (((w / d) < VINO_MIN_WIDTH)
	    || ((h / d) < VINO_MIN_HEIGHT)) {
		w = VINO_MIN_WIDTH * d;
		h = VINO_MIN_HEIGHT * d;
	}

	if ((x + w) > maxwidth) {
		w = maxwidth - x;
		if ((w / d) < VINO_MIN_WIDTH)
			x = maxwidth - VINO_MIN_WIDTH * d;
	}
	if ((y + h) > maxheight) {
		h = maxheight - y;
		if ((h / d) < VINO_MIN_HEIGHT)
			y = maxheight - VINO_MIN_HEIGHT * d;
	}

	vcs->clipping.left = x;
	vcs->clipping.top = y;
	vcs->clipping.right = x + w;
	vcs->clipping.bottom = y + h;

	vino_update_line_size(vcs);

	dprintk("clipping %d, %d, %d, %d / %d - %d\n",
		vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
		vcs->clipping.bottom, vcs->decimation, vcs->line_size);
}

/* execute with input_lock locked */
L
Ladislav Michl 已提交
1484
static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
{
	vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
			  vino_data_norms[vcs->data_norm].height);
}

/* execute with input_lock locked */
static void vino_set_scaling(struct vino_channel_settings *vcs,
			     unsigned int w, unsigned int h)
{
	unsigned int x, y, curw, curh, d;

	x = vcs->clipping.left;
	y = vcs->clipping.top;
	curw = vcs->clipping.right - vcs->clipping.left;
	curh = vcs->clipping.bottom - vcs->clipping.top;

	d = max(curw / w, curh / h);

	dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
		w, h, curw, curh, d);

	if (d < 1) {
		d = 1;
L
Ladislav Michl 已提交
1508
	} else if (d > 8) {
R
Ralf Baechle 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
		d = 8;
	}

	vcs->decimation = d;
	vino_set_clipping(vcs, x, y, w * d, h * d);

	dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
		vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
		vcs->decimation, vcs->line_size);
}

/* execute with input_lock locked */
L
Ladislav Michl 已提交
1521
static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
{
	vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
			 vcs->clipping.bottom - vcs->clipping.top);
}

/* execute with input_lock locked */
static void vino_set_framerate(struct vino_channel_settings *vcs,
			       unsigned int fps)
{
	unsigned int mask;

	switch (vcs->data_norm) {
	case VINO_DATA_NORM_NTSC:
	case VINO_DATA_NORM_D1:
		fps = (unsigned int)(fps / 6) * 6; // FIXME: round!

		if (fps < vino_data_norms[vcs->data_norm].fps_min)
			fps = vino_data_norms[vcs->data_norm].fps_min;
		if (fps > vino_data_norms[vcs->data_norm].fps_max)
			fps = vino_data_norms[vcs->data_norm].fps_max;

		switch (fps) {
		case 6:
			mask = 0x003;
			break;
		case 12:
			mask = 0x0c3;
			break;
		case 18:
			mask = 0x333;
			break;
		case 24:
			mask = 0x3ff;
			break;
		case 30:
			mask = 0xfff;
			break;
		default:
			mask = VINO_FRAMERT_FULL;
		}
		vcs->framert_reg = VINO_FRAMERT_RT(mask);
		break;
	case VINO_DATA_NORM_PAL:
	case VINO_DATA_NORM_SECAM:
		fps = (unsigned int)(fps / 5) * 5; // FIXME: round!

		if (fps < vino_data_norms[vcs->data_norm].fps_min)
			fps = vino_data_norms[vcs->data_norm].fps_min;
		if (fps > vino_data_norms[vcs->data_norm].fps_max)
			fps = vino_data_norms[vcs->data_norm].fps_max;

		switch (fps) {
		case 5:
			mask = 0x003;
			break;
		case 10:
			mask = 0x0c3;
			break;
		case 15:
			mask = 0x333;
			break;
		case 20:
			mask = 0x0ff;
			break;
		case 25:
			mask = 0x3ff;
			break;
		default:
			mask = VINO_FRAMERT_FULL;
		}
		vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
		break;
	}

	vcs->fps = fps;
}

/* execute with input_lock locked */
L
Ladislav Michl 已提交
1600 1601
static inline void vino_set_default_framerate(struct
					      vino_channel_settings *vcs)
R
Ralf Baechle 已提交
1602 1603 1604 1605
{
	vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
}

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
/* VINO I2C bus functions */

struct i2c_algo_sgi_data {
	void *data;	/* private data for lowlevel routines */
	unsigned (*getctrl)(void *data);
	void (*setctrl)(void *data, unsigned val);
	unsigned (*rdata)(void *data);
	void (*wdata)(void *data, unsigned val);

	int xfer_timeout;
	int ack_timeout;
};

static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
{
	int i;

	for (i = 0; i < adap->xfer_timeout; i++) {
		if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
			return 0;
		udelay(1);
	}

	return -ETIMEDOUT;
}

static int wait_ack(struct i2c_algo_sgi_data *adap)
{
	int i;

	if (wait_xfer_done(adap))
		return -ETIMEDOUT;
	for (i = 0; i < adap->ack_timeout; i++) {
		if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
			return 0;
		udelay(1);
	}

	return -ETIMEDOUT;
}

static int force_idle(struct i2c_algo_sgi_data *adap)
{
	int i;

	adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
	for (i = 0; i < adap->xfer_timeout; i++) {
		if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
			goto out;
		udelay(1);
	}
	return -ETIMEDOUT;
out:
	if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
		return -EIO;
	return 0;
}

static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
		      int rd)
{
	if (rd)
		adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
	/* Check if bus is idle, eventually force it to do so */
	if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
		if (force_idle(adap))
			return -EIO;
	/* Write out the i2c chip address and specify operation */
	adap->setctrl(adap->data,
		      SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
	if (rd)
		addr |= 1;
	adap->wdata(adap->data, addr);
	if (wait_ack(adap))
		return -EIO;
	return 0;
}

static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
		    unsigned int len)
{
	int i;

	adap->setctrl(adap->data,
		      SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
	for (i = 0; i < len; i++) {
		if (wait_xfer_done(adap))
			return -EIO;
		buf[i] = adap->rdata(adap->data);
	}
	adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);

	return 0;

}

static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
		     unsigned int len)
{
	int i;

	/* We are already in write state */
	for (i = 0; i < len; i++) {
		adap->wdata(adap->data, buf[i]);
		if (wait_ack(adap))
			return -EIO;
	}
	return 0;
}

static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
		    int num)
{
	struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
	struct i2c_msg *p;
	int i, err = 0;

	for (i = 0; !err && i < num; i++) {
		p = &msgs[i];
		err = do_address(adap, p->addr, p->flags & I2C_M_RD);
		if (err || !p->len)
			continue;
		if (p->flags & I2C_M_RD)
			err = i2c_read(adap, p->buf, p->len);
		else
			err = i2c_write(adap, p->buf, p->len);
	}

	return (err < 0) ? err : i;
}

static u32 sgi_func(struct i2c_adapter *adap)
{
	return I2C_FUNC_SMBUS_EMUL;
}

static const struct i2c_algorithm sgi_algo = {
	.master_xfer	= sgi_xfer,
	.functionality	= sgi_func,
};

static unsigned i2c_vino_getctrl(void *data)
{
	return vino->i2c_control;
}

static void i2c_vino_setctrl(void *data, unsigned val)
{
	vino->i2c_control = val;
}

static unsigned i2c_vino_rdata(void *data)
{
	return vino->i2c_data;
}

static void i2c_vino_wdata(void *data, unsigned val)
{
	vino->i2c_data = val;
}

static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
	.getctrl = &i2c_vino_getctrl,
	.setctrl = &i2c_vino_setctrl,
	.rdata   = &i2c_vino_rdata,
	.wdata   = &i2c_vino_wdata,
	.xfer_timeout = 200,
	.ack_timeout  = 1000,
};

static struct i2c_adapter vino_i2c_adapter = {
	.name			= "VINO I2C bus",
	.algo			= &sgi_algo,
	.algo_data		= &i2c_sgi_vino_data,
	.owner 			= THIS_MODULE,
};

R
Ralf Baechle 已提交
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
/*
 * Prepare VINO for DMA transfer...
 * (execute only with vino_lock and input_lock locked)
 */
static int vino_dma_setup(struct vino_channel_settings *vcs,
			  struct vino_framebuffer *fb)
{
	u32 ctrl, intr;
	struct sgi_vino_channel *ch;
	const struct vino_data_norm *norm;

	dprintk("vino_dma_setup():\n");

	vcs->field = 0;
	fb->frame_counter = 0;

	ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
	norm = &vino_data_norms[vcs->data_norm];

	ch->page_index = 0;
	ch->line_count = 0;

	/* VINO line size register is set 8 bytes less than actual */
	ch->line_size = vcs->line_size - 8;

	/* let VINO know where to transfer data */
	ch->start_desc_tbl = fb->desc_table.dma;
	ch->next_4_desc = fb->desc_table.dma;

	/* give vino time to fetch the first four descriptors, 5 usec
	 * should be more than enough time */
	udelay(VINO_DESC_FETCH_DELAY);

L
Ladislav Michl 已提交
1816 1817 1818
	dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
		ch->start_desc_tbl, ch->next_4_desc);

R
Ralf Baechle 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
	/* set the alpha register */
	ch->alpha = vcs->alpha;

	/* set clipping registers */
	ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
		VINO_CLIP_EVEN(norm->even.top +
			       vcs->clipping.top / 2) |
		VINO_CLIP_X(vcs->clipping.left);
	ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
				     vcs->clipping.bottom / 2 - 1) |
		VINO_CLIP_EVEN(norm->even.top +
			       vcs->clipping.bottom / 2 - 1) |
		VINO_CLIP_X(vcs->clipping.right);

	/* set the size of actual content in the buffer (DECIMATION !) */
	fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
			 vcs->decimation) *
		((vcs->clipping.bottom - vcs->clipping.top) /
		 vcs->decimation) *
		vino_data_formats[vcs->data_format].bpp;

	ch->frame_rate = vcs->framert_reg;

	ctrl = vino->control;
	intr = vino->intr_status;

	if (vcs->channel == VINO_CHANNEL_A) {
		/* All interrupt conditions for this channel was cleared
		 * so clear the interrupt status register and enable
		 * interrupts */
		intr &=	~VINO_INTSTAT_A;
		ctrl |= VINO_CTRL_A_INT;

		/* enable synchronization */
		ctrl |= VINO_CTRL_A_SYNC_ENBL;

		/* enable frame assembly */
		ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;

		/* set decimation used */
		if (vcs->decimation < 2)
			ctrl &= ~VINO_CTRL_A_DEC_ENBL;
		else {
			ctrl |= VINO_CTRL_A_DEC_ENBL;
			ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
			ctrl |= (vcs->decimation - 1) <<
				VINO_CTRL_A_DEC_SCALE_SHIFT;
		}

		/* select input interface */
		if (vcs->input == VINO_INPUT_D1)
			ctrl |= VINO_CTRL_A_SELECT;
		else
			ctrl &= ~VINO_CTRL_A_SELECT;

		/* palette */
		ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
			  VINO_CTRL_A_DITHER);
	} else {
		intr &= ~VINO_INTSTAT_B;
		ctrl |= VINO_CTRL_B_INT;

		ctrl |= VINO_CTRL_B_SYNC_ENBL;
		ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;

		if (vcs->decimation < 2)
			ctrl &= ~VINO_CTRL_B_DEC_ENBL;
		else {
			ctrl |= VINO_CTRL_B_DEC_ENBL;
			ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
			ctrl |= (vcs->decimation - 1) <<
				VINO_CTRL_B_DEC_SCALE_SHIFT;

		}
		if (vcs->input == VINO_INPUT_D1)
			ctrl |= VINO_CTRL_B_SELECT;
		else
			ctrl &= ~VINO_CTRL_B_SELECT;

		ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
			  VINO_CTRL_B_DITHER);
	}

	/* set palette */
	fb->data_format = vcs->data_format;

	switch (vcs->data_format) {
		case VINO_DATA_FMT_GREY:
			ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
				VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
			break;
		case VINO_DATA_FMT_RGB32:
			ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
				VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
			break;
		case VINO_DATA_FMT_YUV:
			/* nothing needs to be done */
			break;
		case VINO_DATA_FMT_RGB332:
			ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
				VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
				VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
			break;
	}

	vino->intr_status = intr;
	vino->control = ctrl;

	return 0;
}

/* (execute only with vino_lock locked) */
L
Ladislav Michl 已提交
1931
static inline void vino_dma_start(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
{
	u32 ctrl = vino->control;

	dprintk("vino_dma_start():\n");
	ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
		VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
	vino->control = ctrl;
}

/* (execute only with vino_lock locked) */
L
Ladislav Michl 已提交
1942
static inline void vino_dma_stop(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
1943 1944 1945 1946 1947
{
	u32 ctrl = vino->control;

	ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
		~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
L
Ladislav Michl 已提交
1948 1949
	ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
		~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
R
Ralf Baechle 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
	vino->control = ctrl;
	dprintk("vino_dma_stop():\n");
}

/*
 * Load dummy page to descriptor registers. This prevents generating of
 * spurious interrupts. (execute only with vino_lock locked)
 */
static void vino_clear_interrupt(struct vino_channel_settings *vcs)
{
	struct sgi_vino_channel *ch;

	ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;

	ch->page_index = 0;
	ch->line_count = 0;

	ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
	ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;

	udelay(VINO_DESC_FETCH_DELAY);
	dprintk("channel %c clear interrupt condition\n",
	       (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
}

static int vino_capture(struct vino_channel_settings *vcs,
			struct vino_framebuffer *fb)
{
	int err = 0;
	unsigned long flags, flags2;

	spin_lock_irqsave(&fb->state_lock, flags);

	if (fb->state == VINO_FRAMEBUFFER_IN_USE)
		err = -EBUSY;
	fb->state = VINO_FRAMEBUFFER_IN_USE;

	spin_unlock_irqrestore(&fb->state_lock, flags);

	if (err)
		return err;

	spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
	spin_lock_irqsave(&vino_drvdata->input_lock, flags2);

	vino_dma_setup(vcs, fb);
	vino_dma_start(vcs);

	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
	spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);

	return err;
}

static
struct vino_framebuffer *vino_capture_enqueue(struct
					      vino_channel_settings *vcs,
					      unsigned int index)
{
	struct vino_framebuffer *fb;
	unsigned long flags;

	dprintk("vino_capture_enqueue():\n");

	spin_lock_irqsave(&vcs->capture_lock, flags);

	fb = vino_queue_add(&vcs->fb_queue, index);
	if (fb == NULL) {
		dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
			"queue full?\n");
		goto out;
	}
out:
	spin_unlock_irqrestore(&vcs->capture_lock, flags);

	return fb;
}

static int vino_capture_next(struct vino_channel_settings *vcs, int start)
{
	struct vino_framebuffer *fb;
	unsigned int incoming, id;
	int err = 0;
L
Ladislav Michl 已提交
2033
	unsigned long flags;
R
Ralf Baechle 已提交
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090

	dprintk("vino_capture_next():\n");

	spin_lock_irqsave(&vcs->capture_lock, flags);

	if (start) {
		/* start capture only if capture isn't in progress already */
		if (vcs->capturing) {
			spin_unlock_irqrestore(&vcs->capture_lock, flags);
			return 0;
		}

	} else {
		/* capture next frame:
		 * stop capture if capturing is not set */
		if (!vcs->capturing) {
			spin_unlock_irqrestore(&vcs->capture_lock, flags);
			return 0;
		}
	}

	err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
	if (err) {
		dprintk("vino_capture_next(): vino_queue_get_incoming() "
			"failed\n");
		err = -EINVAL;
		goto out;
	}
	if (incoming == 0) {
		dprintk("vino_capture_next(): no buffers available\n");
		goto out;
	}

	fb = vino_queue_peek(&vcs->fb_queue, &id);
	if (fb == NULL) {
		dprintk("vino_capture_next(): vino_queue_peek() failed\n");
		err = -EINVAL;
		goto out;
	}

	if (start) {
		vcs->capturing = 1;
	}

	spin_unlock_irqrestore(&vcs->capture_lock, flags);

	err = vino_capture(vcs, fb);

	return err;

out:
	vcs->capturing = 0;
	spin_unlock_irqrestore(&vcs->capture_lock, flags);

	return err;
}

L
Ladislav Michl 已提交
2091
static inline int vino_is_capturing(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
{
	int ret;
	unsigned long flags;

	spin_lock_irqsave(&vcs->capture_lock, flags);

	ret = vcs->capturing;

	spin_unlock_irqrestore(&vcs->capture_lock, flags);

	return ret;
}

/* waits until a frame is captured */
static int vino_wait_for_frame(struct vino_channel_settings *vcs)
{
	wait_queue_t wait;
	int err = 0;

	dprintk("vino_wait_for_frame():\n");

	init_waitqueue_entry(&wait, current);
	/* add ourselves into wait queue */
	add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);

	/* to ensure that schedule_timeout will return immediately
2118
	 * if VINO interrupt was triggered meanwhile */
2119
	schedule_timeout_interruptible(msecs_to_jiffies(100));
R
Ralf Baechle 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200

	if (signal_pending(current))
		err = -EINTR;

	remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);

	dprintk("vino_wait_for_frame(): waiting for frame %s\n",
		err ? "failed" : "ok");

	return err;
}

/* the function assumes that PAGE_SIZE % 4 == 0 */
static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
	unsigned char *pageptr;
	unsigned int page, i;
	unsigned char a;

	for (page = 0; page < fb->desc_table.page_count; page++) {
		pageptr = (unsigned char *)fb->desc_table.virtual[page];

		for (i = 0; i < PAGE_SIZE; i += 4) {
			a = pageptr[0];
			pageptr[0] = pageptr[3];
			pageptr[1] = pageptr[2];
			pageptr[2] = pageptr[1];
			pageptr[3] = a;
			pageptr += 4;
		}
	}
}

/* checks if the buffer is in correct state and syncs data */
static int vino_check_buffer(struct vino_channel_settings *vcs,
			     struct vino_framebuffer *fb)
{
	int err = 0;
	unsigned long flags;

	dprintk("vino_check_buffer():\n");

	spin_lock_irqsave(&fb->state_lock, flags);
	switch (fb->state) {
	case VINO_FRAMEBUFFER_IN_USE:
		err = -EIO;
		break;
	case VINO_FRAMEBUFFER_READY:
		vino_sync_buffer(fb);
		fb->state = VINO_FRAMEBUFFER_UNUSED;
		break;
	default:
		err = -EINVAL;
	}
	spin_unlock_irqrestore(&fb->state_lock, flags);

	if (!err) {
		if (vino_pixel_conversion
		    && (fb->data_format == VINO_DATA_FMT_RGB32)) {
			vino_convert_to_rgba(fb);
		}
	} else if (err && (err != -EINVAL)) {
		dprintk("vino_check_buffer(): buffer not ready\n");

		spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
		vino_dma_stop(vcs);
		vino_clear_interrupt(vcs);
		spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
	}

	return err;
}

/* forcefully terminates capture */
static void vino_capture_stop(struct vino_channel_settings *vcs)
{
	unsigned int incoming = 0, outgoing = 0, id;
	unsigned long flags, flags2;

	dprintk("vino_capture_stop():\n");

	spin_lock_irqsave(&vcs->capture_lock, flags);
L
Ladislav Michl 已提交
2201

R
Ralf Baechle 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
	/* unset capturing to stop queue processing */
	vcs->capturing = 0;

	spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);

	vino_dma_stop(vcs);
	vino_clear_interrupt(vcs);

	spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);

	/* remove all items from the queue */
	if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
		dprintk("vino_capture_stop(): "
			"vino_queue_get_incoming() failed\n");
		goto out;
	}
	while (incoming > 0) {
		vino_queue_transfer(&vcs->fb_queue);

		if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
			dprintk("vino_capture_stop(): "
				"vino_queue_get_incoming() failed\n");
			goto out;
		}
	}

	if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
		dprintk("vino_capture_stop(): "
			"vino_queue_get_outgoing() failed\n");
		goto out;
	}
	while (outgoing > 0) {
		vino_queue_remove(&vcs->fb_queue, &id);

		if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
			dprintk("vino_capture_stop(): "
				"vino_queue_get_outgoing() failed\n");
			goto out;
		}
	}

out:
	spin_unlock_irqrestore(&vcs->capture_lock, flags);
}

L
Ladislav Michl 已提交
2247
#if 0
R
Ralf Baechle 已提交
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
static int vino_capture_failed(struct vino_channel_settings *vcs)
{
	struct vino_framebuffer *fb;
	unsigned long flags;
	unsigned int i;
	int ret;

	dprintk("vino_capture_failed():\n");

	spin_lock_irqsave(&vino_drvdata->vino_lock, flags);

	vino_dma_stop(vcs);
	vino_clear_interrupt(vcs);

	spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);

	ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
	if (ret == VINO_QUEUE_ERROR) {
		dprintk("vino_queue_get_incoming() failed\n");
		return -EINVAL;
	}
	if (i == 0) {
		/* no buffers to process */
		return 0;
	}

	fb = vino_queue_peek(&vcs->fb_queue, &i);
	if (fb == NULL) {
		dprintk("vino_queue_peek() failed\n");
		return -EINVAL;
	}

	spin_lock_irqsave(&fb->state_lock, flags);
	if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
		fb->state = VINO_FRAMEBUFFER_UNUSED;
		vino_queue_transfer(&vcs->fb_queue);
		vino_queue_remove(&vcs->fb_queue, &i);
		/* we should actually discard the newest frame,
		 * but who cares ... */
	}
	spin_unlock_irqrestore(&fb->state_lock, flags);

	return 0;
}
L
Ladislav Michl 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
#endif

static void vino_skip_frame(struct vino_channel_settings *vcs)
{
	struct vino_framebuffer *fb;
	unsigned long flags;
	unsigned int id;

	spin_lock_irqsave(&vcs->capture_lock, flags);
	fb = vino_queue_peek(&vcs->fb_queue, &id);
	if (!fb) {
		spin_unlock_irqrestore(&vcs->capture_lock, flags);
		dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
		return;
	}
	spin_unlock_irqrestore(&vcs->capture_lock, flags);

	spin_lock_irqsave(&fb->state_lock, flags);
	fb->state = VINO_FRAMEBUFFER_UNUSED;
	spin_unlock_irqrestore(&fb->state_lock, flags);

	vino_capture_next(vcs, 0);
}
R
Ralf Baechle 已提交
2315

L
Ladislav Michl 已提交
2316
static void vino_frame_done(struct vino_channel_settings *vcs)
R
Ralf Baechle 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
{
	struct vino_framebuffer *fb;
	unsigned long flags;

	spin_lock_irqsave(&vcs->capture_lock, flags);
	fb = vino_queue_transfer(&vcs->fb_queue);
	if (!fb) {
		spin_unlock_irqrestore(&vcs->capture_lock, flags);
		dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
		return;
	}
	spin_unlock_irqrestore(&vcs->capture_lock, flags);

L
Ladislav Michl 已提交
2330 2331 2332
	fb->frame_counter = vcs->int_data.frame_counter;
	memcpy(&fb->timestamp, &vcs->int_data.timestamp,
	       sizeof(struct timeval));
R
Ralf Baechle 已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343

	spin_lock_irqsave(&fb->state_lock, flags);
	if (fb->state == VINO_FRAMEBUFFER_IN_USE)
		fb->state = VINO_FRAMEBUFFER_READY;
	spin_unlock_irqrestore(&fb->state_lock, flags);

	wake_up(&vcs->fb_queue.frame_wait_queue);

	vino_capture_next(vcs, 0);
}

L
Ladislav Michl 已提交
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
static void vino_capture_tasklet(unsigned long channel) {
	struct vino_channel_settings *vcs;

	vcs = (channel == VINO_CHANNEL_A)
		? &vino_drvdata->a : &vino_drvdata->b;

	if (vcs->int_data.skip)
		vcs->int_data.skip_count++;

	if (vcs->int_data.skip && (vcs->int_data.skip_count
				   <= VINO_MAX_FRAME_SKIP_COUNT)) {
		vino_skip_frame(vcs);
	} else {
		vcs->int_data.skip_count = 0;
		vino_frame_done(vcs);
	}
}

2362
static irqreturn_t vino_interrupt(int irq, void *dev_id)
R
Ralf Baechle 已提交
2363
{
L
Ladislav Michl 已提交
2364
	u32 ctrl, intr;
R
Ralf Baechle 已提交
2365
	unsigned int fc_a, fc_b;
L
Ladislav Michl 已提交
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
	int handled_a = 0, skip_a = 0, done_a = 0;
	int handled_b = 0, skip_b = 0, done_b = 0;

#ifdef VINO_DEBUG_INT
	int loop = 0;
	unsigned int line_count = vino->a.line_count,
		page_index = vino->a.page_index,
		field_counter = vino->a.field_counter,
		start_desc_tbl = vino->a.start_desc_tbl,
		next_4_desc = vino->a.next_4_desc;
	unsigned int line_count_2,
		page_index_2,
		field_counter_2,
		start_desc_tbl_2,
		next_4_desc_2;
#endif
R
Ralf Baechle 已提交
2382 2383 2384

	spin_lock(&vino_drvdata->vino_lock);

L
Ladislav Michl 已提交
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
	while ((intr = vino->intr_status)) {
		fc_a = vino->a.field_counter >> 1;
		fc_b = vino->b.field_counter >> 1;

		/* handle error-interrupts in some special way ?
		 * --> skips frames */
		if (intr & VINO_INTSTAT_A) {
			if (intr & VINO_INTSTAT_A_EOF) {
				vino_drvdata->a.field++;
				if (vino_drvdata->a.field > 1) {
					vino_dma_stop(&vino_drvdata->a);
					vino_clear_interrupt(&vino_drvdata->a);
					vino_drvdata->a.field = 0;
					done_a = 1;
				} else {
					if (vino->a.page_index
					    != vino_drvdata->a.line_size) {
						vino->a.line_count = 0;
						vino->a.page_index =
							vino_drvdata->
							a.line_size;
						vino->a.next_4_desc =
							vino->a.start_desc_tbl;
					}
				}
				dprintk("channel A end-of-field "
					"interrupt: %04x\n", intr);
			} else {
R
Ralf Baechle 已提交
2413 2414 2415
				vino_dma_stop(&vino_drvdata->a);
				vino_clear_interrupt(&vino_drvdata->a);
				vino_drvdata->a.field = 0;
L
Ladislav Michl 已提交
2416 2417 2418
				skip_a = 1;
				dprintk("channel A error interrupt: %04x\n",
					intr);
R
Ralf Baechle 已提交
2419
			}
L
Ladislav Michl 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442

#ifdef VINO_DEBUG_INT
			line_count_2 = vino->a.line_count;
			page_index_2 = vino->a.page_index;
			field_counter_2 = vino->a.field_counter;
			start_desc_tbl_2 = vino->a.start_desc_tbl;
			next_4_desc_2 = vino->a.next_4_desc;

			printk("intr = %04x, loop = %d, field = %d\n",
			       intr, loop, vino_drvdata->a.field);
			printk("1- line count = %04d, page index = %04d, "
			       "start = %08x, next = %08x\n"
			       "   fieldc = %d, framec = %d\n",
			       line_count, page_index, start_desc_tbl,
			       next_4_desc, field_counter, fc_a);
			printk("12-line count = %04d, page index = %04d, "
			       "   start = %08x, next = %08x\n",
			       line_count_2, page_index_2, start_desc_tbl_2,
			       next_4_desc_2);

			if (done_a)
				printk("\n");
#endif
R
Ralf Baechle 已提交
2443
		}
L
Ladislav Michl 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456

		if (intr & VINO_INTSTAT_B) {
			if (intr & VINO_INTSTAT_B_EOF) {
				vino_drvdata->b.field++;
				if (vino_drvdata->b.field > 1) {
					vino_dma_stop(&vino_drvdata->b);
					vino_clear_interrupt(&vino_drvdata->b);
					vino_drvdata->b.field = 0;
					done_b = 1;
				}
				dprintk("channel B end-of-field "
					"interrupt: %04x\n", intr);
			} else {
R
Ralf Baechle 已提交
2457 2458 2459
				vino_dma_stop(&vino_drvdata->b);
				vino_clear_interrupt(&vino_drvdata->b);
				vino_drvdata->b.field = 0;
L
Ladislav Michl 已提交
2460 2461 2462
				skip_b = 1;
				dprintk("channel B error interrupt: %04x\n",
					intr);
R
Ralf Baechle 已提交
2463 2464 2465
			}
		}

L
Ladislav Michl 已提交
2466 2467 2468 2469 2470 2471
		/* Always remember to clear interrupt status.
		 * Disable VINO interrupts while we do this. */
		ctrl = vino->control;
		vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
		vino->intr_status = ~intr;
		vino->control = ctrl;
R
Ralf Baechle 已提交
2472

L
Ladislav Michl 已提交
2473
		spin_unlock(&vino_drvdata->vino_lock);
R
Ralf Baechle 已提交
2474

L
Ladislav Michl 已提交
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
		if ((!handled_a) && (done_a || skip_a)) {
			if (!skip_a) {
				do_gettimeofday(&vino_drvdata->
						a.int_data.timestamp);
				vino_drvdata->a.int_data.frame_counter = fc_a;
			}
			vino_drvdata->a.int_data.skip = skip_a;

			dprintk("channel A %s, interrupt: %d\n",
				skip_a ? "skipping frame" : "frame done",
				intr);
			tasklet_hi_schedule(&vino_tasklet_a);
			handled_a = 1;
		}

		if ((!handled_b) && (done_b || skip_b)) {
			if (!skip_b) {
				do_gettimeofday(&vino_drvdata->
						b.int_data.timestamp);
				vino_drvdata->b.int_data.frame_counter = fc_b;
			}
			vino_drvdata->b.int_data.skip = skip_b;

			dprintk("channel B %s, interrupt: %d\n",
				skip_b ? "skipping frame" : "frame done",
				intr);
			tasklet_hi_schedule(&vino_tasklet_b);
			handled_b = 1;
		}

#ifdef VINO_DEBUG_INT
		loop++;
#endif
		spin_lock(&vino_drvdata->vino_lock);
R
Ralf Baechle 已提交
2509 2510
	}

L
Ladislav Michl 已提交
2511 2512
	spin_unlock(&vino_drvdata->vino_lock);

R
Ralf Baechle 已提交
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
	return IRQ_HANDLED;
}

/* VINO video input management */

static int vino_get_saa7191_input(int input)
{
	switch (input) {
	case VINO_INPUT_COMPOSITE:
		return SAA7191_INPUT_COMPOSITE;
	case VINO_INPUT_SVIDEO:
		return SAA7191_INPUT_SVIDEO;
	default:
		printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
		       "invalid input!\n");
		return -1;
	}
}

/* execute with input_lock locked */
static int vino_is_input_owner(struct vino_channel_settings *vcs)
{
	switch(vcs->input) {
	case VINO_INPUT_COMPOSITE:
	case VINO_INPUT_SVIDEO:
2538
		return vino_drvdata->decoder_owner == vcs->channel;
R
Ralf Baechle 已提交
2539
	case VINO_INPUT_D1:
2540
		return vino_drvdata->camera_owner == vcs->channel;
R
Ralf Baechle 已提交
2541 2542 2543 2544 2545 2546 2547
	default:
		return 0;
	}
}

static int vino_acquire_input(struct vino_channel_settings *vcs)
{
L
Ladislav Michl 已提交
2548
	unsigned long flags;
R
Ralf Baechle 已提交
2549 2550 2551 2552
	int ret = 0;

	dprintk("vino_acquire_input():\n");

L
Ladislav Michl 已提交
2553
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2554 2555

	/* First try D1 and then SAA7191 */
2556 2557 2558
	if (vino_drvdata->camera
	    && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
		vino_drvdata->camera_owner = vcs->channel;
R
Ralf Baechle 已提交
2559 2560
		vcs->input = VINO_INPUT_D1;
		vcs->data_norm = VINO_DATA_NORM_D1;
2561 2562
	} else if (vino_drvdata->decoder
		   && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
2563 2564 2565
		int input;
		int data_norm;
		v4l2_std_id norm;
R
Ralf Baechle 已提交
2566

L
Ladislav Michl 已提交
2567 2568
		input = VINO_INPUT_COMPOSITE;

2569 2570
		ret = decoder_call(video, s_routing,
				vino_get_saa7191_input(input), 0, 0);
L
Ladislav Michl 已提交
2571 2572 2573 2574 2575 2576
		if (ret) {
			ret = -EINVAL;
			goto out;
		}

		spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2577

L
Ladislav Michl 已提交
2578 2579
		/* Don't hold spinlocks while auto-detecting norm
		 * as it may take a while... */
R
Ralf Baechle 已提交
2580

2581
		ret = decoder_call(video, querystd, &norm);
2582 2583 2584 2585 2586 2587 2588
		if (!ret) {
			for (data_norm = 0; data_norm < 3; data_norm++) {
				if (vino_data_norms[data_norm].std & norm)
					break;
			}
			if (data_norm == 3)
				data_norm = VINO_DATA_NORM_PAL;
2589
			ret = decoder_call(core, s_std, norm);
L
Ladislav Michl 已提交
2590 2591 2592 2593 2594 2595 2596 2597 2598
		}

		spin_lock_irqsave(&vino_drvdata->input_lock, flags);

		if (ret) {
			ret = -EINVAL;
			goto out;
		}

2599
		vino_drvdata->decoder_owner = vcs->channel;
L
Ladislav Michl 已提交
2600 2601 2602

		vcs->input = input;
		vcs->data_norm = data_norm;
R
Ralf Baechle 已提交
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
	} else {
		vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
			vino_drvdata->b.input : vino_drvdata->a.input;
		vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
			vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
	}

	if (vcs->input == VINO_INPUT_NONE) {
		ret = -ENODEV;
		goto out;
	}

L
Ladislav Michl 已提交
2615 2616 2617
	vino_set_default_clipping(vcs);
	vino_set_default_scaling(vcs);
	vino_set_default_framerate(vcs);
R
Ralf Baechle 已提交
2618 2619 2620 2621

	dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);

out:
L
Ladislav Michl 已提交
2622
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2623 2624 2625 2626 2627 2628 2629 2630

	return ret;
}

static int vino_set_input(struct vino_channel_settings *vcs, int input)
{
	struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
		&vino_drvdata->b : &vino_drvdata->a;
L
Ladislav Michl 已提交
2631
	unsigned long flags;
R
Ralf Baechle 已提交
2632 2633 2634 2635
	int ret = 0;

	dprintk("vino_set_input():\n");

L
Ladislav Michl 已提交
2636
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2637 2638 2639 2640

	if (vcs->input == input)
		goto out;

L
Ladislav Michl 已提交
2641
	switch (input) {
R
Ralf Baechle 已提交
2642 2643
	case VINO_INPUT_COMPOSITE:
	case VINO_INPUT_SVIDEO:
2644
		if (!vino_drvdata->decoder) {
R
Ralf Baechle 已提交
2645 2646 2647 2648
			ret = -EINVAL;
			goto out;
		}

2649 2650
		if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
			vino_drvdata->decoder_owner = vcs->channel;
R
Ralf Baechle 已提交
2651 2652
		}

2653
		if (vino_drvdata->decoder_owner == vcs->channel) {
L
Ladislav Michl 已提交
2654
			int data_norm;
2655
			v4l2_std_id norm;
R
Ralf Baechle 已提交
2656

2657 2658
			ret = decoder_call(video, s_routing,
					vino_get_saa7191_input(input), 0, 0);
L
Ladislav Michl 已提交
2659
			if (ret) {
2660
				vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
L
Ladislav Michl 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669
				ret = -EINVAL;
				goto out;
			}

			spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);

			/* Don't hold spinlocks while auto-detecting norm
			 * as it may take a while... */

2670
			ret = decoder_call(video, querystd, &norm);
2671 2672 2673 2674 2675 2676 2677
			if (!ret) {
				for (data_norm = 0; data_norm < 3; data_norm++) {
					if (vino_data_norms[data_norm].std & norm)
						break;
				}
				if (data_norm == 3)
					data_norm = VINO_DATA_NORM_PAL;
2678
				ret = decoder_call(core, s_std, norm);
L
Ladislav Michl 已提交
2679
			}
R
Ralf Baechle 已提交
2680

L
Ladislav Michl 已提交
2681 2682 2683
			spin_lock_irqsave(&vino_drvdata->input_lock, flags);

			if (ret) {
2684
				vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
L
Ladislav Michl 已提交
2685 2686 2687 2688 2689 2690
				ret = -EINVAL;
				goto out;
			}

			vcs->input = input;
			vcs->data_norm = data_norm;
R
Ralf Baechle 已提交
2691
		} else {
L
Ladislav Michl 已提交
2692
			if (input != vcs2->input) {
R
Ralf Baechle 已提交
2693 2694 2695 2696 2697 2698 2699 2700
				ret = -EBUSY;
				goto out;
			}

			vcs->input = input;
			vcs->data_norm = vcs2->data_norm;
		}

2701
		if (vino_drvdata->camera_owner == vcs->channel) {
R
Ralf Baechle 已提交
2702 2703
			/* Transfer the ownership or release the input */
			if (vcs2->input == VINO_INPUT_D1) {
2704
				vino_drvdata->camera_owner = vcs2->channel;
R
Ralf Baechle 已提交
2705
			} else {
2706
				vino_drvdata->camera_owner = VINO_NO_CHANNEL;
R
Ralf Baechle 已提交
2707 2708 2709 2710
			}
		}
		break;
	case VINO_INPUT_D1:
2711
		if (!vino_drvdata->camera) {
R
Ralf Baechle 已提交
2712 2713 2714 2715
			ret = -EINVAL;
			goto out;
		}

2716 2717
		if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
			vino_drvdata->camera_owner = vcs->channel;
R
Ralf Baechle 已提交
2718

2719
		if (vino_drvdata->decoder_owner == vcs->channel) {
R
Ralf Baechle 已提交
2720 2721 2722
			/* Transfer the ownership or release the input */
			if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
				 (vcs2->input == VINO_INPUT_SVIDEO)) {
2723
				vino_drvdata->decoder_owner = vcs2->channel;
R
Ralf Baechle 已提交
2724
			} else {
2725
				vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
R
Ralf Baechle 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
			}
		}

		vcs->input = input;
		vcs->data_norm = VINO_DATA_NORM_D1;
		break;
	default:
		ret = -EINVAL;
		goto out;
	}

	vino_set_default_clipping(vcs);
L
Ladislav Michl 已提交
2738
	vino_set_default_scaling(vcs);
R
Ralf Baechle 已提交
2739 2740 2741 2742 2743
	vino_set_default_framerate(vcs);

	dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);

out:
L
Ladislav Michl 已提交
2744
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2745 2746 2747 2748 2749 2750 2751 2752

	return ret;
}

static void vino_release_input(struct vino_channel_settings *vcs)
{
	struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
		&vino_drvdata->b : &vino_drvdata->a;
L
Ladislav Michl 已提交
2753
	unsigned long flags;
R
Ralf Baechle 已提交
2754 2755 2756

	dprintk("vino_release_input():\n");

L
Ladislav Michl 已提交
2757
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2758 2759 2760 2761

	/* Release ownership of the channel
	 * and if the other channel takes input from
	 * the same source, transfer the ownership */
2762
	if (vino_drvdata->camera_owner == vcs->channel) {
R
Ralf Baechle 已提交
2763
		if (vcs2->input == VINO_INPUT_D1) {
2764
			vino_drvdata->camera_owner = vcs2->channel;
R
Ralf Baechle 已提交
2765
		} else {
2766
			vino_drvdata->camera_owner = VINO_NO_CHANNEL;
R
Ralf Baechle 已提交
2767
		}
2768
	} else if (vino_drvdata->decoder_owner == vcs->channel) {
R
Ralf Baechle 已提交
2769 2770
		if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
			 (vcs2->input == VINO_INPUT_SVIDEO)) {
2771
			vino_drvdata->decoder_owner = vcs2->channel;
R
Ralf Baechle 已提交
2772
		} else {
2773
			vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
R
Ralf Baechle 已提交
2774 2775 2776 2777
		}
	}
	vcs->input = VINO_INPUT_NONE;

L
Ladislav Michl 已提交
2778
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2779 2780 2781 2782
}

/* execute with input_lock locked */
static int vino_set_data_norm(struct vino_channel_settings *vcs,
L
Ladislav Michl 已提交
2783 2784
			      unsigned int data_norm,
			      unsigned long *flags)
R
Ralf Baechle 已提交
2785
{
L
Ladislav Michl 已提交
2786 2787 2788 2789
	int err = 0;

	if (data_norm == vcs->data_norm)
		return 0;
R
Ralf Baechle 已提交
2790 2791 2792 2793

	switch (vcs->input) {
	case VINO_INPUT_D1:
		/* only one "norm" supported */
2794
		if (data_norm != VINO_DATA_NORM_D1)
R
Ralf Baechle 已提交
2795 2796 2797
			return -EINVAL;
		break;
	case VINO_INPUT_COMPOSITE:
L
Ladislav Michl 已提交
2798
	case VINO_INPUT_SVIDEO: {
2799 2800
		v4l2_std_id norm;

L
Ladislav Michl 已提交
2801 2802
		if ((data_norm != VINO_DATA_NORM_PAL)
		    && (data_norm != VINO_DATA_NORM_NTSC)
2803
		    && (data_norm != VINO_DATA_NORM_SECAM))
L
Ladislav Michl 已提交
2804
			return -EINVAL;
R
Ralf Baechle 已提交
2805

L
Ladislav Michl 已提交
2806 2807 2808 2809 2810
		spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);

		/* Don't hold spinlocks while setting norm
		 * as it may take a while... */

2811
		norm = vino_data_norms[data_norm].std;
2812
		err = decoder_call(core, s_std, norm);
L
Ladislav Michl 已提交
2813 2814 2815 2816 2817

		spin_lock_irqsave(&vino_drvdata->input_lock, *flags);

		if (err)
			goto out;
R
Ralf Baechle 已提交
2818 2819

		vcs->data_norm = data_norm;
L
Ladislav Michl 已提交
2820 2821 2822 2823

		vino_set_default_clipping(vcs);
		vino_set_default_scaling(vcs);
		vino_set_default_framerate(vcs);
R
Ralf Baechle 已提交
2824
		break;
L
Ladislav Michl 已提交
2825
	}
R
Ralf Baechle 已提交
2826 2827 2828 2829
	default:
		return -EINVAL;
	}

L
Ladislav Michl 已提交
2830 2831
out:
	return err;
R
Ralf Baechle 已提交
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
}

/* V4L2 helper functions */

static int vino_find_data_format(__u32 pixelformat)
{
	int i;

	for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
		if (vino_data_formats[i].pixelformat == pixelformat)
			return i;
	}

	return VINO_DATA_FMT_NONE;
}

2848
static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
R
Ralf Baechle 已提交
2849 2850
{
	int input = VINO_INPUT_NONE;
L
Ladislav Michl 已提交
2851
	unsigned long flags;
R
Ralf Baechle 已提交
2852

L
Ladislav Michl 已提交
2853
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2854
	if (vino_drvdata->decoder && vino_drvdata->camera) {
R
Ralf Baechle 已提交
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
		switch (index) {
		case 0:
			input = VINO_INPUT_COMPOSITE;
			break;
		case 1:
			input = VINO_INPUT_SVIDEO;
			break;
		case 2:
			input = VINO_INPUT_D1;
			break;
		}
2866
	} else if (vino_drvdata->decoder) {
R
Ralf Baechle 已提交
2867 2868 2869 2870 2871 2872 2873 2874
		switch (index) {
		case 0:
			input = VINO_INPUT_COMPOSITE;
			break;
		case 1:
			input = VINO_INPUT_SVIDEO;
			break;
		}
2875
	} else if (vino_drvdata->camera) {
R
Ralf Baechle 已提交
2876 2877 2878 2879 2880 2881
		switch (index) {
		case 0:
			input = VINO_INPUT_D1;
			break;
		}
	}
L
Ladislav Michl 已提交
2882
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892

	return input;
}

/* execute with input_lock locked */
static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
{
	__u32 index = 0;
	// FIXME: detect when no inputs available

2893
	if (vino_drvdata->decoder && vino_drvdata->camera) {
R
Ralf Baechle 已提交
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
		switch (vcs->input) {
		case VINO_INPUT_COMPOSITE:
			index = 0;
			break;
		case VINO_INPUT_SVIDEO:
			index = 1;
			break;
		case VINO_INPUT_D1:
			index = 2;
			break;
		}
2905
	} else if (vino_drvdata->decoder) {
R
Ralf Baechle 已提交
2906 2907 2908 2909 2910 2911 2912 2913
		switch (vcs->input) {
		case VINO_INPUT_COMPOSITE:
			index = 0;
			break;
		case VINO_INPUT_SVIDEO:
			index = 1;
			break;
		}
2914
	} else if (vino_drvdata->camera) {
R
Ralf Baechle 已提交
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
		switch (vcs->input) {
		case VINO_INPUT_D1:
			index = 0;
			break;
		}
	}

	return index;
}

/* V4L2 ioctls */

2927 2928
static int vino_querycap(struct file *file, void *__fh,
		struct v4l2_capability *cap)
R
Ralf Baechle 已提交
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
{
	memset(cap, 0, sizeof(struct v4l2_capability));

	strcpy(cap->driver, vino_driver_name);
	strcpy(cap->card, vino_driver_description);
	strcpy(cap->bus_info, vino_bus_name);
	cap->capabilities =
		V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_STREAMING;
	// V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2939
	return 0;
R
Ralf Baechle 已提交
2940 2941
}

2942
static int vino_enum_input(struct file *file, void *__fh,
R
Ralf Baechle 已提交
2943 2944
			       struct v4l2_input *i)
{
2945
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
2946 2947 2948 2949
	__u32 index = i->index;
	int input;
	dprintk("requested index = %d\n", index);

2950
	input = vino_int_enum_input(vcs, index);
R
Ralf Baechle 已提交
2951 2952 2953 2954 2955 2956 2957
	if (input == VINO_INPUT_NONE)
		return -EINVAL;

	i->type = V4L2_INPUT_TYPE_CAMERA;
	i->std = vino_inputs[input].std;
	strcpy(i->name, vino_inputs[input].name);

2958
	if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2959
		decoder_call(video, g_input_status, &i->status);
R
Ralf Baechle 已提交
2960 2961 2962
	return 0;
}

2963
static int vino_g_input(struct file *file, void *__fh,
L
Ladislav Michl 已提交
2964
			     unsigned int *i)
R
Ralf Baechle 已提交
2965
{
2966
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
2967 2968
	__u32 index;
	int input;
L
Ladislav Michl 已提交
2969
	unsigned long flags;
R
Ralf Baechle 已提交
2970

L
Ladislav Michl 已提交
2971
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2972 2973
	input = vcs->input;
	index = vino_find_input_index(vcs);
L
Ladislav Michl 已提交
2974
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
2975 2976 2977 2978 2979 2980 2981

	dprintk("input = %d\n", input);

	if (input == VINO_INPUT_NONE) {
		return -EINVAL;
	}

L
Ladislav Michl 已提交
2982
	*i = index;
R
Ralf Baechle 已提交
2983 2984 2985 2986

	return 0;
}

2987 2988
static int vino_s_input(struct file *file, void *__fh,
			     unsigned int i)
R
Ralf Baechle 已提交
2989
{
2990
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
2991
	int input;
2992
	dprintk("requested input = %d\n", i);
R
Ralf Baechle 已提交
2993

2994
	input = vino_int_enum_input(vcs, i);
R
Ralf Baechle 已提交
2995 2996 2997 2998 2999 3000
	if (input == VINO_INPUT_NONE)
		return -EINVAL;

	return vino_set_input(vcs, input);
}

3001
static int vino_querystd(struct file *file, void *__fh,
L
Ladislav Michl 已提交
3002 3003
			      v4l2_std_id *std)
{
3004
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
	unsigned long flags;
	int err = 0;

	spin_lock_irqsave(&vino_drvdata->input_lock, flags);

	switch (vcs->input) {
	case VINO_INPUT_D1:
		*std = vino_inputs[vcs->input].std;
		break;
	case VINO_INPUT_COMPOSITE:
	case VINO_INPUT_SVIDEO: {
3016
		decoder_call(video, querystd, std);
L
Ladislav Michl 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
		break;
	}
	default:
		err = -EINVAL;
	}

	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);

	return err;
}

3028
static int vino_g_std(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3029 3030
			   v4l2_std_id *std)
{
3031
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3032 3033 3034 3035
	unsigned long flags;

	spin_lock_irqsave(&vino_drvdata->input_lock, flags);

R
Ralf Baechle 已提交
3036
	*std = vino_data_norms[vcs->data_norm].std;
L
Ladislav Michl 已提交
3037 3038 3039
	dprintk("current standard = %d\n", vcs->data_norm);

	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3040 3041 3042 3043

	return 0;
}

3044
static int vino_s_std(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3045 3046
			   v4l2_std_id *std)
{
3047
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3048
	unsigned long flags;
R
Ralf Baechle 已提交
3049 3050
	int ret = 0;

L
Ladislav Michl 已提交
3051 3052 3053 3054 3055 3056
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);

	if (!vino_is_input_owner(vcs)) {
		ret = -EBUSY;
		goto out;
	}
R
Ralf Baechle 已提交
3057 3058

	/* check if the standard is valid for the current input */
L
Ladislav Michl 已提交
3059
	if ((*std) & vino_inputs[vcs->input].std) {
R
Ralf Baechle 已提交
3060 3061 3062 3063 3064 3065 3066 3067
		dprintk("standard accepted\n");

		/* change the video norm for SAA7191
		 * and accept NTSC for D1 (do nothing) */

		if (vcs->input == VINO_INPUT_D1)
			goto out;

3068
		if ((*std) & V4L2_STD_PAL) {
L
Ladislav Michl 已提交
3069 3070
			ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
						 &flags);
R
Ralf Baechle 已提交
3071
		} else if ((*std) & V4L2_STD_NTSC) {
L
Ladislav Michl 已提交
3072 3073
			ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
						 &flags);
R
Ralf Baechle 已提交
3074
		} else if ((*std) & V4L2_STD_SECAM) {
L
Ladislav Michl 已提交
3075 3076
			ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
						 &flags);
R
Ralf Baechle 已提交
3077 3078 3079
		} else {
			ret = -EINVAL;
		}
L
Ladislav Michl 已提交
3080 3081 3082 3083

		if (ret) {
			ret = -EINVAL;
		}
R
Ralf Baechle 已提交
3084 3085 3086 3087 3088
	} else {
		ret = -EINVAL;
	}

out:
L
Ladislav Michl 已提交
3089
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3090 3091 3092 3093

	return ret;
}

3094
static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3095 3096
			      struct v4l2_fmtdesc *fd)
{
3097
	dprintk("format index = %d\n", fd->index);
3098

3099
	if (fd->index >= VINO_DATA_FMT_COUNT)
R
Ralf Baechle 已提交
3100
		return -EINVAL;
3101 3102 3103 3104
	dprintk("format name = %s\n", vino_data_formats[fd->index].description);

	fd->pixelformat = vino_data_formats[fd->index].pixelformat;
	strcpy(fd->description, vino_data_formats[fd->index].description);
R
Ralf Baechle 已提交
3105 3106 3107
	return 0;
}

3108
static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3109 3110
			     struct v4l2_format *f)
{
3111
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3112
	struct vino_channel_settings tempvcs;
L
Ladislav Michl 已提交
3113
	unsigned long flags;
3114
	struct v4l2_pix_format *pf = &f->fmt.pix;
R
Ralf Baechle 已提交
3115

3116 3117
	dprintk("requested: w = %d, h = %d\n",
			pf->width, pf->height);
R
Ralf Baechle 已提交
3118

3119 3120 3121
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
	memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3122

3123 3124 3125 3126 3127 3128 3129
	tempvcs.data_format = vino_find_data_format(pf->pixelformat);
	if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
		tempvcs.data_format = VINO_DATA_FMT_GREY;
		pf->pixelformat =
			vino_data_formats[tempvcs.data_format].
			pixelformat;
	}
R
Ralf Baechle 已提交
3130

3131 3132
	/* data format must be set before clipping/scaling */
	vino_set_scaling(&tempvcs, pf->width, pf->height);
R
Ralf Baechle 已提交
3133

3134 3135
	dprintk("data format = %s\n",
			vino_data_formats[tempvcs.data_format].description);
R
Ralf Baechle 已提交
3136

3137 3138 3139 3140
	pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
		tempvcs.decimation;
	pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
		tempvcs.decimation;
R
Ralf Baechle 已提交
3141

3142 3143 3144 3145 3146 3147 3148
	pf->field = V4L2_FIELD_INTERLACED;
	pf->bytesperline = tempvcs.line_size;
	pf->sizeimage = tempvcs.line_size *
		(tempvcs.clipping.bottom - tempvcs.clipping.top) /
		tempvcs.decimation;
	pf->colorspace =
		vino_data_formats[tempvcs.data_format].colorspace;
R
Ralf Baechle 已提交
3149

3150
	pf->priv = 0;
R
Ralf Baechle 已提交
3151 3152 3153
	return 0;
}

3154
static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3155 3156
			   struct v4l2_format *f)
{
3157
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3158
	unsigned long flags;
3159
	struct v4l2_pix_format *pf = &f->fmt.pix;
L
Ladislav Michl 已提交
3160

3161
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3162

3163 3164 3165 3166 3167 3168
	pf->width = (vcs->clipping.right - vcs->clipping.left) /
		vcs->decimation;
	pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
		vcs->decimation;
	pf->pixelformat =
		vino_data_formats[vcs->data_format].pixelformat;
R
Ralf Baechle 已提交
3169

3170 3171 3172 3173 3174 3175 3176
	pf->field = V4L2_FIELD_INTERLACED;
	pf->bytesperline = vcs->line_size;
	pf->sizeimage = vcs->line_size *
		(vcs->clipping.bottom - vcs->clipping.top) /
		vcs->decimation;
	pf->colorspace =
		vino_data_formats[vcs->data_format].colorspace;
R
Ralf Baechle 已提交
3177

3178
	pf->priv = 0;
R
Ralf Baechle 已提交
3179

3180
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3181 3182 3183
	return 0;
}

3184
static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3185 3186
			   struct v4l2_format *f)
{
3187
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3188
	int data_format;
L
Ladislav Michl 已提交
3189
	unsigned long flags;
3190
	struct v4l2_pix_format *pf = &f->fmt.pix;
R
Ralf Baechle 已提交
3191

3192
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
L
Ladislav Michl 已提交
3193

3194
	data_format = vino_find_data_format(pf->pixelformat);
R
Ralf Baechle 已提交
3195

3196 3197 3198 3199 3200 3201 3202 3203
	if (data_format == VINO_DATA_FMT_NONE) {
		vcs->data_format = VINO_DATA_FMT_GREY;
		pf->pixelformat =
			vino_data_formats[vcs->data_format].
			pixelformat;
	} else {
		vcs->data_format = data_format;
	}
R
Ralf Baechle 已提交
3204

3205 3206
	/* data format must be set before clipping/scaling */
	vino_set_scaling(vcs, pf->width, pf->height);
R
Ralf Baechle 已提交
3207

3208 3209
	dprintk("data format = %s\n",
	       vino_data_formats[vcs->data_format].description);
R
Ralf Baechle 已提交
3210

3211 3212
	pf->width = vcs->clipping.right - vcs->clipping.left;
	pf->height = vcs->clipping.bottom - vcs->clipping.top;
R
Ralf Baechle 已提交
3213

3214 3215 3216 3217 3218 3219 3220
	pf->field = V4L2_FIELD_INTERLACED;
	pf->bytesperline = vcs->line_size;
	pf->sizeimage = vcs->line_size *
		(vcs->clipping.bottom - vcs->clipping.top) /
		vcs->decimation;
	pf->colorspace =
		vino_data_formats[vcs->data_format].colorspace;
R
Ralf Baechle 已提交
3221

3222
	pf->priv = 0;
R
Ralf Baechle 已提交
3223

3224
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3225 3226 3227
	return 0;
}

3228
static int vino_cropcap(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3229 3230
			     struct v4l2_cropcap *ccap)
{
3231
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3232
	const struct vino_data_norm *norm;
L
Ladislav Michl 已提交
3233
	unsigned long flags;
R
Ralf Baechle 已提交
3234 3235 3236

	switch (ccap->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
L
Ladislav Michl 已提交
3237 3238
		spin_lock_irqsave(&vino_drvdata->input_lock, flags);

R
Ralf Baechle 已提交
3239
		norm = &vino_data_norms[vcs->data_norm];
L
Ladislav Michl 已提交
3240 3241

		spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260

		ccap->bounds.left = 0;
		ccap->bounds.top = 0;
		ccap->bounds.width = norm->width;
		ccap->bounds.height = norm->height;
		memcpy(&ccap->defrect, &ccap->bounds,
		       sizeof(struct v4l2_rect));

		ccap->pixelaspect.numerator = 1;
		ccap->pixelaspect.denominator = 1;
		break;
	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
	default:
		return -EINVAL;
	}

	return 0;
}

3261
static int vino_g_crop(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3262 3263
			    struct v4l2_crop *c)
{
3264
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3265 3266
	unsigned long flags;

R
Ralf Baechle 已提交
3267 3268
	switch (c->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
L
Ladislav Michl 已提交
3269
		spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3270 3271 3272 3273 3274 3275

		c->c.left = vcs->clipping.left;
		c->c.top = vcs->clipping.top;
		c->c.width = vcs->clipping.right - vcs->clipping.left;
		c->c.height = vcs->clipping.bottom - vcs->clipping.top;

L
Ladislav Michl 已提交
3276
		spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3277 3278 3279 3280 3281 3282 3283 3284 3285
		break;
	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
	default:
		return -EINVAL;
	}

	return 0;
}

3286
static int vino_s_crop(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3287 3288
			    struct v4l2_crop *c)
{
3289
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3290 3291
	unsigned long flags;

R
Ralf Baechle 已提交
3292 3293
	switch (c->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
L
Ladislav Michl 已提交
3294
		spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3295 3296 3297 3298

		vino_set_clipping(vcs, c->c.left, c->c.top,
				  c->c.width, c->c.height);

L
Ladislav Michl 已提交
3299
		spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3300 3301 3302 3303 3304 3305 3306 3307 3308
		break;
	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
	default:
		return -EINVAL;
	}

	return 0;
}

3309
static int vino_g_parm(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3310 3311
			    struct v4l2_streamparm *sp)
{
3312
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3313
	unsigned long flags;
3314
	struct v4l2_captureparm *cp = &sp->parm.capture;
L
Ladislav Michl 已提交
3315

3316 3317
	cp->capability = V4L2_CAP_TIMEPERFRAME;
	cp->timeperframe.numerator = 1;
R
Ralf Baechle 已提交
3318

3319
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3320

3321
	cp->timeperframe.denominator = vcs->fps;
L
Ladislav Michl 已提交
3322

3323
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
L
Ladislav Michl 已提交
3324

3325
	/* TODO: cp->readbuffers = xxx; */
R
Ralf Baechle 已提交
3326 3327 3328 3329

	return 0;
}

3330
static int vino_s_parm(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3331 3332
			    struct v4l2_streamparm *sp)
{
3333
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3334
	unsigned long flags;
3335
	struct v4l2_captureparm *cp = &sp->parm.capture;
L
Ladislav Michl 已提交
3336

3337
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3338

3339 3340 3341 3342 3343 3344 3345
	if ((cp->timeperframe.numerator == 0) ||
	    (cp->timeperframe.denominator == 0)) {
		/* reset framerate */
		vino_set_default_framerate(vcs);
	} else {
		vino_set_framerate(vcs, cp->timeperframe.denominator /
				   cp->timeperframe.numerator);
R
Ralf Baechle 已提交
3346 3347
	}

3348 3349
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);

R
Ralf Baechle 已提交
3350 3351 3352
	return 0;
}

3353
static int vino_reqbufs(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3354 3355
			     struct v4l2_requestbuffers *rb)
{
3356
	struct vino_channel_settings *vcs = video_drvdata(file);
3357

R
Ralf Baechle 已提交
3358 3359 3360
	if (vcs->reading)
		return -EBUSY;

3361 3362 3363 3364 3365
	/* TODO: check queue type */
	if (rb->memory != V4L2_MEMORY_MMAP) {
		dprintk("type not mmap\n");
		return -EINVAL;
	}
R
Ralf Baechle 已提交
3366

3367 3368 3369 3370 3371 3372
	dprintk("count = %d\n", rb->count);
	if (rb->count > 0) {
		if (vino_is_capturing(vcs)) {
			dprintk("busy, capturing\n");
			return -EBUSY;
		}
L
Ladislav Michl 已提交
3373

3374 3375 3376
		if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
			dprintk("busy, buffers still mapped\n");
			return -EBUSY;
R
Ralf Baechle 已提交
3377
		} else {
L
Ladislav Michl 已提交
3378
			vcs->streaming = 0;
R
Ralf Baechle 已提交
3379
			vino_queue_free(&vcs->fb_queue);
3380
			vino_queue_init(&vcs->fb_queue, &rb->count);
R
Ralf Baechle 已提交
3381
		}
3382 3383 3384 3385
	} else {
		vcs->streaming = 0;
		vino_capture_stop(vcs);
		vino_queue_free(&vcs->fb_queue);
R
Ralf Baechle 已提交
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
	}

	return 0;
}

static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
					struct vino_framebuffer *fb,
					struct v4l2_buffer *b)
{
	if (vino_queue_outgoing_contains(&vcs->fb_queue,
					 fb->id)) {
		b->flags &= ~V4L2_BUF_FLAG_QUEUED;
		b->flags |= V4L2_BUF_FLAG_DONE;
	} else if (vino_queue_incoming_contains(&vcs->fb_queue,
				       fb->id)) {
		b->flags &= ~V4L2_BUF_FLAG_DONE;
		b->flags |= V4L2_BUF_FLAG_QUEUED;
	} else {
		b->flags &= ~(V4L2_BUF_FLAG_DONE |
			      V4L2_BUF_FLAG_QUEUED);
	}

	b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);

	if (fb->map_count > 0)
		b->flags |= V4L2_BUF_FLAG_MAPPED;

	b->index = fb->id;
	b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
		V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
	b->m.offset = fb->offset;
	b->bytesused = fb->data_size;
	b->length = fb->size;
	b->field = V4L2_FIELD_INTERLACED;
	b->sequence = fb->frame_counter;
	memcpy(&b->timestamp, &fb->timestamp,
	       sizeof(struct timeval));
	// b->input ?

	dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
		fb->id, fb->size, fb->data_size, fb->offset);
}

3429
static int vino_querybuf(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3430 3431
			      struct v4l2_buffer *b)
{
3432
	struct vino_channel_settings *vcs = video_drvdata(file);
3433 3434
	struct vino_framebuffer *fb;

R
Ralf Baechle 已提交
3435 3436 3437
	if (vcs->reading)
		return -EBUSY;

3438 3439 3440 3441 3442
	/* TODO: check queue type */
	if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
		dprintk("invalid index = %d\n",
		       b->index);
		return -EINVAL;
R
Ralf Baechle 已提交
3443
	}
3444 3445 3446 3447 3448

	fb = vino_queue_get_buffer(&vcs->fb_queue,
				   b->index);
	if (fb == NULL) {
		dprintk("vino_queue_get_buffer() failed");
R
Ralf Baechle 已提交
3449 3450 3451
		return -EINVAL;
	}

3452 3453
	vino_v4l2_get_buffer_status(vcs, fb, b);

R
Ralf Baechle 已提交
3454 3455 3456
	return 0;
}

3457
static int vino_qbuf(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3458 3459
			  struct v4l2_buffer *b)
{
3460
	struct vino_channel_settings *vcs = video_drvdata(file);
3461 3462 3463
	struct vino_framebuffer *fb;
	int ret;

R
Ralf Baechle 已提交
3464 3465 3466
	if (vcs->reading)
		return -EBUSY;

3467 3468 3469 3470 3471
	/* TODO: check queue type */
	if (b->memory != V4L2_MEMORY_MMAP) {
		dprintk("type not mmap\n");
		return -EINVAL;
	}
R
Ralf Baechle 已提交
3472

3473 3474 3475
	fb = vino_capture_enqueue(vcs, b->index);
	if (fb == NULL)
		return -EINVAL;
R
Ralf Baechle 已提交
3476

3477
	vino_v4l2_get_buffer_status(vcs, fb, b);
R
Ralf Baechle 已提交
3478

3479 3480 3481 3482
	if (vcs->streaming) {
		ret = vino_capture_next(vcs, 1);
		if (ret)
			return ret;
R
Ralf Baechle 已提交
3483 3484 3485 3486 3487
	}

	return 0;
}

3488 3489
static int vino_dqbuf(struct file *file, void *__fh,
			   struct v4l2_buffer *b)
R
Ralf Baechle 已提交
3490
{
3491 3492
	struct vino_channel_settings *vcs = video_drvdata(file);
	unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3493 3494 3495 3496
	struct vino_framebuffer *fb;
	unsigned int incoming, outgoing;
	int err;

R
Ralf Baechle 已提交
3497 3498 3499
	if (vcs->reading)
		return -EBUSY;

3500
	/* TODO: check queue type */
R
Ralf Baechle 已提交
3501

3502 3503 3504 3505 3506 3507 3508 3509 3510 3511
	err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
	if (err) {
		dprintk("vino_queue_get_incoming() failed\n");
		return -EINVAL;
	}
	err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
	if (err) {
		dprintk("vino_queue_get_outgoing() failed\n");
		return -EINVAL;
	}
R
Ralf Baechle 已提交
3512

3513 3514 3515 3516 3517
	dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);

	if (outgoing == 0) {
		if (incoming == 0) {
			dprintk("no incoming or outgoing buffers\n");
L
Ladislav Michl 已提交
3518
			return -EINVAL;
R
Ralf Baechle 已提交
3519
		}
3520 3521 3522 3523
		if (nonblocking) {
			dprintk("non-blocking I/O was selected and "
				"there are no buffers to dequeue\n");
			return -EAGAIN;
R
Ralf Baechle 已提交
3524 3525
		}

3526 3527
		err = vino_wait_for_frame(vcs);
		if (err) {
R
Ralf Baechle 已提交
3528 3529
			err = vino_wait_for_frame(vcs);
			if (err) {
3530 3531 3532 3533
				/* interrupted or no frames captured because of
				 * frame skipping */
				/* vino_capture_failed(vcs); */
				return -EIO;
R
Ralf Baechle 已提交
3534 3535
			}
		}
3536
	}
R
Ralf Baechle 已提交
3537

3538 3539 3540 3541 3542
	fb = vino_queue_remove(&vcs->fb_queue, &b->index);
	if (fb == NULL) {
		dprintk("vino_queue_remove() failed\n");
		return -EINVAL;
	}
L
Ladislav Michl 已提交
3543

3544
	err = vino_check_buffer(vcs, fb);
L
Ladislav Michl 已提交
3545

3546
	vino_v4l2_get_buffer_status(vcs, fb, b);
R
Ralf Baechle 已提交
3547

3548 3549
	if (err)
		return -EIO;
R
Ralf Baechle 已提交
3550 3551 3552 3553

	return 0;
}

3554 3555
static int vino_streamon(struct file *file, void *__fh,
		enum v4l2_buf_type i)
R
Ralf Baechle 已提交
3556
{
3557
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593
	unsigned int incoming;
	int ret;
	if (vcs->reading)
		return -EBUSY;

	if (vcs->streaming)
		return 0;

	// TODO: check queue type

	if (vino_queue_get_length(&vcs->fb_queue) < 1) {
		dprintk("no buffers allocated\n");
		return -EINVAL;
	}

	ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
	if (ret) {
		dprintk("vino_queue_get_incoming() failed\n");
		return -EINVAL;
	}

	vcs->streaming = 1;

	if (incoming > 0) {
		ret = vino_capture_next(vcs, 1);
		if (ret) {
			vcs->streaming = 0;

			dprintk("couldn't start capture\n");
			return -EINVAL;
		}
	}

	return 0;
}

3594 3595
static int vino_streamoff(struct file *file, void *__fh,
		enum v4l2_buf_type i)
R
Ralf Baechle 已提交
3596
{
3597
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3598 3599 3600 3601 3602 3603 3604
	if (vcs->reading)
		return -EBUSY;

	if (!vcs->streaming)
		return 0;

	vcs->streaming = 0;
L
Ladislav Michl 已提交
3605
	vino_capture_stop(vcs);
R
Ralf Baechle 已提交
3606 3607 3608 3609

	return 0;
}

3610
static int vino_queryctrl(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3611 3612
			       struct v4l2_queryctrl *queryctrl)
{
3613
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3614
	unsigned long flags;
R
Ralf Baechle 已提交
3615 3616 3617
	int i;
	int err = 0;

L
Ladislav Michl 已提交
3618
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3619 3620 3621 3622 3623 3624 3625 3626 3627

	switch (vcs->input) {
	case VINO_INPUT_D1:
		for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
			if (vino_indycam_v4l2_controls[i].id ==
			    queryctrl->id) {
				memcpy(queryctrl,
				       &vino_indycam_v4l2_controls[i],
				       sizeof(struct v4l2_queryctrl));
L
Ladislav Michl 已提交
3628
				queryctrl->reserved[0] = 0;
R
Ralf Baechle 已提交
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
				goto found;
			}
		}

		err =  -EINVAL;
		break;
	case VINO_INPUT_COMPOSITE:
	case VINO_INPUT_SVIDEO:
		for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
			if (vino_saa7191_v4l2_controls[i].id ==
			    queryctrl->id) {
				memcpy(queryctrl,
				       &vino_saa7191_v4l2_controls[i],
				       sizeof(struct v4l2_queryctrl));
L
Ladislav Michl 已提交
3643
				queryctrl->reserved[0] = 0;
R
Ralf Baechle 已提交
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
				goto found;
			}
		}

		err =  -EINVAL;
		break;
	default:
		err =  -EINVAL;
	}

 found:
L
Ladislav Michl 已提交
3655
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3656 3657 3658 3659

	return err;
}

3660
static int vino_g_ctrl(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3661 3662
			    struct v4l2_control *control)
{
3663
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3664 3665
	unsigned long flags;
	int i;
R
Ralf Baechle 已提交
3666 3667
	int err = 0;

L
Ladislav Michl 已提交
3668
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3669 3670

	switch (vcs->input) {
L
Ladislav Michl 已提交
3671
	case VINO_INPUT_D1: {
3672
		err = -EINVAL;
L
Ladislav Michl 已提交
3673
		for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3674 3675 3676
			if (vino_indycam_v4l2_controls[i].id == control->id) {
				err = 0;
				break;
L
Ladislav Michl 已提交
3677 3678 3679
			}
		}

3680
		if (err)
L
Ladislav Michl 已提交
3681 3682
			goto out;

3683
		err = camera_call(core, g_ctrl, control);
3684 3685
		if (err)
			err = -EINVAL;
R
Ralf Baechle 已提交
3686
		break;
L
Ladislav Michl 已提交
3687
	}
R
Ralf Baechle 已提交
3688
	case VINO_INPUT_COMPOSITE:
L
Ladislav Michl 已提交
3689
	case VINO_INPUT_SVIDEO: {
3690
		err = -EINVAL;
L
Ladislav Michl 已提交
3691
		for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3692 3693 3694
			if (vino_saa7191_v4l2_controls[i].id == control->id) {
				err = 0;
				break;
L
Ladislav Michl 已提交
3695 3696 3697
			}
		}

3698
		if (err)
L
Ladislav Michl 已提交
3699 3700
			goto out;

3701
		err = decoder_call(core, g_ctrl, control);
3702 3703
		if (err)
			err = -EINVAL;
R
Ralf Baechle 已提交
3704
		break;
L
Ladislav Michl 已提交
3705
	}
R
Ralf Baechle 已提交
3706 3707 3708 3709
	default:
		err =  -EINVAL;
	}

L
Ladislav Michl 已提交
3710 3711
out:
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3712 3713 3714 3715

	return err;
}

3716
static int vino_s_ctrl(struct file *file, void *__fh,
R
Ralf Baechle 已提交
3717 3718
			    struct v4l2_control *control)
{
3719
	struct vino_channel_settings *vcs = video_drvdata(file);
L
Ladislav Michl 已提交
3720
	unsigned long flags;
R
Ralf Baechle 已提交
3721 3722 3723
	int i;
	int err = 0;

L
Ladislav Michl 已提交
3724 3725 3726 3727 3728 3729
	spin_lock_irqsave(&vino_drvdata->input_lock, flags);

	if (!vino_is_input_owner(vcs)) {
		err = -EBUSY;
		goto out;
	}
R
Ralf Baechle 已提交
3730 3731

	switch (vcs->input) {
L
Ladislav Michl 已提交
3732
	case VINO_INPUT_D1: {
3733
		err = -EINVAL;
R
Ralf Baechle 已提交
3734
		for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3735 3736 3737
			if (vino_indycam_v4l2_controls[i].id == control->id) {
				err = 0;
				break;
R
Ralf Baechle 已提交
3738 3739
			}
		}
3740 3741 3742 3743 3744 3745 3746
		if (err)
			goto out;
		if (control->value < vino_indycam_v4l2_controls[i].minimum ||
		    control->value > vino_indycam_v4l2_controls[i].maximum) {
			err = -ERANGE;
			goto out;
		}
3747
		err = camera_call(core, s_ctrl, control);
L
Ladislav Michl 已提交
3748 3749
		if (err)
			err = -EINVAL;
R
Ralf Baechle 已提交
3750
		break;
L
Ladislav Michl 已提交
3751
	}
R
Ralf Baechle 已提交
3752
	case VINO_INPUT_COMPOSITE:
L
Ladislav Michl 已提交
3753
	case VINO_INPUT_SVIDEO: {
3754
		err = -EINVAL;
R
Ralf Baechle 已提交
3755
		for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3756 3757 3758
			if (vino_saa7191_v4l2_controls[i].id == control->id) {
				err = 0;
				break;
R
Ralf Baechle 已提交
3759 3760
			}
		}
3761 3762 3763 3764 3765 3766 3767
		if (err)
			goto out;
		if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
		    control->value > vino_saa7191_v4l2_controls[i].maximum) {
			err = -ERANGE;
			goto out;
		}
R
Ralf Baechle 已提交
3768

3769
		err = decoder_call(core, s_ctrl, control);
L
Ladislav Michl 已提交
3770 3771
		if (err)
			err = -EINVAL;
R
Ralf Baechle 已提交
3772
		break;
L
Ladislav Michl 已提交
3773
	}
R
Ralf Baechle 已提交
3774 3775 3776 3777
	default:
		err =  -EINVAL;
	}

L
Ladislav Michl 已提交
3778 3779
out:
	spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
R
Ralf Baechle 已提交
3780 3781 3782 3783 3784 3785

	return err;
}

/* File operations */

3786
static int vino_open(struct file *file)
R
Ralf Baechle 已提交
3787
{
3788
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3789 3790 3791 3792
	int ret = 0;
	dprintk("open(): channel = %c\n",
	       (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');

3793
	mutex_lock(&vcs->mutex);
R
Ralf Baechle 已提交
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809

	if (vcs->users) {
		dprintk("open(): driver busy\n");
		ret = -EBUSY;
		goto out;
	}

	ret = vino_acquire_input(vcs);
	if (ret) {
		dprintk("open(): vino_acquire_input() failed\n");
		goto out;
	}

	vcs->users++;

 out:
3810
	mutex_unlock(&vcs->mutex);
R
Ralf Baechle 已提交
3811 3812 3813 3814 3815 3816

	dprintk("open(): %s!\n", ret ? "failed" : "complete");

	return ret;
}

3817
static int vino_close(struct file *file)
R
Ralf Baechle 已提交
3818
{
3819
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3820 3821
	dprintk("close():\n");

3822
	mutex_lock(&vcs->mutex);
R
Ralf Baechle 已提交
3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833

	vcs->users--;

	if (!vcs->users) {
		vino_release_input(vcs);

		/* stop DMA and free buffers */
		vino_capture_stop(vcs);
		vino_queue_free(&vcs->fb_queue);
	}

3834
	mutex_unlock(&vcs->mutex);
R
Ralf Baechle 已提交
3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854

	return 0;
}

static void vino_vm_open(struct vm_area_struct *vma)
{
	struct vino_framebuffer *fb = vma->vm_private_data;

	fb->map_count++;
	dprintk("vino_vm_open(): count = %d\n", fb->map_count);
}

static void vino_vm_close(struct vm_area_struct *vma)
{
	struct vino_framebuffer *fb = vma->vm_private_data;

	fb->map_count--;
	dprintk("vino_vm_close(): count = %d\n", fb->map_count);
}

3855
static const struct vm_operations_struct vino_vm_ops = {
R
Ralf Baechle 已提交
3856 3857 3858 3859 3860 3861
	.open	= vino_vm_open,
	.close	= vino_vm_close,
};

static int vino_mmap(struct file *file, struct vm_area_struct *vma)
{
3862
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875

	unsigned long start = vma->vm_start;
	unsigned long size = vma->vm_end - vma->vm_start;
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;

	struct vino_framebuffer *fb = NULL;
	unsigned int i, length;
	int ret = 0;

	dprintk("mmap():\n");

	// TODO: reject mmap if already mapped

3876
	if (mutex_lock_interruptible(&vcs->mutex))
R
Ralf Baechle 已提交
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
		return -EINTR;

	if (vcs->reading) {
		ret = -EBUSY;
		goto out;
	}

	// TODO: check queue type

	if (!(vma->vm_flags & VM_WRITE)) {
		dprintk("mmap(): app bug: PROT_WRITE please\n");
		ret = -EINVAL;
		goto out;
	}
	if (!(vma->vm_flags & VM_SHARED)) {
		dprintk("mmap(): app bug: MAP_SHARED please\n");
		ret = -EINVAL;
		goto out;
	}

	/* find the correct buffer using offset */
	length = vino_queue_get_length(&vcs->fb_queue);
	if (length == 0) {
		dprintk("mmap(): queue not initialized\n");
		ret = -EINVAL;
		goto out;
	}

	for (i = 0; i < length; i++) {
		fb = vino_queue_get_buffer(&vcs->fb_queue, i);
		if (fb == NULL) {
			dprintk("mmap(): vino_queue_get_buffer() failed\n");
			ret = -EINVAL;
			goto out;
		}

		if (fb->offset == offset)
			goto found;
	}

	dprintk("mmap(): invalid offset = %lu\n", offset);
	ret = -EINVAL;
	goto out;

found:
	dprintk("mmap(): buffer = %d\n", i);

	if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
		dprintk("mmap(): failed: size = %lu > %lu\n",
			size, fb->desc_table.page_count * PAGE_SIZE);
		ret = -EINVAL;
		goto out;
	}

	for (i = 0; i < fb->desc_table.page_count; i++) {
		unsigned long pfn =
			virt_to_phys((void *)fb->desc_table.virtual[i]) >>
			PAGE_SHIFT;

		if (size < PAGE_SIZE)
			break;

		// protection was: PAGE_READONLY
		if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
				    vma->vm_page_prot)) {
			dprintk("mmap(): remap_pfn_range() failed\n");
			ret = -EAGAIN;
			goto out;
		}

		start += PAGE_SIZE;
		size -= PAGE_SIZE;
	}

	fb->map_count = 1;

	vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
	vma->vm_flags &= ~VM_IO;
	vma->vm_private_data = fb;
	vma->vm_file = file;
	vma->vm_ops = &vino_vm_ops;

out:
3960
	mutex_unlock(&vcs->mutex);
R
Ralf Baechle 已提交
3961 3962 3963 3964 3965 3966

	return ret;
}

static unsigned int vino_poll(struct file *file, poll_table *pt)
{
3967
	struct vino_channel_settings *vcs = video_drvdata(file);
R
Ralf Baechle 已提交
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
	unsigned int outgoing;
	unsigned int ret = 0;

	// lock mutex (?)
	// TODO: this has to be corrected for different read modes

	dprintk("poll():\n");

	if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
		dprintk("poll(): vino_queue_get_outgoing() failed\n");
		ret = POLLERR;
		goto error;
	}
	if (outgoing > 0)
		goto over;

	poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);

	if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
		dprintk("poll(): vino_queue_get_outgoing() failed\n");
		ret = POLLERR;
		goto error;
	}

over:
	dprintk("poll(): data %savailable\n",
		(outgoing > 0) ? "" : "not ");
L
Ladislav Michl 已提交
3995 3996

	if (outgoing > 0)
R
Ralf Baechle 已提交
3997 3998 3999 4000 4001 4002
		ret = POLLIN | POLLRDNORM;

error:
	return ret;
}

4003
static long vino_ioctl(struct file *file,
R
Ralf Baechle 已提交
4004 4005
		      unsigned int cmd, unsigned long arg)
{
4006
	struct vino_channel_settings *vcs = video_drvdata(file);
4007
	long ret;
R
Ralf Baechle 已提交
4008

4009
	if (mutex_lock_interruptible(&vcs->mutex))
R
Ralf Baechle 已提交
4010 4011
		return -EINTR;

4012
	ret = video_ioctl2(file, cmd, arg);
R
Ralf Baechle 已提交
4013

4014
	mutex_unlock(&vcs->mutex);
R
Ralf Baechle 已提交
4015 4016 4017 4018 4019 4020

	return ret;
}

/* Initialization and cleanup */

4021 4022
/* __initdata */
static int vino_init_stage;
R
Ralf Baechle 已提交
4023

4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
const struct v4l2_ioctl_ops vino_ioctl_ops = {
	.vidioc_enum_fmt_vid_cap     = vino_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap 	     = vino_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap  	     = vino_s_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap	     = vino_try_fmt_vid_cap,
	.vidioc_querycap    	     = vino_querycap,
	.vidioc_enum_input   	     = vino_enum_input,
	.vidioc_g_input      	     = vino_g_input,
	.vidioc_s_input      	     = vino_s_input,
	.vidioc_g_std 		     = vino_g_std,
	.vidioc_s_std 		     = vino_s_std,
	.vidioc_querystd             = vino_querystd,
	.vidioc_cropcap      	     = vino_cropcap,
	.vidioc_s_crop       	     = vino_s_crop,
	.vidioc_g_crop       	     = vino_g_crop,
	.vidioc_s_parm 		     = vino_s_parm,
	.vidioc_g_parm 		     = vino_g_parm,
	.vidioc_reqbufs              = vino_reqbufs,
	.vidioc_querybuf             = vino_querybuf,
	.vidioc_qbuf                 = vino_qbuf,
	.vidioc_dqbuf                = vino_dqbuf,
	.vidioc_streamon             = vino_streamon,
	.vidioc_streamoff            = vino_streamoff,
	.vidioc_queryctrl            = vino_queryctrl,
	.vidioc_g_ctrl               = vino_g_ctrl,
	.vidioc_s_ctrl               = vino_s_ctrl,
};

4052
static const struct v4l2_file_operations vino_fops = {
R
Ralf Baechle 已提交
4053 4054 4055
	.owner		= THIS_MODULE,
	.open		= vino_open,
	.release	= vino_close,
4056
	.unlocked_ioctl	= vino_ioctl,
R
Ralf Baechle 已提交
4057 4058 4059 4060
	.mmap		= vino_mmap,
	.poll		= vino_poll,
};

4061
static struct video_device vdev_template = {
R
Ralf Baechle 已提交
4062 4063
	.name		= "NOT SET",
	.fops		= &vino_fops,
4064 4065
	.ioctl_ops 	= &vino_ioctl_ops,
	.tvnorms 	= V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
R
Ralf Baechle 已提交
4066 4067 4068 4069 4070
};

static void vino_module_cleanup(int stage)
{
	switch(stage) {
4071
	case 11:
4072 4073
		video_unregister_device(vino_drvdata->b.vdev);
		vino_drvdata->b.vdev = NULL;
4074
	case 10:
4075 4076
		video_unregister_device(vino_drvdata->a.vdev);
		vino_drvdata->a.vdev = NULL;
4077
	case 9:
4078
		i2c_del_adapter(&vino_i2c_adapter);
4079
	case 8:
R
Ralf Baechle 已提交
4080
		free_irq(SGI_VINO_IRQ, NULL);
4081
	case 7:
4082 4083 4084
		if (vino_drvdata->b.vdev) {
			video_device_release(vino_drvdata->b.vdev);
			vino_drvdata->b.vdev = NULL;
R
Ralf Baechle 已提交
4085
		}
4086
	case 6:
4087 4088 4089
		if (vino_drvdata->a.vdev) {
			video_device_release(vino_drvdata->a.vdev);
			vino_drvdata->a.vdev = NULL;
R
Ralf Baechle 已提交
4090
		}
4091
	case 5:
R
Ralf Baechle 已提交
4092 4093 4094 4095 4096 4097 4098 4099 4100
		/* all entries in dma_cpu dummy table have the same address */
		dma_unmap_single(NULL,
				 vino_drvdata->dummy_desc_table.dma_cpu[0],
				 PAGE_SIZE, DMA_FROM_DEVICE);
		dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
				  * sizeof(dma_addr_t),
				  (void *)vino_drvdata->
				  dummy_desc_table.dma_cpu,
				  vino_drvdata->dummy_desc_table.dma);
4101
	case 4:
R
Ralf Baechle 已提交
4102
		free_page(vino_drvdata->dummy_page);
4103 4104
	case 3:
		v4l2_device_unregister(&vino_drvdata->v4l2_dev);
R
Ralf Baechle 已提交
4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150
	case 2:
		kfree(vino_drvdata);
	case 1:
		iounmap(vino);
	case 0:
		break;
	default:
		dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
			stage);
	}
}

static int vino_probe(void)
{
	unsigned long rev_id;

	if (ip22_is_fullhouse()) {
		printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
		return -ENODEV;
	}

	if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
		printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
		return -ENODEV;
	}

	vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
	if (!vino) {
		printk(KERN_ERR "VINO: ioremap() failed\n");
		return -EIO;
	}
	vino_init_stage++;

	if (get_dbe(rev_id, &(vino->rev_id))) {
		printk(KERN_ERR "Failed to read VINO revision register\n");
		vino_module_cleanup(vino_init_stage);
		return -ENODEV;
	}

	if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
		printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
		       rev_id);
		vino_module_cleanup(vino_init_stage);
		return -ENODEV;
	}

L
Ladislav Michl 已提交
4151
	printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
R
Ralf Baechle 已提交
4152 4153 4154 4155 4156 4157 4158

	return 0;
}

static int vino_init(void)
{
	dma_addr_t dma_dummy_address;
4159
	int err;
R
Ralf Baechle 已提交
4160 4161
	int i;

P
 
Panagiotis Issaris 已提交
4162
	vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
R
Ralf Baechle 已提交
4163 4164 4165 4166 4167
	if (!vino_drvdata) {
		vino_module_cleanup(vino_init_stage);
		return -ENOMEM;
	}
	vino_init_stage++;
4168 4169 4170 4171 4172 4173
	strlcpy(vino_drvdata->v4l2_dev.name, "vino",
			sizeof(vino_drvdata->v4l2_dev.name));
	err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
	if (err)
		return err;
	vino_init_stage++;
R
Ralf Baechle 已提交
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232

	/* create a dummy dma descriptor */
	vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
	if (!vino_drvdata->dummy_page) {
		vino_module_cleanup(vino_init_stage);
		return -ENOMEM;
	}
	vino_init_stage++;

	// TODO: use page_count in dummy_desc_table

	vino_drvdata->dummy_desc_table.dma_cpu =
		dma_alloc_coherent(NULL,
		VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
		&vino_drvdata->dummy_desc_table.dma,
		GFP_KERNEL | GFP_DMA);
	if (!vino_drvdata->dummy_desc_table.dma_cpu) {
		vino_module_cleanup(vino_init_stage);
		return -ENOMEM;
	}
	vino_init_stage++;

	dma_dummy_address = dma_map_single(NULL,
					   (void *)vino_drvdata->dummy_page,
					PAGE_SIZE, DMA_FROM_DEVICE);
	for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
		vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
	}

	/* initialize VINO */

	vino->control = 0;
	vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
	vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
	udelay(VINO_DESC_FETCH_DELAY);

	vino->intr_status = 0;

	vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
	vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;

	return 0;
}

static int vino_init_channel_settings(struct vino_channel_settings *vcs,
				 unsigned int channel, const char *name)
{
	vcs->channel = channel;
	vcs->input = VINO_INPUT_NONE;
	vcs->alpha = 0;
	vcs->users = 0;
	vcs->data_format = VINO_DATA_FMT_GREY;
	vcs->data_norm = VINO_DATA_NORM_NTSC;
	vcs->decimation = 1;
	vino_set_default_clipping(vcs);
	vino_set_default_framerate(vcs);

	vcs->capturing = 0;

4233
	mutex_init(&vcs->mutex);
R
Ralf Baechle 已提交
4234 4235
	spin_lock_init(&vcs->capture_lock);

4236
	mutex_init(&vcs->fb_queue.queue_mutex);
R
Ralf Baechle 已提交
4237 4238 4239
	spin_lock_init(&vcs->fb_queue.queue_lock);
	init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);

4240 4241
	vcs->vdev = video_device_alloc();
	if (!vcs->vdev) {
R
Ralf Baechle 已提交
4242 4243 4244 4245 4246
		vino_module_cleanup(vino_init_stage);
		return -ENOMEM;
	}
	vino_init_stage++;

4247
	memcpy(vcs->vdev, &vdev_template,
R
Ralf Baechle 已提交
4248
	       sizeof(struct video_device));
4249 4250
	strcpy(vcs->vdev->name, name);
	vcs->vdev->release = video_device_release;
4251
	vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
R
Ralf Baechle 已提交
4252

4253
	video_set_drvdata(vcs->vdev, vcs);
R
Ralf Baechle 已提交
4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278

	return 0;
}

static int __init vino_module_init(void)
{
	int ret;

	printk(KERN_INFO "SGI VINO driver version %s\n",
	       VINO_MODULE_VERSION);

	ret = vino_probe();
	if (ret)
		return ret;

	ret = vino_init();
	if (ret)
		return ret;

	/* initialize data structures */

	spin_lock_init(&vino_drvdata->vino_lock);
	spin_lock_init(&vino_drvdata->input_lock);

	ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4279
				    vino_vdev_name_a);
R
Ralf Baechle 已提交
4280 4281 4282 4283
	if (ret)
		return ret;

	ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4284
				    vino_vdev_name_b);
R
Ralf Baechle 已提交
4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
	if (ret)
		return ret;

	/* initialize hardware and register V4L devices */

	ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
		vino_driver_description, NULL);
	if (ret) {
		printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
		       SGI_VINO_IRQ);
		vino_module_cleanup(vino_init_stage);
		return -EAGAIN;
	}
	vino_init_stage++;

4300
	ret = i2c_add_adapter(&vino_i2c_adapter);
R
Ralf Baechle 已提交
4301 4302 4303 4304 4305
	if (ret) {
		printk(KERN_ERR "VINO I2C bus registration failed\n");
		vino_module_cleanup(vino_init_stage);
		return ret;
	}
4306
	i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
R
Ralf Baechle 已提交
4307 4308
	vino_init_stage++;

4309
	ret = video_register_device(vino_drvdata->a.vdev,
R
Ralf Baechle 已提交
4310 4311 4312 4313 4314 4315 4316 4317 4318
				    VFL_TYPE_GRABBER, -1);
	if (ret < 0) {
		printk(KERN_ERR "VINO channel A Video4Linux-device "
		       "registration failed\n");
		vino_module_cleanup(vino_init_stage);
		return -EINVAL;
	}
	vino_init_stage++;

4319
	ret = video_register_device(vino_drvdata->b.vdev,
R
Ralf Baechle 已提交
4320 4321 4322 4323 4324 4325 4326 4327 4328
				    VFL_TYPE_GRABBER, -1);
	if (ret < 0) {
		printk(KERN_ERR "VINO channel B Video4Linux-device "
		       "registration failed\n");
		vino_module_cleanup(vino_init_stage);
		return -EINVAL;
	}
	vino_init_stage++;

4329
	vino_drvdata->decoder =
4330
		v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
4331
			       "saa7191", 0, I2C_ADDRS(0x45));
4332
	vino_drvdata->camera =
4333
		v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
4334
			       "indycam", 0, I2C_ADDRS(0x2b));
R
Ralf Baechle 已提交
4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349

	dprintk("init complete!\n");

	return 0;
}

static void __exit vino_module_exit(void)
{
	dprintk("exiting, stage = %d ...\n", vino_init_stage);
	vino_module_cleanup(vino_init_stage);
	dprintk("cleanup complete, exit!\n");
}

module_init(vino_module_init);
module_exit(vino_module_exit);