Testing Linux I2C, USB, and PCI Drivers Using Userspace Tools and `Sysfs`
Linux provides robust mechanisms for interacting with devices via sysfs. This allows developers to query and manipulate kernel objects from user space. Tools like lsusb, lsi2c, and lspci offer easy ways to test USB, I2C, and PCI devices. This article explains how to use these tools, highlights their underlying mechanisms, and provides practical steps to test Linux drivers.
Understanding sysfs
sysfs is a virtual filesystem used to expose kernel and device attributes to user space. It allows users to access hardware details directly.
Key directories include:
/sys/class/: Lists device classes likeusb,i2c-dev, andpci./sys/devices/: Provides a hierarchical view of devices./sys/bus/: Groups devices and drivers by their bus types (e.g.,usb,i2c,pci).
Common Commands
1 | ls /sys/bus/usb/devices/ # List USB devices |
Testing USB Drivers with lsusb
lsusb is a user-space tool for listing USB devices. It retrieves device descriptors, vendor IDs, and product IDs using sysfs and /proc.
How lsusb Works
The source code of lsusb interacts with USB devices via libusb. Here’s a simplified excerpt from the source code:
1 |
|
Installing and Running lsusb
- Install
lsusb:1
sudo apt install usbutils
- Run
lsusbto list connected USB devices:1
lsusb
Testing I2C Drivers with lsi2c
lsi2c is a user-space tool for detecting and interacting with I2C devices. It communicates with devices using the /dev/i2c-* interface provided by the i2c-dev kernel module.
How lsi2c Works
The tool reads and writes I2C registers using the ioctl system call. Below is a simplified excerpt from the source code:
1 |
|
Installing and Running lsi2c
- Clone the repository:
1
2git clone https://github.com/costad2/i2cdev.git
cd i2cdev - Build and run:
1
2make
./lsi2c
Testing PCI Drivers with lspci
lspci is a user-space tool for listing PCI devices and querying their details. It uses the /sys/bus/pci/devices/ directory and the /proc/bus/pci filesystem.
How lspci Works
The source code interacts with PCI configuration space and device information files. Here’s a simplified example:
1 |
|
Installing and Running lspci
- Install
lspci:1
sudo apt install pciutils
- Run
lspcito list PCI devices:1
lspci
- Use
lspci -vvvto display detailed information about each device.
Practical Examples
USB Device Testing
- Connect a USB device.
- Use
lsusbto verify detection:1
lsusb
- Check driver binding:
1
ls /sys/bus/usb/drivers/
I2C Device Testing
- Load the
i2c-devkernel module:1
sudo modprobe i2c-dev
- Use
lsi2cto interact with an I2C device:1
./lsi2c
PCI Device Testing
- Run
lspcito list devices:1
lspci
- Check driver binding for a specific device:
1
lspci -vvv -s <bus:slot.func>
Conclusion
sysfs provides a rich interface for querying and manipulating kernel devices, while tools like lsusb, lsi2c, and lspci simplify testing and debugging of USB, I2C, and PCI devices. Understanding how these tools interact with the kernel and hardware equips developers to validate driver functionality and troubleshoot issues effectively.