omer
omer

Reputation: 1440

Initializing kernel module variables

I'm new to kernel and driver programming, so i hope my question is not too simple.

I'm working with a madwifi driver, in order to add some functionalities of my own. In my code i added some variables and structures that need to be initialized before the actual code starts.
While working i have encountered the following question: where is the best place to put the functions that in charge of initializing this variables/structures? As far as i know, there is a special macro *module_init* which is being executed upon loading the module to the kernel, however, i could not find it in the madwifi driver code. What i have found instead is another famous macro, the *exit_module* though. so my questions are:

  1. Is it recommended to add an init_module and do all my initializations there?
  2. Is it recommended to use the exit_module to free the allocated memory?

Thanks for the help!

Omer

Upvotes: 2

Views: 2900

Answers (2)

Narain
Narain

Reputation: 5421

Every module (driver) defines two functions, one to be invoked when the module is loaded into the kernel and one for when the module is removed. module_init() and module_exit() are the two special kernel macros to declare two functions for these roles.

I suppose your driver has init function. init() functions are generally used to initialize or register your driver.

Also check for the probe() function. If your driver can support multiple devices, once driver is registered, kernel calls probe() once for each device. This probe function starts the per-device initialization: initializing hardware, allocating resources, and registering the device with the kernel as a block or network device or whatever it is.

Upvotes: 5

ugoren
ugoren

Reputation: 16441

As I said in my comment, the initialization code can be in the init_module function.

Regarding your questions:

  1. The module initialization function (init_module) is the right place for driver-level initialization. It's recommended to use it, unless your needs are trivial enough for C static variable initialization.
  2. The cleanup function (cleanup_module) must make sure that the driver has released any resource it has allocated. It's the right place to free anything allocated during initialization.

Upvotes: 2

Related Questions