rndis_wlan: workaround device not returning bss for currently connected AP
[linux-2.6.git] / drivers / net / wireless / rndis_wlan.c
1 /*
2  * Driver for RNDIS based wireless USB devices.
3  *
4  * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5  * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  Portions of this file are based on NDISwrapper project,
22  *  Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
23  *  http://ndiswrapper.sourceforge.net/
24  */
25
26 // #define      DEBUG                   // error path messages, extra info
27 // #define      VERBOSE                 // more; success messages
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/ethtool.h>
34 #include <linux/workqueue.h>
35 #include <linux/mutex.h>
36 #include <linux/mii.h>
37 #include <linux/usb.h>
38 #include <linux/usb/cdc.h>
39 #include <linux/wireless.h>
40 #include <linux/ieee80211.h>
41 #include <linux/if_arp.h>
42 #include <linux/ctype.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <net/iw_handler.h>
46 #include <net/cfg80211.h>
47 #include <linux/usb/usbnet.h>
48 #include <linux/usb/rndis_host.h>
49
50
51 /* NOTE: All these are settings for Broadcom chipset */
52 static char modparam_country[4] = "EU";
53 module_param_string(country, modparam_country, 4, 0444);
54 MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
55
56 static int modparam_frameburst = 1;
57 module_param_named(frameburst, modparam_frameburst, int, 0444);
58 MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
59
60 static int modparam_afterburner = 0;
61 module_param_named(afterburner, modparam_afterburner, int, 0444);
62 MODULE_PARM_DESC(afterburner,
63         "enable afterburner aka '125 High Speed Mode' (default: off)");
64
65 static int modparam_power_save = 0;
66 module_param_named(power_save, modparam_power_save, int, 0444);
67 MODULE_PARM_DESC(power_save,
68         "set power save mode: 0=off, 1=on, 2=fast (default: off)");
69
70 static int modparam_power_output = 3;
71 module_param_named(power_output, modparam_power_output, int, 0444);
72 MODULE_PARM_DESC(power_output,
73         "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
74
75 static int modparam_roamtrigger = -70;
76 module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
77 MODULE_PARM_DESC(roamtrigger,
78         "set roaming dBm trigger: -80=optimize for distance, "
79                                 "-60=bandwidth (default: -70)");
80
81 static int modparam_roamdelta = 1;
82 module_param_named(roamdelta, modparam_roamdelta, int, 0444);
83 MODULE_PARM_DESC(roamdelta,
84         "set roaming tendency: 0=aggressive, 1=moderate, "
85                                 "2=conservative (default: moderate)");
86
87 static int modparam_workaround_interval;
88 module_param_named(workaround_interval, modparam_workaround_interval,
89                                                         int, 0444);
90 MODULE_PARM_DESC(workaround_interval,
91         "set stall workaround interval in msecs (0=disabled) (default: 0)");
92
93
94 /* various RNDIS OID defs */
95 #define OID_GEN_LINK_SPEED                      cpu_to_le32(0x00010107)
96 #define OID_GEN_RNDIS_CONFIG_PARAMETER          cpu_to_le32(0x0001021b)
97
98 #define OID_GEN_XMIT_OK                         cpu_to_le32(0x00020101)
99 #define OID_GEN_RCV_OK                          cpu_to_le32(0x00020102)
100 #define OID_GEN_XMIT_ERROR                      cpu_to_le32(0x00020103)
101 #define OID_GEN_RCV_ERROR                       cpu_to_le32(0x00020104)
102 #define OID_GEN_RCV_NO_BUFFER                   cpu_to_le32(0x00020105)
103
104 #define OID_802_3_CURRENT_ADDRESS               cpu_to_le32(0x01010102)
105 #define OID_802_3_MULTICAST_LIST                cpu_to_le32(0x01010103)
106 #define OID_802_3_MAXIMUM_LIST_SIZE             cpu_to_le32(0x01010104)
107
108 #define OID_802_11_BSSID                        cpu_to_le32(0x0d010101)
109 #define OID_802_11_SSID                         cpu_to_le32(0x0d010102)
110 #define OID_802_11_INFRASTRUCTURE_MODE          cpu_to_le32(0x0d010108)
111 #define OID_802_11_ADD_WEP                      cpu_to_le32(0x0d010113)
112 #define OID_802_11_REMOVE_WEP                   cpu_to_le32(0x0d010114)
113 #define OID_802_11_DISASSOCIATE                 cpu_to_le32(0x0d010115)
114 #define OID_802_11_AUTHENTICATION_MODE          cpu_to_le32(0x0d010118)
115 #define OID_802_11_PRIVACY_FILTER               cpu_to_le32(0x0d010119)
116 #define OID_802_11_BSSID_LIST_SCAN              cpu_to_le32(0x0d01011a)
117 #define OID_802_11_ENCRYPTION_STATUS            cpu_to_le32(0x0d01011b)
118 #define OID_802_11_ADD_KEY                      cpu_to_le32(0x0d01011d)
119 #define OID_802_11_REMOVE_KEY                   cpu_to_le32(0x0d01011e)
120 #define OID_802_11_ASSOCIATION_INFORMATION      cpu_to_le32(0x0d01011f)
121 #define OID_802_11_CAPABILITY                   cpu_to_le32(0x0d010122)
122 #define OID_802_11_PMKID                        cpu_to_le32(0x0d010123)
123 #define OID_802_11_NETWORK_TYPES_SUPPORTED      cpu_to_le32(0x0d010203)
124 #define OID_802_11_NETWORK_TYPE_IN_USE          cpu_to_le32(0x0d010204)
125 #define OID_802_11_TX_POWER_LEVEL               cpu_to_le32(0x0d010205)
126 #define OID_802_11_RSSI                         cpu_to_le32(0x0d010206)
127 #define OID_802_11_RSSI_TRIGGER                 cpu_to_le32(0x0d010207)
128 #define OID_802_11_FRAGMENTATION_THRESHOLD      cpu_to_le32(0x0d010209)
129 #define OID_802_11_RTS_THRESHOLD                cpu_to_le32(0x0d01020a)
130 #define OID_802_11_SUPPORTED_RATES              cpu_to_le32(0x0d01020e)
131 #define OID_802_11_CONFIGURATION                cpu_to_le32(0x0d010211)
132 #define OID_802_11_BSSID_LIST                   cpu_to_le32(0x0d010217)
133
134
135 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
136 #define WL_NOISE        -96     /* typical noise level in dBm */
137 #define WL_SIGMAX       -32     /* typical maximum signal level in dBm */
138
139
140 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
141  * based on wireless rndis) has default txpower of 13dBm.
142  * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
143  *  100% : 20 mW ~ 13dBm
144  *   75% : 15 mW ~ 12dBm
145  *   50% : 10 mW ~ 10dBm
146  *   25% :  5 mW ~  7dBm
147  */
148 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
149 #define BCM4320_DEFAULT_TXPOWER_DBM_75  12
150 #define BCM4320_DEFAULT_TXPOWER_DBM_50  10
151 #define BCM4320_DEFAULT_TXPOWER_DBM_25  7
152
153
154 /* codes for "status" field of completion messages */
155 #define RNDIS_STATUS_ADAPTER_NOT_READY          cpu_to_le32(0xc0010011)
156 #define RNDIS_STATUS_ADAPTER_NOT_OPEN           cpu_to_le32(0xc0010012)
157
158
159 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
160  * slightly modified for datatype endianess, etc
161  */
162 #define NDIS_802_11_LENGTH_SSID 32
163 #define NDIS_802_11_LENGTH_RATES 8
164 #define NDIS_802_11_LENGTH_RATES_EX 16
165
166 enum ndis_80211_net_type {
167         NDIS_80211_TYPE_FREQ_HOP,
168         NDIS_80211_TYPE_DIRECT_SEQ,
169         NDIS_80211_TYPE_OFDM_A,
170         NDIS_80211_TYPE_OFDM_G
171 };
172
173 enum ndis_80211_net_infra {
174         NDIS_80211_INFRA_ADHOC,
175         NDIS_80211_INFRA_INFRA,
176         NDIS_80211_INFRA_AUTO_UNKNOWN
177 };
178
179 enum ndis_80211_auth_mode {
180         NDIS_80211_AUTH_OPEN,
181         NDIS_80211_AUTH_SHARED,
182         NDIS_80211_AUTH_AUTO_SWITCH,
183         NDIS_80211_AUTH_WPA,
184         NDIS_80211_AUTH_WPA_PSK,
185         NDIS_80211_AUTH_WPA_NONE,
186         NDIS_80211_AUTH_WPA2,
187         NDIS_80211_AUTH_WPA2_PSK
188 };
189
190 enum ndis_80211_encr_status {
191         NDIS_80211_ENCR_WEP_ENABLED,
192         NDIS_80211_ENCR_DISABLED,
193         NDIS_80211_ENCR_WEP_KEY_ABSENT,
194         NDIS_80211_ENCR_NOT_SUPPORTED,
195         NDIS_80211_ENCR_TKIP_ENABLED,
196         NDIS_80211_ENCR_TKIP_KEY_ABSENT,
197         NDIS_80211_ENCR_CCMP_ENABLED,
198         NDIS_80211_ENCR_CCMP_KEY_ABSENT
199 };
200
201 enum ndis_80211_priv_filter {
202         NDIS_80211_PRIV_ACCEPT_ALL,
203         NDIS_80211_PRIV_8021X_WEP
204 };
205
206 enum ndis_80211_status_type {
207         NDIS_80211_STATUSTYPE_AUTHENTICATION,
208         NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
209         NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
210         NDIS_80211_STATUSTYPE_RADIOSTATE,
211 };
212
213 enum ndis_80211_media_stream_mode {
214         NDIS_80211_MEDIA_STREAM_OFF,
215         NDIS_80211_MEDIA_STREAM_ON
216 };
217
218 enum ndis_80211_radio_status {
219         NDIS_80211_RADIO_STATUS_ON,
220         NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
221         NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
222 };
223
224 enum ndis_80211_addkey_bits {
225         NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
226         NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
227         NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
228         NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
229 };
230
231 enum ndis_80211_addwep_bits {
232         NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
233         NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
234 };
235
236 struct ndis_80211_auth_request {
237         __le32 length;
238         u8 bssid[6];
239         u8 padding[2];
240         __le32 flags;
241 } __packed;
242
243 struct ndis_80211_pmkid_candidate {
244         u8 bssid[6];
245         u8 padding[2];
246         __le32 flags;
247 } __packed;
248
249 struct ndis_80211_pmkid_cand_list {
250         __le32 version;
251         __le32 num_candidates;
252         struct ndis_80211_pmkid_candidate candidate_list[0];
253 } __packed;
254
255 struct ndis_80211_status_indication {
256         __le32 status_type;
257         union {
258                 __le32                                  media_stream_mode;
259                 __le32                                  radio_status;
260                 struct ndis_80211_auth_request          auth_request[0];
261                 struct ndis_80211_pmkid_cand_list       cand_list;
262         } u;
263 } __packed;
264
265 struct ndis_80211_ssid {
266         __le32 length;
267         u8 essid[NDIS_802_11_LENGTH_SSID];
268 } __packed;
269
270 struct ndis_80211_conf_freq_hop {
271         __le32 length;
272         __le32 hop_pattern;
273         __le32 hop_set;
274         __le32 dwell_time;
275 } __packed;
276
277 struct ndis_80211_conf {
278         __le32 length;
279         __le32 beacon_period;
280         __le32 atim_window;
281         __le32 ds_config;
282         struct ndis_80211_conf_freq_hop fh_config;
283 } __packed;
284
285 struct ndis_80211_bssid_ex {
286         __le32 length;
287         u8 mac[6];
288         u8 padding[2];
289         struct ndis_80211_ssid ssid;
290         __le32 privacy;
291         __le32 rssi;
292         __le32 net_type;
293         struct ndis_80211_conf config;
294         __le32 net_infra;
295         u8 rates[NDIS_802_11_LENGTH_RATES_EX];
296         __le32 ie_length;
297         u8 ies[0];
298 } __packed;
299
300 struct ndis_80211_bssid_list_ex {
301         __le32 num_items;
302         struct ndis_80211_bssid_ex bssid[0];
303 } __packed;
304
305 struct ndis_80211_fixed_ies {
306         u8 timestamp[8];
307         __le16 beacon_interval;
308         __le16 capabilities;
309 } __packed;
310
311 struct ndis_80211_wep_key {
312         __le32 size;
313         __le32 index;
314         __le32 length;
315         u8 material[32];
316 } __packed;
317
318 struct ndis_80211_key {
319         __le32 size;
320         __le32 index;
321         __le32 length;
322         u8 bssid[6];
323         u8 padding[6];
324         u8 rsc[8];
325         u8 material[32];
326 } __packed;
327
328 struct ndis_80211_remove_key {
329         __le32 size;
330         __le32 index;
331         u8 bssid[6];
332         u8 padding[2];
333 } __packed;
334
335 struct ndis_config_param {
336         __le32 name_offs;
337         __le32 name_length;
338         __le32 type;
339         __le32 value_offs;
340         __le32 value_length;
341 } __packed;
342
343 struct ndis_80211_assoc_info {
344         __le32 length;
345         __le16 req_ies;
346         struct req_ie {
347                 __le16 capa;
348                 __le16 listen_interval;
349                 u8 cur_ap_address[6];
350         } req_ie;
351         __le32 req_ie_length;
352         __le32 offset_req_ies;
353         __le16 resp_ies;
354         struct resp_ie {
355                 __le16 capa;
356                 __le16 status_code;
357                 __le16 assoc_id;
358         } resp_ie;
359         __le32 resp_ie_length;
360         __le32 offset_resp_ies;
361 } __packed;
362
363 struct ndis_80211_auth_encr_pair {
364         __le32 auth_mode;
365         __le32 encr_mode;
366 } __packed;
367
368 struct ndis_80211_capability {
369         __le32 length;
370         __le32 version;
371         __le32 num_pmkids;
372         __le32 num_auth_encr_pair;
373         struct ndis_80211_auth_encr_pair auth_encr_pair[0];
374 } __packed;
375
376 struct ndis_80211_bssid_info {
377         u8 bssid[6];
378         u8 pmkid[16];
379 };
380
381 struct ndis_80211_pmkid {
382         __le32 length;
383         __le32 bssid_info_count;
384         struct ndis_80211_bssid_info bssid_info[0];
385 };
386
387 /*
388  *  private data
389  */
390 #define NET_TYPE_11FB   0
391
392 #define CAP_MODE_80211A         1
393 #define CAP_MODE_80211B         2
394 #define CAP_MODE_80211G         4
395 #define CAP_MODE_MASK           7
396
397 #define WORK_LINK_UP            (1<<0)
398 #define WORK_LINK_DOWN          (1<<1)
399 #define WORK_SET_MULTICAST_LIST (1<<2)
400
401 #define RNDIS_WLAN_ALG_NONE     0
402 #define RNDIS_WLAN_ALG_WEP      (1<<0)
403 #define RNDIS_WLAN_ALG_TKIP     (1<<1)
404 #define RNDIS_WLAN_ALG_CCMP     (1<<2)
405
406 #define RNDIS_WLAN_KEY_MGMT_NONE        0
407 #define RNDIS_WLAN_KEY_MGMT_802_1X      (1<<0)
408 #define RNDIS_WLAN_KEY_MGMT_PSK         (1<<1)
409
410 #define COMMAND_BUFFER_SIZE     (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
411
412 static const struct ieee80211_channel rndis_channels[] = {
413         { .center_freq = 2412 },
414         { .center_freq = 2417 },
415         { .center_freq = 2422 },
416         { .center_freq = 2427 },
417         { .center_freq = 2432 },
418         { .center_freq = 2437 },
419         { .center_freq = 2442 },
420         { .center_freq = 2447 },
421         { .center_freq = 2452 },
422         { .center_freq = 2457 },
423         { .center_freq = 2462 },
424         { .center_freq = 2467 },
425         { .center_freq = 2472 },
426         { .center_freq = 2484 },
427 };
428
429 static const struct ieee80211_rate rndis_rates[] = {
430         { .bitrate = 10 },
431         { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
432         { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
433         { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
434         { .bitrate = 60 },
435         { .bitrate = 90 },
436         { .bitrate = 120 },
437         { .bitrate = 180 },
438         { .bitrate = 240 },
439         { .bitrate = 360 },
440         { .bitrate = 480 },
441         { .bitrate = 540 }
442 };
443
444 static const u32 rndis_cipher_suites[] = {
445         WLAN_CIPHER_SUITE_WEP40,
446         WLAN_CIPHER_SUITE_WEP104,
447         WLAN_CIPHER_SUITE_TKIP,
448         WLAN_CIPHER_SUITE_CCMP,
449 };
450
451 struct rndis_wlan_encr_key {
452         int len;
453         u32 cipher;
454         u8 material[32];
455         u8 bssid[ETH_ALEN];
456         bool pairwise;
457         bool tx_key;
458 };
459
460 /* RNDIS device private data */
461 struct rndis_wlan_private {
462         struct usbnet *usbdev;
463
464         struct wireless_dev wdev;
465
466         struct cfg80211_scan_request *scan_request;
467
468         struct workqueue_struct *workqueue;
469         struct delayed_work dev_poller_work;
470         struct delayed_work scan_work;
471         struct work_struct work;
472         struct mutex command_lock;
473         unsigned long work_pending;
474         int last_qual;
475
476         struct ieee80211_supported_band band;
477         struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
478         struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
479         u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
480
481         int caps;
482         int multicast_size;
483
484         /* module parameters */
485         char param_country[4];
486         int  param_frameburst;
487         int  param_afterburner;
488         int  param_power_save;
489         int  param_power_output;
490         int  param_roamtrigger;
491         int  param_roamdelta;
492         u32  param_workaround_interval;
493
494         /* hardware state */
495         bool radio_on;
496         int infra_mode;
497         bool connected;
498         u8 bssid[ETH_ALEN];
499         struct ndis_80211_ssid essid;
500         __le32 current_command_oid;
501
502         /* encryption stuff */
503         int  encr_tx_key_index;
504         struct rndis_wlan_encr_key encr_keys[4];
505         int  wpa_version;
506
507         u8 command_buffer[COMMAND_BUFFER_SIZE];
508 };
509
510 /*
511  * cfg80211 ops
512  */
513 static int rndis_change_virtual_intf(struct wiphy *wiphy,
514                                         struct net_device *dev,
515                                         enum nl80211_iftype type, u32 *flags,
516                                         struct vif_params *params);
517
518 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
519                         struct cfg80211_scan_request *request);
520
521 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
522
523 static int rndis_set_tx_power(struct wiphy *wiphy,
524                               enum nl80211_tx_power_setting type,
525                               int mbm);
526 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
527
528 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
529                                 struct cfg80211_connect_params *sme);
530
531 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
532                                 u16 reason_code);
533
534 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
535                                         struct cfg80211_ibss_params *params);
536
537 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
538
539 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
540         struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
541
542 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
543                          u8 key_index, bool pairwise, const u8 *mac_addr,
544                          struct key_params *params);
545
546 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
547                          u8 key_index, bool pairwise, const u8 *mac_addr);
548
549 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
550                                                                 u8 key_index);
551
552 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
553                                         u8 *mac, struct station_info *sinfo);
554
555 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
556                                int idx, u8 *mac, struct station_info *sinfo);
557
558 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
559                                 struct cfg80211_pmksa *pmksa);
560
561 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
562                                 struct cfg80211_pmksa *pmksa);
563
564 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
565
566 static struct cfg80211_ops rndis_config_ops = {
567         .change_virtual_intf = rndis_change_virtual_intf,
568         .scan = rndis_scan,
569         .set_wiphy_params = rndis_set_wiphy_params,
570         .set_tx_power = rndis_set_tx_power,
571         .get_tx_power = rndis_get_tx_power,
572         .connect = rndis_connect,
573         .disconnect = rndis_disconnect,
574         .join_ibss = rndis_join_ibss,
575         .leave_ibss = rndis_leave_ibss,
576         .set_channel = rndis_set_channel,
577         .add_key = rndis_add_key,
578         .del_key = rndis_del_key,
579         .set_default_key = rndis_set_default_key,
580         .get_station = rndis_get_station,
581         .dump_station = rndis_dump_station,
582         .set_pmksa = rndis_set_pmksa,
583         .del_pmksa = rndis_del_pmksa,
584         .flush_pmksa = rndis_flush_pmksa,
585 };
586
587 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
588
589
590 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
591 {
592         return (struct rndis_wlan_private *)dev->driver_priv;
593 }
594
595 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
596 {
597         switch (priv->param_power_output) {
598         default:
599         case 3:
600                 return BCM4320_DEFAULT_TXPOWER_DBM_100;
601         case 2:
602                 return BCM4320_DEFAULT_TXPOWER_DBM_75;
603         case 1:
604                 return BCM4320_DEFAULT_TXPOWER_DBM_50;
605         case 0:
606                 return BCM4320_DEFAULT_TXPOWER_DBM_25;
607         }
608 }
609
610 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
611 {
612         int cipher = priv->encr_keys[idx].cipher;
613
614         return (cipher == WLAN_CIPHER_SUITE_CCMP ||
615                 cipher == WLAN_CIPHER_SUITE_TKIP);
616 }
617
618 static int rndis_cipher_to_alg(u32 cipher)
619 {
620         switch (cipher) {
621         default:
622                 return RNDIS_WLAN_ALG_NONE;
623         case WLAN_CIPHER_SUITE_WEP40:
624         case WLAN_CIPHER_SUITE_WEP104:
625                 return RNDIS_WLAN_ALG_WEP;
626         case WLAN_CIPHER_SUITE_TKIP:
627                 return RNDIS_WLAN_ALG_TKIP;
628         case WLAN_CIPHER_SUITE_CCMP:
629                 return RNDIS_WLAN_ALG_CCMP;
630         }
631 }
632
633 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
634 {
635         switch (akm_suite) {
636         default:
637                 return RNDIS_WLAN_KEY_MGMT_NONE;
638         case WLAN_AKM_SUITE_8021X:
639                 return RNDIS_WLAN_KEY_MGMT_802_1X;
640         case WLAN_AKM_SUITE_PSK:
641                 return RNDIS_WLAN_KEY_MGMT_PSK;
642         }
643 }
644
645 #ifdef DEBUG
646 static const char *oid_to_string(__le32 oid)
647 {
648         switch (oid) {
649 #define OID_STR(oid) case oid: return(#oid)
650                 /* from rndis_host.h */
651                 OID_STR(OID_802_3_PERMANENT_ADDRESS);
652                 OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
653                 OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
654                 OID_STR(OID_GEN_PHYSICAL_MEDIUM);
655
656                 /* from rndis_wlan.c */
657                 OID_STR(OID_GEN_LINK_SPEED);
658                 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
659
660                 OID_STR(OID_GEN_XMIT_OK);
661                 OID_STR(OID_GEN_RCV_OK);
662                 OID_STR(OID_GEN_XMIT_ERROR);
663                 OID_STR(OID_GEN_RCV_ERROR);
664                 OID_STR(OID_GEN_RCV_NO_BUFFER);
665
666                 OID_STR(OID_802_3_CURRENT_ADDRESS);
667                 OID_STR(OID_802_3_MULTICAST_LIST);
668                 OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
669
670                 OID_STR(OID_802_11_BSSID);
671                 OID_STR(OID_802_11_SSID);
672                 OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
673                 OID_STR(OID_802_11_ADD_WEP);
674                 OID_STR(OID_802_11_REMOVE_WEP);
675                 OID_STR(OID_802_11_DISASSOCIATE);
676                 OID_STR(OID_802_11_AUTHENTICATION_MODE);
677                 OID_STR(OID_802_11_PRIVACY_FILTER);
678                 OID_STR(OID_802_11_BSSID_LIST_SCAN);
679                 OID_STR(OID_802_11_ENCRYPTION_STATUS);
680                 OID_STR(OID_802_11_ADD_KEY);
681                 OID_STR(OID_802_11_REMOVE_KEY);
682                 OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
683                 OID_STR(OID_802_11_PMKID);
684                 OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
685                 OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
686                 OID_STR(OID_802_11_TX_POWER_LEVEL);
687                 OID_STR(OID_802_11_RSSI);
688                 OID_STR(OID_802_11_RSSI_TRIGGER);
689                 OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
690                 OID_STR(OID_802_11_RTS_THRESHOLD);
691                 OID_STR(OID_802_11_SUPPORTED_RATES);
692                 OID_STR(OID_802_11_CONFIGURATION);
693                 OID_STR(OID_802_11_BSSID_LIST);
694 #undef OID_STR
695         }
696
697         return "?";
698 }
699 #else
700 static const char *oid_to_string(__le32 oid)
701 {
702         return "?";
703 }
704 #endif
705
706 /* translate error code */
707 static int rndis_error_status(__le32 rndis_status)
708 {
709         int ret = -EINVAL;
710         switch (rndis_status) {
711         case RNDIS_STATUS_SUCCESS:
712                 ret = 0;
713                 break;
714         case RNDIS_STATUS_FAILURE:
715         case RNDIS_STATUS_INVALID_DATA:
716                 ret = -EINVAL;
717                 break;
718         case RNDIS_STATUS_NOT_SUPPORTED:
719                 ret = -EOPNOTSUPP;
720                 break;
721         case RNDIS_STATUS_ADAPTER_NOT_READY:
722         case RNDIS_STATUS_ADAPTER_NOT_OPEN:
723                 ret = -EBUSY;
724                 break;
725         }
726         return ret;
727 }
728
729 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
730 {
731         struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
732         union {
733                 void                    *buf;
734                 struct rndis_msg_hdr    *header;
735                 struct rndis_query      *get;
736                 struct rndis_query_c    *get_c;
737         } u;
738         int ret, buflen;
739         int resplen, respoffs, copylen;
740
741         buflen = *len + sizeof(*u.get);
742         if (buflen < CONTROL_BUFFER_SIZE)
743                 buflen = CONTROL_BUFFER_SIZE;
744
745         if (buflen > COMMAND_BUFFER_SIZE) {
746                 u.buf = kmalloc(buflen, GFP_KERNEL);
747                 if (!u.buf)
748                         return -ENOMEM;
749         } else {
750                 u.buf = priv->command_buffer;
751         }
752
753         mutex_lock(&priv->command_lock);
754
755         memset(u.get, 0, sizeof *u.get);
756         u.get->msg_type = RNDIS_MSG_QUERY;
757         u.get->msg_len = cpu_to_le32(sizeof *u.get);
758         u.get->oid = oid;
759
760         priv->current_command_oid = oid;
761         ret = rndis_command(dev, u.header, buflen);
762         priv->current_command_oid = 0;
763         if (ret < 0)
764                 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
765                            __func__, oid_to_string(oid), ret,
766                            le32_to_cpu(u.get_c->status));
767
768         if (ret == 0) {
769                 resplen = le32_to_cpu(u.get_c->len);
770                 respoffs = le32_to_cpu(u.get_c->offset) + 8;
771
772                 if (respoffs > buflen) {
773                         /* Device returned data offset outside buffer, error. */
774                         netdev_dbg(dev->net, "%s(%s): received invalid "
775                                 "data offset: %d > %d\n", __func__,
776                                 oid_to_string(oid), respoffs, buflen);
777
778                         ret = -EINVAL;
779                         goto exit_unlock;
780                 }
781
782                 if ((resplen + respoffs) > buflen) {
783                         /* Device would have returned more data if buffer would
784                          * have been big enough. Copy just the bits that we got.
785                          */
786                         copylen = buflen - respoffs;
787                 } else {
788                         copylen = resplen;
789                 }
790
791                 if (copylen > *len)
792                         copylen = *len;
793
794                 memcpy(data, u.buf + respoffs, copylen);
795
796                 *len = resplen;
797
798                 ret = rndis_error_status(u.get_c->status);
799                 if (ret < 0)
800                         netdev_dbg(dev->net, "%s(%s): device returned error,  0x%08x (%d)\n",
801                                    __func__, oid_to_string(oid),
802                                    le32_to_cpu(u.get_c->status), ret);
803         }
804
805 exit_unlock:
806         mutex_unlock(&priv->command_lock);
807
808         if (u.buf != priv->command_buffer)
809                 kfree(u.buf);
810         return ret;
811 }
812
813 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
814 {
815         struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
816         union {
817                 void                    *buf;
818                 struct rndis_msg_hdr    *header;
819                 struct rndis_set        *set;
820                 struct rndis_set_c      *set_c;
821         } u;
822         int ret, buflen;
823
824         buflen = len + sizeof(*u.set);
825         if (buflen < CONTROL_BUFFER_SIZE)
826                 buflen = CONTROL_BUFFER_SIZE;
827
828         if (buflen > COMMAND_BUFFER_SIZE) {
829                 u.buf = kmalloc(buflen, GFP_KERNEL);
830                 if (!u.buf)
831                         return -ENOMEM;
832         } else {
833                 u.buf = priv->command_buffer;
834         }
835
836         mutex_lock(&priv->command_lock);
837
838         memset(u.set, 0, sizeof *u.set);
839         u.set->msg_type = RNDIS_MSG_SET;
840         u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
841         u.set->oid = oid;
842         u.set->len = cpu_to_le32(len);
843         u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
844         u.set->handle = cpu_to_le32(0);
845         memcpy(u.buf + sizeof(*u.set), data, len);
846
847         priv->current_command_oid = oid;
848         ret = rndis_command(dev, u.header, buflen);
849         priv->current_command_oid = 0;
850         if (ret < 0)
851                 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
852                            __func__, oid_to_string(oid), ret,
853                            le32_to_cpu(u.set_c->status));
854
855         if (ret == 0) {
856                 ret = rndis_error_status(u.set_c->status);
857
858                 if (ret < 0)
859                         netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
860                                    __func__, oid_to_string(oid),
861                                    le32_to_cpu(u.set_c->status), ret);
862         }
863
864         mutex_unlock(&priv->command_lock);
865
866         if (u.buf != priv->command_buffer)
867                 kfree(u.buf);
868         return ret;
869 }
870
871 static int rndis_reset(struct usbnet *usbdev)
872 {
873         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
874         struct rndis_reset *reset;
875         int ret;
876
877         mutex_lock(&priv->command_lock);
878
879         reset = (void *)priv->command_buffer;
880         memset(reset, 0, sizeof(*reset));
881         reset->msg_type = RNDIS_MSG_RESET;
882         reset->msg_len = cpu_to_le32(sizeof(*reset));
883         priv->current_command_oid = 0;
884         ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
885
886         mutex_unlock(&priv->command_lock);
887
888         if (ret < 0)
889                 return ret;
890         return 0;
891 }
892
893 /*
894  * Specs say that we can only set config parameters only soon after device
895  * initialization.
896  *   value_type: 0 = u32, 2 = unicode string
897  */
898 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
899                                                 int value_type, void *value)
900 {
901         struct ndis_config_param *infobuf;
902         int value_len, info_len, param_len, ret, i;
903         __le16 *unibuf;
904         __le32 *dst_value;
905
906         if (value_type == 0)
907                 value_len = sizeof(__le32);
908         else if (value_type == 2)
909                 value_len = strlen(value) * sizeof(__le16);
910         else
911                 return -EINVAL;
912
913         param_len = strlen(param) * sizeof(__le16);
914         info_len = sizeof(*infobuf) + param_len + value_len;
915
916 #ifdef DEBUG
917         info_len += 12;
918 #endif
919         infobuf = kmalloc(info_len, GFP_KERNEL);
920         if (!infobuf)
921                 return -ENOMEM;
922
923 #ifdef DEBUG
924         info_len -= 12;
925         /* extra 12 bytes are for padding (debug output) */
926         memset(infobuf, 0xCC, info_len + 12);
927 #endif
928
929         if (value_type == 2)
930                 netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
931                            param, (u8 *)value);
932         else
933                 netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
934                            param, *(u32 *)value);
935
936         infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
937         infobuf->name_length = cpu_to_le32(param_len);
938         infobuf->type = cpu_to_le32(value_type);
939         infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
940         infobuf->value_length = cpu_to_le32(value_len);
941
942         /* simple string to unicode string conversion */
943         unibuf = (void *)infobuf + sizeof(*infobuf);
944         for (i = 0; i < param_len / sizeof(__le16); i++)
945                 unibuf[i] = cpu_to_le16(param[i]);
946
947         if (value_type == 2) {
948                 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
949                 for (i = 0; i < value_len / sizeof(__le16); i++)
950                         unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
951         } else {
952                 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
953                 *dst_value = cpu_to_le32(*(u32 *)value);
954         }
955
956 #ifdef DEBUG
957         netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
958         for (i = 0; i < info_len; i += 12) {
959                 u32 *tmp = (u32 *)((u8 *)infobuf + i);
960                 netdev_dbg(dev->net, "%08X:%08X:%08X\n",
961                            cpu_to_be32(tmp[0]),
962                            cpu_to_be32(tmp[1]),
963                            cpu_to_be32(tmp[2]));
964         }
965 #endif
966
967         ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
968                                                         infobuf, info_len);
969         if (ret != 0)
970                 netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
971                            ret);
972
973         kfree(infobuf);
974         return ret;
975 }
976
977 static int rndis_set_config_parameter_str(struct usbnet *dev,
978                                                 char *param, char *value)
979 {
980         return rndis_set_config_parameter(dev, param, 2, value);
981 }
982
983 /*
984  * data conversion functions
985  */
986 static int level_to_qual(int level)
987 {
988         int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
989         return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
990 }
991
992 /*
993  * common functions
994  */
995 static int set_infra_mode(struct usbnet *usbdev, int mode);
996 static void restore_keys(struct usbnet *usbdev);
997 static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
998                                         bool *matched);
999
1000 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
1001 {
1002         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1003         int ret;
1004
1005         ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
1006         if (ret < 0) {
1007                 netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
1008                 return ret;
1009         }
1010         if (ret == 0) {
1011                 memcpy(&priv->essid, ssid, sizeof(priv->essid));
1012                 priv->radio_on = true;
1013                 netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
1014         }
1015
1016         return ret;
1017 }
1018
1019 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1020 {
1021         int ret;
1022
1023         ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1024         if (ret < 0) {
1025                 netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1026                             bssid, ret);
1027                 return ret;
1028         }
1029
1030         return ret;
1031 }
1032
1033 static int clear_bssid(struct usbnet *usbdev)
1034 {
1035         u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1036
1037         return set_bssid(usbdev, broadcast_mac);
1038 }
1039
1040 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1041 {
1042         int ret, len;
1043
1044         len = ETH_ALEN;
1045         ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1046
1047         if (ret != 0)
1048                 memset(bssid, 0, ETH_ALEN);
1049
1050         return ret;
1051 }
1052
1053 static int get_association_info(struct usbnet *usbdev,
1054                         struct ndis_80211_assoc_info *info, int len)
1055 {
1056         return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1057                                 info, &len);
1058 }
1059
1060 static bool is_associated(struct usbnet *usbdev)
1061 {
1062         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1063         u8 bssid[ETH_ALEN];
1064         int ret;
1065
1066         if (!priv->radio_on)
1067                 return false;
1068
1069         ret = get_bssid(usbdev, bssid);
1070
1071         return (ret == 0 && !is_zero_ether_addr(bssid));
1072 }
1073
1074 static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1075 {
1076         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1077         struct ndis_80211_ssid ssid;
1078         int i, ret = 0;
1079
1080         if (priv->radio_on) {
1081                 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1082                 if (ret == 0) {
1083                         priv->radio_on = false;
1084                         netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1085                                    __func__);
1086
1087                         if (reset_ssid)
1088                                 msleep(100);
1089                 }
1090         }
1091
1092         /* disassociate causes radio to be turned off; if reset_ssid
1093          * is given, set random ssid to enable radio */
1094         if (reset_ssid) {
1095                 /* Set device to infrastructure mode so we don't get ad-hoc
1096                  * 'media connect' indications with the random ssid.
1097                  */
1098                 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1099
1100                 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1101                 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1102                 ssid.essid[0] = 0x1;
1103                 ssid.essid[1] = 0xff;
1104                 for (i = 2; i < sizeof(ssid.essid); i++)
1105                         ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1106                 ret = set_essid(usbdev, &ssid);
1107         }
1108         return ret;
1109 }
1110
1111 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1112                                 enum nl80211_auth_type auth_type, int keymgmt)
1113 {
1114         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1115         __le32 tmp;
1116         int auth_mode, ret;
1117
1118         netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1119                    __func__, wpa_version, auth_type, keymgmt);
1120
1121         if (wpa_version & NL80211_WPA_VERSION_2) {
1122                 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1123                         auth_mode = NDIS_80211_AUTH_WPA2;
1124                 else
1125                         auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1126         } else if (wpa_version & NL80211_WPA_VERSION_1) {
1127                 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1128                         auth_mode = NDIS_80211_AUTH_WPA;
1129                 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1130                         auth_mode = NDIS_80211_AUTH_WPA_PSK;
1131                 else
1132                         auth_mode = NDIS_80211_AUTH_WPA_NONE;
1133         } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1134                 auth_mode = NDIS_80211_AUTH_SHARED;
1135         else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1136                 auth_mode = NDIS_80211_AUTH_OPEN;
1137         else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1138                 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1139         else
1140                 return -ENOTSUPP;
1141
1142         tmp = cpu_to_le32(auth_mode);
1143         ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1144                                                                 sizeof(tmp));
1145         if (ret != 0) {
1146                 netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1147                             ret);
1148                 return ret;
1149         }
1150
1151         priv->wpa_version = wpa_version;
1152
1153         return 0;
1154 }
1155
1156 static int set_priv_filter(struct usbnet *usbdev)
1157 {
1158         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1159         __le32 tmp;
1160
1161         netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1162                    __func__, priv->wpa_version);
1163
1164         if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1165             priv->wpa_version & NL80211_WPA_VERSION_1)
1166                 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1167         else
1168                 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1169
1170         return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1171                                                                 sizeof(tmp));
1172 }
1173
1174 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1175 {
1176         __le32 tmp;
1177         int encr_mode, ret;
1178
1179         netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1180                    __func__, pairwise, groupwise);
1181
1182         if (pairwise & RNDIS_WLAN_ALG_CCMP)
1183                 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1184         else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1185                 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1186         else if (pairwise & RNDIS_WLAN_ALG_WEP)
1187                 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1188         else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1189                 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1190         else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1191                 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1192         else
1193                 encr_mode = NDIS_80211_ENCR_DISABLED;
1194
1195         tmp = cpu_to_le32(encr_mode);
1196         ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1197                                                                 sizeof(tmp));
1198         if (ret != 0) {
1199                 netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1200                             ret);
1201                 return ret;
1202         }
1203
1204         return 0;
1205 }
1206
1207 static int set_infra_mode(struct usbnet *usbdev, int mode)
1208 {
1209         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1210         __le32 tmp;
1211         int ret;
1212
1213         netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1214                    __func__, priv->infra_mode);
1215
1216         tmp = cpu_to_le32(mode);
1217         ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1218                                                                 sizeof(tmp));
1219         if (ret != 0) {
1220                 netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1221                             ret);
1222                 return ret;
1223         }
1224
1225         /* NDIS drivers clear keys when infrastructure mode is
1226          * changed. But Linux tools assume otherwise. So set the
1227          * keys */
1228         restore_keys(usbdev);
1229
1230         priv->infra_mode = mode;
1231         return 0;
1232 }
1233
1234 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1235 {
1236         __le32 tmp;
1237
1238         netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1239
1240         if (rts_threshold < 0 || rts_threshold > 2347)
1241                 rts_threshold = 2347;
1242
1243         tmp = cpu_to_le32(rts_threshold);
1244         return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1245                                                                 sizeof(tmp));
1246 }
1247
1248 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1249 {
1250         __le32 tmp;
1251
1252         netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1253
1254         if (frag_threshold < 256 || frag_threshold > 2346)
1255                 frag_threshold = 2346;
1256
1257         tmp = cpu_to_le32(frag_threshold);
1258         return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1259                                                                 sizeof(tmp));
1260 }
1261
1262 static void set_default_iw_params(struct usbnet *usbdev)
1263 {
1264         set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1265         set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1266                                                 RNDIS_WLAN_KEY_MGMT_NONE);
1267         set_priv_filter(usbdev);
1268         set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1269 }
1270
1271 static int deauthenticate(struct usbnet *usbdev)
1272 {
1273         int ret;
1274
1275         ret = disassociate(usbdev, true);
1276         set_default_iw_params(usbdev);
1277         return ret;
1278 }
1279
1280 static int set_channel(struct usbnet *usbdev, int channel)
1281 {
1282         struct ndis_80211_conf config;
1283         unsigned int dsconfig;
1284         int len, ret;
1285
1286         netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1287
1288         /* this OID is valid only when not associated */
1289         if (is_associated(usbdev))
1290                 return 0;
1291
1292         dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1293
1294         len = sizeof(config);
1295         ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1296         if (ret < 0) {
1297                 netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1298                            __func__);
1299                 return ret;
1300         }
1301
1302         config.ds_config = cpu_to_le32(dsconfig);
1303         ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1304                                                                 sizeof(config));
1305
1306         netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1307
1308         return ret;
1309 }
1310
1311 /* index must be 0 - N, as per NDIS  */
1312 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1313                                                                 int index)
1314 {
1315         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1316         struct ndis_80211_wep_key ndis_key;
1317         u32 cipher;
1318         int ret;
1319
1320         netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1321                    __func__, index, key_len);
1322
1323         if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1324                 return -EINVAL;
1325
1326         if (key_len == 5)
1327                 cipher = WLAN_CIPHER_SUITE_WEP40;
1328         else
1329                 cipher = WLAN_CIPHER_SUITE_WEP104;
1330
1331         memset(&ndis_key, 0, sizeof(ndis_key));
1332
1333         ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1334         ndis_key.length = cpu_to_le32(key_len);
1335         ndis_key.index = cpu_to_le32(index);
1336         memcpy(&ndis_key.material, key, key_len);
1337
1338         if (index == priv->encr_tx_key_index) {
1339                 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1340                 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1341                                                         RNDIS_WLAN_ALG_NONE);
1342                 if (ret)
1343                         netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1344                                     ret);
1345         }
1346
1347         ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1348                                                         sizeof(ndis_key));
1349         if (ret != 0) {
1350                 netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1351                             index + 1, ret);
1352                 return ret;
1353         }
1354
1355         priv->encr_keys[index].len = key_len;
1356         priv->encr_keys[index].cipher = cipher;
1357         memcpy(&priv->encr_keys[index].material, key, key_len);
1358         memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1359
1360         return 0;
1361 }
1362
1363 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1364                         int index, const u8 *addr, const u8 *rx_seq,
1365                         int seq_len, u32 cipher, __le32 flags)
1366 {
1367         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1368         struct ndis_80211_key ndis_key;
1369         bool is_addr_ok;
1370         int ret;
1371
1372         if (index < 0 || index >= 4) {
1373                 netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1374                            __func__, index);
1375                 return -EINVAL;
1376         }
1377         if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1378                 netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1379                            __func__, key_len);
1380                 return -EINVAL;
1381         }
1382         if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1383                 if (!rx_seq || seq_len <= 0) {
1384                         netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1385                                    __func__);
1386                         return -EINVAL;
1387                 }
1388                 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1389                         netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1390                         return -EINVAL;
1391                 }
1392         }
1393
1394         is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1395                                         !is_broadcast_ether_addr(addr);
1396         if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1397                 netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1398                            __func__, addr);
1399                 return -EINVAL;
1400         }
1401
1402         netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1403                    __func__, index,
1404                    !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1405                    !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1406                    !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1407
1408         memset(&ndis_key, 0, sizeof(ndis_key));
1409
1410         ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1411                                 sizeof(ndis_key.material) + key_len);
1412         ndis_key.length = cpu_to_le32(key_len);
1413         ndis_key.index = cpu_to_le32(index) | flags;
1414
1415         if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1416                 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1417                  * different order than NDIS spec, so swap the order here. */
1418                 memcpy(ndis_key.material, key, 16);
1419                 memcpy(ndis_key.material + 16, key + 24, 8);
1420                 memcpy(ndis_key.material + 24, key + 16, 8);
1421         } else
1422                 memcpy(ndis_key.material, key, key_len);
1423
1424         if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1425                 memcpy(ndis_key.rsc, rx_seq, seq_len);
1426
1427         if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1428                 /* pairwise key */
1429                 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1430         } else {
1431                 /* group key */
1432                 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1433                         memset(ndis_key.bssid, 0xff, ETH_ALEN);
1434                 else
1435                         get_bssid(usbdev, ndis_key.bssid);
1436         }
1437
1438         ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1439                                         le32_to_cpu(ndis_key.size));
1440         netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1441                    __func__, ret);
1442         if (ret != 0)
1443                 return ret;
1444
1445         memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1446         priv->encr_keys[index].len = key_len;
1447         priv->encr_keys[index].cipher = cipher;
1448         memcpy(&priv->encr_keys[index].material, key, key_len);
1449         if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1450                 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1451         else
1452                 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1453
1454         if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1455                 priv->encr_tx_key_index = index;
1456
1457         return 0;
1458 }
1459
1460 static int restore_key(struct usbnet *usbdev, int key_idx)
1461 {
1462         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1463         struct rndis_wlan_encr_key key;
1464
1465         if (is_wpa_key(priv, key_idx))
1466                 return 0;
1467
1468         key = priv->encr_keys[key_idx];
1469
1470         netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1471
1472         if (key.len == 0)
1473                 return 0;
1474
1475         return add_wep_key(usbdev, key.material, key.len, key_idx);
1476 }
1477
1478 static void restore_keys(struct usbnet *usbdev)
1479 {
1480         int i;
1481
1482         for (i = 0; i < 4; i++)
1483                 restore_key(usbdev, i);
1484 }
1485
1486 static void clear_key(struct rndis_wlan_private *priv, int idx)
1487 {
1488         memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1489 }
1490
1491 /* remove_key is for both wep and wpa */
1492 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1493 {
1494         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1495         struct ndis_80211_remove_key remove_key;
1496         __le32 keyindex;
1497         bool is_wpa;
1498         int ret;
1499
1500         if (priv->encr_keys[index].len == 0)
1501                 return 0;
1502
1503         is_wpa = is_wpa_key(priv, index);
1504
1505         netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1506                    __func__, index, is_wpa ? "wpa" : "wep",
1507                    priv->encr_keys[index].len);
1508
1509         clear_key(priv, index);
1510
1511         if (is_wpa) {
1512                 remove_key.size = cpu_to_le32(sizeof(remove_key));
1513                 remove_key.index = cpu_to_le32(index);
1514                 if (bssid) {
1515                         /* pairwise key */
1516                         if (!is_broadcast_ether_addr(bssid))
1517                                 remove_key.index |=
1518                                         NDIS_80211_ADDKEY_PAIRWISE_KEY;
1519                         memcpy(remove_key.bssid, bssid,
1520                                         sizeof(remove_key.bssid));
1521                 } else
1522                         memset(remove_key.bssid, 0xff,
1523                                                 sizeof(remove_key.bssid));
1524
1525                 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1526                                                         sizeof(remove_key));
1527                 if (ret != 0)
1528                         return ret;
1529         } else {
1530                 keyindex = cpu_to_le32(index);
1531                 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1532                                                         sizeof(keyindex));
1533                 if (ret != 0) {
1534                         netdev_warn(usbdev->net,
1535                                     "removing encryption key %d failed (%08X)\n",
1536                                     index, ret);
1537                         return ret;
1538                 }
1539         }
1540
1541         /* if it is transmit key, disable encryption */
1542         if (index == priv->encr_tx_key_index)
1543                 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1544
1545         return 0;
1546 }
1547
1548 static void set_multicast_list(struct usbnet *usbdev)
1549 {
1550         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1551         struct netdev_hw_addr *ha;
1552         __le32 filter, basefilter;
1553         int ret;
1554         char *mc_addrs = NULL;
1555         int mc_count;
1556
1557         basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1558                               RNDIS_PACKET_TYPE_BROADCAST;
1559
1560         if (usbdev->net->flags & IFF_PROMISC) {
1561                 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1562                         RNDIS_PACKET_TYPE_ALL_LOCAL;
1563         } else if (usbdev->net->flags & IFF_ALLMULTI) {
1564                 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1565         }
1566
1567         if (filter != basefilter)
1568                 goto set_filter;
1569
1570         /*
1571          * mc_list should be accessed holding the lock, so copy addresses to
1572          * local buffer first.
1573          */
1574         netif_addr_lock_bh(usbdev->net);
1575         mc_count = netdev_mc_count(usbdev->net);
1576         if (mc_count > priv->multicast_size) {
1577                 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1578         } else if (mc_count) {
1579                 int i = 0;
1580
1581                 mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1582                 if (!mc_addrs) {
1583                         netdev_warn(usbdev->net,
1584                                     "couldn't alloc %d bytes of memory\n",
1585                                     mc_count * ETH_ALEN);
1586                         netif_addr_unlock_bh(usbdev->net);
1587                         return;
1588                 }
1589
1590                 netdev_for_each_mc_addr(ha, usbdev->net)
1591                         memcpy(mc_addrs + i++ * ETH_ALEN,
1592                                ha->addr, ETH_ALEN);
1593         }
1594         netif_addr_unlock_bh(usbdev->net);
1595
1596         if (filter != basefilter)
1597                 goto set_filter;
1598
1599         if (mc_count) {
1600                 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
1601                                     mc_count * ETH_ALEN);
1602                 kfree(mc_addrs);
1603                 if (ret == 0)
1604                         filter |= RNDIS_PACKET_TYPE_MULTICAST;
1605                 else
1606                         filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1607
1608                 netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1609                            mc_count, priv->multicast_size, ret);
1610         }
1611
1612 set_filter:
1613         ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1614                                                         sizeof(filter));
1615         if (ret < 0) {
1616                 netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1617                             le32_to_cpu(filter));
1618         }
1619
1620         netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1621                    le32_to_cpu(filter), ret);
1622 }
1623
1624 #ifdef DEBUG
1625 static void debug_print_pmkids(struct usbnet *usbdev,
1626                                 struct ndis_80211_pmkid *pmkids,
1627                                 const char *func_str)
1628 {
1629         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1630         int i, len, count, max_pmkids, entry_len;
1631
1632         max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1633         len = le32_to_cpu(pmkids->length);
1634         count = le32_to_cpu(pmkids->bssid_info_count);
1635
1636         entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1637
1638         netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1639                                 "%d)\n", func_str, count, len, entry_len);
1640
1641         if (count > max_pmkids)
1642                 count = max_pmkids;
1643
1644         for (i = 0; i < count; i++) {
1645                 u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1646
1647                 netdev_dbg(usbdev->net, "%s():  bssid: %pM, "
1648                                 "pmkid: %08X:%08X:%08X:%08X\n",
1649                                 func_str, pmkids->bssid_info[i].bssid,
1650                                 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1651                                 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1652         }
1653 }
1654 #else
1655 static void debug_print_pmkids(struct usbnet *usbdev,
1656                                 struct ndis_80211_pmkid *pmkids,
1657                                 const char *func_str)
1658 {
1659         return;
1660 }
1661 #endif
1662
1663 static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1664 {
1665         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1666         struct ndis_80211_pmkid *pmkids;
1667         int len, ret, max_pmkids;
1668
1669         max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1670         len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
1671
1672         pmkids = kzalloc(len, GFP_KERNEL);
1673         if (!pmkids)
1674                 return ERR_PTR(-ENOMEM);
1675
1676         pmkids->length = cpu_to_le32(len);
1677         pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1678
1679         ret = rndis_query_oid(usbdev, OID_802_11_PMKID, pmkids, &len);
1680         if (ret < 0) {
1681                 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d)"
1682                                 " -> %d\n", __func__, len, max_pmkids, ret);
1683
1684                 kfree(pmkids);
1685                 return ERR_PTR(ret);
1686         }
1687
1688         if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1689                 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1690
1691         debug_print_pmkids(usbdev, pmkids, __func__);
1692
1693         return pmkids;
1694 }
1695
1696 static int set_device_pmkids(struct usbnet *usbdev,
1697                                 struct ndis_80211_pmkid *pmkids)
1698 {
1699         int ret, len, num_pmkids;
1700
1701         num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1702         len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
1703         pmkids->length = cpu_to_le32(len);
1704
1705         debug_print_pmkids(usbdev, pmkids, __func__);
1706
1707         ret = rndis_set_oid(usbdev, OID_802_11_PMKID, pmkids,
1708                                                 le32_to_cpu(pmkids->length));
1709         if (ret < 0) {
1710                 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d) -> %d"
1711                                 "\n", __func__, len, num_pmkids, ret);
1712         }
1713
1714         kfree(pmkids);
1715         return ret;
1716 }
1717
1718 static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1719                                                 struct ndis_80211_pmkid *pmkids,
1720                                                 struct cfg80211_pmksa *pmksa,
1721                                                 int max_pmkids)
1722 {
1723         int i, len, count, newlen, err;
1724
1725         len = le32_to_cpu(pmkids->length);
1726         count = le32_to_cpu(pmkids->bssid_info_count);
1727
1728         if (count > max_pmkids)
1729                 count = max_pmkids;
1730
1731         for (i = 0; i < count; i++)
1732                 if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
1733                                                         pmksa->bssid))
1734                         break;
1735
1736         /* pmkid not found */
1737         if (i == count) {
1738                 netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1739                                         __func__, pmksa->bssid);
1740                 err = -ENOENT;
1741                 goto error;
1742         }
1743
1744         for (; i + 1 < count; i++)
1745                 pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1746
1747         count--;
1748         newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
1749
1750         pmkids->length = cpu_to_le32(newlen);
1751         pmkids->bssid_info_count = cpu_to_le32(count);
1752
1753         return pmkids;
1754 error:
1755         kfree(pmkids);
1756         return ERR_PTR(err);
1757 }
1758
1759 static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1760                                                 struct ndis_80211_pmkid *pmkids,
1761                                                 struct cfg80211_pmksa *pmksa,
1762                                                 int max_pmkids)
1763 {
1764         int i, err, len, count, newlen;
1765
1766         len = le32_to_cpu(pmkids->length);
1767         count = le32_to_cpu(pmkids->bssid_info_count);
1768
1769         if (count > max_pmkids)
1770                 count = max_pmkids;
1771
1772         /* update with new pmkid */
1773         for (i = 0; i < count; i++) {
1774                 if (compare_ether_addr(pmkids->bssid_info[i].bssid,
1775                                                         pmksa->bssid))
1776                         continue;
1777
1778                 memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1779                                                                 WLAN_PMKID_LEN);
1780
1781                 return pmkids;
1782         }
1783
1784         /* out of space, return error */
1785         if (i == max_pmkids) {
1786                 netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1787                 err = -ENOSPC;
1788                 goto error;
1789         }
1790
1791         /* add new pmkid */
1792         newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
1793
1794         pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1795         if (!pmkids) {
1796                 err = -ENOMEM;
1797                 goto error;
1798         }
1799
1800         pmkids->length = cpu_to_le32(newlen);
1801         pmkids->bssid_info_count = cpu_to_le32(count + 1);
1802
1803         memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1804         memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1805
1806         return pmkids;
1807 error:
1808         kfree(pmkids);
1809         return ERR_PTR(err);
1810 }
1811
1812 /*
1813  * cfg80211 ops
1814  */
1815 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1816                                         struct net_device *dev,
1817                                         enum nl80211_iftype type, u32 *flags,
1818                                         struct vif_params *params)
1819 {
1820         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1821         struct usbnet *usbdev = priv->usbdev;
1822         int mode;
1823
1824         switch (type) {
1825         case NL80211_IFTYPE_ADHOC:
1826                 mode = NDIS_80211_INFRA_ADHOC;
1827                 break;
1828         case NL80211_IFTYPE_STATION:
1829                 mode = NDIS_80211_INFRA_INFRA;
1830                 break;
1831         default:
1832                 return -EINVAL;
1833         }
1834
1835         priv->wdev.iftype = type;
1836
1837         return set_infra_mode(usbdev, mode);
1838 }
1839
1840 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1841 {
1842         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1843         struct usbnet *usbdev = priv->usbdev;
1844         int err;
1845
1846         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1847                 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1848                 if (err < 0)
1849                         return err;
1850         }
1851
1852         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1853                 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1854                 if (err < 0)
1855                         return err;
1856         }
1857
1858         return 0;
1859 }
1860
1861 static int rndis_set_tx_power(struct wiphy *wiphy,
1862                               enum nl80211_tx_power_setting type,
1863                               int mbm)
1864 {
1865         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1866         struct usbnet *usbdev = priv->usbdev;
1867
1868         netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1869                    __func__, type, mbm);
1870
1871         if (mbm < 0 || (mbm % 100))
1872                 return -ENOTSUPP;
1873
1874         /* Device doesn't support changing txpower after initialization, only
1875          * turn off/on radio. Support 'auto' mode and setting same dBm that is
1876          * currently used.
1877          */
1878         if (type == NL80211_TX_POWER_AUTOMATIC ||
1879             MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1880                 if (!priv->radio_on)
1881                         disassociate(usbdev, true); /* turn on radio */
1882
1883                 return 0;
1884         }
1885
1886         return -ENOTSUPP;
1887 }
1888
1889 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1890 {
1891         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1892         struct usbnet *usbdev = priv->usbdev;
1893
1894         *dbm = get_bcm4320_power_dbm(priv);
1895
1896         netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1897
1898         return 0;
1899 }
1900
1901 #define SCAN_DELAY_JIFFIES (6 * HZ)
1902 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1903                         struct cfg80211_scan_request *request)
1904 {
1905         struct usbnet *usbdev = netdev_priv(dev);
1906         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1907         int ret;
1908         __le32 tmp;
1909
1910         netdev_dbg(usbdev->net, "cfg80211.scan\n");
1911
1912         /* Get current bssid list from device before new scan, as new scan
1913          * clears internal bssid list.
1914          */
1915         rndis_check_bssid_list(usbdev, NULL, NULL);
1916
1917         if (!request)
1918                 return -EINVAL;
1919
1920         if (priv->scan_request && priv->scan_request != request)
1921                 return -EBUSY;
1922
1923         priv->scan_request = request;
1924
1925         tmp = cpu_to_le32(1);
1926         ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1927                                                         sizeof(tmp));
1928         if (ret == 0) {
1929                 /* Wait before retrieving scan results from device */
1930                 queue_delayed_work(priv->workqueue, &priv->scan_work,
1931                         SCAN_DELAY_JIFFIES);
1932         }
1933
1934         return ret;
1935 }
1936
1937 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1938                                         struct ndis_80211_bssid_ex *bssid)
1939 {
1940         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1941         struct ieee80211_channel *channel;
1942         s32 signal;
1943         u64 timestamp;
1944         u16 capability;
1945         u16 beacon_interval;
1946         struct ndis_80211_fixed_ies *fixed;
1947         int ie_len, bssid_len;
1948         u8 *ie;
1949
1950         netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
1951                    bssid->ssid.essid, bssid->mac);
1952
1953         /* parse bssid structure */
1954         bssid_len = le32_to_cpu(bssid->length);
1955
1956         if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1957                         sizeof(struct ndis_80211_fixed_ies))
1958                 return NULL;
1959
1960         fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1961
1962         ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1963         ie_len = min(bssid_len - (int)sizeof(*bssid),
1964                                         (int)le32_to_cpu(bssid->ie_length));
1965         ie_len -= sizeof(struct ndis_80211_fixed_ies);
1966         if (ie_len < 0)
1967                 return NULL;
1968
1969         /* extract data for cfg80211_inform_bss */
1970         channel = ieee80211_get_channel(priv->wdev.wiphy,
1971                         KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1972         if (!channel)
1973                 return NULL;
1974
1975         signal = level_to_qual(le32_to_cpu(bssid->rssi));
1976         timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1977         capability = le16_to_cpu(fixed->capabilities);
1978         beacon_interval = le16_to_cpu(fixed->beacon_interval);
1979
1980         return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1981                 timestamp, capability, beacon_interval, ie, ie_len, signal,
1982                 GFP_KERNEL);
1983 }
1984
1985 static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
1986                                         bool *matched)
1987 {
1988         void *buf = NULL;
1989         struct ndis_80211_bssid_list_ex *bssid_list;
1990         struct ndis_80211_bssid_ex *bssid;
1991         int ret = -EINVAL, len, count, bssid_len;
1992         bool resized = false;
1993
1994         netdev_dbg(usbdev->net, "check_bssid_list\n");
1995
1996         len = CONTROL_BUFFER_SIZE;
1997 resize_buf:
1998         buf = kmalloc(len, GFP_KERNEL);
1999         if (!buf) {
2000                 ret = -ENOMEM;
2001                 goto out;
2002         }
2003
2004         ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2005         if (ret != 0)
2006                 goto out;
2007
2008         if (!resized && len > CONTROL_BUFFER_SIZE) {
2009                 resized = true;
2010                 kfree(buf);
2011                 goto resize_buf;
2012         }
2013
2014         bssid_list = buf;
2015         bssid = bssid_list->bssid;
2016         bssid_len = le32_to_cpu(bssid->length);
2017         count = le32_to_cpu(bssid_list->num_items);
2018         netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
2019                    count, len);
2020
2021         while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
2022                 if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
2023                     matched) {
2024                         if (compare_ether_addr(bssid->mac, match_bssid))
2025                                 *matched = true;
2026                 }
2027
2028                 bssid = (void *)bssid + bssid_len;
2029                 bssid_len = le32_to_cpu(bssid->length);
2030                 count--;
2031         }
2032
2033 out:
2034         kfree(buf);
2035         return ret;
2036 }
2037
2038 static void rndis_get_scan_results(struct work_struct *work)
2039 {
2040         struct rndis_wlan_private *priv =
2041                 container_of(work, struct rndis_wlan_private, scan_work.work);
2042         struct usbnet *usbdev = priv->usbdev;
2043         int ret;
2044
2045         netdev_dbg(usbdev->net, "get_scan_results\n");
2046
2047         if (!priv->scan_request)
2048                 return;
2049
2050         ret = rndis_check_bssid_list(usbdev, NULL, NULL);
2051
2052         cfg80211_scan_done(priv->scan_request, ret < 0);
2053
2054         priv->scan_request = NULL;
2055 }
2056
2057 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2058                                         struct cfg80211_connect_params *sme)
2059 {
2060         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2061         struct usbnet *usbdev = priv->usbdev;
2062         struct ieee80211_channel *channel = sme->channel;
2063         struct ndis_80211_ssid ssid;
2064         int pairwise = RNDIS_WLAN_ALG_NONE;
2065         int groupwise = RNDIS_WLAN_ALG_NONE;
2066         int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2067         int length, i, ret, chan = -1;
2068
2069         if (channel)
2070                 chan = ieee80211_frequency_to_channel(channel->center_freq);
2071
2072         groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2073         for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2074                 pairwise |=
2075                         rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2076
2077         if (sme->crypto.n_ciphers_pairwise > 0 &&
2078                         pairwise == RNDIS_WLAN_ALG_NONE) {
2079                 netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2080                 return -ENOTSUPP;
2081         }
2082
2083         for (i = 0; i < sme->crypto.n_akm_suites; i++)
2084                 keymgmt |=
2085                         rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2086
2087         if (sme->crypto.n_akm_suites > 0 &&
2088                         keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2089                 netdev_err(usbdev->net, "Invalid keymgmt\n");
2090                 return -ENOTSUPP;
2091         }
2092
2093         netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2094                    sme->ssid, sme->bssid, chan,
2095                    sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2096                    groupwise, pairwise, keymgmt);
2097
2098         if (is_associated(usbdev))
2099                 disassociate(usbdev, false);
2100
2101         ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2102         if (ret < 0) {
2103                 netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2104                            ret);
2105                 goto err_turn_radio_on;
2106         }
2107
2108         ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2109                                                                 keymgmt);
2110         if (ret < 0) {
2111                 netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2112                            ret);
2113                 goto err_turn_radio_on;
2114         }
2115
2116         set_priv_filter(usbdev);
2117
2118         ret = set_encr_mode(usbdev, pairwise, groupwise);
2119         if (ret < 0) {
2120                 netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2121                            ret);
2122                 goto err_turn_radio_on;
2123         }
2124
2125         if (channel) {
2126                 ret = set_channel(usbdev, chan);
2127                 if (ret < 0) {
2128                         netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2129                                    ret);
2130                         goto err_turn_radio_on;
2131                 }
2132         }
2133
2134         if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2135                 priv->encr_tx_key_index = sme->key_idx;
2136                 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2137                 if (ret < 0) {
2138                         netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2139                                    ret, sme->key_len, sme->key_idx);
2140                         goto err_turn_radio_on;
2141                 }
2142         }
2143
2144         if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2145                                 !is_broadcast_ether_addr(sme->bssid)) {
2146                 ret = set_bssid(usbdev, sme->bssid);
2147                 if (ret < 0) {
2148                         netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2149                                    ret);
2150                         goto err_turn_radio_on;
2151                 }
2152         } else
2153                 clear_bssid(usbdev);
2154
2155         length = sme->ssid_len;
2156         if (length > NDIS_802_11_LENGTH_SSID)
2157                 length = NDIS_802_11_LENGTH_SSID;
2158
2159         memset(&ssid, 0, sizeof(ssid));
2160         ssid.length = cpu_to_le32(length);
2161         memcpy(ssid.essid, sme->ssid, length);
2162
2163         /* Pause and purge rx queue, so we don't pass packets before
2164          * 'media connect'-indication.
2165          */
2166         usbnet_pause_rx(usbdev);
2167         usbnet_purge_paused_rxq(usbdev);
2168
2169         ret = set_essid(usbdev, &ssid);
2170         if (ret < 0)
2171                 netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2172         return ret;
2173
2174 err_turn_radio_on:
2175         disassociate(usbdev, true);
2176
2177         return ret;
2178 }
2179
2180 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2181                                                                 u16 reason_code)
2182 {
2183         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2184         struct usbnet *usbdev = priv->usbdev;
2185
2186         netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2187
2188         priv->connected = false;
2189         memset(priv->bssid, 0, ETH_ALEN);
2190
2191         return deauthenticate(usbdev);
2192 }
2193
2194 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2195                                         struct cfg80211_ibss_params *params)
2196 {
2197         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2198         struct usbnet *usbdev = priv->usbdev;
2199         struct ieee80211_channel *channel = params->channel;
2200         struct ndis_80211_ssid ssid;
2201         enum nl80211_auth_type auth_type;
2202         int ret, alg, length, chan = -1;
2203
2204         if (channel)
2205                 chan = ieee80211_frequency_to_channel(channel->center_freq);
2206
2207         /* TODO: How to handle ad-hoc encryption?
2208          * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2209          * pre-shared for encryption (open/shared/wpa), is key set before
2210          * join_ibss? Which auth_type to use (not in params)? What about WPA?
2211          */
2212         if (params->privacy) {
2213                 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2214                 alg = RNDIS_WLAN_ALG_WEP;
2215         } else {
2216                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2217                 alg = RNDIS_WLAN_ALG_NONE;
2218         }
2219
2220         netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2221                    params->ssid, params->bssid, chan, params->privacy);
2222
2223         if (is_associated(usbdev))
2224                 disassociate(usbdev, false);
2225
2226         ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2227         if (ret < 0) {
2228                 netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2229                            ret);
2230                 goto err_turn_radio_on;
2231         }
2232
2233         ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2234         if (ret < 0) {
2235                 netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2236                            ret);
2237                 goto err_turn_radio_on;
2238         }
2239
2240         set_priv_filter(usbdev);
2241
2242         ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2243         if (ret < 0) {
2244                 netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2245                            ret);
2246                 goto err_turn_radio_on;
2247         }
2248
2249         if (channel) {
2250                 ret = set_channel(usbdev, chan);
2251                 if (ret < 0) {
2252                         netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2253                                    ret);
2254                         goto err_turn_radio_on;
2255                 }
2256         }
2257
2258         if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2259                                 !is_broadcast_ether_addr(params->bssid)) {
2260                 ret = set_bssid(usbdev, params->bssid);
2261                 if (ret < 0) {
2262                         netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2263                                    ret);
2264                         goto err_turn_radio_on;
2265                 }
2266         } else
2267                 clear_bssid(usbdev);
2268
2269         length = params->ssid_len;
2270         if (length > NDIS_802_11_LENGTH_SSID)
2271                 length = NDIS_802_11_LENGTH_SSID;
2272
2273         memset(&ssid, 0, sizeof(ssid));
2274         ssid.length = cpu_to_le32(length);
2275         memcpy(ssid.essid, params->ssid, length);
2276
2277         /* Don't need to pause rx queue for ad-hoc. */
2278         usbnet_purge_paused_rxq(usbdev);
2279         usbnet_resume_rx(usbdev);
2280
2281         ret = set_essid(usbdev, &ssid);
2282         if (ret < 0)
2283                 netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2284                            ret);
2285         return ret;
2286
2287 err_turn_radio_on:
2288         disassociate(usbdev, true);
2289
2290         return ret;
2291 }
2292
2293 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2294 {
2295         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2296         struct usbnet *usbdev = priv->usbdev;
2297
2298         netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2299
2300         priv->connected = false;
2301         memset(priv->bssid, 0, ETH_ALEN);
2302
2303         return deauthenticate(usbdev);
2304 }
2305
2306 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
2307         struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2308 {
2309         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2310         struct usbnet *usbdev = priv->usbdev;
2311
2312         return set_channel(usbdev,
2313                         ieee80211_frequency_to_channel(chan->center_freq));
2314 }
2315
2316 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2317                          u8 key_index, bool pairwise, const u8 *mac_addr,
2318                          struct key_params *params)
2319 {
2320         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2321         struct usbnet *usbdev = priv->usbdev;
2322         __le32 flags;
2323
2324         netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2325                    __func__, key_index, mac_addr, params->cipher);
2326
2327         switch (params->cipher) {
2328         case WLAN_CIPHER_SUITE_WEP40:
2329         case WLAN_CIPHER_SUITE_WEP104:
2330                 return add_wep_key(usbdev, params->key, params->key_len,
2331                                                                 key_index);
2332         case WLAN_CIPHER_SUITE_TKIP:
2333         case WLAN_CIPHER_SUITE_CCMP:
2334                 flags = 0;
2335
2336                 if (params->seq && params->seq_len > 0)
2337                         flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2338                 if (mac_addr)
2339                         flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2340                                         NDIS_80211_ADDKEY_TRANSMIT_KEY;
2341
2342                 return add_wpa_key(usbdev, params->key, params->key_len,
2343                                 key_index, mac_addr, params->seq,
2344                                 params->seq_len, params->cipher, flags);
2345         default:
2346                 netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2347                            __func__, params->cipher);
2348                 return -ENOTSUPP;
2349         }
2350 }
2351
2352 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2353                          u8 key_index, bool pairwise, const u8 *mac_addr)
2354 {
2355         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2356         struct usbnet *usbdev = priv->usbdev;
2357
2358         netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2359
2360         return remove_key(usbdev, key_index, mac_addr);
2361 }
2362
2363 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2364                                                                 u8 key_index)
2365 {
2366         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2367         struct usbnet *usbdev = priv->usbdev;
2368         struct rndis_wlan_encr_key key;
2369
2370         netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2371
2372         priv->encr_tx_key_index = key_index;
2373
2374         key = priv->encr_keys[key_index];
2375
2376         return add_wep_key(usbdev, key.material, key.len, key_index);
2377 }
2378
2379 static void rndis_fill_station_info(struct usbnet *usbdev,
2380                                                 struct station_info *sinfo)
2381 {
2382         __le32 linkspeed, rssi;
2383         int ret, len;
2384
2385         memset(sinfo, 0, sizeof(*sinfo));
2386
2387         len = sizeof(linkspeed);
2388         ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2389         if (ret == 0) {
2390                 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2391                 sinfo->filled |= STATION_INFO_TX_BITRATE;
2392         }
2393
2394         len = sizeof(rssi);
2395         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2396         if (ret == 0) {
2397                 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2398                 sinfo->filled |= STATION_INFO_SIGNAL;
2399         }
2400 }
2401
2402 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2403                                         u8 *mac, struct station_info *sinfo)
2404 {
2405         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2406         struct usbnet *usbdev = priv->usbdev;
2407
2408         if (compare_ether_addr(priv->bssid, mac))
2409                 return -ENOENT;
2410
2411         rndis_fill_station_info(usbdev, sinfo);
2412
2413         return 0;
2414 }
2415
2416 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2417                                int idx, u8 *mac, struct station_info *sinfo)
2418 {
2419         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2420         struct usbnet *usbdev = priv->usbdev;
2421
2422         if (idx != 0)
2423                 return -ENOENT;
2424
2425         memcpy(mac, priv->bssid, ETH_ALEN);
2426
2427         rndis_fill_station_info(usbdev, sinfo);
2428
2429         return 0;
2430 }
2431
2432 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2433                                 struct cfg80211_pmksa *pmksa)
2434 {
2435         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2436         struct usbnet *usbdev = priv->usbdev;
2437         struct ndis_80211_pmkid *pmkids;
2438         u32 *tmp = (u32 *)pmksa->pmkid;
2439
2440         netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2441                         pmksa->bssid,
2442                         cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2443                         cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2444
2445         pmkids = get_device_pmkids(usbdev);
2446         if (IS_ERR(pmkids)) {
2447                 /* couldn't read PMKID cache from device */
2448                 return PTR_ERR(pmkids);
2449         }
2450
2451         pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2452         if (IS_ERR(pmkids)) {
2453                 /* not found, list full, etc */
2454                 return PTR_ERR(pmkids);
2455         }
2456
2457         return set_device_pmkids(usbdev, pmkids);
2458 }
2459
2460 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2461                                 struct cfg80211_pmksa *pmksa)
2462 {
2463         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2464         struct usbnet *usbdev = priv->usbdev;
2465         struct ndis_80211_pmkid *pmkids;
2466         u32 *tmp = (u32 *)pmksa->pmkid;
2467
2468         netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2469                         pmksa->bssid,
2470                         cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2471                         cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2472
2473         pmkids = get_device_pmkids(usbdev);
2474         if (IS_ERR(pmkids)) {
2475                 /* Couldn't read PMKID cache from device */
2476                 return PTR_ERR(pmkids);
2477         }
2478
2479         pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2480         if (IS_ERR(pmkids)) {
2481                 /* not found, etc */
2482                 return PTR_ERR(pmkids);
2483         }
2484
2485         return set_device_pmkids(usbdev, pmkids);
2486 }
2487
2488 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2489 {
2490         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2491         struct usbnet *usbdev = priv->usbdev;
2492         struct ndis_80211_pmkid pmkid;
2493
2494         netdev_dbg(usbdev->net, "%s()\n", __func__);
2495
2496         memset(&pmkid, 0, sizeof(pmkid));
2497
2498         pmkid.length = cpu_to_le32(sizeof(pmkid));
2499         pmkid.bssid_info_count = cpu_to_le32(0);
2500
2501         return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
2502 }
2503
2504 static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
2505                                            struct ndis_80211_assoc_info *info)
2506 {
2507         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2508         struct ieee80211_channel *channel;
2509         struct ndis_80211_conf config;
2510         struct ndis_80211_ssid ssid;
2511         s32 signal;
2512         u64 timestamp;
2513         u16 capability;
2514         u16 beacon_interval;
2515         __le32 rssi;
2516         u8 ie_buf[34];
2517         int len, ret, ie_len;
2518
2519         /* Get signal quality, in case of error use rssi=0 and ignore error. */
2520         len = sizeof(rssi);
2521         rssi = 0;
2522         rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2523         signal = level_to_qual(le32_to_cpu(rssi));
2524
2525         netdev_dbg(usbdev->net, "%s(): OID_802_11_RSSI -> %d, "
2526                    "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
2527                    level_to_qual(le32_to_cpu(rssi)));
2528
2529         /* Get AP capabilities */
2530         if (info) {
2531                 capability = le16_to_cpu(info->resp_ie.capa);
2532         } else {
2533                 /* Set atleast ESS/IBSS capability */
2534                 capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
2535                                 WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
2536         }
2537
2538         /* Get channel and beacon interval */
2539         len = sizeof(config);
2540         ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
2541         netdev_dbg(usbdev->net, "%s(): OID_802_11_CONFIGURATION -> %d\n",
2542                                 __func__, ret);
2543         if (ret >= 0) {
2544                 beacon_interval = le16_to_cpu(config.beacon_period);
2545                 channel = ieee80211_get_channel(priv->wdev.wiphy,
2546                                 KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
2547                 if (!channel) {
2548                         netdev_warn(usbdev->net, "%s(): could not get channel."
2549                                                  "\n", __func__);
2550                         return;
2551                 }
2552         } else {
2553                 netdev_warn(usbdev->net, "%s(): could not get configuration.\n",
2554                                          __func__);
2555                 return;
2556         }
2557
2558         /* Get SSID, in case of error, use zero length SSID and ignore error. */
2559         len = sizeof(ssid);
2560         memset(&ssid, 0, sizeof(ssid));
2561         ret = rndis_query_oid(usbdev, OID_802_11_SSID, &ssid, &len);
2562         netdev_dbg(usbdev->net, "%s(): OID_802_11_SSID -> %d, len: %d, ssid: "
2563                                 "'%.32s'\n", __func__, ret,
2564                                 le32_to_cpu(ssid.length), ssid.essid);
2565
2566         if (le32_to_cpu(ssid.length) > 32)
2567                 ssid.length = cpu_to_le32(32);
2568
2569         ie_buf[0] = WLAN_EID_SSID;
2570         ie_buf[1] = le32_to_cpu(ssid.length);
2571         memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
2572
2573         ie_len = le32_to_cpu(ssid.length) + 2;
2574
2575         /* no tsf */
2576         timestamp = 0;
2577
2578         netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
2579                 "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
2580                 "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
2581                 bssid, (u32)timestamp, capability, beacon_interval, ie_len,
2582                 ssid.essid, signal);
2583
2584         cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid,
2585                 timestamp, capability, beacon_interval, ie_buf, ie_len,
2586                 signal, GFP_KERNEL);
2587 }
2588
2589 /*
2590  * workers, indication handlers, device poller
2591  */
2592 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2593 {
2594         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2595         struct ndis_80211_assoc_info *info = NULL;
2596         u8 bssid[ETH_ALEN];
2597         int resp_ie_len, req_ie_len;
2598         u8 *req_ie, *resp_ie;
2599         int ret, offset;
2600         bool roamed = false;
2601         bool match_bss;
2602
2603         if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2604                 /* received media connect indication while connected, either
2605                  * device reassociated with same AP or roamed to new. */
2606                 roamed = true;
2607         }
2608
2609         req_ie_len = 0;
2610         resp_ie_len = 0;
2611         req_ie = NULL;
2612         resp_ie = NULL;
2613
2614         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2615                 info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2616                 if (!info) {
2617                         /* No memory? Try resume work later */
2618                         set_bit(WORK_LINK_UP, &priv->work_pending);
2619                         queue_work(priv->workqueue, &priv->work);
2620                         return;
2621                 }
2622
2623                 /* Get association info IEs from device. */
2624                 ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2625                 if (!ret) {
2626                         req_ie_len = le32_to_cpu(info->req_ie_length);
2627                         if (req_ie_len > 0) {
2628                                 offset = le32_to_cpu(info->offset_req_ies);
2629
2630                                 if (offset > CONTROL_BUFFER_SIZE)
2631                                         offset = CONTROL_BUFFER_SIZE;
2632
2633                                 req_ie = (u8 *)info + offset;
2634
2635                                 if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2636                                         req_ie_len =
2637                                                 CONTROL_BUFFER_SIZE - offset;
2638                         }
2639
2640                         resp_ie_len = le32_to_cpu(info->resp_ie_length);
2641                         if (resp_ie_len > 0) {
2642                                 offset = le32_to_cpu(info->offset_resp_ies);
2643
2644                                 if (offset > CONTROL_BUFFER_SIZE)
2645                                         offset = CONTROL_BUFFER_SIZE;
2646
2647                                 resp_ie = (u8 *)info + offset;
2648
2649                                 if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2650                                         resp_ie_len =
2651                                                 CONTROL_BUFFER_SIZE - offset;
2652                         }
2653                 } else {
2654                         /* Since rndis_wlan_craft_connected_bss() might use info
2655                          * later and expects info to contain valid data if
2656                          * non-null, free info and set NULL here.
2657                          */
2658                         kfree(info);
2659                         info = NULL;
2660                 }
2661         } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2662                 return;
2663
2664         ret = get_bssid(usbdev, bssid);
2665         if (ret < 0)
2666                 memset(bssid, 0, sizeof(bssid));
2667
2668         netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2669                    bssid, roamed ? " roamed" : "");
2670
2671         /* Internal bss list in device should contain at least the currently
2672          * connected bss and we can get it to cfg80211 with
2673          * rndis_check_bssid_list().
2674          *
2675          * NDIS spec says: "If the device is associated, but the associated
2676          *  BSSID is not in its BSSID scan list, then the driver must add an
2677          *  entry for the BSSID at the end of the data that it returns in
2678          *  response to query of OID_802_11_BSSID_LIST."
2679          *
2680          * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
2681          */
2682         match_bss = false;
2683         rndis_check_bssid_list(usbdev, bssid, &match_bss);
2684
2685         if (!is_zero_ether_addr(bssid) && !match_bss) {
2686                 /* Couldn't get bss from device, we need to manually craft bss
2687                  * for cfg80211.
2688                  */
2689                 rndis_wlan_craft_connected_bss(usbdev, bssid, info);
2690         }
2691
2692         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2693                 if (!roamed)
2694                         cfg80211_connect_result(usbdev->net, bssid, req_ie,
2695                                                 req_ie_len, resp_ie,
2696                                                 resp_ie_len, 0, GFP_KERNEL);
2697                 else
2698                         cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2699                                         resp_ie, resp_ie_len, GFP_KERNEL);
2700         } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2701                 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2702
2703         if (info != NULL)
2704                 kfree(info);
2705
2706         priv->connected = true;
2707         memcpy(priv->bssid, bssid, ETH_ALEN);
2708
2709         usbnet_resume_rx(usbdev);
2710         netif_carrier_on(usbdev->net);
2711 }
2712
2713 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2714 {
2715         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2716
2717         if (priv->connected) {
2718                 priv->connected = false;
2719                 memset(priv->bssid, 0, ETH_ALEN);
2720
2721                 deauthenticate(usbdev);
2722
2723                 cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
2724         }
2725
2726         netif_carrier_off(usbdev->net);
2727 }
2728
2729 static void rndis_wlan_worker(struct work_struct *work)
2730 {
2731         struct rndis_wlan_private *priv =
2732                 container_of(work, struct rndis_wlan_private, work);
2733         struct usbnet *usbdev = priv->usbdev;
2734
2735         if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2736                 rndis_wlan_do_link_up_work(usbdev);
2737
2738         if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2739                 rndis_wlan_do_link_down_work(usbdev);
2740
2741         if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2742                 set_multicast_list(usbdev);
2743 }
2744
2745 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2746 {
2747         struct usbnet *usbdev = netdev_priv(dev);
2748         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2749
2750         if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2751                 return;
2752
2753         set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2754         queue_work(priv->workqueue, &priv->work);
2755 }
2756
2757 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2758                                 struct ndis_80211_status_indication *indication,
2759                                 int len)
2760 {
2761         u8 *buf;
2762         const char *type;
2763         int flags, buflen, key_id;
2764         bool pairwise_error, group_error;
2765         struct ndis_80211_auth_request *auth_req;
2766         enum nl80211_key_type key_type;
2767
2768         /* must have at least one array entry */
2769         if (len < offsetof(struct ndis_80211_status_indication, u) +
2770                                 sizeof(struct ndis_80211_auth_request)) {
2771                 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2772                             len);
2773                 return;
2774         }
2775
2776         buf = (void *)&indication->u.auth_request[0];
2777         buflen = len - offsetof(struct ndis_80211_status_indication, u);
2778
2779         while (buflen >= sizeof(*auth_req)) {
2780                 auth_req = (void *)buf;
2781                 type = "unknown";
2782                 flags = le32_to_cpu(auth_req->flags);
2783                 pairwise_error = false;
2784                 group_error = false;
2785
2786                 if (flags & 0x1)
2787                         type = "reauth request";
2788                 if (flags & 0x2)
2789                         type = "key update request";
2790                 if (flags & 0x6) {
2791                         pairwise_error = true;
2792                         type = "pairwise_error";
2793                 }
2794                 if (flags & 0xe) {
2795                         group_error = true;
2796                         type = "group_error";
2797                 }
2798
2799                 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2800                             type, le32_to_cpu(auth_req->flags));
2801
2802                 if (pairwise_error) {
2803                         key_type = NL80211_KEYTYPE_PAIRWISE;
2804                         key_id = -1;
2805
2806                         cfg80211_michael_mic_failure(usbdev->net,
2807                                                         auth_req->bssid,
2808                                                         key_type, key_id, NULL,
2809                                                         GFP_KERNEL);
2810                 }
2811
2812                 if (group_error) {
2813                         key_type = NL80211_KEYTYPE_GROUP;
2814                         key_id = -1;
2815
2816                         cfg80211_michael_mic_failure(usbdev->net,
2817                                                         auth_req->bssid,
2818                                                         key_type, key_id, NULL,
2819                                                         GFP_KERNEL);
2820                 }
2821
2822                 buflen -= le32_to_cpu(auth_req->length);
2823                 buf += le32_to_cpu(auth_req->length);
2824         }
2825 }
2826
2827 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2828                                 struct ndis_80211_status_indication *indication,
2829                                 int len)
2830 {
2831         struct ndis_80211_pmkid_cand_list *cand_list;
2832         int list_len, expected_len, i;
2833
2834         if (len < offsetof(struct ndis_80211_status_indication, u) +
2835                                 sizeof(struct ndis_80211_pmkid_cand_list)) {
2836                 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2837                             len);
2838                 return;
2839         }
2840
2841         list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2842                         sizeof(struct ndis_80211_pmkid_candidate);
2843         expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2844                         offsetof(struct ndis_80211_status_indication, u);
2845
2846         if (len < expected_len) {
2847                 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2848                             len, expected_len);
2849                 return;
2850         }
2851
2852         cand_list = &indication->u.cand_list;
2853
2854         netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2855                     le32_to_cpu(cand_list->version),
2856                     le32_to_cpu(cand_list->num_candidates));
2857
2858         if (le32_to_cpu(cand_list->version) != 1)
2859                 return;
2860
2861         for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2862                 struct ndis_80211_pmkid_candidate *cand =
2863                                                 &cand_list->candidate_list[i];
2864
2865                 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2866                            i, le32_to_cpu(cand->flags), cand->bssid);
2867
2868 #if 0
2869                 struct iw_pmkid_cand pcand;
2870                 union iwreq_data wrqu;
2871
2872                 memset(&pcand, 0, sizeof(pcand));
2873                 if (le32_to_cpu(cand->flags) & 0x01)
2874                         pcand.flags |= IW_PMKID_CAND_PREAUTH;
2875                 pcand.index = i;
2876                 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2877
2878                 memset(&wrqu, 0, sizeof(wrqu));
2879                 wrqu.data.length = sizeof(pcand);
2880                 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2881                                                                 (u8 *)&pcand);
2882 #endif
2883         }
2884 }
2885
2886 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2887                         struct rndis_indicate *msg, int buflen)
2888 {
2889         struct ndis_80211_status_indication *indication;
2890         int len, offset;
2891
2892         offset = offsetof(struct rndis_indicate, status) +
2893                         le32_to_cpu(msg->offset);
2894         len = le32_to_cpu(msg->length);
2895
2896         if (len < 8) {
2897                 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2898                             len);
2899                 return;
2900         }
2901
2902         if (offset + len > buflen) {
2903                 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2904                             offset + len, buflen);
2905                 return;
2906         }
2907
2908         indication = (void *)((u8 *)msg + offset);
2909
2910         switch (le32_to_cpu(indication->status_type)) {
2911         case NDIS_80211_STATUSTYPE_RADIOSTATE:
2912                 netdev_info(usbdev->net, "radio state indication: %i\n",
2913                             le32_to_cpu(indication->u.radio_status));
2914                 return;
2915
2916         case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2917                 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2918                             le32_to_cpu(indication->u.media_stream_mode));
2919                 return;
2920
2921         case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2922                 rndis_wlan_auth_indication(usbdev, indication, len);
2923                 return;
2924
2925         case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2926                 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2927                 return;
2928
2929         default:
2930                 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2931                             le32_to_cpu(indication->status_type));
2932         }
2933 }
2934
2935 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2936 {
2937         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2938         struct rndis_indicate *msg = ind;
2939
2940         switch (msg->status) {
2941         case RNDIS_STATUS_MEDIA_CONNECT:
2942                 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2943                         /* OID_802_11_ADD_KEY causes sometimes extra
2944                          * "media connect" indications which confuses driver
2945                          * and userspace to think that device is
2946                          * roaming/reassociating when it isn't.
2947                          */
2948                         netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
2949                         return;
2950                 }
2951
2952                 usbnet_pause_rx(usbdev);
2953
2954                 netdev_info(usbdev->net, "media connect\n");
2955
2956                 /* queue work to avoid recursive calls into rndis_command */
2957                 set_bit(WORK_LINK_UP, &priv->work_pending);
2958                 queue_work(priv->workqueue, &priv->work);
2959                 break;
2960
2961         case RNDIS_STATUS_MEDIA_DISCONNECT:
2962                 netdev_info(usbdev->net, "media disconnect\n");
2963
2964                 /* queue work to avoid recursive calls into rndis_command */
2965                 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2966                 queue_work(priv->workqueue, &priv->work);
2967                 break;
2968
2969         case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2970                 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2971                 break;
2972
2973         default:
2974                 netdev_info(usbdev->net, "indication: 0x%08x\n",
2975                             le32_to_cpu(msg->status));
2976                 break;
2977         }
2978 }
2979
2980 static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
2981 {
2982         struct {
2983                 __le32  num_items;
2984                 __le32  items[8];
2985         } networks_supported;
2986         struct ndis_80211_capability *caps;
2987         u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
2988         int len, retval, i, n;
2989         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2990
2991         /* determine supported modes */
2992         len = sizeof(networks_supported);
2993         retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2994                                                 &networks_supported, &len);
2995         if (retval >= 0) {
2996                 n = le32_to_cpu(networks_supported.num_items);
2997                 if (n > 8)
2998                         n = 8;
2999                 for (i = 0; i < n; i++) {
3000                         switch (le32_to_cpu(networks_supported.items[i])) {
3001                         case NDIS_80211_TYPE_FREQ_HOP:
3002                         case NDIS_80211_TYPE_DIRECT_SEQ:
3003                                 priv->caps |= CAP_MODE_80211B;
3004                                 break;
3005                         case NDIS_80211_TYPE_OFDM_A:
3006                                 priv->caps |= CAP_MODE_80211A;
3007                                 break;
3008                         case NDIS_80211_TYPE_OFDM_G:
3009                                 priv->caps |= CAP_MODE_80211G;
3010                                 break;
3011                         }
3012                 }
3013         }
3014
3015         /* get device 802.11 capabilities, number of PMKIDs */
3016         caps = (struct ndis_80211_capability *)caps_buf;
3017         len = sizeof(caps_buf);
3018         retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
3019         if (retval >= 0) {
3020                 netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
3021                                 "ver %d, pmkids %d, auth-encr-pairs %d\n",
3022                                 le32_to_cpu(caps->length),
3023                                 le32_to_cpu(caps->version),
3024                                 le32_to_cpu(caps->num_pmkids),
3025                                 le32_to_cpu(caps->num_auth_encr_pair));
3026                 wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
3027         } else
3028                 wiphy->max_num_pmkids = 0;
3029
3030         return retval;
3031 }
3032
3033 #define DEVICE_POLLER_JIFFIES (HZ)
3034 static void rndis_device_poller(struct work_struct *work)
3035 {
3036         struct rndis_wlan_private *priv =
3037                 container_of(work, struct rndis_wlan_private,
3038                                                         dev_poller_work.work);
3039         struct usbnet *usbdev = priv->usbdev;
3040         __le32 rssi, tmp;
3041         int len, ret, j;
3042         int update_jiffies = DEVICE_POLLER_JIFFIES;
3043         void *buf;
3044
3045         /* Only check/do workaround when connected. Calling is_associated()
3046          * also polls device with rndis_command() and catches for media link
3047          * indications.
3048          */
3049         if (!is_associated(usbdev))
3050                 goto end;
3051
3052         len = sizeof(rssi);
3053         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
3054         if (ret == 0)
3055                 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
3056
3057         netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
3058                    ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
3059
3060         /* Workaround transfer stalls on poor quality links.
3061          * TODO: find right way to fix these stalls (as stalls do not happen
3062          * with ndiswrapper/windows driver). */
3063         if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
3064                 /* Decrease stats worker interval to catch stalls.
3065                  * faster. Faster than 400-500ms causes packet loss,
3066                  * Slower doesn't catch stalls fast enough.
3067                  */
3068                 j = msecs_to_jiffies(priv->param_workaround_interval);
3069                 if (j > DEVICE_POLLER_JIFFIES)
3070                         j = DEVICE_POLLER_JIFFIES;
3071                 else if (j <= 0)
3072                         j = 1;
3073                 update_jiffies = j;
3074
3075                 /* Send scan OID. Use of both OIDs is required to get device
3076                  * working.
3077                  */
3078                 tmp = cpu_to_le32(1);
3079                 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
3080                                                                 sizeof(tmp));
3081
3082                 len = CONTROL_BUFFER_SIZE;
3083                 buf = kmalloc(len, GFP_KERNEL);
3084                 if (!buf)
3085                         goto end;
3086
3087                 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
3088                 kfree(buf);
3089         }
3090
3091 end:
3092         if (update_jiffies >= HZ)
3093                 update_jiffies = round_jiffies_relative(update_jiffies);
3094         else {
3095                 j = round_jiffies_relative(update_jiffies);
3096                 if (abs(j - update_jiffies) <= 10)
3097                         update_jiffies = j;
3098         }
3099
3100         queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3101                                                                 update_jiffies);
3102 }
3103
3104 /*
3105  * driver/device initialization
3106  */
3107 static void rndis_copy_module_params(struct usbnet *usbdev)
3108 {
3109         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3110
3111         priv->param_country[0] = modparam_country[0];
3112         priv->param_country[1] = modparam_country[1];
3113         priv->param_country[2] = 0;
3114         priv->param_frameburst   = modparam_frameburst;
3115         priv->param_afterburner  = modparam_afterburner;
3116         priv->param_power_save   = modparam_power_save;
3117         priv->param_power_output = modparam_power_output;
3118         priv->param_roamtrigger  = modparam_roamtrigger;
3119         priv->param_roamdelta    = modparam_roamdelta;
3120
3121         priv->param_country[0] = toupper(priv->param_country[0]);
3122         priv->param_country[1] = toupper(priv->param_country[1]);
3123         /* doesn't support EU as country code, use FI instead */
3124         if (!strcmp(priv->param_country, "EU"))
3125                 strcpy(priv->param_country, "FI");
3126
3127         if (priv->param_power_save < 0)
3128                 priv->param_power_save = 0;
3129         else if (priv->param_power_save > 2)
3130                 priv->param_power_save = 2;
3131
3132         if (priv->param_power_output < 0)
3133                 priv->param_power_output = 0;
3134         else if (priv->param_power_output > 3)
3135                 priv->param_power_output = 3;
3136
3137         if (priv->param_roamtrigger < -80)
3138                 priv->param_roamtrigger = -80;
3139         else if (priv->param_roamtrigger > -60)
3140                 priv->param_roamtrigger = -60;
3141
3142         if (priv->param_roamdelta < 0)
3143                 priv->param_roamdelta = 0;
3144         else if (priv->param_roamdelta > 2)
3145                 priv->param_roamdelta = 2;
3146
3147         if (modparam_workaround_interval < 0)
3148                 priv->param_workaround_interval = 500;
3149         else
3150                 priv->param_workaround_interval = modparam_workaround_interval;
3151 }
3152
3153 static int bcm4320a_early_init(struct usbnet *usbdev)
3154 {
3155         /* copy module parameters for bcm4320a so that iwconfig reports txpower
3156          * and workaround parameter is copied to private structure correctly.
3157          */
3158         rndis_copy_module_params(usbdev);
3159
3160         /* bcm4320a doesn't handle configuration parameters well. Try
3161          * set any and you get partially zeroed mac and broken device.
3162          */
3163
3164         return 0;
3165 }
3166
3167 static int bcm4320b_early_init(struct usbnet *usbdev)
3168 {
3169         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3170         char buf[8];
3171
3172         rndis_copy_module_params(usbdev);
3173
3174         /* Early initialization settings, setting these won't have effect
3175          * if called after generic_rndis_bind().
3176          */
3177
3178         rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3179         rndis_set_config_parameter_str(usbdev, "FrameBursting",
3180                                         priv->param_frameburst ? "1" : "0");
3181         rndis_set_config_parameter_str(usbdev, "Afterburner",
3182                                         priv->param_afterburner ? "1" : "0");
3183         sprintf(buf, "%d", priv->param_power_save);
3184         rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3185         sprintf(buf, "%d", priv->param_power_output);
3186         rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3187         sprintf(buf, "%d", priv->param_roamtrigger);
3188         rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3189         sprintf(buf, "%d", priv->param_roamdelta);
3190         rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3191
3192         return 0;
3193 }
3194
3195 /* same as rndis_netdev_ops but with local multicast handler */
3196 static const struct net_device_ops rndis_wlan_netdev_ops = {
3197         .ndo_open               = usbnet_open,
3198         .ndo_stop               = usbnet_stop,
3199         .ndo_start_xmit         = usbnet_start_xmit,
3200         .ndo_tx_timeout         = usbnet_tx_timeout,
3201         .ndo_set_mac_address    = eth_mac_addr,
3202         .ndo_validate_addr      = eth_validate_addr,
3203         .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3204 };
3205
3206 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3207 {
3208         struct wiphy *wiphy;
3209         struct rndis_wlan_private *priv;
3210         int retval, len;
3211         __le32 tmp;
3212
3213         /* allocate wiphy and rndis private data
3214          * NOTE: We only support a single virtual interface, so wiphy
3215          * and wireless_dev are somewhat synonymous for this device.
3216          */
3217         wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3218         if (!wiphy)
3219                 return -ENOMEM;
3220
3221         priv = wiphy_priv(wiphy);
3222         usbdev->net->ieee80211_ptr = &priv->wdev;
3223         priv->wdev.wiphy = wiphy;
3224         priv->wdev.iftype = NL80211_IFTYPE_STATION;
3225
3226         /* These have to be initialized before calling generic_rndis_bind().
3227          * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3228          */
3229         usbdev->driver_priv = priv;
3230         priv->usbdev = usbdev;
3231
3232         mutex_init(&priv->command_lock);
3233
3234         /* because rndis_command() sleeps we need to use workqueue */
3235         priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3236         INIT_WORK(&priv->work, rndis_wlan_worker);
3237         INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3238         INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3239
3240         /* try bind rndis_host */
3241         retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3242         if (retval < 0)
3243                 goto fail;
3244
3245         /* generic_rndis_bind set packet filter to multicast_all+
3246          * promisc mode which doesn't work well for our devices (device
3247          * picks up rssi to closest station instead of to access point).
3248          *
3249          * rndis_host wants to avoid all OID as much as possible
3250          * so do promisc/multicast handling in rndis_wlan.
3251          */
3252         usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3253
3254         tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3255         retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3256                                                                 sizeof(tmp));
3257
3258         len = sizeof(tmp);
3259         retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3260                                                                 &len);
3261         priv->multicast_size = le32_to_cpu(tmp);
3262         if (retval < 0 || priv->multicast_size < 0)
3263                 priv->multicast_size = 0;
3264         if (priv->multicast_size > 0)
3265                 usbdev->net->flags |= IFF_MULTICAST;
3266         else
3267                 usbdev->net->flags &= ~IFF_MULTICAST;
3268
3269         /* fill-out wiphy structure and register w/ cfg80211 */
3270         memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3271         wiphy->privid = rndis_wiphy_privid;
3272         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3273                                         | BIT(NL80211_IFTYPE_ADHOC);
3274         wiphy->max_scan_ssids = 1;
3275
3276         /* TODO: fill-out band/encr information based on priv->caps */
3277         rndis_wlan_get_caps(usbdev, wiphy);
3278
3279         memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3280         memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3281         priv->band.channels = priv->channels;
3282         priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3283         priv->band.bitrates = priv->rates;
3284         priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3285         wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3286         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3287
3288         memcpy(priv->cipher_suites, rndis_cipher_suites,
3289                                                 sizeof(rndis_cipher_suites));
3290         wiphy->cipher_suites = priv->cipher_suites;
3291         wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3292
3293         set_wiphy_dev(wiphy, &usbdev->udev->dev);
3294
3295         if (wiphy_register(wiphy)) {
3296                 retval = -ENODEV;
3297                 goto fail;
3298         }
3299
3300         set_default_iw_params(usbdev);
3301
3302         /* set default rts/frag */
3303         rndis_set_wiphy_params(wiphy,
3304                         WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3305
3306         /* turn radio on */
3307         priv->radio_on = true;
3308         disassociate(usbdev, true);
3309         netif_carrier_off(usbdev->net);
3310
3311         return 0;
3312
3313 fail:
3314         cancel_delayed_work_sync(&priv->dev_poller_work);
3315         cancel_delayed_work_sync(&priv->scan_work);
3316         cancel_work_sync(&priv->work);
3317         flush_workqueue(priv->workqueue);
3318         destroy_workqueue(priv->workqueue);
3319
3320         wiphy_free(wiphy);
3321         return retval;
3322 }
3323
3324 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3325 {
3326         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3327
3328         /* turn radio off */
3329         disassociate(usbdev, false);
3330
3331         cancel_delayed_work_sync(&priv->dev_poller_work);
3332         cancel_delayed_work_sync(&priv->scan_work);
3333         cancel_work_sync(&priv->work);
3334         flush_workqueue(priv->workqueue);
3335         destroy_workqueue(priv->workqueue);
3336
3337         rndis_unbind(usbdev, intf);
3338
3339         wiphy_unregister(priv->wdev.wiphy);
3340         wiphy_free(priv->wdev.wiphy);
3341 }
3342
3343 static int rndis_wlan_reset(struct usbnet *usbdev)
3344 {
3345         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3346         int retval;
3347
3348         netdev_dbg(usbdev->net, "%s()\n", __func__);
3349
3350         retval = rndis_reset(usbdev);
3351         if (retval)
3352                 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3353
3354         /* rndis_reset cleared multicast list, so restore here.
3355            (set_multicast_list() also turns on current packet filter) */
3356         set_multicast_list(usbdev);
3357
3358         queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3359                 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3360
3361         return deauthenticate(usbdev);
3362 }
3363
3364 static int rndis_wlan_stop(struct usbnet *usbdev)
3365 {
3366         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3367         int retval;
3368         __le32 filter;
3369
3370         netdev_dbg(usbdev->net, "%s()\n", __func__);
3371
3372         retval = disassociate(usbdev, false);
3373
3374         priv->work_pending = 0;
3375         cancel_delayed_work_sync(&priv->dev_poller_work);
3376         cancel_delayed_work_sync(&priv->scan_work);
3377         cancel_work_sync(&priv->work);
3378         flush_workqueue(priv->workqueue);
3379
3380         if (priv->scan_request) {
3381                 cfg80211_scan_done(priv->scan_request, true);
3382                 priv->scan_request = NULL;
3383         }
3384
3385         /* Set current packet filter zero to block receiving data packets from
3386            device. */
3387         filter = 0;
3388         rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3389                                                                 sizeof(filter));
3390
3391         return retval;
3392 }
3393
3394 static const struct driver_info bcm4320b_info = {
3395         .description =  "Wireless RNDIS device, BCM4320b based",
3396         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3397                                 FLAG_AVOID_UNLINK_URBS,
3398         .bind =         rndis_wlan_bind,
3399         .unbind =       rndis_wlan_unbind,
3400         .status =       rndis_status,
3401         .rx_fixup =     rndis_rx_fixup,
3402         .tx_fixup =     rndis_tx_fixup,
3403         .reset =        rndis_wlan_reset,
3404         .stop =         rndis_wlan_stop,
3405         .early_init =   bcm4320b_early_init,
3406         .indication =   rndis_wlan_indication,
3407 };
3408
3409 static const struct driver_info bcm4320a_info = {
3410         .description =  "Wireless RNDIS device, BCM4320a based",
3411         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3412                                 FLAG_AVOID_UNLINK_URBS,
3413         .bind =         rndis_wlan_bind,
3414         .unbind =       rndis_wlan_unbind,
3415         .status =       rndis_status,
3416         .rx_fixup =     rndis_rx_fixup,
3417         .tx_fixup =     rndis_tx_fixup,
3418         .reset =        rndis_wlan_reset,
3419         .stop =         rndis_wlan_stop,
3420         .early_init =   bcm4320a_early_init,
3421         .indication =   rndis_wlan_indication,
3422 };
3423
3424 static const struct driver_info rndis_wlan_info = {
3425         .description =  "Wireless RNDIS device",
3426         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3427                                 FLAG_AVOID_UNLINK_URBS,
3428         .bind =         rndis_wlan_bind,
3429         .unbind =       rndis_wlan_unbind,
3430         .status =       rndis_status,
3431         .rx_fixup =     rndis_rx_fixup,
3432         .tx_fixup =     rndis_tx_fixup,
3433         .reset =        rndis_wlan_reset,
3434         .stop =         rndis_wlan_stop,
3435         .early_init =   bcm4320a_early_init,
3436         .indication =   rndis_wlan_indication,
3437 };
3438
3439 /*-------------------------------------------------------------------------*/
3440
3441 static const struct usb_device_id products [] = {
3442 #define RNDIS_MASTER_INTERFACE \
3443         .bInterfaceClass        = USB_CLASS_COMM, \
3444         .bInterfaceSubClass     = 2 /* ACM */, \
3445         .bInterfaceProtocol     = 0x0ff
3446
3447 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3448  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3449  */
3450 {
3451         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3452                           | USB_DEVICE_ID_MATCH_DEVICE,
3453         .idVendor               = 0x0411,
3454         .idProduct              = 0x00bc,       /* Buffalo WLI-U2-KG125S */
3455         RNDIS_MASTER_INTERFACE,
3456         .driver_info            = (unsigned long) &bcm4320b_info,
3457 }, {
3458         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3459                           | USB_DEVICE_ID_MATCH_DEVICE,
3460         .idVendor               = 0x0baf,
3461         .idProduct              = 0x011b,       /* U.S. Robotics USR5421 */
3462         RNDIS_MASTER_INTERFACE,
3463         .driver_info            = (unsigned long) &bcm4320b_info,
3464 }, {
3465         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3466                           | USB_DEVICE_ID_MATCH_DEVICE,
3467         .idVendor               = 0x050d,
3468         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3469         RNDIS_MASTER_INTERFACE,
3470         .driver_info            = (unsigned long) &bcm4320b_info,
3471 }, {
3472         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3473                           | USB_DEVICE_ID_MATCH_DEVICE,
3474         .idVendor               = 0x1799,       /* Belkin has two vendor ids */
3475         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3476         RNDIS_MASTER_INTERFACE,
3477         .driver_info            = (unsigned long) &bcm4320b_info,
3478 }, {
3479         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3480                           | USB_DEVICE_ID_MATCH_DEVICE,
3481         .idVendor               = 0x13b1,
3482         .idProduct              = 0x0014,       /* Linksys WUSB54GSv2 */
3483         RNDIS_MASTER_INTERFACE,
3484         .driver_info            = (unsigned long) &bcm4320b_info,
3485 }, {
3486         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3487                           | USB_DEVICE_ID_MATCH_DEVICE,
3488         .idVendor               = 0x13b1,
3489         .idProduct              = 0x0026,       /* Linksys WUSB54GSC */
3490         RNDIS_MASTER_INTERFACE,
3491         .driver_info            = (unsigned long) &bcm4320b_info,
3492 }, {
3493         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3494                           | USB_DEVICE_ID_MATCH_DEVICE,
3495         .idVendor               = 0x0b05,
3496         .idProduct              = 0x1717,       /* Asus WL169gE */
3497         RNDIS_MASTER_INTERFACE,
3498         .driver_info            = (unsigned long) &bcm4320b_info,
3499 }, {
3500         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3501                           | USB_DEVICE_ID_MATCH_DEVICE,
3502         .idVendor               = 0x0a5c,
3503         .idProduct              = 0xd11b,       /* Eminent EM4045 */
3504         RNDIS_MASTER_INTERFACE,
3505         .driver_info            = (unsigned long) &bcm4320b_info,
3506 }, {
3507         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3508                           | USB_DEVICE_ID_MATCH_DEVICE,
3509         .idVendor               = 0x1690,
3510         .idProduct              = 0x0715,       /* BT Voyager 1055 */
3511         RNDIS_MASTER_INTERFACE,
3512         .driver_info            = (unsigned long) &bcm4320b_info,
3513 },
3514 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3515  * parameters available, hardware probably contain older firmware version with
3516  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3517  */
3518 {
3519         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3520                           | USB_DEVICE_ID_MATCH_DEVICE,
3521         .idVendor               = 0x13b1,
3522         .idProduct              = 0x000e,       /* Linksys WUSB54GSv1 */
3523         RNDIS_MASTER_INTERFACE,
3524         .driver_info            = (unsigned long) &bcm4320a_info,
3525 }, {
3526         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3527                           | USB_DEVICE_ID_MATCH_DEVICE,
3528         .idVendor               = 0x0baf,
3529         .idProduct              = 0x0111,       /* U.S. Robotics USR5420 */
3530         RNDIS_MASTER_INTERFACE,
3531         .driver_info            = (unsigned long) &bcm4320a_info,
3532 }, {
3533         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3534                           | USB_DEVICE_ID_MATCH_DEVICE,
3535         .idVendor               = 0x0411,
3536         .idProduct              = 0x004b,       /* BUFFALO WLI-USB-G54 */
3537         RNDIS_MASTER_INTERFACE,
3538         .driver_info            = (unsigned long) &bcm4320a_info,
3539 },
3540 /* Generic Wireless RNDIS devices that we don't have exact
3541  * idVendor/idProduct/chip yet.
3542  */
3543 {
3544         /* RNDIS is MSFT's un-official variant of CDC ACM */
3545         USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3546         .driver_info = (unsigned long) &rndis_wlan_info,
3547 }, {
3548         /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3549         USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3550         .driver_info = (unsigned long) &rndis_wlan_info,
3551 },
3552         { },            // END
3553 };
3554 MODULE_DEVICE_TABLE(usb, products);
3555
3556 static struct usb_driver rndis_wlan_driver = {
3557         .name =         "rndis_wlan",
3558         .id_table =     products,
3559         .probe =        usbnet_probe,
3560         .disconnect =   usbnet_disconnect,
3561         .suspend =      usbnet_suspend,
3562         .resume =       usbnet_resume,
3563 };
3564
3565 static int __init rndis_wlan_init(void)
3566 {
3567         return usb_register(&rndis_wlan_driver);
3568 }
3569 module_init(rndis_wlan_init);
3570
3571 static void __exit rndis_wlan_exit(void)
3572 {
3573         usb_deregister(&rndis_wlan_driver);
3574 }
3575 module_exit(rndis_wlan_exit);
3576
3577 MODULE_AUTHOR("Bjorge Dijkstra");
3578 MODULE_AUTHOR("Jussi Kivilinna");
3579 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3580 MODULE_LICENSE("GPL");
3581