Device Model for Linux drivers (7)

Source: Internet
Author: User

8. Class

8.1 class

L class is used to classify devices according to their functions, so as to provide a friendly interface for user space. Class is also a kset, corresponding to a directory under/sys/class

Int _ initclasses_init (void)

{

/* Corresponding to the/sys/class directory */

Class_kset = kset_create_and_add ("class", null, null );

If (! Class_kset)

Return-enomem;

Return 0;

}

 

L in the device model, classes are represented by the class structure

Struct class {

Const char * Name;/* class name */

Struct module * owner;

 

Struct class_attribute * class_attrs;/* Class Attribute */

Struct device_attribute * dev_attrs;/* Class Device attributes */

Struct bin_attribute * dev_bin_attrs;

Struct kobject * dev_kobj;

/* Called when a device is added, the uevent is generated to add the corresponding environment variable */

INT (* dev_uevent) (struct device * Dev, struct kobj_uevent_env * env );

Char * (* devnode) (struct device * Dev, mode_t * mode );

 

Void (* class_release) (struct class * class);/* called when canceling a class */

Void (* dev_release) (struct device * Dev);/* called when the device is removed */

 

/* Power management */

Const struct dev_pm_ops * PM;

 

/* Private data: Embedded kset, device kset, driver kset, device chain, and driver chain */

Struct subsys_private * P;

};

 

8.2 class attributes

L class attributes are represented by class_attribute

Structclass_attribute {

Struct attribute ATTR;

Ssize_t (* Show) (struct class * class, struct class_attribute * ATTR,

Char * BUF );

Ssize_t (* store) (struct class * class, struct class_attribute * ATTR,

Const char * Buf, size_t count );

};

Class_attr (_ name, _ mode, _ show, _ store)

L attribute operations

Int _ must_checkclass_create_file (struct class * class,

Const struct class_attribute * ATTR );

Voidclass_remove_file (struct class * class,

Const struct class_attribute * ATTR );

 

8.3 basic class operations

L class registration and cancellation

Intclass_register (struct class * class );

Voidclass_unregister (struct class * class );

 

L class creation and destruction

Struct class * class_create (struct module * owner, const char * name)

Voidclass_destroy (struct class * CLs );

L add and delete devices to and from a class

Struct device * device_create (struct class * class, struct device * parent,

Dev_t devt, void * drvdata, const char * FMT ,...)

Voiddevice_destroy (struct class * class, dev_t devt)

Devt: Device number

 

8.4 instance resolution

/*

* For learn class

*/

# Include <Linux/init. h>

# Include <Linux/module. h>

# Include <Linux/kernel. h>

# Include <Linux/device. h>

 

Static char * version = "revision 1.0, scclass ";

 

/* Define a class and export the symbol */

Struct class * scclass;

Export_symbol_gpl (scclass );

 

/*

* Export class attribute

*/

Static ssize_t class_show_version (structclass * class,

Structclass_attribute * ATTR, char * BUF)

{

Returnsprintf (BUF, "% s \ n", version );

}

Class_attr (version, 0666, class_show_version, null );

 

Static int _ init scclass_init (void)

{

Interr;

 

/* Create a class named scclass */

Scclass = class_create (this_module, "scclass ");

Err = ptr_err (scclass );

If (is_err (scclass ))

Returnerr;

/* Create an attribute file version */

Err = class_create_file (scclass, & class_attr_version );

If (ERR)

Gotoerr_class_create;

 

Printk ("classcreate \ n ");

Return0;

 

Err_class_create:

Class_destroy (scclass );

Returnerr;

}

 

Static void _ exit scclass_exit (void)

{

Class_remove_file (scclass, & class_attr_version );

Class_destroy (scclass );

Printk ("classdestory \ n ");

}

 

Module_init (scclass_init );

Module_exit (scclass_exit );

 

Module_license ("GPL ");

Module_author ("cjok <cjok.liao@gmail.com> ");

The code above only creates a class. We also need to add a device to the class and modify the device code:

@-8, 6 + 8, 7 @@

Extern struct bus_type scbus_type;
Extern struct device scbus;
+ Extern struct class * scclass;

Static char * version = "revision 1.0, scdevice ";

@-45, 6 + 46,8 @@
Ret = device_create_file (& scdevice, & dev_attr_version );
If (RET)
Goto err_create;
+
+ Device_create (scclass, null, 0, 0, "scdevice0 ");

Printk ("Create a scdevice ");
Return 0;
-56,6 + 59,7 @@

Static void _ exit scdevice_exit (void)
{
+ Device_destroy (scclass, 0 );
Device_remove_file (& scdevice, & dev_attr_version );

Add code in gray

 

Test results:

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.