block: fix warning with calling smp_processor_id() in preemptible section
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / antsel.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/slab.h>
18 #include <net/mac80211.h>
19
20 #include "types.h"
21 #include "bmac.h"
22 #include "main.h"
23 #include "phy_shim.h"
24 #include "antsel.h"
25
26 #define ANT_SELCFG_AUTO         0x80    /* bit indicates antenna sel AUTO */
27 #define ANT_SELCFG_MASK         0x33    /* antenna configuration mask */
28 #define ANT_SELCFG_TX_UNICAST   0       /* unicast tx antenna configuration */
29 #define ANT_SELCFG_RX_UNICAST   1       /* unicast rx antenna configuration */
30 #define ANT_SELCFG_TX_DEF       2       /* default tx antenna configuration */
31 #define ANT_SELCFG_RX_DEF       3       /* default rx antenna configuration */
32
33 /* useful macros */
34 #define BRCMS_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
35 #define BRCMS_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
36 #define BRCMS_ANTIDX_11N(ant)   (((BRCMS_ANTSEL_11N_0(ant)) << 2) +\
37                                 (BRCMS_ANTSEL_11N_1(ant)))
38 #define BRCMS_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
39 #define BRCMS_ANTSEL_11N(ant)   ((ant) & ANT_SELCFG_MASK)
40
41 /* antenna switch */
42 /* defines for no boardlevel antenna diversity */
43 #define ANT_SELCFG_DEF_2x2      0x01    /* default antenna configuration */
44
45 /* 2x3 antdiv defines and tables for GPIO communication */
46 #define ANT_SELCFG_NUM_2x3      3
47 #define ANT_SELCFG_DEF_2x3      0x01    /* default antenna configuration */
48
49 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
50 #define ANT_SELCFG_NUM_2x4      4
51 #define ANT_SELCFG_DEF_2x4      0x02    /* default antenna configuration */
52
53 /* static functions */
54 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
55                                  struct brcms_antselcfg *antsel);
56 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id);
57 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
58 static void brcms_c_antsel_init_cfg(struct antsel_info *asi,
59                                 struct brcms_antselcfg *antsel,
60                                 bool auto_sel);
61
62 const u16 mimo_2x4_div_antselpat_tbl[] = {
63         0, 0, 0x9, 0xa,         /* ant0: 0 ant1: 2,3 */
64         0, 0, 0x5, 0x6,         /* ant0: 1 ant1: 2,3 */
65         0, 0, 0, 0,             /* n.a.              */
66         0, 0, 0, 0              /* n.a.              */
67 };
68
69 const u8 mimo_2x4_div_antselid_tbl[16] = {
70         0, 0, 0, 0, 0, 2, 3, 0,
71         0, 0, 1, 0, 0, 0, 0, 0  /* pat to antselid */
72 };
73
74 const u16 mimo_2x3_div_antselpat_tbl[] = {
75         16, 0, 1, 16,           /* ant0: 0 ant1: 1,2 */
76         16, 16, 16, 16,         /* n.a.              */
77         16, 2, 16, 16,          /* ant0: 2 ant1: 1   */
78         16, 16, 16, 16          /* n.a.              */
79 };
80
81 const u8 mimo_2x3_div_antselid_tbl[16] = {
82         0, 1, 2, 0, 0, 0, 0, 0,
83         0, 0, 0, 0, 0, 0, 0, 0  /* pat to antselid */
84 };
85
86 struct antsel_info *brcms_c_antsel_attach(struct brcms_c_info *wlc)
87 {
88         struct antsel_info *asi;
89
90         asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
91         if (!asi) {
92                 wiphy_err(wlc->wiphy, "wl%d: brcms_c_antsel_attach: out of "
93                           "mem\n", wlc->pub->unit);
94                 return NULL;
95         }
96
97         asi->wlc = wlc;
98         asi->pub = wlc->pub;
99         asi->antsel_type = ANTSEL_NA;
100         asi->antsel_avail = false;
101         asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
102
103         if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
104                 switch (asi->antsel_antswitch) {
105                 case ANTSWITCH_TYPE_1:
106                 case ANTSWITCH_TYPE_2:
107                 case ANTSWITCH_TYPE_3:
108                         /* 4321/2 board with 2x3 switch logic */
109                         asi->antsel_type = ANTSEL_2x3;
110                         /* Antenna selection availability */
111                         if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
112                             ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
113                                 asi->antsel_avail = true;
114                         } else
115                             if (((u16) getintvar(asi->pub->vars, "aa2g") ==
116                                  3)
117                                 || ((u16) getintvar(asi->pub->vars, "aa5g")
118                                     == 3)) {
119                                 asi->antsel_avail = false;
120                         } else {
121                                 asi->antsel_avail = false;
122                                 wiphy_err(wlc->wiphy, "antsel_attach: 2o3 "
123                                           "board cfg invalid\n");
124                         }
125                         break;
126                 default:
127                         break;
128                 }
129         } else if ((asi->pub->sromrev == 4) &&
130                    ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
131                    ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
132                 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
133                 asi->antsel_type = ANTSEL_2x3;
134                 asi->antsel_avail = true;
135         } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
136                 asi->antsel_type = ANTSEL_2x4;
137                 asi->antsel_avail = true;
138         }
139
140         /* Set the antenna selection type for the low driver */
141         brcms_b_antsel_type_set(wlc->hw, asi->antsel_type);
142
143         /* Init (auto/manual) antenna selection */
144         brcms_c_antsel_init_cfg(asi, &asi->antcfg_11n, true);
145         brcms_c_antsel_init_cfg(asi, &asi->antcfg_cur, true);
146
147         return asi;
148 }
149
150 void brcms_c_antsel_detach(struct antsel_info *asi)
151 {
152         kfree(asi);
153 }
154
155 void brcms_c_antsel_init(struct antsel_info *asi)
156 {
157         if ((asi->antsel_type == ANTSEL_2x3) ||
158             (asi->antsel_type == ANTSEL_2x4))
159                 brcms_c_antsel_cfgupd(asi, &asi->antcfg_11n);
160 }
161
162 /* boardlevel antenna selection: init antenna selection structure */
163 static void
164 brcms_c_antsel_init_cfg(struct antsel_info *asi, struct brcms_antselcfg *antsel,
165                     bool auto_sel)
166 {
167         if (asi->antsel_type == ANTSEL_2x3) {
168                 u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
169                     ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
170                 antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
171                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
172                 antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
173                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
174                 antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
175
176         } else if (asi->antsel_type == ANTSEL_2x4) {
177
178                 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
179                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
180                 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
181                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
182                 antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
183
184         } else {                /* no antenna selection available */
185
186                 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
187                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
188                 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
189                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
190                 antsel->num_antcfg = 0;
191         }
192 }
193
194 void
195 brcms_c_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
196                       u8 antselid, u8 fbantselid, u8 *antcfg,
197                       u8 *fbantcfg)
198 {
199         u8 ant;
200
201         /* if use default, assign it and return */
202         if (usedef) {
203                 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
204                 *fbantcfg = *antcfg;
205                 return;
206         }
207
208         if (!sel) {
209                 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
210                 *fbantcfg = *antcfg;
211
212         } else {
213                 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
214                 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
215                         *antcfg = brcms_c_antsel_id2antcfg(asi, antselid);
216                         *fbantcfg = brcms_c_antsel_id2antcfg(asi, fbantselid);
217                 } else {
218                         *antcfg =
219                             asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
220                         *fbantcfg = *antcfg;
221                 }
222         }
223         return;
224 }
225
226 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
227 u8 brcms_c_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
228 {
229         u8 antselid = 0;
230
231         if (asi->antsel_type == ANTSEL_2x4) {
232                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
233                 antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
234                 return antselid;
235
236         } else if (asi->antsel_type == ANTSEL_2x3) {
237                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
238                 antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
239                 return antselid;
240         }
241
242         return antselid;
243 }
244
245 /* boardlevel antenna selection: convert id to ant_cfg */
246 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id)
247 {
248         u8 antcfg = ANT_SELCFG_DEF_2x2;
249
250         if (asi->antsel_type == ANTSEL_2x4) {
251                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
252                 antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
253                 return antcfg;
254
255         } else if (asi->antsel_type == ANTSEL_2x3) {
256                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
257                 antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
258                 return antcfg;
259         }
260
261         return antcfg;
262 }
263
264 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
265 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
266 {
267         u8 idx = BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg));
268         u16 mimo_antsel = 0;
269
270         if (asi->antsel_type == ANTSEL_2x4) {
271                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
272                 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
273                 return mimo_antsel;
274
275         } else if (asi->antsel_type == ANTSEL_2x3) {
276                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
277                 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
278                 return mimo_antsel;
279         }
280
281         return mimo_antsel;
282 }
283
284 /* boardlevel antenna selection: ucode interface control */
285 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
286                                  struct brcms_antselcfg *antsel)
287 {
288         struct brcms_c_info *wlc = asi->wlc;
289         u8 ant_cfg;
290         u16 mimo_antsel;
291
292         /* 1) Update TX antconfig for all frames that are not unicast data
293          *    (aka default TX)
294          */
295         ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
296         mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
297         brcms_c_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
298         /* Update driver stats for currently selected default tx/rx antenna config */
299         asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
300
301         /* 2) Update RX antconfig for all frames that are not unicast data
302          *    (aka default RX)
303          */
304         ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
305         mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
306         brcms_c_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
307         /* Update driver stats for currently selected default tx/rx antenna config */
308         asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
309
310         return 0;
311 }