From db23295f8d2a1ecc08f279c3f295558cc55fbeb5 Mon Sep 17 00:00:00 2001 From: Angel Pons Date: Sun, 2 May 2021 18:56:45 +0200 Subject: lspcon_i2c_spi: Extract I2C bus parameter handling Introduce the `i2c_open_from_programmer_params` function to avoid having to duplicate parameter parsing code on all I2C programmers. This also allows having the same programmer parameters on all I2C programmers. Change-Id: I006b311c88feea37fe4b217f769b21ca1505def9 Signed-off-by: Angel Pons Reviewed-on: https://review.coreboot.org/c/flashrom/+/52830 Tested-by: build bot (Jenkins) Reviewed-by: Edward O'Callaghan --- i2c_helper_linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'i2c_helper_linux.c') diff --git a/i2c_helper_linux.c b/i2c_helper_linux.c index 963c3996..15f8f2a3 100644 --- a/i2c_helper_linux.c +++ b/i2c_helper_linux.c @@ -27,6 +27,7 @@ #include "flash.h" #include "i2c_helper.h" +#include "programmer.h" /* Null characters are placeholders for bus number digits */ #define I2C_DEV_PREFIX "/dev/i2c-\0\0\0" @@ -76,6 +77,52 @@ int i2c_open(int bus, uint16_t addr, int force) return i2c_open_path(dev, addr, force); } +static int get_bus_number(char *bus_str) +{ + char *bus_suffix; + errno = 0; + int bus = (int)strtol(bus_str, &bus_suffix, 10); + if (errno != 0 || bus_str == bus_suffix) { + msg_perr("%s: Could not convert 'bus'.\n", __func__); + return -1; + } + + if (strlen(bus_suffix) > 0) { + msg_perr("%s: Garbage following 'bus' value.\n", __func__); + return -1; + } + + msg_pinfo("Using I2C bus %d.\n", bus); + return bus; +} + +int i2c_open_from_programmer_params(uint16_t addr, int force) +{ + int fd = -1; + + char *bus_str = extract_programmer_param("bus"); + char *device_path = extract_programmer_param("devpath"); + + if (device_path != NULL && bus_str != NULL) { + msg_perr("%s: only one of bus and devpath may be specified\n", __func__); + goto out; + } + if (device_path == NULL && bus_str == NULL) { + msg_perr("%s: one of bus and devpath must be specified\n", __func__); + goto out; + } + + if (device_path != NULL) + fd = i2c_open_path(device_path, addr, force); + else + fd = i2c_open(get_bus_number(bus_str), addr, force); + +out: + free(bus_str); + free(device_path); + return fd; +} + int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf) { if (buf->len == 0) -- cgit v1.2.3