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