Search code examples
clinuxkernellinux-device-driverdevice-driver

platform devices; why 'alloc' and 'add'?


I'm currently looking at a machine driver in sound/soc/imx. In the initialization function there is, in this order;

platform_driver_register(...);
...
platform_device_alloc(..., ...);
platform_set_drvdata(..., ...);
platform_device_add(...);
...

For the platform device, why is it necessary to do 'alloc' and then 'add'?
platform_device_alloc() means "create a platform device"; and platform_device_add() means "add a platform device to device hierarchy", so I'm just curious to know, why do you need both?


Solution

  • The platform_device_alloc function performs only the memory allocation for the platform_device structure and initialize its variables using the external arguments.

    http://lxr.free-electrons.com/source/drivers/base/platform.c#L197

    The platform_device_add function performs all the tasks required to register the intended device within the device driver hierarchy.

    http://lxr.free-electrons.com/source/drivers/base/platform.c#L277

    The separation of the two functions is intended to allow granule level configurations for the driver developer if required.

    The two operations is combined within the platform_device_register_simple and other related functions, which perform both memory allocation as well as device registration within.