annotate cp2102/update_eeprom.c @ 106:de3b299561b3

udev: add rules file that works on my system
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Oct 2023 01:08:28 +0000
parents c59011177e2e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
2 * This program locates a CP2102 device via libusb, reads its internal EEPROM,
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
3 * applies user-requested patches to the EEPROM image, and then reprograms
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
4 * the physical EEPROM with the patched version.
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <unistd.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <usb.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libuwrap/find_dev.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../libuwrap/open_close.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include "cp210x_defs.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
18 extern struct usb_device *find_cp2102_device();
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
19
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char eeprom[SIZE_EEPROM];
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
21 char *device_selector, *baud_change;
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int no_detach;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 process_cmdline(argc, argv)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char **argv;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 int c;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 extern int optind;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 extern char *optarg;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
31 while ((c = getopt(argc, argv, "b:d:n")) != EOF) {
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 switch (c) {
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
33 case 'b':
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
34 baud_change = optarg;
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
35 continue;
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
36 case 'd':
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
37 device_selector = optarg;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
38 continue;
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 case 'n':
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 no_detach = 1;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 continue;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 default:
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
43 usage:
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
44 fprintf(stderr,
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
45 "usage: %s [hw-options] eeprom-changes\n",
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
46 argv[0]);
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 exit(1);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
50 if (argc == optind && !baud_change)
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
51 goto usage;
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 main(argc, argv)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 char **argv;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 struct usb_device *dev;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 usb_dev_handle *usbh;
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
59 extern int optind;
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 process_cmdline(argc, argv);
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
62 dev = find_cp2102_device(device_selector);
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 usbh = usb_open(dev);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (!usbh) {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(stderr, "error: usb_open() failed\n");
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 exit(1);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
99
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
68 read_eeprom(usbh);
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
69 if (baud_change)
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
70 apply_eeprom_patch_baud(baud_change);
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
71 for (; optind < argc; optind++)
c59011177e2e cp2102-update-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 98
diff changeset
72 apply_eeprom_patch_file(argv[optind]);
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (!no_detach)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 usbwrap_claim_all_ifs(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 write_eeprom(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 usb_close(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 exit(0);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }