Hey! While you're here, why not check out my open source endevours?

Using leds-gpio dynamically

In a blog post (Linux LED Subsystem), Fabio Baltieri details using the leds-gpio driver to quickly add GPIO-connected LEDs to a SoC. His method references nslu2-setup.c as an example implementation which utilizes static structures and the platform_add_devices function to add the LED devices. This is fine for an unchanging platform device where the driver will be compiled into the kernel - using static structures makes perfect sense but it does have a major drawback for developers.

If you find yourself developing a board and you want to load and unload a module rather than constantly rebooting while you work on writing parts of the driver you will have a big problem. leds-gpio does not get the opportunity to release (during platform_device_unregister) these statically initialized devices. This means they will be left out in the cold and you cannot reload the module successfully. It makes sense: the underling platform_device_register that platform_add_devices calls is meant for statically initialized devices. Since this memory cannot be freed there is no sense in the release hook being assigned - doing so will only lead to problems.

Rather than statically initializing the devices, they can be created with a combination of platform_device_alloc, platform_device_add_data, and platform_device_add. Within the call to platform_device_alloc, an unregister hook to free the memory is added. This means a later call to platform_device_unregister will actually remove the devices. Thus you can do work in a module that can be cleanly unloaded and loaded again.

Copyright 2021 Daniel M. WeeksLoaded in 0.00147 seconds using 2048 KB
All kinds of people
look all kinds of ways.
You'll never know anything about anybody
just looking all day.