Merge branch 'from-linus' into upstream
[linux-2.6.git] / drivers / net / wireless / bcm43xx / bcm43xx_wx.c
1 /*
2
3   Broadcom BCM43xx wireless driver
4
5   Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6                      Stefano Brivio <st3@riseup.net>
7                      Michael Buesch <mbuesch@freenet.de>
8                      Danny van Dyk <kugelfang@gentoo.org>
9                      Andreas Jaggi <andreas.jaggi@waterwave.ch>
10
11   Some parts of the code in this file are derived from the ipw2200
12   driver  Copyright(c) 2003 - 2004 Intel Corporation.
13
14   This program is free software; you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation; either version 2 of the License, or
17   (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; see the file COPYING.  If not, write to
26   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27   Boston, MA 02110-1301, USA.
28
29 */
30
31 #include <linux/wireless.h>
32 #include <net/iw_handler.h>
33 #include <net/ieee80211softmac.h>
34 #include <net/ieee80211softmac_wx.h>
35 #include <linux/capability.h>
36 #include <linux/sched.h> /* for capable() */
37 #include <linux/delay.h>
38
39 #include "bcm43xx.h"
40 #include "bcm43xx_wx.h"
41 #include "bcm43xx_main.h"
42 #include "bcm43xx_radio.h"
43 #include "bcm43xx_phy.h"
44
45
46 /* The WIRELESS_EXT version, which is implemented by this driver. */
47 #define BCM43xx_WX_VERSION      18
48
49 #define MAX_WX_STRING           80
50 /* FIXME: the next line is a guess as to what the maximum value of RX power
51           (in dBm) might be */
52 #define RX_POWER_MAX            -10
53
54
55 static int bcm43xx_wx_get_name(struct net_device *net_dev,
56                                struct iw_request_info *info,
57                                union iwreq_data *data,
58                                char *extra)
59 {
60         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
61         int i;
62         struct bcm43xx_phyinfo *phy;
63         char suffix[7] = { 0 };
64         int have_a = 0, have_b = 0, have_g = 0;
65
66         mutex_lock(&bcm->mutex);
67         for (i = 0; i < bcm->nr_80211_available; i++) {
68                 phy = &(bcm->core_80211_ext[i].phy);
69                 switch (phy->type) {
70                 case BCM43xx_PHYTYPE_A:
71                         have_a = 1;
72                         break;
73                 case BCM43xx_PHYTYPE_G:
74                         have_g = 1;
75                 case BCM43xx_PHYTYPE_B:
76                         have_b = 1;
77                         break;
78                 default:
79                         assert(0);
80                 }
81         }
82         mutex_unlock(&bcm->mutex);
83
84         i = 0;
85         if (have_a) {
86                 suffix[i++] = 'a';
87                 suffix[i++] = '/';
88         }
89         if (have_b) {
90                 suffix[i++] = 'b';
91                 suffix[i++] = '/';
92         }
93         if (have_g) {
94                 suffix[i++] = 'g';
95                 suffix[i++] = '/';
96         }
97         if (i != 0) 
98                 suffix[i - 1] = '\0';
99
100         snprintf(data->name, IFNAMSIZ, "IEEE 802.11%s", suffix);
101
102         return 0;
103 }
104
105 static int bcm43xx_wx_set_channelfreq(struct net_device *net_dev,
106                                       struct iw_request_info *info,
107                                       union iwreq_data *data,
108                                       char *extra)
109 {
110         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
111         unsigned long flags;
112         u8 channel;
113         int freq;
114         int err = -EINVAL;
115
116         mutex_lock(&bcm->mutex);
117         spin_lock_irqsave(&bcm->irq_lock, flags);
118
119         if ((data->freq.m >= 0) && (data->freq.m <= 1000)) {
120                 channel = data->freq.m;
121                 freq = bcm43xx_channel_to_freq(bcm, channel);
122         } else {
123                 channel = bcm43xx_freq_to_channel(bcm, data->freq.m);
124                 freq = data->freq.m;
125         }
126         if (!ieee80211_is_valid_channel(bcm->ieee, channel))
127                 goto out_unlock;
128         if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
129                 //ieee80211softmac_disassoc(softmac, $REASON);
130                 bcm43xx_mac_suspend(bcm);
131                 err = bcm43xx_radio_selectchannel(bcm, channel, 0);
132                 bcm43xx_mac_enable(bcm);
133         } else {
134                 bcm43xx_current_radio(bcm)->initial_channel = channel;
135                 err = 0;
136         }
137 out_unlock:
138         spin_unlock_irqrestore(&bcm->irq_lock, flags);
139         mutex_unlock(&bcm->mutex);
140
141         return err;
142 }
143
144 static int bcm43xx_wx_get_channelfreq(struct net_device *net_dev,
145                                       struct iw_request_info *info,
146                                       union iwreq_data *data,
147                                       char *extra)
148 {
149         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
150         struct bcm43xx_radioinfo *radio;
151         int err = -ENODEV;
152         u16 channel;
153
154         mutex_lock(&bcm->mutex);
155         radio = bcm43xx_current_radio(bcm);
156         channel = radio->channel;
157         if (channel == 0xFF) {
158                 channel = radio->initial_channel;
159                 if (channel == 0xFF)
160                         goto out_unlock;
161         }
162         assert(channel > 0 && channel <= 1000);
163         data->freq.e = 1;
164         data->freq.m = bcm43xx_channel_to_freq(bcm, channel) * 100000;
165         data->freq.flags = 1;
166
167         err = 0;
168 out_unlock:
169         mutex_unlock(&bcm->mutex);
170
171         return err;
172 }
173
174 static int bcm43xx_wx_set_mode(struct net_device *net_dev,
175                                struct iw_request_info *info,
176                                union iwreq_data *data,
177                                char *extra)
178 {
179         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
180         unsigned long flags;
181         int mode;
182
183         mode = data->mode;
184         if (mode == IW_MODE_AUTO)
185                 mode = BCM43xx_INITIAL_IWMODE;
186
187         mutex_lock(&bcm->mutex);
188         spin_lock_irqsave(&bcm->irq_lock, flags);
189         if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
190                 if (bcm->ieee->iw_mode != mode)
191                         bcm43xx_set_iwmode(bcm, mode);
192         } else
193                 bcm->ieee->iw_mode = mode;
194         spin_unlock_irqrestore(&bcm->irq_lock, flags);
195         mutex_unlock(&bcm->mutex);
196
197         return 0;
198 }
199
200 static int bcm43xx_wx_get_mode(struct net_device *net_dev,
201                                struct iw_request_info *info,
202                                union iwreq_data *data,
203                                char *extra)
204 {
205         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
206
207         mutex_lock(&bcm->mutex);
208         data->mode = bcm->ieee->iw_mode;
209         mutex_unlock(&bcm->mutex);
210
211         return 0;
212 }
213
214 static int bcm43xx_wx_get_rangeparams(struct net_device *net_dev,
215                                       struct iw_request_info *info,
216                                       union iwreq_data *data,
217                                       char *extra)
218 {
219         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
220         struct iw_range *range = (struct iw_range *)extra;
221         const struct ieee80211_geo *geo;
222         int i, j;
223         struct bcm43xx_phyinfo *phy;
224
225         data->data.length = sizeof(*range);
226         memset(range, 0, sizeof(*range));
227
228         //TODO: What about 802.11b?
229         /* 54Mb/s == ~27Mb/s payload throughput (802.11g) */
230         range->throughput = 27 * 1000 * 1000;
231
232         range->max_qual.qual = 100;
233         /* TODO: Real max RSSI */
234         range->max_qual.level = 0;
235         range->max_qual.noise = 0;
236         range->max_qual.updated = IW_QUAL_ALL_UPDATED;
237
238         range->avg_qual.qual = 50;
239         range->avg_qual.level = 0;
240         range->avg_qual.noise = 0;
241         range->avg_qual.updated = IW_QUAL_ALL_UPDATED;
242
243         range->min_rts = BCM43xx_MIN_RTS_THRESHOLD;
244         range->max_rts = BCM43xx_MAX_RTS_THRESHOLD;
245         range->min_frag = MIN_FRAG_THRESHOLD;
246         range->max_frag = MAX_FRAG_THRESHOLD;
247
248         range->encoding_size[0] = 5;
249         range->encoding_size[1] = 13;
250         range->num_encoding_sizes = 2;
251         range->max_encoding_tokens = WEP_KEYS;
252
253         range->we_version_compiled = WIRELESS_EXT;
254         range->we_version_source = BCM43xx_WX_VERSION;
255
256         range->enc_capa = IW_ENC_CAPA_WPA |
257                           IW_ENC_CAPA_WPA2 |
258                           IW_ENC_CAPA_CIPHER_TKIP |
259                           IW_ENC_CAPA_CIPHER_CCMP;
260
261         mutex_lock(&bcm->mutex);
262         phy = bcm43xx_current_phy(bcm);
263
264         range->num_bitrates = 0;
265         i = 0;
266         if (phy->type == BCM43xx_PHYTYPE_A ||
267             phy->type == BCM43xx_PHYTYPE_G) {
268                 range->num_bitrates = 8;
269                 range->bitrate[i++] = IEEE80211_OFDM_RATE_6MB;
270                 range->bitrate[i++] = IEEE80211_OFDM_RATE_9MB;
271                 range->bitrate[i++] = IEEE80211_OFDM_RATE_12MB;
272                 range->bitrate[i++] = IEEE80211_OFDM_RATE_18MB;
273                 range->bitrate[i++] = IEEE80211_OFDM_RATE_24MB;
274                 range->bitrate[i++] = IEEE80211_OFDM_RATE_36MB;
275                 range->bitrate[i++] = IEEE80211_OFDM_RATE_48MB;
276                 range->bitrate[i++] = IEEE80211_OFDM_RATE_54MB;
277         }
278         if (phy->type == BCM43xx_PHYTYPE_B ||
279             phy->type == BCM43xx_PHYTYPE_G) {
280                 range->num_bitrates += 4;
281                 range->bitrate[i++] = IEEE80211_CCK_RATE_1MB;
282                 range->bitrate[i++] = IEEE80211_CCK_RATE_2MB;
283                 range->bitrate[i++] = IEEE80211_CCK_RATE_5MB;
284                 range->bitrate[i++] = IEEE80211_CCK_RATE_11MB;
285         }
286
287         geo = ieee80211_get_geo(bcm->ieee);
288         range->num_channels = geo->a_channels + geo->bg_channels;
289         j = 0;
290         for (i = 0; i < geo->a_channels; i++) {
291                 if (j == IW_MAX_FREQUENCIES)
292                         break;
293                 range->freq[j].i = j + 1;
294                 range->freq[j].m = geo->a[i].freq;//FIXME?
295                 range->freq[j].e = 1;
296                 j++;
297         }
298         for (i = 0; i < geo->bg_channels; i++) {
299                 if (j == IW_MAX_FREQUENCIES)
300                         break;
301                 range->freq[j].i = j + 1;
302                 range->freq[j].m = geo->bg[i].freq;//FIXME?
303                 range->freq[j].e = 1;
304                 j++;
305         }
306         range->num_frequency = j;
307
308         mutex_unlock(&bcm->mutex);
309
310         return 0;
311 }
312
313 static int bcm43xx_wx_set_nick(struct net_device *net_dev,
314                                struct iw_request_info *info,
315                                union iwreq_data *data,
316                                char *extra)
317 {
318         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
319         size_t len;
320
321         mutex_lock(&bcm->mutex);
322         len =  min((size_t)data->data.length, (size_t)IW_ESSID_MAX_SIZE);
323         memcpy(bcm->nick, extra, len);
324         bcm->nick[len] = '\0';
325         mutex_unlock(&bcm->mutex);
326
327         return 0;
328 }
329
330 static int bcm43xx_wx_get_nick(struct net_device *net_dev,
331                                struct iw_request_info *info,
332                                union iwreq_data *data,
333                                char *extra)
334 {
335         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
336         size_t len;
337
338         mutex_lock(&bcm->mutex);
339         len = strlen(bcm->nick) + 1;
340         memcpy(extra, bcm->nick, len);
341         data->data.length = (__u16)len;
342         data->data.flags = 1;
343         mutex_unlock(&bcm->mutex);
344
345         return 0;
346 }
347
348 static int bcm43xx_wx_set_rts(struct net_device *net_dev,
349                               struct iw_request_info *info,
350                               union iwreq_data *data,
351                               char *extra)
352 {
353         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
354         unsigned long flags;
355         int err = -EINVAL;
356
357         mutex_lock(&bcm->mutex);
358         spin_lock_irqsave(&bcm->irq_lock, flags);
359         if (data->rts.disabled) {
360                 bcm->rts_threshold = BCM43xx_MAX_RTS_THRESHOLD;
361                 err = 0;
362         } else {
363                 if (data->rts.value >= BCM43xx_MIN_RTS_THRESHOLD &&
364                     data->rts.value <= BCM43xx_MAX_RTS_THRESHOLD) {
365                         bcm->rts_threshold = data->rts.value;
366                         err = 0;
367                 }
368         }
369         spin_unlock_irqrestore(&bcm->irq_lock, flags);
370         mutex_unlock(&bcm->mutex);
371
372         return err;
373 }
374
375 static int bcm43xx_wx_get_rts(struct net_device *net_dev,
376                               struct iw_request_info *info,
377                               union iwreq_data *data,
378                               char *extra)
379 {
380         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
381
382         mutex_lock(&bcm->mutex);
383         data->rts.value = bcm->rts_threshold;
384         data->rts.fixed = 0;
385         data->rts.disabled = (bcm->rts_threshold == BCM43xx_MAX_RTS_THRESHOLD);
386         mutex_unlock(&bcm->mutex);
387
388         return 0;
389 }
390
391 static int bcm43xx_wx_set_frag(struct net_device *net_dev,
392                                struct iw_request_info *info,
393                                union iwreq_data *data,
394                                char *extra)
395 {
396         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
397         unsigned long flags;
398         int err = -EINVAL;
399
400         mutex_lock(&bcm->mutex);
401         spin_lock_irqsave(&bcm->irq_lock, flags);
402         if (data->frag.disabled) {
403                 bcm->ieee->fts = MAX_FRAG_THRESHOLD;
404                 err = 0;
405         } else {
406                 if (data->frag.value >= MIN_FRAG_THRESHOLD &&
407                     data->frag.value <= MAX_FRAG_THRESHOLD) {
408                         bcm->ieee->fts = data->frag.value & ~0x1;
409                         err = 0;
410                 }
411         }
412         spin_unlock_irqrestore(&bcm->irq_lock, flags);
413         mutex_unlock(&bcm->mutex);
414
415         return err;
416 }
417
418 static int bcm43xx_wx_get_frag(struct net_device *net_dev,
419                                struct iw_request_info *info,
420                                union iwreq_data *data,
421                                char *extra)
422 {
423         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
424
425         mutex_lock(&bcm->mutex);
426         data->frag.value = bcm->ieee->fts;
427         data->frag.fixed = 0;
428         data->frag.disabled = (bcm->ieee->fts == MAX_FRAG_THRESHOLD);
429         mutex_unlock(&bcm->mutex);
430
431         return 0;
432 }
433
434 static int bcm43xx_wx_set_xmitpower(struct net_device *net_dev,
435                                     struct iw_request_info *info,
436                                     union iwreq_data *data,
437                                     char *extra)
438 {
439         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
440         struct bcm43xx_radioinfo *radio;
441         struct bcm43xx_phyinfo *phy;
442         unsigned long flags;
443         int err = -ENODEV;
444         u16 maxpower;
445
446         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM) {
447                 printk(PFX KERN_ERR "TX power not in dBm.\n");
448                 return -EOPNOTSUPP;
449         }
450
451         mutex_lock(&bcm->mutex);
452         spin_lock_irqsave(&bcm->irq_lock, flags);
453         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)
454                 goto out_unlock;
455         radio = bcm43xx_current_radio(bcm);
456         phy = bcm43xx_current_phy(bcm);
457         if (data->txpower.disabled != (!(radio->enabled))) {
458                 if (data->txpower.disabled)
459                         bcm43xx_radio_turn_off(bcm);
460                 else
461                         bcm43xx_radio_turn_on(bcm);
462         }
463         if (data->txpower.value > 0) {
464                 /* desired and maxpower dBm values are in Q5.2 */
465                 if (phy->type == BCM43xx_PHYTYPE_A)
466                         maxpower = bcm->sprom.maxpower_aphy;
467                 else
468                         maxpower = bcm->sprom.maxpower_bgphy;
469                 radio->txpower_desired = limit_value(data->txpower.value << 2,
470                                                      0, maxpower);
471                 bcm43xx_phy_xmitpower(bcm);
472         }
473         err = 0;
474
475 out_unlock:
476         spin_unlock_irqrestore(&bcm->irq_lock, flags);
477         mutex_unlock(&bcm->mutex);
478
479         return err;
480 }
481
482 static int bcm43xx_wx_get_xmitpower(struct net_device *net_dev,
483                                     struct iw_request_info *info,
484                                     union iwreq_data *data,
485                                     char *extra)
486 {
487         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
488         struct bcm43xx_radioinfo *radio;
489         int err = -ENODEV;
490
491         mutex_lock(&bcm->mutex);
492         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)
493                 goto out_unlock;
494         radio = bcm43xx_current_radio(bcm);
495         /* desired dBm value is in Q5.2 */
496         data->txpower.value = radio->txpower_desired >> 2;
497         data->txpower.fixed = 1;
498         data->txpower.flags = IW_TXPOW_DBM;
499         data->txpower.disabled = !(radio->enabled);
500
501         err = 0;
502 out_unlock:
503         mutex_unlock(&bcm->mutex);
504
505         return err;
506 }
507
508 static int bcm43xx_wx_set_encoding(struct net_device *net_dev,
509                                    struct iw_request_info *info,
510                                    union iwreq_data *data,
511                                    char *extra)
512 {
513         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
514         int err;
515
516         err = ieee80211_wx_set_encode(bcm->ieee, info, data, extra);
517
518         return err;
519 }
520
521 static int bcm43xx_wx_set_encodingext(struct net_device *net_dev,
522                                    struct iw_request_info *info,
523                                    union iwreq_data *data,
524                                    char *extra)
525 {
526         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
527         int err;
528
529         err = ieee80211_wx_set_encodeext(bcm->ieee, info, data, extra);
530
531         return err;
532 }
533
534 static int bcm43xx_wx_get_encoding(struct net_device *net_dev,
535                                    struct iw_request_info *info,
536                                    union iwreq_data *data,
537                                    char *extra)
538 {
539         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
540         int err;
541
542         err = ieee80211_wx_get_encode(bcm->ieee, info, data, extra);
543
544         return err;
545 }
546
547 static int bcm43xx_wx_get_encodingext(struct net_device *net_dev,
548                                    struct iw_request_info *info,
549                                    union iwreq_data *data,
550                                    char *extra)
551 {
552         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
553         int err;
554
555         err = ieee80211_wx_get_encodeext(bcm->ieee, info, data, extra);
556
557         return err;
558 }
559
560 static int bcm43xx_wx_set_interfmode(struct net_device *net_dev,
561                                      struct iw_request_info *info,
562                                      union iwreq_data *data,
563                                      char *extra)
564 {
565         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
566         unsigned long flags;
567         int mode, err = 0;
568
569         mode = *((int *)extra);
570         switch (mode) {
571         case 0:
572                 mode = BCM43xx_RADIO_INTERFMODE_NONE;
573                 break;
574         case 1:
575                 mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
576                 break;
577         case 2:
578                 mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
579                 break;
580         case 3:
581                 mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
582                 break;
583         default:
584                 printk(KERN_ERR PFX "set_interfmode allowed parameters are: "
585                                     "0 => None,  1 => Non-WLAN,  2 => WLAN,  "
586                                     "3 => Auto-WLAN\n");
587                 return -EINVAL;
588         }
589
590         mutex_lock(&bcm->mutex);
591         spin_lock_irqsave(&bcm->irq_lock, flags);
592         if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
593                 err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
594                 if (err) {
595                         printk(KERN_ERR PFX "Interference Mitigation not "
596                                             "supported by device\n");
597                 }
598         } else {
599                 if (mode == BCM43xx_RADIO_INTERFMODE_AUTOWLAN) {
600                         printk(KERN_ERR PFX "Interference Mitigation mode Auto-WLAN "
601                                             "not supported while the interface is down.\n");
602                         err = -ENODEV;
603                 } else
604                         bcm43xx_current_radio(bcm)->interfmode = mode;
605         }
606         spin_unlock_irqrestore(&bcm->irq_lock, flags);
607         mutex_unlock(&bcm->mutex);
608
609         return err;
610 }
611
612 static int bcm43xx_wx_get_interfmode(struct net_device *net_dev,
613                                      struct iw_request_info *info,
614                                      union iwreq_data *data,
615                                      char *extra)
616 {
617         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
618         int mode;
619
620         mutex_lock(&bcm->mutex);
621         mode = bcm43xx_current_radio(bcm)->interfmode;
622         mutex_unlock(&bcm->mutex);
623
624         switch (mode) {
625         case BCM43xx_RADIO_INTERFMODE_NONE:
626                 strncpy(extra, "0 (No Interference Mitigation)", MAX_WX_STRING);
627                 break;
628         case BCM43xx_RADIO_INTERFMODE_NONWLAN:
629                 strncpy(extra, "1 (Non-WLAN Interference Mitigation)", MAX_WX_STRING);
630                 break;
631         case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
632                 strncpy(extra, "2 (WLAN Interference Mitigation)", MAX_WX_STRING);
633                 break;
634         default:
635                 assert(0);
636         }
637         data->data.length = strlen(extra) + 1;
638
639         return 0;
640 }
641
642 static int bcm43xx_wx_set_shortpreamble(struct net_device *net_dev,
643                                         struct iw_request_info *info,
644                                         union iwreq_data *data,
645                                         char *extra)
646 {
647         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
648         unsigned long flags;
649         int on;
650
651         on = *((int *)extra);
652         mutex_lock(&bcm->mutex);
653         spin_lock_irqsave(&bcm->irq_lock, flags);
654         bcm->short_preamble = !!on;
655         spin_unlock_irqrestore(&bcm->irq_lock, flags);
656         mutex_unlock(&bcm->mutex);
657
658         return 0;
659 }
660
661 static int bcm43xx_wx_get_shortpreamble(struct net_device *net_dev,
662                                         struct iw_request_info *info,
663                                         union iwreq_data *data,
664                                         char *extra)
665 {
666         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
667         int on;
668
669         mutex_lock(&bcm->mutex);
670         on = bcm->short_preamble;
671         mutex_unlock(&bcm->mutex);
672
673         if (on)
674                 strncpy(extra, "1 (Short Preamble enabled)", MAX_WX_STRING);
675         else
676                 strncpy(extra, "0 (Short Preamble disabled)", MAX_WX_STRING);
677         data->data.length = strlen(extra) + 1;
678
679         return 0;
680 }
681
682 static int bcm43xx_wx_set_swencryption(struct net_device *net_dev,
683                                        struct iw_request_info *info,
684                                        union iwreq_data *data,
685                                        char *extra)
686 {
687         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
688         unsigned long flags;
689         int on;
690         
691         on = *((int *)extra);
692
693         mutex_lock(&bcm->mutex);
694         spin_lock_irqsave(&bcm->irq_lock, flags);
695         bcm->ieee->host_encrypt = !!on;
696         bcm->ieee->host_decrypt = !!on;
697         bcm->ieee->host_build_iv = !on;
698         spin_unlock_irqrestore(&bcm->irq_lock, flags);
699         mutex_unlock(&bcm->mutex);
700
701         return 0;
702 }
703
704 static int bcm43xx_wx_get_swencryption(struct net_device *net_dev,
705                                        struct iw_request_info *info,
706                                        union iwreq_data *data,
707                                        char *extra)
708 {
709         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
710         int on;
711
712         mutex_lock(&bcm->mutex);
713         on = bcm->ieee->host_encrypt;
714         mutex_unlock(&bcm->mutex);
715
716         if (on)
717                 strncpy(extra, "1 (SW encryption enabled) ", MAX_WX_STRING);
718         else
719                 strncpy(extra, "0 (SW encryption disabled) ", MAX_WX_STRING);
720         data->data.length = strlen(extra + 1);
721
722         return 0;
723 }
724
725 /* Enough buffer to hold a hexdump of the sprom data. */
726 #define SPROM_BUFFERSIZE        512
727
728 static int sprom2hex(const u16 *sprom, char *dump)
729 {
730         int i, pos = 0;
731
732         for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
733                 pos += snprintf(dump + pos, SPROM_BUFFERSIZE - pos - 1,
734                                 "%04X", swab16(sprom[i]) & 0xFFFF);
735         }
736
737         return pos + 1;
738 }
739
740 static int hex2sprom(u16 *sprom, const char *dump, unsigned int len)
741 {
742         char tmp[5] = { 0 };
743         int cnt = 0;
744         unsigned long parsed;
745
746         if (len < BCM43xx_SPROM_SIZE * sizeof(u16) * 2)
747                 return -EINVAL;
748         while (cnt < BCM43xx_SPROM_SIZE) {
749                 memcpy(tmp, dump, 4);
750                 dump += 4;
751                 parsed = simple_strtoul(tmp, NULL, 16);
752                 sprom[cnt++] = swab16((u16)parsed);
753         }
754
755         return 0;
756 }
757
758 static int bcm43xx_wx_sprom_read(struct net_device *net_dev,
759                                  struct iw_request_info *info,
760                                  union iwreq_data *data,
761                                  char *extra)
762 {
763         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
764         int err = -EPERM;
765         u16 *sprom;
766         unsigned long flags;
767
768         if (!capable(CAP_SYS_RAWIO))
769                 goto out;
770
771         err = -ENOMEM;
772         sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
773                         GFP_KERNEL);
774         if (!sprom)
775                 goto out;
776
777         mutex_lock(&bcm->mutex);
778         spin_lock_irqsave(&bcm->irq_lock, flags);
779         err = -ENODEV;
780         if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED)
781                 err = bcm43xx_sprom_read(bcm, sprom);
782         spin_unlock_irqrestore(&bcm->irq_lock, flags);
783         mutex_unlock(&bcm->mutex);
784         if (!err)
785                 data->data.length = sprom2hex(sprom, extra);
786         kfree(sprom);
787 out:
788         return err;
789 }
790
791 static int bcm43xx_wx_sprom_write(struct net_device *net_dev,
792                                   struct iw_request_info *info,
793                                   union iwreq_data *data,
794                                   char *extra)
795 {
796         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
797         int err = -EPERM;
798         u16 *sprom;
799         unsigned long flags;
800         char *input;
801         unsigned int len;
802
803         if (!capable(CAP_SYS_RAWIO))
804                 goto out;
805
806         err = -ENOMEM;
807         sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
808                         GFP_KERNEL);
809         if (!sprom)
810                 goto out;
811
812         len = data->data.length;
813         extra[len - 1] = '\0';
814         input = strchr(extra, ':');
815         if (input) {
816                 input++;
817                 len -= input - extra;
818         } else
819                 input = extra;
820         err = hex2sprom(sprom, input, len);
821         if (err)
822                 goto out_kfree;
823
824         mutex_lock(&bcm->mutex);
825         spin_lock_irqsave(&bcm->irq_lock, flags);
826         spin_lock(&bcm->leds_lock);
827         err = -ENODEV;
828         if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED)
829                 err = bcm43xx_sprom_write(bcm, sprom);
830         spin_unlock(&bcm->leds_lock);
831         spin_unlock_irqrestore(&bcm->irq_lock, flags);
832         mutex_unlock(&bcm->mutex);
833 out_kfree:
834         kfree(sprom);
835 out:
836         return err;
837 }
838
839 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
840
841 static struct iw_statistics *bcm43xx_get_wireless_stats(struct net_device *net_dev)
842 {
843         struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
844         struct ieee80211softmac_device *mac = ieee80211_priv(net_dev);
845         struct iw_statistics *wstats;
846         struct ieee80211_network *network = NULL;
847         static int tmp_level = 0;
848         unsigned long flags;
849
850         wstats = &bcm->stats.wstats;
851         if (!mac->associated) {
852                 wstats->miss.beacon = 0;
853 //              bcm->ieee->ieee_stats.tx_retry_limit_exceeded = 0; // FIXME: should this be cleared here?
854                 wstats->discard.retries = 0;
855 //              bcm->ieee->ieee_stats.tx_discards_wrong_sa = 0; // FIXME: same question
856                 wstats->discard.nwid = 0;
857 //              bcm->ieee->ieee_stats.rx_discards_undecryptable = 0; // FIXME: ditto
858                 wstats->discard.code = 0;
859 //              bcm->ieee->ieee_stats.rx_fragments = 0;  // FIXME: same here
860                 wstats->discard.fragment = 0;
861                 wstats->discard.misc = 0;
862                 wstats->qual.qual = 0;
863                 wstats->qual.level = 0;
864                 wstats->qual.noise = 0;
865                 wstats->qual.updated = 7;
866                 wstats->qual.updated |= IW_QUAL_ALL_UPDATED;
867                 return wstats;
868         }
869         /* fill in the real statistics when iface associated */
870         spin_lock_irqsave(&mac->ieee->lock, flags);
871         list_for_each_entry(network, &mac->ieee->network_list, list) {
872                 if (!memcmp(mac->associnfo.bssid, network->bssid, ETH_ALEN)) {
873                         if (!tmp_level)         /* get initial value */
874                                 tmp_level = network->stats.rssi;
875                         else                    /* smooth results */
876                                 tmp_level = (7 * tmp_level + network->stats.rssi)/8;
877                         break;
878                 }
879         }
880         spin_unlock_irqrestore(&mac->ieee->lock, flags);
881         wstats->qual.level = tmp_level;
882         wstats->qual.qual = 100 + tmp_level - RX_POWER_MAX; // TODO: get the real signal quality
883         wstats->qual.noise = bcm->stats.noise;
884         wstats->qual.updated = IW_QUAL_ALL_UPDATED;
885         wstats->discard.code = bcm->ieee->ieee_stats.rx_discards_undecryptable;
886         wstats->discard.retries = bcm->ieee->ieee_stats.tx_retry_limit_exceeded;
887         wstats->discard.nwid = bcm->ieee->ieee_stats.tx_discards_wrong_sa;
888         wstats->discard.fragment = bcm->ieee->ieee_stats.rx_fragments;
889         wstats->discard.misc = 0;       // FIXME
890         wstats->miss.beacon = 0;        // FIXME
891         return wstats;
892 }
893
894
895 #ifdef WX
896 # undef WX
897 #endif
898 #define WX(ioctl)  [(ioctl) - SIOCSIWCOMMIT]
899 static const iw_handler bcm43xx_wx_handlers[] = {
900         /* Wireless Identification */
901         WX(SIOCGIWNAME)         = bcm43xx_wx_get_name,
902         /* Basic operations */
903         WX(SIOCSIWFREQ)         = bcm43xx_wx_set_channelfreq,
904         WX(SIOCGIWFREQ)         = bcm43xx_wx_get_channelfreq,
905         WX(SIOCSIWMODE)         = bcm43xx_wx_set_mode,
906         WX(SIOCGIWMODE)         = bcm43xx_wx_get_mode,
907         /* Informative stuff */
908         WX(SIOCGIWRANGE)        = bcm43xx_wx_get_rangeparams,
909         /* Access Point manipulation */
910         WX(SIOCSIWAP)           = ieee80211softmac_wx_set_wap,
911         WX(SIOCGIWAP)           = ieee80211softmac_wx_get_wap,
912         WX(SIOCSIWSCAN)         = ieee80211softmac_wx_trigger_scan,
913         WX(SIOCGIWSCAN)         = ieee80211softmac_wx_get_scan_results,
914         /* 802.11 specific support */
915         WX(SIOCSIWESSID)        = ieee80211softmac_wx_set_essid,
916         WX(SIOCGIWESSID)        = ieee80211softmac_wx_get_essid,
917         WX(SIOCSIWNICKN)        = bcm43xx_wx_set_nick,
918         WX(SIOCGIWNICKN)        = bcm43xx_wx_get_nick,
919         /* Other parameters */
920         WX(SIOCSIWRATE)         = ieee80211softmac_wx_set_rate,
921         WX(SIOCGIWRATE)         = ieee80211softmac_wx_get_rate,
922         WX(SIOCSIWRTS)          = bcm43xx_wx_set_rts,
923         WX(SIOCGIWRTS)          = bcm43xx_wx_get_rts,
924         WX(SIOCSIWFRAG)         = bcm43xx_wx_set_frag,
925         WX(SIOCGIWFRAG)         = bcm43xx_wx_get_frag,
926         WX(SIOCSIWTXPOW)        = bcm43xx_wx_set_xmitpower,
927         WX(SIOCGIWTXPOW)        = bcm43xx_wx_get_xmitpower,
928 //TODO  WX(SIOCSIWRETRY)        = bcm43xx_wx_set_retry,
929 //TODO  WX(SIOCGIWRETRY)        = bcm43xx_wx_get_retry,
930         /* Encoding */
931         WX(SIOCSIWENCODE)       = bcm43xx_wx_set_encoding,
932         WX(SIOCGIWENCODE)       = bcm43xx_wx_get_encoding,
933         WX(SIOCSIWENCODEEXT)    = bcm43xx_wx_set_encodingext,
934         WX(SIOCGIWENCODEEXT)    = bcm43xx_wx_get_encodingext,
935         /* Power saving */
936 //TODO  WX(SIOCSIWPOWER)        = bcm43xx_wx_set_power,
937 //TODO  WX(SIOCGIWPOWER)        = bcm43xx_wx_get_power,
938         WX(SIOCSIWGENIE)        = ieee80211softmac_wx_set_genie,
939         WX(SIOCGIWGENIE)        = ieee80211softmac_wx_get_genie,
940         WX(SIOCSIWAUTH)         = ieee80211_wx_set_auth,
941         WX(SIOCGIWAUTH)         = ieee80211_wx_get_auth,
942 };
943 #undef WX
944
945 static const iw_handler bcm43xx_priv_wx_handlers[] = {
946         /* Set Interference Mitigation Mode. */
947         bcm43xx_wx_set_interfmode,
948         /* Get Interference Mitigation Mode. */
949         bcm43xx_wx_get_interfmode,
950         /* Enable/Disable Short Preamble mode. */
951         bcm43xx_wx_set_shortpreamble,
952         /* Get Short Preamble mode. */
953         bcm43xx_wx_get_shortpreamble,
954         /* Enable/Disable Software Encryption mode */
955         bcm43xx_wx_set_swencryption,
956         /* Get Software Encryption mode */
957         bcm43xx_wx_get_swencryption,
958         /* Write SRPROM data. */
959         bcm43xx_wx_sprom_write,
960         /* Read SPROM data. */
961         bcm43xx_wx_sprom_read,
962 };
963
964 #define PRIV_WX_SET_INTERFMODE          (SIOCIWFIRSTPRIV + 0)
965 #define PRIV_WX_GET_INTERFMODE          (SIOCIWFIRSTPRIV + 1)
966 #define PRIV_WX_SET_SHORTPREAMBLE       (SIOCIWFIRSTPRIV + 2)
967 #define PRIV_WX_GET_SHORTPREAMBLE       (SIOCIWFIRSTPRIV + 3)
968 #define PRIV_WX_SET_SWENCRYPTION        (SIOCIWFIRSTPRIV + 4)
969 #define PRIV_WX_GET_SWENCRYPTION        (SIOCIWFIRSTPRIV + 5)
970 #define PRIV_WX_SPROM_WRITE             (SIOCIWFIRSTPRIV + 6)
971 #define PRIV_WX_SPROM_READ              (SIOCIWFIRSTPRIV + 7)
972
973 #define PRIV_WX_DUMMY(ioctl)    \
974         {                                       \
975                 .cmd            = (ioctl),      \
976                 .name           = "__unused"    \
977         }
978
979 static const struct iw_priv_args bcm43xx_priv_wx_args[] = {
980         {
981                 .cmd            = PRIV_WX_SET_INTERFMODE,
982                 .set_args       = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
983                 .name           = "set_interfmode",
984         },
985         {
986                 .cmd            = PRIV_WX_GET_INTERFMODE,
987                 .get_args       = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
988                 .name           = "get_interfmode",
989         },
990         {
991                 .cmd            = PRIV_WX_SET_SHORTPREAMBLE,
992                 .set_args       = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
993                 .name           = "set_shortpreamb",
994         },
995         {
996                 .cmd            = PRIV_WX_GET_SHORTPREAMBLE,
997                 .get_args       = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
998                 .name           = "get_shortpreamb",
999         },
1000         {
1001                 .cmd            = PRIV_WX_SET_SWENCRYPTION,
1002                 .set_args       = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1003                 .name           = "set_swencrypt",
1004         },
1005         {
1006                 .cmd            = PRIV_WX_GET_SWENCRYPTION,
1007                 .get_args       = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
1008                 .name           = "get_swencrypt",
1009         },
1010         {
1011                 .cmd            = PRIV_WX_SPROM_WRITE,
1012                 .set_args       = IW_PRIV_TYPE_CHAR | SPROM_BUFFERSIZE,
1013                 .name           = "write_sprom",
1014         },
1015         {
1016                 .cmd            = PRIV_WX_SPROM_READ,
1017                 .get_args       = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | SPROM_BUFFERSIZE,
1018                 .name           = "read_sprom",
1019         },
1020 };
1021
1022 const struct iw_handler_def bcm43xx_wx_handlers_def = {
1023         .standard               = bcm43xx_wx_handlers,
1024         .num_standard           = ARRAY_SIZE(bcm43xx_wx_handlers),
1025         .num_private            = ARRAY_SIZE(bcm43xx_priv_wx_handlers),
1026         .num_private_args       = ARRAY_SIZE(bcm43xx_priv_wx_args),
1027         .private                = bcm43xx_priv_wx_handlers,
1028         .private_args           = bcm43xx_priv_wx_args,
1029         .get_wireless_stats     = bcm43xx_get_wireless_stats,
1030 };