blob: ca421cc84f8fd95e0a5acff82bd272d5bf7e0463 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechled203a7e2005-09-06 15:19:37 -07002 * Driver for the VINO (Video In No Out) system found in SGI Indys.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License version 2 as published by the Free Software Foundation.
6 *
Ralf Baechled203a7e2005-09-06 15:19:37 -07007 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
8 *
9 * Based on the previous version of the driver for 2.4 kernels by:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
11 */
12
Ralf Baechled203a7e2005-09-06 15:19:37 -070013/*
14 * TODO:
Ladislav Michla637a112005-09-01 15:07:34 +000015 * - remove "mark pages reserved-hacks" from memory allocation code
Nick Piggin3c18ddd2008-04-28 02:12:10 -070016 * and implement fault()
Ralf Baechled203a7e2005-09-06 15:19:37 -070017 * - check decimation, calculating and reporting image size when
18 * using decimation
Ladislav Michla637a112005-09-01 15:07:34 +000019 * - implement read(), user mode buffers and overlay (?)
Ralf Baechled203a7e2005-09-06 15:19:37 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/delay.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000025#include <linux/dma-mapping.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070026#include <linux/errno.h>
27#include <linux/fs.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000028#include <linux/interrupt.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070029#include <linux/kernel.h>
30#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000031#include <linux/time.h>
32#include <linux/version.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070033
34#ifdef CONFIG_KMOD
35#include <linux/kmod.h>
36#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/i2c.h>
39#include <linux/i2c-algo-sgi.h>
40
Hans Verkuil33b687c2008-07-25 05:32:50 -030041#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030042#include <media/v4l2-common.h>
Yoichi Yuasac2cfcf72008-07-29 05:30:58 -030043#include <media/v4l2-ioctl.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070044#include <linux/video_decoder.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020045#include <linux/mutex.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/paccess.h>
48#include <asm/io.h>
49#include <asm/sgi/ip22.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/sgi/mc.h>
51
52#include "vino.h"
Ralf Baechled203a7e2005-09-06 15:19:37 -070053#include "saa7191.h"
54#include "indycam.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Ralf Baechled203a7e2005-09-06 15:19:37 -070056/* Uncomment the following line to get lots and lots of (mostly useless)
57 * debug info.
58 * Note that the debug output also slows down the driver significantly */
59// #define VINO_DEBUG
Ladislav Michla637a112005-09-01 15:07:34 +000060// #define VINO_DEBUG_INT
Ralf Baechled203a7e2005-09-06 15:19:37 -070061
Ladislav Michla637a112005-09-01 15:07:34 +000062#define VINO_MODULE_VERSION "0.0.5"
63#define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
Ralf Baechled203a7e2005-09-06 15:19:37 -070064
65MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
66MODULE_VERSION(VINO_MODULE_VERSION);
67MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
68MODULE_LICENSE("GPL");
69
Ralf Baechled203a7e2005-09-06 15:19:37 -070070#ifdef VINO_DEBUG
71#define dprintk(x...) printk("VINO: " x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
Ralf Baechled203a7e2005-09-06 15:19:37 -070073#define dprintk(x...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
Ralf Baechled203a7e2005-09-06 15:19:37 -070076#define VINO_NO_CHANNEL 0
77#define VINO_CHANNEL_A 1
78#define VINO_CHANNEL_B 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Ralf Baechled203a7e2005-09-06 15:19:37 -070080#define VINO_PAL_WIDTH 768
81#define VINO_PAL_HEIGHT 576
82#define VINO_NTSC_WIDTH 640
83#define VINO_NTSC_HEIGHT 480
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Ralf Baechled203a7e2005-09-06 15:19:37 -070085#define VINO_MIN_WIDTH 32
86#define VINO_MIN_HEIGHT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Ralf Baechled203a7e2005-09-06 15:19:37 -070088#define VINO_CLIPPING_START_ODD_D1 1
Ladislav Michla637a112005-09-01 15:07:34 +000089#define VINO_CLIPPING_START_ODD_PAL 15
90#define VINO_CLIPPING_START_ODD_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070091
92#define VINO_CLIPPING_START_EVEN_D1 2
Ladislav Michla637a112005-09-01 15:07:34 +000093#define VINO_CLIPPING_START_EVEN_PAL 15
94#define VINO_CLIPPING_START_EVEN_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070095
96#define VINO_INPUT_CHANNEL_COUNT 3
97
Ladislav Michla637a112005-09-01 15:07:34 +000098/* the number is the index for vino_inputs */
Ralf Baechled203a7e2005-09-06 15:19:37 -070099#define VINO_INPUT_NONE -1
100#define VINO_INPUT_COMPOSITE 0
101#define VINO_INPUT_SVIDEO 1
102#define VINO_INPUT_D1 2
103
104#define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
105
Ladislav Michla637a112005-09-01 15:07:34 +0000106#define VINO_FIFO_THRESHOLD_DEFAULT 16
Ralf Baechled203a7e2005-09-06 15:19:37 -0700107
Ralf Baechled203a7e2005-09-06 15:19:37 -0700108#define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
109 * VINO_PAL_HEIGHT * 4 \
110 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
111
Ladislav Michla637a112005-09-01 15:07:34 +0000112#define VINO_FRAMEBUFFER_COUNT_MAX 8
Ralf Baechled203a7e2005-09-06 15:19:37 -0700113
114#define VINO_FRAMEBUFFER_UNUSED 0
115#define VINO_FRAMEBUFFER_IN_USE 1
116#define VINO_FRAMEBUFFER_READY 2
117
118#define VINO_QUEUE_ERROR -1
119#define VINO_QUEUE_MAGIC 0x20050125
120
121#define VINO_MEMORY_NONE 0
122#define VINO_MEMORY_MMAP 1
123#define VINO_MEMORY_USERPTR 2
124
125#define VINO_DUMMY_DESC_COUNT 4
126#define VINO_DESC_FETCH_DELAY 5 /* microseconds */
127
Ladislav Michla637a112005-09-01 15:07:34 +0000128#define VINO_MAX_FRAME_SKIP_COUNT 128
129
Ralf Baechled203a7e2005-09-06 15:19:37 -0700130/* the number is the index for vino_data_formats */
131#define VINO_DATA_FMT_NONE -1
132#define VINO_DATA_FMT_GREY 0
133#define VINO_DATA_FMT_RGB332 1
134#define VINO_DATA_FMT_RGB32 2
135#define VINO_DATA_FMT_YUV 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700136
137#define VINO_DATA_FMT_COUNT 4
138
Ladislav Michla637a112005-09-01 15:07:34 +0000139/* the number is the index for vino_data_norms */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700140#define VINO_DATA_NORM_NONE -1
141#define VINO_DATA_NORM_NTSC 0
142#define VINO_DATA_NORM_PAL 1
143#define VINO_DATA_NORM_SECAM 2
144#define VINO_DATA_NORM_D1 3
Ladislav Michla637a112005-09-01 15:07:34 +0000145/* The following are special entries that can be used to
Ralf Baechled203a7e2005-09-06 15:19:37 -0700146 * autodetect the norm. */
Ladislav Michla637a112005-09-01 15:07:34 +0000147#define VINO_DATA_NORM_AUTO 0xfe
148#define VINO_DATA_NORM_AUTO_EXT 0xff
Ralf Baechled203a7e2005-09-06 15:19:37 -0700149
150#define VINO_DATA_NORM_COUNT 4
151
152/* Internal data structure definitions */
153
154struct vino_input {
155 char *name;
156 v4l2_std_id std;
157};
158
159struct vino_clipping {
160 unsigned int left, right, top, bottom;
161};
162
163struct vino_data_format {
164 /* the description */
165 char *description;
166 /* bytes per pixel */
167 unsigned int bpp;
168 /* V4L2 fourcc code */
169 __u32 pixelformat;
170 /* V4L2 colorspace (duh!) */
171 enum v4l2_colorspace colorspace;
172};
173
174struct vino_data_norm {
175 char *description;
176 unsigned int width, height;
177 struct vino_clipping odd;
178 struct vino_clipping even;
179
180 v4l2_std_id std;
181 unsigned int fps_min, fps_max;
182 __u32 framelines;
183};
184
185struct vino_descriptor_table {
186 /* the number of PAGE_SIZE sized pages in the buffer */
187 unsigned int page_count;
188 /* virtual (kmalloc'd) pointers to the actual data
189 * (in PAGE_SIZE chunks, used with mmap streaming) */
190 unsigned long *virtual;
191
192 /* cpu address for the VINO descriptor table
193 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
194 unsigned long *dma_cpu;
195 /* dma address for the VINO descriptor table
196 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
197 dma_addr_t dma;
198};
199
200struct vino_framebuffer {
201 /* identifier nubmer */
202 unsigned int id;
203 /* the length of the whole buffer */
204 unsigned int size;
205 /* the length of actual data in buffer */
206 unsigned int data_size;
207 /* the data format */
208 unsigned int data_format;
209 /* the state of buffer data */
210 unsigned int state;
211 /* is the buffer mapped in user space? */
212 unsigned int map_count;
213 /* memory offset for mmap() */
214 unsigned int offset;
215 /* frame counter */
216 unsigned int frame_counter;
217 /* timestamp (written when image capture finishes) */
218 struct timeval timestamp;
219
220 struct vino_descriptor_table desc_table;
221
222 spinlock_t state_lock;
223};
224
225struct vino_framebuffer_fifo {
226 unsigned int length;
227
228 unsigned int used;
229 unsigned int head;
230 unsigned int tail;
231
Ladislav Michla637a112005-09-01 15:07:34 +0000232 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700233};
234
235struct vino_framebuffer_queue {
236 unsigned int magic;
237
238 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
239 unsigned int type;
240 unsigned int length;
241
242 /* data field of in and out contain index numbers for buffer */
243 struct vino_framebuffer_fifo in;
244 struct vino_framebuffer_fifo out;
245
Ladislav Michla637a112005-09-01 15:07:34 +0000246 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700247
248 spinlock_t queue_lock;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200249 struct mutex queue_mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700250 wait_queue_head_t frame_wait_queue;
251};
252
Ladislav Michla637a112005-09-01 15:07:34 +0000253struct vino_interrupt_data {
254 struct timeval timestamp;
255 unsigned int frame_counter;
256 unsigned int skip_count;
257 unsigned int skip;
258};
259
Ralf Baechled203a7e2005-09-06 15:19:37 -0700260struct vino_channel_settings {
261 unsigned int channel;
262
263 int input;
264 unsigned int data_format;
265 unsigned int data_norm;
266 struct vino_clipping clipping;
267 unsigned int decimation;
268 unsigned int line_size;
269 unsigned int alpha;
270 unsigned int fps;
271 unsigned int framert_reg;
272
273 unsigned int fifo_threshold;
274
275 struct vino_framebuffer_queue fb_queue;
276
277 /* number of the current field */
278 unsigned int field;
279
280 /* read in progress */
281 int reading;
282 /* streaming is active */
283 int streaming;
284 /* the driver is currently processing the queue */
285 int capturing;
286
Ingo Molnar3593cab2006-02-07 06:49:14 -0200287 struct mutex mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700288 spinlock_t capture_lock;
289
290 unsigned int users;
291
Ladislav Michla637a112005-09-01 15:07:34 +0000292 struct vino_interrupt_data int_data;
293
Ralf Baechled203a7e2005-09-06 15:19:37 -0700294 /* V4L support */
295 struct video_device *v4l_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
298struct vino_client {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700299 /* the channel which owns this client:
300 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
301 unsigned int owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct i2c_client *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Ralf Baechled203a7e2005-09-06 15:19:37 -0700305struct vino_settings {
306 struct vino_channel_settings a;
307 struct vino_channel_settings b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 struct vino_client decoder;
310 struct vino_client camera;
311
Ralf Baechled203a7e2005-09-06 15:19:37 -0700312 /* a lock for vino register access */
313 spinlock_t vino_lock;
314 /* a lock for channel input changes */
315 spinlock_t input_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long dummy_page;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700318 struct vino_descriptor_table dummy_desc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Ralf Baechled203a7e2005-09-06 15:19:37 -0700321/* Module parameters */
322
323/*
Ladislav Michla637a112005-09-01 15:07:34 +0000324 * Using vino_pixel_conversion the ABGR32-format pixels supplied
Ralf Baechled203a7e2005-09-06 15:19:37 -0700325 * by the VINO chip can be converted to more common formats
326 * like RGBA32 (or probably RGB24 in the future). This way we
327 * can give out data that can be specified correctly with
328 * the V4L2-definitions.
329 *
330 * The pixel format is specified as RGBA32 when no conversion
331 * is used.
332 *
333 * Note that this only affects the 32-bit bit depth.
334 *
335 * Use non-zero value to enable conversion.
336 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300337static int vino_pixel_conversion;
Ladislav Michla637a112005-09-01 15:07:34 +0000338
Ralf Baechled203a7e2005-09-06 15:19:37 -0700339module_param_named(pixelconv, vino_pixel_conversion, int, 0);
Ladislav Michla637a112005-09-01 15:07:34 +0000340
Ralf Baechled203a7e2005-09-06 15:19:37 -0700341MODULE_PARM_DESC(pixelconv,
342 "enable pixel conversion (non-zero value enables)");
343
344/* Internal data structures */
345
346static struct sgi_vino *vino;
347
348static struct vino_settings *vino_drvdata;
349
350static const char *vino_driver_name = "vino";
351static const char *vino_driver_description = "SGI VINO";
352static const char *vino_bus_name = "GIO64 bus";
353static const char *vino_v4l_device_name_a = "SGI VINO Channel A";
354static const char *vino_v4l_device_name_b = "SGI VINO Channel B";
355
Ladislav Michla637a112005-09-01 15:07:34 +0000356static void vino_capture_tasklet(unsigned long channel);
357
358DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
359DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
360
Ralf Baechled203a7e2005-09-06 15:19:37 -0700361static const struct vino_input vino_inputs[] = {
362 {
363 .name = "Composite",
Ladislav Michla637a112005-09-01 15:07:34 +0000364 .std = V4L2_STD_NTSC | V4L2_STD_PAL
365 | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700366 },{
367 .name = "S-Video",
Ladislav Michla637a112005-09-01 15:07:34 +0000368 .std = V4L2_STD_NTSC | V4L2_STD_PAL
369 | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700370 },{
Ladislav Michla637a112005-09-01 15:07:34 +0000371 .name = "D1/IndyCam",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700372 .std = V4L2_STD_NTSC,
373 }
374};
375
376static const struct vino_data_format vino_data_formats[] = {
377 {
378 .description = "8-bit greyscale",
379 .bpp = 1,
380 .pixelformat = V4L2_PIX_FMT_GREY,
381 .colorspace = V4L2_COLORSPACE_SMPTE170M,
382 },{
383 .description = "8-bit dithered RGB 3-3-2",
384 .bpp = 1,
385 .pixelformat = V4L2_PIX_FMT_RGB332,
386 .colorspace = V4L2_COLORSPACE_SRGB,
387 },{
388 .description = "32-bit RGB",
389 .bpp = 4,
390 .pixelformat = V4L2_PIX_FMT_RGB32,
391 .colorspace = V4L2_COLORSPACE_SRGB,
392 },{
393 .description = "YUV 4:2:2",
Ladislav Michla637a112005-09-01 15:07:34 +0000394 .bpp = 2,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700395 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
396 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Ladislav Michla637a112005-09-01 15:07:34 +0000397 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700398};
399
400static const struct vino_data_norm vino_data_norms[] = {
401 {
402 .description = "NTSC",
403 .std = V4L2_STD_NTSC,
404 .fps_min = 6,
405 .fps_max = 30,
406 .framelines = 525,
407 .width = VINO_NTSC_WIDTH,
408 .height = VINO_NTSC_HEIGHT,
409 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000410 .top = VINO_CLIPPING_START_ODD_NTSC,
411 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700412 .bottom = VINO_CLIPPING_START_ODD_NTSC
413 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000414 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700415 },
416 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000417 .top = VINO_CLIPPING_START_EVEN_NTSC,
418 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700419 .bottom = VINO_CLIPPING_START_EVEN_NTSC
420 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000421 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700422 },
423 },{
424 .description = "PAL",
425 .std = V4L2_STD_PAL,
426 .fps_min = 5,
427 .fps_max = 25,
428 .framelines = 625,
429 .width = VINO_PAL_WIDTH,
430 .height = VINO_PAL_HEIGHT,
431 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000432 .top = VINO_CLIPPING_START_ODD_PAL,
433 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700434 .bottom = VINO_CLIPPING_START_ODD_PAL
435 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000436 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700437 },
438 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000439 .top = VINO_CLIPPING_START_EVEN_PAL,
440 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700441 .bottom = VINO_CLIPPING_START_EVEN_PAL
442 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000443 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700444 },
445 },{
446 .description = "SECAM",
447 .std = V4L2_STD_SECAM,
448 .fps_min = 5,
449 .fps_max = 25,
450 .framelines = 625,
451 .width = VINO_PAL_WIDTH,
452 .height = VINO_PAL_HEIGHT,
453 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000454 .top = VINO_CLIPPING_START_ODD_PAL,
455 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700456 .bottom = VINO_CLIPPING_START_ODD_PAL
457 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000458 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700459 },
460 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000461 .top = VINO_CLIPPING_START_EVEN_PAL,
462 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700463 .bottom = VINO_CLIPPING_START_EVEN_PAL
464 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000465 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700466 },
467 },{
Ladislav Michla637a112005-09-01 15:07:34 +0000468 .description = "NTSC/D1",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700469 .std = V4L2_STD_NTSC,
470 .fps_min = 6,
471 .fps_max = 30,
472 .framelines = 525,
473 .width = VINO_NTSC_WIDTH,
474 .height = VINO_NTSC_HEIGHT,
475 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000476 .top = VINO_CLIPPING_START_ODD_D1,
477 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700478 .bottom = VINO_CLIPPING_START_ODD_D1
479 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000480 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700481 },
482 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000483 .top = VINO_CLIPPING_START_EVEN_D1,
484 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700485 .bottom = VINO_CLIPPING_START_EVEN_D1
486 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000487 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700488 },
489 }
490};
491
492#define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
493
494struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
495 {
496 .id = V4L2_CID_AUTOGAIN,
497 .type = V4L2_CTRL_TYPE_BOOLEAN,
498 .name = "Automatic Gain Control",
499 .minimum = 0,
500 .maximum = 1,
501 .step = 1,
502 .default_value = INDYCAM_AGC_DEFAULT,
503 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000504 .reserved = { INDYCAM_CONTROL_AGC, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700505 },{
506 .id = V4L2_CID_AUTO_WHITE_BALANCE,
507 .type = V4L2_CTRL_TYPE_BOOLEAN,
508 .name = "Automatic White Balance",
509 .minimum = 0,
510 .maximum = 1,
511 .step = 1,
512 .default_value = INDYCAM_AWB_DEFAULT,
513 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000514 .reserved = { INDYCAM_CONTROL_AWB, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700515 },{
516 .id = V4L2_CID_GAIN,
517 .type = V4L2_CTRL_TYPE_INTEGER,
518 .name = "Gain",
519 .minimum = INDYCAM_GAIN_MIN,
520 .maximum = INDYCAM_GAIN_MAX,
521 .step = 1,
522 .default_value = INDYCAM_GAIN_DEFAULT,
523 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000524 .reserved = { INDYCAM_CONTROL_GAIN, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700525 },{
526 .id = V4L2_CID_PRIVATE_BASE,
527 .type = V4L2_CTRL_TYPE_INTEGER,
528 .name = "Red Saturation",
529 .minimum = INDYCAM_RED_SATURATION_MIN,
530 .maximum = INDYCAM_RED_SATURATION_MAX,
531 .step = 1,
532 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
533 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000534 .reserved = { INDYCAM_CONTROL_RED_SATURATION, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700535 },{
536 .id = V4L2_CID_PRIVATE_BASE + 1,
537 .type = V4L2_CTRL_TYPE_INTEGER,
538 .name = "Blue Saturation",
539 .minimum = INDYCAM_BLUE_SATURATION_MIN,
540 .maximum = INDYCAM_BLUE_SATURATION_MAX,
541 .step = 1,
542 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
543 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000544 .reserved = { INDYCAM_CONTROL_BLUE_SATURATION, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700545 },{
546 .id = V4L2_CID_RED_BALANCE,
547 .type = V4L2_CTRL_TYPE_INTEGER,
548 .name = "Red Balance",
549 .minimum = INDYCAM_RED_BALANCE_MIN,
550 .maximum = INDYCAM_RED_BALANCE_MAX,
551 .step = 1,
552 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
553 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000554 .reserved = { INDYCAM_CONTROL_RED_BALANCE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700555 },{
556 .id = V4L2_CID_BLUE_BALANCE,
557 .type = V4L2_CTRL_TYPE_INTEGER,
558 .name = "Blue Balance",
559 .minimum = INDYCAM_BLUE_BALANCE_MIN,
560 .maximum = INDYCAM_BLUE_BALANCE_MAX,
561 .step = 1,
562 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
563 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000564 .reserved = { INDYCAM_CONTROL_BLUE_BALANCE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700565 },{
566 .id = V4L2_CID_EXPOSURE,
567 .type = V4L2_CTRL_TYPE_INTEGER,
568 .name = "Shutter Control",
569 .minimum = INDYCAM_SHUTTER_MIN,
570 .maximum = INDYCAM_SHUTTER_MAX,
571 .step = 1,
572 .default_value = INDYCAM_SHUTTER_DEFAULT,
573 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000574 .reserved = { INDYCAM_CONTROL_SHUTTER, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700575 },{
576 .id = V4L2_CID_GAMMA,
577 .type = V4L2_CTRL_TYPE_INTEGER,
578 .name = "Gamma",
579 .minimum = INDYCAM_GAMMA_MIN,
580 .maximum = INDYCAM_GAMMA_MAX,
581 .step = 1,
582 .default_value = INDYCAM_GAMMA_DEFAULT,
583 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000584 .reserved = { INDYCAM_CONTROL_GAMMA, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700585 }
586};
587
Ladislav Michla637a112005-09-01 15:07:34 +0000588#define VINO_SAA7191_V4L2_CONTROL_COUNT 9
Ralf Baechled203a7e2005-09-06 15:19:37 -0700589
590struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
591 {
592 .id = V4L2_CID_HUE,
593 .type = V4L2_CTRL_TYPE_INTEGER,
594 .name = "Hue",
595 .minimum = SAA7191_HUE_MIN,
596 .maximum = SAA7191_HUE_MAX,
597 .step = 1,
598 .default_value = SAA7191_HUE_DEFAULT,
599 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000600 .reserved = { SAA7191_CONTROL_HUE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700601 },{
602 .id = V4L2_CID_PRIVATE_BASE,
Ladislav Michla637a112005-09-01 15:07:34 +0000603 .type = V4L2_CTRL_TYPE_INTEGER,
604 .name = "Luminance Bandpass",
605 .minimum = SAA7191_BANDPASS_MIN,
606 .maximum = SAA7191_BANDPASS_MAX,
607 .step = 1,
608 .default_value = SAA7191_BANDPASS_DEFAULT,
609 .flags = 0,
610 .reserved = { SAA7191_CONTROL_BANDPASS, 0 },
611 },{
612 .id = V4L2_CID_PRIVATE_BASE + 1,
613 .type = V4L2_CTRL_TYPE_INTEGER,
614 .name = "Luminance Bandpass Weight",
615 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
616 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
617 .step = 1,
618 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
619 .flags = 0,
620 .reserved = { SAA7191_CONTROL_BANDPASS_WEIGHT, 0 },
621 },{
622 .id = V4L2_CID_PRIVATE_BASE + 2,
623 .type = V4L2_CTRL_TYPE_INTEGER,
624 .name = "HF Luminance Coring",
625 .minimum = SAA7191_CORING_MIN,
626 .maximum = SAA7191_CORING_MAX,
627 .step = 1,
628 .default_value = SAA7191_CORING_DEFAULT,
629 .flags = 0,
630 .reserved = { SAA7191_CONTROL_CORING, 0 },
631 },{
632 .id = V4L2_CID_PRIVATE_BASE + 3,
633 .type = V4L2_CTRL_TYPE_BOOLEAN,
634 .name = "Force Colour",
635 .minimum = SAA7191_FORCE_COLOUR_MIN,
636 .maximum = SAA7191_FORCE_COLOUR_MAX,
637 .step = 1,
638 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
639 .flags = 0,
640 .reserved = { SAA7191_CONTROL_FORCE_COLOUR, 0 },
641 },{
642 .id = V4L2_CID_PRIVATE_BASE + 4,
643 .type = V4L2_CTRL_TYPE_INTEGER,
644 .name = "Chrominance Gain Control",
645 .minimum = SAA7191_CHROMA_GAIN_MIN,
646 .maximum = SAA7191_CHROMA_GAIN_MAX,
647 .step = 1,
648 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
649 .flags = 0,
650 .reserved = { SAA7191_CONTROL_CHROMA_GAIN, 0 },
651 },{
652 .id = V4L2_CID_PRIVATE_BASE + 5,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700653 .type = V4L2_CTRL_TYPE_BOOLEAN,
654 .name = "VTR Time Constant",
655 .minimum = SAA7191_VTRC_MIN,
656 .maximum = SAA7191_VTRC_MAX,
657 .step = 1,
658 .default_value = SAA7191_VTRC_DEFAULT,
659 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000660 .reserved = { SAA7191_CONTROL_VTRC, 0 },
661 },{
662 .id = V4L2_CID_PRIVATE_BASE + 6,
663 .type = V4L2_CTRL_TYPE_INTEGER,
664 .name = "Luminance Delay Compensation",
665 .minimum = SAA7191_LUMA_DELAY_MIN,
666 .maximum = SAA7191_LUMA_DELAY_MAX,
667 .step = 1,
668 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
669 .flags = 0,
670 .reserved = { SAA7191_CONTROL_LUMA_DELAY, 0 },
671 },{
672 .id = V4L2_CID_PRIVATE_BASE + 7,
673 .type = V4L2_CTRL_TYPE_INTEGER,
674 .name = "Vertical Noise Reduction",
675 .minimum = SAA7191_VNR_MIN,
676 .maximum = SAA7191_VNR_MAX,
677 .step = 1,
678 .default_value = SAA7191_VNR_DEFAULT,
679 .flags = 0,
680 .reserved = { SAA7191_CONTROL_VNR, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700681 }
682};
683
684/* VINO I2C bus functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686unsigned i2c_vino_getctrl(void *data)
687{
688 return vino->i2c_control;
689}
690
691void i2c_vino_setctrl(void *data, unsigned val)
692{
693 vino->i2c_control = val;
694}
695
696unsigned i2c_vino_rdata(void *data)
697{
698 return vino->i2c_data;
699}
700
701void i2c_vino_wdata(void *data, unsigned val)
702{
703 vino->i2c_data = val;
704}
705
706static struct i2c_algo_sgi_data i2c_sgi_vino_data =
707{
708 .getctrl = &i2c_vino_getctrl,
709 .setctrl = &i2c_vino_setctrl,
710 .rdata = &i2c_vino_rdata,
711 .wdata = &i2c_vino_wdata,
712 .xfer_timeout = 200,
713 .ack_timeout = 1000,
714};
715
716/*
717 * There are two possible clients on VINO I2C bus, so we limit usage only
718 * to them.
719 */
720static int i2c_vino_client_reg(struct i2c_client *client)
721{
Ladislav Michla637a112005-09-01 15:07:34 +0000722 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700723 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Ladislav Michla637a112005-09-01 15:07:34 +0000725 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 switch (client->driver->id) {
727 case I2C_DRIVERID_SAA7191:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700728 if (vino_drvdata->decoder.driver)
729 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700731 vino_drvdata->decoder.driver = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 case I2C_DRIVERID_INDYCAM:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700734 if (vino_drvdata->camera.driver)
735 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700737 vino_drvdata->camera.driver = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739 default:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700740 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Ladislav Michla637a112005-09-01 15:07:34 +0000742 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ralf Baechled203a7e2005-09-06 15:19:37 -0700744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static int i2c_vino_client_unreg(struct i2c_client *client)
748{
Ladislav Michla637a112005-09-01 15:07:34 +0000749 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700750 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ladislav Michla637a112005-09-01 15:07:34 +0000752 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700753 if (client == vino_drvdata->decoder.driver) {
754 if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
755 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700757 vino_drvdata->decoder.driver = NULL;
758 } else if (client == vino_drvdata->camera.driver) {
759 if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
760 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700762 vino_drvdata->camera.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Ladislav Michla637a112005-09-01 15:07:34 +0000764 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Ralf Baechled203a7e2005-09-06 15:19:37 -0700766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769static struct i2c_adapter vino_i2c_adapter =
770{
771 .name = "VINO I2C bus",
772 .id = I2C_HW_SGI_VINO,
773 .algo_data = &i2c_sgi_vino_data,
774 .client_register = &i2c_vino_client_reg,
775 .client_unregister = &i2c_vino_client_unreg,
776};
777
778static int vino_i2c_add_bus(void)
779{
780 return i2c_sgi_add_bus(&vino_i2c_adapter);
781}
782
783static int vino_i2c_del_bus(void)
784{
Jean Delvare32697112006-12-10 21:21:33 +0100785 return i2c_del_adapter(&vino_i2c_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Ralf Baechled203a7e2005-09-06 15:19:37 -0700788static int i2c_camera_command(unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700790 return vino_drvdata->camera.driver->
791 driver->command(vino_drvdata->camera.driver,
792 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Ralf Baechled203a7e2005-09-06 15:19:37 -0700795static int i2c_decoder_command(unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700797 return vino_drvdata->decoder.driver->
798 driver->command(vino_drvdata->decoder.driver,
799 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Ralf Baechled203a7e2005-09-06 15:19:37 -0700802/* VINO framebuffer/DMA descriptor management */
803
804static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
805 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700807 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Ralf Baechled203a7e2005-09-06 15:19:37 -0700809 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Ralf Baechled203a7e2005-09-06 15:19:37 -0700811 for (i = 0; i < count; i++) {
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300812 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700813 dma_unmap_single(NULL,
814 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
815 PAGE_SIZE, DMA_FROM_DEVICE);
816 free_page(fb->desc_table.virtual[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Ralf Baechled203a7e2005-09-06 15:19:37 -0700819 dma_free_coherent(NULL,
820 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
821 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
822 fb->desc_table.dma);
823 kfree(fb->desc_table.virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Ralf Baechled203a7e2005-09-06 15:19:37 -0700825 memset(fb, 0, sizeof(struct vino_framebuffer));
826}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Ralf Baechled203a7e2005-09-06 15:19:37 -0700828static void vino_free_buffer(struct vino_framebuffer *fb)
829{
830 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
831}
832
833static int vino_allocate_buffer(struct vino_framebuffer *fb,
834 unsigned int size)
835{
836 unsigned int count, i, j;
837 int ret = 0;
838
839 dprintk("vino_allocate_buffer():\n");
840
841 if (size < 1)
842 return -EINVAL;
843
844 memset(fb, 0, sizeof(struct vino_framebuffer));
845
846 count = ((size / PAGE_SIZE) + 4) & ~3;
847
848 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
849 size, count);
850
851 /* allocate memory for table with virtual (page) addresses */
852 fb->desc_table.virtual = (unsigned long *)
853 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
854 if (!fb->desc_table.virtual)
855 return -ENOMEM;
856
857 /* allocate memory for table with dma addresses
858 * (has space for four extra descriptors) */
859 fb->desc_table.dma_cpu =
860 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
861 sizeof(dma_addr_t), &fb->desc_table.dma,
862 GFP_KERNEL | GFP_DMA);
863 if (!fb->desc_table.dma_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ret = -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700865 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Ralf Baechled203a7e2005-09-06 15:19:37 -0700868 /* allocate pages for the buffer and acquire the according
869 * dma addresses */
870 for (i = 0; i < count; i++) {
871 dma_addr_t dma_data_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Ralf Baechled203a7e2005-09-06 15:19:37 -0700873 fb->desc_table.virtual[i] =
874 get_zeroed_page(GFP_KERNEL | GFP_DMA);
875 if (!fb->desc_table.virtual[i]) {
876 ret = -ENOBUFS;
877 break;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Ralf Baechled203a7e2005-09-06 15:19:37 -0700880 dma_data_addr =
881 dma_map_single(NULL,
882 (void *)fb->desc_table.virtual[i],
883 PAGE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Ralf Baechled203a7e2005-09-06 15:19:37 -0700885 for (j = 0; j < VINO_PAGE_RATIO; j++) {
886 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
887 dma_data_addr + VINO_PAGE_SIZE * j;
888 }
889
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300890 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Ralf Baechled203a7e2005-09-06 15:19:37 -0700893 /* page_count needs to be set anyway, because the descriptor table has
894 * been allocated according to this number */
895 fb->desc_table.page_count = count;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (ret) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700898 /* the descriptor with index i doesn't contain
899 * a valid address yet */
900 vino_free_buffer_with_count(fb, i);
901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
Ralf Baechled203a7e2005-09-06 15:19:37 -0700904 //fb->size = size;
905 fb->size = count * PAGE_SIZE;
906 fb->data_format = VINO_DATA_FMT_NONE;
907
908 /* set the dma stop-bit for the last (count+1)th descriptor */
909 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
910 return 0;
911
912 out_free_virtual:
913 kfree(fb->desc_table.virtual);
914 return ret;
915}
916
917#if 0
918/* user buffers not fully implemented yet */
919static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
920 void *user,
921 unsigned int size)
922{
923 unsigned int count, i, j;
924 int ret = 0;
925
926 dprintk("vino_prepare_user_buffer():\n");
927
928 if (size < 1)
929 return -EINVAL;
930
931 memset(fb, 0, sizeof(struct vino_framebuffer));
932
933 count = ((size / PAGE_SIZE)) & ~3;
934
935 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
936 size, count);
937
938 /* allocate memory for table with virtual (page) addresses */
939 fb->desc_table.virtual = (unsigned long *)
940 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
941 if (!fb->desc_table.virtual)
942 return -ENOMEM;
943
944 /* allocate memory for table with dma addresses
945 * (has space for four extra descriptors) */
946 fb->desc_table.dma_cpu =
947 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
948 sizeof(dma_addr_t), &fb->desc_table.dma,
949 GFP_KERNEL | GFP_DMA);
950 if (!fb->desc_table.dma_cpu) {
951 ret = -ENOMEM;
952 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700954
955 /* allocate pages for the buffer and acquire the according
956 * dma addresses */
957 for (i = 0; i < count; i++) {
958 dma_addr_t dma_data_addr;
959
960 fb->desc_table.virtual[i] =
961 get_zeroed_page(GFP_KERNEL | GFP_DMA);
962 if (!fb->desc_table.virtual[i]) {
963 ret = -ENOBUFS;
964 break;
965 }
966
967 dma_data_addr =
968 dma_map_single(NULL,
969 (void *)fb->desc_table.virtual[i],
970 PAGE_SIZE, DMA_FROM_DEVICE);
971
972 for (j = 0; j < VINO_PAGE_RATIO; j++) {
973 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
974 dma_data_addr + VINO_PAGE_SIZE * j;
975 }
976
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300977 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700978 }
979
980 /* page_count needs to be set anyway, because the descriptor table has
981 * been allocated according to this number */
982 fb->desc_table.page_count = count;
983
984 if (ret) {
985 /* the descriptor with index i doesn't contain
986 * a valid address yet */
987 vino_free_buffer_with_count(fb, i);
988 return ret;
989 }
990
991 //fb->size = size;
992 fb->size = count * PAGE_SIZE;
993
994 /* set the dma stop-bit for the last (count+1)th descriptor */
995 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
996 return 0;
997
998 out_free_virtual:
999 kfree(fb->desc_table.virtual);
1000 return ret;
1001}
1002#endif
1003
1004static void vino_sync_buffer(struct vino_framebuffer *fb)
1005{
1006 int i;
1007
1008 dprintk("vino_sync_buffer():\n");
1009
1010 for (i = 0; i < fb->desc_table.page_count; i++)
1011 dma_sync_single(NULL,
1012 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
1013 PAGE_SIZE, DMA_FROM_DEVICE);
1014}
1015
1016/* Framebuffer fifo functions (need to be locked externally) */
1017
Ladislav Michla637a112005-09-01 15:07:34 +00001018static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
Ralf Baechled203a7e2005-09-06 15:19:37 -07001019 unsigned int length)
1020{
1021 f->length = 0;
1022 f->used = 0;
1023 f->head = 0;
1024 f->tail = 0;
1025
Ladislav Michla637a112005-09-01 15:07:34 +00001026 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
1027 length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001028
1029 f->length = length;
1030}
1031
1032/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +00001033static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
1034 unsigned int id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001035{
1036 unsigned int i;
Ladislav Michla637a112005-09-01 15:07:34 +00001037
Ralf Baechled203a7e2005-09-06 15:19:37 -07001038 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
1039 if (f->data[i] == id)
1040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Ladislav Michla637a112005-09-01 15:07:34 +00001046#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001047/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +00001048static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001049{
1050 return (f->used == f->length);
1051}
Ladislav Michla637a112005-09-01 15:07:34 +00001052#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001053
Ladislav Michla637a112005-09-01 15:07:34 +00001054static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001055{
1056 return f->used;
1057}
1058
1059static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
1060{
1061 if (id >= f->length) {
1062 return VINO_QUEUE_ERROR;
1063 }
1064
1065 if (vino_fifo_has_id(f, id)) {
1066 return VINO_QUEUE_ERROR;
1067 }
1068
1069 if (f->used < f->length) {
1070 f->data[f->tail] = id;
1071 f->tail = (f->tail + 1) % f->length;
1072 f->used++;
1073 } else {
1074 return VINO_QUEUE_ERROR;
1075 }
1076
1077 return 0;
1078}
1079
1080static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
1081{
1082 if (f->used > 0) {
1083 *id = f->data[f->head];
1084 } else {
1085 return VINO_QUEUE_ERROR;
1086 }
1087
1088 return 0;
1089}
1090
1091static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
1092{
1093 if (f->used > 0) {
1094 *id = f->data[f->head];
1095 f->head = (f->head + 1) % f->length;
1096 f->used--;
1097 } else {
1098 return VINO_QUEUE_ERROR;
1099 }
1100
1101 return 0;
1102}
1103
1104/* Framebuffer queue functions */
1105
1106/* execute with queue_lock locked */
1107static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
1108 unsigned int length)
1109{
1110 unsigned int i;
1111
1112 q->length = 0;
1113 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
1114 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
1115 for (i = 0; i < length; i++) {
1116 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
1117 i);
1118 vino_free_buffer(q->buffer[i]);
1119 kfree(q->buffer[i]);
1120 }
1121
1122 q->type = VINO_MEMORY_NONE;
1123 q->magic = 0;
1124}
1125
1126static void vino_queue_free(struct vino_framebuffer_queue *q)
1127{
1128 dprintk("vino_queue_free():\n");
1129
1130 if (q->magic != VINO_QUEUE_MAGIC)
1131 return;
1132 if (q->type != VINO_MEMORY_MMAP)
1133 return;
1134
Ingo Molnar3593cab2006-02-07 06:49:14 -02001135 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001136
1137 vino_queue_free_with_count(q, q->length);
1138
Ingo Molnar3593cab2006-02-07 06:49:14 -02001139 mutex_unlock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001140}
1141
1142static int vino_queue_init(struct vino_framebuffer_queue *q,
1143 unsigned int *length)
1144{
1145 unsigned int i;
1146 int ret = 0;
1147
1148 dprintk("vino_queue_init(): length = %d\n", *length);
1149
1150 if (q->magic == VINO_QUEUE_MAGIC) {
1151 dprintk("vino_queue_init(): queue already initialized!\n");
1152 return -EINVAL;
1153 }
1154
1155 if (q->type != VINO_MEMORY_NONE) {
1156 dprintk("vino_queue_init(): queue already initialized!\n");
1157 return -EINVAL;
1158 }
1159
1160 if (*length < 1)
1161 return -EINVAL;
1162
Ingo Molnar3593cab2006-02-07 06:49:14 -02001163 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001164
Ladislav Michla637a112005-09-01 15:07:34 +00001165 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1166 *length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001167
1168 q->length = 0;
1169
1170 for (i = 0; i < *length; i++) {
1171 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1172 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1173 GFP_KERNEL);
1174 if (!q->buffer[i]) {
1175 dprintk("vino_queue_init(): kmalloc() failed\n");
1176 ret = -ENOMEM;
1177 break;
1178 }
1179
1180 ret = vino_allocate_buffer(q->buffer[i],
1181 VINO_FRAMEBUFFER_SIZE);
1182 if (ret) {
1183 kfree(q->buffer[i]);
1184 dprintk("vino_queue_init(): "
1185 "vino_allocate_buffer() failed\n");
1186 break;
1187 }
1188
1189 q->buffer[i]->id = i;
1190 if (i > 0) {
1191 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1192 q->buffer[i - 1]->size;
1193 } else {
1194 q->buffer[i]->offset = 0;
1195 }
1196
1197 spin_lock_init(&q->buffer[i]->state_lock);
1198
1199 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1200 "size = %d\n", i, q->buffer[i]->offset,
1201 q->buffer[i]->size);
1202 }
1203
1204 if (ret) {
1205 vino_queue_free_with_count(q, i);
1206 *length = 0;
1207 } else {
1208 q->length = *length;
1209 vino_fifo_init(&q->in, q->length);
1210 vino_fifo_init(&q->out, q->length);
1211 q->type = VINO_MEMORY_MMAP;
1212 q->magic = VINO_QUEUE_MAGIC;
1213 }
1214
Ingo Molnar3593cab2006-02-07 06:49:14 -02001215 mutex_unlock(&q->queue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 return ret;
1218}
1219
Ralf Baechled203a7e2005-09-06 15:19:37 -07001220static struct vino_framebuffer *vino_queue_add(struct
1221 vino_framebuffer_queue *q,
1222 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Ralf Baechled203a7e2005-09-06 15:19:37 -07001224 struct vino_framebuffer *ret = NULL;
1225 unsigned int total;
1226 unsigned long flags;
1227
1228 dprintk("vino_queue_add(): id = %d\n", id);
1229
1230 if (q->magic != VINO_QUEUE_MAGIC) {
1231 return ret;
1232 }
1233
1234 spin_lock_irqsave(&q->queue_lock, flags);
1235
1236 if (q->length == 0)
1237 goto out;
1238
1239 if (id >= q->length)
1240 goto out;
1241
1242 /* not needed?: if (vino_fifo_full(&q->out)) {
1243 goto out;
1244 }*/
1245 /* check that outgoing queue isn't already full
1246 * (or that it won't become full) */
1247 total = vino_fifo_get_used(&q->in) +
1248 vino_fifo_get_used(&q->out);
1249 if (total >= q->length)
1250 goto out;
1251
1252 if (vino_fifo_enqueue(&q->in, id))
1253 goto out;
1254
1255 ret = q->buffer[id];
1256
1257out:
1258 spin_unlock_irqrestore(&q->queue_lock, flags);
1259
1260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Ralf Baechled203a7e2005-09-06 15:19:37 -07001263static struct vino_framebuffer *vino_queue_transfer(struct
1264 vino_framebuffer_queue *q)
1265{
1266 struct vino_framebuffer *ret = NULL;
1267 struct vino_framebuffer *fb;
1268 int id;
1269 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Ralf Baechled203a7e2005-09-06 15:19:37 -07001271 dprintk("vino_queue_transfer():\n");
1272
1273 if (q->magic != VINO_QUEUE_MAGIC) {
1274 return ret;
1275 }
1276
1277 spin_lock_irqsave(&q->queue_lock, flags);
1278
1279 if (q->length == 0)
1280 goto out;
1281
1282 // now this actually removes an entry from the incoming queue
1283 if (vino_fifo_dequeue(&q->in, &id)) {
1284 goto out;
1285 }
1286
1287 dprintk("vino_queue_transfer(): id = %d\n", id);
1288 fb = q->buffer[id];
1289
1290 // we have already checked that the outgoing queue is not full, but...
1291 if (vino_fifo_enqueue(&q->out, id)) {
1292 printk(KERN_ERR "vino_queue_transfer(): "
1293 "outgoing queue is full, this shouldn't happen!\n");
1294 goto out;
1295 }
1296
1297 ret = fb;
1298out:
1299 spin_unlock_irqrestore(&q->queue_lock, flags);
1300
1301 return ret;
1302}
1303
1304/* returns true/false */
1305static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1306 unsigned int id)
1307{
1308 int ret = 0;
1309 unsigned long flags;
1310
1311 if (q->magic != VINO_QUEUE_MAGIC) {
1312 return ret;
1313 }
1314
1315 spin_lock_irqsave(&q->queue_lock, flags);
1316
1317 if (q->length == 0)
1318 goto out;
1319
1320 ret = vino_fifo_has_id(&q->in, id);
1321
1322out:
1323 spin_unlock_irqrestore(&q->queue_lock, flags);
1324
1325 return ret;
1326}
1327
1328/* returns true/false */
1329static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1330 unsigned int id)
1331{
1332 int ret = 0;
1333 unsigned long flags;
1334
1335 if (q->magic != VINO_QUEUE_MAGIC) {
1336 return ret;
1337 }
1338
1339 spin_lock_irqsave(&q->queue_lock, flags);
1340
1341 if (q->length == 0)
1342 goto out;
1343
1344 ret = vino_fifo_has_id(&q->out, id);
1345
1346out:
1347 spin_unlock_irqrestore(&q->queue_lock, flags);
1348
1349 return ret;
1350}
1351
1352static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1353 unsigned int *used)
1354{
1355 int ret = 0;
1356 unsigned long flags;
1357
1358 if (q->magic != VINO_QUEUE_MAGIC) {
1359 return VINO_QUEUE_ERROR;
1360 }
1361
1362 spin_lock_irqsave(&q->queue_lock, flags);
1363
1364 if (q->length == 0) {
1365 ret = VINO_QUEUE_ERROR;
1366 goto out;
1367 }
1368
1369 *used = vino_fifo_get_used(&q->in);
1370
1371out:
1372 spin_unlock_irqrestore(&q->queue_lock, flags);
1373
1374 return ret;
1375}
1376
1377static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1378 unsigned int *used)
1379{
1380 int ret = 0;
1381 unsigned long flags;
1382
1383 if (q->magic != VINO_QUEUE_MAGIC) {
1384 return VINO_QUEUE_ERROR;
1385 }
1386
1387 spin_lock_irqsave(&q->queue_lock, flags);
1388
1389 if (q->length == 0) {
1390 ret = VINO_QUEUE_ERROR;
1391 goto out;
1392 }
1393
1394 *used = vino_fifo_get_used(&q->out);
1395
1396out:
1397 spin_unlock_irqrestore(&q->queue_lock, flags);
1398
1399 return ret;
1400}
1401
Ladislav Michla637a112005-09-01 15:07:34 +00001402#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001403static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1404 unsigned int *total)
1405{
1406 int ret = 0;
1407 unsigned long flags;
1408
1409 if (q->magic != VINO_QUEUE_MAGIC) {
1410 return VINO_QUEUE_ERROR;
1411 }
1412
1413 spin_lock_irqsave(&q->queue_lock, flags);
1414
1415 if (q->length == 0) {
1416 ret = VINO_QUEUE_ERROR;
1417 goto out;
1418 }
1419
1420 *total = vino_fifo_get_used(&q->in) +
1421 vino_fifo_get_used(&q->out);
1422
1423out:
1424 spin_unlock_irqrestore(&q->queue_lock, flags);
1425
1426 return ret;
1427}
Ladislav Michla637a112005-09-01 15:07:34 +00001428#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001429
1430static struct vino_framebuffer *vino_queue_peek(struct
1431 vino_framebuffer_queue *q,
1432 unsigned int *id)
1433{
1434 struct vino_framebuffer *ret = NULL;
1435 unsigned long flags;
1436
1437 if (q->magic != VINO_QUEUE_MAGIC) {
1438 return ret;
1439 }
1440
1441 spin_lock_irqsave(&q->queue_lock, flags);
1442
1443 if (q->length == 0)
1444 goto out;
1445
1446 if (vino_fifo_peek(&q->in, id)) {
1447 goto out;
1448 }
1449
1450 ret = q->buffer[*id];
1451out:
1452 spin_unlock_irqrestore(&q->queue_lock, flags);
1453
1454 return ret;
1455}
1456
1457static struct vino_framebuffer *vino_queue_remove(struct
1458 vino_framebuffer_queue *q,
1459 unsigned int *id)
1460{
1461 struct vino_framebuffer *ret = NULL;
1462 unsigned long flags;
1463 dprintk("vino_queue_remove():\n");
1464
1465 if (q->magic != VINO_QUEUE_MAGIC) {
1466 return ret;
1467 }
1468
1469 spin_lock_irqsave(&q->queue_lock, flags);
1470
1471 if (q->length == 0)
1472 goto out;
1473
1474 if (vino_fifo_dequeue(&q->out, id)) {
1475 goto out;
1476 }
1477
1478 dprintk("vino_queue_remove(): id = %d\n", *id);
1479 ret = q->buffer[*id];
1480out:
1481 spin_unlock_irqrestore(&q->queue_lock, flags);
1482
1483 return ret;
1484}
1485
1486static struct
1487vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1488 unsigned int id)
1489{
1490 struct vino_framebuffer *ret = NULL;
1491 unsigned long flags;
1492
1493 if (q->magic != VINO_QUEUE_MAGIC) {
1494 return ret;
1495 }
1496
1497 spin_lock_irqsave(&q->queue_lock, flags);
1498
1499 if (q->length == 0)
1500 goto out;
1501
1502 if (id >= q->length)
1503 goto out;
1504
1505 ret = q->buffer[id];
1506 out:
1507 spin_unlock_irqrestore(&q->queue_lock, flags);
1508
1509 return ret;
1510}
1511
1512static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1513{
1514 unsigned int length = 0;
1515 unsigned long flags;
1516
1517 if (q->magic != VINO_QUEUE_MAGIC) {
1518 return length;
1519 }
1520
1521 spin_lock_irqsave(&q->queue_lock, flags);
1522 length = q->length;
1523 spin_unlock_irqrestore(&q->queue_lock, flags);
1524
1525 return length;
1526}
1527
1528static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1529{
1530 unsigned int i;
1531 int ret = 0;
1532 unsigned long flags;
1533
1534 if (q->magic != VINO_QUEUE_MAGIC) {
1535 return ret;
1536 }
1537
1538 spin_lock_irqsave(&q->queue_lock, flags);
1539 for (i = 0; i < q->length; i++) {
1540 if (q->buffer[i]->map_count > 0) {
1541 ret = 1;
1542 break;
1543 }
1544 }
1545 spin_unlock_irqrestore(&q->queue_lock, flags);
1546
1547 return ret;
1548}
1549
1550/* VINO functions */
1551
1552/* execute with input_lock locked */
1553static void vino_update_line_size(struct vino_channel_settings *vcs)
1554{
1555 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1556 unsigned int d = vcs->decimation;
1557 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001558 unsigned int lsize;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001559
1560 dprintk("update_line_size(): before: w = %d, d = %d, "
1561 "line_size = %d\n", w, d, vcs->line_size);
Ladislav Michla637a112005-09-01 15:07:34 +00001562
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001563 /* line size must be multiple of 8 bytes */
Ralf Baechled203a7e2005-09-06 15:19:37 -07001564 lsize = (bpp * (w / d)) & ~7;
1565 w = (lsize / bpp) * d;
1566
1567 vcs->clipping.right = vcs->clipping.left + w;
1568 vcs->line_size = lsize;
Ladislav Michla637a112005-09-01 15:07:34 +00001569
Ralf Baechled203a7e2005-09-06 15:19:37 -07001570 dprintk("update_line_size(): after: w = %d, d = %d, "
1571 "line_size = %d\n", w, d, vcs->line_size);
1572}
1573
1574/* execute with input_lock locked */
1575static void vino_set_clipping(struct vino_channel_settings *vcs,
1576 unsigned int x, unsigned int y,
1577 unsigned int w, unsigned int h)
1578{
1579 unsigned int maxwidth, maxheight;
1580 unsigned int d;
1581
1582 maxwidth = vino_data_norms[vcs->data_norm].width;
1583 maxheight = vino_data_norms[vcs->data_norm].height;
1584 d = vcs->decimation;
1585
1586 y &= ~1; /* odd/even fields */
1587
1588 if (x > maxwidth) {
1589 x = 0;
1590 }
1591 if (y > maxheight) {
1592 y = 0;
1593 }
1594
1595 if (((w / d) < VINO_MIN_WIDTH)
1596 || ((h / d) < VINO_MIN_HEIGHT)) {
1597 w = VINO_MIN_WIDTH * d;
1598 h = VINO_MIN_HEIGHT * d;
1599 }
1600
1601 if ((x + w) > maxwidth) {
1602 w = maxwidth - x;
1603 if ((w / d) < VINO_MIN_WIDTH)
1604 x = maxwidth - VINO_MIN_WIDTH * d;
1605 }
1606 if ((y + h) > maxheight) {
1607 h = maxheight - y;
1608 if ((h / d) < VINO_MIN_HEIGHT)
1609 y = maxheight - VINO_MIN_HEIGHT * d;
1610 }
1611
1612 vcs->clipping.left = x;
1613 vcs->clipping.top = y;
1614 vcs->clipping.right = x + w;
1615 vcs->clipping.bottom = y + h;
1616
1617 vino_update_line_size(vcs);
1618
1619 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1620 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1621 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1622}
1623
1624/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001625static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001626{
1627 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1628 vino_data_norms[vcs->data_norm].height);
1629}
1630
1631/* execute with input_lock locked */
1632static void vino_set_scaling(struct vino_channel_settings *vcs,
1633 unsigned int w, unsigned int h)
1634{
1635 unsigned int x, y, curw, curh, d;
1636
1637 x = vcs->clipping.left;
1638 y = vcs->clipping.top;
1639 curw = vcs->clipping.right - vcs->clipping.left;
1640 curh = vcs->clipping.bottom - vcs->clipping.top;
1641
1642 d = max(curw / w, curh / h);
1643
1644 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1645 w, h, curw, curh, d);
1646
1647 if (d < 1) {
1648 d = 1;
Ladislav Michla637a112005-09-01 15:07:34 +00001649 } else if (d > 8) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07001650 d = 8;
1651 }
1652
1653 vcs->decimation = d;
1654 vino_set_clipping(vcs, x, y, w * d, h * d);
1655
1656 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1657 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1658 vcs->decimation, vcs->line_size);
1659}
1660
1661/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001662static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001663{
1664 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1665 vcs->clipping.bottom - vcs->clipping.top);
1666}
1667
1668/* execute with input_lock locked */
1669static void vino_set_framerate(struct vino_channel_settings *vcs,
1670 unsigned int fps)
1671{
1672 unsigned int mask;
1673
1674 switch (vcs->data_norm) {
1675 case VINO_DATA_NORM_NTSC:
1676 case VINO_DATA_NORM_D1:
1677 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1678
1679 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1680 fps = vino_data_norms[vcs->data_norm].fps_min;
1681 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1682 fps = vino_data_norms[vcs->data_norm].fps_max;
1683
1684 switch (fps) {
1685 case 6:
1686 mask = 0x003;
1687 break;
1688 case 12:
1689 mask = 0x0c3;
1690 break;
1691 case 18:
1692 mask = 0x333;
1693 break;
1694 case 24:
1695 mask = 0x3ff;
1696 break;
1697 case 30:
1698 mask = 0xfff;
1699 break;
1700 default:
1701 mask = VINO_FRAMERT_FULL;
1702 }
1703 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1704 break;
1705 case VINO_DATA_NORM_PAL:
1706 case VINO_DATA_NORM_SECAM:
1707 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1708
1709 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1710 fps = vino_data_norms[vcs->data_norm].fps_min;
1711 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1712 fps = vino_data_norms[vcs->data_norm].fps_max;
1713
1714 switch (fps) {
1715 case 5:
1716 mask = 0x003;
1717 break;
1718 case 10:
1719 mask = 0x0c3;
1720 break;
1721 case 15:
1722 mask = 0x333;
1723 break;
1724 case 20:
1725 mask = 0x0ff;
1726 break;
1727 case 25:
1728 mask = 0x3ff;
1729 break;
1730 default:
1731 mask = VINO_FRAMERT_FULL;
1732 }
1733 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1734 break;
1735 }
1736
1737 vcs->fps = fps;
1738}
1739
1740/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001741static inline void vino_set_default_framerate(struct
1742 vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001743{
1744 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1745}
1746
1747/*
1748 * Prepare VINO for DMA transfer...
1749 * (execute only with vino_lock and input_lock locked)
1750 */
1751static int vino_dma_setup(struct vino_channel_settings *vcs,
1752 struct vino_framebuffer *fb)
1753{
1754 u32 ctrl, intr;
1755 struct sgi_vino_channel *ch;
1756 const struct vino_data_norm *norm;
1757
1758 dprintk("vino_dma_setup():\n");
1759
1760 vcs->field = 0;
1761 fb->frame_counter = 0;
1762
1763 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1764 norm = &vino_data_norms[vcs->data_norm];
1765
1766 ch->page_index = 0;
1767 ch->line_count = 0;
1768
1769 /* VINO line size register is set 8 bytes less than actual */
1770 ch->line_size = vcs->line_size - 8;
1771
1772 /* let VINO know where to transfer data */
1773 ch->start_desc_tbl = fb->desc_table.dma;
1774 ch->next_4_desc = fb->desc_table.dma;
1775
1776 /* give vino time to fetch the first four descriptors, 5 usec
1777 * should be more than enough time */
1778 udelay(VINO_DESC_FETCH_DELAY);
1779
Ladislav Michla637a112005-09-01 15:07:34 +00001780 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1781 ch->start_desc_tbl, ch->next_4_desc);
1782
Ralf Baechled203a7e2005-09-06 15:19:37 -07001783 /* set the alpha register */
1784 ch->alpha = vcs->alpha;
1785
1786 /* set clipping registers */
1787 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1788 VINO_CLIP_EVEN(norm->even.top +
1789 vcs->clipping.top / 2) |
1790 VINO_CLIP_X(vcs->clipping.left);
1791 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1792 vcs->clipping.bottom / 2 - 1) |
1793 VINO_CLIP_EVEN(norm->even.top +
1794 vcs->clipping.bottom / 2 - 1) |
1795 VINO_CLIP_X(vcs->clipping.right);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001796
1797 /* set the size of actual content in the buffer (DECIMATION !) */
1798 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1799 vcs->decimation) *
1800 ((vcs->clipping.bottom - vcs->clipping.top) /
1801 vcs->decimation) *
1802 vino_data_formats[vcs->data_format].bpp;
1803
1804 ch->frame_rate = vcs->framert_reg;
1805
1806 ctrl = vino->control;
1807 intr = vino->intr_status;
1808
1809 if (vcs->channel == VINO_CHANNEL_A) {
1810 /* All interrupt conditions for this channel was cleared
1811 * so clear the interrupt status register and enable
1812 * interrupts */
1813 intr &= ~VINO_INTSTAT_A;
1814 ctrl |= VINO_CTRL_A_INT;
1815
1816 /* enable synchronization */
1817 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1818
1819 /* enable frame assembly */
1820 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1821
1822 /* set decimation used */
1823 if (vcs->decimation < 2)
1824 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1825 else {
1826 ctrl |= VINO_CTRL_A_DEC_ENBL;
1827 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1828 ctrl |= (vcs->decimation - 1) <<
1829 VINO_CTRL_A_DEC_SCALE_SHIFT;
1830 }
1831
1832 /* select input interface */
1833 if (vcs->input == VINO_INPUT_D1)
1834 ctrl |= VINO_CTRL_A_SELECT;
1835 else
1836 ctrl &= ~VINO_CTRL_A_SELECT;
1837
1838 /* palette */
1839 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1840 VINO_CTRL_A_DITHER);
1841 } else {
1842 intr &= ~VINO_INTSTAT_B;
1843 ctrl |= VINO_CTRL_B_INT;
1844
1845 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1846 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1847
1848 if (vcs->decimation < 2)
1849 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1850 else {
1851 ctrl |= VINO_CTRL_B_DEC_ENBL;
1852 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1853 ctrl |= (vcs->decimation - 1) <<
1854 VINO_CTRL_B_DEC_SCALE_SHIFT;
1855
1856 }
1857 if (vcs->input == VINO_INPUT_D1)
1858 ctrl |= VINO_CTRL_B_SELECT;
1859 else
1860 ctrl &= ~VINO_CTRL_B_SELECT;
1861
1862 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1863 VINO_CTRL_B_DITHER);
1864 }
1865
1866 /* set palette */
1867 fb->data_format = vcs->data_format;
1868
1869 switch (vcs->data_format) {
1870 case VINO_DATA_FMT_GREY:
1871 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1872 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1873 break;
1874 case VINO_DATA_FMT_RGB32:
1875 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1876 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1877 break;
1878 case VINO_DATA_FMT_YUV:
1879 /* nothing needs to be done */
1880 break;
1881 case VINO_DATA_FMT_RGB332:
1882 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1883 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1884 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1885 break;
1886 }
1887
1888 vino->intr_status = intr;
1889 vino->control = ctrl;
1890
1891 return 0;
1892}
1893
1894/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001895static inline void vino_dma_start(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001896{
1897 u32 ctrl = vino->control;
1898
1899 dprintk("vino_dma_start():\n");
1900 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1901 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1902 vino->control = ctrl;
1903}
1904
1905/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001906static inline void vino_dma_stop(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001907{
1908 u32 ctrl = vino->control;
1909
1910 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1911 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
Ladislav Michla637a112005-09-01 15:07:34 +00001912 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1913 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001914 vino->control = ctrl;
1915 dprintk("vino_dma_stop():\n");
1916}
1917
1918/*
1919 * Load dummy page to descriptor registers. This prevents generating of
1920 * spurious interrupts. (execute only with vino_lock locked)
1921 */
1922static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1923{
1924 struct sgi_vino_channel *ch;
1925
1926 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1927
1928 ch->page_index = 0;
1929 ch->line_count = 0;
1930
1931 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1932 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1933
1934 udelay(VINO_DESC_FETCH_DELAY);
1935 dprintk("channel %c clear interrupt condition\n",
1936 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1937}
1938
1939static int vino_capture(struct vino_channel_settings *vcs,
1940 struct vino_framebuffer *fb)
1941{
1942 int err = 0;
1943 unsigned long flags, flags2;
1944
1945 spin_lock_irqsave(&fb->state_lock, flags);
1946
1947 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1948 err = -EBUSY;
1949 fb->state = VINO_FRAMEBUFFER_IN_USE;
1950
1951 spin_unlock_irqrestore(&fb->state_lock, flags);
1952
1953 if (err)
1954 return err;
1955
1956 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1957 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1958
1959 vino_dma_setup(vcs, fb);
1960 vino_dma_start(vcs);
1961
1962 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1963 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1964
1965 return err;
1966}
1967
1968static
1969struct vino_framebuffer *vino_capture_enqueue(struct
1970 vino_channel_settings *vcs,
1971 unsigned int index)
1972{
1973 struct vino_framebuffer *fb;
1974 unsigned long flags;
1975
1976 dprintk("vino_capture_enqueue():\n");
1977
1978 spin_lock_irqsave(&vcs->capture_lock, flags);
1979
1980 fb = vino_queue_add(&vcs->fb_queue, index);
1981 if (fb == NULL) {
1982 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1983 "queue full?\n");
1984 goto out;
1985 }
1986out:
1987 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1988
1989 return fb;
1990}
1991
1992static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1993{
1994 struct vino_framebuffer *fb;
1995 unsigned int incoming, id;
1996 int err = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00001997 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001998
1999 dprintk("vino_capture_next():\n");
2000
2001 spin_lock_irqsave(&vcs->capture_lock, flags);
2002
2003 if (start) {
2004 /* start capture only if capture isn't in progress already */
2005 if (vcs->capturing) {
2006 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2007 return 0;
2008 }
2009
2010 } else {
2011 /* capture next frame:
2012 * stop capture if capturing is not set */
2013 if (!vcs->capturing) {
2014 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2015 return 0;
2016 }
2017 }
2018
2019 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2020 if (err) {
2021 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2022 "failed\n");
2023 err = -EINVAL;
2024 goto out;
2025 }
2026 if (incoming == 0) {
2027 dprintk("vino_capture_next(): no buffers available\n");
2028 goto out;
2029 }
2030
2031 fb = vino_queue_peek(&vcs->fb_queue, &id);
2032 if (fb == NULL) {
2033 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2034 err = -EINVAL;
2035 goto out;
2036 }
2037
Ralf Baechled203a7e2005-09-06 15:19:37 -07002038 if (start) {
2039 vcs->capturing = 1;
2040 }
2041
2042 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2043
2044 err = vino_capture(vcs, fb);
2045
2046 return err;
2047
2048out:
2049 vcs->capturing = 0;
2050 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2051
2052 return err;
2053}
2054
Ladislav Michla637a112005-09-01 15:07:34 +00002055static inline int vino_is_capturing(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002056{
2057 int ret;
2058 unsigned long flags;
2059
2060 spin_lock_irqsave(&vcs->capture_lock, flags);
2061
2062 ret = vcs->capturing;
2063
2064 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2065
2066 return ret;
2067}
2068
2069/* waits until a frame is captured */
2070static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2071{
2072 wait_queue_t wait;
2073 int err = 0;
2074
2075 dprintk("vino_wait_for_frame():\n");
2076
2077 init_waitqueue_entry(&wait, current);
2078 /* add ourselves into wait queue */
2079 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002080
2081 /* to ensure that schedule_timeout will return immediately
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -03002082 * if VINO interrupt was triggered meanwhile */
Mauro Carvalho Chehab818ca472007-07-17 16:29:07 -03002083 schedule_timeout_interruptible(msecs_to_jiffies(100));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002084
2085 if (signal_pending(current))
2086 err = -EINTR;
2087
2088 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2089
2090 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2091 err ? "failed" : "ok");
2092
2093 return err;
2094}
2095
2096/* the function assumes that PAGE_SIZE % 4 == 0 */
2097static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2098 unsigned char *pageptr;
2099 unsigned int page, i;
2100 unsigned char a;
2101
2102 for (page = 0; page < fb->desc_table.page_count; page++) {
2103 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2104
2105 for (i = 0; i < PAGE_SIZE; i += 4) {
2106 a = pageptr[0];
2107 pageptr[0] = pageptr[3];
2108 pageptr[1] = pageptr[2];
2109 pageptr[2] = pageptr[1];
2110 pageptr[3] = a;
2111 pageptr += 4;
2112 }
2113 }
2114}
2115
2116/* checks if the buffer is in correct state and syncs data */
2117static int vino_check_buffer(struct vino_channel_settings *vcs,
2118 struct vino_framebuffer *fb)
2119{
2120 int err = 0;
2121 unsigned long flags;
2122
2123 dprintk("vino_check_buffer():\n");
2124
2125 spin_lock_irqsave(&fb->state_lock, flags);
2126 switch (fb->state) {
2127 case VINO_FRAMEBUFFER_IN_USE:
2128 err = -EIO;
2129 break;
2130 case VINO_FRAMEBUFFER_READY:
2131 vino_sync_buffer(fb);
2132 fb->state = VINO_FRAMEBUFFER_UNUSED;
2133 break;
2134 default:
2135 err = -EINVAL;
2136 }
2137 spin_unlock_irqrestore(&fb->state_lock, flags);
2138
2139 if (!err) {
2140 if (vino_pixel_conversion
2141 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2142 vino_convert_to_rgba(fb);
2143 }
2144 } else if (err && (err != -EINVAL)) {
2145 dprintk("vino_check_buffer(): buffer not ready\n");
2146
2147 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2148 vino_dma_stop(vcs);
2149 vino_clear_interrupt(vcs);
2150 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2151 }
2152
2153 return err;
2154}
2155
2156/* forcefully terminates capture */
2157static void vino_capture_stop(struct vino_channel_settings *vcs)
2158{
2159 unsigned int incoming = 0, outgoing = 0, id;
2160 unsigned long flags, flags2;
2161
2162 dprintk("vino_capture_stop():\n");
2163
2164 spin_lock_irqsave(&vcs->capture_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00002165
Ralf Baechled203a7e2005-09-06 15:19:37 -07002166 /* unset capturing to stop queue processing */
2167 vcs->capturing = 0;
2168
2169 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2170
2171 vino_dma_stop(vcs);
2172 vino_clear_interrupt(vcs);
2173
2174 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2175
2176 /* remove all items from the queue */
2177 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2178 dprintk("vino_capture_stop(): "
2179 "vino_queue_get_incoming() failed\n");
2180 goto out;
2181 }
2182 while (incoming > 0) {
2183 vino_queue_transfer(&vcs->fb_queue);
2184
2185 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2186 dprintk("vino_capture_stop(): "
2187 "vino_queue_get_incoming() failed\n");
2188 goto out;
2189 }
2190 }
2191
2192 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2193 dprintk("vino_capture_stop(): "
2194 "vino_queue_get_outgoing() failed\n");
2195 goto out;
2196 }
2197 while (outgoing > 0) {
2198 vino_queue_remove(&vcs->fb_queue, &id);
2199
2200 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2201 dprintk("vino_capture_stop(): "
2202 "vino_queue_get_outgoing() failed\n");
2203 goto out;
2204 }
2205 }
2206
2207out:
2208 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2209}
2210
Ladislav Michla637a112005-09-01 15:07:34 +00002211#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07002212static int vino_capture_failed(struct vino_channel_settings *vcs)
2213{
2214 struct vino_framebuffer *fb;
2215 unsigned long flags;
2216 unsigned int i;
2217 int ret;
2218
2219 dprintk("vino_capture_failed():\n");
2220
2221 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2222
2223 vino_dma_stop(vcs);
2224 vino_clear_interrupt(vcs);
2225
2226 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2227
2228 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2229 if (ret == VINO_QUEUE_ERROR) {
2230 dprintk("vino_queue_get_incoming() failed\n");
2231 return -EINVAL;
2232 }
2233 if (i == 0) {
2234 /* no buffers to process */
2235 return 0;
2236 }
2237
2238 fb = vino_queue_peek(&vcs->fb_queue, &i);
2239 if (fb == NULL) {
2240 dprintk("vino_queue_peek() failed\n");
2241 return -EINVAL;
2242 }
2243
2244 spin_lock_irqsave(&fb->state_lock, flags);
2245 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2246 fb->state = VINO_FRAMEBUFFER_UNUSED;
2247 vino_queue_transfer(&vcs->fb_queue);
2248 vino_queue_remove(&vcs->fb_queue, &i);
2249 /* we should actually discard the newest frame,
2250 * but who cares ... */
2251 }
2252 spin_unlock_irqrestore(&fb->state_lock, flags);
2253
2254 return 0;
2255}
Ladislav Michla637a112005-09-01 15:07:34 +00002256#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002257
Ladislav Michla637a112005-09-01 15:07:34 +00002258static void vino_skip_frame(struct vino_channel_settings *vcs)
2259{
2260 struct vino_framebuffer *fb;
2261 unsigned long flags;
2262 unsigned int id;
2263
2264 spin_lock_irqsave(&vcs->capture_lock, flags);
2265 fb = vino_queue_peek(&vcs->fb_queue, &id);
2266 if (!fb) {
2267 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2268 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2269 return;
2270 }
2271 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2272
2273 spin_lock_irqsave(&fb->state_lock, flags);
2274 fb->state = VINO_FRAMEBUFFER_UNUSED;
2275 spin_unlock_irqrestore(&fb->state_lock, flags);
2276
2277 vino_capture_next(vcs, 0);
2278}
2279
2280static void vino_frame_done(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002281{
2282 struct vino_framebuffer *fb;
2283 unsigned long flags;
2284
2285 spin_lock_irqsave(&vcs->capture_lock, flags);
2286 fb = vino_queue_transfer(&vcs->fb_queue);
2287 if (!fb) {
2288 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2289 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2290 return;
2291 }
2292 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2293
Ladislav Michla637a112005-09-01 15:07:34 +00002294 fb->frame_counter = vcs->int_data.frame_counter;
2295 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2296 sizeof(struct timeval));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002297
2298 spin_lock_irqsave(&fb->state_lock, flags);
2299 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2300 fb->state = VINO_FRAMEBUFFER_READY;
2301 spin_unlock_irqrestore(&fb->state_lock, flags);
2302
2303 wake_up(&vcs->fb_queue.frame_wait_queue);
2304
2305 vino_capture_next(vcs, 0);
2306}
2307
Ladislav Michla637a112005-09-01 15:07:34 +00002308static void vino_capture_tasklet(unsigned long channel) {
2309 struct vino_channel_settings *vcs;
2310
2311 vcs = (channel == VINO_CHANNEL_A)
2312 ? &vino_drvdata->a : &vino_drvdata->b;
2313
2314 if (vcs->int_data.skip)
2315 vcs->int_data.skip_count++;
2316
2317 if (vcs->int_data.skip && (vcs->int_data.skip_count
2318 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2319 vino_skip_frame(vcs);
2320 } else {
2321 vcs->int_data.skip_count = 0;
2322 vino_frame_done(vcs);
2323 }
2324}
2325
David Howells7d12e782006-10-05 14:55:46 +01002326static irqreturn_t vino_interrupt(int irq, void *dev_id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002327{
Ladislav Michla637a112005-09-01 15:07:34 +00002328 u32 ctrl, intr;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002329 unsigned int fc_a, fc_b;
Ladislav Michla637a112005-09-01 15:07:34 +00002330 int handled_a = 0, skip_a = 0, done_a = 0;
2331 int handled_b = 0, skip_b = 0, done_b = 0;
2332
2333#ifdef VINO_DEBUG_INT
2334 int loop = 0;
2335 unsigned int line_count = vino->a.line_count,
2336 page_index = vino->a.page_index,
2337 field_counter = vino->a.field_counter,
2338 start_desc_tbl = vino->a.start_desc_tbl,
2339 next_4_desc = vino->a.next_4_desc;
2340 unsigned int line_count_2,
2341 page_index_2,
2342 field_counter_2,
2343 start_desc_tbl_2,
2344 next_4_desc_2;
2345#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002346
2347 spin_lock(&vino_drvdata->vino_lock);
2348
Ladislav Michla637a112005-09-01 15:07:34 +00002349 while ((intr = vino->intr_status)) {
2350 fc_a = vino->a.field_counter >> 1;
2351 fc_b = vino->b.field_counter >> 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002352
Ladislav Michla637a112005-09-01 15:07:34 +00002353 /* handle error-interrupts in some special way ?
2354 * --> skips frames */
2355 if (intr & VINO_INTSTAT_A) {
2356 if (intr & VINO_INTSTAT_A_EOF) {
2357 vino_drvdata->a.field++;
2358 if (vino_drvdata->a.field > 1) {
2359 vino_dma_stop(&vino_drvdata->a);
2360 vino_clear_interrupt(&vino_drvdata->a);
2361 vino_drvdata->a.field = 0;
2362 done_a = 1;
2363 } else {
2364 if (vino->a.page_index
2365 != vino_drvdata->a.line_size) {
2366 vino->a.line_count = 0;
2367 vino->a.page_index =
2368 vino_drvdata->
2369 a.line_size;
2370 vino->a.next_4_desc =
2371 vino->a.start_desc_tbl;
2372 }
2373 }
2374 dprintk("channel A end-of-field "
2375 "interrupt: %04x\n", intr);
2376 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002377 vino_dma_stop(&vino_drvdata->a);
2378 vino_clear_interrupt(&vino_drvdata->a);
2379 vino_drvdata->a.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002380 skip_a = 1;
2381 dprintk("channel A error interrupt: %04x\n",
2382 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002383 }
Ladislav Michla637a112005-09-01 15:07:34 +00002384
2385#ifdef VINO_DEBUG_INT
2386 line_count_2 = vino->a.line_count;
2387 page_index_2 = vino->a.page_index;
2388 field_counter_2 = vino->a.field_counter;
2389 start_desc_tbl_2 = vino->a.start_desc_tbl;
2390 next_4_desc_2 = vino->a.next_4_desc;
2391
2392 printk("intr = %04x, loop = %d, field = %d\n",
2393 intr, loop, vino_drvdata->a.field);
2394 printk("1- line count = %04d, page index = %04d, "
2395 "start = %08x, next = %08x\n"
2396 " fieldc = %d, framec = %d\n",
2397 line_count, page_index, start_desc_tbl,
2398 next_4_desc, field_counter, fc_a);
2399 printk("12-line count = %04d, page index = %04d, "
2400 " start = %08x, next = %08x\n",
2401 line_count_2, page_index_2, start_desc_tbl_2,
2402 next_4_desc_2);
2403
2404 if (done_a)
2405 printk("\n");
2406#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002407 }
Ladislav Michla637a112005-09-01 15:07:34 +00002408
2409 if (intr & VINO_INTSTAT_B) {
2410 if (intr & VINO_INTSTAT_B_EOF) {
2411 vino_drvdata->b.field++;
2412 if (vino_drvdata->b.field > 1) {
2413 vino_dma_stop(&vino_drvdata->b);
2414 vino_clear_interrupt(&vino_drvdata->b);
2415 vino_drvdata->b.field = 0;
2416 done_b = 1;
2417 }
2418 dprintk("channel B end-of-field "
2419 "interrupt: %04x\n", intr);
2420 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002421 vino_dma_stop(&vino_drvdata->b);
2422 vino_clear_interrupt(&vino_drvdata->b);
2423 vino_drvdata->b.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002424 skip_b = 1;
2425 dprintk("channel B error interrupt: %04x\n",
2426 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002427 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002428 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002429
Ladislav Michla637a112005-09-01 15:07:34 +00002430 /* Always remember to clear interrupt status.
2431 * Disable VINO interrupts while we do this. */
2432 ctrl = vino->control;
2433 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2434 vino->intr_status = ~intr;
2435 vino->control = ctrl;
2436
2437 spin_unlock(&vino_drvdata->vino_lock);
2438
2439 if ((!handled_a) && (done_a || skip_a)) {
2440 if (!skip_a) {
2441 do_gettimeofday(&vino_drvdata->
2442 a.int_data.timestamp);
2443 vino_drvdata->a.int_data.frame_counter = fc_a;
2444 }
2445 vino_drvdata->a.int_data.skip = skip_a;
2446
2447 dprintk("channel A %s, interrupt: %d\n",
2448 skip_a ? "skipping frame" : "frame done",
2449 intr);
2450 tasklet_hi_schedule(&vino_tasklet_a);
2451 handled_a = 1;
2452 }
2453
2454 if ((!handled_b) && (done_b || skip_b)) {
2455 if (!skip_b) {
2456 do_gettimeofday(&vino_drvdata->
2457 b.int_data.timestamp);
2458 vino_drvdata->b.int_data.frame_counter = fc_b;
2459 }
2460 vino_drvdata->b.int_data.skip = skip_b;
2461
2462 dprintk("channel B %s, interrupt: %d\n",
2463 skip_b ? "skipping frame" : "frame done",
2464 intr);
2465 tasklet_hi_schedule(&vino_tasklet_b);
2466 handled_b = 1;
2467 }
2468
2469#ifdef VINO_DEBUG_INT
2470 loop++;
2471#endif
2472 spin_lock(&vino_drvdata->vino_lock);
2473 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002474
2475 spin_unlock(&vino_drvdata->vino_lock);
2476
Ralf Baechled203a7e2005-09-06 15:19:37 -07002477 return IRQ_HANDLED;
2478}
2479
2480/* VINO video input management */
2481
2482static int vino_get_saa7191_input(int input)
2483{
2484 switch (input) {
2485 case VINO_INPUT_COMPOSITE:
2486 return SAA7191_INPUT_COMPOSITE;
2487 case VINO_INPUT_SVIDEO:
2488 return SAA7191_INPUT_SVIDEO;
2489 default:
2490 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2491 "invalid input!\n");
2492 return -1;
2493 }
2494}
2495
Ladislav Michla637a112005-09-01 15:07:34 +00002496static int vino_get_saa7191_norm(unsigned int data_norm)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002497{
Ladislav Michla637a112005-09-01 15:07:34 +00002498 switch (data_norm) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002499 case VINO_DATA_NORM_AUTO:
2500 return SAA7191_NORM_AUTO;
Ladislav Michla637a112005-09-01 15:07:34 +00002501 case VINO_DATA_NORM_AUTO_EXT:
2502 return SAA7191_NORM_AUTO_EXT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002503 case VINO_DATA_NORM_PAL:
2504 return SAA7191_NORM_PAL;
2505 case VINO_DATA_NORM_NTSC:
2506 return SAA7191_NORM_NTSC;
2507 case VINO_DATA_NORM_SECAM:
2508 return SAA7191_NORM_SECAM;
2509 default:
2510 printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
2511 "invalid norm!\n");
2512 return -1;
2513 }
2514}
2515
Ladislav Michla637a112005-09-01 15:07:34 +00002516static int vino_get_from_saa7191_norm(int saa7191_norm)
2517{
2518 switch (saa7191_norm) {
2519 case SAA7191_NORM_PAL:
2520 return VINO_DATA_NORM_PAL;
2521 case SAA7191_NORM_NTSC:
2522 return VINO_DATA_NORM_NTSC;
2523 case SAA7191_NORM_SECAM:
2524 return VINO_DATA_NORM_SECAM;
2525 default:
2526 printk(KERN_ERR "VINO: vino_get_from_saa7191_norm(): "
2527 "invalid norm!\n");
2528 return VINO_DATA_NORM_NONE;
2529 }
2530}
2531
2532static int vino_saa7191_set_norm(unsigned int *data_norm)
2533{
2534 int saa7191_norm, new_data_norm;
2535 int err = 0;
2536
2537 saa7191_norm = vino_get_saa7191_norm(*data_norm);
2538
2539 err = i2c_decoder_command(DECODER_SAA7191_SET_NORM,
2540 &saa7191_norm);
2541 if (err)
2542 goto out;
2543
2544 if ((*data_norm == VINO_DATA_NORM_AUTO)
2545 || (*data_norm == VINO_DATA_NORM_AUTO_EXT)) {
2546 struct saa7191_status status;
2547
2548 err = i2c_decoder_command(DECODER_SAA7191_GET_STATUS,
2549 &status);
2550 if (err)
2551 goto out;
2552
2553 new_data_norm =
2554 vino_get_from_saa7191_norm(status.norm);
2555 if (new_data_norm == VINO_DATA_NORM_NONE) {
2556 err = -EINVAL;
2557 goto out;
2558 }
2559
2560 *data_norm = (unsigned int)new_data_norm;
2561 }
2562
2563out:
2564 return err;
2565}
2566
Ralf Baechled203a7e2005-09-06 15:19:37 -07002567/* execute with input_lock locked */
2568static int vino_is_input_owner(struct vino_channel_settings *vcs)
2569{
2570 switch(vcs->input) {
2571 case VINO_INPUT_COMPOSITE:
2572 case VINO_INPUT_SVIDEO:
2573 return (vino_drvdata->decoder.owner == vcs->channel);
2574 case VINO_INPUT_D1:
2575 return (vino_drvdata->camera.owner == vcs->channel);
2576 default:
2577 return 0;
2578 }
2579}
2580
2581static int vino_acquire_input(struct vino_channel_settings *vcs)
2582{
Ladislav Michla637a112005-09-01 15:07:34 +00002583 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002584 int ret = 0;
2585
2586 dprintk("vino_acquire_input():\n");
2587
Ladislav Michla637a112005-09-01 15:07:34 +00002588 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002589
2590 /* First try D1 and then SAA7191 */
2591 if (vino_drvdata->camera.driver
2592 && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002593 i2c_use_client(vino_drvdata->camera.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002594 vino_drvdata->camera.owner = vcs->channel;
2595 vcs->input = VINO_INPUT_D1;
2596 vcs->data_norm = VINO_DATA_NORM_D1;
2597 } else if (vino_drvdata->decoder.driver
2598 && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
Ladislav Michla637a112005-09-01 15:07:34 +00002599 int input, data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002600 int saa7191_input;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002601
Jean Delvaree48d3312008-01-27 18:14:48 +01002602 i2c_use_client(vino_drvdata->decoder.driver);
Ladislav Michla637a112005-09-01 15:07:34 +00002603 input = VINO_INPUT_COMPOSITE;
2604
2605 saa7191_input = vino_get_saa7191_input(input);
2606 ret = i2c_decoder_command(DECODER_SET_INPUT,
2607 &saa7191_input);
2608 if (ret) {
2609 ret = -EINVAL;
2610 goto out;
2611 }
2612
2613 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2614
2615 /* Don't hold spinlocks while auto-detecting norm
2616 * as it may take a while... */
2617
2618 data_norm = VINO_DATA_NORM_AUTO_EXT;
2619
2620 ret = vino_saa7191_set_norm(&data_norm);
2621 if ((ret == -EBUSY) || (ret == -EAGAIN)) {
2622 data_norm = VINO_DATA_NORM_PAL;
2623 ret = vino_saa7191_set_norm(&data_norm);
2624 }
2625
2626 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2627
2628 if (ret) {
2629 ret = -EINVAL;
2630 goto out;
2631 }
2632
Ralf Baechled203a7e2005-09-06 15:19:37 -07002633 vino_drvdata->decoder.owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002634
Ladislav Michla637a112005-09-01 15:07:34 +00002635 vcs->input = input;
2636 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002637 } else {
2638 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2639 vino_drvdata->b.input : vino_drvdata->a.input;
2640 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2641 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2642 }
2643
2644 if (vcs->input == VINO_INPUT_NONE) {
2645 ret = -ENODEV;
2646 goto out;
2647 }
2648
Ladislav Michla637a112005-09-01 15:07:34 +00002649 vino_set_default_clipping(vcs);
2650 vino_set_default_scaling(vcs);
2651 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002652
2653 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2654
2655out:
Ladislav Michla637a112005-09-01 15:07:34 +00002656 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002657
2658 return ret;
2659}
2660
2661static int vino_set_input(struct vino_channel_settings *vcs, int input)
2662{
2663 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2664 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002665 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002666 int ret = 0;
2667
2668 dprintk("vino_set_input():\n");
2669
Ladislav Michla637a112005-09-01 15:07:34 +00002670 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002671
2672 if (vcs->input == input)
2673 goto out;
2674
Ladislav Michla637a112005-09-01 15:07:34 +00002675 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002676 case VINO_INPUT_COMPOSITE:
2677 case VINO_INPUT_SVIDEO:
2678 if (!vino_drvdata->decoder.driver) {
2679 ret = -EINVAL;
2680 goto out;
2681 }
2682
2683 if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002684 i2c_use_client(vino_drvdata->decoder.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002685 vino_drvdata->decoder.owner = vcs->channel;
2686 }
2687
2688 if (vino_drvdata->decoder.owner == vcs->channel) {
Ladislav Michla637a112005-09-01 15:07:34 +00002689 int data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002690 int saa7191_input;
Ladislav Michla637a112005-09-01 15:07:34 +00002691
2692 saa7191_input = vino_get_saa7191_input(input);
2693 ret = i2c_decoder_command(DECODER_SET_INPUT,
2694 &saa7191_input);
2695 if (ret) {
2696 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2697 ret = -EINVAL;
2698 goto out;
2699 }
2700
2701 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2702
2703 /* Don't hold spinlocks while auto-detecting norm
2704 * as it may take a while... */
2705
2706 data_norm = VINO_DATA_NORM_AUTO_EXT;
2707
2708 ret = vino_saa7191_set_norm(&data_norm);
2709 if ((ret == -EBUSY) || (ret == -EAGAIN)) {
2710 data_norm = VINO_DATA_NORM_PAL;
2711 ret = vino_saa7191_set_norm(&data_norm);
2712 }
2713
2714 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2715
2716 if (ret) {
2717 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2718 ret = -EINVAL;
2719 goto out;
2720 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002721
2722 vcs->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +00002723 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002724 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00002725 if (input != vcs2->input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002726 ret = -EBUSY;
2727 goto out;
2728 }
2729
2730 vcs->input = input;
2731 vcs->data_norm = vcs2->data_norm;
2732 }
2733
2734 if (vino_drvdata->camera.owner == vcs->channel) {
2735 /* Transfer the ownership or release the input */
2736 if (vcs2->input == VINO_INPUT_D1) {
2737 vino_drvdata->camera.owner = vcs2->channel;
2738 } else {
2739 i2c_release_client(vino_drvdata->
2740 camera.driver);
2741 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2742 }
2743 }
2744 break;
2745 case VINO_INPUT_D1:
2746 if (!vino_drvdata->camera.driver) {
2747 ret = -EINVAL;
2748 goto out;
2749 }
2750
2751 if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002752 i2c_use_client(vino_drvdata->camera.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002753 vino_drvdata->camera.owner = vcs->channel;
2754 }
2755
2756 if (vino_drvdata->decoder.owner == vcs->channel) {
2757 /* Transfer the ownership or release the input */
2758 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2759 (vcs2->input == VINO_INPUT_SVIDEO)) {
2760 vino_drvdata->decoder.owner = vcs2->channel;
2761 } else {
2762 i2c_release_client(vino_drvdata->
2763 decoder.driver);
2764 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2765 }
2766 }
2767
2768 vcs->input = input;
2769 vcs->data_norm = VINO_DATA_NORM_D1;
2770 break;
2771 default:
2772 ret = -EINVAL;
2773 goto out;
2774 }
2775
2776 vino_set_default_clipping(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002777 vino_set_default_scaling(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002778 vino_set_default_framerate(vcs);
2779
2780 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2781
2782out:
Ladislav Michla637a112005-09-01 15:07:34 +00002783 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002784
2785 return ret;
2786}
2787
2788static void vino_release_input(struct vino_channel_settings *vcs)
2789{
2790 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2791 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002792 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002793
2794 dprintk("vino_release_input():\n");
2795
Ladislav Michla637a112005-09-01 15:07:34 +00002796 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002797
2798 /* Release ownership of the channel
2799 * and if the other channel takes input from
2800 * the same source, transfer the ownership */
2801 if (vino_drvdata->camera.owner == vcs->channel) {
2802 if (vcs2->input == VINO_INPUT_D1) {
2803 vino_drvdata->camera.owner = vcs2->channel;
2804 } else {
2805 i2c_release_client(vino_drvdata->camera.driver);
2806 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2807 }
2808 } else if (vino_drvdata->decoder.owner == vcs->channel) {
2809 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2810 (vcs2->input == VINO_INPUT_SVIDEO)) {
2811 vino_drvdata->decoder.owner = vcs2->channel;
2812 } else {
2813 i2c_release_client(vino_drvdata->decoder.driver);
2814 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2815 }
2816 }
2817 vcs->input = VINO_INPUT_NONE;
2818
Ladislav Michla637a112005-09-01 15:07:34 +00002819 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002820}
2821
2822/* execute with input_lock locked */
2823static int vino_set_data_norm(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00002824 unsigned int data_norm,
2825 unsigned long *flags)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002826{
Ladislav Michla637a112005-09-01 15:07:34 +00002827 int err = 0;
2828
2829 if (data_norm == vcs->data_norm)
2830 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002831
2832 switch (vcs->input) {
2833 case VINO_INPUT_D1:
2834 /* only one "norm" supported */
Ladislav Michla637a112005-09-01 15:07:34 +00002835 if ((data_norm != VINO_DATA_NORM_D1)
2836 && (data_norm != VINO_DATA_NORM_AUTO)
2837 && (data_norm != VINO_DATA_NORM_AUTO_EXT))
Ralf Baechled203a7e2005-09-06 15:19:37 -07002838 return -EINVAL;
2839 break;
2840 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00002841 case VINO_INPUT_SVIDEO: {
2842 if ((data_norm != VINO_DATA_NORM_PAL)
2843 && (data_norm != VINO_DATA_NORM_NTSC)
2844 && (data_norm != VINO_DATA_NORM_SECAM)
2845 && (data_norm != VINO_DATA_NORM_AUTO)
2846 && (data_norm != VINO_DATA_NORM_AUTO_EXT))
2847 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002848
Ladislav Michla637a112005-09-01 15:07:34 +00002849 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002850
Ladislav Michla637a112005-09-01 15:07:34 +00002851 /* Don't hold spinlocks while setting norm
2852 * as it may take a while... */
2853
2854 err = vino_saa7191_set_norm(&data_norm);
2855
2856 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2857
2858 if (err)
2859 goto out;
2860
Ralf Baechled203a7e2005-09-06 15:19:37 -07002861 vcs->data_norm = data_norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002862
2863 vino_set_default_clipping(vcs);
2864 vino_set_default_scaling(vcs);
2865 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002866 break;
Ladislav Michla637a112005-09-01 15:07:34 +00002867 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002868 default:
2869 return -EINVAL;
2870 }
2871
Ladislav Michla637a112005-09-01 15:07:34 +00002872out:
2873 return err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002874}
2875
2876/* V4L2 helper functions */
2877
2878static int vino_find_data_format(__u32 pixelformat)
2879{
2880 int i;
2881
2882 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2883 if (vino_data_formats[i].pixelformat == pixelformat)
2884 return i;
2885 }
2886
2887 return VINO_DATA_FMT_NONE;
2888}
2889
2890static int vino_enum_data_norm(struct vino_channel_settings *vcs, __u32 index)
2891{
2892 int data_norm = VINO_DATA_NORM_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002893 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002894
Ladislav Michla637a112005-09-01 15:07:34 +00002895 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002896 switch(vcs->input) {
2897 case VINO_INPUT_COMPOSITE:
2898 case VINO_INPUT_SVIDEO:
2899 if (index == 0) {
2900 data_norm = VINO_DATA_NORM_PAL;
2901 } else if (index == 1) {
2902 data_norm = VINO_DATA_NORM_NTSC;
2903 } else if (index == 2) {
2904 data_norm = VINO_DATA_NORM_SECAM;
2905 }
2906 break;
2907 case VINO_INPUT_D1:
2908 if (index == 0) {
2909 data_norm = VINO_DATA_NORM_D1;
2910 }
2911 break;
2912 }
Ladislav Michla637a112005-09-01 15:07:34 +00002913 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002914
2915 return data_norm;
2916}
2917
2918static int vino_enum_input(struct vino_channel_settings *vcs, __u32 index)
2919{
2920 int input = VINO_INPUT_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002921 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002922
Ladislav Michla637a112005-09-01 15:07:34 +00002923 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002924 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2925 switch (index) {
2926 case 0:
2927 input = VINO_INPUT_COMPOSITE;
2928 break;
2929 case 1:
2930 input = VINO_INPUT_SVIDEO;
2931 break;
2932 case 2:
2933 input = VINO_INPUT_D1;
2934 break;
2935 }
2936 } else if (vino_drvdata->decoder.driver) {
2937 switch (index) {
2938 case 0:
2939 input = VINO_INPUT_COMPOSITE;
2940 break;
2941 case 1:
2942 input = VINO_INPUT_SVIDEO;
2943 break;
2944 }
2945 } else if (vino_drvdata->camera.driver) {
2946 switch (index) {
2947 case 0:
2948 input = VINO_INPUT_D1;
2949 break;
2950 }
2951 }
Ladislav Michla637a112005-09-01 15:07:34 +00002952 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002953
2954 return input;
2955}
2956
2957/* execute with input_lock locked */
2958static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2959{
2960 __u32 index = 0;
2961 // FIXME: detect when no inputs available
2962
2963 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2964 switch (vcs->input) {
2965 case VINO_INPUT_COMPOSITE:
2966 index = 0;
2967 break;
2968 case VINO_INPUT_SVIDEO:
2969 index = 1;
2970 break;
2971 case VINO_INPUT_D1:
2972 index = 2;
2973 break;
2974 }
2975 } else if (vino_drvdata->decoder.driver) {
2976 switch (vcs->input) {
2977 case VINO_INPUT_COMPOSITE:
2978 index = 0;
2979 break;
2980 case VINO_INPUT_SVIDEO:
2981 index = 1;
2982 break;
2983 }
2984 } else if (vino_drvdata->camera.driver) {
2985 switch (vcs->input) {
2986 case VINO_INPUT_D1:
2987 index = 0;
2988 break;
2989 }
2990 }
2991
2992 return index;
2993}
2994
2995/* V4L2 ioctls */
2996
2997static void vino_v4l2_querycap(struct v4l2_capability *cap)
2998{
2999 memset(cap, 0, sizeof(struct v4l2_capability));
3000
3001 strcpy(cap->driver, vino_driver_name);
3002 strcpy(cap->card, vino_driver_description);
3003 strcpy(cap->bus_info, vino_bus_name);
3004 cap->version = VINO_VERSION_CODE;
3005 cap->capabilities =
3006 V4L2_CAP_VIDEO_CAPTURE |
3007 V4L2_CAP_STREAMING;
3008 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
3009}
3010
3011static int vino_v4l2_enuminput(struct vino_channel_settings *vcs,
3012 struct v4l2_input *i)
3013{
3014 __u32 index = i->index;
3015 int input;
3016 dprintk("requested index = %d\n", index);
3017
3018 input = vino_enum_input(vcs, index);
3019 if (input == VINO_INPUT_NONE)
3020 return -EINVAL;
3021
3022 memset(i, 0, sizeof(struct v4l2_input));
3023
3024 i->index = index;
3025 i->type = V4L2_INPUT_TYPE_CAMERA;
3026 i->std = vino_inputs[input].std;
3027 strcpy(i->name, vino_inputs[input].name);
3028
3029 if ((input == VINO_INPUT_COMPOSITE)
3030 || (input == VINO_INPUT_SVIDEO)) {
3031 struct saa7191_status status;
3032 i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
3033 i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
3034 i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
3035 }
3036
3037 return 0;
3038}
3039
3040static int vino_v4l2_g_input(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00003041 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003042{
3043 __u32 index;
3044 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00003045 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003046
Ladislav Michla637a112005-09-01 15:07:34 +00003047 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003048 input = vcs->input;
3049 index = vino_find_input_index(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00003050 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003051
3052 dprintk("input = %d\n", input);
3053
3054 if (input == VINO_INPUT_NONE) {
3055 return -EINVAL;
3056 }
3057
Ladislav Michla637a112005-09-01 15:07:34 +00003058 *i = index;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003059
3060 return 0;
3061}
3062
3063static int vino_v4l2_s_input(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00003064 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003065{
3066 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00003067 dprintk("requested input = %d\n", *i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003068
Ladislav Michla637a112005-09-01 15:07:34 +00003069 input = vino_enum_input(vcs, *i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003070 if (input == VINO_INPUT_NONE)
3071 return -EINVAL;
3072
3073 return vino_set_input(vcs, input);
3074}
3075
3076static int vino_v4l2_enumstd(struct vino_channel_settings *vcs,
3077 struct v4l2_standard *s)
3078{
3079 int index = s->index;
Ladislav Michla637a112005-09-01 15:07:34 +00003080 int data_norm;
3081
3082 data_norm = vino_enum_data_norm(vcs, index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003083 dprintk("standard index = %d\n", index);
3084
3085 if (data_norm == VINO_DATA_NORM_NONE)
3086 return -EINVAL;
3087
3088 dprintk("standard name = %s\n",
3089 vino_data_norms[data_norm].description);
3090
3091 memset(s, 0, sizeof(struct v4l2_standard));
3092 s->index = index;
3093
3094 s->id = vino_data_norms[data_norm].std;
3095 s->frameperiod.numerator = 1;
3096 s->frameperiod.denominator =
3097 vino_data_norms[data_norm].fps_max;
3098 s->framelines =
3099 vino_data_norms[data_norm].framelines;
3100 strcpy(s->name,
3101 vino_data_norms[data_norm].description);
3102
3103 return 0;
3104}
3105
Ladislav Michla637a112005-09-01 15:07:34 +00003106static int vino_v4l2_querystd(struct vino_channel_settings *vcs,
3107 v4l2_std_id *std)
3108{
3109 unsigned long flags;
3110 int err = 0;
3111
3112 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3113
3114 switch (vcs->input) {
3115 case VINO_INPUT_D1:
3116 *std = vino_inputs[vcs->input].std;
3117 break;
3118 case VINO_INPUT_COMPOSITE:
3119 case VINO_INPUT_SVIDEO: {
3120 struct saa7191_status status;
3121
3122 i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
3123
3124 if (status.signal) {
3125 if (status.signal_60hz) {
3126 *std = V4L2_STD_NTSC;
3127 } else {
3128 *std = V4L2_STD_PAL | V4L2_STD_SECAM;
3129 }
3130 } else {
3131 *std = vino_inputs[vcs->input].std;
3132 }
3133 break;
3134 }
3135 default:
3136 err = -EINVAL;
3137 }
3138
3139 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3140
3141 return err;
3142}
3143
Ralf Baechled203a7e2005-09-06 15:19:37 -07003144static int vino_v4l2_g_std(struct vino_channel_settings *vcs,
3145 v4l2_std_id *std)
3146{
Ladislav Michla637a112005-09-01 15:07:34 +00003147 unsigned long flags;
3148
3149 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3150
Ralf Baechled203a7e2005-09-06 15:19:37 -07003151 *std = vino_data_norms[vcs->data_norm].std;
Ladislav Michla637a112005-09-01 15:07:34 +00003152 dprintk("current standard = %d\n", vcs->data_norm);
3153
3154 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003155
3156 return 0;
3157}
3158
3159static int vino_v4l2_s_std(struct vino_channel_settings *vcs,
3160 v4l2_std_id *std)
3161{
Ladislav Michla637a112005-09-01 15:07:34 +00003162 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003163 int ret = 0;
3164
Ladislav Michla637a112005-09-01 15:07:34 +00003165 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3166
3167 if (!vino_is_input_owner(vcs)) {
3168 ret = -EBUSY;
3169 goto out;
3170 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003171
3172 /* check if the standard is valid for the current input */
Ladislav Michla637a112005-09-01 15:07:34 +00003173 if ((*std) & vino_inputs[vcs->input].std) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003174 dprintk("standard accepted\n");
3175
3176 /* change the video norm for SAA7191
3177 * and accept NTSC for D1 (do nothing) */
3178
3179 if (vcs->input == VINO_INPUT_D1)
3180 goto out;
3181
Ladislav Michla637a112005-09-01 15:07:34 +00003182 if (((*std) & V4L2_STD_PAL)
3183 && ((*std) & V4L2_STD_NTSC)
3184 && ((*std) & V4L2_STD_SECAM)) {
3185 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_AUTO_EXT,
3186 &flags);
3187 } else if ((*std) & V4L2_STD_PAL) {
3188 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3189 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003190 } else if ((*std) & V4L2_STD_NTSC) {
Ladislav Michla637a112005-09-01 15:07:34 +00003191 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3192 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003193 } else if ((*std) & V4L2_STD_SECAM) {
Ladislav Michla637a112005-09-01 15:07:34 +00003194 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3195 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003196 } else {
3197 ret = -EINVAL;
3198 }
Ladislav Michla637a112005-09-01 15:07:34 +00003199
3200 if (ret) {
3201 ret = -EINVAL;
3202 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003203 } else {
3204 ret = -EINVAL;
3205 }
3206
3207out:
Ladislav Michla637a112005-09-01 15:07:34 +00003208 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003209
3210 return ret;
3211}
3212
3213static int vino_v4l2_enum_fmt(struct vino_channel_settings *vcs,
3214 struct v4l2_fmtdesc *fd)
3215{
3216 enum v4l2_buf_type type = fd->type;
3217 int index = fd->index;
3218 dprintk("format index = %d\n", index);
3219
3220 switch (fd->type) {
3221 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3222 if ((fd->index < 0) ||
3223 (fd->index >= VINO_DATA_FMT_COUNT))
3224 return -EINVAL;
3225 dprintk("format name = %s\n",
3226 vino_data_formats[index].description);
3227
3228 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3229 fd->index = index;
3230 fd->type = type;
3231 fd->pixelformat = vino_data_formats[index].pixelformat;
3232 strcpy(fd->description, vino_data_formats[index].description);
3233 break;
3234 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3235 default:
3236 return -EINVAL;
3237 }
3238
3239 return 0;
3240}
3241
3242static int vino_v4l2_try_fmt(struct vino_channel_settings *vcs,
3243 struct v4l2_format *f)
3244{
3245 struct vino_channel_settings tempvcs;
Ladislav Michla637a112005-09-01 15:07:34 +00003246 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003247
3248 switch (f->type) {
3249 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3250 struct v4l2_pix_format *pf = &f->fmt.pix;
3251
3252 dprintk("requested: w = %d, h = %d\n",
3253 pf->width, pf->height);
3254
Ladislav Michla637a112005-09-01 15:07:34 +00003255 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003256 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
Ladislav Michla637a112005-09-01 15:07:34 +00003257 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003258
3259 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3260 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
Ladislav Michla637a112005-09-01 15:07:34 +00003261 tempvcs.data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003262 pf->pixelformat =
3263 vino_data_formats[tempvcs.data_format].
3264 pixelformat;
3265 }
3266
3267 /* data format must be set before clipping/scaling */
3268 vino_set_scaling(&tempvcs, pf->width, pf->height);
3269
3270 dprintk("data format = %s\n",
3271 vino_data_formats[tempvcs.data_format].description);
3272
3273 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3274 tempvcs.decimation;
3275 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3276 tempvcs.decimation;
3277
3278 pf->field = V4L2_FIELD_INTERLACED;
3279 pf->bytesperline = tempvcs.line_size;
3280 pf->sizeimage = tempvcs.line_size *
3281 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3282 tempvcs.decimation;
3283 pf->colorspace =
3284 vino_data_formats[tempvcs.data_format].colorspace;
3285
3286 pf->priv = 0;
3287 break;
3288 }
3289 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3290 default:
3291 return -EINVAL;
3292 }
3293
3294 return 0;
3295}
3296
3297static int vino_v4l2_g_fmt(struct vino_channel_settings *vcs,
3298 struct v4l2_format *f)
3299{
Ladislav Michla637a112005-09-01 15:07:34 +00003300 unsigned long flags;
3301
Ralf Baechled203a7e2005-09-06 15:19:37 -07003302 switch (f->type) {
3303 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3304 struct v4l2_pix_format *pf = &f->fmt.pix;
Ladislav Michla637a112005-09-01 15:07:34 +00003305
3306 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003307
3308 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3309 vcs->decimation;
3310 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3311 vcs->decimation;
3312 pf->pixelformat =
3313 vino_data_formats[vcs->data_format].pixelformat;
3314
3315 pf->field = V4L2_FIELD_INTERLACED;
3316 pf->bytesperline = vcs->line_size;
3317 pf->sizeimage = vcs->line_size *
3318 (vcs->clipping.bottom - vcs->clipping.top) /
3319 vcs->decimation;
3320 pf->colorspace =
3321 vino_data_formats[vcs->data_format].colorspace;
3322
3323 pf->priv = 0;
3324
Ladislav Michla637a112005-09-01 15:07:34 +00003325 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003326 break;
3327 }
3328 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3329 default:
3330 return -EINVAL;
3331 }
3332
3333 return 0;
3334}
3335
3336static int vino_v4l2_s_fmt(struct vino_channel_settings *vcs,
3337 struct v4l2_format *f)
3338{
3339 int data_format;
Ladislav Michla637a112005-09-01 15:07:34 +00003340 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003341
3342 switch (f->type) {
3343 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3344 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003345
Ladislav Michla637a112005-09-01 15:07:34 +00003346 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003347
3348 data_format = vino_find_data_format(pf->pixelformat);
Ladislav Michla637a112005-09-01 15:07:34 +00003349
Ralf Baechled203a7e2005-09-06 15:19:37 -07003350 if (data_format == VINO_DATA_FMT_NONE) {
Ladislav Michla637a112005-09-01 15:07:34 +00003351 vcs->data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003352 pf->pixelformat =
3353 vino_data_formats[vcs->data_format].
3354 pixelformat;
3355 } else {
3356 vcs->data_format = data_format;
3357 }
3358
3359 /* data format must be set before clipping/scaling */
3360 vino_set_scaling(vcs, pf->width, pf->height);
3361
3362 dprintk("data format = %s\n",
3363 vino_data_formats[vcs->data_format].description);
3364
3365 pf->width = vcs->clipping.right - vcs->clipping.left;
3366 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3367
3368 pf->field = V4L2_FIELD_INTERLACED;
3369 pf->bytesperline = vcs->line_size;
3370 pf->sizeimage = vcs->line_size *
3371 (vcs->clipping.bottom - vcs->clipping.top) /
3372 vcs->decimation;
3373 pf->colorspace =
3374 vino_data_formats[vcs->data_format].colorspace;
3375
3376 pf->priv = 0;
3377
Ladislav Michla637a112005-09-01 15:07:34 +00003378 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003379 break;
3380 }
3381 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3382 default:
3383 return -EINVAL;
3384 }
3385
3386 return 0;
3387}
3388
3389static int vino_v4l2_cropcap(struct vino_channel_settings *vcs,
3390 struct v4l2_cropcap *ccap)
3391{
3392 const struct vino_data_norm *norm;
Ladislav Michla637a112005-09-01 15:07:34 +00003393 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003394
3395 switch (ccap->type) {
3396 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003397 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3398
Ralf Baechled203a7e2005-09-06 15:19:37 -07003399 norm = &vino_data_norms[vcs->data_norm];
Ladislav Michla637a112005-09-01 15:07:34 +00003400
3401 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003402
3403 ccap->bounds.left = 0;
3404 ccap->bounds.top = 0;
3405 ccap->bounds.width = norm->width;
3406 ccap->bounds.height = norm->height;
3407 memcpy(&ccap->defrect, &ccap->bounds,
3408 sizeof(struct v4l2_rect));
3409
3410 ccap->pixelaspect.numerator = 1;
3411 ccap->pixelaspect.denominator = 1;
3412 break;
3413 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3414 default:
3415 return -EINVAL;
3416 }
3417
3418 return 0;
3419}
3420
3421static int vino_v4l2_g_crop(struct vino_channel_settings *vcs,
3422 struct v4l2_crop *c)
3423{
Ladislav Michla637a112005-09-01 15:07:34 +00003424 unsigned long flags;
3425
Ralf Baechled203a7e2005-09-06 15:19:37 -07003426 switch (c->type) {
3427 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003428 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003429
3430 c->c.left = vcs->clipping.left;
3431 c->c.top = vcs->clipping.top;
3432 c->c.width = vcs->clipping.right - vcs->clipping.left;
3433 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3434
Ladislav Michla637a112005-09-01 15:07:34 +00003435 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003436 break;
3437 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3438 default:
3439 return -EINVAL;
3440 }
3441
3442 return 0;
3443}
3444
3445static int vino_v4l2_s_crop(struct vino_channel_settings *vcs,
3446 struct v4l2_crop *c)
3447{
Ladislav Michla637a112005-09-01 15:07:34 +00003448 unsigned long flags;
3449
Ralf Baechled203a7e2005-09-06 15:19:37 -07003450 switch (c->type) {
3451 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003452 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003453
Ralf Baechled203a7e2005-09-06 15:19:37 -07003454 vino_set_clipping(vcs, c->c.left, c->c.top,
3455 c->c.width, c->c.height);
3456
Ladislav Michla637a112005-09-01 15:07:34 +00003457 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003458 break;
3459 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3460 default:
3461 return -EINVAL;
3462 }
3463
3464 return 0;
3465}
3466
3467static int vino_v4l2_g_parm(struct vino_channel_settings *vcs,
3468 struct v4l2_streamparm *sp)
3469{
Ladislav Michla637a112005-09-01 15:07:34 +00003470 unsigned long flags;
3471
Ralf Baechled203a7e2005-09-06 15:19:37 -07003472 switch (sp->type) {
3473 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3474 struct v4l2_captureparm *cp = &sp->parm.capture;
3475 memset(cp, 0, sizeof(struct v4l2_captureparm));
3476
3477 cp->capability = V4L2_CAP_TIMEPERFRAME;
3478 cp->timeperframe.numerator = 1;
3479
Ladislav Michla637a112005-09-01 15:07:34 +00003480 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3481
Ralf Baechled203a7e2005-09-06 15:19:37 -07003482 cp->timeperframe.denominator = vcs->fps;
Ladislav Michla637a112005-09-01 15:07:34 +00003483
3484 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003485
3486 // TODO: cp->readbuffers = xxx;
3487 break;
3488 }
3489 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3490 default:
3491 return -EINVAL;
3492 }
3493
3494 return 0;
3495}
3496
3497static int vino_v4l2_s_parm(struct vino_channel_settings *vcs,
3498 struct v4l2_streamparm *sp)
3499{
Ladislav Michla637a112005-09-01 15:07:34 +00003500 unsigned long flags;
3501
Ralf Baechled203a7e2005-09-06 15:19:37 -07003502 switch (sp->type) {
3503 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3504 struct v4l2_captureparm *cp = &sp->parm.capture;
3505
Ladislav Michla637a112005-09-01 15:07:34 +00003506 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003507
3508 if ((cp->timeperframe.numerator == 0) ||
3509 (cp->timeperframe.denominator == 0)) {
3510 /* reset framerate */
3511 vino_set_default_framerate(vcs);
3512 } else {
3513 vino_set_framerate(vcs, cp->timeperframe.denominator /
3514 cp->timeperframe.numerator);
3515 }
Ladislav Michla637a112005-09-01 15:07:34 +00003516
3517 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003518
3519 // TODO: set buffers according to cp->readbuffers
3520 break;
3521 }
3522 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3523 default:
3524 return -EINVAL;
3525 }
3526
3527 return 0;
3528}
3529
3530static int vino_v4l2_reqbufs(struct vino_channel_settings *vcs,
3531 struct v4l2_requestbuffers *rb)
3532{
3533 if (vcs->reading)
3534 return -EBUSY;
3535
3536 switch (rb->type) {
3537 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3538 // TODO: check queue type
3539 if (rb->memory != V4L2_MEMORY_MMAP) {
3540 dprintk("type not mmap\n");
3541 return -EINVAL;
3542 }
3543
Ralf Baechled203a7e2005-09-06 15:19:37 -07003544 dprintk("count = %d\n", rb->count);
3545 if (rb->count > 0) {
Ladislav Michla637a112005-09-01 15:07:34 +00003546 if (vino_is_capturing(vcs)) {
3547 dprintk("busy, capturing\n");
3548 return -EBUSY;
3549 }
3550
Ralf Baechled203a7e2005-09-06 15:19:37 -07003551 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3552 dprintk("busy, buffers still mapped\n");
3553 return -EBUSY;
3554 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003555 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003556 vino_queue_free(&vcs->fb_queue);
3557 vino_queue_init(&vcs->fb_queue, &rb->count);
3558 }
3559 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003560 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003561 vino_capture_stop(vcs);
3562 vino_queue_free(&vcs->fb_queue);
3563 }
3564 break;
3565 }
3566 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3567 default:
3568 return -EINVAL;
3569 }
3570
3571 return 0;
3572}
3573
3574static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3575 struct vino_framebuffer *fb,
3576 struct v4l2_buffer *b)
3577{
3578 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3579 fb->id)) {
3580 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3581 b->flags |= V4L2_BUF_FLAG_DONE;
3582 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3583 fb->id)) {
3584 b->flags &= ~V4L2_BUF_FLAG_DONE;
3585 b->flags |= V4L2_BUF_FLAG_QUEUED;
3586 } else {
3587 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3588 V4L2_BUF_FLAG_QUEUED);
3589 }
3590
3591 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3592
3593 if (fb->map_count > 0)
3594 b->flags |= V4L2_BUF_FLAG_MAPPED;
3595
3596 b->index = fb->id;
3597 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3598 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3599 b->m.offset = fb->offset;
3600 b->bytesused = fb->data_size;
3601 b->length = fb->size;
3602 b->field = V4L2_FIELD_INTERLACED;
3603 b->sequence = fb->frame_counter;
3604 memcpy(&b->timestamp, &fb->timestamp,
3605 sizeof(struct timeval));
3606 // b->input ?
3607
3608 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3609 fb->id, fb->size, fb->data_size, fb->offset);
3610}
3611
3612static int vino_v4l2_querybuf(struct vino_channel_settings *vcs,
3613 struct v4l2_buffer *b)
3614{
3615 if (vcs->reading)
3616 return -EBUSY;
3617
3618 switch (b->type) {
3619 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3620 struct vino_framebuffer *fb;
3621
3622 // TODO: check queue type
3623 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3624 dprintk("invalid index = %d\n",
3625 b->index);
3626 return -EINVAL;
3627 }
3628
3629 fb = vino_queue_get_buffer(&vcs->fb_queue,
3630 b->index);
3631 if (fb == NULL) {
3632 dprintk("vino_queue_get_buffer() failed");
3633 return -EINVAL;
3634 }
3635
3636 vino_v4l2_get_buffer_status(vcs, fb, b);
3637 break;
3638 }
3639 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3640 default:
3641 return -EINVAL;
3642 }
3643
3644 return 0;
3645}
3646
3647static int vino_v4l2_qbuf(struct vino_channel_settings *vcs,
3648 struct v4l2_buffer *b)
3649{
3650 if (vcs->reading)
3651 return -EBUSY;
3652
3653 switch (b->type) {
3654 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3655 struct vino_framebuffer *fb;
3656 int ret;
3657
3658 // TODO: check queue type
3659 if (b->memory != V4L2_MEMORY_MMAP) {
3660 dprintk("type not mmap\n");
3661 return -EINVAL;
3662 }
3663
3664 fb = vino_capture_enqueue(vcs, b->index);
3665 if (fb == NULL)
3666 return -EINVAL;
3667
3668 vino_v4l2_get_buffer_status(vcs, fb, b);
3669
3670 if (vcs->streaming) {
3671 ret = vino_capture_next(vcs, 1);
3672 if (ret)
3673 return ret;
3674 }
3675 break;
3676 }
3677 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3678 default:
3679 return -EINVAL;
3680 }
3681
3682 return 0;
3683}
3684
3685static int vino_v4l2_dqbuf(struct vino_channel_settings *vcs,
3686 struct v4l2_buffer *b,
3687 unsigned int nonblocking)
3688{
3689 if (vcs->reading)
3690 return -EBUSY;
3691
3692 switch (b->type) {
3693 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3694 struct vino_framebuffer *fb;
3695 unsigned int incoming, outgoing;
3696 int err;
3697
3698 // TODO: check queue type
3699
3700 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3701 if (err) {
3702 dprintk("vino_queue_get_incoming() failed\n");
Ladislav Michla637a112005-09-01 15:07:34 +00003703 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003704 }
3705 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3706 if (err) {
3707 dprintk("vino_queue_get_outgoing() failed\n");
Ladislav Michla637a112005-09-01 15:07:34 +00003708 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003709 }
3710
3711 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3712
3713 if (outgoing == 0) {
3714 if (incoming == 0) {
3715 dprintk("no incoming or outgoing buffers\n");
3716 return -EINVAL;
3717 }
3718 if (nonblocking) {
3719 dprintk("non-blocking I/O was selected and "
3720 "there are no buffers to dequeue\n");
3721 return -EAGAIN;
3722 }
3723
3724 err = vino_wait_for_frame(vcs);
3725 if (err) {
3726 err = vino_wait_for_frame(vcs);
3727 if (err) {
Ladislav Michla637a112005-09-01 15:07:34 +00003728 /* interrupted or
3729 * no frames captured because
3730 * of frame skipping */
3731 // vino_capture_failed(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003732 return -EIO;
3733 }
3734 }
3735 }
3736
3737 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3738 if (fb == NULL) {
3739 dprintk("vino_queue_remove() failed\n");
3740 return -EINVAL;
3741 }
3742
3743 err = vino_check_buffer(vcs, fb);
Ladislav Michla637a112005-09-01 15:07:34 +00003744
3745 vino_v4l2_get_buffer_status(vcs, fb, b);
3746
Ralf Baechled203a7e2005-09-06 15:19:37 -07003747 if (err)
3748 return -EIO;
3749
Ralf Baechled203a7e2005-09-06 15:19:37 -07003750 break;
3751 }
3752 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3753 default:
3754 return -EINVAL;
3755 }
3756
3757 return 0;
3758}
3759
3760static int vino_v4l2_streamon(struct vino_channel_settings *vcs)
3761{
3762 unsigned int incoming;
3763 int ret;
3764 if (vcs->reading)
3765 return -EBUSY;
3766
3767 if (vcs->streaming)
3768 return 0;
3769
3770 // TODO: check queue type
3771
3772 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3773 dprintk("no buffers allocated\n");
3774 return -EINVAL;
3775 }
3776
3777 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3778 if (ret) {
3779 dprintk("vino_queue_get_incoming() failed\n");
3780 return -EINVAL;
3781 }
3782
3783 vcs->streaming = 1;
3784
3785 if (incoming > 0) {
3786 ret = vino_capture_next(vcs, 1);
3787 if (ret) {
3788 vcs->streaming = 0;
3789
3790 dprintk("couldn't start capture\n");
3791 return -EINVAL;
3792 }
3793 }
3794
3795 return 0;
3796}
3797
3798static int vino_v4l2_streamoff(struct vino_channel_settings *vcs)
3799{
3800 if (vcs->reading)
3801 return -EBUSY;
3802
3803 if (!vcs->streaming)
3804 return 0;
3805
Ralf Baechled203a7e2005-09-06 15:19:37 -07003806 vcs->streaming = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00003807 vino_capture_stop(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003808
3809 return 0;
3810}
3811
3812static int vino_v4l2_queryctrl(struct vino_channel_settings *vcs,
3813 struct v4l2_queryctrl *queryctrl)
3814{
Ladislav Michla637a112005-09-01 15:07:34 +00003815 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003816 int i;
3817 int err = 0;
3818
Ladislav Michla637a112005-09-01 15:07:34 +00003819 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003820
3821 switch (vcs->input) {
3822 case VINO_INPUT_D1:
3823 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3824 if (vino_indycam_v4l2_controls[i].id ==
3825 queryctrl->id) {
3826 memcpy(queryctrl,
3827 &vino_indycam_v4l2_controls[i],
3828 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003829 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003830 goto found;
3831 }
3832 }
3833
3834 err = -EINVAL;
3835 break;
3836 case VINO_INPUT_COMPOSITE:
3837 case VINO_INPUT_SVIDEO:
3838 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3839 if (vino_saa7191_v4l2_controls[i].id ==
3840 queryctrl->id) {
3841 memcpy(queryctrl,
3842 &vino_saa7191_v4l2_controls[i],
3843 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003844 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003845 goto found;
3846 }
3847 }
3848
3849 err = -EINVAL;
3850 break;
3851 default:
3852 err = -EINVAL;
3853 }
3854
3855 found:
Ladislav Michla637a112005-09-01 15:07:34 +00003856 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003857
3858 return err;
3859}
3860
3861static int vino_v4l2_g_ctrl(struct vino_channel_settings *vcs,
3862 struct v4l2_control *control)
3863{
Ladislav Michla637a112005-09-01 15:07:34 +00003864 unsigned long flags;
3865 int i;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003866 int err = 0;
3867
Ladislav Michla637a112005-09-01 15:07:34 +00003868 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003869
3870 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003871 case VINO_INPUT_D1: {
3872 struct indycam_control indycam_ctrl;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003873
Ladislav Michla637a112005-09-01 15:07:34 +00003874 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3875 if (vino_indycam_v4l2_controls[i].id ==
3876 control->id) {
3877 goto found1;
3878 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003879 }
Ladislav Michla637a112005-09-01 15:07:34 +00003880
3881 err = -EINVAL;
3882 goto out;
3883
3884found1:
3885 indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
3886
3887 err = i2c_camera_command(DECODER_INDYCAM_GET_CONTROL,
3888 &indycam_ctrl);
3889 if (err) {
3890 err = -EINVAL;
3891 goto out;
3892 }
3893
3894 control->value = indycam_ctrl.value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003895 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003896 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003897 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003898 case VINO_INPUT_SVIDEO: {
3899 struct saa7191_control saa7191_ctrl;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003900
Ladislav Michla637a112005-09-01 15:07:34 +00003901 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3902 if (vino_saa7191_v4l2_controls[i].id ==
3903 control->id) {
3904 goto found2;
3905 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003906 }
Ladislav Michla637a112005-09-01 15:07:34 +00003907
3908 err = -EINVAL;
3909 goto out;
3910
3911found2:
3912 saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
3913
3914 err = i2c_decoder_command(DECODER_SAA7191_GET_CONTROL,
3915 &saa7191_ctrl);
3916 if (err) {
3917 err = -EINVAL;
3918 goto out;
3919 }
3920
3921 control->value = saa7191_ctrl.value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003922 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003923 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003924 default:
3925 err = -EINVAL;
3926 }
3927
Ladislav Michla637a112005-09-01 15:07:34 +00003928out:
3929 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003930
3931 return err;
3932}
3933
3934static int vino_v4l2_s_ctrl(struct vino_channel_settings *vcs,
3935 struct v4l2_control *control)
3936{
Ladislav Michla637a112005-09-01 15:07:34 +00003937 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003938 int i;
3939 int err = 0;
3940
Ladislav Michla637a112005-09-01 15:07:34 +00003941 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3942
3943 if (!vino_is_input_owner(vcs)) {
3944 err = -EBUSY;
3945 goto out;
3946 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003947
3948 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003949 case VINO_INPUT_D1: {
3950 struct indycam_control indycam_ctrl;
3951
Ralf Baechled203a7e2005-09-06 15:19:37 -07003952 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3953 if (vino_indycam_v4l2_controls[i].id ==
3954 control->id) {
3955 if ((control->value >=
3956 vino_indycam_v4l2_controls[i].minimum)
3957 && (control->value <=
3958 vino_indycam_v4l2_controls[i].
3959 maximum)) {
Ladislav Michla637a112005-09-01 15:07:34 +00003960 goto found1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003961 } else {
3962 err = -ERANGE;
Ladislav Michla637a112005-09-01 15:07:34 +00003963 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003964 }
3965 }
3966 }
Ladislav Michla637a112005-09-01 15:07:34 +00003967
Ralf Baechled203a7e2005-09-06 15:19:37 -07003968 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003969 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003970
Ladislav Michla637a112005-09-01 15:07:34 +00003971found1:
3972 indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
3973 indycam_ctrl.value = control->value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003974
Ladislav Michla637a112005-09-01 15:07:34 +00003975 err = i2c_camera_command(DECODER_INDYCAM_SET_CONTROL,
3976 &indycam_ctrl);
3977 if (err)
3978 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003979 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003980 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003981 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003982 case VINO_INPUT_SVIDEO: {
3983 struct saa7191_control saa7191_ctrl;
3984
Ralf Baechled203a7e2005-09-06 15:19:37 -07003985 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3986 if (vino_saa7191_v4l2_controls[i].id ==
3987 control->id) {
3988 if ((control->value >=
3989 vino_saa7191_v4l2_controls[i].minimum)
3990 && (control->value <=
3991 vino_saa7191_v4l2_controls[i].
3992 maximum)) {
Ladislav Michla637a112005-09-01 15:07:34 +00003993 goto found2;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003994 } else {
3995 err = -ERANGE;
Ladislav Michla637a112005-09-01 15:07:34 +00003996 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003997 }
3998 }
3999 }
4000 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00004001 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004002
Ladislav Michla637a112005-09-01 15:07:34 +00004003found2:
4004 saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
4005 saa7191_ctrl.value = control->value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004006
Ladislav Michla637a112005-09-01 15:07:34 +00004007 err = i2c_decoder_command(DECODER_SAA7191_SET_CONTROL,
4008 &saa7191_ctrl);
4009 if (err)
4010 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004011 break;
Ladislav Michla637a112005-09-01 15:07:34 +00004012 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004013 default:
4014 err = -EINVAL;
4015 }
4016
Ladislav Michla637a112005-09-01 15:07:34 +00004017out:
4018 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004019
4020 return err;
4021}
4022
4023/* File operations */
4024
4025static int vino_open(struct inode *inode, struct file *file)
4026{
4027 struct video_device *dev = video_devdata(file);
4028 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4029 int ret = 0;
4030 dprintk("open(): channel = %c\n",
4031 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
4032
Ingo Molnar3593cab2006-02-07 06:49:14 -02004033 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004034
4035 if (vcs->users) {
4036 dprintk("open(): driver busy\n");
4037 ret = -EBUSY;
4038 goto out;
4039 }
4040
4041 ret = vino_acquire_input(vcs);
4042 if (ret) {
4043 dprintk("open(): vino_acquire_input() failed\n");
4044 goto out;
4045 }
4046
4047 vcs->users++;
4048
4049 out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02004050 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004051
4052 dprintk("open(): %s!\n", ret ? "failed" : "complete");
4053
4054 return ret;
4055}
4056
4057static int vino_close(struct inode *inode, struct file *file)
4058{
4059 struct video_device *dev = video_devdata(file);
4060 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4061 dprintk("close():\n");
4062
Ingo Molnar3593cab2006-02-07 06:49:14 -02004063 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004064
4065 vcs->users--;
4066
4067 if (!vcs->users) {
4068 vino_release_input(vcs);
4069
4070 /* stop DMA and free buffers */
4071 vino_capture_stop(vcs);
4072 vino_queue_free(&vcs->fb_queue);
4073 }
4074
Ingo Molnar3593cab2006-02-07 06:49:14 -02004075 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004076
4077 return 0;
4078}
4079
4080static void vino_vm_open(struct vm_area_struct *vma)
4081{
4082 struct vino_framebuffer *fb = vma->vm_private_data;
4083
4084 fb->map_count++;
4085 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
4086}
4087
4088static void vino_vm_close(struct vm_area_struct *vma)
4089{
4090 struct vino_framebuffer *fb = vma->vm_private_data;
4091
4092 fb->map_count--;
4093 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
4094}
4095
4096static struct vm_operations_struct vino_vm_ops = {
4097 .open = vino_vm_open,
4098 .close = vino_vm_close,
4099};
4100
4101static int vino_mmap(struct file *file, struct vm_area_struct *vma)
4102{
4103 struct video_device *dev = video_devdata(file);
4104 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4105
4106 unsigned long start = vma->vm_start;
4107 unsigned long size = vma->vm_end - vma->vm_start;
4108 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4109
4110 struct vino_framebuffer *fb = NULL;
4111 unsigned int i, length;
4112 int ret = 0;
4113
4114 dprintk("mmap():\n");
4115
4116 // TODO: reject mmap if already mapped
4117
Ingo Molnar3593cab2006-02-07 06:49:14 -02004118 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004119 return -EINTR;
4120
4121 if (vcs->reading) {
4122 ret = -EBUSY;
4123 goto out;
4124 }
4125
4126 // TODO: check queue type
4127
4128 if (!(vma->vm_flags & VM_WRITE)) {
4129 dprintk("mmap(): app bug: PROT_WRITE please\n");
4130 ret = -EINVAL;
4131 goto out;
4132 }
4133 if (!(vma->vm_flags & VM_SHARED)) {
4134 dprintk("mmap(): app bug: MAP_SHARED please\n");
4135 ret = -EINVAL;
4136 goto out;
4137 }
4138
4139 /* find the correct buffer using offset */
4140 length = vino_queue_get_length(&vcs->fb_queue);
4141 if (length == 0) {
4142 dprintk("mmap(): queue not initialized\n");
4143 ret = -EINVAL;
4144 goto out;
4145 }
4146
4147 for (i = 0; i < length; i++) {
4148 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
4149 if (fb == NULL) {
4150 dprintk("mmap(): vino_queue_get_buffer() failed\n");
4151 ret = -EINVAL;
4152 goto out;
4153 }
4154
4155 if (fb->offset == offset)
4156 goto found;
4157 }
4158
4159 dprintk("mmap(): invalid offset = %lu\n", offset);
4160 ret = -EINVAL;
4161 goto out;
4162
4163found:
4164 dprintk("mmap(): buffer = %d\n", i);
4165
4166 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
4167 dprintk("mmap(): failed: size = %lu > %lu\n",
4168 size, fb->desc_table.page_count * PAGE_SIZE);
4169 ret = -EINVAL;
4170 goto out;
4171 }
4172
4173 for (i = 0; i < fb->desc_table.page_count; i++) {
4174 unsigned long pfn =
4175 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
4176 PAGE_SHIFT;
4177
4178 if (size < PAGE_SIZE)
4179 break;
4180
4181 // protection was: PAGE_READONLY
4182 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
4183 vma->vm_page_prot)) {
4184 dprintk("mmap(): remap_pfn_range() failed\n");
4185 ret = -EAGAIN;
4186 goto out;
4187 }
4188
4189 start += PAGE_SIZE;
4190 size -= PAGE_SIZE;
4191 }
4192
4193 fb->map_count = 1;
4194
4195 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
4196 vma->vm_flags &= ~VM_IO;
4197 vma->vm_private_data = fb;
4198 vma->vm_file = file;
4199 vma->vm_ops = &vino_vm_ops;
4200
4201out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02004202 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004203
4204 return ret;
4205}
4206
4207static unsigned int vino_poll(struct file *file, poll_table *pt)
4208{
4209 struct video_device *dev = video_devdata(file);
4210 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4211 unsigned int outgoing;
4212 unsigned int ret = 0;
4213
4214 // lock mutex (?)
4215 // TODO: this has to be corrected for different read modes
4216
4217 dprintk("poll():\n");
4218
4219 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4220 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4221 ret = POLLERR;
4222 goto error;
4223 }
4224 if (outgoing > 0)
4225 goto over;
4226
4227 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
4228
4229 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4230 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4231 ret = POLLERR;
4232 goto error;
4233 }
4234
4235over:
4236 dprintk("poll(): data %savailable\n",
4237 (outgoing > 0) ? "" : "not ");
Ladislav Michla637a112005-09-01 15:07:34 +00004238
4239 if (outgoing > 0)
Ralf Baechled203a7e2005-09-06 15:19:37 -07004240 ret = POLLIN | POLLRDNORM;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004241
4242error:
4243
4244 return ret;
4245}
4246
4247static int vino_do_ioctl(struct inode *inode, struct file *file,
4248 unsigned int cmd, void *arg)
4249{
4250 struct video_device *dev = video_devdata(file);
4251 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4252
Ladislav Michla637a112005-09-01 15:07:34 +00004253#ifdef VINO_DEBUG
Ralf Baechled203a7e2005-09-06 15:19:37 -07004254 switch (_IOC_TYPE(cmd)) {
4255 case 'v':
4256 dprintk("ioctl(): V4L1 unsupported (0x%08x)\n", cmd);
4257 break;
4258 case 'V':
4259 dprintk("ioctl(): V4L2 %s (0x%08x)\n",
4260 v4l2_ioctl_names[_IOC_NR(cmd)], cmd);
4261 break;
4262 default:
4263 dprintk("ioctl(): unsupported command 0x%08x\n", cmd);
4264 }
Ladislav Michla637a112005-09-01 15:07:34 +00004265#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07004266
4267 switch (cmd) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004268 /* V4L2 interface */
4269 case VIDIOC_QUERYCAP: {
4270 vino_v4l2_querycap(arg);
4271 break;
4272 }
4273 case VIDIOC_ENUMINPUT: {
4274 return vino_v4l2_enuminput(vcs, arg);
4275 }
4276 case VIDIOC_G_INPUT: {
4277 return vino_v4l2_g_input(vcs, arg);
4278 }
4279 case VIDIOC_S_INPUT: {
4280 return vino_v4l2_s_input(vcs, arg);
4281 }
4282 case VIDIOC_ENUMSTD: {
4283 return vino_v4l2_enumstd(vcs, arg);
4284 }
Ladislav Michla637a112005-09-01 15:07:34 +00004285 case VIDIOC_QUERYSTD: {
4286 return vino_v4l2_querystd(vcs, arg);
4287 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004288 case VIDIOC_G_STD: {
4289 return vino_v4l2_g_std(vcs, arg);
4290 }
4291 case VIDIOC_S_STD: {
4292 return vino_v4l2_s_std(vcs, arg);
4293 }
4294 case VIDIOC_ENUM_FMT: {
4295 return vino_v4l2_enum_fmt(vcs, arg);
4296 }
4297 case VIDIOC_TRY_FMT: {
4298 return vino_v4l2_try_fmt(vcs, arg);
4299 }
4300 case VIDIOC_G_FMT: {
4301 return vino_v4l2_g_fmt(vcs, arg);
4302 }
4303 case VIDIOC_S_FMT: {
4304 return vino_v4l2_s_fmt(vcs, arg);
4305 }
4306 case VIDIOC_CROPCAP: {
4307 return vino_v4l2_cropcap(vcs, arg);
4308 }
4309 case VIDIOC_G_CROP: {
4310 return vino_v4l2_g_crop(vcs, arg);
4311 }
4312 case VIDIOC_S_CROP: {
4313 return vino_v4l2_s_crop(vcs, arg);
4314 }
4315 case VIDIOC_G_PARM: {
4316 return vino_v4l2_g_parm(vcs, arg);
4317 }
4318 case VIDIOC_S_PARM: {
4319 return vino_v4l2_s_parm(vcs, arg);
4320 }
4321 case VIDIOC_REQBUFS: {
4322 return vino_v4l2_reqbufs(vcs, arg);
4323 }
4324 case VIDIOC_QUERYBUF: {
4325 return vino_v4l2_querybuf(vcs, arg);
4326 }
4327 case VIDIOC_QBUF: {
4328 return vino_v4l2_qbuf(vcs, arg);
4329 }
4330 case VIDIOC_DQBUF: {
4331 return vino_v4l2_dqbuf(vcs, arg, file->f_flags & O_NONBLOCK);
4332 }
4333 case VIDIOC_STREAMON: {
4334 return vino_v4l2_streamon(vcs);
4335 }
4336 case VIDIOC_STREAMOFF: {
4337 return vino_v4l2_streamoff(vcs);
4338 }
4339 case VIDIOC_QUERYCTRL: {
4340 return vino_v4l2_queryctrl(vcs, arg);
4341 }
4342 case VIDIOC_G_CTRL: {
4343 return vino_v4l2_g_ctrl(vcs, arg);
4344 }
4345 case VIDIOC_S_CTRL: {
4346 return vino_v4l2_s_ctrl(vcs, arg);
4347 }
4348 default:
4349 return -ENOIOCTLCMD;
4350 }
4351
4352 return 0;
4353}
4354
4355static int vino_ioctl(struct inode *inode, struct file *file,
4356 unsigned int cmd, unsigned long arg)
4357{
4358 struct video_device *dev = video_devdata(file);
4359 struct vino_channel_settings *vcs = video_get_drvdata(dev);
4360 int ret;
4361
Ingo Molnar3593cab2006-02-07 06:49:14 -02004362 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004363 return -EINTR;
4364
4365 ret = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
4366
Ingo Molnar3593cab2006-02-07 06:49:14 -02004367 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004368
4369 return ret;
4370}
4371
4372/* Initialization and cleanup */
4373
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03004374/* __initdata */
4375static int vino_init_stage;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004376
Arjan van de Venfa027c22007-02-12 00:55:33 -08004377static const struct file_operations vino_fops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004378 .owner = THIS_MODULE,
4379 .open = vino_open,
4380 .release = vino_close,
4381 .ioctl = vino_ioctl,
4382 .mmap = vino_mmap,
4383 .poll = vino_poll,
4384 .llseek = no_llseek,
4385};
4386
4387static struct video_device v4l_device_template = {
4388 .name = "NOT SET",
Ralf Baechled203a7e2005-09-06 15:19:37 -07004389 .fops = &vino_fops,
4390 .minor = -1,
4391};
4392
4393static void vino_module_cleanup(int stage)
4394{
4395 switch(stage) {
4396 case 10:
4397 video_unregister_device(vino_drvdata->b.v4l_device);
4398 vino_drvdata->b.v4l_device = NULL;
4399 case 9:
4400 video_unregister_device(vino_drvdata->a.v4l_device);
4401 vino_drvdata->a.v4l_device = NULL;
4402 case 8:
4403 vino_i2c_del_bus();
4404 case 7:
4405 free_irq(SGI_VINO_IRQ, NULL);
4406 case 6:
4407 if (vino_drvdata->b.v4l_device) {
4408 video_device_release(vino_drvdata->b.v4l_device);
4409 vino_drvdata->b.v4l_device = NULL;
4410 }
4411 case 5:
4412 if (vino_drvdata->a.v4l_device) {
4413 video_device_release(vino_drvdata->a.v4l_device);
4414 vino_drvdata->a.v4l_device = NULL;
4415 }
4416 case 4:
4417 /* all entries in dma_cpu dummy table have the same address */
4418 dma_unmap_single(NULL,
4419 vino_drvdata->dummy_desc_table.dma_cpu[0],
4420 PAGE_SIZE, DMA_FROM_DEVICE);
4421 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4422 * sizeof(dma_addr_t),
4423 (void *)vino_drvdata->
4424 dummy_desc_table.dma_cpu,
4425 vino_drvdata->dummy_desc_table.dma);
4426 case 3:
4427 free_page(vino_drvdata->dummy_page);
4428 case 2:
4429 kfree(vino_drvdata);
4430 case 1:
4431 iounmap(vino);
4432 case 0:
4433 break;
4434 default:
4435 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4436 stage);
4437 }
4438}
4439
4440static int vino_probe(void)
4441{
4442 unsigned long rev_id;
4443
4444 if (ip22_is_fullhouse()) {
4445 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4446 return -ENODEV;
4447 }
4448
4449 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4450 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4451 return -ENODEV;
4452 }
4453
4454 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4455 if (!vino) {
4456 printk(KERN_ERR "VINO: ioremap() failed\n");
4457 return -EIO;
4458 }
4459 vino_init_stage++;
4460
4461 if (get_dbe(rev_id, &(vino->rev_id))) {
4462 printk(KERN_ERR "Failed to read VINO revision register\n");
4463 vino_module_cleanup(vino_init_stage);
4464 return -ENODEV;
4465 }
4466
4467 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4468 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4469 rev_id);
4470 vino_module_cleanup(vino_init_stage);
4471 return -ENODEV;
4472 }
4473
Ladislav Michla637a112005-09-01 15:07:34 +00004474 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004475
4476 return 0;
4477}
4478
4479static int vino_init(void)
4480{
4481 dma_addr_t dma_dummy_address;
4482 int i;
4483
Panagiotis Issaris74081872006-01-11 19:40:56 -02004484 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004485 if (!vino_drvdata) {
4486 vino_module_cleanup(vino_init_stage);
4487 return -ENOMEM;
4488 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004489 vino_init_stage++;
4490
4491 /* create a dummy dma descriptor */
4492 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4493 if (!vino_drvdata->dummy_page) {
4494 vino_module_cleanup(vino_init_stage);
4495 return -ENOMEM;
4496 }
4497 vino_init_stage++;
4498
4499 // TODO: use page_count in dummy_desc_table
4500
4501 vino_drvdata->dummy_desc_table.dma_cpu =
4502 dma_alloc_coherent(NULL,
4503 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4504 &vino_drvdata->dummy_desc_table.dma,
4505 GFP_KERNEL | GFP_DMA);
4506 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4507 vino_module_cleanup(vino_init_stage);
4508 return -ENOMEM;
4509 }
4510 vino_init_stage++;
4511
4512 dma_dummy_address = dma_map_single(NULL,
4513 (void *)vino_drvdata->dummy_page,
4514 PAGE_SIZE, DMA_FROM_DEVICE);
4515 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4516 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4517 }
4518
4519 /* initialize VINO */
4520
4521 vino->control = 0;
4522 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4523 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4524 udelay(VINO_DESC_FETCH_DELAY);
4525
4526 vino->intr_status = 0;
4527
4528 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4529 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4530
4531 return 0;
4532}
4533
4534static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4535 unsigned int channel, const char *name)
4536{
4537 vcs->channel = channel;
4538 vcs->input = VINO_INPUT_NONE;
4539 vcs->alpha = 0;
4540 vcs->users = 0;
4541 vcs->data_format = VINO_DATA_FMT_GREY;
4542 vcs->data_norm = VINO_DATA_NORM_NTSC;
4543 vcs->decimation = 1;
4544 vino_set_default_clipping(vcs);
4545 vino_set_default_framerate(vcs);
4546
4547 vcs->capturing = 0;
4548
Ingo Molnar3593cab2006-02-07 06:49:14 -02004549 mutex_init(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004550 spin_lock_init(&vcs->capture_lock);
4551
Ingo Molnar3593cab2006-02-07 06:49:14 -02004552 mutex_init(&vcs->fb_queue.queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004553 spin_lock_init(&vcs->fb_queue.queue_lock);
4554 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4555
4556 vcs->v4l_device = video_device_alloc();
4557 if (!vcs->v4l_device) {
4558 vino_module_cleanup(vino_init_stage);
4559 return -ENOMEM;
4560 }
4561 vino_init_stage++;
4562
4563 memcpy(vcs->v4l_device, &v4l_device_template,
4564 sizeof(struct video_device));
4565 strcpy(vcs->v4l_device->name, name);
4566 vcs->v4l_device->release = video_device_release;
4567
4568 video_set_drvdata(vcs->v4l_device, vcs);
4569
4570 return 0;
4571}
4572
4573static int __init vino_module_init(void)
4574{
4575 int ret;
4576
4577 printk(KERN_INFO "SGI VINO driver version %s\n",
4578 VINO_MODULE_VERSION);
4579
4580 ret = vino_probe();
4581 if (ret)
4582 return ret;
4583
4584 ret = vino_init();
4585 if (ret)
4586 return ret;
4587
4588 /* initialize data structures */
4589
4590 spin_lock_init(&vino_drvdata->vino_lock);
4591 spin_lock_init(&vino_drvdata->input_lock);
4592
4593 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4594 vino_v4l_device_name_a);
4595 if (ret)
4596 return ret;
4597
4598 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4599 vino_v4l_device_name_b);
4600 if (ret)
4601 return ret;
4602
4603 /* initialize hardware and register V4L devices */
4604
4605 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4606 vino_driver_description, NULL);
4607 if (ret) {
4608 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4609 SGI_VINO_IRQ);
4610 vino_module_cleanup(vino_init_stage);
4611 return -EAGAIN;
4612 }
4613 vino_init_stage++;
4614
4615 ret = vino_i2c_add_bus();
4616 if (ret) {
4617 printk(KERN_ERR "VINO I2C bus registration failed\n");
4618 vino_module_cleanup(vino_init_stage);
4619 return ret;
4620 }
4621 vino_init_stage++;
4622
4623 ret = video_register_device(vino_drvdata->a.v4l_device,
4624 VFL_TYPE_GRABBER, -1);
4625 if (ret < 0) {
4626 printk(KERN_ERR "VINO channel A Video4Linux-device "
4627 "registration failed\n");
4628 vino_module_cleanup(vino_init_stage);
4629 return -EINVAL;
4630 }
4631 vino_init_stage++;
4632
4633 ret = video_register_device(vino_drvdata->b.v4l_device,
4634 VFL_TYPE_GRABBER, -1);
4635 if (ret < 0) {
4636 printk(KERN_ERR "VINO channel B Video4Linux-device "
4637 "registration failed\n");
4638 vino_module_cleanup(vino_init_stage);
4639 return -EINVAL;
4640 }
4641 vino_init_stage++;
4642
4643#if defined(CONFIG_KMOD) && defined(MODULE)
4644 request_module("saa7191");
4645 request_module("indycam");
4646#endif
4647
4648 dprintk("init complete!\n");
4649
4650 return 0;
4651}
4652
4653static void __exit vino_module_exit(void)
4654{
4655 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4656 vino_module_cleanup(vino_init_stage);
4657 dprintk("cleanup complete, exit!\n");
4658}
4659
4660module_init(vino_module_init);
4661module_exit(vino_module_exit);