huangcm
2024-10-12 1f4dc0fe687f74148f03d6ac2ead47c9b45b9463
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
/* drivers/input/touchscreen/gt813_827_828_update.c
 * 
 * 2010 - 2012 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.
 * 
 * Latest Version:1.6
 * Author: andrew@goodix.com
 * Revision Record: 
 *      V1.0:
 *          first release. By Andrew, 2012/08/31
 *      V1.2:
 *          add force update,GT9110P pid map. By Andrew, 2012/10/15
 *      V1.4:
 *          1. add config auto update function;
 *          2. modify enter_update_mode;
 *          3. add update file cal checksum.
 *                          By Andrew, 2012/12/12
 *      V1.6: 
 *          1. replace guitar_client with i2c_connect_client;
 *          2. support firmware header array update.
 *                          By Meta, 2013/03/11
 */
#include <linux/kthread.h>
#include "gt9xx_ts.h"
#if GTP_HEADER_FW_UPDATE
#include <linux/namei.h>
#include "gt9xx_firmware.h"
#endif
 
#define GUP_REG_HW_INFO             0x4220
#define GUP_REG_FW_MSG              0x41E4
#define GUP_REG_PID_VID             0x8140
 
#define GUP_SEARCH_FILE_TIMES       50
#define UPDATE_FILE_PATH_2          "/data/_goodix_update_.bin"
#define UPDATE_FILE_PATH_1          "/sdcard/_goodix_update_.bin"
 
#define CONFIG_FILE_PATH_1          "/data/_goodix_config_.cfg"
#define CONFIG_FILE_PATH_2          "/sdcard/_goodix_config_.cfg"
 
#define FW_HEAD_LENGTH               14
#define FW_SECTION_LENGTH            0x2000
#define FW_DSP_ISP_LENGTH            0x1000
#define FW_DSP_LENGTH                0x1000
#define FW_BOOT_LENGTH               0x800
 
#define PACK_SIZE                    256
#define MAX_FRAME_CHECK_TIME         5
 
#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 FAIL    0
#define SUCCESS 1
 
#pragma pack(1)
typedef struct 
{
    u8  hw_info[4];          //hardware info//
    u8  pid[8];              //product id   //
    u16 vid;                 //version id   //
}st_fw_head;
#pragma pack()
 
typedef struct
{
    u8 force_update;
    u8 fw_flag;
    struct file *file; 
    struct file *cfg_file;
    st_fw_head  ic_fw_msg;
    mm_segment_t old_fs;
}st_update_msg;
 
st_update_msg update_msg;
u16 show_len;
u16 total_len;
u8 got_file_flag = 0;
 
extern void gtp_reset_guitar(struct i2c_client *client, s32 ms);
extern s32  gtp_send_cfg(struct i2c_client *client);
extern struct i2c_client * i2c_connect_client;
extern void gtp_irq_enable(struct goodix_ts_data *ts);
extern void gtp_irq_disable(struct goodix_ts_data *ts);
extern void gtp_set_io_int(void);
extern void gtp_io_init(int ms);
extern void gtp_set_int_value(int status);
extern int ctp_wakeup(int status,int ms);
 
/*******************************************************
Function:
    Read data from the i2c slave device.
Input:
    client:     i2c device.
    buf[0~1]:   read start address.
    buf[2~len-1]:   read data buffer.
    len:    GTP_ADDR_LENGTH + read bytes count
Output:
    numbers of i2c_msgs to transfer: 
      2: succeed, otherwise: failed
*********************************************************/
s32 gup_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
{
        struct i2c_msg msgs[2];
        s32 ret=-1;
        s32 retries = 0;
        
        GTP_DEBUG_FUNC();
        
        msgs[0].flags = !I2C_M_RD;
        msgs[0].addr  = client->addr;
        msgs[0].len   = GTP_ADDR_LENGTH;
        msgs[0].buf   = &buf[0];
        
        msgs[1].flags = I2C_M_RD;
        msgs[1].addr  = client->addr;
        msgs[1].len   = len - GTP_ADDR_LENGTH;
        msgs[1].buf   = &buf[GTP_ADDR_LENGTH];
        
        while(retries < 5) {
                ret = i2c_transfer(client->adapter, msgs, 2);
                if(ret == 2)break;
                retries++;
        }
        
        return ret;
}
 
/*******************************************************
Function:
    Write data to the i2c slave device.
Input:
    client:     i2c device.
    buf[0~1]:   write start address.
    buf[2~len-1]:   data buffer
    len:    GTP_ADDR_LENGTH + write bytes count
Output:
    numbers of i2c_msgs to transfer: 
        1: succeed, otherwise: failed
*********************************************************/
s32 gup_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
{
        struct i2c_msg msg;
        s32 ret=-1;
        s32 retries = 0;
        
        GTP_DEBUG_FUNC();
        
        msg.flags = !I2C_M_RD;
        msg.addr  = client->addr;
        msg.len   = len;
        msg.buf   = buf;
        
        while(retries < 5){
                ret = i2c_transfer(client->adapter, &msg, 1);
                if (ret == 1)break;
                retries++;
        }
        
        return ret;
}
 
static u8 gup_get_ic_msg(struct i2c_client *client, u16 addr, u8* msg, s32 len)
{
        s32 i = 0;
        
        msg[0] = (addr >> 8) & 0xff;
        msg[1] = addr & 0xff;
        
        for (i = 0; i < 5; i++) {
                if (gup_i2c_read(client, msg, GTP_ADDR_LENGTH + len) > 0) {
                    break;
                }
        }
        
        if (i >= 5) {
                GTP_ERROR("Read data from 0x%02x%02x failed!", msg[0], msg[1]);
                return FAIL;
        }
        
        return SUCCESS;
}
 
static u8 gup_set_ic_msg(struct i2c_client *client, u16 addr, u8 val)
{
        s32 i = 0;
        u8 msg[3];
        
        msg[0] = (addr >> 8) & 0xff;
        msg[1] = addr & 0xff;
        msg[2] = val;
        
        for (i = 0; i < 5; i++) {
                if (gup_i2c_write(client, msg, GTP_ADDR_LENGTH + 1) > 0) {
                        break;
                }
        }
        
        if (i >= 5) {
                GTP_ERROR("Set data to 0x%02x%02x failed!", msg[0], msg[1]);
                return FAIL;
        }
        
        return SUCCESS;
}
 
