NCC clean newCell
[fleet.git] / misc / config.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4 #include <string.h>
5 #include "usb-driver.h"
6 #include "parport.h"
7 #ifdef JTAGKEY
8 #include "jtagkey.h"
9 #endif
10 #include "config.h"
11
12 #define LINELEN 1024
13
14 #define PARSEERROR fprintf(stderr,"LIBUSB-DRIVER WARNING: Invalid config statement at line %d\n", line)
15
16 static struct parport_config pp_config[4];
17
18 static void read_config() {
19         int i;
20         static int config_read = 0;
21         FILE *cfg;
22         char buf[LINELEN];
23 #ifdef JTAGKEY
24         char *pbuf;
25         unsigned short vid, pid;
26         int line, len, num;
27 #endif
28
29         if (config_read)
30                 return;
31         
32         config_read = 1;
33
34         for (i=0; i<sizeof(pp_config)/sizeof(struct parport_config); i++) {
35                 pp_config[i].num = i;
36                 pp_config[i].ppbase = i*0x10;
37                 pp_config[i].real = 1;
38                 pp_config[i].open = parport_open;
39                 pp_config[i].close = parport_close;
40                 pp_config[i].transfer = parport_transfer;
41         }
42
43         snprintf(buf, sizeof(buf), "%s/.libusb-driverrc", getenv("HOME"));
44
45         cfg = fopen(buf, "r");
46         if (cfg) {
47 #ifdef JTAGKEY
48                 line = 0;
49                 do {
50                         pbuf = fgets(buf, sizeof(buf), cfg);
51                         if (!pbuf)
52                                 break;
53
54                         line++;
55
56                         len = strlen(buf);
57
58                         if (len > 0 && buf[len-1] == '\n') {
59                                 buf[len-1] = '\0';
60                                 len--;
61                         }
62                         if (len > 0 && buf[len-1] == '\r') {
63                                 buf[len-1] = '\0';
64                                 len--;
65                         }
66                         
67                         for (i = 0; i < len; i++) {
68                                 if (buf[i] != ' ' && buf[i] != '\t')
69                                         break;
70                         }
71
72                         if (buf[i] == '#' || buf[i] == ';' || buf[i] == '\0')
73                                 continue;
74
75                         if (!strncasecmp(buf+i, "LPT", 3)) {
76                                 unsigned char equal_seen = 0;
77
78                                 i += 3;
79                                 pbuf = buf+i;
80                                 for (; i < len; i++) {
81                                         if (buf[i] == ' ' || buf[i] == '\t' || buf[i] == '=') {
82                                                 if (buf[i] == '=')
83                                                         equal_seen = 1;
84
85                                                 buf[i] = '\0';
86                                                 i++;
87                                                 break;
88                                         }
89                                 }
90
91                                 if (*pbuf == '\0') {
92                                         PARSEERROR;
93                                         continue;
94                                 }
95
96                                 num = 0;
97                                 num = strtol(pbuf, NULL, 10);
98                                 if (num < 1) {
99                                         PARSEERROR;
100                                         continue;
101                                 }
102                                 num--;
103
104                                 for (; (i < len) && (!equal_seen) ; i++) {
105                                         if (buf[i] == '=') {
106                                                 equal_seen = 1;
107                                                 i++;
108                                                 break;
109                                         } else if (buf[i] != ' ' && buf[i] != '\t') {
110                                                 break;
111                                         }
112                                 }
113
114                                 if (!equal_seen) {
115                                         PARSEERROR;
116                                         continue;
117                                 }
118
119                                 for (; i < len; i++) {
120                                         if (buf[i] != ' ' && buf[i] != '\t')
121                                                 break;
122                                 }
123
124                                 if (strncasecmp(buf+i, "FTDI:", 5)) {
125                                         PARSEERROR;
126                                         continue;
127                                 }
128
129                                 i += 5;
130                                 pbuf = buf + i;
131
132                                 for (; i < len; i++) {
133                                         if (buf[i] == ':')
134                                                 break;
135                                 }
136
137                                 if (buf[i] != ':') {
138                                         PARSEERROR;
139                                         continue;
140                                 }
141
142                                 buf[i] = '\0';
143
144                                 vid = 0;
145                                 vid = strtol(pbuf, NULL, 16);
146                                 if (!vid) {
147                                         PARSEERROR;
148                                         continue;
149                                 }
150
151                                 i++;
152                                 pbuf = buf + i;
153
154                                 for (; i < len; i++) {
155                                         if (buf[i] == ' ' || buf[i] == '\t')
156                                                 break;
157                                 }
158
159                                 pid = 0;
160                                 pid = strtol(pbuf, NULL, 16);
161                                 if (!pid) {
162                                         PARSEERROR;
163                                         continue;
164                                 }
165
166                                 pp_config[num].real = 0;
167                                 pp_config[num].usb_vid = vid;
168                                 pp_config[num].usb_pid = pid;
169                                 pp_config[num].open = jtagkey_open;
170                                 pp_config[num].close = jtagkey_close;
171                                 pp_config[num].transfer = jtagkey_transfer;
172                         } else {
173                                 PARSEERROR;
174                         }
175                 } while (pbuf);
176 #else
177                 fprintf(stderr,"libusb-driver not compiled with FTDI2232-support, config file ignored!\n");
178 #endif
179                 fclose(cfg);
180         }
181 }
182
183 struct parport_config *config_get(int num) {
184         struct parport_config *ret = NULL;
185         int i;
186
187         read_config();
188         
189         for (i=0; i<sizeof(pp_config)/sizeof(struct parport_config); i++) {
190                 if (pp_config[i].num == num) {
191                         ret = &(pp_config[i]);
192                         break;
193                 }
194         }
195
196         return ret;
197 }
198
199 unsigned char config_is_real_pport(int num) {
200         int ret = 1;
201         int i;
202
203         read_config();
204         
205         for (i=0; i<sizeof(pp_config)/sizeof(struct parport_config); i++) {
206                 if (pp_config[i].num == num) {
207                         ret = pp_config[i].real;
208                         break;
209                 }
210         }
211
212         return ret;
213 }
214
215 unsigned short config_usb_vid(int num) {
216         unsigned short ret = 0x00;
217         int i;
218         
219         read_config();
220         
221         for (i=0; i<sizeof(pp_config)/sizeof(struct parport_config); i++) {
222                 if (pp_config[i].num == num) {
223                         ret = pp_config[i].usb_vid;
224                         break;
225                 }
226         }
227
228         return ret;
229 }
230
231 unsigned short config_usb_pid(int num) {
232         unsigned short ret = 0x00;
233         int i;
234         
235         read_config();
236         
237         for (i=0; i<sizeof(pp_config)/sizeof(struct parport_config); i++) {
238                 if (pp_config[i].num == num) {
239                         ret = pp_config[i].usb_pid;
240                         break;
241                 }
242         }
243
244         return ret;
245 }