日期:2014-05-16 浏览次数:20864 次
#include <linux/init.h> #include <linux/module.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/device.h> #include <linux/spinlock.h> #include <linux/semaphore.h> #include <linux/input.h> #include<linux/slab.h> struct VirtualInputDevice { struct cdev dev; dev_t devno; int count; spinlock_t count_lock; struct semaphore io_lock; }; static struct VirtualInputDevice *virtual_input_devp; static struct class *dev_class; static struct input_dev *input_dev; static ssize_t virtual_input_dev_read(struct file *filp,char __user *user_buffer,size_t size,loff_t *ppos) { printk(KERN_ALERT"Read\n"); down_interruptible(&virtual_input_devp->io_lock); up(&virtual_input_devp->io_lock); return size; } static ssize_t virtual_input_dev_write(struct file *filp,const char __user *user_buffer,size_t size,loff_t *ppos) { //down_interruptible(&virtual_input_devp->io_lock); //input_report_key(input_dev,BTN_TOUCH,1); input_report_abs(input_dev,ABS_X,100); //input_report_abs(input_dev,ABS_Y,100); //input_report_abs(input_dev,REL_X,100); //input_report_abs(input_dev,REL_Y,100); //input_report_abs(input_dev,ABS_PRESSURE,0xff); input_sync(input_dev); printk(KERN_ALERT"Write\n"); //up(&virtual_input_devp->io_lock); return size; } static int virtual_input_dev_open(struct inode *inodno,struct file *filno) { printk(KERN_ALERT"OPEN DEVICE\n"); spin_lock(&virtual_input_devp->count_lock); virtual_input_devp->count++; spin_unlock(&virtual_input_devp->count_lock); return 0; } static int virtual_input_dev_release(struct inode *inodno,struct file *filno) { spin_lock(&virtual_input_devp->count_lock); virtual_input_devp->count--; spin_unlock(&virtual_input_devp->count_lock); return 0; } static const struct file_operations virtual_input_dev_fops= { .owner=THIS_MODULE, .read=virtual_input_dev_read, .write=virtual_input_dev_write, .open=virtual_input_dev_open, .release=virtual_input_dev_release, }; static int __init virtual_input_dev_init(void) { int result; virtual_input_devp=kmalloc(sizeof(struct VirtualInputDevice),GFP_KERNEL); if(!virtual_input_devp) { result= -ENOMEM; return result; } result=alloc_chrdev_region(&virtual_input_devp->devno,0,1,"VirtualInputDevice"); if(result<0) { kfree(virtual_input_devp); return result; } input_dev=input_allocate_device(); if(!input_dev) { kfree(virtual_input_devp); result= -ENOMEM; return result; } input_dev->name="VirtualInputDevice"; input_dev->phys="xxmous/input1"; input_dev->id.bustype=BUS_HOST; set_bit(EV_ABS,input_dev->evbit); //set_bit(EV_REL,input_dev->evbit); //set_bit(EV_KEY,input_dev->evbit); //set_bit(REL_X,input_dev->relbit); set_bit(ABS_X,input_dev->absbit); //set_bit(ABS_Y,input_dev->absbit); //set_bit(ABS_PRESSURE,input_dev->absbit); //set_bit(BTN_TOUCH,input_dev>keybit); bitmap_fill(input_dev->relbit,ABS_MAX); result=input_register_device(input_dev); if(result) { printk(KERN_ALERT"error in register input device\n"); input_free_device(input_dev); kfree(virtual_input_devp); result= -ENOMEM; return result; } cdev_init(&virtual_input_devp->dev,&virtual_input_dev_fops); virtual_