static u8 gup_get_ic_fw_msg(struct i2c_client *client)
{
        s32 ret = -1;
        u8  retry = 0;
        u8  buf[16];
        u8  i;
        
        //step1:get hardware info
        ret = gup_get_ic_msg(client, GUP_REG_HW_INFO, buf, 4);
        if(FAIL == ret) {
                GTP_ERROR("Read hardware info fail.");
                return ret;
        }
        
        memcpy(update_msg.ic_fw_msg.hw_info, &buf[GTP_ADDR_LENGTH], 4);
        for(i=0; i<4; i++) {
                update_msg.ic_fw_msg.hw_info[i] = buf[GTP_ADDR_LENGTH + 3 - i];
        } 
        GTP_DEBUG("IC Hardware info:%02x%02x%02x%02x", update_msg.ic_fw_msg.hw_info[0], update_msg.ic_fw_msg.hw_info[1],
                                                   update_msg.ic_fw_msg.hw_info[2], update_msg.ic_fw_msg.hw_info[3]);
        //step2:get firmware message
        for(retry=0; retry<2; retry++) {
                ret = gup_get_ic_msg(client, GUP_REG_FW_MSG, buf, 1);
                if(FAIL == ret) {
                        GTP_ERROR("Read firmware message fail.");
                        return ret;
                }
        
                update_msg.force_update = buf[GTP_ADDR_LENGTH];
                if((0xBE != update_msg.force_update)&&(!retry)) {
                        GTP_INFO("The check sum in ic is error.");
                        GTP_INFO("The IC will be updated by force.");
                        continue;
                }
                break;
        }
        GTP_DEBUG("IC force update flag:0x%x", update_msg.force_update);
        
        //step3:get pid & vid
        ret = gup_get_ic_msg(client, GUP_REG_PID_VID, buf, 6);
        if(FAIL == ret) {
                GTP_ERROR("Read product id & version id fail.");
                return ret;
        }
        memset(update_msg.ic_fw_msg.pid, 0, sizeof(update_msg.ic_fw_msg.pid));
        memcpy(update_msg.ic_fw_msg.pid, &buf[GTP_ADDR_LENGTH], 4);
        GTP_DEBUG("IC Product id:%s", update_msg.ic_fw_msg.pid);
        
        //GT9XX PID MAPPING
        /*|-----FLASH-----RAM-----|
        |------918------918-----|
        |------968------968-----|
        |------913------913-----|
        |------913P-----913P----|
        |------927------927-----|
        |------927P-----927P----|
        |------9110-----9110----|
        |------9110P----9111----|*/
        if(update_msg.ic_fw_msg.pid[0] != 0) {
                if(!memcmp(update_msg.ic_fw_msg.pid, "9111", 4)) {
                        GTP_DEBUG("IC Mapping Product id:%s", update_msg.ic_fw_msg.pid);
                        memcpy(update_msg.ic_fw_msg.pid, "9110P", 5);
                }
        }
        
        update_msg.ic_fw_msg.vid = buf[GTP_ADDR_LENGTH+4] + (buf[GTP_ADDR_LENGTH+5]<<8);
        GTP_DEBUG("IC version id:%04x", update_msg.ic_fw_msg.vid);
        
        return SUCCESS;
}
 
s32 gup_enter_update_mode(struct i2c_client *client)
{
        s32 ret = -1;
        s32 retry = 0;
        u8 rd_buf[3];
        
        //step1:RST output low last at least 2ms
        ctp_wakeup(0, 0);
        msleep(5);
        
        //step2:select I2C slave addr,INT:0--0xBA;1--0x28.
        gtp_set_int_value(client->addr == 0x14);
        msleep(2);
        
        //step3:RST output high reset guitar
        ctp_wakeup(1, 0);
        
        gtp_set_io_int();
        //20121211 modify start
        msleep(6);
        while(retry++ < 200) {
                //step4:Hold ss51 & dsp
                ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
                if(ret <= 0) {
                        GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
                        continue;
                }
                
                //step5:Confirm hold
                ret = gup_get_ic_msg(client, _rRW_MISCTL__SWRST_B0_, rd_buf, 1);
                if(ret <= 0) {
                        GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
                        continue;
                }
                if(0x0C == rd_buf[GTP_ADDR_LENGTH]) {
                        GTP_DEBUG("Hold ss51 & dsp confirm SUCCESS");
                        break;
                }
                GTP_DEBUG("Hold ss51 & dsp confirm 0x4180 failed,value:%d", rd_buf[GTP_ADDR_LENGTH]);
        }
        
        if(retry >= 200) {
                GTP_ERROR("Enter update Hold ss51 failed.");
                return FAIL;
        }
        
        //step6:DSP_CK and DSP_ALU_CK PowerOn
        ret = gup_set_ic_msg(client, 0x4010, 0x00);
        
        //20121211 modify end
        return ret;
}
 
void gup_leave_update_mode(void)
{
    
        GTP_DEBUG("[leave_update_mode]reset chip.");
        gtp_io_init(20);
        gtp_set_io_int();
}
 
static u8 gup_enter_update_judge(st_fw_head *fw_head)
{
        //Get the correct nvram data
        //The correct conditions: 
        //1. the hardware info is the same
        //2. the product id is the same
        //3. the firmware version in update file is greater than the firmware version in ic 
        //or the check sum in ic is wrong
        u16 u16_tmp;
        
        u16_tmp = fw_head->vid;
        fw_head->vid = (u16)(u16_tmp>>8) + (u16)(u16_tmp<<8);
        
        GTP_DEBUG("FILE HARDWARE INFO:%02x%02x%02x%02x", fw_head->hw_info[0], fw_head->hw_info[1], fw_head->hw_info[2], fw_head->hw_info[3]);
        GTP_DEBUG("FILE PID:%s", fw_head->pid);
        GTP_DEBUG("FILE VID:%04x", fw_head->vid);
        
        GTP_DEBUG("IC HARDWARE INFO:%02x%02x%02x%02x", update_msg.ic_fw_msg.hw_info[0], update_msg.ic_fw_msg.hw_info[1],
                                                   update_msg.ic_fw_msg.hw_info[2], update_msg.ic_fw_msg.hw_info[3]);
        GTP_DEBUG("IC PID:%s", update_msg.ic_fw_msg.pid);
        GTP_DEBUG("IC VID:%04x", update_msg.ic_fw_msg.vid);
        
        //First two conditions
        if ( !memcmp(fw_head->hw_info, update_msg.ic_fw_msg.hw_info, 
            sizeof(update_msg.ic_fw_msg.hw_info))) {
                
                GTP_DEBUG("Get the same hardware info.");
                if( update_msg.force_update != 0xBE ){
                        GTP_INFO("FW chksum error,need enter update.");
                        return SUCCESS;
                }
               
               if (fw_head->vid > update_msg.ic_fw_msg.vid) {
        
                                GTP_INFO("Need enter update.");
                                return SUCCESS;
                }
                if (( !memcmp(fw_head->pid, update_msg.ic_fw_msg.pid, 
                    (strlen(fw_head->pid)<3?3:strlen(fw_head->pid))))||
                    (!memcmp(update_msg.ic_fw_msg.pid, "91XX", 4))||
                    (!memcmp(fw_head->pid, "91XX", 4))) {
                        
                        if(!memcmp(fw_head->pid, "91XX", 4)) {
                                GTP_DEBUG("Force none same pid update mode.");
                        }else {
                                GTP_DEBUG("Get the same pid.");
                        }
 
#if 0 
                        //The third condition
                        if (fw_head->vid > update_msg.ic_fw_msg.vid) {
        
                                GTP_INFO("Need enter update.");
                                return SUCCESS;
                        }
#endif
                        GTP_ERROR("Don't meet the third condition.");
                }       
        }    
        return FAIL;
}
 
