hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
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
/* drivers/input/touchscreen/gt1x_update.c
 *
 * 2010 - 2014 Goodix Technology.
 *
 * 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 a reference
 * to you, when you are integrating the GOODiX's CTP IC into your system,
 * 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.
 *
 * Version: 1.4
 * Release Date:  2015/07/10
 */
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/uaccess.h>
 
#include "gt1x_generic.h"
#if (GTP_HOTKNOT || GTP_HEADER_FW_UPDATE)
#include "gt1x_firmware.h"
#endif
 
#define UPDATE_FILE_PATH_1          "/data/_goodix_update_.bin"
#define UPDATE_FILE_PATH_2          "/sdcard/_goodix_update_.bin"
 
#define CONFIG_FILE_PATH_1          "/data/_gt1x_config_.cfg"
#define CONFIG_FILE_PATH_2          "/sdcard/_gt1x_config_.cfg"
 
#define FOUND_FW_PATH_1              0x01
#define FOUND_FW_PATH_2              0x02
#define FOUND_CFG_PATH_1             0x04
#define FOUND_CFG_PATH_2             0x08
 
#define PACK_SIZE                    256
 
/*hardware register define*/
#define _bRW_MISCTL__SRAM_BANK       0x4048
#define _bRW_MISCTL__MEM_CD_EN       0x4049
#define _bRW_MISCTL__CACHE_EN        0x404B
#define _bRW_MISCTL__TMR0_EN         0x40B0
#define _rRW_MISCTL__SWRST_B0_       0x4180
#define _bWO_MISCTL__CPU_SWRST_PULSE 0x4184
#define _rRW_MISCTL__BOOTCTL_B0_     0x4190
#define _rRW_MISCTL__BOOT_OPT_B0_    0x4218
#define _rRW_MISCTL__BOOT_CTL_       0x5094
#define _bRW_MISCTL__DSP_MCU_PWR_    0x4010
#define _bRW_MISCTL__PATCH_AREA_EN_  0x404D
 
/*
 1.  firmware structure
    header: 128b
    offset           size          content
    0                 4              firmware length
    4                 2              checksum
    6                 6              target MASK name
    12               3              target MASK version
    15               6              TP subsystem PID
    21               3              TP subsystem version
    24               1              subsystem count
    25               1              chip type                             0x91: GT1X,   0x92: GT2X
    26               6              reserved
    32               8              subsystem info[0]
    32               8              subsystem info[1]
    .....
    120             8              subsystem info[11]
    body: followed header
    128             N0              subsystem[0]
    128+N0       N1              subsystem[1]
    ....
 2. subsystem info structure
    offset           size          content
    0                 1              subsystem type
    1                 2              subsystem length
    3                 2              stored address in flash           addr = value * 256
    5                 3              reserved
 
*/
 
#define FW_HEAD_SIZE                         128
#define FW_HEAD_SUBSYSTEM_INFO_SIZE          8
#define FW_HEAD_OFFSET_SUBSYSTEM_INFO_BASE   32
 
#define FW_SECTION_TYPE_SS51_ISP               0x01
#define FW_SECTION_TYPE_SS51_PATCH             0x02
#define FW_SECTION_TYPE_SS51_PATCH_OVERLAY     0x03
#define FW_SECTION_TYPE_DSP                    0x04
#define FW_SECTION_TYPE_HOTKNOT                0x05
#define FW_SECTION_TYPE_GESTURE                0x06
#define FW_SECTION_TYPE_GESTURE_OVERLAY        0x07
#define FW_SECTION_TYPE_FLASHLESS_FAST_POWER   0x08
 
#define UPDATE_TYPE_HEADER 0
#define UPDATE_TYPE_FILE   1
 
#define UPDATE_STATUS_IDLE     0
#define UPDATE_STATUS_RUNNING  1
#define UPDATE_STATUS_ABORT    2
 
struct fw_subsystem_info {
   int type;
   int length;
   u32 address;
   int offset;
};
 
#pragma pack(1)
struct fw_info {
   u32 length;
   u16 checksum;
   u8 target_mask[6];
   u8 target_mask_version[3];
   u8 pid[6];
   u8 version[3];
   u8 subsystem_count;
   u8 chip_type;
   u8 reserved[6];
   struct fw_subsystem_info subsystem[12];
};
#pragma pack()
 
struct fw_update_info update_info = {
   .status = UPDATE_STATUS_IDLE,
   .progress = 0,
   .max_progress = 9,
   .force_update = 0
};
 
// int gt1x_update_prepare(char *filename);
int gt1x_check_firmware(void);
u8 *gt1x_get_fw_data(u32 offset, int length);
int gt1x_update_judge(void);
int gt1x_run_ss51_isp(u8 *ss51_isp, int length);
int gt1x_burn_subsystem(struct fw_subsystem_info *subsystem);
u16 gt1x_calc_checksum(u8 *fw, u32 length);
int gt1x_recall_check(u8 *chk_src, u16 start_rd_addr, u16 chk_length);
void gt1x_update_cleanup(void);
int gt1x_check_subsystem_in_flash(struct fw_subsystem_info *subsystem);
int gt1x_read_flash(u32 addr, int length);
int gt1x_error_erase(void);
// void dump_to_file(u16 addr, int length, char *filepath);
 
int gt1x_update_firmware(void *filename);
int gt1x_auto_update_proc(void *data);
 
#if !GTP_HEADER_FW_UPDATE
// static int gt1x_search_update_files(void);
#endif
 
int gt1x_hold_ss51_dsp(void);
void gt1x_leave_update_mode(void);
 
/**
 * @return: return 0 if success, otherwise return a negative number
 *          which contains the error code.
 */
#if 0 //close for GKI
s32 gt1x_check_fs_mounted(char *path_name)
{
   struct path root_path;
   struct path path;
   s32 err;
 
   err = kern_path("/", LOOKUP_FOLLOW, &root_path);
   if (err)
       return ERROR_PATH;
 
   err = kern_path(path_name, LOOKUP_FOLLOW, &path);
   if (err) {
       err = ERROR_PATH;
       goto check_fs_fail;
   }
 
   if (path.mnt->mnt_sb == root_path.mnt->mnt_sb) {
       /*not mounted*/
       err = ERROR_PATH;
   } else {
       err = 0;
   }
 
   path_put(&path);
check_fs_fail:
   path_put(&root_path);
   return err;
}
#endif
 
