em28xx: i2c bug fixes and cleanups
Subsystems (2)
v3 cover em28xx: i2c bug fixes and cleanups
This patch series contains some I2C bug fixes / cleanups / unifications and improvements for the em28xx driver, which I've made while working on adding support for the em25xx/em276x i2c bus B support and playing with the Terratec Cinergy 200 USB which I've got recently.
Patches 1, 2, 3, 5 fix some bugs, patch 4 is a cleanup/unification patch. Patch 2 actually fixes 2 bugs, but separate commits didn't make sense, because more or less the whole function had to be rewritten.
Changelog:
v2: - removed i2c address type/range check from patch 5 as requested by Antti Palosaari
v3: - removed patch 1 (already applied)
- added comment that explains the reasons for the i2c message size restrictions on em28xx devices
- added another patch that fixes eeprom reading with the em2800
Frank Schäfer (5):
em28xx: respect the message size constraints for i2c transfers
em28xx: fix two severe bugs in function em2800_i2c_recv_bytes()
em28xx: fix the i2c adapter functionality flags
em28xx: fix+improve+unify i2c error handling, debug messages and code
comments
em28xx: consider the message length limitation of the i2c adapter
when reading the eeprom
drivers/media/usb/em28xx/em28xx-core.c | 5 +- drivers/media/usb/em28xx/em28xx-i2c.c | 276 ++++++++++++++++++++------------ drivers/media/usb/em28xx/em28xx.h | 2 +-
3 Dateien geändert, 176 Zeilen hinzugefügt(+), 107 Zeilen entfernt(-)
Signature · 1 line
1.7.10.4
PATCH v3 1/5 em28xx: respect the message size constraints for i2c transfers
The em2800 can transfer up to 4 bytes per i2c message.
All other em25xx/em27xx/28xx chips can transfer at least 64 bytes per message.
I2C adapters should never split messages transferred via the I2C subsystem into multiple message transfers, because the result will almost always NOT be the same as when the whole data is transferred to the I2C client in a single message.
If the message size exceeds the capabilities of the I2C adapter, -EOPNOTSUPP should be returned.
--- drivers/media/usb/em28xx/em28xx-i2c.c | 44 ++++++++++++++-------------------
1 Datei geändert, 18 Zeilen hinzugefügt(+), 26 Zeilen entfernt(-)
drivers/media/usb/em28xx/em28xx-i2c.c +18 -26
@@ -50,14 +50,18 @@ do { \ }while (0 )/* -* em2800_i2c_send_max4() -* send up to 4 bytes to the i2c device +* em2800_i2c_send_bytes() +* send up to 4 bytes to the em2800 i2c device */ -static int em2800_i2c_send_max4(struct em28xx *dev, u8 addr, u8 *buf, u16 len) +static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) {int ret;int write_timeout;u8 b2[6 ]; + +if (len <1 || len >4 ) +return -EOPNOTSUPP ; +BUG_ON (len <1 || len >4 ); b2[5 ] =0x80 + len -1 ; b2[4 ] = addr;
@@ -86,29 +90,6 @@ static int em2800_i2c_send_max4(struct em28xx *dev, u8 addr, u8 *buf, u16 len) }/* -* em2800_i2c_send_bytes() -*/ -static int em2800_i2c_send_bytes (struct em28xx *dev,u8 addr,u8 *buf,u16 len) -{ -u8 *bufPtr = buf; -int ret; -int wrcount =0 ; -int count; -int maxLen =4 ; -while (len >0 ) { - count = (len > maxLen) ? maxLen : len; - ret =em2800_i2c_send_max4 (dev, addr, bufPtr, count); -if (ret >0 ) { - len -= count; - bufPtr += count; - wrcount += count; - }else -return (ret <0 ) ? ret : -EFAULT ; - } -return wrcount; -} - -/* * em2800_i2c_check_for_device() * check if there is a i2c_device at the supplied address */
@@ -150,6 +131,10 @@ static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)static int em2800_i2c_recv_bytes (struct em28xx *dev,u8 addr,u8 *buf,u16 len) {int ret; + +if (len <1 || len >4 ) +return -EOPNOTSUPP ; +/* check for the device and set i2c read address */ ret =em2800_i2c_check_for_device (dev, addr);if (ret) {
@@ -176,6 +161,9 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,int wrcount =0 ;int write_timeout, ret; +if (len <1 || len >64 ) +return -EOPNOTSUPP ; + wrcount = dev->em28xx_write_regs_req (dev, stop ?2 :3 , addr, buf, len);/* Seems to be required after a write */
@@ -197,6 +185,10 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,static int em28xx_i2c_recv_bytes (struct em28xx *dev,u16 addr,u8 *buf,u16 len) {int ret; + +if (len <1 || len >64 ) +return -EOPNOTSUPP ; + ret = dev->em28xx_read_reg_req_len (dev,2 , addr, buf, len);if (ret <0 ) {em28xx_warn ("reading i2c device failed (error=%i)\n" , ret);
Signature · 1 line
1.7.10.4
PATCH v3 2/5 em28xx: fix two severe bugs in function em2800_i2c_recv_bytes()
In reply to Frank SchäferFunction em2800_i2c_recv_bytes() has 2 severe bugs:
1) It does not wait for the i2c read to complete before reading the received message content from the bridge registers.
2) Reading more than 1 byte doesn't work
The former can result in data corruption, the latter always does.
The rewritten code also superseds the content of function em2800_i2c_check_for_device().
Tested with device "Terratec Cinergy 200 USB".
--- drivers/media/usb/em28xx/em28xx-i2c.c | 104 ++++++++++++++++++--------------- drivers/media/usb/em28xx/em28xx.h | 2 +-
2 Dateien geändert, 58 Zeilen hinzugefügt(+), 48 Zeilen entfernt(-)
drivers/media/usb/em28xx/em28xx-i2c.c +57 -47
@@ -73,12 +73,14 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)if (len >3 ) b2[0 ] = buf[3 ]; +/* trigger write */ ret = dev->em28xx_write_regs (dev,4 - len, &b2[4 - len],2 + len);if (ret !=2 + len) {em28xx_warn ("writing to i2c device failed (error=%i)\n" , ret);return -EIO ; } -for (write_timeout =EM2800_I2C_WRITE_TIMEOUT ; write_timeout >0 ; +/* wait for completion */ +for (write_timeout =EM2800_I2C_XFER_TIMEOUT ; write_timeout >0 ; write_timeout -=5 ) { ret = dev->em28xx_read_reg (dev,0x05 );if (ret ==0x80 + len -1 )
@@ -90,66 +92,74 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) }/* -* em2800_i2c_check_for_device() -* check if there is a i2c_device at the supplied address +* em2800_i2c_recv_bytes() +* read up to 4 bytes from the em2800 i2c device */ -static int em2800_i2c_check_for_device (struct em28xx *dev,u8 addr) +static int em2800_i2c_recv_bytes (struct em28xx *dev,u8 addr,u8 *buf,u16 len) { -u8 msg; +u8 buf2[4 ];int ret; -int write_timeout; - msg = addr; - ret = dev->em28xx_write_regs (dev,0x04 , &msg,1 ); -if (ret <0 ) { -em28xx_warn ("setting i2c device address failed (error=%i)\n" , - ret); -return ret; - } - msg =0x84 ; - ret = dev->em28xx_write_regs (dev,0x05 , &msg,1 ); -if (ret <0 ) { -em28xx_warn ("preparing i2c read failed (error=%i)\n" , ret); -return ret; +int read_timeout; +int i; + +if (len <1 || len >4 ) +return -EOPNOTSUPP ; + +/* trigger read */ + buf2[1 ] =0x84 + len -1 ; + buf2[0 ] = addr; + ret = dev->em28xx_write_regs (dev,0x04 , buf2,2 ); +if (ret !=2 ) { +em28xx_warn ("failed to trigger read from i2c address 0x%x " +"(error=%i)\n" , addr, ret); +return (ret <0 ) ? ret : -EIO ; } -for (write_timeout =EM2800_I2C_WRITE_TIMEOUT ; write_timeout >0 ; - write_timeout -=5 ) { -unsigned reg = dev->em28xx_read_reg (dev,0x5 ); -if (reg ==0x94 ) +/* wait for completion */ +for (read_timeout =EM2800_I2C_XFER_TIMEOUT ; read_timeout >0 ; + read_timeout -=5 ) { + ret = dev->em28xx_read_reg (dev,0x05 ); +if (ret ==0x84 + len -1 ) { +break ; + }else if (ret ==0x94 + len -1 ) {return -ENODEV ; -else if (reg ==0x84 ) -return 0 ; + }else if (ret <0 ) { +em28xx_warn ("failed to get i2c transfer status from " +"bridge register (error=%i)\n" , ret); +return ret; + }msleep (5 ); } -return -ENODEV ; +if (ret !=0x84 + len -1 ) +em28xx_warn ("read from i2c device at 0x%x timed out\n" , addr); + +/* get the received message */ + ret = dev->em28xx_read_reg_req_len (dev,0x00 ,4 -len, buf2, len); +if (ret != len) { +em28xx_warn ("reading from i2c device at 0x%x failed: " +"couldn't get the received message from the bridge " +"(error=%i)\n" , addr, ret); +return (ret <0 ) ? ret : -EIO ; + } +for (i=0 ; i<len; i++) + buf[i] = buf2[len-1 -i]; + +return ret; }/* -* em2800_i2c_recv_bytes() -* read from the i2c device +* em2800_i2c_check_for_device() +* check if there is an i2c device at the supplied address */ -static int em2800_i2c_recv_bytes (struct em28xx *dev,u8 addr,u8 *buf,u16 len) +static int em2800_i2c_check_for_device (struct em28xx *dev,u8 addr) { +u8 buf;int ret; -if (len <1 || len >4 ) -return -EOPNOTSUPP ; - -/* check for the device and set i2c read address */ - ret =em2800_i2c_check_for_device (dev, addr); -if (ret) { -em28xx_warn - ("preparing read at i2c address 0x%x failed (error=%i)\n" , - addr, ret); -return ret; - } - ret = dev->em28xx_read_reg_req_len (dev,0x0 ,0x3 , buf, len); -if (ret <0 ) { -em28xx_warn ("reading from i2c device at 0x%x failed (error=%i)" , - addr, ret); -return ret; - } -return ret; + ret =em2800_i2c_recv_bytes (dev, addr, &buf,1 ); +if (ret ==1 ) +return 0 ; +return (ret <0 ) ? ret : -EIO ; }/*
@@ -167,7 +177,7 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf, wrcount = dev->em28xx_write_regs_req (dev, stop ?2 :3 , addr, buf, len);/* Seems to be required after a write */ -for (write_timeout =EM2800_I2C_WRITE_TIMEOUT ; write_timeout >0 ; +for (write_timeout =EM2800_I2C_XFER_TIMEOUT ; write_timeout >0 ; write_timeout -=5 ) { ret = dev->em28xx_read_reg (dev,0x05 );if (!ret)
drivers/media/usb/em28xx/em28xx.h +1 -1
@@ -195,7 +195,7 @@*/ /* time in msecs to wait for i2c writes to finish */ -#define EM2800_I2C_WRITE_TIMEOUT 20 +#define EM2800_I2C_XFER_TIMEOUT 20enum em28xx_mode {EM28XX_SUSPEND ,
Signature · 1 line
1.7.10.4
PATCH v3 3/5 em28xx: fix the i2c adapter functionality flags
In reply to Frank SchäferI2C_FUNC_SMBUS_EMUL includes flag I2C_FUNC_SMBUS_WRITE_BLOCK_DATA which signals that up to 31 data bytes can be written to the ic2 client.
But the EM2800 supports only i2c messages with max. 4 data bytes. I2C_FUNC_IC2 should be set if a master_xfer function pointer is provided in struct i2c_algorithm.
--- drivers/media/usb/em28xx/em28xx-i2c.c | 6 +++++-
1 Datei geändert, 5 Zeilen hinzugefügt(+), 1 Zeile entfernt(-)
drivers/media/usb/em28xx/em28xx-i2c.c +5 -1
@@ -445,7 +445,11 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)*/ static u32 functionality (struct i2c_adapter *adap) { -return I2C_FUNC_SMBUS_EMUL ; +struct em28xx *dev = adap->algo_data; +u32 func_flags =I2C_FUNC_I2C |I2C_FUNC_SMBUS_EMUL ; +if (dev->board.is_em2800) + func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA ; +return func_flags; }static struct i2c_algorithm em28xx_algo = {
Signature · 1 line
1.7.10.4
PATCH v3 4/5 em28xx: fix+improve+unify i2c error handling, debug messages and code comments
In reply to Frank Schäfer- do not pass USB specific error codes to userspace/i2c-subsystem
- unify the returned error codes and make them compliant with the i2c subsystem spec
- check number of actually transferred bytes (via USB) everywehere
- fix/improve debug messages
- improve code comments
Changelog:
v2: removed i2c address type/range check as requested by Antti Palosaari
v3: added comment that explains the reasons for the message size restrictions
on em28xx devices
--- drivers/media/usb/em28xx/em28xx-core.c | 5 +- drivers/media/usb/em28xx/em28xx-i2c.c | 116 +++++++++++++++++++++++---------
2 Dateien geändert, 89 Zeilen hinzugefügt(+), 32 Zeilen entfernt(-)
drivers/media/usb/em28xx/em28xx-core.c +4 -1
@@ -101,7 +101,7 @@ int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,if (reg_debug)printk (" failed!\n" );mutex_unlock (&dev->ctrl_urb_lock); -return ret; +return usb_translate_errors (ret); }if (len)
@@ -182,6 +182,9 @@ int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf, 0x0000, reg, dev->urb_buf, len, HZ);mutex_unlock (&dev->ctrl_urb_lock); +if (ret <0 ) +return usb_translate_errors (ret); +if (dev->wait_after_write)msleep (dev->wait_after_write);
drivers/media/usb/em28xx/em28xx-i2c.c +85 -31
184 lines not on this page · raw message
Signature · 1 line
1.7.10.4
PATCH v3 5/5 em28xx: consider the message length limitation of the i2c adapter when reading the eeprom
In reply to Frank SchäferEEPROMs are currently read in blocks of 16 bytes, but the em2800 is limited to 4 bytes per read. All other chip variants support reading of max. 64 bytes at once (according to the em258x datasheet; also verified with em2710, em2882, and em28174).
Since em2800_i2c_recv_bytes() has been fixed to return with -EOPNOTSUPP when more than 4 bytes are requested, EEPROM reading with this chip is broken. It was actually broken before that change, too, it just didn't throw an error because the i2c adapter silently returned trash data (for all reads >1 byte !).
--- drivers/media/usb/em28xx/em28xx-i2c.c | 12 +++++++++---
1 Datei geändert, 9 Zeilen hinzugefügt(+), 3 Zeilen entfernt(-)
drivers/media/usb/em28xx/em28xx-i2c.c +9 -3
@@ -379,7 +379,7 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) {unsigned char buf, *p = eedata;struct em28xx_eeprom *em_eeprom = (void *)eedata; -int i, err, size = len, block; +int i, err, size = len, block, block_max; if (dev->chip_id == CHIP_ID_EM2874 || dev->chip_id == CHIP_ID_EM28174 ||
@@ -412,9 +412,15 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) dev->name, err);return err; } + +if (dev->board.is_em2800) + block_max =4 ; +else + block_max =64 ; +while (size >0 ) { -if (size >16 ) - block =16 ; +if (size > block_max) + block = block_max;else block = size;
Signature · 1 line
1.7.10.4