-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathesp8266.c
More file actions
730 lines (591 loc) · 15.9 KB
/
Copy pathesp8266.c
File metadata and controls
730 lines (591 loc) · 15.9 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/rtnetlink.h>
#include <linux/etherdevice.h>
#include <net/cfg80211.h>
#include "crc16.h"
#include "esp8266.h"
#include "cfg80211.h"
#define N_ESP8266 26
MODULE_ALIAS_LDISC(N_ESP8266);
MODULE_DESCRIPTION("ESP8266 driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Raashid Muhammed <raashidmuhammed@zilogic.com>");
struct esp8266 {
struct tty_struct *tty;
struct net_device *dev;
struct wiphy *wiphy;
struct wireless_dev wdev;
spinlock_t lock;
struct work_struct tx_work; /* Flush xmit buffer */
uint8_t xbuff[BUF_SIZE];
uint8_t *xhead; /* Pointer to next xmit byte */
int xleft; /* Bytes left in xmit queue */
unsigned int xpos;
uint8_t rbuff[BUF_SIZE];
unsigned int rlen;
uint8_t msg_type;
uint8_t data[BUF_SIZE];
unsigned int len;
uint16_t crc;
uint16_t no_entries;
struct cfg80211_scan_request *scan_req;
};
static void print_buf(uint8_t *buf, unsigned int len)
{
int index;
for(index = 0; index < len; index++)
printk(KERN_CONT "%02X", buf[index]);
printk("\n");
}
static int serial_write(struct esp8266 *esp, uint8_t byte)
{
int actual;
esp->xbuff[esp->xpos] = byte;
esp->xpos += 1;
if (byte == SERIAL_STOP_BYTE) {
set_bit(TTY_DO_WRITE_WAKEUP, &esp->tty->flags);
actual = esp->tty->ops->write(esp->tty, esp->xbuff, esp->xpos);
if (actual < 0) {
printk("esp8266: Serial write failed\n");
return -1;
}
/* Handling partial writes */
esp->xleft = esp->xpos - actual;
esp->xhead = esp->xbuff + actual;
esp->dev->stats.tx_bytes += actual;
esp->xpos = 0;
}
return 0;
}
static int stuff_tx_byte(struct esp8266 *esp, uint8_t byte)
{
int ret;
if ((byte == SERIAL_STOP_BYTE) || (byte == SERIAL_ESC_BYTE)) {
ret = serial_write(esp, SERIAL_ESC_BYTE);
if (ret < 0)
return -1;
ret = serial_write(esp, byte ^ SERIAL_XOR_BYTE);
if (ret < 0)
return -1;
} else {
ret = serial_write(esp, byte);
if (ret < 0)
return -1;
}
return 0;
}
static int byte_destuff_packet(struct esp8266 *esp)
{
printk("esp8266: byte_destuff_packet called\n");
int flag = 0;
int i = 0;
int j = 0;
while (i < esp->rlen) {
if (esp->rbuff[i] == SERIAL_ESC_BYTE) {
flag = 1;
i++;
continue;
}
if (j == (MAX_FRAME_LEN / 2))
return -1;
if (flag == 1) {
esp->rbuff[j++] = esp->rbuff[i++]
^ SERIAL_XOR_BYTE;
flag = 0;
continue;
}
esp->rbuff[j++] = esp->rbuff[i++];
}
esp->rlen = j;
return 0;
}
static int parse_data(struct esp8266 *esp)
{
uint16_t crc_l;
if (esp->rlen < MIN_BYTE_EXPECTED)
return -1;
esp->crc = esp->rbuff[--(esp->rlen)];
crc_l = esp->rbuff[--(esp->rlen)];
esp->crc <<= 8;
esp->crc = esp->crc | crc_l;
return 0;
}
static int check_data_integrity(struct esp8266 *esp)
{
uint16_t cal_crc;
cal_crc = crc16_ccitt_block(esp->rbuff, esp->rlen);
if (esp->crc != cal_crc)
return -3;
return 0;
}
static int crc_stuff_tx_byte(struct esp8266 *esp, uint8_t byte)
{
int ret;
crc16_ccitt_update(&esp->crc, byte);
ret = stuff_tx_byte(esp, byte);
return ret;
}
static int esp_read(struct esp8266 *esp)
{
if (byte_destuff_packet(esp) < 0) {
printk("esp8266: destuff error\n");
return -1;
}
if (parse_data(esp) < 0){
printk("esp8266: parse_data error\n");
return -1;
}
if (check_data_integrity(esp)) {
printk("esp8266: crc failure\n");
esp->dev->stats.rx_crc_errors++;
return -1;
}
return 0;
}
static int esp_send(struct esp8266 *esp)
{
int ret;
int index;
esp->crc = 0;
ret = crc_stuff_tx_byte(esp, esp->msg_type);
if (ret < 0)
return -1;
for(index = 0; index < esp->len; index++){
ret = crc_stuff_tx_byte(esp, esp->data[index]);
if (ret < 0)
return -1;
}
ret = stuff_tx_byte(esp, LSB(esp->crc));
if (ret < 0)
return -1;
ret = stuff_tx_byte(esp, MSB(esp->crc));
if (ret < 0)
return -1;
ret = serial_write(esp, SERIAL_STOP_BYTE);
if (ret < 0)
return -1;
return 0;
}
static int configure_esp(struct esp8266 *esp, uint8_t msg_type, uint8_t mode)
{
esp->msg_type = msg_type;
esp->data[0] = mode;
esp->len = 1;
if (esp_send(esp) < 0)
return -1;
esp->len = 0;
esp->rlen = 0;
return 0;
}
static void scan(struct msg_wifi_scan_request *rscan)
{
memset(rscan, 0, sizeof(struct msg_wifi_scan_request));
rscan->msg_type = MSG_WIFI_SCAN_REQUEST;
}
static void generate_connect_header(struct msg_station_conf *conf, char *ssid, size_t ssid_len, char *password, uint8_t password_len)
{
memset(conf, 0, sizeof(struct msg_station_conf));
conf->msg_type = MSG_STATION_CONF_SET;
conf->ssid_len = ssid_len;
conf->password_len = password_len;
memcpy(&conf->ssid, ssid, conf->ssid_len);
memcpy(&conf->password, password, conf->password_len);
}
static int espnet_init(struct net_device *dev)
{
struct esp8266 *esp = netdev_priv(dev);
if (configure_esp(esp, MSG_WIFI_SLEEP_MODE_SET, WIFI_SLEEP_NONE) < 0) {
printk("esp8266: Error Initializing Sleep Mode");
return -1;
}
if (configure_esp(esp, MSG_SET_FORWARDING_MODE, FORWARDING_MODE_ETHER) < 0) {
printk("esp8266: Error Initializing Forwarding Mode");
return -1;
}
if (configure_esp(esp, MSG_WIFI_MODE_SET, DEVICE_MODE_STAION) < 0) {
printk("esp8266: Error Initializing Device Mode");
return -1;
}
return 0;
}
/* Netdevice DOWN -> UP routine */
static int espnet_open(struct net_device *dev)
{
struct esp8266 *esp = netdev_priv(dev);
if (esp->tty == NULL) {
printk("esp8266: No TTY device\n");
return -ENODEV;
}
netif_start_queue(dev);
return 0;
}
static netdev_tx_t espnet_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct esp8266 *esp = netdev_priv(dev);
/* fixme: should mtu check be done at this point */
spin_lock(&esp->lock);
if (!netif_running(dev)) {
printk(KERN_WARNING "esp8266: %s: xmit: iface is down\n", dev->name);
goto out;
}
if (esp->tty == NULL) {
goto out;
}
netif_stop_queue(esp->dev);
dev->stats.tx_bytes += skb->len;
esp->msg_type = MSG_ETHER_PACKET;
esp->len = skb->len;
memmove(esp->data, skb->data, skb->len);
esp_send(esp);
esp->len = 0;
out:
spin_unlock(&esp->lock);
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
/* Netdevice UP -> DOWN routine */
static int espnet_close(struct net_device *dev)
{
struct esp8266 *esp = netdev_priv(dev);
if (esp->tty) {
/* TTY discipline is running. */
clear_bit(TTY_DO_WRITE_WAKEUP, &esp->tty->flags);
}
netif_stop_queue(dev);
return 0;
}
static void esp_transmit(struct work_struct *work)
{
struct esp8266 *esp = container_of(work, struct esp8266, tx_work);
int actual;
spin_lock_bh(&esp->lock);
/* First make sure we're connected. */
if (!esp->tty || !netif_running(esp->dev)) {
spin_unlock_bh(&esp->lock);
return;
}
if (esp->xleft <= 0) {
/* Now serial buffer is almost free & we can start
* transmission of another packet */
esp->dev->stats.tx_packets++;
clear_bit(TTY_DO_WRITE_WAKEUP, &esp->tty->flags);
spin_unlock_bh(&esp->lock);
netif_wake_queue(esp->dev);
return;
}
actual = esp->tty->ops->write(esp->tty, esp->xhead, esp->xleft);
esp->xleft -= actual;
esp->xhead += actual;
spin_unlock_bh(&esp->lock);
}
static int esp_cfg80211_scan(struct wiphy *wiphy,
struct cfg80211_scan_request *request)
{
printk("esp8266: esp_scan entry\n");
struct msg_wifi_scan_request msg_scan;
struct esp8266 *esp = container_of(request->wdev, struct esp8266, wdev);
esp->scan_req = request;
scan(&msg_scan);
esp->msg_type = msg_scan.msg_type;
memmove(esp->data, &msg_scan, sizeof(struct msg_wifi_scan_request));
esp->len = sizeof(struct msg_wifi_scan_request) - 1;
memmove(esp->data, &esp->data[1], esp->len);
esp_send(esp);
printk("esp8266: esp_scan exit\n");
return 0;
}
static int esp_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
struct cfg80211_connect_params *sme)
{
printk("esp8266: esp_connect called\n");
struct esp8266 *esp = netdev_priv(dev);
struct msg_station_conf conf;
printk("esp8266: ssid:password %s:%s, %d:%d", sme->ssid, sme->key, sme->ssid_len, sme->key_len);
generate_connect_header(&conf, sme->ssid, sme->ssid_len, sme->key, sme->key_len);
esp->msg_type = conf.msg_type;
memmove(esp->data, &conf, sizeof(struct msg_station_conf));
esp->len = sizeof(struct msg_station_conf) - 1;
memmove(esp->data, &esp->data[1], esp->len);
esp_send(esp);
esp->len = 0;
esp->rlen = 0;
return 0;
}
static int esp_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
struct cfg80211_connect_params *sme)
{
printk("esp8266: esp_disconnect callled\n");
return 0;
}
static struct cfg80211_ops esp_cfg80211_ops = {
.scan = esp_cfg80211_scan,
.connect = esp_cfg80211_connect,
.disconnect = esp_cfg80211_disconnect,
};
static const struct net_device_ops esp_netdev_ops = {
.ndo_init = espnet_init,
.ndo_open = espnet_open,
.ndo_stop = espnet_close,
.ndo_start_xmit = espnet_xmit,
};
static void esp_free_netdev(struct net_device *dev)
{
free_netdev(dev);
}
static void esp_setup(struct net_device *dev)
{
ether_setup(dev);
dev->netdev_ops = &esp_netdev_ops;
dev->destructor = esp_free_netdev;
}
int wiphy_init(struct esp8266 *esp)
{
int ret;
struct wiphy *wiphy = esp->wiphy;
// wiphy->mgmt_stypes =
// wiphy->max_remain_on_channel_duration = 5000;
/* set device pointer for wiphy */
set_wiphy_dev(wiphy, esp->tty->dev);
wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
/* max num of ssids that can be probed during scanning */
wiphy->max_scan_ssids = 128;
wiphy->max_scan_ie_len = 1000; /* FIX: what is correct limit? */
// wiphy->available_antennas_tx = ar->hw.tx_ant;
// wiphy->available_antennas_rx = ar->hw.rx_ant;
wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM; /* fixme: */
wiphy->bands[NL80211_BAND_2GHZ] = &esp_band_2ghz;
wiphy->bands[NL80211_BAND_5GHZ] = &esp_band_5ghz;
// *fixme: required? wiphy->max_sched_scan_ssids
/* fixme: required? wiphy->flags ? */
/* fixme: wiphy->probe_resp_offload */
ret = wiphy_register(wiphy);
if (ret < 0) {
printk("esp8266: couldn't register wiphy device");
wiphy_free(esp->wiphy);
return -1;
}
return 0;
}
static int esptty_open(struct tty_struct *tty)
{
int err;
char name[IFNAMSIZ];
struct esp8266 *esp;
/*fixme: name should be netdev */
struct net_device *dev = NULL;
struct wiphy *wiphy;
unsigned char mac_addr[] = {0x5c, 0xcf, 0x7f, 0x0b, 0x9c, 0xb6};
sprintf(name, "esp%d", 0);
dev = alloc_netdev(sizeof(*esp), name, NET_NAME_UNKNOWN, esp_setup);
if (!dev) {
return -1;
}
wiphy = wiphy_new(&esp_cfg80211_ops, sizeof(struct esp8266));
if (!wiphy) {
printk("esp8266: couldn't allocate wiphy device");
return -1;
}
/* fixme: get mac from esp before setting */
ether_addr_copy(dev->dev_addr, mac_addr);
dev->addr_assign_type = NET_ADDR_PERM;
esp = netdev_priv(dev);
esp->dev = dev;
esp->tty = tty;
esp->wiphy = wiphy;
esp->dev->ieee80211_ptr = &esp->wdev;
esp->wdev.wiphy = wiphy;
esp->wdev.iftype = NL80211_IFTYPE_STATION;
esp->wdev.netdev = dev;
SET_NETDEV_DEV(dev, wiphy_dev(esp->wiphy));
esp->len = 0;
spin_lock_init(&esp->lock);
INIT_WORK(&esp->tx_work, esp_transmit);
tty->disc_data = esp;
err = wiphy_init(esp);
if (err) {
return err;
}
printk("wiphy registration complete\n");
err = register_netdev(esp->dev);
if (err) {
printk("Netdevice registration failed.\n");
esp->tty = NULL;
tty->disc_data = NULL;
return err;
}
tty->receive_room = 65536; /* Enables receive */
return 0;
}
static void esp_forward(struct esp8266 *esp)
{
struct sk_buff *skb;
esp->dev->stats.rx_bytes += esp->rlen - 1;
skb = dev_alloc_skb(esp->rlen - 1);
if (skb == NULL) {
printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", esp->dev->name);
esp->dev->stats.rx_dropped++;
return;
}
skb->dev = esp->dev;
memcpy(skb_put(skb, esp->rlen - 1), &esp->rbuff[1], esp->rlen - 1);
skb->protocol = eth_type_trans(skb, esp->dev);
netif_rx_ni(skb);
esp->dev->stats.rx_packets++;
}
void esp_get_entries(struct esp8266 *esp)
{
if (esp->rbuff[1] != 0)
/* fixme: Notify scan failure */
printk("esp8266: scan failed\n");
esp->no_entries = (esp->rbuff[3] << 8) | esp->rbuff[2];
}
void esp_inform_bss(struct esp8266 *esp)
{
printk("esp8266: esp_inform_bss called\n");
struct msg_wifi_scan_entry entry;
struct cfg80211_bss *bss;
struct ieee80211_channel *channel;
u32 freq;
s32 signal = -30;
u64 timestamp = 1;
u16 capability = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY;
u32 beacon_period = 400;
u8 ie[53];
memcpy(&entry, &esp->rbuff[1], sizeof(struct msg_wifi_scan_entry));
freq = ieee80211_channel_to_frequency(entry.channel, NL80211_BAND_2GHZ);
printk("esp8266: freq: %d\n", freq);
channel = ieee80211_get_channel(esp->wiphy, freq);
if (!channel) {
printk("esp8266: No channel\n");
}
/* fixme: Need to fix based on auth */
/* Based on
https://mrncciew.files.wordpress.com/2014/08/cwsp-rsn-5.png,
but there seems to be a mistake in no. of bytes in version
field. */
ie[0] = WLAN_EID_RSN;
ie[1] = 18;
ie[2] = 0x01;
ie[3] = 0x00;
ie[4] = 0x00; /* extra */
ie[5] = 0x0f;
ie[6] = 0xac;
ie[7] = 0x04;
ie[8] = 0x01;
ie[9] = 0x00;
ie[10] = 0x00;
ie[11] = 0x0f;
ie[12] = 0xac;
ie[13] = 0x04;
ie[14] = 0x01;
ie[15] = 0x00;
ie[16] = 0x00;
ie[17] = 0x0f;
ie[18] = 0xac;
ie[19] = 0x02;
ie[20] = WLAN_EID_SSID;
ie[21] = entry.ssid_len;
memcpy(&ie[22], entry.ssid, entry.ssid_len);
bss = cfg80211_inform_bss(esp->wiphy, channel, CFG80211_BSS_FTYPE_UNKNOWN,
entry.bssid, timestamp, capability, beacon_period,
ie, 2 + entry.ssid_len + 6 + 14, entry.rssi, GFP_KERNEL);
//cfg80211_put_bss(esp->wiphy, bss);
esp->no_entries--;
}
static void esptty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
char *fp, int count)
{
printk("esp8266: receive_buf called\n");
struct esp8266 *esp = tty->disc_data;
struct cfg80211_scan_info info = {};
int index = 0;
int ret;
printk("esp8266: while loop start\n");
/* Read the characters out of the buffer */
while (count--) {
if (fp && *fp++) {
cp++;
printk("esp8266: Parity Errors\n");
continue;
}
esp->rbuff[esp->rlen] = *(cp + index);
if (esp->rbuff[esp->rlen] == SERIAL_STOP_BYTE) {
printk("esp8266: proper packet received\n");
print_buf(esp->rbuff, esp->rlen);
ret = esp_read(esp);
if (ret < 0)
printk("esp8266: esp receive error\n");
if (esp->rbuff[0] == MSG_ETHER_PACKET) {
printk("esp8266: ethernet packet\n");
esp_forward(esp);
}
else if(esp->rbuff[0] == MSG_WIFI_SCAN_REPLY) {
printk("esp8266: wifi scan reply\n");
esp_get_entries(esp);
}
else if(esp->rbuff[0] == MSG_WIFI_SCAN_ENTRY) {
printk("esp8266: wifi scan entry\n");
esp_inform_bss(esp);
if (esp->no_entries == 0) {
printk("esp8266: scan bss informed all");
cfg80211_scan_done(esp->scan_req, &info);
}
}
esp->rlen = -1;
}
index++;
esp->rlen++;
}
printk("esp8266: while loop end\n");
}
/*
* Called by the driver when there's room for more data.
* Schedule the transmit.
*/
static void esptty_write_wakeup(struct tty_struct *tty)
{
printk("esp8266: write_wakeup called\n");
struct esp8266 *esp = tty->disc_data;
schedule_work(&esp->tx_work);
}
static void esptty_close(struct tty_struct *tty)
{
struct esp8266 *esp = (struct esp8266 *) tty->disc_data;
tty->disc_data = NULL;
esp->tty = NULL;
flush_work(&esp->tx_work);
unregister_netdev(esp->dev);
wiphy_unregister(esp->wiphy);
}
static struct tty_ldisc_ops esp8266_ldisc = {
.owner = THIS_MODULE,
.magic = TTY_LDISC_MAGIC,
.name = "n_esp",
.open = esptty_open,
.close = esptty_close,
.receive_buf = esptty_receive_buf,
.write_wakeup = esptty_write_wakeup,
};
static int __init esp8266_init(void)
{
int status;
pr_info("esp8266: ESP8266 network driver\n");
status = tty_register_ldisc(N_ESP8266, &esp8266_ldisc);
if (status) {
printk(KERN_ERR "esp8266: can't register line discipline\n");
}
return status;
}
static void __exit esp8266_exit(void)
{
int ret;
ret = tty_unregister_ldisc(N_ESP8266);
if (ret)
printk(KERN_ERR "esp8266: can't unregister ldisc (err %d)\n", ret);
}
module_init(esp8266_init);
module_exit(esp8266_exit);