分类: LINUX
2014-12-30 14:39:20
1、 LIST_HEAD_INIT是宏,INIT_LIST_HEAD是函数
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) /
struct list_head name = LIST_HEAD_INIT(name)
//在list前,增加一个Node:item
static inline void list_add_tail(struct list_node *list, struct list_node *item)
//删除Node:item
static inline void list_delete(struct list_node *item)
2、实例:
(1)、全局
struct synaptics_rmi4_exp_fn {
enum exp_fn fn_type;
bool inserted;
int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
unsigned char intr_mask);
struct list_head link;
};
static bool exp_fn_inited;
static struct mutex exp_fn_list_mutex;
static struct list_head exp_fn_list;
(2)、初始化(probe中)
if (!exp_fn_inited) {
mutex_init(&exp_fn_list_mutex);
INIT_LIST_HEAD(&exp_fn_list);
exp_fn_inited = 1;
}
(3)、添加or删除一个list
void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
unsigned char intr_mask))
{
struct synaptics_rmi4_exp_fn *exp_fhandler;
if (!exp_fn_inited) {
mutex_init(&exp_fn_list_mutex);
INIT_LIST_HEAD(&exp_fn_list);
exp_fn_inited = 1;
}
mutex_lock(&exp_fn_list_mutex);
if (insert) {
exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
if (!exp_fhandler) {
pr_err("%s: Failed to alloc mem for expansion function\n",
__func__);
goto exit;
}
exp_fhandler->fn_type = fn_type;
exp_fhandler->func_init = func_init;
exp_fhandler->func_attn = func_attn;
exp_fhandler->func_remove = func_remove;
exp_fhandler->inserted = false;
list_add_tail(&exp_fhandler->link, &exp_fn_list);
} else {
if (!list_empty(&exp_fn_list)) {
list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
if (exp_fhandler->func_init == func_init) {
exp_fhandler->inserted = false;
exp_fhandler->func_init = NULL;
exp_fhandler->func_attn = NULL;
goto exit;
}
}
}
}
exit:
mutex_unlock(&exp_fn_list_mutex);
return;
}
EXPORT_SYMBOL(synaptics_rmi4_new_function);