#if !GTP_HEADER_FW_UPDATE
static u8 ascii2hex(u8 a)
{
        s8 value = 0;
        
        if(a >= '0' && a <= '9') {
                value = a - '0';
        }else if(a >= 'A' && a <= 'F') {
                value = a - 'A' + 0x0A;
        }else if(a >= 'a' && a <= 'f') {
                value = a - 'a' + 0x0A;
        }else {
                value = 0xff;
        }
        
        return value;
}
 
static s8 gup_update_config(struct i2c_client *client)
{
        s32 file_len = 0;
        s32 ret = 0;
        s32 i = 0;
        s32 file_cfg_len = 0;
        s32 chip_cfg_len = 0;
        s32 count = 0;
        u8 *buf;
        u8 *pre_buf;
        u8 *file_config;
        u8 checksum = 0;
        u8 pid[8];
        
        if(NULL == update_msg.cfg_file) {
                GTP_ERROR("[update_cfg]No need to upgrade config!");
                return FAIL;
        }
        file_len = update_msg.cfg_file->f_op->llseek(update_msg.cfg_file, 0, SEEK_END);
        
        ret = gup_get_ic_msg(client, GUP_REG_PID_VID, pid, 6);
        if(FAIL == ret){
                GTP_ERROR("[update_cfg]Read product id & version id fail.");
                return FAIL;
        }
        pid[5] = '\0';
        GTP_DEBUG("update cfg get pid:%s", &pid[GTP_ADDR_LENGTH]);
        
        chip_cfg_len = 186;
        if(!memcmp(&pid[GTP_ADDR_LENGTH], "968", 3)) {
                chip_cfg_len = 228;
        }
        GTP_DEBUG("[update_cfg]config file len:%d", file_len);
        GTP_DEBUG("[update_cfg]need config len:%d",chip_cfg_len);
        if((file_len+5) < chip_cfg_len*5) {
                GTP_ERROR("Config length error");
                return -1;
        }
        
        buf = (u8*)kzalloc(file_len, GFP_KERNEL);
        pre_buf = (u8*)kzalloc(file_len, GFP_KERNEL);
        file_config = (u8*)kzalloc(chip_cfg_len + GTP_ADDR_LENGTH, GFP_KERNEL);
        update_msg.cfg_file->f_op->llseek(update_msg.cfg_file, 0, SEEK_SET);
        
        GTP_DEBUG("[update_cfg]Read config from file.");
        ret = update_msg.cfg_file->f_op->read(update_msg.cfg_file, (char*)pre_buf, file_len, &update_msg.cfg_file->f_pos);
        if(ret<0){
                GTP_ERROR("[update_cfg]Read config file failed.");
                goto update_cfg_file_failed;
        }
        
        GTP_DEBUG("[update_cfg]Delete illgal charactor.");
        for(i=0,count=0; i<file_len; i++) {
                if (pre_buf[i] == ' ' || pre_buf[i] == '\r' || pre_buf[i] == '\n') {
                        continue;
                }
                buf[count++] = pre_buf[i];
        }
        
        GTP_DEBUG("[update_cfg]Ascii to hex.");
        file_config[0] = GTP_REG_CONFIG_DATA >> 8;
        file_config[1] = GTP_REG_CONFIG_DATA & 0xff;
        for(i=0,file_cfg_len=GTP_ADDR_LENGTH; i<count; i+=5) {
                if((buf[i]=='0') && ((buf[i+1]=='x') || (buf[i+1]=='X'))) {
                        u8 high,low;
                        high = ascii2hex(buf[i+2]);
                        low = ascii2hex(buf[i+3]);
            
                        if((high == 0xFF) || (low == 0xFF)) {
                                ret = 0;
                                GTP_ERROR("[update_cfg]Illegal config file.");
                                goto update_cfg_file_failed;
                        }
                        file_config[file_cfg_len++] = (high<<4) + low;
                }else {
                        ret = 0;
                        GTP_ERROR("[update_cfg]Illegal config file.");
                        goto update_cfg_file_failed;
                }
        }
        GTP_DEBUG("config:");
        GTP_DEBUG_ARRAY(file_config, file_cfg_len);
        
        //cal checksum
        for(i=GTP_ADDR_LENGTH; i<chip_cfg_len; i++) {
                checksum += file_config[i];
        }
        file_config[chip_cfg_len] = (~checksum) + 1;
        
        i = 0;
        while(i++ < 5) {
                ret = gup_i2c_write(client, file_config, file_cfg_len);
                if(ret > 0) {
                        GTP_INFO("[update_cfg]Send config SUCCESS.");
                        break;
                }
                GTP_ERROR("[update_cfg]Send config i2c error.");
        }
    
update_cfg_file_failed:
        kfree(pre_buf);
        kfree(buf);
        kfree(file_config);
        return ret;
}
#endif
 
