动静态申请字符类设备号

1、静态申请字符类设备号

目的:静态申请字符类设备号

头文件:include/linux/fs.h

(1)静态申请字符设备编号:int register_chrdev_region(dev_t, unsigned, const char *);
    * 参数一dev_t    // 设备号
    * 参数二unsigned // 次设备号的数量
    * 参数三char *   // 设备名称

    其中dev_t设备号:
        * 头文件:include/linux/cdev.h
        * cdev类型是是字符设备描述的结构
        * 其中的设备号必须用“dev_t”类型来描述,高12位为主设备号,低20位为次设备号
        * 在include/linux/kdev_t.h头文件中有一系列设备号处理的宏命令,用于处理各种设备号相关的数据,如MKDEV

(2)卸载驱动时使用:void unregister_chrdev_region(dev_t, unsigned);
    * 参数一dev_t    // 设备号
    * 参数二unsigned // 次设备号的数量


加载运行:
    使用命令“cat /proc/devices”查看已经被注册的主设备
    insmod module_cdev.ko numdev_major=9 numdev_minor=0
    rmmod request_cdev_num numdev_major=9 numdev_minor=0

相关代码:

#include <linux/init.h>
#include <linux/module.h>

#include <linux/moduleparam.h>
#include <linux/stat.h>

#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>

#define DEVICE_NAME "scdev"
#define DEVICE_MINOR_NUM 2
#define DEV_MAJOR 0
#define DEV_MINIOR 0

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("LSQ");

int numdev_major = DEV_MAJOR;
int numdev_minor = DEV_MINIOR;

module_param(numdev_major, int, S_IRUSR);

module_param(numdev_minor, int, S_IRUSR);

static int scdev_init(void)
{
    int ret = 0;
    dev_t num_dev;


    printk(KERN_EMERG "numdev_major is %d\n",numdev_major);
    printk(KERN_EMERG "numdev_minor is %d\n",numdev_minor);

    if(numdev_major)
    {
        num_dev = MKDEV(numdev_major, numdev_minor);
        ret = register_chrdev_region(num_dev, DEVICE_MINOR_NUM, DEVICE_NAME);
        printk(KERN_EMERG "num_dev is %d\n", num_dev);
    }
    else
    {
        printk(KERN_EMERG "numdev_major %d is failed!\n",numdev_major);
    }

    if(ret < 0)
    {
        printk(KERN_EMERG "register_chrdev_region req %d is failed!\n", numdev_major);
    }

    printk(KERN_EMERG "scdev enter!\n");

    return 0;
}

static void scdev_exit(void)
{
    printk(KERN_EMERG "scdev exit!\n");
    unregister_chrdev_region(MKDEV(numdev_major, numdev_minor), DEVICE_MINOR_NUM);
}

module_init(scdev_init);
module_exit(scdev_exit);

2、动态申请字符类设备号

目的:动态申请字符类设备号

头文件:include/linux/fs.h

(1)动态分配主次设备号:int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
    * 参数一dev_t    // 存放设备号地址
    * 参数二unsigned // 次设备号
    * 参数三unsigned // 次设备号的数量
    * 参数四char *   // 设备名称

*可用宏定义MAJOR提取dev_t数据中的主设备号

(2)卸载驱动时使用:void unregister_chrdev_region(dev_t, unsigned);
    * 参数一dev_t    // 设备号
    * 参数二unsigned // 次设备号的数量

加载运行:
使用命令“cat /proc/devices”查看已经被注册的主设备
动态加载模块之后再查看设备号

相关代码:(根据上面静态申请修改,所以还有静态申请)

#include <linux/init.h>
#include <linux/module.h>

#include <linux/moduleparam.h>
#include <linux/stat.h>

#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>

#define DEVICE_NAME "ascdev"
#define DEVICE_MINOR_NUM 2
#define DEV_MAJOR 0
#define DEV_MINIOR 0

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("LSQ");

int numdev_major = DEV_MAJOR;
int numdev_minor = DEV_MINIOR;

module_param(numdev_major, int, S_IRUSR);

module_param(numdev_minor, int, S_IRUSR);

static int scdev_init(void)
{
    int ret = 0;
    dev_t num_dev;


    printk(KERN_EMERG "numdev_major is %d\n",numdev_major);
    printk(KERN_EMERG "numdev_minor is %d\n",numdev_minor);

    if(numdev_major)
    {
        // static
        num_dev = MKDEV(numdev_major, numdev_minor);
        ret = register_chrdev_region(num_dev, DEVICE_MINOR_NUM, DEVICE_NAME);
        printk(KERN_EMERG "num_dev is %d\n", num_dev);
    }
    else
    {
        // alloc
        ret = alloc_chrdev_region(&num_dev, numdev_minor, DEVICE_MINOR_NUM, DEVICE_NAME);
        numdev_major = MAJOR(num_dev);

        printk(KERN_EMERG "adev_region req %d !\n", numdev_major);
    }

    if(ret < 0)
    {
        printk(KERN_EMERG "register_chrdev_region req %d is failed!\n", numdev_major);
    }

    printk(KERN_EMERG "scdev enter!\n");

    return 0;
}

static void scdev_exit(void)
{
    printk(KERN_EMERG "scdev exit!\n");
    unregister_chrdev_region(MKDEV(numdev_major, numdev_minor), DEVICE_MINOR_NUM);
}

module_init(scdev_init);
module_exit(scdev_exit);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值