linux 驱动模板

#include<linux/init.h>

#include<linux/module.h>

#include<linux/kernel.h>

#include<linux/slab.h>

#include<linux/fs.h>

#include<linux/errno.h>

#include<linux/types.h>

#include<linux/aio.h>

#include<linux/cdev.h>

#include<linux/wait.h>

#include<linux/sched.h>

MODULE_LICENCE("Dual BSD/GPL");

 

struct cdev hello_cdev;

dev_t hello_dev;

 

wait_queue_head_t hello_wait_queue;

 

int hello_open(struct inode *inode,struct file *filp){

    return 0;

    }

 

int hello_release(struct inode *inode,struct file *filp){

    return 0;

    }

 

ssize_t hello_read(struct file *filp,char *data,size_t count,loff_t *lop ){

    interruptible_sleep_on(&hello_wait_queue);

    return 0;

    }

 

ssize_t hello_write(struct file *filp,const char *data,size_t count,loff_t *lop){

    wake_up_interruptible(&hello_wait_queue);

    return 0;

    }

 

        struct file_operations hello_ops={

            .open=hello_open;

            .release=hello_release;

            .write=hello_write;

            .read=hello_read;

            };

 

 

 

static int helloworld_init(void){

    int ret;

    hello_dev=MKDEV(223,0);

    if((ret=register_chrdev_region(hello_dev,1,"hello"))<0){

        printk("module register error/n");

        return -1;

        }    

    cdev_init(&hello_cdev,&hello_ops);

    

    if((ret=cdev_add(&hello_cdev,hello_dev,1))<0){

        printk("cdev_add error/n");

        unregister_chrdev_region(hello_dev,1);

        return -1;

        }

    init_wait_queue_head(&hello_wait_queue);

    printk("<0>hello init/n");

    retyrn 0;

    }

    

 

 

static void helloworld_exit(void){

    cdev_del(&hello_cdev);

    unregister_chrdev_region(hello_dev,1);

    printk("<0>hello exit/n");

    }

 

 

 

 

module_init(helloworld_init);

module_exit(helloworld_exit);

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值