-
Notifications
You must be signed in to change notification settings - Fork 0
/
axcmd.c
295 lines (248 loc) · 7.06 KB
/
axcmd.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// SPDX-License-Identifier: GPL-2.0
/*******************************************************************************
* Copyright (c) 2022 ASIX Electronic Corporation All rights reserved.
*
* This program 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.
*
* 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, see <https://www.gnu.org/licenses/>.
******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#if NET_INTERFACE == INTERFACE_SCAN
#include <ifaddrs.h>
#endif
#include "ax_ioctl.h"
#define ASIX_IOCTL_VERSION "ASIX Linux Command Tool v1.0.0"
const char help_str1[] =
"./ioctl help [command]\n"
" -- command description\n";
const char help_str2[] =
" [command] - Display usage of specified command\n";
const char usbcommand_str1[] =
"./axcmd usb [ops] [cmd] [value] [index] [size] [data]\n"
" -- ASIX USB Command\n";
const char usbcommand_str2[] =
" [ops] - 0: Read, 1: Write\n"
" [cmd] - USB Command (0 - 0xFF)\n"
" [value] - USB wvalue (0 - 0xFFFF)\n"
" [index] - USB windex (0 - 0xFFFF)\n"
" [size] - USB wlength (0 - 0xFFFF)\n"
" [data] - Data (0 - 0xFFFFFFFF)\n";
static int help_func(struct ax_command_info *info);
static int usbcommand_func(struct ax_command_info *info);
struct _command_list asix_cmd_list[] = {
{ "help",
AX_SIGNATURE,
help_func,
help_str1,
help_str2
},
{ "usb",
AX_USB_COMMAND,
usbcommand_func,
usbcommand_str1,
usbcommand_str2
},
{NULL},
};
static void show_usage(void)
{
int i;
printf("Usage:\n");
for (i = 0; asix_cmd_list[i].cmd != NULL; i++)
printf("%s\n", asix_cmd_list[i].help_ins);
}
static unsigned long STR_TO_U32(const char *cp, char **endp,
unsigned int base)
{
unsigned long result = 0, value;
if (*cp == '0') {
cp++;
if ((*cp == 'x') && isxdigit(cp[1])) {
base = 16;
cp++;
}
if (!base)
base = 8;
}
if (!base)
base = 10;
while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
? toupper(*cp) : *cp)-'A'+10) < base) {
result = result*base + value;
cp++;
}
if (endp)
*endp = (char *)cp;
return result;
}
static int help_func(struct ax_command_info *info)
{
int i;
if (info->argv[2] == NULL) {
for (i = 0; asix_cmd_list[i].cmd != NULL; i++) {
printf("%s%s\n",
asix_cmd_list[i].help_ins,
asix_cmd_list[i].help_desc);
}
}
for (i = 0; asix_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1],
asix_cmd_list[i].cmd,
strlen(asix_cmd_list[i].cmd)) == 0) {
printf("%s%s\n",
asix_cmd_list[i].help_ins,
asix_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
static int usbcommand_func(struct ax_command_info *info)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
struct _ax_usb_command *usb_cmd;
int i;
if (info->argc > 8 || info->argc < 7) {
for (i = 0; asix_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1], asix_cmd_list[i].cmd,
strlen(asix_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", asix_cmd_list[i].help_ins,
asix_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
usb_cmd = &ioctl_cmd.usb_cmd;
memset(usb_cmd, 0, sizeof(*usb_cmd));
usb_cmd->ops = STR_TO_U32(info->argv[2], NULL, 16);
usb_cmd->cmd = STR_TO_U32(info->argv[3], NULL, 16);
usb_cmd->value = STR_TO_U32(info->argv[4], NULL, 16);
usb_cmd->index = STR_TO_U32(info->argv[5], NULL, 16);
usb_cmd->size = STR_TO_U32(info->argv[6], NULL, 16);
if (usb_cmd->ops > USB_WRITE_OPS) {
for (i = 0; asix_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1], asix_cmd_list[i].cmd,
strlen(asix_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", asix_cmd_list[i].help_ins,
asix_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
if (usb_cmd->ops == USB_WRITE_OPS)
usb_cmd->cmd_data = STR_TO_U32(info->argv[7], NULL, 16);
ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
ioctl_cmd.size = usb_cmd->size;
ioctl_cmd.buf = NULL;
ioctl_cmd.type = 0;
ioctl_cmd.delay = 0;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
perror("ioctl");
return -FAIL_IOCTL;
}
if (usb_cmd->ops == USB_READ_OPS) {
printf("Read Command: CMD: 0x%02X\n", usb_cmd->cmd);
printf("wValue: 0x%04X, wIndex: 0x%04X, wLength: 0x%04X",
usb_cmd->value, usb_cmd->index, usb_cmd->size);
printf("\nData: 0x%08lX\n", usb_cmd->cmd_data);
}
printf("Command completely\n");
return SUCCESS;
}
int main(int argc, char **argv)
{
int inet_sock;
struct ifreq ifr;
struct ax_command_info info;
unsigned char i;
unsigned char count = 0;
struct _ax_ioctl_command ioctl_cmd;
#if NET_INTERFACE == INTERFACE_SCAN
struct ifaddrs *addrs, *tmp;
unsigned char dev_exist;
#endif
if (argc < 2) {
show_usage();
return 0;
}
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
#if NET_INTERFACE == INTERFACE_SCAN
getifaddrs(&addrs);
tmp = addrs;
dev_exist = 0;
while (tmp) {
memset(&ioctl_cmd, 0, sizeof(ioctl_cmd));
ioctl_cmd.ioctl_cmd = AX_SIGNATURE;
sprintf(ifr.ifr_name, "%s", tmp->ifa_name);
ifr.ifr_data = (caddr_t)&ioctl_cmd;
tmp = tmp->ifa_next;
if (ioctl(inet_sock, AX_PRIVATE, &ifr) < 0)
continue;
if (strncmp(ioctl_cmd.sig, AX88179_DRV_NAME,
strlen(AX88179_DRV_NAME)) == 0) {
dev_exist = 1;
break;
}
if (strncmp(ioctl_cmd.sig, AX88179A_DRV_NAME,
strlen(AX88179A_DRV_NAME)) == 0) {
dev_exist = 1;
break;
}
}
freeifaddrs(addrs);
if (dev_exist == 0) {
printf("No ASIX device found\n");
return 0;
}
#else
for (i = 0; i < 255; i++) {
memset(&ioctl_cmd, 0, sizeof(ioctl_cmd));
ioctl_cmd.ioctl_cmd = AX_SIGNATURE;
sprintf(ifr.ifr_name, "eth%d", i);
ifr.ifr_data = (caddr_t)&ioctl_cmd;
if (ioctl(inet_sock, AX_PRIVATE, &ifr) < 0)
continue;
if (strncmp(ioctl_cmd.sig, AX88179_DRV_NAME,
strlen(AX88179_DRV_NAME)) == 0)
break;
if (strncmp(ioctl_cmd.sig, AX88179A_DRV_NAME,
strlen(AX88179A_DRV_NAME)) == 0)
break;
}
if (i == 255) {
printf("No ASIX device found\n");
return 0;
}
#endif
for (i = 0; asix_cmd_list[i].cmd != NULL; i++) {
if (strncmp(argv[1], asix_cmd_list[i].cmd,
strlen(asix_cmd_list[i].cmd)) == 0) {
info.help_ins = asix_cmd_list[i].help_ins;
info.help_desc = asix_cmd_list[i].help_desc;
info.ifr = 𝔦
info.argc = argc;
info.argv = argv;
info.inet_sock = inet_sock;
info.ioctl_cmd = asix_cmd_list[i].ioctl_cmd;
(asix_cmd_list[i].OptFunc)(&info);
return 0;
}
}
printf("Wrong command\n");
return 0;
}