* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
-#include "libsysfs.h"
+#include "sysfs/libsysfs.h"
#include "sysfs.h"
/**
- * get_device_bus: retrieves the bus name the device is on, checks path to
- * bus' link to make sure it has correct device.
+ * sysfs_get_device_bus: retrieves the bus name the device is on, checks path
+ * to bus' link to make sure it has correct device.
* @dev: device to get busname.
* returns 0 with success and -1 with error.
*/
-static int get_device_bus(struct sysfs_device *dev)
+int sysfs_get_device_bus(struct sysfs_device *dev)
{
unsigned char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
unsigned char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
}
}
-/**
- * sysfs_del_device: routine for dlist integration
- */
-static void sysfs_del_device(void *dev)
-{
- sysfs_close_device((struct sysfs_device *)dev);
-}
-
/**
* sysfs_close_dev_tree: routine for dlist integration
*/
void sysfs_close_device(struct sysfs_device *dev)
{
if (dev != NULL) {
+ if (dev->parent != NULL)
+ sysfs_close_device(dev->parent);
if (dev->directory != NULL)
sysfs_close_directory(dev->directory);
if (dev->children != NULL && dev->children->count == 0)
dprintf ("Device %s not supported on this system\n", path);
return NULL;
}
- if ((sysfs_read_directory(rdir)) != 0) {
+ if ((sysfs_read_dir_subdirs(rdir)) != 0) {
dprintf ("Error reading device at dir %s\n", path);
sysfs_close_directory(rdir);
return NULL;
}
/**
- * sysfs_open_device: opens and populates device structure
+ * sysfs_open_device_path: opens and populates device structure
* @path: path to device, this is the /sys/devices/ path
* returns sysfs_device structure with success or NULL with error
*/
-struct sysfs_device *sysfs_open_device(const unsigned char *path)
+struct sysfs_device *sysfs_open_device_path(const unsigned char *path)
{
struct sysfs_device *dev = NULL;
return NULL;
}
strcpy(dev->path, path);
+ if ((sysfs_remove_trailing_slash(dev->path)) != 0) {
+ dprintf("Invalid path to device %s\n", dev->path);
+ sysfs_close_device(dev);
+ return NULL;
+ }
/*
* The "name" attribute no longer exists... return the device's
* sysfs representation instead, in the "dev->name" field, which
*/
strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN);
- if (get_device_bus(dev) != 0)
- strcpy(dev->bus, SYSFS_UNKNOWN);
+ if (sysfs_get_device_bus(dev) != 0)
+ dprintf("Could not get device bus\n");
return dev;
}
errno = EINVAL;
return NULL;
}
- rootdev = sysfs_open_device(path);
+ rootdev = sysfs_open_device_path(path);
if (rootdev == NULL) {
dprintf("Error opening root device at %s\n", path);
return NULL;
if (rootdev->children == NULL)
rootdev->children = dlist_new_with_delete
(sizeof(struct sysfs_device),
- sysfs_del_device);
+ sysfs_close_dev_tree);
dlist_unshift(rootdev->children, new);
}
}
return NULL;
}
- if (sysfs_trailing_slash(rootpath) == 0)
- strcat(rootpath, "/");
-
+ strcat(rootpath, "/");
strcat(rootpath, SYSFS_DEVICES_NAME);
strcat(rootpath, "/");
strcat(rootpath, name);
dprintf("calloc failure\n");
return NULL;
}
+ strcpy(root->name, name);
strcpy(root->path, rootpath);
+ if ((sysfs_remove_trailing_slash(root->path)) != 0) {
+ dprintf("Invalid path to root device %s\n", root->path);
+ sysfs_close_root_device(root);
+ return NULL;
+ }
return root;
}
if (device->directory->attributes == NULL) {
if ((sysfs_read_dir_attributes(device->directory)) != 0)
return NULL;
- } else {
- if ((sysfs_path_is_dir(device->path)) != 0) {
- dprintf("Device at %s no longer exists", device->path);
- return NULL;
- }
- if ((sysfs_refresh_attributes
- (device->directory->attributes)) != 0) {
- dprintf("Error refreshing device attributes\n");
- return NULL;
- }
}
return (device->directory->attributes);
}
+/**
+ * sysfs_refresh_device_attributes: refreshes the device's list of attributes
+ * @device: sysfs_device whose attributes to refresh
+ *
+ * NOTE: Upon return, prior references to sysfs_attributes for this device
+ * _may_ not be valid
+ *
+ * Returns list of attributes on success and NULL on failure
+ */
+struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device)
+{
+ if (device == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (device->directory == NULL)
+ return (sysfs_get_device_attributes(device));
+
+ if ((sysfs_refresh_dir_attributes(device->directory)) != 0) {
+ dprintf("Error refreshing device attributes\n");
+ return NULL;
+ }
+
+ return (device->directory->attributes);
+}
+
/**
* sysfs_get_device_attr: searches dev's attributes by name
* @dev: device to look through
struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
const unsigned char *name)
{
- struct sysfs_attribute *cur = NULL;
struct dlist *attrlist = NULL;
if (dev == NULL || name == NULL) {
errno = EINVAL;
return NULL;
}
-
+
attrlist = sysfs_get_device_attributes(dev);
if (attrlist == NULL)
return NULL;
- cur = sysfs_get_directory_attribute(dev->directory,
- (unsigned char *)name);
- if (cur != NULL)
- return cur;
-
- return NULL;
+ return sysfs_get_directory_attribute(dev->directory,
+ (unsigned char *)name);
}
/**
dprintf ("Sysfs not supported on this system\n");
return -1;
}
-
- if (sysfs_trailing_slash(bus_path) == 0)
- strcat(bus_path, "/");
-
+ strcat(bus_path, "/");
strcat(bus_path, SYSFS_BUS_NAME);
strcat(bus_path, "/");
strcat(bus_path, bus);
}
/**
- * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
+ * sysfs_open_device: open a device by id (use the "bus" subsystem)
* @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
* 2. Bus the device is on must be supplied
* Use sysfs_find_device_bus to get the bus name
*/
-struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
+struct sysfs_device *sysfs_open_device(const unsigned char *bus_id,
const unsigned char *bus)
{
char sysfs_path[SYSFS_PATH_MAX];
return NULL;
}
- device = sysfs_open_device(sysfs_path);
+ device = sysfs_open_device_path(sysfs_path);
if (device == NULL) {
dprintf("Error opening device %s\n", bus_id);
return NULL;
return device;
}
+/**
+ * sysfs_get_device_parent: opens up given device's parent and returns a
+ * reference to its sysfs_device
+ * @dev: sysfs_device whose parent is requested
+ * Returns sysfs_device of the parent on success and NULL on failure
+ */
+struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
+{
+ unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL;
+
+ if (dev == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (dev->parent != NULL)
+ return (dev->parent);
+
+ memset(ppath, 0, SYSFS_PATH_MAX);
+ strcpy(ppath, dev->path);
+ tmp = strrchr(ppath, '/');
+ if (tmp == NULL) {
+ dprintf("Invalid path to device %s\n", ppath);
+ return NULL;
+ }
+ if (*(tmp +1) == '\0') {
+ *tmp = '\0';
+ tmp = strrchr(tmp, '/');
+ if (tmp == NULL) {
+ dprintf("Invalid path to device %s\n", ppath);
+ return NULL;
+ }
+ }
+ *tmp = '\0';
+
+ /*
+ * All "devices" have the "detach_state" attribute - validate here
+ */
+ strcat(ppath, "/detach_state");
+ if ((sysfs_path_is_file(ppath)) != 0) {
+ dprintf("Device at %s does not have a parent\n", dev->path);
+ return NULL;
+ }
+ tmp = strrchr(ppath, '/');
+ *tmp = '\0';
+ dev->parent = sysfs_open_device_path(ppath);
+ if (dev->parent == NULL) {
+ dprintf("Error opening device %s's parent at %s\n",
+ dev->bus_id, ppath);
+ return NULL;
+ }
+ return (dev->parent);
+}
+
/*
* sysfs_open_device_attr: open the given device's attribute
* @bus: Bus on which to look