Skip to contentSkip to conversation

usb: typec: Registering real device entries for the muxes

PATCH Heikki Krogerus 1 participant 1 message Apr 1, 2019

Subsystems (4)
lore
Share & tools
lore

Heikki Krogerus

author maintainer
#0 lore raw

Registering real device entries (struct device) for the mode muxes as well as for the orientation switches.

The Type-C mux code was deliberately attempting to avoid creation of separate device entries for the orientation switch and the mode switch (alternate modes) because they are not physical devices. They are functions of a single physical multiplexer/demultiplexer switch device.

Unfortunately because of the dependency we still have on the underlying mux device driver, we had to put in hacks like the one in the commit 3e3b81965cbf ("usb: typec: mux: Take care of driver module reference counting") to make sure the driver does not disappear from underneath us. Even with those hacks we were still left with a potential NUll pointer dereference scenario, so just creating the device entries, and letting the core take care of the dependencies. No more hacks needed.

Fixes 3e3b81965cbf ("usb: typec: mux: Take care of driver module reference counting")
Cc v4.19.x <[email protected]> # v4.19.x+
---
 drivers/usb/typec/bus.h             |  15 ++
 drivers/usb/typec/class.c           |  16 ++-
 drivers/usb/typec/mux.c             | 207 +++++++++++++++++++---------
 drivers/usb/typec/mux/pi3usb30532.c |  46 ++++---
 include/linux/usb/typec_mux.h       |  58 ++++----
 5 files changed, 220 insertions(+), 122 deletions(-)
drivers/usb/typec/bus.h +15 -0
index db40e61d8b72..0c9661c96473 100644
@@ -35,4 +35,19 @@ extern const struct device_type typec_port_dev_type;
 #define is_typec_altmode(_dev_) (_dev_->type == &typec_altmode_dev_type)
 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
 
+extern struct class typec_mux_class;
+
+struct typec_switch {
+	struct device dev;
+	typec_switch_set_fn_t set;
+};
+
+struct typec_mux {
+	struct device dev;
+	typec_mux_set_fn_t set;
+};
+
+#define to_typec_switch(_dev_) container_of(_dev_, struct typec_switch, dev)
+#define to_typec_mux(_dev_) container_of(_dev_, struct typec_mux, dev)
+
 #endif /* __USB_TYPEC_ALTMODE_H__ */
drivers/usb/typec/class.c +14 -2
index 2eb623841847..8f5223879d91 100644
@@ -1646,13 +1646,25 @@ static int __init typec_init(void)
 	if (ret)
 		return ret;
 
+	ret = class_register(&typec_mux_class);
+	if (ret)
+		goto err_unregister_bus;
+
 	typec_class = class_create(THIS_MODULE, "typec");
 	if (IS_ERR(typec_class)) {
-		bus_unregister(&typec_bus);
-		return PTR_ERR(typec_class);
+		ret = PTR_ERR(typec_class);
+		goto err_unregister_mux_class;
 	}
 
 	return 0;
+
+err_unregister_mux_class:
+	class_unregister(&typec_mux_class);
+
+err_unregister_bus:
+	bus_unregister(&typec_bus);
+
+	return ret;
 }
 subsys_initcall(typec_init);
 
drivers/usb/typec/mux.c +139 -68

301 lines not on this page · raw message

drivers/usb/typec/mux/pi3usb30532.c +27 -19

90 lines not on this page · raw message

include/linux/usb/typec_mux.h +25 -33

79 lines not on this page · raw message