[UTC][peripheral-io][Non-ACR][Fix searching for i2c dev] 32/224932/1
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Fri, 14 Feb 2020 07:18:36 +0000 (08:18 +0100)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Fri, 14 Feb 2020 07:21:46 +0000 (08:21 +0100)
Change-Id: I7c35112c9ffa5572dbe287a9484b1cba35353030
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
src/utc/peripheral-io/utc-peripheral-io-i2c.c

index 7fae9093f413b34521d355ba6e94a0003c745e1f..fded5f2798b2de81ed7593bb75bfee81ac8e3c44 100755 (executable)
@@ -15,6 +15,9 @@
 //
 
 #include "assert.h"
+#include <string.h>
+#include <dirent.h>
+#include <limits.h>
 #include <peripheral_io.h>
 #include <system_info.h>
 #include <dlog.h>
@@ -68,6 +71,45 @@ static bool i2c_feature;
 static int  i2c_bus;
 static int  i2c_address;
 
+static int __find_stub_driver(void)
+{
+       DIR *d;
+       struct dirent *dir;
+       FILE *fp;
+       char *dev_path = "/sys/class/i2c-dev/";
+       char name_file[PATH_MAX];
+       char name[17];
+       int bus, n;
+       int ret = -1;
+
+       d = opendir(dev_path);
+       if (!d)
+               return -1;
+
+       while ((dir = readdir(d)) != NULL) {
+               if (sscanf(dir->d_name, "i2c-%d", &bus) <= 0)
+                       continue;
+
+               snprintf(name_file, PATH_MAX, "%s/i2c-%d/name", dev_path, bus);
+               fp = fopen(name_file, "r");
+               if (!fp)
+                       continue;
+
+               memset(name, 0, sizeof(name));
+               n = fread(name, sizeof(char), sizeof(name), fp);
+               fclose(fp);
+
+               if (strncmp(name, "SMBus stub driver", n) == 0) {
+                       ret = bus;
+                       goto cleanup;
+               }
+       }
+
+cleanup:
+       closedir(d);
+       return ret;
+}
+
 static int __init_board()
 {
        int ret = 0;
@@ -79,15 +121,17 @@ static int __init_board()
                return -1;
        }
 
-       if (!strcmp(model_name, PIO_TARGET_MODEL_RPI_3)) {
-               i2c_bus = I2C_BUS_RPI_3;
-               i2c_address = I2C_ADDRESS;
+       i2c_bus = __find_stub_driver();
+       if (i2c_bus == -1) {
+               dlog_print(DLOG_ERROR, PIO_LOG, "[%s:%d] Failed to find i2c stub device.", __FUNCTION__, __LINE__);
+               return -1;
+       }
 
-       } else if(!strcmp(model_name, PIO_TARGET_MODEL_ARTIK_530)) {
-               i2c_bus = I2C_BUS_ARTIK_530;
-               i2c_address = I2C_ADDRESS;
+       i2c_address = I2C_ADDRESS;
+
+       if (strcmp(model_name, PIO_TARGET_MODEL_RPI_3) != 0 &&
+               strcmp(model_name, PIO_TARGET_MODEL_ARTIK_530) != 0) {
 
-       } else {
                dlog_print(DLOG_ERROR, PIO_LOG, "[%s:%d] Unkown the board model name. ret = %d", __FUNCTION__, __LINE__, ret);
                return -1;
        }