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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "m5602_ov9650.h"
22 #include "m5602_ov7660.h"
23 #include "m5602_mt9m111.h"
24 #include "m5602_po1030.h"
25 #include "m5602_s5k83a.h"
26 #include "m5602_s5k4aa.h"
28 /* Kernel module parameters */
30 static bool dump_bridge
;
33 static const struct usb_device_id m5602_table
[] = {
34 {USB_DEVICE(0x0402, 0x5602)},
38 MODULE_DEVICE_TABLE(usb
, m5602_table
);
40 /* A skeleton used for sending messages to the sensor */
41 static const unsigned char sensor_urb_skeleton
[] = {
42 0x23, M5602_XB_GPIO_EN_H
, 0x81, 0x06,
43 0x23, M5602_XB_MISC_CTRL
, 0x81, 0x80,
44 0x13, M5602_XB_I2C_DEV_ADDR
, 0x81, 0x00,
45 0x13, M5602_XB_I2C_REG_ADDR
, 0x81, 0x00,
46 0x13, M5602_XB_I2C_DATA
, 0x81, 0x00,
47 0x13, M5602_XB_I2C_CTRL
, 0x81, 0x11
50 /* A skeleton used for sending messages to the m5602 bridge */
51 static const unsigned char bridge_urb_skeleton
[] = {
52 0x13, 0x00, 0x81, 0x00
55 /* Reads a byte from the m5602 */
56 int m5602_read_bridge(struct sd
*sd
, const u8 address
, u8
*i2c_data
)
59 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
60 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
61 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
63 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
65 0x8100 + address
, buf
,
66 1, M5602_URB_MSG_TIMEOUT
);
69 gspca_dbg(gspca_dev
, D_CONF
, "Reading bridge register 0x%x containing 0x%x\n",
72 /* usb_control_msg(...) returns the number of bytes sent upon success,
73 mask that and return zero instead*/
74 return (err
< 0) ? err
: 0;
77 /* Writes a byte to the m5602 */
78 int m5602_write_bridge(struct sd
*sd
, const u8 address
, const u8 i2c_data
)
81 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
82 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
83 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
85 gspca_dbg(gspca_dev
, D_CONF
, "Writing bridge register 0x%x with 0x%x\n",
88 memcpy(buf
, bridge_urb_skeleton
,
89 sizeof(bridge_urb_skeleton
));
93 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
96 4, M5602_URB_MSG_TIMEOUT
);
98 /* usb_control_msg(...) returns the number of bytes sent upon success,
99 mask that and return zero instead */
100 return (err
< 0) ? err
: 0;
103 static int m5602_wait_for_i2c(struct sd
*sd
)
109 err
= m5602_read_bridge(sd
, M5602_XB_I2C_STATUS
, &data
);
110 } while ((data
& I2C_BUSY
) && !err
);
114 int m5602_read_sensor(struct sd
*sd
, const u8 address
,
115 u8
*i2c_data
, const u8 len
)
118 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
120 if (!len
|| len
> sd
->sensor
->i2c_regW
)
123 err
= m5602_wait_for_i2c(sd
);
127 err
= m5602_write_bridge(sd
, M5602_XB_I2C_DEV_ADDR
,
128 sd
->sensor
->i2c_slave_id
);
132 err
= m5602_write_bridge(sd
, M5602_XB_I2C_REG_ADDR
, address
);
136 /* Sensors with registers that are of only
137 one byte width are differently read */
139 /* FIXME: This works with the ov9650, but has issues with the po1030 */
140 if (sd
->sensor
->i2c_regW
== 1) {
141 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 1);
145 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x08);
147 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x18 + len
);
150 for (i
= 0; (i
< len
) && !err
; i
++) {
151 err
= m5602_wait_for_i2c(sd
);
155 err
= m5602_read_bridge(sd
, M5602_XB_I2C_DATA
, &(i2c_data
[i
]));
157 gspca_dbg(gspca_dev
, D_CONF
, "Reading sensor register 0x%x containing 0x%x\n",
163 int m5602_write_sensor(struct sd
*sd
, const u8 address
,
164 u8
*i2c_data
, const u8 len
)
168 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
169 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
170 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
172 /* No sensor with a data width larger than 16 bits has yet been seen */
173 if (len
> sd
->sensor
->i2c_regW
|| !len
)
176 memcpy(buf
, sensor_urb_skeleton
,
177 sizeof(sensor_urb_skeleton
));
179 buf
[11] = sd
->sensor
->i2c_slave_id
;
182 /* Special case larger sensor writes */
185 /* Copy a four byte write sequence for each byte to be written to */
186 for (i
= 0; i
< len
; i
++) {
187 memcpy(p
, sensor_urb_skeleton
+ 16, 4);
190 gspca_dbg(gspca_dev
, D_CONF
, "Writing sensor register 0x%x with 0x%x\n",
191 address
, i2c_data
[i
]);
194 /* Copy the tailer */
195 memcpy(p
, sensor_urb_skeleton
+ 20, 4);
197 /* Set the total length */
200 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
203 20 + len
* 4, M5602_URB_MSG_TIMEOUT
);
205 return (err
< 0) ? err
: 0;
208 /* Dump all the registers of the m5602 bridge,
209 unfortunately this breaks the camera until it's power cycled */
210 static void m5602_dump_bridge(struct sd
*sd
)
213 for (i
= 0; i
< 0x80; i
++) {
214 unsigned char val
= 0;
215 m5602_read_bridge(sd
, i
, &val
);
216 pr_info("ALi m5602 address 0x%x contains 0x%x\n", i
, val
);
218 pr_info("Warning: The ALi m5602 webcam probably won't work until it's power cycled\n");
221 static int m5602_probe_sensor(struct sd
*sd
)
224 sd
->sensor
= &po1030
;
225 if (!sd
->sensor
->probe(sd
))
228 /* Try the mt9m111 sensor */
229 sd
->sensor
= &mt9m111
;
230 if (!sd
->sensor
->probe(sd
))
234 sd
->sensor
= &s5k4aa
;
235 if (!sd
->sensor
->probe(sd
))
239 sd
->sensor
= &ov9650
;
240 if (!sd
->sensor
->probe(sd
))
244 sd
->sensor
= &ov7660
;
245 if (!sd
->sensor
->probe(sd
))
249 sd
->sensor
= &s5k83a
;
250 if (!sd
->sensor
->probe(sd
))
253 /* More sensor probe function goes here */
254 pr_info("Failed to find a sensor\n");
259 static int m5602_configure(struct gspca_dev
*gspca_dev
,
260 const struct usb_device_id
*id
);
262 static int m5602_init(struct gspca_dev
*gspca_dev
)
264 struct sd
*sd
= (struct sd
*) gspca_dev
;
267 gspca_dbg(gspca_dev
, D_CONF
, "Initializing ALi m5602 webcam\n");
268 /* Run the init sequence */
269 err
= sd
->sensor
->init(sd
);
274 static int m5602_init_controls(struct gspca_dev
*gspca_dev
)
276 struct sd
*sd
= (struct sd
*) gspca_dev
;
278 if (!sd
->sensor
->init_controls
)
281 return sd
->sensor
->init_controls(sd
);
284 static int m5602_start_transfer(struct gspca_dev
*gspca_dev
)
286 struct sd
*sd
= (struct sd
*) gspca_dev
;
287 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
290 /* Send start command to the camera */
291 const u8 buffer
[4] = {0x13, 0xf9, 0x0f, 0x01};
293 if (sd
->sensor
->start
)
294 sd
->sensor
->start(sd
);
296 memcpy(buf
, buffer
, sizeof(buffer
));
297 err
= usb_control_msg(gspca_dev
->dev
,
298 usb_sndctrlpipe(gspca_dev
->dev
, 0),
299 0x04, 0x40, 0x19, 0x0000, buf
,
300 sizeof(buffer
), M5602_URB_MSG_TIMEOUT
);
302 gspca_dbg(gspca_dev
, D_STREAM
, "Transfer started\n");
303 return (err
< 0) ? err
: 0;
306 static void m5602_urb_complete(struct gspca_dev
*gspca_dev
,
309 struct sd
*sd
= (struct sd
*) gspca_dev
;
312 gspca_dbg(gspca_dev
, D_PACK
, "Packet is less than 6 bytes\n");
316 /* Frame delimiter: ff xx xx xx ff ff */
317 if (data
[0] == 0xff && data
[4] == 0xff && data
[5] == 0xff &&
318 data
[2] != sd
->frame_id
) {
319 gspca_dbg(gspca_dev
, D_FRAM
, "Frame delimiter detected\n");
320 sd
->frame_id
= data
[2];
322 /* Remove the extra fluff appended on each header */
326 /* Complete the last frame (if any) */
327 gspca_frame_add(gspca_dev
, LAST_PACKET
,
331 /* Create a new frame */
332 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
334 gspca_dbg(gspca_dev
, D_FRAM
, "Starting new frame %d\n",
340 cur_frame_len
= gspca_dev
->image_len
;
341 /* Remove urb header */
345 if (cur_frame_len
+ len
<= gspca_dev
->pixfmt
.sizeimage
) {
346 gspca_dbg(gspca_dev
, D_FRAM
, "Continuing frame %d copying %d bytes\n",
347 sd
->frame_count
, len
);
349 gspca_frame_add(gspca_dev
, INTER_PACKET
,
352 /* Add the remaining data up to frame size */
353 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
,
354 gspca_dev
->pixfmt
.sizeimage
- cur_frame_len
);
359 static void m5602_stop_transfer(struct gspca_dev
*gspca_dev
)
361 struct sd
*sd
= (struct sd
*) gspca_dev
;
363 /* Run the sensor specific end transfer sequence */
364 if (sd
->sensor
->stop
)
365 sd
->sensor
->stop(sd
);
368 /* sub-driver description */
369 static const struct sd_desc sd_desc
= {
371 .config
= m5602_configure
,
373 .init_controls
= m5602_init_controls
,
374 .start
= m5602_start_transfer
,
375 .stopN
= m5602_stop_transfer
,
376 .pkt_scan
= m5602_urb_complete
379 /* this function is called at probe time */
380 static int m5602_configure(struct gspca_dev
*gspca_dev
,
381 const struct usb_device_id
*id
)
383 struct sd
*sd
= (struct sd
*) gspca_dev
;
387 cam
= &gspca_dev
->cam
;
390 m5602_dump_bridge(sd
);
393 err
= m5602_probe_sensor(sd
);
400 gspca_err(gspca_dev
, "ALi m5602 webcam failed\n");
401 cam
->cam_mode
= NULL
;
407 static int m5602_probe(struct usb_interface
*intf
,
408 const struct usb_device_id
*id
)
410 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
414 static void m5602_disconnect(struct usb_interface
*intf
)
416 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
417 struct sd
*sd
= (struct sd
*) gspca_dev
;
419 if (sd
->sensor
->disconnect
)
420 sd
->sensor
->disconnect(sd
);
422 gspca_disconnect(intf
);
425 static struct usb_driver sd_driver
= {
427 .id_table
= m5602_table
,
428 .probe
= m5602_probe
,
430 .suspend
= gspca_suspend
,
431 .resume
= gspca_resume
,
432 .reset_resume
= gspca_resume
,
434 .disconnect
= m5602_disconnect
437 module_usb_driver(sd_driver
);
439 MODULE_AUTHOR(DRIVER_AUTHOR
);
440 MODULE_DESCRIPTION(DRIVER_DESC
);
441 MODULE_LICENSE("GPL");
442 module_param(force_sensor
, int, S_IRUGO
| S_IWUSR
);
443 MODULE_PARM_DESC(force_sensor
,
444 "forces detection of a sensor, 1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030, 6 = OV7660");
446 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
447 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
449 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
450 MODULE_PARM_DESC(dump_sensor
, "Dumps all usb sensor registers at startup providing a sensor is found");