//
#include "assert.h"
+#include <string.h>
+#include <dirent.h>
+#include <limits.h>
#include <peripheral_io.h>
#include <system_info.h>
#include <dlog.h>
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;
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;
}