4 * Generic device 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
27 * get_device_bus: retrieves the bus name the device is on, checks path to
28 * bus' link to make sure it has correct device.
29 * @dev: device to get busname.
30 * returns 0 with success and -1 with error.
32 static int get_device_bus(struct sysfs_device *dev)
34 unsigned char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
35 unsigned char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
36 struct dlist *buslist = NULL;
43 memset(subsys, 0, SYSFS_NAME_LEN);
45 strcpy(subsys, SYSFS_BUS_NAME); /* subsys = /bus */
46 buslist = sysfs_open_subsystem_list(subsys);
47 if (buslist != NULL) {
48 dlist_for_each_data(buslist, bus, char) {
49 memset(path, 0, SYSFS_PATH_MAX);
50 strcpy(path, dev->path);
51 c = strstr(path, "/devices");
53 dprintf("Invalid path to device %s\n", path);
54 sysfs_close_list(buslist);
59 strcat(path, SYSFS_BUS_NAME);
63 strcat(path, SYSFS_DEVICES_NAME);
65 strcat(path, dev->bus_id);
66 if ((sysfs_path_is_link(path)) == 0) {
67 memset(target, 0, SYSFS_PATH_MAX);
68 if ((sysfs_get_link(path, target,
69 SYSFS_PATH_MAX)) != 0) {
70 dprintf("Error getting link target\n");
71 sysfs_close_list(buslist);
74 if (!(strncmp(target, dev->path,
76 strcpy(dev->bus, bus);
77 sysfs_close_list(buslist);
82 sysfs_close_list(buslist);
88 * sysfs_close_device_tree: closes every device in the supplied tree,
89 * closing children only.
90 * @devroot: device root of tree.
92 static void sysfs_close_device_tree(struct sysfs_device *devroot)
94 if (devroot != NULL) {
95 if (devroot->children != NULL) {
96 struct sysfs_device *child = NULL;
98 dlist_for_each_data(devroot->children, child,
99 struct sysfs_device) {
100 sysfs_close_device_tree(child);
103 sysfs_close_device(devroot);
108 * sysfs_close_dev_tree: routine for dlist integration
110 static void sysfs_close_dev_tree(void *dev)
112 sysfs_close_device_tree((struct sysfs_device *)dev);
116 * sysfs_close_device: closes and cleans up a device
117 * @dev = device to clean up
119 void sysfs_close_device(struct sysfs_device *dev)
122 if (dev->parent != NULL)
123 sysfs_close_device(dev->parent);
124 if (dev->directory != NULL)
125 sysfs_close_directory(dev->directory);
126 if (dev->children != NULL && dev->children->count == 0)
127 dlist_destroy(dev->children);
133 * alloc_device: allocates and initializes device structure
134 * returns struct sysfs_device
136 static struct sysfs_device *alloc_device(void)
138 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
142 * open_device_dir: opens up sysfs_directory for specific root dev
143 * @name: name of root
144 * returns struct sysfs_directory with success and NULL with error
146 static struct sysfs_directory *open_device_dir(const unsigned char *path)
148 struct sysfs_directory *rdir = NULL;
155 rdir = sysfs_open_directory(path);
158 dprintf ("Device %s not supported on this system\n", path);
161 if ((sysfs_read_dir_subdirs(rdir)) != 0) {
162 dprintf ("Error reading device at dir %s\n", path);
163 sysfs_close_directory(rdir);
171 * sysfs_open_device_path: opens and populates device structure
172 * @path: path to device, this is the /sys/devices/ path
173 * returns sysfs_device structure with success or NULL with error
175 struct sysfs_device *sysfs_open_device_path(const unsigned char *path)
177 struct sysfs_device *dev = NULL;
183 if ((sysfs_path_is_dir(path)) != 0) {
184 dprintf("Incorrect path to device: %s\n", path);
187 dev = alloc_device();
189 dprintf("Error allocating device at %s\n", path);
192 if ((sysfs_get_name_from_path(path, dev->bus_id,
193 SYSFS_NAME_LEN)) != 0) {
195 dprintf("Error getting device bus_id\n");
196 sysfs_close_device(dev);
199 strcpy(dev->path, path);
201 * The "name" attribute no longer exists... return the device's
202 * sysfs representation instead, in the "dev->name" field, which
203 * implies that the dev->name and dev->bus_id contain same data.
205 strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN);
207 if (get_device_bus(dev) != 0)
208 strcpy(dev->bus, SYSFS_UNKNOWN);
214 * sysfs_open_device_tree: opens root device and all of its children,
215 * creating a tree of devices. Only opens children.
216 * @path: sysfs path to devices
217 * returns struct sysfs_device and its children with success or NULL with
220 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
222 struct sysfs_device *rootdev = NULL, *new = NULL;
223 struct sysfs_directory *cur = NULL;
229 rootdev = sysfs_open_device_path(path);
230 if (rootdev == NULL) {
231 dprintf("Error opening root device at %s\n", path);
234 if (rootdev->directory == NULL) {
235 rootdev->directory = open_device_dir(rootdev->path);
236 if (rootdev->directory == NULL)
239 if (rootdev->directory->subdirs != NULL) {
240 dlist_for_each_data(rootdev->directory->subdirs, cur,
241 struct sysfs_directory) {
242 new = sysfs_open_device_tree(cur->path);
244 dprintf("Error opening device tree at %s\n",
246 sysfs_close_device_tree(rootdev);
249 if (rootdev->children == NULL)
250 rootdev->children = dlist_new_with_delete
251 (sizeof(struct sysfs_device),
252 sysfs_close_dev_tree);
253 dlist_unshift(rootdev->children, new);
261 * sysfs_close_root_device: closes root and all devices
262 * @root: root device to close
264 void sysfs_close_root_device(struct sysfs_root_device *root)
267 if (root->devices != NULL)
268 dlist_destroy(root->devices);
269 if (root->directory != NULL)
270 sysfs_close_directory(root->directory);
276 * sysfs_get_root_devices: opens up all the devices under this root device
277 * @root: root device to open devices for
278 * returns dlist of devices with success and NULL with error
280 struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
282 struct sysfs_device *dev = NULL;
283 struct sysfs_directory *cur = NULL;
289 if (root->directory == NULL) {
290 root->directory = open_device_dir(root->path);
291 if (root->directory == NULL)
295 if (root->directory->subdirs == NULL)
298 dlist_for_each_data(root->directory->subdirs, cur,
299 struct sysfs_directory) {
300 dev = sysfs_open_device_tree(cur->path);
302 dprintf ("Error opening device at %s\n", cur->path);
305 if (root->devices == NULL)
306 root->devices = dlist_new_with_delete
307 (sizeof(struct sysfs_device),
308 sysfs_close_dev_tree);
309 dlist_unshift(root->devices, dev);
312 return root->devices;
316 * sysfs_open_root_device: opens sysfs devices root and all of its
318 * @name: name of /sys/devices/root to open
319 * returns struct sysfs_root_device if success and NULL with error
321 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
323 struct sysfs_root_device *root = NULL;
324 unsigned char rootpath[SYSFS_PATH_MAX];
331 memset(rootpath, 0, SYSFS_PATH_MAX);
332 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
333 dprintf ("Sysfs not supported on this system\n");
337 if (sysfs_trailing_slash(rootpath) == 0)
338 strcat(rootpath, "/");
339 strcat(rootpath, SYSFS_DEVICES_NAME);
340 strcat(rootpath, "/");
341 strcat(rootpath, name);
342 if ((sysfs_path_is_dir(rootpath)) != 0) {
344 dprintf("Invalid root device: %s\n", name);
347 root = (struct sysfs_root_device *)calloc
348 (1, sizeof(struct sysfs_root_device));
350 dprintf("calloc failure\n");
353 strcpy(root->name, name);
354 strcpy(root->path, rootpath);
359 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
360 * the specific device
361 * @device: struct sysfs_device * for which attributes are to be returned
363 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
368 if (device->directory == NULL) {
369 device->directory = sysfs_open_directory(device->path);
370 if (device->directory == NULL)
373 if (device->directory->attributes == NULL) {
374 if ((sysfs_read_dir_attributes(device->directory)) != 0)
377 if ((sysfs_path_is_dir(device->path)) != 0) {
378 dprintf("Device at %s no longer exists", device->path);
381 if ((sysfs_refresh_attributes
382 (device->directory->attributes)) != 0) {
383 dprintf("Error refreshing device attributes\n");
387 return (device->directory->attributes);
391 * sysfs_get_device_attr: searches dev's attributes by name
392 * @dev: device to look through
393 * @name: attribute name to get
394 * returns sysfs_attribute reference with success or NULL with error.
396 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
397 const unsigned char *name)
399 struct sysfs_attribute *cur = NULL;
400 struct dlist *attrlist = NULL;
402 if (dev == NULL || name == NULL) {
407 attrlist = sysfs_get_device_attributes(dev);
408 if (attrlist == NULL)
411 cur = sysfs_get_directory_attribute(dev->directory,
412 (unsigned char *)name);
418 * get_device_absolute_path: looks up the bus the device is on, gets
419 * absolute path to the device
420 * @device: device for which path is needed
421 * @path: buffer to store absolute path
422 * @psize: size of "path"
423 * Returns 0 on success -1 on failure
425 static int get_device_absolute_path(const unsigned char *device,
426 const unsigned char *bus, unsigned char *path, size_t psize)
428 unsigned char bus_path[SYSFS_PATH_MAX];
430 if (device == NULL || path == NULL) {
435 memset(bus_path, 0, SYSFS_PATH_MAX);
436 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
437 dprintf ("Sysfs not supported on this system\n");
440 if (sysfs_trailing_slash(bus_path) == 0)
441 strcat(bus_path, "/");
442 strcat(bus_path, SYSFS_BUS_NAME);
443 strcat(bus_path, "/");
444 strcat(bus_path, bus);
445 strcat(bus_path, "/");
446 strcat(bus_path, SYSFS_DEVICES_NAME);
447 strcat(bus_path, "/");
448 strcat(bus_path, device);
450 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
451 * Now read this link to reach to the device.
453 if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
454 dprintf("Error getting to device %s\n", device);
461 * sysfs_open_device: open a device by id (use the "bus" subsystem)
462 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
463 * /sys/bus/xxx/devices
464 * @bus: bus the device belongs to
465 * returns struct sysfs_device if found, NULL otherwise
467 * 1. Use sysfs_close_device to close the device
468 * 2. Bus the device is on must be supplied
469 * Use sysfs_find_device_bus to get the bus name
471 struct sysfs_device *sysfs_open_device(const unsigned char *bus_id,
472 const unsigned char *bus)
474 char sysfs_path[SYSFS_PATH_MAX];
475 struct sysfs_device *device = NULL;
477 if (bus_id == NULL || bus == NULL) {
481 memset(sysfs_path, 0, SYSFS_PATH_MAX);
482 if ((get_device_absolute_path(bus_id, bus, sysfs_path,
483 SYSFS_PATH_MAX)) != 0) {
484 dprintf("Error getting to device %s\n", bus_id);
488 device = sysfs_open_device_path(sysfs_path);
489 if (device == NULL) {
490 dprintf("Error opening device %s\n", bus_id);
498 * sysfs_get_device_parent: opens up given device's parent and returns a
499 * reference to its sysfs_device
500 * @dev: sysfs_device whose parent is requested
501 * Returns sysfs_device of the parent on success and NULL on failure
503 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
505 unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL;
512 if (dev->parent != NULL)
513 return (dev->parent);
515 memset(ppath, 0, SYSFS_PATH_MAX);
516 strcpy(ppath, dev->path);
517 tmp = strrchr(ppath, '/');
519 dprintf("Invalid path to device %s\n", ppath);
522 if (*(tmp +1) == '\0') {
524 tmp = strrchr(tmp, '/');
526 dprintf("Invalid path to device %s\n", ppath);
533 * All "devices" have the "detach_state" attribute - validate here
535 strcat(ppath, "/detach_state");
536 if ((sysfs_path_is_file(ppath)) != 0) {
537 dprintf("Device at %s does not have a parent\n", dev->path);
540 tmp = strrchr(ppath, '/');
542 dev->parent = sysfs_open_device_path(ppath);
543 if (dev->parent == NULL) {
544 dprintf("Error opening device %s's parent at %s\n",
548 return (dev->parent);
552 * sysfs_open_device_attr: open the given device's attribute
553 * @bus: Bus on which to look
554 * @dev_id: device for which attribute is required
555 * @attrname: name of the attribute to look for
556 * Returns struct sysfs_attribute on success and NULL on failure
559 * A call to sysfs_close_attribute() is required to close
560 * the attribute returned and free memory.
562 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
563 const unsigned char *bus_id, const unsigned char *attrib)
565 struct sysfs_attribute *attribute = NULL;
566 unsigned char devpath[SYSFS_PATH_MAX];
568 if (bus == NULL || bus_id == NULL || attrib == NULL) {
573 memset(devpath, 0, SYSFS_PATH_MAX);
574 if ((get_device_absolute_path(bus_id, bus, devpath,
575 SYSFS_PATH_MAX)) != 0) {
576 dprintf("Error getting to device %s\n", bus_id);
579 strcat(devpath, "/");
580 strcat(devpath, attrib);
581 attribute = sysfs_open_attribute(devpath);
582 if (attribute == NULL) {
583 dprintf("Error opening attribute %s for device %s\n",
587 if ((sysfs_read_attribute(attribute)) != 0) {
588 dprintf("Error reading attribute %s for device %s\n",
590 sysfs_close_attribute(attribute);