Skip to contentSkip to conversation

chipidea: Fix issue in reconnecing gadget without insmod/rmmod

PATCH In review Niranjan Dighe 3 participants 6 messages Apr 11, 2017 – Apr 14, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chipidea: Fix issue in reconnecing gadget without insmod/rmmod

Currently usb_gadget_connect() is called only through gadget registration via composite_driver_probe(). As a result, after a disconnection, if the role transitions to host and back to gadget, the gadget is not recognized by the host anymore.
This is a typical scenario with an iAP device in the following usecase -
conn iAP dev -> send roleswitch command -> roleswitch ourself to gadget -> disconnect iAP device -> roleswitch back to host to enumerate the device again -> reconnect device -> resend the roleswitch command -> roleswitch ourself to gadget -> ISSUE.

To workaround this, do the following -

1. Restart OTG FSM on SLi interrupt and on switching role to gadget so that device transitions to B_IDLE state from B_PERIPHERAL state. A transition from B_IDLE to B_PERIPHERAL is needed to enable interrups and restore the correct state of the chipidea controller so that communication with host is possible

2. usb_gadget_connect() after roleswitch to gadget so that gadget->ops->pullup() is called and D+ line is asserted. This causes host to "see" the device and enumeration can happen.

---
 drivers/usb/chipidea/debug.c | 10 +++++++++-
 drivers/usb/chipidea/udc.c   |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/usb/chipidea/debug.c +9 -1
index a4f7db2..66c1485 100644
@@ -16,7 +16,7 @@
 #include "udc.h"
 #include "bits.h"
 #include "otg.h"
-
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
 /**
  * ci_device_show: prints information about device capabilities and status
  */
@@ -325,6 +325,14 @@ static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
 	ci_role_stop(ci);
 	ret = ci_role_start(ci, role);
 	enable_irq(ci->irq);
+
+	/* REVISIT - Avoid repeated FSM restart*/
+
+	if (role == CI_ROLE_GADGET) {
+		ci_hdrc_otg_fsm_restart(ci);
+		usb_gadget_connect(&ci->gadget);
+	}
+
 	pm_runtime_put_sync(ci->dev);
 
 	return ret ? ret : count;
drivers/usb/chipidea/udc.c +3 -0
index a0accd5..1e3b827 100644
@@ -29,6 +29,8 @@
 #include "otg.h"
 #include "otg_fsm.h"
 
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
+
 /* control endpoint description */
 static const struct usb_endpoint_descriptor
 ctrl_endpt_out_desc = {
@@ -1881,6 +1883,7 @@ static irqreturn_t udc_irq(struct ci_hdrc *ci)
 				ci->driver->suspend(&ci->gadget);
 				usb_gadget_set_state(&ci->gadget,
 						USB_STATE_SUSPENDED);
+				ci_hdrc_otg_fsm_restart(ci);
 				spin_lock(&ci->lock);
 			}
 		}
Signature · 1 line
2.1.4
#1 lore raw
reproduce:

Hi Niranjan,

