blob: f3b6e15d91f2b40df6e53a685c084a9807c0846b [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>
36#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000037#include <linux/time.h>
38#include <linux/version.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070039#include <linux/kmod.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Hans Verkuil33b687c2008-07-25 05:32:50 -030043#include <linux/videodev2.h>
Hans Verkuil28959632009-02-18 18:53:47 -030044#include <media/v4l2-device.h>
Yoichi Yuasac2cfcf72008-07-29 05:30:58 -030045#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020046#include <linux/mutex.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/paccess.h>
49#include <asm/io.h>
50#include <asm/sgi/ip22.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/sgi/mc.h>
52
53#include "vino.h"
Ralf Baechled203a7e2005-09-06 15:19:37 -070054#include "saa7191.h"
55#include "indycam.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ralf Baechled203a7e2005-09-06 15:19:37 -070057/* Uncomment the following line to get lots and lots of (mostly useless)
58 * debug info.
59 * Note that the debug output also slows down the driver significantly */
60// #define VINO_DEBUG
Ladislav Michla637a112005-09-01 15:07:34 +000061// #define VINO_DEBUG_INT
Ralf Baechled203a7e2005-09-06 15:19:37 -070062
Hans Verkuil3ff4ad82009-04-01 03:15:52 -030063#define VINO_MODULE_VERSION "0.0.6"
64#define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 6)
Ralf Baechled203a7e2005-09-06 15:19:37 -070065
66MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
67MODULE_VERSION(VINO_MODULE_VERSION);
68MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
69MODULE_LICENSE("GPL");
70
Ralf Baechled203a7e2005-09-06 15:19:37 -070071#ifdef VINO_DEBUG
72#define dprintk(x...) printk("VINO: " x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#else
Ralf Baechled203a7e2005-09-06 15:19:37 -070074#define dprintk(x...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
Ralf Baechled203a7e2005-09-06 15:19:37 -070077#define VINO_NO_CHANNEL 0
78#define VINO_CHANNEL_A 1
79#define VINO_CHANNEL_B 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Ralf Baechled203a7e2005-09-06 15:19:37 -070081#define VINO_PAL_WIDTH 768
82#define VINO_PAL_HEIGHT 576
83#define VINO_NTSC_WIDTH 640
84#define VINO_NTSC_HEIGHT 480
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Ralf Baechled203a7e2005-09-06 15:19:37 -070086#define VINO_MIN_WIDTH 32
87#define VINO_MIN_HEIGHT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Ralf Baechled203a7e2005-09-06 15:19:37 -070089#define VINO_CLIPPING_START_ODD_D1 1
Ladislav Michla637a112005-09-01 15:07:34 +000090#define VINO_CLIPPING_START_ODD_PAL 15
91#define VINO_CLIPPING_START_ODD_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070092
93#define VINO_CLIPPING_START_EVEN_D1 2
Ladislav Michla637a112005-09-01 15:07:34 +000094#define VINO_CLIPPING_START_EVEN_PAL 15
95#define VINO_CLIPPING_START_EVEN_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070096
97#define VINO_INPUT_CHANNEL_COUNT 3
98
Ladislav Michla637a112005-09-01 15:07:34 +000099/* the number is the index for vino_inputs */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700100#define VINO_INPUT_NONE -1
101#define VINO_INPUT_COMPOSITE 0
102#define VINO_INPUT_SVIDEO 1
103#define VINO_INPUT_D1 2
104
105#define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
106
Ladislav Michla637a112005-09-01 15:07:34 +0000107#define VINO_FIFO_THRESHOLD_DEFAULT 16
Ralf Baechled203a7e2005-09-06 15:19:37 -0700108
Ralf Baechled203a7e2005-09-06 15:19:37 -0700109#define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
110 * VINO_PAL_HEIGHT * 4 \
111 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
112
Ladislav Michla637a112005-09-01 15:07:34 +0000113#define VINO_FRAMEBUFFER_COUNT_MAX 8
Ralf Baechled203a7e2005-09-06 15:19:37 -0700114
115#define VINO_FRAMEBUFFER_UNUSED 0
116#define VINO_FRAMEBUFFER_IN_USE 1
117#define VINO_FRAMEBUFFER_READY 2
118
119#define VINO_QUEUE_ERROR -1
120#define VINO_QUEUE_MAGIC 0x20050125
121
122#define VINO_MEMORY_NONE 0
123#define VINO_MEMORY_MMAP 1
124#define VINO_MEMORY_USERPTR 2
125
126#define VINO_DUMMY_DESC_COUNT 4
127#define VINO_DESC_FETCH_DELAY 5 /* microseconds */
128
Ladislav Michla637a112005-09-01 15:07:34 +0000129#define VINO_MAX_FRAME_SKIP_COUNT 128
130
Ralf Baechled203a7e2005-09-06 15:19:37 -0700131/* the number is the index for vino_data_formats */
132#define VINO_DATA_FMT_NONE -1
133#define VINO_DATA_FMT_GREY 0
134#define VINO_DATA_FMT_RGB332 1
135#define VINO_DATA_FMT_RGB32 2
136#define VINO_DATA_FMT_YUV 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700137
138#define VINO_DATA_FMT_COUNT 4
139
Ladislav Michla637a112005-09-01 15:07:34 +0000140/* the number is the index for vino_data_norms */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700141#define VINO_DATA_NORM_NONE -1
142#define VINO_DATA_NORM_NTSC 0
143#define VINO_DATA_NORM_PAL 1
144#define VINO_DATA_NORM_SECAM 2
145#define VINO_DATA_NORM_D1 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700146
147#define VINO_DATA_NORM_COUNT 4
148
Jean Delvarecea0c682009-03-06 12:05:43 -0300149/* I2C controller flags */
150#define SGI_I2C_FORCE_IDLE (0 << 0)
151#define SGI_I2C_NOT_IDLE (1 << 0)
152#define SGI_I2C_WRITE (0 << 1)
153#define SGI_I2C_READ (1 << 1)
154#define SGI_I2C_RELEASE_BUS (0 << 2)
155#define SGI_I2C_HOLD_BUS (1 << 2)
156#define SGI_I2C_XFER_DONE (0 << 4)
157#define SGI_I2C_XFER_BUSY (1 << 4)
158#define SGI_I2C_ACK (0 << 5)
159#define SGI_I2C_NACK (1 << 5)
160#define SGI_I2C_BUS_OK (0 << 7)
161#define SGI_I2C_BUS_ERR (1 << 7)
162
Ralf Baechled203a7e2005-09-06 15:19:37 -0700163/* Internal data structure definitions */
164
165struct vino_input {
166 char *name;
167 v4l2_std_id std;
168};
169
170struct vino_clipping {
171 unsigned int left, right, top, bottom;
172};
173
174struct vino_data_format {
175 /* the description */
176 char *description;
177 /* bytes per pixel */
178 unsigned int bpp;
179 /* V4L2 fourcc code */
180 __u32 pixelformat;
181 /* V4L2 colorspace (duh!) */
182 enum v4l2_colorspace colorspace;
183};
184
185struct vino_data_norm {
186 char *description;
187 unsigned int width, height;
188 struct vino_clipping odd;
189 struct vino_clipping even;
190
191 v4l2_std_id std;
192 unsigned int fps_min, fps_max;
193 __u32 framelines;
194};
195
196struct vino_descriptor_table {
197 /* the number of PAGE_SIZE sized pages in the buffer */
198 unsigned int page_count;
199 /* virtual (kmalloc'd) pointers to the actual data
200 * (in PAGE_SIZE chunks, used with mmap streaming) */
201 unsigned long *virtual;
202
203 /* cpu address for the VINO descriptor table
204 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
205 unsigned long *dma_cpu;
206 /* dma address for the VINO descriptor table
207 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
208 dma_addr_t dma;
209};
210
211struct vino_framebuffer {
212 /* identifier nubmer */
213 unsigned int id;
214 /* the length of the whole buffer */
215 unsigned int size;
216 /* the length of actual data in buffer */
217 unsigned int data_size;
218 /* the data format */
219 unsigned int data_format;
220 /* the state of buffer data */
221 unsigned int state;
222 /* is the buffer mapped in user space? */
223 unsigned int map_count;
224 /* memory offset for mmap() */
225 unsigned int offset;
226 /* frame counter */
227 unsigned int frame_counter;
228 /* timestamp (written when image capture finishes) */
229 struct timeval timestamp;
230
231 struct vino_descriptor_table desc_table;
232
233 spinlock_t state_lock;
234};
235
236struct vino_framebuffer_fifo {
237 unsigned int length;
238
239 unsigned int used;
240 unsigned int head;
241 unsigned int tail;
242
Ladislav Michla637a112005-09-01 15:07:34 +0000243 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700244};
245
246struct vino_framebuffer_queue {
247 unsigned int magic;
248
249 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
250 unsigned int type;
251 unsigned int length;
252
253 /* data field of in and out contain index numbers for buffer */
254 struct vino_framebuffer_fifo in;
255 struct vino_framebuffer_fifo out;
256
Ladislav Michla637a112005-09-01 15:07:34 +0000257 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700258
259 spinlock_t queue_lock;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200260 struct mutex queue_mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700261 wait_queue_head_t frame_wait_queue;
262};
263
Ladislav Michla637a112005-09-01 15:07:34 +0000264struct vino_interrupt_data {
265 struct timeval timestamp;
266 unsigned int frame_counter;
267 unsigned int skip_count;
268 unsigned int skip;
269};
270
Ralf Baechled203a7e2005-09-06 15:19:37 -0700271struct vino_channel_settings {
272 unsigned int channel;
273
274 int input;
275 unsigned int data_format;
276 unsigned int data_norm;
277 struct vino_clipping clipping;
278 unsigned int decimation;
279 unsigned int line_size;
280 unsigned int alpha;
281 unsigned int fps;
282 unsigned int framert_reg;
283
284 unsigned int fifo_threshold;
285
286 struct vino_framebuffer_queue fb_queue;
287
288 /* number of the current field */
289 unsigned int field;
290
291 /* read in progress */
292 int reading;
293 /* streaming is active */
294 int streaming;
295 /* the driver is currently processing the queue */
296 int capturing;
297
Ingo Molnar3593cab2006-02-07 06:49:14 -0200298 struct mutex mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700299 spinlock_t capture_lock;
300
301 unsigned int users;
302
Ladislav Michla637a112005-09-01 15:07:34 +0000303 struct vino_interrupt_data int_data;
304
Ralf Baechled203a7e2005-09-06 15:19:37 -0700305 /* V4L support */
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300306 struct video_device *vdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Ralf Baechled203a7e2005-09-06 15:19:37 -0700309struct vino_settings {
Hans Verkuil28959632009-02-18 18:53:47 -0300310 struct v4l2_device v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700311 struct vino_channel_settings a;
312 struct vino_channel_settings b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300314 /* the channel which owns this client:
315 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
316 unsigned int decoder_owner;
317 struct v4l2_subdev *decoder;
318 unsigned int camera_owner;
319 struct v4l2_subdev *camera;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Ralf Baechled203a7e2005-09-06 15:19:37 -0700321 /* a lock for vino register access */
322 spinlock_t vino_lock;
323 /* a lock for channel input changes */
324 spinlock_t input_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned long dummy_page;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700327 struct vino_descriptor_table dummy_desc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328};
329
Ralf Baechled203a7e2005-09-06 15:19:37 -0700330/* Module parameters */
331
332/*
Ladislav Michla637a112005-09-01 15:07:34 +0000333 * Using vino_pixel_conversion the ABGR32-format pixels supplied
Ralf Baechled203a7e2005-09-06 15:19:37 -0700334 * by the VINO chip can be converted to more common formats
335 * like RGBA32 (or probably RGB24 in the future). This way we
336 * can give out data that can be specified correctly with
337 * the V4L2-definitions.
338 *
339 * The pixel format is specified as RGBA32 when no conversion
340 * is used.
341 *
342 * Note that this only affects the 32-bit bit depth.
343 *
344 * Use non-zero value to enable conversion.
345 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300346static int vino_pixel_conversion;
Ladislav Michla637a112005-09-01 15:07:34 +0000347
Ralf Baechled203a7e2005-09-06 15:19:37 -0700348module_param_named(pixelconv, vino_pixel_conversion, int, 0);
Ladislav Michla637a112005-09-01 15:07:34 +0000349
Ralf Baechled203a7e2005-09-06 15:19:37 -0700350MODULE_PARM_DESC(pixelconv,
351 "enable pixel conversion (non-zero value enables)");
352
353/* Internal data structures */
354
355static struct sgi_vino *vino;
356
357static struct vino_settings *vino_drvdata;
358
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300359#define camera_call(o, f, args...) \
360 v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
361#define decoder_call(o, f, args...) \
362 v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
363
Ralf Baechled203a7e2005-09-06 15:19:37 -0700364static const char *vino_driver_name = "vino";
365static const char *vino_driver_description = "SGI VINO";
366static const char *vino_bus_name = "GIO64 bus";
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300367static const char *vino_vdev_name_a = "SGI VINO Channel A";
368static const char *vino_vdev_name_b = "SGI VINO Channel B";
Ralf Baechled203a7e2005-09-06 15:19:37 -0700369
Ladislav Michla637a112005-09-01 15:07:34 +0000370static void vino_capture_tasklet(unsigned long channel);
371
372DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
373DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
374
Ralf Baechled203a7e2005-09-06 15:19:37 -0700375static const struct vino_input vino_inputs[] = {
376 {
377 .name = "Composite",
Ladislav Michla637a112005-09-01 15:07:34 +0000378 .std = V4L2_STD_NTSC | V4L2_STD_PAL
379 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300380 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700381 .name = "S-Video",
Ladislav Michla637a112005-09-01 15:07:34 +0000382 .std = V4L2_STD_NTSC | V4L2_STD_PAL
383 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300384 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000385 .name = "D1/IndyCam",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700386 .std = V4L2_STD_NTSC,
387 }
388};
389
390static const struct vino_data_format vino_data_formats[] = {
391 {
392 .description = "8-bit greyscale",
393 .bpp = 1,
394 .pixelformat = V4L2_PIX_FMT_GREY,
395 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300396 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700397 .description = "8-bit dithered RGB 3-3-2",
398 .bpp = 1,
399 .pixelformat = V4L2_PIX_FMT_RGB332,
400 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300401 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700402 .description = "32-bit RGB",
403 .bpp = 4,
404 .pixelformat = V4L2_PIX_FMT_RGB32,
405 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300406 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700407 .description = "YUV 4:2:2",
Ladislav Michla637a112005-09-01 15:07:34 +0000408 .bpp = 2,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700409 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
410 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Ladislav Michla637a112005-09-01 15:07:34 +0000411 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700412};
413
414static const struct vino_data_norm vino_data_norms[] = {
415 {
416 .description = "NTSC",
417 .std = V4L2_STD_NTSC,
418 .fps_min = 6,
419 .fps_max = 30,
420 .framelines = 525,
421 .width = VINO_NTSC_WIDTH,
422 .height = VINO_NTSC_HEIGHT,
423 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000424 .top = VINO_CLIPPING_START_ODD_NTSC,
425 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700426 .bottom = VINO_CLIPPING_START_ODD_NTSC
427 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000428 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700429 },
430 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000431 .top = VINO_CLIPPING_START_EVEN_NTSC,
432 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700433 .bottom = VINO_CLIPPING_START_EVEN_NTSC
434 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000435 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700436 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300437 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700438 .description = "PAL",
439 .std = V4L2_STD_PAL,
440 .fps_min = 5,
441 .fps_max = 25,
442 .framelines = 625,
443 .width = VINO_PAL_WIDTH,
444 .height = VINO_PAL_HEIGHT,
445 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000446 .top = VINO_CLIPPING_START_ODD_PAL,
447 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700448 .bottom = VINO_CLIPPING_START_ODD_PAL
449 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000450 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700451 },
452 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000453 .top = VINO_CLIPPING_START_EVEN_PAL,
454 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700455 .bottom = VINO_CLIPPING_START_EVEN_PAL
456 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000457 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700458 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300459 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700460 .description = "SECAM",
461 .std = V4L2_STD_SECAM,
462 .fps_min = 5,
463 .fps_max = 25,
464 .framelines = 625,
465 .width = VINO_PAL_WIDTH,
466 .height = VINO_PAL_HEIGHT,
467 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000468 .top = VINO_CLIPPING_START_ODD_PAL,
469 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700470 .bottom = VINO_CLIPPING_START_ODD_PAL
471 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000472 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700473 },
474 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000475 .top = VINO_CLIPPING_START_EVEN_PAL,
476 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700477 .bottom = VINO_CLIPPING_START_EVEN_PAL
478 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000479 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700480 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300481 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000482 .description = "NTSC/D1",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700483 .std = V4L2_STD_NTSC,
484 .fps_min = 6,
485 .fps_max = 30,
486 .framelines = 525,
487 .width = VINO_NTSC_WIDTH,
488 .height = VINO_NTSC_HEIGHT,
489 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000490 .top = VINO_CLIPPING_START_ODD_D1,
491 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700492 .bottom = VINO_CLIPPING_START_ODD_D1
493 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000494 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700495 },
496 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000497 .top = VINO_CLIPPING_START_EVEN_D1,
498 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700499 .bottom = VINO_CLIPPING_START_EVEN_D1
500 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000501 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700502 },
503 }
504};
505
506#define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
507
508struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
509 {
510 .id = V4L2_CID_AUTOGAIN,
511 .type = V4L2_CTRL_TYPE_BOOLEAN,
512 .name = "Automatic Gain Control",
513 .minimum = 0,
514 .maximum = 1,
515 .step = 1,
516 .default_value = INDYCAM_AGC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300517 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700518 .id = V4L2_CID_AUTO_WHITE_BALANCE,
519 .type = V4L2_CTRL_TYPE_BOOLEAN,
520 .name = "Automatic White Balance",
521 .minimum = 0,
522 .maximum = 1,
523 .step = 1,
524 .default_value = INDYCAM_AWB_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300525 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700526 .id = V4L2_CID_GAIN,
527 .type = V4L2_CTRL_TYPE_INTEGER,
528 .name = "Gain",
529 .minimum = INDYCAM_GAIN_MIN,
530 .maximum = INDYCAM_GAIN_MAX,
531 .step = 1,
532 .default_value = INDYCAM_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300533 }, {
534 .id = INDYCAM_CONTROL_RED_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700535 .type = V4L2_CTRL_TYPE_INTEGER,
536 .name = "Red Saturation",
537 .minimum = INDYCAM_RED_SATURATION_MIN,
538 .maximum = INDYCAM_RED_SATURATION_MAX,
539 .step = 1,
540 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300541 }, {
542 .id = INDYCAM_CONTROL_BLUE_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700543 .type = V4L2_CTRL_TYPE_INTEGER,
544 .name = "Blue Saturation",
545 .minimum = INDYCAM_BLUE_SATURATION_MIN,
546 .maximum = INDYCAM_BLUE_SATURATION_MAX,
547 .step = 1,
548 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300549 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700550 .id = V4L2_CID_RED_BALANCE,
551 .type = V4L2_CTRL_TYPE_INTEGER,
552 .name = "Red Balance",
553 .minimum = INDYCAM_RED_BALANCE_MIN,
554 .maximum = INDYCAM_RED_BALANCE_MAX,
555 .step = 1,
556 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300557 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700558 .id = V4L2_CID_BLUE_BALANCE,
559 .type = V4L2_CTRL_TYPE_INTEGER,
560 .name = "Blue Balance",
561 .minimum = INDYCAM_BLUE_BALANCE_MIN,
562 .maximum = INDYCAM_BLUE_BALANCE_MAX,
563 .step = 1,
564 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300565 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700566 .id = V4L2_CID_EXPOSURE,
567 .type = V4L2_CTRL_TYPE_INTEGER,
568 .name = "Shutter Control",
569 .minimum = INDYCAM_SHUTTER_MIN,
570 .maximum = INDYCAM_SHUTTER_MAX,
571 .step = 1,
572 .default_value = INDYCAM_SHUTTER_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300573 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700574 .id = V4L2_CID_GAMMA,
575 .type = V4L2_CTRL_TYPE_INTEGER,
576 .name = "Gamma",
577 .minimum = INDYCAM_GAMMA_MIN,
578 .maximum = INDYCAM_GAMMA_MAX,
579 .step = 1,
580 .default_value = INDYCAM_GAMMA_DEFAULT,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700581 }
582};
583
Ladislav Michla637a112005-09-01 15:07:34 +0000584#define VINO_SAA7191_V4L2_CONTROL_COUNT 9
Ralf Baechled203a7e2005-09-06 15:19:37 -0700585
586struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
587 {
588 .id = V4L2_CID_HUE,
589 .type = V4L2_CTRL_TYPE_INTEGER,
590 .name = "Hue",
591 .minimum = SAA7191_HUE_MIN,
592 .maximum = SAA7191_HUE_MAX,
593 .step = 1,
594 .default_value = SAA7191_HUE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300595 }, {
596 .id = SAA7191_CONTROL_BANDPASS,
Ladislav Michla637a112005-09-01 15:07:34 +0000597 .type = V4L2_CTRL_TYPE_INTEGER,
598 .name = "Luminance Bandpass",
599 .minimum = SAA7191_BANDPASS_MIN,
600 .maximum = SAA7191_BANDPASS_MAX,
601 .step = 1,
602 .default_value = SAA7191_BANDPASS_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300603 }, {
604 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
Ladislav Michla637a112005-09-01 15:07:34 +0000605 .type = V4L2_CTRL_TYPE_INTEGER,
606 .name = "Luminance Bandpass Weight",
607 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
608 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
609 .step = 1,
610 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300611 }, {
612 .id = SAA7191_CONTROL_CORING,
Ladislav Michla637a112005-09-01 15:07:34 +0000613 .type = V4L2_CTRL_TYPE_INTEGER,
614 .name = "HF Luminance Coring",
615 .minimum = SAA7191_CORING_MIN,
616 .maximum = SAA7191_CORING_MAX,
617 .step = 1,
618 .default_value = SAA7191_CORING_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300619 }, {
620 .id = SAA7191_CONTROL_FORCE_COLOUR,
Ladislav Michla637a112005-09-01 15:07:34 +0000621 .type = V4L2_CTRL_TYPE_BOOLEAN,
622 .name = "Force Colour",
623 .minimum = SAA7191_FORCE_COLOUR_MIN,
624 .maximum = SAA7191_FORCE_COLOUR_MAX,
625 .step = 1,
626 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300627 }, {
628 .id = SAA7191_CONTROL_CHROMA_GAIN,
Ladislav Michla637a112005-09-01 15:07:34 +0000629 .type = V4L2_CTRL_TYPE_INTEGER,
630 .name = "Chrominance Gain Control",
631 .minimum = SAA7191_CHROMA_GAIN_MIN,
632 .maximum = SAA7191_CHROMA_GAIN_MAX,
633 .step = 1,
634 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300635 }, {
636 .id = SAA7191_CONTROL_VTRC,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700637 .type = V4L2_CTRL_TYPE_BOOLEAN,
638 .name = "VTR Time Constant",
639 .minimum = SAA7191_VTRC_MIN,
640 .maximum = SAA7191_VTRC_MAX,
641 .step = 1,
642 .default_value = SAA7191_VTRC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300643 }, {
644 .id = SAA7191_CONTROL_LUMA_DELAY,
Ladislav Michla637a112005-09-01 15:07:34 +0000645 .type = V4L2_CTRL_TYPE_INTEGER,
646 .name = "Luminance Delay Compensation",
647 .minimum = SAA7191_LUMA_DELAY_MIN,
648 .maximum = SAA7191_LUMA_DELAY_MAX,
649 .step = 1,
650 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300651 }, {
652 .id = SAA7191_CONTROL_VNR,
Ladislav Michla637a112005-09-01 15:07:34 +0000653 .type = V4L2_CTRL_TYPE_INTEGER,
654 .name = "Vertical Noise Reduction",
655 .minimum = SAA7191_VNR_MIN,
656 .maximum = SAA7191_VNR_MAX,
657 .step = 1,
658 .default_value = SAA7191_VNR_DEFAULT,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700659 }
660};
661
Ralf Baechled203a7e2005-09-06 15:19:37 -0700662/* VINO framebuffer/DMA descriptor management */
663
664static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
665 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700667 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Ralf Baechled203a7e2005-09-06 15:19:37 -0700669 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Ralf Baechled203a7e2005-09-06 15:19:37 -0700671 for (i = 0; i < count; i++) {
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300672 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700673 dma_unmap_single(NULL,
674 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
675 PAGE_SIZE, DMA_FROM_DEVICE);
676 free_page(fb->desc_table.virtual[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Ralf Baechled203a7e2005-09-06 15:19:37 -0700679 dma_free_coherent(NULL,
680 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
681 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
682 fb->desc_table.dma);
683 kfree(fb->desc_table.virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Ralf Baechled203a7e2005-09-06 15:19:37 -0700685 memset(fb, 0, sizeof(struct vino_framebuffer));
686}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Ralf Baechled203a7e2005-09-06 15:19:37 -0700688static void vino_free_buffer(struct vino_framebuffer *fb)
689{
690 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
691}
692
693static int vino_allocate_buffer(struct vino_framebuffer *fb,
694 unsigned int size)
695{
696 unsigned int count, i, j;
697 int ret = 0;
698
699 dprintk("vino_allocate_buffer():\n");
700
701 if (size < 1)
702 return -EINVAL;
703
704 memset(fb, 0, sizeof(struct vino_framebuffer));
705
706 count = ((size / PAGE_SIZE) + 4) & ~3;
707
708 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
709 size, count);
710
711 /* allocate memory for table with virtual (page) addresses */
712 fb->desc_table.virtual = (unsigned long *)
713 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
714 if (!fb->desc_table.virtual)
715 return -ENOMEM;
716
717 /* allocate memory for table with dma addresses
718 * (has space for four extra descriptors) */
719 fb->desc_table.dma_cpu =
720 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
721 sizeof(dma_addr_t), &fb->desc_table.dma,
722 GFP_KERNEL | GFP_DMA);
723 if (!fb->desc_table.dma_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ret = -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700725 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Ralf Baechled203a7e2005-09-06 15:19:37 -0700728 /* allocate pages for the buffer and acquire the according
729 * dma addresses */
730 for (i = 0; i < count; i++) {
731 dma_addr_t dma_data_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Ralf Baechled203a7e2005-09-06 15:19:37 -0700733 fb->desc_table.virtual[i] =
734 get_zeroed_page(GFP_KERNEL | GFP_DMA);
735 if (!fb->desc_table.virtual[i]) {
736 ret = -ENOBUFS;
737 break;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Ralf Baechled203a7e2005-09-06 15:19:37 -0700740 dma_data_addr =
741 dma_map_single(NULL,
742 (void *)fb->desc_table.virtual[i],
743 PAGE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Ralf Baechled203a7e2005-09-06 15:19:37 -0700745 for (j = 0; j < VINO_PAGE_RATIO; j++) {
746 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
747 dma_data_addr + VINO_PAGE_SIZE * j;
748 }
749
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300750 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Ralf Baechled203a7e2005-09-06 15:19:37 -0700753 /* page_count needs to be set anyway, because the descriptor table has
754 * been allocated according to this number */
755 fb->desc_table.page_count = count;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (ret) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700758 /* the descriptor with index i doesn't contain
759 * a valid address yet */
760 vino_free_buffer_with_count(fb, i);
761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
Ralf Baechled203a7e2005-09-06 15:19:37 -0700764 //fb->size = size;
765 fb->size = count * PAGE_SIZE;
766 fb->data_format = VINO_DATA_FMT_NONE;
767
768 /* set the dma stop-bit for the last (count+1)th descriptor */
769 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
770 return 0;
771
772 out_free_virtual:
773 kfree(fb->desc_table.virtual);
774 return ret;
775}
776
777#if 0
778/* user buffers not fully implemented yet */
779static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
780 void *user,
781 unsigned int size)
782{
783 unsigned int count, i, j;
784 int ret = 0;
785
786 dprintk("vino_prepare_user_buffer():\n");
787
788 if (size < 1)
789 return -EINVAL;
790
791 memset(fb, 0, sizeof(struct vino_framebuffer));
792
793 count = ((size / PAGE_SIZE)) & ~3;
794
795 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
796 size, count);
797
798 /* allocate memory for table with virtual (page) addresses */
799 fb->desc_table.virtual = (unsigned long *)
800 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
801 if (!fb->desc_table.virtual)
802 return -ENOMEM;
803
804 /* allocate memory for table with dma addresses
805 * (has space for four extra descriptors) */
806 fb->desc_table.dma_cpu =
807 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
808 sizeof(dma_addr_t), &fb->desc_table.dma,
809 GFP_KERNEL | GFP_DMA);
810 if (!fb->desc_table.dma_cpu) {
811 ret = -ENOMEM;
812 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700814
815 /* allocate pages for the buffer and acquire the according
816 * dma addresses */
817 for (i = 0; i < count; i++) {
818 dma_addr_t dma_data_addr;
819
820 fb->desc_table.virtual[i] =
821 get_zeroed_page(GFP_KERNEL | GFP_DMA);
822 if (!fb->desc_table.virtual[i]) {
823 ret = -ENOBUFS;
824 break;
825 }
826
827 dma_data_addr =
828 dma_map_single(NULL,
829 (void *)fb->desc_table.virtual[i],
830 PAGE_SIZE, DMA_FROM_DEVICE);
831
832 for (j = 0; j < VINO_PAGE_RATIO; j++) {
833 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
834 dma_data_addr + VINO_PAGE_SIZE * j;
835 }
836
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300837 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700838 }
839
840 /* page_count needs to be set anyway, because the descriptor table has
841 * been allocated according to this number */
842 fb->desc_table.page_count = count;
843
844 if (ret) {
845 /* the descriptor with index i doesn't contain
846 * a valid address yet */
847 vino_free_buffer_with_count(fb, i);
848 return ret;
849 }
850
851 //fb->size = size;
852 fb->size = count * PAGE_SIZE;
853
854 /* set the dma stop-bit for the last (count+1)th descriptor */
855 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
856 return 0;
857
858 out_free_virtual:
859 kfree(fb->desc_table.virtual);
860 return ret;
861}
862#endif
863
864static void vino_sync_buffer(struct vino_framebuffer *fb)
865{
866 int i;
867
868 dprintk("vino_sync_buffer():\n");
869
870 for (i = 0; i < fb->desc_table.page_count; i++)
FUJITA Tomonori028bbfe2009-05-27 22:10:43 -0300871 dma_sync_single_for_cpu(NULL,
872 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
873 PAGE_SIZE, DMA_FROM_DEVICE);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700874}
875
876/* Framebuffer fifo functions (need to be locked externally) */
877
Ladislav Michla637a112005-09-01 15:07:34 +0000878static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700879 unsigned int length)
880{
881 f->length = 0;
882 f->used = 0;
883 f->head = 0;
884 f->tail = 0;
885
Ladislav Michla637a112005-09-01 15:07:34 +0000886 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
887 length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700888
889 f->length = length;
890}
891
892/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000893static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
894 unsigned int id)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700895{
896 unsigned int i;
Ladislav Michla637a112005-09-01 15:07:34 +0000897
Ralf Baechled203a7e2005-09-06 15:19:37 -0700898 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
899 if (f->data[i] == id)
900 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Ladislav Michla637a112005-09-01 15:07:34 +0000906#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -0700907/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000908static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700909{
910 return (f->used == f->length);
911}
Ladislav Michla637a112005-09-01 15:07:34 +0000912#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -0700913
Ladislav Michla637a112005-09-01 15:07:34 +0000914static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700915{
916 return f->used;
917}
918
919static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
920{
921 if (id >= f->length) {
922 return VINO_QUEUE_ERROR;
923 }
924
925 if (vino_fifo_has_id(f, id)) {
926 return VINO_QUEUE_ERROR;
927 }
928
929 if (f->used < f->length) {
930 f->data[f->tail] = id;
931 f->tail = (f->tail + 1) % f->length;
932 f->used++;
933 } else {
934 return VINO_QUEUE_ERROR;
935 }
936
937 return 0;
938}
939
940static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
941{
942 if (f->used > 0) {
943 *id = f->data[f->head];
944 } else {
945 return VINO_QUEUE_ERROR;
946 }
947
948 return 0;
949}
950
951static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
952{
953 if (f->used > 0) {
954 *id = f->data[f->head];
955 f->head = (f->head + 1) % f->length;
956 f->used--;
957 } else {
958 return VINO_QUEUE_ERROR;
959 }
960
961 return 0;
962}
963
964/* Framebuffer queue functions */
965
966/* execute with queue_lock locked */
967static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
968 unsigned int length)
969{
970 unsigned int i;
971
972 q->length = 0;
973 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
974 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
975 for (i = 0; i < length; i++) {
976 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
977 i);
978 vino_free_buffer(q->buffer[i]);
979 kfree(q->buffer[i]);
980 }
981
982 q->type = VINO_MEMORY_NONE;
983 q->magic = 0;
984}
985
986static void vino_queue_free(struct vino_framebuffer_queue *q)
987{
988 dprintk("vino_queue_free():\n");
989
990 if (q->magic != VINO_QUEUE_MAGIC)
991 return;
992 if (q->type != VINO_MEMORY_MMAP)
993 return;
994
Ingo Molnar3593cab2006-02-07 06:49:14 -0200995 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700996
997 vino_queue_free_with_count(q, q->length);
998
Ingo Molnar3593cab2006-02-07 06:49:14 -0200999 mutex_unlock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001000}
1001
1002static int vino_queue_init(struct vino_framebuffer_queue *q,
1003 unsigned int *length)
1004{
1005 unsigned int i;
1006 int ret = 0;
1007
1008 dprintk("vino_queue_init(): length = %d\n", *length);
1009
1010 if (q->magic == VINO_QUEUE_MAGIC) {
1011 dprintk("vino_queue_init(): queue already initialized!\n");
1012 return -EINVAL;
1013 }
1014
1015 if (q->type != VINO_MEMORY_NONE) {
1016 dprintk("vino_queue_init(): queue already initialized!\n");
1017 return -EINVAL;
1018 }
1019
1020 if (*length < 1)
1021 return -EINVAL;
1022
Ingo Molnar3593cab2006-02-07 06:49:14 -02001023 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001024
Ladislav Michla637a112005-09-01 15:07:34 +00001025 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1026 *length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001027
1028 q->length = 0;
1029
1030 for (i = 0; i < *length; i++) {
1031 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1032 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1033 GFP_KERNEL);
1034 if (!q->buffer[i]) {
1035 dprintk("vino_queue_init(): kmalloc() failed\n");
1036 ret = -ENOMEM;
1037 break;
1038 }
1039
1040 ret = vino_allocate_buffer(q->buffer[i],
1041 VINO_FRAMEBUFFER_SIZE);
1042 if (ret) {
1043 kfree(q->buffer[i]);
1044 dprintk("vino_queue_init(): "
1045 "vino_allocate_buffer() failed\n");
1046 break;
1047 }
1048
1049 q->buffer[i]->id = i;
1050 if (i > 0) {
1051 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1052 q->buffer[i - 1]->size;
1053 } else {
1054 q->buffer[i]->offset = 0;
1055 }
1056
1057 spin_lock_init(&q->buffer[i]->state_lock);
1058
1059 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1060 "size = %d\n", i, q->buffer[i]->offset,
1061 q->buffer[i]->size);
1062 }
1063
1064 if (ret) {
1065 vino_queue_free_with_count(q, i);
1066 *length = 0;
1067 } else {
1068 q->length = *length;
1069 vino_fifo_init(&q->in, q->length);
1070 vino_fifo_init(&q->out, q->length);
1071 q->type = VINO_MEMORY_MMAP;
1072 q->magic = VINO_QUEUE_MAGIC;
1073 }
1074
Ingo Molnar3593cab2006-02-07 06:49:14 -02001075 mutex_unlock(&q->queue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 return ret;
1078}
1079
Ralf Baechled203a7e2005-09-06 15:19:37 -07001080static struct vino_framebuffer *vino_queue_add(struct
1081 vino_framebuffer_queue *q,
1082 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Ralf Baechled203a7e2005-09-06 15:19:37 -07001084 struct vino_framebuffer *ret = NULL;
1085 unsigned int total;
1086 unsigned long flags;
1087
1088 dprintk("vino_queue_add(): id = %d\n", id);
1089
1090 if (q->magic != VINO_QUEUE_MAGIC) {
1091 return ret;
1092 }
1093
1094 spin_lock_irqsave(&q->queue_lock, flags);
1095
1096 if (q->length == 0)
1097 goto out;
1098
1099 if (id >= q->length)
1100 goto out;
1101
1102 /* not needed?: if (vino_fifo_full(&q->out)) {
1103 goto out;
1104 }*/
1105 /* check that outgoing queue isn't already full
1106 * (or that it won't become full) */
1107 total = vino_fifo_get_used(&q->in) +
1108 vino_fifo_get_used(&q->out);
1109 if (total >= q->length)
1110 goto out;
1111
1112 if (vino_fifo_enqueue(&q->in, id))
1113 goto out;
1114
1115 ret = q->buffer[id];
1116
1117out:
1118 spin_unlock_irqrestore(&q->queue_lock, flags);
1119
1120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Ralf Baechled203a7e2005-09-06 15:19:37 -07001123static struct vino_framebuffer *vino_queue_transfer(struct
1124 vino_framebuffer_queue *q)
1125{
1126 struct vino_framebuffer *ret = NULL;
1127 struct vino_framebuffer *fb;
1128 int id;
1129 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Ralf Baechled203a7e2005-09-06 15:19:37 -07001131 dprintk("vino_queue_transfer():\n");
1132
1133 if (q->magic != VINO_QUEUE_MAGIC) {
1134 return ret;
1135 }
1136
1137 spin_lock_irqsave(&q->queue_lock, flags);
1138
1139 if (q->length == 0)
1140 goto out;
1141
1142 // now this actually removes an entry from the incoming queue
1143 if (vino_fifo_dequeue(&q->in, &id)) {
1144 goto out;
1145 }
1146
1147 dprintk("vino_queue_transfer(): id = %d\n", id);
1148 fb = q->buffer[id];
1149
1150 // we have already checked that the outgoing queue is not full, but...
1151 if (vino_fifo_enqueue(&q->out, id)) {
1152 printk(KERN_ERR "vino_queue_transfer(): "
1153 "outgoing queue is full, this shouldn't happen!\n");
1154 goto out;
1155 }
1156
1157 ret = fb;
1158out:
1159 spin_unlock_irqrestore(&q->queue_lock, flags);
1160
1161 return ret;
1162}
1163
1164/* returns true/false */
1165static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1166 unsigned int id)
1167{
1168 int ret = 0;
1169 unsigned long flags;
1170
1171 if (q->magic != VINO_QUEUE_MAGIC) {
1172 return ret;
1173 }
1174
1175 spin_lock_irqsave(&q->queue_lock, flags);
1176
1177 if (q->length == 0)
1178 goto out;
1179
1180 ret = vino_fifo_has_id(&q->in, id);
1181
1182out:
1183 spin_unlock_irqrestore(&q->queue_lock, flags);
1184
1185 return ret;
1186}
1187
1188/* returns true/false */
1189static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1190 unsigned int id)
1191{
1192 int ret = 0;
1193 unsigned long flags;
1194
1195 if (q->magic != VINO_QUEUE_MAGIC) {
1196 return ret;
1197 }
1198
1199 spin_lock_irqsave(&q->queue_lock, flags);
1200
1201 if (q->length == 0)
1202 goto out;
1203
1204 ret = vino_fifo_has_id(&q->out, id);
1205
1206out:
1207 spin_unlock_irqrestore(&q->queue_lock, flags);
1208
1209 return ret;
1210}
1211
1212static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1213 unsigned int *used)
1214{
1215 int ret = 0;
1216 unsigned long flags;
1217
1218 if (q->magic != VINO_QUEUE_MAGIC) {
1219 return VINO_QUEUE_ERROR;
1220 }
1221
1222 spin_lock_irqsave(&q->queue_lock, flags);
1223
1224 if (q->length == 0) {
1225 ret = VINO_QUEUE_ERROR;
1226 goto out;
1227 }
1228
1229 *used = vino_fifo_get_used(&q->in);
1230
1231out:
1232 spin_unlock_irqrestore(&q->queue_lock, flags);
1233
1234 return ret;
1235}
1236
1237static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1238 unsigned int *used)
1239{
1240 int ret = 0;
1241 unsigned long flags;
1242
1243 if (q->magic != VINO_QUEUE_MAGIC) {
1244 return VINO_QUEUE_ERROR;
1245 }
1246
1247 spin_lock_irqsave(&q->queue_lock, flags);
1248
1249 if (q->length == 0) {
1250 ret = VINO_QUEUE_ERROR;
1251 goto out;
1252 }
1253
1254 *used = vino_fifo_get_used(&q->out);
1255
1256out:
1257 spin_unlock_irqrestore(&q->queue_lock, flags);
1258
1259 return ret;
1260}
1261
Ladislav Michla637a112005-09-01 15:07:34 +00001262#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001263static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1264 unsigned int *total)
1265{
1266 int ret = 0;
1267 unsigned long flags;
1268
1269 if (q->magic != VINO_QUEUE_MAGIC) {
1270 return VINO_QUEUE_ERROR;
1271 }
1272
1273 spin_lock_irqsave(&q->queue_lock, flags);
1274
1275 if (q->length == 0) {
1276 ret = VINO_QUEUE_ERROR;
1277 goto out;
1278 }
1279
1280 *total = vino_fifo_get_used(&q->in) +
1281 vino_fifo_get_used(&q->out);
1282
1283out:
1284 spin_unlock_irqrestore(&q->queue_lock, flags);
1285
1286 return ret;
1287}
Ladislav Michla637a112005-09-01 15:07:34 +00001288#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001289
1290static struct vino_framebuffer *vino_queue_peek(struct
1291 vino_framebuffer_queue *q,
1292 unsigned int *id)
1293{
1294 struct vino_framebuffer *ret = NULL;
1295 unsigned long flags;
1296
1297 if (q->magic != VINO_QUEUE_MAGIC) {
1298 return ret;
1299 }
1300
1301 spin_lock_irqsave(&q->queue_lock, flags);
1302
1303 if (q->length == 0)
1304 goto out;
1305
1306 if (vino_fifo_peek(&q->in, id)) {
1307 goto out;
1308 }
1309
1310 ret = q->buffer[*id];
1311out:
1312 spin_unlock_irqrestore(&q->queue_lock, flags);
1313
1314 return ret;
1315}
1316
1317static struct vino_framebuffer *vino_queue_remove(struct
1318 vino_framebuffer_queue *q,
1319 unsigned int *id)
1320{
1321 struct vino_framebuffer *ret = NULL;
1322 unsigned long flags;
1323 dprintk("vino_queue_remove():\n");
1324
1325 if (q->magic != VINO_QUEUE_MAGIC) {
1326 return ret;
1327 }
1328
1329 spin_lock_irqsave(&q->queue_lock, flags);
1330
1331 if (q->length == 0)
1332 goto out;
1333
1334 if (vino_fifo_dequeue(&q->out, id)) {
1335 goto out;
1336 }
1337
1338 dprintk("vino_queue_remove(): id = %d\n", *id);
1339 ret = q->buffer[*id];
1340out:
1341 spin_unlock_irqrestore(&q->queue_lock, flags);
1342
1343 return ret;
1344}
1345
1346static struct
1347vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1348 unsigned int id)
1349{
1350 struct vino_framebuffer *ret = NULL;
1351 unsigned long flags;
1352
1353 if (q->magic != VINO_QUEUE_MAGIC) {
1354 return ret;
1355 }
1356
1357 spin_lock_irqsave(&q->queue_lock, flags);
1358
1359 if (q->length == 0)
1360 goto out;
1361
1362 if (id >= q->length)
1363 goto out;
1364
1365 ret = q->buffer[id];
1366 out:
1367 spin_unlock_irqrestore(&q->queue_lock, flags);
1368
1369 return ret;
1370}
1371
1372static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1373{
1374 unsigned int length = 0;
1375 unsigned long flags;
1376
1377 if (q->magic != VINO_QUEUE_MAGIC) {
1378 return length;
1379 }
1380
1381 spin_lock_irqsave(&q->queue_lock, flags);
1382 length = q->length;
1383 spin_unlock_irqrestore(&q->queue_lock, flags);
1384
1385 return length;
1386}
1387
1388static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1389{
1390 unsigned int i;
1391 int ret = 0;
1392 unsigned long flags;
1393
1394 if (q->magic != VINO_QUEUE_MAGIC) {
1395 return ret;
1396 }
1397
1398 spin_lock_irqsave(&q->queue_lock, flags);
1399 for (i = 0; i < q->length; i++) {
1400 if (q->buffer[i]->map_count > 0) {
1401 ret = 1;
1402 break;
1403 }
1404 }
1405 spin_unlock_irqrestore(&q->queue_lock, flags);
1406
1407 return ret;
1408}
1409
1410/* VINO functions */
1411
1412/* execute with input_lock locked */
1413static void vino_update_line_size(struct vino_channel_settings *vcs)
1414{
1415 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1416 unsigned int d = vcs->decimation;
1417 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001418 unsigned int lsize;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001419
1420 dprintk("update_line_size(): before: w = %d, d = %d, "
1421 "line_size = %d\n", w, d, vcs->line_size);
Ladislav Michla637a112005-09-01 15:07:34 +00001422
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001423 /* line size must be multiple of 8 bytes */
Ralf Baechled203a7e2005-09-06 15:19:37 -07001424 lsize = (bpp * (w / d)) & ~7;
1425 w = (lsize / bpp) * d;
1426
1427 vcs->clipping.right = vcs->clipping.left + w;
1428 vcs->line_size = lsize;
Ladislav Michla637a112005-09-01 15:07:34 +00001429
Ralf Baechled203a7e2005-09-06 15:19:37 -07001430 dprintk("update_line_size(): after: w = %d, d = %d, "
1431 "line_size = %d\n", w, d, vcs->line_size);
1432}
1433
1434/* execute with input_lock locked */
1435static void vino_set_clipping(struct vino_channel_settings *vcs,
1436 unsigned int x, unsigned int y,
1437 unsigned int w, unsigned int h)
1438{
1439 unsigned int maxwidth, maxheight;
1440 unsigned int d;
1441
1442 maxwidth = vino_data_norms[vcs->data_norm].width;
1443 maxheight = vino_data_norms[vcs->data_norm].height;
1444 d = vcs->decimation;
1445
1446 y &= ~1; /* odd/even fields */
1447
1448 if (x > maxwidth) {
1449 x = 0;
1450 }
1451 if (y > maxheight) {
1452 y = 0;
1453 }
1454
1455 if (((w / d) < VINO_MIN_WIDTH)
1456 || ((h / d) < VINO_MIN_HEIGHT)) {
1457 w = VINO_MIN_WIDTH * d;
1458 h = VINO_MIN_HEIGHT * d;
1459 }
1460
1461 if ((x + w) > maxwidth) {
1462 w = maxwidth - x;
1463 if ((w / d) < VINO_MIN_WIDTH)
1464 x = maxwidth - VINO_MIN_WIDTH * d;
1465 }
1466 if ((y + h) > maxheight) {
1467 h = maxheight - y;
1468 if ((h / d) < VINO_MIN_HEIGHT)
1469 y = maxheight - VINO_MIN_HEIGHT * d;
1470 }
1471
1472 vcs->clipping.left = x;
1473 vcs->clipping.top = y;
1474 vcs->clipping.right = x + w;
1475 vcs->clipping.bottom = y + h;
1476
1477 vino_update_line_size(vcs);
1478
1479 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1480 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1481 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1482}
1483
1484/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001485static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001486{
1487 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1488 vino_data_norms[vcs->data_norm].height);
1489}
1490
1491/* execute with input_lock locked */
1492static void vino_set_scaling(struct vino_channel_settings *vcs,
1493 unsigned int w, unsigned int h)
1494{
1495 unsigned int x, y, curw, curh, d;
1496
1497 x = vcs->clipping.left;
1498 y = vcs->clipping.top;
1499 curw = vcs->clipping.right - vcs->clipping.left;
1500 curh = vcs->clipping.bottom - vcs->clipping.top;
1501
1502 d = max(curw / w, curh / h);
1503
1504 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1505 w, h, curw, curh, d);
1506
1507 if (d < 1) {
1508 d = 1;
Ladislav Michla637a112005-09-01 15:07:34 +00001509 } else if (d > 8) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07001510 d = 8;
1511 }
1512
1513 vcs->decimation = d;
1514 vino_set_clipping(vcs, x, y, w * d, h * d);
1515
1516 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1517 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1518 vcs->decimation, vcs->line_size);
1519}
1520
1521/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001522static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001523{
1524 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1525 vcs->clipping.bottom - vcs->clipping.top);
1526}
1527
1528/* execute with input_lock locked */
1529static void vino_set_framerate(struct vino_channel_settings *vcs,
1530 unsigned int fps)
1531{
1532 unsigned int mask;
1533
1534 switch (vcs->data_norm) {
1535 case VINO_DATA_NORM_NTSC:
1536 case VINO_DATA_NORM_D1:
1537 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1538
1539 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1540 fps = vino_data_norms[vcs->data_norm].fps_min;
1541 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1542 fps = vino_data_norms[vcs->data_norm].fps_max;
1543
1544 switch (fps) {
1545 case 6:
1546 mask = 0x003;
1547 break;
1548 case 12:
1549 mask = 0x0c3;
1550 break;
1551 case 18:
1552 mask = 0x333;
1553 break;
1554 case 24:
1555 mask = 0x3ff;
1556 break;
1557 case 30:
1558 mask = 0xfff;
1559 break;
1560 default:
1561 mask = VINO_FRAMERT_FULL;
1562 }
1563 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1564 break;
1565 case VINO_DATA_NORM_PAL:
1566 case VINO_DATA_NORM_SECAM:
1567 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1568
1569 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1570 fps = vino_data_norms[vcs->data_norm].fps_min;
1571 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1572 fps = vino_data_norms[vcs->data_norm].fps_max;
1573
1574 switch (fps) {
1575 case 5:
1576 mask = 0x003;
1577 break;
1578 case 10:
1579 mask = 0x0c3;
1580 break;
1581 case 15:
1582 mask = 0x333;
1583 break;
1584 case 20:
1585 mask = 0x0ff;
1586 break;
1587 case 25:
1588 mask = 0x3ff;
1589 break;
1590 default:
1591 mask = VINO_FRAMERT_FULL;
1592 }
1593 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1594 break;
1595 }
1596
1597 vcs->fps = fps;
1598}
1599
1600/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001601static inline void vino_set_default_framerate(struct
1602 vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001603{
1604 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1605}
1606
Jean Delvarecea0c682009-03-06 12:05:43 -03001607/* VINO I2C bus functions */
1608
1609struct i2c_algo_sgi_data {
1610 void *data; /* private data for lowlevel routines */
1611 unsigned (*getctrl)(void *data);
1612 void (*setctrl)(void *data, unsigned val);
1613 unsigned (*rdata)(void *data);
1614 void (*wdata)(void *data, unsigned val);
1615
1616 int xfer_timeout;
1617 int ack_timeout;
1618};
1619
1620static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1621{
1622 int i;
1623
1624 for (i = 0; i < adap->xfer_timeout; i++) {
1625 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1626 return 0;
1627 udelay(1);
1628 }
1629
1630 return -ETIMEDOUT;
1631}
1632
1633static int wait_ack(struct i2c_algo_sgi_data *adap)
1634{
1635 int i;
1636
1637 if (wait_xfer_done(adap))
1638 return -ETIMEDOUT;
1639 for (i = 0; i < adap->ack_timeout; i++) {
1640 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1641 return 0;
1642 udelay(1);
1643 }
1644
1645 return -ETIMEDOUT;
1646}
1647
1648static int force_idle(struct i2c_algo_sgi_data *adap)
1649{
1650 int i;
1651
1652 adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1653 for (i = 0; i < adap->xfer_timeout; i++) {
1654 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1655 goto out;
1656 udelay(1);
1657 }
1658 return -ETIMEDOUT;
1659out:
1660 if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1661 return -EIO;
1662 return 0;
1663}
1664
1665static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1666 int rd)
1667{
1668 if (rd)
1669 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1670 /* Check if bus is idle, eventually force it to do so */
1671 if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1672 if (force_idle(adap))
1673 return -EIO;
1674 /* Write out the i2c chip address and specify operation */
1675 adap->setctrl(adap->data,
1676 SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1677 if (rd)
1678 addr |= 1;
1679 adap->wdata(adap->data, addr);
1680 if (wait_ack(adap))
1681 return -EIO;
1682 return 0;
1683}
1684
1685static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1686 unsigned int len)
1687{
1688 int i;
1689
1690 adap->setctrl(adap->data,
1691 SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1692 for (i = 0; i < len; i++) {
1693 if (wait_xfer_done(adap))
1694 return -EIO;
1695 buf[i] = adap->rdata(adap->data);
1696 }
1697 adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1698
1699 return 0;
1700
1701}
1702
1703static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1704 unsigned int len)
1705{
1706 int i;
1707
1708 /* We are already in write state */
1709 for (i = 0; i < len; i++) {
1710 adap->wdata(adap->data, buf[i]);
1711 if (wait_ack(adap))
1712 return -EIO;
1713 }
1714 return 0;
1715}
1716
1717static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1718 int num)
1719{
1720 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1721 struct i2c_msg *p;
1722 int i, err = 0;
1723
1724 for (i = 0; !err && i < num; i++) {
1725 p = &msgs[i];
1726 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1727 if (err || !p->len)
1728 continue;
1729 if (p->flags & I2C_M_RD)
1730 err = i2c_read(adap, p->buf, p->len);
1731 else
1732 err = i2c_write(adap, p->buf, p->len);
1733 }
1734
1735 return (err < 0) ? err : i;
1736}
1737
1738static u32 sgi_func(struct i2c_adapter *adap)
1739{
1740 return I2C_FUNC_SMBUS_EMUL;
1741}
1742
1743static const struct i2c_algorithm sgi_algo = {
1744 .master_xfer = sgi_xfer,
1745 .functionality = sgi_func,
1746};
1747
1748static unsigned i2c_vino_getctrl(void *data)
1749{
1750 return vino->i2c_control;
1751}
1752
1753static void i2c_vino_setctrl(void *data, unsigned val)
1754{
1755 vino->i2c_control = val;
1756}
1757
1758static unsigned i2c_vino_rdata(void *data)
1759{
1760 return vino->i2c_data;
1761}
1762
1763static void i2c_vino_wdata(void *data, unsigned val)
1764{
1765 vino->i2c_data = val;
1766}
1767
1768static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1769 .getctrl = &i2c_vino_getctrl,
1770 .setctrl = &i2c_vino_setctrl,
1771 .rdata = &i2c_vino_rdata,
1772 .wdata = &i2c_vino_wdata,
1773 .xfer_timeout = 200,
1774 .ack_timeout = 1000,
1775};
1776
1777static struct i2c_adapter vino_i2c_adapter = {
1778 .name = "VINO I2C bus",
Jean Delvarecea0c682009-03-06 12:05:43 -03001779 .algo = &sgi_algo,
1780 .algo_data = &i2c_sgi_vino_data,
1781 .owner = THIS_MODULE,
1782};
1783
Ralf Baechled203a7e2005-09-06 15:19:37 -07001784/*
1785 * Prepare VINO for DMA transfer...
1786 * (execute only with vino_lock and input_lock locked)
1787 */
1788static int vino_dma_setup(struct vino_channel_settings *vcs,
1789 struct vino_framebuffer *fb)
1790{
1791 u32 ctrl, intr;
1792 struct sgi_vino_channel *ch;
1793 const struct vino_data_norm *norm;
1794
1795 dprintk("vino_dma_setup():\n");
1796
1797 vcs->field = 0;
1798 fb->frame_counter = 0;
1799
1800 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1801 norm = &vino_data_norms[vcs->data_norm];
1802
1803 ch->page_index = 0;
1804 ch->line_count = 0;
1805
1806 /* VINO line size register is set 8 bytes less than actual */
1807 ch->line_size = vcs->line_size - 8;
1808
1809 /* let VINO know where to transfer data */
1810 ch->start_desc_tbl = fb->desc_table.dma;
1811 ch->next_4_desc = fb->desc_table.dma;
1812
1813 /* give vino time to fetch the first four descriptors, 5 usec
1814 * should be more than enough time */
1815 udelay(VINO_DESC_FETCH_DELAY);
1816
Ladislav Michla637a112005-09-01 15:07:34 +00001817 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1818 ch->start_desc_tbl, ch->next_4_desc);
1819
Ralf Baechled203a7e2005-09-06 15:19:37 -07001820 /* set the alpha register */
1821 ch->alpha = vcs->alpha;
1822
1823 /* set clipping registers */
1824 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1825 VINO_CLIP_EVEN(norm->even.top +
1826 vcs->clipping.top / 2) |
1827 VINO_CLIP_X(vcs->clipping.left);
1828 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1829 vcs->clipping.bottom / 2 - 1) |
1830 VINO_CLIP_EVEN(norm->even.top +
1831 vcs->clipping.bottom / 2 - 1) |
1832 VINO_CLIP_X(vcs->clipping.right);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001833
1834 /* set the size of actual content in the buffer (DECIMATION !) */
1835 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1836 vcs->decimation) *
1837 ((vcs->clipping.bottom - vcs->clipping.top) /
1838 vcs->decimation) *
1839 vino_data_formats[vcs->data_format].bpp;
1840
1841 ch->frame_rate = vcs->framert_reg;
1842
1843 ctrl = vino->control;
1844 intr = vino->intr_status;
1845
1846 if (vcs->channel == VINO_CHANNEL_A) {
1847 /* All interrupt conditions for this channel was cleared
1848 * so clear the interrupt status register and enable
1849 * interrupts */
1850 intr &= ~VINO_INTSTAT_A;
1851 ctrl |= VINO_CTRL_A_INT;
1852
1853 /* enable synchronization */
1854 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1855
1856 /* enable frame assembly */
1857 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1858
1859 /* set decimation used */
1860 if (vcs->decimation < 2)
1861 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1862 else {
1863 ctrl |= VINO_CTRL_A_DEC_ENBL;
1864 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1865 ctrl |= (vcs->decimation - 1) <<
1866 VINO_CTRL_A_DEC_SCALE_SHIFT;
1867 }
1868
1869 /* select input interface */
1870 if (vcs->input == VINO_INPUT_D1)
1871 ctrl |= VINO_CTRL_A_SELECT;
1872 else
1873 ctrl &= ~VINO_CTRL_A_SELECT;
1874
1875 /* palette */
1876 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1877 VINO_CTRL_A_DITHER);
1878 } else {
1879 intr &= ~VINO_INTSTAT_B;
1880 ctrl |= VINO_CTRL_B_INT;
1881
1882 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1883 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1884
1885 if (vcs->decimation < 2)
1886 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1887 else {
1888 ctrl |= VINO_CTRL_B_DEC_ENBL;
1889 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1890 ctrl |= (vcs->decimation - 1) <<
1891 VINO_CTRL_B_DEC_SCALE_SHIFT;
1892
1893 }
1894 if (vcs->input == VINO_INPUT_D1)
1895 ctrl |= VINO_CTRL_B_SELECT;
1896 else
1897 ctrl &= ~VINO_CTRL_B_SELECT;
1898
1899 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1900 VINO_CTRL_B_DITHER);
1901 }
1902
1903 /* set palette */
1904 fb->data_format = vcs->data_format;
1905
1906 switch (vcs->data_format) {
1907 case VINO_DATA_FMT_GREY:
1908 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1909 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1910 break;
1911 case VINO_DATA_FMT_RGB32:
1912 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1913 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1914 break;
1915 case VINO_DATA_FMT_YUV:
1916 /* nothing needs to be done */
1917 break;
1918 case VINO_DATA_FMT_RGB332:
1919 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1920 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1921 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1922 break;
1923 }
1924
1925 vino->intr_status = intr;
1926 vino->control = ctrl;
1927
1928 return 0;
1929}
1930
1931/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001932static inline void vino_dma_start(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001933{
1934 u32 ctrl = vino->control;
1935
1936 dprintk("vino_dma_start():\n");
1937 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1938 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1939 vino->control = ctrl;
1940}
1941
1942/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001943static inline void vino_dma_stop(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001944{
1945 u32 ctrl = vino->control;
1946
1947 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1948 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
Ladislav Michla637a112005-09-01 15:07:34 +00001949 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1950 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001951 vino->control = ctrl;
1952 dprintk("vino_dma_stop():\n");
1953}
1954
1955/*
1956 * Load dummy page to descriptor registers. This prevents generating of
1957 * spurious interrupts. (execute only with vino_lock locked)
1958 */
1959static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1960{
1961 struct sgi_vino_channel *ch;
1962
1963 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1964
1965 ch->page_index = 0;
1966 ch->line_count = 0;
1967
1968 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1969 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1970
1971 udelay(VINO_DESC_FETCH_DELAY);
1972 dprintk("channel %c clear interrupt condition\n",
1973 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1974}
1975
1976static int vino_capture(struct vino_channel_settings *vcs,
1977 struct vino_framebuffer *fb)
1978{
1979 int err = 0;
1980 unsigned long flags, flags2;
1981
1982 spin_lock_irqsave(&fb->state_lock, flags);
1983
1984 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1985 err = -EBUSY;
1986 fb->state = VINO_FRAMEBUFFER_IN_USE;
1987
1988 spin_unlock_irqrestore(&fb->state_lock, flags);
1989
1990 if (err)
1991 return err;
1992
1993 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1994 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1995
1996 vino_dma_setup(vcs, fb);
1997 vino_dma_start(vcs);
1998
1999 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
2000 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2001
2002 return err;
2003}
2004
2005static
2006struct vino_framebuffer *vino_capture_enqueue(struct
2007 vino_channel_settings *vcs,
2008 unsigned int index)
2009{
2010 struct vino_framebuffer *fb;
2011 unsigned long flags;
2012
2013 dprintk("vino_capture_enqueue():\n");
2014
2015 spin_lock_irqsave(&vcs->capture_lock, flags);
2016
2017 fb = vino_queue_add(&vcs->fb_queue, index);
2018 if (fb == NULL) {
2019 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
2020 "queue full?\n");
2021 goto out;
2022 }
2023out:
2024 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2025
2026 return fb;
2027}
2028
2029static int vino_capture_next(struct vino_channel_settings *vcs, int start)
2030{
2031 struct vino_framebuffer *fb;
2032 unsigned int incoming, id;
2033 int err = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002034 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002035
2036 dprintk("vino_capture_next():\n");
2037
2038 spin_lock_irqsave(&vcs->capture_lock, flags);
2039
2040 if (start) {
2041 /* start capture only if capture isn't in progress already */
2042 if (vcs->capturing) {
2043 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2044 return 0;
2045 }
2046
2047 } else {
2048 /* capture next frame:
2049 * stop capture if capturing is not set */
2050 if (!vcs->capturing) {
2051 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2052 return 0;
2053 }
2054 }
2055
2056 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2057 if (err) {
2058 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2059 "failed\n");
2060 err = -EINVAL;
2061 goto out;
2062 }
2063 if (incoming == 0) {
2064 dprintk("vino_capture_next(): no buffers available\n");
2065 goto out;
2066 }
2067
2068 fb = vino_queue_peek(&vcs->fb_queue, &id);
2069 if (fb == NULL) {
2070 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2071 err = -EINVAL;
2072 goto out;
2073 }
2074
Ralf Baechled203a7e2005-09-06 15:19:37 -07002075 if (start) {
2076 vcs->capturing = 1;
2077 }
2078
2079 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2080
2081 err = vino_capture(vcs, fb);
2082
2083 return err;
2084
2085out:
2086 vcs->capturing = 0;
2087 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2088
2089 return err;
2090}
2091
Ladislav Michla637a112005-09-01 15:07:34 +00002092static inline int vino_is_capturing(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002093{
2094 int ret;
2095 unsigned long flags;
2096
2097 spin_lock_irqsave(&vcs->capture_lock, flags);
2098
2099 ret = vcs->capturing;
2100
2101 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2102
2103 return ret;
2104}
2105
2106/* waits until a frame is captured */
2107static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2108{
2109 wait_queue_t wait;
2110 int err = 0;
2111
2112 dprintk("vino_wait_for_frame():\n");
2113
2114 init_waitqueue_entry(&wait, current);
2115 /* add ourselves into wait queue */
2116 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002117
2118 /* to ensure that schedule_timeout will return immediately
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -03002119 * if VINO interrupt was triggered meanwhile */
Mauro Carvalho Chehab818ca472007-07-17 16:29:07 -03002120 schedule_timeout_interruptible(msecs_to_jiffies(100));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002121
2122 if (signal_pending(current))
2123 err = -EINTR;
2124
2125 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2126
2127 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2128 err ? "failed" : "ok");
2129
2130 return err;
2131}
2132
2133/* the function assumes that PAGE_SIZE % 4 == 0 */
2134static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2135 unsigned char *pageptr;
2136 unsigned int page, i;
2137 unsigned char a;
2138
2139 for (page = 0; page < fb->desc_table.page_count; page++) {
2140 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2141
2142 for (i = 0; i < PAGE_SIZE; i += 4) {
2143 a = pageptr[0];
2144 pageptr[0] = pageptr[3];
2145 pageptr[1] = pageptr[2];
2146 pageptr[2] = pageptr[1];
2147 pageptr[3] = a;
2148 pageptr += 4;
2149 }
2150 }
2151}
2152
2153/* checks if the buffer is in correct state and syncs data */
2154static int vino_check_buffer(struct vino_channel_settings *vcs,
2155 struct vino_framebuffer *fb)
2156{
2157 int err = 0;
2158 unsigned long flags;
2159
2160 dprintk("vino_check_buffer():\n");
2161
2162 spin_lock_irqsave(&fb->state_lock, flags);
2163 switch (fb->state) {
2164 case VINO_FRAMEBUFFER_IN_USE:
2165 err = -EIO;
2166 break;
2167 case VINO_FRAMEBUFFER_READY:
2168 vino_sync_buffer(fb);
2169 fb->state = VINO_FRAMEBUFFER_UNUSED;
2170 break;
2171 default:
2172 err = -EINVAL;
2173 }
2174 spin_unlock_irqrestore(&fb->state_lock, flags);
2175
2176 if (!err) {
2177 if (vino_pixel_conversion
2178 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2179 vino_convert_to_rgba(fb);
2180 }
2181 } else if (err && (err != -EINVAL)) {
2182 dprintk("vino_check_buffer(): buffer not ready\n");
2183
2184 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2185 vino_dma_stop(vcs);
2186 vino_clear_interrupt(vcs);
2187 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2188 }
2189
2190 return err;
2191}
2192
2193/* forcefully terminates capture */
2194static void vino_capture_stop(struct vino_channel_settings *vcs)
2195{
2196 unsigned int incoming = 0, outgoing = 0, id;
2197 unsigned long flags, flags2;
2198
2199 dprintk("vino_capture_stop():\n");
2200
2201 spin_lock_irqsave(&vcs->capture_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00002202
Ralf Baechled203a7e2005-09-06 15:19:37 -07002203 /* unset capturing to stop queue processing */
2204 vcs->capturing = 0;
2205
2206 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2207
2208 vino_dma_stop(vcs);
2209 vino_clear_interrupt(vcs);
2210
2211 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2212
2213 /* remove all items from the queue */
2214 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2215 dprintk("vino_capture_stop(): "
2216 "vino_queue_get_incoming() failed\n");
2217 goto out;
2218 }
2219 while (incoming > 0) {
2220 vino_queue_transfer(&vcs->fb_queue);
2221
2222 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2223 dprintk("vino_capture_stop(): "
2224 "vino_queue_get_incoming() failed\n");
2225 goto out;
2226 }
2227 }
2228
2229 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2230 dprintk("vino_capture_stop(): "
2231 "vino_queue_get_outgoing() failed\n");
2232 goto out;
2233 }
2234 while (outgoing > 0) {
2235 vino_queue_remove(&vcs->fb_queue, &id);
2236
2237 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2238 dprintk("vino_capture_stop(): "
2239 "vino_queue_get_outgoing() failed\n");
2240 goto out;
2241 }
2242 }
2243
2244out:
2245 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2246}
2247
Ladislav Michla637a112005-09-01 15:07:34 +00002248#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07002249static int vino_capture_failed(struct vino_channel_settings *vcs)
2250{
2251 struct vino_framebuffer *fb;
2252 unsigned long flags;
2253 unsigned int i;
2254 int ret;
2255
2256 dprintk("vino_capture_failed():\n");
2257
2258 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2259
2260 vino_dma_stop(vcs);
2261 vino_clear_interrupt(vcs);
2262
2263 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2264
2265 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2266 if (ret == VINO_QUEUE_ERROR) {
2267 dprintk("vino_queue_get_incoming() failed\n");
2268 return -EINVAL;
2269 }
2270 if (i == 0) {
2271 /* no buffers to process */
2272 return 0;
2273 }
2274
2275 fb = vino_queue_peek(&vcs->fb_queue, &i);
2276 if (fb == NULL) {
2277 dprintk("vino_queue_peek() failed\n");
2278 return -EINVAL;
2279 }
2280
2281 spin_lock_irqsave(&fb->state_lock, flags);
2282 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2283 fb->state = VINO_FRAMEBUFFER_UNUSED;
2284 vino_queue_transfer(&vcs->fb_queue);
2285 vino_queue_remove(&vcs->fb_queue, &i);
2286 /* we should actually discard the newest frame,
2287 * but who cares ... */
2288 }
2289 spin_unlock_irqrestore(&fb->state_lock, flags);
2290
2291 return 0;
2292}
Ladislav Michla637a112005-09-01 15:07:34 +00002293#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002294
Ladislav Michla637a112005-09-01 15:07:34 +00002295static void vino_skip_frame(struct vino_channel_settings *vcs)
2296{
2297 struct vino_framebuffer *fb;
2298 unsigned long flags;
2299 unsigned int id;
2300
2301 spin_lock_irqsave(&vcs->capture_lock, flags);
2302 fb = vino_queue_peek(&vcs->fb_queue, &id);
2303 if (!fb) {
2304 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2305 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2306 return;
2307 }
2308 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2309
2310 spin_lock_irqsave(&fb->state_lock, flags);
2311 fb->state = VINO_FRAMEBUFFER_UNUSED;
2312 spin_unlock_irqrestore(&fb->state_lock, flags);
2313
2314 vino_capture_next(vcs, 0);
2315}
2316
2317static void vino_frame_done(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002318{
2319 struct vino_framebuffer *fb;
2320 unsigned long flags;
2321
2322 spin_lock_irqsave(&vcs->capture_lock, flags);
2323 fb = vino_queue_transfer(&vcs->fb_queue);
2324 if (!fb) {
2325 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2326 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2327 return;
2328 }
2329 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2330
Ladislav Michla637a112005-09-01 15:07:34 +00002331 fb->frame_counter = vcs->int_data.frame_counter;
2332 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2333 sizeof(struct timeval));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002334
2335 spin_lock_irqsave(&fb->state_lock, flags);
2336 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2337 fb->state = VINO_FRAMEBUFFER_READY;
2338 spin_unlock_irqrestore(&fb->state_lock, flags);
2339
2340 wake_up(&vcs->fb_queue.frame_wait_queue);
2341
2342 vino_capture_next(vcs, 0);
2343}
2344
Ladislav Michla637a112005-09-01 15:07:34 +00002345static void vino_capture_tasklet(unsigned long channel) {
2346 struct vino_channel_settings *vcs;
2347
2348 vcs = (channel == VINO_CHANNEL_A)
2349 ? &vino_drvdata->a : &vino_drvdata->b;
2350
2351 if (vcs->int_data.skip)
2352 vcs->int_data.skip_count++;
2353
2354 if (vcs->int_data.skip && (vcs->int_data.skip_count
2355 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2356 vino_skip_frame(vcs);
2357 } else {
2358 vcs->int_data.skip_count = 0;
2359 vino_frame_done(vcs);
2360 }
2361}
2362
David Howells7d12e782006-10-05 14:55:46 +01002363static irqreturn_t vino_interrupt(int irq, void *dev_id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002364{
Ladislav Michla637a112005-09-01 15:07:34 +00002365 u32 ctrl, intr;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002366 unsigned int fc_a, fc_b;
Ladislav Michla637a112005-09-01 15:07:34 +00002367 int handled_a = 0, skip_a = 0, done_a = 0;
2368 int handled_b = 0, skip_b = 0, done_b = 0;
2369
2370#ifdef VINO_DEBUG_INT
2371 int loop = 0;
2372 unsigned int line_count = vino->a.line_count,
2373 page_index = vino->a.page_index,
2374 field_counter = vino->a.field_counter,
2375 start_desc_tbl = vino->a.start_desc_tbl,
2376 next_4_desc = vino->a.next_4_desc;
2377 unsigned int line_count_2,
2378 page_index_2,
2379 field_counter_2,
2380 start_desc_tbl_2,
2381 next_4_desc_2;
2382#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002383
2384 spin_lock(&vino_drvdata->vino_lock);
2385
Ladislav Michla637a112005-09-01 15:07:34 +00002386 while ((intr = vino->intr_status)) {
2387 fc_a = vino->a.field_counter >> 1;
2388 fc_b = vino->b.field_counter >> 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002389
Ladislav Michla637a112005-09-01 15:07:34 +00002390 /* handle error-interrupts in some special way ?
2391 * --> skips frames */
2392 if (intr & VINO_INTSTAT_A) {
2393 if (intr & VINO_INTSTAT_A_EOF) {
2394 vino_drvdata->a.field++;
2395 if (vino_drvdata->a.field > 1) {
2396 vino_dma_stop(&vino_drvdata->a);
2397 vino_clear_interrupt(&vino_drvdata->a);
2398 vino_drvdata->a.field = 0;
2399 done_a = 1;
2400 } else {
2401 if (vino->a.page_index
2402 != vino_drvdata->a.line_size) {
2403 vino->a.line_count = 0;
2404 vino->a.page_index =
2405 vino_drvdata->
2406 a.line_size;
2407 vino->a.next_4_desc =
2408 vino->a.start_desc_tbl;
2409 }
2410 }
2411 dprintk("channel A end-of-field "
2412 "interrupt: %04x\n", intr);
2413 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002414 vino_dma_stop(&vino_drvdata->a);
2415 vino_clear_interrupt(&vino_drvdata->a);
2416 vino_drvdata->a.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002417 skip_a = 1;
2418 dprintk("channel A error interrupt: %04x\n",
2419 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002420 }
Ladislav Michla637a112005-09-01 15:07:34 +00002421
2422#ifdef VINO_DEBUG_INT
2423 line_count_2 = vino->a.line_count;
2424 page_index_2 = vino->a.page_index;
2425 field_counter_2 = vino->a.field_counter;
2426 start_desc_tbl_2 = vino->a.start_desc_tbl;
2427 next_4_desc_2 = vino->a.next_4_desc;
2428
2429 printk("intr = %04x, loop = %d, field = %d\n",
2430 intr, loop, vino_drvdata->a.field);
2431 printk("1- line count = %04d, page index = %04d, "
2432 "start = %08x, next = %08x\n"
2433 " fieldc = %d, framec = %d\n",
2434 line_count, page_index, start_desc_tbl,
2435 next_4_desc, field_counter, fc_a);
2436 printk("12-line count = %04d, page index = %04d, "
2437 " start = %08x, next = %08x\n",
2438 line_count_2, page_index_2, start_desc_tbl_2,
2439 next_4_desc_2);
2440
2441 if (done_a)
2442 printk("\n");
2443#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002444 }
Ladislav Michla637a112005-09-01 15:07:34 +00002445
2446 if (intr & VINO_INTSTAT_B) {
2447 if (intr & VINO_INTSTAT_B_EOF) {
2448 vino_drvdata->b.field++;
2449 if (vino_drvdata->b.field > 1) {
2450 vino_dma_stop(&vino_drvdata->b);
2451 vino_clear_interrupt(&vino_drvdata->b);
2452 vino_drvdata->b.field = 0;
2453 done_b = 1;
2454 }
2455 dprintk("channel B end-of-field "
2456 "interrupt: %04x\n", intr);
2457 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002458 vino_dma_stop(&vino_drvdata->b);
2459 vino_clear_interrupt(&vino_drvdata->b);
2460 vino_drvdata->b.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002461 skip_b = 1;
2462 dprintk("channel B error interrupt: %04x\n",
2463 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002464 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002465 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002466
Ladislav Michla637a112005-09-01 15:07:34 +00002467 /* Always remember to clear interrupt status.
2468 * Disable VINO interrupts while we do this. */
2469 ctrl = vino->control;
2470 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2471 vino->intr_status = ~intr;
2472 vino->control = ctrl;
2473
2474 spin_unlock(&vino_drvdata->vino_lock);
2475
2476 if ((!handled_a) && (done_a || skip_a)) {
2477 if (!skip_a) {
2478 do_gettimeofday(&vino_drvdata->
2479 a.int_data.timestamp);
2480 vino_drvdata->a.int_data.frame_counter = fc_a;
2481 }
2482 vino_drvdata->a.int_data.skip = skip_a;
2483
2484 dprintk("channel A %s, interrupt: %d\n",
2485 skip_a ? "skipping frame" : "frame done",
2486 intr);
2487 tasklet_hi_schedule(&vino_tasklet_a);
2488 handled_a = 1;
2489 }
2490
2491 if ((!handled_b) && (done_b || skip_b)) {
2492 if (!skip_b) {
2493 do_gettimeofday(&vino_drvdata->
2494 b.int_data.timestamp);
2495 vino_drvdata->b.int_data.frame_counter = fc_b;
2496 }
2497 vino_drvdata->b.int_data.skip = skip_b;
2498
2499 dprintk("channel B %s, interrupt: %d\n",
2500 skip_b ? "skipping frame" : "frame done",
2501 intr);
2502 tasklet_hi_schedule(&vino_tasklet_b);
2503 handled_b = 1;
2504 }
2505
2506#ifdef VINO_DEBUG_INT
2507 loop++;
2508#endif
2509 spin_lock(&vino_drvdata->vino_lock);
2510 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002511
2512 spin_unlock(&vino_drvdata->vino_lock);
2513
Ralf Baechled203a7e2005-09-06 15:19:37 -07002514 return IRQ_HANDLED;
2515}
2516
2517/* VINO video input management */
2518
2519static int vino_get_saa7191_input(int input)
2520{
2521 switch (input) {
2522 case VINO_INPUT_COMPOSITE:
2523 return SAA7191_INPUT_COMPOSITE;
2524 case VINO_INPUT_SVIDEO:
2525 return SAA7191_INPUT_SVIDEO;
2526 default:
2527 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2528 "invalid input!\n");
2529 return -1;
2530 }
2531}
2532
Ralf Baechled203a7e2005-09-06 15:19:37 -07002533/* execute with input_lock locked */
2534static int vino_is_input_owner(struct vino_channel_settings *vcs)
2535{
2536 switch(vcs->input) {
2537 case VINO_INPUT_COMPOSITE:
2538 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002539 return vino_drvdata->decoder_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002540 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002541 return vino_drvdata->camera_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002542 default:
2543 return 0;
2544 }
2545}
2546
2547static int vino_acquire_input(struct vino_channel_settings *vcs)
2548{
Ladislav Michla637a112005-09-01 15:07:34 +00002549 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002550 int ret = 0;
2551
2552 dprintk("vino_acquire_input():\n");
2553
Ladislav Michla637a112005-09-01 15:07:34 +00002554 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002555
2556 /* First try D1 and then SAA7191 */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002557 if (vino_drvdata->camera
2558 && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2559 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002560 vcs->input = VINO_INPUT_D1;
2561 vcs->data_norm = VINO_DATA_NORM_D1;
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002562 } else if (vino_drvdata->decoder
2563 && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002564 int input;
2565 int data_norm;
2566 v4l2_std_id norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002567
Ladislav Michla637a112005-09-01 15:07:34 +00002568 input = VINO_INPUT_COMPOSITE;
2569
Hans Verkuil5325b422009-04-02 11:26:22 -03002570 ret = decoder_call(video, s_routing,
2571 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002572 if (ret) {
2573 ret = -EINVAL;
2574 goto out;
2575 }
2576
2577 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2578
2579 /* Don't hold spinlocks while auto-detecting norm
2580 * as it may take a while... */
2581
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002582 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002583 if (!ret) {
2584 for (data_norm = 0; data_norm < 3; data_norm++) {
2585 if (vino_data_norms[data_norm].std & norm)
2586 break;
2587 }
2588 if (data_norm == 3)
2589 data_norm = VINO_DATA_NORM_PAL;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002590 ret = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002591 }
2592
2593 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2594
2595 if (ret) {
2596 ret = -EINVAL;
2597 goto out;
2598 }
2599
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002600 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002601
Ladislav Michla637a112005-09-01 15:07:34 +00002602 vcs->input = input;
2603 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002604 } else {
2605 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2606 vino_drvdata->b.input : vino_drvdata->a.input;
2607 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2608 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2609 }
2610
2611 if (vcs->input == VINO_INPUT_NONE) {
2612 ret = -ENODEV;
2613 goto out;
2614 }
2615
Ladislav Michla637a112005-09-01 15:07:34 +00002616 vino_set_default_clipping(vcs);
2617 vino_set_default_scaling(vcs);
2618 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002619
2620 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2621
2622out:
Ladislav Michla637a112005-09-01 15:07:34 +00002623 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002624
2625 return ret;
2626}
2627
2628static int vino_set_input(struct vino_channel_settings *vcs, int input)
2629{
2630 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2631 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002632 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002633 int ret = 0;
2634
2635 dprintk("vino_set_input():\n");
2636
Ladislav Michla637a112005-09-01 15:07:34 +00002637 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002638
2639 if (vcs->input == input)
2640 goto out;
2641
Ladislav Michla637a112005-09-01 15:07:34 +00002642 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002643 case VINO_INPUT_COMPOSITE:
2644 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002645 if (!vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002646 ret = -EINVAL;
2647 goto out;
2648 }
2649
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002650 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2651 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002652 }
2653
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002654 if (vino_drvdata->decoder_owner == vcs->channel) {
Ladislav Michla637a112005-09-01 15:07:34 +00002655 int data_norm;
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002656 v4l2_std_id norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002657
Hans Verkuil5325b422009-04-02 11:26:22 -03002658 ret = decoder_call(video, s_routing,
2659 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002660 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002661 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002662 ret = -EINVAL;
2663 goto out;
2664 }
2665
2666 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2667
2668 /* Don't hold spinlocks while auto-detecting norm
2669 * as it may take a while... */
2670
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002671 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002672 if (!ret) {
2673 for (data_norm = 0; data_norm < 3; data_norm++) {
2674 if (vino_data_norms[data_norm].std & norm)
2675 break;
2676 }
2677 if (data_norm == 3)
2678 data_norm = VINO_DATA_NORM_PAL;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002679 ret = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002680 }
2681
2682 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2683
2684 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002685 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002686 ret = -EINVAL;
2687 goto out;
2688 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002689
2690 vcs->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +00002691 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002692 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00002693 if (input != vcs2->input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002694 ret = -EBUSY;
2695 goto out;
2696 }
2697
2698 vcs->input = input;
2699 vcs->data_norm = vcs2->data_norm;
2700 }
2701
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002702 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002703 /* Transfer the ownership or release the input */
2704 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002705 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002706 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002707 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002708 }
2709 }
2710 break;
2711 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002712 if (!vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002713 ret = -EINVAL;
2714 goto out;
2715 }
2716
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002717 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2718 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002719
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002720 if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002721 /* Transfer the ownership or release the input */
2722 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2723 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002724 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002725 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002726 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002727 }
2728 }
2729
2730 vcs->input = input;
2731 vcs->data_norm = VINO_DATA_NORM_D1;
2732 break;
2733 default:
2734 ret = -EINVAL;
2735 goto out;
2736 }
2737
2738 vino_set_default_clipping(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002739 vino_set_default_scaling(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002740 vino_set_default_framerate(vcs);
2741
2742 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2743
2744out:
Ladislav Michla637a112005-09-01 15:07:34 +00002745 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002746
2747 return ret;
2748}
2749
2750static void vino_release_input(struct vino_channel_settings *vcs)
2751{
2752 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2753 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002754 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002755
2756 dprintk("vino_release_input():\n");
2757
Ladislav Michla637a112005-09-01 15:07:34 +00002758 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002759
2760 /* Release ownership of the channel
2761 * and if the other channel takes input from
2762 * the same source, transfer the ownership */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002763 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002764 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002765 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002766 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002767 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002768 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002769 } else if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002770 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2771 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002772 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002773 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002774 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002775 }
2776 }
2777 vcs->input = VINO_INPUT_NONE;
2778
Ladislav Michla637a112005-09-01 15:07:34 +00002779 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002780}
2781
2782/* execute with input_lock locked */
2783static int vino_set_data_norm(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00002784 unsigned int data_norm,
2785 unsigned long *flags)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002786{
Ladislav Michla637a112005-09-01 15:07:34 +00002787 int err = 0;
2788
2789 if (data_norm == vcs->data_norm)
2790 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002791
2792 switch (vcs->input) {
2793 case VINO_INPUT_D1:
2794 /* only one "norm" supported */
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002795 if (data_norm != VINO_DATA_NORM_D1)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002796 return -EINVAL;
2797 break;
2798 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00002799 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002800 v4l2_std_id norm;
2801
Ladislav Michla637a112005-09-01 15:07:34 +00002802 if ((data_norm != VINO_DATA_NORM_PAL)
2803 && (data_norm != VINO_DATA_NORM_NTSC)
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002804 && (data_norm != VINO_DATA_NORM_SECAM))
Ladislav Michla637a112005-09-01 15:07:34 +00002805 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002806
Ladislav Michla637a112005-09-01 15:07:34 +00002807 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002808
Ladislav Michla637a112005-09-01 15:07:34 +00002809 /* Don't hold spinlocks while setting norm
2810 * as it may take a while... */
2811
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002812 norm = vino_data_norms[data_norm].std;
Hans Verkuilf41737e2009-04-01 03:52:39 -03002813 err = decoder_call(core, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002814
2815 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2816
2817 if (err)
2818 goto out;
2819
Ralf Baechled203a7e2005-09-06 15:19:37 -07002820 vcs->data_norm = data_norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002821
2822 vino_set_default_clipping(vcs);
2823 vino_set_default_scaling(vcs);
2824 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002825 break;
Ladislav Michla637a112005-09-01 15:07:34 +00002826 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002827 default:
2828 return -EINVAL;
2829 }
2830
Ladislav Michla637a112005-09-01 15:07:34 +00002831out:
2832 return err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002833}
2834
2835/* V4L2 helper functions */
2836
2837static int vino_find_data_format(__u32 pixelformat)
2838{
2839 int i;
2840
2841 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2842 if (vino_data_formats[i].pixelformat == pixelformat)
2843 return i;
2844 }
2845
2846 return VINO_DATA_FMT_NONE;
2847}
2848
Hans Verkuilc49cb362009-02-12 10:32:50 -03002849static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002850{
2851 int input = VINO_INPUT_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002852 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002853
Ladislav Michla637a112005-09-01 15:07:34 +00002854 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002855 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002856 switch (index) {
2857 case 0:
2858 input = VINO_INPUT_COMPOSITE;
2859 break;
2860 case 1:
2861 input = VINO_INPUT_SVIDEO;
2862 break;
2863 case 2:
2864 input = VINO_INPUT_D1;
2865 break;
2866 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002867 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002868 switch (index) {
2869 case 0:
2870 input = VINO_INPUT_COMPOSITE;
2871 break;
2872 case 1:
2873 input = VINO_INPUT_SVIDEO;
2874 break;
2875 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002876 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002877 switch (index) {
2878 case 0:
2879 input = VINO_INPUT_D1;
2880 break;
2881 }
2882 }
Ladislav Michla637a112005-09-01 15:07:34 +00002883 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002884
2885 return input;
2886}
2887
2888/* execute with input_lock locked */
2889static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2890{
2891 __u32 index = 0;
2892 // FIXME: detect when no inputs available
2893
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002894 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002895 switch (vcs->input) {
2896 case VINO_INPUT_COMPOSITE:
2897 index = 0;
2898 break;
2899 case VINO_INPUT_SVIDEO:
2900 index = 1;
2901 break;
2902 case VINO_INPUT_D1:
2903 index = 2;
2904 break;
2905 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002906 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002907 switch (vcs->input) {
2908 case VINO_INPUT_COMPOSITE:
2909 index = 0;
2910 break;
2911 case VINO_INPUT_SVIDEO:
2912 index = 1;
2913 break;
2914 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002915 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002916 switch (vcs->input) {
2917 case VINO_INPUT_D1:
2918 index = 0;
2919 break;
2920 }
2921 }
2922
2923 return index;
2924}
2925
2926/* V4L2 ioctls */
2927
Hans Verkuilc49cb362009-02-12 10:32:50 -03002928static int vino_querycap(struct file *file, void *__fh,
2929 struct v4l2_capability *cap)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002930{
2931 memset(cap, 0, sizeof(struct v4l2_capability));
2932
2933 strcpy(cap->driver, vino_driver_name);
2934 strcpy(cap->card, vino_driver_description);
2935 strcpy(cap->bus_info, vino_bus_name);
2936 cap->version = VINO_VERSION_CODE;
2937 cap->capabilities =
2938 V4L2_CAP_VIDEO_CAPTURE |
2939 V4L2_CAP_STREAMING;
2940 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
Hans Verkuilc49cb362009-02-12 10:32:50 -03002941 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002942}
2943
Hans Verkuilc49cb362009-02-12 10:32:50 -03002944static int vino_enum_input(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07002945 struct v4l2_input *i)
2946{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002947 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002948 __u32 index = i->index;
2949 int input;
2950 dprintk("requested index = %d\n", index);
2951
Hans Verkuilc49cb362009-02-12 10:32:50 -03002952 input = vino_int_enum_input(vcs, index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002953 if (input == VINO_INPUT_NONE)
2954 return -EINVAL;
2955
2956 memset(i, 0, sizeof(struct v4l2_input));
2957
2958 i->index = index;
2959 i->type = V4L2_INPUT_TYPE_CAMERA;
2960 i->std = vino_inputs[input].std;
2961 strcpy(i->name, vino_inputs[input].name);
2962
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002963 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002964 decoder_call(video, g_input_status, &i->status);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002965 return 0;
2966}
2967
Hans Verkuilc49cb362009-02-12 10:32:50 -03002968static int vino_g_input(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00002969 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002970{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002971 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002972 __u32 index;
2973 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00002974 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002975
Ladislav Michla637a112005-09-01 15:07:34 +00002976 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002977 input = vcs->input;
2978 index = vino_find_input_index(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002979 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002980
2981 dprintk("input = %d\n", input);
2982
2983 if (input == VINO_INPUT_NONE) {
2984 return -EINVAL;
2985 }
2986
Ladislav Michla637a112005-09-01 15:07:34 +00002987 *i = index;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002988
2989 return 0;
2990}
2991
Hans Verkuilc49cb362009-02-12 10:32:50 -03002992static int vino_s_input(struct file *file, void *__fh,
2993 unsigned int i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002994{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002995 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002996 int input;
Hans Verkuilc49cb362009-02-12 10:32:50 -03002997 dprintk("requested input = %d\n", i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002998
Hans Verkuilc49cb362009-02-12 10:32:50 -03002999 input = vino_int_enum_input(vcs, i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003000 if (input == VINO_INPUT_NONE)
3001 return -EINVAL;
3002
3003 return vino_set_input(vcs, input);
3004}
3005
Hans Verkuilc49cb362009-02-12 10:32:50 -03003006static int vino_querystd(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00003007 v4l2_std_id *std)
3008{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003009 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003010 unsigned long flags;
3011 int err = 0;
3012
3013 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3014
3015 switch (vcs->input) {
3016 case VINO_INPUT_D1:
3017 *std = vino_inputs[vcs->input].std;
3018 break;
3019 case VINO_INPUT_COMPOSITE:
3020 case VINO_INPUT_SVIDEO: {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003021 decoder_call(video, querystd, std);
Ladislav Michla637a112005-09-01 15:07:34 +00003022 break;
3023 }
3024 default:
3025 err = -EINVAL;
3026 }
3027
3028 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3029
3030 return err;
3031}
3032
Hans Verkuilc49cb362009-02-12 10:32:50 -03003033static int vino_g_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003034 v4l2_std_id *std)
3035{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003036 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003037 unsigned long flags;
3038
3039 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3040
Ralf Baechled203a7e2005-09-06 15:19:37 -07003041 *std = vino_data_norms[vcs->data_norm].std;
Ladislav Michla637a112005-09-01 15:07:34 +00003042 dprintk("current standard = %d\n", vcs->data_norm);
3043
3044 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003045
3046 return 0;
3047}
3048
Hans Verkuilc49cb362009-02-12 10:32:50 -03003049static int vino_s_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003050 v4l2_std_id *std)
3051{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003052 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003053 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003054 int ret = 0;
3055
Ladislav Michla637a112005-09-01 15:07:34 +00003056 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3057
3058 if (!vino_is_input_owner(vcs)) {
3059 ret = -EBUSY;
3060 goto out;
3061 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003062
3063 /* check if the standard is valid for the current input */
Ladislav Michla637a112005-09-01 15:07:34 +00003064 if ((*std) & vino_inputs[vcs->input].std) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003065 dprintk("standard accepted\n");
3066
3067 /* change the video norm for SAA7191
3068 * and accept NTSC for D1 (do nothing) */
3069
3070 if (vcs->input == VINO_INPUT_D1)
3071 goto out;
3072
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003073 if ((*std) & V4L2_STD_PAL) {
Ladislav Michla637a112005-09-01 15:07:34 +00003074 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3075 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003076 } else if ((*std) & V4L2_STD_NTSC) {
Ladislav Michla637a112005-09-01 15:07:34 +00003077 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3078 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003079 } else if ((*std) & V4L2_STD_SECAM) {
Ladislav Michla637a112005-09-01 15:07:34 +00003080 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3081 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003082 } else {
3083 ret = -EINVAL;
3084 }
Ladislav Michla637a112005-09-01 15:07:34 +00003085
3086 if (ret) {
3087 ret = -EINVAL;
3088 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003089 } else {
3090 ret = -EINVAL;
3091 }
3092
3093out:
Ladislav Michla637a112005-09-01 15:07:34 +00003094 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003095
3096 return ret;
3097}
3098
Hans Verkuilc49cb362009-02-12 10:32:50 -03003099static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003100 struct v4l2_fmtdesc *fd)
3101{
Trent Piepho4f5a7442009-03-28 22:25:36 -03003102 dprintk("format index = %d\n", fd->index);
Hans Verkuilc49cb362009-02-12 10:32:50 -03003103
Trent Piepho4f5a7442009-03-28 22:25:36 -03003104 if (fd->index >= VINO_DATA_FMT_COUNT)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003105 return -EINVAL;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003106 dprintk("format name = %s\n", vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003107
Trent Piepho4f5a7442009-03-28 22:25:36 -03003108 fd->pixelformat = vino_data_formats[fd->index].pixelformat;
3109 strcpy(fd->description, vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003110 return 0;
3111}
3112
Hans Verkuilc49cb362009-02-12 10:32:50 -03003113static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003114 struct v4l2_format *f)
3115{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003116 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003117 struct vino_channel_settings tempvcs;
Ladislav Michla637a112005-09-01 15:07:34 +00003118 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003119 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003120
Hans Verkuilc49cb362009-02-12 10:32:50 -03003121 dprintk("requested: w = %d, h = %d\n",
3122 pf->width, pf->height);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003123
Hans Verkuilc49cb362009-02-12 10:32:50 -03003124 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3125 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3126 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003127
Hans Verkuilc49cb362009-02-12 10:32:50 -03003128 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3129 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3130 tempvcs.data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003131 pf->pixelformat =
Hans Verkuilc49cb362009-02-12 10:32:50 -03003132 vino_data_formats[tempvcs.data_format].
3133 pixelformat;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003134 }
3135
Hans Verkuilc49cb362009-02-12 10:32:50 -03003136 /* data format must be set before clipping/scaling */
3137 vino_set_scaling(&tempvcs, pf->width, pf->height);
3138
3139 dprintk("data format = %s\n",
3140 vino_data_formats[tempvcs.data_format].description);
3141
3142 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3143 tempvcs.decimation;
3144 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3145 tempvcs.decimation;
3146
3147 pf->field = V4L2_FIELD_INTERLACED;
3148 pf->bytesperline = tempvcs.line_size;
3149 pf->sizeimage = tempvcs.line_size *
3150 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3151 tempvcs.decimation;
3152 pf->colorspace =
3153 vino_data_formats[tempvcs.data_format].colorspace;
3154
3155 pf->priv = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003156 return 0;
3157}
3158
Hans Verkuilc49cb362009-02-12 10:32:50 -03003159static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003160 struct v4l2_format *f)
3161{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003162 struct vino_channel_settings *vcs = video_drvdata(file);
3163 unsigned long flags;
3164 struct v4l2_pix_format *pf = &f->fmt.pix;
3165
3166 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3167
3168 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3169 vcs->decimation;
3170 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3171 vcs->decimation;
3172 pf->pixelformat =
3173 vino_data_formats[vcs->data_format].pixelformat;
3174
3175 pf->field = V4L2_FIELD_INTERLACED;
3176 pf->bytesperline = vcs->line_size;
3177 pf->sizeimage = vcs->line_size *
3178 (vcs->clipping.bottom - vcs->clipping.top) /
3179 vcs->decimation;
3180 pf->colorspace =
3181 vino_data_formats[vcs->data_format].colorspace;
3182
3183 pf->priv = 0;
3184
3185 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3186 return 0;
3187}
3188
3189static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3190 struct v4l2_format *f)
3191{
3192 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003193 int data_format;
Ladislav Michla637a112005-09-01 15:07:34 +00003194 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003195 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003196
Hans Verkuilc49cb362009-02-12 10:32:50 -03003197 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003198
Hans Verkuilc49cb362009-02-12 10:32:50 -03003199 data_format = vino_find_data_format(pf->pixelformat);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003200
Hans Verkuilc49cb362009-02-12 10:32:50 -03003201 if (data_format == VINO_DATA_FMT_NONE) {
3202 vcs->data_format = VINO_DATA_FMT_GREY;
3203 pf->pixelformat =
3204 vino_data_formats[vcs->data_format].
3205 pixelformat;
3206 } else {
3207 vcs->data_format = data_format;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003208 }
3209
Hans Verkuilc49cb362009-02-12 10:32:50 -03003210 /* data format must be set before clipping/scaling */
3211 vino_set_scaling(vcs, pf->width, pf->height);
3212
3213 dprintk("data format = %s\n",
3214 vino_data_formats[vcs->data_format].description);
3215
3216 pf->width = vcs->clipping.right - vcs->clipping.left;
3217 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3218
3219 pf->field = V4L2_FIELD_INTERLACED;
3220 pf->bytesperline = vcs->line_size;
3221 pf->sizeimage = vcs->line_size *
3222 (vcs->clipping.bottom - vcs->clipping.top) /
3223 vcs->decimation;
3224 pf->colorspace =
3225 vino_data_formats[vcs->data_format].colorspace;
3226
3227 pf->priv = 0;
3228
3229 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003230 return 0;
3231}
3232
Hans Verkuilc49cb362009-02-12 10:32:50 -03003233static int vino_cropcap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003234 struct v4l2_cropcap *ccap)
3235{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003236 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003237 const struct vino_data_norm *norm;
Ladislav Michla637a112005-09-01 15:07:34 +00003238 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003239
3240 switch (ccap->type) {
3241 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003242 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3243
Ralf Baechled203a7e2005-09-06 15:19:37 -07003244 norm = &vino_data_norms[vcs->data_norm];
Ladislav Michla637a112005-09-01 15:07:34 +00003245
3246 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003247
3248 ccap->bounds.left = 0;
3249 ccap->bounds.top = 0;
3250 ccap->bounds.width = norm->width;
3251 ccap->bounds.height = norm->height;
3252 memcpy(&ccap->defrect, &ccap->bounds,
3253 sizeof(struct v4l2_rect));
3254
3255 ccap->pixelaspect.numerator = 1;
3256 ccap->pixelaspect.denominator = 1;
3257 break;
3258 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3259 default:
3260 return -EINVAL;
3261 }
3262
3263 return 0;
3264}
3265
Hans Verkuilc49cb362009-02-12 10:32:50 -03003266static int vino_g_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003267 struct v4l2_crop *c)
3268{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003269 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003270 unsigned long flags;
3271
Ralf Baechled203a7e2005-09-06 15:19:37 -07003272 switch (c->type) {
3273 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003274 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003275
3276 c->c.left = vcs->clipping.left;
3277 c->c.top = vcs->clipping.top;
3278 c->c.width = vcs->clipping.right - vcs->clipping.left;
3279 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3280
Ladislav Michla637a112005-09-01 15:07:34 +00003281 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003282 break;
3283 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3284 default:
3285 return -EINVAL;
3286 }
3287
3288 return 0;
3289}
3290
Hans Verkuilc49cb362009-02-12 10:32:50 -03003291static int vino_s_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003292 struct v4l2_crop *c)
3293{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003294 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003295 unsigned long flags;
3296
Ralf Baechled203a7e2005-09-06 15:19:37 -07003297 switch (c->type) {
3298 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003299 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003300
Ralf Baechled203a7e2005-09-06 15:19:37 -07003301 vino_set_clipping(vcs, c->c.left, c->c.top,
3302 c->c.width, c->c.height);
3303
Ladislav Michla637a112005-09-01 15:07:34 +00003304 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003305 break;
3306 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3307 default:
3308 return -EINVAL;
3309 }
3310
3311 return 0;
3312}
3313
Hans Verkuilc49cb362009-02-12 10:32:50 -03003314static int vino_g_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003315 struct v4l2_streamparm *sp)
3316{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003317 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003318 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003319 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003320
Trent Piepho4f5a7442009-03-28 22:25:36 -03003321 cp->capability = V4L2_CAP_TIMEPERFRAME;
3322 cp->timeperframe.numerator = 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003323
Trent Piepho4f5a7442009-03-28 22:25:36 -03003324 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003325
Trent Piepho4f5a7442009-03-28 22:25:36 -03003326 cp->timeperframe.denominator = vcs->fps;
Ladislav Michla637a112005-09-01 15:07:34 +00003327
Trent Piepho4f5a7442009-03-28 22:25:36 -03003328 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00003329
Trent Piepho4f5a7442009-03-28 22:25:36 -03003330 /* TODO: cp->readbuffers = xxx; */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003331
3332 return 0;
3333}
3334
Hans Verkuilc49cb362009-02-12 10:32:50 -03003335static int vino_s_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003336 struct v4l2_streamparm *sp)
3337{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003338 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003339 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003340 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003341
Trent Piepho4f5a7442009-03-28 22:25:36 -03003342 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003343
Trent Piepho4f5a7442009-03-28 22:25:36 -03003344 if ((cp->timeperframe.numerator == 0) ||
3345 (cp->timeperframe.denominator == 0)) {
3346 /* reset framerate */
3347 vino_set_default_framerate(vcs);
3348 } else {
3349 vino_set_framerate(vcs, cp->timeperframe.denominator /
3350 cp->timeperframe.numerator);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003351 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003352
3353 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003354
3355 return 0;
3356}
3357
Hans Verkuilc49cb362009-02-12 10:32:50 -03003358static int vino_reqbufs(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003359 struct v4l2_requestbuffers *rb)
3360{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003361 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003362
Ralf Baechled203a7e2005-09-06 15:19:37 -07003363 if (vcs->reading)
3364 return -EBUSY;
3365
Trent Piepho4f5a7442009-03-28 22:25:36 -03003366 /* TODO: check queue type */
3367 if (rb->memory != V4L2_MEMORY_MMAP) {
3368 dprintk("type not mmap\n");
3369 return -EINVAL;
3370 }
3371
3372 dprintk("count = %d\n", rb->count);
3373 if (rb->count > 0) {
3374 if (vino_is_capturing(vcs)) {
3375 dprintk("busy, capturing\n");
3376 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003377 }
3378
Trent Piepho4f5a7442009-03-28 22:25:36 -03003379 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3380 dprintk("busy, buffers still mapped\n");
3381 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003382 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003383 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003384 vino_queue_free(&vcs->fb_queue);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003385 vino_queue_init(&vcs->fb_queue, &rb->count);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003386 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003387 } else {
3388 vcs->streaming = 0;
3389 vino_capture_stop(vcs);
3390 vino_queue_free(&vcs->fb_queue);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003391 }
3392
3393 return 0;
3394}
3395
3396static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3397 struct vino_framebuffer *fb,
3398 struct v4l2_buffer *b)
3399{
3400 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3401 fb->id)) {
3402 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3403 b->flags |= V4L2_BUF_FLAG_DONE;
3404 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3405 fb->id)) {
3406 b->flags &= ~V4L2_BUF_FLAG_DONE;
3407 b->flags |= V4L2_BUF_FLAG_QUEUED;
3408 } else {
3409 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3410 V4L2_BUF_FLAG_QUEUED);
3411 }
3412
3413 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3414
3415 if (fb->map_count > 0)
3416 b->flags |= V4L2_BUF_FLAG_MAPPED;
3417
3418 b->index = fb->id;
3419 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3420 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3421 b->m.offset = fb->offset;
3422 b->bytesused = fb->data_size;
3423 b->length = fb->size;
3424 b->field = V4L2_FIELD_INTERLACED;
3425 b->sequence = fb->frame_counter;
3426 memcpy(&b->timestamp, &fb->timestamp,
3427 sizeof(struct timeval));
3428 // b->input ?
3429
3430 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3431 fb->id, fb->size, fb->data_size, fb->offset);
3432}
3433
Hans Verkuilc49cb362009-02-12 10:32:50 -03003434static int vino_querybuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003435 struct v4l2_buffer *b)
3436{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003437 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003438 struct vino_framebuffer *fb;
3439
Ralf Baechled203a7e2005-09-06 15:19:37 -07003440 if (vcs->reading)
3441 return -EBUSY;
3442
Trent Piepho4f5a7442009-03-28 22:25:36 -03003443 /* TODO: check queue type */
3444 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3445 dprintk("invalid index = %d\n",
3446 b->index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003447 return -EINVAL;
3448 }
3449
Trent Piepho4f5a7442009-03-28 22:25:36 -03003450 fb = vino_queue_get_buffer(&vcs->fb_queue,
3451 b->index);
3452 if (fb == NULL) {
3453 dprintk("vino_queue_get_buffer() failed");
3454 return -EINVAL;
3455 }
3456
3457 vino_v4l2_get_buffer_status(vcs, fb, b);
3458
Ralf Baechled203a7e2005-09-06 15:19:37 -07003459 return 0;
3460}
3461
Hans Verkuilc49cb362009-02-12 10:32:50 -03003462static int vino_qbuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003463 struct v4l2_buffer *b)
3464{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003465 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003466 struct vino_framebuffer *fb;
3467 int ret;
3468
Ralf Baechled203a7e2005-09-06 15:19:37 -07003469 if (vcs->reading)
3470 return -EBUSY;
3471
Trent Piepho4f5a7442009-03-28 22:25:36 -03003472 /* TODO: check queue type */
3473 if (b->memory != V4L2_MEMORY_MMAP) {
3474 dprintk("type not mmap\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003475 return -EINVAL;
3476 }
3477
Trent Piepho4f5a7442009-03-28 22:25:36 -03003478 fb = vino_capture_enqueue(vcs, b->index);
3479 if (fb == NULL)
3480 return -EINVAL;
3481
3482 vino_v4l2_get_buffer_status(vcs, fb, b);
3483
3484 if (vcs->streaming) {
3485 ret = vino_capture_next(vcs, 1);
3486 if (ret)
3487 return ret;
3488 }
3489
Ralf Baechled203a7e2005-09-06 15:19:37 -07003490 return 0;
3491}
3492
Hans Verkuilc49cb362009-02-12 10:32:50 -03003493static int vino_dqbuf(struct file *file, void *__fh,
3494 struct v4l2_buffer *b)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003495{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003496 struct vino_channel_settings *vcs = video_drvdata(file);
3497 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003498 struct vino_framebuffer *fb;
3499 unsigned int incoming, outgoing;
3500 int err;
3501
Ralf Baechled203a7e2005-09-06 15:19:37 -07003502 if (vcs->reading)
3503 return -EBUSY;
3504
Trent Piepho4f5a7442009-03-28 22:25:36 -03003505 /* TODO: check queue type */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003506
Trent Piepho4f5a7442009-03-28 22:25:36 -03003507 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3508 if (err) {
3509 dprintk("vino_queue_get_incoming() failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003510 return -EINVAL;
3511 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003512 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3513 if (err) {
3514 dprintk("vino_queue_get_outgoing() failed\n");
3515 return -EINVAL;
3516 }
3517
3518 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3519
3520 if (outgoing == 0) {
3521 if (incoming == 0) {
3522 dprintk("no incoming or outgoing buffers\n");
3523 return -EINVAL;
3524 }
3525 if (nonblocking) {
3526 dprintk("non-blocking I/O was selected and "
3527 "there are no buffers to dequeue\n");
3528 return -EAGAIN;
3529 }
3530
3531 err = vino_wait_for_frame(vcs);
3532 if (err) {
3533 err = vino_wait_for_frame(vcs);
3534 if (err) {
3535 /* interrupted or no frames captured because of
3536 * frame skipping */
3537 /* vino_capture_failed(vcs); */
3538 return -EIO;
3539 }
3540 }
3541 }
3542
3543 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3544 if (fb == NULL) {
3545 dprintk("vino_queue_remove() failed\n");
3546 return -EINVAL;
3547 }
3548
3549 err = vino_check_buffer(vcs, fb);
3550
3551 vino_v4l2_get_buffer_status(vcs, fb, b);
3552
3553 if (err)
3554 return -EIO;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003555
3556 return 0;
3557}
3558
Hans Verkuilc49cb362009-02-12 10:32:50 -03003559static int vino_streamon(struct file *file, void *__fh,
3560 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003561{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003562 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003563 unsigned int incoming;
3564 int ret;
3565 if (vcs->reading)
3566 return -EBUSY;
3567
3568 if (vcs->streaming)
3569 return 0;
3570
3571 // TODO: check queue type
3572
3573 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3574 dprintk("no buffers allocated\n");
3575 return -EINVAL;
3576 }
3577
3578 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3579 if (ret) {
3580 dprintk("vino_queue_get_incoming() failed\n");
3581 return -EINVAL;
3582 }
3583
3584 vcs->streaming = 1;
3585
3586 if (incoming > 0) {
3587 ret = vino_capture_next(vcs, 1);
3588 if (ret) {
3589 vcs->streaming = 0;
3590
3591 dprintk("couldn't start capture\n");
3592 return -EINVAL;
3593 }
3594 }
3595
3596 return 0;
3597}
3598
Hans Verkuilc49cb362009-02-12 10:32:50 -03003599static int vino_streamoff(struct file *file, void *__fh,
3600 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003601{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003602 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003603 if (vcs->reading)
3604 return -EBUSY;
3605
3606 if (!vcs->streaming)
3607 return 0;
3608
Ralf Baechled203a7e2005-09-06 15:19:37 -07003609 vcs->streaming = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00003610 vino_capture_stop(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003611
3612 return 0;
3613}
3614
Hans Verkuilc49cb362009-02-12 10:32:50 -03003615static int vino_queryctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003616 struct v4l2_queryctrl *queryctrl)
3617{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003618 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003619 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003620 int i;
3621 int err = 0;
3622
Ladislav Michla637a112005-09-01 15:07:34 +00003623 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003624
3625 switch (vcs->input) {
3626 case VINO_INPUT_D1:
3627 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3628 if (vino_indycam_v4l2_controls[i].id ==
3629 queryctrl->id) {
3630 memcpy(queryctrl,
3631 &vino_indycam_v4l2_controls[i],
3632 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003633 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003634 goto found;
3635 }
3636 }
3637
3638 err = -EINVAL;
3639 break;
3640 case VINO_INPUT_COMPOSITE:
3641 case VINO_INPUT_SVIDEO:
3642 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3643 if (vino_saa7191_v4l2_controls[i].id ==
3644 queryctrl->id) {
3645 memcpy(queryctrl,
3646 &vino_saa7191_v4l2_controls[i],
3647 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003648 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003649 goto found;
3650 }
3651 }
3652
3653 err = -EINVAL;
3654 break;
3655 default:
3656 err = -EINVAL;
3657 }
3658
3659 found:
Ladislav Michla637a112005-09-01 15:07:34 +00003660 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003661
3662 return err;
3663}
3664
Hans Verkuilc49cb362009-02-12 10:32:50 -03003665static int vino_g_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003666 struct v4l2_control *control)
3667{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003668 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003669 unsigned long flags;
3670 int i;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003671 int err = 0;
3672
Ladislav Michla637a112005-09-01 15:07:34 +00003673 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003674
3675 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003676 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003677 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003678 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003679 if (vino_indycam_v4l2_controls[i].id == control->id) {
3680 err = 0;
3681 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003682 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003683 }
Ladislav Michla637a112005-09-01 15:07:34 +00003684
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003685 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003686 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003687
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003688 err = camera_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003689 if (err)
3690 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003691 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003692 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003693 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003694 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003695 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003696 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003697 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3698 err = 0;
3699 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003700 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003701 }
Ladislav Michla637a112005-09-01 15:07:34 +00003702
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003703 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003704 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003705
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003706 err = decoder_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003707 if (err)
3708 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003709 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003710 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003711 default:
3712 err = -EINVAL;
3713 }
3714
Ladislav Michla637a112005-09-01 15:07:34 +00003715out:
3716 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003717
3718 return err;
3719}
3720
Hans Verkuilc49cb362009-02-12 10:32:50 -03003721static int vino_s_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003722 struct v4l2_control *control)
3723{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003724 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003725 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003726 int i;
3727 int err = 0;
3728
Ladislav Michla637a112005-09-01 15:07:34 +00003729 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3730
3731 if (!vino_is_input_owner(vcs)) {
3732 err = -EBUSY;
3733 goto out;
3734 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003735
3736 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003737 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003738 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003739 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003740 if (vino_indycam_v4l2_controls[i].id == control->id) {
3741 err = 0;
3742 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003743 }
3744 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003745 if (err)
3746 goto out;
3747 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3748 control->value > vino_indycam_v4l2_controls[i].maximum) {
3749 err = -ERANGE;
3750 goto out;
3751 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003752 err = camera_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003753 if (err)
3754 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003755 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003756 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003757 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003758 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003759 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003760 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003761 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3762 err = 0;
3763 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003764 }
3765 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003766 if (err)
3767 goto out;
3768 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3769 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3770 err = -ERANGE;
3771 goto out;
3772 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003773
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003774 err = decoder_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003775 if (err)
3776 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003777 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003778 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003779 default:
3780 err = -EINVAL;
3781 }
3782
Ladislav Michla637a112005-09-01 15:07:34 +00003783out:
3784 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003785
3786 return err;
3787}
3788
3789/* File operations */
3790
Hans Verkuilbec43662008-12-30 06:58:20 -03003791static int vino_open(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003792{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003793 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003794 int ret = 0;
3795 dprintk("open(): channel = %c\n",
3796 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3797
Ingo Molnar3593cab2006-02-07 06:49:14 -02003798 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003799
3800 if (vcs->users) {
3801 dprintk("open(): driver busy\n");
3802 ret = -EBUSY;
3803 goto out;
3804 }
3805
3806 ret = vino_acquire_input(vcs);
3807 if (ret) {
3808 dprintk("open(): vino_acquire_input() failed\n");
3809 goto out;
3810 }
3811
3812 vcs->users++;
3813
3814 out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003815 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003816
3817 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3818
3819 return ret;
3820}
3821
Hans Verkuilbec43662008-12-30 06:58:20 -03003822static int vino_close(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003823{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003824 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003825 dprintk("close():\n");
3826
Ingo Molnar3593cab2006-02-07 06:49:14 -02003827 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003828
3829 vcs->users--;
3830
3831 if (!vcs->users) {
3832 vino_release_input(vcs);
3833
3834 /* stop DMA and free buffers */
3835 vino_capture_stop(vcs);
3836 vino_queue_free(&vcs->fb_queue);
3837 }
3838
Ingo Molnar3593cab2006-02-07 06:49:14 -02003839 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003840
3841 return 0;
3842}
3843
3844static void vino_vm_open(struct vm_area_struct *vma)
3845{
3846 struct vino_framebuffer *fb = vma->vm_private_data;
3847
3848 fb->map_count++;
3849 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3850}
3851
3852static void vino_vm_close(struct vm_area_struct *vma)
3853{
3854 struct vino_framebuffer *fb = vma->vm_private_data;
3855
3856 fb->map_count--;
3857 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3858}
3859
3860static struct vm_operations_struct vino_vm_ops = {
3861 .open = vino_vm_open,
3862 .close = vino_vm_close,
3863};
3864
3865static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3866{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003867 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003868
3869 unsigned long start = vma->vm_start;
3870 unsigned long size = vma->vm_end - vma->vm_start;
3871 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3872
3873 struct vino_framebuffer *fb = NULL;
3874 unsigned int i, length;
3875 int ret = 0;
3876
3877 dprintk("mmap():\n");
3878
3879 // TODO: reject mmap if already mapped
3880
Ingo Molnar3593cab2006-02-07 06:49:14 -02003881 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07003882 return -EINTR;
3883
3884 if (vcs->reading) {
3885 ret = -EBUSY;
3886 goto out;
3887 }
3888
3889 // TODO: check queue type
3890
3891 if (!(vma->vm_flags & VM_WRITE)) {
3892 dprintk("mmap(): app bug: PROT_WRITE please\n");
3893 ret = -EINVAL;
3894 goto out;
3895 }
3896 if (!(vma->vm_flags & VM_SHARED)) {
3897 dprintk("mmap(): app bug: MAP_SHARED please\n");
3898 ret = -EINVAL;
3899 goto out;
3900 }
3901
3902 /* find the correct buffer using offset */
3903 length = vino_queue_get_length(&vcs->fb_queue);
3904 if (length == 0) {
3905 dprintk("mmap(): queue not initialized\n");
3906 ret = -EINVAL;
3907 goto out;
3908 }
3909
3910 for (i = 0; i < length; i++) {
3911 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3912 if (fb == NULL) {
3913 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3914 ret = -EINVAL;
3915 goto out;
3916 }
3917
3918 if (fb->offset == offset)
3919 goto found;
3920 }
3921
3922 dprintk("mmap(): invalid offset = %lu\n", offset);
3923 ret = -EINVAL;
3924 goto out;
3925
3926found:
3927 dprintk("mmap(): buffer = %d\n", i);
3928
3929 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3930 dprintk("mmap(): failed: size = %lu > %lu\n",
3931 size, fb->desc_table.page_count * PAGE_SIZE);
3932 ret = -EINVAL;
3933 goto out;
3934 }
3935
3936 for (i = 0; i < fb->desc_table.page_count; i++) {
3937 unsigned long pfn =
3938 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3939 PAGE_SHIFT;
3940
3941 if (size < PAGE_SIZE)
3942 break;
3943
3944 // protection was: PAGE_READONLY
3945 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3946 vma->vm_page_prot)) {
3947 dprintk("mmap(): remap_pfn_range() failed\n");
3948 ret = -EAGAIN;
3949 goto out;
3950 }
3951
3952 start += PAGE_SIZE;
3953 size -= PAGE_SIZE;
3954 }
3955
3956 fb->map_count = 1;
3957
3958 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3959 vma->vm_flags &= ~VM_IO;
3960 vma->vm_private_data = fb;
3961 vma->vm_file = file;
3962 vma->vm_ops = &vino_vm_ops;
3963
3964out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003965 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003966
3967 return ret;
3968}
3969
3970static unsigned int vino_poll(struct file *file, poll_table *pt)
3971{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003972 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003973 unsigned int outgoing;
3974 unsigned int ret = 0;
3975
3976 // lock mutex (?)
3977 // TODO: this has to be corrected for different read modes
3978
3979 dprintk("poll():\n");
3980
3981 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3982 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3983 ret = POLLERR;
3984 goto error;
3985 }
3986 if (outgoing > 0)
3987 goto over;
3988
3989 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3990
3991 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3992 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3993 ret = POLLERR;
3994 goto error;
3995 }
3996
3997over:
3998 dprintk("poll(): data %savailable\n",
3999 (outgoing > 0) ? "" : "not ");
Ladislav Michla637a112005-09-01 15:07:34 +00004000
4001 if (outgoing > 0)
Ralf Baechled203a7e2005-09-06 15:19:37 -07004002 ret = POLLIN | POLLRDNORM;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004003
4004error:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004005 return ret;
4006}
4007
Hans Verkuil069b7472008-12-30 07:04:34 -03004008static long vino_ioctl(struct file *file,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004009 unsigned int cmd, unsigned long arg)
4010{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004011 struct vino_channel_settings *vcs = video_drvdata(file);
Hans Verkuil069b7472008-12-30 07:04:34 -03004012 long ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004013
Ingo Molnar3593cab2006-02-07 06:49:14 -02004014 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004015 return -EINTR;
4016
Hans Verkuilc49cb362009-02-12 10:32:50 -03004017 ret = video_ioctl2(file, cmd, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004018
Ingo Molnar3593cab2006-02-07 06:49:14 -02004019 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004020
4021 return ret;
4022}
4023
4024/* Initialization and cleanup */
4025
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03004026/* __initdata */
4027static int vino_init_stage;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004028
Hans Verkuilc49cb362009-02-12 10:32:50 -03004029const struct v4l2_ioctl_ops vino_ioctl_ops = {
4030 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4031 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4032 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4033 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4034 .vidioc_querycap = vino_querycap,
4035 .vidioc_enum_input = vino_enum_input,
4036 .vidioc_g_input = vino_g_input,
4037 .vidioc_s_input = vino_s_input,
4038 .vidioc_g_std = vino_g_std,
4039 .vidioc_s_std = vino_s_std,
4040 .vidioc_querystd = vino_querystd,
4041 .vidioc_cropcap = vino_cropcap,
4042 .vidioc_s_crop = vino_s_crop,
4043 .vidioc_g_crop = vino_g_crop,
4044 .vidioc_s_parm = vino_s_parm,
4045 .vidioc_g_parm = vino_g_parm,
4046 .vidioc_reqbufs = vino_reqbufs,
4047 .vidioc_querybuf = vino_querybuf,
4048 .vidioc_qbuf = vino_qbuf,
4049 .vidioc_dqbuf = vino_dqbuf,
4050 .vidioc_streamon = vino_streamon,
4051 .vidioc_streamoff = vino_streamoff,
4052 .vidioc_queryctrl = vino_queryctrl,
4053 .vidioc_g_ctrl = vino_g_ctrl,
4054 .vidioc_s_ctrl = vino_s_ctrl,
4055};
4056
Hans Verkuilbec43662008-12-30 06:58:20 -03004057static const struct v4l2_file_operations vino_fops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004058 .owner = THIS_MODULE,
4059 .open = vino_open,
4060 .release = vino_close,
Hans Verkuil28959632009-02-18 18:53:47 -03004061 .unlocked_ioctl = vino_ioctl,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004062 .mmap = vino_mmap,
4063 .poll = vino_poll,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004064};
4065
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004066static struct video_device vdev_template = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004067 .name = "NOT SET",
Ralf Baechled203a7e2005-09-06 15:19:37 -07004068 .fops = &vino_fops,
Hans Verkuilc49cb362009-02-12 10:32:50 -03004069 .ioctl_ops = &vino_ioctl_ops,
4070 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004071 .minor = -1,
4072};
4073
4074static void vino_module_cleanup(int stage)
4075{
4076 switch(stage) {
Hans Verkuil28959632009-02-18 18:53:47 -03004077 case 11:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004078 video_unregister_device(vino_drvdata->b.vdev);
4079 vino_drvdata->b.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004080 case 10:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004081 video_unregister_device(vino_drvdata->a.vdev);
4082 vino_drvdata->a.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004083 case 9:
Jean Delvarecea0c682009-03-06 12:05:43 -03004084 i2c_del_adapter(&vino_i2c_adapter);
Hans Verkuil28959632009-02-18 18:53:47 -03004085 case 8:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004086 free_irq(SGI_VINO_IRQ, NULL);
Hans Verkuil28959632009-02-18 18:53:47 -03004087 case 7:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004088 if (vino_drvdata->b.vdev) {
4089 video_device_release(vino_drvdata->b.vdev);
4090 vino_drvdata->b.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004091 }
Hans Verkuil28959632009-02-18 18:53:47 -03004092 case 6:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004093 if (vino_drvdata->a.vdev) {
4094 video_device_release(vino_drvdata->a.vdev);
4095 vino_drvdata->a.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004096 }
Hans Verkuil28959632009-02-18 18:53:47 -03004097 case 5:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004098 /* all entries in dma_cpu dummy table have the same address */
4099 dma_unmap_single(NULL,
4100 vino_drvdata->dummy_desc_table.dma_cpu[0],
4101 PAGE_SIZE, DMA_FROM_DEVICE);
4102 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4103 * sizeof(dma_addr_t),
4104 (void *)vino_drvdata->
4105 dummy_desc_table.dma_cpu,
4106 vino_drvdata->dummy_desc_table.dma);
Hans Verkuil28959632009-02-18 18:53:47 -03004107 case 4:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004108 free_page(vino_drvdata->dummy_page);
Hans Verkuil28959632009-02-18 18:53:47 -03004109 case 3:
4110 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004111 case 2:
4112 kfree(vino_drvdata);
4113 case 1:
4114 iounmap(vino);
4115 case 0:
4116 break;
4117 default:
4118 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4119 stage);
4120 }
4121}
4122
4123static int vino_probe(void)
4124{
4125 unsigned long rev_id;
4126
4127 if (ip22_is_fullhouse()) {
4128 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4129 return -ENODEV;
4130 }
4131
4132 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4133 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4134 return -ENODEV;
4135 }
4136
4137 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4138 if (!vino) {
4139 printk(KERN_ERR "VINO: ioremap() failed\n");
4140 return -EIO;
4141 }
4142 vino_init_stage++;
4143
4144 if (get_dbe(rev_id, &(vino->rev_id))) {
4145 printk(KERN_ERR "Failed to read VINO revision register\n");
4146 vino_module_cleanup(vino_init_stage);
4147 return -ENODEV;
4148 }
4149
4150 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4151 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4152 rev_id);
4153 vino_module_cleanup(vino_init_stage);
4154 return -ENODEV;
4155 }
4156
Ladislav Michla637a112005-09-01 15:07:34 +00004157 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004158
4159 return 0;
4160}
4161
4162static int vino_init(void)
4163{
4164 dma_addr_t dma_dummy_address;
Hans Verkuil28959632009-02-18 18:53:47 -03004165 int err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004166 int i;
4167
Panagiotis Issaris74081872006-01-11 19:40:56 -02004168 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004169 if (!vino_drvdata) {
4170 vino_module_cleanup(vino_init_stage);
4171 return -ENOMEM;
4172 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004173 vino_init_stage++;
Hans Verkuil28959632009-02-18 18:53:47 -03004174 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4175 sizeof(vino_drvdata->v4l2_dev.name));
4176 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4177 if (err)
4178 return err;
4179 vino_init_stage++;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004180
4181 /* create a dummy dma descriptor */
4182 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4183 if (!vino_drvdata->dummy_page) {
4184 vino_module_cleanup(vino_init_stage);
4185 return -ENOMEM;
4186 }
4187 vino_init_stage++;
4188
4189 // TODO: use page_count in dummy_desc_table
4190
4191 vino_drvdata->dummy_desc_table.dma_cpu =
4192 dma_alloc_coherent(NULL,
4193 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4194 &vino_drvdata->dummy_desc_table.dma,
4195 GFP_KERNEL | GFP_DMA);
4196 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4197 vino_module_cleanup(vino_init_stage);
4198 return -ENOMEM;
4199 }
4200 vino_init_stage++;
4201
4202 dma_dummy_address = dma_map_single(NULL,
4203 (void *)vino_drvdata->dummy_page,
4204 PAGE_SIZE, DMA_FROM_DEVICE);
4205 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4206 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4207 }
4208
4209 /* initialize VINO */
4210
4211 vino->control = 0;
4212 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4213 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4214 udelay(VINO_DESC_FETCH_DELAY);
4215
4216 vino->intr_status = 0;
4217
4218 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4219 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4220
4221 return 0;
4222}
4223
4224static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4225 unsigned int channel, const char *name)
4226{
4227 vcs->channel = channel;
4228 vcs->input = VINO_INPUT_NONE;
4229 vcs->alpha = 0;
4230 vcs->users = 0;
4231 vcs->data_format = VINO_DATA_FMT_GREY;
4232 vcs->data_norm = VINO_DATA_NORM_NTSC;
4233 vcs->decimation = 1;
4234 vino_set_default_clipping(vcs);
4235 vino_set_default_framerate(vcs);
4236
4237 vcs->capturing = 0;
4238
Ingo Molnar3593cab2006-02-07 06:49:14 -02004239 mutex_init(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004240 spin_lock_init(&vcs->capture_lock);
4241
Ingo Molnar3593cab2006-02-07 06:49:14 -02004242 mutex_init(&vcs->fb_queue.queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004243 spin_lock_init(&vcs->fb_queue.queue_lock);
4244 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4245
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004246 vcs->vdev = video_device_alloc();
4247 if (!vcs->vdev) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004248 vino_module_cleanup(vino_init_stage);
4249 return -ENOMEM;
4250 }
4251 vino_init_stage++;
4252
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004253 memcpy(vcs->vdev, &vdev_template,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004254 sizeof(struct video_device));
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004255 strcpy(vcs->vdev->name, name);
4256 vcs->vdev->release = video_device_release;
Hans Verkuil28959632009-02-18 18:53:47 -03004257 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004258
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004259 video_set_drvdata(vcs->vdev, vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004260
4261 return 0;
4262}
4263
4264static int __init vino_module_init(void)
4265{
4266 int ret;
4267
4268 printk(KERN_INFO "SGI VINO driver version %s\n",
4269 VINO_MODULE_VERSION);
4270
4271 ret = vino_probe();
4272 if (ret)
4273 return ret;
4274
4275 ret = vino_init();
4276 if (ret)
4277 return ret;
4278
4279 /* initialize data structures */
4280
4281 spin_lock_init(&vino_drvdata->vino_lock);
4282 spin_lock_init(&vino_drvdata->input_lock);
4283
4284 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004285 vino_vdev_name_a);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004286 if (ret)
4287 return ret;
4288
4289 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004290 vino_vdev_name_b);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004291 if (ret)
4292 return ret;
4293
4294 /* initialize hardware and register V4L devices */
4295
4296 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4297 vino_driver_description, NULL);
4298 if (ret) {
4299 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4300 SGI_VINO_IRQ);
4301 vino_module_cleanup(vino_init_stage);
4302 return -EAGAIN;
4303 }
4304 vino_init_stage++;
4305
Jean Delvarecea0c682009-03-06 12:05:43 -03004306 ret = i2c_add_adapter(&vino_i2c_adapter);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004307 if (ret) {
4308 printk(KERN_ERR "VINO I2C bus registration failed\n");
4309 vino_module_cleanup(vino_init_stage);
4310 return ret;
4311 }
Hans Verkuil28959632009-02-18 18:53:47 -03004312 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004313 vino_init_stage++;
4314
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004315 ret = video_register_device(vino_drvdata->a.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004316 VFL_TYPE_GRABBER, -1);
4317 if (ret < 0) {
4318 printk(KERN_ERR "VINO channel A Video4Linux-device "
4319 "registration failed\n");
4320 vino_module_cleanup(vino_init_stage);
4321 return -EINVAL;
4322 }
4323 vino_init_stage++;
4324
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004325 ret = video_register_device(vino_drvdata->b.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004326 VFL_TYPE_GRABBER, -1);
4327 if (ret < 0) {
4328 printk(KERN_ERR "VINO channel B Video4Linux-device "
4329 "registration failed\n");
4330 vino_module_cleanup(vino_init_stage);
4331 return -EINVAL;
4332 }
4333 vino_init_stage++;
4334
Hans Verkuile6574f22009-04-01 03:57:53 -03004335 vino_drvdata->decoder =
Hans Verkuil1792f682009-03-30 11:47:55 -03004336 v4l2_i2c_new_probed_subdev_addr(&vino_drvdata->v4l2_dev,
4337 &vino_i2c_adapter, "saa7191", "saa7191", 0x45);
Hans Verkuile6574f22009-04-01 03:57:53 -03004338 vino_drvdata->camera =
Hans Verkuil1792f682009-03-30 11:47:55 -03004339 v4l2_i2c_new_probed_subdev_addr(&vino_drvdata->v4l2_dev,
4340 &vino_i2c_adapter, "indycam", "indycam", 0x2b);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004341
4342 dprintk("init complete!\n");
4343
4344 return 0;
4345}
4346
4347static void __exit vino_module_exit(void)
4348{
4349 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4350 vino_module_cleanup(vino_init_stage);
4351 dprintk("cleanup complete, exit!\n");
4352}
4353
4354module_init(vino_module_init);
4355module_exit(vino_module_exit);