?? bw-qcam.c
字號:
/* * QuickCam Driver For Video4Linux. * * This version only works as a module. * * Video4Linux conversion work by Alan Cox. * Parport compatibility by Phil Blundell. * Busy loop avoidance by Mark Cooke. * * Module parameters: * * maxpoll=<1 - 5000> * * When polling the QuickCam for a response, busy-wait for a * maximum of this many loops. The default of 250 gives little * impact on interactive response. * * NOTE: If this parameter is set too high, the processor * will busy wait until this loop times out, and then * slowly poll for a further 5 seconds before failing * the transaction. You have been warned. * * yieldlines=<1 - 250> * * When acquiring a frame from the camera, the data gathering * loop will yield back to the scheduler after completing * this many lines. The default of 4 provides a trade-off * between increased frame acquisition time and impact on * interactive response. *//* qcam-lib.c -- Library for programming with the Connectix QuickCam. * See the included documentation for usage instructions and details * of the protocol involved. *//* Version 0.5, August 4, 1996 *//* Version 0.7, August 27, 1996 *//* Version 0.9, November 17, 1996 *//******************************************************************Copyright (C) 1996 by Scott LairdPermission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY CLAIM, DAMAGES OROTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.******************************************************************/#include <linux/module.h>#include <linux/delay.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/malloc.h>#include <linux/mm.h>#include <linux/parport.h>#include <linux/sched.h>#include <linux/version.h>#include <linux/videodev.h>#include <asm/uaccess.h>#include "bw-qcam.h"#if LINUX_VERSION_CODE >= 0x020117MODULE_PARM(maxpoll,"i");MODULE_PARM(yieldlines,"i"); #endifstatic unsigned int maxpoll=250; /* Maximum busy-loop count for qcam I/O */static unsigned int yieldlines=4; /* Yield after this many during capture */extern __inline__ int read_lpstatus(struct qcam_device *q){ return parport_read_status(q->pport);}extern __inline__ int read_lpcontrol(struct qcam_device *q){ return parport_read_control(q->pport);}extern __inline__ int read_lpdata(struct qcam_device *q){ return parport_read_data(q->pport);}extern __inline__ void write_lpdata(struct qcam_device *q, int d){ parport_write_data(q->pport, d);}extern __inline__ void write_lpcontrol(struct qcam_device *q, int d){ parport_write_control(q->pport, d);}static int qc_waithand(struct qcam_device *q, int val);static int qc_command(struct qcam_device *q, int command);static int qc_readparam(struct qcam_device *q);static int qc_setscanmode(struct qcam_device *q);static int qc_readbytes(struct qcam_device *q, char buffer[]);static struct video_device qcam_template;static int qc_calibrate(struct qcam_device *q){ /* * Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96 * The white balance is an individiual value for each * quickcam. */ int value; int count = 0; qc_command(q, 27); /* AutoAdjustOffset */ qc_command(q, 0); /* Dummy Parameter, ignored by the camera */ /* GetOffset (33) will read 255 until autocalibration */ /* is finished. After that, a value of 1-254 will be */ /* returned. */ do { qc_command(q, 33); value = qc_readparam(q); mdelay(1); schedule(); count++; } while (value == 0xff && count<2048); q->whitebal = value; return value;}/* Initialize the QuickCam driver control structure. This is where * defaults are set for people who don't have a config file.*/static struct qcam_device *qcam_init(struct parport *port){ struct qcam_device *q; q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL); q->pport = port; q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL, NULL, 0, NULL); if (q->pdev == NULL) { printk(KERN_ERR "bw-qcam: couldn't register for %s.\n", port->name); kfree(q); return NULL; } memcpy(&q->vdev, &qcam_template, sizeof(qcam_template)); q->port_mode = (QC_ANY | QC_NOTSET); q->width = 320; q->height = 240; q->bpp = 4; q->transfer_scale = 2; q->contrast = 192; q->brightness = 180; q->whitebal = 105; q->top = 1; q->left = 14; q->mode = -1; q->status = QC_PARAM_CHANGE; return q;}/* qc_command is probably a bit of a misnomer -- it's used to send * bytes *to* the camera. Generally, these bytes are either commands * or arguments to commands, so the name fits, but it still bugs me a * bit. See the documentation for a list of commands. */static int qc_command(struct qcam_device *q, int command){ int n1, n2; int cmd; write_lpdata(q, command); write_lpcontrol(q, 6); n1 = qc_waithand(q, 1); write_lpcontrol(q, 0xe); n2 = qc_waithand(q, 0); cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4); return cmd;}static int qc_readparam(struct qcam_device *q){ int n1, n2; int cmd; write_lpcontrol(q, 6); n1 = qc_waithand(q, 1); write_lpcontrol(q, 0xe); n2 = qc_waithand(q, 0); cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4); return cmd;}/* qc_waithand busy-waits for a handshake signal from the QuickCam. * Almost all communication with the camera requires handshaking. */static int qc_waithand(struct qcam_device *q, int val){ int status; int runs=0; if (val) { while (!((status = read_lpstatus(q)) & 8)) { /* 1000 is enough spins on the I/O for all normal cases, at that point we start to poll slowly until the camera wakes up. However, we are busy blocked until the camera responds, so setting it lower is much better for interactive response. */ if(runs++>maxpoll) { current->state=TASK_INTERRUPTIBLE; schedule_timeout(HZ/200); } if(runs>(maxpoll+1000)) /* 5 seconds */ return -1; } } else { while (((status = read_lpstatus(q)) & 8)) { /* 1000 is enough spins on the I/O for all normal cases, at that point we start to poll slowly until the camera wakes up. However, we are busy blocked until the camera responds, so setting it lower is much better for interactive response. */ if(runs++>maxpoll) { current->state=TASK_INTERRUPTIBLE; schedule_timeout(HZ/200); } if(runs++>(maxpoll+1000)) /* 5 seconds */ return -1; } } return status;}/* Waithand2 is used when the qcam is in bidirectional mode, and the * handshaking signal is CamRdy2 (bit 0 of data reg) instead of CamRdy1 * (bit 3 of status register). It also returns the last value read, * since this data is useful. */static unsigned int qc_waithand2(struct qcam_device *q, int val){ unsigned int status; int runs=0; do { status = read_lpdata(q); /* 1000 is enough spins on the I/O for all normal cases, at that point we start to poll slowly until the camera wakes up. However, we are busy blocked until the camera responds, so setting it lower is much better for interactive response. */ if(runs++>maxpoll) { current->state=TASK_INTERRUPTIBLE; schedule_timeout(HZ/200); } if(runs++>(maxpoll+1000)) /* 5 seconds */ return 0; } while ((status & 1) != val); return status;}/* Try to detect a QuickCam. It appears to flash the upper 4 bits of the status register at 5-10 Hz. This is only used in the autoprobe code. Be aware that this isn't the way Connectix detects the camera (they send a reset and try to handshake), but this should be almost completely safe, while their method screws up my printer if I plug it in before the camera. */static int qc_detect(struct qcam_device *q){ int reg, lastreg; int count = 0; int i; lastreg = reg = read_lpstatus(q) & 0xf0; for (i = 0; i < 500; i++) { reg = read_lpstatus(q) & 0xf0; if (reg != lastreg) count++; lastreg = reg; mdelay(2); }#if 0 /* Force camera detection during testing. Sometimes the camera won't be flashing these bits. Possibly unloading the module in the middle of a grab? Or some timeout condition? I've seen this parameter as low as 19 on my 450Mhz box - mpc */ printk("Debugging: QCam detection counter <30-200 counts as detected>: %d\n", count); return 1;#endif /* Be (even more) liberal in what you accept... *//* if (count > 30 && count < 200) */ if (count > 20 && count < 300) return 1; /* found */ else return 0; /* not found */}/* Reset the QuickCam. This uses the same sequence the Windows * QuickPic program uses. Someone with a bi-directional port should * check that bi-directional mode is detected right, and then * implement bi-directional mode in qc_readbyte(). */static void qc_reset(struct qcam_device *q){ switch (q->port_mode & QC_FORCE_MASK) { case QC_FORCE_UNIDIR: q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR; break; case QC_FORCE_BIDIR: q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR; break; case QC_ANY: write_lpcontrol(q, 0x20); write_lpdata(q, 0x75); if (read_lpdata(q) != 0x75) { q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR; } else { q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR; } break; } write_lpcontrol(q, 0xb); udelay(250); write_lpcontrol(q, 0xe); qc_setscanmode(q); /* in case port_mode changed */}/* Decide which scan mode to use. There's no real requirement that * the scanmode match the resolution in q->height and q-> width -- the * camera takes the picture at the resolution specified in the * "scanmode" and then returns the image at the resolution specified * with the resolution commands. If the scan is bigger than the * requested resolution, the upper-left hand corner of the scan is * returned. If the scan is smaller, then the rest of the image * returned contains garbage. */static int qc_setscanmode(struct qcam_device *q){ int old_mode = q->mode; switch (q->transfer_scale) { case 1: q->mode = 0; break; case 2: q->mode = 4; break; case 4: q->mode = 8; break; } switch (q->bpp) { case 4: break; case 6: q->mode += 2; break; } switch (q->port_mode & QC_MODE_MASK) { case QC_BIDIR: q->mode += 1; break; case QC_NOTSET: case QC_UNIDIR: break; } if (q->mode != old_mode) q->status |= QC_PARAM_CHANGE; return 0;}/* Reset the QuickCam and program for brightness, contrast, * white-balance, and resolution. */void qc_set(struct qcam_device *q){ int val; int val2; qc_reset(q); /* Set the brightness. Yes, this is repetitive, but it works. * Shorter versions seem to fail subtly. Feel free to try :-). */ /* I think the problem was in qc_command, not here -- bls */ qc_command(q, 0xb); qc_command(q, q->brightness); val = q->height / q->transfer_scale; qc_command(q, 0x11); qc_command(q, val); if ((q->port_mode & QC_MODE_MASK) == QC_UNIDIR && q->bpp == 6) { /* The normal "transfers per line" calculation doesn't seem to work as expected here (and yet it works fine in qc_scan). No idea why this case is the odd man out. Fortunately, Laird's original working version gives me a good way to guess at working values. -- bls */ val = q->width; val2 = q->transfer_scale * 4; } else { val = q->width * q->bpp; val2 = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) * q->transfer_scale; } val = (val + val2 - 1) / val2; qc_command(q, 0x13); qc_command(q, val); /* Setting top and left -- bls */ qc_command(q, 0xd); qc_command(q, q->top); qc_command(q, 0xf); qc_command(q, q->left / 2); qc_command(q, 0x19); qc_command(q, q->contrast); qc_command(q, 0x1f); qc_command(q, q->whitebal); /* Clear flag that we must update the grabbing parameters on the camera before we grab the next frame */ q->status &= (~QC_PARAM_CHANGE);}/* Qc_readbytes reads some bytes from the QC and puts them in the supplied buffer. It returns the number of bytes read, or -1 on error. */extern __inline__ int qc_readbytes(struct qcam_device *q, char buffer[]){ int ret=1; unsigned int hi, lo; unsigned int hi2, lo2; static int state = 0; if (buffer == NULL) { state = 0; return 0; } switch (q->port_mode & QC_MODE_MASK) { case QC_BIDIR: /* Bi-directional Port */ write_lpcontrol(q, 0x26); lo = (qc_waithand2(q, 1) >> 1); hi = (read_lpstatus(q) >> 3) & 0x1f; write_lpcontrol(q, 0x2e); lo2 = (qc_waithand2(q, 0) >> 1); hi2 = (read_lpstatus(q) >> 3) & 0x1f; switch (q->bpp) { case 4: buffer[0] = lo & 0xf;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -