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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
|
/*
* Synaptics DSX touchscreen driver
*
* Copyright (C) 2012 Synaptics Incorporated
*
* Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
* Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/firmware.h>
#include "synaptics_dsx.h"
#include "synaptics_dsx_i2c.h"
#define FW_IMAGE_NAME "synaptics/startup_fw_update.img"
#define DO_STARTUP_FW_UPDATE
#define STARTUP_FW_UPDATE_DELAY_MS 1000 /* ms */
#define FORCE_UPDATE false
#define DO_LOCKDOWN false
#define MAX_IMAGE_NAME_LEN 256
#define MAX_FIRMWARE_ID_LEN 10
#define LOCKDOWN_OFFSET 0xb0
#define FW_IMAGE_OFFSET 0x100
#define BOOTLOADER_ID_OFFSET 0
#define BLOCK_NUMBER_OFFSET 0
#define V5_PROPERTIES_OFFSET 2
#define V5_BLOCK_SIZE_OFFSET 3
#define V5_BLOCK_COUNT_OFFSET 5
#define V5_BLOCK_DATA_OFFSET 2
#define V6_PROPERTIES_OFFSET 1
#define V6_BLOCK_SIZE_OFFSET 2
#define V6_BLOCK_COUNT_OFFSET 3
#define V6_BLOCK_DATA_OFFSET 1
#define V6_FLASH_COMMAND_OFFSET 2
#define V6_FLASH_STATUS_OFFSET 3
#define LOCKDOWN_BLOCK_COUNT 5
#define REG_MAP (1 << 0)
#define UNLOCKED (1 << 1)
#define HAS_CONFIG_ID (1 << 2)
#define HAS_PERM_CONFIG (1 << 3)
#define HAS_BL_CONFIG (1 << 4)
#define HAS_DISP_CONFIG (1 << 5)
#define HAS_CTRL1 (1 << 6)
#define UI_CONFIG_AREA 0x00
#define PERM_CONFIG_AREA 0x01
#define BL_CONFIG_AREA 0x02
#define DISP_CONFIG_AREA 0x03
#define CMD_WRITE_FW_BLOCK 0x2
#define CMD_ERASE_ALL 0x3
#define CMD_WRITE_LOCKDOWN_BLOCK 0x4
#define CMD_READ_CONFIG_BLOCK 0x5
#define CMD_WRITE_CONFIG_BLOCK 0x6
#define CMD_ERASE_CONFIG 0x7
#define CMD_ERASE_BL_CONFIG 0x9
#define CMD_ERASE_DISP_CONFIG 0xa
#define CMD_ENABLE_FLASH_PROG 0xf
#define SLEEP_MODE_NORMAL (0x00)
#define SLEEP_MODE_SENSOR_SLEEP (0x01)
#define SLEEP_MODE_RESERVED0 (0x02)
#define SLEEP_MODE_RESERVED1 (0x03)
#define ENABLE_WAIT_MS (1 * 1000)
#define WRITE_WAIT_MS (3 * 1000)
#define ERASE_WAIT_MS (5 * 1000)
#define MIN_SLEEP_TIME_US 50
#define MAX_SLEEP_TIME_US 100
static ssize_t fwu_sysfs_show_image(struct file *data_file,
struct kobject *kobj, struct bin_attribute *attributes,
char *buf, loff_t pos, size_t count);
static ssize_t fwu_sysfs_store_image(struct file *data_file,
struct kobject *kobj, struct bin_attribute *attributes,
char *buf, loff_t pos, size_t count);
static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_write_config_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_read_config_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_config_area_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_image_name_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_image_size_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count);
static ssize_t fwu_sysfs_block_size_show(struct device *dev,
struct device_attribute *attr, char *buf);
static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf);
static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf);
static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf);
static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf);
static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf);
enum bl_version {
V5 = 5,
V6 = 6,
};
enum flash_area {
NONE,
UI_FIRMWARE,
CONFIG_AREA,
};
enum update_mode {
NORMAL = 1,
FORCE = 2,
LOCKDOWN = 8,
};
struct image_header {
/* 0x00 - 0x0f */
unsigned char checksum[4];
unsigned char reserved_04;
unsigned char reserved_05;
unsigned char options_firmware_id:1;
unsigned char options_contain_bootloader:1;
unsigned char options_reserved:6;
unsigned char bootloader_version;
unsigned char firmware_size[4];
unsigned char config_size[4];
/* 0x10 - 0x1f */
unsigned char product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE];
unsigned char package_id[2];
unsigned char package_id_revision[2];
unsigned char product_info[SYNAPTICS_RMI4_PRODUCT_INFO_SIZE];
/* 0x20 - 0x2f */
unsigned char reserved_20_2f[16];
/* 0x30 - 0x3f */
unsigned char ds_id[16];
/* 0x40 - 0x4f */
unsigned char ds_info[10];
unsigned char reserved_4a_4f[6];
/* 0x50 - 0x53 */
unsigned char firmware_id[4];
};
struct image_header_data {
bool contains_firmware_id;
unsigned int firmware_id;
unsigned int checksum;
unsigned int firmware_size;
unsigned int config_size;
unsigned char bootloader_version;
unsigned char product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE + 1];
unsigned char product_info[SYNAPTICS_RMI4_PRODUCT_INFO_SIZE];
};
struct pdt_properties {
union {
struct {
unsigned char reserved_1:6;
unsigned char has_bsr:1;
unsigned char reserved_2:1;
} __packed;
unsigned char data[1];
};
};
struct f01_device_status {
union {
struct {
unsigned char status_code:4;
unsigned char reserved:2;
unsigned char flash_prog:1;
unsigned char unconfigured:1;
} __packed;
unsigned char data[1];
};
};
struct f01_device_control {
union {
struct {
unsigned char sleep_mode:2;
unsigned char nosleep:1;
unsigned char reserved:2;
unsigned char charger_connected:1;
unsigned char report_rate:1;
unsigned char configured:1;
} __packed;
unsigned char data[1];
};
};
struct synaptics_rmi4_fwu_handle {
enum bl_version bl_version;
bool initialized;
bool program_enabled;
bool has_perm_config;
bool has_bl_config;
bool has_disp_config;
bool force_update;
bool in_flash_prog_mode;
bool do_lockdown;
unsigned int data_pos;
unsigned int image_size;
unsigned char *image_name;
unsigned char *ext_data_source;
unsigned char *read_config_buf;
unsigned char intr_mask;
unsigned char command;
unsigned char bootloader_id[2];
unsigned char flash_properties;
unsigned char flash_status;
unsigned char productinfo1;
unsigned char productinfo2;
unsigned char properties_off;
unsigned char blk_size_off;
unsigned char blk_count_off;
unsigned char blk_data_off;
unsigned char flash_cmd_off;
unsigned char flash_status_off;
unsigned short block_size;
unsigned short fw_block_count;
unsigned short config_block_count;
unsigned short lockdown_block_count;
unsigned short perm_config_block_count;
unsigned short bl_config_block_count;
unsigned short disp_config_block_count;
unsigned short config_size;
unsigned short config_area;
char product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE + 1];
const unsigned char *firmware_data;
const unsigned char *config_data;
const unsigned char *lockdown_data;
struct workqueue_struct *fwu_workqueue;
struct delayed_work fwu_work;
struct synaptics_rmi4_fn_desc f34_fd;
struct synaptics_rmi4_access_ptr *fn_ptr;
struct synaptics_rmi4_data *rmi4_data;
};
static struct bin_attribute dev_attr_data = {
.attr = {
.name = "data",
.mode = (S_IRUGO | S_IWUGO),
},
.size = 0,
.read = fwu_sysfs_show_image,
.write = fwu_sysfs_store_image,
};
static struct device_attribute attrs[] = {
__ATTR(doreflash, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_do_reflash_store),
__ATTR(writeconfig, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_write_config_store),
__ATTR(readconfig, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_read_config_store),
__ATTR(configarea, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_config_area_store),
__ATTR(imagename, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_image_name_store),
__ATTR(imagesize, S_IWUGO,
synaptics_rmi4_show_error,
fwu_sysfs_image_size_store),
__ATTR(blocksize, S_IRUGO,
fwu_sysfs_block_size_show,
synaptics_rmi4_store_error),
__ATTR(fwblockcount, S_IRUGO,
fwu_sysfs_firmware_block_count_show,
synaptics_rmi4_store_error),
__ATTR(configblockcount, S_IRUGO,
fwu_sysfs_configuration_block_count_show,
synaptics_rmi4_store_error),
__ATTR(permconfigblockcount, S_IRUGO,
fwu_sysfs_perm_config_block_count_show,
synaptics_rmi4_store_error),
__ATTR(blconfigblockcount, S_IRUGO,
fwu_sysfs_bl_config_block_count_show,
synaptics_rmi4_store_error),
__ATTR(dispconfigblockcount, S_IRUGO,
fwu_sysfs_disp_config_block_count_show,
synaptics_rmi4_store_error),
};
static struct synaptics_rmi4_fwu_handle *fwu;
DECLARE_COMPLETION(fwu_remove_complete);
static unsigned int extract_uint_le(const unsigned char *ptr)
{
return (unsigned int)ptr[0] +
(unsigned int)ptr[1] * 0x100 +
(unsigned int)ptr[2] * 0x10000 +
(unsigned int)ptr[3] * 0x1000000;
}
static unsigned int extract_uint_be(const unsigned char *ptr)
{
return (unsigned int)ptr[3] +
(unsigned int)ptr[2] * 0x100 +
(unsigned int)ptr[1] * 0x10000 +
(unsigned int)ptr[0] * 0x1000000;
}
static void parse_header(struct image_header_data *header,
const unsigned char *fw_image)
{
struct image_header *data = (struct image_header *)fw_image;
header->checksum = extract_uint_le(data->checksum);
header->bootloader_version = data->bootloader_version;
header->firmware_size = extract_uint_le(data->firmware_size);
header->config_size = extract_uint_le(data->config_size);
memcpy(header->product_id, data->product_id, sizeof(data->product_id));
header->product_id[sizeof(data->product_id)] = 0;
memcpy(header->product_info, data->product_info,
sizeof(data->product_info));
header->contains_firmware_id = data->options_firmware_id;
if (header->contains_firmware_id)
header->firmware_id = extract_uint_le(data->firmware_id);
return;
}
static int fwu_read_f01_device_status(struct f01_device_status *status)
{
int retval;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
retval = fwu->fn_ptr->read(fwu->rmi4_data,
rmi4_data->f01_data_base_addr,
status->data,
sizeof(status->data));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read F01 device status\n",
__func__);
return retval;
}
return 0;
}
static int fwu_read_f34_queries(void)
{
int retval;
unsigned char count;
unsigned char buf[10];
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.query_base_addr + BOOTLOADER_ID_OFFSET,
fwu->bootloader_id,
sizeof(fwu->bootloader_id));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read bootloader ID\n",
__func__);
return retval;
}
if (fwu->bootloader_id[1] == '5') {
fwu->bl_version = V5;
} else if (fwu->bootloader_id[1] == '6') {
fwu->bl_version = V6;
} else {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Unrecognized bootloader version\n",
__func__);
return -EINVAL;
}
if (fwu->bl_version == V5) {
fwu->properties_off = V5_PROPERTIES_OFFSET;
fwu->blk_size_off = V5_BLOCK_SIZE_OFFSET;
fwu->blk_count_off = V5_BLOCK_COUNT_OFFSET;
fwu->blk_data_off = V5_BLOCK_DATA_OFFSET;
} else if (fwu->bl_version == V6) {
fwu->properties_off = V6_PROPERTIES_OFFSET;
fwu->blk_size_off = V6_BLOCK_SIZE_OFFSET;
fwu->blk_count_off = V6_BLOCK_COUNT_OFFSET;
fwu->blk_data_off = V6_BLOCK_DATA_OFFSET;
}
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.query_base_addr + fwu->properties_off,
&fwu->flash_properties,
sizeof(fwu->flash_properties));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read flash properties\n",
__func__);
return retval;
}
count = 4;
if (fwu->flash_properties & HAS_PERM_CONFIG) {
fwu->has_perm_config = 1;
count += 2;
}
if (fwu->flash_properties & HAS_BL_CONFIG) {
fwu->has_bl_config = 1;
count += 2;
}
if (fwu->flash_properties & HAS_DISP_CONFIG) {
fwu->has_disp_config = 1;
count += 2;
}
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.query_base_addr + fwu->blk_size_off,
buf,
2);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read block size info\n",
__func__);
return retval;
}
batohs(&fwu->block_size, &(buf[0]));
if (fwu->bl_version == V5) {
fwu->flash_cmd_off = fwu->blk_data_off + fwu->block_size;
fwu->flash_status_off = fwu->flash_cmd_off;
} else if (fwu->bl_version == V6) {
fwu->flash_cmd_off = V6_FLASH_COMMAND_OFFSET;
fwu->flash_status_off = V6_FLASH_STATUS_OFFSET;
}
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.query_base_addr + fwu->blk_count_off,
buf,
count);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read block count info\n",
__func__);
return retval;
}
batohs(&fwu->fw_block_count, &(buf[0]));
batohs(&fwu->config_block_count, &(buf[2]));
count = 4;
if (fwu->has_perm_config) {
batohs(&fwu->perm_config_block_count, &(buf[count]));
count += 2;
}
if (fwu->has_bl_config) {
batohs(&fwu->bl_config_block_count, &(buf[count]));
count += 2;
}
if (fwu->has_disp_config)
batohs(&fwu->disp_config_block_count, &(buf[count]));
return 0;
}
static int fwu_read_f34_flash_status(void)
{
int retval;
unsigned char status;
unsigned char command;
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->flash_status_off,
&status,
sizeof(status));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read flash status\n",
__func__);
return retval;
}
fwu->program_enabled = status >> 7;
if (fwu->bl_version == V5)
fwu->flash_status = (status >> 4) & MASK_3BIT;
else if (fwu->bl_version == V6)
fwu->flash_status = status & MASK_3BIT;
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->flash_cmd_off,
&command,
sizeof(command));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read flash command\n",
__func__);
return retval;
}
fwu->command = command & MASK_4BIT;
return 0;
}
static int fwu_write_f34_command(unsigned char cmd)
{
int retval;
unsigned char command = cmd & MASK_4BIT;
fwu->command = cmd;
retval = fwu->fn_ptr->write(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->flash_cmd_off,
&command,
sizeof(command));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write command 0x%02x\n",
__func__, command);
return retval;
}
return 0;
}
static int fwu_wait_for_idle(int timeout_ms)
{
int count = 0;
int timeout_count = ((timeout_ms * 1000) / MAX_SLEEP_TIME_US) + 1;
do {
usleep_range(MIN_SLEEP_TIME_US, MAX_SLEEP_TIME_US);
count++;
if (count == timeout_count)
fwu_read_f34_flash_status();
if ((fwu->command == 0x00) && (fwu->flash_status == 0x00))
return 0;
} while (count < timeout_count);
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Timed out waiting for idle status\n",
__func__);
return -ETIMEDOUT;
}
static enum flash_area fwu_go_nogo(struct image_header_data *header)
{
int retval;
enum flash_area flash_area = NONE;
unsigned char index = 0;
unsigned char config_id[4];
unsigned int device_config_id;
unsigned int image_config_id;
unsigned int device_fw_id;
unsigned long image_fw_id;
char *strptr;
char *firmware_id;
if (fwu->force_update) {
flash_area = UI_FIRMWARE;
goto exit;
}
/* Update both UI and config if device is in bootloader mode */
if (fwu->in_flash_prog_mode) {
flash_area = UI_FIRMWARE;
goto exit;
}
/* Get device firmware ID */
device_fw_id = fwu->rmi4_data->firmware_id;
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Device firmware ID = %d\n",
__func__, device_fw_id);
/* Get image firmware ID */
if (header->contains_firmware_id) {
image_fw_id = header->firmware_id;
} else {
strptr = strstr(fwu->image_name, "PR");
if (!strptr) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: No valid PR number (PRxxxxxxx) "
"found in image file name (%s)\n",
__func__, fwu->image_name);
flash_area = NONE;
goto exit;
}
strptr += 2;
firmware_id = kzalloc(MAX_FIRMWARE_ID_LEN, GFP_KERNEL);
while (strptr[index] >= '0' && strptr[index] <= '9') {
firmware_id[index] = strptr[index];
index++;
}
retval = sstrtoul(firmware_id, 10, &image_fw_id);
kfree(firmware_id);
if (retval) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to obtain image firmware ID\n",
__func__);
flash_area = NONE;
goto exit;
}
}
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Image firmware ID = %d\n",
__func__, (unsigned int)image_fw_id);
if (image_fw_id > device_fw_id) {
flash_area = UI_FIRMWARE;
goto exit;
} else if (image_fw_id < device_fw_id) {
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Image firmware ID older than device firmware ID\n",
__func__);
flash_area = NONE;
goto exit;
}
/* Get device config ID */
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.ctrl_base_addr,
config_id,
sizeof(config_id));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read device config ID\n",
__func__);
flash_area = NONE;
goto exit;
}
device_config_id = extract_uint_be(config_id);
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Device config ID = 0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__,
config_id[0],
config_id[1],
config_id[2],
config_id[3]);
/* Get image config ID */
image_config_id = extract_uint_be(fwu->config_data);
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Image config ID = 0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__,
fwu->config_data[0],
fwu->config_data[1],
fwu->config_data[2],
fwu->config_data[3]);
if (image_config_id > device_config_id) {
flash_area = CONFIG_AREA;
goto exit;
}
flash_area = NONE;
exit:
if (flash_area == NONE) {
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: No need to do reflash\n",
__func__);
} else {
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Updating %s\n",
__func__,
flash_area == UI_FIRMWARE ?
"UI firmware" :
"config only");
}
return flash_area;
}
static int fwu_scan_pdt(void)
{
int retval;
unsigned char ii;
unsigned char intr_count = 0;
unsigned char intr_off;
unsigned char intr_src;
unsigned short addr;
bool f01found = false;
bool f34found = false;
struct synaptics_rmi4_fn_desc rmi_fd;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
for (addr = PDT_START; addr > PDT_END; addr -= PDT_ENTRY_SIZE) {
retval = fwu->fn_ptr->read(fwu->rmi4_data,
addr,
(unsigned char *)&rmi_fd,
sizeof(rmi_fd));
if (retval < 0)
return retval;
if (rmi_fd.fn_number) {
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Found F%02x\n",
__func__, rmi_fd.fn_number);
switch (rmi_fd.fn_number) {
case SYNAPTICS_RMI4_F01:
f01found = true;
rmi4_data->f01_query_base_addr =
rmi_fd.query_base_addr;
rmi4_data->f01_ctrl_base_addr =
rmi_fd.ctrl_base_addr;
rmi4_data->f01_data_base_addr =
rmi_fd.data_base_addr;
rmi4_data->f01_cmd_base_addr =
rmi_fd.cmd_base_addr;
break;
case SYNAPTICS_RMI4_F34:
f34found = true;
fwu->f34_fd.query_base_addr =
rmi_fd.query_base_addr;
fwu->f34_fd.ctrl_base_addr =
rmi_fd.ctrl_base_addr;
fwu->f34_fd.data_base_addr =
rmi_fd.data_base_addr;
fwu->intr_mask = 0;
intr_src = rmi_fd.intr_src_count;
intr_off = intr_count % 8;
for (ii = intr_off;
ii < ((intr_src & MASK_3BIT) +
intr_off);
ii++) {
fwu->intr_mask |= 1 << ii;
}
break;
}
} else {
break;
}
intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
}
if (!f01found || !f34found) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to find both F01 and F34\n",
__func__);
return -EINVAL;
}
return 0;
}
static int fwu_write_blocks(unsigned char *block_ptr, unsigned short block_cnt,
unsigned char command)
{
int retval;
unsigned char block_offset[] = {0, 0};
unsigned short block_num;
block_offset[1] |= (fwu->config_area << 5);
retval = fwu->fn_ptr->write(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
block_offset,
sizeof(block_offset));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write to block number registers\n",
__func__);
return retval;
}
for (block_num = 0; block_num < block_cnt; block_num++) {
retval = fwu->fn_ptr->write(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->blk_data_off,
block_ptr,
fwu->block_size);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write block data (block %d)\n",
__func__, block_num);
return retval;
}
retval = fwu_write_f34_command(command);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write command for block %d\n",
__func__, block_num);
return retval;
}
retval = fwu_wait_for_idle(WRITE_WAIT_MS);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to wait for idle status (block %d)\n",
__func__, block_num);
return retval;
}
block_ptr += fwu->block_size;
}
return 0;
}
static int fwu_write_firmware(void)
{
return fwu_write_blocks((unsigned char *)fwu->firmware_data,
fwu->fw_block_count, CMD_WRITE_FW_BLOCK);
}
static int fwu_write_configuration(void)
{
return fwu_write_blocks((unsigned char *)fwu->config_data,
fwu->config_block_count, CMD_WRITE_CONFIG_BLOCK);
}
static int fwu_write_lockdown(void)
{
return fwu_write_blocks((unsigned char *)fwu->lockdown_data,
fwu->lockdown_block_count, CMD_WRITE_LOCKDOWN_BLOCK);
}
static int fwu_write_bootloader_id(void)
{
int retval;
retval = fwu->fn_ptr->write(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->blk_data_off,
fwu->bootloader_id,
sizeof(fwu->bootloader_id));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write bootloader ID\n",
__func__);
return retval;
}
return 0;
}
static int fwu_enter_flash_prog(void)
{
int retval;
struct f01_device_status f01_device_status;
struct f01_device_control f01_device_control;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
retval = fwu_write_bootloader_id();
if (retval < 0)
return retval;
retval = fwu_write_f34_command(CMD_ENABLE_FLASH_PROG);
if (retval < 0)
return retval;
retval = fwu_wait_for_idle(ENABLE_WAIT_MS);
if (retval < 0)
return retval;
if (!fwu->program_enabled) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Program enabled bit not set\n",
__func__);
return -EINVAL;
}
retval = fwu_scan_pdt();
if (retval < 0)
return retval;
retval = fwu_read_f01_device_status(&f01_device_status);
if (retval < 0)
return retval;
if (!f01_device_status.flash_prog) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Not in flash prog mode\n",
__func__);
return -EINVAL;
}
retval = fwu_read_f34_queries();
if (retval < 0)
return retval;
retval = fwu->fn_ptr->read(fwu->rmi4_data,
rmi4_data->f01_ctrl_base_addr,
f01_device_control.data,
sizeof(f01_device_control.data));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read F01 device control\n",
__func__);
return retval;
}
f01_device_control.nosleep = true;
f01_device_control.sleep_mode = SLEEP_MODE_NORMAL;
retval = fwu->fn_ptr->write(fwu->rmi4_data,
rmi4_data->f01_ctrl_base_addr,
f01_device_control.data,
sizeof(f01_device_control.data));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write F01 device control\n",
__func__);
return retval;
}
return retval;
}
static int fwu_do_reflash(void)
{
int retval;
retval = fwu_enter_flash_prog();
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Entered flash prog mode\n",
__func__);
retval = fwu_write_bootloader_id();
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Bootloader ID written\n",
__func__);
retval = fwu_write_f34_command(CMD_ERASE_ALL);
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Erase all command written\n",
__func__);
retval = fwu_wait_for_idle(ERASE_WAIT_MS);
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Idle status detected\n",
__func__);
if (fwu->firmware_data) {
retval = fwu_write_firmware();
if (retval < 0)
return retval;
pr_notice("%s: Firmware programmed\n", __func__);
}
if (fwu->config_data) {
retval = fwu_write_configuration();
if (retval < 0)
return retval;
pr_notice("%s: Configuration programmed\n", __func__);
}
return retval;
}
static int fwu_do_write_config(void)
{
int retval;
retval = fwu_enter_flash_prog();
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Entered flash prog mode\n",
__func__);
if (fwu->config_area == PERM_CONFIG_AREA) {
fwu->config_block_count = fwu->perm_config_block_count;
goto write_config;
}
retval = fwu_write_bootloader_id();
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Bootloader ID written\n",
__func__);
switch (fwu->config_area) {
case UI_CONFIG_AREA:
retval = fwu_write_f34_command(CMD_ERASE_CONFIG);
break;
case BL_CONFIG_AREA:
retval = fwu_write_f34_command(CMD_ERASE_BL_CONFIG);
fwu->config_block_count = fwu->bl_config_block_count;
break;
case DISP_CONFIG_AREA:
retval = fwu_write_f34_command(CMD_ERASE_DISP_CONFIG);
fwu->config_block_count = fwu->disp_config_block_count;
break;
}
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Erase command written\n",
__func__);
retval = fwu_wait_for_idle(ERASE_WAIT_MS);
if (retval < 0)
return retval;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Idle status detected\n",
__func__);
write_config:
retval = fwu_write_configuration();
if (retval < 0)
return retval;
pr_notice("%s: Config written\n", __func__);
return retval;
}
static int fwu_start_write_config(void)
{
int retval;
unsigned short block_count;
struct image_header_data header;
switch (fwu->config_area) {
case UI_CONFIG_AREA:
block_count = fwu->config_block_count;
break;
case PERM_CONFIG_AREA:
if (!fwu->has_perm_config)
return -EINVAL;
block_count = fwu->perm_config_block_count;
break;
case BL_CONFIG_AREA:
if (!fwu->has_bl_config)
return -EINVAL;
block_count = fwu->bl_config_block_count;
break;
case DISP_CONFIG_AREA:
if (!fwu->has_disp_config)
return -EINVAL;
block_count = fwu->disp_config_block_count;
break;
default:
return -EINVAL;
}
if (fwu->ext_data_source)
fwu->config_data = fwu->ext_data_source;
else
return -EINVAL;
fwu->config_size = fwu->block_size * block_count;
/* Jump to the config area if given a packrat image */
if ((fwu->config_area == UI_CONFIG_AREA) &&
(fwu->config_size != fwu->image_size)) {
parse_header(&header, fwu->ext_data_source);
if (header.config_size) {
fwu->config_data = fwu->ext_data_source +
FW_IMAGE_OFFSET +
header.firmware_size;
} else {
return -EINVAL;
}
}
pr_notice("%s: Start of write config process\n", __func__);
retval = fwu_do_write_config();
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write config\n",
__func__);
}
fwu->rmi4_data->reset_device(fwu->rmi4_data);
pr_notice("%s: End of write config process\n", __func__);
return retval;
}
static int fwu_do_read_config(void)
{
int retval;
unsigned char block_offset[] = {0, 0};
unsigned short block_num;
unsigned short block_count;
unsigned short index = 0;
retval = fwu_enter_flash_prog();
if (retval < 0)
goto exit;
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Entered flash prog mode\n",
__func__);
switch (fwu->config_area) {
case UI_CONFIG_AREA:
block_count = fwu->config_block_count;
break;
case PERM_CONFIG_AREA:
if (!fwu->has_perm_config) {
retval = -EINVAL;
goto exit;
}
block_count = fwu->perm_config_block_count;
break;
case BL_CONFIG_AREA:
if (!fwu->has_bl_config) {
retval = -EINVAL;
goto exit;
}
block_count = fwu->bl_config_block_count;
break;
case DISP_CONFIG_AREA:
if (!fwu->has_disp_config) {
retval = -EINVAL;
goto exit;
}
block_count = fwu->disp_config_block_count;
break;
default:
retval = -EINVAL;
goto exit;
}
fwu->config_size = fwu->block_size * block_count;
kfree(fwu->read_config_buf);
fwu->read_config_buf = kzalloc(fwu->config_size, GFP_KERNEL);
block_offset[1] |= (fwu->config_area << 5);
retval = fwu->fn_ptr->write(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
block_offset,
sizeof(block_offset));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write to block number registers\n",
__func__);
goto exit;
}
for (block_num = 0; block_num < block_count; block_num++) {
retval = fwu_write_f34_command(CMD_READ_CONFIG_BLOCK);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to write read config command\n",
__func__);
goto exit;
}
retval = fwu_wait_for_idle(WRITE_WAIT_MS);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to wait for idle status\n",
__func__);
goto exit;
}
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.data_base_addr + fwu->blk_data_off,
&fwu->read_config_buf[index],
fwu->block_size);
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read block data (block %d)\n",
__func__, block_num);
goto exit;
}
index += fwu->block_size;
}
exit:
fwu->rmi4_data->reset_device(fwu->rmi4_data);
return retval;
}
static int fwu_do_lockdown(void)
{
int retval;
retval = fwu_enter_flash_prog();
if (retval < 0)
return retval;
retval = fwu->fn_ptr->read(fwu->rmi4_data,
fwu->f34_fd.query_base_addr + fwu->properties_off,
&fwu->flash_properties,
sizeof(fwu->flash_properties));
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to read flash properties\n",
__func__);
return retval;
}
if ((fwu->flash_properties & UNLOCKED) == 0) {
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: Device already locked down\n",
__func__);
return retval;
}
retval = fwu_write_lockdown();
if (retval < 0)
return retval;
pr_notice("%s: Lockdown programmed\n", __func__);
return retval;
}
static int fwu_start_reflash(void)
{
int retval = 0;
enum flash_area flash_area;
struct image_header_data header;
struct f01_device_status f01_device_status;
const unsigned char *fw_image;
const struct firmware *fw_entry = NULL;
if (fwu->rmi4_data->sensor_sleep) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Sensor sleeping\n",
__func__);
return -ENODEV;
}
fwu->rmi4_data->stay_awake = true;
pr_notice("%s: Start of reflash process\n", __func__);
if (fwu->ext_data_source) {
fw_image = fwu->ext_data_source;
} else {
strncpy(fwu->image_name, FW_IMAGE_NAME, MAX_IMAGE_NAME_LEN);
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Requesting firmware image %s\n",
__func__, fwu->image_name);
retval = request_firmware(&fw_entry, fwu->image_name,
&fwu->rmi4_data->i2c_client->dev);
if (retval != 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Firmware image %s not available\n",
__func__, fwu->image_name);
retval = -EINVAL;
goto exit;
}
dev_dbg(&fwu->rmi4_data->i2c_client->dev,
"%s: Firmware image size = %d\n",
__func__, fw_entry->size);
fw_image = fw_entry->data;
}
parse_header(&header, fw_image);
if (fwu->bl_version != header.bootloader_version) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Bootloader version mismatch\n",
__func__);
retval = -EINVAL;
goto exit;
}
retval = fwu_read_f01_device_status(&f01_device_status);
if (retval < 0)
goto exit;
if (f01_device_status.flash_prog) {
dev_info(&fwu->rmi4_data->i2c_client->dev,
"%s: In flash prog mode\n",
__func__);
fwu->in_flash_prog_mode = true;
} else {
fwu->in_flash_prog_mode = false;
}
if (fwu->do_lockdown) {
switch (fwu->bl_version) {
case V5:
case V6:
fwu->lockdown_data = fw_image + LOCKDOWN_OFFSET;
fwu->lockdown_block_count = LOCKDOWN_BLOCK_COUNT;
retval = fwu_do_lockdown();
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to do lockdown\n",
__func__);
}
default:
break;
}
}
if (header.firmware_size)
fwu->firmware_data = fw_image + FW_IMAGE_OFFSET;
if (header.config_size) {
fwu->config_data = fw_image + FW_IMAGE_OFFSET +
header.firmware_size;
}
flash_area = fwu_go_nogo(&header);
switch (flash_area) {
case UI_FIRMWARE:
retval = fwu_do_reflash();
break;
case CONFIG_AREA:
retval = fwu_do_write_config();
break;
case NONE:
default:
goto exit;
}
if (retval < 0) {
dev_err(&fwu->rmi4_data->i2c_client->dev,
"%s: Failed to do reflash\n",
__func__);
}
exit:
fwu->rmi4_data->reset_device(fwu->rmi4_data);
if (fw_entry)
release_firmware(fw_entry);
pr_notice("%s: End of reflash process\n", __func__);
fwu->rmi4_data->stay_awake = false;
return retval;
}
int synaptics_fw_updater(unsigned char *fw_data)
{
int retval;
if (!fwu)
return -ENODEV;
if (!fwu->initialized)
return -ENODEV;
fwu->ext_data_source = fw_data;
fwu->config_area = UI_CONFIG_AREA;
retval = fwu_start_reflash();
return retval;
}
EXPORT_SYMBOL(synaptics_fw_updater);
static void fwu_startup_fw_update_work(struct work_struct *work)
{
synaptics_fw_updater(NULL);
return;
}
static ssize_t fwu_sysfs_show_image(struct file *data_file,
struct kobject *kobj, struct bin_attribute *attributes,
char *buf, loff_t pos, size_t count)
{
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
if (count < fwu->config_size) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Not enough space (%d bytes) in buffer\n",
__func__, count);
return -EINVAL;
}
memcpy(buf, fwu->read_config_buf, fwu->config_size);
return fwu->config_size;
}
static ssize_t fwu_sysfs_store_image(struct file *data_file,
struct kobject *kobj, struct bin_attribute *attributes,
char *buf, loff_t pos, size_t count)
{
memcpy((void *)(&fwu->ext_data_source[fwu->data_pos]),
(const void *)buf,
count);
fwu->data_pos += count;
return count;
}
static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int retval;
unsigned int input;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
if (sscanf(buf, "%u", &input) != 1) {
retval = -EINVAL;
goto exit;
}
if (input & LOCKDOWN) {
fwu->do_lockdown = true;
input &= ~LOCKDOWN;
}
if ((input != NORMAL) && (input != FORCE)) {
retval = -EINVAL;
goto exit;
}
if (input == FORCE)
fwu->force_update = true;
retval = synaptics_fw_updater(fwu->ext_data_source);
if (retval < 0) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to do reflash\n",
__func__);
goto exit;
}
retval = count;
exit:
kfree(fwu->ext_data_source);
fwu->ext_data_source = NULL;
fwu->force_update = FORCE_UPDATE;
fwu->do_lockdown = DO_LOCKDOWN;
return retval;
}
static ssize_t fwu_sysfs_write_config_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int retval;
unsigned int input;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
if (sscanf(buf, "%u", &input) != 1) {
retval = -EINVAL;
goto exit;
}
if (input != 1) {
retval = -EINVAL;
goto exit;
}
retval = fwu_start_write_config();
if (retval < 0) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to write config\n",
__func__);
goto exit;
}
retval = count;
exit:
kfree(fwu->ext_data_source);
fwu->ext_data_source = NULL;
return retval;
}
static ssize_t fwu_sysfs_read_config_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int retval;
unsigned int input;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
if (sscanf(buf, "%u", &input) != 1)
return -EINVAL;
if (input != 1)
return -EINVAL;
retval = fwu_do_read_config();
if (retval < 0) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to read config\n",
__func__);
return retval;
}
return count;
}
static ssize_t fwu_sysfs_config_area_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int retval;
unsigned long config_area;
retval = sstrtoul(buf, 10, &config_area);
if (retval)
return retval;
fwu->config_area = config_area;
return count;
}
static ssize_t fwu_sysfs_image_name_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
memcpy(fwu->image_name, buf, count);
return count;
}
static ssize_t fwu_sysfs_image_size_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int retval;
unsigned long size;
struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
retval = sstrtoul(buf, 10, &size);
if (retval)
return retval;
fwu->image_size = size;
fwu->data_pos = 0;
kfree(fwu->ext_data_source);
fwu->ext_data_source = kzalloc(fwu->image_size, GFP_KERNEL);
if (!fwu->ext_data_source) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to alloc mem for image data\n",
__func__);
return -ENOMEM;
}
return count;
}
static ssize_t fwu_sysfs_block_size_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->block_size);
}
static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->fw_block_count);
}
static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->config_block_count);
}
static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->perm_config_block_count);
}
static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->bl_config_block_count);
}
static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", fwu->disp_config_block_count);
}
static void synaptics_rmi4_fwu_attn(struct synaptics_rmi4_data *rmi4_data,
unsigned char intr_mask)
{
if (!fwu)
return;
if (fwu->intr_mask & intr_mask)
fwu_read_f34_flash_status();
return;
}
static int synaptics_rmi4_fwu_init(struct synaptics_rmi4_data *rmi4_data)
{
int retval;
unsigned char attr_count;
struct pdt_properties pdt_props;
fwu = kzalloc(sizeof(*fwu), GFP_KERNEL);
if (!fwu) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to alloc mem for fwu\n",
__func__);
retval = -ENOMEM;
goto exit;
}
fwu->fn_ptr = kzalloc(sizeof(*(fwu->fn_ptr)), GFP_KERNEL);
if (!fwu->fn_ptr) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to alloc mem for fn_ptr\n",
__func__);
retval = -ENOMEM;
goto exit_free_fwu;
}
fwu->image_name = kzalloc(MAX_IMAGE_NAME_LEN, GFP_KERNEL);
if (!fwu->image_name) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to alloc mem for image name\n",
__func__);
retval = -ENOMEM;
goto exit_free_fn_ptr;
}
fwu->rmi4_data = rmi4_data;
fwu->fn_ptr->read = rmi4_data->i2c_read;
fwu->fn_ptr->write = rmi4_data->i2c_write;
fwu->fn_ptr->enable = rmi4_data->irq_enable;
retval = fwu->fn_ptr->read(rmi4_data,
PDT_PROPS,
pdt_props.data,
sizeof(pdt_props.data));
if (retval < 0) {
dev_dbg(&rmi4_data->i2c_client->dev,
"%s: Failed to read PDT properties, assuming 0x00\n",
__func__);
} else if (pdt_props.has_bsr) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Reflash for LTS not currently supported\n",
__func__);
retval = -ENODEV;
goto exit_free_mem;
}
retval = fwu_scan_pdt();
if (retval < 0)
goto exit_free_mem;
fwu->productinfo1 = rmi4_data->rmi4_mod_info.product_info[0];
fwu->productinfo2 = rmi4_data->rmi4_mod_info.product_info[1];
memcpy(fwu->product_id, rmi4_data->rmi4_mod_info.product_id_string,
SYNAPTICS_RMI4_PRODUCT_ID_SIZE);
fwu->product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE] = 0;
dev_dbg(&rmi4_data->i2c_client->dev,
"%s: F01 product info: 0x%04x 0x%04x\n",
__func__, fwu->productinfo1, fwu->productinfo2);
dev_dbg(&rmi4_data->i2c_client->dev,
"%s: F01 product ID: %s\n",
__func__, fwu->product_id);
retval = fwu_read_f34_queries();
if (retval < 0)
goto exit_free_mem;
fwu->force_update = FORCE_UPDATE;
fwu->do_lockdown = DO_LOCKDOWN;
fwu->initialized = true;
retval = sysfs_create_bin_file(&rmi4_data->input_dev->dev.kobj,
&dev_attr_data);
if (retval < 0) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to create sysfs bin file\n",
__func__);
goto exit_free_mem;
}
for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
&attrs[attr_count].attr);
if (retval < 0) {
dev_err(&rmi4_data->i2c_client->dev,
"%s: Failed to create sysfs attributes\n",
__func__);
retval = -ENODEV;
goto exit_remove_attrs;
}
}
#ifdef DO_STARTUP_FW_UPDATE
fwu->fwu_workqueue = create_singlethread_workqueue("fwu_workqueue");
INIT_DELAYED_WORK(&fwu->fwu_work, fwu_startup_fw_update_work);
queue_delayed_work(fwu->fwu_workqueue,
&fwu->fwu_work,
msecs_to_jiffies(STARTUP_FW_UPDATE_DELAY_MS));
#endif
return 0;
exit_remove_attrs:
for (attr_count--; attr_count >= 0; attr_count--) {
sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
&attrs[attr_count].attr);
}
sysfs_remove_bin_file(&rmi4_data->input_dev->dev.kobj, &dev_attr_data);
exit_free_mem:
kfree(fwu->image_name);
exit_free_fn_ptr:
kfree(fwu->fn_ptr);
exit_free_fwu:
kfree(fwu);
fwu = NULL;
exit:
return retval;
}
static void synaptics_rmi4_fwu_remove(struct synaptics_rmi4_data *rmi4_data)
{
unsigned char attr_count;
if (!fwu)
goto exit;
sysfs_remove_bin_file(&rmi4_data->input_dev->dev.kobj, &dev_attr_data);
for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
&attrs[attr_count].attr);
}
kfree(fwu->read_config_buf);
kfree(fwu->image_name);
kfree(fwu->fn_ptr);
kfree(fwu);
fwu = NULL;
exit:
complete(&fwu_remove_complete);
return;
}
static struct synaptics_rmi4_exp_fn fwu_module = {
.fn_type = RMI_FW_UPDATER,
.init = synaptics_rmi4_fwu_init,
.remove = synaptics_rmi4_fwu_remove,
.reset = NULL,
.reinit = NULL,
.early_suspend = NULL,
.suspend = NULL,
.resume = NULL,
.late_resume = NULL,
.attn = synaptics_rmi4_fwu_attn,
};
static int __init rmi4_fw_update_module_init(void)
{
synaptics_rmi4_new_function(&fwu_module, true);
return 0;
}
static void __exit rmi4_fw_update_module_exit(void)
{
synaptics_rmi4_new_function(&fwu_module, false);
wait_for_completion(&fwu_remove_complete);
return;
}
module_init(rmi4_fw_update_module_init);
module_exit(rmi4_fw_update_module_exit);
MODULE_AUTHOR("Synaptics, Inc.");
MODULE_DESCRIPTION("Synaptics DSX FW Update Module");
MODULE_LICENSE("GPL v2");
|