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/videodev.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <linux/mutex.h>
40 #include <linux/jiffies.h>
42 #include <asm/uaccess.h>
45 struct video_device vdev;
46 struct pardevice *pdev;
47 struct parport *pport;
49 int ccd_width, ccd_height;
51 int contrast, brightness, whitebal;
53 unsigned int bidirectional;
61 /* The three possible QuickCam modes */
62 #define QC_MILLIONS 0x18
63 #define QC_BILLIONS 0x10
64 #define QC_THOUSANDS 0x08 /* with VIDEC compression (not supported) */
66 /* The three possible decimations */
67 #define QC_DECIMATION_1 0
68 #define QC_DECIMATION_2 2
69 #define QC_DECIMATION_4 4
71 #define BANNER "Colour QuickCam for Video4Linux v0.05"
73 static int parport[MAX_CAMS] = { [1 ... MAX_CAMS-1] = -1 };
76 static int video_nr = -1;
78 static inline void qcam_set_ack(struct qcam_device *qcam, unsigned int i)
80 /* note: the QC specs refer to the PCAck pin by voltage, not
81 software level. PC ports have builtin inverters. */
82 parport_frob_control(qcam->pport, 8, i ? 8 : 0);
85 static inline unsigned int qcam_ready1(struct qcam_device *qcam)
87 return (parport_read_status(qcam->pport) & 0x8) ? 1 : 0;
90 static inline unsigned int qcam_ready2(struct qcam_device *qcam)
92 return (parport_read_data(qcam->pport) & 0x1) ? 1 : 0;
95 static unsigned int qcam_await_ready1(struct qcam_device *qcam,
98 unsigned long oldjiffies = jiffies;
101 for (oldjiffies = jiffies;
102 time_before(jiffies, oldjiffies + msecs_to_jiffies(40));)
103 if (qcam_ready1(qcam) == value)
106 /* If the camera didn't respond within 1/25 second, poll slowly
108 for (i = 0; i < 50; i++) {
109 if (qcam_ready1(qcam) == value)
111 msleep_interruptible(100);
114 /* Probably somebody pulled the plug out. Not much we can do. */
115 printk(KERN_ERR "c-qcam: ready1 timeout (%d) %x %x\n", value,
116 parport_read_status(qcam->pport),
117 parport_read_control(qcam->pport));
121 static unsigned int qcam_await_ready2(struct qcam_device *qcam, int value)
123 unsigned long oldjiffies = jiffies;
126 for (oldjiffies = jiffies;
127 time_before(jiffies, oldjiffies + msecs_to_jiffies(40));)
128 if (qcam_ready2(qcam) == value)
131 /* If the camera didn't respond within 1/25 second, poll slowly
133 for (i = 0; i < 50; i++) {
134 if (qcam_ready2(qcam) == value)
136 msleep_interruptible(100);
139 /* Probably somebody pulled the plug out. Not much we can do. */
140 printk(KERN_ERR "c-qcam: ready2 timeout (%d) %x %x %x\n", value,
141 parport_read_status(qcam->pport),
142 parport_read_control(qcam->pport),
143 parport_read_data(qcam->pport));
147 static int qcam_read_data(struct qcam_device *qcam)
151 qcam_set_ack(qcam, 0);
152 if (qcam_await_ready1(qcam, 1))
154 idata = parport_read_status(qcam->pport) & 0xf0;
155 qcam_set_ack(qcam, 1);
156 if (qcam_await_ready1(qcam, 0))
158 idata |= parport_read_status(qcam->pport) >> 4;
162 static int qcam_write_data(struct qcam_device *qcam, unsigned int data)
166 parport_write_data(qcam->pport, data);
167 idata = qcam_read_data(qcam);
169 printk(KERN_WARNING "cqcam: sent %x but received %x\n", data,
176 static inline int qcam_set(struct qcam_device *qcam, unsigned int cmd, unsigned int data)
178 if (qcam_write_data(qcam, cmd))
180 if (qcam_write_data(qcam, data))
185 static inline int qcam_get(struct qcam_device *qcam, unsigned int cmd)
187 if (qcam_write_data(qcam, cmd))
189 return qcam_read_data(qcam);
192 static int qc_detect(struct qcam_device *qcam)
194 unsigned int stat, ostat, i, count = 0;
196 /* The probe routine below is not very reliable. The IEEE-1284
197 probe takes precedence. */
198 /* XXX Currently parport provides no way to distinguish between
199 "the IEEE probe was not done" and "the probe was done, but
200 no device was found". Fix this one day. */
201 if (qcam->pport->probe_info[0].class == PARPORT_CLASS_MEDIA
202 && qcam->pport->probe_info[0].model
203 && !strcmp(qcam->pdev->port->probe_info[0].model,
204 "Color QuickCam 2.0")) {
205 printk(KERN_DEBUG "QuickCam: Found by IEEE1284 probe.\n");
212 parport_write_control(qcam->pport, 0xc);
214 /* look for a heartbeat */
215 ostat = stat = parport_read_status(qcam->pport);
216 for (i = 0; i < 250; i++) {
218 stat = parport_read_status(qcam->pport);
226 /* Reset the camera and try again */
227 parport_write_control(qcam->pport, 0xc);
228 parport_write_control(qcam->pport, 0x8);
230 parport_write_control(qcam->pport, 0xc);
234 ostat = stat = parport_read_status(qcam->pport);
235 for (i = 0; i < 250; i++) {
237 stat = parport_read_status(qcam->pport);
245 /* no (or flatline) camera, give up */
249 static void qc_reset(struct qcam_device *qcam)
251 parport_write_control(qcam->pport, 0xc);
252 parport_write_control(qcam->pport, 0x8);
254 parport_write_control(qcam->pport, 0xc);
258 /* Reset the QuickCam and program for brightness, contrast,
259 * white-balance, and resolution. */
261 static void qc_setup(struct qcam_device *q)
265 /* Set the brightness. */
266 qcam_set(q, 11, q->brightness);
268 /* Set the height and width. These refer to the actual
269 CCD area *before* applying the selected decimation. */
270 qcam_set(q, 17, q->ccd_height);
271 qcam_set(q, 19, q->ccd_width / 2);
273 /* Set top and left. */
274 qcam_set(q, 0xd, q->top);
275 qcam_set(q, 0xf, q->left);
277 /* Set contrast and white balance. */
278 qcam_set(q, 0x19, q->contrast);
279 qcam_set(q, 0x1f, q->whitebal);
285 /* Read some bytes from the camera and put them in the buffer.
286 nbytes should be a multiple of 3, because bidirectional mode gives
287 us three bytes at a time. */
289 static unsigned int qcam_read_bytes(struct qcam_device *q, unsigned char *buf, unsigned int nbytes)
291 unsigned int bytes = 0;
294 if (q->bidirectional) {
295 /* It's a bidirectional port */
296 while (bytes < nbytes) {
297 unsigned int lo1, hi1, lo2, hi2;
298 unsigned char r, g, b;
300 if (qcam_await_ready2(q, 1))
302 lo1 = parport_read_data(q->pport) >> 1;
303 hi1 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
305 if (qcam_await_ready2(q, 0))
307 lo2 = parport_read_data(q->pport) >> 1;
308 hi2 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
310 r = lo1 | ((hi1 & 1) << 7);
311 g = ((hi1 & 0x1e) << 3) | ((hi2 & 0x1e) >> 1);
312 b = lo2 | ((hi2 & 1) << 7);
324 /* It's a unidirectional port */
325 int i = 0, n = bytes;
326 unsigned char rgb[3];
328 while (bytes < nbytes) {
331 if (qcam_await_ready1(q, 1))
333 hi = (parport_read_status(q->pport) & 0xf0);
335 if (qcam_await_ready1(q, 0))
337 lo = (parport_read_status(q->pport) & 0xf0);
340 rgb[(i = bytes++ % 3)] = (hi | (lo >> 4)) ^ 0x88;
364 static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long len)
366 unsigned lines, pixelsperline, bitsperxfer;
367 unsigned int is_bi_dir = q->bidirectional;
368 size_t wantlen, outptr = 0;
371 if (!access_ok(VERIFY_WRITE, buf, len))
374 /* Wait for camera to become ready */
376 int i = qcam_get(q, 41);
387 if (qcam_set(q, 7, (q->mode | (is_bi_dir ? 1 : 0)) + 1))
391 pixelsperline = q->width;
392 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);
418 size_t sz = len - outptr;
422 if (__copy_to_user(buf + outptr, tmpbuf, sz))
435 printk(KERN_ERR "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(KERN_ERR "qcam: bad EOF\n");
453 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
454 printk(KERN_ERR "qcam: bad EOF\n");
457 if (qcam_await_ready1(q, 1)) {
458 printk(KERN_ERR "qcam: no ack after EOF\n");
459 parport_data_forward(q->pport);
463 parport_data_forward(q->pport);
466 if (qcam_await_ready1(q, 0)) {
467 printk(KERN_ERR "qcam: no ack to port turnaround\n");
475 l = qcam_read_bytes(q, tmpbuf, 1);
477 } while (l && tmpbuf[0] == 0x7e);
478 l = qcam_read_bytes(q, tmpbuf + 1, 2);
480 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
481 printk(KERN_ERR "qcam: bad EOF\n");
483 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
484 printk(KERN_ERR "qcam: bad EOF\n");
488 qcam_write_data(q, 0);
493 * Video4linux interfacing
496 static long qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg)
498 struct video_device *dev = video_devdata(file);
499 struct qcam_device *qcam = (struct qcam_device *)dev;
504 struct video_capability *b = arg;
506 strcpy(b->name, "Quickcam");
507 b->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
518 struct video_channel *v = arg;
524 /* Good question.. its composite or SVHS so.. */
525 v->type = VIDEO_TYPE_CAMERA;
526 strcpy(v->name, "Camera");
531 struct video_channel *v = arg;
539 struct video_tuner *v = arg;
543 memset(v, 0, sizeof(*v));
544 strcpy(v->name, "Format");
545 v->mode = VIDEO_MODE_AUTO;
550 struct video_tuner *v = arg;
554 if (v->mode != VIDEO_MODE_AUTO)
560 struct video_picture *p = arg;
564 p->brightness = qcam->brightness << 8;
565 p->contrast = qcam->contrast << 8;
566 p->whiteness = qcam->whitebal << 8;
568 p->palette = VIDEO_PALETTE_RGB24;
573 struct video_picture *p = arg;
578 if (p->depth != 24 || p->palette != VIDEO_PALETTE_RGB24)
582 * Now load the camera.
584 qcam->brightness = p->brightness >> 8;
585 qcam->contrast = p->contrast >> 8;
586 qcam->whitebal = p->whiteness >> 8;
588 mutex_lock(&qcam->lock);
589 parport_claim_or_block(qcam->pdev);
591 parport_release(qcam->pdev);
592 mutex_unlock(&qcam->lock);
597 struct video_window *vw = arg;
603 if (vw->height < 60 || vw->height > 240)
605 if (vw->width < 80 || vw->width > 320)
610 qcam->mode = QC_DECIMATION_4;
612 if (vw->width >= 160 && vw->height >= 120) {
615 qcam->mode = QC_DECIMATION_2;
617 if (vw->width >= 320 && vw->height >= 240) {
620 qcam->mode = QC_DECIMATION_1;
622 qcam->mode |= QC_MILLIONS;
624 if (vw->width >= 640 && vw->height >= 480) {
627 qcam->mode = QC_BILLIONS | QC_DECIMATION_1;
630 /* Ok we figured out what to use from our
632 mutex_lock(&qcam->lock);
633 parport_claim_or_block(qcam->pdev);
635 parport_release(qcam->pdev);
636 mutex_unlock(&qcam->lock);
641 struct video_window *vw = arg;
642 memset(vw, 0, sizeof(*vw));
643 vw->width = qcam->width;
644 vw->height = qcam->height;
663 static long qcam_ioctl(struct file *file,
664 unsigned int cmd, unsigned long arg)
666 return video_usercopy(file, cmd, arg, qcam_do_ioctl);
669 static ssize_t qcam_read(struct file *file, char __user *buf,
670 size_t count, loff_t *ppos)
672 struct video_device *v = video_devdata(file);
673 struct qcam_device *qcam = (struct qcam_device *)v;
676 mutex_lock(&qcam->lock);
677 parport_claim_or_block(qcam->pdev);
678 /* Probably should have a semaphore against multiple users */
679 len = qc_capture(qcam, buf, count);
680 parport_release(qcam->pdev);
681 mutex_unlock(&qcam->lock);
685 static int qcam_exclusive_open(struct file *file)
687 struct video_device *dev = video_devdata(file);
688 struct qcam_device *qcam = (struct qcam_device *)dev;
690 return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0;
693 static int qcam_exclusive_release(struct file *file)
695 struct video_device *dev = video_devdata(file);
696 struct qcam_device *qcam = (struct qcam_device *)dev;
698 clear_bit(0, &qcam->in_use);
702 /* video device template */
703 static const struct v4l2_file_operations qcam_fops = {
704 .owner = THIS_MODULE,
705 .open = qcam_exclusive_open,
706 .release = qcam_exclusive_release,
711 static struct video_device qcam_template = {
712 .name = "Colour QuickCam",
714 .release = video_device_release_empty,
717 /* Initialize the QuickCam driver control structure. */
719 static struct qcam_device *qcam_init(struct parport *port)
721 struct qcam_device *q;
723 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
728 q->pdev = parport_register_device(port, "c-qcam", NULL, NULL,
731 q->bidirectional = (q->pport->modes & PARPORT_MODE_TRISTATE) ? 1 : 0;
733 if (q->pdev == NULL) {
734 printk(KERN_ERR "c-qcam: couldn't register for %s.\n",
740 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
742 mutex_init(&q->lock);
743 q->width = q->ccd_width = 320;
744 q->height = q->ccd_height = 240;
745 q->mode = QC_MILLIONS | QC_DECIMATION_1;
754 static struct qcam_device *qcams[MAX_CAMS];
755 static unsigned int num_cams;
757 static int init_cqcam(struct parport *port)
759 struct qcam_device *qcam;
761 if (parport[0] != -1) {
762 /* The user gave specific instructions */
765 for (i = 0; i < MAX_CAMS && parport[i] != -1; i++) {
766 if (parport[0] == port->number)
773 if (num_cams == MAX_CAMS)
776 qcam = qcam_init(port);
780 parport_claim_or_block(qcam->pdev);
784 if (probe && qc_detect(qcam) == 0) {
785 parport_release(qcam->pdev);
786 parport_unregister_device(qcam->pdev);
793 parport_release(qcam->pdev);
795 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
796 printk(KERN_ERR "Unable to register Colour QuickCam on %s\n",
798 parport_unregister_device(qcam->pdev);
803 printk(KERN_INFO "%s: Colour QuickCam found on %s\n",
804 video_device_node_name(&qcam->vdev), qcam->pport->name);
806 qcams[num_cams++] = qcam;
811 static void close_cqcam(struct qcam_device *qcam)
813 video_unregister_device(&qcam->vdev);
814 parport_unregister_device(qcam->pdev);
818 static void cq_attach(struct parport *port)
823 static void cq_detach(struct parport *port)
825 /* Write this some day. */
828 static struct parport_driver cqcam_driver = {
834 static int __init cqcam_init(void)
838 return parport_register_driver(&cqcam_driver);
841 static void __exit cqcam_cleanup(void)
845 for (i = 0; i < num_cams; i++)
846 close_cqcam(qcams[i]);
848 parport_unregister_driver(&cqcam_driver);
851 MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
852 MODULE_DESCRIPTION(BANNER);
853 MODULE_LICENSE("GPL");
855 /* FIXME: parport=auto would never have worked, surely? --RR */
856 MODULE_PARM_DESC(parport, "parport=<auto|n[,n]...> for port detection method\n"
857 "probe=<0|1|2> for camera detection method\n"
858 "force_rgb=<0|1> for RGB data format (default BGR)");
859 module_param_array(parport, int, NULL, 0);
860 module_param(probe, int, 0);
861 module_param(force_rgb, bool, 0);
862 module_param(video_nr, int, 0);
864 module_init(cqcam_init);
865 module_exit(cqcam_cleanup);