+
+static struct sysfs_class_device *open_class_device(const char *path)
+{
+ struct sysfs_class_device *class_dev;
+ int loop;
+
+ loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
+ while (--loop) {
+ class_dev = sysfs_open_class_device_path(path);
+ if (class_dev)
+ break;
+
+ usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+ }
+
+ return (class_dev);
+}
+
+static int wait_for_class_device(struct sysfs_class_device *class_dev,
+ const char **error)
+{
+ struct sysfs_class_device *class_dev_parent;
+ struct sysfs_device *devices_dev = NULL;
+ int loop;
+
+ if (wait_for_class_device_attributes(class_dev, error) != 0)
+ return -ENOENT;
+
+ /* skip devices without devices-link */
+ if (class_device_expect_no_device_link(class_dev)) {
+ dbg("no device symlink expected for '%s', ", class_dev->name);
+ return -ENODEV;
+ }
+
+ /* the symlink may be on the parent device */
+ class_dev_parent = sysfs_get_classdev_parent(class_dev);
+ if (class_dev_parent)
+ dbg("looking at parent device for device link '%s'", class_dev_parent->path);
+
+ /* wait for the symlink to the devices device */
+ dbg("waiting for symlink to devices device");
+ loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
+ while (--loop) {
+ if (class_dev_parent)
+ devices_dev = sysfs_get_classdev_device(class_dev_parent);
+ else
+ devices_dev = sysfs_get_classdev_device(class_dev);
+
+ if (devices_dev)
+ break;
+
+ usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+ }
+ if (!devices_dev) {
+ dbg(" error: no devices device symlink found");
+ *error = "no device symlink";
+ return -ENODEV;
+ }
+ dbg("device symlink found pointing to '%s'", devices_dev->path);
+
+ /* wait for the bus value */
+ if (class_device_expect_no_bus(class_dev)) {
+ dbg("no bus device expected for '%s', ", class_dev->classname);
+ return 0;
+ } else {
+ return wait_for_bus_device(devices_dev, error);
+ }
+}
+
+static struct sysfs_device *open_devices_device(const char *path)
+{
+ struct sysfs_device *devices_dev;
+ int loop;
+
+ loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
+ while (--loop) {
+ devices_dev = sysfs_open_device_path(path);
+ if (devices_dev)
+ break;
+
+ usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+ }
+
+ return(devices_dev);
+}
+