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