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
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(((unsigned 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(((unsigned 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 unsigned char path[SYSFS_PATH_MAX];
111 memset(path, 0, SYSFS_PATH_MAX);
112 strcpy(path, bus->path);
114 strcat(path, SYSFS_DEVICES_NAME);
115 devdir = sysfs_open_directory(path);
119 if (sysfs_read_dir_links(devdir) != 0) {
120 sysfs_close_directory(devdir);
124 dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
125 bdev = sysfs_open_device(curl->target);
127 dprintf("Error opening device at %s\n", curl->target);
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);
135 sysfs_close_directory(devdir);
137 return (bus->devices);
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
145 struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
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];
156 memset(path, 0, SYSFS_PATH_MAX);
157 strcpy(path, bus->path);
159 strcat(path, SYSFS_DRIVERS_NAME);
160 drvdir = sysfs_open_directory(path);
164 if (sysfs_read_dir_subdirs(drvdir) != 0) {
165 sysfs_close_directory(drvdir);
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);
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);
179 sysfs_close_directory(drvdir);
180 return (bus->drivers);
184 * sysfs_open_bus: opens specific bus and all its devices on system
185 * returns sysfs_bus structure with success or NULL with error.
187 struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
189 struct sysfs_bus *bus = NULL;
190 unsigned char buspath[SYSFS_PATH_MAX];
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");
203 if (sysfs_trailing_slash(buspath) == 0)
204 strcat(buspath, "/");
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);
215 dprintf("calloc failed\n");
218 strcpy(bus->name, name);
219 strcpy(bus->path, buspath);
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.
230 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
233 if (bus == NULL || id == NULL) {
238 if (bus->devices == NULL) {
239 bus->devices = sysfs_get_bus_devices(bus);
240 if (bus->devices == NULL)
244 return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
245 bus_device_id_equal);
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.
254 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
255 unsigned char *drvname)
257 if (bus == NULL || drvname == NULL) {
262 if (bus->drivers == NULL) {
263 bus->drivers = sysfs_get_bus_drivers(bus);
264 if (bus->drivers == NULL)
268 return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
269 bus_driver_name_equal);
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.
277 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
282 if (bus->directory == NULL) {
283 bus->directory = sysfs_open_directory(bus->path);
284 if (bus->directory == NULL)
287 if (bus->directory->attributes == NULL) {
288 if ((sysfs_read_dir_attributes(bus->directory)) != 0)
291 if ((sysfs_path_is_dir(bus->path)) != 0) {
292 dprintf("Bus at %s no longer exists\n", bus->path);
295 if ((sysfs_refresh_attributes
296 (bus->directory->attributes)) != 0) {
297 dprintf("Error refreshing bus attributes\n");
301 return bus->directory->attributes;
305 * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
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
311 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
312 unsigned char *attrname)
314 struct dlist *attrlist = NULL;
320 attrlist = sysfs_get_bus_attributes(bus);
321 if (attrlist == NULL)
324 return sysfs_get_directory_attribute(bus->directory, attrname);
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.
334 struct sysfs_device *sysfs_open_bus_device(unsigned char *busname,
335 unsigned char *dev_id)
337 struct sysfs_device *rdev = NULL;
338 char path[SYSFS_PATH_MAX];
340 if (busname == NULL || dev_id == NULL) {
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");
351 if (sysfs_trailing_slash(path) == 0)
354 strcat(path, SYSFS_BUS_NAME);
356 strcat(path, busname);
358 strcat(path, SYSFS_DEVICES_NAME);
360 strcat(path, dev_id);
362 rdev = sysfs_open_device(path);
364 dprintf("Error getting device %s on bus %s\n",
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
379 int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
382 unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
383 struct dlist *buslist = NULL, *drivers = NULL;
385 if (driver == NULL || busname == NULL) {
390 memset(subsys, 0, SYSFS_PATH_MAX);
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);
398 strcpy(subsys, SYSFS_BUS_NAME);
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);
413 sysfs_close_list(drivers);
416 sysfs_close_list(buslist);