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 <asm/semaphore.h>
77 #include <asm/uaccess.h>
81 static unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */
82 static unsigned int yieldlines=4; /* Yield after this many during capture */
83 static int video_nr = -1;
85 module_param(maxpoll, int, 0);
86 module_param(yieldlines, int, 0);
87 module_param(video_nr, int, 0);
89 static inline int read_lpstatus(struct qcam_device *q)
91 return parport_read_status(q->pport);
94 static inline int read_lpdata(struct qcam_device *q)
96 return parport_read_data(q->pport);
99 static inline void write_lpdata(struct qcam_device *q, int d)
101 parport_write_data(q->pport, d);
104 static inline void write_lpcontrol(struct qcam_device *q, int d)
106 parport_write_control(q->pport, d);
109 static int qc_waithand(struct qcam_device *q, int val);
110 static int qc_command(struct qcam_device *q, int command);
111 static int qc_readparam(struct qcam_device *q);
112 static int qc_setscanmode(struct qcam_device *q);
113 static int qc_readbytes(struct qcam_device *q, char buffer[]);
115 static struct video_device qcam_template;
117 static int qc_calibrate(struct qcam_device *q)
120 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
121 * The white balance is an individiual value for each
128 qc_command(q, 27); /* AutoAdjustOffset */
129 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
131 /* GetOffset (33) will read 255 until autocalibration */
132 /* is finished. After that, a value of 1-254 will be */
137 value = qc_readparam(q);
141 } while (value == 0xff && count<2048);
147 /* Initialize the QuickCam driver control structure. This is where
148 * defaults are set for people who don't have a config file.*/
150 static struct qcam_device *qcam_init(struct parport *port)
152 struct qcam_device *q;
154 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
159 q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
163 printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
169 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
171 init_MUTEX(&q->lock);
173 q->port_mode = (QC_ANY | QC_NOTSET);
177 q->transfer_scale = 2;
184 q->status = QC_PARAM_CHANGE;
189 /* qc_command is probably a bit of a misnomer -- it's used to send
190 * bytes *to* the camera. Generally, these bytes are either commands
191 * or arguments to commands, so the name fits, but it still bugs me a
192 * bit. See the documentation for a list of commands. */
194 static int qc_command(struct qcam_device *q, int command)
199 write_lpdata(q, command);
200 write_lpcontrol(q, 6);
202 n1 = qc_waithand(q, 1);
204 write_lpcontrol(q, 0xe);
205 n2 = qc_waithand(q, 0);
207 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
211 static int qc_readparam(struct qcam_device *q)
216 write_lpcontrol(q, 6);
217 n1 = qc_waithand(q, 1);
219 write_lpcontrol(q, 0xe);
220 n2 = qc_waithand(q, 0);
222 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
226 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
227 * Almost all communication with the camera requires handshaking. */
229 static int qc_waithand(struct qcam_device *q, int val)
236 while (!((status = read_lpstatus(q)) & 8))
238 /* 1000 is enough spins on the I/O for all normal
239 cases, at that point we start to poll slowly
240 until the camera wakes up. However, we are
241 busy blocked until the camera responds, so
242 setting it lower is much better for interactive
247 msleep_interruptible(5);
249 if(runs>(maxpoll+1000)) /* 5 seconds */
255 while (((status = read_lpstatus(q)) & 8))
257 /* 1000 is enough spins on the I/O for all normal
258 cases, at that point we start to poll slowly
259 until the camera wakes up. However, we are
260 busy blocked until the camera responds, so
261 setting it lower is much better for interactive
266 msleep_interruptible(5);
268 if(runs++>(maxpoll+1000)) /* 5 seconds */
276 /* Waithand2 is used when the qcam is in bidirectional mode, and the
277 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
278 * (bit 3 of status register). It also returns the last value read,
279 * since this data is useful. */
281 static unsigned int qc_waithand2(struct qcam_device *q, int val)
288 status = read_lpdata(q);
289 /* 1000 is enough spins on the I/O for all normal
290 cases, at that point we start to poll slowly
291 until the camera wakes up. However, we are
292 busy blocked until the camera responds, so
293 setting it lower is much better for interactive
298 msleep_interruptible(5);
300 if(runs++>(maxpoll+1000)) /* 5 seconds */
303 while ((status & 1) != val);
309 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
310 the status register at 5-10 Hz. This is only used in the autoprobe
311 code. Be aware that this isn't the way Connectix detects the
312 camera (they send a reset and try to handshake), but this should be
313 almost completely safe, while their method screws up my printer if
314 I plug it in before the camera. */
316 static int qc_detect(struct qcam_device *q)
322 lastreg = reg = read_lpstatus(q) & 0xf0;
324 for (i = 0; i < 500; i++)
326 reg = read_lpstatus(q) & 0xf0;
335 /* Force camera detection during testing. Sometimes the camera
336 won't be flashing these bits. Possibly unloading the module
337 in the middle of a grab? Or some timeout condition?
338 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
339 printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
343 /* Be (even more) liberal in what you accept... */
345 /* if (count > 30 && count < 200) */
346 if (count > 20 && count < 300)
347 return 1; /* found */
349 return 0; /* not found */
353 /* Reset the QuickCam. This uses the same sequence the Windows
354 * QuickPic program uses. Someone with a bi-directional port should
355 * check that bi-directional mode is detected right, and then
356 * implement bi-directional mode in qc_readbyte(). */
358 static void qc_reset(struct qcam_device *q)
360 switch (q->port_mode & QC_FORCE_MASK)
362 case QC_FORCE_UNIDIR:
363 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
367 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
371 write_lpcontrol(q, 0x20);
372 write_lpdata(q, 0x75);
374 if (read_lpdata(q) != 0x75) {
375 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
377 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
382 write_lpcontrol(q, 0xb);
384 write_lpcontrol(q, 0xe);
385 qc_setscanmode(q); /* in case port_mode changed */
389 /* Decide which scan mode to use. There's no real requirement that
390 * the scanmode match the resolution in q->height and q-> width -- the
391 * camera takes the picture at the resolution specified in the
392 * "scanmode" and then returns the image at the resolution specified
393 * with the resolution commands. If the scan is bigger than the
394 * requested resolution, the upper-left hand corner of the scan is
395 * returned. If the scan is smaller, then the rest of the image
396 * returned contains garbage. */
398 static int qc_setscanmode(struct qcam_device *q)
400 int old_mode = q->mode;
402 switch (q->transfer_scale)
424 switch (q->port_mode & QC_MODE_MASK)
434 if (q->mode != old_mode)
435 q->status |= QC_PARAM_CHANGE;
441 /* Reset the QuickCam and program for brightness, contrast,
442 * white-balance, and resolution. */
444 static void qc_set(struct qcam_device *q)
451 /* Set the brightness. Yes, this is repetitive, but it works.
452 * Shorter versions seem to fail subtly. Feel free to try :-). */
453 /* I think the problem was in qc_command, not here -- bls */
456 qc_command(q, q->brightness);
458 val = q->height / q->transfer_scale;
461 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
462 /* The normal "transfers per line" calculation doesn't seem to work
463 as expected here (and yet it works fine in qc_scan). No idea
464 why this case is the odd man out. Fortunately, Laird's original
465 working version gives me a good way to guess at working values.
468 val2 = q->transfer_scale * 4;
470 val = q->width * q->bpp;
471 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
474 val = (val + val2 - 1) / val2;
478 /* Setting top and left -- bls */
480 qc_command(q, q->top);
482 qc_command(q, q->left / 2);
485 qc_command(q, q->contrast);
487 qc_command(q, q->whitebal);
489 /* Clear flag that we must update the grabbing parameters on the camera
490 before we grab the next frame */
491 q->status &= (~QC_PARAM_CHANGE);
494 /* Qc_readbytes reads some bytes from the QC and puts them in
495 the supplied buffer. It returns the number of bytes read,
498 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
502 unsigned int hi2, lo2;
503 static int state = 0;
511 switch (q->port_mode & QC_MODE_MASK)
513 case QC_BIDIR: /* Bi-directional Port */
514 write_lpcontrol(q, 0x26);
515 lo = (qc_waithand2(q, 1) >> 1);
516 hi = (read_lpstatus(q) >> 3) & 0x1f;
517 write_lpcontrol(q, 0x2e);
518 lo2 = (qc_waithand2(q, 0) >> 1);
519 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
523 buffer[0] = lo & 0xf;
524 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
525 buffer[2] = (hi & 0x1e) >> 1;
526 buffer[3] = lo2 & 0xf;
527 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
528 buffer[5] = (hi2 & 0x1e) >> 1;
532 buffer[0] = lo & 0x3f;
533 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
534 buffer[2] = lo2 & 0x3f;
535 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
541 case QC_UNIDIR: /* Unidirectional Port */
542 write_lpcontrol(q, 6);
543 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
544 write_lpcontrol(q, 0xe);
545 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
558 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
559 q->saved_bits = (hi & 3) << 4;
564 buffer[0] = lo | q->saved_bits;
565 q->saved_bits = hi << 2;
570 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
571 buffer[1] = ((lo & 3) << 4) | hi;
583 /* requests a scan from the camera. It sends the correct instructions
584 * to the camera and then reads back the correct number of bytes. In
585 * previous versions of this routine the return structure contained
586 * the raw output from the camera, and there was a 'qc_convertscan'
587 * function that converted that to a useful format. In version 0.3 I
588 * rolled qc_convertscan into qc_scan and now I only return the
589 * converted scan. The format is just an one-dimensional array of
590 * characters, one for each pixel, with 0=black up to n=white, where
591 * n=2^(bit depth)-1. Ask me for more details if you don't understand
594 static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len)
598 int linestotrans, transperline;
611 qc_command(q, q->mode);
613 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
615 write_lpcontrol(q, 0x2e); /* turn port around */
616 write_lpcontrol(q, 0x26);
617 (void) qc_waithand(q, 1);
618 write_lpcontrol(q, 0x2e);
619 (void) qc_waithand(q, 0);
622 /* strange -- should be 15:63 below, but 4bpp is odd */
623 invert = (q->bpp == 4) ? 16 : 63;
625 linestotrans = q->height / q->transfer_scale;
626 pixels_per_line = q->width / q->transfer_scale;
627 transperline = q->width * q->bpp;
628 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
630 transperline = (transperline + divisor - 1) / divisor;
632 for (i = 0, yield = yieldlines; i < linestotrans; i++)
634 for (pixels_read = j = 0; j < transperline; j++)
636 bytes = qc_readbytes(q, buffer);
637 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++)
640 if (buffer[k] == 0 && invert == 16)
642 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
643 must be 0-15 -- bls */
646 o=i*pixels_per_line + pixels_read + k;
650 put_user((invert - buffer[k])<<shift, buf+o);
653 pixels_read += bytes;
655 (void) qc_readbytes(q, NULL); /* reset state machine */
657 /* Grabbing an entire frame from the quickcam is a lengthy
658 process. We don't (usually) want to busy-block the
659 processor for the entire frame. yieldlines is a module
660 parameter. If we yield every line, the minimum frame
661 time will be 240 / 200 = 1.2 seconds. The compile-time
662 default is to yield every 4 lines. */
664 msleep_interruptible(5);
665 yield = i + yieldlines;
669 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
671 write_lpcontrol(q, 2);
672 write_lpcontrol(q, 6);
674 write_lpcontrol(q, 0xe);
682 * Video4linux interfacing
685 static int qcam_do_ioctl(struct inode *inode, struct file *file,
686 unsigned int cmd, void *arg)
688 struct video_device *dev = video_devdata(file);
689 struct qcam_device *qcam=(struct qcam_device *)dev;
695 struct video_capability *b = arg;
696 strcpy(b->name, "Quickcam");
697 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
708 struct video_channel *v = arg;
713 /* Good question.. its composite or SVHS so.. */
714 v->type = VIDEO_TYPE_CAMERA;
715 strcpy(v->name, "Camera");
720 struct video_channel *v = arg;
727 struct video_tuner *v = arg;
730 strcpy(v->name, "Format");
734 v->mode = VIDEO_MODE_AUTO;
739 struct video_tuner *v = arg;
742 if(v->mode!=VIDEO_MODE_AUTO)
748 struct video_picture *p = arg;
751 p->brightness=qcam->brightness<<8;
752 p->contrast=qcam->contrast<<8;
753 p->whiteness=qcam->whitebal<<8;
755 p->palette=VIDEO_PALETTE_GREY;
760 struct video_picture *p = arg;
761 if(p->palette!=VIDEO_PALETTE_GREY)
763 if(p->depth!=4 && p->depth!=6)
767 * Now load the camera.
770 qcam->brightness = p->brightness>>8;
771 qcam->contrast = p->contrast>>8;
772 qcam->whitebal = p->whiteness>>8;
773 qcam->bpp = p->depth;
776 qc_setscanmode(qcam);
778 qcam->status |= QC_PARAM_CHANGE;
784 struct video_window *vw = arg;
789 if(vw->height<60||vw->height>240)
791 if(vw->width<80||vw->width>320)
796 qcam->transfer_scale = 4;
798 if(vw->width>=160 && vw->height>=120)
800 qcam->transfer_scale = 2;
802 if(vw->width>=320 && vw->height>=240)
806 qcam->transfer_scale = 1;
809 qc_setscanmode(qcam);
812 /* We must update the camera before we grab. We could
813 just have changed the grab size */
814 qcam->status |= QC_PARAM_CHANGE;
816 /* Ok we figured out what to use from our wide choice */
821 struct video_window *vw = arg;
822 memset(vw, 0, sizeof(*vw));
823 vw->width=qcam->width/qcam->transfer_scale;
824 vw->height=qcam->height/qcam->transfer_scale;
843 static int qcam_ioctl(struct inode *inode, struct file *file,
844 unsigned int cmd, unsigned long arg)
846 return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
849 static ssize_t qcam_read(struct file *file, char __user *buf,
850 size_t count, loff_t *ppos)
852 struct video_device *v = video_devdata(file);
853 struct qcam_device *qcam=(struct qcam_device *)v;
855 parport_claim_or_block(qcam->pdev);
861 /* Update the camera parameters if we need to */
862 if (qcam->status & QC_PARAM_CHANGE)
865 len=qc_capture(qcam, buf,count);
869 parport_release(qcam->pdev);
873 static struct file_operations qcam_fops = {
874 .owner = THIS_MODULE,
875 .open = video_exclusive_open,
876 .release = video_exclusive_release,
878 .compat_ioctl = v4l_compat_ioctl32,
882 static struct video_device qcam_template=
884 .owner = THIS_MODULE,
885 .name = "Connectix Quickcam",
886 .type = VID_TYPE_CAPTURE,
887 .hardware = VID_HARDWARE_QCAM_BW,
892 static struct qcam_device *qcams[MAX_CAMS];
893 static unsigned int num_cams = 0;
895 static int init_bwqcam(struct parport *port)
897 struct qcam_device *qcam;
899 if (num_cams == MAX_CAMS)
901 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
905 qcam=qcam_init(port);
909 parport_claim_or_block(qcam->pdev);
913 if(qc_detect(qcam)==0)
915 parport_release(qcam->pdev);
916 parport_unregister_device(qcam->pdev);
922 parport_release(qcam->pdev);
924 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
926 if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
928 parport_unregister_device(qcam->pdev);
933 qcams[num_cams++] = qcam;
938 static void close_bwqcam(struct qcam_device *qcam)
940 video_unregister_device(&qcam->vdev);
941 parport_unregister_device(qcam->pdev);
945 /* The parport parameter controls which parports will be scanned.
946 * Scanning all parports causes some printers to print a garbage page.
947 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
949 static char *parport[MAX_CAMS] = { NULL, };
950 module_param_array(parport, charp, NULL, 0);
953 static int accept_bwqcam(struct parport *port)
958 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
959 /* user gave parport parameters */
960 for(n=0; parport[n] && n<MAX_CAMS; n++){
963 r = simple_strtoul(parport[n], &ep, 0);
964 if (ep == parport[n]) {
966 "bw-qcam: bad port specifier \"%s\"\n",
970 if (r == port->number)
979 static void bwqcam_attach(struct parport *port)
981 if (accept_bwqcam(port))
985 static void bwqcam_detach(struct parport *port)
988 for (i = 0; i < num_cams; i++) {
989 struct qcam_device *qcam = qcams[i];
990 if (qcam && qcam->pdev->port == port) {
997 static struct parport_driver bwqcam_driver = {
999 .attach = bwqcam_attach,
1000 .detach = bwqcam_detach,
1003 static void __exit exit_bw_qcams(void)
1005 parport_unregister_driver(&bwqcam_driver);
1008 static int __init init_bw_qcams(void)
1011 /* Do some sanity checks on the module parameters. */
1012 if (maxpoll > 5000) {
1013 printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1017 if (yieldlines < 1) {
1018 printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1022 return parport_register_driver(&bwqcam_driver);
1025 module_init(init_bw_qcams);
1026 module_exit(exit_bw_qcams);
1028 MODULE_LICENSE("GPL");