summaryrefslogtreecommitdiff
path: root/src/lan951x-led-ctl.c
blob: bff250d8a839c3f71ef5fce3ee0ee401703fba0c (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
172
173
174
175
176
/*
	lan951x-led-ctl - control LEDs of LAN951X ethernet/usb controllers
	
	Copyright (C) 2015-2022 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 "lan951x-led-ctl.h"
#include "version.h"

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

libusb_device_handle* lan951x_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;
	}
//	libusb_set_debug(NULL, 3);
	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)) {
//			fprintf(stderr, "LAN951x found on USB bus %d, device %d\n",
//				libusb_get_bus_number(dev),
//				libusb_get_device_address(dev));
			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;
		}
	}
	fprintf(stderr, "No LAN951x found on USB\n");
	libusb_free_device_list(devs, 1);
	return NULL;
}

int lan951x_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 lan951x_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: lan951x-led-ctl [--fdx=x][--lnk=x][--spd=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("lan951x-led-ctl %s programmed by Dominic Radermacher\n\n", VERSION);
	printf("For further info plese visit\n");
	printf("https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan951x-led-ctl/\n\n");
	printf("The latest version can be found in my git repo:\n");
	printf("https://git.familie-radermacher.ch/linux/lan951x-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, "--fdx=", 6) == 0) {
			led_arr[DUPIDX] = ledmode(p+6);
			printf("setting FDX LED to status %i\n", led_arr[DUPIDX]);
		} else if (strncmp(p, "--lnk=", 6) == 0) {
			led_arr[LNKIDX] = ledmode(p+6);
			printf("setting LNK LED to status %i\n", led_arr[LNKIDX]);
		} else if (strncmp(p, "--spd=", 6) == 0) {
			led_arr[SPDIDX] = ledmode(p+6);
			printf("setting SPD LED to status %i\n", led_arr[SPDIDX]);
		} else if (strcmp(p, "--version") == 0) {
			about();
		} else {
			usage();
		}
	}
	return i;
}

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

	if (argc < 2) {
		printf("at least one argument is required\n");
		usage();
	}
	parse_args(argc, argv);

	uint32_t gp_mask[3];
	gp_mask[DUPIDX] = DUP_MASK;
	gp_mask[LNKIDX] = LNK_MASK;
	gp_mask[SPDIDX] = SPD_MASK;

	if ((handle=lan951x_open(LAN951X_VENDOR_ID, LAN951X_PRODUCT_ID)) == NULL) {
		printf("can not open usb device. are you root?\n");
		exit(1);
	}
	lan951x_rd_reg(handle, LED_GPIO_CFG, &val);
	for (int i=0; i < 3; ++i) {
		if (led_arr[i] == MODE_ON) {
			val &= ~(gp_mask[i] & (GP_ALLCTL|GP_ALLDAT));
			val |= (gp_mask[i] & GP_ALLDIR);
		}
		if (led_arr[i] == MODE_OFF) {
			val &= ~(gp_mask[i] & GP_ALLCTL);
			val |= (gp_mask[i] & (GP_ALLDIR|GP_ALLDAT));
		}
		if (led_arr[i] == MODE_STATUS) {
			val &= ~(gp_mask[i] & (GP_ALLDIR|GP_ALLDAT));
			val |= (gp_mask[i] & GP_ALLCTL);
		}
	}
	lan951x_wr_reg(handle, LED_GPIO_CFG, val);
	libusb_close(handle);
}