Skip to content

Commit 6e3c8be

Browse files
fancergregkh
authored andcommitted
usb: usb251xb: Lock i2c-bus segment the hub resides
SMBus slave configuration is activated by CFG_SEL[1:0]=0x1 pins state. This is the mode the hub is supposed to be to let this driver work correctly. But a race condition might happen right after reset is cleared due to CFG_SEL[0] pin being multiplexed with SMBus SCL function. In case if the reset pin is handled by a i2c GPIO expander, which is also placed at the same i2c-bus segment as the usb251x SMB-interface connected to, then the hub reset clearance might cause the CFG_SEL[0] being latched in unpredictable state. So sometimes the hub configuration mode might be 0x1 (as expected), but sometimes being 0x0, which doesn't imply to have the hub SMBus-slave interface activated and consequently causes this driver failure. In order to fix the problem we must make sure the GPIO-reset chip doesn't reside the same i2c-bus segment as the SMBus-interface of the hub. If it doesn't, we can safely block the segment for the time the reset is cleared to prevent anyone generating a traffic at the i2c-bus SCL lane connected to the CFG_SEL[0] pin. But if it does, nothing we can do, so just return an error. If we locked the i2c-bus segment and tried to communicate with the GPIO-expander, it would cause a deadlock. If we didn't lock the i2c-bus segment, it would randomly cause the CFG_SEL[0] bit flip. Signed-off-by: Serge Semin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 77a4946 commit 6e3c8be

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

drivers/usb/misc/usb251xb.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/delay.h>
1414
#include <linux/gpio/consumer.h>
15+
#include <linux/gpio/driver.h>
1516
#include <linux/i2c.h>
1617
#include <linux/module.h>
1718
#include <linux/nls.h>
@@ -222,18 +223,53 @@ static const struct usb251xb_data usb2517i_data = {
222223
.product_str = "USB2517i",
223224
};
224225

226+
static int usb251xb_check_dev_children(struct device *dev, void *child)
227+
{
228+
if (dev->type == &i2c_adapter_type) {
229+
return device_for_each_child(dev, child,
230+
usb251xb_check_dev_children);
231+
}
232+
233+
return (dev == child);
234+
}
235+
236+
static int usb251x_check_gpio_chip(struct usb251xb *hub)
237+
{
238+
struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset);
239+
struct i2c_adapter *adap = hub->i2c->adapter;
240+
int ret;
241+
242+
if (!hub->gpio_reset)
243+
return 0;
244+
245+
if (!gc)
246+
return -EINVAL;
247+
248+
ret = usb251xb_check_dev_children(&adap->dev, gc->parent);
249+
if (ret) {
250+
dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n");
251+
return -EINVAL;
252+
}
253+
254+
return 0;
255+
}
256+
225257
static void usb251xb_reset(struct usb251xb *hub, int state)
226258
{
227259
if (!hub->gpio_reset)
228260
return;
229261

262+
i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
263+
230264
gpiod_set_value_cansleep(hub->gpio_reset, state);
231265

232266
/* wait for hub recovery/stabilization */
233267
if (!state)
234268
usleep_range(500, 750); /* >=500us at power on */
235269
else
236270
usleep_range(1, 10); /* >=1us at power down */
271+
272+
i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
237273
}
238274

239275
static int usb251xb_connect(struct usb251xb *hub)
@@ -621,6 +657,25 @@ static int usb251xb_probe(struct usb251xb *hub)
621657
}
622658
}
623659

660+
/*
661+
* usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone
662+
* tries to work with the bus at the moment the hub reset is released,
663+
* it may cause an invalid config being latched by usb251x. Particularly
664+
* one of the config modes makes the hub loading a default registers
665+
* value without SMBus-slave interface activation. If the hub
666+
* accidentally gets this mode, this will cause the driver SMBus-
667+
* functions failure. Normally we could just lock the SMBus-segment the
668+
* hub i2c-interface resides for the device-specific reset timing. But
669+
* the GPIO controller, which is used to handle the hub reset, might be
670+
* placed at the same i2c-bus segment. In this case an error should be
671+
* returned since we can't safely use the GPIO controller to clear the
672+
* reset state (it may affect the hub configuration) and we can't lock
673+
* the i2c-bus segment (it will cause a deadlock).
674+
*/
675+
err = usb251x_check_gpio_chip(hub);
676+
if (err)
677+
return err;
678+
624679
err = usb251xb_connect(hub);
625680
if (err) {
626681
dev_err(dev, "Failed to connect hub (%d)\n", err);

0 commit comments

Comments
 (0)