chiark / gitweb /
[PATCH] libsysfs changes for sysfsutils 0.3.0
[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  * get_driver_path: looks up the bus the driver is on and builds path to
167  *              the driver.
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
173  */
174 static int get_driver_path(const unsigned char *bus, const unsigned char *drv, 
175                                 unsigned char *path, size_t psize)
176 {
177         if (bus == NULL || drv == NULL || path == NULL) {
178                 errno = EINVAL;
179                 return -1;
180         }
181         if (sysfs_get_mnt_path(path, psize) != 0) {
182                 dprintf("Error getting sysfs mount path\n");
183                 return -1;
184         }
185         strcat(path, SYSFS_BUS_DIR);
186         strcat(path, "/");
187         strcat(path, bus);
188         strcat(path, SYSFS_DRIVERS_DIR);
189         strcat(path, "/");
190         strcat(path, drv);
191         return 0;
192 }
193
194 /**
195  * sysfs_open_driver_by_name: open a driver by name and return the bus
196  * the driver is on.
197  * @drv_name: driver to open
198  * @bus: the driver bus
199  * @bsize: size of bus buffer
200  * returns struct sysfs_driver if found, NULL otherwise
201  * NOTE: 
202  * 1. Need to call sysfs_close_driver_by_name to free up memory
203  * 2. Bus the driver is registered with must be supplied.
204  *      Use sysfs_find_driver_bus() to obtain the bus name
205  */
206 struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name,
207                                 const unsigned char *bus, size_t bsize)
208 {
209         struct sysfs_driver *driver = NULL;
210         struct sysfs_device *device = NULL;
211         struct sysfs_link *curlink = NULL;
212         unsigned char path[SYSFS_PATH_MAX];
213
214         if (drv_name == NULL || bus == NULL) {
215                 errno = EINVAL;
216                 return NULL;
217         }
218
219         memset(path, 0, SYSFS_PATH_MAX);
220         if (get_driver_path(bus, drv_name, path, SYSFS_PATH_MAX) != 0) {
221                 dprintf("Error getting to driver %s\n", drv_name);
222                 return NULL;
223         }
224         driver = sysfs_open_driver(path);
225         if (driver == NULL) {
226                 dprintf("Could not open driver %s\n", drv_name);
227                 return NULL;
228         }
229         if (driver->directory->links != NULL) {
230                 dlist_for_each_data(driver->directory->links, curlink, 
231                                                         struct sysfs_link) {
232                         device = sysfs_open_device(curlink->target);
233                         if (device == NULL) {
234                                 dprintf("Error opening device at %s\n", 
235                                                 curlink->target);
236                                 sysfs_close_driver_by_name(driver);
237                                 return NULL;
238                         }
239                         strcpy(device->driver_name, drv_name);
240                         if (driver->devices == NULL) 
241                                 driver->devices = dlist_new_with_delete
242                                                 (sizeof(struct sysfs_device),
243                                                         sysfs_close_driver_by_name_dev);
244                         dlist_unshift(driver->devices, device);
245                 }
246         }
247         return driver;
248 }
249
250
251 /**
252  * sysfs_open_driver_attr: read the user supplied driver attribute
253  * @bus: bus on which to look 
254  * @drv: driver whose attribute has to be read
255  * @attrib: Attribute to be read
256  * Returns struct sysfs_attribute on success and NULL on failure
257  *
258  * NOTE:
259  *      A call to sysfs_close_attribute() is required to close the
260  *      attribute returned and to free memory
261  */ 
262 struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, 
263                 const unsigned char *drv, const unsigned char *attrib)
264 {
265         struct sysfs_attribute *attribute = NULL;
266         unsigned char path[SYSFS_PATH_MAX];
267
268         if (bus == NULL || drv == NULL || attrib == NULL) {
269                 errno = EINVAL;
270                 return NULL;
271         }
272
273         memset(path, 0, SYSFS_NAME_LEN);
274         if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
275                 dprintf("Error getting to driver %s\n", drv);
276                 return NULL;
277         }
278         strcat(path, "/");
279         strcat(path, attrib);
280         attribute = sysfs_open_attribute(path);
281         if (attribute == NULL) {
282                 dprintf("Error opening attribute %s for driver %s\n",
283                                 attrib, drv);
284                 return NULL;
285         }
286         if ((sysfs_read_attribute(attribute)) != 0) {
287                 dprintf("Error reading attribute %s for driver %s\n", 
288                                 attrib, drv);
289                 sysfs_close_attribute(attribute);
290                 return NULL;
291         }
292         return attribute;
293 }
294