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