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

omer
  • 1,242
  • 4
  • 18
  • 45

2 Answers2

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.

sawdust
  • 16,103
  • 3
  • 40
  • 50
Narain
  • 4,461
  • 2
  • 17
  • 15
  • 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. – omer Mar 19 '13 at 12:00
  • 1
    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. – sawdust Mar 19 '13 at 19:02
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.
ugoren
  • 16,023
  • 3
  • 35
  • 65
  • 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) – omer Mar 19 '13 at 12:23
  • 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`. – ugoren Mar 19 '13 at 12:44
  • @omer, Thanks for saying "thanks", but the proper way to do it is to upvote or accept the answer. – ugoren Mar 19 '13 at 15:10