linux驱动中宏__setup(str, fn)

(一) 定义如下:

#define __setup(str, fn) \
__setup_param(str, fn, fn, 0)

#define __setup_param(str, unique_id, fn, early) \
static char __setup_str_##unique_id[] __initdata __aligned(1) = str; \
static struct obs_kernel_param __setup_##unique_id \
__used __section(.init.setup) \
__attribute__((aligned((sizeof(long))))) \
= { __setup_str_##unique_id, fn, early }

struct obs_kernel_param {
    const char *str;
    int (*setup_func)(char *);
    int early;
};

(二) 例子:(来源于:net/core/dev.c)

__setup(“netdev=”, netdev_boot_setup);
由(一)可转换为:

__setup_param("netdev=", netdev_boot_setup, netdev_boot_setup, 0);

=====>

#define __setup_param("netdev=", netdev_boot_setup, netdev_boot_setup, 0) \
static char __setup_str_netdev_boot_setup[] __initdata __aligned(1) = "netdev="; \
static struct obs_kernel_param __setup_unique_id \
__used __section(.init.setup) \
__attribute__((aligned((sizeof(long))))) \
= { __setup_str_unique_id, netdev_boot_setup, 0 }

__setup(str, fn)
str是关键字,fn是关联处理函数。__setup只是告诉内核在启动时输入串中含有str时,内核要去执行fn。str必须以“=”符结束以使parse_args更方便解析。紧随“=”后的任何文本都会作为输入传给 fn。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

继续拿__setup(“netdev=”, netdev_boot_setup)举例:
要实现netdev_boot_setup,哪它以什么样的形式出现呢?

struct obs_kernel_param {
    const char *str;
    int (*setup_func)(char *);
    int early;
};

应以这样的形式去实现:

int netdev_boot_setup(char *str)
{
    ...

    retutn 0;
}

当内核启动解析参数时有“netdev”字符串时就会执行netdev_boot_setup函数并将“netdev=”后的字符串当做参数传入。

你可能感兴趣的:(linux驱动,Linux启动)