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