The Linux device model

Source: Internet
Author: User

/sys and/dev questions

1,/dev decentralized is the device file, is created by the application layer Mknod file. If the underlying driver has a corresponding driver for the MKNOD device number, such as the Open function, then the application layer open "/dev/**" is called to the underlying driver. To put it bluntly,/dev decentralized the kernel and application layer files, so that the application layer to open,write,poll and so on.
2,/sys is a file system, when you write kernel code, if you have to call Kobj_init and other functions, you will be/sys under the corresponding directory to generate the corresponding file. It is the function of the kernel registers the device, the driver, the bus to form a tree structure. In addition, the application layer can also interact (Ktype) by reading and writing/sys files and kernels. Plainly,/sys is a tree-like structure that allows you to understand what drives and devices are available in the kernel, which is convenient for power management.

Sysfs Bottom-level operation

1. Kobject structure
The mention of kobject many people do not want to see, the same. But using this structure, we can build a device-driven model, so we have to understand. Developing drivers for me, that is, building several directories, creating several properties files. The kernel's device driver architecture has been played, and several functions are used. In the Sysfs file system, Kobject corresponds to the directory, attribute (attribute) corresponding to the file.

struct kobject {    constchar      * k_name;    char            name[KOBJ_NAME_LEN];    struct kref     kref;    struct list_head    entry;    struct kobject      * parent;    struct kset     * kset;    struct kobj_type    * ktype;    struct sysfs_dirent * sd;    wait_queue_head_t   poll;};

Kobject content includes the name of the directory, parent Upper directory, if Parent=null creates the directory under/sys. Ktype can be understood as a property of this directory.

structKobj_type {void(*release) (structKobject *);structSysfs_ops * SYSFS_OPS;struct attribute* * DEFAULT_ATTRS;};structSysfs_ops {ssize_t (*show) (structKobject *,struct attribute*,char *);//Read methodssize_t (*store) (structKobject *,struct attribute*,Constchar *, size_t);//Write Method};struct attribute{Constchar * name;structmodule * OWNER; mode_t mode;};//Properties file name, mode, read-only set to S_irugo, writable to S_IWUSR

2. Method (Operations)
Think about what you can do on a file system? Nothing but create a directory (Kobject), create a file (property)!

    //Kobject initialization function    voidKobject_init (structKobject * kobj);//Set the name of the specified Kobject    intKobject_set_name (structKobject *kobj,Const Char*format, ...);//Adds a reference count of the Kobj object and returns a pointer to the object    structKobject *kobject_get (structKobject *kobj);//Subtract the reference count of the Kobj object, or call Kobject release () to release the Kobject object if the reference count falls to    voidKobject_put (structKobject * kobj);//Add the Kobj object to the Linux device hierarchy. Hook up the Kobject object into the list chain of kset, increase the number of Kobject at the parent directory level, create a file node under the directory where its parent points, and start the HotPlug function for that type of kernel object    intKobject_add (structKobject * kobj);///Kobject register function, call Kobject init () to initialize Kobj, and then call Kobject_add () to complete the registration of the kernel object    intKobject_register (structKobject * kobj);//Delete the Kobj object from the Linux device hierarchy (hierarchy)    voidKobject_del (structKobject * kobj);///Kobject logoff function. In contrast to Kobject register (), it first calls Kobject del to remove the object from the device hierarchy, and then//with Kobject put () to reduce the reference count of the object, if the reference count falls to, The Kobject object is freed    voidKobject_unregister (structKobject * kobj);
Device Model Upper Container

The upper container referred to here refers to the bus type (bus_type), device, and Driver (Device_driver). LDD3 inside for an analogy, the Kobject as a base class, bus type, device, driver and kobject are inheritance relations, these upper-level containers naturally have the Sysfs operation Method!
Bus types generally do not need to be created, the kernel supports most bus types (PCI,USB,IIC ... ), also contains the virtual bus type (Platform_bus), so here does not involve how to create a bus, how to establish the bus properties file and so on.

1. Device devices

The operation of the device can be simply understood as 1, registering the device, 2, creating the device properties file.
The usual registration and logoff functions:
int device_register (struct device *dev);
void Device_unregister (struct device *dev);
The registration and logoff of the device includes operations on the underlying SYSFS, such as Kobject_init,kobject_add ...
Creating properties

structstruct attribute attr; ssize_t (*show)(structchar *buf); ssize_t (*store)(structconstchar *buf, size_t count); };

