+ clsdev->sysdevice = sysfs_open_device(devlink->target);
+ if (clsdev->sysdevice == NULL)
+ return NULL;
+ if (clsdev->driver != NULL)
+ strcpy(clsdev->sysdevice->driver_name, clsdev->driver->name);
+
+ return (clsdev->sysdevice);
+}
+
+/**
+ * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to
+ * sysfs_class_device, if present
+ * @clsdev: class device whose sysfs_device is required
+ * Returns sysfs_driver on success, NULL on error or if driver is not
+ * implemented
+ */
+struct sysfs_driver *sysfs_get_classdev_driver
+ (struct sysfs_class_device *clsdev)
+{
+ struct sysfs_link *drvlink = NULL;
+
+ if (clsdev == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (clsdev->driver != NULL)
+ return (clsdev->driver);
+
+ if (clsdev->directory == NULL) {
+ clsdev->directory = sysfs_open_directory(clsdev->path);
+ if (clsdev->directory == NULL)
+ return NULL;
+ }
+ drvlink = sysfs_get_directory_link(clsdev->directory, "driver");
+ if (drvlink != NULL) {
+ clsdev->driver = sysfs_open_driver(drvlink->target);
+ if (clsdev->driver == NULL)
+ return NULL;
+
+ }
+ return (clsdev->driver);
+}
+
+/*
+ * get_blockdev_parent: Get the parent class device for a "block" subsystem
+ * device if present
+ * @clsdev: block subsystem class device whose parent needs to be found
+ * Returns 0 on success and 1 on error
+ */
+static int get_blockdev_parent(struct sysfs_class_device *clsdev)
+{
+ unsigned char parent_path[SYSFS_PATH_MAX], value[256], *c = NULL;
+
+ memset(parent_path, 0, SYSFS_PATH_MAX);
+ strcpy(parent_path, clsdev->path);
+
+ c = strstr(parent_path, SYSFS_BLOCK_NAME);
+ if (c == NULL) {
+ dprintf("Class device %s does not belong to BLOCK subsystem",
+ clsdev->name);
+ return 1;
+ }
+
+ c += strlen(SYSFS_BLOCK_NAME);
+ if (*c == '/')
+ c++;
+ else
+ goto errout;
+
+ /* validate whether the given class device is a partition or not */
+ if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
+ dprintf("%s not a partition\n", clsdev->name);
+ return 1;
+ }
+ c = strchr(c, '/');
+ if (c == NULL)
+ goto errout;
+ *c = '\0';
+
+ /* Now validate if the parent has the "dev" attribute */
+ memset(value, 0, 256);
+ strcat(parent_path, "/dev");
+ if ((sysfs_read_attribute_value(parent_path, value, 256)) != 0) {
+ dprintf("Block device %s does not have a parent\n",
+ clsdev->name);
+ return 1;
+ }
+
+ c = strrchr(parent_path, '/');
+ if (c == NULL)
+ goto errout;
+
+ *c = '\0';
+ clsdev->parent = sysfs_open_class_device(parent_path);
+ if (clsdev->parent == NULL) {
+ dprintf("Error opening the parent class device at %s\n",
+ parent_path);
+ return 1;
+ }
+ return 0;
+
+errout:
+ dprintf("Invalid path %s\n", clsdev->path);
+ return 1;
+}
+
+/**
+ * sysfs_get_classdev_parent: Retrieves the parent of a class device.
+ * eg., when working with hda1, this function can be used to retrieve the
+ * sysfs_class_device for hda
+ *
+ * @clsdev: class device whose parent details are required.
+ * Returns sysfs_class_device of the parent on success, NULL on failure
+ */
+struct sysfs_class_device *sysfs_get_classdev_parent
+ (struct sysfs_class_device *clsdev)
+{
+ if (clsdev == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if (clsdev->parent != NULL)
+ return (clsdev->parent);
+
+ /*
+ * As of now, only block devices have a parent child heirarchy in sysfs
+ * We do not know, if, in the future, more classes will have a similar
+ * structure. Hence, we now call a specialized function for block and
+ * later we can add support functions for other subsystems as required.
+ */
+ if (!(strcmp(clsdev->classname, SYSFS_BLOCK_NAME))) {
+ if ((get_blockdev_parent(clsdev)) == 0)
+ return (clsdev->parent);
+ }
+ return NULL;
+}
+
+/**
+ * get_classdev_path: given the class and a device in the class, return the
+ * absolute path to the device
+ * @classname: name of the class
+ * @clsdev: the class device
+ * @path: buffer to return path
+ * @psize: size of "path"
+ * Returns 0 on SUCCESS or -1 on error
+ */
+static int get_classdev_path(const unsigned char *classname,
+ const unsigned char *clsdev, unsigned char *path, size_t len)
+{
+ if (classname == NULL || clsdev == NULL || path == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (sysfs_get_mnt_path(path, len) != 0) {
+ dprintf("Error getting sysfs mount path\n");
+ return -1;
+ }
+
+ if (sysfs_trailing_slash(path) == 0)
+ strcat(path, "/");
+
+ if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
+ strcat(path, SYSFS_BLOCK_NAME);
+ } else {
+ strcat(path, SYSFS_CLASS_NAME);
+ strcat(path, "/");
+ strcat(path, classname);
+ }
+ strcat(path, "/");
+ strcat(path, clsdev);
+ return 0;
+}
+
+/**
+ * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
+ * Class_device must be closed using sysfs_close_class_device
+ * @classname: Class to search
+ * @name: name of the class_device
+ *
+ * NOTE:
+ * Call sysfs_close_class_device() to close the class device
+ */
+struct sysfs_class_device *sysfs_open_class_device_by_name
+ (const unsigned char *classname, const unsigned char *name)
+{
+ unsigned char devpath[SYSFS_PATH_MAX];
+ struct sysfs_class_device *cdev = NULL;
+
+ if (classname == NULL || name == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ if ((get_classdev_path(classname, name, devpath,
+ SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting to device %s on class %s\n",
+ name, classname);
+ return NULL;
+ }
+
+ cdev = sysfs_open_class_device(devpath);
+ if (cdev == NULL) {
+ dprintf("Error getting class device %s from class %s\n",
+ name, classname);
+ return NULL;
+ }
+ return cdev;
+}
+
+/**
+ * sysfs_get_classdev_attributes: returns a dlist of attributes for
+ * the requested class_device
+ * @cdev: sysfs_class_dev for which attributes are needed
+ * returns a dlist of attributes if exists, NULL otherwise
+ */
+struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
+{
+ if (cdev == NULL)
+ return NULL;
+
+ if (cdev->directory == NULL) {
+ cdev->directory = sysfs_open_directory(cdev->path);
+ if (cdev->directory == NULL)
+ return NULL;
+ }
+ if (cdev->directory->attributes == NULL) {
+ if ((sysfs_read_dir_attributes(cdev->directory)) != 0) {
+ dprintf("Error reading attributes for directory %s\n",
+ cdev->directory->path);
+ return NULL;
+ }
+ } else {
+ if ((sysfs_path_is_dir(cdev->path)) != 0) {
+ dprintf("Class device at %s no longer exists\n",
+ cdev->path);
+ return NULL;
+ }
+ if ((sysfs_refresh_attributes
+ (cdev->directory->attributes)) != 0) {
+ dprintf("Error refreshing classdev attributes\n");
+ return NULL;
+ }
+ }
+ return (cdev->directory->attributes);
+}
+
+/**
+ * sysfs_get_classdev_attr: searches class device's attributes by name
+ * @clsdev: class device to look through
+ * @name: attribute name to get
+ * returns sysfs_attribute reference with success or NULL with error
+ */
+struct sysfs_attribute *sysfs_get_classdev_attr
+ (struct sysfs_class_device *clsdev, const unsigned char *name)
+{
+ struct sysfs_attribute *cur = NULL;
+ struct sysfs_directory *sdir = NULL;
+ struct dlist *attrlist = NULL;
+
+ if (clsdev == NULL || name == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ /*
+ * First, see if it's in the current directory. Then look at
+ * subdirs since class devices can have subdirs of attributes.
+ */
+ attrlist = sysfs_get_classdev_attributes(clsdev);
+ if (attrlist == NULL)
+ return NULL;
+ cur = sysfs_get_directory_attribute(clsdev->directory,
+ (unsigned char *)name);
+ if (cur != NULL)
+ return cur;
+
+ if (clsdev->directory->subdirs == NULL)
+ if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
+ clsdev->directory->subdirs == NULL)
+ return NULL;
+
+ dlist_for_each_data(clsdev->directory->subdirs, sdir,
+ struct sysfs_directory) {
+ cur = sysfs_get_directory_attribute(sdir,
+ (unsigned char *)name);
+ if (cur != NULL)
+ return cur;
+ }
+
+ return NULL;
+}
+
+/**
+ * sysfs_open_classdev_attr: read an attribute for a given class device
+ * @classname: name of the class on which to look
+ * @dev: class device name for which the attribute has to be read
+ * @attrib: attribute to read
+ * Returns sysfs_attribute * on SUCCESS and NULL on error
+ *
+ * NOTE:
+ * A call to sysfs_close_attribute() is required to close the
+ * attribute returned and to free memory
+ */
+struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
+ const unsigned char *dev, const unsigned char *attrib)
+{
+ struct sysfs_attribute *attribute = NULL;
+ unsigned char path[SYSFS_PATH_MAX];
+
+ if (classname == NULL || dev == NULL || attrib == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ memset(path, 0, SYSFS_PATH_MAX);
+ if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting to device %s on class %s\n",
+ dev, classname);
+ return NULL;
+ }
+ strcat(path, "/");
+ strcat(path, attrib);
+ attribute = sysfs_open_attribute(path);
+ if (attribute == NULL) {
+ dprintf("Error opening attribute %s on class device %s\n",
+ attrib, dev);
+ return NULL;
+ }
+ if ((sysfs_read_attribute(attribute)) != 0) {
+ dprintf("Error reading attribute %s for class device %s\n",
+ attrib, dev);
+ sysfs_close_attribute(attribute);
+ return NULL;
+ }
+ return attribute;