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 TJ |
44 | |
45 | int str_to_cbus(char *str, int max_allowed) | |
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 | ||
64 | int main(int argc, char *argv[]) | |
65 | { | |
66 | /* | |
67 | configuration options | |
68 | */ | |
69 | cfg_opt_t opts[] = | |
70 | { | |
71 | CFG_INT("vendor_id", 0, 0), | |
72 | CFG_INT("product_id", 0, 0), | |
73 | CFG_BOOL("self_powered", cfg_true, 0), | |
74 | CFG_BOOL("remote_wakeup", cfg_true, 0), | |
75 | CFG_STR_LIST("chip_type", "{BM,R,other}", 0), | |
76 | CFG_BOOL("in_is_isochronous", cfg_false, 0), | |
77 | CFG_BOOL("out_is_isochronous", cfg_false, 0), | |
78 | CFG_BOOL("suspend_pull_downs", cfg_false, 0), | |
79 | CFG_BOOL("use_serial", cfg_false, 0), | |
80 | CFG_BOOL("change_usb_version", cfg_false, 0), | |
81 | CFG_INT("usb_version", 0, 0), | |
82 | CFG_INT("max_power", 0, 0), | |
83 | CFG_STR("manufacturer", "Acme Inc.", 0), | |
84 | CFG_STR("product", "USB Serial Converter", 0), | |
85 | CFG_STR("serial", "08-15", 0), | |
86 | CFG_STR("filename", "", 0), | |
87 | CFG_BOOL("flash_raw", cfg_false, 0), | |
88 | CFG_BOOL("high_current", cfg_false, 0), | |
89 | CFG_STR_LIST("cbus0", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
90 | CFG_STR_LIST("cbus1", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
91 | CFG_STR_LIST("cbus2", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
92 | CFG_STR_LIST("cbus3", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
93 | CFG_STR_LIST("cbus4", "{TXDEN,PWRON,RXLED,TXLED,TX_RX_LED,SLEEP,CLK48,CLK24,CLK12,CLK6}", 0), | |
94 | CFG_BOOL("invert_txd", cfg_false, 0), | |
95 | CFG_BOOL("invert_rxd", cfg_false, 0), | |
96 | CFG_BOOL("invert_rts", cfg_false, 0), | |
97 | CFG_BOOL("invert_cts", cfg_false, 0), | |
98 | CFG_BOOL("invert_dtr", cfg_false, 0), | |
99 | CFG_BOOL("invert_dsr", cfg_false, 0), | |
100 | CFG_BOOL("invert_dcd", cfg_false, 0), | |
101 | CFG_BOOL("invert_ri", cfg_false, 0), | |
102 | CFG_END() | |
103 | }; | |
104 | cfg_t *cfg; | |
105 | ||
106 | /* | |
107 | normal variables | |
108 | */ | |
109 | int _read = 0, _erase = 0, _flash = 0; | |
fa1def99 | 110 | unsigned char eeprom_buf[128]; // TODO: Kill this and look for other hardcoded places of 128 bytes |
e47d7975 TJ |
111 | char *filename; |
112 | int size_check; | |
113 | int i, argc_filename; | |
114 | FILE *fp; | |
115 | ||
116 | struct ftdi_context ftdi; | |
fa1def99 | 117 | struct ftdi_eeprom *eeprom; |
e47d7975 | 118 | |
ade814a5 | 119 | printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); |
8a987aa2 | 120 | printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n"); |
e47d7975 TJ |
121 | |
122 | if (argc != 2 && argc != 3) | |
123 | { | |
124 | printf("Syntax: %s [commands] config-file\n", argv[0]); | |
125 | printf("Valid commands:\n"); | |
126 | printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); | |
127 | printf("--erase-eeprom Erase eeprom\n"); | |
128 | printf("--flash-eeprom Flash eeprom\n"); | |
129 | exit (-1); | |
130 | } | |
131 | ||
132 | if (argc == 3) | |
133 | { | |
134 | if (strcmp(argv[1], "--read-eeprom") == 0) | |
135 | _read = 1; | |
136 | if (strcmp(argv[1], "--erase-eeprom") == 0) | |
137 | _erase = 1; | |
138 | if (strcmp(argv[1], "--flash-eeprom") == 0) | |
139 | _flash = 1; | |
140 | ||
141 | argc_filename = 2; | |
142 | } | |
143 | else | |
144 | { | |
145 | argc_filename = 1; | |
146 | } | |
147 | ||
148 | if ((fp = fopen(argv[argc_filename], "r")) == NULL) | |
149 | { | |
150 | printf ("Can't open configuration file\n"); | |
151 | exit (-1); | |
152 | } | |
153 | fclose (fp); | |
154 | ||
155 | cfg = cfg_init(opts, 0); | |
156 | cfg_parse(cfg, argv[argc_filename]); | |
157 | filename = cfg_getstr(cfg, "filename"); | |
158 | ||
159 | if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) | |
160 | printf("Hint: Self powered devices should have a max_power setting of 0.\n"); | |
161 | ||
162 | ftdi_init(&ftdi); | |
fa1def99 TJ |
163 | ftdi_eeprom_initdefaults (&ftdi, "Acme Inc.", "FTDI Chip", NULL); |
164 | eeprom = ftdi.eeprom; | |
165 | ||
166 | eeprom->vendor_id = cfg_getint(cfg, "vendor_id"); | |
167 | eeprom->product_id = cfg_getint(cfg, "product_id"); | |
e47d7975 TJ |
168 | char *type = cfg_getstr(cfg, "chip_type"); |
169 | if (!strcmp(type, "BM")) { | |
fa1def99 | 170 | ftdi.type = TYPE_BM; |
e47d7975 | 171 | } else if (!strcmp(type, "R")) { |
fa1def99 | 172 | ftdi.type = TYPE_R; |
e47d7975 | 173 | } else { |
fa1def99 | 174 | ftdi.type = TYPE_AM; |
e47d7975 TJ |
175 | } |
176 | ||
fa1def99 TJ |
177 | eeprom->self_powered = cfg_getbool(cfg, "self_powered"); |
178 | eeprom->remote_wakeup = cfg_getbool(cfg, "remote_wakeup"); | |
179 | eeprom->max_power = cfg_getint(cfg, "max_power"); | |
e47d7975 | 180 | |
fa1def99 TJ |
181 | eeprom->in_is_isochronous = cfg_getbool(cfg, "in_is_isochronous"); |
182 | eeprom->out_is_isochronous = cfg_getbool(cfg, "out_is_isochronous"); | |
183 | eeprom->suspend_pull_downs = cfg_getbool(cfg, "suspend_pull_downs"); | |
e47d7975 | 184 | |
fa1def99 TJ |
185 | eeprom->use_serial = cfg_getbool(cfg, "use_serial"); |
186 | eeprom->use_usb_version = cfg_getbool(cfg, "change_usb_version"); | |
187 | eeprom->usb_version = cfg_getint(cfg, "usb_version"); | |
e47d7975 TJ |
188 | |
189 | ||
fa1def99 TJ |
190 | eeprom->manufacturer = cfg_getstr(cfg, "manufacturer"); |
191 | eeprom->product = cfg_getstr(cfg, "product"); | |
192 | eeprom->serial = cfg_getstr(cfg, "serial"); | |
193 | eeprom->high_current = cfg_getbool(cfg, "high_current"); | |
194 | eeprom->cbus_function[0] = str_to_cbus(cfg_getstr(cfg, "cbus0"), 13); | |
195 | eeprom->cbus_function[1] = str_to_cbus(cfg_getstr(cfg, "cbus1"), 13); | |
196 | eeprom->cbus_function[2] = str_to_cbus(cfg_getstr(cfg, "cbus2"), 13); | |
197 | eeprom->cbus_function[3] = str_to_cbus(cfg_getstr(cfg, "cbus3"), 13); | |
198 | eeprom->cbus_function[4] = str_to_cbus(cfg_getstr(cfg, "cbus4"), 9); | |
e47d7975 TJ |
199 | int invert = 0; |
200 | if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD; | |
201 | if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD; | |
202 | if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS; | |
203 | if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS; | |
204 | if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR; | |
205 | if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR; | |
206 | if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD; | |
207 | if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI; | |
fa1def99 | 208 | eeprom->invert = invert; |
e47d7975 TJ |
209 | |
210 | if (_read > 0 || _erase > 0 || _flash > 0) | |
211 | { | |
fa1def99 | 212 | i = ftdi_usb_open(&ftdi, eeprom->vendor_id, eeprom->product_id); |
e47d7975 TJ |
213 | |
214 | if (i == 0) | |
215 | { | |
fa1def99 | 216 | printf("EEPROM size: %d\n", eeprom->size); |
e47d7975 TJ |
217 | } |
218 | else | |
219 | { | |
fa1def99 | 220 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom->vendor_id, eeprom->product_id); |
e47d7975 TJ |
221 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi)); |
222 | printf("Retrying with default FTDI id.\n"); | |
223 | ||
224 | i = ftdi_usb_open(&ftdi, 0x0403, 0x6001); | |
225 | if (i != 0) | |
226 | { | |
227 | printf("Error: %s\n", ftdi.error_str); | |
228 | exit (-1); | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
233 | if (_read > 0) | |
234 | { | |
fa1def99 | 235 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi)); |
e47d7975 | 236 | |
fa1def99 | 237 | ftdi_eeprom_decode(&ftdi, 0); |
e47d7975 TJ |
238 | /* Debug output */ |
239 | /* | |
240 | const char* chip_types[] = {"other", "BM", "R"}; | |
fa1def99 TJ |
241 | printf("vendor_id = \"%04x\"\n", eeprom->vendor_id); |
242 | printf("product_id = \"%04x\"\n", eeprom->product_id); | |
e47d7975 | 243 | printf("chip_type = \"%s\"\n", |
fa1def99 TJ |
244 | (eeprom->chip_type > 0x06) || (eeprom->chip_type & 0x01) ? "unknown": |
245 | chip_types[eeprom->chip_type>>1]); | |
246 | printf("self_powered = \"%s\"\n", eeprom->self_powered?"true":"false"); | |
247 | printf("remote_wakeup = \"%s\"\n", eeprom->remote_wakeup?"true":"false"); | |
248 | printf("max_power = \"%d\"\n", eeprom->max_power); | |
249 | printf("in_is_isochronous = \"%s\"\n", eeprom->in_is_isochronous?"true":"false"); | |
250 | printf("out_is_isochronous = \"%s\"\n", eeprom->out_is_isochronous?"true":"false"); | |
251 | printf("suspend_pull_downs = \"%s\"\n", eeprom->suspend_pull_downs?"true":"false"); | |
252 | printf("use_serial = \"%s\"\n", eeprom->use_serial?"true":"false"); | |
253 | printf("change_usb_version = \"%s\"\n", eeprom->change_usb_version?"true":"false"); | |
254 | printf("usb_version = \"%d\"\n", eeprom->usb_version); | |
255 | printf("manufacturer = \"%s\"\n", eeprom->manufacturer); | |
256 | printf("product = \"%s\"\n", eeprom->product); | |
257 | printf("serial = \"%s\"\n", eeprom->serial); | |
e47d7975 TJ |
258 | */ |
259 | ||
260 | if (filename != NULL && strlen(filename) > 0) | |
261 | { | |
262 | FILE *fp = fopen (filename, "wb"); | |
263 | fwrite (eeprom_buf, 1, 128, fp); | |
264 | fclose (fp); | |
265 | } | |
266 | else | |
267 | { | |
268 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
269 | } | |
270 | ||
271 | goto cleanup; | |
272 | } | |
273 | ||
274 | if (_erase > 0) | |
275 | { | |
276 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi)); | |
277 | } | |
278 | ||
fa1def99 | 279 | size_check = ftdi_eeprom_build(&ftdi); |
e47d7975 TJ |
280 | |
281 | if (size_check == -1) | |
282 | { | |
283 | printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n"); | |
284 | printf ("You need to short your string by: %d bytes\n", size_check); | |
285 | goto cleanup; | |
286 | } else if (size_check < 0) { | |
287 | printf ("ftdi_eeprom_build(): error: %d\n", size_check); | |
288 | } | |
289 | else | |
290 | { | |
291 | printf ("Used eeprom space: %d bytes\n", 128-size_check); | |
292 | } | |
293 | ||
294 | if (_flash > 0) | |
295 | { | |
296 | if (cfg_getbool(cfg, "flash_raw")) | |
297 | { | |
298 | if (filename != NULL && strlen(filename) > 0) | |
299 | { | |
300 | FILE *fp = fopen(filename, "rb"); | |
301 | fread(eeprom_buf, 1, 128, fp); | |
302 | fclose(fp); | |
303 | } | |
304 | } | |
fa1def99 | 305 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi)); |
e47d7975 TJ |
306 | } |
307 | ||
308 | // Write to file? | |
309 | if (filename != NULL && strlen(filename) > 0) | |
310 | { | |
311 | fp = fopen(filename, "w"); | |
312 | if (fp == NULL) | |
313 | { | |
314 | printf ("Can't write eeprom file.\n"); | |
315 | exit (-1); | |
316 | } | |
317 | else | |
318 | printf ("Writing to file: %s\n", filename); | |
319 | ||
320 | fwrite(eeprom_buf, 128, 1, fp); | |
321 | fclose(fp); | |
322 | } | |
323 | ||
324 | cleanup: | |
325 | if (_read > 0 || _erase > 0 || _flash > 0) | |
326 | { | |
327 | printf("FTDI close: %d\n", ftdi_usb_close(&ftdi)); | |
328 | } | |
329 | ||
330 | ftdi_deinit (&ftdi); | |
331 | ||
332 | cfg_free(cfg); | |
333 | ||
334 | printf("\n"); | |
335 | return 0; | |
336 | } |