bcm43xx_wx.c 26.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*

  Broadcom BCM43xx wireless driver

  Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
                     Stefano Brivio <st3@riseup.net>
                     Michael Buesch <mbuesch@freenet.de>
                     Danny van Dyk <kugelfang@gentoo.org>
                     Andreas Jaggi <andreas.jaggi@waterwave.ch>

  Some parts of the code in this file are derived from the ipw2200
  driver  Copyright(c) 2003 - 2004 Intel Corporation.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  Boston, MA 02110-1301, USA.

*/

#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <net/ieee80211softmac.h>
#include <net/ieee80211softmac_wx.h>
#include <linux/capability.h>
#include <linux/sched.h> /* for capable() */
#include <linux/delay.h>

#include "bcm43xx.h"
#include "bcm43xx_wx.h"
#include "bcm43xx_main.h"
#include "bcm43xx_radio.h"
43
#include "bcm43xx_phy.h"
44

45 46 47 48

/* The WIRELESS_EXT version, which is implemented by this driver. */
#define BCM43xx_WX_VERSION	18

49 50 51 52 53 54 55 56 57
#define MAX_WX_STRING		80


static int bcm43xx_wx_get_name(struct net_device *net_dev,
                               struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
58
	int i;
59 60 61 62
	struct bcm43xx_phyinfo *phy;
	char suffix[7] = { 0 };
	int have_a = 0, have_b = 0, have_g = 0;

63
	mutex_lock(&bcm->mutex);
64 65
	for (i = 0; i < bcm->nr_80211_available; i++) {
		phy = &(bcm->core_80211_ext[i].phy);
66 67 68 69 70 71 72 73 74 75 76 77 78
		switch (phy->type) {
		case BCM43xx_PHYTYPE_A:
			have_a = 1;
			break;
		case BCM43xx_PHYTYPE_G:
			have_g = 1;
		case BCM43xx_PHYTYPE_B:
			have_b = 1;
			break;
		default:
			assert(0);
		}
	}
79
	mutex_unlock(&bcm->mutex);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

	i = 0;
	if (have_a) {
		suffix[i++] = 'a';
		suffix[i++] = '/';
	}
	if (have_b) {
		suffix[i++] = 'b';
		suffix[i++] = '/';
	}
	if (have_g) {
		suffix[i++] = 'g';
		suffix[i++] = '/';
	}
	if (i != 0) 
		suffix[i - 1] = '\0';

	snprintf(data->name, IFNAMSIZ, "IEEE 802.11%s", suffix);

	return 0;
}

static int bcm43xx_wx_set_channelfreq(struct net_device *net_dev,
				      struct iw_request_info *info,
				      union iwreq_data *data,
				      char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	u8 channel;
	int freq;
111
	int err = -EINVAL;
112

113 114 115
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);

116 117 118 119 120 121 122
	if ((data->freq.m >= 0) && (data->freq.m <= 1000)) {
		channel = data->freq.m;
		freq = bcm43xx_channel_to_freq(bcm, channel);
	} else {
		channel = bcm43xx_freq_to_channel(bcm, data->freq.m);
		freq = data->freq.m;
	}
123
	if (!ieee80211_is_valid_channel(bcm->ieee, channel))
124
		goto out_unlock;
125
	if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
126 127 128 129
		//ieee80211softmac_disassoc(softmac, $REASON);
		bcm43xx_mac_suspend(bcm);
		err = bcm43xx_radio_selectchannel(bcm, channel, 0);
		bcm43xx_mac_enable(bcm);
130
	} else {
131
		bcm43xx_current_radio(bcm)->initial_channel = channel;
132 133 134
		err = 0;
	}
out_unlock:
135 136
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
137 138 139 140 141 142 143 144 145 146

	return err;
}

