summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Nelson2016-03-16 15:41:39 -0500
committerSam Nelson2016-03-16 15:41:39 -0500
commit57cfda47ebee0d99203bc16100a312b6fcc20e01 (patch)
treeaf247e003644d05a7616b5b81a26a5b2b7ab39eb
parent8315fbefd3e831c8749ae92903bc9a0718771ecf (diff)
downloadmpm-transport-57cfda47ebee0d99203bc16100a312b6fcc20e01.tar.gz
mpm-transport-57cfda47ebee0d99203bc16100a312b6fcc20e01.tar.xz
mpm-transport-57cfda47ebee0d99203bc16100a312b6fcc20e01.zip
uioutils: Update to unix format
Signed-off-by: Sam Nelson <sam.nelson@ti.com>
-rw-r--r--src/utils/uio/uioutils.c624
1 files changed, 312 insertions, 312 deletions
diff --git a/src/utils/uio/uioutils.c b/src/utils/uio/uioutils.c
index 75f092b..d57f528 100644
--- a/src/utils/uio/uioutils.c
+++ b/src/utils/uio/uioutils.c
@@ -1,312 +1,312 @@
1/* 1/*
2 * Copyright (C) 2013-2015 Texas Instruments Incorporated - http://www.ti.com/ 2 * Copyright (C) 2013-2015 Texas Instruments Incorporated - http://www.ti.com/
3 * 3 *
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * Redistributions of source code must retain the above copyright 9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 11 *
12 * Redistributions in binary form must reproduce the above copyright 12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the 14 * documentation and/or other materials provided with the
15 * distribution. 15 * distribution.
16 * 16 *
17 * Neither the name of Texas Instruments Incorporated nor the names of 17 * Neither the name of Texas Instruments Incorporated nor the names of
18 * its contributors may be used to endorse or promote products derived 18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission. 19 * from this software without specific prior written permission.
20 * 20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * 32 *
33 */ 33 */
34 34
35#include <stdio.h> 35#include <stdio.h>
36#include <errno.h> 36#include <errno.h>
37#include <string.h> 37#include <string.h>
38#include <unistd.h> 38#include <unistd.h>
39#include <dirent.h> 39#include <dirent.h>
40#include <stdlib.h> 40#include <stdlib.h>
41#include <fcntl.h> 41#include <fcntl.h>
42#include <sys/types.h> 42#include <sys/types.h>
43#include <sys/stat.h> 43#include <sys/stat.h>
44#include <sys/mman.h> 44#include <sys/mman.h>
45 45
46#include "uioutils.h" 46#include "uioutils.h"
47 47
48#define MAX_FILE_NAME_LENGTH 128 48#define MAX_FILE_NAME_LENGTH 128
49#define MAX_LINE_LENGTH 128 49#define MAX_LINE_LENGTH 128
50#define MAX_NAME_LENGTH 64 50#define MAX_NAME_LENGTH 64
51#define MAX_LABEL_LENGTH 16 51#define MAX_LABEL_LENGTH 16
52 52
53int endian_check() { 53int endian_check() {
54 int check = 1; 54 int check = 1;
55 char ret = *(char *) &check; 55 char ret = *(char *) &check;
56 return ret; 56 return ret;
57} 57}
58 58
59int byte_swap(int x) { 59int byte_swap(int x) {
60 return ( ((x>>0) & 0xff) << 24) 60 return ( ((x>>0) & 0xff) << 24)
61 | ( ((x>>8) & 0xff) << 16) 61 | ( ((x>>8) & 0xff) << 16)
62 | ( ((x>>16) & 0xff) << 8) 62 | ( ((x>>16) & 0xff) << 8)
63 | ( ((x>>24) & 0xff) << 0); 63 | ( ((x>>24) & 0xff) << 0);
64} 64}
65 65
66int uioutil_get_param_int(char *uio_name, char *param_name, int *buf) 66int uioutil_get_param_int(char *uio_name, char *param_name, int *buf)
67{ 67{
68 char filename[MAX_LINE_LENGTH]; 68 char filename[MAX_LINE_LENGTH];
69 FILE *fp; 69 FILE *fp;
70 int ret = -1; 70 int ret = -1;
71 int size = 35 + strlen(uio_name) + strlen(param_name); 71 int size = 35 + strlen(uio_name) + strlen(param_name);
72 if (size > MAX_LINE_LENGTH) return -1; 72 if (size > MAX_LINE_LENGTH) return -1;
73 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name); 73 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name);
74 fp = fopen(filename, "rb"); 74 fp = fopen(filename, "rb");
75 if (fp == NULL) return -1; 75 if (fp == NULL) return -1;
76 if (fread(&ret, sizeof(int), 1, fp) <= 0) { 76 if (fread(&ret, sizeof(int), 1, fp) <= 0) {
77 if (fp) fclose(fp); 77 if (fp) fclose(fp);
78 return -1; 78 return -1;
79 } 79 }
80 if (endian_check()) ret = byte_swap(ret); 80 if (endian_check()) ret = byte_swap(ret);
81 if (fp) fclose(fp); 81 if (fp) fclose(fp);
82 *buf = ret; 82 *buf = ret;
83 return 0; 83 return 0;
84} 84}
85 85
86int uioutil_get_param_int_array(char *uio_name, char *param_name, int array_size, int *buf) 86int uioutil_get_param_int_array(char *uio_name, char *param_name, int array_size, int *buf)
87{ 87{
88 char filename[MAX_LINE_LENGTH]; 88 char filename[MAX_LINE_LENGTH];
89 FILE *fp; 89 FILE *fp;
90 int ret = -1; 90 int ret = -1;
91 int size = 35 + strlen(uio_name) + strlen(param_name); 91 int size = 35 + strlen(uio_name) + strlen(param_name);
92 int i; 92 int i;
93 93
94 if (size > MAX_LINE_LENGTH) return -1; 94 if (size > MAX_LINE_LENGTH) return -1;
95 if (buf == NULL) return -1; 95 if (buf == NULL) return -1;
96 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name); 96 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name);
97 fp = fopen(filename, "rb"); 97 fp = fopen(filename, "rb");
98 if (fp == NULL) return -1; 98 if (fp == NULL) return -1;
99 for (i=0; i<array_size; i++) 99 for (i=0; i<array_size; i++)
100 { 100 {
101 if (fread(&ret, sizeof(int), 1, fp) <= 0) { 101 if (fread(&ret, sizeof(int), 1, fp) <= 0) {
102 if (fp) fclose(fp); 102 if (fp) fclose(fp);
103 return -1; 103 return -1;
104 } 104 }
105 if (endian_check()) ret = byte_swap(ret); 105 if (endian_check()) ret = byte_swap(ret);
106 buf[i] = ret; 106 buf[i] = ret;
107 } 107 }
108 if (fp) fclose(fp); 108 if (fp) fclose(fp);
109 return 0; 109 return 0;
110} 110}
111 111
112int uioutil_get_param_str(char *uio_name, char *param_name, char *buf) 112int uioutil_get_param_str(char *uio_name, char *param_name, char *buf)
113{ 113{
114 char st[MAX_LINE_LENGTH]; 114 char st[MAX_LINE_LENGTH];
115 FILE *fp; 115 FILE *fp;
116 char filename[MAX_LINE_LENGTH] = ""; 116 char filename[MAX_LINE_LENGTH] = "";
117 int size = 35 + strlen(uio_name) + strlen(param_name); 117 int size = 35 + strlen(uio_name) + strlen(param_name);
118 if (size > MAX_LINE_LENGTH) return -1; 118 if (size > MAX_LINE_LENGTH) return -1;
119 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name); 119 snprintf(filename, size, "/proc/device-tree/soc/%s/cfg-params/%s", uio_name, param_name);
120 fp = fopen(filename, "r"); 120 fp = fopen(filename, "r");
121 if (fp==NULL) return -1; 121 if (fp==NULL) return -1;
122 if (fscanf(fp, "%s", st) == EOF) { 122 if (fscanf(fp, "%s", st) == EOF) {
123 if (fp) fclose(fp); 123 if (fp) fclose(fp);
124 return -1; 124 return -1;
125 } 125 }
126 if (fp) fclose(fp); 126 if (fp) fclose(fp);
127 strncpy(buf,st, MAX_LINE_LENGTH); 127 strncpy(buf,st, MAX_LINE_LENGTH);
128 return 0; 128 return 0;
129} 129}
130 130
131int uioutil_check_device_exist(char *st) 131int uioutil_check_device_exist(char *st)
132{ 132{
133 int ret, fd; 133 int ret, fd;
134 char tmp[6] = "/dev/"; 134 char tmp[6] = "/dev/";
135 char dev[MAX_LINE_LENGTH]; 135 char dev[MAX_LINE_LENGTH];
136 snprintf(dev, strlen(tmp) + strlen(st) + 1, "%s%s", tmp, st); 136 snprintf(dev, strlen(tmp) + strlen(st) + 1, "%s%s", tmp, st);
137 fd = open(dev, O_SYNC); 137 fd = open(dev, O_SYNC);
138 if (fd < 0) { 138 if (fd < 0) {
139 /* errno 2 is No such file or directory. */ 139 /* errno 2 is No such file or directory. */
140 if (errno == 2) ret = 0; 140 if (errno == 2) ret = 0;
141 else ret = errno; 141 else ret = errno;
142 } 142 }
143 else { 143 else {
144 close(fd); 144 close(fd);
145 ret = 1; 145 ret = 1;
146 } 146 }
147 return ret; 147 return ret;
148} 148}
149 149
150int uioutil_get_string (char *filename, char *str, int str_len) 150int uioutil_get_string (char *filename, char *str, int str_len)
151{ 151{
152 FILE *fpr = 0; 152 FILE *fpr = 0;
153 int ret_val = -1; 153 int ret_val = -1;
154 154
155 if (!filename || !str || !str_len) { 155 if (!filename || !str || !str_len) {
156 goto close_n_exit; 156 goto close_n_exit;
157 } 157 }
158 158
159 fpr = fopen(filename, "r"); 159 fpr = fopen(filename, "r");
160 if (!fpr) { 160 if (!fpr) {
161 goto close_n_exit; 161 goto close_n_exit;
162 } 162 }
163 if (!fgets(str, str_len, fpr)) { 163 if (!fgets(str, str_len, fpr)) {
164 goto close_n_exit; 164 goto close_n_exit;
165 } 165 }
166 166
167 ret_val = 0; 167 ret_val = 0;
168 168
169close_n_exit: 169close_n_exit:
170 if (fpr) fclose(fpr); 170 if (fpr) fclose(fpr);
171 return ret_val; 171 return ret_val;
172} 172}
173 173
174int uioutil_get_int (char *filename, int *val) 174int uioutil_get_int (char *filename, int *val)
175{ 175{
176 char line[MAX_LINE_LENGTH]; 176 char line[MAX_LINE_LENGTH];
177 177
178 if (!filename || !val) 178 if (!filename || !val)
179 return -1; 179 return -1;
180 180
181 if (uioutil_get_string(filename, line, MAX_LINE_LENGTH) < 0) 181 if (uioutil_get_string(filename, line, MAX_LINE_LENGTH) < 0)
182 return -1; 182 return -1;
183 183
184 *val = strtoul(line, NULL, 0); 184 *val = strtoul(line, NULL, 0);
185 return 0; 185 return 0;
186} 186}
187 187
188char *remove_postfix_from_device_name(char *name, int max_length) 188char *remove_postfix_from_device_name(char *name, int max_length)
189{ 189{
190 int index; 190 int index;
191 191
192 index=0; 192 index=0;
193 193
194 /* strip anything after . in the name */ 194 /* strip anything after . in the name */
195 while(name[index] != '\0') 195 while(name[index] != '\0')
196 { 196 {
197 if(name[index] == '.') 197 if(name[index] == '.')
198 { 198 {
199 name[index] = '\0'; 199 name[index] = '\0';
200 index++; 200 index++;
201 return(&name[0]); 201 return(&name[0]);
202 } 202 }
203 if(index > max_length) 203 if(index > max_length)
204 break; 204 break;
205 index++; 205 index++;
206 } 206 }
207 return(name); 207 return(name);
208} 208}
209 209
210char *remove_prefix_from_device_name(char *name, int max_length) 210char *remove_prefix_from_device_name(char *name, int max_length)
211{ 211{
212 int index; 212 int index;
213 213
214 index=0; 214 index=0;
215 215
216 /* strip anything before . in the name */ 216 /* strip anything before . in the name */
217 while(name[index] != '\0') 217 while(name[index] != '\0')
218 { 218 {
219 if(name[index] == '.') 219 if(name[index] == '.')
220 { 220 {
221 index++; 221 index++;
222 return(&name[index]); 222 return(&name[index]);
223 } 223 }
224 if(index > max_length) 224 if(index > max_length)
225 break; 225 break;
226 index++; 226 index++;
227 } 227 }
228 return(name); 228 return(name);
229} 229}
230int uioutil_get_device (char *uio_name, char *class_name, int class_name_length) 230int uioutil_get_device (char *uio_name, char *class_name, int class_name_length)
231{ 231{
232 struct dirent *entry = 0; 232 struct dirent *entry = 0;
233 char filename[MAX_FILE_NAME_LENGTH]; 233 char filename[MAX_FILE_NAME_LENGTH];
234 char name[MAX_NAME_LENGTH]; 234 char name[MAX_NAME_LENGTH];
235 int ret_val = -1; 235 int ret_val = -1;
236 DIR *dir = 0; 236 DIR *dir = 0;
237 char *name_ptr; 237 char *name_ptr;
238 238
239 if (!uio_name || !class_name || !strlen(uio_name)) { 239 if (!uio_name || !class_name || !strlen(uio_name)) {
240 goto close_n_exit; 240 goto close_n_exit;
241 } 241 }
242 242
243 dir = opendir("/sys/class/uio"); 243 dir = opendir("/sys/class/uio");
244 if (!dir) { 244 if (!dir) {
245 perror("readdir /sys/class/uio"); 245 perror("readdir /sys/class/uio");
246 goto close_n_exit; 246 goto close_n_exit;
247 } 247 }
248 248
249 while ((entry = readdir(dir)) != NULL) { 249 while ((entry = readdir(dir)) != NULL) {
250 if (strstr (entry->d_name, "uio") == NULL) { 250 if (strstr (entry->d_name, "uio") == NULL) {
251 continue; 251 continue;
252 } 252 }
253 253
254 snprintf(filename, MAX_FILE_NAME_LENGTH, "/sys/class/uio/%s/name", entry->d_name); 254 snprintf(filename, MAX_FILE_NAME_LENGTH, "/sys/class/uio/%s/name", entry->d_name);
255 255
256 if (uioutil_get_string(filename, name, MAX_NAME_LENGTH) < 0) 256 if (uioutil_get_string(filename, name, MAX_NAME_LENGTH) < 0)
257 goto close_n_exit; 257 goto close_n_exit;
258 name_ptr = remove_prefix_from_device_name(name, MAX_NAME_LENGTH); 258 name_ptr = remove_prefix_from_device_name(name, MAX_NAME_LENGTH);
259 if (!strncmp (uio_name, name_ptr, strlen(uio_name))) { 259 if (!strncmp (uio_name, name_ptr, strlen(uio_name))) {
260 ret_val = 0; 260 ret_val = 0;
261 strncpy (class_name, entry->d_name, class_name_length); 261 strncpy (class_name, entry->d_name, class_name_length);
262 goto close_n_exit; 262 goto close_n_exit;
263 } 263 }
264 } 264 }
265 265
266close_n_exit: 266close_n_exit:
267 if (dir) closedir(dir); 267 if (dir) closedir(dir);
268 268
269 return ret_val; 269 return ret_val;
270} 270}
271 271
272int uioutil_get_peripheral_device (char *uio_name, char *class_name, int class_name_length) 272int uioutil_get_peripheral_device (char *uio_name, char *class_name, int class_name_length)
273{ 273{
274 struct dirent *entry = 0; 274 struct dirent *entry = 0;
275 char filename[MAX_FILE_NAME_LENGTH]; 275 char filename[MAX_FILE_NAME_LENGTH];
276 char name[MAX_NAME_LENGTH]; 276 char name[MAX_NAME_LENGTH];
277 int ret_val = -1; 277 int ret_val = -1;
278 DIR *dir = 0; 278 DIR *dir = 0;
279 char *name_ptr; 279 char *name_ptr;
280 280
281 if (!uio_name || !class_name || !strlen(uio_name)) { 281 if (!uio_name || !class_name || !strlen(uio_name)) {
282 goto close_n_exit; 282 goto close_n_exit;
283 } 283 }
284 284
285 dir = opendir("/sys/class/uio"); 285 dir = opendir("/sys/class/uio");
286 if (!dir) { 286 if (!dir) {
287 perror("readdir /sys/class/uio"); 287 perror("readdir /sys/class/uio");
288 goto close_n_exit; 288 goto close_n_exit;
289 } 289 }
290 290
291 while ((entry = readdir(dir)) != NULL) { 291 while ((entry = readdir(dir)) != NULL) {
292 if (strstr (entry->d_name, "uio") == NULL) { 292 if (strstr (entry->d_name, "uio") == NULL) {
293 continue; 293 continue;
294 } 294 }
295 295
296 snprintf(filename, MAX_FILE_NAME_LENGTH, "/sys/class/uio/%s/name", entry->d_name); 296 snprintf(filename, MAX_FILE_NAME_LENGTH, "/sys/class/uio/%s/name", entry->d_name);
297 297
298 if (uioutil_get_string(filename, name, MAX_NAME_LENGTH) < 0) 298 if (uioutil_get_string(filename, name, MAX_NAME_LENGTH) < 0)
299 goto close_n_exit; 299 goto close_n_exit;
300 name_ptr = remove_postfix_from_device_name(name, MAX_NAME_LENGTH); 300 name_ptr = remove_postfix_from_device_name(name, MAX_NAME_LENGTH);
301 if (!strncmp (uio_name, name_ptr, strlen(uio_name))) { 301 if (!strncmp (uio_name, name_ptr, strlen(uio_name))) {
302 ret_val = 0; 302 ret_val = 0;
303 strncpy (class_name, entry->d_name, class_name_length); 303 strncpy (class_name, entry->d_name, class_name_length);
304 goto close_n_exit; 304 goto close_n_exit;
305 } 305 }
306 } 306 }
307 307
308close_n_exit: 308close_n_exit:
309 if (dir) closedir(dir); 309 if (dir) closedir(dir);
310 310
311 return ret_val; 311 return ret_val;
312} 312}