1/*
2 *    QuickCam Driver For Video4Linux.
3 *
4 *	Video4Linux conversion work by Alan Cox.
5 *	Parport compatibility by Phil Blundell.
6 *	Busy loop avoidance by Mark Cooke.
7 *
8 *    Module parameters:
9 *
10 *	maxpoll=<1 - 5000>
11 *
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.
15 *
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.
20 *
21 *	yieldlines=<1 - 250>
22 *
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.
28 */
29
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. */
33
34
35/* Version 0.5, August 4, 1996 */
36/* Version 0.7, August 27, 1996 */
37/* Version 0.9, November 17, 1996 */
38
39
40/******************************************************************
41
42Copyright (C) 1996 by Scott Laird
43
44Permission is hereby granted, free of charge, to any person obtaining
45a copy of this software and associated documentation files (the
46"Software"), to deal in the Software without restriction, including
47without limitation the rights to use, copy, modify, merge, publish,
48distribute, sublicense, and/or sell copies of the Software, and to
49permit persons to whom the Software is furnished to do so, subject to
50the following conditions:
51
52The above copyright notice and this permission notice shall be
53included in all copies or substantial portions of the Software.
54
55THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY CLAIM, DAMAGES OR
59OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
60ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
61OTHER DEALINGS IN THE SOFTWARE.
62
63******************************************************************/
64
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>
79
80#include "bw-qcam.h"
81
82static unsigned int maxpoll=250;   /* Maximum busy-loop count for qcam I/O */
83static unsigned int yieldlines=4;  /* Yield after this many during capture */
84static int video_nr = -1;
85
86module_param(maxpoll, int, 0);
87module_param(yieldlines, int, 0);
88module_param(video_nr, int, 0);
89
90static inline int read_lpstatus(struct qcam_device *q)
91{
92	return parport_read_status(q->pport);
93}
94
95static inline int read_lpdata(struct qcam_device *q)
96{
97	return parport_read_data(q->pport);
98}
99
100static inline void write_lpdata(struct qcam_device *q, int d)
101{
102	parport_write_data(q->pport, d);
103}
104
105static inline void write_lpcontrol(struct qcam_device *q, int d)
106{
107	parport_write_control(q->pport, d);
108}
109
110static int qc_waithand(struct qcam_device *q, int val);
111static int qc_command(struct qcam_device *q, int command);
112static int qc_readparam(struct qcam_device *q);
113static int qc_setscanmode(struct qcam_device *q);
114static int qc_readbytes(struct qcam_device *q, char buffer[]);
115
116static struct video_device qcam_template;
117
118static int qc_calibrate(struct qcam_device *q)
119{
120	/*
121	 *	Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
122	 *	The white balance is an individiual value for each
123	 *	quickcam.
124	 */
125
126	int value;
127	int count = 0;
128
129	qc_command(q, 27);	/* AutoAdjustOffset */
130	qc_command(q, 0);	/* Dummy Parameter, ignored by the camera */
131
132	/* GetOffset (33) will read 255 until autocalibration */
133	/* is finished. After that, a value of 1-254 will be */
134	/* returned. */
135
136	do {
137		qc_command(q, 33);
138		value = qc_readparam(q);
139		mdelay(1);
140		schedule();
141		count++;
142	} while (value == 0xff && count<2048);
143
144	q->whitebal = value;
145	return value;
146}
147
148/* Initialize the QuickCam driver control structure.  This is where
149 * defaults are set for people who don't have a config file.*/
150
151static struct qcam_device *qcam_init(struct parport *port)
152{
153	struct qcam_device *q;
154
155	q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
156	if(q==NULL)
157		return NULL;
158
159	q->pport = port;
160	q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
161					  NULL, 0, NULL);
162	if (q->pdev == NULL)
163	{
164		printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
165		       port->name);
166		kfree(q);
167		return NULL;
168	}
169
170	memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
171
172	mutex_init(&q->lock);
173
174	q->port_mode = (QC_ANY | QC_NOTSET);
175	q->width = 320;
176	q->height = 240;
177	q->bpp = 4;
178	q->transfer_scale = 2;
179	q->contrast = 192;
180	q->brightness = 180;
181	q->whitebal = 105;
182	q->top = 1;
183	q->left = 14;
184	q->mode = -1;
185	q->status = QC_PARAM_CHANGE;
186	return q;
187}
188
189
190/* qc_command is probably a bit of a misnomer -- it's used to send
191 * bytes *to* the camera.  Generally, these bytes are either commands
192 * or arguments to commands, so the name fits, but it still bugs me a
193 * bit.  See the documentation for a list of commands. */
194
195static int qc_command(struct qcam_device *q, int command)
196{
197	int n1, n2;
198	int cmd;
199
200	write_lpdata(q, command);
201	write_lpcontrol(q, 6);
202
203	n1 = qc_waithand(q, 1);
204
205	write_lpcontrol(q, 0xe);
206	n2 = qc_waithand(q, 0);
207
208	cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
209	return cmd;
210}
211
212static int qc_readparam(struct qcam_device *q)
213{
214	int n1, n2;
215	int cmd;
216
217	write_lpcontrol(q, 6);
218	n1 = qc_waithand(q, 1);
219
220	write_lpcontrol(q, 0xe);
221	n2 = qc_waithand(q, 0);
222
223	cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
224	return cmd;
225}
226
227/* qc_waithand busy-waits for a handshake signal from the QuickCam.
228 * Almost all communication with the camera requires handshaking. */
229
230static int qc_waithand(struct qcam_device *q, int val)
231{
232	int status;
233	int runs=0;
234
235	if (val)
236	{
237		while (!((status = read_lpstatus(q)) & 8))
238		{
239			/* 1000 is enough spins on the I/O for all normal
240			   cases, at that point we start to poll slowly
241			   until the camera wakes up. However, we are
242			   busy blocked until the camera responds, so
243			   setting it lower is much better for interactive
244			   response. */
245
246			if(runs++>maxpoll)
247			{
248				msleep_interruptible(5);
249			}
250			if(runs>(maxpoll+1000)) /* 5 seconds */
251				return -1;
252		}
253	}
254	else
255	{
256		while (((status = read_lpstatus(q)) & 8))
257		{
258			/* 1000 is enough spins on the I/O for all normal
259			   cases, at that point we start to poll slowly
260			   until the camera wakes up. However, we are
261			   busy blocked until the camera responds, so
262			   setting it lower is much better for interactive
263			   response. */
264
265			if(runs++>maxpoll)
266			{
267				msleep_interruptible(5);
268			}
269			if(runs++>(maxpoll+1000)) /* 5 seconds */
270				return -1;
271		}
272	}
273
274	return status;
275}
276
277/* Waithand2 is used when the qcam is in bidirectional mode, and the
278 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
279 * (bit 3 of status register).  It also returns the last value read,
280 * since this data is useful. */
281
282static unsigned int qc_waithand2(struct qcam_device *q, int val)
283{
284	unsigned int status;
285	int runs=0;
286
287	do
288	{
289		status = read_lpdata(q);
290		/* 1000 is enough spins on the I/O for all normal
291		   cases, at that point we start to poll slowly
292		   until the camera wakes up. However, we are
293		   busy blocked until the camera responds, so
294		   setting it lower is much better for interactive
295		   response. */
296
297		if(runs++>maxpoll)
298		{
299			msleep_interruptible(5);
300		}
301		if(runs++>(maxpoll+1000)) /* 5 seconds */
302			return 0;
303	}
304	while ((status & 1) != val);
305
306	return status;
307}
308
309
310/* Try to detect a QuickCam.  It appears to flash the upper 4 bits of
311   the status register at 5-10 Hz.  This is only used in the autoprobe
312   code.  Be aware that this isn't the way Connectix detects the
313   camera (they send a reset and try to handshake), but this should be
314   almost completely safe, while their method screws up my printer if
315   I plug it in before the camera. */
316
317static int qc_detect(struct qcam_device *q)
318{
319	int reg, lastreg;
320	int count = 0;
321	int i;
322
323	lastreg = reg = read_lpstatus(q) & 0xf0;
324
325	for (i = 0; i < 500; i++)
326	{
327		reg = read_lpstatus(q) & 0xf0;
328		if (reg != lastreg)
329			count++;
330		lastreg = reg;
331		mdelay(2);
332	}
333
334
335
336	/* Be (even more) liberal in what you accept...  */
337
338/*	if (count > 30 && count < 200) */
339	if (count > 20 && count < 300)
340		return 1;	/* found */
341	else
342		return 0;	/* not found */
343}
344
345
346/* Reset the QuickCam.  This uses the same sequence the Windows
347 * QuickPic program uses.  Someone with a bi-directional port should
348 * check that bi-directional mode is detected right, and then
349 * implement bi-directional mode in qc_readbyte(). */
350
351static void qc_reset(struct qcam_device *q)
352{
353	switch (q->port_mode & QC_FORCE_MASK)
354	{
355		case QC_FORCE_UNIDIR:
356			q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
357			break;
358
359		case QC_FORCE_BIDIR:
360			q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
361			break;
362
363		case QC_ANY:
364			write_lpcontrol(q, 0x20);
365			write_lpdata(q, 0x75);
366
367			if (read_lpdata(q) != 0x75) {
368				q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
369			} else {
370				q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
371			}
372			break;
373	}
374
375	write_lpcontrol(q, 0xb);
376	udelay(250);
377	write_lpcontrol(q, 0xe);
378	qc_setscanmode(q);		/* in case port_mode changed */
379}
380
381
382/* Decide which scan mode to use.  There's no real requirement that
383 * the scanmode match the resolution in q->height and q-> width -- the
384 * camera takes the picture at the resolution specified in the
385 * "scanmode" and then returns the image at the resolution specified
386 * with the resolution commands.  If the scan is bigger than the
387 * requested resolution, the upper-left hand corner of the scan is
388 * returned.  If the scan is smaller, then the rest of the image
389 * returned contains garbage. */
390
391static int qc_setscanmode(struct qcam_device *q)
392{
393	int old_mode = q->mode;
394
395	switch (q->transfer_scale)
396	{
397		case 1:
398			q->mode = 0;
399			break;
400		case 2:
401			q->mode = 4;
402			break;
403		case 4:
404			q->mode = 8;
405			break;
406	}
407
408	switch (q->bpp)
409	{
410		case 4:
411			break;
412		case 6:
413			q->mode += 2;
414			break;
415	}
416
417	switch (q->port_mode & QC_MODE_MASK)
418	{
419		case QC_BIDIR:
420			q->mode += 1;
421			break;
422		case QC_NOTSET:
423		case QC_UNIDIR:
424			break;
425	}
426
427	if (q->mode != old_mode)
428		q->status |= QC_PARAM_CHANGE;
429
430	return 0;
431}
432
433
434/* Reset the QuickCam and program for brightness, contrast,
435 * white-balance, and resolution. */
436
437static void qc_set(struct qcam_device *q)
438{
439	int val;
440	int val2;
441
442	qc_reset(q);
443
444	/* Set the brightness.  Yes, this is repetitive, but it works.
445	 * Shorter versions seem to fail subtly.  Feel free to try :-). */
446	/* I think the problem was in qc_command, not here -- bls */
447
448	qc_command(q, 0xb);
449	qc_command(q, q->brightness);
450
451	val = q->height / q->transfer_scale;
452	qc_command(q, 0x11);
453	qc_command(q, val);
454	if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
455		/* The normal "transfers per line" calculation doesn't seem to work
456		   as expected here (and yet it works fine in qc_scan).  No idea
457		   why this case is the odd man out.  Fortunately, Laird's original
458		   working version gives me a good way to guess at working values.
459		   -- bls */
460		val = q->width;
461		val2 = q->transfer_scale * 4;
462	} else {
463		val = q->width * q->bpp;
464		val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
465		    q->transfer_scale;
466	}
467	val = (val + val2 - 1) / val2;
468	qc_command(q, 0x13);
469	qc_command(q, val);
470
471	/* Setting top and left -- bls */
472	qc_command(q, 0xd);
473	qc_command(q, q->top);
474	qc_command(q, 0xf);
475	qc_command(q, q->left / 2);
476
477	qc_command(q, 0x19);
478	qc_command(q, q->contrast);
479	qc_command(q, 0x1f);
480	qc_command(q, q->whitebal);
481
482	/* Clear flag that we must update the grabbing parameters on the camera
483	   before we grab the next frame */
484	q->status &= (~QC_PARAM_CHANGE);
485}
486
487/* Qc_readbytes reads some bytes from the QC and puts them in
488   the supplied buffer.  It returns the number of bytes read,
489   or -1 on error. */
490
491static inline int qc_readbytes(struct qcam_device *q, char buffer[])
492{
493	int ret=1;
494	unsigned int hi, lo;
495	unsigned int hi2, lo2;
496	static int state = 0;
497
498	if (buffer == NULL)
499	{
500		state = 0;
501		return 0;
502	}
503
504	switch (q->port_mode & QC_MODE_MASK)
505	{
506		case QC_BIDIR:		/* Bi-directional Port */
507			write_lpcontrol(q, 0x26);
508			lo = (qc_waithand2(q, 1) >> 1);
509			hi = (read_lpstatus(q) >> 3) & 0x1f;
510			write_lpcontrol(q, 0x2e);
511			lo2 = (qc_waithand2(q, 0) >> 1);
512			hi2 = (read_lpstatus(q) >> 3) & 0x1f;
513			switch (q->bpp)
514			{
515				case 4:
516					buffer[0] = lo & 0xf;
517					buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
518					buffer[2] = (hi & 0x1e) >> 1;
519					buffer[3] = lo2 & 0xf;
520					buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
521					buffer[5] = (hi2 & 0x1e) >> 1;
522					ret = 6;
523					break;
524				case 6:
525					buffer[0] = lo & 0x3f;
526					buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
527					buffer[2] = lo2 & 0x3f;
528					buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
529					ret = 4;
530					break;
531			}
532			break;
533
534		case QC_UNIDIR:	/* Unidirectional Port */
535			write_lpcontrol(q, 6);
536			lo = (qc_waithand(q, 1) & 0xf0) >> 4;
537			write_lpcontrol(q, 0xe);
538			hi = (qc_waithand(q, 0) & 0xf0) >> 4;
539
540			switch (q->bpp)
541			{
542				case 4:
543					buffer[0] = lo;
544					buffer[1] = hi;
545					ret = 2;
546					break;
547				case 6:
548					switch (state)
549					{
550						case 0:
551							buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
552							q->saved_bits = (hi & 3) << 4;
553							state = 1;
554							ret = 1;
555							break;
556						case 1:
557							buffer[0] = lo | q->saved_bits;
558							q->saved_bits = hi << 2;
559							state = 2;
560							ret = 1;
561							break;
562						case 2:
563							buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
564							buffer[1] = ((lo & 3) << 4) | hi;
565							state = 0;
566							ret = 2;
567							break;
568					}
569					break;
570			}
571			break;
572	}
573	return ret;
574}
575
576/* requests a scan from the camera.  It sends the correct instructions
577 * to the camera and then reads back the correct number of bytes.  In
578 * previous versions of this routine the return structure contained
579 * the raw output from the camera, and there was a 'qc_convertscan'
580 * function that converted that to a useful format.  In version 0.3 I
581 * rolled qc_convertscan into qc_scan and now I only return the
582 * converted scan.  The format is just an one-dimensional array of
583 * characters, one for each pixel, with 0=black up to n=white, where
584 * n=2^(bit depth)-1.  Ask me for more details if you don't understand
585 * this. */
586
587static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len)
588{
589	int i, j, k, yield;
590	int bytes;
591	int linestotrans, transperline;
592	int divisor;
593	int pixels_per_line;
594	int pixels_read = 0;
595	int got=0;
596	char buffer[6];
597	int  shift=8-q->bpp;
598	char invert;
599
600	if (q->mode == -1)
601		return -ENXIO;
602
603	qc_command(q, 0x7);
604	qc_command(q, q->mode);
605
606	if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
607	{
608		write_lpcontrol(q, 0x2e);	/* turn port around */
609		write_lpcontrol(q, 0x26);
610		(void) qc_waithand(q, 1);
611		write_lpcontrol(q, 0x2e);
612		(void) qc_waithand(q, 0);
613	}
614
615	/* strange -- should be 15:63 below, but 4bpp is odd */
616	invert = (q->bpp == 4) ? 16 : 63;
617
618	linestotrans = q->height / q->transfer_scale;
619	pixels_per_line = q->width / q->transfer_scale;
620	transperline = q->width * q->bpp;
621	divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
622	    q->transfer_scale;
623	transperline = (transperline + divisor - 1) / divisor;
624
625	for (i = 0, yield = yieldlines; i < linestotrans; i++)
626	{
627		for (pixels_read = j = 0; j < transperline; j++)
628		{
629			bytes = qc_readbytes(q, buffer);
630			for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++)
631			{
632				int o;
633				if (buffer[k] == 0 && invert == 16)
634				{
635					/* 4bpp is odd (again) -- inverter is 16, not 15, but output
636					   must be 0-15 -- bls */
637					buffer[k] = 16;
638				}
639				o=i*pixels_per_line + pixels_read + k;
640				if(o<len)
641				{
642					got++;
643					put_user((invert - buffer[k])<<shift, buf+o);
644				}
645			}
646			pixels_read += bytes;
647		}
648		(void) qc_readbytes(q, NULL);	/* reset state machine */
649
650		/* Grabbing an entire frame from the quickcam is a lengthy
651		   process. We don't (usually) want to busy-block the
652		   processor for the entire frame. yieldlines is a module
653		   parameter. If we yield every line, the minimum frame
654		   time will be 240 / 200 = 1.2 seconds. The compile-time
655		   default is to yield every 4 lines. */
656		if (i >= yield) {
657			msleep_interruptible(5);
658			yield = i + yieldlines;
659		}
660	}
661
662	if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR)
663	{
664		write_lpcontrol(q, 2);
665		write_lpcontrol(q, 6);
666		udelay(3);
667		write_lpcontrol(q, 0xe);
668	}
669	if(got<len)
670		return got;
671	return len;
672}
673
674/*
675 *	Video4linux interfacing
676 */
677
678static int qcam_do_ioctl(struct inode *inode, struct file *file,
679			 unsigned int cmd, void *arg)
680{
681	struct video_device *dev = video_devdata(file);
682	struct qcam_device *qcam=(struct qcam_device *)dev;
683
684	switch(cmd)
685	{
686		case VIDIOCGCAP:
687		{
688			struct video_capability *b = arg;
689			strcpy(b->name, "Quickcam");
690			b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
691			b->channels = 1;
692			b->audios = 0;
693			b->maxwidth = 320;
694			b->maxheight = 240;
695			b->minwidth = 80;
696			b->minheight = 60;
697			return 0;
698		}
699		case VIDIOCGCHAN:
700		{
701			struct video_channel *v = arg;
702			if(v->channel!=0)
703				return -EINVAL;
704			v->flags=0;
705			v->tuners=0;
706			/* Good question.. its composite or SVHS so.. */
707			v->type = VIDEO_TYPE_CAMERA;
708			strcpy(v->name, "Camera");
709			return 0;
710		}
711		case VIDIOCSCHAN:
712		{
713			struct video_channel *v = arg;
714			if(v->channel!=0)
715				return -EINVAL;
716			return 0;
717		}
718		case VIDIOCGTUNER:
719		{
720			struct video_tuner *v = arg;
721			if(v->tuner)
722				return -EINVAL;
723			strcpy(v->name, "Format");
724			v->rangelow=0;
725			v->rangehigh=0;
726			v->flags= 0;
727			v->mode = VIDEO_MODE_AUTO;
728			return 0;
729		}
730		case VIDIOCSTUNER:
731		{
732			struct video_tuner *v = arg;
733			if(v->tuner)
734				return -EINVAL;
735			if(v->mode!=VIDEO_MODE_AUTO)
736				return -EINVAL;
737			return 0;
738		}
739		case VIDIOCGPICT:
740		{
741			struct video_picture *p = arg;
742			p->colour=0x8000;
743			p->hue=0x8000;
744			p->brightness=qcam->brightness<<8;
745			p->contrast=qcam->contrast<<8;
746			p->whiteness=qcam->whitebal<<8;
747			p->depth=qcam->bpp;
748			p->palette=VIDEO_PALETTE_GREY;
749			return 0;
750		}
751		case VIDIOCSPICT:
752		{
753			struct video_picture *p = arg;
754			if(p->palette!=VIDEO_PALETTE_GREY)
755				return -EINVAL;
756			if(p->depth!=4 && p->depth!=6)
757				return -EINVAL;
758
759			/*
760			 *	Now load the camera.
761			 */
762
763			qcam->brightness = p->brightness>>8;
764			qcam->contrast = p->contrast>>8;
765			qcam->whitebal = p->whiteness>>8;
766			qcam->bpp = p->depth;
767
768			mutex_lock(&qcam->lock);
769			qc_setscanmode(qcam);
770			mutex_unlock(&qcam->lock);
771			qcam->status |= QC_PARAM_CHANGE;
772
773			return 0;
774		}
775		case VIDIOCSWIN:
776		{
777			struct video_window *vw = arg;
778			if(vw->flags)
779				return -EINVAL;
780			if(vw->clipcount)
781				return -EINVAL;
782			if(vw->height<60||vw->height>240)
783				return -EINVAL;
784			if(vw->width<80||vw->width>320)
785				return -EINVAL;
786
787			qcam->width = 320;
788			qcam->height = 240;
789			qcam->transfer_scale = 4;
790
791			if(vw->width>=160 && vw->height>=120)
792			{
793				qcam->transfer_scale = 2;
794			}
795			if(vw->width>=320 && vw->height>=240)
796			{
797				qcam->width = 320;
798				qcam->height = 240;
799				qcam->transfer_scale = 1;
800			}
801			mutex_lock(&qcam->lock);
802			qc_setscanmode(qcam);
803			mutex_unlock(&qcam->lock);
804
805			/* We must update the camera before we grab. We could
806			   just have changed the grab size */
807			qcam->status |= QC_PARAM_CHANGE;
808
809			/* Ok we figured out what to use from our wide choice */
810			return 0;
811		}
812		case VIDIOCGWIN:
813		{
814			struct video_window *vw = arg;
815			memset(vw, 0, sizeof(*vw));
816			vw->width=qcam->width/qcam->transfer_scale;
817			vw->height=qcam->height/qcam->transfer_scale;
818			return 0;
819		}
820		case VIDIOCKEY:
821			return 0;
822		case VIDIOCCAPTURE:
823		case VIDIOCGFBUF:
824		case VIDIOCSFBUF:
825		case VIDIOCGFREQ:
826		case VIDIOCSFREQ:
827		case VIDIOCGAUDIO:
828		case VIDIOCSAUDIO:
829			return -EINVAL;
830		default:
831			return -ENOIOCTLCMD;
832	}
833	return 0;
834}
835
836static int qcam_ioctl(struct inode *inode, struct file *file,
837		     unsigned int cmd, unsigned long arg)
838{
839	return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);
840}
841
842static ssize_t qcam_read(struct file *file, char __user *buf,
843			 size_t count, loff_t *ppos)
844{
845	struct video_device *v = video_devdata(file);
846	struct qcam_device *qcam=(struct qcam_device *)v;
847	int len;
848	parport_claim_or_block(qcam->pdev);
849
850	mutex_lock(&qcam->lock);
851
852	qc_reset(qcam);
853
854	/* Update the camera parameters if we need to */
855	if (qcam->status & QC_PARAM_CHANGE)
856		qc_set(qcam);
857
858	len=qc_capture(qcam, buf,count);
859
860	mutex_unlock(&qcam->lock);
861
862	parport_release(qcam->pdev);
863	return len;
864}
865
866static const struct file_operations qcam_fops = {
867	.owner		= THIS_MODULE,
868	.open           = video_exclusive_open,
869	.release        = video_exclusive_release,
870	.ioctl          = qcam_ioctl,
871	.compat_ioctl	= v4l_compat_ioctl32,
872	.read		= qcam_read,
873	.llseek         = no_llseek,
874};
875static struct video_device qcam_template=
876{
877	.owner		= THIS_MODULE,
878	.name		= "Connectix Quickcam",
879	.type		= VID_TYPE_CAPTURE,
880	.hardware	= VID_HARDWARE_QCAM_BW,
881	.fops           = &qcam_fops,
882};
883
884#define MAX_CAMS 4
885static struct qcam_device *qcams[MAX_CAMS];
886static unsigned int num_cams = 0;
887
888static int init_bwqcam(struct parport *port)
889{
890	struct qcam_device *qcam;
891
892	if (num_cams == MAX_CAMS)
893	{
894		printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
895		return -ENOSPC;
896	}
897
898	qcam=qcam_init(port);
899	if(qcam==NULL)
900		return -ENODEV;
901
902	parport_claim_or_block(qcam->pdev);
903
904	qc_reset(qcam);
905
906	if(qc_detect(qcam)==0)
907	{
908		parport_release(qcam->pdev);
909		parport_unregister_device(qcam->pdev);
910		kfree(qcam);
911		return -ENODEV;
912	}
913	qc_calibrate(qcam);
914
915	parport_release(qcam->pdev);
916
917	printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
918
919	if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1)
920	{
921		parport_unregister_device(qcam->pdev);
922		kfree(qcam);
923		return -ENODEV;
924	}
925
926	qcams[num_cams++] = qcam;
927
928	return 0;
929}
930
931static void close_bwqcam(struct qcam_device *qcam)
932{
933	video_unregister_device(&qcam->vdev);
934	parport_unregister_device(qcam->pdev);
935	kfree(qcam);
936}
937
938/* The parport parameter controls which parports will be scanned.
939 * Scanning all parports causes some printers to print a garbage page.
940 *       -- March 14, 1999  Billy Donahue <billy@escape.com> */
941#ifdef MODULE
942static char *parport[MAX_CAMS] = { NULL, };
943module_param_array(parport, charp, NULL, 0);
944#endif
945
946static int accept_bwqcam(struct parport *port)
947{
948#ifdef MODULE
949	int n;
950
951	if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
952		/* user gave parport parameters */
953		for(n=0; parport[n] && n<MAX_CAMS; n++){
954			char *ep;
955			unsigned long r;
956			r = simple_strtoul(parport[n], &ep, 0);
957			if (ep == parport[n]) {
958				printk(KERN_ERR
959					"bw-qcam: bad port specifier \"%s\"\n",
960					parport[n]);
961				continue;
962			}
963			if (r == port->number)
964				return 1;
965		}
966		return 0;
967	}
968#endif
969	return 1;
970}
971
972static void bwqcam_attach(struct parport *port)
973{
974	if (accept_bwqcam(port))
975		init_bwqcam(port);
976}
977
978static void bwqcam_detach(struct parport *port)
979{
980	int i;
981	for (i = 0; i < num_cams; i++) {
982		struct qcam_device *qcam = qcams[i];
983		if (qcam && qcam->pdev->port == port) {
984			qcams[i] = NULL;
985			close_bwqcam(qcam);
986		}
987	}
988}
989
990static struct parport_driver bwqcam_driver = {
991	.name	= "bw-qcam",
992	.attach	= bwqcam_attach,
993	.detach	= bwqcam_detach,
994};
995
996static void __exit exit_bw_qcams(void)
997{
998	parport_unregister_driver(&bwqcam_driver);
999}
1000
1001static int __init init_bw_qcams(void)
1002{
1003#ifdef MODULE
1004	/* Do some sanity checks on the module parameters. */
1005	if (maxpoll > 5000) {
1006		printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1007		maxpoll = 5000;
1008	}
1009
1010	if (yieldlines < 1) {
1011		printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1012		yieldlines = 1;
1013	}
1014#endif
1015	return parport_register_driver(&bwqcam_driver);
1016}
1017
1018module_init(init_bw_qcams);
1019module_exit(exit_bw_qcams);
1020
1021MODULE_LICENSE("GPL");
1022