9563b5bbcbc3aaf063f60265e1d73d5be746d648
[openwrt.git] / package / ifxmips_adsl / src / danube_autoboot_daemon.c
1 /****************************************************************************
2
3                             Copyright (c) 2005
4                          Infineon Technologies AG
5                St. Martin Strasse 53; 81669 Munich, Germany
6
7   Any use of this Software is subject to the conclusion of a respective
8   License Agreement. Without such a License Agreement no rights to the
9   Software are granted.
10
11 *****************************************************************************/
12 /** 
13 History:
14 1.02:
15 09/08/2006 Removed DEMO_ONLY define
16 1.03:
17 31/08/2006 Add IOCTL AUTOBOOT_ENABLE_SET
18 1.04:
19 14/09/2006 Add min snr margin check for in Annex B DMT mode
20 1.05:
21 18/09/2006 Add G.lite support
22 1.06: 611101:tc.chen
23 10/11/2006 change quiet mode and showtime lock implement mechanism (old: using cmv, new: using ioctl to kernel mode, and the mei driver keep the value) per the mail from Oliver Salomon on 08/11/2006
24 1.07: 
25 13/11/2006 TC Chen: Fix minimal snr issue for ADSL Annex B.
26 1.08:
27 08/12/2006 TC Chen: Fix loop diagnostic warning issue
28 1.09:
29 27/12/2006 TC Chen: Fix the issue that "The autoboot daemon will call "./translate" which is a problem when started through scripts."
30 1.10"
31 1/09/2007 Bing Tao: Fix AnnexJ issue
32 */
33
34 #define _IFXMIPS_ADSL_APP
35 //#define IFXMIPS_PORT_RTEMS 1
36 #define __LINUX__
37
38 #if defined(IFXMIPS_PORT_RTEMS)
39 #include "ifxmips_mei_rtems.h"
40 #define KERNEL_SPACE_APPLICATION 1
41 #endif
42
43 #define u32     unsigned int
44  
45 #define VERSION_MAJOR   1
46 #define VERSION_MINOR   10
47  
48 #if defined(__LINUX__)
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <sys/ioctl.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <fcntl.h>
56 #include <errno.h>
57
58 #include <asm/ifxmips/ifxmips_mei_app.h>
59 #include <asm/ifxmips/ifxmips_mei_app_ioctl.h>
60 #include <asm/ifxmips/ifxmips_mei_ioctl.h>
61 #include <sys/types.h>
62 #include <string.h>
63 #include <time.h>
64
65 #define IFXMIPS_MEI_DEV         "/dev/ifxmips/mei"
66 #endif
67
68 #if defined(KERNEL_SPACE_APPLICATION)
69 #include "ifxmips_adsl_fw.h"
70 #endif
71
72 #define IFXMIPS_GPIO_DEVICE     "/dev/ifxmips_port"
73 #undef DEMO_ONLY //todo: remove for normal release
74
75 #define SEGMENT_SIZE (64*1024)
76
77 #define MODE_ALL        0
78 #define MODE_992_1A     1
79 #define MODE_992_1B     2
80 #define MODE_992_3A     3
81 #define MODE_992_3B     4
82 #define MODE_992_5A     5
83 #define MODE_992_5B     6
84
85 #define MODE_992_3I 7
86 #define MODE_992_3J 8
87 #define MODE_992_3M 9
88 #define MODE_992_5I 10
89 #define MODE_992_5J 11
90 #define MODE_992_5M 12
91 #define MODE_M_ALL 13 
92 #define MODE_B_ALL 14 
93 #define MODE_M_B_ALL 15
94
95
96 #if defined(KERNEL_SPACE_APPLICATION)
97 extern int mei_ioctl(int ino, int * fil, unsigned int command, unsigned long lon);
98 #define ioctl(fd,cmd,arg) mei_ioctl(1,0,cmd,(unsigned long)arg)
99 extern void makeCMV(u8 opcode, u8 group, u16 address, u16 index, int size, u16 * data,u16 *CMVMSG);
100 #endif
101
102 static u16 Message[16]__attribute__ ((aligned(4))); 
103
104 #if !defined(KERNEL_SPACE_APPLICATION)
105 void makeCMV(u8 opcode, u8 group, u16 address, u16 index, int size, u16 * data, u16 *CMVMSG);
106 void makeCMV(u8 opcode, u8 group, u16 address, u16 index, int size, u16 * data, u16 *CMVMSG)
107 {
108         memset(CMVMSG, 0, 16*2);
109         CMVMSG[0]= (opcode<<4) + (size&0xf);
110         if(opcode == H2D_DEBUG_WRITE_DM)
111                 CMVMSG[1]= (group&0x7f);
112         else
113                 CMVMSG[1]= (((index==0)?0:1)<<7) + (group&0x7f);
114         CMVMSG[2]= address;
115         CMVMSG[3]= index;
116         if((opcode == H2D_CMV_WRITE)||(opcode == H2D_DEBUG_WRITE_DM))
117                 memcpy(CMVMSG+4, data, size*2);
118         return;
119 }
120 #endif
121
122 void print_usage(char *program_name)
123 {
124         printf("Usage: %s ADSL_Firmware ADSL_MODE \n",program_name);
125         printf("\tADSL_Firmeare: Specify the ADSL firmware file to use.\n");
126         printf("\tADSL_MODE:Specify the ADSL mode to use for training.\n");
127         printf("\t\tSupported Mode: all,1a,3a,5a,1b,3b,5b,3i,3j,3m,5i,5j,5m,m_all,b_all,mb_all\n");
128
129 }
130
131 #if defined(KERNEL_SPACE_APPLICATION)
132
133 int Download_Firmware(char *filename, int fd_mei)
134 {
135         extern ssize_t mei_write(char * filp, const char * buf, size_t size, loff_t * loff);
136         extern unsigned long cgi_pFileData_modemfw_bin[]; 
137         unsigned long offset=0;
138         
139         mei_write(0,(char *)cgi_pFileData_modemfw_bin,sizeof(cgi_pFileData_modemfw_bin),&offset);
140         return 0;
141 }
142 #else
143 int Download_Firmware(char *filename, int fd_mei)
144 {
145         int fd_image=0;
146         char *buf=NULL;
147         int size=0,read_size = SEGMENT_SIZE;
148         struct stat file_stat;
149
150         fd_image=open(filename, O_RDONLY);
151         if (fd_image<=0)
152         {
153                 printf("\n open %s fail.\n",filename);
154                 return -1;
155         }
156         if(lstat(filename, &file_stat)<0){
157                 printf("\n lstat error");
158                 return -1;
159         }
160         size=file_stat.st_size;
161         buf=malloc(read_size);
162         if(buf==NULL){
163                 printf("\n malloc failed in MEI main()");
164                 return -1;
165         }
166
167         lseek(fd_image, 0, SEEK_SET);
168         lseek(fd_mei, 0, SEEK_SET);
169         while(size>0)
170         {
171                 static flag=1;
172                 if (size>SEGMENT_SIZE)
173                         read_size=SEGMENT_SIZE;
174                 else
175                         read_size=size;
176                 if(read(fd_image, buf, read_size)<=0){
177                         printf("\n amazon_mei_image not present");
178                         return -1;
179                 }
180                 if(write(fd_mei, buf, read_size)!=read_size){
181                         printf("\n write to mei driver fail");
182                         free(buf);
183                         return -1;
184                 }
185                 size-=read_size;
186         }
187         free(buf);
188         close(fd_image);
189 }
190 #endif
191
192 // 609141:tc.chen :read min margin from CO for Annex B in DMT mode
193 int read_min_snr_margin(int fd, s16 *rt_ne_ds_min_margin)
194 {
195         makeCMV(H2D_CMV_READ,STAT, 1, 0, 1, NULL, Message);
196         if (ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)!=0)
197                 return -1;
198         if (Message[4] == 0x8) //992.1 AnnexB
199         {
200                 u16 min_margin;
201                 makeCMV(H2D_CMV_READ,INFO, 33, 1, 1, NULL, Message);
202                 if (ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)!=0)
203                 {
204                         return -1;
205                 }
206                 // INFO 33 is 6 bits sign value
207                 min_margin = (Message[4] & 0x3f);
208                 if (min_margin > 31)
209                 {
210                         *rt_ne_ds_min_margin = min_margin - 64;
211                 }else
212                 {
213                         *rt_ne_ds_min_margin = min_margin ;
214                 }
215         }else
216         {
217                 *rt_ne_ds_min_margin = 0;
218         }
219         return 0;
220 }
221 // 609141:tc.chen  end
222
223 #if !defined(KERNEL_SPACE_APPLICATION)
224 int main(int argc, char **argv)
225 #else
226 int ifxmips_autoboot_main (int argc, char **argv)
227 #endif
228 {
229         int fd;
230         int i,j;
231         char systemstr[50];
232         u16 all_data = 0;
233         u16 zero = 0x0;
234         u16 one=0x1;
235         u16 mw = 0x10;
236         u16 link = 0x2;
237         u16 temp=0;
238         u16 rt_macrostate;
239         meireg meiregister;
240         int reboot_flag, rt_ne_los_flag;
241         u16 rt_ne_failure_flags=0;
242         s16 rt_ne_ds_margin=0;
243         s16 rt_ne_ds_min_margin=0; //609141:tc.chen:
244         u16 rt_buildcode_lsw=0, rt_buildcode_msw=0;
245         u32 rt_buildcode = 0;
246         int FarEndResponseReceived;
247         time_t start_time;
248         int timeout_limit;
249         int     tmpShowTimeLockEnabled = 0; 
250         u16 nFECS_L_count=0;
251         u16 nES_L_count=0; 
252         u16 nSES_L_count=0;
253         u16 nLOSS_L_count=0;    
254         u16 first_power_on=1;
255         int mode = -1;
256         int pre_loop_diagnostics_mode=0;
257         int loop_diagnostics_mode=0;
258         
259         if ((argc < 2) || (argc > 5) )
260         {       printf("\n Wrong Argument\n");
261                 print_usage(argv[0]);
262                 return -1;
263         }
264
265 //KD
266 // translate is called with "./translate -ef", but if the autoboot daemon
267 // is started during system boot, the current directory is "/", which
268 // makes the "system" call fail.
269 // Thus we make the program's directory the working directory.
270         {
271          char *argv0, *prog_dir;
272          int argv0_size;
273          /* The dirname system call might change its argument,
274             thus we need a local copy */
275          argv0_size = strlen(argv[0]) + 1;
276          argv0 = malloc(argv0_size);
277          if (argv0 == 0) {
278            printf("\n Insufficient memory\n");
279            return -1;
280          }
281          /* make sure there is no buffer overflow and
282             the string is null-terminated */
283          strncpy(argv0, argv[0], argv0_size);
284          argv0[argv0_size-1] = '\0';
285
286          prog_dir = dirname(argv0);
287          chdir(prog_dir);
288          free(argv0);
289          argv0 = 0;
290         }
291
292         //display version info or download firmware
293         if(argc==2) 
294         {
295                 if ((strncmp(argv[1], "ver", 3)==0) || (strncmp(argv[1], "VER", 3)==0)){
296                         printf("\n ifxmips_autoboot_daemon version: %d.%02d.00\n",VERSION_MAJOR,VERSION_MINOR); 
297                 }else
298                 {
299 #if !defined(KERNEL_SPACE_APPLICATION)          
300                         fd=open(IFXMIPS_MEI_DEV, O_RDWR);
301                         if(fd<0){
302                                 printf("autoboot open %s fail\n",IFXMIPS_MEI_DEV);
303                                 return -1;
304                         }
305 #endif
306                         // Notify mei driver that it is controlled by autoboot daemon
307                         i = 1;
308                         if (ioctl(fd, AUTOBOOT_ENABLE_SET, &i)<0){
309                                 printf("\n\n mei ioctl AUTOBOOT_ENABLE_SET fail.\n");
310                         }
311
312                         Download_Firmware(argv[1],fd);
313                         if (ioctl(fd, IFXMIPS_MEI_START,NULL)<0){
314                                 printf("\n\n mei start fail.\n");
315 #if !defined(KERNEL_SPACE_APPLICATION)                  
316                                 close(fd);
317 #endif                  
318                                 return -1;
319                         }
320 #if !defined(KERNEL_SPACE_APPLICATION)                  
321                         close(fd);
322 #endif                  
323                 }
324                 return 0;
325         }
326
327         if (strncmp(argv[2], "all", 3) == 0)
328                 mode = MODE_ALL;
329         else if ((strncmp(argv[2], "1a", 2) == 0) || (strncmp(argv[2], "1A", 2) == 0))
330                 mode = MODE_992_1A;
331         else if ((strncmp(argv[2], "1b", 2) == 0) || (strncmp(argv[2], "1B", 2) == 0))
332                 mode = MODE_992_1B;
333         else if ((strncmp(argv[2], "3a", 2) == 0) || (strncmp(argv[2], "3A", 2) == 0))
334                 mode = MODE_992_3A;
335         else if ((strncmp(argv[2], "3b", 2) == 0) || (strncmp(argv[2], "3B", 2) == 0))
336                 mode = MODE_992_3B;
337         else if ((strncmp(argv[2], "5a", 2) == 0) || (strncmp(argv[2], "5A", 2) == 0))
338                 mode = MODE_992_5A;
339         else if ((strncmp(argv[2], "5b", 2) == 0) || (strncmp(argv[2], "5B", 2) == 0))
340                 mode = MODE_992_5B;
341
342         else if ((strncmp(argv[2], "3i",2) == 0) || (strncmp(argv[2], "3I",2) == 0))
343                 mode = MODE_992_3I;
344         else if ((strncmp(argv[2], "3j",2) == 0) || (strncmp(argv[2], "3J",2) == 0))
345                 mode = MODE_992_3J;
346         else if ((strncmp(argv[2], "3m",2) == 0) || (strncmp(argv[2], "3M",2) == 0))
347                 mode = MODE_992_3M;
348         else if ((strncmp(argv[2], "5i",2) == 0) || (strncmp(argv[2], "5I",2) == 0))
349                 mode = MODE_992_5I;
350         else if ((strncmp(argv[2], "5j",2) == 0) || (strncmp(argv[2], "5J",2) == 0))
351                 mode = MODE_992_5J;
352         else if ((strncmp(argv[2], "5m",2) == 0) || (strncmp(argv[2], "5M",2) == 0))
353                 mode = MODE_992_5M;
354         else if (strncmp(argv[2], "m_all",5) == 0) 
355                 mode = MODE_M_ALL; 
356         else if (strncmp(argv[2], "b_all",5) == 0) 
357                 mode = MODE_B_ALL; 
358         else if (strncmp(argv[2], "mb_all",6) == 0) 
359                 mode = MODE_M_B_ALL; 
360
361         if (mode == -1)
362         {       printf("\n Wrong Argument\n");
363                 print_usage(argv[0]);
364                 return -1;
365         }
366
367 #if !defined(KERNEL_SPACE_APPLICATION)
368         fd=open(IFXMIPS_MEI_DEV, O_RDWR);
369         if(fd<0){
370                 printf("autoboot open %s fail\n",IFXMIPS_MEI_DEV);
371                 return -1;
372         }
373 #endif
374 fw_download:
375         i = 1;
376         if (ioctl(fd, AUTOBOOT_ENABLE_SET, &i)<0){
377                 printf("\n\n mei ioctl AUTOBOOT_ENABLE_SET fail.\n");
378         }
379         Download_Firmware(argv[1],fd);
380
381         if (ioctl(fd, IFXMIPS_MEI_START,NULL)<0){
382                 printf("\n\n mei start fail.\n");
383                 ioctl(fd, IFXMIPS_MEI_REBOOT,NULL);
384                 goto fw_download;
385         }
386
387         // test Annex A or B
388         makeCMV(H2D_CMV_READ, INFO, 54,1, 1, &temp, Message);
389         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
390                 printf("\n\n read INFO 54 1 fail");
391                 return -1;
392         }
393
394         if(((Message[4]>>8)&0x3f)==0x1){//Annex A
395                 all_data = 0x9105; 
396                 if ((mode == MODE_992_1B) || (mode == MODE_992_3B) || (mode == MODE_992_5B))
397                 {       printf("\n\nWrong Argument, Annex A is running!\n\n");
398                         return -1;
399                 }
400         }
401         else if(((Message[4]>>8)&0x3f)==0x2){// Annex B
402                 all_data=0x4208;
403                 if ( (mode == MODE_992_1A) || (mode == MODE_992_3A) || (mode == MODE_992_5A)\
404 // changed by xu bingtao 09/01/2007
405 /*
406                 || (mode == MODE_992_3I) || (mode == MODE_992_3J) ||(mode == MODE_992_3M)\
407                 || (mode == MODE_992_5I) || (mode == MODE_992_5J) || (mode == MODE_992_5M))
408 */
409                 || (mode == MODE_992_3I) ||(mode == MODE_992_3M)\
410                 || (mode == MODE_992_5I) || (mode == MODE_992_5M))
411 // changed by xu bingtao 09/01/2007
412                 {       printf("\n\nWrong Argument, Annex B is running!\n\n");
413                         return -1;      
414                 }
415         }
416         else{
417                 printf("\n\n Firmware Neither Annex A nor B\n\n");
418                 return -1;
419         }
420
421         /* add by tc chen */
422         system("echo \"0\" > /tmp/adsl_status");
423
424         makeCMV(H2D_CMV_READ, INFO, 55, 0, 1, &temp, Message);
425         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
426                 printf("\n\n read INFO 55 0 fail");
427                 return -1;
428         }
429
430         rt_buildcode_lsw = Message[4];
431         
432         makeCMV(H2D_CMV_READ, INFO, 55, 1, 1, &temp, Message);
433         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
434                 printf("\n\n read INFO 55 1 fail");
435                 return -1;
436         }
437         rt_buildcode_msw = Message[4];
438
439         rt_buildcode = rt_buildcode_lsw + (rt_buildcode_msw << 16);
440         printf("date: %d, month: %d, hour: %d, minute: %d\n",
441                 (rt_buildcode >> 0) & 0xFF,
442                 (rt_buildcode >> 8) & 0xFF,
443                 (rt_buildcode >> 16) & 0xFF,
444                 (rt_buildcode >> 25) & 0xFF
445               );
446
447         while(1){
448                 makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &zero, Message);
449                 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
450                         printf("\n\n write OPTN 0 0 fail");
451                         goto rt_reboot;
452                 }
453                 if (mode == MODE_992_1A)
454                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
455                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
456                                 printf("\n\n read OPTN 0 0 fail");
457                                 goto rt_reboot;
458                         }
459                         temp = Message[4];
460                         temp|=0x4;
461                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
462                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
463                                 printf("\n\n write OPTN 0 0 fail");
464                                 goto rt_reboot;
465                         }
466                 }
467                 else if (mode == MODE_992_3A)
468                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
469                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
470                                 printf("\n\n read OPTN 0 0 fail");
471                                 goto rt_reboot;
472                         }
473                         temp = Message[4];
474                         temp|=0x100;
475                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
476                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
477                                 printf("\n\n write OPTN 0 0 fail");
478                                 goto rt_reboot;
479                         }
480                 }
481                 else if (mode == MODE_992_5A)
482                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
483                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
484                                 printf("\n\n read OPTN 0 0 fail");
485                                 goto rt_reboot;
486                         }
487                         temp = Message[4];
488                         temp|=0x8100;
489                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
490                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
491                                 printf("\n\n write OPTN 0 0 fail");
492                                 goto rt_reboot;
493                         }
494                 }
495                 else if (mode == MODE_992_1B)
496                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
497                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
498                                 printf("\n\n read OPTN 0 0 fail");
499                                 goto rt_reboot;
500                         }
501                         temp = Message[4];
502                         temp|=0x8;
503                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
504                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
505                                 printf("\n\n write OPTN 0 0 fail");
506                                 goto rt_reboot;
507                         }
508                 }
509                 else if (mode == MODE_992_3B)
510                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
511                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
512                                 printf("\n\n read OPTN 0 0 fail");
513                                 goto rt_reboot;
514                         }
515                         temp = Message[4];
516                         temp|=0x200;
517                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
518                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
519                                 printf("\n\n write OPTN 0 0 fail");
520                                 goto rt_reboot;
521                         }
522                 }
523                 else if (mode == MODE_992_5B)
524                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
525                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
526                                 printf("\n\n read OPTN 0 0 fail");
527                                 goto rt_reboot;
528                         }
529                         temp = Message[4];
530                         temp|=0x4200;
531                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
532                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
533                                 printf("\n\n write OPTN 0 0 fail");
534                                 goto rt_reboot;
535                         }
536                 }
537
538                 else if (mode == MODE_992_3I)
539                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
540                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
541                                 printf("\n\n read OPTN 0 0 fail");
542                                 goto rt_reboot;
543                         }
544                         temp = Message[4];
545                         temp|=0x400;
546                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
547                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
548                                 printf("\n\n write OPTN 0 0 fail");
549                                 goto rt_reboot;
550                         }
551                 }
552                 else if (mode == MODE_992_3J)
553                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
554                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
555                                 printf("\n\n read OPTN 0 0 fail");
556                                 goto rt_reboot;
557                         }
558                         temp = Message[4];
559                         temp|=0x800;
560                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
561                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
562                                 printf("\n\n write OPTN 0 0 fail");
563                                 goto rt_reboot;
564                         }
565                 }
566                 else if (mode == MODE_992_3M)
567                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
568                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
569                                 printf("\n\n read OPTN 0 0 fail");
570                                 goto rt_reboot;
571                         }
572                         temp = Message[4];
573                         temp|=0x2000;
574                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
575                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
576                                 printf("\n\n write OPTN 0 0 fail");
577                                 goto rt_reboot;
578                         }
579                 }
580                 else if (mode == MODE_992_5I)
581                 {       makeCMV(H2D_CMV_READ, OPTN, 7, 0, 1, &temp, Message);
582                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
583                                 printf("\n\n read OPTN 7 0 fail");
584                                 goto rt_reboot;
585                         }
586                         temp = Message[4];
587                         temp|=0x1;
588                         makeCMV(H2D_CMV_WRITE, OPTN, 7, 0, 1, &temp, Message);
589                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
590                                 printf("\n\n write OPTN 7 0 fail");
591                                 goto rt_reboot;
592                         }
593                 }
594                 else if (mode == MODE_992_5J)
595                 {       makeCMV(H2D_CMV_READ, OPTN, 7, 0, 1, &temp, Message);
596                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
597                                 printf("\n\n read OPTN 7 0 fail");
598                                 goto rt_reboot;
599                         }
600                         temp = Message[4];
601                         temp|=0x2;
602                         makeCMV(H2D_CMV_WRITE, OPTN, 7, 0, 1, &temp, Message);
603                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
604                                 printf("\n\n write OPTN 7 0 fail");
605                                 goto rt_reboot;
606                         }
607                 }
608                 else if (mode == MODE_992_5M)
609                 {       makeCMV(H2D_CMV_READ, OPTN, 7, 0, 1, &temp, Message);
610                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
611                                 printf("\n\n read OPTN 7 0 fail");
612                                 goto rt_reboot;
613                         }
614                         temp = Message[4];
615                         temp|=0x4;
616                         makeCMV(H2D_CMV_WRITE, OPTN, 7, 0, 1, &temp, Message);
617                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
618                                 printf("\n\n write OPTN 7 0 fail");
619                                 goto rt_reboot;
620                         }
621                 }
622                 else if (mode == MODE_M_ALL){
623                         makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
624                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
625                                 printf("\n\n read OPTN 0 0 fail");
626                                 goto rt_reboot;
627                         }
628                         temp = Message[4];
629                         temp|=0x2000;
630                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
631                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
632                                 printf("\n\n write OPTN 0 0 fail");
633                                 goto rt_reboot;
634                         }
635
636                         makeCMV(H2D_CMV_READ, OPTN, 7, 0, 1, &temp, Message);
637                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
638                                 printf("\n\n read OPTN 7 0 fail");
639                                 goto rt_reboot;
640                         }
641                         temp = Message[4];
642                         temp|=0x4;
643                         makeCMV(H2D_CMV_WRITE, OPTN, 7, 0, 1, &temp, Message);
644                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
645                                 printf("\n\n write OPTN 7 0 fail");
646                                 goto rt_reboot;
647                         }
648                 }
649                 else if (mode == MODE_B_ALL){
650                         makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
651                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
652                                 printf("\n\n read OPTN 0 0 fail");
653                                 goto rt_reboot;
654                         }
655                         temp = Message[4];
656                         temp|=0x4208;
657                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
658                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
659                                 printf("\n\n write OPTN 0 0 fail");
660                                 goto rt_reboot;
661                         }
662                 }else if (mode == MODE_M_B_ALL){
663                         makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
664                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
665                                 printf("\n\n read OPTN 0 0 fail");
666                                 goto rt_reboot;
667                 }
668                         temp = Message[4];
669                         temp|=0x6208;
670                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
671                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
672                                 printf("\n\n write OPTN 0 0 fail");
673                                 goto rt_reboot;
674                         }
675                 }
676                 else if (mode == MODE_ALL)
677                 {       makeCMV(H2D_CMV_READ, OPTN, 0, 0, 1, &temp, Message);
678                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
679                                 printf("\n\n read OPTN 0 0 fail");
680                                 goto rt_reboot; 
681                         }
682                         temp = Message[4];
683                         temp|=all_data;
684                         temp|= 2; //609181:tc.chen support G.lite
685
686                         makeCMV(H2D_CMV_WRITE, OPTN, 0, 0, 1, &temp, Message);
687                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
688                                 printf("\n\n write OPTN 0 0 fail");
689                                 goto rt_reboot; 
690                         }
691                 }
692
693                 if(ioctl(fd, GET_ADSL_LOOP_DIAGNOSTICS_MODE, &j)<0){
694                         printf("\n\n ioctl GET_ADSL_LOOP_DIAGNOSTICS_MODE fail");
695                         goto rt_reboot;
696                 }
697                 if (j == 1) // LOOP DIAGNOSTICS enabled
698                 {
699                         makeCMV(H2D_CMV_READ, OPTN, 9, 0, 1, &temp, Message);
700                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
701                                 printf("\n\n read OPTN 9 0 fail");
702                                 goto rt_reboot; 
703                         }
704                         temp = Message[4];
705                         temp |= 1<<2;
706                         //temp = 1<<2;
707                         makeCMV(H2D_CMV_WRITE, OPTN, 9, 0, 1, &temp, Message);
708                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
709                                 printf("\n\n write OPTN 9 0 fail");
710                                 goto rt_reboot; 
711                         }
712                 }
713
714                 /************ remove in future ***********/             
715
716                 if(argc==4){    //execute script file
717                         strncpy(systemstr, "./translate -ef ",16);
718                         strcpy(systemstr+16, argv[3]);
719                         system(systemstr);
720                 }
721
722 #ifdef DEMO_ONLY
723                 if (mode == MODE_992_1A)
724                         temp=0x303;
725                 else
726                         temp=0x300;
727                 makeCMV(H2D_CMV_WRITE, OPTN, 1, 0, 1, &temp, Message);
728                 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
729                         printf("\n\n write OPTN 1 0 fail");
730                         return -1;
731                 }
732                 temp=0x8f78;
733                 makeCMV(H2D_CMV_WRITE, OPTN, 2, 0, 1, &temp, Message);
734                 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
735                         printf("\n\n write OPTN 2 0 fail");
736                         return -1;
737                 }
738 #endif
739
740                 makeCMV(H2D_CMV_WRITE, CNTL, 0, 0, 1, &link, Message);
741                 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
742                         printf("\n\n set CNTL 0 0 fail");
743                         goto rt_reboot;
744                 }
745                 
746                 FarEndResponseReceived=0;
747                 time(&start_time);              
748                 timeout_limit=60;
749                         
750                 while(1){
751                         int     tmpQuietModeEnabled;
752                         tmpQuietModeEnabled = 0;
753                         if(ioctl(fd, QUIET_MODE_GET, &tmpQuietModeEnabled)<0){
754                                 printf("\n\n ioctl QUIET_MODE_GET fail");
755                                 goto rt_reboot;
756                         }
757 #if 0 // 611101:tc.chen : change to use ioctl QUIET_MODE_GET
758                         makeCMV(H2D_CMV_READ, INFO, 94, 0, 1, &temp, Message);
759                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
760                                 printf("\n\n INFO 94 0 read fail");
761                                 goto rt_reboot;
762                         }
763                         if( Message[4] == 0x0 )
764                                 tmpQuietModeEnabled = 1;
765 #endif
766                         // check if loop diagnostic mode is changed
767                         if(ioctl(fd, GET_ADSL_LOOP_DIAGNOSTICS_MODE, &loop_diagnostics_mode)<0)
768                         {
769                                 printf("\n\n ioctl GET_ADSL_LOOP_DIAGNOSTICS_MODE fail");
770                                 goto rt_reboot;
771                         }
772                         if (loop_diagnostics_mode != pre_loop_diagnostics_mode)
773                         {
774                                 pre_loop_diagnostics_mode = loop_diagnostics_mode;
775                                 goto rt_reboot;
776                         }
777
778                         makeCMV(H2D_CMV_READ, STAT, 0, 0, 1, &rt_macrostate, Message);
779                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
780                                 printf("\n\n STAT read fail");
781                                 goto rt_reboot;
782                         }
783         
784                         if(Message[4]==2)
785                         {
786                                 if( tmpQuietModeEnabled == 0 ) 
787                                         break;
788                         } 
789                 
790                         if((FarEndResponseReceived==0) && ((Message[4]==5) || (Message[4]==6))){
791                                 FarEndResponseReceived=1;
792                                 timeout_limit=60;
793                                 time(&start_time);
794                         }
795                         
796                         if(Message[4]==9){
797                                 timeout_limit=120;
798                                 if(FarEndResponseReceived==0){
799                                         FarEndResponseReceived=1;
800                                         time(&start_time);
801                                 }
802                         }
803
804                         if(Message[4]==3){
805                                 if( tmpQuietModeEnabled == 0 ){ 
806                                         sleep(5);
807                                         break;
808                                 } 
809                         }
810
811                         if(Message[4]==7){
812                                 break;
813                         }
814                         
815                         if(time((time_t *)0)-start_time > timeout_limit){
816                                 if( tmpQuietModeEnabled == 0 ) 
817                                         break;
818                         }
819                         usleep(100000);
820                 }
821                 
822                 if (Message[4] != 7)
823                 {
824                         if (Message[4] == 3) // Done with loop diagnostics
825                         {
826                                 ioctl(fd,LOOP_DIAGNOSTIC_MODE_COMPLETE,NULL);
827                                 while(1)
828                                 {
829                                         if(ioctl(fd, GET_ADSL_LOOP_DIAGNOSTICS_MODE, &loop_diagnostics_mode)<0)
830                                         {
831                                                 printf("\n\n ioctl GET_ADSL_LOOP_DIAGNOSTICS_MODE fail");
832                                                 goto rt_reboot;
833                                         }
834                                         if (loop_diagnostics_mode == 0)
835                                         {
836                                                 pre_loop_diagnostics_mode = loop_diagnostics_mode;
837                                                 goto rt_reboot;
838                                         }
839                                         sleep(1);
840                                 }
841                         }else
842                         {
843                                 printf("\n\n Keep trying to reboot!"); 
844                                 goto rt_reboot; 
845                         }
846                 }
847
848                 j=0;
849                 reboot_flag=0;
850                 while(reboot_flag==0){
851                         if(ioctl(fd, GET_ADSL_LOOP_DIAGNOSTICS_MODE, &loop_diagnostics_mode)<0)
852                         {
853                                 printf("\n\n ioctl GET_ADSL_LOOP_DIAGNOSTICS_MODE fail");
854                                 goto rt_reboot;
855                         }
856                         if (loop_diagnostics_mode != pre_loop_diagnostics_mode)
857                         {
858                                 pre_loop_diagnostics_mode = loop_diagnostics_mode;
859                                 goto rt_reboot;
860                         }
861
862                         makeCMV(H2D_CMV_READ, PLAM, 0, 0, 1, &rt_ne_failure_flags, Message);
863                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
864                                 printf("\n\n PLAM 0 0 read fail");
865                                 goto rt_reboot;
866                         }
867
868                         rt_ne_failure_flags = Message[4];
869                         rt_ne_los_flag=rt_ne_failure_flags & 0x1;
870                         
871 #ifndef DEMO_ONLY
872                         // MIB count start
873                         ioctl(fd, IFXMIPS_MIB_LO_ATUC, (u32*)rt_ne_failure_flags);
874                         makeCMV(H2D_CMV_READ, PLAM, 1, 0, 1, &temp, Message);
875                         
876                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
877                                 printf("\n\n PLAM 1 0 read fail");
878                                 goto rt_reboot;
879                         }
880
881                         ioctl(fd, IFXMIPS_MIB_LO_ATUR, (u32*)Message[4]);
882                         //MIB count end
883 #endif
884                         
885                         rt_ne_ds_margin = 0;
886                         makeCMV(H2D_CMV_READ, PLAM, 46, 0, 1, &rt_ne_ds_margin, Message);
887                         
888                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
889                                 printf("\n\n PLAM 46 0 read fail");
890                                 goto rt_reboot;
891                         }
892
893                         rt_ne_ds_margin = (s16)Message[4];
894
895                         if(ioctl(fd, SHOWTIME_LOCK_GET, &tmpShowTimeLockEnabled)<0){
896                                 printf("\n\n ioctl SHOWTIME_LOCK_GET fail!");
897                                 goto rt_reboot;
898                         }
899 #if 0 //611101:tc.chen change to ioctl SHOWTIME_LOCK
900                         makeCMV(H2D_CMV_READ, TEST, 29, 0, 1, &temp, Message);
901                         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
902                                 printf("\n\n TEST 29 0 read fail");
903                                 goto rt_reboot;
904                         }
905                         if( Message[4] == 0xffff )
906                                 tmpShowTimeLockEnabled = 1;
907 #endif
908                         /* rt_ne_ds_margin is SNR *2  */        
909                         if((rt_ne_los_flag!=0)|| ( rt_ne_ds_margin<(rt_ne_ds_min_margin * 2))){
910                                 if(tmpShowTimeLockEnabled == 1) {
911 // changed by xu bingtao 09/01/2007 
912 /*
913                                         printf("NE_LOS or NE_DS_MARGIN detected but no rebooting because of the showtime lock\n");
914 */
915                                         printf("NE_LOS or NE_DS_MARGIN detected but no rebooting because of the showtime lock\n");
916                                         printf("  Minimum Margin: %d/2 dB  -  Expected Margin: %d dB -  LOS : %d\n",rt_ne_ds_margin, rt_ne_ds_min_margin, rt_ne_los_flag);      
917 // changed by xu bingtao 09/01/2007 
918                                 }
919                                 else
920                                         reboot_flag=1;
921                         }
922
923                         usleep(1500000);
924
925                         if(j<3){//wait for several seconds before setting showtime
926                                 makeCMV(H2D_CMV_READ, STAT, 4, 0, 1, &temp, Message);
927                                 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
928                                         printf("\n\n STAT 4 0 read fail");
929                                         goto rt_reboot;
930                                 }
931                                 if((Message[4]&0x2)==0x2){
932                                         printf("\n\n setting showtime to driver\n\n");
933                                         system("/etc/rc.d/adsl_up &"); //000001:tc.chen
934                                         system("echo \"7\" > /tmp/adsl_status");
935                                         printf("ADSL SHOWTIME!!\n");
936                                         sleep(1);                       
937 //joelin 04/16/2005
938 #ifndef DEMO_ONLY
939                                         if (!first_power_on){
940                 
941                                                 makeCMV(H2D_CMV_WRITE,PLAM, 6, 0, 1, &nFECS_L_count, Message); 
942                                                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
943                 
944                                                 makeCMV(H2D_CMV_WRITE,PLAM, 7, 0, 1, &nES_L_count, Message); 
945                                                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
946          
947                                         
948                                                 makeCMV(H2D_CMV_WRITE,PLAM, 8, 0, 1, &nSES_L_count, Message); 
949                                                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
950         
951                         
952                                                 makeCMV(H2D_CMV_WRITE,PLAM, 9, 0, 1, &nLOSS_L_count, Message); 
953                                                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
954                                         }                                                                       
955 #endif
956                                         ioctl(fd, IFXMIPS_MEI_SHOWTIME,NULL);   //set showtime=1 in driver                      
957                                         // 609141:tc.chen :read min margin from CO for Annex B in DMT mode
958                                         // read adsl link mode
959                                         read_min_snr_margin(fd, &rt_ne_ds_min_margin);
960                                         j=1000;                         
961                                 }
962                                 j++;
963                                 if(j==3){//timeout, set showtimei
964                                         printf("\n\n timeout, setting showtime to driver\n\n");
965                                         //system("echo \"7\" > /tmp/adsl_status");
966                                         printf("ADSL SHOWTIME!!\n");
967                                         sleep(1);                       
968                                         ioctl(fd, IFXMIPS_MEI_SHOWTIME,NULL);   //set showtime=1 in driver
969                                         // 609141:tc.chen :read min margin from CO for Annex B in DMT mode
970                                         // read adsl link mode
971                                         read_min_snr_margin( fd, &rt_ne_ds_min_margin);
972                                 }                                                               
973                         }
974                 }
975 rt_reboot:
976                                 
977 #ifndef DEMO_ONLY
978                 makeCMV(H2D_CMV_READ,PLAM, 6, 0, 1, &temp, Message); 
979                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
980                 nFECS_L_count=nFECS_L_count+Message[4];
981         
982                 makeCMV(H2D_CMV_READ,PLAM, 7, 0, 1, &temp, Message); 
983                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
984                 nES_L_count=nES_L_count+Message[4]; 
985                                 
986                 makeCMV(H2D_CMV_READ,PLAM, 8, 0, 1, &temp, Message); 
987                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
988                 nSES_L_count=nSES_L_count+Message[4];
989                 
990                 makeCMV(H2D_CMV_READ,PLAM, 9, 0, 1, &temp, Message); 
991                 ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message);
992                 nLOSS_L_count=nLOSS_L_count+Message[4]; 
993 #endif
994
995                 /* add by tc chen */
996                 system("echo \"0\" > /tmp/adsl_status");
997                 printf("\n Rebooting ARC\n");
998 reboot_adsl:
999                 ioctl(fd, IFXMIPS_MEI_REBOOT,NULL);
1000                 Download_Firmware(argv[1],fd);
1001                 if (ioctl(fd, IFXMIPS_MEI_START,NULL)<0){
1002                         printf("\n\n mei start fail.\n");
1003                         goto reboot_adsl;
1004                 }
1005         }
1006
1007 #if !defined(KERNEL_SPACE_APPLICATION)                  
1008         close(fd);
1009 #endif
1010         return 0;
1011 }