static int bcm43xx_wx_get_channelfreq(struct net_device *net_dev,
				      struct iw_request_info *info,
				      union iwreq_data *data,
				      char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
147
	struct bcm43xx_radioinfo *radio;
148 149 150
	int err = -ENODEV;
	u16 channel;

151
	mutex_lock(&bcm->mutex);
152 153
	radio = bcm43xx_current_radio(bcm);
	channel = radio->channel;
154
	if (channel == 0xFF) {
155
		channel = radio->initial_channel;
156 157 158 159 160 161 162 163 164 165
		if (channel == 0xFF)
			goto out_unlock;
	}
	assert(channel > 0 && channel <= 1000);
	data->freq.e = 1;
	data->freq.m = bcm43xx_channel_to_freq(bcm, channel) * 100000;
	data->freq.flags = 1;

	err = 0;
out_unlock:
166
	mutex_unlock(&bcm->mutex);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

	return err;
}

static int bcm43xx_wx_set_mode(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int mode;

	mode = data->mode;
	if (mode == IW_MODE_AUTO)
		mode = BCM43xx_INITIAL_IWMODE;

184 185
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
186
	if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
187 188 189 190
		if (bcm->ieee->iw_mode != mode)
			bcm43xx_set_iwmode(bcm, mode);
	} else
		bcm->ieee->iw_mode = mode;
191 192
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
193 194 195 196 197 198 199 200 201 202 203

	return 0;
}

static int bcm43xx_wx_get_mode(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);

204
	mutex_lock(&bcm->mutex);
205
	data->mode = bcm->ieee->iw_mode;
206
	mutex_unlock(&bcm->mutex);
207 208 209 210 211 212 213 214 215 216 217 218 219

	return 0;
}

static int bcm43xx_wx_get_rangeparams(struct net_device *net_dev,
				      struct iw_request_info *info,
				      union iwreq_data *data,
				      char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	struct iw_range *range = (struct iw_range *)extra;
	const struct ieee80211_geo *geo;
	int i, j;
220
	struct bcm43xx_phyinfo *phy;
221 222 223 224 225 226 227 228 229 230

	data->data.length = sizeof(*range);
	memset(range, 0, sizeof(*range));

	//TODO: What about 802.11b?
	/* 54Mb/s == ~27Mb/s payload throughput (802.11g) */
	range->throughput = 27 * 1000 * 1000;

	range->max_qual.qual = 100;
	/* TODO: Real max RSSI */
231 232
	range->max_qual.level = 3;
	range->max_qual.noise = 100;
233 234 235
	range->max_qual.updated = 7;

	range->avg_qual.qual = 70;
236 237
	range->avg_qual.level = 2;
	range->avg_qual.noise = 40;
238 239 240 241 242 243 244 245 246 247 248 249 250
	range->avg_qual.updated = 7;

	range->min_rts = BCM43xx_MIN_RTS_THRESHOLD;
	range->max_rts = BCM43xx_MAX_RTS_THRESHOLD;
	range->min_frag = MIN_FRAG_THRESHOLD;
	range->max_frag = MAX_FRAG_THRESHOLD;

	range->encoding_size[0] = 5;
	range->encoding_size[1] = 13;
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = WEP_KEYS;

	range->we_version_compiled = WIRELESS_EXT;
251 252 253 254 255 256
	range->we_version_source = BCM43xx_WX_VERSION;

	range->enc_capa = IW_ENC_CAPA_WPA |
			  IW_ENC_CAPA_WPA2 |
			  IW_ENC_CAPA_CIPHER_TKIP |
			  IW_ENC_CAPA_CIPHER_CCMP;
257

258
	mutex_lock(&bcm->mutex);
259
	phy = bcm43xx_current_phy(bcm);
260 261 262

	range->num_bitrates = 0;
	i = 0;
263 264
	if (phy->type == BCM43xx_PHYTYPE_A ||
	    phy->type == BCM43xx_PHYTYPE_G) {
265 266 267 268 269 270 271 272 273 274
		range->num_bitrates = 8;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_6MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_9MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_12MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_18MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_24MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_36MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_48MB;
		range->bitrate[i++] = IEEE80211_OFDM_RATE_54MB;
	}
275 276
	if (phy->type == BCM43xx_PHYTYPE_B ||
	    phy->type == BCM43xx_PHYTYPE_G) {
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		range->num_bitrates += 4;
		range->bitrate[i++] = IEEE80211_CCK_RATE_1MB;
		range->bitrate[i++] = IEEE80211_CCK_RATE_2MB;
		range->bitrate[i++] = IEEE80211_CCK_RATE_5MB;
		range->bitrate[i++] = IEEE80211_CCK_RATE_11MB;
	}

	geo = ieee80211_get_geo(bcm->ieee);
	range->num_channels = geo->a_channels + geo->bg_channels;
	j = 0;
	for (i = 0; i < geo->a_channels; i++) {
		if (j == IW_MAX_FREQUENCIES)
			break;
		range->freq[j].i = j + 1;
		range->freq[j].m = geo->a[i].freq;//FIXME?
		range->freq[j].e = 1;
		j++;
	}
	for (i = 0; i < geo->bg_channels; i++) {
		if (j == IW_MAX_FREQUENCIES)
			break;
		range->freq[j].i = j + 1;
		range->freq[j].m = geo->bg[i].freq;//FIXME?
		range->freq[j].e = 1;
		j++;
	}
	range->num_frequency = j;

305
	mutex_unlock(&bcm->mutex);
306 307 308 309 310 311 312 313 314 315 316 317

	return 0;
}

