Skip to contentSkip to conversation

usb: chipidea: replace ci_role with usb_role

PATCH Superseded [email protected] 3 participants 12 messages Jul 3, 2019 – Aug 7, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 usb: chipidea: replace ci_role with usb_role

From: Li Jun <[email protected]>

Since there is usb_role which has similar definition like ci_role, switch to use usb_role, then we can directly compare usb role with a common definition, this can benifit on usb role switch class support.

---
 drivers/usb/chipidea/ci.h      | 28 ++++++++++++----------------
 drivers/usb/chipidea/core.c    | 34 +++++++++++++++++-----------------
 drivers/usb/chipidea/debug.c   | 12 ++++++------
 drivers/usb/chipidea/host.c    |  6 +++---
 drivers/usb/chipidea/otg.c     | 14 +++++++-------
 drivers/usb/chipidea/otg.h     |  2 +-
 drivers/usb/chipidea/otg_fsm.c |  4 ++--
 drivers/usb/chipidea/udc.c     |  6 +++---
 8 files changed, 51 insertions(+), 55 deletions(-)
drivers/usb/chipidea/ci.h +12 -16
index 6a2cc5c..82b86cd 100644
@@ -16,6 +16,7 @@
 #include <linux/usb/gadget.h>
 #include <linux/usb/otg-fsm.h>
 #include <linux/usb/otg.h>
+#include <linux/usb/role.h>
 #include <linux/ulpi/interface.h>
 
 /******************************************************************************
@@ -102,12 +103,6 @@ struct ci_hw_ep {
 	struct td_node				*pending_td;
 };
 
-enum ci_role {
-	CI_ROLE_HOST = 0,
-	CI_ROLE_GADGET,
-	CI_ROLE_END,
-};
-
 enum ci_revision {
 	CI_REVISION_1X = 10,	/* Revision 1.x */
 	CI_REVISION_20 = 20, /* Revision 2.0 */
