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