overview.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. The Linux Kernel Device Model
  2. Patrick Mochel <mochel@digitalimplant.org>
  3. Drafted 26 August 2002
  4. Updated 31 January 2006
  5. Overview
  6. ~~~~~~~~
  7. The Linux Kernel Driver Model is a unification of all the disparate driver
  8. models that were previously used in the kernel. It is intended to augment the
  9. bus-specific drivers for bridges and devices by consolidating a set of data
  10. and operations into globally accessible data structures.
  11. Traditional driver models implemented some sort of tree-like structure
  12. (sometimes just a list) for the devices they control. There wasn't any
  13. uniformity across the different bus types.
  14. The current driver model provides a common, uniform data model for describing
  15. a bus and the devices that can appear under the bus. The unified bus
  16. model includes a set of common attributes which all busses carry, and a set
  17. of common callbacks, such as device discovery during bus probing, bus
  18. shutdown, bus power management, etc.
  19. The common device and bridge interface reflects the goals of the modern
  20. computer: namely the ability to do seamless device "plug and play", power
  21. management, and hot plug. In particular, the model dictated by Intel and
  22. Microsoft (namely ACPI) ensures that almost every device on almost any bus
  23. on an x86-compatible system can work within this paradigm. Of course,
  24. not every bus is able to support all such operations, although most
  25. buses support a most of those operations.
  26. Downstream Access
  27. ~~~~~~~~~~~~~~~~~
  28. Common data fields have been moved out of individual bus layers into a common
  29. data structure. These fields must still be accessed by the bus layers,
  30. and sometimes by the device-specific drivers.
  31. Other bus layers are encouraged to do what has been done for the PCI layer.
  32. struct pci_dev now looks like this:
  33. struct pci_dev {
  34. ...
  35. struct device dev;
  36. };
  37. Note first that it is statically allocated. This means only one allocation on
  38. device discovery. Note also that it is at the _end_ of struct pci_dev. This is
  39. to make people think about what they're doing when switching between the bus
  40. driver and the global driver; and to prevent against mindless casts between
  41. the two.
  42. The PCI bus layer freely accesses the fields of struct device. It knows about
  43. the structure of struct pci_dev, and it should know the structure of struct
  44. device. Individual PCI device drivers that have been converted to the current
  45. driver model generally do not and should not touch the fields of struct device,
  46. unless there is a strong compelling reason to do so.
  47. This abstraction is prevention of unnecessary pain during transitional phases.
  48. If the name of the field changes or is removed, then every downstream driver
  49. will break. On the other hand, if only the bus layer (and not the device
  50. layer) accesses struct device, it is only that layer that needs to change.
  51. User Interface
  52. ~~~~~~~~~~~~~~
  53. By virtue of having a complete hierarchical view of all the devices in the
  54. system, exporting a complete hierarchical view to userspace becomes relatively
  55. easy. This has been accomplished by implementing a special purpose virtual
  56. file system named sysfs. It is hence possible for the user to mount the
  57. whole sysfs filesystem anywhere in userspace.
  58. This can be done permanently by providing the following entry into the
  59. /etc/fstab (under the provision that the mount point does exist, of course):
  60. none /sys sysfs defaults 0 0
  61. Or by hand on the command line:
  62. # mount -t sysfs sysfs /sys
  63. Whenever a device is inserted into the tree, a directory is created for it.
  64. This directory may be populated at each layer of discovery - the global layer,
  65. the bus layer, or the device layer.
  66. The global layer currently creates two files - 'name' and 'power'. The
  67. former only reports the name of the device. The latter reports the
  68. current power state of the device. It will also be used to set the current
  69. power state.
  70. The bus layer may also create files for the devices it finds while probing the
  71. bus. For example, the PCI layer currently creates 'irq' and 'resource' files
  72. for each PCI device.
  73. A device-specific driver may also export files in its directory to expose
  74. device-specific data or tunable interfaces.
  75. More information about the sysfs directory layout can be found in
  76. the other documents in this directory and in the file
  77. Documentation/filesystems/sysfs.txt.