[auto build test ERROR on usb/usb-testing]
[also build test ERROR on v4.11-rc6]
[cannot apply to next-20170411]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Niranjan-Dighe/chipidea-Fix-issue-in-reconnecing-gadget-without-insmod-rmmod/20170411-235845
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
config: x86_64-acpi-redef (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

drivers/built-in.o: In function `ci_role_write':

>> debug.c:(.text+0x56ca41): undefined reference to `ci_hdrc_otg_fsm_restart'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Niranjan Dighe
reproduce:

Hi Niranjan,

[auto build test ERROR on usb/usb-testing]
[also build test ERROR on v4.11-rc6]
[cannot apply to next-20170411]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Niranjan-Dighe/chipidea-Fix-issue-in-reconnecing-gadget-without-insmod-rmmod/20170411-235845
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
config: i386-randconfig-x0-04111920 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> ERROR: "ci_hdrc_otg_fsm_restart" [drivers/usb/chipidea/ci_hdrc.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw
In reply to Niranjan Dighe
Quoted 72 lines · Niranjan Dighe

Currently usb_gadget_connect() is called only through gadget registration via composite_driver_probe(). As a result, after a disconnection, if the role transitions to host and back to gadget, the gadget is not recognized by the host anymore.
This is a typical scenario with an iAP device in the following usecase -
conn iAP dev -> send roleswitch command -> roleswitch ourself to gadget -> disconnect iAP device -> roleswitch back to host to enumerate the device again -> reconnect device -> resend the roleswitch command -> roleswitch ourself to gadget -> ISSUE.

To workaround this, do the following -

1. Restart OTG FSM on SLi interrupt and on switching role to gadget so that device transitions to B_IDLE state from B_PERIPHERAL state. A transition from B_IDLE to B_PERIPHERAL is needed to enable interrups and restore the correct state of the chipidea controller so that communication with host is possible

2. usb_gadget_connect() after roleswitch to gadget so that gadget->ops->pullup() is called and D+ line is asserted. This causes host to "see" the device and enumeration can happen.

---
 drivers/usb/chipidea/debug.c | 10 +++++++++-
 drivers/usb/chipidea/udc.c   |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/usb/chipidea/debug.c +9 -1
index a4f7db2..66c1485 100644
@@ -16,7 +16,7 @@
 #include "udc.h"
 #include "bits.h"
 #include "otg.h"
-
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
 /**
  * ci_device_show: prints information about device capabilities and status
  */
@@ -325,6 +325,14 @@ static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
 	ci_role_stop(ci);
 	ret = ci_role_start(ci, role);
 	enable_irq(ci->irq);
+
+	/* REVISIT - Avoid repeated FSM restart*/
+
+	if (role == CI_ROLE_GADGET) {
+		ci_hdrc_otg_fsm_restart(ci);
+		usb_gadget_connect(&ci->gadget);
+	}
+
 	pm_runtime_put_sync(ci->dev);
 
 	return ret ? ret : count;
drivers/usb/chipidea/udc.c +2 -0
index a0accd5..1e3b827 100644
@@ -29,6 +29,8 @@
 #include "otg.h"
 #include "otg_fsm.h"
 
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
+
 /* control endpoint description */
 static const struct usb_endpoint_descriptor
 ctrl_endpt_out_desc = {

drivers/usb/chipidea/udc.c static irqreturn_t udc_irq(struct ci_hdrc *ci) · line 1889

Quoted 7 lines
drivers/usb/chipidea/udc.c +1 -0
@@ -1881,6 +1883,7 @@ static irqreturn_t udc_irq(struct ci_hdrc *ci)
 				ci->driver->suspend(&ci->gadget);
 				usb_gadget_set_state(&ci->gadget,
 						USB_STATE_SUSPENDED);
+				ci_hdrc_otg_fsm_restart(ci);
 				spin_lock(&ci->lock);
 			}
 		}

Hi Niranjan,

When working with iAP device, there are two role-switch methods
- Through OTG FSM, and using sysfs entries under /sys/bus/platform/devices/ci_hdrc.0/inputs
but you may need to patch code to keep vbus always on for A-device, it is not compliance with OTG spec.
- Using role interface under debugfs (I move it under sysfs for v4.12). But you need to patch the code and let the A-device switch back to host after iAP is disconnected.

You don't need to use above two methods together, I suggest using the 2nd method since OTG FSM is hard to maintain due to no mandatory use case for it.

Signature · 2 lines
Best Regards,
Peter Chen
#4 lore raw
Quoted 92 lines · Peter Chen
Quoted 71 lines · Niranjan Dighe

Currently usb_gadget_connect() is called only through gadget registration via composite_driver_probe(). As a result, after a disconnection, if the role transitions to host and back to gadget, the gadget is not recognized by the host anymore.
This is a typical scenario with an iAP device in the following usecase -
conn iAP dev -> send roleswitch command -> roleswitch ourself to gadget -> disconnect iAP device -> roleswitch back to host to enumerate the device again -> reconnect device -> resend the roleswitch command -> roleswitch ourself to gadget -> ISSUE.

To workaround this, do the following -

1. Restart OTG FSM on SLi interrupt and on switching role to gadget so that device transitions to B_IDLE state from B_PERIPHERAL state. A transition from B_IDLE to B_PERIPHERAL is needed to enable interrups and restore the correct state of the chipidea controller so that communication with host is possible

2. usb_gadget_connect() after roleswitch to gadget so that gadget->ops->pullup() is called and D+ line is asserted. This causes host to "see" the device and enumeration can happen.

---
 drivers/usb/chipidea/debug.c | 10 +++++++++-
 drivers/usb/chipidea/udc.c   |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/usb/chipidea/debug.c +9 -1
index a4f7db2..66c1485 100644
@@ -16,7 +16,7 @@
 #include "udc.h"
 #include "bits.h"
 #include "otg.h"
-
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
 /**
  * ci_device_show: prints information about device capabilities and status
  */
@@ -325,6 +325,14 @@ static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
      ci_role_stop(ci);
      ret = ci_role_start(ci, role);
      enable_irq(ci->irq);
+
+     /* REVISIT - Avoid repeated FSM restart*/
+
+     if (role == CI_ROLE_GADGET) {
+             ci_hdrc_otg_fsm_restart(ci);
+             usb_gadget_connect(&ci->gadget);
+     }
+
      pm_runtime_put_sync(ci->dev);
 
      return ret ? ret : count;
drivers/usb/chipidea/udc.c +2 -0
index a0accd5..1e3b827 100644
@@ -29,6 +29,8 @@
 #include "otg.h"
 #include "otg_fsm.h"
 
+extern void ci_hdrc_otg_fsm_restart(struct ci_hdrc *);
+
 /* control endpoint description */
 static const struct usb_endpoint_descriptor
 ctrl_endpt_out_desc = {

drivers/usb/chipidea/udc.c static irqreturn_t udc_irq(struct ci_hdrc *ci)

Quoted 8 lines
drivers/usb/chipidea/udc.c +1 -1
@@ -1881,6 +1883,7 @@ static irqreturn_t udc_irq(struct ci_hdrc *ci)
                              ci->driver->suspend(&ci->gadget);
                              usb_gadget_set_state(&ci->gadget,
                                              USB_STATE_SUSPENDED);
+                             ci_hdrc_otg_fsm_restart(ci);
                              spin_lock(&ci->lock);
                      }
              }
