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