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