]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/rndis_wlan.c
rndis_wlan: clear cfg80211 scan on rndis_wlan_stop()
[~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         if (!priv->scan_request)
1488                 return;
1489
1490         ret = rndis_check_bssid_list(usbdev);
1491
1492         cfg80211_scan_done(priv->scan_request, ret < 0);
1493
1494         priv->scan_request = NULL;
1495 }
1496
1497
1498 /*
1499  * wireless extension handlers
1500  */
1501
1502 static int rndis_iw_commit(struct net_device *dev,
1503     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1504 {
1505         /* dummy op */
1506         return 0;
1507 }
1508
1509
1510 static int rndis_iw_set_essid(struct net_device *dev,
1511     struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1512 {
1513         struct ndis_80211_ssid ssid;
1514         int length = wrqu->essid.length;
1515         struct usbnet *usbdev = netdev_priv(dev);
1516
1517         devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1518                 wrqu->essid.flags, wrqu->essid.length, essid);
1519
1520         if (length > NDIS_802_11_LENGTH_SSID)
1521                 length = NDIS_802_11_LENGTH_SSID;
1522
1523         ssid.length = cpu_to_le32(length);
1524         if (length > 0)
1525                 memcpy(ssid.essid, essid, length);
1526         else
1527                 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
1528
1529         set_assoc_params(usbdev);
1530
1531         if (!wrqu->essid.flags || length == 0)
1532                 return disassociate(usbdev, 1);
1533         else
1534                 return set_essid(usbdev, &ssid);
1535 }
1536
1537
1538 static int rndis_iw_get_essid(struct net_device *dev,
1539     struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1540 {
1541         struct ndis_80211_ssid ssid;
1542         struct usbnet *usbdev = netdev_priv(dev);
1543         int ret;
1544
1545         ret = get_essid(usbdev, &ssid);
1546
1547         if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
1548                 wrqu->essid.flags = 1;
1549                 wrqu->essid.length = le32_to_cpu(ssid.length);
1550                 memcpy(essid, ssid.essid, wrqu->essid.length);
1551                 essid[wrqu->essid.length] = 0;
1552         } else {
1553                 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1554                 wrqu->essid.flags = 0;
1555                 wrqu->essid.length = 0;
1556         }
1557         devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1558         return ret;
1559 }
1560
1561
1562 static int rndis_iw_get_bssid(struct net_device *dev,
1563     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1564 {
1565         struct usbnet *usbdev = netdev_priv(dev);
1566         unsigned char bssid[ETH_ALEN];
1567         int ret;
1568
1569         ret = get_bssid(usbdev, bssid);
1570
1571         if (ret == 0)
1572                 devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
1573         else
1574                 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1575
1576         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1577         memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1578
1579         return ret;
1580 }
1581
1582
1583 static int rndis_iw_set_bssid(struct net_device *dev,
1584     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1585 {
1586         struct usbnet *usbdev = netdev_priv(dev);
1587         u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1588         int ret;
1589
1590         devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
1591
1592         ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1593
1594         /* user apps may set ap's mac address, which is not required;
1595          * they may fail to work if this function fails, so return
1596          * success */
1597         if (ret)
1598                 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1599
1600         return 0;
1601 }
1602
1603
1604 static int rndis_iw_set_auth(struct net_device *dev,
1605     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1606 {
1607         struct iw_param *p = &wrqu->param;
1608         struct usbnet *usbdev = netdev_priv(dev);
1609         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1610         int ret = -ENOTSUPP;
1611
1612         switch (p->flags & IW_AUTH_INDEX) {
1613         case IW_AUTH_WPA_VERSION:
1614                 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1615                 priv->wpa_version = p->value;
1616                 ret = 0;
1617                 break;
1618
1619         case IW_AUTH_CIPHER_PAIRWISE:
1620                 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1621                 priv->wpa_cipher_pair = p->value;
1622                 ret = 0;
1623                 break;
1624
1625         case IW_AUTH_CIPHER_GROUP:
1626                 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1627                 priv->wpa_cipher_group = p->value;
1628                 ret = 0;
1629                 break;
1630
1631         case IW_AUTH_KEY_MGMT:
1632                 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1633                 priv->wpa_keymgmt = p->value;
1634                 ret = 0;
1635                 break;
1636
1637         case IW_AUTH_TKIP_COUNTERMEASURES:
1638                 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1639                                                                 p->value);
1640                 ret = 0;
1641                 break;
1642
1643         case IW_AUTH_DROP_UNENCRYPTED:
1644                 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1645                 ret = 0;
1646                 break;
1647
1648         case IW_AUTH_80211_AUTH_ALG:
1649                 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1650                 priv->wpa_authalg = p->value;
1651                 ret = 0;
1652                 break;
1653
1654         case IW_AUTH_WPA_ENABLED:
1655                 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1656                 if (wrqu->param.value)
1657                         deauthenticate(usbdev);
1658                 ret = 0;
1659                 break;
1660
1661         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1662                 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1663                                                                 p->value);
1664                 ret = 0;
1665                 break;
1666
1667         case IW_AUTH_ROAMING_CONTROL:
1668                 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1669                 ret = 0;
1670                 break;
1671
1672         case IW_AUTH_PRIVACY_INVOKED:
1673                 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1674                                 wrqu->param.flags & IW_AUTH_INDEX);
1675                 return -EOPNOTSUPP;
1676
1677         default:
1678                 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN  %08x, %08x",
1679                         p->flags & IW_AUTH_INDEX, p->value);
1680         }
1681         return ret;
1682 }
1683
1684
1685 static int rndis_iw_get_auth(struct net_device *dev,
1686     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1687 {
1688         struct iw_param *p = &wrqu->param;
1689         struct usbnet *usbdev = netdev_priv(dev);
1690         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1691
1692         switch (p->flags & IW_AUTH_INDEX) {
1693         case IW_AUTH_WPA_VERSION:
1694                 p->value = priv->wpa_version;
1695                 break;
1696         case IW_AUTH_CIPHER_PAIRWISE:
1697                 p->value = priv->wpa_cipher_pair;
1698                 break;
1699         case IW_AUTH_CIPHER_GROUP:
1700                 p->value = priv->wpa_cipher_group;
1701                 break;
1702         case IW_AUTH_KEY_MGMT:
1703                 p->value = priv->wpa_keymgmt;
1704                 break;
1705         case IW_AUTH_80211_AUTH_ALG:
1706                 p->value = priv->wpa_authalg;
1707                 break;
1708         default:
1709                 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1710                                 wrqu->param.flags & IW_AUTH_INDEX);
1711                 return -EOPNOTSUPP;
1712         }
1713         return 0;
1714 }
1715
1716
1717 static int rndis_iw_set_encode(struct net_device *dev,
1718     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1719 {
1720         struct usbnet *usbdev = netdev_priv(dev);
1721         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1722         int ret, index, key_len;
1723         u8 *key;
1724
1725         index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1726
1727         /* iwconfig gives index as 1 - N */
1728         if (index > 0)
1729                 index--;
1730         else
1731                 index = priv->encr_tx_key_index;
1732
1733         if (index < 0 || index >= 4) {
1734                 devwarn(usbdev, "encryption index out of range (%u)", index);
1735                 return -EINVAL;
1736         }
1737
1738         /* remove key if disabled */
1739         if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1740                 if (remove_key(usbdev, index, NULL))
1741                         return -EINVAL;
1742                 else
1743                         return 0;
1744         }
1745
1746         /* global encryption state (for all keys) */
1747         if (wrqu->data.flags & IW_ENCODE_OPEN)
1748                 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1749                                                 IW_AUTH_ALG_OPEN_SYSTEM);
1750         else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1751                 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1752                                                 IW_AUTH_ALG_SHARED_KEY);
1753         if (ret != 0)
1754                 return ret;
1755
1756         if (wrqu->data.length > 0) {
1757                 key_len = wrqu->data.length;
1758                 key = extra;
1759         } else {
1760                 /* must be set as tx key */
1761                 if (priv->encr_key_len[index] == 0)
1762                         return -EINVAL;
1763                 key_len = priv->encr_key_len[index];
1764                 key = priv->encr_keys[index];
1765                 priv->encr_tx_key_index = index;
1766         }
1767
1768         if (add_wep_key(usbdev, key, key_len, index) != 0)
1769                 return -EINVAL;
1770
1771         if (index == priv->encr_tx_key_index)
1772                 /* ndis drivers want essid to be set after setting encr */
1773                 set_essid(usbdev, &priv->essid);
1774
1775         return 0;
1776 }
1777
1778
1779 static int rndis_iw_set_encode_ext(struct net_device *dev,
1780     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1781 {
1782         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1783         struct usbnet *usbdev = netdev_priv(dev);
1784         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1785         int keyidx, flags;
1786
1787         keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1788
1789         /* iwconfig gives index as 1 - N */
1790         if (keyidx)
1791                 keyidx--;
1792         else
1793                 keyidx = priv->encr_tx_key_index;
1794
1795         if (keyidx < 0 || keyidx >= 4)
1796                 return -EINVAL;
1797
1798         if (ext->alg == WPA_ALG_WEP) {
1799                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1800                         priv->encr_tx_key_index = keyidx;
1801                 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1802         }
1803
1804         if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1805             ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1806                 return remove_key(usbdev, keyidx, NULL);
1807
1808         flags = 0;
1809         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
1810                 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
1811         if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
1812                 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
1813         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1814                 flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
1815
1816         return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx, &ext->addr,
1817                                 ext->rx_seq, ext->alg, flags);
1818 }
1819
1820
1821 static int rndis_iw_set_genie(struct net_device *dev,
1822     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1823 {
1824         struct usbnet *usbdev = netdev_priv(dev);
1825         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1826         int ret = 0;
1827
1828 #ifdef DEBUG
1829         int j;
1830         u8 *gie = extra;
1831         for (j = 0; j < wrqu->data.length; j += 8)
1832                 devdbg(usbdev,
1833                         "SIOCSIWGENIE %04x - "
1834                         "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1835                         gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1836                         gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1837 #endif
1838         /* clear existing IEs */
1839         if (priv->wpa_ie_len) {
1840                 kfree(priv->wpa_ie);
1841                 priv->wpa_ie_len = 0;
1842         }
1843
1844         /* set new IEs */
1845         priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1846         if (priv->wpa_ie) {
1847                 priv->wpa_ie_len = wrqu->data.length;
1848                 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1849         } else
1850                 ret = -ENOMEM;
1851         return ret;
1852 }
1853
1854
1855 static int rndis_iw_get_genie(struct net_device *dev,
1856     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1857 {
1858         struct usbnet *usbdev = netdev_priv(dev);
1859         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1860
1861         devdbg(usbdev, "SIOCGIWGENIE");
1862
1863         if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1864                 wrqu->data.length = 0;
1865                 return 0;
1866         }
1867
1868         if (wrqu->data.length < priv->wpa_ie_len)
1869                 return -E2BIG;
1870
1871         wrqu->data.length = priv->wpa_ie_len;
1872         memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1873
1874         return 0;
1875 }
1876
1877
1878 static int rndis_iw_set_freq(struct net_device *dev,
1879     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1880 {
1881         struct usbnet *usbdev = netdev_priv(dev);
1882         struct ndis_80211_conf config;
1883         unsigned int dsconfig;
1884         int len, ret;
1885
1886         /* this OID is valid only when not associated */
1887         if (is_associated(usbdev))
1888                 return 0;
1889
1890         dsconfig = 0;
1891         if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1892                 return -EINVAL;
1893
1894         len = sizeof(config);
1895         ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1896         if (ret != 0) {
1897                 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1898                 return 0;
1899         }
1900
1901         config.ds_config = cpu_to_le32(dsconfig);
1902
1903         devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1904         return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1905                                                                 sizeof(config));
1906 }
1907
1908
1909 static int rndis_iw_get_freq(struct net_device *dev,
1910     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1911 {
1912         struct usbnet *usbdev = netdev_priv(dev);
1913         struct ndis_80211_conf config;
1914         int len, ret;
1915
1916         len = sizeof(config);
1917         ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1918         if (ret == 0)
1919                 dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
1920
1921         devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
1922         return ret;
1923 }
1924
1925
1926 static int rndis_iw_get_rate(struct net_device *dev,
1927     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1928 {
1929         struct usbnet *usbdev = netdev_priv(dev);
1930         __le32 tmp;
1931         int ret, len;
1932
1933         len = sizeof(tmp);
1934         ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
1935         if (ret == 0) {
1936                 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
1937                 wrqu->bitrate.disabled = 0;
1938                 wrqu->bitrate.flags = 1;
1939         }
1940         return ret;
1941 }
1942
1943
1944 static int rndis_iw_set_mlme(struct net_device *dev,
1945     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1946 {
1947         struct usbnet *usbdev = netdev_priv(dev);
1948         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1949         struct iw_mlme *mlme = (struct iw_mlme *)extra;
1950         unsigned char bssid[ETH_ALEN];
1951
1952         get_bssid(usbdev, bssid);
1953
1954         if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
1955                 return -EINVAL;
1956
1957         switch (mlme->cmd) {
1958         case IW_MLME_DEAUTH:
1959                 return deauthenticate(usbdev);
1960         case IW_MLME_DISASSOC:
1961                 return disassociate(usbdev, priv->radio_on);
1962         default:
1963                 return -EOPNOTSUPP;
1964         }
1965
1966         return 0;
1967 }
1968
1969
1970 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
1971 {
1972         struct usbnet *usbdev = netdev_priv(dev);
1973         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1974         unsigned long flags;
1975
1976         spin_lock_irqsave(&priv->stats_lock, flags);
1977         memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
1978         spin_unlock_irqrestore(&priv->stats_lock, flags);
1979
1980         return &priv->iwstats;
1981 }
1982
1983
1984 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
1985 static const iw_handler rndis_iw_handler[] =
1986 {
1987         IW_IOCTL(SIOCSIWCOMMIT)    = rndis_iw_commit,
1988         IW_IOCTL(SIOCGIWNAME)      = (iw_handler) cfg80211_wext_giwname,
1989         IW_IOCTL(SIOCSIWFREQ)      = rndis_iw_set_freq,
1990         IW_IOCTL(SIOCGIWFREQ)      = rndis_iw_get_freq,
1991         IW_IOCTL(SIOCSIWMODE)      = (iw_handler) cfg80211_wext_siwmode,
1992         IW_IOCTL(SIOCGIWMODE)      = (iw_handler) cfg80211_wext_giwmode,
1993         IW_IOCTL(SIOCGIWRANGE)     = (iw_handler) cfg80211_wext_giwrange,
1994         IW_IOCTL(SIOCSIWAP)        = rndis_iw_set_bssid,
1995         IW_IOCTL(SIOCGIWAP)        = rndis_iw_get_bssid,
1996         IW_IOCTL(SIOCSIWSCAN)      = (iw_handler) cfg80211_wext_siwscan,
1997         IW_IOCTL(SIOCGIWSCAN)      = (iw_handler) cfg80211_wext_giwscan,
1998         IW_IOCTL(SIOCSIWESSID)     = rndis_iw_set_essid,
1999         IW_IOCTL(SIOCGIWESSID)     = rndis_iw_get_essid,
2000         IW_IOCTL(SIOCGIWRATE)      = rndis_iw_get_rate,
2001         IW_IOCTL(SIOCSIWRTS)       = (iw_handler) cfg80211_wext_siwrts,
2002         IW_IOCTL(SIOCGIWRTS)       = (iw_handler) cfg80211_wext_giwrts,
2003         IW_IOCTL(SIOCSIWFRAG)      = (iw_handler) cfg80211_wext_siwfrag,
2004         IW_IOCTL(SIOCGIWFRAG)      = (iw_handler) cfg80211_wext_giwfrag,
2005         IW_IOCTL(SIOCSIWTXPOW)     = (iw_handler) cfg80211_wext_siwtxpower,
2006         IW_IOCTL(SIOCGIWTXPOW)     = (iw_handler) cfg80211_wext_giwtxpower,
2007         IW_IOCTL(SIOCSIWENCODE)    = rndis_iw_set_encode,
2008         IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2009         IW_IOCTL(SIOCSIWAUTH)      = rndis_iw_set_auth,
2010         IW_IOCTL(SIOCGIWAUTH)      = rndis_iw_get_auth,
2011         IW_IOCTL(SIOCSIWGENIE)     = rndis_iw_set_genie,
2012         IW_IOCTL(SIOCGIWGENIE)     = rndis_iw_get_genie,
2013         IW_IOCTL(SIOCSIWMLME)      = rndis_iw_set_mlme,
2014 };
2015
2016 static const iw_handler rndis_wlan_private_handler[] = {
2017 };
2018
2019 static const struct iw_priv_args rndis_wlan_private_args[] = {
2020 };
2021
2022
2023 static const struct iw_handler_def rndis_iw_handlers = {
2024         .num_standard = ARRAY_SIZE(rndis_iw_handler),
2025         .num_private  = ARRAY_SIZE(rndis_wlan_private_handler),
2026         .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
2027         .standard = (iw_handler *)rndis_iw_handler,
2028         .private  = (iw_handler *)rndis_wlan_private_handler,
2029         .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
2030         .get_wireless_stats = rndis_get_wireless_stats,
2031 };
2032
2033
2034 static void rndis_wlan_worker(struct work_struct *work)
2035 {
2036         struct rndis_wlan_private *priv =
2037                 container_of(work, struct rndis_wlan_private, work);
2038         struct usbnet *usbdev = priv->usbdev;
2039         union iwreq_data evt;
2040         unsigned char bssid[ETH_ALEN];
2041         struct ndis_80211_assoc_info *info;
2042         int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
2043         int ret, offset;
2044
2045         if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
2046                 netif_carrier_on(usbdev->net);
2047
2048                 info = kzalloc(assoc_size, GFP_KERNEL);
2049                 if (!info)
2050                         goto get_bssid;
2051
2052                 /* Get association info IEs from device and send them back to
2053                  * userspace. */
2054                 ret = get_association_info(usbdev, info, assoc_size);
2055                 if (!ret) {
2056                         evt.data.length = le32_to_cpu(info->req_ie_length);
2057                         if (evt.data.length > 0) {
2058                                 offset = le32_to_cpu(info->offset_req_ies);
2059                                 wireless_send_event(usbdev->net,
2060                                         IWEVASSOCREQIE, &evt,
2061                                         (char *)info + offset);
2062                         }
2063
2064                         evt.data.length = le32_to_cpu(info->resp_ie_length);
2065                         if (evt.data.length > 0) {
2066                                 offset = le32_to_cpu(info->offset_resp_ies);
2067                                 wireless_send_event(usbdev->net,
2068                                         IWEVASSOCRESPIE, &evt,
2069                                         (char *)info + offset);
2070                         }
2071                 }
2072
2073                 kfree(info);
2074
2075 get_bssid:
2076                 ret = get_bssid(usbdev, bssid);
2077                 if (!ret) {
2078                         evt.data.flags = 0;
2079                         evt.data.length = 0;
2080                         memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2081                         wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2082                 }
2083         }
2084
2085         if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
2086                 netif_carrier_off(usbdev->net);
2087
2088                 evt.data.flags = 0;
2089                 evt.data.length = 0;
2090                 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2091                 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2092         }
2093
2094         if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2095                 set_multicast_list(usbdev);
2096 }
2097
2098 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2099 {
2100         struct usbnet *usbdev = netdev_priv(dev);
2101         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2102
2103         if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2104                 return;
2105
2106         set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2107         queue_work(priv->workqueue, &priv->work);
2108 }
2109
2110 static void rndis_wlan_link_change(struct usbnet *usbdev, int state)
2111 {
2112         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2113
2114         /* queue work to avoid recursive calls into rndis_command */
2115         set_bit(state ? WORK_LINK_UP : WORK_LINK_DOWN, &priv->work_pending);
2116         queue_work(priv->workqueue, &priv->work);
2117 }
2118
2119
2120 static int rndis_wlan_get_caps(struct usbnet *usbdev)
2121 {
2122         struct {
2123                 __le32  num_items;
2124                 __le32  items[8];
2125         } networks_supported;
2126         int len, retval, i, n;
2127         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2128
2129         /* determine supported modes */
2130         len = sizeof(networks_supported);
2131         retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2132                                                 &networks_supported, &len);
2133         if (retval >= 0) {
2134                 n = le32_to_cpu(networks_supported.num_items);
2135                 if (n > 8)
2136                         n = 8;
2137                 for (i = 0; i < n; i++) {
2138                         switch (le32_to_cpu(networks_supported.items[i])) {
2139                         case NDIS_80211_TYPE_FREQ_HOP:
2140                         case NDIS_80211_TYPE_DIRECT_SEQ:
2141                                 priv->caps |= CAP_MODE_80211B;
2142                                 break;
2143                         case NDIS_80211_TYPE_OFDM_A:
2144                                 priv->caps |= CAP_MODE_80211A;
2145                                 break;
2146                         case NDIS_80211_TYPE_OFDM_G:
2147                                 priv->caps |= CAP_MODE_80211G;
2148                                 break;
2149                         }
2150                 }
2151         }
2152
2153         return retval;
2154 }
2155
2156
2157 #define STATS_UPDATE_JIFFIES (HZ)
2158 static void rndis_update_wireless_stats(struct work_struct *work)
2159 {
2160         struct rndis_wlan_private *priv =
2161                 container_of(work, struct rndis_wlan_private, stats_work.work);
2162         struct usbnet *usbdev = priv->usbdev;
2163         struct iw_statistics iwstats;
2164         __le32 rssi, tmp;
2165         int len, ret, j;
2166         unsigned long flags;
2167         int update_jiffies = STATS_UPDATE_JIFFIES;
2168         void *buf;
2169
2170         spin_lock_irqsave(&priv->stats_lock, flags);
2171         memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2172         spin_unlock_irqrestore(&priv->stats_lock, flags);
2173
2174         /* only update stats when connected */
2175         if (!is_associated(usbdev)) {
2176                 iwstats.qual.qual = 0;
2177                 iwstats.qual.level = 0;
2178                 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2179                                 | IW_QUAL_LEVEL_UPDATED
2180                                 | IW_QUAL_NOISE_INVALID
2181                                 | IW_QUAL_QUAL_INVALID
2182                                 | IW_QUAL_LEVEL_INVALID;
2183                 goto end;
2184         }
2185
2186         len = sizeof(rssi);
2187         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2188
2189         devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2190                                                         le32_to_cpu(rssi));
2191         if (ret == 0) {
2192                 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2193                 iwstats.qual.qual  = level_to_qual(le32_to_cpu(rssi));
2194                 iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
2195                 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2196                                 | IW_QUAL_LEVEL_UPDATED
2197                                 | IW_QUAL_NOISE_INVALID;
2198         }
2199
2200         memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2201
2202         len = sizeof(tmp);
2203         ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2204         if (ret == 0)
2205                 iwstats.discard.misc += le32_to_cpu(tmp);
2206
2207         len = sizeof(tmp);
2208         ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2209         if (ret == 0)
2210                 iwstats.discard.misc += le32_to_cpu(tmp);
2211
2212         len = sizeof(tmp);
2213         ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2214         if (ret == 0)
2215                 iwstats.discard.misc += le32_to_cpu(tmp);
2216
2217         /* Workaround transfer stalls on poor quality links.
2218          * TODO: find right way to fix these stalls (as stalls do not happen
2219          * with ndiswrapper/windows driver). */
2220         if (iwstats.qual.qual <= 25) {
2221                 /* Decrease stats worker interval to catch stalls.
2222                  * faster. Faster than 400-500ms causes packet loss,
2223                  * Slower doesn't catch stalls fast enough.
2224                  */
2225                 j = msecs_to_jiffies(priv->param_workaround_interval);
2226                 if (j > STATS_UPDATE_JIFFIES)
2227                         j = STATS_UPDATE_JIFFIES;
2228                 else if (j <= 0)
2229                         j = 1;
2230                 update_jiffies = j;
2231
2232                 /* Send scan OID. Use of both OIDs is required to get device
2233                  * working.
2234                  */
2235                 tmp = cpu_to_le32(1);
2236                 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2237                                                                 sizeof(tmp));
2238
2239                 len = CONTROL_BUFFER_SIZE;
2240                 buf = kmalloc(len, GFP_KERNEL);
2241                 if (!buf)
2242                         goto end;
2243
2244                 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2245                 kfree(buf);
2246         }
2247 end:
2248         spin_lock_irqsave(&priv->stats_lock, flags);
2249         memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2250         spin_unlock_irqrestore(&priv->stats_lock, flags);
2251
2252         if (update_jiffies >= HZ)
2253                 update_jiffies = round_jiffies_relative(update_jiffies);
2254         else {
2255                 j = round_jiffies_relative(update_jiffies);
2256                 if (abs(j - update_jiffies) <= 10)
2257                         update_jiffies = j;
2258         }
2259
2260         queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2261 }
2262
2263
2264 static int bcm4320a_early_init(struct usbnet *usbdev)
2265 {
2266         /* bcm4320a doesn't handle configuration parameters well. Try
2267          * set any and you get partially zeroed mac and broken device.
2268          */
2269
2270         return 0;
2271 }
2272
2273
2274 static int bcm4320b_early_init(struct usbnet *usbdev)
2275 {
2276         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2277         char buf[8];
2278
2279         /* Early initialization settings, setting these won't have effect
2280          * if called after generic_rndis_bind().
2281          */
2282
2283         priv->param_country[0] = modparam_country[0];
2284         priv->param_country[1] = modparam_country[1];
2285         priv->param_country[2] = 0;
2286         priv->param_frameburst   = modparam_frameburst;
2287         priv->param_afterburner  = modparam_afterburner;
2288         priv->param_power_save   = modparam_power_save;
2289         priv->param_power_output = modparam_power_output;
2290         priv->param_roamtrigger  = modparam_roamtrigger;
2291         priv->param_roamdelta    = modparam_roamdelta;
2292
2293         priv->param_country[0] = toupper(priv->param_country[0]);
2294         priv->param_country[1] = toupper(priv->param_country[1]);
2295         /* doesn't support EU as country code, use FI instead */
2296         if (!strcmp(priv->param_country, "EU"))
2297                 strcpy(priv->param_country, "FI");
2298
2299         if (priv->param_power_save < 0)
2300                 priv->param_power_save = 0;
2301         else if (priv->param_power_save > 2)
2302                 priv->param_power_save = 2;
2303
2304         if (priv->param_power_output < 0)
2305                 priv->param_power_output = 0;
2306         else if (priv->param_power_output > 3)
2307                 priv->param_power_output = 3;
2308
2309         if (priv->param_roamtrigger < -80)
2310                 priv->param_roamtrigger = -80;
2311         else if (priv->param_roamtrigger > -60)
2312                 priv->param_roamtrigger = -60;
2313
2314         if (priv->param_roamdelta < 0)
2315                 priv->param_roamdelta = 0;
2316         else if (priv->param_roamdelta > 2)
2317                 priv->param_roamdelta = 2;
2318
2319         if (modparam_workaround_interval < 0)
2320                 priv->param_workaround_interval = 500;
2321         else
2322                 priv->param_workaround_interval = modparam_workaround_interval;
2323
2324         rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
2325         rndis_set_config_parameter_str(usbdev, "FrameBursting",
2326                                         priv->param_frameburst ? "1" : "0");
2327         rndis_set_config_parameter_str(usbdev, "Afterburner",
2328                                         priv->param_afterburner ? "1" : "0");
2329         sprintf(buf, "%d", priv->param_power_save);
2330         rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
2331         sprintf(buf, "%d", priv->param_power_output);
2332         rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
2333         sprintf(buf, "%d", priv->param_roamtrigger);
2334         rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
2335         sprintf(buf, "%d", priv->param_roamdelta);
2336         rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
2337
2338         return 0;
2339 }
2340
2341 /* same as rndis_netdev_ops but with local multicast handler */
2342 static const struct net_device_ops rndis_wlan_netdev_ops = {
2343         .ndo_open               = usbnet_open,
2344         .ndo_stop               = usbnet_stop,
2345         .ndo_start_xmit         = usbnet_start_xmit,
2346         .ndo_tx_timeout         = usbnet_tx_timeout,
2347         .ndo_set_mac_address    = eth_mac_addr,
2348         .ndo_validate_addr      = eth_validate_addr,
2349         .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
2350 };
2351
2352
2353 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
2354 {
2355         struct wiphy *wiphy;
2356         struct rndis_wlan_private *priv;
2357         int retval, len;
2358         __le32 tmp;
2359
2360         /* allocate wiphy and rndis private data
2361          * NOTE: We only support a single virtual interface, so wiphy
2362          * and wireless_dev are somewhat synonymous for this device.
2363          */
2364         wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
2365         if (!wiphy)
2366                 return -ENOMEM;
2367
2368         priv = wiphy_priv(wiphy);
2369         usbdev->net->ieee80211_ptr = &priv->wdev;
2370         priv->wdev.wiphy = wiphy;
2371         priv->wdev.iftype = NL80211_IFTYPE_STATION;
2372
2373         /* These have to be initialized before calling generic_rndis_bind().
2374          * Otherwise we'll be in big trouble in rndis_wlan_early_init().
2375          */
2376         usbdev->driver_priv = priv;
2377         usbdev->net->wireless_handlers = &rndis_iw_handlers;
2378         priv->usbdev = usbdev;
2379
2380         mutex_init(&priv->command_lock);
2381         spin_lock_init(&priv->stats_lock);
2382
2383         /* because rndis_command() sleeps we need to use workqueue */
2384         priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2385         INIT_WORK(&priv->work, rndis_wlan_worker);
2386         INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2387         INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
2388
2389         /* try bind rndis_host */
2390         retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2391         if (retval < 0)
2392                 goto fail;
2393
2394         /* generic_rndis_bind set packet filter to multicast_all+
2395          * promisc mode which doesn't work well for our devices (device
2396          * picks up rssi to closest station instead of to access point).
2397          *
2398          * rndis_host wants to avoid all OID as much as possible
2399          * so do promisc/multicast handling in rndis_wlan.
2400          */
2401         usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
2402
2403         tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2404         retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2405                                                                 sizeof(tmp));
2406
2407         len = sizeof(tmp);
2408         retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
2409                                                                 &len);
2410         priv->multicast_size = le32_to_cpu(tmp);
2411         if (retval < 0 || priv->multicast_size < 0)
2412                 priv->multicast_size = 0;
2413         if (priv->multicast_size > 0)
2414                 usbdev->net->flags |= IFF_MULTICAST;
2415         else
2416                 usbdev->net->flags &= ~IFF_MULTICAST;
2417
2418         priv->iwstats.qual.qual = 0;
2419         priv->iwstats.qual.level = 0;
2420         priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2421                                         | IW_QUAL_LEVEL_UPDATED
2422                                         | IW_QUAL_NOISE_INVALID
2423                                         | IW_QUAL_QUAL_INVALID
2424                                         | IW_QUAL_LEVEL_INVALID;
2425
2426         /* fill-out wiphy structure and register w/ cfg80211 */
2427         memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
2428         wiphy->privid = rndis_wiphy_privid;
2429         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
2430                                         | BIT(NL80211_IFTYPE_ADHOC);
2431         wiphy->max_scan_ssids = 1;
2432
2433         /* TODO: fill-out band information based on priv->caps */
2434         rndis_wlan_get_caps(usbdev);
2435
2436         memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
2437         memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
2438         priv->band.channels = priv->channels;
2439         priv->band.n_channels = ARRAY_SIZE(rndis_channels);
2440         priv->band.bitrates = priv->rates;
2441         priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
2442         wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
2443         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2444
2445         set_wiphy_dev(wiphy, &usbdev->udev->dev);
2446
2447         if (wiphy_register(wiphy)) {
2448                 retval = -ENODEV;
2449                 goto fail;
2450         }
2451
2452         set_default_iw_params(usbdev);
2453
2454         /* set default rts/frag */
2455         rndis_set_wiphy_params(wiphy,
2456                         WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
2457
2458         /* turn radio on */
2459         priv->radio_on = 1;
2460         disassociate(usbdev, 1);
2461         netif_carrier_off(usbdev->net);
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         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2503
2504         devdbg(usbdev, "rndis_wlan_reset");
2505
2506         queue_delayed_work(priv->workqueue, &priv->stats_work,
2507                 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2508
2509         return deauthenticate(usbdev);
2510 }
2511
2512
2513 static int rndis_wlan_stop(struct usbnet *usbdev)
2514 {
2515         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2516         int retval;
2517
2518         devdbg(usbdev, "rndis_wlan_stop");
2519
2520         retval = disassociate(usbdev, 0);
2521
2522         priv->work_pending = 0;
2523         cancel_delayed_work_sync(&priv->stats_work);
2524         cancel_delayed_work_sync(&priv->scan_work);
2525         cancel_work_sync(&priv->work);
2526         flush_workqueue(priv->workqueue);
2527
2528         if (priv->scan_request) {
2529                 cfg80211_scan_done(priv->scan_request, true);
2530                 priv->scan_request = NULL;
2531         }
2532
2533         return retval;
2534 }
2535
2536
2537 static const struct driver_info bcm4320b_info = {
2538         .description =  "Wireless RNDIS device, BCM4320b based",
2539         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2540                                 FLAG_AVOID_UNLINK_URBS,
2541         .bind =         rndis_wlan_bind,
2542         .unbind =       rndis_wlan_unbind,
2543         .status =       rndis_status,
2544         .rx_fixup =     rndis_rx_fixup,
2545         .tx_fixup =     rndis_tx_fixup,
2546         .reset =        rndis_wlan_reset,
2547         .stop =         rndis_wlan_stop,
2548         .early_init =   bcm4320b_early_init,
2549         .link_change =  rndis_wlan_link_change,
2550 };
2551
2552 static const struct driver_info bcm4320a_info = {
2553         .description =  "Wireless RNDIS device, BCM4320a based",
2554         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2555                                 FLAG_AVOID_UNLINK_URBS,
2556         .bind =         rndis_wlan_bind,
2557         .unbind =       rndis_wlan_unbind,
2558         .status =       rndis_status,
2559         .rx_fixup =     rndis_rx_fixup,
2560         .tx_fixup =     rndis_tx_fixup,
2561         .reset =        rndis_wlan_reset,
2562         .stop =         rndis_wlan_stop,
2563         .early_init =   bcm4320a_early_init,
2564         .link_change =  rndis_wlan_link_change,
2565 };
2566
2567 static const struct driver_info rndis_wlan_info = {
2568         .description =  "Wireless RNDIS device",
2569         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2570                                 FLAG_AVOID_UNLINK_URBS,
2571         .bind =         rndis_wlan_bind,
2572         .unbind =       rndis_wlan_unbind,
2573         .status =       rndis_status,
2574         .rx_fixup =     rndis_rx_fixup,
2575         .tx_fixup =     rndis_tx_fixup,
2576         .reset =        rndis_wlan_reset,
2577         .stop =         rndis_wlan_stop,
2578         .early_init =   bcm4320a_early_init,
2579         .link_change =  rndis_wlan_link_change,
2580 };
2581
2582 /*-------------------------------------------------------------------------*/
2583
2584 static const struct usb_device_id products [] = {
2585 #define RNDIS_MASTER_INTERFACE \
2586         .bInterfaceClass        = USB_CLASS_COMM, \
2587         .bInterfaceSubClass     = 2 /* ACM */, \
2588         .bInterfaceProtocol     = 0x0ff
2589
2590 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2591  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2592  */
2593 {
2594         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2595                           | USB_DEVICE_ID_MATCH_DEVICE,
2596         .idVendor               = 0x0411,
2597         .idProduct              = 0x00bc,       /* Buffalo WLI-U2-KG125S */
2598         RNDIS_MASTER_INTERFACE,
2599         .driver_info            = (unsigned long) &bcm4320b_info,
2600 }, {
2601         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2602                           | USB_DEVICE_ID_MATCH_DEVICE,
2603         .idVendor               = 0x0baf,
2604         .idProduct              = 0x011b,       /* U.S. Robotics USR5421 */
2605         RNDIS_MASTER_INTERFACE,
2606         .driver_info            = (unsigned long) &bcm4320b_info,
2607 }, {
2608         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2609                           | USB_DEVICE_ID_MATCH_DEVICE,
2610         .idVendor               = 0x050d,
2611         .idProduct              = 0x011b,       /* Belkin F5D7051 */
2612         RNDIS_MASTER_INTERFACE,
2613         .driver_info            = (unsigned long) &bcm4320b_info,
2614 }, {
2615         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2616                           | USB_DEVICE_ID_MATCH_DEVICE,
2617         .idVendor               = 0x1799,       /* Belkin has two vendor ids */
2618         .idProduct              = 0x011b,       /* Belkin F5D7051 */
2619         RNDIS_MASTER_INTERFACE,
2620         .driver_info            = (unsigned long) &bcm4320b_info,
2621 }, {
2622         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2623                           | USB_DEVICE_ID_MATCH_DEVICE,
2624         .idVendor               = 0x13b1,
2625         .idProduct              = 0x0014,       /* Linksys WUSB54GSv2 */
2626         RNDIS_MASTER_INTERFACE,
2627         .driver_info            = (unsigned long) &bcm4320b_info,
2628 }, {
2629         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2630                           | USB_DEVICE_ID_MATCH_DEVICE,
2631         .idVendor               = 0x13b1,
2632         .idProduct              = 0x0026,       /* Linksys WUSB54GSC */
2633         RNDIS_MASTER_INTERFACE,
2634         .driver_info            = (unsigned long) &bcm4320b_info,
2635 }, {
2636         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2637                           | USB_DEVICE_ID_MATCH_DEVICE,
2638         .idVendor               = 0x0b05,
2639         .idProduct              = 0x1717,       /* Asus WL169gE */
2640         RNDIS_MASTER_INTERFACE,
2641         .driver_info            = (unsigned long) &bcm4320b_info,
2642 }, {
2643         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2644                           | USB_DEVICE_ID_MATCH_DEVICE,
2645         .idVendor               = 0x0a5c,
2646         .idProduct              = 0xd11b,       /* Eminent EM4045 */
2647         RNDIS_MASTER_INTERFACE,
2648         .driver_info            = (unsigned long) &bcm4320b_info,
2649 }, {
2650         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2651                           | USB_DEVICE_ID_MATCH_DEVICE,
2652         .idVendor               = 0x1690,
2653         .idProduct              = 0x0715,       /* BT Voyager 1055 */
2654         RNDIS_MASTER_INTERFACE,
2655         .driver_info            = (unsigned long) &bcm4320b_info,
2656 },
2657 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2658  * parameters available, hardware probably contain older firmware version with
2659  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2660  */
2661 {
2662         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2663                           | USB_DEVICE_ID_MATCH_DEVICE,
2664         .idVendor               = 0x13b1,
2665         .idProduct              = 0x000e,       /* Linksys WUSB54GSv1 */
2666         RNDIS_MASTER_INTERFACE,
2667         .driver_info            = (unsigned long) &bcm4320a_info,
2668 }, {
2669         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2670                           | USB_DEVICE_ID_MATCH_DEVICE,
2671         .idVendor               = 0x0baf,
2672         .idProduct              = 0x0111,       /* U.S. Robotics USR5420 */
2673         RNDIS_MASTER_INTERFACE,
2674         .driver_info            = (unsigned long) &bcm4320a_info,
2675 }, {
2676         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2677                           | USB_DEVICE_ID_MATCH_DEVICE,
2678         .idVendor               = 0x0411,
2679         .idProduct              = 0x004b,       /* BUFFALO WLI-USB-G54 */
2680         RNDIS_MASTER_INTERFACE,
2681         .driver_info            = (unsigned long) &bcm4320a_info,
2682 },
2683 /* Generic Wireless RNDIS devices that we don't have exact
2684  * idVendor/idProduct/chip yet.
2685  */
2686 {
2687         /* RNDIS is MSFT's un-official variant of CDC ACM */
2688         USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2689         .driver_info = (unsigned long) &rndis_wlan_info,
2690 }, {
2691         /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2692         USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2693         .driver_info = (unsigned long) &rndis_wlan_info,
2694 },
2695         { },            // END
2696 };
2697 MODULE_DEVICE_TABLE(usb, products);
2698
2699 static struct usb_driver rndis_wlan_driver = {
2700         .name =         "rndis_wlan",
2701         .id_table =     products,
2702         .probe =        usbnet_probe,
2703         .disconnect =   usbnet_disconnect,
2704         .suspend =      usbnet_suspend,
2705         .resume =       usbnet_resume,
2706 };
2707
2708 static int __init rndis_wlan_init(void)
2709 {
2710         return usb_register(&rndis_wlan_driver);
2711 }
2712 module_init(rndis_wlan_init);
2713
2714 static void __exit rndis_wlan_exit(void)
2715 {
2716         usb_deregister(&rndis_wlan_driver);
2717 }
2718 module_exit(rndis_wlan_exit);
2719
2720 MODULE_AUTHOR("Bjorge Dijkstra");
2721 MODULE_AUTHOR("Jussi Kivilinna");
2722 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2723 MODULE_LICENSE("GPL");
2724