Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / bw-qcam.c
blob032265383df25203eabf1816a8a94f4bf43daa2d
1 /*
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.
8 * Module parameters:
10 * maxpoll=<1 - 5000>
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>
68 #include <linux/fs.h>
69 #include <linux/init.h>
70 #include <linux/kernel.h>
71 #include <linux/slab.h>
72 #include <linux/mm.h>
73 #include <linux/parport.h>
74 #include <linux/sched.h>
75 #include <linux/videodev.h>
76 #include <media/v4l2-common.h>
77 #include <linux/mutex.h>
78 #include <asm/uaccess.h>
80 #include "bw-qcam.h"
82 static unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */
83 static unsigned int yieldlines=4; /* Yield after this many during capture */
84 static int video_nr = -1;
85 static unsigned int force_init; /* Whether to probe aggressively */
87 module_param(maxpoll, int, 0);
88 module_param(yieldlines, int, 0);
89 module_param(video_nr, int, 0);
91 /* Set force_init=1 to avoid detection by polling status register and
92 * immediately attempt to initialize qcam */
93 module_param(force_init, int, 0);
95 static inline int read_lpstatus(struct qcam_device *q)
97 return parport_read_status(q->pport);
100 static inline int read_lpdata(struct qcam_device *q)
102 return parport_read_data(q->pport);
105 static inline void write_lpdata(struct qcam_device *q, int d)
107 parport_write_data(q->pport, d);
110 static inline void write_lpcontrol(struct qcam_device *q, int d)
112 if (d & 0x20) {
113 /* Set bidirectional mode to reverse (data in) */
114 parport_data_reverse(q->pport);
115 } else {
116 /* Set bidirectional mode to forward (data out) */
117 parport_data_forward(q->pport);
120 /* Now issue the regular port command, but strip out the
121 * direction flag */
122 d &= ~0x20;
123 parport_write_control(q->pport, d);
126 static int qc_waithand(struct qcam_device *q, int val);
127 static int qc_command(struct qcam_device *q, int command);
128 static int qc_readparam(struct qcam_device *q);
129 static int qc_setscanmode(struct qcam_device *q);
130 static int qc_readbytes(struct qcam_device *q, char buffer[]);
132 static struct video_device qcam_template;
134 static int qc_calibrate(struct qcam_device *q)
137 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
138 * The white balance is an individiual value for each
139 * quickcam.
142 int value;
143 int count = 0;
145 qc_command(q, 27); /* AutoAdjustOffset */
146 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
148 /* GetOffset (33) will read 255 until autocalibration */
149 /* is finished. After that, a value of 1-254 will be */
150 /* returned. */
152 do {
153 qc_command(q, 33);
154 value = qc_readparam(q);
155 mdelay(1);
156 schedule();
157 count++;
158 } while (value == 0xff && count<2048);
160 q->whitebal = value;
161 return value;
164 /* Initialize the QuickCam driver control structure. This is where
165 * defaults are set for people who don't have a config file.*/
167 static struct qcam_device *qcam_init(struct parport *port)
169 struct qcam_device *q;
171 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
172 if(q==NULL)
173 return NULL;
175 q->pport = port;
176 q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
177 NULL, 0, NULL);
178 if (q->pdev == NULL)
180 printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
181 port->name);
182 kfree(q);
183 return NULL;
186 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
188 mutex_init(&q->lock);
190 q->port_mode = (QC_ANY | QC_NOTSET);
191 q->width = 320;
192 q->height = 240;
193 q->bpp = 4;
194 q->transfer_scale = 2;
195 q->contrast = 192;
196 q->brightness = 180;
197 q->whitebal = 105;
198 q->top = 1;
199 q->left = 14;
200 q->mode = -1;
201 q->status = QC_PARAM_CHANGE;
202 return q;
206 /* qc_command is probably a bit of a misnomer -- it's used to send
207 * bytes *to* the camera. Generally, these bytes are either commands
208 * or arguments to commands, so the name fits, but it still bugs me a
209 * bit. See the documentation for a list of commands. */
211 static int qc_command(struct qcam_device *q, int command)
213 int n1, n2;
214 int cmd;
216 write_lpdata(q, command);
217 write_lpcontrol(q, 6);
219 n1 = qc_waithand(q, 1);
221 write_lpcontrol(q, 0xe);
222 n2 = qc_waithand(q, 0);
224 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
225 return cmd;
228 static int qc_readparam(struct qcam_device *q)
230 int n1, n2;
231 int cmd;
233 write_lpcontrol(q, 6);
234 n1 = qc_waithand(q, 1);
236 write_lpcontrol(q, 0xe);
237 n2 = qc_waithand(q, 0);
239 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
240 return cmd;
243 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
244 * Almost all communication with the camera requires handshaking. */
246 static int qc_waithand(struct qcam_device *q, int val)
248 int status;
249 int runs=0;
251 if (val)
253 while (!((status = read_lpstatus(q)) & 8))
255 /* 1000 is enough spins on the I/O for all normal
256 cases, at that point we start to poll slowly
257 until the camera wakes up. However, we are
258 busy blocked until the camera responds, so
259 setting it lower is much better for interactive
260 response. */
262 if(runs++>maxpoll)
264 msleep_interruptible(5);
266 if(runs>(maxpoll+1000)) /* 5 seconds */
267 return -1;
270 else
272 while (((status = read_lpstatus(q)) & 8))
274 /* 1000 is enough spins on the I/O for all normal
275 cases, at that point we start to poll slowly
276 until the camera wakes up. However, we are
277 busy blocked until the camera responds, so
278 setting it lower is much better for interactive
279 response. */
281 if(runs++>maxpoll)
283 msleep_interruptible(5);
285 if(runs++>(maxpoll+1000)) /* 5 seconds */
286 return -1;
290 return status;
293 /* Waithand2 is used when the qcam is in bidirectional mode, and the
294 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
295 * (bit 3 of status register). It also returns the last value read,
296 * since this data is useful. */
298 static unsigned int qc_waithand2(struct qcam_device *q, int val)
300 unsigned int status;
301 int runs=0;
305 status = read_lpdata(q);
306 /* 1000 is enough spins on the I/O for all normal
307 cases, at that point we start to poll slowly
308 until the camera wakes up. However, we are
309 busy blocked until the camera responds, so
310 setting it lower is much better for interactive
311 response. */
313 if(runs++>maxpoll)
315 msleep_interruptible(5);
317 if(runs++>(maxpoll+1000)) /* 5 seconds */
318 return 0;
320 while ((status & 1) != val);
322 return status;
326 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
327 the status register at 5-10 Hz. This is only used in the autoprobe
328 code. Be aware that this isn't the way Connectix detects the
329 camera (they send a reset and try to handshake), but this should be
330 almost completely safe, while their method screws up my printer if
331 I plug it in before the camera. */
333 static int qc_detect(struct qcam_device *q)
335 int reg, lastreg;
336 int count = 0;
337 int i;
339 if (force_init)
340 return 1;
342 lastreg = reg = read_lpstatus(q) & 0xf0;
344 for (i = 0; i < 500; i++)
346 reg = read_lpstatus(q) & 0xf0;
347 if (reg != lastreg)
348 count++;
349 lastreg = reg;
350 mdelay(2);
354 #if 0
355 /* Force camera detection during testing. Sometimes the camera
356 won't be flashing these bits. Possibly unloading the module
357 in the middle of a grab? Or some timeout condition?
358 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
359 printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
360 return 1;
361 #endif
363 /* Be (even more) liberal in what you accept... */
365 if (count > 20 && count < 400) {
366 return 1; /* found */
367 } else {
368 printk(KERN_ERR "No Quickcam found on port %s\n",
369 q->pport->name);
370 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
371 return 0; /* not found */
376 /* Reset the QuickCam. This uses the same sequence the Windows
377 * QuickPic program uses. Someone with a bi-directional port should
378 * check that bi-directional mode is detected right, and then
379 * implement bi-directional mode in qc_readbyte(). */
381 static void qc_reset(struct qcam_device *q)
383 switch (q->port_mode & QC_FORCE_MASK)
385 case QC_FORCE_UNIDIR:
386 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
387 break;
389 case QC_FORCE_BIDIR:
390 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
391 break;
393 case QC_ANY:
394 write_lpcontrol(q, 0x20);
395 write_lpdata(q, 0x75);
397 if (read_lpdata(q) != 0x75) {
398 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
399 } else {
400 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
402 break;
405 write_lpcontrol(q, 0xb);
406 udelay(250);
407 write_lpcontrol(q, 0xe);
408 qc_setscanmode(q); /* in case port_mode changed */
412 /* Decide which scan mode to use. There's no real requirement that
413 * the scanmode match the resolution in q->height and q-> width -- the
414 * camera takes the picture at the resolution specified in the
415 * "scanmode" and then returns the image at the resolution specified
416 * with the resolution commands. If the scan is bigger than the
417 * requested resolution, the upper-left hand corner of the scan is
418 * returned. If the scan is smaller, then the rest of the image
419 * returned contains garbage. */
421 static int qc_setscanmode(struct qcam_device *q)
423 int old_mode = q->mode;
425 switch (q->transfer_scale)
427 case 1:
428 q->mode = 0;
429 break;
430 case 2:
431 q->mode = 4;
432 break;
433 case 4:
434 q->mode = 8;
435 break;
438 switch (q->bpp)
440 case 4:
441 break;
442 case 6:
443 q->mode += 2;
444 break;
447 switch (q->port_mode & QC_MODE_MASK)
449 case QC_BIDIR:
450 q->mode += 1;
451 break;
452 case QC_NOTSET:
453 case QC_UNIDIR:
454 break;
457 if (q->mode != old_mode)
458 q->status |= QC_PARAM_CHANGE;
460 return 0;
464 /* Reset the QuickCam and program for brightness, contrast,
465 * white-balance, and resolution. */
467 static void qc_set(struct qcam_device *q)
469 int val;
470 int val2;
472 qc_reset(q);
474 /* Set the brightness. Yes, this is repetitive, but it works.
475 * Shorter versions seem to fail subtly. Feel free to try :-). */
476 /* I think the problem was in qc_command, not here -- bls */
478 qc_command(q, 0xb);
479 qc_command(q, q->brightness);
481 val = q->height / q->transfer_scale;
482 qc_command(q, 0x11);
483 qc_command(q, val);
484 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
485 /* The normal "transfers per line" calculation doesn't seem to work
486 as expected here (and yet it works fine in qc_scan). No idea
487 why this case is the odd man out. Fortunately, Laird's original
488 working version gives me a good way to guess at working values.
489 -- bls */
490 val = q->width;
491 val2 = q->transfer_scale * 4;
492 } else {
493 val = q->width * q->bpp;
494 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
495 q->transfer_scale;
497 val = (val + val2 - 1) / val2;
498 qc_command(q, 0x13);
499 qc_command(q, val);
501 /* Setting top and left -- bls */
502 qc_command(q, 0xd);
503 qc_command(q, q->top);
504 qc_command(q, 0xf);
505 qc_command(q, q->left / 2);
507 qc_command(q, 0x19);
508 qc_command(q, q->contrast);
509 qc_command(q, 0x1f);
510 qc_command(q, q->whitebal);
512 /* Clear flag that we must update the grabbing parameters on the camera
513 before we grab the next frame */
514 q->status &= (~QC_PARAM_CHANGE);
517 /* Qc_readbytes reads some bytes from the QC and puts them in
518 the supplied buffer. It returns the number of bytes read,
519 or -1 on error. */
521 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
523 int ret=1;
524 unsigned int hi, lo;
525 unsigned int hi2, lo2;
526 static int state = 0;
528 if (buffer == NULL)
530 state = 0;
531 return 0;
534 switch (q->port_mode & QC_MODE_MASK)
536 case QC_BIDIR: /* Bi-directional Port */
537 write_lpcontrol(q, 0x26);
538 lo = (qc_waithand2(q, 1) >> 1);
539 hi = (read_lpstatus(q) >> 3) & 0x1f;
540 write_lpcontrol(q, 0x2e);
541 lo2 = (qc_waithand2(q, 0) >> 1);
542 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
543 switch (q->bpp)
545 case 4:
546 buffer[0] = lo & 0xf;
547 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
548 buffer[2] = (hi & 0x1e) >> 1;
549 buffer[3] = lo2 & 0xf;
550 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
551 buffer[5] = (hi2 & 0x1e) >> 1;
552 ret = 6;
553 break;
554 case 6:
555 buffer[0] = lo & 0x3f;
556 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
557 buffer[2] = lo2 & 0x3f;
558 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
559 ret = 4;
560 break;
562 break;
564 case QC_UNIDIR: /* Unidirectional Port */
565 write_lpcontrol(q, 6);
566 lo = (qc_waithand(q, 1) & 0xf0) >> 4;
567 write_lpcontrol(q, 0xe);
568 hi = (qc_waithand(q, 0) & 0xf0) >> 4;
570 switch (q->bpp)
572 case 4:
573 buffer[0] = lo;
574 buffer[1] = hi;
575 ret = 2;
576 break;
577 case 6:
578 switch (state)
580 case 0:
581 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
582 q->saved_bits = (hi & 3) << 4;
583 state = 1;
584 ret = 1;
585 break;
586 case 1:
587 buffer[0] = lo | q->saved_bits;
588 q->saved_bits = hi << 2;
589 state = 2;
590 ret = 1;
591 break;
592 case 2:
593 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
594 buffer[1] = ((lo & 3) << 4) | hi;
595 state = 0;
596 ret = 2;
597 break;
599 break;
601 break;
603 return ret;
606 /* requests a scan from the camera. It sends the correct instructions
607 * to the camera and then reads back the correct number of bytes. In
608 * previous versions of this routine the return structure contained
609 * the raw output from the camera, and there was a 'qc_convertscan'
610 * function that converted that to a useful format. In version 0.3 I
611 * rolled qc_convertscan into qc_scan and now I only return the
612 * converted scan. The format is just an one-dimensional array of
613 * characters, one for each pixel, with 0=black up to n=white, where
614 * n=2^(bit depth)-1. Ask me for more details if you don't understand
615 * this. */
617 static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len)
619 int i, j, k, yield;
620 int bytes;
621 int linestotrans, transperline;
622 int divisor;
623 int pixels_per_line;
624 int pixels_read = 0;
625 int got=0;
626 char buffer[6];
627 int shift=8-q->bpp;
628 char invert;
630 if (q->mode == -1)
631 return -ENXIO;
633 qc_command(q, 0x7);
634 qc_command(q, q->mode);
636 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
638 write_lpcontrol(q, 0x2e); /* turn port around */
639 write_lpcontrol(q, 0x26);
640 (void) qc_waithand(q, 1);
641 write_lpcontrol(q, 0x2e);
642 (void) qc_waithand(q, 0);
645 /* strange -- should be 15:63 below, but 4bpp is odd */
646 invert = (q->bpp == 4) ? 16 : 63;
648 linestotrans = q->height / q->transfer_scale;
649 pixels_per_line = q->width / q->transfer_scale;
650 transperline = q->width * q->bpp;
651 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
652 q->transfer_scale;
653 transperline = (transperline + divisor - 1) / divisor;
655 for (i = 0, yield = yieldlines; i < linestotrans; i++)
657 for (pixels_read = j = 0; j < transperline; j++)
659 bytes = qc_readbytes(q, buffer);
660 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++)
662 int o;
663 if (buffer[k] == 0 && invert == 16)
665 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
666 must be 0-15 -- bls */
667 buffer[k] = 16;
669 o=i*pixels_per_line + pixels_read + k;
670 if(o<len)
672 got++;
673 put_user((invert - buffer[k])<<shift, buf+o);
676 pixels_read += bytes;
678 (void) qc_readbytes(q, NULL); /* reset state machine */
680 /* Grabbing an entire frame from the quickcam is a lengthy
681 process. We don't (usually) want to busy-block the
682 processor for the entire frame. yieldlines is a module
683 parameter. If we yield every line, the minimum frame
684 time will be 240 / 200 = 1.2 seconds. The compile-time
685 default is to yield every 4 lines. */
686 if (i >= yield) {
687 msleep_interruptible(5);
688 yield = i + yieldlines;
692 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
694 write_lpcontrol(q, 2);
695 write_lpcontrol(q, 6);
696 udelay(3);
697 write_lpcontrol(q, 0xe);
699 if(got<len)
700 return got;
701 return len;
705 * Video4linux interfacing
708 static int qcam_do_ioctl(struct inode *inode, struct file *file,
709 unsigned int cmd, void *arg)
711 struct video_device *dev = video_devdata(file);
712 struct qcam_device *qcam=(struct qcam_device *)dev;
714 switch(cmd)
716 case VIDIOCGCAP:
718 struct video_capability *b = arg;
719 strcpy(b->name, "Quickcam");
720 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
721 b->channels = 1;
722 b->audios = 0;
723 b->maxwidth = 320;
724 b->maxheight = 240;
725 b->minwidth = 80;
726 b->minheight = 60;
727 return 0;
729 case VIDIOCGCHAN:
731 struct video_channel *v = arg;
732 if(v->channel!=0)
733 return -EINVAL;
734 v->flags=0;
735 v->tuners=0;
736 /* Good question.. its composite or SVHS so.. */
737 v->type = VIDEO_TYPE_CAMERA;
738 strcpy(v->name, "Camera");
739 return 0;
741 case VIDIOCSCHAN:
743 struct video_channel *v = arg;
744 if(v->channel!=0)
745 return -EINVAL;
746 return 0;
748 case VIDIOCGTUNER:
750 struct video_tuner *v = arg;
751 if(v->tuner)
752 return -EINVAL;
753 strcpy(v->name, "Format");
754 v->rangelow=0;
755 v->rangehigh=0;
756 v->flags= 0;
757 v->mode = VIDEO_MODE_AUTO;
758 return 0;
760 case VIDIOCSTUNER:
762 struct video_tuner *v = arg;
763 if(v->tuner)
764 return -EINVAL;
765 if(v->mode!=VIDEO_MODE_AUTO)
766 return -EINVAL;
767 return 0;
769 case VIDIOCGPICT:
771 struct video_picture *p = arg;
772 p->colour=0x8000;
773 p->hue=0x8000;
774 p->brightness=qcam->brightness<<8;
775 p->contrast=qcam->contrast<<8;
776 p->whiteness=qcam->whitebal<<8;
777 p->depth=qcam->bpp;
778 p->palette=VIDEO_PALETTE_GREY;
779 return 0;
781 case VIDIOCSPICT:
783 struct video_picture *p = arg;
784 if(p->palette!=VIDEO_PALETTE_GREY)
785 return -EINVAL;
786 if(p->depth!=4 && p->depth!=6)
787 return -EINVAL;
790 * Now load the camera.
793 qcam->brightness = p->brightness>>8;
794 qcam->contrast = p->contrast>>8;
795 qcam->whitebal = p->whiteness>>8;
796 qcam->bpp = p->depth;
798 mutex_lock(&qcam->lock);
799 qc_setscanmode(qcam);
800 mutex_unlock(&qcam->lock);
801 qcam->status |= QC_PARAM_CHANGE;
803 return 0;
805 case VIDIOCSWIN:
807 struct video_window *vw = arg;
808 if(vw->flags)
809 return -EINVAL;
810 if(vw->clipcount)
811 return -EINVAL;
812 if(vw->height<60||vw->height>240)
813 return -EINVAL;
814 if(vw->width<80||vw->width>320)
815 return -EINVAL;
817 qcam->width = 320;
818 qcam->height = 240;
819 qcam->transfer_scale = 4;
821 if(vw->width>=160 && vw->height>=120)
823 qcam->transfer_scale = 2;
825 if(vw->width>=320 && vw->height>=240)
827 qcam->width = 320;
828 qcam->height = 240;
829 qcam->transfer_scale = 1;
831 mutex_lock(&qcam->lock);
832 qc_setscanmode(qcam);
833 mutex_unlock(&qcam->lock);
835 /* We must update the camera before we grab. We could
836 just have changed the grab size */
837 qcam->status |= QC_PARAM_CHANGE;
839 /* Ok we figured out what to use from our wide choice */
840 return 0;
842 case VIDIOCGWIN:
844 struct video_window *vw = arg;
845 memset(vw, 0, sizeof(*vw));
846 vw->width=qcam->width/qcam->transfer_scale;
847 vw->height=qcam->height/qcam->transfer_scale;
848 return 0;
850 case VIDIOCKEY:
851 return 0;
852 case VIDIOCCAPTURE:
853 case VIDIOCGFBUF:
854 case VIDIOCSFBUF:
855 case VIDIOCGFREQ:
856 case VIDIOCSFREQ:
857 case VIDIOCGAUDIO:
858 case VIDIOCSAUDIO:
859 return -EINVAL;
860 default:
861 return -ENOIOCTLCMD;
863 return 0;
866 static int qcam_ioctl(struct inode *inode, struct file *file,
867 unsigned int cmd, unsigned long arg)
869 return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
872 static ssize_t qcam_read(struct file *file, char __user *buf,
873 size_t count, loff_t *ppos)
875 struct video_device *v = video_devdata(file);
876 struct qcam_device *qcam=(struct qcam_device *)v;
877 int len;
878 parport_claim_or_block(qcam->pdev);
880 mutex_lock(&qcam->lock);
882 qc_reset(qcam);
884 /* Update the camera parameters if we need to */
885 if (qcam->status & QC_PARAM_CHANGE)
886 qc_set(qcam);
888 len=qc_capture(qcam, buf,count);
890 mutex_unlock(&qcam->lock);
892 parport_release(qcam->pdev);
893 return len;
896 static const struct file_operations qcam_fops = {
897 .owner = THIS_MODULE,
898 .open = video_exclusive_open,
899 .release = video_exclusive_release,
900 .ioctl = qcam_ioctl,
901 .compat_ioctl = v4l_compat_ioctl32,
902 .read = qcam_read,
903 .llseek = no_llseek,
905 static struct video_device qcam_template=
907 .owner = THIS_MODULE,
908 .name = "Connectix Quickcam",
909 .type = VID_TYPE_CAPTURE,
910 .fops = &qcam_fops,
913 #define MAX_CAMS 4
914 static struct qcam_device *qcams[MAX_CAMS];
915 static unsigned int num_cams = 0;
917 static int init_bwqcam(struct parport *port)
919 struct qcam_device *qcam;
921 if (num_cams == MAX_CAMS)
923 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
924 return -ENOSPC;
927 qcam=qcam_init(port);
928 if(qcam==NULL)
929 return -ENODEV;
931 parport_claim_or_block(qcam->pdev);
933 qc_reset(qcam);
935 if(qc_detect(qcam)==0)
937 parport_release(qcam->pdev);
938 parport_unregister_device(qcam->pdev);
939 kfree(qcam);
940 return -ENODEV;
942 qc_calibrate(qcam);
944 parport_release(qcam->pdev);
946 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
948 if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
950 parport_unregister_device(qcam->pdev);
951 kfree(qcam);
952 return -ENODEV;
955 qcams[num_cams++] = qcam;
957 return 0;
960 static void close_bwqcam(struct qcam_device *qcam)
962 video_unregister_device(&qcam->vdev);
963 parport_unregister_device(qcam->pdev);
964 kfree(qcam);
967 /* The parport parameter controls which parports will be scanned.
968 * Scanning all parports causes some printers to print a garbage page.
969 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
970 #ifdef MODULE
971 static char *parport[MAX_CAMS] = { NULL, };
972 module_param_array(parport, charp, NULL, 0);
973 #endif
975 static int accept_bwqcam(struct parport *port)
977 #ifdef MODULE
978 int n;
980 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
981 /* user gave parport parameters */
982 for(n=0; parport[n] && n<MAX_CAMS; n++){
983 char *ep;
984 unsigned long r;
985 r = simple_strtoul(parport[n], &ep, 0);
986 if (ep == parport[n]) {
987 printk(KERN_ERR
988 "bw-qcam: bad port specifier \"%s\"\n",
989 parport[n]);
990 continue;
992 if (r == port->number)
993 return 1;
995 return 0;
997 #endif
998 return 1;
1001 static void bwqcam_attach(struct parport *port)
1003 if (accept_bwqcam(port))
1004 init_bwqcam(port);
1007 static void bwqcam_detach(struct parport *port)
1009 int i;
1010 for (i = 0; i < num_cams; i++) {
1011 struct qcam_device *qcam = qcams[i];
1012 if (qcam && qcam->pdev->port == port) {
1013 qcams[i] = NULL;
1014 close_bwqcam(qcam);
1019 static struct parport_driver bwqcam_driver = {
1020 .name = "bw-qcam",
1021 .attach = bwqcam_attach,
1022 .detach = bwqcam_detach,
1025 static void __exit exit_bw_qcams(void)
1027 parport_unregister_driver(&bwqcam_driver);
1030 static int __init init_bw_qcams(void)
1032 #ifdef MODULE
1033 /* Do some sanity checks on the module parameters. */
1034 if (maxpoll > 5000) {
1035 printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1036 maxpoll = 5000;
1039 if (yieldlines < 1) {
1040 printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1041 yieldlines = 1;
1043 #endif
1044 return parport_register_driver(&bwqcam_driver);
1047 module_init(init_bw_qcams);
1048 module_exit(exit_bw_qcams);
1050 MODULE_LICENSE("GPL");