view duart28/main.c @ 68:5cbde3c80c24

fteeprom-{erase,prog}: detach logic: change to detach by default As it turns out, detaching all ttyUSB interfaces of a multichannel device does not require outside knowledge of how many channels there are, as in our previous -d option design that is being removed here - instead we can read the bNumInterfaces constant from the USB device's config descriptor and thus know how many interfaces there are in total. Based on this discovery, change the design of fteeprom-{erase,prog} as follows: * remove -d option; * flip the default to where we detach all interfaces by default; * add -n option to NOT detach any interfaces.
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Sep 2023 06:37:03 +0000
parents f5847be43d35
children
line wrap: on
line source

/*
 * 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>
#include "../libuwrap/open_close.h"

extern struct usb_device *find_duart28_usbdev();
extern unsigned usb_presenting_pid;

oper_show()
{
	struct usb_device *dev;
	usb_dev_handle *usbh;
	int eeconf;

	dev = find_duart28_usbdev();
	if (!dev) {
		fprintf(stderr, "error: no DUART28 adapter found\n");
		exit(1);
	}
	usbh = usb_open(dev);
	if (!usbh) {
		fprintf(stderr, "error: usb_open() failed\n");
		exit(1);
	}
	read_eeprom(usbh);
	usb_close(usbh);
	eeconf = analyze_eeprom();
	if (eeconf == 'C' && usb_presenting_pid != 0x7152)
		printf("Visible USB device has wrong PID for C config"
			" - please replug\n");
	if (eeconf == 'S' && usb_presenting_pid != 0x6010)
		printf("Visible USB device has wrong PID for S config"
			" - please replug\n");
	return 0;
}

oper_program(newconf)
{
	struct usb_device *dev;
	usb_dev_handle *usbh;
	int prev;

	dev = find_duart28_usbdev();
	if (!dev) {
		fprintf(stderr, "error: no DUART28 adapter found\n");
		exit(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");
		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);
	usb_close(usbh);
	printf("EEPROM changed, replug the adapter to take effect\n");
	return 0;
}

main(argc, argv)
	char **argv;
{
	if (argc < 2) {
usage:		fprintf(stderr, "usage: %s show  or  %s set C|S\n",
			argv[0], argv[0]);
		exit(1);
	}
	if (!strcmp(argv[1], "show")) {
		if (argc != 2) {
			fprintf(stderr,
				"error: %s show command takes no arguments\n",
				argv[0]);
			exit(1);
		}
		return oper_show();
	}
	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);
	}
	goto usage;
}