int gt1x_i2c_write_with_readback(u16 addr, u8 *buffer, int length)
{
   u8 buf[100];
   int ret = gt1x_i2c_write(addr, buffer, length);
   if (ret) {
       return ret;
   }
   ret = gt1x_i2c_read(addr, buf, length);
   if (ret) {
       return ret;
   }
   if (memcmp(buf, buffer, length)) {
       return ERROR_CHECK;
   }
   return 0;
}
 
#define getU32(a) ((u32)getUint((u8 *)(a), 4))
#define getU16(a) ((u16)getUint((u8 *)(a), 2))
u32 getUint(u8 *buffer, int len)
{
   u32 num = 0;
   int i;
   for (i = 0; i < len; i++) {
       num <<= 8;
       num += buffer[i];
   }
   return num;
}
 
int gt1x_auto_update_proc(void *data)
{
/* auto update from /data/_goodix_update_.bin
 * Does not meet the gki standard.
 * close auto update function.
 */
   GTP_ERROR("auto update failed! return\n");
   return 0;
#if 0
#if GTP_HEADER_FW_UPDATE
   GTP_INFO("Start auto update thread...");
   gt1x_update_firmware(NULL);
#else
   int ret;
   char *filename;
   u8 config[GTP_CONFIG_MAX_LENGTH] = { 0 };
 
   GTP_INFO("Start auto update thread...");
   ret = gt1x_search_update_files();
   if (ret & (FOUND_FW_PATH_1 | FOUND_FW_PATH_2)) {
       if (ret & FOUND_FW_PATH_1) {
           filename = UPDATE_FILE_PATH_1;
       } else {
           filename = UPDATE_FILE_PATH_2;
       }
       gt1x_update_firmware(filename);
   }
 
   if (ret & (FOUND_CFG_PATH_1 | FOUND_CFG_PATH_2)) {
       if (ret & FOUND_CFG_PATH_1) {
           filename = CONFIG_FILE_PATH_1;
       } else {
           filename = CONFIG_FILE_PATH_2;
       }
 
       if (gt1x_parse_config(filename, config) > 0) {
           if (gt1x_i2c_write(GTP_REG_CONFIG_DATA, config, GTP_CONFIG_MAX_LENGTH)) {
               GTP_ERROR("Update config failed!");
           } else {
               GTP_INFO("Update config successfully!");
           }
       }
   }
#endif
   return 0;
#endif
}
#if 0 //!GTP_HEADER_FW_UPDATE
static int gt1x_search_update_files(void)
{
   /*wait 10s(max) if fs is not ready*/
   int retry = 20 * 2;
   struct file *pfile = NULL;
   mm_segment_t old_fs;
   int found = 0;
 
   old_fs = get_fs();
   set_fs(KERNEL_DS);
 
   GTP_INFO("Search firmware file...");
   while (retry-- > 0) {
       msleep(500);
       /*check if rootfs is ready*/
       if (gt1x_check_fs_mounted("/data")) {
           GTP_DEBUG("filesystem is not ready");
           continue;
       }
       /*search firmware*/
       pfile = filp_open(UPDATE_FILE_PATH_1, O_RDONLY, 0);
       if (IS_ERR(pfile)) {
           pfile = filp_open(UPDATE_FILE_PATH_2, O_RDONLY, 0);
           if (!IS_ERR(pfile)) {
               found |= FOUND_FW_PATH_2;
           }
       } else {
           found |= FOUND_FW_PATH_1;
       }
 
       if (!IS_ERR(pfile)) {
           filp_close(pfile, NULL);
       }
       /*search config file*/
       pfile = filp_open(CONFIG_FILE_PATH_1, O_RDONLY, 0);
       if (IS_ERR(pfile)) {
           pfile = filp_open(CONFIG_FILE_PATH_2, O_RDONLY, 0);
           if (!IS_ERR(pfile)) {
               found |= FOUND_CFG_PATH_2;
           }
       } else {
           found |= FOUND_CFG_PATH_1;
       }
       if (!IS_ERR(pfile)) {
           filp_close(pfile, NULL);
       }
 
       if (found) {
           break;
       }
 
       GTP_INFO("Not found firmware or config file, retry.");
   }
   set_fs(old_fs);
 
   return found;
}
#endif
 
void gt1x_enter_update_mode(void)
{
   GTP_DEBUG("Enter FW update mode.");
#if GTP_ESD_PROTECT
   gt1x_esd_switch(SWITCH_OFF);
#endif
#if GTP_CHARGER_SWITCH
   gt1x_charger_switch(SWITCH_OFF);
#endif
   gt1x_irq_disable();
}
 
