aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/em28xx/em28xx-i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/usb/em28xx/em28xx-i2c.c')
-rw-r--r--drivers/media/usb/em28xx/em28xx-i2c.c691
1 files changed, 496 insertions, 195 deletions
diff --git a/drivers/media/usb/em28xx/em28xx-i2c.c b/drivers/media/usb/em28xx/em28xx-i2c.c
index 8532c1d4fd46..4851cc2e4a4d 100644
--- a/drivers/media/usb/em28xx/em28xx-i2c.c
+++ b/drivers/media/usb/em28xx/em28xx-i2c.c
@@ -5,6 +5,7 @@
5 Markus Rechberger <mrechberger@gmail.com> 5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org> 6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de> 7 Sascha Sommer <saschasommer@freenet.de>
8 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
8 9
9 This program is free software; you can redistribute it and/or modify 10 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by 11 it under the terms of the GNU General Public License as published by
@@ -41,14 +42,6 @@ static unsigned int i2c_debug;
41module_param(i2c_debug, int, 0644); 42module_param(i2c_debug, int, 0644);
42MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
43 44
44#define dprintk2(lvl, fmt, args...) \
45do { \
46 if (i2c_debug >= lvl) { \
47 printk(KERN_DEBUG "%s at %s: " fmt, \
48 dev->name, __func__ , ##args); \
49 } \
50} while (0)
51
52/* 45/*
53 * em2800_i2c_send_bytes() 46 * em2800_i2c_send_bytes()
54 * send up to 4 bytes to the em2800 i2c device 47 * send up to 4 bytes to the em2800 i2c device
@@ -76,8 +69,8 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
76 /* trigger write */ 69 /* trigger write */
77 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len); 70 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
78 if (ret != 2 + len) { 71 if (ret != 2 + len) {
79 em28xx_warn("failed to trigger write to i2c address 0x%x " 72 em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
80 "(error=%i)\n", addr, ret); 73 addr, ret);
81 return (ret < 0) ? ret : -EIO; 74 return (ret < 0) ? ret : -EIO;
82 } 75 }
83 /* wait for completion */ 76 /* wait for completion */
@@ -89,8 +82,8 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
89 } else if (ret == 0x94 + len - 1) { 82 } else if (ret == 0x94 + len - 1) {
90 return -ENODEV; 83 return -ENODEV;
91 } else if (ret < 0) { 84 } else if (ret < 0) {
92 em28xx_warn("failed to get i2c transfer status from " 85 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
93 "bridge register (error=%i)\n", ret); 86 ret);
94 return ret; 87 return ret;
95 } 88 }
96 msleep(5); 89 msleep(5);
@@ -118,8 +111,8 @@ static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
118 buf2[0] = addr; 111 buf2[0] = addr;
119 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2); 112 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
120 if (ret != 2) { 113 if (ret != 2) {
121 em28xx_warn("failed to trigger read from i2c address 0x%x " 114 em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
122 "(error=%i)\n", addr, ret); 115 addr, ret);
123 return (ret < 0) ? ret : -EIO; 116 return (ret < 0) ? ret : -EIO;
124 } 117 }
125 118
@@ -132,8 +125,8 @@ static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
132 } else if (ret == 0x94 + len - 1) { 125 } else if (ret == 0x94 + len - 1) {
133 return -ENODEV; 126 return -ENODEV;
134 } else if (ret < 0) { 127 } else if (ret < 0) {
135 em28xx_warn("failed to get i2c transfer status from " 128 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
136 "bridge register (error=%i)\n", ret); 129 ret);
137 return ret; 130 return ret;
138 } 131 }
139 msleep(5); 132 msleep(5);
@@ -144,9 +137,8 @@ static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
144 /* get the received message */ 137 /* get the received message */
145 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len); 138 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
146 if (ret != len) { 139 if (ret != len) {
147 em28xx_warn("reading from i2c device at 0x%x failed: " 140 em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
148 "couldn't get the received message from the bridge " 141 addr, ret);
149 "(error=%i)\n", addr, ret);
150 return (ret < 0) ? ret : -EIO; 142 return (ret < 0) ? ret : -EIO;
151 } 143 }
152 for (i = 0; i < len; i++) 144 for (i = 0; i < len; i++)
@@ -180,19 +172,20 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
180 172
181 if (len < 1 || len > 64) 173 if (len < 1 || len > 64)
182 return -EOPNOTSUPP; 174 return -EOPNOTSUPP;
183 /* NOTE: limited by the USB ctrl message constraints 175 /*
184 * Zero length reads always succeed, even if no device is connected */ 176 * NOTE: limited by the USB ctrl message constraints
177 * Zero length reads always succeed, even if no device is connected
178 */
185 179
186 /* Write to i2c device */ 180 /* Write to i2c device */
187 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len); 181 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
188 if (ret != len) { 182 if (ret != len) {
189 if (ret < 0) { 183 if (ret < 0) {
190 em28xx_warn("writing to i2c device at 0x%x failed " 184 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
191 "(error=%i)\n", addr, ret); 185 addr, ret);
192 return ret; 186 return ret;
193 } else { 187 } else {
194 em28xx_warn("%i bytes write to i2c device at 0x%x " 188 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
195 "requested, but %i bytes written\n",
196 len, addr, ret); 189 len, addr, ret);
197 return -EIO; 190 return -EIO;
198 } 191 }
@@ -207,14 +200,16 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
207 } else if (ret == 0x10) { 200 } else if (ret == 0x10) {
208 return -ENODEV; 201 return -ENODEV;
209 } else if (ret < 0) { 202 } else if (ret < 0) {
210 em28xx_warn("failed to read i2c transfer status from " 203 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
211 "bridge (error=%i)\n", ret); 204 ret);
212 return ret; 205 return ret;
213 } 206 }
214 msleep(5); 207 msleep(5);
215 /* NOTE: do we really have to wait for success ? 208 /*
216 Never seen anything else than 0x00 or 0x10 209 * NOTE: do we really have to wait for success ?
217 (even with high payload) ... */ 210 * Never seen anything else than 0x00 or 0x10
211 * (even with high payload) ...
212 */
218 } 213 }
219 em28xx_warn("write to i2c device at 0x%x timed out\n", addr); 214 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
220 return -EIO; 215 return -EIO;
@@ -230,29 +225,32 @@ static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
230 225
231 if (len < 1 || len > 64) 226 if (len < 1 || len > 64)
232 return -EOPNOTSUPP; 227 return -EOPNOTSUPP;
233 /* NOTE: limited by the USB ctrl message constraints 228 /*
234 * Zero length reads always succeed, even if no device is connected */ 229 * NOTE: limited by the USB ctrl message constraints
230 * Zero length reads always succeed, even if no device is connected
231 */
235 232
236 /* Read data from i2c device */ 233 /* Read data from i2c device */
237 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len); 234 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
238 if (ret != len) { 235 if (ret < 0) {
239 if (ret < 0) { 236 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
240 em28xx_warn("reading from i2c device at 0x%x failed " 237 addr, ret);
241 "(error=%i)\n", addr, ret); 238 return ret;
242 return ret;
243 } else {
244 em28xx_warn("%i bytes requested from i2c device at "
245 "0x%x, but %i bytes received\n",
246 len, addr, ret);
247 return -EIO;
248 }
249 } 239 }
240 /*
241 * NOTE: some devices with two i2c busses have the bad habit to return 0
242 * bytes if we are on bus B AND there was no write attempt to the
243 * specified slave address before AND no device is present at the
244 * requested slave address.
245 * Anyway, the next check will fail with -ENODEV in this case, so avoid
246 * spamming the system log on device probing and do nothing here.
247 */
250 248
251 /* Check success of the i2c operation */ 249 /* Check success of the i2c operation */
252 ret = dev->em28xx_read_reg(dev, 0x05); 250 ret = dev->em28xx_read_reg(dev, 0x05);
253 if (ret < 0) { 251 if (ret < 0) {
254 em28xx_warn("failed to read i2c transfer status from " 252 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
255 "bridge (error=%i)\n", ret); 253 ret);
256 return ret; 254 return ret;
257 } 255 }
258 if (ret > 0) { 256 if (ret > 0) {
@@ -282,77 +280,254 @@ static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
282} 280}
283 281
284/* 282/*
283 * em25xx_bus_B_send_bytes
284 * write bytes to the i2c device
285 */
286static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
287 u16 len)
288{
289 int ret;
290
291 if (len < 1 || len > 64)
292 return -EOPNOTSUPP;
293 /*
294 * NOTE: limited by the USB ctrl message constraints
295 * Zero length reads always succeed, even if no device is connected
296 */
297
298 /* Set register and write value */
299 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
300 if (ret != len) {
301 if (ret < 0) {
302 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
303 addr, ret);
304 return ret;
305 } else {
306 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
307 len, addr, ret);
308 return -EIO;
309 }
310 }
311 /* Check success */
312 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
313 /*
314 * NOTE: the only error we've seen so far is
315 * 0x01 when the slave device is not present
316 */
317 if (!ret)
318 return len;
319 else if (ret > 0)
320 return -ENODEV;
321
322 return ret;
323 /*
324 * NOTE: With chip types (other chip IDs) which actually don't support
325 * this operation, it seems to succeed ALWAYS ! (even if there is no
326 * slave device or even no second i2c bus provided)
327 */
328}
329
330/*
331 * em25xx_bus_B_recv_bytes
332 * read bytes from the i2c device
333 */
334static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
335 u16 len)
336{
337 int ret;
338
339 if (len < 1 || len > 64)
340 return -EOPNOTSUPP;
341 /*
342 * NOTE: limited by the USB ctrl message constraints
343 * Zero length reads always succeed, even if no device is connected
344 */
345
346 /* Read value */
347 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
348 if (ret < 0) {
349 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
350 addr, ret);
351 return ret;
352 }
353 /*
354 * NOTE: some devices with two i2c busses have the bad habit to return 0
355 * bytes if we are on bus B AND there was no write attempt to the
356 * specified slave address before AND no device is present at the
357 * requested slave address.
358 * Anyway, the next check will fail with -ENODEV in this case, so avoid
359 * spamming the system log on device probing and do nothing here.
360 */
361
362 /* Check success */
363 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
364 /*
365 * NOTE: the only error we've seen so far is
366 * 0x01 when the slave device is not present
367 */
368 if (!ret)
369 return len;
370 else if (ret > 0)
371 return -ENODEV;
372
373 return ret;
374 /*
375 * NOTE: With chip types (other chip IDs) which actually don't support
376 * this operation, it seems to succeed ALWAYS ! (even if there is no
377 * slave device or even no second i2c bus provided)
378 */
379}
380
381/*
382 * em25xx_bus_B_check_for_device()
383 * check if there is a i2c device at the supplied address
384 */
385static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
386{
387 u8 buf;
388 int ret;
389
390 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
391 if (ret < 0)
392 return ret;
393
394 return 0;
395 /*
396 * NOTE: With chips which do not support this operation,
397 * it seems to succeed ALWAYS ! (even if no device connected)
398 */
399}
400
401static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
402{
403 struct em28xx *dev = i2c_bus->dev;
404 int rc = -EOPNOTSUPP;
405
406 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
407 rc = em28xx_i2c_check_for_device(dev, addr);
408 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
409 rc = em2800_i2c_check_for_device(dev, addr);
410 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
411 rc = em25xx_bus_B_check_for_device(dev, addr);
412 if (rc == -ENODEV) {
413 if (i2c_debug)
414 printk(" no device\n");
415 }
416 return rc;
417}
418
419static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
420 struct i2c_msg msg)
421{
422 struct em28xx *dev = i2c_bus->dev;
423 u16 addr = msg.addr << 1;
424 int byte, rc = -EOPNOTSUPP;
425
426 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
427 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
428 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
429 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
430 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
431 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
432 if (i2c_debug) {
433 for (byte = 0; byte < msg.len; byte++)
434 printk(" %02x", msg.buf[byte]);
435 }
436 return rc;
437}
438
439static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
440 struct i2c_msg msg, int stop)
441{
442 struct em28xx *dev = i2c_bus->dev;
443 u16 addr = msg.addr << 1;
444 int byte, rc = -EOPNOTSUPP;
445
446 if (i2c_debug) {
447 for (byte = 0; byte < msg.len; byte++)
448 printk(" %02x", msg.buf[byte]);
449 }
450 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
451 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
452 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
453 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
454 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
455 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
456 return rc;
457}
458
459/*
285 * em28xx_i2c_xfer() 460 * em28xx_i2c_xfer()
286 * the main i2c transfer function 461 * the main i2c transfer function
287 */ 462 */
288static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap, 463static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
289 struct i2c_msg msgs[], int num) 464 struct i2c_msg msgs[], int num)
290{ 465{
291 struct em28xx *dev = i2c_adap->algo_data; 466 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
292 int addr, rc, i, byte; 467 struct em28xx *dev = i2c_bus->dev;
468 unsigned bus = i2c_bus->bus;
469 int addr, rc, i;
470 u8 reg;
471
472 rc = rt_mutex_trylock(&dev->i2c_bus_lock);
473 if (rc < 0)
474 return rc;
475
476 /* Switch I2C bus if needed */
477 if (bus != dev->cur_i2c_bus &&
478 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
479 if (bus == 1)
480 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
481 else
482 reg = 0;
483 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
484 EM2874_I2C_SECONDARY_BUS_SELECT);
485 dev->cur_i2c_bus = bus;
486 }
293 487
294 if (num <= 0) 488 if (num <= 0) {
489 rt_mutex_unlock(&dev->i2c_bus_lock);
295 return 0; 490 return 0;
491 }
296 for (i = 0; i < num; i++) { 492 for (i = 0; i < num; i++) {
297 addr = msgs[i].addr << 1; 493 addr = msgs[i].addr << 1;
298 dprintk2(2, "%s %s addr=%x len=%d:", 494 if (i2c_debug)
299 (msgs[i].flags & I2C_M_RD) ? "read" : "write", 495 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
300 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len); 496 dev->name, __func__ ,
497 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
498 i == num - 1 ? "stop" : "nonstop",
499 addr, msgs[i].len);
301 if (!msgs[i].len) { /* no len: check only for device presence */ 500 if (!msgs[i].len) { /* no len: check only for device presence */
302 if (dev->board.is_em2800) 501 rc = i2c_check_for_device(i2c_bus, addr);
303 rc = em2800_i2c_check_for_device(dev, addr);
304 else
305 rc = em28xx_i2c_check_for_device(dev, addr);
306 if (rc == -ENODEV) { 502 if (rc == -ENODEV) {
307 if (i2c_debug >= 2) 503 rt_mutex_unlock(&dev->i2c_bus_lock);
308 printk(" no device\n");
309 return rc; 504 return rc;
310 } 505 }
311 } else if (msgs[i].flags & I2C_M_RD) { 506 } else if (msgs[i].flags & I2C_M_RD) {
312 /* read bytes */ 507 /* read bytes */
313 if (dev->board.is_em2800) 508 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
314 rc = em2800_i2c_recv_bytes(dev, addr,
315 msgs[i].buf,
316 msgs[i].len);
317 else
318 rc = em28xx_i2c_recv_bytes(dev, addr,
319 msgs[i].buf,
320 msgs[i].len);
321 if (i2c_debug >= 2) {
322 for (byte = 0; byte < msgs[i].len; byte++)
323 printk(" %02x", msgs[i].buf[byte]);
324 }
325 } else { 509 } else {
326 /* write bytes */ 510 /* write bytes */
327 if (i2c_debug >= 2) { 511 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
328 for (byte = 0; byte < msgs[i].len; byte++)
329 printk(" %02x", msgs[i].buf[byte]);
330 }
331 if (dev->board.is_em2800)
332 rc = em2800_i2c_send_bytes(dev, addr,
333 msgs[i].buf,
334 msgs[i].len);
335 else
336 rc = em28xx_i2c_send_bytes(dev, addr,
337 msgs[i].buf,
338 msgs[i].len,
339 i == num - 1);
340 } 512 }
341 if (rc < 0) { 513 if (rc < 0) {
342 if (i2c_debug >= 2) 514 if (i2c_debug)
343 printk(" ERROR: %i\n", rc); 515 printk(" ERROR: %i\n", rc);
516 rt_mutex_unlock(&dev->i2c_bus_lock);
344 return rc; 517 return rc;
345 } 518 }
346 if (i2c_debug >= 2) 519 if (i2c_debug)
347 printk("\n"); 520 printk("\n");
348 } 521 }
349 522
523 rt_mutex_unlock(&dev->i2c_bus_lock);
350 return num; 524 return num;
351} 525}
352 526
353/* based on linux/sunrpc/svcauth.h and linux/hash.h 527/*
528 * based on linux/sunrpc/svcauth.h and linux/hash.h
354 * The original hash function returns a different value, if arch is x86_64 529 * The original hash function returns a different value, if arch is x86_64
355 * or i386. 530 * or i386.
356 */ 531 */
357static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits) 532static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
358{ 533{
@@ -375,127 +550,230 @@ static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
375 return (hash >> (32 - bits)) & 0xffffffffUL; 550 return (hash >> (32 - bits)) & 0xffffffffUL;
376} 551}
377 552
378static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) 553/*
554 * Helper function to read data blocks from i2c clients with 8 or 16 bit
555 * address width, 8 bit register width and auto incrementation been activated
556 */
557static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
558 bool addr_w16, u16 len, u8 *data)
379{ 559{
380 unsigned char buf, *p = eedata; 560 int remain = len, rsize, rsize_max, ret;
381 struct em28xx_eeprom *em_eeprom = (void *)eedata; 561 u8 buf[2];
382 int i, err, size = len, block, block_max; 562
383 563 /* Sanity check */
384 if (dev->chip_id == CHIP_ID_EM2874 || 564 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
385 dev->chip_id == CHIP_ID_EM28174 || 565 return -EINVAL;
386 dev->chip_id == CHIP_ID_EM2884) { 566 /* Select address */
387 /* Empia switched to a 16-bit addressable eeprom in newer 567 buf[0] = addr >> 8;
388 devices. While we could certainly write a routine to read 568 buf[1] = addr & 0xff;
389 the eeprom, there is nothing of use in there that cannot be 569 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
390 accessed through registers, and there is the risk that we 570 if (ret < 0)
391 could corrupt the eeprom (since a 16-bit read call is 571 return ret;
392 interpreted as a write call by 8-bit eeproms). 572 /* Read data */
393 */ 573 if (dev->board.is_em2800)
394 return 0; 574 rsize_max = 4;
575 else
576 rsize_max = 64;
577 while (remain > 0) {
578 if (remain > rsize_max)
579 rsize = rsize_max;
580 else
581 rsize = remain;
582
583 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
584 if (ret < 0)
585 return ret;
586
587 remain -= rsize;
588 data += rsize;
395 } 589 }
396 590
397 dev->i2c_client.addr = 0xa0 >> 1; 591 return len;
592}
593
594static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
595 u8 **eedata, u16 *eedata_len)
596{
597 const u16 len = 256;
598 /*
599 * FIXME common length/size for bytes to read, to display, hash
600 * calculation and returned device dataset. Simplifies the code a lot,
601 * but we might have to deal with multiple sizes in the future !
602 */
603 int i, err;
604 struct em28xx_eeprom *dev_config;
605 u8 buf, *data;
606
607 *eedata = NULL;
608 *eedata_len = 0;
609
610 /* EEPROM is always on i2c bus 0 on all known devices. */
611
612 dev->i2c_client[bus].addr = 0xa0 >> 1;
398 613
399 /* Check if board has eeprom */ 614 /* Check if board has eeprom */
400 err = i2c_master_recv(&dev->i2c_client, &buf, 0); 615 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
401 if (err < 0) { 616 if (err < 0) {
402 em28xx_errdev("board has no eeprom\n"); 617 em28xx_info("board has no eeprom\n");
403 memset(eedata, 0, len);
404 return -ENODEV; 618 return -ENODEV;
405 } 619 }
406 620
407 buf = 0; 621 data = kzalloc(len, GFP_KERNEL);
408 622 if (data == NULL)
409 err = i2c_master_send(&dev->i2c_client, &buf, 1); 623 return -ENOMEM;
410 if (err != 1) { 624
411 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n", 625 /* Read EEPROM content */
412 dev->name, err); 626 err = em28xx_i2c_read_block(dev, bus, 0x0000,
413 return err; 627 dev->eeprom_addrwidth_16bit,
628 len, data);
629 if (err != len) {
630 em28xx_errdev("failed to read eeprom (err=%d)\n", err);
631 goto error;
414 } 632 }
415 633
416 if (dev->board.is_em2800) 634 /* Display eeprom content */
417 block_max = 4;
418 else
419 block_max = 64;
420
421 while (size > 0) {
422 if (size > block_max)
423 block = block_max;
424 else
425 block = size;
426
427 if (block !=
428 (err = i2c_master_recv(&dev->i2c_client, p, block))) {
429 printk(KERN_WARNING
430 "%s: i2c eeprom read error (err=%d)\n",
431 dev->name, err);
432 return err;
433 }
434 size -= block;
435 p += block;
436 }
437 for (i = 0; i < len; i++) { 635 for (i = 0; i < len; i++) {
438 if (0 == (i % 16)) 636 if (0 == (i % 16)) {
439 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i); 637 if (dev->eeprom_addrwidth_16bit)
440 printk(" %02x", eedata[i]); 638 em28xx_info("i2c eeprom %04x:", i);
639 else
640 em28xx_info("i2c eeprom %02x:", i);
641 }
642 printk(" %02x", data[i]);
441 if (15 == (i % 16)) 643 if (15 == (i % 16))
442 printk("\n"); 644 printk("\n");
443 } 645 }
646 if (dev->eeprom_addrwidth_16bit)
647 em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
648
649 if (dev->eeprom_addrwidth_16bit &&
650 data[0] == 0x26 && data[3] == 0x00) {
651 /* new eeprom format; size 4-64kb */
652 u16 mc_start;
653 u16 hwconf_offset;
654
655 dev->hash = em28xx_hash_mem(data, len, 32);
656 mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
657
658 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
659 data[0], data[1], data[2], data[3], dev->hash);
660 em28xx_info("EEPROM info:\n");
661 em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
662 mc_start, data[2]);
663 /*
664 * boot configuration (address 0x0002):
665 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
666 * [1] always selects 12 kb RAM
667 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
668 * [4] 1 = force fast mode and no suspend for device testing
669 * [5:7] USB PHY tuning registers; determined by device
670 * characterization
671 */
672
673 /*
674 * Read hardware config dataset offset from address
675 * (microcode start + 46)
676 */
677 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
678 data);
679 if (err != 2) {
680 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
681 err);
682 goto error;
683 }
444 684
445 if (em_eeprom->id == 0x9567eb1a) 685 /* Calculate hardware config dataset start address */
446 dev->hash = em28xx_hash_mem(eedata, len, 32); 686 hwconf_offset = mc_start + data[0] + (data[1] << 8);
687
688 /* Read hardware config dataset */
689 /*
690 * NOTE: the microcode copy can be multiple pages long, but
691 * we assume the hardware config dataset is the same as in
692 * the old eeprom and not longer than 256 bytes.
693 * tveeprom is currently also limited to 256 bytes.
694 */
695 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
696 data);
697 if (err != len) {
698 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
699 err);
700 goto error;
701 }
447 702
448 printk(KERN_INFO "%s: EEPROM ID= 0x%08x, EEPROM hash = 0x%08lx\n", 703 /* Verify hardware config dataset */
449 dev->name, em_eeprom->id, dev->hash); 704 /* NOTE: not all devices provide this type of dataset */
705 if (data[0] != 0x1a || data[1] != 0xeb ||
706 data[2] != 0x67 || data[3] != 0x95) {
707 em28xx_info("\tno hardware configuration dataset found in eeprom\n");
708 kfree(data);
709 return 0;
710 }
450 711
451 printk(KERN_INFO "%s: EEPROM info:\n", dev->name); 712 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
713
714 } else if (!dev->eeprom_addrwidth_16bit &&
715 data[0] == 0x1a && data[1] == 0xeb &&
716 data[2] == 0x67 && data[3] == 0x95) {
717 dev->hash = em28xx_hash_mem(data, len, 32);
718 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
719 data[0], data[1], data[2], data[3], dev->hash);
720 em28xx_info("EEPROM info:\n");
721 } else {
722 em28xx_info("unknown eeprom format or eeprom corrupted !\n");
723 err = -ENODEV;
724 goto error;
725 }
452 726
453 switch (em_eeprom->chip_conf >> 4 & 0x3) { 727 *eedata = data;
728 *eedata_len = len;
729 dev_config = (void *)eedata;
730
731 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
454 case 0: 732 case 0:
455 printk(KERN_INFO "%s:\tNo audio on board.\n", dev->name); 733 em28xx_info("\tNo audio on board.\n");
456 break; 734 break;
457 case 1: 735 case 1:
458 printk(KERN_INFO "%s:\tAC97 audio (5 sample rates)\n", 736 em28xx_info("\tAC97 audio (5 sample rates)\n");
459 dev->name);
460 break; 737 break;
461 case 2: 738 case 2:
462 printk(KERN_INFO "%s:\tI2S audio, sample rate=32k\n", 739 em28xx_info("\tI2S audio, sample rate=32k\n");
463 dev->name);
464 break; 740 break;
465 case 3: 741 case 3:
466 printk(KERN_INFO "%s:\tI2S audio, 3 sample rates\n", 742 em28xx_info("\tI2S audio, 3 sample rates\n");
467 dev->name);
468 break; 743 break;
469 } 744 }
470 745
471 if (em_eeprom->chip_conf & 1 << 3) 746 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
472 printk(KERN_INFO "%s:\tUSB Remote wakeup capable\n", dev->name); 747 em28xx_info("\tUSB Remote wakeup capable\n");
473 748
474 if (em_eeprom->chip_conf & 1 << 2) 749 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
475 printk(KERN_INFO "%s:\tUSB Self power capable\n", dev->name); 750 em28xx_info("\tUSB Self power capable\n");
476 751
477 switch (em_eeprom->chip_conf & 0x3) { 752 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
478 case 0: 753 case 0:
479 printk(KERN_INFO "%s:\t500mA max power\n", dev->name); 754 em28xx_info("\t500mA max power\n");
480 break; 755 break;
481 case 1: 756 case 1:
482 printk(KERN_INFO "%s:\t400mA max power\n", dev->name); 757 em28xx_info("\t400mA max power\n");
483 break; 758 break;
484 case 2: 759 case 2:
485 printk(KERN_INFO "%s:\t300mA max power\n", dev->name); 760 em28xx_info("\t300mA max power\n");
486 break; 761 break;
487 case 3: 762 case 3:
488 printk(KERN_INFO "%s:\t200mA max power\n", dev->name); 763 em28xx_info("\t200mA max power\n");
489 break; 764 break;
490 } 765 }
491 printk(KERN_INFO "%s:\tTable at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n", 766 em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
492 dev->name, 767 dev_config->string_idx_table,
493 em_eeprom->string_idx_table, 768 le16_to_cpu(dev_config->string1),
494 em_eeprom->string1, 769 le16_to_cpu(dev_config->string2),
495 em_eeprom->string2, 770 le16_to_cpu(dev_config->string3));
496 em_eeprom->string3);
497 771
498 return 0; 772 return 0;
773
774error:
775 kfree(data);
776 return err;
499} 777}
500 778
501/* ----------------------------------------------------------- */ 779/* ----------------------------------------------------------- */
@@ -503,13 +781,20 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
503/* 781/*
504 * functionality() 782 * functionality()
505 */ 783 */
506static u32 functionality(struct i2c_adapter *adap) 784static u32 functionality(struct i2c_adapter *i2c_adap)
507{ 785{
508 struct em28xx *dev = adap->algo_data; 786 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
509 u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 787
510 if (dev->board.is_em2800) 788 if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
511 func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA; 789 (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
512 return func_flags; 790 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
791 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
792 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
793 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
794 }
795
796 WARN(1, "Unknown i2c bus algorithm.\n");
797 return 0;
513} 798}
514 799
515static struct i2c_algorithm em28xx_algo = { 800static struct i2c_algorithm em28xx_algo = {
@@ -556,7 +841,7 @@ static char *i2c_devs[128] = {
556 * do_i2c_scan() 841 * do_i2c_scan()
557 * check i2c address range for devices 842 * check i2c address range for devices
558 */ 843 */
559void em28xx_do_i2c_scan(struct em28xx *dev) 844void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
560{ 845{
561 u8 i2c_devicelist[128]; 846 u8 i2c_devicelist[128];
562 unsigned char buf; 847 unsigned char buf;
@@ -565,55 +850,68 @@ void em28xx_do_i2c_scan(struct em28xx *dev)
565 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist)); 850 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
566 851
567 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { 852 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
568 dev->i2c_client.addr = i; 853 dev->i2c_client[bus].addr = i;
569 rc = i2c_master_recv(&dev->i2c_client, &buf, 0); 854 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
570 if (rc < 0) 855 if (rc < 0)
571 continue; 856 continue;
572 i2c_devicelist[i] = i; 857 i2c_devicelist[i] = i;
573 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", 858 em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
574 dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 859 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
575 } 860 }
576 861
577 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, 862 if (bus == dev->def_i2c_bus)
578 ARRAY_SIZE(i2c_devicelist), 32); 863 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
864 ARRAY_SIZE(i2c_devicelist), 32);
579} 865}
580 866
581/* 867/*
582 * em28xx_i2c_register() 868 * em28xx_i2c_register()
583 * register i2c bus 869 * register i2c bus
584 */ 870 */
585int em28xx_i2c_register(struct em28xx *dev) 871int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
872 enum em28xx_i2c_algo_type algo_type)
586{ 873{
587 int retval; 874 int retval;
588 875
589 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg); 876 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
590 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req); 877 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
591 dev->i2c_adap = em28xx_adap_template;
592 dev->i2c_adap.dev.parent = &dev->udev->dev;
593 strcpy(dev->i2c_adap.name, dev->name);
594 dev->i2c_adap.algo_data = dev;
595 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
596 878
597 retval = i2c_add_adapter(&dev->i2c_adap); 879 if (bus >= NUM_I2C_BUSES)
880 return -ENODEV;
881
882 dev->i2c_adap[bus] = em28xx_adap_template;
883 dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
884 strcpy(dev->i2c_adap[bus].name, dev->name);
885
886 dev->i2c_bus[bus].bus = bus;
887 dev->i2c_bus[bus].algo_type = algo_type;
888 dev->i2c_bus[bus].dev = dev;
889 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
890 i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
891
892 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
598 if (retval < 0) { 893 if (retval < 0) {
599 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n", 894 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
600 __func__, retval); 895 __func__, retval);
601 return retval; 896 return retval;
602 } 897 }
603 898
604 dev->i2c_client = em28xx_client_template; 899 dev->i2c_client[bus] = em28xx_client_template;
605 dev->i2c_client.adapter = &dev->i2c_adap; 900 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
606 901
607 retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata)); 902 /* Up to now, all eeproms are at bus 0 */
608 if ((retval < 0) && (retval != -ENODEV)) { 903 if (!bus) {
609 em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n", 904 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
610 __func__, retval); 905 if ((retval < 0) && (retval != -ENODEV)) {
906 em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
907 __func__, retval);
611 908
612 return retval; 909 return retval;
910 }
613 } 911 }
614 912
615 if (i2c_scan) 913 if (i2c_scan)
616 em28xx_do_i2c_scan(dev); 914 em28xx_do_i2c_scan(dev, bus);
617 915
618 return 0; 916 return 0;
619} 917}
@@ -622,8 +920,11 @@ int em28xx_i2c_register(struct em28xx *dev)
622 * em28xx_i2c_unregister() 920 * em28xx_i2c_unregister()
623 * unregister i2c_bus 921 * unregister i2c_bus
624 */ 922 */
625int em28xx_i2c_unregister(struct em28xx *dev) 923int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
626{ 924{
627 i2c_del_adapter(&dev->i2c_adap); 925 if (bus >= NUM_I2C_BUSES)
926 return -ENODEV;
927
928 i2c_del_adapter(&dev->i2c_adap[bus]);
628 return 0; 929 return 0;
629} 930}