From 89b2f6ff171dcf09b165541d79012471c6035e18 Mon Sep 17 00:00:00 2001 From: mananth Date: Wed, 15 Oct 2003 12:11:03 +0000 Subject: Added sysfs_open_bus_devices_list() and simplified sysfs_find_device_bus_name --- ChangeLog | 4 ++++ include/libsysfs.h | 1 + lib/sysfs_bus.c | 64 ++++++++++++++++++++---------------------------------- lib/sysfs_utils.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index e453a9c..e785fb8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ +09/05/2003 - Ananth Mavinakayanahalli + * Add sysfs_open_bus_devices_list() + * Simplify sysfs_find_device_bus_name() + 09/04/2003 - Ananth Mavinakayanahalli * Simplify sysfs_open_bus_device diff --git a/include/libsysfs.h b/include/libsysfs.h index 8a0fb4d..8f0cd87 100644 --- a/include/libsysfs.h +++ b/include/libsysfs.h @@ -140,6 +140,7 @@ extern int sysfs_get_name_from_path(const unsigned char *path, extern int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len); 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); /* sysfs directory and file access */ diff --git a/lib/sysfs_bus.c b/lib/sysfs_bus.c index 2b169f8..7ef4fa8 100644 --- a/lib/sysfs_bus.c +++ b/lib/sysfs_bus.c @@ -431,53 +431,35 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, int sysfs_find_device_bus_name(unsigned char *dev_id, unsigned char *busname, size_t bsize) { - struct sysfs_directory *busdir = NULL, *cur = NULL, *devdir = NULL; - struct sysfs_link *ln = NULL; - unsigned char path[SYSFS_PATH_MAX]; - unsigned char devpath[SYSFS_PATH_MAX]; + unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL; + struct dlist *buslist = NULL, *device_list = NULL; if (dev_id == NULL || busname == NULL) { errno = EINVAL; return -1; } - if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { - dprintf ("Error finding sysfs root\n"); - return -1; - } - strcat(path, SYSFS_BUS_DIR); - busdir = sysfs_open_directory(path); - if (busdir == NULL) { - dprintf ("Error retrieving bus directory %s\n", path); - return -1; - } - if (sysfs_read_directory(busdir) != 0) { - dprintf ("Error reading directory %s\n", path); - sysfs_close_directory(busdir); - return -1; - } - if (busdir->subdirs != NULL) { - dlist_for_each_data(busdir->subdirs, cur, - struct sysfs_directory) { - memset(devpath, 0, SYSFS_PATH_MAX); - strcpy(devpath, cur->path); - strcat(devpath, SYSFS_DEVICES_DIR); - devdir = sysfs_open_directory(devpath); - if (devdir == NULL) - continue; - if (sysfs_read_directory(devdir) != 0) { - sysfs_close_directory(devdir); - continue; - } - ln = sysfs_get_subdirectory_link(devdir, dev_id); - if (ln != NULL) { - strcpy(busname, cur->name); - sysfs_close_directory(devdir); - return 0; + + strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */ + buslist = sysfs_open_subsystem_list(subsys); + if (buslist != NULL) { + dlist_for_each_data(buslist, bus, char) { + device_list = sysfs_open_bus_devices_list(bus); + if (device_list != NULL) { + dlist_for_each_data(device_list, + curdev, char) { + if (strcmp(dev_id, curdev) == 0) { + strncpy(busname, + bus, bsize); + sysfs_close_list(device_list); + sysfs_close_list(buslist); + return 0; + } + } + sysfs_close_list(device_list); } - sysfs_close_directory(devdir); } + sysfs_close_list(buslist); } - strcpy(busname, SYSFS_UNKNOWN); - sysfs_close_directory(busdir); - return 0; + return -1; } + diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c index 16a2d77..2dcf22a 100644 --- a/lib/sysfs_utils.c +++ b/lib/sysfs_utils.c @@ -229,4 +229,59 @@ struct dlist *sysfs_open_subsystem_list(unsigned char *name) sysfs_close_directory(dir); return list; } - + + +/** + * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus + * @name: name of the subsystem, eg., "pci", "scsi", "usb" + * Returns a dlist of supported names or NULL if subsystem not supported + */ +struct dlist *sysfs_open_bus_devices_list(unsigned char *name) +{ + unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL; + struct sysfs_directory *dir = NULL; + struct sysfs_link *cur = NULL; + struct dlist *list = NULL; + + if (name == NULL) + return NULL; + + if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount point\n"); + return NULL; + } + + strcat(sysfs_path, "/bus/"); + strcat(sysfs_path, name); + strcat(sysfs_path, "/devices"); + dir = sysfs_open_directory(sysfs_path); + if (dir == NULL) { + dprintf("Error opening sysfs_directory at %s\n", sysfs_path); + return NULL; + } + + if (sysfs_read_directory(dir) != 0) { + dprintf("Error reading sysfs_directory at %s\n", sysfs_path); + sysfs_close_directory(dir); + return NULL; + } + + if (dir->links != NULL) { + list = dlist_new_with_delete(SYSFS_NAME_LEN, + sysfs_del_name); + if (list == NULL) { + dprintf("Error creating list\n"); + sysfs_close_directory(dir); + return NULL; + } + + dlist_for_each_data(dir->links, cur, + struct sysfs_link) { + device_name = (char *)calloc(1, SYSFS_NAME_LEN); + strcpy(device_name, cur->name); + dlist_unshift(list, device_name); + } + } + sysfs_close_directory(dir); + return list; +} -- cgit v1.2.1