chiark / gitweb /
[PATCH] another patch for path problem
[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  * open_bus_dir: opens up sysfs bus directory
97  * returns sysfs_directory struct with success and NULL with error
98  */
99 static struct sysfs_directory *open_bus_dir(const unsigned char *name)
100 {
101         struct sysfs_directory *busdir = NULL;
102         unsigned char buspath[SYSFS_PATH_MAX];
103
104         if (name == NULL) {
105                 errno = EINVAL;
106                 return NULL;
107         }
108
109         memset(buspath, 0, SYSFS_PATH_MAX);
110         if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
111                 dprintf("Sysfs not supported on this system\n");
112                 return NULL;
113         }
114
115         if (sysfs_trailing_slash(buspath) == 0)
116                 strcat(buspath, "/");
117                 
118         strcat(buspath, SYSFS_BUS_NAME);
119         strcat(buspath, "/");
120         strcat(buspath, name);
121         busdir = sysfs_open_directory(buspath);
122         if (busdir == NULL) {
123                 errno = EINVAL;
124                 dprintf("Bus %s not supported on this system\n",
125                         name);
126                 return NULL;
127         }
128         if ((sysfs_read_directory(busdir)) != 0) {
129                 dprintf("Error reading %s bus dir %s\n", name, 
130                         buspath);
131                 sysfs_close_directory(busdir);
132                 return NULL;
133         }
134         /* read in devices and drivers subdirs */
135         sysfs_read_all_subdirs(busdir);
136
137         return busdir;
138 }
139
140 /**
141  * get_all_bus_devices: gets all devices for bus
142  * @bus: bus to get devices for
143  * returns 0 with success and -1 with failure
144  */
145 static int get_all_bus_devices(struct sysfs_bus *bus)
146 {
147         struct sysfs_device *bdev = NULL;
148         struct sysfs_directory *cur = NULL;
149         struct sysfs_link *curl = NULL;
150
151         if (bus == NULL || bus->directory == NULL) {
152                 errno = EINVAL;
153                 return -1;
154         }
155         if (bus->directory->subdirs == NULL)
156                 return 0;
157
158         dlist_for_each_data(bus->directory->subdirs, cur, 
159                         struct sysfs_directory) {
160                 if (strcmp(cur->name, SYSFS_DEVICES_NAME) != 0)
161                         continue;
162                 if (cur->links == NULL)
163                         continue;
164                 dlist_for_each_data(cur->links, curl, struct sysfs_link) {
165                         bdev = sysfs_open_device(curl->target);
166                         if (bdev == NULL) {
167                                 dprintf("Error opening device at %s\n",
168                                         curl->target);
169                                 continue;
170                         }
171                         if (bus->devices == NULL)
172                                 bus->devices = dlist_new_with_delete
173                                         (sizeof(struct sysfs_device),
174                                                         sysfs_close_dev);
175                         dlist_unshift(bus->devices, bdev);
176                 }
177         }
178                         
179         return 0;
180 }
181
182 /**
183  * get_all_bus_drivers: get all pci drivers
184  * @bus: pci bus to add drivers to
185  * returns 0 with success and -1 with error
186  */
187 static int get_all_bus_drivers(struct sysfs_bus *bus)
188 {
189         struct sysfs_driver *driver = NULL;
190         struct sysfs_directory *cur = NULL;
191         struct sysfs_directory *cursub = NULL;
192
193         if (bus == NULL || bus->directory == NULL) {
194                 errno = EINVAL;
195                 return -1;
196         }
197         if (bus->directory->subdirs == NULL)
198                 return 0;
199
200         dlist_for_each_data(bus->directory->subdirs, cur,
201                         struct sysfs_directory) {
202                 if (strcmp(cur->name, SYSFS_DRIVERS_NAME) != 0)
203                         continue;
204                 if (cur->subdirs == NULL)
205                         continue;
206                 dlist_for_each_data(cur->subdirs, cursub,
207                                 struct sysfs_directory) {
208                         driver = sysfs_open_driver(cursub->path);
209                         if (driver == NULL) {
210                                 dprintf("Error opening driver at %s\n",
211                                         cursub->path);
212                                 continue;
213                         }
214                         if (bus->drivers == NULL)
215                                 bus->drivers = dlist_new_with_delete
216                                         (sizeof(struct sysfs_driver),
217                                                         sysfs_close_drv);
218                         dlist_unshift(bus->drivers, driver);
219                 }
220         }
221         
222         return 0;
223 }
224
225 /**
226  * match_bus_device_to_driver: returns 1 if device is bound to driver
227  * @driver: driver to match
228  * @busid: busid of device to match
229  * returns 1 if found and 0 if not found
230  */
231 static int match_bus_device_to_driver(struct sysfs_driver *driver, 
232                                                         unsigned char *busid)
233 {
234         struct sysfs_link *cur = NULL;
235         int found = 0;
236
237         if (driver == NULL || driver->directory == NULL || busid == NULL) {
238                 errno = EINVAL;
239                 return found;
240         }
241         if (driver->directory->links != NULL) {
242                 dlist_for_each_data(driver->directory->links, cur,
243                                 struct sysfs_link) {
244                         if ((strcmp(cur->name, busid)) == 0)
245                                 found++;
246                 }
247         }
248         return found;
249 }
250
251 /**
252  * link_bus_devices_to_drivers: goes through and links devices to drivers
253  * @bus: bus to link
254  */
255 static void link_bus_devices_to_drivers(struct sysfs_bus *bus)
256 {
257         struct sysfs_device *dev = NULL;
258         struct sysfs_driver *drv = NULL;
259         
260         if (bus != NULL && bus->devices != NULL && bus->drivers != NULL) {
261                 dlist_for_each_data(bus->devices, dev, struct sysfs_device) {
262                         dlist_for_each_data(bus->drivers, drv,
263                                         struct sysfs_driver) {
264                                 if ((match_bus_device_to_driver(drv, 
265                                                 dev->bus_id)) != 0) {
266                                         strncpy(dev->driver_name, drv->name,
267                                                         SYSFS_NAME_LEN);
268                                         if (drv->devices == NULL)
269                                                 drv->devices = dlist_new
270                                                         (sizeof(struct 
271                                                                 sysfs_device));
272                                         dlist_unshift(drv->devices, dev);
273                                 }
274                         }
275                 }
276         }
277 }
278
279 /**
280  * sysfs_open_bus: opens specific bus and all its devices on system
281  * returns sysfs_bus structure with success or NULL with error.
282  */
283 struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
284 {
285         struct sysfs_bus *bus = NULL;
286         struct sysfs_directory *busdir = NULL;
287
288         if (name == NULL) {
289                 errno = EINVAL;
290                 return NULL;
291         }
292
293         bus = alloc_bus();
294         if (bus == NULL) {
295                 dprintf("calloc failed\n");
296                 return NULL;
297         }
298         strcpy(bus->name, name);        
299         busdir = open_bus_dir(name);
300         if (busdir == NULL) {
301                 dprintf("Invalid bus, %s not supported on this system\n",
302                         name);
303                 sysfs_close_bus(bus);
304                 return NULL;
305         }
306         strcpy(bus->path, busdir->path);
307         bus->directory = busdir;
308         if ((get_all_bus_devices(bus)) != 0) {
309                 dprintf("Error reading %s bus devices\n", name);
310                 sysfs_close_bus(bus);
311                 return NULL;
312         }
313         if ((get_all_bus_drivers(bus)) != 0) {
314                 dprintf("Error reading %s bus drivers\n", name);
315                 sysfs_close_bus(bus);
316                 return NULL;
317         }
318         link_bus_devices_to_drivers(bus);
319
320         return bus;
321 }
322
323 /**
324  * sysfs_get_bus_device: Get specific device on bus using device's id
325  * @bus: bus to find device on
326  * @id: bus_id for device
327  * returns struct sysfs_device reference or NULL if not found.
328  */
329 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, 
330                                                         unsigned char *id)
331 {
332         if (bus == NULL || id == NULL) {
333                 errno = EINVAL;
334                 return NULL;
335         }
336
337         return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
338                 bus_device_id_equal);
339 }
340
341 /**
342  * sysfs_get_bus_driver: Get specific driver on bus using driver name
343  * @bus: bus to find driver on
344  * @drvname: name of driver
345  * returns struct sysfs_driver reference or NULL if not found.
346  */
347 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, 
348                                                         unsigned char *drvname)
349 {
350         if (bus == NULL || drvname == NULL) {
351                 errno = EINVAL;
352                 return NULL;
353         }
354
355         return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
356                 bus_driver_name_equal);
357 }
358
359 /**
360  * sysfs_get_bus_attributes: returns bus' dlist of attributes
361  * @bus: bus to get attributes for.
362  * returns dlist of attributes or NULL if there aren't any.
363  */
364 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
365 {
366         if (bus == NULL || bus->directory == NULL)
367                 return NULL;
368         return bus->directory->attributes;
369 }
370
371 /**
372  * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
373  *      attributes.
374  * @bus: bus to retrieve attribute from
375  * @attrname: attribute name to retrieve
376  * returns reference to sysfs_attribute if found or NULL if not found
377  */
378 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
379                                                 unsigned char *attrname)
380 {
381         if (bus == NULL || bus->directory == NULL || attrname == NULL) {
382                 errno = EINVAL;
383                 return NULL;
384         }
385         return sysfs_get_directory_attribute(bus->directory, attrname);
386 }
387
388 /**
389  * sysfs_open_bus_device: locates a device on a bus and returns it. Device
390  *      must be closed using sysfs_close_device.
391  * @busname: Name of bus to search
392  * @dev_id: Id of device on bus.
393  * returns sysfs_device if found or NULL if not.
394  */
395 struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
396                                                         unsigned char *dev_id)
397 {
398         struct sysfs_device *rdev = NULL;
399         char path[SYSFS_PATH_MAX];
400
401         if (busname == NULL || dev_id == NULL) {
402                 errno = EINVAL;
403                 return NULL;
404         }
405
406         memset(path, 0, SYSFS_PATH_MAX);
407         if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
408                 dprintf("Error getting sysfs mount point\n");
409                 return NULL;
410         }
411
412         if (sysfs_trailing_slash(path) == 0)
413                 strcat(path, "/");
414         strcat(path, SYSFS_BUS_NAME);
415         strcat(path, "/");
416         strcat(path, busname);
417         strcat(path, "/");
418         strcat(path, SYSFS_DEVICES_NAME);
419         strcat(path, "/");
420         strcat(path, dev_id);
421
422         rdev = sysfs_open_device(path);
423         if (rdev == NULL) {
424                 dprintf("Error getting device %s on bus %s\n",
425                                 dev_id, busname);
426                 return NULL;
427         }
428         
429         return rdev;
430 }
431
432 /**
433  * sysfs_find_driver_bus: locates the bus the driver is on.
434  * @driver: name of the driver to locate
435  * @busname: buffer to copy name to
436  * @bsize: buffer size
437  * returns 0 with success, -1 with error
438  */
439 int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
440                                                         size_t bsize)
441 {
442         unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
443         struct dlist *buslist = NULL, *drivers = NULL;
444
445         if (driver == NULL || busname == NULL) {
446                 errno = EINVAL;
447                 return -1;
448         }
449
450         memset(subsys, 0, SYSFS_PATH_MAX);
451         strcpy(subsys, SYSFS_BUS_NAME);
452         buslist = sysfs_open_subsystem_list(subsys);
453         if (buslist != NULL) {
454                 dlist_for_each_data(buslist, bus, char) {
455                         memset(subsys, 0, SYSFS_PATH_MAX);
456                         strcat(subsys, "/");
457                         strcpy(subsys, SYSFS_BUS_NAME);
458                         strcat(subsys, "/");
459                         strcat(subsys, bus);
460                         strcat(subsys, "/");
461                         strcat(subsys, SYSFS_DRIVERS_NAME);
462                         drivers = sysfs_open_subsystem_list(subsys);
463                         if (drivers != NULL) {
464                                 dlist_for_each_data(drivers, curdrv, char) {
465                                         if (strcmp(driver, curdrv) == 0) {
466                                                 strncpy(busname, bus, bsize);
467                                                 sysfs_close_list(drivers);
468                                                 sysfs_close_list(buslist);
469                                                 return 0;
470                                         }
471                                 }
472                                 sysfs_close_list(drivers);
473                         }
474                 }
475                 sysfs_close_list(buslist);
476         }
477         return -1;
478 }
479