2 * Video4Linux Colour QuickCam driver
3 * Copyright 1997-2000 Philip Blundell <philb@gnu.org>
7 * parport=auto -- probe all parports (default)
8 * parport=0 -- parport0 becomes qcam1
9 * parport=2,0,1 -- parports 2,0,1 are tried in that order
11 * probe=0 -- do no probing, assume camera is present
12 * probe=1 -- use IEEE-1284 autoprobe data only (default)
13 * probe=2 -- probe aggressively for cameras
15 * force_rgb=1 -- force data format to RGB (default is BGR)
17 * The parport parameter controls which parports will be scanned.
18 * Scanning all parports causes some printers to print a garbage page.
19 * -- March 14, 1999 Billy Donahue <billy@escape.com>
21 * Fixed data format to BGR, added force_rgb parameter. Added missing
22 * parport_unregister_driver() on module removal.
23 * -- May 28, 2000 Claudio Matsuoka <claudio@conectiva.com>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/parport.h>
35 #include <linux/sched.h>
36 #include <linux/version.h>
37 #include <linux/videodev.h>
38 #include <asm/semaphore.h>
39 #include <asm/uaccess.h>
42 struct video_device vdev;
43 struct pardevice *pdev;
44 struct parport *pport;
46 int ccd_width, ccd_height;
48 int contrast, brightness, whitebal;
50 unsigned int bidirectional;
51 struct semaphore lock;
57 /* The three possible QuickCam modes */
58 #define QC_MILLIONS 0x18
59 #define QC_BILLIONS 0x10
60 #define QC_THOUSANDS 0x08 /* with VIDEC compression (not supported) */
62 /* The three possible decimations */
63 #define QC_DECIMATION_1 0
64 #define QC_DECIMATION_2 2
65 #define QC_DECIMATION_4 4
67 #define BANNER "Colour QuickCam for Video4Linux v0.05"
69 static int parport[MAX_CAMS] = { [1 ... MAX_CAMS-1] = -1 };
71 static int force_rgb = 0;
72 static int video_nr = -1;
74 static inline void qcam_set_ack(struct qcam_device *qcam, unsigned int i)
76 /* note: the QC specs refer to the PCAck pin by voltage, not
77 software level. PC ports have builtin inverters. */
78 parport_frob_control(qcam->pport, 8, i?8:0);
81 static inline unsigned int qcam_ready1(struct qcam_device *qcam)
83 return (parport_read_status(qcam->pport) & 0x8)?1:0;
86 static inline unsigned int qcam_ready2(struct qcam_device *qcam)
88 return (parport_read_data(qcam->pport) & 0x1)?1:0;
91 static unsigned int qcam_await_ready1(struct qcam_device *qcam,
94 unsigned long oldjiffies = jiffies;
97 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
98 if (qcam_ready1(qcam) == value)
101 /* If the camera didn't respond within 1/25 second, poll slowly
103 for (i = 0; i < 50; i++)
105 if (qcam_ready1(qcam) == value)
107 current->state=TASK_INTERRUPTIBLE;
108 schedule_timeout(HZ/10);
111 /* Probably somebody pulled the plug out. Not much we can do. */
112 printk(KERN_ERR "c-qcam: ready1 timeout (%d) %x %x\n", value,
113 parport_read_status(qcam->pport),
114 parport_read_control(qcam->pport));
118 static unsigned int qcam_await_ready2(struct qcam_device *qcam, int value)
120 unsigned long oldjiffies = jiffies;
123 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
124 if (qcam_ready2(qcam) == value)
127 /* If the camera didn't respond within 1/25 second, poll slowly
129 for (i = 0; i < 50; i++)
131 if (qcam_ready2(qcam) == value)
133 current->state=TASK_INTERRUPTIBLE;
134 schedule_timeout(HZ/10);
137 /* Probably somebody pulled the plug out. Not much we can do. */
138 printk(KERN_ERR "c-qcam: ready2 timeout (%d) %x %x %x\n", value,
139 parport_read_status(qcam->pport),
140 parport_read_control(qcam->pport),
141 parport_read_data(qcam->pport));
145 static int qcam_read_data(struct qcam_device *qcam)
148 qcam_set_ack(qcam, 0);
149 if (qcam_await_ready1(qcam, 1)) return -1;
150 idata = parport_read_status(qcam->pport) & 0xf0;
151 qcam_set_ack(qcam, 1);
152 if (qcam_await_ready1(qcam, 0)) return -1;
153 idata |= (parport_read_status(qcam->pport) >> 4);
157 static int qcam_write_data(struct qcam_device *qcam, unsigned int data)
160 parport_write_data(qcam->pport, data);
161 idata = qcam_read_data(qcam);
164 printk(KERN_WARNING "cqcam: sent %x but received %x\n", data,
171 static inline int qcam_set(struct qcam_device *qcam, unsigned int cmd, unsigned int data)
173 if (qcam_write_data(qcam, cmd))
175 if (qcam_write_data(qcam, data))
180 static inline int qcam_get(struct qcam_device *qcam, unsigned int cmd)
182 if (qcam_write_data(qcam, cmd))
184 return qcam_read_data(qcam);
187 static int qc_detect(struct qcam_device *qcam)
189 unsigned int stat, ostat, i, count = 0;
191 /* The probe routine below is not very reliable. The IEEE-1284
192 probe takes precedence. */
193 /* XXX Currently parport provides no way to distinguish between
194 "the IEEE probe was not done" and "the probe was done, but
195 no device was found". Fix this one day. */
196 if (qcam->pport->probe_info[0].class == PARPORT_CLASS_MEDIA
197 && qcam->pport->probe_info[0].model
198 && !strcmp(qcam->pdev->port->probe_info[0].model,
199 "Color QuickCam 2.0")) {
200 printk(KERN_DEBUG "QuickCam: Found by IEEE1284 probe.\n");
207 parport_write_control(qcam->pport, 0xc);
209 /* look for a heartbeat */
210 ostat = stat = parport_read_status(qcam->pport);
211 for (i=0; i<250; i++)
214 stat = parport_read_status(qcam->pport);
217 if (++count >= 3) return 1;
222 /* Reset the camera and try again */
223 parport_write_control(qcam->pport, 0xc);
224 parport_write_control(qcam->pport, 0x8);
226 parport_write_control(qcam->pport, 0xc);
230 ostat = stat = parport_read_status(qcam->pport);
231 for (i=0; i<250; i++)
234 stat = parport_read_status(qcam->pport);
237 if (++count >= 3) return 1;
242 /* no (or flatline) camera, give up */
246 static void qc_reset(struct qcam_device *qcam)
248 parport_write_control(qcam->pport, 0xc);
249 parport_write_control(qcam->pport, 0x8);
251 parport_write_control(qcam->pport, 0xc);
255 /* Reset the QuickCam and program for brightness, contrast,
256 * white-balance, and resolution. */
258 static void qc_setup(struct qcam_device *q)
262 /* Set the brightness. */
263 qcam_set(q, 11, q->brightness);
265 /* Set the height and width. These refer to the actual
266 CCD area *before* applying the selected decimation. */
267 qcam_set(q, 17, q->ccd_height);
268 qcam_set(q, 19, q->ccd_width / 2);
270 /* Set top and left. */
271 qcam_set(q, 0xd, q->top);
272 qcam_set(q, 0xf, q->left);
274 /* Set contrast and white balance. */
275 qcam_set(q, 0x19, q->contrast);
276 qcam_set(q, 0x1f, q->whitebal);
282 /* Read some bytes from the camera and put them in the buffer.
283 nbytes should be a multiple of 3, because bidirectional mode gives
284 us three bytes at a time. */
286 static unsigned int qcam_read_bytes(struct qcam_device *q, unsigned char *buf, unsigned int nbytes)
288 unsigned int bytes = 0;
291 if (q->bidirectional)
293 /* It's a bidirectional port */
294 while (bytes < nbytes)
296 unsigned int lo1, hi1, lo2, hi2;
297 unsigned char r, g, b;
299 if (qcam_await_ready2(q, 1)) return bytes;
300 lo1 = parport_read_data(q->pport) >> 1;
301 hi1 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
303 if (qcam_await_ready2(q, 0)) return bytes;
304 lo2 = parport_read_data(q->pport) >> 1;
305 hi2 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
307 r = (lo1 | ((hi1 & 1)<<7));
308 g = ((hi1 & 0x1e)<<3) | ((hi2 & 0x1e)>>1);
309 b = (lo2 | ((hi2 & 1)<<7));
323 /* It's a unidirectional port */
324 int i = 0, n = bytes;
325 unsigned char rgb[3];
327 while (bytes < nbytes)
331 if (qcam_await_ready1(q, 1)) return bytes;
332 hi = (parport_read_status(q->pport) & 0xf0);
334 if (qcam_await_ready1(q, 0)) return bytes;
335 lo = (parport_read_status(q->pport) & 0xf0);
338 rgb[(i = bytes++ % 3)] = (hi | (lo >> 4)) ^ 0x88;
362 static long qc_capture(struct qcam_device *q, char *buf, unsigned long len)
364 unsigned lines, pixelsperline, bitsperxfer;
365 unsigned int is_bi_dir = q->bidirectional;
366 size_t wantlen, outptr = 0;
369 if (verify_area(VERIFY_WRITE, buf, len))
372 /* Wait for camera to become ready */
375 int i = qcam_get(q, 41);
386 if (qcam_set(q, 7, (q->mode | (is_bi_dir?1:0)) + 1))
390 pixelsperline = q->width;
391 bitsperxfer = (is_bi_dir) ? 24 : 8;
395 /* Turn the port around */
396 parport_data_reverse(q->pport);
399 if (qcam_await_ready1(q, 1)) {
404 if (qcam_await_ready1(q, 0)) {
410 wantlen = lines * pixelsperline * 24 / 8;
415 s = (wantlen > BUFSZ)?BUFSZ:wantlen;
416 t = qcam_read_bytes(q, tmpbuf, s);
419 size_t sz = len - outptr;
421 if (__copy_to_user(buf+outptr, tmpbuf, sz))
435 printk("qcam: short read.\n");
437 parport_data_forward(q->pport);
446 l = qcam_read_bytes(q, tmpbuf, 3);
448 } while (l && (tmpbuf[0] == 0x7e || tmpbuf[1] == 0x7e || tmpbuf[2] == 0x7e));
450 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
451 printk("qcam: bad EOF\n");
453 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
454 printk("qcam: bad EOF\n");
457 if (qcam_await_ready1(q, 1))
459 printk("qcam: no ack after EOF\n");
460 parport_data_forward(q->pport);
464 parport_data_forward(q->pport);
467 if (qcam_await_ready1(q, 0))
469 printk("qcam: no ack to port turnaround\n");
478 l = qcam_read_bytes(q, tmpbuf, 1);
480 } while (l && tmpbuf[0] == 0x7e);
481 l = qcam_read_bytes(q, tmpbuf+1, 2);
483 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
484 printk("qcam: bad EOF\n");
486 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
487 printk("qcam: bad EOF\n");
491 qcam_write_data(q, 0);
496 * Video4linux interfacing
499 static int qcam_do_ioctl(struct inode *inode, struct file *file,
500 unsigned int cmd, void *arg)
502 struct video_device *dev = video_devdata(file);
503 struct qcam_device *qcam=(struct qcam_device *)dev;
509 struct video_capability *b = arg;
510 strcpy(b->name, "Quickcam");
511 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
522 struct video_channel *v = arg;
527 /* Good question.. its composite or SVHS so.. */
528 v->type = VIDEO_TYPE_CAMERA;
529 strcpy(v->name, "Camera");
534 struct video_channel *v = arg;
541 struct video_tuner *v = arg;
544 memset(v,0,sizeof(*v));
545 strcpy(v->name, "Format");
546 v->mode = VIDEO_MODE_AUTO;
551 struct video_tuner *v = arg;
554 if(v->mode!=VIDEO_MODE_AUTO)
560 struct video_picture *p = arg;
563 p->brightness=qcam->brightness<<8;
564 p->contrast=qcam->contrast<<8;
565 p->whiteness=qcam->whitebal<<8;
567 p->palette=VIDEO_PALETTE_RGB24;
572 struct video_picture *p = arg;
577 if (p->depth != 24 || p->palette != VIDEO_PALETTE_RGB24)
581 * Now load the camera.
583 qcam->brightness = p->brightness>>8;
584 qcam->contrast = p->contrast>>8;
585 qcam->whitebal = p->whiteness>>8;
588 parport_claim_or_block(qcam->pdev);
590 parport_release(qcam->pdev);
596 struct video_window *vw = arg;
602 if(vw->height<60||vw->height>240)
604 if(vw->width<80||vw->width>320)
609 qcam->mode = QC_DECIMATION_4;
611 if(vw->width>=160 && vw->height>=120)
615 qcam->mode = QC_DECIMATION_2;
617 if(vw->width>=320 && vw->height>=240)
621 qcam->mode = QC_DECIMATION_1;
623 qcam->mode |= QC_MILLIONS;
625 if(vw->width>=640 && vw->height>=480)
629 qcam->mode = QC_BILLIONS | QC_DECIMATION_1;
632 /* Ok we figured out what to use from our
635 parport_claim_or_block(qcam->pdev);
637 parport_release(qcam->pdev);
643 struct video_window *vw = arg;
644 memset(vw, 0, sizeof(*vw));
645 vw->width=qcam->width;
646 vw->height=qcam->height;
665 static int qcam_ioctl(struct inode *inode, struct file *file,
666 unsigned int cmd, unsigned long arg)
668 return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
671 static ssize_t qcam_read(struct file *file, char *buf,
672 size_t count, loff_t *ppos)
674 struct video_device *v = video_devdata(file);
675 struct qcam_device *qcam=(struct qcam_device *)v;
679 parport_claim_or_block(qcam->pdev);
680 /* Probably should have a semaphore against multiple users */
681 len = qc_capture(qcam, buf,count);
682 parport_release(qcam->pdev);
687 /* video device template */
688 static struct file_operations qcam_fops = {
689 .owner = THIS_MODULE,
690 .open = video_exclusive_open,
691 .release = video_exclusive_release,
697 static struct video_device qcam_template=
699 .owner = THIS_MODULE,
700 .name = "Colour QuickCam",
701 .type = VID_TYPE_CAPTURE,
702 .hardware = VID_HARDWARE_QCAM_C,
706 /* Initialize the QuickCam driver control structure. */
708 static struct qcam_device *qcam_init(struct parport *port)
710 struct qcam_device *q;
712 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
717 q->pdev = parport_register_device(port, "c-qcam", NULL, NULL,
720 q->bidirectional = (q->pport->modes & PARPORT_MODE_TRISTATE)?1:0;
724 printk(KERN_ERR "c-qcam: couldn't register for %s.\n",
730 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
732 init_MUTEX(&q->lock);
733 q->width = q->ccd_width = 320;
734 q->height = q->ccd_height = 240;
735 q->mode = QC_MILLIONS | QC_DECIMATION_1;
744 static struct qcam_device *qcams[MAX_CAMS];
745 static unsigned int num_cams = 0;
747 int init_cqcam(struct parport *port)
749 struct qcam_device *qcam;
751 if (parport[0] != -1)
753 /* The user gave specific instructions */
755 for (i = 0; i < MAX_CAMS && parport[i] != -1; i++)
757 if (parport[0] == port->number)
764 if (num_cams == MAX_CAMS)
767 qcam = qcam_init(port);
771 parport_claim_or_block(qcam->pdev);
775 if (probe && qc_detect(qcam)==0)
777 parport_release(qcam->pdev);
778 parport_unregister_device(qcam->pdev);
785 parport_release(qcam->pdev);
787 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
789 printk(KERN_ERR "Unable to register Colour QuickCam on %s\n",
791 parport_unregister_device(qcam->pdev);
796 printk(KERN_INFO "video%d: Colour QuickCam found on %s\n",
797 qcam->vdev.minor, qcam->pport->name);
799 qcams[num_cams++] = qcam;
804 void close_cqcam(struct qcam_device *qcam)
806 video_unregister_device(&qcam->vdev);
807 parport_unregister_device(qcam->pdev);
811 static void cq_attach(struct parport *port)
816 static void cq_detach(struct parport *port)
818 /* Write this some day. */
821 static struct parport_driver cqcam_driver = {
828 static int __init cqcam_init (void)
832 return parport_register_driver(&cqcam_driver);
835 static void __exit cqcam_cleanup (void)
839 for (i = 0; i < num_cams; i++)
840 close_cqcam(qcams[i]);
842 parport_unregister_driver(&cqcam_driver);
845 MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
846 MODULE_DESCRIPTION(BANNER);
847 MODULE_LICENSE("GPL");
849 MODULE_PARM_DESC(parport ,"parport=<auto|n[,n]...> for port detection method\n\
850 probe=<0|1|2> for camera detection method\n\
851 force_rgb=<0|1> for RGB data format (default BGR)");
852 MODULE_PARM(parport, "1-" __MODULE_STRING(MAX_CAMS) "i");
853 MODULE_PARM(probe, "i");
854 MODULE_PARM(force_rgb, "i");
855 MODULE_PARM(video_nr,"i");
857 module_init(cqcam_init);
858 module_exit(cqcam_cleanup);