sfc: Disable setting feature flags that are not implemented
[linux-2.6/x86.git] / drivers / media / video / bw-qcam.c
blob3c9e754d73a053199376098170ee5e80b9a86f37
1 /*
2 * QuickCam Driver For Video4Linux.
4 * Video4Linux conversion work by Alan Cox.
5 * Parport compatibility by Phil Blundell.
6 * Busy loop avoidance by Mark Cooke.
8 * Module parameters:
10 * maxpoll=<1 - 5000>
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.
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.
21 * yieldlines=<1 - 250>
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.
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. */
35 /* Version 0.5, August 4, 1996 */
36 /* Version 0.7, August 27, 1996 */
37 /* Version 0.9, November 17, 1996 */
40 /******************************************************************
42 Copyright (C) 1996 by Scott Laird
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:
52 The above copyright notice and this permission notice shall be
53 included in all copies or substantial portions of the Software.
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.
63 ******************************************************************/
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 <media/v4l2-ioctl.h>
78 #include <linux/mutex.h>
79 #include <asm/uaccess.h>
81 #include "bw-qcam.h"
83 static unsigned int maxpoll = 250; /* Maximum busy-loop count for qcam I/O */
84 static unsigned int yieldlines = 4; /* Yield after this many during capture */
85 static int video_nr = -1;
86 static unsigned int force_init; /* Whether to probe aggressively */
88 module_param(maxpoll, int, 0);
89 module_param(yieldlines, int, 0);
90 module_param(video_nr, int, 0);
92 /* Set force_init=1 to avoid detection by polling status register and
93 * immediately attempt to initialize qcam */
94 module_param(force_init, int, 0);
96 static inline int read_lpstatus(struct qcam_device *q)
98 return parport_read_status(q->pport);
101 static inline int read_lpdata(struct qcam_device *q)
103 return parport_read_data(q->pport);
106 static inline void write_lpdata(struct qcam_device *q, int d)
108 parport_write_data(q->pport, d);
111 static inline void write_lpcontrol(struct qcam_device *q, int d)
113 if (d & 0x20) {
114 /* Set bidirectional mode to reverse (data in) */
115 parport_data_reverse(q->pport);
116 } else {
117 /* Set bidirectional mode to forward (data out) */
118 parport_data_forward(q->pport);
121 /* Now issue the regular port command, but strip out the
122 * direction flag */
123 d &= ~0x20;
124 parport_write_control(q->pport, d);
127 static int qc_waithand(struct qcam_device *q, int val);
128 static int qc_command(struct qcam_device *q, int command);
129 static int qc_readparam(struct qcam_device *q);
130 static int qc_setscanmode(struct qcam_device *q);
131 static int qc_readbytes(struct qcam_device *q, char buffer[]);
133 static struct video_device qcam_template;
135 static int qc_calibrate(struct qcam_device *q)
138 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
139 * The white balance is an individiual value for each
140 * quickcam.
143 int value;
144 int count = 0;
146 qc_command(q, 27); /* AutoAdjustOffset */
147 qc_command(q, 0); /* Dummy Parameter, ignored by the camera */
149 /* GetOffset (33) will read 255 until autocalibration */
150 /* is finished. After that, a value of 1-254 will be */
151 /* returned. */
153 do {
154 qc_command(q, 33);
155 value = qc_readparam(q);
156 mdelay(1);
157 schedule();
158 count++;
159 } while (value == 0xff && count < 2048);
161 q->whitebal = value;
162 return value;
165 /* Initialize the QuickCam driver control structure. This is where
166 * defaults are set for people who don't have a config file.*/
168 static struct qcam_device *qcam_init(struct parport *port)
170 struct qcam_device *q;
172 q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
173 if (q == NULL)
174 return NULL;
176 q->pport = port;
177 q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
178 NULL, 0, NULL);
179 if (q->pdev == NULL) {
180 printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
181 port->name);
182 kfree(q);
183 return NULL;
186 memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
188 mutex_init(&q->lock);
190 q->port_mode = (QC_ANY | QC_NOTSET);
191 q->width = 320;
192 q->height = 240;
193 q->bpp = 4;
194 q->transfer_scale = 2;
195 q->contrast = 192;
196 q->brightness = 180;
197 q->whitebal = 105;
198 q->top = 1;
199 q->left = 14;
200 q->mode = -1;
201 q->status = QC_PARAM_CHANGE;
202 return q;
206 /* qc_command is probably a bit of a misnomer -- it's used to send
207 * bytes *to* the camera. Generally, these bytes are either commands
208 * or arguments to commands, so the name fits, but it still bugs me a
209 * bit. See the documentation for a list of commands. */
211 static int qc_command(struct qcam_device *q, int command)
213 int n1, n2;
214 int cmd;
216 write_lpdata(q, command);
217 write_lpcontrol(q, 6);
219 n1 = qc_waithand(q, 1);
221 write_lpcontrol(q, 0xe);
222 n2 = qc_waithand(q, 0);
224 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
225 return cmd;
228 static int qc_readparam(struct qcam_device *q)
230 int n1, n2;
231 int cmd;
233 write_lpcontrol(q, 6);
234 n1 = qc_waithand(q, 1);
236 write_lpcontrol(q, 0xe);
237 n2 = qc_waithand(q, 0);
239 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
240 return cmd;
243 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
244 * Almost all communication with the camera requires handshaking. */
246 static int qc_waithand(struct qcam_device *q, int val)
248 int status;
249 int runs = 0;
251 if (val) {
252 while (!((status = read_lpstatus(q)) & 8)) {
253 /* 1000 is enough spins on the I/O for all normal
254 cases, at that point we start to poll slowly
255 until the camera wakes up. However, we are
256 busy blocked until the camera responds, so
257 setting it lower is much better for interactive
258 response. */
260 if (runs++ > maxpoll)
261 msleep_interruptible(5);
262 if (runs > (maxpoll + 1000)) /* 5 seconds */
263 return -1;
265 } else {
266 while (((status = read_lpstatus(q)) & 8)) {
267 /* 1000 is enough spins on the I/O for all normal
268 cases, at that point we start to poll slowly
269 until the camera wakes up. However, we are
270 busy blocked until the camera responds, so
271 setting it lower is much better for interactive
272 response. */
274 if (runs++ > maxpoll)
275 msleep_interruptible(5);
276 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
277 return -1;
281 return status;
284 /* Waithand2 is used when the qcam is in bidirectional mode, and the
285 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
286 * (bit 3 of status register). It also returns the last value read,
287 * since this data is useful. */
289 static unsigned int qc_waithand2(struct qcam_device *q, int val)
291 unsigned int status;
292 int runs = 0;
294 do {
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. */
303 if (runs++ > maxpoll)
304 msleep_interruptible(5);
305 if (runs++ > (maxpoll + 1000)) /* 5 seconds */
306 return 0;
307 } while ((status & 1) != val);
309 return status;
313 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
314 the status register at 5-10 Hz. This is only used in the autoprobe
315 code. Be aware that this isn't the way Connectix detects the
316 camera (they send a reset and try to handshake), but this should be
317 almost completely safe, while their method screws up my printer if
318 I plug it in before the camera. */
320 static int qc_detect(struct qcam_device *q)
322 int reg, lastreg;
323 int count = 0;
324 int i;
326 if (force_init)
327 return 1;
329 lastreg = reg = read_lpstatus(q) & 0xf0;
331 for (i = 0; i < 500; i++) {
332 reg = read_lpstatus(q) & 0xf0;
333 if (reg != lastreg)
334 count++;
335 lastreg = reg;
336 mdelay(2);
340 #if 0
341 /* Force camera detection during testing. Sometimes the camera
342 won't be flashing these bits. Possibly unloading the module
343 in the middle of a grab? Or some timeout condition?
344 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
345 printk(KERN_DEBUG "Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count);
346 return 1;
347 #endif
349 /* Be (even more) liberal in what you accept... */
351 if (count > 20 && count < 400) {
352 return 1; /* found */
353 } else {
354 printk(KERN_ERR "No Quickcam found on port %s\n",
355 q->pport->name);
356 printk(KERN_DEBUG "Quickcam detection counter: %u\n", count);
357 return 0; /* not found */
362 /* Reset the QuickCam. This uses the same sequence the Windows
363 * QuickPic program uses. Someone with a bi-directional port should
364 * check that bi-directional mode is detected right, and then
365 * implement bi-directional mode in qc_readbyte(). */
367 static void qc_reset(struct qcam_device *q)
369 switch (q->port_mode & QC_FORCE_MASK) {
370 case QC_FORCE_UNIDIR:
371 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
372 break;
374 case QC_FORCE_BIDIR:
375 q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
376 break;
378 case QC_ANY:
379 write_lpcontrol(q, 0x20);
380 write_lpdata(q, 0x75);
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 break;
389 write_lpcontrol(q, 0xb);
390 udelay(250);
391 write_lpcontrol(q, 0xe);
392 qc_setscanmode(q); /* in case port_mode changed */
396 /* Decide which scan mode to use. There's no real requirement that
397 * the scanmode match the resolution in q->height and q-> width -- the
398 * camera takes the picture at the resolution specified in the
399 * "scanmode" and then returns the image at the resolution specified
400 * with the resolution commands. If the scan is bigger than the
401 * requested resolution, the upper-left hand corner of the scan is
402 * returned. If the scan is smaller, then the rest of the image
403 * returned contains garbage. */
405 static int qc_setscanmode(struct qcam_device *q)
407 int old_mode = q->mode;
409 switch (q->transfer_scale) {
410 case 1:
411 q->mode = 0;
412 break;
413 case 2:
414 q->mode = 4;
415 break;
416 case 4:
417 q->mode = 8;
418 break;
421 switch (q->bpp) {
422 case 4:
423 break;
424 case 6:
425 q->mode += 2;
426 break;
429 switch (q->port_mode & QC_MODE_MASK) {
430 case QC_BIDIR:
431 q->mode += 1;
432 break;
433 case QC_NOTSET:
434 case QC_UNIDIR:
435 break;
438 if (q->mode != old_mode)
439 q->status |= QC_PARAM_CHANGE;
441 return 0;
445 /* Reset the QuickCam and program for brightness, contrast,
446 * white-balance, and resolution. */
448 static void qc_set(struct qcam_device *q)
450 int val;
451 int val2;
453 qc_reset(q);
455 /* Set the brightness. Yes, this is repetitive, but it works.
456 * Shorter versions seem to fail subtly. Feel free to try :-). */
457 /* I think the problem was in qc_command, not here -- bls */
459 qc_command(q, 0xb);
460 qc_command(q, q->brightness);
462 val = q->height / q->transfer_scale;
463 qc_command(q, 0x11);
464 qc_command(q, val);
465 if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) {
466 /* The normal "transfers per line" calculation doesn't seem to work
467 as expected here (and yet it works fine in qc_scan). No idea
468 why this case is the odd man out. Fortunately, Laird's original
469 working version gives me a good way to guess at working values.
470 -- bls */
471 val = q->width;
472 val2 = q->transfer_scale * 4;
473 } else {
474 val = q->width * q->bpp;
475 val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
476 q->transfer_scale;
478 val = DIV_ROUND_UP(val, val2);
479 qc_command(q, 0x13);
480 qc_command(q, val);
482 /* Setting top and left -- bls */
483 qc_command(q, 0xd);
484 qc_command(q, q->top);
485 qc_command(q, 0xf);
486 qc_command(q, q->left / 2);
488 qc_command(q, 0x19);
489 qc_command(q, q->contrast);
490 qc_command(q, 0x1f);
491 qc_command(q, q->whitebal);
493 /* Clear flag that we must update the grabbing parameters on the camera
494 before we grab the next frame */
495 q->status &= (~QC_PARAM_CHANGE);
498 /* Qc_readbytes reads some bytes from the QC and puts them in
499 the supplied buffer. It returns the number of bytes read,
500 or -1 on error. */
502 static inline int qc_readbytes(struct qcam_device *q, char buffer[])
504 int ret = 1;
505 unsigned int hi, lo;
506 unsigned int hi2, lo2;
507 static int state;
509 if (buffer == NULL) {
510 state = 0;
511 return 0;
514 switch (q->port_mode & QC_MODE_MASK) {
515 case QC_BIDIR: /* Bi-directional Port */
516 write_lpcontrol(q, 0x26);
517 lo = (qc_waithand2(q, 1) >> 1);
518 hi = (read_lpstatus(q) >> 3) & 0x1f;
519 write_lpcontrol(q, 0x2e);
520 lo2 = (qc_waithand2(q, 0) >> 1);
521 hi2 = (read_lpstatus(q) >> 3) & 0x1f;
522 switch (q->bpp) {
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;
540 break;
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;
548 switch (q->bpp) {
549 case 4:
550 buffer[0] = lo;
551 buffer[1] = hi;
552 ret = 2;
553 break;
554 case 6:
555 switch (state) {
556 case 0:
557 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
558 q->saved_bits = (hi & 3) << 4;
559 state = 1;
560 ret = 1;
561 break;
562 case 1:
563 buffer[0] = lo | q->saved_bits;
564 q->saved_bits = hi << 2;
565 state = 2;
566 ret = 1;
567 break;
568 case 2:
569 buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits;
570 buffer[1] = ((lo & 3) << 4) | hi;
571 state = 0;
572 ret = 2;
573 break;
575 break;
577 break;
579 return ret;
582 /* requests a scan from the camera. It sends the correct instructions
583 * to the camera and then reads back the correct number of bytes. In
584 * previous versions of this routine the return structure contained
585 * the raw output from the camera, and there was a 'qc_convertscan'
586 * function that converted that to a useful format. In version 0.3 I
587 * rolled qc_convertscan into qc_scan and now I only return the
588 * converted scan. The format is just an one-dimensional array of
589 * characters, one for each pixel, with 0=black up to n=white, where
590 * n=2^(bit depth)-1. Ask me for more details if you don't understand
591 * this. */
593 static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long len)
595 int i, j, k, yield;
596 int bytes;
597 int linestotrans, transperline;
598 int divisor;
599 int pixels_per_line;
600 int pixels_read = 0;
601 int got = 0;
602 char buffer[6];
603 int shift = 8 - q->bpp;
604 char invert;
606 if (q->mode == -1)
607 return -ENXIO;
609 qc_command(q, 0x7);
610 qc_command(q, q->mode);
612 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
613 write_lpcontrol(q, 0x2e); /* turn port around */
614 write_lpcontrol(q, 0x26);
615 qc_waithand(q, 1);
616 write_lpcontrol(q, 0x2e);
617 qc_waithand(q, 0);
620 /* strange -- should be 15:63 below, but 4bpp is odd */
621 invert = (q->bpp == 4) ? 16 : 63;
623 linestotrans = q->height / q->transfer_scale;
624 pixels_per_line = q->width / q->transfer_scale;
625 transperline = q->width * q->bpp;
626 divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) *
627 q->transfer_scale;
628 transperline = DIV_ROUND_UP(transperline, divisor);
630 for (i = 0, yield = yieldlines; i < linestotrans; i++) {
631 for (pixels_read = j = 0; j < transperline; j++) {
632 bytes = qc_readbytes(q, buffer);
633 for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) {
634 int o;
635 if (buffer[k] == 0 && invert == 16) {
636 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
637 must be 0-15 -- bls */
638 buffer[k] = 16;
640 o = i * pixels_per_line + pixels_read + k;
641 if (o < len) {
642 got++;
643 put_user((invert - buffer[k]) << shift, buf + o);
646 pixels_read += bytes;
648 qc_readbytes(q, NULL); /* reset state machine */
650 /* Grabbing an entire frame from the quickcam is a lengthy
651 process. We don't (usually) want to busy-block the
652 processor for the entire frame. yieldlines is a module
653 parameter. If we yield every line, the minimum frame
654 time will be 240 / 200 = 1.2 seconds. The compile-time
655 default is to yield every 4 lines. */
656 if (i >= yield) {
657 msleep_interruptible(5);
658 yield = i + yieldlines;
662 if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) {
663 write_lpcontrol(q, 2);
664 write_lpcontrol(q, 6);
665 udelay(3);
666 write_lpcontrol(q, 0xe);
668 if (got < len)
669 return got;
670 return len;
674 * Video4linux interfacing
677 static long qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg)
679 struct video_device *dev = video_devdata(file);
680 struct qcam_device *qcam = (struct qcam_device *)dev;
682 switch (cmd) {
683 case VIDIOCGCAP:
685 struct video_capability *b = arg;
686 strcpy(b->name, "Quickcam");
687 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
688 b->channels = 1;
689 b->audios = 0;
690 b->maxwidth = 320;
691 b->maxheight = 240;
692 b->minwidth = 80;
693 b->minheight = 60;
694 return 0;
696 case VIDIOCGCHAN:
698 struct video_channel *v = arg;
699 if (v->channel != 0)
700 return -EINVAL;
701 v->flags = 0;
702 v->tuners = 0;
703 /* Good question.. its composite or SVHS so.. */
704 v->type = VIDEO_TYPE_CAMERA;
705 strcpy(v->name, "Camera");
706 return 0;
708 case VIDIOCSCHAN:
710 struct video_channel *v = arg;
711 if (v->channel != 0)
712 return -EINVAL;
713 return 0;
715 case VIDIOCGTUNER:
717 struct video_tuner *v = arg;
718 if (v->tuner)
719 return -EINVAL;
720 strcpy(v->name, "Format");
721 v->rangelow = 0;
722 v->rangehigh = 0;
723 v->flags = 0;
724 v->mode = VIDEO_MODE_AUTO;
725 return 0;
727 case VIDIOCSTUNER:
729 struct video_tuner *v = arg;
730 if (v->tuner)
731 return -EINVAL;
732 if (v->mode != VIDEO_MODE_AUTO)
733 return -EINVAL;
734 return 0;
736 case VIDIOCGPICT:
738 struct video_picture *p = arg;
739 p->colour = 0x8000;
740 p->hue = 0x8000;
741 p->brightness = qcam->brightness << 8;
742 p->contrast = qcam->contrast << 8;
743 p->whiteness = qcam->whitebal << 8;
744 p->depth = qcam->bpp;
745 p->palette = VIDEO_PALETTE_GREY;
746 return 0;
748 case VIDIOCSPICT:
750 struct video_picture *p = arg;
751 if (p->palette != VIDEO_PALETTE_GREY)
752 return -EINVAL;
753 if (p->depth != 4 && p->depth != 6)
754 return -EINVAL;
757 * Now load the camera.
760 qcam->brightness = p->brightness >> 8;
761 qcam->contrast = p->contrast >> 8;
762 qcam->whitebal = p->whiteness >> 8;
763 qcam->bpp = p->depth;
765 mutex_lock(&qcam->lock);
766 qc_setscanmode(qcam);
767 mutex_unlock(&qcam->lock);
768 qcam->status |= QC_PARAM_CHANGE;
770 return 0;
772 case VIDIOCSWIN:
774 struct video_window *vw = arg;
775 if (vw->flags)
776 return -EINVAL;
777 if (vw->clipcount)
778 return -EINVAL;
779 if (vw->height < 60 || vw->height > 240)
780 return -EINVAL;
781 if (vw->width < 80 || vw->width > 320)
782 return -EINVAL;
784 qcam->width = 320;
785 qcam->height = 240;
786 qcam->transfer_scale = 4;
788 if (vw->width >= 160 && vw->height >= 120)
789 qcam->transfer_scale = 2;
790 if (vw->width >= 320 && vw->height >= 240) {
791 qcam->width = 320;
792 qcam->height = 240;
793 qcam->transfer_scale = 1;
795 mutex_lock(&qcam->lock);
796 qc_setscanmode(qcam);
797 mutex_unlock(&qcam->lock);
799 /* We must update the camera before we grab. We could
800 just have changed the grab size */
801 qcam->status |= QC_PARAM_CHANGE;
803 /* Ok we figured out what to use from our wide choice */
804 return 0;
806 case VIDIOCGWIN:
808 struct video_window *vw = arg;
810 memset(vw, 0, sizeof(*vw));
811 vw->width = qcam->width / qcam->transfer_scale;
812 vw->height = qcam->height / qcam->transfer_scale;
813 return 0;
815 case VIDIOCKEY:
816 return 0;
817 case VIDIOCCAPTURE:
818 case VIDIOCGFBUF:
819 case VIDIOCSFBUF:
820 case VIDIOCGFREQ:
821 case VIDIOCSFREQ:
822 case VIDIOCGAUDIO:
823 case VIDIOCSAUDIO:
824 return -EINVAL;
825 default:
826 return -ENOIOCTLCMD;
828 return 0;
831 static long qcam_ioctl(struct file *file,
832 unsigned int cmd, unsigned long arg)
834 return video_usercopy(file, cmd, arg, qcam_do_ioctl);
837 static ssize_t qcam_read(struct file *file, char __user *buf,
838 size_t count, loff_t *ppos)
840 struct video_device *v = video_devdata(file);
841 struct qcam_device *qcam = (struct qcam_device *)v;
842 int len;
843 parport_claim_or_block(qcam->pdev);
845 mutex_lock(&qcam->lock);
847 qc_reset(qcam);
849 /* Update the camera parameters if we need to */
850 if (qcam->status & QC_PARAM_CHANGE)
851 qc_set(qcam);
853 len = qc_capture(qcam, buf, count);
855 mutex_unlock(&qcam->lock);
857 parport_release(qcam->pdev);
858 return len;
861 static int qcam_exclusive_open(struct file *file)
863 struct video_device *dev = video_devdata(file);
864 struct qcam_device *qcam = (struct qcam_device *)dev;
866 return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0;
869 static int qcam_exclusive_release(struct file *file)
871 struct video_device *dev = video_devdata(file);
872 struct qcam_device *qcam = (struct qcam_device *)dev;
874 clear_bit(0, &qcam->in_use);
875 return 0;
878 static const struct v4l2_file_operations qcam_fops = {
879 .owner = THIS_MODULE,
880 .open = qcam_exclusive_open,
881 .release = qcam_exclusive_release,
882 .ioctl = qcam_ioctl,
883 .read = qcam_read,
885 static struct video_device qcam_template = {
886 .name = "Connectix Quickcam",
887 .fops = &qcam_fops,
888 .release = video_device_release_empty,
891 #define MAX_CAMS 4
892 static struct qcam_device *qcams[MAX_CAMS];
893 static unsigned int num_cams;
895 static int init_bwqcam(struct parport *port)
897 struct qcam_device *qcam;
899 if (num_cams == MAX_CAMS) {
900 printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
901 return -ENOSPC;
904 qcam = qcam_init(port);
905 if (qcam == NULL)
906 return -ENODEV;
908 parport_claim_or_block(qcam->pdev);
910 qc_reset(qcam);
912 if (qc_detect(qcam) == 0) {
913 parport_release(qcam->pdev);
914 parport_unregister_device(qcam->pdev);
915 kfree(qcam);
916 return -ENODEV;
918 qc_calibrate(qcam);
920 parport_release(qcam->pdev);
922 printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
924 if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
925 parport_unregister_device(qcam->pdev);
926 kfree(qcam);
927 return -ENODEV;
930 qcams[num_cams++] = qcam;
932 return 0;
935 static void close_bwqcam(struct qcam_device *qcam)
937 video_unregister_device(&qcam->vdev);
938 parport_unregister_device(qcam->pdev);
939 kfree(qcam);
942 /* The parport parameter controls which parports will be scanned.
943 * Scanning all parports causes some printers to print a garbage page.
944 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
945 #ifdef MODULE
946 static char *parport[MAX_CAMS] = { NULL, };
947 module_param_array(parport, charp, NULL, 0);
948 #endif
950 static int accept_bwqcam(struct parport *port)
952 #ifdef MODULE
953 int n;
955 if (parport[0] && strncmp(parport[0], "auto", 4) != 0) {
956 /* user gave parport parameters */
957 for (n = 0; n < MAX_CAMS && parport[n]; n++) {
958 char *ep;
959 unsigned long r;
960 r = simple_strtoul(parport[n], &ep, 0);
961 if (ep == parport[n]) {
962 printk(KERN_ERR
963 "bw-qcam: bad port specifier \"%s\"\n",
964 parport[n]);
965 continue;
967 if (r == port->number)
968 return 1;
970 return 0;
972 #endif
973 return 1;
976 static void bwqcam_attach(struct parport *port)
978 if (accept_bwqcam(port))
979 init_bwqcam(port);
982 static void bwqcam_detach(struct parport *port)
984 int i;
985 for (i = 0; i < num_cams; i++) {
986 struct qcam_device *qcam = qcams[i];
987 if (qcam && qcam->pdev->port == port) {
988 qcams[i] = NULL;
989 close_bwqcam(qcam);
994 static struct parport_driver bwqcam_driver = {
995 .name = "bw-qcam",
996 .attach = bwqcam_attach,
997 .detach = bwqcam_detach,
1000 static void __exit exit_bw_qcams(void)
1002 parport_unregister_driver(&bwqcam_driver);
1005 static int __init init_bw_qcams(void)
1007 #ifdef MODULE
1008 /* Do some sanity checks on the module parameters. */
1009 if (maxpoll > 5000) {
1010 printk(KERN_INFO "Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1011 maxpoll = 5000;
1014 if (yieldlines < 1) {
1015 printk(KERN_INFO "Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1016 yieldlines = 1;
1018 #endif
1019 return parport_register_driver(&bwqcam_driver);
1022 module_init(init_bw_qcams);
1023 module_exit(exit_bw_qcams);
1025 MODULE_LICENSE("GPL");