int gt1x_update_firmware(void *filename)
{
/*
 * gt1x update firmware doesn't meet gki
 */
   GTP_ERROR("gt1x update firmware failed\n");
   return 0;
#if 0
   int i = 0;
   int ret = 0;
   u8 *p;
 
   if (update_info.status != UPDATE_STATUS_IDLE) {
       GTP_ERROR("Update process is running!");
       return ERROR;
   }
   update_info.status = UPDATE_STATUS_RUNNING;
   update_info.progress = 0;
 
   gt1x_enter_update_mode();
 
   ret = gt1x_update_prepare(filename);
   if (ret) {
       update_info.status = UPDATE_STATUS_IDLE;
       return ret;
   }
 
   ret = gt1x_check_firmware();
   if (ret) {
       update_info.status = UPDATE_STATUS_ABORT;
       goto gt1x_update_exit;
   }
#if GTP_FW_UPDATE_VERIFY
   update_info.max_progress =
       6 + update_info.firmware->subsystem_count;
#else
   update_info.max_progress =
       3 + update_info.firmware->subsystem_count;
#endif
   update_info.progress++;
 
   ret = gt1x_update_judge();
   if (ret) {
       update_info.status = UPDATE_STATUS_ABORT;
       goto gt1x_update_exit;
   }
   update_info.progress++;
 
   p = gt1x_get_fw_data(update_info.firmware->subsystem[0].offset, update_info.firmware->subsystem[0].length);
   if (p == NULL) {
       GTP_ERROR("get isp fail");
       ret = ERROR_FW;
       update_info.status = UPDATE_STATUS_ABORT;
       goto gt1x_update_exit;
   }
   update_info.progress++;
 
   ret = gt1x_run_ss51_isp(p, update_info.firmware->subsystem[0].length);
   if (ret) {
       GTP_ERROR("run isp fail");
       goto gt1x_update_exit;
   }
   update_info.progress++;
   msleep(800);
 
   for (i = 1; i < update_info.firmware->subsystem_count; i++) {
       GTP_INFO("subsystem: %d", update_info.firmware->subsystem[i].type);
       GTP_INFO("Length: %d", update_info.firmware->subsystem[i].length);
       GTP_INFO("Address: %d", update_info.firmware->subsystem[i].address);
 
       ret = gt1x_burn_subsystem(&(update_info.firmware->subsystem[i]));
       if (ret) {
           GTP_ERROR("burn subsystem fail!");
           goto gt1x_update_exit;
       }
       update_info.progress++;
   }
 
#if GTP_FW_UPDATE_VERIFY
   gt1x_reset_guitar();
 
   p = gt1x_get_fw_data(update_info.firmware->subsystem[0].offset, update_info.firmware->subsystem[0].length);
   if (p == NULL) {
       GTP_ERROR("get isp fail");
       ret = ERROR_FW;
       goto gt1x_update_exit;
   }
   update_info.progress++;
 
   ret = gt1x_run_ss51_isp(p, update_info.firmware->subsystem[0].length);
   if (ret) {
       GTP_ERROR("run isp fail");
       goto gt1x_update_exit;
   }
   update_info.progress++;
 
   GTP_INFO("Reset guitar & check firmware in flash.");
   for (i = 1; i < update_info.firmware->subsystem_count; i++) {
       GTP_INFO("subsystem: %d", update_info.firmware->subsystem[i].type);
       GTP_INFO("Length: %d", update_info.firmware->subsystem[i].length);
       GTP_INFO("Address: %d", update_info.firmware->subsystem[i].address);
 
       ret = gt1x_check_subsystem_in_flash(&(update_info.firmware->subsystem[i]));
       if (ret) {
           gt1x_error_erase();
           break;
       }
   }
   update_info.progress++;
#endif
 
gt1x_update_exit:
   gt1x_update_cleanup();
   gt1x_leave_update_mode();
   gt1x_read_version(NULL);
   if (ret) {
       update_info.progress = 2 * update_info.max_progress;
       GTP_ERROR("Update firmware failed!");
       return ret;
   } else if (gt1x_init_failed) {
       gt1x_read_version(&gt1x_version);
       gt1x_init_panel();
#if GTP_CHARGER_SWITCH
       gt1x_parse_chr_cfg(gt1x_version.sensor_id);
#endif
#if GTP_SMART_COVER
       gt1x_parse_sc_cfg(gt1x_version.sensor_id);
#endif
   }
   GTP_INFO("Update firmware succeefully!");
   return ret;
#endif
}
 
#if 0 //close for GKI
int gt1x_update_prepare(char *filename)
{
   int ret = 0;
   int retry = 5;
   if (filename == NULL) {
#if GTP_HEADER_FW_UPDATE
       update_info.fw_name = NULL;
       update_info.update_type = UPDATE_TYPE_HEADER;
       update_info.fw_data = gt1x_default_FW;
       update_info.fw_length = sizeof(gt1x_default_FW);
#else
       GTP_ERROR("No Fw in .h file!");
       return ERROR_FW;
#endif
   } else {
       GTP_INFO("Firmware: %s", filename);
       update_info.old_fs = get_fs();
       set_fs(KERNEL_DS);
       update_info.fw_name = filename;
       update_info.update_type = UPDATE_TYPE_FILE;
       update_info.fw_file = filp_open(update_info.fw_name, O_RDONLY, 0);
       if (IS_ERR(update_info.fw_file)) {
           GTP_ERROR("Open update file(%s) error!", update_info.fw_name);
           set_fs(update_info.old_fs);
           return ERROR_FILE;
       }
       update_info.fw_file->f_op->llseek(update_info.fw_file, 0, SEEK_SET);
       update_info.fw_length = update_info.fw_file->f_op->llseek(update_info.fw_file, 0, SEEK_END);
   }
 
   while (retry > 0) {
       retry--;
       update_info.firmware = kzalloc(sizeof(struct fw_info), GFP_KERNEL);
       if (update_info.firmware == NULL) {
           GTP_INFO("Alloc %zu bytes memory fail.", sizeof(struct fw_info));
           continue;
       } else {
           GTP_INFO("Alloc %zu bytes memory success.", sizeof(struct fw_info));
           break;
       }
   }
   if (retry <= 0) {
       ret = ERROR_RETRY;
       goto gt1x_update_pre_fail1;
   }
 
   retry = 5;
   while (retry > 0) {
       update_info.buffer = kzalloc(1024 * 4, GFP_KERNEL);
       if (update_info.buffer == NULL) {
           GTP_ERROR("Alloc %d bytes memory fail.", 1024 * 4);
           continue;
       } else {
           GTP_INFO("Alloc %d bytes memory success.", 1024 * 4);
           break;
       }
   }
   if (retry <= 0) {
       ret = ERROR_RETRY;
       goto gt1x_update_pre_fail0;
   }
 
   return 0;
 
gt1x_update_pre_fail0:
   kfree(update_info.firmware);
gt1x_update_pre_fail1:
   filp_close(update_info.fw_file, NULL);
   return ret;
}
 
void gt1x_update_cleanup(void)
{
   if (update_info.update_type == UPDATE_TYPE_FILE) {
       if (update_info.fw_file != NULL) {
           filp_close(update_info.fw_file, NULL);
           update_info.fw_file = NULL;
       }
       set_fs(update_info.old_fs);
   }
 
   if (update_info.buffer != NULL) {
       kfree(update_info.buffer);
       update_info.buffer = NULL;
   }
   if (update_info.firmware != NULL) {
       kfree(update_info.firmware);
       update_info.firmware = NULL;
   }
}
 
