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.
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>
69 #include <linux/init.h>
70 #include <linux/kernel.h>
71 #include <linux/slab.h>
73 #include <linux/parport.h>
74 #include <linux/sched.h>
75 #include <linux/videodev.h>
76 #include <media/v4l2-common.h>
77 #include <media/v4l2-ioctl.h>
78 #include <linux/mutex.h>
79 #include <asm/uaccess.h>
83 static unsigned int maxpoll = 250; /* Maximum busy-loop count for qcam I/O */
84 static unsigned int yieldlines = 4; /* Yield after this many during capture */
85 static int video_nr = -1;
86 static unsigned int force_init; /* Whether to probe aggressively */
88 module_param(maxpoll, int, 0);
89 module_param(yieldlines, int, 0);
90 module_param(video_nr, int, 0);
92 /* Set force_init=1 to avoid detection by polling status register and
93 * immediately attempt to initialize qcam */
94 module_param(force_init, int, 0);
96 static inline int read_lpstatus(struct qcam_device *q)
98 return parport_read_status(q->pport);
101 static inline int read_lpdata(struct qcam_device *q)
103 return parport_read_data(q->pport);
106 static inline void write_lpdata(struct qcam_device *q, int d)
108 parport_write_data(q->pport, d);
111 static inline void write_lpcontrol(struct qcam_device *q, int d)
114 /* Set bidirectional mode to reverse (data in) */
115 parport_data_reverse(q->pport);
117 /* Set bidirectional mode to forward (data out) */
118 parport_data_forward(q->pport);
121 /* Now issue the regular port command, but strip out the
124 parport_write_control(q->pport, d);
127 static int qc_waithand(struct qcam_device *q, int val);
128 static int qc_command(struct qcam_device *q, int command);
129 static int qc_readparam(struct qcam_device *q);
130 static int qc_setscanmode(struct qcam_device *q);
131 static int qc_readbytes(struct qcam_device *q, char buffer[]);
133 static struct video_device qcam_template;
135 static int qc_calibrate(struct qcam_device *q)
138 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
139 * The white balance is an individiual value for each
146 qc_command(q, 27); /* AutoAdjustOffset */
147 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
149 /* GetOffset (33) will read 255 until autocalibration */
150 /* is finished. After that, a value of 1-254 will be */
155 value = qc_readparam(q);
159 } while (value == 0xff && count < 2048);
165 /* Initialize the QuickCam driver control structure. This is where
166 * defaults are set for people who don't have a config file.*/
168 static struct qcam_device *qcam_init(struct parport *port)
170 struct qcam_device *q;
172 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
177 q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
179 if (q->pdev == NULL) {
180 printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
186 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
188 mutex_init(&q->lock);
190 q->port_mode = (QC_ANY | QC_NOTSET);
194 q->transfer_scale = 2;
201 q->status = QC_PARAM_CHANGE;
206 /* qc_command is probably a bit of a misnomer -- it's used to send
207 * bytes *to* the camera. Generally, these bytes are either commands
208 * or arguments to commands, so the name fits, but it still bugs me a
209 * bit. See the documentation for a list of commands. */
211 static int qc_command(struct qcam_device *q, int command)
216 write_lpdata(q, command);
217 write_lpcontrol(q, 6);
219 n1 = qc_waithand(q, 1);
221 write_lpcontrol(q, 0xe);
222 n2 = qc_waithand(q, 0);
224 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
228 static int qc_readparam(struct qcam_device *q)
233 write_lpcontrol(q, 6);
234 n1 = qc_waithand(q, 1);
236 write_lpcontrol(q, 0xe);
237 n2 = qc_waithand(q, 0);
239 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
243 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
244 * Almost all communication with the camera requires handshaking. */
246 static int qc_waithand(struct qcam_device *q, int val)
252 while (!((status = read_lpstatus(q)) & 8)) {
253 /* 1000 is enough spins on the I/O for all normal
254 cases, at that point we start to poll slowly
255 until the camera wakes up. However, we are
256 busy blocked until the camera responds, so
257 setting it lower is much better for interactive
260 if (runs++ > maxpoll)
261 msleep_interruptible(5);
262 if (runs > (maxpoll + 1000)) /* 5 seconds */
266 while (((status = read_lpstatus(q)) & 8)) {
267 /* 1000 is enough spins on the I/O for all normal
268 cases, at that point we start to poll slowly
269 until the camera wakes up. However, we are
270 busy blocked until the camera responds, so
271 setting it lower is much better for interactive
274 if (runs++ > maxpoll)
275 msleep_interruptible(5);
276 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
284 /* Waithand2 is used when the qcam is in bidirectional mode, and the
285 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
286 * (bit 3 of status register). It also returns the last value read,
287 * since this data is useful. */
289 static unsigned int qc_waithand2(struct qcam_device *q, int val)
295 status = read_lpdata(q);
296 /* 1000 is enough spins on the I/O for all normal
297 cases, at that point we start to poll slowly
298 until the camera wakes up. However, we are
299 busy blocked until the camera responds, so
300 setting it lower is much better for interactive
303 if (runs++ > maxpoll)
304 msleep_interruptible(5);
305 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
307 } while ((status & 1) != val);
313 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
314 the status register at 5-10 Hz. This is only used in the autoprobe
315 code. Be aware that this isn't the way Connectix detects the
316 camera (they send a reset and try to handshake), but this should be
317 almost completely safe, while their method screws up my printer if
318 I plug it in before the camera. */
320 static int qc_detect(struct qcam_device *q)
329 lastreg = reg = read_lpstatus(q) & 0xf0;
331 for (i = 0; i < 500; i++) {
332 reg = read_lpstatus(q) & 0xf0;
341 /* Force camera detection during testing. Sometimes the camera
342 won't be flashing these bits. Possibly unloading the module
343 in the middle of a grab? Or some timeout condition?
344 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
345 printk(KERN_DEBUG "Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
349 /* Be (even more) liberal in what you accept... */
351 if (count > 20 && count < 400) {
352 return 1; /* found */
354 printk(KERN_ERR "No Quickcam found on port %s\n",
356 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
357 return 0; /* not found */
362 /* Reset the QuickCam. This uses the same sequence the Windows
363 * QuickPic program uses. Someone with a bi-directional port should
364 * check that bi-directional mode is detected right, and then
365 * implement bi-directional mode in qc_readbyte(). */
367 static void qc_reset(struct qcam_device *q)
369 switch (q->port_mode & QC_FORCE_MASK) {
370 case QC_FORCE_UNIDIR:
371 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
375 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
379 write_lpcontrol(q, 0x20);
380 write_lpdata(q, 0x75);
382 if (read_lpdata(q) != 0x75)
383 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
385 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
389 write_lpcontrol(q, 0xb);
391 write_lpcontrol(q, 0xe);
392 qc_setscanmode(q); /* in case port_mode changed */
396 /* Decide which scan mode to use. There's no real requirement that
397 * the scanmode match the resolution in q->height and q-> width -- the
398 * camera takes the picture at the resolution specified in the
399 * "scanmode" and then returns the image at the resolution specified
400 * with the resolution commands. If the scan is bigger than the
401 * requested resolution, the upper-left hand corner of the scan is
402 * returned. If the scan is smaller, then the rest of the image
403 * returned contains garbage. */
405 static int qc_setscanmode(struct qcam_device *q)
407 int old_mode = q->mode;
409 switch (q->transfer_scale) {
429 switch (q->port_mode & QC_MODE_MASK) {
438 if (q->mode != old_mode)
439 q->status |= QC_PARAM_CHANGE;
445 /* Reset the QuickCam and program for brightness, contrast,
446 * white-balance, and resolution. */
448 static void qc_set(struct qcam_device *q)
455 /* Set the brightness. Yes, this is repetitive, but it works.
456 * Shorter versions seem to fail subtly. Feel free to try :-). */
457 /* I think the problem was in qc_command, not here -- bls */
460 qc_command(q, q->brightness);
462 val = q->height / q->transfer_scale;
465 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
466 /* The normal "transfers per line" calculation doesn't seem to work
467 as expected here (and yet it works fine in qc_scan). No idea
468 why this case is the odd man out. Fortunately, Laird's original
469 working version gives me a good way to guess at working values.
472 val2 = q->transfer_scale * 4;
474 val = q->width * q->bpp;
475 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
478 val = DIV_ROUND_UP(val, val2);
482 /* Setting top and left -- bls */
484 qc_command(q, q->top);
486 qc_command(q, q->left / 2);
489 qc_command(q, q->contrast);
491 qc_command(q, q->whitebal);
493 /* Clear flag that we must update the grabbing parameters on the camera
494 before we grab the next frame */
495 q->status &= (~QC_PARAM_CHANGE);
498 /* Qc_readbytes reads some bytes from the QC and puts them in
499 the supplied buffer. It returns the number of bytes read,
502 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
506 unsigned int hi2, lo2;
509 if (buffer == NULL) {
514 switch (q->port_mode & QC_MODE_MASK) {
515 case QC_BIDIR: /* Bi-directional Port */
516 write_lpcontrol(q, 0x26);
517 lo = (qc_waithand2(q, 1) >> 1);
518 hi = (read_lpstatus(q) >> 3) & 0x1f;
519 write_lpcontrol(q, 0x2e);
520 lo2 = (qc_waithand2(q, 0) >> 1);
521 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
524 buffer[0] = lo & 0xf;
525 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
526 buffer[2] = (hi & 0x1e) >> 1;
527 buffer[3] = lo2 & 0xf;
528 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
529 buffer[5] = (hi2 & 0x1e) >> 1;
533 buffer[0] = lo & 0x3f;
534 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
535 buffer[2] = lo2 & 0x3f;
536 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
542 case QC_UNIDIR: /* Unidirectional Port */
543 write_lpcontrol(q, 6);
544 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
545 write_lpcontrol(q, 0xe);
546 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
557 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
558 q->saved_bits = (hi & 3) << 4;
563 buffer[0] = lo | q->saved_bits;
564 q->saved_bits = hi << 2;
569 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
570 buffer[1] = ((lo & 3) << 4) | hi;
582 /* requests a scan from the camera. It sends the correct instructions
583 * to the camera and then reads back the correct number of bytes. In
584 * previous versions of this routine the return structure contained
585 * the raw output from the camera, and there was a 'qc_convertscan'
586 * function that converted that to a useful format. In version 0.3 I
587 * rolled qc_convertscan into qc_scan and now I only return the
588 * converted scan. The format is just an one-dimensional array of
589 * characters, one for each pixel, with 0=black up to n=white, where
590 * n=2^(bit depth)-1. Ask me for more details if you don't understand
593 static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long len)
597 int linestotrans, transperline;
603 int shift = 8 - q->bpp;
610 qc_command(q, q->mode);
612 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
613 write_lpcontrol(q, 0x2e); /* turn port around */
614 write_lpcontrol(q, 0x26);
616 write_lpcontrol(q, 0x2e);
620 /* strange -- should be 15:63 below, but 4bpp is odd */
621 invert = (q->bpp == 4) ? 16 : 63;
623 linestotrans = q->height / q->transfer_scale;
624 pixels_per_line = q->width / q->transfer_scale;
625 transperline = q->width * q->bpp;
626 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
628 transperline = DIV_ROUND_UP(transperline, divisor);
630 for (i = 0, yield = yieldlines; i < linestotrans; i++) {
631 for (pixels_read = j = 0; j < transperline; j++) {
632 bytes = qc_readbytes(q, buffer);
633 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) {
635 if (buffer[k] == 0 && invert == 16) {
636 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
637 must be 0-15 -- bls */
640 o = i * pixels_per_line + pixels_read + k;
643 put_user((invert - buffer[k]) << shift, buf + o);
646 pixels_read += bytes;
648 qc_readbytes(q, NULL); /* reset state machine */
650 /* Grabbing an entire frame from the quickcam is a lengthy
651 process. We don't (usually) want to busy-block the
652 processor for the entire frame. yieldlines is a module
653 parameter. If we yield every line, the minimum frame
654 time will be 240 / 200 = 1.2 seconds. The compile-time
655 default is to yield every 4 lines. */
657 msleep_interruptible(5);
658 yield = i + yieldlines;
662 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
663 write_lpcontrol(q, 2);
664 write_lpcontrol(q, 6);
666 write_lpcontrol(q, 0xe);
674 * Video4linux interfacing
677 static long qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg)
679 struct video_device *dev = video_devdata(file);
680 struct qcam_device *qcam = (struct qcam_device *)dev;
685 struct video_capability *b = arg;
686 strcpy(b->name, "Quickcam");
687 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
698 struct video_channel *v = arg;
703 /* Good question.. its composite or SVHS so.. */
704 v->type = VIDEO_TYPE_CAMERA;
705 strcpy(v->name, "Camera");
710 struct video_channel *v = arg;
717 struct video_tuner *v = arg;
720 strcpy(v->name, "Format");
724 v->mode = VIDEO_MODE_AUTO;
729 struct video_tuner *v = arg;
732 if (v->mode != VIDEO_MODE_AUTO)
738 struct video_picture *p = arg;
741 p->brightness = qcam->brightness << 8;
742 p->contrast = qcam->contrast << 8;
743 p->whiteness = qcam->whitebal << 8;
744 p->depth = qcam->bpp;
745 p->palette = VIDEO_PALETTE_GREY;
750 struct video_picture *p = arg;
751 if (p->palette != VIDEO_PALETTE_GREY)
753 if (p->depth != 4 && p->depth != 6)
757 * Now load the camera.
760 qcam->brightness = p->brightness >> 8;
761 qcam->contrast = p->contrast >> 8;
762 qcam->whitebal = p->whiteness >> 8;
763 qcam->bpp = p->depth;
765 mutex_lock(&qcam->lock);
766 qc_setscanmode(qcam);
767 mutex_unlock(&qcam->lock);
768 qcam->status |= QC_PARAM_CHANGE;
774 struct video_window *vw = arg;
779 if (vw->height < 60 || vw->height > 240)
781 if (vw->width < 80 || vw->width > 320)
786 qcam->transfer_scale = 4;
788 if (vw->width >= 160 && vw->height >= 120)
789 qcam->transfer_scale = 2;
790 if (vw->width >= 320 && vw->height >= 240) {
793 qcam->transfer_scale = 1;
795 mutex_lock(&qcam->lock);
796 qc_setscanmode(qcam);
797 mutex_unlock(&qcam->lock);
799 /* We must update the camera before we grab. We could
800 just have changed the grab size */
801 qcam->status |= QC_PARAM_CHANGE;
803 /* Ok we figured out what to use from our wide choice */
808 struct video_window *vw = arg;
810 memset(vw, 0, sizeof(*vw));
811 vw->width = qcam->width / qcam->transfer_scale;
812 vw->height = qcam->height / qcam->transfer_scale;
831 static long qcam_ioctl(struct file *file,
832 unsigned int cmd, unsigned long arg)
834 return video_usercopy(file, cmd, arg, qcam_do_ioctl);
837 static ssize_t qcam_read(struct file *file, char __user *buf,
838 size_t count, loff_t *ppos)
840 struct video_device *v = video_devdata(file);
841 struct qcam_device *qcam = (struct qcam_device *)v;
843 parport_claim_or_block(qcam->pdev);
845 mutex_lock(&qcam->lock);
849 /* Update the camera parameters if we need to */
850 if (qcam->status & QC_PARAM_CHANGE)
853 len = qc_capture(qcam, buf, count);
855 mutex_unlock(&qcam->lock);
857 parport_release(qcam->pdev);
861 static int qcam_exclusive_open(struct file *file)
863 struct video_device *dev = video_devdata(file);
864 struct qcam_device *qcam = (struct qcam_device *)dev;
866 return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0;
869 static int qcam_exclusive_release(struct file *file)
871 struct video_device *dev = video_devdata(file);
872 struct qcam_device *qcam = (struct qcam_device *)dev;
874 clear_bit(0, &qcam->in_use);
878 static const struct v4l2_file_operations qcam_fops = {
879 .owner = THIS_MODULE,
880 .open = qcam_exclusive_open,
881 .release = qcam_exclusive_release,
885 static struct video_device qcam_template = {
886 .name = "Connectix Quickcam",
888 .release = video_device_release_empty,
892 static struct qcam_device *qcams[MAX_CAMS];
893 static unsigned int num_cams;
895 static int init_bwqcam(struct parport *port)
897 struct qcam_device *qcam;
899 if (num_cams == MAX_CAMS) {
900 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
904 qcam = qcam_init(port);
908 parport_claim_or_block(qcam->pdev);
912 if (qc_detect(qcam) == 0) {
913 parport_release(qcam->pdev);
914 parport_unregister_device(qcam->pdev);
920 parport_release(qcam->pdev);
922 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
924 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
925 parport_unregister_device(qcam->pdev);
930 qcams[num_cams++] = qcam;
935 static void close_bwqcam(struct qcam_device *qcam)
937 video_unregister_device(&qcam->vdev);
938 parport_unregister_device(qcam->pdev);
942 /* The parport parameter controls which parports will be scanned.
943 * Scanning all parports causes some printers to print a garbage page.
944 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
946 static char *parport[MAX_CAMS] = { NULL, };
947 module_param_array(parport, charp, NULL, 0);
950 static int accept_bwqcam(struct parport *port)
955 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
956 /* user gave parport parameters */
957 for (n = 0; n < MAX_CAMS && parport[n]; n++) {
960 r = simple_strtoul(parport[n], &ep, 0);
961 if (ep == parport[n]) {
963 "bw-qcam: bad port specifier \"%s\"\n",
967 if (r == port->number)
976 static void bwqcam_attach(struct parport *port)
978 if (accept_bwqcam(port))
982 static void bwqcam_detach(struct parport *port)
985 for (i = 0; i < num_cams; i++) {
986 struct qcam_device *qcam = qcams[i];
987 if (qcam && qcam->pdev->port == port) {
994 static struct parport_driver bwqcam_driver = {
996 .attach = bwqcam_attach,
997 .detach = bwqcam_detach,
1000 static void __exit exit_bw_qcams(void)
1002 parport_unregister_driver(&bwqcam_driver);
1005 static int __init init_bw_qcams(void)
1008 /* Do some sanity checks on the module parameters. */
1009 if (maxpoll > 5000) {
1010 printk(KERN_INFO "Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1014 if (yieldlines < 1) {
1015 printk(KERN_INFO "Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1019 return parport_register_driver(&bwqcam_driver);
1022 module_init(init_bw_qcams);
1023 module_exit(exit_bw_qcams);
1025 MODULE_LICENSE("GPL");