1*4882a593Smuzhiyun============== 2*4882a593SmuzhiyunDevice Drivers 3*4882a593Smuzhiyun============== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunSee the kerneldoc for the struct device_driver. 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunAllocation 8*4882a593Smuzhiyun~~~~~~~~~~ 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunDevice drivers are statically allocated structures. Though there may 11*4882a593Smuzhiyunbe multiple devices in a system that a driver supports, struct 12*4882a593Smuzhiyundevice_driver represents the driver as a whole (not a particular 13*4882a593Smuzhiyundevice instance). 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunInitialization 16*4882a593Smuzhiyun~~~~~~~~~~~~~~ 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunThe driver must initialize at least the name and bus fields. It should 19*4882a593Smuzhiyunalso initialize the devclass field (when it arrives), so it may obtain 20*4882a593Smuzhiyunthe proper linkage internally. It should also initialize as many of 21*4882a593Smuzhiyunthe callbacks as possible, though each is optional. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunDeclaration 24*4882a593Smuzhiyun~~~~~~~~~~~ 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunAs stated above, struct device_driver objects are statically 27*4882a593Smuzhiyunallocated. Below is an example declaration of the eepro100 28*4882a593Smuzhiyundriver. This declaration is hypothetical only; it relies on the driver 29*4882a593Smuzhiyunbeing converted completely to the new model:: 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun static struct device_driver eepro100_driver = { 32*4882a593Smuzhiyun .name = "eepro100", 33*4882a593Smuzhiyun .bus = &pci_bus_type, 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun .probe = eepro100_probe, 36*4882a593Smuzhiyun .remove = eepro100_remove, 37*4882a593Smuzhiyun .suspend = eepro100_suspend, 38*4882a593Smuzhiyun .resume = eepro100_resume, 39*4882a593Smuzhiyun }; 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunMost drivers will not be able to be converted completely to the new 42*4882a593Smuzhiyunmodel because the bus they belong to has a bus-specific structure with 43*4882a593Smuzhiyunbus-specific fields that cannot be generalized. 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunThe most common example of this are device ID structures. A driver 46*4882a593Smuzhiyuntypically defines an array of device IDs that it supports. The format 47*4882a593Smuzhiyunof these structures and the semantics for comparing device IDs are 48*4882a593Smuzhiyuncompletely bus-specific. Defining them as bus-specific entities would 49*4882a593Smuzhiyunsacrifice type-safety, so we keep bus-specific structures around. 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunBus-specific drivers should include a generic struct device_driver in 52*4882a593Smuzhiyunthe definition of the bus-specific driver. Like this:: 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun struct pci_driver { 55*4882a593Smuzhiyun const struct pci_device_id *id_table; 56*4882a593Smuzhiyun struct device_driver driver; 57*4882a593Smuzhiyun }; 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunA definition that included bus-specific fields would look like 60*4882a593Smuzhiyun(using the eepro100 driver again):: 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun static struct pci_driver eepro100_driver = { 63*4882a593Smuzhiyun .id_table = eepro100_pci_tbl, 64*4882a593Smuzhiyun .driver = { 65*4882a593Smuzhiyun .name = "eepro100", 66*4882a593Smuzhiyun .bus = &pci_bus_type, 67*4882a593Smuzhiyun .probe = eepro100_probe, 68*4882a593Smuzhiyun .remove = eepro100_remove, 69*4882a593Smuzhiyun .suspend = eepro100_suspend, 70*4882a593Smuzhiyun .resume = eepro100_resume, 71*4882a593Smuzhiyun }, 72*4882a593Smuzhiyun }; 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunSome may find the syntax of embedded struct initialization awkward or 75*4882a593Smuzhiyuneven a bit ugly. So far, it's the best way we've found to do what we want... 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunRegistration 78*4882a593Smuzhiyun~~~~~~~~~~~~ 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun:: 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun int driver_register(struct device_driver *drv); 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunThe driver registers the structure on startup. For drivers that have 85*4882a593Smuzhiyunno bus-specific fields (i.e. don't have a bus-specific driver 86*4882a593Smuzhiyunstructure), they would use driver_register and pass a pointer to their 87*4882a593Smuzhiyunstruct device_driver object. 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunMost drivers, however, will have a bus-specific structure and will 90*4882a593Smuzhiyunneed to register with the bus using something like pci_driver_register. 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunIt is important that drivers register their driver structure as early as 93*4882a593Smuzhiyunpossible. Registration with the core initializes several fields in the 94*4882a593Smuzhiyunstruct device_driver object, including the reference count and the 95*4882a593Smuzhiyunlock. These fields are assumed to be valid at all times and may be 96*4882a593Smuzhiyunused by the device model core or the bus driver. 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunTransition Bus Drivers 100*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunBy defining wrapper functions, the transition to the new model can be 103*4882a593Smuzhiyunmade easier. Drivers can ignore the generic structure altogether and 104*4882a593Smuzhiyunlet the bus wrapper fill in the fields. For the callbacks, the bus can 105*4882a593Smuzhiyundefine generic callbacks that forward the call to the bus-specific 106*4882a593Smuzhiyuncallbacks of the drivers. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunThis solution is intended to be only temporary. In order to get class 109*4882a593Smuzhiyuninformation in the driver, the drivers must be modified anyway. Since 110*4882a593Smuzhiyunconverting drivers to the new model should reduce some infrastructural 111*4882a593Smuzhiyuncomplexity and code size, it is recommended that they are converted as 112*4882a593Smuzhiyunclass information is added. 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunAccess 115*4882a593Smuzhiyun~~~~~~ 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunOnce the object has been registered, it may access the common fields of 118*4882a593Smuzhiyunthe object, like the lock and the list of devices:: 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun int driver_for_each_dev(struct device_driver *drv, void *data, 121*4882a593Smuzhiyun int (*callback)(struct device *dev, void *data)); 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunThe devices field is a list of all the devices that have been bound to 124*4882a593Smuzhiyunthe driver. The LDM core provides a helper function to operate on all 125*4882a593Smuzhiyunthe devices a driver controls. This helper locks the driver on each 126*4882a593Smuzhiyunnode access, and does proper reference counting on each device as it 127*4882a593Smuzhiyunaccesses it. 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun 130*4882a593Smuzhiyunsysfs 131*4882a593Smuzhiyun~~~~~ 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunWhen a driver is registered, a sysfs directory is created in its 134*4882a593Smuzhiyunbus's directory. In this directory, the driver can export an interface 135*4882a593Smuzhiyunto userspace to control operation of the driver on a global basis; 136*4882a593Smuzhiyune.g. toggling debugging output in the driver. 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunA future feature of this directory will be a 'devices' directory. This 139*4882a593Smuzhiyundirectory will contain symlinks to the directories of devices it 140*4882a593Smuzhiyunsupports. 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun 144*4882a593SmuzhiyunCallbacks 145*4882a593Smuzhiyun~~~~~~~~~ 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun:: 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun int (*probe) (struct device *dev); 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunThe probe() entry is called in task context, with the bus's rwsem locked 152*4882a593Smuzhiyunand the driver partially bound to the device. Drivers commonly use 153*4882a593Smuzhiyuncontainer_of() to convert "dev" to a bus-specific type, both in probe() 154*4882a593Smuzhiyunand other routines. That type often provides device resource data, such 155*4882a593Smuzhiyunas pci_dev.resource[] or platform_device.resources, which is used in 156*4882a593Smuzhiyunaddition to dev->platform_data to initialize the driver. 157*4882a593Smuzhiyun 158*4882a593SmuzhiyunThis callback holds the driver-specific logic to bind the driver to a 159*4882a593Smuzhiyungiven device. That includes verifying that the device is present, that 160*4882a593Smuzhiyunit's a version the driver can handle, that driver data structures can 161*4882a593Smuzhiyunbe allocated and initialized, and that any hardware can be initialized. 162*4882a593SmuzhiyunDrivers often store a pointer to their state with dev_set_drvdata(). 163*4882a593SmuzhiyunWhen the driver has successfully bound itself to that device, then probe() 164*4882a593Smuzhiyunreturns zero and the driver model code will finish its part of binding 165*4882a593Smuzhiyunthe driver to that device. 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunA driver's probe() may return a negative errno value to indicate that 168*4882a593Smuzhiyunthe driver did not bind to this device, in which case it should have 169*4882a593Smuzhiyunreleased all resources it allocated. 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunOptionally, probe() may return -EPROBE_DEFER if the driver depends on 172*4882a593Smuzhiyunresources that are not yet available (e.g., supplied by a driver that 173*4882a593Smuzhiyunhasn't initialized yet). The driver core will put the device onto the 174*4882a593Smuzhiyundeferred probe list and will try to call it again later. If a driver 175*4882a593Smuzhiyunmust defer, it should return -EPROBE_DEFER as early as possible to 176*4882a593Smuzhiyunreduce the amount of time spent on setup work that will need to be 177*4882a593Smuzhiyununwound and reexecuted at a later time. 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun.. warning:: 180*4882a593Smuzhiyun -EPROBE_DEFER must not be returned if probe() has already created 181*4882a593Smuzhiyun child devices, even if those child devices are removed again 182*4882a593Smuzhiyun in a cleanup path. If -EPROBE_DEFER is returned after a child 183*4882a593Smuzhiyun device has been registered, it may result in an infinite loop of 184*4882a593Smuzhiyun .probe() calls to the same driver. 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun:: 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun void (*sync_state) (struct device *dev); 189*4882a593Smuzhiyun 190*4882a593Smuzhiyunsync_state is called only once for a device. It's called when all the consumer 191*4882a593Smuzhiyundevices of the device have successfully probed. The list of consumers of the 192*4882a593Smuzhiyundevice is obtained by looking at the device links connecting that device to its 193*4882a593Smuzhiyunconsumer devices. 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunThe first attempt to call sync_state() is made during late_initcall_sync() to 196*4882a593Smuzhiyungive firmware and drivers time to link devices to each other. During the first 197*4882a593Smuzhiyunattempt at calling sync_state(), if all the consumers of the device at that 198*4882a593Smuzhiyunpoint in time have already probed successfully, sync_state() is called right 199*4882a593Smuzhiyunaway. If there are no consumers of the device during the first attempt, that 200*4882a593Smuzhiyuntoo is considered as "all consumers of the device have probed" and sync_state() 201*4882a593Smuzhiyunis called right away. 202*4882a593Smuzhiyun 203*4882a593SmuzhiyunIf during the first attempt at calling sync_state() for a device, there are 204*4882a593Smuzhiyunstill consumers that haven't probed successfully, the sync_state() call is 205*4882a593Smuzhiyunpostponed and reattempted in the future only when one or more consumers of the 206*4882a593Smuzhiyundevice probe successfully. If during the reattempt, the driver core finds that 207*4882a593Smuzhiyunthere are one or more consumers of the device that haven't probed yet, then 208*4882a593Smuzhiyunsync_state() call is postponed again. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunA typical use case for sync_state() is to have the kernel cleanly take over 211*4882a593Smuzhiyunmanagement of devices from the bootloader. For example, if a device is left on 212*4882a593Smuzhiyunand at a particular hardware configuration by the bootloader, the device's 213*4882a593Smuzhiyundriver might need to keep the device in the boot configuration until all the 214*4882a593Smuzhiyunconsumers of the device have probed. Once all the consumers of the device have 215*4882a593Smuzhiyunprobed, the device's driver can synchronize the hardware state of the device to 216*4882a593Smuzhiyunmatch the aggregated software state requested by all the consumers. Hence the 217*4882a593Smuzhiyunname sync_state(). 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunWhile obvious examples of resources that can benefit from sync_state() include 220*4882a593Smuzhiyunresources such as regulator, sync_state() can also be useful for complex 221*4882a593Smuzhiyunresources like IOMMUs. For example, IOMMUs with multiple consumers (devices 222*4882a593Smuzhiyunwhose addresses are remapped by the IOMMU) might need to keep their mappings 223*4882a593Smuzhiyunfixed at (or additive to) the boot configuration until all its consumers have 224*4882a593Smuzhiyunprobed. 225*4882a593Smuzhiyun 226*4882a593SmuzhiyunWhile the typical use case for sync_state() is to have the kernel cleanly take 227*4882a593Smuzhiyunover management of devices from the bootloader, the usage of sync_state() is 228*4882a593Smuzhiyunnot restricted to that. Use it whenever it makes sense to take an action after 229*4882a593Smuzhiyunall the consumers of a device have probed:: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun int (*remove) (struct device *dev); 232*4882a593Smuzhiyun 233*4882a593Smuzhiyunremove is called to unbind a driver from a device. This may be 234*4882a593Smuzhiyuncalled if a device is physically removed from the system, if the 235*4882a593Smuzhiyundriver module is being unloaded, during a reboot sequence, or 236*4882a593Smuzhiyunin other cases. 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunIt is up to the driver to determine if the device is present or 239*4882a593Smuzhiyunnot. It should free any resources allocated specifically for the 240*4882a593Smuzhiyundevice; i.e. anything in the device's driver_data field. 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunIf the device is still present, it should quiesce the device and place 243*4882a593Smuzhiyunit into a supported low-power state. 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun:: 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun int (*suspend) (struct device *dev, pm_message_t state); 248*4882a593Smuzhiyun 249*4882a593Smuzhiyunsuspend is called to put the device in a low power state. 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun:: 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun int (*resume) (struct device *dev); 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunResume is used to bring a device back from a low power state. 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun 258*4882a593SmuzhiyunAttributes 259*4882a593Smuzhiyun~~~~~~~~~~ 260*4882a593Smuzhiyun 261*4882a593Smuzhiyun:: 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun struct driver_attribute { 264*4882a593Smuzhiyun struct attribute attr; 265*4882a593Smuzhiyun ssize_t (*show)(struct device_driver *driver, char *buf); 266*4882a593Smuzhiyun ssize_t (*store)(struct device_driver *, const char *buf, size_t count); 267*4882a593Smuzhiyun }; 268*4882a593Smuzhiyun 269*4882a593SmuzhiyunDevice drivers can export attributes via their sysfs directories. 270*4882a593SmuzhiyunDrivers can declare attributes using a DRIVER_ATTR_RW and DRIVER_ATTR_RO 271*4882a593Smuzhiyunmacro that works identically to the DEVICE_ATTR_RW and DEVICE_ATTR_RO 272*4882a593Smuzhiyunmacros. 273*4882a593Smuzhiyun 274*4882a593SmuzhiyunExample:: 275*4882a593Smuzhiyun 276*4882a593Smuzhiyun DRIVER_ATTR_RW(debug); 277*4882a593Smuzhiyun 278*4882a593SmuzhiyunThis is equivalent to declaring:: 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun struct driver_attribute driver_attr_debug; 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunThis can then be used to add and remove the attribute from the 283*4882a593Smuzhiyundriver's directory using:: 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun int driver_create_file(struct device_driver *, const struct driver_attribute *); 286*4882a593Smuzhiyun void driver_remove_file(struct device_driver *, const struct driver_attribute *); 287