static int bcm43xx_wx_set_nick(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	size_t len;

318
	mutex_lock(&bcm->mutex);
319 320 321
	len =  min((size_t)data->data.length, (size_t)IW_ESSID_MAX_SIZE);
	memcpy(bcm->nick, extra, len);
	bcm->nick[len] = '\0';
322
	mutex_unlock(&bcm->mutex);
323 324 325 326 327 328 329 330 331 332 333 334

	return 0;
}

static int bcm43xx_wx_get_nick(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	size_t len;

335
	mutex_lock(&bcm->mutex);
336 337 338 339
	len = strlen(bcm->nick) + 1;
	memcpy(extra, bcm->nick, len);
	data->data.length = (__u16)len;
	data->data.flags = 1;
340
	mutex_unlock(&bcm->mutex);
341 342 343 344 345 346 347 348 349 350 351 352 353

	return 0;
}

static int bcm43xx_wx_set_rts(struct net_device *net_dev,
			      struct iw_request_info *info,
			      union iwreq_data *data,
			      char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int err = -EINVAL;

354 355
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
356 357 358 359 360 361 362 363 364 365
	if (data->rts.disabled) {
		bcm->rts_threshold = BCM43xx_MAX_RTS_THRESHOLD;
		err = 0;
	} else {
		if (data->rts.value >= BCM43xx_MIN_RTS_THRESHOLD &&
		    data->rts.value <= BCM43xx_MAX_RTS_THRESHOLD) {
			bcm->rts_threshold = data->rts.value;
			err = 0;
		}
	}
366 367
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
368 369 370 371 372 373 374 375 376 377 378

	return err;
}

static int bcm43xx_wx_get_rts(struct net_device *net_dev,
			      struct iw_request_info *info,
			      union iwreq_data *data,
			      char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);

379
	mutex_lock(&bcm->mutex);
380 381 382
	data->rts.value = bcm->rts_threshold;
	data->rts.fixed = 0;
	data->rts.disabled = (bcm->rts_threshold == BCM43xx_MAX_RTS_THRESHOLD);
383
	mutex_unlock(&bcm->mutex);
384 385 386 387 388 389 390 391 392 393 394 395 396

	return 0;
}

static int bcm43xx_wx_set_frag(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int err = -EINVAL;

397 398
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
399 400 401 402 403 404 405 406 407 408
	if (data->frag.disabled) {
		bcm->ieee->fts = MAX_FRAG_THRESHOLD;
		err = 0;
	} else {
		if (data->frag.value >= MIN_FRAG_THRESHOLD &&
		    data->frag.value <= MAX_FRAG_THRESHOLD) {
			bcm->ieee->fts = data->frag.value & ~0x1;
			err = 0;
		}
	}
409 410
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
411 412 413 414 415 416 417 418 419 420 421

	return err;
}

static int bcm43xx_wx_get_frag(struct net_device *net_dev,
			       struct iw_request_info *info,
			       union iwreq_data *data,
			       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);

422
	mutex_lock(&bcm->mutex);
