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