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.
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>
69 #include <linux/kernel.h>
70 #include <linux/slab.h>
72 #include <linux/parport.h>
73 #include <linux/sched.h>
74 #include <linux/videodev2.h>
75 #include <linux/mutex.h>
76 #include <asm/uaccess.h>
77 #include <media/v4l2-common.h>
78 #include <media/v4l2-ioctl.h>
79 #include <media/v4l2-device.h>
80 #include <media/v4l2-fh.h>
81 #include <media/v4l2-ctrls.h>
82 #include <media/v4l2-event.h>
83 #include <media/videobuf2-vmalloc.h>
85 /* One from column A... */
91 /* ... and one from column B */
93 #define QC_FORCE_UNIDIR 0x10
94 #define QC_FORCE_BIDIR 0x20
95 #define QC_FORCE_SERIAL 0x30
96 /* in the port_mode member */
98 #define QC_MODE_MASK 0x07
99 #define QC_FORCE_MASK 0x70
101 #define MAX_HEIGHT 243
102 #define MAX_WIDTH 336
104 /* Bit fields for status flags */
105 #define QC_PARAM_CHANGE 0x01 /* Camera status change has occurred */
108 struct v4l2_device v4l2_dev
;
109 struct video_device vdev
;
110 struct v4l2_ctrl_handler hdl
;
111 struct vb2_queue vb_vidq
;
112 struct pardevice
*pdev
;
113 struct parport
*pport
;
115 struct mutex queue_lock
;
119 int contrast
, brightness
, whitebal
;
124 unsigned int saved_bits
;
125 unsigned long in_use
;
128 static unsigned int maxpoll
= 250; /* Maximum busy-loop count for qcam I/O */
129 static unsigned int yieldlines
= 4; /* Yield after this many during capture */
130 static int video_nr
= -1;
131 static unsigned int force_init
; /* Whether to probe aggressively */
133 module_param(maxpoll
, int, 0);
134 module_param(yieldlines
, int, 0);
135 module_param(video_nr
, int, 0);
137 /* Set force_init=1 to avoid detection by polling status register and
138 * immediately attempt to initialize qcam */
139 module_param(force_init
, int, 0);
142 static struct qcam
*qcams
[MAX_CAMS
];
143 static unsigned int num_cams
;
145 static inline int read_lpstatus(struct qcam
*q
)
147 return parport_read_status(q
->pport
);
150 static inline int read_lpdata(struct qcam
*q
)
152 return parport_read_data(q
->pport
);
155 static inline void write_lpdata(struct qcam
*q
, int d
)
157 parport_write_data(q
->pport
, d
);
160 static void write_lpcontrol(struct qcam
*q
, int d
)
163 /* Set bidirectional mode to reverse (data in) */
164 parport_data_reverse(q
->pport
);
166 /* Set bidirectional mode to forward (data out) */
167 parport_data_forward(q
->pport
);
170 /* Now issue the regular port command, but strip out the
173 parport_write_control(q
->pport
, d
);
177 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
178 * Almost all communication with the camera requires handshaking. */
180 static int qc_waithand(struct qcam
*q
, int val
)
186 while (!((status
= read_lpstatus(q
)) & 8)) {
187 /* 1000 is enough spins on the I/O for all normal
188 cases, at that point we start to poll slowly
189 until the camera wakes up. However, we are
190 busy blocked until the camera responds, so
191 setting it lower is much better for interactive
194 if (runs
++ > maxpoll
)
195 msleep_interruptible(5);
196 if (runs
> (maxpoll
+ 1000)) /* 5 seconds */
200 while (((status
= read_lpstatus(q
)) & 8)) {
201 /* 1000 is enough spins on the I/O for all normal
202 cases, at that point we start to poll slowly
203 until the camera wakes up. However, we are
204 busy blocked until the camera responds, so
205 setting it lower is much better for interactive
208 if (runs
++ > maxpoll
)
209 msleep_interruptible(5);
210 if (runs
++ > (maxpoll
+ 1000)) /* 5 seconds */
218 /* Waithand2 is used when the qcam is in bidirectional mode, and the
219 * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1
220 * (bit 3 of status register). It also returns the last value read,
221 * since this data is useful. */
223 static unsigned int qc_waithand2(struct qcam
*q
, int val
)
229 status
= read_lpdata(q
);
230 /* 1000 is enough spins on the I/O for all normal
231 cases, at that point we start to poll slowly
232 until the camera wakes up. However, we are
233 busy blocked until the camera responds, so
234 setting it lower is much better for interactive
237 if (runs
++ > maxpoll
)
238 msleep_interruptible(5);
239 if (runs
++ > (maxpoll
+ 1000)) /* 5 seconds */
241 } while ((status
& 1) != val
);
246 /* qc_command is probably a bit of a misnomer -- it's used to send
247 * bytes *to* the camera. Generally, these bytes are either commands
248 * or arguments to commands, so the name fits, but it still bugs me a
249 * bit. See the documentation for a list of commands. */
251 static int qc_command(struct qcam
*q
, int command
)
256 write_lpdata(q
, command
);
257 write_lpcontrol(q
, 6);
259 n1
= qc_waithand(q
, 1);
261 write_lpcontrol(q
, 0xe);
262 n2
= qc_waithand(q
, 0);
264 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
268 static int qc_readparam(struct qcam
*q
)
273 write_lpcontrol(q
, 6);
274 n1
= qc_waithand(q
, 1);
276 write_lpcontrol(q
, 0xe);
277 n2
= qc_waithand(q
, 0);
279 cmd
= (n1
& 0xf0) | ((n2
& 0xf0) >> 4);
284 /* Try to detect a QuickCam. It appears to flash the upper 4 bits of
285 the status register at 5-10 Hz. This is only used in the autoprobe
286 code. Be aware that this isn't the way Connectix detects the
287 camera (they send a reset and try to handshake), but this should be
288 almost completely safe, while their method screws up my printer if
289 I plug it in before the camera. */
291 static int qc_detect(struct qcam
*q
)
300 lastreg
= reg
= read_lpstatus(q
) & 0xf0;
302 for (i
= 0; i
< 500; i
++) {
303 reg
= read_lpstatus(q
) & 0xf0;
312 /* Force camera detection during testing. Sometimes the camera
313 won't be flashing these bits. Possibly unloading the module
314 in the middle of a grab? Or some timeout condition?
315 I've seen this parameter as low as 19 on my 450Mhz box - mpc */
316 printk(KERN_DEBUG
"Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count
);
320 /* Be (even more) liberal in what you accept... */
322 if (count
> 20 && count
< 400) {
323 return 1; /* found */
325 printk(KERN_ERR
"No Quickcam found on port %s\n",
327 printk(KERN_DEBUG
"Quickcam detection counter: %u\n", count
);
328 return 0; /* not found */
332 /* Decide which scan mode to use. There's no real requirement that
333 * the scanmode match the resolution in q->height and q-> width -- the
334 * camera takes the picture at the resolution specified in the
335 * "scanmode" and then returns the image at the resolution specified
336 * with the resolution commands. If the scan is bigger than the
337 * requested resolution, the upper-left hand corner of the scan is
338 * returned. If the scan is smaller, then the rest of the image
339 * returned contains garbage. */
341 static int qc_setscanmode(struct qcam
*q
)
343 int old_mode
= q
->mode
;
345 switch (q
->transfer_scale
) {
365 switch (q
->port_mode
& QC_MODE_MASK
) {
374 if (q
->mode
!= old_mode
)
375 q
->status
|= QC_PARAM_CHANGE
;
381 /* Reset the QuickCam. This uses the same sequence the Windows
382 * QuickPic program uses. Someone with a bi-directional port should
383 * check that bi-directional mode is detected right, and then
384 * implement bi-directional mode in qc_readbyte(). */
386 static void qc_reset(struct qcam
*q
)
388 switch (q
->port_mode
& QC_FORCE_MASK
) {
389 case QC_FORCE_UNIDIR
:
390 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
394 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
398 write_lpcontrol(q
, 0x20);
399 write_lpdata(q
, 0x75);
401 if (read_lpdata(q
) != 0x75)
402 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_BIDIR
;
404 q
->port_mode
= (q
->port_mode
& ~QC_MODE_MASK
) | QC_UNIDIR
;
408 write_lpcontrol(q
, 0xb);
410 write_lpcontrol(q
, 0xe);
411 qc_setscanmode(q
); /* in case port_mode changed */
416 /* Reset the QuickCam and program for brightness, contrast,
417 * white-balance, and resolution. */
419 static void qc_set(struct qcam
*q
)
424 /* Set the brightness. Yes, this is repetitive, but it works.
425 * Shorter versions seem to fail subtly. Feel free to try :-). */
426 /* I think the problem was in qc_command, not here -- bls */
429 qc_command(q
, q
->brightness
);
431 val
= q
->height
/ q
->transfer_scale
;
434 if ((q
->port_mode
& QC_MODE_MASK
) == QC_UNIDIR
&& q
->bpp
== 6) {
435 /* The normal "transfers per line" calculation doesn't seem to work
436 as expected here (and yet it works fine in qc_scan). No idea
437 why this case is the odd man out. Fortunately, Laird's original
438 working version gives me a good way to guess at working values.
441 val2
= q
->transfer_scale
* 4;
443 val
= q
->width
* q
->bpp
;
444 val2
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
447 val
= DIV_ROUND_UP(val
, val2
);
451 /* Setting top and left -- bls */
453 qc_command(q
, q
->top
);
455 qc_command(q
, q
->left
/ 2);
458 qc_command(q
, q
->contrast
);
460 qc_command(q
, q
->whitebal
);
462 /* Clear flag that we must update the grabbing parameters on the camera
463 before we grab the next frame */
464 q
->status
&= (~QC_PARAM_CHANGE
);
467 /* Qc_readbytes reads some bytes from the QC and puts them in
468 the supplied buffer. It returns the number of bytes read,
471 static inline int qc_readbytes(struct qcam
*q
, char buffer
[])
475 unsigned int hi2
, lo2
;
478 if (buffer
== NULL
) {
483 switch (q
->port_mode
& QC_MODE_MASK
) {
484 case QC_BIDIR
: /* Bi-directional Port */
485 write_lpcontrol(q
, 0x26);
486 lo
= (qc_waithand2(q
, 1) >> 1);
487 hi
= (read_lpstatus(q
) >> 3) & 0x1f;
488 write_lpcontrol(q
, 0x2e);
489 lo2
= (qc_waithand2(q
, 0) >> 1);
490 hi2
= (read_lpstatus(q
) >> 3) & 0x1f;
493 buffer
[0] = lo
& 0xf;
494 buffer
[1] = ((lo
& 0x70) >> 4) | ((hi
& 1) << 3);
495 buffer
[2] = (hi
& 0x1e) >> 1;
496 buffer
[3] = lo2
& 0xf;
497 buffer
[4] = ((lo2
& 0x70) >> 4) | ((hi2
& 1) << 3);
498 buffer
[5] = (hi2
& 0x1e) >> 1;
502 buffer
[0] = lo
& 0x3f;
503 buffer
[1] = ((lo
& 0x40) >> 6) | (hi
<< 1);
504 buffer
[2] = lo2
& 0x3f;
505 buffer
[3] = ((lo2
& 0x40) >> 6) | (hi2
<< 1);
511 case QC_UNIDIR
: /* Unidirectional Port */
512 write_lpcontrol(q
, 6);
513 lo
= (qc_waithand(q
, 1) & 0xf0) >> 4;
514 write_lpcontrol(q
, 0xe);
515 hi
= (qc_waithand(q
, 0) & 0xf0) >> 4;
526 buffer
[0] = (lo
<< 2) | ((hi
& 0xc) >> 2);
527 q
->saved_bits
= (hi
& 3) << 4;
532 buffer
[0] = lo
| q
->saved_bits
;
533 q
->saved_bits
= hi
<< 2;
538 buffer
[0] = ((lo
& 0xc) >> 2) | q
->saved_bits
;
539 buffer
[1] = ((lo
& 3) << 4) | hi
;
551 /* requests a scan from the camera. It sends the correct instructions
552 * to the camera and then reads back the correct number of bytes. In
553 * previous versions of this routine the return structure contained
554 * the raw output from the camera, and there was a 'qc_convertscan'
555 * function that converted that to a useful format. In version 0.3 I
556 * rolled qc_convertscan into qc_scan and now I only return the
557 * converted scan. The format is just an one-dimensional array of
558 * characters, one for each pixel, with 0=black up to n=white, where
559 * n=2^(bit depth)-1. Ask me for more details if you don't understand
562 static long qc_capture(struct qcam
*q
, u8
*buf
, unsigned long len
)
566 int linestotrans
, transperline
;
572 int shift
= 8 - q
->bpp
;
579 qc_command(q
, q
->mode
);
581 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) {
582 write_lpcontrol(q
, 0x2e); /* turn port around */
583 write_lpcontrol(q
, 0x26);
585 write_lpcontrol(q
, 0x2e);
589 /* strange -- should be 15:63 below, but 4bpp is odd */
590 invert
= (q
->bpp
== 4) ? 16 : 63;
592 linestotrans
= q
->height
/ q
->transfer_scale
;
593 pixels_per_line
= q
->width
/ q
->transfer_scale
;
594 transperline
= q
->width
* q
->bpp
;
595 divisor
= (((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) ? 24 : 8) *
597 transperline
= DIV_ROUND_UP(transperline
, divisor
);
599 for (i
= 0, yield
= yieldlines
; i
< linestotrans
; i
++) {
600 for (pixels_read
= j
= 0; j
< transperline
; j
++) {
601 bytes
= qc_readbytes(q
, buffer
);
602 for (k
= 0; k
< bytes
&& (pixels_read
+ k
) < pixels_per_line
; k
++) {
604 if (buffer
[k
] == 0 && invert
== 16) {
605 /* 4bpp is odd (again) -- inverter is 16, not 15, but output
606 must be 0-15 -- bls */
609 o
= i
* pixels_per_line
+ pixels_read
+ k
;
611 u8 ch
= invert
- buffer
[k
];
613 buf
[o
] = ch
<< shift
;
616 pixels_read
+= bytes
;
618 qc_readbytes(q
, NULL
); /* reset state machine */
620 /* Grabbing an entire frame from the quickcam is a lengthy
621 process. We don't (usually) want to busy-block the
622 processor for the entire frame. yieldlines is a module
623 parameter. If we yield every line, the minimum frame
624 time will be 240 / 200 = 1.2 seconds. The compile-time
625 default is to yield every 4 lines. */
627 msleep_interruptible(5);
628 yield
= i
+ yieldlines
;
632 if ((q
->port_mode
& QC_MODE_MASK
) == QC_BIDIR
) {
633 write_lpcontrol(q
, 2);
634 write_lpcontrol(q
, 6);
636 write_lpcontrol(q
, 0xe);
643 /* ------------------------------------------------------------------
645 ------------------------------------------------------------------*/
646 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
647 unsigned int *nbuffers
, unsigned int *nplanes
,
648 unsigned int sizes
[], void *alloc_ctxs
[])
650 struct qcam
*dev
= vb2_get_drv_priv(vq
);
655 mutex_lock(&dev
->lock
);
657 sizes
[0] = fmt
->fmt
.pix
.width
* fmt
->fmt
.pix
.height
;
659 sizes
[0] = (dev
->width
/ dev
->transfer_scale
) *
660 (dev
->height
/ dev
->transfer_scale
);
661 mutex_unlock(&dev
->lock
);
665 static void buffer_queue(struct vb2_buffer
*vb
)
667 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
670 static void buffer_finish(struct vb2_buffer
*vb
)
672 struct qcam
*qcam
= vb2_get_drv_priv(vb
->vb2_queue
);
673 void *vbuf
= vb2_plane_vaddr(vb
, 0);
674 int size
= vb
->vb2_queue
->plane_sizes
[0];
677 if (!vb2_is_streaming(vb
->vb2_queue
))
680 mutex_lock(&qcam
->lock
);
681 parport_claim_or_block(qcam
->pdev
);
685 /* Update the camera parameters if we need to */
686 if (qcam
->status
& QC_PARAM_CHANGE
)
689 len
= qc_capture(qcam
, vbuf
, size
);
691 parport_release(qcam
->pdev
);
692 mutex_unlock(&qcam
->lock
);
693 v4l2_get_timestamp(&vb
->v4l2_buf
.timestamp
);
695 vb
->state
= VB2_BUF_STATE_ERROR
;
696 vb2_set_plane_payload(vb
, 0, len
);
699 static struct vb2_ops qcam_video_qops
= {
700 .queue_setup
= queue_setup
,
701 .buf_queue
= buffer_queue
,
702 .buf_finish
= buffer_finish
,
703 .wait_prepare
= vb2_ops_wait_prepare
,
704 .wait_finish
= vb2_ops_wait_finish
,
708 * Video4linux interfacing
711 static int qcam_querycap(struct file
*file
, void *priv
,
712 struct v4l2_capability
*vcap
)
714 struct qcam
*qcam
= video_drvdata(file
);
716 strlcpy(vcap
->driver
, qcam
->v4l2_dev
.name
, sizeof(vcap
->driver
));
717 strlcpy(vcap
->card
, "Connectix B&W Quickcam", sizeof(vcap
->card
));
718 strlcpy(vcap
->bus_info
, qcam
->pport
->name
, sizeof(vcap
->bus_info
));
719 vcap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
721 vcap
->capabilities
= vcap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
725 static int qcam_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*vin
)
729 strlcpy(vin
->name
, "Camera", sizeof(vin
->name
));
730 vin
->type
= V4L2_INPUT_TYPE_CAMERA
;
738 static int qcam_g_input(struct file
*file
, void *fh
, unsigned int *inp
)
744 static int qcam_s_input(struct file
*file
, void *fh
, unsigned int inp
)
746 return (inp
> 0) ? -EINVAL
: 0;
749 static int qcam_g_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
751 struct qcam
*qcam
= video_drvdata(file
);
752 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
754 pix
->width
= qcam
->width
/ qcam
->transfer_scale
;
755 pix
->height
= qcam
->height
/ qcam
->transfer_scale
;
756 pix
->pixelformat
= (qcam
->bpp
== 4) ? V4L2_PIX_FMT_Y4
: V4L2_PIX_FMT_Y6
;
757 pix
->field
= V4L2_FIELD_NONE
;
758 pix
->bytesperline
= pix
->width
;
759 pix
->sizeimage
= pix
->width
* pix
->height
;
761 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
766 static int qcam_try_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
768 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
770 if (pix
->height
<= 60 || pix
->width
<= 80) {
773 } else if (pix
->height
<= 120 || pix
->width
<= 160) {
780 if (pix
->pixelformat
!= V4L2_PIX_FMT_Y4
&&
781 pix
->pixelformat
!= V4L2_PIX_FMT_Y6
)
782 pix
->pixelformat
= V4L2_PIX_FMT_Y4
;
783 pix
->field
= V4L2_FIELD_NONE
;
784 pix
->bytesperline
= pix
->width
;
785 pix
->sizeimage
= pix
->width
* pix
->height
;
787 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
792 static int qcam_s_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
794 struct qcam
*qcam
= video_drvdata(file
);
795 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
796 int ret
= qcam_try_fmt_vid_cap(file
, fh
, fmt
);
800 if (vb2_is_busy(&qcam
->vb_vidq
))
804 if (pix
->height
== 60)
805 qcam
->transfer_scale
= 4;
806 else if (pix
->height
== 120)
807 qcam
->transfer_scale
= 2;
809 qcam
->transfer_scale
= 1;
810 if (pix
->pixelformat
== V4L2_PIX_FMT_Y6
)
815 qc_setscanmode(qcam
);
816 /* We must update the camera before we grab. We could
817 just have changed the grab size */
818 qcam
->status
|= QC_PARAM_CHANGE
;
822 static int qcam_enum_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_fmtdesc
*fmt
)
824 static struct v4l2_fmtdesc formats
[] = {
826 "4-Bit Monochrome", V4L2_PIX_FMT_Y4
,
830 "6-Bit Monochrome", V4L2_PIX_FMT_Y6
,
834 enum v4l2_buf_type type
= fmt
->type
;
839 *fmt
= formats
[fmt
->index
];
844 static int qcam_enum_framesizes(struct file
*file
, void *fh
,
845 struct v4l2_frmsizeenum
*fsize
)
847 static const struct v4l2_frmsize_discrete sizes
[] = {
853 if (fsize
->index
> 2)
855 if (fsize
->pixel_format
!= V4L2_PIX_FMT_Y4
&&
856 fsize
->pixel_format
!= V4L2_PIX_FMT_Y6
)
858 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
859 fsize
->discrete
= sizes
[fsize
->index
];
863 static int qcam_s_ctrl(struct v4l2_ctrl
*ctrl
)
866 container_of(ctrl
->handler
, struct qcam
, hdl
);
870 case V4L2_CID_BRIGHTNESS
:
871 qcam
->brightness
= ctrl
->val
;
873 case V4L2_CID_CONTRAST
:
874 qcam
->contrast
= ctrl
->val
;
877 qcam
->whitebal
= ctrl
->val
;
884 qcam
->status
|= QC_PARAM_CHANGE
;
888 static const struct v4l2_file_operations qcam_fops
= {
889 .owner
= THIS_MODULE
,
890 .open
= v4l2_fh_open
,
891 .release
= vb2_fop_release
,
892 .poll
= vb2_fop_poll
,
893 .unlocked_ioctl
= video_ioctl2
,
894 .read
= vb2_fop_read
,
895 .mmap
= vb2_fop_mmap
,
898 static const struct v4l2_ioctl_ops qcam_ioctl_ops
= {
899 .vidioc_querycap
= qcam_querycap
,
900 .vidioc_g_input
= qcam_g_input
,
901 .vidioc_s_input
= qcam_s_input
,
902 .vidioc_enum_input
= qcam_enum_input
,
903 .vidioc_enum_fmt_vid_cap
= qcam_enum_fmt_vid_cap
,
904 .vidioc_enum_framesizes
= qcam_enum_framesizes
,
905 .vidioc_g_fmt_vid_cap
= qcam_g_fmt_vid_cap
,
906 .vidioc_s_fmt_vid_cap
= qcam_s_fmt_vid_cap
,
907 .vidioc_try_fmt_vid_cap
= qcam_try_fmt_vid_cap
,
908 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
909 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
910 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
911 .vidioc_querybuf
= vb2_ioctl_querybuf
,
912 .vidioc_qbuf
= vb2_ioctl_qbuf
,
913 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
914 .vidioc_streamon
= vb2_ioctl_streamon
,
915 .vidioc_streamoff
= vb2_ioctl_streamoff
,
916 .vidioc_log_status
= v4l2_ctrl_log_status
,
917 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
918 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
921 static const struct v4l2_ctrl_ops qcam_ctrl_ops
= {
922 .s_ctrl
= qcam_s_ctrl
,
925 /* Initialize the QuickCam driver control structure. This is where
926 * defaults are set for people who don't have a config file.*/
928 static struct qcam
*qcam_init(struct parport
*port
)
931 struct v4l2_device
*v4l2_dev
;
935 qcam
= kzalloc(sizeof(struct qcam
), GFP_KERNEL
);
939 v4l2_dev
= &qcam
->v4l2_dev
;
940 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
), "bw-qcam%d", num_cams
);
942 if (v4l2_device_register(port
->dev
, v4l2_dev
) < 0) {
943 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
948 v4l2_ctrl_handler_init(&qcam
->hdl
, 3);
949 v4l2_ctrl_new_std(&qcam
->hdl
, &qcam_ctrl_ops
,
950 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 180);
951 v4l2_ctrl_new_std(&qcam
->hdl
, &qcam_ctrl_ops
,
952 V4L2_CID_CONTRAST
, 0, 255, 1, 192);
953 v4l2_ctrl_new_std(&qcam
->hdl
, &qcam_ctrl_ops
,
954 V4L2_CID_GAMMA
, 0, 255, 1, 105);
955 if (qcam
->hdl
.error
) {
956 v4l2_err(v4l2_dev
, "couldn't register controls\n");
960 mutex_init(&qcam
->lock
);
961 mutex_init(&qcam
->queue_lock
);
963 /* initialize queue */
965 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
966 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
968 q
->ops
= &qcam_video_qops
;
969 q
->mem_ops
= &vb2_vmalloc_memops
;
970 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
971 err
= vb2_queue_init(q
);
973 v4l2_err(v4l2_dev
, "couldn't init vb2_queue for %s.\n", port
->name
);
976 qcam
->vdev
.queue
= q
;
977 qcam
->vdev
.queue
->lock
= &qcam
->queue_lock
;
980 qcam
->pdev
= parport_register_device(port
, v4l2_dev
->name
, NULL
, NULL
,
982 if (qcam
->pdev
== NULL
) {
983 v4l2_err(v4l2_dev
, "couldn't register for %s.\n", port
->name
);
987 strlcpy(qcam
->vdev
.name
, "Connectix QuickCam", sizeof(qcam
->vdev
.name
));
988 qcam
->vdev
.v4l2_dev
= v4l2_dev
;
989 qcam
->vdev
.ctrl_handler
= &qcam
->hdl
;
990 qcam
->vdev
.fops
= &qcam_fops
;
991 qcam
->vdev
.lock
= &qcam
->lock
;
992 qcam
->vdev
.ioctl_ops
= &qcam_ioctl_ops
;
993 set_bit(V4L2_FL_USE_FH_PRIO
, &qcam
->vdev
.flags
);
994 qcam
->vdev
.release
= video_device_release_empty
;
995 video_set_drvdata(&qcam
->vdev
, qcam
);
997 qcam
->port_mode
= (QC_ANY
| QC_NOTSET
);
1001 qcam
->transfer_scale
= 2;
1002 qcam
->contrast
= 192;
1003 qcam
->brightness
= 180;
1004 qcam
->whitebal
= 105;
1008 qcam
->status
= QC_PARAM_CHANGE
;
1012 v4l2_ctrl_handler_free(&qcam
->hdl
);
1017 static int qc_calibrate(struct qcam
*q
)
1020 * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
1021 * The white balance is an individual value for each
1028 qc_command(q
, 27); /* AutoAdjustOffset */
1029 qc_command(q
, 0); /* Dummy Parameter, ignored by the camera */
1031 /* GetOffset (33) will read 255 until autocalibration */
1032 /* is finished. After that, a value of 1-254 will be */
1037 value
= qc_readparam(q
);
1041 } while (value
== 0xff && count
< 2048);
1043 q
->whitebal
= value
;
1047 static int init_bwqcam(struct parport
*port
)
1051 if (num_cams
== MAX_CAMS
) {
1052 printk(KERN_ERR
"Too many Quickcams (max %d)\n", MAX_CAMS
);
1056 qcam
= qcam_init(port
);
1060 parport_claim_or_block(qcam
->pdev
);
1064 if (qc_detect(qcam
) == 0) {
1065 parport_release(qcam
->pdev
);
1066 parport_unregister_device(qcam
->pdev
);
1071 v4l2_ctrl_handler_setup(&qcam
->hdl
);
1073 parport_release(qcam
->pdev
);
1075 v4l2_info(&qcam
->v4l2_dev
, "Connectix Quickcam on %s\n", qcam
->pport
->name
);
1077 if (video_register_device(&qcam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
1078 parport_unregister_device(qcam
->pdev
);
1083 qcams
[num_cams
++] = qcam
;
1088 static void close_bwqcam(struct qcam
*qcam
)
1090 video_unregister_device(&qcam
->vdev
);
1091 v4l2_ctrl_handler_free(&qcam
->hdl
);
1092 parport_unregister_device(qcam
->pdev
);
1096 /* The parport parameter controls which parports will be scanned.
1097 * Scanning all parports causes some printers to print a garbage page.
1098 * -- March 14, 1999 Billy Donahue <billy@escape.com> */
1100 static char *parport
[MAX_CAMS
] = { NULL
, };
1101 module_param_array(parport
, charp
, NULL
, 0);
1104 static int accept_bwqcam(struct parport
*port
)
1109 if (parport
[0] && strncmp(parport
[0], "auto", 4) != 0) {
1110 /* user gave parport parameters */
1111 for (n
= 0; n
< MAX_CAMS
&& parport
[n
]; n
++) {
1114 r
= simple_strtoul(parport
[n
], &ep
, 0);
1115 if (ep
== parport
[n
]) {
1117 "bw-qcam: bad port specifier \"%s\"\n",
1121 if (r
== port
->number
)
1130 static void bwqcam_attach(struct parport
*port
)
1132 if (accept_bwqcam(port
))
1136 static void bwqcam_detach(struct parport
*port
)
1139 for (i
= 0; i
< num_cams
; i
++) {
1140 struct qcam
*qcam
= qcams
[i
];
1141 if (qcam
&& qcam
->pdev
->port
== port
) {
1148 static struct parport_driver bwqcam_driver
= {
1150 .attach
= bwqcam_attach
,
1151 .detach
= bwqcam_detach
,
1154 static void __exit
exit_bw_qcams(void)
1156 parport_unregister_driver(&bwqcam_driver
);
1159 static int __init
init_bw_qcams(void)
1162 /* Do some sanity checks on the module parameters. */
1163 if (maxpoll
> 5000) {
1164 printk(KERN_INFO
"Connectix Quickcam max-poll was above 5000. Using 5000.\n");
1168 if (yieldlines
< 1) {
1169 printk(KERN_INFO
"Connectix Quickcam yieldlines was less than 1. Using 1.\n");
1173 return parport_register_driver(&bwqcam_driver
);
1176 module_init(init_bw_qcams
);
1177 module_exit(exit_bw_qcams
);
1179 MODULE_LICENSE("GPL");
1180 MODULE_VERSION("0.0.3");