• 大小: 23KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-28
  • 语言: 其他
  • 标签: 代码  linux  

资源简介

很好的东西和大家分享,是基于linux2.6内核的。很好用的!

资源截图

代码片段和文件信息

//模块定义
#include 
#include 
#include 
#include 
#include 
#include 
#include 
MODULE_LICENSE(“GPL“);

static int chdr_major = 150;//主设备号
static int chdr_minor = 0;//次设备号
static int chdr_nr_devs = 1;
static int open_nr=0;//打开设备的进程数
static struct cdev *chdr_cdev;//设备定义声明
static char buffer[100];//缓冲区


//函数声明
static int chdr_open(struct inode *inode struct file *filp);
static int chdr_release(struct inode *inode struct file* filp);
static ssize_t chdr_read(struct file *file char __user *buf size_t count loff_t *f_pos);
static ssize_t chdr_write(struct file *file const char __user *buf size_t count loff_t *f_pos);

//设备模块接口
static struct file_operations chdr_fops = {
    .owner   = THIS_MODULE
    .read    = chdr_read
    .write   = chdr_write
    .open    = chdr_open
    .release = chdr_release
};   


//打开函数
static int chdr_open(struct inode *inode struct file *filp)
{
    if (open_nr == 0) {

        open_nr++;
    } else {
        printk(KERN_ALERT “Another process open the char device.\n“);//进程挂起
        return -1;
    }

    return 0;
}

//阅读函数
static ssize_t chdr_read(struct file *file char __user *buf size_t count loff_t *f_pos)
{
    if (buf == NULL)
        return 0;
    if (count > 100)
        count = 100;

    copy_to_user(buf buffer count);//读缓冲
    return count;
}

//写入函数
static ssize_t chdr_write(struct file *file const char __user *buf size_t count loff_t *f_pos)
{
    if (buf == NULL)
        return 0;
    if (count > 100)
        count = 100;

    copy_from_user(buffer buf count);//写缓冲
    return count;
}

//释放设备函数
static int chdr_release(struct inode *inode struct file* filp)
{
    open_nr--;//进程数减1
    return 0;
}

//注册设备函数
static int __init chdr_init(void)
{
    int result err;
    dev_t dev = 0;

    printk(KERN_ALERT “Begin to init Char Device!\n%d“open_nr);
//注册设备
    if (chdr_major) {
        dev = MKDEV(chdr_major chdr_minor);
        result = register_chrdev_region(dev chdr_nr_devs “chdr“);
    } else {
        result = alloc_chrdev_region(&dev chdr_minor chdr_nr_devs “chdr“);
        chdr_major = MAJOR(dev);
    }

    if (result < 0) {
        printk(KERN_WARNING “chdr: cannot get major %d\n“ chdr_major);
        return result;
    }   
   
    chdr_cdev = cdev_alloc();
    if (chdr_cdev == NULL) {
        printk(KERN_WARNING “chdr: alloc cdev failed.\n“);

        return -1;
    }

//初始化设备
    chdr_cdev->owner = THIS_MODULE;
    chdr_cdev->ops = &chdr_fops;
    cdev_init(chdr_cdev &chdr_fops);
    err = cdev_add(chdr_cdev dev 1);
    if (err) {
        printk(KERN_NOTICE “Error %d adding chdr.\n“ err);
    }

    open_nr = 0;
printk(“%d“open_nr);
    return 0;
}


//注销设备函数
static void __exit chdr_exit(void)
{
    dev_t dev;

    printk(KERN_ALERT “Unloading...\n“);
    dev = MKDEV(chdr_major chdr_minor);
    unregister_chrdev_region(dev chdr_nr_devs);//注销设备
    cdev_del(chdr_cd

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        206  2009-06-03 13:21  驱动设计\Makefile

     文件       3294  2009-06-03 13:31  驱动设计\mydrive.c

     文件        875  2009-06-03 13:32  驱动设计\test.c

     文件      24251  2009-06-15 23:34  驱动设计\设备驱动实习报告.docx

     目录          0  2009-06-15 23:35  驱动设计

----------- ---------  ---------- -----  ----

                28626                    5


评论

共有 条评论