99 lines · plain
1==============2Driver Binding3==============4 5Driver binding is the process of associating a device with a device6driver that can control it. Bus drivers have typically handled this7because there have been bus-specific structures to represent the8devices and the drivers. With generic device and device driver9structures, most of the binding can take place using common code.10 11 12Bus13~~~14 15The bus type structure contains a list of all devices that are on that bus16type in the system. When device_register is called for a device, it is17inserted into the end of this list. The bus object also contains a18list of all drivers of that bus type. When driver_register is called19for a driver, it is inserted at the end of this list. These are the20two events which trigger driver binding.21 22 23device_register24~~~~~~~~~~~~~~~25 26When a new device is added, the bus's list of drivers is iterated over27to find one that supports it. In order to determine that, the device28ID of the device must match one of the device IDs that the driver29supports. The format and semantics for comparing IDs is bus-specific.30Instead of trying to derive a complex state machine and matching31algorithm, it is up to the bus driver to provide a callback to compare32a device against the IDs of a driver. The bus returns 1 if a match was33found; 0 otherwise.34 35int match(struct device * dev, struct device_driver * drv);36 37If a match is found, the device's driver field is set to the driver38and the driver's probe callback is called. This gives the driver a39chance to verify that it really does support the hardware, and that40it's in a working state.41 42Device Class43~~~~~~~~~~~~44 45Upon the successful completion of probe, the device is registered with46the class to which it belongs. Device drivers belong to one and only one47class, and that is set in the driver's devclass field.48devclass_add_device is called to enumerate the device within the class49and actually register it with the class, which happens with the50class's register_dev callback.51 52 53Driver54~~~~~~55 56When a driver is attached to a device, the device is inserted into the57driver's list of devices.58 59 60sysfs61~~~~~62 63A symlink is created in the bus's 'devices' directory that points to64the device's directory in the physical hierarchy.65 66A symlink is created in the driver's 'devices' directory that points67to the device's directory in the physical hierarchy.68 69A directory for the device is created in the class's directory. A70symlink is created in that directory that points to the device's71physical location in the sysfs tree.72 73A symlink can be created (though this isn't done yet) in the device's74physical directory to either its class directory, or the class's75top-level directory. One can also be created to point to its driver's76directory also.77 78 79driver_register80~~~~~~~~~~~~~~~81 82The process is almost identical for when a new driver is added.83The bus's list of devices is iterated over to find a match. Devices84that already have a driver are skipped. All the devices are iterated85over, to bind as many devices as possible to the driver.86 87 88Removal89~~~~~~~90 91When a device is removed, the reference count for it will eventually92go to 0. When it does, the remove callback of the driver is called. It93is removed from the driver's list of devices and the reference count94of the driver is decremented. All symlinks between the two are removed.95 96When a driver is removed, the list of devices that it supports is97iterated over, and the driver's remove callback is called for each98one. The device is removed from that list and the symlinks removed.99