Driving Me Nuts - Device Classes

More necessary instructions for making your new device driver play nice in the 2.6 kernel.
What It All Looks Like

With the i2c-dev driver and two i2c adapter drivers (the i2c-piix4 and i2c-isa drivers) loaded, the /sys/class/i2c-dev directory might look like the following:


$ tree /sys/class/i2c-dev/
/sys/class/i2c-dev/
|-- i2c-0
|   |-- dev
|   |-- device -> ../../../devices/pci0/00:07.3
|   `-- driver -> ../../../bus/pci/drivers/piix4-smbus
`-- i2c-2
    |-- dev
    |-- device -> ../../../devices/legacy/i2c-2
    `-- driver -> ../../../bus/i2c/drivers/i2c_adapter

The dev file in the /sys/class/i2c-dev/i2c-2/ directory would contain the following string:


$ cat /sys/class/i2c-dev/i2c-2/dev
5902

which corresponds to major number 86 and minor number 2, the character major and minor numbers for this specific device.

Also, the /sys/bus/i2c/ directory with a few i2c client drivers loaded looks like:


$ tree /sys/bus/i2c/
/sys/bus/i2c/
|-- devices
|   |-- 0-0050 -> ../../../devices/pci0/00:07.3/i2c-0/0-0050
|   |-- 0-0051 -> ../../../devices/pci0/00:07.3/i2c-0/0-0051
|   |-- 0-0052 -> ../../../devices/pci0/00:07.3/i2c-0/0-0052
|   |-- 0-0053 -> ../../../devices/pci0/00:07.3/i2c-0/0-0053
|   `-- 2-0290 -> ../../../devices/legacy/i2c-2/2-0290
`-- drivers
    |-- dev driver
    |-- eeprom
    |   |-- 0-0050 -> ../../../../devices/pci0/00:07.3/i2c-0/0-0050
    |   |-- 0-0051 -> ../../../../devices/pci0/00:07.3/i2c-0/0-0051
    |   |-- 0-0052 -> ../../../../devices/pci0/00:07.3/i2c-0/0-0052
    |   `-- 0-0053 -> ../../../../devices/pci0/00:07.3/i2c-0/0-0053
    |-- i2c_adapter
    `-- w83781d
        `-- 2-0290 -> ../../../../devices/legacy/i2c-2/2-0290

And, the actual /sys/devices/ directories for the i2c adapters look like:


$ tree /sys/devices/pci0/00:07.3
/sys/devices/pci0/00:07.3
|-- class
|-- device
|-- i2c-0
|   |-- 0-0050
|   |   |-- eeprom_00
|   |   |-- name
|   |   `-- power
|   |-- 0-0051
|   |   |-- eeprom_00
|   |   |-- name
|   |   `-- power
|   |-- 0-0052
|   |   |-- eeprom_00
|   |   |-- name
|   |   `-- power
|   |-- 0-0053
|   |   |-- eeprom_00
|   |   |-- name
|   |   `-- power
|   |-- name
|   `-- power
|-- irq
|-- name
|-- power
|-- resource
|-- subsystem_device
|-- subsystem_vendor
`-- vendor

and:


$ tree /sys/devices/legacy/i2c-2/
/sys/devices/legacy/i2c-2/
|-- 2-0290
|   |-- alarms
|   |-- beep_enable
|   |-- beep_mask
|   |-- fan_div1
|   |-- fan_div2
|   |-- fan_div3
|   |-- fan_input1
|   |-- fan_input2
|   |-- fan_input3
|   |-- fan_min1
|   |-- fan_min2
|   |-- fan_min3
|   |-- in_input0
|   |-- in_input1
|   |-- in_input2
|   |-- in_input3
|   |-- in_input4
|   |-- in_input5
|   |-- in_input6
|   |-- in_input7
|   |-- in_input8
|   |-- in_max0
|   |-- in_max1
|   |-- in_max2
|   |-- in_max3
|   |-- in_max4
|   |-- in_max5
|   |-- in_max6
|   |-- in_max7
|   |-- in_max8
|   |-- in_min0
|   |-- in_min1
|   |-- in_min2
|   |-- in_min3
|   |-- in_min4
|   |-- in_min5
|   |-- in_min6
|   |-- in_min7
|   |-- in_min8
|   |-- name
|   |-- power
|   |-- pwm1
|   |-- pwm2
|   |-- pwm_enable2
|   |-- sensor1
|   |-- sensor2
|   |-- sensor3
|   |-- temp_input1
|   |-- temp_input2
|   |-- temp_input3
|   |-- temp_max1
|   |-- temp_max2
|   |-- temp_max3
|   |-- temp_min1
|   |-- temp_min2
|   |-- temp_min3
|   |-- vid
|   `-- vrm
|-- name
`-- power

I think the best description of the kernel driver model's use of interconnected structure pointers and representation to the user was issued by Jonathan Corbet: “web woven by a spider on drugs” (lwn.net/Articles/31185/). Hopefully, these two articles have helped you unravel the loony web, showing the true interconnectedness of all devices within the kernel.

Acknowledgements

I would like to thank Pat Mochel for creating such a powerful and complete framework in which all kernel drivers and devices easily can be shown to the user. Also, a big thanks to all of the kernel driver subsystem maintainers who have gladly converted their subsystems over to this model; without their help, the driver core code would have been little more than a nice academic exercise.

Greg Kroah-Hartman is currently the Linux USB and PCI Hot Plug kernel maintainer. He works for IBM, doing various Linux kernel-related things and can be reached at greg@kroah.com.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Device Classes

Anonymous's picture

Cool description of sysfs, i wonder if it is possible to describe how a user-space app (kde, gnome, etc) could use it to some benefit for it

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Webcast
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up and watch now

Sponsored by Skybot