Книга: Embedded Linux Primer: A Practical, Real-World Approach

8.2.2. Module Parameters

8.2.2. Module Parameters

Many device driver modules can accept parameters to modify their behavior. Examples include enabling debug mode, setting verbose reporting, or specifying module-specific options. The insmod utility accepts parameters (also called options in some contexts) by specifying them after the module name. Listing 8-6 shows our modified hello1.c example, adding a single module parameter to enable debug mode.

Listing 8-6. Example Driver with Parameter

/* Example Minimal Character Device Driver */
#include <linux/module.h>
static int debug_enable = 0; /* Added driver parameter */
module_param(debug_enable, int, 0); /* and these 2 lines */
MODULE_PARM_DESC(debug_enable, "Enable module debug mode.");
static int __init hello_init(void) {
 /* Now print value of new module parameter */
 printk("Hello Example Init - debug mode is %sn", debug_enable ? "enabled" : "disabled");
 return 0;
}
static void __exit hello_exit(void) {
 printk("Hello Example Exitn");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Chris Hallinan");
MODULE_DESCRIPTION("Hello World Example");
MODULE_LICENSE("GPL");

Three lines have been added to our example device driver module. The first declares a static integer to hold our debug flag. The second line is a macro defined in .../include/linux/moduleparam.h that registers the module parameter with the kernel module subsystem. The third new line is a macro that registers a string description associated with the parameter with the kernel module subsystem. The purpose of this will become clear when we examine the modinfo command later in this chapter.

If we now use insmod to insert our example module, and add the debug_enable option, we should see the resulting output, based on our modified hello1.c module in Listing 8-6.

$ insmod /lib/modules/.../examples/hello1.ko debug_enable=1
Hello Example Init - debug mode is enabled

Or, if we omit the optional module parameter:

$ insmod /lib/modules/.../examples/hello1.ko
Hello Example Init - debug mode is disabled

Оглавление книги


Генерация: 0.966. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз