fixed ftdi_cbus_func and ftdi_cbush_func enumerations and introduced ftdi_cbusx_func
[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>
38
39#include <confuse.h>
2e48e9fd 40#include <libusb.h>
e47d7975 41#include <ftdi.h>
ade814a5 42#include <ftdi_eeprom_version.h>
e47d7975 43
add00ad6 44static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
e47d7975 45{
add00ad6 46 static const char* options[] =
048eb722 47 {
add00ad6
RH
48 "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48",
49 "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD"
048eb722 50 };
add00ad6
RH
51
52 int i;
53 for (i=0; i<sizeof(options)/sizeof(*options); i++)
54 {
55 if (!(strcmp(options[i], value)))
56 {
57 *(int *)result = i;
58 return 0;
59 }
60 }
61
62 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
63 return -1;
64}
65
66static int parse_cbush(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
67{
68 static const char* options[] =
69 {
70 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
71 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK30", "CLK15", "CLK7_5"
72 };
73
74 int i;
75 for (i=0; i<sizeof(options)/sizeof(*options); i++)
76 {
77 if (!(strcmp(options[i], value)))
78 {
79 *(int *)result = i;
80 return 0;
81 }
82 }
83
84 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
85 return -1;
86}
87
88static int parse_cbusx(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
89{
90 static const char* options[] =
91 {
92 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
93 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK24", "CLK12",
94 "CLK6", "BAT_DETECT", "BAT_DETECT_NEG", "I2C_TXE", "I2C_RXF", "VBUS_SENSE",
95 "BB_WR", "BB_RD", "TIME_STAMP", "AWAKE"
96 };
97
e47d7975 98 int i;
add00ad6 99 for (i=0; i<sizeof(options)/sizeof(*options); i++)
048eb722 100 {
add00ad6 101 if (!(strcmp(options[i], value)))
048eb722 102 {
add00ad6
RH
103 *(int *)result = i;
104 return 0;
e47d7975
TJ
105 }
106 }
add00ad6
RH
107
108 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
109 return -1;
e47d7975
TJ
110}
111
d327f924
TJ
112/**
113 * @brief Set eeprom value
114 *
115 * \param ftdi pointer to ftdi_context
116 * \param value_name Enum of the value to set
117 * \param value Value to set
118 *
119 * Function will abort the program on error
120 **/
121static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
122{
123 if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0)
124 {
125 printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
126 exit (-1);
127 }
128}
129
130/**
131 * @brief Get eeprom value
132 *
133 * \param ftdi pointer to ftdi_context
134 * \param value_name Enum of the value to get
135 * \param value Value to get
136 *
137 * Function will abort the program on error
138 **/
139static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
140{
141 if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0)
142 {
143 printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
144 exit (-1);
145 }
146}
147
e47d7975
TJ
148int main(int argc, char *argv[])
149{
150 /*
151 configuration options
152 */
153 cfg_opt_t opts[] =
154 {
155 CFG_INT("vendor_id", 0, 0),
156 CFG_INT("product_id", 0, 0),
157 CFG_BOOL("self_powered", cfg_true, 0),
158 CFG_BOOL("remote_wakeup", cfg_true, 0),
e47d7975
TJ
159 CFG_BOOL("in_is_isochronous", cfg_false, 0),
160 CFG_BOOL("out_is_isochronous", cfg_false, 0),
161 CFG_BOOL("suspend_pull_downs", cfg_false, 0),
162 CFG_BOOL("use_serial", cfg_false, 0),
163 CFG_BOOL("change_usb_version", cfg_false, 0),
164 CFG_INT("usb_version", 0, 0),
89e7cfb5 165 CFG_INT("default_pid", 0x6001, 0),
e47d7975
TJ
166 CFG_INT("max_power", 0, 0),
167 CFG_STR("manufacturer", "Acme Inc.", 0),
168 CFG_STR("product", "USB Serial Converter", 0),
169 CFG_STR("serial", "08-15", 0),
faa85774 170 CFG_INT("eeprom_type", 0x00, 0),
e47d7975
TJ
171 CFG_STR("filename", "", 0),
172 CFG_BOOL("flash_raw", cfg_false, 0),
173 CFG_BOOL("high_current", cfg_false, 0),
add00ad6
RH
174 CFG_INT_CB("cbus0", -1, 0, parse_cbus),
175 CFG_INT_CB("cbus1", -1, 0, parse_cbus),
176 CFG_INT_CB("cbus2", -1, 0, parse_cbus),
177 CFG_INT_CB("cbus3", -1, 0, parse_cbus),
178 CFG_INT_CB("cbus4", -1, 0, parse_cbus),
179 CFG_INT_CB("cbush0", -1, 0, parse_cbush),
180 CFG_INT_CB("cbush1", -1, 0, parse_cbush),
181 CFG_INT_CB("cbush2", -1, 0, parse_cbush),
182 CFG_INT_CB("cbush3", -1, 0, parse_cbush),
183 CFG_INT_CB("cbush4", -1, 0, parse_cbush),
184 CFG_INT_CB("cbush5", -1, 0, parse_cbush),
185 CFG_INT_CB("cbush6", -1, 0, parse_cbush),
186 CFG_INT_CB("cbush7", -1, 0, parse_cbush),
187 CFG_INT_CB("cbush8", -1, 0, parse_cbush),
188 CFG_INT_CB("cbush9", -1, 0, parse_cbush),
189 CFG_INT_CB("cbusx0", -1, 0, parse_cbusx),
190 CFG_INT_CB("cbusx1", -1, 0, parse_cbusx),
191 CFG_INT_CB("cbusx2", -1, 0, parse_cbusx),
192 CFG_INT_CB("cbusx3", -1, 0, parse_cbusx),
e47d7975
TJ
193 CFG_BOOL("invert_txd", cfg_false, 0),
194 CFG_BOOL("invert_rxd", cfg_false, 0),
195 CFG_BOOL("invert_rts", cfg_false, 0),
196 CFG_BOOL("invert_cts", cfg_false, 0),
197 CFG_BOOL("invert_dtr", cfg_false, 0),
198 CFG_BOOL("invert_dsr", cfg_false, 0),
199 CFG_BOOL("invert_dcd", cfg_false, 0),
200 CFG_BOOL("invert_ri", cfg_false, 0),
201 CFG_END()
202 };
203 cfg_t *cfg;
204
205 /*
206 normal variables
207 */
208 int _read = 0, _erase = 0, _flash = 0;
200bd3ed 209
faa85774 210 const int max_eeprom_size = 256;
785ddbca 211 int my_eeprom_size = 0;
4359228a 212 unsigned char *eeprom_buf = NULL;
e47d7975
TJ
213 char *filename;
214 int size_check;
215 int i, argc_filename;
216 FILE *fp;
217
de4871c4 218 struct ftdi_context *ftdi = NULL;
e47d7975 219
ade814a5 220 printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
8a987aa2 221 printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n");
e47d7975
TJ
222
223 if (argc != 2 && argc != 3)
224 {
225 printf("Syntax: %s [commands] config-file\n", argv[0]);
226 printf("Valid commands:\n");
227 printf("--read-eeprom Read eeprom and write to -filename- from config-file\n");
228 printf("--erase-eeprom Erase eeprom\n");
229 printf("--flash-eeprom Flash eeprom\n");
230 exit (-1);
231 }
232
233 if (argc == 3)
234 {
235 if (strcmp(argv[1], "--read-eeprom") == 0)
236 _read = 1;
94c637b8 237 else if (strcmp(argv[1], "--erase-eeprom") == 0)
e47d7975 238 _erase = 1;
94c637b8 239 else if (strcmp(argv[1], "--flash-eeprom") == 0)
e47d7975 240 _flash = 1;
94c637b8
UB
241 else
242 {
243 printf ("Can't open configuration file\n");
244 exit (-1);
245 }
e47d7975
TJ
246 argc_filename = 2;
247 }
248 else
249 {
250 argc_filename = 1;
251 }
252
253 if ((fp = fopen(argv[argc_filename], "r")) == NULL)
254 {
255 printf ("Can't open configuration file\n");
256 exit (-1);
257 }
258 fclose (fp);
259
260 cfg = cfg_init(opts, 0);
261 cfg_parse(cfg, argv[argc_filename]);
262 filename = cfg_getstr(cfg, "filename");
263
264 if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
265 printf("Hint: Self powered devices should have a max_power setting of 0.\n");
266
de4871c4
TJ
267 if ((ftdi = ftdi_new()) == 0)
268 {
269 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
270 ftdi_get_error_string(ftdi));
271 return EXIT_FAILURE;
272 }
273
e47d7975
TJ
274 if (_read > 0 || _erase > 0 || _flash > 0)
275 {
7bdae7cd
UB
276 int vendor_id = cfg_getint(cfg, "vendor_id");
277 int product_id = cfg_getint(cfg, "product_id");
d327f924
TJ
278
279 i = ftdi_usb_open(ftdi, vendor_id, product_id);
e47d7975 280
e8d32978 281 if (i != 0)
e47d7975 282 {
89e7cfb5 283 int default_pid = cfg_getint(cfg, "default_pid");
d327f924 284 printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
de4871c4 285 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
89e7cfb5 286 printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
e47d7975 287
89e7cfb5 288 i = ftdi_usb_open(ftdi, 0x0403, default_pid);
e47d7975
TJ
289 if (i != 0)
290 {
de4871c4 291 printf("Error: %s\n", ftdi->error_str);
e47d7975
TJ
292 exit (-1);
293 }
294 }
295 }
048eb722
TJ
296 ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"),
297 cfg_getstr(cfg, "product"),
e8d32978 298 cfg_getstr(cfg, "serial"));
faa85774 299
e8d32978
UB
300 printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
301 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
e8d32978 302 printf("EEPROM size: %d\n", my_eeprom_size);
faa85774 303
e47d7975
TJ
304 if (_read > 0)
305 {
61620879 306 ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
e47d7975 307
4359228a
UB
308 eeprom_buf = malloc(my_eeprom_size);
309 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
310
311 if (eeprom_buf == NULL)
312 {
313 fprintf(stderr, "Malloc failed, aborting\n");
314 goto cleanup;
315 }
e47d7975
TJ
316 if (filename != NULL && strlen(filename) > 0)
317 {
200bd3ed 318
e47d7975 319 FILE *fp = fopen (filename, "wb");
200bd3ed 320 fwrite (eeprom_buf, 1, my_eeprom_size, fp);
e47d7975
TJ
321 fclose (fp);
322 }
323 else
324 {
325 printf("Warning: Not writing eeprom, you must supply a valid filename\n");
326 }
327
328 goto cleanup;
329 }
330
7bdae7cd
UB
331 eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
332 eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
333
7bdae7cd
UB
334 eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
335 eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
336 eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
337
338 eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
339 eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
340 eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));
341
342 eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
343 eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
344 eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
faa85774 345 eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));
7bdae7cd
UB
346
347 eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
add00ad6
RH
348
349 if (ftdi->type == TYPE_R)
350 {
351 if (cfg_getint(cfg, "cbus0") != -1)
352 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0"));
353 if (cfg_getint(cfg, "cbus1") != -1)
354 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1"));
355 if (cfg_getint(cfg, "cbus2") != -1)
356 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2"));
357 if (cfg_getint(cfg, "cbus3") != -1)
358 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3"));
359 if (cfg_getint(cfg, "cbus4") != -1)
360 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4"));
361 }
362 else if (ftdi->type == TYPE_232H)
363 {
364 if (cfg_getint(cfg, "cbush0") != -1)
365 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0"));
366 if (cfg_getint(cfg, "cbush1") != -1)
367 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1"));
368 if (cfg_getint(cfg, "cbush2") != -1)
369 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2"));
370 if (cfg_getint(cfg, "cbush3") != -1)
371 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3"));
372 if (cfg_getint(cfg, "cbush4") != -1)
373 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4"));
374 if (cfg_getint(cfg, "cbush5") != -1)
375 eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5"));
376 if (cfg_getint(cfg, "cbush6") != -1)
377 eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6"));
378 if (cfg_getint(cfg, "cbush7") != -1)
379 eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7"));
380 if (cfg_getint(cfg, "cbush8") != -1)
381 eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8"));
382 if (cfg_getint(cfg, "cbush9") != -1)
383 eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9"));
384 }
385 else if (ftdi->type == TYPE_230X)
386 {
387 if (cfg_getint(cfg, "cbusx0") != -1)
388 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0"));
389 if (cfg_getint(cfg, "cbusx1") != -1)
390 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1"));
391 if (cfg_getint(cfg, "cbusx2") != -1)
392 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2"));
393 if (cfg_getint(cfg, "cbusx3") != -1)
394 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3"));
395 }
396
7bdae7cd
UB
397 int invert = 0;
398 if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
399 if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
400 if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
401 if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
402 if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
403 if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
404 if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
405 if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
406 eeprom_set_value(ftdi, INVERT, invert);
407
be4bae37
AL
408 eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
409 eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
410 eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP);
411 eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP);
412 eeprom_set_value(ftdi, CHANNEL_A_RS485, 0);
413 eeprom_set_value(ftdi, CHANNEL_B_RS485, 0);
414 eeprom_set_value(ftdi, CHANNEL_C_RS485, 0);
415 eeprom_set_value(ftdi, CHANNEL_D_RS485, 0);
416
e47d7975
TJ
417 if (_erase > 0)
418 {
de4871c4 419 printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
e47d7975
TJ
420 }
421
de4871c4 422 size_check = ftdi_eeprom_build(ftdi);
faa85774 423 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
e47d7975
TJ
424
425 if (size_check == -1)
426 {
427 printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
428 printf ("You need to short your string by: %d bytes\n", size_check);
429 goto cleanup;
048eb722
TJ
430 }
431 else if (size_check < 0)
432 {
e47d7975
TJ
433 printf ("ftdi_eeprom_build(): error: %d\n", size_check);
434 }
435 else
436 {
200bd3ed 437 printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
e47d7975
TJ
438 }
439
440 if (_flash > 0)
441 {
442 if (cfg_getbool(cfg, "flash_raw"))
443 {
444 if (filename != NULL && strlen(filename) > 0)
445 {
faa85774 446 eeprom_buf = malloc(max_eeprom_size);
e47d7975 447 FILE *fp = fopen(filename, "rb");
faa85774
AL
448 if (fp == NULL)
449 {
450 printf ("Can't open eeprom file %s.\n", filename);
451 exit (-1);
452 }
453 my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
e47d7975 454 fclose(fp);
faa85774
AL
455 if (my_eeprom_size < 128)
456 {
457 printf ("Can't read eeprom file %s.\n", filename);
458 exit (-1);
459 }
200bd3ed 460
98c974c7 461 ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
e47d7975
TJ
462 }
463 }
de4871c4 464 printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
2e48e9fd 465 libusb_reset_device(ftdi->usb_dev);
e47d7975
TJ
466 }
467
468 // Write to file?
643a89ea 469 if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
e47d7975
TJ
470 {
471 fp = fopen(filename, "w");
472 if (fp == NULL)
473 {
474 printf ("Can't write eeprom file.\n");
475 exit (-1);
476 }
477 else
478 printf ("Writing to file: %s\n", filename);
479
243f5f66
AL
480 if (eeprom_buf == NULL)
481 eeprom_buf = malloc(my_eeprom_size);
200bd3ed
TJ
482 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
483
484 fwrite(eeprom_buf, my_eeprom_size, 1, fp);
e47d7975
TJ
485 fclose(fp);
486 }
487
488cleanup:
4359228a
UB
489 if (eeprom_buf)
490 free(eeprom_buf);
e47d7975
TJ
491 if (_read > 0 || _erase > 0 || _flash > 0)
492 {
de4871c4 493 printf("FTDI close: %d\n", ftdi_usb_close(ftdi));
e47d7975
TJ
494 }
495
de4871c4 496 ftdi_deinit (ftdi);
4e494b76 497 ftdi_free (ftdi);
e47d7975
TJ
498
499 cfg_free(cfg);
500
501 printf("\n");
502 return 0;
503}