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