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