2

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

2
  • By default, the init_module and cleanup_module functions are called on load/unload. The module_init can replace this by another function. Commented Mar 19, 2013 at 12:06
  • have you looked for __devinit... Commented Mar 19, 2013 at 12:09

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but as i mentioned, i couldn't find the init function in the code.. it seems it just doesn't exist.
A module will always have functions declared in the module_init() and module_exit() macros. But the module may consist of more than one source file, so perhaps you are not looking in the file that has the init routine.
2

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.

3 Comments

I see...So if, for example, i wanted to create an array of buffers that i will be using throughout the whole module, you'd advise me to declare it and allocate memory for it in the init_module? (the same of all other vars\structures that are in use throughout the whole code)
Generally, you should allocate as little as possible, and avoid allocating lots of memory just in case - better wait for the need to arise. But if you do that - then yes, allocate in init_module, free in cleanup_module.
@omer, Thanks for saying "thanks", but the proper way to do it is to upvote or accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.