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/videodev2.h>
75 #include <linux/mutex.h>
76 #include <asm/uaccess.h>
77 #include <media/v4l2-common.h>
78 #include <media/v4l2-ioctl.h>
79 #include <media/v4l2-device.h>
80 #include <media/v4l2-fh.h>
81 #include <media/v4l2-ctrls.h>
82 #include <media/v4l2-event.h>
84 /* One from column A... */
90 /* ... and one from column B */
92 #define QC_FORCE_UNIDIR 0x10
93 #define QC_FORCE_BIDIR 0x20
94 #define QC_FORCE_SERIAL 0x30
95 /* in the port_mode member */
97 #define QC_MODE_MASK 0x07
98 #define QC_FORCE_MASK 0x70
100 #define MAX_HEIGHT 243
101 #define MAX_WIDTH 336
103 /* Bit fields for status flags */
104 #define QC_PARAM_CHANGE 0x01 /* Camera status change has occurred */
107 struct v4l2_device v4l2_dev;
108 struct video_device vdev;
109 struct v4l2_ctrl_handler hdl;
110 struct pardevice *pdev;
111 struct parport *pport;
116 int contrast, brightness, whitebal;
121 unsigned int saved_bits;
122 unsigned long in_use;
125 static unsigned int maxpoll = 250; /* Maximum busy-loop count for qcam I/O */
126 static unsigned int yieldlines = 4; /* Yield after this many during capture */
127 static int video_nr = -1;
128 static unsigned int force_init; /* Whether to probe aggressively */
130 module_param(maxpoll, int, 0);
131 module_param(yieldlines, int, 0);
132 module_param(video_nr, int, 0);
134 /* Set force_init=1 to avoid detection by polling status register and
135 * immediately attempt to initialize qcam */
136 module_param(force_init, int, 0);
139 static struct qcam *qcams[MAX_CAMS];
140 static unsigned int num_cams;
142 static inline int read_lpstatus(struct qcam *q)
144 return parport_read_status(q->pport);
147 static inline int read_lpdata(struct qcam *q)
149 return parport_read_data(q->pport);
152 static inline void write_lpdata(struct qcam *q, int d)
154 parport_write_data(q->pport, d);
157 static void write_lpcontrol(struct qcam *q, int d)
160 /* Set bidirectional mode to reverse (data in) */
161 parport_data_reverse(q->pport);
163 /* Set bidirectional mode to forward (data out) */
164 parport_data_forward(q->pport);
167 /* Now issue the regular port command, but strip out the
170 parport_write_control(q->pport, d);
174 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
175 * Almost all communication with the camera requires handshaking. */
177 static int qc_waithand(struct qcam *q, int val)
183 while (!((status = read_lpstatus(q)) & 8)) {
184 /* 1000 is enough spins on the I/O for all normal
185 cases, at that point we start to poll slowly
186 until the camera wakes up. However, we are
187 busy blocked until the camera responds, so
188 setting it lower is much better for interactive
191 if (runs++ > maxpoll)
192 msleep_interruptible(5);
193 if (runs > (maxpoll + 1000)) /* 5 seconds */
197 while (((status = read_lpstatus(q)) & 8)) {
198 /* 1000 is enough spins on the I/O for all normal
199 cases, at that point we start to poll slowly
200 until the camera wakes up. However, we are
201 busy blocked until the camera responds, so
202 setting it lower is much better for interactive
205 if (runs++ > maxpoll)
206 msleep_interruptible(5);
207 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
215 /* Waithand2 is used when the qcam is in bidirectional mode, and the
216 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
217 * (bit 3 of status register). It also returns the last value read,
218 * since this data is useful. */
220 static unsigned int qc_waithand2(struct qcam *q, int val)
226 status = read_lpdata(q);
227 /* 1000 is enough spins on the I/O for all normal
228 cases, at that point we start to poll slowly
229 until the camera wakes up. However, we are
230 busy blocked until the camera responds, so
231 setting it lower is much better for interactive
234 if (runs++ > maxpoll)
235 msleep_interruptible(5);
236 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
238 } while ((status & 1) != val);
243 /* qc_command is probably a bit of a misnomer -- it's used to send
244 * bytes *to* the camera. Generally, these bytes are either commands
245 * or arguments to commands, so the name fits, but it still bugs me a
246 * bit. See the documentation for a list of commands. */
248 static int qc_command(struct qcam *q, int command)
253 write_lpdata(q, command);
254 write_lpcontrol(q, 6);
256 n1 = qc_waithand(q, 1);
258 write_lpcontrol(q, 0xe);
259 n2 = qc_waithand(q, 0);
261 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
265 static int qc_readparam(struct qcam *q)
270 write_lpcontrol(q, 6);
271 n1 = qc_waithand(q, 1);
273 write_lpcontrol(q, 0xe);
274 n2 = qc_waithand(q, 0);
276 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
281 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
282 the status register at 5-10 Hz. This is only used in the autoprobe
283 code. Be aware that this isn't the way Connectix detects the
284 camera (they send a reset and try to handshake), but this should be
285 almost completely safe, while their method screws up my printer if
286 I plug it in before the camera. */
288 static int qc_detect(struct qcam *q)
297 lastreg = reg = read_lpstatus(q) & 0xf0;
299 for (i = 0; i < 500; i++) {
300 reg = read_lpstatus(q) & 0xf0;
309 /* Force camera detection during testing. Sometimes the camera
310 won't be flashing these bits. Possibly unloading the module
311 in the middle of a grab? Or some timeout condition?
312 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
313 printk(KERN_DEBUG "Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
317 /* Be (even more) liberal in what you accept... */
319 if (count > 20 && count < 400) {
320 return 1; /* found */
322 printk(KERN_ERR "No Quickcam found on port %s\n",
324 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
325 return 0; /* not found */
329 /* Decide which scan mode to use. There's no real requirement that
330 * the scanmode match the resolution in q->height and q-> width -- the
331 * camera takes the picture at the resolution specified in the
332 * "scanmode" and then returns the image at the resolution specified
333 * with the resolution commands. If the scan is bigger than the
334 * requested resolution, the upper-left hand corner of the scan is
335 * returned. If the scan is smaller, then the rest of the image
336 * returned contains garbage. */
338 static int qc_setscanmode(struct qcam *q)
340 int old_mode = q->mode;
342 switch (q->transfer_scale) {
362 switch (q->port_mode & QC_MODE_MASK) {
371 if (q->mode != old_mode)
372 q->status |= QC_PARAM_CHANGE;
378 /* Reset the QuickCam. This uses the same sequence the Windows
379 * QuickPic program uses. Someone with a bi-directional port should
380 * check that bi-directional mode is detected right, and then
381 * implement bi-directional mode in qc_readbyte(). */
383 static void qc_reset(struct qcam *q)
385 switch (q->port_mode & QC_FORCE_MASK) {
386 case QC_FORCE_UNIDIR:
387 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
391 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
395 write_lpcontrol(q, 0x20);
396 write_lpdata(q, 0x75);
398 if (read_lpdata(q) != 0x75)
399 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
401 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
405 write_lpcontrol(q, 0xb);
407 write_lpcontrol(q, 0xe);
408 qc_setscanmode(q); /* in case port_mode changed */
413 /* Reset the QuickCam and program for brightness, contrast,
414 * white-balance, and resolution. */
416 static void qc_set(struct qcam *q)
423 /* Set the brightness. Yes, this is repetitive, but it works.
424 * Shorter versions seem to fail subtly. Feel free to try :-). */
425 /* I think the problem was in qc_command, not here -- bls */
428 qc_command(q, q->brightness);
430 val = q->height / q->transfer_scale;
433 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
434 /* The normal "transfers per line" calculation doesn't seem to work
435 as expected here (and yet it works fine in qc_scan). No idea
436 why this case is the odd man out. Fortunately, Laird's original
437 working version gives me a good way to guess at working values.
440 val2 = q->transfer_scale * 4;
442 val = q->width * q->bpp;
443 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
446 val = DIV_ROUND_UP(val, val2);
450 /* Setting top and left -- bls */
452 qc_command(q, q->top);
454 qc_command(q, q->left / 2);
457 qc_command(q, q->contrast);
459 qc_command(q, q->whitebal);
461 /* Clear flag that we must update the grabbing parameters on the camera
462 before we grab the next frame */
463 q->status &= (~QC_PARAM_CHANGE);
466 /* Qc_readbytes reads some bytes from the QC and puts them in
467 the supplied buffer. It returns the number of bytes read,
470 static inline int qc_readbytes(struct qcam *q, char buffer[])
474 unsigned int hi2, lo2;
477 if (buffer == NULL) {
482 switch (q->port_mode & QC_MODE_MASK) {
483 case QC_BIDIR: /* Bi-directional Port */
484 write_lpcontrol(q, 0x26);
485 lo = (qc_waithand2(q, 1) >> 1);
486 hi = (read_lpstatus(q) >> 3) & 0x1f;
487 write_lpcontrol(q, 0x2e);
488 lo2 = (qc_waithand2(q, 0) >> 1);
489 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
492 buffer[0] = lo & 0xf;
493 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
494 buffer[2] = (hi & 0x1e) >> 1;
495 buffer[3] = lo2 & 0xf;
496 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
497 buffer[5] = (hi2 & 0x1e) >> 1;
501 buffer[0] = lo & 0x3f;
502 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
503 buffer[2] = lo2 & 0x3f;
504 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
510 case QC_UNIDIR: /* Unidirectional Port */
511 write_lpcontrol(q, 6);
512 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
513 write_lpcontrol(q, 0xe);
514 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
525 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
526 q->saved_bits = (hi & 3) << 4;
531 buffer[0] = lo | q->saved_bits;
532 q->saved_bits = hi << 2;
537 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
538 buffer[1] = ((lo & 3) << 4) | hi;
550 /* requests a scan from the camera. It sends the correct instructions
551 * to the camera and then reads back the correct number of bytes. In
552 * previous versions of this routine the return structure contained
553 * the raw output from the camera, and there was a 'qc_convertscan'
554 * function that converted that to a useful format. In version 0.3 I
555 * rolled qc_convertscan into qc_scan and now I only return the
556 * converted scan. The format is just an one-dimensional array of
557 * characters, one for each pixel, with 0=black up to n=white, where
558 * n=2^(bit depth)-1. Ask me for more details if you don't understand
561 static long qc_capture(struct qcam *q, char __user *buf, unsigned long len)
565 int linestotrans, transperline;
571 int shift = 8 - q->bpp;
578 qc_command(q, q->mode);
580 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
581 write_lpcontrol(q, 0x2e); /* turn port around */
582 write_lpcontrol(q, 0x26);
584 write_lpcontrol(q, 0x2e);
588 /* strange -- should be 15:63 below, but 4bpp is odd */
589 invert = (q->bpp == 4) ? 16 : 63;
591 linestotrans = q->height / q->transfer_scale;
592 pixels_per_line = q->width / q->transfer_scale;
593 transperline = q->width * q->bpp;
594 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
596 transperline = DIV_ROUND_UP(transperline, divisor);
598 for (i = 0, yield = yieldlines; i < linestotrans; i++) {
599 for (pixels_read = j = 0; j < transperline; j++) {
600 bytes = qc_readbytes(q, buffer);
601 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) {
603 if (buffer[k] == 0 && invert == 16) {
604 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
605 must be 0-15 -- bls */
608 o = i * pixels_per_line + pixels_read + k;
610 u8 ch = invert - buffer[k];
612 put_user(ch << shift, buf + o);
615 pixels_read += bytes;
617 qc_readbytes(q, NULL); /* reset state machine */
619 /* Grabbing an entire frame from the quickcam is a lengthy
620 process. We don't (usually) want to busy-block the
621 processor for the entire frame. yieldlines is a module
622 parameter. If we yield every line, the minimum frame
623 time will be 240 / 200 = 1.2 seconds. The compile-time
624 default is to yield every 4 lines. */
626 msleep_interruptible(5);
627 yield = i + yieldlines;
631 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
632 write_lpcontrol(q, 2);
633 write_lpcontrol(q, 6);
635 write_lpcontrol(q, 0xe);
643 * Video4linux interfacing
646 static int qcam_querycap(struct file *file, void *priv,
647 struct v4l2_capability *vcap)
649 struct qcam *qcam = video_drvdata(file);
651 strlcpy(vcap->driver, qcam->v4l2_dev.name, sizeof(vcap->driver));
652 strlcpy(vcap->card, "Connectix B&W Quickcam", sizeof(vcap->card));
653 strlcpy(vcap->bus_info, qcam->pport->name, sizeof(vcap->bus_info));
654 vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
655 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
659 static int qcam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
663 strlcpy(vin->name, "Camera", sizeof(vin->name));
664 vin->type = V4L2_INPUT_TYPE_CAMERA;
672 static int qcam_g_input(struct file *file, void *fh, unsigned int *inp)
678 static int qcam_s_input(struct file *file, void *fh, unsigned int inp)
680 return (inp > 0) ? -EINVAL : 0;
683 static int qcam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
685 struct qcam *qcam = video_drvdata(file);
686 struct v4l2_pix_format *pix = &fmt->fmt.pix;
688 pix->width = qcam->width / qcam->transfer_scale;
689 pix->height = qcam->height / qcam->transfer_scale;
690 pix->pixelformat = (qcam->bpp == 4) ? V4L2_PIX_FMT_Y4 : V4L2_PIX_FMT_Y6;
691 pix->field = V4L2_FIELD_NONE;
692 pix->bytesperline = pix->width;
693 pix->sizeimage = pix->width * pix->height;
695 pix->colorspace = V4L2_COLORSPACE_SRGB;
699 static int qcam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
701 struct v4l2_pix_format *pix = &fmt->fmt.pix;
703 if (pix->height <= 60 || pix->width <= 80) {
706 } else if (pix->height <= 120 || pix->width <= 160) {
713 if (pix->pixelformat != V4L2_PIX_FMT_Y4 &&
714 pix->pixelformat != V4L2_PIX_FMT_Y6)
715 pix->pixelformat = V4L2_PIX_FMT_Y4;
716 pix->field = V4L2_FIELD_NONE;
717 pix->bytesperline = pix->width;
718 pix->sizeimage = pix->width * pix->height;
720 pix->colorspace = V4L2_COLORSPACE_SRGB;
724 static int qcam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
726 struct qcam *qcam = video_drvdata(file);
727 struct v4l2_pix_format *pix = &fmt->fmt.pix;
728 int ret = qcam_try_fmt_vid_cap(file, fh, fmt);
734 if (pix->height == 60)
735 qcam->transfer_scale = 4;
736 else if (pix->height == 120)
737 qcam->transfer_scale = 2;
739 qcam->transfer_scale = 1;
740 if (pix->pixelformat == V4L2_PIX_FMT_Y6)
745 mutex_lock(&qcam->lock);
746 qc_setscanmode(qcam);
747 /* We must update the camera before we grab. We could
748 just have changed the grab size */
749 qcam->status |= QC_PARAM_CHANGE;
750 mutex_unlock(&qcam->lock);
754 static int qcam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
756 static struct v4l2_fmtdesc formats[] = {
758 "4-Bit Monochrome", V4L2_PIX_FMT_Y4,
762 "6-Bit Monochrome", V4L2_PIX_FMT_Y6,
766 enum v4l2_buf_type type = fmt->type;
771 *fmt = formats[fmt->index];
776 static int qcam_enum_framesizes(struct file *file, void *fh,
777 struct v4l2_frmsizeenum *fsize)
779 static const struct v4l2_frmsize_discrete sizes[] = {
785 if (fsize->index > 2)
787 if (fsize->pixel_format != V4L2_PIX_FMT_Y4 &&
788 fsize->pixel_format != V4L2_PIX_FMT_Y6)
790 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
791 fsize->discrete = sizes[fsize->index];
795 static ssize_t qcam_read(struct file *file, char __user *buf,
796 size_t count, loff_t *ppos)
798 struct qcam *qcam = video_drvdata(file);
800 parport_claim_or_block(qcam->pdev);
802 mutex_lock(&qcam->lock);
806 /* Update the camera parameters if we need to */
807 if (qcam->status & QC_PARAM_CHANGE)
810 len = qc_capture(qcam, buf, count);
812 mutex_unlock(&qcam->lock);
814 parport_release(qcam->pdev);
818 static unsigned int qcam_poll(struct file *filp, poll_table *wait)
820 return v4l2_ctrl_poll(filp, wait) | POLLIN | POLLRDNORM;
823 static int qcam_s_ctrl(struct v4l2_ctrl *ctrl)
826 container_of(ctrl->handler, struct qcam, hdl);
829 mutex_lock(&qcam->lock);
831 case V4L2_CID_BRIGHTNESS:
832 qcam->brightness = ctrl->val;
834 case V4L2_CID_CONTRAST:
835 qcam->contrast = ctrl->val;
838 qcam->whitebal = ctrl->val;
845 qc_setscanmode(qcam);
846 qcam->status |= QC_PARAM_CHANGE;
848 mutex_unlock(&qcam->lock);
852 static const struct v4l2_file_operations qcam_fops = {
853 .owner = THIS_MODULE,
854 .open = v4l2_fh_open,
855 .release = v4l2_fh_release,
857 .unlocked_ioctl = video_ioctl2,
861 static const struct v4l2_ioctl_ops qcam_ioctl_ops = {
862 .vidioc_querycap = qcam_querycap,
863 .vidioc_g_input = qcam_g_input,
864 .vidioc_s_input = qcam_s_input,
865 .vidioc_enum_input = qcam_enum_input,
866 .vidioc_enum_fmt_vid_cap = qcam_enum_fmt_vid_cap,
867 .vidioc_enum_framesizes = qcam_enum_framesizes,
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,
871 .vidioc_log_status = v4l2_ctrl_log_status,
872 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
873 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
876 static const struct v4l2_ctrl_ops qcam_ctrl_ops = {
877 .s_ctrl = qcam_s_ctrl,
880 /* Initialize the QuickCam driver control structure. This is where
881 * defaults are set for people who don't have a config file.*/
883 static struct qcam *qcam_init(struct parport *port)
886 struct v4l2_device *v4l2_dev;
888 qcam = kzalloc(sizeof(struct qcam), GFP_KERNEL);
892 v4l2_dev = &qcam->v4l2_dev;
893 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "bw-qcam%d", num_cams);
895 if (v4l2_device_register(port->dev, v4l2_dev) < 0) {
896 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
901 v4l2_ctrl_handler_init(&qcam->hdl, 3);
902 v4l2_ctrl_new_std(&qcam->hdl, &qcam_ctrl_ops,
903 V4L2_CID_BRIGHTNESS, 0, 255, 1, 180);
904 v4l2_ctrl_new_std(&qcam->hdl, &qcam_ctrl_ops,
905 V4L2_CID_CONTRAST, 0, 255, 1, 192);
906 v4l2_ctrl_new_std(&qcam->hdl, &qcam_ctrl_ops,
907 V4L2_CID_GAMMA, 0, 255, 1, 105);
908 if (qcam->hdl.error) {
909 v4l2_err(v4l2_dev, "couldn't register controls\n");
910 v4l2_ctrl_handler_free(&qcam->hdl);
915 qcam->pdev = parport_register_device(port, v4l2_dev->name, NULL, NULL,
917 if (qcam->pdev == NULL) {
918 v4l2_err(v4l2_dev, "couldn't register for %s.\n", port->name);
919 v4l2_ctrl_handler_free(&qcam->hdl);
924 strlcpy(qcam->vdev.name, "Connectix QuickCam", sizeof(qcam->vdev.name));
925 qcam->vdev.v4l2_dev = v4l2_dev;
926 qcam->vdev.ctrl_handler = &qcam->hdl;
927 qcam->vdev.fops = &qcam_fops;
928 qcam->vdev.ioctl_ops = &qcam_ioctl_ops;
929 set_bit(V4L2_FL_USE_FH_PRIO, &qcam->vdev.flags);
930 qcam->vdev.release = video_device_release_empty;
931 video_set_drvdata(&qcam->vdev, qcam);
933 mutex_init(&qcam->lock);
935 qcam->port_mode = (QC_ANY | QC_NOTSET);
939 qcam->transfer_scale = 2;
940 qcam->contrast = 192;
941 qcam->brightness = 180;
942 qcam->whitebal = 105;
946 qcam->status = QC_PARAM_CHANGE;
950 static int qc_calibrate(struct qcam *q)
953 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
954 * The white balance is an individual value for each
961 qc_command(q, 27); /* AutoAdjustOffset */
962 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
964 /* GetOffset (33) will read 255 until autocalibration */
965 /* is finished. After that, a value of 1-254 will be */
970 value = qc_readparam(q);
974 } while (value == 0xff && count < 2048);
980 static int init_bwqcam(struct parport *port)
984 if (num_cams == MAX_CAMS) {
985 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
989 qcam = qcam_init(port);
993 parport_claim_or_block(qcam->pdev);
997 if (qc_detect(qcam) == 0) {
998 parport_release(qcam->pdev);
999 parport_unregister_device(qcam->pdev);
1004 v4l2_ctrl_handler_setup(&qcam->hdl);
1006 parport_release(qcam->pdev);
1008 v4l2_info(&qcam->v4l2_dev, "Connectix Quickcam on %s\n", qcam->pport->name);
1010 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1011 parport_unregister_device(qcam->pdev);
1016 qcams[num_cams++] = qcam;
1021 static void close_bwqcam(struct qcam *qcam)
1023 video_unregister_device(&qcam->vdev);
1024 v4l2_ctrl_handler_free(&qcam->hdl);
1025 parport_unregister_device(qcam->pdev);
1029 /* The parport parameter controls which parports will be scanned.
1030 * Scanning all parports causes some printers to print a garbage page.
1031 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
1033 static char *parport[MAX_CAMS] = { NULL, };
1034 module_param_array(parport, charp, NULL, 0);
1037 static int accept_bwqcam(struct parport *port)
1042 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
1043 /* user gave parport parameters */
1044 for (n = 0; n < MAX_CAMS && parport[n]; n++) {
1047 r = simple_strtoul(parport[n], &ep, 0);
1048 if (ep == parport[n]) {
1050 "bw-qcam: bad port specifier \"%s\"\n",
1054 if (r == port->number)
1063 static void bwqcam_attach(struct parport *port)
1065 if (accept_bwqcam(port))
1069 static void bwqcam_detach(struct parport *port)
1072 for (i = 0; i < num_cams; i++) {
1073 struct qcam *qcam = qcams[i];
1074 if (qcam && qcam->pdev->port == port) {
1081 static struct parport_driver bwqcam_driver = {
1083 .attach = bwqcam_attach,
1084 .detach = bwqcam_detach,
1087 static void __exit exit_bw_qcams(void)
1089 parport_unregister_driver(&bwqcam_driver);
1092 static int __init init_bw_qcams(void)
1095 /* Do some sanity checks on the module parameters. */
1096 if (maxpoll > 5000) {
1097 printk(KERN_INFO "Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1101 if (yieldlines < 1) {
1102 printk(KERN_INFO "Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1106 return parport_register_driver(&bwqcam_driver);
1109 module_init(init_bw_qcams);
1110 module_exit(exit_bw_qcams);
1112 MODULE_LICENSE("GPL");
1113 MODULE_VERSION("0.0.3");