The base of the device model is kobject,kset,kobj_type.
Kobject itself does not make sense, the real useful place is embedded in the Kobject structure (object), Kobject can be regarded as a minimum unit, SYSFS hierarchy of elements are composed of kobject.
The relationship between Kset and Kobject:
Kset will contain a class of Kobject objects, which correspond to the same level of subdirectories in Sysfs, and each Kobject object has its own properties that correspond to the files of the respective subdirectories.
In this way, it will constitute a hierarchical organizational structure.
The Kset itself contains a kobject, but this kobject has nothing to do with kobjects in the list of Kset kernel lists.
Two main functions of the Kobject:
1. Reference counting
2. Escalation of events (Kobject does not join a kset is not reported uevent http://www.cnblogs.com/black-mamba/p/5055683.html)
Related documents:
Linux/documentation/kobject.txt
Documentation/filesystems/sysfs.txt
Documentation/filesystems/sysfs-pci.txt
linux/include/linux/kobject.hstructKset {structList_head list;//List of all Kobject objects belonging to the current collectionspinlock_t List_lock;structKobject Kobj;//is not related to the individual kobject contained in the collection, but is used to manage the Kset object itselfConst structKset_uevent_ops *uevent_ops;//used to pass state information for a collection to the user layer. This mechanism has the core use of the driver. };//describe common characteristics of kernel objectsstructKobj_type {void(*release) (structKobject *kobj);Const structSysfs_ops *Sysfs_ops;structAttribute * *Default_attrs;Const structKobj_ns_type_operations * (*child_ns_type) (structKobject *kobj);Const void*(*namespace)(structKobject *kobj);};//Kobject is embedded directly into other data structures, and the objects that contain kobject can be managed by managing Kobject. //Kobject are included in a hierarchical organization. structKobject {Const Char*name;//Object NamestructList_head entry;structKobject *parent;//determines where Kobject appears in the SYSFS hierarchy: If there is a parent object, you need to create a new entry in the same directory as the parent object, otherwise place it in the kobject corresponding to the Kset to which the Kobject is locatedstructKset *kset;//Collection PointersstructKobj_type *Ktype;structSysfs_dirent *SD;structKref Kref;//Reference CountUnsignedintState_initialized:1; unsignedintSTATE_IN_SYSFS:1; unsignedintState_add_uevent_sent:1; unsignedintState_remove_uevent_sent:1; unsignedintUevent_suppress:1;};structKobj_attribute {structattribute attr;ssize_t (*show) (structKobject *kobj,structKobj_attribute *attr,//The kernel calls this function when the user-space Read PropertyChar*buf); ssize_t (*store) (structKobject *kobj,structKobj_attribute *attr,//when a user-space write property is called, the kernel calls the functionConst Char*buf, size_t count);};/disk/a9/linux-3.5/include/linux/Sysfs.hstructattribute {Const Char*name;//the file name displayedumode_t mode;//Permissions for Files#ifdef Config_debug_lock_allocBOOLIGNORE_LOCKDEP:1;structLock_class_key *key;structLock_class_key Skey;#endif};#define__attr (_name,_mode,_show,_store) {\. attr= {. Name = __stringify (_name),. Mode =_mode}, \. Show=_show, \. Store=_store,}structAttribute_group {Const Char*name;umode_t (*is_visible) (structKobject *,structAttribute *,int);structAttribute * *attrs;};structKobject *kobject_get (structKobject *kobj)//Reference +1voidKobject_put (structKobject *kobj)//Reference-1voidKobject_init (structKobject *kobj,structKobj_type *ktype) Initialize the kobeject structurestructKobject *kobject_create (void)//non-export functionsCreate a kobject struct and initialize it with Kobject_initintKobject_add (structKobject *kobj,structKobject *Parent,Const Char*FMT, ...) Register a kobject and generate the appropriate directory in the SYSFS. If the parent is empty, the parent of the kobject points to the kobject of its kset, and if Kset is not assigned, Kobject is located in the/SYS understructKobject *kobject_create_and_add (Const Char*name,structKobject *parent) creates a kobject and registers to SYSFS, which fails to return NULL. intKobject_init_and_add (structKobject *kobj,structKobj_type *Ktype,structKobject *parent,Const Char*FMT, ...) Initializes a kobject and registers to Sysfs, and the properties file is automatically generated.
Q1: What interface does the user space call correspond to show, store?
linux/include/linux/kobject.h/** * struct kset-a set of kobjects of a specific type, belonging to a specific subsystem.** a kset defines a group of Kobjects. They can individually* different "types" but overall these kobjects all want to being grouped* together and operated on The same manner. Ksets is used to* define the attribute callbacks and other common events that happen to* a kobject.** @list: the list of All Kobjects-kset* @list_lock: A lock for iterating over the kobjects* @kobj: The embedded-kobject for this kset (recursion, isn ' t it fun ...) * @uevent_ops: The set of uevent operations for this kset. These are* called whenever a kobject have something happen to it so that the kset* can add new environment variables, or fi Lter out the uevents if so* desired. */ structKset {structList_head list;//List of all Kobject objects belonging to the current collectionspinlock_t List_lock; structKobject Kobj;//is not related to the individual kobject contained in the collection, but is used to manage the Kset object itself Const structKset_uevent_ops *uevent_ops;//used to pass state information for a collection to the user layer. This mechanism is used by the core of the driver. }; /** * kset_create-create a struct kset dynamically * @name: The name for the Kset * @uevent_ops: a struct Kset_ueve Nt_ops for the Kset * @parent_kobj: The parent kobject of this kset, if any. * * This function creates a kset structure dynamically. This structure can * then is registered with the system and show on SYSFS with a call to * Kset_register (). When you were finished with this structure, if * kset_register () had been called, call Kset_unregister () and the * Structur E'll be dynamically freed if it is no longer being used. * If The Kset is not able to be created, NULL would be returned. */ //non-export functionsStatic structKset *kset_create (Const Char*name,Const structKset_uevent_ops *Uevent_ops,structKobject *parent_kobj)/** * Kset_init-initialize a kset for use * @k:kset*/voidKset_init (structKset *k)/** * kset_register-initialize and add a kset. * @k:kset.*/intKset_register (structKset *k)/** * Kset_unregister-remove a kset. * @k:kset.*/voidKset_unregister (structKset *k)/** * kobject_uevent-notify userspace by sending a uevent * * @action: Action is happening * @kobj: struct kobje CT that the action was happening to * * Returns 0 if Kobject_uevent () are completed with success or the * corresponding Erro R when it fails. */intKobject_uevent (structKobject *kobj,enumkobject_action Action)/** * kset_create_and_add-create a struct kset dynamically and add it to SYSFS * * @name: The name for the Kset * @uev ent_ops:a struct Kset_uevent_ops for the kset * @parent_kobj: The parent kobject of this kset, if any. * * This function creates a kset structure dynamically and registers it * with SYSFS. When your is finished with this structure, call * Kset_unregister () and the structure would be dynamically freed when it * is no longer being used. * If The Kset is not able to be created, NULL would be returned. */structKset *kset_create_and_add (Const Char*name,Const structKset_uevent_ops *Uevent_ops,structKobject *parent_kobj)
Basic---of the device model Kobject,kset