423 424 425
	data->frag.value = bcm->ieee->fts;
	data->frag.fixed = 0;
	data->frag.disabled = (bcm->ieee->fts == MAX_FRAG_THRESHOLD);
426
	mutex_unlock(&bcm->mutex);
427 428 429 430 431 432 433 434 435 436

	return 0;
}

static int bcm43xx_wx_set_xmitpower(struct net_device *net_dev,
				    struct iw_request_info *info,
				    union iwreq_data *data,
				    char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
437 438
	struct bcm43xx_radioinfo *radio;
	struct bcm43xx_phyinfo *phy;
439 440
	unsigned long flags;
	int err = -ENODEV;
441
	u16 maxpower;
442

443 444 445 446 447
	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM) {
		printk(PFX KERN_ERR "TX power not in dBm.\n");
		return -EOPNOTSUPP;
	}

448 449
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
450
	if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)
451
		goto out_unlock;
452 453
	radio = bcm43xx_current_radio(bcm);
	phy = bcm43xx_current_phy(bcm);
454 455
	if (data->txpower.disabled != (!(radio->enabled))) {
		if (data->txpower.disabled)
456 457 458 459
			bcm43xx_radio_turn_off(bcm);
		else
			bcm43xx_radio_turn_on(bcm);
	}
460 461 462 463 464 465 466 467 468 469
	if (data->txpower.value > 0) {
		/* desired and maxpower dBm values are in Q5.2 */
		if (phy->type == BCM43xx_PHYTYPE_A)
			maxpower = bcm->sprom.maxpower_aphy;
		else
			maxpower = bcm->sprom.maxpower_bgphy;
		radio->txpower_desired = limit_value(data->txpower.value << 2,
						     0, maxpower);
		bcm43xx_phy_xmitpower(bcm);
	}
470 471 472
	err = 0;

out_unlock:
473 474
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
475 476 477 478 479 480 481 482 483 484

	return err;
}

static int bcm43xx_wx_get_xmitpower(struct net_device *net_dev,
				    struct iw_request_info *info,
				    union iwreq_data *data,
				    char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
485 486
	struct bcm43xx_radioinfo *radio;
	int err = -ENODEV;
487

488
	mutex_lock(&bcm->mutex);
489
	if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)
490
		goto out_unlock;
491
	radio = bcm43xx_current_radio(bcm);
492 493 494 495 496 497 498 499
	/* desired dBm value is in Q5.2 */
	data->txpower.value = radio->txpower_desired >> 2;
	data->txpower.fixed = 1;
	data->txpower.flags = IW_TXPOW_DBM;
	data->txpower.disabled = !(radio->enabled);

	err = 0;
out_unlock:
500
	mutex_unlock(&bcm->mutex);
501

502
	return err;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
}

static int bcm43xx_wx_set_encoding(struct net_device *net_dev,
				   struct iw_request_info *info,
				   union iwreq_data *data,
				   char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int err;

	err = ieee80211_wx_set_encode(bcm->ieee, info, data, extra);

	return err;
}

static int bcm43xx_wx_set_encodingext(struct net_device *net_dev,
                                   struct iw_request_info *info,
                                   union iwreq_data *data,
                                   char *extra)
{
        struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
        int err;

        err = ieee80211_wx_set_encodeext(bcm->ieee, info, data, extra);

        return err;
}

static int bcm43xx_wx_get_encoding(struct net_device *net_dev,
				   struct iw_request_info *info,
				   union iwreq_data *data,
				   char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int err;

	err = ieee80211_wx_get_encode(bcm->ieee, info, data, extra);

	return err;
}

static int bcm43xx_wx_get_encodingext(struct net_device *net_dev,
                                   struct iw_request_info *info,
                                   union iwreq_data *data,
                                   char *extra)
{
        struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
        int err;

        err = ieee80211_wx_get_encodeext(bcm->ieee, info, data, extra);

        return err;
}

