xref: /dragonfly/sys/dev/netif/bwn/bwn/if_bwn_util.c (revision 91dc43dd)
1 /*-
2  * Copyright (c) 2009-2010 Weongyo Jeong <weongyo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD: head/sys/dev/bwn/if_bwn_util.c 299751 2016-05-14 19:52:04Z adrian $
30  */
31 
32 /*
33  * The Broadcom Wireless LAN controller driver.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/endian.h>
42 #include <sys/errno.h>
43 #include <sys/firmware.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #if !defined(__DragonFly__)
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #endif
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_llc.h>
61 #include <net/if_media.h>
62 #include <net/if_types.h>
63 
64 #if defined(__DragonFly__)
65 #include <bus/pci/pcivar.h>
66 #include <bus/pci/pcireg.h>
67 #include <dev/netif/bwn/siba/siba_ids.h>
68 #include <dev/netif/bwn/siba/sibareg.h>
69 #include <dev/netif/bwn/siba/sibavar.h>
70 #else
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/siba/siba_ids.h>
74 #include <dev/siba/sibareg.h>
75 #include <dev/siba/sibavar.h>
76 #endif
77 
78 #if defined(__DragonFly__)
79 #include <netproto/802_11/ieee80211_var.h>
80 #include <netproto/802_11/ieee80211_radiotap.h>
81 #include <netproto/802_11/ieee80211_regdomain.h>
82 #include <netproto/802_11/ieee80211_phy.h>
83 #include <netproto/802_11/ieee80211_ratectl.h>
84 #else
85 #include <net80211/ieee80211_var.h>
86 #include <net80211/ieee80211_radiotap.h>
87 #include <net80211/ieee80211_regdomain.h>
88 #include <net80211/ieee80211_phy.h>
89 #include <net80211/ieee80211_ratectl.h>
90 #endif
91 
92 #if defined(__DragonFly__)
93 #include "if_bwnreg.h"
94 #include "if_bwnvar.h"
95 #else
96 #include <dev/bwn/if_bwnreg.h>
97 #include <dev/bwn/if_bwnvar.h>
98 #endif
99 
100 #if defined(__DragonFly__)
101 #include "if_bwn_chipid.h"
102 #include "if_bwn_debug.h"
103 #include "if_bwn_misc.h"
104 #include "if_bwn_util.h"
105 #else
106 #include <dev/bwn/if_bwn_chipid.h>
107 #include <dev/bwn/if_bwn_debug.h>
108 #include <dev/bwn/if_bwn_misc.h>
109 #include <dev/bwn/if_bwn_util.h>
110 #endif
111 
112 unsigned int
113 bwn_sqrt(struct bwn_mac *mac, unsigned int x)
114 {
115 	/* Table holding (10 * sqrt(x)) for x between 1 and 256. */
116 	static uint8_t sqrt_table[256] = {
117 		10, 14, 17, 20, 22, 24, 26, 28,
118 		30, 31, 33, 34, 36, 37, 38, 40,
119 		41, 42, 43, 44, 45, 46, 47, 48,
120 		50, 50, 51, 52, 53, 54, 55, 56,
121 		57, 58, 59, 60, 60, 61, 62, 63,
122 		64, 64, 65, 66, 67, 67, 68, 69,
123 		70, 70, 71, 72, 72, 73, 74, 74,
124 		75, 76, 76, 77, 78, 78, 79, 80,
125 		80, 81, 81, 82, 83, 83, 84, 84,
126 		85, 86, 86, 87, 87, 88, 88, 89,
127 		90, 90, 91, 91, 92, 92, 93, 93,
128 		94, 94, 95, 95, 96, 96, 97, 97,
129 		98, 98, 99, 100, 100, 100, 101, 101,
130 		102, 102, 103, 103, 104, 104, 105, 105,
131 		106, 106, 107, 107, 108, 108, 109, 109,
132 		110, 110, 110, 111, 111, 112, 112, 113,
133 		113, 114, 114, 114, 115, 115, 116, 116,
134 		117, 117, 117, 118, 118, 119, 119, 120,
135 		120, 120, 121, 121, 122, 122, 122, 123,
136 		123, 124, 124, 124, 125, 125, 126, 126,
137 		126, 127, 127, 128, 128, 128, 129, 129,
138 		130, 130, 130, 131, 131, 131, 132, 132,
139 		133, 133, 133, 134, 134, 134, 135, 135,
140 		136, 136, 136, 137, 137, 137, 138, 138,
141 		138, 139, 139, 140, 140, 140, 141, 141,
142 		141, 142, 142, 142, 143, 143, 143, 144,
143 		144, 144, 145, 145, 145, 146, 146, 146,
144 		147, 147, 147, 148, 148, 148, 149, 149,
145 		150, 150, 150, 150, 151, 151, 151, 152,
146 		152, 152, 153, 153, 153, 154, 154, 154,
147 		155, 155, 155, 156, 156, 156, 157, 157,
148 		157, 158, 158, 158, 159, 159, 159, 160
149 	};
150 
151 	if (x == 0)
152 		return (0);
153 	if (x >= 256) {
154 		unsigned int tmp;
155 
156 		for (tmp = 0; x >= (2 * tmp) + 1; x -= (2 * tmp++) + 1)
157 			/* do nothing */ ;
158 		return (tmp);
159 	}
160 	return (sqrt_table[x - 1] / 10);
161 }
162