Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | The Linux Kernel Device Model |
| 2 | |
| 3 | Patrick Mochel <mochel@osdl.org> |
| 4 | |
| 5 | 26 August 2002 |
| 6 | |
| 7 | |
| 8 | Overview |
| 9 | ~~~~~~~~ |
| 10 | |
| 11 | This driver model is a unification of all the current, disparate driver models |
| 12 | that are currently in the kernel. It is intended to augment the |
| 13 | bus-specific drivers for bridges and devices by consolidating a set of data |
| 14 | and operations into globally accessible data structures. |
| 15 | |
| 16 | Current driver models implement some sort of tree-like structure (sometimes |
| 17 | just a list) for the devices they control. But, there is no linkage between |
| 18 | the different bus types. |
| 19 | |
| 20 | A common data structure can provide this linkage with little overhead: when a |
| 21 | bus driver discovers a particular device, it can insert it into the global |
| 22 | tree as well as its local tree. In fact, the local tree becomes just a subset |
| 23 | of the global tree. |
| 24 | |
| 25 | Common data fields can also be moved out of the local bus models into the |
| 26 | global model. Some of the manipulations of these fields can also be |
| 27 | consolidated. Most likely, manipulation functions will become a set |
| 28 | of helper functions, which the bus drivers wrap around to include any |
| 29 | bus-specific items. |
| 30 | |
| 31 | The common device and bridge interface currently reflects the goals of the |
| 32 | modern PC: namely the ability to do seamless Plug and Play, power management, |
| 33 | and hot plug. (The model dictated by Intel and Microsoft (read: ACPI) ensures |
| 34 | us that any device in the system may fit any of these criteria.) |
| 35 | |
| 36 | In reality, not every bus will be able to support such operations. But, most |
| 37 | buses will support a majority of those operations, and all future buses will. |
| 38 | In other words, a bus that doesn't support an operation is the exception, |
| 39 | instead of the other way around. |
| 40 | |
| 41 | |
| 42 | |
| 43 | Downstream Access |
| 44 | ~~~~~~~~~~~~~~~~~ |
| 45 | |
| 46 | Common data fields have been moved out of individual bus layers into a common |
| 47 | data structure. But, these fields must still be accessed by the bus layers, |
| 48 | and sometimes by the device-specific drivers. |
| 49 | |
| 50 | Other bus layers are encouraged to do what has been done for the PCI layer. |
| 51 | struct pci_dev now looks like this: |
| 52 | |
| 53 | struct pci_dev { |
| 54 | ... |
| 55 | |
| 56 | struct device device; |
| 57 | }; |
| 58 | |
| 59 | Note first that it is statically allocated. This means only one allocation on |
| 60 | device discovery. Note also that it is at the _end_ of struct pci_dev. This is |
| 61 | to make people think about what they're doing when switching between the bus |
| 62 | driver and the global driver; and to prevent against mindless casts between |
| 63 | the two. |
| 64 | |
| 65 | The PCI bus layer freely accesses the fields of struct device. It knows about |
| 66 | the structure of struct pci_dev, and it should know the structure of struct |
| 67 | device. PCI devices that have been converted generally do not touch the fields |
| 68 | of struct device. More precisely, device-specific drivers should not touch |
| 69 | fields of struct device unless there is a strong compelling reason to do so. |
| 70 | |
| 71 | This abstraction is prevention of unnecessary pain during transitional phases. |
| 72 | If the name of the field changes or is removed, then every downstream driver |
| 73 | will break. On the other hand, if only the bus layer (and not the device |
| 74 | layer) accesses struct device, it is only that layer that needs to change. |
| 75 | |
| 76 | |
| 77 | User Interface |
| 78 | ~~~~~~~~~~~~~~ |
| 79 | |
| 80 | By virtue of having a complete hierarchical view of all the devices in the |
| 81 | system, exporting a complete hierarchical view to userspace becomes relatively |
| 82 | easy. This has been accomplished by implementing a special purpose virtual |
| 83 | file system named sysfs. It is hence possible for the user to mount the |
| 84 | whole sysfs filesystem anywhere in userspace. |
| 85 | |
| 86 | This can be done permanently by providing the following entry into the |
| 87 | /etc/fstab (under the provision that the mount point does exist, of course): |
| 88 | |
| 89 | none /sys sysfs defaults 0 0 |
| 90 | |
| 91 | Or by hand on the command line: |
| 92 | |
| 93 | # mount -t sysfs sysfs /sys |
| 94 | |
| 95 | Whenever a device is inserted into the tree, a directory is created for it. |
| 96 | This directory may be populated at each layer of discovery - the global layer, |
| 97 | the bus layer, or the device layer. |
| 98 | |
| 99 | The global layer currently creates two files - 'name' and 'power'. The |
| 100 | former only reports the name of the device. The latter reports the |
| 101 | current power state of the device. It will also be used to set the current |
| 102 | power state. |
| 103 | |
| 104 | The bus layer may also create files for the devices it finds while probing the |
| 105 | bus. For example, the PCI layer currently creates 'irq' and 'resource' files |
| 106 | for each PCI device. |
| 107 | |
| 108 | A device-specific driver may also export files in its directory to expose |
| 109 | device-specific data or tunable interfaces. |
| 110 | |
| 111 | More information about the sysfs directory layout can be found in |
| 112 | the other documents in this directory and in the file |
| 113 | Documentation/filesystems/sysfs.txt. |
| 114 | |