Skip to contentSkip to conversation

usb: chipidea: core: fix device mode not work in non-lpm

PATCH In review Xu Yang 2 participants 7 messages Mar 19 – Mar 30

Subsystems (3)
lore
Share & tools
lore

Xu Yang

author
#0 lore raw

PATCH 1/2 usb: chipidea: core: fix device mode not work in non-lpm

In current design, we expect 2 ci_irq() to handle ID and VBUS events in usb role switch, like what ci_extcon_wakeup_int() does. Now we only call ci_irq() once. However, this won't bring any issues in low power mode, because ci_irq() just take the device out of low power mode, and then ci_extcon_wakeup_int() will call ci_irq() twice. If the device is not in suspend state, the device mode will not work properly because VBUS event won'tbe handled (ID event has higher priority) at all.

Although 2 consecutive ci_irq() can work around the issue, do not do it because ci_usb_role_switch_set() may or not be in low power context which make the ci_irq() purpose not unique here. Because the final processing is in ci_otg_work(), just directly queue an otg work. This also refine the logic for more clarity and not set changed flag.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")
---
 drivers/usb/chipidea/core.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
drivers/usb/chipidea/core.c +11 -19
index fac11f20cf0a..1bd231a852a1 100644
@@ -618,30 +618,22 @@ static int ci_usb_role_switch_set(struct usb_role_switch *sw,
 	struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
 	struct ci_hdrc_cable *cable;
 
-	if (role == USB_ROLE_HOST) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	cable = &ci->platdata->id_extcon;
+	if (role == USB_ROLE_HOST)
 		cable->connected = true;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
-		cable->connected = false;
-	} else if (role == USB_ROLE_DEVICE) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+
+	cable = &ci->platdata->vbus_extcon;
+	if (role == USB_ROLE_DEVICE)
 		cable->connected = true;
-	} else {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
-		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-	}
 
-	ci_irq(ci);
+	ci->id_event = true;
+	ci->b_sess_valid_event = true;
+	ci_otg_queue_work(ci);
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 usb: chipidea: otg: not wait vbus drop if use role_switch

The usb role switch will update ID and VBUS states at the same time, and vbus will not drop when execute data role swap in Type-C usecase. So lets not wait vbus drop in usb role switch case too.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")
---
 drivers/usb/chipidea/otg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/chipidea/otg.c +2 -2
index 647e98f4e351..2371789effa0 100644
@@ -187,8 +187,8 @@ void ci_handle_id_switch(struct ci_hdrc *ci)
 
 		ci_role_stop(ci);
 
