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 * sysfs_open_driver_by_name: open a driver by name and return the bus
168 * @drv_name: driver to open
169 * @bus: the driver bus
170 * @bsize: size of bus buffer
171 * returns struct sysfs_driver if found, NULL otherwise
173 * 1. Need to call sysfs_close_driver_by_name to free up memory
174 * 2. Bus the driver is registered with must be supplied.
175 * Use sysfs_find_driver_bus() to obtain the bus name
177 struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name,
178 const unsigned char *bus, size_t bsize)
180 struct sysfs_driver *driver = NULL;
181 struct sysfs_device *device = NULL;
182 struct sysfs_link *curlink = NULL;
183 unsigned char path[SYSFS_PATH_MAX];
185 if (drv_name == NULL || bus == NULL) {
190 memset(path, 0, SYSFS_PATH_MAX);
191 if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
192 dprintf("Error getting sysfs mount path\n");
195 strcat(path, SYSFS_BUS_DIR);
198 strcat(path, SYSFS_DRIVERS_DIR);
200 strcat(path, drv_name);
201 driver = sysfs_open_driver(path);
202 if (driver == NULL) {
203 dprintf("Could not open driver %s\n", drv_name);
206 if (driver->directory->links != NULL) {
207 dlist_for_each_data(driver->directory->links, curlink,
209 device = sysfs_open_device(curlink->target);
210 if (device == NULL) {
211 dprintf("Error opening device at %s\n",
213 sysfs_close_driver_by_name(driver);
216 strcpy(device->driver_name, drv_name);
217 if (driver->devices == NULL)
218 driver->devices = dlist_new_with_delete
219 (sizeof(struct sysfs_device),
220 sysfs_close_driver_by_name_dev);
221 dlist_unshift(driver->devices, device);
228 * get_driver_path: looks up the bus the driver is on and builds path to
230 * @drv: driver to look for
231 * @path: buffer to return path to driver
232 * @psize: size of "path"
233 * Returns 0 on success and -1 on error
235 static int get_driver_path(const unsigned char *drv,
236 unsigned char *path, size_t psize)
238 unsigned char bus_name[SYSFS_NAME_LEN];
240 if (drv == NULL || path == NULL) {
244 memset(bus_name, 0, SYSFS_NAME_LEN);
245 memset(path, 0, SYSFS_PATH_MAX);
246 if ((sysfs_find_driver_bus(drv, bus_name, SYSFS_NAME_LEN)) < 0) {
247 dprintf("Driver %s not found\n", drv);
250 if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
251 dprintf("Error getting sysfs mount path\n");
254 strcat(path, SYSFS_BUS_DIR);
256 strcat(path, bus_name);
257 strcat(path, SYSFS_DRIVERS_DIR);
260 fprintf(stdout, "get_driver_path %s\n", path);
265 * sysfs_write_driver_attr: modify "writable" driver attribute
266 * @drv: driver whose attribute has to be modified
267 * @attrib: Attribute to be modified
268 * @value: Value to change to
269 * Returns 0 on success -1 on failure
271 int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib,
272 unsigned char *value, size_t len)
274 struct sysfs_attribute *attribute = NULL;
275 unsigned char path[SYSFS_PATH_MAX];
277 if (drv == NULL || attrib == NULL || value == NULL) {
282 memset(path, 0, SYSFS_PATH_MAX);
283 if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) {
284 dprintf("Error getting to driver %s\n", drv);
288 strcat(path, attrib);
289 attribute = sysfs_open_attribute(path);
290 if (attribute == NULL) {
291 dprintf("Attribute %s could not be retrieved for driver %s\n",
295 if (attribute->method & SYSFS_METHOD_SHOW) {
296 if ((sysfs_read_attribute(attribute)) != 0) {
297 dprintf("Error reading attribute %s for driver %s\n",
299 sysfs_close_attribute(attribute);
303 if ((sysfs_write_attribute(attribute, value, len)) < 0) {
304 dprintf("Error setting %s to %s\n", attrib, value);
305 sysfs_close_attribute(attribute);
308 sysfs_close_attribute(attribute);
313 * sysfs_read_driver_attr: read the user supplied driver attribute
314 * @drv: driver whose attribute has to be read
315 * @attrib: Attribute to be read
316 * @value: Buffer to return the read value
317 * @len: Length of the buffer "value"
318 * Returns 0 on success -1 on failure
320 int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib,
321 unsigned char *value, size_t len)
323 struct sysfs_attribute *attribute = NULL;
324 unsigned char path[SYSFS_PATH_MAX];
326 if (drv == NULL || attrib == NULL || value == NULL) {
331 memset(path, 0, SYSFS_NAME_LEN);
332 if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) {
333 dprintf("Error getting to driver %s\n", drv);
337 strcat(path, attrib);
338 attribute = sysfs_open_attribute(path);
339 if (attribute == NULL) {
340 dprintf("Error opening attribute %s for driver %s\n",
344 if (!(attribute->method & SYSFS_METHOD_SHOW)) {
345 dprintf("Show method not supported for attribute %s\n",
347 sysfs_close_attribute(attribute);
350 if ((sysfs_read_attribute(attribute)) != 0) {
351 dprintf("Error reading attribute %s for driver %s\n",
353 sysfs_close_attribute(attribute);
356 if (attribute->len > len) {
357 dprintf("Value length %d is larger than supplied buffer %d\n",
358 attribute->len, len);
359 sysfs_close_attribute(attribute);
362 strncpy(value, attribute->value, attribute->len);
363 value[(attribute->len)+1] = 0;
364 sysfs_close_attribute(attribute);