forked from byates/lsuio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsuio.c
236 lines (198 loc) · 5.69 KB
/
lsuio.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
/*
lsuio - List UIO devices.
Copyright (C) 2007 Hans J. Koch
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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <getopt.h>
#include "uio_helper.h"
#define VERSION "1.0"
static void usage(int status);
static void version(int status);
/* The name the program was run with, stripped of any leading path. */
char* program_name;
/* Option flags and variables */
static struct option const long_options[] =
{
{ "help", no_argument, 0, 'h' },
{ "mmap", no_argument, 0, 'm' },
{ "uiodev", required_argument, 0, 'u' },
{ "verbose", no_argument, 0, 'v' },
{ "version", no_argument, 0, 'V' },
{ NULL, 0, NULL, 0 }
};
int opt_mmap;
int opt_verbose;
int opt_uiodev;
int opt_help;
int opt_version;
int uio_filter;
static int decode_switches(int argc, char** argv);
static void show_uio_info(struct uio_info_t* info);
static void show_device(struct uio_info_t* info)
{
char dev_name[16];
sprintf(dev_name, "uio%d", info->uio_num);
printf("%s: name=%s, version=%s, events=%ld\n",
dev_name, info->name, info->version, info->event_count);
}
static int show_map(struct uio_info_t* info, int map_num)
{
if (info->maps[map_num].size <= 0) return -1;
printf("\tmap[%d]: addr=0x%08lX, size=%d",
map_num,
info->maps[map_num].addr,
info->maps[map_num].size);
if (opt_mmap)
{
printf(", mmap test: ");
switch (info->maps[map_num].mmap_result)
{
case UIO_MMAP_NOT_DONE:
printf("N/A");
break;
case UIO_MMAP_OK:
printf("OK [0x08%X]", (uint32_t)info->maps[map_num].internal_addr);
break;
default:
printf("FAILED");
}
}
printf("\n");
return 0;
}
static void show_dev_attrs(struct uio_info_t* info)
{
struct uio_dev_attr_t* attr = info->dev_attrs;
if (attr) printf("\tDevice attributes:\n");
else printf("\t(No device attributes)\n");
while (attr)
{
printf("\t%s=%s\n", attr->name, attr->value);
attr = attr->next;
}
}
static void show_maps(struct uio_info_t* info)
{
int ret;
int mi = 0;
do
{
ret = show_map(info, mi);
mi++;
}
while ((ret == 0) && (mi < MAX_UIO_MAPS));
}
static void show_uio_info(struct uio_info_t* info)
{
show_device(info);
show_maps(info);
if (opt_verbose) show_dev_attrs(info);
}
/* Set all the option flags according to the switches specified.
Return the index of the first non-option argument. */
static int decode_switches(int argc, char** argv)
{
int opt, opt_index = 0;
opt_mmap = 0;
opt_help = 0;
opt_uiodev = 0;
opt_version = 0;
opt_verbose = 0;
uio_filter = -1;
while (1)
{
opt = getopt_long(argc, argv, "hmu:vV", long_options, &opt_index);
if (opt == EOF) break;
switch (opt)
{
case 'm' :
opt_mmap = 1;
break;
case 'u' :
opt_uiodev = 1;
uio_filter = atoi(optarg);
break;
case 'v' :
opt_verbose = 1;
break;
case 'h' :
opt_help = 1;
break;
case 'V' :
opt_version = 1;
break;
}
}
return 0;
}
static void usage(int status)
{
printf("%s - List UIO devices.\n", program_name);
printf("Usage: %s [OPTIONS]\n", program_name);
printf("\
Options:\n\
-h, --help display this help and exit\n\
-m, --mmap test if mmap() works for all mappings\n\
-u, --uiodev use the specific uio device (default all)\n\
-v, --verbose also display device attributes\n\
-V, --version output version information and exit\n\
");
exit(status);
}
static void version(int status)
{
printf(VERSION"\n");
exit(status);
}
int main(int argc, char** argv)
{
struct uio_info_t* info_list, * p;
program_name = argv[0];
decode_switches(argc, argv);
if (opt_help) usage(0);
if (opt_version) version(0);
info_list = uio_find_devices(uio_filter);
if (!info_list) printf("No UIO devices found.\n");
p = info_list;
while (p)
{
uio_get_all_info(p);
if (opt_verbose) uio_get_device_attributes(p);
if (opt_mmap)
{
char dev_name[16];
int fd;
snprintf(dev_name, sizeof(dev_name),
"/dev/uio%d", p->uio_num);
fd = open(dev_name, O_RDWR);
if (fd >= 0)
{
uio_mmap(p, fd);
}
else
{
printf("ERROR: unable to open %s.\n", dev_name);
}
close(fd);
}
show_uio_info(p);
p = p->next;
}
uio_free_info(info_list);
exit(0);
}