三星210--input中断输入驱动

  目前Android、QT等众多应用对于linux系统中键盘、鼠标、触摸屏等输入设备的支持都通过标准的input输入子系统。

一、input输入子系统框架

下图是input输入子系统框架,输入子系统由输入子系统核心层(input core),驱动层和事件处理层(Event Handler)三部分组成。一个输入事件,比如滑动触摸屏都是通过input driver -> input core -> event handler -> user space 到达用户空间传给应用程序。

 

Input输入子系统框架

 

 

二、Input driver编写要点

1.  分配、注册、注销input设备

参见触摸屏驱动ctp_it7250.c

struct input_dev *input_allocate_device(void)

int input_register_device(struct input_dev *dev)

void input_unregister_device(struct input_dev *dev)

 

2. 设置input设备支持的事件类型、事件码、事件值的范围等信息

参见触摸屏驱动ctp_it7250.c

    __set_bit(EV_ABS, Ctp_it7250->input_dev->evbit);

    __set_bit(ABS_X, Ctp_it7250->input_dev->absbit);

    __set_bit(ABS_Y, Ctp_it7250->input_dev->absbit);

    __set_bit(EV_SYN, Ctp_it7250->input_dev->evbit);

    __set_bit(EV_KEY, Ctp_it7250->input_dev->evbit);

    __set_bit(BTN_TOUCH, Ctp_it7250->input_dev->keybit);

Include/linux/input.h中定义了支持的类型

#define EV_SYN          0x00

#define EV_KEY          0x01

#define EV_REL          0x02

#define EV_ABS          0x03

#define EV_MSC          0x04

#define EV_SW           0x05

#define EV_LED          0x11

#define EV_SND          0x12

#define EV_REP          0x14

#define EV_FF           0x15

#define EV_PWR          0x16

#define EV_FF_STATUS        0x17

#define EV_MAX          0x1f

#define EV_CNT          (EV_MAX+1)

一个设备可以支持一个或多个事件类型。每个事件类型下面还需要设置具体的触发事件码。比如:EV_KEY事件,需要定义其支持哪些按键事件码。

 

3. 如果需要,设置input设备的打开、关闭的处理方法

参见触摸屏驱动ctp_it7250.c

    Ctp_it7250->input_dev->open = Ctp_it7250_touch_open;

Ctp_it7250->input_dev->close = Ctp_it7250_touch_close;

 

4. 在发生输入事件时,向子系统报告事件

参见触摸屏驱动ctp_it7250.c

input_report_key(Ctp_it7250->input_dev,ABS_MT_TRACKING_ID,0);

input_report_abs(Ctp_it7250->input_dev, ABS_MT_POSITION_X, gpdwSampleX[0]);

input_report_abs(Ctp_it7250->input_dev, ABS_MT_POSITION_Y, gpdwSampleY[0]);

input_report_abs(Ctp_it7250->input_dev, ABS_MT_TOUCH_MAJOR, 1);

input_mt_sync(Ctp_it7250->input_dev);

input_sync(Ctp_it7250->input_dev);

而上述函数其实都是通过下面这个函数实现的

void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)

而此函数最终会调用handle->handler->event,也就是evdev_event函数

static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)

 

继续往上,以Android2.1为例,input上报的流程如下,蓝色为文件以及路径,红色为用到的函数

frameworks/base/services/java/com/android/server/KeyInputQueue.java->:Thread mThread = new Thread("InputDeviceReader")
frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp->:static jboolean android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz, jobject event)
frameworks/base/libs/ui/EventHub.cpp->:
bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
        int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
        int32_t* outValue, nsecs_t* outWhen)

其中EventHub.cpp为HAL层代码、com_android_server_KeyInputQueue.cpp为jni本地调用,KeyInputQueue.java为framework的server代码,具体细节可以自己去研究其代码。在Android2.3里多了几层的封装

frameworks/base/services/java/com/android/server/InputManager.java->:public void start()
frameworks/base/services/jni/com_android_server_InputManager.cpp->:static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz)
frameworks/base/libs/ui/InputManager.cpp->:status_t InputManager::start()
frameworks/base/libs/ui/InputReader.cpp->:bool InputReaderThread::threadLoop()
frameworks/base/libs/ui/EventHub.cpp->:bool EventHub::getEvent(RawEvent* outEvent)

二、重要的数据结构

在输入子系统的设备驱动中,最重要的数据结构是struct input_dev,如程序清单 1.1所示。需要完成的大部分工作都是围绕着它来的,它是驱动的主体。每个struct input_dev代表一个输入设备。

程序清单 1.1  struct input_dev成员介绍

/* include/linux/input.h */

struct input_dev {

         constchar *name;                                                                                    /*设备名                                         */

         constchar *phys;                                                                                   

         constchar *uniq;

         structinput_id id;                                                                                    /*用于匹配事件处理层handler   */

 

         unsignedlong evbit[BITS_TO_LONGS(EV_CNT)];                            /*用于记录支持的事件类型的位图*/

         unsignedlong keybit[BITS_TO_LONGS(KEY_CNT)];                       /*记录支持的按键值的位图        */

         unsignedlong relbit[BITS_TO_LONGS(REL_CNT)];                         /*记录支持的相对坐标的位图   */

         unsignedlong absbit[BITS_TO_LONGS(ABS_CNT)];                       /*记录支持的绝对坐标的位图   */

         unsignedlong mscbit[BITS_TO_LONGS(MSC_CNT)];

         unsignedlong ledbit[BITS_TO_LONGS(LED_CNT)];                        /*led                                                 */

         unsignedlong sndbit[BITS_TO_LONGS(SND_CNT)];                       /*beep                                               */

         unsignedlong ffbit[BITS_TO_LONGS(FF_CNT)];

         unsignedlong swbit[BITS_TO_LONGS(SW_CNT)];

 

         unsignedint keycodemax;                                                                       /*支持的按键值的个数                */

         unsignedint keycodesize;                                                                        /*每个键值的字节数                              */

      void *keycode;                                                                                       /*存储按键值的数组首地址       */

         int(*setkeycode)(struct input_dev *dev, int scancode, int keycode);      /* 修改键值的函数,可选             */

         int(*getkeycode)(struct input_dev *dev, int scancode, int *keycode);    /* 获取扫描码的键值,可选         */

 

         structff_device *ff;

 

         unsignedint repeat_key;                                                                          /*最近一次按键值,用于连击     */

         structtimer_list timer;                                                                              /*自动连击计时器                         */

 

         intsync;                                                                                            /*最后一次同步后没有新的事件置1*/

 

         intabs[ABS_MAX + 1];                                                                      /* 当前各个坐标的值                            */

         int rep[REP_MAX + 1];                                                                         /*自动连击的参数              */

 

         unsigned longkey[BITS_TO_LONGS(KEY_CNT)];                           /*反映当前按键状态的位图        */

         unsignedlong led[BITS_TO_LONGS(LED_CNT)];                                      /*反映当前led状态的位图          */

         unsignedlong snd[BITS_TO_LONGS(SND_CNT)];                           /*反映当前beep状态的位图        */

         unsignedlong sw[BITS_TO_LONGS(SW_CNT)];                             

 

         intabsmax[ABS_MAX + 1];                                                                           /*记录各个坐标的最大值            */

         intabsmin[ABS_MAX + 1];                                                                  /*记录各个坐标的最小值            */

         intabsfuzz[ABS_MAX + 1];                                                                           /*记录各个坐标的分辨率            */

         intabsflat[ABS_MAX + 1];                                                                   /*记录各个坐标的基准值            */

 

         int(*open)(struct input_dev *dev);                                                /*打开函数                                     */

         void(*close)(struct input_dev *dev);                                                      /*关闭函数                                     */

         int(*flush)(struct input_dev *dev, struct file *file);                                /* 断开连接时冲洗数据                 */

         int(*event)(struct input_dev *dev, unsigned int type, unsigned int code, intvalue); /* 回调函数,可选       */

 

         structinput_handle *grab;

 

         spinlock_tevent_lock;

         structmutex mutex;

 

         unsignedint users;

         intgoing_away;

 

         structdevice dev;

 

         structlist_head    h_list;                                                                          /*handle链表                                  */  structlist_head    node;                                                                           /*input_dev链表                        */

};

       struct input_event是事件传送的载体,输入子系统的事件都是包装成struct input_event传给用户空间。各个成员如程序清单 1.2所示。

程序清单 1.2  struct input_event成员介绍

/* include/linux/input.h */

struct input_event {

         structtimeval time;                                                                                  /*时间戳                                         */

         __u16type;                                                                                             /*事件类型                                     */

         __u16code;                                                                                             /*事件代码                                     */

         __s32value;                                                                                            /*事件值,如坐标的偏移值        */

};

struct input_dev注册的时候需要跟匹配的hanlder建立连接,匹配的依据就是struct input_dev所包含的struct input_id。struct input_id的各个成员如程序清单 1.3所示。

程序清单 1.3  struct input_id成员描述

/* include/linux/input.h */

struct input_id {

