1. 程式人生 > >輸入子系統------鍵盤按鍵驅動程式 13.Linux鍵盤按鍵驅動 (詳解)

輸入子系統------鍵盤按鍵驅動程式 13.Linux鍵盤按鍵驅動 (詳解)

  • 由上一節的輸入子系統的框架分析可知,其分三層:裝置驅動層,核心層,事件驅動層

我們在為某種裝置的編寫驅動層,只需要關心裝置驅動層,即如何驅動裝置並獲得硬體資料(如按下的按鍵資料),然後呼叫核心層提供的介面,核心層就會自動把資料提交給事件處理層。在輸入子系統中,事件驅動是標準的,適用於所有輸入類的。我們的裝置可以利用一個已經存在的,合適的輸入事件驅動,通過輸入核心,和使用者應用程式介面。

 

一、編寫裝置驅動層的流程

1.分配一個input——dev結構體

2.設定input_dev的成員

3.註冊input_dev 驅動裝置

4.硬體相關程式碼

  1)初始化定時器和中斷

  2)寫中斷服務函式

  3)寫定時器超時函式

  4)在出口函式中 釋放中斷函式,刪除定時器,解除安裝釋放驅動

 

二、相關結構體及函式

input_dev驅動裝置結構體中常用成員及相關函式如下:(include/linux/Input.h)

 1 struct input_dev {      
 2 
 3        void *private;
 4        const char *name;  //裝置名字
 5        const char *phys;  //檔案路徑,比如 input/buttons
 6        const char
*uniq; 7 struct input_id id; 8 9 10 unsigned long evbit[NBITS(EV_MAX)]; //表示支援哪事件,常用有以下幾種事件(可以多選) 11 //EV_SYN 同步事件,當使用input_event()函式後,就要使用這個上報個同步事件 12 //EV_KEY 鍵盤事件 這些都是巨集定義 13 //EV_REL (relative)相對座標事件,比如滑鼠 14 //EV_ABS (absolute)絕對座標事件,比如搖桿、觸控式螢幕感應
15 //EV_MSC 其他事件,功能 16 //EV_LED LED燈事件 17 //EV_SND (sound)聲音事件 18 19 //EV_REP 重複鍵盤按鍵事件 20 //(內部會定義一個定時器,若有鍵盤按鍵事件一直按下/鬆開,就重複定時,時間一到就上報事件) 21 22 //EV_FF 受力事件 23 //EV_PWR 電源事件 24 //EV_FF_STATUS 受力狀態事件 25 26 unsigned long keybit[NBITS(KEY_MAX)]; //存放支援的鍵盤按鍵值,即能產生哪些按鍵 27 //鍵盤變數定義在:include/linux/input.h, 比如: KEY_L(按鍵L) 28 29 unsigned long relbit[NBITS(REL_MAX)]; //存放支援的相對座標值,如x,y,滾輪 30 unsigned long absbit[NBITS(ABS_MAX)]; //存放支援的絕對座標值 31 unsigned long mscbit[NBITS(MSC_MAX)]; //存放支援的其它事件,也就是功能 32 unsigned long ledbit[NBITS(LED_MAX)]; //存放支援的各種狀態LED 33 unsigned long sndbit[NBITS(SND_MAX)]; //存放支援的各種聲音 34 unsigned long ffbit[NBITS(FF_MAX)]; //存放支援的受力裝置 35 unsigned long swbit[NBITS(SW_MAX)]; //存放支援的開關功能

2)函式如下:

 1 struct input_dev *input_allocate_device(void);  //向核心中申請一個input_dev裝置,然後返回這個裝置
 2   
 3 input_unregister_device(struct input_dev *dev);  //解除安裝/sys/class/input目錄下的input_dev這個類裝置, 一般在驅動出口函式寫
 4  
 5 input_free_device(struct input_dev *dev);   //釋放input_dev這個結構體, 一般在驅動出口函式寫
 6  
 7  
 8 
 9 set_bit(nr,p);                  //設定某個結構體成員p裡面的某位等於nr,支援這個功能
