4 * Driver utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 static void sysfs_close_driver_by_name_dev(void *device)
28 sysfs_close_device((struct sysfs_device *)device);
32 * sysfs_close_driver: closes and cleans up driver structure
33 * NOTE: This routine does not deallocate devices list
34 * @driver: driver to close
36 void sysfs_close_driver(struct sysfs_driver *driver)
39 if (driver->devices != NULL) {
40 dlist_for_each(driver->devices)
41 dlist_shift(driver->devices);
42 free(driver->devices);
43 driver->devices = NULL;
45 if (driver->directory != NULL)
46 sysfs_close_directory(driver->directory);
52 * sysfs_close_driver_by_name: closes driver and deletes device lists too
53 * @driver: driver to close
55 void sysfs_close_driver_by_name(struct sysfs_driver *driver)
58 if (driver->devices != NULL)
59 dlist_destroy(driver->devices);
60 if (driver->directory != NULL)
61 sysfs_close_directory(driver->directory);
67 * alloc_driver: allocates and initializes driver
68 * returns struct sysfs_driver with success and NULL with error.
70 static struct sysfs_driver *alloc_driver(void)
72 return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
76 * sysfs_open_driver: opens and initializes driver structure
77 * @path: path to driver directory
78 * returns struct sysfs_driver with success and NULL with error
80 struct sysfs_driver *sysfs_open_driver(const unsigned char *path)
82 struct sysfs_driver *driver = NULL;
83 struct sysfs_directory *sdir = NULL;
89 sdir = sysfs_open_directory(path);
91 dprintf("Error opening directory %s\n", path);
94 if ((sysfs_read_directory(sdir)) != 0) {
95 dprintf("Error reading directory %s\n", path);
96 sysfs_close_directory(sdir);
99 driver = alloc_driver();
100 if (driver == NULL) {
101 dprintf("Error allocating driver at %s\n", path);
102 sysfs_close_directory(sdir);
105 strcpy(driver->name, sdir->name);
106 driver->directory = sdir;
107 strcpy(driver->path, sdir->path);
113 * sysfs_get_driver_attributes: gets list of attributes for the given driver
114 * @driver: sysfs_driver for which attributes are required
115 * returns a dlist of attributes corresponding to the driver if present
118 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
120 if (driver == NULL || driver->directory == NULL)
123 return(driver->directory->attributes);
127 * sysfs_get_driver_attr: searches driver's attributes by name
128 * @drv: driver to look through
129 * @name: attribute name to get
130 * returns sysfs_attribute reference on success or NULL with error
132 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
133 const unsigned char *name)
135 struct sysfs_attribute *cur = NULL;
137 if (drv == NULL || drv->directory == NULL
138 || drv->directory->attributes == NULL || name == NULL) {
143 cur = sysfs_get_directory_attribute(drv->directory,
144 (unsigned char *)name);
152 * sysfs_get_driver_links: gets list of links from the given driver
153 * @driver: sysfs_driver for which links list is required
154 * returns a dlist of links corresponding to the driver if present
157 struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
159 if (driver == NULL || driver->directory == NULL)
162 return(driver->directory->links);
166 * get_driver_path: looks up the bus the driver is on and builds path to
168 * @bus: bus on which to search
169 * @drv: driver to look for
170 * @path: buffer to return path to driver
171 * @psize: size of "path"
172 * Returns 0 on success and -1 on error
174 static int get_driver_path(const unsigned char *bus, const unsigned char *drv,
175 unsigned char *path, size_t psize)
177 if (bus == NULL || drv == NULL || path == NULL) {
181 if (sysfs_get_mnt_path(path, psize) != 0) {
182 dprintf("Error getting sysfs mount path\n");
185 if (sysfs_trailing_slash(path) == 0)
187 strcat(path, SYSFS_BUS_NAME);
190 strcat(path, SYSFS_DRIVERS_DIR);
197 * sysfs_open_driver_by_name: open a driver by name and return the bus
199 * @drv_name: driver to open
200 * @bus: the driver bus
201 * @bsize: size of bus buffer
202 * returns struct sysfs_driver if found, NULL otherwise
204 * 1. Need to call sysfs_close_driver_by_name to free up memory
205 * 2. Bus the driver is registered with must be supplied.
206 * Use sysfs_find_driver_bus() to obtain the bus name
208 struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name,
209 const unsigned char *bus, size_t bsize)
211 struct sysfs_driver *driver = NULL;
212 struct sysfs_device *device = NULL;
213 struct sysfs_link *curlink = NULL;
214 unsigned char path[SYSFS_PATH_MAX];
216 if (drv_name == NULL || bus == NULL) {
221 memset(path, 0, SYSFS_PATH_MAX);
222 if (get_driver_path(bus, drv_name, path, SYSFS_PATH_MAX) != 0) {
223 dprintf("Error getting to driver %s\n", drv_name);
226 driver = sysfs_open_driver(path);
227 if (driver == NULL) {
228 dprintf("Could not open driver %s\n", drv_name);
231 if (driver->directory->links != NULL) {
232 dlist_for_each_data(driver->directory->links, curlink,
234 device = sysfs_open_device(curlink->target);
235 if (device == NULL) {
236 dprintf("Error opening device at %s\n",
238 sysfs_close_driver_by_name(driver);
241 strcpy(device->driver_name, drv_name);
242 if (driver->devices == NULL)
243 driver->devices = dlist_new_with_delete
244 (sizeof(struct sysfs_device),
245 sysfs_close_driver_by_name_dev);
246 dlist_unshift(driver->devices, device);
254 * sysfs_open_driver_attr: read the user supplied driver attribute
255 * @bus: bus on which to look
256 * @drv: driver whose attribute has to be read
257 * @attrib: Attribute to be read
258 * Returns struct sysfs_attribute on success and NULL on failure
261 * A call to sysfs_close_attribute() is required to close the
262 * attribute returned and to free memory
264 struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus,
265 const unsigned char *drv, const unsigned char *attrib)
267 struct sysfs_attribute *attribute = NULL;
268 unsigned char path[SYSFS_PATH_MAX];
270 if (bus == NULL || drv == NULL || attrib == NULL) {
275 memset(path, 0, SYSFS_NAME_LEN);
276 if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
277 dprintf("Error getting to driver %s\n", drv);
281 strcat(path, attrib);
282 attribute = sysfs_open_attribute(path);
283 if (attribute == NULL) {
284 dprintf("Error opening attribute %s for driver %s\n",
288 if ((sysfs_read_attribute(attribute)) != 0) {
289 dprintf("Error reading attribute %s for driver %s\n",
291 sysfs_close_attribute(attribute);