LINUX設備驅動之輸入子系統(一)

在前面鍵盤驅動的分析中已經接觸到了輸入子系統,本文將結合鍵盤驅動,系統分析輸入子系統。

 

回想一下,在設備驅動匹配成功時,創建了一個input_dev並註冊到輸入子系統;在鍵盤中斷處理例程中向輸入子系統上報事件。

 

輸入子系統是所有I/O設備驅動的中間層,如何爲下層衆多各式各樣的輸入設備提供接口以及爲上層提供了一個統一的界面?

 

根據內核代碼,輸入子系統中存在兩個鏈表:input_dev_listinput_handler_list,當註冊一個input_dev時就會把它掛到input_dev_list上,然後去匹配input_handler_list上的input_handler,相反,當註冊一個input_handler時就會把它掛到input_handler_list上,然後去匹配input_dev_list上的input_dev,匹配成功時就會調用該handlerconnect函數,該函數一般會創建和註冊一個input_handle,這個input_handle中包含了input_devinput_handler相關的信息,這樣當下層向輸入子系統上報事件時就可以通過input_handle找到相應的input_handler並調用相關函數,進一步爲上次提供服務。

 

下面將一步一步詳細的分析這個過程。

 

一.Input設備的註冊

Input設備註冊的接口函數爲:input_register_device,不過在註冊Input設備,前必須先創建一個Input設備,可以靜態定義一個Input設備然後自行初始化它,也可以調用input_allocate_device來創建一個Input設備(回想一下,在鍵盤驅動分析的代碼中就是調用這個函數的),看一下這個函數:

1389       struct input_dev *input_allocate_device(void)

1390       {

1391              struct input_dev *dev;

1392      

1393              dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);

1394              if (dev) {

1395                     dev->dev.type = &input_dev_type;

1396                     dev->dev.class = &input_class;

1397                     device_initialize(&dev->dev);

1398                     mutex_init(&dev->mutex);

1399                     spin_lock_init(&dev->event_lock);

1400                     INIT_LIST_HEAD(&dev->h_list);

1401                     INIT_LIST_HEAD(&dev->node);

1402      

1403                     __module_get(THIS_MODULE);

1404              }

1405      

1406              return dev;

1407       }

input_dev結構如下:

1072       struct input_dev {

1073              const char *name;

1074              const char *phys;

1075              const char *uniq;

1076              struct input_id id;

1077      

1078              unsigned long evbit[BITS_TO_LONGS(EV_CNT)];

1079              unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];

1080              unsigned long relbit[BITS_TO_LONGS(REL_CNT)];

1081              unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];

1082              unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];

1083              unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];

1084              unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];

1085              unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];

1086              unsigned long swbit[BITS_TO_LONGS(SW_CNT)];

1087      

1088              unsigned int keycodemax;

1089              unsigned int keycodesize;

1090              void *keycode;

1091              int (*setkeycode)(struct input_dev *dev, int scancode, int keycode);

1092              int (*getkeycode)(struct input_dev *dev, int scancode, int *keycode);

1093      

1094              struct ff_device *ff;

1095      

1096              unsigned int repeat_key;

1097              struct timer_list timer;

1098      

1099              int sync;

1100      

1101              int abs[ABS_MAX + 1];

1102              int rep[REP_MAX + 1];

1103      

1104              unsigned long key[BITS_TO_LONGS(KEY_CNT)];

1105              unsigned long led[BITS_TO_LONGS(LED_CNT)];

1106              unsigned long snd[BITS_TO_LONGS(SND_CNT)];

1107              unsigned long sw[BITS_TO_LONGS(SW_CNT)];

1108      

1109              int absmax[ABS_MAX + 1];

1110        int absmin[ABS_MAX + 1];

1111        int absfuzz[ABS_MAX + 1];

1112        int absflat[ABS_MAX + 1];

1113        int absres[ABS_MAX + 1];

1114

1115        int (*open)(struct input_dev *dev);

1116        void (*close)(struct input_dev *dev);

1117        int (*flush)(struct input_dev *dev, struct file *file);

1118        int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);

1119

1120              struct input_handle *grab;

1121      

1122              spinlock_t event_lock;

1123              struct mutex mutex;

1124      

1125              unsigned int users;

1126              bool going_away;

1127      

1128              struct device dev;

1129      

1130              struct list_head       h_list;

1131              struct list_head       node;

1132              };

和前面分析的其他設備的創建一樣,input_allocate_device函數爲input_dev分配內存空間,出始化其內嵌的device,這裏還初始化了input_dev結構中的h_listnode鏈表頭,node是用於將該input_dev掛到input_dev_list上,而h_list指向的量表用於存放input_handle,在後面分析將會看到這點。

input_dev結構裏邊衆多的其他字段先不理會,等用到時再做分析。

 

創建了input_dev後,根據具體的驅動程序情況對input_dev進行相應的設置(如在鍵盤驅動的分析中,調用了atkbd_set_device_attrs函數等)後,就可以調用input_register_device函數向輸入子系統註冊input_dev了,函數如下:

