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