字符驱动之按键(一:无脑轮询法)

一、添加头文件

二、构造fileoperation结构体

    static struct fileoperations second_drv_fops = {
    
      .owner = THIS_MODULE;
    
      .open  = second_drv_open;
    
      .write = ;second_drv_write
    
    }

三、填充file_operations中的成员函数
static int second_drv_open(struct inode *inode,struct file *file)
{
    



    return 0;
}

ssize_t second_drv_read(struct file *file,char __user *buf,size_t size,loff_t *ppos)
{


    return 0;
}


还有初始化函数

static int second_drv_init(void)
{
    major = register_chrdev(0,"second_drv",&second_drv_fops);
    return 0;
}

static int second_drv_exit(void)
{
    unregister_chrdev(major,"second_drv");
    return 0;
}


module_init(second_drv_init);
module_exit(second_drv_exit);


要使用udev(mdev机制)根据信息来创造设备节点
    先创建一个class,再创建一个设备。
定义两个结构体:
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;

static int second_drv_init(void)
{
//创建一个类,在类下面创建一个设备。
//mdev是udev的简化版本
    major = register_chrdev(0,"second_drv",&second_drv_fops);
    seconddrv_class = class_create(THIS_MODULE,"seconddrv");
    seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");
    return 0;
}

static void second_drv_exit(void)
{
    unregister_chrdev(major,"first_drv");

    class_device_unregister(seconddrv_class_dev);
    class_destroy(seconddrv_class);
}

 

posted @ 2018-01-23 12:16  梦提三尺剑  阅读(139)  评论(0编辑  收藏  举报