linux字符设备驱动创建基本框架

1.总览

  • linux字符设备驱动创建基本流程图
    在这里插入图片描述

2.整体框架

static int xxx_open(struct inode *inode, struct file *filp)
{
        printk("xxx_open\n");
        return 0;
}

static int xxx_release(struct inode *inode, struct file *filp)
{
        printk("xxx_release\n");
        return 0;
}

static ssize_t xxx_read(struct file *filp, char __user *user, size_t size, loff_t *loff)
{
      printk("xxx_read\n");
       return 0;
}

static ssize_t xxx_write(struct file *filp, const char __user *user, size_t size, loff_t *loff)
{
        return 0;
}
static const struct file_operations xxx_fops = {
        .owner          = THIS_MODULE,
        .open           = xxx_open,
        .release        = xxx_release,
        .write          = xxx_write,
        .read           = xxx_read,
};


/* 驱动入口函数 */
static int __init xxx_init(void)
{
/* 入口函数具体内容 */
	.....
	add_cdev(xxx, &xxx_fops);
	.....
	return 0;
}

/* 驱动出口函数 */
static void __exit xxx_exit(void)
{
/* 出口函数具体内容 */
	...
	del_cdev(xxx);
	...
}

/* 将上面两个函数指定为驱动的入口和出口函数 */
module_init(xxx_init);
module_exit(xxx_exit);
MODULE_AUTHOR("yourname");
MODULE_LICENSE("GPL");

3.简化是例代码

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>

#define NEWCHRLED_MAJOR 200
#define NEWCHRLED_COUNT 1
#define NEWCHRLED_NAME  "newchrled"


struct newchrled_dev {
        struct cdev newchrled_cdev;
        struct class *newchrled_class;
        struct device *newchrled_device;
        dev_t devid;    /*设备号*/
        int major;      /*主设备号*/
        int minor;      /*次设备号*/
};

static struct newchrled_dev newchrled;


static int newchrled_open(struct inode *inode, struct file *filp)
{
        printk("newchrled_open\n");
        return 0;
}

static int newchrled_release(struct inode *inode, struct file *filp)
{
        printk("newchrled_release\n");
        return 0;
}

static ssize_t newchrled_read(struct file *filp, char __user *user, size_t size, loff_t *loff)
{
      printk("newchrled_read\n");
       return 0;
}

static ssize_t newchrled_write(struct file *filp, const char __user *user, size_t size, loff_t *loff)
{
        return 0;
}

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

static int __init newchrled_init(void)
{
        printk("newchrled_init\n");
		//1.分配设备号
        alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_COUNT, NEWCHRLED_NAME)
        newchrled.major = MAJOR(newchrled.devid);
        newchrled.minor = MINOR(newchrled.devid);
        printk("newchrled major:%d\tnewchrled minor:%d\n", newchrled.major, newchrled.minor);

		//2.创建一个cdev,并将fops注册到设备中
        newchrled.newchrled_cdev.owner = newchrled_fops.owner;
        cdev_init(&newchrled.newchrled_cdev, &newchrled_fops);

		//3.创建设备号,可以在/proc/devices查看
        cdev_add(&newchrled.newchrled_cdev, newchrled.devid, NEWCHRLED_COUNT

		//4.创建一个类可以在/sys/class查看
        class_create(THIS_MODULE, "newchrled");

		//5.创建一个设备节点可以在/dev查看
		device_create(newchrled.newchrled_class, NULL , newchrled.devid, NULL, NEWCHRLED_NAME);
        return 0;
}
module_init(newchrled_init);

static void __exit newchrled_exit(void)
{
        printk("newchrled_exit\n");
        //1.销毁在/dev创建的设备节点
        device_destroy(newchrled.newchrled_class, newchrled.devid);
        //2.销毁class类
        class_destroy(newchrled.newchrled_class);
        //3.释放申请的cdev
        cdev_del(&newchrled.newchrled_cdev);
        //4.释放申请的设备号
        unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
}
module_exit(newchrled_exit);

MODULE_AUTHOR("yourname");
MODULE_LICENSE("GPL");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值