GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / bw-qcam.c
blob51d4699bea3a702022167183bb5cf73e08f2e046
1 /*
2 * QuickCam Driver For Video4Linux.
4 * Video4Linux conversion work by Alan Cox.
5 * Parport compatibility by Phil Blundell.
6 * Busy loop avoidance by Mark Cooke.
8 * Module parameters:
10 * maxpoll=<1 - 5000>
12 * When polling the QuickCam for a response, busy-wait for a
13 * maximum of this many loops. The default of 250 gives little
14 * impact on interactive response.
16 * NOTE: If this parameter is set too high, the processor
17 * will busy wait until this loop times out, and then
18 * slowly poll for a further 5 seconds before failing
19 * the transaction. You have been warned.
21 * yieldlines=<1 - 250>
23 * When acquiring a frame from the camera, the data gathering
24 * loop will yield back to the scheduler after completing
25 * this many lines. The default of 4 provides a trade-off
26 * between increased frame acquisition time and impact on
27 * interactive response.
30 /* qcam-lib.c -- Library for programming with the Connectix QuickCam.
31 * See the included documentation for usage instructions and details
32 * of the protocol involved. */
35 /* Version 0.5, August 4, 1996 */
36 /* Version 0.7, August 27, 1996 */
37 /* Version 0.9, November 17, 1996 */
40 /******************************************************************
42 Copyright (C) 1996 by Scott Laird
44 Permission is hereby granted, free of charge, to any person obtaining
45 a copy of this software and associated documentation files (the
46 "Software"), to deal in the Software without restriction, including
47 without limitation the rights to use, copy, modify, merge, publish,
48 distribute, sublicense, and/or sell copies of the Software, and to
49 permit persons to whom the Software is furnished to do so, subject to
50 the following conditions:
52 The above copyright notice and this permission notice shall be
53 included in all copies or substantial portions of the Software.
55 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58 IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY CLAIM, DAMAGES OR
59 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
60 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
61 OTHER DEALINGS IN THE SOFTWARE.
63 ******************************************************************/
65 #include <linux/module.h>
66 #include <linux/delay.h>
67 #include <linux/errno.h>
68 #include <linux/fs.h>
69 #include <linux/kernel.h>
70 #include <linux/slab.h>
71 #include <linux/mm.h>
72 #include <linux/parport.h>
73 #include <linux/sched.h>
74 #include <linux/version.h>
75 #include <linux/videodev2.h>
76 #include <linux/mutex.h>
77 #include <asm/uaccess.h>
78 #include <media/v4l2-common.h>
79 #include <media/v4l2-ioctl.h>
80 #include <media/v4l2-device.h>
82 /* One from column A... */
83 #define QC_NOTSET 0
84 #define QC_UNIDIR 1
85 #define QC_BIDIR 2
86 #define QC_SERIAL 3
88 /* ... and one from column B */
89 #define QC_ANY 0x00
90 #define QC_FORCE_UNIDIR 0x10
91 #define QC_FORCE_BIDIR 0x20
92 #define QC_FORCE_SERIAL 0x30
93 /* in the port_mode member */
95 #define QC_MODE_MASK 0x07
96 #define QC_FORCE_MASK 0x70
98 #define MAX_HEIGHT 243
99 #define MAX_WIDTH 336
101 /* Bit fields for status flags */
102 #define QC_PARAM_CHANGE 0x01 /* Camera status change has occurred */
104 struct qcam {
105 struct v4l2_device v4l2_dev;
106 struct video_device vdev;
107 struct pardevice *pdev;
108 struct parport *pport;
109 struct mutex lock;
110 int width, height;
111 int bpp;
112 int mode;
113 int contrast, brightness, whitebal;
114 int port_mode;
115 int transfer_scale;
116 int top, left;
117 int status;
118 unsigned int saved_bits;
119 unsigned long in_use;
122 static unsigned int maxpoll = 250; /* Maximum busy-loop count for qcam I/O */
123 static unsigned int yieldlines = 4; /* Yield after this many during capture */
124 static int video_nr = -1;
125 static unsigned int force_init; /* Whether to probe aggressively */
127 module_param(maxpoll, int, 0);
128 module_param(yieldlines, int, 0);
129 module_param(video_nr, int, 0);
131 /* Set force_init=1 to avoid detection by polling status register and
132 * immediately attempt to initialize qcam */
133 module_param(force_init, int, 0);
135 #define MAX_CAMS 4
136 static struct qcam *qcams[MAX_CAMS];
137 static unsigned int num_cams;
139 static inline int read_lpstatus(struct qcam *q)
141 return parport_read_status(q->pport);
144 static inline int read_lpdata(struct qcam *q)
146 return parport_read_data(q->pport);
149 static inline void write_lpdata(struct qcam *q, int d)
151 parport_write_data(q->pport, d);
154 static void write_lpcontrol(struct qcam *q, int d)
156 if (d & 0x20) {
157 /* Set bidirectional mode to reverse (data in) */
158 parport_data_reverse(q->pport);
159 } else {
160 /* Set bidirectional mode to forward (data out) */
161 parport_data_forward(q->pport);
164 /* Now issue the regular port command, but strip out the
165 * direction flag */
166 d &= ~0x20;
167 parport_write_control(q->pport, d);
171 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
172 * Almost all communication with the camera requires handshaking. */
174 static int qc_waithand(struct qcam *q, int val)
176 int status;
177 int runs = 0;
179 if (val) {
180 while (!((status = read_lpstatus(q)) & 8)) {
181 /* 1000 is enough spins on the I/O for all normal
182 cases, at that point we start to poll slowly
183 until the camera wakes up. However, we are
184 busy blocked until the camera responds, so
185 setting it lower is much better for interactive
186 response. */
188 if (runs++ > maxpoll)
189 msleep_interruptible(5);
190 if (runs > (maxpoll + 1000)) /* 5 seconds */
191 return -1;
193 } else {
194 while (((status = read_lpstatus(q)) & 8)) {
195 /* 1000 is enough spins on the I/O for all normal
196 cases, at that point we start to poll slowly
197 until the camera wakes up. However, we are
198 busy blocked until the camera responds, so
199 setting it lower is much better for interactive
200 response. */
202 if (runs++ > maxpoll)
203 msleep_interruptible(5);
204 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
205 return -1;
209 return status;
212 /* Waithand2 is used when the qcam is in bidirectional mode, and the
213 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
214 * (bit 3 of status register). It also returns the last value read,
215 * since this data is useful. */
217 static unsigned int qc_waithand2(struct qcam *q, int val)
219 unsigned int status;
220 int runs = 0;
222 do {
223 status = read_lpdata(q);
224 /* 1000 is enough spins on the I/O for all normal
225 cases, at that point we start to poll slowly
226 until the camera wakes up. However, we are
227 busy blocked until the camera responds, so
228 setting it lower is much better for interactive
229 response. */
231 if (runs++ > maxpoll)
232 msleep_interruptible(5);
233 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
234 return 0;
235 } while ((status & 1) != val);
237 return status;
240 /* qc_command is probably a bit of a misnomer -- it's used to send
241 * bytes *to* the camera. Generally, these bytes are either commands
242 * or arguments to commands, so the name fits, but it still bugs me a
243 * bit. See the documentation for a list of commands. */
245 static int qc_command(struct qcam *q, int command)
247 int n1, n2;
248 int cmd;
250 write_lpdata(q, command);
251 write_lpcontrol(q, 6);
253 n1 = qc_waithand(q, 1);
255 write_lpcontrol(q, 0xe);
256 n2 = qc_waithand(q, 0);
258 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
259 return cmd;
262 static int qc_readparam(struct qcam *q)
264 int n1, n2;
265 int cmd;
267 write_lpcontrol(q, 6);
268 n1 = qc_waithand(q, 1);
270 write_lpcontrol(q, 0xe);
271 n2 = qc_waithand(q, 0);
273 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
274 return cmd;
278 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
279 the status register at 5-10 Hz. This is only used in the autoprobe
280 code. Be aware that this isn't the way Connectix detects the
281 camera (they send a reset and try to handshake), but this should be
282 almost completely safe, while their method screws up my printer if
283 I plug it in before the camera. */
285 static int qc_detect(struct qcam *q)
287 int reg, lastreg;
288 int count = 0;
289 int i;
291 if (force_init)
292 return 1;
294 lastreg = reg = read_lpstatus(q) & 0xf0;
296 for (i = 0; i < 500; i++) {
297 reg = read_lpstatus(q) & 0xf0;
298 if (reg != lastreg)
299 count++;
300 lastreg = reg;
301 mdelay(2);
306 /* Be (even more) liberal in what you accept... */
308 if (count > 20 && count < 400) {
309 return 1; /* found */
310 } else {
311 printk(KERN_ERR "No Quickcam found on port %s\n",
312 q->pport->name);
313 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
314 return 0; /* not found */
318 /* Decide which scan mode to use. There's no real requirement that
319 * the scanmode match the resolution in q->height and q-> width -- the
320 * camera takes the picture at the resolution specified in the
321 * "scanmode" and then returns the image at the resolution specified
322 * with the resolution commands. If the scan is bigger than the
323 * requested resolution, the upper-left hand corner of the scan is
324 * returned. If the scan is smaller, then the rest of the image
325 * returned contains garbage. */
327 static int qc_setscanmode(struct qcam *q)
329 int old_mode = q->mode;
331 switch (q->transfer_scale) {
332 case 1:
333 q->mode = 0;
334 break;
335 case 2:
336 q->mode = 4;
337 break;
338 case 4:
339 q->mode = 8;
340 break;
343 switch (q->bpp) {
344 case 4:
345 break;
346 case 6:
347 q->mode += 2;
348 break;
351 switch (q->port_mode & QC_MODE_MASK) {
352 case QC_BIDIR:
353 q->mode += 1;
354 break;
355 case QC_NOTSET:
356 case QC_UNIDIR:
357 break;
360 if (q->mode != old_mode)
361 q->status |= QC_PARAM_CHANGE;
363 return 0;
367 /* Reset the QuickCam. This uses the same sequence the Windows
368 * QuickPic program uses. Someone with a bi-directional port should
369 * check that bi-directional mode is detected right, and then
370 * implement bi-directional mode in qc_readbyte(). */
372 static void qc_reset(struct qcam *q)
374 switch (q->port_mode & QC_FORCE_MASK) {
375 case QC_FORCE_UNIDIR:
376 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
377 break;
379 case QC_FORCE_BIDIR:
380 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
381 break;
383 case QC_ANY:
384 write_lpcontrol(q, 0x20);
385 write_lpdata(q, 0x75);
387 if (read_lpdata(q) != 0x75)
388 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
389 else
390 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
391 break;
394 write_lpcontrol(q, 0xb);
395 udelay(250);
396 write_lpcontrol(q, 0xe);
397 qc_setscanmode(q); /* in case port_mode changed */
402 /* Reset the QuickCam and program for brightness, contrast,
403 * white-balance, and resolution. */
405 static void qc_set(struct qcam *q)
407 int val;
408 int val2;
410 qc_reset(q);
412 /* Set the brightness. Yes, this is repetitive, but it works.
413 * Shorter versions seem to fail subtly. Feel free to try :-). */
414 /* I think the problem was in qc_command, not here -- bls */
416 qc_command(q, 0xb);
417 qc_command(q, q->brightness);
419 val = q->height / q->transfer_scale;
420 qc_command(q, 0x11);
421 qc_command(q, val);
422 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
423 /* The normal "transfers per line" calculation doesn't seem to work
424 as expected here (and yet it works fine in qc_scan). No idea
425 why this case is the odd man out. Fortunately, Laird's original
426 working version gives me a good way to guess at working values.
427 -- bls */
428 val = q->width;
429 val2 = q->transfer_scale * 4;
430 } else {
431 val = q->width * q->bpp;
432 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
433 q->transfer_scale;
435 val = DIV_ROUND_UP(val, val2);
436 qc_command(q, 0x13);
437 qc_command(q, val);
439 /* Setting top and left -- bls */
440 qc_command(q, 0xd);
441 qc_command(q, q->top);
442 qc_command(q, 0xf);
443 qc_command(q, q->left / 2);
445 qc_command(q, 0x19);
446 qc_command(q, q->contrast);
447 qc_command(q, 0x1f);
448 qc_command(q, q->whitebal);
450 /* Clear flag that we must update the grabbing parameters on the camera
451 before we grab the next frame */
452 q->status &= (~QC_PARAM_CHANGE);
455 /* Qc_readbytes reads some bytes from the QC and puts them in
456 the supplied buffer. It returns the number of bytes read,
457 or -1 on error. */
459 static inline int qc_readbytes(struct qcam *q, char buffer[])
461 int ret = 1;
462 unsigned int hi, lo;
463 unsigned int hi2, lo2;
464 static int state;
466 if (buffer == NULL) {
467 state = 0;
468 return 0;
471 switch (q->port_mode & QC_MODE_MASK) {
472 case QC_BIDIR: /* Bi-directional Port */
473 write_lpcontrol(q, 0x26);
474 lo = (qc_waithand2(q, 1) >> 1);
475 hi = (read_lpstatus(q) >> 3) & 0x1f;
476 write_lpcontrol(q, 0x2e);
477 lo2 = (qc_waithand2(q, 0) >> 1);
478 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
479 switch (q->bpp) {
480 case 4:
481 buffer[0] = lo & 0xf;
482 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
483 buffer[2] = (hi & 0x1e) >> 1;
484 buffer[3] = lo2 & 0xf;
485 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
486 buffer[5] = (hi2 & 0x1e) >> 1;
487 ret = 6;
488 break;
489 case 6:
490 buffer[0] = lo & 0x3f;
491 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
492 buffer[2] = lo2 & 0x3f;
493 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
494 ret = 4;
495 break;
497 break;
499 case QC_UNIDIR: /* Unidirectional Port */
500 write_lpcontrol(q, 6);
501 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
502 write_lpcontrol(q, 0xe);
503 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
505 switch (q->bpp) {
506 case 4:
507 buffer[0] = lo;
508 buffer[1] = hi;
509 ret = 2;
510 break;
511 case 6:
512 switch (state) {
513 case 0:
514 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
515 q->saved_bits = (hi & 3) << 4;
516 state = 1;
517 ret = 1;
518 break;
519 case 1:
520 buffer[0] = lo | q->saved_bits;
521 q->saved_bits = hi << 2;
522 state = 2;
523 ret = 1;
524 break;
525 case 2:
526 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
527 buffer[1] = ((lo & 3) << 4) | hi;
528 state = 0;
529 ret = 2;
530 break;
532 break;
534 break;
536 return ret;
539 /* requests a scan from the camera. It sends the correct instructions
540 * to the camera and then reads back the correct number of bytes. In
541 * previous versions of this routine the return structure contained
542 * the raw output from the camera, and there was a 'qc_convertscan'
543 * function that converted that to a useful format. In version 0.3 I
544 * rolled qc_convertscan into qc_scan and now I only return the
545 * converted scan. The format is just an one-dimensional array of
546 * characters, one for each pixel, with 0=black up to n=white, where
547 * n=2^(bit depth)-1. Ask me for more details if you don't understand
548 * this. */
550 static long qc_capture(struct qcam *q, char __user *buf, unsigned long len)
552 int i, j, k, yield;
553 int bytes;
554 int linestotrans, transperline;
555 int divisor;
556 int pixels_per_line;
557 int pixels_read = 0;
558 int got = 0;
559 char buffer[6];
560 int shift = 8 - q->bpp;
561 char invert;
563 if (q->mode == -1)
564 return -ENXIO;
566 qc_command(q, 0x7);
567 qc_command(q, q->mode);
569 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
570 write_lpcontrol(q, 0x2e); /* turn port around */
571 write_lpcontrol(q, 0x26);
572 qc_waithand(q, 1);
573 write_lpcontrol(q, 0x2e);
574 qc_waithand(q, 0);
577 /* strange -- should be 15:63 below, but 4bpp is odd */
578 invert = (q->bpp == 4) ? 16 : 63;
580 linestotrans = q->height / q->transfer_scale;
581 pixels_per_line = q->width / q->transfer_scale;
582 transperline = q->width * q->bpp;
583 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
584 q->transfer_scale;
585 transperline = DIV_ROUND_UP(transperline, divisor);
587 for (i = 0, yield = yieldlines; i < linestotrans; i++) {
588 for (pixels_read = j = 0; j < transperline; j++) {
589 bytes = qc_readbytes(q, buffer);
590 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) {
591 int o;
592 if (buffer[k] == 0 && invert == 16) {
593 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
594 must be 0-15 -- bls */
595 buffer[k] = 16;
597 o = i * pixels_per_line + pixels_read + k;
598 if (o < len) {
599 got++;
600 put_user((invert - buffer[k]) << shift, buf + o);
603 pixels_read += bytes;
605 qc_readbytes(q, NULL); /* reset state machine */
607 /* Grabbing an entire frame from the quickcam is a lengthy
608 process. We don't (usually) want to busy-block the
609 processor for the entire frame. yieldlines is a module
610 parameter. If we yield every line, the minimum frame
611 time will be 240 / 200 = 1.2 seconds. The compile-time
612 default is to yield every 4 lines. */
613 if (i >= yield) {
614 msleep_interruptible(5);
615 yield = i + yieldlines;
619 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
620 write_lpcontrol(q, 2);
621 write_lpcontrol(q, 6);
622 udelay(3);
623 write_lpcontrol(q, 0xe);
625 if (got < len)
626 return got;
627 return len;
631 * Video4linux interfacing
634 static int qcam_querycap(struct file *file, void *priv,
635 struct v4l2_capability *vcap)
637 struct qcam *qcam = video_drvdata(file);
639 strlcpy(vcap->driver, qcam->v4l2_dev.name, sizeof(vcap->driver));
640 strlcpy(vcap->card, "B&W Quickcam", sizeof(vcap->card));
641 strlcpy(vcap->bus_info, "parport", sizeof(vcap->bus_info));
642 vcap->version = KERNEL_VERSION(0, 0, 2);
643 vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
644 return 0;
647 static int qcam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
649 if (vin->index > 0)
650 return -EINVAL;
651 strlcpy(vin->name, "Camera", sizeof(vin->name));
652 vin->type = V4L2_INPUT_TYPE_CAMERA;
653 vin->audioset = 0;
654 vin->tuner = 0;
655 vin->std = 0;
656 vin->status = 0;
657 return 0;
660 static int qcam_g_input(struct file *file, void *fh, unsigned int *inp)
662 *inp = 0;
663 return 0;
666 static int qcam_s_input(struct file *file, void *fh, unsigned int inp)
668 return (inp > 0) ? -EINVAL : 0;
671 static int qcam_queryctrl(struct file *file, void *priv,
672 struct v4l2_queryctrl *qc)
674 switch (qc->id) {
675 case V4L2_CID_BRIGHTNESS:
676 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 180);
677 case V4L2_CID_CONTRAST:
678 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 192);
679 case V4L2_CID_GAMMA:
680 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 105);
682 return -EINVAL;
685 static int qcam_g_ctrl(struct file *file, void *priv,
686 struct v4l2_control *ctrl)
688 struct qcam *qcam = video_drvdata(file);
689 int ret = 0;
691 switch (ctrl->id) {
692 case V4L2_CID_BRIGHTNESS:
693 ctrl->value = qcam->brightness;
694 break;
695 case V4L2_CID_CONTRAST:
696 ctrl->value = qcam->contrast;
697 break;
698 case V4L2_CID_GAMMA:
699 ctrl->value = qcam->whitebal;
700 break;
701 default:
702 ret = -EINVAL;
703 break;
705 return ret;
708 static int qcam_s_ctrl(struct file *file, void *priv,
709 struct v4l2_control *ctrl)
711 struct qcam *qcam = video_drvdata(file);
712 int ret = 0;
714 mutex_lock(&qcam->lock);
715 switch (ctrl->id) {
716 case V4L2_CID_BRIGHTNESS:
717 qcam->brightness = ctrl->value;
718 break;
719 case V4L2_CID_CONTRAST:
720 qcam->contrast = ctrl->value;
721 break;
722 case V4L2_CID_GAMMA:
723 qcam->whitebal = ctrl->value;
724 break;
725 default:
726 ret = -EINVAL;
727 break;
729 if (ret == 0) {
730 qc_setscanmode(qcam);
731 qcam->status |= QC_PARAM_CHANGE;
733 mutex_unlock(&qcam->lock);
734 return ret;
737 static int qcam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
739 struct qcam *qcam = video_drvdata(file);
740 struct v4l2_pix_format *pix = &fmt->fmt.pix;
742 pix->width = qcam->width / qcam->transfer_scale;
743 pix->height = qcam->height / qcam->transfer_scale;
744 pix->pixelformat = (qcam->bpp == 4) ? V4L2_PIX_FMT_Y4 : V4L2_PIX_FMT_Y6;
745 pix->field = V4L2_FIELD_NONE;
746 pix->bytesperline = qcam->width;
747 pix->sizeimage = qcam->width * qcam->height;
748 /* Just a guess */
749 pix->colorspace = V4L2_COLORSPACE_SRGB;
750 return 0;
753 static int qcam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
755 struct v4l2_pix_format *pix = &fmt->fmt.pix;
757 if (pix->height <= 60 || pix->width <= 80) {
758 pix->height = 60;
759 pix->width = 80;
760 } else if (pix->height <= 120 || pix->width <= 160) {
761 pix->height = 120;
762 pix->width = 160;
763 } else {
764 pix->height = 240;
765 pix->width = 320;
767 if (pix->pixelformat != V4L2_PIX_FMT_Y4 &&
768 pix->pixelformat != V4L2_PIX_FMT_Y6)
769 pix->pixelformat = V4L2_PIX_FMT_Y4;
770 pix->field = V4L2_FIELD_NONE;
771 pix->bytesperline = pix->width;
772 pix->sizeimage = pix->width * pix->height;
773 /* Just a guess */
774 pix->colorspace = V4L2_COLORSPACE_SRGB;
775 return 0;
778 static int qcam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
780 struct qcam *qcam = video_drvdata(file);
781 struct v4l2_pix_format *pix = &fmt->fmt.pix;
782 int ret = qcam_try_fmt_vid_cap(file, fh, fmt);
784 if (ret)
785 return ret;
786 qcam->width = 320;
787 qcam->height = 240;
788 if (pix->height == 60)
789 qcam->transfer_scale = 4;
790 else if (pix->height == 120)
791 qcam->transfer_scale = 2;
792 else
793 qcam->transfer_scale = 1;
794 if (pix->pixelformat == V4L2_PIX_FMT_Y6)
795 qcam->bpp = 6;
796 else
797 qcam->bpp = 4;
799 mutex_lock(&qcam->lock);
800 qc_setscanmode(qcam);
801 /* We must update the camera before we grab. We could
802 just have changed the grab size */
803 qcam->status |= QC_PARAM_CHANGE;
804 mutex_unlock(&qcam->lock);
805 return 0;
808 static int qcam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
810 static struct v4l2_fmtdesc formats[] = {
811 { 0, 0, 0,
812 "4-Bit Monochrome", V4L2_PIX_FMT_Y4,
813 { 0, 0, 0, 0 }
815 { 0, 0, 0,
816 "6-Bit Monochrome", V4L2_PIX_FMT_Y6,
817 { 0, 0, 0, 0 }
820 enum v4l2_buf_type type = fmt->type;
822 if (fmt->index > 1)
823 return -EINVAL;
825 *fmt = formats[fmt->index];
826 fmt->type = type;
827 return 0;
830 static ssize_t qcam_read(struct file *file, char __user *buf,
831 size_t count, loff_t *ppos)
833 struct qcam *qcam = video_drvdata(file);
834 int len;
835 parport_claim_or_block(qcam->pdev);
837 mutex_lock(&qcam->lock);
839 qc_reset(qcam);
841 /* Update the camera parameters if we need to */
842 if (qcam->status & QC_PARAM_CHANGE)
843 qc_set(qcam);
845 len = qc_capture(qcam, buf, count);
847 mutex_unlock(&qcam->lock);
849 parport_release(qcam->pdev);
850 return len;
853 static const struct v4l2_file_operations qcam_fops = {
854 .owner = THIS_MODULE,
855 .ioctl = video_ioctl2,
856 .read = qcam_read,
859 static const struct v4l2_ioctl_ops qcam_ioctl_ops = {
860 .vidioc_querycap = qcam_querycap,
861 .vidioc_g_input = qcam_g_input,
862 .vidioc_s_input = qcam_s_input,
863 .vidioc_enum_input = qcam_enum_input,
864 .vidioc_queryctrl = qcam_queryctrl,
865 .vidioc_g_ctrl = qcam_g_ctrl,
866 .vidioc_s_ctrl = qcam_s_ctrl,
867 .vidioc_enum_fmt_vid_cap = qcam_enum_fmt_vid_cap,
868 .vidioc_g_fmt_vid_cap = qcam_g_fmt_vid_cap,
869 .vidioc_s_fmt_vid_cap = qcam_s_fmt_vid_cap,
870 .vidioc_try_fmt_vid_cap = qcam_try_fmt_vid_cap,
873 /* Initialize the QuickCam driver control structure. This is where
874 * defaults are set for people who don't have a config file.*/
876 static struct qcam *qcam_init(struct parport *port)
878 struct qcam *qcam;
879 struct v4l2_device *v4l2_dev;
881 qcam = kzalloc(sizeof(struct qcam), GFP_KERNEL);
882 if (qcam == NULL)
883 return NULL;
885 v4l2_dev = &qcam->v4l2_dev;
886 strlcpy(v4l2_dev->name, "bw-qcam", sizeof(v4l2_dev->name));
888 if (v4l2_device_register(NULL, v4l2_dev) < 0) {
889 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
890 return NULL;
893 qcam->pport = port;
894 qcam->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
895 NULL, 0, NULL);
896 if (qcam->pdev == NULL) {
897 v4l2_err(v4l2_dev, "couldn't register for %s.\n", port->name);
898 kfree(qcam);
899 return NULL;
902 strlcpy(qcam->vdev.name, "Connectix QuickCam", sizeof(qcam->vdev.name));
903 qcam->vdev.v4l2_dev = v4l2_dev;
904 qcam->vdev.fops = &qcam_fops;
905 qcam->vdev.ioctl_ops = &qcam_ioctl_ops;
906 qcam->vdev.release = video_device_release_empty;
907 video_set_drvdata(&qcam->vdev, qcam);
909 mutex_init(&qcam->lock);
911 qcam->port_mode = (QC_ANY | QC_NOTSET);
912 qcam->width = 320;
913 qcam->height = 240;
914 qcam->bpp = 4;
915 qcam->transfer_scale = 2;
916 qcam->contrast = 192;
917 qcam->brightness = 180;
918 qcam->whitebal = 105;
919 qcam->top = 1;
920 qcam->left = 14;
921 qcam->mode = -1;
922 qcam->status = QC_PARAM_CHANGE;
923 return qcam;
926 static int qc_calibrate(struct qcam *q)
929 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
930 * The white balance is an individual value for each
931 * quickcam.
934 int value;
935 int count = 0;
937 qc_command(q, 27); /* AutoAdjustOffset */
938 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
940 /* GetOffset (33) will read 255 until autocalibration */
941 /* is finished. After that, a value of 1-254 will be */
942 /* returned. */
944 do {
945 qc_command(q, 33);
946 value = qc_readparam(q);
947 mdelay(1);
948 schedule();
949 count++;
950 } while (value == 0xff && count < 2048);
952 q->whitebal = value;
953 return value;
956 static int init_bwqcam(struct parport *port)
958 struct qcam *qcam;
960 if (num_cams == MAX_CAMS) {
961 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
962 return -ENOSPC;
965 qcam = qcam_init(port);
966 if (qcam == NULL)
967 return -ENODEV;
969 parport_claim_or_block(qcam->pdev);
971 qc_reset(qcam);
973 if (qc_detect(qcam) == 0) {
974 parport_release(qcam->pdev);
975 parport_unregister_device(qcam->pdev);
976 kfree(qcam);
977 return -ENODEV;
979 qc_calibrate(qcam);
981 parport_release(qcam->pdev);
983 v4l2_info(&qcam->v4l2_dev, "Connectix Quickcam on %s\n", qcam->pport->name);
985 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
986 parport_unregister_device(qcam->pdev);
987 kfree(qcam);
988 return -ENODEV;
991 qcams[num_cams++] = qcam;
993 return 0;
996 static void close_bwqcam(struct qcam *qcam)
998 video_unregister_device(&qcam->vdev);
999 parport_unregister_device(qcam->pdev);
1000 kfree(qcam);
1003 /* The parport parameter controls which parports will be scanned.
1004 * Scanning all parports causes some printers to print a garbage page.
1005 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
1006 #ifdef MODULE
1007 static char *parport[MAX_CAMS] = { NULL, };
1008 module_param_array(parport, charp, NULL, 0);
1009 #endif
1011 static int accept_bwqcam(struct parport *port)
1013 #ifdef MODULE
1014 int n;
1016 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
1017 /* user gave parport parameters */
1018 for (n = 0; n < MAX_CAMS && parport[n]; n++) {
1019 char *ep;
1020 unsigned long r;
1021 r = simple_strtoul(parport[n], &ep, 0);
1022 if (ep == parport[n]) {
1023 printk(KERN_ERR
1024 "bw-qcam: bad port specifier \"%s\"\n",
1025 parport[n]);
1026 continue;
1028 if (r == port->number)
1029 return 1;
1031 return 0;
1033 #endif
1034 return 1;
1037 static void bwqcam_attach(struct parport *port)
1039 if (accept_bwqcam(port))
1040 init_bwqcam(port);
1043 static void bwqcam_detach(struct parport *port)
1045 int i;
1046 for (i = 0; i < num_cams; i++) {
1047 struct qcam *qcam = qcams[i];
1048 if (qcam && qcam->pdev->port == port) {
1049 qcams[i] = NULL;
1050 close_bwqcam(qcam);
1055 static struct parport_driver bwqcam_driver = {
1056 .name = "bw-qcam",
1057 .attach = bwqcam_attach,
1058 .detach = bwqcam_detach,
1061 static void __exit exit_bw_qcams(void)
1063 parport_unregister_driver(&bwqcam_driver);
1066 static int __init init_bw_qcams(void)
1068 #ifdef MODULE
1069 /* Do some sanity checks on the module parameters. */
1070 if (maxpoll > 5000) {
1071 printk(KERN_INFO "Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1072 maxpoll = 5000;
1075 if (yieldlines < 1) {
1076 printk(KERN_INFO "Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1077 yieldlines = 1;
1079 #endif
1080 return parport_register_driver(&bwqcam_driver);
1083 module_init(init_bw_qcams);
1084 module_exit(exit_bw_qcams);
1086 MODULE_LICENSE("GPL");