Skip to contentSkip to conversation

usb: typec: hd3ss3220: Propagate regmap_read() errors from attached-state check

PATCH Superseded Ian Rogers 4 participants 5 messages Nov 19 – Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 usb: typec: hd3ss3220: Propagate regmap_read() errors from attached-state check

hd3ss3220_get_attached_state() has return type 'enum usb_role' (an unsigned enum) yet returns negative regmap_read() error codes directly. Neither hd3ss3220_set_role() nor output_poll_execute() checks for errors; on a transient I2C failure (such as -EIO, cast to 0xfffffffb), hd3ss3220_set_role() passes the bogus value to usb_role_switch_set_role() and disables VBUS power. On role-switch providers without a .get callback (where sw->role caches the value passed to usb_role_switch_set_role()), reading /sys/class/usb_role/*/role subsequently indexes usb_roles[role] out-of-bounds.

Return an int status from hd3ss3220_get_attached_state(), pass the resolved enum usb_role via an output pointer, and leave the current role and VBUS state unchanged on I2C read errors.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wimplicit-int-enum-cast enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wimplicit-int-enum-cast; not tested on TI HD3SS3220 hardware.

Fixes 1c48c759ef4b ("usb: typec: driver for TI HD3SS3220 USB Type-C DRP port controller")
---
 drivers/usb/typec/hd3ss3220.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/usb/typec/hd3ss3220.c +13 -9
index 60d6de9ba339..212c3624961c 100644
@@ -163,10 +163,10 @@ static int hd3ss3220_set_source_pref(struct hd3ss3220 *hd3ss3220, int prefer_rol
 				  src_pref);
 }
 
-static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
+static int hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220,
+					enum usb_role *attached_state)
 {
 	unsigned int reg_val;
-	enum usb_role attached_state;
 	int ret;
 
 	ret = regmap_read(hd3ss3220->regmap, HD3SS3220_REG_CN_STAT_CTRL,
@@ -176,17 +176,17 @@ static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
 
 	switch (reg_val & HD3SS3220_REG_CN_STAT_CTRL_ATTACHED_STATE_MASK) {
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_DFP:
-		attached_state = USB_ROLE_HOST;
+		*attached_state = USB_ROLE_HOST;
 		break;
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_UFP:
-		attached_state = USB_ROLE_DEVICE;
+		*attached_state = USB_ROLE_DEVICE;
 		break;
 	default:
-		attached_state = USB_ROLE_NONE;
+		*attached_state = USB_ROLE_NONE;
 		break;
 	}
 
-	return attached_state;
+	return 0;
 }
 
 static int hd3ss3220_try_role(struct typec_port *port, int role)
@@ -231,7 +231,10 @@ static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on)
 
 static void hd3ss3220_set_role(struct hd3ss3220 *hd3ss3220)
 {
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
+
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) < 0)
+		return;
 
 	usb_role_switch_set_role(hd3ss3220->role_sw, role_state);
 
@@ -258,9 +261,10 @@ static void output_poll_execute(struct work_struct *work)
 	struct hd3ss3220 *hd3ss3220 = container_of(delayed_work,
 						   struct hd3ss3220,
 						   output_poll_work);
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
 
-	if (hd3ss3220->role_state != role_state)
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) == 0 &&
+	    hd3ss3220->role_state != role_state)
 		hd3ss3220_set_role(hd3ss3220);
 
 	schedule_delayed_work(&hd3ss3220->output_poll_work, HZ);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

hd3ss3220_get_attached_state() has return type 'enum usb_role' (an unsigned enum) yet returns negative regmap_read() error codes directly. Neither hd3ss3220_set_role() nor output_poll_execute() checks for errors; on a transient I2C failure (such as -EIO, cast to 0xfffffffb), hd3ss3220_set_role() passes the bogus value to usb_role_switch_set_role() and disables VBUS power. On role-switch providers without a .get callback (where sw->role caches the value passed to usb_role_switch_set_role()), reading /sys/class/usb_role/*/role subsequently indexes usb_roles[role] out-of-bounds.

