יום שבת, 11 במאי 2013

Linux Kernel kobject and kset

Like IUnknown in com
kobject  has:Parent , Name , Reference count 
A ktype is the type of object that embeds a kobject
A kset is a group of kobjects.
struct uio_mem {
struct kobject kobj;
unsigned long addr;
unsigned long size;
int memtype;
void __iomem *internal_addr;
};
struct uio_mem *u_mem = container_of(kp, struct uio_mem, kobj);
 
Init:
 
void kobject_init(struct kobject *kobj, struct kobj_type *ktype);



Associate with the parent  
 
int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
 
Get the name :
 
const char *kobject_name(const struct kobject * kobj);
 

Uevents:

 
Notify the word about the new kobject 
 
int kobject_uevent(struct kobject *kobj, enum kobject_action action);
 
KOBJ_ADD or 
KOBJ_REMOVE 
 
 

Reference count:

 

Dec ref count :

void kobject_put(struct kobject *kobj);

 

Inc ref count

struct kobject *kobject_get(struct kobject *kobj);
 
 
Wrap function for creating kobjects 
 
struct kobject *kobject_create_and_add(char *name, struct kobject *parent);
 
a structure protected by a kobject cannot be freed
before its reference count goes to zero
 

Releasing kobject

 

 void learn_object_release(struct kobject *kobj)
{
struct learn_object * thelearnobject = container_of(kobj, struct learn_object kobj);

//Concrate cleaning procedurs

kfree(thelearnobject );
}
 
 
  struct kobj_type {
void (*release)(struct kobject *);
struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
};
 
 

kset

associated kobject sets.

 

subdirectory in sysfs

 

create and delete a kset

 

 struct kset *kset_create_and_add(const char *name,
struct kset_uevent_ops *u,
struct kobject *parent);

void kset_unregister(struct kset *kset);

 

 

In order to assoiate the kobject with a kset

set the kobject kset member before creating the kobject

 

sample source

 

static struct foo_obj *create_foo_obj(const char *name)
0191 {
0192 struct foo_obj *foo;
0193 int retval;
0194
0195 /* allocate the memory for the whole object */
0196 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
0197 if (!foo)
0198 return NULL;
0199
0200 /*
0201 * As we have a kset for this kobject, we need to set it before calling
0202 * the kobject core.
0203 */
0204 foo->kobj.kset = example_kset;
0205
0206 /*
0207 * Initialize and add the kobject to the kernel. All the default files
0208 * will be created here. As we have already specified a kset for this
0209 * kobject, we don't have to set a parent for the kobject, the kobject
0210 * will be placed beneath that kset automatically.
0211 */
0212 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
0213 if (retval) {
0214 kobject_put(&foo->kobj);
0215 return NULL;
0216 }
0217
0218 /*
0219 * We are always responsible for sending the uevent that the kobject
0220 * was added to the system.
0221 */
0222 kobject_uevent(&foo->kobj, KOBJ_ADD);
0223
0224 return foo;
0225 }

 

 

 

 

 

 

 

 
 
 
 
 


 

 
 
 
 
 
 
 
 

אין תגובות:

הוסף רשומת תגובה