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