aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/driver-model
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /Documentation/driver-model
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'Documentation/driver-model')
-rw-r--r--Documentation/driver-model/binding.txt102
-rw-r--r--Documentation/driver-model/bus.txt160
-rw-r--r--Documentation/driver-model/class.txt162
-rw-r--r--Documentation/driver-model/device.txt154
-rw-r--r--Documentation/driver-model/driver.txt287
-rw-r--r--Documentation/driver-model/interface.txt129
-rw-r--r--Documentation/driver-model/overview.txt114
-rw-r--r--Documentation/driver-model/platform.txt99
-rw-r--r--Documentation/driver-model/porting.txt445
9 files changed, 1652 insertions, 0 deletions
diff --git a/Documentation/driver-model/binding.txt b/Documentation/driver-model/binding.txt
new file mode 100644
index 000000000000..f7ec9d625bfc
--- /dev/null
+++ b/Documentation/driver-model/binding.txt
@@ -0,0 +1,102 @@
1
2Driver Binding
3
4Driver binding is the process of associating a device with a device
5driver that can control it. Bus drivers have typically handled this
6because there have been bus-specific structures to represent the
7devices and the drivers. With generic device and device driver
8structures, most of the binding can take place using common code.
9
10
11Bus
12~~~
13
14The bus type structure contains a list of all devices that are on that bus
15type in the system. When device_register is called for a device, it is
16inserted into the end of this list. The bus object also contains a
17list of all drivers of that bus type. When driver_register is called
18for a driver, it is inserted at the end of this list. These are the
19two events which trigger driver binding.
20
21
22device_register
23~~~~~~~~~~~~~~~
24
25When a new device is added, the bus's list of drivers is iterated over
26to find one that supports it. In order to determine that, the device
27ID of the device must match one of the device IDs that the driver
28supports. The format and semantics for comparing IDs is bus-specific.
29Instead of trying to derive a complex state machine and matching
30algorithm, it is up to the bus driver to provide a callback to compare
31a device against the IDs of a driver. The bus returns 1 if a match was
32found; 0 otherwise.
33
34int match(struct device * dev, struct device_driver * drv);
35
36If a match is found, the device's driver field is set to the driver
37and the driver's probe callback is called. This gives the driver a
38chance to verify that it really does support the hardware, and that
39it's in a working state.
40
41Device Class
42~~~~~~~~~~~~
43
44Upon the successful completion of probe, the device is registered with
45the class to which it belongs. Device drivers belong to one and only one
46class, and that is set in the driver's devclass field.
47devclass_add_device is called to enumerate the device within the class
48and actually register it with the class, which happens with the
49class's register_dev callback.
50
51NOTE: The device class structures and core routines to manipulate them
52are not in the mainline kernel, so the discussion is still a bit
53speculative.
54
55
56Driver
57~~~~~~
58
59When a driver is attached to a device, the device is inserted into the
60driver's list of devices.
61
62
63sysfs
64~~~~~
65
66A symlink is created in the bus's 'devices' directory that points to
67the device's directory in the physical hierarchy.
68
69A symlink is created in the driver's 'devices' directory that points
70to the device's directory in the physical hierarchy.
71
72A directory for the device is created in the class's directory. A
73symlink is created in that directory that points to the device's
74physical location in the sysfs tree.
75
76A symlink can be created (though this isn't done yet) in the device's
77physical directory to either its class directory, or the class's
78top-level directory. One can also be created to point to its driver's
79directory also.
80
81
82driver_register
83~~~~~~~~~~~~~~~
84
85The process is almost identical for when a new driver is added.
86The bus's list of devices is iterated over to find a match. Devices
87that already have a driver are skipped. All the devices are iterated
88over, to bind as many devices as possible to the driver.
89
90
91Removal
92~~~~~~~
93
94When a device is removed, the reference count for it will eventually
95go to 0. When it does, the remove callback of the driver is called. It
96is removed from the driver's list of devices and the reference count
97of the driver is decremented. All symlinks between the two are removed.
98
99When a driver is removed, the list of devices that it supports is
100iterated over, and the driver's remove callback is called for each
101one. The device is removed from that list and the symlinks removed.
102
diff --git a/Documentation/driver-model/bus.txt b/Documentation/driver-model/bus.txt
new file mode 100644
index 000000000000..dd62c7b80b3f
--- /dev/null
+++ b/Documentation/driver-model/bus.txt
@@ -0,0 +1,160 @@
1
2Bus Types
3
4Definition
5~~~~~~~~~~
6
7struct bus_type {
8 char * name;
9
10 struct subsystem subsys;
11 struct kset drivers;
12 struct kset devices;
13
14 struct bus_attribute * bus_attrs;
15 struct device_attribute * dev_attrs;
16 struct driver_attribute * drv_attrs;
17
18 int (*match)(struct device * dev, struct device_driver * drv);
19 int (*hotplug) (struct device *dev, char **envp,
20 int num_envp, char *buffer, int buffer_size);
21 int (*suspend)(struct device * dev, u32 state);
22 int (*resume)(struct device * dev);
23};
24
25int bus_register(struct bus_type * bus);
26
27
28Declaration
29~~~~~~~~~~~
30
31Each bus type in the kernel (PCI, USB, etc) should declare one static
32object of this type. They must initialize the name field, and may
33optionally initialize the match callback.
34
35struct bus_type pci_bus_type = {
36 .name = "pci",
37 .match = pci_bus_match,
38};
39
40The structure should be exported to drivers in a header file:
41
42extern struct bus_type pci_bus_type;
43
44
45Registration
46~~~~~~~~~~~~
47
48When a bus driver is initialized, it calls bus_register. This
49initializes the rest of the fields in the bus object and inserts it
50into a global list of bus types. Once the bus object is registered,
51the fields in it are usable by the bus driver.
52
53
54Callbacks
55~~~~~~~~~
56
57match(): Attaching Drivers to Devices
58~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59
60The format of device ID structures and the semantics for comparing
61them are inherently bus-specific. Drivers typically declare an array
62of device IDs of devices they support that reside in a bus-specific
63driver structure.
64
65The purpose of the match callback is provide the bus an opportunity to
66determine if a particular driver supports a particular device by
67comparing the device IDs the driver supports with the device ID of a
68particular device, without sacrificing bus-specific functionality or
69type-safety.
70
71When a driver is registered with the bus, the bus's list of devices is
72iterated over, and the match callback is called for each device that
73does not have a driver associated with it.
74
75
76
77Device and Driver Lists
78~~~~~~~~~~~~~~~~~~~~~~~
79
80The lists of devices and drivers are intended to replace the local
81lists that many buses keep. They are lists of struct devices and
82struct device_drivers, respectively. Bus drivers are free to use the
83lists as they please, but conversion to the bus-specific type may be
84necessary.
85
86The LDM core provides helper functions for iterating over each list.
87
88int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
89 int (*fn)(struct device *, void *));
90
91int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
92 void * data, int (*fn)(struct device_driver *, void *));
93
94These helpers iterate over the respective list, and call the callback
95for each device or driver in the list. All list accesses are
96synchronized by taking the bus's lock (read currently). The reference
97count on each object in the list is incremented before the callback is
98called; it is decremented after the next object has been obtained. The
99lock is not held when calling the callback.
100
101
102sysfs
103~~~~~~~~
104There is a top-level directory named 'bus'.
105
106Each bus gets a directory in the bus directory, along with two default
107directories:
108
109 /sys/bus/pci/
110 |-- devices
111 `-- drivers
112
113Drivers registered with the bus get a directory in the bus's drivers
114directory:
115
116 /sys/bus/pci/
117 |-- devices
118 `-- drivers
119 |-- Intel ICH
120 |-- Intel ICH Joystick
121 |-- agpgart
122 `-- e100
123
124Each device that is discovered on a bus of that type gets a symlink in
125the bus's devices directory to the device's directory in the physical
126hierarchy:
127
128 /sys/bus/pci/
129 |-- devices
130 | |-- 00:00.0 -> ../../../root/pci0/00:00.0
131 | |-- 00:01.0 -> ../../../root/pci0/00:01.0
132 | `-- 00:02.0 -> ../../../root/pci0/00:02.0
133 `-- drivers
134
135
136Exporting Attributes
137~~~~~~~~~~~~~~~~~~~~
138struct bus_attribute {
139 struct attribute attr;
140 ssize_t (*show)(struct bus_type *, char * buf);
141 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
142};
143
144Bus drivers can export attributes using the BUS_ATTR macro that works
145similarly to the DEVICE_ATTR macro for devices. For example, a definition
146like this:
147
148static BUS_ATTR(debug,0644,show_debug,store_debug);
149
150is equivalent to declaring:
151
152static bus_attribute bus_attr_debug;
153
154This can then be used to add and remove the attribute from the bus's
155sysfs directory using:
156
157int bus_create_file(struct bus_type *, struct bus_attribute *);
158void bus_remove_file(struct bus_type *, struct bus_attribute *);
159
160
diff --git a/Documentation/driver-model/class.txt b/Documentation/driver-model/class.txt
new file mode 100644
index 000000000000..2d1d893a5e5d
--- /dev/null
+++ b/Documentation/driver-model/class.txt
@@ -0,0 +1,162 @@
1
2Device Classes
3
4
5Introduction
6~~~~~~~~~~~~
7A device class describes a type of device, like an audio or network
8device. The following device classes have been identified:
9
10<Insert List of Device Classes Here>
11
12
13Each device class defines a set of semantics and a programming interface
14that devices of that class adhere to. Device drivers are the
15implemention of that programming interface for a particular device on
16a particular bus.
17
18Device classes are agnostic with respect to what bus a device resides
19on.
20
21
22Programming Interface
23~~~~~~~~~~~~~~~~~~~~~
24The device class structure looks like:
25
26
27typedef int (*devclass_add)(struct device *);
28typedef void (*devclass_remove)(struct device *);
29
30struct device_class {
31 char * name;
32 rwlock_t lock;
33 u32 devnum;
34 struct list_head node;
35
36 struct list_head drivers;
37 struct list_head intf_list;
38
39 struct driver_dir_entry dir;
40 struct driver_dir_entry device_dir;
41 struct driver_dir_entry driver_dir;
42
43 devclass_add add_device;
44 devclass_remove remove_device;
45};
46
47A typical device class definition would look like:
48
49struct device_class input_devclass = {
50 .name = "input",
51 .add_device = input_add_device,
52 .remove_device = input_remove_device,
53};
54
55Each device class structure should be exported in a header file so it
56can be used by drivers, extensions and interfaces.
57
58Device classes are registered and unregistered with the core using:
59
60int devclass_register(struct device_class * cls);
61void devclass_unregister(struct device_class * cls);
62
63
64Devices
65~~~~~~~
66As devices are bound to drivers, they are added to the device class
67that the driver belongs to. Before the driver model core, this would
68typically happen during the driver's probe() callback, once the device
69has been initialized. It now happens after the probe() callback
70finishes from the core.
71
72The device is enumerated in the class. Each time a device is added to
73the class, the class's devnum field is incremented and assigned to the
74device. The field is never decremented, so if the device is removed
75from the class and re-added, it will receive a different enumerated
76value.
77
78The class is allowed to create a class-specific structure for the
79device and store it in the device's class_data pointer.
80
81There is no list of devices in the device class. Each driver has a
82list of devices that it supports. The device class has a list of
83drivers of that particular class. To access all of the devices in the
84class, iterate over the device lists of each driver in the class.
85
86
87Device Drivers
88~~~~~~~~~~~~~~
89Device drivers are added to device classes when they are registered
90with the core. A driver specifies the class it belongs to by setting
91the struct device_driver::devclass field.
92
93
94sysfs directory structure
95~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96There is a top-level sysfs directory named 'class'.
97
98Each class gets a directory in the class directory, along with two
99default subdirectories:
100
101 class/
102 `-- input
103 |-- devices
104 `-- drivers
105
106
107Drivers registered with the class get a symlink in the drivers/ directory
108that points to the driver's directory (under its bus directory):
109
110 class/
111 `-- input
112 |-- devices
113 `-- drivers
114 `-- usb:usb_mouse -> ../../../bus/drivers/usb_mouse/
115
116
117Each device gets a symlink in the devices/ directory that points to the
118device's directory in the physical hierarchy:
119
120 class/
121 `-- input
122 |-- devices
123 | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
124 `-- drivers
125
126
127Exporting Attributes
128~~~~~~~~~~~~~~~~~~~~
129struct devclass_attribute {
130 struct attribute attr;
131 ssize_t (*show)(struct device_class *, char * buf, size_t count, loff_t off);
132 ssize_t (*store)(struct device_class *, const char * buf, size_t count, loff_t off);
133};
134
135Class drivers can export attributes using the DEVCLASS_ATTR macro that works
136similarly to the DEVICE_ATTR macro for devices. For example, a definition
137like this:
138
139static DEVCLASS_ATTR(debug,0644,show_debug,store_debug);
140
141is equivalent to declaring:
142
143static devclass_attribute devclass_attr_debug;
144
145The bus driver can add and remove the attribute from the class's
146sysfs directory using:
147
148int devclass_create_file(struct device_class *, struct devclass_attribute *);
149void devclass_remove_file(struct device_class *, struct devclass_attribute *);
150
151In the example above, the file will be named 'debug' in placed in the
152class's directory in sysfs.
153
154
155Interfaces
156~~~~~~~~~~
157There may exist multiple mechanisms for accessing the same device of a
158particular class type. Device interfaces describe these mechanisms.
159
160When a device is added to a device class, the core attempts to add it
161to every interface that is registered with the device class.
162
diff --git a/Documentation/driver-model/device.txt b/Documentation/driver-model/device.txt
new file mode 100644
index 000000000000..58cc5dc8fd3e
--- /dev/null
+++ b/Documentation/driver-model/device.txt
@@ -0,0 +1,154 @@
1
2The Basic Device Structure
3~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5struct device {
6 struct list_head g_list;
7 struct list_head node;
8 struct list_head bus_list;
9 struct list_head driver_list;
10 struct list_head intf_list;
11 struct list_head children;
12 struct device * parent;
13
14 char name[DEVICE_NAME_SIZE];
15 char bus_id[BUS_ID_SIZE];
16
17 spinlock_t lock;
18 atomic_t refcount;
19
20 struct bus_type * bus;
21 struct driver_dir_entry dir;
22
23 u32 class_num;
24
25 struct device_driver *driver;
26 void *driver_data;
27 void *platform_data;
28
29 u32 current_state;
30 unsigned char *saved_state;
31
32 void (*release)(struct device * dev);
33};
34
35Fields
36~~~~~~
37g_list: Node in the global device list.
38
39node: Node in device's parent's children list.
40
41bus_list: Node in device's bus's devices list.
42
43driver_list: Node in device's driver's devices list.
44
45intf_list: List of intf_data. There is one structure allocated for
46 each interface that the device supports.
47
48children: List of child devices.
49
50parent: *** FIXME ***
51
52name: ASCII description of device.
53 Example: " 3Com Corporation 3c905 100BaseTX [Boomerang]"
54
55bus_id: ASCII representation of device's bus position. This
56 field should be a name unique across all devices on the
57 bus type the device belongs to.
58
59 Example: PCI bus_ids are in the form of
60 <bus number>:<slot number>.<function number>
61 This name is unique across all PCI devices in the system.
62
63lock: Spinlock for the device.
64
65refcount: Reference count on the device.
66
67bus: Pointer to struct bus_type that device belongs to.
68
69dir: Device's sysfs directory.
70
71class_num: Class-enumerated value of the device.
72
73driver: Pointer to struct device_driver that controls the device.
74
75driver_data: Driver-specific data.
76
77platform_data: Platform data specific to the device.
78
79current_state: Current power state of the device.
80
81saved_state: Pointer to saved state of the device. This is usable by
82 the device driver controlling the device.
83
84release: Callback to free the device after all references have
85 gone away. This should be set by the allocator of the
86 device (i.e. the bus driver that discovered the device).
87
88
89Programming Interface
90~~~~~~~~~~~~~~~~~~~~~
91The bus driver that discovers the device uses this to register the
92device with the core:
93
94int device_register(struct device * dev);
95
96The bus should initialize the following fields:
97
98 - parent
99 - name
100 - bus_id
101 - bus
102
103A device is removed from the core when its reference count goes to
1040. The reference count can be adjusted using:
105
106struct device * get_device(struct device * dev);
107void put_device(struct device * dev);
108
109get_device() will return a pointer to the struct device passed to it
110if the reference is not already 0 (if it's in the process of being
111removed already).
112
113A driver can access the lock in the device structure using:
114
115void lock_device(struct device * dev);
116void unlock_device(struct device * dev);
117
118
119Attributes
120~~~~~~~~~~
121struct device_attribute {
122 struct attribute attr;
123 ssize_t (*show)(struct device * dev, char * buf, size_t count, loff_t off);
124 ssize_t (*store)(struct device * dev, const char * buf, size_t count, loff_t off);
125};
126
127Attributes of devices can be exported via drivers using a simple
128procfs-like interface.
129
130Please see Documentation/filesystems/sysfs.txt for more information
131on how sysfs works.
132
133Attributes are declared using a macro called DEVICE_ATTR:
134
135#define DEVICE_ATTR(name,mode,show,store)
136
137Example:
138
139DEVICE_ATTR(power,0644,show_power,store_power);
140
141This declares a structure of type struct device_attribute named
142'dev_attr_power'. This can then be added and removed to the device's
143directory using:
144
145int device_create_file(struct device *device, struct device_attribute * entry);
146void device_remove_file(struct device * dev, struct device_attribute * attr);
147
148Example:
149
150device_create_file(dev,&dev_attr_power);
151device_remove_file(dev,&dev_attr_power);
152
153The file name will be 'power' with a mode of 0644 (-rw-r--r--).
154
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 *);
diff --git a/Documentation/driver-model/interface.txt b/Documentation/driver-model/interface.txt
new file mode 100644
index 000000000000..c66912bfe866
--- /dev/null
+++ b/Documentation/driver-model/interface.txt
@@ -0,0 +1,129 @@
1
2Device Interfaces
3
4Introduction
5~~~~~~~~~~~~
6
7Device interfaces are the logical interfaces of device classes that correlate
8directly to userspace interfaces, like device nodes.
9
10Each device class may have multiple interfaces through which you can
11access the same device. An input device may support the mouse interface,
12the 'evdev' interface, and the touchscreen interface. A SCSI disk would
13support the disk interface, the SCSI generic interface, and possibly a raw
14device interface.
15
16Device interfaces are registered with the class they belong to. As devices
17are added to the class, they are added to each interface registered with
18the class. The interface is responsible for determining whether the device
19supports the interface or not.
20
21
22Programming Interface
23~~~~~~~~~~~~~~~~~~~~~
24
25struct device_interface {
26 char * name;
27 rwlock_t lock;
28 u32 devnum;
29 struct device_class * devclass;
30
31 struct list_head node;
32 struct driver_dir_entry dir;
33
34 int (*add_device)(struct device *);
35 int (*add_device)(struct intf_data *);
36};
37
38int interface_register(struct device_interface *);
39void interface_unregister(struct device_interface *);
40
41
42An interface must specify the device class it belongs to. It is added
43to that class's list of interfaces on registration.
44
45
46Interfaces can be added to a device class at any time. Whenever it is
47added, each device in the class is passed to the interface's
48add_device callback. When an interface is removed, each device is
49removed from the interface.
50
51
52Devices
53~~~~~~~
54Once a device is added to a device class, it is added to each
55interface that is registered with the device class. The class
56is expected to place a class-specific data structure in
57struct device::class_data. The interface can use that (along with
58other fields of struct device) to determine whether or not the driver
59and/or device support that particular interface.
60
61
62Data
63~~~~
64
65struct intf_data {
66 struct list_head node;
67 struct device_interface * intf;
68 struct device * dev;
69 u32 intf_num;
70};
71
72int interface_add_data(struct interface_data *);
73
74The interface is responsible for allocating and initializing a struct
75intf_data and calling interface_add_data() to add it to the device's list
76of interfaces it belongs to. This list will be iterated over when the device
77is removed from the class (instead of all possible interfaces for a class).
78This structure should probably be embedded in whatever per-device data
79structure the interface is allocating anyway.
80
81Devices are enumerated within the interface. This happens in interface_add_data()
82and the enumerated value is stored in the struct intf_data for that device.
83
84sysfs
85~~~~~
86Each interface is given a directory in the directory of the device
87class it belongs to:
88
89Interfaces get a directory in the class's directory as well:
90
91 class/
92 `-- input
93 |-- devices
94 |-- drivers
95 |-- mouse
96 `-- evdev
97
98When a device is added to the interface, a symlink is created that points
99to the device's directory in the physical hierarchy:
100
101 class/
102 `-- input
103 |-- devices
104 | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
105 |-- drivers
106 | `-- usb:usb_mouse -> ../../../bus/drivers/usb_mouse/
107 |-- mouse
108 | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
109 `-- evdev
110 `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
111
112
113Future Plans
114~~~~~~~~~~~~
115A device interface is correlated directly with a userspace interface
116for a device, specifically a device node. For instance, a SCSI disk
117exposes at least two interfaces to userspace: the standard SCSI disk
118interface and the SCSI generic interface. It might also export a raw
119device interface.
120
121Many interfaces have a major number associated with them and each
122device gets a minor number. Or, multiple interfaces might share one
123major number, and each will receive a range of minor numbers (like in
124the case of input devices).
125
126These major and minor numbers could be stored in the interface
127structure. Major and minor allocations could happen when the interface
128is registered with the class, or via a helper function.
129
diff --git a/Documentation/driver-model/overview.txt b/Documentation/driver-model/overview.txt
new file mode 100644
index 000000000000..44662735cf81
--- /dev/null
+++ b/Documentation/driver-model/overview.txt
@@ -0,0 +1,114 @@
1The Linux Kernel Device Model
2
3Patrick Mochel <mochel@osdl.org>
4
526 August 2002
6
7
8Overview
9~~~~~~~~
10
11This driver model is a unification of all the current, disparate driver models
12that are currently in the kernel. It is intended to augment the
13bus-specific drivers for bridges and devices by consolidating a set of data
14and operations into globally accessible data structures.
15
16Current driver models implement some sort of tree-like structure (sometimes
17just a list) for the devices they control. But, there is no linkage between
18the different bus types.
19
20A common data structure can provide this linkage with little overhead: when a
21bus driver discovers a particular device, it can insert it into the global
22tree as well as its local tree. In fact, the local tree becomes just a subset
23of the global tree.
24
25Common data fields can also be moved out of the local bus models into the
26global model. Some of the manipulations of these fields can also be
27consolidated. Most likely, manipulation functions will become a set
28of helper functions, which the bus drivers wrap around to include any
29bus-specific items.
30
31The common device and bridge interface currently reflects the goals of the
32modern PC: namely the ability to do seamless Plug and Play, power management,
33and hot plug. (The model dictated by Intel and Microsoft (read: ACPI) ensures
34us that any device in the system may fit any of these criteria.)
35
36In reality, not every bus will be able to support such operations. But, most
37buses will support a majority of those operations, and all future buses will.
38In other words, a bus that doesn't support an operation is the exception,
39instead of the other way around.
40
41
42
43Downstream Access
44~~~~~~~~~~~~~~~~~
45
46Common data fields have been moved out of individual bus layers into a common
47data structure. But, these fields must still be accessed by the bus layers,
48and sometimes by the device-specific drivers.
49
50Other bus layers are encouraged to do what has been done for the PCI layer.
51struct pci_dev now looks like this:
52
53struct pci_dev {
54 ...
55
56 struct device device;
57};
58
59Note first that it is statically allocated. This means only one allocation on
60device discovery. Note also that it is at the _end_ of struct pci_dev. This is
61to make people think about what they're doing when switching between the bus
62driver and the global driver; and to prevent against mindless casts between
63the two.
64
65The PCI bus layer freely accesses the fields of struct device. It knows about
66the structure of struct pci_dev, and it should know the structure of struct
67device. PCI devices that have been converted generally do not touch the fields
68of struct device. More precisely, device-specific drivers should not touch
69fields of struct device unless there is a strong compelling reason to do so.
70
71This abstraction is prevention of unnecessary pain during transitional phases.
72If the name of the field changes or is removed, then every downstream driver
73will break. On the other hand, if only the bus layer (and not the device
74layer) accesses struct device, it is only that layer that needs to change.
75
76
77User Interface
78~~~~~~~~~~~~~~
79
80By virtue of having a complete hierarchical view of all the devices in the
81system, exporting a complete hierarchical view to userspace becomes relatively
82easy. This has been accomplished by implementing a special purpose virtual
83file system named sysfs. It is hence possible for the user to mount the
84whole sysfs filesystem anywhere in userspace.
85
86This 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
89none /sys sysfs defaults 0 0
90
91Or by hand on the command line:
92
93# mount -t sysfs sysfs /sys
94
95Whenever a device is inserted into the tree, a directory is created for it.
96This directory may be populated at each layer of discovery - the global layer,
97the bus layer, or the device layer.
98
99The global layer currently creates two files - 'name' and 'power'. The
100former only reports the name of the device. The latter reports the
101current power state of the device. It will also be used to set the current
102power state.
103
104The bus layer may also create files for the devices it finds while probing the
105bus. For example, the PCI layer currently creates 'irq' and 'resource' files
106for each PCI device.
107
108A device-specific driver may also export files in its directory to expose
109device-specific data or tunable interfaces.
110
111More information about the sysfs directory layout can be found in
112the other documents in this directory and in the file
113Documentation/filesystems/sysfs.txt.
114
diff --git a/Documentation/driver-model/platform.txt b/Documentation/driver-model/platform.txt
new file mode 100644
index 000000000000..5eee3e0bfc4c
--- /dev/null
+++ b/Documentation/driver-model/platform.txt
@@ -0,0 +1,99 @@
1Platform Devices and Drivers
2~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
4Platform devices
5~~~~~~~~~~~~~~~~
6Platform devices are devices that typically appear as autonomous
7entities in the system. This includes legacy port-based devices and
8host bridges to peripheral buses.
9
10
11Platform drivers
12~~~~~~~~~~~~~~~~
13Drivers for platform devices are typically very simple and
14unstructured. Either the device was present at a particular I/O port
15and the driver was loaded, or it was not. There was no possibility
16of hotplugging or alternative discovery besides probing at a specific
17I/O address and expecting a specific response.
18
19
20Other Architectures, Modern Firmware, and new Platforms
21~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22These devices are not always at the legacy I/O ports. This is true on
23other architectures and on some modern architectures. In most cases,
24the drivers are modified to discover the devices at other well-known
25ports for the given platform. However, the firmware in these systems
26does usually know where exactly these devices reside, and in some
27cases, it's the only way of discovering them.
28
29
30The Platform Bus
31~~~~~~~~~~~~~~~~
32A platform bus has been created to deal with these issues. First and
33foremost, it groups all the legacy devices under a common bus, and
34gives them a common parent if they don't already have one.
35
36But, besides the organizational benefits, the platform bus can also
37accommodate firmware-based enumeration.
38
39
40Device Discovery
41~~~~~~~~~~~~~~~~
42The platform bus has no concept of probing for devices. Devices
43discovery is left up to either the legacy drivers or the
44firmware. These entities are expected to notify the platform of
45devices that it discovers via the bus's add() callback:
46
47 platform_bus.add(parent,bus_id).
48
49
50Bus IDs
51~~~~~~~
52Bus IDs are the canonical names for the devices. There is no globally
53standard addressing mechanism for legacy devices. In the IA-32 world,
54we have Pnp IDs to use, as well as the legacy I/O ports. However,
55neither tell what the device really is or have any meaning on other
56platforms.
57
58Since both PnP IDs and the legacy I/O ports (and other standard I/O
59ports for specific devices) have a 1:1 mapping, we map the
60platform-specific name or identifier to a generic name (at least
61within the scope of the kernel).
62
63For example, a serial driver might find a device at I/O 0x3f8. The
64ACPI firmware might also discover a device with PnP ID (_HID)
65PNP0501. Both correspond to the same device and should be mapped to the
66canonical name 'serial'.
67
68The bus_id field should be a concatenation of the canonical name and
69the instance of that type of device. For example, the device at I/O
70port 0x3f8 should have a bus_id of "serial0". This places the
71responsibility of enumerating devices of a particular type up to the
72discovery mechanism. But, they are the entity that should know best
73(as opposed to the platform bus driver).
74
75
76Drivers
77~~~~~~~
78Drivers for platform devices should have a name that is the same as
79the canonical name of the devices they support. This allows the
80platform bus driver to do simple matching with the basic data
81structures to determine if a driver supports a certain device.
82
83For example, a legacy serial driver should have a name of 'serial' and
84register itself with the platform bus.
85
86
87Driver Binding
88~~~~~~~~~~~~~~
89Legacy drivers assume they are bound to the device once they start up
90and probe an I/O port. Divorcing them from this will be a difficult
91process. However, that shouldn't prevent us from implementing
92firmware-based enumeration.
93
94The firmware should notify the platform bus about devices before the
95legacy drivers have had a chance to load. Once the drivers are loaded,
96they driver model core will attempt to bind the driver to any
97previously-discovered devices. Once that has happened, it will be free
98to discover any other devices it pleases.
99
diff --git a/Documentation/driver-model/porting.txt b/Documentation/driver-model/porting.txt
new file mode 100644
index 000000000000..ff2fef2107f0
--- /dev/null
+++ b/Documentation/driver-model/porting.txt
@@ -0,0 +1,445 @@
1
2Porting Drivers to the New Driver Model
3
4Patrick Mochel
5
67 January 2003
7
8
9Overview
10
11Please refer to Documentation/driver-model/*.txt for definitions of
12various driver types and concepts.
13
14Most of the work of porting devices drivers to the new model happens
15at the bus driver layer. This was intentional, to minimize the
16negative effect on kernel drivers, and to allow a gradual transition
17of bus drivers.
18
19In a nutshell, the driver model consists of a set of objects that can
20be embedded in larger, bus-specific objects. Fields in these generic
21objects can replace fields in the bus-specific objects.
22
23The generic objects must be registered with the driver model core. By
24doing so, they will exported via the sysfs filesystem. sysfs can be
25mounted by doing
26
27 # mount -t sysfs sysfs /sys
28
29
30
31The Process
32
33Step 0: Read include/linux/device.h for object and function definitions.
34
35Step 1: Registering the bus driver.
36
37
38- Define a struct bus_type for the bus driver.
39
40struct bus_type pci_bus_type = {
41 .name = "pci",
42};
43
44
45- Register the bus type.
46 This should be done in the initialization function for the bus type,
47 which is usually the module_init(), or equivalent, function.
48
49static int __init pci_driver_init(void)
50{
51 return bus_register(&pci_bus_type);
52}
53
54subsys_initcall(pci_driver_init);
55
56
57 The bus type may be unregistered (if the bus driver may be compiled
58 as a module) by doing:
59
60 bus_unregister(&pci_bus_type);
61
62
63- Export the bus type for others to use.
64
65 Other code may wish to reference the bus type, so declare it in a
66 shared header file and export the symbol.
67
68From include/linux/pci.h:
69
70extern struct bus_type pci_bus_type;
71
72
73From file the above code appears in:
74
75EXPORT_SYMBOL(pci_bus_type);
76
77
78
79- This will cause the bus to show up in /sys/bus/pci/ with two
80 subdirectories: 'devices' and 'drivers'.
81
82# tree -d /sys/bus/pci/
83/sys/bus/pci/
84|-- devices
85`-- drivers
86
87
88
89Step 2: Registering Devices.
90
91struct device represents a single device. It mainly contains metadata
92describing the relationship the device has to other entities.
93
94
95- Embedd a struct device in the bus-specific device type.
96
97
98struct pci_dev {
99 ...
100 struct device dev; /* Generic device interface */
101 ...
102};
103
104 It is recommended that the generic device not be the first item in
105 the struct to discourage programmers from doing mindless casts
106 between the object types. Instead macros, or inline functions,
107 should be created to convert from the generic object type.
108
109
110#define to_pci_dev(n) container_of(n, struct pci_dev, dev)
111
112or
113
114static inline struct pci_dev * to_pci_dev(struct kobject * kobj)
115{
116 return container_of(n, struct pci_dev, dev);
117}
118
119 This allows the compiler to verify type-safety of the operations
120 that are performed (which is Good).
121
122
123- Initialize the device on registration.
124
125 When devices are discovered or registered with the bus type, the
126 bus driver should initialize the generic device. The most important
127 things to initialize are the bus_id, parent, and bus fields.
128
129 The bus_id is an ASCII string that contains the device's address on
130 the bus. The format of this string is bus-specific. This is
131 necessary for representing devices in sysfs.
132
133 parent is the physical parent of the device. It is important that
134 the bus driver sets this field correctly.
135
136 The driver model maintains an ordered list of devices that it uses
137 for power management. This list must be in order to guarantee that
138 devices are shutdown before their physical parents, and vice versa.
139 The order of this list is determined by the parent of registered
140 devices.
141
142 Also, the location of the device's sysfs directory depends on a
143 device's parent. sysfs exports a directory structure that mirrors
144 the device hierarchy. Accurately setting the parent guarantees that
145 sysfs will accurately represent the hierarchy.
146
147 The device's bus field is a pointer to the bus type the device
148 belongs to. This should be set to the bus_type that was declared
149 and initialized before.
150
151 Optionally, the bus driver may set the device's name and release
152 fields.
153
154 The name field is an ASCII string describing the device, like
155
156 "ATI Technologies Inc Radeon QD"
157
158 The release field is a callback that the driver model core calls
159 when the device has been removed, and all references to it have
160 been released. More on this in a moment.
161
162
163- Register the device.
164
165 Once the generic device has been initialized, it can be registered
166 with the driver model core by doing:
167
168 device_register(&dev->dev);
169
170 It can later be unregistered by doing:
171
172 device_unregister(&dev->dev);
173
174 This should happen on buses that support hotpluggable devices.
175 If a bus driver unregisters a device, it should not immediately free
176 it. It should instead wait for the driver model core to call the
177 device's release method, then free the bus-specific object.
178 (There may be other code that is currently referencing the device
179 structure, and it would be rude to free the device while that is
180 happening).
181
182
183 When the device is registered, a directory in sysfs is created.
184 The PCI tree in sysfs looks like:
185
186/sys/devices/pci0/
187|-- 00:00.0
188|-- 00:01.0
189| `-- 01:00.0
190|-- 00:02.0
191| `-- 02:1f.0
192| `-- 03:00.0
193|-- 00:1e.0
194| `-- 04:04.0
195|-- 00:1f.0
196|-- 00:1f.1
197| |-- ide0
198| | |-- 0.0
199| | `-- 0.1
200| `-- ide1
201| `-- 1.0
202|-- 00:1f.2
203|-- 00:1f.3
204`-- 00:1f.5
205
206 Also, symlinks are created in the bus's 'devices' directory
207 that point to the device's directory in the physical hierarchy.
208
209/sys/bus/pci/devices/
210|-- 00:00.0 -> ../../../devices/pci0/00:00.0
211|-- 00:01.0 -> ../../../devices/pci0/00:01.0
212|-- 00:02.0 -> ../../../devices/pci0/00:02.0
213|-- 00:1e.0 -> ../../../devices/pci0/00:1e.0
214|-- 00:1f.0 -> ../../../devices/pci0/00:1f.0
215|-- 00:1f.1 -> ../../../devices/pci0/00:1f.1
216|-- 00:1f.2 -> ../../../devices/pci0/00:1f.2
217|-- 00:1f.3 -> ../../../devices/pci0/00:1f.3
218|-- 00:1f.5 -> ../../../devices/pci0/00:1f.5
219|-- 01:00.0 -> ../../../devices/pci0/00:01.0/01:00.0
220|-- 02:1f.0 -> ../../../devices/pci0/00:02.0/02:1f.0
221|-- 03:00.0 -> ../../../devices/pci0/00:02.0/02:1f.0/03:00.0
222`-- 04:04.0 -> ../../../devices/pci0/00:1e.0/04:04.0
223
224
225
226Step 3: Registering Drivers.
227
228struct device_driver is a simple driver structure that contains a set
229of operations that the driver model core may call.
230
231
232- Embed a struct device_driver in the bus-specific driver.
233
234 Just like with devices, do something like:
235
236struct pci_driver {
237 ...
238 struct device_driver driver;
239};
240
241
242- Initialize the generic driver structure.
243
244 When the driver registers with the bus (e.g. doing pci_register_driver()),
245 initialize the necessary fields of the driver: the name and bus
246 fields.
247
248
249- Register the driver.
250
251 After the generic driver has been initialized, call
252
253 driver_register(&drv->driver);
254
255 to register the driver with the core.
256
257 When the driver is unregistered from the bus, unregister it from the
258 core by doing:
259
260 driver_unregister(&drv->driver);
261
262 Note that this will block until all references to the driver have
263 gone away. Normally, there will not be any.
264
265
266- Sysfs representation.
267
268 Drivers are exported via sysfs in their bus's 'driver's directory.
269 For example:
270
271/sys/bus/pci/drivers/
272|-- 3c59x
273|-- Ensoniq AudioPCI
274|-- agpgart-amdk7
275|-- e100
276`-- serial
277
278
279Step 4: Define Generic Methods for Drivers.
280
281struct device_driver defines a set of operations that the driver model
282core calls. Most of these operations are probably similar to
283operations the bus already defines for drivers, but taking different
284parameters.
285
286It would be difficult and tedious to force every driver on a bus to
287simultaneously convert their drivers to generic format. Instead, the
288bus driver should define single instances of the generic methods that
289forward call to the bus-specific drivers. For instance:
290
291
292static int pci_device_remove(struct device * dev)
293{
294 struct pci_dev * pci_dev = to_pci_dev(dev);
295 struct pci_driver * drv = pci_dev->driver;
296
297 if (drv) {
298 if (drv->remove)
299 drv->remove(pci_dev);
300 pci_dev->driver = NULL;
301 }
302 return 0;
303}
304
305
306The generic driver should be initialized with these methods before it
307is registered.
308
309 /* initialize common driver fields */
310 drv->driver.name = drv->name;
311 drv->driver.bus = &pci_bus_type;
312 drv->driver.probe = pci_device_probe;
313 drv->driver.resume = pci_device_resume;
314 drv->driver.suspend = pci_device_suspend;
315 drv->driver.remove = pci_device_remove;
316
317 /* register with core */
318 driver_register(&drv->driver);
319
320
321Ideally, the bus should only initialize the fields if they are not
322already set. This allows the drivers to implement their own generic
323methods.
324
325
326Step 5: Support generic driver binding.
327
328The model assumes that a device or driver can be dynamically
329registered with the bus at any time. When registration happens,
330devices must be bound to a driver, or drivers must be bound to all
331devices that it supports.
332
333A driver typically contains a list of device IDs that it supports. The
334bus driver compares these IDs to the IDs of devices registered with it.
335The format of the device IDs, and the semantics for comparing them are
336bus-specific, so the generic model does attempt to generalize them.
337
338Instead, a bus may supply a method in struct bus_type that does the
339comparison:
340
341 int (*match)(struct device * dev, struct device_driver * drv);
342
343match should return '1' if the driver supports the device, and '0'
344otherwise.
345
346When a device is registered, the bus's list of drivers is iterated
347over. bus->match() is called for each one until a match is found.
348
349When a driver is registered, the bus's list of devices is iterated
350over. bus->match() is called for each device that is not already
351claimed by a driver.
352
353When a device is successfully bound to a device, device->driver is
354set, the device is added to a per-driver list of devices, and a
355symlink is created in the driver's sysfs directory that points to the
356device's physical directory:
357
358/sys/bus/pci/drivers/
359|-- 3c59x
360| `-- 00:0b.0 -> ../../../../devices/pci0/00:0b.0
361|-- Ensoniq AudioPCI
362|-- agpgart-amdk7
363| `-- 00:00.0 -> ../../../../devices/pci0/00:00.0
364|-- e100
365| `-- 00:0c.0 -> ../../../../devices/pci0/00:0c.0
366`-- serial
367
368
369This driver binding should replace the existing driver binding
370mechanism the bus currently uses.
371
372
373Step 6: Supply a hotplug callback.
374
375Whenever a device is registered with the driver model core, the
376userspace program /sbin/hotplug is called to notify userspace.
377Users can define actions to perform when a device is inserted or
378removed.
379
380The driver model core passes several arguments to userspace via
381environment variables, including
382
383- ACTION: set to 'add' or 'remove'
384- DEVPATH: set to the device's physical path in sysfs.
385
386A bus driver may also supply additional parameters for userspace to
387consume. To do this, a bus must implement the 'hotplug' method in
388struct bus_type:
389
390 int (*hotplug) (struct device *dev, char **envp,
391 int num_envp, char *buffer, int buffer_size);
392
393This is called immediately before /sbin/hotplug is executed.
394
395
396Step 7: Cleaning up the bus driver.
397
398The generic bus, device, and driver structures provide several fields
399that can replace those defined privately to the bus driver.
400
401- Device list.
402
403struct bus_type contains a list of all devices registered with the bus
404type. This includes all devices on all instances of that bus type.
405An internal list that the bus uses may be removed, in favor of using
406this one.
407
408The core provides an iterator to access these devices.
409
410int bus_for_each_dev(struct bus_type * bus, struct device * start,
411 void * data, int (*fn)(struct device *, void *));
412
413
414- Driver list.
415
416struct bus_type also contains a list of all drivers registered with
417it. An internal list of drivers that the bus driver maintains may
418be removed in favor of using the generic one.
419
420The drivers may be iterated over, like devices:
421
422int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
423 void * data, int (*fn)(struct device_driver *, void *));
424
425
426Please see drivers/base/bus.c for more information.
427
428
429- rwsem
430
431struct bus_type contains an rwsem that protects all core accesses to
432the device and driver lists. This can be used by the bus driver
433internally, and should be used when accessing the device or driver
434lists the bus maintains.
435
436
437- Device and driver fields.
438
439Some of the fields in struct device and struct device_driver duplicate
440fields in the bus-specific representations of these objects. Feel free
441to remove the bus-specific ones and favor the generic ones. Note
442though, that this will likely mean fixing up all the drivers that
443reference the bus-specific fields (though those should all be 1-line
444changes).
445