-
Notifications
You must be signed in to change notification settings - Fork 29
/
mdio-tool.c
114 lines (94 loc) · 2.85 KB
/
mdio-tool.c
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
/*
mdio-tool allow for direct access to mdio registers in a network phy.
Routines are based on mii-tool: http://freecode.com/projects/mii-tool
mdio-tool comes with ABSOLUTELY NO WARRANTY; Use with care!
Copyright (C) 2013 Pieter Voorthuijsen
mdio-tool is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
mdio-tool 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 mdio-tool. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#ifndef __GLIBC__
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#endif
#include "mii.h"
#define MAX_ETH 8 /* Maximum # of interfaces */
static int skfd = -1; /* AF_INET socket for ioctl() calls. */
static struct ifreq ifr;
/*--------------------------------------------------------------------*/
static int mdio_read(int skfd, int location)
{
struct mii_data *mii = (struct mii_data *)&ifr.ifr_data;
mii->reg_num = location;
if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name,
strerror(errno));
return -1;
}
return mii->val_out;
}
static void mdio_write(int skfd, int location, int value)
{
struct mii_data *mii = (struct mii_data *)&ifr.ifr_data;
mii->reg_num = location;
mii->val_in = value;
if (ioctl(skfd, SIOCSMIIREG, &ifr) < 0) {
fprintf(stderr, "SIOCSMIIREG on %s failed: %s\n", ifr.ifr_name,
strerror(errno));
}
}
int main(int argc, char **argv)
{
int addr, dev, val;
struct mii_data *mii = (struct mii_data *)&ifr.ifr_data;
if(argc < 2) {
printf("Usage mii [r/w] [dev] [reg] [val]\n");
return 0;
}
/* Open a basic socket. */
if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
perror("socket");
return -1;
}
/* Get the vitals from the interface. */
strncpy(ifr.ifr_name, argv[2], IFNAMSIZ);
if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
if (errno != ENODEV)
fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
argv[2], strerror(errno));
return -1;
}
if(argv[1][0] == 'r') {
addr = strtol(argv[3], NULL, 16);
printf("0x%.4x\n", mdio_read(skfd, addr));
}
else if(argv[1][0] == 'w') {
addr = strtol(argv[3], NULL, 16);
val = strtol(argv[4], NULL, 16);
mdio_write(skfd, addr, val);
}
else {
printf("Fout!\n");
}
close(skfd);
}