summaryrefslogtreecommitdiff
path: root/src/lan7800-led-ctl.c
blob: ff1b628f92414a55cce2f24fafbd87b4f91df03c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
	lan7800-led-ctl - control LEDs of LAN7800 ethernet/usb controllers
	
	Copyright (C) 2018-2024 Dominic Radermacher <dominic@familie-radermacher.ch>
	
	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License version 3 as
	published by the Free Software Foundation
	
	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <stdlib.h>		/* exit() */
#include <string.h>		/* strlen() */
#include <stdint.h>
#include <libusb-1.0/libusb.h>

#include "lan7800-led-ctl.h"
#include "version.h"		/* generated by cmake */

/* global variables */
int led_arr[3] = { MODE_KEEP, MODE_KEEP, MODE_KEEP };

libusb_device_handle *usbdev_open(int vid, int pid)
{
	libusb_device **devs;
	libusb_device *dev;
	libusb_device_handle *handle = NULL;
	struct libusb_device_descriptor desc;
	int r,i=0;
	
	if ((libusb_init(NULL)) < 0) {
		fprintf(stderr, "libusb_init() failed\n");
		return NULL;
	}
	if (libusb_get_device_list(NULL, &devs) < 0) {
		return NULL;
	}
	while ((dev=devs[i++]) != NULL) {
		if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
			fprintf(stderr, "failed to get device descriptor");
			libusb_free_device_list(devs, 1);
			return NULL;
		}
		if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
			if ((r=libusb_open(dev, &handle)) != 0) {
				fprintf(stderr, "libusb_open error :%s\n", libusb_error_name(r));
				return NULL;
			}
			libusb_free_device_list(devs, 1);
			return handle;
		}
	}
	libusb_free_device_list(devs, 1);
	return NULL;
}

int lan7800_rd_reg(libusb_device_handle *h, uint16_t reg, uint32_t *val)
{
	return libusb_control_transfer(h, LIBUSB_REQUEST_TYPE_VENDOR|0x80,
		USB_VENDOR_REQUEST_RD_REG, 0, reg, (uint8_t *)val, 4,
		USB_CTRL_TIMEOUT);
}

int lan7800_wr_reg(libusb_device_handle *h, uint16_t reg, uint32_t val)
{
	return libusb_control_transfer(h, LIBUSB_REQUEST_TYPE_VENDOR,
		USB_VENDOR_REQUEST_WR_REG, 0, reg, (uint8_t *)&val, 4,
		USB_CTRL_TIMEOUT);
}

int ledmode(const char* s)
{
	if (*s == '0') {
		return MODE_OFF;
	} else if (*s == '1') {
		return MODE_ON;
	} else if ((*s == 's') || (*s == 'S')) {
		return MODE_STATUS;
	}
	return MODE_ERR;
}

void usage(void)
{
	printf("usage: lan7800-led-ctl [--led0=x][--led1=x]\n");
	printf("\twhere x is one of:\n");
	printf("\t0 - turn LED off\n\t1 - turn LED on\n\ts - LED shows status\n");
	exit(1);
}

void about(void)
{
	printf("lan7800-led-ctl %s programmed by Dominic Radermacher\n", VERSION);
	printf("For further info please visit\n");
	printf("https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan7800-led-ctl/\n");
	printf("The latest version can be found in my git repo:\n");
	printf("https://git.familie-radermacher.ch/linux/lan7800-led-ctl.git\n");
	exit(1);
}

int parse_args(int argc, char **argv)
{
	int i;

	for (i=1; i<argc; i++) {
		char *p=argv[i];
		if (strncmp(p, "--led0=", 7) == 0) {
			led_arr[0] = ledmode(p+7);
			printf("setting LED0 to status %i\n", led_arr[0]);
		} else if (strncmp(p, "--led1=", 7) == 0) {
			led_arr[1] = ledmode(p+7);
			printf("setting LED1 to status %i\n", led_arr[1]);
		} else if (strcmp(p, "--about") == 0) {
			about();
		} else if (strcmp(p, "--version") == 0) {
			about();
		} else {
			usage();
		}
	}
	return i;
}

int main(int argc, char *argv[])
{
	libusb_device_handle *handle = NULL;
	uint32_t reg_hw_cfg;
	uint32_t gp_mask[2];

	gp_mask[0] = LED0_MASK;
	gp_mask[1] = LED1_MASK;
	if (argc < 2) {
		printf("at least one argument is required\n");
		usage();
	}
	parse_args(argc, argv);
	if ((handle=usbdev_open(LAN7800_VENDOR_ID, LAN7800_PRODUCT_ID)) == NULL) {
		printf("No LAN7800 found on USB (are you root?)\n");
		exit(1);
	}
	lan7800_rd_reg(handle, REG_HW_CFG, &reg_hw_cfg);
#ifdef DEBUG
	printf("read HW_CFG register: %08x\n", reg_hw_cfg);
#endif
	for (int i=0; i < 2; i++) {
		if (led_arr[i] == MODE_ON) {
			reg_hw_cfg &= ~(gp_mask[i] & HW_CFG_LED_EN);
			printf("turning LED on not supported yet\n");
		}
		if (led_arr[i] == MODE_OFF) {
			reg_hw_cfg &= ~(gp_mask[i] & HW_CFG_LED_EN);
 		}
		if (led_arr[i] == MODE_STATUS) {
			reg_hw_cfg |= (gp_mask[i] & HW_CFG_LED_EN);
		}
	}
#ifdef DEBUG
	printf("write %08x to HG_CFG\n", reg_hw_cfg);
#endif
	lan7800_wr_reg(handle, REG_HW_CFG, reg_hw_cfg);
	libusb_close(handle);
}