blob: 2ce2fe594cdf8d537fab5fcb26c5299cae412047 [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#include <linux/kmod.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/i2c.h>
36#include <linux/i2c-algo-sgi.h>
37
Hans Verkuil33b687c2008-07-25 05:32:50 -030038#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030039#include <media/v4l2-common.h>
Yoichi Yuasac2cfcf72008-07-29 05:30:58 -030040#include <media/v4l2-ioctl.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070041#include <linux/video_decoder.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020042#include <linux/mutex.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/paccess.h>
45#include <asm/io.h>
46#include <asm/sgi/ip22.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/sgi/mc.h>
48
49#include "vino.h"
Ralf Baechled203a7e2005-09-06 15:19:37 -070050#include "saa7191.h"
51#include "indycam.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Ralf Baechled203a7e2005-09-06 15:19:37 -070053/* Uncomment the following line to get lots and lots of (mostly useless)
54 * debug info.
55 * Note that the debug output also slows down the driver significantly */
56// #define VINO_DEBUG
Ladislav Michla637a112005-09-01 15:07:34 +000057// #define VINO_DEBUG_INT
Ralf Baechled203a7e2005-09-06 15:19:37 -070058
Ladislav Michla637a112005-09-01 15:07:34 +000059#define VINO_MODULE_VERSION "0.0.5"
60#define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
Ralf Baechled203a7e2005-09-06 15:19:37 -070061
62MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
63MODULE_VERSION(VINO_MODULE_VERSION);
64MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
65MODULE_LICENSE("GPL");
66
Ralf Baechled203a7e2005-09-06 15:19:37 -070067#ifdef VINO_DEBUG
68#define dprintk(x...) printk("VINO: " x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#else
Ralf Baechled203a7e2005-09-06 15:19:37 -070070#define dprintk(x...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
Ralf Baechled203a7e2005-09-06 15:19:37 -070073#define VINO_NO_CHANNEL 0
74#define VINO_CHANNEL_A 1
75#define VINO_CHANNEL_B 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ralf Baechled203a7e2005-09-06 15:19:37 -070077#define VINO_PAL_WIDTH 768
78#define VINO_PAL_HEIGHT 576
79#define VINO_NTSC_WIDTH 640
80#define VINO_NTSC_HEIGHT 480
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ralf Baechled203a7e2005-09-06 15:19:37 -070082#define VINO_MIN_WIDTH 32
83#define VINO_MIN_HEIGHT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Ralf Baechled203a7e2005-09-06 15:19:37 -070085#define VINO_CLIPPING_START_ODD_D1 1
Ladislav Michla637a112005-09-01 15:07:34 +000086#define VINO_CLIPPING_START_ODD_PAL 15
87#define VINO_CLIPPING_START_ODD_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070088
89#define VINO_CLIPPING_START_EVEN_D1 2
Ladislav Michla637a112005-09-01 15:07:34 +000090#define VINO_CLIPPING_START_EVEN_PAL 15
91#define VINO_CLIPPING_START_EVEN_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070092
93#define VINO_INPUT_CHANNEL_COUNT 3
94
Ladislav Michla637a112005-09-01 15:07:34 +000095/* the number is the index for vino_inputs */
Ralf Baechled203a7e2005-09-06 15:19:37 -070096#define VINO_INPUT_NONE -1
97#define VINO_INPUT_COMPOSITE 0
98#define VINO_INPUT_SVIDEO 1
99#define VINO_INPUT_D1 2
100
101#define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
102
Ladislav Michla637a112005-09-01 15:07:34 +0000103#define VINO_FIFO_THRESHOLD_DEFAULT 16
Ralf Baechled203a7e2005-09-06 15:19:37 -0700104
Ralf Baechled203a7e2005-09-06 15:19:37 -0700105#define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
106 * VINO_PAL_HEIGHT * 4 \
107 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
108
Ladislav Michla637a112005-09-01 15:07:34 +0000109#define VINO_FRAMEBUFFER_COUNT_MAX 8
Ralf Baechled203a7e2005-09-06 15:19:37 -0700110
111#define VINO_FRAMEBUFFER_UNUSED 0
112#define VINO_FRAMEBUFFER_IN_USE 1
113#define VINO_FRAMEBUFFER_READY 2
114
115#define VINO_QUEUE_ERROR -1
116#define VINO_QUEUE_MAGIC 0x20050125
117
118#define VINO_MEMORY_NONE 0
119#define VINO_MEMORY_MMAP 1
120#define VINO_MEMORY_USERPTR 2
121
122#define VINO_DUMMY_DESC_COUNT 4
123#define VINO_DESC_FETCH_DELAY 5 /* microseconds */
124
Ladislav Michla637a112005-09-01 15:07:34 +0000125#define VINO_MAX_FRAME_SKIP_COUNT 128
126
Ralf Baechled203a7e2005-09-06 15:19:37 -0700127/* the number is the index for vino_data_formats */
128#define VINO_DATA_FMT_NONE -1
129#define VINO_DATA_FMT_GREY 0
130#define VINO_DATA_FMT_RGB332 1
131#define VINO_DATA_FMT_RGB32 2
132#define VINO_DATA_FMT_YUV 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700133
134#define VINO_DATA_FMT_COUNT 4
135
Ladislav Michla637a112005-09-01 15:07:34 +0000136/* the number is the index for vino_data_norms */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700137#define VINO_DATA_NORM_NONE -1
138#define VINO_DATA_NORM_NTSC 0
139#define VINO_DATA_NORM_PAL 1
140#define VINO_DATA_NORM_SECAM 2
141#define VINO_DATA_NORM_D1 3
Ladislav Michla637a112005-09-01 15:07:34 +0000142/* The following are special entries that can be used to
Ralf Baechled203a7e2005-09-06 15:19:37 -0700143 * autodetect the norm. */
Ladislav Michla637a112005-09-01 15:07:34 +0000144#define VINO_DATA_NORM_AUTO 0xfe
145#define VINO_DATA_NORM_AUTO_EXT 0xff
Ralf Baechled203a7e2005-09-06 15:19:37 -0700146
147#define VINO_DATA_NORM_COUNT 4
148
149/* Internal data structure definitions */
150
151struct vino_input {
152 char *name;
153 v4l2_std_id std;
154};
155
156struct vino_clipping {
157 unsigned int left, right, top, bottom;
158};
159
160struct vino_data_format {
161 /* the description */
162 char *description;
163 /* bytes per pixel */
164 unsigned int bpp;
165 /* V4L2 fourcc code */
166 __u32 pixelformat;
167 /* V4L2 colorspace (duh!) */
168 enum v4l2_colorspace colorspace;
169};
170
171struct vino_data_norm {
172 char *description;
173 unsigned int width, height;
174 struct vino_clipping odd;
175 struct vino_clipping even;
176
177 v4l2_std_id std;
178 unsigned int fps_min, fps_max;
179 __u32 framelines;
180};
181
182struct vino_descriptor_table {
183 /* the number of PAGE_SIZE sized pages in the buffer */
184 unsigned int page_count;
185 /* virtual (kmalloc'd) pointers to the actual data
186 * (in PAGE_SIZE chunks, used with mmap streaming) */
187 unsigned long *virtual;
188
189 /* cpu address for the VINO descriptor table
190 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
191 unsigned long *dma_cpu;
192 /* dma address for the VINO descriptor table
193 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
194 dma_addr_t dma;
195};
196
197struct vino_framebuffer {
198 /* identifier nubmer */
199 unsigned int id;
200 /* the length of the whole buffer */
201 unsigned int size;
202 /* the length of actual data in buffer */
203 unsigned int data_size;
204 /* the data format */
205 unsigned int data_format;
206 /* the state of buffer data */
207 unsigned int state;
208 /* is the buffer mapped in user space? */
209 unsigned int map_count;
210 /* memory offset for mmap() */
211 unsigned int offset;
212 /* frame counter */
213 unsigned int frame_counter;
214 /* timestamp (written when image capture finishes) */
215 struct timeval timestamp;
216
217 struct vino_descriptor_table desc_table;
218
219 spinlock_t state_lock;
220};
221
222struct vino_framebuffer_fifo {
223 unsigned int length;
224
225 unsigned int used;
226 unsigned int head;
227 unsigned int tail;
228
Ladislav Michla637a112005-09-01 15:07:34 +0000229 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700230};
231
232struct vino_framebuffer_queue {
233 unsigned int magic;
234
235 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
236 unsigned int type;
237 unsigned int length;
238
239 /* data field of in and out contain index numbers for buffer */
240 struct vino_framebuffer_fifo in;
241 struct vino_framebuffer_fifo out;
242
Ladislav Michla637a112005-09-01 15:07:34 +0000243 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700244
245 spinlock_t queue_lock;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200246 struct mutex queue_mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700247 wait_queue_head_t frame_wait_queue;
248};
249
Ladislav Michla637a112005-09-01 15:07:34 +0000250struct vino_interrupt_data {
251 struct timeval timestamp;
252 unsigned int frame_counter;
253 unsigned int skip_count;
254 unsigned int skip;
255};
256
Ralf Baechled203a7e2005-09-06 15:19:37 -0700257struct vino_channel_settings {
258 unsigned int channel;
259
260 int input;
261 unsigned int data_format;
262 unsigned int data_norm;
263 struct vino_clipping clipping;
264 unsigned int decimation;
265 unsigned int line_size;
266 unsigned int alpha;
267 unsigned int fps;
268 unsigned int framert_reg;
269
270 unsigned int fifo_threshold;
271
272 struct vino_framebuffer_queue fb_queue;
273
274 /* number of the current field */
275 unsigned int field;
276
277 /* read in progress */
278 int reading;
279 /* streaming is active */
280 int streaming;
281 /* the driver is currently processing the queue */
282 int capturing;
283
Ingo Molnar3593cab2006-02-07 06:49:14 -0200284 struct mutex mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700285 spinlock_t capture_lock;
286
287 unsigned int users;
288
Ladislav Michla637a112005-09-01 15:07:34 +0000289 struct vino_interrupt_data int_data;
290
Ralf Baechled203a7e2005-09-06 15:19:37 -0700291 /* V4L support */
292 struct video_device *v4l_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
295struct vino_client {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700296 /* the channel which owns this client:
297 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
298 unsigned int owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct i2c_client *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Ralf Baechled203a7e2005-09-06 15:19:37 -0700302struct vino_settings {
303 struct vino_channel_settings a;
304 struct vino_channel_settings b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 struct vino_client decoder;
307 struct vino_client camera;
308
Ralf Baechled203a7e2005-09-06 15:19:37 -0700309 /* a lock for vino register access */
310 spinlock_t vino_lock;
311 /* a lock for channel input changes */
312 spinlock_t input_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned long dummy_page;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700315 struct vino_descriptor_table dummy_desc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Ralf Baechled203a7e2005-09-06 15:19:37 -0700318/* Module parameters */
319
320/*
Ladislav Michla637a112005-09-01 15:07:34 +0000321 * Using vino_pixel_conversion the ABGR32-format pixels supplied
Ralf Baechled203a7e2005-09-06 15:19:37 -0700322 * by the VINO chip can be converted to more common formats
323 * like RGBA32 (or probably RGB24 in the future). This way we
324 * can give out data that can be specified correctly with
325 * the V4L2-definitions.
326 *
327 * The pixel format is specified as RGBA32 when no conversion
328 * is used.
329 *
330 * Note that this only affects the 32-bit bit depth.
331 *
332 * Use non-zero value to enable conversion.
333 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300334static int vino_pixel_conversion;
Ladislav Michla637a112005-09-01 15:07:34 +0000335
Ralf Baechled203a7e2005-09-06 15:19:37 -0700336module_param_named(pixelconv, vino_pixel_conversion, int, 0);
Ladislav Michla637a112005-09-01 15:07:34 +0000337
Ralf Baechled203a7e2005-09-06 15:19:37 -0700338MODULE_PARM_DESC(pixelconv,
339 "enable pixel conversion (non-zero value enables)");
340
341/* Internal data structures */
342
343static struct sgi_vino *vino;
344
345static struct vino_settings *vino_drvdata;
346
347static const char *vino_driver_name = "vino";
348static const char *vino_driver_description = "SGI VINO";
349static const char *vino_bus_name = "GIO64 bus";
350static const char *vino_v4l_device_name_a = "SGI VINO Channel A";
351static const char *vino_v4l_device_name_b = "SGI VINO Channel B";
352
Ladislav Michla637a112005-09-01 15:07:34 +0000353static void vino_capture_tasklet(unsigned long channel);
354
355DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
356DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
357
Ralf Baechled203a7e2005-09-06 15:19:37 -0700358static const struct vino_input vino_inputs[] = {
359 {
360 .name = "Composite",
Ladislav Michla637a112005-09-01 15:07:34 +0000361 .std = V4L2_STD_NTSC | V4L2_STD_PAL
362 | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700363 },{
364 .name = "S-Video",
Ladislav Michla637a112005-09-01 15:07:34 +0000365 .std = V4L2_STD_NTSC | V4L2_STD_PAL
366 | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700367 },{
Ladislav Michla637a112005-09-01 15:07:34 +0000368 .name = "D1/IndyCam",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700369 .std = V4L2_STD_NTSC,
370 }
371};
372
373static const struct vino_data_format vino_data_formats[] = {
374 {
375 .description = "8-bit greyscale",
376 .bpp = 1,
377 .pixelformat = V4L2_PIX_FMT_GREY,
378 .colorspace = V4L2_COLORSPACE_SMPTE170M,
379 },{
380 .description = "8-bit dithered RGB 3-3-2",
381 .bpp = 1,
382 .pixelformat = V4L2_PIX_FMT_RGB332,
383 .colorspace = V4L2_COLORSPACE_SRGB,
384 },{
385 .description = "32-bit RGB",
386 .bpp = 4,
387 .pixelformat = V4L2_PIX_FMT_RGB32,
388 .colorspace = V4L2_COLORSPACE_SRGB,
389 },{
390 .description = "YUV 4:2:2",
Ladislav Michla637a112005-09-01 15:07:34 +0000391 .bpp = 2,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700392 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
393 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Ladislav Michla637a112005-09-01 15:07:34 +0000394 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700395};
396
397static const struct vino_data_norm vino_data_norms[] = {
398 {
399 .description = "NTSC",
400 .std = V4L2_STD_NTSC,
401 .fps_min = 6,
402 .fps_max = 30,
403 .framelines = 525,
404 .width = VINO_NTSC_WIDTH,
405 .height = VINO_NTSC_HEIGHT,
406 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000407 .top = VINO_CLIPPING_START_ODD_NTSC,
408 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700409 .bottom = VINO_CLIPPING_START_ODD_NTSC
410 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000411 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700412 },
413 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000414 .top = VINO_CLIPPING_START_EVEN_NTSC,
415 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700416 .bottom = VINO_CLIPPING_START_EVEN_NTSC
417 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000418 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700419 },
420 },{
421 .description = "PAL",
422 .std = V4L2_STD_PAL,
423 .fps_min = 5,
424 .fps_max = 25,
425 .framelines = 625,
426 .width = VINO_PAL_WIDTH,
427 .height = VINO_PAL_HEIGHT,
428 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000429 .top = VINO_CLIPPING_START_ODD_PAL,
430 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700431 .bottom = VINO_CLIPPING_START_ODD_PAL
432 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000433 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700434 },
435 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000436 .top = VINO_CLIPPING_START_EVEN_PAL,
437 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700438 .bottom = VINO_CLIPPING_START_EVEN_PAL
439 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000440 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700441 },
442 },{
443 .description = "SECAM",
444 .std = V4L2_STD_SECAM,
445 .fps_min = 5,
446 .fps_max = 25,
447 .framelines = 625,
448 .width = VINO_PAL_WIDTH,
449 .height = VINO_PAL_HEIGHT,
450 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000451 .top = VINO_CLIPPING_START_ODD_PAL,
452 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700453 .bottom = VINO_CLIPPING_START_ODD_PAL
454 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000455 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700456 },
457 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000458 .top = VINO_CLIPPING_START_EVEN_PAL,
459 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700460 .bottom = VINO_CLIPPING_START_EVEN_PAL
461 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000462 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700463 },
464 },{
Ladislav Michla637a112005-09-01 15:07:34 +0000465 .description = "NTSC/D1",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700466 .std = V4L2_STD_NTSC,
467 .fps_min = 6,
468 .fps_max = 30,
469 .framelines = 525,
470 .width = VINO_NTSC_WIDTH,
471 .height = VINO_NTSC_HEIGHT,
472 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000473 .top = VINO_CLIPPING_START_ODD_D1,
474 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700475 .bottom = VINO_CLIPPING_START_ODD_D1
476 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000477 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700478 },
479 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000480 .top = VINO_CLIPPING_START_EVEN_D1,
481 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700482 .bottom = VINO_CLIPPING_START_EVEN_D1
483 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000484 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700485 },
486 }
487};
488
489#define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
490
491struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
492 {
493 .id = V4L2_CID_AUTOGAIN,
494 .type = V4L2_CTRL_TYPE_BOOLEAN,
495 .name = "Automatic Gain Control",
496 .minimum = 0,
497 .maximum = 1,
498 .step = 1,
499 .default_value = INDYCAM_AGC_DEFAULT,
500 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000501 .reserved = { INDYCAM_CONTROL_AGC, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700502 },{
503 .id = V4L2_CID_AUTO_WHITE_BALANCE,
504 .type = V4L2_CTRL_TYPE_BOOLEAN,
505 .name = "Automatic White Balance",
506 .minimum = 0,
507 .maximum = 1,
508 .step = 1,
509 .default_value = INDYCAM_AWB_DEFAULT,
510 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000511 .reserved = { INDYCAM_CONTROL_AWB, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700512 },{
513 .id = V4L2_CID_GAIN,
514 .type = V4L2_CTRL_TYPE_INTEGER,
515 .name = "Gain",
516 .minimum = INDYCAM_GAIN_MIN,
517 .maximum = INDYCAM_GAIN_MAX,
518 .step = 1,
519 .default_value = INDYCAM_GAIN_DEFAULT,
520 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000521 .reserved = { INDYCAM_CONTROL_GAIN, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700522 },{
523 .id = V4L2_CID_PRIVATE_BASE,
524 .type = V4L2_CTRL_TYPE_INTEGER,
525 .name = "Red Saturation",
526 .minimum = INDYCAM_RED_SATURATION_MIN,
527 .maximum = INDYCAM_RED_SATURATION_MAX,
528 .step = 1,
529 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
530 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000531 .reserved = { INDYCAM_CONTROL_RED_SATURATION, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700532 },{
533 .id = V4L2_CID_PRIVATE_BASE + 1,
534 .type = V4L2_CTRL_TYPE_INTEGER,
535 .name = "Blue Saturation",
536 .minimum = INDYCAM_BLUE_SATURATION_MIN,
537 .maximum = INDYCAM_BLUE_SATURATION_MAX,
538 .step = 1,
539 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
540 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000541 .reserved = { INDYCAM_CONTROL_BLUE_SATURATION, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700542 },{
543 .id = V4L2_CID_RED_BALANCE,
544 .type = V4L2_CTRL_TYPE_INTEGER,
545 .name = "Red Balance",
546 .minimum = INDYCAM_RED_BALANCE_MIN,
547 .maximum = INDYCAM_RED_BALANCE_MAX,
548 .step = 1,
549 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
550 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000551 .reserved = { INDYCAM_CONTROL_RED_BALANCE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700552 },{
553 .id = V4L2_CID_BLUE_BALANCE,
554 .type = V4L2_CTRL_TYPE_INTEGER,
555 .name = "Blue Balance",
556 .minimum = INDYCAM_BLUE_BALANCE_MIN,
557 .maximum = INDYCAM_BLUE_BALANCE_MAX,
558 .step = 1,
559 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
560 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000561 .reserved = { INDYCAM_CONTROL_BLUE_BALANCE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700562 },{
563 .id = V4L2_CID_EXPOSURE,
564 .type = V4L2_CTRL_TYPE_INTEGER,
565 .name = "Shutter Control",
566 .minimum = INDYCAM_SHUTTER_MIN,
567 .maximum = INDYCAM_SHUTTER_MAX,
568 .step = 1,
569 .default_value = INDYCAM_SHUTTER_DEFAULT,
570 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000571 .reserved = { INDYCAM_CONTROL_SHUTTER, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700572 },{
573 .id = V4L2_CID_GAMMA,
574 .type = V4L2_CTRL_TYPE_INTEGER,
575 .name = "Gamma",
576 .minimum = INDYCAM_GAMMA_MIN,
577 .maximum = INDYCAM_GAMMA_MAX,
578 .step = 1,
579 .default_value = INDYCAM_GAMMA_DEFAULT,
580 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000581 .reserved = { INDYCAM_CONTROL_GAMMA, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700582 }
583};
584
Ladislav Michla637a112005-09-01 15:07:34 +0000585#define VINO_SAA7191_V4L2_CONTROL_COUNT 9
Ralf Baechled203a7e2005-09-06 15:19:37 -0700586
587struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
588 {
589 .id = V4L2_CID_HUE,
590 .type = V4L2_CTRL_TYPE_INTEGER,
591 .name = "Hue",
592 .minimum = SAA7191_HUE_MIN,
593 .maximum = SAA7191_HUE_MAX,
594 .step = 1,
595 .default_value = SAA7191_HUE_DEFAULT,
596 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000597 .reserved = { SAA7191_CONTROL_HUE, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700598 },{
599 .id = V4L2_CID_PRIVATE_BASE,
Ladislav Michla637a112005-09-01 15:07:34 +0000600 .type = V4L2_CTRL_TYPE_INTEGER,
601 .name = "Luminance Bandpass",
602 .minimum = SAA7191_BANDPASS_MIN,
603 .maximum = SAA7191_BANDPASS_MAX,
604 .step = 1,
605 .default_value = SAA7191_BANDPASS_DEFAULT,
606 .flags = 0,
607 .reserved = { SAA7191_CONTROL_BANDPASS, 0 },
608 },{
609 .id = V4L2_CID_PRIVATE_BASE + 1,
610 .type = V4L2_CTRL_TYPE_INTEGER,
611 .name = "Luminance Bandpass Weight",
612 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
613 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
614 .step = 1,
615 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
616 .flags = 0,
617 .reserved = { SAA7191_CONTROL_BANDPASS_WEIGHT, 0 },
618 },{
619 .id = V4L2_CID_PRIVATE_BASE + 2,
620 .type = V4L2_CTRL_TYPE_INTEGER,
621 .name = "HF Luminance Coring",
622 .minimum = SAA7191_CORING_MIN,
623 .maximum = SAA7191_CORING_MAX,
624 .step = 1,
625 .default_value = SAA7191_CORING_DEFAULT,
626 .flags = 0,
627 .reserved = { SAA7191_CONTROL_CORING, 0 },
628 },{
629 .id = V4L2_CID_PRIVATE_BASE + 3,
630 .type = V4L2_CTRL_TYPE_BOOLEAN,
631 .name = "Force Colour",
632 .minimum = SAA7191_FORCE_COLOUR_MIN,
633 .maximum = SAA7191_FORCE_COLOUR_MAX,
634 .step = 1,
635 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
636 .flags = 0,
637 .reserved = { SAA7191_CONTROL_FORCE_COLOUR, 0 },
638 },{
639 .id = V4L2_CID_PRIVATE_BASE + 4,
640 .type = V4L2_CTRL_TYPE_INTEGER,
641 .name = "Chrominance Gain Control",
642 .minimum = SAA7191_CHROMA_GAIN_MIN,
643 .maximum = SAA7191_CHROMA_GAIN_MAX,
644 .step = 1,
645 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
646 .flags = 0,
647 .reserved = { SAA7191_CONTROL_CHROMA_GAIN, 0 },
648 },{
649 .id = V4L2_CID_PRIVATE_BASE + 5,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700650 .type = V4L2_CTRL_TYPE_BOOLEAN,
651 .name = "VTR Time Constant",
652 .minimum = SAA7191_VTRC_MIN,
653 .maximum = SAA7191_VTRC_MAX,
654 .step = 1,
655 .default_value = SAA7191_VTRC_DEFAULT,
656 .flags = 0,
Ladislav Michla637a112005-09-01 15:07:34 +0000657 .reserved = { SAA7191_CONTROL_VTRC, 0 },
658 },{
659 .id = V4L2_CID_PRIVATE_BASE + 6,
660 .type = V4L2_CTRL_TYPE_INTEGER,
661 .name = "Luminance Delay Compensation",
662 .minimum = SAA7191_LUMA_DELAY_MIN,
663 .maximum = SAA7191_LUMA_DELAY_MAX,
664 .step = 1,
665 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
666 .flags = 0,
667 .reserved = { SAA7191_CONTROL_LUMA_DELAY, 0 },
668 },{
669 .id = V4L2_CID_PRIVATE_BASE + 7,
670 .type = V4L2_CTRL_TYPE_INTEGER,
671 .name = "Vertical Noise Reduction",
672 .minimum = SAA7191_VNR_MIN,
673 .maximum = SAA7191_VNR_MAX,
674 .step = 1,
675 .default_value = SAA7191_VNR_DEFAULT,
676 .flags = 0,
677 .reserved = { SAA7191_CONTROL_VNR, 0 },
Ralf Baechled203a7e2005-09-06 15:19:37 -0700678 }
679};
680
681/* VINO I2C bus functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683unsigned i2c_vino_getctrl(void *data)
684{
685 return vino->i2c_control;
686}
687
688void i2c_vino_setctrl(void *data, unsigned val)
689{
690 vino->i2c_control = val;
691}
692
693unsigned i2c_vino_rdata(void *data)
694{
695 return vino->i2c_data;
696}
697
698void i2c_vino_wdata(void *data, unsigned val)
699{
700 vino->i2c_data = val;
701}
702
703static struct i2c_algo_sgi_data i2c_sgi_vino_data =
704{
705 .getctrl = &i2c_vino_getctrl,
706 .setctrl = &i2c_vino_setctrl,
707 .rdata = &i2c_vino_rdata,
708 .wdata = &i2c_vino_wdata,
709 .xfer_timeout = 200,
710 .ack_timeout = 1000,
711};
712
713/*
714 * There are two possible clients on VINO I2C bus, so we limit usage only
715 * to them.
716 */
717static int i2c_vino_client_reg(struct i2c_client *client)
718{
Ladislav Michla637a112005-09-01 15:07:34 +0000719 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700720 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Ladislav Michla637a112005-09-01 15:07:34 +0000722 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 switch (client->driver->id) {
724 case I2C_DRIVERID_SAA7191:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700725 if (vino_drvdata->decoder.driver)
726 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700728 vino_drvdata->decoder.driver = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
730 case I2C_DRIVERID_INDYCAM:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700731 if (vino_drvdata->camera.driver)
732 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700734 vino_drvdata->camera.driver = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736 default:
Ralf Baechled203a7e2005-09-06 15:19:37 -0700737 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Ladislav Michla637a112005-09-01 15:07:34 +0000739 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Ralf Baechled203a7e2005-09-06 15:19:37 -0700741 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744static int i2c_vino_client_unreg(struct i2c_client *client)
745{
Ladislav Michla637a112005-09-01 15:07:34 +0000746 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700747 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Ladislav Michla637a112005-09-01 15:07:34 +0000749 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700750 if (client == vino_drvdata->decoder.driver) {
751 if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
752 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700754 vino_drvdata->decoder.driver = NULL;
755 } else if (client == vino_drvdata->camera.driver) {
756 if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
757 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 else
Ralf Baechled203a7e2005-09-06 15:19:37 -0700759 vino_drvdata->camera.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Ladislav Michla637a112005-09-01 15:07:34 +0000761 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Ralf Baechled203a7e2005-09-06 15:19:37 -0700763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static struct i2c_adapter vino_i2c_adapter =
767{
768 .name = "VINO I2C bus",
769 .id = I2C_HW_SGI_VINO,
770 .algo_data = &i2c_sgi_vino_data,
771 .client_register = &i2c_vino_client_reg,
772 .client_unregister = &i2c_vino_client_unreg,
773};
774
775static int vino_i2c_add_bus(void)
776{
777 return i2c_sgi_add_bus(&vino_i2c_adapter);
778}
779
780static int vino_i2c_del_bus(void)
781{
Jean Delvare32697112006-12-10 21:21:33 +0100782 return i2c_del_adapter(&vino_i2c_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Ralf Baechled203a7e2005-09-06 15:19:37 -0700785static int i2c_camera_command(unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700787 return vino_drvdata->camera.driver->
788 driver->command(vino_drvdata->camera.driver,
789 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Ralf Baechled203a7e2005-09-06 15:19:37 -0700792static int i2c_decoder_command(unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700794 return vino_drvdata->decoder.driver->
795 driver->command(vino_drvdata->decoder.driver,
796 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Ralf Baechled203a7e2005-09-06 15:19:37 -0700799/* VINO framebuffer/DMA descriptor management */
800
801static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
802 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700804 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Ralf Baechled203a7e2005-09-06 15:19:37 -0700806 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Ralf Baechled203a7e2005-09-06 15:19:37 -0700808 for (i = 0; i < count; i++) {
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300809 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700810 dma_unmap_single(NULL,
811 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
812 PAGE_SIZE, DMA_FROM_DEVICE);
813 free_page(fb->desc_table.virtual[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Ralf Baechled203a7e2005-09-06 15:19:37 -0700816 dma_free_coherent(NULL,
817 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
818 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
819 fb->desc_table.dma);
820 kfree(fb->desc_table.virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Ralf Baechled203a7e2005-09-06 15:19:37 -0700822 memset(fb, 0, sizeof(struct vino_framebuffer));
823}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Ralf Baechled203a7e2005-09-06 15:19:37 -0700825static void vino_free_buffer(struct vino_framebuffer *fb)
826{
827 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
828}
829
830static int vino_allocate_buffer(struct vino_framebuffer *fb,
831 unsigned int size)
832{
833 unsigned int count, i, j;
834 int ret = 0;
835
836 dprintk("vino_allocate_buffer():\n");
837
838 if (size < 1)
839 return -EINVAL;
840
841 memset(fb, 0, sizeof(struct vino_framebuffer));
842
843 count = ((size / PAGE_SIZE) + 4) & ~3;
844
845 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
846 size, count);
847
848 /* allocate memory for table with virtual (page) addresses */
849 fb->desc_table.virtual = (unsigned long *)
850 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
851 if (!fb->desc_table.virtual)
852 return -ENOMEM;
853
854 /* allocate memory for table with dma addresses
855 * (has space for four extra descriptors) */
856 fb->desc_table.dma_cpu =
857 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
858 sizeof(dma_addr_t), &fb->desc_table.dma,
859 GFP_KERNEL | GFP_DMA);
860 if (!fb->desc_table.dma_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ret = -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700862 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Ralf Baechled203a7e2005-09-06 15:19:37 -0700865 /* allocate pages for the buffer and acquire the according
866 * dma addresses */
867 for (i = 0; i < count; i++) {
868 dma_addr_t dma_data_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Ralf Baechled203a7e2005-09-06 15:19:37 -0700870 fb->desc_table.virtual[i] =
871 get_zeroed_page(GFP_KERNEL | GFP_DMA);
872 if (!fb->desc_table.virtual[i]) {
873 ret = -ENOBUFS;
874 break;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Ralf Baechled203a7e2005-09-06 15:19:37 -0700877 dma_data_addr =
878 dma_map_single(NULL,
879 (void *)fb->desc_table.virtual[i],
880 PAGE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Ralf Baechled203a7e2005-09-06 15:19:37 -0700882 for (j = 0; j < VINO_PAGE_RATIO; j++) {
883 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
884 dma_data_addr + VINO_PAGE_SIZE * j;
885 }
886
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300887 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Ralf Baechled203a7e2005-09-06 15:19:37 -0700890 /* page_count needs to be set anyway, because the descriptor table has
891 * been allocated according to this number */
892 fb->desc_table.page_count = count;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (ret) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700895 /* the descriptor with index i doesn't contain
896 * a valid address yet */
897 vino_free_buffer_with_count(fb, i);
898 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
Ralf Baechled203a7e2005-09-06 15:19:37 -0700901 //fb->size = size;
902 fb->size = count * PAGE_SIZE;
903 fb->data_format = VINO_DATA_FMT_NONE;
904
905 /* set the dma stop-bit for the last (count+1)th descriptor */
906 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
907 return 0;
908
909 out_free_virtual:
910 kfree(fb->desc_table.virtual);
911 return ret;
912}
913
914#if 0
915/* user buffers not fully implemented yet */
916static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
917 void *user,
918 unsigned int size)
919{
920 unsigned int count, i, j;
921 int ret = 0;
922
923 dprintk("vino_prepare_user_buffer():\n");
924
925 if (size < 1)
926 return -EINVAL;
927
928 memset(fb, 0, sizeof(struct vino_framebuffer));
929
930 count = ((size / PAGE_SIZE)) & ~3;
931
932 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
933 size, count);
934
935 /* allocate memory for table with virtual (page) addresses */
936 fb->desc_table.virtual = (unsigned long *)
937 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
938 if (!fb->desc_table.virtual)
939 return -ENOMEM;
940
941 /* allocate memory for table with dma addresses
942 * (has space for four extra descriptors) */
943 fb->desc_table.dma_cpu =
944 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
945 sizeof(dma_addr_t), &fb->desc_table.dma,
946 GFP_KERNEL | GFP_DMA);
947 if (!fb->desc_table.dma_cpu) {
948 ret = -ENOMEM;
949 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700951
952 /* allocate pages for the buffer and acquire the according
953 * dma addresses */
954 for (i = 0; i < count; i++) {
955 dma_addr_t dma_data_addr;
956
957 fb->desc_table.virtual[i] =
958 get_zeroed_page(GFP_KERNEL | GFP_DMA);
959 if (!fb->desc_table.virtual[i]) {
960 ret = -ENOBUFS;
961 break;
962 }
963
964 dma_data_addr =
965 dma_map_single(NULL,
966 (void *)fb->desc_table.virtual[i],
967 PAGE_SIZE, DMA_FROM_DEVICE);
968
969 for (j = 0; j < VINO_PAGE_RATIO; j++) {
970 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
971 dma_data_addr + VINO_PAGE_SIZE * j;
972 }
973
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300974 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700975 }
976
977 /* page_count needs to be set anyway, because the descriptor table has
978 * been allocated according to this number */
979 fb->desc_table.page_count = count;
980
981 if (ret) {
982 /* the descriptor with index i doesn't contain
983 * a valid address yet */
984 vino_free_buffer_with_count(fb, i);
985 return ret;
986 }
987
988 //fb->size = size;
989 fb->size = count * PAGE_SIZE;
990
991 /* set the dma stop-bit for the last (count+1)th descriptor */
992 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
993 return 0;
994
995 out_free_virtual:
996 kfree(fb->desc_table.virtual);
997 return ret;
998}
999#endif
1000
1001static void vino_sync_buffer(struct vino_framebuffer *fb)
1002{
1003 int i;
1004
1005 dprintk("vino_sync_buffer():\n");
1006
1007 for (i = 0; i < fb->desc_table.page_count; i++)
1008 dma_sync_single(NULL,
1009 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
1010 PAGE_SIZE, DMA_FROM_DEVICE);
1011}
1012
1013/* Framebuffer fifo functions (need to be locked externally) */
1014
Ladislav Michla637a112005-09-01 15:07:34 +00001015static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
Ralf Baechled203a7e2005-09-06 15:19:37 -07001016 unsigned int length)
1017{
1018 f->length = 0;
1019 f->used = 0;
1020 f->head = 0;
1021 f->tail = 0;
1022
Ladislav Michla637a112005-09-01 15:07:34 +00001023 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
1024 length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001025
1026 f->length = length;
1027}
1028
1029/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +00001030static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
1031 unsigned int id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001032{
1033 unsigned int i;
Ladislav Michla637a112005-09-01 15:07:34 +00001034
Ralf Baechled203a7e2005-09-06 15:19:37 -07001035 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
1036 if (f->data[i] == id)
1037 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Ladislav Michla637a112005-09-01 15:07:34 +00001043#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001044/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +00001045static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001046{
1047 return (f->used == f->length);
1048}
Ladislav Michla637a112005-09-01 15:07:34 +00001049#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001050
Ladislav Michla637a112005-09-01 15:07:34 +00001051static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001052{
1053 return f->used;
1054}
1055
1056static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
1057{
1058 if (id >= f->length) {
1059 return VINO_QUEUE_ERROR;
1060 }
1061
1062 if (vino_fifo_has_id(f, id)) {
1063 return VINO_QUEUE_ERROR;
1064 }
1065
1066 if (f->used < f->length) {
1067 f->data[f->tail] = id;
1068 f->tail = (f->tail + 1) % f->length;
1069 f->used++;
1070 } else {
1071 return VINO_QUEUE_ERROR;
1072 }
1073
1074 return 0;
1075}
1076
1077static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
1078{
1079 if (f->used > 0) {
1080 *id = f->data[f->head];
1081 } else {
1082 return VINO_QUEUE_ERROR;
1083 }
1084
1085 return 0;
1086}
1087
1088static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
1089{
1090 if (f->used > 0) {
1091 *id = f->data[f->head];
1092 f->head = (f->head + 1) % f->length;
1093 f->used--;
1094 } else {
1095 return VINO_QUEUE_ERROR;
1096 }
1097
1098 return 0;
1099}
1100
1101/* Framebuffer queue functions */
1102
1103/* execute with queue_lock locked */
1104static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
1105 unsigned int length)
1106{
1107 unsigned int i;
1108
1109 q->length = 0;
1110 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
1111 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
1112 for (i = 0; i < length; i++) {
1113 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
1114 i);
1115 vino_free_buffer(q->buffer[i]);
1116 kfree(q->buffer[i]);
1117 }
1118
1119 q->type = VINO_MEMORY_NONE;
1120 q->magic = 0;
1121}
1122
1123static void vino_queue_free(struct vino_framebuffer_queue *q)
1124{
1125 dprintk("vino_queue_free():\n");
1126
1127 if (q->magic != VINO_QUEUE_MAGIC)
1128 return;
1129 if (q->type != VINO_MEMORY_MMAP)
1130 return;
1131
Ingo Molnar3593cab2006-02-07 06:49:14 -02001132 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001133
1134 vino_queue_free_with_count(q, q->length);
1135
Ingo Molnar3593cab2006-02-07 06:49:14 -02001136 mutex_unlock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001137}
1138
1139static int vino_queue_init(struct vino_framebuffer_queue *q,
1140 unsigned int *length)
1141{
1142 unsigned int i;
1143 int ret = 0;
1144
1145 dprintk("vino_queue_init(): length = %d\n", *length);
1146
1147 if (q->magic == VINO_QUEUE_MAGIC) {
1148 dprintk("vino_queue_init(): queue already initialized!\n");
1149 return -EINVAL;
1150 }
1151
1152 if (q->type != VINO_MEMORY_NONE) {
1153 dprintk("vino_queue_init(): queue already initialized!\n");
1154 return -EINVAL;
1155 }
1156
1157 if (*length < 1)
1158 return -EINVAL;
1159
Ingo Molnar3593cab2006-02-07 06:49:14 -02001160 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001161
Ladislav Michla637a112005-09-01 15:07:34 +00001162 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1163 *length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001164
1165 q->length = 0;
1166
1167 for (i = 0; i < *length; i++) {
1168 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1169 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1170 GFP_KERNEL);
1171 if (!q->buffer[i]) {
1172 dprintk("vino_queue_init(): kmalloc() failed\n");
1173 ret = -ENOMEM;
1174 break;
1175 }
1176
1177 ret = vino_allocate_buffer(q->buffer[i],
1178 VINO_FRAMEBUFFER_SIZE);
1179 if (ret) {
1180 kfree(q->buffer[i]);
1181 dprintk("vino_queue_init(): "
1182 "vino_allocate_buffer() failed\n");
1183 break;
1184 }
1185
1186 q->buffer[i]->id = i;
1187 if (i > 0) {
1188 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1189 q->buffer[i - 1]->size;
1190 } else {
1191 q->buffer[i]->offset = 0;
1192 }
1193
1194 spin_lock_init(&q->buffer[i]->state_lock);
1195
1196 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1197 "size = %d\n", i, q->buffer[i]->offset,
1198 q->buffer[i]->size);
1199 }
1200
1201 if (ret) {
1202 vino_queue_free_with_count(q, i);
1203 *length = 0;
1204 } else {
1205 q->length = *length;
1206 vino_fifo_init(&q->in, q->length);
1207 vino_fifo_init(&q->out, q->length);
1208 q->type = VINO_MEMORY_MMAP;
1209 q->magic = VINO_QUEUE_MAGIC;
1210 }
1211
Ingo Molnar3593cab2006-02-07 06:49:14 -02001212 mutex_unlock(&q->queue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 return ret;
1215}
1216
Ralf Baechled203a7e2005-09-06 15:19:37 -07001217static struct vino_framebuffer *vino_queue_add(struct
1218 vino_framebuffer_queue *q,
1219 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Ralf Baechled203a7e2005-09-06 15:19:37 -07001221 struct vino_framebuffer *ret = NULL;
1222 unsigned int total;
1223 unsigned long flags;
1224
1225 dprintk("vino_queue_add(): id = %d\n", id);
1226
1227 if (q->magic != VINO_QUEUE_MAGIC) {
1228 return ret;
1229 }
1230
1231 spin_lock_irqsave(&q->queue_lock, flags);
1232
1233 if (q->length == 0)
1234 goto out;
1235
1236 if (id >= q->length)
1237 goto out;
1238
1239 /* not needed?: if (vino_fifo_full(&q->out)) {
1240 goto out;
1241 }*/
1242 /* check that outgoing queue isn't already full
1243 * (or that it won't become full) */
1244 total = vino_fifo_get_used(&q->in) +
1245 vino_fifo_get_used(&q->out);
1246 if (total >= q->length)
1247 goto out;
1248
1249 if (vino_fifo_enqueue(&q->in, id))
1250 goto out;
1251
1252 ret = q->buffer[id];
1253
1254out:
1255 spin_unlock_irqrestore(&q->queue_lock, flags);
1256
1257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Ralf Baechled203a7e2005-09-06 15:19:37 -07001260static struct vino_framebuffer *vino_queue_transfer(struct
1261 vino_framebuffer_queue *q)
1262{
1263 struct vino_framebuffer *ret = NULL;
1264 struct vino_framebuffer *fb;
1265 int id;
1266 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Ralf Baechled203a7e2005-09-06 15:19:37 -07001268 dprintk("vino_queue_transfer():\n");
1269
1270 if (q->magic != VINO_QUEUE_MAGIC) {
1271 return ret;
1272 }
1273
1274 spin_lock_irqsave(&q->queue_lock, flags);
1275
1276 if (q->length == 0)
1277 goto out;
1278
1279 // now this actually removes an entry from the incoming queue
1280 if (vino_fifo_dequeue(&q->in, &id)) {
1281 goto out;
1282 }
1283
1284 dprintk("vino_queue_transfer(): id = %d\n", id);
1285 fb = q->buffer[id];
1286
1287 // we have already checked that the outgoing queue is not full, but...
1288 if (vino_fifo_enqueue(&q->out, id)) {
1289 printk(KERN_ERR "vino_queue_transfer(): "
1290 "outgoing queue is full, this shouldn't happen!\n");
1291 goto out;
1292 }
1293
1294 ret = fb;
1295out:
1296 spin_unlock_irqrestore(&q->queue_lock, flags);
1297
1298 return ret;
1299}
1300
1301/* returns true/false */
1302static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1303 unsigned int id)
1304{
1305 int ret = 0;
1306 unsigned long flags;
1307
1308 if (q->magic != VINO_QUEUE_MAGIC) {
1309 return ret;
1310 }
1311
1312 spin_lock_irqsave(&q->queue_lock, flags);
1313
1314 if (q->length == 0)
1315 goto out;
1316
1317 ret = vino_fifo_has_id(&q->in, id);
1318
1319out:
1320 spin_unlock_irqrestore(&q->queue_lock, flags);
1321
1322 return ret;
1323}
1324
1325/* returns true/false */
1326static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1327 unsigned int id)
1328{
1329 int ret = 0;
1330 unsigned long flags;
1331
1332 if (q->magic != VINO_QUEUE_MAGIC) {
1333 return ret;
1334 }
1335
1336 spin_lock_irqsave(&q->queue_lock, flags);
1337
1338 if (q->length == 0)
1339 goto out;
1340
1341 ret = vino_fifo_has_id(&q->out, id);
1342
1343out:
1344 spin_unlock_irqrestore(&q->queue_lock, flags);
1345
1346 return ret;
1347}
1348
1349static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1350 unsigned int *used)
1351{
1352 int ret = 0;
1353 unsigned long flags;
1354
1355 if (q->magic != VINO_QUEUE_MAGIC) {
1356 return VINO_QUEUE_ERROR;
1357 }
1358
1359 spin_lock_irqsave(&q->queue_lock, flags);
1360
1361 if (q->length == 0) {
1362 ret = VINO_QUEUE_ERROR;
1363 goto out;
1364 }
1365
1366 *used = vino_fifo_get_used(&q->in);
1367
1368out:
1369 spin_unlock_irqrestore(&q->queue_lock, flags);
1370
1371 return ret;
1372}
1373
1374static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1375 unsigned int *used)
1376{
1377 int ret = 0;
1378 unsigned long flags;
1379
1380 if (q->magic != VINO_QUEUE_MAGIC) {
1381 return VINO_QUEUE_ERROR;
1382 }
1383
1384 spin_lock_irqsave(&q->queue_lock, flags);
1385
1386 if (q->length == 0) {
1387 ret = VINO_QUEUE_ERROR;
1388 goto out;
1389 }
1390
1391 *used = vino_fifo_get_used(&q->out);
1392
1393out:
1394 spin_unlock_irqrestore(&q->queue_lock, flags);
1395
1396 return ret;
1397}
1398
Ladislav Michla637a112005-09-01 15:07:34 +00001399#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001400static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1401 unsigned int *total)
1402{
1403 int ret = 0;
1404 unsigned long flags;
1405
1406 if (q->magic != VINO_QUEUE_MAGIC) {
1407 return VINO_QUEUE_ERROR;
1408 }
1409
1410 spin_lock_irqsave(&q->queue_lock, flags);
1411
1412 if (q->length == 0) {
1413 ret = VINO_QUEUE_ERROR;
1414 goto out;
1415 }
1416
1417 *total = vino_fifo_get_used(&q->in) +
1418 vino_fifo_get_used(&q->out);
1419
1420out:
1421 spin_unlock_irqrestore(&q->queue_lock, flags);
1422
1423 return ret;
1424}
Ladislav Michla637a112005-09-01 15:07:34 +00001425#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001426
1427static struct vino_framebuffer *vino_queue_peek(struct
1428 vino_framebuffer_queue *q,
1429 unsigned int *id)
1430{
1431 struct vino_framebuffer *ret = NULL;
1432 unsigned long flags;
1433
1434 if (q->magic != VINO_QUEUE_MAGIC) {
1435 return ret;
1436 }
1437
1438 spin_lock_irqsave(&q->queue_lock, flags);
1439
1440 if (q->length == 0)
1441 goto out;
1442
1443 if (vino_fifo_peek(&q->in, id)) {
1444 goto out;
1445 }
1446
1447 ret = q->buffer[*id];
1448out:
1449 spin_unlock_irqrestore(&q->queue_lock, flags);
1450
1451 return ret;
1452}
1453
1454static struct vino_framebuffer *vino_queue_remove(struct
1455 vino_framebuffer_queue *q,
1456 unsigned int *id)
1457{
1458 struct vino_framebuffer *ret = NULL;
1459 unsigned long flags;
1460 dprintk("vino_queue_remove():\n");
1461
1462 if (q->magic != VINO_QUEUE_MAGIC) {
1463 return ret;
1464 }
1465
1466 spin_lock_irqsave(&q->queue_lock, flags);
1467
1468 if (q->length == 0)
1469 goto out;
1470
1471 if (vino_fifo_dequeue(&q->out, id)) {
1472 goto out;
1473 }
1474
1475 dprintk("vino_queue_remove(): id = %d\n", *id);
1476 ret = q->buffer[*id];
1477out:
1478 spin_unlock_irqrestore(&q->queue_lock, flags);
1479
1480 return ret;
1481}
1482
1483static struct
1484vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1485 unsigned int id)
1486{
1487 struct vino_framebuffer *ret = NULL;
1488 unsigned long flags;
1489
1490 if (q->magic != VINO_QUEUE_MAGIC) {
1491 return ret;
1492 }
1493
1494 spin_lock_irqsave(&q->queue_lock, flags);
1495
1496 if (q->length == 0)
1497 goto out;
1498
1499 if (id >= q->length)
1500 goto out;
1501
1502 ret = q->buffer[id];
1503 out:
1504 spin_unlock_irqrestore(&q->queue_lock, flags);
1505
1506 return ret;
1507}
1508
1509static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1510{
1511 unsigned int length = 0;
1512 unsigned long flags;
1513
1514 if (q->magic != VINO_QUEUE_MAGIC) {
1515 return length;
1516 }
1517
1518 spin_lock_irqsave(&q->queue_lock, flags);
1519 length = q->length;
1520 spin_unlock_irqrestore(&q->queue_lock, flags);
1521
1522 return length;
1523}
1524
1525static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1526{
1527 unsigned int i;
1528 int ret = 0;
1529 unsigned long flags;
1530
1531 if (q->magic != VINO_QUEUE_MAGIC) {
1532 return ret;
1533 }
1534
1535 spin_lock_irqsave(&q->queue_lock, flags);
1536 for (i = 0; i < q->length; i++) {
1537 if (q->buffer[i]->map_count > 0) {
1538 ret = 1;
1539 break;
1540 }
1541 }
1542 spin_unlock_irqrestore(&q->queue_lock, flags);
1543
1544 return ret;
1545}
1546
1547/* VINO functions */
1548
1549/* execute with input_lock locked */
1550static void vino_update_line_size(struct vino_channel_settings *vcs)
1551{
1552 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1553 unsigned int d = vcs->decimation;
1554 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001555 unsigned int lsize;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001556
1557 dprintk("update_line_size(): before: w = %d, d = %d, "
1558 "line_size = %d\n", w, d, vcs->line_size);
Ladislav Michla637a112005-09-01 15:07:34 +00001559
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001560 /* line size must be multiple of 8 bytes */
Ralf Baechled203a7e2005-09-06 15:19:37 -07001561 lsize = (bpp * (w / d)) & ~7;
1562 w = (lsize / bpp) * d;
1563
1564 vcs->clipping.right = vcs->clipping.left + w;
1565 vcs->line_size = lsize;
Ladislav Michla637a112005-09-01 15:07:34 +00001566
Ralf Baechled203a7e2005-09-06 15:19:37 -07001567 dprintk("update_line_size(): after: w = %d, d = %d, "
1568 "line_size = %d\n", w, d, vcs->line_size);
1569}
1570
1571/* execute with input_lock locked */
1572static void vino_set_clipping(struct vino_channel_settings *vcs,
1573 unsigned int x, unsigned int y,
1574 unsigned int w, unsigned int h)
1575{
1576 unsigned int maxwidth, maxheight;
1577 unsigned int d;
1578
1579 maxwidth = vino_data_norms[vcs->data_norm].width;
1580 maxheight = vino_data_norms[vcs->data_norm].height;
1581 d = vcs->decimation;
1582
1583 y &= ~1; /* odd/even fields */
1584
1585 if (x > maxwidth) {
1586 x = 0;
1587 }
1588 if (y > maxheight) {
1589 y = 0;
1590 }
1591
1592 if (((w / d) < VINO_MIN_WIDTH)
1593 || ((h / d) < VINO_MIN_HEIGHT)) {
1594 w = VINO_MIN_WIDTH * d;
1595 h = VINO_MIN_HEIGHT * d;
1596 }
1597
1598 if ((x + w) > maxwidth) {
1599 w = maxwidth - x;
1600 if ((w / d) < VINO_MIN_WIDTH)
1601 x = maxwidth - VINO_MIN_WIDTH * d;
1602 }
1603 if ((y + h) > maxheight) {
1604 h = maxheight - y;
1605 if ((h / d) < VINO_MIN_HEIGHT)
1606 y = maxheight - VINO_MIN_HEIGHT * d;
1607 }
1608
1609 vcs->clipping.left = x;
1610 vcs->clipping.top = y;
1611 vcs->clipping.right = x + w;
1612 vcs->clipping.bottom = y + h;
1613
1614 vino_update_line_size(vcs);
1615
1616 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1617 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1618 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1619}
1620
1621/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001622static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001623{
1624 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1625 vino_data_norms[vcs->data_norm].height);
1626}
1627
1628/* execute with input_lock locked */
1629static void vino_set_scaling(struct vino_channel_settings *vcs,
1630 unsigned int w, unsigned int h)
1631{
1632 unsigned int x, y, curw, curh, d;
1633
1634 x = vcs->clipping.left;
1635 y = vcs->clipping.top;
1636 curw = vcs->clipping.right - vcs->clipping.left;
1637 curh = vcs->clipping.bottom - vcs->clipping.top;
1638
1639 d = max(curw / w, curh / h);
1640
1641 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1642 w, h, curw, curh, d);
1643
1644 if (d < 1) {
1645 d = 1;
Ladislav Michla637a112005-09-01 15:07:34 +00001646 } else if (d > 8) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07001647 d = 8;
1648 }
1649
1650 vcs->decimation = d;
1651 vino_set_clipping(vcs, x, y, w * d, h * d);
1652
1653 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1654 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1655 vcs->decimation, vcs->line_size);
1656}
1657
1658/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001659static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001660{
1661 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1662 vcs->clipping.bottom - vcs->clipping.top);
1663}
1664
1665/* execute with input_lock locked */
1666static void vino_set_framerate(struct vino_channel_settings *vcs,
1667 unsigned int fps)
1668{
1669 unsigned int mask;
1670
1671 switch (vcs->data_norm) {
1672 case VINO_DATA_NORM_NTSC:
1673 case VINO_DATA_NORM_D1:
1674 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1675
1676 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1677 fps = vino_data_norms[vcs->data_norm].fps_min;
1678 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1679 fps = vino_data_norms[vcs->data_norm].fps_max;
1680
1681 switch (fps) {
1682 case 6:
1683 mask = 0x003;
1684 break;
1685 case 12:
1686 mask = 0x0c3;
1687 break;
1688 case 18:
1689 mask = 0x333;
1690 break;
1691 case 24:
1692 mask = 0x3ff;
1693 break;
1694 case 30:
1695 mask = 0xfff;
1696 break;
1697 default:
1698 mask = VINO_FRAMERT_FULL;
1699 }
1700 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1701 break;
1702 case VINO_DATA_NORM_PAL:
1703 case VINO_DATA_NORM_SECAM:
1704 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1705
1706 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1707 fps = vino_data_norms[vcs->data_norm].fps_min;
1708 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1709 fps = vino_data_norms[vcs->data_norm].fps_max;
1710
1711 switch (fps) {
1712 case 5:
1713 mask = 0x003;
1714 break;
1715 case 10:
1716 mask = 0x0c3;
1717 break;
1718 case 15:
1719 mask = 0x333;
1720 break;
1721 case 20:
1722 mask = 0x0ff;
1723 break;
1724 case 25:
1725 mask = 0x3ff;
1726 break;
1727 default:
1728 mask = VINO_FRAMERT_FULL;
1729 }
1730 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1731 break;
1732 }
1733
1734 vcs->fps = fps;
1735}
1736
1737/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001738static inline void vino_set_default_framerate(struct
1739 vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001740{
1741 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1742}
1743
1744/*
1745 * Prepare VINO for DMA transfer...
1746 * (execute only with vino_lock and input_lock locked)
1747 */
1748static int vino_dma_setup(struct vino_channel_settings *vcs,
1749 struct vino_framebuffer *fb)
1750{
1751 u32 ctrl, intr;
1752 struct sgi_vino_channel *ch;
1753 const struct vino_data_norm *norm;
1754
1755 dprintk("vino_dma_setup():\n");
1756
1757 vcs->field = 0;
1758 fb->frame_counter = 0;
1759
1760 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1761 norm = &vino_data_norms[vcs->data_norm];
1762
1763 ch->page_index = 0;
1764 ch->line_count = 0;
1765
1766 /* VINO line size register is set 8 bytes less than actual */
1767 ch->line_size = vcs->line_size - 8;
1768
1769 /* let VINO know where to transfer data */
1770 ch->start_desc_tbl = fb->desc_table.dma;
1771 ch->next_4_desc = fb->desc_table.dma;
1772
1773 /* give vino time to fetch the first four descriptors, 5 usec
1774 * should be more than enough time */
1775 udelay(VINO_DESC_FETCH_DELAY);
1776
Ladislav Michla637a112005-09-01 15:07:34 +00001777 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1778 ch->start_desc_tbl, ch->next_4_desc);
1779
Ralf Baechled203a7e2005-09-06 15:19:37 -07001780 /* set the alpha register */
1781 ch->alpha = vcs->alpha;
1782
1783 /* set clipping registers */
1784 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1785 VINO_CLIP_EVEN(norm->even.top +
1786 vcs->clipping.top / 2) |
1787 VINO_CLIP_X(vcs->clipping.left);
1788 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1789 vcs->clipping.bottom / 2 - 1) |
1790 VINO_CLIP_EVEN(norm->even.top +
1791 vcs->clipping.bottom / 2 - 1) |
1792 VINO_CLIP_X(vcs->clipping.right);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001793
1794 /* set the size of actual content in the buffer (DECIMATION !) */
1795 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1796 vcs->decimation) *
1797 ((vcs->clipping.bottom - vcs->clipping.top) /
1798 vcs->decimation) *
1799 vino_data_formats[vcs->data_format].bpp;
1800
1801 ch->frame_rate = vcs->framert_reg;
1802
1803 ctrl = vino->control;
1804 intr = vino->intr_status;
1805
1806 if (vcs->channel == VINO_CHANNEL_A) {
1807 /* All interrupt conditions for this channel was cleared
1808 * so clear the interrupt status register and enable
1809 * interrupts */
1810 intr &= ~VINO_INTSTAT_A;
1811 ctrl |= VINO_CTRL_A_INT;
1812
1813 /* enable synchronization */
1814 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1815
1816 /* enable frame assembly */
1817 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1818
1819 /* set decimation used */
1820 if (vcs->decimation < 2)
1821 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1822 else {
1823 ctrl |= VINO_CTRL_A_DEC_ENBL;
1824 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1825 ctrl |= (vcs->decimation - 1) <<
1826 VINO_CTRL_A_DEC_SCALE_SHIFT;
1827 }
1828
1829 /* select input interface */
1830 if (vcs->input == VINO_INPUT_D1)
1831 ctrl |= VINO_CTRL_A_SELECT;
1832 else
1833 ctrl &= ~VINO_CTRL_A_SELECT;
1834
1835 /* palette */
1836 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1837 VINO_CTRL_A_DITHER);
1838 } else {
1839 intr &= ~VINO_INTSTAT_B;
1840 ctrl |= VINO_CTRL_B_INT;
1841
1842 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1843 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1844
1845 if (vcs->decimation < 2)
1846 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1847 else {
1848 ctrl |= VINO_CTRL_B_DEC_ENBL;
1849 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1850 ctrl |= (vcs->decimation - 1) <<
1851 VINO_CTRL_B_DEC_SCALE_SHIFT;
1852
1853 }
1854 if (vcs->input == VINO_INPUT_D1)
1855 ctrl |= VINO_CTRL_B_SELECT;
1856 else
1857 ctrl &= ~VINO_CTRL_B_SELECT;
1858
1859 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1860 VINO_CTRL_B_DITHER);
1861 }
1862
1863 /* set palette */
1864 fb->data_format = vcs->data_format;
1865
1866 switch (vcs->data_format) {
1867 case VINO_DATA_FMT_GREY:
1868 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1869 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1870 break;
1871 case VINO_DATA_FMT_RGB32:
1872 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1873 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1874 break;
1875 case VINO_DATA_FMT_YUV:
1876 /* nothing needs to be done */
1877 break;
1878 case VINO_DATA_FMT_RGB332:
1879 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1880 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1881 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1882 break;
1883 }
1884
1885 vino->intr_status = intr;
1886 vino->control = ctrl;
1887
1888 return 0;
1889}
1890
1891/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001892static inline void vino_dma_start(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001893{
1894 u32 ctrl = vino->control;
1895
1896 dprintk("vino_dma_start():\n");
1897 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1898 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1899 vino->control = ctrl;
1900}
1901
1902/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001903static inline void vino_dma_stop(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001904{
1905 u32 ctrl = vino->control;
1906
1907 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1908 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
Ladislav Michla637a112005-09-01 15:07:34 +00001909 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1910 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001911 vino->control = ctrl;
1912 dprintk("vino_dma_stop():\n");
1913}
1914
1915/*
1916 * Load dummy page to descriptor registers. This prevents generating of
1917 * spurious interrupts. (execute only with vino_lock locked)
1918 */
1919static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1920{
1921 struct sgi_vino_channel *ch;
1922
1923 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1924
1925 ch->page_index = 0;
1926 ch->line_count = 0;
1927
1928 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1929 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1930
1931 udelay(VINO_DESC_FETCH_DELAY);
1932 dprintk("channel %c clear interrupt condition\n",
1933 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1934}
1935
1936static int vino_capture(struct vino_channel_settings *vcs,
1937 struct vino_framebuffer *fb)
1938{
1939 int err = 0;
1940 unsigned long flags, flags2;
1941
1942 spin_lock_irqsave(&fb->state_lock, flags);
1943
1944 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1945 err = -EBUSY;
1946 fb->state = VINO_FRAMEBUFFER_IN_USE;
1947
1948 spin_unlock_irqrestore(&fb->state_lock, flags);
1949
1950 if (err)
1951 return err;
1952
1953 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1954 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1955
1956 vino_dma_setup(vcs, fb);
1957 vino_dma_start(vcs);
1958
1959 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1960 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1961
1962 return err;
1963}
1964
1965static
1966struct vino_framebuffer *vino_capture_enqueue(struct
1967 vino_channel_settings *vcs,
1968 unsigned int index)
1969{
1970 struct vino_framebuffer *fb;
1971 unsigned long flags;
1972
1973 dprintk("vino_capture_enqueue():\n");
1974
1975 spin_lock_irqsave(&vcs->capture_lock, flags);
1976
1977 fb = vino_queue_add(&vcs->fb_queue, index);
1978 if (fb == NULL) {
1979 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1980 "queue full?\n");
1981 goto out;
1982 }
1983out:
1984 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1985
1986 return fb;
1987}
1988
1989static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1990{
1991 struct vino_framebuffer *fb;
1992 unsigned int incoming, id;
1993 int err = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00001994 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001995
1996 dprintk("vino_capture_next():\n");
1997
1998 spin_lock_irqsave(&vcs->capture_lock, flags);
1999
2000 if (start) {
2001 /* start capture only if capture isn't in progress already */
2002 if (vcs->capturing) {
2003 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2004 return 0;
2005 }
2006
2007 } else {
2008 /* capture next frame:
2009 * stop capture if capturing is not set */
2010 if (!vcs->capturing) {
2011 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2012 return 0;
2013 }
2014 }
2015
2016 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2017 if (err) {
2018 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2019 "failed\n");
2020 err = -EINVAL;
2021 goto out;
2022 }
2023 if (incoming == 0) {
2024 dprintk("vino_capture_next(): no buffers available\n");
2025 goto out;
2026 }
2027
2028 fb = vino_queue_peek(&vcs->fb_queue, &id);
2029 if (fb == NULL) {
2030 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2031 err = -EINVAL;
2032 goto out;
2033 }
2034
Ralf Baechled203a7e2005-09-06 15:19:37 -07002035 if (start) {
2036 vcs->capturing = 1;
2037 }
2038
2039 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2040
2041 err = vino_capture(vcs, fb);
2042
2043 return err;
2044
2045out:
2046 vcs->capturing = 0;
2047 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2048
2049 return err;
2050}
2051
Ladislav Michla637a112005-09-01 15:07:34 +00002052static inline int vino_is_capturing(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002053{
2054 int ret;
2055 unsigned long flags;
2056
2057 spin_lock_irqsave(&vcs->capture_lock, flags);
2058
2059 ret = vcs->capturing;
2060
2061 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2062
2063 return ret;
2064}
2065
2066/* waits until a frame is captured */
2067static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2068{
2069 wait_queue_t wait;
2070 int err = 0;
2071
2072 dprintk("vino_wait_for_frame():\n");
2073
2074 init_waitqueue_entry(&wait, current);
2075 /* add ourselves into wait queue */
2076 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002077
2078 /* to ensure that schedule_timeout will return immediately
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -03002079 * if VINO interrupt was triggered meanwhile */
Mauro Carvalho Chehab818ca472007-07-17 16:29:07 -03002080 schedule_timeout_interruptible(msecs_to_jiffies(100));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002081
2082 if (signal_pending(current))
2083 err = -EINTR;
2084
2085 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2086
2087 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2088 err ? "failed" : "ok");
2089
2090 return err;
2091}
2092
2093/* the function assumes that PAGE_SIZE % 4 == 0 */
2094static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2095 unsigned char *pageptr;
2096 unsigned int page, i;
2097 unsigned char a;
2098
2099 for (page = 0; page < fb->desc_table.page_count; page++) {
2100 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2101
2102 for (i = 0; i < PAGE_SIZE; i += 4) {
2103 a = pageptr[0];
2104 pageptr[0] = pageptr[3];
2105 pageptr[1] = pageptr[2];
2106 pageptr[2] = pageptr[1];
2107 pageptr[3] = a;
2108 pageptr += 4;
2109 }
2110 }
2111}
2112
2113/* checks if the buffer is in correct state and syncs data */
2114static int vino_check_buffer(struct vino_channel_settings *vcs,
2115 struct vino_framebuffer *fb)
2116{
2117 int err = 0;
2118 unsigned long flags;
2119
2120 dprintk("vino_check_buffer():\n");
2121
2122 spin_lock_irqsave(&fb->state_lock, flags);
2123 switch (fb->state) {
2124 case VINO_FRAMEBUFFER_IN_USE:
2125 err = -EIO;
2126 break;
2127 case VINO_FRAMEBUFFER_READY:
2128 vino_sync_buffer(fb);
2129 fb->state = VINO_FRAMEBUFFER_UNUSED;
2130 break;
2131 default:
2132 err = -EINVAL;
2133 }
2134 spin_unlock_irqrestore(&fb->state_lock, flags);
2135
2136 if (!err) {
2137 if (vino_pixel_conversion
2138 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2139 vino_convert_to_rgba(fb);
2140 }
2141 } else if (err && (err != -EINVAL)) {
2142 dprintk("vino_check_buffer(): buffer not ready\n");
2143
2144 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2145 vino_dma_stop(vcs);
2146 vino_clear_interrupt(vcs);
2147 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2148 }
2149
2150 return err;
2151}
2152
2153/* forcefully terminates capture */
2154static void vino_capture_stop(struct vino_channel_settings *vcs)
2155{
2156 unsigned int incoming = 0, outgoing = 0, id;
2157 unsigned long flags, flags2;
2158
2159 dprintk("vino_capture_stop():\n");
2160
2161 spin_lock_irqsave(&vcs->capture_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00002162
Ralf Baechled203a7e2005-09-06 15:19:37 -07002163 /* unset capturing to stop queue processing */
2164 vcs->capturing = 0;
2165
2166 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2167
2168 vino_dma_stop(vcs);
2169 vino_clear_interrupt(vcs);
2170
2171 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2172
2173 /* remove all items from the queue */
2174 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2175 dprintk("vino_capture_stop(): "
2176 "vino_queue_get_incoming() failed\n");
2177 goto out;
2178 }
2179 while (incoming > 0) {
2180 vino_queue_transfer(&vcs->fb_queue);
2181
2182 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2183 dprintk("vino_capture_stop(): "
2184 "vino_queue_get_incoming() failed\n");
2185 goto out;
2186 }
2187 }
2188
2189 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2190 dprintk("vino_capture_stop(): "
2191 "vino_queue_get_outgoing() failed\n");
2192 goto out;
2193 }
2194 while (outgoing > 0) {
2195 vino_queue_remove(&vcs->fb_queue, &id);
2196
2197 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2198 dprintk("vino_capture_stop(): "
2199 "vino_queue_get_outgoing() failed\n");
2200 goto out;
2201 }
2202 }
2203
2204out:
2205 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2206}
2207
Ladislav Michla637a112005-09-01 15:07:34 +00002208#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07002209static int vino_capture_failed(struct vino_channel_settings *vcs)
2210{
2211 struct vino_framebuffer *fb;
2212 unsigned long flags;
2213 unsigned int i;
2214 int ret;
2215
2216 dprintk("vino_capture_failed():\n");
2217
2218 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2219
2220 vino_dma_stop(vcs);
2221 vino_clear_interrupt(vcs);
2222
2223 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2224
2225 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2226 if (ret == VINO_QUEUE_ERROR) {
2227 dprintk("vino_queue_get_incoming() failed\n");
2228 return -EINVAL;
2229 }
2230 if (i == 0) {
2231 /* no buffers to process */
2232 return 0;
2233 }
2234
2235 fb = vino_queue_peek(&vcs->fb_queue, &i);
2236 if (fb == NULL) {
2237 dprintk("vino_queue_peek() failed\n");
2238 return -EINVAL;
2239 }
2240
2241 spin_lock_irqsave(&fb->state_lock, flags);
2242 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2243 fb->state = VINO_FRAMEBUFFER_UNUSED;
2244 vino_queue_transfer(&vcs->fb_queue);
2245 vino_queue_remove(&vcs->fb_queue, &i);
2246 /* we should actually discard the newest frame,
2247 * but who cares ... */
2248 }
2249 spin_unlock_irqrestore(&fb->state_lock, flags);
2250
2251 return 0;
2252}
Ladislav Michla637a112005-09-01 15:07:34 +00002253#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002254
Ladislav Michla637a112005-09-01 15:07:34 +00002255static void vino_skip_frame(struct vino_channel_settings *vcs)
2256{
2257 struct vino_framebuffer *fb;
2258 unsigned long flags;
2259 unsigned int id;
2260
2261 spin_lock_irqsave(&vcs->capture_lock, flags);
2262 fb = vino_queue_peek(&vcs->fb_queue, &id);
2263 if (!fb) {
2264 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2265 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2266 return;
2267 }
2268 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2269
2270 spin_lock_irqsave(&fb->state_lock, flags);
2271 fb->state = VINO_FRAMEBUFFER_UNUSED;
2272 spin_unlock_irqrestore(&fb->state_lock, flags);
2273
2274 vino_capture_next(vcs, 0);
2275}
2276
2277static void vino_frame_done(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002278{
2279 struct vino_framebuffer *fb;
2280 unsigned long flags;
2281
2282 spin_lock_irqsave(&vcs->capture_lock, flags);
2283 fb = vino_queue_transfer(&vcs->fb_queue);
2284 if (!fb) {
2285 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2286 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2287 return;
2288 }
2289 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2290
Ladislav Michla637a112005-09-01 15:07:34 +00002291 fb->frame_counter = vcs->int_data.frame_counter;
2292 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2293 sizeof(struct timeval));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002294
2295 spin_lock_irqsave(&fb->state_lock, flags);
2296 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2297 fb->state = VINO_FRAMEBUFFER_READY;
2298 spin_unlock_irqrestore(&fb->state_lock, flags);
2299
2300 wake_up(&vcs->fb_queue.frame_wait_queue);
2301
2302 vino_capture_next(vcs, 0);
2303}
2304
Ladislav Michla637a112005-09-01 15:07:34 +00002305static void vino_capture_tasklet(unsigned long channel) {
2306 struct vino_channel_settings *vcs;
2307
2308 vcs = (channel == VINO_CHANNEL_A)
2309 ? &vino_drvdata->a : &vino_drvdata->b;
2310
2311 if (vcs->int_data.skip)
2312 vcs->int_data.skip_count++;
2313
2314 if (vcs->int_data.skip && (vcs->int_data.skip_count
2315 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2316 vino_skip_frame(vcs);
2317 } else {
2318 vcs->int_data.skip_count = 0;
2319 vino_frame_done(vcs);
2320 }
2321}
2322
David Howells7d12e782006-10-05 14:55:46 +01002323static irqreturn_t vino_interrupt(int irq, void *dev_id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002324{
Ladislav Michla637a112005-09-01 15:07:34 +00002325 u32 ctrl, intr;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002326 unsigned int fc_a, fc_b;
Ladislav Michla637a112005-09-01 15:07:34 +00002327 int handled_a = 0, skip_a = 0, done_a = 0;
2328 int handled_b = 0, skip_b = 0, done_b = 0;
2329
2330#ifdef VINO_DEBUG_INT
2331 int loop = 0;
2332 unsigned int line_count = vino->a.line_count,
2333 page_index = vino->a.page_index,
2334 field_counter = vino->a.field_counter,
2335 start_desc_tbl = vino->a.start_desc_tbl,
2336 next_4_desc = vino->a.next_4_desc;
2337 unsigned int line_count_2,
2338 page_index_2,
2339 field_counter_2,
2340 start_desc_tbl_2,
2341 next_4_desc_2;
2342#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002343
2344 spin_lock(&vino_drvdata->vino_lock);
2345
Ladislav Michla637a112005-09-01 15:07:34 +00002346 while ((intr = vino->intr_status)) {
2347 fc_a = vino->a.field_counter >> 1;
2348 fc_b = vino->b.field_counter >> 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002349
Ladislav Michla637a112005-09-01 15:07:34 +00002350 /* handle error-interrupts in some special way ?
2351 * --> skips frames */
2352 if (intr & VINO_INTSTAT_A) {
2353 if (intr & VINO_INTSTAT_A_EOF) {
2354 vino_drvdata->a.field++;
2355 if (vino_drvdata->a.field > 1) {
2356 vino_dma_stop(&vino_drvdata->a);
2357 vino_clear_interrupt(&vino_drvdata->a);
2358 vino_drvdata->a.field = 0;
2359 done_a = 1;
2360 } else {
2361 if (vino->a.page_index
2362 != vino_drvdata->a.line_size) {
2363 vino->a.line_count = 0;
2364 vino->a.page_index =
2365 vino_drvdata->
2366 a.line_size;
2367 vino->a.next_4_desc =
2368 vino->a.start_desc_tbl;
2369 }
2370 }
2371 dprintk("channel A end-of-field "
2372 "interrupt: %04x\n", intr);
2373 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002374 vino_dma_stop(&vino_drvdata->a);
2375 vino_clear_interrupt(&vino_drvdata->a);
2376 vino_drvdata->a.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002377 skip_a = 1;
2378 dprintk("channel A error interrupt: %04x\n",
2379 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002380 }
Ladislav Michla637a112005-09-01 15:07:34 +00002381
2382#ifdef VINO_DEBUG_INT
2383 line_count_2 = vino->a.line_count;
2384 page_index_2 = vino->a.page_index;
2385 field_counter_2 = vino->a.field_counter;
2386 start_desc_tbl_2 = vino->a.start_desc_tbl;
2387 next_4_desc_2 = vino->a.next_4_desc;
2388
2389 printk("intr = %04x, loop = %d, field = %d\n",
2390 intr, loop, vino_drvdata->a.field);
2391 printk("1- line count = %04d, page index = %04d, "
2392 "start = %08x, next = %08x\n"
2393 " fieldc = %d, framec = %d\n",
2394 line_count, page_index, start_desc_tbl,
2395 next_4_desc, field_counter, fc_a);
2396 printk("12-line count = %04d, page index = %04d, "
2397 " start = %08x, next = %08x\n",
2398 line_count_2, page_index_2, start_desc_tbl_2,
2399 next_4_desc_2);
2400
2401 if (done_a)
2402 printk("\n");
2403#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002404 }
Ladislav Michla637a112005-09-01 15:07:34 +00002405
2406 if (intr & VINO_INTSTAT_B) {
2407 if (intr & VINO_INTSTAT_B_EOF) {
2408 vino_drvdata->b.field++;
2409 if (vino_drvdata->b.field > 1) {
2410 vino_dma_stop(&vino_drvdata->b);
2411 vino_clear_interrupt(&vino_drvdata->b);
2412 vino_drvdata->b.field = 0;
2413 done_b = 1;
2414 }
2415 dprintk("channel B end-of-field "
2416 "interrupt: %04x\n", intr);
2417 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002418 vino_dma_stop(&vino_drvdata->b);
2419 vino_clear_interrupt(&vino_drvdata->b);
2420 vino_drvdata->b.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002421 skip_b = 1;
2422 dprintk("channel B error interrupt: %04x\n",
2423 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002424 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002425 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002426
Ladislav Michla637a112005-09-01 15:07:34 +00002427 /* Always remember to clear interrupt status.
2428 * Disable VINO interrupts while we do this. */
2429 ctrl = vino->control;
2430 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2431 vino->intr_status = ~intr;
2432 vino->control = ctrl;
2433
2434 spin_unlock(&vino_drvdata->vino_lock);
2435
2436 if ((!handled_a) && (done_a || skip_a)) {
2437 if (!skip_a) {
2438 do_gettimeofday(&vino_drvdata->
2439 a.int_data.timestamp);
2440 vino_drvdata->a.int_data.frame_counter = fc_a;
2441 }
2442 vino_drvdata->a.int_data.skip = skip_a;
2443
2444 dprintk("channel A %s, interrupt: %d\n",
2445 skip_a ? "skipping frame" : "frame done",
2446 intr);
2447 tasklet_hi_schedule(&vino_tasklet_a);
2448 handled_a = 1;
2449 }
2450
2451 if ((!handled_b) && (done_b || skip_b)) {
2452 if (!skip_b) {
2453 do_gettimeofday(&vino_drvdata->
2454 b.int_data.timestamp);
2455 vino_drvdata->b.int_data.frame_counter = fc_b;
2456 }
2457 vino_drvdata->b.int_data.skip = skip_b;
2458
2459 dprintk("channel B %s, interrupt: %d\n",
2460 skip_b ? "skipping frame" : "frame done",
2461 intr);
2462 tasklet_hi_schedule(&vino_tasklet_b);
2463 handled_b = 1;
2464 }
2465
2466#ifdef VINO_DEBUG_INT
2467 loop++;
2468#endif
2469 spin_lock(&vino_drvdata->vino_lock);
2470 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002471
2472 spin_unlock(&vino_drvdata->vino_lock);
2473
Ralf Baechled203a7e2005-09-06 15:19:37 -07002474 return IRQ_HANDLED;
2475}
2476
2477/* VINO video input management */
2478
2479static int vino_get_saa7191_input(int input)
2480{
2481 switch (input) {
2482 case VINO_INPUT_COMPOSITE:
2483 return SAA7191_INPUT_COMPOSITE;
2484 case VINO_INPUT_SVIDEO:
2485 return SAA7191_INPUT_SVIDEO;
2486 default:
2487 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2488 "invalid input!\n");
2489 return -1;
2490 }
2491}
2492
Ladislav Michla637a112005-09-01 15:07:34 +00002493static int vino_get_saa7191_norm(unsigned int data_norm)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002494{
Ladislav Michla637a112005-09-01 15:07:34 +00002495 switch (data_norm) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002496 case VINO_DATA_NORM_AUTO:
2497 return SAA7191_NORM_AUTO;
Ladislav Michla637a112005-09-01 15:07:34 +00002498 case VINO_DATA_NORM_AUTO_EXT:
2499 return SAA7191_NORM_AUTO_EXT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002500 case VINO_DATA_NORM_PAL:
2501 return SAA7191_NORM_PAL;
2502 case VINO_DATA_NORM_NTSC:
2503 return SAA7191_NORM_NTSC;
2504 case VINO_DATA_NORM_SECAM:
2505 return SAA7191_NORM_SECAM;
2506 default:
2507 printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
2508 "invalid norm!\n");
2509 return -1;
2510 }
2511}
2512
Ladislav Michla637a112005-09-01 15:07:34 +00002513static int vino_get_from_saa7191_norm(int saa7191_norm)
2514{
2515 switch (saa7191_norm) {
2516 case SAA7191_NORM_PAL:
2517 return VINO_DATA_NORM_PAL;
2518 case SAA7191_NORM_NTSC:
2519 return VINO_DATA_NORM_NTSC;
2520 case SAA7191_NORM_SECAM:
2521 return VINO_DATA_NORM_SECAM;
2522 default:
2523 printk(KERN_ERR "VINO: vino_get_from_saa7191_norm(): "
2524 "invalid norm!\n");
2525 return VINO_DATA_NORM_NONE;
2526 }
2527}
2528
2529static int vino_saa7191_set_norm(unsigned int *data_norm)
2530{
2531 int saa7191_norm, new_data_norm;
2532 int err = 0;
2533
2534 saa7191_norm = vino_get_saa7191_norm(*data_norm);
2535
2536 err = i2c_decoder_command(DECODER_SAA7191_SET_NORM,
2537 &saa7191_norm);
2538 if (err)
2539 goto out;
2540
2541 if ((*data_norm == VINO_DATA_NORM_AUTO)
2542 || (*data_norm == VINO_DATA_NORM_AUTO_EXT)) {
2543 struct saa7191_status status;
2544
2545 err = i2c_decoder_command(DECODER_SAA7191_GET_STATUS,
2546 &status);
2547 if (err)
2548 goto out;
2549
2550 new_data_norm =
2551 vino_get_from_saa7191_norm(status.norm);
2552 if (new_data_norm == VINO_DATA_NORM_NONE) {
2553 err = -EINVAL;
2554 goto out;
2555 }
2556
2557 *data_norm = (unsigned int)new_data_norm;
2558 }
2559
2560out:
2561 return err;
2562}
2563
Ralf Baechled203a7e2005-09-06 15:19:37 -07002564/* execute with input_lock locked */
2565static int vino_is_input_owner(struct vino_channel_settings *vcs)
2566{
2567 switch(vcs->input) {
2568 case VINO_INPUT_COMPOSITE:
2569 case VINO_INPUT_SVIDEO:
2570 return (vino_drvdata->decoder.owner == vcs->channel);
2571 case VINO_INPUT_D1:
2572 return (vino_drvdata->camera.owner == vcs->channel);
2573 default:
2574 return 0;
2575 }
2576}
2577
2578static int vino_acquire_input(struct vino_channel_settings *vcs)
2579{
Ladislav Michla637a112005-09-01 15:07:34 +00002580 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002581 int ret = 0;
2582
2583 dprintk("vino_acquire_input():\n");
2584
Ladislav Michla637a112005-09-01 15:07:34 +00002585 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002586
2587 /* First try D1 and then SAA7191 */
2588 if (vino_drvdata->camera.driver
2589 && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002590 i2c_use_client(vino_drvdata->camera.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002591 vino_drvdata->camera.owner = vcs->channel;
2592 vcs->input = VINO_INPUT_D1;
2593 vcs->data_norm = VINO_DATA_NORM_D1;
2594 } else if (vino_drvdata->decoder.driver
2595 && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
Ladislav Michla637a112005-09-01 15:07:34 +00002596 int input, data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002597 int saa7191_input;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002598
Jean Delvaree48d3312008-01-27 18:14:48 +01002599 i2c_use_client(vino_drvdata->decoder.driver);
Ladislav Michla637a112005-09-01 15:07:34 +00002600 input = VINO_INPUT_COMPOSITE;
2601
2602 saa7191_input = vino_get_saa7191_input(input);
2603 ret = i2c_decoder_command(DECODER_SET_INPUT,
2604 &saa7191_input);
2605 if (ret) {
2606 ret = -EINVAL;
2607 goto out;
2608 }
2609
2610 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2611
2612 /* Don't hold spinlocks while auto-detecting norm
2613 * as it may take a while... */
2614
2615 data_norm = VINO_DATA_NORM_AUTO_EXT;
2616
2617 ret = vino_saa7191_set_norm(&data_norm);
2618 if ((ret == -EBUSY) || (ret == -EAGAIN)) {
2619 data_norm = VINO_DATA_NORM_PAL;
2620 ret = vino_saa7191_set_norm(&data_norm);
2621 }
2622
2623 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2624
2625 if (ret) {
2626 ret = -EINVAL;
2627 goto out;
2628 }
2629
Ralf Baechled203a7e2005-09-06 15:19:37 -07002630 vino_drvdata->decoder.owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002631
Ladislav Michla637a112005-09-01 15:07:34 +00002632 vcs->input = input;
2633 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002634 } else {
2635 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2636 vino_drvdata->b.input : vino_drvdata->a.input;
2637 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2638 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2639 }
2640
2641 if (vcs->input == VINO_INPUT_NONE) {
2642 ret = -ENODEV;
2643 goto out;
2644 }
2645
Ladislav Michla637a112005-09-01 15:07:34 +00002646 vino_set_default_clipping(vcs);
2647 vino_set_default_scaling(vcs);
2648 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002649
2650 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2651
2652out:
Ladislav Michla637a112005-09-01 15:07:34 +00002653 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002654
2655 return ret;
2656}
2657
2658static int vino_set_input(struct vino_channel_settings *vcs, int input)
2659{
2660 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2661 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002662 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002663 int ret = 0;
2664
2665 dprintk("vino_set_input():\n");
2666
Ladislav Michla637a112005-09-01 15:07:34 +00002667 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002668
2669 if (vcs->input == input)
2670 goto out;
2671
Ladislav Michla637a112005-09-01 15:07:34 +00002672 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002673 case VINO_INPUT_COMPOSITE:
2674 case VINO_INPUT_SVIDEO:
2675 if (!vino_drvdata->decoder.driver) {
2676 ret = -EINVAL;
2677 goto out;
2678 }
2679
2680 if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002681 i2c_use_client(vino_drvdata->decoder.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002682 vino_drvdata->decoder.owner = vcs->channel;
2683 }
2684
2685 if (vino_drvdata->decoder.owner == vcs->channel) {
Ladislav Michla637a112005-09-01 15:07:34 +00002686 int data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002687 int saa7191_input;
Ladislav Michla637a112005-09-01 15:07:34 +00002688
2689 saa7191_input = vino_get_saa7191_input(input);
2690 ret = i2c_decoder_command(DECODER_SET_INPUT,
2691 &saa7191_input);
2692 if (ret) {
2693 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2694 ret = -EINVAL;
2695 goto out;
2696 }
2697
2698 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2699
2700 /* Don't hold spinlocks while auto-detecting norm
2701 * as it may take a while... */
2702
2703 data_norm = VINO_DATA_NORM_AUTO_EXT;
2704
2705 ret = vino_saa7191_set_norm(&data_norm);
2706 if ((ret == -EBUSY) || (ret == -EAGAIN)) {
2707 data_norm = VINO_DATA_NORM_PAL;
2708 ret = vino_saa7191_set_norm(&data_norm);
2709 }
2710
2711 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2712
2713 if (ret) {
2714 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2715 ret = -EINVAL;
2716 goto out;
2717 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002718
2719 vcs->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +00002720 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002721 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00002722 if (input != vcs2->input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002723 ret = -EBUSY;
2724 goto out;
2725 }
2726
2727 vcs->input = input;
2728 vcs->data_norm = vcs2->data_norm;
2729 }
2730
2731 if (vino_drvdata->camera.owner == vcs->channel) {
2732 /* Transfer the ownership or release the input */
2733 if (vcs2->input == VINO_INPUT_D1) {
2734 vino_drvdata->camera.owner = vcs2->channel;
2735 } else {
2736 i2c_release_client(vino_drvdata->
2737 camera.driver);
2738 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2739 }
2740 }
2741 break;
2742 case VINO_INPUT_D1:
2743 if (!vino_drvdata->camera.driver) {
2744 ret = -EINVAL;
2745 goto out;
2746 }
2747
2748 if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
Jean Delvaree48d3312008-01-27 18:14:48 +01002749 i2c_use_client(vino_drvdata->camera.driver);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002750 vino_drvdata->camera.owner = vcs->channel;
2751 }
2752
2753 if (vino_drvdata->decoder.owner == vcs->channel) {
2754 /* Transfer the ownership or release the input */
2755 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2756 (vcs2->input == VINO_INPUT_SVIDEO)) {
2757 vino_drvdata->decoder.owner = vcs2->channel;
2758 } else {
2759 i2c_release_client(vino_drvdata->
2760 decoder.driver);
2761 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2762 }
2763 }
2764
2765 vcs->input = input;
2766 vcs->data_norm = VINO_DATA_NORM_D1;
2767 break;
2768 default:
2769 ret = -EINVAL;
2770 goto out;
2771 }
2772
2773 vino_set_default_clipping(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002774 vino_set_default_scaling(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002775 vino_set_default_framerate(vcs);
2776
2777 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2778
2779out:
Ladislav Michla637a112005-09-01 15:07:34 +00002780 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002781
2782 return ret;
2783}
2784
2785static void vino_release_input(struct vino_channel_settings *vcs)
2786{
2787 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2788 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002789 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002790
2791 dprintk("vino_release_input():\n");
2792
Ladislav Michla637a112005-09-01 15:07:34 +00002793 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002794
2795 /* Release ownership of the channel
2796 * and if the other channel takes input from
2797 * the same source, transfer the ownership */
2798 if (vino_drvdata->camera.owner == vcs->channel) {
2799 if (vcs2->input == VINO_INPUT_D1) {
2800 vino_drvdata->camera.owner = vcs2->channel;
2801 } else {
2802 i2c_release_client(vino_drvdata->camera.driver);
2803 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2804 }
2805 } else if (vino_drvdata->decoder.owner == vcs->channel) {
2806 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2807 (vcs2->input == VINO_INPUT_SVIDEO)) {
2808 vino_drvdata->decoder.owner = vcs2->channel;
2809 } else {
2810 i2c_release_client(vino_drvdata->decoder.driver);
2811 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2812 }
2813 }
2814 vcs->input = VINO_INPUT_NONE;
2815
Ladislav Michla637a112005-09-01 15:07:34 +00002816 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002817}
2818
2819/* execute with input_lock locked */
2820static int vino_set_data_norm(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00002821 unsigned int data_norm,
2822 unsigned long *flags)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002823{
Ladislav Michla637a112005-09-01 15:07:34 +00002824 int err = 0;
2825
2826 if (data_norm == vcs->data_norm)
2827 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002828
2829 switch (vcs->input) {
2830 case VINO_INPUT_D1:
2831 /* only one "norm" supported */
Ladislav Michla637a112005-09-01 15:07:34 +00002832 if ((data_norm != VINO_DATA_NORM_D1)
2833 && (data_norm != VINO_DATA_NORM_AUTO)
2834 && (data_norm != VINO_DATA_NORM_AUTO_EXT))
Ralf Baechled203a7e2005-09-06 15:19:37 -07002835 return -EINVAL;
2836 break;
2837 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00002838 case VINO_INPUT_SVIDEO: {
2839 if ((data_norm != VINO_DATA_NORM_PAL)
2840 && (data_norm != VINO_DATA_NORM_NTSC)
2841 && (data_norm != VINO_DATA_NORM_SECAM)
2842 && (data_norm != VINO_DATA_NORM_AUTO)
2843 && (data_norm != VINO_DATA_NORM_AUTO_EXT))
2844 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002845
Ladislav Michla637a112005-09-01 15:07:34 +00002846 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002847
Ladislav Michla637a112005-09-01 15:07:34 +00002848 /* Don't hold spinlocks while setting norm
2849 * as it may take a while... */
2850
2851 err = vino_saa7191_set_norm(&data_norm);
2852
2853 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2854
2855 if (err)
2856 goto out;
2857
Ralf Baechled203a7e2005-09-06 15:19:37 -07002858 vcs->data_norm = data_norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002859
2860 vino_set_default_clipping(vcs);
2861 vino_set_default_scaling(vcs);
2862 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002863 break;
Ladislav Michla637a112005-09-01 15:07:34 +00002864 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002865 default:
2866 return -EINVAL;
2867 }
2868
Ladislav Michla637a112005-09-01 15:07:34 +00002869out:
2870 return err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002871}
2872
2873/* V4L2 helper functions */
2874
2875static int vino_find_data_format(__u32 pixelformat)
2876{
2877 int i;
2878
2879 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2880 if (vino_data_formats[i].pixelformat == pixelformat)
2881 return i;
2882 }
2883
2884 return VINO_DATA_FMT_NONE;
2885}
2886
Hans Verkuilc49cb362009-02-12 10:32:50 -03002887static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002888{
2889 int input = VINO_INPUT_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002890 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002891
Ladislav Michla637a112005-09-01 15:07:34 +00002892 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002893 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2894 switch (index) {
2895 case 0:
2896 input = VINO_INPUT_COMPOSITE;
2897 break;
2898 case 1:
2899 input = VINO_INPUT_SVIDEO;
2900 break;
2901 case 2:
2902 input = VINO_INPUT_D1;
2903 break;
2904 }
2905 } else if (vino_drvdata->decoder.driver) {
2906 switch (index) {
2907 case 0:
2908 input = VINO_INPUT_COMPOSITE;
2909 break;
2910 case 1:
2911 input = VINO_INPUT_SVIDEO;
2912 break;
2913 }
2914 } else if (vino_drvdata->camera.driver) {
2915 switch (index) {
2916 case 0:
2917 input = VINO_INPUT_D1;
2918 break;
2919 }
2920 }
Ladislav Michla637a112005-09-01 15:07:34 +00002921 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002922
2923 return input;
2924}
2925
2926/* execute with input_lock locked */
2927static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2928{
2929 __u32 index = 0;
2930 // FIXME: detect when no inputs available
2931
2932 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2933 switch (vcs->input) {
2934 case VINO_INPUT_COMPOSITE:
2935 index = 0;
2936 break;
2937 case VINO_INPUT_SVIDEO:
2938 index = 1;
2939 break;
2940 case VINO_INPUT_D1:
2941 index = 2;
2942 break;
2943 }
2944 } else if (vino_drvdata->decoder.driver) {
2945 switch (vcs->input) {
2946 case VINO_INPUT_COMPOSITE:
2947 index = 0;
2948 break;
2949 case VINO_INPUT_SVIDEO:
2950 index = 1;
2951 break;
2952 }
2953 } else if (vino_drvdata->camera.driver) {
2954 switch (vcs->input) {
2955 case VINO_INPUT_D1:
2956 index = 0;
2957 break;
2958 }
2959 }
2960
2961 return index;
2962}
2963
2964/* V4L2 ioctls */
2965
Hans Verkuilc49cb362009-02-12 10:32:50 -03002966static int vino_querycap(struct file *file, void *__fh,
2967 struct v4l2_capability *cap)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002968{
2969 memset(cap, 0, sizeof(struct v4l2_capability));
2970
2971 strcpy(cap->driver, vino_driver_name);
2972 strcpy(cap->card, vino_driver_description);
2973 strcpy(cap->bus_info, vino_bus_name);
2974 cap->version = VINO_VERSION_CODE;
2975 cap->capabilities =
2976 V4L2_CAP_VIDEO_CAPTURE |
2977 V4L2_CAP_STREAMING;
2978 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
Hans Verkuilc49cb362009-02-12 10:32:50 -03002979 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002980}
2981
Hans Verkuilc49cb362009-02-12 10:32:50 -03002982static int vino_enum_input(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07002983 struct v4l2_input *i)
2984{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002985 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002986 __u32 index = i->index;
2987 int input;
2988 dprintk("requested index = %d\n", index);
2989
Hans Verkuilc49cb362009-02-12 10:32:50 -03002990 input = vino_int_enum_input(vcs, index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002991 if (input == VINO_INPUT_NONE)
2992 return -EINVAL;
2993
2994 memset(i, 0, sizeof(struct v4l2_input));
2995
2996 i->index = index;
2997 i->type = V4L2_INPUT_TYPE_CAMERA;
2998 i->std = vino_inputs[input].std;
2999 strcpy(i->name, vino_inputs[input].name);
3000
3001 if ((input == VINO_INPUT_COMPOSITE)
3002 || (input == VINO_INPUT_SVIDEO)) {
3003 struct saa7191_status status;
3004 i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
3005 i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
3006 i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
3007 }
3008
3009 return 0;
3010}
3011
Hans Verkuilc49cb362009-02-12 10:32:50 -03003012static int vino_g_input(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00003013 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003014{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003015 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003016 __u32 index;
3017 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00003018 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003019
Ladislav Michla637a112005-09-01 15:07:34 +00003020 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003021 input = vcs->input;
3022 index = vino_find_input_index(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00003023 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003024
3025 dprintk("input = %d\n", input);
3026
3027 if (input == VINO_INPUT_NONE) {
3028 return -EINVAL;
3029 }
3030
Ladislav Michla637a112005-09-01 15:07:34 +00003031 *i = index;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003032
3033 return 0;
3034}
3035
Hans Verkuilc49cb362009-02-12 10:32:50 -03003036static int vino_s_input(struct file *file, void *__fh,
3037 unsigned int i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003038{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003039 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003040 int input;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003041 dprintk("requested input = %d\n", i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003042
Hans Verkuilc49cb362009-02-12 10:32:50 -03003043 input = vino_int_enum_input(vcs, i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003044 if (input == VINO_INPUT_NONE)
3045 return -EINVAL;
3046
3047 return vino_set_input(vcs, input);
3048}
3049
Hans Verkuilc49cb362009-02-12 10:32:50 -03003050static int vino_querystd(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00003051 v4l2_std_id *std)
3052{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003053 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003054 unsigned long flags;
3055 int err = 0;
3056
3057 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3058
3059 switch (vcs->input) {
3060 case VINO_INPUT_D1:
3061 *std = vino_inputs[vcs->input].std;
3062 break;
3063 case VINO_INPUT_COMPOSITE:
3064 case VINO_INPUT_SVIDEO: {
3065 struct saa7191_status status;
3066
3067 i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
3068
3069 if (status.signal) {
3070 if (status.signal_60hz) {
3071 *std = V4L2_STD_NTSC;
3072 } else {
3073 *std = V4L2_STD_PAL | V4L2_STD_SECAM;
3074 }
3075 } else {
3076 *std = vino_inputs[vcs->input].std;
3077 }
3078 break;
3079 }
3080 default:
3081 err = -EINVAL;
3082 }
3083
3084 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3085
3086 return err;
3087}
3088
Hans Verkuilc49cb362009-02-12 10:32:50 -03003089static int vino_g_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003090 v4l2_std_id *std)
3091{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003092 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003093 unsigned long flags;
3094
3095 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3096
Ralf Baechled203a7e2005-09-06 15:19:37 -07003097 *std = vino_data_norms[vcs->data_norm].std;
Ladislav Michla637a112005-09-01 15:07:34 +00003098 dprintk("current standard = %d\n", vcs->data_norm);
3099
3100 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003101
3102 return 0;
3103}
3104
Hans Verkuilc49cb362009-02-12 10:32:50 -03003105static int vino_s_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003106 v4l2_std_id *std)
3107{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003108 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003109 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003110 int ret = 0;
3111
Ladislav Michla637a112005-09-01 15:07:34 +00003112 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3113
3114 if (!vino_is_input_owner(vcs)) {
3115 ret = -EBUSY;
3116 goto out;
3117 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003118
3119 /* check if the standard is valid for the current input */
Ladislav Michla637a112005-09-01 15:07:34 +00003120 if ((*std) & vino_inputs[vcs->input].std) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003121 dprintk("standard accepted\n");
3122
3123 /* change the video norm for SAA7191
3124 * and accept NTSC for D1 (do nothing) */
3125
3126 if (vcs->input == VINO_INPUT_D1)
3127 goto out;
3128
Ladislav Michla637a112005-09-01 15:07:34 +00003129 if (((*std) & V4L2_STD_PAL)
3130 && ((*std) & V4L2_STD_NTSC)
3131 && ((*std) & V4L2_STD_SECAM)) {
3132 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_AUTO_EXT,
3133 &flags);
3134 } else if ((*std) & V4L2_STD_PAL) {
3135 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3136 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003137 } else if ((*std) & V4L2_STD_NTSC) {
Ladislav Michla637a112005-09-01 15:07:34 +00003138 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3139 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003140 } else if ((*std) & V4L2_STD_SECAM) {
Ladislav Michla637a112005-09-01 15:07:34 +00003141 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3142 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003143 } else {
3144 ret = -EINVAL;
3145 }
Ladislav Michla637a112005-09-01 15:07:34 +00003146
3147 if (ret) {
3148 ret = -EINVAL;
3149 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003150 } else {
3151 ret = -EINVAL;
3152 }
3153
3154out:
Ladislav Michla637a112005-09-01 15:07:34 +00003155 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003156
3157 return ret;
3158}
3159
Hans Verkuilc49cb362009-02-12 10:32:50 -03003160static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003161 struct v4l2_fmtdesc *fd)
3162{
3163 enum v4l2_buf_type type = fd->type;
3164 int index = fd->index;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003165
Ralf Baechled203a7e2005-09-06 15:19:37 -07003166 dprintk("format index = %d\n", index);
3167
Hans Verkuilc49cb362009-02-12 10:32:50 -03003168 if ((fd->index < 0) ||
3169 (fd->index >= VINO_DATA_FMT_COUNT))
Ralf Baechled203a7e2005-09-06 15:19:37 -07003170 return -EINVAL;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003171 dprintk("format name = %s\n",
3172 vino_data_formats[index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003173
Hans Verkuilc49cb362009-02-12 10:32:50 -03003174 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3175 fd->index = index;
3176 fd->type = type;
3177 fd->pixelformat = vino_data_formats[index].pixelformat;
3178 strcpy(fd->description, vino_data_formats[index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003179 return 0;
3180}
3181
Hans Verkuilc49cb362009-02-12 10:32:50 -03003182static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003183 struct v4l2_format *f)
3184{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003185 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003186 struct vino_channel_settings tempvcs;
Ladislav Michla637a112005-09-01 15:07:34 +00003187 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003188 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003189
Hans Verkuilc49cb362009-02-12 10:32:50 -03003190 dprintk("requested: w = %d, h = %d\n",
3191 pf->width, pf->height);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003192
Hans Verkuilc49cb362009-02-12 10:32:50 -03003193 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3194 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3195 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003196
Hans Verkuilc49cb362009-02-12 10:32:50 -03003197 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3198 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3199 tempvcs.data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003200 pf->pixelformat =
Hans Verkuilc49cb362009-02-12 10:32:50 -03003201 vino_data_formats[tempvcs.data_format].
3202 pixelformat;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003203 }
3204
Hans Verkuilc49cb362009-02-12 10:32:50 -03003205 /* data format must be set before clipping/scaling */
3206 vino_set_scaling(&tempvcs, pf->width, pf->height);
3207
3208 dprintk("data format = %s\n",
3209 vino_data_formats[tempvcs.data_format].description);
3210
3211 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3212 tempvcs.decimation;
3213 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3214 tempvcs.decimation;
3215
3216 pf->field = V4L2_FIELD_INTERLACED;
3217 pf->bytesperline = tempvcs.line_size;
3218 pf->sizeimage = tempvcs.line_size *
3219 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3220 tempvcs.decimation;
3221 pf->colorspace =
3222 vino_data_formats[tempvcs.data_format].colorspace;
3223
3224 pf->priv = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003225 return 0;
3226}
3227
Hans Verkuilc49cb362009-02-12 10:32:50 -03003228static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003229 struct v4l2_format *f)
3230{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003231 struct vino_channel_settings *vcs = video_drvdata(file);
3232 unsigned long flags;
3233 struct v4l2_pix_format *pf = &f->fmt.pix;
3234
3235 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3236
3237 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3238 vcs->decimation;
3239 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3240 vcs->decimation;
3241 pf->pixelformat =
3242 vino_data_formats[vcs->data_format].pixelformat;
3243
3244 pf->field = V4L2_FIELD_INTERLACED;
3245 pf->bytesperline = vcs->line_size;
3246 pf->sizeimage = vcs->line_size *
3247 (vcs->clipping.bottom - vcs->clipping.top) /
3248 vcs->decimation;
3249 pf->colorspace =
3250 vino_data_formats[vcs->data_format].colorspace;
3251
3252 pf->priv = 0;
3253
3254 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3255 return 0;
3256}
3257
3258static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3259 struct v4l2_format *f)
3260{
3261 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003262 int data_format;
Ladislav Michla637a112005-09-01 15:07:34 +00003263 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003264 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003265
Hans Verkuilc49cb362009-02-12 10:32:50 -03003266 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003267
Hans Verkuilc49cb362009-02-12 10:32:50 -03003268 data_format = vino_find_data_format(pf->pixelformat);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003269
Hans Verkuilc49cb362009-02-12 10:32:50 -03003270 if (data_format == VINO_DATA_FMT_NONE) {
3271 vcs->data_format = VINO_DATA_FMT_GREY;
3272 pf->pixelformat =
3273 vino_data_formats[vcs->data_format].
3274 pixelformat;
3275 } else {
3276 vcs->data_format = data_format;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003277 }
3278
Hans Verkuilc49cb362009-02-12 10:32:50 -03003279 /* data format must be set before clipping/scaling */
3280 vino_set_scaling(vcs, pf->width, pf->height);
3281
3282 dprintk("data format = %s\n",
3283 vino_data_formats[vcs->data_format].description);
3284
3285 pf->width = vcs->clipping.right - vcs->clipping.left;
3286 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3287
3288 pf->field = V4L2_FIELD_INTERLACED;
3289 pf->bytesperline = vcs->line_size;
3290 pf->sizeimage = vcs->line_size *
3291 (vcs->clipping.bottom - vcs->clipping.top) /
3292 vcs->decimation;
3293 pf->colorspace =
3294 vino_data_formats[vcs->data_format].colorspace;
3295
3296 pf->priv = 0;
3297
3298 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003299 return 0;
3300}
3301
Hans Verkuilc49cb362009-02-12 10:32:50 -03003302static int vino_cropcap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003303 struct v4l2_cropcap *ccap)
3304{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003305 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003306 const struct vino_data_norm *norm;
Ladislav Michla637a112005-09-01 15:07:34 +00003307 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003308
3309 switch (ccap->type) {
3310 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003311 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3312
Ralf Baechled203a7e2005-09-06 15:19:37 -07003313 norm = &vino_data_norms[vcs->data_norm];
Ladislav Michla637a112005-09-01 15:07:34 +00003314
3315 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003316
3317 ccap->bounds.left = 0;
3318 ccap->bounds.top = 0;
3319 ccap->bounds.width = norm->width;
3320 ccap->bounds.height = norm->height;
3321 memcpy(&ccap->defrect, &ccap->bounds,
3322 sizeof(struct v4l2_rect));
3323
3324 ccap->pixelaspect.numerator = 1;
3325 ccap->pixelaspect.denominator = 1;
3326 break;
3327 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3328 default:
3329 return -EINVAL;
3330 }
3331
3332 return 0;
3333}
3334
Hans Verkuilc49cb362009-02-12 10:32:50 -03003335static int vino_g_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003336 struct v4l2_crop *c)
3337{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003338 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003339 unsigned long flags;
3340
Ralf Baechled203a7e2005-09-06 15:19:37 -07003341 switch (c->type) {
3342 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003343 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003344
3345 c->c.left = vcs->clipping.left;
3346 c->c.top = vcs->clipping.top;
3347 c->c.width = vcs->clipping.right - vcs->clipping.left;
3348 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3349
Ladislav Michla637a112005-09-01 15:07:34 +00003350 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003351 break;
3352 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3353 default:
3354 return -EINVAL;
3355 }
3356
3357 return 0;
3358}
3359
Hans Verkuilc49cb362009-02-12 10:32:50 -03003360static int vino_s_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003361 struct v4l2_crop *c)
3362{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003363 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003364 unsigned long flags;
3365
Ralf Baechled203a7e2005-09-06 15:19:37 -07003366 switch (c->type) {
3367 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003368 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003369
Ralf Baechled203a7e2005-09-06 15:19:37 -07003370 vino_set_clipping(vcs, c->c.left, c->c.top,
3371 c->c.width, c->c.height);
3372
Ladislav Michla637a112005-09-01 15:07:34 +00003373 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003374 break;
3375 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3376 default:
3377 return -EINVAL;
3378 }
3379
3380 return 0;
3381}
3382
Hans Verkuilc49cb362009-02-12 10:32:50 -03003383static int vino_g_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003384 struct v4l2_streamparm *sp)
3385{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003386 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003387 unsigned long flags;
3388
Ralf Baechled203a7e2005-09-06 15:19:37 -07003389 switch (sp->type) {
3390 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3391 struct v4l2_captureparm *cp = &sp->parm.capture;
3392 memset(cp, 0, sizeof(struct v4l2_captureparm));
3393
3394 cp->capability = V4L2_CAP_TIMEPERFRAME;
3395 cp->timeperframe.numerator = 1;
3396
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 cp->timeperframe.denominator = vcs->fps;
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 // TODO: cp->readbuffers = xxx;
3404 break;
3405 }
3406 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3407 default:
3408 return -EINVAL;
3409 }
3410
3411 return 0;
3412}
3413
Hans Verkuilc49cb362009-02-12 10:32:50 -03003414static int vino_s_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003415 struct v4l2_streamparm *sp)
3416{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003417 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003418 unsigned long flags;
3419
Ralf Baechled203a7e2005-09-06 15:19:37 -07003420 switch (sp->type) {
3421 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3422 struct v4l2_captureparm *cp = &sp->parm.capture;
3423
Ladislav Michla637a112005-09-01 15:07:34 +00003424 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003425
3426 if ((cp->timeperframe.numerator == 0) ||
3427 (cp->timeperframe.denominator == 0)) {
3428 /* reset framerate */
3429 vino_set_default_framerate(vcs);
3430 } else {
3431 vino_set_framerate(vcs, cp->timeperframe.denominator /
3432 cp->timeperframe.numerator);
3433 }
Ladislav Michla637a112005-09-01 15:07:34 +00003434
3435 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003436
3437 // TODO: set buffers according to cp->readbuffers
3438 break;
3439 }
3440 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3441 default:
3442 return -EINVAL;
3443 }
3444
3445 return 0;
3446}
3447
Hans Verkuilc49cb362009-02-12 10:32:50 -03003448static int vino_reqbufs(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003449 struct v4l2_requestbuffers *rb)
3450{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003451 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003452 if (vcs->reading)
3453 return -EBUSY;
3454
3455 switch (rb->type) {
3456 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3457 // TODO: check queue type
3458 if (rb->memory != V4L2_MEMORY_MMAP) {
3459 dprintk("type not mmap\n");
3460 return -EINVAL;
3461 }
3462
Ralf Baechled203a7e2005-09-06 15:19:37 -07003463 dprintk("count = %d\n", rb->count);
3464 if (rb->count > 0) {
Ladislav Michla637a112005-09-01 15:07:34 +00003465 if (vino_is_capturing(vcs)) {
3466 dprintk("busy, capturing\n");
3467 return -EBUSY;
3468 }
3469
Ralf Baechled203a7e2005-09-06 15:19:37 -07003470 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3471 dprintk("busy, buffers still mapped\n");
3472 return -EBUSY;
3473 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003474 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003475 vino_queue_free(&vcs->fb_queue);
3476 vino_queue_init(&vcs->fb_queue, &rb->count);
3477 }
3478 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003479 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003480 vino_capture_stop(vcs);
3481 vino_queue_free(&vcs->fb_queue);
3482 }
3483 break;
3484 }
3485 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3486 default:
3487 return -EINVAL;
3488 }
3489
3490 return 0;
3491}
3492
3493static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3494 struct vino_framebuffer *fb,
3495 struct v4l2_buffer *b)
3496{
3497 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3498 fb->id)) {
3499 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3500 b->flags |= V4L2_BUF_FLAG_DONE;
3501 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3502 fb->id)) {
3503 b->flags &= ~V4L2_BUF_FLAG_DONE;
3504 b->flags |= V4L2_BUF_FLAG_QUEUED;
3505 } else {
3506 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3507 V4L2_BUF_FLAG_QUEUED);
3508 }
3509
3510 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3511
3512 if (fb->map_count > 0)
3513 b->flags |= V4L2_BUF_FLAG_MAPPED;
3514
3515 b->index = fb->id;
3516 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3517 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3518 b->m.offset = fb->offset;
3519 b->bytesused = fb->data_size;
3520 b->length = fb->size;
3521 b->field = V4L2_FIELD_INTERLACED;
3522 b->sequence = fb->frame_counter;
3523 memcpy(&b->timestamp, &fb->timestamp,
3524 sizeof(struct timeval));
3525 // b->input ?
3526
3527 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3528 fb->id, fb->size, fb->data_size, fb->offset);
3529}
3530
Hans Verkuilc49cb362009-02-12 10:32:50 -03003531static int vino_querybuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003532 struct v4l2_buffer *b)
3533{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003534 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003535 if (vcs->reading)
3536 return -EBUSY;
3537
3538 switch (b->type) {
3539 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3540 struct vino_framebuffer *fb;
3541
3542 // TODO: check queue type
3543 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3544 dprintk("invalid index = %d\n",
3545 b->index);
3546 return -EINVAL;
3547 }
3548
3549 fb = vino_queue_get_buffer(&vcs->fb_queue,
3550 b->index);
3551 if (fb == NULL) {
3552 dprintk("vino_queue_get_buffer() failed");
3553 return -EINVAL;
3554 }
3555
3556 vino_v4l2_get_buffer_status(vcs, fb, b);
3557 break;
3558 }
3559 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3560 default:
3561 return -EINVAL;
3562 }
3563
3564 return 0;
3565}
3566
Hans Verkuilc49cb362009-02-12 10:32:50 -03003567static int vino_qbuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003568 struct v4l2_buffer *b)
3569{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003570 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003571 if (vcs->reading)
3572 return -EBUSY;
3573
3574 switch (b->type) {
3575 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3576 struct vino_framebuffer *fb;
3577 int ret;
3578
3579 // TODO: check queue type
3580 if (b->memory != V4L2_MEMORY_MMAP) {
3581 dprintk("type not mmap\n");
3582 return -EINVAL;
3583 }
3584
3585 fb = vino_capture_enqueue(vcs, b->index);
3586 if (fb == NULL)
3587 return -EINVAL;
3588
3589 vino_v4l2_get_buffer_status(vcs, fb, b);
3590
3591 if (vcs->streaming) {
3592 ret = vino_capture_next(vcs, 1);
3593 if (ret)
3594 return ret;
3595 }
3596 break;
3597 }
3598 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3599 default:
3600 return -EINVAL;
3601 }
3602
3603 return 0;
3604}
3605
Hans Verkuilc49cb362009-02-12 10:32:50 -03003606static int vino_dqbuf(struct file *file, void *__fh,
3607 struct v4l2_buffer *b)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003608{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003609 struct vino_channel_settings *vcs = video_drvdata(file);
3610 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003611 if (vcs->reading)
3612 return -EBUSY;
3613
3614 switch (b->type) {
3615 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3616 struct vino_framebuffer *fb;
3617 unsigned int incoming, outgoing;
3618 int err;
3619
3620 // TODO: check queue type
3621
3622 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3623 if (err) {
3624 dprintk("vino_queue_get_incoming() failed\n");
Ladislav Michla637a112005-09-01 15:07:34 +00003625 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003626 }
3627 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3628 if (err) {
3629 dprintk("vino_queue_get_outgoing() failed\n");
Ladislav Michla637a112005-09-01 15:07:34 +00003630 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003631 }
3632
3633 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3634
3635 if (outgoing == 0) {
3636 if (incoming == 0) {
3637 dprintk("no incoming or outgoing buffers\n");
3638 return -EINVAL;
3639 }
3640 if (nonblocking) {
3641 dprintk("non-blocking I/O was selected and "
3642 "there are no buffers to dequeue\n");
3643 return -EAGAIN;
3644 }
3645
3646 err = vino_wait_for_frame(vcs);
3647 if (err) {
3648 err = vino_wait_for_frame(vcs);
3649 if (err) {
Ladislav Michla637a112005-09-01 15:07:34 +00003650 /* interrupted or
3651 * no frames captured because
3652 * of frame skipping */
3653 // vino_capture_failed(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003654 return -EIO;
3655 }
3656 }
3657 }
3658
3659 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3660 if (fb == NULL) {
3661 dprintk("vino_queue_remove() failed\n");
3662 return -EINVAL;
3663 }
3664
3665 err = vino_check_buffer(vcs, fb);
Ladislav Michla637a112005-09-01 15:07:34 +00003666
3667 vino_v4l2_get_buffer_status(vcs, fb, b);
3668
Ralf Baechled203a7e2005-09-06 15:19:37 -07003669 if (err)
3670 return -EIO;
3671
Ralf Baechled203a7e2005-09-06 15:19:37 -07003672 break;
3673 }
3674 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3675 default:
3676 return -EINVAL;
3677 }
3678
3679 return 0;
3680}
3681
Hans Verkuilc49cb362009-02-12 10:32:50 -03003682static int vino_streamon(struct file *file, void *__fh,
3683 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003684{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003685 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003686 unsigned int incoming;
3687 int ret;
3688 if (vcs->reading)
3689 return -EBUSY;
3690
3691 if (vcs->streaming)
3692 return 0;
3693
3694 // TODO: check queue type
3695
3696 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3697 dprintk("no buffers allocated\n");
3698 return -EINVAL;
3699 }
3700
3701 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3702 if (ret) {
3703 dprintk("vino_queue_get_incoming() failed\n");
3704 return -EINVAL;
3705 }
3706
3707 vcs->streaming = 1;
3708
3709 if (incoming > 0) {
3710 ret = vino_capture_next(vcs, 1);
3711 if (ret) {
3712 vcs->streaming = 0;
3713
3714 dprintk("couldn't start capture\n");
3715 return -EINVAL;
3716 }
3717 }
3718
3719 return 0;
3720}
3721
Hans Verkuilc49cb362009-02-12 10:32:50 -03003722static int vino_streamoff(struct file *file, void *__fh,
3723 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003724{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003725 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003726 if (vcs->reading)
3727 return -EBUSY;
3728
3729 if (!vcs->streaming)
3730 return 0;
3731
Ralf Baechled203a7e2005-09-06 15:19:37 -07003732 vcs->streaming = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00003733 vino_capture_stop(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003734
3735 return 0;
3736}
3737
Hans Verkuilc49cb362009-02-12 10:32:50 -03003738static int vino_queryctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003739 struct v4l2_queryctrl *queryctrl)
3740{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003741 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003742 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003743 int i;
3744 int err = 0;
3745
Ladislav Michla637a112005-09-01 15:07:34 +00003746 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003747
3748 switch (vcs->input) {
3749 case VINO_INPUT_D1:
3750 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3751 if (vino_indycam_v4l2_controls[i].id ==
3752 queryctrl->id) {
3753 memcpy(queryctrl,
3754 &vino_indycam_v4l2_controls[i],
3755 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003756 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003757 goto found;
3758 }
3759 }
3760
3761 err = -EINVAL;
3762 break;
3763 case VINO_INPUT_COMPOSITE:
3764 case VINO_INPUT_SVIDEO:
3765 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3766 if (vino_saa7191_v4l2_controls[i].id ==
3767 queryctrl->id) {
3768 memcpy(queryctrl,
3769 &vino_saa7191_v4l2_controls[i],
3770 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003771 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003772 goto found;
3773 }
3774 }
3775
3776 err = -EINVAL;
3777 break;
3778 default:
3779 err = -EINVAL;
3780 }
3781
3782 found:
Ladislav Michla637a112005-09-01 15:07:34 +00003783 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003784
3785 return err;
3786}
3787
Hans Verkuilc49cb362009-02-12 10:32:50 -03003788static int vino_g_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003789 struct v4l2_control *control)
3790{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003791 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003792 unsigned long flags;
3793 int i;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003794 int err = 0;
3795
Ladislav Michla637a112005-09-01 15:07:34 +00003796 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003797
3798 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003799 case VINO_INPUT_D1: {
3800 struct indycam_control indycam_ctrl;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003801
Ladislav Michla637a112005-09-01 15:07:34 +00003802 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3803 if (vino_indycam_v4l2_controls[i].id ==
3804 control->id) {
3805 goto found1;
3806 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003807 }
Ladislav Michla637a112005-09-01 15:07:34 +00003808
3809 err = -EINVAL;
3810 goto out;
3811
3812found1:
3813 indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
3814
3815 err = i2c_camera_command(DECODER_INDYCAM_GET_CONTROL,
3816 &indycam_ctrl);
3817 if (err) {
3818 err = -EINVAL;
3819 goto out;
3820 }
3821
3822 control->value = indycam_ctrl.value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003823 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003824 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003825 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003826 case VINO_INPUT_SVIDEO: {
3827 struct saa7191_control saa7191_ctrl;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003828
Ladislav Michla637a112005-09-01 15:07:34 +00003829 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3830 if (vino_saa7191_v4l2_controls[i].id ==
3831 control->id) {
3832 goto found2;
3833 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003834 }
Ladislav Michla637a112005-09-01 15:07:34 +00003835
3836 err = -EINVAL;
3837 goto out;
3838
3839found2:
3840 saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
3841
3842 err = i2c_decoder_command(DECODER_SAA7191_GET_CONTROL,
3843 &saa7191_ctrl);
3844 if (err) {
3845 err = -EINVAL;
3846 goto out;
3847 }
3848
3849 control->value = saa7191_ctrl.value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003850 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003851 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003852 default:
3853 err = -EINVAL;
3854 }
3855
Ladislav Michla637a112005-09-01 15:07:34 +00003856out:
3857 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003858
3859 return err;
3860}
3861
Hans Verkuilc49cb362009-02-12 10:32:50 -03003862static int vino_s_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003863 struct v4l2_control *control)
3864{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003865 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003866 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003867 int i;
3868 int err = 0;
3869
Ladislav Michla637a112005-09-01 15:07:34 +00003870 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3871
3872 if (!vino_is_input_owner(vcs)) {
3873 err = -EBUSY;
3874 goto out;
3875 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003876
3877 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003878 case VINO_INPUT_D1: {
3879 struct indycam_control indycam_ctrl;
3880
Ralf Baechled203a7e2005-09-06 15:19:37 -07003881 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3882 if (vino_indycam_v4l2_controls[i].id ==
3883 control->id) {
3884 if ((control->value >=
3885 vino_indycam_v4l2_controls[i].minimum)
3886 && (control->value <=
3887 vino_indycam_v4l2_controls[i].
3888 maximum)) {
Ladislav Michla637a112005-09-01 15:07:34 +00003889 goto found1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003890 } else {
3891 err = -ERANGE;
Ladislav Michla637a112005-09-01 15:07:34 +00003892 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003893 }
3894 }
3895 }
Ladislav Michla637a112005-09-01 15:07:34 +00003896
Ralf Baechled203a7e2005-09-06 15:19:37 -07003897 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003898 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003899
Ladislav Michla637a112005-09-01 15:07:34 +00003900found1:
3901 indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
3902 indycam_ctrl.value = control->value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003903
Ladislav Michla637a112005-09-01 15:07:34 +00003904 err = i2c_camera_command(DECODER_INDYCAM_SET_CONTROL,
3905 &indycam_ctrl);
3906 if (err)
3907 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003908 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003909 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003910 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003911 case VINO_INPUT_SVIDEO: {
3912 struct saa7191_control saa7191_ctrl;
3913
Ralf Baechled203a7e2005-09-06 15:19:37 -07003914 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3915 if (vino_saa7191_v4l2_controls[i].id ==
3916 control->id) {
3917 if ((control->value >=
3918 vino_saa7191_v4l2_controls[i].minimum)
3919 && (control->value <=
3920 vino_saa7191_v4l2_controls[i].
3921 maximum)) {
Ladislav Michla637a112005-09-01 15:07:34 +00003922 goto found2;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003923 } else {
3924 err = -ERANGE;
Ladislav Michla637a112005-09-01 15:07:34 +00003925 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003926 }
3927 }
3928 }
3929 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003930 goto out;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003931
Ladislav Michla637a112005-09-01 15:07:34 +00003932found2:
3933 saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
3934 saa7191_ctrl.value = control->value;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003935
Ladislav Michla637a112005-09-01 15:07:34 +00003936 err = i2c_decoder_command(DECODER_SAA7191_SET_CONTROL,
3937 &saa7191_ctrl);
3938 if (err)
3939 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003940 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003941 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003942 default:
3943 err = -EINVAL;
3944 }
3945
Ladislav Michla637a112005-09-01 15:07:34 +00003946out:
3947 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003948
3949 return err;
3950}
3951
3952/* File operations */
3953
Hans Verkuilbec43662008-12-30 06:58:20 -03003954static int vino_open(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003955{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003956 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003957 int ret = 0;
3958 dprintk("open(): channel = %c\n",
3959 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3960
Ingo Molnar3593cab2006-02-07 06:49:14 -02003961 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003962
3963 if (vcs->users) {
3964 dprintk("open(): driver busy\n");
3965 ret = -EBUSY;
3966 goto out;
3967 }
3968
3969 ret = vino_acquire_input(vcs);
3970 if (ret) {
3971 dprintk("open(): vino_acquire_input() failed\n");
3972 goto out;
3973 }
3974
3975 vcs->users++;
3976
3977 out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003978 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003979
3980 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3981
3982 return ret;
3983}
3984
Hans Verkuilbec43662008-12-30 06:58:20 -03003985static int vino_close(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003986{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003987 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003988 dprintk("close():\n");
3989
Ingo Molnar3593cab2006-02-07 06:49:14 -02003990 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003991
3992 vcs->users--;
3993
3994 if (!vcs->users) {
3995 vino_release_input(vcs);
3996
3997 /* stop DMA and free buffers */
3998 vino_capture_stop(vcs);
3999 vino_queue_free(&vcs->fb_queue);
4000 }
4001
Ingo Molnar3593cab2006-02-07 06:49:14 -02004002 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004003
4004 return 0;
4005}
4006
4007static void vino_vm_open(struct vm_area_struct *vma)
4008{
4009 struct vino_framebuffer *fb = vma->vm_private_data;
4010
4011 fb->map_count++;
4012 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
4013}
4014
4015static void vino_vm_close(struct vm_area_struct *vma)
4016{
4017 struct vino_framebuffer *fb = vma->vm_private_data;
4018
4019 fb->map_count--;
4020 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
4021}
4022
4023static struct vm_operations_struct vino_vm_ops = {
4024 .open = vino_vm_open,
4025 .close = vino_vm_close,
4026};
4027
4028static int vino_mmap(struct file *file, struct vm_area_struct *vma)
4029{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004030 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004031
4032 unsigned long start = vma->vm_start;
4033 unsigned long size = vma->vm_end - vma->vm_start;
4034 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4035
4036 struct vino_framebuffer *fb = NULL;
4037 unsigned int i, length;
4038 int ret = 0;
4039
4040 dprintk("mmap():\n");
4041
4042 // TODO: reject mmap if already mapped
4043
Ingo Molnar3593cab2006-02-07 06:49:14 -02004044 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004045 return -EINTR;
4046
4047 if (vcs->reading) {
4048 ret = -EBUSY;
4049 goto out;
4050 }
4051
4052 // TODO: check queue type
4053
4054 if (!(vma->vm_flags & VM_WRITE)) {
4055 dprintk("mmap(): app bug: PROT_WRITE please\n");
4056 ret = -EINVAL;
4057 goto out;
4058 }
4059 if (!(vma->vm_flags & VM_SHARED)) {
4060 dprintk("mmap(): app bug: MAP_SHARED please\n");
4061 ret = -EINVAL;
4062 goto out;
4063 }
4064
4065 /* find the correct buffer using offset */
4066 length = vino_queue_get_length(&vcs->fb_queue);
4067 if (length == 0) {
4068 dprintk("mmap(): queue not initialized\n");
4069 ret = -EINVAL;
4070 goto out;
4071 }
4072
4073 for (i = 0; i < length; i++) {
4074 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
4075 if (fb == NULL) {
4076 dprintk("mmap(): vino_queue_get_buffer() failed\n");
4077 ret = -EINVAL;
4078 goto out;
4079 }
4080
4081 if (fb->offset == offset)
4082 goto found;
4083 }
4084
4085 dprintk("mmap(): invalid offset = %lu\n", offset);
4086 ret = -EINVAL;
4087 goto out;
4088
4089found:
4090 dprintk("mmap(): buffer = %d\n", i);
4091
4092 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
4093 dprintk("mmap(): failed: size = %lu > %lu\n",
4094 size, fb->desc_table.page_count * PAGE_SIZE);
4095 ret = -EINVAL;
4096 goto out;
4097 }
4098
4099 for (i = 0; i < fb->desc_table.page_count; i++) {
4100 unsigned long pfn =
4101 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
4102 PAGE_SHIFT;
4103
4104 if (size < PAGE_SIZE)
4105 break;
4106
4107 // protection was: PAGE_READONLY
4108 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
4109 vma->vm_page_prot)) {
4110 dprintk("mmap(): remap_pfn_range() failed\n");
4111 ret = -EAGAIN;
4112 goto out;
4113 }
4114
4115 start += PAGE_SIZE;
4116 size -= PAGE_SIZE;
4117 }
4118
4119 fb->map_count = 1;
4120
4121 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
4122 vma->vm_flags &= ~VM_IO;
4123 vma->vm_private_data = fb;
4124 vma->vm_file = file;
4125 vma->vm_ops = &vino_vm_ops;
4126
4127out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02004128 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004129
4130 return ret;
4131}
4132
4133static unsigned int vino_poll(struct file *file, poll_table *pt)
4134{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004135 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004136 unsigned int outgoing;
4137 unsigned int ret = 0;
4138
4139 // lock mutex (?)
4140 // TODO: this has to be corrected for different read modes
4141
4142 dprintk("poll():\n");
4143
4144 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4145 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4146 ret = POLLERR;
4147 goto error;
4148 }
4149 if (outgoing > 0)
4150 goto over;
4151
4152 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
4153
4154 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4155 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4156 ret = POLLERR;
4157 goto error;
4158 }
4159
4160over:
4161 dprintk("poll(): data %savailable\n",
4162 (outgoing > 0) ? "" : "not ");
Ladislav Michla637a112005-09-01 15:07:34 +00004163
4164 if (outgoing > 0)
Ralf Baechled203a7e2005-09-06 15:19:37 -07004165 ret = POLLIN | POLLRDNORM;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004166
4167error:
4168
4169 return ret;
4170}
4171
Hans Verkuil069b7472008-12-30 07:04:34 -03004172static long vino_ioctl(struct file *file,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004173 unsigned int cmd, unsigned long arg)
4174{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004175 struct vino_channel_settings *vcs = video_drvdata(file);
Hans Verkuil069b7472008-12-30 07:04:34 -03004176 long ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004177
Ingo Molnar3593cab2006-02-07 06:49:14 -02004178 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004179 return -EINTR;
4180
Hans Verkuilc49cb362009-02-12 10:32:50 -03004181 ret = video_ioctl2(file, cmd, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004182
Ingo Molnar3593cab2006-02-07 06:49:14 -02004183 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004184
4185 return ret;
4186}
4187
4188/* Initialization and cleanup */
4189
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03004190/* __initdata */
4191static int vino_init_stage;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004192
Hans Verkuilc49cb362009-02-12 10:32:50 -03004193const struct v4l2_ioctl_ops vino_ioctl_ops = {
4194 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4195 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4196 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4197 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4198 .vidioc_querycap = vino_querycap,
4199 .vidioc_enum_input = vino_enum_input,
4200 .vidioc_g_input = vino_g_input,
4201 .vidioc_s_input = vino_s_input,
4202 .vidioc_g_std = vino_g_std,
4203 .vidioc_s_std = vino_s_std,
4204 .vidioc_querystd = vino_querystd,
4205 .vidioc_cropcap = vino_cropcap,
4206 .vidioc_s_crop = vino_s_crop,
4207 .vidioc_g_crop = vino_g_crop,
4208 .vidioc_s_parm = vino_s_parm,
4209 .vidioc_g_parm = vino_g_parm,
4210 .vidioc_reqbufs = vino_reqbufs,
4211 .vidioc_querybuf = vino_querybuf,
4212 .vidioc_qbuf = vino_qbuf,
4213 .vidioc_dqbuf = vino_dqbuf,
4214 .vidioc_streamon = vino_streamon,
4215 .vidioc_streamoff = vino_streamoff,
4216 .vidioc_queryctrl = vino_queryctrl,
4217 .vidioc_g_ctrl = vino_g_ctrl,
4218 .vidioc_s_ctrl = vino_s_ctrl,
4219};
4220
Hans Verkuilbec43662008-12-30 06:58:20 -03004221static const struct v4l2_file_operations vino_fops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004222 .owner = THIS_MODULE,
4223 .open = vino_open,
4224 .release = vino_close,
4225 .ioctl = vino_ioctl,
4226 .mmap = vino_mmap,
4227 .poll = vino_poll,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004228};
4229
4230static struct video_device v4l_device_template = {
4231 .name = "NOT SET",
Ralf Baechled203a7e2005-09-06 15:19:37 -07004232 .fops = &vino_fops,
Hans Verkuilc49cb362009-02-12 10:32:50 -03004233 .ioctl_ops = &vino_ioctl_ops,
4234 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004235 .minor = -1,
4236};
4237
4238static void vino_module_cleanup(int stage)
4239{
4240 switch(stage) {
4241 case 10:
4242 video_unregister_device(vino_drvdata->b.v4l_device);
4243 vino_drvdata->b.v4l_device = NULL;
4244 case 9:
4245 video_unregister_device(vino_drvdata->a.v4l_device);
4246 vino_drvdata->a.v4l_device = NULL;
4247 case 8:
4248 vino_i2c_del_bus();
4249 case 7:
4250 free_irq(SGI_VINO_IRQ, NULL);
4251 case 6:
4252 if (vino_drvdata->b.v4l_device) {
4253 video_device_release(vino_drvdata->b.v4l_device);
4254 vino_drvdata->b.v4l_device = NULL;
4255 }
4256 case 5:
4257 if (vino_drvdata->a.v4l_device) {
4258 video_device_release(vino_drvdata->a.v4l_device);
4259 vino_drvdata->a.v4l_device = NULL;
4260 }
4261 case 4:
4262 /* all entries in dma_cpu dummy table have the same address */
4263 dma_unmap_single(NULL,
4264 vino_drvdata->dummy_desc_table.dma_cpu[0],
4265 PAGE_SIZE, DMA_FROM_DEVICE);
4266 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4267 * sizeof(dma_addr_t),
4268 (void *)vino_drvdata->
4269 dummy_desc_table.dma_cpu,
4270 vino_drvdata->dummy_desc_table.dma);
4271 case 3:
4272 free_page(vino_drvdata->dummy_page);
4273 case 2:
4274 kfree(vino_drvdata);
4275 case 1:
4276 iounmap(vino);
4277 case 0:
4278 break;
4279 default:
4280 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4281 stage);
4282 }
4283}
4284
4285static int vino_probe(void)
4286{
4287 unsigned long rev_id;
4288
4289 if (ip22_is_fullhouse()) {
4290 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4291 return -ENODEV;
4292 }
4293
4294 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4295 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4296 return -ENODEV;
4297 }
4298
4299 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4300 if (!vino) {
4301 printk(KERN_ERR "VINO: ioremap() failed\n");
4302 return -EIO;
4303 }
4304 vino_init_stage++;
4305
4306 if (get_dbe(rev_id, &(vino->rev_id))) {
4307 printk(KERN_ERR "Failed to read VINO revision register\n");
4308 vino_module_cleanup(vino_init_stage);
4309 return -ENODEV;
4310 }
4311
4312 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4313 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4314 rev_id);
4315 vino_module_cleanup(vino_init_stage);
4316 return -ENODEV;
4317 }
4318
Ladislav Michla637a112005-09-01 15:07:34 +00004319 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004320
4321 return 0;
4322}
4323
4324static int vino_init(void)
4325{
4326 dma_addr_t dma_dummy_address;
4327 int i;
4328
Panagiotis Issaris74081872006-01-11 19:40:56 -02004329 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004330 if (!vino_drvdata) {
4331 vino_module_cleanup(vino_init_stage);
4332 return -ENOMEM;
4333 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004334 vino_init_stage++;
4335
4336 /* create a dummy dma descriptor */
4337 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4338 if (!vino_drvdata->dummy_page) {
4339 vino_module_cleanup(vino_init_stage);
4340 return -ENOMEM;
4341 }
4342 vino_init_stage++;
4343
4344 // TODO: use page_count in dummy_desc_table
4345
4346 vino_drvdata->dummy_desc_table.dma_cpu =
4347 dma_alloc_coherent(NULL,
4348 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4349 &vino_drvdata->dummy_desc_table.dma,
4350 GFP_KERNEL | GFP_DMA);
4351 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4352 vino_module_cleanup(vino_init_stage);
4353 return -ENOMEM;
4354 }
4355 vino_init_stage++;
4356
4357 dma_dummy_address = dma_map_single(NULL,
4358 (void *)vino_drvdata->dummy_page,
4359 PAGE_SIZE, DMA_FROM_DEVICE);
4360 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4361 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4362 }
4363
4364 /* initialize VINO */
4365
4366 vino->control = 0;
4367 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4368 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4369 udelay(VINO_DESC_FETCH_DELAY);
4370
4371 vino->intr_status = 0;
4372
4373 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4374 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4375
4376 return 0;
4377}
4378
4379static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4380 unsigned int channel, const char *name)
4381{
4382 vcs->channel = channel;
4383 vcs->input = VINO_INPUT_NONE;
4384 vcs->alpha = 0;
4385 vcs->users = 0;
4386 vcs->data_format = VINO_DATA_FMT_GREY;
4387 vcs->data_norm = VINO_DATA_NORM_NTSC;
4388 vcs->decimation = 1;
4389 vino_set_default_clipping(vcs);
4390 vino_set_default_framerate(vcs);
4391
4392 vcs->capturing = 0;
4393
Ingo Molnar3593cab2006-02-07 06:49:14 -02004394 mutex_init(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004395 spin_lock_init(&vcs->capture_lock);
4396
Ingo Molnar3593cab2006-02-07 06:49:14 -02004397 mutex_init(&vcs->fb_queue.queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004398 spin_lock_init(&vcs->fb_queue.queue_lock);
4399 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4400
4401 vcs->v4l_device = video_device_alloc();
4402 if (!vcs->v4l_device) {
4403 vino_module_cleanup(vino_init_stage);
4404 return -ENOMEM;
4405 }
4406 vino_init_stage++;
4407
4408 memcpy(vcs->v4l_device, &v4l_device_template,
4409 sizeof(struct video_device));
4410 strcpy(vcs->v4l_device->name, name);
4411 vcs->v4l_device->release = video_device_release;
4412
4413 video_set_drvdata(vcs->v4l_device, vcs);
4414
4415 return 0;
4416}
4417
4418static int __init vino_module_init(void)
4419{
4420 int ret;
4421
4422 printk(KERN_INFO "SGI VINO driver version %s\n",
4423 VINO_MODULE_VERSION);
4424
4425 ret = vino_probe();
4426 if (ret)
4427 return ret;
4428
4429 ret = vino_init();
4430 if (ret)
4431 return ret;
4432
4433 /* initialize data structures */
4434
4435 spin_lock_init(&vino_drvdata->vino_lock);
4436 spin_lock_init(&vino_drvdata->input_lock);
4437
4438 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4439 vino_v4l_device_name_a);
4440 if (ret)
4441 return ret;
4442
4443 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4444 vino_v4l_device_name_b);
4445 if (ret)
4446 return ret;
4447
4448 /* initialize hardware and register V4L devices */
4449
4450 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4451 vino_driver_description, NULL);
4452 if (ret) {
4453 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4454 SGI_VINO_IRQ);
4455 vino_module_cleanup(vino_init_stage);
4456 return -EAGAIN;
4457 }
4458 vino_init_stage++;
4459
4460 ret = vino_i2c_add_bus();
4461 if (ret) {
4462 printk(KERN_ERR "VINO I2C bus registration failed\n");
4463 vino_module_cleanup(vino_init_stage);
4464 return ret;
4465 }
4466 vino_init_stage++;
4467
4468 ret = video_register_device(vino_drvdata->a.v4l_device,
4469 VFL_TYPE_GRABBER, -1);
4470 if (ret < 0) {
4471 printk(KERN_ERR "VINO channel A Video4Linux-device "
4472 "registration failed\n");
4473 vino_module_cleanup(vino_init_stage);
4474 return -EINVAL;
4475 }
4476 vino_init_stage++;
4477
4478 ret = video_register_device(vino_drvdata->b.v4l_device,
4479 VFL_TYPE_GRABBER, -1);
4480 if (ret < 0) {
4481 printk(KERN_ERR "VINO channel B Video4Linux-device "
4482 "registration failed\n");
4483 vino_module_cleanup(vino_init_stage);
4484 return -EINVAL;
4485 }
4486 vino_init_stage++;
4487
Johannes Berga65e5d72008-07-09 10:28:38 +02004488#ifdef MODULE
Ralf Baechled203a7e2005-09-06 15:19:37 -07004489 request_module("saa7191");
4490 request_module("indycam");
4491#endif
4492
4493 dprintk("init complete!\n");
4494
4495 return 0;
4496}
4497
4498static void __exit vino_module_exit(void)
4499{
4500 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4501 vino_module_cleanup(vino_init_stage);
4502 dprintk("cleanup complete, exit!\n");
4503}
4504
4505module_init(vino_module_init);
4506module_exit(vino_module_exit);