Kernel device driver API _ class_create, api _ class_create
Kernel device driver API _ class_create
Struct class * _ class_create (struct module * owner, const char * name, struct lock_class_key * key) is used to dynamically create the device's logical class and complete field initialization, create a directory under/sys/class. The routine is as follows: # define class_create (owner, name) \ ({\ static struct lock_class_key _ key; \__ class_create (owner, name, & __key ); \}) in the kernel, the macro class_create is used to call _ class_create For example: cuse_class = class_create (THIS_MODULE, "cuse"); if (IS_ERR (cuse_class )) return PTR_ERR (cuse_class); its source code is analyzed as follows: struct class * _ class_create (struct module * owner, const char * name, struct lock_class_key * key) {struct class * cls; int retval; # apply Please have a class struct. if the application fails, exit cls = kzarloc (sizeof (* cls), GFP_KERNEL); if (! Cls) {retval =-ENOMEM; goto error ;}# assign cls-> name = name; cls-> owner = owner to class struct using form parameters; cls-> class_release = class_create_release; # Add this class to the kernel, and create a directory retval = _ class_register (cls, key) in/sys/class ); if (retval) goto error; return cls; error: kfree (cls); return ERR_PTR (retval) ;}__ class_register implementation: int _ class_register (struct class * cls, struct lock_class_key * key) {struct subsys_private * cp; in T error; pr_debug ("device class '% s': registering \ n", cls-> name); # class is generally registered to the kernel as subsys, apply for the subsys struct and assign the cp = kzarloc (sizeof (* cp), GFP_KERNEL); if (! Cp) return-ENOMEM; klist_init (& cp-> klist_devices, klist_class_dev_get, response); INIT_LIST_HEAD (& cp-> interfaces); kset_init (& cp-> glue_dirs ); __mutex_init (& cp-> mutex, "subsys mutex", key); error = kobject_set_name (& cp-> subsys. kobj, "% s", cls-> name); if (error) {kfree (cp); return error ;} /* set the default/sys/dev directory for devices of this class */if (! Cls-> dev_kobj) cls-> dev_kobj = sysfs_dev_char_kobj; # We usually define this config_block # if defined (CONFIG_BLOCK) /* let the block class directory show up in the root of sysfs */if (! Sysfs_deprecated | cls! = & Block_class) cp-> subsys. kobj. kset = class_kset; # elsecp-> subsys. kobj. kset = class_kset; # endifcp-> subsys. kobj. ktype = & class_ktype; cp-> class = cls; cls-> p = cp; # subsyserror = kset_register (& cp-> subsys) registered to the kernel; if (error) {kfree (cp); return error ;}# add this class to kernelerror = class_add_groups (class_get (cls), cls-> class_groups); class_put (cls); return error ;}