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