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