2 * QuickCam Driver For Video4Linux.
4 * Video4Linux conversion work by Alan Cox.
5 * Parport compatibility by Phil Blundell.
6 * Busy loop avoidance by Mark Cooke.
12 * When polling the QuickCam for a response, busy-wait for a
13 * maximum of this many loops. The default of 250 gives little
14 * impact on interactive response.
16 * NOTE: If this parameter is set too high, the processor
17 * will busy wait until this loop times out, and then
18 * slowly poll for a further 5 seconds before failing
19 * the transaction. You have been warned.
21 * yieldlines=<1 - 250>
23 * When acquiring a frame from the camera, the data gathering
24 * loop will yield back to the scheduler after completing
25 * this many lines. The default of 4 provides a trade-off
26 * between increased frame acquisition time and impact on
27 * interactive response.
30 /* qcam-lib.c -- Library for programming with the Connectix QuickCam.
31 * See the included documentation for usage instructions and details
32 * of the protocol involved. */
35 /* Version 0.5, August 4, 1996 */
36 /* Version 0.7, August 27, 1996 */
37 /* Version 0.9, November 17, 1996 */
40 /******************************************************************
42 Copyright (C) 1996 by Scott Laird
44 Permission is hereby granted, free of charge, to any person obtaining
45 a copy of this software and associated documentation files (the
46 "Software"), to deal in the Software without restriction, including
47 without limitation the rights to use, copy, modify, merge, publish,
48 distribute, sublicense, and/or sell copies of the Software, and to
49 permit persons to whom the Software is furnished to do so, subject to
50 the following conditions:
52 The above copyright notice and this permission notice shall be
53 included in all copies or substantial portions of the Software.
55 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58 IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY CLAIM, DAMAGES OR
59 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
60 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
61 OTHER DEALINGS IN THE SOFTWARE.
63 ******************************************************************/
65 #include <linux/module.h>
66 #include <linux/delay.h>
67 #include <linux/errno.h>
69 #include <linux/kernel.h>
70 #include <linux/slab.h>
72 #include <linux/parport.h>
73 #include <linux/sched.h>
74 #include <linux/version.h>
75 #include <linux/videodev2.h>
76 #include <linux/mutex.h>
77 #include <asm/uaccess.h>
78 #include <media/v4l2-common.h>
79 #include <media/v4l2-ioctl.h>
80 #include <media/v4l2-device.h>
82 /* One from column A... */
88 /* ... and one from column B */
90 #define QC_FORCE_UNIDIR 0x10
91 #define QC_FORCE_BIDIR 0x20
92 #define QC_FORCE_SERIAL 0x30
93 /* in the port_mode member */
95 #define QC_MODE_MASK 0x07
96 #define QC_FORCE_MASK 0x70
98 #define MAX_HEIGHT 243
101 /* Bit fields for status flags */
102 #define QC_PARAM_CHANGE 0x01 /* Camera status change has occurred */
105 struct v4l2_device v4l2_dev
;
106 struct video_device vdev
;
107 struct pardevice
*pdev
;
108 struct parport
*pport
;
113 int contrast
, brightness
, whitebal
;
118 unsigned int saved_bits
;
119 unsigned long in_use
;
122 static unsigned int maxpoll
= 250; /* Maximum busy-loop count for qcam I/O */
123 static unsigned int yieldlines
= 4; /* Yield after this many during capture */
124 static int video_nr
= -1;
125 static unsigned int force_init
; /* Whether to probe aggressively */
127 module_param(maxpoll
, int, 0);
128 module_param(yieldlines
, int, 0);
129 module_param(video_nr
, int, 0);
131 /* Set force_init=1 to avoid detection by polling status register and
132 * immediately attempt to initialize qcam */
133 module_param(force_init
, int, 0);
136 static struct qcam
*qcams
[MAX_CAMS
];
137 static unsigned int num_cams
;
139 static inline int read_lpstatus(struct qcam
*q
)
141 return parport_read_status(q
->pport
);
144 static inline int read_lpdata(struct qcam
*q
)
146 return parport_read_data(q
->pport
);
149 static inline void write_lpdata(struct qcam
*q
, int d
)
151 parport_write_data(q
->pport
, d
);
154 static void write_lpcontrol(struct qcam
*q
, int d
)
157 /* Set bidirectional mode to reverse (data in) */
158 parport_data_reverse(q
->pport
);
160 /* Set bidirectional mode to forward (data out) */
161 parport_data_forward(q
->pport
);
164 /* Now issue the regular port command, but strip out the
167 parport_write_control(q
->pport
, d
);
171 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
172 * Almost all communication with the camera requires handshaking. */
174 static int qc_waithand(struct qcam
*q
, int val
)
180 while (!((status
= read_lpstatus(q
)) & 8)) {
181 /* 1000 is enough spins on the I/O for all normal
182 cases, at that point we start to poll slowly
183 until the camera wakes up. However, we are
184 busy blocked until the camera responds, so
185 setting it lower is much better for interactive
188 if (runs
++ > maxpoll
)
189 msleep_interruptible(5);
190 if (runs
> (maxpoll
+ 1000)) /* 5 seconds */
194 while (((status
= read_lpstatus(q
)) & 8)) {
195 /* 1000 is enough spins on the I/O for all normal
196 cases, at that point we start to poll slowly
197 until the camera wakes up. However, we are
198 busy blocked until the camera responds, so
199 setting it lower is much better for interactive
202 if (runs
++ > maxpoll
)
203 msleep_interruptible(5);
204 if (runs
++ > (maxpoll
+ 1000)) /* 5 seconds */
212 /* Waithand2 is used when the qcam is in bidirectional mode, and the
213 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
214 * (bit 3 of status register). It also returns the last value read,
215 * since this data is useful. */
217 static unsigned int qc_waithand2(struct qcam
*q
, int val
)
223 status
= read_lpdata(q
);
224 /* 1000 is enough spins on the I/O for all normal
225 cases, at that point we start to poll slowly
226 until the camera wakes up. However, we are
227 busy blocked until the camera responds, so
228 setting it lower is much better for interactive
231 if (runs
++ > maxpoll
)
232 msleep_interruptible(5);
233 if (runs
++ > (maxpoll
+ 1000)) /* 5 seconds */
235 } while ((status
& 1) != val
);
240 /* qc_command is probably a bit of a misnomer -- it's used to send
241 * bytes *to* the camera. Generally, these bytes are either commands
242 * or arguments to commands, so the name fits, but it still bugs me a
243 * bit. See the documentation for a list of commands. */
245 static int qc_command(struct qcam
*q
, int command
)
250 write_lpdata(q
, command
);
251 write_lpcontrol(q
, 6);
253 n1
= qc_waithand(q
, 1);
255 write_lpcontrol(q
, 0xe);
256 n2
= qc_waithand(q
, 0);
258 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
262 static int qc_readparam(struct qcam
*q
)
267 write_lpcontrol(q
, 6);
268 n1
= qc_waithand(q
, 1);
270 write_lpcontrol(q
, 0xe);
271 n2
= qc_waithand(q
, 0);
273 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
278 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
279 the status register at 5-10 Hz. This is only used in the autoprobe
280 code. Be aware that this isn't the way Connectix detects the
281 camera (they send a reset and try to handshake), but this should be
282 almost completely safe, while their method screws up my printer if
283 I plug it in before the camera. */
285 static int qc_detect(struct qcam
*q
)
294 lastreg
= reg
= read_lpstatus(q
) & 0xf0;
296 for (i
= 0; i
< 500; i
++) {
297 reg
= read_lpstatus(q
) & 0xf0;
306 /* Be (even more) liberal in what you accept... */
308 if (count
> 20 && count
< 400) {
309 return 1; /* found */
311 printk(KERN_ERR
"No Quickcam found on port %s\n",
313 printk(KERN_DEBUG
"Quickcam detection counter: %u\n", count
);
314 return 0; /* not found */
318 /* Decide which scan mode to use. There's no real requirement that
319 * the scanmode match the resolution in q->height and q-> width -- the
320 * camera takes the picture at the resolution specified in the
321 * "scanmode" and then returns the image at the resolution specified
322 * with the resolution commands. If the scan is bigger than the
323 * requested resolution, the upper-left hand corner of the scan is
324 * returned. If the scan is smaller, then the rest of the image
325 * returned contains garbage. */
327 static int qc_setscanmode(struct qcam
*q
)
329 int old_mode
= q
->mode
;
331 switch (q
->transfer_scale
) {
351 switch (q
->port_mode
& QC_MODE_MASK
) {
360 if (q
->mode
!= old_mode
)
361 q
->status
|= QC_PARAM_CHANGE
;
367 /* Reset the QuickCam. This uses the same sequence the Windows
368 * QuickPic program uses. Someone with a bi-directional port should
369 * check that bi-directional mode is detected right, and then
370 * implement bi-directional mode in qc_readbyte(). */
372 static void qc_reset(struct qcam
*q
)
374 switch (q
->port_mode
& QC_FORCE_MASK
) {
375 case QC_FORCE_UNIDIR
:
376 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
380 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
384 write_lpcontrol(q
, 0x20);
385 write_lpdata(q
, 0x75);
387 if (read_lpdata(q
) != 0x75)
388 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
390 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
394 write_lpcontrol(q
, 0xb);
396 write_lpcontrol(q
, 0xe);
397 qc_setscanmode(q
); /* in case port_mode changed */
402 /* Reset the QuickCam and program for brightness, contrast,
403 * white-balance, and resolution. */
405 static void qc_set(struct qcam
*q
)
412 /* Set the brightness. Yes, this is repetitive, but it works.
413 * Shorter versions seem to fail subtly. Feel free to try :-). */
414 /* I think the problem was in qc_command, not here -- bls */
417 qc_command(q
, q
->brightness
);
419 val
= q
->height
/ q
->transfer_scale
;
422 if ((q
->port_mode
& QC_MODE_MASK
) == QC_UNIDIR
&& q
->bpp
== 6) {
423 /* The normal "transfers per line" calculation doesn't seem to work
424 as expected here (and yet it works fine in qc_scan). No idea
425 why this case is the odd man out. Fortunately, Laird's original
426 working version gives me a good way to guess at working values.
429 val2
= q
->transfer_scale
* 4;
431 val
= q
->width
* q
->bpp
;
432 val2
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
435 val
= DIV_ROUND_UP(val
, val2
);
439 /* Setting top and left -- bls */
441 qc_command(q
, q
->top
);
443 qc_command(q
, q
->left
/ 2);
446 qc_command(q
, q
->contrast
);
448 qc_command(q
, q
->whitebal
);
450 /* Clear flag that we must update the grabbing parameters on the camera
451 before we grab the next frame */
452 q
->status
&= (~QC_PARAM_CHANGE
);
455 /* Qc_readbytes reads some bytes from the QC and puts them in
456 the supplied buffer. It returns the number of bytes read,
459 static inline int qc_readbytes(struct qcam
*q
, char buffer
[])
463 unsigned int hi2
, lo2
;
466 if (buffer
== NULL
) {
471 switch (q
->port_mode
& QC_MODE_MASK
) {
472 case QC_BIDIR
: /* Bi-directional Port */
473 write_lpcontrol(q
, 0x26);
474 lo
= (qc_waithand2(q
, 1) >> 1);
475 hi
= (read_lpstatus(q
) >> 3) & 0x1f;
476 write_lpcontrol(q
, 0x2e);
477 lo2
= (qc_waithand2(q
, 0) >> 1);
478 hi2
= (read_lpstatus(q
) >> 3) & 0x1f;
481 buffer
[0] = lo
& 0xf;
482 buffer
[1] = ((lo
& 0x70) >> 4) | ((hi
& 1) << 3);
483 buffer
[2] = (hi
& 0x1e) >> 1;
484 buffer
[3] = lo2
& 0xf;
485 buffer
[4] = ((lo2
& 0x70) >> 4) | ((hi2
& 1) << 3);
486 buffer
[5] = (hi2
& 0x1e) >> 1;
490 buffer
[0] = lo
& 0x3f;
491 buffer
[1] = ((lo
& 0x40) >> 6) | (hi
<< 1);
492 buffer
[2] = lo2
& 0x3f;
493 buffer
[3] = ((lo2
& 0x40) >> 6) | (hi2
<< 1);
499 case QC_UNIDIR
: /* Unidirectional Port */
500 write_lpcontrol(q
, 6);
501 lo
= (qc_waithand(q
, 1) & 0xf0) >> 4;
502 write_lpcontrol(q
, 0xe);
503 hi
= (qc_waithand(q
, 0) & 0xf0) >> 4;
514 buffer
[0] = (lo
<< 2) | ((hi
& 0xc) >> 2);
515 q
->saved_bits
= (hi
& 3) << 4;
520 buffer
[0] = lo
| q
->saved_bits
;
521 q
->saved_bits
= hi
<< 2;
526 buffer
[0] = ((lo
& 0xc) >> 2) | q
->saved_bits
;
527 buffer
[1] = ((lo
& 3) << 4) | hi
;
539 /* requests a scan from the camera. It sends the correct instructions
540 * to the camera and then reads back the correct number of bytes. In
541 * previous versions of this routine the return structure contained
542 * the raw output from the camera, and there was a 'qc_convertscan'
543 * function that converted that to a useful format. In version 0.3 I
544 * rolled qc_convertscan into qc_scan and now I only return the
545 * converted scan. The format is just an one-dimensional array of
546 * characters, one for each pixel, with 0=black up to n=white, where
547 * n=2^(bit depth)-1. Ask me for more details if you don't understand
550 static long qc_capture(struct qcam
*q
, char __user
*buf
, unsigned long len
)
554 int linestotrans
, transperline
;
560 int shift
= 8 - q
->bpp
;
567 qc_command(q
, q
->mode
);
569 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) {
570 write_lpcontrol(q
, 0x2e); /* turn port around */
571 write_lpcontrol(q
, 0x26);
573 write_lpcontrol(q
, 0x2e);
577 /* strange -- should be 15:63 below, but 4bpp is odd */
578 invert
= (q
->bpp
== 4) ? 16 : 63;
580 linestotrans
= q
->height
/ q
->transfer_scale
;
581 pixels_per_line
= q
->width
/ q
->transfer_scale
;
582 transperline
= q
->width
* q
->bpp
;
583 divisor
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
585 transperline
= DIV_ROUND_UP(transperline
, divisor
);
587 for (i
= 0, yield
= yieldlines
; i
< linestotrans
; i
++) {
588 for (pixels_read
= j
= 0; j
< transperline
; j
++) {
589 bytes
= qc_readbytes(q
, buffer
);
590 for (k
= 0; k
< bytes
&& (pixels_read
+ k
) < pixels_per_line
; k
++) {
592 if (buffer
[k
] == 0 && invert
== 16) {
593 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
594 must be 0-15 -- bls */
597 o
= i
* pixels_per_line
+ pixels_read
+ k
;
600 put_user((invert
- buffer
[k
]) << shift
, buf
+ o
);
603 pixels_read
+= bytes
;
605 qc_readbytes(q
, NULL
); /* reset state machine */
607 /* Grabbing an entire frame from the quickcam is a lengthy
608 process. We don't (usually) want to busy-block the
609 processor for the entire frame. yieldlines is a module
610 parameter. If we yield every line, the minimum frame
611 time will be 240 / 200 = 1.2 seconds. The compile-time
612 default is to yield every 4 lines. */
614 msleep_interruptible(5);
615 yield
= i
+ yieldlines
;
619 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) {
620 write_lpcontrol(q
, 2);
621 write_lpcontrol(q
, 6);
623 write_lpcontrol(q
, 0xe);
631 * Video4linux interfacing
634 static int qcam_querycap(struct file
*file
, void *priv
,
635 struct v4l2_capability
*vcap
)
637 struct qcam
*qcam
= video_drvdata(file
);
639 strlcpy(vcap
->driver
, qcam
->v4l2_dev
.name
, sizeof(vcap
->driver
));
640 strlcpy(vcap
->card
, "B&W Quickcam", sizeof(vcap
->card
));
641 strlcpy(vcap
->bus_info
, "parport", sizeof(vcap
->bus_info
));
642 vcap
->version
= KERNEL_VERSION(0, 0, 2);
643 vcap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
;
647 static int qcam_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*vin
)
651 strlcpy(vin
->name
, "Camera", sizeof(vin
->name
));
652 vin
->type
= V4L2_INPUT_TYPE_CAMERA
;
660 static int qcam_g_input(struct file
*file
, void *fh
, unsigned int *inp
)
666 static int qcam_s_input(struct file
*file
, void *fh
, unsigned int inp
)
668 return (inp
> 0) ? -EINVAL
: 0;
671 static int qcam_queryctrl(struct file
*file
, void *priv
,
672 struct v4l2_queryctrl
*qc
)
675 case V4L2_CID_BRIGHTNESS
:
676 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 180);
677 case V4L2_CID_CONTRAST
:
678 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 192);
680 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 105);
685 static int qcam_g_ctrl(struct file
*file
, void *priv
,
686 struct v4l2_control
*ctrl
)
688 struct qcam
*qcam
= video_drvdata(file
);
692 case V4L2_CID_BRIGHTNESS
:
693 ctrl
->value
= qcam
->brightness
;
695 case V4L2_CID_CONTRAST
:
696 ctrl
->value
= qcam
->contrast
;
699 ctrl
->value
= qcam
->whitebal
;
708 static int qcam_s_ctrl(struct file
*file
, void *priv
,
709 struct v4l2_control
*ctrl
)
711 struct qcam
*qcam
= video_drvdata(file
);
714 mutex_lock(&qcam
->lock
);
716 case V4L2_CID_BRIGHTNESS
:
717 qcam
->brightness
= ctrl
->value
;
719 case V4L2_CID_CONTRAST
:
720 qcam
->contrast
= ctrl
->value
;
723 qcam
->whitebal
= ctrl
->value
;
730 qc_setscanmode(qcam
);
731 qcam
->status
|= QC_PARAM_CHANGE
;
733 mutex_unlock(&qcam
->lock
);
737 static int qcam_g_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
739 struct qcam
*qcam
= video_drvdata(file
);
740 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
742 pix
->width
= qcam
->width
/ qcam
->transfer_scale
;
743 pix
->height
= qcam
->height
/ qcam
->transfer_scale
;
744 pix
->pixelformat
= (qcam
->bpp
== 4) ? V4L2_PIX_FMT_Y4
: V4L2_PIX_FMT_Y6
;
745 pix
->field
= V4L2_FIELD_NONE
;
746 pix
->bytesperline
= qcam
->width
;
747 pix
->sizeimage
= qcam
->width
* qcam
->height
;
749 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
753 static int qcam_try_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
755 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
757 if (pix
->height
<= 60 || pix
->width
<= 80) {
760 } else if (pix
->height
<= 120 || pix
->width
<= 160) {
767 if (pix
->pixelformat
!= V4L2_PIX_FMT_Y4
&&
768 pix
->pixelformat
!= V4L2_PIX_FMT_Y6
)
769 pix
->pixelformat
= V4L2_PIX_FMT_Y4
;
770 pix
->field
= V4L2_FIELD_NONE
;
771 pix
->bytesperline
= pix
->width
;
772 pix
->sizeimage
= pix
->width
* pix
->height
;
774 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
778 static int qcam_s_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
780 struct qcam
*qcam
= video_drvdata(file
);
781 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
782 int ret
= qcam_try_fmt_vid_cap(file
, fh
, fmt
);
788 if (pix
->height
== 60)
789 qcam
->transfer_scale
= 4;
790 else if (pix
->height
== 120)
791 qcam
->transfer_scale
= 2;
793 qcam
->transfer_scale
= 1;
794 if (pix
->pixelformat
== V4L2_PIX_FMT_Y6
)
799 mutex_lock(&qcam
->lock
);
800 qc_setscanmode(qcam
);
801 /* We must update the camera before we grab. We could
802 just have changed the grab size */
803 qcam
->status
|= QC_PARAM_CHANGE
;
804 mutex_unlock(&qcam
->lock
);
808 static int qcam_enum_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_fmtdesc
*fmt
)
810 static struct v4l2_fmtdesc formats
[] = {
812 "4-Bit Monochrome", V4L2_PIX_FMT_Y4
,
816 "6-Bit Monochrome", V4L2_PIX_FMT_Y6
,
820 enum v4l2_buf_type type
= fmt
->type
;
825 *fmt
= formats
[fmt
->index
];
830 static ssize_t
qcam_read(struct file
*file
, char __user
*buf
,
831 size_t count
, loff_t
*ppos
)
833 struct qcam
*qcam
= video_drvdata(file
);
835 parport_claim_or_block(qcam
->pdev
);
837 mutex_lock(&qcam
->lock
);
841 /* Update the camera parameters if we need to */
842 if (qcam
->status
& QC_PARAM_CHANGE
)
845 len
= qc_capture(qcam
, buf
, count
);
847 mutex_unlock(&qcam
->lock
);
849 parport_release(qcam
->pdev
);
853 static const struct v4l2_file_operations qcam_fops
= {
854 .owner
= THIS_MODULE
,
855 .ioctl
= video_ioctl2
,
859 static const struct v4l2_ioctl_ops qcam_ioctl_ops
= {
860 .vidioc_querycap
= qcam_querycap
,
861 .vidioc_g_input
= qcam_g_input
,
862 .vidioc_s_input
= qcam_s_input
,
863 .vidioc_enum_input
= qcam_enum_input
,
864 .vidioc_queryctrl
= qcam_queryctrl
,
865 .vidioc_g_ctrl
= qcam_g_ctrl
,
866 .vidioc_s_ctrl
= qcam_s_ctrl
,
867 .vidioc_enum_fmt_vid_cap
= qcam_enum_fmt_vid_cap
,
868 .vidioc_g_fmt_vid_cap
= qcam_g_fmt_vid_cap
,
869 .vidioc_s_fmt_vid_cap
= qcam_s_fmt_vid_cap
,
870 .vidioc_try_fmt_vid_cap
= qcam_try_fmt_vid_cap
,
873 /* Initialize the QuickCam driver control structure. This is where
874 * defaults are set for people who don't have a config file.*/
876 static struct qcam
*qcam_init(struct parport
*port
)
879 struct v4l2_device
*v4l2_dev
;
881 qcam
= kzalloc(sizeof(struct qcam
), GFP_KERNEL
);
885 v4l2_dev
= &qcam
->v4l2_dev
;
886 strlcpy(v4l2_dev
->name
, "bw-qcam", sizeof(v4l2_dev
->name
));
888 if (v4l2_device_register(NULL
, v4l2_dev
) < 0) {
889 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
894 qcam
->pdev
= parport_register_device(port
, "bw-qcam", NULL
, NULL
,
896 if (qcam
->pdev
== NULL
) {
897 v4l2_err(v4l2_dev
, "couldn't register for %s.\n", port
->name
);
902 strlcpy(qcam
->vdev
.name
, "Connectix QuickCam", sizeof(qcam
->vdev
.name
));
903 qcam
->vdev
.v4l2_dev
= v4l2_dev
;
904 qcam
->vdev
.fops
= &qcam_fops
;
905 qcam
->vdev
.ioctl_ops
= &qcam_ioctl_ops
;
906 qcam
->vdev
.release
= video_device_release_empty
;
907 video_set_drvdata(&qcam
->vdev
, qcam
);
909 mutex_init(&qcam
->lock
);
911 qcam
->port_mode
= (QC_ANY
| QC_NOTSET
);
915 qcam
->transfer_scale
= 2;
916 qcam
->contrast
= 192;
917 qcam
->brightness
= 180;
918 qcam
->whitebal
= 105;
922 qcam
->status
= QC_PARAM_CHANGE
;
926 static int qc_calibrate(struct qcam
*q
)
929 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
930 * The white balance is an individual value for each
937 qc_command(q
, 27); /* AutoAdjustOffset */
938 qc_command(q
, 0); /* Dummy Parameter, ignored by the camera */
940 /* GetOffset (33) will read 255 until autocalibration */
941 /* is finished. After that, a value of 1-254 will be */
946 value
= qc_readparam(q
);
950 } while (value
== 0xff && count
< 2048);
956 static int init_bwqcam(struct parport
*port
)
960 if (num_cams
== MAX_CAMS
) {
961 printk(KERN_ERR
"Too many Quickcams (max %d)\n", MAX_CAMS
);
965 qcam
= qcam_init(port
);
969 parport_claim_or_block(qcam
->pdev
);
973 if (qc_detect(qcam
) == 0) {
974 parport_release(qcam
->pdev
);
975 parport_unregister_device(qcam
->pdev
);
981 parport_release(qcam
->pdev
);
983 v4l2_info(&qcam
->v4l2_dev
, "Connectix Quickcam on %s\n", qcam
->pport
->name
);
985 if (video_register_device(&qcam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
986 parport_unregister_device(qcam
->pdev
);
991 qcams
[num_cams
++] = qcam
;
996 static void close_bwqcam(struct qcam
*qcam
)
998 video_unregister_device(&qcam
->vdev
);
999 parport_unregister_device(qcam
->pdev
);
1003 /* The parport parameter controls which parports will be scanned.
1004 * Scanning all parports causes some printers to print a garbage page.
1005 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
1007 static char *parport
[MAX_CAMS
] = { NULL
, };
1008 module_param_array(parport
, charp
, NULL
, 0);
1011 static int accept_bwqcam(struct parport
*port
)
1016 if (parport
[0] && strncmp(parport
[0], "auto", 4) != 0) {
1017 /* user gave parport parameters */
1018 for (n
= 0; n
< MAX_CAMS
&& parport
[n
]; n
++) {
1021 r
= simple_strtoul(parport
[n
], &ep
, 0);
1022 if (ep
== parport
[n
]) {
1024 "bw-qcam: bad port specifier \"%s\"\n",
1028 if (r
== port
->number
)
1037 static void bwqcam_attach(struct parport
*port
)
1039 if (accept_bwqcam(port
))
1043 static void bwqcam_detach(struct parport
*port
)
1046 for (i
= 0; i
< num_cams
; i
++) {
1047 struct qcam
*qcam
= qcams
[i
];
1048 if (qcam
&& qcam
->pdev
->port
== port
) {
1055 static struct parport_driver bwqcam_driver
= {
1057 .attach
= bwqcam_attach
,
1058 .detach
= bwqcam_detach
,
1061 static void __exit
exit_bw_qcams(void)
1063 parport_unregister_driver(&bwqcam_driver
);
1066 static int __init
init_bw_qcams(void)
1069 /* Do some sanity checks on the module parameters. */
1070 if (maxpoll
> 5000) {
1071 printk(KERN_INFO
"Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1075 if (yieldlines
< 1) {
1076 printk(KERN_INFO
"Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1080 return parport_register_driver(&bwqcam_driver
);
1083 module_init(init_bw_qcams
);
1084 module_exit(exit_bw_qcams
);
1086 MODULE_LICENSE("GPL");