annotate duart28/main.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 f5847be43d35
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Main module for fc-duart28-conf utility.
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <ctype.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <usb.h>
29
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
11 #include "../libuwrap/open_close.h"
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern struct usb_device *find_duart28_usbdev();
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
14 extern unsigned usb_presenting_pid;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
16 oper_show()
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
29
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
18 struct usb_device *dev;
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
19 usb_dev_handle *usbh;
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
20 int eeconf;
29
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
21
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
22 dev = find_duart28_usbdev();
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
23 if (!dev) {
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
24 fprintf(stderr, "error: no DUART28 adapter found\n");
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
25 exit(1);
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
26 }
33
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
27 usbh = usb_open(dev);
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
28 if (!usbh) {
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
29 fprintf(stderr, "error: usb_open() failed\n");
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
30 exit(1);
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
31 }
29
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
32 read_eeprom(usbh);
33
df284688d0c8 fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
33 usb_close(usbh);
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
34 eeconf = analyze_eeprom();
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
35 if (eeconf == 'C' && usb_presenting_pid != 0x7152)
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
36 printf("Visible USB device has wrong PID for C config"
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
37 " - please replug\n");
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
38 if (eeconf == 'S' && usb_presenting_pid != 0x6010)
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
39 printf("Visible USB device has wrong PID for S config"
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
40 " - please replug\n");
29
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
41 return 0;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 oper_program(newconf)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
31
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
46 struct usb_device *dev;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
47 usb_dev_handle *usbh;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
48 int prev;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
49
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
50 dev = find_duart28_usbdev();
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
51 if (!dev) {
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
52 fprintf(stderr, "error: no DUART28 adapter found\n");
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
53 exit(1);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
54 }
40
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
55 usbh = usb_open(dev);
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
56 if (!usbh) {
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
57 fprintf(stderr, "error: usb_open() failed\n");
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
58 exit(1);
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
59 }
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
60 usbwrap_claim_interface(usbh, 0, 1);
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
61 usbwrap_claim_interface(usbh, 1, 1);
31
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
62 read_eeprom(usbh);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
63 prev = analyze_eeprom();
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
64 if (prev == newconf) {
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
65 printf("EEPROM is already in the right state, nothing to program\n");
40
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
66 usb_close(usbh);
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
67 printf("Please replug the adapter to restore normal operation\n");
31
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
68 return 0;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
69 }
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
70 printf("Reprogramming to DUART28%c configuration\n", newconf);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
71 update_eeprom(usbh, newconf);
40
f5847be43d35 fc-duart28-conf: bump off both ttyUSB devices on set operation
Mychaela Falconia <falcon@freecalypso.org>
parents: 39
diff changeset
72 usb_close(usbh);
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
73 printf("EEPROM changed, replug the adapter to take effect\n");
31
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
74 return 0;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 main(argc, argv)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 char **argv;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 {
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
80 if (argc < 2) {
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
81 usage: fprintf(stderr, "usage: %s show or %s set C|S\n",
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
82 argv[0], argv[0]);
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
83 exit(1);
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
84 }
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
85 if (!strcmp(argv[1], "show")) {
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (argc != 2) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 fprintf(stderr,
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
88 "error: %s show command takes no arguments\n",
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
92 return oper_show();
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (!strcmp(argv[1], "set")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (argc != 3) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 fprintf(stderr,
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 "error: %s set command takes 1 argument\n",
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 if (!strcmp(argv[2], "C") || !strcmp(argv[2], "S"))
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 return oper_program(argv[2][0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 if (!strcmp(argv[2], "c") || !strcmp(argv[2], "s"))
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 return oper_program(toupper(argv[2][0]));
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fprintf(stderr, "error: %s set argument must be C or S\n",
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
109 goto usage;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }