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