#if GTP_HEADER_FW_UPDATE
static u8 gup_check_fs_mounted(char *path_name)
{
        struct path root_path;
        struct path path;
        int err;
        err = kern_path("/", LOOKUP_FOLLOW, &root_path);
 
        if (err) {
                GTP_DEBUG("\"/\" NOT Mounted: %d", err);
                return FAIL;
        }
        err = kern_path(path_name, LOOKUP_FOLLOW, &path);
 
        if (err){
                GTP_DEBUG("/data/ NOT Mounted: %d", err);
                return FAIL;
        }
 
        return SUCCESS;
 
}
#endif
static u8 gup_check_update_file(struct i2c_client *client, st_fw_head* fw_head, u8* path)
{
        s32 ret = 0;
        s32 i = 0;
        s32 fw_checksum = 0;
        u8 buf[FW_HEAD_LENGTH];
    
        if (path) {
                update_msg.file = filp_open(path, O_RDONLY, 0644);
 
                if (IS_ERR(update_msg.file)) {
                        GTP_ERROR("Open update file(%s) error!", path);
                        return FAIL;
                }
        }else {
#if GTP_HEADER_FW_UPDATE
                for (i = 0; i < (GUP_SEARCH_FILE_TIMES); i++) {
                        GTP_DEBUG("Waiting for /data/ mounted [%d]", i);
 
                        if (gup_check_fs_mounted("/data") == SUCCESS) {
                                GTP_DEBUG("/data/ Mounted!");
                                break;
                        }
                        msleep(3000);
                }
                
                if (i >= (GUP_SEARCH_FILE_TIMES)) {
                        GTP_ERROR("Wait for /data/ mounting timeout!");
                        return FAIL;
                }
                update_msg.file = filp_open(UPDATE_FILE_PATH_2, O_CREAT | O_RDWR, 0666);
                
                if ((IS_ERR(update_msg.file))) {
                        GTP_ERROR("Failed to Create fw file for fw_header!");
                        return FAIL;
                }
                update_msg.file->f_op->llseek(update_msg.file, 0, SEEK_SET);
                update_msg.file->f_op->write(update_msg.file, (char *)header_fw_array, sizeof(header_fw_array), &update_msg.file->f_pos);
                filp_close(update_msg.file, NULL);
                update_msg.file = filp_open(UPDATE_FILE_PATH_2, O_RDWR , 0444);
#else
                u8 fp_len = max(sizeof(UPDATE_FILE_PATH_1), sizeof(UPDATE_FILE_PATH_2));
                u8 cfp_len = max(sizeof(CONFIG_FILE_PATH_1), sizeof(CONFIG_FILE_PATH_2));
                u8 *search_update_path = (u8*)kzalloc(fp_len, GFP_KERNEL);
                u8 *search_cfg_path = (u8*)kzalloc(cfp_len, GFP_KERNEL);
                //Begin to search update file,the config file & firmware file must be in the same path,single or double.
                for (i = 0; i < GUP_SEARCH_FILE_TIMES; i++) {
                        if(i%2) {
                                memcpy(search_update_path, UPDATE_FILE_PATH_1, sizeof(UPDATE_FILE_PATH_1));
                                memcpy(search_cfg_path, CONFIG_FILE_PATH_1, sizeof(CONFIG_FILE_PATH_1));
                        }else {
                                memcpy(search_update_path, UPDATE_FILE_PATH_2, sizeof(UPDATE_FILE_PATH_2));
                                memcpy(search_cfg_path, CONFIG_FILE_PATH_2, sizeof(CONFIG_FILE_PATH_2));
                        }
                    
                        if(!(got_file_flag&0x0F)) {
                                update_msg.file = filp_open(search_update_path, O_RDWR, 0444);
                                if(!IS_ERR(update_msg.file)) {
                                        GTP_DEBUG("Find the bin file");
                                         got_file_flag |= 0x0F;
                                }
                        }
                        if(!(got_file_flag&0xF0)) {
                                update_msg.cfg_file = filp_open(search_cfg_path, O_RDWR, 0444);
                                if(!IS_ERR(update_msg.cfg_file)) {
                                        GTP_DEBUG("Find the cfg file");
                                        got_file_flag |= 0xF0;
                                }
                        }
                    
                        if(got_file_flag) {
                                if(got_file_flag == 0xFF) {
                                        break;
                                }else {
                                        i += 4;
                                }
                        }
                        GTP_DEBUG("%3d:Searching %s %s file...", i, (got_file_flag&0x0F)?"":"bin", (got_file_flag&0xF0)?"":"cfg");
                        msleep(3000);
                }
                kfree(search_update_path);
                kfree(search_cfg_path);
                
                if(!got_file_flag) {
                        GTP_ERROR("Can't find update file.");
                        goto load_failed;
                }
                
                if(got_file_flag&0xF0) {
                        GTP_DEBUG("Got the update config file.");
                         ret = gup_update_config(client);
                        if(ret <= 0) {
                                GTP_ERROR("Update config failed.");
                        }
                        filp_close(update_msg.cfg_file, NULL);
                        msleep(500);                //waiting config to be stored in FLASH.
                }
                if(got_file_flag&0x0F) {
                        GTP_DEBUG("Got the update firmware file.");
                }else {
                        GTP_ERROR("No need to upgrade firmware.");
                        goto load_failed;
                }
#endif
        }   
    
        update_msg.old_fs = get_fs();
        set_fs(KERNEL_DS);
 
        update_msg.file->f_op->llseek(update_msg.file, 0, SEEK_SET);
        //update_msg.file->f_pos = 0;
 
        ret = update_msg.file->f_op->read(update_msg.file, (char*)buf, FW_HEAD_LENGTH, &update_msg.file->f_pos);
        if (ret < 0){
                GTP_ERROR("Read firmware head in update file error.");
                goto load_failed;
        }
        memcpy(fw_head, buf, FW_HEAD_LENGTH);
    
        //check firmware legality
        fw_checksum = 0;
        for(i=0; i<FW_SECTION_LENGTH*4+FW_DSP_ISP_LENGTH+FW_DSP_LENGTH+FW_BOOT_LENGTH; i+=2) {
                u16 temp;
                ret = update_msg.file->f_op->read(update_msg.file, (char*)buf, 2, &update_msg.file->f_pos);
                if (ret < 0) {
                        GTP_ERROR("Read firmware file error.");
                        goto load_failed;
                }
                //GTP_DEBUG("BUF[0]:%x", buf[0]);
                temp = (buf[0]<<8) + buf[1];
                fw_checksum += temp;
        }
    
        GTP_DEBUG("firmware checksum:%x", fw_checksum&0xFFFF);
        if(fw_checksum&0xFFFF) {
                GTP_ERROR("Illegal firmware file.");
                goto load_failed;
        }
    
        return SUCCESS;
 
load_failed:
        set_fs(update_msg.old_fs);
        return FAIL;
}
 
