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_path: 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_path
138 (const unsigned char *path)
140 struct sysfs_class_device *cdev = NULL;
146 if ((sysfs_path_is_dir(path)) != 0) {
147 dprintf("%s is not a valid path to a class device\n", path);
150 cdev = alloc_class_device();
152 dprintf("calloc failed\n");
155 if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
157 dprintf("Error getting class device name\n");
158 sysfs_close_class_device(cdev);
162 strcpy(cdev->path, path);
163 if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
164 dprintf("Invalid path to class device %s\n", cdev->path);
165 sysfs_close_class_device(cdev);
168 set_classdev_classname(cdev);
174 * sysfs_get_class_devices: gets all devices for class
175 * @class: class to get devices for
176 * returns dlist of class_devices with success and NULL with error
178 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
180 struct sysfs_class_device *dev = NULL;
181 struct sysfs_directory *cur = NULL;
188 if (cls->devices != NULL)
191 if (cls->directory == NULL) {
192 cls->directory = sysfs_open_directory(cls->path);
193 if (cls->directory == NULL)
197 if ((sysfs_read_dir_subdirs(cls->directory)) != 0)
200 if (cls->directory->subdirs != NULL) {
201 dlist_for_each_data(cls->directory->subdirs, cur,
202 struct sysfs_directory) {
203 dev = sysfs_open_class_device_path(cur->path);
205 dprintf("Error opening device at %s\n",
209 if (cls->devices == NULL)
210 cls->devices = dlist_new_with_delete
211 (sizeof(struct sysfs_class_device),
212 sysfs_close_cls_dev);
213 dlist_unshift(cls->devices, dev);
220 * sysfs_open_class: opens specific class and all its devices on system
221 * returns sysfs_class structure with success or NULL with error.
223 struct sysfs_class *sysfs_open_class(const unsigned char *name)
225 struct sysfs_class *cls = NULL;
226 unsigned char classpath[SYSFS_PATH_MAX];
233 memset(classpath, 0, SYSFS_PATH_MAX);
234 if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
235 dprintf("Sysfs not supported on this system\n");
240 * We shall now treat "block" also as a class. Hence, check here
241 * if "name" is "block" and proceed accordingly
243 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
244 strcat(classpath, "/");
245 strcat(classpath, SYSFS_BLOCK_NAME);
247 strcat(classpath, "/");
248 strcat(classpath, SYSFS_CLASS_NAME);
249 strcat(classpath, "/");
250 strcat(classpath, name);
252 if ((sysfs_path_is_dir(classpath)) != 0) {
253 dprintf("Class %s not found on the system\n", name);
259 dprintf("calloc failed\n");
262 strcpy(cls->name, name);
263 strcpy(cls->path, classpath);
264 if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
265 dprintf("Invalid path to class device %s\n", cls->path);
266 sysfs_close_class(cls);
274 * sysfs_get_class_device: Get specific class device using the device's id
275 * @class: class to find device on
276 * @name: class name of the device
278 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
281 if (class == NULL || name == NULL) {
286 if (class->devices == NULL) {
287 class->devices = sysfs_get_class_devices(class);
288 if (class->devices == NULL)
291 return (struct sysfs_class_device *)dlist_find_custom(class->devices,
292 name, class_name_equal);
296 * sysfs_get_classdev_device: returns the sysfs_device corresponding to
297 * sysfs_class_device, if present
298 * @clsdev: class device whose sysfs_device is required
299 * Returns sysfs_device on success, NULL on error or if device is not
302 struct sysfs_device *sysfs_get_classdev_device
303 (struct sysfs_class_device *clsdev)
305 struct sysfs_link *devlink = NULL;
306 unsigned char devpath[SYSFS_PATH_MAX];
308 if (clsdev == NULL) {
312 strcpy(devpath, clsdev->path);
313 strcat(devpath, "/device");
314 if ((sysfs_path_is_link(devpath)) != 0) {
315 if (clsdev->sysdevice != NULL) {
316 sysfs_close_device(clsdev->sysdevice);
317 clsdev->sysdevice = NULL;
322 if (clsdev->directory == NULL) {
323 clsdev->directory = sysfs_open_directory(clsdev->path);
324 if (clsdev->directory == NULL)
327 devlink = sysfs_get_directory_link(clsdev->directory, "device");
328 if (devlink == NULL) {
329 if (clsdev->sysdevice != NULL) {
330 dprintf("Device link no longer exists\n");
331 sysfs_close_device(clsdev->sysdevice);
332 clsdev->sysdevice = NULL;
337 if (clsdev->sysdevice != NULL) {
338 if (!strncmp(devlink->target, clsdev->sysdevice->path,
340 /* sysdevice hasn't changed */
341 return (clsdev->sysdevice);
343 /* come here only if the device link for has changed */
344 sysfs_close_device(clsdev->sysdevice);
347 clsdev->sysdevice = sysfs_open_device_path(devlink->target);
348 if (clsdev->sysdevice == NULL)
350 if (clsdev->driver != NULL)
351 strcpy(clsdev->sysdevice->driver_name, clsdev->driver->name);
353 return (clsdev->sysdevice);
357 * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to
358 * sysfs_class_device, if present
359 * @clsdev: class device whose sysfs_device is required
360 * Returns sysfs_driver on success, NULL on error or if driver is not
363 struct sysfs_driver *sysfs_get_classdev_driver
364 (struct sysfs_class_device *clsdev)
366 struct sysfs_link *drvlink = NULL;
367 unsigned char drvpath[SYSFS_PATH_MAX];
369 if (clsdev == NULL) {
373 strcpy(drvpath, clsdev->path);
374 strcat(drvpath, "/driver");
375 if ((sysfs_path_is_link(drvpath)) != 0) {
376 if (clsdev->driver != NULL) {
377 sysfs_close_driver(clsdev->driver);
378 clsdev->driver = NULL;
383 if (clsdev->directory == NULL) {
384 clsdev->directory = sysfs_open_directory(clsdev->path);
385 if (clsdev->directory == NULL)
388 drvlink = sysfs_get_directory_link(clsdev->directory, "driver");
389 if (drvlink == NULL) {
390 if (clsdev->driver != NULL) {
391 dprintf("Driver link no longer exists\n");
392 sysfs_close_driver(clsdev->driver);
393 clsdev->driver = NULL;
397 if (clsdev->driver != NULL) {
398 if (!strncmp(drvlink->target, clsdev->driver->path,
400 /* driver hasn't changed */
401 return (clsdev->driver);
403 /* come here only if the device link for has changed */
404 sysfs_close_driver(clsdev->driver);
407 clsdev->driver = sysfs_open_driver_path(drvlink->target);
408 if (clsdev->driver == NULL)
410 if (clsdev->sysdevice != NULL)
411 strcpy(clsdev->sysdevice->driver_name, clsdev->driver->name);
413 return (clsdev->driver);
417 * get_blockdev_parent: Get the parent class device for a "block" subsystem
419 * @clsdev: block subsystem class device whose parent needs to be found
420 * Returns 0 on success and 1 on error
422 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
424 unsigned char parent_path[SYSFS_PATH_MAX], *c = NULL;
426 strcpy(parent_path, clsdev->path);
427 c = strstr(parent_path, SYSFS_BLOCK_NAME);
429 dprintf("Class device %s does not belong to BLOCK subsystem\n",
434 c += strlen(SYSFS_BLOCK_NAME);
440 /* validate whether the given class device is a partition or not */
441 if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
442 dprintf("%s not a partition\n", clsdev->name);
452 clsdev->parent = sysfs_open_class_device_path(parent_path);
453 if (clsdev->parent == NULL) {
454 dprintf("Error opening the parent class device at %s\n",
461 dprintf("Invalid path %s\n", clsdev->path);
466 * sysfs_get_classdev_parent: Retrieves the parent of a class device.
467 * eg., when working with hda1, this function can be used to retrieve the
468 * sysfs_class_device for hda
470 * @clsdev: class device whose parent details are required.
471 * Returns sysfs_class_device of the parent on success, NULL on failure
473 struct sysfs_class_device *sysfs_get_classdev_parent
474 (struct sysfs_class_device *clsdev)
476 if (clsdev == NULL) {
480 if (clsdev->parent != NULL)
481 return (clsdev->parent);
484 * As of now, only block devices have a parent child heirarchy in sysfs
485 * We do not know, if, in the future, more classes will have a similar
486 * structure. Hence, we now call a specialized function for block and
487 * later we can add support functions for other subsystems as required.
489 if (!(strcmp(clsdev->classname, SYSFS_BLOCK_NAME))) {
490 if ((get_blockdev_parent(clsdev)) == 0)
491 return (clsdev->parent);
497 * get_classdev_path: given the class and a device in the class, return the
498 * absolute path to the device
499 * @classname: name of the class
500 * @clsdev: the class device
501 * @path: buffer to return path
502 * @psize: size of "path"
503 * Returns 0 on SUCCESS or -1 on error
505 static int get_classdev_path(const unsigned char *classname,
506 const unsigned char *clsdev, unsigned char *path, size_t len)
508 if (classname == NULL || clsdev == NULL || path == NULL) {
512 if (sysfs_get_mnt_path(path, len) != 0) {
513 dprintf("Error getting sysfs mount path\n");
516 if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
518 strcat(path, SYSFS_BLOCK_NAME);
521 strcat(path, SYSFS_CLASS_NAME);
523 strcat(path, classname);
526 strcat(path, clsdev);
531 * sysfs_open_class_device: Locates a specific class_device and returns it.
532 * Class_device must be closed using sysfs_close_class_device
533 * @classname: Class to search
534 * @name: name of the class_device
537 * Call sysfs_close_class_device() to close the class device
539 struct sysfs_class_device *sysfs_open_class_device
540 (const unsigned char *classname, const unsigned char *name)
542 unsigned char devpath[SYSFS_PATH_MAX];
543 struct sysfs_class_device *cdev = NULL;
545 if (classname == NULL || name == NULL) {
550 memset(devpath, 0, SYSFS_PATH_MAX);
551 if ((get_classdev_path(classname, name, devpath,
552 SYSFS_PATH_MAX)) != 0) {
553 dprintf("Error getting to device %s on class %s\n",
558 cdev = sysfs_open_class_device_path(devpath);
560 dprintf("Error getting class device %s from class %s\n",
568 * sysfs_get_classdev_attributes: returns a dlist of attributes for
569 * the requested class_device
570 * @cdev: sysfs_class_dev for which attributes are needed
571 * returns a dlist of attributes if exists, NULL otherwise
573 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
578 if (cdev->directory == NULL) {
579 cdev->directory = sysfs_open_directory(cdev->path);
580 if (cdev->directory == NULL)
583 if (cdev->directory->attributes == NULL) {
584 if ((sysfs_read_dir_attributes(cdev->directory)) != 0)
587 return (cdev->directory->attributes);
591 * sysfs_refresh_clsassdev_attributes: refreshes the driver's list of attributes
592 * @clsdev: sysfs_class_device whose attributes to refresh
594 * NOTE: Upon return, prior references to sysfs_attributes for this classdev
597 * Returns list of attributes on success and NULL on failure
599 struct dlist *sysfs_refresh_classdev_attributes
600 (struct sysfs_class_device *clsdev)
602 if (clsdev == NULL) {
607 if (clsdev->directory == NULL)
608 return (sysfs_get_classdev_attributes(clsdev));
610 if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) {
611 dprintf("Error refreshing class_device attributes\n");
615 return (clsdev->directory->attributes);
619 * sysfs_get_classdev_attr: searches class device's attributes by name
620 * @clsdev: class device to look through
621 * @name: attribute name to get
622 * returns sysfs_attribute reference with success or NULL with error
624 struct sysfs_attribute *sysfs_get_classdev_attr
625 (struct sysfs_class_device *clsdev, const unsigned char *name)
627 struct sysfs_attribute *cur = NULL;
628 struct sysfs_directory *sdir = NULL;
629 struct dlist *attrlist = NULL;
631 if (clsdev == NULL || name == NULL) {
637 * First, see if it's in the current directory. Then look at
638 * subdirs since class devices can have subdirs of attributes.
640 attrlist = sysfs_get_classdev_attributes(clsdev);
641 if (attrlist != NULL) {
642 cur = sysfs_get_directory_attribute(clsdev->directory,
643 (unsigned char *)name);
648 if (clsdev->directory->subdirs == NULL)
649 if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
650 clsdev->directory->subdirs == NULL)
653 if (clsdev->directory->subdirs != NULL) {
654 dlist_for_each_data(clsdev->directory->subdirs, sdir,
655 struct sysfs_directory) {
656 if ((sysfs_path_is_dir(sdir->path)) != 0)
658 cur = sysfs_get_directory_attribute(sdir,
659 (unsigned char *)name);
668 * sysfs_open_classdev_attr: read an attribute for a given class device
669 * @classname: name of the class on which to look
670 * @dev: class device name for which the attribute has to be read
671 * @attrib: attribute to read
672 * Returns sysfs_attribute * on SUCCESS and NULL on error
675 * A call to sysfs_close_attribute() is required to close the
676 * attribute returned and to free memory
678 struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
679 const unsigned char *dev, const unsigned char *attrib)
681 struct sysfs_attribute *attribute = NULL;
682 unsigned char path[SYSFS_PATH_MAX];
684 if (classname == NULL || dev == NULL || attrib == NULL) {
688 memset(path, 0, SYSFS_PATH_MAX);
689 if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
690 dprintf("Error getting to device %s on class %s\n",
695 strcat(path, attrib);
696 attribute = sysfs_open_attribute(path);
697 if (attribute == NULL) {
698 dprintf("Error opening attribute %s on class device %s\n",
702 if ((sysfs_read_attribute(attribute)) != 0) {
703 dprintf("Error reading attribute %s for class device %s\n",
705 sysfs_close_attribute(attribute);