@@ -208,8 +203,8 @@ struct ci_hdrc {
 	spinlock_t			lock;
 	struct hw_bank			hw_bank;
 	int				irq;
-	struct ci_role_driver		*roles[CI_ROLE_END];
-	enum ci_role			role;
+	struct ci_role_driver		*roles[USB_ROLE_DEVICE + 1];
+	enum usb_role			role;
 	bool				is_otg;
 	struct usb_otg			otg;
 	struct otg_fsm			fsm;
@@ -258,15 +253,16 @@ struct ci_hdrc {
 
 static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
 {
-	BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
+	WARN_ON((ci->role != USB_ROLE_HOST && ci->role != USB_ROLE_DEVICE) ||
+	       !ci->roles[ci->role]);
 	return ci->roles[ci->role];
 }
 
-static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
+static inline int ci_role_start(struct ci_hdrc *ci, enum usb_role role)
 {
 	int ret;
 
-	if (role >= CI_ROLE_END)
+	if (role != USB_ROLE_HOST && role != USB_ROLE_DEVICE)
 		return -EINVAL;
 
 	if (!ci->roles[role])
@@ -280,12 +276,12 @@ static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
 
 static inline void ci_role_stop(struct ci_hdrc *ci)
 {
-	enum ci_role role = ci->role;
+	enum usb_role role = ci->role;
 
-	if (role == CI_ROLE_END)
+	if (role == USB_ROLE_NONE)
 		return;
 
-	ci->role = CI_ROLE_END;
+	ci->role = USB_ROLE_NONE;
 
 	ci->roles[role]->stop(ci);
 }
@@ -416,8 +412,8 @@ static inline bool ci_otg_is_fsm_mode(struct ci_hdrc *ci)
 #ifdef CONFIG_USB_OTG_FSM
 	struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
 
-	return ci->is_otg && ci->roles[CI_ROLE_HOST] &&
-		ci->roles[CI_ROLE_GADGET] && (otg_caps->srp_support ||
+	return ci->is_otg && ci->roles[USB_ROLE_HOST] &&
+		ci->roles[USB_ROLE_DEVICE] && (otg_caps->srp_support ||
 		otg_caps->hnp_support || otg_caps->adp_support);
 #else
 	return false;
drivers/usb/chipidea/core.c +17 -17
index 26062d6..bc24c5b 100644
@@ -581,7 +581,7 @@ static irqreturn_t ci_irq(int irq, void *data)
 	}
 
 	/* Handle device/host interrupt */
-	if (ci->role != CI_ROLE_END)
+	if (ci->role != USB_ROLE_NONE)
 		ret = ci_role(ci)->irq(ci);
 
 	return ret;
@@ -835,7 +835,7 @@ static inline void ci_role_destroy(struct ci_hdrc *ci)
 {
 	ci_hdrc_gadget_destroy(ci);
 	ci_hdrc_host_destroy(ci);
-	if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
+	if (ci->is_otg && ci->roles[USB_ROLE_DEVICE])
 		ci_hdrc_otg_destroy(ci);
 }
 
@@ -860,7 +860,7 @@ static ssize_t role_show(struct device *dev, struct device_attribute *attr,
 {
 	struct ci_hdrc *ci = dev_get_drvdata(dev);
 
-	if (ci->role != CI_ROLE_END)
+	if (ci->role != USB_ROLE_NONE)
 		return sprintf(buf, "%s\n", ci_role(ci)->name);
 
 	return 0;
@@ -870,27 +870,27 @@ static ssize_t role_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t n)
 {
 	struct ci_hdrc *ci = dev_get_drvdata(dev);
-	enum ci_role role;
+	enum usb_role role;
 	int ret;
 
-	if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) {
+	if (!(ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE])) {
 		dev_warn(dev, "Current configuration is not dual-role, quit\n");
 		return -EPERM;
 	}
 
-	for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
+	for (role = USB_ROLE_DEVICE; role > USB_ROLE_NONE; role--)
 		if (!strncmp(buf, ci->roles[role]->name,
 			     strlen(ci->roles[role]->name)))
 			break;
 
-	if (role == CI_ROLE_END || role == ci->role)
+	if (role == USB_ROLE_NONE || role == ci->role)
 		return -EINVAL;
 
 	pm_runtime_get_sync(dev);
 	disable_irq(ci->irq);
 	ci_role_stop(ci);
 	ret = ci_role_start(ci, role);
-	if (!ret && ci->role == CI_ROLE_GADGET)
+	if (!ret && ci->role == USB_ROLE_DEVICE)
 		ci_handle_vbus_change(ci);
 	enable_irq(ci->irq);
 	pm_runtime_put_sync(dev);
@@ -1037,13 +1037,13 @@ static int ci_hdrc_probe(struct platform_device *pdev)
 		}
 	}
 
-	if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
+	if (!ci->roles[USB_ROLE_HOST] && !ci->roles[USB_ROLE_DEVICE]) {
 		dev_err(dev, "no supported roles\n");
 		ret = -ENODEV;
 		goto deinit_gadget;
 	}
 
-	if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
+	if (ci->is_otg && ci->roles[USB_ROLE_DEVICE]) {
 		ret = ci_hdrc_otg_init(ci);
 		if (ret) {
 			dev_err(dev, "init otg fails, ret = %d\n", ret);
@@ -1051,7 +1051,7 @@ static int ci_hdrc_probe(struct platform_device *pdev)
 		}
 	}
 
-	if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
+	if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
 		if (ci->is_otg) {
 			ci->role = ci_otg_role(ci);
 			/* Enable ID change irq */
@@ -1062,17 +1062,17 @@ static int ci_hdrc_probe(struct platform_device *pdev)
 			 * role switch, the defalt role is gadget, and the
 			 * user can switch it through debugfs.
 			 */
-			ci->role = CI_ROLE_GADGET;
+			ci->role = USB_ROLE_DEVICE;
 		}
 	} else {
-		ci->role = ci->roles[CI_ROLE_HOST]
-			? CI_ROLE_HOST
-			: CI_ROLE_GADGET;
+		ci->role = ci->roles[USB_ROLE_HOST]
+			? USB_ROLE_HOST
+			: USB_ROLE_DEVICE;
 	}
 
 	if (!ci_otg_is_fsm_mode(ci)) {
 		/* only update vbus status for peripheral */
-		if (ci->role == CI_ROLE_GADGET)
+		if (ci->role == USB_ROLE_DEVICE)
 			ci_handle_vbus_change(ci);
 
 		ret = ci_role_start(ci, ci->role);
@@ -1115,7 +1115,7 @@ static int ci_hdrc_probe(struct platform_device *pdev)
 remove_debug:
 	dbg_remove_files(ci);
 stop:
-	if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
+	if (ci->is_otg && ci->roles[USB_ROLE_DEVICE])
 		ci_hdrc_otg_destroy(ci);
 deinit_gadget:
 	ci_hdrc_gadget_destroy(ci);
drivers/usb/chipidea/debug.c +6 -6
index fcc91a3..a3c022e 100644
@@ -124,7 +124,7 @@ static int ci_qheads_show(struct seq_file *s, void *data)
 	unsigned long flags;
 	unsigned i, j;
 
-	if (ci->role != CI_ROLE_GADGET) {
+	if (ci->role != USB_ROLE_DEVICE) {
 		seq_printf(s, "not in gadget mode\n");
 		return 0;
 	}
@@ -158,7 +158,7 @@ static int ci_requests_show(struct seq_file *s, void *data)
 	struct td_node *node, *tmpnode;
 	unsigned i, j, qsize = sizeof(struct ci_hw_td)/sizeof(u32);
 
-	if (ci->role != CI_ROLE_GADGET) {
+	if (ci->role != USB_ROLE_DEVICE) {
 		seq_printf(s, "not in gadget mode\n");
 		return 0;
 	}
@@ -251,7 +251,7 @@ static int ci_role_show(struct seq_file *s, void *data)
 {
 	struct ci_hdrc *ci = s->private;
 
-	if (ci->role != CI_ROLE_END)
+	if (ci->role != USB_ROLE_NONE)
 		seq_printf(s, "%s\n", ci_role(ci)->name);
 
 	return 0;
@@ -262,20 +262,20 @@ static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
 {
 	struct seq_file *s = file->private_data;
 	struct ci_hdrc *ci = s->private;
-	enum ci_role role;
+	enum usb_role role;
 	char buf[8];
 	int ret;
 
 	if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
 		return -EFAULT;
 
-	for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
+	for (role = USB_ROLE_DEVICE; role > USB_ROLE_NONE; role--)
 		if (ci->roles[role] &&
 		    !strncmp(buf, ci->roles[role]->name,
 			     strlen(ci->roles[role]->name)))
 			break;
 
-	if (role == CI_ROLE_END || role == ci->role)
+	if (role == USB_ROLE_NONE || role == ci->role)
 		return -EINVAL;
 
 	pm_runtime_get_sync(ci->dev);
drivers/usb/chipidea/host.c +3 -3
index b45ceb9..dcce43d 100644
@@ -198,7 +198,7 @@ static void host_stop(struct ci_hdrc *ci)
 			ci->platdata->notify_event(ci,
 				CI_HDRC_CONTROLLER_STOPPED_EVENT);
 		usb_remove_hcd(hcd);
-		ci->role = CI_ROLE_END;
+		ci->role = USB_ROLE_NONE;
 		synchronize_irq(ci->irq);
 		usb_put_hcd(hcd);
 		if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
@@ -216,7 +216,7 @@ static void host_stop(struct ci_hdrc *ci)
 
 void ci_hdrc_host_destroy(struct ci_hdrc *ci)
 {
-	if (ci->role == CI_ROLE_HOST && ci->hcd)
+	if (ci->role == USB_ROLE_HOST && ci->hcd)
 		host_stop(ci);
 }
 
@@ -362,7 +362,7 @@ int ci_hdrc_host_init(struct ci_hdrc *ci)
 	rdrv->stop	= host_stop;
 	rdrv->irq	= host_irq;
 	rdrv->name	= "host";
-	ci->roles[CI_ROLE_HOST] = rdrv;
+	ci->roles[USB_ROLE_HOST] = rdrv;
 
 	return 0;
 }
drivers/usb/chipidea/otg.c +7 -7
index f25d482..5bde0b5 100644
@@ -117,11 +117,11 @@ void hw_write_otgsc(struct ci_hdrc *ci, u32 mask, u32 data)
  * ci_otg_role - pick role based on ID pin state
  * @ci: the controller
  */
-enum ci_role ci_otg_role(struct ci_hdrc *ci)
+enum usb_role ci_otg_role(struct ci_hdrc *ci)
 {
-	enum ci_role role = hw_read_otgsc(ci, OTGSC_ID)
-		? CI_ROLE_GADGET
-		: CI_ROLE_HOST;
+	enum usb_role role = hw_read_otgsc(ci, OTGSC_ID)
+		? USB_ROLE_DEVICE
+		: USB_ROLE_HOST;
 
 	return role;
 }
@@ -164,7 +164,7 @@ static int hw_wait_vbus_lower_bsv(struct ci_hdrc *ci)
 
 static void ci_handle_id_switch(struct ci_hdrc *ci)
 {
-	enum ci_role role = ci_otg_role(ci);
+	enum usb_role role = ci_otg_role(ci);
 
 	if (role != ci->role) {
 		dev_dbg(ci->dev, "switching from %s to %s\n",
@@ -172,7 +172,7 @@ static void ci_handle_id_switch(struct ci_hdrc *ci)
 
 		ci_role_stop(ci);
 
-		if (role == CI_ROLE_GADGET &&
+		if (role == USB_ROLE_DEVICE &&
 				IS_ERR(ci->platdata->vbus_extcon.edev))
 			/*
 			 * Wait vbus lower than OTGSC_BSV before connecting
@@ -185,7 +185,7 @@ static void ci_handle_id_switch(struct ci_hdrc *ci)
 
 		ci_role_start(ci, role);
 		/* vbus change may have already occurred */
-		if (role == CI_ROLE_GADGET)
+		if (role == USB_ROLE_DEVICE)
 			ci_handle_vbus_change(ci);
 	}
 }
drivers/usb/chipidea/otg.h +1 -1
index 4f8b817..f64bfc6 100644
@@ -12,7 +12,7 @@ u32 hw_read_otgsc(struct ci_hdrc *ci, u32 mask);
 void hw_write_otgsc(struct ci_hdrc *ci, u32 mask, u32 data);
 int ci_hdrc_otg_init(struct ci_hdrc *ci);
 void ci_hdrc_otg_destroy(struct ci_hdrc *ci);
-enum ci_role ci_otg_role(struct ci_hdrc *ci);
+enum usb_role ci_otg_role(struct ci_hdrc *ci);
 void ci_handle_vbus_change(struct ci_hdrc *ci);
 static inline void ci_otg_queue_work(struct ci_hdrc *ci)
 {
drivers/usb/chipidea/otg_fsm.c +2 -2
index 6ed4b00..78c96a1 100644
@@ -547,10 +547,10 @@ static int ci_otg_start_host(struct otg_fsm *fsm, int on)
 
 	if (on) {
 		ci_role_stop(ci);
-		ci_role_start(ci, CI_ROLE_HOST);
+		ci_role_start(ci, USB_ROLE_HOST);
 	} else {
 		ci_role_stop(ci);
-		ci_role_start(ci, CI_ROLE_GADGET);
+		ci_role_start(ci, USB_ROLE_DEVICE);
 	}
 	return 0;
 }
drivers/usb/chipidea/udc.c +3 -3
index 6a5ee8e..e1d745f7 100644
@@ -1606,7 +1606,7 @@ static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
 	 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
 	 * and don't touch Data+ in host mode for dual role config.
 	 */
-	if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
+	if (ci_otg_is_fsm_mode(ci) || ci->role == USB_ROLE_DEVICE)
 		return 0;
 
 	pm_runtime_get_sync(&ci->gadget.dev);
@@ -1973,7 +1973,7 @@ static int udc_start(struct ci_hdrc *ci)
  */
 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
 {
-	if (!ci->roles[CI_ROLE_GADGET])
+	if (!ci->roles[USB_ROLE_DEVICE])
 		return;
 
 	usb_del_gadget_udc(&ci->gadget);
@@ -2039,7 +2039,7 @@ int ci_hdrc_gadget_init(struct ci_hdrc *ci)
 
 	ret = udc_start(ci);
 	if (!ret)
-		ci->roles[CI_ROLE_GADGET] = rdrv;
+		ci->roles[USB_ROLE_DEVICE] = rdrv;
 
 	return ret;
 }
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/5 usb: chipidea: add role switch class support

From: Li Jun <[email protected]>

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125 ++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
drivers/usb/chipidea/ci.h +2 -0
index 82b86cd..5e2f0bc 100644
@@ -212,6 +212,7 @@ struct ci_hdrc {
 	ktime_t				hr_timeouts[NUM_OTG_FSM_TIMERS];
 	unsigned			enabled_otg_timer_bits;
 	enum otg_fsm_timer		next_otg_timer;
+	struct usb_role_switch		*role_switch;
 	struct work_struct		work;
 	struct workqueue_struct		*wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
 	struct dentry			*debugfs;
 	bool				id_event;
 	bool				b_sess_valid_event;
+	bool				role_switch_event;
 	bool				imx28_write_fix;
 	bool				supports_runtime_pm;
 	bool				in_lpm;
drivers/usb/chipidea/core.c +96 -29

162 lines not on this page · raw message

drivers/usb/chipidea/otg.c +13 -0
index 5bde0b5..0a22855 100644
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
 		ci_handle_vbus_change(ci);
 	}
 
+	if (ci->role_switch_event) {
+		ci->role_switch_event = false;
+
+		if (ci->role == USB_ROLE_DEVICE) {
+			usb_gadget_vbus_disconnect(&ci->gadget);
+			ci_role_start(ci, USB_ROLE_HOST);
+		} else if (ci->role == USB_ROLE_HOST) {
+			ci_role_stop(ci);
+			usb_gadget_vbus_connect(&ci->gadget);
+			ci->role = USB_ROLE_DEVICE;
+		}
+	}
+
 	pm_runtime_put_sync(ci->dev);
 
 	enable_irq(ci->irq);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 4 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Then, if the Type-C cable is disconnected from PC host, the controller driver can't be notified?If that, how controller enters low power mode at this situation?

Peter

Quoted 226 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125 ++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
drivers/usb/chipidea/ci.h +2 -0
index 82b86cd..5e2f0bc 100644
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t                         hr_timeouts[NUM_OTG_FSM_TIMERS];
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
drivers/usb/chipidea/core.c +96 -29
index bc24c5b..1bcf6f6 100644
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum usb_role role)
+{
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev)
+{
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get,
+};
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)
 {
@@ -689,6 +730,9 @@ static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) {
@@ -908,6 +952,43 @@ static const struct attribute_group ci_attr_group = {
        .attrs = ci_attrs,
 };
 
+static int ci_start_initial_role(struct ci_hdrc *ci)
+{
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)
 {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct platform_device *pdev)
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
        ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
@@ -1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device *pdev)
        if (ret)
                goto stop;
 
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev);
@@ -1133,6 +1197,9 @@ static int ci_hdrc_remove(struct platform_device *pdev)
 {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
 
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev);
drivers/usb/chipidea/otg.c +13 -1
index 5bde0b5..0a22855 100644
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

author
#3 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 10 lines

-----Original Message-----
From: Peter Chen <[email protected]>
Sent: 2019年8月2日 17:41
To: Jun Li <[email protected]>
Cc: Peter Chen <[email protected]>; Greg Kroah-Hartman <[email protected]>; dl-linux-imx <[email protected]>; USB list <[email protected]>
Subject: Re: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 7 lines
Quoted 4 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Then, if the Type-C cable is disconnected from PC host, the controller driver can't be notified?If that, how controller enters low power mode at this situation?

The controller driver can be notified, but there are only role(host or device) Information, so in you mentioned case, controller driver will get the same input before and after disconnect from host, can't know detachment and enter low power mode accordingly, this is more like something internal handling under device role.

Li Jun

Quoted 224 lines

Peter

Quoted 221 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
drivers/usb/chipidea/ci.h +2 -0
index 82b86cd..5e2f0bc 100644
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t                         hr_timeouts[NUM_OTG_FSM_TIMERS];
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
drivers/usb/chipidea/core.c +91 -30
index bc24c5b..1bcf6f6 100644
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9 @@ static
 int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) { @@ -908,6
+952,43 @@ static const struct attribute_group ci_attr_group = {
        .attrs = ci_attrs,
 };
 
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct platform_device *pdev)
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
        ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device *pdev)
        if (ret)
                goto stop;
 
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6 +1197,9 @@
static int ci_hdrc_remove(struct platform_device *pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
drivers/usb/chipidea/core.c +3 -0
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index 5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +13 -1
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

#4 lore raw

Re: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Quoted 9 lines
Quoted 2 lines

Then, if the Type-C cable is disconnected from PC host, the controller driver can't be notified?If that, how controller enters low power mode at this situation?

The controller driver can be notified, but there are only role(host or device) Information, so in you mentioned case, controller driver will get the same input before and after disconnect from host, can't know detachment and enter low power mode accordingly, this is more like something internal handling under device role.

If the Type-C can't pass "gadget disconnect" event, you may handle it at UDC driver itself, eg at ci_usb_role_switch_set. Otherwise, the gadget class driver can't be notified disconnection, and the controller itself can't enter LPM as well.

Peter

Quoted 226 lines

Li Jun

Quoted 224 lines

Peter

Quoted 221 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
drivers/usb/chipidea/ci.h +2 -0
index 82b86cd..5e2f0bc 100644
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t                         hr_timeouts[NUM_OTG_FSM_TIMERS];
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
drivers/usb/chipidea/core.c +91 -30
index bc24c5b..1bcf6f6 100644
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9 @@ static
 int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) { @@ -908,6
+952,43 @@ static const struct attribute_group ci_attr_group = {
        .attrs = ci_attrs,
 };
 
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct platform_device *pdev)
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
        ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device *pdev)
        if (ret)
                goto stop;
 
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6 +1197,9 @@
static int ci_hdrc_remove(struct platform_device *pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
drivers/usb/chipidea/core.c +3 -0
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index 5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +13 -1
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

author
#5 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 27 lines

-----Original Message-----
From: Peter Chen <[email protected]>
Sent: 2019年8月5日 11:15
To: Jun Li <[email protected]>
Cc: Peter Chen <[email protected]>; Greg Kroah-Hartman <[email protected]>; dl-linux-imx <[email protected]>; USB list <[email protected]>
Subject: Re: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Then, if the Type-C cable is disconnected from PC host, the
controller driver can't be notified?If that, how controller enters low power mode at this

situation?

Quoted 7 lines

The controller driver can be notified, but there are only role(host or device) Information, so in you mentioned case, controller driver will get the same input before and after disconnect from host, can't know detachment and enter low power mode accordingly, this is more like something internal handling under device role.

Quoted 3 lines

If the Type-C can't pass "gadget disconnect" event, you may handle it at UDC driver itself, eg at ci_usb_role_switch_set. Otherwise, the gadget class driver can't be notified disconnection, and the controller itself can't enter LPM as well.

OK, I will enable VBUS irq to handle this in v2.

Li Jun

Quoted 233 lines

Peter

Quoted 13 lines

Li Jun

Quoted 11 lines

Peter

Quoted 8 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
Quoted 6 lines
Quoted 6 lines

drivers/usb/chipidea/ci.h struct ci_hdrc { · line 212

Quoted 6 lines
drivers/usb/chipidea/ci.h +0 -0
index 82b86cd..5e2f0bc 100644
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t

hr_timeouts[NUM_OTG_FSM_TIMERS];

Quoted 210 lines
Quoted 210 lines
Quoted 210 lines
drivers/usb/chipidea/ci.h +2 -0
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
(quoted diff) +0 -0

b/drivers/usb/chipidea/core.c index bc24c5b..1bcf6f6 100644

drivers/usb/chipidea/core.c +83 -30
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum
+usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9 @@
 static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) { @@
-908,6
+952,43 @@ static const struct attribute_group ci_attr_group = {
        .attrs = ci_attrs,
 };
 
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct platform_device *pdev)
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
        ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,

@@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device *pdev)

        if (ret)
                goto stop;
drivers/usb/chipidea/core.c +9 -0
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6 +1197,9
@@ static int ci_hdrc_remove(struct platform_device *pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
drivers/usb/chipidea/core.c +3 -0
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index 5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +13 -1
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

#6 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Then, if the Type-C cable is disconnected from PC host, the controller driver can't be notified?If that, how controller enters low power mode at this

situation?

Quoted 8 lines

The controller driver can be notified, but there are only role(host or
device) Information, so in you mentioned case, controller driver will get the same input before and after disconnect from host, can't know detachment and enter low power mode accordingly, this is more like something internal handling under device role.

Quoted 7 lines
Quoted 3 lines

If the Type-C can't pass "gadget disconnect" event, you may handle it at UDC driver itself, eg at ci_usb_role_switch_set. Otherwise, the gadget class driver can't be notified disconnection, and the controller itself can't

enter LPM as well.

OK, I will enable VBUS irq to handle this in v2.

You may use connect bit at portsc since VBUS may not connect to SoC.

Peter

Quoted 236 lines

Li Jun

Quoted 133 lines

Peter

Quoted 13 lines

Li Jun

Quoted 11 lines

Peter

Quoted 8 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
Quoted 6 lines
Quoted 6 lines

drivers/usb/chipidea/ci.h struct ci_hdrc { · line 212

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/usb/chipidea/ci.h index 82b86cd..5e2f0bc 100644

drivers/usb/chipidea/ci.h +0 -0
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t

hr_timeouts[NUM_OTG_FSM_TIMERS];

Quoted 110 lines
Quoted 110 lines
Quoted 110 lines
drivers/usb/chipidea/ci.h +2 -0
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
(quoted diff) +0 -0

b/drivers/usb/chipidea/core.c index bc24c5b..1bcf6f6 100644

drivers/usb/chipidea/core.c +75 -1
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum
+usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9 @@
 static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) { @@
-908,6
+952,43 @@ static const struct attribute_group ci_attr_group = {
        .attrs = ci_attrs,
 };
 
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/usb/chipidea/core.c static int ci_hdrc_probe(struct

Quoted 8 lines
drivers/usb/chipidea/core.c +5 -0
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct

platform_device *pdev)

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
drivers/usb/chipidea/core.c +3 -29
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/usb/chipidea/core.c +0 -0
        ret = devm_request_irq(dev, ci->irq, ci_irq,

IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device

*pdev)

Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
        if (ret)
                goto stop;
drivers/usb/chipidea/core.c +13 -0
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6
+1197,9 @@ static int ci_hdrc_remove(struct platform_device *pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
 
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index 5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +13 -1
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

author
#7 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Hi

Quoted 31 lines

-----Original Message-----
From: Peter Chen
Sent: 2019年8月5日 12:57
To: Jun Li <[email protected]>; Peter Chen <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; dl-linux-imx <[email protected]>; USB list <[email protected]>
Subject: RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 22 lines
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

USB role is fully controlled by usb role switch consumer(e.g. typec), usb port either at host mode, or at device connected mode, will not stay at USB_ROLE_NONE mode.

Then, if the Type-C cable is disconnected from PC host, the controller driver can't be notified?If that, how controller enters low power mode at this

situation?

Quoted 8 lines

The controller driver can be notified, but there are only role(host or
device) Information, so in you mentioned case, controller driver will get the same input before and after disconnect from host, can't know detachment and enter low power mode accordingly, this is more like something internal handling under device role.

If the Type-C can't pass "gadget disconnect" event, you may handle it at UDC driver itself, eg at ci_usb_role_switch_set. Otherwise, the gadget class driver can't be notified disconnection, and the controller itself can't

Quoted 6 lines
Quoted 4 lines

enter LPM as well.

OK, I will enable VBUS irq to handle this in v2.

You may use connect bit at portsc since VBUS may not connect to SoC.

By this way, disconnect can be decided but connect is a problem in current driver, as controller was stopped in low power mode so can't detect connection from host, unless we also update this behavior too.

Li Jun

Quoted 245 lines

Peter

Quoted 105 lines

Li Jun

Quoted 104 lines

Peter

Quoted 13 lines

Li Jun

Quoted 11 lines

Peter

Quoted 8 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
Quoted 6 lines
Quoted 6 lines

drivers/usb/chipidea/ci.h struct ci_hdrc { · line 212

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/usb/chipidea/ci.h index 82b86cd..5e2f0bc 100644

drivers/usb/chipidea/ci.h +0 -0
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t

hr_timeouts[NUM_OTG_FSM_TIMERS];

Quoted 81 lines
Quoted 81 lines
Quoted 81 lines
drivers/usb/chipidea/ci.h +2 -0
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
(quoted diff) +0 -0

b/drivers/usb/chipidea/core.c index bc24c5b..1bcf6f6 100644

drivers/usb/chipidea/core.c +43 -0
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum
+usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9 @@
 static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
 
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) {

@@
-908,6
+952,43 @@ static const struct attribute_group ci_attr_group = +{
.attrs = ci_attrs,
};

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/usb/chipidea/core.c static const struct attribute_group ci_attr_group = {

Quoted 4 lines
drivers/usb/chipidea/core.c +4 -0
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE])

{

Quoted 132 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/usb/chipidea/core.c +27 -0
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/usb/chipidea/core.c static int ci_hdrc_probe(struct

Quoted 8 lines
drivers/usb/chipidea/core.c +5 -0
+       }
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct

platform_device *pdev)

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
drivers/usb/chipidea/core.c +3 -29
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci->roles[USB_ROLE_DEVICE]) {
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +0 -0
        ret = devm_request_irq(dev, ci->irq, ci_irq,

IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device

*pdev)

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
        if (ret)
                goto stop;
drivers/usb/chipidea/core.c +14 -0
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6
+1197,9 @@ static int ci_hdrc_remove(struct platform_device
+*pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
 
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index
5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +13 -1
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct *work)
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

#8 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 8 lines
Quoted 3 lines

You may use connect bit at portsc since VBUS may not connect to SoC.

By this way, disconnect can be decided but connect is a problem in current driver, as controller was stopped in low power mode so can't detect connection from host, unless we also update this behavior too.

For connection, if current role is USB_ROLE_NONE, you may start device mode.

Peter

Quoted 250 lines

Li Jun

Quoted 214 lines

Peter

Quoted 106 lines

Li Jun

Quoted 105 lines

Peter

Quoted 13 lines

Li Jun

Quoted 11 lines

Peter

Quoted 8 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
Quoted 6 lines
Quoted 6 lines

drivers/usb/chipidea/ci.h struct ci_hdrc { · line 212

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/usb/chipidea/ci.h index 82b86cd..5e2f0bc 100644

drivers/usb/chipidea/ci.h +0 -0
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t

hr_timeouts[NUM_OTG_FSM_TIMERS];

Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
drivers/usb/chipidea/ci.h +2 -0
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
(quoted diff) +0 -0

b/drivers/usb/chipidea/core.c index bc24c5b..1bcf6f6 100644

drivers/usb/chipidea/core.c +40 -0
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev, enum
+usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6 +730,9
 @@ static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |=

CI_HDRC_SET_NON_ZERO_TTHA;

drivers/usb/chipidea/core.c +3 -0
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon")) {

@@
-908,6
+952,43 @@ static const struct attribute_group ci_attr_group += {
.attrs = ci_attrs,
};

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/usb/chipidea/core.c

Quoted 5 lines
drivers/usb/chipidea/core.c +5 -0
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] &&
+ ci->roles[USB_ROLE_DEVICE])

{

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
drivers/usb/chipidea/core.c +28 -0
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
Quoted 12 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/usb/chipidea/core.c static int ci_hdrc_probe(struct

Quoted 7 lines
drivers/usb/chipidea/core.c +4 -0
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct

platform_device *pdev)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +0 -1
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci-

roles[USB_ROLE_DEVICE]) {

Quoted 58 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/usb/chipidea/core.c +3 -28
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +0 -0
        ret = devm_request_irq(dev, ci->irq, ci_irq,

IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device

*pdev)

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
        if (ret)
                goto stop;
drivers/usb/chipidea/core.c +11 -0
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6
+1197,9 @@ static int ci_hdrc_remove(struct platform_device
+*pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +3 -0
+       if (ci->role_switch)
+               usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/usb/chipidea/otg.c static void ci_otg_work(struct work_struct

Quoted 8 lines
                pm_runtime_get_sync(&pdev->dev);
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index
5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +0 -0
@@ -214,6 +214,19 @@ static void ci_otg_work(struct work_struct

*work)

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
drivers/usb/chipidea/otg.c +13 -1
                ci_handle_vbus_change(ci);
        }
 
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

author
#9 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 12 lines

-----Original Message-----
From: Peter Chen
Sent: 2019年8月6日 15:52
To: Jun Li <[email protected]>; Peter Chen <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; dl-linux-imx <[email protected]>; USB list <[email protected]>
Subject: RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

You may use connect bit at portsc since VBUS may not connect to SoC.

By this way, disconnect can be decided but connect is a problem in current driver, as controller was stopped in low power mode so can't detect connection from host, unless we also update this behavior too.

For connection, if current role is USB_ROLE_NONE, you may start device mode.

This is assuming set role only can be called one time between disconnect and connect to host, this may not always true, but this can be resolved, I think we need handle the case device mode is started but connection does not happen, so the gadget need be stopped and enter low power mode again, if this approach is OK to you, I will go in this direction for my v2.

Li Jun

Quoted 257 lines

Peter

Quoted 120 lines

Li Jun

Quoted 119 lines

Peter

Quoted 106 lines

Li Jun

Quoted 105 lines

Peter

Quoted 13 lines

Li Jun

Quoted 11 lines

Peter

Quoted 8 lines
---
 drivers/usb/chipidea/ci.h   |   2 +
 drivers/usb/chipidea/core.c | 125
++++++++++++++++++++++++++++++++++----------
 drivers/usb/chipidea/otg.c  |  13 +++++
 3 files changed, 111 insertions(+), 29 deletions(-)
Quoted 6 lines
Quoted 6 lines

drivers/usb/chipidea/ci.h struct ci_hdrc { · line 212

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/usb/chipidea/ci.h index 82b86cd..5e2f0bc 100644

drivers/usb/chipidea/ci.h +0 -0
@@ -212,6 +212,7 @@ struct ci_hdrc {
        ktime_t

hr_timeouts[NUM_OTG_FSM_TIMERS];

Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
drivers/usb/chipidea/ci.h +2 -0
        unsigned                        enabled_otg_timer_bits;
        enum otg_fsm_timer              next_otg_timer;
+       struct usb_role_switch          *role_switch;
        struct work_struct              work;
        struct workqueue_struct         *wq;
 
@@ -244,6 +245,7 @@ struct ci_hdrc {
        struct dentry                   *debugfs;
        bool                            id_event;
        bool                            b_sess_valid_event;
+       bool                            role_switch_event;
        bool                            imx28_write_fix;
        bool                            supports_runtime_pm;
        bool                            in_lpm;
(quoted diff) +0 -0

b/drivers/usb/chipidea/core.c index bc24c5b..1bcf6f6 100644

drivers/usb/chipidea/core.c +41 -0
@@ -587,6 +587,47 @@ static irqreturn_t ci_irq(int irq, void *data)
        return ret;
 }
 
+static int ci_usb_role_switch_set(struct device *dev,
+enum usb_role
+role) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+
+       if (ci->role == role || role == USB_ROLE_NONE)
+               return 0;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       ci->role_switch_event = true;
+       if (ci->role == USB_ROLE_NONE) {
+               if (role == USB_ROLE_DEVICE)
+                       ci->role = USB_ROLE_HOST;
+               else
+                       ci->role = USB_ROLE_DEVICE;
+       }
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       ci_otg_queue_work(ci);
+
+       return 0;
+}
+
+static enum usb_role ci_usb_role_switch_get(struct device *dev) {
+       struct ci_hdrc *ci = dev_get_drvdata(dev);
+       unsigned long flags;
+       enum usb_role role;
+
+       spin_lock_irqsave(&ci->lock, flags);
+       role = ci->role;
+       spin_unlock_irqrestore(&ci->lock, flags);
+
+       return role;
+}
+
+static struct usb_role_switch_desc ci_role_switch = {
+       .set = ci_usb_role_switch_set,
+       .get = ci_usb_role_switch_get, };
+
 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
                             void *ptr)  { @@ -689,6
+730,9 @@ static int ci_get_platdata(struct device *dev,
        if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
                platdata->flags |=

CI_HDRC_SET_NON_ZERO_TTHA;

drivers/usb/chipidea/core.c +3 -0
+       if (device_property_read_bool(dev, "usb-role-switch"))
+               ci_role_switch.fwnode = dev->fwnode;
+
        ext_id = ERR_PTR(-ENODEV);
        ext_vbus = ERR_PTR(-ENODEV);
        if (of_property_read_bool(dev->of_node, "extcon"))

{ @@
-908,6
+952,43 @@ static const struct attribute_group

+ci_attr_group = {
        .attrs = ci_attrs,  };
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/usb/chipidea/core.c

Quoted 5 lines
drivers/usb/chipidea/core.c +5 -0
+static int ci_start_initial_role(struct ci_hdrc *ci) {
+       int ret = 0;
+
+       if (ci->roles[USB_ROLE_HOST] &&
+ ci->roles[USB_ROLE_DEVICE])

{

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +4 -0
+               if (ci->is_otg) {
+                       ci->role = ci_otg_role(ci);
+                       /* Enable ID change irq */
+                       hw_write_otgsc(ci, OTGSC_IDIE,

OTGSC_IDIE);

Quoted 133 lines
Quoted 96 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/usb/chipidea/core.c +24 -0
+               } else {
+                       /*
+                        * If the controller is not OTG capable, but support
+                        * role switch, the defalt role is gadget, and the
+                        * user can switch it through debugfs.
+                        */
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       } else {
+               ci->role = ci->roles[USB_ROLE_HOST]
+                       ? USB_ROLE_HOST
+                       : USB_ROLE_DEVICE;
+       }
+
+       if (!ci_otg_is_fsm_mode(ci)) {
+               /* only update vbus status for peripheral */
+               if (ci->role == USB_ROLE_DEVICE)
+                       ci_handle_vbus_change(ci);
+
+               ret = ci_role_start(ci, ci->role);
+               if (ret)
+                       dev_err(ci->dev, "can't start %s role\n",
+                                               ci_role(ci)->name);
+       }
Quoted 12 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/usb/chipidea/core.c static int ci_hdrc_probe(struct

Quoted 7 lines
drivers/usb/chipidea/core.c +4 -0
+
+       return ret;
+}
+
 static int ci_hdrc_probe(struct platform_device *pdev)  {
        struct device   *dev = &pdev->dev;
@@ -1051,36 +1132,10 @@ static int ci_hdrc_probe(struct

platform_device *pdev)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +0 -1
                }
        }
 
-       if (ci->roles[USB_ROLE_HOST] && ci-

roles[USB_ROLE_DEVICE]) {

Quoted 59 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/usb/chipidea/core.c +3 -28
-               if (ci->is_otg) {
-                       ci->role = ci_otg_role(ci);
-                       /* Enable ID change irq */
-                       hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
-               } else {
-                       /*
-                        * If the controller is not OTG capable, but support
-                        * role switch, the defalt role is gadget, and the
-                        * user can switch it through debugfs.
-                        */
-                       ci->role = USB_ROLE_DEVICE;
-               }
-       } else {
-               ci->role = ci->roles[USB_ROLE_HOST]
-                       ? USB_ROLE_HOST
-                       : USB_ROLE_DEVICE;
-       }
-
-       if (!ci_otg_is_fsm_mode(ci)) {
-               /* only update vbus status for peripheral */
-               if (ci->role == USB_ROLE_DEVICE)
-                       ci_handle_vbus_change(ci);
-
-               ret = ci_role_start(ci, ci->role);
-               if (ret) {
-                       dev_err(dev, "can't start %s role\n",
-                                               ci_role(ci)->name);
+       if (!ci_role_switch.fwnode) {
+               ret = ci_start_initial_role(ci);
+               if (ret)
                        goto stop;
-               }
        }
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/usb/chipidea/core.c +0 -0
        ret = devm_request_irq(dev, ci->irq, ci_irq,

IRQF_SHARED, @@
-1092,6 +1147,15 @@ static int ci_hdrc_probe(struct platform_device

*pdev)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
        if (ret)
                goto stop;
drivers/usb/chipidea/core.c +12 -0
+       if (ci_role_switch.fwnode) {
+               ci->role_switch = usb_role_switch_register(dev,
+                                       &ci_role_switch);
+               if (IS_ERR(ci->role_switch)) {
+                       ret = PTR_ERR(ci->role_switch);
+                       goto stop;
+               }
+       }
+
        if (ci->supports_runtime_pm) {
                pm_runtime_set_active(&pdev->dev);
                pm_runtime_enable(&pdev->dev); @@ -1133,6
+1197,9 @@ static int ci_hdrc_remove(struct
+platform_device
+*pdev)  {
        struct ci_hdrc *ci = platform_get_drvdata(pdev);
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/usb/chipidea/core.c +4 -0
+       if (ci->role_switch)
+
+ usb_role_switch_unregister(ci->role_switch);
+
        if (ci->supports_runtime_pm) {
                pm_runtime_get_sync(&pdev->dev);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
                pm_runtime_disable(&pdev->dev); diff --git

a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c index
5bde0b5..0a22855 100644

drivers/usb/chipidea/otg.c +0 -0
@@ -214,6 +214,19 @@ static void ci_otg_work(struct

work_struct

*work)

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
                ci_handle_vbus_change(ci);
        }
drivers/usb/chipidea/otg.c +13 -1
+       if (ci->role_switch_event) {
+               ci->role_switch_event = false;
+
+               if (ci->role == USB_ROLE_DEVICE) {
+                       usb_gadget_vbus_disconnect(&ci->gadget);
+                       ci_role_start(ci, USB_ROLE_HOST);
+               } else if (ci->role == USB_ROLE_HOST) {
+                       ci_role_stop(ci);
+                       usb_gadget_vbus_connect(&ci->gadget);
+                       ci->role = USB_ROLE_DEVICE;
+               }
+       }
+
        pm_runtime_put_sync(ci->dev);
 
        enable_irq(ci->irq);
--

2.7.4

#10 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

You may use connect bit at portsc since VBUS may not connect to SoC.

By this way, disconnect can be decided but connect is a problem in current driver, as controller was stopped in low power mode so can't detect connection from host, unless we also update this behavior too.

Quoted 8 lines
Quoted 1 line

For connection, if current role is USB_ROLE_NONE, you may start device mode.

This is assuming set role only can be called one time between disconnect and connect to host, this may not always true, but this can be resolved, I think we need handle the case device mode is started but connection does not happen, so the gadget need be stopped and enter low power mode again, if this approach is OK to you, I will go in this direction for my v2.

After thinking more, I think Type-C tcpm code should set usb_role correctly, it knows physical connection status well, and there are already USB_ROLE_NONE references at tcpm now. Depending on USB device driver workaround to know USB_ROLE_NONE is not a good solution. Look at another USB role driver, intel-xhci-usb-role-switch.c, it could also get USB_ROLE_NONE state.

Peter

author
#11 lore raw

RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 29 lines

-----Original Message-----
From: Peter Chen
Sent: 2019年8月7日 10:41
To: Jun Li <[email protected]>; Peter Chen <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; dl-linux-imx <[email protected]>; USB list <[email protected]>
Subject: RE: [PATCH 2/5] usb: chipidea: add role switch class support

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

You may use connect bit at portsc since VBUS may not connect to SoC.

By this way, disconnect can be decided but connect is a problem in current driver, as controller was stopped in low power mode so can't detect connection from host, unless we also update this behavior too.

Quoted 7 lines
Quoted 1 line

For connection, if current role is USB_ROLE_NONE, you may start device mode.

This is assuming set role only can be called one time between
disconnect and connect to host, this may not always true, but this can
be resolved, I think we need handle the case device mode is started
but connection does not happen, so the gadget need be stopped and
enter low power mode again, if this approach is OK to you, I will go in this direction for my

Quoted 8 lines

v2.

Quoted 1 line

After thinking more, I think Type-C tcpm code should set usb_role correctly, it knows physical connection status well, and there are already USB_ROLE_NONE references at tcpm now. Depending on USB device driver workaround to know USB_ROLE_NONE is not a good solution. Look at another USB role driver, intel-xhci-usb-role-switch.c, it could also get USB_ROLE_NONE state.

Sorry, I re-checked the latest tcpm code found there is already USB_ROLE_NONE setting for not disconnect, so we are fine to handle it in usb controller driver, will post v2 for this soon.

Li Jun

Quoted 2 lines

Peter