revert some breakage
[librecmc/librecmc.git] / package / ifxmips_adsl / src / cmvread.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15  */
16 //-----------------------------------------------------------------------
17 //Description:  
18 // read the CMV register under Linux for Amazon
19 //-----------------------------------------------------------------------
20 //Author:       Joe.Lin@infineon.com
21 //Created:      31-December-2004
22 //-----------------------------------------------------------------------
23 /* History
24  * Last changed on:
25  *  000002:tc.chen 2005/06/10 add get_adsl_rate and ifx_makeCMV api
26  * Last changed by: 
27  *
28 */
29
30 #define _IFXMIPS_ADSL_APP
31 //#define DEBUG
32 #define u32 unsigned int
33 #define u16 unsigned short
34 #define u8  unsigned char
35 #define IFXMIPS_MEI_DEV  "/dev/ifxmips/mei"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <time.h>
43 #include <getopt.h>
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/mman.h>
49 #include <asm/ifxmips/ifxmips_mei_app_ioctl.h>
50 #include <asm/ifxmips/ifxmips_mei_app.h>
51 #include <asm/ifxmips/ifxmips_mei_ioctl.h>
52 #include <asm/ifxmips/ifxmips_mei.h>
53
54 #ifdef IFX_MULTILIB_UTIL 
55 #define main    cmvread_main
56 #define display_version cmvread_display_version
57 #endif
58
59 /*============================definitions======================*/
60 #define OPTN 5
61 #define CNFG 8
62 #define CNTL 1
63 #define STAT 2
64 #define RATE 6
65 #define PLAM 7
66 #define INFO 3
67 #define TEST 4
68
69 typedef unsigned short UINT16;
70 typedef unsigned long UINT32;
71
72
73
74 /*=============================================================*/
75
76
77 /*=============================global variables================*/
78 int c=0;
79 int input_flag=0;
80 int digit_optind=0;
81 FILE* script_file;
82 void (*func)()=NULL;
83 int fd;
84
85 UINT16 var16[8];
86 UINT32 var32[8];
87 UINT16 Message[16];
88
89 /*=============================================================*/
90
91 int ifx_makeCMV(unsigned char opcode, unsigned char group, unsigned short address, unsigned short index, int size, unsigned short * data, unsigned short *Message, int msg_len)
92 {
93         if (msg_len < 16*2)
94                 return -1;
95         memset(Message, 0, 16*2);
96         Message[0]= (opcode<<4) + (size&0xf);
97         if(opcode == H2D_DEBUG_WRITE_DM)
98                 Message[1]= (group&0x7f);
99         else
100                 Message[1]= (((index==0)?0:1)<<7) + (group&0x7f);
101         Message[2]= address;
102         Message[3]= index;
103         if((opcode == H2D_CMV_WRITE)||(opcode == H2D_DEBUG_WRITE_DM))
104                 memcpy(Message+4, data, size*2);
105
106         return 0;
107 }
108
109 void display_version()
110 {
111    printf("adsl cmv reader version1.0\nby Joe Lin \nJoe.Lin@infineon.com\n");
112    return;
113 }
114
115
116 void cmvreader_help()
117 {
118     printf("Usage:cmvread [options] [group name][address][index][size] ...\n");
119     printf("options:\n");
120     printf("    -h --help            Display help information\n");       
121     printf("    -v --version         Display version information\n");
122     printf("group name:                 --group name of CMV to read\n");
123     printf("    OPTN --Read CMV Group 5 \n");
124     printf("    CNFG --Read CMV Group 8 \n");
125     printf("    CNTL --Read CMV Group 1 \n"); 
126     printf("    STAT --Read CMV Group 2 \n");  
127     printf("    RATE --Read CMV Group 6 \n");  
128     printf("    PLAM --Read CMV Group 7 \n");  
129     printf("    INFO --Read CMV Group 3 \n");  
130     printf("    TEST --REad CMV Group 4 \n");  
131     printf("address --address value of CMV to read\n"); 
132     printf("index --index value of CMV to read\n");     
133     printf("size  --number of words(16bits) to read \n");        
134    
135     
136  
137     return;
138 }
139
140 int main (int argc, char** argv) {
141      
142         UINT16 Message[16]; //000002:tc.chen
143         char *endptr; 
144         int group=0,address,index,size;
145
146         if (argc < 2)
147         {
148           cmvreader_help();
149           return;
150         }
151  
152         if (strstr(argv[1], "-h") != NULL){
153           cmvreader_help();
154           return;
155         }
156  
157         if (strstr(argv[1], "-v") != NULL){
158           display_version();
159           return;
160         }
161  
162         fd=open(IFXMIPS_MEI_DEV, O_RDWR);
163         if(fd<0){
164                 printf("\n\n autoboot open device fail\n");
165                 return -1;
166         }
167         
168         if((strcmp(argv[1],"optn")==0)||(strcmp(argv[1],"OPTN")==0))
169         group=OPTN;
170         else if((strcmp(argv[1],"cnfg")==0)||(strcmp(argv[1],"CNFG")==0))
171         group=CNFG;
172         else if((strcmp(argv[1],"cntl")==0)||(strcmp(argv[1],"CNTL")==0))
173         group=CNTL;
174         else if((strcmp(argv[1],"stat")==0)||(strcmp(argv[1],"STAT")==0))
175         group=STAT;
176         else if((strcmp(argv[1],"rate")==0)||(strcmp(argv[1],"RATE")==0))
177         group=RATE;
178         else if((strcmp(argv[1],"plam")==0)||(strcmp(argv[1],"PLAM")==0))
179         group=PLAM;
180         else if((strcmp(argv[1],"info")==0)||(strcmp(argv[1],"INFO")==0))
181         group=INFO;
182         else if((strcmp(argv[1],"test")==0)||(strcmp(argv[1],"TEST")==0))
183         group=TEST;
184         else 
185          {
186            printf("wrong group type!\nplease slect group:OPTN CNFG CNTL STAT RATE PLAM INFO TEST \n");
187                 close(fd);
188                 exit(0);
189          }
190          
191         address = strtoul(argv[2], &endptr, 0);
192         index = strtoul(argv[3], &endptr, 0);
193         size = strtoul(argv[4], &endptr, 0);
194         //makeCMV(H2D_CMV_READ, group, address, index, size, NULL);
195         ifx_makeCMV(H2D_CMV_READ, group, address, index, size, NULL,Message,sizeof(Message));
196         if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
197                 printf("cr read %d %d %d fail",group,address,index);
198                 close(fd);
199                 exit(0);
200         }
201
202         int i;
203         for (i=0;i<size;i++) printf ("0x%X\n",Message[i+4]);
204         
205         
206 //      return Message[4];
207         
208         
209         
210    
211         close(fd);
212         return 0;            
213 }
214
215
216