static int bcm43xx_wx_set_interfmode(struct net_device *net_dev,
				     struct iw_request_info *info,
				     union iwreq_data *data,
				     char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int mode, err = 0;

	mode = *((int *)extra);
	switch (mode) {
	case 0:
		mode = BCM43xx_RADIO_INTERFMODE_NONE;
		break;
	case 1:
		mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
		break;
	case 2:
		mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
		break;
	case 3:
		mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
		break;
	default:
		printk(KERN_ERR PFX "set_interfmode allowed parameters are: "
				    "0 => None,  1 => Non-WLAN,  2 => WLAN,  "
				    "3 => Auto-WLAN\n");
		return -EINVAL;
	}

587 588
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
589
	if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
590 591 592 593 594 595 596 597 598 599 600
		err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
		if (err) {
			printk(KERN_ERR PFX "Interference Mitigation not "
					    "supported by device\n");
		}
	} else {
		if (mode == BCM43xx_RADIO_INTERFMODE_AUTOWLAN) {
			printk(KERN_ERR PFX "Interference Mitigation mode Auto-WLAN "
					    "not supported while the interface is down.\n");
			err = -ENODEV;
		} else
601
			bcm43xx_current_radio(bcm)->interfmode = mode;
602
	}
603 604
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
605 606 607 608 609 610 611 612 613 614 615 616

	return err;
}

static int bcm43xx_wx_get_interfmode(struct net_device *net_dev,
				     struct iw_request_info *info,
				     union iwreq_data *data,
				     char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int mode;

617
	mutex_lock(&bcm->mutex);
618
	mode = bcm43xx_current_radio(bcm)->interfmode;
619
	mutex_unlock(&bcm->mutex);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

	switch (mode) {
	case BCM43xx_RADIO_INTERFMODE_NONE:
		strncpy(extra, "0 (No Interference Mitigation)", MAX_WX_STRING);
		break;
	case BCM43xx_RADIO_INTERFMODE_NONWLAN:
		strncpy(extra, "1 (Non-WLAN Interference Mitigation)", MAX_WX_STRING);
		break;
	case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
		strncpy(extra, "2 (WLAN Interference Mitigation)", MAX_WX_STRING);
		break;
	default:
		assert(0);
	}
	data->data.length = strlen(extra) + 1;

	return 0;
}

static int bcm43xx_wx_set_shortpreamble(struct net_device *net_dev,
					struct iw_request_info *info,
					union iwreq_data *data,
					char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int on;

	on = *((int *)extra);
649 650
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
651
	bcm->short_preamble = !!on;
652 653
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
654 655 656 657 658 659 660 661 662 663 664 665

	return 0;
}

static int bcm43xx_wx_get_shortpreamble(struct net_device *net_dev,
					struct iw_request_info *info,
					union iwreq_data *data,
					char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int on;

666
	mutex_lock(&bcm->mutex);
667
	on = bcm->short_preamble;
668
	mutex_unlock(&bcm->mutex);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

	if (on)
		strncpy(extra, "1 (Short Preamble enabled)", MAX_WX_STRING);
	else
		strncpy(extra, "0 (Short Preamble disabled)", MAX_WX_STRING);
	data->data.length = strlen(extra) + 1;

	return 0;
}

static int bcm43xx_wx_set_swencryption(struct net_device *net_dev,
				       struct iw_request_info *info,
				       union iwreq_data *data,
				       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	unsigned long flags;
	int on;
	
	on = *((int *)extra);
689

690 691
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
692 693 694
	bcm->ieee->host_encrypt = !!on;
	bcm->ieee->host_decrypt = !!on;
	bcm->ieee->host_build_iv = !on;
695 696
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
697

698 699 700 701 702 703 704 705 706 707
	return 0;
}

static int bcm43xx_wx_get_swencryption(struct net_device *net_dev,
				       struct iw_request_info *info,
				       union iwreq_data *data,
				       char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int on;
708

709
	mutex_lock(&bcm->mutex);
710
	on = bcm->ieee->host_encrypt;
711
	mutex_unlock(&bcm->mutex);
712

713 714 715 716 717
	if (on)
		strncpy(extra, "1 (SW encryption enabled) ", MAX_WX_STRING);
	else
		strncpy(extra, "0 (SW encryption disabled) ", MAX_WX_STRING);
	data->data.length = strlen(extra + 1);
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	return 0;
}

