2 * QuickCam Driver For Video4Linux.
4 * This version only works as a module.
6 * Video4Linux conversion work by Alan Cox.
7 * Parport compatibility by Phil Blundell.
10 /* qcam-lib.c -- Library for programming with the Connectix QuickCam.
11 * See the included documentation for usage instructions and details
12 * of the protocol involved. */
15 /* Version 0.5, August 4, 1996 */
16 /* Version 0.7, August 27, 1996 */
17 /* Version 0.9, November 17, 1996 */
20 /******************************************************************
22 Copyright (C) 1996 by Scott Laird
24 Permission is hereby granted, free of charge, to any person obtaining
25 a copy of this software and associated documentation files (the
26 "Software"), to deal in the Software without restriction, including
27 without limitation the rights to use, copy, modify, merge, publish,
28 distribute, sublicense, and/or sell copies of the Software, and to
29 permit persons to whom the Software is furnished to do so, subject to
30 the following conditions:
32 The above copyright notice and this permission notice shall be
33 included in all copies or substantial portions of the Software.
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
38 IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY CLAIM, DAMAGES OR
39 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
40 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
41 OTHER DEALINGS IN THE SOFTWARE.
43 ******************************************************************/
45 #include <linux/module.h>
46 #include <linux/config.h>
47 #include <linux/delay.h>
48 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/kernel.h>
52 #include <linux/malloc.h>
54 #include <linux/parport.h>
55 #include <linux/sched.h>
56 #include <linux/version.h>
57 #include <linux/videodev.h>
58 #include <asm/uaccess.h>
62 extern __inline__
int read_lpstatus(struct qcam_device
*q
)
64 return parport_read_status(q
->pport
);
67 extern __inline__
int read_lpcontrol(struct qcam_device
*q
)
69 return parport_read_control(q
->pport
);
72 extern __inline__
int read_lpdata(struct qcam_device
*q
)
74 return parport_read_data(q
->pport
);
77 extern __inline__
void write_lpdata(struct qcam_device
*q
, int d
)
79 parport_write_data(q
->pport
, d
);
82 extern __inline__
void write_lpcontrol(struct qcam_device
*q
, int d
)
84 parport_write_control(q
->pport
, d
);
87 static int qc_waithand(struct qcam_device
*q
, int val
);
88 static int qc_command(struct qcam_device
*q
, int command
);
89 static int qc_readparam(struct qcam_device
*q
);
90 static int qc_setscanmode(struct qcam_device
*q
);
91 static int qc_readbytes(struct qcam_device
*q
, char buffer
[]);
93 static struct video_device qcam_template
;
95 static int qc_calibrate(struct qcam_device
*q
)
98 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
99 * The white balance is an individiual value for each
106 qc_command(q
, 27); /* AutoAdjustOffset */
107 qc_command(q
, 0); /* Dummy Parameter, ignored by the camera */
109 /* GetOffset (33) will read 255 until autocalibration */
110 /* is finished. After that, a value of 1-254 will be */
115 value
= qc_readparam(q
);
119 } while (value
== 0xff && count
<2048);
125 /* Initialize the QuickCam driver control structure. This is where
126 * defaults are set for people who don't have a config file.*/
128 static struct qcam_device
*qcam_init(struct parport
*port
)
130 struct qcam_device
*q
;
132 q
= kmalloc(sizeof(struct qcam_device
), GFP_KERNEL
);
135 q
->pdev
= parport_register_device(port
, "bw-qcam", NULL
, NULL
,
139 printk(KERN_ERR
"bw-qcam: couldn't register for %s.\n",
145 memcpy(&q
->vdev
, &qcam_template
, sizeof(qcam_template
));
147 q
->port_mode
= (QC_ANY
| QC_NOTSET
);
151 q
->transfer_scale
= 2;
162 /* qc_command is probably a bit of a misnomer -- it's used to send
163 * bytes *to* the camera. Generally, these bytes are either commands
164 * or arguments to commands, so the name fits, but it still bugs me a
165 * bit. See the documentation for a list of commands. */
167 static int qc_command(struct qcam_device
*q
, int command
)
172 write_lpdata(q
, command
);
173 write_lpcontrol(q
, 6);
175 n1
= qc_waithand(q
, 1);
177 write_lpcontrol(q
, 0xe);
178 n2
= qc_waithand(q
, 0);
180 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
184 static int qc_readparam(struct qcam_device
*q
)
189 write_lpcontrol(q
, 6);
190 n1
= qc_waithand(q
, 1);
192 write_lpcontrol(q
, 0xe);
193 n2
= qc_waithand(q
, 0);
195 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
199 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
200 * Almost all communication with the camera requires handshaking. */
202 static int qc_waithand(struct qcam_device
*q
, int val
)
209 while (!((status
= read_lpstatus(q
)) & 8))
211 /* 1000 is enough spins on the I/O for all normal
212 cases, at that point we start to poll slowly
213 until the camera wakes up */
217 current
->state
=TASK_INTERRUPTIBLE
;
218 current
->timeout
= jiffies
+HZ
/10;
227 while (((status
= read_lpstatus(q
)) & 8))
229 /* 1000 is enough spins on the I/O for all normal
230 cases, at that point we start to poll slowly
231 until the camera wakes up */
235 current
->state
=TASK_INTERRUPTIBLE
;
236 current
->timeout
= jiffies
+HZ
/10;
239 if(runs
++>1050) /* 5 seconds */
247 /* Waithand2 is used when the qcam is in bidirectional mode, and the
248 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
249 * (bit 3 of status register). It also returns the last value read,
250 * since this data is useful. */
252 static unsigned int qc_waithand2(struct qcam_device
*q
, int val
)
259 status
= read_lpdata(q
);
260 /* 1000 is enough spins on the I/O for all normal
261 cases, at that point we start to poll slowly
262 until the camera wakes up */
266 current
->state
=TASK_INTERRUPTIBLE
;
267 current
->timeout
= jiffies
+HZ
/10;
270 if(runs
++>1050) /* 5 seconds */
273 while ((status
& 1) != val
);
279 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
280 the status register at 5-10 Hz. This is only used in the autoprobe
281 code. Be aware that this isn't the way Connectix detects the
282 camera (they send a reset and try to handshake), but this should be
283 almost completely safe, while their method screws up my printer if
284 I plug it in before the camera. */
286 static int qc_detect(struct qcam_device
*q
)
292 lastreg
= reg
= read_lpstatus(q
) & 0xf0;
294 for (i
= 0; i
< 300; i
++)
296 reg
= read_lpstatus(q
) & 0xf0;
303 /* Be liberal in what you accept... */
305 if (count
> 30 && count
< 200)
306 return 1; /* found */
308 return 0; /* not found */
312 /* Reset the QuickCam. This uses the same sequence the Windows
313 * QuickPic program uses. Someone with a bi-directional port should
314 * check that bi-directional mode is detected right, and then
315 * implement bi-directional mode in qc_readbyte(). */
317 static void qc_reset(struct qcam_device
*q
)
319 switch (q
->port_mode
& QC_FORCE_MASK
)
321 case QC_FORCE_UNIDIR
:
322 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
326 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
330 write_lpcontrol(q
, 0x20);
331 write_lpdata(q
, 0x75);
333 if (read_lpdata(q
) != 0x75) {
334 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
336 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
341 write_lpcontrol(q
, 0xb);
343 write_lpcontrol(q
, 0xe);
344 qc_setscanmode(q
); /* in case port_mode changed */
348 /* Decide which scan mode to use. There's no real requirement that
349 * the scanmode match the resolution in q->height and q-> width -- the
350 * camera takes the picture at the resolution specified in the
351 * "scanmode" and then returns the image at the resolution specified
352 * with the resolution commands. If the scan is bigger than the
353 * requested resolution, the upper-left hand corner of the scan is
354 * returned. If the scan is smaller, then the rest of the image
355 * returned contains garbage. */
357 static int qc_setscanmode(struct qcam_device
*q
)
359 switch (q
->transfer_scale
)
381 switch (q
->port_mode
& QC_MODE_MASK
)
394 /* Reset the QuickCam and program for brightness, contrast,
395 * white-balance, and resolution. */
397 void qc_set(struct qcam_device
*q
)
404 /* Set the brightness. Yes, this is repetitive, but it works.
405 * Shorter versions seem to fail subtly. Feel free to try :-). */
406 /* I think the problem was in qc_command, not here -- bls */
409 qc_command(q
, q
->brightness
);
411 val
= q
->height
/ q
->transfer_scale
;
414 if ((q
->port_mode
& QC_MODE_MASK
) == QC_UNIDIR
&& q
->bpp
== 6) {
415 /* The normal "transfers per line" calculation doesn't seem to work
416 as expected here (and yet it works fine in qc_scan). No idea
417 why this case is the odd man out. Fortunately, Laird's original
418 working version gives me a good way to guess at working values.
421 val2
= q
->transfer_scale
* 4;
423 val
= q
->width
* q
->bpp
;
424 val2
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
427 val
= (val
+ val2
- 1) / val2
;
431 /* Setting top and left -- bls */
433 qc_command(q
, q
->top
);
435 qc_command(q
, q
->left
/ 2);
438 qc_command(q
, q
->contrast
);
440 qc_command(q
, q
->whitebal
);
444 /* Qc_readbytes reads some bytes from the QC and puts them in
445 the supplied buffer. It returns the number of bytes read,
448 extern __inline__
int qc_readbytes(struct qcam_device
*q
, char buffer
[])
452 unsigned int hi2
, lo2
;
453 static int state
= 0;
461 switch (q
->port_mode
& QC_MODE_MASK
)
463 case QC_BIDIR
: /* Bi-directional Port */
464 write_lpcontrol(q
, 0x26);
465 lo
= (qc_waithand2(q
, 1) >> 1);
466 hi
= (read_lpstatus(q
) >> 3) & 0x1f;
467 write_lpcontrol(q
, 0x2e);
468 lo2
= (qc_waithand2(q
, 0) >> 1);
469 hi2
= (read_lpstatus(q
) >> 3) & 0x1f;
473 buffer
[0] = lo
& 0xf;
474 buffer
[1] = ((lo
& 0x70) >> 4) | ((hi
& 1) << 3);
475 buffer
[2] = (hi
& 0x1e) >> 1;
476 buffer
[3] = lo2
& 0xf;
477 buffer
[4] = ((lo2
& 0x70) >> 4) | ((hi2
& 1) << 3);
478 buffer
[5] = (hi2
& 0x1e) >> 1;
482 buffer
[0] = lo
& 0x3f;
483 buffer
[1] = ((lo
& 0x40) >> 6) | (hi
<< 1);
484 buffer
[2] = lo2
& 0x3f;
485 buffer
[3] = ((lo2
& 0x40) >> 6) | (hi2
<< 1);
491 case QC_UNIDIR
: /* Unidirectional Port */
492 write_lpcontrol(q
, 6);
493 lo
= (qc_waithand(q
, 1) & 0xf0) >> 4;
494 write_lpcontrol(q
, 0xe);
495 hi
= (qc_waithand(q
, 0) & 0xf0) >> 4;
508 buffer
[0] = (lo
<< 2) | ((hi
& 0xc) >> 2);
509 q
->saved_bits
= (hi
& 3) << 4;
514 buffer
[0] = lo
| q
->saved_bits
;
515 q
->saved_bits
= hi
<< 2;
520 buffer
[0] = ((lo
& 0xc) >> 2) | q
->saved_bits
;
521 buffer
[1] = ((lo
& 3) << 4) | hi
;
533 /* requests a scan from the camera. It sends the correct instructions
534 * to the camera and then reads back the correct number of bytes. In
535 * previous versions of this routine the return structure contained
536 * the raw output from the camera, and there was a 'qc_convertscan'
537 * function that converted that to a useful format. In version 0.3 I
538 * rolled qc_convertscan into qc_scan and now I only return the
539 * converted scan. The format is just an one-dimensional array of
540 * characters, one for each pixel, with 0=black up to n=white, where
541 * n=2^(bit depth)-1. Ask me for more details if you don't understand
544 long qc_capture(struct qcam_device
* q
, char *buf
, unsigned long len
)
548 int linestotrans
, transperline
;
561 qc_command(q
, q
->mode
);
563 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
)
565 write_lpcontrol(q
, 0x2e); /* turn port around */
566 write_lpcontrol(q
, 0x26);
567 (void) qc_waithand(q
, 1);
568 write_lpcontrol(q
, 0x2e);
569 (void) qc_waithand(q
, 0);
572 /* strange -- should be 15:63 below, but 4bpp is odd */
573 invert
= (q
->bpp
== 4) ? 16 : 63;
575 linestotrans
= q
->height
/ q
->transfer_scale
;
576 pixels_per_line
= q
->width
/ q
->transfer_scale
;
577 transperline
= q
->width
* q
->bpp
;
578 divisor
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
580 transperline
= (transperline
+ divisor
- 1) / divisor
;
582 for (i
= 0; i
< linestotrans
; i
++)
584 for (pixels_read
= j
= 0; j
< transperline
; j
++)
586 bytes
= qc_readbytes(q
, buffer
);
587 for (k
= 0; k
< bytes
&& (pixels_read
+ k
) < pixels_per_line
; k
++)
590 if (buffer
[k
] == 0 && invert
== 16)
592 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
593 must be 0-15 -- bls */
596 o
=i
*pixels_per_line
+ pixels_read
+ k
;
600 put_user((invert
- buffer
[k
])<<shift
, buf
+o
);
603 pixels_read
+= bytes
;
605 (void) qc_readbytes(q
, 0); /* reset state machine */
608 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
)
610 write_lpcontrol(q
, 2);
611 write_lpcontrol(q
, 6);
613 write_lpcontrol(q
, 0xe);
621 * Video4linux interfacing
624 static int qcam_open(struct video_device
*dev
, int flags
)
630 static void qcam_close(struct video_device
*dev
)
635 static int qcam_init_done(struct video_device
*dev
)
640 static long qcam_write(struct video_device
*v
, const char *buf
, unsigned long count
, int noblock
)
645 static int qcam_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
647 struct qcam_device
*qcam
=(struct qcam_device
*)dev
;
653 struct video_capability b
;
654 strcpy(b
.name
, "Quickcam");
655 b
.type
= VID_TYPE_CAPTURE
|VID_TYPE_SCALES
|VID_TYPE_MONOCHROME
;
662 if(copy_to_user(arg
, &b
,sizeof(b
)))
668 struct video_channel v
;
669 if(copy_from_user(&v
, arg
, sizeof(v
)))
675 /* Good question.. its composite or SVHS so.. */
676 v
.type
= VIDEO_TYPE_CAMERA
;
677 strcpy(v
.name
, "Camera");
678 if(copy_to_user(arg
, &v
, sizeof(v
)))
685 if(copy_from_user(&v
, arg
,sizeof(v
)))
693 struct video_tuner v
;
694 if(copy_from_user(&v
, arg
, sizeof(v
))!=0)
698 strcpy(v
.name
, "Format");
702 v
.mode
= VIDEO_MODE_AUTO
;
703 if(copy_to_user(arg
,&v
,sizeof(v
))!=0)
709 struct video_tuner v
;
710 if(copy_from_user(&v
, arg
, sizeof(v
))!=0)
714 if(v
.mode
!=VIDEO_MODE_AUTO
)
720 struct video_picture p
;
723 p
.brightness
=qcam
->brightness
<<8;
724 p
.contrast
=qcam
->contrast
<<8;
725 p
.whiteness
=qcam
->whitebal
<<8;
727 p
.palette
=VIDEO_PALETTE_GREY
;
728 if(copy_to_user(arg
, &p
, sizeof(p
)))
734 struct video_picture p
;
735 if(copy_from_user(&p
, arg
, sizeof(p
)))
737 if(p
.palette
!=VIDEO_PALETTE_GREY
)
739 if(p
.depth
!=4 && p
.depth
!=6)
743 * Now load the camera.
746 qcam
->brightness
= p
.brightness
>>8;
747 qcam
->contrast
= p
.contrast
>>8;
748 qcam
->whitebal
= p
.whiteness
>>8;
751 qc_setscanmode(qcam
);
752 parport_claim_or_block(qcam
->pdev
);
754 parport_release(qcam
->pdev
);
759 struct video_window vw
;
760 if(copy_from_user(&vw
, arg
,sizeof(vw
)))
766 if(vw
.height
<60||vw
.height
>240)
768 if(vw
.width
<80||vw
.width
>320)
773 qcam
->transfer_scale
= 4;
775 if(vw
.width
>=160 && vw
.height
>=120)
777 qcam
->transfer_scale
= 2;
779 if(vw
.width
>=320 && vw
.height
>=240)
783 qcam
->transfer_scale
= 1;
785 qc_setscanmode(qcam
);
786 /* Ok we figured out what to use from our wide choice */
791 struct video_window vw
;
794 vw
.width
=qcam
->width
/qcam
->transfer_scale
;
795 vw
.height
=qcam
->height
/qcam
->transfer_scale
;
798 if(copy_to_user(arg
, &vw
, sizeof(vw
)))
824 static long qcam_read(struct video_device
*v
, char *buf
, unsigned long count
, int noblock
)
826 struct qcam_device
*qcam
=(struct qcam_device
*)v
;
828 parport_claim_or_block(qcam
->pdev
);
829 /* Probably should have a semaphore against multiple users */
831 len
=qc_capture(qcam
, buf
,count
);
832 parport_release(qcam
->pdev
);
837 static struct video_device qcam_template
=
839 "Connectix Quickcam",
841 VID_HARDWARE_QCAM_BW
,
855 static struct qcam_device
*qcams
[MAX_CAMS
];
856 static unsigned int num_cams
= 0;
858 int init_bwqcam(struct parport
*port
)
860 struct qcam_device
*qcam
;
862 if (num_cams
== MAX_CAMS
)
864 printk(KERN_ERR
"Too many Quickcams (max %d)\n", MAX_CAMS
);
868 qcam
=qcam_init(port
);
872 parport_claim_or_block(qcam
->pdev
);
876 if(qc_detect(qcam
)==0)
878 parport_release(qcam
->pdev
);
879 parport_unregister_device(qcam
->pdev
);
885 parport_release(qcam
->pdev
);
887 printk(KERN_INFO
"Connectix Quickcam on %s\n", qcam
->pport
->name
);
889 if(video_register_device(&qcam
->vdev
)==-1)
891 parport_unregister_device(qcam
->pdev
);
896 qcams
[num_cams
++] = qcam
;
901 void close_bwqcam(struct qcam_device
*qcam
)
903 video_unregister_device(&qcam
->vdev
);
904 parport_unregister_device(qcam
->pdev
);
909 int init_module(void)
911 struct parport
*port
;
913 for (port
= parport_enumerate(); port
; port
=port
->next
)
916 return (num_cams
)?0:-ENODEV
;
919 void cleanup_module(void)
922 for (i
= 0; i
< num_cams
; i
++)
923 close_bwqcam(qcams
[i
]);
926 __initfunc(int init_bw_qcams(struct video_init
*unused
))
928 struct parport
*port
;
930 for (port
= parport_enumerate(); port
; port
=port
->next
)