V4L/DVB: pms: remove unnecessary exclusive open/close
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / w9966.c
CommitLineData
1da177e4
LT
1/*
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:
d56410e0 29
1da177e4
LT
30 *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!
d56410e0 50
1da177e4 51 Changes:
d56410e0 52
1da177e4
LT
53 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>
7e0a16f6 60#include <linux/videodev.h>
5a0e3ad6 61#include <linux/slab.h>
5e87efa3 62#include <media/v4l2-common.h>
35ea11ff 63#include <media/v4l2-ioctl.h>
1da177e4
LT
64#include <linux/parport.h>
65
ff699e6b 66/*#define DEBUG*/ /* Undef me for production */
1da177e4
LT
67
68#ifdef DEBUG
7e28adb2 69#define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
1da177e4
LT
70#else
71#define DPRINTF(x...)
72#endif
73
74/*
75 * Defines, simple typedefs etc.
76 */
77
78#define W9966_DRIVERNAME "W9966CF Webcam"
4bfdd586
HV
79#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 */
1da177e4 83
4bfdd586 84/* Empirically determined window limits */
1da177e4
LT
85#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
4bfdd586 92/* Keep track of our current state */
1da177e4
LT
93#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;
4bfdd586
HV
108 struct parport *pport;
109 struct pardevice *pdev;
1da177e4
LT
110 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;
7d43cd53 117 unsigned long in_use;
1da177e4
LT
118};
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
4bfdd586 130static const char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
1da177e4 131#else
4bfdd586 132static const char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
1da177e4
LT
133#endif
134module_param_array(pardev, charp, NULL, 0);
4bfdd586
HV
135MODULE_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...");
1da177e4 139
ff699e6b 140static int parmode;
1da177e4
LT
141module_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
4bfdd586
HV
157static 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);
1da177e4
LT
159static inline void w9966_pdev_claim(struct w9966_dev *vdev);
160static inline void w9966_pdev_release(struct w9966_dev *vdev);
161
4bfdd586
HV
162static int w9966_rReg(struct w9966_dev *cam, int reg);
163static int w9966_wReg(struct w9966_dev *cam, int reg, int data);
1da177e4 164#if 0
4bfdd586 165static int w9966_rReg_i2c(struct w9966_dev *cam, int reg);
1da177e4 166#endif
4bfdd586 167static int w9966_wReg_i2c(struct w9966_dev *cam, int reg, int data);
1da177e4 168static int w9966_findlen(int near, int size, int maxlen);
4bfdd586
HV
169static 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);
1da177e4 171
4bfdd586
HV
172static int w9966_init(struct w9966_dev *cam, struct parport* port);
173static void w9966_term(struct w9966_dev *cam);
1da177e4 174
4bfdd586
HV
175static 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);
1da177e4 180#if 0
4bfdd586 181static int w9966_i2c_rbyte(struct w9966_dev *cam);
1da177e4
LT
182#endif
183
069b7479 184static long w9966_v4l_ioctl(struct file *file,
1da177e4
LT
185 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
bec43661 189static int w9966_exclusive_open(struct file *file)
7d43cd53 190{
c170ecf4 191 struct w9966_dev *cam = video_drvdata(file);
7d43cd53
HV
192
193 return test_and_set_bit(0, &cam->in_use) ? -EBUSY : 0;
194}
195
bec43661 196static int w9966_exclusive_release(struct file *file)
7d43cd53 197{
c170ecf4 198 struct w9966_dev *cam = video_drvdata(file);
7d43cd53
HV
199
200 clear_bit(0, &cam->in_use);
201 return 0;
202}
203
bec43661 204static const struct v4l2_file_operations w9966_fops = {
1da177e4 205 .owner = THIS_MODULE,
7d43cd53
HV
206 .open = w9966_exclusive_open,
207 .release = w9966_exclusive_release,
1da177e4
LT
208 .ioctl = w9966_v4l_ioctl,
209 .read = w9966_v4l_read,
1da177e4
LT
210};
211static struct video_device w9966_template = {
1da177e4 212 .name = W9966_DRIVERNAME,
1da177e4 213 .fops = &w9966_fops,
aa5e90af 214 .release = video_device_release_empty,
1da177e4
LT
215};
216
217/*
218 * Private function defines
219 */
220
221
4bfdd586
HV
222/* 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)
1da177e4
LT
224{
225 cam->dev_state = (cam->dev_state & ~mask) ^ val;
226}
227
4bfdd586
HV
228/* Get camera phase flags */
229static inline int w9966_getState(struct w9966_dev *cam, int mask, int val)
1da177e4
LT
230{
231 return ((cam->dev_state & mask) == val);
232}
233
4bfdd586
HV
234/* Claim parport for ourself */
235static inline void w9966_pdev_claim(struct w9966_dev *cam)
1da177e4
LT
236{
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
4bfdd586
HV
243/* Release parport for others to use */
244static inline void w9966_pdev_release(struct w9966_dev *cam)
1da177e4
LT
245{
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}
d56410e0 251
4bfdd586
HV
252/* 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)
1da177e4 256{
4bfdd586 257 /* ECP, read, regtransfer, REG, REG, REG, REG, REG */
1da177e4
LT
258 const unsigned char addr = 0x80 | (reg & 0x1f);
259 unsigned char val;
d56410e0 260
1da177e4
LT
261 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
4bfdd586
HV
273/* 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)
1da177e4 277{
4bfdd586 278 /* ECP, write, regtransfer, REG, REG, REG, REG, REG */
1da177e4
LT
279 const unsigned char addr = 0xc0 | (reg & 0x1f);
280 const unsigned char val = data;
d56410e0 281
1da177e4
LT
282 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
4bfdd586
HV
294/* 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)
1da177e4
LT
299{
300 if (cam->dev_state != 0)
301 return -1;
d56410e0 302
1da177e4
LT
303 cam->pport = port;
304 cam->brightness = 128;
305 cam->contrast = 64;
306 cam->color = 64;
307 cam->hue = 0;
308
4bfdd586
HV
309/* Select requested transfer mode */
310 switch (parmode) {
311 default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
1da177e4
LT
312 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;
d56410e0 319 break;
4bfdd586 320 case 1: /* hw- or sw-ecp */
1da177e4
LT
321 cam->ppmode = IEEE1284_MODE_ECP;
322 break;
4bfdd586 323 case 2: /* hw- or sw-epp */
1da177e4
LT
324 cam->ppmode = IEEE1284_MODE_EPP;
325 break;
326 }
d56410e0 327
4bfdd586 328/* Tell the parport driver that we exists */
1da177e4
LT
329 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);
d56410e0 337
4bfdd586 338/* Setup a default capture mode */
1da177e4
LT
339 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
4bfdd586 346/* Fill in the video_device struct and register us to v4l */
1da177e4 347 memcpy(&cam->vdev, &w9966_template, sizeof(struct video_device));
601e9444 348 video_set_drvdata(&cam->vdev, cam);
1da177e4 349
dc60de33 350 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1da177e4 351 return -1;
d56410e0 352
1da177e4 353 w9966_setState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
d56410e0 354
4bfdd586
HV
355 /* All ok */
356 printk(KERN_INFO "w9966cf: Found and initialized a webcam on %s.\n",
357 cam->pport->name);
1da177e4
LT
358 return 0;
359}
360
361
4bfdd586
HV
362/* Terminate everything gracefully */
363static void w9966_term(struct w9966_dev *cam)
1da177e4 364{
4bfdd586 365/* Unregister from v4l */
1da177e4
LT
366 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
4bfdd586 371/* Terminate from IEEE1284 mode and release pdev block */
1da177e4
LT
372 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
4bfdd586 378/* Unregister from parport */
1da177e4
LT
379 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
4bfdd586
HV
386/* 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 */
1da177e4
LT
389static int w9966_findlen(int near, int size, int maxlen)
390{
391 int bestlen = size;
392 int besterr = abs(near - bestlen);
393 int len;
394
4bfdd586 395 for (len = size + 1; len < maxlen; len++) {
1da177e4 396 int err;
4bfdd586 397 if (((64 * size) % len) != 0)
1da177e4
LT
398 continue;
399
400 err = abs(near - len);
401
4bfdd586 402 /* Only continue as long as we keep getting better values */
1da177e4
LT
403 if (err > besterr)
404 break;
d56410e0 405
1da177e4
LT
406 besterr = err;
407 bestlen = len;
408 }
409
410 return bestlen;
411}
412
4bfdd586
HV
413/* 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)
1da177e4
LT
417{
418 int maxlen = max - min;
419 int len = *end - *beg + 1;
420 int newlen = w9966_findlen(len, size, maxlen);
d56410e0 421 int err = newlen - len;
1da177e4 422
4bfdd586 423 /* Check for bad format */
1da177e4
LT
424 if (newlen > maxlen || newlen < size)
425 return -1;
426
4bfdd586
HV
427 /* Set factor (6 bit fixed) */
428 *factor = (64 * size) / newlen;
1da177e4 429 if (*factor == 64)
4bfdd586 430 *factor = 0x00; /* downscale is disabled */
1da177e4 431 else
4bfdd586 432 *factor |= 0x80; /* set downscale-enable bit */
1da177e4 433
4bfdd586 434 /* Modify old beginning and end */
1da177e4
LT
435 *beg -= err / 2;
436 *end += err - (err / 2);
437
4bfdd586 438 /* Move window if outside borders */
1da177e4
LT
439 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
4bfdd586
HV
451/* 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)
1da177e4
LT
455{
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 };
d56410e0
MCC
466
467
4bfdd586 468 if (w * h * 2 > W9966_SRAMSIZE) {
1da177e4 469 DPRINTF("capture window exceeds SRAM size!.\n");
4bfdd586 470 w = 200; h = 160; /* Pick default values */
1da177e4
LT
471 }
472
473 w &= ~0x1;
4bfdd586
HV
474 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;
1da177e4
LT
482
483 cam->width = w;
484 cam->height = h;
485
d56410e0 486 enh_s = 0;
4bfdd586
HV
487 enh_e = w * h * 2;
488
489/* 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;
493
494 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);
496
497/* 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 */
502
503 /* 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) */
511
512 regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */
513
514 /* 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??) */
521
522 /* 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; /* ??? */
529
530 /* SAA7111 chip settings */
1da177e4
LT
531 saa7111_regs[0x0a] = cam->brightness;
532 saa7111_regs[0x0b] = cam->contrast;
533 saa7111_regs[0x0c] = cam->color;
534 saa7111_regs[0x0d] = cam->hue;
535
4bfdd586 536/* Reset (ECP-fifo & serial-bus) */
1da177e4
LT
537 if (w9966_wReg(cam, 0x00, 0x03) == -1)
538 return -1;
539
4bfdd586 540/* Write regs to w9966cf chip */
1da177e4
LT
541 for (i = 0; i < 0x1c; i++)
542 if (w9966_wReg(cam, i, regs[i]) == -1)
543 return -1;
544
4bfdd586 545/* Write regs to saa7111 chip */
1da177e4
LT
546 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
4bfdd586
HV
557/* 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)
1da177e4
LT
560{
561 if (state)
562 cam->i2c_state |= W9966_I2C_W_DATA;
563 else
564 cam->i2c_state &= ~W9966_I2C_W_DATA;
d56410e0 565
1da177e4
LT
566 w9966_wReg(cam, 0x18, cam->i2c_state);
567 udelay(5);
568}
569
4bfdd586
HV
570/* Get peripheral clock line
571 Expects a claimed pdev. */
572static inline int w9966_i2c_getscl(struct w9966_dev *cam)
1da177e4
LT
573{
574 const unsigned char state = w9966_rReg(cam, 0x18);
575 return ((state & W9966_I2C_R_CLOCK) > 0);
576}
577
4bfdd586
HV
578/* 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)
1da177e4
LT
581{
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);
d56410e0 591
4bfdd586 592 /* we go to high, we also expect the peripheral to ack. */
1da177e4
LT
593 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
4bfdd586
HV
603/* Get peripheral data line
604 Expects a claimed pdev. */
605static inline int w9966_i2c_getsda(struct w9966_dev *cam)
1da177e4
LT
606{
607 const unsigned char state = w9966_rReg(cam, 0x18);
608 return ((state & W9966_I2C_R_DATA) > 0);
609}
610
4bfdd586
HV
611/* 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)
1da177e4
LT
614{
615 int i;
4bfdd586
HV
616
617 for (i = 7; i >= 0; i--) {
1da177e4
LT
618 w9966_i2c_setsda(cam, (data >> i) & 0x01);
619
620 if (w9966_i2c_setscl(cam, 1) == -1)
d56410e0 621 return -1;
1da177e4
LT
622 w9966_i2c_setscl(cam, 0);
623 }
624
625 w9966_i2c_setsda(cam, 1);
d56410e0 626
1da177e4
LT
627 if (w9966_i2c_setscl(cam, 1) == -1)
628 return -1;
629 w9966_i2c_setscl(cam, 0);
d56410e0 630
1da177e4
LT
631 return 0;
632}
633
4bfdd586
HV
634/* Read a data byte with ack from the i2c-bus
635 Expects a claimed pdev. -1 on error */
1da177e4 636#if 0
4bfdd586 637static int w9966_i2c_rbyte(struct w9966_dev *cam)
1da177e4
LT
638{
639 unsigned char data = 0x00;
d56410e0
MCC
640 int i;
641
1da177e4
LT
642 w9966_i2c_setsda(cam, 1);
643
4bfdd586 644 for (i = 0; i < 8; i++) {
1da177e4
LT
645 if (w9966_i2c_setscl(cam, 1) == -1)
646 return -1;
647 data = data << 1;
648 if (w9966_i2c_getsda(cam))
649 data |= 0x01;
d56410e0 650
1da177e4
LT
651 w9966_i2c_setscl(cam, 0);
652 }
653 return data;
654}
655#endif
656
4bfdd586
HV
657/* Read a register from the i2c device.
658 Expects claimed pdev. -1 on error */
1da177e4 659#if 0
4bfdd586 660static int w9966_rReg_i2c(struct w9966_dev *cam, int reg)
1da177e4
LT
661{
662 int data;
663
664 w9966_i2c_setsda(cam, 0);
665 w9966_i2c_setscl(cam, 0);
666
4bfdd586
HV
667 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
668 w9966_i2c_wbyte(cam, reg) == -1)
1da177e4
LT
669 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
4bfdd586
HV
677 if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1)
678 return -1;
679 data = w9966_i2c_rbyte(cam);
680 if (data == -1)
1da177e4
LT
681 return -1;
682
683 w9966_i2c_setsda(cam, 0);
d56410e0 684
1da177e4
LT
685 if (w9966_i2c_setscl(cam, 1) == -1)
686 return -1;
687 w9966_i2c_setsda(cam, 1);
d56410e0 688
1da177e4
LT
689 return data;
690}
691#endif
692
4bfdd586
HV
693/* 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)
1da177e4
LT
696{
697 w9966_i2c_setsda(cam, 0);
698 w9966_i2c_setscl(cam, 0);
699
4bfdd586
HV
700 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
701 w9966_i2c_wbyte(cam, reg) == -1 ||
702 w9966_i2c_wbyte(cam, data) == -1)
1da177e4
LT
703 return -1;
704
705 w9966_i2c_setsda(cam, 0);
706 if (w9966_i2c_setscl(cam, 1) == -1)
707 return -1;
d56410e0 708
1da177e4
LT
709 w9966_i2c_setsda(cam, 1);
710
711 return 0;
712}
713
714/*
715 * Video4linux interfacing
716 */
717
069b7479 718static long w9966_v4l_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1da177e4 719{
c170ecf4 720 struct w9966_dev *cam = video_drvdata(file);
d56410e0 721
4bfdd586 722 switch (cmd) {
1da177e4
LT
723 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;
4bfdd586 741 if (vch->channel != 0) /* We only support one channel (#0) */
1da177e4 742 return -EINVAL;
4bfdd586 743 memset(vch, 0, sizeof(*vch));
1da177e4
LT
744 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;
4bfdd586 751 if (vch->channel != 0)
1da177e4
LT
752 return -EINVAL;
753 return 0;
754 }
755 case VIDIOCGTUNER:
756 {
757 struct video_tuner *vtune = arg;
4bfdd586 758 if (vtune->tuner != 0)
1da177e4
LT
759 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 = {
4bfdd586
HV
780 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 */
1da177e4
LT
786 };
787 struct video_picture *pic = arg;
788 *pic = vpic;
789 return 0;
790 }
791 case VIDIOCSPICT:
792 {
793 struct video_picture *vpic = arg;
2485eb0a 794 if (vpic->depth != 16 || (vpic->palette != VIDEO_PALETTE_YUV422 && vpic->palette != VIDEO_PALETTE_YUYV))
1da177e4 795 return -EINVAL;
d56410e0 796
1da177e4
LT
797 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);
d56410e0 803
1da177e4
LT
804 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 }
d56410e0 813
1da177e4
LT
814 w9966_pdev_release(cam);
815 return 0;
816 }
817 case VIDIOCSWIN:
818 {
819 int ret;
820 struct video_window *vwin = arg;
d56410e0 821
1da177e4
LT
822 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)
d56410e0 827 return -EINVAL;
1da177e4
LT
828 if (vwin->height < 1 || vwin->height > W9966_WND_MAX_H)
829 return -EINVAL;
830
4bfdd586 831 /* Update camera regs */
1da177e4
LT
832 w9966_pdev_claim(cam);
833 ret = w9966_setup(cam, 0, 0, 1023, 1023, vwin->width, vwin->height);
834 w9966_pdev_release(cam);
d56410e0 835
1da177e4
LT
836 if (ret != 0) {
837 DPRINTF("VIDIOCSWIN: w9966_setup() failed.\n");
838 return -EIO;
839 }
d56410e0 840
1da177e4
LT
841 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 }
4bfdd586 851 /* Unimplemented */
d56410e0 852 case VIDIOCCAPTURE:
1da177e4
LT
853 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
069b7479 867static long w9966_v4l_ioctl(struct file *file,
1da177e4
LT
868 unsigned int cmd, unsigned long arg)
869{
f473bf76 870 return video_usercopy(file, cmd, arg, w9966_v4l_do_ioctl);
1da177e4
LT
871}
872
4bfdd586 873/* Capture data */
1da177e4
LT
874static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
875 size_t count, loff_t *ppos)
876{
c170ecf4 877 struct w9966_dev *cam = video_drvdata(file);
4bfdd586 878 unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
1da177e4
LT
879 unsigned char __user *dest = (unsigned char __user *)buf;
880 unsigned long dleft = count;
881 unsigned char *tbuf;
d56410e0 882
4bfdd586 883 /* Why would anyone want more than this?? */
1da177e4
LT
884 if (count > cam->width * cam->height * 2)
885 return -EINVAL;
d56410e0 886
1da177e4 887 w9966_pdev_claim(cam);
4bfdd586
HV
888 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 */
891
892 /* 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)) {
1da177e4
LT
896 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
4bfdd586 906 while (dleft > 0) {
1da177e4 907 unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
d56410e0 908
1da177e4
LT
909 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
4bfdd586 921 w9966_wReg(cam, 0x01, 0x18); /* Disable capture */
1da177e4
LT
922
923out:
924 kfree(tbuf);
925 w9966_pdev_release(cam);
926
927 return count;
928}
929
930
4bfdd586 931/* Called once for every parport on init */
1da177e4
LT
932static void w9966_attach(struct parport *port)
933{
934 int i;
d56410e0 935
4bfdd586
HV
936 for (i = 0; i < W9966_MAXCAMS; i++) {
937 if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
1da177e4 938 continue;
4bfdd586 939 if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
1da177e4 940 if (w9966_init(&w9966_cams[i], port) != 0)
4bfdd586
HV
941 w9966_term(&w9966_cams[i]);
942 break; /* return */
1da177e4
LT
943 }
944 }
945}
946
4bfdd586 947/* Called once for every parport on termination */
1da177e4
LT
948static void w9966_detach(struct parport *port)
949{
950 int i;
4bfdd586 951
1da177e4 952 for (i = 0; i < W9966_MAXCAMS; i++)
4bfdd586
HV
953 if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
954 w9966_term(&w9966_cams[i]);
1da177e4
LT
955}
956
957
958static struct parport_driver w9966_ppd = {
959 .name = W9966_DRIVERNAME,
960 .attach = w9966_attach,
961 .detach = w9966_detach,
962};
963
4bfdd586 964/* Module entry point */
1da177e4
LT
965static int __init w9966_mod_init(void)
966{
967 int i;
4bfdd586 968
1da177e4
LT
969 for (i = 0; i < W9966_MAXCAMS; i++)
970 w9966_cams[i].dev_state = 0;
971
972 return parport_register_driver(&w9966_ppd);
973}
974
4bfdd586 975/* Module cleanup */
1da177e4
LT
976static 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);