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");
120 * We shall now treat "block" also as a class. Hence, check here
121 * if "name" is "block" and proceed accordingly
123 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
124 strcat(classpath, SYSFS_BLOCK_DIR);
126 strcat(classpath, SYSFS_CLASS_DIR);
127 strcat(classpath, "/");
128 strcat(classpath, name);
130 classdir = sysfs_open_directory(classpath);
131 if (classdir == NULL) {
133 dprintf("Class %s not supported on this system\n", name);
136 if ((sysfs_read_directory(classdir)) != 0) {
137 dprintf("Error reading %s class dir %s\n", name, classpath);
138 sysfs_close_directory(classdir);
146 * set_classdev_classname: Grabs classname from path
147 * @cdev: class device to set
150 static void set_classdev_classname(struct sysfs_class_device *cdev)
152 unsigned char *c = NULL, *e = NULL;
155 c = strstr(cdev->path, SYSFS_CLASS_DIR);
157 c = strstr(cdev->path, SYSFS_BLOCK_DIR);
160 while (c != NULL && *c != '/')
165 strcpy(cdev->classname, SYSFS_UNKNOWN);
170 while (e != NULL && *e != '/' && *e != '\0') {
174 strncpy(cdev->classname, c, count);
179 * sysfs_open_class_device: Opens and populates class device
180 * @path: path to class device.
181 * returns struct sysfs_class_device with success and NULL with error.
183 struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
185 struct sysfs_class_device *cdev = NULL;
186 struct sysfs_directory *dir = NULL;
187 struct sysfs_link *curl = NULL;
188 struct sysfs_device *sdev = NULL;
189 struct sysfs_driver *drv = NULL;
195 cdev = alloc_class_device();
197 dprintf("calloc failed\n");
200 if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
202 dprintf("Invalid class device path %s\n", path);
203 sysfs_close_class_device(cdev);
207 dir = sysfs_open_directory(path);
209 dprintf("Error opening class device at %s\n", path);
210 sysfs_close_class_device(cdev);
213 if ((sysfs_read_directory(dir)) != 0) {
214 dprintf("Error reading class device at %s\n", path);
215 sysfs_close_directory(dir);
216 sysfs_close_class_device(cdev);
219 sysfs_read_all_subdirs(dir);
220 cdev->directory = dir;
221 strcpy(cdev->path, dir->path);
222 set_classdev_classname(cdev);
224 /* get driver and device, if implemented */
225 if (cdev->directory->links != NULL) {
226 dlist_for_each_data(cdev->directory->links, curl,
228 if (strncmp(curl->name, SYSFS_DEVICES_NAME, 6) == 0) {
229 sdev = sysfs_open_device(curl->target);
231 cdev->sysdevice = sdev;
232 if (cdev->driver != NULL)
233 strncpy(sdev->driver_name,
237 } else if (strncmp(curl->name,
238 SYSFS_DRIVERS_NAME, 6) == 0) {
239 drv = sysfs_open_driver(curl->target);
242 if (cdev->sysdevice != NULL) {
243 strncpy(cdev->sysdevice->name,
246 if (drv->devices == NULL)
251 dlist_unshift(drv->devices,
262 * get_all_class_devices: gets all devices for class
263 * @class: class to get devices for
264 * returns 0 with success and -1 with failure
266 static int get_all_class_devices(struct sysfs_class *cls)
268 struct sysfs_class_device *dev = NULL;
269 struct sysfs_directory *cur = NULL;
271 if (cls == NULL || cls->directory == NULL) {
275 if (cls->directory->subdirs == NULL)
277 dlist_for_each_data(cls->directory->subdirs, cur,
278 struct sysfs_directory) {
279 dev = sysfs_open_class_device(cur->path);
281 dprintf("Error opening device at %s\n", cur->path);
284 if (cls->devices == NULL)
285 cls->devices = dlist_new_with_delete
286 (sizeof(struct sysfs_class_device),
287 sysfs_close_cls_dev);
288 dlist_unshift(cls->devices, dev);
294 * sysfs_open_class: opens specific class and all its devices on system
295 * returns sysfs_class structure with success or NULL with error.
297 struct sysfs_class *sysfs_open_class(const unsigned char *name)
299 struct sysfs_class *cls = NULL;
300 struct sysfs_directory *classdir = NULL;
309 dprintf("calloc failed\n");
312 strcpy(cls->name, name);
313 classdir = open_class_dir(name);
314 if (classdir == NULL) {
315 dprintf("Invalid class, %s not supported on this system\n",
317 sysfs_close_class(cls);
320 cls->directory = classdir;
321 strcpy(cls->path, classdir->path);
322 if ((get_all_class_devices(cls)) != 0) {
323 dprintf("Error reading %s class devices\n", name);
324 sysfs_close_class(cls);
332 * sysfs_get_class_device: Get specific class device using the device's id
333 * @class: class to find device on
334 * @name: class name of the device
336 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
339 if (class == NULL || name == NULL) {
344 return (struct sysfs_class_device *)dlist_find_custom(class->devices,
345 name, class_name_equal);
349 * get_classdev_path: given the class and a device in the class, return the
350 * absolute path to the device
351 * @classname: name of the class
352 * @clsdev: the class device
353 * @path: buffer to return path
354 * @psize: size of "path"
355 * Returns 0 on SUCCESS or -1 on error
357 static int get_classdev_path(const unsigned char *classname,
358 const unsigned char *clsdev, unsigned char *path, size_t len)
360 if (classname == NULL || clsdev == NULL || path == NULL) {
364 if (sysfs_get_mnt_path(path, len) != 0) {
365 dprintf("Error getting sysfs mount path\n");
368 if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
369 strcat(path, SYSFS_BLOCK_DIR);
371 strcat(path, SYSFS_CLASS_DIR);
373 strcat(path, classname);
376 strcat(path, clsdev);
381 * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
382 * Class_device must be closed using sysfs_close_class_device
383 * @classname: Class to search
384 * @name: name of the class_device
387 * Call sysfs_close_class_device() to close the class device
389 struct sysfs_class_device *sysfs_open_class_device_by_name
390 (const unsigned char *classname, const unsigned char *name)
392 unsigned char devpath[SYSFS_PATH_MAX];
393 struct sysfs_class_device *cdev = NULL;
395 if (classname == NULL || name == NULL) {
400 memset(devpath, 0, SYSFS_PATH_MAX);
401 if ((get_classdev_path(classname, name, devpath,
402 SYSFS_PATH_MAX)) != 0) {
403 dprintf("Error getting to device %s on class %s\n",
408 cdev = sysfs_open_class_device(devpath);
410 dprintf("Error getting class device %s from class %s\n",
418 * sysfs_get_classdev_attributes: returns a dlist of attributes for
419 * the requested class_device
420 * @cdev: sysfs_class_dev for which attributes are needed
421 * returns a dlist of attributes if exists, NULL otherwise
423 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
425 if (cdev == NULL || cdev->directory == NULL)
428 return (cdev->directory->attributes);
432 * sysfs_get_classdev_attr: searches class device's attributes by name
433 * @clsdev: class device to look through
434 * @name: attribute name to get
435 * returns sysfs_attribute reference with success or NULL with error
437 struct sysfs_attribute *sysfs_get_classdev_attr
438 (struct sysfs_class_device *clsdev, const unsigned char *name)
440 struct sysfs_attribute *cur = NULL;
442 if (clsdev == NULL || clsdev->directory == NULL ||
443 clsdev->directory->attributes == NULL || name == NULL) {
448 cur = sysfs_get_directory_attribute(clsdev->directory,
449 (unsigned char *)name);
457 * sysfs_open_classdev_attr: read an attribute for a given class device
458 * @classname: name of the class on which to look
459 * @dev: class device name for which the attribute has to be read
460 * @attrib: attribute to read
461 * Returns sysfs_attribute * on SUCCESS and NULL on error
464 * A call to sysfs_close_attribute() is required to close the
465 * attribute returned and to free memory
467 struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
468 const unsigned char *dev, const unsigned char *attrib)
470 struct sysfs_attribute *attribute = NULL;
471 unsigned char path[SYSFS_PATH_MAX];
473 if (classname == NULL || dev == NULL || attrib == NULL) {
477 memset(path, 0, SYSFS_PATH_MAX);
478 if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
479 dprintf("Error getting to device %s on class %s\n",
484 strcat(path, attrib);
485 attribute = sysfs_open_attribute(path);
486 if (attribute == NULL) {
487 dprintf("Error opening attribute %s on class device %s\n",
491 if ((sysfs_read_attribute(attribute)) != 0) {
492 dprintf("Error reading attribute %s for class device %s\n",
494 sysfs_close_attribute(attribute);