26 #include <linux/module.h>
28 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/parport.h>
35 #include <linux/sched.h>
38 #include <linux/videodev2.h>
39 #include <asm/uaccess.h>
66 #define QC_MILLIONS 0x18
67 #define QC_BILLIONS 0x10
68 #define QC_THOUSANDS 0x08
71 #define QC_DECIMATION_1 0
72 #define QC_DECIMATION_2 2
73 #define QC_DECIMATION_4 4
75 #define BANNER "Colour QuickCam for Video4Linux v0.06"
79 static bool force_rgb;
80 static int video_nr = -1;
84 "probe=<0|1|2> for camera detection method\n"
85 "force_rgb=<0|1> for RGB data format (default BGR)");
92 static unsigned int num_cams;
94 static inline void qcam_set_ack(
struct qcam *
qcam,
unsigned int i)
101 static inline unsigned int qcam_ready1(
struct qcam *
qcam)
106 static inline unsigned int qcam_ready2(
struct qcam *qcam)
111 static unsigned int qcam_await_ready1(
struct qcam *qcam,
int value)
114 unsigned long oldjiffies =
jiffies;
117 for (oldjiffies = jiffies;
119 if (qcam_ready1(qcam) ==
value)
124 for (i = 0; i < 50; i++) {
125 if (qcam_ready1(qcam) ==
value)
131 v4l2_err(v4l2_dev,
"ready1 timeout (%d) %x %x\n", value,
137 static unsigned int qcam_await_ready2(
struct qcam *qcam,
int value)
140 unsigned long oldjiffies =
jiffies;
143 for (oldjiffies = jiffies;
145 if (qcam_ready2(qcam) ==
value)
150 for (i = 0; i < 50; i++) {
151 if (qcam_ready2(qcam) ==
value)
157 v4l2_err(v4l2_dev,
"ready2 timeout (%d) %x %x %x\n", value,
164 static int qcam_read_data(
struct qcam *qcam)
168 qcam_set_ack(qcam, 0);
169 if (qcam_await_ready1(qcam, 1))
172 qcam_set_ack(qcam, 1);
173 if (qcam_await_ready1(qcam, 0))
179 static int qcam_write_data(
struct qcam *qcam,
unsigned int data)
185 idata = qcam_read_data(qcam);
187 v4l2_warn(v4l2_dev,
"sent %x but received %x\n", data,
194 static inline int qcam_set(
struct qcam *qcam,
unsigned int cmd,
unsigned int data)
196 if (qcam_write_data(qcam, cmd))
198 if (qcam_write_data(qcam, data))
203 static inline int qcam_get(
struct qcam *qcam,
unsigned int cmd)
205 if (qcam_write_data(qcam, cmd))
207 return qcam_read_data(qcam);
210 static int qc_detect(
struct qcam *qcam)
220 && qcam->
pport->probe_info[0].model
221 && !
strcmp(qcam->
pdev->port->probe_info[0].model,
222 "Color QuickCam 2.0")) {
234 for (i = 0; i < 250; i++) {
253 for (i = 0; i < 250; i++) {
267 static void qc_reset(
struct qcam *qcam)
279 static void qc_setup(
struct qcam *qcam)
292 qcam_set(qcam, 0xd, qcam->
top);
293 qcam_set(qcam, 0xf, qcam->
left);
296 qcam_set(qcam, 0x19, qcam->
contrast);
297 qcam_set(qcam, 0x1f, qcam->
whitebal);
300 qcam_set(qcam, 45, 2);
307 static unsigned int qcam_read_bytes(
struct qcam *qcam,
unsigned char *
buf,
unsigned int nbytes)
309 unsigned int bytes = 0;
311 qcam_set_ack(qcam, 0);
314 while (bytes < nbytes) {
315 unsigned int lo1, hi1, lo2, hi2;
316 unsigned char r,
g,
b;
318 if (qcam_await_ready2(qcam, 1))
322 qcam_set_ack(qcam, 1);
323 if (qcam_await_ready2(qcam, 0))
327 qcam_set_ack(qcam, 0);
328 r = lo1 | ((hi1 & 1) << 7);
329 g = ((hi1 & 0x1e) << 3) | ((hi2 & 0x1e) >> 1);
330 b = lo2 | ((hi2 & 1) << 7);
344 unsigned char rgb[3];
346 while (bytes < nbytes) {
349 if (qcam_await_ready1(qcam, 1))
352 qcam_set_ack(qcam, 1);
353 if (qcam_await_ready1(qcam, 0))
356 qcam_set_ack(qcam, 0);
358 rgb[(i = bytes++ % 3)] = (hi | (lo >> 4)) ^ 0x88;
382 static long qc_capture(
struct qcam *qcam,
char __user *buf,
unsigned long len)
385 unsigned lines, pixelsperline;
387 size_t wantlen, outptr = 0;
395 int i = qcam_get(qcam, 41);
406 if (qcam_set(qcam, 7, (qcam->
mode | (is_bi_dir ? 1 : 0)) + 1))
410 pixelsperline = qcam->
width;
416 qcam_set_ack(qcam, 0);
417 if (qcam_await_ready1(qcam, 1)) {
421 qcam_set_ack(qcam, 1);
422 if (qcam_await_ready1(qcam, 0)) {
428 wantlen = lines * pixelsperline * 24 / 8;
434 t = qcam_read_bytes(qcam, tmpbuf, s);
436 size_t sz = len - outptr;
453 v4l2_err(v4l2_dev,
"short read.\n");
464 l = qcam_read_bytes(qcam, tmpbuf, 3);
466 }
while (l && (tmpbuf[0] == 0x7e || tmpbuf[1] == 0x7e || tmpbuf[2] == 0x7e));
468 if (tmpbuf[0] != 0
xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
471 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0
xe)
474 qcam_set_ack(qcam, 0);
475 if (qcam_await_ready1(qcam, 1)) {
476 v4l2_err(v4l2_dev,
"no ack after EOF\n");
483 qcam_set_ack(qcam, 1);
484 if (qcam_await_ready1(qcam, 0)) {
485 v4l2_err(v4l2_dev,
"no ack to port turnaround\n");
493 l = qcam_read_bytes(qcam, tmpbuf, 1);
495 }
while (l && tmpbuf[0] == 0x7e);
496 l = qcam_read_bytes(qcam, tmpbuf + 1, 2);
498 if (tmpbuf[0] != 0
xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
501 if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0
xe)
506 qcam_write_data(qcam, 0);
517 struct qcam *qcam = video_drvdata(file);
540 static int qcam_g_input(
struct file *file,
void *fh,
unsigned int *
inp)
546 static int qcam_s_input(
struct file *file,
void *fh,
unsigned int inp)
548 return (inp > 0) ? -
EINVAL : 0;
551 static int qcam_g_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_format *
fmt)
553 struct qcam *qcam = video_drvdata(file);
567 static int qcam_try_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_format *
fmt)
574 }
else if (pix->
height < 120 || pix->
width < 160) {
590 static int qcam_s_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_format *fmt)
592 struct qcam *qcam = video_drvdata(file);
594 int ret = qcam_try_fmt_vid_cap(file, fh, fmt);
621 static int qcam_enum_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_fmtdesc *fmt)
634 *fmt = formats[fmt->
index];
639 static ssize_t qcam_read(
struct file *file,
char __user *buf,
640 size_t count, loff_t *ppos)
642 struct qcam *qcam = video_drvdata(file);
648 len = qc_capture(qcam, buf, count);
694 .vidioc_querycap = qcam_querycap,
695 .vidioc_g_input = qcam_g_input,
696 .vidioc_s_input = qcam_s_input,
697 .vidioc_enum_input = qcam_enum_input,
698 .vidioc_enum_fmt_vid_cap = qcam_enum_fmt_vid_cap,
699 .vidioc_g_fmt_vid_cap = qcam_g_fmt_vid_cap,
700 .vidioc_s_fmt_vid_cap = qcam_s_fmt_vid_cap,
701 .vidioc_try_fmt_vid_cap = qcam_try_fmt_vid_cap,
708 .s_ctrl = qcam_s_ctrl,
713 static struct qcam *qcam_init(
struct parport *
port)
726 v4l2_err(v4l2_dev,
"Could not register v4l2_device\n");
738 if (qcam->
hdl.error) {
739 v4l2_err(v4l2_dev,
"couldn't register controls\n");
752 v4l2_err(v4l2_dev,
"couldn't register for %s.\n", port->
name);
759 qcam->
vdev.v4l2_dev = v4l2_dev;
760 qcam->
vdev.fops = &qcam_fops;
761 qcam->
vdev.ioctl_ops = &qcam_ioctl_ops;
763 qcam->
vdev.ctrl_handler = &qcam->
hdl;
765 video_set_drvdata(&qcam->
vdev, qcam);
779 static int init_cqcam(
struct parport *port)
799 qcam = qcam_init(port);
809 if (probe && qc_detect(qcam) == 0) {
821 v4l2_err(v4l2_dev,
"Unable to register Colour QuickCam on %s\n",
828 v4l2_info(v4l2_dev,
"%s: Colour QuickCam found on %s\n",
829 video_device_node_name(&qcam->
vdev), qcam->
pport->name);
831 qcams[num_cams++] = qcam;
836 static void close_cqcam(
struct qcam *qcam)
844 static void cq_attach(
struct parport *port)
849 static void cq_detach(
struct parport *port)
860 static int __init cqcam_init(
void)
867 static void __exit cqcam_cleanup(
void)
871 for (i = 0; i < num_cams; i++)
872 close_cqcam(qcams[i]);