Back to home page

OSCL-LXR

 
 

    


0001 =======================================
0002 Porting Drivers to the New Driver Model
0003 =======================================
0004 
0005 Patrick Mochel
0006 
0007 7 January 2003
0008 
0009 
0010 Overview
0011 
0012 Please refer to `Documentation/driver-api/driver-model/*.rst` for definitions of
0013 various driver types and concepts.
0014 
0015 Most of the work of porting devices drivers to the new model happens
0016 at the bus driver layer. This was intentional, to minimize the
0017 negative effect on kernel drivers, and to allow a gradual transition
0018 of bus drivers.
0019 
0020 In a nutshell, the driver model consists of a set of objects that can
0021 be embedded in larger, bus-specific objects. Fields in these generic
0022 objects can replace fields in the bus-specific objects.
0023 
0024 The generic objects must be registered with the driver model core. By
0025 doing so, they will exported via the sysfs filesystem. sysfs can be
0026 mounted by doing::
0027 
0028         # mount -t sysfs sysfs /sys
0029 
0030 
0031 
0032 The Process
0033 
0034 Step 0: Read include/linux/device.h for object and function definitions.
0035 
0036 Step 1: Registering the bus driver.
0037 
0038 
0039 - Define a struct bus_type for the bus driver::
0040 
0041     struct bus_type pci_bus_type = {
0042           .name           = "pci",
0043     };
0044 
0045 
0046 - Register the bus type.
0047 
0048   This should be done in the initialization function for the bus type,
0049   which is usually the module_init(), or equivalent, function::
0050 
0051     static int __init pci_driver_init(void)
0052     {
0053             return bus_register(&pci_bus_type);
0054     }
0055 
0056     subsys_initcall(pci_driver_init);
0057 
0058 
0059   The bus type may be unregistered (if the bus driver may be compiled
0060   as a module) by doing::
0061 
0062      bus_unregister(&pci_bus_type);
0063 
0064 
0065 - Export the bus type for others to use.
0066 
0067   Other code may wish to reference the bus type, so declare it in a
0068   shared header file and export the symbol.
0069 
0070 From include/linux/pci.h::
0071 
0072   extern struct bus_type pci_bus_type;
0073 
0074 
0075 From file the above code appears in::
0076 
0077   EXPORT_SYMBOL(pci_bus_type);
0078 
0079 
0080 
0081 - This will cause the bus to show up in /sys/bus/pci/ with two
0082   subdirectories: 'devices' and 'drivers'::
0083 
0084     # tree -d /sys/bus/pci/
0085     /sys/bus/pci/
0086     |-- devices
0087     `-- drivers
0088 
0089 
0090 
0091 Step 2: Registering Devices.
0092 
0093 struct device represents a single device. It mainly contains metadata
0094 describing the relationship the device has to other entities.
0095 
0096 
0097 - Embed a struct device in the bus-specific device type::
0098 
0099 
0100     struct pci_dev {
0101            ...
0102            struct  device  dev;            /* Generic device interface */
0103            ...
0104     };
0105 
0106   It is recommended that the generic device not be the first item in
0107   the struct to discourage programmers from doing mindless casts
0108   between the object types. Instead macros, or inline functions,
0109   should be created to convert from the generic object type::
0110 
0111 
0112     #define to_pci_dev(n) container_of(n, struct pci_dev, dev)
0113 
0114     or
0115 
0116     static inline struct pci_dev * to_pci_dev(struct kobject * kobj)
0117     {
0118         return container_of(n, struct pci_dev, dev);
0119     }
0120 
0121   This allows the compiler to verify type-safety of the operations
0122   that are performed (which is Good).
0123 
0124 
0125 - Initialize the device on registration.
0126 
0127   When devices are discovered or registered with the bus type, the
0128   bus driver should initialize the generic device. The most important
0129   things to initialize are the bus_id, parent, and bus fields.
0130 
0131   The bus_id is an ASCII string that contains the device's address on
0132   the bus. The format of this string is bus-specific. This is
0133   necessary for representing devices in sysfs.
0134 
0135   parent is the physical parent of the device. It is important that
0136   the bus driver sets this field correctly.
0137 
0138   The driver model maintains an ordered list of devices that it uses
0139   for power management. This list must be in order to guarantee that
0140   devices are shutdown before their physical parents, and vice versa.
0141   The order of this list is determined by the parent of registered
0142   devices.
0143 
0144   Also, the location of the device's sysfs directory depends on a
0145   device's parent. sysfs exports a directory structure that mirrors
0146   the device hierarchy. Accurately setting the parent guarantees that
0147   sysfs will accurately represent the hierarchy.
0148 
0149   The device's bus field is a pointer to the bus type the device
0150   belongs to. This should be set to the bus_type that was declared
0151   and initialized before.
0152 
0153   Optionally, the bus driver may set the device's name and release
0154   fields.
0155 
0156   The name field is an ASCII string describing the device, like
0157 
0158      "ATI Technologies Inc Radeon QD"
0159 
0160   The release field is a callback that the driver model core calls
0161   when the device has been removed, and all references to it have
0162   been released. More on this in a moment.
0163 
0164 
0165 - Register the device.
0166 
0167   Once the generic device has been initialized, it can be registered
0168   with the driver model core by doing::
0169 
0170        device_register(&dev->dev);
0171 
0172   It can later be unregistered by doing::
0173 
0174        device_unregister(&dev->dev);
0175 
0176   This should happen on buses that support hotpluggable devices.
0177   If a bus driver unregisters a device, it should not immediately free
0178   it. It should instead wait for the driver model core to call the
0179   device's release method, then free the bus-specific object.
0180   (There may be other code that is currently referencing the device
0181   structure, and it would be rude to free the device while that is
0182   happening).
0183 
0184 
0185   When the device is registered, a directory in sysfs is created.
0186   The PCI tree in sysfs looks like::
0187 
0188     /sys/devices/pci0/
0189     |-- 00:00.0
0190     |-- 00:01.0
0191     |   `-- 01:00.0
0192     |-- 00:02.0
0193     |   `-- 02:1f.0
0194     |       `-- 03:00.0
0195     |-- 00:1e.0
0196     |   `-- 04:04.0
0197     |-- 00:1f.0
0198     |-- 00:1f.1
0199     |   |-- ide0
0200     |   |   |-- 0.0
0201     |   |   `-- 0.1
0202     |   `-- ide1
0203     |       `-- 1.0
0204     |-- 00:1f.2
0205     |-- 00:1f.3
0206     `-- 00:1f.5
0207 
0208   Also, symlinks are created in the bus's 'devices' directory
0209   that point to the device's directory in the physical hierarchy::
0210 
0211     /sys/bus/pci/devices/
0212     |-- 00:00.0 -> ../../../devices/pci0/00:00.0
0213     |-- 00:01.0 -> ../../../devices/pci0/00:01.0
0214     |-- 00:02.0 -> ../../../devices/pci0/00:02.0
0215     |-- 00:1e.0 -> ../../../devices/pci0/00:1e.0
0216     |-- 00:1f.0 -> ../../../devices/pci0/00:1f.0
0217     |-- 00:1f.1 -> ../../../devices/pci0/00:1f.1
0218     |-- 00:1f.2 -> ../../../devices/pci0/00:1f.2
0219     |-- 00:1f.3 -> ../../../devices/pci0/00:1f.3
0220     |-- 00:1f.5 -> ../../../devices/pci0/00:1f.5
0221     |-- 01:00.0 -> ../../../devices/pci0/00:01.0/01:00.0
0222     |-- 02:1f.0 -> ../../../devices/pci0/00:02.0/02:1f.0
0223     |-- 03:00.0 -> ../../../devices/pci0/00:02.0/02:1f.0/03:00.0
0224     `-- 04:04.0 -> ../../../devices/pci0/00:1e.0/04:04.0
0225 
0226 
0227 
0228 Step 3: Registering Drivers.
0229 
0230 struct device_driver is a simple driver structure that contains a set
0231 of operations that the driver model core may call.
0232 
0233 
0234 - Embed a struct device_driver in the bus-specific driver.
0235 
0236   Just like with devices, do something like::
0237 
0238     struct pci_driver {
0239            ...
0240            struct device_driver    driver;
0241     };
0242 
0243 
0244 - Initialize the generic driver structure.
0245 
0246   When the driver registers with the bus (e.g. doing pci_register_driver()),
0247   initialize the necessary fields of the driver: the name and bus
0248   fields.
0249 
0250 
0251 - Register the driver.
0252 
0253   After the generic driver has been initialized, call::
0254 
0255         driver_register(&drv->driver);
0256 
0257   to register the driver with the core.
0258 
0259   When the driver is unregistered from the bus, unregister it from the
0260   core by doing::
0261 
0262         driver_unregister(&drv->driver);
0263 
0264   Note that this will block until all references to the driver have
0265   gone away. Normally, there will not be any.
0266 
0267 
0268 - Sysfs representation.
0269 
0270   Drivers are exported via sysfs in their bus's 'driver's directory.
0271   For example::
0272 
0273     /sys/bus/pci/drivers/
0274     |-- 3c59x
0275     |-- Ensoniq AudioPCI
0276     |-- agpgart-amdk7
0277     |-- e100
0278     `-- serial
0279 
0280 
0281 Step 4: Define Generic Methods for Drivers.
0282 
0283 struct device_driver defines a set of operations that the driver model
0284 core calls. Most of these operations are probably similar to
0285 operations the bus already defines for drivers, but taking different
0286 parameters.
0287 
0288 It would be difficult and tedious to force every driver on a bus to
0289 simultaneously convert their drivers to generic format. Instead, the
0290 bus driver should define single instances of the generic methods that
0291 forward call to the bus-specific drivers. For instance::
0292 
0293 
0294   static int pci_device_remove(struct device * dev)
0295   {
0296           struct pci_dev * pci_dev = to_pci_dev(dev);
0297           struct pci_driver * drv = pci_dev->driver;
0298 
0299           if (drv) {
0300                   if (drv->remove)
0301                           drv->remove(pci_dev);
0302                   pci_dev->driver = NULL;
0303           }
0304           return 0;
0305   }
0306 
0307 
0308 The generic driver should be initialized with these methods before it
0309 is registered::
0310 
0311         /* initialize common driver fields */
0312         drv->driver.name = drv->name;
0313         drv->driver.bus = &pci_bus_type;
0314         drv->driver.probe = pci_device_probe;
0315         drv->driver.resume = pci_device_resume;
0316         drv->driver.suspend = pci_device_suspend;
0317         drv->driver.remove = pci_device_remove;
0318 
0319         /* register with core */
0320         driver_register(&drv->driver);
0321 
0322 
0323 Ideally, the bus should only initialize the fields if they are not
0324 already set. This allows the drivers to implement their own generic
0325 methods.
0326 
0327 
0328 Step 5: Support generic driver binding.
0329 
0330 The model assumes that a device or driver can be dynamically
0331 registered with the bus at any time. When registration happens,
0332 devices must be bound to a driver, or drivers must be bound to all
0333 devices that it supports.
0334 
0335 A driver typically contains a list of device IDs that it supports. The
0336 bus driver compares these IDs to the IDs of devices registered with it.
0337 The format of the device IDs, and the semantics for comparing them are
0338 bus-specific, so the generic model does attempt to generalize them.
0339 
0340 Instead, a bus may supply a method in struct bus_type that does the
0341 comparison::
0342 
0343   int (*match)(struct device * dev, struct device_driver * drv);
0344 
0345 match should return positive value if the driver supports the device,
0346 and zero otherwise. It may also return error code (for example
0347 -EPROBE_DEFER) if determining that given driver supports the device is
0348 not possible.
0349 
0350 When a device is registered, the bus's list of drivers is iterated
0351 over. bus->match() is called for each one until a match is found.
0352 
0353 When a driver is registered, the bus's list of devices is iterated
0354 over. bus->match() is called for each device that is not already
0355 claimed by a driver.
0356 
0357 When a device is successfully bound to a driver, device->driver is
0358 set, the device is added to a per-driver list of devices, and a
0359 symlink is created in the driver's sysfs directory that points to the
0360 device's physical directory::
0361 
0362   /sys/bus/pci/drivers/
0363   |-- 3c59x
0364   |   `-- 00:0b.0 -> ../../../../devices/pci0/00:0b.0
0365   |-- Ensoniq AudioPCI
0366   |-- agpgart-amdk7
0367   |   `-- 00:00.0 -> ../../../../devices/pci0/00:00.0
0368   |-- e100
0369   |   `-- 00:0c.0 -> ../../../../devices/pci0/00:0c.0
0370   `-- serial
0371 
0372 
0373 This driver binding should replace the existing driver binding
0374 mechanism the bus currently uses.
0375 
0376 
0377 Step 6: Supply a hotplug callback.
0378 
0379 Whenever a device is registered with the driver model core, the
0380 userspace program /sbin/hotplug is called to notify userspace.
0381 Users can define actions to perform when a device is inserted or
0382 removed.
0383 
0384 The driver model core passes several arguments to userspace via
0385 environment variables, including
0386 
0387 - ACTION: set to 'add' or 'remove'
0388 - DEVPATH: set to the device's physical path in sysfs.
0389 
0390 A bus driver may also supply additional parameters for userspace to
0391 consume. To do this, a bus must implement the 'hotplug' method in
0392 struct bus_type::
0393 
0394      int (*hotplug) (struct device *dev, char **envp,
0395                      int num_envp, char *buffer, int buffer_size);
0396 
0397 This is called immediately before /sbin/hotplug is executed.
0398 
0399 
0400 Step 7: Cleaning up the bus driver.
0401 
0402 The generic bus, device, and driver structures provide several fields
0403 that can replace those defined privately to the bus driver.
0404 
0405 - Device list.
0406 
0407 struct bus_type contains a list of all devices registered with the bus
0408 type. This includes all devices on all instances of that bus type.
0409 An internal list that the bus uses may be removed, in favor of using
0410 this one.
0411 
0412 The core provides an iterator to access these devices::
0413 
0414   int bus_for_each_dev(struct bus_type * bus, struct device * start,
0415                        void * data, int (*fn)(struct device *, void *));
0416 
0417 
0418 - Driver list.
0419 
0420 struct bus_type also contains a list of all drivers registered with
0421 it. An internal list of drivers that the bus driver maintains may
0422 be removed in favor of using the generic one.
0423 
0424 The drivers may be iterated over, like devices::
0425 
0426   int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
0427                        void * data, int (*fn)(struct device_driver *, void *));
0428 
0429 
0430 Please see drivers/base/bus.c for more information.
0431 
0432 
0433 - rwsem
0434 
0435 struct bus_type contains an rwsem that protects all core accesses to
0436 the device and driver lists. This can be used by the bus driver
0437 internally, and should be used when accessing the device or driver
0438 lists the bus maintains.
0439 
0440 
0441 - Device and driver fields.
0442 
0443 Some of the fields in struct device and struct device_driver duplicate
0444 fields in the bus-specific representations of these objects. Feel free
0445 to remove the bus-specific ones and favor the generic ones. Note
0446 though, that this will likely mean fixing up all the drivers that
0447 reference the bus-specific fields (though those should all be 1-line
0448 changes).