-/**
- * sysfs_get_class_devices: gets all devices for class
- * @cls: class to get devices for
- * returns dlist of class_devices with success and NULL with error
- */
-struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
-{
- struct sysfs_class_device *dev = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (cls == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (cls->devices != NULL)
- return cls->devices;
-
- if (cls->directory == NULL) {
- cls->directory = sysfs_open_directory(cls->path);
- if (cls->directory == NULL)
- return NULL;
- }
-
- if ((sysfs_read_dir_subdirs(cls->directory)) != 0)
- return NULL;
-
- if (cls->directory->subdirs != NULL) {
- dlist_for_each_data(cls->directory->subdirs, cur,
- struct sysfs_directory) {
- dev = sysfs_open_class_device_path(cur->path);
- if (dev == NULL) {
- dprintf("Error opening device at %s\n",
- cur->path);
- continue;
- }
- if (cls->devices == NULL)
- cls->devices = dlist_new_with_delete
- (sizeof(struct sysfs_class_device),
- sysfs_close_cls_dev);
- dlist_unshift_sorted(cls->devices, dev, sort_list);
- }
- }
- return cls->devices;
-}
-
-/**
- * sysfs_open_class: opens specific class and all its devices on system
- * returns sysfs_class structure with success or NULL with error.
- */
-struct sysfs_class *sysfs_open_class(const char *name)
-{
- struct sysfs_class *cls = NULL;
- char classpath[SYSFS_PATH_MAX];
-
- if (name == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- memset(classpath, 0, SYSFS_PATH_MAX);
- if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
- dprintf("Sysfs not supported on this system\n");
- return NULL;
- }
-
- /*
- * We shall now treat "block" also as a class. Hence, check here
- * if "name" is "block" and proceed accordingly
- */
- if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
- safestrcat(classpath, "/");
- safestrcat(classpath, SYSFS_BLOCK_NAME);
- } else {
- safestrcat(classpath, "/");
- safestrcat(classpath, SYSFS_CLASS_NAME);
- safestrcat(classpath, "/");
- safestrcat(classpath, name);
- }
- if ((sysfs_path_is_dir(classpath)) != 0) {
- dprintf("Class %s not found on the system\n", name);
- return NULL;
- }
-
- cls = alloc_class();
- if (cls == NULL) {
- dprintf("calloc failed\n");
- return NULL;
- }
- safestrcpy(cls->name, name);
- safestrcpy(cls->path, classpath);
- if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
- dprintf("Invalid path to class device %s\n", cls->path);
- sysfs_close_class(cls);
- return NULL;
- }
-
- return cls;
-}
-
-/**
- * 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 *cls,
- char *name)
-{
- if (cls == NULL || name == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (cls->devices == NULL) {
- cls->devices = sysfs_get_class_devices(cls);
- if (cls->devices == NULL)
- return NULL;
- }
- return (struct sysfs_class_device *)dlist_find_custom(cls->devices,
- name, class_name_equal);
-}
-
-/**
- * sysfs_get_classdev_device: returns the sysfs_device corresponding to
- * sysfs_class_device, if present
- * @clsdev: class device whose sysfs_device is required
- * Returns sysfs_device on success, NULL on error or if device is not
- * implemented
- */
-struct sysfs_device *sysfs_get_classdev_device
- (struct sysfs_class_device *clsdev)
-{
- struct sysfs_link *devlink = NULL;
- char devpath[SYSFS_PATH_MAX];
-
- if (clsdev == NULL) {
- errno = EINVAL;
- return NULL;
- }
- safestrcpy(devpath, clsdev->path);
- safestrcat(devpath, "/device");
- if ((sysfs_path_is_link(devpath)) != 0) {
- if (clsdev->sysdevice != NULL) {
- sysfs_close_device(clsdev->sysdevice);
- clsdev->sysdevice = NULL;
- }
- return NULL;
- }
-
- if (clsdev->directory == NULL) {
- clsdev->directory = sysfs_open_directory(clsdev->path);
- if (clsdev->directory == NULL)
- return NULL;
- }
- devlink = sysfs_get_directory_link(clsdev->directory, "device");
- if (devlink == NULL) {
- if (clsdev->sysdevice != NULL) {
- dprintf("Device link no longer exists\n");
- sysfs_close_device(clsdev->sysdevice);
- clsdev->sysdevice = NULL;
- }
- return NULL;
- }
-
- if (clsdev->sysdevice != NULL) {
- if (!strncmp(devlink->target, clsdev->sysdevice->path,
- SYSFS_PATH_MAX))
- /* sysdevice hasn't changed */
- return (clsdev->sysdevice);
- else
- /* come here only if the device link for has changed */
- sysfs_close_device(clsdev->sysdevice);
- }
-
- clsdev->sysdevice = sysfs_open_device_path(devlink->target);
- if (clsdev->sysdevice == NULL)
- return NULL;
-
- 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;
- char drvpath[SYSFS_PATH_MAX];
-
- if (clsdev == NULL) {
- errno = EINVAL;
- return NULL;
- }
- safestrcpy(drvpath, clsdev->path);
- safestrcat(drvpath, "/driver");
- if ((sysfs_path_is_link(drvpath)) != 0) {
- if (clsdev->driver != NULL) {
- sysfs_close_driver(clsdev->driver);
- clsdev->driver = NULL;
- }
- return NULL;
- }
-
- 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) {
- if (clsdev->driver != NULL) {
- dprintf("Driver link no longer exists\n");
- sysfs_close_driver(clsdev->driver);
- clsdev->driver = NULL;
- }
- return NULL;
- }
- if (clsdev->driver != NULL) {
- if (!strncmp(drvlink->target, clsdev->driver->path,
- SYSFS_PATH_MAX))
- /* driver hasn't changed */
- return (clsdev->driver);
- else
- /* come here only if the device link for has changed */
- sysfs_close_driver(clsdev->driver);
- }
-
- clsdev->driver = sysfs_open_driver_path(drvlink->target);
- if (clsdev->driver == NULL)
- return NULL;
-
- return (clsdev->driver);
-}
-