Import 2.1.81
[davej-history.git] / drivers / char / c-qcam.c
blobdf12a34131da384046ac4df1af6819c2a9e1e709
1 /*
2 * Video4Linux: Colour QuickCam driver
4 * Philip Blundell <philb@gnu.org>, December 30 1997
6 * Largely untested (seems to work at 24bpp with a bidirectional port,
7 * though).
8 */
11 #include <linux/module.h>
12 #include <linux/config.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/malloc.h>
19 #include <linux/mm.h>
20 #include <linux/parport.h>
21 #include <linux/sched.h>
22 #include <linux/version.h>
23 #include <linux/videodev.h>
24 #include <asm/uaccess.h>
26 #include "c-qcam.h"
28 static __inline__ void qcam_set_ack(struct qcam_device *qcam, unsigned int i)
30 /* note: the QC specs refer to the PCAck pin by voltage, not
31 software level. PC ports have builtin inverters. */
32 parport_frob_control(qcam->pport, 8, i?8:0);
35 static __inline__ unsigned int qcam_ready1(struct qcam_device *qcam)
37 return (parport_read_status(qcam->pport) & 0x8)?1:0;
41 static __inline__ unsigned int qcam_ready2(struct qcam_device *qcam)
43 return (parport_read_data(qcam->pport) & 0x1)?1:0;
46 static inline unsigned int qcam_await_ready1(struct qcam_device *qcam, int value)
48 unsigned long oldjiffies = jiffies;
49 unsigned int i;
51 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
52 if (qcam_ready1(qcam) == value)
53 return 0;
55 /* If the camera didn't respond within 1/25 second, poll slowly
56 for a while. */
57 for (i = 0; i < 50; i++)
59 if (qcam_ready1(qcam) == value)
60 return 0;
61 current->state=TASK_INTERRUPTIBLE;
62 current->timeout = jiffies+HZ/10;
63 schedule();
66 /* Probably somebody pulled the plug out. Not much we can do. */
67 printk(KERN_ERR "c-qcam: ready1 timeout (%d) %x %x\n", value,
68 parport_read_status(qcam->pport),
69 parport_read_control(qcam->pport));
70 return 1;
73 static inline unsigned int qcam_await_ready2(struct qcam_device *qcam, int value)
75 unsigned long oldjiffies = jiffies;
76 unsigned int i;
78 for (oldjiffies = jiffies; (jiffies - oldjiffies) < (HZ/25); )
79 if (qcam_ready2(qcam) == value)
80 return 0;
82 /* If the camera didn't respond within 1/25 second, poll slowly
83 for a while. */
84 for (i = 0; i < 50; i++)
86 if (qcam_ready2(qcam) == value)
87 return 0;
88 current->state=TASK_INTERRUPTIBLE;
89 current->timeout = jiffies+HZ/10;
90 schedule();
93 /* Probably somebody pulled the plug out. Not much we can do. */
94 printk(KERN_ERR "c-qcam: ready2 timeout (%d) %x %x %x\n", value,
95 parport_read_status(qcam->pport),
96 parport_read_control(qcam->pport),
97 parport_read_data(qcam->pport));
98 return 1;
101 static inline int qcam_read_data(struct qcam_device *qcam)
103 unsigned int idata;
104 qcam_set_ack(qcam, 0);
105 if (qcam_await_ready1(qcam, 1)) return -1;
106 idata = parport_read_status(qcam->pport) & 0xf0;
107 qcam_set_ack(qcam, 1);
108 if (qcam_await_ready1(qcam, 0)) return -1;
109 idata |= (parport_read_status(qcam->pport) >> 4);
110 return idata;
113 static int qcam_write_data(struct qcam_device *qcam, unsigned int data)
115 unsigned int idata;
116 parport_write_data(qcam->pport, data);
117 idata = qcam_read_data(qcam);
118 if (data != idata)
120 printk(KERN_WARNING "cqcam: sent %x but received %x\n", data,
121 idata);
122 return 1;
124 return 0;
127 static inline int qcam_set(struct qcam_device *qcam, unsigned int cmd, unsigned int data)
129 if (qcam_write_data(qcam, cmd))
130 return -1;
131 if (qcam_write_data(qcam, data))
132 return -1;
133 return 0;
136 static inline int qcam_get(struct qcam_device *qcam, unsigned int cmd)
138 if (qcam_write_data(qcam, cmd))
139 return -1;
140 return qcam_read_data(qcam);
143 static int qc_detect(struct qcam_device *qcam)
145 unsigned int stat, ostat, i, count = 0;
147 parport_write_control(qcam->pport, 0xc);
149 /* look for a heartbeat */
150 ostat = stat = parport_read_status(qcam->pport);
151 for (i=0; i<250; i++)
153 udelay(1000);
154 stat = parport_read_status(qcam->pport);
155 if (ostat != stat)
157 if (++count >= 3) return 1;
158 ostat = stat;
162 /* no (or flatline) camera, give up */
163 return 0;
166 static void qc_reset(struct qcam_device *qcam)
168 parport_write_control(qcam->pport, 0xc);
169 parport_write_control(qcam->pport, 0x8);
170 udelay(1000);
171 parport_write_control(qcam->pport, 0xc);
172 udelay(1000);
175 /* Reset the QuickCam and program for brightness, contrast,
176 * white-balance, and resolution. */
178 static void qc_setup(struct qcam_device *q)
180 qc_reset(q);
182 /* Set the brightness. */
183 qcam_set(q, 11, q->brightness);
185 /* Set the height. */
186 qcam_set(q, 17, q->height);
188 /* Set the width. */
189 qcam_set(q, 19, q->width/2);
191 /* Set top and left. */
192 qcam_set(q, 0xd, q->top);
193 qcam_set(q, 0xf, q->left);
195 /* Set contrast and white balance. */
196 qcam_set(q, 0x19, q->contrast);
197 qcam_set(q, 0x1f, q->whitebal);
199 /* Set the speed. */
200 qcam_set(q, 45, 2);
203 /* Read some bytes from the camera and put them in the buffer.
204 nbytes should be a multiple of 3, because bidirectional mode gives
205 us three bytes at a time. */
207 static unsigned int qcam_read_bytes(struct qcam_device *q, unsigned char *buf, unsigned int nbytes)
209 unsigned int bytes = 0;
210 qcam_set_ack(q, 0);
211 if (q->bidirectional)
213 /* It's a bidirectional port */
214 while (bytes < nbytes)
216 unsigned int lo1, hi1, lo2, hi2;
217 if (qcam_await_ready2(q, 1)) return bytes;
218 lo1 = parport_read_data(q->pport) >> 1;
219 hi1 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
220 qcam_set_ack(q, 1);
221 if (qcam_await_ready2(q, 0)) return bytes;
222 lo2 = parport_read_data(q->pport) >> 1;
223 hi2 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
224 qcam_set_ack(q, 0);
225 buf[bytes++] = (lo1 | ((hi1 & 1)<<7));
226 buf[bytes++] = ((hi1 & 0x1e)<<3) | ((hi2 & 0x1e)>>1);
227 buf[bytes++] = (lo2 | ((hi2 & 1)<<7));
230 else
232 /* It's a unidirectional port */
233 while (bytes < nbytes)
235 unsigned int hi, lo;
236 if (qcam_await_ready1(q, 1)) return bytes;
237 hi = (parport_read_status(q->pport) & 0xf0);
238 qcam_set_ack(q, 1);
239 if (qcam_await_ready1(q, 0)) return bytes;
240 lo = (parport_read_status(q->pport) & 0xf0);
241 qcam_set_ack(q, 0);
242 /* flip some bits; cqcam gets this wrong */
243 buf[bytes++] = (hi | lo) ^ 0x88;
246 return bytes;
249 /* Convert the data the camera gives us into the desired output format.
250 At the moment this is a no-op because read_bytes() does all the
251 required stuff, for 24bpp at least. */
252 static size_t qcam_munge_buffer(struct qcam_device *q, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
254 size_t outptr = 0;
255 switch (q->bpp)
257 case 24:
258 while (inlen && (outptr <= (outlen-3)))
260 unsigned char r, g, b;
261 r = inbuf[0];
262 g = inbuf[1];
263 b = inbuf[2];
264 put_user(r, outbuf+(outptr++));
265 put_user(g, outbuf+(outptr++));
266 put_user(b, outbuf+(outptr++));
267 inlen -= 3;
268 inbuf += 3;
270 break;
271 default:
272 printk("c-qcam: can't convert this format (%d).\n", q->bpp);
273 return 0;
275 return outptr;
278 static long qc_capture(struct qcam_device *q, char *buf, unsigned long len)
280 unsigned int tbpp = 0, tdecimation = 0, lines, pixelsperline, bitsperxfer;
281 unsigned int is_bi_dir = q->bidirectional;
282 size_t wantlen, outptr = 0;
283 char *tmpbuf = kmalloc(768, GFP_KERNEL);
284 if (tmpbuf == NULL)
286 printk(KERN_ERR "cqcam: couldn't allocate a buffer.\n");
287 return -ENOMEM;
290 /* Wait for camera to become ready */
291 for (;;)
293 int i = qcam_get(q, 41);
294 if (i == -1) {
295 qc_setup(q);
296 kfree(tmpbuf);
297 return -EIO;
299 if (i & 0x80)
300 schedule();
301 else
302 break;
305 switch (q->bpp)
307 case 24: tbpp = QC_24BPP; break;
308 case 32: tbpp = QC_32BPP; break;
309 case 16: tbpp = QC_16BPP; break;
310 default: printk("qcam: Bad bpp.\n");
312 switch (q->transfer_scale) {
313 case 1: tdecimation = QC_1_1; break;
314 case 2: tdecimation = QC_2_1; break;
315 case 4: tdecimation = QC_4_1; break;
316 default: printk("qcam: Bad decimation.\n");
319 qcam_set(q, 7, (tbpp | tdecimation) + ((is_bi_dir)?1:0) + 1);
321 lines = q->height / q->transfer_scale;
322 pixelsperline = q->width / q->transfer_scale;
323 bitsperxfer = (is_bi_dir) ? 24 : 8;
325 if (is_bi_dir)
327 /* Turn the port around */
328 parport_frob_control(q->pport, 0x20, 0x20);
329 udelay(3000);
330 qcam_set_ack(q, 0);
331 if (qcam_await_ready1(q, 1)) {
332 kfree(tmpbuf);
333 qc_setup(q);
334 return -EIO;
336 qcam_set_ack(q, 1);
337 if (qcam_await_ready1(q, 0)) {
338 kfree(tmpbuf);
339 qc_setup(q);
340 return -EIO;
344 wantlen = lines * pixelsperline * q->bpp / 8;
346 while (wantlen)
348 size_t t, s, o;
349 s = (wantlen > 768)?768:wantlen;
350 t = qcam_read_bytes(q, tmpbuf, s);
351 if (outptr < len)
353 o = qcam_munge_buffer(q, tmpbuf, t, buf + outptr,
354 len - outptr);
355 outptr += o;
357 wantlen -= t;
358 if (t < s)
359 break;
360 if (need_resched)
361 schedule();
364 len = outptr;
366 if (wantlen)
368 printk("qcam: short read.\n");
369 if (is_bi_dir)
370 parport_frob_control(q->pport, 0x20, 0);
371 qc_setup(q);
372 kfree(tmpbuf);
373 return len;
376 if (is_bi_dir)
378 int l;
379 do {
380 l = qcam_read_bytes(q, tmpbuf, 3);
381 if (need_resched)
382 schedule();
383 } while (l && (tmpbuf[0] == 0x7e || tmpbuf[1] == 0x7e || tmpbuf[2] == 0x7e));
384 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
385 printk("qcam: bad EOF\n");
386 qcam_set_ack(q, 0);
387 if (qcam_await_ready1(q, 1))
389 printk("qcam: no ack after EOF\n");
390 parport_frob_control(q->pport, 0x20, 0);
391 qc_setup(q);
392 kfree(tmpbuf);
393 return len;
395 parport_frob_control(q->pport, 0x20, 0);
396 udelay(3000);
397 qcam_set_ack(q, 1);
398 if (qcam_await_ready1(q, 0))
400 printk("qcam: no ack to port turnaround\n");
401 qc_setup(q);
402 kfree(tmpbuf);
403 return len;
406 else
408 int l;
409 do {
410 l = qcam_read_bytes(q, tmpbuf, 1);
411 if (need_resched)
412 schedule();
413 } while (l && tmpbuf[0] == 0x7e);
414 l = qcam_read_bytes(q, tmpbuf+1, 2);
415 if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
416 printk("qcam: bad EOF\n");
419 kfree(tmpbuf);
421 qcam_write_data(q, 0);
423 return len;
427 * Video4linux interfacing
430 static int qcam_open(struct video_device *dev, int flags)
432 MOD_INC_USE_COUNT;
433 return 0;
436 static void qcam_close(struct video_device *dev)
438 MOD_DEC_USE_COUNT;
441 static int qcam_init_done(struct video_device *dev)
443 return 0;
446 static long qcam_write(struct video_device *v, const char *buf, unsigned long count, int noblock)
448 return -EINVAL;
451 static int qcam_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
453 struct qcam_device *qcam=(struct qcam_device *)dev;
455 switch(cmd)
457 case VIDIOCGCAP:
459 struct video_capability b;
460 strcpy(b.name, "Quickcam");
461 b.type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
462 b.channels = 1;
463 b.audios = 0;
464 b.maxwidth = 320;
465 b.maxheight = 240;
466 b.minwidth = 80;
467 b.minheight = 60;
468 if(copy_to_user(arg, &b,sizeof(b)))
469 return -EFAULT;
470 return 0;
472 case VIDIOCGCHAN:
474 struct video_channel v;
475 if(copy_from_user(&v, arg, sizeof(v)))
476 return -EFAULT;
477 if(v.channel!=0)
478 return -EINVAL;
479 v.flags=0;
480 v.tuners=0;
481 /* Good question.. its composite or SVHS so.. */
482 v.type = VIDEO_TYPE_CAMERA;
483 strcpy(v.name, "Camera");
484 if(copy_to_user(arg, &v, sizeof(v)))
485 return -EFAULT;
486 return 0;
488 case VIDIOCSCHAN:
490 int v;
491 if(copy_from_user(&v, arg,sizeof(v)))
492 return -EFAULT;
493 if(v!=0)
494 return -EINVAL;
495 return 0;
497 case VIDIOCGTUNER:
499 struct video_tuner v;
500 if(copy_from_user(&v, arg, sizeof(v))!=0)
501 return -EFAULT;
502 if(v.tuner)
503 return -EINVAL;
504 strcpy(v.name, "Format");
505 v.rangelow=0;
506 v.rangehigh=0;
507 v.flags= 0;
508 v.mode = VIDEO_MODE_AUTO;
509 if(copy_to_user(arg,&v,sizeof(v))!=0)
510 return -EFAULT;
511 return 0;
513 case VIDIOCSTUNER:
515 struct video_tuner v;
516 if(copy_from_user(&v, arg, sizeof(v))!=0)
517 return -EFAULT;
518 if(v.tuner)
519 return -EINVAL;
520 if(v.mode!=VIDEO_MODE_AUTO)
521 return -EINVAL;
522 return 0;
524 case VIDIOCGPICT:
526 struct video_picture p;
527 p.colour=0x8000;
528 p.hue=0x8000;
529 p.brightness=qcam->brightness<<8;
530 p.contrast=qcam->contrast<<8;
531 p.whiteness=qcam->whitebal<<8;
532 p.depth=qcam->bpp;
533 p.palette=VIDEO_PALETTE_RGB24;
534 if(copy_to_user(arg, &p, sizeof(p)))
535 return -EFAULT;
536 return 0;
538 case VIDIOCSPICT:
540 struct video_picture p;
541 if(copy_from_user(&p, arg, sizeof(p)))
542 return -EFAULT;
544 if (p.palette != VIDEO_PALETTE_RGB24)
545 return -EINVAL;
548 * Now load the camera.
550 qcam->brightness = p.brightness>>8;
551 qcam->contrast = p.contrast>>8;
552 qcam->whitebal = p.whiteness>>8;
553 qcam->bpp = p.depth;
555 parport_claim_or_block(qcam->pdev);
556 qc_setup(qcam);
557 parport_release(qcam->pdev);
558 return 0;
560 case VIDIOCSWIN:
562 struct video_window vw;
563 if(copy_from_user(&vw, arg,sizeof(vw)))
564 return -EFAULT;
565 if(vw.flags)
566 return -EINVAL;
567 if(vw.clipcount)
568 return -EINVAL;
569 if(vw.height<60||vw.height>240)
570 return -EINVAL;
571 if(vw.width<80||vw.width>320)
572 return -EINVAL;
574 qcam->width = 320;
575 qcam->height = 240;
576 qcam->transfer_scale = 4;
578 if(vw.width>=160 && vw.height>=120)
580 qcam->transfer_scale = 2;
582 if(vw.width>=320 && vw.height>=240)
584 qcam->width = 320;
585 qcam->height = 240;
586 qcam->transfer_scale = 1;
588 /* Ok we figured out what to use from our wide choice */
589 parport_claim_or_block(qcam->pdev);
590 qc_setup(qcam);
591 parport_release(qcam->pdev);
592 return 0;
594 case VIDIOCGWIN:
596 struct video_window vw;
597 vw.x=0;
598 vw.y=0;
599 vw.width=qcam->width/qcam->transfer_scale;
600 vw.height=qcam->height/qcam->transfer_scale;
601 vw.chromakey=0;
602 vw.flags=0;
603 if(copy_to_user(arg, &vw, sizeof(vw)))
604 return -EFAULT;
605 return 0;
607 case VIDIOCCAPTURE:
608 return -EINVAL;
609 case VIDIOCGFBUF:
610 return -EINVAL;
611 case VIDIOCSFBUF:
612 return -EINVAL;
613 case VIDIOCKEY:
614 return 0;
615 case VIDIOCGFREQ:
616 return -EINVAL;
617 case VIDIOCSFREQ:
618 return -EINVAL;
619 case VIDIOCGAUDIO:
620 return -EINVAL;
621 case VIDIOCSAUDIO:
622 return -EINVAL;
623 default:
624 return -ENOIOCTLCMD;
626 return 0;
629 static long qcam_read(struct video_device *v, char *buf, unsigned long count, int noblock)
631 struct qcam_device *qcam=(struct qcam_device *)v;
632 int len;
633 parport_claim_or_block(qcam->pdev);
634 /* Probably should have a semaphore against multiple users */
635 len = qc_capture(qcam, buf,count);
636 parport_release(qcam->pdev);
637 return len;
640 /* video device template */
641 static struct video_device qcam_template=
643 "Colour Quickcam",
644 VID_TYPE_CAPTURE,
645 VID_HARDWARE_QCAM_C,
646 qcam_open,
647 qcam_close,
648 qcam_read,
649 qcam_write,
650 qcam_ioctl,
651 NULL,
652 qcam_init_done,
653 NULL,
658 /* Initialize the QuickCam driver control structure. */
660 static struct qcam_device *qcam_init(struct parport *port)
662 struct qcam_device *q;
664 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
666 q->pport = port;
667 q->pdev = parport_register_device(port, "c-qcam", NULL, NULL,
668 NULL, 0, NULL);
670 q->bidirectional = (q->pport->modes & PARPORT_MODE_PCPS2)?1:0;
672 if (q->pdev == NULL)
674 printk(KERN_ERR "c-qcam: couldn't register for %s.\n",
675 port->name);
676 kfree(q);
677 return NULL;
680 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
682 q->width = 320;
683 q->height = 240;
684 q->bpp = 32;
685 q->transfer_scale = 1;
686 q->contrast = 192;
687 q->brightness = 240;
688 q->whitebal = 128;
689 q->top = 1;
690 q->left = 14;
691 return q;
694 #define MAX_CAMS 4
695 static struct qcam_device *qcams[MAX_CAMS];
696 static unsigned int num_cams = 0;
698 int init_cqcam(struct parport *port)
700 struct qcam_device *qcam;
702 if (num_cams == MAX_CAMS)
704 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
705 return -ENOSPC;
708 qcam = qcam_init(port);
709 if (qcam==NULL)
710 return -ENODEV;
712 parport_claim_or_block(qcam->pdev);
714 qc_reset(qcam);
716 if (qc_detect(qcam)==0)
718 parport_release(qcam->pdev);
719 parport_unregister_device(qcam->pdev);
720 kfree(qcam);
721 return -ENODEV;
724 qc_setup(qcam);
726 parport_release(qcam->pdev);
728 printk(KERN_INFO "Connectix Colour Quickcam on %s\n",
729 qcam->pport->name);
731 if (video_register_device(&qcam->vdev)==-1)
733 parport_unregister_device(qcam->pdev);
734 kfree(qcam);
735 return -ENODEV;
738 qcams[num_cams++] = qcam;
740 return 0;
743 void close_cqcam(struct qcam_device *qcam)
745 video_unregister_device(&qcam->vdev);
746 parport_unregister_device(qcam->pdev);
747 kfree(qcam);
750 #ifdef MODULE
751 int init_module(void)
753 struct parport *port;
755 for (port = parport_enumerate(); port; port=port->next)
756 init_cqcam(port);
758 return (num_cams)?0:-ENODEV;
761 void cleanup_module(void)
763 unsigned int i;
764 for (i = 0; i < num_cams; i++)
765 close_cqcam(qcams[i]);
767 #else
768 __initfunc(int init_colour_qcams(struct video_init *unused))
770 struct parport *port;
772 for (port = parport_enumerate(); port; port=port->next)
773 init_cqcam(port);
774 return 0;
776 #endif