]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmfmac/wl_cfg80211.c
staging: brcm80211: move sdio related suspend/resume code to bus interface layer
[mv-sheeva.git] / drivers / staging / brcm80211 / brcmfmac / wl_cfg80211.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/kernel.h>
18 #include <linux/if_arp.h>
19 #include <linux/sched.h>
20 #include <linux/kthread.h>
21 #include <linux/netdevice.h>
22 #include <linux/sched.h>
23 #include <linux/etherdevice.h>
24 #include <linux/wireless.h>
25 #include <linux/ieee80211.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/firmware.h>
28 #include <linux/uaccess.h>
29 #include <net/cfg80211.h>
30 #include <net/rtnetlink.h>
31
32 #include <brcmu_utils.h>
33 #include <defs.h>
34 #include <brcmu_wifi.h>
35 #include "dngl_stats.h"
36 #include "dhd.h"
37 #include "wl_cfg80211.h"
38
39 static struct sdio_func *cfg80211_sdio_func;
40 static struct wl_dev *wl_cfg80211_dev;
41 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
42
43 u32 brcmf_dbg_level = WL_DBG_ERR;
44
45 #define WL_4329_FW_FILE "brcm/bcm4329-fullmac-4.bin"
46 #define WL_4329_NVRAM_FILE "brcm/bcm4329-fullmac-4.txt"
47
48 /*
49 ** cfg80211_ops api/callback list
50 */
51 static s32 wl_cfg80211_change_iface(struct wiphy *wiphy,
52                                       struct net_device *ndev,
53                                       enum nl80211_iftype type, u32 *flags,
54                                       struct vif_params *params);
55 static s32 __wl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
56                                 struct cfg80211_scan_request *request,
57                                 struct cfg80211_ssid *this_ssid);
58 static s32 wl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
59                               struct cfg80211_scan_request *request);
60 static s32 wl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed);
61 static s32 wl_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
62                                    struct cfg80211_ibss_params *params);
63 static s32 wl_cfg80211_leave_ibss(struct wiphy *wiphy,
64                                     struct net_device *dev);
65 static s32 wl_cfg80211_get_station(struct wiphy *wiphy,
66                                      struct net_device *dev, u8 *mac,
67                                      struct station_info *sinfo);
68 static s32 wl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
69                                         struct net_device *dev, bool enabled,
70                                         s32 timeout);
71 static s32 wl_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
72                                           struct net_device *dev,
73                                           const u8 *addr,
74                                           const struct cfg80211_bitrate_mask
75                                           *mask);
76 static int wl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
77                                struct cfg80211_connect_params *sme);
78 static s32 wl_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
79                                     u16 reason_code);
80 static s32 wl_cfg80211_set_tx_power(struct wiphy *wiphy,
81                                       enum nl80211_tx_power_setting type,
82                                       s32 dbm);
83 static s32 wl_cfg80211_get_tx_power(struct wiphy *wiphy, s32 *dbm);
84 static s32 wl_cfg80211_config_default_key(struct wiphy *wiphy,
85                                           struct net_device *dev, u8 key_idx,
86                                           bool unicast, bool multicast);
87 static s32 wl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *dev,
88                                  u8 key_idx, bool pairwise, const u8 *mac_addr,
89                                  struct key_params *params);
90 static s32 wl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *dev,
91                                  u8 key_idx, bool pairwise, const u8 *mac_addr);
92 static s32 wl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *dev,
93                                  u8 key_idx, bool pairwise, const u8 *mac_addr,
94                                  void *cookie, void (*callback) (void *cookie,
95                                                                  struct
96                                                                  key_params *
97                                                                  params));
98 static s32 wl_cfg80211_config_default_mgmt_key(struct wiphy *wiphy,
99                                                  struct net_device *dev,
100                                                  u8 key_idx);
101 static s32 wl_cfg80211_resume(struct wiphy *wiphy);
102 static s32 wl_cfg80211_suspend(struct wiphy *wiphy,
103                                  struct cfg80211_wowlan *wow);
104 static s32 wl_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *dev,
105                                    struct cfg80211_pmksa *pmksa);
106 static s32 wl_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *dev,
107                                    struct cfg80211_pmksa *pmksa);
108 static s32 wl_cfg80211_flush_pmksa(struct wiphy *wiphy,
109                                      struct net_device *dev);
110 /*
111 ** event & event Q handlers for cfg80211 interfaces
112 */
113 static s32 wl_create_event_handler(struct wl_priv *wl);
114 static void wl_destroy_event_handler(struct wl_priv *wl);
115 static s32 wl_event_handler(void *data);
116 static void wl_init_eq(struct wl_priv *wl);
117 static void wl_flush_eq(struct wl_priv *wl);
118 static void wl_lock_eq(struct wl_priv *wl);
119 static void wl_unlock_eq(struct wl_priv *wl);
120 static void wl_init_eq_lock(struct wl_priv *wl);
121 static void wl_init_eloop_handler(struct wl_event_loop *el);
122 static struct wl_event_q *wl_deq_event(struct wl_priv *wl);
123 static s32 wl_enq_event(struct wl_priv *wl, u32 type,
124                           const brcmf_event_msg_t *msg, void *data);
125 static void wl_put_event(struct wl_event_q *e);
126 static void wl_wakeup_event(struct wl_priv *wl);
127 static s32 wl_notify_connect_status(struct wl_priv *wl,
128                                       struct net_device *ndev,
129                                       const brcmf_event_msg_t *e, void *data);
130 static s32 wl_notify_roaming_status(struct wl_priv *wl,
131                                       struct net_device *ndev,
132                                       const brcmf_event_msg_t *e, void *data);
133 static s32 wl_notify_scan_status(struct wl_priv *wl, struct net_device *ndev,
134                                    const brcmf_event_msg_t *e, void *data);
135 static s32 wl_bss_connect_done(struct wl_priv *wl, struct net_device *ndev,
136                                  const brcmf_event_msg_t *e, void *data,
137                                 bool completed);
138 static s32 wl_bss_roaming_done(struct wl_priv *wl, struct net_device *ndev,
139                                  const brcmf_event_msg_t *e, void *data);
140 static s32 wl_notify_mic_status(struct wl_priv *wl, struct net_device *ndev,
141                                   const brcmf_event_msg_t *e, void *data);
142
143 /*
144 ** register/deregister sdio function
145 */
146 struct sdio_func *wl_cfg80211_get_sdio_func(void);
147 static void wl_clear_sdio_func(void);
148
149 /*
150 ** ioctl utilites
151 */
152 static s32 wl_dev_bufvar_get(struct net_device *dev, s8 *name, s8 *buf,
153                                s32 buf_len);
154 static __used s32 wl_dev_bufvar_set(struct net_device *dev, s8 *name,
155                                       s8 *buf, s32 len);
156 static s32 wl_dev_intvar_set(struct net_device *dev, s8 *name, s32 val);
157 static s32 wl_dev_intvar_get(struct net_device *dev, s8 *name,
158                                s32 *retval);
159 static s32 wl_dev_ioctl(struct net_device *dev, u32 cmd, void *arg,
160                           u32 len);
161
162 /*
163 ** cfg80211 set_wiphy_params utilities
164 */
165 static s32 wl_set_frag(struct net_device *dev, u32 frag_threshold);
166 static s32 wl_set_rts(struct net_device *dev, u32 frag_threshold);
167 static s32 wl_set_retry(struct net_device *dev, u32 retry, bool l);
168
169 /*
170 ** wl profile utilities
171 */
172 static s32 wl_update_prof(struct wl_priv *wl, const brcmf_event_msg_t *e,
173                             void *data, s32 item);
174 static void *wl_read_prof(struct wl_priv *wl, s32 item);
175 static void wl_init_prof(struct wl_profile *prof);
176
177 /*
178 ** cfg80211 connect utilites
179 */
180 static s32 wl_set_wpa_version(struct net_device *dev,
181                         struct cfg80211_connect_params *sme);
182 static s32 wl_set_auth_type(struct net_device *dev,
183                         struct cfg80211_connect_params *sme);
184 static s32 wl_set_set_cipher(struct net_device *dev,
185                         struct cfg80211_connect_params *sme);
186 static s32 wl_set_key_mgmt(struct net_device *dev,
187                         struct cfg80211_connect_params *sme);
188 static s32 wl_set_set_sharedkey(struct net_device *dev,
189                         struct cfg80211_connect_params *sme);
190 static s32 wl_get_assoc_ies(struct wl_priv *wl);
191 static void wl_clear_assoc_ies(struct wl_priv *wl);
192 static void wl_ch_to_chanspec(int ch,
193         struct wl_join_params *join_params, size_t *join_params_size);
194
195 /*
196 ** information element utilities
197 */
198 static __used s32 wl_add_ie(struct wl_priv *wl, u8 t, u8 l, u8 *v);
199 static s32 wl_mode_to_nl80211_iftype(s32 mode);
200 static struct wireless_dev *wl_alloc_wdev(s32 sizeof_iface,
201                         struct device *dev);
202 static void wl_free_wdev(struct wl_priv *wl);
203 static s32 wl_inform_bss(struct wl_priv *wl);
204 static s32 wl_inform_single_bss(struct wl_priv *wl, struct wl_bss_info *bi);
205 static s32 wl_update_bss_info(struct wl_priv *wl);
206 static s32 wl_add_keyext(struct wiphy *wiphy, struct net_device *dev,
207                         u8 key_idx, const u8 *mac_addr,
208                         struct key_params *params);
209
210 /*
211 ** key indianess swap utilities
212 */
213 static void swap_key_from_BE(struct wl_wsec_key *key);
214 static void swap_key_to_BE(struct wl_wsec_key *key);
215
216 /*
217 ** wl_priv memory init/deinit utilities
218 */
219 static s32 wl_init_priv_mem(struct wl_priv *wl);
220 static void wl_deinit_priv_mem(struct wl_priv *wl);
221
222 static void wl_delay(u32 ms);
223
224 /*
225 ** store/restore cfg80211 instance data
226 */
227 static void wl_set_drvdata(struct wl_dev *dev, void *data);
228 static void *wl_get_drvdata(struct wl_dev *dev);
229
230 /*
231 ** ibss mode utilities
232 */
233 static bool wl_is_ibssmode(struct wl_priv *wl);
234
235 /*
236 ** dongle up/down , default configuration utilities
237 */
238 static bool wl_is_linkdown(struct wl_priv *wl, const brcmf_event_msg_t *e);
239 static bool wl_is_linkup(struct wl_priv *wl, const brcmf_event_msg_t *e);
240 static bool wl_is_nonetwork(struct wl_priv *wl, const brcmf_event_msg_t *e);
241 static void wl_link_down(struct wl_priv *wl);
242 static s32 wl_dongle_mode(struct net_device *ndev, s32 iftype);
243 static s32 __wl_cfg80211_up(struct wl_priv *wl);
244 static s32 __wl_cfg80211_down(struct wl_priv *wl);
245 static s32 wl_dongle_probecap(struct wl_priv *wl);
246 static void wl_init_conf(struct wl_conf *conf);
247
248 /*
249 ** dongle configuration utilities
250 */
251 static s32 wl_dongle_eventmsg(struct net_device *ndev);
252 static s32 wl_dongle_scantime(struct net_device *ndev, s32 scan_assoc_time,
253                                 s32 scan_unassoc_time, s32 scan_passive_time);
254 static s32 wl_config_dongle(struct wl_priv *wl, bool need_lock);
255 static s32 wl_dongle_roam(struct net_device *ndev, u32 roamvar,
256                             u32 bcn_timeout);
257
258 /*
259 ** iscan handler
260 */
261 static void wl_iscan_timer(unsigned long data);
262 static void wl_term_iscan(struct wl_priv *wl);
263 static s32 wl_init_iscan(struct wl_priv *wl);
264 static s32 wl_iscan_thread(void *data);
265 static s32 wl_dev_iovar_setbuf(struct net_device *dev, s8 *iovar,
266                                  void *param, s32 paramlen, void *bufptr,
267                                  s32 buflen);
268 static s32 wl_dev_iovar_getbuf(struct net_device *dev, s8 *iovar,
269                                  void *param, s32 paramlen, void *bufptr,
270                                  s32 buflen);
271 static s32 wl_run_iscan(struct wl_iscan_ctrl *iscan, struct wlc_ssid *ssid,
272                           u16 action);
273 static s32 wl_do_iscan(struct wl_priv *wl);
274 static s32 wl_wakeup_iscan(struct wl_iscan_ctrl *iscan);
275 static s32 wl_invoke_iscan(struct wl_priv *wl);
276 static s32 wl_get_iscan_results(struct wl_iscan_ctrl *iscan, u32 *status,
277                                   struct wl_scan_results **bss_list);
278 static void wl_notify_iscan_complete(struct wl_iscan_ctrl *iscan, bool aborted);
279 static void wl_init_iscan_eloop(struct wl_iscan_eloop *el);
280 static s32 wl_iscan_done(struct wl_priv *wl);
281 static s32 wl_iscan_pending(struct wl_priv *wl);
282 static s32 wl_iscan_inprogress(struct wl_priv *wl);
283 static s32 wl_iscan_aborted(struct wl_priv *wl);
284
285 /*
286 ** fw/nvram downloading handler
287 */
288 static void wl_init_fw(struct wl_fw_ctrl *fw);
289
290 /*
291 * find most significant bit set
292 */
293 static __used u32 wl_find_msb(u16 bit16);
294
295 /*
296 * update pmklist to dongle
297 */
298 static __used s32 wl_update_pmklist(struct net_device *dev,
299                                       struct wl_pmk_list *pmk_list, s32 err);
300
301 static void wl_set_mpc(struct net_device *ndev, int mpc);
302
303 /*
304 * debufs support
305 */
306 static int wl_debugfs_add_netdev_params(struct wl_priv *wl);
307 static void wl_debugfs_remove_netdev(struct wl_priv *wl);
308
309 #define WL_PRIV_GET()                                                   \
310         ({                                                              \
311         struct wl_iface *ci = wl_get_drvdata(wl_cfg80211_dev);          \
312         if (unlikely(!ci)) {                                            \
313                 WL_ERR("wl_cfg80211_dev is unavailable\n");             \
314                 BUG();                                                  \
315         }                                                               \
316         ci_to_wl(ci);                                                   \
317 })
318
319 #define CHECK_SYS_UP()                                                  \
320 do {                                                                    \
321         struct wl_priv *wl = wiphy_to_wl(wiphy);                        \
322         if (unlikely(!test_bit(WL_STATUS_READY, &wl->status))) {        \
323                 WL_INFO("device is not ready : status (%d)\n",          \
324                         (int)wl->status);                               \
325                 return -EIO;                                            \
326         }                                                               \
327 } while (0)
328
329 #define CHAN2G(_channel, _freq, _flags) {                       \
330         .band                   = IEEE80211_BAND_2GHZ,          \
331         .center_freq            = (_freq),                      \
332         .hw_value               = (_channel),                   \
333         .flags                  = (_flags),                     \
334         .max_antenna_gain       = 0,                            \
335         .max_power              = 30,                           \
336 }
337
338 #define CHAN5G(_channel, _flags) {                              \
339         .band                   = IEEE80211_BAND_5GHZ,          \
340         .center_freq            = 5000 + (5 * (_channel)),      \
341         .hw_value               = (_channel),                   \
342         .flags                  = (_flags),                     \
343         .max_antenna_gain       = 0,                            \
344         .max_power              = 30,                           \
345 }
346
347 #define RATE_TO_BASE100KBPS(rate)   (((rate) * 10) / 2)
348 #define RATETAB_ENT(_rateid, _flags) \
349         {                                                               \
350                 .bitrate        = RATE_TO_BASE100KBPS(_rateid),     \
351                 .hw_value       = (_rateid),                            \
352                 .flags          = (_flags),                             \
353         }
354
355 static struct ieee80211_rate __wl_rates[] = {
356         RATETAB_ENT(WLC_RATE_1M, 0),
357         RATETAB_ENT(WLC_RATE_2M, IEEE80211_RATE_SHORT_PREAMBLE),
358         RATETAB_ENT(WLC_RATE_5M5, IEEE80211_RATE_SHORT_PREAMBLE),
359         RATETAB_ENT(WLC_RATE_11M, IEEE80211_RATE_SHORT_PREAMBLE),
360         RATETAB_ENT(WLC_RATE_6M, 0),
361         RATETAB_ENT(WLC_RATE_9M, 0),
362         RATETAB_ENT(WLC_RATE_12M, 0),
363         RATETAB_ENT(WLC_RATE_18M, 0),
364         RATETAB_ENT(WLC_RATE_24M, 0),
365         RATETAB_ENT(WLC_RATE_36M, 0),
366         RATETAB_ENT(WLC_RATE_48M, 0),
367         RATETAB_ENT(WLC_RATE_54M, 0),
368 };
369
370 #define wl_a_rates              (__wl_rates + 4)
371 #define wl_a_rates_size 8
372 #define wl_g_rates              (__wl_rates + 0)
373 #define wl_g_rates_size 12
374
375 static struct ieee80211_channel __wl_2ghz_channels[] = {
376         CHAN2G(1, 2412, 0),
377         CHAN2G(2, 2417, 0),
378         CHAN2G(3, 2422, 0),
379         CHAN2G(4, 2427, 0),
380         CHAN2G(5, 2432, 0),
381         CHAN2G(6, 2437, 0),
382         CHAN2G(7, 2442, 0),
383         CHAN2G(8, 2447, 0),
384         CHAN2G(9, 2452, 0),
385         CHAN2G(10, 2457, 0),
386         CHAN2G(11, 2462, 0),
387         CHAN2G(12, 2467, 0),
388         CHAN2G(13, 2472, 0),
389         CHAN2G(14, 2484, 0),
390 };
391
392 static struct ieee80211_channel __wl_5ghz_a_channels[] = {
393         CHAN5G(34, 0), CHAN5G(36, 0),
394         CHAN5G(38, 0), CHAN5G(40, 0),
395         CHAN5G(42, 0), CHAN5G(44, 0),
396         CHAN5G(46, 0), CHAN5G(48, 0),
397         CHAN5G(52, 0), CHAN5G(56, 0),
398         CHAN5G(60, 0), CHAN5G(64, 0),
399         CHAN5G(100, 0), CHAN5G(104, 0),
400         CHAN5G(108, 0), CHAN5G(112, 0),
401         CHAN5G(116, 0), CHAN5G(120, 0),
402         CHAN5G(124, 0), CHAN5G(128, 0),
403         CHAN5G(132, 0), CHAN5G(136, 0),
404         CHAN5G(140, 0), CHAN5G(149, 0),
405         CHAN5G(153, 0), CHAN5G(157, 0),
406         CHAN5G(161, 0), CHAN5G(165, 0),
407         CHAN5G(184, 0), CHAN5G(188, 0),
408         CHAN5G(192, 0), CHAN5G(196, 0),
409         CHAN5G(200, 0), CHAN5G(204, 0),
410         CHAN5G(208, 0), CHAN5G(212, 0),
411         CHAN5G(216, 0),
412 };
413
414 static struct ieee80211_channel __wl_5ghz_n_channels[] = {
415         CHAN5G(32, 0), CHAN5G(34, 0),
416         CHAN5G(36, 0), CHAN5G(38, 0),
417         CHAN5G(40, 0), CHAN5G(42, 0),
418         CHAN5G(44, 0), CHAN5G(46, 0),
419         CHAN5G(48, 0), CHAN5G(50, 0),
420         CHAN5G(52, 0), CHAN5G(54, 0),
421         CHAN5G(56, 0), CHAN5G(58, 0),
422         CHAN5G(60, 0), CHAN5G(62, 0),
423         CHAN5G(64, 0), CHAN5G(66, 0),
424         CHAN5G(68, 0), CHAN5G(70, 0),
425         CHAN5G(72, 0), CHAN5G(74, 0),
426         CHAN5G(76, 0), CHAN5G(78, 0),
427         CHAN5G(80, 0), CHAN5G(82, 0),
428         CHAN5G(84, 0), CHAN5G(86, 0),
429         CHAN5G(88, 0), CHAN5G(90, 0),
430         CHAN5G(92, 0), CHAN5G(94, 0),
431         CHAN5G(96, 0), CHAN5G(98, 0),
432         CHAN5G(100, 0), CHAN5G(102, 0),
433         CHAN5G(104, 0), CHAN5G(106, 0),
434         CHAN5G(108, 0), CHAN5G(110, 0),
435         CHAN5G(112, 0), CHAN5G(114, 0),
436         CHAN5G(116, 0), CHAN5G(118, 0),
437         CHAN5G(120, 0), CHAN5G(122, 0),
438         CHAN5G(124, 0), CHAN5G(126, 0),
439         CHAN5G(128, 0), CHAN5G(130, 0),
440         CHAN5G(132, 0), CHAN5G(134, 0),
441         CHAN5G(136, 0), CHAN5G(138, 0),
442         CHAN5G(140, 0), CHAN5G(142, 0),
443         CHAN5G(144, 0), CHAN5G(145, 0),
444         CHAN5G(146, 0), CHAN5G(147, 0),
445         CHAN5G(148, 0), CHAN5G(149, 0),
446         CHAN5G(150, 0), CHAN5G(151, 0),
447         CHAN5G(152, 0), CHAN5G(153, 0),
448         CHAN5G(154, 0), CHAN5G(155, 0),
449         CHAN5G(156, 0), CHAN5G(157, 0),
450         CHAN5G(158, 0), CHAN5G(159, 0),
451         CHAN5G(160, 0), CHAN5G(161, 0),
452         CHAN5G(162, 0), CHAN5G(163, 0),
453         CHAN5G(164, 0), CHAN5G(165, 0),
454         CHAN5G(166, 0), CHAN5G(168, 0),
455         CHAN5G(170, 0), CHAN5G(172, 0),
456         CHAN5G(174, 0), CHAN5G(176, 0),
457         CHAN5G(178, 0), CHAN5G(180, 0),
458         CHAN5G(182, 0), CHAN5G(184, 0),
459         CHAN5G(186, 0), CHAN5G(188, 0),
460         CHAN5G(190, 0), CHAN5G(192, 0),
461         CHAN5G(194, 0), CHAN5G(196, 0),
462         CHAN5G(198, 0), CHAN5G(200, 0),
463         CHAN5G(202, 0), CHAN5G(204, 0),
464         CHAN5G(206, 0), CHAN5G(208, 0),
465         CHAN5G(210, 0), CHAN5G(212, 0),
466         CHAN5G(214, 0), CHAN5G(216, 0),
467         CHAN5G(218, 0), CHAN5G(220, 0),
468         CHAN5G(222, 0), CHAN5G(224, 0),
469         CHAN5G(226, 0), CHAN5G(228, 0),
470 };
471
472 static struct ieee80211_supported_band __wl_band_2ghz = {
473         .band = IEEE80211_BAND_2GHZ,
474         .channels = __wl_2ghz_channels,
475         .n_channels = ARRAY_SIZE(__wl_2ghz_channels),
476         .bitrates = wl_g_rates,
477         .n_bitrates = wl_g_rates_size,
478 };
479
480 static struct ieee80211_supported_band __wl_band_5ghz_a = {
481         .band = IEEE80211_BAND_5GHZ,
482         .channels = __wl_5ghz_a_channels,
483         .n_channels = ARRAY_SIZE(__wl_5ghz_a_channels),
484         .bitrates = wl_a_rates,
485         .n_bitrates = wl_a_rates_size,
486 };
487
488 static struct ieee80211_supported_band __wl_band_5ghz_n = {
489         .band = IEEE80211_BAND_5GHZ,
490         .channels = __wl_5ghz_n_channels,
491         .n_channels = ARRAY_SIZE(__wl_5ghz_n_channels),
492         .bitrates = wl_a_rates,
493         .n_bitrates = wl_a_rates_size,
494 };
495
496 static const u32 __wl_cipher_suites[] = {
497         WLAN_CIPHER_SUITE_WEP40,
498         WLAN_CIPHER_SUITE_WEP104,
499         WLAN_CIPHER_SUITE_TKIP,
500         WLAN_CIPHER_SUITE_CCMP,
501         WLAN_CIPHER_SUITE_AES_CMAC,
502 };
503
504 static void swap_key_from_BE(struct wl_wsec_key *key)
505 {
506         key->index = cpu_to_le32(key->index);
507         key->len = cpu_to_le32(key->len);
508         key->algo = cpu_to_le32(key->algo);
509         key->flags = cpu_to_le32(key->flags);
510         key->rxiv.hi = cpu_to_le32(key->rxiv.hi);
511         key->rxiv.lo = cpu_to_le16(key->rxiv.lo);
512         key->iv_initialized = cpu_to_le32(key->iv_initialized);
513 }
514
515 static void swap_key_to_BE(struct wl_wsec_key *key)
516 {
517         key->index = le32_to_cpu(key->index);
518         key->len = le32_to_cpu(key->len);
519         key->algo = le32_to_cpu(key->algo);
520         key->flags = le32_to_cpu(key->flags);
521         key->rxiv.hi = le32_to_cpu(key->rxiv.hi);
522         key->rxiv.lo = le16_to_cpu(key->rxiv.lo);
523         key->iv_initialized = le32_to_cpu(key->iv_initialized);
524 }
525
526 static s32
527 wl_dev_ioctl(struct net_device *dev, u32 cmd, void *arg, u32 len)
528 {
529         struct ifreq ifr;
530         struct wl_ioctl ioc;
531         mm_segment_t fs;
532         s32 err = 0;
533
534         memset(&ioc, 0, sizeof(ioc));
535         ioc.cmd = cmd;
536         ioc.buf = arg;
537         ioc.len = len;
538         strcpy(ifr.ifr_name, dev->name);
539         ifr.ifr_data = (caddr_t)&ioc;
540
541         fs = get_fs();
542         set_fs(get_ds());
543         err = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, SIOCDEVPRIVATE);
544         set_fs(fs);
545
546         return err;
547 }
548
549 static s32
550 wl_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
551                          enum nl80211_iftype type, u32 *flags,
552                          struct vif_params *params)
553 {
554         struct wl_priv *wl = wiphy_to_wl(wiphy);
555         struct wireless_dev *wdev;
556         s32 infra = 0;
557         s32 err = 0;
558
559         WL_TRACE("Enter\n");
560         CHECK_SYS_UP();
561
562         switch (type) {
563         case NL80211_IFTYPE_MONITOR:
564         case NL80211_IFTYPE_WDS:
565                 WL_ERR("type (%d) : currently we do not support this type\n",
566                        type);
567                 return -EOPNOTSUPP;
568         case NL80211_IFTYPE_ADHOC:
569                 wl->conf->mode = WL_MODE_IBSS;
570                 infra = 0;
571                 break;
572         case NL80211_IFTYPE_STATION:
573                 wl->conf->mode = WL_MODE_BSS;
574                 infra = 1;
575                 break;
576         default:
577                 err = -EINVAL;
578                 goto done;
579         }
580
581         infra = cpu_to_le32(infra);
582         err = wl_dev_ioctl(ndev, BRCMF_C_SET_INFRA, &infra, sizeof(infra));
583         if (unlikely(err)) {
584                 WL_ERR("WLC_SET_INFRA error (%d)\n", err);
585                 err = -EAGAIN;
586         } else {
587                 wdev = ndev->ieee80211_ptr;
588                 wdev->iftype = type;
589         }
590
591         WL_INFO("IF Type = %s\n",
592                 (wl->conf->mode == WL_MODE_IBSS) ? "Adhoc" : "Infra");
593
594 done:
595         WL_TRACE("Exit\n");
596
597         return err;
598 }
599
600 static void wl_iscan_prep(struct wl_scan_params *params, struct wlc_ssid *ssid)
601 {
602         memcpy(params->bssid, ether_bcast, ETH_ALEN);
603         params->bss_type = DOT11_BSSTYPE_ANY;
604         params->scan_type = 0;
605         params->nprobes = -1;
606         params->active_time = -1;
607         params->passive_time = -1;
608         params->home_time = -1;
609         params->channel_num = 0;
610
611         params->nprobes = cpu_to_le32(params->nprobes);
612         params->active_time = cpu_to_le32(params->active_time);
613         params->passive_time = cpu_to_le32(params->passive_time);
614         params->home_time = cpu_to_le32(params->home_time);
615         if (ssid && ssid->SSID_len)
616                 memcpy(&params->ssid, ssid, sizeof(wlc_ssid_t));
617
618 }
619
620 static s32
621 wl_dev_iovar_setbuf(struct net_device *dev, s8 * iovar, void *param,
622                     s32 paramlen, void *bufptr, s32 buflen)
623 {
624         s32 iolen;
625
626         iolen = brcmu_mkiovar(iovar, param, paramlen, bufptr, buflen);
627         BUG_ON(!iolen);
628
629         return wl_dev_ioctl(dev, BRCMF_C_SET_VAR, bufptr, iolen);
630 }
631
632 static s32
633 wl_dev_iovar_getbuf(struct net_device *dev, s8 * iovar, void *param,
634                     s32 paramlen, void *bufptr, s32 buflen)
635 {
636         s32 iolen;
637
638         iolen = brcmu_mkiovar(iovar, param, paramlen, bufptr, buflen);
639         BUG_ON(!iolen);
640
641         return wl_dev_ioctl(dev, BRCMF_C_GET_VAR, bufptr, buflen);
642 }
643
644 static s32
645 wl_run_iscan(struct wl_iscan_ctrl *iscan, struct wlc_ssid *ssid, u16 action)
646 {
647         s32 params_size =
648             (WL_SCAN_PARAMS_FIXED_SIZE + offsetof(wl_iscan_params_t, params));
649         struct wl_iscan_params *params;
650         s32 err = 0;
651
652         if (ssid && ssid->SSID_len)
653                 params_size += sizeof(struct wlc_ssid);
654         params = kzalloc(params_size, GFP_KERNEL);
655         if (unlikely(!params))
656                 return -ENOMEM;
657         BUG_ON(params_size >= BRCMF_C_IOCTL_SMLEN);
658
659         wl_iscan_prep(&params->params, ssid);
660
661         params->version = cpu_to_le32(ISCAN_REQ_VERSION);
662         params->action = cpu_to_le16(action);
663         params->scan_duration = cpu_to_le16(0);
664
665         /* params_size += offsetof(wl_iscan_params_t, params); */
666         err = wl_dev_iovar_setbuf(iscan->dev, "iscan", params, params_size,
667                                 iscan->ioctl_buf, BRCMF_C_IOCTL_SMLEN);
668         if (unlikely(err)) {
669                 if (err == -EBUSY) {
670                         WL_INFO("system busy : iscan canceled\n");
671                 } else {
672                         WL_ERR("error (%d)\n", err);
673                 }
674         }
675         kfree(params);
676         return err;
677 }
678
679 static s32 wl_do_iscan(struct wl_priv *wl)
680 {
681         struct wl_iscan_ctrl *iscan = wl_to_iscan(wl);
682         struct net_device *ndev = wl_to_ndev(wl);
683         struct wlc_ssid ssid;
684         s32 passive_scan;
685         s32 err = 0;
686
687         /* Broadcast scan by default */
688         memset(&ssid, 0, sizeof(ssid));
689
690         iscan->state = WL_ISCAN_STATE_SCANING;
691
692         passive_scan = wl->active_scan ? 0 : 1;
693         err = wl_dev_ioctl(wl_to_ndev(wl), BRCMF_C_SET_PASSIVE_SCAN,
694                         &passive_scan, sizeof(passive_scan));
695         if (unlikely(err)) {
696                 WL_ERR("error (%d)\n", err);
697                 return err;
698         }
699         wl_set_mpc(ndev, 0);
700         wl->iscan_kickstart = true;
701         wl_run_iscan(iscan, &ssid, WL_SCAN_ACTION_START);
702         mod_timer(&iscan->timer, jiffies + iscan->timer_ms * HZ / 1000);
703         iscan->timer_on = 1;
704
705         return err;
706 }
707
708 static s32
709 __wl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
710                    struct cfg80211_scan_request *request,
711                    struct cfg80211_ssid *this_ssid)
712 {
713         struct wl_priv *wl = ndev_to_wl(ndev);
714         struct cfg80211_ssid *ssids;
715         struct wl_scan_req *sr = wl_to_sr(wl);
716         s32 passive_scan;
717         bool iscan_req;
718         bool spec_scan;
719         s32 err = 0;
720
721         if (unlikely(test_bit(WL_STATUS_SCANNING, &wl->status))) {
722                 WL_ERR("Scanning already : status (%d)\n", (int)wl->status);
723                 return -EAGAIN;
724         }
725         if (unlikely(test_bit(WL_STATUS_SCAN_ABORTING, &wl->status))) {
726                 WL_ERR("Scanning being aborted : status (%d)\n",
727                        (int)wl->status);
728                 return -EAGAIN;
729         }
730         if (test_bit(WL_STATUS_CONNECTING, &wl->status)) {
731                 WL_ERR("Connecting : status (%d)\n",
732                        (int)wl->status);
733                 return -EAGAIN;
734         }
735
736         iscan_req = false;
737         spec_scan = false;
738         if (request) {
739                 /* scan bss */
740                 ssids = request->ssids;
741                 if (wl->iscan_on && (!ssids || !ssids->ssid_len))
742                         iscan_req = true;
743         } else {
744                 /* scan in ibss */
745                 /* we don't do iscan in ibss */
746                 ssids = this_ssid;
747         }
748
749         wl->scan_request = request;
750         set_bit(WL_STATUS_SCANNING, &wl->status);
751         if (iscan_req) {
752                 err = wl_do_iscan(wl);
753                 if (likely(!err))
754                         return err;
755                 else
756                         goto scan_out;
757         } else {
758                 WL_SCAN("ssid \"%s\", ssid_len (%d)\n",
759                        ssids->ssid, ssids->ssid_len);
760                 memset(&sr->ssid, 0, sizeof(sr->ssid));
761                 sr->ssid.SSID_len =
762                             min_t(u8, sizeof(sr->ssid.SSID), ssids->ssid_len);
763                 if (sr->ssid.SSID_len) {
764                         memcpy(sr->ssid.SSID, ssids->ssid, sr->ssid.SSID_len);
765                         sr->ssid.SSID_len = cpu_to_le32(sr->ssid.SSID_len);
766                         spec_scan = true;
767                 } else {
768                         WL_SCAN("Broadcast scan\n");
769                 }
770
771                 passive_scan = wl->active_scan ? 0 : 1;
772                 err = wl_dev_ioctl(ndev, BRCMF_C_SET_PASSIVE_SCAN,
773                                 &passive_scan, sizeof(passive_scan));
774                 if (unlikely(err)) {
775                         WL_ERR("WLC_SET_PASSIVE_SCAN error (%d)\n", err);
776                         goto scan_out;
777                 }
778                 wl_set_mpc(ndev, 0);
779                 err = wl_dev_ioctl(ndev, BRCMF_C_SCAN, &sr->ssid,
780                                 sizeof(sr->ssid));
781                 if (err) {
782                         if (err == -EBUSY) {
783                                 WL_INFO("system busy : scan for \"%s\" canceled\n",
784                                         sr->ssid.SSID);
785                         } else {
786                                 WL_ERR("WLC_SCAN error (%d)\n", err);
787                         }
788                         wl_set_mpc(ndev, 1);
789                         goto scan_out;
790                 }
791         }
792
793         return 0;
794
795 scan_out:
796         clear_bit(WL_STATUS_SCANNING, &wl->status);
797         wl->scan_request = NULL;
798         return err;
799 }
800
801 static s32
802 wl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
803                  struct cfg80211_scan_request *request)
804 {
805         s32 err = 0;
806
807         WL_TRACE("Enter\n");
808
809         CHECK_SYS_UP();
810
811         err = __wl_cfg80211_scan(wiphy, ndev, request, NULL);
812         if (unlikely(err))
813                 WL_ERR("scan error (%d)\n", err);
814
815         WL_TRACE("Exit\n");
816         return err;
817 }
818
819 static s32 wl_dev_intvar_set(struct net_device *dev, s8 *name, s32 val)
820 {
821         s8 buf[BRCMF_C_IOCTL_SMLEN];
822         u32 len;
823         s32 err = 0;
824
825         val = cpu_to_le32(val);
826         len = brcmu_mkiovar(name, (char *)(&val), sizeof(val), buf,
827                             sizeof(buf));
828         BUG_ON(!len);
829
830         err = wl_dev_ioctl(dev, BRCMF_C_SET_VAR, buf, len);
831         if (unlikely(err))
832                 WL_ERR("error (%d)\n", err);
833
834         return err;
835 }
836
837 static s32
838 wl_dev_intvar_get(struct net_device *dev, s8 *name, s32 *retval)
839 {
840         union {
841                 s8 buf[BRCMF_C_IOCTL_SMLEN];
842                 s32 val;
843         } var;
844         u32 len;
845         u32 data_null;
846         s32 err = 0;
847
848         len =
849             brcmu_mkiovar(name, (char *)(&data_null), 0, (char *)(&var),
850                         sizeof(var.buf));
851         BUG_ON(!len);
852         err = wl_dev_ioctl(dev, BRCMF_C_GET_VAR, &var, len);
853         if (unlikely(err))
854                 WL_ERR("error (%d)\n", err);
855
856         *retval = le32_to_cpu(var.val);
857
858         return err;
859 }
860
861 static s32 wl_set_rts(struct net_device *dev, u32 rts_threshold)
862 {
863         s32 err = 0;
864
865         err = wl_dev_intvar_set(dev, "rtsthresh", rts_threshold);
866         if (unlikely(err))
867                 WL_ERR("Error (%d)\n", err);
868
869         return err;
870 }
871
872 static s32 wl_set_frag(struct net_device *dev, u32 frag_threshold)
873 {
874         s32 err = 0;
875
876         err = wl_dev_intvar_set(dev, "fragthresh", frag_threshold);
877         if (unlikely(err))
878                 WL_ERR("Error (%d)\n", err);
879
880         return err;
881 }
882
883 static s32 wl_set_retry(struct net_device *dev, u32 retry, bool l)
884 {
885         s32 err = 0;
886         u32 cmd = (l ? WLC_SET_LRL : WLC_SET_SRL);
887
888         retry = cpu_to_le32(retry);
889         err = wl_dev_ioctl(dev, cmd, &retry, sizeof(retry));
890         if (unlikely(err)) {
891                 WL_ERR("cmd (%d) , error (%d)\n", cmd, err);
892                 return err;
893         }
894         return err;
895 }
896
897 static s32 wl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
898 {
899         struct wl_priv *wl = wiphy_to_wl(wiphy);
900         struct net_device *ndev = wl_to_ndev(wl);
901         s32 err = 0;
902
903         WL_TRACE("Enter\n");
904         CHECK_SYS_UP();
905
906         if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
907             (wl->conf->rts_threshold != wiphy->rts_threshold)) {
908                 wl->conf->rts_threshold = wiphy->rts_threshold;
909                 err = wl_set_rts(ndev, wl->conf->rts_threshold);
910                 if (!err)
911                         goto done;
912         }
913         if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
914             (wl->conf->frag_threshold != wiphy->frag_threshold)) {
915                 wl->conf->frag_threshold = wiphy->frag_threshold;
916                 err = wl_set_frag(ndev, wl->conf->frag_threshold);
917                 if (!err)
918                         goto done;
919         }
920         if (changed & WIPHY_PARAM_RETRY_LONG
921             && (wl->conf->retry_long != wiphy->retry_long)) {
922                 wl->conf->retry_long = wiphy->retry_long;
923                 err = wl_set_retry(ndev, wl->conf->retry_long, true);
924                 if (!err)
925                         goto done;
926         }
927         if (changed & WIPHY_PARAM_RETRY_SHORT
928             && (wl->conf->retry_short != wiphy->retry_short)) {
929                 wl->conf->retry_short = wiphy->retry_short;
930                 err = wl_set_retry(ndev, wl->conf->retry_short, false);
931                 if (!err)
932                         goto done;
933         }
934
935 done:
936         WL_TRACE("Exit\n");
937         return err;
938 }
939
940 static s32
941 wl_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
942                       struct cfg80211_ibss_params *params)
943 {
944         struct wl_priv *wl = wiphy_to_wl(wiphy);
945         struct wl_join_params join_params;
946         size_t join_params_size = 0;
947         s32 err = 0;
948         s32 wsec = 0;
949         s32 bcnprd;
950
951         WL_TRACE("Enter\n");
952         CHECK_SYS_UP();
953
954         if (params->ssid)
955                 WL_CONN("SSID: %s\n", params->ssid);
956         else {
957                 WL_CONN("SSID: NULL, Not supported\n");
958                 return -EOPNOTSUPP;
959         }
960
961         set_bit(WL_STATUS_CONNECTING, &wl->status);
962
963         if (params->bssid)
964                 WL_CONN("BSSID: %02X %02X %02X %02X %02X %02X\n",
965                 params->bssid[0], params->bssid[1], params->bssid[2],
966                 params->bssid[3], params->bssid[4], params->bssid[5]);
967         else
968                 WL_CONN("No BSSID specified\n");
969
970         if (params->channel)
971                 WL_CONN("channel: %d\n", params->channel->center_freq);
972         else
973                 WL_CONN("no channel specified\n");
974
975         if (params->channel_fixed)
976                 WL_CONN("fixed channel required\n");
977         else
978                 WL_CONN("no fixed channel required\n");
979
980         if (params->ie && params->ie_len)
981                 WL_CONN("ie len: %d\n", params->ie_len);
982         else
983                 WL_CONN("no ie specified\n");
984
985         if (params->beacon_interval)
986                 WL_CONN("beacon interval: %d\n", params->beacon_interval);
987         else
988                 WL_CONN("no beacon interval specified\n");
989
990         if (params->basic_rates)
991                 WL_CONN("basic rates: %08X\n", params->basic_rates);
992         else
993                 WL_CONN("no basic rates specified\n");
994
995         if (params->privacy)
996                 WL_CONN("privacy required\n");
997         else
998                 WL_CONN("no privacy required\n");
999
1000         /* Configure Privacy for starter */
1001         if (params->privacy)
1002                 wsec |= WEP_ENABLED;
1003
1004         err = wl_dev_intvar_set(dev, "wsec", wsec);
1005         if (unlikely(err)) {
1006                 WL_ERR("wsec failed (%d)\n", err);
1007                 goto done;
1008         }
1009
1010         /* Configure Beacon Interval for starter */
1011         if (params->beacon_interval)
1012                 bcnprd = cpu_to_le32(params->beacon_interval);
1013         else
1014                 bcnprd = cpu_to_le32(100);
1015
1016         err = wl_dev_ioctl(dev, WLC_SET_BCNPRD, &bcnprd, sizeof(bcnprd));
1017         if (unlikely(err)) {
1018                 WL_ERR("WLC_SET_BCNPRD failed (%d)\n", err);
1019                 goto done;
1020         }
1021
1022         /* Configure required join parameter */
1023         memset(&join_params, 0, sizeof(wl_join_params_t));
1024
1025         /* SSID */
1026         join_params.ssid.SSID_len =
1027                         (params->ssid_len > 32) ? 32 : params->ssid_len;
1028         memcpy(join_params.ssid.SSID, params->ssid, join_params.ssid.SSID_len);
1029         join_params.ssid.SSID_len = cpu_to_le32(join_params.ssid.SSID_len);
1030         join_params_size = sizeof(join_params.ssid);
1031         wl_update_prof(wl, NULL, &join_params.ssid, WL_PROF_SSID);
1032
1033         /* BSSID */
1034         if (params->bssid) {
1035                 memcpy(join_params.params.bssid, params->bssid, ETH_ALEN);
1036                 join_params_size =
1037                         sizeof(join_params.ssid) + WL_ASSOC_PARAMS_FIXED_SIZE;
1038         } else {
1039                 memcpy(join_params.params.bssid, ether_bcast, ETH_ALEN);
1040         }
1041         wl_update_prof(wl, NULL, &join_params.params.bssid, WL_PROF_BSSID);
1042
1043         /* Channel */
1044         if (params->channel) {
1045                 u32 target_channel;
1046
1047                 wl->channel =
1048                         ieee80211_frequency_to_channel(
1049                                 params->channel->center_freq);
1050                 if (params->channel_fixed) {
1051                         /* adding chanspec */
1052                         wl_ch_to_chanspec(wl->channel,
1053                                 &join_params, &join_params_size);
1054                 }
1055
1056                 /* set channel for starter */
1057                 target_channel = cpu_to_le32(wl->channel);
1058                 err = wl_dev_ioctl(dev, WLC_SET_CHANNEL,
1059                         &target_channel, sizeof(target_channel));
1060                 if (unlikely(err)) {
1061                         WL_ERR("WLC_SET_CHANNEL failed (%d)\n", err);
1062                         goto done;
1063                 }
1064         } else
1065                 wl->channel = 0;
1066
1067         wl->ibss_starter = false;
1068
1069
1070         err = wl_dev_ioctl(dev, BRCMF_C_SET_SSID,
1071                            &join_params, join_params_size);
1072         if (unlikely(err)) {
1073                 WL_ERR("WLC_SET_SSID failed (%d)\n", err);
1074                 goto done;
1075         }
1076
1077 done:
1078         if (err)
1079                 clear_bit(WL_STATUS_CONNECTING, &wl->status);
1080         WL_TRACE("Exit\n");
1081         return err;
1082 }
1083
1084 static s32 wl_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1085 {
1086         struct wl_priv *wl = wiphy_to_wl(wiphy);
1087         s32 err = 0;
1088
1089         WL_TRACE("Enter\n");
1090         CHECK_SYS_UP();
1091
1092         wl_link_down(wl);
1093
1094         WL_TRACE("Exit\n");
1095
1096         return err;
1097 }
1098
1099 static s32
1100 wl_set_wpa_version(struct net_device *dev, struct cfg80211_connect_params *sme)
1101 {
1102         struct wl_priv *wl = ndev_to_wl(dev);
1103         struct wl_security *sec;
1104         s32 val = 0;
1105         s32 err = 0;
1106
1107         if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_1)
1108                 val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
1109         else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2)
1110                 val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
1111         else
1112                 val = WPA_AUTH_DISABLED;
1113         WL_CONN("setting wpa_auth to 0x%0x\n", val);
1114         err = wl_dev_intvar_set(dev, "wpa_auth", val);
1115         if (unlikely(err)) {
1116                 WL_ERR("set wpa_auth failed (%d)\n", err);
1117                 return err;
1118         }
1119         sec = wl_read_prof(wl, WL_PROF_SEC);
1120         sec->wpa_versions = sme->crypto.wpa_versions;
1121         return err;
1122 }
1123
1124 static s32
1125 wl_set_auth_type(struct net_device *dev, struct cfg80211_connect_params *sme)
1126 {
1127         struct wl_priv *wl = ndev_to_wl(dev);
1128         struct wl_security *sec;
1129         s32 val = 0;
1130         s32 err = 0;
1131
1132         switch (sme->auth_type) {
1133         case NL80211_AUTHTYPE_OPEN_SYSTEM:
1134                 val = 0;
1135                 WL_CONN("open system\n");
1136                 break;
1137         case NL80211_AUTHTYPE_SHARED_KEY:
1138                 val = 1;
1139                 WL_CONN("shared key\n");
1140                 break;
1141         case NL80211_AUTHTYPE_AUTOMATIC:
1142                 val = 2;
1143                 WL_CONN("automatic\n");
1144                 break;
1145         case NL80211_AUTHTYPE_NETWORK_EAP:
1146                 WL_CONN("network eap\n");
1147         default:
1148                 val = 2;
1149                 WL_ERR("invalid auth type (%d)\n", sme->auth_type);
1150                 break;
1151         }
1152
1153         err = wl_dev_intvar_set(dev, "auth", val);
1154         if (unlikely(err)) {
1155                 WL_ERR("set auth failed (%d)\n", err);
1156                 return err;
1157         }
1158         sec = wl_read_prof(wl, WL_PROF_SEC);
1159         sec->auth_type = sme->auth_type;
1160         return err;
1161 }
1162
1163 static s32
1164 wl_set_set_cipher(struct net_device *dev, struct cfg80211_connect_params *sme)
1165 {
1166         struct wl_priv *wl = ndev_to_wl(dev);
1167         struct wl_security *sec;
1168         s32 pval = 0;
1169         s32 gval = 0;
1170         s32 err = 0;
1171
1172         if (sme->crypto.n_ciphers_pairwise) {
1173                 switch (sme->crypto.ciphers_pairwise[0]) {
1174                 case WLAN_CIPHER_SUITE_WEP40:
1175                 case WLAN_CIPHER_SUITE_WEP104:
1176                         pval = WEP_ENABLED;
1177                         break;
1178                 case WLAN_CIPHER_SUITE_TKIP:
1179                         pval = TKIP_ENABLED;
1180                         break;
1181                 case WLAN_CIPHER_SUITE_CCMP:
1182                         pval = AES_ENABLED;
1183                         break;
1184                 case WLAN_CIPHER_SUITE_AES_CMAC:
1185                         pval = AES_ENABLED;
1186                         break;
1187                 default:
1188                         WL_ERR("invalid cipher pairwise (%d)\n",
1189                                sme->crypto.ciphers_pairwise[0]);
1190                         return -EINVAL;
1191                 }
1192         }
1193         if (sme->crypto.cipher_group) {
1194                 switch (sme->crypto.cipher_group) {
1195                 case WLAN_CIPHER_SUITE_WEP40:
1196                 case WLAN_CIPHER_SUITE_WEP104:
1197                         gval = WEP_ENABLED;
1198                         break;
1199                 case WLAN_CIPHER_SUITE_TKIP:
1200                         gval = TKIP_ENABLED;
1201                         break;
1202                 case WLAN_CIPHER_SUITE_CCMP:
1203                         gval = AES_ENABLED;
1204                         break;
1205                 case WLAN_CIPHER_SUITE_AES_CMAC:
1206                         gval = AES_ENABLED;
1207                         break;
1208                 default:
1209                         WL_ERR("invalid cipher group (%d)\n",
1210                                sme->crypto.cipher_group);
1211                         return -EINVAL;
1212                 }
1213         }
1214
1215         WL_CONN("pval (%d) gval (%d)\n", pval, gval);
1216         err = wl_dev_intvar_set(dev, "wsec", pval | gval);
1217         if (unlikely(err)) {
1218                 WL_ERR("error (%d)\n", err);
1219                 return err;
1220         }
1221
1222         sec = wl_read_prof(wl, WL_PROF_SEC);
1223         sec->cipher_pairwise = sme->crypto.ciphers_pairwise[0];
1224         sec->cipher_group = sme->crypto.cipher_group;
1225
1226         return err;
1227 }
1228
1229 static s32
1230 wl_set_key_mgmt(struct net_device *dev, struct cfg80211_connect_params *sme)
1231 {
1232         struct wl_priv *wl = ndev_to_wl(dev);
1233         struct wl_security *sec;
1234         s32 val = 0;
1235         s32 err = 0;
1236
1237         if (sme->crypto.n_akm_suites) {
1238                 err = wl_dev_intvar_get(dev, "wpa_auth", &val);
1239                 if (unlikely(err)) {
1240                         WL_ERR("could not get wpa_auth (%d)\n", err);
1241                         return err;
1242                 }
1243                 if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
1244                         switch (sme->crypto.akm_suites[0]) {
1245                         case WLAN_AKM_SUITE_8021X:
1246                                 val = WPA_AUTH_UNSPECIFIED;
1247                                 break;
1248                         case WLAN_AKM_SUITE_PSK:
1249                                 val = WPA_AUTH_PSK;
1250                                 break;
1251                         default:
1252                                 WL_ERR("invalid cipher group (%d)\n",
1253                                        sme->crypto.cipher_group);
1254                                 return -EINVAL;
1255                         }
1256                 } else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
1257                         switch (sme->crypto.akm_suites[0]) {
1258                         case WLAN_AKM_SUITE_8021X:
1259                                 val = WPA2_AUTH_UNSPECIFIED;
1260                                 break;
1261                         case WLAN_AKM_SUITE_PSK:
1262                                 val = WPA2_AUTH_PSK;
1263                                 break;
1264                         default:
1265                                 WL_ERR("invalid cipher group (%d)\n",
1266                                        sme->crypto.cipher_group);
1267                                 return -EINVAL;
1268                         }
1269                 }
1270
1271                 WL_CONN("setting wpa_auth to %d\n", val);
1272                 err = wl_dev_intvar_set(dev, "wpa_auth", val);
1273                 if (unlikely(err)) {
1274                         WL_ERR("could not set wpa_auth (%d)\n", err);
1275                         return err;
1276                 }
1277         }
1278         sec = wl_read_prof(wl, WL_PROF_SEC);
1279         sec->wpa_auth = sme->crypto.akm_suites[0];
1280
1281         return err;
1282 }
1283
1284 static s32
1285 wl_set_set_sharedkey(struct net_device *dev,
1286                      struct cfg80211_connect_params *sme)
1287 {
1288         struct wl_priv *wl = ndev_to_wl(dev);
1289         struct wl_security *sec;
1290         struct wl_wsec_key key;
1291         s32 val;
1292         s32 err = 0;
1293
1294         WL_CONN("key len (%d)\n", sme->key_len);
1295         if (sme->key_len) {
1296                 sec = wl_read_prof(wl, WL_PROF_SEC);
1297                 WL_CONN("wpa_versions 0x%x cipher_pairwise 0x%x\n",
1298                        sec->wpa_versions, sec->cipher_pairwise);
1299                 if (!
1300                     (sec->wpa_versions & (NL80211_WPA_VERSION_1 |
1301                                           NL80211_WPA_VERSION_2))
1302 && (sec->cipher_pairwise & (WLAN_CIPHER_SUITE_WEP40 |
1303                             WLAN_CIPHER_SUITE_WEP104))) {
1304                         memset(&key, 0, sizeof(key));
1305                         key.len = (u32) sme->key_len;
1306                         key.index = (u32) sme->key_idx;
1307                         if (unlikely(key.len > sizeof(key.data))) {
1308                                 WL_ERR("Too long key length (%u)\n", key.len);
1309                                 return -EINVAL;
1310                         }
1311                         memcpy(key.data, sme->key, key.len);
1312                         key.flags = WL_PRIMARY_KEY;
1313                         switch (sec->cipher_pairwise) {
1314                         case WLAN_CIPHER_SUITE_WEP40:
1315                                 key.algo = CRYPTO_ALGO_WEP1;
1316                                 break;
1317                         case WLAN_CIPHER_SUITE_WEP104:
1318                                 key.algo = CRYPTO_ALGO_WEP128;
1319                                 break;
1320                         default:
1321                                 WL_ERR("Invalid algorithm (%d)\n",
1322                                        sme->crypto.ciphers_pairwise[0]);
1323                                 return -EINVAL;
1324                         }
1325                         /* Set the new key/index */
1326                         WL_CONN("key length (%d) key index (%d) algo (%d)\n",
1327                                key.len, key.index, key.algo);
1328                         WL_CONN("key \"%s\"\n", key.data);
1329                         swap_key_from_BE(&key);
1330                         err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY, &key,
1331                                         sizeof(key));
1332                         if (unlikely(err)) {
1333                                 WL_ERR("WLC_SET_KEY error (%d)\n", err);
1334                                 return err;
1335                         }
1336                         if (sec->auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM) {
1337                                 WL_CONN("set auth_type to shared key\n");
1338                                 val = 1;        /* shared key */
1339                                 err = wl_dev_intvar_set(dev, "auth", val);
1340                                 if (unlikely(err)) {
1341                                         WL_ERR("set auth failed (%d)\n", err);
1342                                         return err;
1343                                 }
1344                         }
1345                 }
1346         }
1347         return err;
1348 }
1349
1350 static s32
1351 wl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
1352                     struct cfg80211_connect_params *sme)
1353 {
1354         struct wl_priv *wl = wiphy_to_wl(wiphy);
1355         struct ieee80211_channel *chan = sme->channel;
1356         struct wl_join_params join_params;
1357         size_t join_params_size;
1358
1359         s32 err = 0;
1360
1361         WL_TRACE("Enter\n");
1362         CHECK_SYS_UP();
1363
1364         if (unlikely(!sme->ssid)) {
1365                 WL_ERR("Invalid ssid\n");
1366                 return -EOPNOTSUPP;
1367         }
1368
1369         set_bit(WL_STATUS_CONNECTING, &wl->status);
1370
1371         if (chan) {
1372                 wl->channel =
1373                         ieee80211_frequency_to_channel(chan->center_freq);
1374                 WL_CONN("channel (%d), center_req (%d)\n",
1375                         wl->channel, chan->center_freq);
1376         } else
1377                 wl->channel = 0;
1378
1379         WL_INFO("ie (%p), ie_len (%zd)\n", sme->ie, sme->ie_len);
1380
1381         err = wl_set_wpa_version(dev, sme);
1382         if (err) {
1383                 WL_ERR("wl_set_wpa_version failed (%d)\n", err);
1384                 goto done;
1385         }
1386
1387         err = wl_set_auth_type(dev, sme);
1388         if (err) {
1389                 WL_ERR("wl_set_auth_type failed (%d)\n", err);
1390                 goto done;
1391         }
1392
1393         err = wl_set_set_cipher(dev, sme);
1394         if (err) {
1395                 WL_ERR("wl_set_set_cipher failed (%d)\n", err);
1396                 goto done;
1397         }
1398
1399         err = wl_set_key_mgmt(dev, sme);
1400         if (err) {
1401                 WL_ERR("wl_set_key_mgmt failed (%d)\n", err);
1402                 goto done;
1403         }
1404
1405         err = wl_set_set_sharedkey(dev, sme);
1406         if (err) {
1407                 WL_ERR("wl_set_set_sharedkey failed (%d)\n", err);
1408                 goto done;
1409         }
1410
1411         wl_update_prof(wl, NULL, sme->bssid, WL_PROF_BSSID);
1412         /*
1413          **  Join with specific BSSID and cached SSID
1414          **  If SSID is zero join based on BSSID only
1415          */
1416         memset(&join_params, 0, sizeof(join_params));
1417         join_params_size = sizeof(join_params.ssid);
1418
1419         join_params.ssid.SSID_len = min(sizeof(join_params.ssid.SSID), sme->ssid_len);
1420         memcpy(&join_params.ssid.SSID, sme->ssid, join_params.ssid.SSID_len);
1421         join_params.ssid.SSID_len = cpu_to_le32(join_params.ssid.SSID_len);
1422         wl_update_prof(wl, NULL, &join_params.ssid, WL_PROF_SSID);
1423
1424         if (sme->bssid)
1425                 memcpy(join_params.params.bssid, sme->bssid, ETH_ALEN);
1426         else
1427                 memcpy(join_params.params.bssid, ether_bcast, ETH_ALEN);
1428
1429         if (join_params.ssid.SSID_len < IEEE80211_MAX_SSID_LEN) {
1430                 WL_CONN("ssid \"%s\", len (%d)\n",
1431                        join_params.ssid.SSID, join_params.ssid.SSID_len);
1432         }
1433
1434         wl_ch_to_chanspec(wl->channel, &join_params, &join_params_size);
1435         err = wl_dev_ioctl(dev, BRCMF_C_SET_SSID,
1436                            &join_params, join_params_size);
1437         if (err)
1438                 WL_ERR("WLC_SET_SSID failed (%d)\n", err);
1439
1440 done:
1441         if (err)
1442                 clear_bit(WL_STATUS_CONNECTING, &wl->status);
1443         WL_TRACE("Exit\n");
1444         return err;
1445 }
1446
1447 static s32
1448 wl_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
1449                        u16 reason_code)
1450 {
1451         struct wl_priv *wl = wiphy_to_wl(wiphy);
1452         scb_val_t scbval;
1453         s32 err = 0;
1454
1455         WL_TRACE("Enter. Reason code = %d\n", reason_code);
1456         CHECK_SYS_UP();
1457
1458         clear_bit(WL_STATUS_CONNECTED, &wl->status);
1459
1460         scbval.val = reason_code;
1461         memcpy(&scbval.ea, wl_read_prof(wl, WL_PROF_BSSID), ETH_ALEN);
1462         scbval.val = cpu_to_le32(scbval.val);
1463         err = wl_dev_ioctl(dev, BRCMF_C_DISASSOC, &scbval,
1464                         sizeof(scb_val_t));
1465         if (unlikely(err))
1466                 WL_ERR("error (%d)\n", err);
1467
1468         wl->link_up = false;
1469
1470         WL_TRACE("Exit\n");
1471         return err;
1472 }
1473
1474 static s32
1475 wl_cfg80211_set_tx_power(struct wiphy *wiphy,
1476                          enum nl80211_tx_power_setting type, s32 dbm)
1477 {
1478
1479         struct wl_priv *wl = wiphy_to_wl(wiphy);
1480         struct net_device *ndev = wl_to_ndev(wl);
1481         u16 txpwrmw;
1482         s32 err = 0;
1483         s32 disable = 0;
1484
1485         WL_TRACE("Enter\n");
1486         CHECK_SYS_UP();
1487
1488         switch (type) {
1489         case NL80211_TX_POWER_AUTOMATIC:
1490                 break;
1491         case NL80211_TX_POWER_LIMITED:
1492                 if (dbm < 0) {
1493                         WL_ERR("TX_POWER_LIMITED - dbm is negative\n");
1494                         err = -EINVAL;
1495                         goto done;
1496                 }
1497                 break;
1498         case NL80211_TX_POWER_FIXED:
1499                 if (dbm < 0) {
1500                         WL_ERR("TX_POWER_FIXED - dbm is negative\n");
1501                         err = -EINVAL;
1502                         goto done;
1503                 }
1504                 break;
1505         }
1506         /* Make sure radio is off or on as far as software is concerned */
1507         disable = WL_RADIO_SW_DISABLE << 16;
1508         disable = cpu_to_le32(disable);
1509         err = wl_dev_ioctl(ndev, BRCMF_C_SET_RADIO, &disable, sizeof(disable));
1510         if (unlikely(err))
1511                 WL_ERR("WLC_SET_RADIO error (%d)\n", err);
1512
1513         if (dbm > 0xffff)
1514                 txpwrmw = 0xffff;
1515         else
1516                 txpwrmw = (u16) dbm;
1517         err = wl_dev_intvar_set(ndev, "qtxpower",
1518                         (s32) (brcmu_mw_to_qdbm(txpwrmw)));
1519         if (unlikely(err))
1520                 WL_ERR("qtxpower error (%d)\n", err);
1521         wl->conf->tx_power = dbm;
1522
1523 done:
1524         WL_TRACE("Exit\n");
1525         return err;
1526 }
1527
1528 static s32 wl_cfg80211_get_tx_power(struct wiphy *wiphy, s32 *dbm)
1529 {
1530         struct wl_priv *wl = wiphy_to_wl(wiphy);
1531         struct net_device *ndev = wl_to_ndev(wl);
1532         s32 txpwrdbm;
1533         u8 result;
1534         s32 err = 0;
1535
1536         WL_TRACE("Enter\n");
1537         CHECK_SYS_UP();
1538
1539         err = wl_dev_intvar_get(ndev, "qtxpower", &txpwrdbm);
1540         if (unlikely(err)) {
1541                 WL_ERR("error (%d)\n", err);
1542                 goto done;
1543         }
1544
1545         result = (u8) (txpwrdbm & ~WL_TXPWR_OVERRIDE);
1546         *dbm = (s32) brcmu_qdbm_to_mw(result);
1547
1548 done:
1549         WL_TRACE("Exit\n");
1550         return err;
1551 }
1552
1553 static s32
1554 wl_cfg80211_config_default_key(struct wiphy *wiphy, struct net_device *dev,
1555                                u8 key_idx, bool unicast, bool multicast)
1556 {
1557         u32 index;
1558         s32 wsec;
1559         s32 err = 0;
1560
1561         WL_TRACE("Enter\n");
1562         WL_CONN("key index (%d)\n", key_idx);
1563         CHECK_SYS_UP();
1564
1565         err = wl_dev_ioctl(dev, BRCMF_C_GET_WSEC, &wsec, sizeof(wsec));
1566         if (unlikely(err)) {
1567                 WL_ERR("WLC_GET_WSEC error (%d)\n", err);
1568                 goto done;
1569         }
1570
1571         wsec = le32_to_cpu(wsec);
1572         if (wsec & WEP_ENABLED) {
1573                 /* Just select a new current key */
1574                 index = (u32) key_idx;
1575                 index = cpu_to_le32(index);
1576                 err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY_PRIMARY, &index,
1577                                 sizeof(index));
1578                 if (unlikely(err))
1579                         WL_ERR("error (%d)\n", err);
1580         }
1581 done:
1582         WL_TRACE("Exit\n");
1583         return err;
1584 }
1585
1586 static s32
1587 wl_add_keyext(struct wiphy *wiphy, struct net_device *dev,
1588               u8 key_idx, const u8 *mac_addr, struct key_params *params)
1589 {
1590         struct wl_wsec_key key;
1591         s32 err = 0;
1592
1593         memset(&key, 0, sizeof(key));
1594         key.index = (u32) key_idx;
1595         /* Instead of bcast for ea address for default wep keys,
1596                  driver needs it to be Null */
1597         if (!is_multicast_ether_addr(mac_addr))
1598                 memcpy((char *)&key.ea, (void *)mac_addr, ETH_ALEN);
1599         key.len = (u32) params->key_len;
1600         /* check for key index change */
1601         if (key.len == 0) {
1602                 /* key delete */
1603                 swap_key_from_BE(&key);
1604                 err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY, &key, sizeof(key));
1605                 if (unlikely(err)) {
1606                         WL_ERR("key delete error (%d)\n", err);
1607                         return err;
1608                 }
1609         } else {
1610                 if (key.len > sizeof(key.data)) {
1611                         WL_ERR("Invalid key length (%d)\n", key.len);
1612                         return -EINVAL;
1613                 }
1614
1615                 WL_CONN("Setting the key index %d\n", key.index);
1616                 memcpy(key.data, params->key, key.len);
1617
1618                 if (params->cipher == WLAN_CIPHER_SUITE_TKIP) {
1619                         u8 keybuf[8];
1620                         memcpy(keybuf, &key.data[24], sizeof(keybuf));
1621                         memcpy(&key.data[24], &key.data[16], sizeof(keybuf));
1622                         memcpy(&key.data[16], keybuf, sizeof(keybuf));
1623                 }
1624
1625                 /* if IW_ENCODE_EXT_RX_SEQ_VALID set */
1626                 if (params->seq && params->seq_len == 6) {
1627                         /* rx iv */
1628                         u8 *ivptr;
1629                         ivptr = (u8 *) params->seq;
1630                         key.rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
1631                             (ivptr[3] << 8) | ivptr[2];
1632                         key.rxiv.lo = (ivptr[1] << 8) | ivptr[0];
1633                         key.iv_initialized = true;
1634                 }
1635
1636                 switch (params->cipher) {
1637                 case WLAN_CIPHER_SUITE_WEP40:
1638                         key.algo = CRYPTO_ALGO_WEP1;
1639                         WL_CONN("WLAN_CIPHER_SUITE_WEP40\n");
1640                         break;
1641                 case WLAN_CIPHER_SUITE_WEP104:
1642                         key.algo = CRYPTO_ALGO_WEP128;
1643                         WL_CONN("WLAN_CIPHER_SUITE_WEP104\n");
1644                         break;
1645                 case WLAN_CIPHER_SUITE_TKIP:
1646                         key.algo = CRYPTO_ALGO_TKIP;
1647                         WL_CONN("WLAN_CIPHER_SUITE_TKIP\n");
1648                         break;
1649                 case WLAN_CIPHER_SUITE_AES_CMAC:
1650                         key.algo = CRYPTO_ALGO_AES_CCM;
1651                         WL_CONN("WLAN_CIPHER_SUITE_AES_CMAC\n");
1652                         break;
1653                 case WLAN_CIPHER_SUITE_CCMP:
1654                         key.algo = CRYPTO_ALGO_AES_CCM;
1655                         WL_CONN("WLAN_CIPHER_SUITE_CCMP\n");
1656                         break;
1657                 default:
1658                         WL_ERR("Invalid cipher (0x%x)\n", params->cipher);
1659                         return -EINVAL;
1660                 }
1661                 swap_key_from_BE(&key);
1662
1663                 brcmf_netdev_wait_pend8021x(dev);
1664                 err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY, &key, sizeof(key));
1665                 if (unlikely(err)) {
1666                         WL_ERR("WLC_SET_KEY error (%d)\n", err);
1667                         return err;
1668                 }
1669         }
1670         return err;
1671 }
1672
1673 static s32
1674 wl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *dev,
1675                     u8 key_idx, bool pairwise, const u8 *mac_addr,
1676                     struct key_params *params)
1677 {
1678         struct wl_wsec_key key;
1679         s32 val;
1680         s32 wsec;
1681         s32 err = 0;
1682         u8 keybuf[8];
1683
1684         WL_TRACE("Enter\n");
1685         WL_CONN("key index (%d)\n", key_idx);
1686         CHECK_SYS_UP();
1687
1688         if (mac_addr) {
1689                 WL_TRACE("Exit");
1690                 return wl_add_keyext(wiphy, dev, key_idx, mac_addr, params);
1691         }
1692         memset(&key, 0, sizeof(key));
1693
1694         key.len = (u32) params->key_len;
1695         key.index = (u32) key_idx;
1696
1697         if (unlikely(key.len > sizeof(key.data))) {
1698                 WL_ERR("Too long key length (%u)\n", key.len);
1699                 err = -EINVAL;
1700                 goto done;
1701         }
1702         memcpy(key.data, params->key, key.len);
1703
1704         key.flags = WL_PRIMARY_KEY;
1705         switch (params->cipher) {
1706         case WLAN_CIPHER_SUITE_WEP40:
1707                 key.algo = CRYPTO_ALGO_WEP1;
1708                 WL_CONN("WLAN_CIPHER_SUITE_WEP40\n");
1709                 break;
1710         case WLAN_CIPHER_SUITE_WEP104:
1711                 key.algo = CRYPTO_ALGO_WEP128;
1712                 WL_CONN("WLAN_CIPHER_SUITE_WEP104\n");
1713                 break;
1714         case WLAN_CIPHER_SUITE_TKIP:
1715                 memcpy(keybuf, &key.data[24], sizeof(keybuf));
1716                 memcpy(&key.data[24], &key.data[16], sizeof(keybuf));
1717                 memcpy(&key.data[16], keybuf, sizeof(keybuf));
1718                 key.algo = CRYPTO_ALGO_TKIP;
1719                 WL_CONN("WLAN_CIPHER_SUITE_TKIP\n");
1720                 break;
1721         case WLAN_CIPHER_SUITE_AES_CMAC:
1722                 key.algo = CRYPTO_ALGO_AES_CCM;
1723                 WL_CONN("WLAN_CIPHER_SUITE_AES_CMAC\n");
1724                 break;
1725         case WLAN_CIPHER_SUITE_CCMP:
1726                 key.algo = CRYPTO_ALGO_AES_CCM;
1727                 WL_CONN("WLAN_CIPHER_SUITE_CCMP\n");
1728                 break;
1729         default:
1730                 WL_ERR("Invalid cipher (0x%x)\n", params->cipher);
1731                 err = -EINVAL;
1732                 goto done;
1733         }
1734
1735         /* Set the new key/index */
1736         swap_key_from_BE(&key);
1737         err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY, &key, sizeof(key));
1738         if (unlikely(err)) {
1739                 WL_ERR("WLC_SET_KEY error (%d)\n", err);
1740                 goto done;
1741         }
1742
1743         val = WEP_ENABLED;
1744         err = wl_dev_intvar_get(dev, "wsec", &wsec);
1745         if (unlikely(err)) {
1746                 WL_ERR("get wsec error (%d)\n", err);
1747                 goto done;
1748         }
1749         wsec &= ~(WEP_ENABLED);
1750         wsec |= val;
1751         err = wl_dev_intvar_set(dev, "wsec", wsec);
1752         if (unlikely(err)) {
1753                 WL_ERR("set wsec error (%d)\n", err);
1754                 goto done;
1755         }
1756
1757         val = 1;                /* assume shared key. otherwise 0 */
1758         val = cpu_to_le32(val);
1759         err = wl_dev_ioctl(dev, BRCMF_C_SET_AUTH, &val, sizeof(val));
1760         if (unlikely(err))
1761                 WL_ERR("WLC_SET_AUTH error (%d)\n", err);
1762 done:
1763         WL_TRACE("Exit\n");
1764         return err;
1765 }
1766
1767 static s32
1768 wl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *dev,
1769                     u8 key_idx, bool pairwise, const u8 *mac_addr)
1770 {
1771         struct wl_wsec_key key;
1772         s32 err = 0;
1773         s32 val;
1774         s32 wsec;
1775
1776         WL_TRACE("Enter\n");
1777         CHECK_SYS_UP();
1778         memset(&key, 0, sizeof(key));
1779
1780         key.index = (u32) key_idx;
1781         key.flags = WL_PRIMARY_KEY;
1782         key.algo = CRYPTO_ALGO_OFF;
1783
1784         WL_CONN("key index (%d)\n", key_idx);
1785         /* Set the new key/index */
1786         swap_key_from_BE(&key);
1787         err = wl_dev_ioctl(dev, BRCMF_C_SET_KEY, &key, sizeof(key));
1788         if (unlikely(err)) {
1789                 if (err == -EINVAL) {
1790                         if (key.index >= DOT11_MAX_DEFAULT_KEYS)
1791                                 /* we ignore this key index in this case */
1792                                 WL_ERR("invalid key index (%d)\n", key_idx);
1793                 } else
1794                         WL_ERR("WLC_SET_KEY error (%d)\n", err);
1795
1796                 /* Ignore this error, may happen during DISASSOC */
1797                 err = -EAGAIN;
1798                 goto done;
1799         }
1800
1801         val = 0;
1802         err = wl_dev_intvar_get(dev, "wsec", &wsec);
1803         if (unlikely(err)) {
1804                 WL_ERR("get wsec error (%d)\n", err);
1805                 /* Ignore this error, may happen during DISASSOC */
1806                 err = -EAGAIN;
1807                 goto done;
1808         }
1809         wsec &= ~(WEP_ENABLED);
1810         wsec |= val;
1811         err = wl_dev_intvar_set(dev, "wsec", wsec);
1812         if (unlikely(err)) {
1813                 WL_ERR("set wsec error (%d)\n", err);
1814                 /* Ignore this error, may happen during DISASSOC */
1815                 err = -EAGAIN;
1816                 goto done;
1817         }
1818
1819         val = 0;                /* assume open key. otherwise 1 */
1820         val = cpu_to_le32(val);
1821         err = wl_dev_ioctl(dev, BRCMF_C_SET_AUTH, &val, sizeof(val));
1822         if (unlikely(err)) {
1823                 WL_ERR("WLC_SET_AUTH error (%d)\n", err);
1824                 /* Ignore this error, may happen during DISASSOC */
1825                 err = -EAGAIN;
1826         }
1827 done:
1828         WL_TRACE("Exit\n");
1829         return err;
1830 }
1831
1832 static s32
1833 wl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *dev,
1834                     u8 key_idx, bool pairwise, const u8 *mac_addr, void *cookie,
1835                     void (*callback) (void *cookie, struct key_params * params))
1836 {
1837         struct key_params params;
1838         struct wl_wsec_key key;
1839         struct wl_priv *wl = wiphy_to_wl(wiphy);
1840         struct wl_security *sec;
1841         s32 wsec;
1842         s32 err = 0;
1843
1844         WL_TRACE("Enter\n");
1845         WL_CONN("key index (%d)\n", key_idx);
1846         CHECK_SYS_UP();
1847
1848         memset(&key, 0, sizeof(key));
1849         key.index = key_idx;
1850         swap_key_to_BE(&key);
1851         memset(&params, 0, sizeof(params));
1852         params.key_len = (u8) min_t(u8, WLAN_MAX_KEY_LEN, key.len);
1853         memcpy(params.key, key.data, params.key_len);
1854
1855         err = wl_dev_ioctl(dev, BRCMF_C_GET_WSEC, &wsec, sizeof(wsec));
1856         if (unlikely(err)) {
1857                 WL_ERR("WLC_GET_WSEC error (%d)\n", err);
1858                 /* Ignore this error, may happen during DISASSOC */
1859                 err = -EAGAIN;
1860                 goto done;
1861         }
1862         wsec = le32_to_cpu(wsec);
1863         switch (wsec) {
1864         case WEP_ENABLED:
1865                 sec = wl_read_prof(wl, WL_PROF_SEC);
1866                 if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP40) {
1867                         params.cipher = WLAN_CIPHER_SUITE_WEP40;
1868                         WL_CONN("WLAN_CIPHER_SUITE_WEP40\n");
1869                 } else if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP104) {
1870                         params.cipher = WLAN_CIPHER_SUITE_WEP104;
1871                         WL_CONN("WLAN_CIPHER_SUITE_WEP104\n");
1872                 }
1873                 break;
1874         case TKIP_ENABLED:
1875                 params.cipher = WLAN_CIPHER_SUITE_TKIP;
1876                 WL_CONN("WLAN_CIPHER_SUITE_TKIP\n");
1877                 break;
1878         case AES_ENABLED:
1879                 params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
1880                 WL_CONN("WLAN_CIPHER_SUITE_AES_CMAC\n");
1881                 break;
1882         default:
1883                 WL_ERR("Invalid algo (0x%x)\n", wsec);
1884                 err = -EINVAL;
1885                 goto done;
1886         }
1887         callback(cookie, &params);
1888
1889 done:
1890         WL_TRACE("Exit\n");
1891         return err;
1892 }
1893
1894 static s32
1895 wl_cfg80211_config_default_mgmt_key(struct wiphy *wiphy,
1896                                     struct net_device *dev, u8 key_idx)
1897 {
1898         WL_INFO("Not supported\n");
1899
1900         CHECK_SYS_UP();
1901         return -EOPNOTSUPP;
1902 }
1903
1904 static s32
1905 wl_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1906                         u8 *mac, struct station_info *sinfo)
1907 {
1908         struct wl_priv *wl = wiphy_to_wl(wiphy);
1909         scb_val_t scb_val;
1910         int rssi;
1911         s32 rate;
1912         s32 err = 0;
1913         u8 *bssid = wl_read_prof(wl, WL_PROF_BSSID);
1914
1915         WL_TRACE("Enter\n");
1916         CHECK_SYS_UP();
1917
1918         if (unlikely
1919             (memcmp(mac, bssid, ETH_ALEN))) {
1920                 WL_ERR("Wrong Mac address cfg_mac-%X:%X:%X:%X:%X:%X"
1921                         "wl_bssid-%X:%X:%X:%X:%X:%X\n",
1922                         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
1923                         bssid[0], bssid[1], bssid[2], bssid[3],
1924                         bssid[4], bssid[5]);
1925                 err = -ENOENT;
1926                 goto done;
1927         }
1928
1929         /* Report the current tx rate */
1930         err = wl_dev_ioctl(dev, BRCMF_C_GET_RATE, &rate, sizeof(rate));
1931         if (err) {
1932                 WL_ERR("Could not get rate (%d)\n", err);
1933         } else {
1934                 rate = le32_to_cpu(rate);
1935                 sinfo->filled |= STATION_INFO_TX_BITRATE;
1936                 sinfo->txrate.legacy = rate * 5;
1937                 WL_CONN("Rate %d Mbps\n", rate / 2);
1938         }
1939
1940         if (test_bit(WL_STATUS_CONNECTED, &wl->status)) {
1941                 scb_val.val = 0;
1942                 err = wl_dev_ioctl(dev, BRCMF_C_GET_RSSI, &scb_val,
1943                                 sizeof(scb_val_t));
1944                 if (unlikely(err)) {
1945                         WL_ERR("Could not get rssi (%d)\n", err);
1946                 }
1947                 rssi = le32_to_cpu(scb_val.val);
1948                 sinfo->filled |= STATION_INFO_SIGNAL;
1949                 sinfo->signal = rssi;
1950                 WL_CONN("RSSI %d dBm\n", rssi);
1951         }
1952
1953 done:
1954         WL_TRACE("Exit\n");
1955         return err;
1956 }
1957
1958 static s32
1959 wl_cfg80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1960                            bool enabled, s32 timeout)
1961 {
1962         s32 pm;
1963         s32 err = 0;
1964
1965         WL_TRACE("Enter\n");
1966         CHECK_SYS_UP();
1967
1968         pm = enabled ? PM_FAST : PM_OFF;
1969         pm = cpu_to_le32(pm);
1970         WL_INFO("power save %s\n", (pm ? "enabled" : "disabled"));
1971
1972         err = wl_dev_ioctl(dev, BRCMF_C_SET_PM, &pm, sizeof(pm));
1973         if (unlikely(err)) {
1974                 if (err == -ENODEV)
1975                         WL_ERR("net_device is not ready yet\n");
1976                 else
1977                         WL_ERR("error (%d)\n", err);
1978         }
1979         WL_TRACE("Exit\n");
1980         return err;
1981 }
1982
1983 static __used u32 wl_find_msb(u16 bit16)
1984 {
1985         u32 ret = 0;
1986
1987         if (bit16 & 0xff00) {
1988                 ret += 8;
1989                 bit16 >>= 8;
1990         }
1991
1992         if (bit16 & 0xf0) {
1993                 ret += 4;
1994                 bit16 >>= 4;
1995         }
1996
1997         if (bit16 & 0xc) {
1998                 ret += 2;
1999                 bit16 >>= 2;
2000         }
2001
2002         if (bit16 & 2)
2003                 ret += bit16 & 2;
2004         else if (bit16)
2005                 ret += bit16;
2006
2007         return ret;
2008 }
2009
2010 static s32
2011 wl_cfg80211_set_bitrate_mask(struct wiphy *wiphy, struct net_device *dev,
2012                              const u8 *addr,
2013                              const struct cfg80211_bitrate_mask *mask)
2014 {
2015         struct wl_rateset rateset;
2016         s32 rate;
2017         s32 val;
2018         s32 err_bg;
2019         s32 err_a;
2020         u32 legacy;
2021         s32 err = 0;
2022
2023         WL_TRACE("Enter\n");
2024         CHECK_SYS_UP();
2025
2026         /* addr param is always NULL. ignore it */
2027         /* Get current rateset */
2028         err = wl_dev_ioctl(dev, WLC_GET_CURR_RATESET, &rateset,
2029                         sizeof(rateset));
2030         if (unlikely(err)) {
2031                 WL_ERR("could not get current rateset (%d)\n", err);
2032                 goto done;
2033         }
2034
2035         rateset.count = le32_to_cpu(rateset.count);
2036
2037         legacy = wl_find_msb(mask->control[IEEE80211_BAND_2GHZ].legacy);
2038         if (!legacy)
2039                 legacy = wl_find_msb(mask->control[IEEE80211_BAND_5GHZ].legacy);
2040
2041         val = wl_g_rates[legacy - 1].bitrate * 100000;
2042
2043         if (val < rateset.count)
2044                 /* Select rate by rateset index */
2045                 rate = rateset.rates[val] & 0x7f;
2046         else
2047                 /* Specified rate in bps */
2048                 rate = val / 500000;
2049
2050         WL_CONN("rate %d mbps\n", rate / 2);
2051
2052         /*
2053          *
2054          *      Set rate override,
2055          *      Since the is a/b/g-blind, both a/bg_rate are enforced.
2056          */
2057         err_bg = wl_dev_intvar_set(dev, "bg_rate", rate);
2058         err_a = wl_dev_intvar_set(dev, "a_rate", rate);
2059         if (unlikely(err_bg && err_a)) {
2060                 WL_ERR("could not set fixed rate (%d) (%d)\n", err_bg, err_a);
2061                 err = err_bg | err_a;
2062         }
2063
2064 done:
2065         WL_TRACE("Exit\n");
2066         return err;
2067 }
2068
2069 static s32 wl_cfg80211_resume(struct wiphy *wiphy)
2070 {
2071         struct wl_priv *wl = wiphy_to_wl(wiphy);
2072
2073         /*
2074          * Check for WL_STATUS_READY before any function call which
2075          * could result is bus access. Don't block the resume for
2076          * any driver error conditions
2077          */
2078         WL_TRACE("Enter\n");
2079
2080 #if defined(CONFIG_PM_SLEEP)
2081         atomic_set(&brcmf_mmc_suspend, false);
2082 #endif  /*  defined(CONFIG_PM_SLEEP) */
2083
2084         if (test_bit(WL_STATUS_READY, &wl->status))
2085                 wl_invoke_iscan(wiphy_to_wl(wiphy));
2086
2087         WL_TRACE("Exit\n");
2088         return 0;
2089 }
2090
2091 static s32 wl_cfg80211_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wow)
2092 {
2093         struct wl_priv *wl = wiphy_to_wl(wiphy);
2094         struct net_device *ndev = wl_to_ndev(wl);
2095
2096         WL_TRACE("Enter\n");
2097
2098         /*
2099          * Check for WL_STATUS_READY before any function call which
2100          * could result is bus access. Don't block the suspend for
2101          * any driver error conditions
2102          */
2103
2104         /*
2105          * While going to suspend if associated with AP disassociate
2106          * from AP to save power while system is in suspended state
2107          */
2108         if ((test_bit(WL_STATUS_CONNECTED, &wl->status) ||
2109              test_bit(WL_STATUS_CONNECTING, &wl->status)) &&
2110              test_bit(WL_STATUS_READY, &wl->status)) {
2111                 WL_INFO("Disassociating from AP"
2112                         " while entering suspend state\n");
2113                 wl_link_down(wl);
2114
2115                 /*
2116                  * Make sure WPA_Supplicant receives all the event
2117                  * generated due to DISASSOC call to the fw to keep
2118                  * the state fw and WPA_Supplicant state consistent
2119                  */
2120                 rtnl_unlock();
2121                 wl_delay(500);
2122                 rtnl_lock();
2123         }
2124
2125         set_bit(WL_STATUS_SCAN_ABORTING, &wl->status);
2126         if (test_bit(WL_STATUS_READY, &wl->status))
2127                 wl_term_iscan(wl);
2128
2129         if (wl->scan_request) {
2130                 /* Indidate scan abort to cfg80211 layer */
2131                 WL_INFO("Terminating scan in progress\n");
2132                 cfg80211_scan_done(wl->scan_request, true);
2133                 wl->scan_request = NULL;
2134         }
2135         clear_bit(WL_STATUS_SCANNING, &wl->status);
2136         clear_bit(WL_STATUS_SCAN_ABORTING, &wl->status);
2137
2138         /* Turn off watchdog timer */
2139         if (test_bit(WL_STATUS_READY, &wl->status)) {
2140                 WL_INFO("Enable MPC\n");
2141                 wl_set_mpc(ndev, 1);
2142         }
2143
2144 #if defined(CONFIG_PM_SLEEP)
2145         atomic_set(&brcmf_mmc_suspend, true);
2146 #endif  /*  defined(CONFIG_PM_SLEEP) */
2147
2148         WL_TRACE("Exit\n");
2149
2150         return 0;
2151 }
2152
2153 static __used s32
2154 wl_update_pmklist(struct net_device *dev, struct wl_pmk_list *pmk_list,
2155                   s32 err)
2156 {
2157         int i, j;
2158
2159         WL_CONN("No of elements %d\n", pmk_list->pmkids.npmkid);
2160         for (i = 0; i < pmk_list->pmkids.npmkid; i++) {
2161                 WL_CONN("PMKID[%d]: %pM =\n", i,
2162                         &pmk_list->pmkids.pmkid[i].BSSID);
2163                 for (j = 0; j < WLAN_PMKID_LEN; j++)
2164                         WL_CONN("%02x\n", pmk_list->pmkids.pmkid[i].PMKID[j]);
2165         }
2166
2167         if (likely(!err))
2168                 wl_dev_bufvar_set(dev, "pmkid_info", (char *)pmk_list,
2169                                         sizeof(*pmk_list));
2170
2171         return err;
2172 }
2173
2174 static s32
2175 wl_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *dev,
2176                       struct cfg80211_pmksa *pmksa)
2177 {
2178         struct wl_priv *wl = wiphy_to_wl(wiphy);
2179         s32 err = 0;
2180         int i;
2181
2182         WL_TRACE("Enter\n");
2183         CHECK_SYS_UP();
2184
2185         for (i = 0; i < wl->pmk_list->pmkids.npmkid; i++)
2186                 if (!memcmp(pmksa->bssid, &wl->pmk_list->pmkids.pmkid[i].BSSID,
2187                             ETH_ALEN))
2188                         break;
2189         if (i < WL_NUM_PMKIDS_MAX) {
2190                 memcpy(&wl->pmk_list->pmkids.pmkid[i].BSSID, pmksa->bssid,
2191                        ETH_ALEN);
2192                 memcpy(&wl->pmk_list->pmkids.pmkid[i].PMKID, pmksa->pmkid,
2193                        WLAN_PMKID_LEN);
2194                 if (i == wl->pmk_list->pmkids.npmkid)
2195                         wl->pmk_list->pmkids.npmkid++;
2196         } else
2197                 err = -EINVAL;
2198
2199         WL_CONN("set_pmksa,IW_PMKSA_ADD - PMKID: %pM =\n",
2200                &wl->pmk_list->pmkids.pmkid[wl->pmk_list->pmkids.npmkid].BSSID);
2201         for (i = 0; i < WLAN_PMKID_LEN; i++)
2202                 WL_CONN("%02x\n",
2203                        wl->pmk_list->pmkids.pmkid[wl->pmk_list->pmkids.npmkid].
2204                        PMKID[i]);
2205
2206         err = wl_update_pmklist(dev, wl->pmk_list, err);
2207
2208         WL_TRACE("Exit\n");
2209         return err;
2210 }
2211
2212 static s32
2213 wl_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *dev,
2214                       struct cfg80211_pmksa *pmksa)
2215 {
2216         struct wl_priv *wl = wiphy_to_wl(wiphy);
2217         struct _pmkid_list pmkid;
2218         s32 err = 0;
2219         int i;
2220
2221         WL_TRACE("Enter\n");
2222         CHECK_SYS_UP();
2223         memcpy(&pmkid.pmkid[0].BSSID, pmksa->bssid, ETH_ALEN);
2224         memcpy(&pmkid.pmkid[0].PMKID, pmksa->pmkid, WLAN_PMKID_LEN);
2225
2226         WL_CONN("del_pmksa,IW_PMKSA_REMOVE - PMKID: %pM =\n",
2227                &pmkid.pmkid[0].BSSID);
2228         for (i = 0; i < WLAN_PMKID_LEN; i++)
2229                 WL_CONN("%02x\n", pmkid.pmkid[0].PMKID[i]);
2230
2231         for (i = 0; i < wl->pmk_list->pmkids.npmkid; i++)
2232                 if (!memcmp
2233                     (pmksa->bssid, &wl->pmk_list->pmkids.pmkid[i].BSSID,
2234                      ETH_ALEN))
2235                         break;
2236
2237         if ((wl->pmk_list->pmkids.npmkid > 0)
2238             && (i < wl->pmk_list->pmkids.npmkid)) {
2239                 memset(&wl->pmk_list->pmkids.pmkid[i], 0, sizeof(pmkid_t));
2240                 for (; i < (wl->pmk_list->pmkids.npmkid - 1); i++) {
2241                         memcpy(&wl->pmk_list->pmkids.pmkid[i].BSSID,
2242                                &wl->pmk_list->pmkids.pmkid[i + 1].BSSID,
2243                                ETH_ALEN);
2244                         memcpy(&wl->pmk_list->pmkids.pmkid[i].PMKID,
2245                                &wl->pmk_list->pmkids.pmkid[i + 1].PMKID,
2246                                WLAN_PMKID_LEN);
2247                 }
2248                 wl->pmk_list->pmkids.npmkid--;
2249         } else
2250                 err = -EINVAL;
2251
2252         err = wl_update_pmklist(dev, wl->pmk_list, err);
2253
2254         WL_TRACE("Exit\n");
2255         return err;
2256
2257 }
2258
2259 static s32
2260 wl_cfg80211_flush_pmksa(struct wiphy *wiphy, struct net_device *dev)
2261 {
2262         struct wl_priv *wl = wiphy_to_wl(wiphy);
2263         s32 err = 0;
2264
2265         WL_TRACE("Enter\n");
2266         CHECK_SYS_UP();
2267
2268         memset(wl->pmk_list, 0, sizeof(*wl->pmk_list));
2269         err = wl_update_pmklist(dev, wl->pmk_list, err);
2270
2271         WL_TRACE("Exit\n");
2272         return err;
2273
2274 }
2275
2276 static struct cfg80211_ops wl_cfg80211_ops = {
2277         .change_virtual_intf = wl_cfg80211_change_iface,
2278         .scan = wl_cfg80211_scan,
2279         .set_wiphy_params = wl_cfg80211_set_wiphy_params,
2280         .join_ibss = wl_cfg80211_join_ibss,
2281         .leave_ibss = wl_cfg80211_leave_ibss,
2282         .get_station = wl_cfg80211_get_station,
2283         .set_tx_power = wl_cfg80211_set_tx_power,
2284         .get_tx_power = wl_cfg80211_get_tx_power,
2285         .add_key = wl_cfg80211_add_key,
2286         .del_key = wl_cfg80211_del_key,
2287         .get_key = wl_cfg80211_get_key,
2288         .set_default_key = wl_cfg80211_config_default_key,
2289         .set_default_mgmt_key = wl_cfg80211_config_default_mgmt_key,
2290         .set_power_mgmt = wl_cfg80211_set_power_mgmt,
2291         .set_bitrate_mask = wl_cfg80211_set_bitrate_mask,
2292         .connect = wl_cfg80211_connect,
2293         .disconnect = wl_cfg80211_disconnect,
2294         .suspend = wl_cfg80211_suspend,
2295         .resume = wl_cfg80211_resume,
2296         .set_pmksa = wl_cfg80211_set_pmksa,
2297         .del_pmksa = wl_cfg80211_del_pmksa,
2298         .flush_pmksa = wl_cfg80211_flush_pmksa
2299 };
2300
2301 static s32 wl_mode_to_nl80211_iftype(s32 mode)
2302 {
2303         s32 err = 0;
2304
2305         switch (mode) {
2306         case WL_MODE_BSS:
2307                 return NL80211_IFTYPE_STATION;
2308         case WL_MODE_IBSS:
2309                 return NL80211_IFTYPE_ADHOC;
2310         default:
2311                 return NL80211_IFTYPE_UNSPECIFIED;
2312         }
2313
2314         return err;
2315 }
2316
2317 static struct wireless_dev *wl_alloc_wdev(s32 sizeof_iface,
2318                                           struct device *dev)
2319 {
2320         struct wireless_dev *wdev;
2321         s32 err = 0;
2322
2323         wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
2324         if (unlikely(!wdev)) {
2325                 WL_ERR("Could not allocate wireless device\n");
2326                 return ERR_PTR(-ENOMEM);
2327         }
2328         wdev->wiphy =
2329             wiphy_new(&wl_cfg80211_ops, sizeof(struct wl_priv) + sizeof_iface);
2330         if (unlikely(!wdev->wiphy)) {
2331                 WL_ERR("Couldn not allocate wiphy device\n");
2332                 err = -ENOMEM;
2333                 goto wiphy_new_out;
2334         }
2335         set_wiphy_dev(wdev->wiphy, dev);
2336         wdev->wiphy->max_scan_ssids = WL_NUM_SCAN_MAX;
2337         wdev->wiphy->max_num_pmkids = WL_NUM_PMKIDS_MAX;
2338         wdev->wiphy->interface_modes =
2339             BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
2340         wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &__wl_band_2ghz;
2341         wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &__wl_band_5ghz_a;    /* Set
2342                                                 * it as 11a by default.
2343                                                 * This will be updated with
2344                                                 * 11n phy tables in
2345                                                 * "ifconfig up"
2346                                                 * if phy has 11n capability
2347                                                 */
2348         wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2349         wdev->wiphy->cipher_suites = __wl_cipher_suites;
2350         wdev->wiphy->n_cipher_suites = ARRAY_SIZE(__wl_cipher_suites);
2351 #ifndef WL_POWERSAVE_DISABLED
2352         wdev->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;      /* enable power
2353                                                                  * save mode
2354                                                                  * by default
2355                                                                  */
2356 #else
2357         wdev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2358 #endif                          /* !WL_POWERSAVE_DISABLED */
2359         err = wiphy_register(wdev->wiphy);
2360         if (unlikely(err < 0)) {
2361                 WL_ERR("Couldn not register wiphy device (%d)\n", err);
2362                 goto wiphy_register_out;
2363         }
2364         return wdev;
2365
2366 wiphy_register_out:
2367         wiphy_free(wdev->wiphy);
2368
2369 wiphy_new_out:
2370         kfree(wdev);
2371
2372         return ERR_PTR(err);
2373 }
2374
2375 static void wl_free_wdev(struct wl_priv *wl)
2376 {
2377         struct wireless_dev *wdev = wl_to_wdev(wl);
2378
2379         if (unlikely(!wdev)) {
2380                 WL_ERR("wdev is invalid\n");
2381                 return;
2382         }
2383         wiphy_unregister(wdev->wiphy);
2384         wiphy_free(wdev->wiphy);
2385         kfree(wdev);
2386         wl_to_wdev(wl) = NULL;
2387 }
2388
2389 static s32 wl_inform_bss(struct wl_priv *wl)
2390 {
2391         struct wl_scan_results *bss_list;
2392         struct wl_bss_info *bi = NULL;  /* must be initialized */
2393         s32 err = 0;
2394         int i;
2395
2396         bss_list = wl->bss_list;
2397         if (unlikely(bss_list->version != WL_BSS_INFO_VERSION)) {
2398                 WL_ERR("Version %d != WL_BSS_INFO_VERSION\n",
2399                        bss_list->version);
2400                 return -EOPNOTSUPP;
2401         }
2402         WL_SCAN("scanned AP count (%d)\n", bss_list->count);
2403         bi = next_bss(bss_list, bi);
2404         for_each_bss(bss_list, bi, i) {
2405                 err = wl_inform_single_bss(wl, bi);
2406                 if (unlikely(err))
2407                         break;
2408         }
2409         return err;
2410 }
2411
2412
2413 static s32 wl_inform_single_bss(struct wl_priv *wl, struct wl_bss_info *bi)
2414 {
2415         struct wiphy *wiphy = wl_to_wiphy(wl);
2416         struct ieee80211_channel *notify_channel;
2417         struct cfg80211_bss *bss;
2418         struct ieee80211_supported_band *band;
2419         s32 err = 0;
2420         u16 channel;
2421         u32 freq;
2422         u64 notify_timestamp;
2423         u16 notify_capability;
2424         u16 notify_interval;
2425         u8 *notify_ie;
2426         size_t notify_ielen;
2427         s32 notify_signal;
2428
2429         if (unlikely(le32_to_cpu(bi->length) > WL_BSS_INFO_MAX)) {
2430                 WL_ERR("Bss info is larger than buffer. Discarding\n");
2431                 return 0;
2432         }
2433
2434         channel = bi->ctl_ch ? bi->ctl_ch :
2435                                 CHSPEC_CHANNEL(le16_to_cpu(bi->chanspec));
2436
2437         if (channel <= CH_MAX_2G_CHANNEL)
2438                 band = wiphy->bands[IEEE80211_BAND_2GHZ];
2439         else
2440                 band = wiphy->bands[IEEE80211_BAND_5GHZ];
2441
2442         freq = ieee80211_channel_to_frequency(channel, band->band);
2443         notify_channel = ieee80211_get_channel(wiphy, freq);
2444
2445         notify_timestamp = jiffies_to_msecs(jiffies)*1000; /* uSec */
2446         notify_capability = le16_to_cpu(bi->capability);
2447         notify_interval = le16_to_cpu(bi->beacon_period);
2448         notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
2449         notify_ielen = le16_to_cpu(bi->ie_length);
2450         notify_signal = (s16)le16_to_cpu(bi->RSSI) * 100;
2451
2452         WL_CONN("bssid: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
2453                         bi->BSSID[0], bi->BSSID[1], bi->BSSID[2],
2454                         bi->BSSID[3], bi->BSSID[4], bi->BSSID[5]);
2455         WL_CONN("Channel: %d(%d)\n", channel, freq);
2456         WL_CONN("Capability: %X\n", notify_capability);
2457         WL_CONN("Beacon interval: %d\n", notify_interval);
2458         WL_CONN("Signal: %d\n", notify_signal);
2459         WL_CONN("notify_timestamp: %#018llx\n", notify_timestamp);
2460
2461         bss = cfg80211_inform_bss(wiphy, notify_channel, (const u8 *)bi->BSSID,
2462                 notify_timestamp, notify_capability, notify_interval, notify_ie,
2463                 notify_ielen, notify_signal, GFP_KERNEL);
2464
2465         if (unlikely(!bss)) {
2466                 WL_ERR("cfg80211_inform_bss_frame error\n");
2467                 return -EINVAL;
2468         }
2469
2470         return err;
2471 }
2472
2473 static s32
2474 wl_inform_ibss(struct wl_priv *wl, struct net_device *dev, const u8 *bssid)
2475 {
2476         struct wiphy *wiphy = wl_to_wiphy(wl);
2477         struct ieee80211_channel *notify_channel;
2478         struct wl_bss_info *bi = NULL;
2479         struct ieee80211_supported_band *band;
2480         u8 *buf = NULL;
2481         s32 err = 0;
2482         u16 channel;
2483         u32 freq;
2484         u64 notify_timestamp;
2485         u16 notify_capability;
2486         u16 notify_interval;
2487         u8 *notify_ie;
2488         size_t notify_ielen;
2489         s32 notify_signal;
2490
2491         WL_TRACE("Enter\n");
2492
2493         buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
2494         if (buf == NULL) {
2495                 WL_ERR("kzalloc() failed\n");
2496                 err = -ENOMEM;
2497                 goto CleanUp;
2498         }
2499
2500         *(u32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
2501
2502         err = wl_dev_ioctl(dev, BRCMF_C_GET_BSS_INFO, buf, WL_BSS_INFO_MAX);
2503         if (unlikely(err)) {
2504                 WL_ERR("WLC_GET_BSS_INFO failed: %d\n", err);
2505                 goto CleanUp;
2506         }
2507
2508         bi = (wl_bss_info_t *)(buf + 4);
2509
2510         channel = bi->ctl_ch ? bi->ctl_ch :
2511                                 CHSPEC_CHANNEL(le16_to_cpu(bi->chanspec));
2512
2513         if (channel <= CH_MAX_2G_CHANNEL)
2514                 band = wiphy->bands[IEEE80211_BAND_2GHZ];
2515         else
2516                 band = wiphy->bands[IEEE80211_BAND_5GHZ];
2517
2518         freq = ieee80211_channel_to_frequency(channel, band->band);
2519         notify_channel = ieee80211_get_channel(wiphy, freq);
2520
2521         notify_timestamp = jiffies_to_msecs(jiffies)*1000; /* uSec */
2522         notify_capability = le16_to_cpu(bi->capability);
2523         notify_interval = le16_to_cpu(bi->beacon_period);
2524         notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
2525         notify_ielen = le16_to_cpu(bi->ie_length);
2526         notify_signal = (s16)le16_to_cpu(bi->RSSI) * 100;
2527
2528         WL_CONN("channel: %d(%d)\n", channel, freq);
2529         WL_CONN("capability: %X\n", notify_capability);
2530         WL_CONN("beacon interval: %d\n", notify_interval);
2531         WL_CONN("signal: %d\n", notify_signal);
2532         WL_CONN("notify_timestamp: %#018llx\n", notify_timestamp);
2533
2534         cfg80211_inform_bss(wiphy, notify_channel, bssid,
2535                 notify_timestamp, notify_capability, notify_interval,
2536                 notify_ie, notify_ielen, notify_signal, GFP_KERNEL);
2537
2538 CleanUp:
2539
2540         kfree(buf);
2541
2542         WL_TRACE("Exit\n");
2543
2544         return err;
2545 }
2546
2547 static bool wl_is_linkup(struct wl_priv *wl, const brcmf_event_msg_t *e)
2548 {
2549         u32 event = be32_to_cpu(e->event_type);
2550         u32 status = be32_to_cpu(e->status);
2551
2552         if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
2553                 WL_CONN("Processing set ssid\n");
2554                 wl->link_up = true;
2555                 return true;
2556         }
2557
2558         return false;
2559 }
2560
2561 static bool wl_is_linkdown(struct wl_priv *wl, const brcmf_event_msg_t *e)
2562 {
2563         u32 event = be32_to_cpu(e->event_type);
2564         u16 flags = be16_to_cpu(e->flags);
2565
2566         if (event == BRCMF_E_LINK && (!(flags & BRCMF_EVENT_MSG_LINK))) {
2567                 WL_CONN("Processing link down\n");
2568                 return true;
2569         }
2570         return false;
2571 }
2572
2573 static bool wl_is_nonetwork(struct wl_priv *wl, const brcmf_event_msg_t *e)
2574 {
2575         u32 event = be32_to_cpu(e->event_type);
2576         u32 status = be32_to_cpu(e->status);
2577
2578         if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
2579                 WL_CONN("Processing Link %s & no network found\n",
2580                                 be16_to_cpu(e->flags) & BRCMF_EVENT_MSG_LINK ?
2581                                 "up" : "down");
2582                 return true;
2583         }
2584
2585         if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
2586                 WL_CONN("Processing connecting & no network found\n");
2587                 return true;
2588         }
2589
2590         return false;
2591 }
2592
2593 static s32
2594 wl_notify_connect_status(struct wl_priv *wl, struct net_device *ndev,
2595                          const brcmf_event_msg_t *e, void *data)
2596 {
2597         s32 err = 0;
2598
2599         if (wl_is_linkup(wl, e)) {
2600                 WL_CONN("Linkup\n");
2601                 if (wl_is_ibssmode(wl)) {
2602                         wl_update_prof(wl, NULL, (void *)e->addr,
2603                                 WL_PROF_BSSID);
2604                         wl_inform_ibss(wl, ndev, e->addr);
2605                         cfg80211_ibss_joined(ndev, e->addr, GFP_KERNEL);
2606                         clear_bit(WL_STATUS_CONNECTING, &wl->status);
2607                         set_bit(WL_STATUS_CONNECTED, &wl->status);
2608                 } else
2609                         wl_bss_connect_done(wl, ndev, e, data, true);
2610         } else if (wl_is_linkdown(wl, e)) {
2611                 WL_CONN("Linkdown\n");
2612                 if (wl_is_ibssmode(wl)) {
2613                         clear_bit(WL_STATUS_CONNECTING, &wl->status);
2614                         if (test_and_clear_bit(WL_STATUS_CONNECTED,
2615                                 &wl->status))
2616                                 wl_link_down(wl);
2617                 } else {
2618                         wl_bss_connect_done(wl, ndev, e, data, false);
2619                         if (test_and_clear_bit(WL_STATUS_CONNECTED,
2620                                 &wl->status)) {
2621                                 cfg80211_disconnected(ndev, 0, NULL, 0,
2622                                         GFP_KERNEL);
2623                                 wl_link_down(wl);
2624                         }
2625                 }
2626                 wl_init_prof(wl->profile);
2627         } else if (wl_is_nonetwork(wl, e)) {
2628                 if (wl_is_ibssmode(wl))
2629                         clear_bit(WL_STATUS_CONNECTING, &wl->status);
2630                 else
2631                         wl_bss_connect_done(wl, ndev, e, data, false);
2632         }
2633
2634         return err;
2635 }
2636
2637 static s32
2638 wl_notify_roaming_status(struct wl_priv *wl, struct net_device *ndev,
2639                          const brcmf_event_msg_t *e, void *data)
2640 {
2641         s32 err = 0;
2642         u32 event = be32_to_cpu(e->event_type);
2643         u32 status = be32_to_cpu(e->status);
2644
2645         if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
2646                 if (test_bit(WL_STATUS_CONNECTED, &wl->status))
2647                         wl_bss_roaming_done(wl, ndev, e, data);
2648                 else
2649                         wl_bss_connect_done(wl, ndev, e, data, true);
2650         }
2651
2652         return err;
2653 }
2654
2655 static __used s32
2656 wl_dev_bufvar_set(struct net_device *dev, s8 *name, s8 *buf, s32 len)
2657 {
2658         struct wl_priv *wl = ndev_to_wl(dev);
2659         u32 buflen;
2660
2661         buflen = brcmu_mkiovar(name, buf, len, wl->ioctl_buf, WL_IOCTL_LEN_MAX);
2662         BUG_ON(!buflen);
2663
2664         return wl_dev_ioctl(dev, BRCMF_C_SET_VAR, wl->ioctl_buf, buflen);
2665 }
2666
2667 static s32
2668 wl_dev_bufvar_get(struct net_device *dev, s8 *name, s8 *buf,
2669                   s32 buf_len)
2670 {
2671         struct wl_priv *wl = ndev_to_wl(dev);
2672         u32 len;
2673         s32 err = 0;
2674
2675         len = brcmu_mkiovar(name, NULL, 0, wl->ioctl_buf, WL_IOCTL_LEN_MAX);
2676         BUG_ON(!len);
2677         err = wl_dev_ioctl(dev, BRCMF_C_GET_VAR, (void *)wl->ioctl_buf,
2678                         WL_IOCTL_LEN_MAX);
2679         if (unlikely(err)) {
2680                 WL_ERR("error (%d)\n", err);
2681                 return err;
2682         }
2683         memcpy(buf, wl->ioctl_buf, buf_len);
2684
2685         return err;
2686 }
2687
2688 static s32 wl_get_assoc_ies(struct wl_priv *wl)
2689 {
2690         struct net_device *ndev = wl_to_ndev(wl);
2691         struct wl_assoc_ielen *assoc_info;
2692         struct wl_connect_info *conn_info = wl_to_conn(wl);
2693         u32 req_len;
2694         u32 resp_len;
2695         s32 err = 0;
2696
2697         wl_clear_assoc_ies(wl);
2698
2699         err = wl_dev_bufvar_get(ndev, "assoc_info", wl->extra_buf,
2700                                 WL_ASSOC_INFO_MAX);
2701         if (unlikely(err)) {
2702                 WL_ERR("could not get assoc info (%d)\n", err);
2703                 return err;
2704         }
2705         assoc_info = (struct wl_assoc_ielen *)wl->extra_buf;
2706         req_len = assoc_info->req_len;
2707         resp_len = assoc_info->resp_len;
2708         if (req_len) {
2709                 err = wl_dev_bufvar_get(ndev, "assoc_req_ies", wl->extra_buf,
2710                                         WL_ASSOC_INFO_MAX);
2711                 if (unlikely(err)) {
2712                         WL_ERR("could not get assoc req (%d)\n", err);
2713                         return err;
2714                 }
2715                 conn_info->req_ie_len = req_len;
2716                 conn_info->req_ie =
2717                     kmemdup(wl->extra_buf, conn_info->req_ie_len, GFP_KERNEL);
2718         } else {
2719                 conn_info->req_ie_len = 0;
2720                 conn_info->req_ie = NULL;
2721         }
2722         if (resp_len) {
2723                 err = wl_dev_bufvar_get(ndev, "assoc_resp_ies", wl->extra_buf,
2724                                         WL_ASSOC_INFO_MAX);
2725                 if (unlikely(err)) {
2726                         WL_ERR("could not get assoc resp (%d)\n", err);
2727                         return err;
2728                 }
2729                 conn_info->resp_ie_len = resp_len;
2730                 conn_info->resp_ie =
2731                     kmemdup(wl->extra_buf, conn_info->resp_ie_len, GFP_KERNEL);
2732         } else {
2733                 conn_info->resp_ie_len = 0;
2734                 conn_info->resp_ie = NULL;
2735         }
2736         WL_CONN("req len (%d) resp len (%d)\n",
2737                conn_info->req_ie_len, conn_info->resp_ie_len);
2738
2739         return err;
2740 }
2741
2742 static void wl_clear_assoc_ies(struct wl_priv *wl)
2743 {
2744         struct wl_connect_info *conn_info = wl_to_conn(wl);
2745
2746         kfree(conn_info->req_ie);
2747         conn_info->req_ie = NULL;
2748         conn_info->req_ie_len = 0;
2749         kfree(conn_info->resp_ie);
2750         conn_info->resp_ie = NULL;
2751         conn_info->resp_ie_len = 0;
2752 }
2753
2754
2755 static void wl_ch_to_chanspec(int ch, struct wl_join_params *join_params,
2756         size_t *join_params_size)
2757 {
2758         chanspec_t chanspec = 0;
2759
2760         if (ch != 0) {
2761                 join_params->params.chanspec_num = 1;
2762                 join_params->params.chanspec_list[0] = ch;
2763
2764                 if (join_params->params.chanspec_list[0] <= CH_MAX_2G_CHANNEL)
2765                         chanspec |= WL_CHANSPEC_BAND_2G;
2766                 else
2767                         chanspec |= WL_CHANSPEC_BAND_5G;
2768
2769                 chanspec |= WL_CHANSPEC_BW_20;
2770                 chanspec |= WL_CHANSPEC_CTL_SB_NONE;
2771
2772                 *join_params_size += WL_ASSOC_PARAMS_FIXED_SIZE +
2773                         join_params->params.chanspec_num * sizeof(chanspec_t);
2774
2775                 join_params->params.chanspec_list[0] &= WL_CHANSPEC_CHAN_MASK;
2776                 join_params->params.chanspec_list[0] |= chanspec;
2777                 join_params->params.chanspec_list[0] =
2778                 cpu_to_le16(join_params->params.chanspec_list[0]);
2779
2780                 join_params->params.chanspec_num =
2781                         cpu_to_le32(join_params->params.chanspec_num);
2782
2783                 WL_CONN("join_params->params.chanspec_list[0]= %#X,"
2784                         "channel %d, chanspec %#X\n",
2785                        join_params->params.chanspec_list[0], ch, chanspec);
2786         }
2787 }
2788
2789 static s32 wl_update_bss_info(struct wl_priv *wl)
2790 {
2791         struct wl_bss_info *bi;
2792         struct wlc_ssid *ssid;
2793         struct brcmu_tlv *tim;
2794         u16 beacon_interval;
2795         u8 dtim_period;
2796         size_t ie_len;
2797         u8 *ie;
2798         s32 err = 0;
2799
2800         WL_TRACE("Enter\n");
2801         if (wl_is_ibssmode(wl))
2802                 return err;
2803
2804         ssid = (struct wlc_ssid *)wl_read_prof(wl, WL_PROF_SSID);
2805
2806         *(u32 *)wl->extra_buf = cpu_to_le32(WL_EXTRA_BUF_MAX);
2807         err = wl_dev_ioctl(wl_to_ndev(wl), BRCMF_C_GET_BSS_INFO,
2808                         wl->extra_buf, WL_EXTRA_BUF_MAX);
2809         if (unlikely(err)) {
2810                 WL_ERR("Could not get bss info %d\n", err);
2811                 goto update_bss_info_out;
2812         }
2813
2814         bi = (struct wl_bss_info *)(wl->extra_buf + 4);
2815         err = wl_inform_single_bss(wl, bi);
2816         if (unlikely(err))
2817                 goto update_bss_info_out;
2818
2819         ie = ((u8 *)bi) + bi->ie_offset;
2820         ie_len = bi->ie_length;
2821         beacon_interval = cpu_to_le16(bi->beacon_period);
2822
2823         tim = brcmu_parse_tlvs(ie, ie_len, WLAN_EID_TIM);
2824         if (tim)
2825                 dtim_period = tim->data[1];
2826         else {
2827                 /*
2828                 * active scan was done so we could not get dtim
2829                 * information out of probe response.
2830                 * so we speficially query dtim information to dongle.
2831                 */
2832                 u32 var;
2833                 err = wl_dev_intvar_get(wl_to_ndev(wl), "dtim_assoc", &var);
2834                 if (unlikely(err)) {
2835                         WL_ERR("wl dtim_assoc failed (%d)\n", err);
2836                         goto update_bss_info_out;
2837                 }
2838                 dtim_period = (u8)var;
2839         }
2840
2841         wl_update_prof(wl, NULL, &beacon_interval, WL_PROF_BEACONINT);
2842         wl_update_prof(wl, NULL, &dtim_period, WL_PROF_DTIMPERIOD);
2843
2844 update_bss_info_out:
2845         WL_TRACE("Exit");
2846         return err;
2847 }
2848
2849 static s32
2850 wl_bss_roaming_done(struct wl_priv *wl, struct net_device *ndev,
2851                     const brcmf_event_msg_t *e, void *data)
2852 {
2853         struct wl_connect_info *conn_info = wl_to_conn(wl);
2854         s32 err = 0;
2855
2856         WL_TRACE("Enter\n");
2857
2858         wl_get_assoc_ies(wl);
2859         wl_update_prof(wl, NULL, &e->addr, WL_PROF_BSSID);
2860         wl_update_bss_info(wl);
2861
2862         cfg80211_roamed(ndev, NULL,
2863                         (u8 *)wl_read_prof(wl, WL_PROF_BSSID),
2864                         conn_info->req_ie, conn_info->req_ie_len,
2865                         conn_info->resp_ie, conn_info->resp_ie_len, GFP_KERNEL);
2866         WL_CONN("Report roaming result\n");
2867
2868         set_bit(WL_STATUS_CONNECTED, &wl->status);
2869         WL_TRACE("Exit\n");
2870         return err;
2871 }
2872
2873 static s32
2874 wl_bss_connect_done(struct wl_priv *wl, struct net_device *ndev,
2875                     const brcmf_event_msg_t *e, void *data, bool completed)
2876 {
2877         struct wl_connect_info *conn_info = wl_to_conn(wl);
2878         s32 err = 0;
2879
2880         WL_TRACE("Enter\n");
2881
2882         if (test_and_clear_bit(WL_STATUS_CONNECTING, &wl->status)) {
2883                 if (completed) {
2884                         wl_get_assoc_ies(wl);
2885                         wl_update_prof(wl, NULL, &e->addr, WL_PROF_BSSID);
2886                         wl_update_bss_info(wl);
2887                 }
2888                 cfg80211_connect_result(ndev,
2889                                         (u8 *)wl_read_prof(wl, WL_PROF_BSSID),
2890                                         conn_info->req_ie,
2891                                         conn_info->req_ie_len,
2892                                         conn_info->resp_ie,
2893                                         conn_info->resp_ie_len,
2894                                         completed ? WLAN_STATUS_SUCCESS : WLAN_STATUS_AUTH_TIMEOUT,
2895                                         GFP_KERNEL);
2896                 if (completed)
2897                         set_bit(WL_STATUS_CONNECTED, &wl->status);
2898                 WL_CONN("Report connect result - connection %s\n",
2899                                 completed ? "succeeded" : "failed");
2900         }
2901         WL_TRACE("Exit\n");
2902         return err;
2903 }
2904
2905 static s32
2906 wl_notify_mic_status(struct wl_priv *wl, struct net_device *ndev,
2907                      const brcmf_event_msg_t *e, void *data)
2908 {
2909         u16 flags = be16_to_cpu(e->flags);
2910         enum nl80211_key_type key_type;
2911
2912         rtnl_lock();
2913         if (flags & BRCMF_EVENT_MSG_GROUP)
2914                 key_type = NL80211_KEYTYPE_GROUP;
2915         else
2916                 key_type = NL80211_KEYTYPE_PAIRWISE;
2917
2918         cfg80211_michael_mic_failure(ndev, (u8 *)&e->addr, key_type, -1,
2919                                      NULL, GFP_KERNEL);
2920         rtnl_unlock();
2921
2922         return 0;
2923 }
2924
2925 static s32
2926 wl_notify_scan_status(struct wl_priv *wl, struct net_device *ndev,
2927                       const brcmf_event_msg_t *e, void *data)
2928 {
2929         struct channel_info channel_inform;
2930         struct wl_scan_results *bss_list;
2931         u32 len = WL_SCAN_BUF_MAX;
2932         s32 err = 0;
2933         bool scan_abort = false;
2934
2935         WL_TRACE("Enter\n");
2936
2937         if (wl->iscan_on && wl->iscan_kickstart) {
2938                 WL_TRACE("Exit\n");
2939                 return wl_wakeup_iscan(wl_to_iscan(wl));
2940         }
2941
2942         if (unlikely(!test_and_clear_bit(WL_STATUS_SCANNING, &wl->status))) {
2943                 WL_ERR("Scan complete while device not scanning\n");
2944                 scan_abort = true;
2945                 err = -EINVAL;
2946                 goto scan_done_out;
2947         }
2948
2949         err = wl_dev_ioctl(ndev, BRCMF_C_GET_CHANNEL, &channel_inform,
2950                         sizeof(channel_inform));
2951         if (unlikely(err)) {
2952                 WL_ERR("scan busy (%d)\n", err);
2953                 scan_abort = true;
2954                 goto scan_done_out;
2955         }
2956         channel_inform.scan_channel = le32_to_cpu(channel_inform.scan_channel);
2957         if (unlikely(channel_inform.scan_channel)) {
2958
2959                 WL_CONN("channel_inform.scan_channel (%d)\n",
2960                        channel_inform.scan_channel);
2961         }
2962         wl->bss_list = wl->scan_results;
2963         bss_list = wl->bss_list;
2964         memset(bss_list, 0, len);
2965         bss_list->buflen = cpu_to_le32(len);
2966
2967         err = wl_dev_ioctl(ndev, BRCMF_C_SCAN_RESULTS, bss_list, len);
2968         if (unlikely(err)) {
2969                 WL_ERR("%s Scan_results error (%d)\n", ndev->name, err);
2970                 err = -EINVAL;
2971                 scan_abort = true;
2972                 goto scan_done_out;
2973         }
2974         bss_list->buflen = le32_to_cpu(bss_list->buflen);
2975         bss_list->version = le32_to_cpu(bss_list->version);
2976         bss_list->count = le32_to_cpu(bss_list->count);
2977
2978         err = wl_inform_bss(wl);
2979         if (err) {
2980                 scan_abort = true;
2981                 goto scan_done_out;
2982         }
2983
2984 scan_done_out:
2985         if (wl->scan_request) {
2986                 WL_SCAN("calling cfg80211_scan_done\n");
2987                 cfg80211_scan_done(wl->scan_request, scan_abort);
2988                 wl_set_mpc(ndev, 1);
2989                 wl->scan_request = NULL;
2990         }
2991
2992         WL_TRACE("Exit\n");
2993
2994         return err;
2995 }
2996
2997 static void wl_init_conf(struct wl_conf *conf)
2998 {
2999         conf->mode = (u32)-1;
3000         conf->frag_threshold = (u32)-1;
3001         conf->rts_threshold = (u32)-1;
3002         conf->retry_short = (u32)-1;
3003         conf->retry_long = (u32)-1;
3004         conf->tx_power = -1;
3005 }
3006
3007 static void wl_init_prof(struct wl_profile *prof)
3008 {
3009         memset(prof, 0, sizeof(*prof));
3010 }
3011
3012 static void wl_init_eloop_handler(struct wl_event_loop *el)
3013 {
3014         memset(el, 0, sizeof(*el));
3015         el->handler[BRCMF_E_SCAN_COMPLETE] = wl_notify_scan_status;
3016         el->handler[BRCMF_E_LINK] = wl_notify_connect_status;
3017         el->handler[BRCMF_E_ROAM] = wl_notify_roaming_status;
3018         el->handler[BRCMF_E_MIC_ERROR] = wl_notify_mic_status;
3019         el->handler[BRCMF_E_SET_SSID] = wl_notify_connect_status;
3020 }
3021
3022 static s32 wl_init_priv_mem(struct wl_priv *wl)
3023 {
3024         wl->scan_results = kzalloc(WL_SCAN_BUF_MAX, GFP_KERNEL);
3025         if (unlikely(!wl->scan_results)) {
3026                 WL_ERR("Scan results alloc failed\n");
3027                 goto init_priv_mem_out;
3028         }
3029         wl->conf = kzalloc(sizeof(*wl->conf), GFP_KERNEL);
3030         if (unlikely(!wl->conf)) {
3031                 WL_ERR("wl_conf alloc failed\n");
3032                 goto init_priv_mem_out;
3033         }
3034         wl->profile = kzalloc(sizeof(*wl->profile), GFP_KERNEL);
3035         if (unlikely(!wl->profile)) {
3036                 WL_ERR("wl_profile alloc failed\n");
3037                 goto init_priv_mem_out;
3038         }
3039         wl->bss_info = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
3040         if (unlikely(!wl->bss_info)) {
3041                 WL_ERR("Bss information alloc failed\n");
3042                 goto init_priv_mem_out;
3043         }
3044         wl->scan_req_int = kzalloc(sizeof(*wl->scan_req_int), GFP_KERNEL);
3045         if (unlikely(!wl->scan_req_int)) {
3046                 WL_ERR("Scan req alloc failed\n");
3047                 goto init_priv_mem_out;
3048         }
3049         wl->ioctl_buf = kzalloc(WL_IOCTL_LEN_MAX, GFP_KERNEL);
3050         if (unlikely(!wl->ioctl_buf)) {
3051                 WL_ERR("Ioctl buf alloc failed\n");
3052                 goto init_priv_mem_out;
3053         }
3054         wl->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
3055         if (unlikely(!wl->extra_buf)) {
3056                 WL_ERR("Extra buf alloc failed\n");
3057                 goto init_priv_mem_out;
3058         }
3059         wl->iscan = kzalloc(sizeof(*wl->iscan), GFP_KERNEL);
3060         if (unlikely(!wl->iscan)) {
3061                 WL_ERR("Iscan buf alloc failed\n");
3062                 goto init_priv_mem_out;
3063         }
3064         wl->fw = kzalloc(sizeof(*wl->fw), GFP_KERNEL);
3065         if (unlikely(!wl->fw)) {
3066                 WL_ERR("fw object alloc failed\n");
3067                 goto init_priv_mem_out;
3068         }
3069         wl->pmk_list = kzalloc(sizeof(*wl->pmk_list), GFP_KERNEL);
3070         if (unlikely(!wl->pmk_list)) {
3071                 WL_ERR("pmk list alloc failed\n");
3072                 goto init_priv_mem_out;
3073         }
3074
3075         return 0;
3076
3077 init_priv_mem_out:
3078         wl_deinit_priv_mem(wl);
3079
3080         return -ENOMEM;
3081 }
3082
3083 static void wl_deinit_priv_mem(struct wl_priv *wl)
3084 {
3085         kfree(wl->scan_results);
3086         wl->scan_results = NULL;
3087         kfree(wl->bss_info);
3088         wl->bss_info = NULL;
3089         kfree(wl->conf);
3090         wl->conf = NULL;
3091         kfree(wl->profile);
3092         wl->profile = NULL;
3093         kfree(wl->scan_req_int);
3094         wl->scan_req_int = NULL;
3095         kfree(wl->ioctl_buf);
3096         wl->ioctl_buf = NULL;
3097         kfree(wl->extra_buf);
3098         wl->extra_buf = NULL;
3099         kfree(wl->iscan);
3100         wl->iscan = NULL;
3101         kfree(wl->fw);
3102         wl->fw = NULL;
3103         kfree(wl->pmk_list);
3104         wl->pmk_list = NULL;
3105 }
3106
3107 static s32 wl_create_event_handler(struct wl_priv *wl)
3108 {
3109         sema_init(&wl->event_sync, 0);
3110         wl->event_tsk = kthread_run(wl_event_handler, wl, "wl_event_handler");
3111         if (IS_ERR(wl->event_tsk)) {
3112                 wl->event_tsk = NULL;
3113                 WL_ERR("failed to create event thread\n");
3114                 return -ENOMEM;
3115         }
3116         return 0;
3117 }
3118
3119 static void wl_destroy_event_handler(struct wl_priv *wl)
3120 {
3121         if (wl->event_tsk) {
3122                 send_sig(SIGTERM, wl->event_tsk, 1);
3123                 kthread_stop(wl->event_tsk);
3124                 wl->event_tsk = NULL;
3125         }
3126 }
3127
3128 static void wl_term_iscan(struct wl_priv *wl)
3129 {
3130         struct wl_iscan_ctrl *iscan = wl_to_iscan(wl);
3131
3132         if (wl->iscan_on && iscan->tsk) {
3133                 iscan->state = WL_ISCAN_STATE_IDLE;
3134                 send_sig(SIGTERM, iscan->tsk, 1);
3135                 kthread_stop(iscan->tsk);
3136                 iscan->tsk = NULL;
3137         }
3138 }
3139
3140 static void wl_notify_iscan_complete(struct wl_iscan_ctrl *iscan, bool aborted)
3141 {
3142         struct wl_priv *wl = iscan_to_wl(iscan);
3143         struct net_device *ndev = wl_to_ndev(wl);
3144
3145         if (unlikely(!test_and_clear_bit(WL_STATUS_SCANNING, &wl->status))) {
3146                 WL_ERR("Scan complete while device not scanning\n");
3147                 return;
3148         }
3149         if (likely(wl->scan_request)) {
3150                 WL_SCAN("ISCAN Completed scan: %s\n",
3151                                 aborted ? "Aborted" : "Done");
3152                 cfg80211_scan_done(wl->scan_request, aborted);
3153                 wl_set_mpc(ndev, 1);
3154                 wl->scan_request = NULL;
3155         }
3156         wl->iscan_kickstart = false;
3157 }
3158
3159 static s32 wl_wakeup_iscan(struct wl_iscan_ctrl *iscan)
3160 {
3161         if (likely(iscan->state != WL_ISCAN_STATE_IDLE)) {
3162                 WL_SCAN("wake up iscan\n");
3163                 up(&iscan->sync);
3164                 return 0;
3165         }
3166
3167         return -EIO;
3168 }
3169
3170 static s32
3171 wl_get_iscan_results(struct wl_iscan_ctrl *iscan, u32 *status,
3172                      struct wl_scan_results **bss_list)
3173 {
3174         struct wl_iscan_results list;
3175         struct wl_scan_results *results;
3176         struct wl_iscan_results *list_buf;
3177         s32 err = 0;
3178
3179         memset(iscan->scan_buf, 0, WL_ISCAN_BUF_MAX);
3180         list_buf = (struct wl_iscan_results *)iscan->scan_buf;
3181         results = &list_buf->results;
3182         results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
3183         results->version = 0;
3184         results->count = 0;
3185
3186         memset(&list, 0, sizeof(list));
3187         list.results.buflen = cpu_to_le32(WL_ISCAN_BUF_MAX);
3188         err = wl_dev_iovar_getbuf(iscan->dev, "iscanresults", &list,
3189                                 WL_ISCAN_RESULTS_FIXED_SIZE, iscan->scan_buf,
3190                                 WL_ISCAN_BUF_MAX);
3191         if (unlikely(err)) {
3192                 WL_ERR("error (%d)\n", err);
3193                 return err;
3194         }
3195         results->buflen = le32_to_cpu(results->buflen);
3196         results->version = le32_to_cpu(results->version);
3197         results->count = le32_to_cpu(results->count);
3198         WL_SCAN("results->count = %d\n", results->count);
3199         WL_SCAN("results->buflen = %d\n", results->buflen);
3200         *status = le32_to_cpu(list_buf->status);
3201         *bss_list = results;
3202
3203         return err;
3204 }
3205
3206 static s32 wl_iscan_done(struct wl_priv *wl)
3207 {
3208         struct wl_iscan_ctrl *iscan = wl->iscan;
3209         s32 err = 0;
3210
3211         iscan->state = WL_ISCAN_STATE_IDLE;
3212         rtnl_lock();
3213         wl_inform_bss(wl);
3214         wl_notify_iscan_complete(iscan, false);
3215         rtnl_unlock();
3216
3217         return err;
3218 }
3219
3220 static s32 wl_iscan_pending(struct wl_priv *wl)
3221 {
3222         struct wl_iscan_ctrl *iscan = wl->iscan;
3223         s32 err = 0;
3224
3225         /* Reschedule the timer */
3226         mod_timer(&iscan->timer, jiffies + iscan->timer_ms * HZ / 1000);
3227         iscan->timer_on = 1;
3228
3229         return err;
3230 }
3231
3232 static s32 wl_iscan_inprogress(struct wl_priv *wl)
3233 {
3234         struct wl_iscan_ctrl *iscan = wl->iscan;
3235         s32 err = 0;
3236
3237         rtnl_lock();
3238         wl_inform_bss(wl);
3239         wl_run_iscan(iscan, NULL, WL_SCAN_ACTION_CONTINUE);
3240         rtnl_unlock();
3241         /* Reschedule the timer */
3242         mod_timer(&iscan->timer, jiffies + iscan->timer_ms * HZ / 1000);
3243         iscan->timer_on = 1;
3244
3245         return err;
3246 }
3247
3248 static s32 wl_iscan_aborted(struct wl_priv *wl)
3249 {
3250         struct wl_iscan_ctrl *iscan = wl->iscan;
3251         s32 err = 0;
3252
3253         iscan->state = WL_ISCAN_STATE_IDLE;
3254         rtnl_lock();
3255         wl_notify_iscan_complete(iscan, true);
3256         rtnl_unlock();
3257
3258         return err;
3259 }
3260
3261 static s32 wl_iscan_thread(void *data)
3262 {
3263         struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1 };
3264         struct wl_iscan_ctrl *iscan = (struct wl_iscan_ctrl *)data;
3265         struct wl_priv *wl = iscan_to_wl(iscan);
3266         struct wl_iscan_eloop *el = &iscan->el;
3267         u32 status;
3268         int err = 0;
3269
3270         sched_setscheduler(current, SCHED_FIFO, &param);
3271         allow_signal(SIGTERM);
3272         status = WL_SCAN_RESULTS_PARTIAL;
3273         while (likely(!down_interruptible(&iscan->sync))) {
3274                 if (kthread_should_stop())
3275                         break;
3276                 if (iscan->timer_on) {
3277                         del_timer_sync(&iscan->timer);
3278                         iscan->timer_on = 0;
3279                 }
3280                 rtnl_lock();
3281                 err = wl_get_iscan_results(iscan, &status, &wl->bss_list);
3282                 if (unlikely(err)) {
3283                         status = WL_SCAN_RESULTS_ABORTED;
3284                         WL_ERR("Abort iscan\n");
3285                 }
3286                 rtnl_unlock();
3287                 el->handler[status] (wl);
3288         }
3289         if (iscan->timer_on) {
3290                 del_timer_sync(&iscan->timer);
3291                 iscan->timer_on = 0;
3292         }
3293         WL_SCAN("ISCAN thread terminated\n");
3294
3295         return 0;
3296 }
3297
3298 static void wl_iscan_timer(unsigned long data)
3299 {
3300         struct wl_iscan_ctrl *iscan = (struct wl_iscan_ctrl *)data;
3301
3302         if (iscan) {
3303                 iscan->timer_on = 0;
3304                 WL_SCAN("timer expired\n");
3305                 wl_wakeup_iscan(iscan);
3306         }
3307 }
3308
3309 static s32 wl_invoke_iscan(struct wl_priv *wl)
3310 {
3311         struct wl_iscan_ctrl *iscan = wl_to_iscan(wl);
3312         int err = 0;
3313
3314         if (wl->iscan_on && !iscan->tsk) {
3315                 iscan->state = WL_ISCAN_STATE_IDLE;
3316                 sema_init(&iscan->sync, 0);
3317                 iscan->tsk = kthread_run(wl_iscan_thread, iscan, "wl_iscan");
3318                 if (IS_ERR(iscan->tsk)) {
3319                         WL_ERR("Could not create iscan thread\n");
3320                         iscan->tsk = NULL;
3321                         return -ENOMEM;
3322                 }
3323         }
3324
3325         return err;
3326 }
3327
3328 static void wl_init_iscan_eloop(struct wl_iscan_eloop *el)
3329 {
3330         memset(el, 0, sizeof(*el));
3331         el->handler[WL_SCAN_RESULTS_SUCCESS] = wl_iscan_done;
3332         el->handler[WL_SCAN_RESULTS_PARTIAL] = wl_iscan_inprogress;
3333         el->handler[WL_SCAN_RESULTS_PENDING] = wl_iscan_pending;
3334         el->handler[WL_SCAN_RESULTS_ABORTED] = wl_iscan_aborted;
3335         el->handler[WL_SCAN_RESULTS_NO_MEM] = wl_iscan_aborted;
3336 }
3337
3338 static s32 wl_init_iscan(struct wl_priv *wl)
3339 {
3340         struct wl_iscan_ctrl *iscan = wl_to_iscan(wl);
3341         int err = 0;
3342
3343         if (wl->iscan_on) {
3344                 iscan->dev = wl_to_ndev(wl);
3345                 iscan->state = WL_ISCAN_STATE_IDLE;
3346                 wl_init_iscan_eloop(&iscan->el);
3347                 iscan->timer_ms = WL_ISCAN_TIMER_INTERVAL_MS;
3348                 init_timer(&iscan->timer);
3349                 iscan->timer.data = (unsigned long) iscan;
3350                 iscan->timer.function = wl_iscan_timer;
3351                 sema_init(&iscan->sync, 0);
3352                 iscan->tsk = kthread_run(wl_iscan_thread, iscan, "wl_iscan");
3353                 if (IS_ERR(iscan->tsk)) {
3354                         WL_ERR("Could not create iscan thread\n");
3355                         iscan->tsk = NULL;
3356                         return -ENOMEM;
3357                 }
3358                 iscan->data = wl;
3359         }
3360
3361         return err;
3362 }
3363
3364 static void wl_init_fw(struct wl_fw_ctrl *fw)
3365 {
3366         fw->status = 0;         /* init fw loading status.
3367                                  0 means nothing was loaded yet */
3368 }
3369
3370 static s32 wl_init_priv(struct wl_priv *wl)
3371 {
3372         struct wiphy *wiphy = wl_to_wiphy(wl);
3373         s32 err = 0;
3374
3375         wl->scan_request = NULL;
3376         wl->pwr_save = !!(wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT);
3377         wl->iscan_on = true;    /* iscan on & off switch.
3378                                  we enable iscan per default */
3379         wl->roam_on = false;    /* roam on & off switch.
3380                                  we enable roam per default */
3381
3382         wl->iscan_kickstart = false;
3383         wl->active_scan = true; /* we do active scan for
3384                                  specific scan per default */
3385         wl->dongle_up = false;  /* dongle is not up yet */
3386         wl_init_eq(wl);
3387         err = wl_init_priv_mem(wl);
3388         if (unlikely(err))
3389                 return err;
3390         if (unlikely(wl_create_event_handler(wl)))
3391                 return -ENOMEM;
3392         wl_init_eloop_handler(&wl->el);
3393         mutex_init(&wl->usr_sync);
3394         err = wl_init_iscan(wl);
3395         if (unlikely(err))
3396                 return err;
3397         wl_init_fw(wl->fw);
3398         wl_init_conf(wl->conf);
3399         wl_init_prof(wl->profile);
3400         wl_link_down(wl);
3401
3402         return err;
3403 }
3404
3405 static void wl_deinit_priv(struct wl_priv *wl)
3406 {
3407         wl_destroy_event_handler(wl);
3408         wl->dongle_up = false;  /* dongle down */
3409         wl_flush_eq(wl);
3410         wl_link_down(wl);
3411         wl_term_iscan(wl);
3412         wl_deinit_priv_mem(wl);
3413 }
3414
3415 s32 wl_cfg80211_attach(struct net_device *ndev, void *data)
3416 {
3417         struct wireless_dev *wdev;
3418         struct wl_priv *wl;
3419         struct wl_iface *ci;
3420         s32 err = 0;
3421
3422         if (unlikely(!ndev)) {
3423                 WL_ERR("ndev is invalid\n");
3424                 return -ENODEV;
3425         }
3426         wl_cfg80211_dev = kzalloc(sizeof(struct wl_dev), GFP_KERNEL);
3427         if (unlikely(!wl_cfg80211_dev)) {
3428                 WL_ERR("wl_cfg80211_dev is invalid\n");
3429                 return -ENOMEM;
3430         }
3431         WL_INFO("func %p\n", wl_cfg80211_get_sdio_func());
3432         wdev = wl_alloc_wdev(sizeof(struct wl_iface), &wl_cfg80211_get_sdio_func()->dev);
3433         if (IS_ERR(wdev))
3434                 return -ENOMEM;
3435
3436         wdev->iftype = wl_mode_to_nl80211_iftype(WL_MODE_BSS);
3437         wl = wdev_to_wl(wdev);
3438         wl->wdev = wdev;
3439         wl->pub = data;
3440         ci = (struct wl_iface *)wl_to_ci(wl);
3441         ci->wl = wl;
3442         ndev->ieee80211_ptr = wdev;
3443         SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
3444         wdev->netdev = ndev;
3445         err = wl_init_priv(wl);
3446         if (unlikely(err)) {
3447                 WL_ERR("Failed to init iwm_priv (%d)\n", err);
3448                 goto cfg80211_attach_out;
3449         }
3450         wl_set_drvdata(wl_cfg80211_dev, ci);
3451
3452         return err;
3453
3454 cfg80211_attach_out:
3455         wl_free_wdev(wl);
3456         return err;
3457 }
3458
3459 void wl_cfg80211_detach(void)
3460 {
3461         struct wl_priv *wl;
3462
3463         wl = WL_PRIV_GET();
3464
3465         wl_deinit_priv(wl);
3466         wl_free_wdev(wl);
3467         wl_set_drvdata(wl_cfg80211_dev, NULL);
3468         kfree(wl_cfg80211_dev);
3469         wl_cfg80211_dev = NULL;
3470         wl_clear_sdio_func();
3471 }
3472
3473 static void wl_wakeup_event(struct wl_priv *wl)
3474 {
3475         up(&wl->event_sync);
3476 }
3477
3478 static s32 wl_event_handler(void *data)
3479 {
3480         struct wl_priv *wl = (struct wl_priv *)data;
3481         struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1 };
3482         struct wl_event_q *e;
3483
3484         sched_setscheduler(current, SCHED_FIFO, &param);
3485         allow_signal(SIGTERM);
3486         while (likely(!down_interruptible(&wl->event_sync))) {
3487                 if (kthread_should_stop())
3488                         break;
3489                 e = wl_deq_event(wl);
3490                 if (unlikely(!e)) {
3491                         WL_ERR("event queue empty...\n");
3492                         BUG();
3493                 }
3494                 WL_INFO("event type (%d)\n", e->etype);
3495                 if (wl->el.handler[e->etype]) {
3496                         wl->el.handler[e->etype] (wl, wl_to_ndev(wl), &e->emsg,
3497                                                   e->edata);
3498                 } else {
3499                         WL_INFO("Unknown Event (%d): ignoring\n", e->etype);
3500                 }
3501                 wl_put_event(e);
3502         }
3503         WL_INFO("was terminated\n");
3504         return 0;
3505 }
3506
3507 void
3508 wl_cfg80211_event(struct net_device *ndev,
3509                   const brcmf_event_msg_t *e, void *data)
3510 {
3511         u32 event_type = be32_to_cpu(e->event_type);
3512         struct wl_priv *wl = ndev_to_wl(ndev);
3513
3514         if (likely(!wl_enq_event(wl, event_type, e, data)))
3515                 wl_wakeup_event(wl);
3516 }
3517
3518 static void wl_init_eq(struct wl_priv *wl)
3519 {
3520         wl_init_eq_lock(wl);
3521         INIT_LIST_HEAD(&wl->eq_list);
3522 }
3523
3524 static void wl_flush_eq(struct wl_priv *wl)
3525 {
3526         struct wl_event_q *e;
3527
3528         wl_lock_eq(wl);
3529         while (!list_empty(&wl->eq_list)) {
3530                 e = list_first_entry(&wl->eq_list, struct wl_event_q, eq_list);
3531                 list_del(&e->eq_list);
3532                 kfree(e);
3533         }
3534         wl_unlock_eq(wl);
3535 }
3536
3537 /*
3538 * retrieve first queued event from head
3539 */
3540
3541 static struct wl_event_q *wl_deq_event(struct wl_priv *wl)
3542 {
3543         struct wl_event_q *e = NULL;
3544
3545         wl_lock_eq(wl);
3546         if (likely(!list_empty(&wl->eq_list))) {
3547                 e = list_first_entry(&wl->eq_list, struct wl_event_q, eq_list);
3548                 list_del(&e->eq_list);
3549         }
3550         wl_unlock_eq(wl);
3551
3552         return e;
3553 }
3554
3555 /*
3556 ** push event to tail of the queue
3557 */
3558
3559 static s32
3560 wl_enq_event(struct wl_priv *wl, u32 event, const brcmf_event_msg_t *msg,
3561              void *data)
3562 {
3563         struct wl_event_q *e;
3564         s32 err = 0;
3565
3566         e = kzalloc(sizeof(struct wl_event_q), GFP_KERNEL);
3567         if (unlikely(!e)) {
3568                 WL_ERR("event alloc failed\n");
3569                 return -ENOMEM;
3570         }
3571
3572         e->etype = event;
3573         memcpy(&e->emsg, msg, sizeof(brcmf_event_msg_t));
3574         if (data) {
3575         }
3576         wl_lock_eq(wl);
3577         list_add_tail(&e->eq_list, &wl->eq_list);
3578         wl_unlock_eq(wl);
3579
3580         return err;
3581 }
3582
3583 static void wl_put_event(struct wl_event_q *e)
3584 {
3585         kfree(e);
3586 }
3587
3588 void wl_cfg80211_sdio_func(void *func)
3589 {
3590         cfg80211_sdio_func = (struct sdio_func *)func;
3591 }
3592
3593 static void wl_clear_sdio_func(void)
3594 {
3595         cfg80211_sdio_func = NULL;
3596 }
3597
3598 struct sdio_func *wl_cfg80211_get_sdio_func(void)
3599 {
3600         return cfg80211_sdio_func;
3601 }
3602
3603 static s32 wl_dongle_mode(struct net_device *ndev, s32 iftype)
3604 {
3605         s32 infra = 0;
3606         s32 err = 0;
3607
3608         switch (iftype) {
3609         case NL80211_IFTYPE_MONITOR:
3610         case NL80211_IFTYPE_WDS:
3611                 WL_ERR("type (%d) : currently we do not support this mode\n",
3612                        iftype);
3613                 err = -EINVAL;
3614                 return err;
3615         case NL80211_IFTYPE_ADHOC:
3616                 infra = 0;
3617                 break;
3618         case NL80211_IFTYPE_STATION:
3619                 infra = 1;
3620                 break;
3621         default:
3622                 err = -EINVAL;
3623                 WL_ERR("invalid type (%d)\n", iftype);
3624                 return err;
3625         }
3626         infra = cpu_to_le32(infra);
3627         err = wl_dev_ioctl(ndev, BRCMF_C_SET_INFRA, &infra, sizeof(infra));
3628         if (unlikely(err)) {
3629                 WL_ERR("WLC_SET_INFRA error (%d)\n", err);
3630                 return err;
3631         }
3632
3633         return 0;
3634 }
3635
3636 static s32 wl_dongle_eventmsg(struct net_device *ndev)
3637 {
3638         s8 iovbuf[WL_EVENTING_MASK_LEN + 12];   /*  Room for "event_msgs" +
3639                                                  '\0' + bitvec  */
3640         s8 eventmask[WL_EVENTING_MASK_LEN];
3641         s32 err = 0;
3642
3643         WL_TRACE("Enter\n");
3644
3645         /* Setup event_msgs */
3646         brcmu_mkiovar("event_msgs", eventmask, WL_EVENTING_MASK_LEN, iovbuf,
3647                     sizeof(iovbuf));
3648         err = wl_dev_ioctl(ndev, BRCMF_C_GET_VAR, iovbuf, sizeof(iovbuf));
3649         if (unlikely(err)) {
3650                 WL_ERR("Get event_msgs error (%d)\n", err);
3651                 goto dongle_eventmsg_out;
3652         }
3653         memcpy(eventmask, iovbuf, WL_EVENTING_MASK_LEN);
3654
3655         setbit(eventmask, BRCMF_E_SET_SSID);
3656         setbit(eventmask, BRCMF_E_ROAM);
3657         setbit(eventmask, BRCMF_E_PRUNE);
3658         setbit(eventmask, BRCMF_E_AUTH);
3659         setbit(eventmask, BRCMF_E_REASSOC);
3660         setbit(eventmask, BRCMF_E_REASSOC_IND);
3661         setbit(eventmask, BRCMF_E_DEAUTH_IND);
3662         setbit(eventmask, BRCMF_E_DISASSOC_IND);
3663         setbit(eventmask, BRCMF_E_DISASSOC);
3664         setbit(eventmask, BRCMF_E_JOIN);
3665         setbit(eventmask, BRCMF_E_ASSOC_IND);
3666         setbit(eventmask, BRCMF_E_PSK_SUP);
3667         setbit(eventmask, BRCMF_E_LINK);
3668         setbit(eventmask, BRCMF_E_NDIS_LINK);
3669         setbit(eventmask, BRCMF_E_MIC_ERROR);
3670         setbit(eventmask, BRCMF_E_PMKID_CACHE);
3671         setbit(eventmask, BRCMF_E_TXFAIL);
3672         setbit(eventmask, BRCMF_E_JOIN_START);
3673         setbit(eventmask, BRCMF_E_SCAN_COMPLETE);
3674
3675         brcmu_mkiovar("event_msgs", eventmask, WL_EVENTING_MASK_LEN, iovbuf,
3676                     sizeof(iovbuf));
3677         err = wl_dev_ioctl(ndev, BRCMF_C_SET_VAR, iovbuf, sizeof(iovbuf));
3678         if (unlikely(err)) {
3679                 WL_ERR("Set event_msgs error (%d)\n", err);
3680                 goto dongle_eventmsg_out;
3681         }
3682
3683 dongle_eventmsg_out:
3684         WL_TRACE("Exit\n");
3685         return err;
3686 }
3687
3688 static s32
3689 wl_dongle_roam(struct net_device *ndev, u32 roamvar, u32 bcn_timeout)
3690 {
3691         s8 iovbuf[32];
3692         s32 roamtrigger[2];
3693         s32 roam_delta[2];
3694         s32 err = 0;
3695
3696         /*
3697          * Setup timeout if Beacons are lost and roam is
3698          * off to report link down
3699          */
3700         if (roamvar) {
3701                 brcmu_mkiovar("bcn_timeout", (char *)&bcn_timeout,
3702                         sizeof(bcn_timeout), iovbuf, sizeof(iovbuf));
3703                 err = wl_dev_ioctl(ndev, BRCMF_C_SET_VAR,
3704                                    iovbuf, sizeof(iovbuf));
3705                 if (unlikely(err)) {
3706                         WL_ERR("bcn_timeout error (%d)\n", err);
3707                         goto dongle_rom_out;
3708                 }
3709         }
3710
3711         /*
3712          * Enable/Disable built-in roaming to allow supplicant
3713          * to take care of roaming
3714          */
3715         WL_INFO("Internal Roaming = %s\n", roamvar ? "Off" : "On");
3716         brcmu_mkiovar("roam_off", (char *)&roamvar,
3717                                 sizeof(roamvar), iovbuf, sizeof(iovbuf));
3718         err = wl_dev_ioctl(ndev, BRCMF_C_SET_VAR, iovbuf, sizeof(iovbuf));
3719         if (unlikely(err)) {
3720                 WL_ERR("roam_off error (%d)\n", err);
3721                 goto dongle_rom_out;
3722         }
3723
3724         roamtrigger[0] = WL_ROAM_TRIGGER_LEVEL;
3725         roamtrigger[1] = WLC_BAND_ALL;
3726         err = wl_dev_ioctl(ndev, BRCMF_C_SET_ROAM_TRIGGER,
3727                         (void *)roamtrigger, sizeof(roamtrigger));
3728         if (unlikely(err)) {
3729                 WL_ERR("WLC_SET_ROAM_TRIGGER error (%d)\n", err);
3730                 goto dongle_rom_out;
3731         }
3732
3733         roam_delta[0] = WL_ROAM_DELTA;
3734         roam_delta[1] = WLC_BAND_ALL;
3735         err = wl_dev_ioctl(ndev, BRCMF_C_SET_ROAM_DELTA,
3736                                 (void *)roam_delta, sizeof(roam_delta));
3737         if (unlikely(err)) {
3738                 WL_ERR("WLC_SET_ROAM_DELTA error (%d)\n", err);
3739                 goto dongle_rom_out;
3740         }
3741
3742 dongle_rom_out:
3743         return err;
3744 }
3745
3746 static s32
3747 wl_dongle_scantime(struct net_device *ndev, s32 scan_assoc_time,
3748                 s32 scan_unassoc_time, s32 scan_passive_time)
3749 {
3750         s32 err = 0;
3751
3752         err = wl_dev_ioctl(ndev, BRCMF_C_SET_SCAN_CHANNEL_TIME,
3753                            &scan_assoc_time, sizeof(scan_assoc_time));
3754         if (err) {
3755                 if (err == -EOPNOTSUPP)
3756                         WL_INFO("Scan assoc time is not supported\n");
3757                 else
3758                         WL_ERR("Scan assoc time error (%d)\n", err);
3759                 goto dongle_scantime_out;
3760         }
3761         err = wl_dev_ioctl(ndev, BRCMF_C_SET_SCAN_UNASSOC_TIME,
3762                            &scan_unassoc_time, sizeof(scan_unassoc_time));
3763         if (err) {
3764                 if (err == -EOPNOTSUPP)
3765                         WL_INFO("Scan unassoc time is not supported\n");
3766                 else
3767                         WL_ERR("Scan unassoc time error (%d)\n", err);
3768                 goto dongle_scantime_out;
3769         }
3770
3771         err = wl_dev_ioctl(ndev, BRCMF_C_SET_SCAN_PASSIVE_TIME,
3772                            &scan_passive_time, sizeof(scan_passive_time));
3773         if (err) {
3774                 if (err == -EOPNOTSUPP)
3775                         WL_INFO("Scan passive time is not supported\n");
3776                 else
3777                         WL_ERR("Scan passive time error (%d)\n", err);
3778                 goto dongle_scantime_out;
3779         }
3780
3781 dongle_scantime_out:
3782         return err;
3783 }
3784
3785 s32 wl_config_dongle(struct wl_priv *wl, bool need_lock)
3786 {
3787         struct net_device *ndev;
3788         struct wireless_dev *wdev;
3789         s32 err = 0;
3790
3791         if (wl->dongle_up)
3792                 return err;
3793
3794         ndev = wl_to_ndev(wl);
3795         wdev = ndev->ieee80211_ptr;
3796         if (need_lock)
3797                 rtnl_lock();
3798
3799         wl_dongle_scantime(ndev, WL_SCAN_CHANNEL_TIME,
3800                         WL_SCAN_UNASSOC_TIME, WL_SCAN_PASSIVE_TIME);
3801
3802         err = wl_dongle_eventmsg(ndev);
3803         if (unlikely(err))
3804                 goto default_conf_out;
3805         err = wl_dongle_roam(ndev, (wl->roam_on ? 0 : 1), WL_BEACON_TIMEOUT);
3806         if (unlikely(err))
3807                 goto default_conf_out;
3808         err = wl_dongle_mode(ndev, wdev->iftype);
3809         if (unlikely(err && err != -EINPROGRESS))
3810                 goto default_conf_out;
3811         err = wl_dongle_probecap(wl);
3812         if (unlikely(err))
3813                 goto default_conf_out;
3814
3815         /* -EINPROGRESS: Call commit handler */
3816
3817 default_conf_out:
3818         if (need_lock)
3819                 rtnl_unlock();
3820
3821         wl->dongle_up = true;
3822
3823         return err;
3824
3825 }
3826
3827 static s32 wl_update_wiphybands(struct wl_priv *wl)
3828 {
3829         struct wiphy *wiphy;
3830         s32 phy_list;
3831         s8 phy;
3832         s32 err = 0;
3833
3834         err = wl_dev_ioctl(wl_to_ndev(wl), WLC_GET_PHYLIST, &phy_list,
3835                         sizeof(phy_list));
3836         if (unlikely(err)) {
3837                 WL_ERR("error (%d)\n", err);
3838                 return err;
3839         }
3840
3841         phy = ((char *)&phy_list)[1];
3842         WL_INFO("%c phy\n", phy);
3843         if (phy == 'n' || phy == 'a') {
3844                 wiphy = wl_to_wiphy(wl);
3845                 wiphy->bands[IEEE80211_BAND_5GHZ] = &__wl_band_5ghz_n;
3846         }
3847
3848         return err;
3849 }
3850
3851 static s32 __wl_cfg80211_up(struct wl_priv *wl)
3852 {
3853         s32 err = 0;
3854
3855         set_bit(WL_STATUS_READY, &wl->status);
3856
3857         wl_debugfs_add_netdev_params(wl);
3858
3859         err = wl_config_dongle(wl, false);
3860         if (unlikely(err))
3861                 return err;
3862
3863         wl_invoke_iscan(wl);
3864
3865         return err;
3866 }
3867
3868 static s32 __wl_cfg80211_down(struct wl_priv *wl)
3869 {
3870         /*
3871          * While going down, if associated with AP disassociate
3872          * from AP to save power
3873          */
3874         if ((test_bit(WL_STATUS_CONNECTED, &wl->status) ||
3875              test_bit(WL_STATUS_CONNECTING, &wl->status)) &&
3876              test_bit(WL_STATUS_READY, &wl->status)) {
3877                 WL_INFO("Disassociating from AP");
3878                 wl_link_down(wl);
3879
3880                 /* Make sure WPA_Supplicant receives all the event
3881                    generated due to DISASSOC call to the fw to keep
3882                    the state fw and WPA_Supplicant state consistent
3883                  */
3884                 rtnl_unlock();
3885                 wl_delay(500);
3886                 rtnl_lock();
3887         }
3888
3889         set_bit(WL_STATUS_SCAN_ABORTING, &wl->status);
3890         wl_term_iscan(wl);
3891         if (wl->scan_request) {
3892                 cfg80211_scan_done(wl->scan_request, true);
3893                 /* May need to perform this to cover rmmod */
3894                 /* wl_set_mpc(wl_to_ndev(wl), 1); */
3895                 wl->scan_request = NULL;
3896         }
3897         clear_bit(WL_STATUS_READY, &wl->status);
3898         clear_bit(WL_STATUS_SCANNING, &wl->status);
3899         clear_bit(WL_STATUS_SCAN_ABORTING, &wl->status);
3900
3901         wl_debugfs_remove_netdev(wl);
3902
3903         return 0;
3904 }
3905
3906 s32 wl_cfg80211_up(void)
3907 {
3908         struct wl_priv *wl;
3909         s32 err = 0;
3910
3911         wl = WL_PRIV_GET();
3912         mutex_lock(&wl->usr_sync);
3913         err = __wl_cfg80211_up(wl);
3914         mutex_unlock(&wl->usr_sync);
3915
3916         return err;
3917 }
3918
3919 s32 wl_cfg80211_down(void)
3920 {
3921         struct wl_priv *wl;
3922         s32 err = 0;
3923
3924         wl = WL_PRIV_GET();
3925         mutex_lock(&wl->usr_sync);
3926         err = __wl_cfg80211_down(wl);
3927         mutex_unlock(&wl->usr_sync);
3928
3929         return err;
3930 }
3931
3932 static s32 wl_dongle_probecap(struct wl_priv *wl)
3933 {
3934         return wl_update_wiphybands(wl);
3935 }
3936
3937 static void *wl_read_prof(struct wl_priv *wl, s32 item)
3938 {
3939         switch (item) {
3940         case WL_PROF_SEC:
3941                 return &wl->profile->sec;
3942         case WL_PROF_BSSID:
3943                 return &wl->profile->bssid;
3944         case WL_PROF_SSID:
3945                 return &wl->profile->ssid;
3946         }
3947         WL_ERR("invalid item (%d)\n", item);
3948         return NULL;
3949 }
3950
3951 static s32
3952 wl_update_prof(struct wl_priv *wl, const brcmf_event_msg_t *e, void *data,
3953                s32 item)
3954 {
3955         s32 err = 0;
3956         struct wlc_ssid *ssid;
3957
3958         switch (item) {
3959         case WL_PROF_SSID:
3960                 ssid = (wlc_ssid_t *) data;
3961                 memset(wl->profile->ssid.SSID, 0,
3962                        sizeof(wl->profile->ssid.SSID));
3963                 memcpy(wl->profile->ssid.SSID, ssid->SSID, ssid->SSID_len);
3964                 wl->profile->ssid.SSID_len = ssid->SSID_len;
3965                 break;
3966         case WL_PROF_BSSID:
3967                 if (data)
3968                         memcpy(wl->profile->bssid, data, ETH_ALEN);
3969                 else
3970                         memset(wl->profile->bssid, 0, ETH_ALEN);
3971                 break;
3972         case WL_PROF_SEC:
3973                 memcpy(&wl->profile->sec, data, sizeof(wl->profile->sec));
3974                 break;
3975         case WL_PROF_BEACONINT:
3976                 wl->profile->beacon_interval = *(u16 *)data;
3977                 break;
3978         case WL_PROF_DTIMPERIOD:
3979                 wl->profile->dtim_period = *(u8 *)data;
3980                 break;
3981         default:
3982                 WL_ERR("unsupported item (%d)\n", item);
3983                 err = -EOPNOTSUPP;
3984                 break;
3985         }
3986
3987         return err;
3988 }
3989
3990 static bool wl_is_ibssmode(struct wl_priv *wl)
3991 {
3992         return wl->conf->mode == WL_MODE_IBSS;
3993 }
3994
3995 static __used s32 wl_add_ie(struct wl_priv *wl, u8 t, u8 l, u8 *v)
3996 {
3997         struct wl_ie *ie = wl_to_ie(wl);
3998         s32 err = 0;
3999
4000         if (unlikely(ie->offset + l + 2 > WL_TLV_INFO_MAX)) {
4001                 WL_ERR("ei crosses buffer boundary\n");
4002                 return -ENOSPC;
4003         }
4004         ie->buf[ie->offset] = t;
4005         ie->buf[ie->offset + 1] = l;
4006         memcpy(&ie->buf[ie->offset + 2], v, l);
4007         ie->offset += l + 2;
4008
4009         return err;
4010 }
4011
4012 static void wl_link_down(struct wl_priv *wl)
4013 {
4014         struct net_device *dev = NULL;
4015         s32 err = 0;
4016
4017         WL_TRACE("Enter\n");
4018
4019         if (wl->link_up) {
4020                 dev = wl_to_ndev(wl);
4021                 WL_INFO("Call WLC_DISASSOC to stop excess roaming\n ");
4022                 err = wl_dev_ioctl(dev, BRCMF_C_DISASSOC, NULL, 0);
4023                 if (unlikely(err))
4024                         WL_ERR("WLC_DISASSOC failed (%d)\n", err);
4025                 wl->link_up = false;
4026         }
4027         WL_TRACE("Exit\n");
4028 }
4029
4030 static void wl_lock_eq(struct wl_priv *wl)
4031 {
4032         spin_lock_irq(&wl->eq_lock);
4033 }
4034
4035 static void wl_unlock_eq(struct wl_priv *wl)
4036 {
4037         spin_unlock_irq(&wl->eq_lock);
4038 }
4039
4040 static void wl_init_eq_lock(struct wl_priv *wl)
4041 {
4042         spin_lock_init(&wl->eq_lock);
4043 }
4044
4045 static void wl_delay(u32 ms)
4046 {
4047         if (ms < 1000 / HZ) {
4048                 cond_resched();
4049                 mdelay(ms);
4050         } else {
4051                 msleep(ms);
4052         }
4053 }
4054
4055 static void wl_set_drvdata(struct wl_dev *dev, void *data)
4056 {
4057         dev->driver_data = data;
4058 }
4059
4060 static void *wl_get_drvdata(struct wl_dev *dev)
4061 {
4062         void *data = NULL;
4063
4064         if (dev)
4065                 data = dev->driver_data;
4066         return data;
4067 }
4068
4069 s32 wl_cfg80211_read_fw(s8 *buf, u32 size)
4070 {
4071         const struct firmware *fw_entry;
4072         struct wl_priv *wl;
4073
4074         wl = WL_PRIV_GET();
4075
4076         fw_entry = wl->fw->fw_entry;
4077
4078         if (fw_entry->size < wl->fw->ptr + size)
4079                 size = fw_entry->size - wl->fw->ptr;
4080
4081         memcpy(buf, &fw_entry->data[wl->fw->ptr], size);
4082         wl->fw->ptr += size;
4083         return size;
4084 }
4085
4086 void wl_cfg80211_release_fw(void)
4087 {
4088         struct wl_priv *wl;
4089
4090         wl = WL_PRIV_GET();
4091         release_firmware(wl->fw->fw_entry);
4092         wl->fw->ptr = 0;
4093 }
4094
4095 void *wl_cfg80211_request_fw(s8 *file_name)
4096 {
4097         struct wl_priv *wl;
4098         const struct firmware *fw_entry = NULL;
4099         s32 err = 0;
4100
4101         WL_INFO("file name : \"%s\"\n", file_name);
4102         wl = WL_PRIV_GET();
4103
4104         if (!test_bit(WL_FW_LOADING_DONE, &wl->fw->status)) {
4105                 err = request_firmware(&wl->fw->fw_entry, file_name,
4106                                 &wl_cfg80211_get_sdio_func()->dev);
4107                 if (unlikely(err)) {
4108                         WL_ERR("Could not download fw (%d)\n", err);
4109                         goto req_fw_out;
4110                 }
4111                 set_bit(WL_FW_LOADING_DONE, &wl->fw->status);
4112                 fw_entry = wl->fw->fw_entry;
4113                 if (fw_entry) {
4114                         WL_INFO("fw size (%zd), data (%p)\n",
4115                                fw_entry->size, fw_entry->data);
4116                 }
4117         } else if (!test_bit(WL_NVRAM_LOADING_DONE, &wl->fw->status)) {
4118                 err = request_firmware(&wl->fw->fw_entry, file_name,
4119                                 &wl_cfg80211_get_sdio_func()->dev);
4120                 if (unlikely(err)) {
4121                         WL_ERR("Could not download nvram (%d)\n", err);
4122                         goto req_fw_out;
4123                 }
4124                 set_bit(WL_NVRAM_LOADING_DONE, &wl->fw->status);
4125                 fw_entry = wl->fw->fw_entry;
4126                 if (fw_entry) {
4127                         WL_INFO("nvram size (%zd), data (%p)\n",
4128                                fw_entry->size, fw_entry->data);
4129                 }
4130         } else {
4131                 WL_INFO("Downloading already done. Nothing to do more\n");
4132                 err = -EPERM;
4133         }
4134
4135 req_fw_out:
4136         if (unlikely(err)) {
4137                 return NULL;
4138         }
4139         wl->fw->ptr = 0;
4140         return (void *)fw_entry->data;
4141 }
4142
4143 s8 *wl_cfg80211_get_fwname(void)
4144 {
4145         struct wl_priv *wl;
4146
4147         wl = WL_PRIV_GET();
4148         strcpy(wl->fw->fw_name, WL_4329_FW_FILE);
4149         return wl->fw->fw_name;
4150 }
4151
4152 s8 *wl_cfg80211_get_nvramname(void)
4153 {
4154         struct wl_priv *wl;
4155
4156         wl = WL_PRIV_GET();
4157         strcpy(wl->fw->nvram_name, WL_4329_NVRAM_FILE);
4158         return wl->fw->nvram_name;
4159 }
4160
4161 static void wl_set_mpc(struct net_device *ndev, int mpc)
4162 {
4163         s32 err = 0;
4164         struct wl_priv *wl = ndev_to_wl(ndev);
4165
4166         if (test_bit(WL_STATUS_READY, &wl->status)) {
4167                 err = wl_dev_intvar_set(ndev, "mpc", mpc);
4168                 if (unlikely(err)) {
4169                         WL_ERR("fail to set mpc\n");
4170                         return;
4171                 }
4172                 WL_INFO("MPC : %d\n", mpc);
4173         }
4174 }
4175
4176 static int wl_debugfs_add_netdev_params(struct wl_priv *wl)
4177 {
4178         char buf[10+IFNAMSIZ];
4179         struct dentry *fd;
4180         s32 err = 0;
4181
4182         sprintf(buf, "netdev:%s", wl_to_ndev(wl)->name);
4183         wl->debugfsdir = debugfs_create_dir(buf, wl_to_wiphy(wl)->debugfsdir);
4184
4185         fd = debugfs_create_u16("beacon_int", S_IRUGO, wl->debugfsdir,
4186                 (u16 *)&wl->profile->beacon_interval);
4187         if (!fd) {
4188                 err = -ENOMEM;
4189                 goto err_out;
4190         }
4191
4192         fd = debugfs_create_u8("dtim_period", S_IRUGO, wl->debugfsdir,
4193                 (u8 *)&wl->profile->dtim_period);
4194         if (!fd) {
4195                 err = -ENOMEM;
4196                 goto err_out;
4197         }
4198
4199 err_out:
4200         return err;
4201 }
4202
4203 static void wl_debugfs_remove_netdev(struct wl_priv *wl)
4204 {
4205         debugfs_remove_recursive(wl->debugfsdir);
4206         wl->debugfsdir = NULL;
4207 }