int gt1x_check_firmware(void)
{
   u16 checksum;
   u16 checksum_in_header;
   u8 *p;
   struct fw_info *firmware;
   int i;
   int offset;
 
   /*compare file length with the length field in the firmware header*/
   if (update_info.fw_length < FW_HEAD_SIZE) {
       GTP_ERROR("Bad firmware!(file length: %d)", update_info.fw_length);
       return ERROR_CHECK;
   }
   p = gt1x_get_fw_data(0, 6);
   if (p == NULL) {
       return ERROR_FW;
   }
 
   if (getU32(p) + 6 != update_info.fw_length) {
       GTP_ERROR("Bad firmware!(file length: %d, header define: %d)", update_info.fw_length, getU32(p));
       return ERROR_CHECK;
   }
   /*check firmware's checksum*/
   checksum_in_header = getU16(&p[4]);
   checksum = 0;
   for (i = 6; i < update_info.fw_length; i++) {
       p = gt1x_get_fw_data(i, 1);
       if (p == NULL) {
           return ERROR_FW;
       }
       checksum += p[0];
   }
 
   if (checksum != checksum_in_header) {
       GTP_ERROR("Bad firmware!(checksum: 0x%04X, header define: 0x%04X)", checksum, checksum_in_header);
       return ERROR_CHECK;
   }
   /*parse firmware*/
   p = gt1x_get_fw_data(0, FW_HEAD_SIZE);
   if (p == NULL) {
       return ERROR_FW;
   }
   memcpy((u8 *) update_info.firmware, p, FW_HEAD_SIZE - 8 * 12);
   update_info.firmware->pid[5] = 0;
 
   p = &p[FW_HEAD_OFFSET_SUBSYSTEM_INFO_BASE];
   firmware = update_info.firmware;
   offset = FW_HEAD_SIZE;
   for (i = 0; i < firmware->subsystem_count; i++) {
       firmware->subsystem[i].type = p[i * FW_HEAD_SUBSYSTEM_INFO_SIZE];
       firmware->subsystem[i].length = getU16(&p[i * FW_HEAD_SUBSYSTEM_INFO_SIZE + 1]);
       firmware->subsystem[i].address = getU16(&p[i * FW_HEAD_SUBSYSTEM_INFO_SIZE + 3]) * 256;
       firmware->subsystem[i].offset = offset;
       offset += firmware->subsystem[i].length;
   }
 
   /*print update information*/
   GTP_INFO("Update type: %s", update_info.update_type == UPDATE_TYPE_HEADER ? "Header" : "File");
   GTP_INFO("Firmware length: %d", update_info.fw_length);
   GTP_INFO("Firmware product: GT%s", update_info.firmware->pid);
   GTP_INFO("Firmware patch: %02X%02X%02X", update_info.firmware->version[0], update_info.firmware->version[1], update_info.firmware->version[2]);
   GTP_INFO("Firmware chip: 0x%02X", update_info.firmware->chip_type);
   GTP_INFO("Subsystem count: %d", update_info.firmware->subsystem_count);
   for (i = 0; i < update_info.firmware->subsystem_count; i++) {
       GTP_DEBUG("------------------------------------------");
       GTP_DEBUG("Subsystem: %d", i);
       GTP_DEBUG("Type: %d", update_info.firmware->subsystem[i].type);
       GTP_DEBUG("Length: %d", update_info.firmware->subsystem[i].length);
       GTP_DEBUG("Address: 0x%08X", update_info.firmware->subsystem[i].address);
       GTP_DEBUG("Offset: %d", update_info.firmware->subsystem[i].offset);
   }
 
   return 0;
}
#endif
 
/**
 * @return: return a pointer pointed at the content of firmware
 *          if success, otherwise return NULL.
 */
u8 *gt1x_get_fw_data(u32 offset, int length)
{
   int ret;
   if (update_info.update_type == UPDATE_TYPE_FILE) {
       update_info.fw_file->f_op->llseek(update_info.fw_file, offset, SEEK_SET);
       ret = update_info.fw_file->f_op->read(update_info.fw_file, (char *)update_info.buffer, length, &update_info.fw_file->f_pos);
       if (ret < 0) {
           GTP_ERROR("Read data error!");
           return NULL;
       }
       return update_info.buffer;
   } else {
       return &update_info.fw_data[offset];
   }
}
 
int gt1x_update_judge(void)
{
   int ret;
   u8 reg_val[2] = {0};
   u8 retry = 2;
   struct gt1x_version_info ver_info;
   struct gt1x_version_info fw_ver_info;
 
   fw_ver_info.mask_id = (update_info.firmware->target_mask_version[0] << 16)
       | (update_info.firmware->target_mask_version[1] << 8)
       | (update_info.firmware->target_mask_version[2]);
   fw_ver_info.patch_id = (update_info.firmware->version[0] << 16)
       | (update_info.firmware->version[1] << 8)
       | (update_info.firmware->version[2]);
   memcpy(fw_ver_info.product_id, update_info.firmware->pid, 4);
   fw_ver_info.product_id[4] = 0;
 
   /* check fw status reg */
   do {
       ret = gt1x_i2c_read_dbl_check(GTP_REG_FW_CHK_MAINSYS, reg_val, 1);
       if (ret < 0) {    /* read reg failed */
           goto _reset;
       } else if (ret > 0) {
           continue;
       }
 
       ret = gt1x_i2c_read_dbl_check(GTP_REG_FW_CHK_SUBSYS, &reg_val[1], 1);
       if (ret < 0) {
           goto _reset;
       } else if (ret > 0) {
           continue;
       }
 
       break;
_reset:
       gt1x_reset_guitar();
   } while (--retry);
 
   if (!retry) {
       GTP_INFO("Update abort because of i2c error.");
       return ERROR_CHECK;
   }
   if (reg_val[0] != 0xBE || reg_val[1] == 0xAA) {
       GTP_INFO("Check fw status reg not pass,reg[0x814E]=0x%2X,reg[0x5095]=0x%2X!",
               reg_val[0], reg_val[1]);
       return 0;
   }
 
   ret = gt1x_read_version(&ver_info);
   if (ret < 0) {
       GTP_INFO("Get IC's version info failed, force update!");
       return 0;
   }
 
   if (memcmp(fw_ver_info.product_id, ver_info.product_id, 4)) {
       GTP_INFO("Product id is not match!");
       return ERROR_CHECK;
   }
   if ((fw_ver_info.mask_id & 0xFFFFFF00) != (ver_info.mask_id & 0xFFFFFF00)) {
       GTP_INFO("Mask id is not match!");
       return ERROR_CHECK;
   }
   if ((fw_ver_info.patch_id & 0xFF0000) != (ver_info.patch_id & 0xFF0000)) {
       GTP_INFO("CID is not equal, need update!");
       return 0;
   }
#if GTP_DEBUG_ON
   if (update_info.force_update) {
       GTP_DEBUG("Debug mode, force update fw.");
       return 0;
   }
#endif
   if ((fw_ver_info.patch_id & 0xFFFF) <= (ver_info.patch_id & 0xFFFF)) {
       GTP_INFO("The version of the fw is not high than the IC's!");
       return ERROR_CHECK;
   }
   return 0;
}
 
