1 /*
2  * libsysfs.h
3  *
4  * Header Definitions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2004-2005
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 #ifndef _LIBSYSFS_H_
24 #define _LIBSYSFS_H_
25 
26 #include <sys/types.h>
27 #include <string.h>
28 #include "dlist.h"
29 
30 #define SYSFS_FSTYPE_NAME	"sysfs"
31 #define SYSFS_PROC_MNTS		"/proc/mounts"
32 #define SYSFS_BUS_NAME		"bus"
33 #define SYSFS_CLASS_NAME	"class"
34 #define SYSFS_BLOCK_NAME	"block"
35 #define SYSFS_DEVICES_NAME	"devices"
36 #define SYSFS_DRIVERS_NAME	"drivers"
37 #define SYSFS_MODULE_NAME	"module"
38 #define SYSFS_NAME_ATTRIBUTE	"name"
39 #define SYSFS_MOD_PARM_NAME	"parameters"
40 #define SYSFS_MOD_SECT_NAME	"sections"
41 #define SYSFS_UNKNOWN		"unknown"
42 #define SYSFS_PATH_ENV		"SYSFS_PATH"
43 
44 #define SYSFS_PATH_MAX		256
45 #define	SYSFS_NAME_LEN		64
46 #define SYSFS_BUS_ID_SIZE	32
47 
48 /* mount path for sysfs, can be overridden by exporting SYSFS_PATH */
49 #define SYSFS_MNT_PATH		"/sys"
50 
51 enum sysfs_attribute_method {
52 	SYSFS_METHOD_SHOW =	0x01,	/* attr can be read by user */
53 	SYSFS_METHOD_STORE =	0x02,	/* attr can be changed by user */
54 };
55 
56 /*
57  * NOTE:
58  * 1. We have the statically allocated "name" as the first element of all
59  * the structures. This feature is used in the "sorter" function for dlists
60  * 2. As is the case with attrlist
61  * 3. As is the case with path
62  */
63 struct sysfs_attribute {
64 	char name[SYSFS_NAME_LEN];
65 	char path[SYSFS_PATH_MAX];
66 	char *value;
67 	unsigned short len;			/* value length */
68 	enum sysfs_attribute_method method;	/* show and store */
69 };
70 
71 struct sysfs_driver {
72 	char name[SYSFS_NAME_LEN];
73 	char path[SYSFS_PATH_MAX];
74 	struct dlist *attrlist;
75 	char bus[SYSFS_NAME_LEN];
76 
77 	/* Private: for internal use only */
78 	struct sysfs_module *module;
79 	struct dlist *devices;
80 };
81 
82 struct sysfs_device {
83 	char name[SYSFS_NAME_LEN];
84 	char path[SYSFS_PATH_MAX];
85 	struct dlist *attrlist;
86 	char bus_id[SYSFS_NAME_LEN];
87 	char bus[SYSFS_NAME_LEN];
88 	char driver_name[SYSFS_NAME_LEN];
89 	char subsystem[SYSFS_NAME_LEN];
90 
91 	/* Private: for internal use only */
92 	struct sysfs_device *parent;
93 	/* NOTE - we still don't populate this */
94 	struct dlist *children;
95 };
96 
97 struct sysfs_bus {
98 	char name[SYSFS_NAME_LEN];
99 	char path[SYSFS_PATH_MAX];
100 	struct dlist *attrlist;
101 
102 	/* Private: for internal use only */
103 	struct dlist *drivers;
104 	struct dlist *devices;
105 };
106 
107 struct sysfs_class_device {
108 	char name[SYSFS_NAME_LEN];
109 	char path[SYSFS_PATH_MAX];
110 	struct dlist *attrlist;
111 	char classname[SYSFS_NAME_LEN];
112 
113 	/* Private: for internal use only */
114 	struct sysfs_class_device *parent;
115 	struct sysfs_device *sysdevice;		/* NULL if virtual */
116 };
117 
118 struct sysfs_class {
119 	char name[SYSFS_NAME_LEN];
120 	char path[SYSFS_PATH_MAX];
121 	struct dlist *attrlist;
122 
123 	/* Private: for internal use only */
124 	struct dlist *devices;
125 };
126 
127 struct sysfs_module {
128 	char name[SYSFS_NAME_LEN];
129 	char path[SYSFS_PATH_MAX];
130 	struct dlist *attrlist;
131 	struct dlist *parmlist;
132 	struct dlist *sections;
133 };
134 
135 #ifdef __cplusplus
136 extern "C" {
137 #endif
138 
139 /*
140  * Function Prototypes
141  */
142 extern int sysfs_get_mnt_path(char *mnt_path, size_t len);
143 extern int sysfs_remove_trailing_slash(char *path);
144 extern int sysfs_get_name_from_path(const char *path, char *name, size_t len);
145 extern int sysfs_path_is_dir(const char *path);
146 extern int sysfs_path_is_link(const char *path);
147 extern int sysfs_path_is_file(const char *path);
148 extern int sysfs_get_link(const char *path, char *target, size_t len);
149 extern struct dlist *sysfs_open_directory_list(const char *path);
150 extern struct dlist *sysfs_open_link_list(const char *path);
151 extern void sysfs_close_list(struct dlist *list);
152 
153 /* sysfs directory and file access */
154 extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
155 extern struct sysfs_attribute *sysfs_open_attribute(const char *path);
156 extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
157 extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
158 		const char *new_value, size_t len);
159 extern struct sysfs_device *sysfs_read_dir_subdirs(const char *path);
160 /* sysfs driver access */
161 extern void sysfs_close_driver(struct sysfs_driver *driver);
162 extern struct sysfs_driver *sysfs_open_driver
163 	(const char *bus_name, const char *drv_name);
164 extern struct sysfs_driver *sysfs_open_driver_path(const char *path);
165 extern struct sysfs_attribute *sysfs_get_driver_attr
166 	(struct sysfs_driver *drv, const char *name);
167 extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *drv);
168 extern struct dlist *sysfs_get_driver_devices(struct sysfs_driver *drv);
169 extern struct sysfs_module *sysfs_get_driver_module(struct sysfs_driver *drv);
170 
171 /* generic sysfs device access */
172 extern void sysfs_close_device_tree(struct sysfs_device *device);
173 extern struct sysfs_device *sysfs_open_device_tree(const char *path);
174 extern void sysfs_close_device(struct sysfs_device *dev);
175 extern struct sysfs_device *sysfs_open_device
176 	(const char *bus, const char *bus_id);
177 extern struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev);
178 extern struct sysfs_device *sysfs_open_device_path(const char *path);
179 extern int sysfs_get_device_bus(struct sysfs_device *dev);
180 extern struct sysfs_attribute *sysfs_get_device_attr
181 	(struct sysfs_device *dev, const char *name);
182 extern struct dlist *sysfs_get_device_attributes
183 	(struct sysfs_device *dev);
184 
185 /* generic sysfs class access */
186 extern void sysfs_close_class_device(struct sysfs_class_device *dev);
187 extern struct sysfs_class_device *sysfs_open_class_device_path
188 	(const char *path);
189 extern struct sysfs_class_device *sysfs_open_class_device
190 	(const char *classname, const char *name);
191 extern struct sysfs_class_device *sysfs_get_classdev_parent
192 	(struct sysfs_class_device *clsdev);
193 extern struct sysfs_attribute *sysfs_get_classdev_attr
194 	(struct sysfs_class_device *clsdev, const char *name);
195 extern struct dlist *sysfs_get_classdev_attributes
196 	(struct sysfs_class_device *clsdev);
197 extern struct sysfs_device *sysfs_get_classdev_device
198 	(struct sysfs_class_device *clsdev);
199 extern void sysfs_close_class(struct sysfs_class *cls);
200 extern struct sysfs_class *sysfs_open_class(const char *name);
201 extern struct sysfs_class_device *sysfs_get_class_device
202 	(struct sysfs_class *cls, const char *name);
203 extern struct dlist *sysfs_get_class_devices(struct sysfs_class *cls);
204 
205 /* generic sysfs bus access */
206 extern void sysfs_close_bus(struct sysfs_bus *bus);
207 extern struct sysfs_bus *sysfs_open_bus(const char *name);
208 extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
209 extern struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus);
210 extern struct sysfs_device *sysfs_get_bus_device
211 	(struct sysfs_bus *bus, const char *id);
212 extern struct sysfs_driver *sysfs_get_bus_driver
213 	(struct sysfs_bus *bus, const char *drvname);
214 
215 /* generic sysfs module access */
216 extern void sysfs_close_module(struct sysfs_module *module);
217 extern struct sysfs_module *sysfs_open_module_path(const char *path);
218 extern struct sysfs_module *sysfs_open_module(const char *name);
219 extern struct dlist *sysfs_get_module_parms(struct sysfs_module *module);
220 extern struct dlist *sysfs_get_module_sections(struct sysfs_module *module);
221 extern struct dlist *sysfs_get_module_attributes(struct sysfs_module *module);
222 extern struct sysfs_attribute *sysfs_get_module_attr
223 	(struct sysfs_module *module, const char *name);
224 extern struct sysfs_attribute *sysfs_get_module_parm
225 	(struct sysfs_module *module, const char *parm);
226 extern struct sysfs_attribute *sysfs_get_module_section
227 	(struct sysfs_module *module, const char *section);
228 
229 /**
230  * sort_list: sorter function to keep list elements sorted in alphabetical
231  * 	order. Just does a strncmp as you can see :)
232  *
233  * Returns 1 if less than 0 otherwise
234  *
235  * NOTE: We take care to have a statically allocated "name" as the first
236  * 	lement of all libsysfs structures. Hence, this function will work
237  * 	AS IS for _ALL_ the lists that have to be sorted.
238  */
sort_list(void * new_elem,void * old_elem)239 static inline int sort_list(void *new_elem, void *old_elem)
240 {
241 	return ((strncmp(((struct sysfs_attribute *)new_elem)->name,
242 		((struct sysfs_attribute *)old_elem)->name,
243 		strlen(((struct sysfs_attribute *)new_elem)->name))) < 0 ? 1 : 0);
244 }
245 
246 
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif /* _LIBSYSFS_H_ */
252