18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * T613 subdriver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Jean-Francois Moine (http://moinejf.free.fr)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *Notes: * t613  + tas5130A
88c2ecf20Sopenharmony_ci *	* Focus to light do not balance well as in win.
98c2ecf20Sopenharmony_ci *	  Quality in win is not good, but its kinda better.
108c2ecf20Sopenharmony_ci *	 * Fix some "extraneous bytes", most of apps will show the image anyway
118c2ecf20Sopenharmony_ci *	 * Gamma table, is there, but its really doing something?
128c2ecf20Sopenharmony_ci *	 * 7~8 Fps, its ok, max on win its 10.
138c2ecf20Sopenharmony_ci *			Costantino Leandro
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define MODULE_NAME "t613"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/input.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include "gspca.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciMODULE_AUTHOR("Leandro Costantino <le_costantino@pixartargentina.com.ar>");
258c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GSPCA/T613 (JPEG Compliance) USB Camera Driver");
268c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct sd {
298c2ecf20Sopenharmony_ci	struct gspca_dev gspca_dev;	/* !! must be the first item */
308c2ecf20Sopenharmony_ci	struct v4l2_ctrl *freq;
318c2ecf20Sopenharmony_ci	struct { /* awb / color gains control cluster */
328c2ecf20Sopenharmony_ci		struct v4l2_ctrl *awb;
338c2ecf20Sopenharmony_ci		struct v4l2_ctrl *gain;
348c2ecf20Sopenharmony_ci		struct v4l2_ctrl *red_balance;
358c2ecf20Sopenharmony_ci		struct v4l2_ctrl *blue_balance;
368c2ecf20Sopenharmony_ci	};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	u8 sensor;
398c2ecf20Sopenharmony_ci	u8 button_pressed;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_cienum sensors {
428c2ecf20Sopenharmony_ci	SENSOR_OM6802,
438c2ecf20Sopenharmony_ci	SENSOR_OTHER,
448c2ecf20Sopenharmony_ci	SENSOR_TAS5130A,
458c2ecf20Sopenharmony_ci	SENSOR_LT168G,		/* must verify if this is the actual model */
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic const struct v4l2_pix_format vga_mode_t16[] = {
498c2ecf20Sopenharmony_ci	{160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
508c2ecf20Sopenharmony_ci		.bytesperline = 160,
518c2ecf20Sopenharmony_ci		.sizeimage = 160 * 120 * 4 / 8 + 590,
528c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG,
538c2ecf20Sopenharmony_ci		.priv = 4},
548c2ecf20Sopenharmony_ci#if 0 /* HDG: broken with my test cam, so lets disable it */
558c2ecf20Sopenharmony_ci	{176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
568c2ecf20Sopenharmony_ci		.bytesperline = 176,
578c2ecf20Sopenharmony_ci		.sizeimage = 176 * 144 * 3 / 8 + 590,
588c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG,
598c2ecf20Sopenharmony_ci		.priv = 3},
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
628c2ecf20Sopenharmony_ci		.bytesperline = 320,
638c2ecf20Sopenharmony_ci		.sizeimage = 320 * 240 * 3 / 8 + 590,
648c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG,
658c2ecf20Sopenharmony_ci		.priv = 2},
668c2ecf20Sopenharmony_ci#if 0 /* HDG: broken with my test cam, so lets disable it */
678c2ecf20Sopenharmony_ci	{352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
688c2ecf20Sopenharmony_ci		.bytesperline = 352,
698c2ecf20Sopenharmony_ci		.sizeimage = 352 * 288 * 3 / 8 + 590,
708c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG,
718c2ecf20Sopenharmony_ci		.priv = 1},
728c2ecf20Sopenharmony_ci#endif
738c2ecf20Sopenharmony_ci	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
748c2ecf20Sopenharmony_ci		.bytesperline = 640,
758c2ecf20Sopenharmony_ci		.sizeimage = 640 * 480 * 3 / 8 + 590,
768c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG,
778c2ecf20Sopenharmony_ci		.priv = 0},
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* sensor specific data */
818c2ecf20Sopenharmony_cistruct additional_sensor_data {
828c2ecf20Sopenharmony_ci	const u8 n3[6];
838c2ecf20Sopenharmony_ci	const u8 *n4, n4sz;
848c2ecf20Sopenharmony_ci	const u8 reg80, reg8e;
858c2ecf20Sopenharmony_ci	const u8 nset8[6];
868c2ecf20Sopenharmony_ci	const u8 data1[10];
878c2ecf20Sopenharmony_ci	const u8 data2[9];
888c2ecf20Sopenharmony_ci	const u8 data3[9];
898c2ecf20Sopenharmony_ci	const u8 data5[6];
908c2ecf20Sopenharmony_ci	const u8 stream[4];
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic const u8 n4_om6802[] = {
948c2ecf20Sopenharmony_ci	0x09, 0x01, 0x12, 0x04, 0x66, 0x8a, 0x80, 0x3c,
958c2ecf20Sopenharmony_ci	0x81, 0x22, 0x84, 0x50, 0x8a, 0x78, 0x8b, 0x68,
968c2ecf20Sopenharmony_ci	0x8c, 0x88, 0x8e, 0x33, 0x8f, 0x24, 0xaa, 0xb1,
978c2ecf20Sopenharmony_ci	0xa2, 0x60, 0xa5, 0x30, 0xa6, 0x3a, 0xa8, 0xe8,
988c2ecf20Sopenharmony_ci	0xae, 0x05, 0xb1, 0x00, 0xbb, 0x04, 0xbc, 0x48,
998c2ecf20Sopenharmony_ci	0xbe, 0x36, 0xc6, 0x88, 0xe9, 0x00, 0xc5, 0xc0,
1008c2ecf20Sopenharmony_ci	0x65, 0x0a, 0xbb, 0x86, 0xaf, 0x58, 0xb0, 0x68,
1018c2ecf20Sopenharmony_ci	0x87, 0x40, 0x89, 0x2b, 0x8d, 0xff, 0x83, 0x40,
1028c2ecf20Sopenharmony_ci	0xac, 0x84, 0xad, 0x86, 0xaf, 0x46
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_cistatic const u8 n4_other[] = {
1058c2ecf20Sopenharmony_ci	0x66, 0x00, 0x7f, 0x00, 0x80, 0xac, 0x81, 0x69,
1068c2ecf20Sopenharmony_ci	0x84, 0x40, 0x85, 0x70, 0x86, 0x20, 0x8a, 0x68,
1078c2ecf20Sopenharmony_ci	0x8b, 0x58, 0x8c, 0x88, 0x8d, 0xff, 0x8e, 0xb8,
1088c2ecf20Sopenharmony_ci	0x8f, 0x28, 0xa2, 0x60, 0xa5, 0x40, 0xa8, 0xa8,
1098c2ecf20Sopenharmony_ci	0xac, 0x84, 0xad, 0x84, 0xae, 0x24, 0xaf, 0x56,
1108c2ecf20Sopenharmony_ci	0xb0, 0x68, 0xb1, 0x00, 0xb2, 0x88, 0xbb, 0xc5,
1118c2ecf20Sopenharmony_ci	0xbc, 0x4a, 0xbe, 0x36, 0xc2, 0x88, 0xc5, 0xc0,
1128c2ecf20Sopenharmony_ci	0xc6, 0xda, 0xe9, 0x26, 0xeb, 0x00
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_cistatic const u8 n4_tas5130a[] = {
1158c2ecf20Sopenharmony_ci	0x80, 0x3c, 0x81, 0x68, 0x83, 0xa0, 0x84, 0x20,
1168c2ecf20Sopenharmony_ci	0x8a, 0x68, 0x8b, 0x58, 0x8c, 0x88, 0x8e, 0xb4,
1178c2ecf20Sopenharmony_ci	0x8f, 0x24, 0xa1, 0xb1, 0xa2, 0x30, 0xa5, 0x10,
1188c2ecf20Sopenharmony_ci	0xa6, 0x4a, 0xae, 0x03, 0xb1, 0x44, 0xb2, 0x08,
1198c2ecf20Sopenharmony_ci	0xb7, 0x06, 0xb9, 0xe7, 0xbb, 0xc4, 0xbc, 0x4a,
1208c2ecf20Sopenharmony_ci	0xbe, 0x36, 0xbf, 0xff, 0xc2, 0x88, 0xc5, 0xc8,
1218c2ecf20Sopenharmony_ci	0xc6, 0xda
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_cistatic const u8 n4_lt168g[] = {
1248c2ecf20Sopenharmony_ci	0x66, 0x01, 0x7f, 0x00, 0x80, 0x7c, 0x81, 0x28,
1258c2ecf20Sopenharmony_ci	0x83, 0x44, 0x84, 0x20, 0x86, 0x20, 0x8a, 0x70,
1268c2ecf20Sopenharmony_ci	0x8b, 0x58, 0x8c, 0x88, 0x8d, 0xa0, 0x8e, 0xb3,
1278c2ecf20Sopenharmony_ci	0x8f, 0x24, 0xa1, 0xb0, 0xa2, 0x38, 0xa5, 0x20,
1288c2ecf20Sopenharmony_ci	0xa6, 0x4a, 0xa8, 0xe8, 0xaf, 0x38, 0xb0, 0x68,
1298c2ecf20Sopenharmony_ci	0xb1, 0x44, 0xb2, 0x88, 0xbb, 0x86, 0xbd, 0x40,
1308c2ecf20Sopenharmony_ci	0xbe, 0x26, 0xc1, 0x05, 0xc2, 0x88, 0xc5, 0xc0,
1318c2ecf20Sopenharmony_ci	0xda, 0x8e, 0xdb, 0xca, 0xdc, 0xa8, 0xdd, 0x8c,
1328c2ecf20Sopenharmony_ci	0xde, 0x44, 0xdf, 0x0c, 0xe9, 0x80
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic const struct additional_sensor_data sensor_data[] = {
1368c2ecf20Sopenharmony_ci[SENSOR_OM6802] = {
1378c2ecf20Sopenharmony_ci	.n3 =
1388c2ecf20Sopenharmony_ci		{0x61, 0x68, 0x65, 0x0a, 0x60, 0x04},
1398c2ecf20Sopenharmony_ci	.n4 = n4_om6802,
1408c2ecf20Sopenharmony_ci	.n4sz = sizeof n4_om6802,
1418c2ecf20Sopenharmony_ci	.reg80 = 0x3c,
1428c2ecf20Sopenharmony_ci	.reg8e = 0x33,
1438c2ecf20Sopenharmony_ci	.nset8 = {0xa8, 0xf0, 0xc6, 0x88, 0xc0, 0x00},
1448c2ecf20Sopenharmony_ci	.data1 =
1458c2ecf20Sopenharmony_ci		{0xc2, 0x28, 0x0f, 0x22, 0xcd, 0x27, 0x2c, 0x06,
1468c2ecf20Sopenharmony_ci		 0xb3, 0xfc},
1478c2ecf20Sopenharmony_ci	.data2 =
1488c2ecf20Sopenharmony_ci		{0x80, 0xff, 0xff, 0x80, 0xff, 0xff, 0x80, 0xff,
1498c2ecf20Sopenharmony_ci		 0xff},
1508c2ecf20Sopenharmony_ci	.data3 =
1518c2ecf20Sopenharmony_ci		{0x80, 0xff, 0xff, 0x80, 0xff, 0xff, 0x80, 0xff,
1528c2ecf20Sopenharmony_ci		 0xff},
1538c2ecf20Sopenharmony_ci	.data5 =	/* this could be removed later */
1548c2ecf20Sopenharmony_ci		{0x0c, 0x03, 0xab, 0x13, 0x81, 0x23},
1558c2ecf20Sopenharmony_ci	.stream =
1568c2ecf20Sopenharmony_ci		{0x0b, 0x04, 0x0a, 0x78},
1578c2ecf20Sopenharmony_ci    },
1588c2ecf20Sopenharmony_ci[SENSOR_OTHER] = {
1598c2ecf20Sopenharmony_ci	.n3 =
1608c2ecf20Sopenharmony_ci		{0x61, 0xc2, 0x65, 0x88, 0x60, 0x00},
1618c2ecf20Sopenharmony_ci	.n4 = n4_other,
1628c2ecf20Sopenharmony_ci	.n4sz = sizeof n4_other,
1638c2ecf20Sopenharmony_ci	.reg80 = 0xac,
1648c2ecf20Sopenharmony_ci	.reg8e = 0xb8,
1658c2ecf20Sopenharmony_ci	.nset8 = {0xa8, 0xa8, 0xc6, 0xda, 0xc0, 0x00},
1668c2ecf20Sopenharmony_ci	.data1 =
1678c2ecf20Sopenharmony_ci		{0xc1, 0x48, 0x04, 0x1b, 0xca, 0x2e, 0x33, 0x3a,
1688c2ecf20Sopenharmony_ci		 0xe8, 0xfc},
1698c2ecf20Sopenharmony_ci	.data2 =
1708c2ecf20Sopenharmony_ci		{0x4e, 0x9c, 0xec, 0x40, 0x80, 0xc0, 0x48, 0x96,
1718c2ecf20Sopenharmony_ci		 0xd9},
1728c2ecf20Sopenharmony_ci	.data3 =
1738c2ecf20Sopenharmony_ci		{0x4e, 0x9c, 0xec, 0x40, 0x80, 0xc0, 0x48, 0x96,
1748c2ecf20Sopenharmony_ci		 0xd9},
1758c2ecf20Sopenharmony_ci	.data5 =
1768c2ecf20Sopenharmony_ci		{0x0c, 0x03, 0xab, 0x29, 0x81, 0x69},
1778c2ecf20Sopenharmony_ci	.stream =
1788c2ecf20Sopenharmony_ci		{0x0b, 0x04, 0x0a, 0x00},
1798c2ecf20Sopenharmony_ci    },
1808c2ecf20Sopenharmony_ci[SENSOR_TAS5130A] = {
1818c2ecf20Sopenharmony_ci	.n3 =
1828c2ecf20Sopenharmony_ci		{0x61, 0xc2, 0x65, 0x0d, 0x60, 0x08},
1838c2ecf20Sopenharmony_ci	.n4 = n4_tas5130a,
1848c2ecf20Sopenharmony_ci	.n4sz = sizeof n4_tas5130a,
1858c2ecf20Sopenharmony_ci	.reg80 = 0x3c,
1868c2ecf20Sopenharmony_ci	.reg8e = 0xb4,
1878c2ecf20Sopenharmony_ci	.nset8 = {0xa8, 0xf0, 0xc6, 0xda, 0xc0, 0x00},
1888c2ecf20Sopenharmony_ci	.data1 =
1898c2ecf20Sopenharmony_ci		{0xbb, 0x28, 0x10, 0x10, 0xbb, 0x28, 0x1e, 0x27,
1908c2ecf20Sopenharmony_ci		 0xc8, 0xfc},
1918c2ecf20Sopenharmony_ci	.data2 =
1928c2ecf20Sopenharmony_ci		{0x60, 0xa8, 0xe0, 0x60, 0xa8, 0xe0, 0x60, 0xa8,
1938c2ecf20Sopenharmony_ci		 0xe0},
1948c2ecf20Sopenharmony_ci	.data3 =
1958c2ecf20Sopenharmony_ci		{0x60, 0xa8, 0xe0, 0x60, 0xa8, 0xe0, 0x60, 0xa8,
1968c2ecf20Sopenharmony_ci		 0xe0},
1978c2ecf20Sopenharmony_ci	.data5 =
1988c2ecf20Sopenharmony_ci		{0x0c, 0x03, 0xab, 0x10, 0x81, 0x20},
1998c2ecf20Sopenharmony_ci	.stream =
2008c2ecf20Sopenharmony_ci		{0x0b, 0x04, 0x0a, 0x40},
2018c2ecf20Sopenharmony_ci    },
2028c2ecf20Sopenharmony_ci[SENSOR_LT168G] = {
2038c2ecf20Sopenharmony_ci	.n3 = {0x61, 0xc2, 0x65, 0x68, 0x60, 0x00},
2048c2ecf20Sopenharmony_ci	.n4 = n4_lt168g,
2058c2ecf20Sopenharmony_ci	.n4sz = sizeof n4_lt168g,
2068c2ecf20Sopenharmony_ci	.reg80 = 0x7c,
2078c2ecf20Sopenharmony_ci	.reg8e = 0xb3,
2088c2ecf20Sopenharmony_ci	.nset8 = {0xa8, 0xf0, 0xc6, 0xba, 0xc0, 0x00},
2098c2ecf20Sopenharmony_ci	.data1 = {0xc0, 0x38, 0x08, 0x10, 0xc0, 0x30, 0x10, 0x40,
2108c2ecf20Sopenharmony_ci		 0xb0, 0xf4},
2118c2ecf20Sopenharmony_ci	.data2 = {0x40, 0x80, 0xc0, 0x50, 0xa0, 0xf0, 0x53, 0xa6,
2128c2ecf20Sopenharmony_ci		 0xff},
2138c2ecf20Sopenharmony_ci	.data3 = {0x40, 0x80, 0xc0, 0x50, 0xa0, 0xf0, 0x53, 0xa6,
2148c2ecf20Sopenharmony_ci		 0xff},
2158c2ecf20Sopenharmony_ci	.data5 = {0x0c, 0x03, 0xab, 0x4b, 0x81, 0x2b},
2168c2ecf20Sopenharmony_ci	.stream = {0x0b, 0x04, 0x0a, 0x28},
2178c2ecf20Sopenharmony_ci    },
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#define MAX_EFFECTS 7
2218c2ecf20Sopenharmony_cistatic const u8 effects_table[MAX_EFFECTS][6] = {
2228c2ecf20Sopenharmony_ci	{0xa8, 0xe8, 0xc6, 0xd2, 0xc0, 0x00},	/* Normal */
2238c2ecf20Sopenharmony_ci	{0xa8, 0xc8, 0xc6, 0x52, 0xc0, 0x04},	/* Repujar */
2248c2ecf20Sopenharmony_ci	{0xa8, 0xe8, 0xc6, 0xd2, 0xc0, 0x20},	/* Monochrome */
2258c2ecf20Sopenharmony_ci	{0xa8, 0xe8, 0xc6, 0xd2, 0xc0, 0x80},	/* Sepia */
2268c2ecf20Sopenharmony_ci	{0xa8, 0xc8, 0xc6, 0x52, 0xc0, 0x02},	/* Croquis */
2278c2ecf20Sopenharmony_ci	{0xa8, 0xc8, 0xc6, 0xd2, 0xc0, 0x10},	/* Sun Effect */
2288c2ecf20Sopenharmony_ci	{0xa8, 0xc8, 0xc6, 0xd2, 0xc0, 0x40},	/* Negative */
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define GAMMA_MAX (15)
2328c2ecf20Sopenharmony_cistatic const u8 gamma_table[GAMMA_MAX+1][17] = {
2338c2ecf20Sopenharmony_ci/* gamma table from cam1690.ini */
2348c2ecf20Sopenharmony_ci	{0x00, 0x00, 0x01, 0x04, 0x08, 0x0e, 0x16, 0x21,	/* 0 */
2358c2ecf20Sopenharmony_ci	 0x2e, 0x3d, 0x50, 0x65, 0x7d, 0x99, 0xb8, 0xdb,
2368c2ecf20Sopenharmony_ci	 0xff},
2378c2ecf20Sopenharmony_ci	{0x00, 0x01, 0x03, 0x08, 0x0e, 0x16, 0x21, 0x2d,	/* 1 */
2388c2ecf20Sopenharmony_ci	 0x3c, 0x4d, 0x60, 0x75, 0x8d, 0xa6, 0xc2, 0xe1,
2398c2ecf20Sopenharmony_ci	 0xff},
2408c2ecf20Sopenharmony_ci	{0x00, 0x01, 0x05, 0x0b, 0x12, 0x1c, 0x28, 0x35,	/* 2 */
2418c2ecf20Sopenharmony_ci	 0x45, 0x56, 0x69, 0x7e, 0x95, 0xad, 0xc7, 0xe3,
2428c2ecf20Sopenharmony_ci	 0xff},
2438c2ecf20Sopenharmony_ci	{0x00, 0x02, 0x07, 0x0f, 0x18, 0x24, 0x30, 0x3f,	/* 3 */
2448c2ecf20Sopenharmony_ci	 0x4f, 0x61, 0x73, 0x88, 0x9d, 0xb4, 0xcd, 0xe6,
2458c2ecf20Sopenharmony_ci	 0xff},
2468c2ecf20Sopenharmony_ci	{0x00, 0x04, 0x0b, 0x15, 0x20, 0x2d, 0x3b, 0x4a,	/* 4 */
2478c2ecf20Sopenharmony_ci	 0x5b, 0x6c, 0x7f, 0x92, 0xa7, 0xbc, 0xd2, 0xe9,
2488c2ecf20Sopenharmony_ci	 0xff},
2498c2ecf20Sopenharmony_ci	{0x00, 0x07, 0x11, 0x15, 0x20, 0x2d, 0x48, 0x58,	/* 5 */
2508c2ecf20Sopenharmony_ci	 0x68, 0x79, 0x8b, 0x9d, 0xb0, 0xc4, 0xd7, 0xec,
2518c2ecf20Sopenharmony_ci	 0xff},
2528c2ecf20Sopenharmony_ci	{0x00, 0x0c, 0x1a, 0x29, 0x38, 0x47, 0x57, 0x67,	/* 6 */
2538c2ecf20Sopenharmony_ci	 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
2548c2ecf20Sopenharmony_ci	 0xff},
2558c2ecf20Sopenharmony_ci	{0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,	/* 7 */
2568c2ecf20Sopenharmony_ci	 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
2578c2ecf20Sopenharmony_ci	 0xff},
2588c2ecf20Sopenharmony_ci	{0x00, 0x15, 0x27, 0x38, 0x49, 0x59, 0x69, 0x79,	/* 8 */
2598c2ecf20Sopenharmony_ci	 0x88, 0x97, 0xa7, 0xb6, 0xc4, 0xd3, 0xe2, 0xf0,
2608c2ecf20Sopenharmony_ci	 0xff},
2618c2ecf20Sopenharmony_ci	{0x00, 0x1c, 0x30, 0x43, 0x54, 0x65, 0x75, 0x84,	/* 9 */
2628c2ecf20Sopenharmony_ci	 0x93, 0xa1, 0xb0, 0xbd, 0xca, 0xd8, 0xe5, 0xf2,
2638c2ecf20Sopenharmony_ci	 0xff},
2648c2ecf20Sopenharmony_ci	{0x00, 0x24, 0x3b, 0x4f, 0x60, 0x70, 0x80, 0x8e,	/* 10 */
2658c2ecf20Sopenharmony_ci	 0x9c, 0xaa, 0xb7, 0xc4, 0xd0, 0xdc, 0xe8, 0xf3,
2668c2ecf20Sopenharmony_ci	 0xff},
2678c2ecf20Sopenharmony_ci	{0x00, 0x2a, 0x3c, 0x5d, 0x6e, 0x7e, 0x8d, 0x9b,	/* 11 */
2688c2ecf20Sopenharmony_ci	 0xa8, 0xb4, 0xc0, 0xcb, 0xd6, 0xe1, 0xeb, 0xf5,
2698c2ecf20Sopenharmony_ci	 0xff},
2708c2ecf20Sopenharmony_ci	{0x00, 0x3f, 0x5a, 0x6e, 0x7f, 0x8e, 0x9c, 0xa8,	/* 12 */
2718c2ecf20Sopenharmony_ci	 0xb4, 0xbf, 0xc9, 0xd3, 0xdc, 0xe5, 0xee, 0xf6,
2728c2ecf20Sopenharmony_ci	 0xff},
2738c2ecf20Sopenharmony_ci	{0x00, 0x54, 0x6f, 0x83, 0x93, 0xa0, 0xad, 0xb7,	/* 13 */
2748c2ecf20Sopenharmony_ci	 0xc2, 0xcb, 0xd4, 0xdc, 0xe4, 0xeb, 0xf2, 0xf9,
2758c2ecf20Sopenharmony_ci	 0xff},
2768c2ecf20Sopenharmony_ci	{0x00, 0x6e, 0x88, 0x9a, 0xa8, 0xb3, 0xbd, 0xc6,	/* 14 */
2778c2ecf20Sopenharmony_ci	 0xcf, 0xd6, 0xdd, 0xe3, 0xe9, 0xef, 0xf4, 0xfa,
2788c2ecf20Sopenharmony_ci	 0xff},
2798c2ecf20Sopenharmony_ci	{0x00, 0x93, 0xa8, 0xb7, 0xc1, 0xca, 0xd2, 0xd8,	/* 15 */
2808c2ecf20Sopenharmony_ci	 0xde, 0xe3, 0xe8, 0xed, 0xf1, 0xf5, 0xf8, 0xfc,
2818c2ecf20Sopenharmony_ci	 0xff}
2828c2ecf20Sopenharmony_ci};
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic const u8 tas5130a_sensor_init[][8] = {
2858c2ecf20Sopenharmony_ci	{0x62, 0x08, 0x63, 0x70, 0x64, 0x1d, 0x60, 0x09},
2868c2ecf20Sopenharmony_ci	{0x62, 0x20, 0x63, 0x01, 0x64, 0x02, 0x60, 0x09},
2878c2ecf20Sopenharmony_ci	{0x62, 0x07, 0x63, 0x03, 0x64, 0x00, 0x60, 0x09},
2888c2ecf20Sopenharmony_ci};
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic u8 sensor_reset[] = {0x61, 0x68, 0x62, 0xff, 0x60, 0x07};
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/* read 1 byte */
2938c2ecf20Sopenharmony_cistatic u8 reg_r(struct gspca_dev *gspca_dev,
2948c2ecf20Sopenharmony_ci		   u16 index)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	usb_control_msg(gspca_dev->dev,
2978c2ecf20Sopenharmony_ci			usb_rcvctrlpipe(gspca_dev->dev, 0),
2988c2ecf20Sopenharmony_ci			0,		/* request */
2998c2ecf20Sopenharmony_ci			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3008c2ecf20Sopenharmony_ci			0,		/* value */
3018c2ecf20Sopenharmony_ci			index,
3028c2ecf20Sopenharmony_ci			gspca_dev->usb_buf, 1, 500);
3038c2ecf20Sopenharmony_ci	return gspca_dev->usb_buf[0];
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic void reg_w(struct gspca_dev *gspca_dev,
3078c2ecf20Sopenharmony_ci		  u16 index)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	usb_control_msg(gspca_dev->dev,
3108c2ecf20Sopenharmony_ci			usb_sndctrlpipe(gspca_dev->dev, 0),
3118c2ecf20Sopenharmony_ci			0,
3128c2ecf20Sopenharmony_ci			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3138c2ecf20Sopenharmony_ci			0, index,
3148c2ecf20Sopenharmony_ci			NULL, 0, 500);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void reg_w_buf(struct gspca_dev *gspca_dev,
3188c2ecf20Sopenharmony_ci		  const u8 *buffer, u16 len)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	if (len <= USB_BUF_SZ) {
3218c2ecf20Sopenharmony_ci		memcpy(gspca_dev->usb_buf, buffer, len);
3228c2ecf20Sopenharmony_ci		usb_control_msg(gspca_dev->dev,
3238c2ecf20Sopenharmony_ci				usb_sndctrlpipe(gspca_dev->dev, 0),
3248c2ecf20Sopenharmony_ci				0,
3258c2ecf20Sopenharmony_ci			   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3268c2ecf20Sopenharmony_ci				0x01, 0,
3278c2ecf20Sopenharmony_ci				gspca_dev->usb_buf, len, 500);
3288c2ecf20Sopenharmony_ci	} else {
3298c2ecf20Sopenharmony_ci		u8 *tmpbuf;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		tmpbuf = kmemdup(buffer, len, GFP_KERNEL);
3328c2ecf20Sopenharmony_ci		if (!tmpbuf) {
3338c2ecf20Sopenharmony_ci			pr_err("Out of memory\n");
3348c2ecf20Sopenharmony_ci			return;
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci		usb_control_msg(gspca_dev->dev,
3378c2ecf20Sopenharmony_ci				usb_sndctrlpipe(gspca_dev->dev, 0),
3388c2ecf20Sopenharmony_ci				0,
3398c2ecf20Sopenharmony_ci			   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3408c2ecf20Sopenharmony_ci				0x01, 0,
3418c2ecf20Sopenharmony_ci				tmpbuf, len, 500);
3428c2ecf20Sopenharmony_ci		kfree(tmpbuf);
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci/* write values to consecutive registers */
3478c2ecf20Sopenharmony_cistatic void reg_w_ixbuf(struct gspca_dev *gspca_dev,
3488c2ecf20Sopenharmony_ci			u8 reg,
3498c2ecf20Sopenharmony_ci			const u8 *buffer, u16 len)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	int i;
3528c2ecf20Sopenharmony_ci	u8 *p, *tmpbuf;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (len * 2 <= USB_BUF_SZ) {
3558c2ecf20Sopenharmony_ci		p = tmpbuf = gspca_dev->usb_buf;
3568c2ecf20Sopenharmony_ci	} else {
3578c2ecf20Sopenharmony_ci		p = tmpbuf = kmalloc_array(len, 2, GFP_KERNEL);
3588c2ecf20Sopenharmony_ci		if (!tmpbuf) {
3598c2ecf20Sopenharmony_ci			pr_err("Out of memory\n");
3608c2ecf20Sopenharmony_ci			return;
3618c2ecf20Sopenharmony_ci		}
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci	i = len;
3648c2ecf20Sopenharmony_ci	while (--i >= 0) {
3658c2ecf20Sopenharmony_ci		*p++ = reg++;
3668c2ecf20Sopenharmony_ci		*p++ = *buffer++;
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	usb_control_msg(gspca_dev->dev,
3698c2ecf20Sopenharmony_ci			usb_sndctrlpipe(gspca_dev->dev, 0),
3708c2ecf20Sopenharmony_ci			0,
3718c2ecf20Sopenharmony_ci			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3728c2ecf20Sopenharmony_ci			0x01, 0,
3738c2ecf20Sopenharmony_ci			tmpbuf, len * 2, 500);
3748c2ecf20Sopenharmony_ci	if (len * 2 > USB_BUF_SZ)
3758c2ecf20Sopenharmony_ci		kfree(tmpbuf);
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic void om6802_sensor_init(struct gspca_dev *gspca_dev)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	int i;
3818c2ecf20Sopenharmony_ci	const u8 *p;
3828c2ecf20Sopenharmony_ci	u8 byte;
3838c2ecf20Sopenharmony_ci	u8 val[6] = {0x62, 0, 0x64, 0, 0x60, 0x05};
3848c2ecf20Sopenharmony_ci	static const u8 sensor_init[] = {
3858c2ecf20Sopenharmony_ci		0xdf, 0x6d,
3868c2ecf20Sopenharmony_ci		0xdd, 0x18,
3878c2ecf20Sopenharmony_ci		0x5a, 0xe0,
3888c2ecf20Sopenharmony_ci		0x5c, 0x07,
3898c2ecf20Sopenharmony_ci		0x5d, 0xb0,
3908c2ecf20Sopenharmony_ci		0x5e, 0x1e,
3918c2ecf20Sopenharmony_ci		0x60, 0x71,
3928c2ecf20Sopenharmony_ci		0xef, 0x00,
3938c2ecf20Sopenharmony_ci		0xe9, 0x00,
3948c2ecf20Sopenharmony_ci		0xea, 0x00,
3958c2ecf20Sopenharmony_ci		0x90, 0x24,
3968c2ecf20Sopenharmony_ci		0x91, 0xb2,
3978c2ecf20Sopenharmony_ci		0x82, 0x32,
3988c2ecf20Sopenharmony_ci		0xfd, 0x41,
3998c2ecf20Sopenharmony_ci		0x00			/* table end */
4008c2ecf20Sopenharmony_ci	};
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor_reset, sizeof sensor_reset);
4038c2ecf20Sopenharmony_ci	msleep(100);
4048c2ecf20Sopenharmony_ci	i = 4;
4058c2ecf20Sopenharmony_ci	while (--i > 0) {
4068c2ecf20Sopenharmony_ci		byte = reg_r(gspca_dev, 0x0060);
4078c2ecf20Sopenharmony_ci		if (!(byte & 0x01))
4088c2ecf20Sopenharmony_ci			break;
4098c2ecf20Sopenharmony_ci		msleep(100);
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	byte = reg_r(gspca_dev, 0x0063);
4128c2ecf20Sopenharmony_ci	if (byte != 0x17) {
4138c2ecf20Sopenharmony_ci		pr_err("Bad sensor reset %02x\n", byte);
4148c2ecf20Sopenharmony_ci		/* continue? */
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	p = sensor_init;
4188c2ecf20Sopenharmony_ci	while (*p != 0) {
4198c2ecf20Sopenharmony_ci		val[1] = *p++;
4208c2ecf20Sopenharmony_ci		val[3] = *p++;
4218c2ecf20Sopenharmony_ci		if (*p == 0)
4228c2ecf20Sopenharmony_ci			reg_w(gspca_dev, 0x3c80);
4238c2ecf20Sopenharmony_ci		reg_w_buf(gspca_dev, val, sizeof val);
4248c2ecf20Sopenharmony_ci		i = 4;
4258c2ecf20Sopenharmony_ci		while (--i >= 0) {
4268c2ecf20Sopenharmony_ci			msleep(15);
4278c2ecf20Sopenharmony_ci			byte = reg_r(gspca_dev, 0x60);
4288c2ecf20Sopenharmony_ci			if (!(byte & 0x01))
4298c2ecf20Sopenharmony_ci				break;
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci	msleep(15);
4338c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x3c80);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/* this function is called at probe time */
4378c2ecf20Sopenharmony_cistatic int sd_config(struct gspca_dev *gspca_dev,
4388c2ecf20Sopenharmony_ci		     const struct usb_device_id *id)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	struct cam *cam  = &gspca_dev->cam;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	cam->cam_mode = vga_mode_t16;
4438c2ecf20Sopenharmony_ci	cam->nmodes = ARRAY_SIZE(vga_mode_t16);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	return 0;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	u8 set6[4] = { 0x8f, 0x24, 0xc3, 0x00 };
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (brightness < 7) {
4538c2ecf20Sopenharmony_ci		set6[1] = 0x26;
4548c2ecf20Sopenharmony_ci		set6[3] = 0x70 - brightness * 0x10;
4558c2ecf20Sopenharmony_ci	} else {
4568c2ecf20Sopenharmony_ci		set6[3] = 0x00 + ((brightness - 7) * 0x10);
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, set6, sizeof set6);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic void setcontrast(struct gspca_dev *gspca_dev, s32 contrast)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	u16 reg_to_write;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (contrast < 7)
4678c2ecf20Sopenharmony_ci		reg_to_write = 0x8ea9 - contrast * 0x200;
4688c2ecf20Sopenharmony_ci	else
4698c2ecf20Sopenharmony_ci		reg_to_write = 0x00a9 + (contrast - 7) * 0x200;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	reg_w(gspca_dev, reg_to_write);
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic void setcolors(struct gspca_dev *gspca_dev, s32 val)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	u16 reg_to_write;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	reg_to_write = 0x80bb + val * 0x100;	/* was 0xc0 */
4798c2ecf20Sopenharmony_ci	reg_w(gspca_dev, reg_to_write);
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic void setgamma(struct gspca_dev *gspca_dev, s32 val)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Gamma: %d\n", val);
4858c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0x90,
4868c2ecf20Sopenharmony_ci		gamma_table[val], sizeof gamma_table[0]);
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic void setawb_n_RGB(struct gspca_dev *gspca_dev)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
4928c2ecf20Sopenharmony_ci	u8 all_gain_reg[8] = {
4938c2ecf20Sopenharmony_ci		0x87, 0x00, 0x88, 0x00, 0x89, 0x00, 0x80, 0x00 };
4948c2ecf20Sopenharmony_ci	s32 red_gain, blue_gain, green_gain;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	green_gain = sd->gain->val;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	red_gain = green_gain + sd->red_balance->val;
4998c2ecf20Sopenharmony_ci	if (red_gain > 0x40)
5008c2ecf20Sopenharmony_ci		red_gain = 0x40;
5018c2ecf20Sopenharmony_ci	else if (red_gain < 0x10)
5028c2ecf20Sopenharmony_ci		red_gain = 0x10;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	blue_gain = green_gain + sd->blue_balance->val;
5058c2ecf20Sopenharmony_ci	if (blue_gain > 0x40)
5068c2ecf20Sopenharmony_ci		blue_gain = 0x40;
5078c2ecf20Sopenharmony_ci	else if (blue_gain < 0x10)
5088c2ecf20Sopenharmony_ci		blue_gain = 0x10;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	all_gain_reg[1] = red_gain;
5118c2ecf20Sopenharmony_ci	all_gain_reg[3] = blue_gain;
5128c2ecf20Sopenharmony_ci	all_gain_reg[5] = green_gain;
5138c2ecf20Sopenharmony_ci	all_gain_reg[7] = sensor_data[sd->sensor].reg80;
5148c2ecf20Sopenharmony_ci	if (!sd->awb->val)
5158c2ecf20Sopenharmony_ci		all_gain_reg[7] &= ~0x04; /* AWB off */
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, all_gain_reg, sizeof all_gain_reg);
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic void setsharpness(struct gspca_dev *gspca_dev, s32 val)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	u16 reg_to_write;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	reg_to_write = 0x0aa6 + 0x1000 * val;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	reg_w(gspca_dev, reg_to_write);
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic void setfreq(struct gspca_dev *gspca_dev, s32 val)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
5328c2ecf20Sopenharmony_ci	u8 reg66;
5338c2ecf20Sopenharmony_ci	u8 freq[4] = { 0x66, 0x00, 0xa8, 0xe8 };
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	switch (sd->sensor) {
5368c2ecf20Sopenharmony_ci	case SENSOR_LT168G:
5378c2ecf20Sopenharmony_ci		if (val != 0)
5388c2ecf20Sopenharmony_ci			freq[3] = 0xa8;
5398c2ecf20Sopenharmony_ci		reg66 = 0x41;
5408c2ecf20Sopenharmony_ci		break;
5418c2ecf20Sopenharmony_ci	case SENSOR_OM6802:
5428c2ecf20Sopenharmony_ci		reg66 = 0xca;
5438c2ecf20Sopenharmony_ci		break;
5448c2ecf20Sopenharmony_ci	default:
5458c2ecf20Sopenharmony_ci		reg66 = 0x40;
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci	switch (val) {
5498c2ecf20Sopenharmony_ci	case 0:				/* no flicker */
5508c2ecf20Sopenharmony_ci		freq[3] = 0xf0;
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci	case 2:				/* 60Hz */
5538c2ecf20Sopenharmony_ci		reg66 &= ~0x40;
5548c2ecf20Sopenharmony_ci		break;
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci	freq[1] = reg66;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, freq, sizeof freq);
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/* this function is called at probe and resume time */
5628c2ecf20Sopenharmony_cistatic int sd_init(struct gspca_dev *gspca_dev)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	/* some of this registers are not really needed, because
5658c2ecf20Sopenharmony_ci	 * they are overridden by setbrigthness, setcontrast, etc.,
5668c2ecf20Sopenharmony_ci	 * but won't hurt anyway, and can help someone with similar webcam
5678c2ecf20Sopenharmony_ci	 * to see the initial parameters.*/
5688c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
5698c2ecf20Sopenharmony_ci	const struct additional_sensor_data *sensor;
5708c2ecf20Sopenharmony_ci	int i;
5718c2ecf20Sopenharmony_ci	u16 sensor_id;
5728c2ecf20Sopenharmony_ci	u8 test_byte = 0;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	static const u8 read_indexs[] =
5758c2ecf20Sopenharmony_ci		{ 0x0a, 0x0b, 0x66, 0x80, 0x81, 0x8e, 0x8f, 0xa5,
5768c2ecf20Sopenharmony_ci		  0xa6, 0xa8, 0xbb, 0xbc, 0xc6, 0x00 };
5778c2ecf20Sopenharmony_ci	static const u8 n1[] =
5788c2ecf20Sopenharmony_ci			{0x08, 0x03, 0x09, 0x03, 0x12, 0x04};
5798c2ecf20Sopenharmony_ci	static const u8 n2[] =
5808c2ecf20Sopenharmony_ci			{0x08, 0x00};
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	sensor_id = (reg_r(gspca_dev, 0x06) << 8)
5838c2ecf20Sopenharmony_ci			| reg_r(gspca_dev, 0x07);
5848c2ecf20Sopenharmony_ci	switch (sensor_id & 0xff0f) {
5858c2ecf20Sopenharmony_ci	case 0x0801:
5868c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_PROBE, "sensor tas5130a\n");
5878c2ecf20Sopenharmony_ci		sd->sensor = SENSOR_TAS5130A;
5888c2ecf20Sopenharmony_ci		break;
5898c2ecf20Sopenharmony_ci	case 0x0802:
5908c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_PROBE, "sensor lt168g\n");
5918c2ecf20Sopenharmony_ci		sd->sensor = SENSOR_LT168G;
5928c2ecf20Sopenharmony_ci		break;
5938c2ecf20Sopenharmony_ci	case 0x0803:
5948c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_PROBE, "sensor 'other'\n");
5958c2ecf20Sopenharmony_ci		sd->sensor = SENSOR_OTHER;
5968c2ecf20Sopenharmony_ci		break;
5978c2ecf20Sopenharmony_ci	case 0x0807:
5988c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_PROBE, "sensor om6802\n");
5998c2ecf20Sopenharmony_ci		sd->sensor = SENSOR_OM6802;
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	default:
6028c2ecf20Sopenharmony_ci		pr_err("unknown sensor %04x\n", sensor_id);
6038c2ecf20Sopenharmony_ci		return -EINVAL;
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_OM6802) {
6078c2ecf20Sopenharmony_ci		reg_w_buf(gspca_dev, n1, sizeof n1);
6088c2ecf20Sopenharmony_ci		i = 5;
6098c2ecf20Sopenharmony_ci		while (--i >= 0) {
6108c2ecf20Sopenharmony_ci			reg_w_buf(gspca_dev, sensor_reset, sizeof sensor_reset);
6118c2ecf20Sopenharmony_ci			test_byte = reg_r(gspca_dev, 0x0063);
6128c2ecf20Sopenharmony_ci			msleep(100);
6138c2ecf20Sopenharmony_ci			if (test_byte == 0x17)
6148c2ecf20Sopenharmony_ci				break;		/* OK */
6158c2ecf20Sopenharmony_ci		}
6168c2ecf20Sopenharmony_ci		if (i < 0) {
6178c2ecf20Sopenharmony_ci			pr_err("Bad sensor reset %02x\n", test_byte);
6188c2ecf20Sopenharmony_ci			return -EIO;
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci		reg_w_buf(gspca_dev, n2, sizeof n2);
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	i = 0;
6248c2ecf20Sopenharmony_ci	while (read_indexs[i] != 0x00) {
6258c2ecf20Sopenharmony_ci		test_byte = reg_r(gspca_dev, read_indexs[i]);
6268c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_STREAM, "Reg 0x%02x = 0x%02x\n",
6278c2ecf20Sopenharmony_ci			  read_indexs[i], test_byte);
6288c2ecf20Sopenharmony_ci		i++;
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	sensor = &sensor_data[sd->sensor];
6328c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->n3, sizeof sensor->n3);
6338c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->n4, sensor->n4sz);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_LT168G) {
6368c2ecf20Sopenharmony_ci		test_byte = reg_r(gspca_dev, 0x80);
6378c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_STREAM, "Reg 0x%02x = 0x%02x\n", 0x80,
6388c2ecf20Sopenharmony_ci			  test_byte);
6398c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x6c80);
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xd0, sensor->data1, sizeof sensor->data1);
6438c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xc7, sensor->data2, sizeof sensor->data2);
6448c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xe0, sensor->data3, sizeof sensor->data3);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80);
6478c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (sensor->reg80 << 8) + 0x80);
6488c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (sensor->reg8e << 8) + 0x8e);
6498c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (0x20 << 8) + 0x87);
6508c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (0x20 << 8) + 0x88);
6518c2ecf20Sopenharmony_ci	reg_w(gspca_dev, (0x20 << 8) + 0x89);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->data5, sizeof sensor->data5);
6548c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->nset8, sizeof sensor->nset8);
6558c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->stream, sizeof sensor->stream);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_LT168G) {
6588c2ecf20Sopenharmony_ci		test_byte = reg_r(gspca_dev, 0x80);
6598c2ecf20Sopenharmony_ci		gspca_dbg(gspca_dev, D_STREAM, "Reg 0x%02x = 0x%02x\n", 0x80,
6608c2ecf20Sopenharmony_ci			  test_byte);
6618c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x6c80);
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xd0, sensor->data1, sizeof sensor->data1);
6658c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xc7, sensor->data2, sizeof sensor->data2);
6668c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xe0, sensor->data3, sizeof sensor->data3);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	return 0;
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic void setmirror(struct gspca_dev *gspca_dev, s32 val)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	u8 hflipcmd[8] =
6748c2ecf20Sopenharmony_ci		{0x62, 0x07, 0x63, 0x03, 0x64, 0x00, 0x60, 0x09};
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	if (val)
6778c2ecf20Sopenharmony_ci		hflipcmd[3] = 0x01;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, hflipcmd, sizeof hflipcmd);
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic void seteffect(struct gspca_dev *gspca_dev, s32 val)
6838c2ecf20Sopenharmony_ci{
6848c2ecf20Sopenharmony_ci	int idx = 0;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	switch (val) {
6878c2ecf20Sopenharmony_ci	case V4L2_COLORFX_NONE:
6888c2ecf20Sopenharmony_ci		break;
6898c2ecf20Sopenharmony_ci	case V4L2_COLORFX_BW:
6908c2ecf20Sopenharmony_ci		idx = 2;
6918c2ecf20Sopenharmony_ci		break;
6928c2ecf20Sopenharmony_ci	case V4L2_COLORFX_SEPIA:
6938c2ecf20Sopenharmony_ci		idx = 3;
6948c2ecf20Sopenharmony_ci		break;
6958c2ecf20Sopenharmony_ci	case V4L2_COLORFX_SKETCH:
6968c2ecf20Sopenharmony_ci		idx = 4;
6978c2ecf20Sopenharmony_ci		break;
6988c2ecf20Sopenharmony_ci	case V4L2_COLORFX_NEGATIVE:
6998c2ecf20Sopenharmony_ci		idx = 6;
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci	default:
7028c2ecf20Sopenharmony_ci		break;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, effects_table[idx],
7068c2ecf20Sopenharmony_ci				sizeof effects_table[0]);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (val == V4L2_COLORFX_SKETCH)
7098c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x4aa6);
7108c2ecf20Sopenharmony_ci	else
7118c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0xfaa6);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci/* Is this really needed?
7158c2ecf20Sopenharmony_ci * i added some module parameters for test with some users */
7168c2ecf20Sopenharmony_cistatic void poll_sensor(struct gspca_dev *gspca_dev)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	static const u8 poll1[] =
7198c2ecf20Sopenharmony_ci		{0x67, 0x05, 0x68, 0x81, 0x69, 0x80, 0x6a, 0x82,
7208c2ecf20Sopenharmony_ci		 0x6b, 0x68, 0x6c, 0x69, 0x72, 0xd9, 0x73, 0x34,
7218c2ecf20Sopenharmony_ci		 0x74, 0x32, 0x75, 0x92, 0x76, 0x00, 0x09, 0x01,
7228c2ecf20Sopenharmony_ci		 0x60, 0x14};
7238c2ecf20Sopenharmony_ci	static const u8 poll2[] =
7248c2ecf20Sopenharmony_ci		{0x67, 0x02, 0x68, 0x71, 0x69, 0x72, 0x72, 0xa9,
7258c2ecf20Sopenharmony_ci		 0x73, 0x02, 0x73, 0x02, 0x60, 0x14};
7268c2ecf20Sopenharmony_ci	static const u8 noise03[] =	/* (some differences / ms-drv) */
7278c2ecf20Sopenharmony_ci		{0xa6, 0x0a, 0xea, 0xcf, 0xbe, 0x26, 0xb1, 0x5f,
7288c2ecf20Sopenharmony_ci		 0xa1, 0xb1, 0xda, 0x6b, 0xdb, 0x98, 0xdf, 0x0c,
7298c2ecf20Sopenharmony_ci		 0xc2, 0x80, 0xc3, 0x10};
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_STREAM, "[Sensor requires polling]\n");
7328c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, poll1, sizeof poll1);
7338c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, poll2, sizeof poll2);
7348c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, noise03, sizeof noise03);
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic int sd_start(struct gspca_dev *gspca_dev)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
7408c2ecf20Sopenharmony_ci	const struct additional_sensor_data *sensor;
7418c2ecf20Sopenharmony_ci	int i, mode;
7428c2ecf20Sopenharmony_ci	u8 t2[] = { 0x07, 0x00, 0x0d, 0x60, 0x0e, 0x80 };
7438c2ecf20Sopenharmony_ci	static const u8 t3[] =
7448c2ecf20Sopenharmony_ci		{ 0x07, 0x00, 0x88, 0x02, 0x06, 0x00, 0xe7, 0x01 };
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
7478c2ecf20Sopenharmony_ci	switch (mode) {
7488c2ecf20Sopenharmony_ci	case 0:		/* 640x480 (0x00) */
7498c2ecf20Sopenharmony_ci		break;
7508c2ecf20Sopenharmony_ci	case 1:		/* 352x288 */
7518c2ecf20Sopenharmony_ci		t2[1] = 0x40;
7528c2ecf20Sopenharmony_ci		break;
7538c2ecf20Sopenharmony_ci	case 2:		/* 320x240 */
7548c2ecf20Sopenharmony_ci		t2[1] = 0x10;
7558c2ecf20Sopenharmony_ci		break;
7568c2ecf20Sopenharmony_ci	case 3:		/* 176x144 */
7578c2ecf20Sopenharmony_ci		t2[1] = 0x50;
7588c2ecf20Sopenharmony_ci		break;
7598c2ecf20Sopenharmony_ci	default:
7608c2ecf20Sopenharmony_ci/*	case 4:		 * 160x120 */
7618c2ecf20Sopenharmony_ci		t2[1] = 0x20;
7628c2ecf20Sopenharmony_ci		break;
7638c2ecf20Sopenharmony_ci	}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	switch (sd->sensor) {
7668c2ecf20Sopenharmony_ci	case SENSOR_OM6802:
7678c2ecf20Sopenharmony_ci		om6802_sensor_init(gspca_dev);
7688c2ecf20Sopenharmony_ci		break;
7698c2ecf20Sopenharmony_ci	case SENSOR_TAS5130A:
7708c2ecf20Sopenharmony_ci		i = 0;
7718c2ecf20Sopenharmony_ci		for (;;) {
7728c2ecf20Sopenharmony_ci			reg_w_buf(gspca_dev, tas5130a_sensor_init[i],
7738c2ecf20Sopenharmony_ci					 sizeof tas5130a_sensor_init[0]);
7748c2ecf20Sopenharmony_ci			if (i >= ARRAY_SIZE(tas5130a_sensor_init) - 1)
7758c2ecf20Sopenharmony_ci				break;
7768c2ecf20Sopenharmony_ci			i++;
7778c2ecf20Sopenharmony_ci		}
7788c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x3c80);
7798c2ecf20Sopenharmony_ci		/* just in case and to keep sync with logs (for mine) */
7808c2ecf20Sopenharmony_ci		reg_w_buf(gspca_dev, tas5130a_sensor_init[i],
7818c2ecf20Sopenharmony_ci				 sizeof tas5130a_sensor_init[0]);
7828c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x3c80);
7838c2ecf20Sopenharmony_ci		break;
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci	sensor = &sensor_data[sd->sensor];
7868c2ecf20Sopenharmony_ci	setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
7878c2ecf20Sopenharmony_ci	reg_r(gspca_dev, 0x0012);
7888c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, t2, sizeof t2);
7898c2ecf20Sopenharmony_ci	reg_w_ixbuf(gspca_dev, 0xb3, t3, sizeof t3);
7908c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x0013);
7918c2ecf20Sopenharmony_ci	msleep(15);
7928c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->stream, sizeof sensor->stream);
7938c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor->stream, sizeof sensor->stream);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_OM6802)
7968c2ecf20Sopenharmony_ci		poll_sensor(gspca_dev);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic void sd_stopN(struct gspca_dev *gspca_dev)
8028c2ecf20Sopenharmony_ci{
8038c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor_data[sd->sensor].stream,
8068c2ecf20Sopenharmony_ci			sizeof sensor_data[sd->sensor].stream);
8078c2ecf20Sopenharmony_ci	reg_w_buf(gspca_dev, sensor_data[sd->sensor].stream,
8088c2ecf20Sopenharmony_ci			sizeof sensor_data[sd->sensor].stream);
8098c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_OM6802) {
8108c2ecf20Sopenharmony_ci		msleep(20);
8118c2ecf20Sopenharmony_ci		reg_w(gspca_dev, 0x0309);
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
8148c2ecf20Sopenharmony_ci	/* If the last button state is pressed, release it now! */
8158c2ecf20Sopenharmony_ci	if (sd->button_pressed) {
8168c2ecf20Sopenharmony_ci		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
8178c2ecf20Sopenharmony_ci		input_sync(gspca_dev->input_dev);
8188c2ecf20Sopenharmony_ci		sd->button_pressed = 0;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci#endif
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic void sd_pkt_scan(struct gspca_dev *gspca_dev,
8248c2ecf20Sopenharmony_ci			u8 *data,			/* isoc packet */
8258c2ecf20Sopenharmony_ci			int len)			/* iso packet length */
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
8288c2ecf20Sopenharmony_ci	int pkt_type;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (data[0] == 0x5a) {
8318c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
8328c2ecf20Sopenharmony_ci		if (len > 20) {
8338c2ecf20Sopenharmony_ci			u8 state = (data[20] & 0x80) ? 1 : 0;
8348c2ecf20Sopenharmony_ci			if (sd->button_pressed != state) {
8358c2ecf20Sopenharmony_ci				input_report_key(gspca_dev->input_dev,
8368c2ecf20Sopenharmony_ci						 KEY_CAMERA, state);
8378c2ecf20Sopenharmony_ci				input_sync(gspca_dev->input_dev);
8388c2ecf20Sopenharmony_ci				sd->button_pressed = state;
8398c2ecf20Sopenharmony_ci			}
8408c2ecf20Sopenharmony_ci		}
8418c2ecf20Sopenharmony_ci#endif
8428c2ecf20Sopenharmony_ci		/* Control Packet, after this came the header again,
8438c2ecf20Sopenharmony_ci		 * but extra bytes came in the packet before this,
8448c2ecf20Sopenharmony_ci		 * sometimes an EOF arrives, sometimes not... */
8458c2ecf20Sopenharmony_ci		return;
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci	data += 2;
8488c2ecf20Sopenharmony_ci	len -= 2;
8498c2ecf20Sopenharmony_ci	if (data[0] == 0xff && data[1] == 0xd8)
8508c2ecf20Sopenharmony_ci		pkt_type = FIRST_PACKET;
8518c2ecf20Sopenharmony_ci	else if (data[len - 2] == 0xff && data[len - 1] == 0xd9)
8528c2ecf20Sopenharmony_ci		pkt_type = LAST_PACKET;
8538c2ecf20Sopenharmony_ci	else
8548c2ecf20Sopenharmony_ci		pkt_type = INTER_PACKET;
8558c2ecf20Sopenharmony_ci	gspca_frame_add(gspca_dev, pkt_type, data, len);
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic int sd_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev =
8618c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
8628c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *)gspca_dev;
8638c2ecf20Sopenharmony_ci	s32 red_gain, blue_gain, green_gain;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	gspca_dev->usb_err = 0;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	switch (ctrl->id) {
8688c2ecf20Sopenharmony_ci	case V4L2_CID_AUTO_WHITE_BALANCE:
8698c2ecf20Sopenharmony_ci		red_gain = reg_r(gspca_dev, 0x0087);
8708c2ecf20Sopenharmony_ci		if (red_gain > 0x40)
8718c2ecf20Sopenharmony_ci			red_gain = 0x40;
8728c2ecf20Sopenharmony_ci		else if (red_gain < 0x10)
8738c2ecf20Sopenharmony_ci			red_gain = 0x10;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci		blue_gain = reg_r(gspca_dev, 0x0088);
8768c2ecf20Sopenharmony_ci		if (blue_gain > 0x40)
8778c2ecf20Sopenharmony_ci			blue_gain = 0x40;
8788c2ecf20Sopenharmony_ci		else if (blue_gain < 0x10)
8798c2ecf20Sopenharmony_ci			blue_gain = 0x10;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci		green_gain = reg_r(gspca_dev, 0x0089);
8828c2ecf20Sopenharmony_ci		if (green_gain > 0x40)
8838c2ecf20Sopenharmony_ci			green_gain = 0x40;
8848c2ecf20Sopenharmony_ci		else if (green_gain < 0x10)
8858c2ecf20Sopenharmony_ci			green_gain = 0x10;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci		sd->gain->val = green_gain;
8888c2ecf20Sopenharmony_ci		sd->red_balance->val = red_gain - green_gain;
8898c2ecf20Sopenharmony_ci		sd->blue_balance->val = blue_gain - green_gain;
8908c2ecf20Sopenharmony_ci		break;
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci	return 0;
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistatic int sd_s_ctrl(struct v4l2_ctrl *ctrl)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev =
8988c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	gspca_dev->usb_err = 0;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	if (!gspca_dev->streaming)
9038c2ecf20Sopenharmony_ci		return 0;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	switch (ctrl->id) {
9068c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
9078c2ecf20Sopenharmony_ci		setbrightness(gspca_dev, ctrl->val);
9088c2ecf20Sopenharmony_ci		break;
9098c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
9108c2ecf20Sopenharmony_ci		setcontrast(gspca_dev, ctrl->val);
9118c2ecf20Sopenharmony_ci		break;
9128c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
9138c2ecf20Sopenharmony_ci		setcolors(gspca_dev, ctrl->val);
9148c2ecf20Sopenharmony_ci		break;
9158c2ecf20Sopenharmony_ci	case V4L2_CID_GAMMA:
9168c2ecf20Sopenharmony_ci		setgamma(gspca_dev, ctrl->val);
9178c2ecf20Sopenharmony_ci		break;
9188c2ecf20Sopenharmony_ci	case V4L2_CID_HFLIP:
9198c2ecf20Sopenharmony_ci		setmirror(gspca_dev, ctrl->val);
9208c2ecf20Sopenharmony_ci		break;
9218c2ecf20Sopenharmony_ci	case V4L2_CID_SHARPNESS:
9228c2ecf20Sopenharmony_ci		setsharpness(gspca_dev, ctrl->val);
9238c2ecf20Sopenharmony_ci		break;
9248c2ecf20Sopenharmony_ci	case V4L2_CID_POWER_LINE_FREQUENCY:
9258c2ecf20Sopenharmony_ci		setfreq(gspca_dev, ctrl->val);
9268c2ecf20Sopenharmony_ci		break;
9278c2ecf20Sopenharmony_ci	case V4L2_CID_BACKLIGHT_COMPENSATION:
9288c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val ? 0xf48e : 0xb48e);
9298c2ecf20Sopenharmony_ci		break;
9308c2ecf20Sopenharmony_ci	case V4L2_CID_AUTO_WHITE_BALANCE:
9318c2ecf20Sopenharmony_ci		setawb_n_RGB(gspca_dev);
9328c2ecf20Sopenharmony_ci		break;
9338c2ecf20Sopenharmony_ci	case V4L2_CID_COLORFX:
9348c2ecf20Sopenharmony_ci		seteffect(gspca_dev, ctrl->val);
9358c2ecf20Sopenharmony_ci		break;
9368c2ecf20Sopenharmony_ci	}
9378c2ecf20Sopenharmony_ci	return gspca_dev->usb_err;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops sd_ctrl_ops = {
9418c2ecf20Sopenharmony_ci	.g_volatile_ctrl = sd_g_volatile_ctrl,
9428c2ecf20Sopenharmony_ci	.s_ctrl = sd_s_ctrl,
9438c2ecf20Sopenharmony_ci};
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic int sd_init_controls(struct gspca_dev *gspca_dev)
9468c2ecf20Sopenharmony_ci{
9478c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *)gspca_dev;
9488c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	gspca_dev->vdev.ctrl_handler = hdl;
9518c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 12);
9528c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9538c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 14, 1, 8);
9548c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9558c2ecf20Sopenharmony_ci			V4L2_CID_CONTRAST, 0, 0x0d, 1, 7);
9568c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9578c2ecf20Sopenharmony_ci			V4L2_CID_SATURATION, 0, 0xf, 1, 5);
9588c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9598c2ecf20Sopenharmony_ci			V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 10);
9608c2ecf20Sopenharmony_ci	/* Activate lowlight, some apps don't bring up the
9618c2ecf20Sopenharmony_ci	   backlight_compensation control) */
9628c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9638c2ecf20Sopenharmony_ci			V4L2_CID_BACKLIGHT_COMPENSATION, 0, 1, 1, 1);
9648c2ecf20Sopenharmony_ci	if (sd->sensor == SENSOR_TAS5130A)
9658c2ecf20Sopenharmony_ci		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9668c2ecf20Sopenharmony_ci				V4L2_CID_HFLIP, 0, 1, 1, 0);
9678c2ecf20Sopenharmony_ci	sd->awb = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9688c2ecf20Sopenharmony_ci			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
9698c2ecf20Sopenharmony_ci	sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9708c2ecf20Sopenharmony_ci			V4L2_CID_GAIN, 0x10, 0x40, 1, 0x20);
9718c2ecf20Sopenharmony_ci	sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9728c2ecf20Sopenharmony_ci			V4L2_CID_BLUE_BALANCE, -0x30, 0x30, 1, 0);
9738c2ecf20Sopenharmony_ci	sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9748c2ecf20Sopenharmony_ci			V4L2_CID_RED_BALANCE, -0x30, 0x30, 1, 0);
9758c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
9768c2ecf20Sopenharmony_ci			V4L2_CID_SHARPNESS, 0, 15, 1, 6);
9778c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
9788c2ecf20Sopenharmony_ci			V4L2_CID_COLORFX, V4L2_COLORFX_SKETCH,
9798c2ecf20Sopenharmony_ci			~((1 << V4L2_COLORFX_NONE) |
9808c2ecf20Sopenharmony_ci			  (1 << V4L2_COLORFX_BW) |
9818c2ecf20Sopenharmony_ci			  (1 << V4L2_COLORFX_SEPIA) |
9828c2ecf20Sopenharmony_ci			  (1 << V4L2_COLORFX_SKETCH) |
9838c2ecf20Sopenharmony_ci			  (1 << V4L2_COLORFX_NEGATIVE)),
9848c2ecf20Sopenharmony_ci			V4L2_COLORFX_NONE);
9858c2ecf20Sopenharmony_ci	sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
9868c2ecf20Sopenharmony_ci			V4L2_CID_POWER_LINE_FREQUENCY,
9878c2ecf20Sopenharmony_ci			V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
9888c2ecf20Sopenharmony_ci			V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (hdl->error) {
9918c2ecf20Sopenharmony_ci		pr_err("Could not initialize controls\n");
9928c2ecf20Sopenharmony_ci		return hdl->error;
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(4, &sd->awb, 0, true);
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	return 0;
9988c2ecf20Sopenharmony_ci}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci/* sub-driver description */
10018c2ecf20Sopenharmony_cistatic const struct sd_desc sd_desc = {
10028c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
10038c2ecf20Sopenharmony_ci	.config = sd_config,
10048c2ecf20Sopenharmony_ci	.init = sd_init,
10058c2ecf20Sopenharmony_ci	.init_controls = sd_init_controls,
10068c2ecf20Sopenharmony_ci	.start = sd_start,
10078c2ecf20Sopenharmony_ci	.stopN = sd_stopN,
10088c2ecf20Sopenharmony_ci	.pkt_scan = sd_pkt_scan,
10098c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
10108c2ecf20Sopenharmony_ci	.other_input = 1,
10118c2ecf20Sopenharmony_ci#endif
10128c2ecf20Sopenharmony_ci};
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci/* -- module initialisation -- */
10158c2ecf20Sopenharmony_cistatic const struct usb_device_id device_table[] = {
10168c2ecf20Sopenharmony_ci	{USB_DEVICE(0x17a1, 0x0128)},
10178c2ecf20Sopenharmony_ci	{}
10188c2ecf20Sopenharmony_ci};
10198c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, device_table);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci/* -- device connect -- */
10228c2ecf20Sopenharmony_cistatic int sd_probe(struct usb_interface *intf,
10238c2ecf20Sopenharmony_ci		    const struct usb_device_id *id)
10248c2ecf20Sopenharmony_ci{
10258c2ecf20Sopenharmony_ci	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
10268c2ecf20Sopenharmony_ci			       THIS_MODULE);
10278c2ecf20Sopenharmony_ci}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_cistatic struct usb_driver sd_driver = {
10308c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
10318c2ecf20Sopenharmony_ci	.id_table = device_table,
10328c2ecf20Sopenharmony_ci	.probe = sd_probe,
10338c2ecf20Sopenharmony_ci	.disconnect = gspca_disconnect,
10348c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
10358c2ecf20Sopenharmony_ci	.suspend = gspca_suspend,
10368c2ecf20Sopenharmony_ci	.resume = gspca_resume,
10378c2ecf20Sopenharmony_ci	.reset_resume = gspca_resume,
10388c2ecf20Sopenharmony_ci#endif
10398c2ecf20Sopenharmony_ci};
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cimodule_usb_driver(sd_driver);
1042