1503       int input_register_device(struct input_dev *dev)

1504       {

1505              static atomic_t input_no = ATOMIC_INIT(0);

1506              struct input_handler *handler;

1507              const char *path;

1508              int error;

1509      

1510              __set_bit(EV_SYN, dev->evbit);

1511      

1512              /*

1513              * If delay and period are pre-set by the driver, then autorepeating

1514              * is handled by the driver itself and we don't do it in input.c.

1515              */

1516      

1517              init_timer(&dev->timer);

1518              if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {

1519                     dev->timer.data = (long) dev;

1520                     dev->timer.function = input_repeat_key;

1521                     dev->rep[REP_DELAY] = 250;

1522                     dev->rep[REP_PERIOD] = 33;

1523              }

1524      

1525              if (!dev->getkeycode)

1526                     dev->getkeycode = input_default_getkeycode;

1527      

1528              if (!dev->setkeycode)

1529                     dev->setkeycode = input_default_setkeycode;

1530      

1531              dev_set_name(&dev->dev, "input%ld",

1532                          (unsigned long) atomic_inc_return(&input_no) - 1);

1533      

1534              error = device_add(&dev->dev);

1535              if (error)

1536                     return error;

1537      

1538              path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);

1539              printk(KERN_INFO "input: %s as %s\n",

1540                     dev->name ? dev->name : "Unspecified device", path ? path : "N/A");

1541              kfree(path);

1542      

1543              error = mutex_lock_interruptible(&input_mutex);

1544              if (error) {

1545                     device_del(&dev->dev);

1546                     return error;

1547              }

1548      

1549              list_add_tail(&dev->node, &input_dev_list);

1550      

1551              list_for_each_entry(handler, &input_handler_list, node)

1552                     input_attach_handler(dev, handler);

1553      

1554              input_wakeup_procfs_readers();

1555      

1556              mutex_unlock(&input_mutex);

1557      

1558              return 0;

1559       }

1525~1529行,如果調用input_register_device前沒有爲input_devgetkeycodesetkeycode函數指針設值,則用input_default_getkeycodeinput_default_setkeycode賦給他們,getkeycodesetkeycode是在上報事件的處理中用於獲得和設置相應的鍵值,看下這兩個函數:

0560       static int input_default_getkeycode(struct input_dev *dev,

0561                                       int scancode, int *keycode)

0562       {

0563              if (!dev->keycodesize)

0564                     return -EINVAL;

0565      

0566              if (scancode >= dev->keycodemax)

0567                     return -EINVAL;

0568      

0569              *keycode = input_fetch_keycode(dev, scancode);

0570      

0571              return 0;

0572       }

 

0546       static int input_fetch_keycode(struct input_dev *dev, int scancode)

0547       {

0548              switch (dev->keycodesize) {

0549                     case 1:

0550                            return ((u8 *)dev->keycode)[scancode];

0551      

0552                     case 2:

0553                            return ((u16 *)dev->keycode)[scancode];

0554      

0555                     default:

0556                            return ((u32 *)dev->keycode)[scancode];

0557              }

0558       }

 

0574       static int input_default_setkeycode(struct input_dev *dev,

0575                                       int scancode, int keycode)

0576       {

0577              int old_keycode;

0578              int i;

0579      

0580              if (scancode >= dev->keycodemax)

0581                     return -EINVAL;

0582      

0583              if (!dev->keycodesize)

0584                     return -EINVAL;

0585      

0586              if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))

0587                     return -EINVAL;

0588      

0589              switch (dev->keycodesize) {

0590                     case 1: {

0591                            u8 *k = (u8 *)dev->keycode;

0592                            old_keycode = k[scancode];

0593                            k[scancode] = keycode;

0594                            break;

0595                     }

0596                     case 2: {

0597                            u16 *k = (u16 *)dev->keycode;

0598                            old_keycode = k[scancode];

0599                            k[scancode] = keycode;

0600                            break;

0601                     }

0602                     default: {

0603                            u32 *k = (u32 *)dev->keycode;

0604                            old_keycode = k[scancode];

0605                            k[scancode] = keycode;

0606                            break;

0607                     }

0608              }

0609      

0610              clear_bit(old_keycode, dev->keybit);

0611              set_bit(keycode, dev->keybit);

0612      

0613              for (i = 0; i < dev->keycodemax; i++) {

0614                     if (input_fetch_keycode(dev, i) == old_keycode) {

0615                            set_bit(old_keycode, dev->keybit);

0616                            break; /* Setting the bit twice is useless, so break */

0617                     }

0618              }

0619      

0620              return 0;

0621       }

不難看出,如上面分析一樣input_default_getkeycode通過scancode索引在鍵值表中查到相應的值並賦給*keycodeinput_default_setkeycode函數則重新設置輸入索引的鍵值表的值。

接着看input_register_device函數,第1534行調用device_add函數註冊input_dev內嵌的device,獲得互斥鎖後,1549行調用list_add_tail(&dev->node, &input_dev_list)將該input_dev掛到input_dev_list鏈表上。

