static int bh_event_add_var(struct bh_event *event, int argv,
const char *format, ...)
{
static char buf[128];
char *s;
va_list args;
int len;
if (argv)
return 0;
va_start(args, format); len = vsnprintf(buf, sizeof(buf), format, args);//将要添加的环境变量临时放在buf中,并获取环境变量的长度len
va_end(args);
if (len >= sizeof(buf)) {
BH_ERR("buffer size too small\n");
WARN_ON(1);
return -ENOMEM;
}
s = skb_put(event->skb, len + 1);//往sk_buff缓冲区中继续添加长度len的数据 strcpy(s, buf);//拷贝实际的数据内容到sk_buff缓冲区。
BH_DBG("added variable '%s'\n", s);
return 0;
}
static int button_hotplug_fill_event(struct bh_event *event)
{
int ret;
ret = bh_event_add_var(event, 0, "HOME=%s", "/");//往sk_buff缓冲区中添加环境变量HOME=/.
if (ret)
return ret;
ret = bh_event_add_var(event, 0, "PATH=%s",//往sk_buff缓冲区中添加环境变量PATH=/sbin:/bin:/usr/sbin:/usr/bin.
"/sbin:/bin:/usr/sbin:/usr/bin");
if (ret)
return ret;
ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); if (ret)//往sk_buff缓冲区中添加环境变量SUBSYSTEM=button.
return ret;
ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); if (ret)//往sk_buff缓冲区中添加环境变量ACTION=pressed(released?)--->详见201行。
return ret;
ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
if (ret)
return ret;
ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
if (ret)
return ret;
ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
return ret;
}
static void button_hotplug_work(struct work_struct *work)
{
struct bh_event *event = container_of(work, struct bh_event, work);
int ret = 0;
event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
if (!event->skb)
goto out_free_event;
ret = bh_event_add_var(event, 0, "%s@", event->action);
if (ret)
goto out_free_skb;
ret = button_hotplug_fill_event(event);//先将硬件设备发生过的各种事件添加到环境变量缓冲区SK_BUFF中。
if (ret)
goto out_free_skb;
/* -------------------------------------------------------------------------*/
static int button_get_index(unsigned int code)
{
int i;
for (i = 0; i < ARRAY_SIZE(button_map); i++)
if (button_map[i].code == code)
return i;
return -1;
}
static void button_hotplug_event(struct input_handle *handle,
unsigned int type, unsigned int code, int value)
{ struct bh_priv *priv = handle->private;//handle为input的总线,该结构体保存了各种输入设备 //在上次产生事件时系统的节拍数。 unsigned long seen = jiffies;//系统本次启动至今的节拍数(即systick总数)
int btn;
static int __init button_hotplug_init(void)
{
int ret;
printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
ret = input_register_handler(&button_hotplug_handler);
if (ret)
BH_ERR("unable to register input handler\n");