File: of_externals.c

package info (click to toggle)
oflib 0git20070620-8
  • links: PTS
  • area: main
  • in suites: buster
  • size: 144 kB
  • sloc: ansic: 751; makefile: 46
file content (229 lines) | stat: -rw-r--r-- 4,619 bytes parent folder | download | duplicates (5)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***************************************************************************
 *   (C) Copyright 2006, 2007 Alastair Poole.  <alastairpoole@gmail.com>   *   
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "of_api.h"

void of_init(void)
{
	OF_ROOT="/proc/device-tree";
}

void of_init_root(char *path)
{
	uint32_t len;

	if(!path)
		of_error("of_init_root() NULL path");
	
	len = strlen(path);
	if(!len)
		of_error("of_init_root() Invalid path");
		
	if(path[len-1] == '/')
		path[len-1] = '\0';
		
	OF_ROOT=path;	
}


int of_test_property(struct device_node *node, const char *name)
{
	struct stat fstats;
	char buf[PATH_MAX]={0};
	
	strcat(strcat(buf, node->full_path), name);
	
	if(stat(buf, &fstats) < 0)
		return 0;
	else
		return 1;
}

void *of_find_property(struct device_node *node, const char *name, int *plen)
{
	char buf[PATH_MAX]={0};
	uint8_t *property;
	uint32_t size;
	struct stat fstats;
	int fd;
	

	strcat(strcat(buf, node->full_path), name);

	if (stat(buf, &fstats) < 0)
		return NULL;

	size = fstats.st_size;

	property = malloc(size);
	if(!property)
		of_error("malloc()");

	if ((fd = open(buf, O_RDONLY)) < 0)
		of_error("open()");

	if ((*plen = read(fd, property, size)) != size)
		of_error("read()");
		
	close(fd);

	return property;
}

int of_property_to_n_uint64(uint64_t *val, void *prop, uint32_t len, int n)
{
	*val = 0;
	int i=0;
	uint8_t *data = prop;
	
	if((n*8 > len) || (n == 0))
		return 0;
	
	for(i=0; i < (n*8)-1; i++);
	
	*val = 		    ((uint64_t) data[i - 7] << 56) +
			    ((uint64_t) data[i - 6] << 48) +
			    ((uint64_t) data[i - 5] << 40)
			    + ((uint64_t) data[i - 4] << 32) +
			    ((uint64_t) data[i - 3] << 24) +
			    ((uint64_t) data[i - 2] << 16)
			    + ((uint64_t) data[i - 1] << 8) + (uint64_t) data[i];
	
	return 1;
}

int of_property_to_n_uint32(uint32_t *val, void *prop, uint32_t len, int n)
{
	*val = 0;
	int i=0;
	uint8_t *data = prop;
	
	if((n*4 > len) || (n == 0))
		return 0;
	
	for(i=0; i < (n*4)-1; i++);
	
	*val = (data[i-3] << 24) + (data[i-2] << 16) +
	       (data[i-1] << 8) + (data[i]);

	return 1;
}


int of_property_to_uint32(uint32_t *val, void *prop, uint32_t len)
{
	*val=0;
	uint8_t *data = prop;

	if(len != 4)
		return 0;

	*val =	    (data[0] << 24) + (data[1] << 16) +
		    (data[2] << 8) + data[3];

	return 1;
}

struct device_node *of_get_parent(struct device_node *node)
{
	struct device_node *tmp;
	char *p;
	char *ptr;
	
	if((!node->path) || (!node) || (strlen(node->path)==1))
		return NULL;
	
	ptr=strdup(node->path);
	
	p=strrchr(ptr, '/');
	*p=0;
	p=strrchr(ptr, '/');
	*p=0;
	
	tmp=of_find_node_by_path(ptr);
	
	free(ptr);
	
	return tmp;
}

struct device_node *of_find_node_by_name(const char *name, int type)
{
	if (!_n_sem)
		_of_find_node_by_parse(OF_ROOT, name, OF_SEARCH_NAME, type);

	return _of_return_nodes(_n_array, &_n_idx, &_n_sem, type);
}

struct device_node *of_find_type_devices(const char *device_type)
{
	struct device_node *list = calloc(1, sizeof(struct device_node));
	struct device_node *tmp = NULL;
	struct device_node *ptr = list;

	while ((tmp = of_find_node_by_type(device_type, 1)) != NULL) {
		ptr->next = tmp;
		ptr = ptr->next;
		ptr->next = NULL;
	}

	return list;
}

void of_find_type_devices_free(struct device_node *root)
{
	struct device_node *cursor = root;
	struct device_node *fwd;

	while (cursor) {
		fwd = cursor->next;
		of_free_node(cursor);
		cursor = fwd;
	}
}

struct device_node *of_find_node_by_path(const char *path)
{
	struct device_node *tmp = NULL;
	char buf[PATH_MAX];
	struct stat fstats;
	
	_of_make_compat_path(path, buf);

	if (stat(buf, &fstats) < 0)
		return NULL;

	tmp = _of_populate_node(buf, NULL);

	return tmp;
}

struct device_node *of_find_node_by_phandle(uint32_t phandle)
{
	uint32_t *ptr = malloc(4);
	if(!ptr)
		of_error("malloc()");

	*ptr = phandle;

	_of_find_node_by_parse(OF_ROOT, (void *)ptr, OF_SEARCH_PHDL, 0);

	free(ptr);

	return _of_return_nodes(_p_array, &_p_idx, &_t_sem, 0);
}

struct device_node *of_find_node_by_type(const char *device_type, int type)
{
	if (!_t_sem)
		_of_find_node_by_parse(OF_ROOT, device_type, OF_SEARCH_TYPE, type);

	return _of_return_nodes(_t_array, &_t_idx, &_t_sem, type);
}