changeset 27:2413a54a1bfc

fc-duart28-conf started
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Sep 2023 17:53:21 +0000
parents 49239efbdcc8
children c4b4ebaa2117
files .hgignore duart28/Makefile duart28/find_usb.c duart28/main.c
diffstat 4 files changed, 184 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sat Sep 09 16:06:56 2023 +0000
+++ b/.hgignore	Sat Sep 09 17:53:21 2023 +0000
@@ -2,6 +2,8 @@
 
 \.[oa]$
 
+^duart28/fc-duart28-conf$
+
 ^fteeprom/ftee-gen2232c$
 ^fteeprom/ftee-gen2232h$
 ^fteeprom/ftee-gen232r$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/duart28/Makefile	Sat Sep 09 17:53:21 2023 +0000
@@ -0,0 +1,21 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	fc-duart28-conf
+OBJS=	find_usb.o main.o
+LIBS=	../libftmini/libftmini.a ../libuwrap/libuwrap.a
+
+INSTALL_PREFIX=	/opt/freecalypso
+
+INSTBIN=${INSTALL_PREFIX}/bin
+
+all:	${PROG}
+
+${PROG}:	${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} -lusb
+
+install:
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f ${PROG} *.o *errs *.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/duart28/find_usb.c	Sat Sep 09 17:53:21 2023 +0000
@@ -0,0 +1,84 @@
+/*
+ * This module implements the step of locating a connected FreeCalypso DUART28
+ * device (either C or S) at the libusb level, without booting off the kernel
+ * driver and claiming the interface yet.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+#include "../libuwrap/prelim_init.h"
+
+static void
+get_string(usb_dev_handle *usbh, int index, char *buf, size_t buflen)
+{
+	int rc;
+
+	rc = usb_get_string_simple(usbh, index, buf, buflen);
+	if (rc <= 0) {
+		fprintf(stderr, "error: USB string retrieval failed\n");
+		exit(1);
+	}
+}
+
+static void
+report_found_dev(struct usb_device *dev, unsigned usb_pid, char string_letter)
+{
+	printf("Found FreeCalypso DUART28 adapter at bus %s device %s,\n",
+		dev->bus->dirname, dev->filename);
+	if (usb_pid == 0x7152 && string_letter == 'C')
+		printf("presenting as DUART28C\n");
+	else if (usb_pid == 0x6010 && string_letter == 'S')
+		printf("presenting as DUART28S\n");
+	else
+		printf("presenting with inconsistent ID!\n");
+}
+
+static int
+is_match(struct usb_device *dev)
+{
+	struct usb_device_descriptor *desc = &dev->descriptor;
+	usb_dev_handle *usbh;
+	char strbuf[1024];
+
+	if (desc->idVendor != 0x0403)
+		return 0;
+	if (desc->idProduct != 0x6010 && desc->idProduct != 0x7152)
+		return 0;
+	usbh = usb_open(dev);
+	if (!usbh) {
+		fprintf(stderr, "error: usb_open() failed\n");
+		exit(1);
+	}
+	get_string(usbh, desc->iManufacturer, strbuf, sizeof strbuf);
+	if (strcmp(strbuf, "FreeCalypso")) {
+		usb_close(usbh);
+		return 0;
+	}
+	get_string(usbh, desc->iProduct, strbuf, sizeof strbuf);
+	if (strcmp(strbuf, "DUART28C") && strcmp(strbuf, "DUART28S")) {
+		usb_close(usbh);
+		return 0;
+	}
+	usb_close(usbh);
+	report_found_dev(dev, desc->idProduct, strbuf[7]);
+	return 1;
+}
+
+struct usb_device *
+find_duart28_usbdev()
+{
+	struct usb_bus *bus;
+	struct usb_device *dev;
+
+	libusb_prelim_init();
+	for (bus = usb_get_busses(); bus; bus = bus->next) {
+		for (dev = bus->devices; dev; dev = dev->next) {
+			if (is_match(dev))
+				return dev;
+		}
+	}
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/duart28/main.c	Sat Sep 09 17:53:21 2023 +0000
@@ -0,0 +1,77 @@
+/*
+ * Main module for fc-duart28-conf utility.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+
+extern struct usb_device *find_duart28_usbdev();
+
+oper_find()
+{
+	struct usb_device *dev;
+
+	dev = find_duart28_usbdev();
+	if (!dev)
+		printf("No DUART28 adapter found\n");
+	return 0;
+}
+
+oper_check_eeprom()
+{
+	fprintf(stderr, "error: check-eeprom command not yet implemented\n");
+	exit(1);
+}
+
+oper_program(newconf)
+{
+	fprintf(stderr, "error: set command not yet implemented\n");
+	exit(1);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc < 2)
+		return oper_find();
+	if (!strcmp(argv[1], "find")) {
+		if (argc != 2) {
+			fprintf(stderr,
+				"error: %s find command takes no arguments\n",
+				argv[0]);
+			exit(1);
+		}
+		return oper_find();
+	}
+	if (!strcmp(argv[1], "check-eeprom")) {
+		if (argc != 2) {
+			fprintf(stderr,
+			"error: %s check-eeprom command takes no arguments\n",
+				argv[0]);
+			exit(1);
+		}
+		return oper_check_eeprom();
+	}
+	if (!strcmp(argv[1], "set")) {
+		if (argc != 3) {
+			fprintf(stderr,
+				"error: %s set command takes 1 argument\n",
+				argv[0]);
+			exit(1);
+		}
+		if (!strcmp(argv[2], "C") || !strcmp(argv[2], "S"))
+			return oper_program(argv[2][0]);
+		if (!strcmp(argv[2], "c") || !strcmp(argv[2], "s"))
+			return oper_program(toupper(argv[2][0]));
+		fprintf(stderr, "error: %s set argument must be C or S\n",
+			argv[0]);
+		exit(1);
+	}
+	fprintf(stderr, "%s error: non-understood command \"%s\"\n", argv[0],
+		argv[1]);
+	exit(1);
+}