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];
31 unsigned char *s = NULL;
34 if (busname == NULL || bus_id == NULL)
37 if (sysfs_get_mnt_path(devpath, SYSFS_PATH_MAX) != 0)
40 s = &devpath[strlen(devpath)-1];
41 if (strncmp(s, "/", 1) == 0)
43 strcat(devpath, SYSFS_BUS_DIR);
45 strcat(devpath, busname);
46 strcat(devpath, SYSFS_DEVICES_DIR);
48 strcat(devpath, bus_id);
50 devlink = sysfs_open_link(devpath);
54 if (strcmp(devlink->target, dev->path) == 0)
56 sysfs_close_link(devlink);
61 * get_device_bus: retrieves the bus name the device is on, checks path to
62 * bus' link to make sure it has correct device.
63 * @dev: device to get busname.
64 * returns 0 with success and -1 with error.
66 static int get_device_bus(struct sysfs_device *dev)
68 unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL;
69 struct dlist *buslist = NULL, *device_list = NULL;
76 strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */
77 buslist = sysfs_open_subsystem_list(subsys);
78 if (buslist != NULL) {
79 dlist_for_each_data(buslist, bus, char) {
80 device_list = sysfs_open_bus_devices_list(bus);
81 if (device_list != NULL) {
82 dlist_for_each_data(device_list,
84 if (strcmp(dev->bus_id, curdev) == 0
85 && confirm_device_bus(dev, bus,
87 strcpy(dev->bus, bus);
88 sysfs_close_list(device_list);
89 sysfs_close_list(buslist);
93 sysfs_close_list(device_list);
96 sysfs_close_list(buslist);
102 * sysfs_close_device_tree: closes every device in the supplied tree,
103 * closing children only.
104 * @devroot: device root of tree.
106 static void sysfs_close_device_tree(struct sysfs_device *devroot)
108 if (devroot != NULL) {
109 if (devroot->children != NULL) {
110 struct sysfs_device *child = NULL;
112 dlist_for_each_data(devroot->children, child,
113 struct sysfs_device) {
114 sysfs_close_device_tree(child);
117 sysfs_close_device(devroot);
122 * sysfs_del_device: routine for dlist integration
124 static void sysfs_del_device(void *dev)
126 sysfs_close_device((struct sysfs_device *)dev);
130 * sysfs_close_dev_tree: routine for dlist integration
132 static void sysfs_close_dev_tree(void *dev)
134 sysfs_close_device_tree((struct sysfs_device *)dev);
138 * sysfs_close_device: closes and cleans up a device
139 * @dev = device to clean up
141 void sysfs_close_device(struct sysfs_device *dev)
144 if (dev->directory != NULL)
145 sysfs_close_directory(dev->directory);
146 if (dev->children != NULL && dev->children->count == 0)
147 dlist_destroy(dev->children);
153 * alloc_device: allocates and initializes device structure
154 * returns struct sysfs_device
156 static struct sysfs_device *alloc_device(void)
158 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
162 * sysfs_get_device_attr: searches dev's attributes by name
163 * @dev: device to look through
164 * @name: attribute name to get
165 * returns sysfs_attribute reference with success or NULL with error.
167 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
168 const unsigned char *name)
170 struct sysfs_attribute *cur = NULL;
172 if (dev == NULL || dev->directory == NULL
173 || dev->directory->attributes == NULL || name == NULL) {
178 cur = sysfs_get_directory_attribute(dev->directory,
179 (unsigned char *)name);
187 * sysfs_open_device: opens and populates device structure
188 * @path: path to device, this is the /sys/devices/ path
189 * returns sysfs_device structure with success or NULL with error
191 struct sysfs_device *sysfs_open_device(const unsigned char *path)
193 struct sysfs_device *dev = NULL;
194 struct sysfs_directory *sdir = NULL;
200 dev = alloc_device();
202 dprintf("Error allocating device at %s\n", path);
205 sdir = sysfs_open_directory(path);
207 dprintf("Invalid device at %s\n", path);
209 sysfs_close_device(dev);
212 if ((sysfs_read_directory(sdir)) != 0) {
213 dprintf("Error reading device directory at %s\n", path);
214 sysfs_close_directory(sdir);
215 sysfs_close_device(dev);
218 dev->directory = sdir;
219 strcpy(dev->bus_id, sdir->name);
220 strcpy(dev->path, sdir->path);
223 * The "name" attribute no longer exists... return the device's
224 * sysfs representation instead, in the "dev->name" field, which
225 * implies that the dev->name and dev->bus_id contain same data.
227 strncpy(dev->name, sdir->name, SYSFS_NAME_LEN);
229 if (get_device_bus(dev) != 0)
230 strcpy(dev->bus, SYSFS_UNKNOWN);
236 * sysfs_open_device_tree: opens root device and all of its children,
237 * creating a tree of devices. Only opens children.
238 * @path: sysfs path to devices
239 * returns struct sysfs_device and its children with success or NULL with
242 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
244 struct sysfs_device *rootdev = NULL, *new = NULL;
245 struct sysfs_directory *cur = NULL;
251 rootdev = sysfs_open_device(path);
252 if (rootdev == NULL) {
253 dprintf("Error opening root device at %s\n", path);
256 if (rootdev->directory->subdirs != NULL) {
257 dlist_for_each_data(rootdev->directory->subdirs, cur,
258 struct sysfs_directory) {
259 new = sysfs_open_device_tree(cur->path);
261 dprintf("Error opening device tree at %s\n",
263 sysfs_close_device_tree(rootdev);
266 if (rootdev->children == NULL)
267 rootdev->children = dlist_new_with_delete
268 (sizeof(struct sysfs_device),
270 dlist_unshift(rootdev->children, new);
278 * sysfs_close_root_device: closes root and all devices
279 * @root: root device to close
281 void sysfs_close_root_device(struct sysfs_root_device *root)
284 if (root->devices != NULL)
285 dlist_destroy(root->devices);
286 if (root->directory != NULL)
287 sysfs_close_directory(root->directory);
293 * open_root_device_dir: opens up sysfs_directory for specific root dev
294 * @name: name of root
295 * returns struct sysfs_directory with success and NULL with error
297 static struct sysfs_directory *open_root_device_dir(const unsigned char *name)
299 struct sysfs_directory *rdir = NULL;
300 unsigned char rootpath[SYSFS_PATH_MAX];
307 memset(rootpath, 0, SYSFS_PATH_MAX);
308 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
309 dprintf ("Sysfs not supported on this system\n");
313 strcat(rootpath, SYSFS_DEVICES_DIR);
314 strcat(rootpath, "/");
315 strcat(rootpath, name);
316 rdir = sysfs_open_directory(rootpath);
319 dprintf ("Root device %s not supported on this system\n",
323 if (sysfs_read_directory(rdir) != 0) {
324 dprintf ("Error reading %s root device at dir %s\n", name,
326 sysfs_close_directory(rdir);
334 * get_all_root_devices: opens up all the devices under this root device
335 * @root: root device to open devices for
336 * returns 0 with success and -1 with error
338 static int get_all_root_devices(struct sysfs_root_device *root)
340 struct sysfs_device *dev = NULL;
341 struct sysfs_directory *cur = NULL;
343 if (root == NULL || root->directory == NULL) {
347 if (root->directory->subdirs == NULL)
350 dlist_for_each_data(root->directory->subdirs, cur,
351 struct sysfs_directory) {
352 dev = sysfs_open_device_tree(cur->path);
354 dprintf ("Error opening device at %s\n", cur->path);
357 if (root->devices == NULL)
358 root->devices = dlist_new_with_delete
359 (sizeof(struct sysfs_device),
360 sysfs_close_dev_tree);
361 dlist_unshift(root->devices, dev);
368 * sysfs_open_root_device: opens sysfs devices root and all of its
370 * @name: name of /sys/devices/root to open
371 * returns struct sysfs_root_device if success and NULL with error
373 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
375 struct sysfs_root_device *root = NULL;
376 struct sysfs_directory *rootdir = NULL;
383 root = (struct sysfs_root_device *)calloc
384 (1, sizeof(struct sysfs_root_device));
386 dprintf("calloc failure\n");
389 rootdir = open_root_device_dir(name);
390 if (rootdir == NULL) {
391 dprintf ("Invalid root device, %s not supported\n", name);
392 sysfs_close_root_device(root);
395 strcpy(root->path, rootdir->path);
396 root->directory = rootdir;
397 if (get_all_root_devices(root) != 0) {
398 dprintf ("Error retrieving devices for root %s\n", name);
399 sysfs_close_root_device(root);
407 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
408 * the specific device
409 * @device: struct sysfs_device * for which attributes are to be returned
411 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
413 if (device == NULL || device->directory == NULL)
416 return (device->directory->attributes);
420 * get_device_absolute_path: looks up the bus the device is on, gets
421 * absolute path to the device
422 * @device: device for which path is needed
423 * @path: buffer to store absolute path
424 * @psize: size of "path"
425 * Returns 0 on success -1 on failure
427 static int get_device_absolute_path(const unsigned char *device,
428 const unsigned char *bus, unsigned char *path, size_t psize)
430 unsigned char bus_path[SYSFS_NAME_LEN];
432 if (device == NULL || path == NULL) {
437 memset(bus_path, 0, SYSFS_NAME_LEN);
438 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
439 dprintf ("Sysfs not supported on this system\n");
442 strcat(bus_path, SYSFS_BUS_DIR);
443 strcat(bus_path, "/");
444 strcat(bus_path, bus);
445 strcat(bus_path, SYSFS_DEVICES_DIR);
446 strcat(bus_path, "/");
447 strcat(bus_path, device);
449 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
450 * Now read this link to reach to the device.
452 if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
453 dprintf("Error getting to device %s\n", device);
460 * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
461 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
462 * /sys/bus/xxx/devices
463 * @bus: bus the device belongs to
464 * @bsize: size of the bus buffer
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_by_id(const unsigned char *bus_id,
472 const unsigned char *bus, size_t bsize)
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(sysfs_path);
489 if (device == NULL) {
490 dprintf("Error opening device %s\n", bus_id);
498 * sysfs_open_device_attr: open the given device's attribute
499 * @bus: Bus on which to look
500 * @dev_id: device for which attribute is required
501 * @attrname: name of the attribute to look for
502 * Returns struct sysfs_attribute on success and NULL on failure
505 * A call to sysfs_close_attribute() is required to close
506 * the attribute returned and free memory.
508 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
509 const unsigned char *bus_id, const unsigned char *attrib)
511 struct sysfs_attribute *attribute = NULL;
512 unsigned char devpath[SYSFS_PATH_MAX];
514 if (bus == NULL || bus_id == NULL || attrib == NULL) {
519 memset(devpath, 0, SYSFS_PATH_MAX);
520 if ((get_device_absolute_path(bus_id, bus, devpath,
521 SYSFS_PATH_MAX)) != 0) {
522 dprintf("Error getting to device %s\n", bus_id);
525 strcat(devpath, "/");
526 strcat(devpath, attrib);
527 attribute = sysfs_open_attribute(devpath);
528 if (attribute == NULL) {
529 dprintf("Error opening attribute %s for device %s\n",
533 if ((sysfs_read_attribute(attribute)) != 0) {
534 dprintf("Error reading attribute %s for device %s\n",
536 sysfs_close_attribute(attribute);