Commit | Line | Data |
---|---|---|
e47d7975 TJ |
1 | /*************************************************************************** |
2 | main.c - description | |
3 | ------------------- | |
4 | begin : Mon Apr 7 12:05:22 CEST 2003 | |
dcd7e8a3 | 5 | copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers |
e47d7975 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 | |
d327f924 | 44 | static int str_to_cbus(char *str, int max_allowed) |
e47d7975 | 45 | { |
048eb722 TJ |
46 | #define MAX_OPTION 14 |
47 | const char* options[MAX_OPTION] = | |
48 | { | |
49 | "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", | |
50 | "CLK48", "CLK24", "CLK12", "CLK6", | |
51 | "IO_MODE", "BITBANG_WR", "BITBANG_RD", "SPECIAL" | |
52 | }; | |
e47d7975 TJ |
53 | int i; |
54 | max_allowed += 1; | |
55 | if (max_allowed > MAX_OPTION) max_allowed = MAX_OPTION; | |
048eb722 TJ |
56 | for (i=0; i<max_allowed; i++) |
57 | { | |
58 | if (!(strcmp(options[i], str))) | |
59 | { | |
e47d7975 TJ |
60 | return i; |
61 | } | |
62 | } | |
63 | printf("WARNING: Invalid cbus option '%s'\n", str); | |
64 | return 0; | |
65 | } | |
66 | ||
d327f924 TJ |
67 | /** |
68 | * @brief Set eeprom value | |
69 | * | |
70 | * \param ftdi pointer to ftdi_context | |
71 | * \param value_name Enum of the value to set | |
72 | * \param value Value to set | |
73 | * | |
74 | * Function will abort the program on error | |
75 | **/ | |
76 | static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value) | |
77 | { | |
78 | if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0) | |
79 | { | |
80 | printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
81 | exit (-1); | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * @brief Get eeprom value | |
87 | * | |
88 | * \param ftdi pointer to ftdi_context | |
89 | * \param value_name Enum of the value to get | |
90 | * \param value Value to get | |
91 | * | |
92 | * Function will abort the program on error | |
93 | **/ | |
94 | static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value) | |
95 | { | |
96 | if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0) | |
97 | { | |
98 | printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
99 | exit (-1); | |
100 | } | |
101 | } | |
102 | ||
e47d7975 TJ |
103 | int main(int argc, char *argv[]) |
104 | { | |
105 | /* | |
106 | configuration options | |
107 | */ | |
108 | cfg_opt_t opts[] = | |
109 | { | |
110 | CFG_INT("vendor_id", 0, 0), | |
111 | CFG_INT("product_id", 0, 0), | |
112 | CFG_BOOL("self_powered", cfg_true, 0), | |
113 | CFG_BOOL("remote_wakeup", cfg_true, 0), | |
e47d7975 TJ |
114 | CFG_BOOL("in_is_isochronous", cfg_false, 0), |
115 | CFG_BOOL("out_is_isochronous", cfg_false, 0), | |
116 | CFG_BOOL("suspend_pull_downs", cfg_false, 0), | |
117 | CFG_BOOL("use_serial", cfg_false, 0), | |
118 | CFG_BOOL("change_usb_version", cfg_false, 0), | |
119 | CFG_INT("usb_version", 0, 0), | |
89e7cfb5 | 120 | CFG_INT("default_pid", 0x6001, 0), |
e47d7975 TJ |
121 | CFG_INT("max_power", 0, 0), |
122 | CFG_STR("manufacturer", "Acme Inc.", 0), | |
123 | CFG_STR("product", "USB Serial Converter", 0), | |
124 | CFG_STR("serial", "08-15", 0), | |
faa85774 | 125 | CFG_INT("eeprom_type", 0x00, 0), |
e47d7975 TJ |
126 | CFG_STR("filename", "", 0), |
127 | CFG_BOOL("flash_raw", cfg_false, 0), | |
128 | CFG_BOOL("high_current", cfg_false, 0), | |
129 | CFG_STR_LIST("cbus0", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
130 | CFG_STR_LIST("cbus1", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
131 | CFG_STR_LIST("cbus2", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
132 | CFG_STR_LIST("cbus3", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
133 | CFG_STR_LIST("cbus4", "{TXDEN,PWRON,RXLED,TXLED,TX_RX_LED,SLEEP,CLK48,CLK24,CLK12,CLK6}", 0), | |
134 | CFG_BOOL("invert_txd", cfg_false, 0), | |
135 | CFG_BOOL("invert_rxd", cfg_false, 0), | |
136 | CFG_BOOL("invert_rts", cfg_false, 0), | |
137 | CFG_BOOL("invert_cts", cfg_false, 0), | |
138 | CFG_BOOL("invert_dtr", cfg_false, 0), | |
139 | CFG_BOOL("invert_dsr", cfg_false, 0), | |
140 | CFG_BOOL("invert_dcd", cfg_false, 0), | |
141 | CFG_BOOL("invert_ri", cfg_false, 0), | |
142 | CFG_END() | |
143 | }; | |
144 | cfg_t *cfg; | |
145 | ||
146 | /* | |
147 | normal variables | |
148 | */ | |
149 | int _read = 0, _erase = 0, _flash = 0; | |
200bd3ed | 150 | |
faa85774 | 151 | const int max_eeprom_size = 256; |
785ddbca | 152 | int my_eeprom_size = 0; |
4359228a | 153 | unsigned char *eeprom_buf = NULL; |
e47d7975 TJ |
154 | char *filename; |
155 | int size_check; | |
156 | int i, argc_filename; | |
157 | FILE *fp; | |
158 | ||
de4871c4 | 159 | struct ftdi_context *ftdi = NULL; |
e47d7975 | 160 | |
ade814a5 | 161 | printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); |
8a987aa2 | 162 | printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n"); |
e47d7975 TJ |
163 | |
164 | if (argc != 2 && argc != 3) | |
165 | { | |
166 | printf("Syntax: %s [commands] config-file\n", argv[0]); | |
167 | printf("Valid commands:\n"); | |
168 | printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); | |
169 | printf("--erase-eeprom Erase eeprom\n"); | |
170 | printf("--flash-eeprom Flash eeprom\n"); | |
171 | exit (-1); | |
172 | } | |
173 | ||
174 | if (argc == 3) | |
175 | { | |
176 | if (strcmp(argv[1], "--read-eeprom") == 0) | |
177 | _read = 1; | |
94c637b8 | 178 | else if (strcmp(argv[1], "--erase-eeprom") == 0) |
e47d7975 | 179 | _erase = 1; |
94c637b8 | 180 | else if (strcmp(argv[1], "--flash-eeprom") == 0) |
e47d7975 | 181 | _flash = 1; |
94c637b8 UB |
182 | else |
183 | { | |
184 | printf ("Can't open configuration file\n"); | |
185 | exit (-1); | |
186 | } | |
e47d7975 TJ |
187 | argc_filename = 2; |
188 | } | |
189 | else | |
190 | { | |
191 | argc_filename = 1; | |
192 | } | |
193 | ||
194 | if ((fp = fopen(argv[argc_filename], "r")) == NULL) | |
195 | { | |
196 | printf ("Can't open configuration file\n"); | |
197 | exit (-1); | |
198 | } | |
199 | fclose (fp); | |
200 | ||
201 | cfg = cfg_init(opts, 0); | |
202 | cfg_parse(cfg, argv[argc_filename]); | |
203 | filename = cfg_getstr(cfg, "filename"); | |
204 | ||
205 | if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) | |
206 | printf("Hint: Self powered devices should have a max_power setting of 0.\n"); | |
207 | ||
de4871c4 TJ |
208 | if ((ftdi = ftdi_new()) == 0) |
209 | { | |
210 | fprintf(stderr, "Failed to allocate ftdi structure :%s \n", | |
211 | ftdi_get_error_string(ftdi)); | |
212 | return EXIT_FAILURE; | |
213 | } | |
214 | ||
e47d7975 TJ |
215 | if (_read > 0 || _erase > 0 || _flash > 0) |
216 | { | |
7bdae7cd UB |
217 | int vendor_id = cfg_getint(cfg, "vendor_id"); |
218 | int product_id = cfg_getint(cfg, "product_id"); | |
d327f924 TJ |
219 | |
220 | i = ftdi_usb_open(ftdi, vendor_id, product_id); | |
e47d7975 | 221 | |
e8d32978 | 222 | if (i != 0) |
e47d7975 | 223 | { |
89e7cfb5 | 224 | int default_pid = cfg_getint(cfg, "default_pid"); |
d327f924 | 225 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id); |
de4871c4 | 226 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); |
89e7cfb5 | 227 | printf("Retrying with default FTDI pid=%#04x.\n", default_pid); |
e47d7975 | 228 | |
89e7cfb5 | 229 | i = ftdi_usb_open(ftdi, 0x0403, default_pid); |
e47d7975 TJ |
230 | if (i != 0) |
231 | { | |
de4871c4 | 232 | printf("Error: %s\n", ftdi->error_str); |
e47d7975 TJ |
233 | exit (-1); |
234 | } | |
235 | } | |
236 | } | |
048eb722 TJ |
237 | ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), |
238 | cfg_getstr(cfg, "product"), | |
e8d32978 | 239 | cfg_getstr(cfg, "serial")); |
faa85774 | 240 | |
e8d32978 UB |
241 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi)); |
242 | eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); | |
e8d32978 | 243 | printf("EEPROM size: %d\n", my_eeprom_size); |
faa85774 | 244 | |
e47d7975 TJ |
245 | if (_read > 0) |
246 | { | |
61620879 | 247 | ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */); |
e47d7975 | 248 | |
4359228a UB |
249 | eeprom_buf = malloc(my_eeprom_size); |
250 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); | |
251 | ||
252 | if (eeprom_buf == NULL) | |
253 | { | |
254 | fprintf(stderr, "Malloc failed, aborting\n"); | |
255 | goto cleanup; | |
256 | } | |
e47d7975 TJ |
257 | if (filename != NULL && strlen(filename) > 0) |
258 | { | |
200bd3ed | 259 | |
e47d7975 | 260 | FILE *fp = fopen (filename, "wb"); |
200bd3ed | 261 | fwrite (eeprom_buf, 1, my_eeprom_size, fp); |
e47d7975 TJ |
262 | fclose (fp); |
263 | } | |
264 | else | |
265 | { | |
266 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
267 | } | |
268 | ||
269 | goto cleanup; | |
270 | } | |
271 | ||
7bdae7cd UB |
272 | eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id")); |
273 | eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id")); | |
274 | ||
7bdae7cd UB |
275 | eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered")); |
276 | eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup")); | |
277 | eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power")); | |
278 | ||
279 | eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous")); | |
280 | eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous")); | |
281 | eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs")); | |
282 | ||
283 | eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial")); | |
284 | eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version")); | |
285 | eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version")); | |
faa85774 | 286 | eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type")); |
7bdae7cd UB |
287 | |
288 | eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current")); | |
289 | eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13)); | |
290 | eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13)); | |
291 | eeprom_set_value(ftdi, CBUS_FUNCTION_2, str_to_cbus(cfg_getstr(cfg, "cbus2"), 13)); | |
292 | eeprom_set_value(ftdi, CBUS_FUNCTION_3, str_to_cbus(cfg_getstr(cfg, "cbus3"), 13)); | |
293 | eeprom_set_value(ftdi, CBUS_FUNCTION_4, str_to_cbus(cfg_getstr(cfg, "cbus4"), 9)); | |
294 | int invert = 0; | |
295 | if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD; | |
296 | if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD; | |
297 | if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS; | |
298 | if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS; | |
299 | if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR; | |
300 | if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR; | |
301 | if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD; | |
302 | if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI; | |
303 | eeprom_set_value(ftdi, INVERT, invert); | |
304 | ||
be4bae37 AL |
305 | eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP); |
306 | eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP); | |
307 | eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP); | |
308 | eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP); | |
309 | eeprom_set_value(ftdi, CHANNEL_A_RS485, 0); | |
310 | eeprom_set_value(ftdi, CHANNEL_B_RS485, 0); | |
311 | eeprom_set_value(ftdi, CHANNEL_C_RS485, 0); | |
312 | eeprom_set_value(ftdi, CHANNEL_D_RS485, 0); | |
313 | ||
e47d7975 TJ |
314 | if (_erase > 0) |
315 | { | |
de4871c4 | 316 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi)); |
e47d7975 TJ |
317 | } |
318 | ||
de4871c4 | 319 | size_check = ftdi_eeprom_build(ftdi); |
faa85774 | 320 | eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); |
e47d7975 TJ |
321 | |
322 | if (size_check == -1) | |
323 | { | |
324 | printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n"); | |
325 | printf ("You need to short your string by: %d bytes\n", size_check); | |
326 | goto cleanup; | |
048eb722 TJ |
327 | } |
328 | else if (size_check < 0) | |
329 | { | |
e47d7975 TJ |
330 | printf ("ftdi_eeprom_build(): error: %d\n", size_check); |
331 | } | |
332 | else | |
333 | { | |
200bd3ed | 334 | printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check); |
e47d7975 TJ |
335 | } |
336 | ||
337 | if (_flash > 0) | |
338 | { | |
339 | if (cfg_getbool(cfg, "flash_raw")) | |
340 | { | |
341 | if (filename != NULL && strlen(filename) > 0) | |
342 | { | |
faa85774 | 343 | eeprom_buf = malloc(max_eeprom_size); |
e47d7975 | 344 | FILE *fp = fopen(filename, "rb"); |
faa85774 AL |
345 | if (fp == NULL) |
346 | { | |
347 | printf ("Can't open eeprom file %s.\n", filename); | |
348 | exit (-1); | |
349 | } | |
350 | my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp); | |
e47d7975 | 351 | fclose(fp); |
faa85774 AL |
352 | if (my_eeprom_size < 128) |
353 | { | |
354 | printf ("Can't read eeprom file %s.\n", filename); | |
355 | exit (-1); | |
356 | } | |
200bd3ed | 357 | |
98c974c7 | 358 | ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
e47d7975 TJ |
359 | } |
360 | } | |
de4871c4 | 361 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi)); |
2e48e9fd | 362 | libusb_reset_device(ftdi->usb_dev); |
e47d7975 TJ |
363 | } |
364 | ||
365 | // Write to file? | |
643a89ea | 366 | if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw")) |
e47d7975 TJ |
367 | { |
368 | fp = fopen(filename, "w"); | |
369 | if (fp == NULL) | |
370 | { | |
371 | printf ("Can't write eeprom file.\n"); | |
372 | exit (-1); | |
373 | } | |
374 | else | |
375 | printf ("Writing to file: %s\n", filename); | |
376 | ||
243f5f66 AL |
377 | if (eeprom_buf == NULL) |
378 | eeprom_buf = malloc(my_eeprom_size); | |
200bd3ed TJ |
379 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
380 | ||
381 | fwrite(eeprom_buf, my_eeprom_size, 1, fp); | |
e47d7975 TJ |
382 | fclose(fp); |
383 | } | |
384 | ||
385 | cleanup: | |
4359228a UB |
386 | if (eeprom_buf) |
387 | free(eeprom_buf); | |
e47d7975 TJ |
388 | if (_read > 0 || _erase > 0 || _flash > 0) |
389 | { | |
de4871c4 | 390 | printf("FTDI close: %d\n", ftdi_usb_close(ftdi)); |
e47d7975 TJ |
391 | } |
392 | ||
de4871c4 | 393 | ftdi_deinit (ftdi); |
4e494b76 | 394 | ftdi_free (ftdi); |
e47d7975 TJ |
395 | |
396 | cfg_free(cfg); | |
397 | ||
398 | printf("\n"); | |
399 | return 0; | |
400 | } |