Linux裝置驅動工程師之路——input子系統

來源:互聯網
上載者:User

Linux裝置驅動工程師之路——input子系統

K-Style

轉載請註明來自于衡陽師範學院08電2  K-Style  http://blog.csdn.net/ayangke,QQ:843308498 郵箱:yangkeemail@qq.com

 

         一、input子系統系統架構

Linux核心實現了一套input子系統,很多輸入裝置,比如說按鍵、鍵盤、滑鼠、觸控螢幕等等都可以利用input子系統提供的介面來編寫驅動,這樣可以幫驅動編寫者減少大量工作量。因為input子系統會幫驅動完成open,read,close等一些列的裝置方法。驅動編寫者只需要在按鍵按下或者滑鼠點擊事件時想input子系統上層的event hander報告相應的事件資訊就可以了。是input子系統的架構。最下層是輸入裝置的驅動層,也就是我們需要編寫的,在最底層上面的一層是input子系統的core層。由input.c檔案實現。再上面一層是對應裝置的handler層,每種裝置對應一種handler。handler會在/dev/input檔案夾下建立對應的檔案,並對底層報告上來的時間進行相應的處理,並且完成一些相應的檔案操作等。

二、Input driver編寫要點


1、分配、註冊、登出input裝置

struct input_dev*input_allocate_device(void)
        intinput_register_device(struct input_dev *dev)
        voidinput_unregister_device(struct input_dev *dev)

2、設定input裝置支援的事件類型、事件碼、事件值的範圍、input_id等資訊

參見usb鍵盤驅動:usbkbd.c

usb_to_input_id(dev,&input_dev->id);//設定bustype、vendo、product等
        input_dev->evbit[0] =BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);//支援的事件類型
        input_dev->ledbit[0] =BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) |BIT(LED_KANA);// EV_LED事件支援的事件碼
        for (i = 0; i < 255; i++)
                set_bit(usb_kbd_keycode[i],input_dev->keybit); //EV_KEY事件支援的事件碼

include/linux/input.h中定義了支援的類型(下面列出的是2.6.22核心的情況)

#define EV_SYN           0x00
        #defineEV_KEY           0x01
        #defineEV_REL           0x02
        #defineEV_ABS           0x03
        #defineEV_MSC          0x04
        #defineEV_SW            0x05
        #defineEV_LED          0x11
        #defineEV_SND         0x12
        #defineEV_REP         0x14
        #define EV_FF            0x15
        #defineEV_PWR        0x16
        #defineEV_FF_STATUS        0x17
        #defineEV_MAX          0x1f

一個裝置可以支援一個或多個事件類型。每個事件類型下面還需要設定具體的觸發事件碼。比如:EV_KEY事件,需要定義其支援哪些按鍵事件碼。

3、如果需要,設定input裝置的開啟、關閉、寫入資料時的處理方法

參見usb鍵盤驅動:usbkbd.c

input_dev->open= usb_kbd_open;
        input_dev->close =usb_kbd_close;
        input_dev->event =usb_kbd_event;

4、在發生輸入事件時,向子系統報告事件


用於報告EV_KEY、EV_REL、EV_ABS等事件的函數有:

void input_report_key(structinput_dev *dev, unsigned int code, int value)
        void input_report_rel(structinput_dev *dev, unsigned int code, int value)
        void input_report_abs(structinput_dev *dev, unsigned int code, int value)

如果你覺得麻煩,你也可以只記住1個函數(因為上述函數都是通過它實現的)

voidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, intvalue)

報告時間之後需要調用

input_sync(input_dev),它告知事件的接收者:驅動已經發生了一個完整的報告。

參考文章:劉洪濤《linux核心input子系統解析》

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.