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