blob: ca7732e7bab56d84181c431476d5f0cf1527fc9b [file] [log] [blame]
developer175704f2021-06-22 17:33:53 +08001/* Copyright (C) 2021 Mediatek Inc. */
2#define _GNU_SOURCE
3
4#include "mt76-vendor.h"
5
6struct csi_data *csi;
7int csi_idx;
8
9static struct nla_policy csi_ctrl_policy[NUM_MTK_VENDOR_ATTRS_CSI_CTRL] = {
10 [MTK_VENDOR_ATTR_CSI_CTRL_CFG] = { .type = NLA_NESTED },
11 [MTK_VENDOR_ATTR_CSI_CTRL_CFG_MODE] = { .type = NLA_U8 },
12 [MTK_VENDOR_ATTR_CSI_CTRL_CFG_TYPE] = { .type = NLA_U8 },
13 [MTK_VENDOR_ATTR_CSI_CTRL_CFG_VAL1] = { .type = NLA_U8 },
14 [MTK_VENDOR_ATTR_CSI_CTRL_CFG_VAL2] = { .type = NLA_U8 },
15 [MTK_VENDOR_ATTR_CSI_CTRL_MAC_ADDR] = { .type = NLA_NESTED },
16 [MTK_VENDOR_ATTR_CSI_CTRL_INTERVAL] = { .type = NLA_U32 },
developer05642a42023-11-28 20:15:39 +080017 [MTK_VENDOR_ATTR_CSI_CTRL_STA_INTERVAL] = { .type = NLA_U32 },
developer175704f2021-06-22 17:33:53 +080018 [MTK_VENDOR_ATTR_CSI_CTRL_DUMP_NUM] = { .type = NLA_U16 },
19 [MTK_VENDOR_ATTR_CSI_CTRL_DATA] = { .type = NLA_NESTED },
20};
21
22static struct nla_policy csi_data_policy[NUM_MTK_VENDOR_ATTRS_CSI_DATA] = {
23 [MTK_VENDOR_ATTR_CSI_DATA_VER] = { .type = NLA_U8 },
24 [MTK_VENDOR_ATTR_CSI_DATA_TS] = { .type = NLA_U32 },
25 [MTK_VENDOR_ATTR_CSI_DATA_RSSI] = { .type = NLA_U8 },
26 [MTK_VENDOR_ATTR_CSI_DATA_SNR] = { .type = NLA_U8 },
27 [MTK_VENDOR_ATTR_CSI_DATA_BW] = { .type = NLA_U8 },
28 [MTK_VENDOR_ATTR_CSI_DATA_CH_IDX] = { .type = NLA_U8 },
29 [MTK_VENDOR_ATTR_CSI_DATA_TA] = { .type = NLA_NESTED },
developer05642a42023-11-28 20:15:39 +080030 [MTK_VENDOR_ATTR_CSI_DATA_NUM] = { .type = NLA_U32 },
developer175704f2021-06-22 17:33:53 +080031 [MTK_VENDOR_ATTR_CSI_DATA_I] = { .type = NLA_NESTED },
32 [MTK_VENDOR_ATTR_CSI_DATA_Q] = { .type = NLA_NESTED },
33 [MTK_VENDOR_ATTR_CSI_DATA_INFO] = { .type = NLA_U32 },
34 [MTK_VENDOR_ATTR_CSI_DATA_TX_ANT] = { .type = NLA_U8 },
35 [MTK_VENDOR_ATTR_CSI_DATA_RX_ANT] = { .type = NLA_U8 },
36 [MTK_VENDOR_ATTR_CSI_DATA_MODE] = { .type = NLA_U8 },
developer05642a42023-11-28 20:15:39 +080037 [MTK_VENDOR_ATTR_CSI_DATA_CHAIN_INFO] = { .type = NLA_U32 },
developer175704f2021-06-22 17:33:53 +080038};
39
40static int mt76_csi_dump_cb(struct nl_msg *msg, void *arg)
41{
42 struct nlattr *tb[NUM_MTK_VENDOR_ATTRS_CSI_CTRL];
43 struct nlattr *tb_data[NUM_MTK_VENDOR_ATTRS_CSI_DATA];
44 struct nlattr *attr;
45 struct nlattr *cur;
developer0497a7b2021-10-04 10:00:27 +080046 size_t idx;
47 int rem;
developer175704f2021-06-22 17:33:53 +080048 struct csi_data *c = &csi[csi_idx];
49
50 attr = unl_find_attr(&unl, msg, NL80211_ATTR_VENDOR_DATA);
51 if (!attr) {
52 fprintf(stderr, "Testdata attribute not found\n");
53 return NL_SKIP;
54 }
55
56 nla_parse_nested(tb, MTK_VENDOR_ATTR_CSI_CTRL_MAX,
57 attr, csi_ctrl_policy);
58
59 if (!tb[MTK_VENDOR_ATTR_CSI_CTRL_DATA])
60 return NL_SKIP;
61
62 nla_parse_nested(tb_data, MTK_VENDOR_ATTR_CSI_DATA_MAX,
63 tb[MTK_VENDOR_ATTR_CSI_CTRL_DATA], csi_data_policy);
64
65 if (!(tb_data[MTK_VENDOR_ATTR_CSI_DATA_VER] &&
66 tb_data[MTK_VENDOR_ATTR_CSI_DATA_TS] &&
67 tb_data[MTK_VENDOR_ATTR_CSI_DATA_RSSI] &&
68 tb_data[MTK_VENDOR_ATTR_CSI_DATA_SNR] &&
69 tb_data[MTK_VENDOR_ATTR_CSI_DATA_BW] &&
70 tb_data[MTK_VENDOR_ATTR_CSI_DATA_CH_IDX] &&
71 tb_data[MTK_VENDOR_ATTR_CSI_DATA_TA] &&
72 tb_data[MTK_VENDOR_ATTR_CSI_DATA_I] &&
73 tb_data[MTK_VENDOR_ATTR_CSI_DATA_Q] &&
74 tb_data[MTK_VENDOR_ATTR_CSI_DATA_INFO] &&
75 tb_data[MTK_VENDOR_ATTR_CSI_DATA_MODE] &&
developer05642a42023-11-28 20:15:39 +080076 tb_data[MTK_VENDOR_ATTR_CSI_DATA_CHAIN_INFO])) {
developer175704f2021-06-22 17:33:53 +080077 fprintf(stderr, "Attributes error for CSI data\n");
78 return NL_SKIP;
79 }
80
81 c->rssi = nla_get_u8(tb_data[MTK_VENDOR_ATTR_CSI_DATA_RSSI]);
82 c->snr = nla_get_u8(tb_data[MTK_VENDOR_ATTR_CSI_DATA_SNR]);
83 c->data_bw = nla_get_u8(tb_data[MTK_VENDOR_ATTR_CSI_DATA_BW]);
84 c->pri_ch_idx = nla_get_u8(tb_data[MTK_VENDOR_ATTR_CSI_DATA_CH_IDX]);
85 c->rx_mode = nla_get_u8(tb_data[MTK_VENDOR_ATTR_CSI_DATA_MODE]);
86
87 c->tx_idx = nla_get_u16(tb_data[MTK_VENDOR_ATTR_CSI_DATA_TX_ANT]);
88 c->rx_idx = nla_get_u16(tb_data[MTK_VENDOR_ATTR_CSI_DATA_RX_ANT]);
89
developer05642a42023-11-28 20:15:39 +080090 c->ext_info = nla_get_u32(tb_data[MTK_VENDOR_ATTR_CSI_DATA_INFO]);
91 c->chain_info = nla_get_u32(tb_data[MTK_VENDOR_ATTR_CSI_DATA_CHAIN_INFO]);
developer175704f2021-06-22 17:33:53 +080092
93 c->ts = nla_get_u32(tb_data[MTK_VENDOR_ATTR_CSI_DATA_TS]);
94
developer05642a42023-11-28 20:15:39 +080095 c->data_num = nla_get_u32(tb_data[MTK_VENDOR_ATTR_CSI_DATA_NUM]);
96
developer175704f2021-06-22 17:33:53 +080097 idx = 0;
98 nla_for_each_nested(cur, tb_data[MTK_VENDOR_ATTR_CSI_DATA_TA], rem) {
developer0497a7b2021-10-04 10:00:27 +080099 if (idx < ETH_ALEN)
100 c->ta[idx++] = nla_get_u8(cur);
developer175704f2021-06-22 17:33:53 +0800101 }
102
103 idx = 0;
104 nla_for_each_nested(cur, tb_data[MTK_VENDOR_ATTR_CSI_DATA_I], rem) {
developer05642a42023-11-28 20:15:39 +0800105 if (idx < c->data_num)
developer0497a7b2021-10-04 10:00:27 +0800106 c->data_i[idx++] = nla_get_u16(cur);
developer175704f2021-06-22 17:33:53 +0800107 }
108
109 idx = 0;
110 nla_for_each_nested(cur, tb_data[MTK_VENDOR_ATTR_CSI_DATA_Q], rem) {
developer05642a42023-11-28 20:15:39 +0800111 if (idx < c->data_num)
developer0497a7b2021-10-04 10:00:27 +0800112 c->data_q[idx++] = nla_get_u16(cur);
developer175704f2021-06-22 17:33:53 +0800113 }
114
115 csi_idx++;
116
117 return NL_SKIP;
118}
119
120static int mt76_csi_to_json(const char *name)
121{
122#define MAX_BUF_SIZE 6000
123 FILE *f;
developer0497a7b2021-10-04 10:00:27 +0800124 int i, ret = -ENOMEM;
developer175704f2021-06-22 17:33:53 +0800125
126 f = fopen(name, "a+");
127 if (!f) {
128 printf("open failure");
129 return 1;
130 }
131
developer0497a7b2021-10-04 10:00:27 +0800132 if (fwrite("[", 1, 1, f) != 1) {
133 perror("fwrite");
134 goto out;
135 }
developer175704f2021-06-22 17:33:53 +0800136
137 for (i = 0; i < csi_idx; i++) {
developer175704f2021-06-22 17:33:53 +0800138 struct csi_data *c = &csi[i];
developer0497a7b2021-10-04 10:00:27 +0800139 char *pos, *buf;
developer175704f2021-06-22 17:33:53 +0800140 int j;
141
developer0497a7b2021-10-04 10:00:27 +0800142 buf = malloc(MAX_BUF_SIZE);
143 if (!buf)
144 goto out;
developer175704f2021-06-22 17:33:53 +0800145
developer0497a7b2021-10-04 10:00:27 +0800146 pos = buf;
developer175704f2021-06-22 17:33:53 +0800147 pos += snprintf(pos, MAX_BUF_SIZE, "%c", '[');
148
developer6f8cffd2021-09-29 19:48:25 +0800149 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->ts);
developer175704f2021-06-22 17:33:53 +0800150 pos += snprintf(pos, MAX_BUF_SIZE, "\"%02x%02x%02x%02x%02x%02x\",", c->ta[0], c->ta[1], c->ta[2], c->ta[3], c->ta[4], c->ta[5]);
151
152 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->rssi);
153 pos += snprintf(pos, MAX_BUF_SIZE, "%u,", c->snr);
154 pos += snprintf(pos, MAX_BUF_SIZE, "%u,", c->data_bw);
155 pos += snprintf(pos, MAX_BUF_SIZE, "%u,", c->pri_ch_idx);
156 pos += snprintf(pos, MAX_BUF_SIZE, "%u,", c->rx_mode);
157 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->tx_idx);
158 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->rx_idx);
developer05642a42023-11-28 20:15:39 +0800159 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->chain_info);
160 pos += snprintf(pos, MAX_BUF_SIZE, "%d,", c->ext_info);
developer175704f2021-06-22 17:33:53 +0800161
162 pos += snprintf(pos, MAX_BUF_SIZE, "%c", '[');
developer05642a42023-11-28 20:15:39 +0800163 for (j = 0; j < c->data_num; j++) {
developer175704f2021-06-22 17:33:53 +0800164 pos += snprintf(pos, MAX_BUF_SIZE, "%d", c->data_i[j]);
developer05642a42023-11-28 20:15:39 +0800165 if (j != (c->data_num - 1))
developer175704f2021-06-22 17:33:53 +0800166 pos += snprintf(pos, MAX_BUF_SIZE, ",");
167 }
168 pos += snprintf(pos, MAX_BUF_SIZE, "%c,", ']');
169
170 pos += snprintf(pos, MAX_BUF_SIZE, "%c", '[');
developer05642a42023-11-28 20:15:39 +0800171 for (j = 0; j < c->data_num; j++) {
developer175704f2021-06-22 17:33:53 +0800172 pos += snprintf(pos, MAX_BUF_SIZE, "%d", c->data_q[j]);
developer05642a42023-11-28 20:15:39 +0800173 if (j != (c->data_num - 1))
developer175704f2021-06-22 17:33:53 +0800174 pos += snprintf(pos, MAX_BUF_SIZE, ",");
175 }
176 pos += snprintf(pos, MAX_BUF_SIZE, "%c", ']');
177
178 pos += snprintf(pos, MAX_BUF_SIZE, "%c", ']');
179 if (i != csi_idx - 1)
180 pos += snprintf(pos, MAX_BUF_SIZE, ",");
181
developer0497a7b2021-10-04 10:00:27 +0800182 if (fwrite(buf, 1, pos - buf, f) != (pos - buf)) {
183 perror("fwrite");
184 free(buf);
185 goto out;
186 }
187
developer175704f2021-06-22 17:33:53 +0800188 free(buf);
189 }
190
developer0497a7b2021-10-04 10:00:27 +0800191 if (fwrite("]", 1, 1, f) != 1) {
192 perror("fwrite");
193 goto out;
194 }
developer6f8cffd2021-09-29 19:48:25 +0800195
developer0497a7b2021-10-04 10:00:27 +0800196 ret = 0;
197out:
198 if (fclose(f))
199 perror("fclose");
200
201 return ret;
developer175704f2021-06-22 17:33:53 +0800202}
203
204int mt76_csi_dump(int idx, int argc, char **argv)
205{
developer6f8cffd2021-09-29 19:48:25 +0800206 int pkt_num, ret = 0, i;
developer175704f2021-06-22 17:33:53 +0800207 struct nl_msg *msg;
208 void *data;
209
210 if (argc < 2)
211 return 1;
developer0497a7b2021-10-04 10:00:27 +0800212
developer175704f2021-06-22 17:33:53 +0800213 pkt_num = strtol(argv[0], NULL, 10);
developer0497a7b2021-10-04 10:00:27 +0800214 if (pkt_num < 0 || pkt_num > 30000)
215 return -EINVAL;
developer175704f2021-06-22 17:33:53 +0800216
217#define CSI_DUMP_PER_NUM 3
218 csi_idx = 0;
219 csi = (struct csi_data *)calloc(pkt_num, sizeof(*csi));
220
221 for (i = 0; i < pkt_num / CSI_DUMP_PER_NUM; i++) {
222 if (unl_genl_init(&unl, "nl80211") < 0) {
223 fprintf(stderr, "Failed to connect to nl80211\n");
224 return 2;
225 }
226
227 msg = unl_genl_msg(&unl, NL80211_CMD_VENDOR, true);
228
229 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, idx) ||
230 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) ||
231 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, MTK_NL80211_VENDOR_SUBCMD_CSI_CTRL))
232 return false;
233
234 data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA | NLA_F_NESTED);
developer0497a7b2021-10-04 10:00:27 +0800235 if (!data)
236 return -ENOMEM;
developer175704f2021-06-22 17:33:53 +0800237
238 if (nla_put_u16(msg, MTK_VENDOR_ATTR_CSI_CTRL_DUMP_NUM, CSI_DUMP_PER_NUM))
239 return false;
240
241 nla_nest_end(msg, data);
242
243 ret = unl_genl_request(&unl, msg, mt76_csi_dump_cb, NULL);
244 if (ret)
245 fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret));
246
247 unl_free(&unl);
developer05642a42023-11-28 20:15:39 +0800248
developer175704f2021-06-22 17:33:53 +0800249 }
250
251 mt76_csi_to_json(argv[1]);
252 free(csi);
253
254 return ret;
255}
256
257static int mt76_csi_set_attr(struct nl_msg *msg, int argc, char **argv)
258{
259 int idx = MTK_VENDOR_ATTR_CSI_CTRL_CFG_MODE;
260 char *val, *s1, *s2, *cur;
261 void *data;
262
263 val = strchr(argv[0], '=');
developer6f8cffd2021-09-29 19:48:25 +0800264 if (!val)
265 return -EINVAL;
266
267 *(val++) = 0;
developer175704f2021-06-22 17:33:53 +0800268
269 if (!strncmp(argv[0], "ctrl", 4)) {
developer175704f2021-06-22 17:33:53 +0800270 data = nla_nest_start(msg, MTK_VENDOR_ATTR_CSI_CTRL_CFG | NLA_F_NESTED);
developer0497a7b2021-10-04 10:00:27 +0800271 if (!data)
272 return -ENOMEM;
developer175704f2021-06-22 17:33:53 +0800273
developer0497a7b2021-10-04 10:00:27 +0800274 s1 = s2 = strdup(val);
275
276 while ((cur = strsep(&s1, ",")) != NULL) {
277 u8 param = strtoul(cur, NULL, 0);
278
279 nla_put_u8(msg, idx++, param);
280 }
developer175704f2021-06-22 17:33:53 +0800281
282 nla_nest_end(msg, data);
283
284 free(s2);
285
developer05642a42023-11-28 20:15:39 +0800286 if ((argc == 2 || argc == 3) &&
developer175704f2021-06-22 17:33:53 +0800287 !strncmp(argv[1], "mac_addr", strlen("mac_addr"))) {
288 u8 a[ETH_ALEN];
289 int matches, i;
290
291 val = strchr(argv[1], '=');
developer0497a7b2021-10-04 10:00:27 +0800292 if (!val)
293 return -EINVAL;
developer175704f2021-06-22 17:33:53 +0800294
developer0497a7b2021-10-04 10:00:27 +0800295 *(val++) = 0;
developer175704f2021-06-22 17:33:53 +0800296 matches = sscanf(val, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
297 a, a+1, a+2, a+3, a+4, a+5);
298
299 if (matches != ETH_ALEN)
300 return -EINVAL;
301
302 data = nla_nest_start(msg, MTK_VENDOR_ATTR_CSI_CTRL_MAC_ADDR | NLA_F_NESTED);
developer0497a7b2021-10-04 10:00:27 +0800303 if (!data)
304 return -ENOMEM;
305
developer175704f2021-06-22 17:33:53 +0800306 for (i = 0; i < ETH_ALEN; i++)
307 nla_put_u8(msg, i, a[i]);
308
309 nla_nest_end(msg, data);
310 }
developer05642a42023-11-28 20:15:39 +0800311
312 if (argc == 3 &&
313 !strncmp(argv[2], "sta_interval", strlen("sta_interval"))) {
314 u32 sta_interval = 0;
315
316 val = strchr(argv[2], '=');
317
318 *(val++) = 0;
319
320 if (!val)
321 return -EINVAL;
322
323 sta_interval = strtoul(val, NULL, 0);
324
325 nla_put_u32(msg, MTK_VENDOR_ATTR_CSI_CTRL_STA_INTERVAL, sta_interval);
326
327 }
328
developer175704f2021-06-22 17:33:53 +0800329 } else if (!strncmp(argv[0], "interval", 8)) {
developer0497a7b2021-10-04 10:00:27 +0800330 u32 interval = strtoul(val, NULL, 0);
331
332 nla_put_u32(msg, MTK_VENDOR_ATTR_CSI_CTRL_INTERVAL, interval);
developer175704f2021-06-22 17:33:53 +0800333 }
334
335 return 0;
336}
337
338int mt76_csi_set(int idx, int argc, char **argv)
339{
340 struct nl_msg *msg;
341 void *data;
342 int ret;
343
344 if (argc < 1)
345 return 1;
346
347 if (unl_genl_init(&unl, "nl80211") < 0) {
348 fprintf(stderr, "Failed to connect to nl80211\n");
349 return 2;
350 }
351
352 msg = unl_genl_msg(&unl, NL80211_CMD_VENDOR, false);
353
354 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, idx) ||
355 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) ||
356 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, MTK_NL80211_VENDOR_SUBCMD_CSI_CTRL))
357 return false;
358
359 data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA | NLA_F_NESTED);
developer0497a7b2021-10-04 10:00:27 +0800360 if (!data)
361 return -ENOMEM;
developer175704f2021-06-22 17:33:53 +0800362
363 mt76_csi_set_attr(msg, argc, argv);
364
365 nla_nest_end(msg, data);
366
367 ret = unl_genl_request(&unl, msg, NULL, NULL);
368 if (ret)
369 fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret));
370
371 unl_free(&unl);
372
373 return ret;
374}