summaryrefslogtreecommitdiff
path: root/lib/sysfs_bus.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sysfs_bus.c')
-rw-r--r--lib/sysfs_bus.c467
1 files changed, 283 insertions, 184 deletions
diff --git a/lib/sysfs_bus.c b/lib/sysfs_bus.c
index c7b6036..d47b94c 100644
--- a/lib/sysfs_bus.c
+++ b/lib/sysfs_bus.c
@@ -3,7 +3,7 @@
*
* Generic bus utility functions for libsysfs
*
- * Copyright (C) 2003 International Business Machines, Inc.
+ * Copyright (C) IBM Corp. 2003
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -23,28 +23,62 @@
#include "libsysfs.h"
#include "sysfs.h"
+static void sysfs_close_dev(void *dev)
+{
+ sysfs_close_device((struct sysfs_device *)dev);
+}
+
+static void sysfs_close_drv(void *drv)
+{
+ sysfs_close_driver((struct sysfs_driver *)drv);
+}
+
+/*
+ * compares devices' bus ids.
+ * @a: device id looking for
+ * @b: sysfs_device comparing being compared
+ * returns 1 if a==b->bus_id or 0 not equal
+ */
+static int bus_device_id_equal(void *a, void *b)
+{
+ if (a == NULL || b == NULL)
+ return 0;
+
+ if (strcmp(((char *)a), ((struct sysfs_device *)b)->bus_id)
+ == 0)
+ return 1;
+ return 0;
+}
+
+/*
+ * compares drivers' names.
+ * @a: driver name looking for
+ * @b: sysfs_driver comparing being compared
+ * returns 1 if a==b->name or 0 not equal
+ */
+static int bus_driver_name_equal(void *a, void *b)
+{
+ if (a == NULL || b == NULL)
+ return 0;
+
+ if (strcmp(((char *)a), ((struct sysfs_driver *)b)->name) == 0)
+ return 1;
+ return 0;
+}
+
/**
* sysfs_close_bus: close single bus
* @bus: bus structure
*/
void sysfs_close_bus(struct sysfs_bus *bus)
{
- struct sysfs_device *curdev = NULL, *nextdev = NULL;
- struct sysfs_driver *curdrv = NULL, *nextdrv = NULL;
-
if (bus != NULL) {
if (bus->directory != NULL)
sysfs_close_directory(bus->directory);
- for (curdev = bus->devices; curdev != NULL;
- curdev = nextdev) {
- nextdev = curdev->next;
- sysfs_close_device(curdev);
- }
- for (curdrv = bus->drivers; curdrv != NULL;
- curdrv = nextdrv) {
- nextdrv = curdrv->next;
- sysfs_close_driver(curdrv);
- }
+ if (bus->devices)
+ dlist_destroy(bus->devices);
+ if (bus->drivers)
+ dlist_destroy(bus->drivers);
free(bus);
}
}
@@ -59,243 +93,308 @@ static struct sysfs_bus *alloc_bus(void)
}
/**
- * open_bus_dir: opens up sysfs bus directory
- * returns sysfs_directory struct with success and NULL with error
+ * sysfs_get_bus_devices: gets all devices for bus
+ * @bus: bus to get devices for
+ * returns dlist of devices with success and NULL with failure
*/
-static struct sysfs_directory *open_bus_dir(const char *name)
+struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
{
- struct sysfs_directory *busdir = NULL, *cur = NULL, *next = NULL;
- char buspath[SYSFS_PATH_MAX];
+ struct sysfs_device *bdev = NULL;
+ struct sysfs_directory *devdir = NULL;
+ struct sysfs_link *curl = NULL;
+ char path[SYSFS_PATH_MAX];
- if (name == NULL) {
+ if (bus == NULL) {
errno = EINVAL;
return NULL;
}
+ memset(path, 0, SYSFS_PATH_MAX);
+ safestrcpy(path, bus->path);
+ safestrcat(path, "/");
+ safestrcat(path, SYSFS_DEVICES_NAME);
+ devdir = sysfs_open_directory(path);
+ if (devdir == NULL)
+ return NULL;
- memset(buspath, 0, SYSFS_PATH_MAX);
- if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
- dprintf(stderr, "Sysfs not supported on this system\n");
+ if (sysfs_read_dir_links(devdir) != 0) {
+ sysfs_close_directory(devdir);
return NULL;
}
- strcat(buspath, SYSFS_BUS_DIR);
- strcat(buspath, "/");
- strcat(buspath, name);
- busdir = sysfs_open_directory(buspath);
- if (busdir == NULL) {
+ if (devdir->links != NULL) {
+ dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
+ bdev = sysfs_open_device_path(curl->target);
+ if (bdev == NULL) {
+ dprintf("Error opening device at %s\n",
+ curl->target);
+ continue;
+ }
+ if (bus->devices == NULL)
+ bus->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_device),
+ sysfs_close_dev);
+ dlist_unshift_sorted(bus->devices, bdev, sort_list);
+ }
+ }
+ sysfs_close_directory(devdir);
+
+ return (bus->devices);
+}
+
+/**
+ * sysfs_get_bus_drivers: get all pci drivers
+ * @bus: pci bus to add drivers to
+ * returns dlist of drivers with success and NULL with error
+ */
+struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
+{
+ struct sysfs_driver *driver = NULL;
+ struct sysfs_directory *drvdir = NULL;
+ struct sysfs_directory *cursub = NULL;
+ char path[SYSFS_PATH_MAX];
+
+ if (bus == NULL) {
errno = EINVAL;
- dprintf(stderr,"Bus %s not supported on this system\n",
- name);
return NULL;
}
- if ((sysfs_read_directory(busdir)) != 0) {
- dprintf(stderr, "Error reading %s bus dir %s\n", name,
- buspath);
- sysfs_close_directory(busdir);
+ memset(path, 0, SYSFS_PATH_MAX);
+ safestrcpy(path, bus->path);
+ safestrcat(path, "/");
+ safestrcat(path, SYSFS_DRIVERS_NAME);
+ drvdir = sysfs_open_directory(path);
+ if (drvdir == NULL)
+ return NULL;
+
+ if (sysfs_read_dir_subdirs(drvdir) != 0) {
+ sysfs_close_directory(drvdir);
return NULL;
}
- /* read in devices and drivers subdirs */
- for (cur = busdir->subdirs; cur != NULL; cur = next) {
- next = cur->next;
- if ((sysfs_read_directory(cur)) != 0)
- continue;
+ if (drvdir->subdirs != NULL) {
+ dlist_for_each_data(drvdir->subdirs, cursub,
+ struct sysfs_directory) {
+ driver = sysfs_open_driver_path(cursub->path);
+ if (driver == NULL) {
+ dprintf("Error opening driver at %s\n",
+ cursub->path);
+ continue;
+ }
+ if (bus->drivers == NULL)
+ bus->drivers = dlist_new_with_delete
+ (sizeof(struct sysfs_driver),
+ sysfs_close_drv);
+ dlist_unshift_sorted(bus->drivers, driver, sort_list);
+ }
}
-
- return busdir;
+ sysfs_close_directory(drvdir);
+ return (bus->drivers);
}
/**
- * add_dev_to_bus: adds a bus device to bus device list
- * @bus: bus to add the device
- * @dev: device to add
+ * sysfs_open_bus: opens specific bus and all its devices on system
+ * returns sysfs_bus structure with success or NULL with error.
*/
-static void add_dev_to_bus(struct sysfs_bus *bus, struct sysfs_device *dev)
+struct sysfs_bus *sysfs_open_bus(const char *name)
{
- if (bus != NULL && dev != NULL) {
- dev->next = bus->devices;
- bus->devices = dev;
+ struct sysfs_bus *bus = NULL;
+ char buspath[SYSFS_PATH_MAX];
+
+ if (name == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ memset(buspath, 0, SYSFS_PATH_MAX);
+ if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
+ dprintf("Sysfs not supported on this system\n");
+ return NULL;
}
+
+ safestrcat(buspath, "/");
+ safestrcat(buspath, SYSFS_BUS_NAME);
+ safestrcat(buspath, "/");
+ safestrcat(buspath, name);
+ if ((sysfs_path_is_dir(buspath)) != 0) {
+ dprintf("Invalid path to bus: %s\n", buspath);
+ return NULL;
+ }
+ bus = alloc_bus();
+ if (bus == NULL) {
+ dprintf("calloc failed\n");
+ return NULL;
+ }
+ safestrcpy(bus->name, name);
+ safestrcpy(bus->path, buspath);
+ if ((sysfs_remove_trailing_slash(bus->path)) != 0) {
+ dprintf("Incorrect path to bus %s\n", bus->path);
+ sysfs_close_bus(bus);
+ return NULL;
+ }
+
+ return bus;
}
/**
- * add_driver_to_bus: adds a bus driver to bus driver list
- * @bus: bus to add driver to
- * @driver: driver to add
+ * sysfs_get_bus_device: Get specific device on bus using device's id
+ * @bus: bus to find device on
+ * @id: bus_id for device
+ * returns struct sysfs_device reference or NULL if not found.
*/
-static void add_driver_to_bus(struct sysfs_bus *bus,
- struct sysfs_driver *driver)
+struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, char *id)
{
- if (bus != NULL && driver != NULL) {
- driver->next = bus->drivers;
- bus->drivers = driver;
+ if (bus == NULL || id == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (bus->devices == NULL) {
+ bus->devices = sysfs_get_bus_devices(bus);
+ if (bus->devices == NULL)
+ return NULL;
}
+
+ return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
+ bus_device_id_equal);
}
/**
- * get_all_bus_devices: gets all devices for bus
- * @bus: bus to get devices for
- * returns 0 with success and -1 with failure
+ * sysfs_get_bus_driver: Get specific driver on bus using driver name
+ * @bus: bus to find driver on
+ * @drvname: name of driver
+ * returns struct sysfs_driver reference or NULL if not found.
*/
-static int get_all_bus_devices(struct sysfs_bus *bus)
+struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
+ char *drvname)
{
- struct sysfs_device *bdev = NULL;
- struct sysfs_directory *cur = NULL;
- struct sysfs_link *curl = NULL, *nextl = NULL;
- char dirname[SYSFS_NAME_LEN];
-
- if (bus == NULL || bus->directory == NULL) {
+ if (bus == NULL || drvname == NULL) {
errno = EINVAL;
- return -1;
+ return NULL;
}
- for (cur = bus->directory->subdirs; cur != NULL; cur = cur->next) {
- memset(dirname, 0, SYSFS_NAME_LEN);
- if ((sysfs_get_name_from_path(cur->path, dirname,
- SYSFS_NAME_LEN)) != 0)
- continue;
- if (strcmp(dirname, SYSFS_DEVICES_NAME) != 0)
- continue;
- for (curl = cur->links; curl != NULL; curl = nextl) {
- nextl = curl->next;
- bdev = sysfs_open_device(curl->target);
- if (bdev == NULL) {
- dprintf(stderr, "Error opening device at %s\n",
- curl->target);
- continue;
- }
- add_dev_to_bus(bus, bdev);
- }
+
+ if (bus->drivers == NULL) {
+ bus->drivers = sysfs_get_bus_drivers(bus);
+ if (bus->drivers == NULL)
+ return NULL;
}
-
- return 0;
+
+ return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
+ bus_driver_name_equal);
}
/**
- * get_all_bus_drivers: get all pci drivers
- * @bus: pci bus to add drivers to
- * returns 0 with success and -1 with error
+ * sysfs_get_bus_attributes: returns bus' dlist of attributes
+ * @bus: bus to get attributes for.
+ * returns dlist of attributes or NULL if there aren't any.
*/
-static int get_all_bus_drivers(struct sysfs_bus *bus)
+struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
{
- struct sysfs_driver *driver = NULL;
- struct sysfs_directory *cur = NULL, *next = NULL;
- struct sysfs_directory *cursub = NULL, *nextsub = NULL;
- char dirname[SYSFS_NAME_LEN];
+ if (bus == NULL)
+ return NULL;
- if (bus == NULL || bus->directory == NULL) {
- errno = EINVAL;
- return -1;
+ if (bus->directory == NULL) {
+ bus->directory = sysfs_open_directory(bus->path);
+ if (bus->directory == NULL)
+ return NULL;
}
- for (cur = bus->directory->subdirs; cur != NULL; cur = next) {
- next = cur->next;
- memset(dirname, 0, SYSFS_NAME_LEN);
- if ((sysfs_get_name_from_path(cur->path, dirname,
- SYSFS_NAME_LEN)) != 0)
- continue;
- if (strcmp(dirname, SYSFS_DRIVERS_NAME) != 0)
- continue;
- for (cursub = cur->subdirs; cursub != NULL; cursub = nextsub) {
- nextsub = cursub->next;
- driver = sysfs_open_driver(cursub->path);
- if (driver == NULL) {
- dprintf(stderr, "Error opening driver at %s\n",
- cursub->path);
- continue;
- }
- add_driver_to_bus(bus, driver);
- }
+ if (bus->directory->attributes == NULL) {
+ if ((sysfs_read_dir_attributes(bus->directory)) != 0)
+ return NULL;
}
-
- return 0;
+ return bus->directory->attributes;
}
/**
- * match_bus_device_to_driver: returns 1 if device is bound to driver
- * @driver: driver to match
- * @busid: busid of device to match
- * returns 1 if found and 0 if not found
+ * sysfs_refresh_bus_attributes: refreshes the bus's list of attributes
+ * @bus: sysfs_bus whose attributes to refresh
+ *
+ * NOTE: Upon return, prior references to sysfs_attributes for this bus
+ * _may_ not be valid
+ *
+ * Returns list of attributes on success and NULL on failure
*/
-static int match_bus_device_to_driver(struct sysfs_driver *driver, char *busid)
+struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus)
{
- struct sysfs_link *cur = NULL, *next = NULL;
- int found = 0;
-
- if (driver == NULL || driver->directory == NULL || busid == NULL) {
+ if (bus == NULL) {
errno = EINVAL;
- return found;
+ return NULL;
}
- for (cur = driver->directory->links; cur != NULL && found == 0;
- cur = next) {
- next = cur->next;
- if ((strcmp(cur->name, busid)) == 0)
- found++;
+
+ if (bus->directory == NULL)
+ return (sysfs_get_bus_attributes(bus));
+
+ if ((sysfs_refresh_dir_attributes(bus->directory)) != 0) {
+ dprintf("Error refreshing bus attributes\n");
+ return NULL;
}
- return found;
+
+ return (bus->directory->attributes);
}
/**
- * link_bus_devices_to_drivers: goes through and links devices to drivers
- * @bus: bus to link
+ * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
+ * attributes.
+ * @bus: bus to retrieve attribute from
+ * @attrname: attribute name to retrieve
+ * returns reference to sysfs_attribute if found or NULL if not found
*/
-static void link_bus_devices_to_drivers(struct sysfs_bus *bus)
+struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
+ char *attrname)
{
- struct sysfs_device *dev = NULL, *nextdev = NULL;
- struct sysfs_driver *drv = NULL, *nextdrv = NULL;
+ struct dlist *attrlist = NULL;
- if (bus != NULL && bus->devices != NULL && bus->drivers != NULL) {
- for (dev = bus->devices; dev != NULL; dev = nextdev) {
- nextdev = dev->next;
-
- for (drv = bus->drivers; drv != NULL; drv = nextdrv) {
- nextdrv = drv->next;
- if ((match_bus_device_to_driver(drv,
- dev->bus_id)) != 0) {
- dev->driver = drv;
- drv->device = dev;
- }
- }
- }
+ if (bus == NULL) {
+ errno = EINVAL;
+ return NULL;
}
+ attrlist = sysfs_get_bus_attributes(bus);
+ if (attrlist == NULL)
+ return NULL;
+
+ return sysfs_get_directory_attribute(bus->directory, attrname);
}
/**
- * sysfs_open_bus: opens specific bus and all its devices on system
- * returns sysfs_bus structure with success or NULL with error.
+ * 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
*/
-struct sysfs_bus *sysfs_open_bus(const char *name)
+int sysfs_find_driver_bus(const char *driver, char *busname, size_t bsize)
{
- struct sysfs_bus *bus = NULL;
- struct sysfs_directory *busdir = NULL;
+ char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
+ struct dlist *buslist = NULL, *drivers = NULL;
- if (name == NULL) {
+ if (driver == NULL || busname == NULL) {
errno = EINVAL;
- return NULL;
+ return -1;
}
- bus = alloc_bus();
- if (bus == NULL) {
- perror("malloc");
- return NULL;
- }
- strcpy(bus->name, name);
- busdir = open_bus_dir(name);
- if (busdir == NULL) {
- dprintf(stderr,"Invalid bus, %s not supported on this system\n",
- name);
- sysfs_close_bus(bus);
- return NULL;
- }
- bus->directory = busdir;
- if ((get_all_bus_devices(bus)) != 0) {
- dprintf(stderr, "Error reading %s bus devices\n", name);
- sysfs_close_bus(bus);
- return NULL;
- }
- if ((get_all_bus_drivers(bus)) != 0) {
- dprintf(stderr, "Error reading %s bus drivers\n", name);
- sysfs_close_bus(bus);
- return NULL;
+ memset(subsys, 0, SYSFS_PATH_MAX);
+ safestrcpy(subsys, SYSFS_BUS_NAME);
+ buslist = sysfs_open_subsystem_list(subsys);
+ if (buslist != NULL) {
+ dlist_for_each_data(buslist, bus, char) {
+ memset(subsys, 0, SYSFS_PATH_MAX);
+ safestrcpy(subsys, SYSFS_BUS_NAME);
+ safestrcat(subsys, "/");
+ safestrcat(subsys, bus);
+ safestrcat(subsys, "/");
+ safestrcat(subsys, SYSFS_DRIVERS_NAME);
+ drivers = sysfs_open_subsystem_list(subsys);
+ if (drivers != NULL) {
+ dlist_for_each_data(drivers, curdrv, char) {
+ if (strcmp(driver, curdrv) == 0) {
+ safestrcpymax(busname,
+ bus, bsize);
+ sysfs_close_list(drivers);
+ sysfs_close_list(buslist);
+ return 0;
+ }
+ }
+ sysfs_close_list(drivers);
+ }
+ }
+ sysfs_close_list(buslist);
}
- link_bus_devices_to_drivers(bus);
-
- return bus;
+ return -1;
}