2 * USB Driver for ALi m5602 based webcams
4 * Copyright (C) 2008 Erik Andrén
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>
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
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.
19 #include "m5602_ov9650.h"
20 #include "m5602_ov7660.h"
21 #include "m5602_mt9m111.h"
22 #include "m5602_po1030.h"
23 #include "m5602_s5k83a.h"
24 #include "m5602_s5k4aa.h"
26 /* Kernel module parameters */
28 static int dump_bridge
;
31 static const struct usb_device_id m5602_table
[] = {
32 {USB_DEVICE(0x0402, 0x5602)},
36 MODULE_DEVICE_TABLE(usb
, m5602_table
);
38 /* Reads a byte from the m5602 */
39 int m5602_read_bridge(struct sd
*sd
, const u8 address
, u8
*i2c_data
)
42 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
43 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
45 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
47 0x8100 + address
, buf
,
48 1, M5602_URB_MSG_TIMEOUT
);
51 PDEBUG(D_CONF
, "Reading bridge register 0x%x containing 0x%x",
54 /* usb_control_msg(...) returns the number of bytes sent upon success,
55 mask that and return zero instead*/
56 return (err
< 0) ? err
: 0;
59 /* Writes a byte to the m5602 */
60 int m5602_write_bridge(struct sd
*sd
, const u8 address
, const u8 i2c_data
)
63 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
64 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
66 PDEBUG(D_CONF
, "Writing bridge register 0x%x with 0x%x",
69 memcpy(buf
, bridge_urb_skeleton
,
70 sizeof(bridge_urb_skeleton
));
74 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
77 4, M5602_URB_MSG_TIMEOUT
);
79 /* usb_control_msg(...) returns the number of bytes sent upon success,
80 mask that and return zero instead */
81 return (err
< 0) ? err
: 0;
84 static int m5602_wait_for_i2c(struct sd
*sd
)
90 err
= m5602_read_bridge(sd
, M5602_XB_I2C_STATUS
, &data
);
91 } while ((data
& I2C_BUSY
) && !err
);
95 int m5602_read_sensor(struct sd
*sd
, const u8 address
,
96 u8
*i2c_data
, const u8 len
)
100 if (!len
|| len
> sd
->sensor
->i2c_regW
)
103 err
= m5602_wait_for_i2c(sd
);
107 err
= m5602_write_bridge(sd
, M5602_XB_I2C_DEV_ADDR
,
108 sd
->sensor
->i2c_slave_id
);
112 err
= m5602_write_bridge(sd
, M5602_XB_I2C_REG_ADDR
, address
);
116 /* Sensors with registers that are of only
117 one byte width are differently read */
119 /* FIXME: This works with the ov9650, but has issues with the po1030 */
120 if (sd
->sensor
->i2c_regW
== 1) {
121 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 1);
125 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x08);
127 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x18 + len
);
130 for (i
= 0; (i
< len
) && !err
; i
++) {
131 err
= m5602_wait_for_i2c(sd
);
135 err
= m5602_read_bridge(sd
, M5602_XB_I2C_DATA
, &(i2c_data
[i
]));
137 PDEBUG(D_CONF
, "Reading sensor register "
138 "0x%x containing 0x%x ", address
, *i2c_data
);
143 int m5602_write_sensor(struct sd
*sd
, const u8 address
,
144 u8
*i2c_data
, const u8 len
)
148 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
149 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
151 /* No sensor with a data width larger than 16 bits has yet been seen */
152 if (len
> sd
->sensor
->i2c_regW
|| !len
)
155 memcpy(buf
, sensor_urb_skeleton
,
156 sizeof(sensor_urb_skeleton
));
158 buf
[11] = sd
->sensor
->i2c_slave_id
;
161 /* Special case larger sensor writes */
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);
169 PDEBUG(D_CONF
, "Writing sensor register 0x%x with 0x%x",
170 address
, i2c_data
[i
]);
173 /* Copy the tailer */
174 memcpy(p
, sensor_urb_skeleton
+ 20, 4);
176 /* Set the total length */
179 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
182 20 + len
* 4, M5602_URB_MSG_TIMEOUT
);
184 return (err
< 0) ? err
: 0;
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
)
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
);
197 info("Warning: The ALi m5602 webcam probably won't work "
198 "until it's power cycled");
201 static int m5602_probe_sensor(struct sd
*sd
)
204 sd
->sensor
= &po1030
;
205 if (!sd
->sensor
->probe(sd
))
208 /* Try the mt9m111 sensor */
209 sd
->sensor
= &mt9m111
;
210 if (!sd
->sensor
->probe(sd
))
214 sd
->sensor
= &s5k4aa
;
215 if (!sd
->sensor
->probe(sd
))
219 sd
->sensor
= &ov9650
;
220 if (!sd
->sensor
->probe(sd
))
224 sd
->sensor
= &ov7660
;
225 if (!sd
->sensor
->probe(sd
))
229 sd
->sensor
= &s5k83a
;
230 if (!sd
->sensor
->probe(sd
))
233 /* More sensor probe function goes here */
234 info("Failed to find a sensor");
239 static int m5602_configure(struct gspca_dev
*gspca_dev
,
240 const struct usb_device_id
*id
);
242 static int m5602_init(struct gspca_dev
*gspca_dev
)
244 struct sd
*sd
= (struct sd
*) gspca_dev
;
247 PDEBUG(D_CONF
, "Initializing ALi m5602 webcam");
248 /* Run the init sequence */
249 err
= sd
->sensor
->init(sd
);
254 static int m5602_start_transfer(struct gspca_dev
*gspca_dev
)
256 struct sd
*sd
= (struct sd
*) gspca_dev
;
257 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
260 /* Send start command to the camera */
261 const u8 buffer
[4] = {0x13, 0xf9, 0x0f, 0x01};
263 if (sd
->sensor
->start
)
264 sd
->sensor
->start(sd
);
266 memcpy(buf
, buffer
, sizeof(buffer
));
267 err
= usb_control_msg(gspca_dev
->dev
,
268 usb_sndctrlpipe(gspca_dev
->dev
, 0),
269 0x04, 0x40, 0x19, 0x0000, buf
,
270 sizeof(buffer
), M5602_URB_MSG_TIMEOUT
);
272 PDEBUG(D_STREAM
, "Transfer started");
273 return (err
< 0) ? err
: 0;
276 static void m5602_urb_complete(struct gspca_dev
*gspca_dev
,
279 struct sd
*sd
= (struct sd
*) gspca_dev
;
282 PDEBUG(D_PACK
, "Packet is less than 6 bytes");
286 /* Frame delimiter: ff xx xx xx ff ff */
287 if (data
[0] == 0xff && data
[4] == 0xff && data
[5] == 0xff &&
288 data
[2] != sd
->frame_id
) {
289 PDEBUG(D_FRAM
, "Frame delimiter detected");
290 sd
->frame_id
= data
[2];
292 /* Remove the extra fluff appended on each header */
296 /* Complete the last frame (if any) */
297 gspca_frame_add(gspca_dev
, LAST_PACKET
,
301 /* Create a new frame */
302 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
304 PDEBUG(D_FRAM
, "Starting new frame %d",
310 cur_frame_len
= gspca_dev
->image_len
;
311 /* Remove urb header */
315 if (cur_frame_len
+ len
<= gspca_dev
->frsz
) {
316 PDEBUG(D_FRAM
, "Continuing frame %d copying %d bytes",
317 sd
->frame_count
, len
);
319 gspca_frame_add(gspca_dev
, INTER_PACKET
,
322 /* Add the remaining data up to frame size */
323 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
,
324 gspca_dev
->frsz
- cur_frame_len
);
329 static void m5602_stop_transfer(struct gspca_dev
*gspca_dev
)
331 struct sd
*sd
= (struct sd
*) gspca_dev
;
333 /* Run the sensor specific end transfer sequence */
334 if (sd
->sensor
->stop
)
335 sd
->sensor
->stop(sd
);
338 /* sub-driver description, the ctrl and nctrl is filled at probe time */
339 static struct sd_desc sd_desc
= {
341 .config
= m5602_configure
,
343 .start
= m5602_start_transfer
,
344 .stopN
= m5602_stop_transfer
,
345 .pkt_scan
= m5602_urb_complete
348 /* this function is called at probe time */
349 static int m5602_configure(struct gspca_dev
*gspca_dev
,
350 const struct usb_device_id
*id
)
352 struct sd
*sd
= (struct sd
*) gspca_dev
;
356 cam
= &gspca_dev
->cam
;
360 m5602_dump_bridge(sd
);
363 err
= m5602_probe_sensor(sd
);
370 PDEBUG(D_ERR
, "ALi m5602 webcam failed");
371 cam
->cam_mode
= NULL
;
377 static int m5602_probe(struct usb_interface
*intf
,
378 const struct usb_device_id
*id
)
380 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
384 static void m5602_disconnect(struct usb_interface
*intf
)
386 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
387 struct sd
*sd
= (struct sd
*) gspca_dev
;
389 if (sd
->sensor
->disconnect
)
390 sd
->sensor
->disconnect(sd
);
392 gspca_disconnect(intf
);
395 static struct usb_driver sd_driver
= {
397 .id_table
= m5602_table
,
398 .probe
= m5602_probe
,
400 .suspend
= gspca_suspend
,
401 .resume
= gspca_resume
,
403 .disconnect
= m5602_disconnect
406 /* -- module insert / remove -- */
407 static int __init
mod_m5602_init(void)
409 return usb_register(&sd_driver
);
412 static void __exit
mod_m5602_exit(void)
414 usb_deregister(&sd_driver
);
417 module_init(mod_m5602_init
);
418 module_exit(mod_m5602_exit
);
420 MODULE_AUTHOR(DRIVER_AUTHOR
);
421 MODULE_DESCRIPTION(DRIVER_DESC
);
422 MODULE_LICENSE("GPL");
423 module_param(force_sensor
, int, S_IRUGO
| S_IWUSR
);
424 MODULE_PARM_DESC(force_sensor
,
425 "forces detection of a sensor, "
426 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, "
427 "4 = MT9M111, 5 = PO1030, 6 = OV7660");
429 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
430 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
432 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
433 MODULE_PARM_DESC(dump_sensor
, "Dumps all usb sensor registers "
434 "at startup providing a sensor is found");