Linux杂项设备

一、杂项设备和字符设备有什么区别

  • In misc driver, the major number will be 10 and the minor number is user convenient. Whereas, in character drivers, the user can select their own major and minor number if it is available.
  • The device node or device file will be automatically generated in misc drivers. Whereas, in character drivers, the user has to create the device node or device file using cdev_initcdev_addclass_create, and device_create.

二、 使用杂项设备

  • If you write character drivers for simple devices, you have to create a major number as well. In such a case, the kernel has to keep that details in the static table. This means, you end up wasting the RAM for simple devices. If you write multiple character drivers for multiple simple devices, then the RAM wastage also increases. To avoid this you can use the misc driver. Because, in the misc driver, you don’t need to specify the major number since it has a fixed major number which is 10.
  • If you use misc drivers, it will automatically create the device file compare to the character driver.

三、 杂项设备核心数据结构

Character drivers have cdev structure to know the driver’s functions and other calls. Like that, the misc driver also has a separate structure miscdevice to maintain the details.

struct miscdevice {
  int minor;
  const char *name;
  struct file_operations *fops;
  struct miscdevice *next, *prev;
};

 注意:Once you loaded the misc driver, then you can see the major and minor number of your misc device driver using ls -l /dev/{misc_driver_name}

四、核心API 

1、int misc_registerstruct miscdevice * misc) —— Register the misc device

This one function will avoid the below functions used in the character device driver while registering the device.
(1)alloc_chrdev_region(); – used for the major and minor number
(2)cdev_init(); – used to initialize cdev
(3)cdev_add(); – used to  add the cdev structure to the device
(4)class_create();  – used to create a class
(5)device_create();  – used to create a device

2、misc_deregister(struct miscdevice * misc) —— Unregister the misc device

This one function will avoid the below functions used in the character device driver while un-registering the device.
(1)cdev_del(); – used to delete cdev
(2)unregister_chrdev_region();  – used to remove the major and minor number
(3)device_destroy();  – used to delete the device
(4)class_destroy(); – used to delete the class

 五、示例代码 

/***************************************************************************//**
*  \file       misc_driver.c
*
*  \details    Simple misc driver explanation
*
*  \author     EmbeTronicX
*
*  \Tested with kernel 5.3.0-42-generic
*
*******************************************************************************/
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

/*
** This function will be called when we open the Misc device file
*/
static int etx_misc_open(struct inode *inode, struct file *file)
{
    pr_info("EtX misc device open\n");
    return 0;
}

/*
** This function will be called when we close the Misc Device file
*/
static int etx_misc_close(struct inode *inodep, struct file *filp)
{
    pr_info("EtX misc device close\n");
    return 0;
}

/*
** This function will be called when we write the Misc Device file
*/
static ssize_t etx_misc_write(struct file *file, const char __user *buf,
               size_t len, loff_t *ppos)
{
    pr_info("EtX misc device write\n");
    
    /* We are not doing anything with this data now */
    
    return len; 
}
 
/*
** This function will be called when we read the Misc Device file
*/
static ssize_t etx_misc_read(struct file *filp, char __user *buf,
                    size_t count, loff_t *f_pos)
{
    pr_info("EtX misc device read\n");
 
    return 0;
}

//File operation structure 
static const struct file_operations fops = {
    .owner          = THIS_MODULE,
    .write          = etx_misc_write,
    .read           = etx_misc_read,
    .open           = etx_misc_open,
    .release        = etx_misc_close,
    .llseek         = no_llseek,
};

//Misc device structure
struct miscdevice etx_misc_device = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = "simple_etx_misc",
    .fops = &fops,
};

/*
** Misc Init function
*/
static int __init misc_init(void)
{
    int error;
 
    error = misc_register(&etx_misc_device);
    if (error) {
        pr_err("misc_register failed!!!\n");
        return error;
    }
 
    pr_info("misc_register init done!!!\n");
    return 0;
}

/*
** Misc exit function
*/
static void __exit misc_exit(void)
{
    misc_deregister(&etx_misc_device);
    pr_info("misc_register exit done!!!\n");
}
 
module_init(misc_init)
module_exit(misc_exit)
 
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A simple device driver - Misc Driver");
MODULE_VERSION("1.29");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值