chiark / gitweb /
ccb9898cb534e27484a4aecce4a85cef04a8105e
[elogind.git] / libsysfs / libsysfs.h
1 /*
2  * libsysfs.h
3  *
4  * Header Definitions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003
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 "dlist.h"
28
29 /*
30  * Generic #defines go here..
31  */ 
32 #define SYSFS_FSTYPE_NAME       "sysfs"
33 #define SYSFS_PROC_MNTS         "/proc/mounts"
34 #define SYSFS_BUS_DIR           "/bus"
35 #define SYSFS_CLASS_DIR         "/class"
36 #define SYSFS_BLOCK_DIR         "/block"
37 #define SYSFS_DEVICES_DIR       "/devices"
38 #define SYSFS_DEVICES_NAME      "devices"
39 #define SYSFS_DRIVERS_DIR       "/drivers"
40 #define SYSFS_DRIVERS_NAME      "drivers"
41 #define SYSFS_NAME_ATTRIBUTE    "name"
42 #define SYSFS_UNKNOWN           "unknown"
43
44 /* Some "block" subsystem specific #defines */
45 #define SYSFS_QUEUE_NAME        "queue"
46 #define SYSFS_IOSCHED_NAME      "iosched"
47
48 #define SYSFS_PATH_MAX          255
49 #define SYSFS_NAME_LEN          50
50 #define SYSFS_BUS_ID_SIZE       20
51
52 #define SYSFS_METHOD_SHOW       0x01    /* attr can be read by user */
53 #define SYSFS_METHOD_STORE      0x02    /* attr can be changed by user */
54
55 struct sysfs_attribute {
56         unsigned char *value;
57         unsigned short len;             /* value length */
58         unsigned short method;          /* show and store */
59         unsigned char name[SYSFS_NAME_LEN];
60         unsigned char path[SYSFS_PATH_MAX];
61 };
62
63 struct sysfs_link {
64         unsigned char name[SYSFS_NAME_LEN];
65         unsigned char path[SYSFS_PATH_MAX];
66         unsigned char target[SYSFS_PATH_MAX];
67 };
68
69 struct sysfs_directory {
70         struct dlist *subdirs;  
71         struct dlist *links;            
72         struct dlist *attributes;
73         unsigned char name[SYSFS_NAME_LEN];
74         unsigned char path[SYSFS_PATH_MAX];
75 };
76
77 struct sysfs_driver {
78         struct dlist *devices;
79         unsigned char name[SYSFS_NAME_LEN];
80         unsigned char path[SYSFS_PATH_MAX];
81
82         /* for internal use only */
83         struct sysfs_directory *directory;      
84 };
85
86 struct sysfs_device {
87         struct sysfs_device *parent;            
88         struct dlist *children; 
89         unsigned char name[SYSFS_NAME_LEN];
90         unsigned char bus_id[SYSFS_NAME_LEN];
91         unsigned char path[SYSFS_PATH_MAX];
92         unsigned char driver_name[SYSFS_NAME_LEN];
93
94         /* for internal use only */
95         struct sysfs_directory *directory;      
96 };
97
98 struct sysfs_root_device {
99         struct dlist *devices;
100         unsigned char name[SYSFS_NAME_LEN];
101         unsigned char path[SYSFS_PATH_MAX];
102
103         /* for internal use only */
104         struct sysfs_directory *directory;
105 };
106
107 struct sysfs_bus {
108         struct dlist *drivers;
109         struct dlist *devices;
110         unsigned char name[SYSFS_NAME_LEN];
111         unsigned char path[SYSFS_PATH_MAX];
112
113         /* internal use only */
114         struct sysfs_directory *directory;      
115 };
116
117 struct sysfs_class_device {
118         struct sysfs_device *sysdevice;         /* NULL if virtual */
119         struct sysfs_driver *driver;            /* NULL if not implemented */
120         unsigned char name[SYSFS_NAME_LEN];
121         unsigned char path[SYSFS_PATH_MAX];
122
123         /* for internal use only */
124         struct sysfs_directory *directory;      
125 };
126
127 struct sysfs_class {
128         struct dlist *devices;
129         unsigned char name[SYSFS_NAME_LEN];
130         unsigned char path[SYSFS_PATH_MAX];
131
132         /* for internal use only */
133         struct sysfs_directory *directory;      
134 };
135
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139
140 /*
141  * Function Prototypes
142  */
143 extern int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len);
144 extern int sysfs_get_name_from_path(const unsigned char *path, 
145                                         unsigned char *name, size_t len);
146 extern int sysfs_get_link(const unsigned char *path, unsigned char *target, 
147                                                                 size_t len);
148 extern struct dlist *sysfs_open_subsystem_list(unsigned char *name);
149 extern struct dlist *sysfs_open_bus_devices_list(unsigned char *name);
150 extern void sysfs_close_list(struct dlist *list);
151
152 /* sysfs directory and file access */
153 extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
154 extern struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path);
155 extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
156 extern int sysfs_read_attribute_value(const unsigned char *attrpath, 
157                                 unsigned char *value, size_t vsize);
158 extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
159                 const unsigned char *new_value, size_t len);
160 extern unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, 
161                                                 const unsigned char * name);
162 extern void sysfs_close_directory(struct sysfs_directory *sysdir);
163 extern struct sysfs_directory *sysfs_open_directory(const unsigned char *path);
164 extern int sysfs_read_directory(struct sysfs_directory *sysdir);
165 extern int sysfs_read_all_subdirs(struct sysfs_directory *sysdir);
166 extern struct sysfs_directory *sysfs_get_subdirectory
167                         (struct sysfs_directory *dir, unsigned char *subname);
168 extern void sysfs_close_link(struct sysfs_link *ln);
169 extern struct sysfs_link *sysfs_open_link(const unsigned char *lnpath);
170 extern struct sysfs_link *sysfs_get_directory_link(struct sysfs_directory *dir,
171                                                 unsigned char *linkname);
172 extern struct sysfs_link *sysfs_get_subdirectory_link
173                         (struct sysfs_directory *dir, unsigned char *linkname);
174 extern struct sysfs_attribute *sysfs_get_directory_attribute
175                         (struct sysfs_directory *dir, unsigned char *attrname);
176
177 /* sysfs driver access */
178 extern void sysfs_close_driver(struct sysfs_driver *driver);
179 extern struct sysfs_driver *sysfs_open_driver(const unsigned char *path);
180 extern struct sysfs_attribute *sysfs_get_driver_attr
181                 (struct sysfs_driver *drv, const unsigned char *name);
182 extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver);
183 extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver);
184 extern void sysfs_close_driver_by_name(struct sysfs_driver *driver);
185 extern struct sysfs_driver *sysfs_open_driver_by_name
186         (const unsigned char *drv_name, const unsigned char *bus, size_t bsize);
187 extern int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib,
188                                 unsigned char *value, size_t len);
189 extern int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib,
190                                 unsigned char *value, size_t len);
191
192 /* generic sysfs device access */
193 extern void sysfs_close_root_device(struct sysfs_root_device *root);
194 extern struct sysfs_root_device *sysfs_open_root_device
195                                                 (const unsigned char *name);
196 extern void sysfs_close_device(struct sysfs_device *dev);
197 extern struct sysfs_device *sysfs_open_device(const unsigned char *path);
198 extern struct sysfs_attribute *sysfs_get_device_attr
199                         (struct sysfs_device *dev, const unsigned char *name);
200 extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device);
201 extern struct sysfs_device *sysfs_open_device_by_id
202         (const unsigned char *bus_id, const unsigned char *bus, size_t bsize);
203 extern int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib,
204                                 unsigned char *value, size_t len);
205 extern int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib,
206                                 unsigned char *value, size_t len);
207
208 /* generic sysfs bus access */
209 extern void sysfs_close_bus(struct sysfs_bus *bus);
210 extern struct sysfs_bus *sysfs_open_bus(const unsigned char *name);
211 extern struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
212                                                 unsigned char *id);
213 extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
214                                                 unsigned char *drvname);
215 extern struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus);
216 extern struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
217                                                 unsigned char *attrname);
218 extern struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
219                                                         unsigned char *dev_id);
220 extern int sysfs_find_device_bus(const unsigned char *dev_id, 
221                                         unsigned char *busname, size_t bsize);
222 extern int sysfs_find_driver_bus(const unsigned char *driver, 
223                                         unsigned char *busname, size_t bsize);
224
225 /* generic sysfs class access */
226 extern void sysfs_close_class_device(struct sysfs_class_device *dev);
227 extern struct sysfs_class_device *sysfs_open_class_device
228                                         (const unsigned char *path);
229 extern void sysfs_close_class(struct sysfs_class *cls);
230 extern struct sysfs_class *sysfs_open_class(const unsigned char *name);
231 extern struct sysfs_class_device *sysfs_get_class_device
232         (struct sysfs_class *class, unsigned char *name);
233 extern struct sysfs_class_device *sysfs_open_class_device_by_name
234         (const unsigned char *class, unsigned char *name);
235 extern struct dlist *sysfs_get_classdev_attributes
236         (struct sysfs_class_device *cdev);
237 extern int sysfs_find_device_class(const unsigned char *bus_id, 
238                 unsigned char *classname, size_t bsize);
239 extern struct sysfs_attribute *sysfs_get_classdev_attr
240         (struct sysfs_class_device *clsdev, const unsigned char *name);
241 extern int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, 
242                 unsigned char *value, size_t len);
243 extern int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, 
244                 unsigned char *value, size_t len);
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif /* _LIBSYSFS_H_ */