10 /* 比如:
11 set_bit(EV_KEY,buttons_dev->evbit);   //設定input_dev結構體buttons_dev->evbit支援EV_KEY
12 set_bit(KEY_S,buttons_dev->keybit);  //設定input_dev結構體buttons_dev->keybit支援按鍵”S”
13 */
14 
15 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);  //上報事件
16  // input_dev *dev :要上報哪個input_dev驅動裝置的事件
17  // type : 要上報哪類事件, 比如按鍵事件,則填入: EV_KEY
18  // code: 對應的事件裡支援的哪個變數,比如按下按鍵L則填入: KEY_L
19  //value:對應的變數裡的數值,比如鬆開按鍵則填入1,鬆開按鍵則填入0

 

input_sync(struct input_dev *dev); //同步事件通知

為什麼使用了input_event()上報事件函式,就要使用這個函式?

因為input_event()函式只是個事件函式,所以需要這個input_sync()同步事件函式來通知系統,然後系統才會知道

input_sync()程式碼如下

static inline void input_sync(struct input_dev *dev)
{
input_event(dev, EV_SYN, SYN_REPORT, 0); //就是上報同步事件,告訴核心:input_event()事件執行完畢
}

 

三、編寫裝置驅動程式

 

  1 //參考:linux-2.6.22.6\linux-2.6.22.6\drivers\input\keyboard\Gpio_keys.c
  2 //pre1.包含標頭檔案
  3 #include <linux/module.h>
  4 #include <linux/version.h>
  5 
  6 #include <linux/init.h>
  7 #include <linux/fs.h>
  8 #include <linux/interrupt.h>
  9 #include <linux/irq.h>
 10 #include <linux/sched.h>
 11 #include <linux/pm.h>
 12 #include <linux/sysctl.h>
 13 #include <linux/proc_fs.h>
 14 #include <linux/delay.h>
 15 #include <linux/platform_device.h>
 16 #include <linux/input.h>
 17 #include <linux/irq.h>
 18 #include <asm/gpio.h>
 19 #include <asm/io.h>
 20 #include <asm/arch/regs-gpio.h>
 21 
 22 
 23 struct pin_desc{
 24     int irq;    //按鍵的外部中斷標誌位
 25     char *name;    //中斷裝置名稱
 26     unsigned int pin;    //引腳
 27     unsigned int key_val;  //dev_id,對應鍵盤的 L ,  S,  空格,  enter  
 28 };
 29 
 30 /* 定義四個按鍵 */
 31 struct pin_desc pins_desc[4] = {
 32     {IRQ_EINT0,  "S2", S3C2410_GPF0,  KEY_L},
 33     {IRQ_EINT2,  "S3", S3C2410_GPF2,  KEY_S},
 34     {IRQ_EINT11, "S4", S3C2410_GPG3,  KEY_ENTER},
 35     {IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT},
 36 };
 37 
 38 static struct pin_desc *irq_pd;             //指標irq_pd用來儲存dev_id
 39 static struct timer_list buttons_timer;    //定時器結構體
 40 static struct input_dev *buttons_dev;    //定義一個input_dev結構體指標 
 41 
 42 /* 中斷服務函式 */
 43 static irqreturn_t buttons_irq(int irq, void *dev_id)
 44 {
 45     /* 10ms後啟動定時器 */
 46     irq_pd = (struct pin_desc *)dev_id;    //儲存當前的dev_id
 47     mod_timer(&buttons_timer, jiffies+HZ/100);  //更新定時器10ms
 48     return IRQ_RETVAL(IRQ_HANDLED);
 49 }
 50 
 51 /* 定時器超時函式 */
 52 static void buttons_timer_function(unsigned long data)
 53 {
 54     //超時處理函式只需要將事件上報即可
 55     
 56     struct pin_desc * pindesc = irq_pd;
 57     unsigned int pinval;
 58     
 59     if (!pindesc)
 60         return;
 61     
 62     pinval = s3c2410_gpio_getpin(pindesc->pin);
 63 
 64     if (pinval)
 65     {
 66         /* 鬆開 :上報EV_KEY型別,button按鍵,0(沒按下)*/
 67         input_event(buttons_dev, EV_KEY, pindesc->key_val, 0); 
 68         input_sync(buttons_dev); //上傳同步事件,告訴系統有事件出現 
 69     }
 70     else
 71     {
 72         /* 按下:上報EV_KEY型別,button按鍵,1(按下) */
 73         input_event(buttons_dev, EV_KEY, pindesc->key_val, 1); 
 74         input_sync(buttons_dev);
 75     }
 76 }
 77 
 78 
 79 
 80 //pre2.寫入口函式
 81 static int buttons_init(void)
 82 {
 83     int i;
 84     /* 1.分配一個input_dev結構體 */
 85     //向核心中申請一個input_dev裝置,然後返回這個裝置,此處省略判斷返回值
 86     buttons_dev = input_allocate_device();
 87     
 88     /* 2.設定input_dev的成員 */
 89     /* 2.1先設定能產生哪一類事件 */
 90     set_bit(EV_KEY, buttons_dev->evbit); //此處表示能產生鍵盤事件
 91    set_bit(EV_REP, buttons_dev->evbit); //支援鍵盤重複按事件
 92     /* 2.2能產生這類操作裡的哪些事件 eg: L,S,ENTER,LEFTSHIFT */
 93     set_bit(KEY_L, buttons_dev->keybit);       //#define KEY_L    38
 94     set_bit(KEY_S, buttons_dev->keybit);    //這些巨集都在Input.h中定義
 95     set_bit(KEY_ENTER, buttons_dev->keybit); //支援按鍵回車
 96     set_bit(KEY_LEFTSHIFT, buttons_dev->keybit);
 97 
 98     /* 3.註冊input_dev 驅動裝置 */
 99     input_register_device(buttons_dev);
100 
101     /* 4.硬體相關程式碼 */
102     init_timer(&buttons_timer);
103     buttons_timer.function = buttons_timer_function;
104     add_timer(&buttons_timer); 
105     
106     for (i = 0; i < 4; i++)
107     {
108         request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]);
109     }
110 
111     return 0;
112 }
113 
114 //pre3.寫出口函式
115 static void buttons_exit(void)
116 {
117     int i;
118     for (i = 0; i < 4; i++)
119     {
120         free_irq(pins_desc[i].irq, &pins_desc[i]); //清中斷
121     }
122     del_timer(&buttons_timer);  //刪除定時器
123     input_unregister_device(buttons_dev);  //解除安裝裝置
124     input_free_device(buttons_dev);  //釋放分配給input_dev裝置的空間
125 }
126 
127 
128 
129 
130 //pre4.修飾,新增屬性
131 module_init(buttons_init);
132 module_exit(buttons_exit);
133 
134 MODULE_LICENSE("GPL");

 

