lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-License-Identifier: GPL-2.0
 
/******************************************************************************
 *
 * Copyright (C) 2020 SeekWave Technology Co.,Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License 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 <linux/version.h>
#include <linux/uaccess.h>
#include <linux/ctype.h>
#include <linux/if_arp.h>
 
#include "skw_compat.h"
#include "skw_log.h"
#include "skw_dentry.h"
#include "skw_cfg80211.h"
#include "skw_iface.h"
#include "skw_rx.h"
 
#define SKW_LL_MASK 0xffff
 
#if defined(CONFIG_SWT6621S_LOG_ERROR)
#define SKW_LOG_LEVEL SKW_ERROR
#elif defined(CONFIG_SWT6621S_LOG_WARN)
#define SKW_LOG_LEVEL SKW_WARN
#elif defined(CONFIG_SWT6621S_LOG_INFO)
#define SKW_LOG_LEVEL SKW_INFO
#elif defined(CONFIG_SWT6621S_LOG_DEBUG)
#define SKW_LOG_LEVEL SKW_DEBUG
#elif defined(CONFIG_SWT6621S_LOG_DETAIL)
#define SKW_LOG_LEVEL SKW_DETAIL
#else
#define SKW_LOG_LEVEL SKW_INFO
#endif
 
static unsigned long skw_dbg_level;
 
unsigned long skw_log_level(void)
{
   return skw_dbg_level;
}
 
static void skw_set_log_level(int level)
{
   unsigned long dbg_level;
 
   dbg_level = skw_log_level() & (~SKW_LL_MASK);
   dbg_level |= ((level << 1) - 1);
 
   xchg(&skw_dbg_level, dbg_level);
}
 
static void skw_enable_func_log(int func, bool enable)
{
   unsigned long dbg_level = skw_log_level();
 
   if (enable)
       dbg_level |= func;
   else
       dbg_level &= (~func);
 
   xchg(&skw_dbg_level, dbg_level);
}
 
 
static struct skw_monitor_dbg_iface *g_skw_dbg_iface;
static DEFINE_MUTEX(skw_dbg_mutex);
 
void skw_dump_frame(u8 *frame, u16 frame_len)
{
   struct skw_radiotap_desc *radio_desc;
   struct sk_buff *skb;
 
   mutex_lock(&skw_dbg_mutex);
   if (g_skw_dbg_iface == NULL) {
       mutex_unlock(&skw_dbg_mutex);
       return;
   }
 
   skb = alloc_skb(frame_len + sizeof(struct skw_radiotap_desc), GFP_KERNEL);
   if (skb == NULL) {
       mutex_unlock(&skw_dbg_mutex);
       return;
   }
 
   radio_desc = (struct skw_radiotap_desc *)skb_put(skb,
                               sizeof(struct skw_radiotap_desc));
   radio_desc->radiotap_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
   radio_desc->radiotap_hdr.it_pad = 0;
   radio_desc->radiotap_hdr.it_len = sizeof(struct skw_radiotap_desc);
   radio_desc->radiotap_hdr.it_present = BIT(IEEE80211_RADIOTAP_FLAGS);
   radio_desc->radiotap_flag = 0;
 
   skw_put_skb_data(skb, frame, frame_len);
 
   __skb_trim(skb, frame_len + radio_desc->radiotap_hdr.it_len);
   skb_reset_mac_header(skb);
 
   skb->dev = g_skw_dbg_iface->ndev;
   skb->ip_summed = CHECKSUM_NONE;
   skb->pkt_type = PACKET_OTHERHOST;
   skb->protocol = htons(ETH_P_80211_RAW);
 
   #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
       netif_rx_ni(skb);
   #else
       netif_rx(skb);
   #endif
 
   mutex_unlock(&skw_dbg_mutex);
}
 
static netdev_tx_t skw_monitor_xmit(struct sk_buff *skb, struct net_device *ndev)
{
   dev_kfree_skb_any(skb);
   return NETDEV_TX_OK;
}
 
static const struct net_device_ops skw_monitor_netdev_ops = {
   .ndo_start_xmit = skw_monitor_xmit,
};
 
static void skw_add_dbg_iface(void)
{
   int priv_size, ret;
   struct net_device *ndev = NULL;
   struct skw_monitor_dbg_iface *iface;
   u8 mac[6] = {0xFe, 0xfd, 0xfc, 0x12, 0x11, 0x88};
 
   if (g_skw_dbg_iface) {
       skw_err("already add dbg iface\n");
       return;
   }
 
   priv_size = sizeof(struct skw_monitor_dbg_iface);
   ndev = alloc_netdev_mqs(priv_size, "skw_dbg",
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
                   NET_NAME_ENUM,
#endif
                   ether_setup, SKW_WMM_AC_MAX, 1);
   if (!ndev) {
       skw_err("alloc ndev fail\n");
       return;
   }
 
   iface = netdev_priv(ndev);
   iface->ndev = ndev;
 
   skw_ether_copy(iface->addr, mac);
   ndev->type = ARPHRD_IEEE80211_RADIOTAP;
 
   ndev->netdev_ops = &skw_monitor_netdev_ops;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
   eth_hw_addr_set(ndev, mac);
#else
   skw_ether_copy(ndev->dev_addr, mac);
#endif
   rtnl_lock();
   ret = skw_register_netdevice(ndev);
   rtnl_unlock();
   if (ret) {
       skw_err("register netdev failed\n");
       goto free_iface;
   }
 
   mutex_lock(&skw_dbg_mutex);
   g_skw_dbg_iface = iface;
   mutex_unlock(&skw_dbg_mutex);
 
   return;
 
free_iface:
   if (ndev)
       free_netdev(ndev);
}
 
void skw_del_dbg_iface(void)
{
   struct skw_monitor_dbg_iface *iface;
 
   if (!g_skw_dbg_iface)
       return;
 
   mutex_lock(&skw_dbg_mutex);
 
   iface = g_skw_dbg_iface;
   g_skw_dbg_iface = NULL;
 
   mutex_unlock(&skw_dbg_mutex);
 
   rtnl_lock();
   skw_unregister_netdevice(iface->ndev);
   rtnl_unlock();
}
 
static void skw_set_dump_frame(bool enable)
{
   if (enable)
       skw_add_dbg_iface();
   else
       skw_del_dbg_iface();
}
 
static char *skw_dump_frame_status(void)
{
   if (g_skw_dbg_iface)
       return "enable";
   else
       return "disable";
}
 
static int skw_log_show(struct seq_file *seq, void *data)
{
   int i;
   u32 level = skw_log_level();
   const char *log_level;
   static const char *log_name[] = {"NONE", "ERROR", "WARN", "INFO", "DEBUG", "DETAIL"};
 
   i = ffs((level & SKW_LL_MASK) + 1) - 1;
   if (i >= 0 && i < ARRAY_SIZE(log_name))
       log_level = log_name[i];
   else
       log_level = "INVALID";
 
   seq_puts(seq, "\n");
   seq_printf(seq, "Log Level: %s    [ERROR|WARN|INFO|DEBUG|DETAIL]\n", log_name[i]);
 
#define SKW_LOG_STATUS(s) (level & (s) ? "enable" : "disable")
   seq_puts(seq, "\n");
   seq_printf(seq, "command log: %s\n", SKW_LOG_STATUS(SKW_CMD));
   seq_printf(seq, "event   log: %s\n", SKW_LOG_STATUS(SKW_EVENT));
   seq_printf(seq, "dump    log: %s\n", SKW_LOG_STATUS(SKW_DUMP));
   seq_printf(seq, "scan    log: %s\n", SKW_LOG_STATUS(SKW_SCAN));
   seq_printf(seq, "timer   log: %s\n", SKW_LOG_STATUS(SKW_TIMER));
   seq_printf(seq, "state   log: %s\n", SKW_LOG_STATUS(SKW_STATE));
   seq_printf(seq, "work    log: %s\n", SKW_LOG_STATUS(SKW_WORK));
   seq_printf(seq, "dump  frame: %s\n", skw_dump_frame_status());
   seq_printf(seq, "DFS     log: %s\n", SKW_LOG_STATUS(SKW_DFS));
#undef SKW_LOG_STATUS
 
   return 0;
}
 
static int skw_log_open(struct inode *inode, struct file *file)
{
   // return single_open(file, &skw_log_show, inode->i_private);
   return single_open(file, &skw_log_show, skw_pde_data(inode));
}
 
static int skw_log_control(const char *cmd, bool enable)
{
   if (!strcmp("command", cmd))
       skw_enable_func_log(SKW_CMD, enable);
   else if (!strcmp("event", cmd))
       skw_enable_func_log(SKW_EVENT, enable);
   else if (!strcmp("dump", cmd))
       skw_enable_func_log(SKW_DUMP, enable);
   else if (!strcmp("scan", cmd))
       skw_enable_func_log(SKW_SCAN, enable);
   else if (!strcmp("timer", cmd))
       skw_enable_func_log(SKW_TIMER, enable);
   else if (!strcmp("state", cmd))
       skw_enable_func_log(SKW_STATE, enable);
   else if (!strcmp("work", cmd))
       skw_enable_func_log(SKW_WORK, enable);
   else if (!strcmp("dfs", cmd))
       skw_enable_func_log(SKW_DFS, enable);
   else if (!strcmp("detail", cmd))
       skw_set_log_level(SKW_DETAIL);
   else if (!strcmp("debug", cmd))
       skw_set_log_level(SKW_DEBUG);
   else if (!strcmp("info", cmd))
       skw_set_log_level(SKW_INFO);
   else if (!strcmp("warn", cmd))
       skw_set_log_level(SKW_WARN);
   else if (!strcmp("error", cmd))
       skw_set_log_level(SKW_ERROR);
   else if (!strcmp("dumpframe", cmd))
       skw_set_dump_frame(enable);
   else
       return -EINVAL;
 
   return 0;
}
 
static ssize_t skw_log_write(struct file *fp, const char __user *buffer,
               size_t len, loff_t *offset)
{
   int i, idx;
   char cmd[32];
   bool enable = false;
 
   for (idx = 0, i = 0; i < len; i++) {
       char c;
 
       if (get_user(c, buffer))
           return -EFAULT;
 
       switch (c) {
       case ' ':
           break;
 
       case ':':
           cmd[idx] = 0;
           if (!strcmp("enable", cmd))
               enable = true;
           else
               enable = false;
 
           idx = 0;
           break;
 
       case '|':
       case '\0':
       case '\n':
           cmd[idx] = 0;
           skw_log_control(cmd, enable);
           idx = 0;
           break;
 
       default:
           cmd[idx++] = tolower(c);
           idx %= 32;
 
           break;
       }
 
       buffer++;
   }
 
   return len;
}
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
static const struct proc_ops skw_log_fops = {
   .proc_open = skw_log_open,
   .proc_read = seq_read,
   .proc_release = single_release,
   .proc_write = skw_log_write,
};
#else
static const struct file_operations skw_log_fops = {
   .owner = THIS_MODULE,
   .open = skw_log_open,
   .read = seq_read,
   .release = single_release,
   .write = skw_log_write,
};
#endif
 
void skw_log_level_init(void)
{
   skw_set_log_level(SKW_LOG_LEVEL);
 
   skw_enable_func_log(SKW_CMD, false);
   skw_enable_func_log(SKW_EVENT, false);
   skw_enable_func_log(SKW_DUMP, false);
   skw_enable_func_log(SKW_SCAN, false);
   skw_enable_func_log(SKW_TIMER, false);
   skw_enable_func_log(SKW_STATE, true);
   skw_enable_func_log(SKW_WORK, false);
   skw_enable_func_log(SKW_DFS, false);
 
   skw_procfs_file(NULL, "log_level", 0666, &skw_log_fops, NULL);
}
 
void skw_log_level_deinit(void)
{
}