Path: blob/master/drivers/media/video/gspca/sn9c20x.c
17687 views
/*1* Sonix sn9c201 sn9c202 library2* Copyright (C) 2008-2009 microdia project <[email protected]>3* Copyright (C) 2009 Brian Johnson <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920#include <linux/input.h>2122#include "gspca.h"23#include "jpeg.h"2425#include <media/v4l2-chip-ident.h>26#include <linux/dmi.h>2728MODULE_AUTHOR("Brian Johnson <[email protected]>, "29"microdia project <[email protected]>");30MODULE_DESCRIPTION("GSPCA/SN9C20X USB Camera Driver");31MODULE_LICENSE("GPL");3233#define MODULE_NAME "sn9c20x"3435/*36* Pixel format private data37*/38#define SCALE_MASK 0x0f39#define SCALE_160x120 040#define SCALE_320x240 141#define SCALE_640x480 242#define SCALE_1280x1024 343#define MODE_RAW 0x1044#define MODE_JPEG 0x2045#define MODE_SXGA 0x804647#define SENSOR_OV9650 048#define SENSOR_OV9655 149#define SENSOR_SOI968 250#define SENSOR_OV7660 351#define SENSOR_OV7670 452#define SENSOR_MT9V011 553#define SENSOR_MT9V111 654#define SENSOR_MT9V112 755#define SENSOR_MT9M001 856#define SENSOR_MT9M111 957#define SENSOR_MT9M112 1058#define SENSOR_HV7131R 1159#define SENSOR_MT9VPRB 206061/* camera flags */62#define HAS_NO_BUTTON 0x163#define LED_REVERSE 0x2 /* some cameras unset gpio to turn on leds */64#define FLIP_DETECT 0x46566/* specific webcam descriptor */67struct sd {68struct gspca_dev gspca_dev;6970#define MIN_AVG_LUM 8071#define MAX_AVG_LUM 13072atomic_t avg_lum;73u8 old_step;74u8 older_step;75u8 exposure_step;7677u8 brightness;78u8 contrast;79u8 saturation;80s16 hue;81u8 gamma;82u8 red;83u8 blue;8485u8 hflip;86u8 vflip;87u8 gain;88u16 exposure;89u8 auto_exposure;9091u8 i2c_addr;92u8 sensor;93u8 hstart;94u8 vstart;9596u8 jpeg_hdr[JPEG_HDR_SZ];97u8 quality;9899u8 flags;100};101102struct i2c_reg_u8 {103u8 reg;104u8 val;105};106107struct i2c_reg_u16 {108u8 reg;109u16 val;110};111112static int sd_setbrightness(struct gspca_dev *gspca_dev, s32 val);113static int sd_getbrightness(struct gspca_dev *gspca_dev, s32 *val);114static int sd_setcontrast(struct gspca_dev *gspca_dev, s32 val);115static int sd_getcontrast(struct gspca_dev *gspca_dev, s32 *val);116static int sd_setsaturation(struct gspca_dev *gspca_dev, s32 val);117static int sd_getsaturation(struct gspca_dev *gspca_dev, s32 *val);118static int sd_sethue(struct gspca_dev *gspca_dev, s32 val);119static int sd_gethue(struct gspca_dev *gspca_dev, s32 *val);120static int sd_setgamma(struct gspca_dev *gspca_dev, s32 val);121static int sd_getgamma(struct gspca_dev *gspca_dev, s32 *val);122static int sd_setredbalance(struct gspca_dev *gspca_dev, s32 val);123static int sd_getredbalance(struct gspca_dev *gspca_dev, s32 *val);124static int sd_setbluebalance(struct gspca_dev *gspca_dev, s32 val);125static int sd_getbluebalance(struct gspca_dev *gspca_dev, s32 *val);126static int sd_setvflip(struct gspca_dev *gspca_dev, s32 val);127static int sd_getvflip(struct gspca_dev *gspca_dev, s32 *val);128static int sd_sethflip(struct gspca_dev *gspca_dev, s32 val);129static int sd_gethflip(struct gspca_dev *gspca_dev, s32 *val);130static int sd_setgain(struct gspca_dev *gspca_dev, s32 val);131static int sd_getgain(struct gspca_dev *gspca_dev, s32 *val);132static int sd_setexposure(struct gspca_dev *gspca_dev, s32 val);133static int sd_getexposure(struct gspca_dev *gspca_dev, s32 *val);134static int sd_setautoexposure(struct gspca_dev *gspca_dev, s32 val);135static int sd_getautoexposure(struct gspca_dev *gspca_dev, s32 *val);136137static const struct dmi_system_id flip_dmi_table[] = {138{139.ident = "MSI MS-1034",140.matches = {141DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD."),142DMI_MATCH(DMI_PRODUCT_NAME, "MS-1034"),143DMI_MATCH(DMI_PRODUCT_VERSION, "0341")144}145},146{147.ident = "MSI MS-1632",148.matches = {149DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),150DMI_MATCH(DMI_BOARD_NAME, "MS-1632")151}152},153{154.ident = "MSI MS-1633X",155.matches = {156DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),157DMI_MATCH(DMI_BOARD_NAME, "MS-1633X")158}159},160{161.ident = "MSI MS-1635X",162.matches = {163DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),164DMI_MATCH(DMI_BOARD_NAME, "MS-1635X")165}166},167{168.ident = "ASUSTeK W7J",169.matches = {170DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),171DMI_MATCH(DMI_BOARD_NAME, "W7J ")172}173},174{}175};176177static const struct ctrl sd_ctrls[] = {178{179#define BRIGHTNESS_IDX 0180{181.id = V4L2_CID_BRIGHTNESS,182.type = V4L2_CTRL_TYPE_INTEGER,183.name = "Brightness",184.minimum = 0,185.maximum = 0xff,186.step = 1,187#define BRIGHTNESS_DEFAULT 0x7f188.default_value = BRIGHTNESS_DEFAULT,189},190.set = sd_setbrightness,191.get = sd_getbrightness,192},193{194#define CONTRAST_IDX 1195{196.id = V4L2_CID_CONTRAST,197.type = V4L2_CTRL_TYPE_INTEGER,198.name = "Contrast",199.minimum = 0,200.maximum = 0xff,201.step = 1,202#define CONTRAST_DEFAULT 0x7f203.default_value = CONTRAST_DEFAULT,204},205.set = sd_setcontrast,206.get = sd_getcontrast,207},208{209#define SATURATION_IDX 2210{211.id = V4L2_CID_SATURATION,212.type = V4L2_CTRL_TYPE_INTEGER,213.name = "Saturation",214.minimum = 0,215.maximum = 0xff,216.step = 1,217#define SATURATION_DEFAULT 0x7f218.default_value = SATURATION_DEFAULT,219},220.set = sd_setsaturation,221.get = sd_getsaturation,222},223{224#define HUE_IDX 3225{226.id = V4L2_CID_HUE,227.type = V4L2_CTRL_TYPE_INTEGER,228.name = "Hue",229.minimum = -180,230.maximum = 180,231.step = 1,232#define HUE_DEFAULT 0233.default_value = HUE_DEFAULT,234},235.set = sd_sethue,236.get = sd_gethue,237},238{239#define GAMMA_IDX 4240{241.id = V4L2_CID_GAMMA,242.type = V4L2_CTRL_TYPE_INTEGER,243.name = "Gamma",244.minimum = 0,245.maximum = 0xff,246.step = 1,247#define GAMMA_DEFAULT 0x10248.default_value = GAMMA_DEFAULT,249},250.set = sd_setgamma,251.get = sd_getgamma,252},253{254#define BLUE_IDX 5255{256.id = V4L2_CID_BLUE_BALANCE,257.type = V4L2_CTRL_TYPE_INTEGER,258.name = "Blue Balance",259.minimum = 0,260.maximum = 0x7f,261.step = 1,262#define BLUE_DEFAULT 0x28263.default_value = BLUE_DEFAULT,264},265.set = sd_setbluebalance,266.get = sd_getbluebalance,267},268{269#define RED_IDX 6270{271.id = V4L2_CID_RED_BALANCE,272.type = V4L2_CTRL_TYPE_INTEGER,273.name = "Red Balance",274.minimum = 0,275.maximum = 0x7f,276.step = 1,277#define RED_DEFAULT 0x28278.default_value = RED_DEFAULT,279},280.set = sd_setredbalance,281.get = sd_getredbalance,282},283{284#define HFLIP_IDX 7285{286.id = V4L2_CID_HFLIP,287.type = V4L2_CTRL_TYPE_BOOLEAN,288.name = "Horizontal Flip",289.minimum = 0,290.maximum = 1,291.step = 1,292#define HFLIP_DEFAULT 0293.default_value = HFLIP_DEFAULT,294},295.set = sd_sethflip,296.get = sd_gethflip,297},298{299#define VFLIP_IDX 8300{301.id = V4L2_CID_VFLIP,302.type = V4L2_CTRL_TYPE_BOOLEAN,303.name = "Vertical Flip",304.minimum = 0,305.maximum = 1,306.step = 1,307#define VFLIP_DEFAULT 0308.default_value = VFLIP_DEFAULT,309},310.set = sd_setvflip,311.get = sd_getvflip,312},313{314#define EXPOSURE_IDX 9315{316.id = V4L2_CID_EXPOSURE,317.type = V4L2_CTRL_TYPE_INTEGER,318.name = "Exposure",319.minimum = 0,320.maximum = 0x1780,321.step = 1,322#define EXPOSURE_DEFAULT 0x33323.default_value = EXPOSURE_DEFAULT,324},325.set = sd_setexposure,326.get = sd_getexposure,327},328{329#define GAIN_IDX 10330{331.id = V4L2_CID_GAIN,332.type = V4L2_CTRL_TYPE_INTEGER,333.name = "Gain",334.minimum = 0,335.maximum = 28,336.step = 1,337#define GAIN_DEFAULT 0x00338.default_value = GAIN_DEFAULT,339},340.set = sd_setgain,341.get = sd_getgain,342},343{344#define AUTOGAIN_IDX 11345{346.id = V4L2_CID_AUTOGAIN,347.type = V4L2_CTRL_TYPE_BOOLEAN,348.name = "Auto Exposure",349.minimum = 0,350.maximum = 1,351.step = 1,352#define AUTO_EXPOSURE_DEFAULT 1353.default_value = AUTO_EXPOSURE_DEFAULT,354},355.set = sd_setautoexposure,356.get = sd_getautoexposure,357},358};359360static const struct v4l2_pix_format vga_mode[] = {361{160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,362.bytesperline = 160,363.sizeimage = 160 * 120 * 4 / 8 + 590,364.colorspace = V4L2_COLORSPACE_JPEG,365.priv = SCALE_160x120 | MODE_JPEG},366{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,367.bytesperline = 160,368.sizeimage = 160 * 120,369.colorspace = V4L2_COLORSPACE_SRGB,370.priv = SCALE_160x120 | MODE_RAW},371{160, 120, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,372.bytesperline = 160,373.sizeimage = 240 * 120,374.colorspace = V4L2_COLORSPACE_SRGB,375.priv = SCALE_160x120},376{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,377.bytesperline = 320,378.sizeimage = 320 * 240 * 4 / 8 + 590,379.colorspace = V4L2_COLORSPACE_JPEG,380.priv = SCALE_320x240 | MODE_JPEG},381{320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,382.bytesperline = 320,383.sizeimage = 320 * 240 ,384.colorspace = V4L2_COLORSPACE_SRGB,385.priv = SCALE_320x240 | MODE_RAW},386{320, 240, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,387.bytesperline = 320,388.sizeimage = 480 * 240 ,389.colorspace = V4L2_COLORSPACE_SRGB,390.priv = SCALE_320x240},391{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,392.bytesperline = 640,393.sizeimage = 640 * 480 * 4 / 8 + 590,394.colorspace = V4L2_COLORSPACE_JPEG,395.priv = SCALE_640x480 | MODE_JPEG},396{640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,397.bytesperline = 640,398.sizeimage = 640 * 480,399.colorspace = V4L2_COLORSPACE_SRGB,400.priv = SCALE_640x480 | MODE_RAW},401{640, 480, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,402.bytesperline = 640,403.sizeimage = 960 * 480,404.colorspace = V4L2_COLORSPACE_SRGB,405.priv = SCALE_640x480},406};407408static const struct v4l2_pix_format sxga_mode[] = {409{160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,410.bytesperline = 160,411.sizeimage = 160 * 120 * 4 / 8 + 590,412.colorspace = V4L2_COLORSPACE_JPEG,413.priv = SCALE_160x120 | MODE_JPEG},414{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,415.bytesperline = 160,416.sizeimage = 160 * 120,417.colorspace = V4L2_COLORSPACE_SRGB,418.priv = SCALE_160x120 | MODE_RAW},419{160, 120, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,420.bytesperline = 160,421.sizeimage = 240 * 120,422.colorspace = V4L2_COLORSPACE_SRGB,423.priv = SCALE_160x120},424{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,425.bytesperline = 320,426.sizeimage = 320 * 240 * 4 / 8 + 590,427.colorspace = V4L2_COLORSPACE_JPEG,428.priv = SCALE_320x240 | MODE_JPEG},429{320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,430.bytesperline = 320,431.sizeimage = 320 * 240 ,432.colorspace = V4L2_COLORSPACE_SRGB,433.priv = SCALE_320x240 | MODE_RAW},434{320, 240, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,435.bytesperline = 320,436.sizeimage = 480 * 240 ,437.colorspace = V4L2_COLORSPACE_SRGB,438.priv = SCALE_320x240},439{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,440.bytesperline = 640,441.sizeimage = 640 * 480 * 4 / 8 + 590,442.colorspace = V4L2_COLORSPACE_JPEG,443.priv = SCALE_640x480 | MODE_JPEG},444{640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,445.bytesperline = 640,446.sizeimage = 640 * 480,447.colorspace = V4L2_COLORSPACE_SRGB,448.priv = SCALE_640x480 | MODE_RAW},449{640, 480, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,450.bytesperline = 640,451.sizeimage = 960 * 480,452.colorspace = V4L2_COLORSPACE_SRGB,453.priv = SCALE_640x480},454{1280, 1024, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,455.bytesperline = 1280,456.sizeimage = 1280 * 1024,457.colorspace = V4L2_COLORSPACE_SRGB,458.priv = SCALE_1280x1024 | MODE_RAW | MODE_SXGA},459};460461static const struct v4l2_pix_format mono_mode[] = {462{160, 120, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,463.bytesperline = 160,464.sizeimage = 160 * 120,465.colorspace = V4L2_COLORSPACE_SRGB,466.priv = SCALE_160x120 | MODE_RAW},467{320, 240, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,468.bytesperline = 320,469.sizeimage = 320 * 240 ,470.colorspace = V4L2_COLORSPACE_SRGB,471.priv = SCALE_320x240 | MODE_RAW},472{640, 480, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,473.bytesperline = 640,474.sizeimage = 640 * 480,475.colorspace = V4L2_COLORSPACE_SRGB,476.priv = SCALE_640x480 | MODE_RAW},477{1280, 1024, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,478.bytesperline = 1280,479.sizeimage = 1280 * 1024,480.colorspace = V4L2_COLORSPACE_SRGB,481.priv = SCALE_1280x1024 | MODE_RAW | MODE_SXGA},482};483484static const s16 hsv_red_x[] = {48541, 44, 46, 48, 50, 52, 54, 56,48658, 60, 62, 64, 66, 68, 70, 72,48774, 76, 78, 80, 81, 83, 85, 87,48888, 90, 92, 93, 95, 97, 98, 100,489101, 102, 104, 105, 107, 108, 109, 110,490112, 113, 114, 115, 116, 117, 118, 119,491120, 121, 122, 123, 123, 124, 125, 125,492126, 127, 127, 128, 128, 129, 129, 129,493130, 130, 130, 130, 131, 131, 131, 131,494131, 131, 131, 131, 130, 130, 130, 130,495129, 129, 129, 128, 128, 127, 127, 126,496125, 125, 124, 123, 122, 122, 121, 120,497119, 118, 117, 116, 115, 114, 112, 111,498110, 109, 107, 106, 105, 103, 102, 101,49999, 98, 96, 94, 93, 91, 90, 88,50086, 84, 83, 81, 79, 77, 75, 74,50172, 70, 68, 66, 64, 62, 60, 58,50256, 54, 52, 49, 47, 45, 43, 41,50339, 36, 34, 32, 30, 28, 25, 23,50421, 19, 16, 14, 12, 9, 7, 5,5053, 0, -1, -3, -6, -8, -10, -12,506-15, -17, -19, -22, -24, -26, -28, -30,507-33, -35, -37, -39, -41, -44, -46, -48,508-50, -52, -54, -56, -58, -60, -62, -64,509-66, -68, -70, -72, -74, -76, -78, -80,510-81, -83, -85, -87, -88, -90, -92, -93,511-95, -97, -98, -100, -101, -102, -104, -105,512-107, -108, -109, -110, -112, -113, -114, -115,513-116, -117, -118, -119, -120, -121, -122, -123,514-123, -124, -125, -125, -126, -127, -127, -128,515-128, -128, -128, -128, -128, -128, -128, -128,516-128, -128, -128, -128, -128, -128, -128, -128,517-128, -128, -128, -128, -128, -128, -128, -128,518-128, -127, -127, -126, -125, -125, -124, -123,519-122, -122, -121, -120, -119, -118, -117, -116,520-115, -114, -112, -111, -110, -109, -107, -106,521-105, -103, -102, -101, -99, -98, -96, -94,522-93, -91, -90, -88, -86, -84, -83, -81,523-79, -77, -75, -74, -72, -70, -68, -66,524-64, -62, -60, -58, -56, -54, -52, -49,525-47, -45, -43, -41, -39, -36, -34, -32,526-30, -28, -25, -23, -21, -19, -16, -14,527-12, -9, -7, -5, -3, 0, 1, 3,5286, 8, 10, 12, 15, 17, 19, 22,52924, 26, 28, 30, 33, 35, 37, 39, 41530};531532static const s16 hsv_red_y[] = {53382, 80, 78, 76, 74, 73, 71, 69,53467, 65, 63, 61, 58, 56, 54, 52,53550, 48, 46, 44, 41, 39, 37, 35,53632, 30, 28, 26, 23, 21, 19, 16,53714, 12, 10, 7, 5, 3, 0, -1,538-3, -6, -8, -10, -13, -15, -17, -19,539-22, -24, -26, -29, -31, -33, -35, -38,540-40, -42, -44, -46, -48, -51, -53, -55,541-57, -59, -61, -63, -65, -67, -69, -71,542-73, -75, -77, -79, -81, -82, -84, -86,543-88, -89, -91, -93, -94, -96, -98, -99,544-101, -102, -104, -105, -106, -108, -109, -110,545-112, -113, -114, -115, -116, -117, -119, -120,546-120, -121, -122, -123, -124, -125, -126, -126,547-127, -128, -128, -128, -128, -128, -128, -128,548-128, -128, -128, -128, -128, -128, -128, -128,549-128, -128, -128, -128, -128, -128, -128, -128,550-128, -128, -128, -128, -128, -128, -128, -128,551-127, -127, -126, -125, -125, -124, -123, -122,552-121, -120, -119, -118, -117, -116, -115, -114,553-113, -111, -110, -109, -107, -106, -105, -103,554-102, -100, -99, -97, -96, -94, -92, -91,555-89, -87, -85, -84, -82, -80, -78, -76,556-74, -73, -71, -69, -67, -65, -63, -61,557-58, -56, -54, -52, -50, -48, -46, -44,558-41, -39, -37, -35, -32, -30, -28, -26,559-23, -21, -19, -16, -14, -12, -10, -7,560-5, -3, 0, 1, 3, 6, 8, 10,56113, 15, 17, 19, 22, 24, 26, 29,56231, 33, 35, 38, 40, 42, 44, 46,56348, 51, 53, 55, 57, 59, 61, 63,56465, 67, 69, 71, 73, 75, 77, 79,56581, 82, 84, 86, 88, 89, 91, 93,56694, 96, 98, 99, 101, 102, 104, 105,567106, 108, 109, 110, 112, 113, 114, 115,568116, 117, 119, 120, 120, 121, 122, 123,569124, 125, 126, 126, 127, 128, 128, 129,570129, 130, 130, 131, 131, 131, 131, 132,571132, 132, 132, 132, 132, 132, 132, 132,572132, 132, 132, 131, 131, 131, 130, 130,573130, 129, 129, 128, 127, 127, 126, 125,574125, 124, 123, 122, 121, 120, 119, 118,575117, 116, 115, 114, 113, 111, 110, 109,576107, 106, 105, 103, 102, 100, 99, 97,57796, 94, 92, 91, 89, 87, 85, 84, 82578};579580static const s16 hsv_green_x[] = {581-124, -124, -125, -125, -125, -125, -125, -125,582-125, -126, -126, -125, -125, -125, -125, -125,583-125, -124, -124, -124, -123, -123, -122, -122,584-121, -121, -120, -120, -119, -118, -117, -117,585-116, -115, -114, -113, -112, -111, -110, -109,586-108, -107, -105, -104, -103, -102, -100, -99,587-98, -96, -95, -93, -92, -91, -89, -87,588-86, -84, -83, -81, -79, -77, -76, -74,589-72, -70, -69, -67, -65, -63, -61, -59,590-57, -55, -53, -51, -49, -47, -45, -43,591-41, -39, -37, -35, -33, -30, -28, -26,592-24, -22, -20, -18, -15, -13, -11, -9,593-7, -4, -2, 0, 1, 3, 6, 8,59410, 12, 14, 17, 19, 21, 23, 25,59527, 29, 32, 34, 36, 38, 40, 42,59644, 46, 48, 50, 52, 54, 56, 58,59760, 62, 64, 66, 68, 70, 71, 73,59875, 77, 78, 80, 82, 83, 85, 87,59988, 90, 91, 93, 94, 96, 97, 98,600100, 101, 102, 104, 105, 106, 107, 108,601109, 111, 112, 113, 113, 114, 115, 116,602117, 118, 118, 119, 120, 120, 121, 122,603122, 123, 123, 124, 124, 124, 125, 125,604125, 125, 125, 125, 125, 126, 126, 125,605125, 125, 125, 125, 125, 124, 124, 124,606123, 123, 122, 122, 121, 121, 120, 120,607119, 118, 117, 117, 116, 115, 114, 113,608112, 111, 110, 109, 108, 107, 105, 104,609103, 102, 100, 99, 98, 96, 95, 93,61092, 91, 89, 87, 86, 84, 83, 81,61179, 77, 76, 74, 72, 70, 69, 67,61265, 63, 61, 59, 57, 55, 53, 51,61349, 47, 45, 43, 41, 39, 37, 35,61433, 30, 28, 26, 24, 22, 20, 18,61515, 13, 11, 9, 7, 4, 2, 0,616-1, -3, -6, -8, -10, -12, -14, -17,617-19, -21, -23, -25, -27, -29, -32, -34,618-36, -38, -40, -42, -44, -46, -48, -50,619-52, -54, -56, -58, -60, -62, -64, -66,620-68, -70, -71, -73, -75, -77, -78, -80,621-82, -83, -85, -87, -88, -90, -91, -93,622-94, -96, -97, -98, -100, -101, -102, -104,623-105, -106, -107, -108, -109, -111, -112, -113,624-113, -114, -115, -116, -117, -118, -118, -119,625-120, -120, -121, -122, -122, -123, -123, -124, -124626};627628static const s16 hsv_green_y[] = {629-100, -99, -98, -97, -95, -94, -93, -91,630-90, -89, -87, -86, -84, -83, -81, -80,631-78, -76, -75, -73, -71, -70, -68, -66,632-64, -63, -61, -59, -57, -55, -53, -51,633-49, -48, -46, -44, -42, -40, -38, -36,634-34, -32, -30, -27, -25, -23, -21, -19,635-17, -15, -13, -11, -9, -7, -4, -2,6360, 1, 3, 5, 7, 9, 11, 14,63716, 18, 20, 22, 24, 26, 28, 30,63832, 34, 36, 38, 40, 42, 44, 46,63948, 50, 52, 54, 56, 58, 59, 61,64063, 65, 67, 68, 70, 72, 74, 75,64177, 78, 80, 82, 83, 85, 86, 88,64289, 90, 92, 93, 95, 96, 97, 98,643100, 101, 102, 103, 104, 105, 106, 107,644108, 109, 110, 111, 112, 112, 113, 114,645115, 115, 116, 116, 117, 117, 118, 118,646119, 119, 119, 120, 120, 120, 120, 120,647121, 121, 121, 121, 121, 121, 120, 120,648120, 120, 120, 119, 119, 119, 118, 118,649117, 117, 116, 116, 115, 114, 114, 113,650112, 111, 111, 110, 109, 108, 107, 106,651105, 104, 103, 102, 100, 99, 98, 97,65295, 94, 93, 91, 90, 89, 87, 86,65384, 83, 81, 80, 78, 76, 75, 73,65471, 70, 68, 66, 64, 63, 61, 59,65557, 55, 53, 51, 49, 48, 46, 44,65642, 40, 38, 36, 34, 32, 30, 27,65725, 23, 21, 19, 17, 15, 13, 11,6589, 7, 4, 2, 0, -1, -3, -5,659-7, -9, -11, -14, -16, -18, -20, -22,660-24, -26, -28, -30, -32, -34, -36, -38,661-40, -42, -44, -46, -48, -50, -52, -54,662-56, -58, -59, -61, -63, -65, -67, -68,663-70, -72, -74, -75, -77, -78, -80, -82,664-83, -85, -86, -88, -89, -90, -92, -93,665-95, -96, -97, -98, -100, -101, -102, -103,666-104, -105, -106, -107, -108, -109, -110, -111,667-112, -112, -113, -114, -115, -115, -116, -116,668-117, -117, -118, -118, -119, -119, -119, -120,669-120, -120, -120, -120, -121, -121, -121, -121,670-121, -121, -120, -120, -120, -120, -120, -119,671-119, -119, -118, -118, -117, -117, -116, -116,672-115, -114, -114, -113, -112, -111, -111, -110,673-109, -108, -107, -106, -105, -104, -103, -102, -100674};675676static const s16 hsv_blue_x[] = {677112, 113, 114, 114, 115, 116, 117, 117,678118, 118, 119, 119, 120, 120, 120, 121,679121, 121, 122, 122, 122, 122, 122, 122,680122, 122, 122, 122, 122, 122, 121, 121,681121, 120, 120, 120, 119, 119, 118, 118,682117, 116, 116, 115, 114, 113, 113, 112,683111, 110, 109, 108, 107, 106, 105, 104,684103, 102, 100, 99, 98, 97, 95, 94,68593, 91, 90, 88, 87, 85, 84, 82,68680, 79, 77, 76, 74, 72, 70, 69,68767, 65, 63, 61, 60, 58, 56, 54,68852, 50, 48, 46, 44, 42, 40, 38,68936, 34, 32, 30, 28, 26, 24, 22,69019, 17, 15, 13, 11, 9, 7, 5,6912, 0, -1, -3, -5, -7, -9, -12,692-14, -16, -18, -20, -22, -24, -26, -28,693-31, -33, -35, -37, -39, -41, -43, -45,694-47, -49, -51, -53, -54, -56, -58, -60,695-62, -64, -66, -67, -69, -71, -73, -74,696-76, -78, -79, -81, -83, -84, -86, -87,697-89, -90, -92, -93, -94, -96, -97, -98,698-99, -101, -102, -103, -104, -105, -106, -107,699-108, -109, -110, -111, -112, -113, -114, -114,700-115, -116, -117, -117, -118, -118, -119, -119,701-120, -120, -120, -121, -121, -121, -122, -122,702-122, -122, -122, -122, -122, -122, -122, -122,703-122, -122, -121, -121, -121, -120, -120, -120,704-119, -119, -118, -118, -117, -116, -116, -115,705-114, -113, -113, -112, -111, -110, -109, -108,706-107, -106, -105, -104, -103, -102, -100, -99,707-98, -97, -95, -94, -93, -91, -90, -88,708-87, -85, -84, -82, -80, -79, -77, -76,709-74, -72, -70, -69, -67, -65, -63, -61,710-60, -58, -56, -54, -52, -50, -48, -46,711-44, -42, -40, -38, -36, -34, -32, -30,712-28, -26, -24, -22, -19, -17, -15, -13,713-11, -9, -7, -5, -2, 0, 1, 3,7145, 7, 9, 12, 14, 16, 18, 20,71522, 24, 26, 28, 31, 33, 35, 37,71639, 41, 43, 45, 47, 49, 51, 53,71754, 56, 58, 60, 62, 64, 66, 67,71869, 71, 73, 74, 76, 78, 79, 81,71983, 84, 86, 87, 89, 90, 92, 93,72094, 96, 97, 98, 99, 101, 102, 103,721104, 105, 106, 107, 108, 109, 110, 111, 112722};723724static const s16 hsv_blue_y[] = {725-11, -13, -15, -17, -19, -21, -23, -25,726-27, -29, -31, -33, -35, -37, -39, -41,727-43, -45, -46, -48, -50, -52, -54, -55,728-57, -59, -61, -62, -64, -66, -67, -69,729-71, -72, -74, -75, -77, -78, -80, -81,730-83, -84, -86, -87, -88, -90, -91, -92,731-93, -95, -96, -97, -98, -99, -100, -101,732-102, -103, -104, -105, -106, -106, -107, -108,733-109, -109, -110, -111, -111, -112, -112, -113,734-113, -114, -114, -114, -115, -115, -115, -115,735-116, -116, -116, -116, -116, -116, -116, -116,736-116, -115, -115, -115, -115, -114, -114, -114,737-113, -113, -112, -112, -111, -111, -110, -110,738-109, -108, -108, -107, -106, -105, -104, -103,739-102, -101, -100, -99, -98, -97, -96, -95,740-94, -93, -91, -90, -89, -88, -86, -85,741-84, -82, -81, -79, -78, -76, -75, -73,742-71, -70, -68, -67, -65, -63, -62, -60,743-58, -56, -55, -53, -51, -49, -47, -45,744-44, -42, -40, -38, -36, -34, -32, -30,745-28, -26, -24, -22, -20, -18, -16, -14,746-12, -10, -8, -6, -4, -2, 0, 1,7473, 5, 7, 9, 11, 13, 15, 17,74819, 21, 23, 25, 27, 29, 31, 33,74935, 37, 39, 41, 43, 45, 46, 48,75050, 52, 54, 55, 57, 59, 61, 62,75164, 66, 67, 69, 71, 72, 74, 75,75277, 78, 80, 81, 83, 84, 86, 87,75388, 90, 91, 92, 93, 95, 96, 97,75498, 99, 100, 101, 102, 103, 104, 105,755106, 106, 107, 108, 109, 109, 110, 111,756111, 112, 112, 113, 113, 114, 114, 114,757115, 115, 115, 115, 116, 116, 116, 116,758116, 116, 116, 116, 116, 115, 115, 115,759115, 114, 114, 114, 113, 113, 112, 112,760111, 111, 110, 110, 109, 108, 108, 107,761106, 105, 104, 103, 102, 101, 100, 99,76298, 97, 96, 95, 94, 93, 91, 90,76389, 88, 86, 85, 84, 82, 81, 79,76478, 76, 75, 73, 71, 70, 68, 67,76565, 63, 62, 60, 58, 56, 55, 53,76651, 49, 47, 45, 44, 42, 40, 38,76736, 34, 32, 30, 28, 26, 24, 22,76820, 18, 16, 14, 12, 10, 8, 6,7694, 2, 0, -1, -3, -5, -7, -9, -11770};771772static u16 i2c_ident[] = {773V4L2_IDENT_OV9650,774V4L2_IDENT_OV9655,775V4L2_IDENT_SOI968,776V4L2_IDENT_OV7660,777V4L2_IDENT_OV7670,778V4L2_IDENT_MT9V011,779V4L2_IDENT_MT9V111,780V4L2_IDENT_MT9V112,781V4L2_IDENT_MT9M001C12ST,782V4L2_IDENT_MT9M111,783V4L2_IDENT_MT9M112,784V4L2_IDENT_HV7131R,785};786787static u16 bridge_init[][2] = {788{0x1000, 0x78}, {0x1001, 0x40}, {0x1002, 0x1c},789{0x1020, 0x80}, {0x1061, 0x01}, {0x1067, 0x40},790{0x1068, 0x30}, {0x1069, 0x20}, {0x106a, 0x10},791{0x106b, 0x08}, {0x1188, 0x87}, {0x11a1, 0x00},792{0x11a2, 0x00}, {0x11a3, 0x6a}, {0x11a4, 0x50},793{0x11ab, 0x00}, {0x11ac, 0x00}, {0x11ad, 0x50},794{0x11ae, 0x3c}, {0x118a, 0x04}, {0x0395, 0x04},795{0x11b8, 0x3a}, {0x118b, 0x0e}, {0x10f7, 0x05},796{0x10f8, 0x14}, {0x10fa, 0xff}, {0x10f9, 0x00},797{0x11ba, 0x0a}, {0x11a5, 0x2d}, {0x11a6, 0x2d},798{0x11a7, 0x3a}, {0x11a8, 0x05}, {0x11a9, 0x04},799{0x11aa, 0x3f}, {0x11af, 0x28}, {0x11b0, 0xd8},800{0x11b1, 0x14}, {0x11b2, 0xec}, {0x11b3, 0x32},801{0x11b4, 0xdd}, {0x11b5, 0x32}, {0x11b6, 0xdd},802{0x10e0, 0x2c}, {0x11bc, 0x40}, {0x11bd, 0x01},803{0x11be, 0xf0}, {0x11bf, 0x00}, {0x118c, 0x1f},804{0x118d, 0x1f}, {0x118e, 0x1f}, {0x118f, 0x1f},805{0x1180, 0x01}, {0x1181, 0x00}, {0x1182, 0x01},806{0x1183, 0x00}, {0x1184, 0x50}, {0x1185, 0x80},807{0x1007, 0x00}808};809810/* Gain = (bit[3:0] / 16 + 1) * (bit[4] + 1) * (bit[5] + 1) * (bit[6] + 1) */811static u8 ov_gain[] = {8120x00 /* 1x */, 0x04 /* 1.25x */, 0x08 /* 1.5x */, 0x0c /* 1.75x */,8130x10 /* 2x */, 0x12 /* 2.25x */, 0x14 /* 2.5x */, 0x16 /* 2.75x */,8140x18 /* 3x */, 0x1a /* 3.25x */, 0x1c /* 3.5x */, 0x1e /* 3.75x */,8150x30 /* 4x */, 0x31 /* 4.25x */, 0x32 /* 4.5x */, 0x33 /* 4.75x */,8160x34 /* 5x */, 0x35 /* 5.25x */, 0x36 /* 5.5x */, 0x37 /* 5.75x */,8170x38 /* 6x */, 0x39 /* 6.25x */, 0x3a /* 6.5x */, 0x3b /* 6.75x */,8180x3c /* 7x */, 0x3d /* 7.25x */, 0x3e /* 7.5x */, 0x3f /* 7.75x */,8190x70 /* 8x */820};821822/* Gain = (bit[8] + 1) * (bit[7] + 1) * (bit[6:0] * 0.03125) */823static u16 micron1_gain[] = {824/* 1x 1.25x 1.5x 1.75x */8250x0020, 0x0028, 0x0030, 0x0038,826/* 2x 2.25x 2.5x 2.75x */8270x00a0, 0x00a4, 0x00a8, 0x00ac,828/* 3x 3.25x 3.5x 3.75x */8290x00b0, 0x00b4, 0x00b8, 0x00bc,830/* 4x 4.25x 4.5x 4.75x */8310x00c0, 0x00c4, 0x00c8, 0x00cc,832/* 5x 5.25x 5.5x 5.75x */8330x00d0, 0x00d4, 0x00d8, 0x00dc,834/* 6x 6.25x 6.5x 6.75x */8350x00e0, 0x00e4, 0x00e8, 0x00ec,836/* 7x 7.25x 7.5x 7.75x */8370x00f0, 0x00f4, 0x00f8, 0x00fc,838/* 8x */8390x01c0840};841842/* mt9m001 sensor uses a different gain formula then other micron sensors */843/* Gain = (bit[6] + 1) * (bit[5-0] * 0.125) */844static u16 micron2_gain[] = {845/* 1x 1.25x 1.5x 1.75x */8460x0008, 0x000a, 0x000c, 0x000e,847/* 2x 2.25x 2.5x 2.75x */8480x0010, 0x0012, 0x0014, 0x0016,849/* 3x 3.25x 3.5x 3.75x */8500x0018, 0x001a, 0x001c, 0x001e,851/* 4x 4.25x 4.5x 4.75x */8520x0020, 0x0051, 0x0052, 0x0053,853/* 5x 5.25x 5.5x 5.75x */8540x0054, 0x0055, 0x0056, 0x0057,855/* 6x 6.25x 6.5x 6.75x */8560x0058, 0x0059, 0x005a, 0x005b,857/* 7x 7.25x 7.5x 7.75x */8580x005c, 0x005d, 0x005e, 0x005f,859/* 8x */8600x0060861};862863/* Gain = .5 + bit[7:0] / 16 */864static u8 hv7131r_gain[] = {8650x08 /* 1x */, 0x0c /* 1.25x */, 0x10 /* 1.5x */, 0x14 /* 1.75x */,8660x18 /* 2x */, 0x1c /* 2.25x */, 0x20 /* 2.5x */, 0x24 /* 2.75x */,8670x28 /* 3x */, 0x2c /* 3.25x */, 0x30 /* 3.5x */, 0x34 /* 3.75x */,8680x38 /* 4x */, 0x3c /* 4.25x */, 0x40 /* 4.5x */, 0x44 /* 4.75x */,8690x48 /* 5x */, 0x4c /* 5.25x */, 0x50 /* 5.5x */, 0x54 /* 5.75x */,8700x58 /* 6x */, 0x5c /* 6.25x */, 0x60 /* 6.5x */, 0x64 /* 6.75x */,8710x68 /* 7x */, 0x6c /* 7.25x */, 0x70 /* 7.5x */, 0x74 /* 7.75x */,8720x78 /* 8x */873};874875static struct i2c_reg_u8 soi968_init[] = {876{0x12, 0x80}, {0x0c, 0x00}, {0x0f, 0x1f},877{0x11, 0x80}, {0x38, 0x52}, {0x1e, 0x00},878{0x33, 0x08}, {0x35, 0x8c}, {0x36, 0x0c},879{0x37, 0x04}, {0x45, 0x04}, {0x47, 0xff},880{0x3e, 0x00}, {0x3f, 0x00}, {0x3b, 0x20},881{0x3a, 0x96}, {0x3d, 0x0a}, {0x14, 0x8e},882{0x13, 0x8b}, {0x12, 0x40}, {0x17, 0x13},883{0x18, 0x63}, {0x19, 0x01}, {0x1a, 0x79},884{0x32, 0x24}, {0x03, 0x00}, {0x11, 0x40},885{0x2a, 0x10}, {0x2b, 0xe0}, {0x10, 0x32},886{0x00, 0x00}, {0x01, 0x80}, {0x02, 0x80},887};888889static struct i2c_reg_u8 ov7660_init[] = {890{0x0e, 0x80}, {0x0d, 0x08}, {0x0f, 0xc3},891{0x04, 0xc3}, {0x10, 0x40}, {0x11, 0x40},892{0x12, 0x05}, {0x13, 0xba}, {0x14, 0x2a},893/* HDG Set hstart and hstop, datasheet default 0x11, 0x61, using8940x10, 0x61 and sd->hstart, vstart = 3, fixes ugly colored borders */895{0x17, 0x10}, {0x18, 0x61},896{0x37, 0x0f}, {0x38, 0x02}, {0x39, 0x43},897{0x3a, 0x00}, {0x69, 0x90}, {0x2d, 0xf6},898{0x2e, 0x0b}, {0x01, 0x78}, {0x02, 0x50},899};900901static struct i2c_reg_u8 ov7670_init[] = {902{0x12, 0x80}, {0x11, 0x80}, {0x3a, 0x04}, {0x12, 0x01},903{0x32, 0xb6}, {0x03, 0x0a}, {0x0c, 0x00}, {0x3e, 0x00},904{0x70, 0x3a}, {0x71, 0x35}, {0x72, 0x11}, {0x73, 0xf0},905{0xa2, 0x02}, {0x13, 0xe0}, {0x00, 0x00}, {0x10, 0x00},906{0x0d, 0x40}, {0x14, 0x28}, {0xa5, 0x05}, {0xab, 0x07},907{0x24, 0x95}, {0x25, 0x33}, {0x26, 0xe3}, {0x9f, 0x75},908{0xa0, 0x65}, {0xa1, 0x0b}, {0xa6, 0xd8}, {0xa7, 0xd8},909{0xa8, 0xf0}, {0xa9, 0x90}, {0xaa, 0x94}, {0x13, 0xe5},910{0x0e, 0x61}, {0x0f, 0x4b}, {0x16, 0x02}, {0x1e, 0x27},911{0x21, 0x02}, {0x22, 0x91}, {0x29, 0x07}, {0x33, 0x0b},912{0x35, 0x0b}, {0x37, 0x1d}, {0x38, 0x71}, {0x39, 0x2a},913{0x3c, 0x78}, {0x4d, 0x40}, {0x4e, 0x20}, {0x69, 0x00},914{0x74, 0x19}, {0x8d, 0x4f}, {0x8e, 0x00}, {0x8f, 0x00},915{0x90, 0x00}, {0x91, 0x00}, {0x96, 0x00}, {0x9a, 0x80},916{0xb0, 0x84}, {0xb1, 0x0c}, {0xb2, 0x0e}, {0xb3, 0x82},917{0xb8, 0x0a}, {0x43, 0x0a}, {0x44, 0xf0}, {0x45, 0x20},918{0x46, 0x7d}, {0x47, 0x29}, {0x48, 0x4a}, {0x59, 0x8c},919{0x5a, 0xa5}, {0x5b, 0xde}, {0x5c, 0x96}, {0x5d, 0x66},920{0x5e, 0x10}, {0x6c, 0x0a}, {0x6d, 0x55}, {0x6e, 0x11},921{0x6f, 0x9e}, {0x6a, 0x40}, {0x01, 0x40}, {0x02, 0x40},922{0x13, 0xe7}, {0x4f, 0x6e}, {0x50, 0x70}, {0x51, 0x02},923{0x52, 0x1d}, {0x53, 0x56}, {0x54, 0x73}, {0x55, 0x0a},924{0x56, 0x55}, {0x57, 0x80}, {0x58, 0x9e}, {0x41, 0x08},925{0x3f, 0x02}, {0x75, 0x03}, {0x76, 0x63}, {0x4c, 0x04},926{0x77, 0x06}, {0x3d, 0x02}, {0x4b, 0x09}, {0xc9, 0x30},927{0x41, 0x08}, {0x56, 0x48}, {0x34, 0x11}, {0xa4, 0x88},928{0x96, 0x00}, {0x97, 0x30}, {0x98, 0x20}, {0x99, 0x30},929{0x9a, 0x84}, {0x9b, 0x29}, {0x9c, 0x03}, {0x9d, 0x99},930{0x9e, 0x7f}, {0x78, 0x04}, {0x79, 0x01}, {0xc8, 0xf0},931{0x79, 0x0f}, {0xc8, 0x00}, {0x79, 0x10}, {0xc8, 0x7e},932{0x79, 0x0a}, {0xc8, 0x80}, {0x79, 0x0b}, {0xc8, 0x01},933{0x79, 0x0c}, {0xc8, 0x0f}, {0x79, 0x0d}, {0xc8, 0x20},934{0x79, 0x09}, {0xc8, 0x80}, {0x79, 0x02}, {0xc8, 0xc0},935{0x79, 0x03}, {0xc8, 0x40}, {0x79, 0x05}, {0xc8, 0x30},936{0x79, 0x26}, {0x62, 0x20}, {0x63, 0x00}, {0x64, 0x06},937{0x65, 0x00}, {0x66, 0x05}, {0x94, 0x05}, {0x95, 0x0a},938{0x17, 0x13}, {0x18, 0x01}, {0x19, 0x02}, {0x1a, 0x7a},939{0x46, 0x59}, {0x47, 0x30}, {0x58, 0x9a}, {0x59, 0x84},940{0x5a, 0x91}, {0x5b, 0x57}, {0x5c, 0x75}, {0x5d, 0x6d},941{0x5e, 0x13}, {0x64, 0x07}, {0x94, 0x07}, {0x95, 0x0d},942{0xa6, 0xdf}, {0xa7, 0xdf}, {0x48, 0x4d}, {0x51, 0x00},943{0x6b, 0x0a}, {0x11, 0x80}, {0x2a, 0x00}, {0x2b, 0x00},944{0x92, 0x00}, {0x93, 0x00}, {0x55, 0x0a}, {0x56, 0x60},945{0x4f, 0x6e}, {0x50, 0x70}, {0x51, 0x00}, {0x52, 0x1d},946{0x53, 0x56}, {0x54, 0x73}, {0x58, 0x9a}, {0x4f, 0x6e},947{0x50, 0x70}, {0x51, 0x00}, {0x52, 0x1d}, {0x53, 0x56},948{0x54, 0x73}, {0x58, 0x9a}, {0x3f, 0x01}, {0x7b, 0x03},949{0x7c, 0x09}, {0x7d, 0x16}, {0x7e, 0x38}, {0x7f, 0x47},950{0x80, 0x53}, {0x81, 0x5e}, {0x82, 0x6a}, {0x83, 0x74},951{0x84, 0x80}, {0x85, 0x8c}, {0x86, 0x9b}, {0x87, 0xb2},952{0x88, 0xcc}, {0x89, 0xe5}, {0x7a, 0x24}, {0x3b, 0x00},953{0x9f, 0x76}, {0xa0, 0x65}, {0x13, 0xe2}, {0x6b, 0x0a},954{0x11, 0x80}, {0x2a, 0x00}, {0x2b, 0x00}, {0x92, 0x00},955{0x93, 0x00},956};957958static struct i2c_reg_u8 ov9650_init[] = {959{0x12, 0x80}, {0x00, 0x00}, {0x01, 0x78},960{0x02, 0x78}, {0x03, 0x36}, {0x04, 0x03},961{0x05, 0x00}, {0x06, 0x00}, {0x08, 0x00},962{0x09, 0x01}, {0x0c, 0x00}, {0x0d, 0x00},963{0x0e, 0xa0}, {0x0f, 0x52}, {0x10, 0x7c},964{0x11, 0x80}, {0x12, 0x45}, {0x13, 0xc2},965{0x14, 0x2e}, {0x15, 0x00}, {0x16, 0x07},966{0x17, 0x24}, {0x18, 0xc5}, {0x19, 0x00},967{0x1a, 0x3c}, {0x1b, 0x00}, {0x1e, 0x04},968{0x1f, 0x00}, {0x24, 0x78}, {0x25, 0x68},969{0x26, 0xd4}, {0x27, 0x80}, {0x28, 0x80},970{0x29, 0x30}, {0x2a, 0x00}, {0x2b, 0x00},971{0x2c, 0x80}, {0x2d, 0x00}, {0x2e, 0x00},972{0x2f, 0x00}, {0x30, 0x08}, {0x31, 0x30},973{0x32, 0x84}, {0x33, 0xe2}, {0x34, 0xbf},974{0x35, 0x81}, {0x36, 0xf9}, {0x37, 0x00},975{0x38, 0x93}, {0x39, 0x50}, {0x3a, 0x01},976{0x3b, 0x01}, {0x3c, 0x73}, {0x3d, 0x19},977{0x3e, 0x0b}, {0x3f, 0x80}, {0x40, 0xc1},978{0x41, 0x00}, {0x42, 0x08}, {0x67, 0x80},979{0x68, 0x80}, {0x69, 0x40}, {0x6a, 0x00},980{0x6b, 0x0a}, {0x8b, 0x06}, {0x8c, 0x20},981{0x8d, 0x00}, {0x8e, 0x00}, {0x8f, 0xdf},982{0x92, 0x00}, {0x93, 0x00}, {0x94, 0x88},983{0x95, 0x88}, {0x96, 0x04}, {0xa1, 0x00},984{0xa5, 0x80}, {0xa8, 0x80}, {0xa9, 0xb8},985{0xaa, 0x92}, {0xab, 0x0a},986};987988static struct i2c_reg_u8 ov9655_init[] = {989{0x12, 0x80}, {0x0e, 0x61}, {0x11, 0x80}, {0x13, 0xba},990{0x14, 0x2e}, {0x16, 0x24}, {0x1e, 0x04}, {0x27, 0x08},991{0x28, 0x08}, {0x29, 0x15}, {0x2c, 0x08}, {0x34, 0x3d},992{0x35, 0x00}, {0x38, 0x12}, {0x0f, 0x42}, {0x39, 0x57},993{0x3a, 0x00}, {0x3b, 0xcc}, {0x3c, 0x0c}, {0x3d, 0x19},994{0x3e, 0x0c}, {0x3f, 0x01}, {0x41, 0x40}, {0x42, 0x80},995{0x45, 0x46}, {0x46, 0x62}, {0x47, 0x2a}, {0x48, 0x3c},996{0x4a, 0xf0}, {0x4b, 0xdc}, {0x4c, 0xdc}, {0x4d, 0xdc},997{0x4e, 0xdc}, {0x6c, 0x04}, {0x6f, 0x9e}, {0x70, 0x05},998{0x71, 0x78}, {0x77, 0x02}, {0x8a, 0x23}, {0x90, 0x7e},999{0x91, 0x7c}, {0x9f, 0x6e}, {0xa0, 0x6e}, {0xa5, 0x68},1000{0xa6, 0x60}, {0xa8, 0xc1}, {0xa9, 0xfa}, {0xaa, 0x92},1001{0xab, 0x04}, {0xac, 0x80}, {0xad, 0x80}, {0xae, 0x80},1002{0xaf, 0x80}, {0xb2, 0xf2}, {0xb3, 0x20}, {0xb5, 0x00},1003{0xb6, 0xaf}, {0xbb, 0xae}, {0xbc, 0x44}, {0xbd, 0x44},1004{0xbe, 0x3b}, {0xbf, 0x3a}, {0xc1, 0xc8}, {0xc2, 0x01},1005{0xc4, 0x00}, {0xc6, 0x85}, {0xc7, 0x81}, {0xc9, 0xe0},1006{0xca, 0xe8}, {0xcc, 0xd8}, {0xcd, 0x93}, {0x2d, 0x00},1007{0x2e, 0x00}, {0x01, 0x80}, {0x02, 0x80}, {0x12, 0x61},1008{0x36, 0xfa}, {0x8c, 0x8d}, {0xc0, 0xaa}, {0x69, 0x0a},1009{0x03, 0x09}, {0x17, 0x16}, {0x18, 0x6e}, {0x19, 0x01},1010{0x1a, 0x3e}, {0x32, 0x09}, {0x2a, 0x10}, {0x2b, 0x0a},1011{0x92, 0x00}, {0x93, 0x00}, {0xa1, 0x00}, {0x10, 0x7c},1012{0x04, 0x03}, {0x00, 0x13},1013};10141015static struct i2c_reg_u16 mt9v112_init[] = {1016{0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0020},1017{0x34, 0xc019}, {0x0a, 0x0011}, {0x0b, 0x000b},1018{0x20, 0x0703}, {0x35, 0x2022}, {0xf0, 0x0001},1019{0x05, 0x0000}, {0x06, 0x340c}, {0x3b, 0x042a},1020{0x3c, 0x0400}, {0xf0, 0x0002}, {0x2e, 0x0c58},1021{0x5b, 0x0001}, {0xc8, 0x9f0b}, {0xf0, 0x0001},1022{0x9b, 0x5300}, {0xf0, 0x0000}, {0x2b, 0x0020},1023{0x2c, 0x002a}, {0x2d, 0x0032}, {0x2e, 0x0020},1024{0x09, 0x01dc}, {0x01, 0x000c}, {0x02, 0x0020},1025{0x03, 0x01e0}, {0x04, 0x0280}, {0x06, 0x000c},1026{0x05, 0x0098}, {0x20, 0x0703}, {0x09, 0x01f2},1027{0x2b, 0x00a0}, {0x2c, 0x00a0}, {0x2d, 0x00a0},1028{0x2e, 0x00a0}, {0x01, 0x000c}, {0x02, 0x0020},1029{0x03, 0x01e0}, {0x04, 0x0280}, {0x06, 0x000c},1030{0x05, 0x0098}, {0x09, 0x01c1}, {0x2b, 0x00ae},1031{0x2c, 0x00ae}, {0x2d, 0x00ae}, {0x2e, 0x00ae},1032};10331034static struct i2c_reg_u16 mt9v111_init[] = {1035{0x01, 0x0004}, {0x0d, 0x0001}, {0x0d, 0x0000},1036{0x01, 0x0001}, {0x05, 0x0004}, {0x2d, 0xe0a0},1037{0x2e, 0x0c64}, {0x2f, 0x0064}, {0x06, 0x600e},1038{0x08, 0x0480}, {0x01, 0x0004}, {0x02, 0x0016},1039{0x03, 0x01e7}, {0x04, 0x0287}, {0x05, 0x0004},1040{0x06, 0x002d}, {0x07, 0x3002}, {0x08, 0x0008},1041{0x0e, 0x0008}, {0x20, 0x0000}1042};10431044static struct i2c_reg_u16 mt9v011_init[] = {1045{0x07, 0x0002}, {0x0d, 0x0001}, {0x0d, 0x0000},1046{0x01, 0x0008}, {0x02, 0x0016}, {0x03, 0x01e1},1047{0x04, 0x0281}, {0x05, 0x0083}, {0x06, 0x0006},1048{0x0d, 0x0002}, {0x0a, 0x0000}, {0x0b, 0x0000},1049{0x0c, 0x0000}, {0x0d, 0x0000}, {0x0e, 0x0000},1050{0x0f, 0x0000}, {0x10, 0x0000}, {0x11, 0x0000},1051{0x12, 0x0000}, {0x13, 0x0000}, {0x14, 0x0000},1052{0x15, 0x0000}, {0x16, 0x0000}, {0x17, 0x0000},1053{0x18, 0x0000}, {0x19, 0x0000}, {0x1a, 0x0000},1054{0x1b, 0x0000}, {0x1c, 0x0000}, {0x1d, 0x0000},1055{0x32, 0x0000}, {0x20, 0x1101}, {0x21, 0x0000},1056{0x22, 0x0000}, {0x23, 0x0000}, {0x24, 0x0000},1057{0x25, 0x0000}, {0x26, 0x0000}, {0x27, 0x0024},1058{0x2f, 0xf7b0}, {0x30, 0x0005}, {0x31, 0x0000},1059{0x32, 0x0000}, {0x33, 0x0000}, {0x34, 0x0100},1060{0x3d, 0x068f}, {0x40, 0x01e0}, {0x41, 0x00d1},1061{0x44, 0x0082}, {0x5a, 0x0000}, {0x5b, 0x0000},1062{0x5c, 0x0000}, {0x5d, 0x0000}, {0x5e, 0x0000},1063{0x5f, 0xa31d}, {0x62, 0x0611}, {0x0a, 0x0000},1064{0x06, 0x0029}, {0x05, 0x0009}, {0x20, 0x1101},1065{0x20, 0x1101}, {0x09, 0x0064}, {0x07, 0x0003},1066{0x2b, 0x0033}, {0x2c, 0x00a0}, {0x2d, 0x00a0},1067{0x2e, 0x0033}, {0x07, 0x0002}, {0x06, 0x0000},1068{0x06, 0x0029}, {0x05, 0x0009},1069};10701071static struct i2c_reg_u16 mt9m001_init[] = {1072{0x0d, 0x0001},1073{0x0d, 0x0000},1074{0x04, 0x0500}, /* hres = 1280 */1075{0x03, 0x0400}, /* vres = 1024 */1076{0x20, 0x1100},1077{0x06, 0x0010},1078{0x2b, 0x0024},1079{0x2e, 0x0024},1080{0x35, 0x0024},1081{0x2d, 0x0020},1082{0x2c, 0x0020},1083{0x09, 0x0ad4},1084{0x35, 0x0057},1085};10861087static struct i2c_reg_u16 mt9m111_init[] = {1088{0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0008},1089{0xf0, 0x0001}, {0x3a, 0x4300}, {0x9b, 0x4300},1090{0x06, 0x708e}, {0xf0, 0x0002}, {0x2e, 0x0a1e},1091{0xf0, 0x0000},1092};10931094static struct i2c_reg_u16 mt9m112_init[] = {1095{0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0008},1096{0xf0, 0x0001}, {0x3a, 0x4300}, {0x9b, 0x4300},1097{0x06, 0x708e}, {0xf0, 0x0002}, {0x2e, 0x0a1e},1098{0xf0, 0x0000},1099};11001101static struct i2c_reg_u8 hv7131r_init[] = {1102{0x02, 0x08}, {0x02, 0x00}, {0x01, 0x08},1103{0x02, 0x00}, {0x20, 0x00}, {0x21, 0xd0},1104{0x22, 0x00}, {0x23, 0x09}, {0x01, 0x08},1105{0x01, 0x08}, {0x01, 0x08}, {0x25, 0x07},1106{0x26, 0xc3}, {0x27, 0x50}, {0x30, 0x62},1107{0x31, 0x10}, {0x32, 0x06}, {0x33, 0x10},1108{0x20, 0x00}, {0x21, 0xd0}, {0x22, 0x00},1109{0x23, 0x09}, {0x01, 0x08},1110};11111112static int reg_r(struct gspca_dev *gspca_dev, u16 reg, u16 length)1113{1114struct usb_device *dev = gspca_dev->dev;1115int result;1116result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),11170x00,1118USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,1119reg,11200x00,1121gspca_dev->usb_buf,1122length,1123500);1124if (unlikely(result < 0 || result != length)) {1125err("Read register failed 0x%02X", reg);1126return -EIO;1127}1128return 0;1129}11301131static int reg_w(struct gspca_dev *gspca_dev, u16 reg,1132const u8 *buffer, int length)1133{1134struct usb_device *dev = gspca_dev->dev;1135int result;1136memcpy(gspca_dev->usb_buf, buffer, length);1137result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),11380x08,1139USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,1140reg,11410x00,1142gspca_dev->usb_buf,1143length,1144500);1145if (unlikely(result < 0 || result != length)) {1146err("Write register failed index 0x%02X", reg);1147return -EIO;1148}1149return 0;1150}11511152static int reg_w1(struct gspca_dev *gspca_dev, u16 reg, const u8 value)1153{1154u8 data[1] = {value};1155return reg_w(gspca_dev, reg, data, 1);1156}11571158static int i2c_w(struct gspca_dev *gspca_dev, const u8 *buffer)1159{1160int i;1161reg_w(gspca_dev, 0x10c0, buffer, 8);1162for (i = 0; i < 5; i++) {1163reg_r(gspca_dev, 0x10c0, 1);1164if (gspca_dev->usb_buf[0] & 0x04) {1165if (gspca_dev->usb_buf[0] & 0x08)1166return -EIO;1167return 0;1168}1169msleep(1);1170}1171return -EIO;1172}11731174static int i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)1175{1176struct sd *sd = (struct sd *) gspca_dev;11771178u8 row[8];11791180/*1181* from the point of view of the bridge, the length1182* includes the address1183*/1184row[0] = 0x81 | (2 << 4);1185row[1] = sd->i2c_addr;1186row[2] = reg;1187row[3] = val;1188row[4] = 0x00;1189row[5] = 0x00;1190row[6] = 0x00;1191row[7] = 0x10;11921193return i2c_w(gspca_dev, row);1194}11951196static int i2c_w2(struct gspca_dev *gspca_dev, u8 reg, u16 val)1197{1198struct sd *sd = (struct sd *) gspca_dev;1199u8 row[8];12001201/*1202* from the point of view of the bridge, the length1203* includes the address1204*/1205row[0] = 0x81 | (3 << 4);1206row[1] = sd->i2c_addr;1207row[2] = reg;1208row[3] = (val >> 8) & 0xff;1209row[4] = val & 0xff;1210row[5] = 0x00;1211row[6] = 0x00;1212row[7] = 0x10;12131214return i2c_w(gspca_dev, row);1215}12161217static int i2c_r1(struct gspca_dev *gspca_dev, u8 reg, u8 *val)1218{1219struct sd *sd = (struct sd *) gspca_dev;1220u8 row[8];12211222row[0] = 0x81 | (1 << 4);1223row[1] = sd->i2c_addr;1224row[2] = reg;1225row[3] = 0;1226row[4] = 0;1227row[5] = 0;1228row[6] = 0;1229row[7] = 0x10;1230if (i2c_w(gspca_dev, row) < 0)1231return -EIO;1232row[0] = 0x81 | (1 << 4) | 0x02;1233row[2] = 0;1234if (i2c_w(gspca_dev, row) < 0)1235return -EIO;1236if (reg_r(gspca_dev, 0x10c2, 5) < 0)1237return -EIO;1238*val = gspca_dev->usb_buf[4];1239return 0;1240}12411242static int i2c_r2(struct gspca_dev *gspca_dev, u8 reg, u16 *val)1243{1244struct sd *sd = (struct sd *) gspca_dev;1245u8 row[8];12461247row[0] = 0x81 | (1 << 4);1248row[1] = sd->i2c_addr;1249row[2] = reg;1250row[3] = 0;1251row[4] = 0;1252row[5] = 0;1253row[6] = 0;1254row[7] = 0x10;1255if (i2c_w(gspca_dev, row) < 0)1256return -EIO;1257row[0] = 0x81 | (2 << 4) | 0x02;1258row[2] = 0;1259if (i2c_w(gspca_dev, row) < 0)1260return -EIO;1261if (reg_r(gspca_dev, 0x10c2, 5) < 0)1262return -EIO;1263*val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];1264return 0;1265}12661267static int ov9650_init_sensor(struct gspca_dev *gspca_dev)1268{1269int i;1270u16 id;1271struct sd *sd = (struct sd *) gspca_dev;12721273if (i2c_r2(gspca_dev, 0x1c, &id) < 0)1274return -EINVAL;12751276if (id != 0x7fa2) {1277err("sensor id for ov9650 doesn't match (0x%04x)", id);1278return -ENODEV;1279}12801281for (i = 0; i < ARRAY_SIZE(ov9650_init); i++) {1282if (i2c_w1(gspca_dev, ov9650_init[i].reg,1283ov9650_init[i].val) < 0) {1284err("OV9650 sensor initialization failed");1285return -ENODEV;1286}1287}1288sd->hstart = 1;1289sd->vstart = 7;1290return 0;1291}12921293static int ov9655_init_sensor(struct gspca_dev *gspca_dev)1294{1295int i;1296struct sd *sd = (struct sd *) gspca_dev;12971298for (i = 0; i < ARRAY_SIZE(ov9655_init); i++) {1299if (i2c_w1(gspca_dev, ov9655_init[i].reg,1300ov9655_init[i].val) < 0) {1301err("OV9655 sensor initialization failed");1302return -ENODEV;1303}1304}1305/* disable hflip and vflip */1306gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX);1307sd->hstart = 1;1308sd->vstart = 2;1309return 0;1310}13111312static int soi968_init_sensor(struct gspca_dev *gspca_dev)1313{1314int i;1315struct sd *sd = (struct sd *) gspca_dev;13161317for (i = 0; i < ARRAY_SIZE(soi968_init); i++) {1318if (i2c_w1(gspca_dev, soi968_init[i].reg,1319soi968_init[i].val) < 0) {1320err("SOI968 sensor initialization failed");1321return -ENODEV;1322}1323}1324/* disable hflip and vflip */1325gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX)1326| (1 << EXPOSURE_IDX);1327sd->hstart = 60;1328sd->vstart = 11;1329return 0;1330}13311332static int ov7660_init_sensor(struct gspca_dev *gspca_dev)1333{1334int i;1335struct sd *sd = (struct sd *) gspca_dev;13361337for (i = 0; i < ARRAY_SIZE(ov7660_init); i++) {1338if (i2c_w1(gspca_dev, ov7660_init[i].reg,1339ov7660_init[i].val) < 0) {1340err("OV7660 sensor initialization failed");1341return -ENODEV;1342}1343}1344sd->hstart = 3;1345sd->vstart = 3;1346return 0;1347}13481349static int ov7670_init_sensor(struct gspca_dev *gspca_dev)1350{1351int i;1352struct sd *sd = (struct sd *) gspca_dev;13531354for (i = 0; i < ARRAY_SIZE(ov7670_init); i++) {1355if (i2c_w1(gspca_dev, ov7670_init[i].reg,1356ov7670_init[i].val) < 0) {1357err("OV7670 sensor initialization failed");1358return -ENODEV;1359}1360}1361/* disable hflip and vflip */1362gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX);1363sd->hstart = 0;1364sd->vstart = 1;1365return 0;1366}13671368static int mt9v_init_sensor(struct gspca_dev *gspca_dev)1369{1370struct sd *sd = (struct sd *) gspca_dev;1371int i;1372u16 value;1373int ret;13741375sd->i2c_addr = 0x5d;1376ret = i2c_r2(gspca_dev, 0xff, &value);1377if ((ret == 0) && (value == 0x8243)) {1378for (i = 0; i < ARRAY_SIZE(mt9v011_init); i++) {1379if (i2c_w2(gspca_dev, mt9v011_init[i].reg,1380mt9v011_init[i].val) < 0) {1381err("MT9V011 sensor initialization failed");1382return -ENODEV;1383}1384}1385sd->hstart = 2;1386sd->vstart = 2;1387sd->sensor = SENSOR_MT9V011;1388info("MT9V011 sensor detected");1389return 0;1390}13911392sd->i2c_addr = 0x5c;1393i2c_w2(gspca_dev, 0x01, 0x0004);1394ret = i2c_r2(gspca_dev, 0xff, &value);1395if ((ret == 0) && (value == 0x823a)) {1396for (i = 0; i < ARRAY_SIZE(mt9v111_init); i++) {1397if (i2c_w2(gspca_dev, mt9v111_init[i].reg,1398mt9v111_init[i].val) < 0) {1399err("MT9V111 sensor initialization failed");1400return -ENODEV;1401}1402}1403gspca_dev->ctrl_dis = (1 << EXPOSURE_IDX)1404| (1 << AUTOGAIN_IDX)1405| (1 << GAIN_IDX);1406sd->hstart = 2;1407sd->vstart = 2;1408sd->sensor = SENSOR_MT9V111;1409info("MT9V111 sensor detected");1410return 0;1411}14121413sd->i2c_addr = 0x5d;1414ret = i2c_w2(gspca_dev, 0xf0, 0x0000);1415if (ret < 0) {1416sd->i2c_addr = 0x48;1417i2c_w2(gspca_dev, 0xf0, 0x0000);1418}1419ret = i2c_r2(gspca_dev, 0x00, &value);1420if ((ret == 0) && (value == 0x1229)) {1421for (i = 0; i < ARRAY_SIZE(mt9v112_init); i++) {1422if (i2c_w2(gspca_dev, mt9v112_init[i].reg,1423mt9v112_init[i].val) < 0) {1424err("MT9V112 sensor initialization failed");1425return -ENODEV;1426}1427}1428sd->hstart = 6;1429sd->vstart = 2;1430sd->sensor = SENSOR_MT9V112;1431info("MT9V112 sensor detected");1432return 0;1433}14341435return -ENODEV;1436}14371438static int mt9m112_init_sensor(struct gspca_dev *gspca_dev)1439{1440struct sd *sd = (struct sd *) gspca_dev;1441int i;1442for (i = 0; i < ARRAY_SIZE(mt9m112_init); i++) {1443if (i2c_w2(gspca_dev, mt9m112_init[i].reg,1444mt9m112_init[i].val) < 0) {1445err("MT9M112 sensor initialization failed");1446return -ENODEV;1447}1448}1449gspca_dev->ctrl_dis = (1 << EXPOSURE_IDX) | (1 << AUTOGAIN_IDX)1450| (1 << GAIN_IDX);1451sd->hstart = 0;1452sd->vstart = 2;1453return 0;1454}14551456static int mt9m111_init_sensor(struct gspca_dev *gspca_dev)1457{1458struct sd *sd = (struct sd *) gspca_dev;1459int i;1460for (i = 0; i < ARRAY_SIZE(mt9m111_init); i++) {1461if (i2c_w2(gspca_dev, mt9m111_init[i].reg,1462mt9m111_init[i].val) < 0) {1463err("MT9M111 sensor initialization failed");1464return -ENODEV;1465}1466}1467gspca_dev->ctrl_dis = (1 << EXPOSURE_IDX) | (1 << AUTOGAIN_IDX)1468| (1 << GAIN_IDX);1469sd->hstart = 0;1470sd->vstart = 2;1471return 0;1472}14731474static int mt9m001_init_sensor(struct gspca_dev *gspca_dev)1475{1476struct sd *sd = (struct sd *) gspca_dev;1477int i;1478u16 id;14791480if (i2c_r2(gspca_dev, 0x00, &id) < 0)1481return -EINVAL;14821483/* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */1484switch (id) {1485case 0x8411:1486case 0x8421:1487info("MT9M001 color sensor detected");1488break;1489case 0x8431:1490info("MT9M001 mono sensor detected");1491break;1492default:1493err("No MT9M001 chip detected, ID = %x\n", id);1494return -ENODEV;1495}14961497for (i = 0; i < ARRAY_SIZE(mt9m001_init); i++) {1498if (i2c_w2(gspca_dev, mt9m001_init[i].reg,1499mt9m001_init[i].val) < 0) {1500err("MT9M001 sensor initialization failed");1501return -ENODEV;1502}1503}1504/* disable hflip and vflip */1505gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX);1506sd->hstart = 1;1507sd->vstart = 1;1508return 0;1509}15101511static int hv7131r_init_sensor(struct gspca_dev *gspca_dev)1512{1513int i;1514struct sd *sd = (struct sd *) gspca_dev;15151516for (i = 0; i < ARRAY_SIZE(hv7131r_init); i++) {1517if (i2c_w1(gspca_dev, hv7131r_init[i].reg,1518hv7131r_init[i].val) < 0) {1519err("HV7131R Sensor initialization failed");1520return -ENODEV;1521}1522}1523sd->hstart = 0;1524sd->vstart = 1;1525return 0;1526}15271528static int set_cmatrix(struct gspca_dev *gspca_dev)1529{1530struct sd *sd = (struct sd *) gspca_dev;1531s32 hue_coord, hue_index = 180 + sd->hue;1532u8 cmatrix[21];15331534memset(cmatrix, 0, sizeof cmatrix);1535cmatrix[2] = (sd->contrast * 0x25 / 0x100) + 0x26;1536cmatrix[0] = 0x13 + (cmatrix[2] - 0x26) * 0x13 / 0x25;1537cmatrix[4] = 0x07 + (cmatrix[2] - 0x26) * 0x07 / 0x25;1538cmatrix[18] = sd->brightness - 0x80;15391540hue_coord = (hsv_red_x[hue_index] * sd->saturation) >> 8;1541cmatrix[6] = hue_coord;1542cmatrix[7] = (hue_coord >> 8) & 0x0f;15431544hue_coord = (hsv_red_y[hue_index] * sd->saturation) >> 8;1545cmatrix[8] = hue_coord;1546cmatrix[9] = (hue_coord >> 8) & 0x0f;15471548hue_coord = (hsv_green_x[hue_index] * sd->saturation) >> 8;1549cmatrix[10] = hue_coord;1550cmatrix[11] = (hue_coord >> 8) & 0x0f;15511552hue_coord = (hsv_green_y[hue_index] * sd->saturation) >> 8;1553cmatrix[12] = hue_coord;1554cmatrix[13] = (hue_coord >> 8) & 0x0f;15551556hue_coord = (hsv_blue_x[hue_index] * sd->saturation) >> 8;1557cmatrix[14] = hue_coord;1558cmatrix[15] = (hue_coord >> 8) & 0x0f;15591560hue_coord = (hsv_blue_y[hue_index] * sd->saturation) >> 8;1561cmatrix[16] = hue_coord;1562cmatrix[17] = (hue_coord >> 8) & 0x0f;15631564return reg_w(gspca_dev, 0x10e1, cmatrix, 21);1565}15661567static int set_gamma(struct gspca_dev *gspca_dev)1568{1569struct sd *sd = (struct sd *) gspca_dev;1570u8 gamma[17];1571u8 gval = sd->gamma * 0xb8 / 0x100;157215731574gamma[0] = 0x0a;1575gamma[1] = 0x13 + (gval * (0xcb - 0x13) / 0xb8);1576gamma[2] = 0x25 + (gval * (0xee - 0x25) / 0xb8);1577gamma[3] = 0x37 + (gval * (0xfa - 0x37) / 0xb8);1578gamma[4] = 0x45 + (gval * (0xfc - 0x45) / 0xb8);1579gamma[5] = 0x55 + (gval * (0xfb - 0x55) / 0xb8);1580gamma[6] = 0x65 + (gval * (0xfc - 0x65) / 0xb8);1581gamma[7] = 0x74 + (gval * (0xfd - 0x74) / 0xb8);1582gamma[8] = 0x83 + (gval * (0xfe - 0x83) / 0xb8);1583gamma[9] = 0x92 + (gval * (0xfc - 0x92) / 0xb8);1584gamma[10] = 0xa1 + (gval * (0xfc - 0xa1) / 0xb8);1585gamma[11] = 0xb0 + (gval * (0xfc - 0xb0) / 0xb8);1586gamma[12] = 0xbf + (gval * (0xfb - 0xbf) / 0xb8);1587gamma[13] = 0xce + (gval * (0xfb - 0xce) / 0xb8);1588gamma[14] = 0xdf + (gval * (0xfd - 0xdf) / 0xb8);1589gamma[15] = 0xea + (gval * (0xf9 - 0xea) / 0xb8);1590gamma[16] = 0xf5;15911592return reg_w(gspca_dev, 0x1190, gamma, 17);1593}15941595static int set_redblue(struct gspca_dev *gspca_dev)1596{1597struct sd *sd = (struct sd *) gspca_dev;1598reg_w1(gspca_dev, 0x118c, sd->red);1599reg_w1(gspca_dev, 0x118f, sd->blue);1600return 0;1601}16021603static int set_hvflip(struct gspca_dev *gspca_dev)1604{1605u8 value, tslb, hflip, vflip;1606u16 value2;1607struct sd *sd = (struct sd *) gspca_dev;16081609if ((sd->flags & FLIP_DETECT) && dmi_check_system(flip_dmi_table)) {1610hflip = !sd->hflip;1611vflip = !sd->vflip;1612} else {1613hflip = sd->hflip;1614vflip = sd->vflip;1615}16161617switch (sd->sensor) {1618case SENSOR_OV7660:1619value = 0x01;1620if (hflip)1621value |= 0x20;1622if (vflip) {1623value |= 0x10;1624sd->vstart = 2;1625} else1626sd->vstart = 3;1627reg_w1(gspca_dev, 0x1182, sd->vstart);1628i2c_w1(gspca_dev, 0x1e, value);1629break;1630case SENSOR_OV9650:1631i2c_r1(gspca_dev, 0x1e, &value);1632value &= ~0x30;1633tslb = 0x01;1634if (hflip)1635value |= 0x20;1636if (vflip) {1637value |= 0x10;1638tslb = 0x49;1639}1640i2c_w1(gspca_dev, 0x1e, value);1641i2c_w1(gspca_dev, 0x3a, tslb);1642break;1643case SENSOR_MT9V111:1644case SENSOR_MT9V011:1645i2c_r2(gspca_dev, 0x20, &value2);1646value2 &= ~0xc0a0;1647if (hflip)1648value2 |= 0x8080;1649if (vflip)1650value2 |= 0x4020;1651i2c_w2(gspca_dev, 0x20, value2);1652break;1653case SENSOR_MT9M112:1654case SENSOR_MT9M111:1655case SENSOR_MT9V112:1656i2c_r2(gspca_dev, 0x20, &value2);1657value2 &= ~0x0003;1658if (hflip)1659value2 |= 0x0002;1660if (vflip)1661value2 |= 0x0001;1662i2c_w2(gspca_dev, 0x20, value2);1663break;1664case SENSOR_HV7131R:1665i2c_r1(gspca_dev, 0x01, &value);1666value &= ~0x03;1667if (vflip)1668value |= 0x01;1669if (hflip)1670value |= 0x02;1671i2c_w1(gspca_dev, 0x01, value);1672break;1673}1674return 0;1675}16761677static int set_exposure(struct gspca_dev *gspca_dev)1678{1679struct sd *sd = (struct sd *) gspca_dev;1680u8 exp[8] = {0x81, sd->i2c_addr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e};1681switch (sd->sensor) {1682case SENSOR_OV7660:1683case SENSOR_OV7670:1684case SENSOR_OV9655:1685case SENSOR_OV9650:1686exp[0] |= (3 << 4);1687exp[2] = 0x2d;1688exp[3] = sd->exposure & 0xff;1689exp[4] = sd->exposure >> 8;1690break;1691case SENSOR_MT9M001:1692case SENSOR_MT9V112:1693case SENSOR_MT9V011:1694exp[0] |= (3 << 4);1695exp[2] = 0x09;1696exp[3] = sd->exposure >> 8;1697exp[4] = sd->exposure & 0xff;1698break;1699case SENSOR_HV7131R:1700exp[0] |= (4 << 4);1701exp[2] = 0x25;1702exp[3] = (sd->exposure >> 5) & 0xff;1703exp[4] = (sd->exposure << 3) & 0xff;1704exp[5] = 0;1705break;1706default:1707return 0;1708}1709i2c_w(gspca_dev, exp);1710return 0;1711}17121713static int set_gain(struct gspca_dev *gspca_dev)1714{1715struct sd *sd = (struct sd *) gspca_dev;1716u8 gain[8] = {0x81, sd->i2c_addr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d};1717switch (sd->sensor) {1718case SENSOR_OV7660:1719case SENSOR_OV7670:1720case SENSOR_SOI968:1721case SENSOR_OV9655:1722case SENSOR_OV9650:1723gain[0] |= (2 << 4);1724gain[3] = ov_gain[sd->gain];1725break;1726case SENSOR_MT9V011:1727gain[0] |= (3 << 4);1728gain[2] = 0x35;1729gain[3] = micron1_gain[sd->gain] >> 8;1730gain[4] = micron1_gain[sd->gain] & 0xff;1731break;1732case SENSOR_MT9V112:1733gain[0] |= (3 << 4);1734gain[2] = 0x2f;1735gain[3] = micron1_gain[sd->gain] >> 8;1736gain[4] = micron1_gain[sd->gain] & 0xff;1737break;1738case SENSOR_MT9M001:1739gain[0] |= (3 << 4);1740gain[2] = 0x2f;1741gain[3] = micron2_gain[sd->gain] >> 8;1742gain[4] = micron2_gain[sd->gain] & 0xff;1743break;1744case SENSOR_HV7131R:1745gain[0] |= (2 << 4);1746gain[2] = 0x30;1747gain[3] = hv7131r_gain[sd->gain];1748break;1749default:1750return 0;1751}1752i2c_w(gspca_dev, gain);1753return 0;1754}17551756static int sd_setbrightness(struct gspca_dev *gspca_dev, s32 val)1757{1758struct sd *sd = (struct sd *) gspca_dev;17591760sd->brightness = val;1761if (gspca_dev->streaming)1762return set_cmatrix(gspca_dev);1763return 0;1764}17651766static int sd_getbrightness(struct gspca_dev *gspca_dev, s32 *val)1767{1768struct sd *sd = (struct sd *) gspca_dev;1769*val = sd->brightness;1770return 0;1771}177217731774static int sd_setcontrast(struct gspca_dev *gspca_dev, s32 val)1775{1776struct sd *sd = (struct sd *) gspca_dev;17771778sd->contrast = val;1779if (gspca_dev->streaming)1780return set_cmatrix(gspca_dev);1781return 0;1782}17831784static int sd_getcontrast(struct gspca_dev *gspca_dev, s32 *val)1785{1786struct sd *sd = (struct sd *) gspca_dev;1787*val = sd->contrast;1788return 0;1789}17901791static int sd_setsaturation(struct gspca_dev *gspca_dev, s32 val)1792{1793struct sd *sd = (struct sd *) gspca_dev;17941795sd->saturation = val;1796if (gspca_dev->streaming)1797return set_cmatrix(gspca_dev);1798return 0;1799}18001801static int sd_getsaturation(struct gspca_dev *gspca_dev, s32 *val)1802{1803struct sd *sd = (struct sd *) gspca_dev;1804*val = sd->saturation;1805return 0;1806}18071808static int sd_sethue(struct gspca_dev *gspca_dev, s32 val)1809{1810struct sd *sd = (struct sd *) gspca_dev;18111812sd->hue = val;1813if (gspca_dev->streaming)1814return set_cmatrix(gspca_dev);1815return 0;1816}18171818static int sd_gethue(struct gspca_dev *gspca_dev, s32 *val)1819{1820struct sd *sd = (struct sd *) gspca_dev;1821*val = sd->hue;1822return 0;1823}18241825static int sd_setgamma(struct gspca_dev *gspca_dev, s32 val)1826{1827struct sd *sd = (struct sd *) gspca_dev;18281829sd->gamma = val;1830if (gspca_dev->streaming)1831return set_gamma(gspca_dev);1832return 0;1833}18341835static int sd_getgamma(struct gspca_dev *gspca_dev, s32 *val)1836{1837struct sd *sd = (struct sd *) gspca_dev;1838*val = sd->gamma;1839return 0;1840}18411842static int sd_setredbalance(struct gspca_dev *gspca_dev, s32 val)1843{1844struct sd *sd = (struct sd *) gspca_dev;18451846sd->red = val;1847if (gspca_dev->streaming)1848return set_redblue(gspca_dev);1849return 0;1850}18511852static int sd_getredbalance(struct gspca_dev *gspca_dev, s32 *val)1853{1854struct sd *sd = (struct sd *) gspca_dev;1855*val = sd->red;1856return 0;1857}18581859static int sd_setbluebalance(struct gspca_dev *gspca_dev, s32 val)1860{1861struct sd *sd = (struct sd *) gspca_dev;18621863sd->blue = val;1864if (gspca_dev->streaming)1865return set_redblue(gspca_dev);1866return 0;1867}18681869static int sd_getbluebalance(struct gspca_dev *gspca_dev, s32 *val)1870{1871struct sd *sd = (struct sd *) gspca_dev;1872*val = sd->blue;1873return 0;1874}18751876static int sd_sethflip(struct gspca_dev *gspca_dev, s32 val)1877{1878struct sd *sd = (struct sd *) gspca_dev;18791880sd->hflip = val;1881if (gspca_dev->streaming)1882return set_hvflip(gspca_dev);1883return 0;1884}18851886static int sd_gethflip(struct gspca_dev *gspca_dev, s32 *val)1887{1888struct sd *sd = (struct sd *) gspca_dev;1889*val = sd->hflip;1890return 0;1891}18921893static int sd_setvflip(struct gspca_dev *gspca_dev, s32 val)1894{1895struct sd *sd = (struct sd *) gspca_dev;18961897sd->vflip = val;1898if (gspca_dev->streaming)1899return set_hvflip(gspca_dev);1900return 0;1901}19021903static int sd_getvflip(struct gspca_dev *gspca_dev, s32 *val)1904{1905struct sd *sd = (struct sd *) gspca_dev;1906*val = sd->vflip;1907return 0;1908}19091910static int sd_setexposure(struct gspca_dev *gspca_dev, s32 val)1911{1912struct sd *sd = (struct sd *) gspca_dev;19131914sd->exposure = val;1915if (gspca_dev->streaming)1916return set_exposure(gspca_dev);1917return 0;1918}19191920static int sd_getexposure(struct gspca_dev *gspca_dev, s32 *val)1921{1922struct sd *sd = (struct sd *) gspca_dev;1923*val = sd->exposure;1924return 0;1925}19261927static int sd_setgain(struct gspca_dev *gspca_dev, s32 val)1928{1929struct sd *sd = (struct sd *) gspca_dev;19301931sd->gain = val;1932if (gspca_dev->streaming)1933return set_gain(gspca_dev);1934return 0;1935}19361937static int sd_getgain(struct gspca_dev *gspca_dev, s32 *val)1938{1939struct sd *sd = (struct sd *) gspca_dev;1940*val = sd->gain;1941return 0;1942}19431944static int sd_setautoexposure(struct gspca_dev *gspca_dev, s32 val)1945{1946struct sd *sd = (struct sd *) gspca_dev;1947sd->auto_exposure = val;1948return 0;1949}19501951static int sd_getautoexposure(struct gspca_dev *gspca_dev, s32 *val)1952{1953struct sd *sd = (struct sd *) gspca_dev;1954*val = sd->auto_exposure;1955return 0;1956}19571958#ifdef CONFIG_VIDEO_ADV_DEBUG1959static int sd_dbg_g_register(struct gspca_dev *gspca_dev,1960struct v4l2_dbg_register *reg)1961{1962struct sd *sd = (struct sd *) gspca_dev;1963switch (reg->match.type) {1964case V4L2_CHIP_MATCH_HOST:1965if (reg->match.addr != 0)1966return -EINVAL;1967if (reg->reg < 0x1000 || reg->reg > 0x11ff)1968return -EINVAL;1969if (reg_r(gspca_dev, reg->reg, 1) < 0)1970return -EINVAL;1971reg->val = gspca_dev->usb_buf[0];1972return 0;1973case V4L2_CHIP_MATCH_I2C_ADDR:1974if (reg->match.addr != sd->i2c_addr)1975return -EINVAL;1976if (sd->sensor >= SENSOR_MT9V011 &&1977sd->sensor <= SENSOR_MT9M112) {1978if (i2c_r2(gspca_dev, reg->reg, (u16 *)®->val) < 0)1979return -EINVAL;1980} else {1981if (i2c_r1(gspca_dev, reg->reg, (u8 *)®->val) < 0)1982return -EINVAL;1983}1984return 0;1985}1986return -EINVAL;1987}19881989static int sd_dbg_s_register(struct gspca_dev *gspca_dev,1990struct v4l2_dbg_register *reg)1991{1992struct sd *sd = (struct sd *) gspca_dev;1993switch (reg->match.type) {1994case V4L2_CHIP_MATCH_HOST:1995if (reg->match.addr != 0)1996return -EINVAL;1997if (reg->reg < 0x1000 || reg->reg > 0x11ff)1998return -EINVAL;1999if (reg_w1(gspca_dev, reg->reg, reg->val) < 0)2000return -EINVAL;2001return 0;2002case V4L2_CHIP_MATCH_I2C_ADDR:2003if (reg->match.addr != sd->i2c_addr)2004return -EINVAL;2005if (sd->sensor >= SENSOR_MT9V011 &&2006sd->sensor <= SENSOR_MT9M112) {2007if (i2c_w2(gspca_dev, reg->reg, reg->val) < 0)2008return -EINVAL;2009} else {2010if (i2c_w1(gspca_dev, reg->reg, reg->val) < 0)2011return -EINVAL;2012}2013return 0;2014}2015return -EINVAL;2016}2017#endif20182019static int sd_chip_ident(struct gspca_dev *gspca_dev,2020struct v4l2_dbg_chip_ident *chip)2021{2022struct sd *sd = (struct sd *) gspca_dev;20232024switch (chip->match.type) {2025case V4L2_CHIP_MATCH_HOST:2026if (chip->match.addr != 0)2027return -EINVAL;2028chip->revision = 0;2029chip->ident = V4L2_IDENT_SN9C20X;2030return 0;2031case V4L2_CHIP_MATCH_I2C_ADDR:2032if (chip->match.addr != sd->i2c_addr)2033return -EINVAL;2034chip->revision = 0;2035chip->ident = i2c_ident[sd->sensor];2036return 0;2037}2038return -EINVAL;2039}20402041static int sd_config(struct gspca_dev *gspca_dev,2042const struct usb_device_id *id)2043{2044struct sd *sd = (struct sd *) gspca_dev;2045struct cam *cam;20462047cam = &gspca_dev->cam;20482049sd->sensor = (id->driver_info >> 8) & 0xff;2050sd->i2c_addr = id->driver_info & 0xff;2051sd->flags = (id->driver_info >> 16) & 0xff;20522053switch (sd->sensor) {2054case SENSOR_MT9M112:2055case SENSOR_MT9M111:2056case SENSOR_OV9650:2057case SENSOR_SOI968:2058cam->cam_mode = sxga_mode;2059cam->nmodes = ARRAY_SIZE(sxga_mode);2060break;2061case SENSOR_MT9M001:2062cam->cam_mode = mono_mode;2063cam->nmodes = ARRAY_SIZE(mono_mode);2064break;2065default:2066cam->cam_mode = vga_mode;2067cam->nmodes = ARRAY_SIZE(vga_mode);2068break;2069}20702071sd->old_step = 0;2072sd->older_step = 0;2073sd->exposure_step = 16;20742075sd->brightness = BRIGHTNESS_DEFAULT;2076sd->contrast = CONTRAST_DEFAULT;2077sd->saturation = SATURATION_DEFAULT;2078sd->hue = HUE_DEFAULT;2079sd->gamma = GAMMA_DEFAULT;2080sd->red = RED_DEFAULT;2081sd->blue = BLUE_DEFAULT;20822083sd->hflip = HFLIP_DEFAULT;2084sd->vflip = VFLIP_DEFAULT;2085sd->exposure = EXPOSURE_DEFAULT;2086sd->gain = GAIN_DEFAULT;2087sd->auto_exposure = AUTO_EXPOSURE_DEFAULT;20882089sd->quality = 95;20902091return 0;2092}20932094static int sd_init(struct gspca_dev *gspca_dev)2095{2096struct sd *sd = (struct sd *) gspca_dev;2097int i;2098u8 value;2099u8 i2c_init[9] =2100{0x80, sd->i2c_addr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};21012102for (i = 0; i < ARRAY_SIZE(bridge_init); i++) {2103value = bridge_init[i][1];2104if (reg_w(gspca_dev, bridge_init[i][0], &value, 1) < 0) {2105err("Device initialization failed");2106return -ENODEV;2107}2108}21092110if (sd->flags & LED_REVERSE)2111reg_w1(gspca_dev, 0x1006, 0x00);2112else2113reg_w1(gspca_dev, 0x1006, 0x20);21142115if (reg_w(gspca_dev, 0x10c0, i2c_init, 9) < 0) {2116err("Device initialization failed");2117return -ENODEV;2118}21192120switch (sd->sensor) {2121case SENSOR_OV9650:2122if (ov9650_init_sensor(gspca_dev) < 0)2123return -ENODEV;2124info("OV9650 sensor detected");2125break;2126case SENSOR_OV9655:2127if (ov9655_init_sensor(gspca_dev) < 0)2128return -ENODEV;2129info("OV9655 sensor detected");2130break;2131case SENSOR_SOI968:2132if (soi968_init_sensor(gspca_dev) < 0)2133return -ENODEV;2134info("SOI968 sensor detected");2135break;2136case SENSOR_OV7660:2137if (ov7660_init_sensor(gspca_dev) < 0)2138return -ENODEV;2139info("OV7660 sensor detected");2140break;2141case SENSOR_OV7670:2142if (ov7670_init_sensor(gspca_dev) < 0)2143return -ENODEV;2144info("OV7670 sensor detected");2145break;2146case SENSOR_MT9VPRB:2147if (mt9v_init_sensor(gspca_dev) < 0)2148return -ENODEV;2149break;2150case SENSOR_MT9M111:2151if (mt9m111_init_sensor(gspca_dev) < 0)2152return -ENODEV;2153info("MT9M111 sensor detected");2154break;2155case SENSOR_MT9M112:2156if (mt9m112_init_sensor(gspca_dev) < 0)2157return -ENODEV;2158info("MT9M112 sensor detected");2159break;2160case SENSOR_MT9M001:2161if (mt9m001_init_sensor(gspca_dev) < 0)2162return -ENODEV;2163break;2164case SENSOR_HV7131R:2165if (hv7131r_init_sensor(gspca_dev) < 0)2166return -ENODEV;2167info("HV7131R sensor detected");2168break;2169default:2170info("Unsupported Sensor");2171return -ENODEV;2172}21732174return 0;2175}21762177static void configure_sensor_output(struct gspca_dev *gspca_dev, int mode)2178{2179struct sd *sd = (struct sd *) gspca_dev;2180u8 value;2181switch (sd->sensor) {2182case SENSOR_SOI968:2183if (mode & MODE_SXGA) {2184i2c_w1(gspca_dev, 0x17, 0x1d);2185i2c_w1(gspca_dev, 0x18, 0xbd);2186i2c_w1(gspca_dev, 0x19, 0x01);2187i2c_w1(gspca_dev, 0x1a, 0x81);2188i2c_w1(gspca_dev, 0x12, 0x00);2189sd->hstart = 140;2190sd->vstart = 19;2191} else {2192i2c_w1(gspca_dev, 0x17, 0x13);2193i2c_w1(gspca_dev, 0x18, 0x63);2194i2c_w1(gspca_dev, 0x19, 0x01);2195i2c_w1(gspca_dev, 0x1a, 0x79);2196i2c_w1(gspca_dev, 0x12, 0x40);2197sd->hstart = 60;2198sd->vstart = 11;2199}2200break;2201case SENSOR_OV9650:2202if (mode & MODE_SXGA) {2203i2c_w1(gspca_dev, 0x17, 0x1b);2204i2c_w1(gspca_dev, 0x18, 0xbc);2205i2c_w1(gspca_dev, 0x19, 0x01);2206i2c_w1(gspca_dev, 0x1a, 0x82);2207i2c_r1(gspca_dev, 0x12, &value);2208i2c_w1(gspca_dev, 0x12, value & 0x07);2209} else {2210i2c_w1(gspca_dev, 0x17, 0x24);2211i2c_w1(gspca_dev, 0x18, 0xc5);2212i2c_w1(gspca_dev, 0x19, 0x00);2213i2c_w1(gspca_dev, 0x1a, 0x3c);2214i2c_r1(gspca_dev, 0x12, &value);2215i2c_w1(gspca_dev, 0x12, (value & 0x7) | 0x40);2216}2217break;2218case SENSOR_MT9M112:2219case SENSOR_MT9M111:2220if (mode & MODE_SXGA) {2221i2c_w2(gspca_dev, 0xf0, 0x0002);2222i2c_w2(gspca_dev, 0xc8, 0x970b);2223i2c_w2(gspca_dev, 0xf0, 0x0000);2224} else {2225i2c_w2(gspca_dev, 0xf0, 0x0002);2226i2c_w2(gspca_dev, 0xc8, 0x8000);2227i2c_w2(gspca_dev, 0xf0, 0x0000);2228}2229break;2230}2231}22322233#define HW_WIN(mode, hstart, vstart) \2234((const u8 []){hstart, 0, vstart, 0, \2235(mode & MODE_SXGA ? 1280 >> 4 : 640 >> 4), \2236(mode & MODE_SXGA ? 1024 >> 3 : 480 >> 3)})22372238#define CLR_WIN(width, height) \2239((const u8 [])\2240{0, width >> 2, 0, height >> 1,\2241((width >> 10) & 0x01) | ((height >> 8) & 0x6)})22422243static int sd_start(struct gspca_dev *gspca_dev)2244{2245struct sd *sd = (struct sd *) gspca_dev;2246int mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;2247int width = gspca_dev->width;2248int height = gspca_dev->height;2249u8 fmt, scale = 0;22502251jpeg_define(sd->jpeg_hdr, height, width,22520x21);2253jpeg_set_qual(sd->jpeg_hdr, sd->quality);22542255if (mode & MODE_RAW)2256fmt = 0x2d;2257else if (mode & MODE_JPEG)2258fmt = 0x2c;2259else2260fmt = 0x2f; /* YUV 420 */22612262switch (mode & SCALE_MASK) {2263case SCALE_1280x1024:2264scale = 0xc0;2265info("Set 1280x1024");2266break;2267case SCALE_640x480:2268scale = 0x80;2269info("Set 640x480");2270break;2271case SCALE_320x240:2272scale = 0x90;2273info("Set 320x240");2274break;2275case SCALE_160x120:2276scale = 0xa0;2277info("Set 160x120");2278break;2279}22802281configure_sensor_output(gspca_dev, mode);2282reg_w(gspca_dev, 0x1100, &sd->jpeg_hdr[JPEG_QT0_OFFSET], 64);2283reg_w(gspca_dev, 0x1140, &sd->jpeg_hdr[JPEG_QT1_OFFSET], 64);2284reg_w(gspca_dev, 0x10fb, CLR_WIN(width, height), 5);2285reg_w(gspca_dev, 0x1180, HW_WIN(mode, sd->hstart, sd->vstart), 6);2286reg_w1(gspca_dev, 0x1189, scale);2287reg_w1(gspca_dev, 0x10e0, fmt);22882289set_cmatrix(gspca_dev);2290set_gamma(gspca_dev);2291set_redblue(gspca_dev);2292set_gain(gspca_dev);2293set_exposure(gspca_dev);2294set_hvflip(gspca_dev);22952296reg_w1(gspca_dev, 0x1007, 0x20);22972298reg_r(gspca_dev, 0x1061, 1);2299reg_w1(gspca_dev, 0x1061, gspca_dev->usb_buf[0] | 0x02);2300return 0;2301}23022303static void sd_stopN(struct gspca_dev *gspca_dev)2304{2305reg_w1(gspca_dev, 0x1007, 0x00);23062307reg_r(gspca_dev, 0x1061, 1);2308reg_w1(gspca_dev, 0x1061, gspca_dev->usb_buf[0] & ~0x02);2309}23102311static void do_autoexposure(struct gspca_dev *gspca_dev, u16 avg_lum)2312{2313struct sd *sd = (struct sd *) gspca_dev;2314s16 new_exp;23152316/*2317* some hardcoded values are present2318* like those for maximal/minimal exposure2319* and exposure steps2320*/2321if (avg_lum < MIN_AVG_LUM) {2322if (sd->exposure > 0x1770)2323return;23242325new_exp = sd->exposure + sd->exposure_step;2326if (new_exp > 0x1770)2327new_exp = 0x1770;2328if (new_exp < 0x10)2329new_exp = 0x10;2330sd->exposure = new_exp;2331set_exposure(gspca_dev);23322333sd->older_step = sd->old_step;2334sd->old_step = 1;23352336if (sd->old_step ^ sd->older_step)2337sd->exposure_step /= 2;2338else2339sd->exposure_step += 2;2340}2341if (avg_lum > MAX_AVG_LUM) {2342if (sd->exposure < 0x10)2343return;2344new_exp = sd->exposure - sd->exposure_step;2345if (new_exp > 0x1700)2346new_exp = 0x1770;2347if (new_exp < 0x10)2348new_exp = 0x10;2349sd->exposure = new_exp;2350set_exposure(gspca_dev);2351sd->older_step = sd->old_step;2352sd->old_step = 0;23532354if (sd->old_step ^ sd->older_step)2355sd->exposure_step /= 2;2356else2357sd->exposure_step += 2;2358}2359}23602361static void do_autogain(struct gspca_dev *gspca_dev, u16 avg_lum)2362{2363struct sd *sd = (struct sd *) gspca_dev;23642365if (avg_lum < MIN_AVG_LUM) {2366if (sd->gain + 1 <= 28) {2367sd->gain++;2368set_gain(gspca_dev);2369}2370}2371if (avg_lum > MAX_AVG_LUM) {2372if (sd->gain > 0) {2373sd->gain--;2374set_gain(gspca_dev);2375}2376}2377}23782379static void sd_dqcallback(struct gspca_dev *gspca_dev)2380{2381struct sd *sd = (struct sd *) gspca_dev;2382int avg_lum;23832384if (!sd->auto_exposure)2385return;23862387avg_lum = atomic_read(&sd->avg_lum);2388if (sd->sensor == SENSOR_SOI968)2389do_autogain(gspca_dev, avg_lum);2390else2391do_autoexposure(gspca_dev, avg_lum);2392}23932394#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)2395static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,2396u8 *data, /* interrupt packet */2397int len) /* interrupt packet length */2398{2399struct sd *sd = (struct sd *) gspca_dev;2400int ret = -EINVAL;2401if (!(sd->flags & HAS_NO_BUTTON) && len == 1) {2402input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);2403input_sync(gspca_dev->input_dev);2404input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);2405input_sync(gspca_dev->input_dev);2406ret = 0;2407}2408return ret;2409}2410#endif24112412static void sd_pkt_scan(struct gspca_dev *gspca_dev,2413u8 *data, /* isoc packet */2414int len) /* iso packet length */2415{2416struct sd *sd = (struct sd *) gspca_dev;2417int avg_lum;2418static u8 frame_header[] =2419{0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};2420if (len == 64 && memcmp(data, frame_header, 6) == 0) {2421avg_lum = ((data[35] >> 2) & 3) |2422(data[20] << 2) |2423(data[19] << 10);2424avg_lum += ((data[35] >> 4) & 3) |2425(data[22] << 2) |2426(data[21] << 10);2427avg_lum += ((data[35] >> 6) & 3) |2428(data[24] << 2) |2429(data[23] << 10);2430avg_lum += (data[36] & 3) |2431(data[26] << 2) |2432(data[25] << 10);2433avg_lum += ((data[36] >> 2) & 3) |2434(data[28] << 2) |2435(data[27] << 10);2436avg_lum += ((data[36] >> 4) & 3) |2437(data[30] << 2) |2438(data[29] << 10);2439avg_lum += ((data[36] >> 6) & 3) |2440(data[32] << 2) |2441(data[31] << 10);2442avg_lum += ((data[44] >> 4) & 3) |2443(data[34] << 2) |2444(data[33] << 10);2445avg_lum >>= 9;2446atomic_set(&sd->avg_lum, avg_lum);2447gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);2448return;2449}2450if (gspca_dev->last_packet_type == LAST_PACKET) {2451if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv2452& MODE_JPEG) {2453gspca_frame_add(gspca_dev, FIRST_PACKET,2454sd->jpeg_hdr, JPEG_HDR_SZ);2455gspca_frame_add(gspca_dev, INTER_PACKET,2456data, len);2457} else {2458gspca_frame_add(gspca_dev, FIRST_PACKET,2459data, len);2460}2461} else {2462gspca_frame_add(gspca_dev, INTER_PACKET, data, len);2463}2464}24652466/* sub-driver description */2467static const struct sd_desc sd_desc = {2468.name = MODULE_NAME,2469.ctrls = sd_ctrls,2470.nctrls = ARRAY_SIZE(sd_ctrls),2471.config = sd_config,2472.init = sd_init,2473.start = sd_start,2474.stopN = sd_stopN,2475.pkt_scan = sd_pkt_scan,2476#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)2477.int_pkt_scan = sd_int_pkt_scan,2478#endif2479.dq_callback = sd_dqcallback,2480#ifdef CONFIG_VIDEO_ADV_DEBUG2481.set_register = sd_dbg_s_register,2482.get_register = sd_dbg_g_register,2483#endif2484.get_chip_ident = sd_chip_ident,2485};24862487#define SN9C20X(sensor, i2c_addr, flags) \2488.driver_info = ((flags & 0xff) << 16) \2489| (SENSOR_ ## sensor << 8) \2490| (i2c_addr)24912492static const struct usb_device_id device_table[] = {2493{USB_DEVICE(0x0c45, 0x6240), SN9C20X(MT9M001, 0x5d, 0)},2494{USB_DEVICE(0x0c45, 0x6242), SN9C20X(MT9M111, 0x5d, 0)},2495{USB_DEVICE(0x0c45, 0x6248), SN9C20X(OV9655, 0x30, 0)},2496{USB_DEVICE(0x0c45, 0x624c), SN9C20X(MT9M112, 0x5d, 0)},2497{USB_DEVICE(0x0c45, 0x624e), SN9C20X(SOI968, 0x30, LED_REVERSE)},2498{USB_DEVICE(0x0c45, 0x624f), SN9C20X(OV9650, 0x30,2499(FLIP_DETECT | HAS_NO_BUTTON))},2500{USB_DEVICE(0x0c45, 0x6251), SN9C20X(OV9650, 0x30, 0)},2501{USB_DEVICE(0x0c45, 0x6253), SN9C20X(OV9650, 0x30, 0)},2502{USB_DEVICE(0x0c45, 0x6260), SN9C20X(OV7670, 0x21, 0)},2503{USB_DEVICE(0x0c45, 0x6270), SN9C20X(MT9VPRB, 0x00, 0)},2504{USB_DEVICE(0x0c45, 0x627b), SN9C20X(OV7660, 0x21, FLIP_DETECT)},2505{USB_DEVICE(0x0c45, 0x627c), SN9C20X(HV7131R, 0x11, 0)},2506{USB_DEVICE(0x0c45, 0x627f), SN9C20X(OV9650, 0x30, 0)},2507{USB_DEVICE(0x0c45, 0x6280), SN9C20X(MT9M001, 0x5d, 0)},2508{USB_DEVICE(0x0c45, 0x6282), SN9C20X(MT9M111, 0x5d, 0)},2509{USB_DEVICE(0x0c45, 0x6288), SN9C20X(OV9655, 0x30, 0)},2510{USB_DEVICE(0x0c45, 0x628c), SN9C20X(MT9M112, 0x5d, 0)},2511{USB_DEVICE(0x0c45, 0x628e), SN9C20X(SOI968, 0x30, 0)},2512{USB_DEVICE(0x0c45, 0x628f), SN9C20X(OV9650, 0x30, 0)},2513{USB_DEVICE(0x0c45, 0x62a0), SN9C20X(OV7670, 0x21, 0)},2514{USB_DEVICE(0x0c45, 0x62b0), SN9C20X(MT9VPRB, 0x00, 0)},2515{USB_DEVICE(0x0c45, 0x62b3), SN9C20X(OV9655, 0x30, 0)},2516{USB_DEVICE(0x0c45, 0x62bb), SN9C20X(OV7660, 0x21, LED_REVERSE)},2517{USB_DEVICE(0x0c45, 0x62bc), SN9C20X(HV7131R, 0x11, 0)},2518{USB_DEVICE(0x045e, 0x00f4), SN9C20X(OV9650, 0x30, 0)},2519{USB_DEVICE(0x145f, 0x013d), SN9C20X(OV7660, 0x21, 0)},2520{USB_DEVICE(0x0458, 0x7029), SN9C20X(HV7131R, 0x11, 0)},2521{USB_DEVICE(0x0458, 0x704a), SN9C20X(MT9M112, 0x5d, 0)},2522{USB_DEVICE(0x0458, 0x704c), SN9C20X(MT9M112, 0x5d, 0)},2523{USB_DEVICE(0xa168, 0x0610), SN9C20X(HV7131R, 0x11, 0)},2524{USB_DEVICE(0xa168, 0x0611), SN9C20X(HV7131R, 0x11, 0)},2525{USB_DEVICE(0xa168, 0x0613), SN9C20X(HV7131R, 0x11, 0)},2526{USB_DEVICE(0xa168, 0x0618), SN9C20X(HV7131R, 0x11, 0)},2527{USB_DEVICE(0xa168, 0x0614), SN9C20X(MT9M111, 0x5d, 0)},2528{USB_DEVICE(0xa168, 0x0615), SN9C20X(MT9M111, 0x5d, 0)},2529{USB_DEVICE(0xa168, 0x0617), SN9C20X(MT9M111, 0x5d, 0)},2530{}2531};2532MODULE_DEVICE_TABLE(usb, device_table);25332534/* -- device connect -- */2535static int sd_probe(struct usb_interface *intf,2536const struct usb_device_id *id)2537{2538return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),2539THIS_MODULE);2540}25412542static struct usb_driver sd_driver = {2543.name = MODULE_NAME,2544.id_table = device_table,2545.probe = sd_probe,2546.disconnect = gspca_disconnect,2547#ifdef CONFIG_PM2548.suspend = gspca_suspend,2549.resume = gspca_resume,2550.reset_resume = gspca_resume,2551#endif2552};25532554/* -- module insert / remove -- */2555static int __init sd_mod_init(void)2556{2557return usb_register(&sd_driver);2558}2559static void __exit sd_mod_exit(void)2560{2561usb_deregister(&sd_driver);2562}25632564module_init(sd_mod_init);2565module_exit(sd_mod_exit);256625672568