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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
|
/*
* MTS-IO Controller
*
* Copyright (C) 2014 by Multi-Tech Systems
* Copyright (C) 2016 by Multi-Tech Systems
* Copyright (C) 2019 by Multi-Tech Systems
* Copyright (C) 2020 by Multi-Tech Systems
*
* This program 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/ioctl.h>
#include <linux/input.h>
#include <linux/cdev.h>
#include <linux/clk.h>
#include <linux/sched.h>
#include <linux/reboot.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/bitops.h>
#include <linux/spi/spi.h>
#include <linux/kmod.h>
#include <linux/ctype.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/firmware.h>
#include "mts_io_module.h"
#include "mts_io.h"
#include "buttons.h"
#define PLATFORM_NAME "mts-io"
#define LED_LS_CONTROLLABLE 0
/* To use AT91 pinctrl to set pull-up
* and pull-down, we use device tree.
* This seems the easiest way. Most
* drivers implement pinctrl in the
* probe function, which causes
* device tree to be read, and ultimately
* causes the at91 pinctrl code to
* set the pull-up/pulldown registers
* as specified in the mts-io
* pinctrl section of device tree.
* .compatible lets us find our device
* tree entries. Probe and remove
* are mandatory even though our code
* is a no-op. This probe code was based
* on the i2c-gpio driver.
*/
static const struct of_device_id mts_io_dt_ids[] = {
{ .compatible = "mts,mts-io", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mts_io_dt_ids);
/* on-board EEPROM */
static struct mts_id_eeprom_layout id_eeprom;
#include "adc.c"
/*
* We must call platform_set_drvdata, or else the
* devres_head for the driver has junk in it, and
* this causes warning stack dumps in dd.c,
* really_probe() function.
*
* priv is the private data structure for the driver,
* and could be used by our driver, but for now,
* it is unused.
*/
static int mts_io_probe(struct platform_device *pdev)
{
int ret = 0;
if (!DEVICE_CAPA(id_eeprom.capa, CAPA_ADC)) {
ret = mts_io_board_adc_probe(pdev);
}
return ret;
}
static int mts_io_remove(struct platform_device *pdev)
{
return 0;
}
/*
* minimal required functions for the platform driver.
*/
static struct platform_driver mts_io_driver = {
.driver = {
.name = "mts-io",
.of_match_table = of_match_ptr(mts_io_dt_ids),
},
.probe = mts_io_probe,
.remove = mts_io_remove,
};
static uint8_t mts_hw_version;
struct platform_device *mts_io_platform_device;
EXPORT_SYMBOL(mts_io_platform_device);
static struct attribute_group *attr_group;
static struct attribute_group *attr_group_lora; // on-board lora peripheral to be stored in the lora/ sub-directory
static struct gpio_pin *gpio_pins;
DEFINE_MUTEX(mts_io_mutex);
static unsigned int *timings_data = NULL;
static unsigned int timings_data_size = 0;
static unsigned int timings_data_index = 0;
static time_t timings_data_stop_seconds = 0;
static struct timer_list radio_reset_timer;
static volatile int radio_reset_timer_is_start = 0;
static struct timer_list radio_reset_available_timer;
static volatile int radio_reset_available_timer_is_start = 0;
static time_t time_now_secs(void);
/* generic GPIO support */
#include "gpio.c"
#include "spi.c"
/* generic Button support */
//#include "buttons.c"
/* reset button handling */
#define RESET_CHECK_PER_SEC 8
#define RESET_INTERVAL (HZ / RESET_CHECK_PER_SEC)
#define RESET_HOLD_COUNT (RESET_CHECK_PER_SEC * 3)
#define RESET_LONG_HOLD_COUNT (RESET_CHECK_PER_SEC * 30)
bool sent_extra_long = false;
/*
* This function takes the product_id and tries to check
* for a modem. If there is an error, assume there is a
* modem. Saying there is a modem when there is not
* just results in a slower boot.
* If no hyphen in product ID, or no product ID,
* assume we have a radio.
* If there is a hyphen test the character after the
* first hyphen:
* If the character is numeric, we have no modem.
* if the chracter is not a B, we have a modem.
* If the character is a B, and it is followed
* by a numeric, we have no modem.
* If the B is the last character or is followed
* by a numeric, we have no modem.
* All other cases, we have a modem.
*/
int
mts_has_radio(const char *product_id, size_t len)
{
char *p;
if (!product_id || ! *product_id)
return 1; /* No Product ID? */
p = memchr(product_id,'-',len);
if (p) { /* Found a hyphen */
log_debug("Found hyphen");
p++;
if (p >= product_id+len) {
log_debug("End of string -- hyphen");
return 1; /* Last character was hyphen */
}
if (isdigit(*p)) {
log_debug("Found digit after hypen");
return 0; /* Modem name never starts with a digit */
}
if (*p != 'B') {
log_debug("Found neither B nor digit after hypen");
return 1; /* Modem starting with a letter, but not B */
}
/* Found a B */
p++;
if (p >= product_id+len) {
log_debug("B at end of product-id string");
return 1; /* Last character was B */
}
if (isdigit(*p)) {
log_debug("B followed by digit after hyphen - no modem");
return 0; /* B[numeric] is MTR Build number */
}
log_debug("B followed by non-digit after hyphen - has modem");
return 1; /* B[non-numeric] so assume a modem that starts with B */
} /* End of found hyphen case */
log_debug("Undefined product-id - has modem");
return 1; /* Product id invalid or empty, so instantiate a radio anyway */
}
EXPORT_SYMBOL(mts_has_radio);
/* active-low socket modem reset */
static ssize_t mts_attr_store_radio_reset(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int value; /* 0 = normal reset; -1 = forced reset */
int err;
struct gpio_pin *pin;
if (sscanf(buf, "%i", &value) != 1) {
return -EINVAL;
}
if (value != 0 && value != -1) {
return -EINVAL;
}
/* check reset timings is enabled */
if (value != -1 && NULL != timings_data) {
/* check reset timer is started */
if (radio_reset_timer_is_start == 1) {
log_info("radio reset timer is running. \n");
return count;
}
/* check reset timer available is started */
if (radio_reset_available_timer_is_start == 1) {
del_timer(&radio_reset_available_timer);
radio_reset_available_timer_is_start = 0;
}
/* reset timer not started, start it */
mod_timer(&radio_reset_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000));
//log_info("radio reset timer is start = [%d]\n", time_now_secs());
/* save timings_data_stop_seconds */
timings_data_stop_seconds = timings_data[timings_data_index] + time_now_secs();
radio_reset_timer_is_start = 1;
}
log_info("radio is reset\n");
pin = gpio_pin_by_attr_name("radio-reset");
if (!pin) {
return -ENODEV;
}
mutex_lock(&mts_io_mutex);
// 250ms low reset
err = reset_gpio_pin(pin, 250, 0);
mutex_unlock(&mts_io_mutex);
if (err) {
return err;
}
return count;
}
static DEVICE_ATTR_MTS(dev_attr_radio_reset, "radio-reset",
mts_attr_show_gpio_pin, mts_attr_store_radio_reset);
/* shared gpio attributes */
static DEVICE_ATTR_MTS(dev_attr_radio_power, "radio-power",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
/* backoff-timers */
static time_t time_now_secs(void)
{
struct timespec ts = current_kernel_time();
return ts.tv_sec;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static void radio_reset_available_timer_callback( struct timer_list *data )
#else
static void radio_reset_available_timer_callback( unsigned long data )
#endif
{
/* do your timer stuff here */
//log_info("radio_reset_available_timer_callback\n");
//log_info("radio reset available timer is stop = [%d]\n", time_now_secs());
/* zero timings_data_index */
timings_data_index = 0;
//log_info("timings data index is zero = [%d]\n", timings_data_index);
radio_reset_available_timer_is_start = 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static void radio_reset_timer_callback( struct timer_list *data )
#else
static void radio_reset_timer_callback( unsigned long data )
#endif
{
/* do your timer stuff here */
//log_info("radio_reset_timer_callback\n");
//log_info("radio reset timer is stop = [%d]\n", time_now_secs());
/* increment timings_data_index */
timings_data_index++;
if(timings_data_index >= timings_data_size) {
timings_data_index = timings_data_size-1;
}
//log_info("timings data index = [%d]\n", timings_data_index);
/* reset available timer not started, start it */
mod_timer(&radio_reset_available_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000));
//log_info("radio reset available timer is start = [%d]\n", time_now_secs());
radio_reset_available_timer_is_start = 1;
radio_reset_timer_is_start = 0;
}
static ssize_t mts_attr_store_radio_reset_backoffs(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
char *timings_data_str = NULL;
const char delimiter[] = " ";
char * pch = NULL;
unsigned int size = 0;
/* free previous timings_data */
if (NULL != timings_data) {
/* stop timers */
del_timer(&radio_reset_timer);
del_timer(&radio_reset_available_timer);
timings_data_index = 0;
radio_reset_timer_is_start = 0;
radio_reset_available_timer_is_start = 0;
//log_info("free previous timings_data\n");
kfree(timings_data);
timings_data = NULL;
timings_data_size = 0;
}
/* make a copy */
if( NULL == (timings_data_str = kzalloc((strlen(buf) + 1), GFP_KERNEL)) ){
log_error("can`t allocate memory\n");
return -EINVAL;
}
//log_info("radio_reset_backoffs buf: [%s]", buf);
strncpy(timings_data_str, buf, (strlen(buf) + 1));
/* get number of tokens */
while (NULL != (pch = strsep (&timings_data_str, delimiter))) {
int value = 0;
sscanf(pch, "%d", &value);
//log_info("radio reset backoffs pch = [%s]\n", pch);
if (value > 0){
size++;
if (NULL == timings_data) {
/* make alloc */
if (NULL == (timings_data = kmalloc(sizeof(unsigned int), GFP_KERNEL))) {
log_error("radio reset backoffs can`t allocate memory\n");
goto free;
}
} else {
/* make realloc */
if (NULL == (timings_data = krealloc(timings_data, size * sizeof(unsigned int), GFP_KERNEL))) {
log_error("radio reset backoffs can`t allocate memory\n");
goto free;
}
}
/* save timings data */
sscanf(pch, "%d", &timings_data[size-1]);
}
}
timings_data_size = size;
//log_info("timings_data_size = %d\n", timings_data_size);
if (NULL != timings_data_str) {
/* free timings_data_str */
/* never get here in happy path */
kfree(timings_data_str);
}
return count;
free:
if (NULL != timings_data_str) {
/* free timings_data_str */
kfree(timings_data_str);
}
if (NULL != timings_data) {
kfree(timings_data);
timings_data = NULL;
timings_data_size = 0;
}
return -EINVAL;
}
static ssize_t mts_attr_store_radio_reset_backoffs_index(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int value;
if (sscanf(buf, "%d", &value) != 1) {
return -EINVAL;
}
if ((value < 0) || (value >= timings_data_size)) {
log_error("incorrect data\n");
return -EINVAL;
}
/* stop timers */
del_timer(&radio_reset_timer);
del_timer(&radio_reset_available_timer);
radio_reset_timer_is_start = 0;
radio_reset_available_timer_is_start = 0;
timings_data_index = value;
return count;
}
static ssize_t mts_attr_show_radio_reset_backoffs(struct device *dev,
struct device_attribute *attr, char *buf)
{
int ret = 0;
size_t i = 0;
size_t buf_left = 0;
if (NULL != timings_data) {
for(i = 0; i < timings_data_size; ++i) {
buf_left = PAGE_SIZE - ret;
ret += snprintf(buf += strlen(buf), buf_left, "%d ", timings_data[i]);
}
}
if (ret > 0) {
ret -= 1;
}
return ret;
}
static ssize_t mts_attr_show_radio_reset_backoff_index(struct device *dev,
struct device_attribute *attr, char *buf)
{
ssize_t value;
if (strcmp(attr->attr.name, "radio-reset-backoff-index") == 0) {
value = sprintf(buf, "%d", timings_data_index);
}
else {
log_error("attribute '%s' not found", attr->attr.name);
value = -1;
}
return value;
}
static ssize_t mts_attr_show_radio_reset_backoff_seconds(struct device *dev,
struct device_attribute *attr, char *buf)
{
ssize_t value;
if (strcmp(attr->attr.name, "radio-reset-backoff-seconds") == 0) {
if (radio_reset_timer_is_start == 1) {
value = sprintf(buf, "%lu", (timings_data_stop_seconds - time_now_secs()));
} else {
value = sprintf(buf, "%d", 0);
}
} else {
log_error("attribute '%s' not found", attr->attr.name);
value = -1;
}
return value;
}
static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoffs, "radio-reset-backoffs",
mts_attr_show_radio_reset_backoffs, mts_attr_store_radio_reset_backoffs);
static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoff_index, "radio-reset-backoff-index",
mts_attr_show_radio_reset_backoff_index, mts_attr_store_radio_reset_backoffs_index);
static DEVICE_ATTR_RO_MTS(dev_attr_radio_reset_backoff_seconds, "radio-reset-backoff-seconds",
mts_attr_show_radio_reset_backoff_seconds);
/* shared gpio-based LEDs */
static DEVICE_ATTR_MTS(dev_attr_led_status, "led-status",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_a_gpio, "led-a",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_b_gpio, "led-b",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_cd_gpio, "led-cd",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_c_gpio, "led-c",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_sig1_gpio, "led-sig1",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_sig2_gpio, "led-sig2",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_sig3_gpio, "led-sig3",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_d_gpio, "led-d",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_e_gpio, "led-e",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
/* eeprom info */
static ssize_t mts_attr_show_product_info(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int i;
ssize_t value;
if (strcmp(attr->attr.name, "vendor-id") == 0) {
value = sprintf(buf, "%.32s\n", id_eeprom.vendor_id);
} else if (strcmp(attr->attr.name, "product-id") == 0) {
value = sprintf(buf, "%.32s\n", id_eeprom.product_id);
} else if (strcmp(attr->attr.name, "has-radio") == 0) {
value = sprintf(buf, "%1d\n",
mts_has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id));
} else if (strcmp(attr->attr.name, "device-id") == 0) {
value = sprintf(buf, "%.32s\n", id_eeprom.device_id);
} else if (strcmp(attr->attr.name, "uuid") == 0) {
//Loop Through UUID Bytes and print them in HEX
for(i = 0; i < 16; i++) {
value = sprintf(buf, "%02X", id_eeprom.uuid[i]);
if(value == -1) {
return value;
}
buf += value;
}
value = sprintf(buf, "\n");
if(value == -1) {
return value;
}
value = 33; //16*2 (ASCII HEX) + 1 ('\n')
} else if (strcmp(attr->attr.name, "hw-version") == 0) {
value = sprintf(buf, "%.32s\n", id_eeprom.hw_version);
} else if (strcmp(attr->attr.name, "imei") == 0) {
value = sprintf(buf, "%.32s\n", id_eeprom.imei);
} else if (strcmp(attr->attr.name, "mac-wifi") == 0) {
value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n",
id_eeprom.mac_wifi[0],
id_eeprom.mac_wifi[1],
id_eeprom.mac_wifi[2],
id_eeprom.mac_wifi[3],
id_eeprom.mac_wifi[4],
id_eeprom.mac_wifi[5]);
} else if (strcmp(attr->attr.name, "mac-bluetooth") == 0) {
value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n",
id_eeprom.mac_bluetooth[0],
id_eeprom.mac_bluetooth[1],
id_eeprom.mac_bluetooth[2],
id_eeprom.mac_bluetooth[3],
id_eeprom.mac_bluetooth[4],
id_eeprom.mac_bluetooth[5]);
} else if (strcmp(attr->attr.name, "mac-eth") == 0) {
value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n",
id_eeprom.mac_addr[0],
id_eeprom.mac_addr[1],
id_eeprom.mac_addr[2],
id_eeprom.mac_addr[3],
id_eeprom.mac_addr[4],
id_eeprom.mac_addr[5]);
} else if (strcmp(attr->attr.name, "lora-eui") == 0) {
value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
id_eeprom.lora_eui[0],
id_eeprom.lora_eui[1],
id_eeprom.lora_eui[2],
id_eeprom.lora_eui[3],
id_eeprom.lora_eui[4],
id_eeprom.lora_eui[5],
id_eeprom.lora_eui[6],
id_eeprom.lora_eui[7]);
}
else {
log_error("attribute '%s' not found", attr->attr.name);
value = -1;
}
return value;
}
static DEVICE_ATTR_RO_MTS(dev_attr_vendor_id, "vendor-id",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_product_id, "product-id",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_has_radio, "has-radio",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_device_id, "device-id",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_uuid, "uuid",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_hw_version, "hw-version",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_imei, "imei",
mts_attr_show_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_eth_mac, "mac-eth",
mts_attr_show_product_info);
static int get_radio_model_from_product_id(void) {
int rc = RADIO_UNKNOWN;
if (strstr(id_eeprom.product_id, "LEU1")) rc = RADIO_LEU1;
else if (strstr(id_eeprom.product_id, "LNA3")) rc = RADIO_LNA3;
else if (strstr(id_eeprom.product_id, "LEU3")) rc = RADIO_LEU3;
// Add other radios as needed.
return rc;
}
/* include on-board lora peripheral */
#include "mts_lora.c"
/* include per-device pins and attributes */
#include "machine/mtcdt.c"
#include "machine/mtcap.c"
#include "machine/mtr.c"
#include "machine/mths.c"
#include "machine/mtcpm.c"
#include "machine/mt100eocg.c"
/* include capabilities sub-directory support */
#include "mts_capab.c"
struct attribute **freelater = NULL; // Storage to free when driver is unloaded.
static int
mts_id_eeprom_load(void)
{
int i, j = 0;
char buf[64] = {0};
char* ptr;
int attr_blength; // Byte length of base attribute array
int current_blength; // Current length in bytes of attribute array
int current_count; // Number of items in array
struct attribute **all_attrs = NULL;
char *tmp;
int noradio;
int ret;
const struct firmware* fw = NULL;
/* Attempt to load the mts-io driver */
if((ret = request_firmware_direct(&fw, "0-0056/eeprom", &mts_io_platform_device->dev)) == 0) {
if(fw->size == sizeof(id_eeprom)) {
memcpy(&id_eeprom, fw->data, sizeof(id_eeprom));
log_info("Platform EEPROM contents loaded");
} else {
log_error("Invalid platform EEPROM length (%d)", fw->size);
return -EINVAL;
}
release_firmware(fw);
} else {
log_error("Unable to load EEPROM contents (%d)", ret);
return -ENODEV;
}
// If we are an MTCPM, the base board sets the radio existance.
if (strncmp(id_eeprom.hw_version,HW_VERSION_MTCPM_DASH,sizeof HW_VERSION_MTCPM_DASH) != 0) {
noradio = ! mts_has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id);
log_debug("mts_id_eeprom: noradio=%d",noradio);
}
if (((tmp=HW_VERSION_MTCAP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) ||
((tmp=HW_VERSION_MTCAP_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) ||
((tmp=HW_VERSION_MTCAP_0_2),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) {
/* See if we have no radio, and if so, prune out the stuff that follows */
if(noradio) {
struct attribute **ap = mtcap_0_0_platform_attribute_group.attrs;
while(1) {
if(ap[j] == NULL) {
log_info("Did not find radio power attribute. Possible driver fault.");
break;
}
j++;
if (is_radio_power_attr_mtcap(ap[j])) {
log_info("Pruning radio feature from mts-io",j);
ap[j] = NULL;
break;
}
}
}
attr_group = &mtcap_0_0_platform_attribute_group;
gpio_pins = gpio_pins_mtcap_0_0;
set_buttons(default_buttons);
if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) {
attr_group_lora = &mtcap_0_0_lora_attribute_group;
}
log_info("detected board %s", tmp);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_0, strlen(HW_VERSION_MTR_0_0)) == 0) {
attr_group = &mtr_platform_attribute_group;
gpio_pins = gpio_pins_mtr_0_0;
set_buttons(default_buttons);
mts_hw_version = MTR_0_0;
log_info("detected board %s", HW_VERSION_MTR_0_0);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_1, strlen(HW_VERSION_MTR_0_1)) == 0) {
attr_group = &mtr_platform_attribute_group;
gpio_pins = gpio_pins_mtr_0_1;
set_buttons(default_buttons);
mts_hw_version = MTR_0_1;
log_info("detected board %s", HW_VERSION_MTR_0_1);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_0, strlen(HW_VERSION_MTRV1_0_0)) == 0) {
attr_group = &mtr_platform_attribute_group;
gpio_pins = gpio_pins_mtrv1_0_0;
set_buttons(default_buttons);
mts_hw_version = MTRV1_0_0;
log_info("detected board %s", HW_VERSION_MTRV1_0_0);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_1, strlen(HW_VERSION_MTRV1_0_1)) == 0) {
attr_group = &mtrv1_0_1_platform_attribute_group;
gpio_pins = gpio_pins_mtrv1_0_1;
set_buttons(default_buttons);
mts_hw_version = MTRV1_0_1;
log_info("detected board %s", HW_VERSION_MTRV1_0_1);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_2, strlen(HW_VERSION_MTRV1_0_2)) == 0) {
attr_group = &mtrv1_0_2_platform_attribute_group;
gpio_pins = gpio_pins_mtrv1_0_2;
set_buttons(default_buttons);
mts_hw_version = MTRV1_0_2;
log_info("detected board %s", HW_VERSION_MTRV1_0_2);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_3, strlen(HW_VERSION_MTRV1_0_3)) == 0) {
attr_group = &mtrv1_0_3_platform_attribute_group;
gpio_pins = gpio_pins_mtrv1_0_3;
set_buttons(default_buttons);
mts_hw_version = MTRV1_0_3;
log_info("detected board %s", HW_VERSION_MTRV1_0_3);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTHS_0_0, strlen(HW_VERSION_MTHS_0_0)) == 0) {
attr_group = &mths_0_0_platform_attribute_group;
gpio_pins = gpio_pins_mths_0_0;
set_buttons(mths_buttons_0_0);
mts_hw_version = MTHS_0_0;
log_info("detected board %s", HW_VERSION_MTHS_0_0);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTHS_0_1, strlen(HW_VERSION_MTHS_0_1)) == 0) {
attr_group = &mths_0_0_platform_attribute_group;
gpio_pins = gpio_pins_mths_0_1;
set_buttons(mths_buttons_0_0); /* Only one button version */
mts_hw_version = MTHS_0_1;
log_info("detected board %s", HW_VERSION_MTHS_0_1);
} else if (((tmp=HW_VERSION_MTCDT_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) ||
((tmp=HW_VERSION_MTCDTIP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) {
current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes;
current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */
/* See if we have no radio, and if so, prune out the stuff that follows */
if(noradio) {
struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs;
while(1) {
if(ap[j] == NULL) {
log_info("Did not find radio power attribute. Possible driver fault.");
break;
}
j++;
if (is_radio_power_attr_mtcdt(ap[j])) {
log_info("Pruning radio feature from mts-io",j);
ap[j] = NULL;
current_blength = attr_blength = j * sizeof (ap[j]);
break;
}
}
}
if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) {
attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes;
}
if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) {
attr_blength += sizeof mtcdt_0_1_gnss_attributes;
}
if (current_blength+(sizeof(struct attribute *)) != attr_blength) {
freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL);
current_count = current_blength/(sizeof (struct attribute *));
memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength);
if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) {
log_info("Adding WiFi/BT to mts-io driver");
memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes);
current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *));
}
if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) {
log_info("Adding GPS to mts-io driver");
attr_blength += sizeof mtcdt_0_1_gnss_attributes;
memcpy(all_attrs + current_count,mtcdt_0_1_gnss_attributes,sizeof mtcdt_0_1_gnss_attributes);
current_count += sizeof mtcdt_0_1_gnss_attributes / (sizeof (struct attribute *));
}
all_attrs[current_count] = (struct attribute *)NULL;
mtcdt_0_1_platform_attribute_group.attrs = all_attrs;
}
attr_group = &mtcdt_0_1_platform_attribute_group;
gpio_pins = gpio_pins_mtcdt_0_1;
set_buttons(default_buttons);
log_info("detected board %s", tmp);
} else if ((tmp=HW_VERSION_MTCDTIPHP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) {
current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes;
current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */
/* See if we have no radio, and if so, prune out the stuff that follows */
if(noradio) {
struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs;
while(1) {
if(ap[j] == NULL) {
log_info("Did not find radio power attribute. Possible driver fault.");
break;
}
j++;
if (is_radio_power_attr_mtcdt(ap[j])) {
log_info("Pruning radio feature from mts-io",j);
ap[j] = NULL;
current_blength = attr_blength = j * sizeof (ap[j]);
break;
}
}
}
if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) {
attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes;
}
if (current_blength+(sizeof(struct attribute *)) != attr_blength) {
freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL);
current_count = current_blength/(sizeof (struct attribute *));
memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength);
if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) {
log_info("Adding WiFi/BT to mts-io driver");
memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes);
current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *));
}
/* MTCDTIPHP does not have GPS reset/interrupt tied to the CPU
* so do not instantiate the GPS attributes */
all_attrs[current_count] = (struct attribute *)NULL;
mtcdt_0_1_platform_attribute_group.attrs = all_attrs;
}
attr_group = &mtcdt_0_1_platform_attribute_group;
gpio_pins = gpio_pins_mtcdt_0_1;
if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) {
attr_group_lora = &mtcdtiphp_0_0_lora_attribute_group;
}
set_buttons(default_buttons);
log_info("detected board %s", tmp);
} else if ((tmp=HW_VERSION_MTCDT_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) {
if(noradio) {
struct attribute **ap = mtcdt_platform_attribute_group.attrs;
while(1) {
if(ap[j] == NULL) {
log_info("Did not find radio power attribute. Possible driver fault.");
break;
}
j++;
if (is_radio_power_attr_mtcdt(ap[j])) {
log_info("Pruning radio feature from mts-io",j);
ap[j] = NULL;
break;
}
}
}
attr_group = &mtcdt_platform_attribute_group;
gpio_pins = gpio_pins_mtcdt_0_0;
mts_hw_version = MTCDT_0_0;
set_buttons(default_buttons);
log_info("detected board %s", tmp);
} else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTCPM_0_0, strlen(HW_VERSION_MTCPM_0_0)) == 0) {
attr_group = &mtcpm_platform_attribute_group;
gpio_pins = gpio_pins_mtcpm;
set_buttons(default_buttons);
mts_hw_version = MTCPM_0_0;
log_info("detected board %s", HW_VERSION_MTCPM_0_0);
} else if (strncmp(id_eeprom.product_id, PRODUCT_ID_MT100EOCG, strlen(PRODUCT_ID_MT100EOCG)) == 0) {
attr_group = &mt100eocg_platform_attribute_group;
gpio_pins = gpio_pins_mt100eocg_0_0;
mts_hw_version = MT100EOCG_0_0;
set_buttons(default_buttons);
log_info("detected board %s", HW_VERSION_MT100EOCG_0_0);
} else {
int i;
for(i=0;i<sizeof id_eeprom.hw_version;i++) {
if(! id_eeprom.hw_version[i])
/* Found NULL so done */
break;
if(! isprint(id_eeprom.hw_version[i]))
break;
}
log_alert("Unsupported EEPROM settings or device");
if(i)
log_alert("Found unsupported EEPROM HW_VERSION: %.*s",i,id_eeprom.hw_version);
log_alert("Check log for HW_VERSION dump");
print_hex_dump_bytes("HW_VERSION: ", DUMP_PREFIX_OFFSET, id_eeprom.hw_version, sizeof id_eeprom.hw_version);
return -ENODEV;
}
log_info("sizeof: %lu", (unsigned long) sizeof(struct mts_id_eeprom_layout));
log_info("vendor-id: %.32s", id_eeprom.vendor_id);
log_info("product-id: %.32s", id_eeprom.product_id);
log_info("device-id: %.32s", id_eeprom.device_id);
log_info("hw-version: %.32s", id_eeprom.hw_version);
log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X",
id_eeprom.mac_addr[0],
id_eeprom.mac_addr[1],
id_eeprom.mac_addr[2],
id_eeprom.mac_addr[3],
id_eeprom.mac_addr[4],
id_eeprom.mac_addr[5]);
log_info("imei: %.32s", id_eeprom.imei);
log_info("capa-gps: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_GPS) ? "yes" : "no");
log_info("capa-din: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DIN) ? "yes" : "no");
log_info("capa-dout: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DOUT) ? "yes" : "no");
log_info("capa-adc: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_ADC) ? "yes" : "no");
log_info("capa-wifi: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI) ? "yes" : "no");
log_info("capa-bluetooth: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH) ? "yes" : "no");
if (!(mts_hw_version != HW_VERSION_MTCPM_0_0)) /* Moved to mtcdt3b driver in MTCDT3 baseboard hardware */
log_info("capa-lora: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) ? "yes" : "no");
log_info("capa-battery: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_BATTERY) ? "yes" : "no");
if (DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH)) {
log_info("mac-bluetooth: %02X:%02X:%02X:%02X:%02X:%02X",
id_eeprom.mac_bluetooth[0],
id_eeprom.mac_bluetooth[1],
id_eeprom.mac_bluetooth[2],
id_eeprom.mac_bluetooth[3],
id_eeprom.mac_bluetooth[4],
id_eeprom.mac_bluetooth[5]);
}
if (DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) {
log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X",
id_eeprom.mac_wifi[0],
id_eeprom.mac_wifi[1],
id_eeprom.mac_wifi[2],
id_eeprom.mac_wifi[3],
id_eeprom.mac_wifi[4],
id_eeprom.mac_wifi[5]);
}
//Loop Through UUID Bytes and print them in HEX
ptr = (char*)buf;
for(i = 0; i < 16; i++) {
ptr += sprintf(ptr, "%02X", id_eeprom.uuid[i]);
}
log_info("uuid: %s", (char*)buf);
if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) {
log_info("lora-eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
id_eeprom.lora_eui[0],
id_eeprom.lora_eui[1],
id_eeprom.lora_eui[2],
id_eeprom.lora_eui[3],
id_eeprom.lora_eui[4],
id_eeprom.lora_eui[5],
id_eeprom.lora_eui[6],
id_eeprom.lora_eui[7]);
log_info("lora-product-id: %.32s", id_eeprom.lora_product_id);
log_info("lora-hw-version: %.32s", id_eeprom.lora_hw_version);
}
return 0;
}
static void cleanup(void)
{
log_info("cleaning up....");
if (mts_io_platform_device) {
platform_device_unregister(mts_io_platform_device);
}
if(freelater) {
kfree(freelater);
freelater = NULL;
}
mts_capab_dir_delete();
mts_cpu_dir_delete();
}
static int __init mts_io_init(void)
{
struct gpio_pin *pin;
int ret;
log_info("init: " DRIVER_VERSION);
/* We do a platform_driver_register to do a probe
* of device tree and set the pinctrl. We then
* unregister to remove
* the probe function. If we don't remove the
* probe function, we will do a 2nd probe in
* platform_device_add, which will result in a
* stack trace in the log. */
ret = platform_driver_register(&mts_io_driver);
if (ret)
printk(KERN_ERR "mts-io: probe failed: %d\n", ret);
platform_driver_unregister(&mts_io_driver);
mts_io_platform_device = platform_device_alloc(PLATFORM_NAME, -1);
if (!mts_io_platform_device)
return -ENOMEM;
/* request_firmware() requires a device, so call after device allocated */
ret = mts_id_eeprom_load();
if (ret) {
kfree(mts_io_platform_device);
return ret;
}
ret = platform_device_add(mts_io_platform_device);
if (ret) {
kfree(mts_io_platform_device);
return ret;
}
if (DEVICE_CAPA(id_eeprom.capa, CAPA_DOUT)) {
ret = spi_register_driver(&mts_spi_dout_driver);
if (ret) {
printk(KERN_ERR "mts-io:mts-io-dout: probe failed: %d\n", ret);
}
}
if (DEVICE_CAPA(id_eeprom.capa, CAPA_DIN)) {
ret = spi_register_driver(&mts_spi_din_driver);
if (ret) {
printk(KERN_ERR "mts-io:mts-io-din: probe failed: %d\n", ret);
}
}
ret = spi_register_driver(&mts_spi_board_temp_driver);
if (ret) {
printk(KERN_ERR "mts-io:mts-io-board-temp: probe failed: %d\n", ret);
}
if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) {
mts_load_lora_port();
}
ret = mts_capab_dir_create(mts_hw_version);
if (ret) {
cleanup();
return ret;
}
ret = sysfs_create_group(&mts_io_platform_device->dev.kobj, attr_group);
if (ret) {
cleanup();
return ret;
}
for (pin = gpio_pins; *pin->name; pin++) {
if (pin->capability == 0 || DEVICE_CAPA(id_eeprom.capa,pin->capability)) {
ret = gpio_request_one(pin->pin.gpio, pin->pin.flags, pin->pin.label);
if (ret)
log_debug("could not request pin %s (%d) but it could have already been requested under a different pin name", pin->name, ret);
}
}
// Create CPU directory if approprate (only MTCDT3 for now)
ret = mts_cpu_dir_create(mts_hw_version);
// start general buttons processing
init_buttons();
/* init timers */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
timer_setup(&radio_reset_timer, radio_reset_timer_callback, 0);
timer_setup(&radio_reset_available_timer, radio_reset_available_timer_callback, 0);
#else
setup_timer(&radio_reset_timer, radio_reset_timer_callback, 0);
setup_timer(&radio_reset_available_timer, radio_reset_available_timer_callback, 0);
#endif
return 0;
}
static void __exit mts_io_exit(void)
{
if (DEVICE_CAPA(id_eeprom.capa, CAPA_DOUT)) {
spi_unregister_driver(&mts_spi_dout_driver);
}
if (DEVICE_CAPA(id_eeprom.capa, CAPA_DIN)) {
spi_unregister_driver(&mts_spi_din_driver);
}
spi_unregister_driver(&mts_spi_board_temp_driver);
struct gpio_pin *pin;
/* delete radio_reset_timer */
del_timer(&radio_reset_timer);
/* delete radio_reset_available_timer */
del_timer(&radio_reset_available_timer);
for (pin = gpio_pins; *pin->name; pin++)
if (pin->capability == 0 || DEVICE_CAPA(id_eeprom.capa,pin->capability))
gpio_free(pin->pin.gpio);
cleanup_buttons();
cleanup();
if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) {
mts_teardown_lora_port();
}
log_info("exiting");
}
module_init(mts_io_init);
module_exit(mts_io_exit);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");
|