diff options
Diffstat (limited to 'Documentation/driver-model')
-rw-r--r-- | Documentation/driver-model/binding.txt | 102 | ||||
-rw-r--r-- | Documentation/driver-model/bus.txt | 160 | ||||
-rw-r--r-- | Documentation/driver-model/class.txt | 162 | ||||
-rw-r--r-- | Documentation/driver-model/device.txt | 154 | ||||
-rw-r--r-- | Documentation/driver-model/driver.txt | 287 | ||||
-rw-r--r-- | Documentation/driver-model/interface.txt | 129 | ||||
-rw-r--r-- | Documentation/driver-model/overview.txt | 114 | ||||
-rw-r--r-- | Documentation/driver-model/platform.txt | 99 | ||||
-rw-r--r-- | Documentation/driver-model/porting.txt | 445 |
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 | |||
2 | Driver Binding | ||
3 | |||
4 | Driver binding is the process of associating a device with a device | ||
5 | driver that can control it. Bus drivers have typically handled this | ||
6 | because there have been bus-specific structures to represent the | ||
7 | devices and the drivers. With generic device and device driver | ||
8 | structures, most of the binding can take place using common code. | ||
9 | |||
10 | |||
11 | Bus | ||
12 | ~~~ | ||
13 | |||
14 | The bus type structure contains a list of all devices that are on that bus | ||
15 | type in the system. When device_register is called for a device, it is | ||
16 | inserted into the end of this list. The bus object also contains a | ||
17 | list of all drivers of that bus type. When driver_register is called | ||
18 | for a driver, it is inserted at the end of this list. These are the | ||
19 | two events which trigger driver binding. | ||
20 | |||
21 | |||
22 | device_register | ||
23 | ~~~~~~~~~~~~~~~ | ||
24 | |||
25 | When a new device is added, the bus's list of drivers is iterated over | ||
26 | to find one that supports it. In order to determine that, the device | ||
27 | ID of the device must match one of the device IDs that the driver | ||
28 | supports. The format and semantics for comparing IDs is bus-specific. | ||
29 | Instead of trying to derive a complex state machine and matching | ||
30 | algorithm, it is up to the bus driver to provide a callback to compare | ||
31 | a device against the IDs of a driver. The bus returns 1 if a match was | ||
32 | found; 0 otherwise. | ||
33 | |||
34 | int match(struct device * dev, struct device_driver * drv); | ||
35 | |||
36 | If a match is found, the device's driver field is set to the driver | ||
37 | and the driver's probe callback is called. This gives the driver a | ||
38 | chance to verify that it really does support the hardware, and that | ||
39 | it's in a working state. | ||
40 | |||
41 | Device Class | ||
42 | ~~~~~~~~~~~~ | ||
43 | |||
44 | Upon the successful completion of probe, the device is registered with | ||
45 | the class to which it belongs. Device drivers belong to one and only one | ||
46 | class, and that is set in the driver's devclass field. | ||
47 | devclass_add_device is called to enumerate the device within the class | ||
48 | and actually register it with the class, which happens with the | ||
49 | class's register_dev callback. | ||
50 | |||
51 | NOTE: The device class structures and core routines to manipulate them | ||
52 | are not in the mainline kernel, so the discussion is still a bit | ||
53 | speculative. | ||
54 | |||
55 | |||
56 | Driver | ||
57 | ~~~~~~ | ||
58 | |||
59 | When a driver is attached to a device, the device is inserted into the | ||
60 | driver's list of devices. | ||
61 | |||
62 | |||
63 | sysfs | ||
64 | ~~~~~ | ||
65 | |||
66 | A symlink is created in the bus's 'devices' directory that points to | ||
67 | the device's directory in the physical hierarchy. | ||
68 | |||
69 | A symlink is created in the driver's 'devices' directory that points | ||
70 | to the device's directory in the physical hierarchy. | ||
71 | |||
72 | A directory for the device is created in the class's directory. A | ||
73 | symlink is created in that directory that points to the device's | ||
74 | physical location in the sysfs tree. | ||
75 | |||
76 | A symlink can be created (though this isn't done yet) in the device's | ||
77 | physical directory to either its class directory, or the class's | ||
78 | top-level directory. One can also be created to point to its driver's | ||
79 | directory also. | ||
80 | |||
81 | |||
82 | driver_register | ||
83 | ~~~~~~~~~~~~~~~ | ||
84 | |||
85 | The process is almost identical for when a new driver is added. | ||
86 | The bus's list of devices is iterated over to find a match. Devices | ||
87 | that already have a driver are skipped. All the devices are iterated | ||
88 | over, to bind as many devices as possible to the driver. | ||
89 | |||
90 | |||
91 | Removal | ||
92 | ~~~~~~~ | ||
93 | |||
94 | When a device is removed, the reference count for it will eventually | ||
95 | go to 0. When it does, the remove callback of the driver is called. It | ||
96 | is removed from the driver's list of devices and the reference count | ||
97 | of the driver is decremented. All symlinks between the two are removed. | ||
98 | |||
99 | When a driver is removed, the list of devices that it supports is | ||
100 | iterated over, and the driver's remove callback is called for each | ||
101 | one. 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 | |||
2 | Bus Types | ||
3 | |||
4 | Definition | ||
5 | ~~~~~~~~~~ | ||
6 | |||
7 | struct 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 | |||
25 | int bus_register(struct bus_type * bus); | ||
26 | |||
27 | |||
28 | Declaration | ||
29 | ~~~~~~~~~~~ | ||
30 | |||
31 | Each bus type in the kernel (PCI, USB, etc) should declare one static | ||
32 | object of this type. They must initialize the name field, and may | ||
33 | optionally initialize the match callback. | ||
34 | |||
35 | struct bus_type pci_bus_type = { | ||
36 | .name = "pci", | ||
37 | .match = pci_bus_match, | ||
38 | }; | ||
39 | |||
40 | The structure should be exported to drivers in a header file: | ||
41 | |||
42 | extern struct bus_type pci_bus_type; | ||
43 | |||
44 | |||
45 | Registration | ||
46 | ~~~~~~~~~~~~ | ||
47 | |||
48 | When a bus driver is initialized, it calls bus_register. This | ||
49 | initializes the rest of the fields in the bus object and inserts it | ||
50 | into a global list of bus types. Once the bus object is registered, | ||
51 | the fields in it are usable by the bus driver. | ||
52 | |||
53 | |||
54 | Callbacks | ||
55 | ~~~~~~~~~ | ||
56 | |||
57 | match(): Attaching Drivers to Devices | ||
58 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
59 | |||
60 | The format of device ID structures and the semantics for comparing | ||
61 | them are inherently bus-specific. Drivers typically declare an array | ||
62 | of device IDs of devices they support that reside in a bus-specific | ||
63 | driver structure. | ||
64 | |||
65 | The purpose of the match callback is provide the bus an opportunity to | ||
66 | determine if a particular driver supports a particular device by | ||
67 | comparing the device IDs the driver supports with the device ID of a | ||
68 | particular device, without sacrificing bus-specific functionality or | ||
69 | type-safety. | ||
70 | |||
71 | When a driver is registered with the bus, the bus's list of devices is | ||
72 | iterated over, and the match callback is called for each device that | ||
73 | does not have a driver associated with it. | ||
74 | |||
75 | |||
76 | |||
77 | Device and Driver Lists | ||
78 | ~~~~~~~~~~~~~~~~~~~~~~~ | ||
79 | |||
80 | The lists of devices and drivers are intended to replace the local | ||
81 | lists that many buses keep. They are lists of struct devices and | ||
82 | struct device_drivers, respectively. Bus drivers are free to use the | ||
83 | lists as they please, but conversion to the bus-specific type may be | ||
84 | necessary. | ||
85 | |||
86 | The LDM core provides helper functions for iterating over each list. | ||
87 | |||
88 | int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data, | ||
89 | int (*fn)(struct device *, void *)); | ||
90 | |||
91 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | ||
92 | void * data, int (*fn)(struct device_driver *, void *)); | ||
93 | |||
94 | These helpers iterate over the respective list, and call the callback | ||
95 | for each device or driver in the list. All list accesses are | ||
96 | synchronized by taking the bus's lock (read currently). The reference | ||
97 | count on each object in the list is incremented before the callback is | ||
98 | called; it is decremented after the next object has been obtained. The | ||
99 | lock is not held when calling the callback. | ||
100 | |||
101 | |||
102 | sysfs | ||
103 | ~~~~~~~~ | ||
104 | There is a top-level directory named 'bus'. | ||
105 | |||
106 | Each bus gets a directory in the bus directory, along with two default | ||
107 | directories: | ||
108 | |||
109 | /sys/bus/pci/ | ||
110 | |-- devices | ||
111 | `-- drivers | ||
112 | |||
113 | Drivers registered with the bus get a directory in the bus's drivers | ||
114 | directory: | ||
115 | |||
116 | /sys/bus/pci/ | ||
117 | |-- devices | ||
118 | `-- drivers | ||
119 | |-- Intel ICH | ||
120 | |-- Intel ICH Joystick | ||
121 | |-- agpgart | ||
122 | `-- e100 | ||
123 | |||
124 | Each device that is discovered on a bus of that type gets a symlink in | ||
125 | the bus's devices directory to the device's directory in the physical | ||
126 | hierarchy: | ||
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 | |||
136 | Exporting Attributes | ||
137 | ~~~~~~~~~~~~~~~~~~~~ | ||
138 | struct 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 | |||
144 | Bus drivers can export attributes using the BUS_ATTR macro that works | ||
145 | similarly to the DEVICE_ATTR macro for devices. For example, a definition | ||
146 | like this: | ||
147 | |||
148 | static BUS_ATTR(debug,0644,show_debug,store_debug); | ||
149 | |||
150 | is equivalent to declaring: | ||
151 | |||
152 | static bus_attribute bus_attr_debug; | ||
153 | |||
154 | This can then be used to add and remove the attribute from the bus's | ||
155 | sysfs directory using: | ||
156 | |||
157 | int bus_create_file(struct bus_type *, struct bus_attribute *); | ||
158 | void 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 | |||
2 | Device Classes | ||
3 | |||
4 | |||
5 | Introduction | ||
6 | ~~~~~~~~~~~~ | ||
7 | A device class describes a type of device, like an audio or network | ||
8 | device. The following device classes have been identified: | ||
9 | |||
10 | <Insert List of Device Classes Here> | ||
11 | |||
12 | |||
13 | Each device class defines a set of semantics and a programming interface | ||
14 | that devices of that class adhere to. Device drivers are the | ||
15 | implemention of that programming interface for a particular device on | ||
16 | a particular bus. | ||
17 | |||
18 | Device classes are agnostic with respect to what bus a device resides | ||
19 | on. | ||
20 | |||
21 | |||
22 | Programming Interface | ||
23 | ~~~~~~~~~~~~~~~~~~~~~ | ||
24 | The device class structure looks like: | ||
25 | |||
26 | |||
27 | typedef int (*devclass_add)(struct device *); | ||
28 | typedef void (*devclass_remove)(struct device *); | ||
29 | |||
30 | struct 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 | |||
47 | A typical device class definition would look like: | ||
48 | |||
49 | struct device_class input_devclass = { | ||
50 | .name = "input", | ||
51 | .add_device = input_add_device, | ||
52 | .remove_device = input_remove_device, | ||
53 | }; | ||
54 | |||
55 | Each device class structure should be exported in a header file so it | ||
56 | can be used by drivers, extensions and interfaces. | ||
57 | |||
58 | Device classes are registered and unregistered with the core using: | ||
59 | |||
60 | int devclass_register(struct device_class * cls); | ||
61 | void devclass_unregister(struct device_class * cls); | ||
62 | |||
63 | |||
64 | Devices | ||
65 | ~~~~~~~ | ||
66 | As devices are bound to drivers, they are added to the device class | ||
67 | that the driver belongs to. Before the driver model core, this would | ||
68 | typically happen during the driver's probe() callback, once the device | ||
69 | has been initialized. It now happens after the probe() callback | ||
70 | finishes from the core. | ||
71 | |||
72 | The device is enumerated in the class. Each time a device is added to | ||
73 | the class, the class's devnum field is incremented and assigned to the | ||
74 | device. The field is never decremented, so if the device is removed | ||
75 | from the class and re-added, it will receive a different enumerated | ||
76 | value. | ||
77 | |||
78 | The class is allowed to create a class-specific structure for the | ||
79 | device and store it in the device's class_data pointer. | ||
80 | |||
81 | There is no list of devices in the device class. Each driver has a | ||
82 | list of devices that it supports. The device class has a list of | ||
83 | drivers of that particular class. To access all of the devices in the | ||
84 | class, iterate over the device lists of each driver in the class. | ||
85 | |||
86 | |||
87 | Device Drivers | ||
88 | ~~~~~~~~~~~~~~ | ||
89 | Device drivers are added to device classes when they are registered | ||
90 | with the core. A driver specifies the class it belongs to by setting | ||
91 | the struct device_driver::devclass field. | ||
92 | |||
93 | |||
94 | sysfs directory structure | ||
95 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
96 | There is a top-level sysfs directory named 'class'. | ||
97 | |||
98 | Each class gets a directory in the class directory, along with two | ||
99 | default subdirectories: | ||
100 | |||
101 | class/ | ||
102 | `-- input | ||
103 | |-- devices | ||
104 | `-- drivers | ||
105 | |||
106 | |||
107 | Drivers registered with the class get a symlink in the drivers/ directory | ||
108 | that 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 | |||
117 | Each device gets a symlink in the devices/ directory that points to the | ||
118 | device'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 | |||
127 | Exporting Attributes | ||
128 | ~~~~~~~~~~~~~~~~~~~~ | ||
129 | struct 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 | |||
135 | Class drivers can export attributes using the DEVCLASS_ATTR macro that works | ||
136 | similarly to the DEVICE_ATTR macro for devices. For example, a definition | ||
137 | like this: | ||
138 | |||
139 | static DEVCLASS_ATTR(debug,0644,show_debug,store_debug); | ||
140 | |||
141 | is equivalent to declaring: | ||
142 | |||
143 | static devclass_attribute devclass_attr_debug; | ||
144 | |||
145 | The bus driver can add and remove the attribute from the class's | ||
146 | sysfs directory using: | ||
147 | |||
148 | int devclass_create_file(struct device_class *, struct devclass_attribute *); | ||
149 | void devclass_remove_file(struct device_class *, struct devclass_attribute *); | ||
150 | |||
151 | In the example above, the file will be named 'debug' in placed in the | ||
152 | class's directory in sysfs. | ||
153 | |||
154 | |||
155 | Interfaces | ||
156 | ~~~~~~~~~~ | ||
157 | There may exist multiple mechanisms for accessing the same device of a | ||
158 | particular class type. Device interfaces describe these mechanisms. | ||
159 | |||
160 | When a device is added to a device class, the core attempts to add it | ||
161 | to 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 | |||
2 | The Basic Device Structure | ||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
4 | |||
5 | struct 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 | |||
35 | Fields | ||
36 | ~~~~~~ | ||
37 | g_list: Node in the global device list. | ||
38 | |||
39 | node: Node in device's parent's children list. | ||
40 | |||
41 | bus_list: Node in device's bus's devices list. | ||
42 | |||
43 | driver_list: Node in device's driver's devices list. | ||
44 | |||
45 | intf_list: List of intf_data. There is one structure allocated for | ||
46 | each interface that the device supports. | ||
47 | |||
48 | children: List of child devices. | ||
49 | |||
50 | parent: *** FIXME *** | ||
51 | |||
52 | name: ASCII description of device. | ||
53 | Example: " 3Com Corporation 3c905 100BaseTX [Boomerang]" | ||
54 | |||
55 | bus_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 | |||
63 | lock: Spinlock for the device. | ||
64 | |||
65 | refcount: Reference count on the device. | ||
66 | |||
67 | bus: Pointer to struct bus_type that device belongs to. | ||
68 | |||
69 | dir: Device's sysfs directory. | ||
70 | |||
71 | class_num: Class-enumerated value of the device. | ||
72 | |||
73 | driver: Pointer to struct device_driver that controls the device. | ||
74 | |||
75 | driver_data: Driver-specific data. | ||
76 | |||
77 | platform_data: Platform data specific to the device. | ||
78 | |||
79 | current_state: Current power state of the device. | ||
80 | |||
81 | saved_state: Pointer to saved state of the device. This is usable by | ||
82 | the device driver controlling the device. | ||
83 | |||
84 | release: 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 | |||
89 | Programming Interface | ||
90 | ~~~~~~~~~~~~~~~~~~~~~ | ||
91 | The bus driver that discovers the device uses this to register the | ||
92 | device with the core: | ||
93 | |||
94 | int device_register(struct device * dev); | ||
95 | |||
96 | The bus should initialize the following fields: | ||
97 | |||
98 | - parent | ||
99 | - name | ||
100 | - bus_id | ||
101 | - bus | ||
102 | |||
103 | A device is removed from the core when its reference count goes to | ||
104 | 0. The reference count can be adjusted using: | ||
105 | |||
106 | struct device * get_device(struct device * dev); | ||
107 | void put_device(struct device * dev); | ||
108 | |||
109 | get_device() will return a pointer to the struct device passed to it | ||
110 | if the reference is not already 0 (if it's in the process of being | ||
111 | removed already). | ||
112 | |||
113 | A driver can access the lock in the device structure using: | ||
114 | |||
115 | void lock_device(struct device * dev); | ||
116 | void unlock_device(struct device * dev); | ||
117 | |||
118 | |||
119 | Attributes | ||
120 | ~~~~~~~~~~ | ||
121 | struct 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 | |||
127 | Attributes of devices can be exported via drivers using a simple | ||
128 | procfs-like interface. | ||
129 | |||
130 | Please see Documentation/filesystems/sysfs.txt for more information | ||
131 | on how sysfs works. | ||
132 | |||
133 | Attributes are declared using a macro called DEVICE_ATTR: | ||
134 | |||
135 | #define DEVICE_ATTR(name,mode,show,store) | ||
136 | |||
137 | Example: | ||
138 | |||
139 | DEVICE_ATTR(power,0644,show_power,store_power); | ||
140 | |||
141 | This declares a structure of type struct device_attribute named | ||
142 | 'dev_attr_power'. This can then be added and removed to the device's | ||
143 | directory using: | ||
144 | |||
145 | int device_create_file(struct device *device, struct device_attribute * entry); | ||
146 | void device_remove_file(struct device * dev, struct device_attribute * attr); | ||
147 | |||
148 | Example: | ||
149 | |||
150 | device_create_file(dev,&dev_attr_power); | ||
151 | device_remove_file(dev,&dev_attr_power); | ||
152 | |||
153 | The 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 | |||
2 | Device Drivers | ||
3 | |||
4 | struct 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 | |||
27 | Allocation | ||
28 | ~~~~~~~~~~ | ||
29 | |||
30 | Device drivers are statically allocated structures. Though there may | ||
31 | be multiple devices in a system that a driver supports, struct | ||
32 | device_driver represents the driver as a whole (not a particular | ||
33 | device instance). | ||
34 | |||
35 | Initialization | ||
36 | ~~~~~~~~~~~~~~ | ||
37 | |||
38 | The driver must initialize at least the name and bus fields. It should | ||
39 | also initialize the devclass field (when it arrives), so it may obtain | ||
40 | the proper linkage internally. It should also initialize as many of | ||
41 | the callbacks as possible, though each is optional. | ||
42 | |||
43 | Declaration | ||
44 | ~~~~~~~~~~~ | ||
45 | |||
46 | As stated above, struct device_driver objects are statically | ||
47 | allocated. Below is an example declaration of the eepro100 | ||
48 | driver. This declaration is hypothetical only; it relies on the driver | ||
49 | being converted completely to the new model. | ||
50 | |||
51 | static struct device_driver eepro100_driver = { | ||
52 | .name = "eepro100", | ||
53 | .bus = &pci_bus_type, | ||
54 | .devclass = ðernet_devclass, /* when it's implemented */ | ||
55 | |||
56 | .probe = eepro100_probe, | ||
57 | .remove = eepro100_remove, | ||
58 | .suspend = eepro100_suspend, | ||
59 | .resume = eepro100_resume, | ||
60 | }; | ||
61 | |||
62 | Most drivers will not be able to be converted completely to the new | ||
63 | model because the bus they belong to has a bus-specific structure with | ||
64 | bus-specific fields that cannot be generalized. | ||
65 | |||
66 | The most common example of this are device ID structures. A driver | ||
67 | typically defines an array of device IDs that it supports. The format | ||
68 | of these structures and the semantics for comparing device IDs are | ||
69 | completely bus-specific. Defining them as bus-specific entities would | ||
70 | sacrifice type-safety, so we keep bus-specific structures around. | ||
71 | |||
72 | Bus-specific drivers should include a generic struct device_driver in | ||
73 | the definition of the bus-specific driver. Like this: | ||
74 | |||
75 | struct pci_driver { | ||
76 | const struct pci_device_id *id_table; | ||
77 | struct device_driver driver; | ||
78 | }; | ||
79 | |||
80 | A definition that included bus-specific fields would look like | ||
81 | (using the eepro100 driver again): | ||
82 | |||
83 | static struct pci_driver eepro100_driver = { | ||
84 | .id_table = eepro100_pci_tbl, | ||
85 | .driver = { | ||
86 | .name = "eepro100", | ||
87 | .bus = &pci_bus_type, | ||
88 | .devclass = ðernet_devclass, /* when it's implemented */ | ||
89 | .probe = eepro100_probe, | ||
90 | .remove = eepro100_remove, | ||
91 | .suspend = eepro100_suspend, | ||
92 | .resume = eepro100_resume, | ||
93 | }, | ||
94 | }; | ||
95 | |||
96 | Some may find the syntax of embedded struct initialization awkward or | ||
97 | even a bit ugly. So far, it's the best way we've found to do what we want... | ||
98 | |||
99 | Registration | ||
100 | ~~~~~~~~~~~~ | ||
101 | |||
102 | int driver_register(struct device_driver * drv); | ||
103 | |||
104 | The driver registers the structure on startup. For drivers that have | ||
105 | no bus-specific fields (i.e. don't have a bus-specific driver | ||
106 | structure), they would use driver_register and pass a pointer to their | ||
107 | struct device_driver object. | ||
108 | |||
109 | Most drivers, however, will have a bus-specific structure and will | ||
110 | need to register with the bus using something like pci_driver_register. | ||
111 | |||
112 | It is important that drivers register their driver structure as early as | ||
113 | possible. Registration with the core initializes several fields in the | ||
114 | struct device_driver object, including the reference count and the | ||
115 | lock. These fields are assumed to be valid at all times and may be | ||
116 | used by the device model core or the bus driver. | ||
117 | |||
118 | |||
119 | Transition Bus Drivers | ||
120 | ~~~~~~~~~~~~~~~~~~~~~~ | ||
121 | |||
122 | By defining wrapper functions, the transition to the new model can be | ||
123 | made easier. Drivers can ignore the generic structure altogether and | ||
124 | let the bus wrapper fill in the fields. For the callbacks, the bus can | ||
125 | define generic callbacks that forward the call to the bus-specific | ||
126 | callbacks of the drivers. | ||
127 | |||
128 | This solution is intended to be only temporary. In order to get class | ||
129 | information in the driver, the drivers must be modified anyway. Since | ||
130 | converting drivers to the new model should reduce some infrastructural | ||
131 | complexity and code size, it is recommended that they are converted as | ||
132 | class information is added. | ||
133 | |||
134 | Access | ||
135 | ~~~~~~ | ||
136 | |||
137 | Once the object has been registered, it may access the common fields of | ||
138 | the object, like the lock and the list of devices. | ||
139 | |||
140 | int driver_for_each_dev(struct device_driver * drv, void * data, | ||
141 | int (*callback)(struct device * dev, void * data)); | ||
142 | |||
143 | The devices field is a list of all the devices that have been bound to | ||
144 | the driver. The LDM core provides a helper function to operate on all | ||
145 | the devices a driver controls. This helper locks the driver on each | ||
146 | node access, and does proper reference counting on each device as it | ||
147 | accesses it. | ||
148 | |||
149 | |||
150 | sysfs | ||
151 | ~~~~~ | ||
152 | |||
153 | When a driver is registered, a sysfs directory is created in its | ||
154 | bus's directory. In this directory, the driver can export an interface | ||
155 | to userspace to control operation of the driver on a global basis; | ||
156 | e.g. toggling debugging output in the driver. | ||
157 | |||
158 | A future feature of this directory will be a 'devices' directory. This | ||
159 | directory will contain symlinks to the directories of devices it | ||
160 | supports. | ||
161 | |||
162 | |||
163 | |||
164 | Callbacks | ||
165 | ~~~~~~~~~ | ||
166 | |||
167 | int (*probe) (struct device * dev); | ||
168 | |||
169 | probe is called to verify the existence of a certain type of | ||
170 | hardware. This is called during the driver binding process, after the | ||
171 | bus has verified that the device ID of a device matches one of the | ||
172 | device IDs supported by the driver. | ||
173 | |||
174 | This callback only verifies that there actually is supported hardware | ||
175 | present. It may allocate a driver-specific structure, but it should | ||
176 | not do any initialization of the hardware itself. The device-specific | ||
177 | structure may be stored in the device's driver_data field. | ||
178 | |||
179 | int (*init) (struct device * dev); | ||
180 | |||
181 | init is called during the binding stage. It is called after probe has | ||
182 | successfully returned and the device has been registered with its | ||
183 | class. It is responsible for initializing the hardware. | ||
184 | |||
185 | int (*remove) (struct device * dev); | ||
186 | |||
187 | remove is called to dissociate a driver with a device. This may be | ||
188 | called if a device is physically removed from the system, if the | ||
189 | driver module is being unloaded, or during a reboot sequence. | ||
190 | |||
191 | It is up to the driver to determine if the device is present or | ||
192 | not. It should free any resources allocated specifically for the | ||
193 | device; i.e. anything in the device's driver_data field. | ||
194 | |||
195 | If the device is still present, it should quiesce the device and place | ||
196 | it into a supported low-power state. | ||
197 | |||
198 | int (*suspend) (struct device * dev, u32 state, u32 level); | ||
199 | |||
200 | suspend is called to put the device in a low power state. There are | ||
201 | several stages to successfully suspending a device, which is denoted in | ||
202 | the @level parameter. Breaking the suspend transition into several | ||
203 | stages affords the platform flexibility in performing device power | ||
204 | management based on the requirements of the system and the | ||
205 | user-defined policy. | ||
206 | |||
207 | SUSPEND_NOTIFY notifies the device that a suspend transition is about | ||
208 | to happen. This happens on system power state transitions to verify | ||
209 | that all devices can successfully suspend. | ||
210 | |||
211 | A driver may choose to fail on this call, which should cause the | ||
212 | entire suspend transition to fail. A driver should fail only if it | ||
213 | knows that the device will not be able to be resumed properly when the | ||
214 | system wakes up again. It could also fail if it somehow determines it | ||
215 | is in the middle of an operation too important to stop. | ||
216 | |||
217 | SUSPEND_DISABLE tells the device to stop I/O transactions. When it | ||
218 | stops transactions, or what it should do with unfinished transactions | ||
219 | is a policy of the driver. After this call, the driver should not | ||
220 | accept any other I/O requests. | ||
221 | |||
222 | SUSPEND_SAVE_STATE tells the device to save the context of the | ||
223 | hardware. This includes any bus-specific hardware state and | ||
224 | device-specific hardware state. A pointer to this saved state can be | ||
225 | stored in the device's saved_state field. | ||
226 | |||
227 | SUSPEND_POWER_DOWN tells the driver to place the device in the low | ||
228 | power state requested. | ||
229 | |||
230 | Whether suspend is called with a given level is a policy of the | ||
231 | platform. Some levels may be omitted; drivers must not assume the | ||
232 | reception of any level. However, all levels must be called in the | ||
233 | order above; i.e. notification will always come before disabling; | ||
234 | disabling the device will come before suspending the device. | ||
235 | |||
236 | All calls are made with interrupts enabled, except for the | ||
237 | SUSPEND_POWER_DOWN level. | ||
238 | |||
239 | int (*resume) (struct device * dev, u32 level); | ||
240 | |||
241 | Resume is used to bring a device back from a low power state. Like the | ||
242 | suspend transition, it happens in several stages. | ||
243 | |||
244 | RESUME_POWER_ON tells the driver to set the power state to the state | ||
245 | before the suspend call (The device could have already been in a low | ||
246 | power state before the suspend call to put in a lower power state). | ||
247 | |||
248 | RESUME_RESTORE_STATE tells the driver to restore the state saved by | ||
249 | the SUSPEND_SAVE_STATE suspend call. | ||
250 | |||
251 | RESUME_ENABLE tells the driver to start accepting I/O transactions | ||
252 | again. Depending on driver policy, the device may already have pending | ||
253 | I/O requests. | ||
254 | |||
255 | RESUME_POWER_ON is called with interrupts disabled. The other resume | ||
256 | levels are called with interrupts enabled. | ||
257 | |||
258 | As with the various suspend stages, the driver must not assume that | ||
259 | any other resume calls have been or will be made. Each call should be | ||
260 | self-contained and not dependent on any external state. | ||
261 | |||
262 | |||
263 | Attributes | ||
264 | ~~~~~~~~~~ | ||
265 | struct 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 | |||
271 | Device drivers can export attributes via their sysfs directories. | ||
272 | Drivers can declare attributes using a DRIVER_ATTR macro that works | ||
273 | identically to the DEVICE_ATTR macro. | ||
274 | |||
275 | Example: | ||
276 | |||
277 | DRIVER_ATTR(debug,0644,show_debug,store_debug); | ||
278 | |||
279 | This is equivalent to declaring: | ||
280 | |||
281 | struct driver_attribute driver_attr_debug; | ||
282 | |||
283 | This can then be used to add and remove the attribute from the | ||
284 | driver's directory using: | ||
285 | |||
286 | int driver_create_file(struct device_driver *, struct driver_attribute *); | ||
287 | void 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 | |||
2 | Device Interfaces | ||
3 | |||
4 | Introduction | ||
5 | ~~~~~~~~~~~~ | ||
6 | |||
7 | Device interfaces are the logical interfaces of device classes that correlate | ||
8 | directly to userspace interfaces, like device nodes. | ||
9 | |||
10 | Each device class may have multiple interfaces through which you can | ||
11 | access the same device. An input device may support the mouse interface, | ||
12 | the 'evdev' interface, and the touchscreen interface. A SCSI disk would | ||
13 | support the disk interface, the SCSI generic interface, and possibly a raw | ||
14 | device interface. | ||
15 | |||
16 | Device interfaces are registered with the class they belong to. As devices | ||
17 | are added to the class, they are added to each interface registered with | ||
18 | the class. The interface is responsible for determining whether the device | ||
19 | supports the interface or not. | ||
20 | |||
21 | |||
22 | Programming Interface | ||
23 | ~~~~~~~~~~~~~~~~~~~~~ | ||
24 | |||
25 | struct 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 | |||
38 | int interface_register(struct device_interface *); | ||
39 | void interface_unregister(struct device_interface *); | ||
40 | |||
41 | |||
42 | An interface must specify the device class it belongs to. It is added | ||
43 | to that class's list of interfaces on registration. | ||
44 | |||
45 | |||
46 | Interfaces can be added to a device class at any time. Whenever it is | ||
47 | added, each device in the class is passed to the interface's | ||
48 | add_device callback. When an interface is removed, each device is | ||
49 | removed from the interface. | ||
50 | |||
51 | |||
52 | Devices | ||
53 | ~~~~~~~ | ||
54 | Once a device is added to a device class, it is added to each | ||
55 | interface that is registered with the device class. The class | ||
56 | is expected to place a class-specific data structure in | ||
57 | struct device::class_data. The interface can use that (along with | ||
58 | other fields of struct device) to determine whether or not the driver | ||
59 | and/or device support that particular interface. | ||
60 | |||
61 | |||
62 | Data | ||
63 | ~~~~ | ||
64 | |||
65 | struct intf_data { | ||
66 | struct list_head node; | ||
67 | struct device_interface * intf; | ||
68 | struct device * dev; | ||
69 | u32 intf_num; | ||
70 | }; | ||
71 | |||
72 | int interface_add_data(struct interface_data *); | ||
73 | |||
74 | The interface is responsible for allocating and initializing a struct | ||
75 | intf_data and calling interface_add_data() to add it to the device's list | ||
76 | of interfaces it belongs to. This list will be iterated over when the device | ||
77 | is removed from the class (instead of all possible interfaces for a class). | ||
78 | This structure should probably be embedded in whatever per-device data | ||
79 | structure the interface is allocating anyway. | ||
80 | |||
81 | Devices are enumerated within the interface. This happens in interface_add_data() | ||
82 | and the enumerated value is stored in the struct intf_data for that device. | ||
83 | |||
84 | sysfs | ||
85 | ~~~~~ | ||
86 | Each interface is given a directory in the directory of the device | ||
87 | class it belongs to: | ||
88 | |||
89 | Interfaces get a directory in the class's directory as well: | ||
90 | |||
91 | class/ | ||
92 | `-- input | ||
93 | |-- devices | ||
94 | |-- drivers | ||
95 | |-- mouse | ||
96 | `-- evdev | ||
97 | |||
98 | When a device is added to the interface, a symlink is created that points | ||
99 | to 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 | |||
113 | Future Plans | ||
114 | ~~~~~~~~~~~~ | ||
115 | A device interface is correlated directly with a userspace interface | ||
116 | for a device, specifically a device node. For instance, a SCSI disk | ||
117 | exposes at least two interfaces to userspace: the standard SCSI disk | ||
118 | interface and the SCSI generic interface. It might also export a raw | ||
119 | device interface. | ||
120 | |||
121 | Many interfaces have a major number associated with them and each | ||
122 | device gets a minor number. Or, multiple interfaces might share one | ||
123 | major number, and each will receive a range of minor numbers (like in | ||
124 | the case of input devices). | ||
125 | |||
126 | These major and minor numbers could be stored in the interface | ||
127 | structure. Major and minor allocations could happen when the interface | ||
128 | is 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 @@ | |||
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 | |||
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 @@ | |||
1 | Platform Devices and Drivers | ||
2 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
3 | |||
4 | Platform devices | ||
5 | ~~~~~~~~~~~~~~~~ | ||
6 | Platform devices are devices that typically appear as autonomous | ||
7 | entities in the system. This includes legacy port-based devices and | ||
8 | host bridges to peripheral buses. | ||
9 | |||
10 | |||
11 | Platform drivers | ||
12 | ~~~~~~~~~~~~~~~~ | ||
13 | Drivers for platform devices are typically very simple and | ||
14 | unstructured. Either the device was present at a particular I/O port | ||
15 | and the driver was loaded, or it was not. There was no possibility | ||
16 | of hotplugging or alternative discovery besides probing at a specific | ||
17 | I/O address and expecting a specific response. | ||
18 | |||
19 | |||
20 | Other Architectures, Modern Firmware, and new Platforms | ||
21 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
22 | These devices are not always at the legacy I/O ports. This is true on | ||
23 | other architectures and on some modern architectures. In most cases, | ||
24 | the drivers are modified to discover the devices at other well-known | ||
25 | ports for the given platform. However, the firmware in these systems | ||
26 | does usually know where exactly these devices reside, and in some | ||
27 | cases, it's the only way of discovering them. | ||
28 | |||
29 | |||
30 | The Platform Bus | ||
31 | ~~~~~~~~~~~~~~~~ | ||
32 | A platform bus has been created to deal with these issues. First and | ||
33 | foremost, it groups all the legacy devices under a common bus, and | ||
34 | gives them a common parent if they don't already have one. | ||
35 | |||
36 | But, besides the organizational benefits, the platform bus can also | ||
37 | accommodate firmware-based enumeration. | ||
38 | |||
39 | |||
40 | Device Discovery | ||
41 | ~~~~~~~~~~~~~~~~ | ||
42 | The platform bus has no concept of probing for devices. Devices | ||
43 | discovery is left up to either the legacy drivers or the | ||
44 | firmware. These entities are expected to notify the platform of | ||
45 | devices that it discovers via the bus's add() callback: | ||
46 | |||
47 | platform_bus.add(parent,bus_id). | ||
48 | |||
49 | |||
50 | Bus IDs | ||
51 | ~~~~~~~ | ||
52 | Bus IDs are the canonical names for the devices. There is no globally | ||
53 | standard addressing mechanism for legacy devices. In the IA-32 world, | ||
54 | we have Pnp IDs to use, as well as the legacy I/O ports. However, | ||
55 | neither tell what the device really is or have any meaning on other | ||
56 | platforms. | ||
57 | |||
58 | Since both PnP IDs and the legacy I/O ports (and other standard I/O | ||
59 | ports for specific devices) have a 1:1 mapping, we map the | ||
60 | platform-specific name or identifier to a generic name (at least | ||
61 | within the scope of the kernel). | ||
62 | |||
63 | For example, a serial driver might find a device at I/O 0x3f8. The | ||
64 | ACPI firmware might also discover a device with PnP ID (_HID) | ||
65 | PNP0501. Both correspond to the same device and should be mapped to the | ||
66 | canonical name 'serial'. | ||
67 | |||
68 | The bus_id field should be a concatenation of the canonical name and | ||
69 | the instance of that type of device. For example, the device at I/O | ||
70 | port 0x3f8 should have a bus_id of "serial0". This places the | ||
71 | responsibility of enumerating devices of a particular type up to the | ||
72 | discovery mechanism. But, they are the entity that should know best | ||
73 | (as opposed to the platform bus driver). | ||
74 | |||
75 | |||
76 | Drivers | ||
77 | ~~~~~~~ | ||
78 | Drivers for platform devices should have a name that is the same as | ||
79 | the canonical name of the devices they support. This allows the | ||
80 | platform bus driver to do simple matching with the basic data | ||
81 | structures to determine if a driver supports a certain device. | ||
82 | |||
83 | For example, a legacy serial driver should have a name of 'serial' and | ||
84 | register itself with the platform bus. | ||
85 | |||
86 | |||
87 | Driver Binding | ||
88 | ~~~~~~~~~~~~~~ | ||
89 | Legacy drivers assume they are bound to the device once they start up | ||
90 | and probe an I/O port. Divorcing them from this will be a difficult | ||
91 | process. However, that shouldn't prevent us from implementing | ||
92 | firmware-based enumeration. | ||
93 | |||
94 | The firmware should notify the platform bus about devices before the | ||
95 | legacy drivers have had a chance to load. Once the drivers are loaded, | ||
96 | they driver model core will attempt to bind the driver to any | ||
97 | previously-discovered devices. Once that has happened, it will be free | ||
98 | to 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 | |||
2 | Porting Drivers to the New Driver Model | ||
3 | |||
4 | Patrick Mochel | ||
5 | |||
6 | 7 January 2003 | ||
7 | |||
8 | |||
9 | Overview | ||
10 | |||
11 | Please refer to Documentation/driver-model/*.txt for definitions of | ||
12 | various driver types and concepts. | ||
13 | |||
14 | Most of the work of porting devices drivers to the new model happens | ||
15 | at the bus driver layer. This was intentional, to minimize the | ||
16 | negative effect on kernel drivers, and to allow a gradual transition | ||
17 | of bus drivers. | ||
18 | |||
19 | In a nutshell, the driver model consists of a set of objects that can | ||
20 | be embedded in larger, bus-specific objects. Fields in these generic | ||
21 | objects can replace fields in the bus-specific objects. | ||
22 | |||
23 | The generic objects must be registered with the driver model core. By | ||
24 | doing so, they will exported via the sysfs filesystem. sysfs can be | ||
25 | mounted by doing | ||
26 | |||
27 | # mount -t sysfs sysfs /sys | ||
28 | |||
29 | |||
30 | |||
31 | The Process | ||
32 | |||
33 | Step 0: Read include/linux/device.h for object and function definitions. | ||
34 | |||
35 | Step 1: Registering the bus driver. | ||
36 | |||
37 | |||
38 | - Define a struct bus_type for the bus driver. | ||
39 | |||
40 | struct 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 | |||
49 | static int __init pci_driver_init(void) | ||
50 | { | ||
51 | return bus_register(&pci_bus_type); | ||
52 | } | ||
53 | |||
54 | subsys_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 | |||
68 | From include/linux/pci.h: | ||
69 | |||
70 | extern struct bus_type pci_bus_type; | ||
71 | |||
72 | |||
73 | From file the above code appears in: | ||
74 | |||
75 | EXPORT_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 | |||
89 | Step 2: Registering Devices. | ||
90 | |||
91 | struct device represents a single device. It mainly contains metadata | ||
92 | describing the relationship the device has to other entities. | ||
93 | |||
94 | |||
95 | - Embedd a struct device in the bus-specific device type. | ||
96 | |||
97 | |||
98 | struct 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 | |||
112 | or | ||
113 | |||
114 | static 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 | |||
226 | Step 3: Registering Drivers. | ||
227 | |||
228 | struct device_driver is a simple driver structure that contains a set | ||
229 | of 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 | |||
236 | struct 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 | |||
279 | Step 4: Define Generic Methods for Drivers. | ||
280 | |||
281 | struct device_driver defines a set of operations that the driver model | ||
282 | core calls. Most of these operations are probably similar to | ||
283 | operations the bus already defines for drivers, but taking different | ||
284 | parameters. | ||
285 | |||
286 | It would be difficult and tedious to force every driver on a bus to | ||
287 | simultaneously convert their drivers to generic format. Instead, the | ||
288 | bus driver should define single instances of the generic methods that | ||
289 | forward call to the bus-specific drivers. For instance: | ||
290 | |||
291 | |||
292 | static 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 | |||
306 | The generic driver should be initialized with these methods before it | ||
307 | is 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 | |||
321 | Ideally, the bus should only initialize the fields if they are not | ||
322 | already set. This allows the drivers to implement their own generic | ||
323 | methods. | ||
324 | |||
325 | |||
326 | Step 5: Support generic driver binding. | ||
327 | |||
328 | The model assumes that a device or driver can be dynamically | ||
329 | registered with the bus at any time. When registration happens, | ||
330 | devices must be bound to a driver, or drivers must be bound to all | ||
331 | devices that it supports. | ||
332 | |||
333 | A driver typically contains a list of device IDs that it supports. The | ||
334 | bus driver compares these IDs to the IDs of devices registered with it. | ||
335 | The format of the device IDs, and the semantics for comparing them are | ||
336 | bus-specific, so the generic model does attempt to generalize them. | ||
337 | |||
338 | Instead, a bus may supply a method in struct bus_type that does the | ||
339 | comparison: | ||
340 | |||
341 | int (*match)(struct device * dev, struct device_driver * drv); | ||
342 | |||
343 | match should return '1' if the driver supports the device, and '0' | ||
344 | otherwise. | ||
345 | |||
346 | When a device is registered, the bus's list of drivers is iterated | ||
347 | over. bus->match() is called for each one until a match is found. | ||
348 | |||
349 | When a driver is registered, the bus's list of devices is iterated | ||
350 | over. bus->match() is called for each device that is not already | ||
351 | claimed by a driver. | ||
352 | |||
353 | When a device is successfully bound to a device, device->driver is | ||
354 | set, the device is added to a per-driver list of devices, and a | ||
355 | symlink is created in the driver's sysfs directory that points to the | ||
356 | device'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 | |||
369 | This driver binding should replace the existing driver binding | ||
370 | mechanism the bus currently uses. | ||
371 | |||
372 | |||
373 | Step 6: Supply a hotplug callback. | ||
374 | |||
375 | Whenever a device is registered with the driver model core, the | ||
376 | userspace program /sbin/hotplug is called to notify userspace. | ||
377 | Users can define actions to perform when a device is inserted or | ||
378 | removed. | ||
379 | |||
380 | The driver model core passes several arguments to userspace via | ||
381 | environment variables, including | ||
382 | |||
383 | - ACTION: set to 'add' or 'remove' | ||
384 | - DEVPATH: set to the device's physical path in sysfs. | ||
385 | |||
386 | A bus driver may also supply additional parameters for userspace to | ||
387 | consume. To do this, a bus must implement the 'hotplug' method in | ||
388 | struct bus_type: | ||
389 | |||
390 | int (*hotplug) (struct device *dev, char **envp, | ||
391 | int num_envp, char *buffer, int buffer_size); | ||
392 | |||
393 | This is called immediately before /sbin/hotplug is executed. | ||
394 | |||
395 | |||
396 | Step 7: Cleaning up the bus driver. | ||
397 | |||
398 | The generic bus, device, and driver structures provide several fields | ||
399 | that can replace those defined privately to the bus driver. | ||
400 | |||
401 | - Device list. | ||
402 | |||
403 | struct bus_type contains a list of all devices registered with the bus | ||
404 | type. This includes all devices on all instances of that bus type. | ||
405 | An internal list that the bus uses may be removed, in favor of using | ||
406 | this one. | ||
407 | |||
408 | The core provides an iterator to access these devices. | ||
409 | |||
410 | int 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 | |||
416 | struct bus_type also contains a list of all drivers registered with | ||
417 | it. An internal list of drivers that the bus driver maintains may | ||
418 | be removed in favor of using the generic one. | ||
419 | |||
420 | The drivers may be iterated over, like devices: | ||
421 | |||
422 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | ||
423 | void * data, int (*fn)(struct device_driver *, void *)); | ||
424 | |||
425 | |||
426 | Please see drivers/base/bus.c for more information. | ||
427 | |||
428 | |||
429 | - rwsem | ||
430 | |||
431 | struct bus_type contains an rwsem that protects all core accesses to | ||
432 | the device and driver lists. This can be used by the bus driver | ||
433 | internally, and should be used when accessing the device or driver | ||
434 | lists the bus maintains. | ||
435 | |||
436 | |||
437 | - Device and driver fields. | ||
438 | |||
439 | Some of the fields in struct device and struct device_driver duplicate | ||
440 | fields in the bus-specific representations of these objects. Feel free | ||
441 | to remove the bus-specific ones and favor the generic ones. Note | ||
442 | though, that this will likely mean fixing up all the drivers that | ||
443 | reference the bus-specific fields (though those should all be 1-line | ||
444 | changes). | ||
445 | |||