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