Return an int status from hd3ss3220_get_attached_state(), pass the resolved enum usb_role via an output pointer, and leave the current role and VBUS state unchanged on I2C read errors.

Quoted 7 lines

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wimplicit-int-enum-cast enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wimplicit-int-enum-cast; not tested on TI HD3SS3220 hardware.

Fixes 1c48c759ef4b ("usb: typec: driver for TI HD3SS3220 USB Type-C DRP port controller")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Ian Rogers
Quoted 14 lines · Ian Rogers

hd3ss3220_get_attached_state() has return type 'enum usb_role' (an unsigned enum) yet returns negative regmap_read() error codes directly. Neither hd3ss3220_set_role() nor output_poll_execute() checks for errors; on a transient I2C failure (such as -EIO, cast to 0xfffffffb), hd3ss3220_set_role() passes the bogus value to usb_role_switch_set_role() and disables VBUS power. On role-switch providers without a .get callback (where sw->role caches the value passed to usb_role_switch_set_role()), reading /sys/class/usb_role/*/role subsequently indexes usb_roles[role] out-of-bounds.

Return an int status from hd3ss3220_get_attached_state(), pass the resolved enum usb_role via an output pointer, and leave the current role and VBUS state unchanged on I2C read errors.

Quoted 7 lines

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wimplicit-int-enum-cast enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wimplicit-int-enum-cast; not tested on TI HD3SS3220 hardware.

Fixes 1c48c759ef4b ("usb: typec: driver for TI HD3SS3220 USB Type-C DRP port controller")

LGTM, but no stable?

Thanks,

Quoted 71 lines
---
 drivers/usb/typec/hd3ss3220.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/usb/typec/hd3ss3220.c +13 -9
index 60d6de9ba339..212c3624961c 100644
@@ -163,10 +163,10 @@ static int hd3ss3220_set_source_pref(struct hd3ss3220 *hd3ss3220, int prefer_rol
 				  src_pref);
 }
 
-static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
+static int hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220,
+					enum usb_role *attached_state)
 {
 	unsigned int reg_val;
-	enum usb_role attached_state;
 	int ret;
 
 	ret = regmap_read(hd3ss3220->regmap, HD3SS3220_REG_CN_STAT_CTRL,
@@ -176,17 +176,17 @@ static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
 
 	switch (reg_val & HD3SS3220_REG_CN_STAT_CTRL_ATTACHED_STATE_MASK) {
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_DFP:
-		attached_state = USB_ROLE_HOST;
+		*attached_state = USB_ROLE_HOST;
 		break;
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_UFP:
-		attached_state = USB_ROLE_DEVICE;
+		*attached_state = USB_ROLE_DEVICE;
 		break;
 	default:
-		attached_state = USB_ROLE_NONE;
+		*attached_state = USB_ROLE_NONE;
 		break;
 	}
 
-	return attached_state;
+	return 0;
 }
 
 static int hd3ss3220_try_role(struct typec_port *port, int role)
@@ -231,7 +231,10 @@ static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on)
 
 static void hd3ss3220_set_role(struct hd3ss3220 *hd3ss3220)
 {
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
+
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) < 0)
+		return;
 
 	usb_role_switch_set_role(hd3ss3220->role_sw, role_state);
 
@@ -258,9 +261,10 @@ static void output_poll_execute(struct work_struct *work)
 	struct hd3ss3220 *hd3ss3220 = container_of(delayed_work,
 						   struct hd3ss3220,
 						   output_poll_work);
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
 
-	if (hd3ss3220->role_state != role_state)
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) == 0 &&
+	    hd3ss3220->role_state != role_state)
 		hd3ss3220_set_role(hd3ss3220);
 
 	schedule_delayed_work(&hd3ss3220->output_poll_work, HZ);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Signature · 1 line
heikki
#3 lore raw
In reply to Ian Rogers
Quoted 65 lines · Ian Rogers

hd3ss3220_get_attached_state() has return type 'enum usb_role' (an unsigned enum) yet returns negative regmap_read() error codes directly. Neither hd3ss3220_set_role() nor output_poll_execute() checks for errors; on a transient I2C failure (such as -EIO, cast to 0xfffffffb), hd3ss3220_set_role() passes the bogus value to usb_role_switch_set_role() and disables VBUS power. On role-switch providers without a .get callback (where sw->role caches the value passed to usb_role_switch_set_role()), reading /sys/class/usb_role/*/role subsequently indexes usb_roles[role] out-of-bounds.

