forked from gkaindl/rfm12b-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfm12b.c
2771 lines (2206 loc) · 72.2 KB
/
rfm12b.c
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* rfm12b-linux: linux kernel driver for the rfm12(b) RF module by HopeRF
* Copyright (C) 2013 Georg Kaindl
*
* This file is part of rfm12b-linux.
*
* rfm12b-linux is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* rfm12b-linux 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.
*
* You should have received a copy of the GNU General Public License
* along with rfm12b-linux. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spi/spi.h>
#define BUILD_MODULE 1
#include "rfm12b_config.h"
#if defined(MODULE_BOARD_CONFIGURED)
#include "rfm12b_ioctl.h"
#include "rfm12b_jeenode.h"
static u8 group_id = RFM12B_DEFAULT_GROUP_ID;
static u8 band_id = RFM12B_DEFAULT_BAND_ID;
static u8 bit_rate = RFM12B_DEFAULT_BIT_RATE;
static u8 jee_id = RFM12B_DEFAULT_JEE_ID;
static u8 jee_autoack = RFM12B_DEFAULT_JEE_AUTOACK;
module_param(group_id, byte, 0000);
MODULE_PARM_DESC(group_id,
"group ID for rfm12b modules. can be changed per board via ioctl().");
module_param(band_id, byte, 0000);
MODULE_PARM_DESC(band_id,
"band ID for rfm12b modules. can be changed per board via ioctl(). "
"1 .. 433mhz; 2 .. 868mhz; 3 .. 915mhz");
module_param(bit_rate, byte, 0000);
MODULE_PARM_DESC(bit_rate,
"bit rate setting byte for rfm12b modules (see datasheet). "
"can be changed per board via ioctl().");
module_param(jee_id, byte, 0000);
MODULE_PARM_DESC(jee_id,
"jeenode id for rfm12b modules, or 0 to disable the jee protocol. this "
"can be changed per board via ioctl().");
module_param(jee_autoack, byte, 0000);
MODULE_PARM_DESC(jee_autoack,
"if the jee-protocol is enabled by setting a jee-id, enable or disable "
"the automated sending of ACKs when a packet requests them. Can also be "
"changed per board via ioctl().");
#define RF_READ_STATUS 0x0000
#define RF_IDLE_MODE 0x820D
#define RF_SLEEP_MODE 0x8205
#define RF_TXREG_WRITE 0xB800
#define RF_RECEIVER_ON 0x82DD
#define RF_XMITTER_ON 0x823D
#define RF_RX_FIFO_READ 0xB000
#define RF_STATUS_BIT_LBAT (0x0400)
#define RF_STATUS_BIT_FFEM (0x0200)
#define RF_STATUS_BIT_FFOV_RGUR (0x2000)
#define RF_STATUS_BIT_RSSI (0x0100)
#define RF_STATUS_BIT_FFIT_RGIT (0x8000)
#define RFM69_MASK_REGWRITE (0x80)
#define RFM69_REG_IRQFLAGS2 (0x28)
#define RFM69_REG_FIFO (0x00)
#define RFM69_REG_OPMODE (0x01)
#define RFM69_REG_RSSIVALUE (0x24)
#define RFM69_REG_DIOMAPPING1 (0x25)
#define RFM69_MODE_STANDBY (0x04)
#define RFM69_MODE_RECEIVER (0x10)
#define RFM69_MODE_XMITTER (0x0C)
#define RFM69_DIOMAPPING1_DIO0_00 (0x00)
#define RFM69_DIOMAPPING1_DIO0_10 (0x80)
#define RFM69_IRQ2_FIFOFULL (0x80)
#define RFM69_IRQ2_FIFONOTEMPTY (0x40)
#define RFM69_IRQ2_FIFOOVERRUN (0x10)
#define RFM69_RSSIVAL_TO_DBM(val) (((int)(-(val)))>>1)
#define RFM69_RSSIVAL_SEND_MIN (-82)
#define RF_MAX_DATA_LEN 66
#define RF_EXTRA_LEN 4 // 4 : 1 byte hdr, 1 byte len, 2 bytes crc16 (see JeeLib)
#define RF_MAX_LEN (RF_MAX_DATA_LEN+RF_EXTRA_LEN)
#define OPEN_WAIT_MILLIS (50)
#define READ_FIFO_WAIT (0)
#define WRITE_TX_WAIT (0)
#define RXTX_WATCHDOG_JIFFIES (HZ/4)
#define TRYSEND_RETRY_JIFFIES (HZ/16)
#define DATA_BUF_SIZE (512)
#define NUM_MAX_CONCURRENT_MSG (3)
#define INTERPRETS_JEENODE_PROTOCOL(rfm12) (0 != (rfm12)->jee_id)
#define CAN_SEND_BYTES_OF_LENGTH(LEN) \
(DATA_BUF_SIZE - (rfm12->out_cur_end - rfm12->out_buf) >= (LEN) + RF_EXTRA_LEN)
static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);
static DECLARE_BITMAP(minors, RFM12B_NUM_SPI_MINORS);
typedef enum _rfm12_state_t {
RFM12_STATE_NO_CHANGE = 0,
RFM12_STATE_CONFIG = 1,
RFM12_STATE_SLEEP = 2,
RFM12_STATE_IDLE = 3,
RFM12_STATE_LISTEN = 4,
RFM12_STATE_RECV = 5,
RFM12_STATE_RECV_FINISH = 6,
RFM12_STATE_SEND_PRE1 = 7,
RFM12_STATE_SEND_PRE2 = 8,
RFM12_STATE_SEND_PRE3 = 9,
RFM12_STATE_SEND_SYN1 = 10,
RFM12_STATE_SEND_SYN2 = 11,
RFM12_STATE_SEND = 12,
RFM12_STATE_SEND_TAIL1 = 13,
RFM12_STATE_SEND_TAIL2 = 14,
RFM12_STATE_SEND_TAIL3 = 15,
RFM12_STATE_SEND_FINISHED = 16
} rfm12_state_t;
struct rfm12_spi_message {
struct spi_message spi_msg;
struct spi_transfer spi_transfers[4];
rfm12_state_t spi_finish_state;
u8 spi_tx[8], spi_rx[8];
void* context;
u8 pos;
};
struct rfm12_data {
u16 irq;
void* irq_identifier;
dev_t devt;
spinlock_t lock;
struct spi_device* spi;
struct list_head device_entry;
rfm12_module_type_t module_type;
u8 open, should_release, trysend;
rfm12_state_t state;
u8 group_id, band_id, bit_rate, jee_id, jee_autoack;
unsigned long bytes_recvd, pkts_recvd;
unsigned long bytes_sent, pkts_sent;
unsigned long num_recv_overflows, num_recv_timeouts, num_recv_crc16_fail;
unsigned long num_send_underruns, num_send_timeouts;
u8* in_buf, *in_buf_pos;
u8* out_buf, *out_buf_pos;
u8* in_cur_len_pos;
u8* in_cur_end, *out_cur_end;
u16 crc16;
int in_cur_num_bytes, out_cur_num_bytes;
struct rfm12_spi_message spi_msgs[NUM_MAX_CONCURRENT_MSG];
u8 free_spi_msgs;
u32 spi_speed_hz;
struct timer_list rxtx_watchdog;
u8 rxtx_watchdog_running;
struct timer_list retry_sending_timer;
u8 retry_sending_running;
wait_queue_head_t wait_read;
wait_queue_head_t wait_write;
u16 rf12_last_status;
u8 rf69_req_mode, rf69_recv_read_rssi;
int rf69_last_rssi;
void (*rf69_mode_callback)(void*);
void (*rf69_flush_fifo_callback)(struct rfm12_data*);
};
// forward declarations
static struct rfm12_spi_message*
rfm_claim_spi_message(struct rfm12_data* rfm12);
static void
rfm_unclaim_spi_message(struct rfm12_spi_message* spi_msg);
struct spi_transfer
rfm_make_spi_transfer(struct rfm12_data* rfm12,
uint16_t cmd, u8* tx_buf, u8* rx_buf);
struct spi_transfer
rfm_control_spi_transfer(struct rfm12_data* rfm12,
struct rfm12_spi_message* msg,
u8 pos, uint16_t cmd);
static void
rfm_spi_completion_common(struct rfm12_spi_message* msg);
static void
__rfm_generic_spi_completion_handler(void *arg);
static void
rfm_generic_spi_completion_handler(void *arg);
static int
rfm_send_generic_async_cmd(struct rfm12_data* rfm12, uint16_t* cmds,
int num_cmds, uint16_t delay_usecs,
void (*callback)(void*),
rfm12_state_t finish_state);
static rfm12_module_type_t
rfm_detect_module_type(struct rfm12_data* rfm12);
static const char*
rfm_string_for_module_type(rfm12_module_type_t module_type);
static int
rfm_start_receiving_common(struct rfm12_data* rfm12);
static int
rfm_reset(struct rfm12_data* rfm12);
static void
rfm_begin_sending_or_receiving(struct rfm12_data* rfm12);
static int
rfm_consume_received_byte(struct rfm12_data* rfm12, u8 recvd_byte);
static int
rfm_finish_receiving(struct rfm12_data* rfm12, int skip_packet);
static void
rfm_update_rxtx_watchdog(struct rfm12_data* rfm12, u8 cancelTimer);
static void
rfm_rxtx_watchdog_expired(unsigned long ptr);
static void
rfm_apply_crc16(struct rfm12_data* rfm12, unsigned char* ptr, unsigned len);
static u16
rfm_crc16_update(u16 crc, u8 b);
static void
rfm_finish_send_or_recv_callback(void *arg);
static void
rfm_finish_trysend(struct rfm12_data* rfm12);
static void
rfm_start_trysend_retry_timer(struct rfm12_data* rfm12);
static void
rfm_trysend_retry_timer_expired(unsigned long ptr);
static int
rfm_finish_sending(struct rfm12_data* rfm12, int success);
static void
rfm_stop_listening_and_send_callback(void *arg);
static void
rfm_release_when_safe(struct rfm12_data* rfm12);
static void
rfm_disable_hardware_on_release_handler(void *arg);
static int
rfmXX_setup(struct rfm12_data* rfm12);
static int
rfmXX_disable_hardware_on_release(struct rfm12_data* rfm12);
static int
rfmXX_try_sending(struct rfm12_data* rfm12);
static int
rfmXX_start_receiving(struct rfm12_data* rfm12);
static int
rfmXX_finish_send_recv_common(struct rfm12_data* rfm12);
static int
rfmXX_stop_listening_and_send(struct rfm12_data* rfm12);
static void
rfmXX_handle_interrupt(struct rfm12_data* rfm12);
static int
rfm12_setup(struct rfm12_data* rfm12);
static int
rfm12_disable_hardware_on_release(struct rfm12_data* rfm12);
static int
rfm12_start_receiving(struct rfm12_data* rfm12);
static int
rfm12_request_fifo_byte(struct rfm12_data* rfm12);
static int
rfm12_finish_send_recv_common(struct rfm12_data* rfm12);
static int
rfm12_try_sending(struct rfm12_data* rfm12);
static void
rfm12_trysend_completion_handler(void *arg);
static int
rfm12_stop_listening_and_send(struct rfm12_data* rfm12);
static void
rfm12_handle_interrupt(struct rfm12_data* rfm12);
static int
rfm69_flush_fifo_blocking(struct rfm12_data* rfm12);
static int
rfm69_set_mode(struct rfm12_data* rfm12, u8 mode, void (*spi_callback)(void*));
static void
rfm69_set_mode_handler(void* arg);
static int
rfm69_setup(struct rfm12_data* rfm12);
static int
rfm69_disable_hardware_on_release(struct rfm12_data* rfm12);
static int
rfm69_start_receiving(struct rfm12_data* rfm12);
static void
rfm69_start_receiving_with_empty_fifo(struct rfm12_data* rfm12);
static int
rfm69_flush_fifo_with_finish_handler(struct rfm12_data* rfm12,
void (*a_handler)(struct rfm12_data*));
static void
rfm69_flush_fifo_async_completion_handler(void* arg);
static void
rfm69_flush_fifo_async_loop_handler(void* arg);
static int
rfm69_try_sending(struct rfm12_data* rfm12);
static void
rfm69_trysend_completion_handler(void *arg);
static int
rfm69_stop_listening_and_send(struct rfm12_data* rfm12);
static void
rfm69_start_sending_with_empty_fifo(struct rfm12_data* rfm12);
static void
rfm69_start_sending_with_empty_fifo_handler(void* arg);
static void
rfm69_set_mode_transmitter_callback(void* arg);
static int
rfm69_send_fifo_loop(struct rfm12_data* rfm12);
static void
rfm69_send_fifo_loop_callback(void* arg);
static int
rfm69_poll_receive_fifo(struct rfm12_data* rfm12);
static void
rfm69_poll_receive_fifo_completion_handler(void* arg);
static int
rfm69_request_fifo_byte(struct rfm12_data* rfm12);
static void
rfm69_request_fifo_byte_completion_handler(void* arg);
static int
rfm69_finish_send_recv_common(struct rfm12_data* rfm12);
static void
rfm69_handle_interrupt(struct rfm12_data* rfm12);
static ssize_t
rfm_filop_read(struct file* filp, char __user *buf, size_t count,
loff_t* f_pos);
static ssize_t
rfm_filop_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos);
static long
rfm_filop_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
static int
rfm_filop_open(struct inode *inode, struct file *filp);
static int
rfm_filop_release(struct inode *inode, struct file *filp);
// 3.8 kernels and later make these obsolete due to changes in HOTPLUG
// we keep them for compatibility with earlier kernel versions...
#ifndef __devinit
#define __devinit
#endif
#ifndef __devexit
#define __devexit
#endif
#ifndef __devexit_p
#define __devexit_p(x) x
#endif
static struct rfm12_spi_message*
rfm_claim_spi_message(struct rfm12_data* rfm12)
{
u8 i;
struct rfm12_spi_message* rv = NULL;
for (i=0; i < NUM_MAX_CONCURRENT_MSG; i++) {
if (0 == (rfm12->free_spi_msgs & (1 << i))) {
rfm12->free_spi_msgs |= (1 << i);
rv = &rfm12->spi_msgs[i];
rv->pos = i;
rv->context = rfm12;
break;
}
}
return rv;
}
static void
rfm_unclaim_spi_message(struct rfm12_spi_message* spi_msg)
{
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
rfm12->free_spi_msgs &= ~(1 << spi_msg->pos);
}
struct spi_transfer
rfm_make_spi_transfer(struct rfm12_data* rfm12,
uint16_t cmd, u8* tx_buf, u8* rx_buf)
{
struct spi_transfer tr = {
.tx_buf = tx_buf,
.rx_buf = rx_buf,
.len = 2,
.cs_change = 0,
.bits_per_word = 0,
.delay_usecs = 0,
.speed_hz = rfm12->spi_speed_hz
};
tx_buf[0] = (cmd >> 8) & 0xff;
tx_buf[1] = cmd & 0xff;
return tr;
}
struct spi_transfer
rfm_control_spi_transfer(struct rfm12_data* rfm12,
struct rfm12_spi_message* msg, u8 pos, uint16_t cmd)
{
return rfm_make_spi_transfer(rfm12, cmd,
msg->spi_tx + 2*pos,
msg->spi_rx + 2*pos);
}
static void
rfm_spi_completion_common(struct rfm12_spi_message* msg)
{
rfm_unclaim_spi_message(msg);
}
static void
__rfm_generic_spi_completion_handler(void *arg)
{
struct rfm12_spi_message* spi_msg =
(struct rfm12_spi_message*)arg;
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
if (RFM12_STATE_NO_CHANGE != spi_msg->spi_finish_state)
rfm12->state = spi_msg->spi_finish_state;
rfm_spi_completion_common(spi_msg);
}
static void
rfm_generic_spi_completion_handler(void *arg)
{
unsigned long flags;
struct rfm12_spi_message* spi_msg =
(struct rfm12_spi_message*)arg;
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
spin_lock_irqsave(&rfm12->lock, flags);
__rfm_generic_spi_completion_handler(arg);
spin_unlock_irqrestore(&rfm12->lock, flags);
}
static int
rfm_send_generic_async_cmd(struct rfm12_data* rfm12, uint16_t* cmds,
int num_cmds, uint16_t delay_usecs,
void (*callback)(void*),
rfm12_state_t finish_state)
{
int err, i;
struct rfm12_spi_message* spi_msg;
spi_msg = rfm_claim_spi_message(rfm12);
if (NULL == spi_msg)
return -EBUSY;
spi_msg->spi_finish_state = finish_state;
spi_message_init(&spi_msg->spi_msg);
spi_msg->spi_msg.complete =
(NULL == callback) ? rfm_generic_spi_completion_handler : callback;
spi_msg->spi_msg.context = (void*)spi_msg;
spi_msg->spi_transfers[0] =
rfm_control_spi_transfer(rfm12, spi_msg, 0, cmds[0]);
i=1;
if (num_cmds > 1) {
for (i=1; i<num_cmds; i++) {
spi_msg->spi_transfers[i-1].cs_change = 1;
spi_msg->spi_transfers[i-1].delay_usecs = delay_usecs;
spi_message_add_tail(&spi_msg->spi_transfers[i-1], &spi_msg->spi_msg);
spi_msg->spi_transfers[i] =
rfm_control_spi_transfer(rfm12, spi_msg, i, cmds[i]);
}
}
spi_message_add_tail(&spi_msg->spi_transfers[i-1], &spi_msg->spi_msg);
err = spi_async(rfm12->spi, &spi_msg->spi_msg);
if (err)
__rfm_generic_spi_completion_handler((void*)spi_msg);
return err;
}
static rfm12_module_type_t
rfm_detect_module_type(struct rfm12_data* rfm12)
{
rfm12_module_type_t module_type = RFM12_TYPE_RF12;
int err;
u8* sync_ptr;
u8 tx_buf[4];
u8 rx_buf[2];
u8 init_synchro[] = { 0xAA, 0x55, 0};
err = 0;
sync_ptr = init_synchro;
rfm12->spi_speed_hz = 250000; // 250KHz should be slow enough
while (0 == err && 0 != *sync_ptr) {
int num_tries = 10;
while (0 == err && --num_tries > 0) {
struct spi_message msg;
struct spi_transfer tr, tr2;
rx_buf[0] = 0;
rx_buf[1] = 0;
tr = rfm_make_spi_transfer(
rfm12,
0x2F00 | (((u16)RFM69_MASK_REGWRITE) << 8) | *sync_ptr,
tx_buf,
NULL
);
tr.cs_change = 1;
tr2 = rfm_make_spi_transfer(
rfm12,
0x2F00,
tx_buf+2,
rx_buf
);
spi_message_init(&msg);
spi_message_add_tail(&tr, &msg);
spi_message_add_tail(&tr2, &msg);
err = spi_sync(rfm12->spi, &msg);
if (0 != err || rx_buf[1] == *sync_ptr) {
break;
}
}
if (0 != err) {
goto pError;
}
if (0 < num_tries) {
module_type = RFM12_TYPE_RF69;
} else {
module_type = RFM12_TYPE_RF12;
}
sync_ptr++;
}
pError:
return module_type;
}
static const char*
rfm_string_for_module_type(rfm12_module_type_t module_type)
{
const char* name = "RFM12(B)";
switch (module_type) {
case RFM12_TYPE_RF69:
name = "RFM69W";
break;
default:
break;
}
return name;
}
static int
rfm_start_receiving_common(struct rfm12_data* rfm12)
{
rfm12->state = RFM12_STATE_LISTEN;
rfm12->in_cur_num_bytes = 0;
return 0;
}
static int
rfm_reset(struct rfm12_data* rfm12)
{
rfmXX_setup(rfm12);
rfm_begin_sending_or_receiving(rfm12);
return 0;
}
static int
rfm_consume_received_byte(struct rfm12_data* rfm12, u8 recvd_byte)
{
uint16_t packet_finished = 0;
if (RFM12_STATE_LISTEN == rfm12->state) {
rfm12->state = RFM12_STATE_RECV;
}
if (NULL != rfm12->in_buf) {
if (rfm12->in_buf_pos < (rfm12->in_buf + DATA_BUF_SIZE)) {
*rfm12->in_buf_pos++ = recvd_byte;
if (0 == rfm12->in_cur_num_bytes)
rfm12->crc16 = rfm_crc16_update(~0, rfm12->group_id);
rfm12->crc16 = rfm_crc16_update(rfm12->crc16, recvd_byte);
}
if (1 == rfm12->in_cur_num_bytes) {
rfm12->in_cur_len_pos = rfm12->in_buf_pos-1;
if (*rfm12->in_cur_len_pos > RF_MAX_DATA_LEN)
// if the data len is larger than RF_MAX_DATA_LEN, we
// ignore this packet early.
*rfm12->in_cur_len_pos = 0;
}
if (1 < rfm12->in_cur_num_bytes) {
// +2 ... those are the CRC bytes, +2 for header & length
if (rfm12->in_cur_num_bytes+1 >= (*rfm12->in_cur_len_pos + RF_EXTRA_LEN) ||
rfm12->in_cur_num_bytes+1 >= (RF_MAX_LEN)) {
(void)rfm_finish_receiving(rfm12, 0);
packet_finished = 1;
}
}
if (!packet_finished) {
rfm12->in_cur_num_bytes++;
rfm_update_rxtx_watchdog(rfm12, 0);
}
} else {
(void)rfm_finish_receiving(rfm12, 1);
packet_finished = 1;
}
return packet_finished;
}
static int
rfm_finish_receiving(struct rfm12_data* rfm12, int skip_packet)
{
int err = 0, num_bytes;
if (RFM12_STATE_RECV == rfm12->state) {
rfm12->state = RFM12_STATE_RECV_FINISH;
num_bytes = rfm12->in_cur_num_bytes;
rfm12->in_cur_num_bytes = 0;
rfm_update_rxtx_watchdog(rfm12, 1);
if (0 == skip_packet && 0 == rfm12->crc16) {
rfm12->pkts_recvd++;
rfm12->bytes_recvd += num_bytes - 3;
// if we are in jeenode-compatible mode and this packet
// needs an ACK, we send one automatically to the source node.
if (INTERPRETS_JEENODE_PROTOCOL(rfm12) && rfm12->jee_autoack && CAN_SEND_BYTES_OF_LENGTH(0)) {
u8 hdr = rfm12->in_cur_len_pos[-1];
if ((hdr & RFM12B_JEE_HDR_ACK_BIT) && !(hdr & RFM12B_JEE_HDR_CTL_BIT)) {
u8 snd_hdr = 0;
if (hdr & RFM12B_JEE_HDR_DST_BIT) {
// if this was only sent to us, send ACK as broadcast
snd_hdr |= RFM12B_JEE_HDR_CTL_BIT | RFM12B_JEE_ID_FROM_HDR(rfm12->jee_id);
} else {
// if this was a broadcast, send ACK directly to source node.
snd_hdr |= RFM12B_JEE_HDR_CTL_BIT | RFM12B_JEE_HDR_DST_BIT |
RFM12B_JEE_ID_FROM_HDR(hdr);
}
*rfm12->out_cur_end++ = snd_hdr;
*rfm12->out_cur_end++ = (u8)0;
rfm_apply_crc16(rfm12, rfm12->out_cur_end-2, 2);
rfm12->out_cur_end += 2; // crc16 = 2 bytes
}
}
rfm12->in_cur_end = rfm12->in_buf_pos;
wake_up_interruptible(&rfm12->wait_read);
} else {
if (0 == skip_packet && 0 != rfm12->crc16)
rfm12->num_recv_crc16_fail++;
rfm12->in_buf_pos = rfm12->in_cur_end;
}
err = rfmXX_finish_send_recv_common(rfm12);
}
return err;
}
static void
rfm_begin_sending_or_receiving(struct rfm12_data* rfm12)
{
if (RFM12_STATE_IDLE == rfm12->state) {
if (!rfmXX_try_sending(rfm12))
rfmXX_start_receiving(rfm12);
}
}
static void
rfm_rxtx_watchdog_expired(unsigned long ptr)
{
unsigned long flags;
struct rfm12_data* rfm12 = (struct rfm12_data*)ptr;
spin_lock_irqsave(&rfm12->lock, flags);
if (RFM12_STATE_RECV >= rfm12->state &&
RFM12_STATE_LISTEN <= rfm12->state) {
rfm12->num_recv_timeouts++;
(void)rfm_finish_receiving(rfm12, 1);
} else if (RFM12_STATE_SEND_PRE1 <= rfm12->state &&
RFM12_STATE_SEND_TAIL3 >= rfm12->state) {
rfm12->num_send_timeouts++;
(void)rfm_finish_sending(rfm12, 0);
}
rfm12->rxtx_watchdog_running = 0;
spin_unlock_irqrestore(&rfm12->lock, flags);
}
static void
rfm_update_rxtx_watchdog(struct rfm12_data* rfm12, u8 cancelTimer)
{
if (rfm12->rxtx_watchdog_running) {
if (cancelTimer) {
// not del_timer_sync, because we might be called within our
// own expiration handler
del_timer(&rfm12->rxtx_watchdog);
rfm12->rxtx_watchdog_running = 0;
} else
mod_timer(&rfm12->rxtx_watchdog,
jiffies + RXTX_WATCHDOG_JIFFIES);
} else if (!cancelTimer) {
init_timer(&rfm12->rxtx_watchdog);
rfm12->rxtx_watchdog.expires = jiffies + RXTX_WATCHDOG_JIFFIES;
rfm12->rxtx_watchdog.data = (unsigned long)rfm12;
rfm12->rxtx_watchdog.function = rfm_rxtx_watchdog_expired;
add_timer(&rfm12->rxtx_watchdog);
rfm12->rxtx_watchdog_running = 1;
}
}
static void
rfm_apply_crc16(struct rfm12_data* rfm12, unsigned char* ptr, unsigned len)
{
u16 i, crc16 = ~0;
if (0 != rfm12->group_id)
crc16 = rfm_crc16_update(~0, rfm12->group_id);
for (i=0; i<len; i++)
crc16 = rfm_crc16_update(crc16, ptr[i]);
// crc16
ptr[len] = crc16 & 0xFF;
ptr[len+1] = (crc16 >> 8) & 0xFF;
}
static u16
rfm_crc16_update(u16 crc, u8 b)
{
int i=0;
crc ^= b;
for (i = 0; i < 8; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xa001;
else
crc = (crc >> 1);
}
return crc;
}
static void
rfm_finish_send_or_recv_callback(void *arg)
{
unsigned long flags;
struct rfm12_spi_message* spi_msg =
(struct rfm12_spi_message*)arg;
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
u8 should_release = 0;
spin_lock_irqsave(&rfm12->lock, flags);
rfm_spi_completion_common(spi_msg);
if ((should_release = rfm12->should_release)) {
spin_unlock_irqrestore(&rfm12->lock, flags);
rfm_release_when_safe(rfm12);
spin_lock_irqsave(&rfm12->lock, flags);
} else {
rfm_begin_sending_or_receiving(rfm12);
}
spin_unlock_irqrestore(&rfm12->lock, flags);
if (!should_release) {
platform_irq_handled(rfm12->irq_identifier);
}
}
static void
rfm_finish_trysend(struct rfm12_data* rfm12)
{
rfm12->retry_sending_running = 0;
rfm12->trysend = 0;
}
static void
rfm_start_trysend_retry_timer(struct rfm12_data* rfm12)
{
init_timer(&rfm12->retry_sending_timer);
rfm12->retry_sending_timer.expires = jiffies + TRYSEND_RETRY_JIFFIES;
rfm12->retry_sending_timer.data = (unsigned long)rfm12;
rfm12->retry_sending_timer.function = rfm_trysend_retry_timer_expired;
add_timer(&rfm12->retry_sending_timer);
rfm12->retry_sending_running = 1;
}
static void
rfm_trysend_retry_timer_expired(unsigned long ptr)
{
unsigned long flags;
struct rfm12_data* rfm12 = (struct rfm12_data*)ptr;
spin_lock_irqsave(&rfm12->lock, flags);
if (rfm12->retry_sending_running) {
rfm_finish_trysend(rfm12);
rfmXX_try_sending(rfm12);
} else {
rfm_finish_trysend(rfm12);
}
spin_unlock_irqrestore(&rfm12->lock, flags);
}
static int
rfm_finish_sending(struct rfm12_data* rfm12, int success)
{
int err = 0, len = 0;
rfm_update_rxtx_watchdog(rfm12, 1);
if (success) {
len = rfm12->out_buf[1] + RF_EXTRA_LEN;
memmove(rfm12->out_buf,
rfm12->out_buf + len,
DATA_BUF_SIZE - len
);
rfm12->out_cur_end -= len;
rfm12->out_buf_pos = rfm12->out_buf;
rfm12->pkts_sent++;
rfm12->bytes_sent += len - RF_EXTRA_LEN;
wake_up_interruptible(&rfm12->wait_write);
}
err = rfmXX_finish_send_recv_common(rfm12);
return err;
}
static void
rfm_stop_listening_and_send_callback(void *arg)
{
unsigned long flags;
struct rfm12_spi_message* spi_msg =
(struct rfm12_spi_message*)arg;
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
spin_lock_irqsave(&rfm12->lock, flags);
rfm_spi_completion_common(spi_msg);
rfm_begin_sending_or_receiving(rfm12);
spin_unlock_irqrestore(&rfm12->lock, flags);
}
static void
rfm_release_when_safe(struct rfm12_data* rfm12)
{
rfm12->should_release = 0;
platform_irq_cleanup(rfm12->irq_identifier);
rfm_update_rxtx_watchdog(rfm12, 1);
(void)rfmXX_disable_hardware_on_release(rfm12);
}
static void
rfm_disable_hardware_on_release_handler(void *arg)
{
unsigned long flags;
int dofree = 0;
struct rfm12_spi_message* spi_msg =
(struct rfm12_spi_message*)arg;
struct rfm12_data* rfm12 =
(struct rfm12_data*)spi_msg->context;
spin_lock_irqsave(&rfm12->lock, flags);
rfm_spi_completion_common(spi_msg);
kfree(rfm12->in_buf);
rfm12->in_buf = rfm12->in_buf_pos = NULL;
rfm12->out_buf = rfm12->out_buf_pos = NULL;
dofree = (rfm12->spi == NULL);
spin_unlock_irqrestore(&rfm12->lock, flags);
if (dofree) {
kfree(rfm12);
}
}
/* RFMXX Wrappers ----------------------------------------------*/
static int
rfmXX_setup(struct rfm12_data* rfm12)
{
int err = 0;
switch (rfm12->module_type) {
case RFM12_TYPE_RF12:
err = rfm12_setup(rfm12);
break;
case RFM12_TYPE_RF69:
err = rfm69_setup(rfm12);
break;
default:
err = -EINVAL;
break;
}
if (0 == err) {
printk(KERN_INFO RFM12B_DRV_NAME
": transceiver <0x%x> (%s) settings now: "
"group %d, band %d, bit rate 0x%.2x (%d bps), "
"jee id: %d, jee autoack: %d.\n",
(unsigned)rfm12->irq_identifier,
rfm_string_for_module_type(rfm12->module_type),
rfm12->group_id, rfm12->band_id,
rfm12->bit_rate, RFM12B_BIT_RATE_FROM_BYTE(rfm12->bit_rate),
rfm12->jee_id, rfm12->jee_autoack);
}
return err;
}
static int
rfmXX_try_sending(struct rfm12_data* rfm12)
{
int rv = 0;
switch (rfm12->module_type) {
case RFM12_TYPE_RF12:
rv = rfm12_try_sending(rfm12);
break;
case RFM12_TYPE_RF69:
rv = rfm69_try_sending(rfm12);
break;