changeset 97:8d35346f1d46

cp2102 tools: accept "default" as device-selector
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 03:26:11 +0000 (15 months ago)
parents c6d04771bf6a
children 1cacc1ae56f0
files cp2102/Makefile cp2102/find_dev.c cp2102/read_baudtab.c cp2102/read_eeprom_main.c cp2102/read_partno.c cp2102/write_eeprom_main.c
diffstat 6 files changed, 61 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/cp2102/Makefile	Thu Sep 28 02:38:59 2023 +0000
+++ b/cp2102/Makefile	Thu Sep 28 03:26:11 2023 +0000
@@ -17,10 +17,13 @@
 DECODE_EEDESC_OBJS=	decode_usb_desc.o intel_hex_in.o
 PATCH_FILE_OBJS=	apply_eeprom_patch.o intel_hex_in.o intel_hex_out.o \
 			patch_ee_file.o
-READ_BAUDTAB_OBJS=	decode_baudtab.o read_baudtab.o read_eeprom.o
-READ_EEPROM_OBJS=	intel_hex_out.o read_eeprom.o read_eeprom_main.o
+READ_BAUDTAB_OBJS=	decode_baudtab.o find_dev.o read_baudtab.o read_eeprom.o
+READ_EEPROM_OBJS=	find_dev.o intel_hex_out.o read_eeprom.o \
+			read_eeprom_main.o
+READ_PARTNO_OBJS=	find_dev.o read_partno.o
 RW_TEST_OBJS=		intel_hex_in.o intel_hex_out.o file_rw_test.o
-WRITE_EEPROM_OBJS=	intel_hex_in.o write_eeprom.o write_eeprom_main.o
+WRITE_EEPROM_OBJS=	find_dev.o intel_hex_in.o write_eeprom.o \
+			write_eeprom_main.o
 
 all:	${PROGS} ${NOINST}
 
@@ -39,8 +42,8 @@
 cp2102-read-eeprom:	${READ_EEPROM_OBJS} ${LIBS}
 	${CC} ${CFLAGS} -o $@ ${READ_EEPROM_OBJS} ${LIBS} -lusb
 
-cp2102-read-partno:	read_partno.o ${LIBS}
-	${CC} ${CFLAGS} -o $@ read_partno.o ${LIBS} -lusb
+cp2102-read-partno:	${READ_PARTNO_OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${READ_PARTNO_OBJS} ${LIBS} -lusb
 
 cp2102-write-eeprom:	${WRITE_EEPROM_OBJS} ${LIBS}
 	${CC} ${CFLAGS} -o $@ ${WRITE_EEPROM_OBJS} ${LIBS} -lusb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/find_dev.c	Thu Sep 28 03:26:11 2023 +0000
@@ -0,0 +1,41 @@
+/*
+ * The function implemented in this module is common to all programs in
+ * FC CP2102 tools suite that operate on physical hardware: it asks libuwrap
+ * to locate either the default CP2102 VID:PID or a custom device selector
+ * string.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <usb.h>
+#include "../libuwrap/find_dev.h"
+
+static const struct usbdev_matchspec default_id = {0x10C4, 0xEA60};
+
+struct usb_device *
+find_cp2102_device(devsel_arg)
+	char *devsel_arg;
+{
+	struct usb_device *dev;
+
+	if (!strcmp(devsel_arg, "default")) {
+		dev = find_usbdev_by_matchspec(&default_id);
+		if (!dev) {
+			fprintf(stderr,
+				"error: no default-ID CP2102 device found\n");
+			exit(1);
+		}
+	} else {
+		dev = find_usbdev_by_desc_string(devsel_arg);
+		if (!dev) {
+			fprintf(stderr,
+				"error: specified USB device not found\n");
+			exit(1);
+		}
+	}
+	return dev;
+}
--- a/cp2102/read_baudtab.c	Thu Sep 28 02:38:59 2023 +0000
+++ b/cp2102/read_baudtab.c	Thu Sep 28 03:26:11 2023 +0000
@@ -14,6 +14,8 @@
 #include "../libuwrap/find_dev.h"
 #include "cp210x_defs.h"
 
+extern struct usb_device *find_cp2102_device();
+
 u_char eeprom[SIZE_EEPROM];
 
 main(argc, argv)
@@ -26,11 +28,7 @@
 		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
 		exit(1);
 	}
-	dev = find_usbdev_by_desc_string(argv[1]);
-	if (!dev) {
-		fprintf(stderr, "error: specified USB device not found\n");
-		exit(1);
-	}
+	dev = find_cp2102_device(argv[1]);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
--- a/cp2102/read_eeprom_main.c	Thu Sep 28 02:38:59 2023 +0000
+++ b/cp2102/read_eeprom_main.c	Thu Sep 28 03:26:11 2023 +0000
@@ -15,6 +15,8 @@
 #include "../libuwrap/find_dev.h"
 #include "cp210x_defs.h"
 
+extern struct usb_device *find_cp2102_device();
+
 u_char eeprom[SIZE_EEPROM];
 
 main(argc, argv)
@@ -29,11 +31,7 @@
 			argv[0]);
 		exit(1);
 	}
-	dev = find_usbdev_by_desc_string(argv[1]);
-	if (!dev) {
-		fprintf(stderr, "error: specified USB device not found\n");
-		exit(1);
-	}
+	dev = find_cp2102_device(argv[1]);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
--- a/cp2102/read_partno.c	Thu Sep 28 02:38:59 2023 +0000
+++ b/cp2102/read_partno.c	Thu Sep 28 03:26:11 2023 +0000
@@ -14,6 +14,8 @@
 #include "../libuwrap/find_dev.h"
 #include "cp210x_defs.h"
 
+extern struct usb_device *find_cp2102_device();
+
 main(argc, argv)
 	char **argv;
 {
@@ -26,11 +28,7 @@
 		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
 		exit(1);
 	}
-	dev = find_usbdev_by_desc_string(argv[1]);
-	if (!dev) {
-		fprintf(stderr, "error: specified USB device not found\n");
-		exit(1);
-	}
+	dev = find_cp2102_device(argv[1]);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
--- a/cp2102/write_eeprom_main.c	Thu Sep 28 02:38:59 2023 +0000
+++ b/cp2102/write_eeprom_main.c	Thu Sep 28 03:26:11 2023 +0000
@@ -14,6 +14,8 @@
 #include "../libuwrap/open_close.h"
 #include "cp210x_defs.h"
 
+extern struct usb_device *find_cp2102_device();
+
 u_char eeprom[SIZE_EEPROM];
 char *device_selector, *input_filename;
 int no_detach;
@@ -53,11 +55,7 @@
 
 	process_cmdline(argc, argv);
 	read_intel_hex(input_filename);
-	dev = find_usbdev_by_desc_string(device_selector);
-	if (!dev) {
-		fprintf(stderr, "error: specified USB device not found\n");
-		exit(1);
-	}
+	dev = find_cp2102_device(device_selector);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");