blob: 25364b8f857b7d6275b57b112abc60d4b35f9376 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Winbond w9966cf Webcam parport driver.
3
4 Version 0.32
5
6 Copyright (C) 2001 Jakob Kemi <jakob.kemi@post.utfors.se>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22/*
23 Supported devices:
24 *Lifeview FlyCam Supra (using the Philips saa7111a chip)
25
26 Does any other model using the w9966 interface chip exist ?
27
28 Todo:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *Add a working EPP mode, since DMA ECP read isn't implemented
31 in the parport drivers. (That's why it's so sloow)
32
33 *Add support for other ccd-control chips than the saa7111
34 please send me feedback on what kind of chips you have.
35
36 *Add proper probing. I don't know what's wrong with the IEEE1284
37 parport drivers but (IEEE1284_MODE_NIBBLE|IEEE1284_DEVICE_ID)
38 and nibble read seems to be broken for some peripherals.
39
40 *Add probing for onboard SRAM, port directions etc. (if possible)
41
42 *Add support for the hardware compressed modes (maybe using v4l2)
43
44 *Fix better support for the capture window (no skewed images, v4l
45 interface to capt. window)
46
47 *Probably some bugs that I don't know of
48
49 Please support me by sending feedback!
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 Changes:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 Alan Cox: Removed RGB mode for kernel merge, added THIS_MODULE
54 and owner support for newer module locks
55*/
56
57#include <linux/module.h>
58#include <linux/init.h>
59#include <linux/delay.h>
Mauro Carvalho Chehab7e0a16f2009-03-10 05:31:34 -030060#include <linux/videodev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030062#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030063#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/parport.h>
65
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030066/*#define DEBUG*/ /* Undef me for production */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#ifdef DEBUG
Harvey Harrison7e28adb2008-04-08 23:20:00 -030069#define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#else
71#define DPRINTF(x...)
72#endif
73
74/*
75 * Defines, simple typedefs etc.
76 */
77
78#define W9966_DRIVERNAME "W9966CF Webcam"
Hans Verkuil4bfdd582010-03-22 04:47:27 -030079#define W9966_MAXCAMS 4 /* Maximum number of cameras */
80#define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */
81#define W9966_SRAMSIZE 131072 /* 128kb */
82#define W9966_SRAMID 0x02 /* check w9966cf.pdf */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Hans Verkuil4bfdd582010-03-22 04:47:27 -030084/* Empirically determined window limits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define W9966_WND_MIN_X 16
86#define W9966_WND_MIN_Y 14
87#define W9966_WND_MAX_X 705
88#define W9966_WND_MAX_Y 253
89#define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
90#define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
91
Hans Verkuil4bfdd582010-03-22 04:47:27 -030092/* Keep track of our current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define W9966_STATE_PDEV 0x01
94#define W9966_STATE_CLAIMED 0x02
95#define W9966_STATE_VDEV 0x04
96
97#define W9966_I2C_W_ID 0x48
98#define W9966_I2C_R_ID 0x49
99#define W9966_I2C_R_DATA 0x08
100#define W9966_I2C_R_CLOCK 0x04
101#define W9966_I2C_W_DATA 0x02
102#define W9966_I2C_W_CLOCK 0x01
103
104struct w9966_dev {
105 unsigned char dev_state;
106 unsigned char i2c_state;
107 unsigned short ppmode;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300108 struct parport *pport;
109 struct pardevice *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct video_device vdev;
111 unsigned short width;
112 unsigned short height;
113 unsigned char brightness;
114 signed char contrast;
115 signed char color;
116 signed char hue;
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300117 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120/*
121 * Module specific properties
122 */
123
124MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
125MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
126MODULE_LICENSE("GPL");
127
128
129#ifdef MODULE
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300130static const char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#else
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300132static const char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif
134module_param_array(pardev, charp, NULL, 0);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300135MODULE_PARM_DESC(pardev, "pardev: where to search for\n"
136 "\teach camera. 'aggressive' means brute-force search.\n"
137 "\tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n"
138 "\tcam 1 to parport3 and search every parport for cam 2 etc...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300140static int parmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141module_param(parmode, int, 0);
142MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp");
143
144static int video_nr = -1;
145module_param(video_nr, int, 0);
146
147/*
148 * Private data
149 */
150
151static struct w9966_dev w9966_cams[W9966_MAXCAMS];
152
153/*
154 * Private function declares
155 */
156
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300157static inline void w9966_setState(struct w9966_dev *cam, int mask, int val);
158static inline int w9966_getState(struct w9966_dev *cam, int mask, int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static inline void w9966_pdev_claim(struct w9966_dev *vdev);
160static inline void w9966_pdev_release(struct w9966_dev *vdev);
161
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300162static int w9966_rReg(struct w9966_dev *cam, int reg);
163static int w9966_wReg(struct w9966_dev *cam, int reg, int data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#if 0
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300165static int w9966_rReg_i2c(struct w9966_dev *cam, int reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300167static int w9966_wReg_i2c(struct w9966_dev *cam, int reg, int data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int w9966_findlen(int near, int size, int maxlen);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300169static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor);
170static int w9966_setup(struct w9966_dev *cam, int x1, int y1, int x2, int y2, int w, int h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300172static int w9966_init(struct w9966_dev *cam, struct parport* port);
173static void w9966_term(struct w9966_dev *cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300175static inline void w9966_i2c_setsda(struct w9966_dev *cam, int state);
176static inline int w9966_i2c_setscl(struct w9966_dev *cam, int state);
177static inline int w9966_i2c_getsda(struct w9966_dev *cam);
178static inline int w9966_i2c_getscl(struct w9966_dev *cam);
179static int w9966_i2c_wbyte(struct w9966_dev *cam, int data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#if 0
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300181static int w9966_i2c_rbyte(struct w9966_dev *cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#endif
183
Hans Verkuil069b7472008-12-30 07:04:34 -0300184static long w9966_v4l_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned int cmd, unsigned long arg);
186static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
187 size_t count, loff_t *ppos);
188
Hans Verkuilbec43662008-12-30 06:58:20 -0300189static int w9966_exclusive_open(struct file *file)
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300190{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300191 struct w9966_dev *cam = video_drvdata(file);
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300192
193 return test_and_set_bit(0, &cam->in_use) ? -EBUSY : 0;
194}
195
Hans Verkuilbec43662008-12-30 06:58:20 -0300196static int w9966_exclusive_release(struct file *file)
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300197{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300198 struct w9966_dev *cam = video_drvdata(file);
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300199
200 clear_bit(0, &cam->in_use);
201 return 0;
202}
203
Hans Verkuilbec43662008-12-30 06:58:20 -0300204static const struct v4l2_file_operations w9966_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .owner = THIS_MODULE,
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300206 .open = w9966_exclusive_open,
207 .release = w9966_exclusive_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .ioctl = w9966_v4l_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .read = w9966_v4l_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211static struct video_device w9966_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .name = W9966_DRIVERNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .fops = &w9966_fops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300214 .release = video_device_release_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217/*
218 * Private function defines
219 */
220
221
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300222/* Set camera phase flags, so we know what to uninit when terminating */
223static inline void w9966_setState(struct w9966_dev *cam, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 cam->dev_state = (cam->dev_state & ~mask) ^ val;
226}
227
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300228/* Get camera phase flags */
229static inline int w9966_getState(struct w9966_dev *cam, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 return ((cam->dev_state & mask) == val);
232}
233
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300234/* Claim parport for ourself */
235static inline void w9966_pdev_claim(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 if (w9966_getState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED))
238 return;
239 parport_claim_or_block(cam->pdev);
240 w9966_setState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED);
241}
242
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300243/* Release parport for others to use */
244static inline void w9966_pdev_release(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 if (w9966_getState(cam, W9966_STATE_CLAIMED, 0))
247 return;
248 parport_release(cam->pdev);
249 w9966_setState(cam, W9966_STATE_CLAIMED, 0);
250}
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300251
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300252/* Read register from W9966 interface-chip
253 Expects a claimed pdev
254 -1 on error, else register data (byte) */
255static int w9966_rReg(struct w9966_dev *cam, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300257 /* ECP, read, regtransfer, REG, REG, REG, REG, REG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 const unsigned char addr = 0x80 | (reg & 0x1f);
259 unsigned char val;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
262 return -1;
263 if (parport_write(cam->pport, &addr, 1) != 1)
264 return -1;
265 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
266 return -1;
267 if (parport_read(cam->pport, &val, 1) != 1)
268 return -1;
269
270 return val;
271}
272
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300273/* Write register to W9966 interface-chip
274 Expects a claimed pdev
275 -1 on error */
276static int w9966_wReg(struct w9966_dev *cam, int reg, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300278 /* ECP, write, regtransfer, REG, REG, REG, REG, REG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 const unsigned char addr = 0xc0 | (reg & 0x1f);
280 const unsigned char val = data;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
283 return -1;
284 if (parport_write(cam->pport, &addr, 1) != 1)
285 return -1;
286 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
287 return -1;
288 if (parport_write(cam->pport, &val, 1) != 1)
289 return -1;
290
291 return 0;
292}
293
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300294/* Initialize camera device. Setup all internal flags, set a
295 default video mode, setup ccd-chip, register v4l device etc..
296 Also used for 'probing' of hardware.
297 -1 on error */
298static int w9966_init(struct w9966_dev *cam, struct parport* port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 if (cam->dev_state != 0)
301 return -1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 cam->pport = port;
304 cam->brightness = 128;
305 cam->contrast = 64;
306 cam->color = 64;
307 cam->hue = 0;
308
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300309/* Select requested transfer mode */
310 switch (parmode) {
311 default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 case 0:
313 if (port->modes & PARPORT_MODE_ECP)
314 cam->ppmode = IEEE1284_MODE_ECP;
315 else if (port->modes & PARPORT_MODE_EPP)
316 cam->ppmode = IEEE1284_MODE_EPP;
317 else
318 cam->ppmode = IEEE1284_MODE_ECP;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300319 break;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300320 case 1: /* hw- or sw-ecp */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 cam->ppmode = IEEE1284_MODE_ECP;
322 break;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300323 case 2: /* hw- or sw-epp */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 cam->ppmode = IEEE1284_MODE_EPP;
325 break;
326 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300327
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300328/* Tell the parport driver that we exists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
330 if (cam->pdev == NULL) {
331 DPRINTF("parport_register_device() failed\n");
332 return -1;
333 }
334 w9966_setState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
335
336 w9966_pdev_claim(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300337
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300338/* Setup a default capture mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
340 DPRINTF("w9966_setup() failed.\n");
341 return -1;
342 }
343
344 w9966_pdev_release(cam);
345
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300346/* Fill in the video_device struct and register us to v4l */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 memcpy(&cam->vdev, &w9966_template, sizeof(struct video_device));
Hans Verkuil601e9442008-08-23 07:24:07 -0300348 video_set_drvdata(&cam->vdev, cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Hans Verkuildc60de32008-09-03 17:11:58 -0300350 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 w9966_setState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300354
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300355 /* All ok */
356 printk(KERN_INFO "w9966cf: Found and initialized a webcam on %s.\n",
357 cam->pport->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359}
360
361
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300362/* Terminate everything gracefully */
363static void w9966_term(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300365/* Unregister from v4l */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (w9966_getState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
367 video_unregister_device(&cam->vdev);
368 w9966_setState(cam, W9966_STATE_VDEV, 0);
369 }
370
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300371/* Terminate from IEEE1284 mode and release pdev block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
373 w9966_pdev_claim(cam);
374 parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
375 w9966_pdev_release(cam);
376 }
377
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300378/* Unregister from parport */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
380 parport_unregister_device(cam->pdev);
381 w9966_setState(cam, W9966_STATE_PDEV, 0);
382 }
383}
384
385
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300386/* Find a good length for capture window (used both for W and H)
387 A bit ugly but pretty functional. The capture length
388 have to match the downscale */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int w9966_findlen(int near, int size, int maxlen)
390{
391 int bestlen = size;
392 int besterr = abs(near - bestlen);
393 int len;
394
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300395 for (len = size + 1; len < maxlen; len++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int err;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300397 if (((64 * size) % len) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 continue;
399
400 err = abs(near - len);
401
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300402 /* Only continue as long as we keep getting better values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (err > besterr)
404 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 besterr = err;
407 bestlen = len;
408 }
409
410 return bestlen;
411}
412
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300413/* Modify capture window (if necessary)
414 and calculate downscaling
415 Return -1 on error */
416static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 int maxlen = max - min;
419 int len = *end - *beg + 1;
420 int newlen = w9966_findlen(len, size, maxlen);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300421 int err = newlen - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300423 /* Check for bad format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (newlen > maxlen || newlen < size)
425 return -1;
426
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300427 /* Set factor (6 bit fixed) */
428 *factor = (64 * size) / newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (*factor == 64)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300430 *factor = 0x00; /* downscale is disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 else
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300432 *factor |= 0x80; /* set downscale-enable bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300434 /* Modify old beginning and end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *beg -= err / 2;
436 *end += err - (err / 2);
437
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300438 /* Move window if outside borders */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (*beg < min) {
440 *end += min - *beg;
441 *beg += min - *beg;
442 }
443 if (*end > max) {
444 *beg -= *end - max;
445 *end -= *end - max;
446 }
447
448 return 0;
449}
450
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300451/* Setup the cameras capture window etc.
452 Expects a claimed pdev
453 return -1 on error */
454static int w9966_setup(struct w9966_dev *cam, int x1, int y1, int x2, int y2, int w, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned int i;
457 unsigned int enh_s, enh_e;
458 unsigned char scale_x, scale_y;
459 unsigned char regs[0x1c];
460 unsigned char saa7111_regs[] = {
461 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00,
462 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00,
463 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
464 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0
465 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300466
467
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300468 if (w * h * 2 > W9966_SRAMSIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 DPRINTF("capture window exceeds SRAM size!.\n");
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300470 w = 200; h = 160; /* Pick default values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 w &= ~0x1;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300474 if (w < 2)
475 w = 2;
476 if (h < 1)
477 h = 1;
478 if (w > W9966_WND_MAX_W)
479 w = W9966_WND_MAX_W;
480 if (h > W9966_WND_MAX_H)
481 h = W9966_WND_MAX_H;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 cam->width = w;
484 cam->height = h;
485
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300486 enh_s = 0;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300487 enh_e = w * h * 2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300488
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300489/* Modify capture window if necessary and calculate downscaling */
490 if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 ||
491 w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0)
492 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300494 DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n",
495 w, h, x1, x2, y1, y2, scale_x & ~0x80, scale_y & ~0x80);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300496
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300497/* Setup registers */
498 regs[0x00] = 0x00; /* Set normal operation */
499 regs[0x01] = 0x18; /* Capture mode */
500 regs[0x02] = scale_y; /* V-scaling */
501 regs[0x03] = scale_x; /* H-scaling */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300502
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300503 /* Capture window */
504 regs[0x04] = (x1 & 0x0ff); /* X-start (8 low bits) */
505 regs[0x05] = (x1 & 0x300)>>8; /* X-start (2 high bits) */
506 regs[0x06] = (y1 & 0x0ff); /* Y-start (8 low bits) */
507 regs[0x07] = (y1 & 0x300)>>8; /* Y-start (2 high bits) */
508 regs[0x08] = (x2 & 0x0ff); /* X-end (8 low bits) */
509 regs[0x09] = (x2 & 0x300)>>8; /* X-end (2 high bits) */
510 regs[0x0a] = (y2 & 0x0ff); /* Y-end (8 low bits) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300512 regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300513
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300514 /* Enhancement layer */
515 regs[0x0d] = (enh_s & 0x000ff); /* Enh. start (0-7) */
516 regs[0x0e] = (enh_s & 0x0ff00) >> 8; /* Enh. start (8-15) */
517 regs[0x0f] = (enh_s & 0x70000) >> 16; /* Enh. start (16-17/18??) */
518 regs[0x10] = (enh_e & 0x000ff); /* Enh. end (0-7) */
519 regs[0x11] = (enh_e & 0x0ff00) >> 8; /* Enh. end (8-15) */
520 regs[0x12] = (enh_e & 0x70000) >> 16; /* Enh. end (16-17/18??) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300522 /* Misc */
523 regs[0x13] = 0x40; /* VEE control (raw 4:2:2) */
524 regs[0x17] = 0x00; /* ??? */
525 regs[0x18] = cam->i2c_state = 0x00; /* Serial bus */
526 regs[0x19] = 0xff; /* I/O port direction control */
527 regs[0x1a] = 0xff; /* I/O port data register */
528 regs[0x1b] = 0x10; /* ??? */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300529
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300530 /* SAA7111 chip settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 saa7111_regs[0x0a] = cam->brightness;
532 saa7111_regs[0x0b] = cam->contrast;
533 saa7111_regs[0x0c] = cam->color;
534 saa7111_regs[0x0d] = cam->hue;
535
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300536/* Reset (ECP-fifo & serial-bus) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (w9966_wReg(cam, 0x00, 0x03) == -1)
538 return -1;
539
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300540/* Write regs to w9966cf chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (i = 0; i < 0x1c; i++)
542 if (w9966_wReg(cam, i, regs[i]) == -1)
543 return -1;
544
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300545/* Write regs to saa7111 chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for (i = 0; i < 0x20; i++)
547 if (w9966_wReg_i2c(cam, i, saa7111_regs[i]) == -1)
548 return -1;
549
550 return 0;
551}
552
553/*
554 * Ugly and primitive i2c protocol functions
555 */
556
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300557/* Sets the data line on the i2c bus.
558 Expects a claimed pdev. */
559static inline void w9966_i2c_setsda(struct w9966_dev *cam, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 if (state)
562 cam->i2c_state |= W9966_I2C_W_DATA;
563 else
564 cam->i2c_state &= ~W9966_I2C_W_DATA;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 w9966_wReg(cam, 0x18, cam->i2c_state);
567 udelay(5);
568}
569
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300570/* Get peripheral clock line
571 Expects a claimed pdev. */
572static inline int w9966_i2c_getscl(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 const unsigned char state = w9966_rReg(cam, 0x18);
575 return ((state & W9966_I2C_R_CLOCK) > 0);
576}
577
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300578/* Sets the clock line on the i2c bus.
579 Expects a claimed pdev. -1 on error */
580static inline int w9966_i2c_setscl(struct w9966_dev *cam, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 unsigned long timeout;
583
584 if (state)
585 cam->i2c_state |= W9966_I2C_W_CLOCK;
586 else
587 cam->i2c_state &= ~W9966_I2C_W_CLOCK;
588
589 w9966_wReg(cam, 0x18, cam->i2c_state);
590 udelay(5);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300591
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300592 /* we go to high, we also expect the peripheral to ack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (state) {
594 timeout = jiffies + 100;
595 while (!w9966_i2c_getscl(cam)) {
596 if (time_after(jiffies, timeout))
597 return -1;
598 }
599 }
600 return 0;
601}
602
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300603/* Get peripheral data line
604 Expects a claimed pdev. */
605static inline int w9966_i2c_getsda(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 const unsigned char state = w9966_rReg(cam, 0x18);
608 return ((state & W9966_I2C_R_DATA) > 0);
609}
610
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300611/* Write a byte with ack to the i2c bus.
612 Expects a claimed pdev. -1 on error */
613static int w9966_i2c_wbyte(struct w9966_dev *cam, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int i;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300616
617 for (i = 7; i >= 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 w9966_i2c_setsda(cam, (data >> i) & 0x01);
619
620 if (w9966_i2c_setscl(cam, 1) == -1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300621 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 w9966_i2c_setscl(cam, 0);
623 }
624
625 w9966_i2c_setsda(cam, 1);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (w9966_i2c_setscl(cam, 1) == -1)
628 return -1;
629 w9966_i2c_setscl(cam, 0);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632}
633
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300634/* Read a data byte with ack from the i2c-bus
635 Expects a claimed pdev. -1 on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#if 0
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300637static int w9966_i2c_rbyte(struct w9966_dev *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 unsigned char data = 0x00;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300640 int i;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 w9966_i2c_setsda(cam, 1);
643
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300644 for (i = 0; i < 8; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (w9966_i2c_setscl(cam, 1) == -1)
646 return -1;
647 data = data << 1;
648 if (w9966_i2c_getsda(cam))
649 data |= 0x01;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 w9966_i2c_setscl(cam, 0);
652 }
653 return data;
654}
655#endif
656
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300657/* Read a register from the i2c device.
658 Expects claimed pdev. -1 on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#if 0
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300660static int w9966_rReg_i2c(struct w9966_dev *cam, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 int data;
663
664 w9966_i2c_setsda(cam, 0);
665 w9966_i2c_setscl(cam, 0);
666
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300667 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
668 w9966_i2c_wbyte(cam, reg) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -1;
670
671 w9966_i2c_setsda(cam, 1);
672 if (w9966_i2c_setscl(cam, 1) == -1)
673 return -1;
674 w9966_i2c_setsda(cam, 0);
675 w9966_i2c_setscl(cam, 0);
676
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300677 if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1)
678 return -1;
679 data = w9966_i2c_rbyte(cam);
680 if (data == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -1;
682
683 w9966_i2c_setsda(cam, 0);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (w9966_i2c_setscl(cam, 1) == -1)
686 return -1;
687 w9966_i2c_setsda(cam, 1);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return data;
690}
691#endif
692
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300693/* Write a register to the i2c device.
694 Expects claimed pdev. -1 on error */
695static int w9966_wReg_i2c(struct w9966_dev *cam, int reg, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 w9966_i2c_setsda(cam, 0);
698 w9966_i2c_setscl(cam, 0);
699
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300700 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
701 w9966_i2c_wbyte(cam, reg) == -1 ||
702 w9966_i2c_wbyte(cam, data) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -1;
704
705 w9966_i2c_setsda(cam, 0);
706 if (w9966_i2c_setscl(cam, 1) == -1)
707 return -1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 w9966_i2c_setsda(cam, 1);
710
711 return 0;
712}
713
714/*
715 * Video4linux interfacing
716 */
717
Hans Verkuil069b7472008-12-30 07:04:34 -0300718static long w9966_v4l_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300720 struct w9966_dev *cam = video_drvdata(file);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300721
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300722 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case VIDIOCGCAP:
724 {
725 static struct video_capability vcap = {
726 .name = W9966_DRIVERNAME,
727 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
728 .channels = 1,
729 .maxwidth = W9966_WND_MAX_W,
730 .maxheight = W9966_WND_MAX_H,
731 .minwidth = 2,
732 .minheight = 1,
733 };
734 struct video_capability *cap = arg;
735 *cap = vcap;
736 return 0;
737 }
738 case VIDIOCGCHAN:
739 {
740 struct video_channel *vch = arg;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300741 if (vch->channel != 0) /* We only support one channel (#0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EINVAL;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300743 memset(vch, 0, sizeof(*vch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 strcpy(vch->name, "CCD-input");
745 vch->type = VIDEO_TYPE_CAMERA;
746 return 0;
747 }
748 case VIDIOCSCHAN:
749 {
750 struct video_channel *vch = arg;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300751 if (vch->channel != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EINVAL;
753 return 0;
754 }
755 case VIDIOCGTUNER:
756 {
757 struct video_tuner *vtune = arg;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300758 if (vtune->tuner != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EINVAL;
760 strcpy(vtune->name, "no tuner");
761 vtune->rangelow = 0;
762 vtune->rangehigh = 0;
763 vtune->flags = VIDEO_TUNER_NORM;
764 vtune->mode = VIDEO_MODE_AUTO;
765 vtune->signal = 0xffff;
766 return 0;
767 }
768 case VIDIOCSTUNER:
769 {
770 struct video_tuner *vtune = arg;
771 if (vtune->tuner != 0)
772 return -EINVAL;
773 if (vtune->mode != VIDEO_MODE_AUTO)
774 return -EINVAL;
775 return 0;
776 }
777 case VIDIOCGPICT:
778 {
779 struct video_picture vpic = {
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300780 cam->brightness << 8, /* brightness */
781 (cam->hue + 128) << 8, /* hue */
782 cam->color << 9, /* color */
783 cam->contrast << 9, /* contrast */
784 0x8000, /* whiteness */
785 16, VIDEO_PALETTE_YUV422/* bpp, palette format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 };
787 struct video_picture *pic = arg;
788 *pic = vpic;
789 return 0;
790 }
791 case VIDIOCSPICT:
792 {
793 struct video_picture *vpic = arg;
audetto@tiscali.it2485eb02006-12-12 10:35:57 -0300794 if (vpic->depth != 16 || (vpic->palette != VIDEO_PALETTE_YUV422 && vpic->palette != VIDEO_PALETTE_YUYV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 cam->brightness = vpic->brightness >> 8;
798 cam->hue = (vpic->hue >> 8) - 128;
799 cam->color = vpic->colour >> 9;
800 cam->contrast = vpic->contrast >> 9;
801
802 w9966_pdev_claim(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (
805 w9966_wReg_i2c(cam, 0x0a, cam->brightness) == -1 ||
806 w9966_wReg_i2c(cam, 0x0b, cam->contrast) == -1 ||
807 w9966_wReg_i2c(cam, 0x0c, cam->color) == -1 ||
808 w9966_wReg_i2c(cam, 0x0d, cam->hue) == -1
809 ) {
810 w9966_pdev_release(cam);
811 return -EIO;
812 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 w9966_pdev_release(cam);
815 return 0;
816 }
817 case VIDIOCSWIN:
818 {
819 int ret;
820 struct video_window *vwin = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (vwin->flags != 0)
823 return -EINVAL;
824 if (vwin->clipcount != 0)
825 return -EINVAL;
826 if (vwin->width < 2 || vwin->width > W9966_WND_MAX_W)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (vwin->height < 1 || vwin->height > W9966_WND_MAX_H)
829 return -EINVAL;
830
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300831 /* Update camera regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 w9966_pdev_claim(cam);
833 ret = w9966_setup(cam, 0, 0, 1023, 1023, vwin->width, vwin->height);
834 w9966_pdev_release(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (ret != 0) {
837 DPRINTF("VIDIOCSWIN: w9966_setup() failed.\n");
838 return -EIO;
839 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842 }
843 case VIDIOCGWIN:
844 {
845 struct video_window *vwin = arg;
846 memset(vwin, 0, sizeof(*vwin));
847 vwin->width = cam->width;
848 vwin->height = cam->height;
849 return 0;
850 }
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300851 /* Unimplemented */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300852 case VIDIOCCAPTURE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 case VIDIOCGFBUF:
854 case VIDIOCSFBUF:
855 case VIDIOCKEY:
856 case VIDIOCGFREQ:
857 case VIDIOCSFREQ:
858 case VIDIOCGAUDIO:
859 case VIDIOCSAUDIO:
860 return -EINVAL;
861 default:
862 return -ENOIOCTLCMD;
863 }
864 return 0;
865}
866
Hans Verkuil069b7472008-12-30 07:04:34 -0300867static long w9966_v4l_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int cmd, unsigned long arg)
869{
Hans Verkuilf473bf72008-11-01 08:25:11 -0300870 return video_usercopy(file, cmd, arg, w9966_v4l_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300873/* Capture data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
875 size_t count, loff_t *ppos)
876{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300877 struct w9966_dev *cam = video_drvdata(file);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300878 unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned char __user *dest = (unsigned char __user *)buf;
880 unsigned long dleft = count;
881 unsigned char *tbuf;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300882
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300883 /* Why would anyone want more than this?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (count > cam->width * cam->height * 2)
885 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 w9966_pdev_claim(cam);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300888 w9966_wReg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */
889 w9966_wReg(cam, 0x00, 0x00); /* Return to normal operation */
890 w9966_wReg(cam, 0x01, 0x98); /* Enable capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300892 /* write special capture-addr and negotiate into data transfer */
893 if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) ||
894 (parport_write(cam->pport, &addr, 1) != 1) ||
895 (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 w9966_pdev_release(cam);
897 return -EFAULT;
898 }
899
900 tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
901 if (tbuf == NULL) {
902 count = -ENOMEM;
903 goto out;
904 }
905
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300906 while (dleft > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (parport_read(cam->pport, tbuf, tsize) < tsize) {
910 count = -EFAULT;
911 goto out;
912 }
913 if (copy_to_user(dest, tbuf, tsize) != 0) {
914 count = -EFAULT;
915 goto out;
916 }
917 dest += tsize;
918 dleft -= tsize;
919 }
920
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300921 w9966_wReg(cam, 0x01, 0x18); /* Disable capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923out:
924 kfree(tbuf);
925 w9966_pdev_release(cam);
926
927 return count;
928}
929
930
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300931/* Called once for every parport on init */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static void w9966_attach(struct parport *port)
933{
934 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300935
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300936 for (i = 0; i < W9966_MAXCAMS; i++) {
937 if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 continue;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300939 if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (w9966_init(&w9966_cams[i], port) != 0)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300941 w9966_term(&w9966_cams[i]);
942 break; /* return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 }
945}
946
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300947/* Called once for every parport on termination */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948static void w9966_detach(struct parport *port)
949{
950 int i;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (i = 0; i < W9966_MAXCAMS; i++)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300953 if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
954 w9966_term(&w9966_cams[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957
958static struct parport_driver w9966_ppd = {
959 .name = W9966_DRIVERNAME,
960 .attach = w9966_attach,
961 .detach = w9966_detach,
962};
963
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300964/* Module entry point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static int __init w9966_mod_init(void)
966{
967 int i;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 for (i = 0; i < W9966_MAXCAMS; i++)
970 w9966_cams[i].dev_state = 0;
971
972 return parport_register_driver(&w9966_ppd);
973}
974
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300975/* Module cleanup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976static void __exit w9966_mod_term(void)
977{
978 parport_unregister_driver(&w9966_ppd);
979}
980
981module_init(w9966_mod_init);
982module_exit(w9966_mod_term);