/* Enough buffer to hold a hexdump of the sprom data. */
#define SPROM_BUFFERSIZE	512

static int sprom2hex(const u16 *sprom, char *dump)
{
	int i, pos = 0;

	for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
		pos += snprintf(dump + pos, SPROM_BUFFERSIZE - pos - 1,
				"%04X", swab16(sprom[i]) & 0xFFFF);
	}

	return pos + 1;
}

static int hex2sprom(u16 *sprom, const char *dump, unsigned int len)
{
	char tmp[5] = { 0 };
	int cnt = 0;
	unsigned long parsed;

	if (len < BCM43xx_SPROM_SIZE * sizeof(u16) * 2)
		return -EINVAL;
	while (cnt < BCM43xx_SPROM_SIZE) {
		memcpy(tmp, dump, 4);
		dump += 4;
		parsed = simple_strtoul(tmp, NULL, 16);
		sprom[cnt++] = swab16((u16)parsed);
	}

	return 0;
}

static int bcm43xx_wx_sprom_read(struct net_device *net_dev,
				 struct iw_request_info *info,
				 union iwreq_data *data,
				 char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
761
	int err = -EPERM;
762 763 764 765 766 767 768 769 770 771 772 773
	u16 *sprom;
	unsigned long flags;

	if (!capable(CAP_SYS_RAWIO))
		goto out;

	err = -ENOMEM;
	sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
			GFP_KERNEL);
	if (!sprom)
		goto out;

774 775
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
776
	err = -ENODEV;
777
	if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED)
778
		err = bcm43xx_sprom_read(bcm, sprom);
779 780
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
781 782
	if (!err)
		data->data.length = sprom2hex(sprom, extra);
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	kfree(sprom);
out:
	return err;
}

static int bcm43xx_wx_sprom_write(struct net_device *net_dev,
				  struct iw_request_info *info,
				  union iwreq_data *data,
				  char *extra)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	int err = -EPERM;
	u16 *sprom;
	unsigned long flags;
	char *input;
	unsigned int len;

	if (!capable(CAP_SYS_RAWIO))
		goto out;

	err = -ENOMEM;
	sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
			GFP_KERNEL);
	if (!sprom)
		goto out;

	len = data->data.length;
	extra[len - 1] = '\0';
	input = strchr(extra, ':');
	if (input) {
		input++;
		len -= input - extra;
	} else
		input = extra;
	err = hex2sprom(sprom, input, len);
	if (err)
		goto out_kfree;

821 822 823
	mutex_lock(&bcm->mutex);
	spin_lock_irqsave(&bcm->irq_lock, flags);
	spin_lock(&bcm->leds_lock);
824
	err = -ENODEV;
825
	if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED)
826
		err = bcm43xx_sprom_write(bcm, sprom);
827 828 829
	spin_unlock(&bcm->leds_lock);
	spin_unlock_irqrestore(&bcm->irq_lock, flags);
	mutex_unlock(&bcm->mutex);
830 831 832 833 834 835
out_kfree:
	kfree(sprom);
out:
	return err;
}

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
/* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */

static struct iw_statistics *bcm43xx_get_wireless_stats(struct net_device *net_dev)
{
	struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
	struct ieee80211softmac_device *mac = ieee80211_priv(net_dev);
	struct iw_statistics *wstats;

