Linux 触摸屏驱动程序设计

一 。输入子系统模型解析

1. 为什么需要输入子系统

  完成一个设备驱动基本上需要三步

  1.注册一个字符设备模型

  2.open 或者read对用户程序的操作

  3.对不同的硬件设备进行操作

   但这时候为求方便发现了一个不同硬件的共性 所以引进了输入子系统的概念

2.输入子系统模型的概述

  

3. 输入子系统模型的案例分析

    key.c

    

#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/input.h>


#define GPFCON 0x56000050
#define GPFDAT 0x56000054

struct work_struct *work1;

struct timer_list buttons_timer;

unsigned int *gpio_data;

unsigned int key_num = 0;

wait_queue_head_t key_q;

struct input_dev *button_dev;

void work1_func(struct work_struct *work)
{
mod_timer(&buttons_timer, jiffies + (HZ /10));
}

void buttons_timer_function(unsigned long data)
{
unsigned int key_val;

key_val = readw(gpio_data)&0x1;
if (key_val == 0)
{
key_num = 4;
input_report_key(button_dev,KEY_4,1);
}

key_val = readw(gpio_data)&0x4;
if (key_val == 0)
{
key_num = 3;
input_report_key(button_dev,KEY_3,1);
}
input_sync(button_dev);

}


irqreturn_t key_int(int irq, void *dev_id)
{
//1. 检测是否发生了按键中断

//2. 清除已经发生的按键中断

//3. 提交下半部
schedule_work(work1);

//return 0;
return IRQ_HANDLED;

}

void key_hw_init()
{
unsigned int *gpio_config;
unsigned short data;

gpio_config = ioremap(GPFCON,4);
data = readw(gpio_config);
data &= ~0b110011;
data |= 0b100010;

writew(data,gpio_config);

gpio_data = ioremap(GPFDAT,4);

}


static int button_init()
{
int ret;

/*分配输入型设备结构*/
button_dev = input_allocate_device();

/*申明所支持的事件类型*/
set_bit(EV_KEY,button_dev->evbit);

/*申明可能上报的键编号*/
set_bit(KEY_3,button_dev->keybit);
set_bit(KEY_4,button_dev->keybit);

/*注册输入型设备*/
input_register_device(button_dev);

//注册中断处理程序
request_irq(IRQ_EINT0,key_int,IRQF_TRIGGER_FALLING,"key",(void *)4);
request_irq(IRQ_EINT2,key_int,IRQF_TRIGGER_FALLING,"key",(void *)3);

//按键初始化
key_hw_init();

//. 创建工作
work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work1, work1_func);

/* 初始化定时器 */
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;

/* 向内核注册一个定时器 */
add_timer(&buttons_timer);

/*初始化等待队列*/
init_waitqueue_head(&key_q);

return 0;

}


static void button_exit()
{
input_unregister_device(button_dev);
}


module_init(button_init);
module_exit(button_exit);

    key_app.c

*
* Buttons Example for Matrix V
*
* Copyright (C) 2004 capbily - friendly-arm
* capbily@hotmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <linux/input.h>
int main(void)
{
int buttons_fd;
int key_value,i=0,count;

struct input_event ev_key;
buttons_fd = open("/dev/event1", O_RDWR);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}

for (;;) {
count = read(buttons_fd,&ev_key,sizeof(struct input_event));
// printf("count=%d ",count);
for(i=0; i<(int)count/sizeof(struct input_event); i++)
if(EV_KEY==ev_key.type)
printf("type:%d,code:%d,value:%d ", ev_key.type,ev_key.code-1,ev_key.value);
if(EV_SYN==ev_key.type)
printf("syn event ");

}

close(buttons_fd);
return 0;
}

原文地址:https://www.cnblogs.com/lvxiaoning/p/6392856.html