Return an int status from hd3ss3220_get_attached_state(), pass the resolved enum usb_role via an output pointer, and leave the current role and VBUS state unchanged on I2C read errors.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wimplicit-int-enum-cast enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wimplicit-int-enum-cast; not tested on TI HD3SS3220 hardware.

Fixes 1c48c759ef4b ("usb: typec: driver for TI HD3SS3220 USB Type-C DRP port controller")
---
 drivers/usb/typec/hd3ss3220.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/usb/typec/hd3ss3220.c +6 -6
index 60d6de9ba339..212c3624961c 100644
@@ -163,10 +163,10 @@ static int hd3ss3220_set_source_pref(struct hd3ss3220 *hd3ss3220, int prefer_rol
 				  src_pref);
 }
 
-static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
+static int hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220,
+					enum usb_role *attached_state)
 {
 	unsigned int reg_val;
-	enum usb_role attached_state;
 	int ret;
 
 	ret = regmap_read(hd3ss3220->regmap, HD3SS3220_REG_CN_STAT_CTRL,
@@ -176,17 +176,17 @@ static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
 
 	switch (reg_val & HD3SS3220_REG_CN_STAT_CTRL_ATTACHED_STATE_MASK) {
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_DFP:
-		attached_state = USB_ROLE_HOST;
+		*attached_state = USB_ROLE_HOST;
 		break;
 	case HD3SS3220_REG_CN_STAT_CTRL_AS_UFP:
-		attached_state = USB_ROLE_DEVICE;
+		*attached_state = USB_ROLE_DEVICE;
 		break;
 	default:
-		attached_state = USB_ROLE_NONE;
+		*attached_state = USB_ROLE_NONE;
 		break;
 	}
 
-	return attached_state;
+	return 0;
 }
 
 static int hd3ss3220_try_role(struct typec_port *port, int role)

drivers/usb/typec/hd3ss3220.c static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on) · line 237

Quoted 8 lines
drivers/usb/typec/hd3ss3220.c +4 -1
@@ -231,7 +231,10 @@ static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on)
 
 static void hd3ss3220_set_role(struct hd3ss3220 *hd3ss3220)
 {
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
+
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) < 0)
+		return;

No error propagation backward?

drivers/usb/typec/hd3ss3220.c static void output_poll_execute(struct work_struct *work) · line 266

Quoted 12 lines
drivers/usb/typec/hd3ss3220.c +2 -2
 	usb_role_switch_set_role(hd3ss3220->role_sw, role_state);
 
@@ -258,9 +261,10 @@ static void output_poll_execute(struct work_struct *work)
 	struct hd3ss3220 *hd3ss3220 = container_of(delayed_work,
 						   struct hd3ss3220,
 						   output_poll_work);
-	enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+	enum usb_role role_state;
 
-	if (hd3ss3220->role_state != role_state)
+	if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) == 0 &&

Why test for 0?

thanks,

greg k-h

author
#4 lore raw
Quoted 68 lines · Greg Kroah-Hartman
Quoted 66 lines · Ian Rogers

