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