chiark / gitweb /
[PATCH] Libsysfs updates
[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 "sysfs/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  * alloc_driver: allocates and initializes driver
70  * returns struct sysfs_driver with success and NULL with error.
71  */
72 static struct sysfs_driver *alloc_driver(void)
73 {
74         return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
75 }
76
77 /**
78  * sysfs_open_driver_path: opens and initializes driver structure
79  * @path: path to driver directory
80  * returns struct sysfs_driver with success and NULL with error
81  */
82 struct sysfs_driver *sysfs_open_driver_path(const char *path)
83 {
84         struct sysfs_driver *driver = NULL;
85
86         if (path == NULL) {
87                 errno = EINVAL;
88                 return NULL;
89         }
90         if ((sysfs_path_is_dir(path)) != 0) {
91                 dprintf("Invalid path to driver: %s\n", path);
92                 return NULL;
93         }
94         driver = alloc_driver();
95         if (driver == NULL) {
96                 dprintf("Error allocating driver at %s\n", path);
97                 return NULL;
98         }
99         if ((sysfs_get_name_from_path(path, driver->name, 
100                                         SYSFS_NAME_LEN)) != 0) {
101                 dprintf("Error getting driver name from path\n");
102                 free(driver);
103                 return NULL;
104         }
105         safestrcpy(driver->path, path);
106         if ((sysfs_remove_trailing_slash(driver->path)) != 0) {
107                 dprintf("Invalid path to driver %s\n", driver->path);
108                 sysfs_close_driver(driver);
109                 return NULL;
110         }
111         
112         return driver;
113 }
114
115 /**
116  * sysfs_get_driver_attributes: gets list of attributes for the given driver
117  * @driver: sysfs_driver for which attributes are required
118  * returns a dlist of attributes corresponding to the driver if present
119  *      NULL otherwise
120  */
121 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
122 {
123         if (driver == NULL) {
124                 errno = EINVAL;
125                 return NULL;
126         }
127
128         if (driver->directory == NULL) {
129                 if ((open_driver_dir(driver)) == 1) 
130                         return NULL;
131         }
132         if (driver->directory->attributes == NULL) {
133                 if ((sysfs_read_dir_attributes(driver->directory)) != 0) 
134                         return NULL;
135         }
136         return(driver->directory->attributes);
137 }
138
139 /**
140  * sysfs_refresh_driver_attributes: refreshes the driver's list of attributes
141  * @driver: sysfs_driver whose attributes to refresh
142  *
143  * NOTE: Upon return, prior references to sysfs_attributes for this driver
144  *              _may_ not be valid
145  *              
146  * Returns list of attributes on success and NULL on failure
147  */
148 struct dlist *sysfs_refresh_driver_attributes(struct sysfs_driver *driver)
149 {
150         if (driver == NULL) {
151                 errno = EINVAL;
152                 return NULL;
153         }
154         if (driver->directory == NULL)
155                 return (sysfs_get_driver_attributes(driver));
156         
157         if ((sysfs_refresh_dir_attributes(driver->directory)) != 0) {
158                 dprintf("Error refreshing driver attributes\n");
159                 return NULL;
160         }
161         return (driver->directory->attributes);
162 }
163
164 /**
165  * sysfs_get_driver_attr: searches driver's attributes by name
166  * @drv: driver to look through
167  * @name: attribute name to get
168  * returns sysfs_attribute reference on success or NULL with error
169  */ 
170 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
171                                         const char *name)
172 {
173         struct dlist *attrlist = NULL;
174
175         if (drv == NULL) {
176                 errno = EINVAL;
177                 return NULL;
178         }
179         
180         attrlist = sysfs_get_driver_attributes(drv);
181         if (attrlist == NULL) 
182                 return NULL;
183
184         return sysfs_get_directory_attribute(drv->directory, (char *)name);
185 }
186
187 /**
188  * sysfs_get_driver_links: gets list of links from the given driver
189  * @driver: sysfs_driver for which links list is required
190  * returns a dlist of links corresponding to the driver if present
191  *      NULL otherwise
192  */
193 struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
194 {
195         if (driver == NULL) {
196                 errno = EINVAL;
197                 return NULL;
198         }
199
200         if (driver->directory == NULL) 
201                 if ((open_driver_dir(driver)) == 1)
202                         return NULL;
203         
204         if (driver->directory->links == NULL)
205                 if ((sysfs_read_dir_links(driver->directory)) != 0) 
206                         return NULL;
207                 
208         return(driver->directory->links);
209 }
210
211 /**
212  * sysfs_get_driver_devices: open up the list of devices this driver supports
213  * @driver: sysfs_driver for which devices are needed
214  * Returns dlist of devices on SUCCESS or NULL with ERROR
215  */ 
216 struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver)
217 {
218         struct sysfs_link *curlink = NULL;
219         struct sysfs_device *device = NULL;
220
221         if (driver == NULL) {
222                 errno = EINVAL;
223                 return NULL;
224         }
225         
226         if (driver->devices != NULL)
227                 return (driver->devices);
228
229         if (driver->directory == NULL || driver->directory->links == NULL) {
230                 struct dlist *list = NULL;
231                 list = sysfs_get_driver_links(driver);
232         }
233         
234         if (driver->directory->links != NULL) {
235                 dlist_for_each_data(driver->directory->links, curlink, 
236                                                 struct sysfs_link) {
237                         device = sysfs_open_device_path(curlink->target);
238                         if (device == NULL) {
239                                 dprintf("Error opening device at %s\n", 
240                                                 curlink->target);
241                                 return NULL;
242                         }
243                         if (driver->devices == NULL) 
244                                 driver->devices = dlist_new_with_delete
245                                                 (sizeof(struct sysfs_device),
246                                                  sysfs_close_driver_device);
247                         dlist_unshift_sorted(driver->devices, device, 
248                                                                 sort_list);
249                 }
250         }
251         return (driver->devices);
252 }
253
254 /**
255  * sysfs_refresh_driver_devices: Refreshes drivers list of devices
256  * @driver: sysfs_driver whose devices list needs to be refreshed
257  *
258  * NOTE: Upon return from this function, prior sysfs_device references from
259  *              this driver's list of devices _may_ not be valid
260  *              
261  * Returns dlist of devices on success and NULL on failure
262  */
263 struct dlist *sysfs_refresh_driver_devices(struct sysfs_driver *driver)
264 {
265         if (driver == NULL) {
266                 errno = EINVAL;
267                 return NULL;
268         }
269         
270         if (driver->devices != NULL) {
271                 dlist_destroy(driver->devices);
272                 driver->devices = NULL;
273         }
274         
275         if (driver->directory == NULL)
276                 return (sysfs_get_driver_devices(driver));
277
278         if ((sysfs_refresh_dir_links(driver->directory)) != 0) {
279                 dprintf("Error refreshing driver links\n");
280                 return NULL;
281         }
282         
283         return (sysfs_get_driver_devices(driver));
284 }
285
286 /**
287  * sysfs_get_driver_device: looks up a device from a list of driver's devices
288  *      and returns its sysfs_device corresponding to it
289  * @driver: sysfs_driver on which to search
290  * @name: name of the device to search
291  * Returns a sysfs_device if found, NULL otherwise
292  */
293 struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver,
294                                 const char *name)
295 {
296         struct sysfs_device *device = NULL;
297         struct dlist *devlist = NULL;
298
299         if (driver == NULL || name == NULL) {
300                 errno = EINVAL;
301                 return NULL;
302         }
303
304         if (driver->devices == NULL) {
305                 devlist = sysfs_get_driver_devices(driver);
306                 if (devlist == NULL) {
307                         dprintf("Error getting driver devices\n");
308                         return NULL;
309                 }
310         }
311         dlist_for_each_data(driver->devices, device, struct sysfs_device) {
312                 if (!(strncmp(device->name, name, SYSFS_NAME_LEN)))
313                         return device;
314         }
315         return NULL;
316 }
317
318 /**
319  * get_driver_path: looks up the bus the driver is on and builds path to
320  *              the driver.
321  * @bus: bus on which to search
322  * @drv: driver to look for
323  * @path: buffer to return path to driver
324  * @psize: size of "path"
325  * Returns 0 on success and -1 on error
326  */
327 static int get_driver_path(const char *bus, const char *drv, 
328                         char *path, size_t psize)
329 {
330         if (bus == NULL || drv == NULL || path == NULL || psize == 0) {
331                 errno = EINVAL;
332                 return -1;
333         }
334         if (sysfs_get_mnt_path(path, psize) != 0) {
335                 dprintf("Error getting sysfs mount path\n");
336                 return -1;
337         }
338         safestrncat(path, "/", psize);
339         safestrncat(path, SYSFS_BUS_NAME, psize);
340         safestrncat(path, "/", psize);
341         safestrncat(path, bus, psize);
342         safestrncat(path, "/", psize);
343         safestrncat(path, SYSFS_DRIVERS_NAME, psize);
344         safestrncat(path, "/", psize);
345         safestrncat(path, drv, psize);
346         return 0;
347 }
348
349 /**
350  * sysfs_open_driver_attr: read the user supplied driver attribute
351  * @bus: bus on which to look 
352  * @drv: driver whose attribute has to be read
353  * @attrib: Attribute to be read
354  * Returns struct sysfs_attribute on success and NULL on failure
355  *
356  * NOTE:
357  *      A call to sysfs_close_attribute() is required to close the
358  *      attribute returned and to free memory
359  */ 
360 struct sysfs_attribute *sysfs_open_driver_attr(const char *bus, 
361                 const char *drv, const char *attrib)
362 {
363         struct sysfs_attribute *attribute = NULL;
364         char path[SYSFS_PATH_MAX];
365
366         if (bus == NULL || drv == NULL || attrib == NULL) {
367                 errno = EINVAL;
368                 return NULL;
369         }
370
371         memset(path, 0, SYSFS_PATH_MAX);
372         if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
373                 dprintf("Error getting to driver %s\n", drv);
374                 return NULL;
375         }
376         safestrcat(path, "/");
377         safestrcat(path, attrib);
378         attribute = sysfs_open_attribute(path);
379         if (attribute == NULL) {
380                 dprintf("Error opening attribute %s for driver %s\n",
381                                 attrib, drv);
382                 return NULL;
383         }
384         if ((sysfs_read_attribute(attribute)) != 0) {
385                 dprintf("Error reading attribute %s for driver %s\n", 
386                                 attrib, drv);
387                 sysfs_close_attribute(attribute);
388                 return NULL;
389         }
390         return attribute;
391 }
392
393 /**
394  * sysfs_open_driver: open driver by name, given its bus
395  * @bus_name: Name of the bus
396  * @drv_name: Name of the driver
397  * Returns the sysfs_driver reference on success and NULL on failure
398  */
399 struct sysfs_driver *sysfs_open_driver(const char *bus_name, 
400                         const char *drv_name)
401 {
402         char path[SYSFS_PATH_MAX];
403         struct sysfs_driver *driver = NULL;
404
405         if (drv_name == NULL || bus_name == NULL) {
406                 errno = EINVAL;
407                 return NULL;
408         }
409
410         memset(path, 0, SYSFS_PATH_MAX);
411         if ((get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) != 0) {
412                 dprintf("Error getting to driver %s\n", drv_name);
413                 return NULL;
414         }
415         driver = sysfs_open_driver_path(path);
416         if (driver == NULL) {
417                 dprintf("Error opening driver at %s\n", path);
418                 return NULL;
419         }
420         return driver;
421 }
422