Linux 2.3.7pre1
[davej-history.git] / drivers / char / c-qcam.c
blob5f9341eaca55050e7be42ca9d47707e0a272036e
1 /*
2 * Video4Linux Colour QuickCam driver
3 * Copyright 1997-1998 Philip Blundell <philb@gnu.org>
5 */
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 #include <linux/fs.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/malloc.h>
14 #include <linux/mm.h>
15 #include <linux/parport.h>
16 #include <linux/sched.h>
17 #include <linux/version.h>
18 #include <linux/videodev.h>
19 #include <asm/uaccess.h>
21 struct qcam_device {
22 struct video_device vdev;
23 struct pardevice *pdev;
24 struct parport *pport;
25 int width, height;
26 int ccd_width, ccd_height;
27 int mode;
28 int contrast, brightness, whitebal;
29 int top, left;
30 unsigned int bidirectional;
33 /* The three possible QuickCam modes */
34 #define QC_MILLIONS 0x18
35 #define QC_BILLIONS 0x10
36 #define QC_THOUSANDS 0x08 /* with VIDEC compression (not supported) */
38 /* The three possible decimations */
39 #define QC_DECIMATION_1 0
40 #define QC_DECIMATION_2 2
41 #define QC_DECIMATION_4 4
43 static inline void qcam_set_ack(struct qcam_device *qcam, unsigned int i)
45 /* note: the QC specs refer to the PCAck pin by voltage, not
46 software level. PC ports have builtin inverters. */
47 parport_frob_control(qcam->pport, 8, i?8:0);
50 static inline unsigned int qcam_ready1(struct qcam_device *qcam)
52 return (parport_read_status(qcam->pport) & 0x8)?1:0;
55 static inline unsigned int qcam_ready2(struct qcam_device *qcam)
57 return (parport_read_data(qcam->pport) & 0x1)?1:0;
60 static unsigned int qcam_await_ready1(struct qcam_device *qcam,
61 int value)
63 unsigned long oldjiffies = jiffies;
64 unsigned int i;
66 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
67 if (qcam_ready1(qcam) == value)
68 return 0;
70 /* If the camera didn't respond within 1/25 second, poll slowly
71 for a while. */
72 for (i = 0; i < 50; i++)
74 if (qcam_ready1(qcam) == value)
75 return 0;
76 current->state=TASK_INTERRUPTIBLE;
77 schedule_timeout(HZ/10);
80 /* Probably somebody pulled the plug out. Not much we can do. */
81 printk(KERN_ERR "c-qcam: ready1 timeout (%d) %x %x\n", value,
82 parport_read_status(qcam->pport),
83 parport_read_control(qcam->pport));
84 return 1;
87 static unsigned int qcam_await_ready2(struct qcam_device *qcam, int value)
89 unsigned long oldjiffies = jiffies;
90 unsigned int i;
92 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
93 if (qcam_ready2(qcam) == value)
94 return 0;
96 /* If the camera didn't respond within 1/25 second, poll slowly
97 for a while. */
98 for (i = 0; i < 50; i++)
100 if (qcam_ready2(qcam) == value)
101 return 0;
102 current->state=TASK_INTERRUPTIBLE;
103 schedule_timeout(HZ/10);
106 /* Probably somebody pulled the plug out. Not much we can do. */
107 printk(KERN_ERR "c-qcam: ready2 timeout (%d) %x %x %x\n", value,
108 parport_read_status(qcam->pport),
109 parport_read_control(qcam->pport),
110 parport_read_data(qcam->pport));
111 return 1;
114 static int qcam_read_data(struct qcam_device *qcam)
116 unsigned int idata;
117 qcam_set_ack(qcam, 0);
118 if (qcam_await_ready1(qcam, 1)) return -1;
119 idata = parport_read_status(qcam->pport) & 0xf0;
120 qcam_set_ack(qcam, 1);
121 if (qcam_await_ready1(qcam, 0)) return -1;
122 idata |= (parport_read_status(qcam->pport) >> 4);
123 return idata;
126 static int qcam_write_data(struct qcam_device *qcam, unsigned int data)
128 unsigned int idata;
129 parport_write_data(qcam->pport, data);
130 idata = qcam_read_data(qcam);
131 if (data != idata)
133 printk(KERN_WARNING "cqcam: sent %x but received %x\n", data,
134 idata);
135 return 1;
137 return 0;
140 static inline int qcam_set(struct qcam_device *qcam, unsigned int cmd, unsigned int data)
142 if (qcam_write_data(qcam, cmd))
143 return -1;
144 if (qcam_write_data(qcam, data))
145 return -1;
146 return 0;
149 static inline int qcam_get(struct qcam_device *qcam, unsigned int cmd)
151 if (qcam_write_data(qcam, cmd))
152 return -1;
153 return qcam_read_data(qcam);
156 static int qc_detect(struct qcam_device *qcam)
158 unsigned int stat, ostat, i, count = 0;
160 parport_write_control(qcam->pport, 0xc);
162 /* look for a heartbeat */
163 ostat = stat = parport_read_status(qcam->pport);
164 for (i=0; i<250; i++)
166 mdelay(1);
167 stat = parport_read_status(qcam->pport);
168 if (ostat != stat)
170 if (++count >= 3) return 1;
171 ostat = stat;
175 /* no (or flatline) camera, give up */
176 return 0;
179 static void qc_reset(struct qcam_device *qcam)
181 parport_write_control(qcam->pport, 0xc);
182 parport_write_control(qcam->pport, 0x8);
183 mdelay(1);
184 parport_write_control(qcam->pport, 0xc);
185 mdelay(1);
188 /* Reset the QuickCam and program for brightness, contrast,
189 * white-balance, and resolution. */
191 static void qc_setup(struct qcam_device *q)
193 qc_reset(q);
195 /* Set the brightness. */
196 qcam_set(q, 11, q->brightness);
198 /* Set the height and width. These refer to the actual
199 CCD area *before* applying the selected decimation. */
200 qcam_set(q, 17, q->ccd_height);
201 qcam_set(q, 19, q->ccd_width / 2);
203 /* Set top and left. */
204 qcam_set(q, 0xd, q->top);
205 qcam_set(q, 0xf, q->left);
207 /* Set contrast and white balance. */
208 qcam_set(q, 0x19, q->contrast);
209 qcam_set(q, 0x1f, q->whitebal);
211 /* Set the speed. */
212 qcam_set(q, 45, 2);
215 /* Read some bytes from the camera and put them in the buffer.
216 nbytes should be a multiple of 3, because bidirectional mode gives
217 us three bytes at a time. */
219 static unsigned int qcam_read_bytes(struct qcam_device *q, unsigned char *buf, unsigned int nbytes)
221 unsigned int bytes = 0;
222 qcam_set_ack(q, 0);
223 if (q->bidirectional)
225 /* It's a bidirectional port */
226 while (bytes < nbytes)
228 unsigned int lo1, hi1, lo2, hi2;
229 if (qcam_await_ready2(q, 1)) return bytes;
230 lo1 = parport_read_data(q->pport) >> 1;
231 hi1 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
232 qcam_set_ack(q, 1);
233 if (qcam_await_ready2(q, 0)) return bytes;
234 lo2 = parport_read_data(q->pport) >> 1;
235 hi2 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
236 qcam_set_ack(q, 0);
237 buf[bytes++] = (lo1 | ((hi1 & 1)<<7));
238 buf[bytes++] = ((hi1 & 0x1e)<<3) | ((hi2 & 0x1e)>>1);
239 buf[bytes++] = (lo2 | ((hi2 & 1)<<7));
242 else
244 /* It's a unidirectional port */
245 while (bytes < nbytes)
247 unsigned int hi, lo;
248 if (qcam_await_ready1(q, 1)) return bytes;
249 hi = (parport_read_status(q->pport) & 0xf0);
250 qcam_set_ack(q, 1);
251 if (qcam_await_ready1(q, 0)) return bytes;
252 lo = (parport_read_status(q->pport) & 0xf0);
253 qcam_set_ack(q, 0);
254 /* flip some bits */
255 buf[bytes++] = (hi | (lo >> 4)) ^ 0x88;
258 return bytes;
261 #define BUFSZ 150
263 static long qc_capture(struct qcam_device *q, char *buf, unsigned long len)
265 unsigned lines, pixelsperline, bitsperxfer;
266 unsigned int is_bi_dir = q->bidirectional;
267 size_t wantlen, outptr = 0;
268 char tmpbuf[BUFSZ];
270 if (verify_area(VERIFY_WRITE, buf, len))
271 return -EFAULT;
273 /* Wait for camera to become ready */
274 for (;;)
276 int i = qcam_get(q, 41);
277 if (i == -1) {
278 qc_setup(q);
279 return -EIO;
281 if ((i & 0x80) == 0)
282 break;
283 else
284 schedule();
287 if (qcam_set(q, 7, (q->mode | (is_bi_dir?1:0)) + 1))
288 return -EIO;
290 lines = q->height;
291 pixelsperline = q->width;
292 bitsperxfer = (is_bi_dir) ? 24 : 8;
294 if (is_bi_dir)
296 /* Turn the port around */
297 parport_frob_control(q->pport, 0x20, 0x20);
298 mdelay(3);
299 qcam_set_ack(q, 0);
300 if (qcam_await_ready1(q, 1)) {
301 qc_setup(q);
302 return -EIO;
304 qcam_set_ack(q, 1);
305 if (qcam_await_ready1(q, 0)) {
306 qc_setup(q);
307 return -EIO;
311 wantlen = lines * pixelsperline * 24 / 8;
313 while (wantlen)
315 size_t t, s;
316 s = (wantlen > BUFSZ)?BUFSZ:wantlen;
317 t = qcam_read_bytes(q, tmpbuf, s);
318 if (outptr < len)
320 size_t sz = len - outptr;
321 if (sz > t) sz = t;
322 if (__copy_to_user(buf+outptr, tmpbuf, sz))
323 break;
324 outptr += sz;
326 wantlen -= t;
327 if (t < s)
328 break;
329 if (current->need_resched)
330 schedule();
333 len = outptr;
335 if (wantlen)
337 printk("qcam: short read.\n");
338 if (is_bi_dir)
339 parport_frob_control(q->pport, 0x20, 0);
340 qc_setup(q);
341 return len;
344 if (is_bi_dir)
346 int l;
347 do {
348 l = qcam_read_bytes(q, tmpbuf, 3);
349 if (current->need_resched)
350 schedule();
351 } while (l && (tmpbuf[0] == 0x7e || tmpbuf[1] == 0x7e || tmpbuf[2] == 0x7e));
352 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
353 printk("qcam: bad EOF\n");
354 qcam_set_ack(q, 0);
355 if (qcam_await_ready1(q, 1))
357 printk("qcam: no ack after EOF\n");
358 parport_frob_control(q->pport, 0x20, 0);
359 qc_setup(q);
360 return len;
362 parport_frob_control(q->pport, 0x20, 0);
363 mdelay(3);
364 qcam_set_ack(q, 1);
365 if (qcam_await_ready1(q, 0))
367 printk("qcam: no ack to port turnaround\n");
368 qc_setup(q);
369 return len;
372 else
374 int l;
375 do {
376 l = qcam_read_bytes(q, tmpbuf, 1);
377 if (current->need_resched)
378 schedule();
379 } while (l && tmpbuf[0] == 0x7e);
380 l = qcam_read_bytes(q, tmpbuf+1, 2);
381 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
382 printk("qcam: bad EOF\n");
385 qcam_write_data(q, 0);
386 return len;
390 * Video4linux interfacing
393 static int qcam_open(struct video_device *dev, int flags)
395 MOD_INC_USE_COUNT;
396 return 0;
399 static void qcam_close(struct video_device *dev)
401 MOD_DEC_USE_COUNT;
404 static int qcam_init_done(struct video_device *dev)
406 return 0;
409 static long qcam_write(struct video_device *v, const char *buf, unsigned long count, int noblock)
411 return -EINVAL;
414 static int qcam_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
416 struct qcam_device *qcam=(struct qcam_device *)dev;
418 switch(cmd)
420 case VIDIOCGCAP:
422 struct video_capability b;
423 strcpy(b.name, "Quickcam");
424 b.type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
425 b.channels = 1;
426 b.audios = 0;
427 b.maxwidth = 320;
428 b.maxheight = 240;
429 b.minwidth = 80;
430 b.minheight = 60;
431 if(copy_to_user(arg, &b,sizeof(b)))
432 return -EFAULT;
433 return 0;
435 case VIDIOCGCHAN:
437 struct video_channel v;
438 if(copy_from_user(&v, arg, sizeof(v)))
439 return -EFAULT;
440 if(v.channel!=0)
441 return -EINVAL;
442 v.flags=0;
443 v.tuners=0;
444 /* Good question.. its composite or SVHS so.. */
445 v.type = VIDEO_TYPE_CAMERA;
446 strcpy(v.name, "Camera");
447 if(copy_to_user(arg, &v, sizeof(v)))
448 return -EFAULT;
449 return 0;
451 case VIDIOCSCHAN:
453 int v;
454 if(copy_from_user(&v, arg,sizeof(v)))
455 return -EFAULT;
456 if(v!=0)
457 return -EINVAL;
458 return 0;
460 case VIDIOCGTUNER:
462 struct video_tuner v;
463 if(copy_from_user(&v, arg, sizeof(v))!=0)
464 return -EFAULT;
465 if(v.tuner)
466 return -EINVAL;
467 strcpy(v.name, "Format");
468 v.rangelow=0;
469 v.rangehigh=0;
470 v.flags= 0;
471 v.mode = VIDEO_MODE_AUTO;
472 if(copy_to_user(arg,&v,sizeof(v))!=0)
473 return -EFAULT;
474 return 0;
476 case VIDIOCSTUNER:
478 struct video_tuner v;
479 if(copy_from_user(&v, arg, sizeof(v))!=0)
480 return -EFAULT;
481 if(v.tuner)
482 return -EINVAL;
483 if(v.mode!=VIDEO_MODE_AUTO)
484 return -EINVAL;
485 return 0;
487 case VIDIOCGPICT:
489 struct video_picture p;
490 p.colour=0x8000;
491 p.hue=0x8000;
492 p.brightness=qcam->brightness<<8;
493 p.contrast=qcam->contrast<<8;
494 p.whiteness=qcam->whitebal<<8;
495 p.depth=24;
496 p.palette=VIDEO_PALETTE_RGB24;
497 if(copy_to_user(arg, &p, sizeof(p)))
498 return -EFAULT;
499 return 0;
501 case VIDIOCSPICT:
503 struct video_picture p;
504 if(copy_from_user(&p, arg, sizeof(p)))
505 return -EFAULT;
508 * Sanity check args
510 if (p.depth != 24 || p.palette != VIDEO_PALETTE_RGB24)
511 return -EINVAL;
514 * Now load the camera.
516 qcam->brightness = p.brightness>>8;
517 qcam->contrast = p.contrast>>8;
518 qcam->whitebal = p.whiteness>>8;
520 parport_claim_or_block(qcam->pdev);
521 qc_setup(qcam);
522 parport_release(qcam->pdev);
523 return 0;
525 case VIDIOCSWIN:
527 struct video_window vw;
529 if(copy_from_user(&vw, arg,sizeof(vw)))
530 return -EFAULT;
531 if(vw.flags)
532 return -EINVAL;
533 if(vw.clipcount)
534 return -EINVAL;
535 if(vw.height<60||vw.height>240)
536 return -EINVAL;
537 if(vw.width<80||vw.width>320)
538 return -EINVAL;
540 qcam->width = 80;
541 qcam->height = 60;
542 qcam->mode = QC_DECIMATION_4;
544 if(vw.width>=160 && vw.height>=120)
546 qcam->width = 160;
547 qcam->height = 120;
548 qcam->mode = QC_DECIMATION_2;
550 if(vw.width>=320 && vw.height>=240)
552 qcam->width = 320;
553 qcam->height = 240;
554 qcam->mode = QC_DECIMATION_1;
556 qcam->mode |= QC_MILLIONS;
557 #if 0
558 if(vw.width>=640 && vw.height>=480)
560 qcam->width = 640;
561 qcam->height = 480;
562 qcam->mode = QC_BILLIONS | QC_DECIMATION_1;
564 #endif
565 /* Ok we figured out what to use from our
566 wide choice */
567 parport_claim_or_block(qcam->pdev);
568 qc_setup(qcam);
569 parport_release(qcam->pdev);
570 return 0;
572 case VIDIOCGWIN:
574 struct video_window vw;
575 vw.x=0;
576 vw.y=0;
577 vw.width=qcam->width;
578 vw.height=qcam->height;
579 vw.chromakey=0;
580 vw.flags=0;
581 if(copy_to_user(arg, &vw, sizeof(vw)))
582 return -EFAULT;
583 return 0;
585 case VIDIOCCAPTURE:
586 return -EINVAL;
587 case VIDIOCGFBUF:
588 return -EINVAL;
589 case VIDIOCSFBUF:
590 return -EINVAL;
591 case VIDIOCKEY:
592 return 0;
593 case VIDIOCGFREQ:
594 return -EINVAL;
595 case VIDIOCSFREQ:
596 return -EINVAL;
597 case VIDIOCGAUDIO:
598 return -EINVAL;
599 case VIDIOCSAUDIO:
600 return -EINVAL;
601 default:
602 return -ENOIOCTLCMD;
604 return 0;
607 static long qcam_read(struct video_device *v, char *buf, unsigned long count, int noblock)
609 struct qcam_device *qcam=(struct qcam_device *)v;
610 int len;
611 parport_claim_or_block(qcam->pdev);
612 /* Probably should have a semaphore against multiple users */
613 len = qc_capture(qcam, buf,count);
614 parport_release(qcam->pdev);
615 return len;
618 /* video device template */
619 static struct video_device qcam_template=
621 "Colour Quickcam",
622 VID_TYPE_CAPTURE,
623 VID_HARDWARE_QCAM_C,
624 qcam_open,
625 qcam_close,
626 qcam_read,
627 qcam_write,
628 NULL,
629 qcam_ioctl,
630 NULL,
631 qcam_init_done,
632 NULL,
637 /* Initialize the QuickCam driver control structure. */
639 static struct qcam_device *qcam_init(struct parport *port)
641 struct qcam_device *q;
643 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
645 q->pport = port;
646 q->pdev = parport_register_device(port, "c-qcam", NULL, NULL,
647 NULL, 0, NULL);
649 q->bidirectional = (q->pport->modes & PARPORT_MODE_PCPS2)?1:0;
651 if (q->pdev == NULL)
653 printk(KERN_ERR "c-qcam: couldn't register for %s.\n",
654 port->name);
655 kfree(q);
656 return NULL;
659 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
661 q->width = q->ccd_width = 320;
662 q->height = q->ccd_height = 240;
663 q->mode = QC_MILLIONS | QC_DECIMATION_1;
664 q->contrast = 192;
665 q->brightness = 240;
666 q->whitebal = 128;
667 q->top = 1;
668 q->left = 14;
669 return q;
672 #define MAX_CAMS 4
673 static struct qcam_device *qcams[MAX_CAMS];
674 static unsigned int num_cams = 0;
676 int init_cqcam(struct parport *port)
678 struct qcam_device *qcam;
680 if (num_cams == MAX_CAMS)
682 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
683 return -ENOSPC;
686 qcam = qcam_init(port);
687 if (qcam==NULL)
688 return -ENODEV;
690 parport_claim_or_block(qcam->pdev);
692 qc_reset(qcam);
694 if (qc_detect(qcam)==0)
696 parport_release(qcam->pdev);
697 parport_unregister_device(qcam->pdev);
698 kfree(qcam);
699 return -ENODEV;
702 qc_setup(qcam);
704 parport_release(qcam->pdev);
706 printk(KERN_INFO "Colour Quickcam found on %s\n",
707 qcam->pport->name);
709 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER)==-1)
711 parport_unregister_device(qcam->pdev);
712 kfree(qcam);
713 return -ENODEV;
716 qcams[num_cams++] = qcam;
718 return 0;
721 void close_cqcam(struct qcam_device *qcam)
723 video_unregister_device(&qcam->vdev);
724 parport_unregister_device(qcam->pdev);
725 kfree(qcam);
728 #define BANNER "Connectix Colour Quickcam driver v0.02\n"
730 #ifdef MODULE
731 int init_module(void)
733 struct parport *port;
735 printk(BANNER);
737 for (port = parport_enumerate(); port; port=port->next)
738 init_cqcam(port);
740 return (num_cams)?0:-ENODEV;
743 void cleanup_module(void)
745 unsigned int i;
746 for (i = 0; i < num_cams; i++)
747 close_cqcam(qcams[i]);
749 #else
750 __initfunc(int init_colour_qcams(struct video_init *unused))
752 struct parport *port;
754 printk(BANNER);
756 for (port = parport_enumerate(); port; port=port->next)
757 init_cqcam(port);
758 return 0;
760 #endif