-		if (role == CI_ROLE_GADGET &&
-				IS_ERR(ci->platdata->vbus_extcon.edev))
+		if (role == CI_ROLE_GADGET && !ci->role_switch &&
+		    IS_ERR(ci->platdata->vbus_extcon.edev))
 			/*
 			 * Wait vbus lower than OTGSC_BSV before connecting
 			 * to host. If connecting status is from an external
Signature · 1 line
2.34.1
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] usb: chipidea: otg: not wait vbus drop if use role_switch

Quoted 8 lines · Xu Yang

The usb role switch will update ID and VBUS states at the same time, and vbus will not drop when execute data role swap in Type-C usecase. So lets not wait vbus drop in usb role switch case too.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")

Peter

Quoted 22 lines
---
 drivers/usb/chipidea/otg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/chipidea/otg.c +2 -2
index 647e98f4e351..2371789effa0 100644
@@ -187,8 +187,8 @@ void ci_handle_id_switch(struct ci_hdrc *ci)
 
 		ci_role_stop(ci);
 
-		if (role == CI_ROLE_GADGET &&
-				IS_ERR(ci->platdata->vbus_extcon.edev))
+		if (role == CI_ROLE_GADGET && !ci->role_switch &&
+		    IS_ERR(ci->platdata->vbus_extcon.edev))
 			/*
 			 * Wait vbus lower than OTGSC_BSV before connecting
 			 * to host. If connecting status is from an external
Signature · 1 line
2.34.1
Signature · 2 lines
Best regards,
Peter
maintainer
#3 lore raw
In reply to Xu Yang
Quoted 7 lines · Xu Yang

In current design, we expect 2 ci_irq() to handle ID and VBUS events in usb role switch, like what ci_extcon_wakeup_int() does. Now we only call ci_irq() once. However, this won't bring any issues in low power mode, because ci_irq() just take the device out of low power mode, and then ci_extcon_wakeup_int() will call ci_irq() twice. If the device is not in suspend state, the device mode will not work properly because VBUS event won'tbe handled (ID event has higher priority) at all.

%s/won'tbe/won't be

Is it possible change ci_irq_handler and handle both events?

Quoted 46 lines

Although 2 consecutive ci_irq() can work around the issue, do not do it because ci_usb_role_switch_set() may or not be in low power context which make the ci_irq() purpose not unique here. Because the final processing is in ci_otg_work(), just directly queue an otg work. This also refine the logic for more clarity and not set changed flag.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")
---
 drivers/usb/chipidea/core.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
drivers/usb/chipidea/core.c +6 -14
index fac11f20cf0a..1bd231a852a1 100644
@@ -618,30 +618,22 @@ static int ci_usb_role_switch_set(struct usb_role_switch *sw,
 	struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
 	struct ci_hdrc_cable *cable;
 
-	if (role == USB_ROLE_HOST) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	cable = &ci->platdata->id_extcon;
+	if (role == USB_ROLE_HOST)
 		cable->connected = true;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
-		cable->connected = false;
-	} else if (role == USB_ROLE_DEVICE) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+
+	cable = &ci->platdata->vbus_extcon;
+	if (role == USB_ROLE_DEVICE)
 		cable->connected = true;
-	} else {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;

drivers/usb/chipidea/core.c static int ci_usb_role_switch_set(struct usb_role_switch *sw, · line 634

Quoted 10 lines
drivers/usb/chipidea/core.c +3 -5
-		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-	}
 
-	ci_irq(ci);
+	ci->id_event = true;
+	ci->b_sess_valid_event = true;

Why both ID and VBUS event are set as true unconditionally?

Signature · 2 lines
Best regards,
Peter
author
#4 lore raw
Quoted 10 lines · Peter Chen (CIX)
Quoted 7 lines · Xu Yang

In current design, we expect 2 ci_irq() to handle ID and VBUS events in usb role switch, like what ci_extcon_wakeup_int() does. Now we only call ci_irq() once. However, this won't bring any issues in low power mode, because ci_irq() just take the device out of low power mode, and then ci_extcon_wakeup_int() will call ci_irq() twice. If the device is not in suspend state, the device mode will not work properly because VBUS event won'tbe handled (ID event has higher priority) at all.

%s/won'tbe/won't be

OK.

Quoted 2 lines

Is it possible change ci_irq_handler and handle both events?

Yes, we can change ci_irq_handler() and let it set both ci->id_event and ci->b_sess_valid_event flags, then queue a ci_otg_work() to handle them later.

I think this just unnecessarily call ci_irq_handler() to handle lpm/non-lpm case as the final path is ci_otg_work() and it will handle lpm/non-lpm case by naturally calling pm_runtime_get/put_sync(), otherwise it relies on ci_extcon_wakeup_int() to achieve the same purpose.

Both methods work for me, may I know which one do you prefer? :)

Quoted 54 lines
Quoted 54 lines

Although 2 consecutive ci_irq() can work around the issue, do not do it because ci_usb_role_switch_set() may or not be in low power context which make the ci_irq() purpose not unique here. Because the final processing is in ci_otg_work(), just directly queue an otg work. This also refine the logic for more clarity and not set changed flag.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")
---
 drivers/usb/chipidea/core.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
drivers/usb/chipidea/core.c +7 -18
index fac11f20cf0a..1bd231a852a1 100644
@@ -618,30 +618,22 @@ static int ci_usb_role_switch_set(struct usb_role_switch *sw,
 	struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
 	struct ci_hdrc_cable *cable;
 
-	if (role == USB_ROLE_HOST) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	cable = &ci->platdata->id_extcon;
+	if (role == USB_ROLE_HOST)
 		cable->connected = true;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
-		cable->connected = false;
-	} else if (role == USB_ROLE_DEVICE) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+
+	cable = &ci->platdata->vbus_extcon;
+	if (role == USB_ROLE_DEVICE)
 		cable->connected = true;
-	} else {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
-		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-	}
 
Quoted 5 lines

drivers/usb/chipidea/core.c static int ci_usb_role_switch_set(struct usb_role_switch *sw, · line 634

Quoted 3 lines
drivers/usb/chipidea/core.c +2 -1
-	ci_irq(ci);
+	ci->id_event = true;
+	ci->b_sess_valid_event = true;

Why both ID and VBUS event are set as true unconditionally?

The main purpose is to simplify the handling of the various situations.

The usb role include below types:
- host
- device
- none.

1. host <--> none
Above change means ID change occur.

2. device <--> none
Above change means VBUS change occur.

3. host <--> device
Above change means ID and VBUS change occur.

By setting both of them as true, the logic can be simplified here and ID and VBUS otg work will check if a real state change happen by comparing old state and current OTGSC_ID/OTGSC_BSV bit.

Thanks,
Xu Yang

Quoted 5 lines
Signature · 4 lines
-- 

Best regards,
Peter
maintainer
#5 lore raw
Quoted 21 lines · Xu Yang
Quoted 10 lines · Peter Chen (CIX)
Quoted 7 lines · Xu Yang

In current design, we expect 2 ci_irq() to handle ID and VBUS events in usb role switch, like what ci_extcon_wakeup_int() does. Now we only call ci_irq() once. However, this won't bring any issues in low power mode, because ci_irq() just take the device out of low power mode, and then ci_extcon_wakeup_int() will call ci_irq() twice. If the device is not in suspend state, the device mode will not work properly because VBUS event won'tbe handled (ID event has higher priority) at all.

%s/won'tbe/won't be

OK.

Quoted 2 lines

Is it possible change ci_irq_handler and handle both events?

Yes, we can change ci_irq_handler() and let it set both ci->id_event and ci->b_sess_valid_event flags, then queue a ci_otg_work() to handle them later.

Quoted 6 lines

I think this just unnecessarily call ci_irq_handler() to handle lpm/non-lpm case as the final path is ci_otg_work() and it will handle lpm/non-lpm case by naturally calling pm_runtime_get/put_sync(), otherwise it relies on ci_extcon_wakeup_int() to achieve the same purpose.

Both methods work for me, may I know which one do you prefer? :)

My main concerns are below code, and it may confuse the reader.

ci->id_event = true;
ci->b_sess_valid_event = true;

I may prefer the first option, it is easy to understand.

Peter

Quoted 81 lines
Quoted 55 lines
Quoted 55 lines

Although 2 consecutive ci_irq() can work around the issue, do not do it because ci_usb_role_switch_set() may or not be in low power context which make the ci_irq() purpose not unique here. Because the final processing is in ci_otg_work(), just directly queue an otg work. This also refine the logic for more clarity and not set changed flag.

Fixes e1b5d2bed67c ("usb: chipidea: core: handle usb role switch in a common way")
---
 drivers/usb/chipidea/core.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
drivers/usb/chipidea/core.c +7 -18
index fac11f20cf0a..1bd231a852a1 100644
@@ -618,30 +618,22 @@ static int ci_usb_role_switch_set(struct usb_role_switch *sw,
 	struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
 	struct ci_hdrc_cable *cable;
 
-	if (role == USB_ROLE_HOST) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	cable = &ci->platdata->id_extcon;
+	if (role == USB_ROLE_HOST)
 		cable->connected = true;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
-		cable->connected = false;
-	} else if (role == USB_ROLE_DEVICE) {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+
+	cable = &ci->platdata->vbus_extcon;
+	if (role == USB_ROLE_DEVICE)
 		cable->connected = true;
-	} else {
-		cable = &ci->platdata->id_extcon;
-		cable->changed = true;
-		cable->connected = false;
-		cable = &ci->platdata->vbus_extcon;
-		cable->changed = true;
+	else
 		cable->connected = false;
-	}
 
Quoted 5 lines

drivers/usb/chipidea/core.c static int ci_usb_role_switch_set(struct usb_role_switch *sw, · line 634

Quoted 3 lines
drivers/usb/chipidea/core.c +2 -1
-	ci_irq(ci);
+	ci->id_event = true;
+	ci->b_sess_valid_event = true;

Why both ID and VBUS event are set as true unconditionally?

The main purpose is to simplify the handling of the various situations.

The usb role include below types:
- host
- device
- none.

1. host <--> none
Above change means ID change occur.

2. device <--> none
Above change means VBUS change occur.

3. host <--> device
Above change means ID and VBUS change occur.

By setting both of them as true, the logic can be simplified here and ID and VBUS otg work will check if a real state change happen by comparing old state and current OTGSC_ID/OTGSC_BSV bit.

Signature · 2 lines
Best regards,
Peter
author
#6 lore raw
Quoted 27 lines · Peter Chen (CIX)
Quoted 26 lines · Xu Yang
Quoted 10 lines · Peter Chen (CIX)
Quoted 7 lines · Xu Yang

In current design, we expect 2 ci_irq() to handle ID and VBUS events in usb role switch, like what ci_extcon_wakeup_int() does. Now we only call ci_irq() once. However, this won't bring any issues in low power mode, because ci_irq() just take the device out of low power mode, and then ci_extcon_wakeup_int() will call ci_irq() twice. If the device is not in suspend state, the device mode will not work properly because VBUS event won'tbe handled (ID event has higher priority) at all.

%s/won'tbe/won't be

OK.

Quoted 2 lines

Is it possible change ci_irq_handler and handle both events?

Yes, we can change ci_irq_handler() and let it set both ci->id_event and ci->b_sess_valid_event flags, then queue a ci_otg_work() to handle them later.

I think this just unnecessarily call ci_irq_handler() to handle lpm/non-lpm case as the final path is ci_otg_work() and it will handle lpm/non-lpm case by naturally calling pm_runtime_get/put_sync(), otherwise it relies on ci_extcon_wakeup_int() to achieve the same purpose.

Quoted 8 lines
Quoted 1 line

Both methods work for me, may I know which one do you prefer? :)

My main concerns are below code, and it may confuse the reader.

ci->id_event = true;
ci->b_sess_valid_event = true;

I may prefer the first option, it is easy to understand.

OK. Thanks for your suggestion. I will rework the patch later.

Best Regards,
Xu Yang