-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuio_helper.c
412 lines (367 loc) · 9.29 KB
/
uio_helper.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
uio_helper.c
UIO helper functions.
Copyright (C) 2007 Hans J. Koch
Copyright (C) 2020 QBayLogic B.V.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "uio_helper.h"
int uio_get_mem_offset(struct uio_info_t* info, int map_num)
{
int ret;
char filename[64];
info->maps[map_num].offset = UIO_INVALID_OFFSET;
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/offset",
info->uio_num, map_num);
FILE* file = fopen(filename,"r");
if (!file) return -1;
ret = fscanf(file,"0x%x",
(unsigned int *) &info->maps[map_num].offset);
fclose(file);
if (ret<0) return -2;
return 0;
}
int uio_get_mem_size(struct uio_info_t* info, int map_num)
{
int ret;
char filename[64];
info->maps[map_num].size = UIO_INVALID_SIZE;
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/size",
info->uio_num, map_num);
FILE* file = fopen(filename,"r");
if (!file) return -1;
ret = fscanf(file,"0x%x",(unsigned int *) &info->maps[map_num].size);
fclose(file);
if (ret<0) return -2;
return 0;
}
int uio_get_mem_addr(struct uio_info_t* info, int map_num)
{
int ret;
char filename[64];
info->maps[map_num].addr = UIO_INVALID_ADDR;
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/addr",
info->uio_num, map_num);
FILE* file = fopen(filename,"r");
if (!file) return -1;
ret = fscanf(file,"0x%lx",&info->maps[map_num].addr);
fclose(file);
if (ret<0) return -2;
return 0;
}
static int line_from_file(char *filename, char *linebuf)
{
char *s;
int i;
memset(linebuf, 0, UIO_MAX_NAME_SIZE);
FILE* file = fopen(filename,"r");
if (!file) return -1;
s = fgets(linebuf,UIO_MAX_NAME_SIZE,file);
if (!s) return -2;
for (i=0; (*s)&&(i<UIO_MAX_NAME_SIZE); i++) {
if (*s == '\n') *s = 0;
s++;
}
fclose(file);
return 0;
}
int uio_get_mem_name(struct uio_info_t* info, int map_num)
{
char filename[64];
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/name",
info->uio_num, map_num);
return line_from_file(filename, info->maps[map_num].name);
}
int uio_get_event_count(struct uio_info_t* info)
{
int ret;
char filename[64];
info->event_count = 0;
sprintf(filename, "/sys/class/uio/uio%d/event", info->uio_num);
FILE* file = fopen(filename,"r");
if (!file) return -1;
ret = fscanf(file,"%lu",&info->event_count);
fclose(file);
if (ret<0) return -2;
return 0;
}
int uio_get_name(struct uio_info_t* info)
{
char filename[64];
sprintf(filename, "/sys/class/uio/uio%d/name", info->uio_num);
return line_from_file(filename, info->name);
}
int uio_get_version(struct uio_info_t* info)
{
char filename[64];
sprintf(filename, "/sys/class/uio/uio%d/version", info->uio_num);
return line_from_file(filename, info->version);
}
int uio_get_all_info(struct uio_info_t* info)
{
int i;
if (!info)
return -1;
if ((info->uio_num < 0)||(info->uio_num > UIO_MAX_NUM))
return -1;
for (i = 0; i < MAX_UIO_MAPS; i++) {
uio_get_mem_size(info, i);
uio_get_mem_addr(info, i);
uio_get_mem_offset(info, i);
uio_get_mem_name(info, i);
}
uio_get_event_count(info);
uio_get_name(info);
uio_get_version(info);
return 0;
}
static int dev_attr_filter(char *filename)
{
struct stat filestat;
if (lstat(filename, &filestat))
return 0;
if (S_ISREG(filestat.st_mode))
return 1;
return 0;
}
static struct uio_dev_attr_t *uio_get_uevent_attributes(char *filename,
struct uio_dev_attr_t **link)
{
const char prefix[] = "uevent/";
const size_t prefix_len = strlen(prefix);
char linebuf[2*UIO_MAX_NAME_SIZE - prefix_len + 3];
/* ^ Enough room for VAR=VALUE\n where VAR will be prefixed. Note that
* if UIO_MAX_NAME_SIZE ever proves insufficient,
* include/linux/kobject.h in the kernel source defines
* UEVENT_BUFFER_SIZE (2048) as holding the entire uevent file, so
* this is an upper bound on the size of any variable.
*/
char *s1, *s2;
struct uio_dev_attr_t *attr = NULL;
FILE* file;
file = fopen(filename, "r");
if (!file)
return NULL;
s1 = fgets(linebuf, sizeof(linebuf), file);
while (s1) {
attr = malloc(sizeof(*attr));
if (!attr) {
fclose(file);
return NULL;
}
s2 = strsep(&s1, "=\n");
strcpy(attr->name, prefix);
strncpy(attr->name + prefix_len, s2,
sizeof(attr->name) - prefix_len);
attr->name[sizeof(attr->name)-1] = '\0';
if (s1) {
s2 = strsep(&s1, "\n");
strncpy(attr->value, s2, sizeof(attr->value));
attr->value[sizeof(attr->value)-1] = '\0';
} else {
attr->value[0] = '\0';
}
*link = attr;
attr->next = NULL;
link = &attr->next;
s1 = fgets(linebuf, sizeof(linebuf), file);
}
if (!attr) {
/* uevent file was empty */
attr = malloc(sizeof(*attr));
if (!attr) {
fclose(file);
return NULL;
}
strcpy(attr->name, "uevent");
attr->value[0] = '\0';
*link = attr;
attr->next = NULL;
}
fclose(file);
return attr;
}
int uio_get_device_attributes(struct uio_info_t* info)
{
struct dirent **namelist;
struct uio_dev_attr_t *attr, *last;
char fullname[96];
int n;
info->dev_attrs = NULL;
sprintf(fullname, "/sys/class/uio/uio%d/device", info->uio_num);
n = scandir(fullname, &namelist, 0, alphasort);
if (n < 0)
return -1;
while(n--) {
snprintf(fullname, sizeof(fullname),
"/sys/class/uio/uio%d/device/%s",
info->uio_num, namelist[n]->d_name);
if (!dev_attr_filter(fullname))
continue;
if (!strcmp(namelist[n]->d_name, "uevent")) {
struct uio_dev_attr_t **link;
if (!info->dev_attrs)
link = &info->dev_attrs;
else
link = &last->next;
attr = uio_get_uevent_attributes(fullname, link);
if (!attr)
return -1;
last = attr;
continue;
}
attr = malloc(sizeof(struct uio_dev_attr_t));
if (!attr)
return -1;
strncpy(attr->name, namelist[n]->d_name, UIO_MAX_NAME_SIZE);
free(namelist[n]);
if (line_from_file(fullname, attr->value)) {
free(attr);
continue;
}
if (!info->dev_attrs)
info->dev_attrs = attr;
else
last->next = attr;
attr->next = NULL;
last = attr;
}
free(namelist);
return 0;
}
void uio_free_dev_attrs(struct uio_info_t* info)
{
struct uio_dev_attr_t *p1, *p2;
p1 = info->dev_attrs;
while (p1) {
p2 = p1->next;
free(p1);
p1 = p2;
}
info->dev_attrs = NULL;
}
void uio_free_info(struct uio_info_t* info)
{
struct uio_info_t *p1,*p2;
p1 = info;
while (p1) {
uio_free_dev_attrs(p1);
p2 = p1->next;
free(p1);
p1 = p2;
}
}
static int uio_num_from_filename(char* name)
{
enum scan_states { ss_u, ss_i, ss_o, ss_num, ss_err };
enum scan_states state = ss_u;
int i=0, num = -1;
char ch = name[0];
while (ch && (state != ss_err)) {
switch (ch) {
case 'u': if (state == ss_u) state = ss_i;
else state = ss_err;
break;
case 'i': if (state == ss_i) state = ss_o;
else state = ss_err;
break;
case 'o': if (state == ss_o) state = ss_num;
else state = ss_err;
break;
default: if ( (ch>='0') && (ch<='9')
&&(state == ss_num) ) {
if (num < 0) num = (ch - '0');
else num = (num * 10) + (ch - '0');
}
else state = ss_err;
}
i++;
ch = name[i];
}
if (state == ss_err) num = -1;
return num;
}
static struct uio_info_t* info_from_name(char* name, int filter_num)
{
struct uio_info_t* info;
int num = uio_num_from_filename(name);
if (num < 0)
return NULL;
if ((filter_num >= 0) && (num != filter_num))
return NULL;
info = malloc(sizeof(struct uio_info_t));
if (!info)
return NULL;
memset(info,0,sizeof(struct uio_info_t));
info->uio_num = num;
return info;
}
struct uio_info_t* uio_find_devices(int filter_num)
{
struct dirent **namelist;
struct uio_info_t *infolist = NULL, *infp, *last;
int n;
n = scandir("/sys/class/uio", &namelist, 0, alphasort);
if (n < 0)
return NULL;
while(n--) {
infp = info_from_name(namelist[n]->d_name, filter_num);
free(namelist[n]);
if (!infp)
continue;
if (!infolist)
infolist = infp;
else
last->next = infp;
last = infp;
}
free(namelist);
return infolist;
}
static void uio_single_mmap_test(struct uio_info_t* info, int map_num)
{
info->maps[map_num].mmap_result = UIO_MMAP_NOT_DONE;
if (info->maps[map_num].size <= 0)
return;
info->maps[map_num].mmap_result = UIO_MMAP_FAILED;
char dev_name[16];
sprintf(dev_name,"/dev/uio%d",info->uio_num);
int fd = open(dev_name,O_RDONLY);
if (fd < 0)
return;
void* map_addr = mmap( NULL,
info->maps[map_num].size,
PROT_READ,
MAP_SHARED,
fd,
map_num*getpagesize());
if (map_addr != MAP_FAILED) {
info->maps[map_num].mmap_result = UIO_MMAP_OK;
munmap(map_addr, info->maps[map_num].size);
}
close(fd);
}
void uio_mmap_test(struct uio_info_t* info)
{
int map_num;
for (map_num= 0; map_num < MAX_UIO_MAPS; map_num++)
uio_single_mmap_test(info, map_num);
}