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 upon success 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 upon success 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);
130 int m5602_write_sensor(struct sd *sd, const u8 address,
131 u8 *i2c_data, const u8 len)
135 struct usb_device *udev = sd->gspca_dev.dev;
136 __u8 *buf = sd->gspca_dev.usb_buf;
138 /* No sensor with a data width larger than 16 bits has yet been seen */
139 if (len > sd->sensor->i2c_regW || !len)
142 memcpy(buf, sensor_urb_skeleton,
143 sizeof(sensor_urb_skeleton));
145 buf[11] = sd->sensor->i2c_slave_id;
148 /* Special case larger sensor writes */
151 /* Copy a four byte write sequence for each byte to be written to */
152 for (i = 0; i < len; i++) {
153 memcpy(p, sensor_urb_skeleton + 16, 4);
156 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
157 address, i2c_data[i]);
160 /* Copy the tailer */
161 memcpy(p, sensor_urb_skeleton + 20, 4);
163 /* Set the total length */
166 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
169 20 + len * 4, M5602_URB_MSG_TIMEOUT);
171 return (err < 0) ? err : 0;
174 /* Dump all the registers of the m5602 bridge,
175 unfortunately this breaks the camera until it's power cycled */
176 static void m5602_dump_bridge(struct sd *sd)
179 for (i = 0; i < 0x80; i++) {
180 unsigned char val = 0;
181 m5602_read_bridge(sd, i, &val);
182 info("ALi m5602 address 0x%x contains 0x%x", i, val);
184 info("Warning: The ALi m5602 webcam probably won't work "
185 "until it's power cycled");
188 static int m5602_probe_sensor(struct sd *sd)
191 sd->sensor = &po1030;
192 if (!sd->sensor->probe(sd))
195 /* Try the mt9m111 sensor */
196 sd->sensor = &mt9m111;
197 if (!sd->sensor->probe(sd))
201 sd->sensor = &s5k4aa;
202 if (!sd->sensor->probe(sd))
206 sd->sensor = &ov9650;
207 if (!sd->sensor->probe(sd))
211 sd->sensor = &s5k83a;
212 if (!sd->sensor->probe(sd))
215 /* More sensor probe function goes here */
216 info("Failed to find a sensor");
221 static int m5602_configure(struct gspca_dev *gspca_dev,
222 const struct usb_device_id *id);
224 static int m5602_init(struct gspca_dev *gspca_dev)
226 struct sd *sd = (struct sd *) gspca_dev;
229 PDEBUG(D_CONF, "Initializing ALi m5602 webcam");
230 /* Run the init sequence */
231 err = sd->sensor->init(sd);
236 static int m5602_start_transfer(struct gspca_dev *gspca_dev)
238 struct sd *sd = (struct sd *) gspca_dev;
239 __u8 *buf = sd->gspca_dev.usb_buf;
242 /* Send start command to the camera */
243 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
245 if (sd->sensor->start)
246 sd->sensor->start(sd);
248 memcpy(buf, buffer, sizeof(buffer));
249 err = usb_control_msg(gspca_dev->dev,
250 usb_sndctrlpipe(gspca_dev->dev, 0),
251 0x04, 0x40, 0x19, 0x0000, buf,
252 sizeof(buffer), M5602_URB_MSG_TIMEOUT);
254 PDEBUG(D_STREAM, "Transfer started");
255 return (err < 0) ? err : 0;
258 static void m5602_urb_complete(struct gspca_dev *gspca_dev,
259 struct gspca_frame *frame,
262 struct sd *sd = (struct sd *) gspca_dev;
265 PDEBUG(D_PACK, "Packet is less than 6 bytes");
269 /* Frame delimiter: ff xx xx xx ff ff */
270 if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff &&
271 data[2] != sd->frame_id) {
272 PDEBUG(D_FRAM, "Frame delimiter detected");
273 sd->frame_id = data[2];
275 /* Remove the extra fluff appended on each header */
279 /* Complete the last frame (if any) */
280 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
284 /* Create a new frame */
285 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
287 PDEBUG(D_FRAM, "Starting new frame %d",
291 int cur_frame_len = frame->data_end - frame->data;
293 /* Remove urb header */
297 if (cur_frame_len + len <= frame->v4l2_buf.length) {
298 PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes",
299 sd->frame_count, len);
301 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
303 } else if (frame->v4l2_buf.length - cur_frame_len > 0) {
304 /* Add the remaining data up to frame size */
305 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data,
306 frame->v4l2_buf.length - cur_frame_len);
311 static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
313 /* Is there are a command to stop a data transfer? */
316 /* sub-driver description, the ctrl and nctrl is filled at probe time */
317 static struct sd_desc sd_desc = {
319 .config = m5602_configure,
321 .start = m5602_start_transfer,
322 .stopN = m5602_stop_transfer,
323 .pkt_scan = m5602_urb_complete
326 /* this function is called at probe time */
327 static int m5602_configure(struct gspca_dev *gspca_dev,
328 const struct usb_device_id *id)
330 struct sd *sd = (struct sd *) gspca_dev;
334 cam = &gspca_dev->cam;
335 cam->epaddr = M5602_ISOC_ENDPOINT_ADDR;
339 m5602_dump_bridge(sd);
342 err = m5602_probe_sensor(sd);
349 PDEBUG(D_ERR, "ALi m5602 webcam failed");
350 cam->cam_mode = NULL;
356 static int m5602_probe(struct usb_interface *intf,
357 const struct usb_device_id *id)
359 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
363 static struct usb_driver sd_driver = {
365 .id_table = m5602_table,
366 .probe = m5602_probe,
368 .suspend = gspca_suspend,
369 .resume = gspca_resume,
371 .disconnect = gspca_disconnect
374 /* -- module insert / remove -- */
375 static int __init mod_m5602_init(void)
377 if (usb_register(&sd_driver) < 0)
379 PDEBUG(D_PROBE, "registered");
383 static void __exit mod_m5602_exit(void)
385 usb_deregister(&sd_driver);
386 PDEBUG(D_PROBE, "deregistered");
389 module_init(mod_m5602_init);
390 module_exit(mod_m5602_exit);
392 MODULE_AUTHOR(DRIVER_AUTHOR);
393 MODULE_DESCRIPTION(DRIVER_DESC);
394 MODULE_LICENSE("GPL");
395 module_param(force_sensor, int, S_IRUGO | S_IWUSR);
396 MODULE_PARM_DESC(force_sensor,
397 "force detection of sensor, "
398 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030");
400 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
401 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
403 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
404 MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
405 "at startup providing a sensor is found");