input子系统学习(一)

1、输入子系统框架



2、编写一个简单的设备驱动层代码

c 复制代码
#include<linux/module.h>
#include<linux/init.h>
#include<linux/input.h>
#include<linux/time.h>

struct input_dev *my_input_dev;


static void timer_function(struct timer_list *t);
DEFINE_TIMER(test_timer,timer_function);
static void timer_function(struct timer_list *t)
{
     static int value = 0;
     value = value?0:1;

     input_event(my_input_dev,EV_KEY,KEY_1,value);
     input_event(my_input_dev,EV_SYN,SYN_REPORT,0);
     //printk("in timer_function\n");
     mod_timer(&test_timer,jiffies_64+msecs_to_jiffies(1000));
}

static int my_input_dev_init(void)
{
     int ret;
     my_input_dev = input_allocate_device();

     if(!my_input_dev)
     {
          printk("input_allocate_device is error\n");
          return -1;
     }
     my_input_dev->name = "my_input_dev_test";
     __set_bit(EV_KEY,my_input_dev->evbit);
     __set_bit(EV_SYN,my_input_dev->evbit);
     __set_bit(KEY_1,my_input_dev->keybit);

     ret = input_register_device(my_input_dev);
     if(ret < 0)
     {
          printk("input_register_device is error\n");
          goto error;
     }

     test_timer.expires = jiffies_64 + msecs_to_jiffies(1000);
     add_timer(&test_timer);
     printk("my_input_dev_init ok\n");
     return 0;
error:
     printk("my_input_dev_init error\n");
     input_free_device(my_input_dev);
     return ret;
}

static void my_input_dev_exit(void)
{

     //struct input_dev *my_input_dev;
     input_unregister_device(my_input_dev);
     del_timer(&test_timer);
     printk("my_input_dev_exit ok\n");

}

module_init(my_input_dev_init);
module_exit(my_input_dev_exit);
MODULE_LICENSE("GPL");

编译成ko

放到开发板上加载

cat /proc/bus/input/devices

hexdump /dev/input/event11 查看上报的数据包

3、匹配规则和流程

c 复制代码
input_register_device
	input_attach_handler(dev, handler)
		input_match_device(handler, dev)
		//如果input_match_device返回id不为0,则调用connect函数
		handler->connect(handler, dev, id)

遍历链表,查找匹配的事件处理层input_handler

可以看到,有没有定义match函数都可以进行匹配

通用事件处理层input_handler

这里是可以和所有的input设备驱动进行匹配


先后注册dev和handler都可以进行匹配

c 复制代码
input_register_handler(&evdev_handler)
	input_attach_handler(dev, handler)
		input_match_device(handler, dev)
		//如果input_match_device返回id不为0,则调用connect函数
		handler->connect(handler, dev, id)

4、多对多的匹配关系


可以看到有四个handler和我的dev进行匹配

那为什么只生成一个设备节点呢?因为只有evdev中的connec函数注册了字符设备

可以看下kbd的connect函数

c 复制代码
grep "kbd" drivers/* -nwr



5、上报数据格式分析




所以一个input_event数据包所占大小为8+2+2+4=24字节

c 复制代码
tv_sec:e08f 667f 0000 0000 
tv_usec:c3ac 0000 0000 0000
type:0001 
code:0002 
value:0001 0000 

6、编写app获取type、code和value

c 复制代码
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<linux/input.h>

#define INPUT_DEV  "/dev/input/event11"
int main()
{
     struct input_event input_event;
     int ret;
     
     int fd = open(INPUT_DEV,O_RDWR);
     if(fd < 0)
     {
          printf("open input_dev error\n");
          return -1;
     }

     while(1)
     {
          ret = read(fd,&input_event,sizeof(struct input_event));
          if(ret < 0)
          {
               printf("read input_dev error\n");
               return -2;
          }
          printf("input_event.type = %04x,input_event.code = %04x,input_event.value = %08x\n",\
               input_event.type,input_event.code,input_event.value);
     }
     
     return 0;
}

交叉编译app

c 复制代码
/home/johan/share/rk3588/linux_sdk/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc app_test.c -o app_test



相关推荐
Winner13007 分钟前
Debian、Buildroot 和 Ubuntu 都是基于 Linux 的系统区别
linux·ubuntu·debian
1nullptr34 分钟前
UNIX程序设计基本概念和术语
linux
是阿建吖!1 小时前
【Linux | 网络】网络基础
linux·网络
mmoyula1 小时前
【RK3568 驱动开发:实现一个最基础的网络设备】
android·linux·驱动开发
阿杆1 小时前
😡同事查日志太慢,我现场教他一套 grep 组合拳!
linux·后端
墨小傲2 小时前
基于Linux下的vscode c/c++开发环境搭建详细教程
linux·c语言·vscode
cui_win2 小时前
【网络】Linux 内核优化实战 - net.ipv4.tcp_timestamps
linux·网络·tcp/ip
czhc11400756632 小时前
Linux 77 FTP
linux·运维·服务器
魂尾ac2 小时前
Centos Docker 安装(100%成功)
linux·docker·centos
天若有情6732 小时前
【Linux】02_CentOS 7 开机运行级别详解:从基础概念到实战配置
linux·运维·centos