1fe34c89dSMauro Carvalho Chehab============== 2fe34c89dSMauro Carvalho ChehabDevice Drivers 3fe34c89dSMauro Carvalho Chehab============== 4fe34c89dSMauro Carvalho Chehab 5fe34c89dSMauro Carvalho ChehabSee the kerneldoc for the struct device_driver. 6fe34c89dSMauro Carvalho Chehab 7fe34c89dSMauro Carvalho ChehabAllocation 8fe34c89dSMauro Carvalho Chehab~~~~~~~~~~ 9fe34c89dSMauro Carvalho Chehab 10fe34c89dSMauro Carvalho ChehabDevice drivers are statically allocated structures. Though there may 11fe34c89dSMauro Carvalho Chehabbe multiple devices in a system that a driver supports, struct 12fe34c89dSMauro Carvalho Chehabdevice_driver represents the driver as a whole (not a particular 13fe34c89dSMauro Carvalho Chehabdevice instance). 14fe34c89dSMauro Carvalho Chehab 15fe34c89dSMauro Carvalho ChehabInitialization 16fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~~~ 17fe34c89dSMauro Carvalho Chehab 18fe34c89dSMauro Carvalho ChehabThe driver must initialize at least the name and bus fields. It should 19fe34c89dSMauro Carvalho Chehabalso initialize the devclass field (when it arrives), so it may obtain 20fe34c89dSMauro Carvalho Chehabthe proper linkage internally. It should also initialize as many of 21fe34c89dSMauro Carvalho Chehabthe callbacks as possible, though each is optional. 22fe34c89dSMauro Carvalho Chehab 23fe34c89dSMauro Carvalho ChehabDeclaration 24fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~ 25fe34c89dSMauro Carvalho Chehab 26fe34c89dSMauro Carvalho ChehabAs stated above, struct device_driver objects are statically 27fe34c89dSMauro Carvalho Chehaballocated. Below is an example declaration of the eepro100 28fe34c89dSMauro Carvalho Chehabdriver. This declaration is hypothetical only; it relies on the driver 29fe34c89dSMauro Carvalho Chehabbeing converted completely to the new model:: 30fe34c89dSMauro Carvalho Chehab 31fe34c89dSMauro Carvalho Chehab static struct device_driver eepro100_driver = { 32fe34c89dSMauro Carvalho Chehab .name = "eepro100", 33fe34c89dSMauro Carvalho Chehab .bus = &pci_bus_type, 34fe34c89dSMauro Carvalho Chehab 35fe34c89dSMauro Carvalho Chehab .probe = eepro100_probe, 36fe34c89dSMauro Carvalho Chehab .remove = eepro100_remove, 37fe34c89dSMauro Carvalho Chehab .suspend = eepro100_suspend, 38fe34c89dSMauro Carvalho Chehab .resume = eepro100_resume, 39fe34c89dSMauro Carvalho Chehab }; 40fe34c89dSMauro Carvalho Chehab 41fe34c89dSMauro Carvalho ChehabMost drivers will not be able to be converted completely to the new 42fe34c89dSMauro Carvalho Chehabmodel because the bus they belong to has a bus-specific structure with 43fe34c89dSMauro Carvalho Chehabbus-specific fields that cannot be generalized. 44fe34c89dSMauro Carvalho Chehab 45fe34c89dSMauro Carvalho ChehabThe most common example of this are device ID structures. A driver 46fe34c89dSMauro Carvalho Chehabtypically defines an array of device IDs that it supports. The format 47fe34c89dSMauro Carvalho Chehabof these structures and the semantics for comparing device IDs are 48fe34c89dSMauro Carvalho Chehabcompletely bus-specific. Defining them as bus-specific entities would 49fe34c89dSMauro Carvalho Chehabsacrifice type-safety, so we keep bus-specific structures around. 50fe34c89dSMauro Carvalho Chehab 51fe34c89dSMauro Carvalho ChehabBus-specific drivers should include a generic struct device_driver in 52fe34c89dSMauro Carvalho Chehabthe definition of the bus-specific driver. Like this:: 53fe34c89dSMauro Carvalho Chehab 54fe34c89dSMauro Carvalho Chehab struct pci_driver { 55fe34c89dSMauro Carvalho Chehab const struct pci_device_id *id_table; 56fe34c89dSMauro Carvalho Chehab struct device_driver driver; 57fe34c89dSMauro Carvalho Chehab }; 58fe34c89dSMauro Carvalho Chehab 59fe34c89dSMauro Carvalho ChehabA definition that included bus-specific fields would look like 60fe34c89dSMauro Carvalho Chehab(using the eepro100 driver again):: 61fe34c89dSMauro Carvalho Chehab 62fe34c89dSMauro Carvalho Chehab static struct pci_driver eepro100_driver = { 63fe34c89dSMauro Carvalho Chehab .id_table = eepro100_pci_tbl, 64fe34c89dSMauro Carvalho Chehab .driver = { 65fe34c89dSMauro Carvalho Chehab .name = "eepro100", 66fe34c89dSMauro Carvalho Chehab .bus = &pci_bus_type, 67fe34c89dSMauro Carvalho Chehab .probe = eepro100_probe, 68fe34c89dSMauro Carvalho Chehab .remove = eepro100_remove, 69fe34c89dSMauro Carvalho Chehab .suspend = eepro100_suspend, 70fe34c89dSMauro Carvalho Chehab .resume = eepro100_resume, 71fe34c89dSMauro Carvalho Chehab }, 72fe34c89dSMauro Carvalho Chehab }; 73fe34c89dSMauro Carvalho Chehab 74fe34c89dSMauro Carvalho ChehabSome may find the syntax of embedded struct initialization awkward or 75fe34c89dSMauro Carvalho Chehabeven a bit ugly. So far, it's the best way we've found to do what we want... 76fe34c89dSMauro Carvalho Chehab 77fe34c89dSMauro Carvalho ChehabRegistration 78fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~ 79fe34c89dSMauro Carvalho Chehab 80fe34c89dSMauro Carvalho Chehab:: 81fe34c89dSMauro Carvalho Chehab 82fe34c89dSMauro Carvalho Chehab int driver_register(struct device_driver *drv); 83fe34c89dSMauro Carvalho Chehab 84fe34c89dSMauro Carvalho ChehabThe driver registers the structure on startup. For drivers that have 85fe34c89dSMauro Carvalho Chehabno bus-specific fields (i.e. don't have a bus-specific driver 86fe34c89dSMauro Carvalho Chehabstructure), they would use driver_register and pass a pointer to their 87fe34c89dSMauro Carvalho Chehabstruct device_driver object. 88fe34c89dSMauro Carvalho Chehab 89fe34c89dSMauro Carvalho ChehabMost drivers, however, will have a bus-specific structure and will 90fe34c89dSMauro Carvalho Chehabneed to register with the bus using something like pci_driver_register. 91fe34c89dSMauro Carvalho Chehab 92fe34c89dSMauro Carvalho ChehabIt is important that drivers register their driver structure as early as 93fe34c89dSMauro Carvalho Chehabpossible. Registration with the core initializes several fields in the 94fe34c89dSMauro Carvalho Chehabstruct device_driver object, including the reference count and the 95fe34c89dSMauro Carvalho Chehablock. These fields are assumed to be valid at all times and may be 96fe34c89dSMauro Carvalho Chehabused by the device model core or the bus driver. 97fe34c89dSMauro Carvalho Chehab 98fe34c89dSMauro Carvalho Chehab 99fe34c89dSMauro Carvalho ChehabTransition Bus Drivers 100fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~ 101fe34c89dSMauro Carvalho Chehab 102fe34c89dSMauro Carvalho ChehabBy defining wrapper functions, the transition to the new model can be 103fe34c89dSMauro Carvalho Chehabmade easier. Drivers can ignore the generic structure altogether and 104fe34c89dSMauro Carvalho Chehablet the bus wrapper fill in the fields. For the callbacks, the bus can 105fe34c89dSMauro Carvalho Chehabdefine generic callbacks that forward the call to the bus-specific 106fe34c89dSMauro Carvalho Chehabcallbacks of the drivers. 107fe34c89dSMauro Carvalho Chehab 108fe34c89dSMauro Carvalho ChehabThis solution is intended to be only temporary. In order to get class 109fe34c89dSMauro Carvalho Chehabinformation in the driver, the drivers must be modified anyway. Since 110fe34c89dSMauro Carvalho Chehabconverting drivers to the new model should reduce some infrastructural 111fe34c89dSMauro Carvalho Chehabcomplexity and code size, it is recommended that they are converted as 112fe34c89dSMauro Carvalho Chehabclass information is added. 113fe34c89dSMauro Carvalho Chehab 114fe34c89dSMauro Carvalho ChehabAccess 115fe34c89dSMauro Carvalho Chehab~~~~~~ 116fe34c89dSMauro Carvalho Chehab 117fe34c89dSMauro Carvalho ChehabOnce the object has been registered, it may access the common fields of 118fe34c89dSMauro Carvalho Chehabthe object, like the lock and the list of devices:: 119fe34c89dSMauro Carvalho Chehab 120fe34c89dSMauro Carvalho Chehab int driver_for_each_dev(struct device_driver *drv, void *data, 121fe34c89dSMauro Carvalho Chehab int (*callback)(struct device *dev, void *data)); 122fe34c89dSMauro Carvalho Chehab 123fe34c89dSMauro Carvalho ChehabThe devices field is a list of all the devices that have been bound to 124fe34c89dSMauro Carvalho Chehabthe driver. The LDM core provides a helper function to operate on all 125fe34c89dSMauro Carvalho Chehabthe devices a driver controls. This helper locks the driver on each 126fe34c89dSMauro Carvalho Chehabnode access, and does proper reference counting on each device as it 127fe34c89dSMauro Carvalho Chehabaccesses it. 128fe34c89dSMauro Carvalho Chehab 129fe34c89dSMauro Carvalho Chehab 130fe34c89dSMauro Carvalho Chehabsysfs 131fe34c89dSMauro Carvalho Chehab~~~~~ 132fe34c89dSMauro Carvalho Chehab 133fe34c89dSMauro Carvalho ChehabWhen a driver is registered, a sysfs directory is created in its 134fe34c89dSMauro Carvalho Chehabbus's directory. In this directory, the driver can export an interface 135fe34c89dSMauro Carvalho Chehabto userspace to control operation of the driver on a global basis; 136fe34c89dSMauro Carvalho Chehabe.g. toggling debugging output in the driver. 137fe34c89dSMauro Carvalho Chehab 138fe34c89dSMauro Carvalho ChehabA future feature of this directory will be a 'devices' directory. This 139fe34c89dSMauro Carvalho Chehabdirectory will contain symlinks to the directories of devices it 140fe34c89dSMauro Carvalho Chehabsupports. 141fe34c89dSMauro Carvalho Chehab 142fe34c89dSMauro Carvalho Chehab 143fe34c89dSMauro Carvalho Chehab 144fe34c89dSMauro Carvalho ChehabCallbacks 145fe34c89dSMauro Carvalho Chehab~~~~~~~~~ 146fe34c89dSMauro Carvalho Chehab 147fe34c89dSMauro Carvalho Chehab:: 148fe34c89dSMauro Carvalho Chehab 149fe34c89dSMauro Carvalho Chehab int (*probe) (struct device *dev); 150fe34c89dSMauro Carvalho Chehab 151fe34c89dSMauro Carvalho ChehabThe probe() entry is called in task context, with the bus's rwsem locked 152fe34c89dSMauro Carvalho Chehaband the driver partially bound to the device. Drivers commonly use 153fe34c89dSMauro Carvalho Chehabcontainer_of() to convert "dev" to a bus-specific type, both in probe() 154fe34c89dSMauro Carvalho Chehaband other routines. That type often provides device resource data, such 155fe34c89dSMauro Carvalho Chehabas pci_dev.resource[] or platform_device.resources, which is used in 156fe34c89dSMauro Carvalho Chehabaddition to dev->platform_data to initialize the driver. 157fe34c89dSMauro Carvalho Chehab 158fe34c89dSMauro Carvalho ChehabThis callback holds the driver-specific logic to bind the driver to a 159fe34c89dSMauro Carvalho Chehabgiven device. That includes verifying that the device is present, that 160fe34c89dSMauro Carvalho Chehabit's a version the driver can handle, that driver data structures can 161fe34c89dSMauro Carvalho Chehabbe allocated and initialized, and that any hardware can be initialized. 162fe34c89dSMauro Carvalho ChehabDrivers often store a pointer to their state with dev_set_drvdata(). 163fe34c89dSMauro Carvalho ChehabWhen the driver has successfully bound itself to that device, then probe() 164fe34c89dSMauro Carvalho Chehabreturns zero and the driver model code will finish its part of binding 165fe34c89dSMauro Carvalho Chehabthe driver to that device. 166fe34c89dSMauro Carvalho Chehab 167fe34c89dSMauro Carvalho ChehabA driver's probe() may return a negative errno value to indicate that 168fe34c89dSMauro Carvalho Chehabthe driver did not bind to this device, in which case it should have 169*fbc35b45SGrant Likelyreleased all resources it allocated. 170*fbc35b45SGrant Likely 171*fbc35b45SGrant LikelyOptionally, probe() may return -EPROBE_DEFER if the driver depends on 172*fbc35b45SGrant Likelyresources that are not yet available (e.g., supplied by a driver that 173*fbc35b45SGrant Likelyhasn't initialized yet). The driver core will put the device onto the 174*fbc35b45SGrant Likelydeferred probe list and will try to call it again later. If a driver 175*fbc35b45SGrant Likelymust defer, it should return -EPROBE_DEFER as early as possible to 176*fbc35b45SGrant Likelyreduce the amount of time spent on setup work that will need to be 177*fbc35b45SGrant Likelyunwound and reexecuted at a later time. 178*fbc35b45SGrant Likely 179*fbc35b45SGrant Likely.. warning:: 180*fbc35b45SGrant Likely -EPROBE_DEFER must not be returned if probe() has already created 181*fbc35b45SGrant Likely child devices, even if those child devices are removed again 182*fbc35b45SGrant Likely in a cleanup path. If -EPROBE_DEFER is returned after a child 183*fbc35b45SGrant Likely device has been registered, it may result in an infinite loop of 184*fbc35b45SGrant Likely .probe() calls to the same driver. 185*fbc35b45SGrant Likely 186*fbc35b45SGrant Likely:: 187fe34c89dSMauro Carvalho Chehab 188a3caeb8fSSaravana Kannan void (*sync_state) (struct device *dev); 189a3caeb8fSSaravana Kannan 190a3caeb8fSSaravana Kannansync_state is called only once for a device. It's called when all the consumer 191a3caeb8fSSaravana Kannandevices of the device have successfully probed. The list of consumers of the 192a3caeb8fSSaravana Kannandevice is obtained by looking at the device links connecting that device to its 193a3caeb8fSSaravana Kannanconsumer devices. 194a3caeb8fSSaravana Kannan 195a3caeb8fSSaravana KannanThe first attempt to call sync_state() is made during late_initcall_sync() to 196a3caeb8fSSaravana Kannangive firmware and drivers time to link devices to each other. During the first 197a3caeb8fSSaravana Kannanattempt at calling sync_state(), if all the consumers of the device at that 198a3caeb8fSSaravana Kannanpoint in time have already probed successfully, sync_state() is called right 199a3caeb8fSSaravana Kannanaway. If there are no consumers of the device during the first attempt, that 200a3caeb8fSSaravana Kannantoo is considered as "all consumers of the device have probed" and sync_state() 201a3caeb8fSSaravana Kannanis called right away. 202a3caeb8fSSaravana Kannan 203a3caeb8fSSaravana KannanIf during the first attempt at calling sync_state() for a device, there are 204a3caeb8fSSaravana Kannanstill consumers that haven't probed successfully, the sync_state() call is 205a3caeb8fSSaravana Kannanpostponed and reattempted in the future only when one or more consumers of the 206a3caeb8fSSaravana Kannandevice probe successfully. If during the reattempt, the driver core finds that 207a3caeb8fSSaravana Kannanthere are one or more consumers of the device that haven't probed yet, then 208a3caeb8fSSaravana Kannansync_state() call is postponed again. 209a3caeb8fSSaravana Kannan 210a3caeb8fSSaravana KannanA typical use case for sync_state() is to have the kernel cleanly take over 211a3caeb8fSSaravana Kannanmanagement of devices from the bootloader. For example, if a device is left on 212a3caeb8fSSaravana Kannanand at a particular hardware configuration by the bootloader, the device's 213a3caeb8fSSaravana Kannandriver might need to keep the device in the boot configuration until all the 214a3caeb8fSSaravana Kannanconsumers of the device have probed. Once all the consumers of the device have 215a3caeb8fSSaravana Kannanprobed, the device's driver can synchronize the hardware state of the device to 216a3caeb8fSSaravana Kannanmatch the aggregated software state requested by all the consumers. Hence the 217a3caeb8fSSaravana Kannanname sync_state(). 218a3caeb8fSSaravana Kannan 219a3caeb8fSSaravana KannanWhile obvious examples of resources that can benefit from sync_state() include 220a3caeb8fSSaravana Kannanresources such as regulator, sync_state() can also be useful for complex 221a3caeb8fSSaravana Kannanresources like IOMMUs. For example, IOMMUs with multiple consumers (devices 222a3caeb8fSSaravana Kannanwhose addresses are remapped by the IOMMU) might need to keep their mappings 223a3caeb8fSSaravana Kannanfixed at (or additive to) the boot configuration until all its consumers have 224a3caeb8fSSaravana Kannanprobed. 225a3caeb8fSSaravana Kannan 226a3caeb8fSSaravana KannanWhile the typical use case for sync_state() is to have the kernel cleanly take 227a3caeb8fSSaravana Kannanover management of devices from the bootloader, the usage of sync_state() is 228a3caeb8fSSaravana Kannannot restricted to that. Use it whenever it makes sense to take an action after 22999d1a38aSMauro Carvalho Chehaball the consumers of a device have probed:: 230a3caeb8fSSaravana Kannan 231fe34c89dSMauro Carvalho Chehab int (*remove) (struct device *dev); 232fe34c89dSMauro Carvalho Chehab 233fe34c89dSMauro Carvalho Chehabremove is called to unbind a driver from a device. This may be 234fe34c89dSMauro Carvalho Chehabcalled if a device is physically removed from the system, if the 235fe34c89dSMauro Carvalho Chehabdriver module is being unloaded, during a reboot sequence, or 236fe34c89dSMauro Carvalho Chehabin other cases. 237fe34c89dSMauro Carvalho Chehab 238fe34c89dSMauro Carvalho ChehabIt is up to the driver to determine if the device is present or 239fe34c89dSMauro Carvalho Chehabnot. It should free any resources allocated specifically for the 240fe34c89dSMauro Carvalho Chehabdevice; i.e. anything in the device's driver_data field. 241fe34c89dSMauro Carvalho Chehab 242fe34c89dSMauro Carvalho ChehabIf the device is still present, it should quiesce the device and place 243*fbc35b45SGrant Likelyit into a supported low-power state. 244*fbc35b45SGrant Likely 245*fbc35b45SGrant Likely:: 246fe34c89dSMauro Carvalho Chehab 247fe34c89dSMauro Carvalho Chehab int (*suspend) (struct device *dev, pm_message_t state); 248fe34c89dSMauro Carvalho Chehab 249*fbc35b45SGrant Likelysuspend is called to put the device in a low power state. 250*fbc35b45SGrant Likely 251*fbc35b45SGrant Likely:: 252fe34c89dSMauro Carvalho Chehab 253fe34c89dSMauro Carvalho Chehab int (*resume) (struct device *dev); 254fe34c89dSMauro Carvalho Chehab 255fe34c89dSMauro Carvalho ChehabResume is used to bring a device back from a low power state. 256fe34c89dSMauro Carvalho Chehab 257fe34c89dSMauro Carvalho Chehab 258fe34c89dSMauro Carvalho ChehabAttributes 259fe34c89dSMauro Carvalho Chehab~~~~~~~~~~ 260fe34c89dSMauro Carvalho Chehab 261fe34c89dSMauro Carvalho Chehab:: 262fe34c89dSMauro Carvalho Chehab 263fe34c89dSMauro Carvalho Chehab struct driver_attribute { 264fe34c89dSMauro Carvalho Chehab struct attribute attr; 265fe34c89dSMauro Carvalho Chehab ssize_t (*show)(struct device_driver *driver, char *buf); 266fe34c89dSMauro Carvalho Chehab ssize_t (*store)(struct device_driver *, const char *buf, size_t count); 267fe34c89dSMauro Carvalho Chehab }; 268fe34c89dSMauro Carvalho Chehab 269fe34c89dSMauro Carvalho ChehabDevice drivers can export attributes via their sysfs directories. 270fe34c89dSMauro Carvalho ChehabDrivers can declare attributes using a DRIVER_ATTR_RW and DRIVER_ATTR_RO 271fe34c89dSMauro Carvalho Chehabmacro that works identically to the DEVICE_ATTR_RW and DEVICE_ATTR_RO 272fe34c89dSMauro Carvalho Chehabmacros. 273fe34c89dSMauro Carvalho Chehab 274fe34c89dSMauro Carvalho ChehabExample:: 275fe34c89dSMauro Carvalho Chehab 276fe34c89dSMauro Carvalho Chehab DRIVER_ATTR_RW(debug); 277fe34c89dSMauro Carvalho Chehab 278fe34c89dSMauro Carvalho ChehabThis is equivalent to declaring:: 279fe34c89dSMauro Carvalho Chehab 280fe34c89dSMauro Carvalho Chehab struct driver_attribute driver_attr_debug; 281fe34c89dSMauro Carvalho Chehab 282fe34c89dSMauro Carvalho ChehabThis can then be used to add and remove the attribute from the 283fe34c89dSMauro Carvalho Chehabdriver's directory using:: 284fe34c89dSMauro Carvalho Chehab 285fe34c89dSMauro Carvalho Chehab int driver_create_file(struct device_driver *, const struct driver_attribute *); 286fe34c89dSMauro Carvalho Chehab void driver_remove_file(struct device_driver *, const struct driver_attribute *); 287