#if 0
static u8 gup_check_update_header(struct i2c_client *client, st_fw_head* fw_head)
{
        const u8* pos;
        int i = 0;
        u8 mask_num = 0;
        s32 ret = 0;
 
        pos = HEADER_UPDATE_DATA;
      
        memcpy(fw_head, pos, FW_HEAD_LENGTH);
        pos += FW_HEAD_LENGTH;
 
        ret = gup_enter_update_judge(fw_head);
        if(SUCCESS == ret) {
                return SUCCESS;
        }
        return FAIL;
}
#endif
 
static u8 gup_burn_proc(struct i2c_client *client, u8 *burn_buf, u16 start_addr, u16 total_length)
{
        s32 ret = 0;
        u16 burn_addr = start_addr;
        u16 frame_length = 0;
        u16 burn_length = 0;
        u8  wr_buf[PACK_SIZE + GTP_ADDR_LENGTH];
        u8  rd_buf[PACK_SIZE + GTP_ADDR_LENGTH];
        u8  retry = 0;
    
        GTP_DEBUG("Begin burn %dk data to addr 0x%x", (total_length/1024), start_addr);
        while(burn_length < total_length) {
                GTP_DEBUG("B/T:%04d/%04d", burn_length, total_length);
                frame_length = ((total_length - burn_length) > PACK_SIZE) ? PACK_SIZE : (total_length - burn_length);
                wr_buf[0] = (u8)(burn_addr>>8);
                rd_buf[0] = wr_buf[0];
                wr_buf[1] = (u8)burn_addr;
                rd_buf[1] = wr_buf[1];
                memcpy(&wr_buf[GTP_ADDR_LENGTH], &burn_buf[burn_length], frame_length);
        
                for(retry = 0; retry < MAX_FRAME_CHECK_TIME; retry++) {
                        ret = gup_i2c_write(client, wr_buf, GTP_ADDR_LENGTH + frame_length);
                        if(ret <= 0) {
                                GTP_ERROR("Write frame data i2c error.");
                                continue;
                        }
                        ret = gup_i2c_read(client, rd_buf, GTP_ADDR_LENGTH + frame_length);
                        if(ret <= 0) {
                                GTP_ERROR("Read back frame data i2c error.");
                                continue;
                        }
            
                        if(memcmp(&wr_buf[GTP_ADDR_LENGTH], &rd_buf[GTP_ADDR_LENGTH], frame_length)) {
                                GTP_ERROR("Check frame data fail,not equal.");
                                GTP_DEBUG("write array:");
                                GTP_DEBUG_ARRAY(&wr_buf[GTP_ADDR_LENGTH], frame_length);
                                GTP_DEBUG("read array:");
                                GTP_DEBUG_ARRAY(&rd_buf[GTP_ADDR_LENGTH], frame_length);
                                continue;
                        }else {
                                //GTP_DEBUG("Check frame data success.");
                                break;
                        }
                }   
                    
                if(retry >= MAX_FRAME_CHECK_TIME) {
                        GTP_ERROR("Burn frame data time out,exit.");
                        return FAIL;
                }
                burn_length += frame_length;
                burn_addr += frame_length;
        }
        
        return SUCCESS;
}
 
static u8 gup_load_section_file(u8* buf, u16 offset, u16 length)
{
        s32 ret = 0;
        
        if(update_msg.file == NULL) {
                GTP_ERROR("cannot find update file,load section file fail.");
                return FAIL;
        }
        update_msg.file->f_pos = FW_HEAD_LENGTH + offset;
        
        ret = update_msg.file->f_op->read(update_msg.file, (char*)buf, length, &update_msg.file->f_pos);
        if(ret < 0) {
                GTP_ERROR("Read update file fail.");
                return FAIL;
        }
        
        return SUCCESS;
}
 
static u8 gup_recall_check(struct i2c_client *client, u8* chk_src, u16 start_rd_addr, u16 chk_length)
{
        u8  rd_buf[PACK_SIZE + GTP_ADDR_LENGTH];
        s32 ret = 0;
        u16 recall_addr = start_rd_addr;
        u16 recall_length = 0;
        u16 frame_length = 0;
        
        while(recall_length < chk_length) {
                frame_length = ((chk_length - recall_length) > PACK_SIZE) ? PACK_SIZE : (chk_length - recall_length);
                ret = gup_get_ic_msg(client, recall_addr, rd_buf, frame_length);
                if(ret <= 0) {
                        GTP_ERROR("recall i2c error,exit");
                        return FAIL;
                }
        
                if(memcmp(&rd_buf[GTP_ADDR_LENGTH], &chk_src[recall_length], frame_length)) {
                        GTP_ERROR("Recall frame data fail,not equal.");
                        GTP_DEBUG("chk_src array:");
                        GTP_DEBUG_ARRAY(&chk_src[recall_length], frame_length);
                        GTP_DEBUG("recall array:");
                        GTP_DEBUG_ARRAY(&rd_buf[GTP_ADDR_LENGTH], frame_length);
                        return FAIL;
                }
        
                recall_length += frame_length;
                recall_addr += frame_length;
        }
        GTP_DEBUG("Recall check %dk firmware success.", (chk_length/1024));
        
        return SUCCESS;
}
 
static u8 gup_burn_fw_section(struct i2c_client *client, u8 *fw_section, u16 start_addr, u8 bank_cmd )
{
        s32 ret = 0;
        u8  rd_buf[5];
        
        //step1:hold ss51 & dsp
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]hold ss51 & dsp fail.");
                return FAIL;
        }
        
        //step2:set scramble
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]set scramble fail.");
                return FAIL;
        }
        
        //step3:select bank
        ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, (bank_cmd >> 4)&0x0F);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]select bank %d fail.", (bank_cmd >> 4)&0x0F);
                return FAIL;
        }
        
        //step4:enable accessing code
        ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]enable accessing code fail.");
                return FAIL;
        }
        
        //step5:burn 8k fw section
        ret = gup_burn_proc(client, fw_section, start_addr, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_section]burn fw_section fail.");
                return FAIL;
        }
        
        //step6:hold ss51 & release dsp
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]hold ss51 & release dsp fail.");
                return FAIL;
        }
        //must delay
        msleep(1);
        
        //step7:send burn cmd to move data to flash from sram
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, bank_cmd&0x0f);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]send burn cmd fail.");
                return FAIL;
        }
        GTP_DEBUG("[burn_fw_section]Wait for the burn is complete......");
        do{
                ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
                if(ret <= 0) {
                        GTP_ERROR("[burn_fw_section]Get burn state fail");
                        return FAIL;
                }
                msleep(10);
                //GTP_DEBUG("[burn_fw_section]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]);
        }while(rd_buf[GTP_ADDR_LENGTH]);
        
        //step8:select bank
        ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, (bank_cmd >> 4)&0x0F);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]select bank %d fail.", (bank_cmd >> 4)&0x0F);
                return FAIL;
        }
        
        //step9:enable accessing code
        ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]enable accessing code fail.");
                return FAIL;
        }
        
        //step10:recall 8k fw section
        ret = gup_recall_check(client, fw_section, start_addr, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_section]recall check 8k firmware fail.");
                return FAIL;
        }
        
        //step11:disable accessing code
        ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_section]disable accessing code fail.");
                return FAIL;
        }
        
        return SUCCESS;
}
 
