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 * open_bus_dir: opens up sysfs bus directory
97 * returns sysfs_directory struct with success and NULL with error
99 static struct sysfs_directory *open_bus_dir(const unsigned char *name)
101 struct sysfs_directory *busdir = NULL;
102 unsigned char buspath[SYSFS_PATH_MAX];
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");
115 if (sysfs_trailing_slash(buspath) == 0)
116 strcat(buspath, "/");
118 strcat(buspath, SYSFS_BUS_NAME);
119 strcat(buspath, "/");
120 strcat(buspath, name);
121 busdir = sysfs_open_directory(buspath);
122 if (busdir == NULL) {
124 dprintf("Bus %s not supported on this system\n",
128 if ((sysfs_read_directory(busdir)) != 0) {
129 dprintf("Error reading %s bus dir %s\n", name,
131 sysfs_close_directory(busdir);
134 /* read in devices and drivers subdirs */
135 sysfs_read_all_subdirs(busdir);
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
145 static int get_all_bus_devices(struct sysfs_bus *bus)
147 struct sysfs_device *bdev = NULL;
148 struct sysfs_directory *cur = NULL;
149 struct sysfs_link *curl = NULL;
151 if (bus == NULL || bus->directory == NULL) {
155 if (bus->directory->subdirs == NULL)
158 dlist_for_each_data(bus->directory->subdirs, cur,
159 struct sysfs_directory) {
160 if (strcmp(cur->name, SYSFS_DEVICES_NAME) != 0)
162 if (cur->links == NULL)
164 dlist_for_each_data(cur->links, curl, struct sysfs_link) {
165 bdev = sysfs_open_device(curl->target);
167 dprintf("Error opening device at %s\n",
171 if (bus->devices == NULL)
172 bus->devices = dlist_new_with_delete
173 (sizeof(struct sysfs_device),
175 dlist_unshift(bus->devices, bdev);
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
187 static int get_all_bus_drivers(struct sysfs_bus *bus)
189 struct sysfs_driver *driver = NULL;
190 struct sysfs_directory *cur = NULL;
191 struct sysfs_directory *cursub = NULL;
193 if (bus == NULL || bus->directory == NULL) {
197 if (bus->directory->subdirs == NULL)
200 dlist_for_each_data(bus->directory->subdirs, cur,
201 struct sysfs_directory) {
202 if (strcmp(cur->name, SYSFS_DRIVERS_NAME) != 0)
204 if (cur->subdirs == NULL)
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",
214 if (bus->drivers == NULL)
215 bus->drivers = dlist_new_with_delete
216 (sizeof(struct sysfs_driver),
218 dlist_unshift(bus->drivers, driver);
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
231 static int match_bus_device_to_driver(struct sysfs_driver *driver,
232 unsigned char *busid)
234 struct sysfs_link *cur = NULL;
237 if (driver == NULL || driver->directory == NULL || busid == NULL) {
241 if (driver->directory->links != NULL) {
242 dlist_for_each_data(driver->directory->links, cur,
244 if ((strcmp(cur->name, busid)) == 0)
252 * link_bus_devices_to_drivers: goes through and links devices to drivers
255 static void link_bus_devices_to_drivers(struct sysfs_bus *bus)
257 struct sysfs_device *dev = NULL;
258 struct sysfs_driver *drv = NULL;
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,
268 if (drv->devices == NULL)
269 drv->devices = dlist_new
272 dlist_unshift(drv->devices, dev);
280 * sysfs_open_bus: opens specific bus and all its devices on system
281 * returns sysfs_bus structure with success or NULL with error.
283 struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
285 struct sysfs_bus *bus = NULL;
286 struct sysfs_directory *busdir = NULL;
295 dprintf("calloc failed\n");
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",
303 sysfs_close_bus(bus);
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);
313 if ((get_all_bus_drivers(bus)) != 0) {
314 dprintf("Error reading %s bus drivers\n", name);
315 sysfs_close_bus(bus);
318 link_bus_devices_to_drivers(bus);
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.
329 struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
332 if (bus == NULL || id == NULL) {
337 return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
338 bus_device_id_equal);
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.
347 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
348 unsigned char *drvname)
350 if (bus == NULL || drvname == NULL) {
355 return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
356 bus_driver_name_equal);
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.
364 struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
366 if (bus == NULL || bus->directory == NULL)
368 return bus->directory->attributes;
372 * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
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
378 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
379 unsigned char *attrname)
381 if (bus == NULL || bus->directory == NULL || attrname == NULL) {
385 return sysfs_get_directory_attribute(bus->directory, attrname);
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.
395 struct sysfs_device *sysfs_open_bus_device(unsigned char *busname,
396 unsigned char *dev_id)
398 struct sysfs_device *rdev = NULL;
399 char path[SYSFS_PATH_MAX];
401 if (busname == NULL || dev_id == NULL) {
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");
412 if (sysfs_trailing_slash(path) == 0)
414 strcat(path, SYSFS_BUS_NAME);
416 strcat(path, busname);
418 strcat(path, SYSFS_DEVICES_NAME);
420 strcat(path, dev_id);
422 rdev = sysfs_open_device(path);
424 dprintf("Error getting device %s on bus %s\n",
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
439 int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
442 unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
443 struct dlist *buslist = NULL, *drivers = NULL;
445 if (driver == NULL || busname == NULL) {
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);
457 strcpy(subsys, SYSFS_BUS_NAME);
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);
472 sysfs_close_list(drivers);
475 sysfs_close_list(buslist);