Const correctness for char* strings
[libftdi] / examples / eeprom.c
CommitLineData
2db3a766
UB
1/* LIBFTDI EEPROM access example
2
3 This program is distributed under the GPL, version 2
4*/
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <ctype.h>
10#include <unistd.h>
11#include <getopt.h>
12#include <ftdi.h>
13
8ee8775e
UB
14int read_decode_eeprom(struct ftdi_context *ftdi)
15{
16 int i, j, f;
17 int value;
18 int size;
19 unsigned char buf[256];
20
21 f = ftdi_read_eeprom(ftdi);
22 if (f < 0)
23 {
24 fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n",
25 f, ftdi_get_error_string(ftdi));
26 return -1;
27 }
28
29
30 ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value);
31 if (value <0)
32 {
2e4e22c4
UB
33 fprintf(stderr, "No EEPROM found or EEPROM empty\n");
34 fprintf(stderr, "On empty EEPROM, use -w option to write default values\n");
8ee8775e
UB
35 return -1;
36 }
37 fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value);
38 if (ftdi->type == TYPE_R)
39 size = 0xa0;
40 else
41 size = value;
42 ftdi_get_eeprom_buf(ftdi, buf, size);
43 for (i=0; i < size; i += 16)
44 {
45 fprintf(stdout,"0x%03x:", i);
46
47 for (j = 0; j< 8; j++)
48 fprintf(stdout," %02x", buf[i+j]);
49 fprintf(stdout," ");
50 for (; j< 16; j++)
51 fprintf(stdout," %02x", buf[i+j]);
52 fprintf(stdout," ");
53 for (j = 0; j< 8; j++)
54 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
55 fprintf(stdout," ");
56 for (; j< 16; j++)
57 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
58 fprintf(stdout,"\n");
59 }
60
61 f = ftdi_eeprom_decode(ftdi, 1);
62 if (f < 0)
63 {
64 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
65 f, ftdi_get_error_string(ftdi));
66 return -1;
67 }
68 return 0;
69}
70
2db3a766
UB
71int main(int argc, char **argv)
72{
f38b0866 73 struct ftdi_context *ftdi;
8ee8775e
UB
74 int f, i;
75 int vid = 0;
76 int pid = 0;
2db3a766
UB
77 char const *desc = 0;
78 char const *serial = 0;
79 int erase = 0;
4fb2ebbb 80 int use_defaults = 0;
2db3a766 81 int large_chip = 0;
0091182e 82 int do_write = 0;
bd4a9e38 83 int retval = 0;
8ee8775e 84 int value;
2db3a766 85
f38b0866
UB
86 if ((ftdi = ftdi_new()) == 0)
87 {
05c2e40a
TJ
88 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
89 ftdi_get_error_string(ftdi));
f38b0866
UB
90 return EXIT_FAILURE;
91 }
92
0091182e 93 while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
2db3a766
UB
94 {
95 switch (i)
96 {
05c2e40a
TJ
97 case 'd':
98 use_defaults = 1;
99 if (optarg)
100 large_chip = 0x66;
101 break;
102 case 'e':
103 erase = 1;
104 break;
105 case 'v':
106 vid = strtoul(optarg, NULL, 0);
107 break;
108 case 'p':
109 pid = strtoul(optarg, NULL, 0);
110 break;
111 case 'P':
2db3a766 112 desc = optarg;
05c2e40a
TJ
113 break;
114 case 'S':
115 serial = optarg;
116 break;
117 case 'w':
118 do_write = 1;
119 break;
120 default:
121 fprintf(stderr, "usage: %s [options]\n", *argv);
122 fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte "
82e0fc5a 123 "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
05c2e40a
TJ
124 fprintf(stderr, "\t-w write\n");
125 fprintf(stderr, "\t-e erase\n");
126 fprintf(stderr, "\t-v verbose decoding\n");
127 fprintf(stderr, "\t-p <number> Search for device with PID == number\n");
128 fprintf(stderr, "\t-v <number> Search for device with VID == number\n");
129 fprintf(stderr, "\t-P <string? Search for device with given "
2db3a766 130 "product description\n");
05c2e40a 131 fprintf(stderr, "\t-S <string? Search for device with given "
2db3a766 132 "serial number\n");
bd4a9e38
UB
133 retval = -1;
134 goto done;
2db3a766
UB
135 }
136 }
137
2db3a766 138 // Select first interface
f38b0866 139 ftdi_set_interface(ftdi, INTERFACE_ANY);
2db3a766 140
8ee8775e 141 if (!vid && !pid && desc == NULL && serial == NULL)
2db3a766 142 {
8ee8775e
UB
143 struct ftdi_device_list *devlist, *curdev;
144 int res;
145 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
146 {
147 fprintf(stderr, "No FTDI with default VID/PID found\n");
148 retval = EXIT_FAILURE;
149 goto do_deinit;
150 }
151 if (res > 1)
152 {
153 int i = 1;
154 fprintf(stderr, "%d FTDI devices found: Only Readout on EEPROM done. ",res);
155 fprintf(stderr, "Use VID/PID/desc/serial to select device\n");
156 for (curdev = devlist; curdev != NULL; curdev= curdev->next, i++)
157 {
158 f = ftdi_usb_open_dev(ftdi, curdev->dev);
159 if (f<0)
160 {
161 fprintf(stderr, "Unable to open device %d: (%s)",
162 i, ftdi_get_error_string(ftdi));
163 continue;
164 }
165 fprintf(stderr, "Decoded values of device %d:\n", i);
166 read_decode_eeprom(ftdi);
167 ftdi_usb_close(ftdi);
168 }
169 ftdi_list_free(&devlist);
170 retval = EXIT_SUCCESS;
171 goto do_deinit;
172 }
95f29fc4
UB
173 else if (res == 1)
174 {
175 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
176 if (f<0)
177 {
178 fprintf(stderr, "Unable to open device %d: (%s)",
179 i, ftdi_get_error_string(ftdi));
180 }
181 }
182 else
183 {
184 fprintf(stderr, "No devices found\n");
185 f = 0;
186 }
8ee8775e
UB
187 ftdi_list_free(&devlist);
188 }
189 else
190 {
191 // Open device
192 f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
193 if (f < 0)
194 {
195 fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
196 if (desc)
197 fprintf(stderr, " Desc %s", desc);
198 if (serial)
199 fprintf(stderr, " Serial %s", serial);
200 fprintf(stderr, "\n");
201 fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
202 f, ftdi_get_error_string(ftdi));
203
204 retval = -1;
205 goto done;
206 }
2db3a766 207 }
2db3a766
UB
208 if (erase)
209 {
82e0fc5a 210 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
2db3a766
UB
211 if (f < 0)
212 {
05c2e40a 213 fprintf(stderr, "Erase failed: %s",
f38b0866 214 ftdi_get_error_string(ftdi));
bd4a9e38
UB
215 retval = -2;
216 goto done;
2db3a766 217 }
05c2e40a
TJ
218 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
219 {
220 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 221 f, ftdi_get_error_string(ftdi));
05c2e40a 222 }
82e0fc5a 223 if (value == -1)
2db3a766 224 fprintf(stderr, "No EEPROM\n");
82e0fc5a 225 else if (value == 0)
2db3a766
UB
226 fprintf(stderr, "Internal EEPROM\n");
227 else
82e0fc5a 228 fprintf(stderr, "Found 93x%02x\n", value);
bd4a9e38
UB
229 retval = 0;
230 goto done;
05c2e40a 231 }
2db3a766 232
05c2e40a 233 if (use_defaults)
2db3a766 234 {
bd4a9e38 235 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
05c2e40a
TJ
236 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
237 {
238 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 239 f, ftdi_get_error_string(ftdi));
05c2e40a
TJ
240 }
241 if (large_chip)
242 if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0)
243 {
244 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
245 f, ftdi_get_error_string(ftdi));
246 }
a35aa9bd 247 f=(ftdi_eeprom_build(ftdi));
2db3a766
UB
248 if (f < 0)
249 {
05c2e40a 250 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
f38b0866 251 f, ftdi_get_error_string(ftdi));
bd4a9e38
UB
252 retval = -1;
253 goto done;
2db3a766
UB
254 }
255 }
05c2e40a 256 else if (do_write)
0091182e 257 {
bd4a9e38 258 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
05c2e40a
TJ
259 f = ftdi_erase_eeprom(ftdi);
260 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
261 {
262 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 263 f, ftdi_get_error_string(ftdi));
05c2e40a 264 }
82e0fc5a 265 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
05c2e40a
TJ
266 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
267 {
268 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 269 f, ftdi_get_error_string(ftdi));
05c2e40a 270 }
82e0fc5a 271 if (value == -1)
0091182e 272 fprintf(stderr, "No EEPROM\n");
82e0fc5a 273 else if (value == 0)
0091182e
UB
274 fprintf(stderr, "Internal EEPROM\n");
275 else
82e0fc5a 276 fprintf(stderr, "Found 93x%02x\n", value);
0091182e
UB
277 f=(ftdi_eeprom_build(ftdi));
278 if (f < 0)
279 {
05c2e40a 280 fprintf(stderr, "Erase failed: %s",
0091182e 281 ftdi_get_error_string(ftdi));
bd4a9e38
UB
282 retval = -2;
283 goto done;
0091182e
UB
284 }
285 f = ftdi_write_eeprom(ftdi);
286 {
05c2e40a 287 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
0091182e 288 f, ftdi_get_error_string(ftdi));
bd4a9e38
UB
289 retval = 1;
290 goto done;
0091182e 291 }
05c2e40a 292 }
8ee8775e 293 retval = read_decode_eeprom(ftdi);
bd4a9e38 294done:
f38b0866 295 ftdi_usb_close(ftdi);
8ee8775e 296do_deinit:
f38b0866 297 ftdi_free(ftdi);
bd4a9e38 298 return retval;
2db3a766 299}