changeset 73:201503e4be18

ftee-gen2232c: add ftdi-chip and eeprom settings
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Sep 2023 23:23:32 +0000
parents 6dc3aa777fd6
children d069e2a6760e
files fteeprom/ftee-gen2232c.c
diffstat 1 files changed, 68 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/fteeprom/ftee-gen2232c.c	Mon Sep 25 00:04:22 2023 +0000
+++ b/fteeprom/ftee-gen2232c.c	Mon Sep 25 23:23:32 2023 +0000
@@ -1,3 +1,8 @@
+/*
+ * This program constucts a configuration EEPROM image for FT2232C/D chips
+ * based on a configuration source file giving various settings.
+ */
+
 #include <sys/types.h>
 #include <ctype.h>
 #include <string.h>
@@ -18,22 +23,26 @@
 u_short usb_version = 0x0200;
 
 u_short eeprom[128];
-u_char eeprom_chip = 0x46;
+u_char eeprom_chip = 0x46, eeprom_chip_cmdline;
 unsigned eeprom_size, eeprom_string_ptr;
 
+static void
 process_cmdline(argc, argv)
 	char **argv;
 {
 	int c;
 	extern int optind;
 
-	while ((c = getopt(argc, argv, "bB")) != EOF) {
+	while ((c = getopt(argc, argv, "bBs")) != EOF) {
 		switch (c) {
 		case 'b':
-			eeprom_chip = 0x56;
+			eeprom_chip = eeprom_chip_cmdline = 0x56;
 			continue;
 		case 'B':
-			eeprom_chip = 0x66;
+			eeprom_chip = eeprom_chip_cmdline = 0x66;
+			continue;
+		case 's':
+			eeprom_chip = eeprom_chip_cmdline = 0x46;
 			continue;
 		default:
 			/* error msg already printed */
@@ -50,17 +59,45 @@
 	serial = argv[optind+1];
 }
 
-init_eeprom_size()
+static void
+ftdi_chip_setting(arg, filename_for_errs, lineno)
+	char *arg, *filename_for_errs;
 {
-	if (eeprom_chip == 0x46) {
-		eeprom_size = 64;
-		eeprom_string_ptr = 0x0B;
-	} else {
-		eeprom_size = 128;
-		eeprom_string_ptr = 0x4B;
+	if (!strcasecmp(arg, "FT2232C"))
+		return;
+	if (!strcasecmp(arg, "FT2232D"))
+		return;
+	if (!strcasecmp(arg, "FT2232x"))
+		return;
+	fprintf(stderr, "%s line %d: config is for wrong FTDI chip\n",
+		filename_for_errs, lineno);
+	exit(1);
+}
+
+static void
+eeprom_setting(arg, filename_for_errs, lineno)
+	char *arg, *filename_for_errs;
+{
+	if (!strcasecmp(arg, "93C46"))
+		eeprom_chip = 0x46;
+	else if (!strcasecmp(arg, "93C56"))
+		eeprom_chip = 0x56;
+	else if (!strcasecmp(arg, "93C66"))
+		eeprom_chip = 0x66;
+	else {
+		fprintf(stderr, "%s line %d: invalid eeprom setting\n",
+			filename_for_errs, lineno);
+		exit(1);
+	}
+	if (eeprom_chip_cmdline && eeprom_chip_cmdline != eeprom_chip) {
+		fprintf(stderr,
+	"%s line %d: eeprom setting disagrees with command line option\n",
+			filename_for_errs, lineno);
+		exit(1);
 	}
 }
 
+static void
 read_config_file()
 {
 	FILE *inf;
@@ -118,6 +155,10 @@
 			maxpower = strtoul(cp, 0, 10);
 		else if (!strcmp(np, "usbver"))
 			usb_version = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "ftdi-chip"))
+			ftdi_chip_setting(cp, configfile, lineno);
+		else if (!strcmp(np, "eeprom"))
+			eeprom_setting(cp, configfile, lineno);
 		else {
 			fprintf(stderr, "%s line %d: unknown \"%s\" setting\n",
 				configfile, lineno, np);
@@ -135,6 +176,19 @@
 	}
 }
 
+static void
+init_eeprom_size()
+{
+	if (eeprom_chip == 0x46) {
+		eeprom_size = 64;
+		eeprom_string_ptr = 0x0B;
+	} else {
+		eeprom_size = 128;
+		eeprom_string_ptr = 0x4B;
+	}
+}
+
+static int
 write_string(str)
 	char *str;
 {
@@ -152,6 +206,7 @@
 	return (longlen << 8) | 0x80 | (startptr << 1);
 }
 
+static void
 fill_eeprom()
 {
 	u_char byte09;
@@ -177,6 +232,7 @@
 	eeprom[10] = eeprom_chip;
 }
 
+static void
 do_checksum()
 {
 	u_short chksum = 0xAAAA;
@@ -189,6 +245,7 @@
 	eeprom[n] = chksum;
 }
 
+static void
 emit_output()
 {
 	unsigned n, col;