4 * Generic class 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 void sysfs_close_cls_dev(void *dev)
28 sysfs_close_class_device((struct sysfs_class_device *)dev);
32 * class_name_equal: compares class_devices' name
33 * @a: class_name looking for
34 * @b: sysfs_class_device being compared
36 static int class_name_equal(void *a, void *b)
38 if (a == NULL || b == NULL)
41 if (strcmp(((unsigned char *)a), ((struct sysfs_class_device *)b)->name)
49 * sysfs_close_class_device: closes a single class device.
50 * @dev: class device to close.
52 void sysfs_close_class_device(struct sysfs_class_device *dev)
55 if (dev->directory != NULL)
56 sysfs_close_directory(dev->directory);
57 if (dev->sysdevice != NULL)
58 sysfs_close_device(dev->sysdevice);
59 if (dev->driver != NULL)
60 sysfs_close_driver(dev->driver);
61 if (dev->parent != NULL)
62 sysfs_close_class_device(dev->parent);
68 * sysfs_close_class: close single class
69 * @class: class structure
71 void sysfs_close_class(struct sysfs_class *cls)
74 if (cls->directory != NULL)
75 sysfs_close_directory(cls->directory);
76 if (cls->devices != NULL)
77 dlist_destroy(cls->devices);
83 * alloc_class_device: mallocs and initializes new class device struct.
84 * returns sysfs_class_device or NULL.
86 static struct sysfs_class_device *alloc_class_device(void)
88 return (struct sysfs_class_device *)
89 calloc(1, sizeof(struct sysfs_class_device));
93 * alloc_class: mallocs new class structure
94 * returns sysfs_class struct or NULL
96 static struct sysfs_class *alloc_class(void)
98 return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
102 * set_classdev_classname: Grabs classname from path
103 * @cdev: class device to set
106 static void set_classdev_classname(struct sysfs_class_device *cdev)
108 unsigned char *c = NULL, *e = NULL;
111 c = strstr(cdev->path, SYSFS_CLASS_NAME);
113 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
119 strcpy(cdev->classname, SYSFS_UNKNOWN);
124 while (e != NULL && *e != '/' && *e != '\0') {
128 strncpy(cdev->classname, c, count);
133 * sysfs_open_class_device: Opens and populates class device
134 * @path: path to class device.
135 * returns struct sysfs_class_device with success and NULL with error.
137 struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
139 struct sysfs_class_device *cdev = NULL;
145 if ((sysfs_path_is_dir(path)) != 0) {
146 dprintf("%s is not a valid path to a class device\n", path);
149 cdev = alloc_class_device();
151 dprintf("calloc failed\n");
154 if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
156 dprintf("Error getting class device name\n");
157 sysfs_close_class_device(cdev);
161 strcpy(cdev->path, path);
162 set_classdev_classname(cdev);
168 * sysfs_get_class_devices: gets all devices for class
169 * @class: class to get devices for
170 * returns dlist of class_devices with success and NULL with error
172 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
174 struct sysfs_class_device *dev = NULL;
175 struct sysfs_directory *cur = NULL;
181 if (cls->directory == NULL) {
182 cls->directory = sysfs_open_directory(cls->path);
183 if (cls->directory == NULL)
187 if ((sysfs_read_dir_subdirs(cls->directory) != 0)
188 || cls->directory->subdirs == NULL)
191 dlist_for_each_data(cls->directory->subdirs, cur,
192 struct sysfs_directory) {
193 dev = sysfs_open_class_device(cur->path);
195 dprintf("Error opening device at %s\n", cur->path);
198 if (cls->devices == NULL)
199 cls->devices = dlist_new_with_delete
200 (sizeof(struct sysfs_class_device),
201 sysfs_close_cls_dev);
202 dlist_unshift(cls->devices, dev);
208 * sysfs_open_class: opens specific class and all its devices on system
209 * returns sysfs_class structure with success or NULL with error.
211 struct sysfs_class *sysfs_open_class(const unsigned char *name)
213 struct sysfs_class *cls = NULL;
214 unsigned char classpath[SYSFS_PATH_MAX];
221 memset(classpath, 0, SYSFS_PATH_MAX);
222 if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
223 dprintf("Sysfs not supported on this system\n");
227 if (sysfs_trailing_slash(classpath) == 0)
228 strcat(classpath, "/");
231 * We shall now treat "block" also as a class. Hence, check here
232 * if "name" is "block" and proceed accordingly
234 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
235 strcat(classpath, SYSFS_BLOCK_NAME);
237 strcat(classpath, SYSFS_CLASS_NAME);
238 strcat(classpath, "/");
239 strcat(classpath, name);
241 if ((sysfs_path_is_dir(classpath)) != 0) {
242 dprintf("Class %s not found on the system\n", name);
248 dprintf("calloc failed\n");
251 strcpy(cls->name, name);
252 strcpy(cls->path, classpath);
258 * sysfs_get_class_device: Get specific class device using the device's id
259 * @class: class to find device on
260 * @name: class name of the device
262 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
265 struct dlist *devlist = NULL;
267 if (class == NULL || name == NULL) {
272 if (class->devices == NULL) {
273 class->devices = sysfs_get_class_devices(class);
277 return (struct sysfs_class_device *)dlist_find_custom(class->devices,
278 name, class_name_equal);
282 * sysfs_get_classdev_device: returns the sysfs_device corresponding to
283 * sysfs_class_device, if present
284 * @clsdev: class device whose sysfs_device is required
285 * Returns sysfs_device on success, NULL on error or if device is not
288 struct sysfs_device *sysfs_get_classdev_device
289 (struct sysfs_class_device *clsdev)
291 struct sysfs_link *devlink = NULL;
293 if (clsdev == NULL) {
298 if (clsdev->sysdevice != NULL)
299 return (clsdev->sysdevice);
301 if (clsdev->directory == NULL) {
302 clsdev->directory = sysfs_open_directory(clsdev->path);
303 if (clsdev->directory == NULL)
306 devlink = sysfs_get_directory_link(clsdev->directory, "device");
310 clsdev->sysdevice = sysfs_open_device(devlink->target);
311 if (clsdev->sysdevice == NULL)
313 if (clsdev->driver != NULL)
314 strcpy(clsdev->sysdevice->driver_name, clsdev->driver->name);
316 return (clsdev->sysdevice);
320 * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to
321 * sysfs_class_device, if present
322 * @clsdev: class device whose sysfs_device is required
323 * Returns sysfs_driver on success, NULL on error or if driver is not
326 struct sysfs_driver *sysfs_get_classdev_driver
327 (struct sysfs_class_device *clsdev)
329 struct sysfs_link *drvlink = NULL;
331 if (clsdev == NULL) {
336 if (clsdev->driver != NULL)
337 return (clsdev->driver);
339 if (clsdev->directory == NULL) {
340 clsdev->directory = sysfs_open_directory(clsdev->path);
341 if (clsdev->directory == NULL)
344 drvlink = sysfs_get_directory_link(clsdev->directory, "driver");
345 if (drvlink != NULL) {
346 clsdev->driver = sysfs_open_driver(drvlink->target);
347 if (clsdev->driver == NULL)
351 return (clsdev->driver);
355 * get_blockdev_parent: Get the parent class device for a "block" subsystem
357 * @clsdev: block subsystem class device whose parent needs to be found
358 * Returns 0 on success and 1 on error
360 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
362 unsigned char parent_path[SYSFS_PATH_MAX], value[256], *c = NULL;
364 memset(parent_path, 0, SYSFS_PATH_MAX);
365 strcpy(parent_path, clsdev->path);
367 c = strstr(parent_path, SYSFS_BLOCK_NAME);
369 dprintf("Class device %s does not belong to BLOCK subsystem",
374 c += strlen(SYSFS_BLOCK_NAME);
380 /* validate whether the given class device is a partition or not */
381 if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
382 dprintf("%s not a partition\n", clsdev->name);
390 /* Now validate if the parent has the "dev" attribute */
391 memset(value, 0, 256);
392 strcat(parent_path, "/dev");
393 if ((sysfs_read_attribute_value(parent_path, value, 256)) != 0) {
394 dprintf("Block device %s does not have a parent\n",
399 c = strrchr(parent_path, '/');
404 clsdev->parent = sysfs_open_class_device(parent_path);
405 if (clsdev->parent == NULL) {
406 dprintf("Error opening the parent class device at %s\n",
413 dprintf("Invalid path %s\n", clsdev->path);
418 * sysfs_get_classdev_parent: Retrieves the parent of a class device.
419 * eg., when working with hda1, this function can be used to retrieve the
420 * sysfs_class_device for hda
422 * @clsdev: class device whose parent details are required.
423 * Returns sysfs_class_device of the parent on success, NULL on failure
425 struct sysfs_class_device *sysfs_get_classdev_parent
426 (struct sysfs_class_device *clsdev)
428 if (clsdev == NULL) {
432 if (clsdev->parent != NULL)
433 return (clsdev->parent);
436 * As of now, only block devices have a parent child heirarchy in sysfs
437 * We do not know, if, in the future, more classes will have a similar
438 * structure. Hence, we now call a specialized function for block and
439 * later we can add support functions for other subsystems as required.
441 if (!(strcmp(clsdev->classname, SYSFS_BLOCK_NAME))) {
442 if ((get_blockdev_parent(clsdev)) == 0)
443 return (clsdev->parent);
449 * get_classdev_path: given the class and a device in the class, return the
450 * absolute path to the device
451 * @classname: name of the class
452 * @clsdev: the class device
453 * @path: buffer to return path
454 * @psize: size of "path"
455 * Returns 0 on SUCCESS or -1 on error
457 static int get_classdev_path(const unsigned char *classname,
458 const unsigned char *clsdev, unsigned char *path, size_t len)
460 if (classname == NULL || clsdev == NULL || path == NULL) {
464 if (sysfs_get_mnt_path(path, len) != 0) {
465 dprintf("Error getting sysfs mount path\n");
469 if (sysfs_trailing_slash(path) == 0)
472 if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
473 strcat(path, SYSFS_BLOCK_NAME);
475 strcat(path, SYSFS_CLASS_NAME);
477 strcat(path, classname);
480 strcat(path, clsdev);
485 * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
486 * Class_device must be closed using sysfs_close_class_device
487 * @classname: Class to search
488 * @name: name of the class_device
491 * Call sysfs_close_class_device() to close the class device
493 struct sysfs_class_device *sysfs_open_class_device_by_name
494 (const unsigned char *classname, const unsigned char *name)
496 unsigned char devpath[SYSFS_PATH_MAX];
497 struct sysfs_class_device *cdev = NULL;
499 if (classname == NULL || name == NULL) {
504 memset(devpath, 0, SYSFS_PATH_MAX);
505 if ((get_classdev_path(classname, name, devpath,
506 SYSFS_PATH_MAX)) != 0) {
507 dprintf("Error getting to device %s on class %s\n",
512 cdev = sysfs_open_class_device(devpath);
514 dprintf("Error getting class device %s from class %s\n",
522 * sysfs_get_classdev_attributes: returns a dlist of attributes for
523 * the requested class_device
524 * @cdev: sysfs_class_dev for which attributes are needed
525 * returns a dlist of attributes if exists, NULL otherwise
527 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
532 if (cdev->directory == NULL) {
533 cdev->directory = sysfs_open_directory(cdev->path);
534 if (cdev->directory == NULL)
537 if (cdev->directory->attributes == NULL) {
538 if ((sysfs_read_dir_attributes(cdev->directory)) != 0) {
539 dprintf("Error reading attributes for directory %s\n",
540 cdev->directory->path);
544 if ((sysfs_path_is_dir(cdev->path)) != 0) {
545 dprintf("Class device at %s no longer exists\n",
549 if ((sysfs_refresh_attributes
550 (cdev->directory->attributes)) != 0) {
551 dprintf("Error refreshing classdev attributes\n");
555 return (cdev->directory->attributes);
559 * sysfs_get_classdev_attr: searches class device's attributes by name
560 * @clsdev: class device to look through
561 * @name: attribute name to get
562 * returns sysfs_attribute reference with success or NULL with error
564 struct sysfs_attribute *sysfs_get_classdev_attr
565 (struct sysfs_class_device *clsdev, const unsigned char *name)
567 struct sysfs_attribute *cur = NULL;
568 struct sysfs_directory *sdir = NULL;
569 struct dlist *attrlist = NULL;
571 if (clsdev == NULL || name == NULL) {
576 * First, see if it's in the current directory. Then look at
577 * subdirs since class devices can have subdirs of attributes.
579 attrlist = sysfs_get_classdev_attributes(clsdev);
580 if (attrlist == NULL)
582 cur = sysfs_get_directory_attribute(clsdev->directory,
583 (unsigned char *)name);
587 if (clsdev->directory->subdirs == NULL)
588 if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
589 clsdev->directory->subdirs == NULL)
592 dlist_for_each_data(clsdev->directory->subdirs, sdir,
593 struct sysfs_directory) {
594 cur = sysfs_get_directory_attribute(sdir,
595 (unsigned char *)name);
604 * sysfs_open_classdev_attr: read an attribute for a given class device
605 * @classname: name of the class on which to look
606 * @dev: class device name for which the attribute has to be read
607 * @attrib: attribute to read
608 * Returns sysfs_attribute * on SUCCESS and NULL on error
611 * A call to sysfs_close_attribute() is required to close the
612 * attribute returned and to free memory
614 struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
615 const unsigned char *dev, const unsigned char *attrib)
617 struct sysfs_attribute *attribute = NULL;
618 unsigned char path[SYSFS_PATH_MAX];
620 if (classname == NULL || dev == NULL || attrib == NULL) {
624 memset(path, 0, SYSFS_PATH_MAX);
625 if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
626 dprintf("Error getting to device %s on class %s\n",
631 strcat(path, attrib);
632 attribute = sysfs_open_attribute(path);
633 if (attribute == NULL) {
634 dprintf("Error opening attribute %s on class device %s\n",
638 if ((sysfs_read_attribute(attribute)) != 0) {
639 dprintf("Error reading attribute %s for class device %s\n",
641 sysfs_close_attribute(attribute);