int __gt1x_hold_ss51_dsp_20(void)
{
   int ret = -1;
   int retry = 0;
   u8 buf[1];
   int hold_times = 0;
 
   while (retry++ < 30) {
       /*Hold ss51 & dsp*/
       buf[0] = 0x0C;
       ret = gt1x_i2c_write(_rRW_MISCTL__SWRST_B0_, buf, 1);
       if (ret) {
           GTP_ERROR("Hold ss51 & dsp I2C error,retry:%d", retry);
           continue;
       }
       /*Confirm hold*/
       buf[0] = 0x00;
       ret = gt1x_i2c_read(_rRW_MISCTL__SWRST_B0_, buf, 1);
       if (ret) {
           GTP_ERROR("Hold ss51 & dsp I2C error,retry:%d", retry);
           continue;
       }
       if (0x0C == buf[0]) {
           if (hold_times++ < 20) {
               continue;
           } else {
               break;
           }
       }
       GTP_ERROR("Hold ss51 & dsp confirm 0x4180 failed,value:%d", buf[0]);
   }
   if (retry >= 30) {
       GTP_ERROR("Hold ss51&dsp failed!");
       return ERROR_RETRY;
   }
 
   GTP_INFO("Hold ss51&dsp successfully.");
   return 0;
}
 
int gt1x_hold_ss51_dsp(void)
{
   int ret = ERROR, retry = 5;
   u8 buffer[2];
 
   do {
       gt1x_select_addr();
       ret =  gt1x_i2c_read(0x4220, buffer, 1);
 
   } while (retry-- && ret < 0);
 
   if (ret < 0)
       return ERROR;
 
   /*hold ss51_dsp*/
   ret = __gt1x_hold_ss51_dsp_20();
   if (ret) {
       return ret;
   }
   /*enable dsp & mcu power*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__DSP_MCU_PWR_, buffer, 1);
   if (ret) {
       GTP_ERROR("enabel dsp & mcu power fail!");
       return ret;
   }
   /*disable watchdog*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__TMR0_EN, buffer, 1);
   if (ret) {
       GTP_ERROR("disable wdt fail!");
       return ret;
   }
   /*clear cache*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__CACHE_EN, buffer, 1);
   if (ret) {
       GTP_ERROR("clear cache fail!");
       return ret;
   }
   /*soft reset*/
   buffer[0] = 0x01;
   ret = gt1x_i2c_write(_bWO_MISCTL__CPU_SWRST_PULSE, buffer, 1);
   if (ret) {
       GTP_ERROR("software reset fail!");
       return ret;
   }
   /*set scramble*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_rRW_MISCTL__BOOT_OPT_B0_, buffer, 1);
   if (ret) {
       GTP_ERROR("set scramble fail!");
       return ret;
   }
 
   return 0;
}
 
int gt1x_run_ss51_isp(u8 *ss51_isp, int length)
{
   int ret;
   u8 buffer[10];
 
   ret = gt1x_hold_ss51_dsp();
   if (ret) {
       return ret;
   }
   /*select bank4*/
   buffer[0] = 0x04;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__SRAM_BANK, buffer, 1);
   if (ret) {
       GTP_ERROR("select bank4 fail.");
       return ret;
   }
   /*enable patch area access*/
   buffer[0] = 0x01;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__PATCH_AREA_EN_, buffer, 1);
   if (ret) {
       GTP_ERROR("enable patch area access fail!");
       return ret;
   }
 
   GTP_INFO("ss51_isp length: %d, checksum: 0x%04X", length, gt1x_calc_checksum(ss51_isp, length));
   /*load ss51 isp*/
   ret = gt1x_i2c_write(0xC000, ss51_isp, length);
   if (ret) {
       GTP_ERROR("load ss51 isp fail!");
       return ret;
   }
   /*recall compare*/
   ret = gt1x_recall_check(ss51_isp, 0xC000, length);
   if (ret) {
       GTP_ERROR("recall check ss51 isp fail!");
       return ret;
   }
 
   memset(buffer, 0xAA, 10);
   ret = gt1x_i2c_write_with_readback(0x8140, buffer, 10);
 
   /*disable patch area access*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__PATCH_AREA_EN_, buffer, 1);
   if (ret) {
       GTP_ERROR("disable patch area access fail!");
       return ret;
   }
   /*set 0x8006*/
   memset(buffer, 0x55, 8);
   ret = gt1x_i2c_write_with_readback(0x8006, buffer, 8);
   if (ret) {
       GTP_ERROR("set 0x8006[0~7] 0x55 fail!");
       return ret;
   }
   /*release ss51*/
   buffer[0] = 0x08;
   ret = gt1x_i2c_write_with_readback(_rRW_MISCTL__SWRST_B0_, buffer, 1);
   if (ret) {
       GTP_ERROR("release ss51 fail!");
       return ret;
   }
 
   msleep(100);
   /*check run state*/
   ret = gt1x_i2c_read(0x8006, buffer, 2);
   if (ret) {
       GTP_ERROR("read 0x8006 fail!");
       return ret;
   }
   if (!(buffer[0] == 0xAA && buffer[1] == 0xBB)) {
       GTP_ERROR("ERROR: isp is not running! 0x8006: %02X %02X", buffer[0], buffer[1]);
       return ERROR_CHECK;
   }
 
   return 0;
}
 
