+
+/**
+ * sysfs_get_class_device: Get specific class device using the device's id
+ * @class: class to find device on
+ * @name: class name of the device
+ */
+struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
+ unsigned char *name)
+{
+ if (class == NULL || name == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ return (struct sysfs_class_device *)dlist_find_custom(class->devices,
+ name, class_name_equal);
+}
+
+/**
+ * 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 (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
+ strcat(path, SYSFS_BLOCK_DIR);
+ } else {
+ strcat(path, SYSFS_CLASS_DIR);
+ 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 || cdev->directory == NULL)
+ 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;
+
+ if (clsdev == NULL || clsdev->directory == NULL ||
+ clsdev->directory->attributes == NULL || name == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ cur = sysfs_get_directory_attribute(clsdev->directory,
+ (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;
+}