Skip to contentSkip to conversation

driver core: Add sysfs support for physical location of a device

PATCH Superseded Won Chung 2 participants 2 messages Mar 9, 2022 Driver core

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Won Chung

author
#0 lore raw

PATCH v4 1/1 driver core: Add sysfs support for physical location of a device

When ACPI table includes _PLD fields for a device, create a new directory (physical_location) in sysfs to share _PLD fields.

Currently without PLD information, when there are multiple of same devices, it is hard to distinguish which device corresponds to which physical device at which location. For example, when there are two Type C connectors, it is hard to find out which connector corresponds to the Type C port on the left panel versus the Type C port on the right panel. With PLD information provided, we can determine which specific device at which location is doing what.

_PLD output includes much more fields, but only generic fields are added and exposed to sysfs, so that non-ACPI devices can also support it in the future. The minimal generic fields needed for locating a device are the following.
- panel
- vertical_position
- horizontal_position
- dock
- lid

---

Changes from v3
- Move dev_add_physical_location() and dev_attr_physical_location_group to driver/base/physical_location.h.
- Use pointer and reorder physical_location in struct device to pack its bytes. (checked using pahole)
- Unify naming to physical_location since the name location is used in some places like USB port for different information.

Changes from v2
- Use sysfs_emit to create files.
- Correct mix of spaces and tabs.

Changes from v1
- Correct directory names in Documentation.
- Clarify namings in core.c

 .../testing/sysfs-devices-physical_location   |  42 ++++++
 drivers/base/Makefile                         |   1 +
 drivers/base/core.c                           |  10 ++
 drivers/base/physical_location.c              | 137 ++++++++++++++++++
 drivers/base/physical_location.h              |  16 ++
 include/linux/device.h                        |  73 ++++++++++
 6 files changed, 279 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-devices-physical_location
 create mode 100644 drivers/base/physical_location.c
 create mode 100644 drivers/base/physical_location.h
Documentation/ABI/testing/sysfs-devices-physical_location new file +42 -0
new file mode 100644
index 000000000000..202324b87083
@@ -0,0 +1,42 @@
+What:		/sys/devices/.../physical_location
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		This directory contains information on physical location of
+		the device connection point with respect to the system's
+		housing.
+
+What:		/sys/devices/.../physical_location/panel
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		Describes which panel surface of the system’s housing the
+		device connection point resides on.
+
+What:		/sys/devices/.../physical_location/vertical_position
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		Describes vertical position of the device connection point on
+		the panel surface.
+
+What:		/sys/devices/.../physical_location/horizontal_position
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		Describes horizontal position of the device connection point on
+		the panel surface.
+
+What:		/sys/devices/.../physical_location/dock
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		"Yes" if the device connection point resides in a docking
+		station or a port replicator. "No" otherwise.
+
+What:		/sys/devices/.../physical_location/lid
+Date:		March 2022
+Contact:	Won Chung <[email protected]>
+Description:
+		"Yes" if the device connection point resides on the lid of
+		laptop system. "No" otherwise.
drivers/base/Makefile +1 -0
index 02f7f1358e86..83217d243c25 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_DEV_COREDUMP) += devcoredump.o
 obj-$(CONFIG_GENERIC_MSI_IRQ_DOMAIN) += platform-msi.o
 obj-$(CONFIG_GENERIC_ARCH_TOPOLOGY) += arch_topology.o
 obj-$(CONFIG_GENERIC_ARCH_NUMA) += arch_numa.o
+obj-$(CONFIG_ACPI) += physical_location.o
 
 obj-y			+= test/
 
drivers/base/core.c +10 -0
index 7bb957b11861..132c2734cc58 100644
@@ -32,6 +32,7 @@
 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
 
 #include "base.h"
+#include "physical_location.h"
 #include "power/power.h"
 
 #ifdef CONFIG_SYSFS_DEPRECATED
@@ -2649,8 +2650,17 @@ static int device_add_attrs(struct device *dev)
 			goto err_remove_dev_waiting_for_supplier;
 	}
 
+	if (dev_add_physical_location(dev)) {
+		error = device_add_group(dev,
+			&dev_attr_physical_location_group);
+		if (error)
+			goto err_remove_dev_physical_location;
+	}
+
 	return 0;
 
+ err_remove_dev_physical_location:
+	device_remove_group(dev, &dev_attr_physical_location_group);
  err_remove_dev_waiting_for_supplier:
 	device_remove_file(dev, &dev_attr_waiting_for_supplier);
  err_remove_dev_online:
drivers/base/physical_location.c new file +137 -0

137 lines not on this page · raw message

drivers/base/physical_location.h new file +16 -0
new file mode 100644
index 000000000000..82cde9f1b161
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Device physical location support
+ *
+ * Author: Won Chung <[email protected]>
+ */
+
+#include <linux/device.h>
+
+#ifdef CONFIG_ACPI
+extern bool dev_add_physical_location(struct device *dev);
+extern const struct attribute_group dev_attr_physical_location_group;
+#else
+static inline bool dev_add_physical_location(struct device *dev) { return false; };
+static const struct attribute_group dev_attr_physical_location_group = {};
+#endif
include/linux/device.h +73 -0

91 lines not on this page · raw message

Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#1 lore raw
Quoted 21 lines · Won Chung

When ACPI table includes _PLD fields for a device, create a new directory (physical_location) in sysfs to share _PLD fields.

Currently without PLD information, when there are multiple of same devices, it is hard to distinguish which device corresponds to which physical device at which location. For example, when there are two Type C connectors, it is hard to find out which connector corresponds to the Type C port on the left panel versus the Type C port on the right panel. With PLD information provided, we can determine which specific device at which location is doing what.

_PLD output includes much more fields, but only generic fields are added and exposed to sysfs, so that non-ACPI devices can also support it in the future. The minimal generic fields needed for locating a device are the following.
- panel
- vertical_position
- horizontal_position
- dock
- lid

Quoted 8 lines

---

Changes from v3
- Move dev_add_physical_location() and dev_attr_physical_location_group to driver/base/physical_location.h.
- Use pointer and reorder physical_location in struct device to pack its bytes. (checked using pahole)

You forgot to test your code and see what happens when you remove a device :(