aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/driver-model/driver.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/driver-model/driver.txt')
-rw-r--r--Documentation/driver-model/driver.txt287
1 files changed, 287 insertions, 0 deletions
diff --git a/Documentation/driver-model/driver.txt b/Documentation/driver-model/driver.txt
new file mode 100644
index 000000000000..12447787d329
--- /dev/null
+++ b/Documentation/driver-model/driver.txt
@@ -0,0 +1,287 @@
1
2Device Drivers
3
4struct device_driver {
5 char * name;
6 struct bus_type * bus;
7
8 rwlock_t lock;
9 atomic_t refcount;
10
11 list_t bus_list;
12 list_t devices;
13
14 struct driver_dir_entry dir;
15
16 int (*probe) (struct device * dev);
17 int (*remove) (struct device * dev);
18
19 int (*suspend) (struct device * dev, u32 state, u32 level);
20 int (*resume) (struct device * dev, u32 level);
21
22 void (*release) (struct device_driver * drv);
23};
24
25
26
27Allocation
28~~~~~~~~~~
29
30Device drivers are statically allocated structures. Though there may
31be multiple devices in a system that a driver supports, struct
32device_driver represents the driver as a whole (not a particular
33device instance).
34
35Initialization
36~~~~~~~~~~~~~~
37
38The driver must initialize at least the name and bus fields. It should
39also initialize the devclass field (when it arrives), so it may obtain
40the proper linkage internally. It should also initialize as many of
41the callbacks as possible, though each is optional.
42
43Declaration
44~~~~~~~~~~~
45
46As stated above, struct device_driver objects are statically
47allocated. Below is an example declaration of the eepro100
48driver. This declaration is hypothetical only; it relies on the driver
49being converted completely to the new model.
50
51static struct device_driver eepro100_driver = {
52 .name = "eepro100",
53 .bus = &pci_bus_type,
54 .devclass = &ethernet_devclass, /* when it's implemented */
55
56 .probe = eepro100_probe,
57 .remove = eepro100_remove,
58 .suspend = eepro100_suspend,
59 .resume = eepro100_resume,
60};
61
62Most drivers will not be able to be converted completely to the new
63model because the bus they belong to has a bus-specific structure with
64bus-specific fields that cannot be generalized.
65
66The most common example of this are device ID structures. A driver
67typically defines an array of device IDs that it supports. The format
68of these structures and the semantics for comparing device IDs are
69completely bus-specific. Defining them as bus-specific entities would
70sacrifice type-safety, so we keep bus-specific structures around.
71
72Bus-specific drivers should include a generic struct device_driver in
73the definition of the bus-specific driver. Like this:
74
75struct pci_driver {
76 const struct pci_device_id *id_table;
77 struct device_driver driver;
78};
79
80A definition that included bus-specific fields would look like
81(using the eepro100 driver again):
82
83static struct pci_driver eepro100_driver = {
84 .id_table = eepro100_pci_tbl,
85 .driver = {
86 .name = "eepro100",
87 .bus = &pci_bus_type,
88 .devclass = &ethernet_devclass, /* when it's implemented */
89 .probe = eepro100_probe,
90 .remove = eepro100_remove,
91 .suspend = eepro100_suspend,
92 .resume = eepro100_resume,
93 },
94};
95
96Some may find the syntax of embedded struct initialization awkward or
97even a bit ugly. So far, it's the best way we've found to do what we want...
98
99Registration
100~~~~~~~~~~~~
101
102int driver_register(struct device_driver * drv);
103
104The driver registers the structure on startup. For drivers that have
105no bus-specific fields (i.e. don't have a bus-specific driver
106structure), they would use driver_register and pass a pointer to their
107struct device_driver object.
108
109Most drivers, however, will have a bus-specific structure and will
110need to register with the bus using something like pci_driver_register.
111
112It is important that drivers register their driver structure as early as
113possible. Registration with the core initializes several fields in the
114struct device_driver object, including the reference count and the
115lock. These fields are assumed to be valid at all times and may be
116used by the device model core or the bus driver.
117
118
119Transition Bus Drivers
120~~~~~~~~~~~~~~~~~~~~~~
121
122By defining wrapper functions, the transition to the new model can be
123made easier. Drivers can ignore the generic structure altogether and
124let the bus wrapper fill in the fields. For the callbacks, the bus can
125define generic callbacks that forward the call to the bus-specific
126callbacks of the drivers.
127
128This solution is intended to be only temporary. In order to get class
129information in the driver, the drivers must be modified anyway. Since
130converting drivers to the new model should reduce some infrastructural
131complexity and code size, it is recommended that they are converted as
132class information is added.
133
134Access
135~~~~~~
136
137Once the object has been registered, it may access the common fields of
138the object, like the lock and the list of devices.
139
140int driver_for_each_dev(struct device_driver * drv, void * data,
141 int (*callback)(struct device * dev, void * data));
142
143The devices field is a list of all the devices that have been bound to
144the driver. The LDM core provides a helper function to operate on all
145the devices a driver controls. This helper locks the driver on each
146node access, and does proper reference counting on each device as it
147accesses it.
148
149
150sysfs
151~~~~~
152
153When a driver is registered, a sysfs directory is created in its
154bus's directory. In this directory, the driver can export an interface
155to userspace to control operation of the driver on a global basis;
156e.g. toggling debugging output in the driver.
157
158A future feature of this directory will be a 'devices' directory. This
159directory will contain symlinks to the directories of devices it
160supports.
161
162
163
164Callbacks
165~~~~~~~~~
166
167 int (*probe) (struct device * dev);
168
169probe is called to verify the existence of a certain type of
170hardware. This is called during the driver binding process, after the
171bus has verified that the device ID of a device matches one of the
172device IDs supported by the driver.
173
174This callback only verifies that there actually is supported hardware
175present. It may allocate a driver-specific structure, but it should
176not do any initialization of the hardware itself. The device-specific
177structure may be stored in the device's driver_data field.
178
179 int (*init) (struct device * dev);
180
181init is called during the binding stage. It is called after probe has
182successfully returned and the device has been registered with its
183class. It is responsible for initializing the hardware.
184
185 int (*remove) (struct device * dev);
186
187remove is called to dissociate a driver with a device. This may be
188called if a device is physically removed from the system, if the
189driver module is being unloaded, or during a reboot sequence.
190
191It is up to the driver to determine if the device is present or
192not. It should free any resources allocated specifically for the
193device; i.e. anything in the device's driver_data field.
194
195If the device is still present, it should quiesce the device and place
196it into a supported low-power state.
197
198 int (*suspend) (struct device * dev, u32 state, u32 level);
199
200suspend is called to put the device in a low power state. There are
201several stages to successfully suspending a device, which is denoted in
202the @level parameter. Breaking the suspend transition into several
203stages affords the platform flexibility in performing device power
204management based on the requirements of the system and the
205user-defined policy.
206
207SUSPEND_NOTIFY notifies the device that a suspend transition is about
208to happen. This happens on system power state transitions to verify
209that all devices can successfully suspend.
210
211A driver may choose to fail on this call, which should cause the
212entire suspend transition to fail. A driver should fail only if it
213knows that the device will not be able to be resumed properly when the
214system wakes up again. It could also fail if it somehow determines it
215is in the middle of an operation too important to stop.
216
217SUSPEND_DISABLE tells the device to stop I/O transactions. When it
218stops transactions, or what it should do with unfinished transactions
219is a policy of the driver. After this call, the driver should not
220accept any other I/O requests.
221
222SUSPEND_SAVE_STATE tells the device to save the context of the
223hardware. This includes any bus-specific hardware state and
224device-specific hardware state. A pointer to this saved state can be
225stored in the device's saved_state field.
226
227SUSPEND_POWER_DOWN tells the driver to place the device in the low
228power state requested.
229
230Whether suspend is called with a given level is a policy of the
231platform. Some levels may be omitted; drivers must not assume the
232reception of any level. However, all levels must be called in the
233order above; i.e. notification will always come before disabling;
234disabling the device will come before suspending the device.
235
236All calls are made with interrupts enabled, except for the
237SUSPEND_POWER_DOWN level.
238
239 int (*resume) (struct device * dev, u32 level);
240
241Resume is used to bring a device back from a low power state. Like the
242suspend transition, it happens in several stages.
243
244RESUME_POWER_ON tells the driver to set the power state to the state
245before the suspend call (The device could have already been in a low
246power state before the suspend call to put in a lower power state).
247
248RESUME_RESTORE_STATE tells the driver to restore the state saved by
249the SUSPEND_SAVE_STATE suspend call.
250
251RESUME_ENABLE tells the driver to start accepting I/O transactions
252again. Depending on driver policy, the device may already have pending
253I/O requests.
254
255RESUME_POWER_ON is called with interrupts disabled. The other resume
256levels are called with interrupts enabled.
257
258As with the various suspend stages, the driver must not assume that
259any other resume calls have been or will be made. Each call should be
260self-contained and not dependent on any external state.
261
262
263Attributes
264~~~~~~~~~~
265struct driver_attribute {
266 struct attribute attr;
267 ssize_t (*show)(struct device_driver *, char * buf, size_t count, loff_t off);
268 ssize_t (*store)(struct device_driver *, const char * buf, size_t count, loff_t off);
269};
270
271Device drivers can export attributes via their sysfs directories.
272Drivers can declare attributes using a DRIVER_ATTR macro that works
273identically to the DEVICE_ATTR macro.
274
275Example:
276
277DRIVER_ATTR(debug,0644,show_debug,store_debug);
278
279This is equivalent to declaring:
280
281struct driver_attribute driver_attr_debug;
282
283This can then be used to add and remove the attribute from the
284driver's directory using:
285
286int driver_create_file(struct device_driver *, struct driver_attribute *);
287void driver_remove_file(struct device_driver *, struct driver_attribute *);