- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / 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         /*
118          * zdreq = (struct zdap_ioctl *)req->ifr_data;
119          * printf( "reg = %4x, value = %4x\n", zdreq->addr, zdreq->value);
120          */
121
122         return 0;
123 }
124
125
126 int read_mem(int sock, struct ifreq *req)
127 {
128         struct zdap_ioctl *zdreq = 0;
129         int i;
130
131         if (!set_ioctl(sock, req))
132                 return -1;
133
134         /*
135          * zdreq = (struct zdap_ioctl *)req->ifr_data;
136          * printf("dump mem from %x, length = %x\n", zdreq->addr, zdreq->value);
137          *
138          * for (i=0; i<zdreq->value; i++) {
139          *      printf("%02x", zdreq->data[i]);
140          *      printf(" ");
141          *
142          *      if ((i>0) && ((i+1)%16 == 0))
143          *              printf("\n");
144          * }
145          */
146
147         return 0;
148 }
149
150
151 int main(int argc, char **argv)
152 {
153         int sock;
154         int addr, value;
155         struct ifreq req;
156         char *action = NULL;
157         struct zdap_ioctl zdreq;
158
159         prgname = argv[0];
160
161         if (argc < 3) {
162                 fprintf(stderr, "%s: usage is \"%s <ifname> <operation>"
163                                 "[<address>] [<value>]\"\n", prgname, prgname);
164                 fprintf(stderr, "valid operation : read, write, mem, reg, \n");
165                 fprintf(stderr, "               : txd, rxd, rmem, wmem\n");
166                 fprintf(stderr, "               : dmat, regt, test\n");
167
168                 fprintf(stderr, "       scan, Channel Scan\n");
169                 fprintf(stderr, "       rts <decimal>, Set RTS Threshold\n");
170                 fprintf(stderr, "       frag <decimal>, Set Fragment"
171                         " Threshold\n");
172                 fprintf(stderr, "       rate <0-28>, 0:AUTO, 1-4:CCK,"
173                         " 5-12:OFDM, 13-28:HT\n");
174                 fprintf(stderr, "       TBD mix <0 or 1>, Set 1 to enable"
175                         " mixed mode\n");
176                 fprintf(stderr, "       enc, <0-3>, 0=>OPEN, 1=>WEP64, "
177                         "2=>WEP128, 3=>WEP256\n");
178                 fprintf(stderr, "       skey <key>, Set WEP key\n");
179                 fprintf(stderr, "       txcnt, Get TxQ Cnt\n");
180                 fprintf(stderr, "       dagcnt, Get Deaggregate Cnt\n");
181                 fprintf(stderr, "       durmode <mode>, Set Duration Mode "
182                         "0=>HW, 1=>SW\n");
183                 fprintf(stderr, "       aeskey <user> <key>\n");
184                 fprintf(stderr, "       aesmode <mode>\n");
185                 fprintf(stderr, "       wlanmode <0,1> 0:Station mode, "
186                         "1:PIBSS mode\n");
187                 fprintf(stderr, "       tal <0,1>, Get Current Tally Info, "
188                         "0=>read, 1=>read and reset\n");
189
190                 exit(1);
191         }
192
193         strcpy(req.ifr_name, argv[1]);
194         zdreq.addr = 0;
195         zdreq.value = 0;
196
197         /* a silly raw socket just for ioctl()ling it */
198         sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
199         if (sock < 0) {
200                 fprintf(stderr, "%s: socket(): %s\n", argv[0], strerror(errno));
201                 exit(1);
202         }
203
204         if (argc >= 4)
205                 sscanf(argv[3], "%x", &addr);
206
207         if (argc >= 5)
208                 sscanf(argv[4], "%x", &value);
209
210         zdreq.addr = addr;
211         zdreq.value = value;
212
213         if (!strcmp(argv[2], "read"))
214                 zdreq.cmd = ZM_IOCTL_REG_READ;
215         else if (!strcmp(argv[2], "mem"))
216                 zdreq.cmd = ZM_IOCTL_MEM_DUMP;
217         else if (!strcmp(argv[2], "write"))
218                 zdreq.cmd = ZM_IOCTL_REG_WRITE;
219         else if (!strcmp(argv[2], "reg"))
220                 zdreq.cmd = ZM_IOCTL_REG_DUMP;
221         else if (!strcmp(argv[2], "txd"))
222                 zdreq.cmd = ZM_IOCTL_TXD_DUMP;
223         else if (!strcmp(argv[2], "rxd"))
224                 zdreq.cmd = ZM_IOCTL_RXD_DUMP;
225         else if (!strcmp(argv[2], "rmem"))
226                 zdreq.cmd = ZM_IOCTL_MEM_READ;
227         else if (!strcmp(argv[2], "wmem"))
228                 zdreq.cmd = ZM_IOCTL_MEM_WRITE;
229         else if (!strcmp(argv[2], "dmat"))
230                 zdreq.cmd = ZM_IOCTL_DMA_TEST;
231         else if (!strcmp(argv[2], "regt"))
232                 zdreq.cmd = ZM_IOCTL_REG_TEST;
233         else if (!strcmp(argv[2], "test"))
234                 zdreq.cmd = ZM_IOCTL_TEST;
235         else if (!strcmp(argv[2], "tal")) {
236                 sscanf(argv[3], "%d", &addr);
237                 zdreq.addr = addr;
238                 zdreq.cmd = ZM_IOCTL_TALLY;
239         } else if (!strcmp(argv[2], "rts")) {
240                 sscanf(argv[3], "%d", &addr);
241                 zdreq.addr = addr;
242                 zdreq.cmd = ZM_IOCTL_RTS;
243         } else if (!strcmp(argv[2], "mix")) {
244                 zdreq.cmd = ZM_IOCTL_MIX_MODE;
245         } else if (!strcmp(argv[2], "frag")) {
246                 sscanf(argv[3], "%d", &addr);
247                 zdreq.addr = addr;
248                 zdreq.cmd = ZM_IOCTL_FRAG;
249         } else if (!strcmp(argv[2], "scan")) {
250                 zdreq.cmd = ZM_IOCTL_SCAN;
251         } else if (!strcmp(argv[2], "skey")) {
252                 zdreq.cmd = ZM_IOCTL_KEY;
253
254                 if (argc >= 4) {
255                         unsigned char temp[29];
256                         int i;
257                         int keyLen;
258                         int encType;
259
260                         keyLen = strlen(argv[3]);
261
262                         if (keyLen == 10)
263                                 sscanf(argv[3], "%02x%02x%02x%02x%02x",
264                                         &temp[0], &temp[1], &temp[2], &temp[3],
265                                         &temp[4]);
266                         else if (keyLen == 26)
267                                 sscanf(argv[3], "%02x%02x%02x%02x%02x%02x"
268                                         "%02x%02x%02x%02x%02x%02x%02x",
269                                         &temp[0], &temp[1], &temp[2], &temp[3],
270                                         &temp[4], &temp[5], &temp[6], &temp[7],
271                                         &temp[8], &temp[9], &temp[10],
272                                         &temp[11], &temp[12]);
273                         else if (keyLen == 58)
274                                 sscanf(argv[3], "%02x%02x%02x%02x%02x%02x"
275                                         "%02x%02x%02x%02x%02x%02x%02x%02x%02x"
276                                         "%02x%02x%02x%02x%02x%02x%02x%02x%02x"
277                                         "%02x%02x%02x%02x%02x",
278                                         &temp[0], &temp[1], &temp[2], &temp[3],
279                                         &temp[4], &temp[5], &temp[6], &temp[7],
280                                         &temp[8], &temp[9], &temp[10],
281                                         &temp[11], &temp[12], &temp[13],
282                                         &temp[14], &temp[15], &temp[16],
283                                         &temp[17], &temp[18], &temp[19],
284                                         &temp[20], &temp[21], &temp[22],
285                                         &temp[23], &temp[24], &temp[25],
286                                         &temp[26], &temp[27], &temp[28]);
287                         else {
288                                 fprintf(stderr, "Invalid key length\n");
289                                 exit(1);
290                         }
291                         zdreq.addr = keyLen/2;
292
293                         for (i = 0; i < zdreq.addr; i++)
294                                 zdreq.data[i] = temp[i];
295                 } else {
296                         printf("Error : Key required!\n");
297                 }
298         } else if (!strcmp(argv[2], "rate")) {
299                 sscanf(argv[3], "%d", &addr);
300
301                 if (addr > 28) {
302                         fprintf(stderr, "Invalid rate, range:0~28\n");
303                         exit(1);
304                 }
305                 zdreq.addr = addr;
306                 zdreq.cmd = ZM_IOCTL_RATE;
307         } else if (!strcmp(argv[2], "enc")) {
308                 sscanf(argv[3], "%d", &addr);
309
310                 if (addr > 3) {
311                         fprintf(stderr, "Invalid encryption mode, range:0~3\n");
312                         exit(1);
313                 }
314
315                 if (addr == 2)
316                         addr = 5;
317                 else if (addr == 3)
318                         addr = 6;
319
320                 zdreq.addr = addr;
321                 zdreq.cmd = ZM_IOCTL_ENCRYPTION_MODE;
322         } else if (!strcmp(argv[2], "txcnt")) {
323                 zdreq.cmd = ZM_IOCTL_GET_TXCNT;
324         } else if (!strcmp(argv[2], "dagcnt")) {
325                 sscanf(argv[3], "%d", &addr);
326
327                 if (addr != 0 && addr != 1) {
328                         fprintf(stderr, "The value should be 0 or 1\n");
329                         exit(0);
330                 }
331
332                 zdreq.addr = addr;
333                 zdreq.cmd = ZM_IOCTL_GET_DEAGG_CNT;
334         } else if (!strcmp(argv[2], "durmode")) {
335                 sscanf(argv[3], "%d", &addr);
336
337                 if (addr != 0 && addr != 1) {
338                         fprintf(stderr, "The Duration mode should be 0 or 1\n");
339                         exit(0);
340                 }
341
342                 zdreq.addr = addr;
343                 zdreq.cmd = ZM_IOCTL_DURATION_MODE;
344         } else if (!strcmp(argv[2], "aeskey")) {
345                 unsigned char temp[16];
346                 int i;
347
348                 sscanf(argv[3], "%d", &addr);
349
350                 sscanf(argv[4], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
351                         "%02x%02x%02x%02x%02x%02x", &temp[0], &temp[1],
352                         &temp[2], &temp[3], &temp[4], &temp[5], &temp[6],
353                         &temp[7], &temp[8], &temp[9], &temp[10], &temp[11],
354                         &temp[12], &temp[13], &temp[14], &temp[15]);
355
356                 for (i = 0; i < 16; i++)
357                         zdreq.data[i] = temp[i];
358
359                 zdreq.addr = addr;
360                 zdreq.cmd = ZM_IOCTL_SET_AES_KEY;
361         } else if (!strcmp(argv[2], "aesmode")) {
362                 sscanf(argv[3], "%d", &addr);
363
364                 zdreq.addr = addr;
365                 zdreq.cmd = ZM_IOCTL_SET_AES_MODE;
366         } else if (!strcmp(argv[2], "wlanmode")) {
367                 sscanf(argv[3], "%d", &addr);
368
369                 zdreq.addr = addr;
370                 zdreq.cmd = ZM_IOCTL_SET_PIBSS_MODE;
371         } else  {
372                 fprintf(stderr, "error action\n");
373                 exit(1);
374         }
375
376         req.ifr_data = (char *)&zdreq;
377         set_ioctl(sock, &req);
378
379 fail:
380         exit(0);
381 }
382
383 unsigned char asctohex(char *str)
384 {
385         unsigned char value;
386
387         value = hex(*str) & 0x0f;
388         value = value << 4;
389         str++;
390         value |= hex(*str) & 0x0f;
391
392         return value;
393 }
394
395 char hex(char v)
396 {
397         if (isdigit(v))
398                 return v - '0';
399         else if (isxdigit(v))
400                 return tolower(v) - 'a' + 10;
401         else
402                 return 0;
403 }
404