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