四、測試

 1.掛載

掛載鍵盤驅動後, 如下圖,可以通過  ls -l /dev/event*   命令檢視已掛載的裝置節點:

 載入了驅動之後,多出事件event1,代表我們的按鍵驅動

 其中主裝置號13,次裝置號是65,

在事件處理驅動的函式中,如Evdev.c中的evdev_connect函式中,

for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);

,其中event驅動本身的此裝置號是從64開始的,如上圖,核心啟動時,會載入自帶觸控式螢幕驅動,所以我們的鍵盤驅動的次裝置號=64+1

2.執行

測試執行有兩種,一種是直接開啟/dev/tyy1,第二種是使用exec命令

方法1:

cat /dev/tty1     //tty1:LCD終端,就會通過tty_io.c來訪問鍵盤驅動,然後列印在tty1終端上

方法2:

exec 0</dev/tty1    //將/dev/tty1掛載到-sh程序描述符0下,此時的鍵盤驅動就會直接列印在tty1終端上

3.除錯:

若測試不成功,板子又在QT下進行的:

1)可以使用vi命令,在板子上開啟記事本,按按鍵測試

2)或者刪除/etc/init.d/rcS 裡面有關QT自啟動的命令,然後重啟

若板子沒在QT下進行,也無法測試成功:

1)可以使用hexdump命令來除錯程式碼

詳見NQian博主的文章

(exec命令詳解入口地址: http://www.cnblogs.com/lifexy/p/7553228.html)

(hexdump命令除錯程式碼詳解地址:http://www.cnblogs.com/lifexy/p/7553550.html)

 

參考:

13.Linux鍵盤按鍵驅動 (詳解)