chiark / gitweb /
[PATCH] 004 changelog entrys
[elogind.git] / libsysfs / sysfs_driver.c
1 /*
2  * sysfs_driver.c
3  *
4  * Driver utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 static void sysfs_close_driver_by_name_dev(void *device)
27 {
28         sysfs_close_device((struct sysfs_device *)device);
29 }
30
31 /**
32  * sysfs_close_driver: closes and cleans up driver structure
33  * NOTE: This routine does not deallocate devices list
34  * @driver: driver to close
35  */
36 void sysfs_close_driver(struct sysfs_driver *driver)
37 {
38         if (driver != NULL) {
39                 if (driver->devices != NULL) {
40                         dlist_for_each(driver->devices) 
41                                 dlist_shift(driver->devices);
42                         free(driver->devices);
43                         driver->devices = NULL;
44                 }
45                 if (driver->directory != NULL)
46                         sysfs_close_directory(driver->directory);
47                 free(driver);
48         }
49 }
50
51 /** 
52  * sysfs_close_driver_by_name: closes driver and deletes device lists too
53  * @driver: driver to close
54  */ 
55 void sysfs_close_driver_by_name(struct sysfs_driver *driver)
56 {
57         if (driver != NULL) {
58                 if (driver->devices != NULL) 
59                         dlist_destroy(driver->devices);
60                 if (driver->directory != NULL)
61                         sysfs_close_directory(driver->directory);
62                 free(driver);
63         }
64 }
65                 
66 /**
67  * alloc_driver: allocates and initializes driver
68  * returns struct sysfs_driver with success and NULL with error.
69  */
70 static struct sysfs_driver *alloc_driver(void)
71 {
72         return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
73 }
74
75 /**
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
79  */
80 struct sysfs_driver *sysfs_open_driver(const unsigned char *path)
81 {
82         struct sysfs_driver *driver = NULL;
83         struct sysfs_directory *sdir = NULL;
84
85         if (path == NULL) {
86                 errno = EINVAL;
87                 return NULL;
88         }
89         sdir = sysfs_open_directory(path);
90         if (sdir == NULL) {
91                 dprintf("Error opening directory %s\n", path);
92                 return NULL;
93         }
94         if ((sysfs_read_directory(sdir)) != 0) {
95                 dprintf("Error reading directory %s\n", path);
96                 sysfs_close_directory(sdir);
97                 return NULL;
98         }
99         driver = alloc_driver();
100         if (driver == NULL) {
101                 dprintf("Error allocating driver at %s\n", path);
102                 sysfs_close_directory(sdir);
103                 return NULL;
104         }
105         strcpy(driver->name, sdir->name);
106         driver->directory = sdir;       
107         strcpy(driver->path, sdir->path);
108         
109         return driver;
110 }
111
112 /**
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
116  *      NULL otherwise
117  */
118 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
119 {
120         if (driver == NULL || driver->directory == NULL)
121                 return NULL;
122
123         return(driver->directory->attributes);
124 }
125
126 /**
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
131  */ 
132 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
133                                         const unsigned char *name)
134 {
135         struct sysfs_attribute *cur = NULL;
136
137         if (drv == NULL || drv->directory == NULL
138             || drv->directory->attributes == NULL || name == NULL) {
139                 errno = EINVAL;
140                 return NULL;
141         }
142
143         cur = sysfs_get_directory_attribute(drv->directory,
144                                         (unsigned char *)name);
145         if (cur != NULL)
146                 return cur;
147
148         return NULL;
149 }
150
151 /**
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
155  *      NULL otherwise
156  */
157 struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
158 {
159         if (driver == NULL || driver->directory == NULL)
160                 return NULL;
161
162         return(driver->directory->links);
163 }
164
165 /**
166  * sysfs_open_driver_by_name: open a driver by name and return the bus
167  * the driver is on.
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
172  * NOTE: 
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
176  */
177 struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name,
178                                 const unsigned char *bus, size_t bsize)
179 {
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];
184
185         if (drv_name == NULL || bus == NULL) {
186                 errno = EINVAL;
187                 return NULL;
188         }
189
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");
193                 return NULL;
194         }
195         strcat(path, SYSFS_BUS_DIR);
196         strcat(path, "/");
197         strcat(path, bus);
198         strcat(path, SYSFS_DRIVERS_DIR);
199         strcat(path, "/");
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);
204                 return NULL;
205         }
206         if (driver->directory->links != NULL) {
207                 dlist_for_each_data(driver->directory->links, curlink, 
208                                                         struct sysfs_link) {
209                         device = sysfs_open_device(curlink->target);
210                         if (device == NULL) {
211                                 dprintf("Error opening device at %s\n", 
212                                                 curlink->target);
213                                 sysfs_close_driver_by_name(driver);
214                                 return NULL;
215                         }
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);
222                 }
223         }
224         return driver;
225 }
226
227 /**
228  * get_driver_path: looks up the bus the driver is on and builds path to
229  *              the driver.
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
234  */
235 static int get_driver_path(const unsigned char *drv, 
236                                 unsigned char *path, size_t psize)
237 {
238         unsigned char bus_name[SYSFS_NAME_LEN];
239
240         if (drv == NULL || path == NULL) {
241                 errno = EINVAL;
242                 return -1;
243         }
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);
248                 return -1;
249         }
250         if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
251                 dprintf("Error getting sysfs mount path\n");
252                 return -1;
253         }
254         strcat(path, SYSFS_BUS_DIR);
255         strcat(path, "/");
256         strcat(path, bus_name);
257         strcat(path, SYSFS_DRIVERS_DIR);
258         strcat(path, "/");
259         strcat(path, drv);
260         fprintf(stdout, "get_driver_path %s\n", path);
261         return 0;
262 }
263
264 /**
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
270  */ 
271 int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib,
272                                         unsigned char *value, size_t len)
273 {
274         struct sysfs_attribute *attribute = NULL;
275         unsigned char path[SYSFS_PATH_MAX];
276
277         if (drv == NULL || attrib == NULL || value == NULL) {
278                 errno = EINVAL;
279                 return -1;
280         }
281
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);
285                 return -1;
286         }
287         strcat(path, "/");
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",
292                                                         attrib, drv);
293                 return -1;
294         }
295         if (attribute->method & SYSFS_METHOD_SHOW) {
296                 if ((sysfs_read_attribute(attribute)) != 0) {
297                         dprintf("Error reading attribute %s for driver %s\n",
298                                         attrib, drv);
299                         sysfs_close_attribute(attribute);
300                         return -1;
301                 }
302         }
303         if ((sysfs_write_attribute(attribute, value, len)) < 0) {
304                 dprintf("Error setting %s to %s\n", attrib, value);
305                 sysfs_close_attribute(attribute);
306                 return -1;
307         }
308         sysfs_close_attribute(attribute);
309         return 0;
310 }
311
312 /**
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
319  */ 
320 int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib,
321                                         unsigned char *value, size_t len)
322 {
323         struct sysfs_attribute *attribute = NULL;
324         unsigned char path[SYSFS_PATH_MAX];
325
326         if (drv == NULL || attrib == NULL || value == NULL) {
327                 errno = EINVAL;
328                 return -1;
329         }
330
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);
334                 return -1;
335         }
336         strcat(path, "/");
337         strcat(path, attrib);
338         attribute = sysfs_open_attribute(path);
339         if (attribute == NULL) {
340                 dprintf("Error opening attribute %s for driver %s\n",
341                                 attrib, drv);
342                 return -1;
343         }
344         if (!(attribute->method & SYSFS_METHOD_SHOW)) {
345                 dprintf("Show method not supported for attribute %s\n",
346                                 attrib);
347                 sysfs_close_attribute(attribute);
348                 return -1;
349         }
350         if ((sysfs_read_attribute(attribute)) != 0) {
351                 dprintf("Error reading attribute %s for driver %s\n", 
352                                 attrib, drv);
353                 sysfs_close_attribute(attribute);
354                 return -1;
355         }
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);
360                 return -1;
361         }
362         strncpy(value, attribute->value, attribute->len);
363         value[(attribute->len)+1] = 0;
364         sysfs_close_attribute(attribute);
365         return 0;
366 }
367