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,
181 printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
187 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
189 mutex_init(&q->lock);
191 q->port_mode = (QC_ANY | QC_NOTSET);
195 q->transfer_scale = 2;
202 q->status = QC_PARAM_CHANGE;
207 /* qc_command is probably a bit of a misnomer -- it's used to send
208 * bytes *to* the camera. Generally, these bytes are either commands
209 * or arguments to commands, so the name fits, but it still bugs me a
210 * bit. See the documentation for a list of commands. */
212 static int qc_command(struct qcam_device *q, int command)
217 write_lpdata(q, command);
218 write_lpcontrol(q, 6);
220 n1 = qc_waithand(q, 1);
222 write_lpcontrol(q, 0xe);
223 n2 = qc_waithand(q, 0);
225 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
229 static int qc_readparam(struct qcam_device *q)
234 write_lpcontrol(q, 6);
235 n1 = qc_waithand(q, 1);
237 write_lpcontrol(q, 0xe);
238 n2 = qc_waithand(q, 0);
240 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
244 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
245 * Almost all communication with the camera requires handshaking. */
247 static int qc_waithand(struct qcam_device *q, int val)
254 while (!((status = read_lpstatus(q)) & 8))
256 /* 1000 is enough spins on the I/O for all normal
257 cases, at that point we start to poll slowly
258 until the camera wakes up. However, we are
259 busy blocked until the camera responds, so
260 setting it lower is much better for interactive
265 msleep_interruptible(5);
267 if(runs>(maxpoll+1000)) /* 5 seconds */
273 while (((status = read_lpstatus(q)) & 8))
275 /* 1000 is enough spins on the I/O for all normal
276 cases, at that point we start to poll slowly
277 until the camera wakes up. However, we are
278 busy blocked until the camera responds, so
279 setting it lower is much better for interactive
284 msleep_interruptible(5);
286 if(runs++>(maxpoll+1000)) /* 5 seconds */
294 /* Waithand2 is used when the qcam is in bidirectional mode, and the
295 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
296 * (bit 3 of status register). It also returns the last value read,
297 * since this data is useful. */
299 static unsigned int qc_waithand2(struct qcam_device *q, int val)
306 status = read_lpdata(q);
307 /* 1000 is enough spins on the I/O for all normal
308 cases, at that point we start to poll slowly
309 until the camera wakes up. However, we are
310 busy blocked until the camera responds, so
311 setting it lower is much better for interactive
316 msleep_interruptible(5);
318 if(runs++>(maxpoll+1000)) /* 5 seconds */
321 while ((status & 1) != val);
327 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
328 the status register at 5-10 Hz. This is only used in the autoprobe
329 code. Be aware that this isn't the way Connectix detects the
330 camera (they send a reset and try to handshake), but this should be
331 almost completely safe, while their method screws up my printer if
332 I plug it in before the camera. */
334 static int qc_detect(struct qcam_device *q)
343 lastreg = reg = read_lpstatus(q) & 0xf0;
345 for (i = 0; i < 500; i++)
347 reg = read_lpstatus(q) & 0xf0;
356 /* Force camera detection during testing. Sometimes the camera
357 won't be flashing these bits. Possibly unloading the module
358 in the middle of a grab? Or some timeout condition?
359 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
360 printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
364 /* Be (even more) liberal in what you accept... */
366 if (count > 20 && count < 400) {
367 return 1; /* found */
369 printk(KERN_ERR "No Quickcam found on port %s\n",
371 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
372 return 0; /* not found */
377 /* Reset the QuickCam. This uses the same sequence the Windows
378 * QuickPic program uses. Someone with a bi-directional port should
379 * check that bi-directional mode is detected right, and then
380 * implement bi-directional mode in qc_readbyte(). */
382 static void qc_reset(struct qcam_device *q)
384 switch (q->port_mode & QC_FORCE_MASK)
386 case QC_FORCE_UNIDIR:
387 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
391 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
395 write_lpcontrol(q, 0x20);
396 write_lpdata(q, 0x75);
398 if (read_lpdata(q) != 0x75) {
399 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
401 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
406 write_lpcontrol(q, 0xb);
408 write_lpcontrol(q, 0xe);
409 qc_setscanmode(q); /* in case port_mode changed */
413 /* Decide which scan mode to use. There's no real requirement that
414 * the scanmode match the resolution in q->height and q-> width -- the
415 * camera takes the picture at the resolution specified in the
416 * "scanmode" and then returns the image at the resolution specified
417 * with the resolution commands. If the scan is bigger than the
418 * requested resolution, the upper-left hand corner of the scan is
419 * returned. If the scan is smaller, then the rest of the image
420 * returned contains garbage. */
422 static int qc_setscanmode(struct qcam_device *q)
424 int old_mode = q->mode;
426 switch (q->transfer_scale)
448 switch (q->port_mode & QC_MODE_MASK)
458 if (q->mode != old_mode)
459 q->status |= QC_PARAM_CHANGE;
465 /* Reset the QuickCam and program for brightness, contrast,
466 * white-balance, and resolution. */
468 static void qc_set(struct qcam_device *q)
475 /* Set the brightness. Yes, this is repetitive, but it works.
476 * Shorter versions seem to fail subtly. Feel free to try :-). */
477 /* I think the problem was in qc_command, not here -- bls */
480 qc_command(q, q->brightness);
482 val = q->height / q->transfer_scale;
485 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
486 /* The normal "transfers per line" calculation doesn't seem to work
487 as expected here (and yet it works fine in qc_scan). No idea
488 why this case is the odd man out. Fortunately, Laird's original
489 working version gives me a good way to guess at working values.
492 val2 = q->transfer_scale * 4;
494 val = q->width * q->bpp;
495 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
498 val = (val + val2 - 1) / val2;
502 /* Setting top and left -- bls */
504 qc_command(q, q->top);
506 qc_command(q, q->left / 2);
509 qc_command(q, q->contrast);
511 qc_command(q, q->whitebal);
513 /* Clear flag that we must update the grabbing parameters on the camera
514 before we grab the next frame */
515 q->status &= (~QC_PARAM_CHANGE);
518 /* Qc_readbytes reads some bytes from the QC and puts them in
519 the supplied buffer. It returns the number of bytes read,
522 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
526 unsigned int hi2, lo2;
535 switch (q->port_mode & QC_MODE_MASK)
537 case QC_BIDIR: /* Bi-directional Port */
538 write_lpcontrol(q, 0x26);
539 lo = (qc_waithand2(q, 1) >> 1);
540 hi = (read_lpstatus(q) >> 3) & 0x1f;
541 write_lpcontrol(q, 0x2e);
542 lo2 = (qc_waithand2(q, 0) >> 1);
543 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
547 buffer[0] = lo & 0xf;
548 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
549 buffer[2] = (hi & 0x1e) >> 1;
550 buffer[3] = lo2 & 0xf;
551 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
552 buffer[5] = (hi2 & 0x1e) >> 1;
556 buffer[0] = lo & 0x3f;
557 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
558 buffer[2] = lo2 & 0x3f;
559 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
565 case QC_UNIDIR: /* Unidirectional Port */
566 write_lpcontrol(q, 6);
567 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
568 write_lpcontrol(q, 0xe);
569 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
582 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
583 q->saved_bits = (hi & 3) << 4;
588 buffer[0] = lo | q->saved_bits;
589 q->saved_bits = hi << 2;
594 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
595 buffer[1] = ((lo & 3) << 4) | hi;
607 /* requests a scan from the camera. It sends the correct instructions
608 * to the camera and then reads back the correct number of bytes. In
609 * previous versions of this routine the return structure contained
610 * the raw output from the camera, and there was a 'qc_convertscan'
611 * function that converted that to a useful format. In version 0.3 I
612 * rolled qc_convertscan into qc_scan and now I only return the
613 * converted scan. The format is just an one-dimensional array of
614 * characters, one for each pixel, with 0=black up to n=white, where
615 * n=2^(bit depth)-1. Ask me for more details if you don't understand
618 static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len)
622 int linestotrans, transperline;
635 qc_command(q, q->mode);
637 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
639 write_lpcontrol(q, 0x2e); /* turn port around */
640 write_lpcontrol(q, 0x26);
641 (void) qc_waithand(q, 1);
642 write_lpcontrol(q, 0x2e);
643 (void) qc_waithand(q, 0);
646 /* strange -- should be 15:63 below, but 4bpp is odd */
647 invert = (q->bpp == 4) ? 16 : 63;
649 linestotrans = q->height / q->transfer_scale;
650 pixels_per_line = q->width / q->transfer_scale;
651 transperline = q->width * q->bpp;
652 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
654 transperline = (transperline + divisor - 1) / divisor;
656 for (i = 0, yield = yieldlines; i < linestotrans; i++)
658 for (pixels_read = j = 0; j < transperline; j++)
660 bytes = qc_readbytes(q, buffer);
661 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++)
664 if (buffer[k] == 0 && invert == 16)
666 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
667 must be 0-15 -- bls */
670 o=i*pixels_per_line + pixels_read + k;
674 put_user((invert - buffer[k])<<shift, buf+o);
677 pixels_read += bytes;
679 (void) qc_readbytes(q, NULL); /* reset state machine */
681 /* Grabbing an entire frame from the quickcam is a lengthy
682 process. We don't (usually) want to busy-block the
683 processor for the entire frame. yieldlines is a module
684 parameter. If we yield every line, the minimum frame
685 time will be 240 / 200 = 1.2 seconds. The compile-time
686 default is to yield every 4 lines. */
688 msleep_interruptible(5);
689 yield = i + yieldlines;
693 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
695 write_lpcontrol(q, 2);
696 write_lpcontrol(q, 6);
698 write_lpcontrol(q, 0xe);
706 * Video4linux interfacing
709 static int qcam_do_ioctl(struct inode *inode, struct file *file,
710 unsigned int cmd, void *arg)
712 struct video_device *dev = video_devdata(file);
713 struct qcam_device *qcam=(struct qcam_device *)dev;
719 struct video_capability *b = arg;
720 strcpy(b->name, "Quickcam");
721 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
732 struct video_channel *v = arg;
737 /* Good question.. its composite or SVHS so.. */
738 v->type = VIDEO_TYPE_CAMERA;
739 strcpy(v->name, "Camera");
744 struct video_channel *v = arg;
751 struct video_tuner *v = arg;
754 strcpy(v->name, "Format");
758 v->mode = VIDEO_MODE_AUTO;
763 struct video_tuner *v = arg;
766 if(v->mode!=VIDEO_MODE_AUTO)
772 struct video_picture *p = arg;
775 p->brightness=qcam->brightness<<8;
776 p->contrast=qcam->contrast<<8;
777 p->whiteness=qcam->whitebal<<8;
779 p->palette=VIDEO_PALETTE_GREY;
784 struct video_picture *p = arg;
785 if(p->palette!=VIDEO_PALETTE_GREY)
787 if(p->depth!=4 && p->depth!=6)
791 * Now load the camera.
794 qcam->brightness = p->brightness>>8;
795 qcam->contrast = p->contrast>>8;
796 qcam->whitebal = p->whiteness>>8;
797 qcam->bpp = p->depth;
799 mutex_lock(&qcam->lock);
800 qc_setscanmode(qcam);
801 mutex_unlock(&qcam->lock);
802 qcam->status |= QC_PARAM_CHANGE;
808 struct video_window *vw = arg;
813 if(vw->height<60||vw->height>240)
815 if(vw->width<80||vw->width>320)
820 qcam->transfer_scale = 4;
822 if(vw->width>=160 && vw->height>=120)
824 qcam->transfer_scale = 2;
826 if(vw->width>=320 && vw->height>=240)
830 qcam->transfer_scale = 1;
832 mutex_lock(&qcam->lock);
833 qc_setscanmode(qcam);
834 mutex_unlock(&qcam->lock);
836 /* We must update the camera before we grab. We could
837 just have changed the grab size */
838 qcam->status |= QC_PARAM_CHANGE;
840 /* Ok we figured out what to use from our wide choice */
845 struct video_window *vw = arg;
846 memset(vw, 0, sizeof(*vw));
847 vw->width=qcam->width/qcam->transfer_scale;
848 vw->height=qcam->height/qcam->transfer_scale;
867 static int qcam_ioctl(struct inode *inode, struct file *file,
868 unsigned int cmd, unsigned long arg)
870 return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
873 static ssize_t qcam_read(struct file *file, char __user *buf,
874 size_t count, loff_t *ppos)
876 struct video_device *v = video_devdata(file);
877 struct qcam_device *qcam=(struct qcam_device *)v;
879 parport_claim_or_block(qcam->pdev);
881 mutex_lock(&qcam->lock);
885 /* Update the camera parameters if we need to */
886 if (qcam->status & QC_PARAM_CHANGE)
889 len=qc_capture(qcam, buf,count);
891 mutex_unlock(&qcam->lock);
893 parport_release(qcam->pdev);
897 static const struct file_operations qcam_fops = {
898 .owner = THIS_MODULE,
899 .open = video_exclusive_open,
900 .release = video_exclusive_release,
903 .compat_ioctl = v4l_compat_ioctl32,
908 static struct video_device qcam_template=
910 .name = "Connectix Quickcam",
915 static struct qcam_device *qcams[MAX_CAMS];
916 static unsigned int num_cams;
918 static int init_bwqcam(struct parport *port)
920 struct qcam_device *qcam;
922 if (num_cams == MAX_CAMS)
924 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
928 qcam=qcam_init(port);
932 parport_claim_or_block(qcam->pdev);
936 if(qc_detect(qcam)==0)
938 parport_release(qcam->pdev);
939 parport_unregister_device(qcam->pdev);
945 parport_release(qcam->pdev);
947 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
949 if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
951 parport_unregister_device(qcam->pdev);
956 qcams[num_cams++] = qcam;
961 static void close_bwqcam(struct qcam_device *qcam)
963 video_unregister_device(&qcam->vdev);
964 parport_unregister_device(qcam->pdev);
968 /* The parport parameter controls which parports will be scanned.
969 * Scanning all parports causes some printers to print a garbage page.
970 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
972 static char *parport[MAX_CAMS] = { NULL, };
973 module_param_array(parport, charp, NULL, 0);
976 static int accept_bwqcam(struct parport *port)
981 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
982 /* user gave parport parameters */
983 for(n=0; parport[n] && n<MAX_CAMS; n++){
986 r = simple_strtoul(parport[n], &ep, 0);
987 if (ep == parport[n]) {
989 "bw-qcam: bad port specifier \"%s\"\n",
993 if (r == port->number)
1002 static void bwqcam_attach(struct parport *port)
1004 if (accept_bwqcam(port))
1008 static void bwqcam_detach(struct parport *port)
1011 for (i = 0; i < num_cams; i++) {
1012 struct qcam_device *qcam = qcams[i];
1013 if (qcam && qcam->pdev->port == port) {
1020 static struct parport_driver bwqcam_driver = {
1022 .attach = bwqcam_attach,
1023 .detach = bwqcam_detach,
1026 static void __exit exit_bw_qcams(void)
1028 parport_unregister_driver(&bwqcam_driver);
1031 static int __init init_bw_qcams(void)
1034 /* Do some sanity checks on the module parameters. */
1035 if (maxpoll > 5000) {
1036 printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1040 if (yieldlines < 1) {
1041 printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1045 return parport_register_driver(&bwqcam_driver);
1048 module_init(init_bw_qcams);
1049 module_exit(exit_bw_qcams);
1051 MODULE_LICENSE("GPL");