2 * USB Driver for ALi m5602 based webcams
4 * Copyright (C) 2008 Erik Andren
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 */
29 unsigned int m5602_debug;
31 static const __devinitdata 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, 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(DBG_TRACE, "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 upon success instead*/
56 return (err < 0) ? err : 0;
59 /* Writes a byte to to the m5602 */
60 int m5602_write_bridge(struct sd *sd, u8 address, u8 i2c_data)
63 struct usb_device *udev = sd->gspca_dev.dev;
64 __u8 *buf = sd->gspca_dev.usb_buf;
66 PDEBUG(DBG_TRACE, "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 upon success instead */
81 return (err < 0) ? err : 0;
84 /* Dump all the registers of the m5602 bridge,
85 unfortunately this breaks the camera until it's power cycled */
86 static void m5602_dump_bridge(struct sd *sd)
89 for (i = 0; i < 0x80; i++) {
90 unsigned char val = 0;
91 m5602_read_bridge(sd, i, &val);
92 info("ALi m5602 address 0x%x contains 0x%x", i, val);
94 info("Warning: The camera probably won't work until it's power cycled");
97 static int m5602_probe_sensor(struct sd *sd)
100 sd->sensor = &po1030;
101 if (!sd->sensor->probe(sd))
104 /* Try the mt9m111 sensor */
105 sd->sensor = &mt9m111;
106 if (!sd->sensor->probe(sd))
110 sd->sensor = &s5k4aa;
111 if (!sd->sensor->probe(sd))
115 sd->sensor = &ov9650;
116 if (!sd->sensor->probe(sd))
120 sd->sensor = &s5k83a;
121 if (!sd->sensor->probe(sd))
124 /* More sensor probe function goes here */
125 info("Failed to find a sensor");
130 static int m5602_configure(struct gspca_dev *gspca_dev,
131 const struct usb_device_id *id);
133 static int m5602_init(struct gspca_dev *gspca_dev)
135 struct sd *sd = (struct sd *) gspca_dev;
138 PDEBUG(DBG_TRACE, "Initializing ALi m5602 webcam");
139 /* Run the init sequence */
140 err = sd->sensor->init(sd);
145 static int m5602_start_transfer(struct gspca_dev *gspca_dev)
147 struct sd *sd = (struct sd *) gspca_dev;
148 __u8 *buf = sd->gspca_dev.usb_buf;
150 /* Send start command to the camera */
151 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
152 memcpy(buf, buffer, sizeof(buffer));
153 usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
154 0x04, 0x40, 0x19, 0x0000, buf,
155 4, M5602_URB_MSG_TIMEOUT);
157 PDEBUG(DBG_V4L2, "Transfer started");
161 static void m5602_urb_complete(struct gspca_dev *gspca_dev,
162 struct gspca_frame *frame,
165 struct sd *sd = (struct sd *) gspca_dev;
168 PDEBUG(DBG_DATA, "Packet is less than 6 bytes");
172 /* Frame delimiter: ff xx xx xx ff ff */
173 if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff &&
174 data[2] != sd->frame_id) {
175 PDEBUG(DBG_DATA, "Frame delimiter detected");
176 sd->frame_id = data[2];
178 /* Remove the extra fluff appended on each header */
182 /* Complete the last frame (if any) */
183 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
187 /* Create a new frame */
188 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
190 PDEBUG(DBG_V4L2, "Starting new frame %d",
194 int cur_frame_len = frame->data_end - frame->data;
196 /* Remove urb header */
200 if (cur_frame_len + len <= frame->v4l2_buf.length) {
201 PDEBUG(DBG_DATA, "Continuing frame %d copying %d bytes",
202 sd->frame_count, len);
204 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
206 } else if (frame->v4l2_buf.length - cur_frame_len > 0) {
207 /* Add the remaining data up to frame size */
208 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data,
209 frame->v4l2_buf.length - cur_frame_len);
214 static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
216 /* Is there are a command to stop a data transfer? */
219 /* sub-driver description, the ctrl and nctrl is filled at probe time */
220 static struct sd_desc sd_desc = {
222 .config = m5602_configure,
224 .start = m5602_start_transfer,
225 .stopN = m5602_stop_transfer,
226 .pkt_scan = m5602_urb_complete
229 /* this function is called at probe time */
230 static int m5602_configure(struct gspca_dev *gspca_dev,
231 const struct usb_device_id *id)
233 struct sd *sd = (struct sd *) gspca_dev;
237 PDEBUG(DBG_GSPCA, "m5602_configure start");
239 cam = &gspca_dev->cam;
240 cam->epaddr = M5602_ISOC_ENDPOINT_ADDR;
244 m5602_dump_bridge(sd);
247 err = m5602_probe_sensor(sd);
251 PDEBUG(DBG_GSPCA, "m5602_configure end");
255 PDEBUG(DBG_GSPCA, "m5602_configure failed");
256 cam->cam_mode = NULL;
262 static int m5602_probe(struct usb_interface *intf,
263 const struct usb_device_id *id)
265 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
269 static struct usb_driver sd_driver = {
271 .id_table = m5602_table,
272 .probe = m5602_probe,
274 .suspend = gspca_suspend,
275 .resume = gspca_resume,
277 .disconnect = gspca_disconnect
280 /* -- module insert / remove -- */
281 static int __init mod_m5602_init(void)
283 if (usb_register(&sd_driver) < 0)
285 PDEBUG(D_PROBE, "m5602 module registered");
288 static void __exit mod_m5602_exit(void)
290 usb_deregister(&sd_driver);
291 PDEBUG(D_PROBE, "m5602 module deregistered");
294 module_init(mod_m5602_init);
295 module_exit(mod_m5602_exit);
297 MODULE_AUTHOR(DRIVER_AUTHOR);
298 MODULE_DESCRIPTION(DRIVER_DESC);
299 MODULE_LICENSE("GPL");
300 module_param_named(debug, m5602_debug, int, S_IRUGO | S_IWUSR);
301 MODULE_PARM_DESC(debug, "toggles debug on/off");
303 module_param(force_sensor, int, S_IRUGO | S_IWUSR);
304 MODULE_PARM_DESC(force_sensor,
305 "force detection of sensor, "
306 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030");
308 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
309 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
311 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
312 MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
313 "at startup providing a sensor is found");