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