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