+
+/**
+ * get_driver_path: looks up the bus the driver is on and builds path to
+ * the driver.
+ * @bus: bus on which to search
+ * @drv: driver to look for
+ * @path: buffer to return path to driver
+ * @psize: size of "path"
+ * Returns 0 on success and -1 on error
+ */
+static int get_driver_path(const char *bus, const char *drv,
+ char *path, size_t psize)
+{
+ if (!bus || !drv || !path || psize == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (sysfs_get_mnt_path(path, psize)) {
+ dprintf("Error getting sysfs mount path\n");
+ return -1;
+ }
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, SYSFS_BUS_NAME, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, bus, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, SYSFS_DRIVERS_NAME, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, drv, psize);
+ return 0;
+}
+
+/**
+ * sysfs_open_driver: open driver by name, given its bus
+ * @bus_name: Name of the bus
+ * @drv_name: Name of the driver
+ * Returns the sysfs_driver reference on success and NULL on failure
+ */
+struct sysfs_driver *sysfs_open_driver(const char *bus_name,
+ const char *drv_name)
+{
+ char path[SYSFS_PATH_MAX];
+ struct sysfs_driver *driver = NULL;
+
+ if (!drv_name || !bus_name) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ memset(path, 0, SYSFS_PATH_MAX);
+ if (get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) {
+ dprintf("Error getting to driver %s\n", drv_name);
+ return NULL;
+ }
+ driver = sysfs_open_driver_path(path);
+ if (!driver) {
+ dprintf("Error opening driver at %s\n", path);
+ return NULL;
+ }
+ return driver;
+}
+
+/**
+ * sysfs_get_driver_devices: gets list of devices that use the driver
+ * @drv: sysfs_driver whose device list is needed
+ * Returns dlist of struct sysfs_device on success and NULL on failure
+ */
+struct dlist *sysfs_get_driver_devices(struct sysfs_driver *drv)
+{
+ char *ln = NULL;
+ struct dlist *linklist = NULL;
+ struct sysfs_device *dev = NULL;
+
+ if (!drv) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ linklist = read_dir_links(drv->path);
+ if (linklist) {
+ dlist_for_each_data(linklist, ln, char) {
+
+ if (!strncmp(ln, SYSFS_MODULE_NAME, strlen(ln)))
+ continue;
+
+ dev = sysfs_open_device(drv->bus, ln);
+ if (!dev) {
+ dprintf("Error opening driver's device\n");
+ sysfs_close_list(linklist);
+ return NULL;
+ }
+ if (!drv->devices) {
+ drv->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_device),
+ sysfs_close_driver_device);
+ if (!drv->devices) {
+ dprintf("Error creating device list\n");
+ sysfs_close_list(linklist);
+ return NULL;
+ }
+ }
+ dlist_unshift_sorted(drv->devices, dev, sort_list);
+ }
+ sysfs_close_list(linklist);
+ }
+ return drv->devices;
+}