虚拟鼠标设备驱动

虚拟鼠标设备驱动

#include   
#include 
#include 
#include 
#include 
#include 
#include  

static struct input_dev *vir_mouse_device = NULL;
int vir_mouse_open(struct input_dev *dev)
{
    return 0;
}

void vir_mouse_release(struct input_dev *dev)
{

}
static int vir_mouse_setup(void){
    int ret ;

    vir_mouse_device = input_allocate_device();

    if(IS_ERR(vir_mouse_device)){
        ret = PTR_ERR(vir_mouse_device);

        return ret;
    }
    vir_mouse_device->open = vir_mouse_open;
    vir_mouse_device->close = vir_mouse_release;
    vir_mouse_device->name = "virmouse";
    vir_mouse_device->phys = "virmouse/input0";

    set_bit(EV_KEY, vir_mouse_device->evbit);
    set_bit(EV_REL, vir_mouse_device->evbit);
    set_bit(EV_REP, vir_mouse_device->evbit);
    set_bit(REL_X, vir_mouse_device->relbit);
    set_bit(REL_Y, vir_mouse_device->relbit);
    set_bit(REL_WHEEL, vir_mouse_device->relbit);

    set_bit(BTN_LEFT,vir_mouse_device->keybit);
    set_bit(BTN_RIGHT,vir_mouse_device->keybit);
    set_bit(BTN_MIDDLE,vir_mouse_device->keybit);
    ret = input_register_device(vir_mouse_device);
    if(ret < 0)                                                            
    {                                                                           
        input_free_device(vir_mouse_device);
    }
    return ret;
}

static void vir_mouse_destroy(void){

    input_unregister_device(vir_mouse_device);
    input_free_device(vir_mouse_device);         
}

#define VIRMDEV_MAJOR 0//using dynamic major

static int virmouse_major = VIRMDEV_MAJOR;
#define device_name "virmouse"
typedef struct _tagvirmouse{
        struct cdev cdev;
}virmouseDev;

virmouseDev *pVirmouseDev = NULL;
struct class *virmouse_class;  

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

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

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

/*
*定义协议,写入到系统内部
*/

ssize_t virmouse_deve_write(struct file* filp, const char __user * buf, size_t count, loff_t* f_pos)
{
    int ret = 0;
    signed char mousebuf[4];

    signed int rel;
    int i;
    while(ret < count)
    {
        if(copy_from_user(mousebuf, buf + ret, sizeof(mousebuf)))
        {
            printk(KERN_ALERT "%s copy error \n",__func__);
            return -EFAULT;
        }

        for( i =0 ;i< count ;i++){
            printk(KERN_ALERT "buf[%d]:%#x ",i,mousebuf[i]);
        }
        printk("count: %d  \n",count);
        ret += sizeof(mousebuf);
        //rel = (signed int )mousebuf[1];
        input_report_key(vir_mouse_device, BTN_LEFT,   mousebuf[0] & 0x01);
        input_report_key(vir_mouse_device, BTN_RIGHT,  mousebuf[0] & 0x02);
        input_report_key(vir_mouse_device, BTN_MIDDLE, mousebuf[0] & 0x04);
        //input_report_key(vir_mouse_device, BTN_SIDE,   mousebuf[0] & 0x08); 
        //input_report_key(vir_mouse_device, BTN_EXTRA,  mousebuf[0] & 0x10);
        rel = (signed int )mousebuf[1];
        //printk("1-%d \n",rel);
        input_report_rel(vir_mouse_device, REL_X,     rel);

        rel = (signed int )mousebuf[2];
        //printk("2-%d \n",rel);
        input_report_rel(vir_mouse_device, REL_Y,     rel);
        rel = (signed int )mousebuf[3];
        //printk("3-%d \n",rel);
        input_report_rel(vir_mouse_device, REL_WHEEL, rel);
        input_sync(vir_mouse_device);

    }

    return ret;
}

static struct file_operations file_ops ={
    .owner = THIS_MODULE,
    .open    = virmouse_dev_open,
    .release = virmouse_dev_release,
    .read    = virmouse_dev_read,
    .write   = virmouse_deve_write,
};
static void virmouse_setup_cdev(virmouseDev* dev , int index){


    int err,devno=MKDEV(virmouse_major,index);
    cdev_init(&dev->cdev,&file_ops);
    dev->cdev.owner = THIS_MODULE;
    dev->cdev.ops= &file_ops;
    err= cdev_add(&dev->cdev,devno,1);
    if(err)
        printk(KERN_NOTICE "ERROR %d adding virmouse device",err);

}
static int __init  virmouse_device_init(void){
    int result;
    dev_t devno;
    devno = MKDEV(virmouse_major,0);
    if(virmouse_major){
        result = register_chrdev_region(devno,1,device_name);
    }
    else{
        result = alloc_chrdev_region(&devno,0,1,device_name);
        virmouse_major = MAJOR(devno);
    } 
    if(result<0){
        return result;
    }
    pVirmouseDev = kmalloc(sizeof(virmouseDev), GFP_KERNEL);
    if (!pVirmouseDev){ 
        result = -ENOMEM;
        goto fail_malloc;
    }
    memset(pVirmouseDev,0,sizeof(pVirmouseDev));

    virmouse_class = class_create(THIS_MODULE,device_name);  
    if(IS_ERR(virmouse_class)){ 
      printk("%s create class error\n",__func__);
      unregister_chrdev_region(devno, 1);
      return -1;  
    }  

    device_create(virmouse_class, NULL, devno, NULL, device_name);  
    virmouse_setup_cdev(pVirmouseDev,0);
    vir_mouse_setup();
    return 0;

fail_malloc:

    unregister_chrdev_region(devno, 1);
    return 0;

}

static void __exit  virmouse_device_exit(void){
    dev_t devno;
    devno = MKDEV(virmouse_major,0);
    cdev_del(&pVirmouseDev->cdev);
    device_destroy(virmouse_class,devno);  
    class_destroy(virmouse_class);
    kfree(pVirmouseDev);
    unregister_chrdev_region(MKDEV(virmouse_major,0),1);
    vir_mouse_destroy();

}

module_init(virmouse_device_init); 
module_exit(virmouse_device_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(嵌入式)