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