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_mt9m111.h"
21 #include "m5602_po1030.h"
22 #include "m5602_s5k83a.h"
23 #include "m5602_s5k4aa.h"
25 /* Kernel module parameters */
27 static int dump_bridge;
30 static const __devinitdata struct usb_device_id m5602_table[] = {
31 {USB_DEVICE(0x0402, 0x5602)},
35 MODULE_DEVICE_TABLE(usb, m5602_table);
37 /* Reads a byte from the m5602 */
38 int m5602_read_bridge(struct sd *sd, u8 address, u8 *i2c_data)
41 struct usb_device *udev = sd->gspca_dev.dev;
42 __u8 *buf = sd->gspca_dev.usb_buf;
44 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
46 0x8100 + address, buf,
47 1, M5602_URB_MSG_TIMEOUT);
50 PDEBUG(D_CONF, "Reading bridge register 0x%x containing 0x%x",
53 /* usb_control_msg(...) returns the number of bytes sent upon success,
54 mask that and return zero instead*/
55 return (err < 0) ? err : 0;
58 /* Writes a byte to to the m5602 */
59 int m5602_write_bridge(struct sd *sd, u8 address, u8 i2c_data)
62 struct usb_device *udev = sd->gspca_dev.dev;
63 __u8 *buf = sd->gspca_dev.usb_buf;
65 PDEBUG(D_CONF, "Writing bridge register 0x%x with 0x%x",
68 memcpy(buf, bridge_urb_skeleton,
69 sizeof(bridge_urb_skeleton));
73 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
76 4, M5602_URB_MSG_TIMEOUT);
78 /* usb_control_msg(...) returns the number of bytes sent upon success,
79 mask that and return zero instead */
80 return (err < 0) ? err : 0;
83 int m5602_read_sensor(struct sd *sd, const u8 address,
84 u8 *i2c_data, const u8 len)
88 if (!len || len > sd->sensor->i2c_regW)
92 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
93 } while ((*i2c_data & I2C_BUSY) && !err);
97 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
98 sd->sensor->i2c_slave_id);
102 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
106 if (sd->sensor->i2c_regW == 1) {
107 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, len);
111 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
115 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len);
120 for (i = 0; (i < len) && !err; i++) {
121 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
123 PDEBUG(D_CONF, "Reading sensor register "
124 "0x%x containing 0x%x ", address, *i2c_data);
129 int m5602_write_sensor(struct sd *sd, const u8 address,
130 u8 *i2c_data, const u8 len)
134 struct usb_device *udev = sd->gspca_dev.dev;
135 __u8 *buf = sd->gspca_dev.usb_buf;
137 /* No sensor with a data width larger than 16 bits has yet been seen */
138 if (len > sd->sensor->i2c_regW || !len)
141 memcpy(buf, sensor_urb_skeleton,
142 sizeof(sensor_urb_skeleton));
144 buf[11] = sd->sensor->i2c_slave_id;
147 /* Special case larger sensor writes */
150 /* Copy a four byte write sequence for each byte to be written to */
151 for (i = 0; i < len; i++) {
152 memcpy(p, sensor_urb_skeleton + 16, 4);
155 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
156 address, i2c_data[i]);
159 /* Copy the tailer */
160 memcpy(p, sensor_urb_skeleton + 20, 4);
162 /* Set the total length */
165 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
168 20 + len * 4, M5602_URB_MSG_TIMEOUT);
170 return (err < 0) ? err : 0;
173 /* Dump all the registers of the m5602 bridge,
174 unfortunately this breaks the camera until it's power cycled */
175 static void m5602_dump_bridge(struct sd *sd)
178 for (i = 0; i < 0x80; i++) {
179 unsigned char val = 0;
180 m5602_read_bridge(sd, i, &val);
181 info("ALi m5602 address 0x%x contains 0x%x", i, val);
183 info("Warning: The ALi m5602 webcam probably won't work "
184 "until it's power cycled");
187 static int m5602_probe_sensor(struct sd *sd)
190 sd->sensor = &po1030;
191 if (!sd->sensor->probe(sd))
194 /* Try the mt9m111 sensor */
195 sd->sensor = &mt9m111;
196 if (!sd->sensor->probe(sd))
200 sd->sensor = &s5k4aa;
201 if (!sd->sensor->probe(sd))
205 sd->sensor = &ov9650;
206 if (!sd->sensor->probe(sd))
210 sd->sensor = &s5k83a;
211 if (!sd->sensor->probe(sd))
214 /* More sensor probe function goes here */
215 info("Failed to find a sensor");
220 static int m5602_configure(struct gspca_dev *gspca_dev,
221 const struct usb_device_id *id);
223 static int m5602_init(struct gspca_dev *gspca_dev)
225 struct sd *sd = (struct sd *) gspca_dev;
228 PDEBUG(D_CONF, "Initializing ALi m5602 webcam");
229 /* Run the init sequence */
230 err = sd->sensor->init(sd);
235 static int m5602_start_transfer(struct gspca_dev *gspca_dev)
237 struct sd *sd = (struct sd *) gspca_dev;
238 __u8 *buf = sd->gspca_dev.usb_buf;
241 /* Send start command to the camera */
242 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
244 if (sd->sensor->start)
245 sd->sensor->start(sd);
247 memcpy(buf, buffer, sizeof(buffer));
248 err = usb_control_msg(gspca_dev->dev,
249 usb_sndctrlpipe(gspca_dev->dev, 0),
250 0x04, 0x40, 0x19, 0x0000, buf,
251 sizeof(buffer), M5602_URB_MSG_TIMEOUT);
253 PDEBUG(D_STREAM, "Transfer started");
254 return (err < 0) ? err : 0;
257 static void m5602_urb_complete(struct gspca_dev *gspca_dev,
258 struct gspca_frame *frame,
261 struct sd *sd = (struct sd *) gspca_dev;
264 PDEBUG(D_PACK, "Packet is less than 6 bytes");
268 /* Frame delimiter: ff xx xx xx ff ff */
269 if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff &&
270 data[2] != sd->frame_id) {
271 PDEBUG(D_FRAM, "Frame delimiter detected");
272 sd->frame_id = data[2];
274 /* Remove the extra fluff appended on each header */
278 /* Complete the last frame (if any) */
279 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
283 /* Create a new frame */
284 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
286 PDEBUG(D_FRAM, "Starting new frame %d",
290 int cur_frame_len = frame->data_end - frame->data;
292 /* Remove urb header */
296 if (cur_frame_len + len <= frame->v4l2_buf.length) {
297 PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes",
298 sd->frame_count, len);
300 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
302 } else if (frame->v4l2_buf.length - cur_frame_len > 0) {
303 /* Add the remaining data up to frame size */
304 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data,
305 frame->v4l2_buf.length - cur_frame_len);
310 static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
312 struct sd *sd = (struct sd *) gspca_dev;
314 /* Run the sensor specific end transfer sequence */
315 if (sd->sensor->stop)
316 sd->sensor->stop(sd);
319 /* sub-driver description, the ctrl and nctrl is filled at probe time */
320 static struct sd_desc sd_desc = {
322 .config = m5602_configure,
324 .start = m5602_start_transfer,
325 .stopN = m5602_stop_transfer,
326 .pkt_scan = m5602_urb_complete
329 /* this function is called at probe time */
330 static int m5602_configure(struct gspca_dev *gspca_dev,
331 const struct usb_device_id *id)
333 struct sd *sd = (struct sd *) gspca_dev;
337 cam = &gspca_dev->cam;
341 m5602_dump_bridge(sd);
344 err = m5602_probe_sensor(sd);
351 PDEBUG(D_ERR, "ALi m5602 webcam failed");
352 cam->cam_mode = NULL;
358 static int m5602_probe(struct usb_interface *intf,
359 const struct usb_device_id *id)
361 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
365 void m5602_disconnect(struct usb_interface *intf)
367 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
368 struct sd *sd = (struct sd *) gspca_dev;
370 if (sd->sensor->disconnect)
371 sd->sensor->disconnect(sd);
373 gspca_disconnect(intf);
376 static struct usb_driver sd_driver = {
378 .id_table = m5602_table,
379 .probe = m5602_probe,
381 .suspend = gspca_suspend,
382 .resume = gspca_resume,
384 .disconnect = m5602_disconnect
387 /* -- module insert / remove -- */
388 static int __init mod_m5602_init(void)
391 ret = usb_register(&sd_driver);
394 PDEBUG(D_PROBE, "registered");
398 static void __exit mod_m5602_exit(void)
400 usb_deregister(&sd_driver);
401 PDEBUG(D_PROBE, "deregistered");
404 module_init(mod_m5602_init);
405 module_exit(mod_m5602_exit);
407 MODULE_AUTHOR(DRIVER_AUTHOR);
408 MODULE_DESCRIPTION(DRIVER_DESC);
409 MODULE_LICENSE("GPL");
410 module_param(force_sensor, int, S_IRUGO | S_IWUSR);
411 MODULE_PARM_DESC(force_sensor,
412 "force detection of sensor, "
413 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030");
415 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
416 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
418 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
419 MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
420 "at startup providing a sensor is found");