static u8 gup_burn_dsp_isp(struct i2c_client *client)
{
        s32 ret = 0;
        u8* fw_dsp_isp = NULL;
        u8  retry = 0;
        
        GTP_DEBUG("[burn_dsp_isp]Begin burn dsp isp---->>");
        
        //step1:alloc memory
        GTP_DEBUG("[burn_dsp_isp]step1:alloc memory");
        while(retry++ < 5) {
                fw_dsp_isp = (u8*)kzalloc(FW_DSP_ISP_LENGTH, GFP_KERNEL);
                if(fw_dsp_isp == NULL) {
                        continue;
                }else {
                        GTP_INFO("[burn_dsp_isp]Alloc %dk byte memory success.", (FW_DSP_ISP_LENGTH/1024));
                        break;
                }
        }
        
        if(retry >= 5) {
                GTP_ERROR("[burn_dsp_isp]Alloc memory fail,exit.");
                return FAIL;
        }
    
        //step2:load dsp isp file data
        GTP_DEBUG("[burn_dsp_isp]step2:load dsp isp file data");
        ret = gup_load_section_file(fw_dsp_isp, (4*FW_SECTION_LENGTH+FW_DSP_LENGTH+FW_BOOT_LENGTH), FW_DSP_ISP_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_dsp_isp]load firmware dsp_isp fail.");
                goto exit_burn_dsp_isp;
        }
    
        //step3:disable wdt,clear cache enable
        GTP_DEBUG("[burn_dsp_isp]step3:disable wdt,clear cache enable");
        ret = gup_set_ic_msg(client, _bRW_MISCTL__TMR0_EN, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]disable wdt fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
        
        ret = gup_set_ic_msg(client, _bRW_MISCTL__CACHE_EN, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]clear cache enable fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step4:hold ss51 & dsp
        GTP_DEBUG("[burn_dsp_isp]step4:hold ss51 & dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]hold ss51 & dsp fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step5:set boot from sram
        GTP_DEBUG("[burn_dsp_isp]step5:set boot from sram");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOTCTL_B0_, 0x02);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]set boot from sram fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step6:software reboot
        GTP_DEBUG("[burn_dsp_isp]step6:software reboot");
        ret = gup_set_ic_msg(client, _bWO_MISCTL__CPU_SWRST_PULSE, 0x01);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]software reboot fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step7:select bank2
        GTP_DEBUG("[burn_dsp_isp]step7:select bank2");
        ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x02);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]select bank2 fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step8:enable accessing code
        GTP_DEBUG("[burn_dsp_isp]step8:enable accessing code");
        ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]enable accessing code fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
    
        //step9:burn 4k dsp_isp
        GTP_DEBUG("[burn_dsp_isp]step9:burn 4k dsp_isp");
        ret = gup_burn_proc(client, fw_dsp_isp, 0xC000, FW_DSP_ISP_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_dsp_isp]burn dsp_isp fail.");
                goto exit_burn_dsp_isp;
        }
    
        //step10:set scramble
        GTP_DEBUG("[burn_dsp_isp]step10:set scramble");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_dsp_isp]set scramble fail.");
                ret = FAIL;
                goto exit_burn_dsp_isp;
        }
        ret = SUCCESS;
 
exit_burn_dsp_isp:
        kfree(fw_dsp_isp);
        return ret;
}
 
static u8 gup_burn_fw_ss51(struct i2c_client *client)
{
        u8* fw_ss51 = NULL;
        u8  retry = 0;
        s32 ret = 0;
        
        GTP_DEBUG("[burn_fw_ss51]Begin burn ss51 firmware---->>");
        
        //step1:alloc memory
        GTP_DEBUG("[burn_fw_ss51]step1:alloc memory");
        while(retry++ < 5) {
                fw_ss51 = (u8*)kzalloc(FW_SECTION_LENGTH, GFP_KERNEL);
                if(fw_ss51 == NULL) {
                    continue;
                }else {
                    GTP_INFO("[burn_fw_ss51]Alloc %dk byte memory success.", (FW_SECTION_LENGTH/1024));
                    break;
                }
        }
        
        if(retry >= 5) {
                GTP_ERROR("[burn_fw_ss51]Alloc memory fail,exit.");
                return FAIL;
        }
    
        //step2:load ss51 firmware section 1 file data
        GTP_DEBUG("[burn_fw_ss51]step2:load ss51 firmware section 1 file data");
        ret = gup_load_section_file(fw_ss51, 0, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 1 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step3:clear control flag
        GTP_DEBUG("[burn_fw_ss51]step3:clear control flag");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_ss51]clear control flag fail.");
                ret = FAIL;
                goto exit_burn_fw_ss51;
        }
    
        //step4:burn ss51 firmware section 1
        GTP_DEBUG("[burn_fw_ss51]step4:burn ss51 firmware section 1");
        ret = gup_burn_fw_section(client, fw_ss51, 0xC000, 0x01);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 1 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step5:load ss51 firmware section 2 file data
        GTP_DEBUG("[burn_fw_ss51]step5:load ss51 firmware section 2 file data");
         ret = gup_load_section_file(fw_ss51, FW_SECTION_LENGTH, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 2 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step6:burn ss51 firmware section 2
        GTP_DEBUG("[burn_fw_ss51]step6:burn ss51 firmware section 2");
        ret = gup_burn_fw_section(client, fw_ss51, 0xE000, 0x02);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 2 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step7:load ss51 firmware section 3 file data
        GTP_DEBUG("[burn_fw_ss51]step7:load ss51 firmware section 3 file data");
        ret = gup_load_section_file(fw_ss51, 2*FW_SECTION_LENGTH, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 3 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step8:burn ss51 firmware section 3
        GTP_DEBUG("[burn_fw_ss51]step8:burn ss51 firmware section 3");
        ret = gup_burn_fw_section(client, fw_ss51, 0xC000, 0x13);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 3 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step9:load ss51 firmware section 4 file data
        GTP_DEBUG("[burn_fw_ss51]step9:load ss51 firmware section 4 file data");
        ret = gup_load_section_file(fw_ss51, 3*FW_SECTION_LENGTH, FW_SECTION_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 4 fail.");
                goto exit_burn_fw_ss51;
        }
    
        //step10:burn ss51 firmware section 4
        GTP_DEBUG("[burn_fw_ss51]step10:burn ss51 firmware section 4");
        ret = gup_burn_fw_section(client, fw_ss51, 0xE000, 0x14);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 4 fail.");
                goto exit_burn_fw_ss51;
        }
    
        ret = SUCCESS;
    
exit_burn_fw_ss51:
        kfree(fw_ss51);
        return ret;
}
 
