4 * Generic device utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003-2005
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
27 * get_dev_driver: fills in the dev->driver_name field
28 * Returns 0 on SUCCESS and -1 on error
30 static int get_dev_driver(struct sysfs_device *dev)
32 char path[SYSFS_PATH_MAX];
33 char devpath[SYSFS_PATH_MAX];
39 memset(path, 0, SYSFS_PATH_MAX);
40 memset(devpath, 0, SYSFS_PATH_MAX);
41 safestrcpymax(path, dev->path, SYSFS_PATH_MAX);
42 safestrcatmax(path, "/driver", SYSFS_PATH_MAX);
43 if (!sysfs_path_is_link(path)) {
44 if (!sysfs_get_link(path, devpath, SYSFS_PATH_MAX)) {
45 if (sysfs_get_name_from_path(devpath,
46 dev->driver_name, SYSFS_NAME_LEN))
55 * sysfs_get_device_bus: retrieves the bus name the device is on, checks path
56 * to bus' link to make sure it has correct device.
57 * @dev: device to get busname.
58 * returns 0 with success and -1 with error.
60 int sysfs_get_device_bus(struct sysfs_device *dev)
62 char devpath[SYSFS_PATH_MAX];
63 char path[SYSFS_PATH_MAX];
70 memset(path, 0, SYSFS_PATH_MAX);
71 memset(devpath, 0, SYSFS_PATH_MAX);
72 safestrcpymax(path, dev->path, SYSFS_PATH_MAX);
73 safestrcatmax(path, "/bus", SYSFS_PATH_MAX);
74 if (!sysfs_path_is_link(path)) {
75 if (!sysfs_get_link(path, devpath, SYSFS_PATH_MAX)) {
76 if (sysfs_get_name_from_path(devpath,
77 dev->bus, SYSFS_NAME_LEN))
86 * sysfs_close_device_tree: closes every device in the supplied tree,
87 * closing children only.
88 * @devroot: device root of tree.
90 void sysfs_close_device_tree(struct sysfs_device *devroot)
93 if (devroot->children) {
94 struct sysfs_device *child = NULL;
96 dlist_for_each_data(devroot->children, child,
97 struct sysfs_device) {
98 sysfs_close_device_tree(child);
101 sysfs_close_device(devroot);
106 * sysfs_close_device: closes and cleans up a device
107 * @dev = device to clean up
109 void sysfs_close_device(struct sysfs_device *dev)
113 sysfs_close_device(dev->parent);
114 if (dev->children && dev->children->count)
115 dlist_destroy(dev->children);
117 dlist_destroy(dev->attrlist);
123 * alloc_device: allocates and initializes device structure
124 * returns struct sysfs_device
126 static struct sysfs_device *alloc_device(void)
128 return (struct sysfs_device *) calloc(1, sizeof(struct sysfs_device));
132 * sysfs_open_device_path: opens and populates device structure
133 * @path: path to device, this is the /sys/devices/ path
134 * returns sysfs_device structure with success or NULL with error
136 struct sysfs_device *sysfs_open_device_path(const char *path)
138 struct sysfs_device *dev;
144 if (sysfs_path_is_dir(path)) {
145 dprintf("Incorrect path to device: %s\n", path);
148 dev = alloc_device();
150 dprintf("Error allocating device at %s\n", path);
153 if (sysfs_get_name_from_path(path, dev->bus_id, SYSFS_NAME_LEN)) {
155 dprintf("Error getting device bus_id\n");
156 sysfs_close_device(dev);
159 safestrcpy(dev->path, path);
160 if (sysfs_remove_trailing_slash(dev->path)) {
161 dprintf("Invalid path to device %s\n", dev->path);
162 sysfs_close_device(dev);
166 * The "name" attribute no longer exists... return the device's
167 * sysfs representation instead, in the "dev->name" field, which
168 * implies that the dev->name and dev->bus_id contain same data.
170 safestrcpy(dev->name, dev->bus_id);
172 if (sysfs_get_device_bus(dev))
173 dprintf("Could not get device bus\n");
175 if (get_dev_driver(dev)) {
176 dprintf("Could not get device %s's driver\n", dev->bus_id);
177 safestrcpy(dev->driver_name, SYSFS_UNKNOWN);
184 * sysfs_get_device_attr: searches dev's attributes by name
185 * @dev: device to look through
186 * @name: attribute name to get
187 * returns sysfs_attribute reference with success or NULL with error.
189 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
196 return get_attribute(dev, (char *)name);
200 * sysfs_get_device_attributes: gets list of device attributes
201 * @dev: device whose attributes list is needed
202 * returns dlist of attributes on success or NULL on error
204 struct dlist *sysfs_get_device_attributes(struct sysfs_device *dev)
210 return get_attributes_list(dev);
214 * get_device_absolute_path: looks up the bus the device is on, gets
215 * absolute path to the device
216 * @device: device for which path is needed
217 * @path: buffer to store absolute path
218 * @psize: size of "path"
219 * Returns 0 on success -1 on failure
221 static int get_device_absolute_path(const char *device, const char *bus,
222 char *path, size_t psize)
224 char bus_path[SYSFS_PATH_MAX];
226 if (!device || !path) {
231 memset(bus_path, 0, SYSFS_PATH_MAX);
232 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX)) {
233 dprintf ("Sysfs not supported on this system\n");
236 safestrcat(bus_path, "/");
237 safestrcat(bus_path, SYSFS_BUS_NAME);
238 safestrcat(bus_path, "/");
239 safestrcat(bus_path, bus);
240 safestrcat(bus_path, "/");
241 safestrcat(bus_path, SYSFS_DEVICES_NAME);
242 safestrcat(bus_path, "/");
243 safestrcat(bus_path, device);
245 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
246 * Now read this link to reach to the device.
248 if (sysfs_get_link(bus_path, path, psize)) {
249 dprintf("Error getting to device %s\n", device);
256 * sysfs_open_device: open a device by id (use the "bus" subsystem)
257 * @bus: bus the device belongs to
258 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
259 * /sys/bus/xxx/devices
260 * returns struct sysfs_device if found, NULL otherwise
262 * 1. Use sysfs_close_device to close the device
263 * 2. Bus the device is on must be supplied
264 * Use sysfs_find_device_bus to get the bus name
266 struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
268 char sysfs_path[SYSFS_PATH_MAX];
269 struct sysfs_device *device;
271 if (!bus_id || !bus) {
275 memset(sysfs_path, 0, SYSFS_PATH_MAX);
276 if (get_device_absolute_path(bus_id, bus, sysfs_path,
278 dprintf("Error getting to device %s\n", bus_id);
282 device = sysfs_open_device_path(sysfs_path);
284 dprintf("Error opening device %s\n", bus_id);
291 * sysfs_get_device_parent: opens up given device's parent and returns a
292 * reference to its sysfs_device
293 * @dev: sysfs_device whose parent is requested
294 * Returns sysfs_device of the parent on success and NULL on failure
296 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
298 char ppath[SYSFS_PATH_MAX], dpath[SYSFS_PATH_MAX], *tmp;
306 return (dev->parent);
308 memset(ppath, 0, SYSFS_PATH_MAX);
309 memset(dpath, 0, SYSFS_PATH_MAX);
310 safestrcpy(ppath, dev->path);
311 tmp = strrchr(ppath, '/');
313 dprintf("Invalid path to device %s\n", ppath);
316 if (*(tmp + 1) == '\0') {
318 tmp = strrchr(tmp, '/');
320 dprintf("Invalid path to device %s\n", ppath);
326 /* Make sure we're not at the top of the device tree */
327 sysfs_get_mnt_path(dpath, SYSFS_PATH_MAX);
328 safestrcat(dpath, "/" SYSFS_DEVICES_NAME);
329 if (strcmp(dpath, ppath) == 0) {
330 dprintf("Device at %s does not have a parent\n", dev->path);
334 dev->parent = sysfs_open_device_path(ppath);
336 dprintf("Error opening device %s's parent at %s\n",
340 return (dev->parent);