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