chiark / gitweb /
[PATCH] pre-libsysfs-0.4.0 patch
[elogind.git] / libsysfs / sysfs_bus.c
1 /*
2  * sysfs_bus.c
3  *
4  * Generic bus 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_dev(void *dev)
27 {
28         sysfs_close_device((struct sysfs_device *)dev);
29 }
30
31 static void sysfs_close_drv(void *drv)
32 {
33         sysfs_close_driver((struct sysfs_driver *)drv);
34 }
35
36 /*
37  * compares devices' bus ids.
38  * @a: device id looking for
39  * @b: sysfs_device comparing being compared
40  * returns 1 if a==b->bus_id or 0 not equal
41  */
42 static int bus_device_id_equal(void *a, void *b)
43 {
44         if (a == NULL || b == NULL)
45                 return 0;
46
47         if (strcmp(((unsigned char *)a), ((struct sysfs_device *)b)->bus_id) 
48             == 0)
49                 return 1;
50         return 0;
51 }
52
53 /*
54  * compares drivers' names.
55  * @a: driver name looking for
56  * @b: sysfs_driver comparing being compared
57  * returns 1 if a==b->name or 0 not equal
58  */
59 static int bus_driver_name_equal(void *a, void *b)
60 {
61         if (a == NULL || b == NULL)
62                 return 0;
63
64         if (strcmp(((unsigned char *)a), ((struct sysfs_driver *)b)->name) == 0)
65                 return 1;
66         return 0;
67 }
68
69 /**
70  * sysfs_close_bus: close single bus
71  * @bus: bus structure
72  */
73 void sysfs_close_bus(struct sysfs_bus *bus)
74 {
75         if (bus != NULL) {
76                 if (bus->directory != NULL)
77                         sysfs_close_directory(bus->directory);
78                 if (bus->devices)
79                         dlist_destroy(bus->devices);
80                 if (bus->drivers)
81                         dlist_destroy(bus->drivers);
82                 free(bus);
83         }
84 }
85
86 /**
87  * alloc_bus: mallocs new bus structure
88  * returns sysfs_bus_bus struct or NULL
89  */
90 static struct sysfs_bus *alloc_bus(void)
91 {
92         return (struct sysfs_bus *)calloc(1, sizeof(struct sysfs_bus));
93 }
94
95 /**
96  * sysfs_get_bus_devices: gets all devices for bus
97  * @bus: bus to get devices for
98  * returns dlist of devices with success and NULL with failure
99  */
100 struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
101 {
102         struct sysfs_device *bdev = NULL;
103         struct sysfs_directory *devdir = NULL;
104         struct sysfs_link *curl = NULL;
105         unsigned char path[SYSFS_PATH_MAX];
106
107         if (bus == NULL) {
108                 errno = EINVAL;
109                 return NULL;
110         }
111         memset(path, 0, SYSFS_PATH_MAX);
112         strcpy(path, bus->path);
113         strcat(path, "/");
114         strcat(path, SYSFS_DEVICES_NAME);
115         devdir = sysfs_open_directory(path);
116         if (devdir == NULL) 
117                 return NULL;
118
119         if (sysfs_read_dir_links(devdir) != 0) {
120                 sysfs_close_directory(devdir);
121                 return NULL;
122         }
123
124         dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
125                 bdev = sysfs_open_device(curl->target);
126                 if (bdev == NULL) {
127                         dprintf("Error opening device at %s\n", curl->target);
128                         continue;
129                 }
130                 if (bus->devices == NULL)
131                         bus->devices = dlist_new_with_delete
132                                 (sizeof(struct sysfs_device), sysfs_close_dev);
133                 dlist_unshift(bus->devices, bdev);
134         }
135         sysfs_close_directory(devdir);
136
137         return (bus->devices);
138 }
139
140 /**
141  * sysfs_get_bus_drivers: get all pci drivers
142  * @bus: pci bus to add drivers to
143  * returns dlist of drivers with success and NULL with error
144  */
145 struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
146 {
147         struct sysfs_driver *driver = NULL;
148         struct sysfs_directory *drvdir = NULL;
149         struct sysfs_directory *cursub = NULL;
150         unsigned char path[SYSFS_PATH_MAX];
151
152         if (bus == NULL) {
153                 errno = EINVAL;
154                 return NULL;
155         }
156         memset(path, 0, SYSFS_PATH_MAX);
157         strcpy(path, bus->path);
158         strcat(path, "/");
159         strcat(path, SYSFS_DRIVERS_NAME);
160         drvdir = sysfs_open_directory(path);
161         if (drvdir == NULL) 
162                 return NULL;
163
164         if (sysfs_read_dir_subdirs(drvdir) != 0) {
165                 sysfs_close_directory(drvdir);
166                 return NULL;
167         }
168         dlist_for_each_data(drvdir->subdirs, cursub, struct sysfs_directory) {
169                 driver = sysfs_open_driver(cursub->path);
170                 if (driver == NULL) {
171                         dprintf("Error opening driver at %s\n", cursub->path);
172                         continue;
173                 }
174                 if (bus->drivers == NULL)
175                         bus->drivers = dlist_new_with_delete
176                                 (sizeof(struct sysfs_driver), sysfs_close_drv);
177                 dlist_unshift(bus->drivers, driver);
178         }
179         sysfs_close_directory(drvdir);
180         return (bus->drivers);
181 }
182
183 /**
184  * sysfs_open_bus: opens specific bus and all its devices on system
185  * returns sysfs_bus structure with success or NULL with error.
186  */
187 struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
188 {
189         struct sysfs_bus *bus = NULL;
190         unsigned char buspath[SYSFS_PATH_MAX];
191
192         if (name == NULL) {
193                 errno = EINVAL;
194                 return NULL;
195         }
196
197         memset(buspath, 0, SYSFS_PATH_MAX);
198         if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
199                 dprintf("Sysfs not supported on this system\n");
200                 return NULL;
201         }
202
203         if (sysfs_trailing_slash(buspath) == 0)
204                 strcat(buspath, "/");
205
206         strcat(buspath, SYSFS_BUS_NAME);
207         strcat(buspath, "/");
208         strcat(buspath, name);
209         if ((sysfs_path_is_dir(buspath)) != 0) {
210                 dprintf("Invalid path to bus: %s\n", buspath);
211                 return NULL;
212         }
213         bus = alloc_bus();
214         if (bus == NULL) {
215                 dprintf("calloc failed\n");
216                 return NULL;
217         }
218         strcpy(bus->name, name);        
219         strcpy(bus->path, buspath);
220
221         return bus;
222 }
223
224 /**
225  * sysfs_get_bus_device: Get specific device on bus using device's id
226  * @bus: bus to find device on
227  * @id: bus_id for device
228  * returns struct sysfs_device reference or NULL if not found.
229  */
230 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, 
231                                                         unsigned char *id)
232 {
233         if (bus == NULL || id == NULL) {
234                 errno = EINVAL;
235                 return NULL;
236         }
237
238         if (bus->devices == NULL) {
239                 bus->devices = sysfs_get_bus_devices(bus);
240                 if (bus->devices == NULL)
241                         return NULL;
242         }
243                 
244         return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
245                 bus_device_id_equal);
246 }
247
248 /**
249  * sysfs_get_bus_driver: Get specific driver on bus using driver name
250  * @bus: bus to find driver on
251  * @drvname: name of driver
252  * returns struct sysfs_driver reference or NULL if not found.
253  */
254 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, 
255                                                         unsigned char *drvname)
256 {
257         if (bus == NULL || drvname == NULL) {
258                 errno = EINVAL;
259                 return NULL;
260         }
261
262         if (bus->drivers == NULL) {
263                 bus->drivers = sysfs_get_bus_drivers(bus);
264                 if (bus->drivers == NULL)
265                         return NULL;
266         }
267         
268         return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
269                 bus_driver_name_equal);
270 }
271
272 /**
273  * sysfs_get_bus_attributes: returns bus' dlist of attributes
274  * @bus: bus to get attributes for.
275  * returns dlist of attributes or NULL if there aren't any.
276  */
277 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
278 {
279         if (bus == NULL)
280                 return NULL;
281
282         if (bus->directory == NULL) {
283                 bus->directory = sysfs_open_directory(bus->path);
284                 if (bus->directory == NULL)
285                         return NULL;
286         }
287         if (bus->directory->attributes == NULL) {
288                 if ((sysfs_read_dir_attributes(bus->directory)) != 0) 
289                         return NULL;
290         } else {
291                 if ((sysfs_path_is_dir(bus->path)) != 0) {
292                         dprintf("Bus at %s no longer exists\n", bus->path);
293                         return NULL;
294                 }
295                 if ((sysfs_refresh_attributes
296                                         (bus->directory->attributes)) != 0) {
297                         dprintf("Error refreshing bus attributes\n");
298                         return NULL;
299                 }
300         }
301         return bus->directory->attributes;
302 }
303
304 /**
305  * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
306  *      attributes.
307  * @bus: bus to retrieve attribute from
308  * @attrname: attribute name to retrieve
309  * returns reference to sysfs_attribute if found or NULL if not found
310  */
311 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
312                                                 unsigned char *attrname)
313 {
314         struct dlist *attrlist = NULL;
315         
316         if (bus == NULL) {
317                 errno = EINVAL;
318                 return NULL;
319         }
320         attrlist = sysfs_get_bus_attributes(bus);
321         if (attrlist == NULL)
322                 return NULL;
323         
324         return sysfs_get_directory_attribute(bus->directory, attrname);
325 }
326
327 /**
328  * sysfs_open_bus_device: locates a device on a bus and returns it. Device
329  *      must be closed using sysfs_close_device.
330  * @busname: Name of bus to search
331  * @dev_id: Id of device on bus.
332  * returns sysfs_device if found or NULL if not.
333  */
334 struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
335                                                         unsigned char *dev_id)
336 {
337         struct sysfs_device *rdev = NULL;
338         char path[SYSFS_PATH_MAX];
339
340         if (busname == NULL || dev_id == NULL) {
341                 errno = EINVAL;
342                 return NULL;
343         }
344
345         memset(path, 0, SYSFS_PATH_MAX);
346         if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
347                 dprintf("Error getting sysfs mount point\n");
348                 return NULL;
349         }
350         
351         if (sysfs_trailing_slash(path) == 0)
352                 strcat(path, "/");
353
354         strcat(path, SYSFS_BUS_NAME);
355         strcat(path, "/");
356         strcat(path, busname);
357         strcat(path, "/");
358         strcat(path, SYSFS_DEVICES_NAME);
359         strcat(path, "/");
360         strcat(path, dev_id);
361
362         rdev = sysfs_open_device(path);
363         if (rdev == NULL) {
364                 dprintf("Error getting device %s on bus %s\n",
365                                 dev_id, busname);
366                 return NULL;
367         }
368         
369         return rdev;
370 }
371
372 /**
373  * sysfs_find_driver_bus: locates the bus the driver is on.
374  * @driver: name of the driver to locate
375  * @busname: buffer to copy name to
376  * @bsize: buffer size
377  * returns 0 with success, -1 with error
378  */
379 int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
380                                                         size_t bsize)
381 {
382         unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
383         struct dlist *buslist = NULL, *drivers = NULL;
384
385         if (driver == NULL || busname == NULL) {
386                 errno = EINVAL;
387                 return -1;
388         }
389
390         memset(subsys, 0, SYSFS_PATH_MAX);
391         strcat(subsys, "/");
392         strcpy(subsys, SYSFS_BUS_NAME);
393         buslist = sysfs_open_subsystem_list(subsys);
394         if (buslist != NULL) {
395                 dlist_for_each_data(buslist, bus, char) {
396                         memset(subsys, 0, SYSFS_PATH_MAX);
397                         strcat(subsys, "/");
398                         strcpy(subsys, SYSFS_BUS_NAME);
399                         strcat(subsys, "/");
400                         strcat(subsys, bus);
401                         strcat(subsys, "/");
402                         strcat(subsys, SYSFS_DRIVERS_NAME);
403                         drivers = sysfs_open_subsystem_list(subsys);
404                         if (drivers != NULL) {
405                                 dlist_for_each_data(drivers, curdrv, char) {
406                                         if (strcmp(driver, curdrv) == 0) {
407                                                 strncpy(busname, bus, bsize);
408                                                 sysfs_close_list(drivers);
409                                                 sysfs_close_list(buslist);
410                                                 return 0;
411                                         }
412                                 }
413                                 sysfs_close_list(drivers);
414                         }
415                 }
416                 sysfs_close_list(buslist);
417         }
418         return -1;
419 }