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