Input子系统学习笔记

本文详细介绍了Linux输入子系统的工作原理,包括驱动框架、设备分配、配置和注册过程,以及hs0038驱动的具体实现,强调了中断配合下获取和上报事件的重要性。
摘要由CSDN通过智能技术生成

        传统的驱动模型是自己构造file_operation结构体,完善其中的读、写等函数,但输入子系统的优点是别人已经给你写好了框架,你只需要分配、配置、注册即可。

        输入子系统分为游戏杆、鼠标和事件三种驱动程序(主要是使用事件类程序,通常需要配合中断使用)

        备注:输入子系统产生的设备节点在/dev/input/目录下,如果是事件类设备名称为eventn,当使用到输入子系统函数时候需要包含#include<linux\input.h>头文件,也可以参考evdev.c的程序,evdev.c中帮助你实现了file_operation结构体。

一、框架

        Input子系统的框架类似于linux驱动程序的框架,都是采用了分离,分层结构。分为上下两层,下层分为左右两边,一边是设备,一边是驱动,当满足一定条件时他们匹配。

二、相关函数

        Input子系统的思想是分配子系统、配置子系统、注册子系统:

        分配:Input_allocate_device()

                该函数会返回一个input_dev类型的指针,因此在使用该函数之前我们需要先定一个这样的指针,通过该函数得到分配。

        配置:__set_bit()。我们需要设置多次,首先我们需要设置该输入子系统支持什么样的事件如:按键类、位移类类等(  __set_bit(EV_KEY, 分配的结构体->evbit)其中evbit是指事件位

                其次我们需要配置支持事件下的那种事件,如按键类事件是支持哪几个按键,位移类事件是指支持哪些方向的?

                __set_bit(KEY_1,分配的结构体->keybit),这就是支持key1

        注册:分配、配置了结构体,那我们需要告诉系统啊,因此我们需要注册input_register_device()。这样一个完整的流程才结束。

        获取事件:(获取事件往往是配合中断的,比如我们配置一个按键驱动,我们是不是要当按键按下之后才获取事件呀)            input_event(子系统结构体,EV_KEY,value,1);其中的参数是子系统,事件类,具体种,值。

        当应用层read函数进行读取数值后,会调用evdev.c中的file_opereation函数中注册的read函数,该read函数是linux内核做好的,该函数在没有值的时候会进行休眠,当input_event时候会唤醒该函数,并且其最后一个参数会控制read是否休眠。1不休眠 0休眠。且该函数会直接把数据上报给应用层的read函数,即是一个input_event类型结构体其中包括time type code value。

三、流程

四、实例代码

        这是一个对hs0038驱动的代码

驱动部分:

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/dmi.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/of_gpio.h>
#include <asm/uaccess.h>
#include <linux/input.h>
static int GPIO_hs0038_irq;
static int major ;
static struct class *class ;
static struct gpio_desc *GPIO_hs0038;
static int hs0038_val = 0;
static int irq_cnt = 0;
static u64 irq_time[100];
static unsigned int  value;
static int r = 0;
static int w = 0;
static unsigned int circle_data[8];
static struct input_dev *hs0038_input_device;
static DECLARE_WAIT_QUEUE_HEAD(hs0038_wait);//创建名为gpio_key_wait的等待队列
/* 成功地唤醒一个被wait_event_interruptible()的进程,需要满足: 
  
   在 1)condition为真的前提下,2) 调用wake_up()/wake_up_interruptible()。
   可以这样理解wake_up函数仅仅是让休眠函数再判断一次condition是否满足
   
*/



static int parse_hs0038_data(unsigned int *value)
{
    int i,j;
    int m = 3;
    //由于是一个字节一个字节解析因此把其定义为char类型即一个字节
    unsigned char data[4];
    unsigned int last_data;
    for(i = 0; i<4;i++)
    {
        data[i] = 0;
        for(j=0;j<8;j++)
        {
            if(irq_time[m+1] - irq_time[m] > 1000000)
                data[i] |= (1<<j);
            m+=2;
        }
    }

    //解析后进行判断
    data[1] = ~data[1];
    data[3] = ~data[3];

    if(data[0]!=data[1])
    {
        return -1;
    }
    if(data[2]!=data[3])
    {
        return -1;
    }
    last_data = ((data[0]<<8)|data[2]);
    *value = last_data;
    return 0;
}
static irqreturn_t hs0038_handler(int irq, void *dev_id)
{
    int err ;
    irq_time[irq_cnt] = ktime_get_boot_ns();
    irq_cnt++;
    if (irq_cnt >= 2)
	{
		if (irq_time[irq_cnt-1] - irq_time[irq_cnt-2] > 40000000)
		{
			/* 超时 */
			irq_time[0] = irq_time[irq_cnt-1];
			irq_cnt = 1;			
			return IRQ_HANDLED; // IRQ_WAKE_THREAD;
		}
	}
    	
    if(irq_cnt >= 67)
    {
        //解析数据
        err = parse_hs0038_data(&value);
        if(err == 0)
        {
            
            //解析成功则irq_cnt清零
            irq_cnt = 0;
            //上报数据 
            input_event(hs0038_input_device,EV_KEY,value,1);
            input_event(hs0038_input_device,EV_KEY,value,0);
            //上报同步事件并唤醒read函数
            input_sync(hs0038_input_device);
        }   
    }
    return IRQ_HANDLED;
}



static int hs0038_dri_probe(struct platform_device *pdev)
{
    struct device_node *np;
    printk("in probe\r\n");
    np = pdev -> dev.of_node;
    if(!np)
    {
        printk("cant get node \r\n");
        return -1;
    }
    //获取具体硬件信息,申请中断处理函数
    GPIO_hs0038 = gpiod_get(&pdev->dev,"hs0038", GPIOD_IN);
	GPIO_hs0038_irq  = gpiod_to_irq(GPIO_hs0038);
    request_irq(GPIO_hs0038_irq ,hs0038_handler,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "hs0038",NULL);
    printk("GPIO_hs0038_irq %d\r\n",GPIO_hs0038_irq);

    //分配INPUT子系统
    hs0038_input_device = input_allocate_device();
    hs0038_input_device->name = "hs0038";

    //设置子系统
    //1.事件 按键 重复类
    __set_bit(EV_KEY, hs0038_input_device->evbit);
	__set_bit(EV_REP, hs0038_input_device->evbit);

    //2.哪些 所有按键
    memset(hs0038_input_device->keybit, 0xff, sizeof(hs0038_input_device->keybit));

    //3.注册
    input_register_device(hs0038_input_device);
    
    return 0;
}
static int hs0038_dri_remove(struct platform_device *pdev)
{
    printk("remove!!!\r\n");
    free_irq(GPIO_hs0038_irq,NULL);
    gpiod_put(GPIO_hs0038);
    input_unregister_device(hs0038_input_device);
    input_free_device(hs0038_input_device);
    return 0;
}



static const struct of_device_id hs0038_dri_of_match[] = {
	{ .compatible = "fly,hs0038" },
	{ /* Sentinel */ }
};

 
static struct platform_driver hs0038_dri = {
	.probe			= hs0038_dri_probe,
	.remove			= hs0038_dri_remove,
    .driver = {
        		.name		= "hs0038_dri",
                //of_match_table中的compatible匹配设备树
                //of_match_table是of_device_id类型
        		.of_match_table	= hs0038_dri_of_match,
	},

};



//入口函数
static int SR_501_module_init(void)
{
    //注册platform_drive结构体   
    platform_driver_register(&hs0038_dri);
    printk("hs0038_dri register!\r\n");
    return 0;
}

//出口函数
static void SR_501_module_exit(void)
{
    
    platform_driver_unregister(&hs0038_dri);
}
module_init(SR_501_module_init);
module_exit(SR_501_module_exit);
MODULE_LICENSE("GPL");


应用部分:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/input.h>


int main(int argc ,char ** argv)
{
    int fd;
    unsigned int  buff;
    char status=0;
    struct input_event hs0038_event;
    if(argc != 2)
    {
        printf("Usage: %s <dev> \r\n",argv[0]);
        return -1;
    }
    fd = open(argv[1],O_RDWR);
    while(1)
    {
        if(read(fd,&hs0038_event,sizeof(hs0038_event))==sizeof(hs0038_event))
        {
            printf("read buff :\r\n",buff);
            printf("code 0x%x,value 0x%x\r\n",hs0038_event.code,hs0038_event.value);
        }
        else
            printf("err read %d\r\n",sizeof(hs0038_event));
    }
    return 0;
}

五、总结

        在没引入输入子系统是时候我们需要自己注册字符驱动程序、注册class、注册设备、实现file_operation结构体,但我们引入之后我们只需要分配、配置、注册输入子系统即可,再配合中断服务函数进行数据解析、数据上报。

六、补充

        所有设备都是可以用输入子系统吗?

        这显然不是,我们认为鼠标、触摸屏、遥控器这样的设备为输入设备。比如eeprom就不被认为是输入设备,这很明显是一个存储设备。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值