hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
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
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/completion.h>
#include <linux/workqueue.h>
#include <linux/timer.h>
 
#include "wcn_glb.h"
#include "wcn_procfs.h"
 
#define LOOPCHECK_TIMER_INTERVAL      5
#define WCN_LOOPCHECK_INIT    1
#define WCN_LOOPCHECK_OPEN    2
 
#ifdef CONFIG_WCN_LOOPCHECK
struct wcn_loopcheck {
   unsigned long status;
   struct completion completion;
   struct delayed_work work;
   struct workqueue_struct *workqueue;
};
 
static struct wcn_loopcheck loopcheck;
#endif
static struct completion atcmd_completion;
static struct mutex atcmd_lock;
 
int at_cmd_send(char *buf, unsigned int len)
{
   unsigned char *send_buf = NULL;
   struct mbuf_t *head, *tail;
   int num = 1;
 
   WCN_DEBUG("%s len=%d\n", __func__, len);
   if (unlikely(marlin_get_module_status() != true)) {
       WCN_ERR("WCN module have not open\n");
       return -EIO;
   }
 
   send_buf = kzalloc(len + PUB_HEAD_RSV + 1, GFP_KERNEL);
   if (!send_buf)
       return -ENOMEM;
   memcpy(send_buf + PUB_HEAD_RSV, buf, len);
 
   if (!sprdwcn_bus_list_alloc(mdbg_proc_ops[MDBG_AT_TX_OPS].channel,
                   &head, &tail, &num)) {
       head->buf = send_buf;
       head->len = len;
       head->next = NULL;
       sprdwcn_bus_push_list(mdbg_proc_ops[MDBG_AT_TX_OPS].channel,
                     head, tail, num);
   }
   return 0;
}
 
#ifdef CONFIG_WCN_LOOPCHECK
 
static void loopcheck_work_queue(struct work_struct *work)
{
   int ret;
   unsigned long timeleft;
   char a[] = "at+loopcheck\r\n";
 
   if (!test_bit(WCN_LOOPCHECK_OPEN, &loopcheck.status))
       return;
   mutex_lock(&atcmd_lock);
   at_cmd_send(a, sizeof(a));
 
   timeleft = wait_for_completion_timeout(&loopcheck.completion,
                          (3 * HZ));
   mutex_unlock(&atcmd_lock);
   if (!test_bit(WCN_LOOPCHECK_OPEN, &loopcheck.status))
       return;
   if (!timeleft) {
       stop_loopcheck();
       WCN_ERR("didn't get loopcheck ack\n");
       WCN_INFO("start dump CP2 mem\n");
       mdbg_assert_interface("loopcheck fail");
       return;
   }
 
   ret = queue_delayed_work(loopcheck.workqueue, &loopcheck.work,
                LOOPCHECK_TIMER_INTERVAL * HZ);
}
#endif
 
void switch_cp2_log(bool flag)
{
   unsigned long timeleft;
   char a[32];
   unsigned char ret;
 
   WCN_INFO("%s - %s entry!\n", __func__, (flag ? "open" : "close"));
   mutex_lock(&atcmd_lock);
   sprintf(a, "at+armlog=%d\r\n", (flag ? 1 : 0));
   ret = at_cmd_send(a, sizeof(a));
   if (ret) {
       mutex_unlock(&atcmd_lock);
       WCN_ERR("%s fail!\n", __func__);
       return;
   }
   timeleft = wait_for_completion_timeout(&atcmd_completion, (3 * HZ));
   mutex_unlock(&atcmd_lock);
   if (!timeleft)
       WCN_ERR("didn't get %s ack\n", __func__);
}
 
int get_board_ant_num(void)
{
   unsigned long timeleft;
   char a[] = "at+getantnum\r\n";
   unsigned char *at_cmd_buf;
   unsigned char ret;
 
   WCN_DEBUG("%s entry!\n", __func__);
 
   /* 1. uwe5621 module on RK board(rk3368):
    * Antenna num is fixed to one.
    */
#ifdef CONFIG_RK_BOARD
#ifndef CONFIG_CHECK_DRIVER_BY_CHIPID
#ifdef CONFIG_UWE5621
   WCN_INFO("%s [one_ant]\n", __func__);
   return MARLIN_ONE_ANT;
#endif
#else /*CONFIG_CHECK_DRIVER_BY_CHIPID*/
   if (wcn_get_chip_model() == WCN_CHIP_MARLIN3) {
       WCN_INFO("%s [one_ant]\n", __func__);
       return MARLIN_ONE_ANT;
   }
#endif
#endif /* CONFIG_RK_BOARD */
 
   /* 2. uwe5622 module:
    * Antenna num is fixed to one.
    */
#ifndef CONFIG_CHECK_DRIVER_BY_CHIPID
#ifdef CONFIG_UWE5622
   WCN_INFO("%s [one_ant]\n", __func__);
   return MARLIN_ONE_ANT;
#endif
#else /*CONFIG_CHECK_DRIVER_BY_CHIPID*/
   if (wcn_get_chip_model() == WCN_CHIP_MARLIN3L) {
       WCN_INFO("%s [one_ant]\n", __func__);
       return MARLIN_ONE_ANT;
   }
#endif
 
   /* 3. Other situations:
    * send at cmd to get antenna num.
    */
   mutex_lock(&atcmd_lock);
   ret = at_cmd_send(a, sizeof(a));
   if (ret) {
       mutex_unlock(&atcmd_lock);
       WCN_ERR("%s fail!\n", __func__);
       return ret;
   }
   timeleft = wait_for_completion_timeout(&atcmd_completion, (3 * HZ));
   if (!timeleft) {
       mutex_unlock(&atcmd_lock);
       WCN_ERR("didn't get board ant num, default:[three_ant]\n");
       return MARLIN_THREE_ANT;
   }
   at_cmd_buf = mdbg_get_at_cmd_buf();
   mutex_unlock(&atcmd_lock);
   if (at_cmd_buf[0] == '2') {
       WCN_INFO("%s [two_ant]\n", __func__);
       return MARLIN_TWO_ANT;
   } else if (at_cmd_buf[0] == '3') {
       WCN_INFO("%s [three_ant]\n", __func__);
       return MARLIN_THREE_ANT;
   }
   WCN_ERR("%s read err:%s, default:[three_ant]\n",
       __func__, at_cmd_buf);
   return MARLIN_THREE_ANT;
}
 
void get_cp2_version(void)
{
   unsigned long timeleft;
   char a[] = "at+spatgetcp2info\r\n";
   unsigned char ret;
 
   WCN_INFO("%s entry!\n", __func__);
   mutex_lock(&atcmd_lock);
   ret = at_cmd_send(a, sizeof(a));
   if (ret) {
       mutex_unlock(&atcmd_lock);
       WCN_ERR("%s fail!\n", __func__);
       return;
   }
   timeleft = wait_for_completion_timeout(&atcmd_completion, (3 * HZ));
   mutex_unlock(&atcmd_lock);
   if (!timeleft)
       WCN_ERR("didn't get CP2 version\n");
}
 
#ifdef CONFIG_WCN_LOOPCHECK
void start_loopcheck(void)
{
   if (!test_bit(WCN_LOOPCHECK_INIT, &loopcheck.status) ||
       test_and_set_bit(WCN_LOOPCHECK_OPEN, &loopcheck.status))
       return;
   WCN_INFO("%s\n", __func__);
   reinit_completion(&loopcheck.completion);
   queue_delayed_work(loopcheck.workqueue, &loopcheck.work, HZ);
}
 
void stop_loopcheck(void)
{
   if (!test_bit(WCN_LOOPCHECK_INIT, &loopcheck.status) ||
       !test_and_clear_bit(WCN_LOOPCHECK_OPEN, &loopcheck.status))
       return;
   WCN_INFO("%s\n", __func__);
   complete_all(&loopcheck.completion);
   cancel_delayed_work_sync(&loopcheck.work);
}
 
void complete_kernel_loopcheck(void)
{
   complete(&loopcheck.completion);
}
#endif
 
void complete_kernel_atcmd(void)
{
   complete(&atcmd_completion);
}
 
int loopcheck_init(void)
{
#ifdef CONFIG_WCN_LOOPCHECK
   WCN_DEBUG("loopcheck_init\n");
   loopcheck.status = 0;
   init_completion(&loopcheck.completion);
   loopcheck.workqueue =
           create_singlethread_workqueue("WCN_LOOPCHECK_QUEUE");
   if (!loopcheck.workqueue) {
       WCN_ERR("WCN_LOOPCHECK_QUEUE create failed");
       return -ENOMEM;
   }
   set_bit(WCN_LOOPCHECK_INIT, &loopcheck.status);
   INIT_DELAYED_WORK(&loopcheck.work, loopcheck_work_queue);
#endif
   init_completion(&atcmd_completion);
   mutex_init(&atcmd_lock);
 
   return 0;
}
 
int loopcheck_deinit(void)
{
#ifdef CONFIG_WCN_LOOPCHECK
   stop_loopcheck();
   destroy_workqueue(loopcheck.workqueue);
   loopcheck.status = 0;
#endif
   mutex_destroy(&atcmd_lock);
 
   return 0;
}