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