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