1, create the corresponding kobj_attribute. This experiment used to turn on the flashlight, turn off the flashlight, turn on the flashing lights, turn off the flash. So create four kobj_attribute. Because the four formats are similar, only the function function implementation function is different, so this article only takes Sysfs_torch as an example to explain.
static struct Kobj_attribute Sysfs_torch = __attr (torch,s_irugo,sysfs_torch,null)
<pre name= "code" class= "CPP" >static struct Kobj_attribute sysfs_off = __attr (off,s_irugo,sysfs_off,null)
Kobj_attribute definition
struct Kobj_attribute { struct attribute attr; ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr, char *buf); ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr, const char *BUF, size_t count);};
Where __attr is a macro, it is defined as follows:
#define __ATTR (_name, _mode, _show, _store) { . ATTR = {. Name = __stringify (_name),. Mode = _mode}, . Show = _s How, . Store = _store, }
2, create the corresponding function function Sysfs_torch
Static ssize_t sysfs_torch (struct kobject *kobj,struct kobj_attribute *attr, char *buf) {
<span style= "White-space:pre" ></span> implement flashlight function code. }
3. Create struct attribute struct array and struct Attribute_group object.
static struct attribute *flash_sysfs[] = {&sysfs_torch.attr,&sysfs_off.attr,&sysfs_flash.attr,null,}
static struct Attribute_group Flash_attr_group = {. Attrs = Flash_sysfs,}
4. Writing the init function
init function main Call function
Sysfs_create_group (struct kobject *kobj,const struct attribute_group *grp);
The pass-in parameters enable the use of SYSFS to control the flash.
You can also create a struct kobject assignment to null and create it using Kobject_create_and_add (const char *name, struct kobject *parent). Pass the return value to Sysfs_create_group
When I implement, add sysfs_create_group directly in Platform_probe, according to the command created, will be persisted and your device in the devices directory, if you do not know in that directory, you can use the command
Find./-name "You define the name" to search.
5. Exit function
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SYSFS application-------Use SYSFS to control the strobe light