虚拟键盘驱动程序

 

这个是很久之前我就想解决的问题了
以前开发板上的按键,都不是真正意义上,近似USB键盘的效果,根本不能作为Qt等图形界面的输入
看ELDD终于讲到了input子系统

虚拟键盘驱动程序_第1张图片

从图中可以看出,靠直接读写 /dev/input/ 目录下的设备节点是不合理的,而我以前只能想到这点
http://blog.sina.com.cn/s/blog_4673bfa50100b7jf.html
但是看ELDD自己折腾了半天,又看源码文档给的例子,更是恶心
还是从http://blog.csdn.net/absurd/archive/2009/09/13/4549514.aspx 获得了实现实例
驱动代码:
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>   
#include <linux/aio.h>
#include <asm/uaccess.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/input.h>
int vkeyboard_major = 201;
module_param(vkeyboard_major, int, 0);
MODULE_AUTHOR("Li XianJing <[email protected]>");
MODULE_LICENSE("GPL");

static struct input_dev* vkeyboard_idev = NULL;
static int vkeyboard_input_dev_setup(void);
int vkeyboard_open(struct inode* inode, struct file* filp)
{
return 0;
}

int vkeyboard_release(struct inode* inode, struct file* filp)
{
return 0;
}

ssize_t vkeyboard_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos)
{
printk(KERN_INFO"%s\n", __func__);
return count;
}

struct keyboard_event
{
int press;
int key;
};

ssize_t vkeyboard_write(struct file* filp, const char __user * buf, size_t count, loff_t* f_pos)
{
int ret = 0;
struct keyboard_event event;

while(ret < count)
{
   if(copy_from_user(&event, buf + ret, sizeof(event)))
   {
    return -EFAULT;
   }
   ret += sizeof(event);

   input_event(vkeyboard_idev, EV_MSC, MSC_SCAN, event.key);
   input_report_key(vkeyboard_idev, event.key, event.press);
   input_sync (vkeyboard_idev);

   printk(KERN_INFO"%s p=%d key=%d with scan code\n", __func__, event.press, event.key);
}

return ret;
}

static struct file_operations vkeyboard_fops =
{
.owner = THIS_MODULE,
.open    = vkeyboard_open,
.release = vkeyboard_release,
.read    = vkeyboard_read,
.write   = vkeyboard_write,
};

static int __init vkeyboard_init(void)
{
int result = register_chrdev(vkeyboard_major, "vkeyboard", &vkeyboard_fops);
vkeyboard_input_dev_setup();

return result;
}

static void __exit vkeyboard_cleanup(void)
{
input_unregister_device(vkeyboard_idev);
unregister_chrdev(vkeyboard_major, "vkeyboard");

return;
}

module_init(vkeyboard_init);
module_exit(vkeyboard_cleanup);

static int vkeyboard_input_dev_open(struct input_dev* idev)
{
printk(KERN_INFO"%s\n", __func__);

return 0;
}

static void vkeyboard_input_dev_close(struct input_dev* idev)
{
printk(KERN_INFO"%s\n", __func__);

return;
}

static int vkeyboard_input_dev_setup(void)
{
int ret = 0;
int i = 0;
vkeyboard_idev = input_allocate_device();

if(vkeyboard_idev == NULL)
{
   return -ENOMEM;
}

vkeyboard_idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_MSC);
vkeyboard_idev->mscbit[0] = BIT_MASK(MSC_SCAN) | BIT_MASK(MSC_SERIAL) | BIT_MASK(MSC_RAW);

bitmap_fill(vkeyboard_idev->keybit, KEY_MAX);
bitmap_fill(vkeyboard_idev->relbit, REL_MAX);
bitmap_fill(vkeyboard_idev->absbit, ABS_MAX);               

vkeyboard_idev->name = "vkeyboard";
vkeyboard_idev->phys = "vkeyboard/input0";
vkeyboard_idev->open = vkeyboard_input_dev_open;
vkeyboard_idev->close = vkeyboard_input_dev_close;

for(i = 32; i < KEY_MAX; i++)
{
   input_set_capability(vkeyboard_idev, EV_KEY, i);
}
__set_bit(EV_KEY, vkeyboard_idev->evbit);

ret = input_register_device(vkeyboard_idev);

return ret;
}

用户端代码

#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <linux/input.h>

#define DEVICE_NAME "/dev/vkeyboard"

struct keyboard_event
{
int press;
int key;
};

int main(void)
{
struct keyboard_event event;
int fd;
int i=0;
event.key=KEY_UP;
fd=open(DEVICE_NAME,O_RDWR|O_NDELAY);
if(fd<0)
{
   perror("open error");
   exit(-1);
}
while(1)
{
   //read(fd,event,1);
   event.press=1;
   write(fd,&event,sizeof(event));
   event.press=0;
   write(fd,&event,sizeof(event));
   sleep(1);
  
   //sleep(1);
   printf("%d \n",i++);
}
close(fd);
return 0;
}

这里仅仅是输入一个 上箭头
你要什么可以自己去 /linux/input.h 里寻找


你可能感兴趣的:(struct,user,Module,File,input,keyboard)