Constify all users of dktypenames to unbreak buildworld.
[dragonfly.git] / usr.sbin / iwicontrol / iwicontrol.c
blob78fae479473cd478aa3ba8a1b94363eb8441f784
1 /*
2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>.
4 * Copyright (c) 2004, 2005
5 * Andrew Atrens <atrens@nortelnetworks.com>.
7 * All rights reserved.
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.
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.
31 * $DragonFly: src/usr.sbin/iwicontrol/iwicontrol.c,v 1.1 2005/03/06 05:02:03 dillon Exp $
34 #include <sys/cdefs.h>
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>
43 #include <net/if.h>
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>
54 #define SIOCSLOADFW _IOW('i', 137, struct ifreq)
55 #define SIOCSLOADIBSSFW _IOW('i', 138, struct ifreq)
56 #define SIOCSKILLFW _IOW('i', 139, struct ifreq)
58 struct firmware {
59 char *boot;
60 int boot_size;
61 char *ucode;
62 int ucode_size;
63 char *main;
64 int main_size;
67 struct header {
68 u_int32_t version;
69 u_int32_t mode;
70 } __attribute__((__packed__));
72 static void usage(void);
73 static int do_req(const char *, unsigned long, void *);
74 static void mmap_file(const char *, char **, size_t *);
75 static void load_firmware(const char *, const char *, const char *);
76 static void kill_firmware(const char *);
77 static void get_radio_state(const char *);
78 static void get_statistics(const char *);
79 static void dump_debug(const char *);
81 int
82 main(int argc, char **argv)
84 int ch;
85 const char *iface = NULL, *mode = "bss", *path = NULL;
86 int noflag = 1, kflag = 0, rflag = 0, dflag = 0;
88 if (argc > 1 && argv[1][0] != '-') {
89 iface = argv[1];
90 optind++;
93 while ((ch = getopt(argc, argv, "d:i:km:rD")) != -1) {
94 if (ch != 'i')
95 noflag = 0;
97 switch (ch) {
98 case 'd':
99 path = optarg;
100 break;
102 case 'i':
103 iface = optarg;
104 break;
106 case 'k':
107 kflag = 1;
108 break;
110 case 'm':
111 mode = optarg;
112 break;
114 case 'r':
115 rflag = 1;
116 break;
118 case 'D':
119 dflag = 1;
120 break;
122 default:
123 usage();
127 if (iface == NULL)
128 usage();
130 if (kflag && (path != NULL || rflag))
131 usage();
133 if (kflag)
134 kill_firmware(iface);
136 if (path != NULL)
137 load_firmware(iface, path, mode);
139 if (rflag)
140 get_radio_state(iface);
142 if (dflag)
143 dump_debug(iface);
145 if (noflag)
146 get_statistics(iface);
148 return EX_OK;
151 extern char *__progname;
153 static void
154 usage(void)
156 (void)fprintf(stderr, "usage: %s iface\n"
157 "\t%s iface -d path [-m bss|ibss]\n"
158 "\t%s iface -k\n"
159 "\t%s iface -r\n", __progname, __progname, __progname,
160 __progname);
162 exit(EX_USAGE);
165 static int
166 do_req(const char *iface, unsigned long req, void *data)
168 int s;
169 struct ifreq ifr;
170 int error;
172 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
173 err(EX_OSERR, "Can't create socket");
175 (void)memset(&ifr, 0, sizeof ifr);
176 (void)strncpy(ifr.ifr_name, iface, sizeof ifr.ifr_name);
177 ifr.ifr_data = data;
178 error = ioctl(s, req, &ifr);
180 (void)close(s);
182 return error;
185 static void
186 mmap_file(const char *filename, char **addr, size_t *len)
188 int fd;
189 struct stat st;
191 if ((fd = open(filename, O_RDONLY)) == -1)
192 err(EX_OSERR, "%s", filename);
194 if (fstat(fd, &st) == -1)
195 err(EX_OSERR, "Unable to stat %s", filename);
197 *len = st.st_size;
199 if ((*addr = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
200 err(EX_OSERR, "Can't map %s into memory", filename);
202 *addr += sizeof (struct header);
203 *len -= sizeof (struct header);
205 (void)close(fd);
208 static void
209 load_firmware(const char *iface, const char *path, const char *mode)
211 char filename[FILENAME_MAX];
212 struct firmware fw;
214 (void)snprintf(filename, sizeof filename, "%s/iwi-boot.fw", path);
215 mmap_file(filename, &fw.boot, &fw.boot_size);
217 (void)snprintf(filename, sizeof filename, "%s/iwi-ucode-%s.fw", path,
218 mode);
219 mmap_file(filename, &fw.ucode, &fw.ucode_size);
221 (void)snprintf(filename, sizeof filename, "%s/iwi-%s.fw", path, mode);
222 mmap_file(filename, &fw.main, &fw.main_size);
224 if (do_req(iface, strstr(mode,"ibss")
225 ? SIOCSLOADIBSSFW : SIOCSLOADFW, &fw) == -1)
226 err(EX_OSERR, "Can't load firmware to driver");
229 static void
230 kill_firmware(const char *iface)
232 if (do_req(iface, SIOCSKILLFW, NULL) == -1)
233 err(EX_OSERR, "Can't kill firmware");
236 static void
237 dump_debug(const char *iface)
239 int dump = 1, len = sizeof dump;
240 char oid_name[128];
241 snprintf(oid_name, sizeof oid_name, "hw.%s.firmware_logs", iface );
242 if (sysctlbyname(oid_name, 0, 0, &dump, len) == -1)
243 err(EX_OSERR, "Can't dump firmware logs");
244 (void)printf("All firmware logs dumped.\n");
248 static void
249 get_radio_state(const char *iface)
251 int radio, len;
252 char oid_name[128];
254 snprintf(oid_name, sizeof oid_name, "hw.%s.radio", iface );
255 len = sizeof radio;
256 if (sysctlbyname(oid_name, &radio, &len, NULL, 0) == -1)
257 err(EX_OSERR, "Can't get radio transmitter state");
259 (void)printf("Radio is %s\n", radio ? "ON" : "OFF");
262 struct statistic {
263 int index;
264 const char *desc;
267 static const struct statistic tbl[] = {
268 { 1, "Current transmission rate" },
269 { 2, "Fragmentation threshold" },
270 { 3, "RTS threshold" },
271 { 4, "Number of frames submitted for transfer" },
272 { 5, "Number of frames transmitted" },
273 { 6, "Number of unicast frames transmitted" },
274 { 7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
275 { 8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
276 { 9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
277 { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
279 { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
280 { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
281 { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
282 { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
283 { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
284 { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
285 { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
286 { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
287 { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
288 { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
289 { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
290 { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
291 { 31, "Number of multicast frames transmitted" },
292 { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
293 { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
294 { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
295 { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
297 { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
298 { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
299 { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
300 { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
301 { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
302 { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
303 { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
304 { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
305 { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
306 { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
307 { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
308 { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
309 { 56, "Number of transmission retries" },
310 { 57, "Number of transmission failures" },
311 { 58, "Number of frames with a bad CRC received" },
313 { 61, "Number of full scans" },
314 { 62, "Number of partial scans" },
316 { 64, "Number of bytes transmitted" },
317 { 65, "Current RSSI" },
318 { 66, "Number of beacons received" },
319 { 67, "Number of beacons missed" },
321 { 0, NULL }
324 static void
325 get_statistics(const char *iface)
327 static u_int32_t stats[256];
328 char oid_name[128];
329 const struct statistic *stt;
330 int len;
332 snprintf(oid_name, sizeof oid_name, "hw.%s.stats", iface );
333 len = sizeof stats;
334 if (sysctlbyname(oid_name, stats, &len, NULL, 0) == -1)
335 err(EX_OSERR, "Can't retrieve statistics");
337 for (stt = tbl; stt->index != 0; stt++)
338 (void)printf("%-60s[%u]\n", stt->desc, stats[stt->index]);