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 strcat(devpath, SYSFS_BUS_DIR);
41 strcat(devpath, busname);
42 strcat(devpath, SYSFS_DEVICES_DIR);
44 strcat(devpath, bus_id);
46 devlink = sysfs_open_link(devpath);
50 if (strcmp(devlink->target, dev->path) == 0)
52 sysfs_close_link(devlink);
57 * get_device_bus: retrieves the bus name the device is on, checks path to
58 * bus' link to make sure it has correct device.
59 * @dev: device to get busname.
60 * returns 0 with success and -1 with error.
62 static int get_device_bus(struct sysfs_device *dev)
64 unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL;
65 struct dlist *buslist = NULL, *device_list = NULL;
72 strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */
73 buslist = sysfs_open_subsystem_list(subsys);
74 if (buslist != NULL) {
75 dlist_for_each_data(buslist, bus, char) {
76 device_list = sysfs_open_bus_devices_list(bus);
77 if (device_list != NULL) {
78 dlist_for_each_data(device_list,
80 if (strcmp(dev->bus_id, curdev) == 0
81 && confirm_device_bus(dev, bus,
83 strcpy(dev->bus, bus);
84 sysfs_close_list(device_list);
85 sysfs_close_list(buslist);
89 sysfs_close_list(device_list);
92 sysfs_close_list(buslist);
98 * sysfs_close_device_tree: closes every device in the supplied tree,
99 * closing children only.
100 * @devroot: device root of tree.
102 static void sysfs_close_device_tree(struct sysfs_device *devroot)
104 if (devroot != NULL) {
105 if (devroot->children != NULL) {
106 struct sysfs_device *child = NULL;
108 dlist_for_each_data(devroot->children, child,
109 struct sysfs_device) {
110 sysfs_close_device_tree(child);
113 sysfs_close_device(devroot);
118 * sysfs_del_device: routine for dlist integration
120 static void sysfs_del_device(void *dev)
122 sysfs_close_device((struct sysfs_device *)dev);
126 * sysfs_close_dev_tree: routine for dlist integration
128 static void sysfs_close_dev_tree(void *dev)
130 sysfs_close_device_tree((struct sysfs_device *)dev);
134 * sysfs_close_device: closes and cleans up a device
135 * @dev = device to clean up
137 void sysfs_close_device(struct sysfs_device *dev)
140 if (dev->directory != NULL)
141 sysfs_close_directory(dev->directory);
142 if (dev->children != NULL && dev->children->count == 0)
143 dlist_destroy(dev->children);
149 * alloc_device: allocates and initializes device structure
150 * returns struct sysfs_device
152 static struct sysfs_device *alloc_device(void)
154 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
158 * sysfs_get_device_attr: searches dev's attributes by name
159 * @dev: device to look through
160 * @name: attribute name to get
161 * returns sysfs_attribute reference with success or NULL with error.
163 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
164 const unsigned char *name)
166 struct sysfs_attribute *cur = NULL;
168 if (dev == NULL || dev->directory == NULL
169 || dev->directory->attributes == NULL || name == NULL) {
174 cur = sysfs_get_directory_attribute(dev->directory,
175 (unsigned char *)name);
183 * sysfs_open_device: opens and populates device structure
184 * @path: path to device, this is the /sys/devices/ path
185 * returns sysfs_device structure with success or NULL with error
187 struct sysfs_device *sysfs_open_device(const unsigned char *path)
189 struct sysfs_device *dev = NULL;
190 struct sysfs_directory *sdir = NULL;
196 dev = alloc_device();
198 dprintf("Error allocating device at %s\n", path);
201 sdir = sysfs_open_directory(path);
203 dprintf("Invalid device at %s\n", path);
205 sysfs_close_device(dev);
208 if ((sysfs_read_directory(sdir)) != 0) {
209 dprintf("Error reading device directory at %s\n", path);
210 sysfs_close_directory(sdir);
211 sysfs_close_device(dev);
214 dev->directory = sdir;
215 strcpy(dev->bus_id, sdir->name);
216 strcpy(dev->path, sdir->path);
219 * The "name" attribute no longer exists... return the device's
220 * sysfs representation instead, in the "dev->name" field, which
221 * implies that the dev->name and dev->bus_id contain same data.
223 strncpy(dev->name, sdir->name, SYSFS_NAME_LEN);
225 if (get_device_bus(dev) != 0)
226 strcpy(dev->bus, SYSFS_UNKNOWN);
232 * sysfs_open_device_tree: opens root device and all of its children,
233 * creating a tree of devices. Only opens children.
234 * @path: sysfs path to devices
235 * returns struct sysfs_device and its children with success or NULL with
238 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
240 struct sysfs_device *rootdev = NULL, *new = NULL;
241 struct sysfs_directory *cur = NULL;
247 rootdev = sysfs_open_device(path);
248 if (rootdev == NULL) {
249 dprintf("Error opening root device at %s\n", path);
252 if (rootdev->directory->subdirs != NULL) {
253 dlist_for_each_data(rootdev->directory->subdirs, cur,
254 struct sysfs_directory) {
255 new = sysfs_open_device_tree(cur->path);
257 dprintf("Error opening device tree at %s\n",
259 sysfs_close_device_tree(rootdev);
262 if (rootdev->children == NULL)
263 rootdev->children = dlist_new_with_delete
264 (sizeof(struct sysfs_device),
266 dlist_unshift(rootdev->children, new);
274 * sysfs_close_root_device: closes root and all devices
275 * @root: root device to close
277 void sysfs_close_root_device(struct sysfs_root_device *root)
280 if (root->devices != NULL)
281 dlist_destroy(root->devices);
282 if (root->directory != NULL)
283 sysfs_close_directory(root->directory);
289 * open_root_device_dir: opens up sysfs_directory for specific root dev
290 * @name: name of root
291 * returns struct sysfs_directory with success and NULL with error
293 static struct sysfs_directory *open_root_device_dir(const unsigned char *name)
295 struct sysfs_directory *rdir = NULL;
296 unsigned char rootpath[SYSFS_PATH_MAX];
303 memset(rootpath, 0, SYSFS_PATH_MAX);
304 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
305 dprintf ("Sysfs not supported on this system\n");
309 strcat(rootpath, SYSFS_DEVICES_DIR);
310 strcat(rootpath, "/");
311 strcat(rootpath, name);
312 rdir = sysfs_open_directory(rootpath);
315 dprintf ("Root device %s not supported on this system\n",
319 if (sysfs_read_directory(rdir) != 0) {
320 dprintf ("Error reading %s root device at dir %s\n", name,
322 sysfs_close_directory(rdir);
330 * get_all_root_devices: opens up all the devices under this root device
331 * @root: root device to open devices for
332 * returns 0 with success and -1 with error
334 static int get_all_root_devices(struct sysfs_root_device *root)
336 struct sysfs_device *dev = NULL;
337 struct sysfs_directory *cur = NULL;
339 if (root == NULL || root->directory == NULL) {
343 if (root->directory->subdirs == NULL)
346 dlist_for_each_data(root->directory->subdirs, cur,
347 struct sysfs_directory) {
348 dev = sysfs_open_device_tree(cur->path);
350 dprintf ("Error opening device at %s\n", cur->path);
353 if (root->devices == NULL)
354 root->devices = dlist_new_with_delete
355 (sizeof(struct sysfs_device),
356 sysfs_close_dev_tree);
357 dlist_unshift(root->devices, dev);
364 * sysfs_open_root_device: opens sysfs devices root and all of its
366 * @name: name of /sys/devices/root to open
367 * returns struct sysfs_root_device if success and NULL with error
369 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
371 struct sysfs_root_device *root = NULL;
372 struct sysfs_directory *rootdir = NULL;
379 root = (struct sysfs_root_device *)calloc
380 (1, sizeof(struct sysfs_root_device));
382 dprintf("calloc failure\n");
385 rootdir = open_root_device_dir(name);
386 if (rootdir == NULL) {
387 dprintf ("Invalid root device, %s not supported\n", name);
388 sysfs_close_root_device(root);
391 strcpy(root->path, rootdir->path);
392 root->directory = rootdir;
393 if (get_all_root_devices(root) != 0) {
394 dprintf ("Error retrieving devices for root %s\n", name);
395 sysfs_close_root_device(root);
403 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
404 * the specific device
405 * @device: struct sysfs_device * for which attributes are to be returned
407 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
409 if (device == NULL || device->directory == NULL)
412 return (device->directory->attributes);
416 * get_device_absolute_path: looks up the bus the device is on, gets
417 * absolute path to the device
418 * @device: device for which path is needed
419 * @path: buffer to store absolute path
420 * @psize: size of "path"
421 * Returns 0 on success -1 on failure
423 static int get_device_absolute_path(const unsigned char *device,
424 const unsigned char *bus, unsigned char *path, size_t psize)
426 unsigned char bus_path[SYSFS_NAME_LEN];
428 if (device == NULL || path == NULL) {
433 memset(bus_path, 0, SYSFS_NAME_LEN);
434 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
435 dprintf ("Sysfs not supported on this system\n");
438 strcat(bus_path, SYSFS_BUS_DIR);
439 strcat(bus_path, "/");
440 strcat(bus_path, bus);
441 strcat(bus_path, SYSFS_DEVICES_DIR);
442 strcat(bus_path, "/");
443 strcat(bus_path, device);
445 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
446 * Now read this link to reach to the device.
448 if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
449 dprintf("Error getting to device %s\n", device);
456 * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
457 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
458 * /sys/bus/xxx/devices
459 * @bus: bus the device belongs to
460 * @bsize: size of the bus buffer
461 * returns struct sysfs_device if found, NULL otherwise
463 * 1. Use sysfs_close_device to close the device
464 * 2. Bus the device is on must be supplied
465 * Use sysfs_find_device_bus to get the bus name
467 struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id,
468 const unsigned char *bus, size_t bsize)
470 char sysfs_path[SYSFS_PATH_MAX];
471 struct sysfs_device *device = NULL;
473 if (bus_id == NULL || bus == NULL) {
477 memset(sysfs_path, 0, SYSFS_PATH_MAX);
478 if ((get_device_absolute_path(bus_id, bus, sysfs_path,
479 SYSFS_PATH_MAX)) != 0) {
480 dprintf("Error getting to device %s\n", bus_id);
484 device = sysfs_open_device(sysfs_path);
485 if (device == NULL) {
486 dprintf("Error opening device %s\n", bus_id);
494 * sysfs_open_device_attr: open the given device's attribute
495 * @bus: Bus on which to look
496 * @dev_id: device for which attribute is required
497 * @attrname: name of the attribute to look for
498 * Returns struct sysfs_attribute on success and NULL on failure
501 * A call to sysfs_close_attribute() is required to close
502 * the attribute returned and free memory.
504 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
505 const unsigned char *bus_id, const unsigned char *attrib)
507 struct sysfs_attribute *attribute = NULL;
508 unsigned char devpath[SYSFS_PATH_MAX];
510 if (bus == NULL || bus_id == NULL || attrib == NULL) {
515 memset(devpath, 0, SYSFS_PATH_MAX);
516 if ((get_device_absolute_path(bus_id, bus, devpath,
517 SYSFS_PATH_MAX)) != 0) {
518 dprintf("Error getting to device %s\n", bus_id);
521 strcat(devpath, "/");
522 strcat(devpath, attrib);
523 attribute = sysfs_open_attribute(devpath);
524 if (attribute == NULL) {
525 dprintf("Error opening attribute %s for device %s\n",
529 if ((sysfs_read_attribute(attribute)) != 0) {
530 dprintf("Error reading attribute %s for device %s\n",
532 sysfs_close_attribute(attribute);