09_输入子系统

总结

这里学的不是很透彻,只是知道和设备树没太大关系,用中断实验的设备节点就好
用中断实验的驱动进行创建和 输入设备结构体并且初始化 在有中断的时候上报一下就没了
在07_按键中断驱动中 初始化的时候找个地方 申请一个输入子系统结构体 用系统提供的api进行简单初始化
中断处理函数中使用 input_event()上报输入事件,后续交给系统的input核心层进行事件的分发就行

输入子系统

核心层

  • 创建input设备类 /sys/class/input ,并且提供各种函数

  • 根据输入设备种类、 分发事件到不同事件处理器
    事件处理层
    事件处理器帮助创建设备文件 /dev/input/event
    提供具体设备的操作接口,为输入设备(struct input_dev)创建具体设备文件

  • 通用事件处理器(drivers/input/evdev.c)

  • 鼠标事件处理器(drivers/input/mousedev.c)

  • 摇杆事件处理器(drivers/input/joydev.c)

核心层创建
linux开机后直接运行
input_init()函数

struct class input_class = {
	.name		= "input",
	.devnode	= input_devnode,
};

static int __init input_init(void)
	err = class_register(&input_class); //注册class类
	err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),  //创建/proc/devices  主设备号13
				      INPUT_MAX_CHAR_DEVICES, "input");
驱动中去填充一个input_dev结构体
struct input_dev {
	const char *name;  //输入设备的 设备名
	const char *phys;
	const char *uniq;
	struct input_id id; //指定输入设备的特征 厂家 版本号

	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];

	unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; //支持输入事件(按键,相对位移,绝对位移
	unsigned long keybit[BITS_TO_LONGS(KEY_CNT)]; //支持哪些具体按键,用宏表示
	unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
	unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
	unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
	unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
	unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
	unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
	unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
	...
	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, int value);
	...
	struct device dev;  //继承linux设备模型的dev

	struct list_head	h_list;
	struct list_head	node;

	unsigned int num_vals;
	unsigned int max_vals;
	struct input_value *vals;

	bool devres_managed;
};
驱动中注册填充好的input_dev结构体
input_allocate_device()函数

drivers/input/input.c

分配并初步初始化 input_dev 结构体变量

struct input_dev *input_allocate_device(void)
input_register_device()函数

drivers/input/input.c

向系统注册输入设备

int input_register_device(struct input_dev *dev)
input_unregister_device()函数

drivers/input/input.c

向系统注释输入设备

void input_unregister_device(struct input_dev *dev)
input_free_device()函数

drivers/input/input.c

释放 input_dev 结构体变量

void input_free_device(struct input_dev *dev)
驱动中产生中断的时候需要上报数据给系统
input_event()函数

drivers/input/input.c

通用事件上报接口

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

参数:

  • dev:需要上报信息的输入设备

  • type:上报的具体输入事件类型

    • 按键输入类型:EV_KEY

    • 坐标输入类型:EV_REL、EV_ABS

    • 特殊类型:EV_SYN

      • 同步事件:通知用户空间的程序接收消息
  • code:记录输入事件类型中的具体事件

    • 键盘发生按键输入类型事件时,记录键盘那个值被按下

  • value:具体事件的对应值

    • 按键按下,value值为1;按键松开,value值为0

返回值:无

input_report_key()函数

include/linux/input.h

按键事件上报接口

static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_KEY, code, !!value);
}
input_report_rel()函数

include/linux/input.h

相对坐标事件上报接口

static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_REL, code, value);
}
input_report_abs()函数

include/linux/input.h

绝对坐标事件上报接口

static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_ABS, code, value);
}
input_sync()函数

include/linux/input.h

同步事件上报接口

static inline void input_sync(struct input_dev *dev)
{
	input_event(dev, EV_SYN, SYN_REPORT, 0);
}

详细代码

/dev/input/event0 ,1,2如何识别 是对应哪个驱动呢

/dev/input/event0 ,1,2如何识别 是对应哪个驱动呢

