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