1 /*
2 * USB Driver for ALi m5602 based webcams
3 *
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>
7 *
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
12 *
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.
16 *
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
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"
27
28 /* Kernel module parameters */
29 int force_sensor;
30 static bool dump_bridge;
31 bool dump_sensor;
32
33 static const struct usb_device_id m5602_table[] = {
34 {USB_DEVICE(0x0402, 0x5602)},
35 {}
36 };
37
38 MODULE_DEVICE_TABLE(usb, m5602_table);
39
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
48 };
49
50 /* A skeleton used for sending messages to the m5602 bridge */
51 static const unsigned char bridge_urb_skeleton[] = {
52 0x13, 0x00, 0x81, 0x00
53 };
54
55 /* Reads a byte from the m5602 */
m5602_read_bridge(struct sd * sd,const u8 address,u8 * i2c_data)56 int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data)
57 {
58 int err;
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;
62
63 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
64 0x04, 0xc0, 0x14,
65 0x8100 + address, buf,
66 1, M5602_URB_MSG_TIMEOUT);
67 *i2c_data = buf[0];
68
69 PDEBUG(D_CONF, "Reading bridge register 0x%x containing 0x%x",
70 address, *i2c_data);
71
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;
75 }
76
77 /* Writes a byte to the m5602 */
m5602_write_bridge(struct sd * sd,const u8 address,const u8 i2c_data)78 int m5602_write_bridge(struct sd *sd, const u8 address, const u8 i2c_data)
79 {
80 int err;
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;
84
85 PDEBUG(D_CONF, "Writing bridge register 0x%x with 0x%x",
86 address, i2c_data);
87
88 memcpy(buf, bridge_urb_skeleton,
89 sizeof(bridge_urb_skeleton));
90 buf[1] = address;
91 buf[3] = i2c_data;
92
93 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
94 0x04, 0x40, 0x19,
95 0x0000, buf,
96 4, M5602_URB_MSG_TIMEOUT);
97
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;
101 }
102
m5602_wait_for_i2c(struct sd * sd)103 static int m5602_wait_for_i2c(struct sd *sd)
104 {
105 int err;
106 u8 data;
107
108 do {
109 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, &data);
110 } while ((data & I2C_BUSY) && !err);
111 return err;
112 }
113
m5602_read_sensor(struct sd * sd,const u8 address,u8 * i2c_data,const u8 len)114 int m5602_read_sensor(struct sd *sd, const u8 address,
115 u8 *i2c_data, const u8 len)
116 {
117 int err, i;
118 struct gspca_dev *gspca_dev = (struct gspca_dev *) sd;
119
120 if (!len || len > sd->sensor->i2c_regW)
121 return -EINVAL;
122
123 err = m5602_wait_for_i2c(sd);
124 if (err < 0)
125 return err;
126
127 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
128 sd->sensor->i2c_slave_id);
129 if (err < 0)
130 return err;
131
132 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
133 if (err < 0)
134 return err;
135
136 /* Sensors with registers that are of only
137 one byte width are differently read */
138
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);
142 if (err < 0)
143 return err;
144
145 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
146 } else {
147 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len);
148 }
149
150 for (i = 0; (i < len) && !err; i++) {
151 err = m5602_wait_for_i2c(sd);
152 if (err < 0)
153 return err;
154
155 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
156
157 PDEBUG(D_CONF, "Reading sensor register "
158 "0x%x containing 0x%x ", address, *i2c_data);
159 }
160 return err;
161 }
162
m5602_write_sensor(struct sd * sd,const u8 address,u8 * i2c_data,const u8 len)163 int m5602_write_sensor(struct sd *sd, const u8 address,
164 u8 *i2c_data, const u8 len)
165 {
166 int err, i;
167 u8 *p;
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;
171
172 /* No sensor with a data width larger than 16 bits has yet been seen */
173 if (len > sd->sensor->i2c_regW || !len)
174 return -EINVAL;
175
176 memcpy(buf, sensor_urb_skeleton,
177 sizeof(sensor_urb_skeleton));
178
179 buf[11] = sd->sensor->i2c_slave_id;
180 buf[15] = address;
181
182 /* Special case larger sensor writes */
183 p = buf + 16;
184
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);
188 p[3] = i2c_data[i];
189 p += 4;
190 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
191 address, i2c_data[i]);
192 }
193
194 /* Copy the tailer */
195 memcpy(p, sensor_urb_skeleton + 20, 4);
196
197 /* Set the total length */
198 p[3] = 0x10 + len;
199
200 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
201 0x04, 0x40, 0x19,
202 0x0000, buf,
203 20 + len * 4, M5602_URB_MSG_TIMEOUT);
204
205 return (err < 0) ? err : 0;
206 }
207
208 /* Dump all the registers of the m5602 bridge,
209 unfortunately this breaks the camera until it's power cycled */
m5602_dump_bridge(struct sd * sd)210 static void m5602_dump_bridge(struct sd *sd)
211 {
212 int i;
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);
217 }
218 pr_info("Warning: The ALi m5602 webcam probably won't work until it's power cycled\n");
219 }
220
m5602_probe_sensor(struct sd * sd)221 static int m5602_probe_sensor(struct sd *sd)
222 {
223 /* Try the po1030 */
224 sd->sensor = &po1030;
225 if (!sd->sensor->probe(sd))
226 return 0;
227
228 /* Try the mt9m111 sensor */
229 sd->sensor = &mt9m111;
230 if (!sd->sensor->probe(sd))
231 return 0;
232
233 /* Try the s5k4aa */
234 sd->sensor = &s5k4aa;
235 if (!sd->sensor->probe(sd))
236 return 0;
237
238 /* Try the ov9650 */
239 sd->sensor = &ov9650;
240 if (!sd->sensor->probe(sd))
241 return 0;
242
243 /* Try the ov7660 */
244 sd->sensor = &ov7660;
245 if (!sd->sensor->probe(sd))
246 return 0;
247
248 /* Try the s5k83a */
249 sd->sensor = &s5k83a;
250 if (!sd->sensor->probe(sd))
251 return 0;
252
253 /* More sensor probe function goes here */
254 pr_info("Failed to find a sensor\n");
255 sd->sensor = NULL;
256 return -ENODEV;
257 }
258
259 static int m5602_configure(struct gspca_dev *gspca_dev,
260 const struct usb_device_id *id);
261
m5602_init(struct gspca_dev * gspca_dev)262 static int m5602_init(struct gspca_dev *gspca_dev)
263 {
264 struct sd *sd = (struct sd *) gspca_dev;
265 int err;
266
267 PDEBUG(D_CONF, "Initializing ALi m5602 webcam");
268 /* Run the init sequence */
269 err = sd->sensor->init(sd);
270
271 return err;
272 }
273
m5602_init_controls(struct gspca_dev * gspca_dev)274 static int m5602_init_controls(struct gspca_dev *gspca_dev)
275 {
276 struct sd *sd = (struct sd *) gspca_dev;
277
278 if (!sd->sensor->init_controls)
279 return 0;
280
281 return sd->sensor->init_controls(sd);
282 }
283
m5602_start_transfer(struct gspca_dev * gspca_dev)284 static int m5602_start_transfer(struct gspca_dev *gspca_dev)
285 {
286 struct sd *sd = (struct sd *) gspca_dev;
287 __u8 *buf = sd->gspca_dev.usb_buf;
288 int err;
289
290 /* Send start command to the camera */
291 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
292
293 if (sd->sensor->start)
294 sd->sensor->start(sd);
295
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);
301
302 PDEBUG(D_STREAM, "Transfer started");
303 return (err < 0) ? err : 0;
304 }
305
m5602_urb_complete(struct gspca_dev * gspca_dev,u8 * data,int len)306 static void m5602_urb_complete(struct gspca_dev *gspca_dev,
307 u8 *data, int len)
308 {
309 struct sd *sd = (struct sd *) gspca_dev;
310
311 if (len < 6) {
312 PDEBUG(D_PACK, "Packet is less than 6 bytes");
313 return;
314 }
315
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 PDEBUG(D_FRAM, "Frame delimiter detected");
320 sd->frame_id = data[2];
321
322 /* Remove the extra fluff appended on each header */
323 data += 6;
324 len -= 6;
325
326 /* Complete the last frame (if any) */
327 gspca_frame_add(gspca_dev, LAST_PACKET,
328 NULL, 0);
329 sd->frame_count++;
330
331 /* Create a new frame */
332 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
333
334 PDEBUG(D_FRAM, "Starting new frame %d",
335 sd->frame_count);
336
337 } else {
338 int cur_frame_len;
339
340 cur_frame_len = gspca_dev->image_len;
341 /* Remove urb header */
342 data += 4;
343 len -= 4;
344
345 if (cur_frame_len + len <= gspca_dev->frsz) {
346 PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes",
347 sd->frame_count, len);
348
349 gspca_frame_add(gspca_dev, INTER_PACKET,
350 data, len);
351 } else {
352 /* Add the remaining data up to frame size */
353 gspca_frame_add(gspca_dev, INTER_PACKET, data,
354 gspca_dev->frsz - cur_frame_len);
355 }
356 }
357 }
358
m5602_stop_transfer(struct gspca_dev * gspca_dev)359 static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
360 {
361 struct sd *sd = (struct sd *) gspca_dev;
362
363 /* Run the sensor specific end transfer sequence */
364 if (sd->sensor->stop)
365 sd->sensor->stop(sd);
366 }
367
368 /* sub-driver description */
369 static const struct sd_desc sd_desc = {
370 .name = MODULE_NAME,
371 .config = m5602_configure,
372 .init = m5602_init,
373 .init_controls = m5602_init_controls,
374 .start = m5602_start_transfer,
375 .stopN = m5602_stop_transfer,
376 .pkt_scan = m5602_urb_complete
377 };
378
379 /* this function is called at probe time */
m5602_configure(struct gspca_dev * gspca_dev,const struct usb_device_id * id)380 static int m5602_configure(struct gspca_dev *gspca_dev,
381 const struct usb_device_id *id)
382 {
383 struct sd *sd = (struct sd *) gspca_dev;
384 struct cam *cam;
385 int err;
386
387 cam = &gspca_dev->cam;
388
389 if (dump_bridge)
390 m5602_dump_bridge(sd);
391
392 /* Probe sensor */
393 err = m5602_probe_sensor(sd);
394 if (err)
395 goto fail;
396
397 return 0;
398
399 fail:
400 PERR("ALi m5602 webcam failed");
401 cam->cam_mode = NULL;
402 cam->nmodes = 0;
403
404 return err;
405 }
406
m5602_probe(struct usb_interface * intf,const struct usb_device_id * id)407 static int m5602_probe(struct usb_interface *intf,
408 const struct usb_device_id *id)
409 {
410 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
411 THIS_MODULE);
412 }
413
m5602_disconnect(struct usb_interface * intf)414 static void m5602_disconnect(struct usb_interface *intf)
415 {
416 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
417 struct sd *sd = (struct sd *) gspca_dev;
418
419 if (sd->sensor->disconnect)
420 sd->sensor->disconnect(sd);
421
422 gspca_disconnect(intf);
423 }
424
425 static struct usb_driver sd_driver = {
426 .name = MODULE_NAME,
427 .id_table = m5602_table,
428 .probe = m5602_probe,
429 #ifdef CONFIG_PM
430 .suspend = gspca_suspend,
431 .resume = gspca_resume,
432 .reset_resume = gspca_resume,
433 #endif
434 .disconnect = m5602_disconnect
435 };
436
437 module_usb_driver(sd_driver);
438
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, "
445 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, "
446 "4 = MT9M111, 5 = PO1030, 6 = OV7660");
447
448 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
449 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
450
451 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
452 MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
453 "at startup providing a sensor is found");
454