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