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.3 2006/05/18 13:51:45 sephe 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 SIOCSKILLFW _IOW('i', 138, struct ifreq)
69 } __attribute__((__packed__
));
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 *);
81 main(int argc
, char **argv
)
84 const char *iface
= NULL
, *mode
= "bss", *path
= NULL
;
85 int noflag
= 1, kflag
= 0, rflag
= 0, dflag
= 0;
87 if (argc
> 1 && argv
[1][0] != '-') {
92 while ((ch
= getopt(argc
, argv
, "d:i:km:rD")) != -1) {
129 if (kflag
&& (path
!= NULL
|| rflag
))
133 kill_firmware(iface
);
136 load_firmware(iface
, path
, mode
);
139 get_radio_state(iface
);
145 get_statistics(iface
);
150 extern char *__progname
;
155 fprintf(stderr
, "usage: %s iface\n"
156 "\t%s iface -d path [-m bss|ibss]\n"
158 "\t%s iface -r\n", __progname
, __progname
, __progname
,
165 do_req(const char *iface
, unsigned long req
, void *data
)
171 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
172 err(EX_OSERR
, "Can't create socket");
174 memset(&ifr
, 0, sizeof ifr
);
175 strncpy(ifr
.ifr_name
, iface
, sizeof ifr
.ifr_name
);
177 error
= ioctl(s
, req
, &ifr
);
185 mmap_file(const char *filename
, char **addr
, size_t *len
)
190 if ((fd
= open(filename
, O_RDONLY
)) == -1)
191 err(EX_OSERR
, "%s", filename
);
193 if (fstat(fd
, &st
) == -1)
194 err(EX_OSERR
, "Unable to stat %s", filename
);
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
);
201 *addr
+= sizeof (struct header
);
202 *len
-= sizeof (struct header
);
208 load_firmware(const char *iface
, const char *path
, const char *mode
)
210 char filename
[FILENAME_MAX
];
213 snprintf(filename
, sizeof filename
, "%s/iwi-boot.fw", path
);
214 mmap_file(filename
, &fw
.boot
, &fw
.boot_size
);
216 snprintf(filename
, sizeof filename
, "%s/iwi-ucode-%s.fw", path
,
218 mmap_file(filename
, &fw
.ucode
, &fw
.ucode_size
);
220 snprintf(filename
, sizeof filename
, "%s/iwi-%s.fw", path
, mode
);
221 mmap_file(filename
, &fw
.main
, &fw
.main_size
);
223 if (do_req(iface
, SIOCSLOADFW
, &fw
) == -1)
224 err(EX_OSERR
, "Can't load firmware to driver");
228 kill_firmware(const char *iface
)
230 if (do_req(iface
, SIOCSKILLFW
, NULL
) == -1)
231 err(EX_OSERR
, "Can't kill firmware");
235 dump_debug(const char *iface
)
237 int dump
= 1, len
= sizeof dump
;
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");
247 get_radio_state(const char *iface
)
252 snprintf(oid_name
, sizeof oid_name
, "hw.%s.radio", iface
);
254 if (sysctlbyname(oid_name
, &radio
, &len
, NULL
, 0) == -1)
255 err(EX_OSERR
, "Can't get radio transmitter state");
257 printf("Radio is %s\n", radio
? "ON" : "OFF");
265 static const struct statistic tbl
[] = {
266 { 1, "Current transmission rate" },
267 { 2, "Fragmentation threshold" },
268 { 3, "RTS threshold" },
269 { 4, "Number of frames submitted for transfer" },
270 { 5, "Number of frames transmitted" },
271 { 6, "Number of unicast frames transmitted" },
272 { 7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
273 { 8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
274 { 9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
275 { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
277 { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
278 { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
279 { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
280 { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
281 { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
282 { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
283 { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
284 { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
285 { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
286 { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
287 { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
288 { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
289 { 31, "Number of multicast frames transmitted" },
290 { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
291 { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
292 { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
293 { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
295 { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
296 { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
297 { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
298 { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
299 { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
300 { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
301 { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
302 { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
303 { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
304 { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
305 { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
306 { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
307 { 56, "Number of transmission retries" },
308 { 57, "Number of transmission failures" },
309 { 58, "Number of frames with a bad CRC received" },
311 { 61, "Number of full scans" },
312 { 62, "Number of partial scans" },
314 { 64, "Number of bytes transmitted" },
315 { 65, "Current RSSI" },
316 { 66, "Number of beacons received" },
317 { 67, "Number of beacons missed" },
323 get_statistics(const char *iface
)
325 static u_int32_t stats
[256];
327 const struct statistic
*stt
;
330 snprintf(oid_name
, sizeof oid_name
, "hw.%s.stats", iface
);
332 if (sysctlbyname(oid_name
, stats
, &len
, NULL
, 0) == -1)
333 err(EX_OSERR
, "Can't retrieve statistics");
335 for (stt
= tbl
; stt
->index
!= 0; stt
++)
336 printf("%-60s[%u]\n", stt
->desc
, stats
[stt
->index
]);