blob: e2421f24d49f5632eeaa51be2a53a84b7a04ca47 [file] [log] [blame]
developer8fb759f2022-02-21 16:39:38 +08001/* Copyright (C) 2021 Mediatek Inc. */
2#define _GNU_SOURCE
3
4#include <net/if.h>
5
6#include "mtk_vendor_nl80211.h"
7#include "mt76-vendor.h"
8#include "iwpriv_compat.h"
9#include "mwctl.h"
10
11static const char *progname;
12struct unl unl;
13
14int (*registered_handler)(struct nl_msg *, void *);
15void *registered_handler_data;
16
17void register_handler(int (*handler)(struct nl_msg *, void *), void *data)
18{
19 registered_handler = handler;
20 registered_handler_data = data;
21}
22
23int valid_handler(struct nl_msg *msg, void *arg)
24{
25 if (registered_handler)
26 return registered_handler(msg, registered_handler_data);
27
28 return NL_OK;
29}
30
31extern struct cmd *__start___cmd[];
32extern struct cmd *__stop___cmd;
33
34#define for_each_cmd(_cmd, i) \
35 for (i = 0; i < &__stop___cmd - __start___cmd; i++) \
36 if ((_cmd = __start___cmd[i]))
37
38void usage(void)
39{
40 static const char *const commands[] = {
41 "set csi ctrl=<opt1>,<opt2>,<opt3>,<opt4> (macaddr=<macaddr>)",
42 "set csi interval=<interval (us)>",
43 "dump csi <packet num> <filename>",
44
45 "set amnt <index>(0x0~0xf) <mac addr>(xx:xx:xx:xx:xx:xx)",
46 "dump amnt <index> (0x0~0xf or 0xff)",
47
48 "set ap_rfeatures he_gi=<val>",
49 "set ap_rfeatures he_ltf=<val>",
50 "set ap_rfeatures trig_type=<enable>,<val> (val: 0-7)",
51 "set ap_rfeatures ack_policy=<val> (val: 0-4)",
52 "set ap_wireless fixed_mcs=<val>",
53 "set ap_wireless ofdma=<val> (0: disable, 1: DL, 2: UL)",
54 "set ap_wireless nusers_ofdma=<val>",
55 "set ap_wireless ppdu_type=<val> (0: SU, 1: MU, 4: LEGACY)",
56 "set ap_wireless add_ba_req_bufsize=<val>",
57 "set ap_wireless mimo=<val> (0: DL, 1: UL)",
58 "set ap_wireless ampdu=<enable>",
59 "set ap_wireless amsdu=<enable>",
60 "set ap_wireless cert=<enable>",
61 };
62 int i;
63
64 fprintf(stderr, "Usage:\n");
65 for (i = 0; i < ARRAY_SIZE(commands); i++)
66 printf(" %s wlanX %s\n", progname, commands[i]);
67
68 exit(1);
69}
70SECTION(dump);
71
72int main(int argc, char **argv)
73{
74 int if_idx, ret = 0;
75 const struct cmd *cmd, *match = NULL, *sectcmd;
76 const char *command, *section;
77 enum command_identify_by command_idby = CIB_NONE;
78 int err, i;
79 struct nl_msg *msg;
80
81 progname = argv[0];
82
83 if_idx = if_nametoindex(argv[1]);
84 if (!if_idx) {
85 fprintf(stderr, "%s\n", strerror(errno));
86 return 2;
87 }
88
89 argc -= 2;
90 argv += 2;
91
92#if 0
93 if (!strncmp(cmd_str, "dump", 4)) {
94 if (!strncmp(subcmd, "csi", 3))
95 ret = mt76_csi_dump(if_idx, argc, argv);
96 else if (!strncmp(subcmd, "amnt", 4))
97 ret = mt76_amnt_dump(if_idx, argc, argv);
98 } else if (!strncmp(cmd_str, "set", 3)) {
99 if (!strncmp(subcmd, "csi", 3))
100 ret = mt76_csi_set(if_idx, argc, argv);
101 else if (!strncmp(subcmd, "amnt", 4))
102 ret = mt76_amnt_set(if_idx, argc, argv);
103 else if (!strncmp(subcmd, "ap_rfeatures", 12))
104 ret = mt76_ap_rfeatures_set(if_idx, argc, argv);
105 else if (!strncmp(subcmd, "ap_wireless", 11))
106 ret = mt76_ap_wireless_set(if_idx, argc, argv);
107 } else {
108 usage();
109 }
110#endif
111
112 command_idby = CIB_NETDEV;
113 section = *argv;
114 argc--;
115 argv++;
116
117 for_each_cmd(sectcmd, i) {
118 if (sectcmd->parent)
119 continue;
120 /* ok ... bit of a hack for the dupe 'info' section */
121 if (match && sectcmd->idby != command_idby)
122 continue;
123 if (strcmp(sectcmd->name, section) == 0)
124 match = sectcmd;
125 }
126
127 sectcmd = match;
128 match = NULL;
129 if (!sectcmd)
130 return 1;
131
132 if (argc > 0) {
133 command = *argv;
134
135 for_each_cmd(cmd, i) {
136 if (!cmd->handler)
137 continue;
138 if (cmd->parent != sectcmd)
139 continue;
140 /*
141 * ignore mismatch id by, but allow WDEV
142 * in place of NETDEV
143 */
144 if (cmd->idby != command_idby &&
145 !(cmd->idby == CIB_NETDEV &&
146 command_idby == CIB_WDEV))
147 continue;
148 if (strcmp(cmd->name, command))
149 continue;
150 if (argc > 1 && !cmd->args)
151 continue;
152 match = cmd;
153 break;
154 }
155
156 if (match) {
157 argc--;
158 argv++;
159 }
160 }
161
162
163 if (match)
164 cmd = match;
165 else {
166 /* Use the section itself, if possible. */
167 cmd = sectcmd;
168 if (argc && !cmd->args)
169 return 1;
170 if (cmd->idby != command_idby &&
171 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
172 return 1;
173 if (!cmd->handler)
174 return 1;
175 }
176
177 if (unl_genl_init(&unl, "nl80211") < 0) {
178 fprintf(stderr, "Failed to connect to nl80211\n");
179 return 2;
180 }
181
182 msg = unl_genl_msg(&unl, NL80211_CMD_VENDOR, false);
183
184 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_idx) ||
185 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) ||
186 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, cmd->cmd)) {
187 nlmsg_free(msg);
188 goto out;
189 }
190
191 err = cmd->handler(msg, argc, argv, (void*)&if_idx);
192 if (err) {
193 nlmsg_free(msg);
194 goto out;
195 }
196
197 ret = unl_genl_request(&unl, msg, valid_handler, NULL);
198 if (ret)
199 fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret));
200out:
201 unl_free(&unl);
202
203 return ret;
204}