1 // SPDX-License-Identifier: GPL-2.0-only
3 * USB Driver for ALi m5602 based webcams
5 * Copyright (C) 2008 Erik Andrén
6 * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
7 * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
9 * Portions of code to USB interface and ALi driver software,
10 * Copyright (c) 2006 Willem Duinker
11 * v4l2 interface modeled after the V4L2 driver
12 * for SN9C10x PC Camera Controllers
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include "m5602_ov9650.h"
18 #include "m5602_ov7660.h"
19 #include "m5602_mt9m111.h"
20 #include "m5602_po1030.h"
21 #include "m5602_s5k83a.h"
22 #include "m5602_s5k4aa.h"
24 /* Kernel module parameters */
26 static bool dump_bridge
;
29 static const struct usb_device_id m5602_table
[] = {
30 {USB_DEVICE(0x0402, 0x5602)},
34 MODULE_DEVICE_TABLE(usb
, m5602_table
);
36 /* A skeleton used for sending messages to the sensor */
37 static const unsigned char sensor_urb_skeleton
[] = {
38 0x23, M5602_XB_GPIO_EN_H
, 0x81, 0x06,
39 0x23, M5602_XB_MISC_CTRL
, 0x81, 0x80,
40 0x13, M5602_XB_I2C_DEV_ADDR
, 0x81, 0x00,
41 0x13, M5602_XB_I2C_REG_ADDR
, 0x81, 0x00,
42 0x13, M5602_XB_I2C_DATA
, 0x81, 0x00,
43 0x13, M5602_XB_I2C_CTRL
, 0x81, 0x11
46 /* A skeleton used for sending messages to the m5602 bridge */
47 static const unsigned char bridge_urb_skeleton
[] = {
48 0x13, 0x00, 0x81, 0x00
51 /* Reads a byte from the m5602 */
52 int m5602_read_bridge(struct sd
*sd
, const u8 address
, u8
*i2c_data
)
55 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
56 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
57 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
59 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
61 0x8100 + address
, buf
,
62 1, M5602_URB_MSG_TIMEOUT
);
65 gspca_dbg(gspca_dev
, D_CONF
, "Reading bridge register 0x%x containing 0x%x\n",
68 /* usb_control_msg(...) returns the number of bytes sent upon success,
69 mask that and return zero instead*/
70 return (err
< 0) ? err
: 0;
73 /* Writes a byte to the m5602 */
74 int m5602_write_bridge(struct sd
*sd
, const u8 address
, const u8 i2c_data
)
77 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
78 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
79 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
81 gspca_dbg(gspca_dev
, D_CONF
, "Writing bridge register 0x%x with 0x%x\n",
84 memcpy(buf
, bridge_urb_skeleton
,
85 sizeof(bridge_urb_skeleton
));
89 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
92 4, M5602_URB_MSG_TIMEOUT
);
94 /* usb_control_msg(...) returns the number of bytes sent upon success,
95 mask that and return zero instead */
96 return (err
< 0) ? err
: 0;
99 static int m5602_wait_for_i2c(struct sd
*sd
)
105 err
= m5602_read_bridge(sd
, M5602_XB_I2C_STATUS
, &data
);
106 } while ((data
& I2C_BUSY
) && !err
);
110 int m5602_read_sensor(struct sd
*sd
, const u8 address
,
111 u8
*i2c_data
, const u8 len
)
114 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
116 if (!len
|| len
> sd
->sensor
->i2c_regW
)
119 err
= m5602_wait_for_i2c(sd
);
123 err
= m5602_write_bridge(sd
, M5602_XB_I2C_DEV_ADDR
,
124 sd
->sensor
->i2c_slave_id
);
128 err
= m5602_write_bridge(sd
, M5602_XB_I2C_REG_ADDR
, address
);
132 /* Sensors with registers that are of only
133 one byte width are differently read */
135 /* FIXME: This works with the ov9650, but has issues with the po1030 */
136 if (sd
->sensor
->i2c_regW
== 1) {
137 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 1);
141 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x08);
143 err
= m5602_write_bridge(sd
, M5602_XB_I2C_CTRL
, 0x18 + len
);
146 for (i
= 0; (i
< len
) && !err
; i
++) {
147 err
= m5602_wait_for_i2c(sd
);
151 err
= m5602_read_bridge(sd
, M5602_XB_I2C_DATA
, &(i2c_data
[i
]));
153 gspca_dbg(gspca_dev
, D_CONF
, "Reading sensor register 0x%x containing 0x%x\n",
159 int m5602_write_sensor(struct sd
*sd
, const u8 address
,
160 u8
*i2c_data
, const u8 len
)
164 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) sd
;
165 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
166 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
168 /* No sensor with a data width larger than 16 bits has yet been seen */
169 if (len
> sd
->sensor
->i2c_regW
|| !len
)
172 memcpy(buf
, sensor_urb_skeleton
,
173 sizeof(sensor_urb_skeleton
));
175 buf
[11] = sd
->sensor
->i2c_slave_id
;
178 /* Special case larger sensor writes */
181 /* Copy a four byte write sequence for each byte to be written to */
182 for (i
= 0; i
< len
; i
++) {
183 memcpy(p
, sensor_urb_skeleton
+ 16, 4);
186 gspca_dbg(gspca_dev
, D_CONF
, "Writing sensor register 0x%x with 0x%x\n",
187 address
, i2c_data
[i
]);
190 /* Copy the tailer */
191 memcpy(p
, sensor_urb_skeleton
+ 20, 4);
193 /* Set the total length */
196 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
199 20 + len
* 4, M5602_URB_MSG_TIMEOUT
);
201 return (err
< 0) ? err
: 0;
204 /* Dump all the registers of the m5602 bridge,
205 unfortunately this breaks the camera until it's power cycled */
206 static void m5602_dump_bridge(struct sd
*sd
)
209 for (i
= 0; i
< 0x80; i
++) {
210 unsigned char val
= 0;
211 m5602_read_bridge(sd
, i
, &val
);
212 pr_info("ALi m5602 address 0x%x contains 0x%x\n", i
, val
);
214 pr_info("Warning: The ALi m5602 webcam probably won't work until it's power cycled\n");
217 static int m5602_probe_sensor(struct sd
*sd
)
220 sd
->sensor
= &po1030
;
221 if (!sd
->sensor
->probe(sd
))
224 /* Try the mt9m111 sensor */
225 sd
->sensor
= &mt9m111
;
226 if (!sd
->sensor
->probe(sd
))
230 sd
->sensor
= &s5k4aa
;
231 if (!sd
->sensor
->probe(sd
))
235 sd
->sensor
= &ov9650
;
236 if (!sd
->sensor
->probe(sd
))
240 sd
->sensor
= &ov7660
;
241 if (!sd
->sensor
->probe(sd
))
245 sd
->sensor
= &s5k83a
;
246 if (!sd
->sensor
->probe(sd
))
249 /* More sensor probe function goes here */
250 pr_info("Failed to find a sensor\n");
255 static int m5602_configure(struct gspca_dev
*gspca_dev
,
256 const struct usb_device_id
*id
);
258 static int m5602_init(struct gspca_dev
*gspca_dev
)
260 struct sd
*sd
= (struct sd
*) gspca_dev
;
263 gspca_dbg(gspca_dev
, D_CONF
, "Initializing ALi m5602 webcam\n");
264 /* Run the init sequence */
265 err
= sd
->sensor
->init(sd
);
270 static int m5602_init_controls(struct gspca_dev
*gspca_dev
)
272 struct sd
*sd
= (struct sd
*) gspca_dev
;
274 if (!sd
->sensor
->init_controls
)
277 return sd
->sensor
->init_controls(sd
);
280 static int m5602_start_transfer(struct gspca_dev
*gspca_dev
)
282 struct sd
*sd
= (struct sd
*) gspca_dev
;
283 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
286 /* Send start command to the camera */
287 const u8 buffer
[4] = {0x13, 0xf9, 0x0f, 0x01};
289 if (sd
->sensor
->start
)
290 sd
->sensor
->start(sd
);
292 memcpy(buf
, buffer
, sizeof(buffer
));
293 err
= usb_control_msg(gspca_dev
->dev
,
294 usb_sndctrlpipe(gspca_dev
->dev
, 0),
295 0x04, 0x40, 0x19, 0x0000, buf
,
296 sizeof(buffer
), M5602_URB_MSG_TIMEOUT
);
298 gspca_dbg(gspca_dev
, D_STREAM
, "Transfer started\n");
299 return (err
< 0) ? err
: 0;
302 static void m5602_urb_complete(struct gspca_dev
*gspca_dev
,
305 struct sd
*sd
= (struct sd
*) gspca_dev
;
308 gspca_dbg(gspca_dev
, D_PACK
, "Packet is less than 6 bytes\n");
312 /* Frame delimiter: ff xx xx xx ff ff */
313 if (data
[0] == 0xff && data
[4] == 0xff && data
[5] == 0xff &&
314 data
[2] != sd
->frame_id
) {
315 gspca_dbg(gspca_dev
, D_FRAM
, "Frame delimiter detected\n");
316 sd
->frame_id
= data
[2];
318 /* Remove the extra fluff appended on each header */
322 /* Complete the last frame (if any) */
323 gspca_frame_add(gspca_dev
, LAST_PACKET
,
327 /* Create a new frame */
328 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
330 gspca_dbg(gspca_dev
, D_FRAM
, "Starting new frame %d\n",
336 cur_frame_len
= gspca_dev
->image_len
;
337 /* Remove urb header */
341 if (cur_frame_len
+ len
<= gspca_dev
->pixfmt
.sizeimage
) {
342 gspca_dbg(gspca_dev
, D_FRAM
, "Continuing frame %d copying %d bytes\n",
343 sd
->frame_count
, len
);
345 gspca_frame_add(gspca_dev
, INTER_PACKET
,
348 /* Add the remaining data up to frame size */
349 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
,
350 gspca_dev
->pixfmt
.sizeimage
- cur_frame_len
);
355 static void m5602_stop_transfer(struct gspca_dev
*gspca_dev
)
357 struct sd
*sd
= (struct sd
*) gspca_dev
;
359 /* Run the sensor specific end transfer sequence */
360 if (sd
->sensor
->stop
)
361 sd
->sensor
->stop(sd
);
364 /* sub-driver description */
365 static const struct sd_desc sd_desc
= {
367 .config
= m5602_configure
,
369 .init_controls
= m5602_init_controls
,
370 .start
= m5602_start_transfer
,
371 .stopN
= m5602_stop_transfer
,
372 .pkt_scan
= m5602_urb_complete
375 /* this function is called at probe time */
376 static int m5602_configure(struct gspca_dev
*gspca_dev
,
377 const struct usb_device_id
*id
)
379 struct sd
*sd
= (struct sd
*) gspca_dev
;
383 cam
= &gspca_dev
->cam
;
386 m5602_dump_bridge(sd
);
389 err
= m5602_probe_sensor(sd
);
396 gspca_err(gspca_dev
, "ALi m5602 webcam failed\n");
397 cam
->cam_mode
= NULL
;
403 static int m5602_probe(struct usb_interface
*intf
,
404 const struct usb_device_id
*id
)
406 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
410 static void m5602_disconnect(struct usb_interface
*intf
)
412 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
413 struct sd
*sd
= (struct sd
*) gspca_dev
;
415 if (sd
->sensor
->disconnect
)
416 sd
->sensor
->disconnect(sd
);
418 gspca_disconnect(intf
);
421 static struct usb_driver sd_driver
= {
423 .id_table
= m5602_table
,
424 .probe
= m5602_probe
,
426 .suspend
= gspca_suspend
,
427 .resume
= gspca_resume
,
428 .reset_resume
= gspca_resume
,
430 .disconnect
= m5602_disconnect
433 module_usb_driver(sd_driver
);
435 MODULE_AUTHOR(DRIVER_AUTHOR
);
436 MODULE_DESCRIPTION(DRIVER_DESC
);
437 MODULE_LICENSE("GPL");
438 module_param(force_sensor
, int, S_IRUGO
| S_IWUSR
);
439 MODULE_PARM_DESC(force_sensor
,
440 "forces detection of a sensor, 1 = OV9650, 2 = S5K83A, 3 = S5K4AA, 4 = MT9M111, 5 = PO1030, 6 = OV7660");
442 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
443 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
445 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
446 MODULE_PARM_DESC(dump_sensor
, "Dumps all usb sensor registers at startup providing a sensor is found");