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