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
26 static int confirm_device_bus(struct sysfs_device *dev,
27 unsigned char *busname, unsigned char *bus_id)
29 struct sysfs_link *devlink = NULL;
30 unsigned char devpath[SYSFS_PATH_MAX];
33 if (busname == NULL || bus_id == NULL)
36 if (sysfs_get_mnt_path(devpath, SYSFS_PATH_MAX) != 0)
39 if (sysfs_trailing_slash(devpath) == 0)
41 strcat(devpath, SYSFS_BUS_NAME);
43 strcat(devpath, busname);
44 strcat(devpath, SYSFS_DEVICES_DIR);
46 strcat(devpath, bus_id);
48 devlink = sysfs_open_link(devpath);
52 if (strcmp(devlink->target, dev->path) == 0)
54 sysfs_close_link(devlink);
59 * get_device_bus: retrieves the bus name the device is on, checks path to
60 * bus' link to make sure it has correct device.
61 * @dev: device to get busname.
62 * returns 0 with success and -1 with error.
64 static int get_device_bus(struct sysfs_device *dev)
66 unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL;
67 struct dlist *buslist = NULL, *device_list = NULL;
74 strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */
75 buslist = sysfs_open_subsystem_list(subsys);
76 if (buslist != NULL) {
77 dlist_for_each_data(buslist, bus, char) {
78 device_list = sysfs_open_bus_devices_list(bus);
79 if (device_list != NULL) {
80 dlist_for_each_data(device_list,
82 if (strcmp(dev->bus_id, curdev) == 0
83 && confirm_device_bus(dev, bus,
85 strcpy(dev->bus, bus);
86 sysfs_close_list(device_list);
87 sysfs_close_list(buslist);
91 sysfs_close_list(device_list);
94 sysfs_close_list(buslist);
100 * sysfs_close_device_tree: closes every device in the supplied tree,
101 * closing children only.
102 * @devroot: device root of tree.
104 static void sysfs_close_device_tree(struct sysfs_device *devroot)
106 if (devroot != NULL) {
107 if (devroot->children != NULL) {
108 struct sysfs_device *child = NULL;
110 dlist_for_each_data(devroot->children, child,
111 struct sysfs_device) {
112 sysfs_close_device_tree(child);
115 sysfs_close_device(devroot);
120 * sysfs_del_device: routine for dlist integration
122 static void sysfs_del_device(void *dev)
124 sysfs_close_device((struct sysfs_device *)dev);
128 * sysfs_close_dev_tree: routine for dlist integration
130 static void sysfs_close_dev_tree(void *dev)
132 sysfs_close_device_tree((struct sysfs_device *)dev);
136 * sysfs_close_device: closes and cleans up a device
137 * @dev = device to clean up
139 void sysfs_close_device(struct sysfs_device *dev)
142 if (dev->directory != NULL)
143 sysfs_close_directory(dev->directory);
144 if (dev->children != NULL && dev->children->count == 0)
145 dlist_destroy(dev->children);
151 * alloc_device: allocates and initializes device structure
152 * returns struct sysfs_device
154 static struct sysfs_device *alloc_device(void)
156 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
160 * sysfs_get_device_attr: searches dev's attributes by name
161 * @dev: device to look through
162 * @name: attribute name to get
163 * returns sysfs_attribute reference with success or NULL with error.
165 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
166 const unsigned char *name)
168 struct sysfs_attribute *cur = NULL;
170 if (dev == NULL || dev->directory == NULL
171 || dev->directory->attributes == NULL || name == NULL) {
176 cur = sysfs_get_directory_attribute(dev->directory,
177 (unsigned char *)name);
185 * sysfs_open_device: opens and populates device structure
186 * @path: path to device, this is the /sys/devices/ path
187 * returns sysfs_device structure with success or NULL with error
189 struct sysfs_device *sysfs_open_device(const unsigned char *path)
191 struct sysfs_device *dev = NULL;
192 struct sysfs_directory *sdir = NULL;
198 dev = alloc_device();
200 dprintf("Error allocating device at %s\n", path);
203 sdir = sysfs_open_directory(path);
205 dprintf("Invalid device at %s\n", path);
207 sysfs_close_device(dev);
210 if ((sysfs_read_directory(sdir)) != 0) {
211 dprintf("Error reading device directory at %s\n", path);
212 sysfs_close_directory(sdir);
213 sysfs_close_device(dev);
216 dev->directory = sdir;
217 strcpy(dev->bus_id, sdir->name);
218 strcpy(dev->path, sdir->path);
221 * The "name" attribute no longer exists... return the device's
222 * sysfs representation instead, in the "dev->name" field, which
223 * implies that the dev->name and dev->bus_id contain same data.
225 strncpy(dev->name, sdir->name, SYSFS_NAME_LEN);
227 if (get_device_bus(dev) != 0)
228 strcpy(dev->bus, SYSFS_UNKNOWN);
234 * sysfs_open_device_tree: opens root device and all of its children,
235 * creating a tree of devices. Only opens children.
236 * @path: sysfs path to devices
237 * returns struct sysfs_device and its children with success or NULL with
240 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
242 struct sysfs_device *rootdev = NULL, *new = NULL;
243 struct sysfs_directory *cur = NULL;
249 rootdev = sysfs_open_device(path);
250 if (rootdev == NULL) {
251 dprintf("Error opening root device at %s\n", path);
254 if (rootdev->directory->subdirs != NULL) {
255 dlist_for_each_data(rootdev->directory->subdirs, cur,
256 struct sysfs_directory) {
257 new = sysfs_open_device_tree(cur->path);
259 dprintf("Error opening device tree at %s\n",
261 sysfs_close_device_tree(rootdev);
264 if (rootdev->children == NULL)
265 rootdev->children = dlist_new_with_delete
266 (sizeof(struct sysfs_device),
268 dlist_unshift(rootdev->children, new);
276 * sysfs_close_root_device: closes root and all devices
277 * @root: root device to close
279 void sysfs_close_root_device(struct sysfs_root_device *root)
282 if (root->devices != NULL)
283 dlist_destroy(root->devices);
284 if (root->directory != NULL)
285 sysfs_close_directory(root->directory);
291 * open_root_device_dir: opens up sysfs_directory for specific root dev
292 * @name: name of root
293 * returns struct sysfs_directory with success and NULL with error
295 static struct sysfs_directory *open_root_device_dir(const unsigned char *name)
297 struct sysfs_directory *rdir = NULL;
298 unsigned char rootpath[SYSFS_PATH_MAX];
305 memset(rootpath, 0, SYSFS_PATH_MAX);
306 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
307 dprintf ("Sysfs not supported on this system\n");
311 if (sysfs_trailing_slash(rootpath) == 0)
312 strcat(rootpath, "/");
314 strcat(rootpath, SYSFS_DEVICES_NAME);
315 strcat(rootpath, "/");
316 strcat(rootpath, name);
317 rdir = sysfs_open_directory(rootpath);
320 dprintf ("Root device %s not supported on this system\n",
324 if (sysfs_read_directory(rdir) != 0) {
325 dprintf ("Error reading %s root device at dir %s\n", name,
327 sysfs_close_directory(rdir);
335 * get_all_root_devices: opens up all the devices under this root device
336 * @root: root device to open devices for
337 * returns 0 with success and -1 with error
339 static int get_all_root_devices(struct sysfs_root_device *root)
341 struct sysfs_device *dev = NULL;
342 struct sysfs_directory *cur = NULL;
344 if (root == NULL || root->directory == NULL) {
348 if (root->directory->subdirs == NULL)
351 dlist_for_each_data(root->directory->subdirs, cur,
352 struct sysfs_directory) {
353 dev = sysfs_open_device_tree(cur->path);
355 dprintf ("Error opening device at %s\n", cur->path);
358 if (root->devices == NULL)
359 root->devices = dlist_new_with_delete
360 (sizeof(struct sysfs_device),
361 sysfs_close_dev_tree);
362 dlist_unshift(root->devices, dev);
369 * sysfs_open_root_device: opens sysfs devices root and all of its
371 * @name: name of /sys/devices/root to open
372 * returns struct sysfs_root_device if success and NULL with error
374 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
376 struct sysfs_root_device *root = NULL;
377 struct sysfs_directory *rootdir = NULL;
384 root = (struct sysfs_root_device *)calloc
385 (1, sizeof(struct sysfs_root_device));
387 dprintf("calloc failure\n");
390 rootdir = open_root_device_dir(name);
391 if (rootdir == NULL) {
392 dprintf ("Invalid root device, %s not supported\n", name);
393 sysfs_close_root_device(root);
396 strcpy(root->path, rootdir->path);
397 root->directory = rootdir;
398 if (get_all_root_devices(root) != 0) {
399 dprintf ("Error retrieving devices for root %s\n", name);
400 sysfs_close_root_device(root);
408 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
409 * the specific device
410 * @device: struct sysfs_device * for which attributes are to be returned
412 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
414 if (device == NULL || device->directory == NULL)
417 return (device->directory->attributes);
421 * get_device_absolute_path: looks up the bus the device is on, gets
422 * absolute path to the device
423 * @device: device for which path is needed
424 * @path: buffer to store absolute path
425 * @psize: size of "path"
426 * Returns 0 on success -1 on failure
428 static int get_device_absolute_path(const unsigned char *device,
429 const unsigned char *bus, unsigned char *path, size_t psize)
431 unsigned char bus_path[SYSFS_NAME_LEN];
433 if (device == NULL || path == NULL) {
438 memset(bus_path, 0, SYSFS_NAME_LEN);
439 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
440 dprintf ("Sysfs not supported on this system\n");
443 if (sysfs_trailing_slash(bus_path) == 0)
444 strcat(bus_path, "/");
445 strcat(bus_path, SYSFS_BUS_NAME);
446 strcat(bus_path, "/");
447 strcat(bus_path, bus);
448 strcat(bus_path, SYSFS_DEVICES_DIR);
449 strcat(bus_path, "/");
450 strcat(bus_path, device);
452 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
453 * Now read this link to reach to the device.
455 if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
456 dprintf("Error getting to device %s\n", device);
463 * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
464 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
465 * /sys/bus/xxx/devices
466 * @bus: bus the device belongs to
467 * @bsize: size of the bus buffer
468 * returns struct sysfs_device if found, NULL otherwise
470 * 1. Use sysfs_close_device to close the device
471 * 2. Bus the device is on must be supplied
472 * Use sysfs_find_device_bus to get the bus name
474 struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
475 const unsigned char *bus, size_t bsize)
477 char sysfs_path[SYSFS_PATH_MAX];
478 struct sysfs_device *device = NULL;
480 if (bus_id == NULL || bus == NULL) {
484 memset(sysfs_path, 0, SYSFS_PATH_MAX);
485 if ((get_device_absolute_path(bus_id, bus, sysfs_path,
486 SYSFS_PATH_MAX)) != 0) {
487 dprintf("Error getting to device %s\n", bus_id);
491 device = sysfs_open_device(sysfs_path);
492 if (device == NULL) {
493 dprintf("Error opening device %s\n", bus_id);
501 * sysfs_open_device_attr: open the given device's attribute
502 * @bus: Bus on which to look
503 * @dev_id: device for which attribute is required
504 * @attrname: name of the attribute to look for
505 * Returns struct sysfs_attribute on success and NULL on failure
508 * A call to sysfs_close_attribute() is required to close
509 * the attribute returned and free memory.
511 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
512 const unsigned char *bus_id, const unsigned char *attrib)
514 struct sysfs_attribute *attribute = NULL;
515 unsigned char devpath[SYSFS_PATH_MAX];
517 if (bus == NULL || bus_id == NULL || attrib == NULL) {
522 memset(devpath, 0, SYSFS_PATH_MAX);
523 if ((get_device_absolute_path(bus_id, bus, devpath,
524 SYSFS_PATH_MAX)) != 0) {
525 dprintf("Error getting to device %s\n", bus_id);
528 strcat(devpath, "/");
529 strcat(devpath, attrib);
530 attribute = sysfs_open_attribute(devpath);
531 if (attribute == NULL) {
532 dprintf("Error opening attribute %s for device %s\n",
536 if ((sysfs_read_attribute(attribute)) != 0) {
537 dprintf("Error reading attribute %s for device %s\n",
539 sysfs_close_attribute(attribute);