Path: blob/master/drivers/media/video/gspca/sonixb.c
17602 views
/*1* sonix sn9c102 (bayer) library2*3* Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>4* Copyright (C) 2003 2004 Michel Xhaard [email protected]5* Add Pas106 Stefano Mozzi (C) 20046*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*/2122/* Some documentation on known sonixb registers:2324Reg Use25sn9c101 / sn9c102:260x10 high nibble red gain low nibble blue gain270x11 low nibble green gain28sn9c103:290x05 red gain 0-127300x06 blue gain 0-127310x07 green gain 0-12732all:330x08-0x0f i2c / 3wire registers340x12 hstart350x13 vstart360x15 hsize (hsize = register-value * 16)370x16 vsize (vsize = register-value * 16)380x17 bit 0 toggle compression quality (according to sn9c102 driver)390x18 bit 7 enables compression, bit 4-5 set image down scaling:4000 scale 1, 01 scale 1/2, 10, scale 1/4410x19 high-nibble is sensor clock divider, changes exposure on sensors which42use a clock generated by the bridge. Some sensors have their own clock.430x1c auto_exposure area (for avg_lum) startx (startx = register-value * 32)440x1d auto_exposure area (for avg_lum) starty (starty = register-value * 32)450x1e auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)460x1f auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)47*/4849#define MODULE_NAME "sonixb"5051#include <linux/input.h>52#include "gspca.h"5354MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");55MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");56MODULE_LICENSE("GPL");5758/* controls */59enum e_ctrl {60BRIGHTNESS,61GAIN,62EXPOSURE,63AUTOGAIN,64FREQ,65NCTRLS /* number of controls */66};6768/* specific webcam descriptor */69struct sd {70struct gspca_dev gspca_dev; /* !! must be the first item */7172struct gspca_ctrl ctrls[NCTRLS];7374atomic_t avg_lum;75int prev_avg_lum;76int exp_too_low_cnt;77int exp_too_high_cnt;78int header_read;79u8 header[12]; /* Header without sof marker */8081unsigned char autogain_ignore_frames;82unsigned char frames_to_drop;8384__u8 bridge; /* Type of bridge */85#define BRIDGE_101 086#define BRIDGE_102 0 /* We make no difference between 101 and 102 */87#define BRIDGE_103 18889__u8 sensor; /* Type of image sensor chip */90#define SENSOR_HV7131D 091#define SENSOR_HV7131R 192#define SENSOR_OV6650 293#define SENSOR_OV7630 394#define SENSOR_PAS106 495#define SENSOR_PAS202 596#define SENSOR_TAS5110C 697#define SENSOR_TAS5110D 798#define SENSOR_TAS5130CXX 899__u8 reg11;100};101102typedef const __u8 sensor_init_t[8];103104struct sensor_data {105const __u8 *bridge_init;106sensor_init_t *sensor_init;107int sensor_init_size;108int flags;109unsigned ctrl_dis;110__u8 sensor_addr;111};112113/* sensor_data flags */114#define F_GAIN 0x01 /* has gain */115#define F_SIF 0x02 /* sif or vga */116#define F_COARSE_EXPO 0x04 /* exposure control is coarse */117118/* priv field of struct v4l2_pix_format flags (do not use low nibble!) */119#define MODE_RAW 0x10 /* raw bayer mode */120#define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */121122/* ctrl_dis helper macros */123#define NO_EXPO ((1 << EXPOSURE) | (1 << AUTOGAIN))124#define NO_FREQ (1 << FREQ)125#define NO_BRIGHTNESS (1 << BRIGHTNESS)126127#define COMP 0xc7 /* 0x87 //0x07 */128#define COMP1 0xc9 /* 0x89 //0x09 */129130#define MCK_INIT 0x63131#define MCK_INIT1 0x20 /*fixme: Bayer - 0x50 for JPEG ??*/132133#define SYS_CLK 0x04134135#define SENS(bridge, sensor, _flags, _ctrl_dis, _sensor_addr) \136{ \137.bridge_init = bridge, \138.sensor_init = sensor, \139.sensor_init_size = sizeof(sensor), \140.flags = _flags, .ctrl_dis = _ctrl_dis, .sensor_addr = _sensor_addr \141}142143/* We calculate the autogain at the end of the transfer of a frame, at this144moment a frame with the old settings is being captured and transmitted. So145if we adjust the gain or exposure we must ignore atleast the next frame for146the new settings to come into effect before doing any other adjustments. */147#define AUTOGAIN_IGNORE_FRAMES 1148149/* V4L2 controls supported by the driver */150static void setbrightness(struct gspca_dev *gspca_dev);151static void setgain(struct gspca_dev *gspca_dev);152static void setexposure(struct gspca_dev *gspca_dev);153static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);154static void setfreq(struct gspca_dev *gspca_dev);155156static const struct ctrl sd_ctrls[NCTRLS] = {157[BRIGHTNESS] = {158{159.id = V4L2_CID_BRIGHTNESS,160.type = V4L2_CTRL_TYPE_INTEGER,161.name = "Brightness",162.minimum = 0,163.maximum = 255,164.step = 1,165.default_value = 127,166},167.set_control = setbrightness168},169[GAIN] = {170{171.id = V4L2_CID_GAIN,172.type = V4L2_CTRL_TYPE_INTEGER,173.name = "Gain",174.minimum = 0,175.maximum = 255,176.step = 1,177#define GAIN_KNEE 230178.default_value = 127,179},180.set_control = setgain181},182[EXPOSURE] = {183{184.id = V4L2_CID_EXPOSURE,185.type = V4L2_CTRL_TYPE_INTEGER,186.name = "Exposure",187.minimum = 0,188.maximum = 1023,189.step = 1,190.default_value = 66,191/* 33 ms / 30 fps (except on PASXXX) */192#define EXPOSURE_KNEE 200 /* 100 ms / 10 fps (except on PASXXX) */193.flags = 0,194},195.set_control = setexposure196},197/* for coarse exposure */198#define COARSE_EXPOSURE_MIN 2199#define COARSE_EXPOSURE_MAX 15200#define COARSE_EXPOSURE_DEF 2 /* 30 fps */201[AUTOGAIN] = {202{203.id = V4L2_CID_AUTOGAIN,204.type = V4L2_CTRL_TYPE_BOOLEAN,205.name = "Automatic Gain (and Exposure)",206.minimum = 0,207.maximum = 1,208.step = 1,209#define AUTOGAIN_DEF 1210.default_value = AUTOGAIN_DEF,211.flags = V4L2_CTRL_FLAG_UPDATE212},213.set = sd_setautogain,214},215[FREQ] = {216{217.id = V4L2_CID_POWER_LINE_FREQUENCY,218.type = V4L2_CTRL_TYPE_MENU,219.name = "Light frequency filter",220.minimum = 0,221.maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */222.step = 1,223#define FREQ_DEF 0224.default_value = FREQ_DEF,225},226.set_control = setfreq227},228};229230static const struct v4l2_pix_format vga_mode[] = {231{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,232.bytesperline = 160,233.sizeimage = 160 * 120,234.colorspace = V4L2_COLORSPACE_SRGB,235.priv = 2 | MODE_RAW},236{160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,237.bytesperline = 160,238.sizeimage = 160 * 120 * 5 / 4,239.colorspace = V4L2_COLORSPACE_SRGB,240.priv = 2},241{320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,242.bytesperline = 320,243.sizeimage = 320 * 240 * 5 / 4,244.colorspace = V4L2_COLORSPACE_SRGB,245.priv = 1},246{640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,247.bytesperline = 640,248.sizeimage = 640 * 480 * 5 / 4,249.colorspace = V4L2_COLORSPACE_SRGB,250.priv = 0},251};252static const struct v4l2_pix_format sif_mode[] = {253{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,254.bytesperline = 160,255.sizeimage = 160 * 120,256.colorspace = V4L2_COLORSPACE_SRGB,257.priv = 1 | MODE_RAW | MODE_REDUCED_SIF},258{160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,259.bytesperline = 160,260.sizeimage = 160 * 120 * 5 / 4,261.colorspace = V4L2_COLORSPACE_SRGB,262.priv = 1 | MODE_REDUCED_SIF},263{176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,264.bytesperline = 176,265.sizeimage = 176 * 144,266.colorspace = V4L2_COLORSPACE_SRGB,267.priv = 1 | MODE_RAW},268{176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,269.bytesperline = 176,270.sizeimage = 176 * 144 * 5 / 4,271.colorspace = V4L2_COLORSPACE_SRGB,272.priv = 1},273{320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,274.bytesperline = 320,275.sizeimage = 320 * 240 * 5 / 4,276.colorspace = V4L2_COLORSPACE_SRGB,277.priv = 0 | MODE_REDUCED_SIF},278{352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,279.bytesperline = 352,280.sizeimage = 352 * 288 * 5 / 4,281.colorspace = V4L2_COLORSPACE_SRGB,282.priv = 0},283};284285static const __u8 initHv7131d[] = {2860x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,2870x00, 0x00,2880x00, 0x00, 0x00, 0x02, 0x02, 0x00,2890x28, 0x1e, 0x60, 0x8e, 0x42,290};291static const __u8 hv7131d_sensor_init[][8] = {292{0xa0, 0x11, 0x01, 0x04, 0x00, 0x00, 0x00, 0x17},293{0xa0, 0x11, 0x02, 0x00, 0x00, 0x00, 0x00, 0x17},294{0xa0, 0x11, 0x28, 0x00, 0x00, 0x00, 0x00, 0x17},295{0xa0, 0x11, 0x30, 0x30, 0x00, 0x00, 0x00, 0x17}, /* reset level */296{0xa0, 0x11, 0x34, 0x02, 0x00, 0x00, 0x00, 0x17}, /* pixel bias volt */297};298299static const __u8 initHv7131r[] = {3000x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,3010x00, 0x00,3020x00, 0x00, 0x00, 0x02, 0x01, 0x00,3030x28, 0x1e, 0x60, 0x8a, 0x20,304};305static const __u8 hv7131r_sensor_init[][8] = {306{0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},307{0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},308{0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},309{0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},310{0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},311};312static const __u8 initOv6650[] = {3130x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,3140x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3150x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,3160x10,317};318static const __u8 ov6650_sensor_init[][8] = {319/* Bright, contrast, etc are set through SCBB interface.320* AVCAP on win2 do not send any data on this controls. */321/* Anyway, some registers appears to alter bright and constrat */322323/* Reset sensor */324{0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},325/* Set clock register 0x11 low nibble is clock divider */326{0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},327/* Next some unknown stuff */328{0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},329/* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},330* THIS SET GREEN SCREEN331* (pixels could be innverted in decode kind of "brg",332* but blue wont be there. Avoid this data ... */333{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */334{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},335{0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},336/* Enable rgb brightness control */337{0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},338/* HDG: Note windows uses the line below, which sets both register 0x60339and 0x61 I believe these registers of the ov6650 are identical as340those of the ov7630, because if this is true the windows settings341add a bit additional red gain and a lot additional blue gain, which342matches my findings that the windows settings make blue much too343blue and red a little too red.344{0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */345/* Some more unknown stuff */346{0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},347{0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */348};349350static const __u8 initOv7630[] = {3510x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */3520x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */3530x00, 0x01, 0x01, 0x0a, /* r11 .. r14 */3540x28, 0x1e, /* H & V sizes r15 .. r16 */3550x68, 0x8f, MCK_INIT1, /* r17 .. r19 */356};357static const __u8 ov7630_sensor_init[][8] = {358{0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},359{0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},360/* {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10}, jfm */361{0xd0, 0x21, 0x12, 0x5c, 0x00, 0x80, 0x34, 0x10}, /* jfm */362{0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},363{0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},364{0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},365{0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},366{0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},367{0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},368{0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},369{0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},370/* {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10}, * jfm */371{0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},372{0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},373{0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},374{0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},375{0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},376{0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},377};378379static const __u8 initPas106[] = {3800x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,3810x00, 0x00,3820x00, 0x00, 0x00, 0x04, 0x01, 0x00,3830x16, 0x12, 0x24, COMP1, MCK_INIT1,384};385/* compression 0x86 mckinit1 0x2b */386387/* "Known" PAS106B registers:3880x02 clock divider3890x03 Variable framerate bits 4-113900x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!391The variable framerate control must never be set lower then 300,392which sets the framerate at 90 / reg02, otherwise vsync is lost.3930x05 Shutter Time Line Offset, this can be used as an exposure control:3940 = use full frame time, 255 = no exposure at all395Note this may never be larger then "var-framerate control" / 2 - 2.396When var-framerate control is < 514, no exposure is reached at the max397allowed value for the framerate control value, rather then at 255.3980x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but399only a very little bit, leave at 0xcd4000x07 offset sign bit (bit0 1 > negative offset)4010x08 offset4020x09 Blue Gain4030x0a Green1 Gain4040x0b Green2 Gain4050x0c Red Gain4060x0e Global gain4070x13 Write 1 to commit settings to sensor408*/409410static const __u8 pas106_sensor_init[][8] = {411/* Pixel Clock Divider 6 */412{ 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },413/* Frame Time MSB (also seen as 0x12) */414{ 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },415/* Frame Time LSB (also seen as 0x05) */416{ 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },417/* Shutter Time Line Offset (also seen as 0x6d) */418{ 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },419/* Shutter Time Pixel Offset (also seen as 0xb1) */420{ 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },421/* Black Level Subtract Sign (also seen 0x00) */422{ 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },423/* Black Level Subtract Level (also seen 0x01) */424{ 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },425{ 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },426/* Color Gain B Pixel 5 a */427{ 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },428/* Color Gain G1 Pixel 1 5 */429{ 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },430/* Color Gain G2 Pixel 1 0 5 */431{ 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },432/* Color Gain R Pixel 3 1 */433{ 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },434/* Color GainH Pixel */435{ 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },436/* Global Gain */437{ 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },438/* Contrast */439{ 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },440/* H&V synchro polarity */441{ 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },442/* ?default */443{ 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },444/* DAC scale */445{ 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },446/* ?default */447{ 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },448/* Validate Settings */449{ 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },450};451452static const __u8 initPas202[] = {4530x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,4540x00, 0x00,4550x00, 0x00, 0x00, 0x06, 0x03, 0x0a,4560x28, 0x1e, 0x20, 0x89, 0x20,457};458459/* "Known" PAS202BCB registers:4600x02 clock divider4610x04 Variable framerate bits 6-11 (*)4620x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !!4630x07 Blue Gain4640x08 Green Gain4650x09 Red Gain4660x0b offset sign bit (bit0 1 > negative offset)4670x0c offset4680x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,469leave at 1 otherwise we get a jump in our exposure control4700x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all4710x10 Master gain 0 - 314720x11 write 1 to apply changes473(*) The variable framerate control must never be set lower then 500474which sets the framerate at 30 / reg02, otherwise vsync is lost.475*/476static const __u8 pas202_sensor_init[][8] = {477/* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like478to set it lower, but for some reason the bridge starts missing479vsync's then */480{0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},481{0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},482{0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},483{0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},484{0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},485{0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},486{0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},487{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},488{0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},489{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},490};491492static const __u8 initTas5110c[] = {4930x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,4940x00, 0x00,4950x00, 0x00, 0x00, 0x45, 0x09, 0x0a,4960x16, 0x12, 0x60, 0x86, 0x2b,497};498/* Same as above, except a different hstart */499static const __u8 initTas5110d[] = {5000x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,5010x00, 0x00,5020x00, 0x00, 0x00, 0x41, 0x09, 0x0a,5030x16, 0x12, 0x60, 0x86, 0x2b,504};505/* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */506static const __u8 tas5110c_sensor_init[][8] = {507{0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},508{0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},509};510/* Known TAS5110D registers511* reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain512* reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted)513* Note: writing reg03 seems to only work when written together with 02514*/515static const __u8 tas5110d_sensor_init[][8] = {516{0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */517};518519static const __u8 initTas5130[] = {5200x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,5210x00, 0x00,5220x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,5230x28, 0x1e, 0x60, COMP, MCK_INIT,524};525static const __u8 tas5130_sensor_init[][8] = {526/* {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},527* shutter 0x47 short exposure? */528{0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},529/* shutter 0x01 long exposure */530{0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},531};532533static const struct sensor_data sensor_data[] = {534SENS(initHv7131d, hv7131d_sensor_init, F_GAIN, NO_BRIGHTNESS|NO_FREQ, 0),535SENS(initHv7131r, hv7131r_sensor_init, 0, NO_BRIGHTNESS|NO_EXPO|NO_FREQ, 0),536SENS(initOv6650, ov6650_sensor_init, F_GAIN|F_SIF, 0, 0x60),537SENS(initOv7630, ov7630_sensor_init, F_GAIN, 0, 0x21),538SENS(initPas106, pas106_sensor_init, F_GAIN|F_SIF, NO_FREQ, 0),539SENS(initPas202, pas202_sensor_init, F_GAIN, NO_FREQ, 0),540SENS(initTas5110c, tas5110c_sensor_init, F_GAIN|F_SIF|F_COARSE_EXPO,541NO_BRIGHTNESS|NO_FREQ, 0),542SENS(initTas5110d, tas5110d_sensor_init, F_GAIN|F_SIF|F_COARSE_EXPO,543NO_BRIGHTNESS|NO_FREQ, 0),544SENS(initTas5130, tas5130_sensor_init, F_GAIN,545NO_BRIGHTNESS|NO_EXPO|NO_FREQ, 0),546};547548/* get one byte in gspca_dev->usb_buf */549static void reg_r(struct gspca_dev *gspca_dev,550__u16 value)551{552usb_control_msg(gspca_dev->dev,553usb_rcvctrlpipe(gspca_dev->dev, 0),5540, /* request */555USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,556value,5570, /* index */558gspca_dev->usb_buf, 1,559500);560}561562static void reg_w(struct gspca_dev *gspca_dev,563__u16 value,564const __u8 *buffer,565int len)566{567#ifdef GSPCA_DEBUG568if (len > USB_BUF_SZ) {569PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow");570return;571}572#endif573memcpy(gspca_dev->usb_buf, buffer, len);574usb_control_msg(gspca_dev->dev,575usb_sndctrlpipe(gspca_dev->dev, 0),5760x08, /* request */577USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,578value,5790, /* index */580gspca_dev->usb_buf, len,581500);582}583584static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)585{586int retry = 60;587588/* is i2c ready */589reg_w(gspca_dev, 0x08, buffer, 8);590while (retry--) {591msleep(10);592reg_r(gspca_dev, 0x08);593if (gspca_dev->usb_buf[0] & 0x04) {594if (gspca_dev->usb_buf[0] & 0x08)595return -1;596return 0;597}598}599return -1;600}601602static void i2c_w_vector(struct gspca_dev *gspca_dev,603const __u8 buffer[][8], int len)604{605for (;;) {606reg_w(gspca_dev, 0x08, *buffer, 8);607len -= 8;608if (len <= 0)609break;610buffer++;611}612}613614static void setbrightness(struct gspca_dev *gspca_dev)615{616struct sd *sd = (struct sd *) gspca_dev;617618switch (sd->sensor) {619case SENSOR_OV6650:620case SENSOR_OV7630: {621__u8 i2cOV[] =622{0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};623624/* change reg 0x06 */625i2cOV[1] = sensor_data[sd->sensor].sensor_addr;626i2cOV[3] = sd->ctrls[BRIGHTNESS].val;627if (i2c_w(gspca_dev, i2cOV) < 0)628goto err;629break;630}631case SENSOR_PAS106:632case SENSOR_PAS202: {633__u8 i2cpbright[] =634{0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};635__u8 i2cpdoit[] =636{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};637638/* PAS106 uses reg 7 and 8 instead of b and c */639if (sd->sensor == SENSOR_PAS106) {640i2cpbright[2] = 7;641i2cpdoit[2] = 0x13;642}643644if (sd->ctrls[BRIGHTNESS].val < 127) {645/* change reg 0x0b, signreg */646i2cpbright[3] = 0x01;647/* set reg 0x0c, offset */648i2cpbright[4] = 127 - sd->ctrls[BRIGHTNESS].val;649} else650i2cpbright[4] = sd->ctrls[BRIGHTNESS].val - 127;651652if (i2c_w(gspca_dev, i2cpbright) < 0)653goto err;654if (i2c_w(gspca_dev, i2cpdoit) < 0)655goto err;656break;657}658}659return;660err:661PDEBUG(D_ERR, "i2c error brightness");662}663664static void setsensorgain(struct gspca_dev *gspca_dev)665{666struct sd *sd = (struct sd *) gspca_dev;667u8 gain = sd->ctrls[GAIN].val;668669switch (sd->sensor) {670case SENSOR_HV7131D: {671__u8 i2c[] =672{0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};673674i2c[3] = 0x3f - (gain / 4);675i2c[4] = 0x3f - (gain / 4);676i2c[5] = 0x3f - (gain / 4);677678if (i2c_w(gspca_dev, i2c) < 0)679goto err;680break;681}682case SENSOR_TAS5110C:683case SENSOR_TAS5130CXX: {684__u8 i2c[] =685{0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};686687i2c[4] = 255 - gain;688if (i2c_w(gspca_dev, i2c) < 0)689goto err;690break;691}692case SENSOR_TAS5110D: {693__u8 i2c[] = {6940xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };695gain = 255 - gain;696/* The bits in the register are the wrong way around!! */697i2c[3] |= (gain & 0x80) >> 7;698i2c[3] |= (gain & 0x40) >> 5;699i2c[3] |= (gain & 0x20) >> 3;700i2c[3] |= (gain & 0x10) >> 1;701i2c[3] |= (gain & 0x08) << 1;702i2c[3] |= (gain & 0x04) << 3;703i2c[3] |= (gain & 0x02) << 5;704i2c[3] |= (gain & 0x01) << 7;705if (i2c_w(gspca_dev, i2c) < 0)706goto err;707break;708}709710case SENSOR_OV6650:711gain >>= 1;712/* fall thru */713case SENSOR_OV7630: {714__u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};715716i2c[1] = sensor_data[sd->sensor].sensor_addr;717i2c[3] = gain >> 2;718if (i2c_w(gspca_dev, i2c) < 0)719goto err;720break;721}722case SENSOR_PAS106:723case SENSOR_PAS202: {724__u8 i2cpgain[] =725{0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};726__u8 i2cpcolorgain[] =727{0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};728__u8 i2cpdoit[] =729{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};730731/* PAS106 uses different regs (and has split green gains) */732if (sd->sensor == SENSOR_PAS106) {733i2cpgain[2] = 0x0e;734i2cpcolorgain[0] = 0xd0;735i2cpcolorgain[2] = 0x09;736i2cpdoit[2] = 0x13;737}738739i2cpgain[3] = gain >> 3;740i2cpcolorgain[3] = gain >> 4;741i2cpcolorgain[4] = gain >> 4;742i2cpcolorgain[5] = gain >> 4;743i2cpcolorgain[6] = gain >> 4;744745if (i2c_w(gspca_dev, i2cpgain) < 0)746goto err;747if (i2c_w(gspca_dev, i2cpcolorgain) < 0)748goto err;749if (i2c_w(gspca_dev, i2cpdoit) < 0)750goto err;751break;752}753}754return;755err:756PDEBUG(D_ERR, "i2c error gain");757}758759static void setgain(struct gspca_dev *gspca_dev)760{761struct sd *sd = (struct sd *) gspca_dev;762__u8 gain;763__u8 buf[3] = { 0, 0, 0 };764765if (sensor_data[sd->sensor].flags & F_GAIN) {766/* Use the sensor gain to do the actual gain */767setsensorgain(gspca_dev);768return;769}770771if (sd->bridge == BRIDGE_103) {772gain = sd->ctrls[GAIN].val >> 1;773buf[0] = gain; /* Red */774buf[1] = gain; /* Green */775buf[2] = gain; /* Blue */776reg_w(gspca_dev, 0x05, buf, 3);777} else {778gain = sd->ctrls[GAIN].val >> 4;779buf[0] = gain << 4 | gain; /* Red and blue */780buf[1] = gain; /* Green */781reg_w(gspca_dev, 0x10, buf, 2);782}783}784785static void setexposure(struct gspca_dev *gspca_dev)786{787struct sd *sd = (struct sd *) gspca_dev;788789switch (sd->sensor) {790case SENSOR_HV7131D: {791/* Note the datasheet wrongly says line mode exposure uses reg7920x26 and 0x27, testing has shown 0x25 + 0x26 */793__u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};794/* The HV7131D's exposure goes from 0 - 65535, we scale our795exposure of 0-1023 to 0-6138. There are 2 reasons for this:7961) This puts our exposure knee of 200 at approx the point797where the framerate starts dropping7982) At 6138 the framerate has already dropped to 2 fps,799going any lower makes little sense */800u16 reg = sd->ctrls[EXPOSURE].val * 6;801802i2c[3] = reg >> 8;803i2c[4] = reg & 0xff;804if (i2c_w(gspca_dev, i2c) != 0)805goto err;806break;807}808case SENSOR_TAS5110C:809case SENSOR_TAS5110D: {810/* register 19's high nibble contains the sn9c10x clock divider811The high nibble configures the no fps according to the812formula: 60 / high_nibble. With a maximum of 30 fps */813u8 reg = sd->ctrls[EXPOSURE].val;814815reg = (reg << 4) | 0x0b;816reg_w(gspca_dev, 0x19, ®, 1);817break;818}819case SENSOR_OV6650:820case SENSOR_OV7630: {821/* The ov6650 / ov7630 have 2 registers which both influence822exposure, register 11, whose low nibble sets the nr off fps823according to: fps = 30 / (low_nibble + 1)824825The fps configures the maximum exposure setting, but it is826possible to use less exposure then what the fps maximum827allows by setting register 10. register 10 configures the828actual exposure as quotient of the full exposure, with 0829being no exposure at all (not very useful) and reg10_max830being max exposure possible at that framerate.831832The code maps our 0 - 510 ms exposure ctrl to these 2833registers, trying to keep fps as high as possible.834*/835__u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};836int reg10, reg11, reg10_max;837838/* ov6645 datasheet says reg10_max is 9a, but that uses839tline * 2 * reg10 as formula for calculating texpo, the840ov6650 probably uses the same formula as the 7730 which uses841tline * 4 * reg10, which explains why the reg10max we've842found experimentally for the ov6650 is exactly half that of843the ov6645. The ov7630 datasheet says the max is 0x41. */844if (sd->sensor == SENSOR_OV6650) {845reg10_max = 0x4d;846i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */847} else848reg10_max = 0x41;849850reg11 = (15 * sd->ctrls[EXPOSURE].val + 999) / 1000;851if (reg11 < 1)852reg11 = 1;853else if (reg11 > 16)854reg11 = 16;855856/* In 640x480, if the reg11 has less than 4, the image is857unstable (the bridge goes into a higher compression mode858which we have not reverse engineered yet). */859if (gspca_dev->width == 640 && reg11 < 4)860reg11 = 4;861862/* frame exposure time in ms = 1000 * reg11 / 30 ->863reg10 = (sd->ctrls[EXPOSURE].val / 2) * reg10_max864/ (1000 * reg11 / 30) */865reg10 = (sd->ctrls[EXPOSURE].val * 15 * reg10_max)866/ (1000 * reg11);867868/* Don't allow this to get below 10 when using autogain, the869steps become very large (relatively) when below 10 causing870the image to oscilate from much too dark, to much too bright871and back again. */872if (sd->ctrls[AUTOGAIN].val && reg10 < 10)873reg10 = 10;874else if (reg10 > reg10_max)875reg10 = reg10_max;876877/* Write reg 10 and reg11 low nibble */878i2c[1] = sensor_data[sd->sensor].sensor_addr;879i2c[3] = reg10;880i2c[4] |= reg11 - 1;881882/* If register 11 didn't change, don't change it */883if (sd->reg11 == reg11)884i2c[0] = 0xa0;885886if (i2c_w(gspca_dev, i2c) == 0)887sd->reg11 = reg11;888else889goto err;890break;891}892case SENSOR_PAS202: {893__u8 i2cpframerate[] =894{0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};895__u8 i2cpexpo[] =896{0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};897const __u8 i2cpdoit[] =898{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};899int framerate_ctrl;900901/* The exposure knee for the autogain algorithm is 200902(100 ms / 10 fps on other sensors), for values below this903use the control for setting the partial frame expose time,904above that use variable framerate. This way we run at max905framerate ([email protected] fps, 320x240@10fps) until the knee906is reached. Using the variable framerate control above 200907is better then playing around with both clockdiv + partial908frame exposure times (like we are doing with the ov chips),909as that sometimes leads to jumps in the exposure control,910which are bad for auto exposure. */911if (sd->ctrls[EXPOSURE].val < 200) {912i2cpexpo[3] = 255 - (sd->ctrls[EXPOSURE].val * 255)913/ 200;914framerate_ctrl = 500;915} else {916/* The PAS202's exposure control goes from 0 - 4095,917but anything below 500 causes vsync issues, so scale918our 200-1023 to 500-4095 */919framerate_ctrl = (sd->ctrls[EXPOSURE].val - 200)920* 1000 / 229 + 500;921}922923i2cpframerate[3] = framerate_ctrl >> 6;924i2cpframerate[4] = framerate_ctrl & 0x3f;925if (i2c_w(gspca_dev, i2cpframerate) < 0)926goto err;927if (i2c_w(gspca_dev, i2cpexpo) < 0)928goto err;929if (i2c_w(gspca_dev, i2cpdoit) < 0)930goto err;931break;932}933case SENSOR_PAS106: {934__u8 i2cpframerate[] =935{0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};936__u8 i2cpexpo[] =937{0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};938const __u8 i2cpdoit[] =939{0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};940int framerate_ctrl;941942/* For values below 150 use partial frame exposure, above943that use framerate ctrl */944if (sd->ctrls[EXPOSURE].val < 150) {945i2cpexpo[3] = 150 - sd->ctrls[EXPOSURE].val;946framerate_ctrl = 300;947} else {948/* The PAS106's exposure control goes from 0 - 4095,949but anything below 300 causes vsync issues, so scale950our 150-1023 to 300-4095 */951framerate_ctrl = (sd->ctrls[EXPOSURE].val - 150)952* 1000 / 230 + 300;953}954955i2cpframerate[3] = framerate_ctrl >> 4;956i2cpframerate[4] = framerate_ctrl & 0x0f;957if (i2c_w(gspca_dev, i2cpframerate) < 0)958goto err;959if (i2c_w(gspca_dev, i2cpexpo) < 0)960goto err;961if (i2c_w(gspca_dev, i2cpdoit) < 0)962goto err;963break;964}965}966return;967err:968PDEBUG(D_ERR, "i2c error exposure");969}970971static void setfreq(struct gspca_dev *gspca_dev)972{973struct sd *sd = (struct sd *) gspca_dev;974975switch (sd->sensor) {976case SENSOR_OV6650:977case SENSOR_OV7630: {978/* Framerate adjust register for artificial light 50 hz flicker979compensation, for the ov6650 this is identical to ov66309800x2b register, see ov6630 datasheet.9810x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */982__u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};983switch (sd->ctrls[FREQ].val) {984default:985/* case 0: * no filter*/986/* case 2: * 60 hz */987i2c[3] = 0;988break;989case 1: /* 50 hz */990i2c[3] = (sd->sensor == SENSOR_OV6650)991? 0x4f : 0x8a;992break;993}994i2c[1] = sensor_data[sd->sensor].sensor_addr;995if (i2c_w(gspca_dev, i2c) < 0)996PDEBUG(D_ERR, "i2c error setfreq");997break;998}999}1000}10011002#include "autogain_functions.h"10031004static void do_autogain(struct gspca_dev *gspca_dev)1005{1006int deadzone, desired_avg_lum, result;1007struct sd *sd = (struct sd *) gspca_dev;1008int avg_lum = atomic_read(&sd->avg_lum);10091010if ((gspca_dev->ctrl_dis & (1 << AUTOGAIN)) ||1011avg_lum == -1 || !sd->ctrls[AUTOGAIN].val)1012return;10131014if (sd->autogain_ignore_frames > 0) {1015sd->autogain_ignore_frames--;1016return;1017}10181019/* SIF / VGA sensors have a different autoexposure area and thus1020different avg_lum values for the same picture brightness */1021if (sensor_data[sd->sensor].flags & F_SIF) {1022deadzone = 500;1023/* SIF sensors tend to overexpose, so keep this small */1024desired_avg_lum = 5000;1025} else {1026deadzone = 1500;1027desired_avg_lum = 13000;1028}10291030if (sensor_data[sd->sensor].flags & F_COARSE_EXPO)1031result = coarse_grained_expo_autogain(gspca_dev, avg_lum,1032sd->ctrls[BRIGHTNESS].val1033* desired_avg_lum / 127,1034deadzone);1035else1036result = auto_gain_n_exposure(gspca_dev, avg_lum,1037sd->ctrls[BRIGHTNESS].val1038* desired_avg_lum / 127,1039deadzone, GAIN_KNEE, EXPOSURE_KNEE);10401041if (result) {1042PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d",1043(int) sd->ctrls[GAIN].val,1044(int) sd->ctrls[EXPOSURE].val);1045sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;1046}1047}10481049/* this function is called at probe time */1050static int sd_config(struct gspca_dev *gspca_dev,1051const struct usb_device_id *id)1052{1053struct sd *sd = (struct sd *) gspca_dev;1054struct cam *cam;10551056reg_r(gspca_dev, 0x00);1057if (gspca_dev->usb_buf[0] != 0x10)1058return -ENODEV;10591060/* copy the webcam info from the device id */1061sd->sensor = id->driver_info >> 8;1062sd->bridge = id->driver_info & 0xff;10631064gspca_dev->ctrl_dis = sensor_data[sd->sensor].ctrl_dis;1065#if AUTOGAIN_DEF1066if (!(gspca_dev->ctrl_dis & (1 << AUTOGAIN)))1067gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);1068#endif10691070cam = &gspca_dev->cam;1071cam->ctrls = sd->ctrls;1072if (!(sensor_data[sd->sensor].flags & F_SIF)) {1073cam->cam_mode = vga_mode;1074cam->nmodes = ARRAY_SIZE(vga_mode);1075} else {1076cam->cam_mode = sif_mode;1077cam->nmodes = ARRAY_SIZE(sif_mode);1078}1079cam->npkt = 36; /* 36 packets per ISOC message */10801081if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) {1082sd->ctrls[EXPOSURE].min = COARSE_EXPOSURE_MIN;1083sd->ctrls[EXPOSURE].max = COARSE_EXPOSURE_MAX;1084sd->ctrls[EXPOSURE].def = COARSE_EXPOSURE_DEF;1085}10861087return 0;1088}10891090/* this function is called at probe and resume time */1091static int sd_init(struct gspca_dev *gspca_dev)1092{1093const __u8 stop = 0x09; /* Disable stream turn of LED */10941095reg_w(gspca_dev, 0x01, &stop, 1);10961097return 0;1098}10991100/* -- start the camera -- */1101static int sd_start(struct gspca_dev *gspca_dev)1102{1103struct sd *sd = (struct sd *) gspca_dev;1104struct cam *cam = &gspca_dev->cam;1105int i, mode;1106__u8 regs[0x31];11071108mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;1109/* Copy registers 0x01 - 0x19 from the template */1110memcpy(®s[0x01], sensor_data[sd->sensor].bridge_init, 0x19);1111/* Set the mode */1112regs[0x18] |= mode << 4;11131114/* Set bridge gain to 1.0 */1115if (sd->bridge == BRIDGE_103) {1116regs[0x05] = 0x20; /* Red */1117regs[0x06] = 0x20; /* Green */1118regs[0x07] = 0x20; /* Blue */1119} else {1120regs[0x10] = 0x00; /* Red and blue */1121regs[0x11] = 0x00; /* Green */1122}11231124/* Setup pixel numbers and auto exposure window */1125if (sensor_data[sd->sensor].flags & F_SIF) {1126regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */1127regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */1128regs[0x1c] = 0x02; /* AE H-start 64 */1129regs[0x1d] = 0x02; /* AE V-start 64 */1130regs[0x1e] = 0x09; /* AE H-end 288 */1131regs[0x1f] = 0x07; /* AE V-end 224 */1132} else {1133regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */1134regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */1135regs[0x1c] = 0x05; /* AE H-start 160 */1136regs[0x1d] = 0x03; /* AE V-start 96 */1137regs[0x1e] = 0x0f; /* AE H-end 480 */1138regs[0x1f] = 0x0c; /* AE V-end 384 */1139}11401141/* Setup the gamma table (only used with the sn9c103 bridge) */1142for (i = 0; i < 16; i++)1143regs[0x20 + i] = i * 16;1144regs[0x20 + i] = 255;11451146/* Special cases where some regs depend on mode or bridge */1147switch (sd->sensor) {1148case SENSOR_TAS5130CXX:1149/* FIXME / TESTME1150probably not mode specific at all most likely the upper1151nibble of 0x19 is exposure (clock divider) just as with1152the tas5110, we need someone to test this. */1153regs[0x19] = mode ? 0x23 : 0x43;1154break;1155case SENSOR_OV7630:1156/* FIXME / TESTME for some reason with the 101/102 bridge the1157clock is set to 12 Mhz (reg1 == 0x04), rather then 24.1158Also the hstart needs to go from 1 to 2 when using a 103,1159which is likely related. This does not seem right. */1160if (sd->bridge == BRIDGE_103) {1161regs[0x01] = 0x44; /* Select 24 Mhz clock */1162regs[0x12] = 0x02; /* Set hstart to 2 */1163}1164}1165/* Disable compression when the raw bayer format has been selected */1166if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)1167regs[0x18] &= ~0x80;11681169/* Vga mode emulation on SIF sensor? */1170if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {1171regs[0x12] += 16; /* hstart adjust */1172regs[0x13] += 24; /* vstart adjust */1173regs[0x15] = 320 / 16; /* hsize */1174regs[0x16] = 240 / 16; /* vsize */1175}11761177/* reg 0x01 bit 2 video transfert on */1178reg_w(gspca_dev, 0x01, ®s[0x01], 1);1179/* reg 0x17 SensorClk enable inv Clk 0x60 */1180reg_w(gspca_dev, 0x17, ®s[0x17], 1);1181/* Set the registers from the template */1182reg_w(gspca_dev, 0x01, ®s[0x01],1183(sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);11841185/* Init the sensor */1186i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,1187sensor_data[sd->sensor].sensor_init_size);11881189/* Mode / bridge specific sensor setup */1190switch (sd->sensor) {1191case SENSOR_PAS202: {1192const __u8 i2cpclockdiv[] =1193{0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};1194/* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */1195if (mode)1196i2c_w(gspca_dev, i2cpclockdiv);1197break;1198}1199case SENSOR_OV7630:1200/* FIXME / TESTME We should be able to handle this identical1201for the 101/102 and the 103 case */1202if (sd->bridge == BRIDGE_103) {1203const __u8 i2c[] = { 0xa0, 0x21, 0x13,12040x80, 0x00, 0x00, 0x00, 0x10 };1205i2c_w(gspca_dev, i2c);1206}1207break;1208}1209/* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */1210reg_w(gspca_dev, 0x15, ®s[0x15], 2);1211/* compression register */1212reg_w(gspca_dev, 0x18, ®s[0x18], 1);1213/* H_start */1214reg_w(gspca_dev, 0x12, ®s[0x12], 1);1215/* V_START */1216reg_w(gspca_dev, 0x13, ®s[0x13], 1);1217/* reset 0x17 SensorClk enable inv Clk 0x60 */1218/*fixme: ov7630 [17]=68 8f (+20 if 102)*/1219reg_w(gspca_dev, 0x17, ®s[0x17], 1);1220/*MCKSIZE ->3 */ /*fixme: not ov7630*/1221reg_w(gspca_dev, 0x19, ®s[0x19], 1);1222/* AE_STRX AE_STRY AE_ENDX AE_ENDY */1223reg_w(gspca_dev, 0x1c, ®s[0x1c], 4);1224/* Enable video transfert */1225reg_w(gspca_dev, 0x01, ®s[0x01], 1);1226/* Compression */1227reg_w(gspca_dev, 0x18, ®s[0x18], 2);1228msleep(20);12291230sd->reg11 = -1;12311232setgain(gspca_dev);1233setbrightness(gspca_dev);1234setexposure(gspca_dev);1235setfreq(gspca_dev);12361237sd->frames_to_drop = 0;1238sd->autogain_ignore_frames = 0;1239sd->exp_too_high_cnt = 0;1240sd->exp_too_low_cnt = 0;1241atomic_set(&sd->avg_lum, -1);1242return 0;1243}12441245static void sd_stopN(struct gspca_dev *gspca_dev)1246{1247sd_init(gspca_dev);1248}12491250static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)1251{1252struct sd *sd = (struct sd *) gspca_dev;1253int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;12541255/* frames start with:1256* ff ff 00 c4 c4 96 synchro1257* 00 (unknown)1258* xx (frame sequence / size / compression)1259* (xx) (idem - extra byte for sn9c103)1260* ll mm brightness sum inside auto exposure1261* ll mm brightness sum outside auto exposure1262* (xx xx xx xx xx) audio values for snc1031263*/1264for (i = 0; i < len; i++) {1265switch (sd->header_read) {1266case 0:1267if (data[i] == 0xff)1268sd->header_read++;1269break;1270case 1:1271if (data[i] == 0xff)1272sd->header_read++;1273else1274sd->header_read = 0;1275break;1276case 2:1277if (data[i] == 0x00)1278sd->header_read++;1279else if (data[i] != 0xff)1280sd->header_read = 0;1281break;1282case 3:1283if (data[i] == 0xc4)1284sd->header_read++;1285else if (data[i] == 0xff)1286sd->header_read = 1;1287else1288sd->header_read = 0;1289break;1290case 4:1291if (data[i] == 0xc4)1292sd->header_read++;1293else if (data[i] == 0xff)1294sd->header_read = 1;1295else1296sd->header_read = 0;1297break;1298case 5:1299if (data[i] == 0x96)1300sd->header_read++;1301else if (data[i] == 0xff)1302sd->header_read = 1;1303else1304sd->header_read = 0;1305break;1306default:1307sd->header[sd->header_read - 6] = data[i];1308sd->header_read++;1309if (sd->header_read == header_size) {1310sd->header_read = 0;1311return data + i + 1;1312}1313}1314}1315return NULL;1316}13171318static void sd_pkt_scan(struct gspca_dev *gspca_dev,1319u8 *data, /* isoc packet */1320int len) /* iso packet length */1321{1322int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;1323struct sd *sd = (struct sd *) gspca_dev;1324struct cam *cam = &gspca_dev->cam;1325u8 *sof;13261327sof = find_sof(gspca_dev, data, len);1328if (sof) {1329if (sd->bridge == BRIDGE_103) {1330fr_h_sz = 18;1331lum_offset = 3;1332} else {1333fr_h_sz = 12;1334lum_offset = 2;1335}13361337len_after_sof = len - (sof - data);1338len = (sof - data) - fr_h_sz;1339if (len < 0)1340len = 0;1341}13421343if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {1344/* In raw mode we sometimes get some garbage after the frame1345ignore this */1346int used;1347int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;13481349used = gspca_dev->image_len;1350if (used + len > size)1351len = size - used;1352}13531354gspca_frame_add(gspca_dev, INTER_PACKET, data, len);13551356if (sof) {1357int lum = sd->header[lum_offset] +1358(sd->header[lum_offset + 1] << 8);13591360/* When exposure changes midway a frame we1361get a lum of 0 in this case drop 2 frames1362as the frames directly after an exposure1363change have an unstable image. Sometimes lum1364*really* is 0 (cam used in low light with1365low exposure setting), so do not drop frames1366if the previous lum was 0 too. */1367if (lum == 0 && sd->prev_avg_lum != 0) {1368lum = -1;1369sd->frames_to_drop = 2;1370sd->prev_avg_lum = 0;1371} else1372sd->prev_avg_lum = lum;1373atomic_set(&sd->avg_lum, lum);13741375if (sd->frames_to_drop)1376sd->frames_to_drop--;1377else1378gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);13791380gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);1381}1382}13831384static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)1385{1386struct sd *sd = (struct sd *) gspca_dev;13871388sd->ctrls[AUTOGAIN].val = val;1389sd->exp_too_high_cnt = 0;1390sd->exp_too_low_cnt = 0;13911392/* when switching to autogain set defaults to make sure1393we are on a valid point of the autogain gain /1394exposure knee graph, and give this change time to1395take effect before doing autogain. */1396if (sd->ctrls[AUTOGAIN].val1397&& !(sensor_data[sd->sensor].flags & F_COARSE_EXPO)) {1398sd->ctrls[EXPOSURE].val = sd->ctrls[EXPOSURE].def;1399sd->ctrls[GAIN].val = sd->ctrls[GAIN].def;1400if (gspca_dev->streaming) {1401sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;1402setexposure(gspca_dev);1403setgain(gspca_dev);1404}1405}14061407if (sd->ctrls[AUTOGAIN].val)1408gspca_dev->ctrl_inac = (1 << GAIN) | (1 << EXPOSURE);1409else1410gspca_dev->ctrl_inac = 0;14111412return 0;1413}14141415static int sd_querymenu(struct gspca_dev *gspca_dev,1416struct v4l2_querymenu *menu)1417{1418switch (menu->id) {1419case V4L2_CID_POWER_LINE_FREQUENCY:1420switch (menu->index) {1421case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */1422strcpy((char *) menu->name, "NoFliker");1423return 0;1424case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */1425strcpy((char *) menu->name, "50 Hz");1426return 0;1427case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */1428strcpy((char *) menu->name, "60 Hz");1429return 0;1430}1431break;1432}1433return -EINVAL;1434}14351436#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)1437static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,1438u8 *data, /* interrupt packet data */1439int len) /* interrupt packet length */1440{1441int ret = -EINVAL;14421443if (len == 1 && data[0] == 1) {1444input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);1445input_sync(gspca_dev->input_dev);1446input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);1447input_sync(gspca_dev->input_dev);1448ret = 0;1449}14501451return ret;1452}1453#endif14541455/* sub-driver description */1456static const struct sd_desc sd_desc = {1457.name = MODULE_NAME,1458.ctrls = sd_ctrls,1459.nctrls = ARRAY_SIZE(sd_ctrls),1460.config = sd_config,1461.init = sd_init,1462.start = sd_start,1463.stopN = sd_stopN,1464.pkt_scan = sd_pkt_scan,1465.querymenu = sd_querymenu,1466.dq_callback = do_autogain,1467#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)1468.int_pkt_scan = sd_int_pkt_scan,1469#endif1470};14711472/* -- module initialisation -- */1473#define SB(sensor, bridge) \1474.driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge147514761477static const struct usb_device_id device_table[] = {1478{USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */1479{USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */1480{USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */1481{USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},1482{USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},1483{USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},1484{USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},1485#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE1486{USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},1487{USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},1488#endif1489{USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},1490{USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},1491{USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},1492/* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */1493{USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},1494{USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},1495{USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},1496/* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */1497/* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */1498{USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},1499{USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},1500/* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */1501{USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},1502{USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},1503{USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},1504{USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},1505{USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},1506{}1507};1508MODULE_DEVICE_TABLE(usb, device_table);15091510/* -- device connect -- */1511static int sd_probe(struct usb_interface *intf,1512const struct usb_device_id *id)1513{1514return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),1515THIS_MODULE);1516}15171518static struct usb_driver sd_driver = {1519.name = MODULE_NAME,1520.id_table = device_table,1521.probe = sd_probe,1522.disconnect = gspca_disconnect,1523#ifdef CONFIG_PM1524.suspend = gspca_suspend,1525.resume = gspca_resume,1526#endif1527};15281529/* -- module insert / remove -- */1530static int __init sd_mod_init(void)1531{1532return usb_register(&sd_driver);1533}1534static void __exit sd_mod_exit(void)1535{1536usb_deregister(&sd_driver);1537}15381539module_init(sd_mod_init);1540module_exit(sd_mod_exit);154115421543