static u8 gup_burn_fw_dsp(struct i2c_client *client)
{
        s32 ret = 0;
        u8* fw_dsp = NULL;
        u8  retry = 0;
        u8  rd_buf[5];
        
        GTP_DEBUG("[burn_fw_dsp]Begin burn dsp firmware---->>");
        //step1:alloc memory
        GTP_DEBUG("[burn_fw_dsp]step1:alloc memory");
        while(retry++ < 5) {
                fw_dsp = (u8*)kzalloc(FW_DSP_LENGTH, GFP_KERNEL);
                if(fw_dsp == NULL) {
                        continue;
                }else {
                        GTP_INFO("[burn_fw_dsp]Alloc %dk byte memory success.", (FW_SECTION_LENGTH/1024));
                        break;
                }
        }
        
        if(retry >= 5) {
                GTP_ERROR("[burn_fw_dsp]Alloc memory fail,exit.");
                return FAIL;
        }
        
        //step2:load firmware dsp
        GTP_DEBUG("[burn_fw_dsp]step2:load firmware dsp");
        ret = gup_load_section_file(fw_dsp, 4*FW_SECTION_LENGTH, FW_DSP_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_dsp]load firmware dsp fail.");
                goto exit_burn_fw_dsp;
        }
        
        //step3:select bank3
        GTP_DEBUG("[burn_fw_dsp]step3:select bank3");
        ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x03);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_dsp]select bank3 fail.");
                ret = FAIL;
                goto exit_burn_fw_dsp;
        }
        
        //step4:hold ss51 & dsp
        GTP_DEBUG("[burn_fw_dsp]step4:hold ss51 & dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_dsp]hold ss51 & dsp fail.");
                ret = FAIL;
                goto exit_burn_fw_dsp;
        }
        
        //step5:set scramble
        GTP_DEBUG("[burn_fw_dsp]step5:set scramble");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_dsp]set scramble fail.");
                ret = FAIL;
                goto exit_burn_fw_dsp;
        }
        
        //step6:release ss51 & dsp
        GTP_DEBUG("[burn_fw_dsp]step6:release ss51 & dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04);                 //20121211
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_dsp]release ss51 & dsp fail.");
                ret = FAIL;
                goto exit_burn_fw_dsp;
        }
        //must delay
        msleep(1);
        
        //step7:burn 4k dsp firmware
        GTP_DEBUG("[burn_fw_dsp]step7:burn 4k dsp firmware");
        ret = gup_burn_proc(client, fw_dsp, 0x9000, FW_DSP_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_dsp]burn fw_section fail.");
                goto exit_burn_fw_dsp;
        }
        
        //step8:send burn cmd to move data to flash from sram
        GTP_DEBUG("[burn_fw_dsp]step8:send burn cmd to move data to flash from sram");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x05);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_dsp]send burn cmd fail.");
                goto exit_burn_fw_dsp;
        }
        GTP_DEBUG("[burn_fw_dsp]Wait for the burn is complete......");
        do{
                ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
                if(ret <= 0) {
                        GTP_ERROR("[burn_fw_dsp]Get burn state fail");
                        goto exit_burn_fw_dsp;
                }
                msleep(10);
        //GTP_DEBUG("[burn_fw_dsp]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]);
        }while(rd_buf[GTP_ADDR_LENGTH]);
        
        //step9:recall check 4k dsp firmware
        GTP_DEBUG("[burn_fw_dsp]step9:recall check 4k dsp firmware");
        ret = gup_recall_check(client, fw_dsp, 0x9000, FW_DSP_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_dsp]recall check 4k dsp firmware fail.");
                goto exit_burn_fw_dsp;
        }
        
        ret = SUCCESS;
    
exit_burn_fw_dsp:
        kfree(fw_dsp);
        return ret;
}
 
