Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 76 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 77 | #include <media/v4l2-ioctl.h> |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 78 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include <asm/uaccess.h> |
| 80 | |
| 81 | #include "bw-qcam.h" |
| 82 | |
| 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; |
Brett T. Warden | d685a48 | 2008-01-10 04:33:31 -0300 | [diff] [blame] | 86 | static unsigned int force_init; /* Whether to probe aggressively */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | module_param(maxpoll, int, 0); |
| 89 | module_param(yieldlines, int, 0); |
| 90 | module_param(video_nr, int, 0); |
| 91 | |
Brett T. Warden | d685a48 | 2008-01-10 04:33:31 -0300 | [diff] [blame] | 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); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static inline int read_lpstatus(struct qcam_device *q) |
| 97 | { |
| 98 | return parport_read_status(q->pport); |
| 99 | } |
| 100 | |
| 101 | static inline int read_lpdata(struct qcam_device *q) |
| 102 | { |
| 103 | return parport_read_data(q->pport); |
| 104 | } |
| 105 | |
| 106 | static inline void write_lpdata(struct qcam_device *q, int d) |
| 107 | { |
| 108 | parport_write_data(q->pport, d); |
| 109 | } |
| 110 | |
| 111 | static inline void write_lpcontrol(struct qcam_device *q, int d) |
| 112 | { |
Brett Warden | 7c596fa | 2007-10-02 17:37:21 -0300 | [diff] [blame] | 113 | if (d & 0x20) { |
Brett Warden | 9e19db5 | 2007-09-28 03:19:04 -0300 | [diff] [blame] | 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); |
| 119 | } |
| 120 | |
| 121 | /* Now issue the regular port command, but strip out the |
| 122 | * direction flag */ |
| 123 | d &= ~0x20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | parport_write_control(q->pport, d); |
| 125 | } |
| 126 | |
| 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[]); |
| 132 | |
| 133 | static struct video_device qcam_template; |
| 134 | |
| 135 | static int qc_calibrate(struct qcam_device *q) |
| 136 | { |
| 137 | /* |
| 138 | * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96 |
| 139 | * The white balance is an individiual value for each |
| 140 | * quickcam. |
| 141 | */ |
| 142 | |
| 143 | int value; |
| 144 | int count = 0; |
| 145 | |
| 146 | qc_command(q, 27); /* AutoAdjustOffset */ |
| 147 | qc_command(q, 0); /* Dummy Parameter, ignored by the camera */ |
| 148 | |
| 149 | /* GetOffset (33) will read 255 until autocalibration */ |
| 150 | /* is finished. After that, a value of 1-254 will be */ |
| 151 | /* returned. */ |
| 152 | |
| 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); |
| 160 | |
| 161 | q->whitebal = value; |
| 162 | return value; |
| 163 | } |
| 164 | |
| 165 | /* Initialize the QuickCam driver control structure. This is where |
| 166 | * defaults are set for people who don't have a config file.*/ |
| 167 | |
| 168 | static struct qcam_device *qcam_init(struct parport *port) |
| 169 | { |
| 170 | struct qcam_device *q; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL); |
| 173 | if(q==NULL) |
| 174 | return NULL; |
| 175 | |
| 176 | q->pport = port; |
| 177 | q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL, |
| 178 | NULL, 0, NULL); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 179 | if (q->pdev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | printk(KERN_ERR "bw-qcam: couldn't register for %s.\n", |
| 182 | port->name); |
| 183 | kfree(q); |
| 184 | return NULL; |
| 185 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | memcpy(&q->vdev, &qcam_template, sizeof(qcam_template)); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 188 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 189 | mutex_init(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | q->port_mode = (QC_ANY | QC_NOTSET); |
| 192 | q->width = 320; |
| 193 | q->height = 240; |
| 194 | q->bpp = 4; |
| 195 | q->transfer_scale = 2; |
| 196 | q->contrast = 192; |
| 197 | q->brightness = 180; |
| 198 | q->whitebal = 105; |
| 199 | q->top = 1; |
| 200 | q->left = 14; |
| 201 | q->mode = -1; |
| 202 | q->status = QC_PARAM_CHANGE; |
| 203 | return q; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /* qc_command is probably a bit of a misnomer -- it's used to send |
| 208 | * bytes *to* the camera. Generally, these bytes are either commands |
| 209 | * or arguments to commands, so the name fits, but it still bugs me a |
| 210 | * bit. See the documentation for a list of commands. */ |
| 211 | |
| 212 | static int qc_command(struct qcam_device *q, int command) |
| 213 | { |
| 214 | int n1, n2; |
| 215 | int cmd; |
| 216 | |
| 217 | write_lpdata(q, command); |
| 218 | write_lpcontrol(q, 6); |
| 219 | |
| 220 | n1 = qc_waithand(q, 1); |
| 221 | |
| 222 | write_lpcontrol(q, 0xe); |
| 223 | n2 = qc_waithand(q, 0); |
| 224 | |
| 225 | cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4); |
| 226 | return cmd; |
| 227 | } |
| 228 | |
| 229 | static int qc_readparam(struct qcam_device *q) |
| 230 | { |
| 231 | int n1, n2; |
| 232 | int cmd; |
| 233 | |
| 234 | write_lpcontrol(q, 6); |
| 235 | n1 = qc_waithand(q, 1); |
| 236 | |
| 237 | write_lpcontrol(q, 0xe); |
| 238 | n2 = qc_waithand(q, 0); |
| 239 | |
| 240 | cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4); |
| 241 | return cmd; |
| 242 | } |
| 243 | |
| 244 | /* qc_waithand busy-waits for a handshake signal from the QuickCam. |
| 245 | * Almost all communication with the camera requires handshaking. */ |
| 246 | |
| 247 | static int qc_waithand(struct qcam_device *q, int val) |
| 248 | { |
| 249 | int status; |
| 250 | int runs=0; |
| 251 | |
| 252 | if (val) |
| 253 | { |
| 254 | while (!((status = read_lpstatus(q)) & 8)) |
| 255 | { |
| 256 | /* 1000 is enough spins on the I/O for all normal |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 257 | cases, at that point we start to poll slowly |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | until the camera wakes up. However, we are |
| 259 | busy blocked until the camera responds, so |
| 260 | setting it lower is much better for interactive |
| 261 | response. */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if(runs++>maxpoll) |
| 264 | { |
| 265 | msleep_interruptible(5); |
| 266 | } |
| 267 | if(runs>(maxpoll+1000)) /* 5 seconds */ |
| 268 | return -1; |
| 269 | } |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | while (((status = read_lpstatus(q)) & 8)) |
| 274 | { |
| 275 | /* 1000 is enough spins on the I/O for all normal |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 276 | cases, at that point we start to poll slowly |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | until the camera wakes up. However, we are |
| 278 | busy blocked until the camera responds, so |
| 279 | setting it lower is much better for interactive |
| 280 | response. */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if(runs++>maxpoll) |
| 283 | { |
| 284 | msleep_interruptible(5); |
| 285 | } |
| 286 | if(runs++>(maxpoll+1000)) /* 5 seconds */ |
| 287 | return -1; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return status; |
| 292 | } |
| 293 | |
| 294 | /* Waithand2 is used when the qcam is in bidirectional mode, and the |
| 295 | * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1 |
| 296 | * (bit 3 of status register). It also returns the last value read, |
| 297 | * since this data is useful. */ |
| 298 | |
| 299 | static unsigned int qc_waithand2(struct qcam_device *q, int val) |
| 300 | { |
| 301 | unsigned int status; |
| 302 | int runs=0; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 303 | |
| 304 | do |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | status = read_lpdata(q); |
| 307 | /* 1000 is enough spins on the I/O for all normal |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 308 | cases, at that point we start to poll slowly |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | until the camera wakes up. However, we are |
| 310 | busy blocked until the camera responds, so |
| 311 | setting it lower is much better for interactive |
| 312 | response. */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if(runs++>maxpoll) |
| 315 | { |
| 316 | msleep_interruptible(5); |
| 317 | } |
| 318 | if(runs++>(maxpoll+1000)) /* 5 seconds */ |
| 319 | return 0; |
| 320 | } |
| 321 | while ((status & 1) != val); |
| 322 | |
| 323 | return status; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /* Try to detect a QuickCam. It appears to flash the upper 4 bits of |
| 328 | the status register at 5-10 Hz. This is only used in the autoprobe |
| 329 | code. Be aware that this isn't the way Connectix detects the |
| 330 | camera (they send a reset and try to handshake), but this should be |
| 331 | almost completely safe, while their method screws up my printer if |
| 332 | I plug it in before the camera. */ |
| 333 | |
| 334 | static int qc_detect(struct qcam_device *q) |
| 335 | { |
| 336 | int reg, lastreg; |
| 337 | int count = 0; |
| 338 | int i; |
| 339 | |
Brett T. Warden | d685a48 | 2008-01-10 04:33:31 -0300 | [diff] [blame] | 340 | if (force_init) |
| 341 | return 1; |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | lastreg = reg = read_lpstatus(q) & 0xf0; |
| 344 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 345 | for (i = 0; i < 500; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | reg = read_lpstatus(q) & 0xf0; |
| 348 | if (reg != lastreg) |
| 349 | count++; |
| 350 | lastreg = reg; |
| 351 | mdelay(2); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | #if 0 |
| 356 | /* Force camera detection during testing. Sometimes the camera |
| 357 | won't be flashing these bits. Possibly unloading the module |
| 358 | in the middle of a grab? Or some timeout condition? |
| 359 | I've seen this parameter as low as 19 on my 450Mhz box - mpc */ |
| 360 | printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count); |
| 361 | return 1; |
| 362 | #endif |
| 363 | |
| 364 | /* Be (even more) liberal in what you accept... */ |
| 365 | |
Brett Warden | 7c596fa | 2007-10-02 17:37:21 -0300 | [diff] [blame] | 366 | if (count > 20 && count < 400) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return 1; /* found */ |
Brett Warden | 9e19db5 | 2007-09-28 03:19:04 -0300 | [diff] [blame] | 368 | } else { |
Brett Warden | 7c596fa | 2007-10-02 17:37:21 -0300 | [diff] [blame] | 369 | printk(KERN_ERR "No Quickcam found on port %s\n", |
| 370 | q->pport->name); |
Brett T. Warden | d685a48 | 2008-01-10 04:33:31 -0300 | [diff] [blame] | 371 | printk(KERN_DEBUG "Quickcam detection counter: %u\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return 0; /* not found */ |
Brett Warden | 9e19db5 | 2007-09-28 03:19:04 -0300 | [diff] [blame] | 373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
| 377 | /* Reset the QuickCam. This uses the same sequence the Windows |
| 378 | * QuickPic program uses. Someone with a bi-directional port should |
| 379 | * check that bi-directional mode is detected right, and then |
| 380 | * implement bi-directional mode in qc_readbyte(). */ |
| 381 | |
| 382 | static void qc_reset(struct qcam_device *q) |
| 383 | { |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 384 | switch (q->port_mode & QC_FORCE_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
| 386 | case QC_FORCE_UNIDIR: |
| 387 | q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR; |
| 388 | break; |
| 389 | |
| 390 | case QC_FORCE_BIDIR: |
| 391 | q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR; |
| 392 | break; |
| 393 | |
| 394 | case QC_ANY: |
| 395 | write_lpcontrol(q, 0x20); |
| 396 | write_lpdata(q, 0x75); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (read_lpdata(q) != 0x75) { |
| 399 | q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR; |
| 400 | } else { |
| 401 | q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR; |
| 402 | } |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | write_lpcontrol(q, 0xb); |
| 407 | udelay(250); |
| 408 | write_lpcontrol(q, 0xe); |
| 409 | qc_setscanmode(q); /* in case port_mode changed */ |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /* Decide which scan mode to use. There's no real requirement that |
| 414 | * the scanmode match the resolution in q->height and q-> width -- the |
| 415 | * camera takes the picture at the resolution specified in the |
| 416 | * "scanmode" and then returns the image at the resolution specified |
| 417 | * with the resolution commands. If the scan is bigger than the |
| 418 | * requested resolution, the upper-left hand corner of the scan is |
| 419 | * returned. If the scan is smaller, then the rest of the image |
| 420 | * returned contains garbage. */ |
| 421 | |
| 422 | static int qc_setscanmode(struct qcam_device *q) |
| 423 | { |
| 424 | int old_mode = q->mode; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 425 | |
| 426 | switch (q->transfer_scale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | case 1: |
| 429 | q->mode = 0; |
| 430 | break; |
| 431 | case 2: |
| 432 | q->mode = 4; |
| 433 | break; |
| 434 | case 4: |
| 435 | q->mode = 8; |
| 436 | break; |
| 437 | } |
| 438 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 439 | switch (q->bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | case 4: |
| 442 | break; |
| 443 | case 6: |
| 444 | q->mode += 2; |
| 445 | break; |
| 446 | } |
| 447 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 448 | switch (q->port_mode & QC_MODE_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | case QC_BIDIR: |
| 451 | q->mode += 1; |
| 452 | break; |
| 453 | case QC_NOTSET: |
| 454 | case QC_UNIDIR: |
| 455 | break; |
| 456 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if (q->mode != old_mode) |
| 459 | q->status |= QC_PARAM_CHANGE; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /* Reset the QuickCam and program for brightness, contrast, |
| 466 | * white-balance, and resolution. */ |
| 467 | |
| 468 | static void qc_set(struct qcam_device *q) |
| 469 | { |
| 470 | int val; |
| 471 | int val2; |
| 472 | |
| 473 | qc_reset(q); |
| 474 | |
| 475 | /* Set the brightness. Yes, this is repetitive, but it works. |
| 476 | * Shorter versions seem to fail subtly. Feel free to try :-). */ |
| 477 | /* I think the problem was in qc_command, not here -- bls */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | qc_command(q, 0xb); |
| 480 | qc_command(q, q->brightness); |
| 481 | |
| 482 | val = q->height / q->transfer_scale; |
| 483 | qc_command(q, 0x11); |
| 484 | qc_command(q, val); |
| 485 | if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) { |
| 486 | /* The normal "transfers per line" calculation doesn't seem to work |
| 487 | as expected here (and yet it works fine in qc_scan). No idea |
| 488 | why this case is the odd man out. Fortunately, Laird's original |
| 489 | working version gives me a good way to guess at working values. |
| 490 | -- bls */ |
| 491 | val = q->width; |
| 492 | val2 = q->transfer_scale * 4; |
| 493 | } else { |
| 494 | val = q->width * q->bpp; |
| 495 | val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) * |
| 496 | q->transfer_scale; |
| 497 | } |
Julia Lawall | e9e24ce | 2008-08-20 20:44:53 -0300 | [diff] [blame] | 498 | val = DIV_ROUND_UP(val, val2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | qc_command(q, 0x13); |
| 500 | qc_command(q, val); |
| 501 | |
| 502 | /* Setting top and left -- bls */ |
| 503 | qc_command(q, 0xd); |
| 504 | qc_command(q, q->top); |
| 505 | qc_command(q, 0xf); |
| 506 | qc_command(q, q->left / 2); |
| 507 | |
| 508 | qc_command(q, 0x19); |
| 509 | qc_command(q, q->contrast); |
| 510 | qc_command(q, 0x1f); |
| 511 | qc_command(q, q->whitebal); |
| 512 | |
| 513 | /* Clear flag that we must update the grabbing parameters on the camera |
| 514 | before we grab the next frame */ |
| 515 | q->status &= (~QC_PARAM_CHANGE); |
| 516 | } |
| 517 | |
| 518 | /* Qc_readbytes reads some bytes from the QC and puts them in |
| 519 | the supplied buffer. It returns the number of bytes read, |
| 520 | or -1 on error. */ |
| 521 | |
| 522 | static inline int qc_readbytes(struct qcam_device *q, char buffer[]) |
| 523 | { |
| 524 | int ret=1; |
| 525 | unsigned int hi, lo; |
| 526 | unsigned int hi2, lo2; |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 527 | static int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 529 | if (buffer == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
| 531 | state = 0; |
| 532 | return 0; |
| 533 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 534 | |
| 535 | switch (q->port_mode & QC_MODE_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
| 537 | case QC_BIDIR: /* Bi-directional Port */ |
| 538 | write_lpcontrol(q, 0x26); |
| 539 | lo = (qc_waithand2(q, 1) >> 1); |
| 540 | hi = (read_lpstatus(q) >> 3) & 0x1f; |
| 541 | write_lpcontrol(q, 0x2e); |
| 542 | lo2 = (qc_waithand2(q, 0) >> 1); |
| 543 | hi2 = (read_lpstatus(q) >> 3) & 0x1f; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 544 | switch (q->bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
| 546 | case 4: |
| 547 | buffer[0] = lo & 0xf; |
| 548 | buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3); |
| 549 | buffer[2] = (hi & 0x1e) >> 1; |
| 550 | buffer[3] = lo2 & 0xf; |
| 551 | buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3); |
| 552 | buffer[5] = (hi2 & 0x1e) >> 1; |
| 553 | ret = 6; |
| 554 | break; |
| 555 | case 6: |
| 556 | buffer[0] = lo & 0x3f; |
| 557 | buffer[1] = ((lo & 0x40) >> 6) | (hi << 1); |
| 558 | buffer[2] = lo2 & 0x3f; |
| 559 | buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1); |
| 560 | ret = 4; |
| 561 | break; |
| 562 | } |
| 563 | break; |
| 564 | |
| 565 | case QC_UNIDIR: /* Unidirectional Port */ |
| 566 | write_lpcontrol(q, 6); |
| 567 | lo = (qc_waithand(q, 1) & 0xf0) >> 4; |
| 568 | write_lpcontrol(q, 0xe); |
| 569 | hi = (qc_waithand(q, 0) & 0xf0) >> 4; |
| 570 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 571 | switch (q->bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
| 573 | case 4: |
| 574 | buffer[0] = lo; |
| 575 | buffer[1] = hi; |
| 576 | ret = 2; |
| 577 | break; |
| 578 | case 6: |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 579 | switch (state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
| 581 | case 0: |
| 582 | buffer[0] = (lo << 2) | ((hi & 0xc) >> 2); |
| 583 | q->saved_bits = (hi & 3) << 4; |
| 584 | state = 1; |
| 585 | ret = 1; |
| 586 | break; |
| 587 | case 1: |
| 588 | buffer[0] = lo | q->saved_bits; |
| 589 | q->saved_bits = hi << 2; |
| 590 | state = 2; |
| 591 | ret = 1; |
| 592 | break; |
| 593 | case 2: |
| 594 | buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits; |
| 595 | buffer[1] = ((lo & 3) << 4) | hi; |
| 596 | state = 0; |
| 597 | ret = 2; |
| 598 | break; |
| 599 | } |
| 600 | break; |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | /* requests a scan from the camera. It sends the correct instructions |
| 608 | * to the camera and then reads back the correct number of bytes. In |
| 609 | * previous versions of this routine the return structure contained |
| 610 | * the raw output from the camera, and there was a 'qc_convertscan' |
| 611 | * function that converted that to a useful format. In version 0.3 I |
| 612 | * rolled qc_convertscan into qc_scan and now I only return the |
| 613 | * converted scan. The format is just an one-dimensional array of |
| 614 | * characters, one for each pixel, with 0=black up to n=white, where |
| 615 | * n=2^(bit depth)-1. Ask me for more details if you don't understand |
| 616 | * this. */ |
| 617 | |
| 618 | static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len) |
| 619 | { |
| 620 | int i, j, k, yield; |
| 621 | int bytes; |
| 622 | int linestotrans, transperline; |
| 623 | int divisor; |
| 624 | int pixels_per_line; |
| 625 | int pixels_read = 0; |
| 626 | int got=0; |
| 627 | char buffer[6]; |
| 628 | int shift=8-q->bpp; |
| 629 | char invert; |
| 630 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 631 | if (q->mode == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return -ENXIO; |
| 633 | |
| 634 | qc_command(q, 0x7); |
| 635 | qc_command(q, q->mode); |
| 636 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 637 | if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
| 639 | write_lpcontrol(q, 0x2e); /* turn port around */ |
| 640 | write_lpcontrol(q, 0x26); |
| 641 | (void) qc_waithand(q, 1); |
| 642 | write_lpcontrol(q, 0x2e); |
| 643 | (void) qc_waithand(q, 0); |
| 644 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* strange -- should be 15:63 below, but 4bpp is odd */ |
| 647 | invert = (q->bpp == 4) ? 16 : 63; |
| 648 | |
| 649 | linestotrans = q->height / q->transfer_scale; |
| 650 | pixels_per_line = q->width / q->transfer_scale; |
| 651 | transperline = q->width * q->bpp; |
| 652 | divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) * |
| 653 | q->transfer_scale; |
Julia Lawall | e9e24ce | 2008-08-20 20:44:53 -0300 | [diff] [blame] | 654 | transperline = DIV_ROUND_UP(transperline, divisor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 656 | for (i = 0, yield = yieldlines; i < linestotrans; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 658 | for (pixels_read = j = 0; j < transperline; j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
| 660 | bytes = qc_readbytes(q, buffer); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 661 | for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | int o; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 664 | if (buffer[k] == 0 && invert == 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
| 666 | /* 4bpp is odd (again) -- inverter is 16, not 15, but output |
| 667 | must be 0-15 -- bls */ |
| 668 | buffer[k] = 16; |
| 669 | } |
| 670 | o=i*pixels_per_line + pixels_read + k; |
| 671 | if(o<len) |
| 672 | { |
| 673 | got++; |
| 674 | put_user((invert - buffer[k])<<shift, buf+o); |
| 675 | } |
| 676 | } |
| 677 | pixels_read += bytes; |
| 678 | } |
| 679 | (void) qc_readbytes(q, NULL); /* reset state machine */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | /* Grabbing an entire frame from the quickcam is a lengthy |
| 682 | process. We don't (usually) want to busy-block the |
| 683 | processor for the entire frame. yieldlines is a module |
| 684 | parameter. If we yield every line, the minimum frame |
| 685 | time will be 240 / 200 = 1.2 seconds. The compile-time |
| 686 | default is to yield every 4 lines. */ |
| 687 | if (i >= yield) { |
| 688 | msleep_interruptible(5); |
| 689 | yield = i + yieldlines; |
| 690 | } |
| 691 | } |
| 692 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 693 | if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | write_lpcontrol(q, 2); |
| 696 | write_lpcontrol(q, 6); |
| 697 | udelay(3); |
| 698 | write_lpcontrol(q, 0xe); |
| 699 | } |
| 700 | if(got<len) |
| 701 | return got; |
| 702 | return len; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Video4linux interfacing |
| 707 | */ |
| 708 | |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 709 | static int qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | struct video_device *dev = video_devdata(file); |
| 712 | struct qcam_device *qcam=(struct qcam_device *)dev; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 785 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if(p->depth!=4 && p->depth!=6) |
| 787 | return -EINVAL; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 798 | mutex_lock(&qcam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | qc_setscanmode(qcam); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 800 | mutex_unlock(&qcam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | qcam->width = 320; |
| 818 | qcam->height = 240; |
| 819 | qcam->transfer_scale = 4; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 831 | mutex_lock(&qcam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | qc_setscanmode(qcam); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 833 | mutex_unlock(&qcam->lock); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* We must update the camera before we grab. We could |
| 836 | just have changed the grab size */ |
| 837 | qcam->status |= QC_PARAM_CHANGE; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 866 | static int qcam_ioctl(struct inode *inode, struct file *file, |
| 867 | unsigned int cmd, unsigned long arg) |
| 868 | { |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 869 | return video_usercopy(file, cmd, arg, qcam_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static 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); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 879 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 880 | mutex_lock(&qcam->lock); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 889 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 890 | mutex_unlock(&qcam->lock); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | parport_release(qcam->pdev); |
| 893 | return len; |
| 894 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 895 | |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 896 | static int qcam_exclusive_open(struct inode *inode, struct file *file) |
| 897 | { |
| 898 | struct video_device *dev = video_devdata(file); |
| 899 | struct qcam_device *qcam = (struct qcam_device *)dev; |
| 900 | |
| 901 | return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0; |
| 902 | } |
| 903 | |
| 904 | static int qcam_exclusive_release(struct inode *inode, struct file *file) |
| 905 | { |
| 906 | struct video_device *dev = video_devdata(file); |
| 907 | struct qcam_device *qcam = (struct qcam_device *)dev; |
| 908 | |
| 909 | clear_bit(0, &qcam->in_use); |
| 910 | return 0; |
| 911 | } |
| 912 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 913 | static const struct file_operations qcam_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | .owner = THIS_MODULE, |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 915 | .open = qcam_exclusive_open, |
| 916 | .release = qcam_exclusive_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | .ioctl = qcam_ioctl, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 918 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 0d0fbf8 | 2006-01-09 15:24:57 -0200 | [diff] [blame] | 919 | .compat_ioctl = v4l_compat_ioctl32, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 920 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | .read = qcam_read, |
| 922 | .llseek = no_llseek, |
| 923 | }; |
| 924 | static struct video_device qcam_template= |
| 925 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | .name = "Connectix Quickcam", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | .fops = &qcam_fops, |
Hans Verkuil | aa5e90a | 2008-08-23 06:23:55 -0300 | [diff] [blame] | 928 | .release = video_device_release_empty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | }; |
| 930 | |
| 931 | #define MAX_CAMS 4 |
| 932 | static struct qcam_device *qcams[MAX_CAMS]; |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 933 | static unsigned int num_cams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
| 935 | static int init_bwqcam(struct parport *port) |
| 936 | { |
| 937 | struct qcam_device *qcam; |
| 938 | |
| 939 | if (num_cams == MAX_CAMS) |
| 940 | { |
| 941 | printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS); |
| 942 | return -ENOSPC; |
| 943 | } |
| 944 | |
| 945 | qcam=qcam_init(port); |
| 946 | if(qcam==NULL) |
| 947 | return -ENODEV; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | parport_claim_or_block(qcam->pdev); |
| 950 | |
| 951 | qc_reset(qcam); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | if(qc_detect(qcam)==0) |
| 954 | { |
| 955 | parport_release(qcam->pdev); |
| 956 | parport_unregister_device(qcam->pdev); |
| 957 | kfree(qcam); |
| 958 | return -ENODEV; |
| 959 | } |
| 960 | qc_calibrate(qcam); |
| 961 | |
| 962 | parport_release(qcam->pdev); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 965 | |
Hans Verkuil | dc60de3 | 2008-09-03 17:11:58 -0300 | [diff] [blame] | 966 | if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | parport_unregister_device(qcam->pdev); |
| 968 | kfree(qcam); |
| 969 | return -ENODEV; |
| 970 | } |
| 971 | |
| 972 | qcams[num_cams++] = qcam; |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static void close_bwqcam(struct qcam_device *qcam) |
| 978 | { |
| 979 | video_unregister_device(&qcam->vdev); |
| 980 | parport_unregister_device(qcam->pdev); |
| 981 | kfree(qcam); |
| 982 | } |
| 983 | |
| 984 | /* The parport parameter controls which parports will be scanned. |
| 985 | * Scanning all parports causes some printers to print a garbage page. |
| 986 | * -- March 14, 1999 Billy Donahue <billy@escape.com> */ |
| 987 | #ifdef MODULE |
| 988 | static char *parport[MAX_CAMS] = { NULL, }; |
| 989 | module_param_array(parport, charp, NULL, 0); |
| 990 | #endif |
| 991 | |
| 992 | static int accept_bwqcam(struct parport *port) |
| 993 | { |
| 994 | #ifdef MODULE |
| 995 | int n; |
| 996 | |
| 997 | if (parport[0] && strncmp(parport[0], "auto", 4) != 0) { |
| 998 | /* user gave parport parameters */ |
| 999 | for(n=0; parport[n] && n<MAX_CAMS; n++){ |
| 1000 | char *ep; |
| 1001 | unsigned long r; |
| 1002 | r = simple_strtoul(parport[n], &ep, 0); |
| 1003 | if (ep == parport[n]) { |
| 1004 | printk(KERN_ERR |
| 1005 | "bw-qcam: bad port specifier \"%s\"\n", |
| 1006 | parport[n]); |
| 1007 | continue; |
| 1008 | } |
| 1009 | if (r == port->number) |
| 1010 | return 1; |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } |
| 1014 | #endif |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
| 1018 | static void bwqcam_attach(struct parport *port) |
| 1019 | { |
| 1020 | if (accept_bwqcam(port)) |
| 1021 | init_bwqcam(port); |
| 1022 | } |
| 1023 | |
| 1024 | static void bwqcam_detach(struct parport *port) |
| 1025 | { |
| 1026 | int i; |
| 1027 | for (i = 0; i < num_cams; i++) { |
| 1028 | struct qcam_device *qcam = qcams[i]; |
| 1029 | if (qcam && qcam->pdev->port == port) { |
| 1030 | qcams[i] = NULL; |
| 1031 | close_bwqcam(qcam); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | static struct parport_driver bwqcam_driver = { |
| 1037 | .name = "bw-qcam", |
| 1038 | .attach = bwqcam_attach, |
| 1039 | .detach = bwqcam_detach, |
| 1040 | }; |
| 1041 | |
| 1042 | static void __exit exit_bw_qcams(void) |
| 1043 | { |
| 1044 | parport_unregister_driver(&bwqcam_driver); |
| 1045 | } |
| 1046 | |
| 1047 | static int __init init_bw_qcams(void) |
| 1048 | { |
| 1049 | #ifdef MODULE |
| 1050 | /* Do some sanity checks on the module parameters. */ |
| 1051 | if (maxpoll > 5000) { |
| 1052 | printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n"); |
| 1053 | maxpoll = 5000; |
| 1054 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | if (yieldlines < 1) { |
| 1057 | printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n"); |
| 1058 | yieldlines = 1; |
| 1059 | } |
| 1060 | #endif |
| 1061 | return parport_register_driver(&bwqcam_driver); |
| 1062 | } |
| 1063 | |
| 1064 | module_init(init_bw_qcams); |
| 1065 | module_exit(exit_bw_qcams); |
| 1066 | |
| 1067 | MODULE_LICENSE("GPL"); |