chiark / gitweb /
[PATCH] pre-libsysfs-0.4.0 patch
[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_device(void *device)
27 {
28         sysfs_close_device((struct sysfs_device *)device);
29 }
30
31 /** 
32  * sysfs_close_driver: closes driver and deletes device lists too
33  * @driver: driver to close
34  */ 
35 void sysfs_close_driver(struct sysfs_driver *driver)
36 {
37         if (driver != NULL) {
38                 if (driver->devices != NULL) 
39                         dlist_destroy(driver->devices);
40                 if (driver->directory != NULL)
41                         sysfs_close_directory(driver->directory);
42                 free(driver);
43         }
44 }
45                 
46 /**
47  * open_driver_dir: Open the sysfs_directory for this driver
48  * @driver: Driver whose directory to be opened
49  * Returns 0 on success and 1 on failure
50  */ 
51 static int open_driver_dir(struct sysfs_driver *driver)
52 {
53         if (driver == NULL) {
54                 errno = EINVAL;
55                 return 1;
56         }
57         if (driver->directory == NULL) {
58                 driver->directory = sysfs_open_directory(driver->path);
59                 if (driver->directory == NULL) {
60                         dprintf("Error opening driver directory at %s\n", 
61                                         driver->path);
62                         return 1;
63                 }
64         }
65         return 0;
66 }
67
68 /**
69  * read_driver_dir: Read driver directory's subdirs and links
70  * @driver: Driver to read
71  * Returns 0 on success and 1 on failure
72  */
73 static int read_driver_dir(struct sysfs_driver *driver)
74 {
75         if (driver == NULL) {
76                 errno = EINVAL;
77                 return 1;
78         }
79         if (driver->directory == NULL) {
80                 if ((open_driver_dir(driver)) == 1)
81                         return 1;
82         }
83         if ((sysfs_read_directory(driver->directory)) != 0) {
84                 dprintf("Error reading driver directory at %s\n", 
85                                 driver->path);
86                 return 1;
87         }
88         return 0;
89 }
90
91 /**
92  * alloc_driver: allocates and initializes driver
93  * returns struct sysfs_driver with success and NULL with error.
94  */
95 static struct sysfs_driver *alloc_driver(void)
96 {
97         return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
98 }
99
100 /**
101  * sysfs_open_driver: opens and initializes driver structure
102  * @path: path to driver directory
103  * returns struct sysfs_driver with success and NULL with error
104  */
105 struct sysfs_driver *sysfs_open_driver(const unsigned char *path)
106 {
107         struct sysfs_driver *driver = NULL;
108
109         if (path == NULL) {
110                 errno = EINVAL;
111                 return NULL;
112         }
113         if ((sysfs_path_is_dir(path)) != 0) {
114                 dprintf("Invalid path to driver: %s\n", path);
115                 return NULL;
116         }
117         driver = alloc_driver();
118         if (driver == NULL) {
119                 dprintf("Error allocating driver at %s\n", path);
120                 return NULL;
121         }
122         if ((sysfs_get_name_from_path(path, driver->name, 
123                                         SYSFS_NAME_LEN)) != 0) {
124                 dprintf("Error getting driver name from path\n");
125                 free(driver);
126                 return NULL;
127         }
128         strcpy(driver->path, path);
129         
130         return driver;
131 }
132
133 /**
134  * sysfs_get_driver_attributes: gets list of attributes for the given driver
135  * @driver: sysfs_driver for which attributes are required
136  * returns a dlist of attributes corresponding to the driver if present
137  *      NULL otherwise
138  */
139 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
140 {
141         if (driver == NULL) {
142                 errno = EINVAL;
143                 return NULL;
144         }
145
146         if (driver->directory == NULL) {
147                 if ((open_driver_dir(driver)) == 1) 
148                         return NULL;
149         }
150         if (driver->directory->attributes == NULL) {
151                 if ((sysfs_read_dir_attributes(driver->directory)) != 0) {
152                         dprintf("Error reading driver attributes\n");
153                         return NULL;
154                 }
155         } else {
156                 if ((sysfs_path_is_dir(driver->path)) != 0) {
157                         dprintf("Driver at %s no longer exists\n", 
158                                                         driver->path);
159                         return NULL;
160                 }
161                 if ((sysfs_refresh_attributes
162                                 (driver->directory->attributes)) != 0) {
163                         dprintf("Error refreshing driver attributes\n");
164                         return NULL;
165                 }
166         }
167         return(driver->directory->attributes);
168 }
169
170 /**
171  * sysfs_get_driver_attr: searches driver's attributes by name
172  * @drv: driver to look through
173  * @name: attribute name to get
174  * returns sysfs_attribute reference on success or NULL with error
175  */ 
176 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
177                                         const unsigned char *name)
178 {
179         struct sysfs_attribute *cur = NULL;
180         struct dlist *attrlist = NULL;
181
182         if (drv == NULL) {
183                 errno = EINVAL;
184                 return NULL;
185         }
186         
187         attrlist = sysfs_get_driver_attributes(drv);
188         if (attrlist != NULL) {
189                 cur = sysfs_get_directory_attribute(drv->directory,
190                                                 (unsigned char *)name);
191                 if (cur != NULL)
192                         return cur;
193         }
194         return NULL;
195 }
196
197 /**
198  * sysfs_get_driver_links: gets list of links from the given driver
199  * @driver: sysfs_driver for which links list is required
200  * returns a dlist of links corresponding to the driver if present
201  *      NULL otherwise
202  */
203 struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
204 {
205         if (driver == NULL) {
206                 errno = EINVAL;
207                 return NULL;
208         }
209         if (driver->directory == NULL) {
210                 if ((open_driver_dir(driver)) == 1)
211                         return NULL;
212                 if ((read_driver_dir(driver)) != 0) 
213                         return NULL;
214         }
215         return(driver->directory->links);
216 }
217
218 /**
219  * sysfs_get_driver_devices: open up the list of devices this driver supports
220  * @driver: sysfs_driver for which devices are needed
221  * Returns dlist of devices on SUCCESS or NULL with ERROR
222  */ 
223 struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver)
224 {
225         struct sysfs_link *curlink = NULL;
226         struct sysfs_device *device = NULL;
227
228         if (driver == NULL) {
229                 errno = EINVAL;
230                 return NULL;
231         }
232         
233         if (driver->devices != NULL)
234                 return (driver->devices);
235
236         if (driver->directory == NULL) {
237                 if ((open_driver_dir(driver)) == 1) 
238                         return NULL;
239                 if ((read_driver_dir(driver)) != 0) 
240                         return NULL;
241         }
242         if (driver->directory->links != NULL) {
243                 dlist_for_each_data(driver->directory->links, curlink, 
244                                                 struct sysfs_link) {
245                         device = sysfs_open_device(curlink->target);
246                         if (device == NULL) {
247                                 dprintf("Error opening device at %s\n", 
248                                                 curlink->target);
249                                 return NULL;
250                         }
251                         strcpy(device->driver_name, driver->name);
252                         if (driver->devices == NULL) 
253                                 driver->devices = dlist_new_with_delete
254                                                 (sizeof(struct sysfs_device),
255                                                  sysfs_close_driver_device);
256                         dlist_unshift(driver->devices, device);
257                 }
258         }
259         return (driver->devices);
260 }
261
262 /**
263  * sysfs_get_driver_device: looks up a device from a list of driver's devices
264  *      and returns its sysfs_device corresponding to it
265  * @driver: sysfs_driver on which to search
266  * @name: name of the device to search
267  * Returns a sysfs_device if found, NULL otherwise
268  */
269 struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver,
270                                 const unsigned char *name)
271 {
272         struct sysfs_device *device = NULL;
273         struct dlist *devlist = NULL;
274
275         if (driver == NULL || name == NULL) {
276                 errno = EINVAL;
277                 return NULL;
278         }
279
280         if (driver->devices == NULL) {
281                 devlist = sysfs_get_driver_devices(driver);
282                 if (devlist == NULL) {
283                         dprintf("Error getting driver devices\n");
284                         return NULL;
285                 }
286         }
287         dlist_for_each_data(driver->devices, device, struct sysfs_device) {
288                 if (!(strncmp(device->name, name, SYSFS_NAME_LEN)))
289                         return device;
290         }
291         return NULL;
292 }
293
294 /**
295  * get_driver_path: looks up the bus the driver is on and builds path to
296  *              the driver.
297  * @bus: bus on which to search
298  * @drv: driver to look for
299  * @path: buffer to return path to driver
300  * @psize: size of "path"
301  * Returns 0 on success and -1 on error
302  */
303 static int get_driver_path(const unsigned char *bus, 
304                 const unsigned char *drv, unsigned char *path, size_t psize)
305 {
306         if (bus == NULL || drv == NULL || path == NULL) {
307                 errno = EINVAL;
308                 return -1;
309         }
310         if (sysfs_get_mnt_path(path, psize) != 0) {
311                 dprintf("Error getting sysfs mount path\n");
312                 return -1;
313         }
314         if (sysfs_trailing_slash(path) == 0)
315                 strcat(path, "/");
316         strcat(path, SYSFS_BUS_NAME);
317         strcat(path, "/");
318         strcat(path, bus);
319         strcat(path, "/");
320         strcat(path, SYSFS_DRIVERS_NAME);
321         strcat(path, "/");
322         strcat(path, drv);
323         return 0;
324 }
325
326 /**
327  * sysfs_open_driver_attr: read the user supplied driver attribute
328  * @bus: bus on which to look 
329  * @drv: driver whose attribute has to be read
330  * @attrib: Attribute to be read
331  * Returns struct sysfs_attribute on success and NULL on failure
332  *
333  * NOTE:
334  *      A call to sysfs_close_attribute() is required to close the
335  *      attribute returned and to free memory
336  */ 
337 struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, 
338                 const unsigned char *drv, const unsigned char *attrib)
339 {
340         struct sysfs_attribute *attribute = NULL;
341         unsigned char path[SYSFS_PATH_MAX];
342
343         if (bus == NULL || drv == NULL || attrib == NULL) {
344                 errno = EINVAL;
345                 return NULL;
346         }
347
348         memset(path, 0, SYSFS_NAME_LEN);
349         if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
350                 dprintf("Error getting to driver %s\n", drv);
351                 return NULL;
352         }
353         strcat(path, "/");
354         strcat(path, attrib);
355         attribute = sysfs_open_attribute(path);
356         if (attribute == NULL) {
357                 dprintf("Error opening attribute %s for driver %s\n",
358                                 attrib, drv);
359                 return NULL;
360         }
361         if ((sysfs_read_attribute(attribute)) != 0) {
362                 dprintf("Error reading attribute %s for driver %s\n", 
363                                 attrib, drv);
364                 sysfs_close_attribute(attribute);
365                 return NULL;
366         }
367         return attribute;
368 }
369