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