--

Hi Niranjan,

When working with iAP device, there are two role-switch methods
- Through OTG FSM, and using sysfs entries under /sys/bus/platform/devices/ci_hdrc.0/inputs
but you may need to patch code to keep vbus always on for A-device, it is not compliance with OTG spec.
- Using role interface under debugfs (I move it under sysfs for v4.12). But you need to patch the code and let the A-device switch back to host after iAP is disconnected.

Quoted 8 lines

You don't need to use above two methods together, I suggest using the 2nd method since OTG FSM is hard to maintain due to no mandatory use case for it.

--

Best Regards,
Peter Chen

Thank you Peter for your response.

Yes, I will try to switch the role on disconnection of iAP device to Host. If I understand correctly I have to do something like this -

ci_role_stop(ci); //Gadget role stop ret = ci_role_start(ci, role); //Host role start

correct?

Regards,
Niranjan

#5 lore raw
Quoted 20 lines · Niranjan Dighe
Quoted 11 lines

Hi Niranjan,

When working with iAP device, there are two role-switch methods
- Through OTG FSM, and using sysfs entries under /sys/bus/platform/devices/ci_hdrc.0/inputs
but you may need to patch code to keep vbus always on for A-device, it is not compliance with OTG spec.
- Using role interface under debugfs (I move it under sysfs for v4.12). But you need to patch the code and let the A-device switch back to host after iAP is disconnected.

Quoted 8 lines

You don't need to use above two methods together, I suggest using the 2nd method since OTG FSM is hard to maintain due to no mandatory use case for it.

--

Best Regards,
Peter Chen

Quoted 8 lines

Thank you Peter for your response.

Yes, I will try to switch the role on disconnection of iAP device to Host. If I understand correctly I have to do something like this -

ci_role_stop(ci); //Gadget role stop ret = ci_role_start(ci, role); //Host role start

Applying below patch [1] first.

- When your app detects the board needs to switch role to gadget, do echo gadget > /sys/bus/platform/devices/ci_hdrc.0/role
- When your app detects the iAP devices is disconnected from the board, do echo host > /sys/bus/platform/devices/ci_hdrc.0/role

[1] https://git.kernel.org/pub/scm/linux/kernel/git/peter.chen/usb.git/commit/?h=ci-for-usb-next

Signature · 2 lines
Best Regards,
Peter Chen