1. Why Do We Need to Know the Device Location in sysfs?
When learning driver development, you often need to locate "your device" in the sysfs (/sys) directory tree. Understanding the device mapping relationship helps you:
- Observe where the device you are registering or debugging appears;
- Understand device organization, parent-child relationships, and physical/logical classification;
- Proficiently read and manipulate various device attribute files.
2. Main Entry Points for Device Mapping in sysfs
The Linux device model automatically generates dedicated sysfs directories and attributes for each device, primarily appearing in the following paths:
/sys/devices/Maps the real device topology (tree structure, representing physical or virtual connections).
/sys/class/Logical device directories organized by functional category, such as
net(network devices),block(block devices),leds(LEDs),input(input devices), etc./sys/bus/<bus-name>/devices/Device list grouped by bus membership. For example, all devices under the platform bus are listed in
/sys/bus/platform/devices/./sys/bus/<bus-name>/drivers/Registered drivers grouped by bus membership.
3. Practical Examples
1. Real Topology Structure
Linux organizes all devices in a hierarchical manner under
/sys/devices/based on physical or virtual "bus relationships". For example:bash/sys/devices/ platform/ soc/ 1c28000.i2c/ 1c28000.i2c:devicexx/ pci0000:00/ 0000:00:14.0/ 0000:01:00.0/ virtual/ net/ lo/1
2
3
4
5
6
7
8
9
10
11You can recursively view all registered physical (or virtual) devices.
Each device is a directory containing numerous attribute files (such as uevent, power, subsystem, driver, resource registers, etc.).
2. Grouped by Function
- Under
/sys/class/, devices are categorized by "functional type", for example:
/sys/class/net/ # Network devices
/sys/class/leds/ # LED devices
/sys/class/input/ # Input devices
/sys/class/block/ # Block devices (disks/partitions)2
3
4
- The "devices" under these directories are often symlinks — pointing to the real device directories in
/sys/devices/, facilitating unified management by function.
3. Grouped by Bus
/sys/bus/usb/devices/: All recognized devices under the USB bus./sys/bus/platform/devices/: All devices under the platform bus (embedded CPU/chip internal resources, etc.).- These directories are also usually symlinks, pointing to actual device nodes under
/sys/devices/.
For example:
4. Quickly Locating Your Device
1. Determine Device Type
- For example, when developing an LED driver or looking for LED nodes:
ls /sys/class/leds/
# List all LED names2
2. Trace Symlinks to Physical Devices
- Taking LED "work" as an example:
ls -l /sys/class/leds/workOutput:
lrwxrwxrwx 1 root root 0 Dec 16 18:42 /sys/class/leds/work -> ../../devices/platform/leds/leds/workThis indicates that /sys/class/leds/work actually points to /sys/devices/platform/leds/leds/work
- You can
cdinto that directory to view all detailed attributes, drivers, parent-child relationships bound to that physical device.
3. Search by Bus
- If you are developing an I2C device, you can use:
ls /sys/bus/i2c/devices/- Each entry is an I2C device node link, pointing to the corresponding location in
/sys/devices/.
5. Practical Examples with Commands
- Find all character devices and quickly locate parent devices/drivers:
# Find /dev/tty0
udevadm info -a -p $(udevadm info -q path -n /dev/tty0)2
- Check which driver a device belongs to:
ls -l /sys/class/net/eth0/device/driverAs shown in the figure, it is the
rk_gmac-dwmacdriver
6. Diagram
/sys- Root node, i.e., the mount point of the sysfs filesystem.
/sys/devices/...- Represents the "real device topology tree"
- Path:
/sys/devices/platform/soc/.../work - This shows all real devices and their physical/virtual hierarchy.
- For example, platform devices common in embedded devices, peripherals connected internally to the SoC (System on Chip), etc.
/sys/class/...- Represents the "functional classification index"
- Path:
/sys/class/leds/work - This is classified by the device's "functional type" — for example, all LED devices, all network devices (net), all block devices (block), etc.
- Device names in these directories are usually symlinks, actually pointing to real device nodes under /devices.
- Convenient for users to find devices by purpose.
/sys/bus/...- Represents "bus organization and device-driver relationships"
- Example path:
/sys/bus/platform/devices/work - Buses are a management structure in Linux, such as platform, i2c, PCI, USB, etc.
- Each bus has its own devices and drivers subdirectories.
/bus/platform/devices/workis also a symlink, pointing to the actual device at/devices/.../work.
7. Summary
- A device usually has multiple entry paths in sysfs (physical topology, functional category, bus membership).
/sys/devices/is the "real physical" existence of devices, while class/bus are classifications or indexes (symlinks).- Proficiently finding the full path of your device in sysfs is a fundamental skill for driver development and debugging.