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