summaryrefslogtreecommitdiff
path: root/lib/sysfs_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sysfs_utils.c')
-rw-r--r--lib/sysfs_utils.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c
index 2dcf22a..617311e 100644
--- a/lib/sysfs_utils.c
+++ b/lib/sysfs_utils.c
@@ -285,3 +285,72 @@ struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
sysfs_close_directory(dir);
return list;
}
+
+
+/**
+ * sysfs_change_attribute_value: "write" attribute support
+ * @device: bus/class device for which attribute has to be changed
+ * @attribute: attribute thats needs modification
+ * @value: value to change to
+ * returns 0 on success and -1 on error
+ */
+int sysfs_change_attribute_value(unsigned char *device,
+ unsigned char *attribute, unsigned char *value)
+{
+ struct sysfs_device *dev = NULL;
+ struct sysfs_class_device *clsdev = NULL;
+ struct sysfs_attribute *attrib = NULL;
+ struct dlist *attributes = NULL;
+ char subsys_name[SYSFS_NAME_LEN];
+
+ if (device == NULL || attribute == NULL || value == NULL) {
+ dprintf("Incorrect parameters supplied\n");
+ return -1;
+ }
+
+ dev = sysfs_open_device_by_name(device, subsys_name, SYSFS_NAME_LEN);
+ if (dev != NULL) {
+ attrib = sysfs_get_directory_attribute(dev->directory,
+ attribute);
+ if (attrib == NULL) {
+ dprintf("Attribute %s not defined for device %s\n",
+ attribute, device);
+ sysfs_close_device(dev);
+ return -1;
+ }
+ if ((sysfs_write_attribute(attrib, value)) < 0) {
+ dprintf("Error writing value %s to attribute %s\n",
+ value, attribute);
+ sysfs_close_device(dev);
+ return -1;
+ }
+ return 0;
+ }
+
+ /* not found in bus subsys - look in class */
+ if ((sysfs_find_device_class_name(device, subsys_name,
+ SYSFS_NAME_LEN)) < 0) {
+ dprintf("Device %s not found\n", device);
+ return -1;
+ }
+ clsdev = sysfs_open_class_device_by_name(subsys_name, device);
+ if (clsdev != NULL) {
+ attrib = sysfs_get_directory_attribute(clsdev->directory,
+ attribute);
+ if (attrib != NULL) {
+ if ((sysfs_write_attribute(attrib, value)) == 0) {
+ sysfs_close_class_device(clsdev);
+ return 0;
+ }
+ dprintf("Error writing attribute value\n");
+ sysfs_close_class_device(clsdev);
+ return -1;
+ }
+ dprintf("Attribute %s not defined for device %s\n",
+ attribute, device);
+ return -1;
+ }
+ dprintf("Device %s not found\n", device);
+ return -1;
+}
+