diff options
author | mananth <mananth> | 2003-10-15 12:19:21 +0000 |
---|---|---|
committer | mananth <mananth> | 2003-10-15 12:19:21 +0000 |
commit | 7838df3254db021c96f1c53593c1753cb4315057 (patch) | |
tree | 3626e61378839368b1b719baf2649528682c5eb6 | |
parent | a8dc5520e8863135dd277f37711564a8b4c73a06 (diff) | |
download | sysfsutils-7838df3254db021c96f1c53593c1753cb4315057.tar.gz |
More write attribute changes
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | include/libsysfs.h | 12 | ||||
-rw-r--r-- | lib/sysfs_class.c | 48 | ||||
-rw-r--r-- | lib/sysfs_device.c | 58 | ||||
-rw-r--r-- | lib/sysfs_dir.c | 15 | ||||
-rw-r--r-- | lib/sysfs_driver.c | 41 | ||||
-rw-r--r-- | lib/sysfs_utils.c | 68 |
7 files changed, 162 insertions, 88 deletions
@@ -1,4 +1,12 @@ +09/24/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com> + * Added "write" attribute functions specific to device, + driver and class device. + * Removed generic write function + sysfs_change_attribue_value() + * Changed sysfs_open_device_by_name() to + sysfs_change_device_by_id() + 09/23/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com> * Fixed "write" attribute functions. * Removed sysfs_write_attribute_value() diff --git a/include/libsysfs.h b/include/libsysfs.h index d7fc69b..2a96b6d 100644 --- a/include/libsysfs.h +++ b/include/libsysfs.h @@ -142,8 +142,6 @@ extern int sysfs_get_link(const unsigned char *path, unsigned char *target, extern struct dlist *sysfs_open_subsystem_list(unsigned char *name); extern struct dlist *sysfs_open_bus_devices_list(unsigned char *name); extern void sysfs_close_list(struct dlist *list); -extern int sysfs_change_attribute_value(unsigned char *device, - unsigned char *attribute, unsigned char *value); /* sysfs directory and file access */ extern void sysfs_close_attribute(struct sysfs_attribute *sysattr); @@ -177,6 +175,8 @@ extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver); extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver); extern struct sysfs_driver *sysfs_open_driver_by_name (unsigned char *drv_name, unsigned char *bus, size_t bsize); +extern int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value); /* generic sysfs device access */ extern void sysfs_close_root_device(struct sysfs_root_device *root); @@ -189,8 +189,10 @@ extern struct sysfs_device *sysfs_open_device_tree(const unsigned char *path); extern struct sysfs_attribute *sysfs_get_device_attr (struct sysfs_device *dev, const unsigned char *name); extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device); -extern struct sysfs_device *sysfs_open_device_by_name - (unsigned char *dev_name, unsigned char *bus, size_t bsize); +extern struct sysfs_device *sysfs_open_device_by_id + (unsigned char *bus_id, unsigned char *bus, size_t bsize); +extern int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value); /* generic sysfs bus access */ extern void sysfs_close_bus(struct sysfs_bus *bus); @@ -221,6 +223,8 @@ extern struct dlist *sysfs_get_classdev_attributes (struct sysfs_class_device *cdev); extern int sysfs_find_device_class_name(unsigned char *bus_id, unsigned char *classname, size_t bsize); +extern int sysfs_write_classdev_attr(unsigned char *dev, + unsigned char *attrib, unsigned char *value); #ifdef __cplusplus } diff --git a/lib/sysfs_class.c b/lib/sysfs_class.c index 898a5bc..6c969b5 100644 --- a/lib/sysfs_class.c +++ b/lib/sysfs_class.c @@ -406,4 +406,50 @@ int sysfs_find_device_class_name(unsigned char *bus_id, } return -1; } - + +/** + * sysfs_write_classdev_attr: modify writable attribute value for the given + * class device + * @dev: class device name for which the attribute has to be changed + * @attrib: attribute to change + * @value: value to change to + * Returns 0 on success and -1 on error + */ +int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value) +{ + struct sysfs_class_device *clsdev = NULL; + struct sysfs_attribute *attribute = NULL; + unsigned char class_name[SYSFS_NAME_LEN]; + + if (dev == NULL || attrib == NULL || value == NULL) { + dprintf("Invalid parameters\n"); + return -1; + } + + memset(class_name, 0, SYSFS_NAME_LEN); + if ((sysfs_find_device_class_name(dev, + class_name, SYSFS_NAME_LEN)) < 0) { + dprintf("Class device %s not found\n", dev); + return -1; + } + clsdev = sysfs_open_class_device_by_name(class_name, dev); + if (clsdev == NULL) { + dprintf("Error opening %s in class %s\n", dev, class_name); + return -1; + } + attribute = sysfs_get_directory_attribute(clsdev->directory, attrib); + if (attribute == NULL) { + dprintf("Attribute %s not defined for device %s on class %s\n", + attrib, dev, class_name); + sysfs_close_class_device(clsdev); + return -1; + } + if ((sysfs_write_attribute(attribute, value)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_class_device(clsdev); + return -1; + } + sysfs_close_class_device(clsdev); + return 0; +} diff --git a/lib/sysfs_device.c b/lib/sysfs_device.c index 6f28a24..afd5e63 100644 --- a/lib/sysfs_device.c +++ b/lib/sysfs_device.c @@ -340,27 +340,27 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) /** * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem) - * @dev_name: name of the device to open - has to be the name in + * @bus_id: bus_id of the device to open - has to be the "bus_id" in * /sys/bus/xxx/devices * @bus: bus the device belongs to * @bsize: size of the bus buffer * returns struct sysfs_device if found, NULL otherwise * NOTE: Use sysfs_close_device to close the device */ -struct sysfs_device *sysfs_open_device_by_name(unsigned char *dev_name, +struct sysfs_device *sysfs_open_device_by_id(unsigned char *bus_id, unsigned char *bus, size_t bsize) { char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX]; struct sysfs_device *device = NULL; struct stat astats; - if (dev_name == NULL || bus == NULL) { + if (bus_id == NULL || bus == NULL) { errno = EINVAL; return NULL; } - - if ((sysfs_find_device_bus_name(dev_name, bus, bsize)) != 0) { - dprintf("Device %s not found\n", dev_name); + + if ((sysfs_find_device_bus_name(bus_id, bus, bsize)) != 0) { + dprintf("Device %s not found\n", bus_id); errno = EINVAL; return NULL; } @@ -374,7 +374,7 @@ struct sysfs_device *sysfs_open_device_by_name(unsigned char *dev_name, strcat(sysfs_path, "/"); strncat(sysfs_path, bus, bsize); strcat(sysfs_path, "/devices/"); /* sysfs_path = "/sys/bus/xxx/devices/" */ - strcat(sysfs_path, dev_name); + strcat(sysfs_path, bus_id); /* devices under /sys/bus/xxx/devices are links to devices subsystem */ if ((sysfs_get_link(sysfs_path, device_path, SYSFS_PATH_MAX)) < 0) { @@ -384,9 +384,51 @@ struct sysfs_device *sysfs_open_device_by_name(unsigned char *dev_name, device = sysfs_open_device(device_path); if (device == NULL) { - dprintf("Error opening device %s\n", dev_name); + dprintf("Error opening device %s\n", bus_id); return NULL; } return device; } + +/** + * sysfs_write_device_attr: modify a "writable" attribute for the given device + * @dev: device bus_id for which attribute has to be changed + * @attrib: attribute to change + * @value: value to change to + * Returns 0 on success -1 on error + */ +int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value) +{ + struct sysfs_device *device = NULL; + struct sysfs_attribute *attribute = NULL; + unsigned char subsys_name[SYSFS_NAME_LEN]; + + if (dev == NULL || attrib == NULL || value == NULL) { + dprintf("Invalid parameters\n"); + return -1; + } + + memset(subsys_name, 0, SYSFS_NAME_LEN); + device = sysfs_open_device_by_id(dev, subsys_name, SYSFS_NAME_LEN); + if (device == NULL) { + dprintf("Device %s not found\n", dev); + return -1; + } + attribute = sysfs_get_directory_attribute(device->directory, attrib); + if (attribute == NULL) { + dprintf("Attribute %s not defined for device %s\n", + attrib, dev); + sysfs_close_device(device); + return -1; + } + if ((sysfs_write_attribute(attribute, value)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_device(device); + return -1; + } + sysfs_close_device(device); + return 0; +} + diff --git a/lib/sysfs_dir.c b/lib/sysfs_dir.c index af48032..1d730e7 100644 --- a/lib/sysfs_dir.c +++ b/lib/sysfs_dir.c @@ -182,10 +182,12 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr, sysattr->path); return -1; } - if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { - dprintf("Attribute %s already has the requested value %s\n", - sysattr->name, new_value); - return 0; + if (sysattr->method & SYSFS_METHOD_SHOW) { + if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { + dprintf("Attribute %s already has the requested value %s\n", + sysattr->name, new_value); + return 0; + } } /* * open O_WRONLY since some attributes have no "read" but only @@ -199,9 +201,8 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr, length = write(fd, new_value, strlen(new_value)); if (length < 0) { - perror("sysfs_write_attribute: write"); - dprintf("Error write to the attribute %s\n", - sysattr->path); + dprintf("Error writing to the attribute %s - invalid value?\n", + sysattr->name); close(fd); return -1; } diff --git a/lib/sysfs_driver.c b/lib/sysfs_driver.c index 6a5a913..3c98bba 100644 --- a/lib/sysfs_driver.c +++ b/lib/sysfs_driver.c @@ -222,3 +222,44 @@ open_device: return driver; } +/** + * sysfs_write_driver_attr: modify "writable" driver attribute + * @drv: driver whose attribute has to be modified + * @attrib: Attribute to be modified + * @value: Value to change to + * Returns 0 on success -1 on failure + */ +int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value) +{ + struct sysfs_driver *driver = NULL; + struct sysfs_attribute *attribute = NULL; + unsigned char busname[SYSFS_NAME_LEN]; + + if (drv == NULL || attrib == NULL || value == NULL) { + dprintf("Invalid parameters\n"); + return -1; + } + + memset(busname, 0, SYSFS_NAME_LEN); + driver = sysfs_open_driver_by_name(drv, busname, SYSFS_NAME_LEN); + if (driver == NULL) { + dprintf("Driver %s not found\n", drv); + return -1; + } + attribute = sysfs_get_directory_attribute(driver->directory, attrib); + if (attribute == NULL) { + dprintf("Attribute %s not defined for driver %s\n", + attrib, drv); + sysfs_close_drv(driver); + return -1; + } + if ((sysfs_write_attribute(attribute, value)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_drv(driver); + return -1; + } + sysfs_close_drv(driver); + return 0; +} + diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c index 617311e..0d2b481 100644 --- a/lib/sysfs_utils.c +++ b/lib/sysfs_utils.c @@ -286,71 +286,3 @@ struct dlist *sysfs_open_bus_devices_list(unsigned char *name) 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; -} - |