hd3ss3220_get_attached_state() has return type 'enum usb_role' (an unsigned enum) yet returns negative regmap_read() error codes directly. Neither hd3ss3220_set_role() nor output_poll_execute() checks for errors; on a transient I2C failure (such as -EIO, cast to 0xfffffffb), hd3ss3220_set_role() passes the bogus value to usb_role_switch_set_role() and disables VBUS power. On role-switch providers without a .get callback (where sw->role caches the value passed to usb_role_switch_set_role()), reading /sys/class/usb_role/*/role subsequently indexes usb_roles[role] out-of-bounds.

Return an int status from hd3ss3220_get_attached_state(), pass the resolved enum usb_role via an output pointer, and leave the current role and VBUS state unchanged on I2C read errors.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wimplicit-int-enum-cast enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wimplicit-int-enum-cast; not tested on TI HD3SS3220 hardware.

Fixes 1c48c759ef4b ("usb: typec: driver for TI HD3SS3220 USB Type-C DRP port controller")
---
 drivers/usb/typec/hd3ss3220.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/usb/typec/hd3ss3220.c +6 -6
index 60d6de9ba339..212c3624961c 100644
@@ -163,10 +163,10 @@ static int hd3ss3220_set_source_pref(struct hd3ss3220 *hd3ss3220, int prefer_rol
                                src_pref);
 }
 
-static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
+static int hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220,
+                                     enum usb_role *attached_state)
 {
      unsigned int reg_val;
-     enum usb_role attached_state;
      int ret;
 
      ret = regmap_read(hd3ss3220->regmap, HD3SS3220_REG_CN_STAT_CTRL,
@@ -176,17 +176,17 @@ static enum usb_role hd3ss3220_get_attached_state(struct hd3ss3220 *hd3ss3220)
 
      switch (reg_val & HD3SS3220_REG_CN_STAT_CTRL_ATTACHED_STATE_MASK) {
      case HD3SS3220_REG_CN_STAT_CTRL_AS_DFP:
-             attached_state = USB_ROLE_HOST;
+             *attached_state = USB_ROLE_HOST;
              break;
      case HD3SS3220_REG_CN_STAT_CTRL_AS_UFP:
-             attached_state = USB_ROLE_DEVICE;
+             *attached_state = USB_ROLE_DEVICE;
              break;
      default:
-             attached_state = USB_ROLE_NONE;
+             *attached_state = USB_ROLE_NONE;
              break;
      }
 
-     return attached_state;
+     return 0;
 }
 
 static int hd3ss3220_try_role(struct typec_port *port, int role)
@@ -231,7 +231,10 @@ static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on)
 
Quoted 9 lines

drivers/usb/typec/hd3ss3220.c static void hd3ss3220_regulator_control(struct hd3ss3220 *hd3ss3220, bool on) · line 237

Quoted 7 lines
drivers/usb/typec/hd3ss3220.c +4 -1
 static void hd3ss3220_set_role(struct hd3ss3220 *hd3ss3220)
 {
-     enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+     enum usb_role role_state;
+
+     if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) < 0)
+             return;

No error propagation backward?

Good point - hd3ss3220_probe() can fail probe and hd3ss3220_irq() can
return IRQ_NONE without clearing INT_STATUS if hd3ss3220_set_role() returns an int error (the third caller, output_poll_execute(), is a void delayed_work callback). I'll propagate the error out of hd3ss3220_set_role() in v2.

Quoted 10 lines
Quoted 10 lines
drivers/usb/typec/hd3ss3220.c +1 -1
      usb_role_switch_set_role(hd3ss3220->role_sw, role_state);
 
@@ -258,9 +261,10 @@ static void output_poll_execute(struct work_struct *work)
      struct hd3ss3220 *hd3ss3220 = container_of(delayed_work,
                                                 struct hd3ss3220,
                                                 output_poll_work);
-     enum usb_role role_state = hd3ss3220_get_attached_state(hd3ss3220);
+     enum usb_role role_state;
 
Quoted 4 lines

drivers/usb/typec/hd3ss3220.c static void output_poll_execute(struct work_struct *work) · line 266

Quoted 2 lines
drivers/usb/typec/hd3ss3220.c +1 -1
-     if (hd3ss3220->role_state != role_state)
+     if (hd3ss3220_get_attached_state(hd3ss3220, &role_state) == 0 &&

Why test for 0?

I'll change this to '!hd3ss3220_get_attached_state(...)' in v2 (and add Cc: [email protected] per Heikki's note).

Thanks,
Ian

Quoted 3 lines

thanks,

greg k-h