xref: /openbmc/linux/drivers/media/usb/gspca/spca561.c (revision c93396e13576928a073154b5715761ff8a998368)
10c0d06caSMauro Carvalho Chehab /*
20c0d06caSMauro Carvalho Chehab  * Sunplus spca561 subdriver
30c0d06caSMauro Carvalho Chehab  *
40c0d06caSMauro Carvalho Chehab  * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
50c0d06caSMauro Carvalho Chehab  *
60c0d06caSMauro Carvalho Chehab  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
70c0d06caSMauro Carvalho Chehab  *
80c0d06caSMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
90c0d06caSMauro Carvalho Chehab  * it under the terms of the GNU General Public License as published by
100c0d06caSMauro Carvalho Chehab  * the Free Software Foundation; either version 2 of the License, or
110c0d06caSMauro Carvalho Chehab  * any later version.
120c0d06caSMauro Carvalho Chehab  *
130c0d06caSMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
140c0d06caSMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
150c0d06caSMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160c0d06caSMauro Carvalho Chehab  * GNU General Public License for more details.
170c0d06caSMauro Carvalho Chehab  *
180c0d06caSMauro Carvalho Chehab  * You should have received a copy of the GNU General Public License
190c0d06caSMauro Carvalho Chehab  * along with this program; if not, write to the Free Software
200c0d06caSMauro Carvalho Chehab  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
210c0d06caSMauro Carvalho Chehab  */
220c0d06caSMauro Carvalho Chehab 
230c0d06caSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
240c0d06caSMauro Carvalho Chehab 
250c0d06caSMauro Carvalho Chehab #define MODULE_NAME "spca561"
260c0d06caSMauro Carvalho Chehab 
270c0d06caSMauro Carvalho Chehab #include <linux/input.h>
280c0d06caSMauro Carvalho Chehab #include "gspca.h"
290c0d06caSMauro Carvalho Chehab 
300c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
310c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
320c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
330c0d06caSMauro Carvalho Chehab 
340c0d06caSMauro Carvalho Chehab #define EXPOSURE_MAX (2047 + 325)
350c0d06caSMauro Carvalho Chehab 
360c0d06caSMauro Carvalho Chehab /* specific webcam descriptor */
370c0d06caSMauro Carvalho Chehab struct sd {
380c0d06caSMauro Carvalho Chehab 	struct gspca_dev gspca_dev;	/* !! must be the first item */
390c0d06caSMauro Carvalho Chehab 
400c0d06caSMauro Carvalho Chehab 	struct { /* hue/contrast control cluster */
410c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *contrast;
420c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *hue;
430c0d06caSMauro Carvalho Chehab 	};
440c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl *autogain;
450c0d06caSMauro Carvalho Chehab 
460c0d06caSMauro Carvalho Chehab #define EXPO12A_DEF 3
470c0d06caSMauro Carvalho Chehab 	__u8 expo12a;		/* expo/gain? for rev 12a */
480c0d06caSMauro Carvalho Chehab 
490c0d06caSMauro Carvalho Chehab 	__u8 chip_revision;
500c0d06caSMauro Carvalho Chehab #define Rev012A 0
510c0d06caSMauro Carvalho Chehab #define Rev072A 1
520c0d06caSMauro Carvalho Chehab 
530c0d06caSMauro Carvalho Chehab 	signed char ag_cnt;
540c0d06caSMauro Carvalho Chehab #define AG_CNT_START 13
550c0d06caSMauro Carvalho Chehab };
560c0d06caSMauro Carvalho Chehab 
570c0d06caSMauro Carvalho Chehab static const struct v4l2_pix_format sif_012a_mode[] = {
580c0d06caSMauro Carvalho Chehab 	{160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
590c0d06caSMauro Carvalho Chehab 		.bytesperline = 160,
600c0d06caSMauro Carvalho Chehab 		.sizeimage = 160 * 120,
610c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
620c0d06caSMauro Carvalho Chehab 		.priv = 3},
630c0d06caSMauro Carvalho Chehab 	{176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
640c0d06caSMauro Carvalho Chehab 		.bytesperline = 176,
650c0d06caSMauro Carvalho Chehab 		.sizeimage = 176 * 144,
660c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
670c0d06caSMauro Carvalho Chehab 		.priv = 2},
680c0d06caSMauro Carvalho Chehab 	{320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
690c0d06caSMauro Carvalho Chehab 		.bytesperline = 320,
700c0d06caSMauro Carvalho Chehab 		.sizeimage = 320 * 240 * 4 / 8,
710c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
720c0d06caSMauro Carvalho Chehab 		.priv = 1},
730c0d06caSMauro Carvalho Chehab 	{352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
740c0d06caSMauro Carvalho Chehab 		.bytesperline = 352,
750c0d06caSMauro Carvalho Chehab 		.sizeimage = 352 * 288 * 4 / 8,
760c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
770c0d06caSMauro Carvalho Chehab 		.priv = 0},
780c0d06caSMauro Carvalho Chehab };
790c0d06caSMauro Carvalho Chehab 
800c0d06caSMauro Carvalho Chehab static const struct v4l2_pix_format sif_072a_mode[] = {
810c0d06caSMauro Carvalho Chehab 	{160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
820c0d06caSMauro Carvalho Chehab 		.bytesperline = 160,
830c0d06caSMauro Carvalho Chehab 		.sizeimage = 160 * 120,
840c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
850c0d06caSMauro Carvalho Chehab 		.priv = 3},
860c0d06caSMauro Carvalho Chehab 	{176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
870c0d06caSMauro Carvalho Chehab 		.bytesperline = 176,
880c0d06caSMauro Carvalho Chehab 		.sizeimage = 176 * 144,
890c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
900c0d06caSMauro Carvalho Chehab 		.priv = 2},
910c0d06caSMauro Carvalho Chehab 	{320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
920c0d06caSMauro Carvalho Chehab 		.bytesperline = 320,
930c0d06caSMauro Carvalho Chehab 		.sizeimage = 320 * 240,
940c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
950c0d06caSMauro Carvalho Chehab 		.priv = 1},
960c0d06caSMauro Carvalho Chehab 	{352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
970c0d06caSMauro Carvalho Chehab 		.bytesperline = 352,
980c0d06caSMauro Carvalho Chehab 		.sizeimage = 352 * 288,
990c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
1000c0d06caSMauro Carvalho Chehab 		.priv = 0},
1010c0d06caSMauro Carvalho Chehab };
1020c0d06caSMauro Carvalho Chehab 
1030c0d06caSMauro Carvalho Chehab /*
1040c0d06caSMauro Carvalho Chehab  * Initialization data
1050c0d06caSMauro Carvalho Chehab  * I'm not very sure how to split initialization from open data
1060c0d06caSMauro Carvalho Chehab  * chunks. For now, we'll consider everything as initialization
1070c0d06caSMauro Carvalho Chehab  */
1080c0d06caSMauro Carvalho Chehab /* Frame packet header offsets for the spca561 */
1090c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_SNAP 1
1100c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_TYPE 2
1110c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_COMPRESS 3
1120c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_FRAMSEQ   4
1130c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_GPIO 5
1140c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_USBBUFF 6
1150c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN2GRAVE 7
1160c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN2RAVE 8
1170c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN2BAVE 9
1180c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN2GBAVE 10
1190c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN1GRAVE 11
1200c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN1RAVE 12
1210c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN1BAVE 13
1220c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_WIN1GBAVE 14
1230c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_FREQ 15
1240c0d06caSMauro Carvalho Chehab #define SPCA561_OFFSET_VSYNC 16
1250c0d06caSMauro Carvalho Chehab #define SPCA561_INDEX_I2C_BASE 0x8800
1260c0d06caSMauro Carvalho Chehab #define SPCA561_SNAPBIT 0x20
1270c0d06caSMauro Carvalho Chehab #define SPCA561_SNAPCTRL 0x40
1280c0d06caSMauro Carvalho Chehab 
1290c0d06caSMauro Carvalho Chehab static const u16 rev72a_reset[][2] = {
1300c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8114},	/* Software GPIO output data */
1310c0d06caSMauro Carvalho Chehab 	{0x0001, 0x8114},	/* Software GPIO output data */
1320c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8112},	/* Some kind of reset */
1330c0d06caSMauro Carvalho Chehab 	{}
1340c0d06caSMauro Carvalho Chehab };
1350c0d06caSMauro Carvalho Chehab static const __u16 rev72a_init_data1[][2] = {
1360c0d06caSMauro Carvalho Chehab 	{0x0003, 0x8701},	/* PCLK clock delay adjustment */
1370c0d06caSMauro Carvalho Chehab 	{0x0001, 0x8703},	/* HSYNC from cmos inverted */
1380c0d06caSMauro Carvalho Chehab 	{0x0011, 0x8118},	/* Enable and conf sensor */
1390c0d06caSMauro Carvalho Chehab 	{0x0001, 0x8118},	/* Conf sensor */
1400c0d06caSMauro Carvalho Chehab 	{0x0092, 0x8804},	/* I know nothing about these */
1410c0d06caSMauro Carvalho Chehab 	{0x0010, 0x8802},	/* 0x88xx registers, so I won't */
1420c0d06caSMauro Carvalho Chehab 	{}
1430c0d06caSMauro Carvalho Chehab };
1440c0d06caSMauro Carvalho Chehab static const u16 rev72a_init_sensor1[][2] = {
1450c0d06caSMauro Carvalho Chehab 	{0x0001, 0x000d},
1460c0d06caSMauro Carvalho Chehab 	{0x0002, 0x0018},
1470c0d06caSMauro Carvalho Chehab 	{0x0004, 0x0165},
1480c0d06caSMauro Carvalho Chehab 	{0x0005, 0x0021},
1490c0d06caSMauro Carvalho Chehab 	{0x0007, 0x00aa},
1500c0d06caSMauro Carvalho Chehab 	{0x0020, 0x1504},
1510c0d06caSMauro Carvalho Chehab 	{0x0039, 0x0002},
1520c0d06caSMauro Carvalho Chehab 	{0x0035, 0x0010},
1530c0d06caSMauro Carvalho Chehab 	{0x0009, 0x1049},
1540c0d06caSMauro Carvalho Chehab 	{0x0028, 0x000b},
1550c0d06caSMauro Carvalho Chehab 	{0x003b, 0x000f},
1560c0d06caSMauro Carvalho Chehab 	{0x003c, 0x0000},
1570c0d06caSMauro Carvalho Chehab 	{}
1580c0d06caSMauro Carvalho Chehab };
1590c0d06caSMauro Carvalho Chehab static const __u16 rev72a_init_data2[][2] = {
1600c0d06caSMauro Carvalho Chehab 	{0x0018, 0x8601},	/* Pixel/line selection for color separation */
1610c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8602},	/* Optical black level for user setting */
1620c0d06caSMauro Carvalho Chehab 	{0x0060, 0x8604},	/* Optical black horizontal offset */
1630c0d06caSMauro Carvalho Chehab 	{0x0002, 0x8605},	/* Optical black vertical offset */
1640c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8603},	/* Non-automatic optical black level */
1650c0d06caSMauro Carvalho Chehab 	{0x0002, 0x865b},	/* Horizontal offset for valid pixels */
1660c0d06caSMauro Carvalho Chehab 	{0x0000, 0x865f},	/* Vertical valid pixels window (x2) */
1670c0d06caSMauro Carvalho Chehab 	{0x00b0, 0x865d},	/* Horizontal valid pixels window (x2) */
1680c0d06caSMauro Carvalho Chehab 	{0x0090, 0x865e},	/* Vertical valid lines window (x2) */
1690c0d06caSMauro Carvalho Chehab 	{0x00e0, 0x8406},	/* Memory buffer threshold */
1700c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8660},	/* Compensation memory stuff */
1710c0d06caSMauro Carvalho Chehab 	{0x0002, 0x8201},	/* Output address for r/w serial EEPROM */
1720c0d06caSMauro Carvalho Chehab 	{0x0008, 0x8200},	/* Clear valid bit for serial EEPROM */
1730c0d06caSMauro Carvalho Chehab 	{0x0001, 0x8200},	/* OprMode to be executed by hardware */
1740c0d06caSMauro Carvalho Chehab /* from ms-win */
1750c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8611},	/* R offset for white balance */
1760c0d06caSMauro Carvalho Chehab 	{0x00fd, 0x8612},	/* Gr offset for white balance */
1770c0d06caSMauro Carvalho Chehab 	{0x0003, 0x8613},	/* B offset for white balance */
1780c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8614},	/* Gb offset for white balance */
1790c0d06caSMauro Carvalho Chehab /* from ms-win */
1800c0d06caSMauro Carvalho Chehab 	{0x0035, 0x8651},	/* R gain for white balance */
1810c0d06caSMauro Carvalho Chehab 	{0x0040, 0x8652},	/* Gr gain for white balance */
1820c0d06caSMauro Carvalho Chehab 	{0x005f, 0x8653},	/* B gain for white balance */
1830c0d06caSMauro Carvalho Chehab 	{0x0040, 0x8654},	/* Gb gain for white balance */
1840c0d06caSMauro Carvalho Chehab 	{0x0002, 0x8502},	/* Maximum average bit rate stuff */
1850c0d06caSMauro Carvalho Chehab 	{0x0011, 0x8802},
1860c0d06caSMauro Carvalho Chehab 
1870c0d06caSMauro Carvalho Chehab 	{0x0087, 0x8700},	/* Set master clock (96Mhz????) */
1880c0d06caSMauro Carvalho Chehab 	{0x0081, 0x8702},	/* Master clock output enable */
1890c0d06caSMauro Carvalho Chehab 
1900c0d06caSMauro Carvalho Chehab 	{0x0000, 0x8500},	/* Set image type (352x288 no compression) */
1910c0d06caSMauro Carvalho Chehab 	/* Originally was 0x0010 (352x288 compression) */
1920c0d06caSMauro Carvalho Chehab 
1930c0d06caSMauro Carvalho Chehab 	{0x0002, 0x865b},	/* Horizontal offset for valid pixels */
1940c0d06caSMauro Carvalho Chehab 	{0x0003, 0x865c},	/* Vertical offset for valid lines */
1950c0d06caSMauro Carvalho Chehab 	{}
1960c0d06caSMauro Carvalho Chehab };
1970c0d06caSMauro Carvalho Chehab static const u16 rev72a_init_sensor2[][2] = {
1980c0d06caSMauro Carvalho Chehab 	{0x0003, 0x0121},
1990c0d06caSMauro Carvalho Chehab 	{0x0004, 0x0165},
2000c0d06caSMauro Carvalho Chehab 	{0x0005, 0x002f},	/* blanking control column */
2010c0d06caSMauro Carvalho Chehab 	{0x0006, 0x0000},	/* blanking mode row*/
2020c0d06caSMauro Carvalho Chehab 	{0x000a, 0x0002},
2030c0d06caSMauro Carvalho Chehab 	{0x0009, 0x1061},	/* setexposure times && pixel clock
2040c0d06caSMauro Carvalho Chehab 				 * 0001 0 | 000 0110 0001 */
2050c0d06caSMauro Carvalho Chehab 	{0x0035, 0x0014},
2060c0d06caSMauro Carvalho Chehab 	{}
2070c0d06caSMauro Carvalho Chehab };
2080c0d06caSMauro Carvalho Chehab 
2090c0d06caSMauro Carvalho Chehab /******************** QC Express etch2 stuff ********************/
2100c0d06caSMauro Carvalho Chehab static const __u16 Pb100_1map8300[][2] = {
2110c0d06caSMauro Carvalho Chehab 	/* reg, value */
2120c0d06caSMauro Carvalho Chehab 	{0x8320, 0x3304},
2130c0d06caSMauro Carvalho Chehab 
2140c0d06caSMauro Carvalho Chehab 	{0x8303, 0x0125},	/* image area */
2150c0d06caSMauro Carvalho Chehab 	{0x8304, 0x0169},
2160c0d06caSMauro Carvalho Chehab 	{0x8328, 0x000b},
2170c0d06caSMauro Carvalho Chehab 	{0x833c, 0x0001},		/*fixme: win:07*/
2180c0d06caSMauro Carvalho Chehab 
2190c0d06caSMauro Carvalho Chehab 	{0x832f, 0x1904},		/*fixme: was 0419*/
2200c0d06caSMauro Carvalho Chehab 	{0x8307, 0x00aa},
2210c0d06caSMauro Carvalho Chehab 	{0x8301, 0x0003},
2220c0d06caSMauro Carvalho Chehab 	{0x8302, 0x000e},
2230c0d06caSMauro Carvalho Chehab 	{}
2240c0d06caSMauro Carvalho Chehab };
2250c0d06caSMauro Carvalho Chehab static const __u16 Pb100_2map8300[][2] = {
2260c0d06caSMauro Carvalho Chehab 	/* reg, value */
2270c0d06caSMauro Carvalho Chehab 	{0x8339, 0x0000},
2280c0d06caSMauro Carvalho Chehab 	{0x8307, 0x00aa},
2290c0d06caSMauro Carvalho Chehab 	{}
2300c0d06caSMauro Carvalho Chehab };
2310c0d06caSMauro Carvalho Chehab 
2320c0d06caSMauro Carvalho Chehab static const __u16 spca561_161rev12A_data1[][2] = {
2330c0d06caSMauro Carvalho Chehab 	{0x29, 0x8118},		/* Control register (various enable bits) */
2340c0d06caSMauro Carvalho Chehab 	{0x08, 0x8114},		/* GPIO: Led off */
2350c0d06caSMauro Carvalho Chehab 	{0x0e, 0x8112},		/* 0x0e stream off 0x3e stream on */
2360c0d06caSMauro Carvalho Chehab 	{0x00, 0x8102},		/* white balance - new */
2370c0d06caSMauro Carvalho Chehab 	{0x92, 0x8804},
2380c0d06caSMauro Carvalho Chehab 	{0x04, 0x8802},		/* windows uses 08 */
2390c0d06caSMauro Carvalho Chehab 	{}
2400c0d06caSMauro Carvalho Chehab };
2410c0d06caSMauro Carvalho Chehab static const __u16 spca561_161rev12A_data2[][2] = {
2420c0d06caSMauro Carvalho Chehab 	{0x21, 0x8118},
2430c0d06caSMauro Carvalho Chehab 	{0x10, 0x8500},
2440c0d06caSMauro Carvalho Chehab 	{0x07, 0x8601},
2450c0d06caSMauro Carvalho Chehab 	{0x07, 0x8602},
2460c0d06caSMauro Carvalho Chehab 	{0x04, 0x8501},
2470c0d06caSMauro Carvalho Chehab 
2480c0d06caSMauro Carvalho Chehab 	{0x07, 0x8201},		/* windows uses 02 */
2490c0d06caSMauro Carvalho Chehab 	{0x08, 0x8200},
2500c0d06caSMauro Carvalho Chehab 	{0x01, 0x8200},
2510c0d06caSMauro Carvalho Chehab 
2520c0d06caSMauro Carvalho Chehab 	{0x90, 0x8604},
2530c0d06caSMauro Carvalho Chehab 	{0x00, 0x8605},
2540c0d06caSMauro Carvalho Chehab 	{0xb0, 0x8603},
2550c0d06caSMauro Carvalho Chehab 
2560c0d06caSMauro Carvalho Chehab 	/* sensor gains */
2570c0d06caSMauro Carvalho Chehab 	{0x07, 0x8601},		/* white balance - new */
2580c0d06caSMauro Carvalho Chehab 	{0x07, 0x8602},		/* white balance - new */
2590c0d06caSMauro Carvalho Chehab 	{0x00, 0x8610},		/* *red */
2600c0d06caSMauro Carvalho Chehab 	{0x00, 0x8611},		/* 3f   *green */
2610c0d06caSMauro Carvalho Chehab 	{0x00, 0x8612},		/* green *blue */
2620c0d06caSMauro Carvalho Chehab 	{0x00, 0x8613},		/* blue *green */
2630c0d06caSMauro Carvalho Chehab 	{0x43, 0x8614},		/* green *red - white balance - was 0x35 */
2640c0d06caSMauro Carvalho Chehab 	{0x40, 0x8615},		/* 40   *green - white balance - was 0x35 */
2650c0d06caSMauro Carvalho Chehab 	{0x71, 0x8616},		/* 7a   *blue - white balance - was 0x35 */
2660c0d06caSMauro Carvalho Chehab 	{0x40, 0x8617},		/* 40   *green - white balance - was 0x35 */
2670c0d06caSMauro Carvalho Chehab 
2680c0d06caSMauro Carvalho Chehab 	{0x0c, 0x8620},		/* 0c */
2690c0d06caSMauro Carvalho Chehab 	{0xc8, 0x8631},		/* c8 */
2700c0d06caSMauro Carvalho Chehab 	{0xc8, 0x8634},		/* c8 */
2710c0d06caSMauro Carvalho Chehab 	{0x23, 0x8635},		/* 23 */
2720c0d06caSMauro Carvalho Chehab 	{0x1f, 0x8636},		/* 1f */
2730c0d06caSMauro Carvalho Chehab 	{0xdd, 0x8637},		/* dd */
2740c0d06caSMauro Carvalho Chehab 	{0xe1, 0x8638},		/* e1 */
2750c0d06caSMauro Carvalho Chehab 	{0x1d, 0x8639},		/* 1d */
2760c0d06caSMauro Carvalho Chehab 	{0x21, 0x863a},		/* 21 */
2770c0d06caSMauro Carvalho Chehab 	{0xe3, 0x863b},		/* e3 */
2780c0d06caSMauro Carvalho Chehab 	{0xdf, 0x863c},		/* df */
2790c0d06caSMauro Carvalho Chehab 	{0xf0, 0x8505},
2800c0d06caSMauro Carvalho Chehab 	{0x32, 0x850a},
2810c0d06caSMauro Carvalho Chehab /*	{0x99, 0x8700},		 * - white balance - new (removed) */
2820c0d06caSMauro Carvalho Chehab 	/* HDG we used to do this in stop0, making the init state and the state
2830c0d06caSMauro Carvalho Chehab 	   after a start / stop different, so do this here instead. */
2840c0d06caSMauro Carvalho Chehab 	{0x29, 0x8118},
2850c0d06caSMauro Carvalho Chehab 	{}
2860c0d06caSMauro Carvalho Chehab };
2870c0d06caSMauro Carvalho Chehab 
288*c93396e1STheodore Kilgore static void reg_w_val(struct gspca_dev *gspca_dev, __u16 index, __u8 value)
2890c0d06caSMauro Carvalho Chehab {
2900c0d06caSMauro Carvalho Chehab 	int ret;
291*c93396e1STheodore Kilgore 	struct usb_device *dev = gspca_dev->dev;
2920c0d06caSMauro Carvalho Chehab 
2930c0d06caSMauro Carvalho Chehab 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
2940c0d06caSMauro Carvalho Chehab 			      0,		/* request */
2950c0d06caSMauro Carvalho Chehab 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2960c0d06caSMauro Carvalho Chehab 			      value, index, NULL, 0, 500);
2970c0d06caSMauro Carvalho Chehab 	PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
2980c0d06caSMauro Carvalho Chehab 	if (ret < 0)
2990c0d06caSMauro Carvalho Chehab 		pr_err("reg write: error %d\n", ret);
3000c0d06caSMauro Carvalho Chehab }
3010c0d06caSMauro Carvalho Chehab 
3020c0d06caSMauro Carvalho Chehab static void write_vector(struct gspca_dev *gspca_dev,
3030c0d06caSMauro Carvalho Chehab 			const __u16 data[][2])
3040c0d06caSMauro Carvalho Chehab {
3050c0d06caSMauro Carvalho Chehab 	int i;
3060c0d06caSMauro Carvalho Chehab 
3070c0d06caSMauro Carvalho Chehab 	i = 0;
3080c0d06caSMauro Carvalho Chehab 	while (data[i][1] != 0) {
309*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, data[i][1], data[i][0]);
3100c0d06caSMauro Carvalho Chehab 		i++;
3110c0d06caSMauro Carvalho Chehab 	}
3120c0d06caSMauro Carvalho Chehab }
3130c0d06caSMauro Carvalho Chehab 
3140c0d06caSMauro Carvalho Chehab /* read 'len' bytes to gspca_dev->usb_buf */
3150c0d06caSMauro Carvalho Chehab static void reg_r(struct gspca_dev *gspca_dev,
3160c0d06caSMauro Carvalho Chehab 		  __u16 index, __u16 length)
3170c0d06caSMauro Carvalho Chehab {
3180c0d06caSMauro Carvalho Chehab 	usb_control_msg(gspca_dev->dev,
3190c0d06caSMauro Carvalho Chehab 			usb_rcvctrlpipe(gspca_dev->dev, 0),
3200c0d06caSMauro Carvalho Chehab 			0,			/* request */
3210c0d06caSMauro Carvalho Chehab 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3220c0d06caSMauro Carvalho Chehab 			0,			/* value */
3230c0d06caSMauro Carvalho Chehab 			index, gspca_dev->usb_buf, length, 500);
3240c0d06caSMauro Carvalho Chehab }
3250c0d06caSMauro Carvalho Chehab 
3260c0d06caSMauro Carvalho Chehab /* write 'len' bytes from gspca_dev->usb_buf */
3270c0d06caSMauro Carvalho Chehab static void reg_w_buf(struct gspca_dev *gspca_dev,
3280c0d06caSMauro Carvalho Chehab 		      __u16 index, __u16 len)
3290c0d06caSMauro Carvalho Chehab {
3300c0d06caSMauro Carvalho Chehab 	usb_control_msg(gspca_dev->dev,
3310c0d06caSMauro Carvalho Chehab 			usb_sndctrlpipe(gspca_dev->dev, 0),
3320c0d06caSMauro Carvalho Chehab 			0,			/* request */
3330c0d06caSMauro Carvalho Chehab 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3340c0d06caSMauro Carvalho Chehab 			0,			/* value */
3350c0d06caSMauro Carvalho Chehab 			index, gspca_dev->usb_buf, len, 500);
3360c0d06caSMauro Carvalho Chehab }
3370c0d06caSMauro Carvalho Chehab 
3380c0d06caSMauro Carvalho Chehab static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
3390c0d06caSMauro Carvalho Chehab {
3400c0d06caSMauro Carvalho Chehab 	int retry = 60;
3410c0d06caSMauro Carvalho Chehab 
342*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8801, reg);
343*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8805, value);
344*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8800, value >> 8);
3450c0d06caSMauro Carvalho Chehab 	do {
3460c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8803, 1);
3470c0d06caSMauro Carvalho Chehab 		if (!gspca_dev->usb_buf[0])
3480c0d06caSMauro Carvalho Chehab 			return;
3490c0d06caSMauro Carvalho Chehab 		msleep(10);
3500c0d06caSMauro Carvalho Chehab 	} while (--retry);
3510c0d06caSMauro Carvalho Chehab }
3520c0d06caSMauro Carvalho Chehab 
3530c0d06caSMauro Carvalho Chehab static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
3540c0d06caSMauro Carvalho Chehab {
3550c0d06caSMauro Carvalho Chehab 	int retry = 60;
3560c0d06caSMauro Carvalho Chehab 	__u8 value;
3570c0d06caSMauro Carvalho Chehab 
358*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8804, 0x92);
359*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8801, reg);
360*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8802, mode | 0x01);
3610c0d06caSMauro Carvalho Chehab 	do {
3620c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8803, 1);
3630c0d06caSMauro Carvalho Chehab 		if (!gspca_dev->usb_buf[0]) {
3640c0d06caSMauro Carvalho Chehab 			reg_r(gspca_dev, 0x8800, 1);
3650c0d06caSMauro Carvalho Chehab 			value = gspca_dev->usb_buf[0];
3660c0d06caSMauro Carvalho Chehab 			reg_r(gspca_dev, 0x8805, 1);
3670c0d06caSMauro Carvalho Chehab 			return ((int) value << 8) | gspca_dev->usb_buf[0];
3680c0d06caSMauro Carvalho Chehab 		}
3690c0d06caSMauro Carvalho Chehab 		msleep(10);
3700c0d06caSMauro Carvalho Chehab 	} while (--retry);
3710c0d06caSMauro Carvalho Chehab 	return -1;
3720c0d06caSMauro Carvalho Chehab }
3730c0d06caSMauro Carvalho Chehab 
3740c0d06caSMauro Carvalho Chehab static void sensor_mapwrite(struct gspca_dev *gspca_dev,
3750c0d06caSMauro Carvalho Chehab 			    const __u16 (*sensormap)[2])
3760c0d06caSMauro Carvalho Chehab {
3770c0d06caSMauro Carvalho Chehab 	while ((*sensormap)[0]) {
3780c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[0] = (*sensormap)[1];
3790c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
3800c0d06caSMauro Carvalho Chehab 		reg_w_buf(gspca_dev, (*sensormap)[0], 2);
3810c0d06caSMauro Carvalho Chehab 		sensormap++;
3820c0d06caSMauro Carvalho Chehab 	}
3830c0d06caSMauro Carvalho Chehab }
3840c0d06caSMauro Carvalho Chehab 
3850c0d06caSMauro Carvalho Chehab static void write_sensor_72a(struct gspca_dev *gspca_dev,
3860c0d06caSMauro Carvalho Chehab 			    const __u16 (*sensor)[2])
3870c0d06caSMauro Carvalho Chehab {
3880c0d06caSMauro Carvalho Chehab 	while ((*sensor)[0]) {
3890c0d06caSMauro Carvalho Chehab 		i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
3900c0d06caSMauro Carvalho Chehab 		sensor++;
3910c0d06caSMauro Carvalho Chehab 	}
3920c0d06caSMauro Carvalho Chehab }
3930c0d06caSMauro Carvalho Chehab 
3940c0d06caSMauro Carvalho Chehab static void init_161rev12A(struct gspca_dev *gspca_dev)
3950c0d06caSMauro Carvalho Chehab {
3960c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, spca561_161rev12A_data1);
3970c0d06caSMauro Carvalho Chehab 	sensor_mapwrite(gspca_dev, Pb100_1map8300);
3980c0d06caSMauro Carvalho Chehab /*fixme: should be in sd_start*/
3990c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, spca561_161rev12A_data2);
4000c0d06caSMauro Carvalho Chehab 	sensor_mapwrite(gspca_dev, Pb100_2map8300);
4010c0d06caSMauro Carvalho Chehab }
4020c0d06caSMauro Carvalho Chehab 
4030c0d06caSMauro Carvalho Chehab /* this function is called at probe time */
4040c0d06caSMauro Carvalho Chehab static int sd_config(struct gspca_dev *gspca_dev,
4050c0d06caSMauro Carvalho Chehab 		     const struct usb_device_id *id)
4060c0d06caSMauro Carvalho Chehab {
4070c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4080c0d06caSMauro Carvalho Chehab 	struct cam *cam;
4090c0d06caSMauro Carvalho Chehab 	__u16 vendor, product;
4100c0d06caSMauro Carvalho Chehab 	__u8 data1, data2;
4110c0d06caSMauro Carvalho Chehab 
4120c0d06caSMauro Carvalho Chehab 	/* Read frm global register the USB product and vendor IDs, just to
4130c0d06caSMauro Carvalho Chehab 	 * prove that we can communicate with the device.  This works, which
4140c0d06caSMauro Carvalho Chehab 	 * confirms at we are communicating properly and that the device
4150c0d06caSMauro Carvalho Chehab 	 * is a 561. */
4160c0d06caSMauro Carvalho Chehab 	reg_r(gspca_dev, 0x8104, 1);
4170c0d06caSMauro Carvalho Chehab 	data1 = gspca_dev->usb_buf[0];
4180c0d06caSMauro Carvalho Chehab 	reg_r(gspca_dev, 0x8105, 1);
4190c0d06caSMauro Carvalho Chehab 	data2 = gspca_dev->usb_buf[0];
4200c0d06caSMauro Carvalho Chehab 	vendor = (data2 << 8) | data1;
4210c0d06caSMauro Carvalho Chehab 	reg_r(gspca_dev, 0x8106, 1);
4220c0d06caSMauro Carvalho Chehab 	data1 = gspca_dev->usb_buf[0];
4230c0d06caSMauro Carvalho Chehab 	reg_r(gspca_dev, 0x8107, 1);
4240c0d06caSMauro Carvalho Chehab 	data2 = gspca_dev->usb_buf[0];
4250c0d06caSMauro Carvalho Chehab 	product = (data2 << 8) | data1;
4260c0d06caSMauro Carvalho Chehab 	if (vendor != id->idVendor || product != id->idProduct) {
4270c0d06caSMauro Carvalho Chehab 		PDEBUG(D_PROBE, "Bad vendor / product from device");
4280c0d06caSMauro Carvalho Chehab 		return -EINVAL;
4290c0d06caSMauro Carvalho Chehab 	}
4300c0d06caSMauro Carvalho Chehab 
4310c0d06caSMauro Carvalho Chehab 	cam = &gspca_dev->cam;
4320c0d06caSMauro Carvalho Chehab 	cam->needs_full_bandwidth = 1;
4330c0d06caSMauro Carvalho Chehab 
4340c0d06caSMauro Carvalho Chehab 	sd->chip_revision = id->driver_info;
4350c0d06caSMauro Carvalho Chehab 	if (sd->chip_revision == Rev012A) {
4360c0d06caSMauro Carvalho Chehab 		cam->cam_mode = sif_012a_mode;
4370c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(sif_012a_mode);
4380c0d06caSMauro Carvalho Chehab 	} else {
4390c0d06caSMauro Carvalho Chehab 		cam->cam_mode = sif_072a_mode;
4400c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(sif_072a_mode);
4410c0d06caSMauro Carvalho Chehab 	}
4420c0d06caSMauro Carvalho Chehab 	sd->expo12a = EXPO12A_DEF;
4430c0d06caSMauro Carvalho Chehab 	return 0;
4440c0d06caSMauro Carvalho Chehab }
4450c0d06caSMauro Carvalho Chehab 
4460c0d06caSMauro Carvalho Chehab /* this function is called at probe and resume time */
4470c0d06caSMauro Carvalho Chehab static int sd_init_12a(struct gspca_dev *gspca_dev)
4480c0d06caSMauro Carvalho Chehab {
4490c0d06caSMauro Carvalho Chehab 	PDEBUG(D_STREAM, "Chip revision: 012a");
4500c0d06caSMauro Carvalho Chehab 	init_161rev12A(gspca_dev);
4510c0d06caSMauro Carvalho Chehab 	return 0;
4520c0d06caSMauro Carvalho Chehab }
4530c0d06caSMauro Carvalho Chehab static int sd_init_72a(struct gspca_dev *gspca_dev)
4540c0d06caSMauro Carvalho Chehab {
4550c0d06caSMauro Carvalho Chehab 	PDEBUG(D_STREAM, "Chip revision: 072a");
4560c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, rev72a_reset);
4570c0d06caSMauro Carvalho Chehab 	msleep(200);
4580c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, rev72a_init_data1);
4590c0d06caSMauro Carvalho Chehab 	write_sensor_72a(gspca_dev, rev72a_init_sensor1);
4600c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, rev72a_init_data2);
4610c0d06caSMauro Carvalho Chehab 	write_sensor_72a(gspca_dev, rev72a_init_sensor2);
462*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8112, 0x30);
4630c0d06caSMauro Carvalho Chehab 	return 0;
4640c0d06caSMauro Carvalho Chehab }
4650c0d06caSMauro Carvalho Chehab 
4660c0d06caSMauro Carvalho Chehab static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
4670c0d06caSMauro Carvalho Chehab {
4680c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4690c0d06caSMauro Carvalho Chehab 	__u16 reg;
4700c0d06caSMauro Carvalho Chehab 
4710c0d06caSMauro Carvalho Chehab 	if (sd->chip_revision == Rev012A)
4720c0d06caSMauro Carvalho Chehab 		reg = 0x8610;
4730c0d06caSMauro Carvalho Chehab 	else
4740c0d06caSMauro Carvalho Chehab 		reg = 0x8611;
4750c0d06caSMauro Carvalho Chehab 
476*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg + 0, val);		/* R */
477*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg + 1, val);		/* Gr */
478*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg + 2, val);		/* B */
479*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg + 3, val);		/* Gb */
4800c0d06caSMauro Carvalho Chehab }
4810c0d06caSMauro Carvalho Chehab 
4820c0d06caSMauro Carvalho Chehab static void setwhite(struct gspca_dev *gspca_dev, s32 white, s32 contrast)
4830c0d06caSMauro Carvalho Chehab {
4840c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4850c0d06caSMauro Carvalho Chehab 	__u8 blue, red;
4860c0d06caSMauro Carvalho Chehab 	__u16 reg;
4870c0d06caSMauro Carvalho Chehab 
4880c0d06caSMauro Carvalho Chehab 	/* try to emulate MS-win as possible */
4890c0d06caSMauro Carvalho Chehab 	red = 0x20 + white * 3 / 8;
4900c0d06caSMauro Carvalho Chehab 	blue = 0x90 - white * 5 / 8;
4910c0d06caSMauro Carvalho Chehab 	if (sd->chip_revision == Rev012A) {
4920c0d06caSMauro Carvalho Chehab 		reg = 0x8614;
4930c0d06caSMauro Carvalho Chehab 	} else {
4940c0d06caSMauro Carvalho Chehab 		reg = 0x8651;
4950c0d06caSMauro Carvalho Chehab 		red += contrast - 0x20;
4960c0d06caSMauro Carvalho Chehab 		blue += contrast - 0x20;
497*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8652, contrast + 0x20); /* Gr */
498*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8654, contrast + 0x20); /* Gb */
4990c0d06caSMauro Carvalho Chehab 	}
500*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg, red);
501*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, reg + 2, blue);
5020c0d06caSMauro Carvalho Chehab }
5030c0d06caSMauro Carvalho Chehab 
5040c0d06caSMauro Carvalho Chehab /* rev 12a only */
5050c0d06caSMauro Carvalho Chehab static void setexposure(struct gspca_dev *gspca_dev, s32 val)
5060c0d06caSMauro Carvalho Chehab {
5070c0d06caSMauro Carvalho Chehab 	int i, expo = 0;
5080c0d06caSMauro Carvalho Chehab 
5090c0d06caSMauro Carvalho Chehab 	/* Register 0x8309 controls exposure for the spca561,
5100c0d06caSMauro Carvalho Chehab 	   the basic exposure setting goes from 1-2047, where 1 is completely
5110c0d06caSMauro Carvalho Chehab 	   dark and 2047 is very bright. It not only influences exposure but
5120c0d06caSMauro Carvalho Chehab 	   also the framerate (to allow for longer exposure) from 1 - 300 it
5130c0d06caSMauro Carvalho Chehab 	   only raises the exposure time then from 300 - 600 it halves the
5140c0d06caSMauro Carvalho Chehab 	   framerate to be able to further raise the exposure time and for every
5150c0d06caSMauro Carvalho Chehab 	   300 more it halves the framerate again. This allows for a maximum
5160c0d06caSMauro Carvalho Chehab 	   exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
5170c0d06caSMauro Carvalho Chehab 	   Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
5180c0d06caSMauro Carvalho Chehab 	   configure a divider for the base framerate which us used at the
5190c0d06caSMauro Carvalho Chehab 	   exposure setting of 1-300. These bits configure the base framerate
5200c0d06caSMauro Carvalho Chehab 	   according to the following formula: fps = 60 / (value + 2) */
5210c0d06caSMauro Carvalho Chehab 
5220c0d06caSMauro Carvalho Chehab 	/* We choose to use the high bits setting the fixed framerate divisor
5230c0d06caSMauro Carvalho Chehab 	   asap, as setting high basic exposure setting without the fixed
5240c0d06caSMauro Carvalho Chehab 	   divider in combination with high gains makes the cam stop */
5250c0d06caSMauro Carvalho Chehab 	int table[] =  { 0, 450, 550, 625, EXPOSURE_MAX };
5260c0d06caSMauro Carvalho Chehab 
5270c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(table) - 1; i++) {
5280c0d06caSMauro Carvalho Chehab 		if (val <= table[i + 1]) {
5290c0d06caSMauro Carvalho Chehab 			expo  = val - table[i];
5300c0d06caSMauro Carvalho Chehab 			if (i)
5310c0d06caSMauro Carvalho Chehab 				expo += 300;
5320c0d06caSMauro Carvalho Chehab 			expo |= i << 11;
5330c0d06caSMauro Carvalho Chehab 			break;
5340c0d06caSMauro Carvalho Chehab 		}
5350c0d06caSMauro Carvalho Chehab 	}
5360c0d06caSMauro Carvalho Chehab 
5370c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[0] = expo;
5380c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[1] = expo >> 8;
5390c0d06caSMauro Carvalho Chehab 	reg_w_buf(gspca_dev, 0x8309, 2);
5400c0d06caSMauro Carvalho Chehab }
5410c0d06caSMauro Carvalho Chehab 
5420c0d06caSMauro Carvalho Chehab /* rev 12a only */
5430c0d06caSMauro Carvalho Chehab static void setgain(struct gspca_dev *gspca_dev, s32 val)
5440c0d06caSMauro Carvalho Chehab {
5450c0d06caSMauro Carvalho Chehab 	/* gain reg low 6 bits  0-63 gain, bit 6 and 7, both double the
5460c0d06caSMauro Carvalho Chehab 	   sensitivity when set, so 31 + one of them set == 63, and 15
5470c0d06caSMauro Carvalho Chehab 	   with both of them set == 63 */
5480c0d06caSMauro Carvalho Chehab 	if (val < 64)
5490c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[0] = val;
5500c0d06caSMauro Carvalho Chehab 	else if (val < 128)
5510c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[0] = (val / 2) | 0x40;
5520c0d06caSMauro Carvalho Chehab 	else
5530c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[0] = (val / 4) | 0xc0;
5540c0d06caSMauro Carvalho Chehab 
5550c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[1] = 0;
5560c0d06caSMauro Carvalho Chehab 	reg_w_buf(gspca_dev, 0x8335, 2);
5570c0d06caSMauro Carvalho Chehab }
5580c0d06caSMauro Carvalho Chehab 
5590c0d06caSMauro Carvalho Chehab static void setautogain(struct gspca_dev *gspca_dev, s32 val)
5600c0d06caSMauro Carvalho Chehab {
5610c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
5620c0d06caSMauro Carvalho Chehab 
5630c0d06caSMauro Carvalho Chehab 	if (val)
5640c0d06caSMauro Carvalho Chehab 		sd->ag_cnt = AG_CNT_START;
5650c0d06caSMauro Carvalho Chehab 	else
5660c0d06caSMauro Carvalho Chehab 		sd->ag_cnt = -1;
5670c0d06caSMauro Carvalho Chehab }
5680c0d06caSMauro Carvalho Chehab 
5690c0d06caSMauro Carvalho Chehab static int sd_start_12a(struct gspca_dev *gspca_dev)
5700c0d06caSMauro Carvalho Chehab {
5710c0d06caSMauro Carvalho Chehab 	int mode;
5720c0d06caSMauro Carvalho Chehab 	static const __u8 Reg8391[8] =
5730c0d06caSMauro Carvalho Chehab 		{0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
5740c0d06caSMauro Carvalho Chehab 
5750c0d06caSMauro Carvalho Chehab 	mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
5760c0d06caSMauro Carvalho Chehab 	if (mode <= 1) {
5770c0d06caSMauro Carvalho Chehab 		/* Use compression on 320x240 and above */
578*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8500, 0x10 | mode);
5790c0d06caSMauro Carvalho Chehab 	} else {
5800c0d06caSMauro Carvalho Chehab 		/* I couldn't get the compression to work below 320x240
5810c0d06caSMauro Carvalho Chehab 		 * Fortunately at these resolutions the bandwidth
5820c0d06caSMauro Carvalho Chehab 		 * is sufficient to push raw frames at ~20fps */
583*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8500, mode);
5840c0d06caSMauro Carvalho Chehab 	}		/* -- qq@kuku.eu.org */
5850c0d06caSMauro Carvalho Chehab 
5860c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[0] = 0xaa;
5870c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[1] = 0x00;
5880c0d06caSMauro Carvalho Chehab 	reg_w_buf(gspca_dev, 0x8307, 2);
5890c0d06caSMauro Carvalho Chehab 	/* clock - lower 0x8X values lead to fps > 30 */
590*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8700, 0x8a);
5910c0d06caSMauro Carvalho Chehab 					/* 0x8f 0x85 0x27 clock */
592*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8112, 0x1e | 0x20);
593*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x850b, 0x03);
5940c0d06caSMauro Carvalho Chehab 	memcpy(gspca_dev->usb_buf, Reg8391, 8);
5950c0d06caSMauro Carvalho Chehab 	reg_w_buf(gspca_dev, 0x8391, 8);
5960c0d06caSMauro Carvalho Chehab 	reg_w_buf(gspca_dev, 0x8390, 8);
5970c0d06caSMauro Carvalho Chehab 
5980c0d06caSMauro Carvalho Chehab 	/* Led ON (bit 3 -> 0 */
599*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8114, 0x00);
6000c0d06caSMauro Carvalho Chehab 	return 0;
6010c0d06caSMauro Carvalho Chehab }
6020c0d06caSMauro Carvalho Chehab static int sd_start_72a(struct gspca_dev *gspca_dev)
6030c0d06caSMauro Carvalho Chehab {
6040c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
6050c0d06caSMauro Carvalho Chehab 	int Clck;
6060c0d06caSMauro Carvalho Chehab 	int mode;
6070c0d06caSMauro Carvalho Chehab 
6080c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, rev72a_reset);
6090c0d06caSMauro Carvalho Chehab 	msleep(200);
6100c0d06caSMauro Carvalho Chehab 	write_vector(gspca_dev, rev72a_init_data1);
6110c0d06caSMauro Carvalho Chehab 	write_sensor_72a(gspca_dev, rev72a_init_sensor1);
6120c0d06caSMauro Carvalho Chehab 
6130c0d06caSMauro Carvalho Chehab 	mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
6140c0d06caSMauro Carvalho Chehab 	switch (mode) {
6150c0d06caSMauro Carvalho Chehab 	default:
6160c0d06caSMauro Carvalho Chehab 	case 0:
6170c0d06caSMauro Carvalho Chehab 		Clck = 0x27;		/* ms-win 0x87 */
6180c0d06caSMauro Carvalho Chehab 		break;
6190c0d06caSMauro Carvalho Chehab 	case 1:
6200c0d06caSMauro Carvalho Chehab 		Clck = 0x25;
6210c0d06caSMauro Carvalho Chehab 		break;
6220c0d06caSMauro Carvalho Chehab 	case 2:
6230c0d06caSMauro Carvalho Chehab 		Clck = 0x22;
6240c0d06caSMauro Carvalho Chehab 		break;
6250c0d06caSMauro Carvalho Chehab 	case 3:
6260c0d06caSMauro Carvalho Chehab 		Clck = 0x21;
6270c0d06caSMauro Carvalho Chehab 		break;
6280c0d06caSMauro Carvalho Chehab 	}
629*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8700, Clck);	/* 0x27 clock */
630*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8702, 0x81);
631*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8500, mode);	/* mode */
6320c0d06caSMauro Carvalho Chehab 	write_sensor_72a(gspca_dev, rev72a_init_sensor2);
6330c0d06caSMauro Carvalho Chehab 	setwhite(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue),
6340c0d06caSMauro Carvalho Chehab 			v4l2_ctrl_g_ctrl(sd->contrast));
6350c0d06caSMauro Carvalho Chehab /*	setbrightness(gspca_dev);	 * fixme: bad values */
6360c0d06caSMauro Carvalho Chehab 	setautogain(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
637*c93396e1STheodore Kilgore 	reg_w_val(gspca_dev, 0x8112, 0x10 | 0x20);
6380c0d06caSMauro Carvalho Chehab 	return 0;
6390c0d06caSMauro Carvalho Chehab }
6400c0d06caSMauro Carvalho Chehab 
6410c0d06caSMauro Carvalho Chehab static void sd_stopN(struct gspca_dev *gspca_dev)
6420c0d06caSMauro Carvalho Chehab {
6430c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
6440c0d06caSMauro Carvalho Chehab 
6450c0d06caSMauro Carvalho Chehab 	if (sd->chip_revision == Rev012A) {
646*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8112, 0x0e);
6470c0d06caSMauro Carvalho Chehab 		/* Led Off (bit 3 -> 1 */
648*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8114, 0x08);
6490c0d06caSMauro Carvalho Chehab 	} else {
650*c93396e1STheodore Kilgore 		reg_w_val(gspca_dev, 0x8112, 0x20);
651*c93396e1STheodore Kilgore /*		reg_w_val(gspca_dev, 0x8102, 0x00); ?? */
6520c0d06caSMauro Carvalho Chehab 	}
6530c0d06caSMauro Carvalho Chehab }
6540c0d06caSMauro Carvalho Chehab 
6550c0d06caSMauro Carvalho Chehab static void do_autogain(struct gspca_dev *gspca_dev)
6560c0d06caSMauro Carvalho Chehab {
6570c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
6580c0d06caSMauro Carvalho Chehab 	int expotimes;
6590c0d06caSMauro Carvalho Chehab 	int pixelclk;
6600c0d06caSMauro Carvalho Chehab 	int gainG;
6610c0d06caSMauro Carvalho Chehab 	__u8 R, Gr, Gb, B;
6620c0d06caSMauro Carvalho Chehab 	int y;
6630c0d06caSMauro Carvalho Chehab 	__u8 luma_mean = 110;
6640c0d06caSMauro Carvalho Chehab 	__u8 luma_delta = 20;
6650c0d06caSMauro Carvalho Chehab 	__u8 spring = 4;
6660c0d06caSMauro Carvalho Chehab 
6670c0d06caSMauro Carvalho Chehab 	if (sd->ag_cnt < 0)
6680c0d06caSMauro Carvalho Chehab 		return;
6690c0d06caSMauro Carvalho Chehab 	if (--sd->ag_cnt >= 0)
6700c0d06caSMauro Carvalho Chehab 		return;
6710c0d06caSMauro Carvalho Chehab 	sd->ag_cnt = AG_CNT_START;
6720c0d06caSMauro Carvalho Chehab 
6730c0d06caSMauro Carvalho Chehab 	switch (sd->chip_revision) {
6740c0d06caSMauro Carvalho Chehab 	case Rev072A:
6750c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8621, 1);
6760c0d06caSMauro Carvalho Chehab 		Gr = gspca_dev->usb_buf[0];
6770c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8622, 1);
6780c0d06caSMauro Carvalho Chehab 		R = gspca_dev->usb_buf[0];
6790c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8623, 1);
6800c0d06caSMauro Carvalho Chehab 		B = gspca_dev->usb_buf[0];
6810c0d06caSMauro Carvalho Chehab 		reg_r(gspca_dev, 0x8624, 1);
6820c0d06caSMauro Carvalho Chehab 		Gb = gspca_dev->usb_buf[0];
6830c0d06caSMauro Carvalho Chehab 		y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
6840c0d06caSMauro Carvalho Chehab 		/* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
6850c0d06caSMauro Carvalho Chehab 		/* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
6860c0d06caSMauro Carvalho Chehab 		/* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
6870c0d06caSMauro Carvalho Chehab 
6880c0d06caSMauro Carvalho Chehab 		if (y < luma_mean - luma_delta ||
6890c0d06caSMauro Carvalho Chehab 		    y > luma_mean + luma_delta) {
6900c0d06caSMauro Carvalho Chehab 			expotimes = i2c_read(gspca_dev, 0x09, 0x10);
6910c0d06caSMauro Carvalho Chehab 			pixelclk = 0x0800;
6920c0d06caSMauro Carvalho Chehab 			expotimes = expotimes & 0x07ff;
6930c0d06caSMauro Carvalho Chehab 			/* PDEBUG(D_PACK,
6940c0d06caSMauro Carvalho Chehab 				"Exposition Times 0x%03X Clock 0x%04X ",
6950c0d06caSMauro Carvalho Chehab 				expotimes,pixelclk); */
6960c0d06caSMauro Carvalho Chehab 			gainG = i2c_read(gspca_dev, 0x35, 0x10);
6970c0d06caSMauro Carvalho Chehab 			/* PDEBUG(D_PACK,
6980c0d06caSMauro Carvalho Chehab 				"reading Gain register %d", gainG); */
6990c0d06caSMauro Carvalho Chehab 
7000c0d06caSMauro Carvalho Chehab 			expotimes += (luma_mean - y) >> spring;
7010c0d06caSMauro Carvalho Chehab 			gainG += (luma_mean - y) / 50;
7020c0d06caSMauro Carvalho Chehab 			/* PDEBUG(D_PACK,
7030c0d06caSMauro Carvalho Chehab 				"compute expotimes %d gain %d",
7040c0d06caSMauro Carvalho Chehab 				expotimes,gainG); */
7050c0d06caSMauro Carvalho Chehab 
7060c0d06caSMauro Carvalho Chehab 			if (gainG > 0x3f)
7070c0d06caSMauro Carvalho Chehab 				gainG = 0x3f;
7080c0d06caSMauro Carvalho Chehab 			else if (gainG < 3)
7090c0d06caSMauro Carvalho Chehab 				gainG = 3;
7100c0d06caSMauro Carvalho Chehab 			i2c_write(gspca_dev, gainG, 0x35);
7110c0d06caSMauro Carvalho Chehab 
7120c0d06caSMauro Carvalho Chehab 			if (expotimes > 0x0256)
7130c0d06caSMauro Carvalho Chehab 				expotimes = 0x0256;
7140c0d06caSMauro Carvalho Chehab 			else if (expotimes < 3)
7150c0d06caSMauro Carvalho Chehab 				expotimes = 3;
7160c0d06caSMauro Carvalho Chehab 			i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
7170c0d06caSMauro Carvalho Chehab 		}
7180c0d06caSMauro Carvalho Chehab 		break;
7190c0d06caSMauro Carvalho Chehab 	}
7200c0d06caSMauro Carvalho Chehab }
7210c0d06caSMauro Carvalho Chehab 
7220c0d06caSMauro Carvalho Chehab static void sd_pkt_scan(struct gspca_dev *gspca_dev,
7230c0d06caSMauro Carvalho Chehab 			u8 *data,		/* isoc packet */
7240c0d06caSMauro Carvalho Chehab 			int len)		/* iso packet length */
7250c0d06caSMauro Carvalho Chehab {
7260c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
7270c0d06caSMauro Carvalho Chehab 
7280c0d06caSMauro Carvalho Chehab 	len--;
7290c0d06caSMauro Carvalho Chehab 	switch (*data++) {			/* sequence number */
7300c0d06caSMauro Carvalho Chehab 	case 0:					/* start of frame */
7310c0d06caSMauro Carvalho Chehab 		gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
7320c0d06caSMauro Carvalho Chehab 
7330c0d06caSMauro Carvalho Chehab 		/* This should never happen */
7340c0d06caSMauro Carvalho Chehab 		if (len < 2) {
735*c93396e1STheodore Kilgore 			PERR("Short SOF packet, ignoring");
7360c0d06caSMauro Carvalho Chehab 			gspca_dev->last_packet_type = DISCARD_PACKET;
7370c0d06caSMauro Carvalho Chehab 			return;
7380c0d06caSMauro Carvalho Chehab 		}
7390c0d06caSMauro Carvalho Chehab 
74060d21563SPeter Senna Tschudin #if IS_ENABLED(CONFIG_INPUT)
7410c0d06caSMauro Carvalho Chehab 		if (data[0] & 0x20) {
7420c0d06caSMauro Carvalho Chehab 			input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
7430c0d06caSMauro Carvalho Chehab 			input_sync(gspca_dev->input_dev);
7440c0d06caSMauro Carvalho Chehab 			input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
7450c0d06caSMauro Carvalho Chehab 			input_sync(gspca_dev->input_dev);
7460c0d06caSMauro Carvalho Chehab 		}
7470c0d06caSMauro Carvalho Chehab #endif
7480c0d06caSMauro Carvalho Chehab 
7490c0d06caSMauro Carvalho Chehab 		if (data[1] & 0x10) {
7500c0d06caSMauro Carvalho Chehab 			/* compressed bayer */
7510c0d06caSMauro Carvalho Chehab 			gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
7520c0d06caSMauro Carvalho Chehab 		} else {
7530c0d06caSMauro Carvalho Chehab 			/* raw bayer (with a header, which we skip) */
7540c0d06caSMauro Carvalho Chehab 			if (sd->chip_revision == Rev012A) {
7550c0d06caSMauro Carvalho Chehab 				data += 20;
7560c0d06caSMauro Carvalho Chehab 				len -= 20;
7570c0d06caSMauro Carvalho Chehab 			} else {
7580c0d06caSMauro Carvalho Chehab 				data += 16;
7590c0d06caSMauro Carvalho Chehab 				len -= 16;
7600c0d06caSMauro Carvalho Chehab 			}
7610c0d06caSMauro Carvalho Chehab 			gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
7620c0d06caSMauro Carvalho Chehab 		}
7630c0d06caSMauro Carvalho Chehab 		return;
7640c0d06caSMauro Carvalho Chehab 	case 0xff:			/* drop (empty mpackets) */
7650c0d06caSMauro Carvalho Chehab 		return;
7660c0d06caSMauro Carvalho Chehab 	}
7670c0d06caSMauro Carvalho Chehab 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
7680c0d06caSMauro Carvalho Chehab }
7690c0d06caSMauro Carvalho Chehab 
7700c0d06caSMauro Carvalho Chehab static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
7710c0d06caSMauro Carvalho Chehab {
7720c0d06caSMauro Carvalho Chehab 	struct gspca_dev *gspca_dev =
7730c0d06caSMauro Carvalho Chehab 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
7740c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *)gspca_dev;
7750c0d06caSMauro Carvalho Chehab 
7760c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_err = 0;
7770c0d06caSMauro Carvalho Chehab 
7780c0d06caSMauro Carvalho Chehab 	if (!gspca_dev->streaming)
7790c0d06caSMauro Carvalho Chehab 		return 0;
7800c0d06caSMauro Carvalho Chehab 
7810c0d06caSMauro Carvalho Chehab 	switch (ctrl->id) {
7820c0d06caSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
7830c0d06caSMauro Carvalho Chehab 		setbrightness(gspca_dev, ctrl->val);
7840c0d06caSMauro Carvalho Chehab 		break;
7850c0d06caSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
7860c0d06caSMauro Carvalho Chehab 		/* hue/contrast control cluster for 72a */
7870c0d06caSMauro Carvalho Chehab 		setwhite(gspca_dev, sd->hue->val, ctrl->val);
7880c0d06caSMauro Carvalho Chehab 		break;
7890c0d06caSMauro Carvalho Chehab 	case V4L2_CID_HUE:
7900c0d06caSMauro Carvalho Chehab 		/* just plain hue control for 12a */
7910c0d06caSMauro Carvalho Chehab 		setwhite(gspca_dev, ctrl->val, 0);
7920c0d06caSMauro Carvalho Chehab 		break;
7930c0d06caSMauro Carvalho Chehab 	case V4L2_CID_EXPOSURE:
7940c0d06caSMauro Carvalho Chehab 		setexposure(gspca_dev, ctrl->val);
7950c0d06caSMauro Carvalho Chehab 		break;
7960c0d06caSMauro Carvalho Chehab 	case V4L2_CID_GAIN:
7970c0d06caSMauro Carvalho Chehab 		setgain(gspca_dev, ctrl->val);
7980c0d06caSMauro Carvalho Chehab 		break;
7990c0d06caSMauro Carvalho Chehab 	case V4L2_CID_AUTOGAIN:
8000c0d06caSMauro Carvalho Chehab 		setautogain(gspca_dev, ctrl->val);
8010c0d06caSMauro Carvalho Chehab 		break;
8020c0d06caSMauro Carvalho Chehab 	}
8030c0d06caSMauro Carvalho Chehab 	return gspca_dev->usb_err;
8040c0d06caSMauro Carvalho Chehab }
8050c0d06caSMauro Carvalho Chehab 
8060c0d06caSMauro Carvalho Chehab static const struct v4l2_ctrl_ops sd_ctrl_ops = {
8070c0d06caSMauro Carvalho Chehab 	.s_ctrl = sd_s_ctrl,
8080c0d06caSMauro Carvalho Chehab };
8090c0d06caSMauro Carvalho Chehab 
8100c0d06caSMauro Carvalho Chehab static int sd_init_controls_12a(struct gspca_dev *gspca_dev)
8110c0d06caSMauro Carvalho Chehab {
8120c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
8130c0d06caSMauro Carvalho Chehab 
8140c0d06caSMauro Carvalho Chehab 	gspca_dev->vdev.ctrl_handler = hdl;
8150c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_handler_init(hdl, 3);
8160c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8170c0d06caSMauro Carvalho Chehab 			V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
8180c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8190c0d06caSMauro Carvalho Chehab 			V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
8200c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8210c0d06caSMauro Carvalho Chehab 			V4L2_CID_EXPOSURE, 1, EXPOSURE_MAX, 1, 700);
8220c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8230c0d06caSMauro Carvalho Chehab 			V4L2_CID_GAIN, 0, 255, 1, 63);
8240c0d06caSMauro Carvalho Chehab 
8250c0d06caSMauro Carvalho Chehab 	if (hdl->error) {
8260c0d06caSMauro Carvalho Chehab 		pr_err("Could not initialize controls\n");
8270c0d06caSMauro Carvalho Chehab 		return hdl->error;
8280c0d06caSMauro Carvalho Chehab 	}
8290c0d06caSMauro Carvalho Chehab 	return 0;
8300c0d06caSMauro Carvalho Chehab }
8310c0d06caSMauro Carvalho Chehab 
8320c0d06caSMauro Carvalho Chehab static int sd_init_controls_72a(struct gspca_dev *gspca_dev)
8330c0d06caSMauro Carvalho Chehab {
8340c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *)gspca_dev;
8350c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
8360c0d06caSMauro Carvalho Chehab 
8370c0d06caSMauro Carvalho Chehab 	gspca_dev->vdev.ctrl_handler = hdl;
8380c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_handler_init(hdl, 4);
8390c0d06caSMauro Carvalho Chehab 	sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8400c0d06caSMauro Carvalho Chehab 			V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x20);
8410c0d06caSMauro Carvalho Chehab 	sd->hue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8420c0d06caSMauro Carvalho Chehab 			V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
8430c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8440c0d06caSMauro Carvalho Chehab 			V4L2_CID_BRIGHTNESS, 0, 0x3f, 1, 0x20);
8450c0d06caSMauro Carvalho Chehab 	sd->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
8460c0d06caSMauro Carvalho Chehab 			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
8470c0d06caSMauro Carvalho Chehab 
8480c0d06caSMauro Carvalho Chehab 	if (hdl->error) {
8490c0d06caSMauro Carvalho Chehab 		pr_err("Could not initialize controls\n");
8500c0d06caSMauro Carvalho Chehab 		return hdl->error;
8510c0d06caSMauro Carvalho Chehab 	}
8520c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_cluster(2, &sd->contrast);
8530c0d06caSMauro Carvalho Chehab 	return 0;
8540c0d06caSMauro Carvalho Chehab }
8550c0d06caSMauro Carvalho Chehab 
8560c0d06caSMauro Carvalho Chehab /* sub-driver description */
8570c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_12a = {
8580c0d06caSMauro Carvalho Chehab 	.name = MODULE_NAME,
8590c0d06caSMauro Carvalho Chehab 	.init_controls = sd_init_controls_12a,
8600c0d06caSMauro Carvalho Chehab 	.config = sd_config,
8610c0d06caSMauro Carvalho Chehab 	.init = sd_init_12a,
8620c0d06caSMauro Carvalho Chehab 	.start = sd_start_12a,
8630c0d06caSMauro Carvalho Chehab 	.stopN = sd_stopN,
8640c0d06caSMauro Carvalho Chehab 	.pkt_scan = sd_pkt_scan,
86560d21563SPeter Senna Tschudin #if IS_ENABLED(CONFIG_INPUT)
8660c0d06caSMauro Carvalho Chehab 	.other_input = 1,
8670c0d06caSMauro Carvalho Chehab #endif
8680c0d06caSMauro Carvalho Chehab };
8690c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_72a = {
8700c0d06caSMauro Carvalho Chehab 	.name = MODULE_NAME,
8710c0d06caSMauro Carvalho Chehab 	.init_controls = sd_init_controls_72a,
8720c0d06caSMauro Carvalho Chehab 	.config = sd_config,
8730c0d06caSMauro Carvalho Chehab 	.init = sd_init_72a,
8740c0d06caSMauro Carvalho Chehab 	.start = sd_start_72a,
8750c0d06caSMauro Carvalho Chehab 	.stopN = sd_stopN,
8760c0d06caSMauro Carvalho Chehab 	.pkt_scan = sd_pkt_scan,
8770c0d06caSMauro Carvalho Chehab 	.dq_callback = do_autogain,
87860d21563SPeter Senna Tschudin #if IS_ENABLED(CONFIG_INPUT)
8790c0d06caSMauro Carvalho Chehab 	.other_input = 1,
8800c0d06caSMauro Carvalho Chehab #endif
8810c0d06caSMauro Carvalho Chehab };
8820c0d06caSMauro Carvalho Chehab static const struct sd_desc *sd_desc[2] = {
8830c0d06caSMauro Carvalho Chehab 	&sd_desc_12a,
8840c0d06caSMauro Carvalho Chehab 	&sd_desc_72a
8850c0d06caSMauro Carvalho Chehab };
8860c0d06caSMauro Carvalho Chehab 
8870c0d06caSMauro Carvalho Chehab /* -- module initialisation -- */
8880c0d06caSMauro Carvalho Chehab static const struct usb_device_id device_table[] = {
8890c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
8900c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
8910c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
8920c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x0461, 0x0815), .driver_info = Rev072A},
8930c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
8940c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
8950c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
8960c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
8970c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
8980c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
8990c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
9000c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
9010c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
9020c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
9030c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
9040c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
9050c0d06caSMauro Carvalho Chehab 	{}
9060c0d06caSMauro Carvalho Chehab };
9070c0d06caSMauro Carvalho Chehab 
9080c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, device_table);
9090c0d06caSMauro Carvalho Chehab 
9100c0d06caSMauro Carvalho Chehab /* -- device connect -- */
9110c0d06caSMauro Carvalho Chehab static int sd_probe(struct usb_interface *intf,
9120c0d06caSMauro Carvalho Chehab 		    const struct usb_device_id *id)
9130c0d06caSMauro Carvalho Chehab {
9140c0d06caSMauro Carvalho Chehab 	return gspca_dev_probe(intf, id,
9150c0d06caSMauro Carvalho Chehab 				sd_desc[id->driver_info],
9160c0d06caSMauro Carvalho Chehab 				sizeof(struct sd),
9170c0d06caSMauro Carvalho Chehab 			       THIS_MODULE);
9180c0d06caSMauro Carvalho Chehab }
9190c0d06caSMauro Carvalho Chehab 
9200c0d06caSMauro Carvalho Chehab static struct usb_driver sd_driver = {
9210c0d06caSMauro Carvalho Chehab 	.name = MODULE_NAME,
9220c0d06caSMauro Carvalho Chehab 	.id_table = device_table,
9230c0d06caSMauro Carvalho Chehab 	.probe = sd_probe,
9240c0d06caSMauro Carvalho Chehab 	.disconnect = gspca_disconnect,
9250c0d06caSMauro Carvalho Chehab #ifdef CONFIG_PM
9260c0d06caSMauro Carvalho Chehab 	.suspend = gspca_suspend,
9270c0d06caSMauro Carvalho Chehab 	.resume = gspca_resume,
9280c0d06caSMauro Carvalho Chehab 	.reset_resume = gspca_resume,
9290c0d06caSMauro Carvalho Chehab #endif
9300c0d06caSMauro Carvalho Chehab };
9310c0d06caSMauro Carvalho Chehab 
9320c0d06caSMauro Carvalho Chehab module_usb_driver(sd_driver);
933