-
Notifications
You must be signed in to change notification settings - Fork 9
/
acc_mdio.c
186 lines (149 loc) · 3.92 KB
/
acc_mdio.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
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
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2018 Pengutronix, Uwe Kleine-König <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, 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.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <linux/if.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include "fileaccpriv.h"
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *) &((type *)0)->member)
struct memtool_mdio_fd {
struct memtool_fd mfd;
int fd;
uint16_t phy_id;
char ifrn_name[IFNAMSIZ];
};
static ssize_t mdio_read(struct memtool_fd *handle, off_t offset,
void *buf, size_t nbytes, int width)
{
struct memtool_mdio_fd *mdio_fd =
container_of(handle, struct memtool_mdio_fd, mfd);
struct ifreq ifr;
struct mii_ioctl_data *mii = (void *)&ifr.ifr_data;
size_t i = 0;
int ret;
if (width != 2) {
fprintf(stderr,
"mdio can only be accessed with memory width 2\n");
return -EINVAL;
}
assert((nbytes & 1) == 0);
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, mdio_fd->ifrn_name);
mii->phy_id = mdio_fd->phy_id;
while (2 * i < nbytes) {
mii->reg_num = offset / 2 + i;
ret = ioctl(mdio_fd->fd, SIOCGMIIREG, &ifr);
if (ret < 0) {
perror("Failure to read register");
return -1;
}
((uint16_t *)buf)[i] = mii->val_out;
++i;
}
return 2 * i;
}
static ssize_t mdio_write(struct memtool_fd *handle, off_t offset,
const void *buf, size_t nbytes, int width)
{
struct memtool_mdio_fd *mdio_fd =
container_of(handle, struct memtool_mdio_fd, mfd);
struct ifreq ifr;
struct mii_ioctl_data *mii = (void *)&ifr.ifr_data;
size_t i = 0;
int ret;
if (width != 2) {
fprintf(stderr,
"mdio can only be accessed with memory width 2\n");
return -EINVAL;
}
assert((nbytes & 1) == 0);
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, mdio_fd->ifrn_name);
mii->phy_id = mdio_fd->phy_id;
while (2 * i < nbytes) {
mii->reg_num = offset / 2 + i;
mii->val_in = ((uint16_t *)buf)[i];
ret = ioctl(mdio_fd->fd, SIOCSMIIREG, &ifr);
if (ret < 0) {
perror("Failure to write register");
return -1;
}
++i;
}
return 2 * i;
}
static int mdio_close(struct memtool_fd *handle)
{
struct memtool_mdio_fd *mdio_fd =
container_of(handle, struct memtool_mdio_fd, mfd);
int ret;
ret = close(mdio_fd->fd);
free(mdio_fd);
return ret;
}
struct memtool_fd *mdio_open(const char *spec, int flags)
{
struct memtool_mdio_fd *mdio_fd;
char *delim;
char *endp;
long int val;
mdio_fd = malloc(sizeof(*mdio_fd));
if (!mdio_fd) {
fprintf(stderr, "Failure to allocate mdio_fd\n");
return NULL;
}
mdio_fd->mfd.read = mdio_read;
mdio_fd->mfd.write = mdio_write;
mdio_fd->mfd.close = mdio_close;
mdio_fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (mdio_fd->fd < 0) {
perror("socket");
goto err_socket;
}
delim = strrchr(spec, '.');
if (!delim) {
fprintf(stderr, "Failed to parse phy specifier, no \".\"\n");
goto err_parse;
}
if (delim - spec >= sizeof(mdio_fd->ifrn_name)) {
fprintf(stderr, "device string too long\n");
goto err_parse;
}
memcpy(mdio_fd->ifrn_name, spec, delim - spec);
mdio_fd->ifrn_name[delim - spec] = '\0';
val = strtol(delim + 1, &endp, 0);
if (*endp != '\0') {
fprintf(stderr, "failed to parse phy_id\n");
goto err_parse;
}
if (val < 0 || val >= (1 << 16)) {
fprintf(stderr, "phy_id out of range\n");
err_parse:
close(mdio_fd->fd);
err_socket:
free(mdio_fd);
return NULL;
}
mdio_fd->phy_id = val;
return &mdio_fd->mfd;
}