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