These property structures can be built at compile time, using these macros:
device_attr (name, mode, show, store);
The result structure is named by the prefix dev_attr_ to the given name. The actual management of the properties file is handled using the usual function pairs:
int device_create_file (struct device *device, struct device_attribute *entry);
void Device_remove_file (struct device *dev, struct device_attribute *attr);

2. Drive Driver

The operation of the drive can be simply understood as 1, the registration driver, 2, the drive properties file.
The usual registration and logoff functions:
int Driver_register (struct device_driver *drv);
void Driver_unregister (struct device_driver *drv);
The registration and logoff of the device includes operations on the underlying SYSFS, such as Kobject_init,kobject_add ...
Creating properties
driver_attr (name, mode, show, store);
int driver_create_file (struct device_driver *drv, struct driver_attribute *attr);
void Driver_remove_file (struct device_driver *drv, struct driver_attribute *attr);

Pick a drive code to talk about
#include <linux/fs.h>#include <asm/uaccess.h>#include <linux/pci.h>#include <linux/input.h>#include <linux/platform_device.h>structInput_dev *vms_input_dev;/ * Representation of an input device * /Static structPlatform_device *vms_dev;/ * Device structure * /                                        / * Sysfs method to input simulated coordinates to the virtual Mouse driver * /StaticSsize_twrite_vms (structDevice *dev,structDevice_attribute *attr,Const Char*buffer, size_t count) {intx, y;sscanf(Buffer,"%d%d", &x, &y);/ * Report relative coordinates via the event interface * /Input_report_rel (Vms_input_dev, rel_x, X);  Input_report_rel (Vms_input_dev, rel_y, Y); Input_sync (Vms_input_dev);returnCount;}/ * Attach The Sysfs Write method * /device_attr (Coordinates,0644, NULL, Write_vms);/ * Attribute Descriptor * /Static structAttribute *vms_attrs[] = {&dev_attr_coordinates.attr, NULL};/ * Attribute Group * /Static structAttribute_group Vms_attr_group = {. Attrs = Vms_attrs,};/ * Driver initialization * /int__initvms_init (void){/ * Register a platform device * /Vms_dev = Platform_device_register_simple ("VMS", -1Null0);if(Is_err (Vms_dev))    {Ptr_err (Vms_dev); Printk"vms_init:error\n"); }/ * Create a SYSFS node to read simulated coordinates * /Sysfs_create_group (&vms_dev->dev.kobj, &vms_attr_group);/ * Allocate an input device data structure * /Vms_input_dev = Input_allocate_device ();if(!vms_input_dev) {PRINTK ("Bad Input_alloc_device () \ n"); }/ * announce that virtual mouse would generate relative coordinates * *Set_bit (Ev_rel, vms_input_dev->evbit);  Set_bit (rel_x, vms_input_dev->relbit); Set_bit (rel_y, vms_input_dev->relbit);/ * Register with the input subsystem * /Input_register_device (Vms_input_dev); Printk"Virtual Mouse Driver initialized.\n");return 0;}/ * Driver Exit * /voidVms_cleanup (void){/ * Unregister from the input subsystem * /Input_unregister_device (Vms_input_dev);/ * Cleanup SYSFS node * /Sysfs_remove_group (&vms_dev->dev.kobj, &vms_attr_group);/ * Unregister driver * /Platform_device_unregister (Vms_dev);return;} Module_init (Vms_init); Module_exit (Vms_cleanup);

This is from "Essential Linux device Drivers" The seventh chapter input device pick code, to achieve from the virtual mouse to read the data coordinates escalated function.

The bus platform_bus is used here.
Vms_dev = Platform_device_register_simple ("VMS",-1, NULL, 0); Register the device to the bus;
Sysfs_create_group (&vms_dev->dev.kobj, &vms_attr_group); Creates a property file, referenced in Linux/sysfs.h, similar to Sysfs_create_ File
Input_register_device (Vms_input_dev); Register the device file to input subsystem, the specific details of reference source;
In general, there are two things done, creating a directory object and creating attributes, then adding it to the device model and creating a resource management tree!

The article is only one-sided, the macroscopic talk about the driving model, the experience is insufficient, only for the reference!

Resources

http://bbs.csdn.net/topics/380166008
http://blog.csdn.net/xiahouzuoxin/article/details/8943863

The Linux device model

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.