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