-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdtree.h
195 lines (169 loc) · 4.08 KB
/
dtree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Access to device-tree in embedded Linux.
* Public API.
* Jan Viktorin <[email protected]>
*
* The main principle if to be able to iterate
* over all devices or search among them.
*
* The library is not reentrant nor thread safe.
*/
#ifndef DTREE_H
#define DTREE_H
#include <stdint.h>
//
// Module initialization & destruction
//
/**
* Opens device tree using the given root directory
* (typically /proc/device-tree). Setups internal
* structures. Clears error state.
*
* It is safe to call dtree_reset() after dtree_open()
* but it has no effect.
* It is an error to call dtree_open() twice (without
* dtree_close() in between).
*
* Returns 0 on success. On error sets error state.
*/
int dtree_open(const char *rootd);
/**
* Free's resources of the module.
* It is an error to call it when dtree_open()
* has failed or to call it twice.
*/
void dtree_close(void);
//
// Data types
//
/**
* Representation of address.
*/
typedef uint32_t dtree_addr_t;
/**
* Device info representation.
*
* Consists of the name, address (base and highest address)
* and array of compatible devices. Last pointer in compat is NULL.
*/
struct dtree_dev_t {
const char *name;
dtree_addr_t base;
dtree_addr_t high;
const char **compat;
};
#define DTREE_GETTER static inline
/**
* Get name of the device.
*/
DTREE_GETTER
const char *dtree_dev_name(const struct dtree_dev_t *d)
{
return d->name;
}
/**
* Get base address of the device.
*/
DTREE_GETTER
dtree_addr_t dtree_dev_base(const struct dtree_dev_t *d)
{
return d->base;
}
/**
* Get highest address of the device.
* Eg.
* base := 0x81000000
* high := 0x8100FFFF
* range := 0x00010000
*
* If dev.high <= dev.base then high is invalid (eg. not provided by implementation).
*/
DTREE_GETTER
dtree_addr_t dtree_dev_high(const struct dtree_dev_t *d)
{
return d->high;
}
/**
* Get the list of compatible devices. Last entry
* points to NULL.
*/
DTREE_GETTER
const char **dtree_dev_compat(const struct dtree_dev_t *d)
{
return d->compat;
}
//
// Iteration routines
//
/**
* Returns next available device entry.
* The entry should be free'd by dtree_dev_free().
*
* Uses shared internal iterator.
* To search from beginning call dtree_reset().
*
* When no more entries are available or and error occoures
* returns NULL. On error sets error state.
*/
struct dtree_dev_t *dtree_next(void);
/**
* Look up for device by name. Returns the first occurence
* of device with the given name.
* The entry should be free'd by dtree_dev_free().
*
* Uses shared internal iterator.
* To search from beginning call dtree_reset().
*
* Returns NULL when not found or on error.
* On error sets error state.
*/
struct dtree_dev_t *dtree_byname(const char *name);
/**
* Looks up for device compatible with the given type.
* The entry should be free'd by dtree_dev_free().
*
* Uses shared internal iterator.
* To search from beginning call dtree_reset().
*
* Returns NULL when not found or on error.
* On error sets error state.
*/
struct dtree_dev_t *dtree_bycompat(const char *compat);
/**
* Resets the iteration over devices.
* Eg. after this call dtree_next() will return the first
* device again. Affects the internal shared iterator.
*
* Returns 0 on success. On error sets error state.
*/
int dtree_reset(void);
//
// Common functions
//
/**
* Frees the given device entry (returned mostly by iterators).
* It is recommended to free every dev instance before next
* iterator call (or as soon as possible).
*/
void dtree_dev_free(struct dtree_dev_t *dev);
/**
* Tests whether the module is in an error state.
* When it is in an error state the behaviour of all
* operations except dtree_close() and dtree_errstr()
* is undefined.
*
* Returns true when the module is in an error state.
*
* Error handling has to be done before next possible
* failing call. That call would overwrite current
* error state.
*/
int dtree_iserror(void);
/**
* When an error occures this function should
* return a description of what happend.
*
* The pointer points to static memory.
*/
const char *dtree_errstr(void);
#endif