Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411815
  • 博文数量: 73
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 785
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-20 12:26
文章分类

全部博文(73)

文章存档

2013年(4)

2012年(10)

2011年(32)

2010年(27)

分类: LINUX

2010-11-03 13:47:31

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

MODULE_AUTHOR("sky Mei ");
MODULE_DESCRIPTION("Virtual Mouse");
MODULE_LICENSE("GPL");

static int SmiMouse_Open(struct inode *inode,struct file *filp);
static int SmiMouse_Close(struct inode *inode,struct file *filp);
static ssize_t SmiMouse_Read(struct file *filp,char *buffer,size_t length,loff_t * offset);
static ssize_t SmiMouse_Write(struct file *filp, const char *buff, size_t len, loff_t * off);

static int ISmiMouse_Open(struct input_dev *idev);
static void ISmiMouse_Close(struct input_dev *idev);
static int ISmiMouse_Init(void);
static void ISmiMouse_Exit(void);

static struct file_operations SmiMouse_fops={
    .owner   = THIS_MODULE,
    .open    = SmiMouse_Open,
    .release = SmiMouse_Close,
    .read    = SmiMouse_Read,
    .write   = SmiMouse_Write,
};
struct mouse_event{
    int press,x,y;
};
int SmiMouse_Major=0;
static struct input_dev *ISmiMouse_dev=NULL;
struct class *SmiClass=NULL;
//static struct i2c_adapter *SystemUart = NULL;
//module_param(SmiMouse_Major,int,0);
MODULE_AUTHOR("SKY MEI!");
MODULE_LICENSE("GPL");
#define DEVICE_NAME "SmiMouse"
static int SmiMouse_Open(struct inode *inode,struct file *filp){
    printk(KERN_INFO "SmiMouse device open!\n");
    return 0;
}

static int SmiMouse_Close(struct inode *inode,struct file *filp){
    printk(KERN_INFO "SmiMouse device close!\n");
    return 0;
}

static ssize_t SmiMouse_Read(struct file *filp,    /* see include/linux/fs.h   */
               char *buff,    /* buffer to fill with data */
               size_t len,    /* length of the buffer     */
               loff_t * off){
    
    printk(KERN_INFO "SmiMouse read!\n");
    return 0;   
}
static ssize_t SmiMouse_Write(struct file *filp, const char *buff, size_t len, loff_t * off){
    int ret=0;
    struct mouse_event event;
    if(copy_from_user(&event,buff,sizeof(event))){
        return -EFAULT;
    }
    input_report_abs(ISmiMouse_dev,ABS_X,event.x&0x7FF);
    input_report_abs(ISmiMouse_dev,ABS_Y,event.y&0x7FF);
    //input_report_key(ISmiMouse_dev,BTN_TOUCH,event.press&0xFF);
    input_sync(ISmiMouse_dev);
    printk(KERN_INFO "SmiMouse: write press: %d %4x %4x\n", event.press,event.x,event.y);
    return ret;
}

static int __init SmiMouse_Init(void){
    int ret=0;
    SmiMouse_Major =register_chrdev(SmiMouse_Major,DEVICE_NAME,&SmiMouse_fops);
    if (SmiMouse_Major < 0) {
        printk(KERN_ALERT "SmiMouse: Registerdevice failed with%d\n", SmiMouse_Major);
        return SmiMouse_Major;
    }
    SmiClass = class_create(THIS_MODULE,DEVICE_NAME);
        device_create(SmiClass, NULL, MKDEV(SmiMouse_Major,0),NULL,DEVICE_NAME);
    printk(KERN_INFO "SmiMouse: Register device success SmiMouse_Major=%d!\n",SmiMouse_Major);
    ret=ISmiMouse_Init();
    return ret;
}
static void __exit SmiMouse_Exit(void){
    ISmiMouse_Exit();
    device_destroy(SmiClass,MKDEV(SmiMouse_Major,0));
        class_unregister(SmiClass);
        class_destroy(SmiClass);
    unregister_chrdev(SmiMouse_Major,DEVICE_NAME);
    printk(KERN_INFO "SmiMouse: unRegister device success!\n");
       
}
module_init(SmiMouse_Init);
module_exit(SmiMouse_Exit);
//-----------------------------------------------------------------------------------------   
static int  ISmiMouse_Open(struct input_dev *idev){
    printk(KERN_INFO "ISmiMouse: open!\n");
    return 0;
}

static void ISmiMouse_Close(struct input_dev *idev){
    printk(KERN_INFO "ISmiMouse: close!\n");
    return;
}

static int  ISmiMouse_Init(void){
    int  ret=0;

    ISmiMouse_dev = input_allocate_device();
    if (!ISmiMouse_dev) {
        printk(KERN_ERR "ISmiMouse: Not enough memory\n");
        ret = -ENOMEM;
    }

    ISmiMouse_dev->evbit[0] =BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
    ISmiMouse_dev->absbit[0]=BIT_MASK(ABS_X) | BIT_MASK(ABS_Y);
    ISmiMouse_dev->absmax[ABS_X]=0x7ff;
    ISmiMouse_dev->absmax[ABS_Y]=0x7ff;
    set_bit(BTN_0,ISmiMouse_dev->keybit);
    //ISmiMouse_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
   
    //input_set_abs_params(ISmiMouse_dev, ABS_X, 0, 0x7ff, 0, 0);
    //input_set_abs_params(ISmiMouse_dev, ABS_Y, 0, 0x7ff, 0, 0);
    //input_set_abs_params(ISmiMouse_dev, ABS_PRESSURE, 0, 1, 0, 0);
    //ISmiMouse_dev->name="SmiMouse";
    //ISmiMouse_dev->phys="SmiMouse/input0";
    //ISmiMouse_dev->open=ISmiMouse_Open;
    //ISmiMouse_dev->close=ISmiMouse_Close;
    /*__set_bit(EV_ABS, ISmiMouse_dev->evbit);
    __set_bit(EV_KEY, ISmiMouse_dev->evbit);
    __set_bit(ABS_X, ISmiMouse_dev->absbit);
    __set_bit(ABS_Y,ISmiMouse_dev->absbit);
    __set_bit(ABS_PRESSURE,ISmiMouse_dev->absbit);
    __set_bit(BTN_TOUCH, ISmiMouse_dev->keybit);*/
   
    ret = input_register_device(ISmiMouse_dev);
    if (ret) {
        printk(KERN_ERR "ISmiMouse: Failed to register device\n");
        input_free_device(ISmiMouse_dev);
        return ret;
    }
    printk("ISmiMouse: Register device success!\n");
    return ret;
}

static void  ISmiMouse_Exit(void){
        input_unregister_device(ISmiMouse_dev);
    printk(KERN_INFO "ISmiMouse: unregister device success!\n");
}

 
阅读(946) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Linux 内核驱动--简单的Module驱动Makefile

给主人留下些什么吧!~~