static u8 gup_burn_fw_boot(struct i2c_client *client)
{
        s32 ret = 0;
        u8* fw_boot = NULL;
        u8  retry = 0;
        u8  rd_buf[5];
        
        GTP_DEBUG("[burn_fw_boot]Begin burn bootloader firmware---->>");
        
        //step1:Alloc memory
        GTP_DEBUG("[burn_fw_boot]step1:Alloc memory");
        while(retry++ < 5) {
                fw_boot = (u8*)kzalloc(FW_BOOT_LENGTH, GFP_KERNEL);
                if(fw_boot == NULL) {
                        continue;
                }else {
                        GTP_INFO("[burn_fw_boot]Alloc %dk byte memory success.", (FW_BOOT_LENGTH/1024));
                        break;
                }
        }
        if(retry >= 5) {
                GTP_ERROR("[burn_fw_boot]Alloc memory fail,exit.");
                return FAIL;
        }
    
        //step2:load firmware bootloader
        GTP_DEBUG("[burn_fw_boot]step2:load firmware bootloader");
        ret = gup_load_section_file(fw_boot, (4*FW_SECTION_LENGTH+FW_DSP_LENGTH), FW_BOOT_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_boot]load firmware dsp fail.");
                goto exit_burn_fw_boot;
        }
    
        //step3:hold ss51 & dsp
        GTP_DEBUG("[burn_fw_boot]step3:hold ss51 & dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]hold ss51 & dsp fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
    
        //step4:set scramble
        GTP_DEBUG("[burn_fw_boot]step4:set scramble");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]set scramble fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
    
        //step5:release ss51 & dsp
        GTP_DEBUG("[burn_fw_boot]step5:release ss51 & dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04);                 //20121211
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]release ss51 & dsp fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
        //must delay
        msleep(1);
    
        //step6:select bank3
        GTP_DEBUG("[burn_fw_boot]step6:select bank3");
        ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x03);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]select bank3 fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
    
        //step7:burn 2k bootloader firmware
        GTP_DEBUG("[burn_fw_boot]step7:burn 2k bootloader firmware");
        ret = gup_burn_proc(client, fw_boot, 0x9000, FW_BOOT_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_boot]burn fw_section fail.");
                goto exit_burn_fw_boot;
        }
    
        //step7:send burn cmd to move data to flash from sram
        GTP_DEBUG("[burn_fw_boot]step7:send burn cmd to move data to flash from sram");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x06);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]send burn cmd fail.");
                goto exit_burn_fw_boot;
        }
        GTP_DEBUG("[burn_fw_boot]Wait for the burn is complete......");
        do{
                ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
                if(ret <= 0) {
                        GTP_ERROR("[burn_fw_boot]Get burn state fail");
                        goto exit_burn_fw_boot;
                }
                msleep(10);
                //GTP_DEBUG("[burn_fw_boot]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]);
        }while(rd_buf[GTP_ADDR_LENGTH]);
    
        //step8:recall check 2k bootloader firmware
        GTP_DEBUG("[burn_fw_boot]step8:recall check 2k bootloader firmware");
        ret = gup_recall_check(client, fw_boot, 0x9000, FW_BOOT_LENGTH);
        if(FAIL == ret) {
                GTP_ERROR("[burn_fw_boot]recall check 4k dsp firmware fail.");
                goto exit_burn_fw_boot;
        }
    
        //step9:enable download DSP code 
        GTP_DEBUG("[burn_fw_boot]step9:enable download DSP code ");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x99);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]enable download DSP code fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
    
        //step10:release ss51 & hold dsp
        GTP_DEBUG("[burn_fw_boot]step10:release ss51 & hold dsp");
        ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x08);
        if(ret <= 0) {
                GTP_ERROR("[burn_fw_boot]release ss51 & hold dsp fail.");
                ret = FAIL;
                goto exit_burn_fw_boot;
        }
    
        ret = SUCCESS;
    
exit_burn_fw_boot:
        kfree(fw_boot);
        return ret;
}
 
s32 gup_update_proc(void *dir)
{
        s32 ret = 0;
        u8  retry = 0;
        st_fw_head fw_head;
        struct goodix_ts_data *ts = NULL;
        
        GTP_DEBUG("[update_proc]Begin update ......");
        
        show_len = 1;
        total_len = 100;
        if(dir == NULL)
        {
        msleep(3000);                               //wait main thread to be completed
        }
        
        ts = i2c_get_clientdata(i2c_connect_client);
        
        update_msg.file = NULL;
        ret = gup_check_update_file(i2c_connect_client, &fw_head, (u8*)dir);     //20121211
        if(FAIL == ret) {
                GTP_ERROR("[update_proc]check update file fail.");
                goto update_fail;
        }
    
        ts->enter_update = 1;        // Shield interrupt handler
        gtp_irq_disable(ts);
        gtp_io_init(20);
        gtp_set_io_int();
        ret = gup_get_ic_fw_msg(i2c_connect_client);
        if(FAIL == ret) {
                GTP_ERROR("[update_proc]get ic message fail.");
                goto update_fail;
        }    
    
        ret = gup_enter_update_judge(&fw_head);
        if(FAIL == ret) {
                GTP_ERROR("[update_proc]Check *.bin file fail.");
                goto update_fail;
        }
    
        ret = gup_enter_update_mode(i2c_connect_client);
        if(FAIL == ret) {
                GTP_ERROR("[update_proc]enter update mode fail.");
                goto update_fail;
        }
    
        while(retry++ < 5) {
                show_len = 10;
                total_len = 100;
                ret = gup_burn_dsp_isp(i2c_connect_client);
                if(FAIL == ret) {
                        GTP_ERROR("[update_proc]burn dsp isp fail.");
                        continue;
                }
        
                show_len += 10;
                ret = gup_burn_fw_ss51(i2c_connect_client);
                if(FAIL == ret) {
                        GTP_ERROR("[update_proc]burn ss51 firmware fail.");
                        continue;
                }
        
                show_len += 40;
                ret = gup_burn_fw_dsp(i2c_connect_client);
                if(FAIL == ret) {
                        GTP_ERROR("[update_proc]burn dsp firmware fail.");
                        continue;
                }
        
                show_len += 20;
                ret = gup_burn_fw_boot(i2c_connect_client);
                if(FAIL == ret) {
                        GTP_ERROR("[update_proc]burn bootloader firmware fail.");
                        continue;
                }
                show_len += 10;
                GTP_INFO("[update_proc]UPDATE SUCCESS.");
                break;
        }
        
         if(retry >= 5) {
                GTP_ERROR("[update_proc]retry timeout,UPDATE FAIL.");
                goto update_fail;
        }
    
        GTP_DEBUG("[update_proc]leave update mode.");
        gup_leave_update_mode();
    
    
        msleep(100);
        GTP_DEBUG("[update_proc]send config.");
        //ret = gtp_send_cfg(i2c_connect_client);
       // if(ret < 0) {
       //         GTP_ERROR("[update_proc]send config fail.");
      //  }
        show_len = 100;
        total_len = 100;
        ts->enter_update = 0; 
        gtp_irq_enable(ts);
        filp_close(update_msg.file, NULL);    
        return SUCCESS;
    
update_fail:
        ts->enter_update = 0;
        gtp_irq_enable(ts);
        if(update_msg.file && !IS_ERR(update_msg.file)) {
                filp_close(update_msg.file, NULL);
        }
        show_len = 200;
        total_len = 100;
        return FAIL;
}
 
#if GTP_AUTO_UPDATE
u8 gup_init_update_proc(struct goodix_ts_data *ts)
{
        struct task_struct *thread = NULL;
 
        GTP_INFO("Ready to run update thread.");
        thread = kthread_run(gup_update_proc, (void*)NULL, "guitar_update");
        if (IS_ERR(thread)) {
                GTP_ERROR("Failed to create update thread.\n");
                return -1;
        }
 
        return 0;
}
#endif