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