	wstats = &bcm->stats.wstats;
	if (!mac->associated) {
		wstats->miss.beacon = 0;
//		bcm->ieee->ieee_stats.tx_retry_limit_exceeded = 0; // FIXME: should this be cleared here?
		wstats->discard.retries = 0;
//		bcm->ieee->ieee_stats.tx_discards_wrong_sa = 0; // FIXME: same question
		wstats->discard.nwid = 0;
//		bcm->ieee->ieee_stats.rx_discards_undecryptable = 0; // FIXME: ditto
		wstats->discard.code = 0;
//		bcm->ieee->ieee_stats.rx_fragments = 0;  // FIXME: same here
		wstats->discard.fragment = 0;
		wstats->discard.misc = 0;
		wstats->qual.qual = 0;
		wstats->qual.level = 0;
		wstats->qual.noise = 0;
		wstats->qual.updated = 7;
		wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
			IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
		return wstats;
	}
	/* fill in the real statistics when iface associated */
	wstats->qual.qual = 100;     // TODO: get the real signal quality
	wstats->qual.level = 3 - bcm->stats.link_quality;
	wstats->qual.noise = bcm->stats.noise;
	wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
			IW_QUAL_NOISE_UPDATED;
	wstats->discard.code = bcm->ieee->ieee_stats.rx_discards_undecryptable;
	wstats->discard.retries = bcm->ieee->ieee_stats.tx_retry_limit_exceeded;
	wstats->discard.nwid = bcm->ieee->ieee_stats.tx_discards_wrong_sa;
	wstats->discard.fragment = bcm->ieee->ieee_stats.rx_fragments;
	wstats->discard.misc = 0;	// FIXME
	wstats->miss.beacon = 0;	// FIXME
	return wstats;
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

#ifdef WX
# undef WX
#endif
#define WX(ioctl)  [(ioctl) - SIOCSIWCOMMIT]
static const iw_handler bcm43xx_wx_handlers[] = {
	/* Wireless Identification */
	WX(SIOCGIWNAME)		= bcm43xx_wx_get_name,
	/* Basic operations */
	WX(SIOCSIWFREQ)		= bcm43xx_wx_set_channelfreq,
	WX(SIOCGIWFREQ)		= bcm43xx_wx_get_channelfreq,
	WX(SIOCSIWMODE)		= bcm43xx_wx_set_mode,
	WX(SIOCGIWMODE)		= bcm43xx_wx_get_mode,
	/* Informative stuff */
	WX(SIOCGIWRANGE)	= bcm43xx_wx_get_rangeparams,
	/* Access Point manipulation */
	WX(SIOCSIWAP)           = ieee80211softmac_wx_set_wap,
	WX(SIOCGIWAP)           = ieee80211softmac_wx_get_wap,
	WX(SIOCSIWSCAN)		= ieee80211softmac_wx_trigger_scan,
	WX(SIOCGIWSCAN)		= ieee80211softmac_wx_get_scan_results,
	/* 802.11 specific support */
	WX(SIOCSIWESSID)	= ieee80211softmac_wx_set_essid,
	WX(SIOCGIWESSID)	= ieee80211softmac_wx_get_essid,
	WX(SIOCSIWNICKN)	= bcm43xx_wx_set_nick,
	WX(SIOCGIWNICKN)	= bcm43xx_wx_get_nick,
	/* Other parameters */
	WX(SIOCSIWRATE)		= ieee80211softmac_wx_set_rate,
	WX(SIOCGIWRATE)		= ieee80211softmac_wx_get_rate,
	WX(SIOCSIWRTS)		= bcm43xx_wx_set_rts,
	WX(SIOCGIWRTS)		= bcm43xx_wx_get_rts,
	WX(SIOCSIWFRAG)		= bcm43xx_wx_set_frag,
	WX(SIOCGIWFRAG)		= bcm43xx_wx_get_frag,
	WX(SIOCSIWTXPOW)	= bcm43xx_wx_set_xmitpower,
	WX(SIOCGIWTXPOW)	= bcm43xx_wx_get_xmitpower,
//TODO	WX(SIOCSIWRETRY)	= bcm43xx_wx_set_retry,
//TODO	WX(SIOCGIWRETRY)	= bcm43xx_wx_get_retry,
	/* Encoding */
	WX(SIOCSIWENCODE)	= bcm43xx_wx_set_encoding,
	WX(SIOCGIWENCODE)	= bcm43xx_wx_get_encoding,
	WX(SIOCSIWENCODEEXT)	= bcm43xx_wx_set_encodingext,
	WX(SIOCGIWENCODEEXT)	= bcm43xx_wx_get_encodingext,
	/* Power saving */
//TODO	WX(SIOCSIWPOWER)	= bcm43xx_wx_set_power,
//TODO	WX(SIOCGIWPOWER)	= bcm43xx_wx_get_power,
	WX(SIOCSIWGENIE)	= ieee80211softmac_wx_set_genie,
	WX(SIOCGIWGENIE)	= ieee80211softmac_wx_get_genie,
	WX(SIOCSIWAUTH)		= ieee80211_wx_set_auth,
	WX(SIOCGIWAUTH)		= ieee80211_wx_get_auth,
};
#undef WX

static const iw_handler bcm43xx_priv_wx_handlers[] = {
	/* Set Interference Mitigation Mode. */
	bcm43xx_wx_set_interfmode,
	/* Get Interference Mitigation Mode. */
	bcm43xx_wx_get_interfmode,
	/* Enable/Disable Short Preamble mode. */
	bcm43xx_wx_set_shortpreamble,
	/* Get Short Preamble mode. */
	bcm43xx_wx_get_shortpreamble,
	/* Enable/Disable Software Encryption mode */
	bcm43xx_wx_set_swencryption,
	/* Get Software Encryption mode */
	bcm43xx_wx_get_swencryption,
	/* Write SRPROM data. */
	bcm43xx_wx_sprom_write,
	/* Read SPROM data. */
	bcm43xx_wx_sprom_read,
};

#define PRIV_WX_SET_INTERFMODE		(SIOCIWFIRSTPRIV + 0)
#define PRIV_WX_GET_INTERFMODE		(SIOCIWFIRSTPRIV + 1)
#define PRIV_WX_SET_SHORTPREAMBLE	(SIOCIWFIRSTPRIV + 2)
#define PRIV_WX_GET_SHORTPREAMBLE	(SIOCIWFIRSTPRIV + 3)
#define PRIV_WX_SET_SWENCRYPTION	(SIOCIWFIRSTPRIV + 4)
#define PRIV_WX_GET_SWENCRYPTION	(SIOCIWFIRSTPRIV + 5)
#define PRIV_WX_SPROM_WRITE		(SIOCIWFIRSTPRIV + 6)
#define PRIV_WX_SPROM_READ		(SIOCIWFIRSTPRIV + 7)

#define PRIV_WX_DUMMY(ioctl)	\
	{					\
		.cmd		= (ioctl),	\
		.name		= "__unused"	\
	}

static const struct iw_priv_args bcm43xx_priv_wx_args[] = {
	{
		.cmd		= PRIV_WX_SET_INTERFMODE,
		.set_args	= IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
		.name		= "set_interfmode",
	},
	{
		.cmd		= PRIV_WX_GET_INTERFMODE,
		.get_args	= IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
		.name		= "get_interfmode",
	},
	{
		.cmd		= PRIV_WX_SET_SHORTPREAMBLE,
		.set_args	= IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
978
		.name		= "set_shortpreamb",
979 980 981 982
	},
	{
		.cmd		= PRIV_WX_GET_SHORTPREAMBLE,
		.get_args	= IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
983
		.name		= "get_shortpreamb",
984 985 986 987
	},
	{
		.cmd		= PRIV_WX_SET_SWENCRYPTION,
		.set_args	= IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
988
		.name		= "set_swencrypt",
989 990 991 992
	},
	{
		.cmd		= PRIV_WX_GET_SWENCRYPTION,
		.get_args	= IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
993
		.name		= "get_swencrypt",
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	},
	{
		.cmd		= PRIV_WX_SPROM_WRITE,
		.set_args	= IW_PRIV_TYPE_CHAR | SPROM_BUFFERSIZE,
		.name		= "write_sprom",
	},
	{
		.cmd		= PRIV_WX_SPROM_READ,
		.get_args	= IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | SPROM_BUFFERSIZE,
		.name		= "read_sprom",
	},
};

const struct iw_handler_def bcm43xx_wx_handlers_def = {
	.standard		= bcm43xx_wx_handlers,
	.num_standard		= ARRAY_SIZE(bcm43xx_wx_handlers),
	.num_private		= ARRAY_SIZE(bcm43xx_priv_wx_handlers),
	.num_private_args	= ARRAY_SIZE(bcm43xx_priv_wx_args),
	.private		= bcm43xx_priv_wx_handlers,
	.private_args		= bcm43xx_priv_wx_args,
1014
	.get_wireless_stats	= bcm43xx_get_wireless_stats,
1015
};