18c2ecf20Sopenharmony_ci=============================
28c2ecf20Sopenharmony_ciThe Linux Kernel Device Model
38c2ecf20Sopenharmony_ci=============================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciPatrick Mochel	<mochel@digitalimplant.org>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciDrafted 26 August 2002
88c2ecf20Sopenharmony_ciUpdated 31 January 2006
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciOverview
128c2ecf20Sopenharmony_ci~~~~~~~~
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciThe Linux Kernel Driver Model is a unification of all the disparate driver
158c2ecf20Sopenharmony_cimodels that were previously used in the kernel. It is intended to augment the
168c2ecf20Sopenharmony_cibus-specific drivers for bridges and devices by consolidating a set of data
178c2ecf20Sopenharmony_ciand operations into globally accessible data structures.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciTraditional driver models implemented some sort of tree-like structure
208c2ecf20Sopenharmony_ci(sometimes just a list) for the devices they control. There wasn't any
218c2ecf20Sopenharmony_ciuniformity across the different bus types.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciThe current driver model provides a common, uniform data model for describing
248c2ecf20Sopenharmony_cia bus and the devices that can appear under the bus. The unified bus
258c2ecf20Sopenharmony_cimodel includes a set of common attributes which all busses carry, and a set
268c2ecf20Sopenharmony_ciof common callbacks, such as device discovery during bus probing, bus
278c2ecf20Sopenharmony_cishutdown, bus power management, etc.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciThe common device and bridge interface reflects the goals of the modern
308c2ecf20Sopenharmony_cicomputer: namely the ability to do seamless device "plug and play", power
318c2ecf20Sopenharmony_cimanagement, and hot plug. In particular, the model dictated by Intel and
328c2ecf20Sopenharmony_ciMicrosoft (namely ACPI) ensures that almost every device on almost any bus
338c2ecf20Sopenharmony_cion an x86-compatible system can work within this paradigm.  Of course,
348c2ecf20Sopenharmony_cinot every bus is able to support all such operations, although most
358c2ecf20Sopenharmony_cibuses support most of those operations.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciDownstream Access
398c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciCommon data fields have been moved out of individual bus layers into a common
428c2ecf20Sopenharmony_cidata structure. These fields must still be accessed by the bus layers,
438c2ecf20Sopenharmony_ciand sometimes by the device-specific drivers.
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciOther bus layers are encouraged to do what has been done for the PCI layer.
468c2ecf20Sopenharmony_cistruct pci_dev now looks like this::
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci  struct pci_dev {
498c2ecf20Sopenharmony_ci	...
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	struct device dev;     /* Generic device interface */
528c2ecf20Sopenharmony_ci	...
538c2ecf20Sopenharmony_ci  };
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciNote first that the struct device dev within the struct pci_dev is
568c2ecf20Sopenharmony_cistatically allocated. This means only one allocation on device discovery.
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciNote also that that struct device dev is not necessarily defined at the
598c2ecf20Sopenharmony_cifront of the pci_dev structure.  This is to make people think about what
608c2ecf20Sopenharmony_cithey're doing when switching between the bus driver and the global driver,
618c2ecf20Sopenharmony_ciand to discourage meaningless and incorrect casts between the two.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciThe PCI bus layer freely accesses the fields of struct device. It knows about
648c2ecf20Sopenharmony_cithe structure of struct pci_dev, and it should know the structure of struct
658c2ecf20Sopenharmony_cidevice. Individual PCI device drivers that have been converted to the current
668c2ecf20Sopenharmony_cidriver model generally do not and should not touch the fields of struct device,
678c2ecf20Sopenharmony_ciunless there is a compelling reason to do so.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciThe above abstraction prevents unnecessary pain during transitional phases.
708c2ecf20Sopenharmony_ciIf it were not done this way, then when a field was renamed or removed, every
718c2ecf20Sopenharmony_cidownstream driver would break.  On the other hand, if only the bus layer
728c2ecf20Sopenharmony_ci(and not the device layer) accesses the struct device, it is only the bus
738c2ecf20Sopenharmony_cilayer that needs to change.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciUser Interface
778c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciBy virtue of having a complete hierarchical view of all the devices in the
808c2ecf20Sopenharmony_cisystem, exporting a complete hierarchical view to userspace becomes relatively
818c2ecf20Sopenharmony_cieasy. This has been accomplished by implementing a special purpose virtual
828c2ecf20Sopenharmony_cifile system named sysfs.
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciAlmost all mainstream Linux distros mount this filesystem automatically; you
858c2ecf20Sopenharmony_cican see some variation of the following in the output of the "mount" command::
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci  $ mount
888c2ecf20Sopenharmony_ci  ...
898c2ecf20Sopenharmony_ci  none on /sys type sysfs (rw,noexec,nosuid,nodev)
908c2ecf20Sopenharmony_ci  ...
918c2ecf20Sopenharmony_ci  $
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciThe auto-mounting of sysfs is typically accomplished by an entry similar to
948c2ecf20Sopenharmony_cithe following in the /etc/fstab file::
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci  none     	/sys	sysfs    defaults	  	0 0
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cior something similar in the /lib/init/fstab file on Debian-based systems::
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci  none            /sys    sysfs    nodev,noexec,nosuid    0 0
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciIf sysfs is not automatically mounted, you can always do it manually with::
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	# mount -t sysfs sysfs /sys
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciWhenever a device is inserted into the tree, a directory is created for it.
1078c2ecf20Sopenharmony_ciThis directory may be populated at each layer of discovery - the global layer,
1088c2ecf20Sopenharmony_cithe bus layer, or the device layer.
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciThe global layer currently creates two files - 'name' and 'power'. The
1118c2ecf20Sopenharmony_ciformer only reports the name of the device. The latter reports the
1128c2ecf20Sopenharmony_cicurrent power state of the device. It will also be used to set the current
1138c2ecf20Sopenharmony_cipower state.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciThe bus layer may also create files for the devices it finds while probing the
1168c2ecf20Sopenharmony_cibus. For example, the PCI layer currently creates 'irq' and 'resource' files
1178c2ecf20Sopenharmony_cifor each PCI device.
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciA device-specific driver may also export files in its directory to expose
1208c2ecf20Sopenharmony_cidevice-specific data or tunable interfaces.
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciMore information about the sysfs directory layout can be found in
1238c2ecf20Sopenharmony_cithe other documents in this directory and in the file
1248c2ecf20Sopenharmony_ciDocumentation/filesystems/sysfs.rst.
125