Bring in Damien's IPW driver.
[dragonfly.git] / usr.sbin / ipwcontrol / ipwcontrol.c
1 /*-
2  * Copyright (c) 2004, 2005
3  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $Id: ipwcontrol.c,v 1.5.2.1 2005/01/13 20:01:05 damien Exp $
28  * $DragonFly: src/usr.sbin/ipwcontrol/Attic/ipwcontrol.c,v 1.1 2005/03/06 18:25:14 dillon Exp $
29  */
30
31 #include <sys/cdefs.h>
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/sysctl.h>
39
40 #include <net/if.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50
51 #define SIOCSLOADFW      _IOW('i', 137, struct ifreq)
52 #define SIOCSKILLFW      _IOW('i', 138, struct ifreq)
53
54 static void usage(void);
55 static int do_req(const char *, unsigned long, void *);
56 static void load_firmware(const char *, const char *);
57 static void kill_firmware(const char *);
58 static void get_radio_state(void);
59 static void get_statistics(void);
60
61 int
62 main(int argc, char **argv)
63 {
64         int ch;
65         const char *iface;
66
67         opterr = 0;
68         ch = getopt(argc, argv, "i:");
69         if (ch == 'i') {
70                 iface = optarg;
71         } else {
72                 if (argc > 1 && argv[1][0] != '-') {
73                         iface = argv[1];
74                         optind = 2;
75                 } else {
76                         iface = "ipw0";
77                         optind = 1;
78                 }
79                 optreset = 1;
80         }
81         opterr = 1;
82
83         while ((ch = getopt(argc, argv, "f:kr")) != -1) {
84                 switch (ch) {
85                 case 'f':
86                         load_firmware(iface, optarg);
87                         return EX_OK;
88
89                 case 'k':
90                         kill_firmware(iface);
91                         return EX_OK;
92
93                 case 'r':
94                         get_radio_state();
95                         return EX_OK;
96
97                 default:
98                         usage();
99                 }
100         }
101
102         get_statistics();
103
104         return EX_OK;
105 }
106
107 extern char *__progname;
108
109 static void
110 usage(void)
111 {
112         fprintf(stderr, "usage:  %s -i iface\n"
113             "\t%s -i iface -f firmware\n"
114             "\t%s -i iface -k\n"
115             "\t%s -i iface -r\n", __progname, __progname, __progname,
116             __progname);
117
118         exit(EX_USAGE);
119 }
120
121 static int
122 do_req(const char *iface, unsigned long req, void *data)
123 {
124         int s;
125         struct ifreq ifr;
126         int error;
127
128         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
129                 err(EX_OSERR, "Can't create socket");
130
131         (void)memset(&ifr, 0, sizeof(ifr));
132         (void)strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
133         ifr.ifr_data = data;
134         error = ioctl(s, req, &ifr);
135
136         (void)close(s);
137
138         return error;
139 }
140
141 static void
142 load_firmware(const char *iface, const char *firmware)
143 {
144         int fd;
145         struct stat st;
146         void *map;
147
148         if ((fd = open(firmware, O_RDONLY)) == -1)
149                 err(EX_OSERR, "%s", firmware);
150
151         if (fstat(fd, &st) == -1)
152                 err(EX_OSERR, "Unable to stat %s", firmware);
153
154         if ((map = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
155                 err(EX_OSERR, "Can't map %s into memory", firmware);
156
157         if (do_req(iface, SIOCSLOADFW, map) == -1)
158                 err(EX_OSERR, "Can't load %s to driver", firmware);
159
160         (void)munmap(map, st.st_size);
161         (void)close(fd);
162 }
163
164 static void
165 kill_firmware(const char *iface)
166 {
167         if (do_req(iface, SIOCSKILLFW, NULL) == -1)
168                 err(EX_OSERR, "Can't kill firmware");
169 }
170
171 static void
172 get_radio_state()
173 {
174         int radio, len;
175
176         len = sizeof radio;
177         if (sysctlbyname("dev.ipw.0.radio", &radio, &len, NULL, 0) == -1)
178                 err(EX_OSERR, "Could not get radio transmitter state");
179
180         (void)printf("Radio is %s\n", radio ? "ON" : "OFF");
181 }
182
183 struct statistic {
184         int index;
185         const char *desc;
186         int unit;
187 #define INT             1
188 #define HEX             2
189 #define MASK            HEX
190 #define PERCENTAGE      3
191 #define BOOL            4
192 };
193
194 /*-
195  * TIM  = Traffic Information Message
196  * DTIM = Delivery TIM
197  * ATIM = Announcement TIM
198  * PSP  = Power Save Poll
199  * RTS  = Request To Send
200  * CTS  = Clear To Send
201  * RSSI = Received Signal Strength Indicator
202  */
203
204 static const struct statistic tbl[] = {
205         { 1, "Number of frames submitted for transfer", INT },
206         { 2, "Number of frames transmitted", INT },
207         { 3, "Number of unicast frames transmitted", INT },
208         { 4, "Number of unicast frames transmitted at 1Mb/s", INT },
209         { 5, "Number of unicast frames transmitted at 2Mb/s", INT },
210         { 6, "Number of unicast frames transmitted at 5.5Mb/s", INT },
211         { 7, "Number of unicast frames transmitted at 11Mb/s", INT },
212
213         { 13, "Number of multicast frames transmitted at 1Mb/s", INT },
214         { 14, "Number of multicast frames transmitted at 2Mb/s", INT },
215         { 15, "Number of multicast frames transmitted at 5.5Mb/s", INT },
216         { 16, "Number of multicast frames transmitted at 11Mb/s", INT },
217
218         { 21, "Number of null frames transmitted", INT },
219         { 22, "Number of RTS frames transmitted", INT },
220         { 23, "Number of CTS frames transmitted", INT },
221         { 24, "Number of ACK frames transmitted", INT },
222         { 25, "Number of association requests transmitted", INT },
223         { 26, "Number of association responses transmitted", INT },
224         { 27, "Number of reassociation requests transmitted", INT },
225         { 28, "Number of reassociation responses transmitted", INT },
226         { 29, "Number of probe requests transmitted", INT },
227         { 30, "Number of probe reponses transmitted", INT },
228         { 31, "Number of beacons transmitted", INT },
229         { 32, "Number of ATIM frames transmitted", INT },
230         { 33, "Number of disassociation requests transmitted", INT },
231         { 34, "Number of authentication requests transmitted", INT },
232         { 35, "Number of deauthentication requests transmitted", INT },
233
234         { 41, "Number of bytes transmitted", INT },
235         { 42, "Number of transmission retries", INT },
236         { 43, "Number of transmission retries at 1Mb/s", INT },
237         { 44, "Number of transmission retries at 2Mb/s", INT },
238         { 45, "Number of transmission retries at 5.5Mb/s", INT },
239         { 46, "Number of transmission retries at 11Mb/s", INT },
240
241         { 51, "Number of transmission failures", INT },
242
243         { 54, "Number of transmission aborted due to DMA", INT },
244
245         { 56, "Number of disassociation failures", INT },
246
247         { 58, "Number of spanning tree frames transmitted", INT },
248         { 59, "Number of transmission errors due to missing ACK", INT },
249
250         { 61, "Number of frames received", INT },
251         { 62, "Number of unicast frames received", INT },
252         { 63, "Number of unicast frames received at 1Mb/s", INT },
253         { 64, "Number of unicast frames received at 2Mb/s", INT },
254         { 65, "Number of unicast frames received at 5.5Mb/s", INT },
255         { 66, "Number of unicast frames received at 11Mb/s", INT },
256
257         { 71, "Number of multicast frames received", INT },
258         { 72, "Number of multicast frames received at 1Mb/s", INT },
259         { 73, "Number of multicast frames received at 2Mb/s", INT },
260         { 74, "Number of multicast frames received at 5.5Mb/s", INT },
261         { 75, "Number of multicast frames received at 11Mb/s", INT },
262
263         { 80, "Number of null frames received", INT },
264         { 81, "Number of poll frames received", INT },
265         { 82, "Number of RTS frames received", INT },
266         { 83, "Number of CTS frames received", INT },
267         { 84, "Number of ACK frames received", INT },
268         { 85, "Number of CF-End frames received", INT },
269         { 86, "Number of CF-End + CF-Ack frames received", INT },
270         { 87, "Number of association requests received", INT },
271         { 88, "Number of association responses received", INT },
272         { 89, "Number of reassociation requests received", INT },
273         { 90, "Number of reassociation responses received", INT },
274         { 91, "Number of probe requests received", INT },
275         { 92, "Number of probe reponses received", INT },
276         { 93, "Number of beacons received", INT },
277         { 94, "Number of ATIM frames received", INT },
278         { 95, "Number of disassociation requests received", INT },
279         { 96, "Number of authentication requests received", INT },
280         { 97, "Number of deauthentication requests received", INT },
281
282         { 101, "Number of bytes received", INT },
283         { 102, "Number of frames with a bad CRC received", INT },
284         { 103, "Number of frames with a bad CRC received at 1Mb/s", INT },
285         { 104, "Number of frames with a bad CRC received at 2Mb/s", INT },
286         { 105, "Number of frames with a bad CRC received at 5.5Mb/s", INT },
287         { 106, "Number of frames with a bad CRC received at 11Mb/s", INT },
288
289         { 112, "Number of duplicated frames received at 1Mb/s", INT },
290         { 113, "Number of duplicated frames received at 2Mb/s", INT },
291         { 114, "Number of duplicated frames received at 5.5Mb/s", INT },
292         { 115, "Number of duplicated frames received at 11Mb/s", INT },
293
294         { 119, "Number of duplicated frames received", INT },
295
296         { 123, "Number of frames with a bad protocol received", INT },
297         { 124, "Boot time", INT },
298         { 125, "Number of frames dropped due to missing buffer", INT },
299         { 126, "Number of frames dropped due to DMA", INT },
300
301         { 128, "Number of frames dropped due to missing fragment", INT },
302         { 129, "Number of frames dropped due to non-seq fragment", INT },
303         { 130, "Number of frames dropped due to missing first frame", INT },
304         { 131, "Number of frames dropped due to uncompleted frame", INT },
305
306         { 137, "Number of times adapter suspended", INT },
307         { 138, "Beacon timeout", INT },
308         { 139, "Number of poll response timeouts", INT },
309
310         { 141, "Number of PSP DTIM frames received", INT },
311         { 142, "Number of PSP TIM frames received", INT },
312         { 143, "PSP station Id", INT },
313
314         { 147, "RTC time of last association", INT },
315         { 148, "Percentage of missed beacons", PERCENTAGE },
316         { 149, "Percentage of missed transmission retries", PERCENTAGE },
317
318         { 151, "Number of access points in access points table", INT },
319
320         { 153, "Number of associations", INT },
321         { 154, "Number of association failures", INT },
322         { 156, "Number of full scans", INT },
323         { 157, "Card disabled", BOOL },
324
325         { 160, "RSSI at time of association", INT },
326         { 161, "Number of reassociations due to no probe response", INT },
327         { 162, "Number of reassociations due to poor line quality", INT },
328         { 163, "Number of reassociations due to load", INT },
329         { 164, "Number of reassociations due to access point RSSI level", INT },
330         { 165, "Number of reassociations due to load leveling", INT },
331
332         { 170, "Number of times authentication failed", INT },
333         { 171, "Number of times authentication response failed", INT },
334         { 172, "Number of entries in association table", INT },
335         { 173, "Average RSSI", INT },
336
337         { 176, "Self test status", INT },
338         { 177, "Power mode", INT },
339         { 178, "Power index", INT },
340         { 179, "IEEE country code", HEX },
341         { 180, "Channels supported for this country", MASK },
342         { 181, "Number of adapter warm resets", INT },
343         { 182, "Beacon interval", INT },
344
345         { 184, "Princeton version", INT },
346         { 185, "Antenna diversity disabled", BOOL },
347         { 186, "CCA RSSI", INT },
348         { 187, "Number of times EEPROM updated", INT },
349         { 188, "Beacon intervals between DTIM", INT },
350         { 189, "Current channel", INT },
351         { 190, "RTC time", INT },
352         { 191, "Operating mode", INT },
353         { 192, "Transmission rate", HEX },
354         { 193, "Supported transmission rates", MASK },
355         { 194, "ATIM window", INT },
356         { 195, "Supported basic transmission rates", MASK },
357         { 196, "Adapter highest rate", HEX },
358         { 197, "Access point highest rate", HEX },
359         { 198, "Management frame capability", BOOL },
360         { 199, "Type of authentication", INT },
361         { 200, "Adapter card platform type", INT },
362         { 201, "RTS threshold", INT },
363         { 202, "International mode", BOOL },
364         { 203, "Fragmentation threshold", INT },
365
366         { 213, "Microcode version", INT },
367
368         { 0, NULL, 0 }
369 };
370
371 static void
372 get_statistics()
373 {
374         static u_int32_t stats[256];
375         const struct statistic *st;
376         int len;
377
378         len = sizeof stats;
379         if (sysctlbyname("dev.ipw.0.stats", stats, &len, NULL, 0) == -1)
380                 err(EX_OSERR, "Can't retrieve statistics");
381
382         for (st = tbl; st->index != 0; st++) {
383                 (void)printf("%-60s[", st->desc);
384                 switch (st->unit) {
385                 case INT:
386                         (void)printf("%u", stats[st->index]);
387                         break;
388                 
389                 case BOOL:
390                         (void)printf(stats[st->index] ? "true" : "false");
391                         break;
392
393                 case PERCENTAGE:
394                         (void)printf("%u%%", stats[st->index]);
395                         break;
396
397                 case HEX:
398                 default:
399                         (void)printf("0x%08X", stats[st->index]);
400                 }
401                 (void)printf("]\n");
402         }
403 }