20135302 Wei Quiet--book 17th study notes

Source: Internet
Author: User

17th Chapter module and Equipment

    • Device type: The classification used in all Unix systems in order to unify the operation of a common device.

    • Module: a mechanism for loading and unloading target codes on demand in the Linux kernel.

    • Kernel objects: The kernel data structures support simple object-oriented operations, and also support the maintenance of parent-child relationships between objects.

    • SYSFS: Represents a file system for the device tree in the system.

1. Device type

Three different types

块设备字符设备网络设备

2. Module

1. Hello,world

    • All initialization functions of the module must conform to the form: int my _ init (void);
    • The exit function must conform to the form: void My_exit (void);

2. Build

3. Installation

    • Install the compiled module: Make Modules_install

4. Dependency generation

    • This dependency information, and is updated on each startup.
    • To generate kernel dependency information, the root user can run the command: Depmod
    • Generate dependency information for the new module only, without generating all dependencies, the root user can run the command: depmod-a

5. Loading

    • Root Run command: insmod module. Ko
    • To insert a module in the kernel via modprobe, you need to run Zmodprobe module [module parameters] as root (the parameter module specifies the modules that need to be loaded).
    • Uninstalling the module from the kernel, running as root: modprobe-r modules

6. Manage Configuration options

    • Set Config_fishing_pole configuration options.
    • If a new subdirectory is established and you want the Kconfig file (possibly Drivers/char/kconfig) to exist in the directory, introduce it in the Kconfig file that already exists: source "Drivers/char/fishing/kcor, Lfig "

7. Parameters

    • Defining a module parameter can be done by macro: Module_param (name, type, perm).

      The types of parameters can be byte, short, ushort, int, uint, long, ulong, CHARP, bool, or Invbool.

8. Export symbol table

    • Exporting kernel functions requires special instructions: Export_ SYMBOL () and EXPORT_SYMBOL_GPL ().
    • Exported kernel symbol table: The exported kernel interface, called the Kernel API.
    • After declaring the function, follow the Export_symbol ().

3. Device model

    • The initial motive for implementing the device model is that it can traverse the leaves of the plant tree to its root in order to ensure that the power of each device is turned off in the correct order.

1. Kobject

    • The core part of the device model.
    • struct kobject struct, defined in header file .
    • is usually embedded in other structures.

2. Ktype

    • The Kobject object is associated to a special type: Ktype.
    • Represented by the Kobj_type struct, defined in the header file .
    • In order to describe the universal characteristics of a family of kobject.

3. Kset

    • The kobject can be concentrated into a set, and Ktype describes the properties common to the relevant type KO ect, the important difference between them is that 2 kobject with the same ktype can be grouped into different kset.
    • In the Linux kernel, there are only a few ktype, but there are multiple kset.
    • Represented by the KSET structure, defined in the header file <linux/kobject.h>:

4. Kobject, Ktype, Kset relations

    • Kobject, let those structures that contain it have kobject characteristics.
    • Ktype defines some kobject-related default attributes.
    • Kset provides two functions: first, where the embedded Kobj is authored as the base class for the Kobject group. Second, kset the associated kobject together.

5. Management and operation of Kobject

    • Kobject is initialized through the function koject_init, which is defined in the file : void Kobject_init (struct kobject kobj, struct kobj_type /c2>ktype);

      The first parameter is a Kobject object that needs to be initialized;
      Before invoking the initialization function, the kobject must be emptied;
      Not emptied, call memset () to: memset (kobj, 0, sizeof (*kobj));

    • You should call Kobject_createo to create the koject.

6. Counting

    • Kobject's main function: Provides a unified reference counting system.
    • After initialization, the reference count of Kobject is set to 1, the reference count is nonzero, and the object continues to remain in memory.
    • The reference count falls to 0 o'clock, the object can be revoked, and the associated memory is freed.
    • The reference count for Koject is implemented by the Kref struct, which is defined in the header file .

4. Sysfs

    • An in-memory virtual file system that gives us a view of the Kobject object hierarchy.
    • The Kobject object is closely linked to the catalog item through the Dentry field in the Kobject object.

1. Add or remove Kobject

    • To import the koject into SYSFS, you need to use the function Kobject_add (): int kobject_add (struct kobj ect kobj, struct kobj ect parent, const char * Fmt. .. ) ;
      -delete a kobject corresponding file directory from Sysfs, use function Kobject_del (): void Kobject _del (struct kobject *kobj);

2. Add Files

L. Default Properties

    • All kobject with the same type have the same default collection of files in their corresponding SYSFS directories.

      2. Create a new attribute
    • Create a symbolic connection in SYSFS: int sysfs_create_link (struct kobject kobj, struct kobject target, char name);

3. Delete new properties

    • Deleting an attribute is done through the function Sysfs_remove_ file (): void Sysfs_remove_file (struct kobject kobj, const struct attribute attr);
    • Symbolic connections created by Sysfs_ creat_ link () can be removed by deleting: void Sysfs_remove_link (struct kobject kobj, char name);

4. SYSFS Convention

    • This guarantees that only one value is exported per file.
    • Organize your data at a clear level.
    • Provides kernel-to-user service.

3. Kernel Event Layer

    • The kernel-to-user message notification system is implemented.
    • Kernel events are passed netlink through the kernel space to the user space.
    • User space implements a system background service for listening to sockets, processing any read information, and delivering events to the system stack.

20135302 Wei Quiet--book 17th study notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.