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