All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: hub: port: add sysfs entry to switch port power
@ 2022-05-10 23:13 Michael Grzeschik
  2022-05-11  5:43 ` Greg KH
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Michael Grzeschik @ 2022-05-10 23:13 UTC (permalink / raw)
  To: linux-usb; +Cc: gregkh, kernel

This patch adds an sysfs switch to enable/disable a port on an power
switchable hub. It also ensures that the associated device gets
disconnected from the logical usb tree.

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
 drivers/usb/core/port.c | 47 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/drivers/usb/core/port.c b/drivers/usb/core/port.c
index d5bc36ca5b1f77..abc618d87888f3 100644
--- a/drivers/usb/core/port.c
+++ b/drivers/usb/core/port.c
@@ -17,6 +17,52 @@ static int usb_port_block_power_off;
 
 static const struct attribute_group *port_dev_group[];
 
+static ssize_t port_power_store(struct device *dev, struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	struct usb_port *port_dev = to_usb_port(dev);
+	struct usb_device *udev = port_dev->child;
+	struct usb_device *hdev = to_usb_device(dev->parent->parent);
+	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
+	int port1 = port_dev->portnum;
+	bool value;
+	int rc = 0;
+
+	if (!hub)
+		return -EINVAL;
+
+	if (hub->in_reset)
+		return -EBUSY;
+
+	rc = strtobool(buf, &value);
+	if (rc)
+		return rc;
+
+	if (value)
+		usb_remote_wakeup(hdev);
+
+	rc = usb_hub_set_port_power(hdev, hub, port1, value);
+	if (rc)
+		return rc;
+
+	if (!value) {
+		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
+		if (!port_dev->is_superspeed)
+			usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
+
+		if (udev) {
+			port_dev->child = NULL;
+			usb_disconnect(&udev);
+		}
+	}
+
+	if (!rc)
+		rc = count;
+
+	return rc;
+}
+static DEVICE_ATTR_WO(port_power);
+
 static ssize_t location_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
@@ -153,6 +199,7 @@ static struct attribute *port_dev_attrs[] = {
 	&dev_attr_location.attr,
 	&dev_attr_quirks.attr,
 	&dev_attr_over_current_count.attr,
+	&dev_attr_port_power.attr,
 	NULL,
 };
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-05-31 23:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10 23:13 [PATCH] usb: hub: port: add sysfs entry to switch port power Michael Grzeschik
2022-05-11  5:43 ` Greg KH
2022-05-11  8:26   ` Michael Grzeschik
2022-05-11  8:33 ` kernel test robot
2022-05-11 14:09 ` Alan Stern
2022-05-11 20:37   ` Michael Grzeschik
2022-05-12  1:19     ` Alan Stern
2022-05-14 19:52       ` Alan Stern
2022-05-31 23:45         ` Michael Grzeschik

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.