         __u16bustype;                                                                                        /*总线类型                                     */

         __u16vendor;                                                                                         /*生产商编号                                 */

         __u16product;                                                                                        /*产品编号                                     */

         __u16version;                                                                                        /*版本号                                         */

};

三 、驱动程序例子

#include <linux/input.h>   
#include <linux/module.h>   
#include <linux/init.h>   
#include <linux/kernel.h>   
#include <linux/irq.h>   
#include <linux/interrupt.h>   
#include <mach/gpio.h>   
#include <mach/regs-gpio.h>   
#include <asm/irq.h>   
#include <asm/io.h>   
       
#define DEV_NAME         "KEY1"
#define DEV_NAME1     "KEY2"   
#define BUTTON_IRQ        IRQ_EINT0   
#define BUTTON_IRQ1        IRQ_EINT0   

static struct input_dev *button_dev;  
void delay()
{
    unsigned int i,j;
    for(i=5000;i>0;i--)
    {
        for(j=5000;j>0;j--);
    }
}

static irqreturn_t button_interrupt(int irq, void *p)  
{  
    /*get pin value <down 0, up 1> */  
    int val = gpio_get_value (S5PV210_GPH0(0));    // s3c2410_gpio_getpin(S3C2410_GPG(0));  
    /* val must a variable */  
    printk("Enter EIENT0 ! \n");
    input_report_key(button_dev,KEY_5, val);  
    input_sync(button_dev);
    
    input_report_key(button_dev,KEY_5, !val);  
    input_sync(button_dev);

delay();
    input_report_key(button_dev,KEY_LEFT, val);  
    input_sync(button_dev);
    
    input_report_key(button_dev,KEY_LEFT, !val);  
    input_sync(button_dev);

    return IRQ_RETVAL(IRQ_HANDLED);  
}

//--------------------------------------------------------------------------------------------
static int __init button_init(void)  
{  
    int err;  
    
    gpio_direction_input(S5PV210_GPH0(0));
    s3c_gpio_cfgpin(S5PV210_GPH0(0), S3C_GPIO_SFN(0x0000000f));    //设置为外部中断 eint0 ,参考GP0CON【】
    set_irq_type(IRQ_EINT0, IRQ_TYPE_EDGE_FALLING);    
    gpio_direction_input(S5PV210_GPH0(1));
    s3c_gpio_cfgpin(S5PV210_GPH0(1), S3C_GPIO_SFN(0x000000f0));    //设置为外部中断 eint1 ,参考GP0CON【】
    set_irq_type(IRQ_EINT1, IRQ_TYPE_EDGE_FALLING);        
           
    if(request_irq(BUTTON_IRQ, button_interrupt,IRQ_TYPE_EDGE_FALLING,DEV_NAME, button_dev))
    {
        input_free_device(button_dev);
        printk(KERN_ERR"cannot allocate irq0");  
        return- EBUSY;  
    }  


//--------------------------------------------------------------------------------------              
    button_dev= input_allocate_device();  
    if(button_dev == NULL)
    {  
        printk(KERN_ERR"not enough memory\n");  
        err= - ENOMEM;  
        goto err_free_irq;  
    }  
//-----------------------------------------------------------------------------------------
    button_dev->name = "Mini Button Optical Finger Navigation Module ";
printk("1");
    button_dev->phys = "OFN/OFN0";
printk("2");
    button_dev->id.bustype = BUS_HOST;
printk("3");
    button_dev->id.vendor = 0x0001;
printk("4");
    button_dev->id.product = 0x0001;
printk("5");
    button_dev->id.version = 0x0100;
printk("6");

//-----------------------------------------------------------------------------------------  
//    __set_bit(EV_REP, button_dev->evbit);
    set_bit(EV_KEY,button_dev->evbit);  
    set_bit(KEY_5,button_dev->keybit);
    set_bit(KEY_LEFT,button_dev->keybit);
//------------------------------------------------------------------------------------------

    err= input_register_device(button_dev);  
    if(err)
    {  
        printk(KERN_ERR"failed to register device\n");  
        goto err_free_dev;  
    }  
    printk("initialized\n");  
    return 0;  
       
    err_free_dev:  
        input_free_device(button_dev);  
        return err;
    err_free_irq:  
        free_irq(BUTTON_IRQ,NULL);  
        return err;  
}

static void __exit button_exit(void)  
{  
    input_unregister_device(button_dev);  
    free_irq(BUTTON_IRQ,NULL);  
}  

module_init(button_init);  
module_exit(button_exit);  

MODULE_LICENSE("GPL");  
MODULE_AUTHOR("light>");




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值