1*4882a593Smuzhiyun======================================= 2*4882a593SmuzhiyunPorting Drivers to the New Driver Model 3*4882a593Smuzhiyun======================================= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunPatrick Mochel 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun7 January 2003 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunOverview 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunPlease refer to `Documentation/driver-api/driver-model/*.rst` for definitions of 13*4882a593Smuzhiyunvarious driver types and concepts. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunMost of the work of porting devices drivers to the new model happens 16*4882a593Smuzhiyunat the bus driver layer. This was intentional, to minimize the 17*4882a593Smuzhiyunnegative effect on kernel drivers, and to allow a gradual transition 18*4882a593Smuzhiyunof bus drivers. 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunIn a nutshell, the driver model consists of a set of objects that can 21*4882a593Smuzhiyunbe embedded in larger, bus-specific objects. Fields in these generic 22*4882a593Smuzhiyunobjects can replace fields in the bus-specific objects. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunThe generic objects must be registered with the driver model core. By 25*4882a593Smuzhiyundoing so, they will exported via the sysfs filesystem. sysfs can be 26*4882a593Smuzhiyunmounted by doing:: 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun # mount -t sysfs sysfs /sys 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunThe Process 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunStep 0: Read include/linux/device.h for object and function definitions. 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunStep 1: Registering the bus driver. 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun- Define a struct bus_type for the bus driver:: 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun struct bus_type pci_bus_type = { 42*4882a593Smuzhiyun .name = "pci", 43*4882a593Smuzhiyun }; 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun- Register the bus type. 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun This should be done in the initialization function for the bus type, 49*4882a593Smuzhiyun which is usually the module_init(), or equivalent, function:: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun static int __init pci_driver_init(void) 52*4882a593Smuzhiyun { 53*4882a593Smuzhiyun return bus_register(&pci_bus_type); 54*4882a593Smuzhiyun } 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun subsys_initcall(pci_driver_init); 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun The bus type may be unregistered (if the bus driver may be compiled 60*4882a593Smuzhiyun as a module) by doing:: 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun bus_unregister(&pci_bus_type); 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun- Export the bus type for others to use. 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun Other code may wish to reference the bus type, so declare it in a 68*4882a593Smuzhiyun shared header file and export the symbol. 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunFrom include/linux/pci.h:: 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun extern struct bus_type pci_bus_type; 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunFrom file the above code appears in:: 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun EXPORT_SYMBOL(pci_bus_type); 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun- This will cause the bus to show up in /sys/bus/pci/ with two 82*4882a593Smuzhiyun subdirectories: 'devices' and 'drivers':: 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun # tree -d /sys/bus/pci/ 85*4882a593Smuzhiyun /sys/bus/pci/ 86*4882a593Smuzhiyun |-- devices 87*4882a593Smuzhiyun `-- drivers 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunStep 2: Registering Devices. 92*4882a593Smuzhiyun 93*4882a593Smuzhiyunstruct device represents a single device. It mainly contains metadata 94*4882a593Smuzhiyundescribing the relationship the device has to other entities. 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun- Embed a struct device in the bus-specific device type:: 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun struct pci_dev { 101*4882a593Smuzhiyun ... 102*4882a593Smuzhiyun struct device dev; /* Generic device interface */ 103*4882a593Smuzhiyun ... 104*4882a593Smuzhiyun }; 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun It is recommended that the generic device not be the first item in 107*4882a593Smuzhiyun the struct to discourage programmers from doing mindless casts 108*4882a593Smuzhiyun between the object types. Instead macros, or inline functions, 109*4882a593Smuzhiyun should be created to convert from the generic object type:: 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun #define to_pci_dev(n) container_of(n, struct pci_dev, dev) 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun or 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun static inline struct pci_dev * to_pci_dev(struct kobject * kobj) 117*4882a593Smuzhiyun { 118*4882a593Smuzhiyun return container_of(n, struct pci_dev, dev); 119*4882a593Smuzhiyun } 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun This allows the compiler to verify type-safety of the operations 122*4882a593Smuzhiyun that are performed (which is Good). 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun- Initialize the device on registration. 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun When devices are discovered or registered with the bus type, the 128*4882a593Smuzhiyun bus driver should initialize the generic device. The most important 129*4882a593Smuzhiyun things to initialize are the bus_id, parent, and bus fields. 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun The bus_id is an ASCII string that contains the device's address on 132*4882a593Smuzhiyun the bus. The format of this string is bus-specific. This is 133*4882a593Smuzhiyun necessary for representing devices in sysfs. 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun parent is the physical parent of the device. It is important that 136*4882a593Smuzhiyun the bus driver sets this field correctly. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun The driver model maintains an ordered list of devices that it uses 139*4882a593Smuzhiyun for power management. This list must be in order to guarantee that 140*4882a593Smuzhiyun devices are shutdown before their physical parents, and vice versa. 141*4882a593Smuzhiyun The order of this list is determined by the parent of registered 142*4882a593Smuzhiyun devices. 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun Also, the location of the device's sysfs directory depends on a 145*4882a593Smuzhiyun device's parent. sysfs exports a directory structure that mirrors 146*4882a593Smuzhiyun the device hierarchy. Accurately setting the parent guarantees that 147*4882a593Smuzhiyun sysfs will accurately represent the hierarchy. 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun The device's bus field is a pointer to the bus type the device 150*4882a593Smuzhiyun belongs to. This should be set to the bus_type that was declared 151*4882a593Smuzhiyun and initialized before. 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun Optionally, the bus driver may set the device's name and release 154*4882a593Smuzhiyun fields. 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun The name field is an ASCII string describing the device, like 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun "ATI Technologies Inc Radeon QD" 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun The release field is a callback that the driver model core calls 161*4882a593Smuzhiyun when the device has been removed, and all references to it have 162*4882a593Smuzhiyun been released. More on this in a moment. 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun- Register the device. 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun Once the generic device has been initialized, it can be registered 168*4882a593Smuzhiyun with the driver model core by doing:: 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun device_register(&dev->dev); 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun It can later be unregistered by doing:: 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun device_unregister(&dev->dev); 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun This should happen on buses that support hotpluggable devices. 177*4882a593Smuzhiyun If a bus driver unregisters a device, it should not immediately free 178*4882a593Smuzhiyun it. It should instead wait for the driver model core to call the 179*4882a593Smuzhiyun device's release method, then free the bus-specific object. 180*4882a593Smuzhiyun (There may be other code that is currently referencing the device 181*4882a593Smuzhiyun structure, and it would be rude to free the device while that is 182*4882a593Smuzhiyun happening). 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun When the device is registered, a directory in sysfs is created. 186*4882a593Smuzhiyun The PCI tree in sysfs looks like:: 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun /sys/devices/pci0/ 189*4882a593Smuzhiyun |-- 00:00.0 190*4882a593Smuzhiyun |-- 00:01.0 191*4882a593Smuzhiyun | `-- 01:00.0 192*4882a593Smuzhiyun |-- 00:02.0 193*4882a593Smuzhiyun | `-- 02:1f.0 194*4882a593Smuzhiyun | `-- 03:00.0 195*4882a593Smuzhiyun |-- 00:1e.0 196*4882a593Smuzhiyun | `-- 04:04.0 197*4882a593Smuzhiyun |-- 00:1f.0 198*4882a593Smuzhiyun |-- 00:1f.1 199*4882a593Smuzhiyun | |-- ide0 200*4882a593Smuzhiyun | | |-- 0.0 201*4882a593Smuzhiyun | | `-- 0.1 202*4882a593Smuzhiyun | `-- ide1 203*4882a593Smuzhiyun | `-- 1.0 204*4882a593Smuzhiyun |-- 00:1f.2 205*4882a593Smuzhiyun |-- 00:1f.3 206*4882a593Smuzhiyun `-- 00:1f.5 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun Also, symlinks are created in the bus's 'devices' directory 209*4882a593Smuzhiyun that point to the device's directory in the physical hierarchy:: 210*4882a593Smuzhiyun 211*4882a593Smuzhiyun /sys/bus/pci/devices/ 212*4882a593Smuzhiyun |-- 00:00.0 -> ../../../devices/pci0/00:00.0 213*4882a593Smuzhiyun |-- 00:01.0 -> ../../../devices/pci0/00:01.0 214*4882a593Smuzhiyun |-- 00:02.0 -> ../../../devices/pci0/00:02.0 215*4882a593Smuzhiyun |-- 00:1e.0 -> ../../../devices/pci0/00:1e.0 216*4882a593Smuzhiyun |-- 00:1f.0 -> ../../../devices/pci0/00:1f.0 217*4882a593Smuzhiyun |-- 00:1f.1 -> ../../../devices/pci0/00:1f.1 218*4882a593Smuzhiyun |-- 00:1f.2 -> ../../../devices/pci0/00:1f.2 219*4882a593Smuzhiyun |-- 00:1f.3 -> ../../../devices/pci0/00:1f.3 220*4882a593Smuzhiyun |-- 00:1f.5 -> ../../../devices/pci0/00:1f.5 221*4882a593Smuzhiyun |-- 01:00.0 -> ../../../devices/pci0/00:01.0/01:00.0 222*4882a593Smuzhiyun |-- 02:1f.0 -> ../../../devices/pci0/00:02.0/02:1f.0 223*4882a593Smuzhiyun |-- 03:00.0 -> ../../../devices/pci0/00:02.0/02:1f.0/03:00.0 224*4882a593Smuzhiyun `-- 04:04.0 -> ../../../devices/pci0/00:1e.0/04:04.0 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunStep 3: Registering Drivers. 229*4882a593Smuzhiyun 230*4882a593Smuzhiyunstruct device_driver is a simple driver structure that contains a set 231*4882a593Smuzhiyunof operations that the driver model core may call. 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun- Embed a struct device_driver in the bus-specific driver. 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun Just like with devices, do something like:: 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun struct pci_driver { 239*4882a593Smuzhiyun ... 240*4882a593Smuzhiyun struct device_driver driver; 241*4882a593Smuzhiyun }; 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun- Initialize the generic driver structure. 245*4882a593Smuzhiyun 246*4882a593Smuzhiyun When the driver registers with the bus (e.g. doing pci_register_driver()), 247*4882a593Smuzhiyun initialize the necessary fields of the driver: the name and bus 248*4882a593Smuzhiyun fields. 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun- Register the driver. 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun After the generic driver has been initialized, call:: 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun driver_register(&drv->driver); 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun to register the driver with the core. 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun When the driver is unregistered from the bus, unregister it from the 260*4882a593Smuzhiyun core by doing:: 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun driver_unregister(&drv->driver); 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun Note that this will block until all references to the driver have 265*4882a593Smuzhiyun gone away. Normally, there will not be any. 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun- Sysfs representation. 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun Drivers are exported via sysfs in their bus's 'driver's directory. 271*4882a593Smuzhiyun For example:: 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun /sys/bus/pci/drivers/ 274*4882a593Smuzhiyun |-- 3c59x 275*4882a593Smuzhiyun |-- Ensoniq AudioPCI 276*4882a593Smuzhiyun |-- agpgart-amdk7 277*4882a593Smuzhiyun |-- e100 278*4882a593Smuzhiyun `-- serial 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun 281*4882a593SmuzhiyunStep 4: Define Generic Methods for Drivers. 282*4882a593Smuzhiyun 283*4882a593Smuzhiyunstruct device_driver defines a set of operations that the driver model 284*4882a593Smuzhiyuncore calls. Most of these operations are probably similar to 285*4882a593Smuzhiyunoperations the bus already defines for drivers, but taking different 286*4882a593Smuzhiyunparameters. 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunIt would be difficult and tedious to force every driver on a bus to 289*4882a593Smuzhiyunsimultaneously convert their drivers to generic format. Instead, the 290*4882a593Smuzhiyunbus driver should define single instances of the generic methods that 291*4882a593Smuzhiyunforward call to the bus-specific drivers. For instance:: 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun 294*4882a593Smuzhiyun static int pci_device_remove(struct device * dev) 295*4882a593Smuzhiyun { 296*4882a593Smuzhiyun struct pci_dev * pci_dev = to_pci_dev(dev); 297*4882a593Smuzhiyun struct pci_driver * drv = pci_dev->driver; 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun if (drv) { 300*4882a593Smuzhiyun if (drv->remove) 301*4882a593Smuzhiyun drv->remove(pci_dev); 302*4882a593Smuzhiyun pci_dev->driver = NULL; 303*4882a593Smuzhiyun } 304*4882a593Smuzhiyun return 0; 305*4882a593Smuzhiyun } 306*4882a593Smuzhiyun 307*4882a593Smuzhiyun 308*4882a593SmuzhiyunThe generic driver should be initialized with these methods before it 309*4882a593Smuzhiyunis registered:: 310*4882a593Smuzhiyun 311*4882a593Smuzhiyun /* initialize common driver fields */ 312*4882a593Smuzhiyun drv->driver.name = drv->name; 313*4882a593Smuzhiyun drv->driver.bus = &pci_bus_type; 314*4882a593Smuzhiyun drv->driver.probe = pci_device_probe; 315*4882a593Smuzhiyun drv->driver.resume = pci_device_resume; 316*4882a593Smuzhiyun drv->driver.suspend = pci_device_suspend; 317*4882a593Smuzhiyun drv->driver.remove = pci_device_remove; 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun /* register with core */ 320*4882a593Smuzhiyun driver_register(&drv->driver); 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun 323*4882a593SmuzhiyunIdeally, the bus should only initialize the fields if they are not 324*4882a593Smuzhiyunalready set. This allows the drivers to implement their own generic 325*4882a593Smuzhiyunmethods. 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunStep 5: Support generic driver binding. 329*4882a593Smuzhiyun 330*4882a593SmuzhiyunThe model assumes that a device or driver can be dynamically 331*4882a593Smuzhiyunregistered with the bus at any time. When registration happens, 332*4882a593Smuzhiyundevices must be bound to a driver, or drivers must be bound to all 333*4882a593Smuzhiyundevices that it supports. 334*4882a593Smuzhiyun 335*4882a593SmuzhiyunA driver typically contains a list of device IDs that it supports. The 336*4882a593Smuzhiyunbus driver compares these IDs to the IDs of devices registered with it. 337*4882a593SmuzhiyunThe format of the device IDs, and the semantics for comparing them are 338*4882a593Smuzhiyunbus-specific, so the generic model does attempt to generalize them. 339*4882a593Smuzhiyun 340*4882a593SmuzhiyunInstead, a bus may supply a method in struct bus_type that does the 341*4882a593Smuzhiyuncomparison:: 342*4882a593Smuzhiyun 343*4882a593Smuzhiyun int (*match)(struct device * dev, struct device_driver * drv); 344*4882a593Smuzhiyun 345*4882a593Smuzhiyunmatch should return positive value if the driver supports the device, 346*4882a593Smuzhiyunand zero otherwise. It may also return error code (for example 347*4882a593Smuzhiyun-EPROBE_DEFER) if determining that given driver supports the device is 348*4882a593Smuzhiyunnot possible. 349*4882a593Smuzhiyun 350*4882a593SmuzhiyunWhen a device is registered, the bus's list of drivers is iterated 351*4882a593Smuzhiyunover. bus->match() is called for each one until a match is found. 352*4882a593Smuzhiyun 353*4882a593SmuzhiyunWhen a driver is registered, the bus's list of devices is iterated 354*4882a593Smuzhiyunover. bus->match() is called for each device that is not already 355*4882a593Smuzhiyunclaimed by a driver. 356*4882a593Smuzhiyun 357*4882a593SmuzhiyunWhen a device is successfully bound to a driver, device->driver is 358*4882a593Smuzhiyunset, the device is added to a per-driver list of devices, and a 359*4882a593Smuzhiyunsymlink is created in the driver's sysfs directory that points to the 360*4882a593Smuzhiyundevice's physical directory:: 361*4882a593Smuzhiyun 362*4882a593Smuzhiyun /sys/bus/pci/drivers/ 363*4882a593Smuzhiyun |-- 3c59x 364*4882a593Smuzhiyun | `-- 00:0b.0 -> ../../../../devices/pci0/00:0b.0 365*4882a593Smuzhiyun |-- Ensoniq AudioPCI 366*4882a593Smuzhiyun |-- agpgart-amdk7 367*4882a593Smuzhiyun | `-- 00:00.0 -> ../../../../devices/pci0/00:00.0 368*4882a593Smuzhiyun |-- e100 369*4882a593Smuzhiyun | `-- 00:0c.0 -> ../../../../devices/pci0/00:0c.0 370*4882a593Smuzhiyun `-- serial 371*4882a593Smuzhiyun 372*4882a593Smuzhiyun 373*4882a593SmuzhiyunThis driver binding should replace the existing driver binding 374*4882a593Smuzhiyunmechanism the bus currently uses. 375*4882a593Smuzhiyun 376*4882a593Smuzhiyun 377*4882a593SmuzhiyunStep 6: Supply a hotplug callback. 378*4882a593Smuzhiyun 379*4882a593SmuzhiyunWhenever a device is registered with the driver model core, the 380*4882a593Smuzhiyunuserspace program /sbin/hotplug is called to notify userspace. 381*4882a593SmuzhiyunUsers can define actions to perform when a device is inserted or 382*4882a593Smuzhiyunremoved. 383*4882a593Smuzhiyun 384*4882a593SmuzhiyunThe driver model core passes several arguments to userspace via 385*4882a593Smuzhiyunenvironment variables, including 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun- ACTION: set to 'add' or 'remove' 388*4882a593Smuzhiyun- DEVPATH: set to the device's physical path in sysfs. 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunA bus driver may also supply additional parameters for userspace to 391*4882a593Smuzhiyunconsume. To do this, a bus must implement the 'hotplug' method in 392*4882a593Smuzhiyunstruct bus_type:: 393*4882a593Smuzhiyun 394*4882a593Smuzhiyun int (*hotplug) (struct device *dev, char **envp, 395*4882a593Smuzhiyun int num_envp, char *buffer, int buffer_size); 396*4882a593Smuzhiyun 397*4882a593SmuzhiyunThis is called immediately before /sbin/hotplug is executed. 398*4882a593Smuzhiyun 399*4882a593Smuzhiyun 400*4882a593SmuzhiyunStep 7: Cleaning up the bus driver. 401*4882a593Smuzhiyun 402*4882a593SmuzhiyunThe generic bus, device, and driver structures provide several fields 403*4882a593Smuzhiyunthat can replace those defined privately to the bus driver. 404*4882a593Smuzhiyun 405*4882a593Smuzhiyun- Device list. 406*4882a593Smuzhiyun 407*4882a593Smuzhiyunstruct bus_type contains a list of all devices registered with the bus 408*4882a593Smuzhiyuntype. This includes all devices on all instances of that bus type. 409*4882a593SmuzhiyunAn internal list that the bus uses may be removed, in favor of using 410*4882a593Smuzhiyunthis one. 411*4882a593Smuzhiyun 412*4882a593SmuzhiyunThe core provides an iterator to access these devices:: 413*4882a593Smuzhiyun 414*4882a593Smuzhiyun int bus_for_each_dev(struct bus_type * bus, struct device * start, 415*4882a593Smuzhiyun void * data, int (*fn)(struct device *, void *)); 416*4882a593Smuzhiyun 417*4882a593Smuzhiyun 418*4882a593Smuzhiyun- Driver list. 419*4882a593Smuzhiyun 420*4882a593Smuzhiyunstruct bus_type also contains a list of all drivers registered with 421*4882a593Smuzhiyunit. An internal list of drivers that the bus driver maintains may 422*4882a593Smuzhiyunbe removed in favor of using the generic one. 423*4882a593Smuzhiyun 424*4882a593SmuzhiyunThe drivers may be iterated over, like devices:: 425*4882a593Smuzhiyun 426*4882a593Smuzhiyun int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, 427*4882a593Smuzhiyun void * data, int (*fn)(struct device_driver *, void *)); 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun 430*4882a593SmuzhiyunPlease see drivers/base/bus.c for more information. 431*4882a593Smuzhiyun 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun- rwsem 434*4882a593Smuzhiyun 435*4882a593Smuzhiyunstruct bus_type contains an rwsem that protects all core accesses to 436*4882a593Smuzhiyunthe device and driver lists. This can be used by the bus driver 437*4882a593Smuzhiyuninternally, and should be used when accessing the device or driver 438*4882a593Smuzhiyunlists the bus maintains. 439*4882a593Smuzhiyun 440*4882a593Smuzhiyun 441*4882a593Smuzhiyun- Device and driver fields. 442*4882a593Smuzhiyun 443*4882a593SmuzhiyunSome of the fields in struct device and struct device_driver duplicate 444*4882a593Smuzhiyunfields in the bus-specific representations of these objects. Feel free 445*4882a593Smuzhiyunto remove the bus-specific ones and favor the generic ones. Note 446*4882a593Smuzhiyunthough, that this will likely mean fixing up all the drivers that 447*4882a593Smuzhiyunreference the bus-specific fields (though those should all be 1-line 448*4882a593Smuzhiyunchanges). 449