0001 =============================
0002 The Linux Kernel Device Model
0003 =============================
0004
0005 Patrick Mochel <mochel@digitalimplant.org>
0006
0007 Drafted 26 August 2002
0008 Updated 31 January 2006
0009
0010
0011 Overview
0012 ~~~~~~~~
0013
0014 The Linux Kernel Driver Model is a unification of all the disparate driver
0015 models that were previously used in the kernel. It is intended to augment the
0016 bus-specific drivers for bridges and devices by consolidating a set of data
0017 and operations into globally accessible data structures.
0018
0019 Traditional driver models implemented some sort of tree-like structure
0020 (sometimes just a list) for the devices they control. There wasn't any
0021 uniformity across the different bus types.
0022
0023 The current driver model provides a common, uniform data model for describing
0024 a bus and the devices that can appear under the bus. The unified bus
0025 model includes a set of common attributes which all busses carry, and a set
0026 of common callbacks, such as device discovery during bus probing, bus
0027 shutdown, bus power management, etc.
0028
0029 The common device and bridge interface reflects the goals of the modern
0030 computer: namely the ability to do seamless device "plug and play", power
0031 management, and hot plug. In particular, the model dictated by Intel and
0032 Microsoft (namely ACPI) ensures that almost every device on almost any bus
0033 on an x86-compatible system can work within this paradigm. Of course,
0034 not every bus is able to support all such operations, although most
0035 buses support most of those operations.
0036
0037
0038 Downstream Access
0039 ~~~~~~~~~~~~~~~~~
0040
0041 Common data fields have been moved out of individual bus layers into a common
0042 data structure. These fields must still be accessed by the bus layers,
0043 and sometimes by the device-specific drivers.
0044
0045 Other bus layers are encouraged to do what has been done for the PCI layer.
0046 struct pci_dev now looks like this::
0047
0048 struct pci_dev {
0049 ...
0050
0051 struct device dev; /* Generic device interface */
0052 ...
0053 };
0054
0055 Note first that the struct device dev within the struct pci_dev is
0056 statically allocated. This means only one allocation on device discovery.
0057
0058 Note also that that struct device dev is not necessarily defined at the
0059 front of the pci_dev structure. This is to make people think about what
0060 they're doing when switching between the bus driver and the global driver,
0061 and to discourage meaningless and incorrect casts between the two.
0062
0063 The PCI bus layer freely accesses the fields of struct device. It knows about
0064 the structure of struct pci_dev, and it should know the structure of struct
0065 device. Individual PCI device drivers that have been converted to the current
0066 driver model generally do not and should not touch the fields of struct device,
0067 unless there is a compelling reason to do so.
0068
0069 The above abstraction prevents unnecessary pain during transitional phases.
0070 If it were not done this way, then when a field was renamed or removed, every
0071 downstream driver would break. On the other hand, if only the bus layer
0072 (and not the device layer) accesses the struct device, it is only the bus
0073 layer that needs to change.
0074
0075
0076 User Interface
0077 ~~~~~~~~~~~~~~
0078
0079 By virtue of having a complete hierarchical view of all the devices in the
0080 system, exporting a complete hierarchical view to userspace becomes relatively
0081 easy. This has been accomplished by implementing a special purpose virtual
0082 file system named sysfs.
0083
0084 Almost all mainstream Linux distros mount this filesystem automatically; you
0085 can see some variation of the following in the output of the "mount" command::
0086
0087 $ mount
0088 ...
0089 none on /sys type sysfs (rw,noexec,nosuid,nodev)
0090 ...
0091 $
0092
0093 The auto-mounting of sysfs is typically accomplished by an entry similar to
0094 the following in the /etc/fstab file::
0095
0096 none /sys sysfs defaults 0 0
0097
0098 or something similar in the /lib/init/fstab file on Debian-based systems::
0099
0100 none /sys sysfs nodev,noexec,nosuid 0 0
0101
0102 If sysfs is not automatically mounted, you can always do it manually with::
0103
0104 # mount -t sysfs sysfs /sys
0105
0106 Whenever a device is inserted into the tree, a directory is created for it.
0107 This directory may be populated at each layer of discovery - the global layer,
0108 the bus layer, or the device layer.
0109
0110 The global layer currently creates two files - 'name' and 'power'. The
0111 former only reports the name of the device. The latter reports the
0112 current power state of the device. It will also be used to set the current
0113 power state.
0114
0115 The bus layer may also create files for the devices it finds while probing the
0116 bus. For example, the PCI layer currently creates 'irq' and 'resource' files
0117 for each PCI device.
0118
0119 A device-specific driver may also export files in its directory to expose
0120 device-specific data or tunable interfaces.
0121
0122 More information about the sysfs directory layout can be found in
0123 the other documents in this directory and in the file
0124 Documentation/filesystems/sysfs.rst.