view duart28/main.c @ 94:dd35206a5159

demo/Pirelli-CP2102-recreate: add demo file
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 02:04:36 +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;
}