Linux字符设备驱动开发模板

Linux驱动下的字符设备基本模板(未使用设备树)

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include<linux/cdev.h>
#include<linux/device.h>

#define LED_MAJOR       200
#define LED_NAME        "led"

#define NEWCHRLED_NAME  "newchrled"
#define NEWCHRLED_COUNT    1


#define LEDOFF 0    //关闭
#define LEDON  1    //打开



//设备结构体
struct newchrled_dev
{
    struct cdev cdev;   //字符设备
    dev_t devid;       //设备号
    struct class * class;    //类
    struct device * device;  //设备
    int major;
    int minor;
};

struct newchrled_dev newchrled;     //led设备



static int newchrled_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &newchrled;
	return 0;
}

static ssize_t newchrled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

static int newchrled_release(struct inode *inode, struct file *filp)
{
    struct newchrled_dev *dev = (struct newchrled_dev *)filp->private_data;
    
	return 0;
}

static const struct file_operations newchrled_fops=
{
    .owner = THIS_MODULE,
    .write = newchrled_write,
    .open = newchrled_open,
    .release = newchrled_release,
};

//入口
static int __init newchrled_init(void)
{
    int ret;
    //2、分配设备号
    newchrled.major = 0;    //表示由系统申请设备号
    if(newchrled.major)
    {
        newchrled.devid = MKDEV(newchrled.major,0);
        ret = register_chrdev_region(newchrled.devid,NEWCHRLED_COUNT,NEWCHRLED_NAME);
    }
    else
    {
        ret = alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_COUNT,NEWCHRLED_NAME);
        newchrled.major = MAJOR(newchrled.devid);
        newchrled.minor = MINOR(newchrled.devid);
    }

    if(ret < 0)
    {
        printk("newchrled chrdev_region err!\r\n");
        goto fail_devid;
    }
    printk("newchrled major = %d,minor = %d\r\n",newchrled.major,newchrled.minor);

    
    //3、注册字符设备
    newchrled.cdev.owner = THIS_MODULE;
    cdev_init(&newchrled.cdev,&newchrled_fops);

    ret = cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_COUNT);
    if(ret < 0)
        goto fail_cdev;

    //4、自动创建设备节点
	newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
	if (IS_ERR(newchrled.class))
    {
        ret = PTR_ERR(newchrled.class);
        goto fail_class;  
    }
  

    newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, NEWCHRLED_NAME);
	if (IS_ERR(newchrled.device))
    {
    	ret = PTR_ERR(newchrled.device); 
        goto fail_device;
    }

    return 0;
fail_device:
    class_destroy(newchrled.class);
fail_class:
    cdev_del(&newchrled.cdev);
fail_cdev:
	unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
fail_devid:
    return ret;

}

static void __exit newchrled_exit(void)
{

    //删除字符设备
    cdev_del(&newchrled.cdev);
    //1、注销设备号
    unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
    //设备摧毁
    device_destroy(newchrled.class, newchrled.devid);
    //类的删除
    class_destroy(newchrled.class); 
}





//注册和卸载驱动
module_init(newchrled_init);
module_exit(newchrled_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("wzh");



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Linux字符设备驱动程序模板,供参考: ```c #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/uaccess.h> #define DEVICE_NAME "mychardev" #define CLASS_NAME "mycharclass" static int major_num = 0; static struct class* mycharclass = NULL; static struct device* mychardev = NULL; static struct cdev mycdev; static int mychardev_open(struct inode*, struct file*); static int mychardev_release(struct inode*, struct file*); static ssize_t mychardev_read(struct file*, char*, size_t, loff_t*); static ssize_t mychardev_write(struct file*, const char*, size_t, loff_t*); static struct file_operations mychardev_fops = { .owner = THIS_MODULE, .open = mychardev_open, .release = mychardev_release, .read = mychardev_read, .write = mychardev_write, }; static int __init mychardev_init(void) { // allocate major number dynamically if (alloc_chrdev_region(&major_num, 0, 1, DEVICE_NAME) < 0) { return -1; } // create device class if ((mycharclass = class_create(THIS_MODULE, CLASS_NAME)) == NULL) { unregister_chrdev_region(major_num, 1); return -1; } // create device if ((mychardev = device_create(mycharclass, NULL, major_num, NULL, DEVICE_NAME)) == NULL) { class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); return -1; } // initialize cdev cdev_init(&mycdev, &mychardev_fops); mycdev.owner = THIS_MODULE; // add cdev to kernel if (cdev_add(&mycdev, major_num, 1) < 0) { device_destroy(mycharclass, major_num); class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); return -1; } printk(KERN_INFO "mychardev: registered\n"); return 0; } static void __exit mychardev_exit(void) { cdev_del(&mycdev); device_destroy(mycharclass, major_num); class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); printk(KERN_INFO "mychardev: unregistered\n"); } static int mychardev_open(struct inode* inodep, struct file* filep) { printk(KERN_INFO "mychardev: opened\n"); return 0; } static int mychardev_release(struct inode* inodep, struct file* filep) { printk(KERN_INFO "mychardev: closed\n"); return 0; } static ssize_t mychardev_read(struct file* filep, char* buffer, size_t len, loff_t* offset) { printk(KERN_INFO "mychardev: read from device\n"); return 0; } static ssize_t mychardev_write(struct file* filep, const char* buffer, size_t len, loff_t* offset) { printk(KERN_INFO "mychardev: wrote to device\n"); return len; } module_init(mychardev_init); module_exit(mychardev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple character device driver"); ``` 需要注意的是,该模板仅提供了一个最基本的字符设备驱动程序框架,需要根据实际需求进行修改。此外,还需要在Makefile中添加相应的编译规则,将驱动程序编译成内核模块。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值