Module Parameters

Several parameters that a driver needs to know can change from system to system. These can vary from the device number to use (as we'll see in the next chapter) to numerous aspects of how the driver should operate. For example, drivers for SCSI adapters often have options controlling the use of tagged command queuing, and the Integrated Device Electronics (IDE) drivers allow user control of DMA operations. If your driver controls older hardware, it may also need to be told explicitly where to find that hardware's I/O ports or I/O memory addresses. The kernel supports these needs by making it possible for a driver to designate parameters that may be changed when the driver's module is loaded. 驱动程序需要知道的几个参数可能会因系统而异。 这些可以从要使用的设备编号(我们将在下一章中看到)到驱动程序应该如何操作的许多方面有所不同。 例如,SCSI 适配器的驱动程序通常具有控制标记命令队列使用的选项,而集成设备电子 (IDE) 驱动程序允许用户控制 DMA 操作。 如果您的驱动程序控制较旧的硬件,则可能还需要明确告知在哪里可以找到该硬件的 I/O 端口或 I/O 内存地址。 内核通过使驱动程序可以指定在加载驱动程序模块时可能更改的参数来支持这些需求。

These parameter values can be assigned at load time by insmod or modprobe ; the latter can also read parameter assignment from its configuration file (/etc/modprobe.conf ). The commands accept the specification of several types of values on the command line. As a way of demonstrating this capability, imagine a much-needed enhancement to the "hello world" module (called hellop) shown at the beginning of this chapter. We add two parameters: an integer value called howmany and a character string called whom. Our vastly more functional module then, at load time, greets whom not just once, but howmany times. Such a module could then be loaded with a command line such as: 这些参数值可以在加载时由 insmod 或 modprobe 分配; 后者还可以从其配置文件(/etc/modprobe.conf)中读取参数分配。 这些命令接受命令行上几种类型的值的规范。 作为演示这种能力的一种方式,想象一下对本章开头所示的“hello world”模块(称为 hellop)进行了急需的增强。 我们添加了两个参数:一个整数值,称为 howmany 和一个字符串,称为 whom。 然后,我们功能强大得多的模块,在加载时,不是向whom打招呼1次,而是向whom打招呼howmany次。 然后可以使用命令行加载这样的模块,例如:

insmod hellop howmany=10 whom="Mom"

Upon being loaded that way, hellop would say "Hello, Mom" 10 times. 以这种方式加载后,hellop 会说“你好,妈妈”10 次。

However, before insmod can change module parameters, the module must make them available. Parameters are declared with the module_param macro, which is defined in moduleparam.h. module_param takes three parameters: the name of the variable, its type, and a permissions mask to be used for an accompanying sysfs entry. The macro should be placed outside of any function and is typically found near the head of the source file. So hellop would declare its parameters and make them available to insmod as follows: 但是,在 insmod 可以更改模块参数之前,模块必须使它们可用。 参数使用 module_param 宏声明,该宏在 moduleparam.h 中定义。 module_param 接受三个参数:变量的名称、它的类型和用于随附的 sysfs 条目的权限掩码。 宏应放置在任何函数之外,通常位于源文件头部附近。 所以 hellop 会声明它的参数并让它们对 insmod 可用,如下所示:

static char *whom = "world";

static int howmany = 1;

module_param(howmany, int, S_IRUGO);

module_param(whom, charp, S_IRUGO);

Numerous types are supported for module parameters: 模块参数支持多种类型:

bool
invbool

A boolean (true or false) value (the associated variable should be of type int). The invbool type inverts the value, so that true values become false and vice versa. 一个布尔值(true 或 false)(相关变量应该是 int 类型)。 invbool 类型反转值,使真值变为假,反之亦然。

charp

A char pointer value. Memory is allocated for user-provided strings, and the pointer is set accordingly. 一个字符指针值。 为用户提供的字符串分配内存,并相应地设置指针。

int
long
short
uint
ulong
ushort

Basic integer values of various lengths. The versions starting with u are for unsigned values. 各种长度的基本整数值。 以 u 开头的版本用于无符号值。

Array parameters, where the values are supplied as a comma-separated list, are also supported by the module loader. To declare an array parameter, use: 模块加载器也支持数组参数,其中值以逗号分隔的列表形式提供。 要声明数组参数,请使用:

module_param_array(name,type,nump,perm);

Where name is the name of your array (and of the parameter), type is the type of the array elements, nump is an integer variable, and perm is the usual permissions value. If the array parameter is set at load time, nump is set to the number of values supplied. The module loader refuses to accept more values than will fit in the array. 其中 name 是数组(和参数)的名称,type 是数组元素的类型,nump 是整数变量,perm 是通常的权限值。 如果在加载时设置了数组参数,则将 nump 设置为提供的值的数量。 模块加载器拒绝接受超出数组容量的值。

If you really need a type that does not appear in the list above, there are hooks in the module code that allow you to define them; see moduleparam.h for details on how to do that. All module parameters should be given a default value; insmod changes the value only if explicitly told to by the user. The module can check for explicit parameters by testing parameters against their default values. 如果你真的需要上面列表中没有出现的类型,模块代码中有钩子可以让你定义它们; 有关如何执行此操作的详细信息,请参阅 moduleparam.h。 所有模块参数都应该被赋予一个默认值; insmod 仅在用户明确告知时才更改该值。 该模块可以通过根据参数的默认值测试参数来检查显式参数。

The final module_param field is a permission value; you should use the definitions found in <linux/stat.h>. This value controls who can access the representation of the module parameter in sysfs. If perm is set to 0, there is no sysfs entry at all; otherwise, it appears under /sys/module [3] with the given set of permissions. Use S_IRUGO for a parameter that can be read by the world but cannot be changed; S_IRUGO|S_IWUSR allows root to change the parameter. Note that if a parameter is changed by sysfs, the value of that parameter as seen by your module changes, but your module is not notified in any other way. You should probably not make module parameters writable, unless you are prepared to detect the change and react accordingly. 最后的 module_param 字段是一个权限值; 您应该使用 <linux/stat.h> 中的定义。 该值控制谁可以访问 sysfs 中模块参数的表示。 如果 perm 设置为 0,则根本没有 sysfs 条目; 否则,它会出现在具有给定权限集的 /sys/module [3] 下。 使用 S_IRUGO 作为世界可以读取但不能更改的参数; S_IRUGO|S_IWUSR 允许 root 更改参数。 请注意,如果 sysfs 更改了某个参数,则您的模块看到的该参数的值会更改,但不会以任何其他方式通知您的模块。 您可能不应该使模块参数可写,除非您准备好检测更改并做出相应的反应。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mounter625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值