因爲分析一所講的每種數據結構都代表一類對象,所以每種數據結構都會對應一個註冊函數,他們都定義在子系統核心的input.c文件中。主要有三個註冊函數
input_register_device 向內核註冊一個input設備
input_register_handle 向內核註冊一個handle結構
input_register_handler 註冊一個事件處理器
1. input_register_device 註冊一個input輸入設備,這個註冊函數在三個註冊函數中是驅動程序唯一調用的。下面分析這個函數:
- int input_register_device(struct input_dev *dev)
- {
- static atomic_t input_no = ATOMIC_INIT(0);
- //這個原子變量,代表總共註冊的input設備,每註冊一個加1,因爲是靜態變量,所以每次調用都不會清零的
- struct input_handler *handler;
- const char *path;
- int error;
- __set_bit(EV_SYN, dev->evbit); //EN_SYN 這個是設備都要支持的事件類型,所以要設置
- /*
- * If delay and period are pre-set by the driver, then autorepeating
- * is handled by the driver itself and we don't do it in input.c.
- */
- // 這個內核定時器是爲了重複按鍵而設置的
- init_timer(&dev->timer);
- if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
- dev->timer.data = (long) dev;
- dev->timer.function = input_repeat_key;
- dev->rep[REP_DELAY] = 250;
- dev->rep[REP_PERIOD] = 33;
- //如果沒有定義有關重複按鍵的相關值,就用內核默認的
- }
- if (!dev->getkeycode)
- dev->getkeycode = input_default_getkeycode;
- if (!dev->setkeycode)
- dev->setkeycode = input_default_setkeycode;
- //以上設置的默認函數由input核心提供
- dev_set_name(&dev->dev, "input%ld",
- (unsigned long) atomic_inc_return(&input_no) - 1);
- //設置input_dev中device的名字,這個名字會在/class/input中出現
- error = device_add(&dev->dev);
- //將device加入到linux設備模型中去
- if (error)
- return error;
- path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
- printk(KERN_INFO "input: %s as %s\n",
- dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
- kfree(path);
- //這個得到路徑名稱,並打印出來
- error = mutex_lock_interruptible(&input_mutex);
- if (error) {
- device_del(&dev->dev);
- return error;
- }
- list_add_tail(&dev->node, &input_dev_list);
- // 將新分配的input設備連接到input_dev_list鏈表上
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- //遍歷input_handler_list鏈表,配對 input_dev 和 input_handler
- //input_attach_handler 這個函數是配對的關鍵,下面將詳細分析
- input_wakeup_procfs_readers();
- // 和proc文件系統有關,暫時不考慮
- mutex_unlock(&input_mutex);
- return 0;
- }
- static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
- {
- const struct input_device_id *id;
- int error;
- if (handler->blacklist && input_match_device(handler->blacklist, dev))
- return -ENODEV;
- //blacklist是handler因該忽略的input設備類型,如果應該忽略的input設備也配對上了,那就出錯了
- id = input_match_device(handler->id_table, dev);
- //這個是主要的配對函數,主要比較id中的各項,下面詳細分析
- if (!id)
- return -ENODEV;
- error = handler->connect(handler, dev, id);
- //配對成功調用handler的connect函數,這個函數在事件處理器中定義,主要生成一個input_handle結構,並初始化,還生成一個事件處理器相關的設備結構,後面詳細分析
- if (error && error != -ENODEV)
- printk(KERN_ERR
- "input: failed to attach handler %s to device %s, "
- "error: %d\n",
- handler->name, kobject_name(&dev->dev.kobj), error);
- //出錯處理
- return error;
- }
下面分析input_match_device函數:
- static const struct input_device_id *input_match_device(const struct input_device_id *id,
- struct input_dev *dev)
- {
- int i;
- //函數傳入的參數是所要配對handler的id_table,下面遍歷這個id_table尋找合適的id進行配對
- for (; id->flags || id->driver_info; id++) {
- if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
- if (id->bustype != dev->id.bustype)
- continue;
- ......
- //針對handler->id->flag,比較不同的類型
- //如果比較成功進入下面的宏,否則進入下一個id
- MATCH_BIT(evbit, EV_MAX);
- ......
- MATCH_BIT(swbit, SW_MAX);
- return id;
- }
- }
然後依據id->flag來比較內容,如果都比較成功進入MATCH_BIT,這個宏是用來按位進行比較的,功能是比較所支持事件的類型,只有所有的位都匹配才成功返回,否則進行下一個id的比較。
- #define MATCH_BIT(bit, max) \
- for (i = 0; i < BITS_TO_LONGS(max); i++) \
- if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
- break; \
- if (i != BITS_TO_LONGS(max)) \
- continue;
對於connect函數,每種事件處理器的實現都有差異,但原理都相同,因爲觸摸屏用的事件處理器爲evdev,下面分析evdev的connect函數evdev_connect
- static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
- const struct input_device_id *id)
- {
- //此函數傳入三個參數,分別是:handler,dev,id
- struct evdev *evdev;
- int minor;
- int error;
- for (minor = 0; minor < EVDEV_MINORS; minor++)
- if (!evdev_table[minor])
- break;
- //EVDEV_MINORS爲32,說明evdev這個handler可以同時有32個輸入設備和他配對,evdev_table中以minor(非次設備號,但是有一個換算關係)存放evdev結構體,後面要詳細分析這個結構體
- if (minor == EVDEV_MINORS) {
- printk(KERN_ERR "evdev: no more free evdev devices\n");
- return -ENFILE;
- }
- //這個說明32個位置全都被佔用了,連接失敗
- evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
- //分配一個evdev結構體,這個結構體是evdev事件處理器特有的,後面會詳細分析
- if (!evdev)
- return -ENOMEM;
- INIT_LIST_HEAD(&evdev->client_list);
- spin_lock_init(&evdev->client_lock);
- mutex_init(&evdev->mutex);
- init_waitqueue_head(&evdev->wait);
- //初始化結構體的一些成員
- dev_set_name(&evdev->dev, "event%d", minor);
- //這個是設置evdev中device的名字,他將出現在/class/input中。
- //前面也有一個device是input_dev的,名字是input(n),注意與他的不同
- //這個結構是配對後的虛擬設備結構,沒有對應的硬件,但是通過它可以找到相關的硬件
- evdev->exist = 1;
- evdev->minor = minor;
- evdev->handle.dev = input_get_device(dev);
- evdev->handle.name = dev_name(&evdev->dev);
- evdev->handle.handler = handler;
- evdev->handle.private = evdev;
- //因爲evdev中包含handle了,所以初始化它就可以了,這樣就連接了input_handler與input_dev
- evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor); //注意:這個minor不是真正的次設備號,還要加上EVDEV_MINOR_BASE
- evdev->dev.class = &input_class;
- evdev->dev.parent = &dev->dev;
- //配對生成的device,父設備是與他相關連的input_dev
- evdev->dev.release = evdev_free;
- device_initialize(&evdev->dev);
- error = input_register_handle(&evdev->handle);
- //註冊handle結構體,這個函數後面詳細分析
- if (error)
- goto err_free_evdev;
- error = evdev_install_chrdev(evdev);
- //這個函數只做了一件事,就是把evdev結構保存到evdev_table中,這個數組也minor爲索引
- if (error)
- goto err_unregister_handle;
- error = device_add(&evdev->dev);
- //註冊到linux設備模型中
- if (error)
- goto err_cleanup_evdev;
- return 0;
- err_cleanup_evdev:
- evdev_cleanup(evdev);
- err_unregister_handle:
- input_unregister_handle(&evdev->handle);
- err_free_evdev:
- put_device(&evdev->dev);
- return error;
- }
2. input_register_handle 註冊一個input_handle結構體,比較簡單
- int input_register_handle(struct input_handle *handle)
- {
- struct input_handler *handler = handle->handler;
- struct input_dev *dev = handle->dev;
- int error;
- /*
- * We take dev->mutex here to prevent race with
- * input_release_device().
- */
- error = mutex_lock_interruptible(&dev->mutex);
- if (error)
- return error;
- list_add_tail_rcu(&handle->d_node, &dev->h_list);
- //將handle的d_node,鏈接到其相關的input_dev的h_list鏈表中
- mutex_unlock(&dev->mutex);
- list_add_tail(&handle->h_node, &handler->h_list);
- //將handle的h_node,鏈接到其相關的input_handler的h_list鏈表中
- if (handler->start)
- handler->start(handle);
- return 0;
- }
3. input_register_handler 註冊一個input_handler結構體
- int input_register_handler(struct input_handler *handler)
- {
- struct input_dev *dev;
- int retval;
- retval = mutex_lock_interruptible(&input_mutex);
- if (retval)
- return retval;
- INIT_LIST_HEAD(&handler->h_list);
- if (handler->fops != NULL) {
- if (input_table[handler->minor >> 5]) {
- retval = -EBUSY;
- goto out;
- }
- input_table[handler->minor >> 5] = handler;
- }
- //input_table,每個註冊的handler都會將自己保存到這裏,索引值爲handler->minor右移5爲,也就是除以32
- //爲什麼會這樣呢,因爲每個handler都會處理最大32個input_dev,所以要以minor的32爲倍數對齊,這個minor是傳進來的handler的MINOR_BASE
- //每一個handler都有一個這一個MINOR_BASE,以evdev爲例,EVDEV_MINOR_BASE = 64,可以看出系統總共可以註冊8個handler
- list_add_tail(&handler->node, &input_handler_list);
- //連接到input_handler_list鏈表中
- list_for_each_entry(dev, &input_dev_list, node)
- input_attach_handler(dev, handler);
- //又是配對,不過這次遍歷input_dev,和註冊input_dev過程一樣的
- input_wakeup_procfs_readers();
- out:
- mutex_unlock(&input_mutex);
- return retval;
- }