+
+/**
+ * get_device_absolute_path: looks up the bus the device is on, gets
+ * absolute path to the device
+ * @device: device for which path is needed
+ * @path: buffer to store absolute path
+ * @psize: size of "path"
+ * Returns 0 on success -1 on failure
+ */
+static int get_device_absolute_path(const unsigned char *device,
+ const unsigned char *bus, unsigned char *path, size_t psize)
+{
+ unsigned char bus_path[SYSFS_PATH_MAX];
+
+ if (device == NULL || path == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ memset(bus_path, 0, SYSFS_PATH_MAX);
+ if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
+ dprintf ("Sysfs not supported on this system\n");
+ return -1;
+ }
+ if (sysfs_trailing_slash(bus_path) == 0)
+ strcat(bus_path, "/");
+ strcat(bus_path, SYSFS_BUS_NAME);
+ strcat(bus_path, "/");
+ strcat(bus_path, bus);
+ strcat(bus_path, "/");
+ strcat(bus_path, SYSFS_DEVICES_NAME);
+ strcat(bus_path, "/");
+ strcat(bus_path, device);
+ /*
+ * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
+ * Now read this link to reach to the device.
+ */
+ if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting to device %s\n", device);
+ return -1;
+ }
+ return 0;
+}
+
+/**
+ * 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
+ * returns struct sysfs_device if found, NULL otherwise
+ * NOTE:
+ * 1. Use sysfs_close_device to close the device
+ * 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(const unsigned char *bus_id,
+ const unsigned char *bus)
+{
+ char sysfs_path[SYSFS_PATH_MAX];
+ struct sysfs_device *device = NULL;
+
+ if (bus_id == NULL || bus == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ memset(sysfs_path, 0, SYSFS_PATH_MAX);
+ if ((get_device_absolute_path(bus_id, bus, sysfs_path,
+ SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting to device %s\n", bus_id);
+ return NULL;
+ }
+
+ 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
+ * @dev_id: device for which attribute is required
+ * @attrname: name of the attribute to look for
+ * Returns struct sysfs_attribute on success and NULL on failure
+ *
+ * NOTE:
+ * A call to sysfs_close_attribute() is required to close
+ * the attribute returned and free memory.
+ */
+struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
+ const unsigned char *bus_id, const unsigned char *attrib)
+{
+ struct sysfs_attribute *attribute = NULL;
+ unsigned char devpath[SYSFS_PATH_MAX];
+
+ if (bus == NULL || bus_id == NULL || attrib == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ if ((get_device_absolute_path(bus_id, bus, devpath,
+ SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting to device %s\n", bus_id);
+ return NULL;
+ }
+ strcat(devpath, "/");
+ strcat(devpath, attrib);
+ attribute = sysfs_open_attribute(devpath);
+ if (attribute == NULL) {
+ dprintf("Error opening attribute %s for device %s\n",
+ attrib, bus_id);
+ return NULL;
+ }
+ if ((sysfs_read_attribute(attribute)) != 0) {
+ dprintf("Error reading attribute %s for device %s\n",
+ attrib, bus_id);
+ sysfs_close_attribute(attribute);
+ return NULL;
+ }
+ return attribute;
+}
+