OSDN Git Service

rndis_wlan: add cfg80211 get_station
[uclinux-h8/linux.git] / drivers / net / wireless / rndis_wlan.c
1 /*
2  * Driver for RNDIS based wireless USB devices.
3  *
4  * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5  * Copyright (C) 2008-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_CURRENT_ADDRESS               cpu_to_le32(0x01010102)
104 #define OID_802_3_MULTICAST_LIST                cpu_to_le32(0x01010103)
105 #define OID_802_3_MAXIMUM_LIST_SIZE             cpu_to_le32(0x01010104)
106
107 #define OID_802_11_BSSID                        cpu_to_le32(0x0d010101)
108 #define OID_802_11_SSID                         cpu_to_le32(0x0d010102)
109 #define OID_802_11_INFRASTRUCTURE_MODE          cpu_to_le32(0x0d010108)
110 #define OID_802_11_ADD_WEP                      cpu_to_le32(0x0d010113)
111 #define OID_802_11_REMOVE_WEP                   cpu_to_le32(0x0d010114)
112 #define OID_802_11_DISASSOCIATE                 cpu_to_le32(0x0d010115)
113 #define OID_802_11_AUTHENTICATION_MODE          cpu_to_le32(0x0d010118)
114 #define OID_802_11_PRIVACY_FILTER               cpu_to_le32(0x0d010119)
115 #define OID_802_11_BSSID_LIST_SCAN              cpu_to_le32(0x0d01011a)
116 #define OID_802_11_ENCRYPTION_STATUS            cpu_to_le32(0x0d01011b)
117 #define OID_802_11_ADD_KEY                      cpu_to_le32(0x0d01011d)
118 #define OID_802_11_REMOVE_KEY                   cpu_to_le32(0x0d01011e)
119 #define OID_802_11_ASSOCIATION_INFORMATION      cpu_to_le32(0x0d01011f)
120 #define OID_802_11_PMKID                        cpu_to_le32(0x0d010123)
121 #define OID_802_11_NETWORK_TYPES_SUPPORTED      cpu_to_le32(0x0d010203)
122 #define OID_802_11_NETWORK_TYPE_IN_USE          cpu_to_le32(0x0d010204)
123 #define OID_802_11_TX_POWER_LEVEL               cpu_to_le32(0x0d010205)
124 #define OID_802_11_RSSI                         cpu_to_le32(0x0d010206)
125 #define OID_802_11_RSSI_TRIGGER                 cpu_to_le32(0x0d010207)
126 #define OID_802_11_FRAGMENTATION_THRESHOLD      cpu_to_le32(0x0d010209)
127 #define OID_802_11_RTS_THRESHOLD                cpu_to_le32(0x0d01020a)
128 #define OID_802_11_SUPPORTED_RATES              cpu_to_le32(0x0d01020e)
129 #define OID_802_11_CONFIGURATION                cpu_to_le32(0x0d010211)
130 #define OID_802_11_BSSID_LIST                   cpu_to_le32(0x0d010217)
131
132
133 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
134 #define WL_NOISE        -96     /* typical noise level in dBm */
135 #define WL_SIGMAX       -32     /* typical maximum signal level in dBm */
136
137
138 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
139  * based on wireless rndis) has default txpower of 13dBm.
140  * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
141  *  100% : 20 mW ~ 13dBm
142  *   75% : 15 mW ~ 12dBm
143  *   50% : 10 mW ~ 10dBm
144  *   25% :  5 mW ~  7dBm
145  */
146 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
147 #define BCM4320_DEFAULT_TXPOWER_DBM_75  12
148 #define BCM4320_DEFAULT_TXPOWER_DBM_50  10
149 #define BCM4320_DEFAULT_TXPOWER_DBM_25  7
150
151
152 /* codes for "status" field of completion messages */
153 #define RNDIS_STATUS_ADAPTER_NOT_READY          cpu_to_le32(0xc0010011)
154 #define RNDIS_STATUS_ADAPTER_NOT_OPEN           cpu_to_le32(0xc0010012)
155
156
157 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
158  * slightly modified for datatype endianess, etc
159  */
160 #define NDIS_802_11_LENGTH_SSID 32
161 #define NDIS_802_11_LENGTH_RATES 8
162 #define NDIS_802_11_LENGTH_RATES_EX 16
163
164 enum ndis_80211_net_type {
165         NDIS_80211_TYPE_FREQ_HOP,
166         NDIS_80211_TYPE_DIRECT_SEQ,
167         NDIS_80211_TYPE_OFDM_A,
168         NDIS_80211_TYPE_OFDM_G
169 };
170
171 enum ndis_80211_net_infra {
172         NDIS_80211_INFRA_ADHOC,
173         NDIS_80211_INFRA_INFRA,
174         NDIS_80211_INFRA_AUTO_UNKNOWN
175 };
176
177 enum ndis_80211_auth_mode {
178         NDIS_80211_AUTH_OPEN,
179         NDIS_80211_AUTH_SHARED,
180         NDIS_80211_AUTH_AUTO_SWITCH,
181         NDIS_80211_AUTH_WPA,
182         NDIS_80211_AUTH_WPA_PSK,
183         NDIS_80211_AUTH_WPA_NONE,
184         NDIS_80211_AUTH_WPA2,
185         NDIS_80211_AUTH_WPA2_PSK
186 };
187
188 enum ndis_80211_encr_status {
189         NDIS_80211_ENCR_WEP_ENABLED,
190         NDIS_80211_ENCR_DISABLED,
191         NDIS_80211_ENCR_WEP_KEY_ABSENT,
192         NDIS_80211_ENCR_NOT_SUPPORTED,
193         NDIS_80211_ENCR_TKIP_ENABLED,
194         NDIS_80211_ENCR_TKIP_KEY_ABSENT,
195         NDIS_80211_ENCR_CCMP_ENABLED,
196         NDIS_80211_ENCR_CCMP_KEY_ABSENT
197 };
198
199 enum ndis_80211_priv_filter {
200         NDIS_80211_PRIV_ACCEPT_ALL,
201         NDIS_80211_PRIV_8021X_WEP
202 };
203
204 enum ndis_80211_status_type {
205         NDIS_80211_STATUSTYPE_AUTHENTICATION,
206         NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
207         NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
208         NDIS_80211_STATUSTYPE_RADIOSTATE,
209 };
210
211 enum ndis_80211_media_stream_mode {
212         NDIS_80211_MEDIA_STREAM_OFF,
213         NDIS_80211_MEDIA_STREAM_ON
214 };
215
216 enum ndis_80211_radio_status {
217         NDIS_80211_RADIO_STATUS_ON,
218         NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
219         NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
220 };
221
222 enum ndis_80211_addkey_bits {
223         NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
224         NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
225         NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
226         NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
227 };
228
229 enum ndis_80211_addwep_bits {
230         NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
231         NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
232 };
233
234 struct ndis_80211_auth_request {
235         __le32 length;
236         u8 bssid[6];
237         u8 padding[2];
238         __le32 flags;
239 } __attribute__((packed));
240
241 struct ndis_80211_pmkid_candidate {
242         u8 bssid[6];
243         u8 padding[2];
244         __le32 flags;
245 } __attribute__((packed));
246
247 struct ndis_80211_pmkid_cand_list {
248         __le32 version;
249         __le32 num_candidates;
250         struct ndis_80211_pmkid_candidate candidate_list[0];
251 } __attribute__((packed));
252
253 struct ndis_80211_status_indication {
254         __le32 status_type;
255         union {
256                 enum ndis_80211_media_stream_mode       media_stream_mode;
257                 enum ndis_80211_radio_status            radio_status;
258                 struct ndis_80211_auth_request          auth_request[0];
259                 struct ndis_80211_pmkid_cand_list       cand_list;
260         } u;
261 } __attribute__((packed));
262
263 struct ndis_80211_ssid {
264         __le32 length;
265         u8 essid[NDIS_802_11_LENGTH_SSID];
266 } __attribute__((packed));
267
268 struct ndis_80211_conf_freq_hop {
269         __le32 length;
270         __le32 hop_pattern;
271         __le32 hop_set;
272         __le32 dwell_time;
273 } __attribute__((packed));
274
275 struct ndis_80211_conf {
276         __le32 length;
277         __le32 beacon_period;
278         __le32 atim_window;
279         __le32 ds_config;
280         struct ndis_80211_conf_freq_hop fh_config;
281 } __attribute__((packed));
282
283 struct ndis_80211_bssid_ex {
284         __le32 length;
285         u8 mac[6];
286         u8 padding[2];
287         struct ndis_80211_ssid ssid;
288         __le32 privacy;
289         __le32 rssi;
290         __le32 net_type;
291         struct ndis_80211_conf config;
292         __le32 net_infra;
293         u8 rates[NDIS_802_11_LENGTH_RATES_EX];
294         __le32 ie_length;
295         u8 ies[0];
296 } __attribute__((packed));
297
298 struct ndis_80211_bssid_list_ex {
299         __le32 num_items;
300         struct ndis_80211_bssid_ex bssid[0];
301 } __attribute__((packed));
302
303 struct ndis_80211_fixed_ies {
304         u8 timestamp[8];
305         __le16 beacon_interval;
306         __le16 capabilities;
307 } __attribute__((packed));
308
309 struct ndis_80211_wep_key {
310         __le32 size;
311         __le32 index;
312         __le32 length;
313         u8 material[32];
314 } __attribute__((packed));
315
316 struct ndis_80211_key {
317         __le32 size;
318         __le32 index;
319         __le32 length;
320         u8 bssid[6];
321         u8 padding[6];
322         u8 rsc[8];
323         u8 material[32];
324 } __attribute__((packed));
325
326 struct ndis_80211_remove_key {
327         __le32 size;
328         __le32 index;
329         u8 bssid[6];
330         u8 padding[2];
331 } __attribute__((packed));
332
333 struct ndis_config_param {
334         __le32 name_offs;
335         __le32 name_length;
336         __le32 type;
337         __le32 value_offs;
338         __le32 value_length;
339 } __attribute__((packed));
340
341 struct ndis_80211_assoc_info {
342         __le32 length;
343         __le16 req_ies;
344         struct req_ie {
345                 __le16 capa;
346                 __le16 listen_interval;
347                 u8 cur_ap_address[6];
348         } req_ie;
349         __le32 req_ie_length;
350         __le32 offset_req_ies;
351         __le16 resp_ies;
352         struct resp_ie {
353                 __le16 capa;
354                 __le16 status_code;
355                 __le16 assoc_id;
356         } resp_ie;
357         __le32 resp_ie_length;
358         __le32 offset_resp_ies;
359 } __attribute__((packed));
360
361 /*
362  *  private data
363  */
364 #define NET_TYPE_11FB   0
365
366 #define CAP_MODE_80211A         1
367 #define CAP_MODE_80211B         2
368 #define CAP_MODE_80211G         4
369 #define CAP_MODE_MASK           7
370
371 #define WORK_LINK_UP            (1<<0)
372 #define WORK_LINK_DOWN          (1<<1)
373 #define WORK_SET_MULTICAST_LIST (1<<2)
374
375 #define RNDIS_WLAN_ALG_NONE     0
376 #define RNDIS_WLAN_ALG_WEP      (1<<0)
377 #define RNDIS_WLAN_ALG_TKIP     (1<<1)
378 #define RNDIS_WLAN_ALG_CCMP     (1<<2)
379
380 #define RNDIS_WLAN_KEY_MGMT_NONE        0
381 #define RNDIS_WLAN_KEY_MGMT_802_1X      (1<<0)
382 #define RNDIS_WLAN_KEY_MGMT_PSK         (1<<1)
383
384 #define COMMAND_BUFFER_SIZE     (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
385
386 static const struct ieee80211_channel rndis_channels[] = {
387         { .center_freq = 2412 },
388         { .center_freq = 2417 },
389         { .center_freq = 2422 },
390         { .center_freq = 2427 },
391         { .center_freq = 2432 },
392         { .center_freq = 2437 },
393         { .center_freq = 2442 },
394         { .center_freq = 2447 },
395         { .center_freq = 2452 },
396         { .center_freq = 2457 },
397         { .center_freq = 2462 },
398         { .center_freq = 2467 },
399         { .center_freq = 2472 },
400         { .center_freq = 2484 },
401 };
402
403 static const struct ieee80211_rate rndis_rates[] = {
404         { .bitrate = 10 },
405         { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
406         { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
407         { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
408         { .bitrate = 60 },
409         { .bitrate = 90 },
410         { .bitrate = 120 },
411         { .bitrate = 180 },
412         { .bitrate = 240 },
413         { .bitrate = 360 },
414         { .bitrate = 480 },
415         { .bitrate = 540 }
416 };
417
418 static const u32 rndis_cipher_suites[] = {
419         WLAN_CIPHER_SUITE_WEP40,
420         WLAN_CIPHER_SUITE_WEP104,
421         WLAN_CIPHER_SUITE_TKIP,
422         WLAN_CIPHER_SUITE_CCMP,
423 };
424
425 struct rndis_wlan_encr_key {
426         int len;
427         u32 cipher;
428         u8 material[32];
429         u8 bssid[ETH_ALEN];
430         bool pairwise;
431         bool tx_key;
432 };
433
434 /* RNDIS device private data */
435 struct rndis_wlan_private {
436         struct usbnet *usbdev;
437
438         struct wireless_dev wdev;
439
440         struct cfg80211_scan_request *scan_request;
441
442         struct workqueue_struct *workqueue;
443         struct delayed_work stats_work;
444         struct delayed_work scan_work;
445         struct work_struct work;
446         struct mutex command_lock;
447         unsigned long work_pending;
448         int last_qual;
449
450         struct ieee80211_supported_band band;
451         struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
452         struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
453         u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
454
455         int caps;
456         int multicast_size;
457
458         /* module parameters */
459         char param_country[4];
460         int  param_frameburst;
461         int  param_afterburner;
462         int  param_power_save;
463         int  param_power_output;
464         int  param_roamtrigger;
465         int  param_roamdelta;
466         u32  param_workaround_interval;
467
468         /* hardware state */
469         int radio_on;
470         int infra_mode;
471         bool connected;
472         u8 bssid[ETH_ALEN];
473         struct ndis_80211_ssid essid;
474         __le32 current_command_oid;
475
476         /* encryption stuff */
477         int  encr_tx_key_index;
478         struct rndis_wlan_encr_key encr_keys[4];
479         enum nl80211_auth_type wpa_auth_type;
480         int  wpa_version;
481         int  wpa_keymgmt;
482         int  wpa_ie_len;
483         u8  *wpa_ie;
484         int  wpa_cipher_pair;
485         int  wpa_cipher_group;
486
487         u8 command_buffer[COMMAND_BUFFER_SIZE];
488 };
489
490 /*
491  * cfg80211 ops
492  */
493 static int rndis_change_virtual_intf(struct wiphy *wiphy,
494                                         struct net_device *dev,
495                                         enum nl80211_iftype type, u32 *flags,
496                                         struct vif_params *params);
497
498 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
499                         struct cfg80211_scan_request *request);
500
501 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
502
503 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
504                                 int dbm);
505 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
506
507 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
508                                 struct cfg80211_connect_params *sme);
509
510 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
511                                 u16 reason_code);
512
513 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
514                                         struct cfg80211_ibss_params *params);
515
516 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
517
518 static int rndis_set_channel(struct wiphy *wiphy,
519         struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
520
521 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
522                                         u8 key_index, const u8 *mac_addr,
523                                         struct key_params *params);
524
525 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
526                                         u8 key_index, const u8 *mac_addr);
527
528 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
529                                                                 u8 key_index);
530
531 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
532                                         u8 *mac, struct station_info *sinfo);
533
534 static struct cfg80211_ops rndis_config_ops = {
535         .change_virtual_intf = rndis_change_virtual_intf,
536         .scan = rndis_scan,
537         .set_wiphy_params = rndis_set_wiphy_params,
538         .set_tx_power = rndis_set_tx_power,
539         .get_tx_power = rndis_get_tx_power,
540         .connect = rndis_connect,
541         .disconnect = rndis_disconnect,
542         .join_ibss = rndis_join_ibss,
543         .leave_ibss = rndis_leave_ibss,
544         .set_channel = rndis_set_channel,
545         .add_key = rndis_add_key,
546         .del_key = rndis_del_key,
547         .set_default_key = rndis_set_default_key,
548         .get_station = rndis_get_station,
549 };
550
551 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
552
553
554 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
555 {
556         return (struct rndis_wlan_private *)dev->driver_priv;
557 }
558
559
560 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
561 {
562         switch (priv->param_power_output) {
563         default:
564         case 3:
565                 return BCM4320_DEFAULT_TXPOWER_DBM_100;
566         case 2:
567                 return BCM4320_DEFAULT_TXPOWER_DBM_75;
568         case 1:
569                 return BCM4320_DEFAULT_TXPOWER_DBM_50;
570         case 0:
571                 return BCM4320_DEFAULT_TXPOWER_DBM_25;
572         }
573 }
574
575
576 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
577 {
578         int cipher = priv->encr_keys[idx].cipher;
579
580         return (cipher == WLAN_CIPHER_SUITE_CCMP ||
581                 cipher == WLAN_CIPHER_SUITE_TKIP);
582 }
583
584
585 static int rndis_cipher_to_alg(u32 cipher)
586 {
587         switch (cipher) {
588         default:
589                 return RNDIS_WLAN_ALG_NONE;
590         case WLAN_CIPHER_SUITE_WEP40:
591         case WLAN_CIPHER_SUITE_WEP104:
592                 return RNDIS_WLAN_ALG_WEP;
593         case WLAN_CIPHER_SUITE_TKIP:
594                 return RNDIS_WLAN_ALG_TKIP;
595         case WLAN_CIPHER_SUITE_CCMP:
596                 return RNDIS_WLAN_ALG_CCMP;
597         }
598 }
599
600 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
601 {
602         switch (akm_suite) {
603         default:
604                 return RNDIS_WLAN_KEY_MGMT_NONE;
605         case WLAN_AKM_SUITE_8021X:
606                 return RNDIS_WLAN_KEY_MGMT_802_1X;
607         case WLAN_AKM_SUITE_PSK:
608                 return RNDIS_WLAN_KEY_MGMT_PSK;
609         }
610 }
611
612
613 #ifdef DEBUG
614 static const char *oid_to_string(__le32 oid)
615 {
616         switch (oid) {
617 #define OID_STR(oid) case oid: return(#oid)
618                 /* from rndis_host.h */
619                 OID_STR(OID_802_3_PERMANENT_ADDRESS);
620                 OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
621                 OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
622                 OID_STR(OID_GEN_PHYSICAL_MEDIUM);
623
624                 /* from rndis_wlan.c */
625                 OID_STR(OID_GEN_LINK_SPEED);
626                 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
627
628                 OID_STR(OID_GEN_XMIT_OK);
629                 OID_STR(OID_GEN_RCV_OK);
630                 OID_STR(OID_GEN_XMIT_ERROR);
631                 OID_STR(OID_GEN_RCV_ERROR);
632                 OID_STR(OID_GEN_RCV_NO_BUFFER);
633
634                 OID_STR(OID_802_3_CURRENT_ADDRESS);
635                 OID_STR(OID_802_3_MULTICAST_LIST);
636                 OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
637
638                 OID_STR(OID_802_11_BSSID);
639                 OID_STR(OID_802_11_SSID);
640                 OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
641                 OID_STR(OID_802_11_ADD_WEP);
642                 OID_STR(OID_802_11_REMOVE_WEP);
643                 OID_STR(OID_802_11_DISASSOCIATE);
644                 OID_STR(OID_802_11_AUTHENTICATION_MODE);
645                 OID_STR(OID_802_11_PRIVACY_FILTER);
646                 OID_STR(OID_802_11_BSSID_LIST_SCAN);
647                 OID_STR(OID_802_11_ENCRYPTION_STATUS);
648                 OID_STR(OID_802_11_ADD_KEY);
649                 OID_STR(OID_802_11_REMOVE_KEY);
650                 OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
651                 OID_STR(OID_802_11_PMKID);
652                 OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
653                 OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
654                 OID_STR(OID_802_11_TX_POWER_LEVEL);
655                 OID_STR(OID_802_11_RSSI);
656                 OID_STR(OID_802_11_RSSI_TRIGGER);
657                 OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
658                 OID_STR(OID_802_11_RTS_THRESHOLD);
659                 OID_STR(OID_802_11_SUPPORTED_RATES);
660                 OID_STR(OID_802_11_CONFIGURATION);
661                 OID_STR(OID_802_11_BSSID_LIST);
662 #undef OID_STR
663         }
664
665         return "?";
666 }
667 #else
668 static const char *oid_to_string(__le32 oid)
669 {
670         return "?";
671 }
672 #endif
673
674
675 /* translate error code */
676 static int rndis_error_status(__le32 rndis_status)
677 {
678         int ret = -EINVAL;
679         switch (rndis_status) {
680         case RNDIS_STATUS_SUCCESS:
681                 ret = 0;
682                 break;
683         case RNDIS_STATUS_FAILURE:
684         case RNDIS_STATUS_INVALID_DATA:
685                 ret = -EINVAL;
686                 break;
687         case RNDIS_STATUS_NOT_SUPPORTED:
688                 ret = -EOPNOTSUPP;
689                 break;
690         case RNDIS_STATUS_ADAPTER_NOT_READY:
691         case RNDIS_STATUS_ADAPTER_NOT_OPEN:
692                 ret = -EBUSY;
693                 break;
694         }
695         return ret;
696 }
697
698
699 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
700 {
701         struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
702         union {
703                 void                    *buf;
704                 struct rndis_msg_hdr    *header;
705                 struct rndis_query      *get;
706                 struct rndis_query_c    *get_c;
707         } u;
708         int ret, buflen;
709
710         buflen = *len + sizeof(*u.get);
711         if (buflen < CONTROL_BUFFER_SIZE)
712                 buflen = CONTROL_BUFFER_SIZE;
713
714         if (buflen > COMMAND_BUFFER_SIZE) {
715                 u.buf = kmalloc(buflen, GFP_KERNEL);
716                 if (!u.buf)
717                         return -ENOMEM;
718         } else {
719                 u.buf = priv->command_buffer;
720         }
721
722         mutex_lock(&priv->command_lock);
723
724         memset(u.get, 0, sizeof *u.get);
725         u.get->msg_type = RNDIS_MSG_QUERY;
726         u.get->msg_len = cpu_to_le32(sizeof *u.get);
727         u.get->oid = oid;
728
729         priv->current_command_oid = oid;
730         ret = rndis_command(dev, u.header, buflen);
731         priv->current_command_oid = 0;
732         if (ret < 0)
733                 devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
734                         "(%08x)", oid_to_string(oid), ret,
735                         le32_to_cpu(u.get_c->status));
736
737         if (ret == 0) {
738                 ret = le32_to_cpu(u.get_c->len);
739                 if (ret > *len)
740                         *len = ret;
741                 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
742                 ret = rndis_error_status(u.get_c->status);
743
744                 if (ret < 0)
745                         devdbg(dev, "rndis_query_oid(%s): device returned "
746                                 "error,  0x%08x (%d)", oid_to_string(oid),
747                                 le32_to_cpu(u.get_c->status), ret);
748         }
749
750         mutex_unlock(&priv->command_lock);
751
752         if (u.buf != priv->command_buffer)
753                 kfree(u.buf);
754         return ret;
755 }
756
757
758 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
759 {
760         struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
761         union {
762                 void                    *buf;
763                 struct rndis_msg_hdr    *header;
764                 struct rndis_set        *set;
765                 struct rndis_set_c      *set_c;
766         } u;
767         int ret, buflen;
768
769         buflen = len + sizeof(*u.set);
770         if (buflen < CONTROL_BUFFER_SIZE)
771                 buflen = CONTROL_BUFFER_SIZE;
772
773         if (buflen > COMMAND_BUFFER_SIZE) {
774                 u.buf = kmalloc(buflen, GFP_KERNEL);
775                 if (!u.buf)
776                         return -ENOMEM;
777         } else {
778                 u.buf = priv->command_buffer;
779         }
780
781         mutex_lock(&priv->command_lock);
782
783         memset(u.set, 0, sizeof *u.set);
784         u.set->msg_type = RNDIS_MSG_SET;
785         u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
786         u.set->oid = oid;
787         u.set->len = cpu_to_le32(len);
788         u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
789         u.set->handle = cpu_to_le32(0);
790         memcpy(u.buf + sizeof(*u.set), data, len);
791
792         priv->current_command_oid = oid;
793         ret = rndis_command(dev, u.header, buflen);
794         priv->current_command_oid = 0;
795         if (ret < 0)
796                 devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
797                         "(%08x)", oid_to_string(oid), ret,
798                         le32_to_cpu(u.set_c->status));
799
800         if (ret == 0) {
801                 ret = rndis_error_status(u.set_c->status);
802
803                 if (ret < 0)
804                         devdbg(dev, "rndis_set_oid(%s): device returned error, "
805                                 "0x%08x (%d)", oid_to_string(oid),
806                                 le32_to_cpu(u.set_c->status), ret);
807         }
808
809         mutex_unlock(&priv->command_lock);
810
811         if (u.buf != priv->command_buffer)
812                 kfree(u.buf);
813         return ret;
814 }
815
816
817 static int rndis_reset(struct usbnet *usbdev)
818 {
819         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
820         struct rndis_reset *reset;
821         int ret;
822
823         mutex_lock(&priv->command_lock);
824
825         reset = (void *)priv->command_buffer;
826         memset(reset, 0, sizeof(*reset));
827         reset->msg_type = RNDIS_MSG_RESET;
828         reset->msg_len = cpu_to_le32(sizeof(*reset));
829         priv->current_command_oid = 0;
830         ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
831
832         mutex_unlock(&priv->command_lock);
833
834         if (ret < 0)
835                 return ret;
836         return 0;
837 }
838
839
840 /*
841  * Specs say that we can only set config parameters only soon after device
842  * initialization.
843  *   value_type: 0 = u32, 2 = unicode string
844  */
845 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
846                                                 int value_type, void *value)
847 {
848         struct ndis_config_param *infobuf;
849         int value_len, info_len, param_len, ret, i;
850         __le16 *unibuf;
851         __le32 *dst_value;
852
853         if (value_type == 0)
854                 value_len = sizeof(__le32);
855         else if (value_type == 2)
856                 value_len = strlen(value) * sizeof(__le16);
857         else
858                 return -EINVAL;
859
860         param_len = strlen(param) * sizeof(__le16);
861         info_len = sizeof(*infobuf) + param_len + value_len;
862
863 #ifdef DEBUG
864         info_len += 12;
865 #endif
866         infobuf = kmalloc(info_len, GFP_KERNEL);
867         if (!infobuf)
868                 return -ENOMEM;
869
870 #ifdef DEBUG
871         info_len -= 12;
872         /* extra 12 bytes are for padding (debug output) */
873         memset(infobuf, 0xCC, info_len + 12);
874 #endif
875
876         if (value_type == 2)
877                 devdbg(dev, "setting config parameter: %s, value: %s",
878                                                 param, (u8 *)value);
879         else
880                 devdbg(dev, "setting config parameter: %s, value: %d",
881                                                 param, *(u32 *)value);
882
883         infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
884         infobuf->name_length = cpu_to_le32(param_len);
885         infobuf->type = cpu_to_le32(value_type);
886         infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
887         infobuf->value_length = cpu_to_le32(value_len);
888
889         /* simple string to unicode string conversion */
890         unibuf = (void *)infobuf + sizeof(*infobuf);
891         for (i = 0; i < param_len / sizeof(__le16); i++)
892                 unibuf[i] = cpu_to_le16(param[i]);
893
894         if (value_type == 2) {
895                 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
896                 for (i = 0; i < value_len / sizeof(__le16); i++)
897                         unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
898         } else {
899                 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
900                 *dst_value = cpu_to_le32(*(u32 *)value);
901         }
902
903 #ifdef DEBUG
904         devdbg(dev, "info buffer (len: %d):", info_len);
905         for (i = 0; i < info_len; i += 12) {
906                 u32 *tmp = (u32 *)((u8 *)infobuf + i);
907                 devdbg(dev, "%08X:%08X:%08X",
908                         cpu_to_be32(tmp[0]),
909                         cpu_to_be32(tmp[1]),
910                         cpu_to_be32(tmp[2]));
911         }
912 #endif
913
914         ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
915                                                         infobuf, info_len);
916         if (ret != 0)
917                 devdbg(dev, "setting rndis config parameter failed, %d.", ret);
918
919         kfree(infobuf);
920         return ret;
921 }
922
923 static int rndis_set_config_parameter_str(struct usbnet *dev,
924                                                 char *param, char *value)
925 {
926         return(rndis_set_config_parameter(dev, param, 2, value));
927 }
928
929 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
930                                                 char *param, u32 value)
931 {
932         return(rndis_set_config_parameter(dev, param, 0, &value));
933 }*/
934
935
936 /*
937  * data conversion functions
938  */
939 static int level_to_qual(int level)
940 {
941         int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
942         return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
943 }
944
945
946 /*
947  * common functions
948  */
949 static int set_infra_mode(struct usbnet *usbdev, int mode);
950 static void restore_keys(struct usbnet *usbdev);
951 static int rndis_check_bssid_list(struct usbnet *usbdev);
952
953 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
954 {
955         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
956         int ret;
957
958         ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
959         if (ret < 0) {
960                 devwarn(usbdev, "setting SSID failed (%08X)", ret);
961                 return ret;
962         }
963         if (ret == 0) {
964                 memcpy(&priv->essid, ssid, sizeof(priv->essid));
965                 priv->radio_on = 1;
966                 devdbg(usbdev, "set_essid: radio_on = 1");
967         }
968
969         return ret;
970 }
971
972 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
973 {
974         int ret;
975
976         ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
977         if (ret < 0) {
978                 devwarn(usbdev, "setting BSSID[%pM] failed (%08X)", bssid, ret);
979                 return ret;
980         }
981
982         return ret;
983 }
984
985 static int clear_bssid(struct usbnet *usbdev)
986 {
987         u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
988
989         return set_bssid(usbdev, broadcast_mac);
990 }
991
992 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
993 {
994         int ret, len;
995
996         len = ETH_ALEN;
997         ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
998
999         if (ret != 0)
1000                 memset(bssid, 0, ETH_ALEN);
1001
1002         return ret;
1003 }
1004
1005 static int get_association_info(struct usbnet *usbdev,
1006                         struct ndis_80211_assoc_info *info, int len)
1007 {
1008         return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1009                                 info, &len);
1010 }
1011
1012 static bool is_associated(struct usbnet *usbdev)
1013 {
1014         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1015         u8 bssid[ETH_ALEN];
1016         int ret;
1017
1018         if (!priv->radio_on)
1019                 return false;
1020
1021         ret = get_bssid(usbdev, bssid);
1022
1023         return (ret == 0 && !is_zero_ether_addr(bssid));
1024 }
1025
1026
1027 static int disassociate(struct usbnet *usbdev, int reset_ssid)
1028 {
1029         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1030         struct ndis_80211_ssid ssid;
1031         int i, ret = 0;
1032
1033         if (priv->radio_on) {
1034                 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1035                 if (ret == 0) {
1036                         priv->radio_on = 0;
1037                         devdbg(usbdev, "disassociate: radio_on = 0");
1038
1039                         if (reset_ssid)
1040                                 msleep(100);
1041                 }
1042         }
1043
1044         /* disassociate causes radio to be turned off; if reset_ssid
1045          * is given, set random ssid to enable radio */
1046         if (reset_ssid) {
1047                 /* Set device to infrastructure mode so we don't get ad-hoc
1048                  * 'media connect' indications with the random ssid.
1049                  */
1050                 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1051
1052                 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1053                 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1054                 ssid.essid[0] = 0x1;
1055                 ssid.essid[1] = 0xff;
1056                 for (i = 2; i < sizeof(ssid.essid); i++)
1057                         ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1058                 ret = set_essid(usbdev, &ssid);
1059         }
1060         return ret;
1061 }
1062
1063
1064 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1065                                 enum nl80211_auth_type auth_type, int keymgmt)
1066 {
1067         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1068         __le32 tmp;
1069         int auth_mode, ret;
1070
1071         devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
1072                 "keymgmt=0x%x", wpa_version, auth_type, keymgmt);
1073
1074         if (wpa_version & NL80211_WPA_VERSION_2) {
1075                 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1076                         auth_mode = NDIS_80211_AUTH_WPA2;
1077                 else
1078                         auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1079         } else if (wpa_version & NL80211_WPA_VERSION_1) {
1080                 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1081                         auth_mode = NDIS_80211_AUTH_WPA;
1082                 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1083                         auth_mode = NDIS_80211_AUTH_WPA_PSK;
1084                 else
1085                         auth_mode = NDIS_80211_AUTH_WPA_NONE;
1086         } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1087                 auth_mode = NDIS_80211_AUTH_SHARED;
1088         else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1089                 auth_mode = NDIS_80211_AUTH_OPEN;
1090         else
1091                 return -ENOTSUPP;
1092
1093         tmp = cpu_to_le32(auth_mode);
1094         ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1095                                                                 sizeof(tmp));
1096         if (ret != 0) {
1097                 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
1098                 return ret;
1099         }
1100
1101         priv->wpa_version = wpa_version;
1102         priv->wpa_auth_type = auth_type;
1103         priv->wpa_keymgmt = keymgmt;
1104
1105         return 0;
1106 }
1107
1108
1109 static int set_priv_filter(struct usbnet *usbdev)
1110 {
1111         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1112         __le32 tmp;
1113
1114         devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
1115
1116         if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1117             priv->wpa_version & NL80211_WPA_VERSION_1)
1118                 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1119         else
1120                 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1121
1122         return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1123                                                                 sizeof(tmp));
1124 }
1125
1126
1127 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1128 {
1129         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1130         __le32 tmp;
1131         int encr_mode, ret;
1132
1133         devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
1134                 pairwise, groupwise);
1135
1136         if (pairwise & RNDIS_WLAN_ALG_CCMP)
1137                 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1138         else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1139                 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1140         else if (pairwise & RNDIS_WLAN_ALG_WEP)
1141                 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1142         else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1143                 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1144         else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1145                 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1146         else
1147                 encr_mode = NDIS_80211_ENCR_DISABLED;
1148
1149         tmp = cpu_to_le32(encr_mode);
1150         ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1151                                                                 sizeof(tmp));
1152         if (ret != 0) {
1153                 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
1154                 return ret;
1155         }
1156
1157         priv->wpa_cipher_pair = pairwise;
1158         priv->wpa_cipher_group = groupwise;
1159         return 0;
1160 }
1161
1162
1163 static int set_infra_mode(struct usbnet *usbdev, int mode)
1164 {
1165         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1166         __le32 tmp;
1167         int ret;
1168
1169         devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
1170
1171         tmp = cpu_to_le32(mode);
1172         ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1173                                                                 sizeof(tmp));
1174         if (ret != 0) {
1175                 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
1176                 return ret;
1177         }
1178
1179         /* NDIS drivers clear keys when infrastructure mode is
1180          * changed. But Linux tools assume otherwise. So set the
1181          * keys */
1182         restore_keys(usbdev);
1183
1184         priv->infra_mode = mode;
1185         return 0;
1186 }
1187
1188
1189 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1190 {
1191         __le32 tmp;
1192
1193         devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
1194
1195         if (rts_threshold < 0 || rts_threshold > 2347)
1196                 rts_threshold = 2347;
1197
1198         tmp = cpu_to_le32(rts_threshold);
1199         return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1200                                                                 sizeof(tmp));
1201 }
1202
1203
1204 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1205 {
1206         __le32 tmp;
1207
1208         devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
1209
1210         if (frag_threshold < 256 || frag_threshold > 2346)
1211                 frag_threshold = 2346;
1212
1213         tmp = cpu_to_le32(frag_threshold);
1214         return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1215                                                                 sizeof(tmp));
1216 }
1217
1218
1219 static void set_default_iw_params(struct usbnet *usbdev)
1220 {
1221         set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1222         set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1223                                                 RNDIS_WLAN_KEY_MGMT_NONE);
1224         set_priv_filter(usbdev);
1225         set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1226 }
1227
1228
1229 static int deauthenticate(struct usbnet *usbdev)
1230 {
1231         int ret;
1232
1233         ret = disassociate(usbdev, 1);
1234         set_default_iw_params(usbdev);
1235         return ret;
1236 }
1237
1238
1239 static int set_channel(struct usbnet *usbdev, int channel)
1240 {
1241         struct ndis_80211_conf config;
1242         unsigned int dsconfig;
1243         int len, ret;
1244
1245         devdbg(usbdev, "set_channel(%d)", channel);
1246
1247         /* this OID is valid only when not associated */
1248         if (is_associated(usbdev))
1249                 return 0;
1250
1251         dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1252
1253         len = sizeof(config);
1254         ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1255         if (ret < 0) {
1256                 devdbg(usbdev, "set_channel: querying configuration failed");
1257                 return ret;
1258         }
1259
1260         config.ds_config = cpu_to_le32(dsconfig);
1261         ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1262                                                                 sizeof(config));
1263
1264         devdbg(usbdev, "set_channel: %d -> %d", channel, ret);
1265
1266         return ret;
1267 }
1268
1269
1270 /* index must be 0 - N, as per NDIS  */
1271 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1272                                                                 int index)
1273 {
1274         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1275         struct ndis_80211_wep_key ndis_key;
1276         u32 cipher;
1277         int ret;
1278
1279         devdbg(usbdev, "add_wep_key(idx: %d, len: %d)", index, key_len);
1280
1281         if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1282                 return -EINVAL;
1283
1284         if (key_len == 5)
1285                 cipher = WLAN_CIPHER_SUITE_WEP40;
1286         else
1287                 cipher = WLAN_CIPHER_SUITE_WEP104;
1288
1289         memset(&ndis_key, 0, sizeof(ndis_key));
1290
1291         ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1292         ndis_key.length = cpu_to_le32(key_len);
1293         ndis_key.index = cpu_to_le32(index);
1294         memcpy(&ndis_key.material, key, key_len);
1295
1296         if (index == priv->encr_tx_key_index) {
1297                 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1298                 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1299                                                         RNDIS_WLAN_ALG_NONE);
1300                 if (ret)
1301                         devwarn(usbdev, "encryption couldn't be enabled (%08X)",
1302                                                                         ret);
1303         }
1304
1305         ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1306                                                         sizeof(ndis_key));
1307         if (ret != 0) {
1308                 devwarn(usbdev, "adding encryption key %d failed (%08X)",
1309                                                         index+1, ret);
1310                 return ret;
1311         }
1312
1313         priv->encr_keys[index].len = key_len;
1314         priv->encr_keys[index].cipher = cipher;
1315         memcpy(&priv->encr_keys[index].material, key, key_len);
1316         memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1317
1318         return 0;
1319 }
1320
1321
1322 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1323                         int index, const u8 *addr, const u8 *rx_seq,
1324                         int seq_len, u32 cipher, int flags)
1325 {
1326         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1327         struct ndis_80211_key ndis_key;
1328         bool is_addr_ok;
1329         int ret;
1330
1331         if (index < 0 || index >= 4) {
1332                 devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
1333                 return -EINVAL;
1334         }
1335         if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1336                 devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
1337                         key_len);
1338                 return -EINVAL;
1339         }
1340         if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1341                 if (!rx_seq || seq_len <= 0) {
1342                         devdbg(usbdev, "add_wpa_key: recv seq flag without"
1343                                         "buffer");
1344                         return -EINVAL;
1345                 }
1346                 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1347                         devdbg(usbdev, "add_wpa_key: too big recv seq buffer");
1348                         return -EINVAL;
1349                 }
1350         }
1351
1352         is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1353                                         !is_broadcast_ether_addr(addr);
1354         if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1355                 devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
1356                         addr);
1357                 return -EINVAL;
1358         }
1359
1360         devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
1361                         !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1362                         !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1363                         !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1364
1365         memset(&ndis_key, 0, sizeof(ndis_key));
1366
1367         ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1368                                 sizeof(ndis_key.material) + key_len);
1369         ndis_key.length = cpu_to_le32(key_len);
1370         ndis_key.index = cpu_to_le32(index) | flags;
1371
1372         if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1373                 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1374                  * different order than NDIS spec, so swap the order here. */
1375                 memcpy(ndis_key.material, key, 16);
1376                 memcpy(ndis_key.material + 16, key + 24, 8);
1377                 memcpy(ndis_key.material + 24, key + 16, 8);
1378         } else
1379                 memcpy(ndis_key.material, key, key_len);
1380
1381         if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1382                 memcpy(ndis_key.rsc, rx_seq, seq_len);
1383
1384         if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1385                 /* pairwise key */
1386                 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1387         } else {
1388                 /* group key */
1389                 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1390                         memset(ndis_key.bssid, 0xff, ETH_ALEN);
1391                 else
1392                         get_bssid(usbdev, ndis_key.bssid);
1393         }
1394
1395         ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1396                                         le32_to_cpu(ndis_key.size));
1397         devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
1398         if (ret != 0)
1399                 return ret;
1400
1401         memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1402         priv->encr_keys[index].len = key_len;
1403         priv->encr_keys[index].cipher = cipher;
1404         memcpy(&priv->encr_keys[index].material, key, key_len);
1405         if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1406                 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1407         else
1408                 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1409
1410         if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1411                 priv->encr_tx_key_index = index;
1412
1413         return 0;
1414 }
1415
1416
1417 static int restore_key(struct usbnet *usbdev, int key_idx)
1418 {
1419         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1420         struct rndis_wlan_encr_key key;
1421
1422         if (is_wpa_key(priv, key_idx))
1423                 return 0;
1424
1425         key = priv->encr_keys[key_idx];
1426
1427         devdbg(usbdev, "restore_key: %i:%i", key_idx, key.len);
1428
1429         if (key.len == 0)
1430                 return 0;
1431
1432         return add_wep_key(usbdev, key.material, key.len, key_idx);
1433 }
1434
1435
1436 static void restore_keys(struct usbnet *usbdev)
1437 {
1438         int i;
1439
1440         for (i = 0; i < 4; i++)
1441                 restore_key(usbdev, i);
1442 }
1443
1444
1445 static void clear_key(struct rndis_wlan_private *priv, int idx)
1446 {
1447         memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1448 }
1449
1450
1451 /* remove_key is for both wep and wpa */
1452 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1453 {
1454         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1455         struct ndis_80211_remove_key remove_key;
1456         __le32 keyindex;
1457         bool is_wpa;
1458         int ret;
1459
1460         if (priv->encr_keys[index].len == 0)
1461                 return 0;
1462
1463         is_wpa = is_wpa_key(priv, index);
1464
1465         devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
1466                 priv->encr_keys[index].len);
1467
1468         clear_key(priv, index);
1469
1470         if (is_wpa) {
1471                 remove_key.size = cpu_to_le32(sizeof(remove_key));
1472                 remove_key.index = cpu_to_le32(index);
1473                 if (bssid) {
1474                         /* pairwise key */
1475                         if (!is_broadcast_ether_addr(bssid))
1476                                 remove_key.index |=
1477                                         NDIS_80211_ADDKEY_PAIRWISE_KEY;
1478                         memcpy(remove_key.bssid, bssid,
1479                                         sizeof(remove_key.bssid));
1480                 } else
1481                         memset(remove_key.bssid, 0xff,
1482                                                 sizeof(remove_key.bssid));
1483
1484                 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1485                                                         sizeof(remove_key));
1486                 if (ret != 0)
1487                         return ret;
1488         } else {
1489                 keyindex = cpu_to_le32(index);
1490                 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1491                                                         sizeof(keyindex));
1492                 if (ret != 0) {
1493                         devwarn(usbdev,
1494                                 "removing encryption key %d failed (%08X)",
1495                                 index, ret);
1496                         return ret;
1497                 }
1498         }
1499
1500         /* if it is transmit key, disable encryption */
1501         if (index == priv->encr_tx_key_index)
1502                 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1503
1504         return 0;
1505 }
1506
1507
1508 static void set_multicast_list(struct usbnet *usbdev)
1509 {
1510         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1511         struct dev_mc_list *mclist;
1512         __le32 filter;
1513         int ret, i, size;
1514         char *buf;
1515
1516         filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1517
1518         if (usbdev->net->flags & IFF_PROMISC) {
1519                 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1520                         RNDIS_PACKET_TYPE_ALL_LOCAL;
1521         } else if (usbdev->net->flags & IFF_ALLMULTI ||
1522                    usbdev->net->mc_count > priv->multicast_size) {
1523                 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1524         } else if (usbdev->net->mc_count > 0) {
1525                 size = min(priv->multicast_size, usbdev->net->mc_count);
1526                 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1527                 if (!buf) {
1528                         devwarn(usbdev,
1529                                 "couldn't alloc %d bytes of memory",
1530                                 size * ETH_ALEN);
1531                         return;
1532                 }
1533
1534                 mclist = usbdev->net->mc_list;
1535                 for (i = 0; i < size && mclist; mclist = mclist->next) {
1536                         if (mclist->dmi_addrlen != ETH_ALEN)
1537                                 continue;
1538
1539                         memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1540                         i++;
1541                 }
1542
1543                 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1544                                                                 i * ETH_ALEN);
1545                 if (ret == 0 && i > 0)
1546                         filter |= RNDIS_PACKET_TYPE_MULTICAST;
1547                 else
1548                         filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1549
1550                 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1551                                                 i, priv->multicast_size, ret);
1552
1553                 kfree(buf);
1554         }
1555
1556         ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1557                                                         sizeof(filter));
1558         if (ret < 0) {
1559                 devwarn(usbdev, "couldn't set packet filter: %08x",
1560                                                         le32_to_cpu(filter));
1561         }
1562
1563         devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1564                                                 le32_to_cpu(filter), ret);
1565 }
1566
1567
1568 /*
1569  * cfg80211 ops
1570  */
1571 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1572                                         struct net_device *dev,
1573                                         enum nl80211_iftype type, u32 *flags,
1574                                         struct vif_params *params)
1575 {
1576         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1577         struct usbnet *usbdev = priv->usbdev;
1578         int mode;
1579
1580         switch (type) {
1581         case NL80211_IFTYPE_ADHOC:
1582                 mode = NDIS_80211_INFRA_ADHOC;
1583                 break;
1584         case NL80211_IFTYPE_STATION:
1585                 mode = NDIS_80211_INFRA_INFRA;
1586                 break;
1587         default:
1588                 return -EINVAL;
1589         }
1590
1591         priv->wdev.iftype = type;
1592
1593         return set_infra_mode(usbdev, mode);
1594 }
1595
1596
1597 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1598 {
1599         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1600         struct usbnet *usbdev = priv->usbdev;
1601         int err;
1602
1603         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1604                 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1605                 if (err < 0)
1606                         return err;
1607         }
1608
1609         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1610                 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1611                 if (err < 0)
1612                         return err;
1613         }
1614
1615         return 0;
1616 }
1617
1618
1619 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
1620                                 int dbm)
1621 {
1622         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1623         struct usbnet *usbdev = priv->usbdev;
1624
1625         devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
1626
1627         /* Device doesn't support changing txpower after initialization, only
1628          * turn off/on radio. Support 'auto' mode and setting same dBm that is
1629          * currently used.
1630          */
1631         if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
1632                 if (!priv->radio_on)
1633                         disassociate(usbdev, 1); /* turn on radio */
1634
1635                 return 0;
1636         }
1637
1638         return -ENOTSUPP;
1639 }
1640
1641
1642 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1643 {
1644         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1645         struct usbnet *usbdev = priv->usbdev;
1646
1647         *dbm = get_bcm4320_power_dbm(priv);
1648
1649         devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
1650
1651         return 0;
1652 }
1653
1654
1655 #define SCAN_DELAY_JIFFIES (6 * HZ)
1656 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1657                         struct cfg80211_scan_request *request)
1658 {
1659         struct usbnet *usbdev = netdev_priv(dev);
1660         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1661         int ret;
1662         __le32 tmp;
1663
1664         devdbg(usbdev, "cfg80211.scan");
1665
1666         /* Get current bssid list from device before new scan, as new scan
1667          * clears internal bssid list.
1668          */
1669         rndis_check_bssid_list(usbdev);
1670
1671         if (!request)
1672                 return -EINVAL;
1673
1674         if (priv->scan_request && priv->scan_request != request)
1675                 return -EBUSY;
1676
1677         priv->scan_request = request;
1678
1679         tmp = cpu_to_le32(1);
1680         ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1681                                                         sizeof(tmp));
1682         if (ret == 0) {
1683                 /* Wait before retrieving scan results from device */
1684                 queue_delayed_work(priv->workqueue, &priv->scan_work,
1685                         SCAN_DELAY_JIFFIES);
1686         }
1687
1688         return ret;
1689 }
1690
1691
1692 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1693                                         struct ndis_80211_bssid_ex *bssid)
1694 {
1695         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1696         struct ieee80211_channel *channel;
1697         s32 signal;
1698         u64 timestamp;
1699         u16 capability;
1700         u16 beacon_interval;
1701         struct ndis_80211_fixed_ies *fixed;
1702         int ie_len, bssid_len;
1703         u8 *ie;
1704
1705         devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
1706                                                         bssid->mac);
1707
1708         /* parse bssid structure */
1709         bssid_len = le32_to_cpu(bssid->length);
1710
1711         if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1712                         sizeof(struct ndis_80211_fixed_ies))
1713                 return NULL;
1714
1715         fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1716
1717         ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1718         ie_len = min(bssid_len - (int)sizeof(*bssid),
1719                                         (int)le32_to_cpu(bssid->ie_length));
1720         ie_len -= sizeof(struct ndis_80211_fixed_ies);
1721         if (ie_len < 0)
1722                 return NULL;
1723
1724         /* extract data for cfg80211_inform_bss */
1725         channel = ieee80211_get_channel(priv->wdev.wiphy,
1726                         KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1727         if (!channel)
1728                 return NULL;
1729
1730         signal = level_to_qual(le32_to_cpu(bssid->rssi));
1731         timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1732         capability = le16_to_cpu(fixed->capabilities);
1733         beacon_interval = le16_to_cpu(fixed->beacon_interval);
1734
1735         return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1736                 timestamp, capability, beacon_interval, ie, ie_len, signal,
1737                 GFP_KERNEL);
1738 }
1739
1740
1741 static int rndis_check_bssid_list(struct usbnet *usbdev)
1742 {
1743         void *buf = NULL;
1744         struct ndis_80211_bssid_list_ex *bssid_list;
1745         struct ndis_80211_bssid_ex *bssid;
1746         int ret = -EINVAL, len, count, bssid_len;
1747         bool resized = false;
1748
1749         devdbg(usbdev, "check_bssid_list");
1750
1751         len = CONTROL_BUFFER_SIZE;
1752 resize_buf:
1753         buf = kmalloc(len, GFP_KERNEL);
1754         if (!buf) {
1755                 ret = -ENOMEM;
1756                 goto out;
1757         }
1758
1759         ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1760         if (ret != 0)
1761                 goto out;
1762
1763         if (!resized && len > CONTROL_BUFFER_SIZE) {
1764                 resized = true;
1765                 kfree(buf);
1766                 goto resize_buf;
1767         }
1768
1769         bssid_list = buf;
1770         bssid = bssid_list->bssid;
1771         bssid_len = le32_to_cpu(bssid->length);
1772         count = le32_to_cpu(bssid_list->num_items);
1773         devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
1774                                                                         len);
1775
1776         while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1777                 rndis_bss_info_update(usbdev, bssid);
1778
1779                 bssid = (void *)bssid + bssid_len;
1780                 bssid_len = le32_to_cpu(bssid->length);
1781                 count--;
1782         }
1783
1784 out:
1785         kfree(buf);
1786         return ret;
1787 }
1788
1789
1790 static void rndis_get_scan_results(struct work_struct *work)
1791 {
1792         struct rndis_wlan_private *priv =
1793                 container_of(work, struct rndis_wlan_private, scan_work.work);
1794         struct usbnet *usbdev = priv->usbdev;
1795         int ret;
1796
1797         devdbg(usbdev, "get_scan_results");
1798
1799         if (!priv->scan_request)
1800                 return;
1801
1802         ret = rndis_check_bssid_list(usbdev);
1803
1804         cfg80211_scan_done(priv->scan_request, ret < 0);
1805
1806         priv->scan_request = NULL;
1807 }
1808
1809 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
1810                                         struct cfg80211_connect_params *sme)
1811 {
1812         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1813         struct usbnet *usbdev = priv->usbdev;
1814         struct ieee80211_channel *channel = sme->channel;
1815         struct ndis_80211_ssid ssid;
1816         int pairwise = RNDIS_WLAN_ALG_NONE;
1817         int groupwise = RNDIS_WLAN_ALG_NONE;
1818         int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
1819         int length, i, ret, chan = -1;
1820
1821         if (channel)
1822                 chan = ieee80211_frequency_to_channel(channel->center_freq);
1823
1824         groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
1825         for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
1826                 pairwise |=
1827                         rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
1828
1829         if (sme->crypto.n_ciphers_pairwise > 0 &&
1830                         pairwise == RNDIS_WLAN_ALG_NONE) {
1831                 deverr(usbdev, "Unsupported pairwise cipher");
1832                 return -ENOTSUPP;
1833         }
1834
1835         for (i = 0; i < sme->crypto.n_akm_suites; i++)
1836                 keymgmt |=
1837                         rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
1838
1839         if (sme->crypto.n_akm_suites > 0 &&
1840                         keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
1841                 deverr(usbdev, "Invalid keymgmt");
1842                 return -ENOTSUPP;
1843         }
1844
1845         devdbg(usbdev, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:"
1846                         "0x%x]:0x%x)", sme->ssid, sme->bssid, chan,
1847                         sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
1848                         groupwise, pairwise, keymgmt);
1849
1850         if (is_associated(usbdev))
1851                 disassociate(usbdev, false);
1852
1853         ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1854         if (ret < 0) {
1855                 devdbg(usbdev, "connect: set_infra_mode failed, %d", ret);
1856                 goto err_turn_radio_on;
1857         }
1858
1859         ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
1860                                                                 keymgmt);
1861         if (ret < 0) {
1862                 devdbg(usbdev, "connect: set_auth_mode failed, %d", ret);
1863                 goto err_turn_radio_on;
1864         }
1865
1866         set_priv_filter(usbdev);
1867
1868         ret = set_encr_mode(usbdev, pairwise, groupwise);
1869         if (ret < 0) {
1870                 devdbg(usbdev, "connect: set_encr_mode failed, %d", ret);
1871                 goto err_turn_radio_on;
1872         }
1873
1874         if (channel) {
1875                 ret = set_channel(usbdev, chan);
1876                 if (ret < 0) {
1877                         devdbg(usbdev, "connect: set_channel failed, %d", ret);
1878                         goto err_turn_radio_on;
1879                 }
1880         }
1881
1882         if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
1883                 priv->encr_tx_key_index = sme->key_idx;
1884                 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
1885                 if (ret < 0) {
1886                         devdbg(usbdev, "connect: add_wep_key failed, %d "
1887                                 "(%d, %d)", ret, sme->key_len, sme->key_idx);
1888                         goto err_turn_radio_on;
1889                 }
1890         }
1891
1892         if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
1893                                 !is_broadcast_ether_addr(sme->bssid)) {
1894                 ret = set_bssid(usbdev, sme->bssid);
1895                 if (ret < 0) {
1896                         devdbg(usbdev, "connect: set_bssid failed, %d", ret);
1897                         goto err_turn_radio_on;
1898                 }
1899         } else
1900                 clear_bssid(usbdev);
1901
1902         length = sme->ssid_len;
1903         if (length > NDIS_802_11_LENGTH_SSID)
1904                 length = NDIS_802_11_LENGTH_SSID;
1905
1906         memset(&ssid, 0, sizeof(ssid));
1907         ssid.length = cpu_to_le32(length);
1908         memcpy(ssid.essid, sme->ssid, length);
1909
1910         /* Pause and purge rx queue, so we don't pass packets before
1911          * 'media connect'-indication.
1912          */
1913         usbnet_pause_rx(usbdev);
1914         usbnet_purge_paused_rxq(usbdev);
1915
1916         ret = set_essid(usbdev, &ssid);
1917         if (ret < 0)
1918                 devdbg(usbdev, "connect: set_essid failed, %d", ret);
1919         return ret;
1920
1921 err_turn_radio_on:
1922         disassociate(usbdev, 1);
1923
1924         return ret;
1925 }
1926
1927 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
1928                                                                 u16 reason_code)
1929 {
1930         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1931         struct usbnet *usbdev = priv->usbdev;
1932
1933         devdbg(usbdev, "cfg80211.disconnect(%d)", reason_code);
1934
1935         priv->connected = false;
1936         memset(priv->bssid, 0, ETH_ALEN);
1937
1938         return deauthenticate(usbdev);
1939 }
1940
1941 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1942                                         struct cfg80211_ibss_params *params)
1943 {
1944         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1945         struct usbnet *usbdev = priv->usbdev;
1946         struct ieee80211_channel *channel = params->channel;
1947         struct ndis_80211_ssid ssid;
1948         enum nl80211_auth_type auth_type;
1949         int ret, alg, length, chan = -1;
1950
1951         if (channel)
1952                 chan = ieee80211_frequency_to_channel(channel->center_freq);
1953
1954         /* TODO: How to handle ad-hoc encryption?
1955          * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
1956          * pre-shared for encryption (open/shared/wpa), is key set before
1957          * join_ibss? Which auth_type to use (not in params)? What about WPA?
1958          */
1959         if (params->privacy) {
1960                 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1961                 alg = RNDIS_WLAN_ALG_WEP;
1962         } else {
1963                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1964                 alg = RNDIS_WLAN_ALG_NONE;
1965         }
1966
1967         devdbg(usbdev, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)", params->ssid,
1968                                         params->bssid, chan, params->privacy);
1969
1970         if (is_associated(usbdev))
1971                 disassociate(usbdev, false);
1972
1973         ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
1974         if (ret < 0) {
1975                 devdbg(usbdev, "join_ibss: set_infra_mode failed, %d", ret);
1976                 goto err_turn_radio_on;
1977         }
1978
1979         ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
1980         if (ret < 0) {
1981                 devdbg(usbdev, "join_ibss: set_auth_mode failed, %d", ret);
1982                 goto err_turn_radio_on;
1983         }
1984
1985         set_priv_filter(usbdev);
1986
1987         ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
1988         if (ret < 0) {
1989                 devdbg(usbdev, "join_ibss: set_encr_mode failed, %d", ret);
1990                 goto err_turn_radio_on;
1991         }
1992
1993         if (channel) {
1994                 ret = set_channel(usbdev, chan);
1995                 if (ret < 0) {
1996                         devdbg(usbdev, "join_ibss: set_channel failed, %d",
1997                                 ret);
1998                         goto err_turn_radio_on;
1999                 }
2000         }
2001
2002         if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2003                                 !is_broadcast_ether_addr(params->bssid)) {
2004                 ret = set_bssid(usbdev, params->bssid);
2005                 if (ret < 0) {
2006                         devdbg(usbdev, "join_ibss: set_bssid failed, %d", ret);
2007                         goto err_turn_radio_on;
2008                 }
2009         } else
2010                 clear_bssid(usbdev);
2011
2012         length = params->ssid_len;
2013         if (length > NDIS_802_11_LENGTH_SSID)
2014                 length = NDIS_802_11_LENGTH_SSID;
2015
2016         memset(&ssid, 0, sizeof(ssid));
2017         ssid.length = cpu_to_le32(length);
2018         memcpy(ssid.essid, params->ssid, length);
2019
2020         /* Don't need to pause rx queue for ad-hoc. */
2021         usbnet_purge_paused_rxq(usbdev);
2022         usbnet_resume_rx(usbdev);
2023
2024         ret = set_essid(usbdev, &ssid);
2025         if (ret < 0)
2026                 devdbg(usbdev, "join_ibss: set_essid failed, %d", ret);
2027         return ret;
2028
2029 err_turn_radio_on:
2030         disassociate(usbdev, 1);
2031
2032         return ret;
2033 }
2034
2035 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2036 {
2037         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2038         struct usbnet *usbdev = priv->usbdev;
2039
2040         devdbg(usbdev, "cfg80211.leave_ibss()");
2041
2042         priv->connected = false;
2043         memset(priv->bssid, 0, ETH_ALEN);
2044
2045         return deauthenticate(usbdev);
2046 }
2047
2048 static int rndis_set_channel(struct wiphy *wiphy,
2049         struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2050 {
2051         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2052         struct usbnet *usbdev = priv->usbdev;
2053
2054         return set_channel(usbdev,
2055                         ieee80211_frequency_to_channel(chan->center_freq));
2056 }
2057
2058 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2059                                         u8 key_index, const u8 *mac_addr,
2060                                         struct key_params *params)
2061 {
2062         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2063         struct usbnet *usbdev = priv->usbdev;
2064         int flags;
2065
2066         devdbg(usbdev, "rndis_add_key(%i, %pM, %08x)", key_index, mac_addr,
2067                                                         params->cipher);
2068
2069         switch (params->cipher) {
2070         case WLAN_CIPHER_SUITE_WEP40:
2071         case WLAN_CIPHER_SUITE_WEP104:
2072                 return add_wep_key(usbdev, params->key, params->key_len,
2073                                                                 key_index);
2074         case WLAN_CIPHER_SUITE_TKIP:
2075         case WLAN_CIPHER_SUITE_CCMP:
2076                 flags = 0;
2077
2078                 if (params->seq && params->seq_len > 0)
2079                         flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2080                 if (mac_addr)
2081                         flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2082                                         NDIS_80211_ADDKEY_TRANSMIT_KEY;
2083
2084                 return add_wpa_key(usbdev, params->key, params->key_len,
2085                                 key_index, mac_addr, params->seq,
2086                                 params->seq_len, params->cipher, flags);
2087         default:
2088                 devdbg(usbdev, "rndis_add_key: unsupported cipher %08x",
2089                                                         params->cipher);
2090                 return -ENOTSUPP;
2091         }
2092 }
2093
2094 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2095                                         u8 key_index, const u8 *mac_addr)
2096 {
2097         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2098         struct usbnet *usbdev = priv->usbdev;
2099
2100         devdbg(usbdev, "rndis_del_key(%i, %pM)", key_index, mac_addr);
2101
2102         return remove_key(usbdev, key_index, mac_addr);
2103 }
2104
2105 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2106                                                                 u8 key_index)
2107 {
2108         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2109         struct usbnet *usbdev = priv->usbdev;
2110         struct rndis_wlan_encr_key key;
2111
2112         devdbg(usbdev, "rndis_set_default_key(%i)", key_index);
2113
2114         priv->encr_tx_key_index = key_index;
2115
2116         key = priv->encr_keys[key_index];
2117
2118         return add_wep_key(usbdev, key.material, key.len, key_index);
2119 }
2120
2121 static void rndis_fill_station_info(struct usbnet *usbdev,
2122                                                 struct station_info *sinfo)
2123 {
2124         __le32 linkspeed, rssi;
2125         int ret, len;
2126
2127         memset(sinfo, 0, sizeof(*sinfo));
2128
2129         len = sizeof(linkspeed);
2130         ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2131         if (ret == 0) {
2132                 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2133                 sinfo->filled |= STATION_INFO_TX_BITRATE;
2134         }
2135
2136         len = sizeof(rssi);
2137         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2138         if (ret == 0) {
2139                 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2140                 sinfo->filled |= STATION_INFO_SIGNAL;
2141         }
2142 }
2143
2144 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2145                                         u8 *mac, struct station_info *sinfo)
2146 {
2147         struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2148         struct usbnet *usbdev = priv->usbdev;
2149
2150         if (compare_ether_addr(priv->bssid, mac))
2151                 return -ENOENT;
2152
2153         rndis_fill_station_info(usbdev, sinfo);
2154
2155         return 0;
2156 }
2157
2158 /*
2159  * wireless extension handlers
2160  */
2161
2162 static int rndis_iw_commit(struct net_device *dev,
2163     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2164 {
2165         /* dummy op */
2166         return 0;
2167 }
2168
2169 #if 0
2170 /* Commented code out instead of removing to have more sane patch for review.
2171  * Will be removed later in the set.
2172  */
2173 static int rndis_iw_set_essid(struct net_device *dev,
2174     struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
2175 {
2176         struct ndis_80211_ssid ssid;
2177         int length = wrqu->essid.length;
2178         struct usbnet *usbdev = netdev_priv(dev);
2179
2180         devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
2181                 wrqu->essid.flags, wrqu->essid.length, essid);
2182
2183         if (length > NDIS_802_11_LENGTH_SSID)
2184                 length = NDIS_802_11_LENGTH_SSID;
2185
2186         ssid.length = cpu_to_le32(length);
2187         if (length > 0)
2188                 memcpy(ssid.essid, essid, length);
2189         else
2190                 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
2191
2192         set_assoc_params(usbdev);
2193
2194         if (!wrqu->essid.flags || length == 0)
2195                 return disassociate(usbdev, 1);
2196         else {
2197                 /* Pause and purge rx queue, so we don't pass packets before
2198                  * 'media connect'-indication.
2199                  */
2200                 usbnet_pause_rx(usbdev);
2201                 usbnet_purge_paused_rxq(usbdev);
2202
2203                 return set_essid(usbdev, &ssid);
2204         }
2205 }
2206
2207
2208 static int rndis_iw_get_essid(struct net_device *dev,
2209     struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
2210 {
2211         struct ndis_80211_ssid ssid;
2212         struct usbnet *usbdev = netdev_priv(dev);
2213         int ret;
2214
2215         ret = get_essid(usbdev, &ssid);
2216
2217         if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
2218                 wrqu->essid.flags = 1;
2219                 wrqu->essid.length = le32_to_cpu(ssid.length);
2220                 memcpy(essid, ssid.essid, wrqu->essid.length);
2221                 essid[wrqu->essid.length] = 0;
2222         } else {
2223                 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
2224                 wrqu->essid.flags = 0;
2225                 wrqu->essid.length = 0;
2226         }
2227         devdbg(usbdev, "SIOCGIWESSID: %s", essid);
2228         return ret;
2229 }
2230
2231
2232 static int rndis_iw_get_bssid(struct net_device *dev,
2233     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2234 {
2235         struct usbnet *usbdev = netdev_priv(dev);
2236         unsigned char bssid[ETH_ALEN];
2237         int ret;
2238
2239         ret = get_bssid(usbdev, bssid);
2240
2241         if (ret == 0)
2242                 devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
2243         else
2244                 devdbg(usbdev, "SIOCGIWAP: <not associated>");
2245
2246         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
2247         memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
2248
2249         return ret;
2250 }
2251
2252
2253 static int rndis_iw_set_bssid(struct net_device *dev,
2254     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2255 {
2256         struct usbnet *usbdev = netdev_priv(dev);
2257         u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
2258         int ret;
2259
2260         devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
2261
2262         ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
2263
2264         /* user apps may set ap's mac address, which is not required;
2265          * they may fail to work if this function fails, so return
2266          * success */
2267         if (ret)
2268                 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
2269
2270         return 0;
2271 }
2272
2273
2274 static int rndis_iw_set_auth(struct net_device *dev,
2275     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2276 {
2277         struct iw_param *p = &wrqu->param;
2278         struct usbnet *usbdev = netdev_priv(dev);
2279         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2280         int ret = -ENOTSUPP;
2281
2282         switch (p->flags & IW_AUTH_INDEX) {
2283         case IW_AUTH_WPA_VERSION:
2284                 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
2285                 priv->wpa_version = p->value;
2286                 ret = 0;
2287                 break;
2288
2289         case IW_AUTH_CIPHER_PAIRWISE:
2290                 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
2291                 priv->wpa_cipher_pair = p->value;
2292                 ret = 0;
2293                 break;
2294
2295         case IW_AUTH_CIPHER_GROUP:
2296                 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
2297                 priv->wpa_cipher_group = p->value;
2298                 ret = 0;
2299                 break;
2300
2301         case IW_AUTH_KEY_MGMT:
2302                 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
2303                 priv->wpa_keymgmt = p->value;
2304                 ret = 0;
2305                 break;
2306
2307         case IW_AUTH_TKIP_COUNTERMEASURES:
2308                 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
2309                                                                 p->value);
2310                 ret = 0;
2311                 break;
2312
2313         case IW_AUTH_DROP_UNENCRYPTED:
2314                 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
2315                 ret = 0;
2316                 break;
2317
2318         case IW_AUTH_80211_AUTH_ALG:
2319                 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
2320                 priv->wpa_authalg = p->value;
2321                 ret = 0;
2322                 break;
2323
2324         case IW_AUTH_WPA_ENABLED:
2325                 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
2326                 if (wrqu->param.value)
2327                         deauthenticate(usbdev);
2328                 ret = 0;
2329                 break;
2330
2331         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
2332                 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
2333                                                                 p->value);
2334                 ret = 0;
2335                 break;
2336
2337         case IW_AUTH_ROAMING_CONTROL:
2338                 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
2339                 ret = 0;
2340                 break;
2341
2342         case IW_AUTH_PRIVACY_INVOKED:
2343                 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
2344                                 wrqu->param.flags & IW_AUTH_INDEX);
2345                 return -EOPNOTSUPP;
2346
2347         default:
2348                 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN  %08x, %08x",
2349                         p->flags & IW_AUTH_INDEX, p->value);
2350         }
2351         return ret;
2352 }
2353
2354
2355 static int rndis_iw_get_auth(struct net_device *dev,
2356     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2357 {
2358         struct iw_param *p = &wrqu->param;
2359         struct usbnet *usbdev = netdev_priv(dev);
2360         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2361
2362         switch (p->flags & IW_AUTH_INDEX) {
2363         case IW_AUTH_WPA_VERSION:
2364                 p->value = priv->wpa_version;
2365                 break;
2366         case IW_AUTH_CIPHER_PAIRWISE:
2367                 p->value = priv->wpa_cipher_pair;
2368                 break;
2369         case IW_AUTH_CIPHER_GROUP:
2370                 p->value = priv->wpa_cipher_group;
2371                 break;
2372         case IW_AUTH_KEY_MGMT:
2373                 p->value = priv->wpa_keymgmt;
2374                 break;
2375         case IW_AUTH_80211_AUTH_ALG:
2376                 p->value = priv->wpa_authalg;
2377                 break;
2378         default:
2379                 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
2380                                 wrqu->param.flags & IW_AUTH_INDEX);
2381                 return -EOPNOTSUPP;
2382         }
2383         return 0;
2384 }
2385
2386
2387 static int rndis_iw_set_encode(struct net_device *dev,
2388     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2389 {
2390         struct usbnet *usbdev = netdev_priv(dev);
2391         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2392         struct rndis_wlan_encr_key key;
2393         int ret, index, key_len;
2394         u8 *keybuf;
2395
2396         index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
2397
2398         /* iwconfig gives index as 1 - N */
2399         if (index > 0)
2400                 index--;
2401         else
2402                 index = priv->encr_tx_key_index;
2403
2404         if (index < 0 || index >= 4) {
2405                 devwarn(usbdev, "encryption index out of range (%u)", index);
2406                 return -EINVAL;
2407         }
2408
2409         /* remove key if disabled */
2410         if (wrqu->data.flags & IW_ENCODE_DISABLED) {
2411                 if (remove_key(usbdev, index, NULL))
2412                         return -EINVAL;
2413                 else
2414                         return 0;
2415         }
2416
2417         /* global encryption state (for all keys) */
2418         if (wrqu->data.flags & IW_ENCODE_OPEN)
2419                 ret = set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
2420                                                 RNDIS_WLAN_KEY_MGMT_NONE);
2421         else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
2422                 ret = set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_SHARED_KEY,
2423                                                 RNDIS_WLAN_KEY_MGMT_NONE);
2424         if (ret != 0)
2425                 return ret;
2426
2427         if (wrqu->data.length > 0) {
2428                 key_len = wrqu->data.length;
2429                 keybuf = extra;
2430         } else {
2431                 /* must be set as tx key */
2432                 if (priv->encr_keys[index].len == 0)
2433                         return -EINVAL;
2434                 key = priv->encr_keys[index];
2435                 key_len = key.len;
2436                 keybuf = key.material;
2437                 priv->encr_tx_key_index = index;
2438         }
2439
2440         if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
2441                 return -EINVAL;
2442
2443         if (index == priv->encr_tx_key_index)
2444                 /* ndis drivers want essid to be set after setting encr */
2445                 set_essid(usbdev, &priv->essid);
2446
2447         return 0;
2448 }
2449
2450
2451 static int rndis_iw_set_encode_ext(struct net_device *dev,
2452     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2453 {
2454         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2455         struct usbnet *usbdev = netdev_priv(dev);
2456         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2457         int keyidx, flags, cipher;
2458
2459         keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
2460
2461         /* iwconfig gives index as 1 - N */
2462         if (keyidx)
2463                 keyidx--;
2464         else
2465                 keyidx = priv->encr_tx_key_index;
2466
2467         if (keyidx < 0 || keyidx >= 4) {
2468                 devwarn(usbdev, "encryption index out of range (%u)", keyidx);
2469                 return -EINVAL;
2470         }
2471
2472         if (ext->alg == IW_ENCODE_ALG_WEP) {
2473                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2474                         priv->encr_tx_key_index = keyidx;
2475                 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
2476         }
2477
2478         cipher = -1;
2479         if (ext->alg == IW_ENCODE_ALG_TKIP)
2480                 cipher = WLAN_CIPHER_SUITE_TKIP;
2481         else if (ext->alg == IW_ENCODE_ALG_CCMP)
2482                 cipher = WLAN_CIPHER_SUITE_CCMP;
2483
2484         if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
2485             ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
2486                 return remove_key(usbdev, keyidx, NULL);
2487
2488         if (cipher == -1)
2489                 return -EOPNOTSUPP;
2490
2491         flags = 0;
2492         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
2493                 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2494         if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
2495                 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
2496         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2497                 flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
2498
2499         return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
2500                                 (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
2501                                 flags);
2502 }
2503
2504
2505 static int rndis_iw_get_rate(struct net_device *dev,
2506     struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2507 {
2508         struct usbnet *usbdev = netdev_priv(dev);
2509         __le32 tmp;
2510         int ret, len;
2511
2512         len = sizeof(tmp);
2513         ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2514         if (ret == 0) {
2515                 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2516                 wrqu->bitrate.disabled = 0;
2517                 wrqu->bitrate.flags = 1;
2518         }
2519         return ret;
2520 }
2521
2522
2523 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2524 {
2525         struct usbnet *usbdev = netdev_priv(dev);
2526         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2527         unsigned long flags;
2528
2529         spin_lock_irqsave(&priv->stats_lock, flags);
2530         memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2531         spin_unlock_irqrestore(&priv->stats_lock, flags);
2532
2533         return &priv->iwstats;
2534 }
2535 #endif
2536
2537
2538 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2539 static const iw_handler rndis_iw_handler[] =
2540 {
2541         IW_IOCTL(SIOCSIWCOMMIT)    = rndis_iw_commit,
2542         IW_IOCTL(SIOCGIWNAME)      = (iw_handler) cfg80211_wext_giwname,
2543         IW_IOCTL(SIOCSIWFREQ)      = (iw_handler) cfg80211_wext_siwfreq,
2544         IW_IOCTL(SIOCGIWFREQ)      = (iw_handler) cfg80211_wext_giwfreq,
2545         IW_IOCTL(SIOCSIWMODE)      = (iw_handler) cfg80211_wext_siwmode,
2546         IW_IOCTL(SIOCGIWMODE)      = (iw_handler) cfg80211_wext_giwmode,
2547         IW_IOCTL(SIOCGIWRANGE)     = (iw_handler) cfg80211_wext_giwrange,
2548         IW_IOCTL(SIOCSIWAP)        = (iw_handler) cfg80211_wext_siwap,
2549         IW_IOCTL(SIOCGIWAP)        = (iw_handler) cfg80211_wext_giwap,
2550         IW_IOCTL(SIOCSIWSCAN)      = (iw_handler) cfg80211_wext_siwscan,
2551         IW_IOCTL(SIOCGIWSCAN)      = (iw_handler) cfg80211_wext_giwscan,
2552         IW_IOCTL(SIOCSIWESSID)     = (iw_handler) cfg80211_wext_siwessid,
2553         IW_IOCTL(SIOCGIWESSID)     = (iw_handler) cfg80211_wext_giwessid,
2554         IW_IOCTL(SIOCGIWRATE)      = (iw_handler) cfg80211_wext_giwrate,
2555         IW_IOCTL(SIOCSIWRTS)       = (iw_handler) cfg80211_wext_siwrts,
2556         IW_IOCTL(SIOCGIWRTS)       = (iw_handler) cfg80211_wext_giwrts,
2557         IW_IOCTL(SIOCSIWFRAG)      = (iw_handler) cfg80211_wext_siwfrag,
2558         IW_IOCTL(SIOCGIWFRAG)      = (iw_handler) cfg80211_wext_giwfrag,
2559         IW_IOCTL(SIOCSIWTXPOW)     = (iw_handler) cfg80211_wext_siwtxpower,
2560         IW_IOCTL(SIOCGIWTXPOW)     = (iw_handler) cfg80211_wext_giwtxpower,
2561         IW_IOCTL(SIOCSIWENCODE)    = (iw_handler) cfg80211_wext_siwencode,
2562         IW_IOCTL(SIOCSIWENCODEEXT) = (iw_handler) cfg80211_wext_siwencodeext,
2563         IW_IOCTL(SIOCSIWAUTH)      = (iw_handler) cfg80211_wext_siwauth,
2564         IW_IOCTL(SIOCGIWAUTH)      = (iw_handler) cfg80211_wext_giwauth,
2565         IW_IOCTL(SIOCSIWGENIE)     = (iw_handler) cfg80211_wext_siwgenie,
2566         IW_IOCTL(SIOCSIWMLME)      = (iw_handler) cfg80211_wext_siwmlme,
2567 };
2568
2569 static const iw_handler rndis_wlan_private_handler[] = {
2570 };
2571
2572 static const struct iw_priv_args rndis_wlan_private_args[] = {
2573 };
2574
2575
2576 static const struct iw_handler_def rndis_iw_handlers = {
2577         .num_standard = ARRAY_SIZE(rndis_iw_handler),
2578         .num_private  = ARRAY_SIZE(rndis_wlan_private_handler),
2579         .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
2580         .standard = (iw_handler *)rndis_iw_handler,
2581         .private  = (iw_handler *)rndis_wlan_private_handler,
2582         .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
2583         .get_wireless_stats = cfg80211_wireless_stats,
2584 };
2585
2586
2587 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2588 {
2589         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2590         struct ndis_80211_assoc_info *info;
2591         u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
2592         u8 bssid[ETH_ALEN];
2593         int resp_ie_len, req_ie_len;
2594         u8 *req_ie, *resp_ie;
2595         int ret, offset;
2596         bool roamed = false;
2597
2598         if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2599                 /* received media connect indication while connected, either
2600                  * device reassociated with same AP or roamed to new. */
2601                 roamed = true;
2602         }
2603
2604         req_ie_len = 0;
2605         resp_ie_len = 0;
2606         req_ie = NULL;
2607         resp_ie = NULL;
2608
2609         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2610                 memset(assoc_buf, 0, sizeof(assoc_buf));
2611                 info = (void *)assoc_buf;
2612
2613                 /* Get association info IEs from device and send them back to
2614                  * userspace. */
2615                 ret = get_association_info(usbdev, info, sizeof(assoc_buf));
2616                 if (!ret) {
2617                         req_ie_len = le32_to_cpu(info->req_ie_length);
2618                         if (req_ie_len > 0) {
2619                                 offset = le32_to_cpu(info->offset_req_ies);
2620                                 req_ie = (u8 *)info + offset;
2621                         }
2622
2623                         resp_ie_len = le32_to_cpu(info->resp_ie_length);
2624                         if (resp_ie_len > 0) {
2625                                 offset = le32_to_cpu(info->offset_resp_ies);
2626                                 resp_ie = (u8 *)info + offset;
2627                         }
2628                 }
2629         } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2630                 return;
2631
2632         ret = get_bssid(usbdev, bssid);
2633         if (ret < 0)
2634                 memset(bssid, 0, sizeof(bssid));
2635
2636         devdbg(usbdev, "link up work: [%pM] %s", bssid, roamed ? "roamed" : "");
2637
2638         /* Internal bss list in device always contains at least the currently
2639          * connected bss and we can get it to cfg80211 with
2640          * rndis_check_bssid_list().
2641          * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2642          * spec.
2643          */
2644         rndis_check_bssid_list(usbdev);
2645
2646         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2647                 if (!roamed)
2648                         cfg80211_connect_result(usbdev->net, bssid, req_ie,
2649                                                 req_ie_len, resp_ie,
2650                                                 resp_ie_len, 0, GFP_KERNEL);
2651                 else
2652                         cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2653                                         resp_ie, resp_ie_len, GFP_KERNEL);
2654         } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2655                 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2656
2657         priv->connected = true;
2658         memcpy(priv->bssid, bssid, ETH_ALEN);
2659
2660         usbnet_resume_rx(usbdev);
2661         netif_carrier_on(usbdev->net);
2662 }
2663
2664 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2665 {
2666         union iwreq_data evt;
2667
2668         netif_carrier_off(usbdev->net);
2669
2670         evt.data.flags = 0;
2671         evt.data.length = 0;
2672         memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2673         wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2674 }
2675
2676 static void rndis_wlan_worker(struct work_struct *work)
2677 {
2678         struct rndis_wlan_private *priv =
2679                 container_of(work, struct rndis_wlan_private, work);
2680         struct usbnet *usbdev = priv->usbdev;
2681
2682         if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2683                 rndis_wlan_do_link_up_work(usbdev);
2684
2685         if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2686                 rndis_wlan_do_link_down_work(usbdev);
2687
2688         if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2689                 set_multicast_list(usbdev);
2690 }
2691
2692 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2693 {
2694         struct usbnet *usbdev = netdev_priv(dev);
2695         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2696
2697         if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2698                 return;
2699
2700         set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2701         queue_work(priv->workqueue, &priv->work);
2702 }
2703
2704
2705 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2706                                 struct ndis_80211_status_indication *indication,
2707                                 int len)
2708 {
2709         u8 *buf;
2710         const char *type;
2711         int flags, buflen;
2712         bool pairwise_error, group_error;
2713         struct ndis_80211_auth_request *auth_req;
2714
2715         /* must have at least one array entry */
2716         if (len < offsetof(struct ndis_80211_status_indication, u) +
2717                                 sizeof(struct ndis_80211_auth_request)) {
2718                 devinfo(usbdev, "authentication indication: "
2719                                 "too short message (%i)", len);
2720                 return;
2721         }
2722
2723         buf = (void *)&indication->u.auth_request[0];
2724         buflen = len - offsetof(struct ndis_80211_status_indication, u);
2725
2726         while (buflen >= sizeof(*auth_req)) {
2727                 auth_req = (void *)buf;
2728                 type = "unknown";
2729                 flags = le32_to_cpu(auth_req->flags);
2730                 pairwise_error = false;
2731                 group_error = false;
2732
2733                 if (flags & 0x1)
2734                         type = "reauth request";
2735                 if (flags & 0x2)
2736                         type = "key update request";
2737                 if (flags & 0x6) {
2738                         pairwise_error = true;
2739                         type = "pairwise_error";
2740                 }
2741                 if (flags & 0xe) {
2742                         group_error = true;
2743                         type = "group_error";
2744                 }
2745
2746                 devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
2747                                 le32_to_cpu(auth_req->flags));
2748
2749                 if (pairwise_error || group_error) {
2750                         union iwreq_data wrqu;
2751                         struct iw_michaelmicfailure micfailure;
2752
2753                         memset(&micfailure, 0, sizeof(micfailure));
2754                         if (pairwise_error)
2755                                 micfailure.flags |= IW_MICFAILURE_PAIRWISE;
2756                         if (group_error)
2757                                 micfailure.flags |= IW_MICFAILURE_GROUP;
2758
2759                         memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
2760                                 ETH_ALEN);
2761
2762                         memset(&wrqu, 0, sizeof(wrqu));
2763                         wrqu.data.length = sizeof(micfailure);
2764                         wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
2765                                                 &wrqu, (u8 *)&micfailure);
2766                 }
2767
2768                 buflen -= le32_to_cpu(auth_req->length);
2769                 buf += le32_to_cpu(auth_req->length);
2770         }
2771 }
2772
2773 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2774                                 struct ndis_80211_status_indication *indication,
2775                                 int len)
2776 {
2777         struct ndis_80211_pmkid_cand_list *cand_list;
2778         int list_len, expected_len, i;
2779
2780         if (len < offsetof(struct ndis_80211_status_indication, u) +
2781                                 sizeof(struct ndis_80211_pmkid_cand_list)) {
2782                 devinfo(usbdev, "pmkid candidate list indication: "
2783                                 "too short message (%i)", len);
2784                 return;
2785         }
2786
2787         list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2788                         sizeof(struct ndis_80211_pmkid_candidate);
2789         expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2790                         offsetof(struct ndis_80211_status_indication, u);
2791
2792         if (len < expected_len) {
2793                 devinfo(usbdev, "pmkid candidate list indication: "
2794                                 "list larger than buffer (%i < %i)",
2795                                 len, expected_len);
2796                 return;
2797         }
2798
2799         cand_list = &indication->u.cand_list;
2800
2801         devinfo(usbdev, "pmkid candidate list indication: "
2802                         "version %i, candidates %i",
2803                         le32_to_cpu(cand_list->version),
2804                         le32_to_cpu(cand_list->num_candidates));
2805
2806         if (le32_to_cpu(cand_list->version) != 1)
2807                 return;
2808
2809         for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2810                 struct iw_pmkid_cand pcand;
2811                 union iwreq_data wrqu;
2812                 struct ndis_80211_pmkid_candidate *cand =
2813                                                 &cand_list->candidate_list[i];
2814
2815                 devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
2816                                 i, le32_to_cpu(cand->flags), cand->bssid);
2817
2818                 memset(&pcand, 0, sizeof(pcand));
2819                 if (le32_to_cpu(cand->flags) & 0x01)
2820                         pcand.flags |= IW_PMKID_CAND_PREAUTH;
2821                 pcand.index = i;
2822                 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2823
2824                 memset(&wrqu, 0, sizeof(wrqu));
2825                 wrqu.data.length = sizeof(pcand);
2826                 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2827                                                                 (u8 *)&pcand);
2828         }
2829 }
2830
2831 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2832                         struct rndis_indicate *msg, int buflen)
2833 {
2834         struct ndis_80211_status_indication *indication;
2835         int len, offset;
2836
2837         offset = offsetof(struct rndis_indicate, status) +
2838                         le32_to_cpu(msg->offset);
2839         len = le32_to_cpu(msg->length);
2840
2841         if (len < 8) {
2842                 devinfo(usbdev, "media specific indication, "
2843                                 "ignore too short message (%i < 8)", len);
2844                 return;
2845         }
2846
2847         if (offset + len > buflen) {
2848                 devinfo(usbdev, "media specific indication, "
2849                                 "too large to fit to buffer (%i > %i)",
2850                                 offset + len, buflen);
2851                 return;
2852         }
2853
2854         indication = (void *)((u8 *)msg + offset);
2855
2856         switch (le32_to_cpu(indication->status_type)) {
2857         case NDIS_80211_STATUSTYPE_RADIOSTATE:
2858                 devinfo(usbdev, "radio state indication: %i",
2859                         le32_to_cpu(indication->u.radio_status));
2860                 return;
2861
2862         case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2863                 devinfo(usbdev, "media stream mode indication: %i",
2864                         le32_to_cpu(indication->u.media_stream_mode));
2865                 return;
2866
2867         case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2868                 rndis_wlan_auth_indication(usbdev, indication, len);
2869                 return;
2870
2871         case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2872                 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2873                 return;
2874
2875         default:
2876                 devinfo(usbdev, "media specific indication: "
2877                                 "unknown status type 0x%08x",
2878                                 le32_to_cpu(indication->status_type));
2879         }
2880 }
2881
2882
2883 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2884 {
2885         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2886         struct rndis_indicate *msg = ind;
2887
2888         switch (msg->status) {
2889         case RNDIS_STATUS_MEDIA_CONNECT:
2890                 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2891                         /* OID_802_11_ADD_KEY causes sometimes extra
2892                          * "media connect" indications which confuses driver
2893                          * and userspace to think that device is
2894                          * roaming/reassociating when it isn't.
2895                          */
2896                         devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
2897                                         "'media connect'");
2898                         return;
2899                 }
2900
2901                 usbnet_pause_rx(usbdev);
2902
2903                 devinfo(usbdev, "media connect");
2904
2905                 /* queue work to avoid recursive calls into rndis_command */
2906                 set_bit(WORK_LINK_UP, &priv->work_pending);
2907                 queue_work(priv->workqueue, &priv->work);
2908                 break;
2909
2910         case RNDIS_STATUS_MEDIA_DISCONNECT:
2911                 devinfo(usbdev, "media disconnect");
2912
2913                 /* queue work to avoid recursive calls into rndis_command */
2914                 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2915                 queue_work(priv->workqueue, &priv->work);
2916                 break;
2917
2918         case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2919                 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2920                 break;
2921
2922         default:
2923                 devinfo(usbdev, "indication: 0x%08x",
2924                                 le32_to_cpu(msg->status));
2925                 break;
2926         }
2927 }
2928
2929
2930 static int rndis_wlan_get_caps(struct usbnet *usbdev)
2931 {
2932         struct {
2933                 __le32  num_items;
2934                 __le32  items[8];
2935         } networks_supported;
2936         int len, retval, i, n;
2937         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2938
2939         /* determine supported modes */
2940         len = sizeof(networks_supported);
2941         retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2942                                                 &networks_supported, &len);
2943         if (retval >= 0) {
2944                 n = le32_to_cpu(networks_supported.num_items);
2945                 if (n > 8)
2946                         n = 8;
2947                 for (i = 0; i < n; i++) {
2948                         switch (le32_to_cpu(networks_supported.items[i])) {
2949                         case NDIS_80211_TYPE_FREQ_HOP:
2950                         case NDIS_80211_TYPE_DIRECT_SEQ:
2951                                 priv->caps |= CAP_MODE_80211B;
2952                                 break;
2953                         case NDIS_80211_TYPE_OFDM_A:
2954                                 priv->caps |= CAP_MODE_80211A;
2955                                 break;
2956                         case NDIS_80211_TYPE_OFDM_G:
2957                                 priv->caps |= CAP_MODE_80211G;
2958                                 break;
2959                         }
2960                 }
2961         }
2962
2963         return retval;
2964 }
2965
2966
2967 #define STATS_UPDATE_JIFFIES (HZ)
2968 static void rndis_update_wireless_stats(struct work_struct *work)
2969 {
2970         struct rndis_wlan_private *priv =
2971                 container_of(work, struct rndis_wlan_private, stats_work.work);
2972         struct usbnet *usbdev = priv->usbdev;
2973         __le32 rssi, tmp;
2974         int len, ret, j;
2975         int update_jiffies = STATS_UPDATE_JIFFIES;
2976         void *buf;
2977
2978         /* Only check/do workaround when connected. Calling is_associated()
2979          * also polls device with rndis_command() and catches for media link
2980          * indications.
2981          */
2982         if (!is_associated(usbdev))
2983                 goto end;
2984
2985         len = sizeof(rssi);
2986         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2987         if (ret == 0)
2988                 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
2989
2990         devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2991                                                         le32_to_cpu(rssi));
2992
2993         /* Workaround transfer stalls on poor quality links.
2994          * TODO: find right way to fix these stalls (as stalls do not happen
2995          * with ndiswrapper/windows driver). */
2996         if (priv->last_qual <= 25) {
2997                 /* Decrease stats worker interval to catch stalls.
2998                  * faster. Faster than 400-500ms causes packet loss,
2999                  * Slower doesn't catch stalls fast enough.
3000                  */
3001                 j = msecs_to_jiffies(priv->param_workaround_interval);
3002                 if (j > STATS_UPDATE_JIFFIES)
3003                         j = STATS_UPDATE_JIFFIES;
3004                 else if (j <= 0)
3005                         j = 1;
3006                 update_jiffies = j;
3007
3008                 /* Send scan OID. Use of both OIDs is required to get device
3009                  * working.
3010                  */
3011                 tmp = cpu_to_le32(1);
3012                 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
3013                                                                 sizeof(tmp));
3014
3015                 len = CONTROL_BUFFER_SIZE;
3016                 buf = kmalloc(len, GFP_KERNEL);
3017                 if (!buf)
3018                         goto end;
3019
3020                 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
3021                 kfree(buf);
3022         }
3023 end:
3024
3025         if (update_jiffies >= HZ)
3026                 update_jiffies = round_jiffies_relative(update_jiffies);
3027         else {
3028                 j = round_jiffies_relative(update_jiffies);
3029                 if (abs(j - update_jiffies) <= 10)
3030                         update_jiffies = j;
3031         }
3032
3033         queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
3034 }
3035
3036
3037 static int bcm4320a_early_init(struct usbnet *usbdev)
3038 {
3039         /* bcm4320a doesn't handle configuration parameters well. Try
3040          * set any and you get partially zeroed mac and broken device.
3041          */
3042
3043         return 0;
3044 }
3045
3046
3047 static int bcm4320b_early_init(struct usbnet *usbdev)
3048 {
3049         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3050         char buf[8];
3051
3052         /* Early initialization settings, setting these won't have effect
3053          * if called after generic_rndis_bind().
3054          */
3055
3056         priv->param_country[0] = modparam_country[0];
3057         priv->param_country[1] = modparam_country[1];
3058         priv->param_country[2] = 0;
3059         priv->param_frameburst   = modparam_frameburst;
3060         priv->param_afterburner  = modparam_afterburner;
3061         priv->param_power_save   = modparam_power_save;
3062         priv->param_power_output = modparam_power_output;
3063         priv->param_roamtrigger  = modparam_roamtrigger;
3064         priv->param_roamdelta    = modparam_roamdelta;
3065
3066         priv->param_country[0] = toupper(priv->param_country[0]);
3067         priv->param_country[1] = toupper(priv->param_country[1]);
3068         /* doesn't support EU as country code, use FI instead */
3069         if (!strcmp(priv->param_country, "EU"))
3070                 strcpy(priv->param_country, "FI");
3071
3072         if (priv->param_power_save < 0)
3073                 priv->param_power_save = 0;
3074         else if (priv->param_power_save > 2)
3075                 priv->param_power_save = 2;
3076
3077         if (priv->param_power_output < 0)
3078                 priv->param_power_output = 0;
3079         else if (priv->param_power_output > 3)
3080                 priv->param_power_output = 3;
3081
3082         if (priv->param_roamtrigger < -80)
3083                 priv->param_roamtrigger = -80;
3084         else if (priv->param_roamtrigger > -60)
3085                 priv->param_roamtrigger = -60;
3086
3087         if (priv->param_roamdelta < 0)
3088                 priv->param_roamdelta = 0;
3089         else if (priv->param_roamdelta > 2)
3090                 priv->param_roamdelta = 2;
3091
3092         if (modparam_workaround_interval < 0)
3093                 priv->param_workaround_interval = 500;
3094         else
3095                 priv->param_workaround_interval = modparam_workaround_interval;
3096
3097         rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3098         rndis_set_config_parameter_str(usbdev, "FrameBursting",
3099                                         priv->param_frameburst ? "1" : "0");
3100         rndis_set_config_parameter_str(usbdev, "Afterburner",
3101                                         priv->param_afterburner ? "1" : "0");
3102         sprintf(buf, "%d", priv->param_power_save);
3103         rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3104         sprintf(buf, "%d", priv->param_power_output);
3105         rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3106         sprintf(buf, "%d", priv->param_roamtrigger);
3107         rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3108         sprintf(buf, "%d", priv->param_roamdelta);
3109         rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3110
3111         return 0;
3112 }
3113
3114 /* same as rndis_netdev_ops but with local multicast handler */
3115 static const struct net_device_ops rndis_wlan_netdev_ops = {
3116         .ndo_open               = usbnet_open,
3117         .ndo_stop               = usbnet_stop,
3118         .ndo_start_xmit         = usbnet_start_xmit,
3119         .ndo_tx_timeout         = usbnet_tx_timeout,
3120         .ndo_set_mac_address    = eth_mac_addr,
3121         .ndo_validate_addr      = eth_validate_addr,
3122         .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3123 };
3124
3125
3126 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3127 {
3128         struct wiphy *wiphy;
3129         struct rndis_wlan_private *priv;
3130         int retval, len;
3131         __le32 tmp;
3132
3133         /* allocate wiphy and rndis private data
3134          * NOTE: We only support a single virtual interface, so wiphy
3135          * and wireless_dev are somewhat synonymous for this device.
3136          */
3137         wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3138         if (!wiphy)
3139                 return -ENOMEM;
3140
3141         priv = wiphy_priv(wiphy);
3142         usbdev->net->ieee80211_ptr = &priv->wdev;
3143         priv->wdev.wiphy = wiphy;
3144         priv->wdev.iftype = NL80211_IFTYPE_STATION;
3145
3146         /* These have to be initialized before calling generic_rndis_bind().
3147          * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3148          */
3149         usbdev->driver_priv = priv;
3150         usbdev->net->wireless_handlers = &rndis_iw_handlers;
3151         priv->usbdev = usbdev;
3152
3153         mutex_init(&priv->command_lock);
3154
3155         /* because rndis_command() sleeps we need to use workqueue */
3156         priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3157         INIT_WORK(&priv->work, rndis_wlan_worker);
3158         INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
3159         INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3160
3161         /* try bind rndis_host */
3162         retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3163         if (retval < 0)
3164                 goto fail;
3165
3166         /* generic_rndis_bind set packet filter to multicast_all+
3167          * promisc mode which doesn't work well for our devices (device
3168          * picks up rssi to closest station instead of to access point).
3169          *
3170          * rndis_host wants to avoid all OID as much as possible
3171          * so do promisc/multicast handling in rndis_wlan.
3172          */
3173         usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3174
3175         tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3176         retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3177                                                                 sizeof(tmp));
3178
3179         len = sizeof(tmp);
3180         retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3181                                                                 &len);
3182         priv->multicast_size = le32_to_cpu(tmp);
3183         if (retval < 0 || priv->multicast_size < 0)
3184                 priv->multicast_size = 0;
3185         if (priv->multicast_size > 0)
3186                 usbdev->net->flags |= IFF_MULTICAST;
3187         else
3188                 usbdev->net->flags &= ~IFF_MULTICAST;
3189
3190         /* fill-out wiphy structure and register w/ cfg80211 */
3191         memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3192         wiphy->privid = rndis_wiphy_privid;
3193         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3194                                         | BIT(NL80211_IFTYPE_ADHOC);
3195         wiphy->max_scan_ssids = 1;
3196
3197         /* TODO: fill-out band/encr information based on priv->caps */
3198         rndis_wlan_get_caps(usbdev);
3199
3200         memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3201         memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3202         priv->band.channels = priv->channels;
3203         priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3204         priv->band.bitrates = priv->rates;
3205         priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3206         wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3207         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3208
3209         memcpy(priv->cipher_suites, rndis_cipher_suites,
3210                                                 sizeof(rndis_cipher_suites));
3211         wiphy->cipher_suites = priv->cipher_suites;
3212         wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3213
3214         set_wiphy_dev(wiphy, &usbdev->udev->dev);
3215
3216         if (wiphy_register(wiphy)) {
3217                 retval = -ENODEV;
3218                 goto fail;
3219         }
3220
3221         set_default_iw_params(usbdev);
3222
3223         /* set default rts/frag */
3224         rndis_set_wiphy_params(wiphy,
3225                         WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3226
3227         /* turn radio on */
3228         priv->radio_on = 1;
3229         disassociate(usbdev, 1);
3230         netif_carrier_off(usbdev->net);
3231
3232         return 0;
3233
3234 fail:
3235         cancel_delayed_work_sync(&priv->stats_work);
3236         cancel_delayed_work_sync(&priv->scan_work);
3237         cancel_work_sync(&priv->work);
3238         flush_workqueue(priv->workqueue);
3239         destroy_workqueue(priv->workqueue);
3240
3241         wiphy_free(wiphy);
3242         return retval;
3243 }
3244
3245
3246 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3247 {
3248         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3249
3250         /* turn radio off */
3251         disassociate(usbdev, 0);
3252
3253         cancel_delayed_work_sync(&priv->stats_work);
3254         cancel_delayed_work_sync(&priv->scan_work);
3255         cancel_work_sync(&priv->work);
3256         flush_workqueue(priv->workqueue);
3257         destroy_workqueue(priv->workqueue);
3258
3259         if (priv && priv->wpa_ie_len)
3260                 kfree(priv->wpa_ie);
3261
3262         rndis_unbind(usbdev, intf);
3263
3264         wiphy_unregister(priv->wdev.wiphy);
3265         wiphy_free(priv->wdev.wiphy);
3266 }
3267
3268
3269 static int rndis_wlan_reset(struct usbnet *usbdev)
3270 {
3271         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3272         int retval;
3273
3274         devdbg(usbdev, "rndis_wlan_reset");
3275
3276         retval = rndis_reset(usbdev);
3277         if (retval)
3278                 devwarn(usbdev, "rndis_reset() failed: %d", retval);
3279
3280         /* rndis_reset cleared multicast list, so restore here.
3281            (set_multicast_list() also turns on current packet filter) */
3282         set_multicast_list(usbdev);
3283
3284         queue_delayed_work(priv->workqueue, &priv->stats_work,
3285                 round_jiffies_relative(STATS_UPDATE_JIFFIES));
3286
3287         return deauthenticate(usbdev);
3288 }
3289
3290
3291 static int rndis_wlan_stop(struct usbnet *usbdev)
3292 {
3293         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3294         int retval;
3295         __le32 filter;
3296
3297         devdbg(usbdev, "rndis_wlan_stop");
3298
3299         retval = disassociate(usbdev, 0);
3300
3301         priv->work_pending = 0;
3302         cancel_delayed_work_sync(&priv->stats_work);
3303         cancel_delayed_work_sync(&priv->scan_work);
3304         cancel_work_sync(&priv->work);
3305         flush_workqueue(priv->workqueue);
3306
3307         if (priv->scan_request) {
3308                 cfg80211_scan_done(priv->scan_request, true);
3309                 priv->scan_request = NULL;
3310         }
3311
3312         /* Set current packet filter zero to block receiving data packets from
3313            device. */
3314         filter = 0;
3315         rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3316                                                                 sizeof(filter));
3317
3318         return retval;
3319 }
3320
3321
3322 static const struct driver_info bcm4320b_info = {
3323         .description =  "Wireless RNDIS device, BCM4320b based",
3324         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3325                                 FLAG_AVOID_UNLINK_URBS,
3326         .bind =         rndis_wlan_bind,
3327         .unbind =       rndis_wlan_unbind,
3328         .status =       rndis_status,
3329         .rx_fixup =     rndis_rx_fixup,
3330         .tx_fixup =     rndis_tx_fixup,
3331         .reset =        rndis_wlan_reset,
3332         .stop =         rndis_wlan_stop,
3333         .early_init =   bcm4320b_early_init,
3334         .indication =   rndis_wlan_indication,
3335 };
3336
3337 static const struct driver_info bcm4320a_info = {
3338         .description =  "Wireless RNDIS device, BCM4320a based",
3339         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3340                                 FLAG_AVOID_UNLINK_URBS,
3341         .bind =         rndis_wlan_bind,
3342         .unbind =       rndis_wlan_unbind,
3343         .status =       rndis_status,
3344         .rx_fixup =     rndis_rx_fixup,
3345         .tx_fixup =     rndis_tx_fixup,
3346         .reset =        rndis_wlan_reset,
3347         .stop =         rndis_wlan_stop,
3348         .early_init =   bcm4320a_early_init,
3349         .indication =   rndis_wlan_indication,
3350 };
3351
3352 static const struct driver_info rndis_wlan_info = {
3353         .description =  "Wireless RNDIS device",
3354         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3355                                 FLAG_AVOID_UNLINK_URBS,
3356         .bind =         rndis_wlan_bind,
3357         .unbind =       rndis_wlan_unbind,
3358         .status =       rndis_status,
3359         .rx_fixup =     rndis_rx_fixup,
3360         .tx_fixup =     rndis_tx_fixup,
3361         .reset =        rndis_wlan_reset,
3362         .stop =         rndis_wlan_stop,
3363         .early_init =   bcm4320a_early_init,
3364         .indication =   rndis_wlan_indication,
3365 };
3366
3367 /*-------------------------------------------------------------------------*/
3368
3369 static const struct usb_device_id products [] = {
3370 #define RNDIS_MASTER_INTERFACE \
3371         .bInterfaceClass        = USB_CLASS_COMM, \
3372         .bInterfaceSubClass     = 2 /* ACM */, \
3373         .bInterfaceProtocol     = 0x0ff
3374
3375 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3376  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3377  */
3378 {
3379         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3380                           | USB_DEVICE_ID_MATCH_DEVICE,
3381         .idVendor               = 0x0411,
3382         .idProduct              = 0x00bc,       /* Buffalo WLI-U2-KG125S */
3383         RNDIS_MASTER_INTERFACE,
3384         .driver_info            = (unsigned long) &bcm4320b_info,
3385 }, {
3386         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3387                           | USB_DEVICE_ID_MATCH_DEVICE,
3388         .idVendor               = 0x0baf,
3389         .idProduct              = 0x011b,       /* U.S. Robotics USR5421 */
3390         RNDIS_MASTER_INTERFACE,
3391         .driver_info            = (unsigned long) &bcm4320b_info,
3392 }, {
3393         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3394                           | USB_DEVICE_ID_MATCH_DEVICE,
3395         .idVendor               = 0x050d,
3396         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3397         RNDIS_MASTER_INTERFACE,
3398         .driver_info            = (unsigned long) &bcm4320b_info,
3399 }, {
3400         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3401                           | USB_DEVICE_ID_MATCH_DEVICE,
3402         .idVendor               = 0x1799,       /* Belkin has two vendor ids */
3403         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3404         RNDIS_MASTER_INTERFACE,
3405         .driver_info            = (unsigned long) &bcm4320b_info,
3406 }, {
3407         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3408                           | USB_DEVICE_ID_MATCH_DEVICE,
3409         .idVendor               = 0x13b1,
3410         .idProduct              = 0x0014,       /* Linksys WUSB54GSv2 */
3411         RNDIS_MASTER_INTERFACE,
3412         .driver_info            = (unsigned long) &bcm4320b_info,
3413 }, {
3414         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3415                           | USB_DEVICE_ID_MATCH_DEVICE,
3416         .idVendor               = 0x13b1,
3417         .idProduct              = 0x0026,       /* Linksys WUSB54GSC */
3418         RNDIS_MASTER_INTERFACE,
3419         .driver_info            = (unsigned long) &bcm4320b_info,
3420 }, {
3421         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3422                           | USB_DEVICE_ID_MATCH_DEVICE,
3423         .idVendor               = 0x0b05,
3424         .idProduct              = 0x1717,       /* Asus WL169gE */
3425         RNDIS_MASTER_INTERFACE,
3426         .driver_info            = (unsigned long) &bcm4320b_info,
3427 }, {
3428         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3429                           | USB_DEVICE_ID_MATCH_DEVICE,
3430         .idVendor               = 0x0a5c,
3431         .idProduct              = 0xd11b,       /* Eminent EM4045 */
3432         RNDIS_MASTER_INTERFACE,
3433         .driver_info            = (unsigned long) &bcm4320b_info,
3434 }, {
3435         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3436                           | USB_DEVICE_ID_MATCH_DEVICE,
3437         .idVendor               = 0x1690,
3438         .idProduct              = 0x0715,       /* BT Voyager 1055 */
3439         RNDIS_MASTER_INTERFACE,
3440         .driver_info            = (unsigned long) &bcm4320b_info,
3441 },
3442 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3443  * parameters available, hardware probably contain older firmware version with
3444  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3445  */
3446 {
3447         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3448                           | USB_DEVICE_ID_MATCH_DEVICE,
3449         .idVendor               = 0x13b1,
3450         .idProduct              = 0x000e,       /* Linksys WUSB54GSv1 */
3451         RNDIS_MASTER_INTERFACE,
3452         .driver_info            = (unsigned long) &bcm4320a_info,
3453 }, {
3454         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3455                           | USB_DEVICE_ID_MATCH_DEVICE,
3456         .idVendor               = 0x0baf,
3457         .idProduct              = 0x0111,       /* U.S. Robotics USR5420 */
3458         RNDIS_MASTER_INTERFACE,
3459         .driver_info            = (unsigned long) &bcm4320a_info,
3460 }, {
3461         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3462                           | USB_DEVICE_ID_MATCH_DEVICE,
3463         .idVendor               = 0x0411,
3464         .idProduct              = 0x004b,       /* BUFFALO WLI-USB-G54 */
3465         RNDIS_MASTER_INTERFACE,
3466         .driver_info            = (unsigned long) &bcm4320a_info,
3467 },
3468 /* Generic Wireless RNDIS devices that we don't have exact
3469  * idVendor/idProduct/chip yet.
3470  */
3471 {
3472         /* RNDIS is MSFT's un-official variant of CDC ACM */
3473         USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3474         .driver_info = (unsigned long) &rndis_wlan_info,
3475 }, {
3476         /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3477         USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3478         .driver_info = (unsigned long) &rndis_wlan_info,
3479 },
3480         { },            // END
3481 };
3482 MODULE_DEVICE_TABLE(usb, products);
3483
3484 static struct usb_driver rndis_wlan_driver = {
3485         .name =         "rndis_wlan",
3486         .id_table =     products,
3487         .probe =        usbnet_probe,
3488         .disconnect =   usbnet_disconnect,
3489         .suspend =      usbnet_suspend,
3490         .resume =       usbnet_resume,
3491 };
3492
3493 static int __init rndis_wlan_init(void)
3494 {
3495         return usb_register(&rndis_wlan_driver);
3496 }
3497 module_init(rndis_wlan_init);
3498
3499 static void __exit rndis_wlan_exit(void)
3500 {
3501         usb_deregister(&rndis_wlan_driver);
3502 }
3503 module_exit(rndis_wlan_exit);
3504
3505 MODULE_AUTHOR("Bjorge Dijkstra");
3506 MODULE_AUTHOR("Jussi Kivilinna");
3507 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3508 MODULE_LICENSE("GPL");
3509