u16 gt1x_calc_checksum(u8 *fw, u32 length)
{
   u32 i = 0;
   u32 checksum = 0;
 
   for (i = 0; i < length; i += 2) {
       checksum += (((int)fw[i]) << 8);
       checksum += fw[i + 1];
   }
   return (checksum & 0xFFFF);
}
 
int gt1x_recall_check(u8 *chk_src, u16 start_addr, u16 chk_length)
{
   u8 rd_buf[PACK_SIZE];
   s32 ret = 0;
   u16 len = 0;
   u32 compared_length = 0;
 
   while (chk_length > 0) {
       len = (chk_length > PACK_SIZE ? PACK_SIZE : chk_length);
 
       ret = gt1x_i2c_read(start_addr + compared_length, rd_buf, len);
       if (ret) {
           GTP_ERROR("recall i2c error,exit!");
           return ret;
       }
 
       if (memcmp(rd_buf, &chk_src[compared_length], len)) {
           GTP_ERROR("Recall frame not equal(addr: 0x%04X)", start_addr + compared_length);
           GTP_DEBUG("chk_src array:");
           GTP_DEBUG_ARRAY(&chk_src[compared_length], len);
           GTP_DEBUG("recall array:");
           GTP_DEBUG_ARRAY(rd_buf, len);
           return ERROR_CHECK;
       }
 
       chk_length -= len;
       compared_length += len;
   }
 
   GTP_DEBUG("Recall check %d bytes(address: 0x%04X) success.", compared_length, start_addr);
   return 0;
}
 
int gt1x_burn_subsystem(struct fw_subsystem_info *subsystem)
{
   int block_len;
   u16 checksum;
   int burn_len = 0;
   u16 cur_addr;
   u32 length = subsystem->length;
   u8 buffer[10];
   int ret;
   int wait_time;
   int burn_state;
   int retry = 5;
   u8 *fw;
 
   GTP_INFO("Subsystem: %d", subsystem->type);
   GTP_INFO("Length: %d", subsystem->length);
   GTP_INFO("Address: 0x%08X", subsystem->address);
 
   while (length > 0 && retry > 0) {
       retry--;
 
       block_len = length > 1024 * 4 ? 1024 * 4 : length;
 
       GTP_INFO("Burn block ==> length: %d, address: 0x%08X", block_len, subsystem->address + burn_len);
       fw = gt1x_get_fw_data(subsystem->offset + burn_len, block_len);
       if (!fw)
           return ERROR_FW;
       cur_addr = ((subsystem->address + burn_len) >> 8);
 
       checksum = 0;
       checksum += block_len;
       checksum += cur_addr;
       checksum += gt1x_calc_checksum(fw, block_len);
       checksum = (0 - checksum);
 
       buffer[0] = ((block_len >> 8) & 0xFF);
       buffer[1] = (block_len & 0xFF);
       buffer[2] = ((cur_addr >> 8) & 0xFF);
       buffer[3] = (cur_addr & 0xFF);
 
       ret = gt1x_i2c_write_with_readback(0x8100, buffer, 4);
       if (ret) {
           GTP_ERROR("write length & address fail!");
           continue;
       }
 
       ret = gt1x_i2c_write(0x8100 + 4, fw, block_len);
       if (ret) {
           GTP_ERROR("write fw data fail!");
           continue;
       }
 
       buffer[0] = ((checksum >> 8) & 0xFF);
       buffer[1] = (checksum & 0xFF);
       ret = gt1x_i2c_write_with_readback(0x8100 + 4 + block_len, buffer, 2);
       if (ret) {
           GTP_ERROR("write checksum fail!");
           continue;
       }
 
       buffer[0] = 0;
       ret = gt1x_i2c_write_with_readback(0x8022, buffer, 1);
       if (ret) {
           GTP_ERROR("clear control flag fail!");
           continue;
       }
 
       buffer[0] = subsystem->type;
       buffer[1] = subsystem->type;
       ret = gt1x_i2c_write_with_readback(0x8020, buffer, 2);
       if (ret) {
           GTP_ERROR("write subsystem type fail!");
           continue;
       }
       burn_state = ERROR;
       wait_time = 200;
       usleep_range(5000, 6000);
 
       while (wait_time-- > 0) {
           u8 confirm = 0x55;
 
           ret = gt1x_i2c_read(0x8022, buffer, 1);
           if (ret < 0) {
               continue;
           }
           usleep_range(5000, 6000);
           ret = gt1x_i2c_read(0x8022, &confirm, 1);
           if (ret < 0) {
               continue;
           }
           if (buffer[0] != confirm)
               continue;
 
           if (buffer[0] == 0xAA) {
               GTP_DEBUG("burning.....");
               continue;
           } else if (buffer[0] == 0xDD) {
               GTP_ERROR("checksum error!");
               break;
           } else if (buffer[0] == 0xBB) {
               GTP_INFO("burning success.");
               burn_state = 0;
               break;
           } else if (buffer[0] == 0xCC) {
               GTP_ERROR("burning failed!");
               break;
           } else {
               GTP_DEBUG("unknown state!(0x8022: 0x%02X)", buffer[0]);
           }
       }
 
       if (!burn_state) {
           length -= block_len;
           burn_len += block_len;
           retry = 5;
       }
   }
   if (length == 0) {
       return 0;
   } else {
       return ERROR_RETRY;
   }
}
 
