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