Commit | Line | Data |
---|---|---|
e47d7975 TJ |
1 | /*************************************************************************** |
2 | main.c - description | |
3 | ------------------- | |
4 | begin : Mon Apr 7 12:05:22 CEST 2003 | |
dcd7e8a3 | 5 | copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers |
e47d7975 | 6 | email : opensource@intra2net.com |
5b110dec | 7 | SPDX-License-Identifier: GPL-2.0-only |
e47d7975 TJ |
8 | ***************************************************************************/ |
9 | ||
10 | /*************************************************************************** | |
11 | * * | |
12 | * This program is free software; you can redistribute it and/or modify * | |
13 | * it under the terms of the GNU General Public License version 2 as * | |
14 | * published by the Free Software Foundation. * | |
15 | * * | |
16 | ***************************************************************************/ | |
17 | ||
8a987aa2 TJ |
18 | /* |
19 | TODO: | |
8a987aa2 TJ |
20 | - Merge Uwe's eeprom tool. Current features: |
21 | - Init eeprom defaults based upon eeprom type | |
22 | - Read -> Already there | |
23 | - Write -> Already there | |
24 | - Erase -> Already there | |
25 | - Decode on stdout | |
26 | - Ability to find device by PID/VID, product name or serial | |
27 | ||
28 | TODO nice-to-have: | |
29 | - Out-of-the-box compatibility with FTDI's eeprom tool configuration files | |
30 | */ | |
31 | ||
e47d7975 TJ |
32 | #ifdef HAVE_CONFIG_H |
33 | #include <config.h> | |
34 | #endif | |
35 | ||
36 | #include <stdlib.h> | |
37 | #include <stdio.h> | |
38 | #include <string.h> | |
701c887b | 39 | #include <errno.h> |
6e962b9a | 40 | #include <sys/stat.h> |
e47d7975 TJ |
41 | |
42 | #include <confuse.h> | |
2e48e9fd | 43 | #include <libusb.h> |
e47d7975 | 44 | #include <ftdi.h> |
ade814a5 | 45 | #include <ftdi_eeprom_version.h> |
e47d7975 | 46 | |
add00ad6 | 47 | static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) |
e47d7975 | 48 | { |
add00ad6 | 49 | static const char* options[] = |
048eb722 | 50 | { |
add00ad6 RH |
51 | "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48", |
52 | "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD" | |
048eb722 | 53 | }; |
add00ad6 RH |
54 | |
55 | int i; | |
56 | for (i=0; i<sizeof(options)/sizeof(*options); i++) | |
57 | { | |
58 | if (!(strcmp(options[i], value))) | |
59 | { | |
60 | *(int *)result = i; | |
61 | return 0; | |
62 | } | |
63 | } | |
64 | ||
65 | cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value); | |
66 | return -1; | |
67 | } | |
68 | ||
c7db7f11 CL |
69 | static int parse_group0_drive(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) |
70 | { | |
71 | static const char* options[] = | |
72 | { | |
73 | "4MA", "8MA", "12MA", "16MA" | |
74 | }; | |
75 | ||
76 | int i; | |
77 | for (i=0; i<sizeof(options)/sizeof(*options); i++) | |
78 | { | |
79 | if (!(strcasecmp(options[i], value))) | |
80 | { | |
81 | *(int *)result = i; | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
86 | cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value); | |
87 | return -1; | |
88 | } | |
89 | ||
add00ad6 RH |
90 | static int parse_cbush(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) |
91 | { | |
92 | static const char* options[] = | |
93 | { | |
94 | "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP", | |
95 | "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK30", "CLK15", "CLK7_5" | |
96 | }; | |
97 | ||
98 | int i; | |
99 | for (i=0; i<sizeof(options)/sizeof(*options); i++) | |
100 | { | |
101 | if (!(strcmp(options[i], value))) | |
102 | { | |
103 | *(int *)result = i; | |
104 | return 0; | |
105 | } | |
106 | } | |
107 | ||
108 | cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value); | |
109 | return -1; | |
110 | } | |
111 | ||
112 | static int parse_cbusx(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) | |
113 | { | |
114 | static const char* options[] = | |
115 | { | |
116 | "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP", | |
117 | "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK24", "CLK12", | |
118 | "CLK6", "BAT_DETECT", "BAT_DETECT_NEG", "I2C_TXE", "I2C_RXF", "VBUS_SENSE", | |
119 | "BB_WR", "BB_RD", "TIME_STAMP", "AWAKE" | |
120 | }; | |
121 | ||
e47d7975 | 122 | int i; |
add00ad6 | 123 | for (i=0; i<sizeof(options)/sizeof(*options); i++) |
048eb722 | 124 | { |
add00ad6 | 125 | if (!(strcmp(options[i], value))) |
048eb722 | 126 | { |
add00ad6 RH |
127 | *(int *)result = i; |
128 | return 0; | |
e47d7975 TJ |
129 | } |
130 | } | |
add00ad6 RH |
131 | |
132 | cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value); | |
133 | return -1; | |
e47d7975 TJ |
134 | } |
135 | ||
190fca12 SL |
136 | static int parse_chtype(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) |
137 | { | |
138 | static const struct | |
139 | { | |
140 | char* key; | |
141 | int opt; | |
142 | } options[] = | |
143 | { | |
144 | { "UART", CHANNEL_IS_UART }, | |
145 | { "FIFO", CHANNEL_IS_FIFO }, | |
146 | { "OPTO", CHANNEL_IS_OPTO }, | |
147 | { "CPU", CHANNEL_IS_CPU }, | |
148 | { "FT1284", CHANNEL_IS_FT1284} | |
149 | }; | |
150 | ||
151 | int i; | |
152 | for (i=0; i<sizeof(options)/sizeof(*options); i++) | |
153 | { | |
154 | if (!(strcmp(options[i].key, value))) | |
155 | { | |
156 | *(int *)result = options[i].opt; | |
157 | return 0; | |
158 | } | |
159 | } | |
160 | ||
161 | cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value); | |
162 | return -1; | |
163 | } | |
164 | ||
d327f924 TJ |
165 | /** |
166 | * @brief Set eeprom value | |
167 | * | |
168 | * \param ftdi pointer to ftdi_context | |
169 | * \param value_name Enum of the value to set | |
170 | * \param value Value to set | |
171 | * | |
172 | * Function will abort the program on error | |
173 | **/ | |
174 | static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value) | |
175 | { | |
176 | if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0) | |
177 | { | |
178 | printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
179 | exit (-1); | |
180 | } | |
181 | } | |
182 | ||
183 | /** | |
184 | * @brief Get eeprom value | |
185 | * | |
186 | * \param ftdi pointer to ftdi_context | |
187 | * \param value_name Enum of the value to get | |
188 | * \param value Value to get | |
189 | * | |
190 | * Function will abort the program on error | |
191 | **/ | |
192 | static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value) | |
193 | { | |
194 | if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0) | |
195 | { | |
196 | printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
197 | exit (-1); | |
198 | } | |
199 | } | |
200 | ||
f6ac3c25 RH |
201 | static void usage(const char *program) |
202 | { | |
203 | fprintf(stderr, "Syntax: %s [...options...] <config-file>\n", program); | |
204 | fprintf(stderr, "Valid Options:\n"); | |
205 | fprintf(stderr, "--device <description> Specify device to open by description string. One of:\n"); | |
206 | fprintf(stderr, " d:<devicenode>\n"); | |
207 | fprintf(stderr, " i:<vendor>:<product>\n"); | |
208 | fprintf(stderr, " i:<vendor>:<product>:<index>\n"); | |
209 | fprintf(stderr, " s:<vendor>:<product>:<serial>\n"); | |
210 | fprintf(stderr, "--read-eeprom Read eeprom and write to -filename- from config-file\n"); | |
bc7a46d1 | 211 | fprintf(stderr, "--build-eeprom Build eeprom image\n"); |
f6ac3c25 RH |
212 | fprintf(stderr, "--erase-eeprom Erase eeprom\n"); |
213 | fprintf(stderr, "--flash-eeprom Flash eeprom\n"); | |
214 | } | |
215 | ||
e47d7975 TJ |
216 | int main(int argc, char *argv[]) |
217 | { | |
218 | /* | |
219 | configuration options | |
220 | */ | |
221 | cfg_opt_t opts[] = | |
222 | { | |
223 | CFG_INT("vendor_id", 0, 0), | |
224 | CFG_INT("product_id", 0, 0), | |
225 | CFG_BOOL("self_powered", cfg_true, 0), | |
226 | CFG_BOOL("remote_wakeup", cfg_true, 0), | |
e47d7975 TJ |
227 | CFG_BOOL("in_is_isochronous", cfg_false, 0), |
228 | CFG_BOOL("out_is_isochronous", cfg_false, 0), | |
229 | CFG_BOOL("suspend_pull_downs", cfg_false, 0), | |
230 | CFG_BOOL("use_serial", cfg_false, 0), | |
231 | CFG_BOOL("change_usb_version", cfg_false, 0), | |
232 | CFG_INT("usb_version", 0, 0), | |
89e7cfb5 | 233 | CFG_INT("default_pid", 0x6001, 0), |
e47d7975 TJ |
234 | CFG_INT("max_power", 0, 0), |
235 | CFG_STR("manufacturer", "Acme Inc.", 0), | |
236 | CFG_STR("product", "USB Serial Converter", 0), | |
237 | CFG_STR("serial", "08-15", 0), | |
faa85774 | 238 | CFG_INT("eeprom_type", 0x00, 0), |
e47d7975 TJ |
239 | CFG_STR("filename", "", 0), |
240 | CFG_BOOL("flash_raw", cfg_false, 0), | |
241 | CFG_BOOL("high_current", cfg_false, 0), | |
add00ad6 RH |
242 | CFG_INT_CB("cbus0", -1, 0, parse_cbus), |
243 | CFG_INT_CB("cbus1", -1, 0, parse_cbus), | |
244 | CFG_INT_CB("cbus2", -1, 0, parse_cbus), | |
245 | CFG_INT_CB("cbus3", -1, 0, parse_cbus), | |
246 | CFG_INT_CB("cbus4", -1, 0, parse_cbus), | |
247 | CFG_INT_CB("cbush0", -1, 0, parse_cbush), | |
248 | CFG_INT_CB("cbush1", -1, 0, parse_cbush), | |
249 | CFG_INT_CB("cbush2", -1, 0, parse_cbush), | |
250 | CFG_INT_CB("cbush3", -1, 0, parse_cbush), | |
251 | CFG_INT_CB("cbush4", -1, 0, parse_cbush), | |
252 | CFG_INT_CB("cbush5", -1, 0, parse_cbush), | |
253 | CFG_INT_CB("cbush6", -1, 0, parse_cbush), | |
254 | CFG_INT_CB("cbush7", -1, 0, parse_cbush), | |
255 | CFG_INT_CB("cbush8", -1, 0, parse_cbush), | |
256 | CFG_INT_CB("cbush9", -1, 0, parse_cbush), | |
257 | CFG_INT_CB("cbusx0", -1, 0, parse_cbusx), | |
258 | CFG_INT_CB("cbusx1", -1, 0, parse_cbusx), | |
259 | CFG_INT_CB("cbusx2", -1, 0, parse_cbusx), | |
260 | CFG_INT_CB("cbusx3", -1, 0, parse_cbusx), | |
c7db7f11 | 261 | CFG_INT_CB("group0_drive", -1, 0, parse_group0_drive), |
e47d7975 TJ |
262 | CFG_BOOL("invert_txd", cfg_false, 0), |
263 | CFG_BOOL("invert_rxd", cfg_false, 0), | |
264 | CFG_BOOL("invert_rts", cfg_false, 0), | |
265 | CFG_BOOL("invert_cts", cfg_false, 0), | |
266 | CFG_BOOL("invert_dtr", cfg_false, 0), | |
267 | CFG_BOOL("invert_dsr", cfg_false, 0), | |
268 | CFG_BOOL("invert_dcd", cfg_false, 0), | |
269 | CFG_BOOL("invert_ri", cfg_false, 0), | |
190fca12 SL |
270 | CFG_INT_CB("cha_type", -1, 0, parse_chtype), |
271 | CFG_INT_CB("chb_type", -1, 0, parse_chtype), | |
272 | CFG_BOOL("cha_vcp", cfg_true, 0), | |
273 | CFG_BOOL("chb_vcp", cfg_true, 0), | |
274 | CFG_BOOL("chc_vcp", cfg_true, 0), | |
275 | CFG_BOOL("chd_vcp", cfg_true, 0), | |
276 | CFG_BOOL("cha_rs485", cfg_false, 0), | |
277 | CFG_BOOL("chb_rs485", cfg_false, 0), | |
278 | CFG_BOOL("chc_rs485", cfg_false, 0), | |
279 | CFG_BOOL("chd_rs485", cfg_false, 0), | |
a0763384 | 280 | CFG_FUNC("include", &cfg_include), |
6e962b9a SET |
281 | CFG_INT("user_data_addr", 0x18, 0), |
282 | CFG_STR("user_data_file", "", 0), | |
e47d7975 TJ |
283 | CFG_END() |
284 | }; | |
285 | cfg_t *cfg; | |
286 | ||
287 | /* | |
288 | normal variables | |
289 | */ | |
f6ac3c25 RH |
290 | enum { |
291 | COMMAND_READ = 1, | |
292 | COMMAND_ERASE, | |
bc7a46d1 SET |
293 | COMMAND_FLASH, |
294 | COMMAND_BUILD | |
f6ac3c25 RH |
295 | } command = 0; |
296 | const char *cfg_filename = NULL; | |
297 | const char *device_description = NULL; | |
6e962b9a SET |
298 | const char *user_data_file = NULL; |
299 | char *user_data_buffer = NULL; | |
200bd3ed | 300 | |
faa85774 | 301 | const int max_eeprom_size = 256; |
785ddbca | 302 | int my_eeprom_size = 0; |
4359228a | 303 | unsigned char *eeprom_buf = NULL; |
e47d7975 TJ |
304 | char *filename; |
305 | int size_check; | |
f6ac3c25 | 306 | int i; |
e47d7975 TJ |
307 | FILE *fp; |
308 | ||
de4871c4 | 309 | struct ftdi_context *ftdi = NULL; |
e47d7975 | 310 | |
ade814a5 | 311 | printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); |
8a987aa2 | 312 | printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n"); |
e47d7975 | 313 | |
f6ac3c25 RH |
314 | for (i = 1; i < argc; i++) { |
315 | if (*argv[i] != '-') | |
316 | { | |
317 | cfg_filename = argv[i]; | |
318 | } | |
319 | else if (!strcmp(argv[i], "--device")) | |
320 | { | |
321 | if (i+1 >= argc) | |
322 | { | |
323 | usage(argv[0]); | |
324 | exit(-1); | |
325 | } | |
326 | device_description = argv[++i]; | |
327 | } | |
328 | else if (!strcmp(argv[i], "--read-eeprom")) | |
329 | { | |
330 | command = COMMAND_READ; | |
331 | } | |
332 | else if (!strcmp(argv[i], "--erase-eeprom")) | |
333 | { | |
334 | command = COMMAND_ERASE; | |
335 | } | |
336 | else if (!strcmp(argv[i], "--flash-eeprom")) | |
337 | { | |
338 | command = COMMAND_FLASH; | |
339 | } | |
bc7a46d1 SET |
340 | else if (!strcmp(argv[i], "--build-eeprom")) |
341 | { | |
342 | command = COMMAND_BUILD; | |
343 | } | |
94c637b8 UB |
344 | else |
345 | { | |
f6ac3c25 RH |
346 | usage(argv[0]); |
347 | exit(-1); | |
94c637b8 | 348 | } |
e47d7975 | 349 | } |
f6ac3c25 RH |
350 | |
351 | if (!cfg_filename) | |
e47d7975 | 352 | { |
f6ac3c25 RH |
353 | usage(argv[0]); |
354 | exit(-1); | |
e47d7975 TJ |
355 | } |
356 | ||
f6ac3c25 | 357 | if ((fp = fopen(cfg_filename, "r")) == NULL) |
e47d7975 TJ |
358 | { |
359 | printf ("Can't open configuration file\n"); | |
360 | exit (-1); | |
361 | } | |
362 | fclose (fp); | |
363 | ||
364 | cfg = cfg_init(opts, 0); | |
f6ac3c25 | 365 | cfg_parse(cfg, cfg_filename); |
e47d7975 TJ |
366 | filename = cfg_getstr(cfg, "filename"); |
367 | ||
368 | if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) | |
369 | printf("Hint: Self powered devices should have a max_power setting of 0.\n"); | |
370 | ||
de4871c4 TJ |
371 | if ((ftdi = ftdi_new()) == 0) |
372 | { | |
373 | fprintf(stderr, "Failed to allocate ftdi structure :%s \n", | |
374 | ftdi_get_error_string(ftdi)); | |
375 | return EXIT_FAILURE; | |
376 | } | |
377 | ||
f6ac3c25 RH |
378 | if (device_description != NULL) |
379 | { | |
380 | i = ftdi_usb_open_string(ftdi, device_description); | |
381 | ||
382 | if (i != 0) | |
383 | { | |
384 | printf("Unable to find FTDI device with description: %s\n", | |
385 | device_description); | |
386 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); | |
387 | exit (-1); | |
388 | } | |
389 | } | |
390 | else if (command > 0) | |
e47d7975 | 391 | { |
7bdae7cd UB |
392 | int vendor_id = cfg_getint(cfg, "vendor_id"); |
393 | int product_id = cfg_getint(cfg, "product_id"); | |
d327f924 TJ |
394 | |
395 | i = ftdi_usb_open(ftdi, vendor_id, product_id); | |
e47d7975 | 396 | |
e8d32978 | 397 | if (i != 0) |
e47d7975 | 398 | { |
89e7cfb5 | 399 | int default_pid = cfg_getint(cfg, "default_pid"); |
d327f924 | 400 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id); |
de4871c4 | 401 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); |
89e7cfb5 | 402 | printf("Retrying with default FTDI pid=%#04x.\n", default_pid); |
e47d7975 | 403 | |
89e7cfb5 | 404 | i = ftdi_usb_open(ftdi, 0x0403, default_pid); |
e47d7975 TJ |
405 | if (i != 0) |
406 | { | |
de4871c4 | 407 | printf("Error: %s\n", ftdi->error_str); |
e47d7975 TJ |
408 | exit (-1); |
409 | } | |
410 | } | |
411 | } | |
048eb722 TJ |
412 | ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), |
413 | cfg_getstr(cfg, "product"), | |
e8d32978 | 414 | cfg_getstr(cfg, "serial")); |
faa85774 | 415 | |
e8d32978 UB |
416 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi)); |
417 | eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); | |
e8d32978 | 418 | printf("EEPROM size: %d\n", my_eeprom_size); |
faa85774 | 419 | |
f6ac3c25 | 420 | if (command == COMMAND_READ) |
e47d7975 | 421 | { |
61620879 | 422 | ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */); |
e47d7975 | 423 | |
4359228a UB |
424 | eeprom_buf = malloc(my_eeprom_size); |
425 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); | |
426 | ||
427 | if (eeprom_buf == NULL) | |
428 | { | |
429 | fprintf(stderr, "Malloc failed, aborting\n"); | |
430 | goto cleanup; | |
431 | } | |
e47d7975 TJ |
432 | if (filename != NULL && strlen(filename) > 0) |
433 | { | |
434 | FILE *fp = fopen (filename, "wb"); | |
701c887b TS |
435 | |
436 | if(fp) | |
437 | { | |
438 | fwrite(eeprom_buf, 1, my_eeprom_size, fp); | |
439 | fclose(fp); | |
440 | } | |
441 | else | |
442 | fprintf(stderr, "Could not open output file %s: %s\n", filename, strerror(errno)); | |
e47d7975 TJ |
443 | } |
444 | else | |
445 | { | |
446 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
447 | } | |
448 | ||
449 | goto cleanup; | |
450 | } | |
451 | ||
7bdae7cd UB |
452 | eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id")); |
453 | eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id")); | |
454 | ||
7bdae7cd UB |
455 | eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered")); |
456 | eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup")); | |
457 | eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power")); | |
458 | ||
459 | eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous")); | |
460 | eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous")); | |
461 | eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs")); | |
462 | ||
463 | eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial")); | |
464 | eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version")); | |
465 | eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version")); | |
faa85774 | 466 | eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type")); |
7bdae7cd UB |
467 | |
468 | eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current")); | |
add00ad6 RH |
469 | |
470 | if (ftdi->type == TYPE_R) | |
471 | { | |
472 | if (cfg_getint(cfg, "cbus0") != -1) | |
473 | eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0")); | |
474 | if (cfg_getint(cfg, "cbus1") != -1) | |
475 | eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1")); | |
476 | if (cfg_getint(cfg, "cbus2") != -1) | |
477 | eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2")); | |
478 | if (cfg_getint(cfg, "cbus3") != -1) | |
479 | eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3")); | |
480 | if (cfg_getint(cfg, "cbus4") != -1) | |
481 | eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4")); | |
482 | } | |
483 | else if (ftdi->type == TYPE_232H) | |
484 | { | |
485 | if (cfg_getint(cfg, "cbush0") != -1) | |
486 | eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0")); | |
487 | if (cfg_getint(cfg, "cbush1") != -1) | |
488 | eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1")); | |
489 | if (cfg_getint(cfg, "cbush2") != -1) | |
490 | eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2")); | |
491 | if (cfg_getint(cfg, "cbush3") != -1) | |
492 | eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3")); | |
493 | if (cfg_getint(cfg, "cbush4") != -1) | |
494 | eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4")); | |
495 | if (cfg_getint(cfg, "cbush5") != -1) | |
496 | eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5")); | |
497 | if (cfg_getint(cfg, "cbush6") != -1) | |
498 | eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6")); | |
499 | if (cfg_getint(cfg, "cbush7") != -1) | |
500 | eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7")); | |
501 | if (cfg_getint(cfg, "cbush8") != -1) | |
502 | eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8")); | |
503 | if (cfg_getint(cfg, "cbush9") != -1) | |
504 | eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9")); | |
c7db7f11 CL |
505 | if (cfg_getint(cfg, "group0_drive") != -1) |
506 | eeprom_set_value(ftdi, GROUP0_DRIVE, cfg_getint(cfg, "group0_drive")); | |
add00ad6 RH |
507 | } |
508 | else if (ftdi->type == TYPE_230X) | |
509 | { | |
510 | if (cfg_getint(cfg, "cbusx0") != -1) | |
511 | eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0")); | |
512 | if (cfg_getint(cfg, "cbusx1") != -1) | |
513 | eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1")); | |
514 | if (cfg_getint(cfg, "cbusx2") != -1) | |
515 | eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2")); | |
516 | if (cfg_getint(cfg, "cbusx3") != -1) | |
517 | eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3")); | |
518 | } | |
519 | ||
7bdae7cd UB |
520 | int invert = 0; |
521 | if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD; | |
522 | if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD; | |
523 | if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS; | |
524 | if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS; | |
525 | if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR; | |
526 | if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR; | |
527 | if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD; | |
528 | if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI; | |
529 | eeprom_set_value(ftdi, INVERT, invert); | |
530 | ||
190fca12 SL |
531 | if (cfg_getint(cfg, "cha_type") != -1) |
532 | eeprom_set_value(ftdi, CHANNEL_A_TYPE, cfg_getint(cfg, "cha_type")); | |
533 | if (cfg_getint(cfg, "chb_type") != -1) | |
534 | eeprom_set_value(ftdi, CHANNEL_B_TYPE, cfg_getint(cfg, "chb_type")); | |
535 | ||
536 | eeprom_set_value(ftdi, CHANNEL_A_DRIVER, | |
537 | cfg_getbool(cfg, "cha_vcp") ? DRIVER_VCP : 0); | |
538 | eeprom_set_value(ftdi, CHANNEL_B_DRIVER, | |
539 | cfg_getbool(cfg, "chb_vcp") ? DRIVER_VCP : 0); | |
540 | eeprom_set_value(ftdi, CHANNEL_C_DRIVER, | |
541 | cfg_getbool(cfg, "chc_vcp") ? DRIVER_VCP : 0); | |
542 | eeprom_set_value(ftdi, CHANNEL_D_DRIVER, | |
543 | cfg_getbool(cfg, "chd_vcp") ? DRIVER_VCP : 0); | |
544 | ||
545 | eeprom_set_value(ftdi, CHANNEL_A_RS485, cfg_getbool(cfg, "cha_rs485")); | |
546 | eeprom_set_value(ftdi, CHANNEL_B_RS485, cfg_getbool(cfg, "chb_rs485")); | |
547 | eeprom_set_value(ftdi, CHANNEL_C_RS485, cfg_getbool(cfg, "chc_rs485")); | |
548 | eeprom_set_value(ftdi, CHANNEL_D_RS485, cfg_getbool(cfg, "chd_rs485")); | |
be4bae37 | 549 | |
6e962b9a SET |
550 | /* Arbitrary user data */ |
551 | eeprom_set_value(ftdi, USER_DATA_ADDR, cfg_getint(cfg, "user_data_addr")); | |
552 | user_data_file = cfg_getstr(cfg, "user_data_file"); | |
553 | if (user_data_file && strlen(user_data_file) > 0) | |
554 | { | |
555 | int data_size; | |
556 | struct stat st; | |
557 | ||
558 | printf("User data file: %s\n", user_data_file); | |
559 | /* Allocate a buffer for the user data */ | |
560 | user_data_buffer = (char *)malloc(max_eeprom_size); | |
561 | if (user_data_buffer == NULL) | |
562 | { | |
563 | fprintf(stderr, "Malloc failed, aborting\n"); | |
564 | goto cleanup; | |
565 | } | |
566 | ||
567 | if (stat(user_data_file, &st)) | |
568 | { | |
569 | printf ("Can't stat user data file %s.\n", user_data_file); | |
570 | exit (-1); | |
571 | } | |
572 | if (st.st_size > max_eeprom_size) | |
573 | printf("Warning: %s is too big, only reading %d bytes\n", | |
574 | user_data_file, max_eeprom_size); | |
575 | /* Read the user data file, no more than max_eeprom_size bytes */ | |
576 | FILE *fp = fopen(user_data_file, "rb"); | |
577 | if (fp == NULL) | |
578 | { | |
579 | printf ("Can't open user data file %s.\n", user_data_file); | |
580 | exit (-1); | |
581 | } | |
582 | data_size = fread(user_data_buffer, 1, max_eeprom_size, fp); | |
583 | fclose(fp); | |
584 | if (data_size < 1) | |
585 | { | |
586 | printf ("Can't read user data file %s.\n", user_data_file); | |
587 | exit (-1); | |
588 | } | |
589 | printf("User data size: %d\n", data_size); | |
590 | ||
591 | ftdi_set_eeprom_user_data(ftdi, user_data_buffer, data_size); | |
592 | } | |
593 | ||
594 | ||
f6ac3c25 | 595 | if (command == COMMAND_ERASE) |
e47d7975 | 596 | { |
de4871c4 | 597 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi)); |
e47d7975 TJ |
598 | } |
599 | ||
de4871c4 | 600 | size_check = ftdi_eeprom_build(ftdi); |
faa85774 | 601 | eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); |
e47d7975 TJ |
602 | |
603 | if (size_check == -1) | |
604 | { | |
6e962b9a | 605 | printf ("Sorry, the eeprom can only contain %d bytes.\n", my_eeprom_size); |
e47d7975 | 606 | goto cleanup; |
048eb722 TJ |
607 | } |
608 | else if (size_check < 0) | |
609 | { | |
e47d7975 | 610 | printf ("ftdi_eeprom_build(): error: %d\n", size_check); |
f6ac3c25 | 611 | goto cleanup; |
e47d7975 TJ |
612 | } |
613 | else | |
614 | { | |
200bd3ed | 615 | printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check); |
e47d7975 TJ |
616 | } |
617 | ||
f6ac3c25 | 618 | if (command == COMMAND_FLASH) |
e47d7975 TJ |
619 | { |
620 | if (cfg_getbool(cfg, "flash_raw")) | |
621 | { | |
622 | if (filename != NULL && strlen(filename) > 0) | |
623 | { | |
faa85774 | 624 | eeprom_buf = malloc(max_eeprom_size); |
e47d7975 | 625 | FILE *fp = fopen(filename, "rb"); |
faa85774 AL |
626 | if (fp == NULL) |
627 | { | |
628 | printf ("Can't open eeprom file %s.\n", filename); | |
629 | exit (-1); | |
630 | } | |
631 | my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp); | |
e47d7975 | 632 | fclose(fp); |
faa85774 AL |
633 | if (my_eeprom_size < 128) |
634 | { | |
635 | printf ("Can't read eeprom file %s.\n", filename); | |
636 | exit (-1); | |
637 | } | |
200bd3ed | 638 | |
23d7ac2b TJ |
639 | printf("Flashing raw eeprom from file %s (%d bytes)\n", |
640 | filename, my_eeprom_size); | |
641 | ||
98c974c7 | 642 | ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
23d7ac2b TJ |
643 | } else |
644 | { | |
645 | printf ("ERROR: flash_raw mode enabled, but no eeprom filename " | |
646 | "given in config file.\n"); | |
647 | exit (-1); | |
e47d7975 TJ |
648 | } |
649 | } | |
de4871c4 | 650 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi)); |
2e48e9fd | 651 | libusb_reset_device(ftdi->usb_dev); |
e47d7975 TJ |
652 | } |
653 | ||
654 | // Write to file? | |
643a89ea | 655 | if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw")) |
e47d7975 TJ |
656 | { |
657 | fp = fopen(filename, "w"); | |
658 | if (fp == NULL) | |
659 | { | |
660 | printf ("Can't write eeprom file.\n"); | |
661 | exit (-1); | |
662 | } | |
663 | else | |
664 | printf ("Writing to file: %s\n", filename); | |
665 | ||
243f5f66 AL |
666 | if (eeprom_buf == NULL) |
667 | eeprom_buf = malloc(my_eeprom_size); | |
200bd3ed TJ |
668 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
669 | ||
670 | fwrite(eeprom_buf, my_eeprom_size, 1, fp); | |
e47d7975 TJ |
671 | fclose(fp); |
672 | } | |
673 | ||
674 | cleanup: | |
4359228a UB |
675 | if (eeprom_buf) |
676 | free(eeprom_buf); | |
6e962b9a SET |
677 | if (user_data_buffer) |
678 | free(user_data_buffer); | |
f6ac3c25 | 679 | if (command > 0) |
e47d7975 | 680 | { |
de4871c4 | 681 | printf("FTDI close: %d\n", ftdi_usb_close(ftdi)); |
e47d7975 TJ |
682 | } |
683 | ||
de4871c4 | 684 | ftdi_deinit (ftdi); |
4e494b76 | 685 | ftdi_free (ftdi); |
e47d7975 TJ |
686 | |
687 | cfg_free(cfg); | |
688 | ||
689 | printf("\n"); | |
690 | return 0; | |
691 | } |