Commit | Line | Data |
---|---|---|
50850385 TJ |
1 | /*************************************************************************** |
2 | main.c - description | |
3 | ------------------- | |
4 | begin : Mon Apr 7 12:05:22 CEST 2003 | |
5 | copyright : (C) 2003 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 * | |
12 | * it under the terms of the GNU General Public License as published by * | |
13 | * the Free Software Foundation; either version 2 of the License, or * | |
14 | * (at your option) any later version. * | |
15 | * * | |
16 | ***************************************************************************/ | |
17 | ||
18 | #ifdef HAVE_CONFIG_H | |
19 | #include <config.h> | |
20 | #endif | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | ||
26 | #include <confuse.h> | |
27 | #include <ftdi.h> | |
28 | ||
29 | int main(int argc, char *argv[]) { | |
30 | /* | |
31 | configuration options | |
32 | */ | |
33 | cfg_opt_t opts[] = { | |
34 | CFG_INT("vendor_id", 0, 0), | |
35 | CFG_INT("product_id", 0, 0), | |
36 | CFG_BOOL("self_powered", cfg_true, 0), | |
37 | CFG_BOOL("remote_wakeup", cfg_true, 0), | |
38 | CFG_BOOL("BM_type_chip", cfg_true, 0), | |
39 | CFG_BOOL("in_is_isochronous", cfg_false, 0), | |
40 | CFG_BOOL("out_is_isochronous", cfg_false, 0), | |
41 | CFG_BOOL("suspend_pull_downs", cfg_false, 0), | |
42 | CFG_BOOL("use_serial", cfg_false, 0), | |
43 | CFG_BOOL("change_usb_version", cfg_false, 0), | |
44 | CFG_INT("usb_version", 0, 0), | |
45 | CFG_INT("max_power", 0, 0), | |
46 | CFG_STR("manufacturer", "Acme Inc.", 0), | |
47 | CFG_STR("product", "USB Serial Converter", 0), | |
48 | CFG_STR("serial", "08-15", 0), | |
49 | CFG_STR("filename", "", 0), | |
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 | ||
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) { | |
129 | printf("FTDI init: %d\n", ftdi_init(&ftdi)); | |
130 | i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id); | |
131 | ||
132 | if (i != 0) { | |
133 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id); | |
134 | printf("Retrying with default FTDI id.\n"); | |
135 | ||
136 | i = ftdi_usb_open(&ftdi, 0x0403, 0x6001); | |
137 | if (i != 0) { | |
138 | printf("Sorry, unable to find FTDI USB chip\n"); | |
139 | exit (-1); | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | if (_read > 0) { | |
145 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char *)&eeprom_buf)); | |
146 | if (filename != NULL && strlen(filename) > 0) { | |
147 | FILE *fp = fopen (filename, "wb"); | |
148 | fwrite (&eeprom_buf, 1, 128, fp); | |
149 | fclose (fp); | |
150 | } else { | |
151 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
152 | } | |
153 | ||
154 | goto cleanup; | |
155 | } | |
156 | ||
157 | if (_erase > 0) { | |
158 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi)); | |
159 | } | |
160 | ||
161 | size_check = ftdi_eeprom_build(&eeprom, eeprom_buf); | |
162 | if (size_check == -1) { | |
163 | printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n"); | |
164 | printf ("You need to short your string by: %d bytes\n", size_check); | |
165 | goto cleanup; | |
166 | } else { | |
167 | printf ("Used eeprom space: %d bytes\n", 128-size_check); | |
168 | } | |
169 | ||
170 | if (_flash > 0) { | |
171 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char *)&eeprom_buf)); | |
172 | } | |
173 | ||
174 | // Write to file? | |
175 | if (filename != NULL && strlen(filename) > 0) { | |
176 | fp = fopen(filename, "w"); | |
177 | if (fp == NULL) { | |
178 | printf ("Can't write eeprom file.\n"); | |
179 | exit (-1); | |
180 | } else | |
181 | printf ("Writing to file: %s\n", filename); | |
182 | ||
183 | fwrite(eeprom_buf, 128, 1, fp); | |
184 | fclose(fp); | |
185 | } | |
186 | ||
187 | cleanup: | |
188 | if (_read > 0 || _erase > 0 || _flash > 0) { | |
189 | printf("FTDI close: %d\n", ftdi_usb_close(&ftdi)); | |
190 | } | |
e61fbf76 TJ |
191 | |
192 | ftdi_deinit (&ftdi); | |
50850385 TJ |
193 | |
194 | cfg_free(cfg); | |
195 | ||
196 | printf("\n"); | |
197 | return 0; | |
198 | } |