Increase version
[ftdi_eeprom] / src / main.c
CommitLineData
50850385
TJ
1/***************************************************************************
2 main.c - description
3 -------------------
4 begin : Mon Apr 7 12:05:22 CEST 2003
f2c67a7e 5 copyright : (C) 2003,2008 by Intra2net AG
c0c68460 6 email : opensource@intra2net.com
50850385
TJ
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
373c31e6
GE
12 * it under the terms of the GNU General Public License version 2 as *
13 * published by the Free Software Foundation. *
50850385
TJ
14 * *
15 ***************************************************************************/
16
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24
25#include <confuse.h>
26#include <ftdi.h>
27
28int main(int argc, char *argv[]) {
29 /*
30 configuration options
31 */
32 cfg_opt_t opts[] = {
33 CFG_INT("vendor_id", 0, 0),
34 CFG_INT("product_id", 0, 0),
35 CFG_BOOL("self_powered", cfg_true, 0),
36 CFG_BOOL("remote_wakeup", cfg_true, 0),
37 CFG_BOOL("BM_type_chip", cfg_true, 0),
38 CFG_BOOL("in_is_isochronous", cfg_false, 0),
39 CFG_BOOL("out_is_isochronous", cfg_false, 0),
40 CFG_BOOL("suspend_pull_downs", cfg_false, 0),
41 CFG_BOOL("use_serial", cfg_false, 0),
42 CFG_BOOL("change_usb_version", cfg_false, 0),
43 CFG_INT("usb_version", 0, 0),
44 CFG_INT("max_power", 0, 0),
45 CFG_STR("manufacturer", "Acme Inc.", 0),
46 CFG_STR("product", "USB Serial Converter", 0),
47 CFG_STR("serial", "08-15", 0),
48 CFG_STR("filename", "", 0),
49 CFG_END()
50 };
51 cfg_t *cfg;
52
53 /*
54 normal variables
55 */
56 int _read = 0, _erase = 0, _flash = 0;
57 unsigned char eeprom_buf[128];
58 char *filename;
59 int size_check;
60 int i, argc_filename;
61 FILE *fp;
62
63 struct ftdi_context ftdi;
64 struct ftdi_eeprom eeprom;
65
66 printf("\nFTDI eeprom generator v%s\n", VERSION);
c0c68460 67 printf ("(c) Intra2net AG <opensource@intra2net.com>\n");
50850385
TJ
68
69 if (argc != 2 && argc != 3) {
70 printf("Syntax: %s [commands] config-file\n", argv[0]);
71 printf("Valid commands:\n");
72 printf("--read-eeprom Read eeprom and write to -filename- from config-file\n");
73 printf("--erase-eeprom Erase eeprom\n");
74 printf("--flash-eeprom Flash eeprom\n");
75 exit (-1);
76 }
77
78 if (argc == 3) {
79 if (strcmp(argv[1], "--read-eeprom") == 0)
80 _read = 1;
81 if (strcmp(argv[1], "--erase-eeprom") == 0)
82 _erase = 1;
83 if (strcmp(argv[1], "--flash-eeprom") == 0)
84 _flash = 1;
85
86 argc_filename = 2;
87 } else {
88 argc_filename = 1;
89 }
90
91 if ((fp = fopen(argv[argc_filename], "r")) == NULL) {
92 printf ("Can't open configuration file\n");
93 exit (-1);
94 }
95 fclose (fp);
96
97 cfg = cfg_init(opts, 0);
98 cfg_parse(cfg, argv[argc_filename]);
99 filename = cfg_getstr(cfg, "filename");
100
101 if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
102 printf("Hint: Self powered devices should have a max_power setting of 0.\n");
103
a3d000be 104 ftdi_init(&ftdi);
50850385
TJ
105 ftdi_eeprom_initdefaults (&eeprom);
106
107 eeprom.vendor_id = cfg_getint(cfg, "vendor_id");
108 eeprom.product_id = cfg_getint(cfg, "product_id");
109 eeprom.BM_type_chip = cfg_getbool(cfg, "BM_type_chip");
110
111 eeprom.self_powered = cfg_getbool(cfg, "self_powered");
112 eeprom.remote_wakeup = cfg_getbool(cfg, "remote_wakeup");
113 eeprom.max_power = cfg_getint(cfg, "max_power");
114
115 eeprom.in_is_isochronous = cfg_getbool(cfg, "in_is_isochronous");
116 eeprom.out_is_isochronous = cfg_getbool(cfg, "out_is_isochronous");
117 eeprom.suspend_pull_downs = cfg_getbool(cfg, "suspend_pull_downs");
118
119 eeprom.use_serial = cfg_getbool(cfg, "use_serial");
120 eeprom.change_usb_version = cfg_getbool(cfg, "change_usb_version");
121 eeprom.usb_version = cfg_getint(cfg, "usb_version");
122
123
124 eeprom.manufacturer = cfg_getstr(cfg, "manufacturer");
125 eeprom.product = cfg_getstr(cfg, "product");
126 eeprom.serial = cfg_getstr(cfg, "serial");
127
128 if (_read > 0 || _erase > 0 || _flash > 0) {
50850385
TJ
129 i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id);
130
131 if (i != 0) {
132 printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id);
133 printf("Retrying with default FTDI id.\n");
134
135 i = ftdi_usb_open(&ftdi, 0x0403, 0x6001);
136 if (i != 0) {
3d1f902c 137 printf("Error: %s\n", ftdi.error_str);
50850385
TJ
138 exit (-1);
139 }
140 }
141 }
142
143 if (_read > 0) {
144 printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char *)&eeprom_buf));
145 if (filename != NULL && strlen(filename) > 0) {
146 FILE *fp = fopen (filename, "wb");
147 fwrite (&eeprom_buf, 1, 128, fp);
148 fclose (fp);
149 } else {
150 printf("Warning: Not writing eeprom, you must supply a valid filename\n");
151 }
152
153 goto cleanup;
154 }
155
156 if (_erase > 0) {
157 printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi));
158 }
159
160 size_check = ftdi_eeprom_build(&eeprom, eeprom_buf);
161 if (size_check == -1) {
162 printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
163 printf ("You need to short your string by: %d bytes\n", size_check);
164 goto cleanup;
165 } else {
166 printf ("Used eeprom space: %d bytes\n", 128-size_check);
167 }
168
169 if (_flash > 0) {
170 printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char *)&eeprom_buf));
171 }
172
173 // Write to file?
174 if (filename != NULL && strlen(filename) > 0) {
175 fp = fopen(filename, "w");
176 if (fp == NULL) {
177 printf ("Can't write eeprom file.\n");
178 exit (-1);
179 } else
180 printf ("Writing to file: %s\n", filename);
181
182 fwrite(eeprom_buf, 128, 1, fp);
183 fclose(fp);
184 }
185
186cleanup:
187 if (_read > 0 || _erase > 0 || _flash > 0) {
188 printf("FTDI close: %d\n", ftdi_usb_close(&ftdi));
189 }
e61fbf76
TJ
190
191 ftdi_deinit (&ftdi);
50850385
TJ
192
193 cfg_free(cfg);
194
195 printf("\n");
196 return 0;
197}