In the world of Linux, sysfs and USB play crucial roles in system management and device interaction. This guide will provide an in-depth understanding of sysfs and USB, covering their functionalities, usage, and importance in Linux systems. Whether you’re a developer or an enthusiast, this comprehensive guide will help you grasp these concepts and utilize them effectively.
What is sysfs?
sysfs is a virtual filesystem in Linux that provides a standardized interface for kernel objects. It is mounted at /sys
and offers a hierarchical view of kernel subsystems, hardware devices, and their attributes. sysfs was introduced to provide a more structured and user-friendly way of interacting with kernel data.
Key Features of sysfs:
- Hierarchical Structure: sysfs organizes kernel objects in a hierarchical manner, making it easy to navigate and access relevant information.
- Device and Driver Information: It provides detailed information about devices and their associated drivers, aiding in debugging and system management.
- User-Space Interaction: sysfs allows user-space applications to interact with kernel objects by reading and writing to files in the
/sys
directory. - Attributes: Each file in sysfs represents an attribute of a kernel object, such as configuration parameters or status information.
Common sysfs Directories:
/sys/class
: Contains class devices and their attributes./sys/block
: Contains block devices like hard drives and their attributes./sys/bus
: Contains information about various buses (e.g., USB, PCI) and devices connected to them./sys/devices
: Contains a tree of devices and their attributes.
Example Usage of sysfs:
To view information about a specific device, you can navigate to its directory in /sys
and read the relevant files. For example, to check the status of a USB device:
cd /sys/bus/usb/devices/usb1
cat product
cat manufacturer
Understanding USB in Linux
USB (Universal Serial Bus) is a standard for connecting peripherals to a computer. In Linux, USB devices are managed by the kernel, and information about these devices is exposed through sysfs.
Key Concepts of USB in Linux:
- Device Enumeration: When a USB device is connected, the kernel detects it, assigns it a unique address, and loads the appropriate driver.
- Driver Binding: The kernel uses device descriptors to identify the appropriate driver for the connected USB device.
- Hotplugging: USB supports hotplugging, allowing devices to be connected and disconnected without rebooting the system.
USB sysfs Directories:
/sys/bus/usb
: Contains information about the USB bus and connected devices./sys/class/usb_device
: Contains USB device classes and their attributes./sys/devices/platform/usb
: Contains information about USB controllers and ports.
Managing USB Devices with sysfs:
You can manage and interact with USB devices using sysfs. For instance, to unbind a USB device from its driver:
echo -n "usb1" > /sys/bus/usb/drivers/usb/unbind
To rebind the device to its driver:
echo -n "usb1" > /sys/bus/usb/drivers/usb/bind
Practical Example: Listing USB Devices
You can list all connected USB devices and their details using the lsusb
command, which utilizes sysfs information:
lsusb
This command provides a summary of connected USB devices, including their IDs and descriptions.
Conclusion
Understanding sysfs and USB in Linux is essential for system administrators, developers, and enthusiasts. sysfs provides a structured and user-friendly interface for accessing kernel objects, while USB management ensures seamless interaction with peripheral devices. By leveraging sysfs and USB, you can gain deeper insights into your Linux system and optimize device management.
This guide has provided a comprehensive overview of sysfs and USB, covering their functionalities, key concepts, and practical usage. By mastering these tools, you can enhance your Linux system management skills and improve your overall productivity.
Other viewpoint..
As we know the USB itself is a very complex peripheral to understand and work upon for development, due to such complexity the sysfs representation of USB bus and the peripherals attached to it is also complex.
We can now see how many root hubs our machine has as below,
$ cd /sys/bus/usb/devices
$ ls -al usb*
usb1 -> ../../../devices/pci0000:00/0000:00:14.0/usb1
usb2 -> ../../../devices/pci0000:00/0000:00:14.0/usb2
This shows that it has 2 root hubs. Now, lets try to correlate it with lsusb. A simple shell script which will print only RootHubs available on your machine.
$ vim lsusb.sh
#!/bin/sh
TOTAL_BUS_NUM=$1
for (( i=1; i <= $TOTAL_BUS_NUM; i++ ))
do
lsusb -s $i:1
done
Run it as “bash lsusb.sh 2” [ Note: here 2 is the number of root hubs we seen in sysfs ]
$ bash lsusb.sh 2
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Reference’s