mtd: nand: make Open Firmware device id constant
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / rndis_wlan.c
blob2ecbedb26e152c28e697ac8927be151934ee4144
1 /*
2 * Driver for RNDIS based wireless USB devices.
4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5 * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
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.
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.
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
21 * Portions of this file are based on NDISwrapper project,
22 * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
23 * http://ndiswrapper.sourceforge.net/
26 // #define DEBUG // error path messages, extra info
27 // #define VERBOSE // more; success messages
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>
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");
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)");
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)");
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)");
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%)");
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)");
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)");
86 static int modparam_workaround_interval;
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 (0=disabled) (default: 0)");
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)
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)
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)
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)
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 */
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
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
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)
157 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
158 * slightly modified for datatype endianess, etc
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
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
171 enum ndis_80211_net_infra {
172 NDIS_80211_INFRA_ADHOC,
173 NDIS_80211_INFRA_INFRA,
174 NDIS_80211_INFRA_AUTO_UNKNOWN
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
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
199 enum ndis_80211_priv_filter {
200 NDIS_80211_PRIV_ACCEPT_ALL,
201 NDIS_80211_PRIV_8021X_WEP
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,
211 enum ndis_80211_media_stream_mode {
212 NDIS_80211_MEDIA_STREAM_OFF,
213 NDIS_80211_MEDIA_STREAM_ON
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,
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)
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)
234 struct ndis_80211_auth_request {
235 __le32 length;
236 u8 bssid[6];
237 u8 padding[2];
238 __le32 flags;
239 } __attribute__((packed));
241 struct ndis_80211_pmkid_candidate {
242 u8 bssid[6];
243 u8 padding[2];
244 __le32 flags;
245 } __attribute__((packed));
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));
253 struct ndis_80211_status_indication {
254 __le32 status_type;
255 union {
256 __le32 media_stream_mode;
257 __le32 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));
263 struct ndis_80211_ssid {
264 __le32 length;
265 u8 essid[NDIS_802_11_LENGTH_SSID];
266 } __attribute__((packed));
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));
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));
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));
298 struct ndis_80211_bssid_list_ex {
299 __le32 num_items;
300 struct ndis_80211_bssid_ex bssid[0];
301 } __attribute__((packed));
303 struct ndis_80211_fixed_ies {
304 u8 timestamp[8];
305 __le16 beacon_interval;
306 __le16 capabilities;
307 } __attribute__((packed));
309 struct ndis_80211_wep_key {
310 __le32 size;
311 __le32 index;
312 __le32 length;
313 u8 material[32];
314 } __attribute__((packed));
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));
326 struct ndis_80211_remove_key {
327 __le32 size;
328 __le32 index;
329 u8 bssid[6];
330 u8 padding[2];
331 } __attribute__((packed));
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));
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));
362 * private data
364 #define NET_TYPE_11FB 0
366 #define CAP_MODE_80211A 1
367 #define CAP_MODE_80211B 2
368 #define CAP_MODE_80211G 4
369 #define CAP_MODE_MASK 7
371 #define WORK_LINK_UP (1<<0)
372 #define WORK_LINK_DOWN (1<<1)
373 #define WORK_SET_MULTICAST_LIST (1<<2)
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)
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)
384 #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
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 },
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 }
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,
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;
434 /* RNDIS device private data */
435 struct rndis_wlan_private {
436 struct usbnet *usbdev;
438 struct wireless_dev wdev;
440 struct cfg80211_scan_request *scan_request;
442 struct workqueue_struct *workqueue;
443 struct delayed_work dev_poller_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;
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)];
455 int caps;
456 int multicast_size;
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;
468 /* hardware state */
469 bool 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;
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;
487 u8 command_buffer[COMMAND_BUFFER_SIZE];
491 * cfg80211 ops
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);
498 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
499 struct cfg80211_scan_request *request);
501 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
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);
507 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
508 struct cfg80211_connect_params *sme);
510 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
511 u16 reason_code);
513 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
514 struct cfg80211_ibss_params *params);
516 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
518 static int rndis_set_channel(struct wiphy *wiphy,
519 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
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);
525 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
526 u8 key_index, const u8 *mac_addr);
528 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
529 u8 key_index);
531 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
532 u8 *mac, struct station_info *sinfo);
534 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
535 int idx, u8 *mac, struct station_info *sinfo);
537 static struct cfg80211_ops rndis_config_ops = {
538 .change_virtual_intf = rndis_change_virtual_intf,
539 .scan = rndis_scan,
540 .set_wiphy_params = rndis_set_wiphy_params,
541 .set_tx_power = rndis_set_tx_power,
542 .get_tx_power = rndis_get_tx_power,
543 .connect = rndis_connect,
544 .disconnect = rndis_disconnect,
545 .join_ibss = rndis_join_ibss,
546 .leave_ibss = rndis_leave_ibss,
547 .set_channel = rndis_set_channel,
548 .add_key = rndis_add_key,
549 .del_key = rndis_del_key,
550 .set_default_key = rndis_set_default_key,
551 .get_station = rndis_get_station,
552 .dump_station = rndis_dump_station,
555 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
558 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
560 return (struct rndis_wlan_private *)dev->driver_priv;
563 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
565 switch (priv->param_power_output) {
566 default:
567 case 3:
568 return BCM4320_DEFAULT_TXPOWER_DBM_100;
569 case 2:
570 return BCM4320_DEFAULT_TXPOWER_DBM_75;
571 case 1:
572 return BCM4320_DEFAULT_TXPOWER_DBM_50;
573 case 0:
574 return BCM4320_DEFAULT_TXPOWER_DBM_25;
578 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
580 int cipher = priv->encr_keys[idx].cipher;
582 return (cipher == WLAN_CIPHER_SUITE_CCMP ||
583 cipher == WLAN_CIPHER_SUITE_TKIP);
586 static int rndis_cipher_to_alg(u32 cipher)
588 switch (cipher) {
589 default:
590 return RNDIS_WLAN_ALG_NONE;
591 case WLAN_CIPHER_SUITE_WEP40:
592 case WLAN_CIPHER_SUITE_WEP104:
593 return RNDIS_WLAN_ALG_WEP;
594 case WLAN_CIPHER_SUITE_TKIP:
595 return RNDIS_WLAN_ALG_TKIP;
596 case WLAN_CIPHER_SUITE_CCMP:
597 return RNDIS_WLAN_ALG_CCMP;
601 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
603 switch (akm_suite) {
604 default:
605 return RNDIS_WLAN_KEY_MGMT_NONE;
606 case WLAN_AKM_SUITE_8021X:
607 return RNDIS_WLAN_KEY_MGMT_802_1X;
608 case WLAN_AKM_SUITE_PSK:
609 return RNDIS_WLAN_KEY_MGMT_PSK;
613 #ifdef DEBUG
614 static const char *oid_to_string(__le32 oid)
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);
624 /* from rndis_wlan.c */
625 OID_STR(OID_GEN_LINK_SPEED);
626 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
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);
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);
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
665 return "?";
667 #else
668 static const char *oid_to_string(__le32 oid)
670 return "?";
672 #endif
674 /* translate error code */
675 static int rndis_error_status(__le32 rndis_status)
677 int ret = -EINVAL;
678 switch (rndis_status) {
679 case RNDIS_STATUS_SUCCESS:
680 ret = 0;
681 break;
682 case RNDIS_STATUS_FAILURE:
683 case RNDIS_STATUS_INVALID_DATA:
684 ret = -EINVAL;
685 break;
686 case RNDIS_STATUS_NOT_SUPPORTED:
687 ret = -EOPNOTSUPP;
688 break;
689 case RNDIS_STATUS_ADAPTER_NOT_READY:
690 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
691 ret = -EBUSY;
692 break;
694 return ret;
697 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
699 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
700 union {
701 void *buf;
702 struct rndis_msg_hdr *header;
703 struct rndis_query *get;
704 struct rndis_query_c *get_c;
705 } u;
706 int ret, buflen;
708 buflen = *len + sizeof(*u.get);
709 if (buflen < CONTROL_BUFFER_SIZE)
710 buflen = CONTROL_BUFFER_SIZE;
712 if (buflen > COMMAND_BUFFER_SIZE) {
713 u.buf = kmalloc(buflen, GFP_KERNEL);
714 if (!u.buf)
715 return -ENOMEM;
716 } else {
717 u.buf = priv->command_buffer;
720 mutex_lock(&priv->command_lock);
722 memset(u.get, 0, sizeof *u.get);
723 u.get->msg_type = RNDIS_MSG_QUERY;
724 u.get->msg_len = cpu_to_le32(sizeof *u.get);
725 u.get->oid = oid;
727 priv->current_command_oid = oid;
728 ret = rndis_command(dev, u.header, buflen);
729 priv->current_command_oid = 0;
730 if (ret < 0)
731 devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
732 "(%08x)", oid_to_string(oid), ret,
733 le32_to_cpu(u.get_c->status));
735 if (ret == 0) {
736 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
738 ret = le32_to_cpu(u.get_c->len);
739 if (ret > *len)
740 *len = ret;
742 ret = rndis_error_status(u.get_c->status);
743 if (ret < 0)
744 devdbg(dev, "rndis_query_oid(%s): device returned "
745 "error, 0x%08x (%d)", oid_to_string(oid),
746 le32_to_cpu(u.get_c->status), ret);
749 mutex_unlock(&priv->command_lock);
751 if (u.buf != priv->command_buffer)
752 kfree(u.buf);
753 return ret;
756 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
758 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
759 union {
760 void *buf;
761 struct rndis_msg_hdr *header;
762 struct rndis_set *set;
763 struct rndis_set_c *set_c;
764 } u;
765 int ret, buflen;
767 buflen = len + sizeof(*u.set);
768 if (buflen < CONTROL_BUFFER_SIZE)
769 buflen = CONTROL_BUFFER_SIZE;
771 if (buflen > COMMAND_BUFFER_SIZE) {
772 u.buf = kmalloc(buflen, GFP_KERNEL);
773 if (!u.buf)
774 return -ENOMEM;
775 } else {
776 u.buf = priv->command_buffer;
779 mutex_lock(&priv->command_lock);
781 memset(u.set, 0, sizeof *u.set);
782 u.set->msg_type = RNDIS_MSG_SET;
783 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
784 u.set->oid = oid;
785 u.set->len = cpu_to_le32(len);
786 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
787 u.set->handle = cpu_to_le32(0);
788 memcpy(u.buf + sizeof(*u.set), data, len);
790 priv->current_command_oid = oid;
791 ret = rndis_command(dev, u.header, buflen);
792 priv->current_command_oid = 0;
793 if (ret < 0)
794 devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
795 "(%08x)", oid_to_string(oid), ret,
796 le32_to_cpu(u.set_c->status));
798 if (ret == 0) {
799 ret = rndis_error_status(u.set_c->status);
801 if (ret < 0)
802 devdbg(dev, "rndis_set_oid(%s): device returned error, "
803 "0x%08x (%d)", oid_to_string(oid),
804 le32_to_cpu(u.set_c->status), ret);
807 mutex_unlock(&priv->command_lock);
809 if (u.buf != priv->command_buffer)
810 kfree(u.buf);
811 return ret;
814 static int rndis_reset(struct usbnet *usbdev)
816 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
817 struct rndis_reset *reset;
818 int ret;
820 mutex_lock(&priv->command_lock);
822 reset = (void *)priv->command_buffer;
823 memset(reset, 0, sizeof(*reset));
824 reset->msg_type = RNDIS_MSG_RESET;
825 reset->msg_len = cpu_to_le32(sizeof(*reset));
826 priv->current_command_oid = 0;
827 ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
829 mutex_unlock(&priv->command_lock);
831 if (ret < 0)
832 return ret;
833 return 0;
837 * Specs say that we can only set config parameters only soon after device
838 * initialization.
839 * value_type: 0 = u32, 2 = unicode string
841 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
842 int value_type, void *value)
844 struct ndis_config_param *infobuf;
845 int value_len, info_len, param_len, ret, i;
846 __le16 *unibuf;
847 __le32 *dst_value;
849 if (value_type == 0)
850 value_len = sizeof(__le32);
851 else if (value_type == 2)
852 value_len = strlen(value) * sizeof(__le16);
853 else
854 return -EINVAL;
856 param_len = strlen(param) * sizeof(__le16);
857 info_len = sizeof(*infobuf) + param_len + value_len;
859 #ifdef DEBUG
860 info_len += 12;
861 #endif
862 infobuf = kmalloc(info_len, GFP_KERNEL);
863 if (!infobuf)
864 return -ENOMEM;
866 #ifdef DEBUG
867 info_len -= 12;
868 /* extra 12 bytes are for padding (debug output) */
869 memset(infobuf, 0xCC, info_len + 12);
870 #endif
872 if (value_type == 2)
873 devdbg(dev, "setting config parameter: %s, value: %s",
874 param, (u8 *)value);
875 else
876 devdbg(dev, "setting config parameter: %s, value: %d",
877 param, *(u32 *)value);
879 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
880 infobuf->name_length = cpu_to_le32(param_len);
881 infobuf->type = cpu_to_le32(value_type);
882 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
883 infobuf->value_length = cpu_to_le32(value_len);
885 /* simple string to unicode string conversion */
886 unibuf = (void *)infobuf + sizeof(*infobuf);
887 for (i = 0; i < param_len / sizeof(__le16); i++)
888 unibuf[i] = cpu_to_le16(param[i]);
890 if (value_type == 2) {
891 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
892 for (i = 0; i < value_len / sizeof(__le16); i++)
893 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
894 } else {
895 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
896 *dst_value = cpu_to_le32(*(u32 *)value);
899 #ifdef DEBUG
900 devdbg(dev, "info buffer (len: %d):", info_len);
901 for (i = 0; i < info_len; i += 12) {
902 u32 *tmp = (u32 *)((u8 *)infobuf + i);
903 devdbg(dev, "%08X:%08X:%08X",
904 cpu_to_be32(tmp[0]),
905 cpu_to_be32(tmp[1]),
906 cpu_to_be32(tmp[2]));
908 #endif
910 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
911 infobuf, info_len);
912 if (ret != 0)
913 devdbg(dev, "setting rndis config parameter failed, %d.", ret);
915 kfree(infobuf);
916 return ret;
919 static int rndis_set_config_parameter_str(struct usbnet *dev,
920 char *param, char *value)
922 return rndis_set_config_parameter(dev, param, 2, value);
926 * data conversion functions
928 static int level_to_qual(int level)
930 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
931 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
935 * common functions
937 static int set_infra_mode(struct usbnet *usbdev, int mode);
938 static void restore_keys(struct usbnet *usbdev);
939 static int rndis_check_bssid_list(struct usbnet *usbdev);
941 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
943 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
944 int ret;
946 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
947 if (ret < 0) {
948 devwarn(usbdev, "setting SSID failed (%08X)", ret);
949 return ret;
951 if (ret == 0) {
952 memcpy(&priv->essid, ssid, sizeof(priv->essid));
953 priv->radio_on = true;
954 devdbg(usbdev, "set_essid: radio_on = true");
957 return ret;
960 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
962 int ret;
964 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
965 if (ret < 0) {
966 devwarn(usbdev, "setting BSSID[%pM] failed (%08X)", bssid, ret);
967 return ret;
970 return ret;
973 static int clear_bssid(struct usbnet *usbdev)
975 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
977 return set_bssid(usbdev, broadcast_mac);
980 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
982 int ret, len;
984 len = ETH_ALEN;
985 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
987 if (ret != 0)
988 memset(bssid, 0, ETH_ALEN);
990 return ret;
993 static int get_association_info(struct usbnet *usbdev,
994 struct ndis_80211_assoc_info *info, int len)
996 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
997 info, &len);
1000 static bool is_associated(struct usbnet *usbdev)
1002 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1003 u8 bssid[ETH_ALEN];
1004 int ret;
1006 if (!priv->radio_on)
1007 return false;
1009 ret = get_bssid(usbdev, bssid);
1011 return (ret == 0 && !is_zero_ether_addr(bssid));
1014 static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1016 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1017 struct ndis_80211_ssid ssid;
1018 int i, ret = 0;
1020 if (priv->radio_on) {
1021 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1022 if (ret == 0) {
1023 priv->radio_on = false;
1024 devdbg(usbdev, "disassociate: radio_on = false");
1026 if (reset_ssid)
1027 msleep(100);
1031 /* disassociate causes radio to be turned off; if reset_ssid
1032 * is given, set random ssid to enable radio */
1033 if (reset_ssid) {
1034 /* Set device to infrastructure mode so we don't get ad-hoc
1035 * 'media connect' indications with the random ssid.
1037 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1039 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1040 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1041 ssid.essid[0] = 0x1;
1042 ssid.essid[1] = 0xff;
1043 for (i = 2; i < sizeof(ssid.essid); i++)
1044 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1045 ret = set_essid(usbdev, &ssid);
1047 return ret;
1050 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1051 enum nl80211_auth_type auth_type, int keymgmt)
1053 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1054 __le32 tmp;
1055 int auth_mode, ret;
1057 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
1058 "keymgmt=0x%x", wpa_version, auth_type, keymgmt);
1060 if (wpa_version & NL80211_WPA_VERSION_2) {
1061 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1062 auth_mode = NDIS_80211_AUTH_WPA2;
1063 else
1064 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1065 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1066 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1067 auth_mode = NDIS_80211_AUTH_WPA;
1068 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1069 auth_mode = NDIS_80211_AUTH_WPA_PSK;
1070 else
1071 auth_mode = NDIS_80211_AUTH_WPA_NONE;
1072 } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1073 auth_mode = NDIS_80211_AUTH_SHARED;
1074 else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1075 auth_mode = NDIS_80211_AUTH_OPEN;
1076 else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1077 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1078 else
1079 return -ENOTSUPP;
1081 tmp = cpu_to_le32(auth_mode);
1082 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1083 sizeof(tmp));
1084 if (ret != 0) {
1085 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
1086 return ret;
1089 priv->wpa_version = wpa_version;
1090 priv->wpa_auth_type = auth_type;
1091 priv->wpa_keymgmt = keymgmt;
1093 return 0;
1096 static int set_priv_filter(struct usbnet *usbdev)
1098 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1099 __le32 tmp;
1101 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
1103 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1104 priv->wpa_version & NL80211_WPA_VERSION_1)
1105 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1106 else
1107 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1109 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1110 sizeof(tmp));
1113 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1115 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1116 __le32 tmp;
1117 int encr_mode, ret;
1119 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
1120 pairwise, groupwise);
1122 if (pairwise & RNDIS_WLAN_ALG_CCMP)
1123 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1124 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1125 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1126 else if (pairwise & RNDIS_WLAN_ALG_WEP)
1127 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1128 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1129 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1130 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1131 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1132 else
1133 encr_mode = NDIS_80211_ENCR_DISABLED;
1135 tmp = cpu_to_le32(encr_mode);
1136 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1137 sizeof(tmp));
1138 if (ret != 0) {
1139 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
1140 return ret;
1143 priv->wpa_cipher_pair = pairwise;
1144 priv->wpa_cipher_group = groupwise;
1145 return 0;
1148 static int set_infra_mode(struct usbnet *usbdev, int mode)
1150 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1151 __le32 tmp;
1152 int ret;
1154 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
1156 tmp = cpu_to_le32(mode);
1157 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1158 sizeof(tmp));
1159 if (ret != 0) {
1160 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
1161 return ret;
1164 /* NDIS drivers clear keys when infrastructure mode is
1165 * changed. But Linux tools assume otherwise. So set the
1166 * keys */
1167 restore_keys(usbdev);
1169 priv->infra_mode = mode;
1170 return 0;
1173 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1175 __le32 tmp;
1177 devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
1179 if (rts_threshold < 0 || rts_threshold > 2347)
1180 rts_threshold = 2347;
1182 tmp = cpu_to_le32(rts_threshold);
1183 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1184 sizeof(tmp));
1187 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1189 __le32 tmp;
1191 devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
1193 if (frag_threshold < 256 || frag_threshold > 2346)
1194 frag_threshold = 2346;
1196 tmp = cpu_to_le32(frag_threshold);
1197 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1198 sizeof(tmp));
1201 static void set_default_iw_params(struct usbnet *usbdev)
1203 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1204 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1205 RNDIS_WLAN_KEY_MGMT_NONE);
1206 set_priv_filter(usbdev);
1207 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1210 static int deauthenticate(struct usbnet *usbdev)
1212 int ret;
1214 ret = disassociate(usbdev, true);
1215 set_default_iw_params(usbdev);
1216 return ret;
1219 static int set_channel(struct usbnet *usbdev, int channel)
1221 struct ndis_80211_conf config;
1222 unsigned int dsconfig;
1223 int len, ret;
1225 devdbg(usbdev, "set_channel(%d)", channel);
1227 /* this OID is valid only when not associated */
1228 if (is_associated(usbdev))
1229 return 0;
1231 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1233 len = sizeof(config);
1234 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1235 if (ret < 0) {
1236 devdbg(usbdev, "set_channel: querying configuration failed");
1237 return ret;
1240 config.ds_config = cpu_to_le32(dsconfig);
1241 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1242 sizeof(config));
1244 devdbg(usbdev, "set_channel: %d -> %d", channel, ret);
1246 return ret;
1249 /* index must be 0 - N, as per NDIS */
1250 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1251 int index)
1253 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1254 struct ndis_80211_wep_key ndis_key;
1255 u32 cipher;
1256 int ret;
1258 devdbg(usbdev, "add_wep_key(idx: %d, len: %d)", index, key_len);
1260 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1261 return -EINVAL;
1263 if (key_len == 5)
1264 cipher = WLAN_CIPHER_SUITE_WEP40;
1265 else
1266 cipher = WLAN_CIPHER_SUITE_WEP104;
1268 memset(&ndis_key, 0, sizeof(ndis_key));
1270 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1271 ndis_key.length = cpu_to_le32(key_len);
1272 ndis_key.index = cpu_to_le32(index);
1273 memcpy(&ndis_key.material, key, key_len);
1275 if (index == priv->encr_tx_key_index) {
1276 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1277 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1278 RNDIS_WLAN_ALG_NONE);
1279 if (ret)
1280 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
1281 ret);
1284 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1285 sizeof(ndis_key));
1286 if (ret != 0) {
1287 devwarn(usbdev, "adding encryption key %d failed (%08X)",
1288 index+1, ret);
1289 return ret;
1292 priv->encr_keys[index].len = key_len;
1293 priv->encr_keys[index].cipher = cipher;
1294 memcpy(&priv->encr_keys[index].material, key, key_len);
1295 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1297 return 0;
1300 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1301 int index, const u8 *addr, const u8 *rx_seq,
1302 int seq_len, u32 cipher, __le32 flags)
1304 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1305 struct ndis_80211_key ndis_key;
1306 bool is_addr_ok;
1307 int ret;
1309 if (index < 0 || index >= 4) {
1310 devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
1311 return -EINVAL;
1313 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1314 devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
1315 key_len);
1316 return -EINVAL;
1318 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1319 if (!rx_seq || seq_len <= 0) {
1320 devdbg(usbdev, "add_wpa_key: recv seq flag without"
1321 "buffer");
1322 return -EINVAL;
1324 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1325 devdbg(usbdev, "add_wpa_key: too big recv seq buffer");
1326 return -EINVAL;
1330 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1331 !is_broadcast_ether_addr(addr);
1332 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1333 devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
1334 addr);
1335 return -EINVAL;
1338 devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
1339 !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1340 !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1341 !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1343 memset(&ndis_key, 0, sizeof(ndis_key));
1345 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1346 sizeof(ndis_key.material) + key_len);
1347 ndis_key.length = cpu_to_le32(key_len);
1348 ndis_key.index = cpu_to_le32(index) | flags;
1350 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1351 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1352 * different order than NDIS spec, so swap the order here. */
1353 memcpy(ndis_key.material, key, 16);
1354 memcpy(ndis_key.material + 16, key + 24, 8);
1355 memcpy(ndis_key.material + 24, key + 16, 8);
1356 } else
1357 memcpy(ndis_key.material, key, key_len);
1359 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1360 memcpy(ndis_key.rsc, rx_seq, seq_len);
1362 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1363 /* pairwise key */
1364 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1365 } else {
1366 /* group key */
1367 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1368 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1369 else
1370 get_bssid(usbdev, ndis_key.bssid);
1373 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1374 le32_to_cpu(ndis_key.size));
1375 devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
1376 if (ret != 0)
1377 return ret;
1379 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1380 priv->encr_keys[index].len = key_len;
1381 priv->encr_keys[index].cipher = cipher;
1382 memcpy(&priv->encr_keys[index].material, key, key_len);
1383 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1384 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1385 else
1386 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1388 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1389 priv->encr_tx_key_index = index;
1391 return 0;
1394 static int restore_key(struct usbnet *usbdev, int key_idx)
1396 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1397 struct rndis_wlan_encr_key key;
1399 if (is_wpa_key(priv, key_idx))
1400 return 0;
1402 key = priv->encr_keys[key_idx];
1404 devdbg(usbdev, "restore_key: %i:%i", key_idx, key.len);
1406 if (key.len == 0)
1407 return 0;
1409 return add_wep_key(usbdev, key.material, key.len, key_idx);
1412 static void restore_keys(struct usbnet *usbdev)
1414 int i;
1416 for (i = 0; i < 4; i++)
1417 restore_key(usbdev, i);
1420 static void clear_key(struct rndis_wlan_private *priv, int idx)
1422 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1425 /* remove_key is for both wep and wpa */
1426 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1428 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1429 struct ndis_80211_remove_key remove_key;
1430 __le32 keyindex;
1431 bool is_wpa;
1432 int ret;
1434 if (priv->encr_keys[index].len == 0)
1435 return 0;
1437 is_wpa = is_wpa_key(priv, index);
1439 devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
1440 priv->encr_keys[index].len);
1442 clear_key(priv, index);
1444 if (is_wpa) {
1445 remove_key.size = cpu_to_le32(sizeof(remove_key));
1446 remove_key.index = cpu_to_le32(index);
1447 if (bssid) {
1448 /* pairwise key */
1449 if (!is_broadcast_ether_addr(bssid))
1450 remove_key.index |=
1451 NDIS_80211_ADDKEY_PAIRWISE_KEY;
1452 memcpy(remove_key.bssid, bssid,
1453 sizeof(remove_key.bssid));
1454 } else
1455 memset(remove_key.bssid, 0xff,
1456 sizeof(remove_key.bssid));
1458 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1459 sizeof(remove_key));
1460 if (ret != 0)
1461 return ret;
1462 } else {
1463 keyindex = cpu_to_le32(index);
1464 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1465 sizeof(keyindex));
1466 if (ret != 0) {
1467 devwarn(usbdev,
1468 "removing encryption key %d failed (%08X)",
1469 index, ret);
1470 return ret;
1474 /* if it is transmit key, disable encryption */
1475 if (index == priv->encr_tx_key_index)
1476 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1478 return 0;
1481 static void set_multicast_list(struct usbnet *usbdev)
1483 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1484 struct dev_mc_list *mclist;
1485 __le32 filter;
1486 int ret, i, size;
1487 char *buf;
1489 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1491 if (usbdev->net->flags & IFF_PROMISC) {
1492 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1493 RNDIS_PACKET_TYPE_ALL_LOCAL;
1494 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1495 usbdev->net->mc_count > priv->multicast_size) {
1496 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1497 } else if (usbdev->net->mc_count > 0) {
1498 size = min(priv->multicast_size, usbdev->net->mc_count);
1499 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1500 if (!buf) {
1501 devwarn(usbdev,
1502 "couldn't alloc %d bytes of memory",
1503 size * ETH_ALEN);
1504 return;
1507 mclist = usbdev->net->mc_list;
1508 for (i = 0; i < size && mclist; mclist = mclist->next) {
1509 if (mclist->dmi_addrlen != ETH_ALEN)
1510 continue;
1512 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1513 i++;
1516 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1517 i * ETH_ALEN);
1518 if (ret == 0 && i > 0)
1519 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1520 else
1521 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1523 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1524 i, priv->multicast_size, ret);
1526 kfree(buf);
1529 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1530 sizeof(filter));
1531 if (ret < 0) {
1532 devwarn(usbdev, "couldn't set packet filter: %08x",
1533 le32_to_cpu(filter));
1536 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1537 le32_to_cpu(filter), ret);
1541 * cfg80211 ops
1543 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1544 struct net_device *dev,
1545 enum nl80211_iftype type, u32 *flags,
1546 struct vif_params *params)
1548 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1549 struct usbnet *usbdev = priv->usbdev;
1550 int mode;
1552 switch (type) {
1553 case NL80211_IFTYPE_ADHOC:
1554 mode = NDIS_80211_INFRA_ADHOC;
1555 break;
1556 case NL80211_IFTYPE_STATION:
1557 mode = NDIS_80211_INFRA_INFRA;
1558 break;
1559 default:
1560 return -EINVAL;
1563 priv->wdev.iftype = type;
1565 return set_infra_mode(usbdev, mode);
1568 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1570 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1571 struct usbnet *usbdev = priv->usbdev;
1572 int err;
1574 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1575 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1576 if (err < 0)
1577 return err;
1580 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1581 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1582 if (err < 0)
1583 return err;
1586 return 0;
1589 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
1590 int dbm)
1592 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1593 struct usbnet *usbdev = priv->usbdev;
1595 devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
1597 /* Device doesn't support changing txpower after initialization, only
1598 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1599 * currently used.
1601 if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
1602 if (!priv->radio_on)
1603 disassociate(usbdev, true); /* turn on radio */
1605 return 0;
1608 return -ENOTSUPP;
1611 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1613 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1614 struct usbnet *usbdev = priv->usbdev;
1616 *dbm = get_bcm4320_power_dbm(priv);
1618 devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
1620 return 0;
1623 #define SCAN_DELAY_JIFFIES (6 * HZ)
1624 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1625 struct cfg80211_scan_request *request)
1627 struct usbnet *usbdev = netdev_priv(dev);
1628 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1629 int ret;
1630 __le32 tmp;
1632 devdbg(usbdev, "cfg80211.scan");
1634 /* Get current bssid list from device before new scan, as new scan
1635 * clears internal bssid list.
1637 rndis_check_bssid_list(usbdev);
1639 if (!request)
1640 return -EINVAL;
1642 if (priv->scan_request && priv->scan_request != request)
1643 return -EBUSY;
1645 priv->scan_request = request;
1647 tmp = cpu_to_le32(1);
1648 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1649 sizeof(tmp));
1650 if (ret == 0) {
1651 /* Wait before retrieving scan results from device */
1652 queue_delayed_work(priv->workqueue, &priv->scan_work,
1653 SCAN_DELAY_JIFFIES);
1656 return ret;
1659 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1660 struct ndis_80211_bssid_ex *bssid)
1662 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1663 struct ieee80211_channel *channel;
1664 s32 signal;
1665 u64 timestamp;
1666 u16 capability;
1667 u16 beacon_interval;
1668 struct ndis_80211_fixed_ies *fixed;
1669 int ie_len, bssid_len;
1670 u8 *ie;
1672 devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
1673 bssid->mac);
1675 /* parse bssid structure */
1676 bssid_len = le32_to_cpu(bssid->length);
1678 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1679 sizeof(struct ndis_80211_fixed_ies))
1680 return NULL;
1682 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1684 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1685 ie_len = min(bssid_len - (int)sizeof(*bssid),
1686 (int)le32_to_cpu(bssid->ie_length));
1687 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1688 if (ie_len < 0)
1689 return NULL;
1691 /* extract data for cfg80211_inform_bss */
1692 channel = ieee80211_get_channel(priv->wdev.wiphy,
1693 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1694 if (!channel)
1695 return NULL;
1697 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1698 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1699 capability = le16_to_cpu(fixed->capabilities);
1700 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1702 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1703 timestamp, capability, beacon_interval, ie, ie_len, signal,
1704 GFP_KERNEL);
1707 static int rndis_check_bssid_list(struct usbnet *usbdev)
1709 void *buf = NULL;
1710 struct ndis_80211_bssid_list_ex *bssid_list;
1711 struct ndis_80211_bssid_ex *bssid;
1712 int ret = -EINVAL, len, count, bssid_len;
1713 bool resized = false;
1715 devdbg(usbdev, "check_bssid_list");
1717 len = CONTROL_BUFFER_SIZE;
1718 resize_buf:
1719 buf = kmalloc(len, GFP_KERNEL);
1720 if (!buf) {
1721 ret = -ENOMEM;
1722 goto out;
1725 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1726 if (ret != 0)
1727 goto out;
1729 if (!resized && len > CONTROL_BUFFER_SIZE) {
1730 resized = true;
1731 kfree(buf);
1732 goto resize_buf;
1735 bssid_list = buf;
1736 bssid = bssid_list->bssid;
1737 bssid_len = le32_to_cpu(bssid->length);
1738 count = le32_to_cpu(bssid_list->num_items);
1739 devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
1740 len);
1742 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1743 rndis_bss_info_update(usbdev, bssid);
1745 bssid = (void *)bssid + bssid_len;
1746 bssid_len = le32_to_cpu(bssid->length);
1747 count--;
1750 out:
1751 kfree(buf);
1752 return ret;
1755 static void rndis_get_scan_results(struct work_struct *work)
1757 struct rndis_wlan_private *priv =
1758 container_of(work, struct rndis_wlan_private, scan_work.work);
1759 struct usbnet *usbdev = priv->usbdev;
1760 int ret;
1762 devdbg(usbdev, "get_scan_results");
1764 if (!priv->scan_request)
1765 return;
1767 ret = rndis_check_bssid_list(usbdev);
1769 cfg80211_scan_done(priv->scan_request, ret < 0);
1771 priv->scan_request = NULL;
1774 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
1775 struct cfg80211_connect_params *sme)
1777 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1778 struct usbnet *usbdev = priv->usbdev;
1779 struct ieee80211_channel *channel = sme->channel;
1780 struct ndis_80211_ssid ssid;
1781 int pairwise = RNDIS_WLAN_ALG_NONE;
1782 int groupwise = RNDIS_WLAN_ALG_NONE;
1783 int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
1784 int length, i, ret, chan = -1;
1786 if (channel)
1787 chan = ieee80211_frequency_to_channel(channel->center_freq);
1789 groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
1790 for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
1791 pairwise |=
1792 rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
1794 if (sme->crypto.n_ciphers_pairwise > 0 &&
1795 pairwise == RNDIS_WLAN_ALG_NONE) {
1796 deverr(usbdev, "Unsupported pairwise cipher");
1797 return -ENOTSUPP;
1800 for (i = 0; i < sme->crypto.n_akm_suites; i++)
1801 keymgmt |=
1802 rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
1804 if (sme->crypto.n_akm_suites > 0 &&
1805 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
1806 deverr(usbdev, "Invalid keymgmt");
1807 return -ENOTSUPP;
1810 devdbg(usbdev, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:"
1811 "0x%x]:0x%x)", sme->ssid, sme->bssid, chan,
1812 sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
1813 groupwise, pairwise, keymgmt);
1815 if (is_associated(usbdev))
1816 disassociate(usbdev, false);
1818 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1819 if (ret < 0) {
1820 devdbg(usbdev, "connect: set_infra_mode failed, %d", ret);
1821 goto err_turn_radio_on;
1824 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
1825 keymgmt);
1826 if (ret < 0) {
1827 devdbg(usbdev, "connect: set_auth_mode failed, %d", ret);
1828 goto err_turn_radio_on;
1831 set_priv_filter(usbdev);
1833 ret = set_encr_mode(usbdev, pairwise, groupwise);
1834 if (ret < 0) {
1835 devdbg(usbdev, "connect: set_encr_mode failed, %d", ret);
1836 goto err_turn_radio_on;
1839 if (channel) {
1840 ret = set_channel(usbdev, chan);
1841 if (ret < 0) {
1842 devdbg(usbdev, "connect: set_channel failed, %d", ret);
1843 goto err_turn_radio_on;
1847 if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
1848 priv->encr_tx_key_index = sme->key_idx;
1849 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
1850 if (ret < 0) {
1851 devdbg(usbdev, "connect: add_wep_key failed, %d "
1852 "(%d, %d)", ret, sme->key_len, sme->key_idx);
1853 goto err_turn_radio_on;
1857 if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
1858 !is_broadcast_ether_addr(sme->bssid)) {
1859 ret = set_bssid(usbdev, sme->bssid);
1860 if (ret < 0) {
1861 devdbg(usbdev, "connect: set_bssid failed, %d", ret);
1862 goto err_turn_radio_on;
1864 } else
1865 clear_bssid(usbdev);
1867 length = sme->ssid_len;
1868 if (length > NDIS_802_11_LENGTH_SSID)
1869 length = NDIS_802_11_LENGTH_SSID;
1871 memset(&ssid, 0, sizeof(ssid));
1872 ssid.length = cpu_to_le32(length);
1873 memcpy(ssid.essid, sme->ssid, length);
1875 /* Pause and purge rx queue, so we don't pass packets before
1876 * 'media connect'-indication.
1878 usbnet_pause_rx(usbdev);
1879 usbnet_purge_paused_rxq(usbdev);
1881 ret = set_essid(usbdev, &ssid);
1882 if (ret < 0)
1883 devdbg(usbdev, "connect: set_essid failed, %d", ret);
1884 return ret;
1886 err_turn_radio_on:
1887 disassociate(usbdev, true);
1889 return ret;
1892 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
1893 u16 reason_code)
1895 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1896 struct usbnet *usbdev = priv->usbdev;
1898 devdbg(usbdev, "cfg80211.disconnect(%d)", reason_code);
1900 priv->connected = false;
1901 memset(priv->bssid, 0, ETH_ALEN);
1903 return deauthenticate(usbdev);
1906 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1907 struct cfg80211_ibss_params *params)
1909 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1910 struct usbnet *usbdev = priv->usbdev;
1911 struct ieee80211_channel *channel = params->channel;
1912 struct ndis_80211_ssid ssid;
1913 enum nl80211_auth_type auth_type;
1914 int ret, alg, length, chan = -1;
1916 if (channel)
1917 chan = ieee80211_frequency_to_channel(channel->center_freq);
1919 /* TODO: How to handle ad-hoc encryption?
1920 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
1921 * pre-shared for encryption (open/shared/wpa), is key set before
1922 * join_ibss? Which auth_type to use (not in params)? What about WPA?
1924 if (params->privacy) {
1925 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1926 alg = RNDIS_WLAN_ALG_WEP;
1927 } else {
1928 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1929 alg = RNDIS_WLAN_ALG_NONE;
1932 devdbg(usbdev, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)", params->ssid,
1933 params->bssid, chan, params->privacy);
1935 if (is_associated(usbdev))
1936 disassociate(usbdev, false);
1938 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
1939 if (ret < 0) {
1940 devdbg(usbdev, "join_ibss: set_infra_mode failed, %d", ret);
1941 goto err_turn_radio_on;
1944 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
1945 if (ret < 0) {
1946 devdbg(usbdev, "join_ibss: set_auth_mode failed, %d", ret);
1947 goto err_turn_radio_on;
1950 set_priv_filter(usbdev);
1952 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
1953 if (ret < 0) {
1954 devdbg(usbdev, "join_ibss: set_encr_mode failed, %d", ret);
1955 goto err_turn_radio_on;
1958 if (channel) {
1959 ret = set_channel(usbdev, chan);
1960 if (ret < 0) {
1961 devdbg(usbdev, "join_ibss: set_channel failed, %d",
1962 ret);
1963 goto err_turn_radio_on;
1967 if (params->bssid && !is_zero_ether_addr(params->bssid) &&
1968 !is_broadcast_ether_addr(params->bssid)) {
1969 ret = set_bssid(usbdev, params->bssid);
1970 if (ret < 0) {
1971 devdbg(usbdev, "join_ibss: set_bssid failed, %d", ret);
1972 goto err_turn_radio_on;
1974 } else
1975 clear_bssid(usbdev);
1977 length = params->ssid_len;
1978 if (length > NDIS_802_11_LENGTH_SSID)
1979 length = NDIS_802_11_LENGTH_SSID;
1981 memset(&ssid, 0, sizeof(ssid));
1982 ssid.length = cpu_to_le32(length);
1983 memcpy(ssid.essid, params->ssid, length);
1985 /* Don't need to pause rx queue for ad-hoc. */
1986 usbnet_purge_paused_rxq(usbdev);
1987 usbnet_resume_rx(usbdev);
1989 ret = set_essid(usbdev, &ssid);
1990 if (ret < 0)
1991 devdbg(usbdev, "join_ibss: set_essid failed, %d", ret);
1992 return ret;
1994 err_turn_radio_on:
1995 disassociate(usbdev, true);
1997 return ret;
2000 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2002 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2003 struct usbnet *usbdev = priv->usbdev;
2005 devdbg(usbdev, "cfg80211.leave_ibss()");
2007 priv->connected = false;
2008 memset(priv->bssid, 0, ETH_ALEN);
2010 return deauthenticate(usbdev);
2013 static int rndis_set_channel(struct wiphy *wiphy,
2014 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2016 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2017 struct usbnet *usbdev = priv->usbdev;
2019 return set_channel(usbdev,
2020 ieee80211_frequency_to_channel(chan->center_freq));
2023 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2024 u8 key_index, const u8 *mac_addr,
2025 struct key_params *params)
2027 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2028 struct usbnet *usbdev = priv->usbdev;
2029 __le32 flags;
2031 devdbg(usbdev, "rndis_add_key(%i, %pM, %08x)", key_index, mac_addr,
2032 params->cipher);
2034 switch (params->cipher) {
2035 case WLAN_CIPHER_SUITE_WEP40:
2036 case WLAN_CIPHER_SUITE_WEP104:
2037 return add_wep_key(usbdev, params->key, params->key_len,
2038 key_index);
2039 case WLAN_CIPHER_SUITE_TKIP:
2040 case WLAN_CIPHER_SUITE_CCMP:
2041 flags = 0;
2043 if (params->seq && params->seq_len > 0)
2044 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2045 if (mac_addr)
2046 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2047 NDIS_80211_ADDKEY_TRANSMIT_KEY;
2049 return add_wpa_key(usbdev, params->key, params->key_len,
2050 key_index, mac_addr, params->seq,
2051 params->seq_len, params->cipher, flags);
2052 default:
2053 devdbg(usbdev, "rndis_add_key: unsupported cipher %08x",
2054 params->cipher);
2055 return -ENOTSUPP;
2059 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2060 u8 key_index, const u8 *mac_addr)
2062 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2063 struct usbnet *usbdev = priv->usbdev;
2065 devdbg(usbdev, "rndis_del_key(%i, %pM)", key_index, mac_addr);
2067 return remove_key(usbdev, key_index, mac_addr);
2070 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2071 u8 key_index)
2073 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2074 struct usbnet *usbdev = priv->usbdev;
2075 struct rndis_wlan_encr_key key;
2077 devdbg(usbdev, "rndis_set_default_key(%i)", key_index);
2079 priv->encr_tx_key_index = key_index;
2081 key = priv->encr_keys[key_index];
2083 return add_wep_key(usbdev, key.material, key.len, key_index);
2086 static void rndis_fill_station_info(struct usbnet *usbdev,
2087 struct station_info *sinfo)
2089 __le32 linkspeed, rssi;
2090 int ret, len;
2092 memset(sinfo, 0, sizeof(*sinfo));
2094 len = sizeof(linkspeed);
2095 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2096 if (ret == 0) {
2097 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2098 sinfo->filled |= STATION_INFO_TX_BITRATE;
2101 len = sizeof(rssi);
2102 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2103 if (ret == 0) {
2104 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2105 sinfo->filled |= STATION_INFO_SIGNAL;
2109 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2110 u8 *mac, struct station_info *sinfo)
2112 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2113 struct usbnet *usbdev = priv->usbdev;
2115 if (compare_ether_addr(priv->bssid, mac))
2116 return -ENOENT;
2118 rndis_fill_station_info(usbdev, sinfo);
2120 return 0;
2123 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2124 int idx, u8 *mac, struct station_info *sinfo)
2126 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2127 struct usbnet *usbdev = priv->usbdev;
2129 if (idx != 0)
2130 return -ENOENT;
2132 memcpy(mac, priv->bssid, ETH_ALEN);
2134 rndis_fill_station_info(usbdev, sinfo);
2136 return 0;
2140 * workers, indication handlers, device poller
2142 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2144 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2145 struct ndis_80211_assoc_info *info;
2146 u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
2147 u8 bssid[ETH_ALEN];
2148 int resp_ie_len, req_ie_len;
2149 u8 *req_ie, *resp_ie;
2150 int ret, offset;
2151 bool roamed = false;
2153 if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2154 /* received media connect indication while connected, either
2155 * device reassociated with same AP or roamed to new. */
2156 roamed = true;
2159 req_ie_len = 0;
2160 resp_ie_len = 0;
2161 req_ie = NULL;
2162 resp_ie = NULL;
2164 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2165 memset(assoc_buf, 0, sizeof(assoc_buf));
2166 info = (void *)assoc_buf;
2168 /* Get association info IEs from device and send them back to
2169 * userspace. */
2170 ret = get_association_info(usbdev, info, sizeof(assoc_buf));
2171 if (!ret) {
2172 req_ie_len = le32_to_cpu(info->req_ie_length);
2173 if (req_ie_len > 0) {
2174 offset = le32_to_cpu(info->offset_req_ies);
2175 req_ie = (u8 *)info + offset;
2178 resp_ie_len = le32_to_cpu(info->resp_ie_length);
2179 if (resp_ie_len > 0) {
2180 offset = le32_to_cpu(info->offset_resp_ies);
2181 resp_ie = (u8 *)info + offset;
2184 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2185 return;
2187 ret = get_bssid(usbdev, bssid);
2188 if (ret < 0)
2189 memset(bssid, 0, sizeof(bssid));
2191 devdbg(usbdev, "link up work: [%pM] %s", bssid, roamed ? "roamed" : "");
2193 /* Internal bss list in device always contains at least the currently
2194 * connected bss and we can get it to cfg80211 with
2195 * rndis_check_bssid_list().
2196 * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2197 * spec.
2199 rndis_check_bssid_list(usbdev);
2201 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2202 if (!roamed)
2203 cfg80211_connect_result(usbdev->net, bssid, req_ie,
2204 req_ie_len, resp_ie,
2205 resp_ie_len, 0, GFP_KERNEL);
2206 else
2207 cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2208 resp_ie, resp_ie_len, GFP_KERNEL);
2209 } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2210 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2212 priv->connected = true;
2213 memcpy(priv->bssid, bssid, ETH_ALEN);
2215 usbnet_resume_rx(usbdev);
2216 netif_carrier_on(usbdev->net);
2219 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2221 union iwreq_data evt;
2223 netif_carrier_off(usbdev->net);
2225 evt.data.flags = 0;
2226 evt.data.length = 0;
2227 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2228 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2231 static void rndis_wlan_worker(struct work_struct *work)
2233 struct rndis_wlan_private *priv =
2234 container_of(work, struct rndis_wlan_private, work);
2235 struct usbnet *usbdev = priv->usbdev;
2237 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2238 rndis_wlan_do_link_up_work(usbdev);
2240 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2241 rndis_wlan_do_link_down_work(usbdev);
2243 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2244 set_multicast_list(usbdev);
2247 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2249 struct usbnet *usbdev = netdev_priv(dev);
2250 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2252 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2253 return;
2255 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2256 queue_work(priv->workqueue, &priv->work);
2259 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2260 struct ndis_80211_status_indication *indication,
2261 int len)
2263 u8 *buf;
2264 const char *type;
2265 int flags, buflen, key_id;
2266 bool pairwise_error, group_error;
2267 struct ndis_80211_auth_request *auth_req;
2268 enum nl80211_key_type key_type;
2270 /* must have at least one array entry */
2271 if (len < offsetof(struct ndis_80211_status_indication, u) +
2272 sizeof(struct ndis_80211_auth_request)) {
2273 devinfo(usbdev, "authentication indication: "
2274 "too short message (%i)", len);
2275 return;
2278 buf = (void *)&indication->u.auth_request[0];
2279 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2281 while (buflen >= sizeof(*auth_req)) {
2282 auth_req = (void *)buf;
2283 type = "unknown";
2284 flags = le32_to_cpu(auth_req->flags);
2285 pairwise_error = false;
2286 group_error = false;
2288 if (flags & 0x1)
2289 type = "reauth request";
2290 if (flags & 0x2)
2291 type = "key update request";
2292 if (flags & 0x6) {
2293 pairwise_error = true;
2294 type = "pairwise_error";
2296 if (flags & 0xe) {
2297 group_error = true;
2298 type = "group_error";
2301 devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
2302 le32_to_cpu(auth_req->flags));
2304 if (pairwise_error) {
2305 key_type = NL80211_KEYTYPE_PAIRWISE;
2306 key_id = -1;
2308 cfg80211_michael_mic_failure(usbdev->net,
2309 auth_req->bssid,
2310 key_type, key_id, NULL,
2311 GFP_KERNEL);
2314 if (group_error) {
2315 key_type = NL80211_KEYTYPE_GROUP;
2316 key_id = -1;
2318 cfg80211_michael_mic_failure(usbdev->net,
2319 auth_req->bssid,
2320 key_type, key_id, NULL,
2321 GFP_KERNEL);
2324 buflen -= le32_to_cpu(auth_req->length);
2325 buf += le32_to_cpu(auth_req->length);
2329 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2330 struct ndis_80211_status_indication *indication,
2331 int len)
2333 struct ndis_80211_pmkid_cand_list *cand_list;
2334 int list_len, expected_len, i;
2336 if (len < offsetof(struct ndis_80211_status_indication, u) +
2337 sizeof(struct ndis_80211_pmkid_cand_list)) {
2338 devinfo(usbdev, "pmkid candidate list indication: "
2339 "too short message (%i)", len);
2340 return;
2343 list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2344 sizeof(struct ndis_80211_pmkid_candidate);
2345 expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2346 offsetof(struct ndis_80211_status_indication, u);
2348 if (len < expected_len) {
2349 devinfo(usbdev, "pmkid candidate list indication: "
2350 "list larger than buffer (%i < %i)",
2351 len, expected_len);
2352 return;
2355 cand_list = &indication->u.cand_list;
2357 devinfo(usbdev, "pmkid candidate list indication: "
2358 "version %i, candidates %i",
2359 le32_to_cpu(cand_list->version),
2360 le32_to_cpu(cand_list->num_candidates));
2362 if (le32_to_cpu(cand_list->version) != 1)
2363 return;
2365 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2366 struct ndis_80211_pmkid_candidate *cand =
2367 &cand_list->candidate_list[i];
2369 devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
2370 i, le32_to_cpu(cand->flags), cand->bssid);
2372 #if 0
2373 struct iw_pmkid_cand pcand;
2374 union iwreq_data wrqu;
2376 memset(&pcand, 0, sizeof(pcand));
2377 if (le32_to_cpu(cand->flags) & 0x01)
2378 pcand.flags |= IW_PMKID_CAND_PREAUTH;
2379 pcand.index = i;
2380 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2382 memset(&wrqu, 0, sizeof(wrqu));
2383 wrqu.data.length = sizeof(pcand);
2384 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2385 (u8 *)&pcand);
2386 #endif
2390 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2391 struct rndis_indicate *msg, int buflen)
2393 struct ndis_80211_status_indication *indication;
2394 int len, offset;
2396 offset = offsetof(struct rndis_indicate, status) +
2397 le32_to_cpu(msg->offset);
2398 len = le32_to_cpu(msg->length);
2400 if (len < 8) {
2401 devinfo(usbdev, "media specific indication, "
2402 "ignore too short message (%i < 8)", len);
2403 return;
2406 if (offset + len > buflen) {
2407 devinfo(usbdev, "media specific indication, "
2408 "too large to fit to buffer (%i > %i)",
2409 offset + len, buflen);
2410 return;
2413 indication = (void *)((u8 *)msg + offset);
2415 switch (le32_to_cpu(indication->status_type)) {
2416 case NDIS_80211_STATUSTYPE_RADIOSTATE:
2417 devinfo(usbdev, "radio state indication: %i",
2418 le32_to_cpu(indication->u.radio_status));
2419 return;
2421 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2422 devinfo(usbdev, "media stream mode indication: %i",
2423 le32_to_cpu(indication->u.media_stream_mode));
2424 return;
2426 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2427 rndis_wlan_auth_indication(usbdev, indication, len);
2428 return;
2430 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2431 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2432 return;
2434 default:
2435 devinfo(usbdev, "media specific indication: "
2436 "unknown status type 0x%08x",
2437 le32_to_cpu(indication->status_type));
2441 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2443 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2444 struct rndis_indicate *msg = ind;
2446 switch (msg->status) {
2447 case RNDIS_STATUS_MEDIA_CONNECT:
2448 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2449 /* OID_802_11_ADD_KEY causes sometimes extra
2450 * "media connect" indications which confuses driver
2451 * and userspace to think that device is
2452 * roaming/reassociating when it isn't.
2454 devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
2455 "'media connect'");
2456 return;
2459 usbnet_pause_rx(usbdev);
2461 devinfo(usbdev, "media connect");
2463 /* queue work to avoid recursive calls into rndis_command */
2464 set_bit(WORK_LINK_UP, &priv->work_pending);
2465 queue_work(priv->workqueue, &priv->work);
2466 break;
2468 case RNDIS_STATUS_MEDIA_DISCONNECT:
2469 devinfo(usbdev, "media disconnect");
2471 /* queue work to avoid recursive calls into rndis_command */
2472 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2473 queue_work(priv->workqueue, &priv->work);
2474 break;
2476 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2477 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2478 break;
2480 default:
2481 devinfo(usbdev, "indication: 0x%08x",
2482 le32_to_cpu(msg->status));
2483 break;
2487 static int rndis_wlan_get_caps(struct usbnet *usbdev)
2489 struct {
2490 __le32 num_items;
2491 __le32 items[8];
2492 } networks_supported;
2493 int len, retval, i, n;
2494 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2496 /* determine supported modes */
2497 len = sizeof(networks_supported);
2498 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2499 &networks_supported, &len);
2500 if (retval >= 0) {
2501 n = le32_to_cpu(networks_supported.num_items);
2502 if (n > 8)
2503 n = 8;
2504 for (i = 0; i < n; i++) {
2505 switch (le32_to_cpu(networks_supported.items[i])) {
2506 case NDIS_80211_TYPE_FREQ_HOP:
2507 case NDIS_80211_TYPE_DIRECT_SEQ:
2508 priv->caps |= CAP_MODE_80211B;
2509 break;
2510 case NDIS_80211_TYPE_OFDM_A:
2511 priv->caps |= CAP_MODE_80211A;
2512 break;
2513 case NDIS_80211_TYPE_OFDM_G:
2514 priv->caps |= CAP_MODE_80211G;
2515 break;
2520 return retval;
2523 #define DEVICE_POLLER_JIFFIES (HZ)
2524 static void rndis_device_poller(struct work_struct *work)
2526 struct rndis_wlan_private *priv =
2527 container_of(work, struct rndis_wlan_private,
2528 dev_poller_work.work);
2529 struct usbnet *usbdev = priv->usbdev;
2530 __le32 rssi, tmp;
2531 int len, ret, j;
2532 int update_jiffies = DEVICE_POLLER_JIFFIES;
2533 void *buf;
2535 /* Only check/do workaround when connected. Calling is_associated()
2536 * also polls device with rndis_command() and catches for media link
2537 * indications.
2539 if (!is_associated(usbdev))
2540 goto end;
2542 len = sizeof(rssi);
2543 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2544 if (ret == 0)
2545 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
2547 devdbg(usbdev, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d",
2548 ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
2550 /* Workaround transfer stalls on poor quality links.
2551 * TODO: find right way to fix these stalls (as stalls do not happen
2552 * with ndiswrapper/windows driver). */
2553 if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
2554 /* Decrease stats worker interval to catch stalls.
2555 * faster. Faster than 400-500ms causes packet loss,
2556 * Slower doesn't catch stalls fast enough.
2558 j = msecs_to_jiffies(priv->param_workaround_interval);
2559 if (j > DEVICE_POLLER_JIFFIES)
2560 j = DEVICE_POLLER_JIFFIES;
2561 else if (j <= 0)
2562 j = 1;
2563 update_jiffies = j;
2565 /* Send scan OID. Use of both OIDs is required to get device
2566 * working.
2568 tmp = cpu_to_le32(1);
2569 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2570 sizeof(tmp));
2572 len = CONTROL_BUFFER_SIZE;
2573 buf = kmalloc(len, GFP_KERNEL);
2574 if (!buf)
2575 goto end;
2577 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2578 kfree(buf);
2581 end:
2582 if (update_jiffies >= HZ)
2583 update_jiffies = round_jiffies_relative(update_jiffies);
2584 else {
2585 j = round_jiffies_relative(update_jiffies);
2586 if (abs(j - update_jiffies) <= 10)
2587 update_jiffies = j;
2590 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2591 update_jiffies);
2595 * driver/device initialization
2597 static int bcm4320a_early_init(struct usbnet *usbdev)
2599 /* bcm4320a doesn't handle configuration parameters well. Try
2600 * set any and you get partially zeroed mac and broken device.
2603 return 0;
2606 static int bcm4320b_early_init(struct usbnet *usbdev)
2608 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2609 char buf[8];
2611 /* Early initialization settings, setting these won't have effect
2612 * if called after generic_rndis_bind().
2615 priv->param_country[0] = modparam_country[0];
2616 priv->param_country[1] = modparam_country[1];
2617 priv->param_country[2] = 0;
2618 priv->param_frameburst = modparam_frameburst;
2619 priv->param_afterburner = modparam_afterburner;
2620 priv->param_power_save = modparam_power_save;
2621 priv->param_power_output = modparam_power_output;
2622 priv->param_roamtrigger = modparam_roamtrigger;
2623 priv->param_roamdelta = modparam_roamdelta;
2625 priv->param_country[0] = toupper(priv->param_country[0]);
2626 priv->param_country[1] = toupper(priv->param_country[1]);
2627 /* doesn't support EU as country code, use FI instead */
2628 if (!strcmp(priv->param_country, "EU"))
2629 strcpy(priv->param_country, "FI");
2631 if (priv->param_power_save < 0)
2632 priv->param_power_save = 0;
2633 else if (priv->param_power_save > 2)
2634 priv->param_power_save = 2;
2636 if (priv->param_power_output < 0)
2637 priv->param_power_output = 0;
2638 else if (priv->param_power_output > 3)
2639 priv->param_power_output = 3;
2641 if (priv->param_roamtrigger < -80)
2642 priv->param_roamtrigger = -80;
2643 else if (priv->param_roamtrigger > -60)
2644 priv->param_roamtrigger = -60;
2646 if (priv->param_roamdelta < 0)
2647 priv->param_roamdelta = 0;
2648 else if (priv->param_roamdelta > 2)
2649 priv->param_roamdelta = 2;
2651 if (modparam_workaround_interval < 0)
2652 priv->param_workaround_interval = 500;
2653 else
2654 priv->param_workaround_interval = modparam_workaround_interval;
2656 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
2657 rndis_set_config_parameter_str(usbdev, "FrameBursting",
2658 priv->param_frameburst ? "1" : "0");
2659 rndis_set_config_parameter_str(usbdev, "Afterburner",
2660 priv->param_afterburner ? "1" : "0");
2661 sprintf(buf, "%d", priv->param_power_save);
2662 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
2663 sprintf(buf, "%d", priv->param_power_output);
2664 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
2665 sprintf(buf, "%d", priv->param_roamtrigger);
2666 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
2667 sprintf(buf, "%d", priv->param_roamdelta);
2668 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
2670 return 0;
2673 /* same as rndis_netdev_ops but with local multicast handler */
2674 static const struct net_device_ops rndis_wlan_netdev_ops = {
2675 .ndo_open = usbnet_open,
2676 .ndo_stop = usbnet_stop,
2677 .ndo_start_xmit = usbnet_start_xmit,
2678 .ndo_tx_timeout = usbnet_tx_timeout,
2679 .ndo_set_mac_address = eth_mac_addr,
2680 .ndo_validate_addr = eth_validate_addr,
2681 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
2684 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
2686 struct wiphy *wiphy;
2687 struct rndis_wlan_private *priv;
2688 int retval, len;
2689 __le32 tmp;
2691 /* allocate wiphy and rndis private data
2692 * NOTE: We only support a single virtual interface, so wiphy
2693 * and wireless_dev are somewhat synonymous for this device.
2695 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
2696 if (!wiphy)
2697 return -ENOMEM;
2699 priv = wiphy_priv(wiphy);
2700 usbdev->net->ieee80211_ptr = &priv->wdev;
2701 priv->wdev.wiphy = wiphy;
2702 priv->wdev.iftype = NL80211_IFTYPE_STATION;
2704 /* These have to be initialized before calling generic_rndis_bind().
2705 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
2707 usbdev->driver_priv = priv;
2708 priv->usbdev = usbdev;
2710 mutex_init(&priv->command_lock);
2712 /* because rndis_command() sleeps we need to use workqueue */
2713 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2714 INIT_WORK(&priv->work, rndis_wlan_worker);
2715 INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
2716 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
2718 /* try bind rndis_host */
2719 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2720 if (retval < 0)
2721 goto fail;
2723 /* generic_rndis_bind set packet filter to multicast_all+
2724 * promisc mode which doesn't work well for our devices (device
2725 * picks up rssi to closest station instead of to access point).
2727 * rndis_host wants to avoid all OID as much as possible
2728 * so do promisc/multicast handling in rndis_wlan.
2730 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
2732 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2733 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2734 sizeof(tmp));
2736 len = sizeof(tmp);
2737 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
2738 &len);
2739 priv->multicast_size = le32_to_cpu(tmp);
2740 if (retval < 0 || priv->multicast_size < 0)
2741 priv->multicast_size = 0;
2742 if (priv->multicast_size > 0)
2743 usbdev->net->flags |= IFF_MULTICAST;
2744 else
2745 usbdev->net->flags &= ~IFF_MULTICAST;
2747 /* fill-out wiphy structure and register w/ cfg80211 */
2748 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
2749 wiphy->privid = rndis_wiphy_privid;
2750 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
2751 | BIT(NL80211_IFTYPE_ADHOC);
2752 wiphy->max_scan_ssids = 1;
2754 /* TODO: fill-out band/encr information based on priv->caps */
2755 rndis_wlan_get_caps(usbdev);
2757 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
2758 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
2759 priv->band.channels = priv->channels;
2760 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
2761 priv->band.bitrates = priv->rates;
2762 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
2763 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
2764 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2766 memcpy(priv->cipher_suites, rndis_cipher_suites,
2767 sizeof(rndis_cipher_suites));
2768 wiphy->cipher_suites = priv->cipher_suites;
2769 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
2771 set_wiphy_dev(wiphy, &usbdev->udev->dev);
2773 if (wiphy_register(wiphy)) {
2774 retval = -ENODEV;
2775 goto fail;
2778 set_default_iw_params(usbdev);
2780 /* set default rts/frag */
2781 rndis_set_wiphy_params(wiphy,
2782 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
2784 /* turn radio on */
2785 priv->radio_on = true;
2786 disassociate(usbdev, true);
2787 netif_carrier_off(usbdev->net);
2789 return 0;
2791 fail:
2792 cancel_delayed_work_sync(&priv->dev_poller_work);
2793 cancel_delayed_work_sync(&priv->scan_work);
2794 cancel_work_sync(&priv->work);
2795 flush_workqueue(priv->workqueue);
2796 destroy_workqueue(priv->workqueue);
2798 wiphy_free(wiphy);
2799 return retval;
2802 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
2804 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2806 /* turn radio off */
2807 disassociate(usbdev, false);
2809 cancel_delayed_work_sync(&priv->dev_poller_work);
2810 cancel_delayed_work_sync(&priv->scan_work);
2811 cancel_work_sync(&priv->work);
2812 flush_workqueue(priv->workqueue);
2813 destroy_workqueue(priv->workqueue);
2815 if (priv && priv->wpa_ie_len)
2816 kfree(priv->wpa_ie);
2818 rndis_unbind(usbdev, intf);
2820 wiphy_unregister(priv->wdev.wiphy);
2821 wiphy_free(priv->wdev.wiphy);
2824 static int rndis_wlan_reset(struct usbnet *usbdev)
2826 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2827 int retval;
2829 devdbg(usbdev, "rndis_wlan_reset");
2831 retval = rndis_reset(usbdev);
2832 if (retval)
2833 devwarn(usbdev, "rndis_reset() failed: %d", retval);
2835 /* rndis_reset cleared multicast list, so restore here.
2836 (set_multicast_list() also turns on current packet filter) */
2837 set_multicast_list(usbdev);
2839 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2840 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
2842 return deauthenticate(usbdev);
2845 static int rndis_wlan_stop(struct usbnet *usbdev)
2847 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2848 int retval;
2849 __le32 filter;
2851 devdbg(usbdev, "rndis_wlan_stop");
2853 retval = disassociate(usbdev, false);
2855 priv->work_pending = 0;
2856 cancel_delayed_work_sync(&priv->dev_poller_work);
2857 cancel_delayed_work_sync(&priv->scan_work);
2858 cancel_work_sync(&priv->work);
2859 flush_workqueue(priv->workqueue);
2861 if (priv->scan_request) {
2862 cfg80211_scan_done(priv->scan_request, true);
2863 priv->scan_request = NULL;
2866 /* Set current packet filter zero to block receiving data packets from
2867 device. */
2868 filter = 0;
2869 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
2870 sizeof(filter));
2872 return retval;
2875 static const struct driver_info bcm4320b_info = {
2876 .description = "Wireless RNDIS device, BCM4320b based",
2877 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2878 FLAG_AVOID_UNLINK_URBS,
2879 .bind = rndis_wlan_bind,
2880 .unbind = rndis_wlan_unbind,
2881 .status = rndis_status,
2882 .rx_fixup = rndis_rx_fixup,
2883 .tx_fixup = rndis_tx_fixup,
2884 .reset = rndis_wlan_reset,
2885 .stop = rndis_wlan_stop,
2886 .early_init = bcm4320b_early_init,
2887 .indication = rndis_wlan_indication,
2890 static const struct driver_info bcm4320a_info = {
2891 .description = "Wireless RNDIS device, BCM4320a based",
2892 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2893 FLAG_AVOID_UNLINK_URBS,
2894 .bind = rndis_wlan_bind,
2895 .unbind = rndis_wlan_unbind,
2896 .status = rndis_status,
2897 .rx_fixup = rndis_rx_fixup,
2898 .tx_fixup = rndis_tx_fixup,
2899 .reset = rndis_wlan_reset,
2900 .stop = rndis_wlan_stop,
2901 .early_init = bcm4320a_early_init,
2902 .indication = rndis_wlan_indication,
2905 static const struct driver_info rndis_wlan_info = {
2906 .description = "Wireless RNDIS device",
2907 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2908 FLAG_AVOID_UNLINK_URBS,
2909 .bind = rndis_wlan_bind,
2910 .unbind = rndis_wlan_unbind,
2911 .status = rndis_status,
2912 .rx_fixup = rndis_rx_fixup,
2913 .tx_fixup = rndis_tx_fixup,
2914 .reset = rndis_wlan_reset,
2915 .stop = rndis_wlan_stop,
2916 .early_init = bcm4320a_early_init,
2917 .indication = rndis_wlan_indication,
2920 /*-------------------------------------------------------------------------*/
2922 static const struct usb_device_id products [] = {
2923 #define RNDIS_MASTER_INTERFACE \
2924 .bInterfaceClass = USB_CLASS_COMM, \
2925 .bInterfaceSubClass = 2 /* ACM */, \
2926 .bInterfaceProtocol = 0x0ff
2928 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2929 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2932 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2933 | USB_DEVICE_ID_MATCH_DEVICE,
2934 .idVendor = 0x0411,
2935 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2936 RNDIS_MASTER_INTERFACE,
2937 .driver_info = (unsigned long) &bcm4320b_info,
2938 }, {
2939 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2940 | USB_DEVICE_ID_MATCH_DEVICE,
2941 .idVendor = 0x0baf,
2942 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2943 RNDIS_MASTER_INTERFACE,
2944 .driver_info = (unsigned long) &bcm4320b_info,
2945 }, {
2946 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2947 | USB_DEVICE_ID_MATCH_DEVICE,
2948 .idVendor = 0x050d,
2949 .idProduct = 0x011b, /* Belkin F5D7051 */
2950 RNDIS_MASTER_INTERFACE,
2951 .driver_info = (unsigned long) &bcm4320b_info,
2952 }, {
2953 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2954 | USB_DEVICE_ID_MATCH_DEVICE,
2955 .idVendor = 0x1799, /* Belkin has two vendor ids */
2956 .idProduct = 0x011b, /* Belkin F5D7051 */
2957 RNDIS_MASTER_INTERFACE,
2958 .driver_info = (unsigned long) &bcm4320b_info,
2959 }, {
2960 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2961 | USB_DEVICE_ID_MATCH_DEVICE,
2962 .idVendor = 0x13b1,
2963 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2964 RNDIS_MASTER_INTERFACE,
2965 .driver_info = (unsigned long) &bcm4320b_info,
2966 }, {
2967 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2968 | USB_DEVICE_ID_MATCH_DEVICE,
2969 .idVendor = 0x13b1,
2970 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2971 RNDIS_MASTER_INTERFACE,
2972 .driver_info = (unsigned long) &bcm4320b_info,
2973 }, {
2974 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2975 | USB_DEVICE_ID_MATCH_DEVICE,
2976 .idVendor = 0x0b05,
2977 .idProduct = 0x1717, /* Asus WL169gE */
2978 RNDIS_MASTER_INTERFACE,
2979 .driver_info = (unsigned long) &bcm4320b_info,
2980 }, {
2981 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2982 | USB_DEVICE_ID_MATCH_DEVICE,
2983 .idVendor = 0x0a5c,
2984 .idProduct = 0xd11b, /* Eminent EM4045 */
2985 RNDIS_MASTER_INTERFACE,
2986 .driver_info = (unsigned long) &bcm4320b_info,
2987 }, {
2988 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2989 | USB_DEVICE_ID_MATCH_DEVICE,
2990 .idVendor = 0x1690,
2991 .idProduct = 0x0715, /* BT Voyager 1055 */
2992 RNDIS_MASTER_INTERFACE,
2993 .driver_info = (unsigned long) &bcm4320b_info,
2995 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2996 * parameters available, hardware probably contain older firmware version with
2997 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3000 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3001 | USB_DEVICE_ID_MATCH_DEVICE,
3002 .idVendor = 0x13b1,
3003 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3004 RNDIS_MASTER_INTERFACE,
3005 .driver_info = (unsigned long) &bcm4320a_info,
3006 }, {
3007 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3008 | USB_DEVICE_ID_MATCH_DEVICE,
3009 .idVendor = 0x0baf,
3010 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3011 RNDIS_MASTER_INTERFACE,
3012 .driver_info = (unsigned long) &bcm4320a_info,
3013 }, {
3014 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3015 | USB_DEVICE_ID_MATCH_DEVICE,
3016 .idVendor = 0x0411,
3017 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3018 RNDIS_MASTER_INTERFACE,
3019 .driver_info = (unsigned long) &bcm4320a_info,
3021 /* Generic Wireless RNDIS devices that we don't have exact
3022 * idVendor/idProduct/chip yet.
3025 /* RNDIS is MSFT's un-official variant of CDC ACM */
3026 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3027 .driver_info = (unsigned long) &rndis_wlan_info,
3028 }, {
3029 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3030 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3031 .driver_info = (unsigned long) &rndis_wlan_info,
3033 { }, // END
3035 MODULE_DEVICE_TABLE(usb, products);
3037 static struct usb_driver rndis_wlan_driver = {
3038 .name = "rndis_wlan",
3039 .id_table = products,
3040 .probe = usbnet_probe,
3041 .disconnect = usbnet_disconnect,
3042 .suspend = usbnet_suspend,
3043 .resume = usbnet_resume,
3046 static int __init rndis_wlan_init(void)
3048 return usb_register(&rndis_wlan_driver);
3050 module_init(rndis_wlan_init);
3052 static void __exit rndis_wlan_exit(void)
3054 usb_deregister(&rndis_wlan_driver);
3056 module_exit(rndis_wlan_exit);
3058 MODULE_AUTHOR("Bjorge Dijkstra");
3059 MODULE_AUTHOR("Jussi Kivilinna");
3060 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3061 MODULE_LICENSE("GPL");