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 */
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 /* Dump all the registers of the m5602 bridge,
84 unfortunately this breaks the camera until it's power cycled */
85 static void m5602_dump_bridge(struct sd *sd)
88 for (i = 0; i < 0x80; i++) {
89 unsigned char val = 0;
90 m5602_read_bridge(sd, i, &val);
91 info("ALi m5602 address 0x%x contains 0x%x", i, val);
93 info("Warning: The ALi m5602 webcam probably won't work "
94 "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(D_CONF, "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;
151 /* Send start command to the camera */
152 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
153 memcpy(buf, buffer, sizeof(buffer));
154 err = usb_control_msg(gspca_dev->dev,
155 usb_sndctrlpipe(gspca_dev->dev, 0),
156 0x04, 0x40, 0x19, 0x0000, buf,
157 4, M5602_URB_MSG_TIMEOUT);
159 PDEBUG(D_STREAM, "Transfer started");
160 return (err < 0) ? err : 0;
163 static void m5602_urb_complete(struct gspca_dev *gspca_dev,
164 struct gspca_frame *frame,
167 struct sd *sd = (struct sd *) gspca_dev;
170 PDEBUG(D_PACK, "Packet is less than 6 bytes");
174 /* Frame delimiter: ff xx xx xx ff ff */
175 if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff &&
176 data[2] != sd->frame_id) {
177 PDEBUG(D_FRAM, "Frame delimiter detected");
178 sd->frame_id = data[2];
180 /* Remove the extra fluff appended on each header */
184 /* Complete the last frame (if any) */
185 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
189 /* Create a new frame */
190 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
192 PDEBUG(D_FRAM, "Starting new frame %d",
196 int cur_frame_len = frame->data_end - frame->data;
198 /* Remove urb header */
202 if (cur_frame_len + len <= frame->v4l2_buf.length) {
203 PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes",
204 sd->frame_count, len);
206 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
208 } else if (frame->v4l2_buf.length - cur_frame_len > 0) {
209 /* Add the remaining data up to frame size */
210 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data,
211 frame->v4l2_buf.length - cur_frame_len);
216 static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
218 /* Is there are a command to stop a data transfer? */
221 /* sub-driver description, the ctrl and nctrl is filled at probe time */
222 static struct sd_desc sd_desc = {
224 .config = m5602_configure,
226 .start = m5602_start_transfer,
227 .stopN = m5602_stop_transfer,
228 .pkt_scan = m5602_urb_complete
231 /* this function is called at probe time */
232 static int m5602_configure(struct gspca_dev *gspca_dev,
233 const struct usb_device_id *id)
235 struct sd *sd = (struct sd *) gspca_dev;
239 cam = &gspca_dev->cam;
240 cam->epaddr = M5602_ISOC_ENDPOINT_ADDR;
244 m5602_dump_bridge(sd);
247 err = m5602_probe_sensor(sd);
254 PDEBUG(D_ERR, "ALi m5602 webcam failed");
255 cam->cam_mode = NULL;
261 static int m5602_probe(struct usb_interface *intf,
262 const struct usb_device_id *id)
264 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
268 static struct usb_driver sd_driver = {
270 .id_table = m5602_table,
271 .probe = m5602_probe,
273 .suspend = gspca_suspend,
274 .resume = gspca_resume,
276 .disconnect = gspca_disconnect
279 /* -- module insert / remove -- */
280 static int __init mod_m5602_init(void)
282 if (usb_register(&sd_driver) < 0)
284 PDEBUG(D_PROBE, "registered");
287 static void __exit mod_m5602_exit(void)
289 usb_deregister(&sd_driver);
290 PDEBUG(D_PROBE, "deregistered");
293 module_init(mod_m5602_init);
294 module_exit(mod_m5602_exit);
296 MODULE_AUTHOR(DRIVER_AUTHOR);
297 MODULE_DESCRIPTION(DRIVER_DESC);
298 MODULE_LICENSE("GPL");
299 module_param(force_sensor, int, S_IRUGO | S_IWUSR);
300 MODULE_PARM_DESC(force_sensor,
301 "force detection of sensor, "
302 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030");
304 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
305 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
307 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
308 MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
309 "at startup providing a sensor is found");