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 <linux/mutex.h>
78 #include <asm/uaccess.h>
82 static unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */
83 static unsigned int yieldlines=4; /* Yield after this many during capture */
84 static int video_nr = -1;
85 static unsigned int force_init; /* Whether to probe aggressively */
87 module_param(maxpoll, int, 0);
88 module_param(yieldlines, int, 0);
89 module_param(video_nr, int, 0);
91 /* Set force_init=1 to avoid detection by polling status register and
92 * immediately attempt to initialize qcam */
93 module_param(force_init, int, 0);
95 static inline int read_lpstatus(struct qcam_device *q)
97 return parport_read_status(q->pport);
100 static inline int read_lpdata(struct qcam_device *q)
102 return parport_read_data(q->pport);
105 static inline void write_lpdata(struct qcam_device *q, int d)
107 parport_write_data(q->pport, d);
110 static inline void write_lpcontrol(struct qcam_device *q, int d)
113 /* Set bidirectional mode to reverse (data in) */
114 parport_data_reverse(q->pport);
116 /* Set bidirectional mode to forward (data out) */
117 parport_data_forward(q->pport);
120 /* Now issue the regular port command, but strip out the
123 parport_write_control(q->pport, d);
126 static int qc_waithand(struct qcam_device *q, int val);
127 static int qc_command(struct qcam_device *q, int command);
128 static int qc_readparam(struct qcam_device *q);
129 static int qc_setscanmode(struct qcam_device *q);
130 static int qc_readbytes(struct qcam_device *q, char buffer[]);
132 static struct video_device qcam_template;
134 static int qc_calibrate(struct qcam_device *q)
137 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
138 * The white balance is an individiual value for each
145 qc_command(q, 27); /* AutoAdjustOffset */
146 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
148 /* GetOffset (33) will read 255 until autocalibration */
149 /* is finished. After that, a value of 1-254 will be */
154 value = qc_readparam(q);
158 } while (value == 0xff && count<2048);
164 /* Initialize the QuickCam driver control structure. This is where
165 * defaults are set for people who don't have a config file.*/
167 static struct qcam_device *qcam_init(struct parport *port)
169 struct qcam_device *q;
171 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
176 q->pdev = parport_register_device(port, "bw-qcam", NULL, 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)
253 while (!((status = read_lpstatus(q)) & 8))
255 /* 1000 is enough spins on the I/O for all normal
256 cases, at that point we start to poll slowly
257 until the camera wakes up. However, we are
258 busy blocked until the camera responds, so
259 setting it lower is much better for interactive
264 msleep_interruptible(5);
266 if(runs>(maxpoll+1000)) /* 5 seconds */
272 while (((status = read_lpstatus(q)) & 8))
274 /* 1000 is enough spins on the I/O for all normal
275 cases, at that point we start to poll slowly
276 until the camera wakes up. However, we are
277 busy blocked until the camera responds, so
278 setting it lower is much better for interactive
283 msleep_interruptible(5);
285 if(runs++>(maxpoll+1000)) /* 5 seconds */
293 /* Waithand2 is used when the qcam is in bidirectional mode, and the
294 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
295 * (bit 3 of status register). It also returns the last value read,
296 * since this data is useful. */
298 static unsigned int qc_waithand2(struct qcam_device *q, int val)
305 status = read_lpdata(q);
306 /* 1000 is enough spins on the I/O for all normal
307 cases, at that point we start to poll slowly
308 until the camera wakes up. However, we are
309 busy blocked until the camera responds, so
310 setting it lower is much better for interactive
315 msleep_interruptible(5);
317 if(runs++>(maxpoll+1000)) /* 5 seconds */
320 while ((status & 1) != val);
326 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
327 the status register at 5-10 Hz. This is only used in the autoprobe
328 code. Be aware that this isn't the way Connectix detects the
329 camera (they send a reset and try to handshake), but this should be
330 almost completely safe, while their method screws up my printer if
331 I plug it in before the camera. */
333 static int qc_detect(struct qcam_device *q)
342 lastreg = reg = read_lpstatus(q) & 0xf0;
344 for (i = 0; i < 500; i++)
346 reg = read_lpstatus(q) & 0xf0;
355 /* Force camera detection during testing. Sometimes the camera
356 won't be flashing these bits. Possibly unloading the module
357 in the middle of a grab? Or some timeout condition?
358 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
359 printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
363 /* Be (even more) liberal in what you accept... */
365 if (count > 20 && count < 400) {
366 return 1; /* found */
368 printk(KERN_ERR "No Quickcam found on port %s\n",
370 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
371 return 0; /* not found */
376 /* Reset the QuickCam. This uses the same sequence the Windows
377 * QuickPic program uses. Someone with a bi-directional port should
378 * check that bi-directional mode is detected right, and then
379 * implement bi-directional mode in qc_readbyte(). */
381 static void qc_reset(struct qcam_device *q)
383 switch (q->port_mode & QC_FORCE_MASK)
385 case QC_FORCE_UNIDIR:
386 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
390 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
394 write_lpcontrol(q, 0x20);
395 write_lpdata(q, 0x75);
397 if (read_lpdata(q) != 0x75) {
398 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
400 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
405 write_lpcontrol(q, 0xb);
407 write_lpcontrol(q, 0xe);
408 qc_setscanmode(q); /* in case port_mode changed */
412 /* Decide which scan mode to use. There's no real requirement that
413 * the scanmode match the resolution in q->height and q-> width -- the
414 * camera takes the picture at the resolution specified in the
415 * "scanmode" and then returns the image at the resolution specified
416 * with the resolution commands. If the scan is bigger than the
417 * requested resolution, the upper-left hand corner of the scan is
418 * returned. If the scan is smaller, then the rest of the image
419 * returned contains garbage. */
421 static int qc_setscanmode(struct qcam_device *q)
423 int old_mode = q->mode;
425 switch (q->transfer_scale)
447 switch (q->port_mode & QC_MODE_MASK)
457 if (q->mode != old_mode)
458 q->status |= QC_PARAM_CHANGE;
464 /* Reset the QuickCam and program for brightness, contrast,
465 * white-balance, and resolution. */
467 static void qc_set(struct qcam_device *q)
474 /* Set the brightness. Yes, this is repetitive, but it works.
475 * Shorter versions seem to fail subtly. Feel free to try :-). */
476 /* I think the problem was in qc_command, not here -- bls */
479 qc_command(q, q->brightness);
481 val = q->height / q->transfer_scale;
484 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
485 /* The normal "transfers per line" calculation doesn't seem to work
486 as expected here (and yet it works fine in qc_scan). No idea
487 why this case is the odd man out. Fortunately, Laird's original
488 working version gives me a good way to guess at working values.
491 val2 = q->transfer_scale * 4;
493 val = q->width * q->bpp;
494 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
497 val = (val + val2 - 1) / val2;
501 /* Setting top and left -- bls */
503 qc_command(q, q->top);
505 qc_command(q, q->left / 2);
508 qc_command(q, q->contrast);
510 qc_command(q, q->whitebal);
512 /* Clear flag that we must update the grabbing parameters on the camera
513 before we grab the next frame */
514 q->status &= (~QC_PARAM_CHANGE);
517 /* Qc_readbytes reads some bytes from the QC and puts them in
518 the supplied buffer. It returns the number of bytes read,
521 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
525 unsigned int hi2, lo2;
534 switch (q->port_mode & QC_MODE_MASK)
536 case QC_BIDIR: /* Bi-directional Port */
537 write_lpcontrol(q, 0x26);
538 lo = (qc_waithand2(q, 1) >> 1);
539 hi = (read_lpstatus(q) >> 3) & 0x1f;
540 write_lpcontrol(q, 0x2e);
541 lo2 = (qc_waithand2(q, 0) >> 1);
542 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
546 buffer[0] = lo & 0xf;
547 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
548 buffer[2] = (hi & 0x1e) >> 1;
549 buffer[3] = lo2 & 0xf;
550 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
551 buffer[5] = (hi2 & 0x1e) >> 1;
555 buffer[0] = lo & 0x3f;
556 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
557 buffer[2] = lo2 & 0x3f;
558 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
564 case QC_UNIDIR: /* Unidirectional Port */
565 write_lpcontrol(q, 6);
566 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
567 write_lpcontrol(q, 0xe);
568 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
581 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
582 q->saved_bits = (hi & 3) << 4;
587 buffer[0] = lo | q->saved_bits;
588 q->saved_bits = hi << 2;
593 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
594 buffer[1] = ((lo & 3) << 4) | hi;
606 /* requests a scan from the camera. It sends the correct instructions
607 * to the camera and then reads back the correct number of bytes. In
608 * previous versions of this routine the return structure contained
609 * the raw output from the camera, and there was a 'qc_convertscan'
610 * function that converted that to a useful format. In version 0.3 I
611 * rolled qc_convertscan into qc_scan and now I only return the
612 * converted scan. The format is just an one-dimensional array of
613 * characters, one for each pixel, with 0=black up to n=white, where
614 * n=2^(bit depth)-1. Ask me for more details if you don't understand
617 static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len)
621 int linestotrans, transperline;
634 qc_command(q, q->mode);
636 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
638 write_lpcontrol(q, 0x2e); /* turn port around */
639 write_lpcontrol(q, 0x26);
640 (void) qc_waithand(q, 1);
641 write_lpcontrol(q, 0x2e);
642 (void) qc_waithand(q, 0);
645 /* strange -- should be 15:63 below, but 4bpp is odd */
646 invert = (q->bpp == 4) ? 16 : 63;
648 linestotrans = q->height / q->transfer_scale;
649 pixels_per_line = q->width / q->transfer_scale;
650 transperline = q->width * q->bpp;
651 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
653 transperline = (transperline + divisor - 1) / divisor;
655 for (i = 0, yield = yieldlines; i < linestotrans; i++)
657 for (pixels_read = j = 0; j < transperline; j++)
659 bytes = qc_readbytes(q, buffer);
660 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++)
663 if (buffer[k] == 0 && invert == 16)
665 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
666 must be 0-15 -- bls */
669 o=i*pixels_per_line + pixels_read + k;
673 put_user((invert - buffer[k])<<shift, buf+o);
676 pixels_read += bytes;
678 (void) qc_readbytes(q, NULL); /* reset state machine */
680 /* Grabbing an entire frame from the quickcam is a lengthy
681 process. We don't (usually) want to busy-block the
682 processor for the entire frame. yieldlines is a module
683 parameter. If we yield every line, the minimum frame
684 time will be 240 / 200 = 1.2 seconds. The compile-time
685 default is to yield every 4 lines. */
687 msleep_interruptible(5);
688 yield = i + yieldlines;
692 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
694 write_lpcontrol(q, 2);
695 write_lpcontrol(q, 6);
697 write_lpcontrol(q, 0xe);
705 * Video4linux interfacing
708 static int qcam_do_ioctl(struct inode *inode, struct file *file,
709 unsigned int cmd, void *arg)
711 struct video_device *dev = video_devdata(file);
712 struct qcam_device *qcam=(struct qcam_device *)dev;
718 struct video_capability *b = arg;
719 strcpy(b->name, "Quickcam");
720 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
731 struct video_channel *v = arg;
736 /* Good question.. its composite or SVHS so.. */
737 v->type = VIDEO_TYPE_CAMERA;
738 strcpy(v->name, "Camera");
743 struct video_channel *v = arg;
750 struct video_tuner *v = arg;
753 strcpy(v->name, "Format");
757 v->mode = VIDEO_MODE_AUTO;
762 struct video_tuner *v = arg;
765 if(v->mode!=VIDEO_MODE_AUTO)
771 struct video_picture *p = arg;
774 p->brightness=qcam->brightness<<8;
775 p->contrast=qcam->contrast<<8;
776 p->whiteness=qcam->whitebal<<8;
778 p->palette=VIDEO_PALETTE_GREY;
783 struct video_picture *p = arg;
784 if(p->palette!=VIDEO_PALETTE_GREY)
786 if(p->depth!=4 && p->depth!=6)
790 * Now load the camera.
793 qcam->brightness = p->brightness>>8;
794 qcam->contrast = p->contrast>>8;
795 qcam->whitebal = p->whiteness>>8;
796 qcam->bpp = p->depth;
798 mutex_lock(&qcam->lock);
799 qc_setscanmode(qcam);
800 mutex_unlock(&qcam->lock);
801 qcam->status |= QC_PARAM_CHANGE;
807 struct video_window *vw = arg;
812 if(vw->height<60||vw->height>240)
814 if(vw->width<80||vw->width>320)
819 qcam->transfer_scale = 4;
821 if(vw->width>=160 && vw->height>=120)
823 qcam->transfer_scale = 2;
825 if(vw->width>=320 && vw->height>=240)
829 qcam->transfer_scale = 1;
831 mutex_lock(&qcam->lock);
832 qc_setscanmode(qcam);
833 mutex_unlock(&qcam->lock);
835 /* We must update the camera before we grab. We could
836 just have changed the grab size */
837 qcam->status |= QC_PARAM_CHANGE;
839 /* Ok we figured out what to use from our wide choice */
844 struct video_window *vw = arg;
845 memset(vw, 0, sizeof(*vw));
846 vw->width=qcam->width/qcam->transfer_scale;
847 vw->height=qcam->height/qcam->transfer_scale;
866 static int qcam_ioctl(struct inode *inode, struct file *file,
867 unsigned int cmd, unsigned long arg)
869 return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
872 static ssize_t qcam_read(struct file *file, char __user *buf,
873 size_t count, loff_t *ppos)
875 struct video_device *v = video_devdata(file);
876 struct qcam_device *qcam=(struct qcam_device *)v;
878 parport_claim_or_block(qcam->pdev);
880 mutex_lock(&qcam->lock);
884 /* Update the camera parameters if we need to */
885 if (qcam->status & QC_PARAM_CHANGE)
888 len=qc_capture(qcam, buf,count);
890 mutex_unlock(&qcam->lock);
892 parport_release(qcam->pdev);
896 static const struct file_operations qcam_fops = {
897 .owner = THIS_MODULE,
898 .open = video_exclusive_open,
899 .release = video_exclusive_release,
901 .compat_ioctl = v4l_compat_ioctl32,
905 static struct video_device qcam_template=
907 .owner = THIS_MODULE,
908 .name = "Connectix Quickcam",
909 .type = VID_TYPE_CAPTURE,
914 static struct qcam_device *qcams[MAX_CAMS];
915 static unsigned int num_cams;
917 static int init_bwqcam(struct parport *port)
919 struct qcam_device *qcam;
921 if (num_cams == MAX_CAMS)
923 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
927 qcam=qcam_init(port);
931 parport_claim_or_block(qcam->pdev);
935 if(qc_detect(qcam)==0)
937 parport_release(qcam->pdev);
938 parport_unregister_device(qcam->pdev);
944 parport_release(qcam->pdev);
946 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
948 if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
950 parport_unregister_device(qcam->pdev);
955 qcams[num_cams++] = qcam;
960 static void close_bwqcam(struct qcam_device *qcam)
962 video_unregister_device(&qcam->vdev);
963 parport_unregister_device(qcam->pdev);
967 /* The parport parameter controls which parports will be scanned.
968 * Scanning all parports causes some printers to print a garbage page.
969 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
971 static char *parport[MAX_CAMS] = { NULL, };
972 module_param_array(parport, charp, NULL, 0);
975 static int accept_bwqcam(struct parport *port)
980 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
981 /* user gave parport parameters */
982 for(n=0; parport[n] && n<MAX_CAMS; n++){
985 r = simple_strtoul(parport[n], &ep, 0);
986 if (ep == parport[n]) {
988 "bw-qcam: bad port specifier \"%s\"\n",
992 if (r == port->number)
1001 static void bwqcam_attach(struct parport *port)
1003 if (accept_bwqcam(port))
1007 static void bwqcam_detach(struct parport *port)
1010 for (i = 0; i < num_cams; i++) {
1011 struct qcam_device *qcam = qcams[i];
1012 if (qcam && qcam->pdev->port == port) {
1019 static struct parport_driver bwqcam_driver = {
1021 .attach = bwqcam_attach,
1022 .detach = bwqcam_detach,
1025 static void __exit exit_bw_qcams(void)
1027 parport_unregister_driver(&bwqcam_driver);
1030 static int __init init_bw_qcams(void)
1033 /* Do some sanity checks on the module parameters. */
1034 if (maxpoll > 5000) {
1035 printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1039 if (yieldlines < 1) {
1040 printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1044 return parport_register_driver(&bwqcam_driver);
1047 module_init(init_bw_qcams);
1048 module_exit(exit_bw_qcams);
1050 MODULE_LICENSE("GPL");