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