PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / parport / w9966.c
blobdb2a6003a1c3990e032f9fd2a5a9ff40bbb595ba
1 /*
2 Winbond w9966cf Webcam parport driver.
4 Version 0.33
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/videodev2.h>
61 #include <linux/slab.h>
62 #include <media/v4l2-common.h>
63 #include <media/v4l2-ioctl.h>
64 #include <media/v4l2-device.h>
65 #include <media/v4l2-fh.h>
66 #include <media/v4l2-ctrls.h>
67 #include <media/v4l2-event.h>
68 #include <linux/parport.h>
70 /*#define DEBUG*/ /* Undef me for production */
72 #ifdef DEBUG
73 #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
74 #else
75 #define DPRINTF(x...)
76 #endif
79 * Defines, simple typedefs etc.
82 #define W9966_DRIVERNAME "W9966CF Webcam"
83 #define W9966_MAXCAMS 4 /* Maximum number of cameras */
84 #define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */
85 #define W9966_SRAMSIZE 131072 /* 128kb */
86 #define W9966_SRAMID 0x02 /* check w9966cf.pdf */
88 /* Empirically determined window limits */
89 #define W9966_WND_MIN_X 16
90 #define W9966_WND_MIN_Y 14
91 #define W9966_WND_MAX_X 705
92 #define W9966_WND_MAX_Y 253
93 #define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
94 #define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
96 /* Keep track of our current state */
97 #define W9966_STATE_PDEV 0x01
98 #define W9966_STATE_CLAIMED 0x02
99 #define W9966_STATE_VDEV 0x04
101 #define W9966_I2C_W_ID 0x48
102 #define W9966_I2C_R_ID 0x49
103 #define W9966_I2C_R_DATA 0x08
104 #define W9966_I2C_R_CLOCK 0x04
105 #define W9966_I2C_W_DATA 0x02
106 #define W9966_I2C_W_CLOCK 0x01
108 struct w9966 {
109 struct v4l2_device v4l2_dev;
110 struct v4l2_ctrl_handler hdl;
111 unsigned char dev_state;
112 unsigned char i2c_state;
113 unsigned short ppmode;
114 struct parport *pport;
115 struct pardevice *pdev;
116 struct video_device vdev;
117 unsigned short width;
118 unsigned short height;
119 unsigned char brightness;
120 signed char contrast;
121 signed char color;
122 signed char hue;
123 struct mutex lock;
127 * Module specific properties
130 MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
131 MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
132 MODULE_LICENSE("GPL");
133 MODULE_VERSION("0.33.1");
135 #ifdef MODULE
136 static char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
137 #else
138 static char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
139 #endif
140 module_param_array(pardev, charp, NULL, 0);
141 MODULE_PARM_DESC(pardev, "pardev: where to search for\n"
142 "\teach camera. 'aggressive' means brute-force search.\n"
143 "\tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n"
144 "\tcam 1 to parport3 and search every parport for cam 2 etc...");
146 static int parmode;
147 module_param(parmode, int, 0);
148 MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp");
150 static int video_nr = -1;
151 module_param(video_nr, int, 0);
153 static struct w9966 w9966_cams[W9966_MAXCAMS];
156 * Private function defines
160 /* Set camera phase flags, so we know what to uninit when terminating */
161 static inline void w9966_set_state(struct w9966 *cam, int mask, int val)
163 cam->dev_state = (cam->dev_state & ~mask) ^ val;
166 /* Get camera phase flags */
167 static inline int w9966_get_state(struct w9966 *cam, int mask, int val)
169 return ((cam->dev_state & mask) == val);
172 /* Claim parport for ourself */
173 static void w9966_pdev_claim(struct w9966 *cam)
175 if (w9966_get_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED))
176 return;
177 parport_claim_or_block(cam->pdev);
178 w9966_set_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED);
181 /* Release parport for others to use */
182 static void w9966_pdev_release(struct w9966 *cam)
184 if (w9966_get_state(cam, W9966_STATE_CLAIMED, 0))
185 return;
186 parport_release(cam->pdev);
187 w9966_set_state(cam, W9966_STATE_CLAIMED, 0);
190 /* Read register from W9966 interface-chip
191 Expects a claimed pdev
192 -1 on error, else register data (byte) */
193 static int w9966_read_reg(struct w9966 *cam, int reg)
195 /* ECP, read, regtransfer, REG, REG, REG, REG, REG */
196 const unsigned char addr = 0x80 | (reg & 0x1f);
197 unsigned char val;
199 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
200 return -1;
201 if (parport_write(cam->pport, &addr, 1) != 1)
202 return -1;
203 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
204 return -1;
205 if (parport_read(cam->pport, &val, 1) != 1)
206 return -1;
208 return val;
211 /* Write register to W9966 interface-chip
212 Expects a claimed pdev
213 -1 on error */
214 static int w9966_write_reg(struct w9966 *cam, int reg, int data)
216 /* ECP, write, regtransfer, REG, REG, REG, REG, REG */
217 const unsigned char addr = 0xc0 | (reg & 0x1f);
218 const unsigned char val = data;
220 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
221 return -1;
222 if (parport_write(cam->pport, &addr, 1) != 1)
223 return -1;
224 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
225 return -1;
226 if (parport_write(cam->pport, &val, 1) != 1)
227 return -1;
229 return 0;
233 * Ugly and primitive i2c protocol functions
236 /* Sets the data line on the i2c bus.
237 Expects a claimed pdev. */
238 static void w9966_i2c_setsda(struct w9966 *cam, int state)
240 if (state)
241 cam->i2c_state |= W9966_I2C_W_DATA;
242 else
243 cam->i2c_state &= ~W9966_I2C_W_DATA;
245 w9966_write_reg(cam, 0x18, cam->i2c_state);
246 udelay(5);
249 /* Get peripheral clock line
250 Expects a claimed pdev. */
251 static int w9966_i2c_getscl(struct w9966 *cam)
253 const unsigned char state = w9966_read_reg(cam, 0x18);
254 return ((state & W9966_I2C_R_CLOCK) > 0);
257 /* Sets the clock line on the i2c bus.
258 Expects a claimed pdev. -1 on error */
259 static int w9966_i2c_setscl(struct w9966 *cam, int state)
261 unsigned long timeout;
263 if (state)
264 cam->i2c_state |= W9966_I2C_W_CLOCK;
265 else
266 cam->i2c_state &= ~W9966_I2C_W_CLOCK;
268 w9966_write_reg(cam, 0x18, cam->i2c_state);
269 udelay(5);
271 /* we go to high, we also expect the peripheral to ack. */
272 if (state) {
273 timeout = jiffies + 100;
274 while (!w9966_i2c_getscl(cam)) {
275 if (time_after(jiffies, timeout))
276 return -1;
279 return 0;
282 #if 0
283 /* Get peripheral data line
284 Expects a claimed pdev. */
285 static int w9966_i2c_getsda(struct w9966 *cam)
287 const unsigned char state = w9966_read_reg(cam, 0x18);
288 return ((state & W9966_I2C_R_DATA) > 0);
290 #endif
292 /* Write a byte with ack to the i2c bus.
293 Expects a claimed pdev. -1 on error */
294 static int w9966_i2c_wbyte(struct w9966 *cam, int data)
296 int i;
298 for (i = 7; i >= 0; i--) {
299 w9966_i2c_setsda(cam, (data >> i) & 0x01);
301 if (w9966_i2c_setscl(cam, 1) == -1)
302 return -1;
303 w9966_i2c_setscl(cam, 0);
306 w9966_i2c_setsda(cam, 1);
308 if (w9966_i2c_setscl(cam, 1) == -1)
309 return -1;
310 w9966_i2c_setscl(cam, 0);
312 return 0;
315 /* Read a data byte with ack from the i2c-bus
316 Expects a claimed pdev. -1 on error */
317 #if 0
318 static int w9966_i2c_rbyte(struct w9966 *cam)
320 unsigned char data = 0x00;
321 int i;
323 w9966_i2c_setsda(cam, 1);
325 for (i = 0; i < 8; i++) {
326 if (w9966_i2c_setscl(cam, 1) == -1)
327 return -1;
328 data = data << 1;
329 if (w9966_i2c_getsda(cam))
330 data |= 0x01;
332 w9966_i2c_setscl(cam, 0);
334 return data;
336 #endif
338 /* Read a register from the i2c device.
339 Expects claimed pdev. -1 on error */
340 #if 0
341 static int w9966_read_reg_i2c(struct w9966 *cam, int reg)
343 int data;
345 w9966_i2c_setsda(cam, 0);
346 w9966_i2c_setscl(cam, 0);
348 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
349 w9966_i2c_wbyte(cam, reg) == -1)
350 return -1;
352 w9966_i2c_setsda(cam, 1);
353 if (w9966_i2c_setscl(cam, 1) == -1)
354 return -1;
355 w9966_i2c_setsda(cam, 0);
356 w9966_i2c_setscl(cam, 0);
358 if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1)
359 return -1;
360 data = w9966_i2c_rbyte(cam);
361 if (data == -1)
362 return -1;
364 w9966_i2c_setsda(cam, 0);
366 if (w9966_i2c_setscl(cam, 1) == -1)
367 return -1;
368 w9966_i2c_setsda(cam, 1);
370 return data;
372 #endif
374 /* Write a register to the i2c device.
375 Expects claimed pdev. -1 on error */
376 static int w9966_write_reg_i2c(struct w9966 *cam, int reg, int data)
378 w9966_i2c_setsda(cam, 0);
379 w9966_i2c_setscl(cam, 0);
381 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
382 w9966_i2c_wbyte(cam, reg) == -1 ||
383 w9966_i2c_wbyte(cam, data) == -1)
384 return -1;
386 w9966_i2c_setsda(cam, 0);
387 if (w9966_i2c_setscl(cam, 1) == -1)
388 return -1;
390 w9966_i2c_setsda(cam, 1);
392 return 0;
395 /* Find a good length for capture window (used both for W and H)
396 A bit ugly but pretty functional. The capture length
397 have to match the downscale */
398 static int w9966_findlen(int near, int size, int maxlen)
400 int bestlen = size;
401 int besterr = abs(near - bestlen);
402 int len;
404 for (len = size + 1; len < maxlen; len++) {
405 int err;
406 if (((64 * size) % len) != 0)
407 continue;
409 err = abs(near - len);
411 /* Only continue as long as we keep getting better values */
412 if (err > besterr)
413 break;
415 besterr = err;
416 bestlen = len;
419 return bestlen;
422 /* Modify capture window (if necessary)
423 and calculate downscaling
424 Return -1 on error */
425 static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor)
427 int maxlen = max - min;
428 int len = *end - *beg + 1;
429 int newlen = w9966_findlen(len, size, maxlen);
430 int err = newlen - len;
432 /* Check for bad format */
433 if (newlen > maxlen || newlen < size)
434 return -1;
436 /* Set factor (6 bit fixed) */
437 *factor = (64 * size) / newlen;
438 if (*factor == 64)
439 *factor = 0x00; /* downscale is disabled */
440 else
441 *factor |= 0x80; /* set downscale-enable bit */
443 /* Modify old beginning and end */
444 *beg -= err / 2;
445 *end += err - (err / 2);
447 /* Move window if outside borders */
448 if (*beg < min) {
449 *end += min - *beg;
450 *beg += min - *beg;
452 if (*end > max) {
453 *beg -= *end - max;
454 *end -= *end - max;
457 return 0;
460 /* Setup the cameras capture window etc.
461 Expects a claimed pdev
462 return -1 on error */
463 static int w9966_setup(struct w9966 *cam, int x1, int y1, int x2, int y2, int w, int h)
465 unsigned int i;
466 unsigned int enh_s, enh_e;
467 unsigned char scale_x, scale_y;
468 unsigned char regs[0x1c];
469 unsigned char saa7111_regs[] = {
470 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00,
471 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00,
472 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0
477 if (w * h * 2 > W9966_SRAMSIZE) {
478 DPRINTF("capture window exceeds SRAM size!.\n");
479 w = 200; h = 160; /* Pick default values */
482 w &= ~0x1;
483 if (w < 2)
484 w = 2;
485 if (h < 1)
486 h = 1;
487 if (w > W9966_WND_MAX_W)
488 w = W9966_WND_MAX_W;
489 if (h > W9966_WND_MAX_H)
490 h = W9966_WND_MAX_H;
492 cam->width = w;
493 cam->height = h;
495 enh_s = 0;
496 enh_e = w * h * 2;
498 /* Modify capture window if necessary and calculate downscaling */
499 if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 ||
500 w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0)
501 return -1;
503 DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n",
504 w, h, x1, x2, y1, y2, scale_x & ~0x80, scale_y & ~0x80);
506 /* Setup registers */
507 regs[0x00] = 0x00; /* Set normal operation */
508 regs[0x01] = 0x18; /* Capture mode */
509 regs[0x02] = scale_y; /* V-scaling */
510 regs[0x03] = scale_x; /* H-scaling */
512 /* Capture window */
513 regs[0x04] = (x1 & 0x0ff); /* X-start (8 low bits) */
514 regs[0x05] = (x1 & 0x300)>>8; /* X-start (2 high bits) */
515 regs[0x06] = (y1 & 0x0ff); /* Y-start (8 low bits) */
516 regs[0x07] = (y1 & 0x300)>>8; /* Y-start (2 high bits) */
517 regs[0x08] = (x2 & 0x0ff); /* X-end (8 low bits) */
518 regs[0x09] = (x2 & 0x300)>>8; /* X-end (2 high bits) */
519 regs[0x0a] = (y2 & 0x0ff); /* Y-end (8 low bits) */
521 regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */
523 /* Enhancement layer */
524 regs[0x0d] = (enh_s & 0x000ff); /* Enh. start (0-7) */
525 regs[0x0e] = (enh_s & 0x0ff00) >> 8; /* Enh. start (8-15) */
526 regs[0x0f] = (enh_s & 0x70000) >> 16; /* Enh. start (16-17/18??) */
527 regs[0x10] = (enh_e & 0x000ff); /* Enh. end (0-7) */
528 regs[0x11] = (enh_e & 0x0ff00) >> 8; /* Enh. end (8-15) */
529 regs[0x12] = (enh_e & 0x70000) >> 16; /* Enh. end (16-17/18??) */
531 /* Misc */
532 regs[0x13] = 0x40; /* VEE control (raw 4:2:2) */
533 regs[0x17] = 0x00; /* ??? */
534 regs[0x18] = cam->i2c_state = 0x00; /* Serial bus */
535 regs[0x19] = 0xff; /* I/O port direction control */
536 regs[0x1a] = 0xff; /* I/O port data register */
537 regs[0x1b] = 0x10; /* ??? */
539 /* SAA7111 chip settings */
540 saa7111_regs[0x0a] = cam->brightness;
541 saa7111_regs[0x0b] = cam->contrast;
542 saa7111_regs[0x0c] = cam->color;
543 saa7111_regs[0x0d] = cam->hue;
545 /* Reset (ECP-fifo & serial-bus) */
546 if (w9966_write_reg(cam, 0x00, 0x03) == -1)
547 return -1;
549 /* Write regs to w9966cf chip */
550 for (i = 0; i < 0x1c; i++)
551 if (w9966_write_reg(cam, i, regs[i]) == -1)
552 return -1;
554 /* Write regs to saa7111 chip */
555 for (i = 0; i < 0x20; i++)
556 if (w9966_write_reg_i2c(cam, i, saa7111_regs[i]) == -1)
557 return -1;
559 return 0;
563 * Video4linux interfacing
566 static int cam_querycap(struct file *file, void *priv,
567 struct v4l2_capability *vcap)
569 struct w9966 *cam = video_drvdata(file);
571 strlcpy(vcap->driver, cam->v4l2_dev.name, sizeof(vcap->driver));
572 strlcpy(vcap->card, W9966_DRIVERNAME, sizeof(vcap->card));
573 strlcpy(vcap->bus_info, "parport", sizeof(vcap->bus_info));
574 vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
575 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
576 return 0;
579 static int cam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
581 if (vin->index > 0)
582 return -EINVAL;
583 strlcpy(vin->name, "Camera", sizeof(vin->name));
584 vin->type = V4L2_INPUT_TYPE_CAMERA;
585 vin->audioset = 0;
586 vin->tuner = 0;
587 vin->std = 0;
588 vin->status = 0;
589 return 0;
592 static int cam_g_input(struct file *file, void *fh, unsigned int *inp)
594 *inp = 0;
595 return 0;
598 static int cam_s_input(struct file *file, void *fh, unsigned int inp)
600 return (inp > 0) ? -EINVAL : 0;
603 static int cam_s_ctrl(struct v4l2_ctrl *ctrl)
605 struct w9966 *cam =
606 container_of(ctrl->handler, struct w9966, hdl);
607 int ret = 0;
609 mutex_lock(&cam->lock);
610 switch (ctrl->id) {
611 case V4L2_CID_BRIGHTNESS:
612 cam->brightness = ctrl->val;
613 break;
614 case V4L2_CID_CONTRAST:
615 cam->contrast = ctrl->val;
616 break;
617 case V4L2_CID_SATURATION:
618 cam->color = ctrl->val;
619 break;
620 case V4L2_CID_HUE:
621 cam->hue = ctrl->val;
622 break;
623 default:
624 ret = -EINVAL;
625 break;
628 if (ret == 0) {
629 w9966_pdev_claim(cam);
631 if (w9966_write_reg_i2c(cam, 0x0a, cam->brightness) == -1 ||
632 w9966_write_reg_i2c(cam, 0x0b, cam->contrast) == -1 ||
633 w9966_write_reg_i2c(cam, 0x0c, cam->color) == -1 ||
634 w9966_write_reg_i2c(cam, 0x0d, cam->hue) == -1) {
635 ret = -EIO;
638 w9966_pdev_release(cam);
640 mutex_unlock(&cam->lock);
641 return ret;
644 static int cam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
646 struct w9966 *cam = video_drvdata(file);
647 struct v4l2_pix_format *pix = &fmt->fmt.pix;
649 pix->width = cam->width;
650 pix->height = cam->height;
651 pix->pixelformat = V4L2_PIX_FMT_YUYV;
652 pix->field = V4L2_FIELD_NONE;
653 pix->bytesperline = 2 * cam->width;
654 pix->sizeimage = 2 * cam->width * cam->height;
655 /* Just a guess */
656 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
657 return 0;
660 static int cam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
662 struct v4l2_pix_format *pix = &fmt->fmt.pix;
664 if (pix->width < 2)
665 pix->width = 2;
666 if (pix->height < 1)
667 pix->height = 1;
668 if (pix->width > W9966_WND_MAX_W)
669 pix->width = W9966_WND_MAX_W;
670 if (pix->height > W9966_WND_MAX_H)
671 pix->height = W9966_WND_MAX_H;
672 pix->pixelformat = V4L2_PIX_FMT_YUYV;
673 pix->field = V4L2_FIELD_NONE;
674 pix->bytesperline = 2 * pix->width;
675 pix->sizeimage = 2 * pix->width * pix->height;
676 /* Just a guess */
677 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
678 return 0;
681 static int cam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
683 struct w9966 *cam = video_drvdata(file);
684 struct v4l2_pix_format *pix = &fmt->fmt.pix;
685 int ret = cam_try_fmt_vid_cap(file, fh, fmt);
687 if (ret)
688 return ret;
690 mutex_lock(&cam->lock);
691 /* Update camera regs */
692 w9966_pdev_claim(cam);
693 ret = w9966_setup(cam, 0, 0, 1023, 1023, pix->width, pix->height);
694 w9966_pdev_release(cam);
695 mutex_unlock(&cam->lock);
696 return ret;
699 static int cam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
701 static struct v4l2_fmtdesc formats[] = {
702 { 0, 0, 0,
703 "YUV 4:2:2", V4L2_PIX_FMT_YUYV,
704 { 0, 0, 0, 0 }
707 enum v4l2_buf_type type = fmt->type;
709 if (fmt->index > 0)
710 return -EINVAL;
712 *fmt = formats[fmt->index];
713 fmt->type = type;
714 return 0;
717 /* Capture data */
718 static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
719 size_t count, loff_t *ppos)
721 struct w9966 *cam = video_drvdata(file);
722 unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
723 unsigned char __user *dest = (unsigned char __user *)buf;
724 unsigned long dleft = count;
725 unsigned char *tbuf;
727 /* Why would anyone want more than this?? */
728 if (count > cam->width * cam->height * 2)
729 return -EINVAL;
731 mutex_lock(&cam->lock);
732 w9966_pdev_claim(cam);
733 w9966_write_reg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */
734 w9966_write_reg(cam, 0x00, 0x00); /* Return to normal operation */
735 w9966_write_reg(cam, 0x01, 0x98); /* Enable capture */
737 /* write special capture-addr and negotiate into data transfer */
738 if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) ||
739 (parport_write(cam->pport, &addr, 1) != 1) ||
740 (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) {
741 w9966_pdev_release(cam);
742 mutex_unlock(&cam->lock);
743 return -EFAULT;
746 tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
747 if (tbuf == NULL) {
748 count = -ENOMEM;
749 goto out;
752 while (dleft > 0) {
753 unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
755 if (parport_read(cam->pport, tbuf, tsize) < tsize) {
756 count = -EFAULT;
757 goto out;
759 if (copy_to_user(dest, tbuf, tsize) != 0) {
760 count = -EFAULT;
761 goto out;
763 dest += tsize;
764 dleft -= tsize;
767 w9966_write_reg(cam, 0x01, 0x18); /* Disable capture */
769 out:
770 kfree(tbuf);
771 w9966_pdev_release(cam);
772 mutex_unlock(&cam->lock);
774 return count;
777 static const struct v4l2_file_operations w9966_fops = {
778 .owner = THIS_MODULE,
779 .open = v4l2_fh_open,
780 .release = v4l2_fh_release,
781 .poll = v4l2_ctrl_poll,
782 .unlocked_ioctl = video_ioctl2,
783 .read = w9966_v4l_read,
786 static const struct v4l2_ioctl_ops w9966_ioctl_ops = {
787 .vidioc_querycap = cam_querycap,
788 .vidioc_g_input = cam_g_input,
789 .vidioc_s_input = cam_s_input,
790 .vidioc_enum_input = cam_enum_input,
791 .vidioc_enum_fmt_vid_cap = cam_enum_fmt_vid_cap,
792 .vidioc_g_fmt_vid_cap = cam_g_fmt_vid_cap,
793 .vidioc_s_fmt_vid_cap = cam_s_fmt_vid_cap,
794 .vidioc_try_fmt_vid_cap = cam_try_fmt_vid_cap,
795 .vidioc_log_status = v4l2_ctrl_log_status,
796 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
797 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
800 static const struct v4l2_ctrl_ops cam_ctrl_ops = {
801 .s_ctrl = cam_s_ctrl,
805 /* Initialize camera device. Setup all internal flags, set a
806 default video mode, setup ccd-chip, register v4l device etc..
807 Also used for 'probing' of hardware.
808 -1 on error */
809 static int w9966_init(struct w9966 *cam, struct parport *port)
811 struct v4l2_device *v4l2_dev = &cam->v4l2_dev;
813 if (cam->dev_state != 0)
814 return -1;
816 strlcpy(v4l2_dev->name, "w9966", sizeof(v4l2_dev->name));
818 if (v4l2_device_register(NULL, v4l2_dev) < 0) {
819 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
820 return -1;
823 v4l2_ctrl_handler_init(&cam->hdl, 4);
824 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
825 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
826 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
827 V4L2_CID_CONTRAST, -64, 64, 1, 64);
828 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
829 V4L2_CID_SATURATION, -64, 64, 1, 64);
830 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
831 V4L2_CID_HUE, -128, 127, 1, 0);
832 if (cam->hdl.error) {
833 v4l2_err(v4l2_dev, "couldn't register controls\n");
834 return -1;
836 cam->pport = port;
837 cam->brightness = 128;
838 cam->contrast = 64;
839 cam->color = 64;
840 cam->hue = 0;
842 /* Select requested transfer mode */
843 switch (parmode) {
844 default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
845 case 0:
846 if (port->modes & PARPORT_MODE_ECP)
847 cam->ppmode = IEEE1284_MODE_ECP;
848 else if (port->modes & PARPORT_MODE_EPP)
849 cam->ppmode = IEEE1284_MODE_EPP;
850 else
851 cam->ppmode = IEEE1284_MODE_ECP;
852 break;
853 case 1: /* hw- or sw-ecp */
854 cam->ppmode = IEEE1284_MODE_ECP;
855 break;
856 case 2: /* hw- or sw-epp */
857 cam->ppmode = IEEE1284_MODE_EPP;
858 break;
861 /* Tell the parport driver that we exists */
862 cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
863 if (cam->pdev == NULL) {
864 DPRINTF("parport_register_device() failed\n");
865 return -1;
867 w9966_set_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
869 w9966_pdev_claim(cam);
871 /* Setup a default capture mode */
872 if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
873 DPRINTF("w9966_setup() failed.\n");
874 return -1;
877 w9966_pdev_release(cam);
879 /* Fill in the video_device struct and register us to v4l */
880 strlcpy(cam->vdev.name, W9966_DRIVERNAME, sizeof(cam->vdev.name));
881 cam->vdev.v4l2_dev = v4l2_dev;
882 cam->vdev.fops = &w9966_fops;
883 cam->vdev.ioctl_ops = &w9966_ioctl_ops;
884 cam->vdev.release = video_device_release_empty;
885 cam->vdev.ctrl_handler = &cam->hdl;
886 set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
887 video_set_drvdata(&cam->vdev, cam);
889 mutex_init(&cam->lock);
891 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
892 return -1;
894 w9966_set_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
896 /* All ok */
897 v4l2_info(v4l2_dev, "Found and initialized a webcam on %s.\n",
898 cam->pport->name);
899 return 0;
903 /* Terminate everything gracefully */
904 static void w9966_term(struct w9966 *cam)
906 /* Unregister from v4l */
907 if (w9966_get_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
908 video_unregister_device(&cam->vdev);
909 w9966_set_state(cam, W9966_STATE_VDEV, 0);
912 v4l2_ctrl_handler_free(&cam->hdl);
914 /* Terminate from IEEE1284 mode and release pdev block */
915 if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
916 w9966_pdev_claim(cam);
917 parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
918 w9966_pdev_release(cam);
921 /* Unregister from parport */
922 if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
923 parport_unregister_device(cam->pdev);
924 w9966_set_state(cam, W9966_STATE_PDEV, 0);
926 memset(cam, 0, sizeof(*cam));
930 /* Called once for every parport on init */
931 static void w9966_attach(struct parport *port)
933 int i;
935 for (i = 0; i < W9966_MAXCAMS; i++) {
936 if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
937 continue;
938 if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
939 if (w9966_init(&w9966_cams[i], port) != 0)
940 w9966_term(&w9966_cams[i]);
941 break; /* return */
946 /* Called once for every parport on termination */
947 static void w9966_detach(struct parport *port)
949 int i;
951 for (i = 0; i < W9966_MAXCAMS; i++)
952 if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
953 w9966_term(&w9966_cams[i]);
957 static struct parport_driver w9966_ppd = {
958 .name = W9966_DRIVERNAME,
959 .attach = w9966_attach,
960 .detach = w9966_detach,
963 /* Module entry point */
964 static int __init w9966_mod_init(void)
966 int i;
968 for (i = 0; i < W9966_MAXCAMS; i++)
969 w9966_cams[i].dev_state = 0;
971 return parport_register_driver(&w9966_ppd);
974 /* Module cleanup */
975 static void __exit w9966_mod_term(void)
977 parport_unregister_driver(&w9966_ppd);
980 module_init(w9966_mod_init);
981 module_exit(w9966_mod_term);