int gt1x_check_subsystem_in_flash(struct fw_subsystem_info *subsystem)
{
   int block_len;
   int checked_len = 0;
   u32 length = subsystem->length;
   int ret;
   int check_state = 0;
   int retry = 5;
   u8 *fw;
 
   GTP_INFO("Subsystem: %d", subsystem->type);
   GTP_INFO("Length: %d", subsystem->length);
   GTP_INFO("Address: 0x%08X", subsystem->address);
 
   while (length > 0) {
       block_len = length > 1024 * 4 ? 1024 * 4 : length;
 
       GTP_INFO("Check block ==> length: %d, address: 0x%08X", block_len, subsystem->address + checked_len);
       fw = gt1x_get_fw_data(subsystem->offset + checked_len, block_len);
       if (fw == NULL) {
           return ERROR_FW;
       }
       ret = gt1x_read_flash(subsystem->address + checked_len, block_len);
       if (ret) {
           check_state |= ret;
       }
 
       ret = gt1x_recall_check(fw, 0x8100, block_len);
       if (ret) {
           GTP_ERROR("Block in flash is broken!");
           check_state |= ret;
       }
 
       length -= block_len;
       checked_len += block_len;
       retry = 5;
   }
   if (check_state) {
       GTP_ERROR("Subsystem in flash is broken!");
   } else {
       GTP_INFO("Subsystem in flash is correct!");
   }
   return check_state;
}
 
int gt1x_read_flash(u32 addr, int length)
{
   int wait_time;
   int ret = 0;
   u8 buffer[4];
   u16 read_addr = (addr >> 8);
 
   GTP_INFO("Read flash: 0x%04X, length: %d", addr, length);
 
   buffer[0] = 0;
   ret = gt1x_i2c_write_with_readback(0x8022, buffer, 1);
 
   buffer[0] = ((length >> 8) & 0xFF);
   buffer[1] = (length & 0xFF);
   buffer[2] = ((read_addr >> 8) & 0xFF);
   buffer[3] = (read_addr & 0xFF);
   ret |= gt1x_i2c_write_with_readback(0x8100, buffer, 4);
 
   buffer[0] = 0xAA;
   buffer[1] = 0xAA;
   ret |= gt1x_i2c_write(0x8020, buffer, 2);
   if (ret) {
       GTP_ERROR("Error occured.");
       return ret;
   }
 
   wait_time = 200;
   while (wait_time > 0) {
       wait_time--;
       usleep_range(5000, 6000);
       ret = gt1x_i2c_read_dbl_check(0x8022, buffer, 1);
       if (ret) {
           continue;
       }
       if (buffer[0] == 0xBB) {
           GTP_INFO("Read success(addr: 0x%04X, length: %d)", addr, length);
           break;
       }
   }
   if (wait_time == 0) {
       GTP_ERROR("Read Flash FAIL!");
       return ERROR_RETRY;
   }
   return 0;
}
 
int gt1x_error_erase(void)
{
   int block_len;
   u16 checksum;
   u16 erase_addr;
   u8 buffer[10];
   int ret;
   int wait_time;
   int burn_state = ERROR;
   int retry = 5;
   u8 *fw = NULL;
 
   GTP_INFO("Erase flash area of ss51.");
 
   gt1x_reset_guitar();
 
   fw = gt1x_get_fw_data(update_info.firmware->subsystem[0].offset,
           update_info.firmware->subsystem[0].length);
   if (!fw) {
       GTP_ERROR("get isp fail");
       return ERROR_FW;
   }
   ret = gt1x_run_ss51_isp(fw, update_info.firmware->subsystem[0].length);
   if (ret) {
       GTP_ERROR("run isp fail");
       return ERROR_PATH;
   }
 
   fw = kmalloc(1024 * 4, GFP_KERNEL);
   if (!fw) {
       GTP_ERROR("error when alloc mem.");
       return ERROR_MEM;
   }
 
   memset(fw, 0xFF, 1024 * 4);
   erase_addr = 0x00;
   block_len = 1024 * 4;
 
   while (retry-- > 0) {
       checksum = 0;
       checksum += block_len;
       checksum += erase_addr;
       checksum += gt1x_calc_checksum(fw, block_len);
       checksum = (0 - checksum);
 
       buffer[0] = ((block_len >> 8) & 0xFF);
       buffer[1] = (block_len & 0xFF);
       buffer[2] = ((erase_addr >> 8) & 0xFF);
       buffer[3] = (erase_addr & 0xFF);
 
       ret = gt1x_i2c_write_with_readback(0x8100, buffer, 4);
       if (ret) {
           GTP_ERROR("write length & address fail!");
           continue;
       }
 
       ret = gt1x_i2c_write(0x8100 + 4, fw, block_len);
       if (ret) {
           GTP_ERROR("write fw data fail!");
           continue;
       }
 
       ret = gt1x_recall_check(fw, 0x8100 + 4, block_len);
       if (ret)
           continue;
 
       buffer[0] = ((checksum >> 8) & 0xFF);
       buffer[1] = (checksum & 0xFF);
       ret = gt1x_i2c_write_with_readback(0x8100 + 4 + block_len, buffer, 2);
       if (ret) {
           GTP_ERROR("write checksum fail!");
           continue;
       }
 
       buffer[0] = 0;
       ret = gt1x_i2c_write_with_readback(0x8022, buffer, 1);
       if (ret) {
           GTP_ERROR("clear control flag fail!");
           continue;
       }
 
       buffer[0] = FW_SECTION_TYPE_SS51_PATCH;
       buffer[1] = FW_SECTION_TYPE_SS51_PATCH;
       ret = gt1x_i2c_write_with_readback(0x8020, buffer, 2);
       if (ret) {
           GTP_ERROR("write subsystem type fail!");
           continue;
       }
       burn_state = ERROR;
       wait_time = 200;
       while (wait_time > 0) {
           wait_time--;
           usleep_range(5000, 6000);
           ret = gt1x_i2c_read_dbl_check(0x8022, buffer, 1);
           if (ret)
               continue;
 
           if (buffer[0] == 0xAA) {
               GTP_DEBUG("burning.....");
               continue;
           } else if (buffer[0] == 0xDD) {
               GTP_ERROR("checksum error!");
               break;
           } else if (buffer[0] == 0xBB) {
               GTP_INFO("burning success.");
               burn_state = 0;
               break;
           } else if (buffer[0] == 0xCC) {
               GTP_ERROR("burning failed!");
               break;
           } else {
               GTP_DEBUG("unknown state!(0x8022: 0x%02X)", buffer[0]);
           }
       }
    }
 
    kfree(fw);
    if (burn_state == 0)
       return 0;
    else
       return ERROR_RETRY;
}
 
