diff fteeprom/fteeprom-prog.c @ 20:43b8e88dae02

fteeprom-prog: convert to new local libs
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 08 Sep 2023 22:40:14 +0000
parents 11b8a30333b3
children 7e6dcceb5ee8
line wrap: on
line diff
--- a/fteeprom/fteeprom-prog.c	Thu Sep 07 23:24:04 2023 +0000
+++ b/fteeprom/fteeprom-prog.c	Fri Sep 08 22:40:14 2023 +0000
@@ -1,3 +1,12 @@
+/*
+ * This program connects to an FTDI chip via libusb and programs its EEPROM
+ * with a raw hex image read from stdin or from a backup file.
+ *
+ * The present version has been converted to use our local libraries
+ * (libftmini and libuwrap) instead of libftdi - thus the external dependency
+ * is only libusb.
+ */
+
 #include <sys/types.h>
 #include <ctype.h>
 #include <string.h>
@@ -5,7 +14,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <ftdi.h>
+#include <usb.h>
+#include "../libuwrap/find_dev.h"
+#include "../libuwrap/open_close.h"
+#include "../libftmini/eeprom_func.h"
 
 unsigned eeprom_size;
 u_short eeprom[256];
@@ -71,8 +83,8 @@
 main(argc, argv)
 	char **argv;
 {
-	struct ftdi_context ftdi;
-	u_short modem_status;
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
 	unsigned n;
 
 	if (argc < 2 || argc > 3) {
@@ -84,34 +96,15 @@
 		read_eeprom_from_file(argv[2]);
 	else
 		read_eeprom_from_stdin();
-	ftdi_init(&ftdi);
-	if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) {
-		fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
-		exit(1);
-	}
-	/* magic sequence apparently required for FT232R */
-	if (ftdi_usb_reset(&ftdi) < 0) {
-		fprintf(stderr, "ftdi_usb_reset() failed: %s\n",
-			ftdi.error_str);
-		exit(1);
-	}
-	if (ftdi_poll_modem_status(&ftdi, &modem_status) < 0) {
-		fprintf(stderr, "ftdi_poll_modem_status() failed: %s\n",
-			ftdi.error_str);
+	dev = find_usbdev_by_desc_string(argv[1]);
+	if (!dev) {
+		fprintf(stderr, "error: specified USB device not found\n");
 		exit(1);
 	}
-	if (ftdi_set_latency_timer(&ftdi, 0x77) < 0) {
-		fprintf(stderr, "ftdi_set_latency_timer() failed: %s\n",
-			ftdi.error_str);
-		exit(1);
-	}
-	for (n = 0; n < eeprom_size; n++) {
-		if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
-			fprintf(stderr, "EEPROM write error: %s\n",
-				ftdi.error_str);
-			exit(1);
-		}
-	}
-	ftdi_usb_close(&ftdi);
+	usbh = usbwrap_open_dev(dev, 1);
+	/* FIXME: reimplement FT232R magic sequence */
+	for (n = 0; n < eeprom_size; n++)
+		ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
+	usbwrap_close_dev(usbh);
 	exit(0);
 }