xref: /OK3568_Linux_fs/kernel/drivers/media/usb/gspca/sq930x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SQ930x subdriver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
6*4882a593Smuzhiyun  * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
7*4882a593Smuzhiyun  * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define MODULE_NAME "sq930x"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "gspca.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
17*4882a593Smuzhiyun 		"Gerard Klaver <gerard at gkall dot hobby dot nl\n"
18*4882a593Smuzhiyun 		"Sam Revitch <samr7@cs.washington.edu>");
19*4882a593Smuzhiyun MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
20*4882a593Smuzhiyun MODULE_LICENSE("GPL");
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* Structure to hold all of our device specific stuff */
23*4882a593Smuzhiyun struct sd {
24*4882a593Smuzhiyun 	struct gspca_dev gspca_dev;	/* !! must be the first item */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	struct { /* exposure/gain control cluster */
27*4882a593Smuzhiyun 		struct v4l2_ctrl *exposure;
28*4882a593Smuzhiyun 		struct v4l2_ctrl *gain;
29*4882a593Smuzhiyun 	};
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	u8 do_ctrl;
32*4882a593Smuzhiyun 	u8 gpio[2];
33*4882a593Smuzhiyun 	u8 sensor;
34*4882a593Smuzhiyun 	u8 type;
35*4882a593Smuzhiyun #define Generic 0
36*4882a593Smuzhiyun #define Creative_live_motion 1
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun enum sensors {
39*4882a593Smuzhiyun 	SENSOR_ICX098BQ,
40*4882a593Smuzhiyun 	SENSOR_LZ24BP,
41*4882a593Smuzhiyun 	SENSOR_MI0360,
42*4882a593Smuzhiyun 	SENSOR_MT9V111,		/* = MI360SOC */
43*4882a593Smuzhiyun 	SENSOR_OV7660,
44*4882a593Smuzhiyun 	SENSOR_OV9630,
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static struct v4l2_pix_format vga_mode[] = {
48*4882a593Smuzhiyun 	{320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
49*4882a593Smuzhiyun 		.bytesperline = 320,
50*4882a593Smuzhiyun 		.sizeimage = 320 * 240,
51*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
52*4882a593Smuzhiyun 		.priv = 0},
53*4882a593Smuzhiyun 	{640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
54*4882a593Smuzhiyun 		.bytesperline = 640,
55*4882a593Smuzhiyun 		.sizeimage = 640 * 480,
56*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
57*4882a593Smuzhiyun 		.priv = 1},
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /* sq930x registers */
61*4882a593Smuzhiyun #define SQ930_CTRL_UCBUS_IO	0x0001
62*4882a593Smuzhiyun #define SQ930_CTRL_I2C_IO	0x0002
63*4882a593Smuzhiyun #define SQ930_CTRL_GPIO		0x0005
64*4882a593Smuzhiyun #define SQ930_CTRL_CAP_START	0x0010
65*4882a593Smuzhiyun #define SQ930_CTRL_CAP_STOP	0x0011
66*4882a593Smuzhiyun #define SQ930_CTRL_SET_EXPOSURE 0x001d
67*4882a593Smuzhiyun #define SQ930_CTRL_RESET	0x001e
68*4882a593Smuzhiyun #define SQ930_CTRL_GET_DEV_INFO 0x001f
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* gpio 1 (8..15) */
71*4882a593Smuzhiyun #define SQ930_GPIO_DFL_I2C_SDA	0x0001
72*4882a593Smuzhiyun #define SQ930_GPIO_DFL_I2C_SCL	0x0002
73*4882a593Smuzhiyun #define SQ930_GPIO_RSTBAR	0x0004
74*4882a593Smuzhiyun #define SQ930_GPIO_EXTRA1	0x0040
75*4882a593Smuzhiyun #define SQ930_GPIO_EXTRA2	0x0080
76*4882a593Smuzhiyun /* gpio 3 (24..31) */
77*4882a593Smuzhiyun #define SQ930_GPIO_POWER	0x0200
78*4882a593Smuzhiyun #define SQ930_GPIO_DFL_LED	0x1000
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun struct ucbus_write_cmd {
81*4882a593Smuzhiyun 	u16	bw_addr;
82*4882a593Smuzhiyun 	u8	bw_data;
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun struct i2c_write_cmd {
85*4882a593Smuzhiyun 	u8	reg;
86*4882a593Smuzhiyun 	u16	val;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static const struct ucbus_write_cmd icx098bq_start_0[] = {
90*4882a593Smuzhiyun 	{0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
91*4882a593Smuzhiyun 	{0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
92*4882a593Smuzhiyun 	{0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
93*4882a593Smuzhiyun 	{0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
94*4882a593Smuzhiyun 	{0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
95*4882a593Smuzhiyun 	{0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
96*4882a593Smuzhiyun 	{0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
97*4882a593Smuzhiyun 	{0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
98*4882a593Smuzhiyun 	{0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
99*4882a593Smuzhiyun 	{0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
100*4882a593Smuzhiyun 	{0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
101*4882a593Smuzhiyun 	{0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
102*4882a593Smuzhiyun 	{0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
103*4882a593Smuzhiyun 	{0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
104*4882a593Smuzhiyun 	{0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
105*4882a593Smuzhiyun 	{0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
106*4882a593Smuzhiyun 	{0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
107*4882a593Smuzhiyun 	{0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
108*4882a593Smuzhiyun 	{0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
109*4882a593Smuzhiyun 	{0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
110*4882a593Smuzhiyun 	{0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
111*4882a593Smuzhiyun 	{0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
112*4882a593Smuzhiyun 	{0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
113*4882a593Smuzhiyun 	{0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
114*4882a593Smuzhiyun 	{0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
115*4882a593Smuzhiyun 	{0xf800, 0x03}
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun static const struct ucbus_write_cmd icx098bq_start_1[] = {
118*4882a593Smuzhiyun 	{0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
119*4882a593Smuzhiyun 	{0xf5f4, 0xc0},
120*4882a593Smuzhiyun 	{0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
121*4882a593Smuzhiyun 	{0xf5f4, 0xc0},
122*4882a593Smuzhiyun 	{0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
123*4882a593Smuzhiyun 	{0xf5f9, 0x00}
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static const struct ucbus_write_cmd icx098bq_start_2[] = {
127*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
128*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
129*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
130*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
131*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
132*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
133*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
134*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03}
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun static const struct ucbus_write_cmd lz24bp_start_0[] = {
138*4882a593Smuzhiyun 	{0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
139*4882a593Smuzhiyun 	{0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
140*4882a593Smuzhiyun 	{0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
141*4882a593Smuzhiyun 	{0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
142*4882a593Smuzhiyun 	{0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
143*4882a593Smuzhiyun 	{0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
144*4882a593Smuzhiyun 	{0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
145*4882a593Smuzhiyun 	{0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
146*4882a593Smuzhiyun 	{0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
147*4882a593Smuzhiyun 	{0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
148*4882a593Smuzhiyun 	{0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
149*4882a593Smuzhiyun 	{0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
150*4882a593Smuzhiyun 	{0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
151*4882a593Smuzhiyun 	{0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
152*4882a593Smuzhiyun 	{0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
153*4882a593Smuzhiyun 	{0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
154*4882a593Smuzhiyun 	{0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
155*4882a593Smuzhiyun 	{0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
156*4882a593Smuzhiyun 	{0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
157*4882a593Smuzhiyun 	{0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
158*4882a593Smuzhiyun 	{0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
159*4882a593Smuzhiyun 	{0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
160*4882a593Smuzhiyun 	{0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
161*4882a593Smuzhiyun 	{0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
162*4882a593Smuzhiyun 	{0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
163*4882a593Smuzhiyun 	{0xf800, 0x03}
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
166*4882a593Smuzhiyun 	{0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
167*4882a593Smuzhiyun 	{0xf5f4, 0xb3},
168*4882a593Smuzhiyun 	{0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
169*4882a593Smuzhiyun 	{0xf5f4, 0xb3},
170*4882a593Smuzhiyun 	{0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
171*4882a593Smuzhiyun 	{0xf5f9, 0x00}
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
175*4882a593Smuzhiyun 	{0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
176*4882a593Smuzhiyun 	{0xf5f4, 0xc0},
177*4882a593Smuzhiyun 	{0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
178*4882a593Smuzhiyun 	{0xf5f4, 0xc0},
179*4882a593Smuzhiyun 	{0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
180*4882a593Smuzhiyun 	{0xf5f9, 0x00}
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun static const struct ucbus_write_cmd lz24bp_start_2[] = {
184*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
185*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
186*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
187*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
188*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
189*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03},
190*4882a593Smuzhiyun 	{0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
191*4882a593Smuzhiyun 	{0xf807, 0x7f}, {0xf800, 0x03}
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun static const struct ucbus_write_cmd mi0360_start_0[] = {
195*4882a593Smuzhiyun 	{0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
196*4882a593Smuzhiyun 	{0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
197*4882a593Smuzhiyun };
198*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_init_23[] = {
199*4882a593Smuzhiyun 	{0x30, 0x0040},		/* reserved - def 0x0005 */
200*4882a593Smuzhiyun 	{0x31, 0x0000},		/* reserved - def 0x002a */
201*4882a593Smuzhiyun 	{0x34, 0x0100},		/* reserved - def 0x0100 */
202*4882a593Smuzhiyun 	{0x3d, 0x068f},		/* reserved - def 0x068f */
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_init_24[] = {
205*4882a593Smuzhiyun 	{0x03, 0x01e5},		/* window height */
206*4882a593Smuzhiyun 	{0x04, 0x0285},		/* window width */
207*4882a593Smuzhiyun };
208*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_init_25[] = {
209*4882a593Smuzhiyun 	{0x35, 0x0020},		/* global gain */
210*4882a593Smuzhiyun 	{0x2b, 0x0020},		/* green1 gain */
211*4882a593Smuzhiyun 	{0x2c, 0x002a},		/* blue gain */
212*4882a593Smuzhiyun 	{0x2d, 0x0028},		/* red gain */
213*4882a593Smuzhiyun 	{0x2e, 0x0020},		/* green2 gain */
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun static const struct ucbus_write_cmd mi0360_start_1[] = {
216*4882a593Smuzhiyun 	{0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
217*4882a593Smuzhiyun 	{0xf5f4, 0xa6},
218*4882a593Smuzhiyun 	{0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
219*4882a593Smuzhiyun 	{0xf5f4, 0xa6},
220*4882a593Smuzhiyun 	{0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
221*4882a593Smuzhiyun 	{0xf5f9, 0x00}
222*4882a593Smuzhiyun };
223*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_start_2[] = {
224*4882a593Smuzhiyun 	{0x62, 0x041d},		/* reserved - def 0x0418 */
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_start_3[] = {
227*4882a593Smuzhiyun 	{0x05, 0x007b},		/* horiz blanking */
228*4882a593Smuzhiyun };
229*4882a593Smuzhiyun static const struct i2c_write_cmd mi0360_start_4[] = {
230*4882a593Smuzhiyun 	{0x05, 0x03f5},		/* horiz blanking */
231*4882a593Smuzhiyun };
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun static const struct i2c_write_cmd mt9v111_init_0[] = {
234*4882a593Smuzhiyun 	{0x01, 0x0001},		/* select IFP/SOC registers */
235*4882a593Smuzhiyun 	{0x06, 0x300c},		/* operating mode control */
236*4882a593Smuzhiyun 	{0x08, 0xcc00},		/* output format control (RGB) */
237*4882a593Smuzhiyun 	{0x01, 0x0004},		/* select sensor core registers */
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun static const struct i2c_write_cmd mt9v111_init_1[] = {
240*4882a593Smuzhiyun 	{0x03, 0x01e5},		/* window height */
241*4882a593Smuzhiyun 	{0x04, 0x0285},		/* window width */
242*4882a593Smuzhiyun };
243*4882a593Smuzhiyun static const struct i2c_write_cmd mt9v111_init_2[] = {
244*4882a593Smuzhiyun 	{0x30, 0x7800},
245*4882a593Smuzhiyun 	{0x31, 0x0000},
246*4882a593Smuzhiyun 	{0x07, 0x3002},		/* output control */
247*4882a593Smuzhiyun 	{0x35, 0x0020},		/* global gain */
248*4882a593Smuzhiyun 	{0x2b, 0x0020},		/* green1 gain */
249*4882a593Smuzhiyun 	{0x2c, 0x0020},		/* blue gain */
250*4882a593Smuzhiyun 	{0x2d, 0x0020},		/* red gain */
251*4882a593Smuzhiyun 	{0x2e, 0x0020},		/* green2 gain */
252*4882a593Smuzhiyun };
253*4882a593Smuzhiyun static const struct ucbus_write_cmd mt9v111_start_1[] = {
254*4882a593Smuzhiyun 	{0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
255*4882a593Smuzhiyun 	{0xf5f4, 0xaa},
256*4882a593Smuzhiyun 	{0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
257*4882a593Smuzhiyun 	{0xf5f4, 0xaa},
258*4882a593Smuzhiyun 	{0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
259*4882a593Smuzhiyun 	{0xf5f9, 0x0a}
260*4882a593Smuzhiyun };
261*4882a593Smuzhiyun static const struct i2c_write_cmd mt9v111_init_3[] = {
262*4882a593Smuzhiyun 	{0x62, 0x0405},
263*4882a593Smuzhiyun };
264*4882a593Smuzhiyun static const struct i2c_write_cmd mt9v111_init_4[] = {
265*4882a593Smuzhiyun /*	{0x05, 0x00ce}, */
266*4882a593Smuzhiyun 	{0x05, 0x005d},		/* horizontal blanking */
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun static const struct ucbus_write_cmd ov7660_start_0[] = {
270*4882a593Smuzhiyun 	{0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
271*4882a593Smuzhiyun 	{0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun static const struct ucbus_write_cmd ov9630_start_0[] = {
275*4882a593Smuzhiyun 	{0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
276*4882a593Smuzhiyun 	{0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
277*4882a593Smuzhiyun };
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun /* start parameters indexed by [sensor][mode] */
280*4882a593Smuzhiyun static const struct cap_s {
281*4882a593Smuzhiyun 	u8	cc_sizeid;
282*4882a593Smuzhiyun 	u8	cc_bytes[32];
283*4882a593Smuzhiyun } capconfig[4][2] = {
284*4882a593Smuzhiyun 	[SENSOR_ICX098BQ] = {
285*4882a593Smuzhiyun 		{2,				/* Bayer 320x240 */
286*4882a593Smuzhiyun 		  {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
287*4882a593Smuzhiyun 		   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
288*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
289*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
290*4882a593Smuzhiyun 		{4,				/* Bayer 640x480 */
291*4882a593Smuzhiyun 		  {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
292*4882a593Smuzhiyun 		   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
293*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
295*4882a593Smuzhiyun 	},
296*4882a593Smuzhiyun 	[SENSOR_LZ24BP] = {
297*4882a593Smuzhiyun 		{2,				/* Bayer 320x240 */
298*4882a593Smuzhiyun 		  {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
299*4882a593Smuzhiyun 		   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
300*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
302*4882a593Smuzhiyun 		{4,				/* Bayer 640x480 */
303*4882a593Smuzhiyun 		  {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
304*4882a593Smuzhiyun 		   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
305*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
307*4882a593Smuzhiyun 	},
308*4882a593Smuzhiyun 	[SENSOR_MI0360] = {
309*4882a593Smuzhiyun 		{2,				/* Bayer 320x240 */
310*4882a593Smuzhiyun 		  {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
311*4882a593Smuzhiyun 		   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
312*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
314*4882a593Smuzhiyun 		{4,				/* Bayer 640x480 */
315*4882a593Smuzhiyun 		  {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
316*4882a593Smuzhiyun 		   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
317*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
319*4882a593Smuzhiyun 	},
320*4882a593Smuzhiyun 	[SENSOR_MT9V111] = {
321*4882a593Smuzhiyun 		{2,				/* Bayer 320x240 */
322*4882a593Smuzhiyun 		  {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
323*4882a593Smuzhiyun 		   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
324*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
325*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
326*4882a593Smuzhiyun 		{4,				/* Bayer 640x480 */
327*4882a593Smuzhiyun 		  {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
328*4882a593Smuzhiyun 		   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
329*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330*4882a593Smuzhiyun 		   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
331*4882a593Smuzhiyun 	},
332*4882a593Smuzhiyun };
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun struct sensor_s {
335*4882a593Smuzhiyun 	const char *name;
336*4882a593Smuzhiyun 	u8 i2c_addr;
337*4882a593Smuzhiyun 	u8 i2c_dum;
338*4882a593Smuzhiyun 	u8 gpio[5];
339*4882a593Smuzhiyun 	u8 cmd_len;
340*4882a593Smuzhiyun 	const struct ucbus_write_cmd *cmd;
341*4882a593Smuzhiyun };
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun static const struct sensor_s sensor_tb[] = {
344*4882a593Smuzhiyun 	[SENSOR_ICX098BQ] = {
345*4882a593Smuzhiyun 		"icx098bp",
346*4882a593Smuzhiyun 		0x00, 0x00,
347*4882a593Smuzhiyun 		{0,
348*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
349*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
350*4882a593Smuzhiyun 		 0,
351*4882a593Smuzhiyun 		 SQ930_GPIO_RSTBAR
352*4882a593Smuzhiyun 		},
353*4882a593Smuzhiyun 		8, icx098bq_start_0
354*4882a593Smuzhiyun 	    },
355*4882a593Smuzhiyun 	[SENSOR_LZ24BP] = {
356*4882a593Smuzhiyun 		"lz24bp",
357*4882a593Smuzhiyun 		0x00, 0x00,
358*4882a593Smuzhiyun 		{0,
359*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
360*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
361*4882a593Smuzhiyun 		 0,
362*4882a593Smuzhiyun 		 SQ930_GPIO_RSTBAR
363*4882a593Smuzhiyun 		},
364*4882a593Smuzhiyun 		8, lz24bp_start_0
365*4882a593Smuzhiyun 	    },
366*4882a593Smuzhiyun 	[SENSOR_MI0360] = {
367*4882a593Smuzhiyun 		"mi0360",
368*4882a593Smuzhiyun 		0x5d, 0x80,
369*4882a593Smuzhiyun 		{SQ930_GPIO_RSTBAR,
370*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
371*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
372*4882a593Smuzhiyun 		 0,
373*4882a593Smuzhiyun 		 0
374*4882a593Smuzhiyun 		},
375*4882a593Smuzhiyun 		7, mi0360_start_0
376*4882a593Smuzhiyun 	    },
377*4882a593Smuzhiyun 	[SENSOR_MT9V111] = {
378*4882a593Smuzhiyun 		"mt9v111",
379*4882a593Smuzhiyun 		0x5c, 0x7f,
380*4882a593Smuzhiyun 		{SQ930_GPIO_RSTBAR,
381*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
382*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
383*4882a593Smuzhiyun 		 0,
384*4882a593Smuzhiyun 		 0
385*4882a593Smuzhiyun 		},
386*4882a593Smuzhiyun 		7, mi0360_start_0
387*4882a593Smuzhiyun 	    },
388*4882a593Smuzhiyun 	[SENSOR_OV7660] = {
389*4882a593Smuzhiyun 		"ov7660",
390*4882a593Smuzhiyun 		0x21, 0x00,
391*4882a593Smuzhiyun 		{0,
392*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
393*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
394*4882a593Smuzhiyun 		 0,
395*4882a593Smuzhiyun 		 SQ930_GPIO_RSTBAR
396*4882a593Smuzhiyun 		},
397*4882a593Smuzhiyun 		7, ov7660_start_0
398*4882a593Smuzhiyun 	    },
399*4882a593Smuzhiyun 	[SENSOR_OV9630] = {
400*4882a593Smuzhiyun 		"ov9630",
401*4882a593Smuzhiyun 		0x30, 0x00,
402*4882a593Smuzhiyun 		{0,
403*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
404*4882a593Smuzhiyun 		 SQ930_GPIO_DFL_I2C_SDA,
405*4882a593Smuzhiyun 		 0,
406*4882a593Smuzhiyun 		 SQ930_GPIO_RSTBAR
407*4882a593Smuzhiyun 		},
408*4882a593Smuzhiyun 		7, ov9630_start_0
409*4882a593Smuzhiyun 	    },
410*4882a593Smuzhiyun };
411*4882a593Smuzhiyun 
reg_r(struct gspca_dev * gspca_dev,u16 value,int len)412*4882a593Smuzhiyun static void reg_r(struct gspca_dev *gspca_dev,
413*4882a593Smuzhiyun 		u16 value, int len)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	int ret;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
418*4882a593Smuzhiyun 		return;
419*4882a593Smuzhiyun 	ret = usb_control_msg(gspca_dev->dev,
420*4882a593Smuzhiyun 			usb_rcvctrlpipe(gspca_dev->dev, 0),
421*4882a593Smuzhiyun 			0x0c,
422*4882a593Smuzhiyun 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
423*4882a593Smuzhiyun 			value, 0, gspca_dev->usb_buf, len,
424*4882a593Smuzhiyun 			500);
425*4882a593Smuzhiyun 	if (ret < 0) {
426*4882a593Smuzhiyun 		pr_err("reg_r %04x failed %d\n", value, ret);
427*4882a593Smuzhiyun 		gspca_dev->usb_err = ret;
428*4882a593Smuzhiyun 		/*
429*4882a593Smuzhiyun 		 * Make sure the buffer is zeroed to avoid uninitialized
430*4882a593Smuzhiyun 		 * values.
431*4882a593Smuzhiyun 		 */
432*4882a593Smuzhiyun 		memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
reg_w(struct gspca_dev * gspca_dev,u16 value,u16 index)436*4882a593Smuzhiyun static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	int ret;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
441*4882a593Smuzhiyun 		return;
442*4882a593Smuzhiyun 	gspca_dbg(gspca_dev, D_USBO, "reg_w v: %04x i: %04x\n", value, index);
443*4882a593Smuzhiyun 	ret = usb_control_msg(gspca_dev->dev,
444*4882a593Smuzhiyun 			usb_sndctrlpipe(gspca_dev->dev, 0),
445*4882a593Smuzhiyun 			0x0c,			/* request */
446*4882a593Smuzhiyun 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
447*4882a593Smuzhiyun 			value, index, NULL, 0,
448*4882a593Smuzhiyun 			500);
449*4882a593Smuzhiyun 	msleep(30);
450*4882a593Smuzhiyun 	if (ret < 0) {
451*4882a593Smuzhiyun 		pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
452*4882a593Smuzhiyun 		gspca_dev->usb_err = ret;
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
reg_wb(struct gspca_dev * gspca_dev,u16 value,u16 index,const u8 * data,int len)456*4882a593Smuzhiyun static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
457*4882a593Smuzhiyun 		const u8 *data, int len)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	int ret;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
462*4882a593Smuzhiyun 		return;
463*4882a593Smuzhiyun 	gspca_dbg(gspca_dev, D_USBO, "reg_wb v: %04x i: %04x %02x...%02x\n",
464*4882a593Smuzhiyun 		  value, index, *data, data[len - 1]);
465*4882a593Smuzhiyun 	memcpy(gspca_dev->usb_buf, data, len);
466*4882a593Smuzhiyun 	ret = usb_control_msg(gspca_dev->dev,
467*4882a593Smuzhiyun 			usb_sndctrlpipe(gspca_dev->dev, 0),
468*4882a593Smuzhiyun 			0x0c,			/* request */
469*4882a593Smuzhiyun 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
470*4882a593Smuzhiyun 			value, index, gspca_dev->usb_buf, len,
471*4882a593Smuzhiyun 			1000);
472*4882a593Smuzhiyun 	msleep(30);
473*4882a593Smuzhiyun 	if (ret < 0) {
474*4882a593Smuzhiyun 		pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
475*4882a593Smuzhiyun 		gspca_dev->usb_err = ret;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
i2c_write(struct sd * sd,const struct i2c_write_cmd * cmd,int ncmds)479*4882a593Smuzhiyun static void i2c_write(struct sd *sd,
480*4882a593Smuzhiyun 			const struct i2c_write_cmd *cmd,
481*4882a593Smuzhiyun 			int ncmds)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev = &sd->gspca_dev;
484*4882a593Smuzhiyun 	const struct sensor_s *sensor;
485*4882a593Smuzhiyun 	u16 val, idx;
486*4882a593Smuzhiyun 	u8 *buf;
487*4882a593Smuzhiyun 	int ret;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
490*4882a593Smuzhiyun 		return;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	sensor = &sensor_tb[sd->sensor];
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
495*4882a593Smuzhiyun 	idx = (cmd->val & 0xff00) | cmd->reg;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	buf = gspca_dev->usb_buf;
498*4882a593Smuzhiyun 	*buf++ = sensor->i2c_dum;
499*4882a593Smuzhiyun 	*buf++ = cmd->val;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	while (--ncmds > 0) {
502*4882a593Smuzhiyun 		cmd++;
503*4882a593Smuzhiyun 		*buf++ = cmd->reg;
504*4882a593Smuzhiyun 		*buf++ = cmd->val >> 8;
505*4882a593Smuzhiyun 		*buf++ = sensor->i2c_dum;
506*4882a593Smuzhiyun 		*buf++ = cmd->val;
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	gspca_dbg(gspca_dev, D_USBO, "i2c_w v: %04x i: %04x %02x...%02x\n",
510*4882a593Smuzhiyun 		  val, idx, gspca_dev->usb_buf[0], buf[-1]);
511*4882a593Smuzhiyun 	ret = usb_control_msg(gspca_dev->dev,
512*4882a593Smuzhiyun 			usb_sndctrlpipe(gspca_dev->dev, 0),
513*4882a593Smuzhiyun 			0x0c,			/* request */
514*4882a593Smuzhiyun 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
515*4882a593Smuzhiyun 			val, idx,
516*4882a593Smuzhiyun 			gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
517*4882a593Smuzhiyun 			500);
518*4882a593Smuzhiyun 	if (ret < 0) {
519*4882a593Smuzhiyun 		pr_err("i2c_write failed %d\n", ret);
520*4882a593Smuzhiyun 		gspca_dev->usb_err = ret;
521*4882a593Smuzhiyun 	}
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
ucbus_write(struct gspca_dev * gspca_dev,const struct ucbus_write_cmd * cmd,int ncmds,int batchsize)524*4882a593Smuzhiyun static void ucbus_write(struct gspca_dev *gspca_dev,
525*4882a593Smuzhiyun 			const struct ucbus_write_cmd *cmd,
526*4882a593Smuzhiyun 			int ncmds,
527*4882a593Smuzhiyun 			int batchsize)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	u8 *buf;
530*4882a593Smuzhiyun 	u16 val, idx;
531*4882a593Smuzhiyun 	int len, ret;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
534*4882a593Smuzhiyun 		return;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if ((batchsize - 1) * 3 > USB_BUF_SZ) {
537*4882a593Smuzhiyun 		gspca_err(gspca_dev, "Bug: usb_buf overflow\n");
538*4882a593Smuzhiyun 		gspca_dev->usb_err = -ENOMEM;
539*4882a593Smuzhiyun 		return;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	for (;;) {
543*4882a593Smuzhiyun 		len = ncmds;
544*4882a593Smuzhiyun 		if (len > batchsize)
545*4882a593Smuzhiyun 			len = batchsize;
546*4882a593Smuzhiyun 		ncmds -= len;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
549*4882a593Smuzhiyun 		idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		buf = gspca_dev->usb_buf;
552*4882a593Smuzhiyun 		while (--len > 0) {
553*4882a593Smuzhiyun 			cmd++;
554*4882a593Smuzhiyun 			*buf++ = cmd->bw_addr;
555*4882a593Smuzhiyun 			*buf++ = cmd->bw_addr >> 8;
556*4882a593Smuzhiyun 			*buf++ = cmd->bw_data;
557*4882a593Smuzhiyun 		}
558*4882a593Smuzhiyun 		if (buf != gspca_dev->usb_buf)
559*4882a593Smuzhiyun 			gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
560*4882a593Smuzhiyun 				  val, idx,
561*4882a593Smuzhiyun 				  gspca_dev->usb_buf[0], buf[-1]);
562*4882a593Smuzhiyun 		else
563*4882a593Smuzhiyun 			gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
564*4882a593Smuzhiyun 				  val, idx);
565*4882a593Smuzhiyun 		ret = usb_control_msg(gspca_dev->dev,
566*4882a593Smuzhiyun 				usb_sndctrlpipe(gspca_dev->dev, 0),
567*4882a593Smuzhiyun 				0x0c,			/* request */
568*4882a593Smuzhiyun 			   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
569*4882a593Smuzhiyun 				val, idx,
570*4882a593Smuzhiyun 				gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
571*4882a593Smuzhiyun 				500);
572*4882a593Smuzhiyun 		if (ret < 0) {
573*4882a593Smuzhiyun 			pr_err("ucbus_write failed %d\n", ret);
574*4882a593Smuzhiyun 			gspca_dev->usb_err = ret;
575*4882a593Smuzhiyun 			return;
576*4882a593Smuzhiyun 		}
577*4882a593Smuzhiyun 		msleep(30);
578*4882a593Smuzhiyun 		if (ncmds <= 0)
579*4882a593Smuzhiyun 			break;
580*4882a593Smuzhiyun 		cmd++;
581*4882a593Smuzhiyun 	}
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
gpio_set(struct sd * sd,u16 val,u16 mask)584*4882a593Smuzhiyun static void gpio_set(struct sd *sd, u16 val, u16 mask)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev = &sd->gspca_dev;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (mask & 0x00ff) {
589*4882a593Smuzhiyun 		sd->gpio[0] &= ~mask;
590*4882a593Smuzhiyun 		sd->gpio[0] |= val;
591*4882a593Smuzhiyun 		reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
592*4882a593Smuzhiyun 			~sd->gpio[0] << 8);
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 	mask >>= 8;
595*4882a593Smuzhiyun 	val >>= 8;
596*4882a593Smuzhiyun 	if (mask) {
597*4882a593Smuzhiyun 		sd->gpio[1] &= ~mask;
598*4882a593Smuzhiyun 		sd->gpio[1] |= val;
599*4882a593Smuzhiyun 		reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
600*4882a593Smuzhiyun 			~sd->gpio[1] << 8);
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
gpio_init(struct sd * sd,const u8 * gpio)604*4882a593Smuzhiyun static void gpio_init(struct sd *sd,
605*4882a593Smuzhiyun 			const u8 *gpio)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun 	gpio_set(sd, *gpio++, 0x000f);
608*4882a593Smuzhiyun 	gpio_set(sd, *gpio++, 0x000f);
609*4882a593Smuzhiyun 	gpio_set(sd, *gpio++, 0x000f);
610*4882a593Smuzhiyun 	gpio_set(sd, *gpio++, 0x000f);
611*4882a593Smuzhiyun 	gpio_set(sd, *gpio, 0x000f);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
bridge_init(struct sd * sd)614*4882a593Smuzhiyun static void bridge_init(struct sd *sd)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	static const struct ucbus_write_cmd clkfreq_cmd = {
617*4882a593Smuzhiyun 				0xf031, 0	/* SQ930_CLKFREQ_60MHZ */
618*4882a593Smuzhiyun 	};
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
cmos_probe(struct gspca_dev * gspca_dev)625*4882a593Smuzhiyun static void cmos_probe(struct gspca_dev *gspca_dev)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
628*4882a593Smuzhiyun 	int i;
629*4882a593Smuzhiyun 	const struct sensor_s *sensor;
630*4882a593Smuzhiyun 	static const u8 probe_order[] = {
631*4882a593Smuzhiyun /*		SENSOR_LZ24BP,		(tested as ccd) */
632*4882a593Smuzhiyun 		SENSOR_OV9630,
633*4882a593Smuzhiyun 		SENSOR_MI0360,
634*4882a593Smuzhiyun 		SENSOR_OV7660,
635*4882a593Smuzhiyun 		SENSOR_MT9V111,
636*4882a593Smuzhiyun 	};
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
639*4882a593Smuzhiyun 		sensor = &sensor_tb[probe_order[i]];
640*4882a593Smuzhiyun 		ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
641*4882a593Smuzhiyun 		gpio_init(sd, sensor->gpio);
642*4882a593Smuzhiyun 		msleep(100);
643*4882a593Smuzhiyun 		reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
644*4882a593Smuzhiyun 		msleep(100);
645*4882a593Smuzhiyun 		if (gspca_dev->usb_buf[0] != 0)
646*4882a593Smuzhiyun 			break;
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 	if (i >= ARRAY_SIZE(probe_order)) {
649*4882a593Smuzhiyun 		pr_err("Unknown sensor\n");
650*4882a593Smuzhiyun 		gspca_dev->usb_err = -EINVAL;
651*4882a593Smuzhiyun 		return;
652*4882a593Smuzhiyun 	}
653*4882a593Smuzhiyun 	sd->sensor = probe_order[i];
654*4882a593Smuzhiyun 	switch (sd->sensor) {
655*4882a593Smuzhiyun 	case SENSOR_OV7660:
656*4882a593Smuzhiyun 	case SENSOR_OV9630:
657*4882a593Smuzhiyun 		pr_err("Sensor %s not yet treated\n",
658*4882a593Smuzhiyun 		       sensor_tb[sd->sensor].name);
659*4882a593Smuzhiyun 		gspca_dev->usb_err = -EINVAL;
660*4882a593Smuzhiyun 		break;
661*4882a593Smuzhiyun 	}
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun 
mt9v111_init(struct gspca_dev * gspca_dev)664*4882a593Smuzhiyun static void mt9v111_init(struct gspca_dev *gspca_dev)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	int i, nwait;
667*4882a593Smuzhiyun 	static const u8 cmd_001b[] = {
668*4882a593Smuzhiyun 		0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
669*4882a593Smuzhiyun 		0x00, 0x00, 0x00
670*4882a593Smuzhiyun 	};
671*4882a593Smuzhiyun 	static const u8 cmd_011b[][7] = {
672*4882a593Smuzhiyun 		{0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
673*4882a593Smuzhiyun 		{0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
674*4882a593Smuzhiyun 		{0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
675*4882a593Smuzhiyun 		{0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
676*4882a593Smuzhiyun 	};
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
679*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
680*4882a593Smuzhiyun 		reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
681*4882a593Smuzhiyun 				ARRAY_SIZE(cmd_011b[0]));
682*4882a593Smuzhiyun 		msleep(400);
683*4882a593Smuzhiyun 		nwait = 20;
684*4882a593Smuzhiyun 		for (;;) {
685*4882a593Smuzhiyun 			reg_r(gspca_dev, 0x031b, 1);
686*4882a593Smuzhiyun 			if (gspca_dev->usb_buf[0] == 0
687*4882a593Smuzhiyun 			 || gspca_dev->usb_err != 0)
688*4882a593Smuzhiyun 				break;
689*4882a593Smuzhiyun 			if (--nwait < 0) {
690*4882a593Smuzhiyun 				gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
691*4882a593Smuzhiyun 				gspca_dev->usb_err = -ETIME;
692*4882a593Smuzhiyun 				return;
693*4882a593Smuzhiyun 			}
694*4882a593Smuzhiyun 			msleep(50);
695*4882a593Smuzhiyun 		}
696*4882a593Smuzhiyun 	}
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
global_init(struct sd * sd,int first_time)699*4882a593Smuzhiyun static void global_init(struct sd *sd, int first_time)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	switch (sd->sensor) {
702*4882a593Smuzhiyun 	case SENSOR_ICX098BQ:
703*4882a593Smuzhiyun 		if (first_time)
704*4882a593Smuzhiyun 			ucbus_write(&sd->gspca_dev,
705*4882a593Smuzhiyun 					icx098bq_start_0,
706*4882a593Smuzhiyun 					8, 8);
707*4882a593Smuzhiyun 		gpio_init(sd, sensor_tb[sd->sensor].gpio);
708*4882a593Smuzhiyun 		break;
709*4882a593Smuzhiyun 	case SENSOR_LZ24BP:
710*4882a593Smuzhiyun 		if (sd->type != Creative_live_motion)
711*4882a593Smuzhiyun 			gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
712*4882a593Smuzhiyun 		else
713*4882a593Smuzhiyun 			gpio_set(sd, 0, 0x00ff);
714*4882a593Smuzhiyun 		msleep(50);
715*4882a593Smuzhiyun 		if (first_time)
716*4882a593Smuzhiyun 			ucbus_write(&sd->gspca_dev,
717*4882a593Smuzhiyun 					lz24bp_start_0,
718*4882a593Smuzhiyun 					8, 8);
719*4882a593Smuzhiyun 		gpio_init(sd, sensor_tb[sd->sensor].gpio);
720*4882a593Smuzhiyun 		break;
721*4882a593Smuzhiyun 	case SENSOR_MI0360:
722*4882a593Smuzhiyun 		if (first_time)
723*4882a593Smuzhiyun 			ucbus_write(&sd->gspca_dev,
724*4882a593Smuzhiyun 					mi0360_start_0,
725*4882a593Smuzhiyun 					ARRAY_SIZE(mi0360_start_0),
726*4882a593Smuzhiyun 					8);
727*4882a593Smuzhiyun 		gpio_init(sd, sensor_tb[sd->sensor].gpio);
728*4882a593Smuzhiyun 		gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
729*4882a593Smuzhiyun 		break;
730*4882a593Smuzhiyun 	default:
731*4882a593Smuzhiyun /*	case SENSOR_MT9V111: */
732*4882a593Smuzhiyun 		if (first_time)
733*4882a593Smuzhiyun 			mt9v111_init(&sd->gspca_dev);
734*4882a593Smuzhiyun 		else
735*4882a593Smuzhiyun 			gpio_init(sd, sensor_tb[sd->sensor].gpio);
736*4882a593Smuzhiyun 		break;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
lz24bp_ppl(struct sd * sd,u16 ppl)740*4882a593Smuzhiyun static void lz24bp_ppl(struct sd *sd, u16 ppl)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	struct ucbus_write_cmd cmds[2] = {
743*4882a593Smuzhiyun 		{0xf810, ppl >> 8},
744*4882a593Smuzhiyun 		{0xf811, ppl}
745*4882a593Smuzhiyun 	};
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun 
setexposure(struct gspca_dev * gspca_dev,s32 expo,s32 gain)750*4882a593Smuzhiyun static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
751*4882a593Smuzhiyun {
752*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
753*4882a593Smuzhiyun 	int i, integclks, intstartclk, frameclks, min_frclk;
754*4882a593Smuzhiyun 	const struct sensor_s *sensor;
755*4882a593Smuzhiyun 	u16 cmd;
756*4882a593Smuzhiyun 	u8 buf[15];
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	integclks = expo;
759*4882a593Smuzhiyun 	i = 0;
760*4882a593Smuzhiyun 	cmd = SQ930_CTRL_SET_EXPOSURE;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	switch (sd->sensor) {
763*4882a593Smuzhiyun 	case SENSOR_ICX098BQ:			/* ccd */
764*4882a593Smuzhiyun 	case SENSOR_LZ24BP:
765*4882a593Smuzhiyun 		min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
766*4882a593Smuzhiyun 		if (integclks >= min_frclk) {
767*4882a593Smuzhiyun 			intstartclk = 0;
768*4882a593Smuzhiyun 			frameclks = integclks;
769*4882a593Smuzhiyun 		} else {
770*4882a593Smuzhiyun 			intstartclk = min_frclk - integclks;
771*4882a593Smuzhiyun 			frameclks = min_frclk;
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 		buf[i++] = intstartclk >> 8;
774*4882a593Smuzhiyun 		buf[i++] = intstartclk;
775*4882a593Smuzhiyun 		buf[i++] = frameclks >> 8;
776*4882a593Smuzhiyun 		buf[i++] = frameclks;
777*4882a593Smuzhiyun 		buf[i++] = gain;
778*4882a593Smuzhiyun 		break;
779*4882a593Smuzhiyun 	default:				/* cmos */
780*4882a593Smuzhiyun /*	case SENSOR_MI0360: */
781*4882a593Smuzhiyun /*	case SENSOR_MT9V111: */
782*4882a593Smuzhiyun 		cmd |= 0x0100;
783*4882a593Smuzhiyun 		sensor = &sensor_tb[sd->sensor];
784*4882a593Smuzhiyun 		buf[i++] = sensor->i2c_addr;	/* i2c_slave_addr */
785*4882a593Smuzhiyun 		buf[i++] = 0x08;	/* 2 * ni2c */
786*4882a593Smuzhiyun 		buf[i++] = 0x09;	/* reg = shutter width */
787*4882a593Smuzhiyun 		buf[i++] = integclks >> 8; /* val H */
788*4882a593Smuzhiyun 		buf[i++] = sensor->i2c_dum;
789*4882a593Smuzhiyun 		buf[i++] = integclks;	/* val L */
790*4882a593Smuzhiyun 		buf[i++] = 0x35;	/* reg = global gain */
791*4882a593Smuzhiyun 		buf[i++] = 0x00;	/* val H */
792*4882a593Smuzhiyun 		buf[i++] = sensor->i2c_dum;
793*4882a593Smuzhiyun 		buf[i++] = 0x80 + gain / 2; /* val L */
794*4882a593Smuzhiyun 		buf[i++] = 0x00;
795*4882a593Smuzhiyun 		buf[i++] = 0x00;
796*4882a593Smuzhiyun 		buf[i++] = 0x00;
797*4882a593Smuzhiyun 		buf[i++] = 0x00;
798*4882a593Smuzhiyun 		buf[i++] = 0x83;
799*4882a593Smuzhiyun 		break;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 	reg_wb(gspca_dev, cmd, 0, buf, i);
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun /* This function is called at probe time just before sd_init */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)805*4882a593Smuzhiyun static int sd_config(struct gspca_dev *gspca_dev,
806*4882a593Smuzhiyun 		const struct usb_device_id *id)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
809*4882a593Smuzhiyun 	struct cam *cam = &gspca_dev->cam;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	sd->sensor = id->driver_info >> 8;
812*4882a593Smuzhiyun 	sd->type = id->driver_info;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	cam->cam_mode = vga_mode;
815*4882a593Smuzhiyun 	cam->nmodes = ARRAY_SIZE(vga_mode);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	cam->bulk = 1;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	return 0;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)823*4882a593Smuzhiyun static int sd_init(struct gspca_dev *gspca_dev)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	sd->gpio[0] = sd->gpio[1] = 0xff;	/* force gpio rewrite */
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun /*fixme: is this needed for icx098bp and mi0360?
830*4882a593Smuzhiyun 	if (sd->sensor != SENSOR_LZ24BP)
831*4882a593Smuzhiyun 		reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
832*4882a593Smuzhiyun  */
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
835*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
836*4882a593Smuzhiyun 		return gspca_dev->usb_err;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun /* it returns:
839*4882a593Smuzhiyun  * 03 00 12 93 0b f6 c9 00	live! ultra
840*4882a593Smuzhiyun  * 03 00 07 93 0b f6 ca 00	live! ultra for notebook
841*4882a593Smuzhiyun  * 03 00 12 93 0b fe c8 00	Trust WB-3500T
842*4882a593Smuzhiyun  * 02 00 06 93 0b fe c8 00	Joy-IT 318S
843*4882a593Smuzhiyun  * 03 00 12 93 0b f6 cf 00	icam tracer - sensor icx098bq
844*4882a593Smuzhiyun  * 02 00 12 93 0b fe cf 00	ProQ Motion Webcam
845*4882a593Smuzhiyun  *
846*4882a593Smuzhiyun  * byte
847*4882a593Smuzhiyun  * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
848*4882a593Smuzhiyun  * 1: 00
849*4882a593Smuzhiyun  * 2: 06 / 07 / 12 = mode webcam? firmware??
850*4882a593Smuzhiyun  * 3: 93 chip = 930b (930b or 930c)
851*4882a593Smuzhiyun  * 4: 0b
852*4882a593Smuzhiyun  * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
853*4882a593Smuzhiyun  * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
854*4882a593Smuzhiyun  * 7: 00
855*4882a593Smuzhiyun  */
856*4882a593Smuzhiyun 	gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 8, gspca_dev->usb_buf);
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	bridge_init(sd);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_MI0360) {
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 		/* no sensor probe for icam tracer */
863*4882a593Smuzhiyun 		if (gspca_dev->usb_buf[5] == 0xf6)	/* if ccd */
864*4882a593Smuzhiyun 			sd->sensor = SENSOR_ICX098BQ;
865*4882a593Smuzhiyun 		else
866*4882a593Smuzhiyun 			cmos_probe(gspca_dev);
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 	if (gspca_dev->usb_err >= 0) {
869*4882a593Smuzhiyun 		gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
870*4882a593Smuzhiyun 			  sensor_tb[sd->sensor].name);
871*4882a593Smuzhiyun 		global_init(sd, 1);
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun 	return gspca_dev->usb_err;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun /* send the start/stop commands to the webcam */
send_start(struct gspca_dev * gspca_dev)877*4882a593Smuzhiyun static void send_start(struct gspca_dev *gspca_dev)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
880*4882a593Smuzhiyun 	const struct cap_s *cap;
881*4882a593Smuzhiyun 	int mode;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
884*4882a593Smuzhiyun 	cap = &capconfig[sd->sensor][mode];
885*4882a593Smuzhiyun 	reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
886*4882a593Smuzhiyun 			0x0a00 | cap->cc_sizeid,
887*4882a593Smuzhiyun 			cap->cc_bytes, 32);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
send_stop(struct gspca_dev * gspca_dev)890*4882a593Smuzhiyun static void send_stop(struct gspca_dev *gspca_dev)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun 	reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun /* function called at start time before URB creation */
sd_isoc_init(struct gspca_dev * gspca_dev)896*4882a593Smuzhiyun static int sd_isoc_init(struct gspca_dev *gspca_dev)
897*4882a593Smuzhiyun {
898*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	gspca_dev->cam.bulk_nurbs = 1;	/* there must be one URB only */
901*4882a593Smuzhiyun 	sd->do_ctrl = 0;
902*4882a593Smuzhiyun 	gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
903*4882a593Smuzhiyun 			gspca_dev->pixfmt.height + 8;
904*4882a593Smuzhiyun 	return 0;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun /* start the capture */
sd_start(struct gspca_dev * gspca_dev)908*4882a593Smuzhiyun static int sd_start(struct gspca_dev *gspca_dev)
909*4882a593Smuzhiyun {
910*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
911*4882a593Smuzhiyun 	int mode;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	bridge_init(sd);
914*4882a593Smuzhiyun 	global_init(sd, 0);
915*4882a593Smuzhiyun 	msleep(100);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	switch (sd->sensor) {
918*4882a593Smuzhiyun 	case SENSOR_ICX098BQ:
919*4882a593Smuzhiyun 		ucbus_write(gspca_dev, icx098bq_start_0,
920*4882a593Smuzhiyun 				ARRAY_SIZE(icx098bq_start_0),
921*4882a593Smuzhiyun 				8);
922*4882a593Smuzhiyun 		ucbus_write(gspca_dev, icx098bq_start_1,
923*4882a593Smuzhiyun 				ARRAY_SIZE(icx098bq_start_1),
924*4882a593Smuzhiyun 				5);
925*4882a593Smuzhiyun 		ucbus_write(gspca_dev, icx098bq_start_2,
926*4882a593Smuzhiyun 				ARRAY_SIZE(icx098bq_start_2),
927*4882a593Smuzhiyun 				6);
928*4882a593Smuzhiyun 		msleep(50);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 		/* 1st start */
931*4882a593Smuzhiyun 		send_start(gspca_dev);
932*4882a593Smuzhiyun 		gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
933*4882a593Smuzhiyun 		msleep(70);
934*4882a593Smuzhiyun 		reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
935*4882a593Smuzhiyun 		gpio_set(sd, 0x7f, 0x00ff);
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 		/* 2nd start */
938*4882a593Smuzhiyun 		send_start(gspca_dev);
939*4882a593Smuzhiyun 		gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
940*4882a593Smuzhiyun 		goto out;
941*4882a593Smuzhiyun 	case SENSOR_LZ24BP:
942*4882a593Smuzhiyun 		ucbus_write(gspca_dev, lz24bp_start_0,
943*4882a593Smuzhiyun 				ARRAY_SIZE(lz24bp_start_0),
944*4882a593Smuzhiyun 				8);
945*4882a593Smuzhiyun 		if (sd->type != Creative_live_motion)
946*4882a593Smuzhiyun 			ucbus_write(gspca_dev, lz24bp_start_1_gen,
947*4882a593Smuzhiyun 					ARRAY_SIZE(lz24bp_start_1_gen),
948*4882a593Smuzhiyun 					5);
949*4882a593Smuzhiyun 		else
950*4882a593Smuzhiyun 			ucbus_write(gspca_dev, lz24bp_start_1_clm,
951*4882a593Smuzhiyun 					ARRAY_SIZE(lz24bp_start_1_clm),
952*4882a593Smuzhiyun 					5);
953*4882a593Smuzhiyun 		ucbus_write(gspca_dev, lz24bp_start_2,
954*4882a593Smuzhiyun 				ARRAY_SIZE(lz24bp_start_2),
955*4882a593Smuzhiyun 				6);
956*4882a593Smuzhiyun 		mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
957*4882a593Smuzhiyun 		lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
958*4882a593Smuzhiyun 		msleep(10);
959*4882a593Smuzhiyun 		break;
960*4882a593Smuzhiyun 	case SENSOR_MI0360:
961*4882a593Smuzhiyun 		ucbus_write(gspca_dev, mi0360_start_0,
962*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_start_0),
963*4882a593Smuzhiyun 				8);
964*4882a593Smuzhiyun 		i2c_write(sd, mi0360_init_23,
965*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_init_23));
966*4882a593Smuzhiyun 		i2c_write(sd, mi0360_init_24,
967*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_init_24));
968*4882a593Smuzhiyun 		i2c_write(sd, mi0360_init_25,
969*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_init_25));
970*4882a593Smuzhiyun 		ucbus_write(gspca_dev, mi0360_start_1,
971*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_start_1),
972*4882a593Smuzhiyun 				5);
973*4882a593Smuzhiyun 		i2c_write(sd, mi0360_start_2,
974*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_start_2));
975*4882a593Smuzhiyun 		i2c_write(sd, mi0360_start_3,
976*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_start_3));
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 		/* 1st start */
979*4882a593Smuzhiyun 		send_start(gspca_dev);
980*4882a593Smuzhiyun 		msleep(60);
981*4882a593Smuzhiyun 		send_stop(gspca_dev);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		i2c_write(sd,
984*4882a593Smuzhiyun 			mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
985*4882a593Smuzhiyun 		break;
986*4882a593Smuzhiyun 	default:
987*4882a593Smuzhiyun /*	case SENSOR_MT9V111: */
988*4882a593Smuzhiyun 		ucbus_write(gspca_dev, mi0360_start_0,
989*4882a593Smuzhiyun 				ARRAY_SIZE(mi0360_start_0),
990*4882a593Smuzhiyun 				8);
991*4882a593Smuzhiyun 		i2c_write(sd, mt9v111_init_0,
992*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_init_0));
993*4882a593Smuzhiyun 		i2c_write(sd, mt9v111_init_1,
994*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_init_1));
995*4882a593Smuzhiyun 		i2c_write(sd, mt9v111_init_2,
996*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_init_2));
997*4882a593Smuzhiyun 		ucbus_write(gspca_dev, mt9v111_start_1,
998*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_start_1),
999*4882a593Smuzhiyun 				5);
1000*4882a593Smuzhiyun 		i2c_write(sd, mt9v111_init_3,
1001*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_init_3));
1002*4882a593Smuzhiyun 		i2c_write(sd, mt9v111_init_4,
1003*4882a593Smuzhiyun 				ARRAY_SIZE(mt9v111_init_4));
1004*4882a593Smuzhiyun 		break;
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	send_start(gspca_dev);
1008*4882a593Smuzhiyun out:
1009*4882a593Smuzhiyun 	msleep(1000);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_MT9V111)
1012*4882a593Smuzhiyun 		gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	sd->do_ctrl = 1;	/* set the exposure */
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	return gspca_dev->usb_err;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
sd_stopN(struct gspca_dev * gspca_dev)1019*4882a593Smuzhiyun static void sd_stopN(struct gspca_dev *gspca_dev)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_MT9V111)
1024*4882a593Smuzhiyun 		gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
1025*4882a593Smuzhiyun 	send_stop(gspca_dev);
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun /* function called when the application gets a new frame */
1029*4882a593Smuzhiyun /* It sets the exposure if required and restart the bulk transfer. */
sd_dq_callback(struct gspca_dev * gspca_dev)1030*4882a593Smuzhiyun static void sd_dq_callback(struct gspca_dev *gspca_dev)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1033*4882a593Smuzhiyun 	int ret;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1036*4882a593Smuzhiyun 		return;
1037*4882a593Smuzhiyun 	sd->do_ctrl = 0;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1040*4882a593Smuzhiyun 			v4l2_ctrl_g_ctrl(sd->gain));
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	gspca_dev->cam.bulk_nurbs = 1;
1043*4882a593Smuzhiyun 	ret = usb_submit_urb(gspca_dev->urb[0], GFP_KERNEL);
1044*4882a593Smuzhiyun 	if (ret < 0)
1045*4882a593Smuzhiyun 		pr_err("sd_dq_callback() err %d\n", ret);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	/* wait a little time, otherwise the webcam crashes */
1048*4882a593Smuzhiyun 	msleep(100);
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun 
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)1051*4882a593Smuzhiyun static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1052*4882a593Smuzhiyun 			u8 *data,		/* isoc packet */
1053*4882a593Smuzhiyun 			int len)		/* iso packet length */
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (sd->do_ctrl)
1058*4882a593Smuzhiyun 		gspca_dev->cam.bulk_nurbs = 0;
1059*4882a593Smuzhiyun 	gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
1060*4882a593Smuzhiyun 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
1061*4882a593Smuzhiyun 	gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun 
sd_s_ctrl(struct v4l2_ctrl * ctrl)1064*4882a593Smuzhiyun static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev =
1067*4882a593Smuzhiyun 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1068*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	gspca_dev->usb_err = 0;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	if (!gspca_dev->streaming)
1073*4882a593Smuzhiyun 		return 0;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	switch (ctrl->id) {
1076*4882a593Smuzhiyun 	case V4L2_CID_EXPOSURE:
1077*4882a593Smuzhiyun 		setexposure(gspca_dev, ctrl->val, sd->gain->val);
1078*4882a593Smuzhiyun 		break;
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 	return gspca_dev->usb_err;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1084*4882a593Smuzhiyun 	.s_ctrl = sd_s_ctrl,
1085*4882a593Smuzhiyun };
1086*4882a593Smuzhiyun 
sd_init_controls(struct gspca_dev * gspca_dev)1087*4882a593Smuzhiyun static int sd_init_controls(struct gspca_dev *gspca_dev)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1090*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	gspca_dev->vdev.ctrl_handler = hdl;
1093*4882a593Smuzhiyun 	v4l2_ctrl_handler_init(hdl, 2);
1094*4882a593Smuzhiyun 	sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1095*4882a593Smuzhiyun 			V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
1096*4882a593Smuzhiyun 	sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1097*4882a593Smuzhiyun 			V4L2_CID_GAIN, 1, 255, 1, 0x8d);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	if (hdl->error) {
1100*4882a593Smuzhiyun 		pr_err("Could not initialize controls\n");
1101*4882a593Smuzhiyun 		return hdl->error;
1102*4882a593Smuzhiyun 	}
1103*4882a593Smuzhiyun 	v4l2_ctrl_cluster(2, &sd->exposure);
1104*4882a593Smuzhiyun 	return 0;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun /* sub-driver description */
1108*4882a593Smuzhiyun static const struct sd_desc sd_desc = {
1109*4882a593Smuzhiyun 	.name   = MODULE_NAME,
1110*4882a593Smuzhiyun 	.config = sd_config,
1111*4882a593Smuzhiyun 	.init   = sd_init,
1112*4882a593Smuzhiyun 	.init_controls = sd_init_controls,
1113*4882a593Smuzhiyun 	.isoc_init = sd_isoc_init,
1114*4882a593Smuzhiyun 	.start  = sd_start,
1115*4882a593Smuzhiyun 	.stopN  = sd_stopN,
1116*4882a593Smuzhiyun 	.pkt_scan = sd_pkt_scan,
1117*4882a593Smuzhiyun 	.dq_callback = sd_dq_callback,
1118*4882a593Smuzhiyun };
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun /* Table of supported USB devices */
1121*4882a593Smuzhiyun #define ST(sensor, type) \
1122*4882a593Smuzhiyun 	.driver_info = (SENSOR_ ## sensor << 8) \
1123*4882a593Smuzhiyun 			| (type)
1124*4882a593Smuzhiyun static const struct usb_device_id device_table[] = {
1125*4882a593Smuzhiyun 	{USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
1126*4882a593Smuzhiyun 	{USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
1127*4882a593Smuzhiyun 	{USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
1128*4882a593Smuzhiyun 	{USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
1129*4882a593Smuzhiyun 	{USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
1130*4882a593Smuzhiyun 	{USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1131*4882a593Smuzhiyun 	{}
1132*4882a593Smuzhiyun };
1133*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, device_table);
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)1137*4882a593Smuzhiyun static int sd_probe(struct usb_interface *intf,
1138*4882a593Smuzhiyun 		const struct usb_device_id *id)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1141*4882a593Smuzhiyun 			THIS_MODULE);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun static struct usb_driver sd_driver = {
1145*4882a593Smuzhiyun 	.name	    = MODULE_NAME,
1146*4882a593Smuzhiyun 	.id_table   = device_table,
1147*4882a593Smuzhiyun 	.probe	    = sd_probe,
1148*4882a593Smuzhiyun 	.disconnect = gspca_disconnect,
1149*4882a593Smuzhiyun #ifdef CONFIG_PM
1150*4882a593Smuzhiyun 	.suspend    = gspca_suspend,
1151*4882a593Smuzhiyun 	.resume     = gspca_resume,
1152*4882a593Smuzhiyun 	.reset_resume = gspca_resume,
1153*4882a593Smuzhiyun #endif
1154*4882a593Smuzhiyun };
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun module_usb_driver(sd_driver);
1157