void gt1x_leave_update_mode(void)
{
   GTP_DEBUG("Leave FW update mode.");
   if (update_info.status != UPDATE_STATUS_ABORT)
       gt1x_reset_guitar();
#if GTP_CHARGER_SWITCH
   gt1x_charger_switch(SWITCH_ON);
#endif
#if GTP_ESD_PROTECT
   gt1x_esd_switch(SWITCH_ON);
#endif
   update_info.status = UPDATE_STATUS_IDLE;
   gt1x_irq_enable();
}
 
#if 0 //close for GKI
void dump_to_file(u16 addr, int length, char *filepath)
{
   struct file *flp = NULL;
   u8 buf[128];
   const int READ_BLOCK_SIZE = 128;
   int read_length = 0;
   int len = 0;
 
   GTP_INFO("Dump(0x%04X, %d bytes) to file: %s\n", addr, length, filepath);
   flp = filp_open(filepath, O_RDWR | O_CREAT, 0666);
   if (IS_ERR(flp)) {
       GTP_ERROR("can not open file: %s\n", filepath);
       return;
   }
   flp->f_op->llseek(flp, 0, SEEK_SET);
 
   while (length > 0) {
       len = (length > READ_BLOCK_SIZE ? READ_BLOCK_SIZE : length);
       memset(buf, 0x33, len);
       if (gt1x_i2c_read(addr + read_length, buf, len))
           memset(buf, 0x33, len);
       flp->f_op->write(flp, (char *)buf, len, &flp->f_pos);
       read_length += len;
       length -= len;
   }
   filp_close(flp, NULL);
}
#endif
 
int gt1x_hold_ss51_dsp_no_reset(void)
{
   int ret = ERROR;
   u8 buffer[2];
 
   /*hold ss51_dsp*/
   ret = __gt1x_hold_ss51_dsp_20();
   if (ret)
       return ret;
   /*enable dsp & mcu power*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__DSP_MCU_PWR_, buffer, 1);
   if (ret) {
       GTP_ERROR("enabel dsp & mcu power fail!");
       return ret;
   }
   /*disable watchdog*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__TMR0_EN, buffer, 1);
   if (ret) {
       GTP_ERROR("disable wdt fail!");
       return ret;
   }
   /*clear cache*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__CACHE_EN, buffer, 1);
   if (ret) {
       GTP_ERROR("clear cache fail!");
       return ret;
   }
   /*soft reset*/
   buffer[0] = 0x01;
   ret = gt1x_i2c_write(_bWO_MISCTL__CPU_SWRST_PULSE, buffer, 1);
   if (ret) {
       GTP_ERROR("software reset fail!");
       return ret;
   }
   /*set scramble*/
   buffer[0] = 0x00;
   ret = gt1x_i2c_write_with_readback(_rRW_MISCTL__BOOT_OPT_B0_, buffer, 1);
   if (ret) {
       GTP_ERROR("set scramble fail!");
       return ret;
   }
 
   return 0;
}
 
#define GT1X_LOAD_PACKET_SIZE (1024 * 2)
 
int gt1x_load_patch(u8 *patch, u32 patch_size, int offset, int bank_size)
{
   s32 loaded_length = 0;
   s32 len = 0;
   s32 ret = 0;
   u8 bank = 0, tmp;
   u16 address;
 
   GTP_INFO("Load patch code(size: %d, checksum: 0x%04X, position: 0x%04X, bank-size: %d", patch_size, gt1x_calc_checksum(patch, patch_size), 0xC000 + offset, bank_size);
   while (loaded_length != patch_size) {
       if (loaded_length == 0 || (loaded_length + offset) % bank_size == 0) {
           /*select bank*/
           bank = 0x04 + (loaded_length + offset) / bank_size;
           ret = gt1x_i2c_write(_bRW_MISCTL__SRAM_BANK, &bank, 1);
           if (ret) {
               GTP_ERROR("select bank%d fail!", bank);
               return ret;
           }
           GTP_INFO("Select bank%d success.", bank);
           /*enable patch area access*/
           tmp = 0x01;
           ret = gt1x_i2c_write_with_readback(_bRW_MISCTL__PATCH_AREA_EN_ + bank - 4, &tmp, 1);
           if (ret) {
               GTP_ERROR("enable patch area access fail!");
               return ret;
           }
       }
 
       len = patch_size - loaded_length > GT1X_LOAD_PACKET_SIZE ? GT1X_LOAD_PACKET_SIZE : patch_size - loaded_length;
       address = 0xC000 + (loaded_length + offset) % bank_size;
 
       ret = gt1x_i2c_write(address, &patch[loaded_length], len);
       if (ret) {
           GTP_ERROR("load 0x%04X, %dbytes fail!", address, len);
           return ret;
       }
       ret = gt1x_recall_check(&patch[loaded_length], address, len);
       if (ret) {
           GTP_ERROR("Recall check 0x%04X, %dbytes fail!", address, len);
           return ret;
       }
       GTP_INFO("load code 0x%04X, %dbytes success.", address, len);
 
       loaded_length += len;
   }
 
   return 0;
}
 
int gt1x_startup_patch(void)
{
   s32 ret = 0;
   u8 buffer[8] = { 0x55 };
 
   buffer[0] = 0x00;
   buffer[1] = 0x00;
   ret |= gt1x_i2c_write(_bRW_MISCTL__PATCH_AREA_EN_, buffer, 2);
 
   memset(buffer, 0x55, 8);
   ret |= gt1x_i2c_write(GTP_REG_FLASH_PASSBY, buffer, 8);
   ret |= gt1x_i2c_write(GTP_REG_VERSION, buffer, 5);
 
   buffer[0] = 0xAA;
   ret |= gt1x_i2c_write(GTP_REG_CMD, buffer, 1);
   ret |= gt1x_i2c_write(GTP_REG_ESD_CHECK, buffer, 1);
 
   buffer[0] = 0x00;
   ret |= gt1x_i2c_write(_rRW_MISCTL__SWRST_B0_, buffer, 1);
 
   msleep(200);
 
   return ret;
}