diff options
Diffstat (limited to 'lib/sysfs_driver.c')
-rw-r--r-- | lib/sysfs_driver.c | 195 |
1 files changed, 102 insertions, 93 deletions
diff --git a/lib/sysfs_driver.c b/lib/sysfs_driver.c index 15eb239..2842c0f 100644 --- a/lib/sysfs_driver.c +++ b/lib/sysfs_driver.c @@ -28,26 +28,6 @@ static void sysfs_close_driver_device(void *device) sysfs_close_device((struct sysfs_device *)device); } -/** - * sysfs_close_bus_driver: closes and cleans up driver structure - * NOTE: This routine does not deallocate devices list - * @driver: driver to close - */ -void sysfs_close_bus_driver(struct sysfs_driver *driver) -{ - if (driver != NULL) { - if (driver->devices != NULL) { - dlist_for_each(driver->devices) - dlist_shift(driver->devices); - free(driver->devices); - driver->devices = NULL; - } - if (driver->directory != NULL) - sysfs_close_directory(driver->directory); - free(driver); - } -} - /** * sysfs_close_driver: closes driver and deletes device lists too * @driver: driver to close @@ -64,6 +44,51 @@ void sysfs_close_driver(struct sysfs_driver *driver) } /** + * open_driver_dir: Open the sysfs_directory for this driver + * @driver: Driver whose directory to be opened + * Returns 0 on success and 1 on failure + */ +static int open_driver_dir(struct sysfs_driver *driver) +{ + if (driver == NULL) { + errno = EINVAL; + return 1; + } + if (driver->directory == NULL) { + driver->directory = sysfs_open_directory(driver->path); + if (driver->directory == NULL) { + dprintf("Error opening driver directory at %s\n", + driver->path); + return 1; + } + } + return 0; +} + +/** + * read_driver_dir: Read driver directory's subdirs and links + * @driver: Driver to read + * Returns 0 on success and 1 on failure + */ +static int read_driver_dir(struct sysfs_driver *driver) +{ + if (driver == NULL) { + errno = EINVAL; + return 1; + } + if (driver->directory == NULL) { + if ((open_driver_dir(driver)) == 1) + return 1; + } + if ((sysfs_read_directory(driver->directory)) != 0) { + dprintf("Error reading driver directory at %s\n", + driver->path); + return 1; + } + return 0; +} + +/** * alloc_driver: allocates and initializes driver * returns struct sysfs_driver with success and NULL with error. */ @@ -73,11 +98,11 @@ static struct sysfs_driver *alloc_driver(void) } /** - * sysfs_open_bus_driver: opens and initializes driver structure + * sysfs_open_driver: opens and initializes driver structure * @path: path to driver directory * returns struct sysfs_driver with success and NULL with error */ -struct sysfs_driver *sysfs_open_bus_driver(const unsigned char *path) +struct sysfs_driver *sysfs_open_driver(const unsigned char *path) { struct sysfs_driver *driver = NULL; struct sysfs_directory *sdir = NULL; @@ -86,25 +111,22 @@ struct sysfs_driver *sysfs_open_bus_driver(const unsigned char *path) errno = EINVAL; return NULL; } - sdir = sysfs_open_directory(path); - if (sdir == NULL) { - dprintf("Error opening directory %s\n", path); - return NULL; - } - if ((sysfs_read_directory(sdir)) != 0) { - dprintf("Error reading directory %s\n", path); - sysfs_close_directory(sdir); + if ((sysfs_path_is_dir(path)) != 0) { + dprintf("Invalid path to driver: %s\n", path); return NULL; } driver = alloc_driver(); if (driver == NULL) { dprintf("Error allocating driver at %s\n", path); - sysfs_close_directory(sdir); return NULL; } - strcpy(driver->name, sdir->name); - driver->directory = sdir; - strcpy(driver->path, sdir->path); + if ((sysfs_get_name_from_path(path, driver->name, + SYSFS_NAME_LEN)) != 0) { + dprintf("Error getting driver name from path\n"); + free(driver); + return NULL; + } + strcpy(driver->path, path); return driver; } @@ -117,12 +139,20 @@ struct sysfs_driver *sysfs_open_bus_driver(const unsigned char *path) */ struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver) { - if (driver == NULL || driver->directory == NULL) + if (driver == NULL) { + errno = EINVAL; return NULL; + } + if (driver->directory == NULL) { + if ((open_driver_dir(driver)) == 1) + return NULL; + } if (driver->directory->attributes == NULL) { - if ((sysfs_read_dir_attributes(driver->directory)) != 0) + if ((sysfs_read_dir_attributes(driver->directory)) != 0) { + dprintf("Error reading driver attributes\n"); return NULL; + } } return(driver->directory->attributes); } @@ -137,21 +167,20 @@ struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv, const unsigned char *name) { struct sysfs_attribute *cur = NULL; + struct dlist *attrlist = NULL; - if (drv == NULL || drv->directory == NULL || name == NULL) { + if (drv == NULL) { errno = EINVAL; return NULL; } - - if (drv->directory->attributes == NULL) { - if ((sysfs_read_dir_attributes(drv->directory)) != 0) - return NULL; + + attrlist = sysfs_get_driver_attributes(drv); + if (attrlist != NULL) { + cur = sysfs_get_directory_attribute(drv->directory, + (unsigned char *)name); + if (cur != NULL) + return cur; } - cur = sysfs_get_directory_attribute(drv->directory, - (unsigned char *)name); - if (cur != NULL) - return cur; - return NULL; } @@ -163,31 +192,43 @@ struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv, */ struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver) { - if (driver == NULL || driver->directory == NULL) + if (driver == NULL) { + errno = EINVAL; return NULL; - + } + if (driver->directory == NULL) { + if ((open_driver_dir(driver)) == 1) + return NULL; + if ((read_driver_dir(driver)) != 0) + return NULL; + } return(driver->directory->links); } - /** - * sysfs_open_driver_devices: open up the list of devices this driver supports + * sysfs_get_driver_devices: open up the list of devices this driver supports * @driver: sysfs_driver for which devices are needed - * Returns 0 on SUCCESS or -1 with ERROR + * Returns dlist of devices on SUCCESS or NULL with ERROR */ -static int sysfs_open_driver_devices(struct sysfs_driver *driver) +struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver) { struct sysfs_link *curlink = NULL; struct sysfs_device *device = NULL; if (driver == NULL) { errno = EINVAL; - return -1; + return NULL; } if (driver->devices != NULL) - return 0; - + return (driver->devices); + + if (driver->directory == NULL) { + if ((open_driver_dir(driver)) == 1) + return NULL; + if ((read_driver_dir(driver)) != 0) + return NULL; + } if (driver->directory->links != NULL) { dlist_for_each_data(driver->directory->links, curlink, struct sysfs_link) { @@ -195,7 +236,7 @@ static int sysfs_open_driver_devices(struct sysfs_driver *driver) if (device == NULL) { dprintf("Error opening device at %s\n", curlink->target); - return -1; + return NULL; } strcpy(device->driver_name, driver->name); if (driver->devices == NULL) @@ -205,37 +246,7 @@ static int sysfs_open_driver_devices(struct sysfs_driver *driver) dlist_unshift(driver->devices, device); } } - return 0; -} -/** - * sysfs_open_driver: open a driver given its path - * @path: path to driver to open - * returns struct sysfs_driver if found, NULL otherwise - * NOTE: - * 1. Need to call sysfs_close_driver to free up memory - */ -struct sysfs_driver *sysfs_open_driver(const unsigned char *path) -{ - struct sysfs_driver *driver = NULL; - - if (path == NULL) { - errno = EINVAL; - return NULL; - } - - driver = sysfs_open_bus_driver(path); - if (driver == NULL) { - dprintf("Could not open driver %s\n", drv_name); - return NULL; - } - if (driver->directory->links != NULL) { - if ((sysfs_open_driver_devices(driver)) != 0) { - dprintf("Error reading driver devices\n"); - sysfs_close_driver(driver); - return NULL; - } - } - return driver; + return (driver->devices); } /** @@ -249,6 +260,7 @@ struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver, const unsigned char *name) { struct sysfs_device *device = NULL; + struct dlist *devlist = NULL; if (driver == NULL || name == NULL) { errno = EINVAL; @@ -256,13 +268,10 @@ struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver, } if (driver->devices == NULL) { - if (driver->directory->links == NULL) { + devlist = sysfs_get_driver_devices(driver); + if (devlist == NULL) { + dprintf("Error getting driver devices\n"); return NULL; - } else { - if ((sysfs_open_driver_devices(driver)) != 0) { - dprintf("Error getting driver devices\n"); - return NULL; - } } } dlist_for_each_data(driver->devices, device, struct sysfs_device) { |