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