chiark / gitweb /
[PATCH] 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         if (devdir->links != 0) {
125                 dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
126                         bdev = sysfs_open_device_path(curl->target);
127                         if (bdev == NULL) {
128                                 dprintf("Error opening device at %s\n", 
129                                                                 curl->target);
130                                 continue;
131                         }
132                         if (bus->devices == NULL)
133                                 bus->devices = dlist_new_with_delete
134                                         (sizeof(struct sysfs_device), 
135                                                         sysfs_close_dev);
136                         dlist_unshift(bus->devices, bdev);
137                 }
138         }
139         sysfs_close_directory(devdir);
140
141         return (bus->devices);
142 }
143
144 /**
145  * sysfs_get_bus_drivers: get all pci drivers
146  * @bus: pci bus to add drivers to
147  * returns dlist of drivers with success and NULL with error
148  */
149 struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
150 {
151         struct sysfs_driver *driver = NULL;
152         struct sysfs_directory *drvdir = NULL;
153         struct sysfs_directory *cursub = NULL;
154         unsigned char path[SYSFS_PATH_MAX];
155
156         if (bus == NULL) {
157                 errno = EINVAL;
158                 return NULL;
159         }
160         memset(path, 0, SYSFS_PATH_MAX);
161         strcpy(path, bus->path);
162         strcat(path, "/");
163         strcat(path, SYSFS_DRIVERS_NAME);
164         drvdir = sysfs_open_directory(path);
165         if (drvdir == NULL) 
166                 return NULL;
167
168         if (sysfs_read_dir_subdirs(drvdir) != 0) {
169                 sysfs_close_directory(drvdir);
170                 return NULL;
171         }
172         if (drvdir->subdirs != NULL) {
173                 dlist_for_each_data(drvdir->subdirs, cursub, 
174                                                 struct sysfs_directory) {
175                         driver = sysfs_open_driver_path(cursub->path);
176                         if (driver == NULL) {
177                                 dprintf("Error opening driver at %s\n", 
178                                                                 cursub->path);
179                                 continue;
180                         }
181                         if (bus->drivers == NULL)
182                                 bus->drivers = dlist_new_with_delete
183                                         (sizeof(struct sysfs_driver), 
184                                                         sysfs_close_drv);
185                         dlist_unshift(bus->drivers, driver);
186                 }
187         }
188         sysfs_close_directory(drvdir);
189         return (bus->drivers);
190 }
191
192 /**
193  * sysfs_open_bus: opens specific bus and all its devices on system
194  * returns sysfs_bus structure with success or NULL with error.
195  */
196 struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
197 {
198         struct sysfs_bus *bus = NULL;
199         unsigned char buspath[SYSFS_PATH_MAX];
200
201         if (name == NULL) {
202                 errno = EINVAL;
203                 return NULL;
204         }
205
206         memset(buspath, 0, SYSFS_PATH_MAX);
207         if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
208                 dprintf("Sysfs not supported on this system\n");
209                 return NULL;
210         }
211
212         if (sysfs_trailing_slash(buspath) == 0)
213                 strcat(buspath, "/");
214
215         strcat(buspath, SYSFS_BUS_NAME);
216         strcat(buspath, "/");
217         strcat(buspath, name);
218         if ((sysfs_path_is_dir(buspath)) != 0) {
219                 dprintf("Invalid path to bus: %s\n", buspath);
220                 return NULL;
221         }
222         bus = alloc_bus();
223         if (bus == NULL) {
224                 dprintf("calloc failed\n");
225                 return NULL;
226         }
227         strcpy(bus->name, name);        
228         strcpy(bus->path, buspath);
229
230         return bus;
231 }
232
233 /**
234  * sysfs_get_bus_device: Get specific device on bus using device's id
235  * @bus: bus to find device on
236  * @id: bus_id for device
237  * returns struct sysfs_device reference or NULL if not found.
238  */
239 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, 
240                                                         unsigned char *id)
241 {
242         if (bus == NULL || id == NULL) {
243                 errno = EINVAL;
244                 return NULL;
245         }
246
247         if (bus->devices == NULL) {
248                 bus->devices = sysfs_get_bus_devices(bus);
249                 if (bus->devices == NULL)
250                         return NULL;
251         }
252                 
253         return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
254                 bus_device_id_equal);
255 }
256
257 /**
258  * sysfs_get_bus_driver: Get specific driver on bus using driver name
259  * @bus: bus to find driver on
260  * @drvname: name of driver
261  * returns struct sysfs_driver reference or NULL if not found.
262  */
263 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, 
264                                                         unsigned char *drvname)
265 {
266         if (bus == NULL || drvname == NULL) {
267                 errno = EINVAL;
268                 return NULL;
269         }
270
271         if (bus->drivers == NULL) {
272                 bus->drivers = sysfs_get_bus_drivers(bus);
273                 if (bus->drivers == NULL)
274                         return NULL;
275         }
276         
277         return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
278                 bus_driver_name_equal);
279 }
280
281 /**
282  * sysfs_get_bus_attributes: returns bus' dlist of attributes
283  * @bus: bus to get attributes for.
284  * returns dlist of attributes or NULL if there aren't any.
285  */
286 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
287 {
288         if (bus == NULL)
289                 return NULL;
290
291         if (bus->directory == NULL) {
292                 bus->directory = sysfs_open_directory(bus->path);
293                 if (bus->directory == NULL)
294                         return NULL;
295         }
296         if (bus->directory->attributes == NULL) {
297                 if ((sysfs_read_dir_attributes(bus->directory)) != 0) 
298                         return NULL;
299         } else {
300                 if ((sysfs_path_is_dir(bus->path)) != 0) {
301                         dprintf("Bus at %s no longer exists\n", bus->path);
302                         return NULL;
303                 }
304                 if ((sysfs_refresh_attributes
305                                         (bus->directory->attributes)) != 0) {
306                         dprintf("Error refreshing bus attributes\n");
307                         return NULL;
308                 }
309         }
310         return bus->directory->attributes;
311 }
312
313 /**
314  * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
315  *      attributes.
316  * @bus: bus to retrieve attribute from
317  * @attrname: attribute name to retrieve
318  * returns reference to sysfs_attribute if found or NULL if not found
319  */
320 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
321                                                 unsigned char *attrname)
322 {
323         struct dlist *attrlist = NULL;
324         
325         if (bus == NULL) {
326                 errno = EINVAL;
327                 return NULL;
328         }
329         attrlist = sysfs_get_bus_attributes(bus);
330         if (attrlist == NULL)
331                 return NULL;
332         
333         return sysfs_get_directory_attribute(bus->directory, attrname);
334 }
335
336 /**
337  * sysfs_open_bus_device: locates a device on a bus and returns it. Device
338  *      must be closed using sysfs_close_device.
339  * @busname: Name of bus to search
340  * @dev_id: Id of device on bus.
341  * returns sysfs_device if found or NULL if not.
342  */
343 struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
344                                                         unsigned char *dev_id)
345 {
346         struct sysfs_device *rdev = NULL;
347         char path[SYSFS_PATH_MAX];
348
349         if (busname == NULL || dev_id == NULL) {
350                 errno = EINVAL;
351                 return NULL;
352         }
353
354         memset(path, 0, SYSFS_PATH_MAX);
355         if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
356                 dprintf("Error getting sysfs mount point\n");
357                 return NULL;
358         }
359
360         if (sysfs_trailing_slash(path) == 0)
361                 strcat(path, "/");
362         strcat(path, SYSFS_BUS_NAME);
363         strcat(path, "/");
364         strcat(path, busname);
365         strcat(path, "/");
366         strcat(path, SYSFS_DEVICES_NAME);
367         strcat(path, "/");
368         strcat(path, dev_id);
369
370         rdev = sysfs_open_device_path(path);
371         if (rdev == NULL) {
372                 dprintf("Error getting device %s on bus %s\n",
373                                 dev_id, busname);
374                 return NULL;
375         }
376         
377         return rdev;
378 }
379
380 /**
381  * sysfs_find_driver_bus: locates the bus the driver is on.
382  * @driver: name of the driver to locate
383  * @busname: buffer to copy name to
384  * @bsize: buffer size
385  * returns 0 with success, -1 with error
386  */
387 int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
388                                                         size_t bsize)
389 {
390         unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
391         struct dlist *buslist = NULL, *drivers = NULL;
392
393         if (driver == NULL || busname == NULL) {
394                 errno = EINVAL;
395                 return -1;
396         }
397
398         memset(subsys, 0, SYSFS_PATH_MAX);
399         strcat(subsys, "/");
400         strcpy(subsys, SYSFS_BUS_NAME);
401         buslist = sysfs_open_subsystem_list(subsys);
402         if (buslist != NULL) {
403                 dlist_for_each_data(buslist, bus, char) {
404                         memset(subsys, 0, SYSFS_PATH_MAX);
405                         strcat(subsys, "/");
406                         strcpy(subsys, SYSFS_BUS_NAME);
407                         strcat(subsys, "/");
408                         strcat(subsys, bus);
409                         strcat(subsys, "/");
410                         strcat(subsys, SYSFS_DRIVERS_NAME);
411                         drivers = sysfs_open_subsystem_list(subsys);
412                         if (drivers != NULL) {
413                                 dlist_for_each_data(drivers, curdrv, char) {
414                                         if (strcmp(driver, curdrv) == 0) {
415                                                 strncpy(busname, bus, bsize);
416                                                 sysfs_close_list(drivers);
417                                                 sysfs_close_list(buslist);
418                                                 return 0;
419                                         }
420                                 }
421                                 sysfs_close_list(drivers);
422                         }
423                 }
424                 sysfs_close_list(buslist);
425         }
426         return -1;
427 }