TEXT   53

overview.txt

Guest on 1st August 2021 06:09:17 AM

  1. The Linux Kernel Device Model
  2.  
  3. Patrick Mochel  <mochel@digitalimplant.org>
  4.  
  5.  
  6.  
  7. Overview
  8. ~~~~~~~~
  9.  
  10. The Linux Kernel Driver Model is a unification of all the disparate driver
  11. models that were previously used in the kernel. It is intended to augment the
  12. bus-specific drivers for bridges and devices by consolidating a set of data
  13. and operations into globally accessible data structures.
  14.  
  15. Traditional driver models implemented some sort of tree-like structure
  16. (sometimes just a list) for the devices they control. There wasn't any
  17. uniformity across the different bus types.
  18.  
  19. The current driver model provides a common, uniform data model for describing
  20. a bus and the devices that can appear under the bus. The unified bus
  21. model includes a set of common attributes which all busses carry, and a set
  22. of common callbacks, such as device discovery during bus probing, bus
  23. shutdown, bus power management, etc.
  24.  
  25. The common device and bridge interface reflects the goals of the modern
  26. computer: namely the ability to do seamless device "plug and play", power
  27. management, and hot plug. In particular, the model dictated by Intel and
  28. Microsoft (namely ACPI) ensures that almost every device on almost any bus
  29. on an x86-compatible system can work within this paradigm.  Of course,
  30. not every bus is able to support all such operations, although most
  31. buses support most of those operations.
  32.  
  33.  
  34. Downstream Access
  35. ~~~~~~~~~~~~~~~~~
  36.  
  37. Common data fields have been moved out of individual bus layers into a common
  38. data structure. These fields must still be accessed by the bus layers,
  39. and sometimes by the device-specific drivers.
  40.  
  41. Other bus layers are encouraged to do what has been done for the PCI layer.
  42. struct pci_dev now looks like this:
  43.  
  44. struct pci_dev {
  45.         ...
  46.  
  47.         struct device dev;     /* Generic device interface */
  48.         ...
  49. };
  50.  
  51. Note first that the struct device dev within the struct pci_dev is
  52. statically allocated. This means only one allocation on device discovery.
  53.  
  54. Note also that that struct device dev is not necessarily defined at the
  55. front of the pci_dev structure.  This is to make people think about what
  56. they're doing when switching between the bus driver and the global driver,
  57. and to discourage meaningless and incorrect casts between the two.
  58.  
  59. The PCI bus layer freely accesses the fields of struct device. It knows about
  60. the structure of struct pci_dev, and it should know the structure of struct
  61. device. Individual PCI device drivers that have been converted to the current
  62. driver model generally do not and should not touch the fields of struct device,
  63. unless there is a compelling reason to do so.
  64.  
  65. The above abstraction prevents unnecessary pain during transitional phases.
  66. If it were not done this way, then when a field was renamed or removed, every
  67. downstream driver would break.  On the other hand, if only the bus layer
  68. (and not the device layer) accesses the struct device, it is only the bus
  69. layer that needs to change.
  70.  
  71.  
  72. User Interface
  73. ~~~~~~~~~~~~~~
  74.  
  75. By virtue of having a complete hierarchical view of all the devices in the
  76. system, exporting a complete hierarchical view to userspace becomes relatively
  77. easy. This has been accomplished by implementing a special purpose virtual
  78. file system named sysfs.
  79.  
  80. Almost all mainstream Linux distros mount this filesystem automatically; you
  81. can see some variation of the following in the output of the "mount" command:
  82.  
  83. $ mount
  84. ...
  85. none on /sys type sysfs (rw,noexec,nosuid,nodev)
  86. ...
  87. $
  88.  
  89. The auto-mounting of sysfs is typically accomplished by an entry similar to
  90. the following in the /etc/fstab file:
  91.  
  92. none            /sys    sysfs    defaults               0 0
  93.  
  94. or something similar in the /lib/init/fstab file on Debian-based systems:
  95.  
  96. none            /sys    sysfs    nodev,noexec,nosuid    0 0
  97.  
  98. If sysfs is not automatically mounted, you can always do it manually with:
  99.  
  100. # mount -t sysfs sysfs /sys
  101.  
  102. Whenever a device is inserted into the tree, a directory is created for it.
  103. This directory may be populated at each layer of discovery - the global layer,
  104. the bus layer, or the device layer.
  105.  
  106. The global layer currently creates two files - 'name' and 'power'. The
  107. former only reports the name of the device. The latter reports the
  108. current power state of the device. It will also be used to set the current
  109. power state.
  110.  
  111. The bus layer may also create files for the devices it finds while probing the
  112. bus. For example, the PCI layer currently creates 'irq' and 'resource' files
  113. for each PCI device.
  114.  
  115. A device-specific driver may also export files in its directory to expose
  116. device-specific data or tunable interfaces.
  117.  
  118. More information about the sysfs directory layout can be found in
  119. the other documents in this directory and in the file
  120. Documentation/filesystems/sysfs.txt.

Raw Paste


Login or Register to edit or fork this paste. It's free.