ftdi_eeprom: reset the device after flashing the eeprom
[libftdi] / ftdi_eeprom / main.c
1 /***************************************************************************
2                              main.c  -  description
3                            -------------------
4     begin                : Mon Apr  7 12:05:22 CEST 2003
5     copyright            : (C) 2003-2011 by Intra2net AG and the libftdi developers
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
17 /*
18  TODO:
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
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>
40 #include <libusb.h>
41 #include <ftdi.h>
42 #include <ftdi_eeprom_version.h>
43
44 static int str_to_cbus(char *str, int max_allowed)
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
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  **/
72 static 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  **/
90 static 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
99 int 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_BOOL("in_is_isochronous", cfg_false, 0),
111         CFG_BOOL("out_is_isochronous", cfg_false, 0),
112         CFG_BOOL("suspend_pull_downs", cfg_false, 0),
113         CFG_BOOL("use_serial", cfg_false, 0),
114         CFG_BOOL("change_usb_version", cfg_false, 0),
115         CFG_INT("usb_version", 0, 0),
116         CFG_INT("default_pid", 0x6001, 0),
117         CFG_INT("max_power", 0, 0),
118         CFG_STR("manufacturer", "Acme Inc.", 0),
119         CFG_STR("product", "USB Serial Converter", 0),
120         CFG_STR("serial", "08-15", 0),
121         CFG_INT("eeprom_type", 0x00, 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;
146
147     const int max_eeprom_size = 256;
148     int my_eeprom_size = 0;
149     unsigned char *eeprom_buf = NULL;
150     char *filename;
151     int size_check;
152     int i, argc_filename;
153     FILE *fp;
154
155     struct ftdi_context *ftdi = NULL;
156
157     printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
158     printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n");
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         else if (strcmp(argv[1], "--erase-eeprom") == 0)
175             _erase = 1;
176         else if (strcmp(argv[1], "--flash-eeprom") == 0)
177             _flash = 1;
178         else
179         {
180             printf ("Can't open configuration file\n");
181             exit (-1);
182         }
183         argc_filename = 2;
184     }
185     else
186     {
187         argc_filename = 1;
188     }
189
190     if ((fp = fopen(argv[argc_filename], "r")) == NULL)
191     {
192         printf ("Can't open configuration file\n");
193         exit (-1);
194     }
195     fclose (fp);
196
197     cfg = cfg_init(opts, 0);
198     cfg_parse(cfg, argv[argc_filename]);
199     filename = cfg_getstr(cfg, "filename");
200
201     if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
202         printf("Hint: Self powered devices should have a max_power setting of 0.\n");
203
204     if ((ftdi = ftdi_new()) == 0)
205     {
206         fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
207                 ftdi_get_error_string(ftdi));
208         return EXIT_FAILURE;
209     }
210
211     if (_read > 0 || _erase > 0 || _flash > 0)
212     {
213         int vendor_id = cfg_getint(cfg, "vendor_id");
214         int product_id = cfg_getint(cfg, "product_id");
215
216         i = ftdi_usb_open(ftdi, vendor_id, product_id);
217
218         if (i != 0)
219         {
220             int default_pid = cfg_getint(cfg, "default_pid");
221             printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
222             printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
223             printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
224
225             i = ftdi_usb_open(ftdi, 0x0403, default_pid);
226             if (i != 0)
227             {
228                 printf("Error: %s\n", ftdi->error_str);
229                 exit (-1);
230             }
231         }
232     }
233     ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), 
234                               cfg_getstr(cfg, "product"), 
235                               cfg_getstr(cfg, "serial"));
236
237     printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
238     eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
239     printf("EEPROM size: %d\n", my_eeprom_size);
240
241     if (_read > 0)
242     {
243         ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
244
245         eeprom_buf = malloc(my_eeprom_size);
246         ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
247
248         if (eeprom_buf == NULL)
249         {
250             fprintf(stderr, "Malloc failed, aborting\n");
251             goto cleanup;
252         }
253         if (filename != NULL && strlen(filename) > 0)
254         {
255
256             FILE *fp = fopen (filename, "wb");
257             fwrite (eeprom_buf, 1, my_eeprom_size, fp);
258             fclose (fp);
259         }
260         else
261         {
262             printf("Warning: Not writing eeprom, you must supply a valid filename\n");
263         }
264
265         goto cleanup;
266     }
267
268     eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
269     eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
270
271     eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
272     eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
273     eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
274
275     eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
276     eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
277     eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));
278
279     eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
280     eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
281     eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
282     eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));
283
284     eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
285     eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13));
286     eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13));
287     eeprom_set_value(ftdi, CBUS_FUNCTION_2, str_to_cbus(cfg_getstr(cfg, "cbus2"), 13));
288     eeprom_set_value(ftdi, CBUS_FUNCTION_3, str_to_cbus(cfg_getstr(cfg, "cbus3"), 13));
289     eeprom_set_value(ftdi, CBUS_FUNCTION_4, str_to_cbus(cfg_getstr(cfg, "cbus4"), 9));
290     int invert = 0;
291     if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
292     if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
293     if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
294     if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
295     if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
296     if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
297     if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
298     if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
299     eeprom_set_value(ftdi, INVERT, invert);
300
301     eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
302     eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
303     eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP);
304     eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP);
305     eeprom_set_value(ftdi, CHANNEL_A_RS485, 0);
306     eeprom_set_value(ftdi, CHANNEL_B_RS485, 0);
307     eeprom_set_value(ftdi, CHANNEL_C_RS485, 0);
308     eeprom_set_value(ftdi, CHANNEL_D_RS485, 0);
309
310     if (_erase > 0)
311     {
312         printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
313     }
314
315     size_check = ftdi_eeprom_build(ftdi);
316     eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
317
318     if (size_check == -1)
319     {
320         printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
321         printf ("You need to short your string by: %d bytes\n", size_check);
322         goto cleanup;
323     } else if (size_check < 0) {
324         printf ("ftdi_eeprom_build(): error: %d\n", size_check);
325     }
326     else
327     {
328         printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
329     }
330
331     if (_flash > 0)
332     {
333         if (cfg_getbool(cfg, "flash_raw"))
334         {
335             if (filename != NULL && strlen(filename) > 0)
336             {
337                 eeprom_buf = malloc(max_eeprom_size);
338                 FILE *fp = fopen(filename, "rb");
339                 if (fp == NULL)
340                 {
341                     printf ("Can't open eeprom file %s.\n", filename);
342                     exit (-1);
343                 }
344                 my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
345                 fclose(fp);
346                 if (my_eeprom_size < 128)
347                 {
348                     printf ("Can't read eeprom file %s.\n", filename);
349                     exit (-1);
350                 }
351
352                 ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
353             }
354         }
355         printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
356         libusb_reset_device(ftdi->usb_dev);
357     }
358
359     // Write to file?
360     if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
361     {
362         fp = fopen(filename, "w");
363         if (fp == NULL)
364         {
365             printf ("Can't write eeprom file.\n");
366             exit (-1);
367         }
368         else
369             printf ("Writing to file: %s\n", filename);
370
371         if (eeprom_buf == NULL)
372             eeprom_buf = malloc(my_eeprom_size);
373         ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
374
375         fwrite(eeprom_buf, my_eeprom_size, 1, fp);
376         fclose(fp);
377     }
378
379 cleanup:
380     if (eeprom_buf)
381         free(eeprom_buf);
382     if (_read > 0 || _erase > 0 || _flash > 0)
383     {
384         printf("FTDI close: %d\n", ftdi_usb_close(ftdi));
385     }
386
387     ftdi_deinit (ftdi);
388     ftdi_free (ftdi);
389
390     cfg_free(cfg);
391
392     printf("\n");
393     return 0;
394 }