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