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