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