2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>.
4 * Copyright (c) 2004, 2005
5 * Andrew Atrens <atrens@nortelnetworks.com>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
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
31 * $DragonFly: src/usr.sbin/iwicontrol/iwicontrol.c,v 1.2 2005/12/05 01:04:01 swildner Exp $
34 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
39 #include <sys/socket.h>
41 #include <sys/sysctl.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)
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 *);
82 main(int argc
, char **argv
)
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] != '-') {
93 while ((ch
= getopt(argc
, argv
, "d:i:km:rD")) != -1) {
130 if (kflag
&& (path
!= NULL
|| rflag
))
134 kill_firmware(iface
);
137 load_firmware(iface
, path
, mode
);
140 get_radio_state(iface
);
146 get_statistics(iface
);
151 extern char *__progname
;
156 fprintf(stderr
, "usage: %s iface\n"
157 "\t%s iface -d path [-m bss|ibss]\n"
159 "\t%s iface -r\n", __progname
, __progname
, __progname
,
166 do_req(const char *iface
, unsigned long req
, void *data
)
172 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
173 err(EX_OSERR
, "Can't create socket");
175 memset(&ifr
, 0, sizeof ifr
);
176 strncpy(ifr
.ifr_name
, iface
, sizeof ifr
.ifr_name
);
178 error
= ioctl(s
, req
, &ifr
);
186 mmap_file(const char *filename
, char **addr
, size_t *len
)
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
);
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
);
209 load_firmware(const char *iface
, const char *path
, const char *mode
)
211 char filename
[FILENAME_MAX
];
214 snprintf(filename
, sizeof filename
, "%s/iwi-boot.fw", path
);
215 mmap_file(filename
, &fw
.boot
, &fw
.boot_size
);
217 snprintf(filename
, sizeof filename
, "%s/iwi-ucode-%s.fw", path
,
219 mmap_file(filename
, &fw
.ucode
, &fw
.ucode_size
);
221 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");
230 kill_firmware(const char *iface
)
232 if (do_req(iface
, SIOCSKILLFW
, NULL
) == -1)
233 err(EX_OSERR
, "Can't kill firmware");
237 dump_debug(const char *iface
)
239 int dump
= 1, len
= sizeof dump
;
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 printf("All firmware logs dumped.\n");
249 get_radio_state(const char *iface
)
254 snprintf(oid_name
, sizeof oid_name
, "hw.%s.radio", iface
);
256 if (sysctlbyname(oid_name
, &radio
, &len
, NULL
, 0) == -1)
257 err(EX_OSERR
, "Can't get radio transmitter state");
259 printf("Radio is %s\n", radio
? "ON" : "OFF");
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" },
325 get_statistics(const char *iface
)
327 static u_int32_t stats
[256];
329 const struct statistic
*stt
;
332 snprintf(oid_name
, sizeof oid_name
, "hw.%s.stats", iface
);
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 printf("%-60s[%u]\n", stt
->desc
, stats
[stt
->index
]);