xref: /linux/drivers/media/usb/em28xx/em28xx-i2c.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 //
5 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 //		      Markus Rechberger <mrechberger@gmail.com>
7 //		      Mauro Carvalho Chehab <mchehab@kernel.org>
8 //		      Sascha Sommer <saschasommer@freenet.de>
9 // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
10 
11 #include "em28xx.h"
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/usb.h>
16 #include <linux/i2c.h>
17 #include <linux/jiffies.h>
18 
19 #include "xc2028.h"
20 #include <media/v4l2-common.h>
21 #include <media/tuner.h>
22 
23 /* ----------------------------------------------------------- */
24 
25 static unsigned int i2c_scan;
26 module_param(i2c_scan, int, 0444);
27 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
28 
29 static unsigned int i2c_debug;
30 module_param(i2c_debug, int, 0644);
31 MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
32 
33 #define dprintk(level, fmt, arg...) do {				\
34 	if (i2c_debug > level)						\
35 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
36 			   "i2c: %s: " fmt, __func__, ## arg);		\
37 } while (0)
38 
39 /*
40  * Time in msecs to wait for i2c xfers to finish.
41  * 35ms is the maximum time a SMBUS device could wait when
42  * clock stretching is used. As the transfer itself will take
43  * some time to happen, set it to 35 ms.
44  *
45  * Ok, I2C doesn't specify any limit. So, eventually, we may need
46  * to increase this timeout.
47  */
48 #define EM28XX_I2C_XFER_TIMEOUT         35 /* ms */
49 
em28xx_i2c_timeout(struct em28xx * dev)50 static int em28xx_i2c_timeout(struct em28xx *dev)
51 {
52 	int time = EM28XX_I2C_XFER_TIMEOUT;
53 
54 	switch (dev->i2c_speed & 0x03) {
55 	case EM28XX_I2C_FREQ_25_KHZ:
56 		time += 4;		/* Assume 4 ms for transfers */
57 		break;
58 	case EM28XX_I2C_FREQ_100_KHZ:
59 	case EM28XX_I2C_FREQ_400_KHZ:
60 		time += 1;		/* Assume 1 ms for transfers */
61 		break;
62 	default: /* EM28XX_I2C_FREQ_1_5_MHZ */
63 		break;
64 	}
65 
66 	return msecs_to_jiffies(time);
67 }
68 
69 /*
70  * em2800_i2c_send_bytes()
71  * send up to 4 bytes to the em2800 i2c device
72  */
em2800_i2c_send_bytes(struct em28xx * dev,u8 addr,u8 * buf,u16 len)73 static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
74 {
75 	unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
76 	int ret;
77 	u8 b2[6];
78 
79 	if (len < 1 || len > 4)
80 		return -EOPNOTSUPP;
81 
82 	b2[5] = 0x80 + len - 1;
83 	b2[4] = addr;
84 	b2[3] = buf[0];
85 	if (len > 1)
86 		b2[2] = buf[1];
87 	if (len > 2)
88 		b2[1] = buf[2];
89 	if (len > 3)
90 		b2[0] = buf[3];
91 
92 	/* trigger write */
93 	ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
94 	if (ret != 2 + len) {
95 		dev_warn(&dev->intf->dev,
96 			 "failed to trigger write to i2c address 0x%x (error=%i)\n",
97 			    addr, ret);
98 		return (ret < 0) ? ret : -EIO;
99 	}
100 	/* wait for completion */
101 	while (time_is_after_jiffies(timeout)) {
102 		ret = dev->em28xx_read_reg(dev, 0x05);
103 		if (ret == 0x80 + len - 1)
104 			return len;
105 		if (ret == 0x94 + len - 1) {
106 			dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
107 			return -ENXIO;
108 		}
109 		if (ret < 0) {
110 			dev_warn(&dev->intf->dev,
111 				 "failed to get i2c transfer status from bridge register (error=%i)\n",
112 				ret);
113 			return ret;
114 		}
115 		usleep_range(5000, 6000);
116 	}
117 	dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
118 	return -ETIMEDOUT;
119 }
120 
121 /*
122  * em2800_i2c_recv_bytes()
123  * read up to 4 bytes from the em2800 i2c device
124  */
em2800_i2c_recv_bytes(struct em28xx * dev,u8 addr,u8 * buf,u16 len)125 static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
126 {
127 	unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
128 	u8 buf2[4];
129 	int ret;
130 	int i;
131 
132 	if (len < 1 || len > 4)
133 		return -EOPNOTSUPP;
134 
135 	/* trigger read */
136 	buf2[1] = 0x84 + len - 1;
137 	buf2[0] = addr;
138 	ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
139 	if (ret != 2) {
140 		dev_warn(&dev->intf->dev,
141 			 "failed to trigger read from i2c address 0x%x (error=%i)\n",
142 			 addr, ret);
143 		return (ret < 0) ? ret : -EIO;
144 	}
145 
146 	/* wait for completion */
147 	while (time_is_after_jiffies(timeout)) {
148 		ret = dev->em28xx_read_reg(dev, 0x05);
149 		if (ret == 0x84 + len - 1)
150 			break;
151 		if (ret == 0x94 + len - 1) {
152 			dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
153 				ret);
154 			return -ENXIO;
155 		}
156 		if (ret < 0) {
157 			dev_warn(&dev->intf->dev,
158 				 "failed to get i2c transfer status from bridge register (error=%i)\n",
159 				 ret);
160 			return ret;
161 		}
162 		usleep_range(5000, 6000);
163 	}
164 	if (ret != 0x84 + len - 1)
165 		dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
166 
167 	/* get the received message */
168 	ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4 - len, buf2, len);
169 	if (ret != len) {
170 		dev_warn(&dev->intf->dev,
171 			 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
172 			 addr, ret);
173 		return (ret < 0) ? ret : -EIO;
174 	}
175 	for (i = 0; i < len; i++)
176 		buf[i] = buf2[len - 1 - i];
177 
178 	return ret;
179 }
180 
181 /*
182  * em2800_i2c_check_for_device()
183  * check if there is an i2c device at the supplied address
184  */
em2800_i2c_check_for_device(struct em28xx * dev,u8 addr)185 static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
186 {
187 	u8 buf;
188 	int ret;
189 
190 	ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
191 	if (ret == 1)
192 		return 0;
193 	return (ret < 0) ? ret : -EIO;
194 }
195 
196 /*
197  * em28xx_i2c_send_bytes()
198  */
em28xx_i2c_send_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len,int stop)199 static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
200 				 u16 len, int stop)
201 {
202 	unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
203 	int ret;
204 
205 	if (len < 1 || len > 64)
206 		return -EOPNOTSUPP;
207 	/*
208 	 * NOTE: limited by the USB ctrl message constraints
209 	 * Zero length reads always succeed, even if no device is connected
210 	 */
211 
212 	/* Write to i2c device */
213 	ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
214 	if (ret != len) {
215 		if (ret < 0) {
216 			dev_warn(&dev->intf->dev,
217 				 "writing to i2c device at 0x%x failed (error=%i)\n",
218 				 addr, ret);
219 			return ret;
220 		}
221 		dev_warn(&dev->intf->dev,
222 			 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
223 				len, addr, ret);
224 		return -EIO;
225 	}
226 
227 	/* wait for completion */
228 	while (time_is_after_jiffies(timeout)) {
229 		ret = dev->em28xx_read_reg(dev, 0x05);
230 		if (ret == 0) /* success */
231 			return len;
232 		if (ret == 0x10) {
233 			dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
234 				addr);
235 			return -ENXIO;
236 		}
237 		if (ret < 0) {
238 			dev_warn(&dev->intf->dev,
239 				 "failed to get i2c transfer status from bridge register (error=%i)\n",
240 				 ret);
241 			return ret;
242 		}
243 		usleep_range(5000, 6000);
244 		/*
245 		 * NOTE: do we really have to wait for success ?
246 		 * Never seen anything else than 0x00 or 0x10
247 		 * (even with high payload) ...
248 		 */
249 	}
250 
251 	if (ret == 0x02 || ret == 0x04) {
252 		/* NOTE: these errors seem to be related to clock stretching */
253 		dprintk(0,
254 			"write to i2c device at 0x%x timed out (status=%i)\n",
255 			addr, ret);
256 		return -ETIMEDOUT;
257 	}
258 
259 	dev_warn(&dev->intf->dev,
260 		 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
261 		 addr, ret);
262 	return -EIO;
263 }
264 
265 /*
266  * em28xx_i2c_recv_bytes()
267  * read a byte from the i2c device
268  */
em28xx_i2c_recv_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)269 static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
270 {
271 	int ret;
272 
273 	if (len < 1 || len > 64)
274 		return -EOPNOTSUPP;
275 	/*
276 	 * NOTE: limited by the USB ctrl message constraints
277 	 * Zero length reads always succeed, even if no device is connected
278 	 */
279 
280 	/* Read data from i2c device */
281 	ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
282 	if (ret < 0) {
283 		dev_warn(&dev->intf->dev,
284 			 "reading from i2c device at 0x%x failed (error=%i)\n",
285 			 addr, ret);
286 		return ret;
287 	} else if (ret != len) {
288 		dev_dbg(&dev->intf->dev,
289 			"%i bytes read from i2c device at 0x%x requested, but %i bytes written\n",
290 				ret, addr, len);
291 	}
292 	/*
293 	 * NOTE: some devices with two i2c buses have the bad habit to return 0
294 	 * bytes if we are on bus B AND there was no write attempt to the
295 	 * specified slave address before AND no device is present at the
296 	 * requested slave address.
297 	 * Anyway, the next check will fail with -ENXIO in this case, so avoid
298 	 * spamming the system log on device probing and do nothing here.
299 	 */
300 
301 	/* Check success of the i2c operation */
302 	ret = dev->em28xx_read_reg(dev, 0x05);
303 	if (ret == 0) /* success */
304 		return len;
305 	if (ret < 0) {
306 		dev_warn(&dev->intf->dev,
307 			 "failed to get i2c transfer status from bridge register (error=%i)\n",
308 			 ret);
309 		return ret;
310 	}
311 	if (ret == 0x10) {
312 		dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
313 			addr);
314 		return -ENXIO;
315 	}
316 
317 	if (ret == 0x02 || ret == 0x04) {
318 		/* NOTE: these errors seem to be related to clock stretching */
319 		dprintk(0,
320 			"write to i2c device at 0x%x timed out (status=%i)\n",
321 			addr, ret);
322 		return -ETIMEDOUT;
323 	}
324 
325 	dev_warn(&dev->intf->dev,
326 		 "read from i2c device at 0x%x failed with unknown error (status=%i)\n",
327 		 addr, ret);
328 	return -EIO;
329 }
330 
331 /*
332  * em28xx_i2c_check_for_device()
333  * check if there is a i2c_device at the supplied address
334  */
em28xx_i2c_check_for_device(struct em28xx * dev,u16 addr)335 static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
336 {
337 	int ret;
338 	u8 buf;
339 
340 	ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
341 	if (ret == 1)
342 		return 0;
343 	return (ret < 0) ? ret : -EIO;
344 }
345 
346 /*
347  * em25xx_bus_B_send_bytes
348  * write bytes to the i2c device
349  */
em25xx_bus_B_send_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)350 static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
351 				   u16 len)
352 {
353 	int ret;
354 
355 	if (len < 1 || len > 64)
356 		return -EOPNOTSUPP;
357 	/*
358 	 * NOTE: limited by the USB ctrl message constraints
359 	 * Zero length reads always succeed, even if no device is connected
360 	 */
361 
362 	/* Set register and write value */
363 	ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
364 	if (ret != len) {
365 		if (ret < 0) {
366 			dev_warn(&dev->intf->dev,
367 				 "writing to i2c device at 0x%x failed (error=%i)\n",
368 				 addr, ret);
369 			return ret;
370 		}
371 
372 		dev_warn(&dev->intf->dev,
373 			 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
374 			 len, addr, ret);
375 		return -EIO;
376 	}
377 	/* Check success */
378 	ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
379 	/*
380 	 * NOTE: the only error we've seen so far is
381 	 * 0x01 when the slave device is not present
382 	 */
383 	if (!ret)
384 		return len;
385 
386 	if (ret > 0) {
387 		dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
388 		return -ENXIO;
389 	}
390 
391 	return ret;
392 	/*
393 	 * NOTE: With chip types (other chip IDs) which actually don't support
394 	 * this operation, it seems to succeed ALWAYS ! (even if there is no
395 	 * slave device or even no second i2c bus provided)
396 	 */
397 }
398 
399 /*
400  * em25xx_bus_B_recv_bytes
401  * read bytes from the i2c device
402  */
em25xx_bus_B_recv_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)403 static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
404 				   u16 len)
405 {
406 	int ret;
407 
408 	if (len < 1 || len > 64)
409 		return -EOPNOTSUPP;
410 	/*
411 	 * NOTE: limited by the USB ctrl message constraints
412 	 * Zero length reads always succeed, even if no device is connected
413 	 */
414 
415 	/* Read value */
416 	ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
417 	if (ret < 0) {
418 		dev_warn(&dev->intf->dev,
419 			 "reading from i2c device at 0x%x failed (error=%i)\n",
420 			 addr, ret);
421 		return ret;
422 	}
423 	/*
424 	 * NOTE: some devices with two i2c buses have the bad habit to return 0
425 	 * bytes if we are on bus B AND there was no write attempt to the
426 	 * specified slave address before AND no device is present at the
427 	 * requested slave address.
428 	 * Anyway, the next check will fail with -ENXIO in this case, so avoid
429 	 * spamming the system log on device probing and do nothing here.
430 	 */
431 
432 	/* Check success */
433 	ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
434 	/*
435 	 * NOTE: the only error we've seen so far is
436 	 * 0x01 when the slave device is not present
437 	 */
438 	if (!ret)
439 		return len;
440 
441 	if (ret > 0) {
442 		dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
443 		return -ENXIO;
444 	}
445 
446 	return ret;
447 	/*
448 	 * NOTE: With chip types (other chip IDs) which actually don't support
449 	 * this operation, it seems to succeed ALWAYS ! (even if there is no
450 	 * slave device or even no second i2c bus provided)
451 	 */
452 }
453 
454 /*
455  * em25xx_bus_B_check_for_device()
456  * check if there is a i2c device at the supplied address
457  */
em25xx_bus_B_check_for_device(struct em28xx * dev,u16 addr)458 static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
459 {
460 	u8 buf;
461 	int ret;
462 
463 	ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
464 	if (ret < 0)
465 		return ret;
466 
467 	return 0;
468 	/*
469 	 * NOTE: With chips which do not support this operation,
470 	 * it seems to succeed ALWAYS ! (even if no device connected)
471 	 */
472 }
473 
i2c_check_for_device(struct em28xx_i2c_bus * i2c_bus,u16 addr)474 static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
475 {
476 	struct em28xx *dev = i2c_bus->dev;
477 	int rc = -EOPNOTSUPP;
478 
479 	if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
480 		rc = em28xx_i2c_check_for_device(dev, addr);
481 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
482 		rc = em2800_i2c_check_for_device(dev, addr);
483 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
484 		rc = em25xx_bus_B_check_for_device(dev, addr);
485 	return rc;
486 }
487 
i2c_recv_bytes(struct em28xx_i2c_bus * i2c_bus,struct i2c_msg msg)488 static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
489 				 struct i2c_msg msg)
490 {
491 	struct em28xx *dev = i2c_bus->dev;
492 	u16 addr = msg.addr << 1;
493 	int rc = -EOPNOTSUPP;
494 
495 	if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
496 		rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
497 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
498 		rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
499 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
500 		rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
501 	return rc;
502 }
503 
i2c_send_bytes(struct em28xx_i2c_bus * i2c_bus,struct i2c_msg msg,int stop)504 static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
505 				 struct i2c_msg msg, int stop)
506 {
507 	struct em28xx *dev = i2c_bus->dev;
508 	u16 addr = msg.addr << 1;
509 	int rc = -EOPNOTSUPP;
510 
511 	if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
512 		rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
513 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
514 		rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
515 	else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
516 		rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
517 	return rc;
518 }
519 
520 /*
521  * em28xx_i2c_xfer()
522  * the main i2c transfer function
523  */
em28xx_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg msgs[],int num)524 static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
525 			   struct i2c_msg msgs[], int num)
526 {
527 	struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
528 	struct em28xx *dev = i2c_bus->dev;
529 	unsigned int bus = i2c_bus->bus;
530 	int addr, rc, i;
531 	u8 reg;
532 
533 	/*
534 	 * prevent i2c xfer attempts after device is disconnected
535 	 * some fe's try to do i2c writes/reads from their release
536 	 * interfaces when called in disconnect path
537 	 */
538 	if (dev->disconnected)
539 		return -ENODEV;
540 
541 	if (!rt_mutex_trylock(&dev->i2c_bus_lock))
542 		return -EAGAIN;
543 
544 	/* Switch I2C bus if needed */
545 	if (bus != dev->cur_i2c_bus &&
546 	    i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
547 		if (bus == 1)
548 			reg = EM2874_I2C_SECONDARY_BUS_SELECT;
549 		else
550 			reg = 0;
551 		em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
552 				      EM2874_I2C_SECONDARY_BUS_SELECT);
553 		dev->cur_i2c_bus = bus;
554 	}
555 
556 	for (i = 0; i < num; i++) {
557 		addr = msgs[i].addr << 1;
558 		if (!msgs[i].len) {
559 			/*
560 			 * no len: check only for device presence
561 			 * This code is only called during device probe.
562 			 */
563 			rc = i2c_check_for_device(i2c_bus, addr);
564 
565 			if (rc == -ENXIO)
566 				rc = -ENODEV;
567 		} else if (msgs[i].flags & I2C_M_RD) {
568 			/* read bytes */
569 			rc = i2c_recv_bytes(i2c_bus, msgs[i]);
570 		} else {
571 			/* write bytes */
572 			rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
573 		}
574 
575 		if (rc < 0)
576 			goto error;
577 
578 		dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
579 			(msgs[i].flags & I2C_M_RD) ? "read" : "write",
580 			i == num - 1 ? "stop" : "nonstop",
581 			addr, msgs[i].len,
582 			msgs[i].len, msgs[i].buf);
583 	}
584 
585 	rt_mutex_unlock(&dev->i2c_bus_lock);
586 	return num;
587 
588 error:
589 	dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
590 		(msgs[i].flags & I2C_M_RD) ? "read" : "write",
591 		i == num - 1 ? "stop" : "nonstop",
592 		addr, msgs[i].len,
593 		(rc == -ENODEV) ? "no device " : "",
594 		rc);
595 
596 	rt_mutex_unlock(&dev->i2c_bus_lock);
597 	return rc;
598 }
599 
600 /*
601  * based on linux/sunrpc/svcauth.h and linux/hash.h
602  * The original hash function returns a different value, if arch is x86_64
603  * or i386.
604  */
em28xx_hash_mem(char * buf,int length,int bits)605 static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
606 {
607 	unsigned long hash = 0;
608 	unsigned long l = 0;
609 	int len = 0;
610 	unsigned char c;
611 
612 	do {
613 		if (len == length) {
614 			c = (char)len;
615 			len = -1;
616 		} else {
617 			c = *buf++;
618 		}
619 		l = (l << 8) | c;
620 		len++;
621 		if ((len & (32 / 8 - 1)) == 0)
622 			hash = ((hash ^ l) * 0x9e370001UL);
623 	} while (len);
624 
625 	return (hash >> (32 - bits)) & 0xffffffffUL;
626 }
627 
628 /*
629  * Helper function to read data blocks from i2c clients with 8 or 16 bit
630  * address width, 8 bit register width and auto incrementation been activated
631  */
em28xx_i2c_read_block(struct em28xx * dev,unsigned int bus,u16 addr,bool addr_w16,u16 len,u8 * data)632 static int em28xx_i2c_read_block(struct em28xx *dev, unsigned int bus, u16 addr,
633 				 bool addr_w16, u16 len, u8 *data)
634 {
635 	int remain = len, rsize, rsize_max, ret;
636 	u8 buf[2];
637 
638 	/* Sanity check */
639 	if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
640 		return -EINVAL;
641 	/* Select address */
642 	buf[0] = addr >> 8;
643 	buf[1] = addr & 0xff;
644 	ret = i2c_master_send(&dev->i2c_client[bus],
645 			      buf + !addr_w16, 1 + addr_w16);
646 	if (ret < 0)
647 		return ret;
648 	/* Read data */
649 	if (dev->board.is_em2800)
650 		rsize_max = 4;
651 	else
652 		rsize_max = 64;
653 	while (remain > 0) {
654 		if (remain > rsize_max)
655 			rsize = rsize_max;
656 		else
657 			rsize = remain;
658 
659 		ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
660 		if (ret < 0)
661 			return ret;
662 
663 		remain -= rsize;
664 		data += rsize;
665 	}
666 
667 	return len;
668 }
669 
em28xx_i2c_eeprom(struct em28xx * dev,unsigned int bus,u8 ** eedata,u16 * eedata_len)670 static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned int bus,
671 			     u8 **eedata, u16 *eedata_len)
672 {
673 	const u16 len = 256;
674 	/*
675 	 * FIXME common length/size for bytes to read, to display, hash
676 	 * calculation and returned device dataset. Simplifies the code a lot,
677 	 * but we might have to deal with multiple sizes in the future !
678 	 */
679 	int err;
680 	struct em28xx_eeprom *dev_config;
681 	u8 buf, *data;
682 
683 	*eedata = NULL;
684 	*eedata_len = 0;
685 
686 	/* EEPROM is always on i2c bus 0 on all known devices. */
687 
688 	dev->i2c_client[bus].addr = 0xa0 >> 1;
689 
690 	/* Check if board has eeprom */
691 	err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
692 	if (err < 0) {
693 		dev_info(&dev->intf->dev, "board has no eeprom\n");
694 		return -ENODEV;
695 	}
696 
697 	data = kzalloc(len, GFP_KERNEL);
698 	if (!data)
699 		return -ENOMEM;
700 
701 	/* Read EEPROM content */
702 	err = em28xx_i2c_read_block(dev, bus, 0x0000,
703 				    dev->eeprom_addrwidth_16bit,
704 				    len, data);
705 	if (err != len) {
706 		dev_err(&dev->intf->dev,
707 			"failed to read eeprom (err=%d)\n", err);
708 		goto error;
709 	}
710 
711 	if (i2c_debug) {
712 		/* Display eeprom content */
713 		print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
714 			       16, 1, data, len, true);
715 
716 		if (dev->eeprom_addrwidth_16bit)
717 			dev_info(&dev->intf->dev,
718 				 "eeprom %06x: ... (skipped)\n", 256);
719 	}
720 
721 	if (dev->eeprom_addrwidth_16bit &&
722 	    data[0] == 0x26 && data[3] == 0x00) {
723 		/* new eeprom format; size 4-64kb */
724 		u16 mc_start;
725 		u16 hwconf_offset;
726 
727 		dev->hash = em28xx_hash_mem(data, len, 32);
728 		mc_start = (data[1] << 8) + 4;	/* usually 0x0004 */
729 
730 		dev_info(&dev->intf->dev,
731 			 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
732 			 data, dev->hash);
733 		dev_info(&dev->intf->dev,
734 			 "EEPROM info:\n");
735 		dev_info(&dev->intf->dev,
736 			 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
737 			 mc_start, data[2]);
738 		/*
739 		 * boot configuration (address 0x0002):
740 		 * [0]   microcode download speed: 1 = 400 kHz; 0 = 100 kHz
741 		 * [1]   always selects 12 kb RAM
742 		 * [2]   USB device speed: 1 = force Full Speed; 0 = auto detect
743 		 * [4]   1 = force fast mode and no suspend for device testing
744 		 * [5:7] USB PHY tuning registers; determined by device
745 		 *       characterization
746 		 */
747 
748 		/*
749 		 * Read hardware config dataset offset from address
750 		 * (microcode start + 46)
751 		 */
752 		err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
753 					    data);
754 		if (err != 2) {
755 			dev_err(&dev->intf->dev,
756 				"failed to read hardware configuration data from eeprom (err=%d)\n",
757 				err);
758 			goto error;
759 		}
760 
761 		/* Calculate hardware config dataset start address */
762 		hwconf_offset = mc_start + data[0] + (data[1] << 8);
763 
764 		/* Read hardware config dataset */
765 		/*
766 		 * NOTE: the microcode copy can be multiple pages long, but
767 		 * we assume the hardware config dataset is the same as in
768 		 * the old eeprom and not longer than 256 bytes.
769 		 * tveeprom is currently also limited to 256 bytes.
770 		 */
771 		err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
772 					    data);
773 		if (err != len) {
774 			dev_err(&dev->intf->dev,
775 				"failed to read hardware configuration data from eeprom (err=%d)\n",
776 				err);
777 			goto error;
778 		}
779 
780 		/* Verify hardware config dataset */
781 		/* NOTE: not all devices provide this type of dataset */
782 		if (data[0] != 0x1a || data[1] != 0xeb ||
783 		    data[2] != 0x67 || data[3] != 0x95) {
784 			dev_info(&dev->intf->dev,
785 				 "\tno hardware configuration dataset found in eeprom\n");
786 			kfree(data);
787 			return 0;
788 		}
789 
790 		/*
791 		 * TODO: decrypt eeprom data for camera bridges
792 		 * (em25xx, em276x+)
793 		 */
794 
795 	} else if (!dev->eeprom_addrwidth_16bit &&
796 		   data[0] == 0x1a && data[1] == 0xeb &&
797 		   data[2] == 0x67 && data[3] == 0x95) {
798 		dev->hash = em28xx_hash_mem(data, len, 32);
799 		dev_info(&dev->intf->dev,
800 			 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
801 			 data, dev->hash);
802 		dev_info(&dev->intf->dev,
803 			 "EEPROM info:\n");
804 	} else {
805 		dev_info(&dev->intf->dev,
806 			 "unknown eeprom format or eeprom corrupted !\n");
807 		err = -ENODEV;
808 		goto error;
809 	}
810 
811 	*eedata = data;
812 	*eedata_len = len;
813 	dev_config = (void *)*eedata;
814 
815 	switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
816 	case 0:
817 		dev_info(&dev->intf->dev, "\tNo audio on board.\n");
818 		break;
819 	case 1:
820 		dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
821 		break;
822 	case 2:
823 		if (dev->chip_id < CHIP_ID_EM2860)
824 			dev_info(&dev->intf->dev,
825 				 "\tI2S audio, sample rate=32k\n");
826 		else
827 			dev_info(&dev->intf->dev,
828 				 "\tI2S audio, 3 sample rates\n");
829 		break;
830 	case 3:
831 		if (dev->chip_id < CHIP_ID_EM2860)
832 			dev_info(&dev->intf->dev,
833 				 "\tI2S audio, 3 sample rates\n");
834 		else
835 			dev_info(&dev->intf->dev,
836 				 "\tI2S audio, 5 sample rates\n");
837 		break;
838 	}
839 
840 	if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
841 		dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
842 
843 	if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
844 		dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
845 
846 	switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
847 	case 0:
848 		dev_info(&dev->intf->dev, "\t500mA max power\n");
849 		break;
850 	case 1:
851 		dev_info(&dev->intf->dev, "\t400mA max power\n");
852 		break;
853 	case 2:
854 		dev_info(&dev->intf->dev, "\t300mA max power\n");
855 		break;
856 	case 3:
857 		dev_info(&dev->intf->dev, "\t200mA max power\n");
858 		break;
859 	}
860 	dev_info(&dev->intf->dev,
861 		 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
862 		 dev_config->string_idx_table,
863 		 le16_to_cpu(dev_config->string1),
864 		 le16_to_cpu(dev_config->string2),
865 		 le16_to_cpu(dev_config->string3));
866 
867 	return 0;
868 
869 error:
870 	kfree(data);
871 	return err;
872 }
873 
874 /* ----------------------------------------------------------- */
875 
876 /*
877  * functionality()
878  */
functionality(struct i2c_adapter * i2c_adap)879 static u32 functionality(struct i2c_adapter *i2c_adap)
880 {
881 	struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
882 
883 	if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX ||
884 	    i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) {
885 		return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
886 	} else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)  {
887 		return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
888 			~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
889 	}
890 
891 	WARN(1, "Unknown i2c bus algorithm.\n");
892 	return 0;
893 }
894 
895 static const struct i2c_algorithm em28xx_algo = {
896 	.master_xfer   = em28xx_i2c_xfer,
897 	.functionality = functionality,
898 };
899 
900 static const struct i2c_adapter em28xx_adap_template = {
901 	.owner = THIS_MODULE,
902 	.name = "em28xx",
903 	.algo = &em28xx_algo,
904 };
905 
906 static const struct i2c_client em28xx_client_template = {
907 	.name = "em28xx internal",
908 };
909 
910 /* ----------------------------------------------------------- */
911 
912 /*
913  * i2c_devs
914  * incomplete list of known devices
915  */
916 static char *i2c_devs[128] = {
917 	[0x1c >> 1] = "lgdt330x",
918 	[0x3e >> 1] = "remote IR sensor",
919 	[0x4a >> 1] = "saa7113h",
920 	[0x52 >> 1] = "drxk",
921 	[0x60 >> 1] = "remote IR sensor",
922 	[0x8e >> 1] = "remote IR sensor",
923 	[0x86 >> 1] = "tda9887",
924 	[0x80 >> 1] = "msp34xx",
925 	[0x88 >> 1] = "msp34xx",
926 	[0xa0 >> 1] = "eeprom",
927 	[0xb0 >> 1] = "tda9874",
928 	[0xb8 >> 1] = "tvp5150a",
929 	[0xba >> 1] = "webcam sensor or tvp5150a",
930 	[0xc0 >> 1] = "tuner (analog)",
931 	[0xc2 >> 1] = "tuner (analog)",
932 	[0xc4 >> 1] = "tuner (analog)",
933 	[0xc6 >> 1] = "tuner (analog)",
934 };
935 
936 /*
937  * do_i2c_scan()
938  * check i2c address range for devices
939  */
em28xx_do_i2c_scan(struct em28xx * dev,unsigned int bus)940 void em28xx_do_i2c_scan(struct em28xx *dev, unsigned int bus)
941 {
942 	u8 i2c_devicelist[128];
943 	unsigned char buf;
944 	int i, rc;
945 
946 	memset(i2c_devicelist, 0, sizeof(i2c_devicelist));
947 
948 	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
949 		dev->i2c_client[bus].addr = i;
950 		rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
951 		if (rc < 0)
952 			continue;
953 		i2c_devicelist[i] = i;
954 		dev_info(&dev->intf->dev,
955 			 "found i2c device @ 0x%x on bus %d [%s]\n",
956 			 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
957 	}
958 
959 	if (bus == dev->def_i2c_bus)
960 		dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
961 						sizeof(i2c_devicelist), 32);
962 }
963 
964 /*
965  * em28xx_i2c_register()
966  * register i2c bus
967  */
em28xx_i2c_register(struct em28xx * dev,unsigned int bus,enum em28xx_i2c_algo_type algo_type)968 int em28xx_i2c_register(struct em28xx *dev, unsigned int bus,
969 			enum em28xx_i2c_algo_type algo_type)
970 {
971 	int retval;
972 
973 	if (WARN_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg ||
974 		    !dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req))
975 		return -ENODEV;
976 
977 	if (bus >= NUM_I2C_BUSES)
978 		return -ENODEV;
979 
980 	dev->i2c_adap[bus] = em28xx_adap_template;
981 	dev->i2c_adap[bus].dev.parent = &dev->intf->dev;
982 	strscpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev),
983 		sizeof(dev->i2c_adap[bus].name));
984 
985 	dev->i2c_bus[bus].bus = bus;
986 	dev->i2c_bus[bus].algo_type = algo_type;
987 	dev->i2c_bus[bus].dev = dev;
988 	dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
989 
990 	retval = i2c_add_adapter(&dev->i2c_adap[bus]);
991 	if (retval < 0) {
992 		dev_err(&dev->intf->dev,
993 			"%s: i2c_add_adapter failed! retval [%d]\n",
994 			__func__, retval);
995 		return retval;
996 	}
997 
998 	dev->i2c_client[bus] = em28xx_client_template;
999 	dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
1000 
1001 	/* Up to now, all eeproms are at bus 0 */
1002 	if (!bus) {
1003 		retval = em28xx_i2c_eeprom(dev, bus,
1004 					   &dev->eedata, &dev->eedata_len);
1005 		if (retval < 0 && retval != -ENODEV) {
1006 			dev_err(&dev->intf->dev,
1007 				"%s: em28xx_i2_eeprom failed! retval [%d]\n",
1008 				__func__, retval);
1009 		}
1010 	}
1011 
1012 	if (i2c_scan)
1013 		em28xx_do_i2c_scan(dev, bus);
1014 
1015 	return 0;
1016 }
1017 
1018 /*
1019  * em28xx_i2c_unregister()
1020  * unregister i2c_bus
1021  */
em28xx_i2c_unregister(struct em28xx * dev,unsigned int bus)1022 int em28xx_i2c_unregister(struct em28xx *dev, unsigned int bus)
1023 {
1024 	if (bus >= NUM_I2C_BUSES)
1025 		return -ENODEV;
1026 
1027 	i2c_del_adapter(&dev->i2c_adap[bus]);
1028 	return 0;
1029 }
1030