4 * Generic bus utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
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.
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.
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
23 #include "sysfs/libsysfs.h"
26 static void sysfs_close_dev(void *dev)
28 sysfs_close_device((struct sysfs_device *)dev);
31 static void sysfs_close_drv(void *drv)
33 sysfs_close_driver((struct sysfs_driver *)drv);
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
42 static int bus_device_id_equal(void *a, void *b)
44 if (a == NULL || b == NULL)
47 if (strcmp(((char *)a), ((struct sysfs_device *)b)->bus_id)
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
59 static int bus_driver_name_equal(void *a, void *b)
61 if (a == NULL || b == NULL)
64 if (strcmp(((char *)a), ((struct sysfs_driver *)b)->name) == 0)
70 * sysfs_close_bus: close single bus
73 void sysfs_close_bus(struct sysfs_bus *bus)
76 if (bus->directory != NULL)
77 sysfs_close_directory(bus->directory);
79 dlist_destroy(bus->devices);
81 dlist_destroy(bus->drivers);
87 * alloc_bus: mallocs new bus structure
88 * returns sysfs_bus_bus struct or NULL
90 static struct sysfs_bus *alloc_bus(void)
92 return (struct sysfs_bus *)calloc(1, sizeof(struct sysfs_bus));
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
100 struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
102 struct sysfs_device *bdev = NULL;
103 struct sysfs_directory *devdir = NULL;
104 struct sysfs_link *curl = NULL;
105 char path[SYSFS_PATH_MAX];
111 memset(path, 0, SYSFS_PATH_MAX);
112 safestrcpy(path, bus->path);
113 safestrcat(path, "/");
114 safestrcat(path, SYSFS_DEVICES_NAME);
115 devdir = sysfs_open_directory(path);
119 if (sysfs_read_dir_links(devdir) != 0) {
120 sysfs_close_directory(devdir);
124 if (devdir->links != NULL) {
125 dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
126 bdev = sysfs_open_device_path(curl->target);
128 dprintf("Error opening device at %s\n",
132 if (bus->devices == NULL)
133 bus->devices = dlist_new_with_delete
134 (sizeof(struct sysfs_device),
136 dlist_unshift_sorted(bus->devices, bdev, sort_list);
139 sysfs_close_directory(devdir);
141 return (bus->devices);
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
149 struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
151 struct sysfs_driver *driver = NULL;
152 struct sysfs_directory *drvdir = NULL;
153 struct sysfs_directory *cursub = NULL;
154 char path[SYSFS_PATH_MAX];
160 memset(path, 0, SYSFS_PATH_MAX);
161 safestrcpy(path, bus->path);
162 safestrcat(path, "/");
163 safestrcat(path, SYSFS_DRIVERS_NAME);
164 drvdir = sysfs_open_directory(path);
168 if (sysfs_read_dir_subdirs(drvdir) != 0) {
169 sysfs_close_directory(drvdir);
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",
181 if (bus->drivers == NULL)
182 bus->drivers = dlist_new_with_delete
183 (sizeof(struct sysfs_driver),
185 dlist_unshift_sorted(bus->drivers, driver, sort_list);
188 sysfs_close_directory(drvdir);
189 return (bus->drivers);
193 * sysfs_open_bus: opens specific bus and all its devices on system
194 * returns sysfs_bus structure with success or NULL with error.
196 struct sysfs_bus *sysfs_open_bus(const char *name)
198 struct sysfs_bus *bus = NULL;
199 char buspath[SYSFS_PATH_MAX];
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");
212 safestrcat(buspath, "/");
213 safestrcat(buspath, SYSFS_BUS_NAME);
214 safestrcat(buspath, "/");
215 safestrcat(buspath, name);
216 if ((sysfs_path_is_dir(buspath)) != 0) {
217 dprintf("Invalid path to bus: %s\n", buspath);
222 dprintf("calloc failed\n");
225 safestrcpy(bus->name, name);
226 safestrcpy(bus->path, buspath);
227 if ((sysfs_remove_trailing_slash(bus->path)) != 0) {
228 dprintf("Incorrect path to bus %s\n", bus->path);
229 sysfs_close_bus(bus);
237 * sysfs_get_bus_device: Get specific device on bus using device's id
238 * @bus: bus to find device on
239 * @id: bus_id for device
240 * returns struct sysfs_device reference or NULL if not found.
242 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, char *id)
244 if (bus == NULL || id == NULL) {
249 if (bus->devices == NULL) {
250 bus->devices = sysfs_get_bus_devices(bus);
251 if (bus->devices == NULL)
255 return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
256 bus_device_id_equal);
260 * sysfs_get_bus_driver: Get specific driver on bus using driver name
261 * @bus: bus to find driver on
262 * @drvname: name of driver
263 * returns struct sysfs_driver reference or NULL if not found.
265 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
268 if (bus == NULL || drvname == NULL) {
273 if (bus->drivers == NULL) {
274 bus->drivers = sysfs_get_bus_drivers(bus);
275 if (bus->drivers == NULL)
279 return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
280 bus_driver_name_equal);
284 * sysfs_get_bus_attributes: returns bus' dlist of attributes
285 * @bus: bus to get attributes for.
286 * returns dlist of attributes or NULL if there aren't any.
288 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
293 if (bus->directory == NULL) {
294 bus->directory = sysfs_open_directory(bus->path);
295 if (bus->directory == NULL)
298 if (bus->directory->attributes == NULL) {
299 if ((sysfs_read_dir_attributes(bus->directory)) != 0)
302 return bus->directory->attributes;
306 * sysfs_refresh_bus_attributes: refreshes the bus's list of attributes
307 * @bus: sysfs_bus whose attributes to refresh
309 * NOTE: Upon return, prior references to sysfs_attributes for this bus
312 * Returns list of attributes on success and NULL on failure
314 struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus)
321 if (bus->directory == NULL)
322 return (sysfs_get_bus_attributes(bus));
324 if ((sysfs_refresh_dir_attributes(bus->directory)) != 0) {
325 dprintf("Error refreshing bus attributes\n");
329 return (bus->directory->attributes);
333 * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
335 * @bus: bus to retrieve attribute from
336 * @attrname: attribute name to retrieve
337 * returns reference to sysfs_attribute if found or NULL if not found
339 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
342 struct dlist *attrlist = NULL;
348 attrlist = sysfs_get_bus_attributes(bus);
349 if (attrlist == NULL)
352 return sysfs_get_directory_attribute(bus->directory, attrname);
356 * sysfs_find_driver_bus: locates the bus the driver is on.
357 * @driver: name of the driver to locate
358 * @busname: buffer to copy name to
359 * @bsize: buffer size
360 * returns 0 with success, -1 with error
362 int sysfs_find_driver_bus(const char *driver, char *busname, size_t bsize)
364 char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
365 struct dlist *buslist = NULL, *drivers = NULL;
367 if (driver == NULL || busname == NULL) {
372 memset(subsys, 0, SYSFS_PATH_MAX);
373 safestrcpy(subsys, SYSFS_BUS_NAME);
374 buslist = sysfs_open_subsystem_list(subsys);
375 if (buslist != NULL) {
376 dlist_for_each_data(buslist, bus, char) {
377 memset(subsys, 0, SYSFS_PATH_MAX);
378 safestrcpy(subsys, SYSFS_BUS_NAME);
379 safestrcat(subsys, "/");
380 safestrcat(subsys, bus);
381 safestrcat(subsys, "/");
382 safestrcat(subsys, SYSFS_DRIVERS_NAME);
383 drivers = sysfs_open_subsystem_list(subsys);
384 if (drivers != NULL) {
385 dlist_for_each_data(drivers, curdrv, char) {
386 if (strcmp(driver, curdrv) == 0) {
387 safestrcpymax(busname,
389 sysfs_close_list(drivers);
390 sysfs_close_list(buslist);
394 sysfs_close_list(drivers);
397 sysfs_close_list(buslist);