From 2ce335030ece164a7e0b3f124b30e373586f6372 Mon Sep 17 00:00:00 2001 From: mananth Date: Wed, 15 Oct 2003 12:24:43 +0000 Subject: Added support functions; use #defined values where possible --- ChangeLog | 5 +++++ include/libsysfs.h | 6 ++++-- lib/sysfs_bus.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- lib/sysfs_device.c | 11 ++++++----- lib/sysfs_driver.c | 49 +++++++++++++++---------------------------------- lib/sysfs_utils.c | 5 +++-- 6 files changed, 84 insertions(+), 46 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8a8a97e..0e3a509 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,9 @@ +09/30/2003 - Ananth Mavinakayanahalli + * Modified routines to find device/driver on a bus + * Made some miscellaneous changes to use #defines + instead of hardcoding values + 09/29/2003 - Ananth Mavinakayanahalli * Added "block" subsystem support * Modified systool to use sysfs_root_device diff --git a/include/libsysfs.h b/include/libsysfs.h index da0a3ee..7fe8139 100644 --- a/include/libsysfs.h +++ b/include/libsysfs.h @@ -212,7 +212,7 @@ 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_id - (unsigned char *bus_id, unsigned char *bus, size_t bsize); + (const 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); @@ -228,7 +228,9 @@ extern struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus, unsigned char *attrname); extern struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, unsigned char *dev_id); -extern int sysfs_find_device_bus_name(unsigned char *dev_id, +extern int sysfs_find_device_bus(const unsigned char *dev_id, + unsigned char *busname, size_t bsize); +extern int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname, size_t bsize); /* generic sysfs class access */ diff --git a/lib/sysfs_bus.c b/lib/sysfs_bus.c index 7ef4fa8..4d42c29 100644 --- a/lib/sysfs_bus.c +++ b/lib/sysfs_bus.c @@ -406,9 +406,11 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, return NULL; } - strcat(path, "/bus/"); + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); strcat(path, busname); - strcat(path, "/devices/"); + strcat(path, SYSFS_DEVICES_DIR); + strcat(path, "/"); strcat(path, dev_id); rdev = sysfs_open_device(path); @@ -428,7 +430,7 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, * @bsize: buffer size * returns 0 with success or -1 with error */ -int sysfs_find_device_bus_name(unsigned char *dev_id, unsigned char *busname, +int sysfs_find_device_bus(const unsigned char *dev_id, unsigned char *busname, size_t bsize) { unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL; @@ -463,3 +465,49 @@ int sysfs_find_device_bus_name(unsigned char *dev_id, unsigned char *busname, return -1; } +/** + * sysfs_find_driver_bus: locates the bus the driver is on. + * @driver: name of the driver to locate + * @busname: buffer to copy name to + * @bsize: buffer size + * returns 0 with success, -1 with error + */ +int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname, + size_t bsize) +{ + unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL; + struct dlist *buslist = NULL, *drivers = NULL; + + if (driver == NULL || busname == NULL) { + errno = EINVAL; + return -1; + } + + memset(subsys, 0, SYSFS_PATH_MAX); + strcpy(subsys, SYSFS_BUS_DIR); + buslist = sysfs_open_subsystem_list(subsys); + if (buslist != NULL) { + dlist_for_each_data(buslist, bus, char) { + memset(subsys, 0, SYSFS_PATH_MAX); + strcpy(subsys, SYSFS_BUS_DIR); + strcat(subsys, "/"); + strcat(subsys, bus); + strcat(subsys, SYSFS_DRIVERS_DIR); + drivers = sysfs_open_subsystem_list(subsys); + if (drivers != NULL) { + dlist_for_each_data(drivers, curdrv, char) { + if (strcmp(driver, curdrv) == 0) { + strncpy(busname, bus, bsize); + sysfs_close_list(drivers); + sysfs_close_list(buslist); + return 0; + } + } + sysfs_close_list(drivers); + } + } + sysfs_close_list(buslist); + } + return -1; +} + diff --git a/lib/sysfs_device.c b/lib/sysfs_device.c index afd5e63..59d2f1d 100644 --- a/lib/sysfs_device.c +++ b/lib/sysfs_device.c @@ -347,7 +347,7 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) * returns struct sysfs_device if found, NULL otherwise * NOTE: Use sysfs_close_device to close the device */ -struct sysfs_device *sysfs_open_device_by_id(unsigned char *bus_id, +struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, unsigned char *bus, size_t bsize) { char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX]; @@ -358,13 +358,13 @@ struct sysfs_device *sysfs_open_device_by_id(unsigned char *bus_id, errno = EINVAL; return NULL; } - - if ((sysfs_find_device_bus_name(bus_id, bus, bsize)) != 0) { +/* + if ((sysfs_find_device_bus(bus_id, bus, bsize)) != 0) { dprintf("Device %s not found\n", bus_id); errno = EINVAL; return NULL; } - +*/ memset(sysfs_path, 0, SYSFS_PATH_MAX); if ((sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX)) != 0) { dprintf("Error getting sysfs mount path\n"); @@ -373,7 +373,8 @@ struct sysfs_device *sysfs_open_device_by_id(unsigned char *bus_id, strcat(sysfs_path, SYSFS_BUS_DIR); strcat(sysfs_path, "/"); strncat(sysfs_path, bus, bsize); - strcat(sysfs_path, "/devices/"); /* sysfs_path = "/sys/bus/xxx/devices/" */ + strcat(sysfs_path, SYSFS_DEVICES_DIR); + strcat(sysfs_path, "/"); strcat(sysfs_path, bus_id); /* devices under /sys/bus/xxx/devices are links to devices subsystem */ diff --git a/lib/sysfs_driver.c b/lib/sysfs_driver.c index 3c98bba..fe19af9 100644 --- a/lib/sysfs_driver.c +++ b/lib/sysfs_driver.c @@ -155,8 +155,7 @@ struct sysfs_driver *sysfs_open_driver_by_name(unsigned char *drv_name, struct sysfs_driver *driver = NULL; struct sysfs_device *device = NULL; struct sysfs_link *curlink = NULL; - char path[SYSFS_PATH_MAX], fullpath[SYSFS_PATH_MAX], - *curbus = NULL, *curdrv = NULL; + unsigned char path[SYSFS_PATH_MAX]; if (drv_name == NULL || bus == NULL) { errno = EINVAL; @@ -164,41 +163,19 @@ struct sysfs_driver *sysfs_open_driver_by_name(unsigned char *drv_name, } memset(path, 0, SYSFS_PATH_MAX); - memset(fullpath, 0, SYSFS_PATH_MAX); - buslist = sysfs_open_subsystem_list(SYSFS_BUS_DIR); - if (buslist != NULL) { - dlist_for_each_data(buslist, curbus, char) { - strcpy(path, SYSFS_BUS_DIR); - strcat(path, "/"); - strcat(path, curbus); - strcat(path, "/drivers/"); - drivers = sysfs_open_subsystem_list(path); - if (drivers != NULL) { - dlist_for_each_data(drivers, curdrv, char) { - if (strcmp(drv_name, curdrv) == 0) { - strncpy(bus, curbus, bsize); - strcat(path, curdrv); - sysfs_close_list(drivers); - sysfs_close_list(buslist); - goto open_device; - } - } - sysfs_close_list(drivers); - } - } - sysfs_close_list(buslist); - return NULL; - } - -open_device: - if (sysfs_get_mnt_path(fullpath, SYSFS_PATH_MAX) != 0) { + if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { dprintf("Error getting sysfs mount path\n"); return NULL; } - strcat(fullpath, path); - driver = sysfs_open_driver(fullpath); + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); + strcat(path, bus); + strcat(path, SYSFS_DRIVERS_DIR); + strcat(path, "/"); + strcat(path, drv_name); + driver = sysfs_open_driver(path); if (driver == NULL) { - dprintf("Driver %s not found\n", drv_name); + dprintf("Could not open driver %s\n", drv_name); return NULL; } if (driver->directory->links != NULL) { @@ -242,9 +219,13 @@ int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, } memset(busname, 0, SYSFS_NAME_LEN); + if ((sysfs_find_driver_bus(drv, busname, SYSFS_NAME_LEN)) < 0) { + dprintf("Driver %s not found\n", drv); + return -1; + } driver = sysfs_open_driver_by_name(drv, busname, SYSFS_NAME_LEN); if (driver == NULL) { - dprintf("Driver %s not found\n", drv); + dprintf("Could not open driverr %s\n", drv); return -1; } attribute = sysfs_get_directory_attribute(driver->directory, attrib); diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c index 0d2b481..4475342 100644 --- a/lib/sysfs_utils.c +++ b/lib/sysfs_utils.c @@ -251,9 +251,10 @@ struct dlist *sysfs_open_bus_devices_list(unsigned char *name) return NULL; } - strcat(sysfs_path, "/bus/"); + strcat(sysfs_path, SYSFS_BUS_DIR); + strcat(sysfs_path, "/"); strcat(sysfs_path, name); - strcat(sysfs_path, "/devices"); + strcat(sysfs_path, SYSFS_DEVICES_DIR); dir = sysfs_open_directory(sysfs_path); if (dir == NULL) { dprintf("Error opening sysfs_directory at %s\n", sysfs_path); -- cgit v1.2.1