1*fe34c89dSMauro Carvalho Chehab============== 2*fe34c89dSMauro Carvalho ChehabDriver Binding 3*fe34c89dSMauro Carvalho Chehab============== 4*fe34c89dSMauro Carvalho Chehab 5*fe34c89dSMauro Carvalho ChehabDriver binding is the process of associating a device with a device 6*fe34c89dSMauro Carvalho Chehabdriver that can control it. Bus drivers have typically handled this 7*fe34c89dSMauro Carvalho Chehabbecause there have been bus-specific structures to represent the 8*fe34c89dSMauro Carvalho Chehabdevices and the drivers. With generic device and device driver 9*fe34c89dSMauro Carvalho Chehabstructures, most of the binding can take place using common code. 10*fe34c89dSMauro Carvalho Chehab 11*fe34c89dSMauro Carvalho Chehab 12*fe34c89dSMauro Carvalho ChehabBus 13*fe34c89dSMauro Carvalho Chehab~~~ 14*fe34c89dSMauro Carvalho Chehab 15*fe34c89dSMauro Carvalho ChehabThe bus type structure contains a list of all devices that are on that bus 16*fe34c89dSMauro Carvalho Chehabtype in the system. When device_register is called for a device, it is 17*fe34c89dSMauro Carvalho Chehabinserted into the end of this list. The bus object also contains a 18*fe34c89dSMauro Carvalho Chehablist of all drivers of that bus type. When driver_register is called 19*fe34c89dSMauro Carvalho Chehabfor a driver, it is inserted at the end of this list. These are the 20*fe34c89dSMauro Carvalho Chehabtwo events which trigger driver binding. 21*fe34c89dSMauro Carvalho Chehab 22*fe34c89dSMauro Carvalho Chehab 23*fe34c89dSMauro Carvalho Chehabdevice_register 24*fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~~~~ 25*fe34c89dSMauro Carvalho Chehab 26*fe34c89dSMauro Carvalho ChehabWhen a new device is added, the bus's list of drivers is iterated over 27*fe34c89dSMauro Carvalho Chehabto find one that supports it. In order to determine that, the device 28*fe34c89dSMauro Carvalho ChehabID of the device must match one of the device IDs that the driver 29*fe34c89dSMauro Carvalho Chehabsupports. The format and semantics for comparing IDs is bus-specific. 30*fe34c89dSMauro Carvalho ChehabInstead of trying to derive a complex state machine and matching 31*fe34c89dSMauro Carvalho Chehabalgorithm, it is up to the bus driver to provide a callback to compare 32*fe34c89dSMauro Carvalho Chehaba device against the IDs of a driver. The bus returns 1 if a match was 33*fe34c89dSMauro Carvalho Chehabfound; 0 otherwise. 34*fe34c89dSMauro Carvalho Chehab 35*fe34c89dSMauro Carvalho Chehabint match(struct device * dev, struct device_driver * drv); 36*fe34c89dSMauro Carvalho Chehab 37*fe34c89dSMauro Carvalho ChehabIf a match is found, the device's driver field is set to the driver 38*fe34c89dSMauro Carvalho Chehaband the driver's probe callback is called. This gives the driver a 39*fe34c89dSMauro Carvalho Chehabchance to verify that it really does support the hardware, and that 40*fe34c89dSMauro Carvalho Chehabit's in a working state. 41*fe34c89dSMauro Carvalho Chehab 42*fe34c89dSMauro Carvalho ChehabDevice Class 43*fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~ 44*fe34c89dSMauro Carvalho Chehab 45*fe34c89dSMauro Carvalho ChehabUpon the successful completion of probe, the device is registered with 46*fe34c89dSMauro Carvalho Chehabthe class to which it belongs. Device drivers belong to one and only one 47*fe34c89dSMauro Carvalho Chehabclass, and that is set in the driver's devclass field. 48*fe34c89dSMauro Carvalho Chehabdevclass_add_device is called to enumerate the device within the class 49*fe34c89dSMauro Carvalho Chehaband actually register it with the class, which happens with the 50*fe34c89dSMauro Carvalho Chehabclass's register_dev callback. 51*fe34c89dSMauro Carvalho Chehab 52*fe34c89dSMauro Carvalho Chehab 53*fe34c89dSMauro Carvalho ChehabDriver 54*fe34c89dSMauro Carvalho Chehab~~~~~~ 55*fe34c89dSMauro Carvalho Chehab 56*fe34c89dSMauro Carvalho ChehabWhen a driver is attached to a device, the device is inserted into the 57*fe34c89dSMauro Carvalho Chehabdriver's list of devices. 58*fe34c89dSMauro Carvalho Chehab 59*fe34c89dSMauro Carvalho Chehab 60*fe34c89dSMauro Carvalho Chehabsysfs 61*fe34c89dSMauro Carvalho Chehab~~~~~ 62*fe34c89dSMauro Carvalho Chehab 63*fe34c89dSMauro Carvalho ChehabA symlink is created in the bus's 'devices' directory that points to 64*fe34c89dSMauro Carvalho Chehabthe device's directory in the physical hierarchy. 65*fe34c89dSMauro Carvalho Chehab 66*fe34c89dSMauro Carvalho ChehabA symlink is created in the driver's 'devices' directory that points 67*fe34c89dSMauro Carvalho Chehabto the device's directory in the physical hierarchy. 68*fe34c89dSMauro Carvalho Chehab 69*fe34c89dSMauro Carvalho ChehabA directory for the device is created in the class's directory. A 70*fe34c89dSMauro Carvalho Chehabsymlink is created in that directory that points to the device's 71*fe34c89dSMauro Carvalho Chehabphysical location in the sysfs tree. 72*fe34c89dSMauro Carvalho Chehab 73*fe34c89dSMauro Carvalho ChehabA symlink can be created (though this isn't done yet) in the device's 74*fe34c89dSMauro Carvalho Chehabphysical directory to either its class directory, or the class's 75*fe34c89dSMauro Carvalho Chehabtop-level directory. One can also be created to point to its driver's 76*fe34c89dSMauro Carvalho Chehabdirectory also. 77*fe34c89dSMauro Carvalho Chehab 78*fe34c89dSMauro Carvalho Chehab 79*fe34c89dSMauro Carvalho Chehabdriver_register 80*fe34c89dSMauro Carvalho Chehab~~~~~~~~~~~~~~~ 81*fe34c89dSMauro Carvalho Chehab 82*fe34c89dSMauro Carvalho ChehabThe process is almost identical for when a new driver is added. 83*fe34c89dSMauro Carvalho ChehabThe bus's list of devices is iterated over to find a match. Devices 84*fe34c89dSMauro Carvalho Chehabthat already have a driver are skipped. All the devices are iterated 85*fe34c89dSMauro Carvalho Chehabover, to bind as many devices as possible to the driver. 86*fe34c89dSMauro Carvalho Chehab 87*fe34c89dSMauro Carvalho Chehab 88*fe34c89dSMauro Carvalho ChehabRemoval 89*fe34c89dSMauro Carvalho Chehab~~~~~~~ 90*fe34c89dSMauro Carvalho Chehab 91*fe34c89dSMauro Carvalho ChehabWhen a device is removed, the reference count for it will eventually 92*fe34c89dSMauro Carvalho Chehabgo to 0. When it does, the remove callback of the driver is called. It 93*fe34c89dSMauro Carvalho Chehabis removed from the driver's list of devices and the reference count 94*fe34c89dSMauro Carvalho Chehabof the driver is decremented. All symlinks between the two are removed. 95*fe34c89dSMauro Carvalho Chehab 96*fe34c89dSMauro Carvalho ChehabWhen a driver is removed, the list of devices that it supports is 97*fe34c89dSMauro Carvalho Chehabiterated over, and the driver's remove callback is called for each 98*fe34c89dSMauro Carvalho Chehabone. The device is removed from that list and the symlinks removed. 99