blob: d63e9d97849339c069e3820298384b1c49be9cb8 [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>
Hans Verkuila99e30d2009-03-06 12:15:08 -030011 *
12 * v4l2_device/v4l2_subdev conversion by:
13 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
14 *
15 * Note: this conversion is untested! Please contact the linux-media
16 * mailinglist if you can test this, together with the test results.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Ralf Baechled203a7e2005-09-06 15:19:37 -070019/*
20 * TODO:
Ladislav Michla637a112005-09-01 15:07:34 +000021 * - remove "mark pages reserved-hacks" from memory allocation code
Nick Piggin3c18ddd2008-04-28 02:12:10 -070022 * and implement fault()
Ralf Baechled203a7e2005-09-06 15:19:37 -070023 * - check decimation, calculating and reporting image size when
24 * using decimation
Ladislav Michla637a112005-09-01 15:07:34 +000025 * - implement read(), user mode buffers and overlay (?)
Ralf Baechled203a7e2005-09-06 15:19:37 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000031#include <linux/dma-mapping.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070032#include <linux/errno.h>
33#include <linux/fs.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000034#include <linux/interrupt.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070035#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070037#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000038#include <linux/time.h>
39#include <linux/version.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070040#include <linux/kmod.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Hans Verkuil33b687c2008-07-25 05:32:50 -030044#include <linux/videodev2.h>
Hans Verkuil28959632009-02-18 18:53:47 -030045#include <media/v4l2-device.h>
Yoichi Yuasac2cfcf72008-07-29 05:30:58 -030046#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020047#include <linux/mutex.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/paccess.h>
50#include <asm/io.h>
51#include <asm/sgi/ip22.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/sgi/mc.h>
53
54#include "vino.h"
Ralf Baechled203a7e2005-09-06 15:19:37 -070055#include "saa7191.h"
56#include "indycam.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Ralf Baechled203a7e2005-09-06 15:19:37 -070058/* Uncomment the following line to get lots and lots of (mostly useless)
59 * debug info.
60 * Note that the debug output also slows down the driver significantly */
61// #define VINO_DEBUG
Ladislav Michla637a112005-09-01 15:07:34 +000062// #define VINO_DEBUG_INT
Ralf Baechled203a7e2005-09-06 15:19:37 -070063
Hans Verkuil3ff4ad82009-04-01 03:15:52 -030064#define VINO_MODULE_VERSION "0.0.6"
65#define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 6)
Ralf Baechled203a7e2005-09-06 15:19:37 -070066
67MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
68MODULE_VERSION(VINO_MODULE_VERSION);
69MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
70MODULE_LICENSE("GPL");
71
Ralf Baechled203a7e2005-09-06 15:19:37 -070072#ifdef VINO_DEBUG
73#define dprintk(x...) printk("VINO: " x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#else
Ralf Baechled203a7e2005-09-06 15:19:37 -070075#define dprintk(x...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77
Ralf Baechled203a7e2005-09-06 15:19:37 -070078#define VINO_NO_CHANNEL 0
79#define VINO_CHANNEL_A 1
80#define VINO_CHANNEL_B 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ralf Baechled203a7e2005-09-06 15:19:37 -070082#define VINO_PAL_WIDTH 768
83#define VINO_PAL_HEIGHT 576
84#define VINO_NTSC_WIDTH 640
85#define VINO_NTSC_HEIGHT 480
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Ralf Baechled203a7e2005-09-06 15:19:37 -070087#define VINO_MIN_WIDTH 32
88#define VINO_MIN_HEIGHT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Ralf Baechled203a7e2005-09-06 15:19:37 -070090#define VINO_CLIPPING_START_ODD_D1 1
Ladislav Michla637a112005-09-01 15:07:34 +000091#define VINO_CLIPPING_START_ODD_PAL 15
92#define VINO_CLIPPING_START_ODD_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070093
94#define VINO_CLIPPING_START_EVEN_D1 2
Ladislav Michla637a112005-09-01 15:07:34 +000095#define VINO_CLIPPING_START_EVEN_PAL 15
96#define VINO_CLIPPING_START_EVEN_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070097
98#define VINO_INPUT_CHANNEL_COUNT 3
99
Ladislav Michla637a112005-09-01 15:07:34 +0000100/* the number is the index for vino_inputs */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700101#define VINO_INPUT_NONE -1
102#define VINO_INPUT_COMPOSITE 0
103#define VINO_INPUT_SVIDEO 1
104#define VINO_INPUT_D1 2
105
106#define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
107
Ladislav Michla637a112005-09-01 15:07:34 +0000108#define VINO_FIFO_THRESHOLD_DEFAULT 16
Ralf Baechled203a7e2005-09-06 15:19:37 -0700109
Ralf Baechled203a7e2005-09-06 15:19:37 -0700110#define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
111 * VINO_PAL_HEIGHT * 4 \
112 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
113
Ladislav Michla637a112005-09-01 15:07:34 +0000114#define VINO_FRAMEBUFFER_COUNT_MAX 8
Ralf Baechled203a7e2005-09-06 15:19:37 -0700115
116#define VINO_FRAMEBUFFER_UNUSED 0
117#define VINO_FRAMEBUFFER_IN_USE 1
118#define VINO_FRAMEBUFFER_READY 2
119
120#define VINO_QUEUE_ERROR -1
121#define VINO_QUEUE_MAGIC 0x20050125
122
123#define VINO_MEMORY_NONE 0
124#define VINO_MEMORY_MMAP 1
125#define VINO_MEMORY_USERPTR 2
126
127#define VINO_DUMMY_DESC_COUNT 4
128#define VINO_DESC_FETCH_DELAY 5 /* microseconds */
129
Ladislav Michla637a112005-09-01 15:07:34 +0000130#define VINO_MAX_FRAME_SKIP_COUNT 128
131
Ralf Baechled203a7e2005-09-06 15:19:37 -0700132/* the number is the index for vino_data_formats */
133#define VINO_DATA_FMT_NONE -1
134#define VINO_DATA_FMT_GREY 0
135#define VINO_DATA_FMT_RGB332 1
136#define VINO_DATA_FMT_RGB32 2
137#define VINO_DATA_FMT_YUV 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700138
139#define VINO_DATA_FMT_COUNT 4
140
Ladislav Michla637a112005-09-01 15:07:34 +0000141/* the number is the index for vino_data_norms */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700142#define VINO_DATA_NORM_NONE -1
143#define VINO_DATA_NORM_NTSC 0
144#define VINO_DATA_NORM_PAL 1
145#define VINO_DATA_NORM_SECAM 2
146#define VINO_DATA_NORM_D1 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700147
148#define VINO_DATA_NORM_COUNT 4
149
Jean Delvarecea0c682009-03-06 12:05:43 -0300150/* I2C controller flags */
151#define SGI_I2C_FORCE_IDLE (0 << 0)
152#define SGI_I2C_NOT_IDLE (1 << 0)
153#define SGI_I2C_WRITE (0 << 1)
154#define SGI_I2C_READ (1 << 1)
155#define SGI_I2C_RELEASE_BUS (0 << 2)
156#define SGI_I2C_HOLD_BUS (1 << 2)
157#define SGI_I2C_XFER_DONE (0 << 4)
158#define SGI_I2C_XFER_BUSY (1 << 4)
159#define SGI_I2C_ACK (0 << 5)
160#define SGI_I2C_NACK (1 << 5)
161#define SGI_I2C_BUS_OK (0 << 7)
162#define SGI_I2C_BUS_ERR (1 << 7)
163
Ralf Baechled203a7e2005-09-06 15:19:37 -0700164/* Internal data structure definitions */
165
166struct vino_input {
167 char *name;
168 v4l2_std_id std;
169};
170
171struct vino_clipping {
172 unsigned int left, right, top, bottom;
173};
174
175struct vino_data_format {
176 /* the description */
177 char *description;
178 /* bytes per pixel */
179 unsigned int bpp;
180 /* V4L2 fourcc code */
181 __u32 pixelformat;
182 /* V4L2 colorspace (duh!) */
183 enum v4l2_colorspace colorspace;
184};
185
186struct vino_data_norm {
187 char *description;
188 unsigned int width, height;
189 struct vino_clipping odd;
190 struct vino_clipping even;
191
192 v4l2_std_id std;
193 unsigned int fps_min, fps_max;
194 __u32 framelines;
195};
196
197struct vino_descriptor_table {
198 /* the number of PAGE_SIZE sized pages in the buffer */
199 unsigned int page_count;
200 /* virtual (kmalloc'd) pointers to the actual data
201 * (in PAGE_SIZE chunks, used with mmap streaming) */
202 unsigned long *virtual;
203
204 /* cpu address for the VINO descriptor table
205 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
206 unsigned long *dma_cpu;
207 /* dma address for the VINO descriptor table
208 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
209 dma_addr_t dma;
210};
211
212struct vino_framebuffer {
213 /* identifier nubmer */
214 unsigned int id;
215 /* the length of the whole buffer */
216 unsigned int size;
217 /* the length of actual data in buffer */
218 unsigned int data_size;
219 /* the data format */
220 unsigned int data_format;
221 /* the state of buffer data */
222 unsigned int state;
223 /* is the buffer mapped in user space? */
224 unsigned int map_count;
225 /* memory offset for mmap() */
226 unsigned int offset;
227 /* frame counter */
228 unsigned int frame_counter;
229 /* timestamp (written when image capture finishes) */
230 struct timeval timestamp;
231
232 struct vino_descriptor_table desc_table;
233
234 spinlock_t state_lock;
235};
236
237struct vino_framebuffer_fifo {
238 unsigned int length;
239
240 unsigned int used;
241 unsigned int head;
242 unsigned int tail;
243
Ladislav Michla637a112005-09-01 15:07:34 +0000244 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700245};
246
247struct vino_framebuffer_queue {
248 unsigned int magic;
249
250 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
251 unsigned int type;
252 unsigned int length;
253
254 /* data field of in and out contain index numbers for buffer */
255 struct vino_framebuffer_fifo in;
256 struct vino_framebuffer_fifo out;
257
Ladislav Michla637a112005-09-01 15:07:34 +0000258 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700259
260 spinlock_t queue_lock;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200261 struct mutex queue_mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700262 wait_queue_head_t frame_wait_queue;
263};
264
Ladislav Michla637a112005-09-01 15:07:34 +0000265struct vino_interrupt_data {
266 struct timeval timestamp;
267 unsigned int frame_counter;
268 unsigned int skip_count;
269 unsigned int skip;
270};
271
Ralf Baechled203a7e2005-09-06 15:19:37 -0700272struct vino_channel_settings {
273 unsigned int channel;
274
275 int input;
276 unsigned int data_format;
277 unsigned int data_norm;
278 struct vino_clipping clipping;
279 unsigned int decimation;
280 unsigned int line_size;
281 unsigned int alpha;
282 unsigned int fps;
283 unsigned int framert_reg;
284
285 unsigned int fifo_threshold;
286
287 struct vino_framebuffer_queue fb_queue;
288
289 /* number of the current field */
290 unsigned int field;
291
292 /* read in progress */
293 int reading;
294 /* streaming is active */
295 int streaming;
296 /* the driver is currently processing the queue */
297 int capturing;
298
Ingo Molnar3593cab2006-02-07 06:49:14 -0200299 struct mutex mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700300 spinlock_t capture_lock;
301
302 unsigned int users;
303
Ladislav Michla637a112005-09-01 15:07:34 +0000304 struct vino_interrupt_data int_data;
305
Ralf Baechled203a7e2005-09-06 15:19:37 -0700306 /* V4L support */
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300307 struct video_device *vdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
Ralf Baechled203a7e2005-09-06 15:19:37 -0700310struct vino_settings {
Hans Verkuil28959632009-02-18 18:53:47 -0300311 struct v4l2_device v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700312 struct vino_channel_settings a;
313 struct vino_channel_settings b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300315 /* the channel which owns this client:
316 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
317 unsigned int decoder_owner;
318 struct v4l2_subdev *decoder;
319 unsigned int camera_owner;
320 struct v4l2_subdev *camera;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ralf Baechled203a7e2005-09-06 15:19:37 -0700322 /* a lock for vino register access */
323 spinlock_t vino_lock;
324 /* a lock for channel input changes */
325 spinlock_t input_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned long dummy_page;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700328 struct vino_descriptor_table dummy_desc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
Ralf Baechled203a7e2005-09-06 15:19:37 -0700331/* Module parameters */
332
333/*
Ladislav Michla637a112005-09-01 15:07:34 +0000334 * Using vino_pixel_conversion the ABGR32-format pixels supplied
Ralf Baechled203a7e2005-09-06 15:19:37 -0700335 * by the VINO chip can be converted to more common formats
336 * like RGBA32 (or probably RGB24 in the future). This way we
337 * can give out data that can be specified correctly with
338 * the V4L2-definitions.
339 *
340 * The pixel format is specified as RGBA32 when no conversion
341 * is used.
342 *
343 * Note that this only affects the 32-bit bit depth.
344 *
345 * Use non-zero value to enable conversion.
346 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300347static int vino_pixel_conversion;
Ladislav Michla637a112005-09-01 15:07:34 +0000348
Ralf Baechled203a7e2005-09-06 15:19:37 -0700349module_param_named(pixelconv, vino_pixel_conversion, int, 0);
Ladislav Michla637a112005-09-01 15:07:34 +0000350
Ralf Baechled203a7e2005-09-06 15:19:37 -0700351MODULE_PARM_DESC(pixelconv,
352 "enable pixel conversion (non-zero value enables)");
353
354/* Internal data structures */
355
356static struct sgi_vino *vino;
357
358static struct vino_settings *vino_drvdata;
359
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300360#define camera_call(o, f, args...) \
361 v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
362#define decoder_call(o, f, args...) \
363 v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
364
Ralf Baechled203a7e2005-09-06 15:19:37 -0700365static const char *vino_driver_name = "vino";
366static const char *vino_driver_description = "SGI VINO";
367static const char *vino_bus_name = "GIO64 bus";
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300368static const char *vino_vdev_name_a = "SGI VINO Channel A";
369static const char *vino_vdev_name_b = "SGI VINO Channel B";
Ralf Baechled203a7e2005-09-06 15:19:37 -0700370
Ladislav Michla637a112005-09-01 15:07:34 +0000371static void vino_capture_tasklet(unsigned long channel);
372
373DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
374DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
375
Ralf Baechled203a7e2005-09-06 15:19:37 -0700376static const struct vino_input vino_inputs[] = {
377 {
378 .name = "Composite",
Ladislav Michla637a112005-09-01 15:07:34 +0000379 .std = V4L2_STD_NTSC | V4L2_STD_PAL
380 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300381 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700382 .name = "S-Video",
Ladislav Michla637a112005-09-01 15:07:34 +0000383 .std = V4L2_STD_NTSC | V4L2_STD_PAL
384 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300385 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000386 .name = "D1/IndyCam",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700387 .std = V4L2_STD_NTSC,
388 }
389};
390
391static const struct vino_data_format vino_data_formats[] = {
392 {
393 .description = "8-bit greyscale",
394 .bpp = 1,
395 .pixelformat = V4L2_PIX_FMT_GREY,
396 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300397 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700398 .description = "8-bit dithered RGB 3-3-2",
399 .bpp = 1,
400 .pixelformat = V4L2_PIX_FMT_RGB332,
401 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300402 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700403 .description = "32-bit RGB",
404 .bpp = 4,
405 .pixelformat = V4L2_PIX_FMT_RGB32,
406 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300407 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700408 .description = "YUV 4:2:2",
Ladislav Michla637a112005-09-01 15:07:34 +0000409 .bpp = 2,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700410 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
411 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Ladislav Michla637a112005-09-01 15:07:34 +0000412 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700413};
414
415static const struct vino_data_norm vino_data_norms[] = {
416 {
417 .description = "NTSC",
418 .std = V4L2_STD_NTSC,
419 .fps_min = 6,
420 .fps_max = 30,
421 .framelines = 525,
422 .width = VINO_NTSC_WIDTH,
423 .height = VINO_NTSC_HEIGHT,
424 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000425 .top = VINO_CLIPPING_START_ODD_NTSC,
426 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700427 .bottom = VINO_CLIPPING_START_ODD_NTSC
428 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000429 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700430 },
431 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000432 .top = VINO_CLIPPING_START_EVEN_NTSC,
433 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700434 .bottom = VINO_CLIPPING_START_EVEN_NTSC
435 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000436 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700437 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300438 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700439 .description = "PAL",
440 .std = V4L2_STD_PAL,
441 .fps_min = 5,
442 .fps_max = 25,
443 .framelines = 625,
444 .width = VINO_PAL_WIDTH,
445 .height = VINO_PAL_HEIGHT,
446 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000447 .top = VINO_CLIPPING_START_ODD_PAL,
448 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700449 .bottom = VINO_CLIPPING_START_ODD_PAL
450 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000451 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700452 },
453 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000454 .top = VINO_CLIPPING_START_EVEN_PAL,
455 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700456 .bottom = VINO_CLIPPING_START_EVEN_PAL
457 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000458 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700459 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300460 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700461 .description = "SECAM",
462 .std = V4L2_STD_SECAM,
463 .fps_min = 5,
464 .fps_max = 25,
465 .framelines = 625,
466 .width = VINO_PAL_WIDTH,
467 .height = VINO_PAL_HEIGHT,
468 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000469 .top = VINO_CLIPPING_START_ODD_PAL,
470 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700471 .bottom = VINO_CLIPPING_START_ODD_PAL
472 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000473 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700474 },
475 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000476 .top = VINO_CLIPPING_START_EVEN_PAL,
477 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700478 .bottom = VINO_CLIPPING_START_EVEN_PAL
479 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000480 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700481 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300482 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000483 .description = "NTSC/D1",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700484 .std = V4L2_STD_NTSC,
485 .fps_min = 6,
486 .fps_max = 30,
487 .framelines = 525,
488 .width = VINO_NTSC_WIDTH,
489 .height = VINO_NTSC_HEIGHT,
490 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000491 .top = VINO_CLIPPING_START_ODD_D1,
492 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700493 .bottom = VINO_CLIPPING_START_ODD_D1
494 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000495 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700496 },
497 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000498 .top = VINO_CLIPPING_START_EVEN_D1,
499 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700500 .bottom = VINO_CLIPPING_START_EVEN_D1
501 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000502 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700503 },
504 }
505};
506
507#define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
508
509struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
510 {
511 .id = V4L2_CID_AUTOGAIN,
512 .type = V4L2_CTRL_TYPE_BOOLEAN,
513 .name = "Automatic Gain Control",
514 .minimum = 0,
515 .maximum = 1,
516 .step = 1,
517 .default_value = INDYCAM_AGC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300518 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700519 .id = V4L2_CID_AUTO_WHITE_BALANCE,
520 .type = V4L2_CTRL_TYPE_BOOLEAN,
521 .name = "Automatic White Balance",
522 .minimum = 0,
523 .maximum = 1,
524 .step = 1,
525 .default_value = INDYCAM_AWB_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300526 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700527 .id = V4L2_CID_GAIN,
528 .type = V4L2_CTRL_TYPE_INTEGER,
529 .name = "Gain",
530 .minimum = INDYCAM_GAIN_MIN,
531 .maximum = INDYCAM_GAIN_MAX,
532 .step = 1,
533 .default_value = INDYCAM_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300534 }, {
535 .id = INDYCAM_CONTROL_RED_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700536 .type = V4L2_CTRL_TYPE_INTEGER,
537 .name = "Red Saturation",
538 .minimum = INDYCAM_RED_SATURATION_MIN,
539 .maximum = INDYCAM_RED_SATURATION_MAX,
540 .step = 1,
541 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300542 }, {
543 .id = INDYCAM_CONTROL_BLUE_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700544 .type = V4L2_CTRL_TYPE_INTEGER,
545 .name = "Blue Saturation",
546 .minimum = INDYCAM_BLUE_SATURATION_MIN,
547 .maximum = INDYCAM_BLUE_SATURATION_MAX,
548 .step = 1,
549 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300550 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700551 .id = V4L2_CID_RED_BALANCE,
552 .type = V4L2_CTRL_TYPE_INTEGER,
553 .name = "Red Balance",
554 .minimum = INDYCAM_RED_BALANCE_MIN,
555 .maximum = INDYCAM_RED_BALANCE_MAX,
556 .step = 1,
557 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300558 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700559 .id = V4L2_CID_BLUE_BALANCE,
560 .type = V4L2_CTRL_TYPE_INTEGER,
561 .name = "Blue Balance",
562 .minimum = INDYCAM_BLUE_BALANCE_MIN,
563 .maximum = INDYCAM_BLUE_BALANCE_MAX,
564 .step = 1,
565 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300566 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700567 .id = V4L2_CID_EXPOSURE,
568 .type = V4L2_CTRL_TYPE_INTEGER,
569 .name = "Shutter Control",
570 .minimum = INDYCAM_SHUTTER_MIN,
571 .maximum = INDYCAM_SHUTTER_MAX,
572 .step = 1,
573 .default_value = INDYCAM_SHUTTER_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300574 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700575 .id = V4L2_CID_GAMMA,
576 .type = V4L2_CTRL_TYPE_INTEGER,
577 .name = "Gamma",
578 .minimum = INDYCAM_GAMMA_MIN,
579 .maximum = INDYCAM_GAMMA_MAX,
580 .step = 1,
581 .default_value = INDYCAM_GAMMA_DEFAULT,
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,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300596 }, {
597 .id = SAA7191_CONTROL_BANDPASS,
Ladislav Michla637a112005-09-01 15:07:34 +0000598 .type = V4L2_CTRL_TYPE_INTEGER,
599 .name = "Luminance Bandpass",
600 .minimum = SAA7191_BANDPASS_MIN,
601 .maximum = SAA7191_BANDPASS_MAX,
602 .step = 1,
603 .default_value = SAA7191_BANDPASS_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300604 }, {
605 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
Ladislav Michla637a112005-09-01 15:07:34 +0000606 .type = V4L2_CTRL_TYPE_INTEGER,
607 .name = "Luminance Bandpass Weight",
608 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
609 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
610 .step = 1,
611 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300612 }, {
613 .id = SAA7191_CONTROL_CORING,
Ladislav Michla637a112005-09-01 15:07:34 +0000614 .type = V4L2_CTRL_TYPE_INTEGER,
615 .name = "HF Luminance Coring",
616 .minimum = SAA7191_CORING_MIN,
617 .maximum = SAA7191_CORING_MAX,
618 .step = 1,
619 .default_value = SAA7191_CORING_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300620 }, {
621 .id = SAA7191_CONTROL_FORCE_COLOUR,
Ladislav Michla637a112005-09-01 15:07:34 +0000622 .type = V4L2_CTRL_TYPE_BOOLEAN,
623 .name = "Force Colour",
624 .minimum = SAA7191_FORCE_COLOUR_MIN,
625 .maximum = SAA7191_FORCE_COLOUR_MAX,
626 .step = 1,
627 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300628 }, {
629 .id = SAA7191_CONTROL_CHROMA_GAIN,
Ladislav Michla637a112005-09-01 15:07:34 +0000630 .type = V4L2_CTRL_TYPE_INTEGER,
631 .name = "Chrominance Gain Control",
632 .minimum = SAA7191_CHROMA_GAIN_MIN,
633 .maximum = SAA7191_CHROMA_GAIN_MAX,
634 .step = 1,
635 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300636 }, {
637 .id = SAA7191_CONTROL_VTRC,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700638 .type = V4L2_CTRL_TYPE_BOOLEAN,
639 .name = "VTR Time Constant",
640 .minimum = SAA7191_VTRC_MIN,
641 .maximum = SAA7191_VTRC_MAX,
642 .step = 1,
643 .default_value = SAA7191_VTRC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300644 }, {
645 .id = SAA7191_CONTROL_LUMA_DELAY,
Ladislav Michla637a112005-09-01 15:07:34 +0000646 .type = V4L2_CTRL_TYPE_INTEGER,
647 .name = "Luminance Delay Compensation",
648 .minimum = SAA7191_LUMA_DELAY_MIN,
649 .maximum = SAA7191_LUMA_DELAY_MAX,
650 .step = 1,
651 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300652 }, {
653 .id = SAA7191_CONTROL_VNR,
Ladislav Michla637a112005-09-01 15:07:34 +0000654 .type = V4L2_CTRL_TYPE_INTEGER,
655 .name = "Vertical Noise Reduction",
656 .minimum = SAA7191_VNR_MIN,
657 .maximum = SAA7191_VNR_MAX,
658 .step = 1,
659 .default_value = SAA7191_VNR_DEFAULT,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700660 }
661};
662
Ralf Baechled203a7e2005-09-06 15:19:37 -0700663/* VINO framebuffer/DMA descriptor management */
664
665static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
666 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700668 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Ralf Baechled203a7e2005-09-06 15:19:37 -0700670 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Ralf Baechled203a7e2005-09-06 15:19:37 -0700672 for (i = 0; i < count; i++) {
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300673 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700674 dma_unmap_single(NULL,
675 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
676 PAGE_SIZE, DMA_FROM_DEVICE);
677 free_page(fb->desc_table.virtual[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Ralf Baechled203a7e2005-09-06 15:19:37 -0700680 dma_free_coherent(NULL,
681 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
682 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
683 fb->desc_table.dma);
684 kfree(fb->desc_table.virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ralf Baechled203a7e2005-09-06 15:19:37 -0700686 memset(fb, 0, sizeof(struct vino_framebuffer));
687}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Ralf Baechled203a7e2005-09-06 15:19:37 -0700689static void vino_free_buffer(struct vino_framebuffer *fb)
690{
691 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
692}
693
694static int vino_allocate_buffer(struct vino_framebuffer *fb,
695 unsigned int size)
696{
697 unsigned int count, i, j;
698 int ret = 0;
699
700 dprintk("vino_allocate_buffer():\n");
701
702 if (size < 1)
703 return -EINVAL;
704
705 memset(fb, 0, sizeof(struct vino_framebuffer));
706
707 count = ((size / PAGE_SIZE) + 4) & ~3;
708
709 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
710 size, count);
711
712 /* allocate memory for table with virtual (page) addresses */
713 fb->desc_table.virtual = (unsigned long *)
714 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
715 if (!fb->desc_table.virtual)
716 return -ENOMEM;
717
718 /* allocate memory for table with dma addresses
719 * (has space for four extra descriptors) */
720 fb->desc_table.dma_cpu =
721 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
722 sizeof(dma_addr_t), &fb->desc_table.dma,
723 GFP_KERNEL | GFP_DMA);
724 if (!fb->desc_table.dma_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ret = -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700726 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Ralf Baechled203a7e2005-09-06 15:19:37 -0700729 /* allocate pages for the buffer and acquire the according
730 * dma addresses */
731 for (i = 0; i < count; i++) {
732 dma_addr_t dma_data_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Ralf Baechled203a7e2005-09-06 15:19:37 -0700734 fb->desc_table.virtual[i] =
735 get_zeroed_page(GFP_KERNEL | GFP_DMA);
736 if (!fb->desc_table.virtual[i]) {
737 ret = -ENOBUFS;
738 break;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Ralf Baechled203a7e2005-09-06 15:19:37 -0700741 dma_data_addr =
742 dma_map_single(NULL,
743 (void *)fb->desc_table.virtual[i],
744 PAGE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Ralf Baechled203a7e2005-09-06 15:19:37 -0700746 for (j = 0; j < VINO_PAGE_RATIO; j++) {
747 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
748 dma_data_addr + VINO_PAGE_SIZE * j;
749 }
750
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300751 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Ralf Baechled203a7e2005-09-06 15:19:37 -0700754 /* page_count needs to be set anyway, because the descriptor table has
755 * been allocated according to this number */
756 fb->desc_table.page_count = count;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (ret) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700759 /* the descriptor with index i doesn't contain
760 * a valid address yet */
761 vino_free_buffer_with_count(fb, i);
762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
Ralf Baechled203a7e2005-09-06 15:19:37 -0700765 //fb->size = size;
766 fb->size = count * PAGE_SIZE;
767 fb->data_format = VINO_DATA_FMT_NONE;
768
769 /* set the dma stop-bit for the last (count+1)th descriptor */
770 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
771 return 0;
772
773 out_free_virtual:
774 kfree(fb->desc_table.virtual);
775 return ret;
776}
777
778#if 0
779/* user buffers not fully implemented yet */
780static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
781 void *user,
782 unsigned int size)
783{
784 unsigned int count, i, j;
785 int ret = 0;
786
787 dprintk("vino_prepare_user_buffer():\n");
788
789 if (size < 1)
790 return -EINVAL;
791
792 memset(fb, 0, sizeof(struct vino_framebuffer));
793
794 count = ((size / PAGE_SIZE)) & ~3;
795
796 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
797 size, count);
798
799 /* allocate memory for table with virtual (page) addresses */
800 fb->desc_table.virtual = (unsigned long *)
801 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
802 if (!fb->desc_table.virtual)
803 return -ENOMEM;
804
805 /* allocate memory for table with dma addresses
806 * (has space for four extra descriptors) */
807 fb->desc_table.dma_cpu =
808 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
809 sizeof(dma_addr_t), &fb->desc_table.dma,
810 GFP_KERNEL | GFP_DMA);
811 if (!fb->desc_table.dma_cpu) {
812 ret = -ENOMEM;
813 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700815
816 /* allocate pages for the buffer and acquire the according
817 * dma addresses */
818 for (i = 0; i < count; i++) {
819 dma_addr_t dma_data_addr;
820
821 fb->desc_table.virtual[i] =
822 get_zeroed_page(GFP_KERNEL | GFP_DMA);
823 if (!fb->desc_table.virtual[i]) {
824 ret = -ENOBUFS;
825 break;
826 }
827
828 dma_data_addr =
829 dma_map_single(NULL,
830 (void *)fb->desc_table.virtual[i],
831 PAGE_SIZE, DMA_FROM_DEVICE);
832
833 for (j = 0; j < VINO_PAGE_RATIO; j++) {
834 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
835 dma_data_addr + VINO_PAGE_SIZE * j;
836 }
837
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300838 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700839 }
840
841 /* page_count needs to be set anyway, because the descriptor table has
842 * been allocated according to this number */
843 fb->desc_table.page_count = count;
844
845 if (ret) {
846 /* the descriptor with index i doesn't contain
847 * a valid address yet */
848 vino_free_buffer_with_count(fb, i);
849 return ret;
850 }
851
852 //fb->size = size;
853 fb->size = count * PAGE_SIZE;
854
855 /* set the dma stop-bit for the last (count+1)th descriptor */
856 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
857 return 0;
858
859 out_free_virtual:
860 kfree(fb->desc_table.virtual);
861 return ret;
862}
863#endif
864
865static void vino_sync_buffer(struct vino_framebuffer *fb)
866{
867 int i;
868
869 dprintk("vino_sync_buffer():\n");
870
871 for (i = 0; i < fb->desc_table.page_count; i++)
FUJITA Tomonori028bbfe2009-05-27 22:10:43 -0300872 dma_sync_single_for_cpu(NULL,
873 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
874 PAGE_SIZE, DMA_FROM_DEVICE);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700875}
876
877/* Framebuffer fifo functions (need to be locked externally) */
878
Ladislav Michla637a112005-09-01 15:07:34 +0000879static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700880 unsigned int length)
881{
882 f->length = 0;
883 f->used = 0;
884 f->head = 0;
885 f->tail = 0;
886
Ladislav Michla637a112005-09-01 15:07:34 +0000887 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
888 length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700889
890 f->length = length;
891}
892
893/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000894static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
895 unsigned int id)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700896{
897 unsigned int i;
Ladislav Michla637a112005-09-01 15:07:34 +0000898
Ralf Baechled203a7e2005-09-06 15:19:37 -0700899 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
900 if (f->data[i] == id)
901 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700905}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ladislav Michla637a112005-09-01 15:07:34 +0000907#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -0700908/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000909static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700910{
911 return (f->used == f->length);
912}
Ladislav Michla637a112005-09-01 15:07:34 +0000913#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -0700914
Ladislav Michla637a112005-09-01 15:07:34 +0000915static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700916{
917 return f->used;
918}
919
920static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
921{
922 if (id >= f->length) {
923 return VINO_QUEUE_ERROR;
924 }
925
926 if (vino_fifo_has_id(f, id)) {
927 return VINO_QUEUE_ERROR;
928 }
929
930 if (f->used < f->length) {
931 f->data[f->tail] = id;
932 f->tail = (f->tail + 1) % f->length;
933 f->used++;
934 } else {
935 return VINO_QUEUE_ERROR;
936 }
937
938 return 0;
939}
940
941static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
942{
943 if (f->used > 0) {
944 *id = f->data[f->head];
945 } else {
946 return VINO_QUEUE_ERROR;
947 }
948
949 return 0;
950}
951
952static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
953{
954 if (f->used > 0) {
955 *id = f->data[f->head];
956 f->head = (f->head + 1) % f->length;
957 f->used--;
958 } else {
959 return VINO_QUEUE_ERROR;
960 }
961
962 return 0;
963}
964
965/* Framebuffer queue functions */
966
967/* execute with queue_lock locked */
968static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
969 unsigned int length)
970{
971 unsigned int i;
972
973 q->length = 0;
974 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
975 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
976 for (i = 0; i < length; i++) {
977 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
978 i);
979 vino_free_buffer(q->buffer[i]);
980 kfree(q->buffer[i]);
981 }
982
983 q->type = VINO_MEMORY_NONE;
984 q->magic = 0;
985}
986
987static void vino_queue_free(struct vino_framebuffer_queue *q)
988{
989 dprintk("vino_queue_free():\n");
990
991 if (q->magic != VINO_QUEUE_MAGIC)
992 return;
993 if (q->type != VINO_MEMORY_MMAP)
994 return;
995
Ingo Molnar3593cab2006-02-07 06:49:14 -0200996 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700997
998 vino_queue_free_with_count(q, q->length);
999
Ingo Molnar3593cab2006-02-07 06:49:14 -02001000 mutex_unlock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001001}
1002
1003static int vino_queue_init(struct vino_framebuffer_queue *q,
1004 unsigned int *length)
1005{
1006 unsigned int i;
1007 int ret = 0;
1008
1009 dprintk("vino_queue_init(): length = %d\n", *length);
1010
1011 if (q->magic == VINO_QUEUE_MAGIC) {
1012 dprintk("vino_queue_init(): queue already initialized!\n");
1013 return -EINVAL;
1014 }
1015
1016 if (q->type != VINO_MEMORY_NONE) {
1017 dprintk("vino_queue_init(): queue already initialized!\n");
1018 return -EINVAL;
1019 }
1020
1021 if (*length < 1)
1022 return -EINVAL;
1023
Ingo Molnar3593cab2006-02-07 06:49:14 -02001024 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001025
Ladislav Michla637a112005-09-01 15:07:34 +00001026 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1027 *length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001028
1029 q->length = 0;
1030
1031 for (i = 0; i < *length; i++) {
1032 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1033 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1034 GFP_KERNEL);
1035 if (!q->buffer[i]) {
1036 dprintk("vino_queue_init(): kmalloc() failed\n");
1037 ret = -ENOMEM;
1038 break;
1039 }
1040
1041 ret = vino_allocate_buffer(q->buffer[i],
1042 VINO_FRAMEBUFFER_SIZE);
1043 if (ret) {
1044 kfree(q->buffer[i]);
1045 dprintk("vino_queue_init(): "
1046 "vino_allocate_buffer() failed\n");
1047 break;
1048 }
1049
1050 q->buffer[i]->id = i;
1051 if (i > 0) {
1052 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1053 q->buffer[i - 1]->size;
1054 } else {
1055 q->buffer[i]->offset = 0;
1056 }
1057
1058 spin_lock_init(&q->buffer[i]->state_lock);
1059
1060 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1061 "size = %d\n", i, q->buffer[i]->offset,
1062 q->buffer[i]->size);
1063 }
1064
1065 if (ret) {
1066 vino_queue_free_with_count(q, i);
1067 *length = 0;
1068 } else {
1069 q->length = *length;
1070 vino_fifo_init(&q->in, q->length);
1071 vino_fifo_init(&q->out, q->length);
1072 q->type = VINO_MEMORY_MMAP;
1073 q->magic = VINO_QUEUE_MAGIC;
1074 }
1075
Ingo Molnar3593cab2006-02-07 06:49:14 -02001076 mutex_unlock(&q->queue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 return ret;
1079}
1080
Ralf Baechled203a7e2005-09-06 15:19:37 -07001081static struct vino_framebuffer *vino_queue_add(struct
1082 vino_framebuffer_queue *q,
1083 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Ralf Baechled203a7e2005-09-06 15:19:37 -07001085 struct vino_framebuffer *ret = NULL;
1086 unsigned int total;
1087 unsigned long flags;
1088
1089 dprintk("vino_queue_add(): id = %d\n", id);
1090
1091 if (q->magic != VINO_QUEUE_MAGIC) {
1092 return ret;
1093 }
1094
1095 spin_lock_irqsave(&q->queue_lock, flags);
1096
1097 if (q->length == 0)
1098 goto out;
1099
1100 if (id >= q->length)
1101 goto out;
1102
1103 /* not needed?: if (vino_fifo_full(&q->out)) {
1104 goto out;
1105 }*/
1106 /* check that outgoing queue isn't already full
1107 * (or that it won't become full) */
1108 total = vino_fifo_get_used(&q->in) +
1109 vino_fifo_get_used(&q->out);
1110 if (total >= q->length)
1111 goto out;
1112
1113 if (vino_fifo_enqueue(&q->in, id))
1114 goto out;
1115
1116 ret = q->buffer[id];
1117
1118out:
1119 spin_unlock_irqrestore(&q->queue_lock, flags);
1120
1121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Ralf Baechled203a7e2005-09-06 15:19:37 -07001124static struct vino_framebuffer *vino_queue_transfer(struct
1125 vino_framebuffer_queue *q)
1126{
1127 struct vino_framebuffer *ret = NULL;
1128 struct vino_framebuffer *fb;
1129 int id;
1130 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Ralf Baechled203a7e2005-09-06 15:19:37 -07001132 dprintk("vino_queue_transfer():\n");
1133
1134 if (q->magic != VINO_QUEUE_MAGIC) {
1135 return ret;
1136 }
1137
1138 spin_lock_irqsave(&q->queue_lock, flags);
1139
1140 if (q->length == 0)
1141 goto out;
1142
1143 // now this actually removes an entry from the incoming queue
1144 if (vino_fifo_dequeue(&q->in, &id)) {
1145 goto out;
1146 }
1147
1148 dprintk("vino_queue_transfer(): id = %d\n", id);
1149 fb = q->buffer[id];
1150
1151 // we have already checked that the outgoing queue is not full, but...
1152 if (vino_fifo_enqueue(&q->out, id)) {
1153 printk(KERN_ERR "vino_queue_transfer(): "
1154 "outgoing queue is full, this shouldn't happen!\n");
1155 goto out;
1156 }
1157
1158 ret = fb;
1159out:
1160 spin_unlock_irqrestore(&q->queue_lock, flags);
1161
1162 return ret;
1163}
1164
1165/* returns true/false */
1166static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1167 unsigned int id)
1168{
1169 int ret = 0;
1170 unsigned long flags;
1171
1172 if (q->magic != VINO_QUEUE_MAGIC) {
1173 return ret;
1174 }
1175
1176 spin_lock_irqsave(&q->queue_lock, flags);
1177
1178 if (q->length == 0)
1179 goto out;
1180
1181 ret = vino_fifo_has_id(&q->in, id);
1182
1183out:
1184 spin_unlock_irqrestore(&q->queue_lock, flags);
1185
1186 return ret;
1187}
1188
1189/* returns true/false */
1190static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1191 unsigned int id)
1192{
1193 int ret = 0;
1194 unsigned long flags;
1195
1196 if (q->magic != VINO_QUEUE_MAGIC) {
1197 return ret;
1198 }
1199
1200 spin_lock_irqsave(&q->queue_lock, flags);
1201
1202 if (q->length == 0)
1203 goto out;
1204
1205 ret = vino_fifo_has_id(&q->out, id);
1206
1207out:
1208 spin_unlock_irqrestore(&q->queue_lock, flags);
1209
1210 return ret;
1211}
1212
1213static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1214 unsigned int *used)
1215{
1216 int ret = 0;
1217 unsigned long flags;
1218
1219 if (q->magic != VINO_QUEUE_MAGIC) {
1220 return VINO_QUEUE_ERROR;
1221 }
1222
1223 spin_lock_irqsave(&q->queue_lock, flags);
1224
1225 if (q->length == 0) {
1226 ret = VINO_QUEUE_ERROR;
1227 goto out;
1228 }
1229
1230 *used = vino_fifo_get_used(&q->in);
1231
1232out:
1233 spin_unlock_irqrestore(&q->queue_lock, flags);
1234
1235 return ret;
1236}
1237
1238static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1239 unsigned int *used)
1240{
1241 int ret = 0;
1242 unsigned long flags;
1243
1244 if (q->magic != VINO_QUEUE_MAGIC) {
1245 return VINO_QUEUE_ERROR;
1246 }
1247
1248 spin_lock_irqsave(&q->queue_lock, flags);
1249
1250 if (q->length == 0) {
1251 ret = VINO_QUEUE_ERROR;
1252 goto out;
1253 }
1254
1255 *used = vino_fifo_get_used(&q->out);
1256
1257out:
1258 spin_unlock_irqrestore(&q->queue_lock, flags);
1259
1260 return ret;
1261}
1262
Ladislav Michla637a112005-09-01 15:07:34 +00001263#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001264static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1265 unsigned int *total)
1266{
1267 int ret = 0;
1268 unsigned long flags;
1269
1270 if (q->magic != VINO_QUEUE_MAGIC) {
1271 return VINO_QUEUE_ERROR;
1272 }
1273
1274 spin_lock_irqsave(&q->queue_lock, flags);
1275
1276 if (q->length == 0) {
1277 ret = VINO_QUEUE_ERROR;
1278 goto out;
1279 }
1280
1281 *total = vino_fifo_get_used(&q->in) +
1282 vino_fifo_get_used(&q->out);
1283
1284out:
1285 spin_unlock_irqrestore(&q->queue_lock, flags);
1286
1287 return ret;
1288}
Ladislav Michla637a112005-09-01 15:07:34 +00001289#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001290
1291static struct vino_framebuffer *vino_queue_peek(struct
1292 vino_framebuffer_queue *q,
1293 unsigned int *id)
1294{
1295 struct vino_framebuffer *ret = NULL;
1296 unsigned long flags;
1297
1298 if (q->magic != VINO_QUEUE_MAGIC) {
1299 return ret;
1300 }
1301
1302 spin_lock_irqsave(&q->queue_lock, flags);
1303
1304 if (q->length == 0)
1305 goto out;
1306
1307 if (vino_fifo_peek(&q->in, id)) {
1308 goto out;
1309 }
1310
1311 ret = q->buffer[*id];
1312out:
1313 spin_unlock_irqrestore(&q->queue_lock, flags);
1314
1315 return ret;
1316}
1317
1318static struct vino_framebuffer *vino_queue_remove(struct
1319 vino_framebuffer_queue *q,
1320 unsigned int *id)
1321{
1322 struct vino_framebuffer *ret = NULL;
1323 unsigned long flags;
1324 dprintk("vino_queue_remove():\n");
1325
1326 if (q->magic != VINO_QUEUE_MAGIC) {
1327 return ret;
1328 }
1329
1330 spin_lock_irqsave(&q->queue_lock, flags);
1331
1332 if (q->length == 0)
1333 goto out;
1334
1335 if (vino_fifo_dequeue(&q->out, id)) {
1336 goto out;
1337 }
1338
1339 dprintk("vino_queue_remove(): id = %d\n", *id);
1340 ret = q->buffer[*id];
1341out:
1342 spin_unlock_irqrestore(&q->queue_lock, flags);
1343
1344 return ret;
1345}
1346
1347static struct
1348vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1349 unsigned int id)
1350{
1351 struct vino_framebuffer *ret = NULL;
1352 unsigned long flags;
1353
1354 if (q->magic != VINO_QUEUE_MAGIC) {
1355 return ret;
1356 }
1357
1358 spin_lock_irqsave(&q->queue_lock, flags);
1359
1360 if (q->length == 0)
1361 goto out;
1362
1363 if (id >= q->length)
1364 goto out;
1365
1366 ret = q->buffer[id];
1367 out:
1368 spin_unlock_irqrestore(&q->queue_lock, flags);
1369
1370 return ret;
1371}
1372
1373static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1374{
1375 unsigned int length = 0;
1376 unsigned long flags;
1377
1378 if (q->magic != VINO_QUEUE_MAGIC) {
1379 return length;
1380 }
1381
1382 spin_lock_irqsave(&q->queue_lock, flags);
1383 length = q->length;
1384 spin_unlock_irqrestore(&q->queue_lock, flags);
1385
1386 return length;
1387}
1388
1389static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1390{
1391 unsigned int i;
1392 int ret = 0;
1393 unsigned long flags;
1394
1395 if (q->magic != VINO_QUEUE_MAGIC) {
1396 return ret;
1397 }
1398
1399 spin_lock_irqsave(&q->queue_lock, flags);
1400 for (i = 0; i < q->length; i++) {
1401 if (q->buffer[i]->map_count > 0) {
1402 ret = 1;
1403 break;
1404 }
1405 }
1406 spin_unlock_irqrestore(&q->queue_lock, flags);
1407
1408 return ret;
1409}
1410
1411/* VINO functions */
1412
1413/* execute with input_lock locked */
1414static void vino_update_line_size(struct vino_channel_settings *vcs)
1415{
1416 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1417 unsigned int d = vcs->decimation;
1418 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001419 unsigned int lsize;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001420
1421 dprintk("update_line_size(): before: w = %d, d = %d, "
1422 "line_size = %d\n", w, d, vcs->line_size);
Ladislav Michla637a112005-09-01 15:07:34 +00001423
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001424 /* line size must be multiple of 8 bytes */
Ralf Baechled203a7e2005-09-06 15:19:37 -07001425 lsize = (bpp * (w / d)) & ~7;
1426 w = (lsize / bpp) * d;
1427
1428 vcs->clipping.right = vcs->clipping.left + w;
1429 vcs->line_size = lsize;
Ladislav Michla637a112005-09-01 15:07:34 +00001430
Ralf Baechled203a7e2005-09-06 15:19:37 -07001431 dprintk("update_line_size(): after: w = %d, d = %d, "
1432 "line_size = %d\n", w, d, vcs->line_size);
1433}
1434
1435/* execute with input_lock locked */
1436static void vino_set_clipping(struct vino_channel_settings *vcs,
1437 unsigned int x, unsigned int y,
1438 unsigned int w, unsigned int h)
1439{
1440 unsigned int maxwidth, maxheight;
1441 unsigned int d;
1442
1443 maxwidth = vino_data_norms[vcs->data_norm].width;
1444 maxheight = vino_data_norms[vcs->data_norm].height;
1445 d = vcs->decimation;
1446
1447 y &= ~1; /* odd/even fields */
1448
1449 if (x > maxwidth) {
1450 x = 0;
1451 }
1452 if (y > maxheight) {
1453 y = 0;
1454 }
1455
1456 if (((w / d) < VINO_MIN_WIDTH)
1457 || ((h / d) < VINO_MIN_HEIGHT)) {
1458 w = VINO_MIN_WIDTH * d;
1459 h = VINO_MIN_HEIGHT * d;
1460 }
1461
1462 if ((x + w) > maxwidth) {
1463 w = maxwidth - x;
1464 if ((w / d) < VINO_MIN_WIDTH)
1465 x = maxwidth - VINO_MIN_WIDTH * d;
1466 }
1467 if ((y + h) > maxheight) {
1468 h = maxheight - y;
1469 if ((h / d) < VINO_MIN_HEIGHT)
1470 y = maxheight - VINO_MIN_HEIGHT * d;
1471 }
1472
1473 vcs->clipping.left = x;
1474 vcs->clipping.top = y;
1475 vcs->clipping.right = x + w;
1476 vcs->clipping.bottom = y + h;
1477
1478 vino_update_line_size(vcs);
1479
1480 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1481 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1482 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1483}
1484
1485/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001486static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001487{
1488 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1489 vino_data_norms[vcs->data_norm].height);
1490}
1491
1492/* execute with input_lock locked */
1493static void vino_set_scaling(struct vino_channel_settings *vcs,
1494 unsigned int w, unsigned int h)
1495{
1496 unsigned int x, y, curw, curh, d;
1497
1498 x = vcs->clipping.left;
1499 y = vcs->clipping.top;
1500 curw = vcs->clipping.right - vcs->clipping.left;
1501 curh = vcs->clipping.bottom - vcs->clipping.top;
1502
1503 d = max(curw / w, curh / h);
1504
1505 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1506 w, h, curw, curh, d);
1507
1508 if (d < 1) {
1509 d = 1;
Ladislav Michla637a112005-09-01 15:07:34 +00001510 } else if (d > 8) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07001511 d = 8;
1512 }
1513
1514 vcs->decimation = d;
1515 vino_set_clipping(vcs, x, y, w * d, h * d);
1516
1517 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1518 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1519 vcs->decimation, vcs->line_size);
1520}
1521
1522/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001523static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001524{
1525 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1526 vcs->clipping.bottom - vcs->clipping.top);
1527}
1528
1529/* execute with input_lock locked */
1530static void vino_set_framerate(struct vino_channel_settings *vcs,
1531 unsigned int fps)
1532{
1533 unsigned int mask;
1534
1535 switch (vcs->data_norm) {
1536 case VINO_DATA_NORM_NTSC:
1537 case VINO_DATA_NORM_D1:
1538 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1539
1540 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1541 fps = vino_data_norms[vcs->data_norm].fps_min;
1542 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1543 fps = vino_data_norms[vcs->data_norm].fps_max;
1544
1545 switch (fps) {
1546 case 6:
1547 mask = 0x003;
1548 break;
1549 case 12:
1550 mask = 0x0c3;
1551 break;
1552 case 18:
1553 mask = 0x333;
1554 break;
1555 case 24:
1556 mask = 0x3ff;
1557 break;
1558 case 30:
1559 mask = 0xfff;
1560 break;
1561 default:
1562 mask = VINO_FRAMERT_FULL;
1563 }
1564 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1565 break;
1566 case VINO_DATA_NORM_PAL:
1567 case VINO_DATA_NORM_SECAM:
1568 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1569
1570 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1571 fps = vino_data_norms[vcs->data_norm].fps_min;
1572 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1573 fps = vino_data_norms[vcs->data_norm].fps_max;
1574
1575 switch (fps) {
1576 case 5:
1577 mask = 0x003;
1578 break;
1579 case 10:
1580 mask = 0x0c3;
1581 break;
1582 case 15:
1583 mask = 0x333;
1584 break;
1585 case 20:
1586 mask = 0x0ff;
1587 break;
1588 case 25:
1589 mask = 0x3ff;
1590 break;
1591 default:
1592 mask = VINO_FRAMERT_FULL;
1593 }
1594 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1595 break;
1596 }
1597
1598 vcs->fps = fps;
1599}
1600
1601/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001602static inline void vino_set_default_framerate(struct
1603 vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001604{
1605 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1606}
1607
Jean Delvarecea0c682009-03-06 12:05:43 -03001608/* VINO I2C bus functions */
1609
1610struct i2c_algo_sgi_data {
1611 void *data; /* private data for lowlevel routines */
1612 unsigned (*getctrl)(void *data);
1613 void (*setctrl)(void *data, unsigned val);
1614 unsigned (*rdata)(void *data);
1615 void (*wdata)(void *data, unsigned val);
1616
1617 int xfer_timeout;
1618 int ack_timeout;
1619};
1620
1621static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1622{
1623 int i;
1624
1625 for (i = 0; i < adap->xfer_timeout; i++) {
1626 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1627 return 0;
1628 udelay(1);
1629 }
1630
1631 return -ETIMEDOUT;
1632}
1633
1634static int wait_ack(struct i2c_algo_sgi_data *adap)
1635{
1636 int i;
1637
1638 if (wait_xfer_done(adap))
1639 return -ETIMEDOUT;
1640 for (i = 0; i < adap->ack_timeout; i++) {
1641 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1642 return 0;
1643 udelay(1);
1644 }
1645
1646 return -ETIMEDOUT;
1647}
1648
1649static int force_idle(struct i2c_algo_sgi_data *adap)
1650{
1651 int i;
1652
1653 adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1654 for (i = 0; i < adap->xfer_timeout; i++) {
1655 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1656 goto out;
1657 udelay(1);
1658 }
1659 return -ETIMEDOUT;
1660out:
1661 if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1662 return -EIO;
1663 return 0;
1664}
1665
1666static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1667 int rd)
1668{
1669 if (rd)
1670 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1671 /* Check if bus is idle, eventually force it to do so */
1672 if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1673 if (force_idle(adap))
1674 return -EIO;
1675 /* Write out the i2c chip address and specify operation */
1676 adap->setctrl(adap->data,
1677 SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1678 if (rd)
1679 addr |= 1;
1680 adap->wdata(adap->data, addr);
1681 if (wait_ack(adap))
1682 return -EIO;
1683 return 0;
1684}
1685
1686static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1687 unsigned int len)
1688{
1689 int i;
1690
1691 adap->setctrl(adap->data,
1692 SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1693 for (i = 0; i < len; i++) {
1694 if (wait_xfer_done(adap))
1695 return -EIO;
1696 buf[i] = adap->rdata(adap->data);
1697 }
1698 adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1699
1700 return 0;
1701
1702}
1703
1704static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1705 unsigned int len)
1706{
1707 int i;
1708
1709 /* We are already in write state */
1710 for (i = 0; i < len; i++) {
1711 adap->wdata(adap->data, buf[i]);
1712 if (wait_ack(adap))
1713 return -EIO;
1714 }
1715 return 0;
1716}
1717
1718static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1719 int num)
1720{
1721 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1722 struct i2c_msg *p;
1723 int i, err = 0;
1724
1725 for (i = 0; !err && i < num; i++) {
1726 p = &msgs[i];
1727 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1728 if (err || !p->len)
1729 continue;
1730 if (p->flags & I2C_M_RD)
1731 err = i2c_read(adap, p->buf, p->len);
1732 else
1733 err = i2c_write(adap, p->buf, p->len);
1734 }
1735
1736 return (err < 0) ? err : i;
1737}
1738
1739static u32 sgi_func(struct i2c_adapter *adap)
1740{
1741 return I2C_FUNC_SMBUS_EMUL;
1742}
1743
1744static const struct i2c_algorithm sgi_algo = {
1745 .master_xfer = sgi_xfer,
1746 .functionality = sgi_func,
1747};
1748
1749static unsigned i2c_vino_getctrl(void *data)
1750{
1751 return vino->i2c_control;
1752}
1753
1754static void i2c_vino_setctrl(void *data, unsigned val)
1755{
1756 vino->i2c_control = val;
1757}
1758
1759static unsigned i2c_vino_rdata(void *data)
1760{
1761 return vino->i2c_data;
1762}
1763
1764static void i2c_vino_wdata(void *data, unsigned val)
1765{
1766 vino->i2c_data = val;
1767}
1768
1769static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1770 .getctrl = &i2c_vino_getctrl,
1771 .setctrl = &i2c_vino_setctrl,
1772 .rdata = &i2c_vino_rdata,
1773 .wdata = &i2c_vino_wdata,
1774 .xfer_timeout = 200,
1775 .ack_timeout = 1000,
1776};
1777
1778static struct i2c_adapter vino_i2c_adapter = {
1779 .name = "VINO I2C bus",
Jean Delvarecea0c682009-03-06 12:05:43 -03001780 .algo = &sgi_algo,
1781 .algo_data = &i2c_sgi_vino_data,
1782 .owner = THIS_MODULE,
1783};
1784
Ralf Baechled203a7e2005-09-06 15:19:37 -07001785/*
1786 * Prepare VINO for DMA transfer...
1787 * (execute only with vino_lock and input_lock locked)
1788 */
1789static int vino_dma_setup(struct vino_channel_settings *vcs,
1790 struct vino_framebuffer *fb)
1791{
1792 u32 ctrl, intr;
1793 struct sgi_vino_channel *ch;
1794 const struct vino_data_norm *norm;
1795
1796 dprintk("vino_dma_setup():\n");
1797
1798 vcs->field = 0;
1799 fb->frame_counter = 0;
1800
1801 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1802 norm = &vino_data_norms[vcs->data_norm];
1803
1804 ch->page_index = 0;
1805 ch->line_count = 0;
1806
1807 /* VINO line size register is set 8 bytes less than actual */
1808 ch->line_size = vcs->line_size - 8;
1809
1810 /* let VINO know where to transfer data */
1811 ch->start_desc_tbl = fb->desc_table.dma;
1812 ch->next_4_desc = fb->desc_table.dma;
1813
1814 /* give vino time to fetch the first four descriptors, 5 usec
1815 * should be more than enough time */
1816 udelay(VINO_DESC_FETCH_DELAY);
1817
Ladislav Michla637a112005-09-01 15:07:34 +00001818 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1819 ch->start_desc_tbl, ch->next_4_desc);
1820
Ralf Baechled203a7e2005-09-06 15:19:37 -07001821 /* set the alpha register */
1822 ch->alpha = vcs->alpha;
1823
1824 /* set clipping registers */
1825 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1826 VINO_CLIP_EVEN(norm->even.top +
1827 vcs->clipping.top / 2) |
1828 VINO_CLIP_X(vcs->clipping.left);
1829 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1830 vcs->clipping.bottom / 2 - 1) |
1831 VINO_CLIP_EVEN(norm->even.top +
1832 vcs->clipping.bottom / 2 - 1) |
1833 VINO_CLIP_X(vcs->clipping.right);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001834
1835 /* set the size of actual content in the buffer (DECIMATION !) */
1836 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1837 vcs->decimation) *
1838 ((vcs->clipping.bottom - vcs->clipping.top) /
1839 vcs->decimation) *
1840 vino_data_formats[vcs->data_format].bpp;
1841
1842 ch->frame_rate = vcs->framert_reg;
1843
1844 ctrl = vino->control;
1845 intr = vino->intr_status;
1846
1847 if (vcs->channel == VINO_CHANNEL_A) {
1848 /* All interrupt conditions for this channel was cleared
1849 * so clear the interrupt status register and enable
1850 * interrupts */
1851 intr &= ~VINO_INTSTAT_A;
1852 ctrl |= VINO_CTRL_A_INT;
1853
1854 /* enable synchronization */
1855 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1856
1857 /* enable frame assembly */
1858 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1859
1860 /* set decimation used */
1861 if (vcs->decimation < 2)
1862 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1863 else {
1864 ctrl |= VINO_CTRL_A_DEC_ENBL;
1865 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1866 ctrl |= (vcs->decimation - 1) <<
1867 VINO_CTRL_A_DEC_SCALE_SHIFT;
1868 }
1869
1870 /* select input interface */
1871 if (vcs->input == VINO_INPUT_D1)
1872 ctrl |= VINO_CTRL_A_SELECT;
1873 else
1874 ctrl &= ~VINO_CTRL_A_SELECT;
1875
1876 /* palette */
1877 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1878 VINO_CTRL_A_DITHER);
1879 } else {
1880 intr &= ~VINO_INTSTAT_B;
1881 ctrl |= VINO_CTRL_B_INT;
1882
1883 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1884 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1885
1886 if (vcs->decimation < 2)
1887 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1888 else {
1889 ctrl |= VINO_CTRL_B_DEC_ENBL;
1890 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1891 ctrl |= (vcs->decimation - 1) <<
1892 VINO_CTRL_B_DEC_SCALE_SHIFT;
1893
1894 }
1895 if (vcs->input == VINO_INPUT_D1)
1896 ctrl |= VINO_CTRL_B_SELECT;
1897 else
1898 ctrl &= ~VINO_CTRL_B_SELECT;
1899
1900 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1901 VINO_CTRL_B_DITHER);
1902 }
1903
1904 /* set palette */
1905 fb->data_format = vcs->data_format;
1906
1907 switch (vcs->data_format) {
1908 case VINO_DATA_FMT_GREY:
1909 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1910 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1911 break;
1912 case VINO_DATA_FMT_RGB32:
1913 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1914 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1915 break;
1916 case VINO_DATA_FMT_YUV:
1917 /* nothing needs to be done */
1918 break;
1919 case VINO_DATA_FMT_RGB332:
1920 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1921 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1922 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1923 break;
1924 }
1925
1926 vino->intr_status = intr;
1927 vino->control = ctrl;
1928
1929 return 0;
1930}
1931
1932/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001933static inline void vino_dma_start(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001934{
1935 u32 ctrl = vino->control;
1936
1937 dprintk("vino_dma_start():\n");
1938 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1939 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1940 vino->control = ctrl;
1941}
1942
1943/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001944static inline void vino_dma_stop(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001945{
1946 u32 ctrl = vino->control;
1947
1948 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1949 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
Ladislav Michla637a112005-09-01 15:07:34 +00001950 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1951 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001952 vino->control = ctrl;
1953 dprintk("vino_dma_stop():\n");
1954}
1955
1956/*
1957 * Load dummy page to descriptor registers. This prevents generating of
1958 * spurious interrupts. (execute only with vino_lock locked)
1959 */
1960static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1961{
1962 struct sgi_vino_channel *ch;
1963
1964 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1965
1966 ch->page_index = 0;
1967 ch->line_count = 0;
1968
1969 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1970 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1971
1972 udelay(VINO_DESC_FETCH_DELAY);
1973 dprintk("channel %c clear interrupt condition\n",
1974 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1975}
1976
1977static int vino_capture(struct vino_channel_settings *vcs,
1978 struct vino_framebuffer *fb)
1979{
1980 int err = 0;
1981 unsigned long flags, flags2;
1982
1983 spin_lock_irqsave(&fb->state_lock, flags);
1984
1985 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1986 err = -EBUSY;
1987 fb->state = VINO_FRAMEBUFFER_IN_USE;
1988
1989 spin_unlock_irqrestore(&fb->state_lock, flags);
1990
1991 if (err)
1992 return err;
1993
1994 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1995 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1996
1997 vino_dma_setup(vcs, fb);
1998 vino_dma_start(vcs);
1999
2000 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
2001 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2002
2003 return err;
2004}
2005
2006static
2007struct vino_framebuffer *vino_capture_enqueue(struct
2008 vino_channel_settings *vcs,
2009 unsigned int index)
2010{
2011 struct vino_framebuffer *fb;
2012 unsigned long flags;
2013
2014 dprintk("vino_capture_enqueue():\n");
2015
2016 spin_lock_irqsave(&vcs->capture_lock, flags);
2017
2018 fb = vino_queue_add(&vcs->fb_queue, index);
2019 if (fb == NULL) {
2020 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
2021 "queue full?\n");
2022 goto out;
2023 }
2024out:
2025 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2026
2027 return fb;
2028}
2029
2030static int vino_capture_next(struct vino_channel_settings *vcs, int start)
2031{
2032 struct vino_framebuffer *fb;
2033 unsigned int incoming, id;
2034 int err = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002035 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002036
2037 dprintk("vino_capture_next():\n");
2038
2039 spin_lock_irqsave(&vcs->capture_lock, flags);
2040
2041 if (start) {
2042 /* start capture only if capture isn't in progress already */
2043 if (vcs->capturing) {
2044 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2045 return 0;
2046 }
2047
2048 } else {
2049 /* capture next frame:
2050 * stop capture if capturing is not set */
2051 if (!vcs->capturing) {
2052 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2053 return 0;
2054 }
2055 }
2056
2057 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2058 if (err) {
2059 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2060 "failed\n");
2061 err = -EINVAL;
2062 goto out;
2063 }
2064 if (incoming == 0) {
2065 dprintk("vino_capture_next(): no buffers available\n");
2066 goto out;
2067 }
2068
2069 fb = vino_queue_peek(&vcs->fb_queue, &id);
2070 if (fb == NULL) {
2071 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2072 err = -EINVAL;
2073 goto out;
2074 }
2075
Ralf Baechled203a7e2005-09-06 15:19:37 -07002076 if (start) {
2077 vcs->capturing = 1;
2078 }
2079
2080 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2081
2082 err = vino_capture(vcs, fb);
2083
2084 return err;
2085
2086out:
2087 vcs->capturing = 0;
2088 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2089
2090 return err;
2091}
2092
Ladislav Michla637a112005-09-01 15:07:34 +00002093static inline int vino_is_capturing(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002094{
2095 int ret;
2096 unsigned long flags;
2097
2098 spin_lock_irqsave(&vcs->capture_lock, flags);
2099
2100 ret = vcs->capturing;
2101
2102 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2103
2104 return ret;
2105}
2106
2107/* waits until a frame is captured */
2108static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2109{
2110 wait_queue_t wait;
2111 int err = 0;
2112
2113 dprintk("vino_wait_for_frame():\n");
2114
2115 init_waitqueue_entry(&wait, current);
2116 /* add ourselves into wait queue */
2117 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002118
2119 /* to ensure that schedule_timeout will return immediately
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -03002120 * if VINO interrupt was triggered meanwhile */
Mauro Carvalho Chehab818ca472007-07-17 16:29:07 -03002121 schedule_timeout_interruptible(msecs_to_jiffies(100));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002122
2123 if (signal_pending(current))
2124 err = -EINTR;
2125
2126 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2127
2128 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2129 err ? "failed" : "ok");
2130
2131 return err;
2132}
2133
2134/* the function assumes that PAGE_SIZE % 4 == 0 */
2135static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2136 unsigned char *pageptr;
2137 unsigned int page, i;
2138 unsigned char a;
2139
2140 for (page = 0; page < fb->desc_table.page_count; page++) {
2141 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2142
2143 for (i = 0; i < PAGE_SIZE; i += 4) {
2144 a = pageptr[0];
2145 pageptr[0] = pageptr[3];
2146 pageptr[1] = pageptr[2];
2147 pageptr[2] = pageptr[1];
2148 pageptr[3] = a;
2149 pageptr += 4;
2150 }
2151 }
2152}
2153
2154/* checks if the buffer is in correct state and syncs data */
2155static int vino_check_buffer(struct vino_channel_settings *vcs,
2156 struct vino_framebuffer *fb)
2157{
2158 int err = 0;
2159 unsigned long flags;
2160
2161 dprintk("vino_check_buffer():\n");
2162
2163 spin_lock_irqsave(&fb->state_lock, flags);
2164 switch (fb->state) {
2165 case VINO_FRAMEBUFFER_IN_USE:
2166 err = -EIO;
2167 break;
2168 case VINO_FRAMEBUFFER_READY:
2169 vino_sync_buffer(fb);
2170 fb->state = VINO_FRAMEBUFFER_UNUSED;
2171 break;
2172 default:
2173 err = -EINVAL;
2174 }
2175 spin_unlock_irqrestore(&fb->state_lock, flags);
2176
2177 if (!err) {
2178 if (vino_pixel_conversion
2179 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2180 vino_convert_to_rgba(fb);
2181 }
2182 } else if (err && (err != -EINVAL)) {
2183 dprintk("vino_check_buffer(): buffer not ready\n");
2184
2185 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2186 vino_dma_stop(vcs);
2187 vino_clear_interrupt(vcs);
2188 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2189 }
2190
2191 return err;
2192}
2193
2194/* forcefully terminates capture */
2195static void vino_capture_stop(struct vino_channel_settings *vcs)
2196{
2197 unsigned int incoming = 0, outgoing = 0, id;
2198 unsigned long flags, flags2;
2199
2200 dprintk("vino_capture_stop():\n");
2201
2202 spin_lock_irqsave(&vcs->capture_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00002203
Ralf Baechled203a7e2005-09-06 15:19:37 -07002204 /* unset capturing to stop queue processing */
2205 vcs->capturing = 0;
2206
2207 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2208
2209 vino_dma_stop(vcs);
2210 vino_clear_interrupt(vcs);
2211
2212 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2213
2214 /* remove all items from the queue */
2215 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2216 dprintk("vino_capture_stop(): "
2217 "vino_queue_get_incoming() failed\n");
2218 goto out;
2219 }
2220 while (incoming > 0) {
2221 vino_queue_transfer(&vcs->fb_queue);
2222
2223 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2224 dprintk("vino_capture_stop(): "
2225 "vino_queue_get_incoming() failed\n");
2226 goto out;
2227 }
2228 }
2229
2230 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2231 dprintk("vino_capture_stop(): "
2232 "vino_queue_get_outgoing() failed\n");
2233 goto out;
2234 }
2235 while (outgoing > 0) {
2236 vino_queue_remove(&vcs->fb_queue, &id);
2237
2238 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2239 dprintk("vino_capture_stop(): "
2240 "vino_queue_get_outgoing() failed\n");
2241 goto out;
2242 }
2243 }
2244
2245out:
2246 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2247}
2248
Ladislav Michla637a112005-09-01 15:07:34 +00002249#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07002250static int vino_capture_failed(struct vino_channel_settings *vcs)
2251{
2252 struct vino_framebuffer *fb;
2253 unsigned long flags;
2254 unsigned int i;
2255 int ret;
2256
2257 dprintk("vino_capture_failed():\n");
2258
2259 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2260
2261 vino_dma_stop(vcs);
2262 vino_clear_interrupt(vcs);
2263
2264 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2265
2266 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2267 if (ret == VINO_QUEUE_ERROR) {
2268 dprintk("vino_queue_get_incoming() failed\n");
2269 return -EINVAL;
2270 }
2271 if (i == 0) {
2272 /* no buffers to process */
2273 return 0;
2274 }
2275
2276 fb = vino_queue_peek(&vcs->fb_queue, &i);
2277 if (fb == NULL) {
2278 dprintk("vino_queue_peek() failed\n");
2279 return -EINVAL;
2280 }
2281
2282 spin_lock_irqsave(&fb->state_lock, flags);
2283 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2284 fb->state = VINO_FRAMEBUFFER_UNUSED;
2285 vino_queue_transfer(&vcs->fb_queue);
2286 vino_queue_remove(&vcs->fb_queue, &i);
2287 /* we should actually discard the newest frame,
2288 * but who cares ... */
2289 }
2290 spin_unlock_irqrestore(&fb->state_lock, flags);
2291
2292 return 0;
2293}
Ladislav Michla637a112005-09-01 15:07:34 +00002294#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002295
Ladislav Michla637a112005-09-01 15:07:34 +00002296static void vino_skip_frame(struct vino_channel_settings *vcs)
2297{
2298 struct vino_framebuffer *fb;
2299 unsigned long flags;
2300 unsigned int id;
2301
2302 spin_lock_irqsave(&vcs->capture_lock, flags);
2303 fb = vino_queue_peek(&vcs->fb_queue, &id);
2304 if (!fb) {
2305 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2306 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2307 return;
2308 }
2309 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2310
2311 spin_lock_irqsave(&fb->state_lock, flags);
2312 fb->state = VINO_FRAMEBUFFER_UNUSED;
2313 spin_unlock_irqrestore(&fb->state_lock, flags);
2314
2315 vino_capture_next(vcs, 0);
2316}
2317
2318static void vino_frame_done(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002319{
2320 struct vino_framebuffer *fb;
2321 unsigned long flags;
2322
2323 spin_lock_irqsave(&vcs->capture_lock, flags);
2324 fb = vino_queue_transfer(&vcs->fb_queue);
2325 if (!fb) {
2326 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2327 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2328 return;
2329 }
2330 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2331
Ladislav Michla637a112005-09-01 15:07:34 +00002332 fb->frame_counter = vcs->int_data.frame_counter;
2333 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2334 sizeof(struct timeval));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002335
2336 spin_lock_irqsave(&fb->state_lock, flags);
2337 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2338 fb->state = VINO_FRAMEBUFFER_READY;
2339 spin_unlock_irqrestore(&fb->state_lock, flags);
2340
2341 wake_up(&vcs->fb_queue.frame_wait_queue);
2342
2343 vino_capture_next(vcs, 0);
2344}
2345
Ladislav Michla637a112005-09-01 15:07:34 +00002346static void vino_capture_tasklet(unsigned long channel) {
2347 struct vino_channel_settings *vcs;
2348
2349 vcs = (channel == VINO_CHANNEL_A)
2350 ? &vino_drvdata->a : &vino_drvdata->b;
2351
2352 if (vcs->int_data.skip)
2353 vcs->int_data.skip_count++;
2354
2355 if (vcs->int_data.skip && (vcs->int_data.skip_count
2356 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2357 vino_skip_frame(vcs);
2358 } else {
2359 vcs->int_data.skip_count = 0;
2360 vino_frame_done(vcs);
2361 }
2362}
2363
David Howells7d12e782006-10-05 14:55:46 +01002364static irqreturn_t vino_interrupt(int irq, void *dev_id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002365{
Ladislav Michla637a112005-09-01 15:07:34 +00002366 u32 ctrl, intr;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002367 unsigned int fc_a, fc_b;
Ladislav Michla637a112005-09-01 15:07:34 +00002368 int handled_a = 0, skip_a = 0, done_a = 0;
2369 int handled_b = 0, skip_b = 0, done_b = 0;
2370
2371#ifdef VINO_DEBUG_INT
2372 int loop = 0;
2373 unsigned int line_count = vino->a.line_count,
2374 page_index = vino->a.page_index,
2375 field_counter = vino->a.field_counter,
2376 start_desc_tbl = vino->a.start_desc_tbl,
2377 next_4_desc = vino->a.next_4_desc;
2378 unsigned int line_count_2,
2379 page_index_2,
2380 field_counter_2,
2381 start_desc_tbl_2,
2382 next_4_desc_2;
2383#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002384
2385 spin_lock(&vino_drvdata->vino_lock);
2386
Ladislav Michla637a112005-09-01 15:07:34 +00002387 while ((intr = vino->intr_status)) {
2388 fc_a = vino->a.field_counter >> 1;
2389 fc_b = vino->b.field_counter >> 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002390
Ladislav Michla637a112005-09-01 15:07:34 +00002391 /* handle error-interrupts in some special way ?
2392 * --> skips frames */
2393 if (intr & VINO_INTSTAT_A) {
2394 if (intr & VINO_INTSTAT_A_EOF) {
2395 vino_drvdata->a.field++;
2396 if (vino_drvdata->a.field > 1) {
2397 vino_dma_stop(&vino_drvdata->a);
2398 vino_clear_interrupt(&vino_drvdata->a);
2399 vino_drvdata->a.field = 0;
2400 done_a = 1;
2401 } else {
2402 if (vino->a.page_index
2403 != vino_drvdata->a.line_size) {
2404 vino->a.line_count = 0;
2405 vino->a.page_index =
2406 vino_drvdata->
2407 a.line_size;
2408 vino->a.next_4_desc =
2409 vino->a.start_desc_tbl;
2410 }
2411 }
2412 dprintk("channel A end-of-field "
2413 "interrupt: %04x\n", intr);
2414 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002415 vino_dma_stop(&vino_drvdata->a);
2416 vino_clear_interrupt(&vino_drvdata->a);
2417 vino_drvdata->a.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002418 skip_a = 1;
2419 dprintk("channel A error interrupt: %04x\n",
2420 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002421 }
Ladislav Michla637a112005-09-01 15:07:34 +00002422
2423#ifdef VINO_DEBUG_INT
2424 line_count_2 = vino->a.line_count;
2425 page_index_2 = vino->a.page_index;
2426 field_counter_2 = vino->a.field_counter;
2427 start_desc_tbl_2 = vino->a.start_desc_tbl;
2428 next_4_desc_2 = vino->a.next_4_desc;
2429
2430 printk("intr = %04x, loop = %d, field = %d\n",
2431 intr, loop, vino_drvdata->a.field);
2432 printk("1- line count = %04d, page index = %04d, "
2433 "start = %08x, next = %08x\n"
2434 " fieldc = %d, framec = %d\n",
2435 line_count, page_index, start_desc_tbl,
2436 next_4_desc, field_counter, fc_a);
2437 printk("12-line count = %04d, page index = %04d, "
2438 " start = %08x, next = %08x\n",
2439 line_count_2, page_index_2, start_desc_tbl_2,
2440 next_4_desc_2);
2441
2442 if (done_a)
2443 printk("\n");
2444#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002445 }
Ladislav Michla637a112005-09-01 15:07:34 +00002446
2447 if (intr & VINO_INTSTAT_B) {
2448 if (intr & VINO_INTSTAT_B_EOF) {
2449 vino_drvdata->b.field++;
2450 if (vino_drvdata->b.field > 1) {
2451 vino_dma_stop(&vino_drvdata->b);
2452 vino_clear_interrupt(&vino_drvdata->b);
2453 vino_drvdata->b.field = 0;
2454 done_b = 1;
2455 }
2456 dprintk("channel B end-of-field "
2457 "interrupt: %04x\n", intr);
2458 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002459 vino_dma_stop(&vino_drvdata->b);
2460 vino_clear_interrupt(&vino_drvdata->b);
2461 vino_drvdata->b.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002462 skip_b = 1;
2463 dprintk("channel B error interrupt: %04x\n",
2464 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002465 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002466 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002467
Ladislav Michla637a112005-09-01 15:07:34 +00002468 /* Always remember to clear interrupt status.
2469 * Disable VINO interrupts while we do this. */
2470 ctrl = vino->control;
2471 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2472 vino->intr_status = ~intr;
2473 vino->control = ctrl;
2474
2475 spin_unlock(&vino_drvdata->vino_lock);
2476
2477 if ((!handled_a) && (done_a || skip_a)) {
2478 if (!skip_a) {
2479 do_gettimeofday(&vino_drvdata->
2480 a.int_data.timestamp);
2481 vino_drvdata->a.int_data.frame_counter = fc_a;
2482 }
2483 vino_drvdata->a.int_data.skip = skip_a;
2484
2485 dprintk("channel A %s, interrupt: %d\n",
2486 skip_a ? "skipping frame" : "frame done",
2487 intr);
2488 tasklet_hi_schedule(&vino_tasklet_a);
2489 handled_a = 1;
2490 }
2491
2492 if ((!handled_b) && (done_b || skip_b)) {
2493 if (!skip_b) {
2494 do_gettimeofday(&vino_drvdata->
2495 b.int_data.timestamp);
2496 vino_drvdata->b.int_data.frame_counter = fc_b;
2497 }
2498 vino_drvdata->b.int_data.skip = skip_b;
2499
2500 dprintk("channel B %s, interrupt: %d\n",
2501 skip_b ? "skipping frame" : "frame done",
2502 intr);
2503 tasklet_hi_schedule(&vino_tasklet_b);
2504 handled_b = 1;
2505 }
2506
2507#ifdef VINO_DEBUG_INT
2508 loop++;
2509#endif
2510 spin_lock(&vino_drvdata->vino_lock);
2511 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002512
2513 spin_unlock(&vino_drvdata->vino_lock);
2514
Ralf Baechled203a7e2005-09-06 15:19:37 -07002515 return IRQ_HANDLED;
2516}
2517
2518/* VINO video input management */
2519
2520static int vino_get_saa7191_input(int input)
2521{
2522 switch (input) {
2523 case VINO_INPUT_COMPOSITE:
2524 return SAA7191_INPUT_COMPOSITE;
2525 case VINO_INPUT_SVIDEO:
2526 return SAA7191_INPUT_SVIDEO;
2527 default:
2528 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2529 "invalid input!\n");
2530 return -1;
2531 }
2532}
2533
Ralf Baechled203a7e2005-09-06 15:19:37 -07002534/* execute with input_lock locked */
2535static int vino_is_input_owner(struct vino_channel_settings *vcs)
2536{
2537 switch(vcs->input) {
2538 case VINO_INPUT_COMPOSITE:
2539 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002540 return vino_drvdata->decoder_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002541 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002542 return vino_drvdata->camera_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002543 default:
2544 return 0;
2545 }
2546}
2547
2548static int vino_acquire_input(struct vino_channel_settings *vcs)
2549{
Ladislav Michla637a112005-09-01 15:07:34 +00002550 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002551 int ret = 0;
2552
2553 dprintk("vino_acquire_input():\n");
2554
Ladislav Michla637a112005-09-01 15:07:34 +00002555 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002556
2557 /* First try D1 and then SAA7191 */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002558 if (vino_drvdata->camera
2559 && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2560 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002561 vcs->input = VINO_INPUT_D1;
2562 vcs->data_norm = VINO_DATA_NORM_D1;
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002563 } else if (vino_drvdata->decoder
2564 && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002565 int input;
2566 int data_norm;
2567 v4l2_std_id norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002568
Ladislav Michla637a112005-09-01 15:07:34 +00002569 input = VINO_INPUT_COMPOSITE;
2570
Hans Verkuil5325b422009-04-02 11:26:22 -03002571 ret = decoder_call(video, s_routing,
2572 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002573 if (ret) {
2574 ret = -EINVAL;
2575 goto out;
2576 }
2577
2578 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2579
2580 /* Don't hold spinlocks while auto-detecting norm
2581 * as it may take a while... */
2582
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002583 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002584 if (!ret) {
2585 for (data_norm = 0; data_norm < 3; data_norm++) {
2586 if (vino_data_norms[data_norm].std & norm)
2587 break;
2588 }
2589 if (data_norm == 3)
2590 data_norm = VINO_DATA_NORM_PAL;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002591 ret = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002592 }
2593
2594 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2595
2596 if (ret) {
2597 ret = -EINVAL;
2598 goto out;
2599 }
2600
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002601 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002602
Ladislav Michla637a112005-09-01 15:07:34 +00002603 vcs->input = input;
2604 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002605 } else {
2606 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2607 vino_drvdata->b.input : vino_drvdata->a.input;
2608 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2609 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2610 }
2611
2612 if (vcs->input == VINO_INPUT_NONE) {
2613 ret = -ENODEV;
2614 goto out;
2615 }
2616
Ladislav Michla637a112005-09-01 15:07:34 +00002617 vino_set_default_clipping(vcs);
2618 vino_set_default_scaling(vcs);
2619 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002620
2621 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2622
2623out:
Ladislav Michla637a112005-09-01 15:07:34 +00002624 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002625
2626 return ret;
2627}
2628
2629static int vino_set_input(struct vino_channel_settings *vcs, int input)
2630{
2631 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2632 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002633 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002634 int ret = 0;
2635
2636 dprintk("vino_set_input():\n");
2637
Ladislav Michla637a112005-09-01 15:07:34 +00002638 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002639
2640 if (vcs->input == input)
2641 goto out;
2642
Ladislav Michla637a112005-09-01 15:07:34 +00002643 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002644 case VINO_INPUT_COMPOSITE:
2645 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002646 if (!vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002647 ret = -EINVAL;
2648 goto out;
2649 }
2650
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002651 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2652 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002653 }
2654
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002655 if (vino_drvdata->decoder_owner == vcs->channel) {
Ladislav Michla637a112005-09-01 15:07:34 +00002656 int data_norm;
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002657 v4l2_std_id norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002658
Hans Verkuil5325b422009-04-02 11:26:22 -03002659 ret = decoder_call(video, s_routing,
2660 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002661 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002662 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002663 ret = -EINVAL;
2664 goto out;
2665 }
2666
2667 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2668
2669 /* Don't hold spinlocks while auto-detecting norm
2670 * as it may take a while... */
2671
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002672 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002673 if (!ret) {
2674 for (data_norm = 0; data_norm < 3; data_norm++) {
2675 if (vino_data_norms[data_norm].std & norm)
2676 break;
2677 }
2678 if (data_norm == 3)
2679 data_norm = VINO_DATA_NORM_PAL;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002680 ret = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002681 }
2682
2683 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2684
2685 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002686 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002687 ret = -EINVAL;
2688 goto out;
2689 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002690
2691 vcs->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +00002692 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002693 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00002694 if (input != vcs2->input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002695 ret = -EBUSY;
2696 goto out;
2697 }
2698
2699 vcs->input = input;
2700 vcs->data_norm = vcs2->data_norm;
2701 }
2702
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002703 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002704 /* Transfer the ownership or release the input */
2705 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002706 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002707 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002708 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002709 }
2710 }
2711 break;
2712 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002713 if (!vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002714 ret = -EINVAL;
2715 goto out;
2716 }
2717
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002718 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2719 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002720
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002721 if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002722 /* Transfer the ownership or release the input */
2723 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2724 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002725 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002726 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002727 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002728 }
2729 }
2730
2731 vcs->input = input;
2732 vcs->data_norm = VINO_DATA_NORM_D1;
2733 break;
2734 default:
2735 ret = -EINVAL;
2736 goto out;
2737 }
2738
2739 vino_set_default_clipping(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002740 vino_set_default_scaling(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002741 vino_set_default_framerate(vcs);
2742
2743 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2744
2745out:
Ladislav Michla637a112005-09-01 15:07:34 +00002746 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002747
2748 return ret;
2749}
2750
2751static void vino_release_input(struct vino_channel_settings *vcs)
2752{
2753 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2754 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002755 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002756
2757 dprintk("vino_release_input():\n");
2758
Ladislav Michla637a112005-09-01 15:07:34 +00002759 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002760
2761 /* Release ownership of the channel
2762 * and if the other channel takes input from
2763 * the same source, transfer the ownership */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002764 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002765 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002766 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002767 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002768 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002769 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002770 } else if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002771 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2772 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002773 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002774 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002775 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002776 }
2777 }
2778 vcs->input = VINO_INPUT_NONE;
2779
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
2783/* execute with input_lock locked */
2784static int vino_set_data_norm(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00002785 unsigned int data_norm,
2786 unsigned long *flags)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002787{
Ladislav Michla637a112005-09-01 15:07:34 +00002788 int err = 0;
2789
2790 if (data_norm == vcs->data_norm)
2791 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002792
2793 switch (vcs->input) {
2794 case VINO_INPUT_D1:
2795 /* only one "norm" supported */
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002796 if (data_norm != VINO_DATA_NORM_D1)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002797 return -EINVAL;
2798 break;
2799 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00002800 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002801 v4l2_std_id norm;
2802
Ladislav Michla637a112005-09-01 15:07:34 +00002803 if ((data_norm != VINO_DATA_NORM_PAL)
2804 && (data_norm != VINO_DATA_NORM_NTSC)
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002805 && (data_norm != VINO_DATA_NORM_SECAM))
Ladislav Michla637a112005-09-01 15:07:34 +00002806 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002807
Ladislav Michla637a112005-09-01 15:07:34 +00002808 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002809
Ladislav Michla637a112005-09-01 15:07:34 +00002810 /* Don't hold spinlocks while setting norm
2811 * as it may take a while... */
2812
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002813 norm = vino_data_norms[data_norm].std;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002814 err = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002815
2816 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2817
2818 if (err)
2819 goto out;
2820
Ralf Baechled203a7e2005-09-06 15:19:37 -07002821 vcs->data_norm = data_norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002822
2823 vino_set_default_clipping(vcs);
2824 vino_set_default_scaling(vcs);
2825 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002826 break;
Ladislav Michla637a112005-09-01 15:07:34 +00002827 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002828 default:
2829 return -EINVAL;
2830 }
2831
Ladislav Michla637a112005-09-01 15:07:34 +00002832out:
2833 return err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002834}
2835
2836/* V4L2 helper functions */
2837
2838static int vino_find_data_format(__u32 pixelformat)
2839{
2840 int i;
2841
2842 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2843 if (vino_data_formats[i].pixelformat == pixelformat)
2844 return i;
2845 }
2846
2847 return VINO_DATA_FMT_NONE;
2848}
2849
Hans Verkuilc49cb362009-02-12 10:32:50 -03002850static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002851{
2852 int input = VINO_INPUT_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002853 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002854
Ladislav Michla637a112005-09-01 15:07:34 +00002855 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002856 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002857 switch (index) {
2858 case 0:
2859 input = VINO_INPUT_COMPOSITE;
2860 break;
2861 case 1:
2862 input = VINO_INPUT_SVIDEO;
2863 break;
2864 case 2:
2865 input = VINO_INPUT_D1;
2866 break;
2867 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002868 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002869 switch (index) {
2870 case 0:
2871 input = VINO_INPUT_COMPOSITE;
2872 break;
2873 case 1:
2874 input = VINO_INPUT_SVIDEO;
2875 break;
2876 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002877 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002878 switch (index) {
2879 case 0:
2880 input = VINO_INPUT_D1;
2881 break;
2882 }
2883 }
Ladislav Michla637a112005-09-01 15:07:34 +00002884 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002885
2886 return input;
2887}
2888
2889/* execute with input_lock locked */
2890static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2891{
2892 __u32 index = 0;
2893 // FIXME: detect when no inputs available
2894
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002895 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002896 switch (vcs->input) {
2897 case VINO_INPUT_COMPOSITE:
2898 index = 0;
2899 break;
2900 case VINO_INPUT_SVIDEO:
2901 index = 1;
2902 break;
2903 case VINO_INPUT_D1:
2904 index = 2;
2905 break;
2906 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002907 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002908 switch (vcs->input) {
2909 case VINO_INPUT_COMPOSITE:
2910 index = 0;
2911 break;
2912 case VINO_INPUT_SVIDEO:
2913 index = 1;
2914 break;
2915 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002916 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002917 switch (vcs->input) {
2918 case VINO_INPUT_D1:
2919 index = 0;
2920 break;
2921 }
2922 }
2923
2924 return index;
2925}
2926
2927/* V4L2 ioctls */
2928
Hans Verkuilc49cb362009-02-12 10:32:50 -03002929static int vino_querycap(struct file *file, void *__fh,
2930 struct v4l2_capability *cap)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002931{
2932 memset(cap, 0, sizeof(struct v4l2_capability));
2933
2934 strcpy(cap->driver, vino_driver_name);
2935 strcpy(cap->card, vino_driver_description);
2936 strcpy(cap->bus_info, vino_bus_name);
2937 cap->version = VINO_VERSION_CODE;
2938 cap->capabilities =
2939 V4L2_CAP_VIDEO_CAPTURE |
2940 V4L2_CAP_STREAMING;
2941 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
Hans Verkuilc49cb362009-02-12 10:32:50 -03002942 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002943}
2944
Hans Verkuilc49cb362009-02-12 10:32:50 -03002945static int vino_enum_input(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07002946 struct v4l2_input *i)
2947{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002948 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002949 __u32 index = i->index;
2950 int input;
2951 dprintk("requested index = %d\n", index);
2952
Hans Verkuilc49cb362009-02-12 10:32:50 -03002953 input = vino_int_enum_input(vcs, index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002954 if (input == VINO_INPUT_NONE)
2955 return -EINVAL;
2956
Ralf Baechled203a7e2005-09-06 15:19:37 -07002957 i->type = V4L2_INPUT_TYPE_CAMERA;
2958 i->std = vino_inputs[input].std;
2959 strcpy(i->name, vino_inputs[input].name);
2960
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002961 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002962 decoder_call(video, g_input_status, &i->status);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002963 return 0;
2964}
2965
Hans Verkuilc49cb362009-02-12 10:32:50 -03002966static int vino_g_input(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00002967 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002968{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002969 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002970 __u32 index;
2971 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00002972 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002973
Ladislav Michla637a112005-09-01 15:07:34 +00002974 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002975 input = vcs->input;
2976 index = vino_find_input_index(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002977 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002978
2979 dprintk("input = %d\n", input);
2980
2981 if (input == VINO_INPUT_NONE) {
2982 return -EINVAL;
2983 }
2984
Ladislav Michla637a112005-09-01 15:07:34 +00002985 *i = index;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002986
2987 return 0;
2988}
2989
Hans Verkuilc49cb362009-02-12 10:32:50 -03002990static int vino_s_input(struct file *file, void *__fh,
2991 unsigned int i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002992{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002993 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002994 int input;
Hans Verkuilc49cb362009-02-12 10:32:50 -03002995 dprintk("requested input = %d\n", i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002996
Hans Verkuilc49cb362009-02-12 10:32:50 -03002997 input = vino_int_enum_input(vcs, i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002998 if (input == VINO_INPUT_NONE)
2999 return -EINVAL;
3000
3001 return vino_set_input(vcs, input);
3002}
3003
Hans Verkuilc49cb362009-02-12 10:32:50 -03003004static int vino_querystd(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00003005 v4l2_std_id *std)
3006{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003007 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003008 unsigned long flags;
3009 int err = 0;
3010
3011 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3012
3013 switch (vcs->input) {
3014 case VINO_INPUT_D1:
3015 *std = vino_inputs[vcs->input].std;
3016 break;
3017 case VINO_INPUT_COMPOSITE:
3018 case VINO_INPUT_SVIDEO: {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003019 decoder_call(video, querystd, std);
Ladislav Michla637a112005-09-01 15:07:34 +00003020 break;
3021 }
3022 default:
3023 err = -EINVAL;
3024 }
3025
3026 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3027
3028 return err;
3029}
3030
Hans Verkuilc49cb362009-02-12 10:32:50 -03003031static int vino_g_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003032 v4l2_std_id *std)
3033{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003034 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003035 unsigned long flags;
3036
3037 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3038
Ralf Baechled203a7e2005-09-06 15:19:37 -07003039 *std = vino_data_norms[vcs->data_norm].std;
Ladislav Michla637a112005-09-01 15:07:34 +00003040 dprintk("current standard = %d\n", vcs->data_norm);
3041
3042 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003043
3044 return 0;
3045}
3046
Hans Verkuilc49cb362009-02-12 10:32:50 -03003047static int vino_s_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003048 v4l2_std_id *std)
3049{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003050 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003051 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003052 int ret = 0;
3053
Ladislav Michla637a112005-09-01 15:07:34 +00003054 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3055
3056 if (!vino_is_input_owner(vcs)) {
3057 ret = -EBUSY;
3058 goto out;
3059 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003060
3061 /* check if the standard is valid for the current input */
Ladislav Michla637a112005-09-01 15:07:34 +00003062 if ((*std) & vino_inputs[vcs->input].std) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003063 dprintk("standard accepted\n");
3064
3065 /* change the video norm for SAA7191
3066 * and accept NTSC for D1 (do nothing) */
3067
3068 if (vcs->input == VINO_INPUT_D1)
3069 goto out;
3070
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003071 if ((*std) & V4L2_STD_PAL) {
Ladislav Michla637a112005-09-01 15:07:34 +00003072 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3073 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003074 } else if ((*std) & V4L2_STD_NTSC) {
Ladislav Michla637a112005-09-01 15:07:34 +00003075 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3076 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003077 } else if ((*std) & V4L2_STD_SECAM) {
Ladislav Michla637a112005-09-01 15:07:34 +00003078 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3079 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003080 } else {
3081 ret = -EINVAL;
3082 }
Ladislav Michla637a112005-09-01 15:07:34 +00003083
3084 if (ret) {
3085 ret = -EINVAL;
3086 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003087 } else {
3088 ret = -EINVAL;
3089 }
3090
3091out:
Ladislav Michla637a112005-09-01 15:07:34 +00003092 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003093
3094 return ret;
3095}
3096
Hans Verkuilc49cb362009-02-12 10:32:50 -03003097static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003098 struct v4l2_fmtdesc *fd)
3099{
Trent Piepho4f5a7442009-03-28 22:25:36 -03003100 dprintk("format index = %d\n", fd->index);
Hans Verkuilc49cb362009-02-12 10:32:50 -03003101
Trent Piepho4f5a7442009-03-28 22:25:36 -03003102 if (fd->index >= VINO_DATA_FMT_COUNT)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003103 return -EINVAL;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003104 dprintk("format name = %s\n", vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003105
Trent Piepho4f5a7442009-03-28 22:25:36 -03003106 fd->pixelformat = vino_data_formats[fd->index].pixelformat;
3107 strcpy(fd->description, vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003108 return 0;
3109}
3110
Hans Verkuilc49cb362009-02-12 10:32:50 -03003111static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003112 struct v4l2_format *f)
3113{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003114 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003115 struct vino_channel_settings tempvcs;
Ladislav Michla637a112005-09-01 15:07:34 +00003116 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003117 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003118
Hans Verkuilc49cb362009-02-12 10:32:50 -03003119 dprintk("requested: w = %d, h = %d\n",
3120 pf->width, pf->height);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003121
Hans Verkuilc49cb362009-02-12 10:32:50 -03003122 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3123 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3124 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003125
Hans Verkuilc49cb362009-02-12 10:32:50 -03003126 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3127 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3128 tempvcs.data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003129 pf->pixelformat =
Hans Verkuilc49cb362009-02-12 10:32:50 -03003130 vino_data_formats[tempvcs.data_format].
3131 pixelformat;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003132 }
3133
Hans Verkuilc49cb362009-02-12 10:32:50 -03003134 /* data format must be set before clipping/scaling */
3135 vino_set_scaling(&tempvcs, pf->width, pf->height);
3136
3137 dprintk("data format = %s\n",
3138 vino_data_formats[tempvcs.data_format].description);
3139
3140 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3141 tempvcs.decimation;
3142 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3143 tempvcs.decimation;
3144
3145 pf->field = V4L2_FIELD_INTERLACED;
3146 pf->bytesperline = tempvcs.line_size;
3147 pf->sizeimage = tempvcs.line_size *
3148 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3149 tempvcs.decimation;
3150 pf->colorspace =
3151 vino_data_formats[tempvcs.data_format].colorspace;
3152
3153 pf->priv = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003154 return 0;
3155}
3156
Hans Verkuilc49cb362009-02-12 10:32:50 -03003157static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003158 struct v4l2_format *f)
3159{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003160 struct vino_channel_settings *vcs = video_drvdata(file);
3161 unsigned long flags;
3162 struct v4l2_pix_format *pf = &f->fmt.pix;
3163
3164 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3165
3166 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3167 vcs->decimation;
3168 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3169 vcs->decimation;
3170 pf->pixelformat =
3171 vino_data_formats[vcs->data_format].pixelformat;
3172
3173 pf->field = V4L2_FIELD_INTERLACED;
3174 pf->bytesperline = vcs->line_size;
3175 pf->sizeimage = vcs->line_size *
3176 (vcs->clipping.bottom - vcs->clipping.top) /
3177 vcs->decimation;
3178 pf->colorspace =
3179 vino_data_formats[vcs->data_format].colorspace;
3180
3181 pf->priv = 0;
3182
3183 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3184 return 0;
3185}
3186
3187static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3188 struct v4l2_format *f)
3189{
3190 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003191 int data_format;
Ladislav Michla637a112005-09-01 15:07:34 +00003192 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003193 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003194
Hans Verkuilc49cb362009-02-12 10:32:50 -03003195 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003196
Hans Verkuilc49cb362009-02-12 10:32:50 -03003197 data_format = vino_find_data_format(pf->pixelformat);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003198
Hans Verkuilc49cb362009-02-12 10:32:50 -03003199 if (data_format == VINO_DATA_FMT_NONE) {
3200 vcs->data_format = VINO_DATA_FMT_GREY;
3201 pf->pixelformat =
3202 vino_data_formats[vcs->data_format].
3203 pixelformat;
3204 } else {
3205 vcs->data_format = data_format;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003206 }
3207
Hans Verkuilc49cb362009-02-12 10:32:50 -03003208 /* data format must be set before clipping/scaling */
3209 vino_set_scaling(vcs, pf->width, pf->height);
3210
3211 dprintk("data format = %s\n",
3212 vino_data_formats[vcs->data_format].description);
3213
3214 pf->width = vcs->clipping.right - vcs->clipping.left;
3215 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3216
3217 pf->field = V4L2_FIELD_INTERLACED;
3218 pf->bytesperline = vcs->line_size;
3219 pf->sizeimage = vcs->line_size *
3220 (vcs->clipping.bottom - vcs->clipping.top) /
3221 vcs->decimation;
3222 pf->colorspace =
3223 vino_data_formats[vcs->data_format].colorspace;
3224
3225 pf->priv = 0;
3226
3227 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003228 return 0;
3229}
3230
Hans Verkuilc49cb362009-02-12 10:32:50 -03003231static int vino_cropcap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003232 struct v4l2_cropcap *ccap)
3233{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003234 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003235 const struct vino_data_norm *norm;
Ladislav Michla637a112005-09-01 15:07:34 +00003236 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003237
3238 switch (ccap->type) {
3239 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003240 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3241
Ralf Baechled203a7e2005-09-06 15:19:37 -07003242 norm = &vino_data_norms[vcs->data_norm];
Ladislav Michla637a112005-09-01 15:07:34 +00003243
3244 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003245
3246 ccap->bounds.left = 0;
3247 ccap->bounds.top = 0;
3248 ccap->bounds.width = norm->width;
3249 ccap->bounds.height = norm->height;
3250 memcpy(&ccap->defrect, &ccap->bounds,
3251 sizeof(struct v4l2_rect));
3252
3253 ccap->pixelaspect.numerator = 1;
3254 ccap->pixelaspect.denominator = 1;
3255 break;
3256 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3257 default:
3258 return -EINVAL;
3259 }
3260
3261 return 0;
3262}
3263
Hans Verkuilc49cb362009-02-12 10:32:50 -03003264static int vino_g_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003265 struct v4l2_crop *c)
3266{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003267 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003268 unsigned long flags;
3269
Ralf Baechled203a7e2005-09-06 15:19:37 -07003270 switch (c->type) {
3271 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003272 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003273
3274 c->c.left = vcs->clipping.left;
3275 c->c.top = vcs->clipping.top;
3276 c->c.width = vcs->clipping.right - vcs->clipping.left;
3277 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3278
Ladislav Michla637a112005-09-01 15:07:34 +00003279 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003280 break;
3281 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3282 default:
3283 return -EINVAL;
3284 }
3285
3286 return 0;
3287}
3288
Hans Verkuilc49cb362009-02-12 10:32:50 -03003289static int vino_s_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003290 struct v4l2_crop *c)
3291{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003292 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003293 unsigned long flags;
3294
Ralf Baechled203a7e2005-09-06 15:19:37 -07003295 switch (c->type) {
3296 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003297 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003298
Ralf Baechled203a7e2005-09-06 15:19:37 -07003299 vino_set_clipping(vcs, c->c.left, c->c.top,
3300 c->c.width, c->c.height);
3301
Ladislav Michla637a112005-09-01 15:07:34 +00003302 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003303 break;
3304 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3305 default:
3306 return -EINVAL;
3307 }
3308
3309 return 0;
3310}
3311
Hans Verkuilc49cb362009-02-12 10:32:50 -03003312static int vino_g_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003313 struct v4l2_streamparm *sp)
3314{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003315 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003316 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003317 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003318
Trent Piepho4f5a7442009-03-28 22:25:36 -03003319 cp->capability = V4L2_CAP_TIMEPERFRAME;
3320 cp->timeperframe.numerator = 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003321
Trent Piepho4f5a7442009-03-28 22:25:36 -03003322 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003323
Trent Piepho4f5a7442009-03-28 22:25:36 -03003324 cp->timeperframe.denominator = vcs->fps;
Ladislav Michla637a112005-09-01 15:07:34 +00003325
Trent Piepho4f5a7442009-03-28 22:25:36 -03003326 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00003327
Trent Piepho4f5a7442009-03-28 22:25:36 -03003328 /* TODO: cp->readbuffers = xxx; */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003329
3330 return 0;
3331}
3332
Hans Verkuilc49cb362009-02-12 10:32:50 -03003333static int vino_s_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003334 struct v4l2_streamparm *sp)
3335{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003336 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003337 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003338 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003339
Trent Piepho4f5a7442009-03-28 22:25:36 -03003340 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003341
Trent Piepho4f5a7442009-03-28 22:25:36 -03003342 if ((cp->timeperframe.numerator == 0) ||
3343 (cp->timeperframe.denominator == 0)) {
3344 /* reset framerate */
3345 vino_set_default_framerate(vcs);
3346 } else {
3347 vino_set_framerate(vcs, cp->timeperframe.denominator /
3348 cp->timeperframe.numerator);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003349 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003350
3351 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003352
3353 return 0;
3354}
3355
Hans Verkuilc49cb362009-02-12 10:32:50 -03003356static int vino_reqbufs(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003357 struct v4l2_requestbuffers *rb)
3358{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003359 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003360
Ralf Baechled203a7e2005-09-06 15:19:37 -07003361 if (vcs->reading)
3362 return -EBUSY;
3363
Trent Piepho4f5a7442009-03-28 22:25:36 -03003364 /* TODO: check queue type */
3365 if (rb->memory != V4L2_MEMORY_MMAP) {
3366 dprintk("type not mmap\n");
3367 return -EINVAL;
3368 }
3369
3370 dprintk("count = %d\n", rb->count);
3371 if (rb->count > 0) {
3372 if (vino_is_capturing(vcs)) {
3373 dprintk("busy, capturing\n");
3374 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003375 }
3376
Trent Piepho4f5a7442009-03-28 22:25:36 -03003377 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3378 dprintk("busy, buffers still mapped\n");
3379 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003380 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003381 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003382 vino_queue_free(&vcs->fb_queue);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003383 vino_queue_init(&vcs->fb_queue, &rb->count);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003384 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003385 } else {
3386 vcs->streaming = 0;
3387 vino_capture_stop(vcs);
3388 vino_queue_free(&vcs->fb_queue);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003389 }
3390
3391 return 0;
3392}
3393
3394static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3395 struct vino_framebuffer *fb,
3396 struct v4l2_buffer *b)
3397{
3398 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3399 fb->id)) {
3400 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3401 b->flags |= V4L2_BUF_FLAG_DONE;
3402 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3403 fb->id)) {
3404 b->flags &= ~V4L2_BUF_FLAG_DONE;
3405 b->flags |= V4L2_BUF_FLAG_QUEUED;
3406 } else {
3407 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3408 V4L2_BUF_FLAG_QUEUED);
3409 }
3410
3411 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3412
3413 if (fb->map_count > 0)
3414 b->flags |= V4L2_BUF_FLAG_MAPPED;
3415
3416 b->index = fb->id;
3417 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3418 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3419 b->m.offset = fb->offset;
3420 b->bytesused = fb->data_size;
3421 b->length = fb->size;
3422 b->field = V4L2_FIELD_INTERLACED;
3423 b->sequence = fb->frame_counter;
3424 memcpy(&b->timestamp, &fb->timestamp,
3425 sizeof(struct timeval));
3426 // b->input ?
3427
3428 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3429 fb->id, fb->size, fb->data_size, fb->offset);
3430}
3431
Hans Verkuilc49cb362009-02-12 10:32:50 -03003432static int vino_querybuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003433 struct v4l2_buffer *b)
3434{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003435 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003436 struct vino_framebuffer *fb;
3437
Ralf Baechled203a7e2005-09-06 15:19:37 -07003438 if (vcs->reading)
3439 return -EBUSY;
3440
Trent Piepho4f5a7442009-03-28 22:25:36 -03003441 /* TODO: check queue type */
3442 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3443 dprintk("invalid index = %d\n",
3444 b->index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003445 return -EINVAL;
3446 }
3447
Trent Piepho4f5a7442009-03-28 22:25:36 -03003448 fb = vino_queue_get_buffer(&vcs->fb_queue,
3449 b->index);
3450 if (fb == NULL) {
3451 dprintk("vino_queue_get_buffer() failed");
3452 return -EINVAL;
3453 }
3454
3455 vino_v4l2_get_buffer_status(vcs, fb, b);
3456
Ralf Baechled203a7e2005-09-06 15:19:37 -07003457 return 0;
3458}
3459
Hans Verkuilc49cb362009-02-12 10:32:50 -03003460static int vino_qbuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003461 struct v4l2_buffer *b)
3462{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003463 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003464 struct vino_framebuffer *fb;
3465 int ret;
3466
Ralf Baechled203a7e2005-09-06 15:19:37 -07003467 if (vcs->reading)
3468 return -EBUSY;
3469
Trent Piepho4f5a7442009-03-28 22:25:36 -03003470 /* TODO: check queue type */
3471 if (b->memory != V4L2_MEMORY_MMAP) {
3472 dprintk("type not mmap\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003473 return -EINVAL;
3474 }
3475
Trent Piepho4f5a7442009-03-28 22:25:36 -03003476 fb = vino_capture_enqueue(vcs, b->index);
3477 if (fb == NULL)
3478 return -EINVAL;
3479
3480 vino_v4l2_get_buffer_status(vcs, fb, b);
3481
3482 if (vcs->streaming) {
3483 ret = vino_capture_next(vcs, 1);
3484 if (ret)
3485 return ret;
3486 }
3487
Ralf Baechled203a7e2005-09-06 15:19:37 -07003488 return 0;
3489}
3490
Hans Verkuilc49cb362009-02-12 10:32:50 -03003491static int vino_dqbuf(struct file *file, void *__fh,
3492 struct v4l2_buffer *b)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003493{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003494 struct vino_channel_settings *vcs = video_drvdata(file);
3495 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003496 struct vino_framebuffer *fb;
3497 unsigned int incoming, outgoing;
3498 int err;
3499
Ralf Baechled203a7e2005-09-06 15:19:37 -07003500 if (vcs->reading)
3501 return -EBUSY;
3502
Trent Piepho4f5a7442009-03-28 22:25:36 -03003503 /* TODO: check queue type */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003504
Trent Piepho4f5a7442009-03-28 22:25:36 -03003505 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3506 if (err) {
3507 dprintk("vino_queue_get_incoming() failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003508 return -EINVAL;
3509 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003510 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3511 if (err) {
3512 dprintk("vino_queue_get_outgoing() failed\n");
3513 return -EINVAL;
3514 }
3515
3516 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3517
3518 if (outgoing == 0) {
3519 if (incoming == 0) {
3520 dprintk("no incoming or outgoing buffers\n");
3521 return -EINVAL;
3522 }
3523 if (nonblocking) {
3524 dprintk("non-blocking I/O was selected and "
3525 "there are no buffers to dequeue\n");
3526 return -EAGAIN;
3527 }
3528
3529 err = vino_wait_for_frame(vcs);
3530 if (err) {
3531 err = vino_wait_for_frame(vcs);
3532 if (err) {
3533 /* interrupted or no frames captured because of
3534 * frame skipping */
3535 /* vino_capture_failed(vcs); */
3536 return -EIO;
3537 }
3538 }
3539 }
3540
3541 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3542 if (fb == NULL) {
3543 dprintk("vino_queue_remove() failed\n");
3544 return -EINVAL;
3545 }
3546
3547 err = vino_check_buffer(vcs, fb);
3548
3549 vino_v4l2_get_buffer_status(vcs, fb, b);
3550
3551 if (err)
3552 return -EIO;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003553
3554 return 0;
3555}
3556
Hans Verkuilc49cb362009-02-12 10:32:50 -03003557static int vino_streamon(struct file *file, void *__fh,
3558 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003559{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003560 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003561 unsigned int incoming;
3562 int ret;
3563 if (vcs->reading)
3564 return -EBUSY;
3565
3566 if (vcs->streaming)
3567 return 0;
3568
3569 // TODO: check queue type
3570
3571 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3572 dprintk("no buffers allocated\n");
3573 return -EINVAL;
3574 }
3575
3576 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3577 if (ret) {
3578 dprintk("vino_queue_get_incoming() failed\n");
3579 return -EINVAL;
3580 }
3581
3582 vcs->streaming = 1;
3583
3584 if (incoming > 0) {
3585 ret = vino_capture_next(vcs, 1);
3586 if (ret) {
3587 vcs->streaming = 0;
3588
3589 dprintk("couldn't start capture\n");
3590 return -EINVAL;
3591 }
3592 }
3593
3594 return 0;
3595}
3596
Hans Verkuilc49cb362009-02-12 10:32:50 -03003597static int vino_streamoff(struct file *file, void *__fh,
3598 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003599{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003600 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003601 if (vcs->reading)
3602 return -EBUSY;
3603
3604 if (!vcs->streaming)
3605 return 0;
3606
Ralf Baechled203a7e2005-09-06 15:19:37 -07003607 vcs->streaming = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00003608 vino_capture_stop(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003609
3610 return 0;
3611}
3612
Hans Verkuilc49cb362009-02-12 10:32:50 -03003613static int vino_queryctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003614 struct v4l2_queryctrl *queryctrl)
3615{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003616 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003617 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003618 int i;
3619 int err = 0;
3620
Ladislav Michla637a112005-09-01 15:07:34 +00003621 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003622
3623 switch (vcs->input) {
3624 case VINO_INPUT_D1:
3625 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3626 if (vino_indycam_v4l2_controls[i].id ==
3627 queryctrl->id) {
3628 memcpy(queryctrl,
3629 &vino_indycam_v4l2_controls[i],
3630 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003631 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003632 goto found;
3633 }
3634 }
3635
3636 err = -EINVAL;
3637 break;
3638 case VINO_INPUT_COMPOSITE:
3639 case VINO_INPUT_SVIDEO:
3640 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3641 if (vino_saa7191_v4l2_controls[i].id ==
3642 queryctrl->id) {
3643 memcpy(queryctrl,
3644 &vino_saa7191_v4l2_controls[i],
3645 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003646 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003647 goto found;
3648 }
3649 }
3650
3651 err = -EINVAL;
3652 break;
3653 default:
3654 err = -EINVAL;
3655 }
3656
3657 found:
Ladislav Michla637a112005-09-01 15:07:34 +00003658 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003659
3660 return err;
3661}
3662
Hans Verkuilc49cb362009-02-12 10:32:50 -03003663static int vino_g_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003664 struct v4l2_control *control)
3665{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003666 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003667 unsigned long flags;
3668 int i;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003669 int err = 0;
3670
Ladislav Michla637a112005-09-01 15:07:34 +00003671 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003672
3673 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003674 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003675 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003676 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003677 if (vino_indycam_v4l2_controls[i].id == control->id) {
3678 err = 0;
3679 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003680 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003681 }
Ladislav Michla637a112005-09-01 15:07:34 +00003682
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003683 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003684 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003685
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003686 err = camera_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003687 if (err)
3688 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003689 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003690 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003691 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003692 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003693 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003694 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003695 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3696 err = 0;
3697 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003698 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003699 }
Ladislav Michla637a112005-09-01 15:07:34 +00003700
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003701 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003702 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003703
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003704 err = decoder_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003705 if (err)
3706 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003707 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003708 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003709 default:
3710 err = -EINVAL;
3711 }
3712
Ladislav Michla637a112005-09-01 15:07:34 +00003713out:
3714 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003715
3716 return err;
3717}
3718
Hans Verkuilc49cb362009-02-12 10:32:50 -03003719static int vino_s_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003720 struct v4l2_control *control)
3721{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003722 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003723 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003724 int i;
3725 int err = 0;
3726
Ladislav Michla637a112005-09-01 15:07:34 +00003727 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3728
3729 if (!vino_is_input_owner(vcs)) {
3730 err = -EBUSY;
3731 goto out;
3732 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003733
3734 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003735 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003736 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003737 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003738 if (vino_indycam_v4l2_controls[i].id == control->id) {
3739 err = 0;
3740 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003741 }
3742 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003743 if (err)
3744 goto out;
3745 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3746 control->value > vino_indycam_v4l2_controls[i].maximum) {
3747 err = -ERANGE;
3748 goto out;
3749 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003750 err = camera_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003751 if (err)
3752 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003753 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003754 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003755 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003756 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003757 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003758 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003759 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3760 err = 0;
3761 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003762 }
3763 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003764 if (err)
3765 goto out;
3766 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3767 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3768 err = -ERANGE;
3769 goto out;
3770 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003771
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003772 err = decoder_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003773 if (err)
3774 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003775 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003776 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003777 default:
3778 err = -EINVAL;
3779 }
3780
Ladislav Michla637a112005-09-01 15:07:34 +00003781out:
3782 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003783
3784 return err;
3785}
3786
3787/* File operations */
3788
Hans Verkuilbec43662008-12-30 06:58:20 -03003789static int vino_open(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003790{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003791 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003792 int ret = 0;
3793 dprintk("open(): channel = %c\n",
3794 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3795
Ingo Molnar3593cab2006-02-07 06:49:14 -02003796 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003797
3798 if (vcs->users) {
3799 dprintk("open(): driver busy\n");
3800 ret = -EBUSY;
3801 goto out;
3802 }
3803
3804 ret = vino_acquire_input(vcs);
3805 if (ret) {
3806 dprintk("open(): vino_acquire_input() failed\n");
3807 goto out;
3808 }
3809
3810 vcs->users++;
3811
3812 out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003813 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003814
3815 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3816
3817 return ret;
3818}
3819
Hans Verkuilbec43662008-12-30 06:58:20 -03003820static int vino_close(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003821{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003822 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003823 dprintk("close():\n");
3824
Ingo Molnar3593cab2006-02-07 06:49:14 -02003825 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003826
3827 vcs->users--;
3828
3829 if (!vcs->users) {
3830 vino_release_input(vcs);
3831
3832 /* stop DMA and free buffers */
3833 vino_capture_stop(vcs);
3834 vino_queue_free(&vcs->fb_queue);
3835 }
3836
Ingo Molnar3593cab2006-02-07 06:49:14 -02003837 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003838
3839 return 0;
3840}
3841
3842static void vino_vm_open(struct vm_area_struct *vma)
3843{
3844 struct vino_framebuffer *fb = vma->vm_private_data;
3845
3846 fb->map_count++;
3847 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3848}
3849
3850static void vino_vm_close(struct vm_area_struct *vma)
3851{
3852 struct vino_framebuffer *fb = vma->vm_private_data;
3853
3854 fb->map_count--;
3855 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3856}
3857
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003858static const struct vm_operations_struct vino_vm_ops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003859 .open = vino_vm_open,
3860 .close = vino_vm_close,
3861};
3862
3863static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3864{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003865 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003866
3867 unsigned long start = vma->vm_start;
3868 unsigned long size = vma->vm_end - vma->vm_start;
3869 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3870
3871 struct vino_framebuffer *fb = NULL;
3872 unsigned int i, length;
3873 int ret = 0;
3874
3875 dprintk("mmap():\n");
3876
3877 // TODO: reject mmap if already mapped
3878
Ingo Molnar3593cab2006-02-07 06:49:14 -02003879 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07003880 return -EINTR;
3881
3882 if (vcs->reading) {
3883 ret = -EBUSY;
3884 goto out;
3885 }
3886
3887 // TODO: check queue type
3888
3889 if (!(vma->vm_flags & VM_WRITE)) {
3890 dprintk("mmap(): app bug: PROT_WRITE please\n");
3891 ret = -EINVAL;
3892 goto out;
3893 }
3894 if (!(vma->vm_flags & VM_SHARED)) {
3895 dprintk("mmap(): app bug: MAP_SHARED please\n");
3896 ret = -EINVAL;
3897 goto out;
3898 }
3899
3900 /* find the correct buffer using offset */
3901 length = vino_queue_get_length(&vcs->fb_queue);
3902 if (length == 0) {
3903 dprintk("mmap(): queue not initialized\n");
3904 ret = -EINVAL;
3905 goto out;
3906 }
3907
3908 for (i = 0; i < length; i++) {
3909 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3910 if (fb == NULL) {
3911 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3912 ret = -EINVAL;
3913 goto out;
3914 }
3915
3916 if (fb->offset == offset)
3917 goto found;
3918 }
3919
3920 dprintk("mmap(): invalid offset = %lu\n", offset);
3921 ret = -EINVAL;
3922 goto out;
3923
3924found:
3925 dprintk("mmap(): buffer = %d\n", i);
3926
3927 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3928 dprintk("mmap(): failed: size = %lu > %lu\n",
3929 size, fb->desc_table.page_count * PAGE_SIZE);
3930 ret = -EINVAL;
3931 goto out;
3932 }
3933
3934 for (i = 0; i < fb->desc_table.page_count; i++) {
3935 unsigned long pfn =
3936 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3937 PAGE_SHIFT;
3938
3939 if (size < PAGE_SIZE)
3940 break;
3941
3942 // protection was: PAGE_READONLY
3943 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3944 vma->vm_page_prot)) {
3945 dprintk("mmap(): remap_pfn_range() failed\n");
3946 ret = -EAGAIN;
3947 goto out;
3948 }
3949
3950 start += PAGE_SIZE;
3951 size -= PAGE_SIZE;
3952 }
3953
3954 fb->map_count = 1;
3955
3956 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3957 vma->vm_flags &= ~VM_IO;
3958 vma->vm_private_data = fb;
3959 vma->vm_file = file;
3960 vma->vm_ops = &vino_vm_ops;
3961
3962out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003963 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003964
3965 return ret;
3966}
3967
3968static unsigned int vino_poll(struct file *file, poll_table *pt)
3969{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003970 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003971 unsigned int outgoing;
3972 unsigned int ret = 0;
3973
3974 // lock mutex (?)
3975 // TODO: this has to be corrected for different read modes
3976
3977 dprintk("poll():\n");
3978
3979 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3980 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3981 ret = POLLERR;
3982 goto error;
3983 }
3984 if (outgoing > 0)
3985 goto over;
3986
3987 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3988
3989 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3990 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3991 ret = POLLERR;
3992 goto error;
3993 }
3994
3995over:
3996 dprintk("poll(): data %savailable\n",
3997 (outgoing > 0) ? "" : "not ");
Ladislav Michla637a112005-09-01 15:07:34 +00003998
3999 if (outgoing > 0)
Ralf Baechled203a7e2005-09-06 15:19:37 -07004000 ret = POLLIN | POLLRDNORM;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004001
4002error:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004003 return ret;
4004}
4005
Hans Verkuil069b7472008-12-30 07:04:34 -03004006static long vino_ioctl(struct file *file,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004007 unsigned int cmd, unsigned long arg)
4008{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004009 struct vino_channel_settings *vcs = video_drvdata(file);
Hans Verkuil069b7472008-12-30 07:04:34 -03004010 long ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004011
Ingo Molnar3593cab2006-02-07 06:49:14 -02004012 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004013 return -EINTR;
4014
Hans Verkuilc49cb362009-02-12 10:32:50 -03004015 ret = video_ioctl2(file, cmd, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004016
Ingo Molnar3593cab2006-02-07 06:49:14 -02004017 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004018
4019 return ret;
4020}
4021
4022/* Initialization and cleanup */
4023
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03004024/* __initdata */
4025static int vino_init_stage;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004026
Hans Verkuilc49cb362009-02-12 10:32:50 -03004027const struct v4l2_ioctl_ops vino_ioctl_ops = {
4028 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4029 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4030 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4031 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4032 .vidioc_querycap = vino_querycap,
4033 .vidioc_enum_input = vino_enum_input,
4034 .vidioc_g_input = vino_g_input,
4035 .vidioc_s_input = vino_s_input,
4036 .vidioc_g_std = vino_g_std,
4037 .vidioc_s_std = vino_s_std,
4038 .vidioc_querystd = vino_querystd,
4039 .vidioc_cropcap = vino_cropcap,
4040 .vidioc_s_crop = vino_s_crop,
4041 .vidioc_g_crop = vino_g_crop,
4042 .vidioc_s_parm = vino_s_parm,
4043 .vidioc_g_parm = vino_g_parm,
4044 .vidioc_reqbufs = vino_reqbufs,
4045 .vidioc_querybuf = vino_querybuf,
4046 .vidioc_qbuf = vino_qbuf,
4047 .vidioc_dqbuf = vino_dqbuf,
4048 .vidioc_streamon = vino_streamon,
4049 .vidioc_streamoff = vino_streamoff,
4050 .vidioc_queryctrl = vino_queryctrl,
4051 .vidioc_g_ctrl = vino_g_ctrl,
4052 .vidioc_s_ctrl = vino_s_ctrl,
4053};
4054
Hans Verkuilbec43662008-12-30 06:58:20 -03004055static const struct v4l2_file_operations vino_fops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004056 .owner = THIS_MODULE,
4057 .open = vino_open,
4058 .release = vino_close,
Hans Verkuil28959632009-02-18 18:53:47 -03004059 .unlocked_ioctl = vino_ioctl,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004060 .mmap = vino_mmap,
4061 .poll = vino_poll,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004062};
4063
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004064static struct video_device vdev_template = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004065 .name = "NOT SET",
Ralf Baechled203a7e2005-09-06 15:19:37 -07004066 .fops = &vino_fops,
Hans Verkuilc49cb362009-02-12 10:32:50 -03004067 .ioctl_ops = &vino_ioctl_ops,
4068 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004069};
4070
4071static void vino_module_cleanup(int stage)
4072{
4073 switch(stage) {
Hans Verkuil28959632009-02-18 18:53:47 -03004074 case 11:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004075 video_unregister_device(vino_drvdata->b.vdev);
4076 vino_drvdata->b.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004077 case 10:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004078 video_unregister_device(vino_drvdata->a.vdev);
4079 vino_drvdata->a.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004080 case 9:
Jean Delvarecea0c682009-03-06 12:05:43 -03004081 i2c_del_adapter(&vino_i2c_adapter);
Hans Verkuil28959632009-02-18 18:53:47 -03004082 case 8:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004083 free_irq(SGI_VINO_IRQ, NULL);
Hans Verkuil28959632009-02-18 18:53:47 -03004084 case 7:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004085 if (vino_drvdata->b.vdev) {
4086 video_device_release(vino_drvdata->b.vdev);
4087 vino_drvdata->b.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004088 }
Hans Verkuil28959632009-02-18 18:53:47 -03004089 case 6:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004090 if (vino_drvdata->a.vdev) {
4091 video_device_release(vino_drvdata->a.vdev);
4092 vino_drvdata->a.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004093 }
Hans Verkuil28959632009-02-18 18:53:47 -03004094 case 5:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004095 /* all entries in dma_cpu dummy table have the same address */
4096 dma_unmap_single(NULL,
4097 vino_drvdata->dummy_desc_table.dma_cpu[0],
4098 PAGE_SIZE, DMA_FROM_DEVICE);
4099 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4100 * sizeof(dma_addr_t),
4101 (void *)vino_drvdata->
4102 dummy_desc_table.dma_cpu,
4103 vino_drvdata->dummy_desc_table.dma);
Hans Verkuil28959632009-02-18 18:53:47 -03004104 case 4:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004105 free_page(vino_drvdata->dummy_page);
Hans Verkuil28959632009-02-18 18:53:47 -03004106 case 3:
4107 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004108 case 2:
4109 kfree(vino_drvdata);
4110 case 1:
4111 iounmap(vino);
4112 case 0:
4113 break;
4114 default:
4115 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4116 stage);
4117 }
4118}
4119
4120static int vino_probe(void)
4121{
4122 unsigned long rev_id;
4123
4124 if (ip22_is_fullhouse()) {
4125 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4126 return -ENODEV;
4127 }
4128
4129 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4130 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4131 return -ENODEV;
4132 }
4133
4134 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4135 if (!vino) {
4136 printk(KERN_ERR "VINO: ioremap() failed\n");
4137 return -EIO;
4138 }
4139 vino_init_stage++;
4140
4141 if (get_dbe(rev_id, &(vino->rev_id))) {
4142 printk(KERN_ERR "Failed to read VINO revision register\n");
4143 vino_module_cleanup(vino_init_stage);
4144 return -ENODEV;
4145 }
4146
4147 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4148 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4149 rev_id);
4150 vino_module_cleanup(vino_init_stage);
4151 return -ENODEV;
4152 }
4153
Ladislav Michla637a112005-09-01 15:07:34 +00004154 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004155
4156 return 0;
4157}
4158
4159static int vino_init(void)
4160{
4161 dma_addr_t dma_dummy_address;
Hans Verkuil28959632009-02-18 18:53:47 -03004162 int err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004163 int i;
4164
Panagiotis Issaris74081872006-01-11 19:40:56 -02004165 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004166 if (!vino_drvdata) {
4167 vino_module_cleanup(vino_init_stage);
4168 return -ENOMEM;
4169 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004170 vino_init_stage++;
Hans Verkuil28959632009-02-18 18:53:47 -03004171 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4172 sizeof(vino_drvdata->v4l2_dev.name));
4173 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4174 if (err)
4175 return err;
4176 vino_init_stage++;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004177
4178 /* create a dummy dma descriptor */
4179 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4180 if (!vino_drvdata->dummy_page) {
4181 vino_module_cleanup(vino_init_stage);
4182 return -ENOMEM;
4183 }
4184 vino_init_stage++;
4185
4186 // TODO: use page_count in dummy_desc_table
4187
4188 vino_drvdata->dummy_desc_table.dma_cpu =
4189 dma_alloc_coherent(NULL,
4190 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4191 &vino_drvdata->dummy_desc_table.dma,
4192 GFP_KERNEL | GFP_DMA);
4193 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4194 vino_module_cleanup(vino_init_stage);
4195 return -ENOMEM;
4196 }
4197 vino_init_stage++;
4198
4199 dma_dummy_address = dma_map_single(NULL,
4200 (void *)vino_drvdata->dummy_page,
4201 PAGE_SIZE, DMA_FROM_DEVICE);
4202 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4203 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4204 }
4205
4206 /* initialize VINO */
4207
4208 vino->control = 0;
4209 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4210 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4211 udelay(VINO_DESC_FETCH_DELAY);
4212
4213 vino->intr_status = 0;
4214
4215 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4216 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4217
4218 return 0;
4219}
4220
4221static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4222 unsigned int channel, const char *name)
4223{
4224 vcs->channel = channel;
4225 vcs->input = VINO_INPUT_NONE;
4226 vcs->alpha = 0;
4227 vcs->users = 0;
4228 vcs->data_format = VINO_DATA_FMT_GREY;
4229 vcs->data_norm = VINO_DATA_NORM_NTSC;
4230 vcs->decimation = 1;
4231 vino_set_default_clipping(vcs);
4232 vino_set_default_framerate(vcs);
4233
4234 vcs->capturing = 0;
4235
Ingo Molnar3593cab2006-02-07 06:49:14 -02004236 mutex_init(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004237 spin_lock_init(&vcs->capture_lock);
4238
Ingo Molnar3593cab2006-02-07 06:49:14 -02004239 mutex_init(&vcs->fb_queue.queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004240 spin_lock_init(&vcs->fb_queue.queue_lock);
4241 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4242
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004243 vcs->vdev = video_device_alloc();
4244 if (!vcs->vdev) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004245 vino_module_cleanup(vino_init_stage);
4246 return -ENOMEM;
4247 }
4248 vino_init_stage++;
4249
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004250 memcpy(vcs->vdev, &vdev_template,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004251 sizeof(struct video_device));
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004252 strcpy(vcs->vdev->name, name);
4253 vcs->vdev->release = video_device_release;
Hans Verkuil28959632009-02-18 18:53:47 -03004254 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004255
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004256 video_set_drvdata(vcs->vdev, vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004257
4258 return 0;
4259}
4260
4261static int __init vino_module_init(void)
4262{
4263 int ret;
4264
4265 printk(KERN_INFO "SGI VINO driver version %s\n",
4266 VINO_MODULE_VERSION);
4267
4268 ret = vino_probe();
4269 if (ret)
4270 return ret;
4271
4272 ret = vino_init();
4273 if (ret)
4274 return ret;
4275
4276 /* initialize data structures */
4277
4278 spin_lock_init(&vino_drvdata->vino_lock);
4279 spin_lock_init(&vino_drvdata->input_lock);
4280
4281 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004282 vino_vdev_name_a);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004283 if (ret)
4284 return ret;
4285
4286 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004287 vino_vdev_name_b);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004288 if (ret)
4289 return ret;
4290
4291 /* initialize hardware and register V4L devices */
4292
4293 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4294 vino_driver_description, NULL);
4295 if (ret) {
4296 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4297 SGI_VINO_IRQ);
4298 vino_module_cleanup(vino_init_stage);
4299 return -EAGAIN;
4300 }
4301 vino_init_stage++;
4302
Jean Delvarecea0c682009-03-06 12:05:43 -03004303 ret = i2c_add_adapter(&vino_i2c_adapter);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004304 if (ret) {
4305 printk(KERN_ERR "VINO I2C bus registration failed\n");
4306 vino_module_cleanup(vino_init_stage);
4307 return ret;
4308 }
Hans Verkuil28959632009-02-18 18:53:47 -03004309 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004310 vino_init_stage++;
4311
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004312 ret = video_register_device(vino_drvdata->a.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004313 VFL_TYPE_GRABBER, -1);
4314 if (ret < 0) {
4315 printk(KERN_ERR "VINO channel A Video4Linux-device "
4316 "registration failed\n");
4317 vino_module_cleanup(vino_init_stage);
4318 return -EINVAL;
4319 }
4320 vino_init_stage++;
4321
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004322 ret = video_register_device(vino_drvdata->b.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004323 VFL_TYPE_GRABBER, -1);
4324 if (ret < 0) {
4325 printk(KERN_ERR "VINO channel B Video4Linux-device "
4326 "registration failed\n");
4327 vino_module_cleanup(vino_init_stage);
4328 return -EINVAL;
4329 }
4330 vino_init_stage++;
4331
Hans Verkuile6574f22009-04-01 03:57:53 -03004332 vino_drvdata->decoder =
Hans Verkuil53dacb12009-08-10 02:49:08 -03004333 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03004334 "saa7191", 0, I2C_ADDRS(0x45));
Hans Verkuile6574f22009-04-01 03:57:53 -03004335 vino_drvdata->camera =
Hans Verkuil53dacb12009-08-10 02:49:08 -03004336 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03004337 "indycam", 0, I2C_ADDRS(0x2b));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004338
4339 dprintk("init complete!\n");
4340
4341 return 0;
4342}
4343
4344static void __exit vino_module_exit(void)
4345{
4346 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4347 vino_module_cleanup(vino_init_stage);
4348 dprintk("cleanup complete, exit!\n");
4349}
4350
4351module_init(vino_module_init);
4352module_exit(vino_module_exit);