blob: 91d44ea16f275257cfc4f57d5c420b674a0495c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechled203a7e2005-09-06 15:19:37 -07002 * Driver for the VINO (Video In No Out) system found in SGI Indys.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License version 2 as published by the Free Software Foundation.
6 *
Ralf Baechled203a7e2005-09-06 15:19:37 -07007 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
8 *
9 * Based on the previous version of the driver for 2.4 kernels by:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
Hans Verkuila99e30d2009-03-06 12:15:08 -030011 *
12 * v4l2_device/v4l2_subdev conversion by:
13 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
14 *
15 * Note: this conversion is untested! Please contact the linux-media
16 * mailinglist if you can test this, together with the test results.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Ralf Baechled203a7e2005-09-06 15:19:37 -070019/*
20 * TODO:
Ladislav Michla637a112005-09-01 15:07:34 +000021 * - remove "mark pages reserved-hacks" from memory allocation code
Nick Piggin3c18ddd2008-04-28 02:12:10 -070022 * and implement fault()
Ralf Baechled203a7e2005-09-06 15:19:37 -070023 * - check decimation, calculating and reporting image size when
24 * using decimation
Ladislav Michla637a112005-09-01 15:07:34 +000025 * - implement read(), user mode buffers and overlay (?)
Ralf Baechled203a7e2005-09-06 15:19:37 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000031#include <linux/dma-mapping.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070032#include <linux/errno.h>
33#include <linux/fs.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000034#include <linux/interrupt.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070035#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070037#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000038#include <linux/time.h>
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
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030063#define VINO_MODULE_VERSION "0.0.7"
Ralf Baechled203a7e2005-09-06 15:19:37 -070064
65MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
66MODULE_VERSION(VINO_MODULE_VERSION);
67MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
68MODULE_LICENSE("GPL");
69
Ralf Baechled203a7e2005-09-06 15:19:37 -070070#ifdef VINO_DEBUG
71#define dprintk(x...) printk("VINO: " x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
Ralf Baechled203a7e2005-09-06 15:19:37 -070073#define dprintk(x...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
Ralf Baechled203a7e2005-09-06 15:19:37 -070076#define VINO_NO_CHANNEL 0
77#define VINO_CHANNEL_A 1
78#define VINO_CHANNEL_B 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Ralf Baechled203a7e2005-09-06 15:19:37 -070080#define VINO_PAL_WIDTH 768
81#define VINO_PAL_HEIGHT 576
82#define VINO_NTSC_WIDTH 640
83#define VINO_NTSC_HEIGHT 480
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Ralf Baechled203a7e2005-09-06 15:19:37 -070085#define VINO_MIN_WIDTH 32
86#define VINO_MIN_HEIGHT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Ralf Baechled203a7e2005-09-06 15:19:37 -070088#define VINO_CLIPPING_START_ODD_D1 1
Ladislav Michla637a112005-09-01 15:07:34 +000089#define VINO_CLIPPING_START_ODD_PAL 15
90#define VINO_CLIPPING_START_ODD_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070091
92#define VINO_CLIPPING_START_EVEN_D1 2
Ladislav Michla637a112005-09-01 15:07:34 +000093#define VINO_CLIPPING_START_EVEN_PAL 15
94#define VINO_CLIPPING_START_EVEN_NTSC 12
Ralf Baechled203a7e2005-09-06 15:19:37 -070095
96#define VINO_INPUT_CHANNEL_COUNT 3
97
Ladislav Michla637a112005-09-01 15:07:34 +000098/* the number is the index for vino_inputs */
Ralf Baechled203a7e2005-09-06 15:19:37 -070099#define VINO_INPUT_NONE -1
100#define VINO_INPUT_COMPOSITE 0
101#define VINO_INPUT_SVIDEO 1
102#define VINO_INPUT_D1 2
103
104#define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
105
Ladislav Michla637a112005-09-01 15:07:34 +0000106#define VINO_FIFO_THRESHOLD_DEFAULT 16
Ralf Baechled203a7e2005-09-06 15:19:37 -0700107
Ralf Baechled203a7e2005-09-06 15:19:37 -0700108#define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
109 * VINO_PAL_HEIGHT * 4 \
110 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
111
Ladislav Michla637a112005-09-01 15:07:34 +0000112#define VINO_FRAMEBUFFER_COUNT_MAX 8
Ralf Baechled203a7e2005-09-06 15:19:37 -0700113
114#define VINO_FRAMEBUFFER_UNUSED 0
115#define VINO_FRAMEBUFFER_IN_USE 1
116#define VINO_FRAMEBUFFER_READY 2
117
118#define VINO_QUEUE_ERROR -1
119#define VINO_QUEUE_MAGIC 0x20050125
120
121#define VINO_MEMORY_NONE 0
122#define VINO_MEMORY_MMAP 1
123#define VINO_MEMORY_USERPTR 2
124
125#define VINO_DUMMY_DESC_COUNT 4
126#define VINO_DESC_FETCH_DELAY 5 /* microseconds */
127
Ladislav Michla637a112005-09-01 15:07:34 +0000128#define VINO_MAX_FRAME_SKIP_COUNT 128
129
Ralf Baechled203a7e2005-09-06 15:19:37 -0700130/* the number is the index for vino_data_formats */
131#define VINO_DATA_FMT_NONE -1
132#define VINO_DATA_FMT_GREY 0
133#define VINO_DATA_FMT_RGB332 1
134#define VINO_DATA_FMT_RGB32 2
135#define VINO_DATA_FMT_YUV 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700136
137#define VINO_DATA_FMT_COUNT 4
138
Ladislav Michla637a112005-09-01 15:07:34 +0000139/* the number is the index for vino_data_norms */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700140#define VINO_DATA_NORM_NONE -1
141#define VINO_DATA_NORM_NTSC 0
142#define VINO_DATA_NORM_PAL 1
143#define VINO_DATA_NORM_SECAM 2
144#define VINO_DATA_NORM_D1 3
Ralf Baechled203a7e2005-09-06 15:19:37 -0700145
146#define VINO_DATA_NORM_COUNT 4
147
Jean Delvarecea0c682009-03-06 12:05:43 -0300148/* I2C controller flags */
149#define SGI_I2C_FORCE_IDLE (0 << 0)
150#define SGI_I2C_NOT_IDLE (1 << 0)
151#define SGI_I2C_WRITE (0 << 1)
152#define SGI_I2C_READ (1 << 1)
153#define SGI_I2C_RELEASE_BUS (0 << 2)
154#define SGI_I2C_HOLD_BUS (1 << 2)
155#define SGI_I2C_XFER_DONE (0 << 4)
156#define SGI_I2C_XFER_BUSY (1 << 4)
157#define SGI_I2C_ACK (0 << 5)
158#define SGI_I2C_NACK (1 << 5)
159#define SGI_I2C_BUS_OK (0 << 7)
160#define SGI_I2C_BUS_ERR (1 << 7)
161
Ralf Baechled203a7e2005-09-06 15:19:37 -0700162/* Internal data structure definitions */
163
164struct vino_input {
165 char *name;
166 v4l2_std_id std;
167};
168
169struct vino_clipping {
170 unsigned int left, right, top, bottom;
171};
172
173struct vino_data_format {
174 /* the description */
175 char *description;
176 /* bytes per pixel */
177 unsigned int bpp;
178 /* V4L2 fourcc code */
179 __u32 pixelformat;
180 /* V4L2 colorspace (duh!) */
181 enum v4l2_colorspace colorspace;
182};
183
184struct vino_data_norm {
185 char *description;
186 unsigned int width, height;
187 struct vino_clipping odd;
188 struct vino_clipping even;
189
190 v4l2_std_id std;
191 unsigned int fps_min, fps_max;
192 __u32 framelines;
193};
194
195struct vino_descriptor_table {
196 /* the number of PAGE_SIZE sized pages in the buffer */
197 unsigned int page_count;
198 /* virtual (kmalloc'd) pointers to the actual data
199 * (in PAGE_SIZE chunks, used with mmap streaming) */
200 unsigned long *virtual;
201
202 /* cpu address for the VINO descriptor table
203 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
204 unsigned long *dma_cpu;
205 /* dma address for the VINO descriptor table
206 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
207 dma_addr_t dma;
208};
209
210struct vino_framebuffer {
211 /* identifier nubmer */
212 unsigned int id;
213 /* the length of the whole buffer */
214 unsigned int size;
215 /* the length of actual data in buffer */
216 unsigned int data_size;
217 /* the data format */
218 unsigned int data_format;
219 /* the state of buffer data */
220 unsigned int state;
221 /* is the buffer mapped in user space? */
222 unsigned int map_count;
223 /* memory offset for mmap() */
224 unsigned int offset;
225 /* frame counter */
226 unsigned int frame_counter;
227 /* timestamp (written when image capture finishes) */
228 struct timeval timestamp;
229
230 struct vino_descriptor_table desc_table;
231
232 spinlock_t state_lock;
233};
234
235struct vino_framebuffer_fifo {
236 unsigned int length;
237
238 unsigned int used;
239 unsigned int head;
240 unsigned int tail;
241
Ladislav Michla637a112005-09-01 15:07:34 +0000242 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700243};
244
245struct vino_framebuffer_queue {
246 unsigned int magic;
247
248 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
249 unsigned int type;
250 unsigned int length;
251
252 /* data field of in and out contain index numbers for buffer */
253 struct vino_framebuffer_fifo in;
254 struct vino_framebuffer_fifo out;
255
Ladislav Michla637a112005-09-01 15:07:34 +0000256 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700257
258 spinlock_t queue_lock;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200259 struct mutex queue_mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700260 wait_queue_head_t frame_wait_queue;
261};
262
Ladislav Michla637a112005-09-01 15:07:34 +0000263struct vino_interrupt_data {
264 struct timeval timestamp;
265 unsigned int frame_counter;
266 unsigned int skip_count;
267 unsigned int skip;
268};
269
Ralf Baechled203a7e2005-09-06 15:19:37 -0700270struct vino_channel_settings {
271 unsigned int channel;
272
273 int input;
274 unsigned int data_format;
275 unsigned int data_norm;
276 struct vino_clipping clipping;
277 unsigned int decimation;
278 unsigned int line_size;
279 unsigned int alpha;
280 unsigned int fps;
281 unsigned int framert_reg;
282
283 unsigned int fifo_threshold;
284
285 struct vino_framebuffer_queue fb_queue;
286
287 /* number of the current field */
288 unsigned int field;
289
290 /* read in progress */
291 int reading;
292 /* streaming is active */
293 int streaming;
294 /* the driver is currently processing the queue */
295 int capturing;
296
Ingo Molnar3593cab2006-02-07 06:49:14 -0200297 struct mutex mutex;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700298 spinlock_t capture_lock;
299
300 unsigned int users;
301
Ladislav Michla637a112005-09-01 15:07:34 +0000302 struct vino_interrupt_data int_data;
303
Ralf Baechled203a7e2005-09-06 15:19:37 -0700304 /* V4L support */
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300305 struct video_device *vdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Ralf Baechled203a7e2005-09-06 15:19:37 -0700308struct vino_settings {
Hans Verkuil28959632009-02-18 18:53:47 -0300309 struct v4l2_device v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700310 struct vino_channel_settings a;
311 struct vino_channel_settings b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300313 /* the channel which owns this client:
314 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
315 unsigned int decoder_owner;
316 struct v4l2_subdev *decoder;
317 unsigned int camera_owner;
318 struct v4l2_subdev *camera;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ralf Baechled203a7e2005-09-06 15:19:37 -0700320 /* a lock for vino register access */
321 spinlock_t vino_lock;
322 /* a lock for channel input changes */
323 spinlock_t input_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned long dummy_page;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700326 struct vino_descriptor_table dummy_desc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
Ralf Baechled203a7e2005-09-06 15:19:37 -0700329/* Module parameters */
330
331/*
Ladislav Michla637a112005-09-01 15:07:34 +0000332 * Using vino_pixel_conversion the ABGR32-format pixels supplied
Ralf Baechled203a7e2005-09-06 15:19:37 -0700333 * by the VINO chip can be converted to more common formats
334 * like RGBA32 (or probably RGB24 in the future). This way we
335 * can give out data that can be specified correctly with
336 * the V4L2-definitions.
337 *
338 * The pixel format is specified as RGBA32 when no conversion
339 * is used.
340 *
341 * Note that this only affects the 32-bit bit depth.
342 *
343 * Use non-zero value to enable conversion.
344 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300345static int vino_pixel_conversion;
Ladislav Michla637a112005-09-01 15:07:34 +0000346
Ralf Baechled203a7e2005-09-06 15:19:37 -0700347module_param_named(pixelconv, vino_pixel_conversion, int, 0);
Ladislav Michla637a112005-09-01 15:07:34 +0000348
Ralf Baechled203a7e2005-09-06 15:19:37 -0700349MODULE_PARM_DESC(pixelconv,
350 "enable pixel conversion (non-zero value enables)");
351
352/* Internal data structures */
353
354static struct sgi_vino *vino;
355
356static struct vino_settings *vino_drvdata;
357
Hans Verkuil48f4dac2009-02-18 19:18:26 -0300358#define camera_call(o, f, args...) \
359 v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
360#define decoder_call(o, f, args...) \
361 v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
362
Ralf Baechled203a7e2005-09-06 15:19:37 -0700363static const char *vino_driver_name = "vino";
364static const char *vino_driver_description = "SGI VINO";
365static const char *vino_bus_name = "GIO64 bus";
Hans Verkuil0ef4dba2009-02-12 11:31:13 -0300366static const char *vino_vdev_name_a = "SGI VINO Channel A";
367static const char *vino_vdev_name_b = "SGI VINO Channel B";
Ralf Baechled203a7e2005-09-06 15:19:37 -0700368
Ladislav Michla637a112005-09-01 15:07:34 +0000369static void vino_capture_tasklet(unsigned long channel);
370
371DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
372DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
373
Ralf Baechled203a7e2005-09-06 15:19:37 -0700374static const struct vino_input vino_inputs[] = {
375 {
376 .name = "Composite",
Ladislav Michla637a112005-09-01 15:07:34 +0000377 .std = V4L2_STD_NTSC | V4L2_STD_PAL
378 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300379 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700380 .name = "S-Video",
Ladislav Michla637a112005-09-01 15:07:34 +0000381 .std = V4L2_STD_NTSC | V4L2_STD_PAL
382 | V4L2_STD_SECAM,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300383 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000384 .name = "D1/IndyCam",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700385 .std = V4L2_STD_NTSC,
386 }
387};
388
389static const struct vino_data_format vino_data_formats[] = {
390 {
391 .description = "8-bit greyscale",
392 .bpp = 1,
393 .pixelformat = V4L2_PIX_FMT_GREY,
394 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300395 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700396 .description = "8-bit dithered RGB 3-3-2",
397 .bpp = 1,
398 .pixelformat = V4L2_PIX_FMT_RGB332,
399 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300400 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700401 .description = "32-bit RGB",
402 .bpp = 4,
403 .pixelformat = V4L2_PIX_FMT_RGB32,
404 .colorspace = V4L2_COLORSPACE_SRGB,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300405 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700406 .description = "YUV 4:2:2",
Ladislav Michla637a112005-09-01 15:07:34 +0000407 .bpp = 2,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700408 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
409 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Ladislav Michla637a112005-09-01 15:07:34 +0000410 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700411};
412
413static const struct vino_data_norm vino_data_norms[] = {
414 {
415 .description = "NTSC",
416 .std = V4L2_STD_NTSC,
417 .fps_min = 6,
418 .fps_max = 30,
419 .framelines = 525,
420 .width = VINO_NTSC_WIDTH,
421 .height = VINO_NTSC_HEIGHT,
422 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000423 .top = VINO_CLIPPING_START_ODD_NTSC,
424 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700425 .bottom = VINO_CLIPPING_START_ODD_NTSC
426 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000427 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700428 },
429 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000430 .top = VINO_CLIPPING_START_EVEN_NTSC,
431 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700432 .bottom = VINO_CLIPPING_START_EVEN_NTSC
433 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000434 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700435 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300436 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700437 .description = "PAL",
438 .std = V4L2_STD_PAL,
439 .fps_min = 5,
440 .fps_max = 25,
441 .framelines = 625,
442 .width = VINO_PAL_WIDTH,
443 .height = VINO_PAL_HEIGHT,
444 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000445 .top = VINO_CLIPPING_START_ODD_PAL,
446 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700447 .bottom = VINO_CLIPPING_START_ODD_PAL
448 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000449 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700450 },
451 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000452 .top = VINO_CLIPPING_START_EVEN_PAL,
453 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700454 .bottom = VINO_CLIPPING_START_EVEN_PAL
455 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000456 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700457 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300458 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700459 .description = "SECAM",
460 .std = V4L2_STD_SECAM,
461 .fps_min = 5,
462 .fps_max = 25,
463 .framelines = 625,
464 .width = VINO_PAL_WIDTH,
465 .height = VINO_PAL_HEIGHT,
466 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000467 .top = VINO_CLIPPING_START_ODD_PAL,
468 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700469 .bottom = VINO_CLIPPING_START_ODD_PAL
470 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000471 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700472 },
473 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000474 .top = VINO_CLIPPING_START_EVEN_PAL,
475 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700476 .bottom = VINO_CLIPPING_START_EVEN_PAL
477 + VINO_PAL_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000478 .right = VINO_PAL_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700479 },
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300480 }, {
Ladislav Michla637a112005-09-01 15:07:34 +0000481 .description = "NTSC/D1",
Ralf Baechled203a7e2005-09-06 15:19:37 -0700482 .std = V4L2_STD_NTSC,
483 .fps_min = 6,
484 .fps_max = 30,
485 .framelines = 525,
486 .width = VINO_NTSC_WIDTH,
487 .height = VINO_NTSC_HEIGHT,
488 .odd = {
Ladislav Michla637a112005-09-01 15:07:34 +0000489 .top = VINO_CLIPPING_START_ODD_D1,
490 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700491 .bottom = VINO_CLIPPING_START_ODD_D1
492 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000493 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700494 },
495 .even = {
Ladislav Michla637a112005-09-01 15:07:34 +0000496 .top = VINO_CLIPPING_START_EVEN_D1,
497 .left = 0,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700498 .bottom = VINO_CLIPPING_START_EVEN_D1
499 + VINO_NTSC_HEIGHT / 2 - 1,
Ladislav Michla637a112005-09-01 15:07:34 +0000500 .right = VINO_NTSC_WIDTH,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700501 },
502 }
503};
504
505#define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
506
507struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
508 {
509 .id = V4L2_CID_AUTOGAIN,
510 .type = V4L2_CTRL_TYPE_BOOLEAN,
511 .name = "Automatic Gain Control",
512 .minimum = 0,
513 .maximum = 1,
514 .step = 1,
515 .default_value = INDYCAM_AGC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300516 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700517 .id = V4L2_CID_AUTO_WHITE_BALANCE,
518 .type = V4L2_CTRL_TYPE_BOOLEAN,
519 .name = "Automatic White Balance",
520 .minimum = 0,
521 .maximum = 1,
522 .step = 1,
523 .default_value = INDYCAM_AWB_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300524 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700525 .id = V4L2_CID_GAIN,
526 .type = V4L2_CTRL_TYPE_INTEGER,
527 .name = "Gain",
528 .minimum = INDYCAM_GAIN_MIN,
529 .maximum = INDYCAM_GAIN_MAX,
530 .step = 1,
531 .default_value = INDYCAM_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300532 }, {
533 .id = INDYCAM_CONTROL_RED_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700534 .type = V4L2_CTRL_TYPE_INTEGER,
535 .name = "Red Saturation",
536 .minimum = INDYCAM_RED_SATURATION_MIN,
537 .maximum = INDYCAM_RED_SATURATION_MAX,
538 .step = 1,
539 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300540 }, {
541 .id = INDYCAM_CONTROL_BLUE_SATURATION,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700542 .type = V4L2_CTRL_TYPE_INTEGER,
543 .name = "Blue Saturation",
544 .minimum = INDYCAM_BLUE_SATURATION_MIN,
545 .maximum = INDYCAM_BLUE_SATURATION_MAX,
546 .step = 1,
547 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300548 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700549 .id = V4L2_CID_RED_BALANCE,
550 .type = V4L2_CTRL_TYPE_INTEGER,
551 .name = "Red Balance",
552 .minimum = INDYCAM_RED_BALANCE_MIN,
553 .maximum = INDYCAM_RED_BALANCE_MAX,
554 .step = 1,
555 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300556 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700557 .id = V4L2_CID_BLUE_BALANCE,
558 .type = V4L2_CTRL_TYPE_INTEGER,
559 .name = "Blue Balance",
560 .minimum = INDYCAM_BLUE_BALANCE_MIN,
561 .maximum = INDYCAM_BLUE_BALANCE_MAX,
562 .step = 1,
563 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300564 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700565 .id = V4L2_CID_EXPOSURE,
566 .type = V4L2_CTRL_TYPE_INTEGER,
567 .name = "Shutter Control",
568 .minimum = INDYCAM_SHUTTER_MIN,
569 .maximum = INDYCAM_SHUTTER_MAX,
570 .step = 1,
571 .default_value = INDYCAM_SHUTTER_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300572 }, {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700573 .id = V4L2_CID_GAMMA,
574 .type = V4L2_CTRL_TYPE_INTEGER,
575 .name = "Gamma",
576 .minimum = INDYCAM_GAMMA_MIN,
577 .maximum = INDYCAM_GAMMA_MAX,
578 .step = 1,
579 .default_value = INDYCAM_GAMMA_DEFAULT,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700580 }
581};
582
Ladislav Michla637a112005-09-01 15:07:34 +0000583#define VINO_SAA7191_V4L2_CONTROL_COUNT 9
Ralf Baechled203a7e2005-09-06 15:19:37 -0700584
585struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
586 {
587 .id = V4L2_CID_HUE,
588 .type = V4L2_CTRL_TYPE_INTEGER,
589 .name = "Hue",
590 .minimum = SAA7191_HUE_MIN,
591 .maximum = SAA7191_HUE_MAX,
592 .step = 1,
593 .default_value = SAA7191_HUE_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300594 }, {
595 .id = SAA7191_CONTROL_BANDPASS,
Ladislav Michla637a112005-09-01 15:07:34 +0000596 .type = V4L2_CTRL_TYPE_INTEGER,
597 .name = "Luminance Bandpass",
598 .minimum = SAA7191_BANDPASS_MIN,
599 .maximum = SAA7191_BANDPASS_MAX,
600 .step = 1,
601 .default_value = SAA7191_BANDPASS_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300602 }, {
603 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
Ladislav Michla637a112005-09-01 15:07:34 +0000604 .type = V4L2_CTRL_TYPE_INTEGER,
605 .name = "Luminance Bandpass Weight",
606 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
607 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
608 .step = 1,
609 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300610 }, {
611 .id = SAA7191_CONTROL_CORING,
Ladislav Michla637a112005-09-01 15:07:34 +0000612 .type = V4L2_CTRL_TYPE_INTEGER,
613 .name = "HF Luminance Coring",
614 .minimum = SAA7191_CORING_MIN,
615 .maximum = SAA7191_CORING_MAX,
616 .step = 1,
617 .default_value = SAA7191_CORING_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300618 }, {
619 .id = SAA7191_CONTROL_FORCE_COLOUR,
Ladislav Michla637a112005-09-01 15:07:34 +0000620 .type = V4L2_CTRL_TYPE_BOOLEAN,
621 .name = "Force Colour",
622 .minimum = SAA7191_FORCE_COLOUR_MIN,
623 .maximum = SAA7191_FORCE_COLOUR_MAX,
624 .step = 1,
625 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300626 }, {
627 .id = SAA7191_CONTROL_CHROMA_GAIN,
Ladislav Michla637a112005-09-01 15:07:34 +0000628 .type = V4L2_CTRL_TYPE_INTEGER,
629 .name = "Chrominance Gain Control",
630 .minimum = SAA7191_CHROMA_GAIN_MIN,
631 .maximum = SAA7191_CHROMA_GAIN_MAX,
632 .step = 1,
633 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300634 }, {
635 .id = SAA7191_CONTROL_VTRC,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700636 .type = V4L2_CTRL_TYPE_BOOLEAN,
637 .name = "VTR Time Constant",
638 .minimum = SAA7191_VTRC_MIN,
639 .maximum = SAA7191_VTRC_MAX,
640 .step = 1,
641 .default_value = SAA7191_VTRC_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300642 }, {
643 .id = SAA7191_CONTROL_LUMA_DELAY,
Ladislav Michla637a112005-09-01 15:07:34 +0000644 .type = V4L2_CTRL_TYPE_INTEGER,
645 .name = "Luminance Delay Compensation",
646 .minimum = SAA7191_LUMA_DELAY_MIN,
647 .maximum = SAA7191_LUMA_DELAY_MAX,
648 .step = 1,
649 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300650 }, {
651 .id = SAA7191_CONTROL_VNR,
Ladislav Michla637a112005-09-01 15:07:34 +0000652 .type = V4L2_CTRL_TYPE_INTEGER,
653 .name = "Vertical Noise Reduction",
654 .minimum = SAA7191_VNR_MIN,
655 .maximum = SAA7191_VNR_MAX,
656 .step = 1,
657 .default_value = SAA7191_VNR_DEFAULT,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700658 }
659};
660
Ralf Baechled203a7e2005-09-06 15:19:37 -0700661/* VINO framebuffer/DMA descriptor management */
662
663static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
664 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Ralf Baechled203a7e2005-09-06 15:19:37 -0700666 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ralf Baechled203a7e2005-09-06 15:19:37 -0700668 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Ralf Baechled203a7e2005-09-06 15:19:37 -0700670 for (i = 0; i < count; i++) {
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300671 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700672 dma_unmap_single(NULL,
673 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
674 PAGE_SIZE, DMA_FROM_DEVICE);
675 free_page(fb->desc_table.virtual[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Ralf Baechled203a7e2005-09-06 15:19:37 -0700678 dma_free_coherent(NULL,
679 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
680 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
681 fb->desc_table.dma);
682 kfree(fb->desc_table.virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Ralf Baechled203a7e2005-09-06 15:19:37 -0700684 memset(fb, 0, sizeof(struct vino_framebuffer));
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Ralf Baechled203a7e2005-09-06 15:19:37 -0700687static void vino_free_buffer(struct vino_framebuffer *fb)
688{
689 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
690}
691
692static int vino_allocate_buffer(struct vino_framebuffer *fb,
693 unsigned int size)
694{
695 unsigned int count, i, j;
696 int ret = 0;
697
698 dprintk("vino_allocate_buffer():\n");
699
700 if (size < 1)
701 return -EINVAL;
702
703 memset(fb, 0, sizeof(struct vino_framebuffer));
704
705 count = ((size / PAGE_SIZE) + 4) & ~3;
706
707 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
708 size, count);
709
710 /* allocate memory for table with virtual (page) addresses */
Thomas Meyer82c80f82011-11-17 18:43:40 -0300711 fb->desc_table.virtual =
Ralf Baechled203a7e2005-09-06 15:19:37 -0700712 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
713 if (!fb->desc_table.virtual)
714 return -ENOMEM;
715
716 /* allocate memory for table with dma addresses
717 * (has space for four extra descriptors) */
718 fb->desc_table.dma_cpu =
719 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
720 sizeof(dma_addr_t), &fb->desc_table.dma,
721 GFP_KERNEL | GFP_DMA);
722 if (!fb->desc_table.dma_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700724 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Ralf Baechled203a7e2005-09-06 15:19:37 -0700727 /* allocate pages for the buffer and acquire the according
728 * dma addresses */
729 for (i = 0; i < count; i++) {
730 dma_addr_t dma_data_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Ralf Baechled203a7e2005-09-06 15:19:37 -0700732 fb->desc_table.virtual[i] =
733 get_zeroed_page(GFP_KERNEL | GFP_DMA);
734 if (!fb->desc_table.virtual[i]) {
735 ret = -ENOBUFS;
736 break;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Ralf Baechled203a7e2005-09-06 15:19:37 -0700739 dma_data_addr =
740 dma_map_single(NULL,
741 (void *)fb->desc_table.virtual[i],
742 PAGE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ralf Baechled203a7e2005-09-06 15:19:37 -0700744 for (j = 0; j < VINO_PAGE_RATIO; j++) {
745 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
746 dma_data_addr + VINO_PAGE_SIZE * j;
747 }
748
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300749 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Ralf Baechled203a7e2005-09-06 15:19:37 -0700752 /* page_count needs to be set anyway, because the descriptor table has
753 * been allocated according to this number */
754 fb->desc_table.page_count = count;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ret) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700757 /* the descriptor with index i doesn't contain
758 * a valid address yet */
759 vino_free_buffer_with_count(fb, i);
760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Ralf Baechled203a7e2005-09-06 15:19:37 -0700763 //fb->size = size;
764 fb->size = count * PAGE_SIZE;
765 fb->data_format = VINO_DATA_FMT_NONE;
766
767 /* set the dma stop-bit for the last (count+1)th descriptor */
768 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
769 return 0;
770
771 out_free_virtual:
772 kfree(fb->desc_table.virtual);
773 return ret;
774}
775
776#if 0
777/* user buffers not fully implemented yet */
778static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
779 void *user,
780 unsigned int size)
781{
782 unsigned int count, i, j;
783 int ret = 0;
784
785 dprintk("vino_prepare_user_buffer():\n");
786
787 if (size < 1)
788 return -EINVAL;
789
790 memset(fb, 0, sizeof(struct vino_framebuffer));
791
792 count = ((size / PAGE_SIZE)) & ~3;
793
794 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
795 size, count);
796
797 /* allocate memory for table with virtual (page) addresses */
798 fb->desc_table.virtual = (unsigned long *)
799 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
800 if (!fb->desc_table.virtual)
801 return -ENOMEM;
802
803 /* allocate memory for table with dma addresses
804 * (has space for four extra descriptors) */
805 fb->desc_table.dma_cpu =
806 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
807 sizeof(dma_addr_t), &fb->desc_table.dma,
808 GFP_KERNEL | GFP_DMA);
809 if (!fb->desc_table.dma_cpu) {
810 ret = -ENOMEM;
811 goto out_free_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700813
814 /* allocate pages for the buffer and acquire the according
815 * dma addresses */
816 for (i = 0; i < count; i++) {
817 dma_addr_t dma_data_addr;
818
819 fb->desc_table.virtual[i] =
820 get_zeroed_page(GFP_KERNEL | GFP_DMA);
821 if (!fb->desc_table.virtual[i]) {
822 ret = -ENOBUFS;
823 break;
824 }
825
826 dma_data_addr =
827 dma_map_single(NULL,
828 (void *)fb->desc_table.virtual[i],
829 PAGE_SIZE, DMA_FROM_DEVICE);
830
831 for (j = 0; j < VINO_PAGE_RATIO; j++) {
832 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
833 dma_data_addr + VINO_PAGE_SIZE * j;
834 }
835
Hans Verkuileda9e4e2008-08-22 17:12:08 -0300836 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
Ralf Baechled203a7e2005-09-06 15:19:37 -0700837 }
838
839 /* page_count needs to be set anyway, because the descriptor table has
840 * been allocated according to this number */
841 fb->desc_table.page_count = count;
842
843 if (ret) {
844 /* the descriptor with index i doesn't contain
845 * a valid address yet */
846 vino_free_buffer_with_count(fb, i);
847 return ret;
848 }
849
850 //fb->size = size;
851 fb->size = count * PAGE_SIZE;
852
853 /* set the dma stop-bit for the last (count+1)th descriptor */
854 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
855 return 0;
856
857 out_free_virtual:
858 kfree(fb->desc_table.virtual);
859 return ret;
860}
861#endif
862
863static void vino_sync_buffer(struct vino_framebuffer *fb)
864{
865 int i;
866
867 dprintk("vino_sync_buffer():\n");
868
869 for (i = 0; i < fb->desc_table.page_count; i++)
FUJITA Tomonori028bbfe2009-05-27 22:10:43 -0300870 dma_sync_single_for_cpu(NULL,
871 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
872 PAGE_SIZE, DMA_FROM_DEVICE);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700873}
874
875/* Framebuffer fifo functions (need to be locked externally) */
876
Ladislav Michla637a112005-09-01 15:07:34 +0000877static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700878 unsigned int length)
879{
880 f->length = 0;
881 f->used = 0;
882 f->head = 0;
883 f->tail = 0;
884
Ladislav Michla637a112005-09-01 15:07:34 +0000885 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
886 length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700887
888 f->length = length;
889}
890
891/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000892static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
893 unsigned int id)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700894{
895 unsigned int i;
Ladislav Michla637a112005-09-01 15:07:34 +0000896
Ralf Baechled203a7e2005-09-06 15:19:37 -0700897 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
898 if (f->data[i] == id)
899 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Ladislav Michla637a112005-09-01 15:07:34 +0000905#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -0700906/* returns true/false */
Ladislav Michla637a112005-09-01 15:07:34 +0000907static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700908{
909 return (f->used == f->length);
910}
Ladislav Michla637a112005-09-01 15:07:34 +0000911#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -0700912
Ladislav Michla637a112005-09-01 15:07:34 +0000913static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700914{
915 return f->used;
916}
917
918static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
919{
920 if (id >= f->length) {
921 return VINO_QUEUE_ERROR;
922 }
923
924 if (vino_fifo_has_id(f, id)) {
925 return VINO_QUEUE_ERROR;
926 }
927
928 if (f->used < f->length) {
929 f->data[f->tail] = id;
930 f->tail = (f->tail + 1) % f->length;
931 f->used++;
932 } else {
933 return VINO_QUEUE_ERROR;
934 }
935
936 return 0;
937}
938
939static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
940{
941 if (f->used > 0) {
942 *id = f->data[f->head];
943 } else {
944 return VINO_QUEUE_ERROR;
945 }
946
947 return 0;
948}
949
950static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
951{
952 if (f->used > 0) {
953 *id = f->data[f->head];
954 f->head = (f->head + 1) % f->length;
955 f->used--;
956 } else {
957 return VINO_QUEUE_ERROR;
958 }
959
960 return 0;
961}
962
963/* Framebuffer queue functions */
964
965/* execute with queue_lock locked */
966static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
967 unsigned int length)
968{
969 unsigned int i;
970
971 q->length = 0;
972 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
973 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
974 for (i = 0; i < length; i++) {
975 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
976 i);
977 vino_free_buffer(q->buffer[i]);
978 kfree(q->buffer[i]);
979 }
980
981 q->type = VINO_MEMORY_NONE;
982 q->magic = 0;
983}
984
985static void vino_queue_free(struct vino_framebuffer_queue *q)
986{
987 dprintk("vino_queue_free():\n");
988
989 if (q->magic != VINO_QUEUE_MAGIC)
990 return;
991 if (q->type != VINO_MEMORY_MMAP)
992 return;
993
Ingo Molnar3593cab2006-02-07 06:49:14 -0200994 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700995
996 vino_queue_free_with_count(q, q->length);
997
Ingo Molnar3593cab2006-02-07 06:49:14 -0200998 mutex_unlock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700999}
1000
1001static int vino_queue_init(struct vino_framebuffer_queue *q,
1002 unsigned int *length)
1003{
1004 unsigned int i;
1005 int ret = 0;
1006
1007 dprintk("vino_queue_init(): length = %d\n", *length);
1008
1009 if (q->magic == VINO_QUEUE_MAGIC) {
1010 dprintk("vino_queue_init(): queue already initialized!\n");
1011 return -EINVAL;
1012 }
1013
1014 if (q->type != VINO_MEMORY_NONE) {
1015 dprintk("vino_queue_init(): queue already initialized!\n");
1016 return -EINVAL;
1017 }
1018
1019 if (*length < 1)
1020 return -EINVAL;
1021
Ingo Molnar3593cab2006-02-07 06:49:14 -02001022 mutex_lock(&q->queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001023
Ladislav Michla637a112005-09-01 15:07:34 +00001024 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1025 *length = VINO_FRAMEBUFFER_COUNT_MAX;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001026
1027 q->length = 0;
1028
1029 for (i = 0; i < *length; i++) {
1030 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1031 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1032 GFP_KERNEL);
1033 if (!q->buffer[i]) {
1034 dprintk("vino_queue_init(): kmalloc() failed\n");
1035 ret = -ENOMEM;
1036 break;
1037 }
1038
1039 ret = vino_allocate_buffer(q->buffer[i],
1040 VINO_FRAMEBUFFER_SIZE);
1041 if (ret) {
1042 kfree(q->buffer[i]);
1043 dprintk("vino_queue_init(): "
1044 "vino_allocate_buffer() failed\n");
1045 break;
1046 }
1047
1048 q->buffer[i]->id = i;
1049 if (i > 0) {
1050 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1051 q->buffer[i - 1]->size;
1052 } else {
1053 q->buffer[i]->offset = 0;
1054 }
1055
1056 spin_lock_init(&q->buffer[i]->state_lock);
1057
1058 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1059 "size = %d\n", i, q->buffer[i]->offset,
1060 q->buffer[i]->size);
1061 }
1062
1063 if (ret) {
1064 vino_queue_free_with_count(q, i);
1065 *length = 0;
1066 } else {
1067 q->length = *length;
1068 vino_fifo_init(&q->in, q->length);
1069 vino_fifo_init(&q->out, q->length);
1070 q->type = VINO_MEMORY_MMAP;
1071 q->magic = VINO_QUEUE_MAGIC;
1072 }
1073
Ingo Molnar3593cab2006-02-07 06:49:14 -02001074 mutex_unlock(&q->queue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return ret;
1077}
1078
Ralf Baechled203a7e2005-09-06 15:19:37 -07001079static struct vino_framebuffer *vino_queue_add(struct
1080 vino_framebuffer_queue *q,
1081 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Ralf Baechled203a7e2005-09-06 15:19:37 -07001083 struct vino_framebuffer *ret = NULL;
1084 unsigned int total;
1085 unsigned long flags;
1086
1087 dprintk("vino_queue_add(): id = %d\n", id);
1088
1089 if (q->magic != VINO_QUEUE_MAGIC) {
1090 return ret;
1091 }
1092
1093 spin_lock_irqsave(&q->queue_lock, flags);
1094
1095 if (q->length == 0)
1096 goto out;
1097
1098 if (id >= q->length)
1099 goto out;
1100
1101 /* not needed?: if (vino_fifo_full(&q->out)) {
1102 goto out;
1103 }*/
1104 /* check that outgoing queue isn't already full
1105 * (or that it won't become full) */
1106 total = vino_fifo_get_used(&q->in) +
1107 vino_fifo_get_used(&q->out);
1108 if (total >= q->length)
1109 goto out;
1110
1111 if (vino_fifo_enqueue(&q->in, id))
1112 goto out;
1113
1114 ret = q->buffer[id];
1115
1116out:
1117 spin_unlock_irqrestore(&q->queue_lock, flags);
1118
1119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Ralf Baechled203a7e2005-09-06 15:19:37 -07001122static struct vino_framebuffer *vino_queue_transfer(struct
1123 vino_framebuffer_queue *q)
1124{
1125 struct vino_framebuffer *ret = NULL;
1126 struct vino_framebuffer *fb;
1127 int id;
1128 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Ralf Baechled203a7e2005-09-06 15:19:37 -07001130 dprintk("vino_queue_transfer():\n");
1131
1132 if (q->magic != VINO_QUEUE_MAGIC) {
1133 return ret;
1134 }
1135
1136 spin_lock_irqsave(&q->queue_lock, flags);
1137
1138 if (q->length == 0)
1139 goto out;
1140
1141 // now this actually removes an entry from the incoming queue
1142 if (vino_fifo_dequeue(&q->in, &id)) {
1143 goto out;
1144 }
1145
1146 dprintk("vino_queue_transfer(): id = %d\n", id);
1147 fb = q->buffer[id];
1148
1149 // we have already checked that the outgoing queue is not full, but...
1150 if (vino_fifo_enqueue(&q->out, id)) {
1151 printk(KERN_ERR "vino_queue_transfer(): "
1152 "outgoing queue is full, this shouldn't happen!\n");
1153 goto out;
1154 }
1155
1156 ret = fb;
1157out:
1158 spin_unlock_irqrestore(&q->queue_lock, flags);
1159
1160 return ret;
1161}
1162
1163/* returns true/false */
1164static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1165 unsigned int id)
1166{
1167 int ret = 0;
1168 unsigned long flags;
1169
1170 if (q->magic != VINO_QUEUE_MAGIC) {
1171 return ret;
1172 }
1173
1174 spin_lock_irqsave(&q->queue_lock, flags);
1175
1176 if (q->length == 0)
1177 goto out;
1178
1179 ret = vino_fifo_has_id(&q->in, id);
1180
1181out:
1182 spin_unlock_irqrestore(&q->queue_lock, flags);
1183
1184 return ret;
1185}
1186
1187/* returns true/false */
1188static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1189 unsigned int id)
1190{
1191 int ret = 0;
1192 unsigned long flags;
1193
1194 if (q->magic != VINO_QUEUE_MAGIC) {
1195 return ret;
1196 }
1197
1198 spin_lock_irqsave(&q->queue_lock, flags);
1199
1200 if (q->length == 0)
1201 goto out;
1202
1203 ret = vino_fifo_has_id(&q->out, id);
1204
1205out:
1206 spin_unlock_irqrestore(&q->queue_lock, flags);
1207
1208 return ret;
1209}
1210
1211static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1212 unsigned int *used)
1213{
1214 int ret = 0;
1215 unsigned long flags;
1216
1217 if (q->magic != VINO_QUEUE_MAGIC) {
1218 return VINO_QUEUE_ERROR;
1219 }
1220
1221 spin_lock_irqsave(&q->queue_lock, flags);
1222
1223 if (q->length == 0) {
1224 ret = VINO_QUEUE_ERROR;
1225 goto out;
1226 }
1227
1228 *used = vino_fifo_get_used(&q->in);
1229
1230out:
1231 spin_unlock_irqrestore(&q->queue_lock, flags);
1232
1233 return ret;
1234}
1235
1236static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1237 unsigned int *used)
1238{
1239 int ret = 0;
1240 unsigned long flags;
1241
1242 if (q->magic != VINO_QUEUE_MAGIC) {
1243 return VINO_QUEUE_ERROR;
1244 }
1245
1246 spin_lock_irqsave(&q->queue_lock, flags);
1247
1248 if (q->length == 0) {
1249 ret = VINO_QUEUE_ERROR;
1250 goto out;
1251 }
1252
1253 *used = vino_fifo_get_used(&q->out);
1254
1255out:
1256 spin_unlock_irqrestore(&q->queue_lock, flags);
1257
1258 return ret;
1259}
1260
Ladislav Michla637a112005-09-01 15:07:34 +00001261#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07001262static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1263 unsigned int *total)
1264{
1265 int ret = 0;
1266 unsigned long flags;
1267
1268 if (q->magic != VINO_QUEUE_MAGIC) {
1269 return VINO_QUEUE_ERROR;
1270 }
1271
1272 spin_lock_irqsave(&q->queue_lock, flags);
1273
1274 if (q->length == 0) {
1275 ret = VINO_QUEUE_ERROR;
1276 goto out;
1277 }
1278
1279 *total = vino_fifo_get_used(&q->in) +
1280 vino_fifo_get_used(&q->out);
1281
1282out:
1283 spin_unlock_irqrestore(&q->queue_lock, flags);
1284
1285 return ret;
1286}
Ladislav Michla637a112005-09-01 15:07:34 +00001287#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07001288
1289static struct vino_framebuffer *vino_queue_peek(struct
1290 vino_framebuffer_queue *q,
1291 unsigned int *id)
1292{
1293 struct vino_framebuffer *ret = NULL;
1294 unsigned long flags;
1295
1296 if (q->magic != VINO_QUEUE_MAGIC) {
1297 return ret;
1298 }
1299
1300 spin_lock_irqsave(&q->queue_lock, flags);
1301
1302 if (q->length == 0)
1303 goto out;
1304
1305 if (vino_fifo_peek(&q->in, id)) {
1306 goto out;
1307 }
1308
1309 ret = q->buffer[*id];
1310out:
1311 spin_unlock_irqrestore(&q->queue_lock, flags);
1312
1313 return ret;
1314}
1315
1316static struct vino_framebuffer *vino_queue_remove(struct
1317 vino_framebuffer_queue *q,
1318 unsigned int *id)
1319{
1320 struct vino_framebuffer *ret = NULL;
1321 unsigned long flags;
1322 dprintk("vino_queue_remove():\n");
1323
1324 if (q->magic != VINO_QUEUE_MAGIC) {
1325 return ret;
1326 }
1327
1328 spin_lock_irqsave(&q->queue_lock, flags);
1329
1330 if (q->length == 0)
1331 goto out;
1332
1333 if (vino_fifo_dequeue(&q->out, id)) {
1334 goto out;
1335 }
1336
1337 dprintk("vino_queue_remove(): id = %d\n", *id);
1338 ret = q->buffer[*id];
1339out:
1340 spin_unlock_irqrestore(&q->queue_lock, flags);
1341
1342 return ret;
1343}
1344
1345static struct
1346vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1347 unsigned int id)
1348{
1349 struct vino_framebuffer *ret = NULL;
1350 unsigned long flags;
1351
1352 if (q->magic != VINO_QUEUE_MAGIC) {
1353 return ret;
1354 }
1355
1356 spin_lock_irqsave(&q->queue_lock, flags);
1357
1358 if (q->length == 0)
1359 goto out;
1360
1361 if (id >= q->length)
1362 goto out;
1363
1364 ret = q->buffer[id];
1365 out:
1366 spin_unlock_irqrestore(&q->queue_lock, flags);
1367
1368 return ret;
1369}
1370
1371static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1372{
1373 unsigned int length = 0;
1374 unsigned long flags;
1375
1376 if (q->magic != VINO_QUEUE_MAGIC) {
1377 return length;
1378 }
1379
1380 spin_lock_irqsave(&q->queue_lock, flags);
1381 length = q->length;
1382 spin_unlock_irqrestore(&q->queue_lock, flags);
1383
1384 return length;
1385}
1386
1387static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1388{
1389 unsigned int i;
1390 int ret = 0;
1391 unsigned long flags;
1392
1393 if (q->magic != VINO_QUEUE_MAGIC) {
1394 return ret;
1395 }
1396
1397 spin_lock_irqsave(&q->queue_lock, flags);
1398 for (i = 0; i < q->length; i++) {
1399 if (q->buffer[i]->map_count > 0) {
1400 ret = 1;
1401 break;
1402 }
1403 }
1404 spin_unlock_irqrestore(&q->queue_lock, flags);
1405
1406 return ret;
1407}
1408
1409/* VINO functions */
1410
1411/* execute with input_lock locked */
1412static void vino_update_line_size(struct vino_channel_settings *vcs)
1413{
1414 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1415 unsigned int d = vcs->decimation;
1416 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001417 unsigned int lsize;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001418
1419 dprintk("update_line_size(): before: w = %d, d = %d, "
1420 "line_size = %d\n", w, d, vcs->line_size);
Ladislav Michla637a112005-09-01 15:07:34 +00001421
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001422 /* line size must be multiple of 8 bytes */
Ralf Baechled203a7e2005-09-06 15:19:37 -07001423 lsize = (bpp * (w / d)) & ~7;
1424 w = (lsize / bpp) * d;
1425
1426 vcs->clipping.right = vcs->clipping.left + w;
1427 vcs->line_size = lsize;
Ladislav Michla637a112005-09-01 15:07:34 +00001428
Ralf Baechled203a7e2005-09-06 15:19:37 -07001429 dprintk("update_line_size(): after: w = %d, d = %d, "
1430 "line_size = %d\n", w, d, vcs->line_size);
1431}
1432
1433/* execute with input_lock locked */
1434static void vino_set_clipping(struct vino_channel_settings *vcs,
1435 unsigned int x, unsigned int y,
1436 unsigned int w, unsigned int h)
1437{
1438 unsigned int maxwidth, maxheight;
1439 unsigned int d;
1440
1441 maxwidth = vino_data_norms[vcs->data_norm].width;
1442 maxheight = vino_data_norms[vcs->data_norm].height;
1443 d = vcs->decimation;
1444
1445 y &= ~1; /* odd/even fields */
1446
1447 if (x > maxwidth) {
1448 x = 0;
1449 }
1450 if (y > maxheight) {
1451 y = 0;
1452 }
1453
1454 if (((w / d) < VINO_MIN_WIDTH)
1455 || ((h / d) < VINO_MIN_HEIGHT)) {
1456 w = VINO_MIN_WIDTH * d;
1457 h = VINO_MIN_HEIGHT * d;
1458 }
1459
1460 if ((x + w) > maxwidth) {
1461 w = maxwidth - x;
1462 if ((w / d) < VINO_MIN_WIDTH)
1463 x = maxwidth - VINO_MIN_WIDTH * d;
1464 }
1465 if ((y + h) > maxheight) {
1466 h = maxheight - y;
1467 if ((h / d) < VINO_MIN_HEIGHT)
1468 y = maxheight - VINO_MIN_HEIGHT * d;
1469 }
1470
1471 vcs->clipping.left = x;
1472 vcs->clipping.top = y;
1473 vcs->clipping.right = x + w;
1474 vcs->clipping.bottom = y + h;
1475
1476 vino_update_line_size(vcs);
1477
1478 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1479 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1480 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1481}
1482
1483/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001484static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001485{
1486 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1487 vino_data_norms[vcs->data_norm].height);
1488}
1489
1490/* execute with input_lock locked */
1491static void vino_set_scaling(struct vino_channel_settings *vcs,
1492 unsigned int w, unsigned int h)
1493{
1494 unsigned int x, y, curw, curh, d;
1495
1496 x = vcs->clipping.left;
1497 y = vcs->clipping.top;
1498 curw = vcs->clipping.right - vcs->clipping.left;
1499 curh = vcs->clipping.bottom - vcs->clipping.top;
1500
1501 d = max(curw / w, curh / h);
1502
1503 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1504 w, h, curw, curh, d);
1505
1506 if (d < 1) {
1507 d = 1;
Ladislav Michla637a112005-09-01 15:07:34 +00001508 } else if (d > 8) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07001509 d = 8;
1510 }
1511
1512 vcs->decimation = d;
1513 vino_set_clipping(vcs, x, y, w * d, h * d);
1514
1515 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1516 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1517 vcs->decimation, vcs->line_size);
1518}
1519
1520/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001521static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001522{
1523 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1524 vcs->clipping.bottom - vcs->clipping.top);
1525}
1526
1527/* execute with input_lock locked */
1528static void vino_set_framerate(struct vino_channel_settings *vcs,
1529 unsigned int fps)
1530{
1531 unsigned int mask;
1532
1533 switch (vcs->data_norm) {
1534 case VINO_DATA_NORM_NTSC:
1535 case VINO_DATA_NORM_D1:
1536 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1537
1538 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1539 fps = vino_data_norms[vcs->data_norm].fps_min;
1540 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1541 fps = vino_data_norms[vcs->data_norm].fps_max;
1542
1543 switch (fps) {
1544 case 6:
1545 mask = 0x003;
1546 break;
1547 case 12:
1548 mask = 0x0c3;
1549 break;
1550 case 18:
1551 mask = 0x333;
1552 break;
1553 case 24:
1554 mask = 0x3ff;
1555 break;
1556 case 30:
1557 mask = 0xfff;
1558 break;
1559 default:
1560 mask = VINO_FRAMERT_FULL;
1561 }
1562 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1563 break;
1564 case VINO_DATA_NORM_PAL:
1565 case VINO_DATA_NORM_SECAM:
1566 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1567
1568 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1569 fps = vino_data_norms[vcs->data_norm].fps_min;
1570 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1571 fps = vino_data_norms[vcs->data_norm].fps_max;
1572
1573 switch (fps) {
1574 case 5:
1575 mask = 0x003;
1576 break;
1577 case 10:
1578 mask = 0x0c3;
1579 break;
1580 case 15:
1581 mask = 0x333;
1582 break;
1583 case 20:
1584 mask = 0x0ff;
1585 break;
1586 case 25:
1587 mask = 0x3ff;
1588 break;
1589 default:
1590 mask = VINO_FRAMERT_FULL;
1591 }
1592 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1593 break;
1594 }
1595
1596 vcs->fps = fps;
1597}
1598
1599/* execute with input_lock locked */
Ladislav Michla637a112005-09-01 15:07:34 +00001600static inline void vino_set_default_framerate(struct
1601 vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001602{
1603 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1604}
1605
Jean Delvarecea0c682009-03-06 12:05:43 -03001606/* VINO I2C bus functions */
1607
1608struct i2c_algo_sgi_data {
1609 void *data; /* private data for lowlevel routines */
1610 unsigned (*getctrl)(void *data);
1611 void (*setctrl)(void *data, unsigned val);
1612 unsigned (*rdata)(void *data);
1613 void (*wdata)(void *data, unsigned val);
1614
1615 int xfer_timeout;
1616 int ack_timeout;
1617};
1618
1619static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1620{
1621 int i;
1622
1623 for (i = 0; i < adap->xfer_timeout; i++) {
1624 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1625 return 0;
1626 udelay(1);
1627 }
1628
1629 return -ETIMEDOUT;
1630}
1631
1632static int wait_ack(struct i2c_algo_sgi_data *adap)
1633{
1634 int i;
1635
1636 if (wait_xfer_done(adap))
1637 return -ETIMEDOUT;
1638 for (i = 0; i < adap->ack_timeout; i++) {
1639 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1640 return 0;
1641 udelay(1);
1642 }
1643
1644 return -ETIMEDOUT;
1645}
1646
1647static int force_idle(struct i2c_algo_sgi_data *adap)
1648{
1649 int i;
1650
1651 adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1652 for (i = 0; i < adap->xfer_timeout; i++) {
1653 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1654 goto out;
1655 udelay(1);
1656 }
1657 return -ETIMEDOUT;
1658out:
1659 if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1660 return -EIO;
1661 return 0;
1662}
1663
1664static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1665 int rd)
1666{
1667 if (rd)
1668 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1669 /* Check if bus is idle, eventually force it to do so */
1670 if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1671 if (force_idle(adap))
1672 return -EIO;
1673 /* Write out the i2c chip address and specify operation */
1674 adap->setctrl(adap->data,
1675 SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1676 if (rd)
1677 addr |= 1;
1678 adap->wdata(adap->data, addr);
1679 if (wait_ack(adap))
1680 return -EIO;
1681 return 0;
1682}
1683
1684static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1685 unsigned int len)
1686{
1687 int i;
1688
1689 adap->setctrl(adap->data,
1690 SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1691 for (i = 0; i < len; i++) {
1692 if (wait_xfer_done(adap))
1693 return -EIO;
1694 buf[i] = adap->rdata(adap->data);
1695 }
1696 adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1697
1698 return 0;
1699
1700}
1701
1702static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1703 unsigned int len)
1704{
1705 int i;
1706
1707 /* We are already in write state */
1708 for (i = 0; i < len; i++) {
1709 adap->wdata(adap->data, buf[i]);
1710 if (wait_ack(adap))
1711 return -EIO;
1712 }
1713 return 0;
1714}
1715
1716static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1717 int num)
1718{
1719 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1720 struct i2c_msg *p;
1721 int i, err = 0;
1722
1723 for (i = 0; !err && i < num; i++) {
1724 p = &msgs[i];
1725 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1726 if (err || !p->len)
1727 continue;
1728 if (p->flags & I2C_M_RD)
1729 err = i2c_read(adap, p->buf, p->len);
1730 else
1731 err = i2c_write(adap, p->buf, p->len);
1732 }
1733
1734 return (err < 0) ? err : i;
1735}
1736
1737static u32 sgi_func(struct i2c_adapter *adap)
1738{
1739 return I2C_FUNC_SMBUS_EMUL;
1740}
1741
1742static const struct i2c_algorithm sgi_algo = {
1743 .master_xfer = sgi_xfer,
1744 .functionality = sgi_func,
1745};
1746
1747static unsigned i2c_vino_getctrl(void *data)
1748{
1749 return vino->i2c_control;
1750}
1751
1752static void i2c_vino_setctrl(void *data, unsigned val)
1753{
1754 vino->i2c_control = val;
1755}
1756
1757static unsigned i2c_vino_rdata(void *data)
1758{
1759 return vino->i2c_data;
1760}
1761
1762static void i2c_vino_wdata(void *data, unsigned val)
1763{
1764 vino->i2c_data = val;
1765}
1766
1767static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1768 .getctrl = &i2c_vino_getctrl,
1769 .setctrl = &i2c_vino_setctrl,
1770 .rdata = &i2c_vino_rdata,
1771 .wdata = &i2c_vino_wdata,
1772 .xfer_timeout = 200,
1773 .ack_timeout = 1000,
1774};
1775
1776static struct i2c_adapter vino_i2c_adapter = {
1777 .name = "VINO I2C bus",
Jean Delvarecea0c682009-03-06 12:05:43 -03001778 .algo = &sgi_algo,
1779 .algo_data = &i2c_sgi_vino_data,
1780 .owner = THIS_MODULE,
1781};
1782
Ralf Baechled203a7e2005-09-06 15:19:37 -07001783/*
1784 * Prepare VINO for DMA transfer...
1785 * (execute only with vino_lock and input_lock locked)
1786 */
1787static int vino_dma_setup(struct vino_channel_settings *vcs,
1788 struct vino_framebuffer *fb)
1789{
1790 u32 ctrl, intr;
1791 struct sgi_vino_channel *ch;
1792 const struct vino_data_norm *norm;
1793
1794 dprintk("vino_dma_setup():\n");
1795
1796 vcs->field = 0;
1797 fb->frame_counter = 0;
1798
1799 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1800 norm = &vino_data_norms[vcs->data_norm];
1801
1802 ch->page_index = 0;
1803 ch->line_count = 0;
1804
1805 /* VINO line size register is set 8 bytes less than actual */
1806 ch->line_size = vcs->line_size - 8;
1807
1808 /* let VINO know where to transfer data */
1809 ch->start_desc_tbl = fb->desc_table.dma;
1810 ch->next_4_desc = fb->desc_table.dma;
1811
1812 /* give vino time to fetch the first four descriptors, 5 usec
1813 * should be more than enough time */
1814 udelay(VINO_DESC_FETCH_DELAY);
1815
Ladislav Michla637a112005-09-01 15:07:34 +00001816 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1817 ch->start_desc_tbl, ch->next_4_desc);
1818
Ralf Baechled203a7e2005-09-06 15:19:37 -07001819 /* set the alpha register */
1820 ch->alpha = vcs->alpha;
1821
1822 /* set clipping registers */
1823 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1824 VINO_CLIP_EVEN(norm->even.top +
1825 vcs->clipping.top / 2) |
1826 VINO_CLIP_X(vcs->clipping.left);
1827 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1828 vcs->clipping.bottom / 2 - 1) |
1829 VINO_CLIP_EVEN(norm->even.top +
1830 vcs->clipping.bottom / 2 - 1) |
1831 VINO_CLIP_X(vcs->clipping.right);
Ralf Baechled203a7e2005-09-06 15:19:37 -07001832
1833 /* set the size of actual content in the buffer (DECIMATION !) */
1834 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1835 vcs->decimation) *
1836 ((vcs->clipping.bottom - vcs->clipping.top) /
1837 vcs->decimation) *
1838 vino_data_formats[vcs->data_format].bpp;
1839
1840 ch->frame_rate = vcs->framert_reg;
1841
1842 ctrl = vino->control;
1843 intr = vino->intr_status;
1844
1845 if (vcs->channel == VINO_CHANNEL_A) {
1846 /* All interrupt conditions for this channel was cleared
1847 * so clear the interrupt status register and enable
1848 * interrupts */
1849 intr &= ~VINO_INTSTAT_A;
1850 ctrl |= VINO_CTRL_A_INT;
1851
1852 /* enable synchronization */
1853 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1854
1855 /* enable frame assembly */
1856 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1857
1858 /* set decimation used */
1859 if (vcs->decimation < 2)
1860 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1861 else {
1862 ctrl |= VINO_CTRL_A_DEC_ENBL;
1863 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1864 ctrl |= (vcs->decimation - 1) <<
1865 VINO_CTRL_A_DEC_SCALE_SHIFT;
1866 }
1867
1868 /* select input interface */
1869 if (vcs->input == VINO_INPUT_D1)
1870 ctrl |= VINO_CTRL_A_SELECT;
1871 else
1872 ctrl &= ~VINO_CTRL_A_SELECT;
1873
1874 /* palette */
1875 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1876 VINO_CTRL_A_DITHER);
1877 } else {
1878 intr &= ~VINO_INTSTAT_B;
1879 ctrl |= VINO_CTRL_B_INT;
1880
1881 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1882 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1883
1884 if (vcs->decimation < 2)
1885 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1886 else {
1887 ctrl |= VINO_CTRL_B_DEC_ENBL;
1888 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1889 ctrl |= (vcs->decimation - 1) <<
1890 VINO_CTRL_B_DEC_SCALE_SHIFT;
1891
1892 }
1893 if (vcs->input == VINO_INPUT_D1)
1894 ctrl |= VINO_CTRL_B_SELECT;
1895 else
1896 ctrl &= ~VINO_CTRL_B_SELECT;
1897
1898 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1899 VINO_CTRL_B_DITHER);
1900 }
1901
1902 /* set palette */
1903 fb->data_format = vcs->data_format;
1904
1905 switch (vcs->data_format) {
1906 case VINO_DATA_FMT_GREY:
1907 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1908 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1909 break;
1910 case VINO_DATA_FMT_RGB32:
1911 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1912 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1913 break;
1914 case VINO_DATA_FMT_YUV:
1915 /* nothing needs to be done */
1916 break;
1917 case VINO_DATA_FMT_RGB332:
1918 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1919 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1920 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1921 break;
1922 }
1923
1924 vino->intr_status = intr;
1925 vino->control = ctrl;
1926
1927 return 0;
1928}
1929
1930/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001931static inline void vino_dma_start(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001932{
1933 u32 ctrl = vino->control;
1934
1935 dprintk("vino_dma_start():\n");
1936 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1937 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1938 vino->control = ctrl;
1939}
1940
1941/* (execute only with vino_lock locked) */
Ladislav Michla637a112005-09-01 15:07:34 +00001942static inline void vino_dma_stop(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07001943{
1944 u32 ctrl = vino->control;
1945
1946 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1947 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
Ladislav Michla637a112005-09-01 15:07:34 +00001948 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1949 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
Ralf Baechled203a7e2005-09-06 15:19:37 -07001950 vino->control = ctrl;
1951 dprintk("vino_dma_stop():\n");
1952}
1953
1954/*
1955 * Load dummy page to descriptor registers. This prevents generating of
1956 * spurious interrupts. (execute only with vino_lock locked)
1957 */
1958static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1959{
1960 struct sgi_vino_channel *ch;
1961
1962 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1963
1964 ch->page_index = 0;
1965 ch->line_count = 0;
1966
1967 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1968 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1969
1970 udelay(VINO_DESC_FETCH_DELAY);
1971 dprintk("channel %c clear interrupt condition\n",
1972 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1973}
1974
1975static int vino_capture(struct vino_channel_settings *vcs,
1976 struct vino_framebuffer *fb)
1977{
1978 int err = 0;
1979 unsigned long flags, flags2;
1980
1981 spin_lock_irqsave(&fb->state_lock, flags);
1982
1983 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1984 err = -EBUSY;
1985 fb->state = VINO_FRAMEBUFFER_IN_USE;
1986
1987 spin_unlock_irqrestore(&fb->state_lock, flags);
1988
1989 if (err)
1990 return err;
1991
1992 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1993 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1994
1995 vino_dma_setup(vcs, fb);
1996 vino_dma_start(vcs);
1997
1998 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1999 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2000
2001 return err;
2002}
2003
2004static
2005struct vino_framebuffer *vino_capture_enqueue(struct
2006 vino_channel_settings *vcs,
2007 unsigned int index)
2008{
2009 struct vino_framebuffer *fb;
2010 unsigned long flags;
2011
2012 dprintk("vino_capture_enqueue():\n");
2013
2014 spin_lock_irqsave(&vcs->capture_lock, flags);
2015
2016 fb = vino_queue_add(&vcs->fb_queue, index);
2017 if (fb == NULL) {
2018 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
2019 "queue full?\n");
2020 goto out;
2021 }
2022out:
2023 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2024
2025 return fb;
2026}
2027
2028static int vino_capture_next(struct vino_channel_settings *vcs, int start)
2029{
2030 struct vino_framebuffer *fb;
2031 unsigned int incoming, id;
2032 int err = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002033 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002034
2035 dprintk("vino_capture_next():\n");
2036
2037 spin_lock_irqsave(&vcs->capture_lock, flags);
2038
2039 if (start) {
2040 /* start capture only if capture isn't in progress already */
2041 if (vcs->capturing) {
2042 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2043 return 0;
2044 }
2045
2046 } else {
2047 /* capture next frame:
2048 * stop capture if capturing is not set */
2049 if (!vcs->capturing) {
2050 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2051 return 0;
2052 }
2053 }
2054
2055 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2056 if (err) {
2057 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2058 "failed\n");
2059 err = -EINVAL;
2060 goto out;
2061 }
2062 if (incoming == 0) {
2063 dprintk("vino_capture_next(): no buffers available\n");
2064 goto out;
2065 }
2066
2067 fb = vino_queue_peek(&vcs->fb_queue, &id);
2068 if (fb == NULL) {
2069 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2070 err = -EINVAL;
2071 goto out;
2072 }
2073
Ralf Baechled203a7e2005-09-06 15:19:37 -07002074 if (start) {
2075 vcs->capturing = 1;
2076 }
2077
2078 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2079
2080 err = vino_capture(vcs, fb);
2081
2082 return err;
2083
2084out:
2085 vcs->capturing = 0;
2086 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2087
2088 return err;
2089}
2090
Ladislav Michla637a112005-09-01 15:07:34 +00002091static inline int vino_is_capturing(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002092{
2093 int ret;
2094 unsigned long flags;
2095
2096 spin_lock_irqsave(&vcs->capture_lock, flags);
2097
2098 ret = vcs->capturing;
2099
2100 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2101
2102 return ret;
2103}
2104
2105/* waits until a frame is captured */
2106static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2107{
2108 wait_queue_t wait;
2109 int err = 0;
2110
2111 dprintk("vino_wait_for_frame():\n");
2112
2113 init_waitqueue_entry(&wait, current);
2114 /* add ourselves into wait queue */
2115 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002116
2117 /* to ensure that schedule_timeout will return immediately
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -03002118 * if VINO interrupt was triggered meanwhile */
Mauro Carvalho Chehab818ca472007-07-17 16:29:07 -03002119 schedule_timeout_interruptible(msecs_to_jiffies(100));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002120
2121 if (signal_pending(current))
2122 err = -EINTR;
2123
2124 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2125
2126 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2127 err ? "failed" : "ok");
2128
2129 return err;
2130}
2131
2132/* the function assumes that PAGE_SIZE % 4 == 0 */
2133static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2134 unsigned char *pageptr;
2135 unsigned int page, i;
2136 unsigned char a;
2137
2138 for (page = 0; page < fb->desc_table.page_count; page++) {
2139 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2140
2141 for (i = 0; i < PAGE_SIZE; i += 4) {
2142 a = pageptr[0];
2143 pageptr[0] = pageptr[3];
2144 pageptr[1] = pageptr[2];
2145 pageptr[2] = pageptr[1];
2146 pageptr[3] = a;
2147 pageptr += 4;
2148 }
2149 }
2150}
2151
2152/* checks if the buffer is in correct state and syncs data */
2153static int vino_check_buffer(struct vino_channel_settings *vcs,
2154 struct vino_framebuffer *fb)
2155{
2156 int err = 0;
2157 unsigned long flags;
2158
2159 dprintk("vino_check_buffer():\n");
2160
2161 spin_lock_irqsave(&fb->state_lock, flags);
2162 switch (fb->state) {
2163 case VINO_FRAMEBUFFER_IN_USE:
2164 err = -EIO;
2165 break;
2166 case VINO_FRAMEBUFFER_READY:
2167 vino_sync_buffer(fb);
2168 fb->state = VINO_FRAMEBUFFER_UNUSED;
2169 break;
2170 default:
2171 err = -EINVAL;
2172 }
2173 spin_unlock_irqrestore(&fb->state_lock, flags);
2174
2175 if (!err) {
2176 if (vino_pixel_conversion
2177 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2178 vino_convert_to_rgba(fb);
2179 }
2180 } else if (err && (err != -EINVAL)) {
2181 dprintk("vino_check_buffer(): buffer not ready\n");
2182
2183 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2184 vino_dma_stop(vcs);
2185 vino_clear_interrupt(vcs);
2186 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2187 }
2188
2189 return err;
2190}
2191
2192/* forcefully terminates capture */
2193static void vino_capture_stop(struct vino_channel_settings *vcs)
2194{
2195 unsigned int incoming = 0, outgoing = 0, id;
2196 unsigned long flags, flags2;
2197
2198 dprintk("vino_capture_stop():\n");
2199
2200 spin_lock_irqsave(&vcs->capture_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00002201
Ralf Baechled203a7e2005-09-06 15:19:37 -07002202 /* unset capturing to stop queue processing */
2203 vcs->capturing = 0;
2204
2205 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2206
2207 vino_dma_stop(vcs);
2208 vino_clear_interrupt(vcs);
2209
2210 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2211
2212 /* remove all items from the queue */
2213 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2214 dprintk("vino_capture_stop(): "
2215 "vino_queue_get_incoming() failed\n");
2216 goto out;
2217 }
2218 while (incoming > 0) {
2219 vino_queue_transfer(&vcs->fb_queue);
2220
2221 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2222 dprintk("vino_capture_stop(): "
2223 "vino_queue_get_incoming() failed\n");
2224 goto out;
2225 }
2226 }
2227
2228 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2229 dprintk("vino_capture_stop(): "
2230 "vino_queue_get_outgoing() failed\n");
2231 goto out;
2232 }
2233 while (outgoing > 0) {
2234 vino_queue_remove(&vcs->fb_queue, &id);
2235
2236 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2237 dprintk("vino_capture_stop(): "
2238 "vino_queue_get_outgoing() failed\n");
2239 goto out;
2240 }
2241 }
2242
2243out:
2244 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2245}
2246
Ladislav Michla637a112005-09-01 15:07:34 +00002247#if 0
Ralf Baechled203a7e2005-09-06 15:19:37 -07002248static int vino_capture_failed(struct vino_channel_settings *vcs)
2249{
2250 struct vino_framebuffer *fb;
2251 unsigned long flags;
2252 unsigned int i;
2253 int ret;
2254
2255 dprintk("vino_capture_failed():\n");
2256
2257 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2258
2259 vino_dma_stop(vcs);
2260 vino_clear_interrupt(vcs);
2261
2262 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2263
2264 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2265 if (ret == VINO_QUEUE_ERROR) {
2266 dprintk("vino_queue_get_incoming() failed\n");
2267 return -EINVAL;
2268 }
2269 if (i == 0) {
2270 /* no buffers to process */
2271 return 0;
2272 }
2273
2274 fb = vino_queue_peek(&vcs->fb_queue, &i);
2275 if (fb == NULL) {
2276 dprintk("vino_queue_peek() failed\n");
2277 return -EINVAL;
2278 }
2279
2280 spin_lock_irqsave(&fb->state_lock, flags);
2281 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2282 fb->state = VINO_FRAMEBUFFER_UNUSED;
2283 vino_queue_transfer(&vcs->fb_queue);
2284 vino_queue_remove(&vcs->fb_queue, &i);
2285 /* we should actually discard the newest frame,
2286 * but who cares ... */
2287 }
2288 spin_unlock_irqrestore(&fb->state_lock, flags);
2289
2290 return 0;
2291}
Ladislav Michla637a112005-09-01 15:07:34 +00002292#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002293
Ladislav Michla637a112005-09-01 15:07:34 +00002294static void vino_skip_frame(struct vino_channel_settings *vcs)
2295{
2296 struct vino_framebuffer *fb;
2297 unsigned long flags;
2298 unsigned int id;
2299
2300 spin_lock_irqsave(&vcs->capture_lock, flags);
2301 fb = vino_queue_peek(&vcs->fb_queue, &id);
2302 if (!fb) {
2303 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2304 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2305 return;
2306 }
2307 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2308
2309 spin_lock_irqsave(&fb->state_lock, flags);
2310 fb->state = VINO_FRAMEBUFFER_UNUSED;
2311 spin_unlock_irqrestore(&fb->state_lock, flags);
2312
2313 vino_capture_next(vcs, 0);
2314}
2315
2316static void vino_frame_done(struct vino_channel_settings *vcs)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002317{
2318 struct vino_framebuffer *fb;
2319 unsigned long flags;
2320
2321 spin_lock_irqsave(&vcs->capture_lock, flags);
2322 fb = vino_queue_transfer(&vcs->fb_queue);
2323 if (!fb) {
2324 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2325 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2326 return;
2327 }
2328 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2329
Ladislav Michla637a112005-09-01 15:07:34 +00002330 fb->frame_counter = vcs->int_data.frame_counter;
2331 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2332 sizeof(struct timeval));
Ralf Baechled203a7e2005-09-06 15:19:37 -07002333
2334 spin_lock_irqsave(&fb->state_lock, flags);
2335 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2336 fb->state = VINO_FRAMEBUFFER_READY;
2337 spin_unlock_irqrestore(&fb->state_lock, flags);
2338
2339 wake_up(&vcs->fb_queue.frame_wait_queue);
2340
2341 vino_capture_next(vcs, 0);
2342}
2343
Ladislav Michla637a112005-09-01 15:07:34 +00002344static void vino_capture_tasklet(unsigned long channel) {
2345 struct vino_channel_settings *vcs;
2346
2347 vcs = (channel == VINO_CHANNEL_A)
2348 ? &vino_drvdata->a : &vino_drvdata->b;
2349
2350 if (vcs->int_data.skip)
2351 vcs->int_data.skip_count++;
2352
2353 if (vcs->int_data.skip && (vcs->int_data.skip_count
2354 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2355 vino_skip_frame(vcs);
2356 } else {
2357 vcs->int_data.skip_count = 0;
2358 vino_frame_done(vcs);
2359 }
2360}
2361
David Howells7d12e782006-10-05 14:55:46 +01002362static irqreturn_t vino_interrupt(int irq, void *dev_id)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002363{
Ladislav Michla637a112005-09-01 15:07:34 +00002364 u32 ctrl, intr;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002365 unsigned int fc_a, fc_b;
Ladislav Michla637a112005-09-01 15:07:34 +00002366 int handled_a = 0, skip_a = 0, done_a = 0;
2367 int handled_b = 0, skip_b = 0, done_b = 0;
2368
2369#ifdef VINO_DEBUG_INT
2370 int loop = 0;
2371 unsigned int line_count = vino->a.line_count,
2372 page_index = vino->a.page_index,
2373 field_counter = vino->a.field_counter,
2374 start_desc_tbl = vino->a.start_desc_tbl,
2375 next_4_desc = vino->a.next_4_desc;
2376 unsigned int line_count_2,
2377 page_index_2,
2378 field_counter_2,
2379 start_desc_tbl_2,
2380 next_4_desc_2;
2381#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002382
2383 spin_lock(&vino_drvdata->vino_lock);
2384
Ladislav Michla637a112005-09-01 15:07:34 +00002385 while ((intr = vino->intr_status)) {
2386 fc_a = vino->a.field_counter >> 1;
2387 fc_b = vino->b.field_counter >> 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002388
Ladislav Michla637a112005-09-01 15:07:34 +00002389 /* handle error-interrupts in some special way ?
2390 * --> skips frames */
2391 if (intr & VINO_INTSTAT_A) {
2392 if (intr & VINO_INTSTAT_A_EOF) {
2393 vino_drvdata->a.field++;
2394 if (vino_drvdata->a.field > 1) {
2395 vino_dma_stop(&vino_drvdata->a);
2396 vino_clear_interrupt(&vino_drvdata->a);
2397 vino_drvdata->a.field = 0;
2398 done_a = 1;
2399 } else {
2400 if (vino->a.page_index
2401 != vino_drvdata->a.line_size) {
2402 vino->a.line_count = 0;
2403 vino->a.page_index =
2404 vino_drvdata->
2405 a.line_size;
2406 vino->a.next_4_desc =
2407 vino->a.start_desc_tbl;
2408 }
2409 }
2410 dprintk("channel A end-of-field "
2411 "interrupt: %04x\n", intr);
2412 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002413 vino_dma_stop(&vino_drvdata->a);
2414 vino_clear_interrupt(&vino_drvdata->a);
2415 vino_drvdata->a.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002416 skip_a = 1;
2417 dprintk("channel A error interrupt: %04x\n",
2418 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002419 }
Ladislav Michla637a112005-09-01 15:07:34 +00002420
2421#ifdef VINO_DEBUG_INT
2422 line_count_2 = vino->a.line_count;
2423 page_index_2 = vino->a.page_index;
2424 field_counter_2 = vino->a.field_counter;
2425 start_desc_tbl_2 = vino->a.start_desc_tbl;
2426 next_4_desc_2 = vino->a.next_4_desc;
2427
2428 printk("intr = %04x, loop = %d, field = %d\n",
2429 intr, loop, vino_drvdata->a.field);
2430 printk("1- line count = %04d, page index = %04d, "
2431 "start = %08x, next = %08x\n"
2432 " fieldc = %d, framec = %d\n",
2433 line_count, page_index, start_desc_tbl,
2434 next_4_desc, field_counter, fc_a);
2435 printk("12-line count = %04d, page index = %04d, "
2436 " start = %08x, next = %08x\n",
2437 line_count_2, page_index_2, start_desc_tbl_2,
2438 next_4_desc_2);
2439
2440 if (done_a)
2441 printk("\n");
2442#endif
Ralf Baechled203a7e2005-09-06 15:19:37 -07002443 }
Ladislav Michla637a112005-09-01 15:07:34 +00002444
2445 if (intr & VINO_INTSTAT_B) {
2446 if (intr & VINO_INTSTAT_B_EOF) {
2447 vino_drvdata->b.field++;
2448 if (vino_drvdata->b.field > 1) {
2449 vino_dma_stop(&vino_drvdata->b);
2450 vino_clear_interrupt(&vino_drvdata->b);
2451 vino_drvdata->b.field = 0;
2452 done_b = 1;
2453 }
2454 dprintk("channel B end-of-field "
2455 "interrupt: %04x\n", intr);
2456 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002457 vino_dma_stop(&vino_drvdata->b);
2458 vino_clear_interrupt(&vino_drvdata->b);
2459 vino_drvdata->b.field = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00002460 skip_b = 1;
2461 dprintk("channel B error interrupt: %04x\n",
2462 intr);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002463 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002464 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002465
Ladislav Michla637a112005-09-01 15:07:34 +00002466 /* Always remember to clear interrupt status.
2467 * Disable VINO interrupts while we do this. */
2468 ctrl = vino->control;
2469 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2470 vino->intr_status = ~intr;
2471 vino->control = ctrl;
2472
2473 spin_unlock(&vino_drvdata->vino_lock);
2474
2475 if ((!handled_a) && (done_a || skip_a)) {
2476 if (!skip_a) {
Sakari Ailus8e6057b2012-09-15 15:14:42 -03002477 v4l2_get_timestamp(
2478 &vino_drvdata->a.int_data.timestamp);
Ladislav Michla637a112005-09-01 15:07:34 +00002479 vino_drvdata->a.int_data.frame_counter = fc_a;
2480 }
2481 vino_drvdata->a.int_data.skip = skip_a;
2482
2483 dprintk("channel A %s, interrupt: %d\n",
2484 skip_a ? "skipping frame" : "frame done",
2485 intr);
2486 tasklet_hi_schedule(&vino_tasklet_a);
2487 handled_a = 1;
2488 }
2489
2490 if ((!handled_b) && (done_b || skip_b)) {
2491 if (!skip_b) {
Sakari Ailus8e6057b2012-09-15 15:14:42 -03002492 v4l2_get_timestamp(
2493 &vino_drvdata->b.int_data.timestamp);
Ladislav Michla637a112005-09-01 15:07:34 +00002494 vino_drvdata->b.int_data.frame_counter = fc_b;
2495 }
2496 vino_drvdata->b.int_data.skip = skip_b;
2497
2498 dprintk("channel B %s, interrupt: %d\n",
2499 skip_b ? "skipping frame" : "frame done",
2500 intr);
2501 tasklet_hi_schedule(&vino_tasklet_b);
2502 handled_b = 1;
2503 }
2504
2505#ifdef VINO_DEBUG_INT
2506 loop++;
2507#endif
2508 spin_lock(&vino_drvdata->vino_lock);
2509 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002510
2511 spin_unlock(&vino_drvdata->vino_lock);
2512
Ralf Baechled203a7e2005-09-06 15:19:37 -07002513 return IRQ_HANDLED;
2514}
2515
2516/* VINO video input management */
2517
2518static int vino_get_saa7191_input(int input)
2519{
2520 switch (input) {
2521 case VINO_INPUT_COMPOSITE:
2522 return SAA7191_INPUT_COMPOSITE;
2523 case VINO_INPUT_SVIDEO:
2524 return SAA7191_INPUT_SVIDEO;
2525 default:
2526 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2527 "invalid input!\n");
2528 return -1;
2529 }
2530}
2531
Ralf Baechled203a7e2005-09-06 15:19:37 -07002532/* execute with input_lock locked */
2533static int vino_is_input_owner(struct vino_channel_settings *vcs)
2534{
2535 switch(vcs->input) {
2536 case VINO_INPUT_COMPOSITE:
2537 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002538 return vino_drvdata->decoder_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002539 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002540 return vino_drvdata->camera_owner == vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002541 default:
2542 return 0;
2543 }
2544}
2545
2546static int vino_acquire_input(struct vino_channel_settings *vcs)
2547{
Ladislav Michla637a112005-09-01 15:07:34 +00002548 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002549 int ret = 0;
2550
2551 dprintk("vino_acquire_input():\n");
2552
Ladislav Michla637a112005-09-01 15:07:34 +00002553 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002554
2555 /* First try D1 and then SAA7191 */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002556 if (vino_drvdata->camera
2557 && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2558 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002559 vcs->input = VINO_INPUT_D1;
2560 vcs->data_norm = VINO_DATA_NORM_D1;
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002561 } else if (vino_drvdata->decoder
2562 && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002563 int input;
Hans Verkuilf9ac50c2012-05-23 06:32:52 -03002564 int data_norm = 0;
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002565 v4l2_std_id norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002566
Ladislav Michla637a112005-09-01 15:07:34 +00002567 input = VINO_INPUT_COMPOSITE;
2568
Hans Verkuil5325b422009-04-02 11:26:22 -03002569 ret = decoder_call(video, s_routing,
2570 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002571 if (ret) {
2572 ret = -EINVAL;
2573 goto out;
2574 }
2575
2576 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2577
2578 /* Don't hold spinlocks while auto-detecting norm
2579 * as it may take a while... */
2580
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002581 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002582 if (!ret) {
2583 for (data_norm = 0; data_norm < 3; data_norm++) {
2584 if (vino_data_norms[data_norm].std & norm)
2585 break;
2586 }
2587 if (data_norm == 3)
2588 data_norm = VINO_DATA_NORM_PAL;
Laurent Pinchart8774bed2014-04-28 16:53:01 -03002589 ret = decoder_call(video, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002590 }
2591
2592 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2593
2594 if (ret) {
2595 ret = -EINVAL;
2596 goto out;
2597 }
2598
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002599 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002600
Ladislav Michla637a112005-09-01 15:07:34 +00002601 vcs->input = input;
2602 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002603 } else {
2604 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2605 vino_drvdata->b.input : vino_drvdata->a.input;
2606 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2607 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2608 }
2609
2610 if (vcs->input == VINO_INPUT_NONE) {
2611 ret = -ENODEV;
2612 goto out;
2613 }
2614
Ladislav Michla637a112005-09-01 15:07:34 +00002615 vino_set_default_clipping(vcs);
2616 vino_set_default_scaling(vcs);
2617 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002618
2619 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2620
2621out:
Ladislav Michla637a112005-09-01 15:07:34 +00002622 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002623
2624 return ret;
2625}
2626
2627static int vino_set_input(struct vino_channel_settings *vcs, int input)
2628{
2629 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2630 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002631 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002632 int ret = 0;
2633
2634 dprintk("vino_set_input():\n");
2635
Ladislav Michla637a112005-09-01 15:07:34 +00002636 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002637
2638 if (vcs->input == input)
2639 goto out;
2640
Ladislav Michla637a112005-09-01 15:07:34 +00002641 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002642 case VINO_INPUT_COMPOSITE:
2643 case VINO_INPUT_SVIDEO:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002644 if (!vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002645 ret = -EINVAL;
2646 goto out;
2647 }
2648
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002649 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2650 vino_drvdata->decoder_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002651 }
2652
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002653 if (vino_drvdata->decoder_owner == vcs->channel) {
Hans Verkuilf9ac50c2012-05-23 06:32:52 -03002654 int data_norm = 0;
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002655 v4l2_std_id norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002656
Hans Verkuil5325b422009-04-02 11:26:22 -03002657 ret = decoder_call(video, s_routing,
2658 vino_get_saa7191_input(input), 0, 0);
Ladislav Michla637a112005-09-01 15:07:34 +00002659 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002660 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002661 ret = -EINVAL;
2662 goto out;
2663 }
2664
2665 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2666
2667 /* Don't hold spinlocks while auto-detecting norm
2668 * as it may take a while... */
2669
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002670 ret = decoder_call(video, querystd, &norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002671 if (!ret) {
2672 for (data_norm = 0; data_norm < 3; data_norm++) {
2673 if (vino_data_norms[data_norm].std & norm)
2674 break;
2675 }
2676 if (data_norm == 3)
2677 data_norm = VINO_DATA_NORM_PAL;
Laurent Pinchart8774bed2014-04-28 16:53:01 -03002678 ret = decoder_call(video, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002679 }
2680
2681 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2682
2683 if (ret) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002684 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ladislav Michla637a112005-09-01 15:07:34 +00002685 ret = -EINVAL;
2686 goto out;
2687 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002688
2689 vcs->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +00002690 vcs->data_norm = data_norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002691 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00002692 if (input != vcs2->input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002693 ret = -EBUSY;
2694 goto out;
2695 }
2696
2697 vcs->input = input;
2698 vcs->data_norm = vcs2->data_norm;
2699 }
2700
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002701 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002702 /* Transfer the ownership or release the input */
2703 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002704 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002705 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002706 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002707 }
2708 }
2709 break;
2710 case VINO_INPUT_D1:
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002711 if (!vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002712 ret = -EINVAL;
2713 goto out;
2714 }
2715
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002716 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2717 vino_drvdata->camera_owner = vcs->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002718
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002719 if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002720 /* Transfer the ownership or release the input */
2721 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2722 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002723 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002724 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002725 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002726 }
2727 }
2728
2729 vcs->input = input;
2730 vcs->data_norm = VINO_DATA_NORM_D1;
2731 break;
2732 default:
2733 ret = -EINVAL;
2734 goto out;
2735 }
2736
2737 vino_set_default_clipping(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002738 vino_set_default_scaling(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002739 vino_set_default_framerate(vcs);
2740
2741 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2742
2743out:
Ladislav Michla637a112005-09-01 15:07:34 +00002744 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002745
2746 return ret;
2747}
2748
2749static void vino_release_input(struct vino_channel_settings *vcs)
2750{
2751 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2752 &vino_drvdata->b : &vino_drvdata->a;
Ladislav Michla637a112005-09-01 15:07:34 +00002753 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002754
2755 dprintk("vino_release_input():\n");
2756
Ladislav Michla637a112005-09-01 15:07:34 +00002757 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002758
2759 /* Release ownership of the channel
2760 * and if the other channel takes input from
2761 * the same source, transfer the ownership */
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002762 if (vino_drvdata->camera_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002763 if (vcs2->input == VINO_INPUT_D1) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002764 vino_drvdata->camera_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002765 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002766 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002767 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002768 } else if (vino_drvdata->decoder_owner == vcs->channel) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002769 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2770 (vcs2->input == VINO_INPUT_SVIDEO)) {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002771 vino_drvdata->decoder_owner = vcs2->channel;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002772 } else {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002773 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002774 }
2775 }
2776 vcs->input = VINO_INPUT_NONE;
2777
Ladislav Michla637a112005-09-01 15:07:34 +00002778 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002779}
2780
2781/* execute with input_lock locked */
2782static int vino_set_data_norm(struct vino_channel_settings *vcs,
Ladislav Michla637a112005-09-01 15:07:34 +00002783 unsigned int data_norm,
2784 unsigned long *flags)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002785{
Ladislav Michla637a112005-09-01 15:07:34 +00002786 int err = 0;
2787
2788 if (data_norm == vcs->data_norm)
2789 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002790
2791 switch (vcs->input) {
2792 case VINO_INPUT_D1:
2793 /* only one "norm" supported */
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002794 if (data_norm != VINO_DATA_NORM_D1)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002795 return -EINVAL;
2796 break;
2797 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00002798 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002799 v4l2_std_id norm;
2800
Ladislav Michla637a112005-09-01 15:07:34 +00002801 if ((data_norm != VINO_DATA_NORM_PAL)
2802 && (data_norm != VINO_DATA_NORM_NTSC)
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002803 && (data_norm != VINO_DATA_NORM_SECAM))
Ladislav Michla637a112005-09-01 15:07:34 +00002804 return -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002805
Ladislav Michla637a112005-09-01 15:07:34 +00002806 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002807
Ladislav Michla637a112005-09-01 15:07:34 +00002808 /* Don't hold spinlocks while setting norm
2809 * as it may take a while... */
2810
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002811 norm = vino_data_norms[data_norm].std;
Laurent Pinchart8774bed2014-04-28 16:53:01 -03002812 err = decoder_call(video, s_std, norm);
Ladislav Michla637a112005-09-01 15:07:34 +00002813
2814 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2815
2816 if (err)
2817 goto out;
2818
Ralf Baechled203a7e2005-09-06 15:19:37 -07002819 vcs->data_norm = data_norm;
Ladislav Michla637a112005-09-01 15:07:34 +00002820
2821 vino_set_default_clipping(vcs);
2822 vino_set_default_scaling(vcs);
2823 vino_set_default_framerate(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002824 break;
Ladislav Michla637a112005-09-01 15:07:34 +00002825 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07002826 default:
2827 return -EINVAL;
2828 }
2829
Ladislav Michla637a112005-09-01 15:07:34 +00002830out:
2831 return err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002832}
2833
2834/* V4L2 helper functions */
2835
2836static int vino_find_data_format(__u32 pixelformat)
2837{
2838 int i;
2839
2840 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2841 if (vino_data_formats[i].pixelformat == pixelformat)
2842 return i;
2843 }
2844
2845 return VINO_DATA_FMT_NONE;
2846}
2847
Hans Verkuilc49cb362009-02-12 10:32:50 -03002848static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002849{
2850 int input = VINO_INPUT_NONE;
Ladislav Michla637a112005-09-01 15:07:34 +00002851 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002852
Ladislav Michla637a112005-09-01 15:07:34 +00002853 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002854 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002855 switch (index) {
2856 case 0:
2857 input = VINO_INPUT_COMPOSITE;
2858 break;
2859 case 1:
2860 input = VINO_INPUT_SVIDEO;
2861 break;
2862 case 2:
2863 input = VINO_INPUT_D1;
2864 break;
2865 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002866 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002867 switch (index) {
2868 case 0:
2869 input = VINO_INPUT_COMPOSITE;
2870 break;
2871 case 1:
2872 input = VINO_INPUT_SVIDEO;
2873 break;
2874 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002875 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002876 switch (index) {
2877 case 0:
2878 input = VINO_INPUT_D1;
2879 break;
2880 }
2881 }
Ladislav Michla637a112005-09-01 15:07:34 +00002882 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002883
2884 return input;
2885}
2886
2887/* execute with input_lock locked */
2888static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2889{
2890 __u32 index = 0;
2891 // FIXME: detect when no inputs available
2892
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002893 if (vino_drvdata->decoder && vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002894 switch (vcs->input) {
2895 case VINO_INPUT_COMPOSITE:
2896 index = 0;
2897 break;
2898 case VINO_INPUT_SVIDEO:
2899 index = 1;
2900 break;
2901 case VINO_INPUT_D1:
2902 index = 2;
2903 break;
2904 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002905 } else if (vino_drvdata->decoder) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002906 switch (vcs->input) {
2907 case VINO_INPUT_COMPOSITE:
2908 index = 0;
2909 break;
2910 case VINO_INPUT_SVIDEO:
2911 index = 1;
2912 break;
2913 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002914 } else if (vino_drvdata->camera) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07002915 switch (vcs->input) {
2916 case VINO_INPUT_D1:
2917 index = 0;
2918 break;
2919 }
2920 }
2921
2922 return index;
2923}
2924
2925/* V4L2 ioctls */
2926
Hans Verkuilc49cb362009-02-12 10:32:50 -03002927static int vino_querycap(struct file *file, void *__fh,
2928 struct v4l2_capability *cap)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002929{
2930 memset(cap, 0, sizeof(struct v4l2_capability));
2931
2932 strcpy(cap->driver, vino_driver_name);
2933 strcpy(cap->card, vino_driver_description);
2934 strcpy(cap->bus_info, vino_bus_name);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002935 cap->capabilities =
2936 V4L2_CAP_VIDEO_CAPTURE |
2937 V4L2_CAP_STREAMING;
2938 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
Hans Verkuilc49cb362009-02-12 10:32:50 -03002939 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002940}
2941
Hans Verkuilc49cb362009-02-12 10:32:50 -03002942static int vino_enum_input(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07002943 struct v4l2_input *i)
2944{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002945 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002946 __u32 index = i->index;
2947 int input;
2948 dprintk("requested index = %d\n", index);
2949
Hans Verkuilc49cb362009-02-12 10:32:50 -03002950 input = vino_int_enum_input(vcs, index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002951 if (input == VINO_INPUT_NONE)
2952 return -EINVAL;
2953
Ralf Baechled203a7e2005-09-06 15:19:37 -07002954 i->type = V4L2_INPUT_TYPE_CAMERA;
2955 i->std = vino_inputs[input].std;
2956 strcpy(i->name, vino_inputs[input].name);
2957
Hans Verkuilcf4e9482009-02-27 09:05:10 -03002958 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
Hans Verkuil48f4dac2009-02-18 19:18:26 -03002959 decoder_call(video, g_input_status, &i->status);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002960 return 0;
2961}
2962
Hans Verkuilc49cb362009-02-12 10:32:50 -03002963static int vino_g_input(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00002964 unsigned int *i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002965{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002966 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002967 __u32 index;
2968 int input;
Ladislav Michla637a112005-09-01 15:07:34 +00002969 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002970
Ladislav Michla637a112005-09-01 15:07:34 +00002971 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002972 input = vcs->input;
2973 index = vino_find_input_index(vcs);
Ladislav Michla637a112005-09-01 15:07:34 +00002974 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002975
2976 dprintk("input = %d\n", input);
2977
2978 if (input == VINO_INPUT_NONE) {
2979 return -EINVAL;
2980 }
2981
Ladislav Michla637a112005-09-01 15:07:34 +00002982 *i = index;
Ralf Baechled203a7e2005-09-06 15:19:37 -07002983
2984 return 0;
2985}
2986
Hans Verkuilc49cb362009-02-12 10:32:50 -03002987static int vino_s_input(struct file *file, void *__fh,
2988 unsigned int i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07002989{
Hans Verkuilc49cb362009-02-12 10:32:50 -03002990 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002991 int input;
Hans Verkuilc49cb362009-02-12 10:32:50 -03002992 dprintk("requested input = %d\n", i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002993
Hans Verkuilc49cb362009-02-12 10:32:50 -03002994 input = vino_int_enum_input(vcs, i);
Ralf Baechled203a7e2005-09-06 15:19:37 -07002995 if (input == VINO_INPUT_NONE)
2996 return -EINVAL;
2997
2998 return vino_set_input(vcs, input);
2999}
3000
Hans Verkuilc49cb362009-02-12 10:32:50 -03003001static int vino_querystd(struct file *file, void *__fh,
Ladislav Michla637a112005-09-01 15:07:34 +00003002 v4l2_std_id *std)
3003{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003004 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003005 unsigned long flags;
3006 int err = 0;
3007
3008 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3009
3010 switch (vcs->input) {
3011 case VINO_INPUT_D1:
3012 *std = vino_inputs[vcs->input].std;
3013 break;
3014 case VINO_INPUT_COMPOSITE:
3015 case VINO_INPUT_SVIDEO: {
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003016 decoder_call(video, querystd, std);
Ladislav Michla637a112005-09-01 15:07:34 +00003017 break;
3018 }
3019 default:
3020 err = -EINVAL;
3021 }
3022
3023 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3024
3025 return err;
3026}
3027
Hans Verkuilc49cb362009-02-12 10:32:50 -03003028static int vino_g_std(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003029 v4l2_std_id *std)
3030{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003031 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003032 unsigned long flags;
3033
3034 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3035
Ralf Baechled203a7e2005-09-06 15:19:37 -07003036 *std = vino_data_norms[vcs->data_norm].std;
Ladislav Michla637a112005-09-01 15:07:34 +00003037 dprintk("current standard = %d\n", vcs->data_norm);
3038
3039 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003040
3041 return 0;
3042}
3043
Hans Verkuilc49cb362009-02-12 10:32:50 -03003044static int vino_s_std(struct file *file, void *__fh,
Hans Verkuil314527a2013-03-15 06:10:40 -03003045 v4l2_std_id std)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003046{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003047 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003048 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003049 int ret = 0;
3050
Ladislav Michla637a112005-09-01 15:07:34 +00003051 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3052
3053 if (!vino_is_input_owner(vcs)) {
3054 ret = -EBUSY;
3055 goto out;
3056 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003057
3058 /* check if the standard is valid for the current input */
Hans Verkuil314527a2013-03-15 06:10:40 -03003059 if (std & vino_inputs[vcs->input].std) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003060 dprintk("standard accepted\n");
3061
3062 /* change the video norm for SAA7191
3063 * and accept NTSC for D1 (do nothing) */
3064
3065 if (vcs->input == VINO_INPUT_D1)
3066 goto out;
3067
Hans Verkuil314527a2013-03-15 06:10:40 -03003068 if (std & V4L2_STD_PAL) {
Ladislav Michla637a112005-09-01 15:07:34 +00003069 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3070 &flags);
Hans Verkuil314527a2013-03-15 06:10:40 -03003071 } else if (std & V4L2_STD_NTSC) {
Ladislav Michla637a112005-09-01 15:07:34 +00003072 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3073 &flags);
Hans Verkuil314527a2013-03-15 06:10:40 -03003074 } else if (std & V4L2_STD_SECAM) {
Ladislav Michla637a112005-09-01 15:07:34 +00003075 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3076 &flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003077 } else {
3078 ret = -EINVAL;
3079 }
Ladislav Michla637a112005-09-01 15:07:34 +00003080
3081 if (ret) {
3082 ret = -EINVAL;
3083 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003084 } else {
3085 ret = -EINVAL;
3086 }
3087
3088out:
Ladislav Michla637a112005-09-01 15:07:34 +00003089 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003090
3091 return ret;
3092}
3093
Hans Verkuilc49cb362009-02-12 10:32:50 -03003094static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003095 struct v4l2_fmtdesc *fd)
3096{
Trent Piepho4f5a7442009-03-28 22:25:36 -03003097 dprintk("format index = %d\n", fd->index);
Hans Verkuilc49cb362009-02-12 10:32:50 -03003098
Trent Piepho4f5a7442009-03-28 22:25:36 -03003099 if (fd->index >= VINO_DATA_FMT_COUNT)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003100 return -EINVAL;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003101 dprintk("format name = %s\n", vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003102
Trent Piepho4f5a7442009-03-28 22:25:36 -03003103 fd->pixelformat = vino_data_formats[fd->index].pixelformat;
3104 strcpy(fd->description, vino_data_formats[fd->index].description);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003105 return 0;
3106}
3107
Hans Verkuilc49cb362009-02-12 10:32:50 -03003108static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003109 struct v4l2_format *f)
3110{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003111 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003112 struct vino_channel_settings tempvcs;
Ladislav Michla637a112005-09-01 15:07:34 +00003113 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003114 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003115
Hans Verkuilc49cb362009-02-12 10:32:50 -03003116 dprintk("requested: w = %d, h = %d\n",
3117 pf->width, pf->height);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003118
Hans Verkuilc49cb362009-02-12 10:32:50 -03003119 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3120 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3121 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003122
Hans Verkuilc49cb362009-02-12 10:32:50 -03003123 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3124 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3125 tempvcs.data_format = VINO_DATA_FMT_GREY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003126 pf->pixelformat =
Hans Verkuilc49cb362009-02-12 10:32:50 -03003127 vino_data_formats[tempvcs.data_format].
3128 pixelformat;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003129 }
3130
Hans Verkuilc49cb362009-02-12 10:32:50 -03003131 /* data format must be set before clipping/scaling */
3132 vino_set_scaling(&tempvcs, pf->width, pf->height);
3133
3134 dprintk("data format = %s\n",
3135 vino_data_formats[tempvcs.data_format].description);
3136
3137 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3138 tempvcs.decimation;
3139 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3140 tempvcs.decimation;
3141
3142 pf->field = V4L2_FIELD_INTERLACED;
3143 pf->bytesperline = tempvcs.line_size;
3144 pf->sizeimage = tempvcs.line_size *
3145 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3146 tempvcs.decimation;
3147 pf->colorspace =
3148 vino_data_formats[tempvcs.data_format].colorspace;
3149
Ralf Baechled203a7e2005-09-06 15:19:37 -07003150 return 0;
3151}
3152
Hans Verkuilc49cb362009-02-12 10:32:50 -03003153static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003154 struct v4l2_format *f)
3155{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003156 struct vino_channel_settings *vcs = video_drvdata(file);
3157 unsigned long flags;
3158 struct v4l2_pix_format *pf = &f->fmt.pix;
3159
3160 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3161
3162 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3163 vcs->decimation;
3164 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3165 vcs->decimation;
3166 pf->pixelformat =
3167 vino_data_formats[vcs->data_format].pixelformat;
3168
3169 pf->field = V4L2_FIELD_INTERLACED;
3170 pf->bytesperline = vcs->line_size;
3171 pf->sizeimage = vcs->line_size *
3172 (vcs->clipping.bottom - vcs->clipping.top) /
3173 vcs->decimation;
3174 pf->colorspace =
3175 vino_data_formats[vcs->data_format].colorspace;
3176
Hans Verkuilc49cb362009-02-12 10:32:50 -03003177 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3178 return 0;
3179}
3180
3181static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3182 struct v4l2_format *f)
3183{
3184 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003185 int data_format;
Ladislav Michla637a112005-09-01 15:07:34 +00003186 unsigned long flags;
Hans Verkuilc49cb362009-02-12 10:32:50 -03003187 struct v4l2_pix_format *pf = &f->fmt.pix;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003188
Hans Verkuilc49cb362009-02-12 10:32:50 -03003189 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003190
Hans Verkuilc49cb362009-02-12 10:32:50 -03003191 data_format = vino_find_data_format(pf->pixelformat);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003192
Hans Verkuilc49cb362009-02-12 10:32:50 -03003193 if (data_format == VINO_DATA_FMT_NONE) {
3194 vcs->data_format = VINO_DATA_FMT_GREY;
3195 pf->pixelformat =
3196 vino_data_formats[vcs->data_format].
3197 pixelformat;
3198 } else {
3199 vcs->data_format = data_format;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003200 }
3201
Hans Verkuilc49cb362009-02-12 10:32:50 -03003202 /* data format must be set before clipping/scaling */
3203 vino_set_scaling(vcs, pf->width, pf->height);
3204
3205 dprintk("data format = %s\n",
3206 vino_data_formats[vcs->data_format].description);
3207
3208 pf->width = vcs->clipping.right - vcs->clipping.left;
3209 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3210
3211 pf->field = V4L2_FIELD_INTERLACED;
3212 pf->bytesperline = vcs->line_size;
3213 pf->sizeimage = vcs->line_size *
3214 (vcs->clipping.bottom - vcs->clipping.top) /
3215 vcs->decimation;
3216 pf->colorspace =
3217 vino_data_formats[vcs->data_format].colorspace;
3218
Hans Verkuilc49cb362009-02-12 10:32:50 -03003219 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003220 return 0;
3221}
3222
Hans Verkuilc49cb362009-02-12 10:32:50 -03003223static int vino_cropcap(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003224 struct v4l2_cropcap *ccap)
3225{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003226 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003227 const struct vino_data_norm *norm;
Ladislav Michla637a112005-09-01 15:07:34 +00003228 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003229
3230 switch (ccap->type) {
3231 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003232 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3233
Ralf Baechled203a7e2005-09-06 15:19:37 -07003234 norm = &vino_data_norms[vcs->data_norm];
Ladislav Michla637a112005-09-01 15:07:34 +00003235
3236 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003237
3238 ccap->bounds.left = 0;
3239 ccap->bounds.top = 0;
3240 ccap->bounds.width = norm->width;
3241 ccap->bounds.height = norm->height;
3242 memcpy(&ccap->defrect, &ccap->bounds,
3243 sizeof(struct v4l2_rect));
3244
3245 ccap->pixelaspect.numerator = 1;
3246 ccap->pixelaspect.denominator = 1;
3247 break;
3248 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3249 default:
3250 return -EINVAL;
3251 }
3252
3253 return 0;
3254}
3255
Hans Verkuilc49cb362009-02-12 10:32:50 -03003256static int vino_g_crop(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003257 struct v4l2_crop *c)
3258{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003259 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003260 unsigned long flags;
3261
Ralf Baechled203a7e2005-09-06 15:19:37 -07003262 switch (c->type) {
3263 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003264 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003265
3266 c->c.left = vcs->clipping.left;
3267 c->c.top = vcs->clipping.top;
3268 c->c.width = vcs->clipping.right - vcs->clipping.left;
3269 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3270
Ladislav Michla637a112005-09-01 15:07:34 +00003271 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003272 break;
3273 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3274 default:
3275 return -EINVAL;
3276 }
3277
3278 return 0;
3279}
3280
Hans Verkuilc49cb362009-02-12 10:32:50 -03003281static int vino_s_crop(struct file *file, void *__fh,
Hans Verkuil4f996592012-09-05 05:10:48 -03003282 const struct v4l2_crop *c)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003283{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003284 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003285 unsigned long flags;
3286
Ralf Baechled203a7e2005-09-06 15:19:37 -07003287 switch (c->type) {
3288 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Ladislav Michla637a112005-09-01 15:07:34 +00003289 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003290
Ralf Baechled203a7e2005-09-06 15:19:37 -07003291 vino_set_clipping(vcs, c->c.left, c->c.top,
3292 c->c.width, c->c.height);
3293
Ladislav Michla637a112005-09-01 15:07:34 +00003294 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003295 break;
3296 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3297 default:
3298 return -EINVAL;
3299 }
3300
3301 return 0;
3302}
3303
Hans Verkuilc49cb362009-02-12 10:32:50 -03003304static int vino_g_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003305 struct v4l2_streamparm *sp)
3306{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003307 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003308 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003309 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003310
Trent Piepho4f5a7442009-03-28 22:25:36 -03003311 cp->capability = V4L2_CAP_TIMEPERFRAME;
3312 cp->timeperframe.numerator = 1;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003313
Trent Piepho4f5a7442009-03-28 22:25:36 -03003314 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003315
Trent Piepho4f5a7442009-03-28 22:25:36 -03003316 cp->timeperframe.denominator = vcs->fps;
Ladislav Michla637a112005-09-01 15:07:34 +00003317
Trent Piepho4f5a7442009-03-28 22:25:36 -03003318 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ladislav Michla637a112005-09-01 15:07:34 +00003319
Trent Piepho4f5a7442009-03-28 22:25:36 -03003320 /* TODO: cp->readbuffers = xxx; */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003321
3322 return 0;
3323}
3324
Hans Verkuilc49cb362009-02-12 10:32:50 -03003325static int vino_s_parm(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003326 struct v4l2_streamparm *sp)
3327{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003328 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003329 unsigned long flags;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003330 struct v4l2_captureparm *cp = &sp->parm.capture;
Ladislav Michla637a112005-09-01 15:07:34 +00003331
Trent Piepho4f5a7442009-03-28 22:25:36 -03003332 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003333
Trent Piepho4f5a7442009-03-28 22:25:36 -03003334 if ((cp->timeperframe.numerator == 0) ||
3335 (cp->timeperframe.denominator == 0)) {
3336 /* reset framerate */
3337 vino_set_default_framerate(vcs);
3338 } else {
3339 vino_set_framerate(vcs, cp->timeperframe.denominator /
3340 cp->timeperframe.numerator);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003341 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003342
3343 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003344
3345 return 0;
3346}
3347
Hans Verkuilc49cb362009-02-12 10:32:50 -03003348static int vino_reqbufs(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003349 struct v4l2_requestbuffers *rb)
3350{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003351 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003352
Ralf Baechled203a7e2005-09-06 15:19:37 -07003353 if (vcs->reading)
3354 return -EBUSY;
3355
Trent Piepho4f5a7442009-03-28 22:25:36 -03003356 /* TODO: check queue type */
3357 if (rb->memory != V4L2_MEMORY_MMAP) {
3358 dprintk("type not mmap\n");
3359 return -EINVAL;
3360 }
3361
3362 dprintk("count = %d\n", rb->count);
3363 if (rb->count > 0) {
3364 if (vino_is_capturing(vcs)) {
3365 dprintk("busy, capturing\n");
3366 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003367 }
3368
Trent Piepho4f5a7442009-03-28 22:25:36 -03003369 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3370 dprintk("busy, buffers still mapped\n");
3371 return -EBUSY;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003372 } else {
Ladislav Michla637a112005-09-01 15:07:34 +00003373 vcs->streaming = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003374 vino_queue_free(&vcs->fb_queue);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003375 vino_queue_init(&vcs->fb_queue, &rb->count);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003376 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003377 } else {
3378 vcs->streaming = 0;
3379 vino_capture_stop(vcs);
3380 vino_queue_free(&vcs->fb_queue);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003381 }
3382
3383 return 0;
3384}
3385
3386static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3387 struct vino_framebuffer *fb,
3388 struct v4l2_buffer *b)
3389{
3390 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3391 fb->id)) {
3392 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3393 b->flags |= V4L2_BUF_FLAG_DONE;
3394 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3395 fb->id)) {
3396 b->flags &= ~V4L2_BUF_FLAG_DONE;
3397 b->flags |= V4L2_BUF_FLAG_QUEUED;
3398 } else {
3399 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3400 V4L2_BUF_FLAG_QUEUED);
3401 }
3402
3403 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3404
3405 if (fb->map_count > 0)
3406 b->flags |= V4L2_BUF_FLAG_MAPPED;
3407
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03003408 b->flags &= ~V4L2_BUF_FLAG_TIMESTAMP_MASK;
3409 b->flags |= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
3410
Ralf Baechled203a7e2005-09-06 15:19:37 -07003411 b->index = fb->id;
3412 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3413 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3414 b->m.offset = fb->offset;
3415 b->bytesused = fb->data_size;
3416 b->length = fb->size;
3417 b->field = V4L2_FIELD_INTERLACED;
3418 b->sequence = fb->frame_counter;
3419 memcpy(&b->timestamp, &fb->timestamp,
3420 sizeof(struct timeval));
3421 // b->input ?
3422
3423 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3424 fb->id, fb->size, fb->data_size, fb->offset);
3425}
3426
Hans Verkuilc49cb362009-02-12 10:32:50 -03003427static int vino_querybuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003428 struct v4l2_buffer *b)
3429{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003430 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003431 struct vino_framebuffer *fb;
3432
Ralf Baechled203a7e2005-09-06 15:19:37 -07003433 if (vcs->reading)
3434 return -EBUSY;
3435
Trent Piepho4f5a7442009-03-28 22:25:36 -03003436 /* TODO: check queue type */
3437 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3438 dprintk("invalid index = %d\n",
3439 b->index);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003440 return -EINVAL;
3441 }
3442
Trent Piepho4f5a7442009-03-28 22:25:36 -03003443 fb = vino_queue_get_buffer(&vcs->fb_queue,
3444 b->index);
3445 if (fb == NULL) {
3446 dprintk("vino_queue_get_buffer() failed");
3447 return -EINVAL;
3448 }
3449
3450 vino_v4l2_get_buffer_status(vcs, fb, b);
3451
Ralf Baechled203a7e2005-09-06 15:19:37 -07003452 return 0;
3453}
3454
Hans Verkuilc49cb362009-02-12 10:32:50 -03003455static int vino_qbuf(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003456 struct v4l2_buffer *b)
3457{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003458 struct vino_channel_settings *vcs = video_drvdata(file);
Trent Piepho4f5a7442009-03-28 22:25:36 -03003459 struct vino_framebuffer *fb;
3460 int ret;
3461
Ralf Baechled203a7e2005-09-06 15:19:37 -07003462 if (vcs->reading)
3463 return -EBUSY;
3464
Trent Piepho4f5a7442009-03-28 22:25:36 -03003465 /* TODO: check queue type */
3466 if (b->memory != V4L2_MEMORY_MMAP) {
3467 dprintk("type not mmap\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003468 return -EINVAL;
3469 }
3470
Trent Piepho4f5a7442009-03-28 22:25:36 -03003471 fb = vino_capture_enqueue(vcs, b->index);
3472 if (fb == NULL)
3473 return -EINVAL;
3474
3475 vino_v4l2_get_buffer_status(vcs, fb, b);
3476
3477 if (vcs->streaming) {
3478 ret = vino_capture_next(vcs, 1);
3479 if (ret)
3480 return ret;
3481 }
3482
Ralf Baechled203a7e2005-09-06 15:19:37 -07003483 return 0;
3484}
3485
Hans Verkuilc49cb362009-02-12 10:32:50 -03003486static int vino_dqbuf(struct file *file, void *__fh,
3487 struct v4l2_buffer *b)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003488{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003489 struct vino_channel_settings *vcs = video_drvdata(file);
3490 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
Trent Piepho4f5a7442009-03-28 22:25:36 -03003491 struct vino_framebuffer *fb;
3492 unsigned int incoming, outgoing;
3493 int err;
3494
Ralf Baechled203a7e2005-09-06 15:19:37 -07003495 if (vcs->reading)
3496 return -EBUSY;
3497
Trent Piepho4f5a7442009-03-28 22:25:36 -03003498 /* TODO: check queue type */
Ralf Baechled203a7e2005-09-06 15:19:37 -07003499
Trent Piepho4f5a7442009-03-28 22:25:36 -03003500 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3501 if (err) {
3502 dprintk("vino_queue_get_incoming() failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -07003503 return -EINVAL;
3504 }
Trent Piepho4f5a7442009-03-28 22:25:36 -03003505 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3506 if (err) {
3507 dprintk("vino_queue_get_outgoing() failed\n");
3508 return -EINVAL;
3509 }
3510
3511 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3512
3513 if (outgoing == 0) {
3514 if (incoming == 0) {
3515 dprintk("no incoming or outgoing buffers\n");
3516 return -EINVAL;
3517 }
3518 if (nonblocking) {
3519 dprintk("non-blocking I/O was selected and "
3520 "there are no buffers to dequeue\n");
3521 return -EAGAIN;
3522 }
3523
3524 err = vino_wait_for_frame(vcs);
3525 if (err) {
3526 err = vino_wait_for_frame(vcs);
3527 if (err) {
3528 /* interrupted or no frames captured because of
3529 * frame skipping */
3530 /* vino_capture_failed(vcs); */
3531 return -EIO;
3532 }
3533 }
3534 }
3535
3536 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3537 if (fb == NULL) {
3538 dprintk("vino_queue_remove() failed\n");
3539 return -EINVAL;
3540 }
3541
3542 err = vino_check_buffer(vcs, fb);
3543
3544 vino_v4l2_get_buffer_status(vcs, fb, b);
3545
3546 if (err)
3547 return -EIO;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003548
3549 return 0;
3550}
3551
Hans Verkuilc49cb362009-02-12 10:32:50 -03003552static int vino_streamon(struct file *file, void *__fh,
3553 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003554{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003555 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003556 unsigned int incoming;
3557 int ret;
3558 if (vcs->reading)
3559 return -EBUSY;
3560
3561 if (vcs->streaming)
3562 return 0;
3563
3564 // TODO: check queue type
3565
3566 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3567 dprintk("no buffers allocated\n");
3568 return -EINVAL;
3569 }
3570
3571 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3572 if (ret) {
3573 dprintk("vino_queue_get_incoming() failed\n");
3574 return -EINVAL;
3575 }
3576
3577 vcs->streaming = 1;
3578
3579 if (incoming > 0) {
3580 ret = vino_capture_next(vcs, 1);
3581 if (ret) {
3582 vcs->streaming = 0;
3583
3584 dprintk("couldn't start capture\n");
3585 return -EINVAL;
3586 }
3587 }
3588
3589 return 0;
3590}
3591
Hans Verkuilc49cb362009-02-12 10:32:50 -03003592static int vino_streamoff(struct file *file, void *__fh,
3593 enum v4l2_buf_type i)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003594{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003595 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003596 if (vcs->reading)
3597 return -EBUSY;
3598
3599 if (!vcs->streaming)
3600 return 0;
3601
Ralf Baechled203a7e2005-09-06 15:19:37 -07003602 vcs->streaming = 0;
Ladislav Michla637a112005-09-01 15:07:34 +00003603 vino_capture_stop(vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003604
3605 return 0;
3606}
3607
Hans Verkuilc49cb362009-02-12 10:32:50 -03003608static int vino_queryctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003609 struct v4l2_queryctrl *queryctrl)
3610{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003611 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003612 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003613 int i;
3614 int err = 0;
3615
Ladislav Michla637a112005-09-01 15:07:34 +00003616 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003617
3618 switch (vcs->input) {
3619 case VINO_INPUT_D1:
3620 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3621 if (vino_indycam_v4l2_controls[i].id ==
3622 queryctrl->id) {
3623 memcpy(queryctrl,
3624 &vino_indycam_v4l2_controls[i],
3625 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003626 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003627 goto found;
3628 }
3629 }
3630
3631 err = -EINVAL;
3632 break;
3633 case VINO_INPUT_COMPOSITE:
3634 case VINO_INPUT_SVIDEO:
3635 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3636 if (vino_saa7191_v4l2_controls[i].id ==
3637 queryctrl->id) {
3638 memcpy(queryctrl,
3639 &vino_saa7191_v4l2_controls[i],
3640 sizeof(struct v4l2_queryctrl));
Ladislav Michla637a112005-09-01 15:07:34 +00003641 queryctrl->reserved[0] = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003642 goto found;
3643 }
3644 }
3645
3646 err = -EINVAL;
3647 break;
3648 default:
3649 err = -EINVAL;
3650 }
3651
3652 found:
Ladislav Michla637a112005-09-01 15:07:34 +00003653 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003654
3655 return err;
3656}
3657
Hans Verkuilc49cb362009-02-12 10:32:50 -03003658static int vino_g_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003659 struct v4l2_control *control)
3660{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003661 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003662 unsigned long flags;
3663 int i;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003664 int err = 0;
3665
Ladislav Michla637a112005-09-01 15:07:34 +00003666 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003667
3668 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003669 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003670 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003671 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003672 if (vino_indycam_v4l2_controls[i].id == control->id) {
3673 err = 0;
3674 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003675 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003676 }
Ladislav Michla637a112005-09-01 15:07:34 +00003677
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003678 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003679 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003680
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003681 err = camera_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003682 if (err)
3683 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003684 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003685 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003686 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003687 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003688 err = -EINVAL;
Ladislav Michla637a112005-09-01 15:07:34 +00003689 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003690 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3691 err = 0;
3692 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003693 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003694 }
Ladislav Michla637a112005-09-01 15:07:34 +00003695
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003696 if (err)
Ladislav Michla637a112005-09-01 15:07:34 +00003697 goto out;
Ladislav Michla637a112005-09-01 15:07:34 +00003698
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003699 err = decoder_call(core, g_ctrl, control);
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003700 if (err)
3701 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003702 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003703 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003704 default:
3705 err = -EINVAL;
3706 }
3707
Ladislav Michla637a112005-09-01 15:07:34 +00003708out:
3709 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003710
3711 return err;
3712}
3713
Hans Verkuilc49cb362009-02-12 10:32:50 -03003714static int vino_s_ctrl(struct file *file, void *__fh,
Ralf Baechled203a7e2005-09-06 15:19:37 -07003715 struct v4l2_control *control)
3716{
Hans Verkuilc49cb362009-02-12 10:32:50 -03003717 struct vino_channel_settings *vcs = video_drvdata(file);
Ladislav Michla637a112005-09-01 15:07:34 +00003718 unsigned long flags;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003719 int i;
3720 int err = 0;
3721
Ladislav Michla637a112005-09-01 15:07:34 +00003722 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3723
3724 if (!vino_is_input_owner(vcs)) {
3725 err = -EBUSY;
3726 goto out;
3727 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003728
3729 switch (vcs->input) {
Ladislav Michla637a112005-09-01 15:07:34 +00003730 case VINO_INPUT_D1: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003731 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003732 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003733 if (vino_indycam_v4l2_controls[i].id == control->id) {
3734 err = 0;
3735 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003736 }
3737 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003738 if (err)
3739 goto out;
3740 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3741 control->value > vino_indycam_v4l2_controls[i].maximum) {
3742 err = -ERANGE;
3743 goto out;
3744 }
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003745 err = camera_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003746 if (err)
3747 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003748 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003749 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003750 case VINO_INPUT_COMPOSITE:
Ladislav Michla637a112005-09-01 15:07:34 +00003751 case VINO_INPUT_SVIDEO: {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003752 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003753 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003754 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3755 err = 0;
3756 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003757 }
3758 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -03003759 if (err)
3760 goto out;
3761 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3762 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3763 err = -ERANGE;
3764 goto out;
3765 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003766
Hans Verkuil48f4dac2009-02-18 19:18:26 -03003767 err = decoder_call(core, s_ctrl, control);
Ladislav Michla637a112005-09-01 15:07:34 +00003768 if (err)
3769 err = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003770 break;
Ladislav Michla637a112005-09-01 15:07:34 +00003771 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07003772 default:
3773 err = -EINVAL;
3774 }
3775
Ladislav Michla637a112005-09-01 15:07:34 +00003776out:
3777 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003778
3779 return err;
3780}
3781
3782/* File operations */
3783
Hans Verkuilbec43662008-12-30 06:58:20 -03003784static int vino_open(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003785{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003786 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003787 int ret = 0;
3788 dprintk("open(): channel = %c\n",
3789 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3790
Ingo Molnar3593cab2006-02-07 06:49:14 -02003791 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003792
3793 if (vcs->users) {
3794 dprintk("open(): driver busy\n");
3795 ret = -EBUSY;
3796 goto out;
3797 }
3798
3799 ret = vino_acquire_input(vcs);
3800 if (ret) {
3801 dprintk("open(): vino_acquire_input() failed\n");
3802 goto out;
3803 }
3804
3805 vcs->users++;
3806
3807 out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003808 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003809
3810 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3811
3812 return ret;
3813}
3814
Hans Verkuilbec43662008-12-30 06:58:20 -03003815static int vino_close(struct file *file)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003816{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003817 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003818 dprintk("close():\n");
3819
Ingo Molnar3593cab2006-02-07 06:49:14 -02003820 mutex_lock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003821
3822 vcs->users--;
3823
3824 if (!vcs->users) {
3825 vino_release_input(vcs);
3826
3827 /* stop DMA and free buffers */
3828 vino_capture_stop(vcs);
3829 vino_queue_free(&vcs->fb_queue);
3830 }
3831
Ingo Molnar3593cab2006-02-07 06:49:14 -02003832 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003833
3834 return 0;
3835}
3836
3837static void vino_vm_open(struct vm_area_struct *vma)
3838{
3839 struct vino_framebuffer *fb = vma->vm_private_data;
3840
3841 fb->map_count++;
3842 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3843}
3844
3845static void vino_vm_close(struct vm_area_struct *vma)
3846{
3847 struct vino_framebuffer *fb = vma->vm_private_data;
3848
3849 fb->map_count--;
3850 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3851}
3852
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003853static const struct vm_operations_struct vino_vm_ops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07003854 .open = vino_vm_open,
3855 .close = vino_vm_close,
3856};
3857
3858static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3859{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003860 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003861
3862 unsigned long start = vma->vm_start;
3863 unsigned long size = vma->vm_end - vma->vm_start;
3864 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3865
3866 struct vino_framebuffer *fb = NULL;
3867 unsigned int i, length;
3868 int ret = 0;
3869
3870 dprintk("mmap():\n");
3871
3872 // TODO: reject mmap if already mapped
3873
Ingo Molnar3593cab2006-02-07 06:49:14 -02003874 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07003875 return -EINTR;
3876
3877 if (vcs->reading) {
3878 ret = -EBUSY;
3879 goto out;
3880 }
3881
3882 // TODO: check queue type
3883
3884 if (!(vma->vm_flags & VM_WRITE)) {
3885 dprintk("mmap(): app bug: PROT_WRITE please\n");
3886 ret = -EINVAL;
3887 goto out;
3888 }
3889 if (!(vma->vm_flags & VM_SHARED)) {
3890 dprintk("mmap(): app bug: MAP_SHARED please\n");
3891 ret = -EINVAL;
3892 goto out;
3893 }
3894
3895 /* find the correct buffer using offset */
3896 length = vino_queue_get_length(&vcs->fb_queue);
3897 if (length == 0) {
3898 dprintk("mmap(): queue not initialized\n");
3899 ret = -EINVAL;
3900 goto out;
3901 }
3902
3903 for (i = 0; i < length; i++) {
3904 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3905 if (fb == NULL) {
3906 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3907 ret = -EINVAL;
3908 goto out;
3909 }
3910
3911 if (fb->offset == offset)
3912 goto found;
3913 }
3914
3915 dprintk("mmap(): invalid offset = %lu\n", offset);
3916 ret = -EINVAL;
3917 goto out;
3918
3919found:
3920 dprintk("mmap(): buffer = %d\n", i);
3921
3922 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3923 dprintk("mmap(): failed: size = %lu > %lu\n",
3924 size, fb->desc_table.page_count * PAGE_SIZE);
3925 ret = -EINVAL;
3926 goto out;
3927 }
3928
3929 for (i = 0; i < fb->desc_table.page_count; i++) {
3930 unsigned long pfn =
3931 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3932 PAGE_SHIFT;
3933
3934 if (size < PAGE_SIZE)
3935 break;
3936
3937 // protection was: PAGE_READONLY
3938 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3939 vma->vm_page_prot)) {
3940 dprintk("mmap(): remap_pfn_range() failed\n");
3941 ret = -EAGAIN;
3942 goto out;
3943 }
3944
3945 start += PAGE_SIZE;
3946 size -= PAGE_SIZE;
3947 }
3948
3949 fb->map_count = 1;
3950
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003951 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003952 vma->vm_flags &= ~VM_IO;
3953 vma->vm_private_data = fb;
3954 vma->vm_file = file;
3955 vma->vm_ops = &vino_vm_ops;
3956
3957out:
Ingo Molnar3593cab2006-02-07 06:49:14 -02003958 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003959
3960 return ret;
3961}
3962
3963static unsigned int vino_poll(struct file *file, poll_table *pt)
3964{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03003965 struct vino_channel_settings *vcs = video_drvdata(file);
Ralf Baechled203a7e2005-09-06 15:19:37 -07003966 unsigned int outgoing;
3967 unsigned int ret = 0;
3968
3969 // lock mutex (?)
3970 // TODO: this has to be corrected for different read modes
3971
3972 dprintk("poll():\n");
3973
3974 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3975 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3976 ret = POLLERR;
3977 goto error;
3978 }
3979 if (outgoing > 0)
3980 goto over;
3981
3982 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3983
3984 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3985 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3986 ret = POLLERR;
3987 goto error;
3988 }
3989
3990over:
3991 dprintk("poll(): data %savailable\n",
3992 (outgoing > 0) ? "" : "not ");
Ladislav Michla637a112005-09-01 15:07:34 +00003993
3994 if (outgoing > 0)
Ralf Baechled203a7e2005-09-06 15:19:37 -07003995 ret = POLLIN | POLLRDNORM;
Ralf Baechled203a7e2005-09-06 15:19:37 -07003996
3997error:
Ralf Baechled203a7e2005-09-06 15:19:37 -07003998 return ret;
3999}
4000
Hans Verkuil069b7472008-12-30 07:04:34 -03004001static long vino_ioctl(struct file *file,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004002 unsigned int cmd, unsigned long arg)
4003{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03004004 struct vino_channel_settings *vcs = video_drvdata(file);
Hans Verkuil069b7472008-12-30 07:04:34 -03004005 long ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004006
Ingo Molnar3593cab2006-02-07 06:49:14 -02004007 if (mutex_lock_interruptible(&vcs->mutex))
Ralf Baechled203a7e2005-09-06 15:19:37 -07004008 return -EINTR;
4009
Hans Verkuilc49cb362009-02-12 10:32:50 -03004010 ret = video_ioctl2(file, cmd, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004011
Ingo Molnar3593cab2006-02-07 06:49:14 -02004012 mutex_unlock(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004013
4014 return ret;
4015}
4016
4017/* Initialization and cleanup */
4018
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03004019/* __initdata */
4020static int vino_init_stage;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004021
Hans Verkuilc49cb362009-02-12 10:32:50 -03004022const struct v4l2_ioctl_ops vino_ioctl_ops = {
4023 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4024 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4025 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4026 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4027 .vidioc_querycap = vino_querycap,
4028 .vidioc_enum_input = vino_enum_input,
4029 .vidioc_g_input = vino_g_input,
4030 .vidioc_s_input = vino_s_input,
4031 .vidioc_g_std = vino_g_std,
4032 .vidioc_s_std = vino_s_std,
4033 .vidioc_querystd = vino_querystd,
4034 .vidioc_cropcap = vino_cropcap,
4035 .vidioc_s_crop = vino_s_crop,
4036 .vidioc_g_crop = vino_g_crop,
4037 .vidioc_s_parm = vino_s_parm,
4038 .vidioc_g_parm = vino_g_parm,
4039 .vidioc_reqbufs = vino_reqbufs,
4040 .vidioc_querybuf = vino_querybuf,
4041 .vidioc_qbuf = vino_qbuf,
4042 .vidioc_dqbuf = vino_dqbuf,
4043 .vidioc_streamon = vino_streamon,
4044 .vidioc_streamoff = vino_streamoff,
4045 .vidioc_queryctrl = vino_queryctrl,
4046 .vidioc_g_ctrl = vino_g_ctrl,
4047 .vidioc_s_ctrl = vino_s_ctrl,
4048};
4049
Hans Verkuilbec43662008-12-30 06:58:20 -03004050static const struct v4l2_file_operations vino_fops = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004051 .owner = THIS_MODULE,
4052 .open = vino_open,
4053 .release = vino_close,
Hans Verkuil28959632009-02-18 18:53:47 -03004054 .unlocked_ioctl = vino_ioctl,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004055 .mmap = vino_mmap,
4056 .poll = vino_poll,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004057};
4058
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004059static struct video_device vdev_template = {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004060 .name = "NOT SET",
Ralf Baechled203a7e2005-09-06 15:19:37 -07004061 .fops = &vino_fops,
Hans Verkuilc49cb362009-02-12 10:32:50 -03004062 .ioctl_ops = &vino_ioctl_ops,
4063 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004064};
4065
4066static void vino_module_cleanup(int stage)
4067{
4068 switch(stage) {
Hans Verkuil28959632009-02-18 18:53:47 -03004069 case 11:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004070 video_unregister_device(vino_drvdata->b.vdev);
4071 vino_drvdata->b.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004072 case 10:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004073 video_unregister_device(vino_drvdata->a.vdev);
4074 vino_drvdata->a.vdev = NULL;
Hans Verkuil28959632009-02-18 18:53:47 -03004075 case 9:
Jean Delvarecea0c682009-03-06 12:05:43 -03004076 i2c_del_adapter(&vino_i2c_adapter);
Hans Verkuil28959632009-02-18 18:53:47 -03004077 case 8:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004078 free_irq(SGI_VINO_IRQ, NULL);
Hans Verkuil28959632009-02-18 18:53:47 -03004079 case 7:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004080 if (vino_drvdata->b.vdev) {
4081 video_device_release(vino_drvdata->b.vdev);
4082 vino_drvdata->b.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004083 }
Hans Verkuil28959632009-02-18 18:53:47 -03004084 case 6:
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004085 if (vino_drvdata->a.vdev) {
4086 video_device_release(vino_drvdata->a.vdev);
4087 vino_drvdata->a.vdev = NULL;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004088 }
Hans Verkuil28959632009-02-18 18:53:47 -03004089 case 5:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004090 /* all entries in dma_cpu dummy table have the same address */
4091 dma_unmap_single(NULL,
4092 vino_drvdata->dummy_desc_table.dma_cpu[0],
4093 PAGE_SIZE, DMA_FROM_DEVICE);
4094 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4095 * sizeof(dma_addr_t),
4096 (void *)vino_drvdata->
4097 dummy_desc_table.dma_cpu,
4098 vino_drvdata->dummy_desc_table.dma);
Hans Verkuil28959632009-02-18 18:53:47 -03004099 case 4:
Ralf Baechled203a7e2005-09-06 15:19:37 -07004100 free_page(vino_drvdata->dummy_page);
Hans Verkuil28959632009-02-18 18:53:47 -03004101 case 3:
4102 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004103 case 2:
4104 kfree(vino_drvdata);
4105 case 1:
4106 iounmap(vino);
4107 case 0:
4108 break;
4109 default:
4110 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4111 stage);
4112 }
4113}
4114
4115static int vino_probe(void)
4116{
4117 unsigned long rev_id;
4118
4119 if (ip22_is_fullhouse()) {
4120 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4121 return -ENODEV;
4122 }
4123
4124 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4125 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4126 return -ENODEV;
4127 }
4128
4129 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4130 if (!vino) {
4131 printk(KERN_ERR "VINO: ioremap() failed\n");
4132 return -EIO;
4133 }
4134 vino_init_stage++;
4135
4136 if (get_dbe(rev_id, &(vino->rev_id))) {
4137 printk(KERN_ERR "Failed to read VINO revision register\n");
4138 vino_module_cleanup(vino_init_stage);
4139 return -ENODEV;
4140 }
4141
4142 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4143 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4144 rev_id);
4145 vino_module_cleanup(vino_init_stage);
4146 return -ENODEV;
4147 }
4148
Ladislav Michla637a112005-09-01 15:07:34 +00004149 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004150
4151 return 0;
4152}
4153
4154static int vino_init(void)
4155{
4156 dma_addr_t dma_dummy_address;
Hans Verkuil28959632009-02-18 18:53:47 -03004157 int err;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004158 int i;
4159
Panagiotis Issaris74081872006-01-11 19:40:56 -02004160 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004161 if (!vino_drvdata) {
4162 vino_module_cleanup(vino_init_stage);
4163 return -ENOMEM;
4164 }
Ralf Baechled203a7e2005-09-06 15:19:37 -07004165 vino_init_stage++;
Hans Verkuil28959632009-02-18 18:53:47 -03004166 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4167 sizeof(vino_drvdata->v4l2_dev.name));
4168 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4169 if (err)
4170 return err;
4171 vino_init_stage++;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004172
4173 /* create a dummy dma descriptor */
4174 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4175 if (!vino_drvdata->dummy_page) {
4176 vino_module_cleanup(vino_init_stage);
4177 return -ENOMEM;
4178 }
4179 vino_init_stage++;
4180
4181 // TODO: use page_count in dummy_desc_table
4182
4183 vino_drvdata->dummy_desc_table.dma_cpu =
4184 dma_alloc_coherent(NULL,
4185 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4186 &vino_drvdata->dummy_desc_table.dma,
4187 GFP_KERNEL | GFP_DMA);
4188 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4189 vino_module_cleanup(vino_init_stage);
4190 return -ENOMEM;
4191 }
4192 vino_init_stage++;
4193
4194 dma_dummy_address = dma_map_single(NULL,
4195 (void *)vino_drvdata->dummy_page,
4196 PAGE_SIZE, DMA_FROM_DEVICE);
4197 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4198 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4199 }
4200
4201 /* initialize VINO */
4202
4203 vino->control = 0;
4204 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4205 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4206 udelay(VINO_DESC_FETCH_DELAY);
4207
4208 vino->intr_status = 0;
4209
4210 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4211 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4212
4213 return 0;
4214}
4215
4216static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4217 unsigned int channel, const char *name)
4218{
4219 vcs->channel = channel;
4220 vcs->input = VINO_INPUT_NONE;
4221 vcs->alpha = 0;
4222 vcs->users = 0;
4223 vcs->data_format = VINO_DATA_FMT_GREY;
4224 vcs->data_norm = VINO_DATA_NORM_NTSC;
4225 vcs->decimation = 1;
4226 vino_set_default_clipping(vcs);
4227 vino_set_default_framerate(vcs);
4228
4229 vcs->capturing = 0;
4230
Ingo Molnar3593cab2006-02-07 06:49:14 -02004231 mutex_init(&vcs->mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004232 spin_lock_init(&vcs->capture_lock);
4233
Ingo Molnar3593cab2006-02-07 06:49:14 -02004234 mutex_init(&vcs->fb_queue.queue_mutex);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004235 spin_lock_init(&vcs->fb_queue.queue_lock);
4236 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4237
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004238 vcs->vdev = video_device_alloc();
4239 if (!vcs->vdev) {
Ralf Baechled203a7e2005-09-06 15:19:37 -07004240 vino_module_cleanup(vino_init_stage);
4241 return -ENOMEM;
4242 }
4243 vino_init_stage++;
4244
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004245 memcpy(vcs->vdev, &vdev_template,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004246 sizeof(struct video_device));
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004247 strcpy(vcs->vdev->name, name);
4248 vcs->vdev->release = video_device_release;
Hans Verkuil28959632009-02-18 18:53:47 -03004249 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
Ralf Baechled203a7e2005-09-06 15:19:37 -07004250
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004251 video_set_drvdata(vcs->vdev, vcs);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004252
4253 return 0;
4254}
4255
4256static int __init vino_module_init(void)
4257{
4258 int ret;
4259
4260 printk(KERN_INFO "SGI VINO driver version %s\n",
4261 VINO_MODULE_VERSION);
4262
4263 ret = vino_probe();
4264 if (ret)
4265 return ret;
4266
4267 ret = vino_init();
4268 if (ret)
4269 return ret;
4270
4271 /* initialize data structures */
4272
4273 spin_lock_init(&vino_drvdata->vino_lock);
4274 spin_lock_init(&vino_drvdata->input_lock);
4275
4276 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004277 vino_vdev_name_a);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004278 if (ret)
4279 return ret;
4280
4281 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004282 vino_vdev_name_b);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004283 if (ret)
4284 return ret;
4285
4286 /* initialize hardware and register V4L devices */
4287
4288 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4289 vino_driver_description, NULL);
4290 if (ret) {
4291 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4292 SGI_VINO_IRQ);
4293 vino_module_cleanup(vino_init_stage);
4294 return -EAGAIN;
4295 }
4296 vino_init_stage++;
4297
Jean Delvarecea0c682009-03-06 12:05:43 -03004298 ret = i2c_add_adapter(&vino_i2c_adapter);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004299 if (ret) {
4300 printk(KERN_ERR "VINO I2C bus registration failed\n");
4301 vino_module_cleanup(vino_init_stage);
4302 return ret;
4303 }
Hans Verkuil28959632009-02-18 18:53:47 -03004304 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
Ralf Baechled203a7e2005-09-06 15:19:37 -07004305 vino_init_stage++;
4306
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004307 ret = video_register_device(vino_drvdata->a.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004308 VFL_TYPE_GRABBER, -1);
4309 if (ret < 0) {
4310 printk(KERN_ERR "VINO channel A Video4Linux-device "
4311 "registration failed\n");
4312 vino_module_cleanup(vino_init_stage);
4313 return -EINVAL;
4314 }
4315 vino_init_stage++;
4316
Hans Verkuil0ef4dba2009-02-12 11:31:13 -03004317 ret = video_register_device(vino_drvdata->b.vdev,
Ralf Baechled203a7e2005-09-06 15:19:37 -07004318 VFL_TYPE_GRABBER, -1);
4319 if (ret < 0) {
4320 printk(KERN_ERR "VINO channel B Video4Linux-device "
4321 "registration failed\n");
4322 vino_module_cleanup(vino_init_stage);
4323 return -EINVAL;
4324 }
4325 vino_init_stage++;
4326
Hans Verkuile6574f22009-04-01 03:57:53 -03004327 vino_drvdata->decoder =
Hans Verkuil53dacb12009-08-10 02:49:08 -03004328 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03004329 "saa7191", 0, I2C_ADDRS(0x45));
Hans Verkuile6574f22009-04-01 03:57:53 -03004330 vino_drvdata->camera =
Hans Verkuil53dacb12009-08-10 02:49:08 -03004331 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03004332 "indycam", 0, I2C_ADDRS(0x2b));
Ralf Baechled203a7e2005-09-06 15:19:37 -07004333
4334 dprintk("init complete!\n");
4335
4336 return 0;
4337}
4338
4339static void __exit vino_module_exit(void)
4340{
4341 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4342 vino_module_cleanup(vino_init_stage);
4343 dprintk("cleanup complete, exit!\n");
4344}
4345
4346module_init(vino_module_init);
4347module_exit(vino_module_exit);