One more remainder of the post-SoC amd64 to fix VKERNEL build.
[dragonfly.git] / usr.sbin / iwicontrol / iwicontrol.c
1 /*
2  * Copyright (c) 2004, 2005
3  *      Damien Bergamini <damien.bergamini@free.fr>.
4  * Copyright (c) 2004, 2005
5  *      Andrew Atrens <atrens@nortelnetworks.com>.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $DragonFly: src/usr.sbin/iwicontrol/iwicontrol.c,v 1.4 2008/11/10 05:00:12 swildner Exp $
32  */
33
34 #include <sys/cdefs.h>
35
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
42
43 #include <net/if.h>
44
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <unistd.h>
53
54 #define SIOCSLOADFW      _IOW('i', 137, struct ifreq)
55 #define SIOCSKILLFW      _IOW('i', 138, struct ifreq)
56
57 struct firmware {
58         char    *boot;
59         size_t  boot_size;
60         char    *ucode;
61         size_t  ucode_size;
62         char    *main;
63         size_t  main_size;
64 };
65
66 struct header {
67         u_int32_t       version;
68         u_int32_t       mode;
69 } __attribute__((__packed__));
70
71 static void usage(void);
72 static int do_req(const char *, unsigned long, void *);
73 static void mmap_file(const char *, char **, size_t *);
74 static void load_firmware(const char *, const char *, const char *);
75 static void kill_firmware(const char *);
76 static void get_radio_state(const char *);
77 static void get_statistics(const char *);
78 static void dump_debug(const char *);
79
80 int
81 main(int argc, char **argv)
82 {
83         int ch;
84         const char *iface = NULL, *mode = "bss", *path = NULL;
85         int noflag = 1, kflag = 0, rflag = 0, dflag = 0;
86
87         if (argc > 1 && argv[1][0] != '-') {
88                 iface = argv[1];
89                 optind++;
90         }
91
92         while ((ch = getopt(argc, argv, "d:i:km:rD")) != -1) {
93                 if (ch != 'i')
94                         noflag = 0;
95
96                 switch (ch) {
97                 case 'd':
98                         path = optarg;
99                         break;
100
101                 case 'i':
102                         iface = optarg;
103                         break;
104
105                 case 'k':
106                         kflag = 1;
107                         break;
108
109                 case 'm':
110                         mode = optarg;
111                         break;
112
113                 case 'r':
114                         rflag = 1;
115                         break;
116
117                 case 'D':
118                         dflag = 1;
119                         break;
120
121                 default:
122                         usage();
123                 }
124         }
125
126         if (iface == NULL)
127                 usage();
128
129         if (kflag && (path != NULL || rflag))
130                 usage();
131
132         if (kflag)
133                 kill_firmware(iface);
134
135         if (path != NULL)
136                 load_firmware(iface, path, mode);
137
138         if (rflag)
139                 get_radio_state(iface);
140
141         if (dflag)
142                 dump_debug(iface);
143
144         if (noflag)
145                 get_statistics(iface);
146
147         return EX_OK;
148 }
149
150 extern char *__progname;
151
152 static void
153 usage(void)
154 {
155         fprintf(stderr, "usage:  %s iface\n"
156             "\t%s iface -d path [-m bss|ibss]\n"
157             "\t%s iface -k\n"
158             "\t%s iface -r\n", __progname, __progname, __progname,
159             __progname);
160
161         exit(EX_USAGE);
162 }
163
164 static int
165 do_req(const char *iface, unsigned long req, void *data)
166 {
167         int s;
168         struct ifreq ifr;
169         int error;
170
171         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
172                 err(EX_OSERR, "Can't create socket");
173
174         memset(&ifr, 0, sizeof ifr);
175         strncpy(ifr.ifr_name, iface, sizeof ifr.ifr_name);
176         ifr.ifr_data = data;
177         error = ioctl(s, req, &ifr);
178
179         close(s);
180
181         return error;
182 }
183
184 static void
185 mmap_file(const char *filename, char **addr, size_t *len)
186 {
187         int fd;
188         struct stat st;
189
190         if ((fd = open(filename, O_RDONLY)) == -1)
191                 err(EX_OSERR, "%s", filename);
192
193         if (fstat(fd, &st) == -1)
194                 err(EX_OSERR, "Unable to stat %s", filename);
195
196         *len = st.st_size;
197
198         if ((*addr = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
199                 err(EX_OSERR, "Can't map %s into memory", filename);
200
201         *addr += sizeof (struct header);
202         *len -= sizeof (struct header);
203
204         close(fd);
205 }
206
207 static void
208 load_firmware(const char *iface, const char *path, const char *mode)
209 {
210         char filename[FILENAME_MAX];
211         struct firmware fw;
212
213         snprintf(filename, sizeof filename, "%s/iwi-boot.fw", path);
214         mmap_file(filename, &fw.boot, &fw.boot_size);
215
216         snprintf(filename, sizeof filename, "%s/iwi-ucode-%s.fw", path,
217             mode);
218         mmap_file(filename, &fw.ucode, &fw.ucode_size);
219
220         snprintf(filename, sizeof filename, "%s/iwi-%s.fw", path, mode);
221         mmap_file(filename, &fw.main, &fw.main_size);
222
223         if (do_req(iface, SIOCSLOADFW, &fw) == -1)
224                 err(EX_OSERR, "Can't load firmware to driver");
225 }
226
227 static void
228 kill_firmware(const char *iface)
229 {
230         if (do_req(iface, SIOCSKILLFW, NULL) == -1)
231                 err(EX_OSERR, "Can't kill firmware");
232 }
233
234 static void
235 dump_debug(const char *iface)
236 {
237         int dump = 1, len = sizeof dump;
238         char oid_name[128];
239         snprintf(oid_name, sizeof oid_name, "hw.%s.firmware_logs", iface );
240         if (sysctlbyname(oid_name, 0, 0, &dump, len) == -1)
241                 err(EX_OSERR, "Can't dump firmware logs");
242         printf("All firmware logs dumped.\n");
243 }
244
245
246 static void
247 get_radio_state(const char *iface)
248 {
249         int radio;
250         size_t len;
251         char oid_name[128];
252
253         snprintf(oid_name, sizeof oid_name, "hw.%s.radio", iface );
254         len = sizeof radio;
255         if (sysctlbyname(oid_name, &radio, &len, NULL, 0) == -1)
256                 err(EX_OSERR, "Can't get radio transmitter state");
257
258         printf("Radio is %s\n", radio ? "ON" : "OFF");
259 }
260
261 struct statistic {
262         int             index;
263         const char      *desc;
264 };
265
266 static const struct statistic tbl[] = {
267         {  1, "Current transmission rate" },
268         {  2, "Fragmentation threshold" },
269         {  3, "RTS threshold" },
270         {  4, "Number of frames submitted for transfer" },
271         {  5, "Number of frames transmitted" },
272         {  6, "Number of unicast frames transmitted" },
273         {  7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
274         {  8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
275         {  9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
276         { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
277
278         { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
279         { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
280         { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
281         { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
282         { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
283         { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
284         { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
285         { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
286         { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
287         { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
288         { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
289         { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
290         { 31, "Number of multicast frames transmitted" },
291         { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
292         { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
293         { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
294         { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
295
296         { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
297         { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
298         { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
299         { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
300         { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
301         { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
302         { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
303         { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
304         { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
305         { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
306         { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
307         { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
308         { 56, "Number of transmission retries" },
309         { 57, "Number of transmission failures" },
310         { 58, "Number of frames with a bad CRC received" },
311
312         { 61, "Number of full scans" },
313         { 62, "Number of partial scans" },
314
315         { 64, "Number of bytes transmitted" },
316         { 65, "Current RSSI" },
317         { 66, "Number of beacons received" },
318         { 67, "Number of beacons missed" },
319
320         { 0, NULL }
321 };
322
323 static void
324 get_statistics(const char *iface)
325 {
326         static u_int32_t stats[256];
327         char oid_name[128];
328         const struct statistic *stt;
329         size_t len;
330
331         snprintf(oid_name, sizeof oid_name, "hw.%s.stats", iface );
332         len = sizeof stats;
333         if (sysctlbyname(oid_name, stats, &len, NULL, 0) == -1)
334                 err(EX_OSERR, "Can't retrieve statistics");
335
336         for (stt = tbl; stt->index != 0; stt++)
337                 printf("%-60s[%u]\n", stt->desc, stats[stt->index]);
338 }
339