內核版本:linux-2.6.22.6
輸入子系統源碼位置drivers/input/input.c
input_init()函數中執行了註冊了input_fops
err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
input_fops的內容如下
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file,
};
只有一個open函數
怎麼打開對應設備?
input_open_file()函數
struct input_handler *handler = input_table[iminor(inode) >> 5]; //從input_table數組中根據驅動的次設備號取出一個handler 裏面包含一個 file_operations結構體
new_fops = fops_get(handler->fops) //取得handler中的fops
file->f_op = new_fops;
err = new_fops->open(inode, file);//執行對應驅動的open函數
問:應用程序怎麼讀按鍵?
app: read > ... > file->f_op->read //應用程序read最後會執行到 file->f_op的read函數
input_table數組由誰構造?
在”軟件“驅動程序的init中 使用input_register_handler(&handler) 註冊 handler
註冊input_handler(軟件)
input_register_handler()函數
// 根據次設備號將handler放入數組
input_table[handler->minor >> 5] = handler;
// 放入input_handler_list鏈表
list_add_tail(&handler->node, &input_handler_list);
// 對於每個註冊input_dev,調用input_attach_handler
list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler); // 根據input_handler的id_table判斷能否支持這個input_dev
註冊輸入設備(硬件):
input_register_device()函數
// 放入鏈表
list_add_tail(&dev->node, &input_dev_list);
// 對於每一個input_handler,都調用input_attach_handler
list_for_each_entry(handler, &input_handler_list, node)
input_attach_handler(dev, handler); // 根據input_handler的id_table判斷能否支持這個input_dev
input_attach_handler()函數
//如果存在黑名單並且能匹配黑名單
if (handler->blacklist && input_match_device(handler->blacklist, dev))
return -ENODEV;
id = input_match_device(handler->id_table, dev);
if (!id)
return -ENODEV;
error = handler->connect(handler, dev, id); //執行註冊handler的connect()函數
if (error && error != -ENODEV)
printk(KERN_ERR
"input: failed to attach handler %s to device %s, "
"error: %d\n",
handler->name, kobject_name(&dev->cdev.kobj), error);
return error;
註冊input_dev或input_handler時,會兩兩比較左邊的input_dev和右邊的input_handler,
根據input_handler的id_table判斷這個input_handler能否支持這個input_dev,
如果能支持,則調用input_handler的connect函數建立"連接"
input_match_device()函數內容
linux-2.6.22.6\drivers\input\input.c內
檢測input_handler的id_table是否完全與input_dev匹配
#define MATCH_BIT(bit, max) \
for (i = 0; i < NBITS(max); i++) \
if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
break; \
if (i != NBITS(max)) \
continue;
//使用MATCH_BIT()檢測id->bit[i] & dev->bit[i]是否相等 如相等返回id
for (; id->flags || id->driver_info; id++) {
MATCH_BIT(evbit, EV_MAX);
MATCH_BIT(keybit, KEY_MAX);
MATCH_BIT(relbit, REL_MAX);
MATCH_BIT(absbit, ABS_MAX);
MATCH_BIT(mscbit, MSC_MAX);
MATCH_BIT(ledbit, LED_MAX);
MATCH_BIT(sndbit, SND_MAX);
MATCH_BIT(ffbit, FF_MAX);
MATCH_BIT(swbit, SW_MAX);
return id;
}
typeof介紹https://blog.csdn.net/bytxl/article/details/22985517
怎麼建立連接?(connect函數中需要做的事情)
1. 分配一個input_handle結構體kzalloc
2.
input_handle.dev = input_dev; // 指向硬件設備的input_dev
input_handle.handler = input_handler; // 指向驅動程序的input_handler
3. 註冊:
執行input_register_handle() 作用是將新建好的input_handle結構體
放入硬件設備與驅動設備的h_list(可支持列表)
input_handler->h_list = &input_handle;
inpu_dev->h_list = &input_handle;
下面是一個connect函數的例子
evdev_connect()函數
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); // 分配一個input_handle
// 設置參數
evdev->handle.dev = dev; // 指向硬件設備的input_dev
evdev->handle.name = evdev->name;
evdev->handle.handler = handler; // 指向驅動設備的input_handler
evdev->handle.private = evdev;
// 向內核註冊
error = input_register_handle(&evdev->handle);
怎麼讀按鍵?
app: read
--------------------------
.......
evdev_read
// 無數據並且是非阻塞方式打開,則立刻返回
if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
return -EAGAIN;
// 否則休眠
retval = wait_event_interruptible(evdev->wait,
client->head != client->tail || !evdev->exist);
休眠由誰喚醒?
input_handler 結構體內的evdev_event()來進行喚醒
wake_up_interruptible(&evdev->wait);
evdev_event被誰調用?
猜:應該是硬件相關的代碼,input_dev那層調用的
在設備的中斷服務程序裏,確定事件是什麼,然後調用相應的input_handler的event處理函數
以gpio_keys_isr()爲例
// 上報事件
input_event(input, type, button->code, !!state);
input_sync(input);
input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
struct input_handle *handle;
//對鏈表裏的每一項已經打開的設備執行它的event函數
list_for_each_entry(handle, &dev->h_list, d_node)
if (handle->open)
handle->handler->event(handle, type, code, value);