65 #include <linux/module.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>
76 #include <asm/uaccess.h>
92 #define QC_FORCE_UNIDIR 0x10
93 #define QC_FORCE_BIDIR 0x20
94 #define QC_FORCE_SERIAL 0x30
97 #define QC_MODE_MASK 0x07
98 #define QC_FORCE_MASK 0x70
100 #define MAX_HEIGHT 243
101 #define MAX_WIDTH 336
104 #define QC_PARAM_CHANGE 0x01
125 static unsigned int maxpoll = 250;
126 static unsigned int yieldlines = 4;
127 static int video_nr = -1;
128 static unsigned int force_init;
140 static unsigned int num_cams;
142 static inline int read_lpstatus(
struct qcam *
q)
147 static inline int read_lpdata(
struct qcam *
q)
152 static inline void write_lpdata(
struct qcam *
q,
int d)
157 static void write_lpcontrol(
struct qcam *
q,
int d)
177 static int qc_waithand(
struct qcam *q,
int val)
183 while (!((status = read_lpstatus(q)) & 8)) {
191 if (runs++ > maxpoll)
193 if (runs > (maxpoll + 1000))
197 while (((status = read_lpstatus(q)) & 8)) {
205 if (runs++ > maxpoll)
207 if (runs++ > (maxpoll + 1000))
220 static unsigned int qc_waithand2(
struct qcam *q,
int val)
226 status = read_lpdata(q);
234 if (runs++ > maxpoll)
236 if (runs++ > (maxpoll + 1000))
238 }
while ((status & 1) != val);
253 write_lpdata(q, command);
254 write_lpcontrol(q, 6);
256 n1 = qc_waithand(q, 1);
258 write_lpcontrol(q, 0
xe);
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, 0
xe);
274 n2 = qc_waithand(q, 0);
276 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
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;
313 printk(
KERN_DEBUG "Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
319 if (count > 20 && count < 400) {
338 static int qc_setscanmode(
struct qcam *q)
340 int old_mode = q->
mode;
371 if (q->
mode != old_mode)
383 static void qc_reset(
struct qcam *q)
395 write_lpcontrol(q, 0x20);
396 write_lpdata(q, 0x75);
398 if (read_lpdata(q) != 0x75)
405 write_lpcontrol(q, 0
xb);
407 write_lpcontrol(q, 0
xe);
416 static void qc_set(
struct qcam *q)
452 qc_command(q, q->
top);
454 qc_command(q, q->
left / 2);
470 static inline int qc_readbytes(
struct qcam *q,
char buffer[])
474 unsigned int hi2, lo2;
477 if (buffer ==
NULL) {
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);
511 write_lpcontrol(q, 6);
512 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
513 write_lpcontrol(q, 0
xe);
514 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
525 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
537 buffer[0] = ((lo & 0xc) >> 2) | q->
saved_bits;
538 buffer[1] = ((lo & 3) << 4) | hi;
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);
581 write_lpcontrol(q, 0x2e);
582 write_lpcontrol(q, 0x26);
584 write_lpcontrol(q, 0x2e);
589 invert = (q->
bpp == 4) ? 16 : 63;
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) {
608 o = i * pixels_per_line + pixels_read +
k;
610 u8 ch = invert - buffer[
k];
615 pixels_read +=
bytes;
617 qc_readbytes(q,
NULL);
627 yield = i + yieldlines;
632 write_lpcontrol(q, 2);
633 write_lpcontrol(q, 6);
635 write_lpcontrol(q, 0
xe);
649 struct qcam *
qcam = video_drvdata(file);
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);
699 static int qcam_try_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_format *
fmt)
706 }
else if (pix->
height <= 120 || pix->
width <= 160) {
724 static int qcam_s_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_format *fmt)
726 struct qcam *
qcam = video_drvdata(file);
728 int ret = qcam_try_fmt_vid_cap(file, fh, fmt);
736 else if (pix->
height == 120)
746 qc_setscanmode(qcam);
754 static int qcam_enum_fmt_vid_cap(
struct file *file,
void *fh,
struct v4l2_fmtdesc *fmt)
771 *fmt = formats[fmt->
index];
776 static int qcam_enum_framesizes(
struct file *file,
void *fh,
785 if (fsize->
index > 2)
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);
810 len = qc_capture(qcam, buf, count);
818 static unsigned int qcam_poll(
struct file *filp,
poll_table *
wait)
845 qc_setscanmode(qcam);
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,
877 .s_ctrl = qcam_s_ctrl,
883 static struct qcam *qcam_init(
struct parport *
port)
888 qcam = kzalloc(
sizeof(
struct qcam),
GFP_KERNEL);
896 v4l2_err(v4l2_dev,
"Could not register v4l2_device\n");
908 if (qcam->
hdl.error) {
909 v4l2_err(v4l2_dev,
"couldn't register controls\n");
918 v4l2_err(v4l2_dev,
"couldn't register for %s.\n", port->
name);
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;
931 video_set_drvdata(&qcam->
vdev, qcam);
950 static int qc_calibrate(
struct qcam *q)
970 value = qc_readparam(q);
974 }
while (value == 0xff && count < 2048);
980 static int init_bwqcam(
struct parport *port)
989 qcam = qcam_init(port);
997 if (qc_detect(qcam) == 0) {
1016 qcams[num_cams++] = qcam;
1021 static void close_bwqcam(
struct qcam *qcam)
1037 static int accept_bwqcam(
struct parport *port)
1042 if (parport[0] &&
strncmp(parport[0],
"auto", 4) != 0) {
1044 for (n = 0; n <
MAX_CAMS && parport[
n]; n++) {
1048 if (ep == parport[n]) {
1050 "bw-qcam: bad port specifier \"%s\"\n",
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) {
1083 .attach = bwqcam_attach,
1084 .detach = bwqcam_detach,
1087 static void __exit exit_bw_qcams(
void)
1092 static int __init init_bw_qcams(
void)
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");