--- a/drivers/usb/misc/usbio.c +++ b/drivers/usb/misc/usbio.c @@ -83,6 +83,7 @@ * @gpios: GPIO bank descriptors * @nr_gpio_banks: Number of I2C busses * @gpios: I2C bank descriptors + * @reset_count: Number of times the bridge was reset while bound */ struct usbio_device { struct device *dev; @@ -115,6 +116,8 @@ unsigned int nr_i2c_buses; struct usbio_i2c_bus_desc i2cs[USBIO_MAX_I2CBUSES]; + + unsigned int reset_count; }; /** @@ -428,6 +431,21 @@ } EXPORT_SYMBOL_NS_GPL(usbio_get_quirks, "USBIO"); +unsigned int usbio_get_reset_count(struct auxiliary_device *adev) +{ + struct usbio_client *client = adev_to_client(adev); + struct usbio_device *usbio; + + guard(mutex)(&client->mutex); + + usbio = client->bridge; + if (!usbio) + return 0; /* Disconnected */ + + return usbio->reset_count; +} +EXPORT_SYMBOL_NS_GPL(usbio_get_reset_count, "USBIO"); + static void usbio_auxdev_release(struct device *dev) { struct auxiliary_device *adev = to_auxiliary_dev(dev); @@ -503,6 +521,47 @@ return 0; } +static int usbio_init_bridge(struct usbio_device *usbio) +{ + struct usbio_protver protver; + struct usbio_fwver fwver; + int ret; + + guard(mutex)(&usbio->ctrl_mutex); + + ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_HS, NULL, 0, NULL, 0); + if (ret < 0) + return ret; + + ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_PROTVER, NULL, 0, + &protver, sizeof(protver)); + if (ret < 0) + return ret; + + ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_FWVER, NULL, 0, + &fwver, sizeof(fwver)); + if (ret < 0) + return ret; + + ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMGPIO, NULL, 0, + usbio->gpios, sizeof(usbio->gpios)); + if (ret < 0 || ret % sizeof(struct usbio_gpio_bank_desc)) + return (ret < 0) ? ret : -EPROTO; + usbio->nr_gpio_banks = ret / sizeof(struct usbio_gpio_bank_desc); + + ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMI2C, NULL, 0, + usbio->i2cs, sizeof(usbio->i2cs)); + if (ret < 0 || ret % sizeof(struct usbio_i2c_bus_desc)) + return (ret < 0) ? ret : -EPROTO; + usbio->nr_i2c_buses = ret / sizeof(struct usbio_i2c_bus_desc); + + dev_dbg(usbio->dev, "ProtVer(BCD): %02x FwVer: %d.%d.%d.%d\n", + protver.ver, fwver.major, fwver.minor, + le16_to_cpu(fwver.patch), le16_to_cpu(fwver.build)); + + return 0; +} + static int usbio_suspend(struct usb_interface *intf, pm_message_t msg) { struct usbio_device *usbio = usb_get_intfdata(intf); @@ -519,6 +578,26 @@ return usb_submit_urb(usbio->urb, GFP_KERNEL); } +static int usbio_reset_resume(struct usb_interface *intf) +{ + struct usbio_device *usbio = usb_get_intfdata(intf); + int ret; + + ret = usb_submit_urb(usbio->urb, GFP_KERNEL); + if (ret) + return ret; + + ret = usbio_init_bridge(usbio); + if (ret) { + usb_kill_urb(usbio->urb); + return ret; + } + + usbio->reset_count++; + + return 0; +} + static void usbio_disconnect(struct usb_interface *intf) { struct usbio_device *usbio = usb_get_intfdata(intf); @@ -550,9 +629,7 @@ struct usb_device *udev = interface_to_usbdev(intf); struct usb_endpoint_descriptor *ep_in, *ep_out; struct device *dev = &intf->dev; - struct usbio_protver protver; struct usbio_device *usbio; - struct usbio_fwver fwver; int ret; usbio = devm_kzalloc(dev, sizeof(*usbio), GFP_KERNEL); @@ -623,51 +700,16 @@ goto err_free_urb; } - mutex_lock(&usbio->ctrl_mutex); - - ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_HS, NULL, 0, NULL, 0); - if (ret < 0) - goto err_unlock; - - ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_PROTVER, NULL, 0, - &protver, sizeof(protver)); - if (ret < 0) - goto err_unlock; - - ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_FWVER, NULL, 0, - &fwver, sizeof(fwver)); - if (ret < 0) - goto err_unlock; - - ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMGPIO, NULL, 0, - usbio->gpios, sizeof(usbio->gpios)); - if (ret < 0 || ret % sizeof(struct usbio_gpio_bank_desc)) { - ret = (ret < 0) ? ret : -EPROTO; - goto err_unlock; - } - usbio->nr_gpio_banks = ret / sizeof(struct usbio_gpio_bank_desc); - - ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMI2C, NULL, 0, - usbio->i2cs, sizeof(usbio->i2cs)); - if (ret < 0 || ret % sizeof(struct usbio_i2c_bus_desc)) { - ret = (ret < 0) ? ret : -EPROTO; - goto err_unlock; - } - usbio->nr_i2c_buses = ret / sizeof(struct usbio_i2c_bus_desc); - - mutex_unlock(&usbio->ctrl_mutex); - - dev_dbg(dev, "ProtVer(BCD): %02x FwVer: %d.%d.%d.%d\n", - protver.ver, fwver.major, fwver.minor, - le16_to_cpu(fwver.patch), le16_to_cpu(fwver.build)); + ret = usbio_init_bridge(usbio); + if (ret) + goto err_kill_urb; usbio_enum_gpios(usbio); usbio_enum_i2cs(usbio); return 0; -err_unlock: - mutex_unlock(&usbio->ctrl_mutex); +err_kill_urb: usb_kill_urb(usbio->urb); err_free_urb: usb_free_urb(usbio->urb); @@ -694,6 +736,7 @@ .disconnect = usbio_disconnect, .suspend = usbio_suspend, .resume = usbio_resume, + .reset_resume = usbio_reset_resume, .id_table = usbio_table, .supports_autosuspend = 1, }; --- a/drivers/gpio/gpio-usbio.c +++ b/drivers/gpio/gpio-usbio.c @@ -15,14 +15,16 @@ struct usbio_gpio_bank { u8 config[USBIO_GPIOSPERBANK]; + u32 value; u32 bitmap; }; struct usbio_gpio { - struct mutex config_mutex; /* Protects banks[x].config */ + struct mutex config_mutex; /* Protects banks[x].config and banks[x].value */ struct usbio_gpio_bank banks[USBIO_MAX_GPIOBANKS]; struct gpio_chip gc; struct auxiliary_device *adev; + unsigned int reset_count; }; static const struct acpi_device_id usbio_gpio_acpi_hids[] = { @@ -101,6 +103,13 @@ usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); + scoped_guard(mutex, &gpio->config_mutex) { + if (value) + bank->value |= BIT(pin); + else + bank->value &= ~BIT(pin); + } + gbuf.bankid = offset / USBIO_GPIOSPERBANK; gbuf.pincount = 1; gbuf.pin = pin; @@ -178,6 +187,66 @@ return usbio_gpio_update_config(gc, offset, USBIO_GPIO_PINCFG_MASK, value); } +static int usbio_gpio_restore(struct usbio_gpio *gpio) +{ + struct usbio_gpio_init ginit; + struct usbio_gpio_rw gbuf; + int ret; + + guard(mutex)(&gpio->config_mutex); + + for (unsigned int b = 0; b < USBIO_MAX_GPIOBANKS; b++) { + struct usbio_gpio_bank *bank = &gpio->banks[b]; + + for (unsigned int pin = 0; pin < USBIO_GPIOSPERBANK; pin++) { + u8 mode = bank->config[pin] & USBIO_GPIO_PINMOD_MASK; + + if (mode == USBIO_GPIO_PINMOD_INVAL) + continue; + + ginit.bankid = b; + ginit.config = bank->config[pin]; + ginit.pincount = 1; + ginit.pin = pin; + + ret = usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_INIT, + &ginit, sizeof(ginit), NULL, 0); + if (ret) + return ret; + + if (mode != USBIO_GPIO_PINMOD_OUTPUT) + continue; + + gbuf.bankid = b; + gbuf.pincount = 1; + gbuf.pin = pin; + gbuf.value = cpu_to_le32(bank->value & BIT(pin)); + + ret = usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_WRITE, + &gbuf, sizeof(gbuf), NULL, 0); + if (ret) + return ret; + } + } + + return 0; +} + +static int usbio_gpio_resume(struct device *dev) +{ + struct usbio_gpio *gpio = dev_get_drvdata(dev); + unsigned int reset_count = usbio_get_reset_count(gpio->adev); + + if (reset_count == gpio->reset_count) + return 0; + + gpio->reset_count = reset_count; + + return usbio_gpio_restore(gpio); +} + +static DEFINE_SIMPLE_DEV_PM_OPS(usbio_gpio_pm_ops, NULL, usbio_gpio_resume); + static int usbio_gpio_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *adev_id) { @@ -199,6 +268,8 @@ return ret; gpio->adev = adev; + gpio->reset_count = usbio_get_reset_count(adev); + dev_set_drvdata(dev, gpio); usbio_acpi_bind(gpio->adev, usbio_gpio_acpi_hids); @@ -238,7 +309,8 @@ static struct auxiliary_driver usbio_gpio_driver = { .name = USBIO_GPIO_CLIENT, .probe = usbio_gpio_probe, - .id_table = usbio_gpio_id_table + .id_table = usbio_gpio_id_table, + .driver.pm = pm_sleep_ptr(&usbio_gpio_pm_ops), }; module_auxiliary_driver(usbio_gpio_driver); --- a/include/linux/usb/usbio.h +++ b/include/linux/usb/usbio.h @@ -172,6 +172,7 @@ void usbio_release(struct auxiliary_device *adev); void usbio_get_txrxbuf_len(struct auxiliary_device *adev, u16 *txbuf_len, u16 *rxbuf_len); unsigned long usbio_get_quirks(struct auxiliary_device *adev); +unsigned int usbio_get_reset_count(struct auxiliary_device *adev); void usbio_acpi_bind(struct auxiliary_device *adev, const struct acpi_device_id *hids); #endif