?? ov511.c
字號:
/* * OmniVision OV511 Camera-to-USB Bridge Driver * * Copyright (c) 1999-2001 Mark W. McClelland * Original decompression code Copyright 1998-2000 OmniVision Technologies * Many improvements by Bret Wallach <bwallac1@san.rr.com> * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000) * Snapshot code by Kevin Moore * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org> * Changes by Claudio Matsuoka <claudio@conectiva.com> * Original SAA7111A code by Dave Perks <dperks@ibm.net> * Kernel I2C interface adapted from nt1003 driver * * Based on the Linux CPiA driver written by Peter Pregler, * Scott J. Bertin and Johannes Erdfelt. * * Please see the file: linux/Documentation/usb/ov511.txt * and the website at: http://alpha.dyndns.org/ov511 * for more info. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/config.h>#include <linux/version.h>#include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/vmalloc.h>#include <linux/slab.h>#include <linux/proc_fs.h>#include <linux/ctype.h>#include <linux/pagemap.h>#include <asm/io.h>#include <asm/semaphore.h>#include <asm/processor.h>#include <linux/wrapper.h>#if defined (__i386__) #include <asm/cpufeature.h>#endif#include "ov511.h"/* * Version Information */#define DRIVER_VERSION "v1.48a for Linux 2.4"#define EMAIL "mmcclell@bigfoot.com"#define DRIVER_AUTHOR "Mark McClelland <mmcclell@bigfoot.com> & Bret Wallach \ & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \ <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"#define DRIVER_DESC "OV511 USB Camera Driver"#define OV511_I2C_RETRIES 3#define ENABLE_Y_QUANTABLE 1#define ENABLE_UV_QUANTABLE 1/* Pixel count * 3 bytes for RGB */#define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3)#define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))/* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */#define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)#define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)/* PARAMETER VARIABLES: *//* (See ov511.txt for detailed descriptions of these.) *//* Sensor automatically changes brightness */static int autobright = 1;/* Sensor automatically changes gain */static int autogain = 1;/* Sensor automatically changes exposure */static int autoexp = 1;/* 0=no debug messages * 1=init/detection/unload and other significant messages, * 2=some warning messages * 3=config/control function calls * 4=most function calls and data parsing messages * 5=highly repetitive mesgs * NOTE: This should be changed to 0, 1, or 2 for production kernels */static int debug; /* = 0 *//* Fix vertical misalignment of red and blue at 640x480 */static int fix_rgb_offset; /* = 0 *//* Snapshot mode enabled flag */static int snapshot; /* = 0 *//* Force image to be read in RGB instead of BGR. This option allow * programs that expect RGB data (e.g. gqcam) to work with this driver. */static int force_rgb; /* = 0 *//* Number of seconds before inactive buffers are deallocated */static int buf_timeout = 5;/* Number of cameras to stream from simultaneously */static int cams = 1;/* Enable compression. Needs a fast (>300 MHz) CPU. */static int compress; /* = 0 *//* Display test pattern - doesn't work yet either */static int testpat; /* = 0 *//* Setting this to 1 will make the sensor output GBR422 instead of YUV420. Only * affects RGB24 mode. */static int sensor_gbr; /* = 0 *//* Dump raw pixel data. */static int dumppix; /* = 0 *//* LED policy. Only works on some OV511+ cameras. 0=off, 1=on (default), 2=auto * (on when open) */static int led = 1;/* Set this to 1 to dump the bridge register contents after initialization */static int dump_bridge; /* = 0 *//* Set this to 1 to dump the sensor register contents after initialization */static int dump_sensor; /* = 0 *//* Temporary option for debugging "works, but no image" problem. Prints the * first 12 bytes of data (potentially a packet header) in each isochronous * data frame. */static int printph; /* = 0 *//* Compression parameters - I'm not exactly sure what these do yet */static int phy = 0x1f;static int phuv = 0x05;static int pvy = 0x06;static int pvuv = 0x06;static int qhy = 0x14;static int qhuv = 0x03;static int qvy = 0x04;static int qvuv = 0x04;/* Light frequency. Set to 50 or 60 (Hz), or zero for default settings */static int lightfreq; /* = 0 *//* Set this to 1 to enable banding filter by default. Compensates for * alternating horizontal light/dark bands caused by (usually fluorescent) * lights */static int bandingfilter; /* = 0 *//* Pixel clock divisor */static int clockdiv = -1;/* Isoc packet size */static int packetsize = -1;/* Frame drop register (16h) */static int framedrop = -1;/* Allows picture settings (brightness, hue, etc...) to take effect immediately, * even in the middle of a frame. This reduces the time to change settings, but * can ruin frames during the change. Only affects OmniVision sensors. */static int fastset; /* = 0 *//* Forces the palette to a specific value. If an application requests a * different palette, it will be rejected. */static int force_palette; /* = 0 *//* Set tuner type, if not autodetected */static int tuner = -1;/* Allows proper exposure of objects that are illuminated from behind. Only * affects OmniVision sensors. */static int backlight; /* = 0 *//* If you change this, you must also change the MODULE_PARM definition */#define OV511_MAX_UNIT_VIDEO 16/* Allows specified minor numbers to be forced. They will be assigned in the * order that devices are detected. Note that you cannot specify 0 as a minor * number. If you do not specify any, the next available one will be used. This * requires kernel 2.4.5 or later. */static int unit_video[OV511_MAX_UNIT_VIDEO];/* Remove zero-padding from uncompressed incoming data. This will compensate for * the blocks of corruption that appear when the camera cannot keep up with the * speed of the USB bus (eg. at low frame resolutions) */static int remove_zeros; /* = 0 */MODULE_PARM(autobright, "i");MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");MODULE_PARM(autogain, "i");MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");MODULE_PARM(autoexp, "i");MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");MODULE_PARM(debug, "i");MODULE_PARM_DESC(debug, "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");MODULE_PARM(fix_rgb_offset, "i");MODULE_PARM_DESC(fix_rgb_offset, "Fix vertical misalignment of red and blue at 640x480");MODULE_PARM(snapshot, "i");MODULE_PARM_DESC(snapshot, "Enable snapshot mode");MODULE_PARM(force_rgb, "i");MODULE_PARM_DESC(force_rgb, "Read RGB instead of BGR");MODULE_PARM(buf_timeout, "i");MODULE_PARM_DESC(buf_timeout, "Number of seconds before buffer deallocation");MODULE_PARM(cams, "i");MODULE_PARM_DESC(cams, "Number of simultaneous cameras");MODULE_PARM(compress, "i");MODULE_PARM_DESC(compress, "Turn on compression (not reliable yet)");MODULE_PARM(testpat, "i");MODULE_PARM_DESC(testpat, "Replace image with vertical bar testpattern (only partially working)");// Temporarily removed (needs to be rewritten for new format conversion code)// MODULE_PARM(sensor_gbr, "i");// MODULE_PARM_DESC(sensor_gbr, "Make sensor output GBR422 rather than YUV420");MODULE_PARM(dumppix, "i");MODULE_PARM_DESC(dumppix, "Dump raw pixel data");MODULE_PARM(led, "i");MODULE_PARM_DESC(led, "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");MODULE_PARM(dump_bridge, "i");MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");MODULE_PARM(dump_sensor, "i");MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");MODULE_PARM(printph, "i");MODULE_PARM_DESC(printph, "Print frame start/end headers");MODULE_PARM(phy, "i");MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");MODULE_PARM(phuv, "i");MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");MODULE_PARM(pvy, "i");MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");MODULE_PARM(pvuv, "i");MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");MODULE_PARM(qhy, "i");MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");MODULE_PARM(qhuv, "i");MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");MODULE_PARM(qvy, "i");MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");MODULE_PARM(qvuv, "i");MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");MODULE_PARM(lightfreq, "i");MODULE_PARM_DESC(lightfreq, "Light frequency. Set to 50 or 60 Hz, or zero for default settings");MODULE_PARM(bandingfilter, "i");MODULE_PARM_DESC(bandingfilter, "Enable banding filter (to reduce effects of fluorescent lighting)");MODULE_PARM(clockdiv, "i");MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");MODULE_PARM(packetsize, "i");MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");MODULE_PARM(framedrop, "i");MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");MODULE_PARM(fastset, "i");MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");MODULE_PARM(force_palette, "i");MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");MODULE_PARM(tuner, "i");MODULE_PARM_DESC(tuner, "Set tuner type, if not autodetected");MODULE_PARM(backlight, "i");MODULE_PARM_DESC(backlight, "For objects that are lit from behind");MODULE_PARM(unit_video, "0-16i");MODULE_PARM_DESC(unit_video, "Force use of specific minor number(s). 0 is not allowed.");MODULE_PARM(remove_zeros, "i");MODULE_PARM_DESC(remove_zeros, "Remove zero-padding from uncompressed incoming data");MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);MODULE_LICENSE("GPL");static struct usb_driver ov511_driver;static struct ov51x_decomp_ops *ov511_decomp_ops;static struct ov51x_decomp_ops *ov511_mmx_decomp_ops;static struct ov51x_decomp_ops *ov518_decomp_ops;static struct ov51x_decomp_ops *ov518_mmx_decomp_ops;/* Number of times to retry a failed I2C transaction. Increase this if you * are getting "Failed to read sensor ID..." */static int i2c_detect_tries = 5;/* MMX support is present in kernel and CPU. Checked upon decomp module load. */static int ov51x_mmx_available;/* Function prototypes */static void ov51x_clear_snapshot(struct usb_ov511 *);static int ov51x_check_snapshot(struct usb_ov511 *);static inline int sensor_get_picture(struct usb_ov511 *, struct video_picture *);static int sensor_get_exposure(struct usb_ov511 *, unsigned char *);static int ov511_control_ioctl(struct inode *, struct file *, unsigned int, unsigned long);/********************************************************************** * List of known OV511-based cameras **********************************************************************/static struct cam_list clist[] = { { 0, "Generic Camera (no ID)" }, { 1, "Mustek WCam 3X" }, { 3, "D-Link DSB-C300" }, { 4, "Generic OV511/OV7610" }, { 5, "Puretek PT-6007" }, { 6, "Lifeview USB Life TV (NTSC)" }, { 21, "Creative Labs WebCam 3" }, { 36, "Koala-Cam" }, { 38, "Lifeview USB Life TV" }, { 41, "Samsung Anycam MPC-M10" }, { 43, "Mtekvision Zeca MV402" }, { 46, "Suma eON" }, { 100, "Lifeview RoboCam" }, { 102, "AverMedia InterCam Elite" }, { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */ { -1, NULL }};static __devinitdata struct usb_device_id device_table [] = { { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) }, { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) }, { USB_DEVICE(VEND_OMNIVISION, PROD_OV518) }, { USB_DEVICE(VEND_OMNIVISION, PROD_OV518PLUS) }, { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) }, { } /* Terminating entry */};MODULE_DEVICE_TABLE (usb, device_table);#if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)static struct palette_list plist[] = { { VIDEO_PALETTE_GREY, "GREY" }, { VIDEO_PALETTE_HI240, "HI240" }, { VIDEO_PALETTE_RGB565, "RGB565" }, { VIDEO_PALETTE_RGB24, "RGB24" }, { VIDEO_PALETTE_RGB32, "RGB32" }, { VIDEO_PALETTE_RGB555, "RGB555" }, { VIDEO_PALETTE_YUV422, "YUV422" }, { VIDEO_PALETTE_YUYV, "YUYV" }, { VIDEO_PALETTE_UYVY, "UYVY" }, { VIDEO_PALETTE_YUV420, "YUV420" }, { VIDEO_PALETTE_YUV411, "YUV411" }, { VIDEO_PALETTE_RAW, "RAW" }, { VIDEO_PALETTE_YUV422P,"YUV422P" }, { VIDEO_PALETTE_YUV411P,"YUV411P" }, { VIDEO_PALETTE_YUV420P,"YUV420P" }, { VIDEO_PALETTE_YUV410P,"YUV410P" }, { -1, NULL }};#endifstatic unsigned char yQuanTable511[] = OV511_YQUANTABLE;static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;static unsigned char yQuanTable518[] = OV518_YQUANTABLE;static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;/********************************************************************** * * Memory management * * This is a shameless copy from the USB-cpia driver (linux kernel * version 2.3.29 or so, I have no idea what this code actually does ;). * Actually it seems to be a copy of a shameless copy of the bttv-driver. * Or that is a copy of a shameless copy of ... (To the powers: is there * no generic kernel-function to do this sort of stuff?) * * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says * there will be one, but apparentely not yet -jerdfelt * * So I copied it again for the OV511 driver -claudio **********************************************************************//* Given PGD from the address space's page table, return the kernel * virtual mapping of the physical memory mapped at ADR. */static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr){ unsigned long ret = 0UL; pmd_t *pmd; pte_t *ptep, pte; if (!pgd_none(*pgd)) { pmd = pmd_offset(pgd, adr); if (!pmd_none(*pmd)) { ptep = pte_offset(pmd, adr); pte = *ptep; if (pte_present(pte)) { ret = (unsigned long) page_address(pte_page(pte)); ret |= (adr & (PAGE_SIZE - 1)); } } } return ret;}/* Here we want the physical address of the memory. * This is used when initializing the contents of the * area and marking the pages as reserved. */static inline unsigned long kvirt_to_pa(unsigned long adr){ unsigned long va, kva, ret; va = VMALLOC_VMADDR(adr); kva = uvirt_to_kva(pgd_offset_k(va), va); ret = __pa(kva); return ret;}static void *rvmalloc(unsigned long size){ void *mem; unsigned long adr, page; /* Round it off to PAGE_SIZE */ size += (PAGE_SIZE - 1); size &= ~(PAGE_SIZE - 1); mem = vmalloc_32(size); if (!mem) return NULL; memset(mem, 0, size); /* Clear the ram out, no junk to the user */ adr = (unsigned long) mem; while (size > 0) { page = kvirt_to_pa(adr); mem_map_reserve(virt_to_page(__va(page))); adr += PAGE_SIZE; if (size > PAGE_SIZE) size -= PAGE_SIZE; else size = 0; } return mem;}static void rvfree(void *mem, unsigned long size){ unsigned long adr, page; if (!mem) return; size += (PAGE_SIZE - 1); size &= ~(PAGE_SIZE - 1); adr=(unsigned long) mem; while (size > 0) { page = kvirt_to_pa(adr); mem_map_unreserve(virt_to_page(__va(page))); adr += PAGE_SIZE; if (size > PAGE_SIZE) size -= PAGE_SIZE; else size = 0; } vfree(mem);}/********************************************************************** * /proc interface * Based on the CPiA driver version 0.7.4 -claudio **********************************************************************/#if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)static struct proc_dir_entry *ov511_proc_entry = NULL;extern struct proc_dir_entry *video_proc_entry;static struct file_operations ov511_control_fops = { ioctl: ov511_control_ioctl,};#define YES_NO(x) ((x) ? "yes" : "no")/* /proc/video/ov511/<minor#>/info */static int ov511_read_proc_info(char *page, char **start, off_t off, int count, int *eof, void *data){ char *out = page; int i, j, len; struct usb_ov511 *ov511 = data;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -