blob: 9af5532db1423eb1d7971e5a00396a49c03f1959 [file] [log] [blame]
/*
* OmniVision OV511 Camera-to-USB Bridge Driver
*
* Copyright (c) 1999-2003 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>
* URB error messages from pwc driver by Nemosoft
* generic_ioctl() code from videodev.c by Gerd Knorr and Alan Cox
* Memory management (rvmalloc) code from bttv driver, by Gerd Knorr and others
*
* Based on the Linux CPiA driver written by Peter Pregler,
* Scott J. Bertin and Johannes Erdfelt.
*
* Please see the file: 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/module.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/pagemap.h>
#include <asm/processor.h>
#include <linux/mm.h>
#include <linux/device.h>
#if defined (__i386__)
#include <asm/cpufeature.h>
#endif
#include "ov511.h"
/*
* Version Information
*/
#define DRIVER_VERSION "v1.64 for Linux 2.5"
#define EMAIL "mark@alpha.dyndns.org"
#define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & 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
#define OV511_MAX_UNIT_VIDEO 16
/* Pixel count * bytes per YUV420 pixel (1.5) */
#define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
#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)
/**********************************************************************
* Module Parameters
* (See ov511.txt for detailed descriptions of these)
**********************************************************************/
/* These variables (and all static globals) default to zero */
static int autobright = 1;
static int autogain = 1;
static int autoexp = 1;
static int debug;
static int snapshot;
static int cams = 1;
static int compress;
static int testpat;
static int dumppix;
static int led = 1;
static int dump_bridge;
static int dump_sensor;
static int printph;
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;
static int lightfreq;
static int bandingfilter;
static int clockdiv = -1;
static int packetsize = -1;
static int framedrop = -1;
static int fastset;
static int force_palette;
static int backlight;
static int unit_video[OV511_MAX_UNIT_VIDEO];
static int remove_zeros;
static int mirror;
static int ov518_color;
module_param(autobright, int, 0);
MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
module_param(autogain, int, 0);
MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
module_param(autoexp, int, 0);
MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
module_param(debug, int, 0);
MODULE_PARM_DESC(debug,
"Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
module_param(snapshot, int, 0);
MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
module_param(cams, int, 0);
MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
module_param(compress, int, 0);
MODULE_PARM_DESC(compress, "Turn on compression");
module_param(testpat, int, 0);
MODULE_PARM_DESC(testpat,
"Replace image with vertical bar testpattern (only partially working)");
module_param(dumppix, int, 0);
MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
module_param(led, int, 0);
MODULE_PARM_DESC(led,
"LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
module_param(dump_bridge, int, 0);
MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
module_param(dump_sensor, int, 0);
MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
module_param(printph, int, 0);
MODULE_PARM_DESC(printph, "Print frame start/end headers");
module_param(phy, int, 0);
MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
module_param(phuv, int, 0);
MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
module_param(pvy, int, 0);
MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
module_param(pvuv, int, 0);
MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
module_param(qhy, int, 0);
MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
module_param(qhuv, int, 0);
MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
module_param(qvy, int, 0);
MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
module_param(qvuv, int, 0);
MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
module_param(lightfreq, int, 0);
MODULE_PARM_DESC(lightfreq,
"Light frequency. Set to 50 or 60 Hz, or zero for default settings");
module_param(bandingfilter, int, 0);
MODULE_PARM_DESC(bandingfilter,
"Enable banding filter (to reduce effects of fluorescent lighting)");
module_param(clockdiv, int, 0);
MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
module_param(packetsize, int, 0);
MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
module_param(framedrop, int, 0);
MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
module_param(fastset, int, 0);
MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
module_param(force_palette, int, 0);
MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
module_param(backlight, int, 0);
MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
static unsigned int num_uv;
module_param_array(unit_video, int, &num_uv, 0);
MODULE_PARM_DESC(unit_video,
"Force use of specific minor number(s). 0 is not allowed.");
module_param(remove_zeros, int, 0);
MODULE_PARM_DESC(remove_zeros,
"Remove zero-padding from uncompressed incoming data");
module_param(mirror, int, 0);
MODULE_PARM_DESC(mirror, "Reverse image horizontally");
module_param(ov518_color, int, 0);
MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
/**********************************************************************
* Miscellaneous Globals
**********************************************************************/
static struct usb_driver ov511_driver;
/* Number of times to retry a failed I2C transaction. Increase this if you
* are getting "Failed to read sensor ID..." */
static const int i2c_detect_tries = 5;
static 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);
static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
/**********************************************************************
* Symbolic Names
**********************************************************************/
/* Known OV511-based cameras */
static struct symbolic_list camlist[] = {
{ 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" },
{ 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
{ 36, "Koala-Cam" },
{ 38, "Lifeview USB Life TV (PAL)" },
{ 41, "Samsung Anycam MPC-M10" },
{ 43, "Mtekvision Zeca MV402" },
{ 46, "Suma eON" },
{ 70, "Lifeview USB Life TV (PAL/SECAM)" },
{ 100, "Lifeview RoboCam" },
{ 102, "AverMedia InterCam Elite" },
{ 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
{ 134, "Ezonics EZCam II" },
{ 192, "Webeye 2000B" },
{ 253, "Alpha Vision Tech. AlphaCam SE" },
{ -1, NULL }
};
/* Video4Linux1 Palettes */
static struct symbolic_list v4l1_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 }
};
static struct symbolic_list brglist[] = {
{ BRG_OV511, "OV511" },
{ BRG_OV511PLUS, "OV511+" },
{ BRG_OV518, "OV518" },
{ BRG_OV518PLUS, "OV518+" },
{ -1, NULL }
};
static struct symbolic_list senlist[] = {
{ SEN_OV76BE, "OV76BE" },
{ SEN_OV7610, "OV7610" },
{ SEN_OV7620, "OV7620" },
{ SEN_OV7620AE, "OV7620AE" },
{ SEN_OV6620, "OV6620" },
{ SEN_OV6630, "OV6630" },
{ SEN_OV6630AE, "OV6630AE" },
{ SEN_OV6630AF, "OV6630AF" },
{ SEN_OV8600, "OV8600" },
{ SEN_KS0127, "KS0127" },
{ SEN_KS0127B, "KS0127B" },
{ SEN_SAA7111A, "SAA7111A" },
{ -1, NULL }
};
/* URB error codes: */
static struct symbolic_list urb_errlist[] = {
{ -ENOSR, "Buffer error (overrun)" },
{ -EPIPE, "Stalled (device not responding)" },
{ -EOVERFLOW, "Babble (device sends too much data)" },
{ -EPROTO, "Bit-stuff error (bad cable?)" },
{ -EILSEQ, "CRC/Timeout (bad cable?)" },
{ -ETIME, "Device does not respond to token" },
{ -ETIMEDOUT, "Device does not respond to command" },
{ -1, NULL }
};
/**********************************************************************
* Memory management
**********************************************************************/
static void *
rvmalloc(unsigned long size)
{
void *mem;
unsigned long adr;
size = PAGE_ALIGN(size);
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) {
SetPageReserved(vmalloc_to_page((void *)adr));
adr += PAGE_SIZE;
size -= PAGE_SIZE;
}
return mem;
}
static void
rvfree(void *mem, unsigned long size)
{
unsigned long adr;
if (!mem)
return;
adr = (unsigned long) mem;
while ((long) size > 0) {
ClearPageReserved(vmalloc_to_page((void *)adr));
adr += PAGE_SIZE;
size -= PAGE_SIZE;
}
vfree(mem);
}
/**********************************************************************
*
* Register I/O
*
**********************************************************************/
/* Write an OV51x register */
static int
reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
{
int rc;
PDEBUG(5, "0x%02X:0x%02X", reg, value);
mutex_lock(&ov->cbuf_lock);
ov->cbuf[0] = value;
rc = usb_control_msg(ov->dev,
usb_sndctrlpipe(ov->dev, 0),
(ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0, (__u16)reg, &ov->cbuf[0], 1, 1000);
mutex_unlock(&ov->cbuf_lock);
if (rc < 0)
err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
return rc;
}
/* Read from an OV51x register */
/* returns: negative is error, pos or zero is data */
static int
reg_r(struct usb_ov511 *ov, unsigned char reg)
{
int rc;
mutex_lock(&ov->cbuf_lock);
rc = usb_control_msg(ov->dev,
usb_rcvctrlpipe(ov->dev, 0),
(ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0, (__u16)reg, &ov->cbuf[0], 1, 1000);
if (rc < 0) {
err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
} else {
rc = ov->cbuf[0];
PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
}
mutex_unlock(&ov->cbuf_lock);
return rc;
}
/*
* Writes bits at positions specified by mask to an OV51x reg. Bits that are in
* the same position as 1's in "mask" are cleared and set to "value". Bits
* that are in the same position as 0's in "mask" are preserved, regardless
* of their respective state in "value".
*/
static int
reg_w_mask(struct usb_ov511 *ov,
unsigned char reg,
unsigned char value,
unsigned char mask)
{
int ret;
unsigned char oldval, newval;
ret = reg_r(ov, reg);
if (ret < 0)
return ret;
oldval = (unsigned char) ret;
oldval &= (~mask); /* Clear the masked bits */
value &= mask; /* Enforce mask on value */
newval = oldval | value; /* Set the desired bits */
return (reg_w(ov, reg, newval));
}
/*
* Writes multiple (n) byte value to a single register. Only valid with certain
* registers (0x30 and 0xc4 - 0xce).
*/
static int
ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
{
int rc;
PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
mutex_lock(&ov->cbuf_lock);
*((__le32 *)ov->cbuf) = __cpu_to_le32(val);
rc = usb_control_msg(ov->dev,
usb_sndctrlpipe(ov->dev, 0),
1 /* REG_IO */,
USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0, (__u16)reg, ov->cbuf, n, 1000);
mutex_unlock(&ov->cbuf_lock);
if (rc < 0)
err("reg write multiple: error %d: %s", rc,
symbolic(urb_errlist, rc));
return rc;
}
static int
ov511_upload_quan_tables(struct usb_ov511 *ov)
{
unsigned char *pYTable = yQuanTable511;
unsigned char *pUVTable = uvQuanTable511;
unsigned char val0, val1;
int i, rc, reg = R511_COMP_LUT_BEGIN;
PDEBUG(4, "Uploading quantization tables");
for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
if (ENABLE_Y_QUANTABLE) {
val0 = *pYTable++;
val1 = *pYTable++;
val0 &= 0x0f;
val1 &= 0x0f;
val0 |= val1 << 4;
rc = reg_w(ov, reg, val0);
if (rc < 0)
return rc;
}
if (ENABLE_UV_QUANTABLE) {
val0 = *pUVTable++;
val1 = *pUVTable++;
val0 &= 0x0f;
val1 &= 0x0f;
val0 |= val1 << 4;
rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
if (rc < 0)
return rc;
}
reg++;
}
return 0;
}
/* OV518 quantization tables are 8x4 (instead of 8x8) */
static int
ov518_upload_quan_tables(struct usb_ov511 *ov)
{
unsigned char *pYTable = yQuanTable518;
unsigned char *pUVTable = uvQuanTable518;
unsigned char val0, val1;
int i, rc, reg = R511_COMP_LUT_BEGIN;
PDEBUG(4, "Uploading quantization tables");
for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
if (ENABLE_Y_QUANTABLE) {
val0 = *pYTable++;
val1 = *pYTable++;
val0 &= 0x0f;
val1 &= 0x0f;
val0 |= val1 << 4;
rc = reg_w(ov, reg, val0);
if (rc < 0)
return rc;
}
if (ENABLE_UV_QUANTABLE) {
val0 = *pUVTable++;
val1 = *pUVTable++;
val0 &= 0x0f;
val1 &= 0x0f;
val0 |= val1 << 4;
rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
if (rc < 0)
return rc;
}
reg++;
}
return 0;
}
static int
ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
{
int rc;
/* Setting bit 0 not allowed on 518/518Plus */
if (ov->bclass == BCL_OV518)
reset_type &= 0xfe;
PDEBUG(4, "Reset: type=0x%02X", reset_type);
rc = reg_w(ov, R51x_SYS_RESET, reset_type);
rc = reg_w(ov, R51x_SYS_RESET, 0);
if (rc < 0)
err("reset: command failed");
return rc;
}
/**********************************************************************
*
* Low-level I2C I/O functions
*
**********************************************************************/
/* NOTE: Do not call this function directly!
* The OV518 I2C I/O procedure is different, hence, this function.
* This is normally only called from i2c_w(). Note that this function
* always succeeds regardless of whether the sensor is present and working.
*/
static int
ov518_i2c_write_internal(struct usb_ov511 *ov,
unsigned char reg,
unsigned char value)
{
int rc;
PDEBUG(5, "0x%02X:0x%02X", reg, value);
/* Select camera register */
rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
if (rc < 0)
return rc;
/* Write "value" to I2C data port of OV511 */
rc = reg_w(ov, R51x_I2C_DATA, value);
if (rc < 0)
return rc;
/* Initiate 3-byte write cycle */
rc = reg_w(ov, R518_I2C_CTL, 0x01);
if (rc < 0)
return rc;
return 0;
}
/* NOTE: Do not call this function directly! */
static int
ov511_i2c_write_internal(struct usb_ov511 *ov,
unsigned char reg,
unsigned char value)
{
int rc, retries;
PDEBUG(5, "0x%02X:0x%02X", reg, value);
/* Three byte write cycle */
for (retries = OV511_I2C_RETRIES; ; ) {
/* Select camera register */
rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
if (rc < 0)
break;
/* Write "value" to I2C data port of OV511 */
rc = reg_w(ov, R51x_I2C_DATA, value);
if (rc < 0)
break;
/* Initiate 3-byte write cycle */
rc = reg_w(ov, R511_I2C_CTL, 0x01);
if (rc < 0)
break;
/* Retry until idle */
do {
rc = reg_r(ov, R511_I2C_CTL);
} while (rc > 0 && ((rc&1) == 0));
if (rc < 0)
break;
/* Ack? */
if ((rc&2) == 0) {
rc = 0;
break;
}
#if 0
/* I2C abort */
reg_w(ov, R511_I2C_CTL, 0x10);
#endif
if (--retries < 0) {
err("i2c write retries exhausted");
rc = -1;
break;
}
}
return rc;
}
/* NOTE: Do not call this function directly!
* The OV518 I2C I/O procedure is different, hence, this function.
* This is normally only called from i2c_r(). Note that this function
* always succeeds regardless of whether the sensor is present and working.
*/
static int
ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
{
int rc, value;
/* Select camera register */
rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
if (rc < 0)
return rc;
/* Initiate 2-byte write cycle */
rc = reg_w(ov, R518_I2C_CTL, 0x03);
if (rc < 0)
return rc;
/* Initiate 2-byte read cycle */
rc = reg_w(ov, R518_I2C_CTL, 0x05);
if (rc < 0)
return rc;
value = reg_r(ov, R51x_I2C_DATA);
PDEBUG(5, "0x%02X:0x%02X", reg, value);
return value;
}
/* NOTE: Do not call this function directly!
* returns: negative is error, pos or zero is data */
static int
ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
{
int rc, value, retries;
/* Two byte write cycle */
for (retries = OV511_I2C_RETRIES; ; ) {
/* Select camera register */
rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
if (rc < 0)
return rc;
/* Initiate 2-byte write cycle */
rc = reg_w(ov, R511_I2C_CTL, 0x03);
if (rc < 0)
return rc;
/* Retry until idle */
do {
rc = reg_r(ov, R511_I2C_CTL);
} while (rc > 0 && ((rc & 1) == 0));
if (rc < 0)
return rc;
if ((rc&2) == 0) /* Ack? */
break;
/* I2C abort */
reg_w(ov, R511_I2C_CTL, 0x10);
if (--retries < 0) {
err("i2c write retries exhausted");
return -1;
}
}
/* Two byte read cycle */
for (retries = OV511_I2C_RETRIES; ; ) {
/* Initiate 2-byte read cycle */
rc = reg_w(ov, R511_I2C_CTL, 0x05);
if (rc < 0)
return rc;
/* Retry until idle */
do {
rc = reg_r(ov, R511_I2C_CTL);
} while (rc > 0 && ((rc&1) == 0));
if (rc < 0)
return rc;
if ((rc&2) == 0) /* Ack? */
break;
/* I2C abort */
rc = reg_w(ov, R511_I2C_CTL, 0x10);
if (rc < 0)
return rc;
if (--retries < 0) {
err("i2c read retries exhausted");
return -1;
}
}
value = reg_r(ov, R51x_I2C_DATA);
PDEBUG(5, "0x%02X:0x%02X", reg, value);
/* This is needed to make i2c_w() work */
rc = reg_w(ov, R511_I2C_CTL, 0x05);
if (rc < 0)
return rc;
return value;
}
/* returns: negative is error, pos or zero is data */
static int
i2c_r(struct usb_ov511 *ov, unsigned char reg)
{
int rc;
mutex_lock(&ov->i2c_lock);
if (ov->bclass == BCL_OV518)
rc = ov518_i2c_read_internal(ov, reg);
else
rc = ov511_i2c_read_internal(ov, reg);
mutex_unlock(&ov->i2c_lock);
return rc;
}
static int
i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
{
int rc;
mutex_lock(&ov->i2c_lock);
if (ov->bclass == BCL_OV518)
rc = ov518_i2c_write_internal(ov, reg, value);
else
rc = ov511_i2c_write_internal(ov, reg, value);
mutex_unlock(&ov->i2c_lock);
return rc;
}
/* Do not call this function directly! */
static int
ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
unsigned char reg,
unsigned char value,
unsigned char mask)
{
int rc;
unsigned char oldval, newval;
if (mask == 0xff) {
newval = value;
} else {
if (ov->bclass == BCL_OV518)
rc = ov518_i2c_read_internal(ov, reg);
else
rc = ov511_i2c_read_internal(ov, reg);
if (rc < 0)
return rc;
oldval = (unsigned char) rc;
oldval &= (~mask); /* Clear the masked bits */
value &= mask; /* Enforce mask on value */
newval = oldval | value; /* Set the desired bits */
}
if (ov->bclass == BCL_OV518)
return (ov518_i2c_write_internal(ov, reg, newval));
else
return (ov511_i2c_write_internal(ov, reg, newval));
}
/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
* the same position as 1's in "mask" are cleared and set to "value". Bits
* that are in the same position as 0's in "mask" are preserved, regardless
* of their respective state in "value".
*/
static int
i2c_w_mask(struct usb_ov511 *ov,
unsigned char reg,
unsigned char value,
unsigned char mask)
{
int rc;
mutex_lock(&ov->i2c_lock);
rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
mutex_unlock(&ov->i2c_lock);
return rc;
}
/* Set the read and write slave IDs. The "slave" argument is the write slave,
* and the read slave will be set to (slave + 1). ov->i2c_lock should be held
* when calling this. This should not be called from outside the i2c I/O
* functions.
*/
static int
i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
{
int rc;
rc = reg_w(ov, R51x_I2C_W_SID, slave);
if (rc < 0)
return rc;
rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
if (rc < 0)
return rc;
return 0;
}
/* Write to a specific I2C slave ID and register, using the specified mask */
static int
i2c_w_slave(struct usb_ov511 *ov,
unsigned char slave,
unsigned char reg,
unsigned char value,
unsigned char mask)
{
int rc = 0;
mutex_lock(&ov->i2c_lock);
/* Set new slave IDs */
rc = i2c_set_slave_internal(ov, slave);
if (rc < 0)
goto out;
rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
out:
/* Restore primary IDs */
if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
err("Couldn't restore primary I2C slave");
mutex_unlock(&ov->i2c_lock);
return rc;
}
/* Read from a specific I2C slave ID and register */
static int
i2c_r_slave(struct usb_ov511 *ov,
unsigned char slave,
unsigned char reg)
{
int rc;
mutex_lock(&ov->i2c_lock);
/* Set new slave IDs */
rc = i2c_set_slave_internal(ov, slave);
if (rc < 0)
goto out;
if (ov->bclass == BCL_OV518)
rc = ov518_i2c_read_internal(ov, reg);
else
rc = ov511_i2c_read_internal(ov, reg);
out:
/* Restore primary IDs */
if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
err("Couldn't restore primary I2C slave");
mutex_unlock(&ov->i2c_lock);
return rc;
}
/* Sets I2C read and write slave IDs. Returns <0 for error */
static int
ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
{
int rc;
mutex_lock(&ov->i2c_lock);
rc = i2c_set_slave_internal(ov, sid);
if (rc < 0)
goto out;
// FIXME: Is this actually necessary?
rc = ov51x_reset(ov, OV511_RESET_NOREGS);
out:
mutex_unlock(&ov->i2c_lock);
return rc;
}
static int
write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
{
int rc;
while (pRegvals->bus != OV511_DONE_BUS) {
if (pRegvals->bus == OV511_REG_BUS) {
if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
return rc;
} else if (pRegvals->bus == OV511_I2C_BUS) {
if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
return rc;
} else {
err("Bad regval array");
return -1;
}
pRegvals++;
}
return 0;
}
#ifdef OV511_DEBUG
static void
dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
{
int i, rc;
for (i = reg1; i <= regn; i++) {
rc = i2c_r(ov, i);
dev_info(&ov->dev->dev, "Sensor[0x%02X] = 0x%02X\n", i, rc);
}
}
static void
dump_i2c_regs(struct usb_ov511 *ov)
{
dev_info(&ov->dev->dev, "I2C REGS\n");
dump_i2c_range(ov, 0x00, 0x7C);
}
static void
dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
{
int i, rc;
for (i = reg1; i <= regn; i++) {
rc = reg_r(ov, i);
dev_info(&ov->dev->dev, "OV511[0x%02X] = 0x%02X\n", i, rc);
}
}
static void
ov511_dump_regs(struct usb_ov511 *ov)
{
dev_info(&ov->dev->dev, "CAMERA INTERFACE REGS\n");
dump_reg_range(ov, 0x10, 0x1f);
dev_info(&ov->dev->dev, "DRAM INTERFACE REGS\n");
dump_reg_range(ov, 0x20, 0x23);
dev_info(&ov->dev->dev, "ISO FIFO REGS\n");
dump_reg_range(ov, 0x30, 0x31);
dev_info(&ov->dev->dev, "PIO REGS\n");
dump_reg_range(ov, 0x38, 0x39);
dump_reg_range(ov, 0x3e, 0x3e);
dev_info(&ov->dev->dev, "I2C REGS\n");
dump_reg_range(ov, 0x40, 0x49);
dev_info(&ov->dev->dev, "SYSTEM CONTROL REGS\n");
dump_reg_range(ov, 0x50, 0x55);
dump_reg_range(ov, 0x5e, 0x5f);
dev_info(&ov->dev->dev, "OmniCE REGS\n");
dump_reg_range(ov, 0x70, 0x79);
/* NOTE: Quantization tables are not readable. You will get the value
* in reg. 0x79 for every table register */
dump_reg_range(ov, 0x80, 0x9f);
dump_reg_range(ov, 0xa0, 0xbf);
}
static void
ov518_dump_regs(struct usb_ov511 *ov)
{
dev_info(&ov->dev->dev, "VIDEO MODE REGS\n");
dump_reg_range(ov, 0x20, 0x2f);
dev_info(&ov->dev->dev, "DATA PUMP AND SNAPSHOT REGS\n");
dump_reg_range(ov, 0x30, 0x3f);
dev_info(&ov->dev->dev, "I2C REGS\n");
dump_reg_range(ov, 0x40, 0x4f);
dev_info(&ov->dev->dev, "SYSTEM CONTROL AND VENDOR REGS\n");
dump_reg_range(ov, 0x50, 0x5f);
dev_info(&ov->dev->dev, "60 - 6F\n");
dump_reg_range(ov, 0x60, 0x6f);
dev_info(&ov->dev->dev, "70 - 7F\n");
dump_reg_range(ov, 0x70, 0x7f);
dev_info(&ov->dev->dev, "Y QUANTIZATION TABLE\n");
dump_reg_range(ov, 0x80, 0x8f);
dev_info(&ov->dev->dev, "UV QUANTIZATION TABLE\n");
dump_reg_range(ov, 0x90, 0x9f);
dev_info(&ov->dev->dev, "A0 - BF\n");
dump_reg_range(ov, 0xa0, 0xbf);
dev_info(&ov->dev->dev, "CBR\n");
dump_reg_range(ov, 0xc0, 0xcf);
}
#endif
/*****************************************************************************/
/* Temporarily stops OV511 from functioning. Must do this before changing
* registers while the camera is streaming */
static inline int
ov51x_stop(struct usb_ov511 *ov)
{
PDEBUG(4, "stopping");
ov->stopped = 1;
if (ov->bclass == BCL_OV518)
return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
else
return (reg_w(ov, R51x_SYS_RESET, 0x3d));
}
/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
* actually stopped (for performance). */
static inline int
ov51x_restart(struct usb_ov511 *ov)
{
if (ov->stopped) {
PDEBUG(4, "restarting");
ov->stopped = 0;
/* Reinitialize the stream */
if (ov->bclass == BCL_OV518)
reg_w(ov, 0x2f, 0x80);
return (reg_w(ov, R51x_SYS_RESET, 0x00));
}
return 0;
}
/* Sleeps until no frames are active. Returns !0 if got signal */
static int
ov51x_wait_frames_inactive(struct usb_ov511 *ov)
{
return wait_event_interruptible(ov->wq, ov->curframe < 0);
}
/* Resets the hardware snapshot button */
static void
ov51x_clear_snapshot(struct usb_ov511 *ov)
{
if (ov->bclass == BCL_OV511) {
reg_w(ov, R51x_SYS_SNAP, 0x00);
reg_w(ov, R51x_SYS_SNAP, 0x02);
reg_w(ov, R51x_SYS_SNAP, 0x00);
} else if (ov->bclass == BCL_OV518) {
dev_warn(&ov->dev->dev,
"snapshot reset not supported yet on OV518(+)\n");
} else {
dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
}
}
#if 0
/* Checks the status of the snapshot button. Returns 1 if it was pressed since
* it was last cleared, and zero in all other cases (including errors) */
static int
ov51x_check_snapshot(struct usb_ov511 *ov)
{
int ret, status = 0;
if (ov->bclass == BCL_OV511) {
ret = reg_r(ov, R51x_SYS_SNAP);
if (ret < 0) {
dev_err(&ov->dev->dev,
"Error checking snspshot status (%d)\n", ret);
} else if (ret & 0x08) {
status = 1;
}
} else if (ov->bclass == BCL_OV518) {
dev_warn(&ov->dev->dev,
"snapshot check not supported yet on OV518(+)\n");
} else {
dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
}
return status;
}
#endif
/* This does an initial reset of an OmniVision sensor and ensures that I2C
* is synchronized. Returns <0 for failure.
*/
static int
init_ov_sensor(struct usb_ov511 *ov)
{
int i, success;
/* Reset the sensor */
if (i2c_w(ov, 0x12, 0x80) < 0)
return -EIO;
/* Wait for it to initialize */
msleep(150);
for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
(i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
success = 1;
continue;
}
/* Reset the sensor */
if (i2c_w(ov, 0x12, 0x80) < 0)
return -EIO;
/* Wait for it to initialize */
msleep(150);
/* Dummy read to sync I2C */
if (i2c_r(ov, 0x00) < 0)
return -EIO;
}
if (!success)
return -EIO;
PDEBUG(1, "I2C synced in %d attempt(s)", i);
return 0;
}
static int
ov511_set_packet_size(struct usb_ov511 *ov, int size)
{
int alt, mult;
if (ov51x_stop(ov) < 0)
return -EIO;
mult = size >> 5;
if (ov->bridge == BRG_OV511) {
if (size == 0)
alt = OV511_ALT_SIZE_0;
else if (size == 257)
alt = OV511_ALT_SIZE_257;
else if (size == 513)
alt = OV511_ALT_SIZE_513;
else if (size == 769)
alt = OV511_ALT_SIZE_769;
else if (size == 993)
alt = OV511_ALT_SIZE_993;
else {
err("Set packet size: invalid size (%d)", size);
return -EINVAL;
}
} else if (ov->bridge == BRG_OV511PLUS) {
if (size == 0)
alt = OV511PLUS_ALT_SIZE_0;
else if (size == 33)
alt = OV511PLUS_ALT_SIZE_33;
else if (size == 129)
alt = OV511PLUS_ALT_SIZE_129;
else if (size == 257)
alt = OV511PLUS_ALT_SIZE_257;
else if (size == 385)
alt = OV511PLUS_ALT_SIZE_385;
else if (size == 513)
alt = OV511PLUS_ALT_SIZE_513;
else if (size == 769)
alt = OV511PLUS_ALT_SIZE_769;
else if (size == 961)
alt = OV511PLUS_ALT_SIZE_961;
else {
err("Set packet size: invalid size (%d)", size);
return -EINVAL;
}
} else {
err("Set packet size: Invalid bridge type");
return -EINVAL;
}
PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
return -EIO;
if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
err("Set packet size: set interface error");
return -EBUSY;
}
if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
return -EIO;
ov->packet_size = size;
if (ov51x_restart(ov) < 0)
return -EIO;
return 0;
}
/* Note: Unlike the OV511/OV511+, the size argument does NOT include the
* optional packet number byte. The actual size *is* stored in ov->packet_size,
* though. */
static int
ov518_set_packet_size(struct usb_ov511 *ov, int size)
{
int alt;
if (ov51x_stop(ov) < 0)
return -EIO;
if (ov->bclass == BCL_OV518) {
if (size == 0)
alt = OV518_ALT_SIZE_0;
else if (size == 128)
alt = OV518_ALT_SIZE_128;
else if (size == 256)
alt = OV518_ALT_SIZE_256;
else if (size == 384)
alt = OV518_ALT_SIZE_384;
else if (size == 512)
alt = OV518_ALT_SIZE_512;
else if (size == 640)
alt = OV518_ALT_SIZE_640;
else if (size == 768)
alt = OV518_ALT_SIZE_768;
else if (size == 896)
alt = OV518_ALT_SIZE_896;
else {
err("Set packet size: invalid size (%d)", size);
return -EINVAL;
}
} else {
err("Set packet size: Invalid bridge type");
return -EINVAL;
}
PDEBUG(3, "%d, alt=%d", size, alt);
ov->packet_size = size;
if (size > 0) {
/* Program ISO FIFO size reg (packet number isn't included) */
ov518_reg_w32(ov, 0x30, size, 2);
if (ov->packet_numbering)
++ov->packet_size;
}
if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
err("Set packet size: set interface error");
return -EBUSY;
}
/* Initialize the stream */
if (reg_w(ov, 0x2f, 0x80) < 0)
return -EIO;
if (ov51x_restart(ov) < 0)
return -EIO;
if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
return -EIO;
return 0;
}
/* Upload compression params and quantization tables. Returns 0 for success. */
static int
ov511_init_compression(struct usb_ov511 *ov)
{
int rc = 0;
if (!ov->compress_inited) {
reg_w(ov, 0x70, phy);
reg_w(ov, 0x71, phuv);
reg_w(ov, 0x72, pvy);
reg_w(ov, 0x73, pvuv);
reg_w(ov, 0x74, qhy);
reg_w(ov, 0x75, qhuv);
reg_w(ov, 0x76, qvy);
reg_w(ov, 0x77, qvuv);
if (ov511_upload_quan_tables(ov) < 0) {
err("Error uploading quantization tables");
rc = -EIO;
goto out;
}
}
ov->compress_inited = 1;
out:
return rc;
}
/* Upload compression params and quantization tables. Returns 0 for success. */
static int
ov518_init_compression(struct usb_ov511 *ov)
{
int rc = 0;
if (!ov->compress_inited) {
if (ov518_upload_quan_tables(ov) < 0) {
err("Error uploading quantization tables");
rc = -EIO;
goto out;
}
}
ov->compress_inited = 1;
out:
return rc;
}
/* -------------------------------------------------------------------------- */
/* Sets sensor's contrast setting to "val" */
static int
sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
{
int rc;
PDEBUG(3, "%d", val);
if (ov->stop_during_set)
if (ov51x_stop(ov) < 0)
return -EIO;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV6620:
{
rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
if (rc < 0)
goto out;
break;
}
case SEN_OV6630:
{
rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
if (rc < 0)
goto out;
break;
}
case SEN_OV7620:
{
unsigned char ctab[] = {
0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
};
/* Use Y gamma control instead. Bit 0 enables it. */
rc = i2c_w(ov, 0x64, ctab[val>>12]);
if (rc < 0)
goto out;
break;
}
case SEN_SAA7111A:
{
rc = i2c_w(ov, 0x0b, val >> 9);
if (rc < 0)
goto out;
break;
}
default:
{
PDEBUG(3, "Unsupported with this sensor");
rc = -EPERM;
goto out;
}
}
rc = 0; /* Success */
ov->contrast = val;
out:
if (ov51x_restart(ov) < 0)
return -EIO;
return rc;
}
/* Gets sensor's contrast setting */
static int
sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
{
int rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV6620:
rc = i2c_r(ov, OV7610_REG_CNT);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_OV6630:
rc = i2c_r(ov, OV7610_REG_CNT);
if (rc < 0)
return rc;
else
*val = rc << 12;
break;
case SEN_OV7620:
/* Use Y gamma reg instead. Bit 0 is the enable bit. */
rc = i2c_r(ov, 0x64);
if (rc < 0)
return rc;
else
*val = (rc & 0xfe) << 8;
break;
case SEN_SAA7111A:
*val = ov->contrast;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
}
PDEBUG(3, "%d", *val);
ov->contrast = *val;
return 0;
}
/* -------------------------------------------------------------------------- */
/* Sets sensor's brightness setting to "val" */
static int
sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
{
int rc;
PDEBUG(4, "%d", val);
if (ov->stop_during_set)
if (ov51x_stop(ov) < 0)
return -EIO;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV76BE:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
if (rc < 0)
goto out;
break;
case SEN_OV7620:
/* 7620 doesn't like manual changes when in auto mode */
if (!ov->auto_brt) {
rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
if (rc < 0)
goto out;
}
break;
case SEN_SAA7111A:
rc = i2c_w(ov, 0x0a, val >> 8);
if (rc < 0)
goto out;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
rc = -EPERM;
goto out;
}
rc = 0; /* Success */
ov->brightness = val;
out:
if (ov51x_restart(ov) < 0)
return -EIO;
return rc;
}
/* Gets sensor's brightness setting */
static int
sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
{
int rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV76BE:
case SEN_OV7620:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_r(ov, OV7610_REG_BRT);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_SAA7111A:
*val = ov->brightness;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
}
PDEBUG(3, "%d", *val);
ov->brightness = *val;
return 0;
}
/* -------------------------------------------------------------------------- */
/* Sets sensor's saturation (color intensity) setting to "val" */
static int
sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
{
int rc;
PDEBUG(3, "%d", val);
if (ov->stop_during_set)
if (ov51x_stop(ov) < 0)
return -EIO;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV76BE:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
if (rc < 0)
goto out;
break;
case SEN_OV7620:
// /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
// rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
// if (rc < 0)
// goto out;
rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
if (rc < 0)
goto out;
break;
case SEN_SAA7111A:
rc = i2c_w(ov, 0x0c, val >> 9);
if (rc < 0)
goto out;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
rc = -EPERM;
goto out;
}
rc = 0; /* Success */
ov->colour = val;
out:
if (ov51x_restart(ov) < 0)
return -EIO;
return rc;
}
/* Gets sensor's saturation (color intensity) setting */
static int
sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
{
int rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV76BE:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_r(ov, OV7610_REG_SAT);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_OV7620:
// /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
// rc = i2c_r(ov, 0x62);
// if (rc < 0)
// return rc;
// else
// *val = (rc & 0x7e) << 9;
rc = i2c_r(ov, OV7610_REG_SAT);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_SAA7111A:
*val = ov->colour;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
}
PDEBUG(3, "%d", *val);
ov->colour = *val;
return 0;
}
/* -------------------------------------------------------------------------- */
/* Sets sensor's hue (red/blue balance) setting to "val" */
static int
sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
{
int rc;
PDEBUG(3, "%d", val);
if (ov->stop_during_set)
if (ov51x_stop(ov) < 0)
return -EIO;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
if (rc < 0)
goto out;
rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
if (rc < 0)
goto out;
break;
case SEN_OV7620:
// Hue control is causing problems. I will enable it once it's fixed.
#if 0
rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
if (rc < 0)
goto out;
rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
if (rc < 0)
goto out;
#endif
break;
case SEN_SAA7111A:
rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
if (rc < 0)
goto out;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
rc = -EPERM;
goto out;
}
rc = 0; /* Success */
ov->hue = val;
out:
if (ov51x_restart(ov) < 0)
return -EIO;
return rc;
}
/* Gets sensor's hue (red/blue balance) setting */
static int
sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
{
int rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV6620:
case SEN_OV6630:
rc = i2c_r(ov, OV7610_REG_BLUE);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_OV7620:
rc = i2c_r(ov, 0x7a);
if (rc < 0)
return rc;
else
*val = rc << 8;
break;
case SEN_SAA7111A:
*val = ov->hue;
break;
default:
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
}
PDEBUG(3, "%d", *val);
ov->hue = *val;
return 0;
}
/* -------------------------------------------------------------------------- */
static int
sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
{
int rc;
PDEBUG(4, "sensor_set_picture");
ov->whiteness = p->whiteness;
/* Don't return error if a setting is unsupported, or rest of settings
* will not be performed */
rc = sensor_set_contrast(ov, p->contrast);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_brightness(ov, p->brightness);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_saturation(ov, p->colour);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_hue(ov, p->hue);
if (FATAL_ERROR(rc))
return rc;
return 0;
}
static int
sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
{
int rc;
PDEBUG(4, "sensor_get_picture");
/* Don't return error if a setting is unsupported, or rest of settings
* will not be performed */
rc = sensor_get_contrast(ov, &(p->contrast));
if (FATAL_ERROR(rc))
return rc;
rc = sensor_get_brightness(ov, &(p->brightness));
if (FATAL_ERROR(rc))
return rc;
rc = sensor_get_saturation(ov, &(p->colour));
if (FATAL_ERROR(rc))
return rc;
rc = sensor_get_hue(ov, &(p->hue));
if (FATAL_ERROR(rc))
return rc;
p->whiteness = 105 << 8;
return 0;
}
#if 0
// FIXME: Exposure range is only 0x00-0x7f in interlace mode
/* Sets current exposure for sensor. This only has an effect if auto-exposure
* is off */
static inline int
sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
{
int rc;
PDEBUG(3, "%d", val);
if (ov->stop_during_set)
if (ov51x_stop(ov) < 0)
return -EIO;
switch (ov->sensor) {
case SEN_OV6620:
case SEN_OV6630:
case SEN_OV7610:
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
rc = i2c_w(ov, 0x10, val);
if (rc < 0)
goto out;
break;
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for set_exposure");
return -EINVAL;
}
rc = 0; /* Success */
ov->exposure = val;
out:
if (ov51x_restart(ov) < 0)
return -EIO;
return rc;
}
#endif
/* Gets current exposure level from sensor, regardless of whether it is under
* manual control. */
static int
sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
{
int rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV6620:
case SEN_OV6630:
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
rc = i2c_r(ov, 0x10);
if (rc < 0)
return rc;
else
*val = rc;
break;
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
val = NULL;
PDEBUG(3, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for get_exposure");
return -EINVAL;
}
PDEBUG(3, "%d", *val);
ov->exposure = *val;
return 0;
}
/* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
static void
ov51x_led_control(struct usb_ov511 *ov, int enable)
{
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
if (ov->bridge == BRG_OV511PLUS)
reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
else if (ov->bclass == BCL_OV518)
reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
return;
}
/* Matches the sensor's internal frame rate to the lighting frequency.
* Valid frequencies are:
* 50 - 50Hz, for European and Asian lighting
* 60 - 60Hz, for American lighting
*
* Tested with: OV7610, OV7620, OV76BE, OV6620
* Unsupported: KS0127, KS0127B, SAA7111A
* Returns: 0 for success
*/
static int
sensor_set_light_freq(struct usb_ov511 *ov, int freq)
{
int sixty;
PDEBUG(4, "%d Hz", freq);
if (freq == 60)
sixty = 1;
else if (freq == 50)
sixty = 0;
else {
err("Invalid light freq (%d Hz)", freq);
return -EINVAL;
}
switch (ov->sensor) {
case SEN_OV7610:
i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
i2c_w(ov, 0x2b, sixty?0x00:0xac);
i2c_w_mask(ov, 0x13, 0x10, 0x10);
i2c_w_mask(ov, 0x13, 0x00, 0x10);
break;
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
i2c_w(ov, 0x2b, sixty?0x00:0xac);
i2c_w_mask(ov, 0x76, 0x01, 0x01);
break;
case SEN_OV6620:
case SEN_OV6630:
i2c_w(ov, 0x2b, sixty?0xa8:0x28);
i2c_w(ov, 0x2a, sixty?0x84:0xa4);
break;
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for set_light_freq");
return -EINVAL;
}
ov->lightfreq = freq;
return 0;
}
/* If enable is true, turn on the sensor's banding filter, otherwise turn it
* off. This filter tries to reduce the pattern of horizontal light/dark bands
* caused by some (usually fluorescent) lighting. The light frequency must be
* set either before or after enabling it with ov51x_set_light_freq().
*
* Tested with: OV7610, OV7620, OV76BE, OV6620.
* Unsupported: KS0127, KS0127B, SAA7111A
* Returns: 0 for success
*/
static int
sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
{
int rc;
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
|| ov->sensor == SEN_SAA7111A) {
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
}
rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
if (rc < 0)
return rc;
ov->bandfilt = enable;
return 0;
}
/* If enable is true, turn on the sensor's auto brightness control, otherwise
* turn it off.
*
* Unsupported: KS0127, KS0127B, SAA7111A
* Returns: 0 for success
*/
static int
sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
{
int rc;
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
|| ov->sensor == SEN_SAA7111A) {
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
}
rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
if (rc < 0)
return rc;
ov->auto_brt = enable;
return 0;
}
/* If enable is true, turn on the sensor's auto exposure control, otherwise
* turn it off.
*
* Unsupported: KS0127, KS0127B, SAA7111A
* Returns: 0 for success
*/
static int
sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
{
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
switch (ov->sensor) {
case SEN_OV7610:
i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
break;
case SEN_OV6620:
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
break;
case SEN_OV6630:
i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
break;
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for set_auto_exposure");
return -EINVAL;
}
ov->auto_exp = enable;
return 0;
}
/* Modifies the sensor's exposure algorithm to allow proper exposure of objects
* that are illuminated from behind.
*
* Tested with: OV6620, OV7620
* Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
* Returns: 0 for success
*/
static int
sensor_set_backlight(struct usb_ov511 *ov, int enable)
{
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
switch (ov->sensor) {
case SEN_OV7620:
case SEN_OV8600:
i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
break;
case SEN_OV6620:
i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
break;
case SEN_OV6630:
i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
break;
case SEN_OV7610:
case SEN_OV76BE:
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for set_backlight");
return -EINVAL;
}
ov->backlight = enable;
return 0;
}
static int
sensor_set_mirror(struct usb_ov511 *ov, int enable)
{
PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
switch (ov->sensor) {
case SEN_OV6620:
case SEN_OV6630:
case SEN_OV7610:
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
break;
case SEN_KS0127:
case SEN_KS0127B:
case SEN_SAA7111A:
PDEBUG(5, "Unsupported with this sensor");
return -EPERM;
default:
err("Sensor not supported for set_mirror");
return -EINVAL;
}
ov->mirror = enable;
return 0;
}
/* Returns number of bits per pixel (regardless of where they are located;
* planar or not), or zero for unsupported format.
*/
static inline int
get_depth(int palette)
{
switch (palette) {
case VIDEO_PALETTE_GREY: return 8;
case VIDEO_PALETTE_YUV420: return 12;
case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
default: return 0; /* Invalid format */
}
}
/* Bytes per frame. Used by read(). Return of 0 indicates error */
static inline long int
get_frame_length(struct ov511_frame *frame)
{
if (!frame)
return 0;
else
return ((frame->width * frame->height
* get_depth(frame->format)) >> 3);
}
static int
mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
int mode, int sub_flag, int qvga)
{
int clock;
/******** Mode (VGA/QVGA) and sensor specific regs ********/
switch (ov->sensor) {
case SEN_OV7610:
i2c_w(ov, 0x14, qvga?0x24:0x04);
// FIXME: Does this improve the image quality or frame rate?
#if 0
i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
i2c_w(ov, 0x24, 0x10);
i2c_w(ov, 0x25, qvga?0x40:0x8a);
i2c_w(ov, 0x2f, qvga?0x30:0xb0);
i2c_w(ov, 0x35, qvga?0x1c:0x9c);
#endif
break;
case SEN_OV7620:
// i2c_w(ov, 0x2b, 0x00);
i2c_w(ov, 0x14, qvga?0xa4:0x84);
i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
i2c_w(ov, 0x24, qvga?0x20:0x3a);
i2c_w(ov, 0x25, qvga?0x30:0x60);
i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
break;
case SEN_OV76BE:
// i2c_w(ov, 0x2b, 0x00);
i2c_w(ov, 0x14, qvga?0xa4:0x84);
// FIXME: Enable this once 7620AE uses 7620 initial settings
#if 0
i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
i2c_w(ov, 0x24, qvga?0x20:0x3a);
i2c_w(ov, 0x25, qvga?0x30:0x60);
i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
#endif
break;
case SEN_OV6620:
i2c_w(ov, 0x14, qvga?0x24:0x04);
break;
case SEN_OV6630:
i2c_w(ov, 0x14, qvga?0xa0:0x80);
break;
default:
err("Invalid sensor");
return -EINVAL;
}
/******** Palette-specific regs ********/
if (mode == VIDEO_PALETTE_GREY) {
if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
/* these aren't valid on the OV6620/OV7620/6630? */
i2c_w_mask(ov, 0x0e, 0x40, 0x40);
}
if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
&& ov518_color) {
i2c_w_mask(ov, 0x12, 0x00, 0x10);
i2c_w_mask(ov, 0x13, 0x00, 0x20);
} else {
i2c_w_mask(ov, 0x13, 0x20, 0x20);
}
} else {
if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
/* not valid on the OV6620/OV7620/6630? */
i2c_w_mask(ov, 0x0e, 0x00, 0x40);
}
/* The OV518 needs special treatment. Although both the OV518
* and the OV6630 support a 16-bit video bus, only the 8 bit Y
* bus is actually used. The UV bus is tied to ground.
* Therefore, the OV6630 needs to be in 8-bit multiplexed
* output mode */
if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
&& ov518_color) {
i2c_w_mask(ov, 0x12, 0x10, 0x10);
i2c_w_mask(ov, 0x13, 0x20, 0x20);
} else {
i2c_w_mask(ov, 0x13, 0x00, 0x20);
}
}
/******** Clock programming ********/
/* The OV6620 needs special handling. This prevents the
* severe banding that normally occurs */
if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
{
/* Clock down */
i2c_w(ov, 0x2a, 0x04);
if (ov->compress) {
// clock = 0; /* This ensures the highest frame rate */
clock = 3;
} else if (clockdiv == -1) { /* If user didn't override it */
clock = 3; /* Gives better exposure time */
} else {
clock = clockdiv;
}
PDEBUG(4, "Setting clock divisor to %d", clock);
i2c_w(ov, 0x11, clock);
i2c_w(ov, 0x2a, 0x84);
/* This next setting is critical. It seems to improve
* the gain or the contrast. The "reserved" bits seem
* to have some effect in this case. */
i2c_w(ov, 0x2d, 0x85);
}
else
{
if (ov->compress) {
clock = 1; /* This ensures the highest frame rate */
} else if (clockdiv == -1) { /* If user didn't override it */
/* Calculate and set the clock divisor */
clock = ((sub_flag ? ov->subw * ov->subh
: width * height)
* (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
/ 66000;
} else {
clock = clockdiv;
}
PDEBUG(4, "Setting clock divisor to %d", clock);
i2c_w(ov, 0x11, clock);
}
/******** Special Features ********/
if (framedrop >= 0)
i2c_w(ov, 0x16, framedrop);
/* Test Pattern */
i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
/* Enable auto white balance */
i2c_w_mask(ov, 0x12, 0x04, 0x04);
// This will go away as soon as ov51x_mode_init_sensor_regs()
// is fully tested.
/* 7620/6620/6630? don't have register 0x35, so play it safe */
if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
if (width == 640 && height == 480)
i2c_w(ov, 0x35, 0x9e);
else
i2c_w(ov, 0x35, 0x1e);
}
return 0;
}
static int
set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
int sub_flag)
{
int ret;
int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
int hoffset, voffset, hwscale = 0, vwscale = 0;
/* The different sensor ICs handle setting up of window differently.
* IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV76BE:
hwsbase = 0x38;
hwebase = 0x3a;
vwsbase = vwebase = 0x05;
break;
case SEN_OV6620:
case SEN_OV6630:
hwsbase = 0x38;
hwebase = 0x3a;
vwsbase = 0x05;
vwebase = 0x06;
break;
case SEN_OV7620:
hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
hwebase = 0x2f;
vwsbase = vwebase = 0x05;
break;
default:
err("Invalid sensor");
return -EINVAL;
}
if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
/* Note: OV518(+) does downsample on its own) */
if ((width > 176 && height > 144)
|| ov->bclass == BCL_OV518) { /* CIF */
ret = mode_init_ov_sensor_regs(ov, width, height,
mode, sub_flag, 0);
if (ret < 0)
return ret;
hwscale = 1;
vwscale = 1; /* The datasheet says 0; it's wrong */
hwsize = 352;
vwsize = 288;
} else if (width > 176 || height > 144) {
err("Illegal dimensions");
return -EINVAL;
} else { /* QCIF */
ret = mode_init_ov_sensor_regs(ov, width, height,
mode, sub_flag, 1);
if (ret < 0)
return ret;
hwsize = 176;
vwsize = 144;
}
} else {
if (width > 320 && height > 240) { /* VGA */
ret = mode_init_ov_sensor_regs(ov, width, height,
mode, sub_flag, 0);
if (ret < 0)
return ret;
hwscale = 2;
vwscale = 1;
hwsize = 640;
vwsize = 480;
} else if (width > 320 || height > 240) {
err("Illegal dimensions");
return -EINVAL;
} else { /* QVGA */
ret = mode_init_ov_sensor_regs(ov, width, height,
mode, sub_flag, 1);
if (ret < 0)
return ret;
hwscale = 1;
hwsize = 320;
vwsize = 240;
}
}
/* Center the window */
hoffset = ((hwsize - width) / 2) >> hwscale;
voffset = ((vwsize - height) / 2) >> vwscale;
/* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
if (sub_flag) {
i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
} else {
i2c_w(ov, 0x17, hwsbase + hoffset);
i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
i2c_w(ov, 0x19, vwsbase + voffset);
i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
}
#ifdef OV511_DEBUG
if (dump_sensor)
dump_i2c_regs(ov);
#endif
return 0;
}
/* Set up the OV511/OV511+ with the given image parameters.
*
* Do not put any sensor-specific code in here (including I2C I/O functions)
*/
static int
ov511_mode_init_regs(struct usb_ov511 *ov,
int width, int height, int mode, int sub_flag)
{
int hsegs, vsegs;
if (sub_flag) {
width = ov->subw;
height = ov->subh;
}
PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
width, height, mode, sub_flag);
// FIXME: This should be moved to a 7111a-specific function once
// subcapture is dealt with properly
if (ov->sensor == SEN_SAA7111A) {
if (width == 320 && height == 240) {
/* No need to do anything special */
} else if (width == 640 && height == 480) {
/* Set the OV511 up as 320x480, but keep the
* V4L resolution as 640x480 */
width = 320;
} else {
err("SAA7111A only allows 320x240 or 640x480");
return -EINVAL;
}
}
/* Make sure width and height are a multiple of 8 */
if (width % 8 || height % 8) {
err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
return -EINVAL;
}
if (width < ov->minwidth || height < ov->minheight) {
err("Requested dimensions are too small");
return -EINVAL;
}
if (ov51x_stop(ov) < 0)
return -EIO;
if (mode == VIDEO_PALETTE_GREY) {
reg_w(ov, R511_CAM_UV_EN, 0x00);
reg_w(ov, R511_SNAP_UV_EN, 0x00);
reg_w(ov, R511_SNAP_OPTS, 0x01);
} else {
reg_w(ov, R511_CAM_UV_EN, 0x01);
reg_w(ov, R511_SNAP_UV_EN, 0x01);
reg_w(ov, R511_SNAP_OPTS, 0x03);
}
/* Here I'm assuming that snapshot size == image size.
* I hope that's always true. --claudio
*/
hsegs = (width >> 3) - 1;
vsegs = (height >> 3) - 1;
reg_w(ov, R511_CAM_PXCNT, hsegs);
reg_w(ov, R511_CAM_LNCNT, vsegs);
reg_w(ov, R511_CAM_PXDIV, 0x00);
reg_w(ov, R511_CAM_LNDIV, 0x00);
/* YUV420, low pass filter on */
reg_w(ov, R511_CAM_OPTS, 0x03);
/* Snapshot additions */
reg_w(ov, R511_SNAP_PXCNT, hsegs);
reg_w(ov, R511_SNAP_LNCNT, vsegs);
reg_w(ov, R511_SNAP_PXDIV, 0x00);
reg_w(ov, R511_SNAP_LNDIV, 0x00);
if (ov->compress) {
/* Enable Y and UV quantization and compression */
reg_w(ov, R511_COMP_EN, 0x07);
reg_w(ov, R511_COMP_LUT_EN, 0x03);
ov51x_reset(ov, OV511_RESET_OMNICE);
}
if (ov51x_restart(ov) < 0)
return -EIO;
return 0;
}
/* Sets up the OV518/OV518+ with the given image parameters
*
* OV518 needs a completely different approach, until we can figure out what
* the individual registers do. Also, only 15 FPS is supported now.
*
* Do not put any sensor-specific code in here (including I2C I/O functions)
*/
static int
ov518_mode_init_regs(struct usb_ov511 *ov,
int width, int height, int mode, int sub_flag)
{
int hsegs, vsegs, hi_res;
if (sub_flag) {
width = ov->subw;
height = ov->subh;
}
PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
width, height, mode, sub_flag);
if (width % 16 || height % 8) {
err("Invalid size (%d, %d)", width, height);
return -EINVAL;
}
if (width < ov->minwidth || height < ov->minheight) {
err("Requested dimensions are too small");
return -EINVAL;
}
if (width >= 320 && height >= 240) {
hi_res = 1;
} else if (width >= 320 || height >= 240) {
err("Invalid width/height combination (%d, %d)", width, height);
return -EINVAL;
} else {
hi_res = 0;
}
if (ov51x_stop(ov) < 0)
return -EIO;
/******** Set the mode ********/
reg_w(ov, 0x2b, 0);
reg_w(ov, 0x2c, 0);
reg_w(ov, 0x2d, 0);
reg_w(ov, 0x2e, 0);
reg_w(ov, 0x3b, 0);
reg_w(ov, 0x3c, 0);
reg_w(ov, 0x3d, 0);
reg_w(ov, 0x3e, 0);
if (ov->bridge == BRG_OV518 && ov518_color) {
/* OV518 needs U and V swapped */
i2c_w_mask(ov, 0x15, 0x00, 0x01);
if (mode == VIDEO_PALETTE_GREY) {
/* Set 16-bit input format (UV data are ignored) */
reg_w_mask(ov, 0x20, 0x00, 0x08);
/* Set 8-bit (4:0:0) output format */
reg_w_mask(ov, 0x28, 0x00, 0xf0);
reg_w_mask(ov, 0x38, 0x00, 0xf0);
} else {
/* Set 8-bit (YVYU) input format */
reg_w_mask(ov, 0x20, 0x08, 0x08);
/* Set 12-bit (4:2:0) output format */
reg_w_mask(ov, 0x28, 0x80, 0xf0);
reg_w_mask(ov, 0x38, 0x80, 0xf0);
}
} else {
reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
}
hsegs = width / 16;
vsegs = height / 4;
reg_w(ov, 0x29, hsegs);
reg_w(ov, 0x2a, vsegs);
reg_w(ov, 0x39, hsegs);
reg_w(ov, 0x3a, vsegs);
/* Windows driver does this here; who knows why */
reg_w(ov, 0x2f, 0x80);
/******** Set the framerate (to 15 FPS) ********/
/* Mode independent, but framerate dependent, regs */
reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
reg_w(ov, 0x22, 0x18);
reg_w(ov, 0x23, 0xff);
if (ov->bridge == BRG_OV518PLUS)
reg_w(ov, 0x21, 0x19);
else
reg_w(ov, 0x71, 0x19); /* Compression-related? */
// FIXME: Sensor-specific
/* Bit 5 is what matters here. Of course, it is "reserved" */
i2c_w(ov, 0x54, 0x23);
reg_w(ov, 0x2f, 0x80);
if (ov->bridge == BRG_OV518PLUS) {
reg_w(ov, 0x24, 0x94);
reg_w(ov, 0x25, 0x90);
ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
} else {
reg_w(ov, 0x24, 0x9f);
reg_w(ov, 0x25, 0x90);
ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
}
reg_w(ov, 0x2f, 0x80);
if (ov51x_restart(ov) < 0)
return -EIO;
/* Reset it just for good measure */
if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
return -EIO;
return 0;
}
/* This is a wrapper around the OV511, OV518, and sensor specific functions */
static int
mode_init_regs(struct usb_ov511 *ov,
int width, int height, int mode, int sub_flag)
{
int rc = 0;
if (!ov || !ov->dev)
return -EFAULT;
if (ov->bclass == BCL_OV518) {
rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
} else {
rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
}
if (FATAL_ERROR(rc))
return rc;
switch (ov->sensor) {
case SEN_OV7610:
case SEN_OV7620:
case SEN_OV76BE:
case SEN_OV8600:
case SEN_OV6620:
case SEN_OV6630:
rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
break;
case SEN_KS0127:
case SEN_KS0127B:
err("KS0127-series decoders not supported yet");
rc = -EINVAL;
break;
case SEN_SAA7111A:
// rc = mode_init_saa_sensor_regs(ov, width, height, mode,
// sub_flag);
PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
break;
default:
err("Unknown sensor");
rc = -EINVAL;
}
if (FATAL_ERROR(rc))
return rc;
/* Sensor-independent settings */
rc = sensor_set_auto_brightness(ov, ov->auto_brt);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_auto_exposure(ov, ov->auto_exp);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_banding_filter(ov, bandingfilter);
if (FATAL_ERROR(rc))
return rc;
if (ov->lightfreq) {
rc = sensor_set_light_freq(ov, lightfreq);
if (FATAL_ERROR(rc))
return rc;
}
rc = sensor_set_backlight(ov, ov->backlight);
if (FATAL_ERROR(rc))
return rc;
rc = sensor_set_mirror(ov, ov->mirror);
if (FATAL_ERROR(rc))
return rc;
return 0;
}
/* This sets the default image parameters. This is useful for apps that use
* read() and do not set these.
*/
static int
ov51x_set_default_params(struct usb_ov511 *ov)
{
int i;
/* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
* (using read() instead). */
for (i = 0; i < OV511_NUMFRAMES; i++) {
ov->frame[i].width = ov->maxwidth;
ov->frame[i].height = ov->maxheight;
ov->frame[i].bytes_read = 0;
if (force_palette)
ov->frame[i].format = force_palette;
else
ov->frame[i].format = VIDEO_PALETTE_YUV420;
ov->frame[i].depth = get_depth(ov->frame[i].format);
}
PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
symbolic(v4l1_plist, ov->frame[0].format));
/* Initialize to max width/height, YUV420 or RGB24 (if supported) */
if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
ov->frame[0].format, 0) < 0)
return -EINVAL;
return 0;
}
/**********************************************************************
*
* Video decoder stuff
*
**********************************************************************/
/* Set analog input port of decoder */
static int
decoder_set_input(struct usb_ov511 *ov, int input)
{
PDEBUG(4, "port %d", input);
switch (ov->sensor) {
case SEN_SAA7111A:
{
/* Select mode */
i2c_w_mask(ov, 0x02, input, 0x07);
/* Bypass chrominance trap for modes 4..7 */
i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
break;
}
default:
return -EINVAL;
}
return 0;
}
/* Get ASCII name of video input */
static int
decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
{
switch (ov->sensor) {
case SEN_SAA7111A:
{
if (input < 0 || input > 7)
return -EINVAL;
else if (input < 4)
sprintf(name, "CVBS-%d", input);
else // if (input < 8)
sprintf(name, "S-Video-%d", input - 4);
break;
}
default:
sprintf(name, "%s", "Camera");
}
return 0;
}
/* Set norm (NTSC, PAL, SECAM, AUTO) */
static int
decoder_set_norm(struct usb_ov511 *ov, int norm)
{
PDEBUG(4, "%d", norm);
switch (ov->sensor) {
case SEN_SAA7111A:
{
int reg_8, reg_e;
if (norm == VIDEO_MODE_NTSC) {
reg_8 = 0x40; /* 60 Hz */
reg_e = 0x00; /* NTSC M / PAL BGHI */
} else if (norm == VIDEO_MODE_PAL) {
reg_8 = 0x00; /* 50 Hz */
reg_e = 0x00; /* NTSC M / PAL BGHI */
} else if (norm == VIDEO_MODE_AUTO) {
reg_8 = 0x80; /* Auto field detect */
reg_e = 0x00; /* NTSC M / PAL BGHI */
} else if (norm == VIDEO_MODE_SECAM) {
reg_8 = 0x00; /* 50 Hz */
reg_e = 0x50; /* SECAM / PAL 4.43 */
} else {
return -EINVAL;
}
i2c_w_mask(ov, 0x08, reg_8, 0xc0);
i2c_w_mask(ov, 0x0e, reg_e, 0x70);
break;
}
default:
return -EINVAL;
}
return 0;
}
/**********************************************************************
*
* Raw data parsing
*
**********************************************************************/
/* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
* image at pOut is specified by w.
*/
static inline void
make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
{
unsigned char *pOut1 = pOut;
int x, y;
for (y = 0; y < 8; y++) {
pOut1 = pOut;
for (x = 0; x < 8; x++) {
*pOut1++ = *pIn++;
}
pOut += w;
}
}
/*
* For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
* The segments represent 4 squares of 8x8 pixels as follows:
*
* 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
* 8 9 ... 15 72 73 ... 79 200 201 ... 207
* ... ... ...
* 56 57 ... 63 120 121 ... 127 248 249 ... 255
*
*/
static void
yuv400raw_to_yuv400p(struct ov511_frame *frame,
unsigned char *pIn0, unsigned char *pOut0)
{
int x, y;
unsigned char *pIn, *pOut, *pOutLine;
/* Copy Y */
pIn = pIn0;
pOutLine = pOut0;
for (y = 0; y < frame->rawheight - 1; y += 8) {
pOut = pOutLine;
for (x = 0; x < frame->rawwidth - 1; x += 8) {
make_8x8(pIn, pOut, frame->rawwidth);
pIn += 64;
pOut += 8;
}
pOutLine += 8 * frame->rawwidth;
}
}
/*
* For YUV 4:2:0 images, the data show up in 384 byte segments.
* The first 64 bytes of each segment are U, the next 64 are V. The U and
* V are arranged as follows:
*
* 0 1 ... 7
* 8 9 ... 15
* ...
* 56 57 ... 63
*
* U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
*
* The next 256 bytes are full resolution Y data and represent 4 squares
* of 8x8 pixels as follows:
*
* 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
* 8 9 ... 15 72 73 ... 79 200 201 ... 207
* ... ... ...
* 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
*
* Note that the U and V data in one segment represent a 16 x 16 pixel
* area, but the Y data represent a 32 x 8 pixel area. If the width is not an
* even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
* next horizontal stripe.
*
* If dumppix module param is set, _parse_data just dumps the incoming segments,
* verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
* this puts the data on the standard output and can be analyzed with the
* parseppm.c utility I wrote. That's a much faster way for figuring out how
* these data are scrambled.
*/
/* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
*
* FIXME: Currently only handles width and height that are multiples of 16
*/
static void
yuv420raw_to_yuv420p(struct ov511_frame *frame,
unsigned char *pIn0, unsigned char *pOut0)
{
int k, x, y;
unsigned char *pIn, *pOut, *pOutLine;
const unsigned int a = frame->rawwidth * frame->rawheight;
const unsigned int w = frame->rawwidth / 2;
/* Copy U and V */
pIn = pIn0;
pOutLine = pOut0 + a;
for (y = 0; y < frame->rawheight - 1; y += 16) {
pOut = pOutLine;
for (x = 0; x < frame->rawwidth - 1; x += 16) {
make_8x8(pIn, pOut, w);
make_8x8(pIn + 64, pOut + a/4, w);
pIn += 384;
pOut += 8;
}
pOutLine += 8 * w;
}
/* Copy Y */
pIn = pIn0 + 128;
pOutLine = pOut0;
k = 0;
for (y = 0; y < frame->rawheight - 1; y += 8) {
pOut = pOutLine;
for (x = 0; x < frame->rawwidth - 1; x += 8) {
make_8x8(pIn, pOut, frame->rawwidth);
pIn += 64;
pOut += 8;
if ((++k) > 3) {
k = 0;
pIn += 128;
}
}
pOutLine += 8 * frame->rawwidth;
}
}
/**********************************************************************
*
* Decompression
*
**********************************************************************/
static int
request_decompressor(struct usb_ov511 *ov)
{
if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
err("No decompressor available");
} else {
err("Unknown bridge");
}
return -ENOSYS;
}
static void
decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
unsigned char *pIn0, unsigned char *pOut0)
{
if (!ov->decomp_ops)
if (request_decompressor(ov))
return;