Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[linux-2.6] / drivers / staging / otus / apdbg.c
1 /*
2  * Copyright (c) 2007-2008 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 /*                                                                      */
17 /*  Module Name : apdbg.c                                               */
18 /*                                                                      */
19 /*  Abstract                                                            */
20 /*      Debug tools                                                     */
21 /*                                                                      */
22 /*  NOTES                                                               */
23 /*      None                                                            */
24 /*                                                                      */
25 /************************************************************************/
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <net/if.h>
37 #include <netinet/in.h>
38
39 #include <linux/sockios.h>
40
41 #define ZM_IOCTL_REG_READ           0x01
42 #define ZM_IOCTL_REG_WRITE          0x02
43 #define ZM_IOCTL_MEM_DUMP           0x03
44 #define ZM_IOCTL_REG_DUMP           0x05
45 #define ZM_IOCTL_TXD_DUMP           0x06
46 #define ZM_IOCTL_RXD_DUMP           0x07
47 #define ZM_IOCTL_MEM_READ           0x0B
48 #define ZM_IOCTL_MEM_WRITE          0x0C
49 #define ZM_IOCTL_DMA_TEST           0x10
50 #define ZM_IOCTL_REG_TEST           0x11
51 #define ZM_IOCTL_TEST               0x80
52 #define ZM_IOCTL_TALLY              0x81 //CWYang(+)
53 #define ZM_IOCTL_RTS                0xA0
54 #define ZM_IOCTL_MIX_MODE           0xA1
55 #define ZM_IOCTL_FRAG               0xA2
56 #define ZM_IOCTL_SCAN               0xA3
57 #define ZM_IOCTL_KEY                0xA4
58 #define ZM_IOCTL_RATE               0xA5
59 #define ZM_IOCTL_ENCRYPTION_MODE    0xA6
60 #define ZM_IOCTL_GET_TXCNT          0xA7
61 #define ZM_IOCTL_GET_DEAGG_CNT      0xA8
62 #define ZM_IOCTL_DURATION_MODE      0xA9
63 #define ZM_IOCTL_SET_AES_KEY        0xAA
64 #define ZM_IOCTL_SET_AES_MODE       0xAB
65 #define ZM_IOCTL_SIGNAL_STRENGTH    0xAC //CWYang(+)
66 #define ZM_IOCTL_SIGNAL_QUALITY     0xAD //CWYang(+)
67 #define ZM_IOCTL_SET_PIBSS_MODE     0xAE
68 #define ZDAPIOCTL                   SIOCDEVPRIVATE
69
70 struct zdap_ioctl {
71         unsigned short cmd;                /* Command to run */
72         unsigned int   addr;                /* Length of the data buffer */
73         unsigned int   value;               /* Pointer to the data buffer */
74         unsigned char data[0x100];
75 };
76
77 /* Declaration of macro and function for handling WEP Keys */
78
79 #if 0
80
81 #define SKIP_ELEM { \
82     while(isxdigit(*p)) \
83         p++; \
84 }
85
86 #define SKIP_DELIMETER { \
87     if(*p == ':' || *p == ' ') \
88         p++; \
89 }
90
91 #endif
92
93 char hex(char);
94 unsigned char asctohex(char *str);
95
96 char *prgname;
97
98 int set_ioctl(int sock, struct ifreq *req)
99 {
100     if (ioctl(sock, ZDAPIOCTL, req) < 0) {
101         fprintf(stderr, "%s: ioctl(SIOCGIFMAP): %s\n",
102                 prgname, strerror(errno));
103         return -1;
104     }
105
106     return 0;
107 }
108
109
110 int read_reg(int sock, struct ifreq *req)
111 {
112     struct zdap_ioctl *zdreq = 0;
113
114     if (!set_ioctl(sock, req))
115         return -1;
116
117     //zdreq = (struct zdap_ioctl *)req->ifr_data;
118     //printf( "reg = %4x, value = %4x\n", zdreq->addr, zdreq->value);
119
120     return 0;
121 }
122
123
124 int read_mem(int sock, struct ifreq *req)
125 {
126     struct zdap_ioctl *zdreq = 0;
127     int i;
128
129     if (!set_ioctl(sock, req))
130         return -1;
131
132     /*zdreq = (struct zdap_ioctl *)req->ifr_data;
133     printf( "dump mem from %x, length = %x\n", zdreq->addr, zdreq->value);
134
135     for (i=0; i<zdreq->value; i++) {
136         printf("%02x", zdreq->data[i]);
137         printf(" ");
138
139         if ((i>0) && ((i+1)%16 == 0))
140             printf("\n");
141     }*/
142
143     return 0;
144 }
145
146
147 int main(int argc, char **argv)
148 {
149     int sock;
150     int addr, value;
151     struct ifreq req;
152     char *action = NULL;
153     struct zdap_ioctl zdreq;
154
155     prgname = argv[0];
156
157     if (argc < 3) {
158         fprintf(stderr,"%s: usage is \"%s <ifname> <operation> [<address>] [<value>]\"\n",
159                 prgname, prgname);
160         fprintf(stderr,"valid operation: read, write, mem, reg,\n");
161         fprintf(stderr,"               : txd, rxd, rmem, wmem\n");
162         fprintf(stderr,"               : dmat, regt, test\n");
163
164         fprintf(stderr,"    scan, Channel Scan\n");
165         fprintf(stderr,"    rts <decimal>, Set RTS Threshold\n");
166         fprintf(stderr,"    frag <decimal>, Set Fragment Threshold\n");
167         fprintf(stderr,"    rate <0-28>, 0:AUTO, 1-4:CCK, 5-12:OFDM, 13-28:HT\n");
168         fprintf(stderr,"    TBD mix <0 or 1>, Set 1 to enable mixed mode\n");
169         fprintf(stderr,"    enc, <0-3>, 0=>OPEN, 1=>WEP64, 2=>WEP128, 3=>WEP256\n");
170         fprintf(stderr,"    skey <key>, Set WEP key\n");
171         fprintf(stderr,"    txcnt, Get TxQ Cnt\n");
172         fprintf(stderr,"    dagcnt, Get Deaggregate Cnt\n");
173         fprintf(stderr,"    durmode <mode>, Set Duration Mode 0=>HW, 1=>SW\n");
174         fprintf(stderr,"    aeskey <user> <key>\n");
175         fprintf(stderr,"    aesmode <mode>\n");
176         fprintf(stderr,"    wlanmode <0,1> 0:Station mode, 1:PIBSS mode\n");
177         fprintf(stderr,"    tal <0,1>, Get Current Tally Info, 0=>read, 1=>read and reset\n");
178
179         exit(1);
180     }
181
182     strcpy(req.ifr_name, argv[1]);
183     zdreq.addr = 0;
184     zdreq.value = 0;
185
186     /* a silly raw socket just for ioctl()ling it */
187     sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
188     if (sock < 0) {
189         fprintf(stderr, "%s: socket(): %s\n", argv[0], strerror(errno));
190         exit(1);
191     }
192
193     if (argc >= 4)
194     {
195         sscanf(argv[3], "%x", &addr);
196     }
197
198     if (argc >= 5)
199     {
200         sscanf(argv[4], "%x", &value);
201     }
202
203     zdreq.addr = addr;
204     zdreq.value = value;
205
206     if (!strcmp(argv[2], "read"))
207     {
208         zdreq.cmd = ZM_IOCTL_REG_READ;
209     }
210     else if (!strcmp(argv[2], "mem"))
211     {
212         zdreq.cmd = ZM_IOCTL_MEM_DUMP;
213     }
214     else if (!strcmp(argv[2], "write"))
215     {
216         zdreq.cmd = ZM_IOCTL_REG_WRITE;
217     }
218     else if (!strcmp(argv[2], "reg"))
219     {
220         zdreq.cmd = ZM_IOCTL_REG_DUMP;
221     }
222     else if (!strcmp(argv[2], "txd"))
223     {
224         zdreq.cmd = ZM_IOCTL_TXD_DUMP;
225     }
226     else if (!strcmp(argv[2], "rxd"))
227     {
228         zdreq.cmd = ZM_IOCTL_RXD_DUMP;
229     }
230     else if (!strcmp(argv[2], "rmem"))
231     {
232         zdreq.cmd = ZM_IOCTL_MEM_READ;
233     }
234     else if (!strcmp(argv[2], "wmem"))
235     {
236         zdreq.cmd = ZM_IOCTL_MEM_WRITE;
237     }
238     else if (!strcmp(argv[2], "dmat"))
239     {
240         zdreq.cmd = ZM_IOCTL_DMA_TEST;
241     }
242     else if (!strcmp(argv[2], "regt"))
243     {
244         zdreq.cmd = ZM_IOCTL_REG_TEST;
245     }
246     else if (!strcmp(argv[2], "test"))
247     {
248         zdreq.cmd = ZM_IOCTL_TEST;
249     }
250     else if (!strcmp(argv[2], "tal"))
251     {
252         sscanf(argv[3], "%d", &addr);
253         zdreq.addr = addr;
254         zdreq.cmd = ZM_IOCTL_TALLY;
255     }
256     else if (!strcmp(argv[2], "rts"))
257     {
258         sscanf(argv[3], "%d", &addr);
259         zdreq.addr = addr;
260         zdreq.cmd = ZM_IOCTL_RTS;
261     }
262     else if (!strcmp(argv[2], "mix"))
263     {
264         zdreq.cmd = ZM_IOCTL_MIX_MODE;
265     }
266     else if (!strcmp(argv[2], "frag"))
267     {
268         sscanf(argv[3], "%d", &addr);
269         zdreq.addr = addr;
270         zdreq.cmd = ZM_IOCTL_FRAG;
271     }
272     else if (!strcmp(argv[2], "scan"))
273     {
274         zdreq.cmd = ZM_IOCTL_SCAN;
275     }
276     else if (!strcmp(argv[2], "skey"))
277     {
278         zdreq.cmd = ZM_IOCTL_KEY;
279
280         if (argc >= 4)
281         {
282             unsigned char temp[29];
283             int i;
284             int keyLen;
285             int encType;
286
287             keyLen = strlen(argv[3]);
288
289             if (keyLen == 10)
290             {
291                 sscanf(argv[3], "%02x%02x%02x%02x%02x", &temp[0], &temp[1],
292                         &temp[2], &temp[3], &temp[4]);
293             }
294             else if (keyLen == 26)
295             {
296                 sscanf(argv[3], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
297                         &temp[0], &temp[1], &temp[2], &temp[3], &temp[4],
298                         &temp[5], &temp[6], &temp[7], &temp[8], &temp[9],
299                          &temp[10], &temp[11], &temp[12]);
300             }
301             else if (keyLen == 58)
302             {
303                 sscanf(argv[3], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
304                         &temp[0], &temp[1], &temp[2], &temp[3], &temp[4],
305                         &temp[5], &temp[6], &temp[7], &temp[8], &temp[9],
306                         &temp[10], &temp[11], &temp[12], &temp[13], &temp[14],
307                         &temp[15], &temp[16], &temp[17], &temp[18], &temp[19],
308                         &temp[20], &temp[21], &temp[22], &temp[23], &temp[24],
309                         &temp[25], &temp[26], &temp[27], &temp[28]);
310             }
311             else
312             {
313                 fprintf(stderr, "Invalid key length\n");
314                 exit(1);
315             }
316             zdreq.addr = keyLen/2;
317
318             for(i=0; i<zdreq.addr; i++)
319             {
320                 zdreq.data[i] = temp[i];
321             }
322         }
323         else
324         {
325             printf("Error : Key required!\n");
326         }
327     }
328     else if (!strcmp(argv[2], "rate"))
329     {
330         sscanf(argv[3], "%d", &addr);
331
332         if (addr > 28)
333         {
334             fprintf(stderr, "Invalid rate, range:0~28\n");
335             exit(1);
336         }
337         zdreq.addr = addr;
338         zdreq.cmd = ZM_IOCTL_RATE;
339     }
340     else if (!strcmp(argv[2], "enc"))
341     {
342         sscanf(argv[3], "%d", &addr);
343
344         if (addr > 3)
345         {
346             fprintf(stderr, "Invalid encryption mode, range:0~3\n");
347             exit(1);
348         }
349
350         if (addr == 2)
351         {
352             addr = 5;
353         }
354         else if (addr == 3)
355         {
356             addr = 6;
357         }
358
359         zdreq.addr = addr;
360         zdreq.cmd = ZM_IOCTL_ENCRYPTION_MODE;
361     }
362     else if (!strcmp(argv[2], "txcnt"))
363     {
364         zdreq.cmd = ZM_IOCTL_GET_TXCNT;
365     }
366     else if (!strcmp(argv[2], "dagcnt"))
367     {
368         sscanf(argv[3], "%d", &addr);
369
370         if (addr != 0 && addr != 1)
371         {
372             fprintf(stderr, "The value should be 0 or 1\n");
373             exit(0);
374         }
375
376         zdreq.addr = addr;
377         zdreq.cmd = ZM_IOCTL_GET_DEAGG_CNT;
378     }
379     else if (!strcmp(argv[2], "durmode"))
380     {
381         sscanf(argv[3], "%d", &addr);
382
383         if (addr != 0 && addr != 1)
384         {
385             fprintf(stderr, "The Duration mode should be 0 or 1\n");
386             exit(0);
387         }
388
389         zdreq.addr = addr;
390         zdreq.cmd = ZM_IOCTL_DURATION_MODE;
391     }
392     else if (!strcmp(argv[2], "aeskey"))
393     {
394         unsigned char temp[16];
395         int i;
396
397         sscanf(argv[3], "%d", &addr);
398
399         sscanf(argv[4], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5], &temp[6], &temp[7], &temp[8], &temp[9], &temp[10], &temp[11], &temp[12], &temp[13], &temp[14], &temp[15]);
400
401         for(i = 0; i < 16; i++)
402         {
403             zdreq.data[i] = temp[i];
404         }
405
406         zdreq.addr = addr;
407         zdreq.cmd = ZM_IOCTL_SET_AES_KEY;
408     }
409     else if (!strcmp(argv[2], "aesmode"))
410     {
411         sscanf(argv[3], "%d", &addr);
412
413         zdreq.addr = addr;
414         zdreq.cmd = ZM_IOCTL_SET_AES_MODE;
415     }
416     else if (!strcmp(argv[2], "wlanmode"))
417     {
418         sscanf(argv[3], "%d", &addr);
419
420         zdreq.addr = addr;
421         zdreq.cmd = ZM_IOCTL_SET_PIBSS_MODE;
422     }
423     else
424     {
425             fprintf(stderr, "error action\n");
426         exit(1);
427     }
428
429     req.ifr_data = (char *)&zdreq;
430     set_ioctl(sock, &req);
431
432 fail:
433     exit(0);
434 }
435
436 unsigned char asctohex(char *str)
437 {
438     unsigned char value;
439
440     value = hex(*str) & 0x0f;
441     value = value << 4;
442     str++;
443     value |= hex(*str) & 0x0f;
444
445     return value;
446 }
447
448 char hex(char v)
449 {
450     if(isdigit(v))
451         return v - '0';
452     else if(isxdigit(v))
453         return (tolower(v) - 'a' + 10);
454     else
455         return 0;
456 }
457