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