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