Thursday, December 1, 2011

Order in which linux device drivers are probed!

The oder in which the device driver are initialized depends on the level of initcall in the driver. The levels range from 0-7.

#define pure_initcall(fn) __define_initcall("0",fn,0)

#define core_initcall(fn) __define_initcall("1",fn,1)
#define core_initcall_sync(fn) __define_initcall("1s",fn,1s)
#define postcore_initcall(fn) __define_initcall("2",fn,2)
#define postcore_initcall_sync(fn) __define_initcall("2s",fn,2s)
#define arch_initcall(fn) __define_initcall("3",fn,3)
#define arch_initcall_sync(fn) __define_initcall("3s",fn,3s)
#define subsys_initcall(fn) __define_initcall("4",fn,4)
#define subsys_initcall_sync(fn) __define_initcall("4s",fn,4s)
#define fs_initcall(fn) __define_initcall("5",fn,5)
#define fs_initcall_sync(fn) __define_initcall("5s",fn,5s)
#define rootfs_initcall(fn) __define_initcall("rootfs",fn,rootfs)
#define device_initcall(fn) __define_initcall("6",fn,6)
#define device_initcall_sync(fn) __define_initcall("6s",fn,6s)
#define late_initcall(fn) __define_initcall("7",fn,7)
#define late_initcall_sync(fn) __define_initcall("7s",fn,7s)

Ref: include/linux/init.h

Below is the issue that I faced.

I have an mfd device and the count of the clients goes upto 14. Now that I am interested in only 3 among them(say x, y and z) and the order that I want them to be probed in is x->y->z.
In my mfd core driver, the order in which I add the devices also suits my requirement, i.e x->y->z.
The level of initcall in all the 3 drivers are same, say subsys_initcall_sync(). Also all the 3 drivers(x, y and z) are in the same folder.

The expected result would be 'x' should be probed first followed by 'y' and 'z'.
But no, the order that I got is x->z->y.

I tried altering the order in which the mfd devices are added but no use. The actual problem was in the Makefile, the order in which linking had happened.
In makefile
obj-$(CONFIG_XXXX) += x.0 z.0 y.0
Here is where the issue was, later I changed the order in makefile and things started working as expected.

So remember the order in which linking happens also matters!

No comments: