-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrovewifiv2.c
More file actions
317 lines (249 loc) · 9.68 KB
/
grovewifiv2.c
File metadata and controls
317 lines (249 loc) · 9.68 KB
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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/serdev.h>
#include <linux/of_device.h>
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/sysfs.h>
#include <linux/kobject.h>
#define GROVE_WIFI_DRIVER_NAME "grove_wifi"
#define RESPONSE_OK "OK"
#define RESPONSE_ERROR "ERROR"
#define RESPONSE_FAIL "FAIL"
#define GROVE_CMD_MAX_LENGTH 60
#define GROVEWIFI_RSP_MAX_LENGTH 35
#define GROVEWIFI_RSP_RAW_MAX_LENGTH 200
#define STD_TIMEOUT 3*HZ
#define EXT_TIMEOUT 10*HZ
static char cmd_cli[GROVE_CMD_MAX_LENGTH];
static unsigned int cli_id;
static kobject *kern_obj;
enum grove_wifi_cli {
CLI_CHK_STATE,
CLI_CONNECT,
CLI_DISCONNECT,
CLI_PING,
CLI_GET_IP,
};
enum grove_wifi_cmd {
CMD_TEST,
CMD_NO_ECHO,
CMD_DISABLE_SLEEP,
CMD_SET_UART,
CMD_STATION_MODE,
CMD_CLI,
};
enum grove_wifi_comm_state{
OK,
ERROR,
NEUTRAL,
};
struct grove_wifi_state {
struct serdev_device *serdev;
struct completion cmd_done;
struct mutex lock;
char response[GROVEWIFI_RSP_RAW_MAX_LENGTH];
size_t response_len;
enum grove_wifi_comm_state comm;
};
struct grove_wifi_state *state_global;
static u8 grove_wifi_cmd_tbl[][GROVE_CMD_MAX_LENGTH] = {
[CMD_TEST] = "\r\nAT\r\n",
[CMD_NO_ECHO] = "\r\nATE0\r\n",
[CMD_DISABLE_SLEEP] = "\r\nAT+SLEEP=0\r\n",
[CMD_SET_UART] = "\r\nAT+UART_CUR=115200,8,1,0,0\r\n",
[CMD_STATION_MODE] = "\r\nAT+CWMODE=1\r\n",
[CMD_CLI] = "\r\n\r\n",
};
/******************************************************************************************************************/
/******************************************************************************************************************/
static int grove_wifi_sysfs_setup(struct serdev_device *serdev);
static void grove_wifi_clear_frame (struct grove_wifi_state *state);
static int grove_wifi_do_cmd(struct grove_wifi_state *state, enum grove_wifi_cmd cmd);
/******************************************************************************************************************/
/******************************************************************************************************************/
static ssize_t grove_wifi_fct_id_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%du", &cli_id);
return count;
}
static ssize_t grove_wifi_fct_id_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
pr_alert("Operation not allowed: cli file is Write-only\n");
return -EPERM;
}
static struct kobj_attribute id =
__ATTR(cli_id, 0644, grove_wifi_fct_id_show, grove_wifi_fct_id_store);
/******************************************************************************************************************/
/******************************************************************************************************************/
static ssize_t grove_wifi_cli_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
int ret;
struct serdev_device *serdev = state_global->serdev;
sprintf(grove_wifi_cmd_tbl[CMD_CLI], "\r\n%s\r\n", buf);
ret = grove_wifi_do_cmd(state_global, CMD_CLI);
if (ret < 0) {
strcpy(grove_wifi_cmd_tbl[CMD_CLI], "\r\n\r\n");
return -EIO;
}
strcpy(grove_wifi_cmd_tbl[CMD_CLI], "\r\n\r\n");
return count;
}
static ssize_t grove_wifi_cli_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
pr_alert("Operation not allowed: cli file is Write-only\n");
return -EPERM;
}
static struct kobj_attribute cli =
__ATTR(cmd_cli, 0644, grove_wifi_cli_show, grove_wifi_cli_store);
/******************************************************************************************************************/
/******************************************************************************************************************/
static ssize_t grove_wifi_response_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
pr_alert("Operation not allowed: cli file is Write-only\n");
return -EPERM;
}
static ssize_t grove_wifi_response_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
int ret;
enum grove_wifi_comm_state comm_state = state_global->comm;
char response_cli[GROVEWIFI_RSP_MAX_LENGTH];
switch (comm_state)
{
case ERROR:
strcpy(response_cli,"Unsuccessful Operation");
break;
case OK:
strcpy(response_cli,"Successful operation");
break;
default:
strcpy(response_cli,"Waiting for response");
break;
}
return sprintf(buf, "%s", response_cli);
}
static struct kobj_attribute response =
__ATTR(response_cli, 0644, grove_wifi_response_show, grove_wifi_response_store);
/******************************************************************************************************************/
/******************************************************************************************************************/
static int grove_wifi_sysfs_setup(struct serdev_device *serdev){
struct grove_wifi_state *state = serdev_device_get_drvdata(serdev);
int error;
kern_obj = kobject_create_and_add("grovewifiv2", kernel_kobj);
if ( !kern_obj )
return -ENOMEM;
error = sysfs_create_file(kern_obj, &cli.attr);
if (error) {
dev_err(&serdev->dev, "CLI file not created under /sys/kernel/grovewifiv2\n");
return -ENOMEM;
}
error = sysfs_create_file(kern_obj, &response.attr);
if (error) {
dev_err(&serdev->dev, "RESPONSE file not created under /sys/kernel/grovewifiv2\n");
return -ENOMEM;
}
error = sysfs_create_file(kern_obj, &id.attr);
if (error) {
dev_err(&serdev->dev, "ID file not created under /sys/kernel/grovewifiv2\n");
return -ENOMEM;
}
dev_info(&serdev->dev, "Successfully created /sys/kernel/grovewifiv2\n");
return 0;
}
/******************************************************************************************************************/
/******************************************************************************************************************/
static int grove_wifi_receive_buf(struct serdev_device *serdev, const u8 *buf, size_t size) {
struct grove_wifi_state *state = serdev_device_get_drvdata(serdev);
size_t num;
mutex_lock(&state->lock);
num = min(size, sizeof(state->response) - state->response_len - 1);
memcpy(state->response + state->response_len, buf, num);
state->response_len += num;
state->response[state->response_len] = '\0';
if (strstr(state->response, RESPONSE_OK)) {
dev_info(&serdev->dev, "Command successfully executed");
complete(&state->cmd_done);
}
mutex_unlock(&state->lock);
return (int) size;
}
static const struct serdev_device_ops grove_wifi_serdev_ops = {
.receive_buf = grove_wifi_receive_buf,
.write_wakeup = serdev_device_write_wakeup,
};
static void grove_wifi_clear_frame (struct grove_wifi_state *state)
{
mutex_lock(&state->lock);
strcpy(state->response,"");
state->response_len = 0;
state->comm = NEUTRAL;
mutex_unlock(&state->lock);
}
static int grove_wifi_do_cmd(struct grove_wifi_state *state, enum grove_wifi_cmd cmd) //TODO Response "OK"
{
struct serdev_device *serdev = state->serdev;
int ret;
grove_wifi_clear_frame(state);
mutex_lock(&state->lock);
ret = serdev_device_write(serdev, grove_wifi_cmd_tbl[cmd], strlen(grove_wifi_cmd_tbl[cmd]), STD_TIMEOUT);
mutex_unlock(&state->lock);
if (ret < strlen(grove_wifi_cmd_tbl[cmd]))
return ret < 0 ? ret : -EIO;
ret = wait_for_completion_timeout(&state->cmd_done, EXT_TIMEOUT);
if (!ret){
dev_err(&serdev->dev, "Timeout waiting for response\n");
reinit_completion(&state->cmd_done);
state->comm = ERROR;
return -ETIMEDOUT;
}
state->comm = OK;
reinit_completion(&state->cmd_done);
return 0;
}
static int grove_wifi_probe(struct serdev_device *serdev) {
struct grove_wifi_state *state;
int ret;
state = devm_kzalloc(&serdev->dev, sizeof(*state), GFP_KERNEL);
state_global = state;
if (!state)
return -ENOMEM;
serdev_device_set_drvdata(serdev, state);
state->serdev = serdev;
mutex_init(&state->lock);
init_completion(&state->cmd_done);
serdev_device_set_client_ops(serdev, &grove_wifi_serdev_ops);
ret = devm_serdev_device_open(&serdev->dev, serdev);
if (ret)
return ret;
serdev_device_set_baudrate(serdev, 115200);
serdev_device_set_flow_control(serdev, false);
ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
if (ret)
return ret;
for ( enum grove_wifi_cmd cmd = CMD_TEST; cmd <= CMD_STATION_MODE; cmd++ ){
ret = grove_wifi_do_cmd(state, cmd);
if (ret < 0){
dev_err(&serdev->dev, "Error occured, unexpected response\n");
return -EIO;
}
}
dev_info(&serdev->dev, "Grove WiFi module is configured\n");
grove_wifi_sysfs_setup (serdev);
return 0;
}
static const struct of_device_id grove_wifi_of_match[] = {
{ .compatible = "seeedstudio,grovewifiv1" },
{ .compatible = "seeedstudio,grovewifiv2" },
{ }
};
MODULE_DEVICE_TABLE(of, grove_wifi_of_match);
static struct serdev_device_driver grove_wifi_driver = {
.driver = {
.name = GROVE_WIFI_DRIVER_NAME,
.of_match_table = of_match_ptr(grove_wifi_of_match),
},
.probe = grove_wifi_probe,
};
module_serdev_device_driver(grove_wifi_driver);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("AKhadhraoui47");
MODULE_DESCRIPTION("Grove Wifi Kernel Module");
/******************************************************************************************************************/
/******************************************************************************************************************/