Linux Driver Design button (1) - hybrid device driver model

1. The concept of hybrid device

  • In the Linux system, there is a class of character devices, they have the same major number (10) , but different minor number, we call this type of equipment is mixed equipment (miscdevice) . All hybrid devices forming a linked list, access to the device to be found by the kernel hybrid apparatus according minor number.
  • Hybrid device is a character device, they share a main equipment (10), but different minor number, all mixed to form a device list, when the device initiates the access, according to the respective kernel to find minor number miscdevice device. The benefit of this, saving major number, some devices will be linked together in the form of the list, and finally by looking for the minor number to distinguish. Generally LED driver and key drivers may be classified into hybrid devices.

2. Device Description

  • Using Linux struct miscdevice to describe a hybrid device.
struct miscdevice  {
    int minor;  // 次设备号
    const char *name;  // 设备名
    const struct file_operations *fops; // 文件操作
    struct list_head list;
    struct device *parent;
    struct device *this_device;
    const char *nodename;
    mode_t mode;
};
  • The main concern here minor number , device name and file operations three members.

3, device registration and cancellation

  • registered
    • Linux is used misc_register function to register a hybrid device driver.
    • int misc_register(struct miscdevice * misc)
  • Logout
    • int misc_deregister(struct miscdevice * misc)

4. The example code

  • Hybrid driving device is a module of code remains, while according to the above described frame can write a device driver hybrid.
    • Write module code framework
      • 1. Initialize miscdevice: mostly minor, name, fops members
      • 2. Registration miscdevice
      • 3. To achieve the function code inside file_operations
      • 4. Log off miscdevice
  • Key drive frame

key.c

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

int key_open(struct inode *node, struct file *filp)
{
    return 0;
}

struct file_operations key_fops = 
{
    .open = key_open,		
};

// 初始化miscdevice
struct miscdevice key_miscdev = 
{
    .minor = 200,   // 次设备号
    .name = key,   // 设备名
    .fops = &key_fops,  // 文件操作
};	

static int key_init()
{
    // 注册miscdevice
    misc_register(&key_miscdev);
    
    return 0;	
}

static void key_exit()
{	
     // 注销miscdevice
    misc_deregister(&key_miscdev);   	
}

MODULE_LICENSE("GPL");

module_init(key_init);
module_exit(key_exit);

 

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/91377664