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_del_device: routine for dlist integration
110 static void sysfs_del_device(void *dev)
112 sysfs_close_device((struct sysfs_device *)dev);
116 * sysfs_close_dev_tree: routine for dlist integration
118 static void sysfs_close_dev_tree(void *dev)
120 sysfs_close_device_tree((struct sysfs_device *)dev);
124 * sysfs_close_device: closes and cleans up a device
125 * @dev = device to clean up
127 void sysfs_close_device(struct sysfs_device *dev)
130 if (dev->directory != NULL)
131 sysfs_close_directory(dev->directory);
132 if (dev->children != NULL && dev->children->count == 0)
133 dlist_destroy(dev->children);
139 * alloc_device: allocates and initializes device structure
140 * returns struct sysfs_device
142 static struct sysfs_device *alloc_device(void)
144 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
148 * open_device_dir: opens up sysfs_directory for specific root dev
149 * @name: name of root
150 * returns struct sysfs_directory with success and NULL with error
152 static struct sysfs_directory *open_device_dir(const unsigned char *path)
154 struct sysfs_directory *rdir = NULL;
161 rdir = sysfs_open_directory(path);
164 dprintf ("Device %s not supported on this system\n", path);
167 if ((sysfs_read_directory(rdir)) != 0) {
168 dprintf ("Error reading device at dir %s\n", path);
169 sysfs_close_directory(rdir);
177 * sysfs_open_device: opens and populates device structure
178 * @path: path to device, this is the /sys/devices/ path
179 * returns sysfs_device structure with success or NULL with error
181 struct sysfs_device *sysfs_open_device(const unsigned char *path)
183 struct sysfs_device *dev = NULL;
189 if ((sysfs_path_is_dir(path)) != 0) {
190 dprintf("Incorrect path to device: %s\n", path);
193 dev = alloc_device();
195 dprintf("Error allocating device at %s\n", path);
198 if ((sysfs_get_name_from_path(path, dev->bus_id,
199 SYSFS_NAME_LEN)) != 0) {
201 dprintf("Error getting device bus_id\n");
202 sysfs_close_device(dev);
205 strcpy(dev->path, path);
207 * The "name" attribute no longer exists... return the device's
208 * sysfs representation instead, in the "dev->name" field, which
209 * implies that the dev->name and dev->bus_id contain same data.
211 strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN);
213 if (get_device_bus(dev) != 0)
214 strcpy(dev->bus, SYSFS_UNKNOWN);
220 * sysfs_open_device_tree: opens root device and all of its children,
221 * creating a tree of devices. Only opens children.
222 * @path: sysfs path to devices
223 * returns struct sysfs_device and its children with success or NULL with
226 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
228 struct sysfs_device *rootdev = NULL, *new = NULL;
229 struct sysfs_directory *cur = NULL;
235 rootdev = sysfs_open_device(path);
236 if (rootdev == NULL) {
237 dprintf("Error opening root device at %s\n", path);
240 if (rootdev->directory == NULL) {
241 rootdev->directory = open_device_dir(rootdev->path);
242 if (rootdev->directory == NULL)
245 if (rootdev->directory->subdirs != NULL) {
246 dlist_for_each_data(rootdev->directory->subdirs, cur,
247 struct sysfs_directory) {
248 new = sysfs_open_device_tree(cur->path);
250 dprintf("Error opening device tree at %s\n",
252 sysfs_close_device_tree(rootdev);
255 if (rootdev->children == NULL)
256 rootdev->children = dlist_new_with_delete
257 (sizeof(struct sysfs_device),
259 dlist_unshift(rootdev->children, new);
267 * sysfs_close_root_device: closes root and all devices
268 * @root: root device to close
270 void sysfs_close_root_device(struct sysfs_root_device *root)
273 if (root->devices != NULL)
274 dlist_destroy(root->devices);
275 if (root->directory != NULL)
276 sysfs_close_directory(root->directory);
282 * sysfs_get_root_devices: opens up all the devices under this root device
283 * @root: root device to open devices for
284 * returns dlist of devices with success and NULL with error
286 struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
288 struct sysfs_device *dev = NULL;
289 struct sysfs_directory *cur = NULL;
295 if (root->directory == NULL) {
296 root->directory = open_device_dir(root->path);
297 if (root->directory == NULL)
301 if (root->directory->subdirs == NULL)
304 dlist_for_each_data(root->directory->subdirs, cur,
305 struct sysfs_directory) {
306 dev = sysfs_open_device_tree(cur->path);
308 dprintf ("Error opening device at %s\n", cur->path);
311 if (root->devices == NULL)
312 root->devices = dlist_new_with_delete
313 (sizeof(struct sysfs_device),
314 sysfs_close_dev_tree);
315 dlist_unshift(root->devices, dev);
318 return root->devices;
322 * sysfs_open_root_device: opens sysfs devices root and all of its
324 * @name: name of /sys/devices/root to open
325 * returns struct sysfs_root_device if success and NULL with error
327 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
329 struct sysfs_root_device *root = NULL;
330 unsigned char rootpath[SYSFS_PATH_MAX];
337 memset(rootpath, 0, SYSFS_PATH_MAX);
338 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
339 dprintf ("Sysfs not supported on this system\n");
343 if (sysfs_trailing_slash(rootpath) == 0)
344 strcat(rootpath, "/");
346 strcat(rootpath, SYSFS_DEVICES_NAME);
347 strcat(rootpath, "/");
348 strcat(rootpath, name);
349 if ((sysfs_path_is_dir(rootpath)) != 0) {
351 dprintf("Invalid root device: %s\n", name);
354 root = (struct sysfs_root_device *)calloc
355 (1, sizeof(struct sysfs_root_device));
357 dprintf("calloc failure\n");
360 strcpy(root->path, rootpath);
365 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
366 * the specific device
367 * @device: struct sysfs_device * for which attributes are to be returned
369 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
374 if (device->directory == NULL) {
375 device->directory = sysfs_open_directory(device->path);
376 if (device->directory == NULL)
379 if (device->directory->attributes == NULL) {
380 if ((sysfs_read_dir_attributes(device->directory)) != 0)
383 if ((sysfs_path_is_dir(device->path)) != 0) {
384 dprintf("Device at %s no longer exists", device->path);
387 if ((sysfs_refresh_attributes
388 (device->directory->attributes)) != 0) {
389 dprintf("Error refreshing device attributes\n");
393 return (device->directory->attributes);
397 * sysfs_get_device_attr: searches dev's attributes by name
398 * @dev: device to look through
399 * @name: attribute name to get
400 * returns sysfs_attribute reference with success or NULL with error.
402 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
403 const unsigned char *name)
405 struct sysfs_attribute *cur = NULL;
406 struct dlist *attrlist = NULL;
408 if (dev == NULL || name == NULL) {
413 attrlist = sysfs_get_device_attributes(dev);
414 if (attrlist == NULL)
417 cur = sysfs_get_directory_attribute(dev->directory,
418 (unsigned char *)name);
426 * get_device_absolute_path: looks up the bus the device is on, gets
427 * absolute path to the device
428 * @device: device for which path is needed
429 * @path: buffer to store absolute path
430 * @psize: size of "path"
431 * Returns 0 on success -1 on failure
433 static int get_device_absolute_path(const unsigned char *device,
434 const unsigned char *bus, unsigned char *path, size_t psize)
436 unsigned char bus_path[SYSFS_PATH_MAX];
438 if (device == NULL || path == NULL) {
443 memset(bus_path, 0, SYSFS_PATH_MAX);
444 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
445 dprintf ("Sysfs not supported on this system\n");
449 if (sysfs_trailing_slash(bus_path) == 0)
450 strcat(bus_path, "/");
452 strcat(bus_path, SYSFS_BUS_NAME);
453 strcat(bus_path, "/");
454 strcat(bus_path, bus);
455 strcat(bus_path, "/");
456 strcat(bus_path, SYSFS_DEVICES_NAME);
457 strcat(bus_path, "/");
458 strcat(bus_path, device);
460 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
461 * Now read this link to reach to the device.
463 if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
464 dprintf("Error getting to device %s\n", device);
471 * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
472 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
473 * /sys/bus/xxx/devices
474 * @bus: bus the device belongs to
475 * returns struct sysfs_device if found, NULL otherwise
477 * 1. Use sysfs_close_device to close the device
478 * 2. Bus the device is on must be supplied
479 * Use sysfs_find_device_bus to get the bus name
481 struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
482 const unsigned char *bus)
484 char sysfs_path[SYSFS_PATH_MAX];
485 struct sysfs_device *device = NULL;
487 if (bus_id == NULL || bus == NULL) {
491 memset(sysfs_path, 0, SYSFS_PATH_MAX);
492 if ((get_device_absolute_path(bus_id, bus, sysfs_path,
493 SYSFS_PATH_MAX)) != 0) {
494 dprintf("Error getting to device %s\n", bus_id);
498 device = sysfs_open_device(sysfs_path);
499 if (device == NULL) {
500 dprintf("Error opening device %s\n", bus_id);
508 * sysfs_open_device_attr: open the given device's attribute
509 * @bus: Bus on which to look
510 * @dev_id: device for which attribute is required
511 * @attrname: name of the attribute to look for
512 * Returns struct sysfs_attribute on success and NULL on failure
515 * A call to sysfs_close_attribute() is required to close
516 * the attribute returned and free memory.
518 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
519 const unsigned char *bus_id, const unsigned char *attrib)
521 struct sysfs_attribute *attribute = NULL;
522 unsigned char devpath[SYSFS_PATH_MAX];
524 if (bus == NULL || bus_id == NULL || attrib == NULL) {
529 memset(devpath, 0, SYSFS_PATH_MAX);
530 if ((get_device_absolute_path(bus_id, bus, devpath,
531 SYSFS_PATH_MAX)) != 0) {
532 dprintf("Error getting to device %s\n", bus_id);
535 strcat(devpath, "/");
536 strcat(devpath, attrib);
537 attribute = sysfs_open_attribute(devpath);
538 if (attribute == NULL) {
539 dprintf("Error opening attribute %s for device %s\n",
543 if ((sysfs_read_attribute(attribute)) != 0) {
544 dprintf("Error reading attribute %s for device %s\n",
546 sysfs_close_attribute(attribute);