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