annotate fteeprom/fteeprom-read.c @ 68:5cbde3c80c24

fteeprom-{erase,prog}: detach logic: change to detach by default As it turns out, detaching all ttyUSB interfaces of a multichannel device does not require outside knowledge of how many channels there are, as in our previous -d option design that is being removed here - instead we can read the bNumInterfaces constant from the USB device's config descriptor and thus know how many interfaces there are in total. Based on this discovery, change the design of fteeprom-{erase,prog} as follows: * remove -d option; * flip the default to where we detach all interfaces by default; * add -n option to NOT detach any interfaces.
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Sep 2023 06:37:03 +0000
parents bb1d0956b0a8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
1 /*
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
2 * This program connects to an FTDI chip via libusb and reads out its EEPROM.
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
3 * The present version has been converted to use our local libraries
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
4 * (libftmini and libuwrap) instead of libftdi - thus the external dependency
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
5 * is only libusb.
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
6 */
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
7
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <unistd.h>
16
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
14 #include <usb.h>
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
15 #include "../libuwrap/find_dev.h"
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
16 #include "../libuwrap/open_close.h"
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
17 #include "../libftmini/eeprom_func.h"
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *device_selector;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 unsigned eeprom_size = 64;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 process_cmdline(argc, argv)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 char **argv;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 int c;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 extern int optind;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 while ((c = getopt(argc, argv, "bB")) != EOF) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 switch (c) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 case 'b':
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 eeprom_size = 128;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 continue;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 case 'B':
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 eeprom_size = 256;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 continue;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 default:
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 /* error msg already printed */
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (argc != optind + 1) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 fprintf(stderr, "usage: %s [options] device-selector\n",
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 argv[0]);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 device_selector = argv[optind];
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 main(argc, argv)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 char **argv;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 {
16
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
52 struct usb_device *dev;
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
53 usb_dev_handle *usbh;
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 u_short word;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 unsigned n, col;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 process_cmdline(argc, argv);
16
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
58 dev = find_usbdev_by_desc_string(device_selector);
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
59 if (!dev) {
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
60 fprintf(stderr, "error: specified USB device not found\n");
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
32
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
63 usbh = usb_open(dev);
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
64 if (!usbh) {
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
65 fprintf(stderr, "error: usb_open() failed\n");
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
66 exit(1);
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
67 }
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 for (n = 0; n < eeprom_size; n++) {
16
1d76deae1e74 fteeprom-read: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
69 word = ftmini_read_eeprom_loc(usbh, n);
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 col = n & 7;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (col == 0)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 printf("%02X:", n * 2);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 printf(" %04X", word);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (col == 7)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putchar('\n');
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
32
bb1d0956b0a8 fteeprom-read: we can do it without kicking off ftdi_sio driver
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
77 usb_close(usbh);
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 exit(0);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }