changeset 40:f5847be43d35

fc-duart28-conf: bump off both ttyUSB devices on set operation
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 10 Sep 2023 21:13:58 +0000
parents b9ecfa54fe2b
children 940cde8a99b6
files duart28/main.c libuwrap/Makefile libuwrap/claim_if.c libuwrap/open_close.h
diffstat 4 files changed, 36 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/duart28/main.c	Sun Sep 10 19:21:20 2023 +0000
+++ b/duart28/main.c	Sun Sep 10 21:13:58 2023 +0000
@@ -52,18 +52,24 @@
 		fprintf(stderr, "error: no DUART28 adapter found\n");
 		exit(1);
 	}
-	usbh = usbwrap_open_dev(dev, 1);
+	usbh = usb_open(dev);
+	if (!usbh) {
+		fprintf(stderr, "error: usb_open() failed\n");
+		exit(1);
+	}
+	usbwrap_claim_interface(usbh, 0, 1);
+	usbwrap_claim_interface(usbh, 1, 1);
 	read_eeprom(usbh);
 	prev = analyze_eeprom();
 	if (prev == newconf) {
 	printf("EEPROM is already in the right state, nothing to program\n");
-		usbwrap_close_dev(usbh);
+		usb_close(usbh);
 	printf("Please replug the adapter to restore normal operation\n");
 		return 0;
 	}
 	printf("Reprogramming to DUART28%c configuration\n", newconf);
 	update_eeprom(usbh, newconf);
-	usbwrap_close_dev(usbh);
+	usb_close(usbh);
 	printf("EEPROM changed, replug the adapter to take effect\n");
 	return 0;
 }
--- a/libuwrap/Makefile	Sun Sep 10 19:21:20 2023 +0000
+++ b/libuwrap/Makefile	Sun Sep 10 21:13:58 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	find_busdev.o find_desc_str.o find_matchspec.o open_close.o \
+OBJS=	claim_if.o find_busdev.o find_desc_str.o find_matchspec.o open_close.o \
 	prelim_init.o
 LIB=	libuwrap.a
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libuwrap/claim_if.c	Sun Sep 10 21:13:58 2023 +0000
@@ -0,0 +1,23 @@
+/*
+ * When we are reprogramming the EEPROM on DUART28 or possibly other devices
+ * in the future where we know that we are operating on a multichannel FTDI
+ * chip (or perhaps a multichannel chip from some other vendor), it is most
+ * philosophically correct to bump off ftdi_sio from both interfaces,
+ * removing both ttyUSB devices.  Here we implement a function for claiming
+ * one interface, easily callable for multiple interfaces in a row.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <usb.h>
+#include "open_close.h"
+
+void usbwrap_claim_interface(usb_dev_handle *usbh, int ifnum, int detach)
+{
+	if (detach)
+		usb_detach_kernel_driver_np(usbh, ifnum);
+	if (usb_claim_interface(usbh, ifnum) != 0) {
+		fprintf(stderr, "error: usb_claim_interface() failed\n");
+		exit(1);
+	}
+}
--- a/libuwrap/open_close.h	Sun Sep 10 19:21:20 2023 +0000
+++ b/libuwrap/open_close.h	Sun Sep 10 21:13:58 2023 +0000
@@ -1,2 +1,5 @@
 extern usb_dev_handle *usbwrap_open_dev(struct usb_device *dev, int detach);
 extern void usbwrap_close_dev(usb_dev_handle *usbh);
+
+extern void
+usbwrap_claim_interface(usb_dev_handle *usbh, int ifnum, int detach);