Commit | Line | Data |
---|---|---|
17ea88ae | 1 | /* |
c109f816 EA |
2 | * USB Driver for ALi m5602 based webcams |
3 | * | |
0c505e68 | 4 | * Copyright (C) 2008 Erik Andrén |
c109f816 EA |
5 | * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project. |
6 | * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br> | |
7 | * | |
8 | * Portions of code to USB interface and ALi driver software, | |
9 | * Copyright (c) 2006 Willem Duinker | |
10 | * v4l2 interface modeled after the V4L2 driver | |
11 | * for SN9C10x PC Camera Controllers | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License as | |
15 | * published by the Free Software Foundation, version 2. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include "m5602_ov9650.h" | |
7b826503 | 20 | #include "m5602_ov7660.h" |
c109f816 EA |
21 | #include "m5602_mt9m111.h" |
22 | #include "m5602_po1030.h" | |
23 | #include "m5602_s5k83a.h" | |
24 | #include "m5602_s5k4aa.h" | |
25 | ||
26 | /* Kernel module parameters */ | |
27 | int force_sensor; | |
5c1a15a1 | 28 | static int dump_bridge; |
c109f816 | 29 | int dump_sensor; |
c109f816 EA |
30 | |
31 | static const __devinitdata struct usb_device_id m5602_table[] = { | |
32 | {USB_DEVICE(0x0402, 0x5602)}, | |
33 | {} | |
34 | }; | |
35 | ||
36 | MODULE_DEVICE_TABLE(usb, m5602_table); | |
37 | ||
c109f816 | 38 | /* Reads a byte from the m5602 */ |
ff1e6a21 | 39 | int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data) |
c109f816 EA |
40 | { |
41 | int err; | |
42 | struct usb_device *udev = sd->gspca_dev.dev; | |
43 | __u8 *buf = sd->gspca_dev.usb_buf; | |
44 | ||
45 | err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
46 | 0x04, 0xc0, 0x14, | |
47 | 0x8100 + address, buf, | |
48 | 1, M5602_URB_MSG_TIMEOUT); | |
49 | *i2c_data = buf[0]; | |
50 | ||
17ea88ae | 51 | PDEBUG(D_CONF, "Reading bridge register 0x%x containing 0x%x", |
c109f816 EA |
52 | address, *i2c_data); |
53 | ||
54 | /* usb_control_msg(...) returns the number of bytes sent upon success, | |
bd99ffbd | 55 | mask that and return zero instead*/ |
c109f816 EA |
56 | return (err < 0) ? err : 0; |
57 | } | |
58 | ||
fd589a8f | 59 | /* Writes a byte to the m5602 */ |
ff1e6a21 | 60 | int m5602_write_bridge(struct sd *sd, const u8 address, const u8 i2c_data) |
c109f816 EA |
61 | { |
62 | int err; | |
63 | struct usb_device *udev = sd->gspca_dev.dev; | |
64 | __u8 *buf = sd->gspca_dev.usb_buf; | |
65 | ||
17ea88ae | 66 | PDEBUG(D_CONF, "Writing bridge register 0x%x with 0x%x", |
c109f816 EA |
67 | address, i2c_data); |
68 | ||
69 | memcpy(buf, bridge_urb_skeleton, | |
70 | sizeof(bridge_urb_skeleton)); | |
71 | buf[1] = address; | |
72 | buf[3] = i2c_data; | |
73 | ||
74 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
75 | 0x04, 0x40, 0x19, | |
76 | 0x0000, buf, | |
77 | 4, M5602_URB_MSG_TIMEOUT); | |
78 | ||
79 | /* usb_control_msg(...) returns the number of bytes sent upon success, | |
bd99ffbd | 80 | mask that and return zero instead */ |
c109f816 EA |
81 | return (err < 0) ? err : 0; |
82 | } | |
83 | ||
ac3d5bfe LK |
84 | int m5602_wait_for_i2c(struct sd *sd) |
85 | { | |
86 | int err; | |
87 | u8 data; | |
4c3414ee | 88 | |
ac3d5bfe LK |
89 | do { |
90 | err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, &data); | |
91 | } while ((data & I2C_BUSY) && !err); | |
4c3414ee | 92 | return err; |
ac3d5bfe LK |
93 | } |
94 | ||
e7a7f510 EA |
95 | int m5602_read_sensor(struct sd *sd, const u8 address, |
96 | u8 *i2c_data, const u8 len) | |
97 | { | |
98 | int err, i; | |
99 | ||
edbfbdb3 EA |
100 | if (!len || len > sd->sensor->i2c_regW) |
101 | return -EINVAL; | |
102 | ||
ac3d5bfe | 103 | err = m5602_wait_for_i2c(sd); |
edbfbdb3 | 104 | if (err < 0) |
051781b3 | 105 | return err; |
e7a7f510 EA |
106 | |
107 | err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR, | |
108 | sd->sensor->i2c_slave_id); | |
109 | if (err < 0) | |
051781b3 | 110 | return err; |
e7a7f510 EA |
111 | |
112 | err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address); | |
113 | if (err < 0) | |
051781b3 | 114 | return err; |
e7a7f510 | 115 | |
1b844b53 EA |
116 | /* Sensors with registers that are of only |
117 | one byte width are differently read */ | |
118 | ||
52fa70b7 | 119 | /* FIXME: This works with the ov9650, but has issues with the po1030 */ |
edbfbdb3 | 120 | if (sd->sensor->i2c_regW == 1) { |
52fa70b7 | 121 | err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 1); |
edbfbdb3 | 122 | if (err < 0) |
051781b3 | 123 | return err; |
edbfbdb3 EA |
124 | |
125 | err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08); | |
edbfbdb3 EA |
126 | } else { |
127 | err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len); | |
edbfbdb3 | 128 | } |
e7a7f510 EA |
129 | |
130 | for (i = 0; (i < len) && !err; i++) { | |
ac3d5bfe LK |
131 | err = m5602_wait_for_i2c(sd); |
132 | if (err < 0) | |
133 | return err; | |
134 | ||
e7a7f510 EA |
135 | err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i])); |
136 | ||
137 | PDEBUG(D_CONF, "Reading sensor register " | |
138 | "0x%x containing 0x%x ", address, *i2c_data); | |
139 | } | |
e7a7f510 EA |
140 | return err; |
141 | } | |
142 | ||
4b8f3933 EA |
143 | int m5602_write_sensor(struct sd *sd, const u8 address, |
144 | u8 *i2c_data, const u8 len) | |
145 | { | |
146 | int err, i; | |
147 | u8 *p; | |
148 | struct usb_device *udev = sd->gspca_dev.dev; | |
149 | __u8 *buf = sd->gspca_dev.usb_buf; | |
150 | ||
151 | /* No sensor with a data width larger than 16 bits has yet been seen */ | |
152 | if (len > sd->sensor->i2c_regW || !len) | |
153 | return -EINVAL; | |
154 | ||
155 | memcpy(buf, sensor_urb_skeleton, | |
156 | sizeof(sensor_urb_skeleton)); | |
157 | ||
158 | buf[11] = sd->sensor->i2c_slave_id; | |
159 | buf[15] = address; | |
160 | ||
161 | /* Special case larger sensor writes */ | |
162 | p = buf + 16; | |
163 | ||
164 | /* Copy a four byte write sequence for each byte to be written to */ | |
165 | for (i = 0; i < len; i++) { | |
166 | memcpy(p, sensor_urb_skeleton + 16, 4); | |
167 | p[3] = i2c_data[i]; | |
168 | p += 4; | |
169 | PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x", | |
170 | address, i2c_data[i]); | |
171 | } | |
172 | ||
173 | /* Copy the tailer */ | |
174 | memcpy(p, sensor_urb_skeleton + 20, 4); | |
175 | ||
176 | /* Set the total length */ | |
177 | p[3] = 0x10 + len; | |
178 | ||
179 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
180 | 0x04, 0x40, 0x19, | |
181 | 0x0000, buf, | |
182 | 20 + len * 4, M5602_URB_MSG_TIMEOUT); | |
183 | ||
184 | return (err < 0) ? err : 0; | |
185 | } | |
186 | ||
c109f816 EA |
187 | /* Dump all the registers of the m5602 bridge, |
188 | unfortunately this breaks the camera until it's power cycled */ | |
189 | static void m5602_dump_bridge(struct sd *sd) | |
190 | { | |
191 | int i; | |
192 | for (i = 0; i < 0x80; i++) { | |
193 | unsigned char val = 0; | |
194 | m5602_read_bridge(sd, i, &val); | |
195 | info("ALi m5602 address 0x%x contains 0x%x", i, val); | |
196 | } | |
17ea88ae EA |
197 | info("Warning: The ALi m5602 webcam probably won't work " |
198 | "until it's power cycled"); | |
c109f816 EA |
199 | } |
200 | ||
e66c6419 | 201 | static int m5602_probe_sensor(struct sd *sd) |
c109f816 EA |
202 | { |
203 | /* Try the po1030 */ | |
204 | sd->sensor = &po1030; | |
d2d7e9ae | 205 | if (!sd->sensor->probe(sd)) |
c109f816 | 206 | return 0; |
c109f816 EA |
207 | |
208 | /* Try the mt9m111 sensor */ | |
209 | sd->sensor = &mt9m111; | |
d2d7e9ae | 210 | if (!sd->sensor->probe(sd)) |
c109f816 | 211 | return 0; |
c109f816 EA |
212 | |
213 | /* Try the s5k4aa */ | |
214 | sd->sensor = &s5k4aa; | |
d2d7e9ae | 215 | if (!sd->sensor->probe(sd)) |
c109f816 | 216 | return 0; |
c109f816 EA |
217 | |
218 | /* Try the ov9650 */ | |
219 | sd->sensor = &ov9650; | |
d2d7e9ae | 220 | if (!sd->sensor->probe(sd)) |
c109f816 | 221 | return 0; |
c109f816 | 222 | |
7b826503 EA |
223 | /* Try the ov7660 */ |
224 | sd->sensor = &ov7660; | |
225 | if (!sd->sensor->probe(sd)) | |
226 | return 0; | |
227 | ||
c109f816 EA |
228 | /* Try the s5k83a */ |
229 | sd->sensor = &s5k83a; | |
d2d7e9ae | 230 | if (!sd->sensor->probe(sd)) |
c109f816 | 231 | return 0; |
1b334599 | 232 | |
c109f816 EA |
233 | /* More sensor probe function goes here */ |
234 | info("Failed to find a sensor"); | |
235 | sd->sensor = NULL; | |
236 | return -ENODEV; | |
237 | } | |
238 | ||
e66c6419 JFM |
239 | static int m5602_configure(struct gspca_dev *gspca_dev, |
240 | const struct usb_device_id *id); | |
241 | ||
242 | static int m5602_init(struct gspca_dev *gspca_dev) | |
c109f816 EA |
243 | { |
244 | struct sd *sd = (struct sd *) gspca_dev; | |
245 | int err; | |
246 | ||
17ea88ae | 247 | PDEBUG(D_CONF, "Initializing ALi m5602 webcam"); |
c109f816 EA |
248 | /* Run the init sequence */ |
249 | err = sd->sensor->init(sd); | |
250 | ||
251 | return err; | |
252 | } | |
253 | ||
e66c6419 | 254 | static int m5602_start_transfer(struct gspca_dev *gspca_dev) |
c109f816 EA |
255 | { |
256 | struct sd *sd = (struct sd *) gspca_dev; | |
257 | __u8 *buf = sd->gspca_dev.usb_buf; | |
588dbbb5 | 258 | int err; |
c109f816 EA |
259 | |
260 | /* Send start command to the camera */ | |
261 | const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01}; | |
619a4d5c | 262 | |
3da3dce5 EA |
263 | if (sd->sensor->start) |
264 | sd->sensor->start(sd); | |
265 | ||
c109f816 | 266 | memcpy(buf, buffer, sizeof(buffer)); |
588dbbb5 EA |
267 | err = usb_control_msg(gspca_dev->dev, |
268 | usb_sndctrlpipe(gspca_dev->dev, 0), | |
269 | 0x04, 0x40, 0x19, 0x0000, buf, | |
619a4d5c | 270 | sizeof(buffer), M5602_URB_MSG_TIMEOUT); |
c109f816 | 271 | |
17ea88ae | 272 | PDEBUG(D_STREAM, "Transfer started"); |
588dbbb5 | 273 | return (err < 0) ? err : 0; |
c109f816 EA |
274 | } |
275 | ||
e66c6419 JFM |
276 | static void m5602_urb_complete(struct gspca_dev *gspca_dev, |
277 | struct gspca_frame *frame, | |
c109f816 EA |
278 | __u8 *data, int len) |
279 | { | |
280 | struct sd *sd = (struct sd *) gspca_dev; | |
281 | ||
282 | if (len < 6) { | |
17ea88ae | 283 | PDEBUG(D_PACK, "Packet is less than 6 bytes"); |
c109f816 EA |
284 | return; |
285 | } | |
286 | ||
287 | /* Frame delimiter: ff xx xx xx ff ff */ | |
288 | if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff && | |
289 | data[2] != sd->frame_id) { | |
17ea88ae | 290 | PDEBUG(D_FRAM, "Frame delimiter detected"); |
c109f816 EA |
291 | sd->frame_id = data[2]; |
292 | ||
293 | /* Remove the extra fluff appended on each header */ | |
294 | data += 6; | |
295 | len -= 6; | |
296 | ||
297 | /* Complete the last frame (if any) */ | |
298 | frame = gspca_frame_add(gspca_dev, LAST_PACKET, | |
299 | frame, data, 0); | |
1b334599 | 300 | sd->frame_count++; |
c109f816 EA |
301 | |
302 | /* Create a new frame */ | |
303 | gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len); | |
304 | ||
17ea88ae | 305 | PDEBUG(D_FRAM, "Starting new frame %d", |
1b334599 | 306 | sd->frame_count); |
c109f816 EA |
307 | |
308 | } else { | |
309 | int cur_frame_len = frame->data_end - frame->data; | |
310 | ||
311 | /* Remove urb header */ | |
312 | data += 4; | |
313 | len -= 4; | |
314 | ||
315 | if (cur_frame_len + len <= frame->v4l2_buf.length) { | |
17ea88ae | 316 | PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes", |
1b334599 | 317 | sd->frame_count, len); |
c109f816 EA |
318 | |
319 | gspca_frame_add(gspca_dev, INTER_PACKET, frame, | |
320 | data, len); | |
321 | } else if (frame->v4l2_buf.length - cur_frame_len > 0) { | |
322 | /* Add the remaining data up to frame size */ | |
323 | gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, | |
324 | frame->v4l2_buf.length - cur_frame_len); | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
e66c6419 | 329 | static void m5602_stop_transfer(struct gspca_dev *gspca_dev) |
c109f816 | 330 | { |
4a7581f0 LK |
331 | struct sd *sd = (struct sd *) gspca_dev; |
332 | ||
333 | /* Run the sensor specific end transfer sequence */ | |
334 | if (sd->sensor->stop) | |
335 | sd->sensor->stop(sd); | |
c109f816 EA |
336 | } |
337 | ||
e66c6419 JFM |
338 | /* sub-driver description, the ctrl and nctrl is filled at probe time */ |
339 | static struct sd_desc sd_desc = { | |
340 | .name = MODULE_NAME, | |
341 | .config = m5602_configure, | |
342 | .init = m5602_init, | |
343 | .start = m5602_start_transfer, | |
344 | .stopN = m5602_stop_transfer, | |
345 | .pkt_scan = m5602_urb_complete | |
346 | }; | |
347 | ||
c109f816 | 348 | /* this function is called at probe time */ |
e66c6419 | 349 | static int m5602_configure(struct gspca_dev *gspca_dev, |
c109f816 EA |
350 | const struct usb_device_id *id) |
351 | { | |
352 | struct sd *sd = (struct sd *) gspca_dev; | |
353 | struct cam *cam; | |
354 | int err; | |
355 | ||
c109f816 | 356 | cam = &gspca_dev->cam; |
d2d7e9ae | 357 | sd->desc = &sd_desc; |
c109f816 EA |
358 | |
359 | if (dump_bridge) | |
360 | m5602_dump_bridge(sd); | |
361 | ||
362 | /* Probe sensor */ | |
363 | err = m5602_probe_sensor(sd); | |
364 | if (err) | |
365 | goto fail; | |
366 | ||
c109f816 EA |
367 | return 0; |
368 | ||
369 | fail: | |
17ea88ae | 370 | PDEBUG(D_ERR, "ALi m5602 webcam failed"); |
c109f816 EA |
371 | cam->cam_mode = NULL; |
372 | cam->nmodes = 0; | |
373 | ||
374 | return err; | |
375 | } | |
376 | ||
377 | static int m5602_probe(struct usb_interface *intf, | |
378 | const struct usb_device_id *id) | |
379 | { | |
380 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), | |
381 | THIS_MODULE); | |
382 | } | |
383 | ||
d9c700d4 EA |
384 | void m5602_disconnect(struct usb_interface *intf) |
385 | { | |
386 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); | |
387 | struct sd *sd = (struct sd *) gspca_dev; | |
388 | ||
389 | if (sd->sensor->disconnect) | |
390 | sd->sensor->disconnect(sd); | |
391 | ||
392 | gspca_disconnect(intf); | |
393 | } | |
394 | ||
c109f816 EA |
395 | static struct usb_driver sd_driver = { |
396 | .name = MODULE_NAME, | |
397 | .id_table = m5602_table, | |
398 | .probe = m5602_probe, | |
399 | #ifdef CONFIG_PM | |
400 | .suspend = gspca_suspend, | |
401 | .resume = gspca_resume, | |
402 | #endif | |
d9c700d4 | 403 | .disconnect = m5602_disconnect |
c109f816 EA |
404 | }; |
405 | ||
406 | /* -- module insert / remove -- */ | |
407 | static int __init mod_m5602_init(void) | |
408 | { | |
f69e9529 AK |
409 | int ret; |
410 | ret = usb_register(&sd_driver); | |
411 | if (ret < 0) | |
e6b14849 | 412 | return ret; |
17ea88ae | 413 | PDEBUG(D_PROBE, "registered"); |
c109f816 EA |
414 | return 0; |
415 | } | |
619a4d5c | 416 | |
c109f816 EA |
417 | static void __exit mod_m5602_exit(void) |
418 | { | |
419 | usb_deregister(&sd_driver); | |
17ea88ae | 420 | PDEBUG(D_PROBE, "deregistered"); |
c109f816 EA |
421 | } |
422 | ||
423 | module_init(mod_m5602_init); | |
424 | module_exit(mod_m5602_exit); | |
425 | ||
426 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
427 | MODULE_DESCRIPTION(DRIVER_DESC); | |
428 | MODULE_LICENSE("GPL"); | |
c109f816 EA |
429 | module_param(force_sensor, int, S_IRUGO | S_IWUSR); |
430 | MODULE_PARM_DESC(force_sensor, | |
56513ed5 EA |
431 | "forces detection of a sensor, " |
432 | "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, " | |
433 | "4 = MT9M111, 5 = PO1030, 6 = OV7660"); | |
c109f816 EA |
434 | |
435 | module_param(dump_bridge, bool, S_IRUGO | S_IWUSR); | |
436 | MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup"); | |
437 | ||
438 | module_param(dump_sensor, bool, S_IRUGO | S_IWUSR); | |
439 | MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers " | |
440 | "at startup providing a sensor is found"); |