查看输入设备名和event对应关系
cat /proc/bus/input/devices 记录了设备文件和设备节点关系
I: Bus=0019 Vendor=0000 Product=0001 Version=0000
N: Name=“Power Button” //驱动里设置的名字
P: Phys=LNXPWRBN/button/input0
S: Sysfs=/devices/LNXSYSTM:00/LNXPWRBN:00/input/input0
U: Uniq=
H: Handlers=kbd event0 //这里表示为event0
B: PROP=0
B: EV=3
B: KEY=10000000000000 0

代码

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>

#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>

#include "input_sub_system.h"

#include <linux/input.h>
#include <linux/semaphore.h>
#include <asm/uaccess.h>

struct device_node *button_device_node = NULL; //定义按键设备节点结构体
unsigned button_GPIO_number = 0;			   //保存button使用的GPIO引脚编号
u32 interrupt_number = 0;					   // button 引脚中断编号

/*input 子系统相关内容*/
#define BUTTON_NAME "button"
struct input_dev *button_input_dev = NULL; //定义按键对应的输入子系统结构体


/*按键中断处理函数*/
static irqreturn_t button_irq_hander(int irq, void *dev_id)
{
	int button_satus = 0;

	/*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
	button_satus = gpio_get_value(button_GPIO_number);
	if(0 == button_satus)
	{
		input_report_key(button_input_dev, KEY_1, 0);
		input_sync(button_input_dev);
	}
	else
	{
		input_report_key(button_input_dev, KEY_1, 1);
		input_sync(button_input_dev);
	}
	
	return IRQ_HANDLED;
}


/*
*驱动初始化函数
*/
static int __init button_driver_init(void)
{

	int error;
	printk(KERN_ERR"button_driver_init \n");

	/*获取按键 设备树节点*/
	button_device_node = of_find_node_by_path("/button_interrupt");
	if (NULL == button_device_node)
	{
		printk(KERN_ERR "of_find_node_by_path error!");
		return -1;
	}

	/*获取按键使用的GPIO*/
	button_GPIO_number = of_get_named_gpio(button_device_node, "button_gpio", 0);
	if (0 == button_GPIO_number)
	{
		printk(KERN_ERR"of_get_named_gpio error");
		return -1;
	}

	/*申请GPIO  , 记得释放*/
	error = gpio_request(button_GPIO_number, "button_gpio");
	if (error < 0)
	{
		printk(KERN_ERR "gpio_request error");
		gpio_free(button_GPIO_number);
		return -1;
	}

	error = gpio_direction_input(button_GPIO_number); //设置引脚为输入模式

	/*获取中断号*/
	interrupt_number = irq_of_parse_and_map(button_device_node, 0);
	printk(KERN_ERR"\n interrupt_number =  %d \n", interrupt_number);

	/*申请中断, 记得释放*/
	error = request_irq(interrupt_number, button_irq_hander, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "button_interrupt", NULL);
	if (error != 0)
	{
		printk(KERN_ERR "request_irq error");
		gpio_free(button_GPIO_number);
		free_irq(interrupt_number, NULL);
		return -1;
	}

	/*申请输入子系统结构体*/
	button_input_dev = input_allocate_device();
	if (NULL == button_input_dev)
	{
		printk(KERN_ERR "input_allocate_device error");
		return -1;
	}
	button_input_dev->name = BUTTON_NAME;

	/*设置要使用的输入事件类型*/
	button_input_dev->evbit[0] = BIT_MASK(EV_KEY);
	input_set_capability(button_input_dev, EV_KEY, KEY_1); //标记设备能够触发的事件

	/*注册输入设备*/
	error = input_register_device(button_input_dev);
	if (0 != error)
	{
		printk(KERN_ERR "input_register_device error");
		gpio_free(button_GPIO_number);
		free_irq(interrupt_number, NULL);
		input_unregister_device(button_input_dev);
		return -1;
	}

	return 0;
}


/*
*驱动注销函数
*/
static void __exit button_driver_exit(void)
{
	printk(KERN_ERR "button_driver_exit\n");

	/*释放申请的引脚,和中断*/
	gpio_free(button_GPIO_number);
	free_irq(interrupt_number, NULL);
	
	/*释放输入子系统相关内容*/
	input_unregister_device(button_input_dev);
}


module_init(button_driver_init);
module_exit(button_driver_exit);

MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值