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