"Linux Kernel Design and Implementation" Reading Notes-Equipment and Modules

Four kernel components related to device driver and management

Device type : The classification used in all Unix systems to unify the operation of common devices.

Module : A mechanism in the Linux kernel for loading and unloading object codes on demand.

Kernel objects : simple object-oriented operations are supported in the kernel data structure, and the parent-child relationship between objects is also maintained.

sysfs : Represents a file system of the device tree in the system.

 

Equipment type

Devices in Linux are divided into three types:

Block device (blkdev);

Character device (cdev);

Network equipment: Breaking the Unix design principle of "Everything is a file", it is not accessed through device nodes, but through sockets;

Not all devices represent physical devices. For example, /dev/random is a random number generator, and /dev/null is a null device.

 

Module

Register the module entry point to the system:

/* Each module must use one module_init(). */
#define module_init(initfn)                 \
    static inline initcall_t __inittest(void)       \
    { return initfn; }                  \
    int init_module(void) __attribute__((alias(#initfn)));

Register the module exit point to the system:

/* This is only required if you want to be unloadable. */
#define module_exit(exitfn)                 \
    static inline exitcall_t __exittest(void)       \
    { return exitfn; }                  \
    void cleanup_module(void) __attribute__((alias(#exitfn)));

Specify the copyright of the module:

#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)

Declare parameters for the driver:

#define module_param_named(name, value, type, perm)            \
    param_check_##type(name, &(value));                \
    module_param_call(name, param_set_##type, param_get_##type, &value, perm); \
    __MODULE_PARM_TYPE(name, #type)
#define module_param(name, type, perm)              \
    module_param_named(name, name, type, perm)

name : the parameter name visible to the user;

type : the type of storage parameters;

perm : Specify the permissions of the corresponding files in the sysfs file system of the module;

The above is just a declaration, the relevant variables must be defined before using this macro.

Module description:

#define MODULE_PARM_DESC(_parm, desc) \
    __MODULE_INFO(parm, _parm, #_parm ":" desc)

Export symbol table:

#define EXPORT_SYMBOL(sym)                  \
    __EXPORT_SYMBOL(sym, "")
#define EXPORT_SYMBOL_GPL(sym)                  \
    __EXPORT_SYMBOL(sym, "_gpl")

Only kernel functions that are explicitly exported can be called by the module.

Install the module using the command:

make modules_install

Load the module:

insmod

Uninstall the module:

rmmod

Module detection:

modprobe

 

Equipment model

The core part of the device model is to look at kobject, corresponding to the structure (include\linux\kobject.h):

struct kobject {
    const char      *name;
    struct list_head    entry;
    struct kobject      *parent;
    struct kset     *kset;
    struct kobj_type    *ktype;
    struct sysfs_dirent *sd;
    struct kref     kref;
    unsigned int state_initialized:1;
    unsigned int state_in_sysfs:1;
    unsigned int state_add_uevent_sent:1;
    unsigned int state_remove_uevent_sent:1;
    unsigned int uevent_suppress:1;
};

Kobject is usually embedded in other structures, and its independent existence is of little significance. For example (include\linux\cdev.h):

struct cdev {
    struct kobject kobj;
    struct module *owner;
    const struct file_operations *ops;
    struct list_head list;
    dev_t dev;
    unsigned int count;
};

 

sysfs

The sysfs file system is a virtual file system in memory, which provides us with a view of the kobject object hierarchy.

sysfs replaces the device-related files previously under /proc.

sysfs is mounted in the sys directory:

 

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/106153199