
答:
1 分配一个input_dev结构体
2 设置
3 注册
4 硬件相关的代码,比如在中断服务程序里上报事件
问:如何分配input_dev结构体?
答:使用input_allocate_device函数
input_dev结构体的重要成员
[cpp] view plain copy print
struct input_dev {
const char name;
const char phys;
const char uniq;
struct input_id id;
unsigned long evbit[NBITS(EV_MAX)]; // 表示能产生哪类事件
unsigned long keybit[NBITS(KEY_MAX)]; // 表示能产生哪些按键
unsigned long relbit[NBITS(REL_MAX)]; // 表示能产生哪些相对位移事件, x,y,滚轮
unsigned long absbit[NBITS(ABS_MAX)]; // 表示能产生哪些绝对位移事件, x,y
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)];
}
问:第二步的设置,应该怎么设置,应该设置什么?
答:举例,在此按键驱动里
[cpp] view plain copy print
/ 2设置 /
/ 21 设置按键能产生哪类事件 /
set_bit(EV_KEY,buttons_dev->evbit);
set_bit(EV_REP,buttons_dev->evbit);
/ 22 设置能产生这类 *** 作的哪些事件 /
set_bit(KEY_L,buttons_dev->keybit);
set_bit(KEY_S,buttons_dev->keybit);
set_bit(KEY_ENTER,buttons_dev->keybit);
set_bit(KEY_LEFTSHIFT,buttons_dev->keybit);
问:有哪些类呢?
答:在inputh里有以下类
[cpp] view plain copy print
#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)
问:如何注册?
答:使用input_register_device(struct input_dev dev)函数来注册
问:此按键驱动的硬件 *** 作包括哪些 *** 作?
答:申请定时器、申请中断 *** 作
驱动源码:
[cpp] view plain copy print
#include <linux/kernelh>
#include <linux/fsh>
#include <linux/inith>
#include <linux/delayh>
#include <linux/irqh>
#include <asm/uaccessh>
#include <asm/irqh>
#include <asm/ioh>
#include <linux/moduleh>
#include <linux/deviceh> //class_create
#include <mach/regs-gpioh> //S3C2410_GPF1
//#include <asm/arch/regs-gpioh>
#include <mach/hardwareh>
//#include <asm/hardwareh>
#include <linux/interrupth> //wait_event_interruptible
#include <linux/pollh> //poll
#include <linux/fcntlh>
#include <linux/inputh>
static struct pin_desc{
int irq;
unsigned char name;
unsigned int pin;
unsigned int key_val;
};
static struct pin_desc pins_desc[4] = {
{IRQ_EINT1,"K1",S3C2410_GPF1,KEY_L},
{IRQ_EINT4,"K2",S3C2410_GPF4,KEY_S},
{IRQ_EINT2,"K3",S3C2410_GPF2,KEY_ENTER},
{IRQ_EINT0,"K4",S3C2410_GPF0,KEY_LEFTSHIFT},
};
static struct pin_desc irq_pd;
static struct input_dev buttons_dev;
static struct timer_list buttons_timer;
/ 用户中断处理函数 /
static irqreturn_t buttons_irq(int irq, void dev_id)
{
irq_pd = (struct pin_desc )dev_id;
/ 修改定时器定时时间,定时10ms,即10秒后启动定时器
HZ 表示100个jiffies,jiffies的单位为10ms,即HZ = 10010ms = 1s
这里HZ/100即定时10ms
/
mod_timer(&buttons_timer, jiffies + (HZ /100));
return IRQ_HANDLED;
}
/ 定时器处理函数 /
static void buttons_timer_function(unsigned long data)
{
struct pin_desc pindesc = irq_pd;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
/ 松开 最后一个参数: 0-松开, 1-按下 /
input_event(buttons_dev,EV_KEY,pindesc->key_val,0);
input_sync(buttons_dev);
}
else
{
/ 按下 /
input_event(buttons_dev,EV_KEY,pindesc->key_val,1);
input_sync(buttons_dev);
}
}
/ 驱动入口函数 /
static int buttons_input_init(void)
{
int i;
/ 1分配一个input_dev结构体 /
buttons_dev = input_allocate_device();
/ 2设置 /
/ 21 设置按键能产生哪类事件 /
set_bit(EV_KEY,buttons_dev->evbit);
set_bit(EV_REP,buttons_dev->evbit);
/ 22 设置能产生这类 *** 作的哪些事件 /
set_bit(KEY_L,buttons_dev->keybit);
set_bit(KEY_S,buttons_dev->keybit);
set_bit(KEY_ENTER,buttons_dev->keybit);
set_bit(KEY_LEFTSHIFT,buttons_dev->keybit);
/ 3注册 /
input_register_device(buttons_dev);
/ 4硬件相关的设置 /
/ 41 定时器相关的 *** 作 /
init_timer(&buttons_timer);
buttons_timerfunction = buttons_timer_function;
add_timer(&buttons_timer);
/ 42 申请中断 /
for(i = 0;i < sizeof(pins_desc)/sizeof(pins_desc[0]);i++)
{
request_irq(pins_desc[i]irq, buttons_irq, IRQ_TYPE_EDGE_BOTH, pins_desc[i]name, &pins_desc[i]);
}
return 0;
}
/ 驱动出口函数 /
static void buttons_input_exit(void)
{
int i;
for(i = 0;i < sizeof(pins_desc)/sizeof(pins_desc[0]);i++)
{
free_irq(pins_desc[i]irq, &pins_desc[i]);
}
del_timer(&buttons_timer);
input_unregister_device(buttons_dev);
input_free_device(buttons_dev);
}
module_init(buttons_input_init); //用于修饰入口函数
module_exit(buttons_input_exit); //用于修饰出口函数
MODULE_AUTHOR("LWJ");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL"); //遵循GPL协议
测试步骤方法一:
[cpp] view plain copy print
[WJ2440]# ls
Qt first_test second_test
TQLedtest fourth_drvko sixth_drvko
app_test fourth_test sixth_test
bin home sixthdrvtest
buttons_all_drvko lib sys
buttons_all_test linuxrc third_drvko
buttons_inputko mnt third_test
dev opt tmp
driver_test proc udisk
etc root usr
fifth_drvko sbin var
fifth_test sddisk web
first_drvko second_drvko
[WJ2440]# ls /dev/event -l
crw-rw---- 1 root root 13, 64 Jan 2 06:04 /dev/event0
[WJ2440]# insmod buttons_inputko
input: Unspecified device as /devices/virtual/input/input1
[WJ2440]# ls /dev/event -l
crw-rw---- 1 root root 13, 64 Jan 2 06:04 /dev/event0
crw-rw---- 1 root root 13, 65 Jan 2 06:06 /dev/event1
[WJ2440]# cat /dev/tty1
[WJ2440]# cat /dev/tty1
ls
ls
输入cat /dev/tty1命令后,顺序按下K1,K2,K3则会显示ls
测试步骤方法二、
[cpp] view plain copy print
[WJ2440]# hexdump /dev/event1
0000000 b738 495d 8456 0007 0001 0026 0001 0000
0000010 b738 495d 846f 0007 0000 0000 0000 0000
0000020 b738 495d 2fb8 000a 0001 0026 0000 0000
0000030 b738 495d 2fc7 000a 0000 0000 0000 0000
分析:
hexdump /dev/event1 (open(/dev/event1), read(), )
秒 微秒 类 code value
0000000 0bb2 0000 0e48 000c 0001 0026 0001 0000
0000010 0bb2 0000 0e54 000c 0000 0000 0000 0000
0000020 0bb2 0000 5815 000e 0001 0026 0000 0000
0000030 0bb2 0000 581f 000e 0000 0000 0000 0000
[cpp] view plain copy print
struct input_event {
struct timeval time; //时间
__u16 type; //类
__u16 code; //类下事件的值
__s32 value; //0-松开, 1-按下,2-重复
};
struct timeval {
__kernel_time_t tv_sec; //秒
__kernel_suseconds_t tv_usec; //微秒
};
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)