📄 bw-qcam.c
字号:
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; buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3); buffer[2] = (hi & 0x1e) >> 1; buffer[3] = lo2 & 0xf; buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3); buffer[5] = (hi2 & 0x1e) >> 1; ret = 6; break; case 6: buffer[0] = lo & 0x3f; buffer[1] = ((lo & 0x40) >> 6) | (hi << 1); buffer[2] = lo2 & 0x3f; buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1); ret = 4; break; } break; case QC_UNIDIR: /* Unidirectional Port */ write_lpcontrol(q, 6); lo = (qc_waithand(q, 1) & 0xf0) >> 4; write_lpcontrol(q, 0xe); hi = (qc_waithand(q, 0) & 0xf0) >> 4; switch (q->bpp) { case 4: buffer[0] = lo; buffer[1] = hi; ret = 2; break; case 6: switch (state) { case 0: buffer[0] = (lo << 2) | ((hi & 0xc) >> 2); q->saved_bits = (hi & 3) << 4; state = 1; ret = 1; break; case 1: buffer[0] = lo | q->saved_bits; q->saved_bits = hi << 2; state = 2; ret = 1; break; case 2: buffer[0] = ((lo & 0xc) >> 2) | q->saved_bits; buffer[1] = ((lo & 3) << 4) | hi; state = 0; ret = 2; break; } break; } break; } return ret;}/* requests a scan from the camera. It sends the correct instructions * to the camera and then reads back the correct number of bytes. In * previous versions of this routine the return structure contained * the raw output from the camera, and there was a 'qc_convertscan' * function that converted that to a useful format. In version 0.3 I * rolled qc_convertscan into qc_scan and now I only return the * converted scan. The format is just an one-dimensional array of * characters, one for each pixel, with 0=black up to n=white, where * n=2^(bit depth)-1. Ask me for more details if you don't understand * this. */static long qc_capture(struct qcam_device * q, char __user *buf, unsigned long len){ int i, j, k, yield; int bytes; int linestotrans, transperline; int divisor; int pixels_per_line; int pixels_read = 0; int got=0; char buffer[6]; int shift=8-q->bpp; char invert; if (q->mode == -1) return -ENXIO; qc_command(q, 0x7); qc_command(q, q->mode); if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) { write_lpcontrol(q, 0x2e); /* turn port around */ write_lpcontrol(q, 0x26); (void) qc_waithand(q, 1); write_lpcontrol(q, 0x2e); (void) qc_waithand(q, 0); } /* strange -- should be 15:63 below, but 4bpp is odd */ invert = (q->bpp == 4) ? 16 : 63; linestotrans = q->height / q->transfer_scale; pixels_per_line = q->width / q->transfer_scale; transperline = q->width * q->bpp; divisor = (((q->port_mode & QC_MODE_MASK) == QC_BIDIR) ? 24 : 8) * q->transfer_scale; transperline = (transperline + divisor - 1) / divisor; for (i = 0, yield = yieldlines; i < linestotrans; i++) { for (pixels_read = j = 0; j < transperline; j++) { bytes = qc_readbytes(q, buffer); for (k = 0; k < bytes && (pixels_read + k) < pixels_per_line; k++) { int o; if (buffer[k] == 0 && invert == 16) { /* 4bpp is odd (again) -- inverter is 16, not 15, but output must be 0-15 -- bls */ buffer[k] = 16; } o=i*pixels_per_line + pixels_read + k; if(o<len) { got++; put_user((invert - buffer[k])<<shift, buf+o); } } pixels_read += bytes; } (void) qc_readbytes(q, NULL); /* reset state machine */ /* Grabbing an entire frame from the quickcam is a lengthy process. We don't (usually) want to busy-block the processor for the entire frame. yieldlines is a module parameter. If we yield every line, the minimum frame time will be 240 / 200 = 1.2 seconds. The compile-time default is to yield every 4 lines. */ if (i >= yield) { msleep_interruptible(5); yield = i + yieldlines; } } if ((q->port_mode & QC_MODE_MASK) == QC_BIDIR) { write_lpcontrol(q, 2); write_lpcontrol(q, 6); udelay(3); write_lpcontrol(q, 0xe); } if(got<len) return got; return len;}/* * Video4linux interfacing */static int qcam_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg){ struct video_device *dev = video_devdata(file); struct qcam_device *qcam=(struct qcam_device *)dev; switch(cmd) { case VIDIOCGCAP: { struct video_capability *b = arg; strcpy(b->name, "Quickcam"); b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME; b->channels = 1; b->audios = 0; b->maxwidth = 320; b->maxheight = 240; b->minwidth = 80; b->minheight = 60; return 0; } case VIDIOCGCHAN: { struct video_channel *v = arg; if(v->channel!=0) return -EINVAL; v->flags=0; v->tuners=0; /* Good question.. its composite or SVHS so.. */ v->type = VIDEO_TYPE_CAMERA; strcpy(v->name, "Camera"); return 0; } case VIDIOCSCHAN: { struct video_channel *v = arg; if(v->channel!=0) return -EINVAL; return 0; } case VIDIOCGTUNER: { struct video_tuner *v = arg; if(v->tuner) return -EINVAL; strcpy(v->name, "Format"); v->rangelow=0; v->rangehigh=0; v->flags= 0; v->mode = VIDEO_MODE_AUTO; return 0; } case VIDIOCSTUNER: { struct video_tuner *v = arg; if(v->tuner) return -EINVAL; if(v->mode!=VIDEO_MODE_AUTO) return -EINVAL; return 0; } case VIDIOCGPICT: { struct video_picture *p = arg; p->colour=0x8000; p->hue=0x8000; p->brightness=qcam->brightness<<8; p->contrast=qcam->contrast<<8; p->whiteness=qcam->whitebal<<8; p->depth=qcam->bpp; p->palette=VIDEO_PALETTE_GREY; return 0; } case VIDIOCSPICT: { struct video_picture *p = arg; if(p->palette!=VIDEO_PALETTE_GREY) return -EINVAL; if(p->depth!=4 && p->depth!=6) return -EINVAL; /* * Now load the camera. */ qcam->brightness = p->brightness>>8; qcam->contrast = p->contrast>>8; qcam->whitebal = p->whiteness>>8; qcam->bpp = p->depth; down(&qcam->lock); qc_setscanmode(qcam); up(&qcam->lock); qcam->status |= QC_PARAM_CHANGE; return 0; } case VIDIOCSWIN: { struct video_window *vw = arg; if(vw->flags) return -EINVAL; if(vw->clipcount) return -EINVAL; if(vw->height<60||vw->height>240) return -EINVAL; if(vw->width<80||vw->width>320) return -EINVAL; qcam->width = 320; qcam->height = 240; qcam->transfer_scale = 4; if(vw->width>=160 && vw->height>=120) { qcam->transfer_scale = 2; } if(vw->width>=320 && vw->height>=240) { qcam->width = 320; qcam->height = 240; qcam->transfer_scale = 1; } down(&qcam->lock); qc_setscanmode(qcam); up(&qcam->lock); /* We must update the camera before we grab. We could just have changed the grab size */ qcam->status |= QC_PARAM_CHANGE; /* Ok we figured out what to use from our wide choice */ return 0; } case VIDIOCGWIN: { struct video_window *vw = arg; memset(vw, 0, sizeof(*vw)); vw->width=qcam->width/qcam->transfer_scale; vw->height=qcam->height/qcam->transfer_scale; return 0; } case VIDIOCKEY: return 0; case VIDIOCCAPTURE: case VIDIOCGFBUF: case VIDIOCSFBUF: case VIDIOCGFREQ: case VIDIOCSFREQ: case VIDIOCGAUDIO: case VIDIOCSAUDIO: return -EINVAL; default: return -ENOIOCTLCMD; } return 0;}static int qcam_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ return video_usercopy(inode, file, cmd, arg, qcam_do_ioctl);}static ssize_t qcam_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){ struct video_device *v = video_devdata(file); struct qcam_device *qcam=(struct qcam_device *)v; int len; parport_claim_or_block(qcam->pdev); down(&qcam->lock); qc_reset(qcam); /* Update the camera parameters if we need to */ if (qcam->status & QC_PARAM_CHANGE) qc_set(qcam); len=qc_capture(qcam, buf,count); up(&qcam->lock); parport_release(qcam->pdev); return len;} static struct file_operations qcam_fops = { .owner = THIS_MODULE, .open = video_exclusive_open, .release = video_exclusive_release, .ioctl = qcam_ioctl, .read = qcam_read, .llseek = no_llseek,};static struct video_device qcam_template={ .owner = THIS_MODULE, .name = "Connectix Quickcam", .type = VID_TYPE_CAPTURE, .hardware = VID_HARDWARE_QCAM_BW, .fops = &qcam_fops,};#define MAX_CAMS 4static struct qcam_device *qcams[MAX_CAMS];static unsigned int num_cams = 0;static int init_bwqcam(struct parport *port){ struct qcam_device *qcam; if (num_cams == MAX_CAMS) { printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS); return -ENOSPC; } qcam=qcam_init(port); if(qcam==NULL) return -ENODEV; parport_claim_or_block(qcam->pdev); qc_reset(qcam); if(qc_detect(qcam)==0) { parport_release(qcam->pdev); parport_unregister_device(qcam->pdev); kfree(qcam); return -ENODEV; } qc_calibrate(qcam); parport_release(qcam->pdev); printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name); if(video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr)==-1) { parport_unregister_device(qcam->pdev); kfree(qcam); return -ENODEV; } qcams[num_cams++] = qcam; return 0;}static void close_bwqcam(struct qcam_device *qcam){ video_unregister_device(&qcam->vdev); parport_unregister_device(qcam->pdev); kfree(qcam);}/* The parport parameter controls which parports will be scanned. * Scanning all parports causes some printers to print a garbage page. * -- March 14, 1999 Billy Donahue <billy@escape.com> */#ifdef MODULEstatic char *parport[MAX_CAMS] = { NULL, };module_param_array(parport, charp, NULL, 0);#endifstatic int accept_bwqcam(struct parport *port){#ifdef MODULE int n; if (parport[0] && strncmp(parport[0], "auto", 4) != 0) { /* user gave parport parameters */ for(n=0; parport[n] && n<MAX_CAMS; n++){ char *ep; unsigned long r; r = simple_strtoul(parport[n], &ep, 0); if (ep == parport[n]) { printk(KERN_ERR "bw-qcam: bad port specifier \"%s\"\n", parport[n]); continue; } if (r == port->number) return 1; } return 0; }#endif return 1;}static void bwqcam_attach(struct parport *port){ if (accept_bwqcam(port)) init_bwqcam(port);}static void bwqcam_detach(struct parport *port){ int i; for (i = 0; i < num_cams; i++) { struct qcam_device *qcam = qcams[i]; if (qcam && qcam->pdev->port == port) { qcams[i] = NULL; close_bwqcam(qcam); } }}static struct parport_driver bwqcam_driver = { .name = "bw-qcam", .attach = bwqcam_attach, .detach = bwqcam_detach,};static void __exit exit_bw_qcams(void){ parport_unregister_driver(&bwqcam_driver);}static int __init init_bw_qcams(void){#ifdef MODULE /* Do some sanity checks on the module parameters. */ if (maxpoll > 5000) { printk("Connectix Quickcam max-poll was above 5000. Using 5000.\n"); maxpoll = 5000; } if (yieldlines < 1) { printk("Connectix Quickcam yieldlines was less than 1. Using 1.\n"); yieldlines = 1; }#endif return parport_register_driver(&bwqcam_driver);}module_init(init_bw_qcams);module_exit(exit_bw_qcams);MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -