Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / w9966.c
blob08aaae07c7e0cb3c4a58e386bb49312068c2f613
1 /*
2 Winbond w9966cf Webcam parport driver.
4 Version 0.32
6 Copyright (C) 2001 Jakob Kemi <jakob.kemi@post.utfors.se>
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.
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.
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.
23 Supported devices:
24 *Lifeview FlyCam Supra (using the Philips saa7111a chip)
26 Does any other model using the w9966 interface chip exist ?
28 Todo:
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)
33 *Add support for other ccd-control chips than the saa7111
34 please send me feedback on what kind of chips you have.
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.
40 *Add probing for onboard SRAM, port directions etc. (if possible)
42 *Add support for the hardware compressed modes (maybe using v4l2)
44 *Fix better support for the capture window (no skewed images, v4l
45 interface to capt. window)
47 *Probably some bugs that I don't know of
49 Please support me by sending feedback!
51 Changes:
53 Alan Cox: Removed RGB mode for kernel merge, added THIS_MODULE
54 and owner support for newer module locks
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/delay.h>
60 #include <linux/videodev.h>
61 #include <media/v4l2-common.h>
62 #include <linux/parport.h>
64 //#define DEBUG // Undef me for production
66 #ifdef DEBUG
67 #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __FUNCTION__ , ##a)
68 #else
69 #define DPRINTF(x...)
70 #endif
73 * Defines, simple typedefs etc.
76 #define W9966_DRIVERNAME "W9966CF Webcam"
77 #define W9966_MAXCAMS 4 // Maximum number of cameras
78 #define W9966_RBUFFER 2048 // Read buffer (must be an even number)
79 #define W9966_SRAMSIZE 131072 // 128kb
80 #define W9966_SRAMID 0x02 // check w9966cf.pdf
82 // Empirically determined window limits
83 #define W9966_WND_MIN_X 16
84 #define W9966_WND_MIN_Y 14
85 #define W9966_WND_MAX_X 705
86 #define W9966_WND_MAX_Y 253
87 #define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
88 #define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
90 // Keep track of our current state
91 #define W9966_STATE_PDEV 0x01
92 #define W9966_STATE_CLAIMED 0x02
93 #define W9966_STATE_VDEV 0x04
95 #define W9966_I2C_W_ID 0x48
96 #define W9966_I2C_R_ID 0x49
97 #define W9966_I2C_R_DATA 0x08
98 #define W9966_I2C_R_CLOCK 0x04
99 #define W9966_I2C_W_DATA 0x02
100 #define W9966_I2C_W_CLOCK 0x01
102 struct w9966_dev {
103 unsigned char dev_state;
104 unsigned char i2c_state;
105 unsigned short ppmode;
106 struct parport* pport;
107 struct pardevice* pdev;
108 struct video_device vdev;
109 unsigned short width;
110 unsigned short height;
111 unsigned char brightness;
112 signed char contrast;
113 signed char color;
114 signed char hue;
118 * Module specific properties
121 MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
122 MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
123 MODULE_LICENSE("GPL");
126 #ifdef MODULE
127 static const char* pardev[] = {[0 ... W9966_MAXCAMS] = ""};
128 #else
129 static const char* pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
130 #endif
131 module_param_array(pardev, charp, NULL, 0);
132 MODULE_PARM_DESC(pardev, "pardev: where to search for\n\
133 \teach camera. 'aggressive' means brute-force search.\n\
134 \tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n\
135 \tcam 1 to parport3 and search every parport for cam 2 etc...");
137 static int parmode = 0;
138 module_param(parmode, int, 0);
139 MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp");
141 static int video_nr = -1;
142 module_param(video_nr, int, 0);
145 * Private data
148 static struct w9966_dev w9966_cams[W9966_MAXCAMS];
151 * Private function declares
154 static inline void w9966_setState(struct w9966_dev* cam, int mask, int val);
155 static inline int w9966_getState(struct w9966_dev* cam, int mask, int val);
156 static inline void w9966_pdev_claim(struct w9966_dev *vdev);
157 static inline void w9966_pdev_release(struct w9966_dev *vdev);
159 static int w9966_rReg(struct w9966_dev* cam, int reg);
160 static int w9966_wReg(struct w9966_dev* cam, int reg, int data);
161 #if 0
162 static int w9966_rReg_i2c(struct w9966_dev* cam, int reg);
163 #endif
164 static int w9966_wReg_i2c(struct w9966_dev* cam, int reg, int data);
165 static int w9966_findlen(int near, int size, int maxlen);
166 static int w9966_calcscale(int size, int min, int max, int* beg, int* end, unsigned char* factor);
167 static int w9966_setup(struct w9966_dev* cam, int x1, int y1, int x2, int y2, int w, int h);
169 static int w9966_init(struct w9966_dev* cam, struct parport* port);
170 static void w9966_term(struct w9966_dev* cam);
172 static inline void w9966_i2c_setsda(struct w9966_dev* cam, int state);
173 static inline int w9966_i2c_setscl(struct w9966_dev* cam, int state);
174 static inline int w9966_i2c_getsda(struct w9966_dev* cam);
175 static inline int w9966_i2c_getscl(struct w9966_dev* cam);
176 static int w9966_i2c_wbyte(struct w9966_dev* cam, int data);
177 #if 0
178 static int w9966_i2c_rbyte(struct w9966_dev* cam);
179 #endif
181 static int w9966_v4l_ioctl(struct inode *inode, struct file *file,
182 unsigned int cmd, unsigned long arg);
183 static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
184 size_t count, loff_t *ppos);
186 static const struct file_operations w9966_fops = {
187 .owner = THIS_MODULE,
188 .open = video_exclusive_open,
189 .release = video_exclusive_release,
190 .ioctl = w9966_v4l_ioctl,
191 .compat_ioctl = v4l_compat_ioctl32,
192 .read = w9966_v4l_read,
193 .llseek = no_llseek,
195 static struct video_device w9966_template = {
196 .owner = THIS_MODULE,
197 .name = W9966_DRIVERNAME,
198 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
199 .fops = &w9966_fops,
203 * Private function defines
207 // Set camera phase flags, so we know what to uninit when terminating
208 static inline void w9966_setState(struct w9966_dev* cam, int mask, int val)
210 cam->dev_state = (cam->dev_state & ~mask) ^ val;
213 // Get camera phase flags
214 static inline int w9966_getState(struct w9966_dev* cam, int mask, int val)
216 return ((cam->dev_state & mask) == val);
219 // Claim parport for ourself
220 static inline void w9966_pdev_claim(struct w9966_dev* cam)
222 if (w9966_getState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED))
223 return;
224 parport_claim_or_block(cam->pdev);
225 w9966_setState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED);
228 // Release parport for others to use
229 static inline void w9966_pdev_release(struct w9966_dev* cam)
231 if (w9966_getState(cam, W9966_STATE_CLAIMED, 0))
232 return;
233 parport_release(cam->pdev);
234 w9966_setState(cam, W9966_STATE_CLAIMED, 0);
237 // Read register from W9966 interface-chip
238 // Expects a claimed pdev
239 // -1 on error, else register data (byte)
240 static int w9966_rReg(struct w9966_dev* cam, int reg)
242 // ECP, read, regtransfer, REG, REG, REG, REG, REG
243 const unsigned char addr = 0x80 | (reg & 0x1f);
244 unsigned char val;
246 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
247 return -1;
248 if (parport_write(cam->pport, &addr, 1) != 1)
249 return -1;
250 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
251 return -1;
252 if (parport_read(cam->pport, &val, 1) != 1)
253 return -1;
255 return val;
258 // Write register to W9966 interface-chip
259 // Expects a claimed pdev
260 // -1 on error
261 static int w9966_wReg(struct w9966_dev* cam, int reg, int data)
263 // ECP, write, regtransfer, REG, REG, REG, REG, REG
264 const unsigned char addr = 0xc0 | (reg & 0x1f);
265 const unsigned char val = data;
267 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
268 return -1;
269 if (parport_write(cam->pport, &addr, 1) != 1)
270 return -1;
271 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
272 return -1;
273 if (parport_write(cam->pport, &val, 1) != 1)
274 return -1;
276 return 0;
279 // Initialize camera device. Setup all internal flags, set a
280 // default video mode, setup ccd-chip, register v4l device etc..
281 // Also used for 'probing' of hardware.
282 // -1 on error
283 static int w9966_init(struct w9966_dev* cam, struct parport* port)
285 if (cam->dev_state != 0)
286 return -1;
288 cam->pport = port;
289 cam->brightness = 128;
290 cam->contrast = 64;
291 cam->color = 64;
292 cam->hue = 0;
294 // Select requested transfer mode
295 switch(parmode)
297 default: // Auto-detect (priority: hw-ecp, hw-epp, sw-ecp)
298 case 0:
299 if (port->modes & PARPORT_MODE_ECP)
300 cam->ppmode = IEEE1284_MODE_ECP;
301 else if (port->modes & PARPORT_MODE_EPP)
302 cam->ppmode = IEEE1284_MODE_EPP;
303 else
304 cam->ppmode = IEEE1284_MODE_ECP;
305 break;
306 case 1: // hw- or sw-ecp
307 cam->ppmode = IEEE1284_MODE_ECP;
308 break;
309 case 2: // hw- or sw-epp
310 cam->ppmode = IEEE1284_MODE_EPP;
311 break;
314 // Tell the parport driver that we exists
315 cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
316 if (cam->pdev == NULL) {
317 DPRINTF("parport_register_device() failed\n");
318 return -1;
320 w9966_setState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
322 w9966_pdev_claim(cam);
324 // Setup a default capture mode
325 if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
326 DPRINTF("w9966_setup() failed.\n");
327 return -1;
330 w9966_pdev_release(cam);
332 // Fill in the video_device struct and register us to v4l
333 memcpy(&cam->vdev, &w9966_template, sizeof(struct video_device));
334 cam->vdev.priv = cam;
336 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) == -1)
337 return -1;
339 w9966_setState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
341 // All ok
342 printk(
343 "w9966cf: Found and initialized a webcam on %s.\n",
344 cam->pport->name
346 return 0;
350 // Terminate everything gracefully
351 static void w9966_term(struct w9966_dev* cam)
353 // Unregister from v4l
354 if (w9966_getState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
355 video_unregister_device(&cam->vdev);
356 w9966_setState(cam, W9966_STATE_VDEV, 0);
359 // Terminate from IEEE1284 mode and release pdev block
360 if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
361 w9966_pdev_claim(cam);
362 parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
363 w9966_pdev_release(cam);
366 // Unregister from parport
367 if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
368 parport_unregister_device(cam->pdev);
369 w9966_setState(cam, W9966_STATE_PDEV, 0);
374 // Find a good length for capture window (used both for W and H)
375 // A bit ugly but pretty functional. The capture length
376 // have to match the downscale
377 static int w9966_findlen(int near, int size, int maxlen)
379 int bestlen = size;
380 int besterr = abs(near - bestlen);
381 int len;
383 for(len = size+1;len < maxlen;len++)
385 int err;
386 if ( ((64*size) %len) != 0)
387 continue;
389 err = abs(near - len);
391 // Only continue as long as we keep getting better values
392 if (err > besterr)
393 break;
395 besterr = err;
396 bestlen = len;
399 return bestlen;
402 // Modify capture window (if necessary)
403 // and calculate downscaling
404 // Return -1 on error
405 static int w9966_calcscale(int size, int min, int max, int* beg, int* end, unsigned char* factor)
407 int maxlen = max - min;
408 int len = *end - *beg + 1;
409 int newlen = w9966_findlen(len, size, maxlen);
410 int err = newlen - len;
412 // Check for bad format
413 if (newlen > maxlen || newlen < size)
414 return -1;
416 // Set factor (6 bit fixed)
417 *factor = (64*size) / newlen;
418 if (*factor == 64)
419 *factor = 0x00; // downscale is disabled
420 else
421 *factor |= 0x80; // set downscale-enable bit
423 // Modify old beginning and end
424 *beg -= err / 2;
425 *end += err - (err / 2);
427 // Move window if outside borders
428 if (*beg < min) {
429 *end += min - *beg;
430 *beg += min - *beg;
432 if (*end > max) {
433 *beg -= *end - max;
434 *end -= *end - max;
437 return 0;
440 // Setup the cameras capture window etc.
441 // Expects a claimed pdev
442 // return -1 on error
443 static int w9966_setup(struct w9966_dev* cam, int x1, int y1, int x2, int y2, int w, int h)
445 unsigned int i;
446 unsigned int enh_s, enh_e;
447 unsigned char scale_x, scale_y;
448 unsigned char regs[0x1c];
449 unsigned char saa7111_regs[] = {
450 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00,
451 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00,
452 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
453 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0
457 if (w*h*2 > W9966_SRAMSIZE)
459 DPRINTF("capture window exceeds SRAM size!.\n");
460 w = 200; h = 160; // Pick default values
463 w &= ~0x1;
464 if (w < 2) w = 2;
465 if (h < 1) h = 1;
466 if (w > W9966_WND_MAX_W) w = W9966_WND_MAX_W;
467 if (h > W9966_WND_MAX_H) h = W9966_WND_MAX_H;
469 cam->width = w;
470 cam->height = h;
472 enh_s = 0;
473 enh_e = w*h*2;
475 // Modify capture window if necessary and calculate downscaling
476 if (
477 w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 ||
478 w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0
479 ) return -1;
481 DPRINTF(
482 "%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n",
483 w, h, x1, x2, y1, y2, scale_x&~0x80, scale_y&~0x80
486 // Setup registers
487 regs[0x00] = 0x00; // Set normal operation
488 regs[0x01] = 0x18; // Capture mode
489 regs[0x02] = scale_y; // V-scaling
490 regs[0x03] = scale_x; // H-scaling
492 // Capture window
493 regs[0x04] = (x1 & 0x0ff); // X-start (8 low bits)
494 regs[0x05] = (x1 & 0x300)>>8; // X-start (2 high bits)
495 regs[0x06] = (y1 & 0x0ff); // Y-start (8 low bits)
496 regs[0x07] = (y1 & 0x300)>>8; // Y-start (2 high bits)
497 regs[0x08] = (x2 & 0x0ff); // X-end (8 low bits)
498 regs[0x09] = (x2 & 0x300)>>8; // X-end (2 high bits)
499 regs[0x0a] = (y2 & 0x0ff); // Y-end (8 low bits)
501 regs[0x0c] = W9966_SRAMID; // SRAM-banks (1x 128kb)
503 // Enhancement layer
504 regs[0x0d] = (enh_s& 0x000ff); // Enh. start (0-7)
505 regs[0x0e] = (enh_s& 0x0ff00)>>8; // Enh. start (8-15)
506 regs[0x0f] = (enh_s& 0x70000)>>16; // Enh. start (16-17/18??)
507 regs[0x10] = (enh_e& 0x000ff); // Enh. end (0-7)
508 regs[0x11] = (enh_e& 0x0ff00)>>8; // Enh. end (8-15)
509 regs[0x12] = (enh_e& 0x70000)>>16; // Enh. end (16-17/18??)
511 // Misc
512 regs[0x13] = 0x40; // VEE control (raw 4:2:2)
513 regs[0x17] = 0x00; // ???
514 regs[0x18] = cam->i2c_state = 0x00; // Serial bus
515 regs[0x19] = 0xff; // I/O port direction control
516 regs[0x1a] = 0xff; // I/O port data register
517 regs[0x1b] = 0x10; // ???
519 // SAA7111 chip settings
520 saa7111_regs[0x0a] = cam->brightness;
521 saa7111_regs[0x0b] = cam->contrast;
522 saa7111_regs[0x0c] = cam->color;
523 saa7111_regs[0x0d] = cam->hue;
525 // Reset (ECP-fifo & serial-bus)
526 if (w9966_wReg(cam, 0x00, 0x03) == -1)
527 return -1;
529 // Write regs to w9966cf chip
530 for (i = 0; i < 0x1c; i++)
531 if (w9966_wReg(cam, i, regs[i]) == -1)
532 return -1;
534 // Write regs to saa7111 chip
535 for (i = 0; i < 0x20; i++)
536 if (w9966_wReg_i2c(cam, i, saa7111_regs[i]) == -1)
537 return -1;
539 return 0;
543 * Ugly and primitive i2c protocol functions
546 // Sets the data line on the i2c bus.
547 // Expects a claimed pdev.
548 static inline void w9966_i2c_setsda(struct w9966_dev* cam, int state)
550 if (state)
551 cam->i2c_state |= W9966_I2C_W_DATA;
552 else
553 cam->i2c_state &= ~W9966_I2C_W_DATA;
555 w9966_wReg(cam, 0x18, cam->i2c_state);
556 udelay(5);
559 // Get peripheral clock line
560 // Expects a claimed pdev.
561 static inline int w9966_i2c_getscl(struct w9966_dev* cam)
563 const unsigned char state = w9966_rReg(cam, 0x18);
564 return ((state & W9966_I2C_R_CLOCK) > 0);
567 // Sets the clock line on the i2c bus.
568 // Expects a claimed pdev. -1 on error
569 static inline int w9966_i2c_setscl(struct w9966_dev* cam, int state)
571 unsigned long timeout;
573 if (state)
574 cam->i2c_state |= W9966_I2C_W_CLOCK;
575 else
576 cam->i2c_state &= ~W9966_I2C_W_CLOCK;
578 w9966_wReg(cam, 0x18, cam->i2c_state);
579 udelay(5);
581 // we go to high, we also expect the peripheral to ack.
582 if (state) {
583 timeout = jiffies + 100;
584 while (!w9966_i2c_getscl(cam)) {
585 if (time_after(jiffies, timeout))
586 return -1;
589 return 0;
592 // Get peripheral data line
593 // Expects a claimed pdev.
594 static inline int w9966_i2c_getsda(struct w9966_dev* cam)
596 const unsigned char state = w9966_rReg(cam, 0x18);
597 return ((state & W9966_I2C_R_DATA) > 0);
600 // Write a byte with ack to the i2c bus.
601 // Expects a claimed pdev. -1 on error
602 static int w9966_i2c_wbyte(struct w9966_dev* cam, int data)
604 int i;
605 for (i = 7; i >= 0; i--)
607 w9966_i2c_setsda(cam, (data >> i) & 0x01);
609 if (w9966_i2c_setscl(cam, 1) == -1)
610 return -1;
611 w9966_i2c_setscl(cam, 0);
614 w9966_i2c_setsda(cam, 1);
616 if (w9966_i2c_setscl(cam, 1) == -1)
617 return -1;
618 w9966_i2c_setscl(cam, 0);
620 return 0;
623 // Read a data byte with ack from the i2c-bus
624 // Expects a claimed pdev. -1 on error
625 #if 0
626 static int w9966_i2c_rbyte(struct w9966_dev* cam)
628 unsigned char data = 0x00;
629 int i;
631 w9966_i2c_setsda(cam, 1);
633 for (i = 0; i < 8; i++)
635 if (w9966_i2c_setscl(cam, 1) == -1)
636 return -1;
637 data = data << 1;
638 if (w9966_i2c_getsda(cam))
639 data |= 0x01;
641 w9966_i2c_setscl(cam, 0);
643 return data;
645 #endif
647 // Read a register from the i2c device.
648 // Expects claimed pdev. -1 on error
649 #if 0
650 static int w9966_rReg_i2c(struct w9966_dev* cam, int reg)
652 int data;
654 w9966_i2c_setsda(cam, 0);
655 w9966_i2c_setscl(cam, 0);
657 if (
658 w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
659 w9966_i2c_wbyte(cam, reg) == -1
661 return -1;
663 w9966_i2c_setsda(cam, 1);
664 if (w9966_i2c_setscl(cam, 1) == -1)
665 return -1;
666 w9966_i2c_setsda(cam, 0);
667 w9966_i2c_setscl(cam, 0);
669 if (
670 w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1 ||
671 (data = w9966_i2c_rbyte(cam)) == -1
673 return -1;
675 w9966_i2c_setsda(cam, 0);
677 if (w9966_i2c_setscl(cam, 1) == -1)
678 return -1;
679 w9966_i2c_setsda(cam, 1);
681 return data;
683 #endif
685 // Write a register to the i2c device.
686 // Expects claimed pdev. -1 on error
687 static int w9966_wReg_i2c(struct w9966_dev* cam, int reg, int data)
689 w9966_i2c_setsda(cam, 0);
690 w9966_i2c_setscl(cam, 0);
692 if (
693 w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
694 w9966_i2c_wbyte(cam, reg) == -1 ||
695 w9966_i2c_wbyte(cam, data) == -1
697 return -1;
699 w9966_i2c_setsda(cam, 0);
700 if (w9966_i2c_setscl(cam, 1) == -1)
701 return -1;
703 w9966_i2c_setsda(cam, 1);
705 return 0;
709 * Video4linux interfacing
712 static int w9966_v4l_do_ioctl(struct inode *inode, struct file *file,
713 unsigned int cmd, void *arg)
715 struct video_device *vdev = video_devdata(file);
716 struct w9966_dev *cam = vdev->priv;
718 switch(cmd)
720 case VIDIOCGCAP:
722 static struct video_capability vcap = {
723 .name = W9966_DRIVERNAME,
724 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
725 .channels = 1,
726 .maxwidth = W9966_WND_MAX_W,
727 .maxheight = W9966_WND_MAX_H,
728 .minwidth = 2,
729 .minheight = 1,
731 struct video_capability *cap = arg;
732 *cap = vcap;
733 return 0;
735 case VIDIOCGCHAN:
737 struct video_channel *vch = arg;
738 if(vch->channel != 0) // We only support one channel (#0)
739 return -EINVAL;
740 memset(vch,0,sizeof(*vch));
741 strcpy(vch->name, "CCD-input");
742 vch->type = VIDEO_TYPE_CAMERA;
743 return 0;
745 case VIDIOCSCHAN:
747 struct video_channel *vch = arg;
748 if(vch->channel != 0)
749 return -EINVAL;
750 return 0;
752 case VIDIOCGTUNER:
754 struct video_tuner *vtune = arg;
755 if(vtune->tuner != 0)
756 return -EINVAL;
757 strcpy(vtune->name, "no tuner");
758 vtune->rangelow = 0;
759 vtune->rangehigh = 0;
760 vtune->flags = VIDEO_TUNER_NORM;
761 vtune->mode = VIDEO_MODE_AUTO;
762 vtune->signal = 0xffff;
763 return 0;
765 case VIDIOCSTUNER:
767 struct video_tuner *vtune = arg;
768 if (vtune->tuner != 0)
769 return -EINVAL;
770 if (vtune->mode != VIDEO_MODE_AUTO)
771 return -EINVAL;
772 return 0;
774 case VIDIOCGPICT:
776 struct video_picture vpic = {
777 cam->brightness << 8, // brightness
778 (cam->hue + 128) << 8, // hue
779 cam->color << 9, // color
780 cam->contrast << 9, // contrast
781 0x8000, // whiteness
782 16, VIDEO_PALETTE_YUV422// bpp, palette format
784 struct video_picture *pic = arg;
785 *pic = vpic;
786 return 0;
788 case VIDIOCSPICT:
790 struct video_picture *vpic = arg;
791 if (vpic->depth != 16 || (vpic->palette != VIDEO_PALETTE_YUV422 && vpic->palette != VIDEO_PALETTE_YUYV))
792 return -EINVAL;
794 cam->brightness = vpic->brightness >> 8;
795 cam->hue = (vpic->hue >> 8) - 128;
796 cam->color = vpic->colour >> 9;
797 cam->contrast = vpic->contrast >> 9;
799 w9966_pdev_claim(cam);
801 if (
802 w9966_wReg_i2c(cam, 0x0a, cam->brightness) == -1 ||
803 w9966_wReg_i2c(cam, 0x0b, cam->contrast) == -1 ||
804 w9966_wReg_i2c(cam, 0x0c, cam->color) == -1 ||
805 w9966_wReg_i2c(cam, 0x0d, cam->hue) == -1
807 w9966_pdev_release(cam);
808 return -EIO;
811 w9966_pdev_release(cam);
812 return 0;
814 case VIDIOCSWIN:
816 int ret;
817 struct video_window *vwin = arg;
819 if (vwin->flags != 0)
820 return -EINVAL;
821 if (vwin->clipcount != 0)
822 return -EINVAL;
823 if (vwin->width < 2 || vwin->width > W9966_WND_MAX_W)
824 return -EINVAL;
825 if (vwin->height < 1 || vwin->height > W9966_WND_MAX_H)
826 return -EINVAL;
828 // Update camera regs
829 w9966_pdev_claim(cam);
830 ret = w9966_setup(cam, 0, 0, 1023, 1023, vwin->width, vwin->height);
831 w9966_pdev_release(cam);
833 if (ret != 0) {
834 DPRINTF("VIDIOCSWIN: w9966_setup() failed.\n");
835 return -EIO;
838 return 0;
840 case VIDIOCGWIN:
842 struct video_window *vwin = arg;
843 memset(vwin, 0, sizeof(*vwin));
844 vwin->width = cam->width;
845 vwin->height = cam->height;
846 return 0;
848 // Unimplemented
849 case VIDIOCCAPTURE:
850 case VIDIOCGFBUF:
851 case VIDIOCSFBUF:
852 case VIDIOCKEY:
853 case VIDIOCGFREQ:
854 case VIDIOCSFREQ:
855 case VIDIOCGAUDIO:
856 case VIDIOCSAUDIO:
857 return -EINVAL;
858 default:
859 return -ENOIOCTLCMD;
861 return 0;
864 static int w9966_v4l_ioctl(struct inode *inode, struct file *file,
865 unsigned int cmd, unsigned long arg)
867 return video_usercopy(inode, file, cmd, arg, w9966_v4l_do_ioctl);
870 // Capture data
871 static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
872 size_t count, loff_t *ppos)
874 struct video_device *vdev = video_devdata(file);
875 struct w9966_dev *cam = vdev->priv;
876 unsigned char addr = 0xa0; // ECP, read, CCD-transfer, 00000
877 unsigned char __user *dest = (unsigned char __user *)buf;
878 unsigned long dleft = count;
879 unsigned char *tbuf;
881 // Why would anyone want more than this??
882 if (count > cam->width * cam->height * 2)
883 return -EINVAL;
885 w9966_pdev_claim(cam);
886 w9966_wReg(cam, 0x00, 0x02); // Reset ECP-FIFO buffer
887 w9966_wReg(cam, 0x00, 0x00); // Return to normal operation
888 w9966_wReg(cam, 0x01, 0x98); // Enable capture
890 // write special capture-addr and negotiate into data transfer
891 if (
892 (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0 )||
893 (parport_write(cam->pport, &addr, 1) != 1 )||
894 (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0 )
896 w9966_pdev_release(cam);
897 return -EFAULT;
900 tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
901 if (tbuf == NULL) {
902 count = -ENOMEM;
903 goto out;
906 while(dleft > 0)
908 unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
910 if (parport_read(cam->pport, tbuf, tsize) < tsize) {
911 count = -EFAULT;
912 goto out;
914 if (copy_to_user(dest, tbuf, tsize) != 0) {
915 count = -EFAULT;
916 goto out;
918 dest += tsize;
919 dleft -= tsize;
922 w9966_wReg(cam, 0x01, 0x18); // Disable capture
924 out:
925 kfree(tbuf);
926 w9966_pdev_release(cam);
928 return count;
932 // Called once for every parport on init
933 static void w9966_attach(struct parport *port)
935 int i;
937 for (i = 0; i < W9966_MAXCAMS; i++)
939 if (w9966_cams[i].dev_state != 0) // Cam is already assigned
940 continue;
941 if (
942 strcmp(pardev[i], "aggressive") == 0 ||
943 strcmp(pardev[i], port->name) == 0
945 if (w9966_init(&w9966_cams[i], port) != 0)
946 w9966_term(&w9966_cams[i]);
947 break; // return
952 // Called once for every parport on termination
953 static void w9966_detach(struct parport *port)
955 int i;
956 for (i = 0; i < W9966_MAXCAMS; i++)
957 if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
958 w9966_term(&w9966_cams[i]);
962 static struct parport_driver w9966_ppd = {
963 .name = W9966_DRIVERNAME,
964 .attach = w9966_attach,
965 .detach = w9966_detach,
968 // Module entry point
969 static int __init w9966_mod_init(void)
971 int i;
972 for (i = 0; i < W9966_MAXCAMS; i++)
973 w9966_cams[i].dev_state = 0;
975 return parport_register_driver(&w9966_ppd);
978 // Module cleanup
979 static void __exit w9966_mod_term(void)
981 parport_unregister_driver(&w9966_ppd);
984 module_init(w9966_mod_init);
985 module_exit(w9966_mod_term);