i2c: fix thread safety
authorJunhwan Park <junhwan.park@samsung.com>
Thu, 22 Jun 2017 11:05:54 +0000 (20:05 +0900)
committersunghan <sh924.chang@samsung.com>
Wed, 20 Sep 2017 05:18:01 +0000 (14:18 +0900)
In the i2c_read / i2c_write function at the os level, a problem may
occur when the device data value is not declared. This patch is modified
to check the device data value first to ensure thread safety.

Change-Id: I7a47791939412ef8b2828526de7130276bd3a03c
Signed-off-by: Junhwan Park <junhwan.park@samsung.com>
os/drivers/i2c/i2c_read.c
os/drivers/i2c/i2c_write.c
os/drivers/i2c/i2c_writeread.c

index 6e44b20..ddb95c4 100644 (file)
@@ -89,6 +89,7 @@ int i2c_read(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *config, F
 {
        struct i2c_msg_s msg;
        unsigned int flags;
+       int ret = -1;
 
        /* 7- or 10-bit? */
 
@@ -106,8 +107,12 @@ int i2c_read(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *config, F
         * assure thread safety.
         */
 
-       I2C_SETFREQUENCY(dev, config->frequency);
-       return I2C_TRANSFER(dev, &msg, 1);
+       if (dev != 0x0) {
+               I2C_SETFREQUENCY(dev, config->frequency);
+               ret = I2C_TRANSFER(dev, &msg, 1);
+       }
+
+       return ret;
 }
 
 #endif                                                 /* CONFIG_I2C_TRANSFER */
index d17d96b..9302d0a 100644 (file)
@@ -88,6 +88,7 @@
 int i2c_write(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *config, FAR const uint8_t *buffer, int buflen)
 {
        struct i2c_msg_s msg;
+       int ret = -1;
 
        /* Setup for the transfer */
 
@@ -102,8 +103,12 @@ int i2c_write(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *config,
         * assure thread safety.
         */
 
-       I2C_SETFREQUENCY(dev, config->frequency);
-       return I2C_TRANSFER(dev, &msg, 1);
+       if (dev != 0x0) {
+               I2C_SETFREQUENCY(dev, config->frequency);
+               ret = I2C_TRANSFER(dev, &msg, 1);
+       }
+
+       return ret;
 }
 
 #endif                                                 /* CONFIG_I2C_TRANSFER */
index f1e53fb..345655d 100644 (file)
@@ -90,6 +90,7 @@ int i2c_writeread(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *conf
 {
        struct i2c_msg_s msg[2];
        unsigned int flags;
+       int ret = -1;
 
        /* 7- or 10-bit address? */
 
@@ -124,8 +125,12 @@ int i2c_writeread(FAR struct i2c_dev_s *dev, FAR const struct i2c_config_s *conf
         * assure thread safety.
         */
 
-       I2C_SETFREQUENCY(dev, config->frequency);
-       return I2C_TRANSFER(dev, msg, 2);
+       if (dev != 0x0) {
+               I2C_SETFREQUENCY(dev, config->frequency);
+               ret = I2C_TRANSFER(dev, msg, 2);
+       }
+
+       return ret;
 }
 
 #endif                                                 /* CONFIG_I2C_TRANSFER */