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);
66 * sysfs_close_class: close single class
67 * @class: class structure
69 void sysfs_close_class(struct sysfs_class *cls)
72 if (cls->directory != NULL)
73 sysfs_close_directory(cls->directory);
74 if (cls->devices != NULL)
75 dlist_destroy(cls->devices);
81 * alloc_class_device: mallocs and initializes new class device struct.
82 * returns sysfs_class_device or NULL.
84 static struct sysfs_class_device *alloc_class_device(void)
86 return (struct sysfs_class_device *)
87 calloc(1, sizeof(struct sysfs_class_device));
91 * alloc_class: mallocs new class structure
92 * returns sysfs_class struct or NULL
94 static struct sysfs_class *alloc_class(void)
96 return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
100 * open_class_dir: opens up sysfs class directory
101 * returns sysfs_directory struct with success and NULL with error
103 static struct sysfs_directory *open_class_dir(const unsigned char *name)
105 struct sysfs_directory *classdir = NULL;
106 unsigned char classpath[SYSFS_PATH_MAX];
113 memset(classpath, 0, SYSFS_PATH_MAX);
114 if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
115 dprintf("Sysfs not supported on this system\n");
119 if (sysfs_trailing_slash(classpath) == 0)
120 strcat(classpath, "/");
122 * We shall now treat "block" also as a class. Hence, check here
123 * if "name" is "block" and proceed accordingly
125 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
126 strcat(classpath, SYSFS_BLOCK_NAME);
128 strcat(classpath, SYSFS_CLASS_NAME);
129 strcat(classpath, "/");
130 strcat(classpath, name);
132 classdir = sysfs_open_directory(classpath);
133 if (classdir == NULL) {
135 dprintf("Class %s not supported on this system\n", name);
138 if ((sysfs_read_directory(classdir)) != 0) {
139 dprintf("Error reading %s class dir %s\n", name, classpath);
140 sysfs_close_directory(classdir);
148 * set_classdev_classname: Grabs classname from path
149 * @cdev: class device to set
152 static void set_classdev_classname(struct sysfs_class_device *cdev)
154 unsigned char *c = NULL, *e = NULL;
157 c = strstr(cdev->path, SYSFS_CLASS_DIR);
159 c = strstr(cdev->path, SYSFS_BLOCK_DIR);
162 while (c != NULL && *c != '/')
167 strcpy(cdev->classname, SYSFS_UNKNOWN);
172 while (e != NULL && *e != '/' && *e != '\0') {
176 strncpy(cdev->classname, c, count);
181 * sysfs_open_class_device: Opens and populates class device
182 * @path: path to class device.
183 * returns struct sysfs_class_device with success and NULL with error.
185 struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
187 struct sysfs_class_device *cdev = NULL;
188 struct sysfs_directory *dir = NULL;
189 struct sysfs_link *curl = NULL;
190 struct sysfs_device *sdev = NULL;
191 struct sysfs_driver *drv = NULL;
197 cdev = alloc_class_device();
199 dprintf("calloc failed\n");
202 if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
204 dprintf("Invalid class device path %s\n", path);
205 sysfs_close_class_device(cdev);
209 dir = sysfs_open_directory(path);
211 dprintf("Error opening class device at %s\n", path);
212 sysfs_close_class_device(cdev);
215 if ((sysfs_read_directory(dir)) != 0) {
216 dprintf("Error reading class device at %s\n", path);
217 sysfs_close_directory(dir);
218 sysfs_close_class_device(cdev);
221 sysfs_read_all_subdirs(dir);
222 cdev->directory = dir;
223 strcpy(cdev->path, dir->path);
224 set_classdev_classname(cdev);
226 /* get driver and device, if implemented */
227 if (cdev->directory->links != NULL) {
228 dlist_for_each_data(cdev->directory->links, curl,
230 if (strncmp(curl->name, SYSFS_DEVICES_NAME, 6) == 0) {
231 sdev = sysfs_open_device(curl->target);
233 cdev->sysdevice = sdev;
234 if (cdev->driver != NULL)
235 strncpy(sdev->driver_name,
239 } else if (strncmp(curl->name,
240 SYSFS_DRIVERS_NAME, 6) == 0) {
241 drv = sysfs_open_driver(curl->target);
244 if (cdev->sysdevice != NULL) {
245 strncpy(cdev->sysdevice->name,
248 if (drv->devices == NULL)
253 dlist_unshift(drv->devices,
264 * get_all_class_devices: gets all devices for class
265 * @class: class to get devices for
266 * returns 0 with success and -1 with failure
268 static int get_all_class_devices(struct sysfs_class *cls)
270 struct sysfs_class_device *dev = NULL;
271 struct sysfs_directory *cur = NULL;
273 if (cls == NULL || cls->directory == NULL) {
277 if (cls->directory->subdirs == NULL)
279 dlist_for_each_data(cls->directory->subdirs, cur,
280 struct sysfs_directory) {
281 dev = sysfs_open_class_device(cur->path);
283 dprintf("Error opening device at %s\n", cur->path);
286 if (cls->devices == NULL)
287 cls->devices = dlist_new_with_delete
288 (sizeof(struct sysfs_class_device),
289 sysfs_close_cls_dev);
290 dlist_unshift(cls->devices, dev);
296 * sysfs_open_class: opens specific class and all its devices on system
297 * returns sysfs_class structure with success or NULL with error.
299 struct sysfs_class *sysfs_open_class(const unsigned char *name)
301 struct sysfs_class *cls = NULL;
302 struct sysfs_directory *classdir = NULL;
311 dprintf("calloc failed\n");
314 strcpy(cls->name, name);
315 classdir = open_class_dir(name);
316 if (classdir == NULL) {
317 dprintf("Invalid class, %s not supported on this system\n",
319 sysfs_close_class(cls);
322 cls->directory = classdir;
323 strcpy(cls->path, classdir->path);
324 if ((get_all_class_devices(cls)) != 0) {
325 dprintf("Error reading %s class devices\n", name);
326 sysfs_close_class(cls);
334 * sysfs_get_class_device: Get specific class device using the device's id
335 * @class: class to find device on
336 * @name: class name of the device
338 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
341 if (class == NULL || name == NULL) {
346 return (struct sysfs_class_device *)dlist_find_custom(class->devices,
347 name, class_name_equal);
351 * get_classdev_path: given the class and a device in the class, return the
352 * absolute path to the device
353 * @classname: name of the class
354 * @clsdev: the class device
355 * @path: buffer to return path
356 * @psize: size of "path"
357 * Returns 0 on SUCCESS or -1 on error
359 static int get_classdev_path(const unsigned char *classname,
360 const unsigned char *clsdev, unsigned char *path, size_t len)
362 if (classname == NULL || clsdev == NULL || path == NULL) {
366 if (sysfs_get_mnt_path(path, len) != 0) {
367 dprintf("Error getting sysfs mount path\n");
370 if (sysfs_trailing_slash(path) == 0)
373 if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
374 strcat(path, SYSFS_BLOCK_NAME);
376 strcat(path, SYSFS_CLASS_NAME);
378 strcat(path, classname);
381 strcat(path, clsdev);
386 * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
387 * Class_device must be closed using sysfs_close_class_device
388 * @classname: Class to search
389 * @name: name of the class_device
392 * Call sysfs_close_class_device() to close the class device
394 struct sysfs_class_device *sysfs_open_class_device_by_name
395 (const unsigned char *classname, const unsigned char *name)
397 unsigned char devpath[SYSFS_PATH_MAX];
398 struct sysfs_class_device *cdev = NULL;
400 if (classname == NULL || name == NULL) {
405 memset(devpath, 0, SYSFS_PATH_MAX);
406 if ((get_classdev_path(classname, name, devpath,
407 SYSFS_PATH_MAX)) != 0) {
408 dprintf("Error getting to device %s on class %s\n",
413 cdev = sysfs_open_class_device(devpath);
415 dprintf("Error getting class device %s from class %s\n",
423 * sysfs_get_classdev_attributes: returns a dlist of attributes for
424 * the requested class_device
425 * @cdev: sysfs_class_dev for which attributes are needed
426 * returns a dlist of attributes if exists, NULL otherwise
428 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
430 if (cdev == NULL || cdev->directory == NULL)
433 return (cdev->directory->attributes);
437 * sysfs_get_classdev_attr: searches class device's attributes by name
438 * @clsdev: class device to look through
439 * @name: attribute name to get
440 * returns sysfs_attribute reference with success or NULL with error
442 struct sysfs_attribute *sysfs_get_classdev_attr
443 (struct sysfs_class_device *clsdev, const unsigned char *name)
445 struct sysfs_attribute *cur = NULL;
447 if (clsdev == NULL || clsdev->directory == NULL ||
448 clsdev->directory->attributes == NULL || name == NULL) {
453 cur = sysfs_get_directory_attribute(clsdev->directory,
454 (unsigned char *)name);
462 * sysfs_open_classdev_attr: read an attribute for a given class device
463 * @classname: name of the class on which to look
464 * @dev: class device name for which the attribute has to be read
465 * @attrib: attribute to read
466 * Returns sysfs_attribute * on SUCCESS and NULL on error
469 * A call to sysfs_close_attribute() is required to close the
470 * attribute returned and to free memory
472 struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
473 const unsigned char *dev, const unsigned char *attrib)
475 struct sysfs_attribute *attribute = NULL;
476 unsigned char path[SYSFS_PATH_MAX];
478 if (classname == NULL || dev == NULL || attrib == NULL) {
482 memset(path, 0, SYSFS_PATH_MAX);
483 if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
484 dprintf("Error getting to device %s on class %s\n",
489 strcat(path, attrib);
490 attribute = sysfs_open_attribute(path);
491 if (attribute == NULL) {
492 dprintf("Error opening attribute %s on class device %s\n",
496 if ((sysfs_read_attribute(attribute)) != 0) {
497 dprintf("Error reading attribute %s for class device %s\n",
499 sysfs_close_attribute(attribute);