xref: /freebsd/sys/dev/bwn/if_bwn_util.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
17780dd71SAdrian Chadd /*-
27780dd71SAdrian Chadd  * Copyright (c) 2009-2010 Weongyo Jeong <weongyo@freebsd.org>
37780dd71SAdrian Chadd  * All rights reserved.
47780dd71SAdrian Chadd  *
57780dd71SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
67780dd71SAdrian Chadd  * modification, are permitted provided that the following conditions
77780dd71SAdrian Chadd  * are met:
87780dd71SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
97780dd71SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
107780dd71SAdrian Chadd  *    without modification.
117780dd71SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127780dd71SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
137780dd71SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
147780dd71SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
157780dd71SAdrian Chadd  *
167780dd71SAdrian Chadd  * NO WARRANTY
177780dd71SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187780dd71SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197780dd71SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
207780dd71SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
217780dd71SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
227780dd71SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
237780dd71SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
247780dd71SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
257780dd71SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
267780dd71SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
277780dd71SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
287780dd71SAdrian Chadd  */
297780dd71SAdrian Chadd 
307780dd71SAdrian Chadd #include <sys/cdefs.h>
317780dd71SAdrian Chadd /*
327780dd71SAdrian Chadd  * The Broadcom Wireless LAN controller driver.
337780dd71SAdrian Chadd  */
347780dd71SAdrian Chadd 
35*5025b8d5SAdrian Chadd #include "opt_bwn.h"
36*5025b8d5SAdrian Chadd #include "opt_wlan.h"
37*5025b8d5SAdrian Chadd 
387780dd71SAdrian Chadd #include <sys/param.h>
397780dd71SAdrian Chadd #include <sys/systm.h>
407780dd71SAdrian Chadd #include <sys/kernel.h>
417780dd71SAdrian Chadd #include <sys/malloc.h>
427780dd71SAdrian Chadd #include <sys/module.h>
437780dd71SAdrian Chadd #include <sys/endian.h>
447780dd71SAdrian Chadd #include <sys/errno.h>
457780dd71SAdrian Chadd #include <sys/firmware.h>
467780dd71SAdrian Chadd #include <sys/lock.h>
477780dd71SAdrian Chadd #include <sys/mutex.h>
487780dd71SAdrian Chadd #include <machine/bus.h>
497780dd71SAdrian Chadd #include <machine/resource.h>
507780dd71SAdrian Chadd #include <sys/bus.h>
517780dd71SAdrian Chadd #include <sys/rman.h>
527780dd71SAdrian Chadd #include <sys/socket.h>
537780dd71SAdrian Chadd #include <sys/sockio.h>
547780dd71SAdrian Chadd 
557780dd71SAdrian Chadd #include <net/ethernet.h>
567780dd71SAdrian Chadd #include <net/if.h>
577780dd71SAdrian Chadd #include <net/if_var.h>
587780dd71SAdrian Chadd #include <net/if_arp.h>
597780dd71SAdrian Chadd #include <net/if_dl.h>
607780dd71SAdrian Chadd #include <net/if_llc.h>
617780dd71SAdrian Chadd #include <net/if_media.h>
627780dd71SAdrian Chadd #include <net/if_types.h>
637780dd71SAdrian Chadd 
647780dd71SAdrian Chadd #include <dev/pci/pcivar.h>
657780dd71SAdrian Chadd #include <dev/pci/pcireg.h>
667780dd71SAdrian Chadd 
677780dd71SAdrian Chadd #include <net80211/ieee80211_var.h>
687780dd71SAdrian Chadd #include <net80211/ieee80211_radiotap.h>
697780dd71SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
707780dd71SAdrian Chadd #include <net80211/ieee80211_phy.h>
717780dd71SAdrian Chadd #include <net80211/ieee80211_ratectl.h>
727780dd71SAdrian Chadd 
737780dd71SAdrian Chadd #include <dev/bwn/if_bwnreg.h>
747780dd71SAdrian Chadd #include <dev/bwn/if_bwnvar.h>
757780dd71SAdrian Chadd 
767780dd71SAdrian Chadd #include <dev/bwn/if_bwn_debug.h>
777780dd71SAdrian Chadd #include <dev/bwn/if_bwn_misc.h>
787780dd71SAdrian Chadd #include <dev/bwn/if_bwn_util.h>
797780dd71SAdrian Chadd 
807780dd71SAdrian Chadd unsigned int
bwn_sqrt(struct bwn_mac * mac,unsigned int x)817780dd71SAdrian Chadd bwn_sqrt(struct bwn_mac *mac, unsigned int x)
827780dd71SAdrian Chadd {
837780dd71SAdrian Chadd 	/* Table holding (10 * sqrt(x)) for x between 1 and 256. */
847780dd71SAdrian Chadd 	static uint8_t sqrt_table[256] = {
857780dd71SAdrian Chadd 		10, 14, 17, 20, 22, 24, 26, 28,
867780dd71SAdrian Chadd 		30, 31, 33, 34, 36, 37, 38, 40,
877780dd71SAdrian Chadd 		41, 42, 43, 44, 45, 46, 47, 48,
887780dd71SAdrian Chadd 		50, 50, 51, 52, 53, 54, 55, 56,
897780dd71SAdrian Chadd 		57, 58, 59, 60, 60, 61, 62, 63,
907780dd71SAdrian Chadd 		64, 64, 65, 66, 67, 67, 68, 69,
917780dd71SAdrian Chadd 		70, 70, 71, 72, 72, 73, 74, 74,
927780dd71SAdrian Chadd 		75, 76, 76, 77, 78, 78, 79, 80,
937780dd71SAdrian Chadd 		80, 81, 81, 82, 83, 83, 84, 84,
947780dd71SAdrian Chadd 		85, 86, 86, 87, 87, 88, 88, 89,
957780dd71SAdrian Chadd 		90, 90, 91, 91, 92, 92, 93, 93,
967780dd71SAdrian Chadd 		94, 94, 95, 95, 96, 96, 97, 97,
977780dd71SAdrian Chadd 		98, 98, 99, 100, 100, 100, 101, 101,
987780dd71SAdrian Chadd 		102, 102, 103, 103, 104, 104, 105, 105,
997780dd71SAdrian Chadd 		106, 106, 107, 107, 108, 108, 109, 109,
1007780dd71SAdrian Chadd 		110, 110, 110, 111, 111, 112, 112, 113,
1017780dd71SAdrian Chadd 		113, 114, 114, 114, 115, 115, 116, 116,
1027780dd71SAdrian Chadd 		117, 117, 117, 118, 118, 119, 119, 120,
1037780dd71SAdrian Chadd 		120, 120, 121, 121, 122, 122, 122, 123,
1047780dd71SAdrian Chadd 		123, 124, 124, 124, 125, 125, 126, 126,
1057780dd71SAdrian Chadd 		126, 127, 127, 128, 128, 128, 129, 129,
1067780dd71SAdrian Chadd 		130, 130, 130, 131, 131, 131, 132, 132,
1077780dd71SAdrian Chadd 		133, 133, 133, 134, 134, 134, 135, 135,
1087780dd71SAdrian Chadd 		136, 136, 136, 137, 137, 137, 138, 138,
1097780dd71SAdrian Chadd 		138, 139, 139, 140, 140, 140, 141, 141,
1107780dd71SAdrian Chadd 		141, 142, 142, 142, 143, 143, 143, 144,
1117780dd71SAdrian Chadd 		144, 144, 145, 145, 145, 146, 146, 146,
1127780dd71SAdrian Chadd 		147, 147, 147, 148, 148, 148, 149, 149,
1137780dd71SAdrian Chadd 		150, 150, 150, 150, 151, 151, 151, 152,
1147780dd71SAdrian Chadd 		152, 152, 153, 153, 153, 154, 154, 154,
1157780dd71SAdrian Chadd 		155, 155, 155, 156, 156, 156, 157, 157,
1167780dd71SAdrian Chadd 		157, 158, 158, 158, 159, 159, 159, 160
1177780dd71SAdrian Chadd 	};
1187780dd71SAdrian Chadd 
1197780dd71SAdrian Chadd 	if (x == 0)
1207780dd71SAdrian Chadd 		return (0);
1217780dd71SAdrian Chadd 	if (x >= 256) {
1227780dd71SAdrian Chadd 		unsigned int tmp;
1237780dd71SAdrian Chadd 
1247780dd71SAdrian Chadd 		for (tmp = 0; x >= (2 * tmp) + 1; x -= (2 * tmp++) + 1)
1257780dd71SAdrian Chadd 			/* do nothing */ ;
1267780dd71SAdrian Chadd 		return (tmp);
1277780dd71SAdrian Chadd 	}
1287780dd71SAdrian Chadd 	return (sqrt_table[x - 1] / 10);
1297780dd71SAdrian Chadd }
130