Commit | Line | Data |
---|---|---|
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 | ||
28 | int 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), | |
b53be740 | 49 | CFG_BOOL("flash_raw", cfg_false, 0), |
50850385 TJ |
50 | CFG_END() |
51 | }; | |
52 | cfg_t *cfg; | |
53 | ||
54 | /* | |
55 | normal variables | |
56 | */ | |
57 | int _read = 0, _erase = 0, _flash = 0; | |
58 | unsigned char eeprom_buf[128]; | |
59 | char *filename; | |
60 | int size_check; | |
61 | int i, argc_filename; | |
62 | FILE *fp; | |
63 | ||
64 | struct ftdi_context ftdi; | |
65 | struct ftdi_eeprom eeprom; | |
66 | ||
67 | printf("\nFTDI eeprom generator v%s\n", VERSION); | |
c0c68460 | 68 | printf ("(c) Intra2net AG <opensource@intra2net.com>\n"); |
50850385 TJ |
69 | |
70 | if (argc != 2 && argc != 3) { | |
71 | printf("Syntax: %s [commands] config-file\n", argv[0]); | |
72 | printf("Valid commands:\n"); | |
73 | printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); | |
74 | printf("--erase-eeprom Erase eeprom\n"); | |
75 | printf("--flash-eeprom Flash eeprom\n"); | |
76 | exit (-1); | |
77 | } | |
78 | ||
79 | if (argc == 3) { | |
80 | if (strcmp(argv[1], "--read-eeprom") == 0) | |
81 | _read = 1; | |
82 | if (strcmp(argv[1], "--erase-eeprom") == 0) | |
83 | _erase = 1; | |
84 | if (strcmp(argv[1], "--flash-eeprom") == 0) | |
85 | _flash = 1; | |
86 | ||
87 | argc_filename = 2; | |
88 | } else { | |
89 | argc_filename = 1; | |
90 | } | |
91 | ||
92 | if ((fp = fopen(argv[argc_filename], "r")) == NULL) { | |
93 | printf ("Can't open configuration file\n"); | |
94 | exit (-1); | |
95 | } | |
96 | fclose (fp); | |
97 | ||
98 | cfg = cfg_init(opts, 0); | |
99 | cfg_parse(cfg, argv[argc_filename]); | |
100 | filename = cfg_getstr(cfg, "filename"); | |
101 | ||
102 | if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) | |
103 | printf("Hint: Self powered devices should have a max_power setting of 0.\n"); | |
104 | ||
a3d000be | 105 | ftdi_init(&ftdi); |
50850385 | 106 | ftdi_eeprom_initdefaults (&eeprom); |
50850385 TJ |
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 | ||
b53be740 | 128 | |
50850385 | 129 | if (_read > 0 || _erase > 0 || _flash > 0) { |
50850385 TJ |
130 | i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id); |
131 | ||
b53be740 MK |
132 | if (i == 0) { |
133 | printf("EEPROM size: %d\n", ftdi.eeprom_size); | |
134 | } | |
135 | else { | |
50850385 | 136 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id); |
b53be740 | 137 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi)); |
50850385 TJ |
138 | printf("Retrying with default FTDI id.\n"); |
139 | ||
140 | i = ftdi_usb_open(&ftdi, 0x0403, 0x6001); | |
141 | if (i != 0) { | |
3d1f902c | 142 | printf("Error: %s\n", ftdi.error_str); |
50850385 TJ |
143 | exit (-1); |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | if (_read > 0) { | |
b53be740 MK |
149 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char *)eeprom_buf)); |
150 | ||
13a25f0f MK |
151 | ftdi_eeprom_decode(&eeprom, eeprom_buf); |
152 | printf("vendor_id = \"%04x\"\n", eeprom.vendor_id); | |
153 | printf("product_id = \"%04x\"\n", eeprom.product_id); | |
154 | printf("BM_type_chip = \"%s\"\n", eeprom.BM_type_chip?"true":"false"); | |
155 | printf("self_powered = \"%s\"\n", eeprom.self_powered?"true":"false"); | |
156 | printf("remote_wakeup = \"%s\"\n", eeprom.remote_wakeup?"true":"false"); | |
157 | printf("max_power = \"%d\"\n", eeprom.max_power); | |
158 | printf("in_is_isochronous = \"%s\"\n", eeprom.in_is_isochronous?"true":"false"); | |
159 | printf("out_is_isochronous = \"%s\"\n", eeprom.out_is_isochronous?"true":"false"); | |
160 | printf("suspend_pull_downs = \"%s\"\n", eeprom.suspend_pull_downs?"true":"false"); | |
161 | printf("use_serial = \"%s\"\n", eeprom.use_serial?"true":"false"); | |
162 | printf("change_usb_version = \"%s\"\n", eeprom.change_usb_version?"true":"false"); | |
163 | printf("usb_version = \"%d\"\n", eeprom.usb_version); | |
164 | printf("manufacturer = \"%s\"\n", eeprom.manufacturer); | |
165 | printf("product = \"%s\"\n", eeprom.product); | |
166 | printf("serial = \"%s\"\n", eeprom.serial); | |
b53be740 | 167 | |
50850385 TJ |
168 | if (filename != NULL && strlen(filename) > 0) { |
169 | FILE *fp = fopen (filename, "wb"); | |
b53be740 | 170 | fwrite (eeprom_buf, 1, 128, fp); |
50850385 TJ |
171 | fclose (fp); |
172 | } else { | |
173 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
174 | } | |
175 | ||
176 | goto cleanup; | |
177 | } | |
178 | ||
179 | if (_erase > 0) { | |
180 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi)); | |
181 | } | |
182 | ||
183 | size_check = ftdi_eeprom_build(&eeprom, eeprom_buf); | |
184 | if (size_check == -1) { | |
185 | printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n"); | |
186 | printf ("You need to short your string by: %d bytes\n", size_check); | |
187 | goto cleanup; | |
188 | } else { | |
189 | printf ("Used eeprom space: %d bytes\n", 128-size_check); | |
190 | } | |
191 | ||
192 | if (_flash > 0) { | |
b53be740 MK |
193 | if (cfg_getbool(cfg, "flash_raw")) { |
194 | if (filename != NULL && strlen(filename) > 0) { | |
195 | FILE *fp = fopen(filename, "rb"); | |
196 | fread(eeprom_buf, 1, 128, fp); | |
197 | fclose(fp); | |
198 | } | |
199 | } | |
200 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char *)eeprom_buf)); | |
50850385 TJ |
201 | } |
202 | ||
203 | // Write to file? | |
204 | if (filename != NULL && strlen(filename) > 0) { | |
205 | fp = fopen(filename, "w"); | |
206 | if (fp == NULL) { | |
207 | printf ("Can't write eeprom file.\n"); | |
208 | exit (-1); | |
209 | } else | |
210 | printf ("Writing to file: %s\n", filename); | |
211 | ||
212 | fwrite(eeprom_buf, 128, 1, fp); | |
213 | fclose(fp); | |
214 | } | |
215 | ||
216 | cleanup: | |
217 | if (_read > 0 || _erase > 0 || _flash > 0) { | |
218 | printf("FTDI close: %d\n", ftdi_usb_close(&ftdi)); | |
219 | } | |
e61fbf76 TJ |
220 | |
221 | ftdi_deinit (&ftdi); | |
50850385 TJ |
222 | |
223 | cfg_free(cfg); | |
224 | ||
225 | printf("\n"); | |
226 | return 0; | |
227 | } |