接着1551~1552行掃面input_handler_list上的每個handler,用input_attach_handler函數進行devhandler的匹配,這裏input_handler_list上的handler是什麼時候掛上去的,我們在後面再分析,繼續分析input_attach_handler函數:

0739       static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)

0740       {

0741              const struct input_device_id *id;

0742              int error;

0743      

0744              if (handler->blacklist && input_match_device(handler->blacklist, dev))

0745                     return -ENODEV;

0746      

0747              id = input_match_device(handler->id_table, dev);

0748              if (!id)

0749                     return -ENODEV;

0750      

0751              error = handler->connect(handler, dev, id);

0752              if (error && error != -ENODEV)

0753                     printk(KERN_ERR

0754                            "input: failed to attach handler %s to device %s, "

0755                            "error: %d\n",

0756                            handler->name, kobject_name(&dev->dev.kobj), error);

0757      

0758              return error;

0759       }

0744行先匹配blacklistdev,如果匹配成功則返回-ENODEV,這裏明顯屏蔽掉了blacklist列表上的設備,即使其在handler->id_table中能匹配成功。

0747行匹配handler->id_table dev,如果匹配成功則調用handlerconnect函數。

input_device_id結構定義如下:

0312       struct input_device_id {

0313      

0314              kernel_ulong_t flags;

0315      

0316              __u16 bustype;

0317              __u16 vendor;

0318              __u16 product;

0319              __u16 version;

0320      

0321              kernel_ulong_t evbit[INPUT_DEVICE_ID_EV_MAX / BITS_PER_LONG + 1];

0322              kernel_ulong_t keybit[INPUT_DEVICE_ID_KEY_MAX / BITS_PER_LONG + 1];

0323              kernel_ulong_t relbit[INPUT_DEVICE_ID_REL_MAX / BITS_PER_LONG + 1];

0324              kernel_ulong_t absbit[INPUT_DEVICE_ID_ABS_MAX / BITS_PER_LONG + 1];

0325              kernel_ulong_t mscbit[INPUT_DEVICE_ID_MSC_MAX / BITS_PER_LONG + 1];

0326              kernel_ulong_t ledbit[INPUT_DEVICE_ID_LED_MAX / BITS_PER_LONG + 1];

0327              kernel_ulong_t sndbit[INPUT_DEVICE_ID_SND_MAX / BITS_PER_LONG + 1];

0328              kernel_ulong_t ffbit[INPUT_DEVICE_ID_FF_MAX / BITS_PER_LONG + 1];

0329              kernel_ulong_t swbit[INPUT_DEVICE_ID_SW_MAX / BITS_PER_LONG + 1];

0330      

0331              kernel_ulong_t driver_info;

0332       };

接着看這個匹配過程:

0700       static const struct input_device_id *input_match_device(const struct input_device_id *id,

0701                                                        struct input_dev *dev)

0702       {

0703              int i;

0704      

0705              for (; id->flags || id->driver_info; id++) {

0706      

0707                     if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)

0708                            if (id->bustype != dev->id.bustype)

0709                                   continue;

0710      

0711                     if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)

0712                            if (id->vendor != dev->id.vendor)

0713                                   continue;

0714      

0715                     if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)

0716                            if (id->product != dev->id.product)

0717                                   continue;

0718      

0719                     if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)

0720                            if (id->version != dev->id.version)

0721                                   continue;

0722      

0723                     MATCH_BIT(evbit,  EV_MAX);

0724                     MATCH_BIT(keybit, KEY_MAX);

0725                     MATCH_BIT(relbit, REL_MAX);

0726                     MATCH_BIT(absbit, ABS_MAX);

0727                     MATCH_BIT(mscbit, MSC_MAX);

0728                     MATCH_BIT(ledbit, LED_MAX);

0729                     MATCH_BIT(sndbit, SND_MAX);

0730                     MATCH_BIT(ffbit,  FF_MAX);

0731                     MATCH_BIT(swbit,  SW_MAX);

0732      

0733                     return id;

0734              }

0735      

0736              return NULL;

0737       }

只要設置了id->driver_info for循環就會執行,id->flags標示按需要匹配的字段,0723~0731MATCH_BIT部分是必須匹配部分,看看這個宏定義:

0693       #define MATCH_BIT(bit, max) \

0694                     for (i = 0; i < BITS_TO_LONGS(max); i++) \

0695                            if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \

0696                                   break; \

0697                     if (i != BITS_TO_LONGS(max)) \

0698                            continue;

可以看出如果id->bit[i]有設值,要匹配成功dev->bit[i]也必須設置相應的值;如果id->bit[i]沒設值,則該項不需要匹配。

接着input_register_device 函數第1554input_wakeup_procfs_readers()函數:

0768       static inline void input_wakeup_procfs_readers(void)

0769       {

0770              input_devices_state++;

0771              wake_up(&input_devices_poll_wait);

0772       }

自增input_devices_state並喚醒在文件操作poll上阻塞的進程,input_devices_state是用於更新相應文件filef_version

input_register_device 函數接着釋放信號後返回。

Input設備的註冊就分析到這裏。

發佈了18 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章