changeset 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 742c41f44658
children 065f68a94b6b
files fteeprom/fteeprom-erase.c fteeprom/fteeprom-prog.c libuwrap/claim_if.c libuwrap/open_close.h
diffstat 4 files changed, 17 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/fteeprom/fteeprom-erase.c	Wed Sep 13 00:43:14 2023 +0000
+++ b/fteeprom/fteeprom-erase.c	Wed Sep 13 06:37:03 2023 +0000
@@ -20,7 +20,7 @@
 #include "../libftmini/eeprom_func.h"
 
 char *device_selector;
-int detach_num;
+int no_detach;
 
 process_cmdline(argc, argv)
 	char **argv;
@@ -29,20 +29,10 @@
 	extern int optind;
 	extern char *optarg;
 
-	while ((c = getopt(argc, argv, "d:")) != EOF) {
+	while ((c = getopt(argc, argv, "n")) != EOF) {
 		switch (c) {
-		case 'd':
-			detach_num = atoi(optarg);
-			switch (detach_num) {
-			case 0:
-			case 1:
-			case 2:
-			case 4:
-				break;
-			default:
-				fprintf(stderr, "error: invalid -d argument\n");
-				exit(1);
-			}
+		case 'n':
+			no_detach = 1;
 			continue;
 		default:
 			/* error msg already printed */
@@ -74,8 +64,8 @@
 		fprintf(stderr, "error: usb_open() failed\n");
 		exit(1);
 	}
-	if (detach_num)
-		usbwrap_claim_all_ifs(usbh, detach_num);
+	if (!no_detach)
+		usbwrap_claim_all_ifs(usbh);
 	ftmini_erase_eeprom(usbh);
 	usb_close(usbh);
 	exit(0);
--- a/fteeprom/fteeprom-prog.c	Wed Sep 13 00:43:14 2023 +0000
+++ b/fteeprom/fteeprom-prog.c	Wed Sep 13 06:37:03 2023 +0000
@@ -23,7 +23,7 @@
 extern u_short eeprom[256];
 
 char *device_selector, *input_filename;
-int detach_num, ft232r_mode;
+int ft232r_mode, no_detach;
 
 process_cmdline(argc, argv)
 	char **argv;
@@ -32,24 +32,13 @@
 	extern int optind;
 	extern char *optarg;
 
-	while ((c = getopt(argc, argv, "d:r")) != EOF) {
+	while ((c = getopt(argc, argv, "nr")) != EOF) {
 		switch (c) {
-		case 'd':
-			detach_num = atoi(optarg);
-			switch (detach_num) {
-			case 0:
-			case 1:
-			case 2:
-			case 4:
-				break;
-			default:
-				fprintf(stderr, "error: invalid -d argument\n");
-				exit(1);
-			}
+		case 'n':
+			no_detach = 1;
 			continue;
 		case 'r':
 			ft232r_mode = 1;
-			detach_num = 1;
 			continue;
 		default:
 			/* error msg already printed */
@@ -88,8 +77,8 @@
 		fprintf(stderr, "error: usb_open() failed\n");
 		exit(1);
 	}
-	if (detach_num)
-		usbwrap_claim_all_ifs(usbh, detach_num);
+	if (!no_detach)
+		usbwrap_claim_all_ifs(usbh);
 	if (ft232r_mode)
 		ft232r_eeprom_magic(usbh);
 	for (n = 0; n < eeprom_size; n++)
--- a/libuwrap/claim_if.c	Wed Sep 13 00:43:14 2023 +0000
+++ b/libuwrap/claim_if.c	Wed Sep 13 06:37:03 2023 +0000
@@ -27,10 +27,12 @@
 	}
 }
 
-void usbwrap_claim_all_ifs(usb_dev_handle *usbh, unsigned nchannels)
+void usbwrap_claim_all_ifs(usb_dev_handle *usbh)
 {
+	struct usb_device *dev = usb_device(usbh);
+	unsigned numint = dev->config->bNumInterfaces;
 	unsigned n;
 
-	for (n = 0; n < nchannels; n++)
+	for (n = 0; n < numint; n++)
 		usbwrap_claim_interface(usbh, n, 1);
 }
--- a/libuwrap/open_close.h	Wed Sep 13 00:43:14 2023 +0000
+++ b/libuwrap/open_close.h	Wed Sep 13 06:37:03 2023 +0000
@@ -3,4 +3,4 @@
 
 extern void
 usbwrap_claim_interface(usb_dev_handle *usbh, int ifnum, int detach);
-extern void usbwrap_claim_all_ifs(usb_dev_handle *usbh, unsigned nchannels);
+extern void usbwrap_claim_all_ifs(usb_dev_handle *usbh);