From 9ff486a8f97bd8ddf6a8f88f26ec9d0fc813f91d Mon Sep 17 00:00:00 2001 From: Andriy Gapon Date: Fri, 19 Aug 2022 13:06:01 +0300 Subject: [PATCH] fixup! linuxkpi: Try to unbreak linux_i2cbb - implemented pre_xfer and post_xfer glue as iicbb methods instead of direct calls; - removed unnecessary and harmful delays (and other extra logic) from iicbb methods as iicbb driver already has them; - added setting of iicbb speed based on algo_data->udelay, so that iicbb uses correct delays; - fixed target device for IICBUS_TRANSFER; - fixed return value of lkpi_i2cbb_transfer; - improved code style in lkpi_i2cbb_transfer, too long lines, variable scope, etc; - added slave address transaltion in lkpi_i2cbb_transfer, Linux uses the so called 7-bit format while FreeBSD uses the so called 8-bit format. --- sys/compat/linuxkpi/common/src/linux_i2cbb.c | 119 ++++++++++--------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/sys/compat/linuxkpi/common/src/linux_i2cbb.c b/sys/compat/linuxkpi/common/src/linux_i2cbb.c index 8dca9912a791..4020cd91d8c5 100644 --- a/sys/compat/linuxkpi/common/src/linux_i2cbb.c +++ b/sys/compat/linuxkpi/common/src/linux_i2cbb.c @@ -50,6 +50,8 @@ static void lkpi_iicbb_setscl(device_t dev, int val); static int lkpi_iicbb_getscl(device_t dev); static int lkpi_iicbb_getsda(device_t dev); static int lkpi_iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr); +static int lkpi_iicbb_pre_xfer(device_t dev); +static void lkpi_iicbb_post_xfer(device_t dev); struct lkpi_iicbb_softc { device_t iicbb; @@ -115,10 +117,17 @@ static int lkpi_iicbb_add_adapter(device_t dev, struct i2c_adapter *adapter) { struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); sc->adapter = adapter; + /* + * Set iicbb timing parameters deriving speed from the protocol delay. + */ + algo_data = adapter->algo_data; + if (algo_data->udelay != 0) + IICBUS_RESET(sc->iicbb, 1000000 / algo_data->udelay, 0, NULL); return (0); } @@ -145,6 +154,8 @@ static device_method_t lkpi_iicbb_methods[] = { DEVMETHOD(iicbb_getsda, lkpi_iicbb_getsda), DEVMETHOD(iicbb_getscl, lkpi_iicbb_getscl), DEVMETHOD(iicbb_reset, lkpi_iicbb_reset), + DEVMETHOD(iicbb_pre_xfer, lkpi_iicbb_pre_xfer), + DEVMETHOD(iicbb_post_xfer, lkpi_iicbb_post_xfer), /* lkpi_iicbb interface */ DEVMETHOD(lkpi_iic_add_adapter, lkpi_iicbb_add_adapter), @@ -171,10 +182,8 @@ lkpi_iicbb_setsda(device_t dev, int val) struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; + algo_data = sc->adapter->algo_data; algo_data->setsda(algo_data->data, val); - cpu_spinwait(); - DELAY(algo_data->udelay); } static void @@ -184,11 +193,8 @@ lkpi_iicbb_setscl(device_t dev, int val) struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); - - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; + algo_data = sc->adapter->algo_data; algo_data->setscl(algo_data->data, val); - cpu_spinwait(); - DELAY(algo_data->udelay); } static int @@ -196,27 +202,11 @@ lkpi_iicbb_getscl(device_t dev) { struct lkpi_iicbb_softc *sc; struct i2c_algo_bit_data *algo_data; - unsigned long orig_ticks; - int ret = 0; + int ret; sc = device_get_softc(dev); - - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; - - orig_ticks = ticks; - while (!ret) { - ret = algo_data->getscl(algo_data->data); - - if (ret) - break; - - if (ticks > orig_ticks + algo_data->timeout) - return (ETIMEDOUT); - - cpu_spinwait(); - DELAY(algo_data->udelay); - } - DELAY(algo_data->udelay); + algo_data = sc->adapter->algo_data; + ret = algo_data->getscl(algo_data->data); return (ret); } @@ -225,16 +215,11 @@ lkpi_iicbb_getsda(device_t dev) { struct lkpi_iicbb_softc *sc; struct i2c_algo_bit_data *algo_data; - int ret = 0; + int ret; sc = device_get_softc(dev); - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; - - cpu_spinwait(); - DELAY(algo_data->udelay); + algo_data = sc->adapter->algo_data; ret = algo_data->getsda(algo_data->data); - cpu_spinwait(); - DELAY(algo_data->udelay); return (ret); } @@ -246,49 +231,75 @@ lkpi_iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) return (0); } +static int +lkpi_iicbb_pre_xfer(device_t dev) +{ + struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; + int rc = 0; + + sc = device_get_softc(dev); + algo_data = sc->adapter->algo_data; + if (algo_data->pre_xfer != 0) + rc = algo_data->pre_xfer(sc->adapter); + return (rc); +} + +static void +lkpi_iicbb_post_xfer(device_t dev) +{ + struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; + + sc = device_get_softc(dev); + algo_data = sc->adapter->algo_data; + if (algo_data->post_xfer != NULL) + algo_data->post_xfer(sc->adapter); +} + int -lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) +lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, + int nmsgs) { - device_t child; struct iic_msg *bsd_msgs; - struct i2c_algo_bit_data *algo_data; - int i, unit, ret = 0; + int ret = ENXIO; linux_set_current(curthread); - bsd_msgs = malloc(sizeof(struct i2c_msg) * nmsgs, + bsd_msgs = malloc(sizeof(struct iic_msg) * nmsgs, M_DEVBUF, M_WAITOK | M_ZERO); - for (i = 0; i < nmsgs; i++) { - bsd_msgs[i].slave = msgs[i].addr; + for (int i = 0; i < nmsgs; i++) { + bsd_msgs[i].slave = msgs[i].addr << 1; bsd_msgs[i].len = msgs[i].len; bsd_msgs[i].buf = msgs[i].buf; - if (msgs[i].flags & I2C_M_RD) { + if (msgs[i].flags & I2C_M_RD) bsd_msgs[i].flags |= IIC_M_RD; - for (int j = 0; j < msgs[i].len; j++) - bsd_msgs[i].buf[j] = 0; - } if (msgs[i].flags & I2C_M_NOSTART) bsd_msgs[i].flags |= IIC_M_NOSTART; } - algo_data = (struct i2c_algo_bit_data *)adapter->algo_data; - unit = 0; - while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iicbb", unit++)) != NULL) { + for (int unit = 0; ; unit++) { + device_t child; + struct lkpi_iicbb_softc *sc; + + child = device_find_child(adapter->dev.parent->bsddev, + "lkpi_iicbb", unit); + if (child == NULL) + break; if (adapter == LKPI_IIC_GET_ADAPTER(child)) { - if (algo_data->pre_xfer != NULL) - algo_data->pre_xfer(adapter); - ret = IICBUS_TRANSFER(child, bsd_msgs, nmsgs); - if (algo_data->post_xfer != NULL) - algo_data->post_xfer(adapter); + sc = device_get_softc(child); + ret = IICBUS_TRANSFER(sc->iicbb, bsd_msgs, nmsgs); + ret = iic2errno(ret); + break; } } free(bsd_msgs, M_DEVBUF); - if (ret < 0) + if (ret != 0) return (-ret); - return (0); + return (nmsgs); } int -- 2.37.2