From 08135c85d8e868f7eaf9cebeb356fd1b2816995f Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 23 Jul 2015 09:00:31 +0200 Subject: i2c: Fix return status of i2c dev read/write --- cpukit/dev/i2c/i2c-dev.c | 16 ++++++++++------ testsuites/libtests/i2c01/init.c | 23 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cpukit/dev/i2c/i2c-dev.c b/cpukit/dev/i2c/i2c-dev.c index 6710632e87..b00a9bdf21 100644 --- a/cpukit/dev/i2c/i2c-dev.c +++ b/cpukit/dev/i2c/i2c-dev.c @@ -43,11 +43,13 @@ static ssize_t i2c_dev_read( ssize_t n; n = (*dev->read)(dev, buffer, count, iop->offset); - if (n > 0) { + if (n >= 0) { iop->offset += n; - } - return n; + return n; + } else { + rtems_set_errno_and_return_minus_one(-n); + } } static ssize_t i2c_dev_write( @@ -60,11 +62,13 @@ static ssize_t i2c_dev_write( ssize_t n; n = (*dev->write)(dev, buffer, count, iop->offset); - if (n > 0) { + if (n >= 0) { iop->offset += n; - } - return n; + return n; + } else { + rtems_set_errno_and_return_minus_one(-n); + } } static int i2c_dev_ioctl( diff --git a/testsuites/libtests/i2c01/init.c b/testsuites/libtests/i2c01/init.c index ddce494120..0d6fc839f3 100644 --- a/testsuites/libtests/i2c01/init.c +++ b/testsuites/libtests/i2c01/init.c @@ -71,6 +71,7 @@ typedef struct { typedef struct { test_device base; + bool eio; unsigned current_address; uint8_t data[EEPROM_SIZE]; } test_device_eeprom; @@ -198,6 +199,10 @@ static int test_eeprom_transfer( i2c_msg *msg = &msgs[0]; uint32_t i; + if (dev->eio) { + return -EIO; + } + if (msg_count > 0 && (msg->flags & I2C_M_RD) == 0) { if (msg->len < 1) { return -EIO; @@ -406,7 +411,7 @@ static void test_gpio_nxp_pca9535(void) rtems_test_assert(rv == 0); } -static void test_eeprom(void) +static void test_eeprom(test_bus *bus) { int rv; int fd_in; @@ -442,6 +447,20 @@ static void test_eeprom(void) memset(&out[0], 0, sizeof(out)); + bus->eeprom.eio = true; + + errno = 0; + n = read(fd_in, &in[0], 1); + rtems_test_assert(n == -1); + rtems_test_assert(errno == EIO); + + errno = 0; + n = write(fd_out, &out[0], 1); + rtems_test_assert(n == -1); + rtems_test_assert(errno == EIO); + + bus->eeprom.eio = false; + n = read(fd_in, &in[0], sizeof(in) + 1); rtems_test_assert(n == (ssize_t) sizeof(in)); @@ -608,7 +627,7 @@ static void test(void) rtems_test_assert(bus->base.timeout == 0); test_simple_read_write(bus, fd); - test_eeprom(); + test_eeprom(bus); test_gpio_nxp_pca9535(); test_switch_nxp_pca9548a(); -- cgit v1.2.3